SHARE
TWEET

reactor.lua

a guest Jun 19th, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local running = true
  2. local term = require("term")
  3. local gpu = term.gpu()
  4. local component = require("component")
  5. local reactor = component.nc_fission_reactor
  6. local thread = require("thread")
  7.  
  8. if not reactor then
  9.   io.write("No fission reactor found!")
  10.   return
  11. elseif not reactor.isComplete() then
  12.   io.write("Reactor is incomplete!")
  13.   return
  14. end
  15.  
  16. local x,y = term.gpu().getResolution()
  17. term.clear()
  18. gpu.set(2,2," ------ REACTOR CONTROL")
  19. term.setCursor(1,y)
  20.  
  21. local auto = false
  22.  
  23. local function update()
  24.   local heatlevel = reactor.getHeatLevel()
  25.   local maxheat = reactor.getMaxHeatLevel()
  26.   if heatlevel > maxheat then
  27.     status = "MELTDOWN!!!"
  28.   elseif heatlevel > 0 then
  29.     local percent = math.floor((heatlevel / maxheat)*100)
  30.     if percent > 75 then
  31.       status = "HEAT!!"
  32.       reactor.deactivate()
  33.     elseif percent > 50 then
  34.       status = "HEAT! "
  35.     else
  36.       status = "HEAT  "
  37.     end
  38.     status = status .. (" (%d)"):format(percent)
  39.   elseif reactor.getReactorProcessPower() < 0 then
  40.     status = "ENERGY PRODUCTION NEGATIVE"
  41.   elseif (reactor.getEnergyStored()/reactor.getMaxEnergyStored()) >= .9 then
  42.     status = "CAPACITOR FULL"
  43.     if auto then
  44.       reactor.deactivate()
  45.     end
  46.   elseif reactor.isProcessing() then
  47.     status = "OK"
  48.   elseif (reactor.getEnergyStored()/reactor.getMaxEnergyStored()) <= .1 then
  49.     status = "CAPACITOR LOW/EMPTY"
  50.     if auto then
  51.       reactor.activate()
  52.     end
  53.   else
  54.     status = "OFFLINE"
  55.   end
  56.   if auto then
  57.     gpu.set(2,3, "AUTO CONTROL ON ")
  58.   else
  59.     gpu.set(2,3, "AUTO CONTROL OFF")
  60.   end
  61.   local spaces = string.rep(" ",x - 10)
  62.   local problem = reactor.getProblem()
  63.   if problem ~= "No Problem" then
  64.     gpu.set(2,5," " .. string.upper(problem) .. spaces)
  65.   end
  66.   gpu.set(2,4," Status: " .. status .. spaces)
  67.   gpu.set(2,6," Heat  : " .. math.floor(reactor.getHeatLevel()) .. "/" .. reactor.getMaxHeatLevel()  .. " (Delta: " .. math.floor(reactor.getReactorProcessHeat()) .. ")" .. spaces)
  68.   gpu.set(2,7," Energy: " .. reactor.getEnergyStored() .. "/" .. reactor.getMaxEnergyStored() .. " RF (Delta: " .. reactor.getEnergyChange() .. ")" .. spaces)
  69. end
  70.  
  71. local th = thread.create(function()
  72.   while true do
  73.     update()
  74.     os.sleep(2)
  75.   end
  76. end)
  77.  
  78. local commands = {}
  79. commands.start = function()
  80.   if reactor.getReactorProcessHeat() > 0 then
  81.     if (reactor.getMaxHeatLevel()/reactor.getReactorProcessHeat())/20 < 60 then
  82.       gpu.set(2,y-4,"Error: Heat delta too high!")
  83.       return
  84.     else
  85.       gpu.set(2,y-4,"Warn: Reactor heat delta > 0")
  86.     end
  87.   end
  88.   gpu.set(2,y-3,"STARTING FISSION REACTOR")
  89.   reactor.activate()
  90. end
  91.  
  92. commands.stop = function()
  93.   gpu.set(2,y-3,"SHUTTING DOWN REACTOR")
  94.   reactor.deactivate()
  95. end
  96.  
  97. commands.auto = function(state)
  98.   if state == "on" then
  99.     auto = true
  100.   elseif state == "off" then
  101.     auto = false
  102.   elseif state ~= nil then
  103.     gpu.set(2,y-5,"Argument must be 'on' or 'off'")
  104.   else
  105.     gpu.set(2,y-5,"Missing argument")
  106.   end
  107. end
  108.  
  109. while true do
  110.   gpu.set(1,y-1,"Enter command:")
  111.   read = term.read({nowrap = true, dobreak = false, hint = {"start","stop","auto","quit"}})
  112.   term.clearLine()
  113.   gpu.fill(2,y-4,x-2,2," ")
  114.   cmd,arg = read:match("^(%g+) (%g+)%G?")
  115.   if not cmd then
  116.     cmd = read:match("^(%g+)")
  117.   end
  118.   cmd = string.lower(cmd)
  119.   local f = commands[cmd]
  120.   if f then
  121.     f(arg)
  122.   end
  123.   if cmd == "quit" or cmd == "exit" then
  124.     reactor.deactivate()
  125.     th:kill()
  126.     term.clear()
  127.     io.write("Reactor deactivated; program closed. Goodbye.")
  128.     break
  129.   end
  130. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top