Advertisement
serafim7

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

May 15th, 2019 (edited)
1,624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.27 KB | None | 0 0
  1. --[[opencomputers(1.7+) контроль реактора IC2(1.7.10) на конденсаторах
  2.     pastebin.com/kfRBNS0w                              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,"eactor") 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+1).."  износ: "..per.." %")
  109.       if per >= perDamage then
  110.         print("\n".."замените конденсатор в слоте: "..(i+1))
  111.         os.exit()
  112.       end
  113.       table.insert(slotReac, i+1)
  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+1)
  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]-1].damage then
  211.       per = math.ceil(100*data[slotReac[i]-1].damage/data[slotReac[i]-1].maxDamage)
  212.       gpu.setForeground(0xFF9900)
  213.       gpu.set(tonumber(xy[slotReac[i]][1]),tonumber(xy[slotReac[i]][2]),tostring(per))
  214.     else
  215.       per = 0
  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 per >= perDamage or per == 0 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+1, 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.     local sh = 0
  255.     for i = 1,#slotReac do
  256.       if data[slotReac[i]-1].damage then
  257.         sh = sh + 1
  258.       end
  259.     end
  260.     if sh == #slotReac then
  261.       return true
  262.     else
  263.       return false
  264.     end
  265.   end
  266.   if reactor.getHeat() > offReac then
  267.     red.setOutput(sideRed, 0)
  268.     alert("     перегрев реактора !    ")
  269.   elseif not circuitCheck() then
  270.     alert(" нет целых конденсаторов !  ")
  271.   elseif toReac == 0 then
  272.     alert("    в сундуке нет места !   ")
  273.   elseif noFuel >= 5 then
  274.     alert("       нет топлива !        ")
  275.   else
  276.     red.setOutput(sideRed, 15)
  277.     if not reactor.producesEnergy() then
  278.       alert("  реактор не запускается !  ")
  279.     else
  280.       OutputEUt = math.ceil(reactor.getReactorEUOutput())
  281.       gpu.setForeground(0x00FF00)
  282.       gpu.set(1,14,"      eu/t =  "..OutputEUt.."            ")
  283.       gpu.set(1,15,"      реактор активен       ")
  284.     end
  285.   end
  286.   stop(0.7)
  287.   if reactor.producesEnergy() and reactor.getReactorEUOutput() == 0 then
  288.     noFuel = noFuel + 1
  289.   else
  290.     noFuel = 0
  291.     if OutputEUt and OutputEUt < StartEUt then
  292.       lowEu = lowEu + 1
  293.     else
  294.       lowEu = 0
  295.     end
  296.   end
  297.   if noFuel == 3 or lowEu == 3 then
  298.     local data2 = tr.getAllStacks(sideReac).getAll()
  299.     local data3 = tr.getAllStacks(sideInv).getAll()
  300.     for i2 = 0,slotsReac do
  301.       if data2[i2] and data2[i2].name then
  302.         if string.find(data2[i2].name,"depleted") then
  303.           gpu.setForeground(0xFF9900)
  304.           gpu.set(1,15,"      замена стержней       ")
  305.           tr.transferItem(sideReac, sideInv, 1, i2+1)
  306.           for i3 = 0,slotsInv do
  307.             if data3[i3] and data3[i3].name then
  308.               if string.find(data3[i3].name,"MOX") or string.find(data3[i3].name,"Uran") then
  309.                 if not string.find(data3[i3].name,"depleted") then
  310.                   tr.transferItem(sideInv, sideReac, 1, i3+1, i2+1)
  311.                   break
  312.                 end
  313.               end
  314.             end
  315.           end
  316.         end
  317.       end
  318.     end
  319.     StartEUt = math.ceil(reactor.getReactorEUOutput())
  320.     lowEu = 0
  321.     if Reflector and #Reflector > 1 then
  322.       red.setOutput(sideRed, 0)
  323.     end
  324.     for i4 = 1,#Reflector do
  325.       if not data2[Reflector[i4]].name then
  326.         for i5 = 0,slotsInv do
  327.           if data3[i5] and data3[i5].name then
  328.             if string.find(data3[i5].name,"eflector") then
  329.               gpu.setForeground(0xFF9900)
  330.               gpu.set(1,15,"      замена отражателя     ")
  331.               tr.transferItem(sideInv, sideReac, 1, i5+1, Reflector[i4])
  332.             end
  333.           end
  334.         end
  335.       end
  336.     end
  337.   end
  338.   if OutputEUt and OutputEUt > StartEUt then
  339.     StartEUt = math.ceil(reactor.getReactorEUOutput())
  340.   end
  341. end
  342.  
  343. gui()
  344. gpu.setForeground(0xFF9900)
  345. gpu.set(1,14,"  для завершения нажмите E  ")
  346. os.sleep(1)
  347. while run do
  348.   local ok,err = pcall(ReactorControl)
  349.   if not ok then
  350.     red.setOutput(sideRed, 0)
  351.     gpu.setResolution(w, h)
  352.     gpu.setBackground(0x000000)
  353.     gpu.setForeground(0xFFFFFF)
  354.     os.execute("cls")
  355.     print("программа аварийно завершена")
  356.     if type(err) == "table" then
  357.       io.stderr:write(table.unpack(err))
  358.     else
  359.       io.stderr:write(err)
  360.     end
  361.     run = false
  362.   end
  363. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement