Advertisement
RlonRyan

Reactor V2

Jun 4th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.76 KB | None | 0 0
  1. local RPOWER = 10000000
  2. local TPOWER = 1000000
  3. local LIQCAP = 2600
  4.  
  5. local react = peripheral.find("BigReactors-Reactor")
  6. local turbines = nil
  7. local activecool = false
  8. local w, h = term.getSize()
  9.  
  10. function getReactor()
  11.   react = peripheral.find("BigReactors-Reactor")
  12.   activecool = react.isActivelyCooled()
  13. end
  14.  
  15. function getTurbines()
  16.   turbines = {peripheral.find("BigReactors-Turbine")}
  17. end
  18.  
  19. function getRPM()
  20.     local rpm = 0;
  21.     for i,t in ipairs(turbines) do
  22.         rpm = rpm + t.getRotorSpeed()
  23.     end
  24.     return (rpm / table.getn(turbines))
  25. end
  26.  
  27. function getReactorPowerFill()
  28.     return react.getEnergyStored() / RPOWER
  29. end
  30.  
  31. function getTurbinePowerFill()
  32.     local p = 0
  33.    
  34.     for i,t in ipairs(turbines) do
  35.         p = p + t.getEnergyStored()
  36.     end
  37.     return (p / (TPOWER * table.getn(turbines)))
  38. end
  39.  
  40. function setTurbinesActive(value)
  41.     for i,t in ipairs(turbines) do
  42.         t.setActive(value)
  43.     end
  44. end
  45.  
  46. function output(text, color)
  47.   color = color or colors.white
  48.   term.setTextColor(color)
  49.   write(text)
  50. end
  51.  
  52. function outputBool(b)
  53.   if (b) then
  54.     output("true", colors.green)
  55.   else
  56.     output("false", colors.red)
  57.   end
  58. end
  59.  
  60. function outputPower()
  61.   local p = 0
  62.   local f = 0
  63.   if (activecool) then
  64.     for i,t in ipairs(turbines) do
  65.         p = p + t.getEnergyStored()
  66.     end
  67.     f = p / (TPOWER * table.getn(turbines))
  68.   else
  69.     p = react.getEnergyStored()
  70.     f = p / RPOWER
  71.   end
  72.  
  73.   if (f >= .50) then
  74.     output(tostring(p) .. "rf", colors.green)
  75.   elseif (f >= .25) then
  76.     output(tostring(p) .. "rf", colors.yellow)
  77.   else
  78.     output(tostring(p) .. "rf", colors.red)
  79.   end
  80. end
  81.  
  82. function outputFill(f)
  83.   f = f * 100
  84.   if (f >= 50) then
  85.     output(tostring(f) .. "%", colors.green)
  86.   elseif (f >= 25) then
  87.     output(tostring(f) .. "%", colors.yellow)
  88.   else
  89.     output(tostring(f) .. "%", colors.red)
  90.   end
  91. end
  92.  
  93. function outputPowerFill()
  94.   local p = 0
  95.   local f = 0
  96.   if (activecool) then
  97.     for i,t in ipairs(turbines) do
  98.         p = p + t.getEnergyStored()
  99.     end
  100.     f = p / (TPOWER * table.getn(turbines))
  101.   else
  102.     p = react.getEnergyStored()
  103.     f = p / RPOWER
  104.   end
  105.  
  106.   outputFill(f)
  107. end
  108.  
  109. function outputCoolant(c)
  110.   local c = react.getCoolantAmount()
  111.   local f = c / LIQCAP
  112.  
  113.   if (f >= .50) then
  114.     output(tostring(c) .. "mB", colors.green)
  115.   elseif (f >= .25) then
  116.     output(tostring(c) .. "mB", colors.yellow)
  117.   else
  118.     output(tostring(c) .. "mB", colors.red)
  119.   end
  120. end
  121.  
  122. function outputSteam()
  123.   local steam = react.getHotFluidAmount()
  124.   local f = steam / LIQCAP
  125.  
  126.   if (f >= .75) then
  127.     output(tostring(steam) .. "mB", colors.red)
  128.   elseif (f >= .50) then
  129.     output(tostring(steam) .. "mB", colors.yellow)
  130.   else
  131.     output(tostring(steam) .. "mB", colors.green)
  132.   end
  133. end
  134.  
  135. function outputRPM()
  136.   local r = getRPM()
  137.  
  138.   if (r >= 2000) then
  139.     output(tostring(r) .. "RPM", colors.red)
  140.   elseif (r >= 1900) then
  141.     output(tostring(r) .. "RPM", colors.yellow)
  142.   elseif (r >= 1700) then
  143.     output(tostring(r) .. "RPM", colors.green)
  144.   else
  145.     output(tostring(r) .. "RPM", colors.yellow)
  146.   end
  147. end
  148.  
  149. function printInfo()
  150.   term.clear()
  151.   term.setCursorPos(1, 1)
  152.   for i=1, ((w - 18) / 2), 1 do
  153.     output(" ")
  154.   end
  155.   output("Reactor Controller")
  156.   output("\n")
  157.   for i=1, w, 1 do
  158.     output("=")
  159.   end
  160.   output("\n")
  161.  
  162.   output("Connected: ")
  163.   outputBool(react.getConnected())
  164.   output("\n")
  165.  
  166.   output("Active: ")
  167.   outputBool(react.getActive())
  168.   output("\n")
  169.  
  170.   output("Fuel: ")
  171.   outputFill(react.getFuelAmount() / react.getFuelAmountMax())
  172.   output("\n")
  173.  
  174.   if(activecool) then
  175.     output("Coolant: ")
  176.     outputCoolant()
  177.     output("\n")
  178.    
  179.     output("Steam: ")
  180.     outputSteam()
  181.     output("\n")
  182.    
  183.     output("RPM: ")
  184.     outputRPM()
  185.     output("\n")
  186.   end
  187.  
  188.   output("Power: ")
  189.   outputPower()
  190.   output("\n")
  191.  
  192.   output("Fill: ")
  193.   outputPowerFill()
  194.   output("\n")
  195. end
  196.  
  197. function power()
  198.   if (activecool) then
  199.     if ((react.getActive() and ((react.getHotFluidAmount() / LIQCAP) > .75)) or ((react.getCoolantAmount() / LIQCAP) < .25) or (getTurbinePowerFill() > .75)) then
  200.       react.setActive(false)
  201.     elseif (((react.getHotFluidAmount() / LIQCAP) < .25) and ((react.getCoolantAmount() / LIQCAP) > .75) and (getTurbinePowerFill() < .25)) then
  202.       react.setActive(true)
  203.       setTurbinesActive(true)
  204.     end
  205.   else
  206.     if (react.getActive() and (getReactorPowerFill() > .90)) then
  207.       react.setActive(false)
  208.     elseif (getReactorPowerFill() < .25) then
  209.       react.setActive(true)
  210.     end
  211.   end
  212. end
  213.  
  214. function run()
  215.   getReactor()
  216.   if (activecool) then
  217.     getTurbines()
  218.   end
  219.   while react do
  220.     power()
  221.     printInfo()
  222.     sleep(0.5)
  223.   end
  224.   term.clear()
  225.   term.setCursorPos(1,1)
  226.   print("Reactor Connection Lost!")
  227. end
  228.  
  229. run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement