abc123mewot

Fusion powerplant monitor

Dec 17th, 2023 (edited)
780
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.02 KB | None | 0 0
  1. local mainMonitorSide = 'monitor_1'
  2. local laserPowerSide = 'bottom'
  3. local laserPulseSide = 'top'
  4. local hohlraumSide = 'north'
  5. local reactorInjectionRate = 4
  6.  
  7. local mon = peripheral.wrap(mainMonitorSide)
  8. local ria = peripheral.wrap('redstone_integrator_1')
  9. local dtn = peripheral.wrap('Elite Gas Tank_2')
  10. local ttn = peripheral.wrap('Elite Gas Tank_3')
  11.  
  12. local ind = peripheral.find('Induction Matrix')
  13. local amp = peripheral.find('Laser Amplifier')
  14. local rla = peripheral.find('Reactor Logic Adapter')
  15. local trb = peripheral.find('Industrial Turbine')
  16.  
  17. local monW, monH = mon.getSize()
  18.  
  19. print("Reactor monitoring script")
  20.  
  21. mon.setTextScale(1.25)
  22. mon.setBackgroundColor(colors.black)
  23. local monW, monH = mon.getSize()
  24. if not fs.exists("ui.lua") then
  25.     print("Downloadind UI API")
  26.     shell.run("pastebin get W13Fx2tv ui.lua")
  27. end
  28. if not fs.exists("buttons.lua") then
  29.     print("Downloadind Buttons API")
  30.     shell.run("pastebin get 3nqSJzqc buttons.lua")
  31. end
  32.  
  33. os.loadAPI("ui.lua")
  34. local ui = ui.initUI(mon)
  35.  
  36. os.loadAPI("buttons.lua")
  37. local reactorQuenchBtn = nil
  38. local reactorIgniteBtn = nil
  39. local increaseInjectionBtn = nil
  40. local decreaseInjectionBtn = nil
  41.  
  42. local autoIgnited = false
  43. local autoQuenched = false
  44.  
  45. info = {}
  46. local function collectInfo()
  47.     info.dPct = math.floor((100 * dtn.getStoredGas()) / dtn.getMaxGas())
  48.     info.tPct = math.floor((100 * ttn.getStoredGas()) / ttn.getMaxGas())
  49.     info.cTemp, info.pTemp, info.iTemp = math.floor(rla.getCaseHeat() / 1000000), math.floor(rla.getPlasmaHeat() / 1000000), math.floor(rla.getIgnitionTemp() / 1000000)
  50.     info.injRate = rla.getInjectionRate()
  51.     info.ignited = rla.isIgnited()
  52.     info.hasFuel = rla.hasFuel()
  53.     info.laserEnergyMRF = math.floor(amp.getEnergy() * 0.0000004)
  54.     info.indPct = math.ceil(100.0 * (ind.getEnergy() / ind.getMaxEnergy()))
  55. end
  56. local function igniteReactor()
  57.     ria.setOutput(hohlraumSide, true)
  58.     rla.setInjectionRate(reactorInjectionRate)
  59.     os.sleep(1)
  60.     ria.setOutput(laserPulseSide, true)
  61.     os.sleep(0.3)
  62.     ria.setOutput(hohlraumSide, false)
  63.     ria.setOutput(laserPulseSide, false)
  64. end
  65. local function quenchReactor()
  66.     rla.setInjectionRate(0)
  67. end
  68. local function drawReactor()
  69.     local cx, cy = mon.getCursorPos()
  70.     mon.setTextColor(colors.yellow)
  71.     mon.setBackgroundColor(colors.black)
  72.    
  73.     cy = cy + 1
  74.     mon.setCursorPos(cx, cy)
  75.     local disabled = true
  76.     if rla.isIgnited() then
  77.         mon.setTextColor(colors.green)
  78.         mon.write("REACTOR ONLINE ")
  79.         autoIgnited = false
  80.         if not autoQuenched and info.indPct > 95 then
  81.             print("Autoquenching reactor!")
  82.             autoQuenched = true
  83.             quenchReactor()
  84.         end
  85.     else
  86.         mon.setTextColor(colors.red)
  87.         mon.write("REACTOR OFFLINE")
  88.         autoQuenched = false
  89.         if not autoIgnited and info.indPct < 20 then
  90.             print("Autoigniting reactor")
  91.             autoIgnited = true
  92.             igniteReactor()
  93.         end
  94.     end
  95.  
  96.     cy = cy + 1
  97.    
  98.     cy = cy + 1 mon.setCursorPos(cx, cy) mon.setTextColor(colors.yellow)  mon.write("Injection: ")
  99.     if info.injRate < 2 then mon.setTextColor(colors.red) elseif info.injRate < 4 then mon.setTextColor(colors.yellow) else mon.setTextColor(colors.green) end
  100.     mon.write(info.injRate .. "mB/t")
  101.    
  102.     if increaseInjectionBtn then increaseInjectionBtn.enabled = info.ignited and info.hasFuel else
  103.         increaseInjectionBtn = buttons.createButton(cx, cy, buttons.createProceduralStyle(
  104.             buttons.createStyle("+2mB/t Injection Rate", colors.lightGray, colors.green, 1, 1), -- Enabled
  105.             buttons.createStyle("+2mB/t Injection Rate", colors.gray, colors.black, 1, 1)),     -- Disabled
  106.             false, function()
  107.                 rla.setInjectionRate(reactorInjectionRate + 2)
  108.                 reactorInjectionRate = rla.getInjectionRate()
  109.             end)
  110.         buttons.addButton(increaseInjectionBtn)
  111.     end buttons.monitorDrawButton(mon, increaseInjectionBtn)
  112.     cy = cy + 4
  113.    
  114.     if decreaseInjectionBtn then decreaseInjectionBtn.enabled = info.ignited and info.hasFuel and reactorInjectionRate > 2 else
  115.         decreaseInjectionBtn = buttons.createButton(cx, cy, buttons.createProceduralStyle(
  116.             buttons.createStyle("-2mB/t Injection Rate", colors.lightGray, colors.red, 1, 1), -- Enabled
  117.             buttons.createStyle("-2mB/t Injection Rate", colors.gray, colors.black, 1, 1)),   -- Disabled
  118.             false, function()
  119.                 rla.setInjectionRate(reactorInjectionRate - 2)
  120.                 reactorInjectionRate = rla.getInjectionRate()
  121.             end)
  122.         buttons.addButton(decreaseInjectionBtn)
  123.     end buttons.monitorDrawButton(mon, decreaseInjectionBtn)
  124.    
  125.     if reactorIgniteBtn then reactorIgniteBtn.enabled = (not info.ignited) and (info.hasFuel) and (info.laserEnergyMRF > 500) else
  126.         reactorIgniteBtn = buttons.createButton(2, monH - 3, buttons.createProceduralStyle(
  127.             buttons.createStyle("IGNITE", colors.lightGray, colors.yellow, 1, 1), -- Enabled
  128.             buttons.createStyle("IGNITE", colors.gray, colors.black, 1, 1)),      -- Disabled
  129.             false, function()
  130.                 if reactorIgniteBtn.enabled then
  131.                     reactorIgniteBtn.s.pStyle.dispTxt = " WAIT "
  132.                     buttons.monitorDrawButton(mon, reactorIgniteBtn)
  133.                     igniteReactor()
  134.                     reactorIgniteBtn.s.pStyle.dispTxt = "IGNITE"
  135.                     reactorIgniteBtn.enabled = false
  136.                     buttons.monitorDrawButton(mon, reactorIgniteBtn)
  137.                     print("Reactor ignited!")
  138.                 end
  139.                 print("Reactor ignite button handled!")
  140.             end)
  141.         buttons.addButton(reactorIgniteBtn)
  142.     end buttons.monitorDrawButton(mon, reactorIgniteBtn)
  143.    
  144.     if reactorQuenchBtn then reactorQuenchBtn.enabled = info.ignited else
  145.         reactorQuenchBtn = buttons.createButton(12, monH - 3, buttons.createProceduralStyle(
  146.             buttons.createStyle("QUENCH", colors.lightGray, colors.red, 1, 1),    -- Enabled
  147.             buttons.createStyle("QUENCH", colors.gray, colors.black, 1, 1)),      -- Disabled
  148.             false, function()
  149.                 if reactorQuenchBtn.enabled then
  150.                     quenchReactor()
  151.                     reactorIgniteBtn.enabled = false
  152.                     print("Reactor quenched!")
  153.                 end
  154.                 print("Reactor quench button handled!")
  155.             end)
  156.         buttons.addButton(reactorQuenchBtn)
  157.     end buttons.monitorDrawButton(mon, reactorQuenchBtn)
  158. end
  159. local function drawLaser()
  160.     local cx, cy = mon.getCursorPos()
  161.     mon.setTextColor(colors.yellow)
  162.     mon.setBackgroundColor(colors.black)
  163.    
  164.     cy = cy + 1
  165.     mon.setCursorPos(cx, cy)
  166.     mon.write("Laser: ")
  167.     local charged = info.laserEnergyMRF > 500
  168.     if charged then mon.setTextColor(colors.green) elseif info.laserEnergyMRF > 250 then mon.setTextColor(colors.yellow) else mon.setTextColor(colors.red) end
  169.     mon.write(info.laserEnergyMRF .. " / 500MRF ")
  170.     if charged then
  171.         mon.setTextColor(colors.green)
  172.         mon.write("Charged!")
  173.         ria.setOutput(laserPowerSide, false)
  174.     else
  175.         mon.setTextColor(colors.yellow)
  176.         mon.write("Charging!")
  177.         ria.setOutput(laserPowerSide, true)
  178.     end
  179.    
  180.     cy = cy + 1
  181.    
  182.     cy = cy + 1 mon.setCursorPos(cx, cy)  mon.setTextColor(colors.yellow) mon.write("Casing temp: ")
  183.     if info.cTemp < 50 then mon.setTextColor(colors.red) elseif info.cTemp < 100 then mon.setTextColor(colors.yellow) else mon.setTextColor(colors.green) end
  184.     mon.write(info.cTemp .. "MK")
  185.    
  186.     cy = cy + 1 mon.setCursorPos(cx, cy) mon.setTextColor(colors.yellow)  mon.write("Plasma temp: ")
  187.     if info.pTemp < info.iTemp then mon.setTextColor(colors.red) else mon.setTextColor(colors.green) end
  188.     mon.write(info.pTemp .. "/" .. info.iTemp .. "MK")
  189.    
  190.     cy = cy + 1
  191.    
  192.     cy = cy + 1 mon.setCursorPos(cx, cy) mon.setTextColor(colors.yellow) mon.write("Deuterium reserve: ")
  193.     if info.dPct < 90 then mon.setTextColor(colors.red) else mon.setTextColor(colors.green) end mon.write(info.dPct .. "%")
  194.     cy = cy + 1 mon.setCursorPos(cx, cy) mon.setTextColor(colors.yellow) mon.write("Tritium reserve: ")
  195.     if info.tPct < 90 then mon.setTextColor(colors.red) else mon.setTextColor(colors.green) end mon.write(info.tPct .. "%")
  196.    
  197.     cy = cy + 1
  198.    
  199.     cy = cy + 1 mon.setCursorPos(cx, cy) mon.setTextColor(colors.yellow) mon.write("Injection rate: ")
  200.     if info.injRate < 2 then mon.setTextColor(colors.red) elseif info.injRate < 4 then mon.setTextColor(colors.yellow) else mon.setTextColor(colors.green) end mon.write(info.injRate .. "mB/t")
  201. end
  202.  
  203. local loopTimer = os.startTimer(0.3)
  204. local blinky = false
  205. while true do
  206.     local event, param1, param2, param3, param4 = os.pullEvent()
  207.     local wasTouched = event == "monitor_touch" and param1 == mainMonitorSide
  208.     local wasTimer = event == "timer" and param1 == loopTimer
  209.     if wasTouched or wasTimer then -- Touchscreen or timer event!
  210.         loopTimer = os.startTimer(0.3)
  211.         collectInfo()
  212.         mon.clear()
  213.         if wasTouched then
  214.             buttons.onTouch(mon, param2, param3)
  215.             loopTimer = os.startTimer(0.3)
  216.         end
  217.         mon.setBackgroundColor(colors.gray)
  218.         mon.setCursorPos(1, 1)
  219.         mon.setTextColor(colors.white)
  220.         ui.spacer(1, "Systems overview")
  221.         ui.spacer(2, "")
  222.         mon.setCursorPos(monW, 1)
  223.         mon.setTextColor(colors.black)
  224.         mon.setBackgroundColor(colors.gray)
  225.         if blinky then mon.write("\\") blinky = false else mon.write("/") blinky = true end
  226.         mon.setTextColor(colors.white)
  227.        
  228.         local monW2 = monW / 2
  229.         mon.setCursorPos(2, 3)
  230.         drawReactor()
  231.        
  232.         mon.setBackgroundColor(colors.gray)
  233.         mon.setCursorPos(monW2 - 1, 3) ui.drawVbar(3, monH)
  234.         mon.setCursorPos(monW2, 3) ui.drawVbar(3, monH)
  235.        
  236.         mon.setCursorPos(monW2 + 2, 3)
  237.         drawLaser()
  238.     end
  239. end
  240.  
Advertisement
Add Comment
Please, Sign In to add comment