Advertisement
Guest User

reactor

a guest
Jan 12th, 2014
18,348
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.   if (not r.getConnected()) then
  35.     return nil, "Computer port not connected to a valid reactor"
  36.   end
  37.   --if (r.getNumberOfControlRods() <1) then
  38.   --  return nil, "Reactor seems invalid"
  39.   --end
  40.   r.getEnergyPercent = function ()
  41.     return math.floor(1000 * r.getEnergyStored() / emax)/10
  42.   end
  43.   if r.nativeEPLT then
  44.     r.getEnergyProducedLastTick = r.nativeEPLT
  45.   end
  46.   r.nativeEPLT = r.getEnergyProducedLastTick
  47.   r.getEnergyProducedLastTick = function ()
  48.     return math.floor(r.nativeEPLT()*1000)/1000
  49.   end
  50.  
  51.   if redirected then
  52.     term.restore()
  53.     redirected = false
  54.   end
  55.   m=findDev("monitor")
  56.   if m then
  57.     m.setTextScale(0.5)
  58.     term.clear()
  59.     term.setCursorPos(1,1)
  60.     print("Redirecting to attached monitor")
  61.     term.redirect(m)
  62.     redirected = true
  63.   end
  64.  
  65.   term.setCursorBlink(false)
  66.   p=findDev("printer")
  67. end
  68.  
  69. function ft ()
  70.   local d=os.day()
  71.   local t=os.time()
  72.   local h=math.floor(t)
  73.   local m=math.floor((t-h)*60)
  74.   return string.format("Day %d, %02d:%02d",d,h,m)
  75. end
  76.  
  77. function log (msg)
  78.   local stamp=ft()
  79.   print (stamp..": "..msg)
  80. end
  81.  
  82. function tableWidth(t)
  83.   local width=0
  84.   for _,v in pairs(t) do
  85.     if #v>width then width=#v end
  86.   end
  87.   return width
  88. end
  89.  
  90. function ljust(s,w)
  91.   local pad=w-#s
  92.   return s .. string.rep(" ",pad)
  93. end
  94.  
  95. function rjust(s,w)
  96.   local pad=w-#s
  97.   return string.rep(" ",pad) .. s
  98. end
  99.  
  100. function display()
  101.   term.clear()
  102.   term.setCursorPos(1,1)
  103.   print("Reactor Status")
  104.   print(ft())
  105.   print("")
  106.   local funcs={"Connected","Active","NumberOfControlRods","EnergyStored","EnergyPercent","Temperature","FuelAmount","WasteAmount","FuelAmountMax","EnergyProducedLastTick"}
  107.   local units={"","","","RF","%","C","mB","mB","mB","RF/t"}
  108.   local values={}
  109.   for _,v in pairs(funcs) do
  110.     values[#values+1] = tostring(r["get"..v]())
  111.   end
  112.   local funcW=tableWidth(funcs)
  113.   local valW=tableWidth(values)
  114.   for i,v in pairs(funcs) do
  115.     print(rjust(v,funcW)..": "..rjust(values[i],valW).." "..units[i])
  116.   end  
  117. end
  118.  
  119. log("Starting")
  120. setupDevs()
  121. while true do
  122.   local e=r.getEnergyStored()
  123.   local p=math.floor(100*e/emax)
  124.   local a=p<100
  125.   local elt=r.getEnergyProducedLastTick()
  126.   display()
  127.   r.setAllControlRodLevels(p)
  128.   r.setActive(a)
  129.   os.startTimer(0.8333334)
  130.   local event,p1,p2,p3,p4,p5 = os.pullEvent()
  131.   if event == "key" then
  132.     break
  133.   elseif event == "peripheral_detach" or event == "peripheral" or event == "monitor_resize" then
  134.     setupDevs()
  135.   elseif not (event == "timer" or event=="disk" or event=="disk_eject") then
  136.     error("received "..event)
  137.   end
  138. end
  139.  
  140. end)
  141. term.restore()
  142. error(msg)
Advertisement
RAW Paste Data Copied
Advertisement