Advertisement
mikebald

Detailed Reactor

Jul 13th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.32 KB | None | 0 0
  1. -- Basic control for BigReactors-Reactor
  2. -- BSD3 License
  3. -- Emily Backes <lucca@accela.net>
  4.  
  5. -- Uses the first monitor it finds, if any
  6. -- May need 3x3 or larger for that
  7. -- No log output or printer usage yet
  8. -- Will work on adv comps but mouse event handling
  9. --   would need to be added below
  10. -- Suitable for use in /startup
  11.  
  12. -- Max energy in a reactor's internal cell
  13. local emax=10000000
  14.  
  15. -- wrap everything in an exception handler
  16. local ok,msg=pcall(function ()
  17. local r
  18. local m
  19. local redirected=false
  20. local p
  21.  
  22. function findDev (dType)
  23.   local d
  24.   for _,d in pairs(peripheral.getNames()) do
  25.     if (peripheral.getType(d) == dType) then
  26.       return peripheral.wrap(d)
  27.     end
  28.   end
  29.   return nil, dType..": not found"
  30. end
  31.  
  32. function setupDevs()    
  33.   r=assert(findDev("BigReactors-Reactor"))
  34.   while (not r.getConnected()) do
  35.     os.sleep(2)
  36.   end
  37.   r.getEnergyPercent = function ()
  38.     return math.floor(1000 * r.getEnergyStored() / emax)/10
  39.   end
  40.   if r.nativeEPLT then
  41.     r.getEnergyProducedLastTick = r.nativeEPLT
  42.   end
  43.   r.nativeEPLT = r.getEnergyProducedLastTick
  44.   r.getEnergyProducedLastTick = function ()
  45.     return math.floor(r.nativeEPLT()*1000)/1000
  46.   end
  47.  
  48.   if redirected then
  49.     term.restore()
  50.     redirected = false
  51.   end
  52.   m=findDev("monitor")
  53.   if m then
  54.     m.setTextScale(1)
  55.     term.clear()
  56.     term.setCursorPos(1,1)
  57.     print("Redirecting to attached monitor")
  58.     term.redirect(m)
  59.     redirected = true
  60.   end
  61.  
  62.   term.setCursorBlink(false)
  63.   p=findDev("printer")
  64. end
  65.  
  66. function ft ()
  67.   local d=os.day()
  68.   local t=os.time()
  69.   local h=math.floor(t)
  70.   local m=math.floor((t-h)*60)
  71.   return string.format("Day %d, %02d:%02d",d,h,m)
  72. end
  73.  
  74. function log (msg)
  75.   local stamp=ft()
  76.   print (stamp..": "..msg)
  77. end
  78.  
  79. function tableWidth(t)
  80.   local width=0
  81.   for _,v in pairs(t) do
  82.     if #v>width then width=#v end
  83.   end
  84.   return width
  85. end
  86.  
  87. function ljust(s,w)
  88.   local pad=w-#s
  89.   return s .. string.rep(" ",pad)
  90. end
  91.  
  92. function rjust(s,w)
  93.   local pad=w-#s
  94.   return string.rep(" ",pad) .. s
  95. end
  96.  
  97. function display()
  98.   term.clear()
  99.   term.setCursorPos(1,1)
  100.   print("Reactor Status")
  101.   print(ft())
  102.   print("")
  103.   local funcs={"Active","EnergyStored","FuelAmount","EnergyProducedLastTick"}
  104.   local units={"","","","RF","%","C","mB","mB","mB","RF/t"}
  105.   local values={}
  106.   for _,v in pairs(funcs) do
  107.     values[#values+1] = tostring(r["get"..v]())
  108.   end
  109.   local funcW=tableWidth(funcs)
  110.   local valW=tableWidth(values)
  111.   local dataToSend = {}
  112.   for i,v in pairs(funcs) do
  113.     print(rjust(v,funcW)..": "..rjust(values[i],valW).." "..units[i])
  114.     dataToSend[i] = rjust(v,funcW)..": "..rjust(values[i],valW).." "..units[i]
  115.   end
  116.   rednet.open("right")
  117.   rednet.broadcast( textutils.serialize(dataToSend) )
  118.   rednet.close("right")
  119. end
  120.  
  121. log("Starting")
  122. setupDevs()
  123. while true do
  124.   local e=r.getEnergyStored()
  125.   local p=math.floor(100*e/emax)
  126.   local a=p<100
  127.   local elt=r.getEnergyProducedLastTick()
  128.   display()
  129.   r.setAllControlRodLevels(p)
  130.   r.setActive(a)
  131.   os.startTimer(2)
  132.   local event,p1,p2,p3,p4,p5 = os.pullEvent()
  133.   if event == "key" then
  134.     break
  135.   elseif event == "peripheral_detach" or event == "peripheral" or event == "monitor_resize" then
  136.     setupDevs()
  137.   elseif not (event == "timer" or event=="disk" or event=="disk_eject") then
  138.     error("received "..event)
  139.   end
  140. end
  141.  
  142. end)
  143. term.restore()
  144. error(msg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement