Advertisement
Guest User

reactorControl

a guest
Aug 21st, 2014
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.79 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.   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.  
  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 names={"Connected","Active","Control Rods","Power Stored","Saturation","Temperature","Fuel","Waste","Capacity","Rate","Rod Level","Consumption","Reactivity"}
  104.   local funcs={"Connected","Active","NumberOfControlRods","EnergyStored","EnergyPercent","FuelTemperature","FuelAmount","WasteAmount","FuelAmountMax","EnergyProducedLastTick","ControlRodLevel","FuelConsumedLastTick","FuelReactivity"}
  105.   local units={"","","","RF","%","C","mB","mB","mB","RF/t","%","mB","%"}
  106.   local values={}
  107.   for _,v in pairs(funcs) do
  108.     if v == "ControlRodLevel" then
  109.       values[#values+1] = tostring(r["get"..v](0))
  110.     elseif v == "FuelConsumedLastTick" then
  111.       values[#values+1] = string.sub(tostring(r["get"..v]()), 1,10)
  112.     else
  113.       values[#values+1] = tostring(r["get"..v]())
  114.     end
  115.   end
  116.   local funcW=tableWidth(names)
  117.   local valW=tableWidth(values)
  118.   for i,v in pairs(funcs) do
  119.     print(rjust(names[i],funcW)..": "..rjust(values[i],valW).." "..units[i])
  120.   end  
  121. end
  122.  
  123. log("Starting")
  124. setupDevs()
  125. while true do
  126.   local e=r.getEnergyStored()
  127.   local p=math.floor(100*e/emax)
  128.   local a=p<100
  129.   local elt=r.getEnergyProducedLastTick()
  130.   display()
  131.   r.setAllControlRodLevels(p)
  132.   r.setActive(a)
  133.   os.startTimer(0.8333334)
  134.   local event,p1,p2,p3,p4,p5 = os.pullEvent()
  135.   if event == "key" then
  136.     break
  137.   elseif event == "peripheral_detach" or event == "peripheral" or event == "monitor_resize" then
  138.     setupDevs()
  139.   elseif not (event == "timer" or event=="disk" or event=="disk_eject") then
  140.     error("received "..event)
  141.   end
  142. end
  143.  
  144. end)
  145. error(msg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement