serafim7

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

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