Guest User

reactor.lua

a guest
Aug 19th, 2020
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.01 KB | None | 0 0
  1. local computer = require("computer")
  2. computer.beep(440, .1)
  3. local component = require("component")
  4. local reactors = {}
  5. do
  6.   local t = component.list("nc_fission_reactor")
  7.   for address in pairs(t) do table.insert(reactors, component.proxy(address)) end
  8. end
  9. local event = require("event")
  10. local term = require("term")
  11. if term then term.clear() end
  12. local controls, formatstring, header = {}, "", ""
  13. do
  14.   for i = 1, #reactors do
  15.     controls[i] = {fuzzyenergyshutdown = false, fuzzyheatshutdown = false, shutdown = true}
  16.   end
  17.   header = string.format(string.format("%%-%ds   Fuel       Heat  Energy  Status", tostring(#reactors):len()), "#")
  18.   formatstring = string.format("%%-0%dd   %%-7s  %%5.1f%%%%  %%5.1f%%%%  %%s", tostring(#reactors):len())
  19. end
  20. local low, high = 0.25, 0.5
  21. if term then
  22.   term.clear()
  23.   print(header)
  24. end
  25. repeat
  26.   local keypress = event.pull(0, "key_down")
  27.   if keypress then
  28.     print("Keypress detected! Shutting down all reactors.")
  29.     for _, reactor in pairs(reactors) do
  30.       reactor.deactivate()
  31.     end
  32.     os.exit()
  33.   end
  34.   if term then term.setCursor(1, 2) end
  35.   for i = 1, #reactors do
  36.     local reactor = reactors[i]
  37.     local energylevel = reactor.getEnergyStored() / reactor.getMaxEnergyStored()
  38.     local heatlevel = reactor.getHeatLevel() / reactor.getMaxHeatLevel()
  39.     local newfuzzyenergyshutdown = controls[i].fuzzyenergyshutdown and (energylevel > low) or (energylevel > high)
  40.     local newfuzzyheatshutdown = controls[i].fuzzyheatshutdown and (heatlevel > low) or (heatlevel > high)
  41.     local newshutdown = newfuzzyenergyshutdown or newfuzzyheatshutdown
  42.     if newshutdown then reactor.deactivate() else reactor.activate() end
  43.     if term then
  44.       print(string.format(formatstring, i, reactor.getFissionFuelName(), heatlevel * 100, energylevel * 100, outoffuel and "Out of fuel!" or (newshutdown and "Off         " or "On          ")))
  45.     end
  46.      controls[i].fuzzyenergyshutdown, controls[i].fuzzyheatshutdown, controls[i].shutdown = newfuzzyenergyshutdown, newfuzzyheatshutdown, newshutdown
  47.   end
  48. until false
Advertisement
Add Comment
Please, Sign In to add comment