Advertisement
Kodos

reactortwopointoh

Feb 2nd, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.71 KB | None | 0 0
  1. local component = require("component")
  2. local serialization = require("serialization")
  3.  
  4. local modem = component.modem
  5. local portNumber = 2805
  6.  
  7. local term = require("term")
  8.  
  9. -- Max energy in a reactor's internal cell
  10. local emax=10000000
  11.  
  12. -- wrap everything in an exception handler
  13. local ok,msg=pcall(function ()
  14. local r
  15. local m
  16. local redirected=false
  17. local p
  18.  
  19. function setupDevs()
  20.   r=assert(component.br_reactor)
  21.   if (not r.getConnected()) then
  22.     return nil, "Computer port not connected to a valid reactor"
  23.   end
  24.   --if (r.getNumberOfControlRods() <1) then
  25.   --  return nil, "Reactor seems invalid"
  26.   --end
  27.   r.getEnergyPercent = function ()
  28.     return math.floor(1000 * r.getEnergyStored() / emax)/10
  29.   end
  30.   if r.nativeEPLT then
  31.     r.getEnergyProducedLastTick = r.nativeEPLT
  32.   end
  33.   r.nativeEPLT = r.getEnergyProducedLastTick
  34.   r.getEnergyProducedLastTick = function ()
  35.     return math.floor(r.nativeEPLT()*1000)/1000
  36.   end
  37.  
  38.   term.setCursorBlink(false)
  39. end
  40.  
  41. function ft ()
  42.   -- local d=os.day()
  43.   -- local t=os.time()
  44.   -- local h=math.floor(t)
  45.   -- local m=math.floor((t-h)*60)
  46.   -- return string.format("Day %d, %02d:%02d",d,h,m)
  47.      return "timestamp..."
  48. end
  49.  
  50. function log (msg)
  51.   local stamp=ft()
  52.   print (stamp..": "..msg)
  53. end
  54.  
  55. function tableWidth(t)
  56.   local width=0
  57.   for _,v in pairs(t) do
  58.     if #v>width then width=#v end
  59.   end
  60.   return width + 4
  61. end
  62.  
  63. function ljust(s,w)
  64.   local pad=w-#s
  65.   return s .. string.rep(" ",pad)
  66. end
  67.  
  68. function rjust(s,w)
  69.   local pad=w-#s
  70.   return string.rep(" ",pad) .. s
  71. end
  72.  
  73. function display()
  74.   local floatUnits = {"%","C","mB","RF/t"}
  75.   term.clear()
  76.   term.setCursor(1,1)
  77.   msg = {name="Reactor A - TEST"}
  78.   msg["metric"] = {}
  79.   print("Reactor Status")
  80.   print("")
  81.   local funcs={"Connected","Active","NumberOfControlRods","EnergyStored","EnergyPercent","FuelTemperature","CasingTemperature","FuelAmount","WasteAmount","FuelAmountMax", "FuelReactivity", "FuelConsumedLastTick", "EnergyProducedLastTick"}
  82.   local units={"","","","RF","%","C","C","mB","mB","mB","%","mB", "RF/t"}
  83.   local values={}
  84.   for _,v in pairs(funcs) do
  85.     thisVal = tostring(r["get"..v]())
  86.     values[#values+1] = thisVal
  87.     msg["metric"][v] = thisVal
  88.   end
  89.  
  90.   local funcW=tableWidth(funcs)
  91.   local valW=tableWidth(values)
  92.   for i,v in pairs(funcs) do
  93.     print(string.format("%-12s: %s %s", v, values[i], units[i]))
  94.   end
  95.  
  96.   modem.broadcast(2805, serialization.serialize(msg))
  97. end
  98.  
  99. log("Starting")
  100. setupDevs()
  101. while true do
  102.   local e=r.getEnergyStored()
  103.   local p=math.floor(100*e/emax)
  104.   local a=p<100
  105.   local elt=r.getEnergyProducedLastTick()
  106.   display()
  107.   r.setAllControlRodLevels(p)
  108.   r.setActive(a)
  109.   os.sleep(1)
  110. end
  111.  
  112. end)
  113.  
  114. error(msg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement