Advertisement
Guest User

reactor.lua

a guest
Aug 20th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.67 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local reactor = component.nc_fission_reactor
  4. local modem = component.modem
  5. local term = require("term")
  6. local gpu = component.gpu
  7. local serialization = require("serialization")
  8. local w, h = gpu.getResolution()
  9.  
  10. local colors = {}
  11. colors.red = 0xff0000
  12. colors.gray = 0x555555
  13. colors.lime = 0x00ff00
  14.  
  15. local data = {}
  16. data.heat = 0.0
  17. data.energy = 0.0
  18. data.active = False
  19.  
  20. local cnt = 0
  21.  
  22. function label(x, y, color, message, ...)
  23.   local color = color or gpu.getForeground()
  24.   local oldColor = gpu.getForeground()
  25.  
  26.   gpu.setForeground(color)
  27.   term.setCursor(x,y)
  28.   print(string.format(message, ...))
  29.   gpu.setForeground(oldColor)
  30. end
  31.  
  32.  
  33. modem.open(10)
  34. term.clear()
  35.  
  36. repeat
  37.   data.energy = (reactor.getEnergyStored()/reactor.getMaxEnergyStored()) * 100
  38.   data.heat = (reactor.getHeatLevel()/reactor.getMaxHeatLevel())*100
  39.   data.active = reactor.isProcessing()
  40.  
  41.   label(1, 1, colors.lime, "Fission Reactor")
  42.   label(1, 3, colors.lime, "Energy Buffer %.2f", data.energy)
  43.   label(1, 5, colors.red, "Heat Buffer %.2f", data.heat)
  44.   if (data.heat > 30) then
  45.     --generating heat, unbalanced fuel
  46.     reactor.deactivate()
  47.     label(w/2, 1, colors.red, "Off")
  48.   end
  49.  
  50.   if (data.active and data.energy > 98) then
  51.     --conserve fuel
  52.     reactor.deactivate()
  53.     label(w/2, 1, colors.red, "Off")
  54.   end
  55.  
  56.   if (not data.active and data.energy < 20 and data.heat < 1) then
  57.     reactor.activate()
  58.     label(w/2, 1, colors.lime, "On ")
  59.   end
  60.  
  61.   local msg = {}
  62.   msg.type = "reactor_update"
  63.   msg.id = os.getenv("HOSTNAME")
  64.   msg.data = data
  65.   modem.broadcast(10, serialization.serialize(msg))
  66.  
  67. until event.pull(0.5, "interrupt")
  68.  
  69. modem.close(10)
  70. os.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement