Anaerin

reactorMonitor

Dec 20th, 2014
1,719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.36 KB | None | 0 0
  1. -- Globals - Change these to suit your turbines and consumption needs.
  2. RPMTarget = 1700
  3. StoredShutoff = 90 -- Capacitor amount, in %
  4. StoredRestart = 10 -- Capacitor amount, in %
  5.  
  6. -- Set up variables
  7. turbines = {}
  8. local pers = peripheral.getNames()
  9. local screenWidth
  10. local screenHeight
  11. calcHeight = 0
  12. calcWidth = 0
  13. textScale = 1
  14.  
  15. -- Pad strings to width characters wide.
  16. function padRight(toPad, width)
  17.     local output = string.rep(' ',width)
  18.     output = output .. toPad
  19.     return string.sub(output, 0-width)
  20. end
  21.  
  22. -- Function to insert thousands commas.  
  23.   function comma_value(amount)
  24.   local formatted = amount
  25.   while true do  
  26.     formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  27.     if (k==0) then
  28.       break
  29.     end
  30.   end
  31.   return formatted
  32. end
  33.  
  34. -- Write temperatures in colour!
  35. function writeTemperature(value)
  36.     if value > 1000 then
  37.         hdr.setTextColor(colors.red)
  38.     elseif value > 500 then
  39.         hdr.setTextColor(colors.yellow)
  40.     elseif value > 100 then
  41.         hdr.setTextColor(colors.green)
  42.     else
  43.         hdr.setTextColor(colors.lightGray)
  44.     end
  45.     hdr.write(comma_value(value).."C")
  46. end
  47.  
  48. -- Write speed, with colour coding (ish)
  49. function writeSpeed(value)
  50.     if value > 2000 then
  51.         tbn.setTextColor(colors.red)
  52.     elseif value > 1800 then
  53.         tbn.setTextColor(colors.yellow)
  54.     elseif value > 1700 then
  55.         tbn.setTextColor(colors.green)
  56.     elseif value > 1000 then
  57.         tbn.setTextColor(colors.lightGray)
  58.     elseif value > 800 then
  59.         tbn.setTextColor(colors.green)
  60.     elseif value > 100 then
  61.         tbn.setTextColor(colors.lightGray)
  62.     else
  63.         tbn.setTextColor(colors.yellow)
  64.     end
  65.     tbn.write(padRight(comma_value(math.floor(value)),5).."RPM")
  66. end
  67.  
  68. -- Write information line about turbine.
  69. function writeTurbineInfo(turbine)
  70.     if not turbine.getInductorEngaged() then
  71.         tbn.setBackgroundColor(colors.gray)
  72.     end
  73.     writeSpeed(turbine.getRotorSpeed())
  74.     tbn.setBackgroundColor(colors.black)
  75.     tbn.setTextColor(colors.lightGray)
  76.     tbn.write(", ")
  77.     tbn.setTextColor(colors.yellow)
  78.     tbn.write(padRight(comma_value(string.format("%u", turbine.getEnergyProducedLastTick())),7))
  79.     tbn.setTextColor(colors.lightGray)
  80.     tbn.write("RFt, ")
  81.     tbn.write(padRight(comma_value(math.floor(turbine.getEnergyStored())),9).."RF Stored")
  82. end
  83.  
  84. function idleTurbine(turbine)
  85.     turbine.setInductorEngaged(false)
  86.     -- Disengage the coils
  87.     turbine.setFluidFlowRateMax(4)
  88.     -- 4mb/t of steam will keep a turbine idling if the coils aren't engaged.
  89. end
  90.  
  91. function powerTurbine(turbine)
  92.     turbine.setInductorEngaged(true)
  93.     turbine.setFluidFlowRateMax(turbine.getFluidFlowRateMaxMax())
  94. end
  95.  
  96. function idleReactor(reactor)
  97.     if reactor.getFuelTemperature() > 150 then
  98.         -- Fuel is hotter than 150 degrees, more than enough to generate steam still.
  99.         -- Disable the reactor.
  100.         reactor.setActive(false)
  101.     else
  102.         if not reactor.getActive() then
  103.             -- Reactor wasn't active, probably cooling down. But now it's under temperature
  104.             -- So time to turn it back on to idle.
  105.             reactor.setActive(true)
  106.         end
  107.         reactor.setAllControlRodLevels(100)
  108.        
  109.         for i=0,reactor.getNumberOfControlRods()-1 do
  110.             if reactor.getControlRodName(i) == "Idle" then
  111.                 -- If this rod is called "Idle", the reactor maker wanted it to be a
  112.                 -- little on when idling. set it to 95% inserted.
  113.                 reactor.setControlRodLevel(i, 97)
  114.             end
  115.         end
  116.     end
  117. end
  118.  
  119. function powerReactor(reactor)
  120.     if not reactor.getActive() then
  121.         reactor.setActive(true)
  122.     end
  123. end
  124.  
  125. function setupScreenScaling()
  126.     -- Set up monitor
  127.     local screenWidth
  128.     local screenHeight
  129.     local widthNeeded = 52
  130.     local heightNeeded = #turbines + 7
  131.     local newScale = 0.5
  132.     monitor.setTextScale(1)
  133.     screenWidth, screenHeight = monitor.getSize()
  134.     calcHeight = screenHeight
  135.     calcWidth = screenWidth
  136.     if calcHeight > heightNeeded then
  137.         while calcHeight > heightNeeded do
  138.             -- Scale up by 0.5 until height no longer fits...
  139.             newScale = newScale + 0.5
  140.             if newScale > 5 then
  141.                 newScale = 5
  142.                 print("Screen so tall we can't scale up to match it. Setting to largest value (5)")
  143.                 break
  144.             end
  145.             calcHeight = math.floor(screenHeight / newScale)
  146.             calcWidth = math.floor(screenWidth / newScale)
  147.             print("Sizing to "..newScale..", "..calcWidth.."x"..calcHeight)
  148.         end
  149.         -- Then take it back a step.
  150.         newScale = newScale - 0.5
  151.         if newScale < 0.5 then
  152.             newScale = 0.5
  153.         end
  154.         calcHeight = math.floor(screenHeight / newScale)
  155.         calcWidth = math.floor(screenWidth / newScale)
  156.     else
  157.         newScale = 0.5
  158.     end
  159.     print("Height determined scaling factor to be "..newScale)
  160.     if calcWidth < widthNeeded then
  161.         print("need more width, though.")
  162.         while calcWidth < widthNeeded do
  163.             -- Then scale down some more until width fits.
  164.             newScale = newScale - 0.5
  165.             if newScale < 0.5 then
  166.                 newScale = 0.5
  167.                 print("Screen too narrow to contain display. Scaling set to smallest possible value (0.5), aborting auto-size")
  168.                 break
  169.             end
  170.             calcHeight = math.floor(screenHeight / newScale)
  171.             calcWidth = math.floor(screenWidth / newScale)
  172.             print("Sizing to "..newScale..", "..calcWidth.."x"..calcHeight)
  173.         end
  174.         else
  175.             print("Which seems to be wide enough too.")
  176.     end
  177.     -- Everything should fit now.
  178.     print("Monitor scaling factor determined to be "..newScale..", "..calcWidth.."x"..calcHeight)
  179.     monitor.setTextScale(newScale)
  180.     monitor.clear()
  181.     if hdr then
  182.         hdr.reposition(1, 1, calcWidth, 3)
  183.         tbn.reposition(1, 5, calcWidth, #turbines)
  184.         tot.reposition(1, calcHeight - 1, calcWidth, 2)
  185.     else
  186.         hdr = window.create(monitor, 1, 1, calcWidth, 3)
  187.         tbn = window.create(monitor, 1, 5, calcWidth, #turbines)
  188.         tot = window.create(monitor, 1, calcHeight - 1, calcWidth, 2)
  189.     end
  190.    
  191. end
  192.  
  193. -- Find attached peripherals
  194. for i=1,#pers do
  195.     if peripheral.getType(pers[i]) == "BigReactors-Turbine" then
  196.         table.insert(turbines, peripheral.wrap(pers[i]))
  197.     end
  198. end
  199. monitor = peripheral.find("monitor")
  200. reactor = peripheral.find("BigReactors-Reactor")
  201. capacitor = peripheral.find("tile_blockcapacitorbank_name")
  202. capacitorFullIdling = false
  203. setupScreenScaling()
  204. local trigger = os.startTimer(1)
  205. -- Main loop.
  206. while true do
  207.     -- Display reactor information
  208.     hdr.clear()
  209.     hdr.setCursorPos(1,1)
  210.     attenAmount = 0
  211.     powerStoredPercent = math.floor((capacitor.getEnergyStored() / capacitor.getMaxEnergyStored()) * 100)
  212.     if reactor.getConnected() then
  213.         if powerStoredPercent < StoredRestart then
  214.             powerReactor(reactor)
  215.             capacitorFullIdling = false
  216.         end
  217.         if powerStoredPercent > StoredShutoff then
  218.             idleReactor(reactor)
  219.             capacitorFullIdling = true
  220.         end
  221.         if reactor.getActive() then
  222.             hdr.setTextColor(colors.green)
  223.             hdr.write("Online")
  224.         else
  225.             hdr.setTextColor(colors.lightGray)
  226.             hdr.write("Offline")
  227.         end
  228.         hdr.setTextColor(colors.lightGray)
  229.         hdr.write(", Producing ")
  230.         hdr.setTextColor(colors.yellow)
  231.         hdr.write(comma_value(reactor.getHotFluidProducedLastTick()) .. "mB")
  232.         hdr.setTextColor(colors.lightGray)
  233.         if reactor.getHotFluidType() then
  234.                                                 hdr.write(" of " .. reactor.getHotFluidType())
  235.         else
  236.          for i=1,#turbines do
  237.                 if (turbines[i].getConnected() and turbines[i].getInputType()) then
  238.                     hdr.write(" of " .. turbines[i].getInputType())
  239.                     break
  240.                 end
  241.             end
  242.         end
  243.         hdr.write(" per tick")
  244.         hdr.setCursorPos(1,2)
  245.         hdr.write("Using " .. (math.floor(reactor.getFuelConsumedLastTick()*1000)/1000) .. "mB of ")
  246.         hdr.write("fuel at " .. math.floor(reactor.getFuelReactivity()) .. "% reactivity")
  247.         hdr.setCursorPos(1,3)
  248.         hdr.write("Fuel: ")
  249.         writeTemperature(math.floor(reactor.getFuelTemperature()))
  250.         hdr.setTextColor(colors.lightGray)
  251.         hdr.write(", Casing: ")
  252.         writeTemperature(math.floor(reactor.getCasingTemperature()))
  253.     else
  254.         hdr.setTextColor(colors.red)
  255.         hdr.write("Unable to connect to Reactor.")
  256.     end
  257.     tbn.clear()
  258.     local powerTotal = 0
  259.     local powerStored = 0
  260.     local turbinesActive = 0
  261.     local turbinesIdle = 0
  262.     for i=1,#turbines do
  263.         tbn.setCursorPos(1,i)
  264.         tbn.setTextColor(colors.lightGray)
  265.         if turbines[i].getConnected() then
  266.             if capacitorFullIdling then
  267.                 idleTurbine(turbines[i])
  268.             else
  269.                 powerTurbine(turbines[i])
  270.             end
  271.             if turbines[i].getActive() then
  272.                 tbn.setTextColor(colors.green)
  273.             else
  274.                 tbn.setTextColor(colors.yellow)
  275.             end
  276.             tbn.write("Turbine "..i)
  277.             tbn.setTextColor(colors.lightGray)
  278.             tbn.write(": ")
  279.             powerTotal = powerTotal + turbines[i].getEnergyProducedLastTick()
  280.             powerStored = powerStored + turbines[i].getEnergyStored()
  281.             writeTurbineInfo(turbines[i])
  282.         else
  283.             tbn.setTextColor(colors.red)
  284.             tbn.write("Turbine "..i.." INVALID")
  285.         end
  286.     end
  287.     -- Slow the reactor, if necessary.
  288.     tot.clear()
  289.     tot.setCursorPos(1,1)
  290.     tot.setTextColor(colors.lightGray)
  291.     tot.write(turbinesActive - turbinesIdle.."/"..#turbines.." Active.")
  292.     tot.write(" RF/Ingot: "..comma_value(math.floor((powerTotal/reactor.getFuelConsumedLastTick())*1000)))
  293.     tot.setCursorPos(1,2)
  294.     tot.write(turbinesIdle.."/"..#turbines.." Idle.   ")
  295.     tot.write("Total: ")
  296.     tot.setTextColor(colors.yellow)
  297.     tot.write(padRight(comma_value(math.floor(powerTotal)),9))
  298.     tot.setTextColor(colors.lightGray)
  299.     tot.write("RFt, Stored: ")
  300.     tot.setTextColor(colors.yellow)
  301.     tot.write(padRight(comma_value(math.floor(powerStored)),10))
  302.     tot.setTextColor(colors.lightGray)
  303.     tot.write("RF")
  304.     tot.write(" Capacitor "..powerStoredPercent.."%")
  305.     local event = os.pullEvent()
  306.     if event == "timer" then
  307.         trigger = os.startTimer(1)
  308.     elseif event == "term_resize" then
  309.         setupScreenScaling()
  310.     end
  311. end
Advertisement
Add Comment
Please, Sign In to add comment