Advertisement
Guest User

startup

a guest
Jan 29th, 2020
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.35 KB | None | 0 0
  1. local bank = peripheral.wrap("back")
  2. local reac = peripheral.wrap("BigReactors-Reactor_0")
  3.  
  4. local minEnergyStoragePercent = 50
  5. local maxEnergyStoragePercent = 90
  6. local stepSize = 2
  7. local brakeTime = 5
  8. local action = "none"
  9. local defaultColor = colors.white
  10.  
  11. local monitors = {
  12.   peripheral.wrap("top");
  13.   peripheral.wrap("monitor_1");
  14.   peripheral.wrap("monitor_2");
  15. }
  16.  
  17. --TODO low fuel warning
  18.  
  19. --clears all monitors
  20. function clear()
  21.   for i=1, #monitors do
  22.     monitors[i].clear()
  23.     monitors[i].setTextColor(defaultColor)
  24.     monitors[i].setCursorPos(1,1)
  25.   end
  26. end
  27.  
  28. --goes to the next line for all monitors
  29. function nextline()
  30.   for i=1, #monitors do
  31.     local x,y = monitors[i].getCursorPos()
  32.     local y = y + 1
  33.     monitors[i].setTextColor(defaultColor)
  34.     monitors[i].setCursorPos(1,y)
  35.   end
  36. end
  37.  
  38. --writes text on all monitors
  39. function writeText(text)
  40.   for i=1, #monitors do
  41.     monitors[i].write(text)
  42.   end
  43. end
  44.  
  45. --sets text color for all monitors
  46. function textColor(color)
  47.   for i=1, #monitors do
  48.     monitors[i].setTextColor(color)
  49.   end
  50. end
  51.  
  52. --prints the statistics of the capacitor bank
  53. function printCapacitorStats()
  54.   --get the Percentage how full the capacitor is
  55.   local filledPercent = bank.getEnergyStored()/bank.getMaxEnergyStored()*100
  56.   filledPercent = math.floor(filledPercent*10)/10
  57.   writeText("Capacitor Status: ")
  58.     --if filled less then minimum, set color to red
  59.   if filledPercent <  minEnergyStoragePercent then
  60.     --sets color to red
  61.     textColor(colors.orange)
  62.    
  63.   elseif filledPercent > minEnergyStoragePercent then
  64.   --filled at least to minimum percent
  65.     if filledPercent < maxEnergyStoragePercent then
  66.     --filled within limits, set color to green
  67.       textColor(colors.green)
  68.     else
  69.     --filled to much, set color to orange
  70.       textColor(colors.purple)
  71.     end
  72.   end
  73.   writeText(filledPercent)
  74.   textColor(defaultColor)
  75.   writeText(" %")
  76.   nextline()
  77. end
  78.  
  79. function printReactorOnlineStats()
  80.   writeText("Reactor Status: ")
  81.   if reac.getConnected() then
  82.   --reactor valid and connected
  83.     if reac.getActive() then
  84.     --reactor online
  85.     textColor(colors.green)
  86.     writeText("online")
  87.     else
  88.     --reactor offline
  89.     textColor(colors.orange)
  90.     writeText("offline")
  91.     end
  92.   else
  93.   --reactor unvalid or disconnected
  94.     textColor(colors.blue)
  95.     writeText("invalid")
  96.   end
  97.   textColor(colors.white)
  98.   nextline()
  99. end
  100.  
  101. function printFuelRodStatus()
  102.   writeText("Fuel Rod dampening: "..reac.getControlRodLevel(0).." %")
  103.   nextline()
  104. end
  105.  
  106. function printEnergyOutput()
  107.   local output = math.floor(reac.getEnergyProducedLastTick()*10)/10
  108.   writeText("Reactor Output: ")
  109.   if bank.getAverageOutputPerTick() > output then
  110.     textColor(colors.blue)
  111.   else
  112.     textColor(colors.green)
  113.   end
  114.   writeText(output)
  115.   textColor(defaultColor)
  116.   writeText(" Rf/t")
  117.   nextline()
  118. end
  119.  
  120. function printCurrentAction()
  121.   writeText("Action: "..action)
  122.   nextline()  
  123. end
  124.  
  125. function printEfficiencyEstimate()
  126.   eff = reac.getFuelConsumedLastTick()*20*60*60
  127.   eff = math.floor(eff/1000)
  128.   writeText("Efficiency: ~ "..eff.." b/h")
  129.   nextline()
  130. end
  131.  
  132. function printDivider()
  133.   local divider = "+"
  134.   for i=1,27,1 do
  135.     divider = divider .. "-"
  136.   end
  137.   divider = divider .. "+"
  138.   writeText(divider)
  139.   nextline()
  140. end
  141.  
  142. function printEnergyIO()
  143.   local io = bank.getAverageChangePerTick()
  144.   writeText("Current IO: ")
  145.   if io == 0 then
  146.     textColor(colors.blue)
  147.   elseif io < 0 then
  148.     textColor(colors.orange)
  149.   elseif io > 0 then
  150.     textColor(colors.green)
  151.   end
  152.   io = math.floor(io*10)/10
  153.   writeText(io)
  154.   textColor(defaultColor)
  155.   writeText(" RF/t")
  156.   nextline()
  157. end
  158.  
  159. function printEnergyDuration()
  160.   local duration = 0
  161.   if bank.getAverageChangePerTick() < 0 then
  162.     local duration = bank.getMaxEnergyStored() / bank.getAverageChangePerTick()
  163.     duration = math.floor((duration/20/60/60)*10)/10
  164.     writeText("Sustainable for: ~ ")
  165.     textColor(colors.orange)
  166.     duration = duration * (-1)
  167.     writeText(duration)
  168.     textColor(defaultColor)
  169.     writeText(" h")
  170.   else
  171.     textColor(colors.green)
  172.     writeText("Sustainable")
  173.   end
  174.   nextline()
  175. end
  176.  
  177. function control()
  178.   if reac.getActive then
  179.     --reactor is active
  180.     local current = bank.getEnergyStored() / bank.getMaxEnergyStored()*100
  181.     current = math.floor(current*10)/10
  182.  
  183.     if current < minEnergyStoragePercent then
  184.       --lack of energy, produce
  185.       reac.setAllControlRodLevels(0)
  186.       action = "Increasing output"
  187.     elseif current > (maxEnergyStoragePercent - brakeTime) then
  188.       --starting to get near energy limit, start stopping
  189.       local newRod = reac.getControlRodLevel(0) + stepSize
  190.       if newRod > 100 then
  191.         newRod = 100
  192.       end
  193.       reac.setAllControlRodLevels(newRod)
  194.       action = "Throtteling output"
  195.     elseif current > maxEnergyStoragePercent then
  196.       --energy limit overshoot, shutting down
  197.       reac.setAllControlRodLevels(100)
  198.       action = "Stopping Output"
  199.     end
  200.   else
  201.     --reactor is inactive
  202.     mon.setBackgroundColor(colors.red)
  203.     mon.clear()
  204.   end
  205. end
  206.  
  207. while true do
  208.  
  209. clear()
  210. control()
  211. printCapacitorStats()
  212. printReactorOnlineStats()
  213. printFuelRodStatus()
  214. printEnergyOutput()
  215. printEfficiencyEstimate()
  216. printCurrentAction()
  217. printDivider()
  218. printEnergyIO()
  219. printEnergyDuration()
  220. sleep(1)
  221. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement