s3ptum

Proper reactor control

Aug 30th, 2021 (edited)
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.36 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local term = require("term")
  4. local thread = require("thread")
  5.  
  6. local function eprint(s)
  7.   io.stderr:write(s.."\n")
  8. end
  9.  
  10. local DETECT_TIME = 1
  11. local REFRESH_TIME = 0.5
  12. local ENERGY_CRIT_RATIO = 0.5
  13. local HEAT_CRIT_RATIO = 0.25
  14.  
  15. local stopping = false
  16.  
  17. local function handler()
  18.   event.pull("interrupted")
  19.   eprint("Stopping Controller...")
  20.   stopping = true
  21. end
  22.  
  23. local function control(reactor)
  24.   local max_energy = reactor.getMaxEnergyStored()
  25.   local max_heat = reactor.getMaxHeatLevel()
  26.   while not stopping do
  27.     local energy = reactor.getEnergyStored()
  28.     local heat = reactor.getHeatLevel()
  29.     local energy_ratio = energy / max_energy
  30.     local heat_ratio = heat / max_heat
  31.     if heat_ratio < HEAT_CRIT_RATIO and energy_ratio < ENERGY_CRIT_RATIO then
  32.       if not reactor.isProcessing() then
  33.         reactor.activate()
  34.       end
  35.     else
  36.       if reactor.isProcessing() then
  37.         reactor.deactivate()
  38.       end
  39.     end
  40.     os.sleep(DETECT_TIME)
  41.   end
  42.   reactor.deactivate()
  43. end
  44.  
  45. local function reactor_info(r)
  46.   print(string.format("Size: %d x %d x %d", r.getLengthX(), r.getLengthY(), r.getLengthZ()))
  47.   print(string.format("Cells: %d", r.getNumberOfCells()))
  48.   print(string.format("EnergyCapacity: %d", r.getMaxEnergyStored()))
  49.   print(string.format("HeatCapacity: %d", r.getMaxHeatLevel()))
  50.   print(string.format("Efficiency: %f", r.getEfficiency()))
  51.   print(string.format("HeatMultiplier: %f", r.getHeatMultiplier()))
  52. end
  53.  
  54. local function write_color(s, fg, bg)
  55.   local gpu = component.gpu
  56.   if fg ~= nil then
  57.     gpu.setForeground(fg)
  58.   end
  59.   if bg ~= nil then
  60.     gpu.setBackground(bg)
  61.   end
  62.   term.write(s, false)
  63. end
  64.  
  65. local h = thread.create(handler)
  66. threads = {}
  67. managed_reactors = {}
  68. for address, _ in component.list("nc_fission_reactor") do
  69.   eprint(string.format("Found reactor %s", address))
  70.   local reactor = component.proxy(address)
  71.   if reactor.isComplete() then
  72.     reactor_info(reactor)
  73.     table.insert(managed_reactors, reactor)
  74.     local t = thread.create(control, reactor)
  75.     table.insert(threads, t)
  76.   else
  77.     eprint("Reactor is incomplete!")
  78.   end
  79. end
  80.  
  81. -- Print Current Status
  82. local reactor_num = #managed_reactors
  83. while not stopping do
  84.   for _, r in pairs(managed_reactors) do
  85.     local energy = r.getEnergyStored()
  86.     local max_energy = r.getMaxEnergyStored()
  87.     local heat = r.getHeatLevel()
  88.     local max_heat = r.getMaxHeatLevel()
  89.     local running = r.isProcessing()
  90.  
  91.     term.clearLine()
  92.     write_color(string.format("%.8s: ", r.address), 0xFFFF00)
  93.     if running then
  94.       write_color("ON  ", 0x00FF00)
  95.     else
  96.       if r.getFissionFuelTime() == 0 then
  97.         write_color("Fuel", 0xFF7F00)
  98.       else
  99.         write_color("OFF ", 0xFF0000)
  100.       end
  101.     end
  102.     write_color(" -- ", 0xFFFFFF)
  103.     write_color(string.format("Energy: %d/%d  ", energy, max_energy), 0x66DBFF)
  104.     write_color(string.format("Heat: %d/%d", heat, max_heat), 0xFF4900)
  105.     write_color("\n", 0xFFFFFF)
  106.   end
  107.   os.sleep(REFRESH_TIME)
  108.   local _, cur_y = term.getCursor()
  109.   term.setCursor(1, cur_y - reactor_num)
  110. end
  111. component.gpu.setForeground(0xFFFFFF)
  112. component.gpu.setBackground(0x000000)
  113. local _, cur_y = term.getCursor()
  114. term.setCursor(1, cur_y + reactor_num)
  115.  
  116. thread.waitForAll(threads)
  117. eprint("Controller Exit")
  118. os.exit()
Add Comment
Please, Sign In to add comment