serafim7

контроль реактора IC2(1.12.2) на конденсаторах [OpenComputers]

Jul 30th, 2020 (edited)
482
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.11 KB | None | 0 0
  1. --[[opencomputers(1.7+) контроль реактора IC2(1.12.2) на конденсаторах
  2.     pastebin.com/nneD0Mv5                              update 22.06.21
  3.  
  4. для проекта computercraft.ru  
  5. https://computercraft.ru/topic/2604-programma-ochen-mnogo-elektrichestva
  6. авторы: Asior, serafim
  7.  
  8. требования:
  9. монитор и видеокарта 2-го уровня,
  10. адаптер, транспозер, контроллер красного камня.
  11.  
  12. пример сборки:
  13. https://i.imgur.com/Zv24ihq.png
  14. https://disk.yandex.ua/d/MGMzFwD8jyaWiw/reactor_control.mp4
  15.  
  16. схема реактора:
  17. https://i.imgur.com/SXbeOu8.png
  18. https://i.imgur.com/OW27suK.png
  19.  
  20. использование:
  21. собрать установку, для плутония нагреть реактор до 8400 градусов,
  22. в сундук положить запасные конденсаторы и стержни,
  23. желательно обеспечить автокрафт перезарядки конденсаторов
  24. https://pastebin.com/WgcCKBFn
  25. ]]--
  26.  
  27. local offReac = 8400 --температура перегрева реактора до отключения
  28. local perDamage = 95 --процент износа конденсатора при котором он меняется
  29.  
  30. local com = require("component")
  31. local computer = require("computer")
  32. local event = require("event")
  33. local gpu = com.gpu
  34. local w,h = gpu.getResolution()
  35. local per,noFuel,lowEu,toReac,run = 0,0,0,1,true
  36. local sideReac,sideInv,sideRed,OutputEUt,StartEUt
  37. local slotReac,Reflector = {},{}
  38.  
  39. if tonumber(string.format('%d%d%d',_OSVERSION:match("(%d).(%d).(%d)"))) < 172 then
  40.   print("обнаружен ".._OSVERSION.."\n".."требуется версия 1.7 +")
  41.   os.exit()
  42. end
  43.  
  44. if not com.isAvailable("transposer") then
  45.   print("нет транспозера")
  46.   os.exit()
  47. end
  48. local tr = com.transposer
  49.  
  50. if not com.isAvailable("redstone") then
  51.   print("нет контроллера красного камня")
  52.   os.exit()
  53. end
  54. local red = com.redstone
  55.  
  56. if not com.isAvailable("reactor_chamber") then
  57.   print("камера реактора не найдена")
  58.   os.exit()
  59. end
  60. local reactor = com.reactor_chamber
  61.  
  62. if reactor.producesEnergy() then
  63.   print("\n".."остановите реактор !")
  64.   for i = 0,5 do
  65.     if red.getOutput(i) > 0 then
  66.       red.setOutput(i, 0)
  67.     end
  68.   end
  69.   os.exit()
  70. end
  71.  
  72. if reactor.getHeat() > offReac then
  73.   print("\n".."перегрев реактора !")
  74.   os.exit()
  75. end
  76.  
  77. print("поиск реактора и сундука")
  78. for i = 0,5 do
  79.   local vr = tr.getInventoryName(i)
  80.   if vr then
  81.     if string.find(vr,"ic2:te") then
  82.       print("реактор в стороне: "..i)
  83.       sideReac = i
  84.     end
  85.     if string.find(vr,"hest") then
  86.       print("сундук в стороне:  "..i)
  87.       sideInv = i
  88.     end
  89.   end
  90. end
  91. if not sideReac then
  92.   print("\n".."камера реактора не найдена")
  93.   os.exit()
  94. end
  95. if not sideInv then
  96.   print("\n".."сундук не найден")
  97.   os.exit()
  98. end
  99. local slotsReac = tr.getInventorySize(sideReac)
  100. local slotsInv = tr.getInventorySize(sideInv)
  101.  
  102. print("сохранение конденсаторов")
  103. local data = tr.getAllStacks(sideReac).getAll()
  104. for i = 0,slotsReac do
  105.   if data[i] and data[i].name then
  106.     if string.find(data[i].name,"ondensator") then
  107.       local per = math.ceil(100*data[i].damage/data[i].maxDamage)
  108.       print("слот: "..(i).."  износ: "..per.." %")
  109.       if per >= perDamage then
  110.         print("\n".."замените конденсатор в слоте: "..(i))
  111.         os.exit()
  112.       end
  113.       table.insert(slotReac, i)
  114.     end
  115.   end
  116. end
  117. print("сохранение отражателей")
  118. for i = 0,slotsReac do
  119.   if data[i] and data[i].name then
  120.     if string.find(data[i].name,"eflector") then
  121.       print("слот: "..(i))
  122.       table.insert(Reflector, i)
  123.     end
  124.   end
  125. end
  126.  
  127. print("пробный запуск")
  128. for k,n in pairs({3,2,4,5,0,1}) do
  129.   red.setOutput(n, 15)
  130.   if reactor.producesEnergy() then
  131.     os.sleep(1)
  132.     StartEUt = math.ceil(reactor.getReactorEUOutput())
  133.     print("StartEUt = "..StartEUt)
  134.     sideRed = n
  135.     red.setOutput(n, 0)
  136.     print("редстоун в стороне: "..sideRed)
  137.     break
  138.   else
  139.     red.setOutput(n, 0)
  140.   end
  141.   if k == 6 then
  142.     print("\n".."реактор не запускается")
  143.     os.exit()
  144.   end
  145. end
  146.  
  147. print("настройка завершена".."\n".."старт...")
  148. os.sleep(2)
  149.  
  150. local function gui()
  151.   gpu.setResolution(28,15)
  152.   gpu.setBackground(0x000000)
  153.   gpu.fill(1,1,28,15," ")
  154.   gpu.setForeground(0x669999)
  155.   gpu.set(1,1,"┌──────────────────────────┐")
  156.   gpu.set(1,2,"│  │  │  │  │  │  │  │  │  │")
  157.   gpu.set(1,3,"│──────────────────────────│")
  158.   gpu.set(1,4,"│  │  │  │  │  │  │  │  │  │")
  159.   gpu.set(1,5,"│──────────────────────────│")
  160.   gpu.set(1,6,"│  │  │  │  │  │  │  │  │  │")
  161.   gpu.set(1,7,"│──────────────────────────│")
  162.   gpu.set(1,8,"│  │  │  │  │  │  │  │  │  │")
  163.   gpu.set(1,9,"│──────────────────────────│")
  164.  gpu.set(1,10,"│  │  │  │  │  │  │  │  │  │")
  165.  gpu.set(1,11,"│──────────────────────────│")
  166.  gpu.set(1,12,"│  │  │  │  │  │  │  │  │  │")
  167.  gpu.set(1,13,"└──────────────────────────┘")
  168. end
  169.  
  170. local xy = {
  171. {"2","2"},{"5","2"},{"8","2"},{"11","2"},{"14","2"},{"17","2"},{"20","2"},{"23","2"},{"26","2"},
  172. {"2","4"},{"5","4"},{"8","4"},{"11","4"},{"14","4"},{"17","4"},{"20","4"},{"23","4"},{"26","4"},
  173. {"2","6"},{"5","6"},{"8","6"},{"11","6"},{"14","6"},{"17","6"},{"20","6"},{"23","6"},{"26","6"},
  174. {"2","8"},{"5","8"},{"8","8"},{"11","8"},{"14","8"},{"17","8"},{"20","8"},{"23","8"},{"26","8"},
  175. {"2","10"},{"5","10"},{"8","10"},{"11","10"},{"14","10"},{"17","10"},{"20","10"},{"23","10"},{"26","10"},
  176. {"2","12"},{"5","12"},{"8","12"},{"11","12"},{"14","12"},{"17","12"},{"20","12"},{"23","12"},{"26","12"},
  177. }
  178.  
  179. local function stop(wait)
  180.   local e = ({event.pull(wait,"key_down")})[4]
  181.   if e == 18 or e == 20 then
  182.     red.setOutput(sideRed, 0)
  183.     gpu.setResolution(w, h)
  184.     gpu.setBackground(0x000000)
  185.     gpu.setForeground(0xFFFFFF)
  186.     gpu.fill(1, 1, w, h, " ")
  187.     print("программа завершена")
  188.     os.sleep(1)
  189.     if reactor.producesEnergy() then
  190.       print("ВНИМАНИЕ реактор по прежнему активен !!!")
  191.     else
  192.       print("реактор остановлен")
  193.     end
  194.     run = false
  195.   end
  196. end
  197.  
  198. local function alert(message)
  199.   gpu.setForeground(0xFF9900)
  200.   gpu.set(1,14,"  для завершения нажмите E  ")
  201.   gpu.setForeground(0xFF0000)
  202.   gpu.set(1,15,message)
  203.   computer.beep(500, 1)
  204.   stop(3)
  205. end
  206.  
  207. local function ReactorControl()
  208.   local data = tr.getAllStacks(sideReac).getAll()
  209.   for i = 1,#slotReac do
  210.     if data[slotReac[i]].name == "minecraft:air" then
  211.       per = nil
  212.     elseif data[slotReac[i]].damage then
  213.       per = math.ceil(100*data[slotReac[i]].damage/data[slotReac[i]].maxDamage)
  214.       gpu.setForeground(0xFF9900)
  215.       gpu.set(tonumber(xy[slotReac[i]][1]),tonumber(xy[slotReac[i]][2]),tostring(per))
  216.     end
  217.     if per == 100 then
  218.       red.setOutput(sideRed, 0)
  219.       alert(" снизте % замены конденсат. ")
  220.       while run do
  221.         computer.beep(500, 1)
  222.         stop(3)
  223.       end
  224.       os.exit()
  225.     end
  226.     if not per or per >= perDamage then
  227.       gpu.setForeground(0xFF9900)
  228.       gpu.set(1,15,"    замена конденсаторов    ")
  229.       if red.getOutput(sideRed) > 0 then
  230.         red.setOutput(sideRed, 0)
  231.         os.sleep(0.5)
  232.       end
  233.       tr.transferItem(sideReac, sideInv, 1, slotReac[i])
  234.       gpu.setForeground(0xFF0000)
  235.       gpu.set(tonumber(xy[slotReac[i]][1]),tonumber(xy[slotReac[i]][2]),"██")
  236.       local data1 = tr.getAllStacks(sideInv).getAll()
  237.       for i1 = 0,slotsInv do
  238.         if data1[i1] and data1[i1].name then
  239.           local per = math.ceil(100*data1[i1].damage/data1[i1].maxDamage)
  240.           if string.find(data1[i1].name,"ondensator") and per < 90 then
  241.             toReac = tr.transferItem(sideInv, sideReac, 1, i1, slotReac[i])
  242.             gpu.setForeground(0x00FF00)
  243.             gpu.set(tonumber(xy[slotReac[i]][1]),tonumber(xy[slotReac[i]][2]),"██")
  244.             break
  245.           else
  246.             toReac = 0
  247.           end
  248.         end
  249.       end
  250.     end
  251.   end
  252.   local function circuitCheck()
  253.     local data = tr.getAllStacks(sideReac).getAll()
  254.     for i = 1,#slotReac do
  255.       if data[slotReac[i]].name == "minecraft:air" then
  256.         return false
  257.       end
  258.     end
  259.     return true
  260.   end
  261.   if reactor.getHeat() > offReac then
  262.     red.setOutput(sideRed, 0)
  263.     alert("     перегрев реактора !    ")
  264.   elseif not circuitCheck() then
  265.     alert(" нет целых конденсаторов !  ")
  266.   elseif toReac == 0 then
  267.     alert("    в сундуке нет места !   ")
  268.   elseif noFuel >= 5 then
  269.     alert("       нет топлива !        ")
  270.   else
  271.     red.setOutput(sideRed, 15)
  272.     if not reactor.producesEnergy() then
  273.       alert("  реактор не запускается !  ")
  274.     else
  275.       OutputEUt = math.ceil(reactor.getReactorEUOutput())
  276.       gpu.setForeground(0x00FF00)
  277.       gpu.set(1,14,"      eu/t =  "..OutputEUt.."            ")
  278.       gpu.set(1,15,"      реактор активен       ")
  279.     end
  280.   end
  281.   stop(0.7)
  282.   if reactor.producesEnergy() and reactor.getReactorEUOutput() == 0 then
  283.     noFuel = noFuel + 1
  284.   else
  285.     noFuel = 0
  286.     if OutputEUt and OutputEUt < StartEUt then
  287.       lowEu = lowEu + 1
  288.     else
  289.       lowEu = 0
  290.     end
  291.   end
  292.   if noFuel == 3 or lowEu == 3 then
  293.     local data2 = tr.getAllStacks(sideReac).getAll()
  294.     local data3 = tr.getAllStacks(sideInv).getAll()
  295.     for i2 = 0,slotsReac do
  296.       if data2[i2] and data2[i2].name then
  297.         if string.find(data2[i2].name,"nuclear") then
  298.           gpu.setForeground(0xFF9900)
  299.           gpu.set(1,15,"      замена стержней       ")
  300.           tr.transferItem(sideReac, sideInv, 1, i2)
  301.           for i3 = 0,slotsInv do
  302.             if data3[i3] and data3[i3].name then
  303.               if string.find(data3[i3].name,"fuel_rod") then
  304.                 tr.transferItem(sideInv, sideReac, 1, i3, i2)
  305.                 break
  306.               end
  307.             end
  308.           end
  309.         end
  310.       end
  311.     end
  312.     StartEUt = math.ceil(reactor.getReactorEUOutput())
  313.     lowEu = 0
  314.     if Reflector and #Reflector > 1 then
  315.       red.setOutput(sideRed, 0)
  316.     end
  317.     for i4 = 1,#Reflector do
  318.       if data2[Reflector[i4]].name == "minecraft:air" then
  319.         for i5 = 0,slotsInv do
  320.           if data3[i5] and data3[i5].name then
  321.             if string.find(data3[i5].name,"eflector") then
  322.               gpu.setForeground(0xFF9900)
  323.               gpu.set(1,15,"      замена отражателя     ")
  324.               tr.transferItem(sideInv, sideReac, 1, i5, Reflector[i4])
  325.             end
  326.           end
  327.         end
  328.       end
  329.     end
  330.   end
  331.   if OutputEUt and OutputEUt > StartEUt then
  332.     StartEUt = math.ceil(reactor.getReactorEUOutput())
  333.   end
  334. end
  335.  
  336. gui()
  337. gpu.setForeground(0xFF9900)
  338. gpu.set(1,14,"  для завершения нажмите E  ")
  339. os.sleep(1)
  340. while run do
  341.   local ok,err = pcall(ReactorControl)
  342.   if not ok then
  343.     red.setOutput(sideRed, 0)
  344.     gpu.setResolution(w, h)
  345.     gpu.setBackground(0x000000)
  346.     gpu.setForeground(0xFFFFFF)
  347.     os.execute("cls")
  348.     print("программа аварийно завершена")
  349.     if type(err) == "table" then
  350.       io.stderr:write(table.unpack(err))
  351.     else
  352.       io.stderr:write(err)
  353.     end
  354.     run = false
  355.   end
  356. end
Add Comment
Please, Sign In to add comment