Advertisement
taoshi

nuc_control for nuclear reactors on LZH

Jun 18th, 2024
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.17 KB | Gaming | 0 0
  1. local lapis_minimum = 30000
  2. local lapis_recomended = 90000
  3. local lapis_precraft_size = 30000
  4. local component = require('component')
  5. local keyboard = require('keyboard')
  6. local gpu = component.gpu
  7. local modem = component.modem
  8. local event = require('event')
  9. local math = require('math')
  10. local nuc, me, xr, yr = nil
  11. local fingerprint = {name='minecraft:lapis_block',damage=0}
  12. local isWork = true
  13. local nucWork, producesEnergy, totalEUfromNucs = 0, 0, 0
  14. local preCraft = false
  15. local inProcess = false
  16. local isDone,isCanceled = true,false
  17. local request = nil
  18.  
  19. modem.open(1)
  20. modem.setWakeMessage('Wake!')
  21.  
  22. function getDevice(name)
  23. name=name or 'computer'  
  24. local device={}
  25.   local c=component.list()
  26.   for k,v in pairs(c) do if string.find(v,name) then
  27.     device[#device+1]=component.proxy(k) end
  28.   end
  29.   return device
  30. end
  31.  
  32. local lapis = getDevice('me_')[1].getCraftables(fingerprint)[1]
  33. if lapis and lapis.request then
  34.   request = lapis.request
  35.   preCraft = true
  36. end
  37.  
  38. function nuc_off()
  39.     for num in pairs(nuc) do
  40.         nuc[num].stopReactor()
  41.     end
  42.     return false
  43. end
  44.  
  45. function nuc_on()
  46.     for num in pairs(nuc) do
  47.         nuc[num].startReactor()
  48.     end
  49.     return true
  50. end
  51.  
  52. function is_nuc_work()
  53.     local output = nil
  54.   nucWork, producesEnergy, totalEUfromNucs = 0, 0, 0
  55.     for num in pairs(nuc) do
  56.         gpu.set(xr-26,4,'Проверка реактора '..num..'       ')
  57.         if nuc[num].isReactorWorking() then
  58.             nucWork = nucWork+1
  59.             output = nuc[num].getReactorEUOutput()
  60.             if output > 0 then
  61.                 producesEnergy = producesEnergy + 1
  62.                 totalEUfromNucs = totalEUfromNucs + output
  63.             end
  64.         end
  65.  
  66.     end
  67.     gpu.fill(xr-26,4,26,1,' ')
  68. end
  69.  
  70. function lapis_available()
  71.     local lapis, cooldown, second = me.getItemsInNetwork(fingerprint)[1].size
  72.     if not lapis then
  73.     local timer = second.sub(second,1,#second-1)+1
  74.     while timer >= 0 do os.sleep(1)
  75.       timer = timer - 1
  76.       gpu.set(xr-26,5,cooldown .. second)
  77.     end
  78.         return lapis_available()
  79.     end
  80.      return lapis
  81. end
  82.  
  83. function lapisPreCraft()
  84.  
  85.   return request(lapis_precraft_size)
  86. end
  87.  
  88. function nuc_control()
  89.     xr, yr = gpu.getResolution()
  90.     nuc = getDevice('reactor_chamber')
  91.     me = getDevice('me_')[1]
  92.    
  93.   is_nuc_work()
  94.     local text = nucWork .. '/' .. #nuc
  95.     gpu.set(xr-26,1,'Реакторов включено: ' .. text)
  96.     text = nucWork - producesEnergy.. '/' .. nucWork
  97.     gpu.set(xr-26,2,'Из них без топлива: ' .. text)
  98.     text = tostring(math.floor(totalEUfromNucs))
  99.     gpu.set(xr-26,3,'EU реакторов:      ' .. text)
  100.  
  101.     local lapis = lapis_available()
  102.   gpu.set(xr-26,5,'Блоков лазурита: ' .. lapis..'    ')
  103.   --Выключение при малом количестве лазурита
  104.   if isWork and lapis < lapis_minimum then
  105.     gpu.set(xr-26,4,'Мало лазурита. Выключение')
  106.     isWork = nuc_off()
  107.     gpu.set(xr-26,4,'Реакторы выключены        ')
  108.   end
  109.   --Включение пр достаточном количестве лазурита
  110.   if not isWork and lapis >= lapis_recomended then
  111.     gpu.set(xr-26,4,'Включение...              ')
  112.     isWork = nuc_on()
  113.     gpu.set(xr-26,4,'Реакторы включены        ')
  114.   end
  115.   --Докрафт при малом количестве лазурита
  116.   if preCraft and lapis <= lapis_recomended then
  117.     if not inProcess then
  118.       gpu.set(xr-26,4,'Докрафт лазуритовых блоков')
  119.       inProcess = true
  120.       local craft=lapisPreCraft()
  121.       isDone = craft.isDone
  122.       isCanceled = craft.isCanceled
  123.     else
  124.       --inProgress = not isDone()
  125.     end
  126.  
  127.     if isDone() and inProcess then
  128.       gpu.set(xr-26,4,'Докрафт лазурита завершён')
  129.       inProcess = false
  130.     else
  131.       if inProcess and isCanceled() then
  132.         gpu.set(xr-26,4,'Неудачная попытка докрафта')
  133.       end
  134.     end
  135.   end
  136.  
  137.     return true
  138. end
  139.  
  140. local nucMonitoringID=event.timer(59,nuc_control,math.huge)
  141.  
  142. while true do --absolutely nothing
  143.   event.pull(1.9)
  144.   if keyboard.isKeyDown(keyboard.keys.delete) then  
  145.     event.cancel(nucMonitoringID)
  146.     nucMonitoringID=nil
  147.     break
  148.   end
  149.   return true
  150. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement