_QuietStorm_

RTM.lua

Jun 24th, 2017
4,339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 28.32 KB | None | 0 0
  1. --[[------------------------------------------------RECTOR / TURBINE MONITOR--------------------------------------------------------------
  2. Written by QuietStorm
  3. My first shot at lua and opencomputers
  4.  
  5. Multiple reactor and turbine information and control panel. Use number pad 0-9 to change the color of the interface. Only tested with Opencomputers tier 3 hardware.
  6.  
  7. Pros: Almost 100% working as I intended it to.
  8. Cons: Having to use term.clear() to update guages and remove unwanted text causes a flickering of the screen (afaik). Any help would be appreciated  
  9.  
  10. Features:
  11. -Interface color can be changed with number pad keys 0-9
  12. -Nearly every reactor & turbine methods used
  13. -Realtime guages
  14. -Automatically sets turbine flowrate according to the number of blades used. 100% Efficiency
  15. -Automatically shuts down turbines that have filled the energy buffer
  16. -Shutdown individual reactors and turbines
  17. -Insert or retract reactor control rods in multiples of 10
  18. -Can cycle through each reactor and turbine
  19. -Lots of machine info
  20.  
  21. ]]--
  22. -- VARIABLES
  23. local com = require "component"
  24. local keyboard = require "keyboard"
  25. local term = require "term"
  26. local os = require "os"
  27. local event = require "event"
  28. local gpu = com.gpu
  29. local reactor = {}
  30. local turbine = {}
  31. local reactorAvailable
  32. local turbineAvailable
  33. local w, h = gpu.getResolution()
  34. defaultBgColor = gpu.getBackground()
  35. defaultFgColor = gpu.getForeground()
  36. local running = true
  37. local refreshRate = .8
  38. local rhTable = {"CASING TEMP", "FUEL TEMP", "WASTE", "CONTROL RODS", "STEAM OUT", "REACTIVITY", "RF OUT", "STORED RF"}
  39. local thTable = {"STEAM IN", "COOLANT OUT", "ROTOR SPEED", "ROTOR MASS", "EFFICIENCY", "INDUCTOR STATE", "RF OUT", "STORED RF"}
  40. local turbineMaxSteamIn = 0
  41. local controlRods = {}
  42. local powerON = 975001 -- Set here where the turbines should reactivate after a shutdown
  43. local powerOFF = 1000000 -- Turbine max energy buffer. Turbines will shutdown
  44. buttonInsertActive = false
  45. buttonRetractActive = false
  46. --CLEAR THE SCREEN
  47. gpu.fill(1, 1, w, h, " ")
  48.  
  49.  
  50. -- COLORS
  51. local colors = {}
  52. colors.white      = 0xFFFFFF
  53. colors.orange     = 0xFFA500
  54. colors.magenta    = 0xFF00FF
  55. colors.lightblue  = 0x00AEEF
  56. colors.yellow     = 0xFFFF00
  57. colors.lime       = 0x00FF00
  58. colors.pink       = 0xFFC0CB
  59. colors.gray       = 0x555555
  60. colors.grey       = 0x555555
  61. colors.silver     = 0xAAAAAA
  62. colors.cyan       = 0x00FFFF
  63. colors.purple     = 0x800080
  64. colors.blue       = 0x0000FF
  65. colors.brown      = 0x603913
  66. colors.green      = 0x008000
  67. colors.red        = 0xFF0000
  68. colors.black      = 0x000000
  69. local interfaceColor = colors.gray
  70. -- CHECK FOR REACTOR & TURBINE COMPUTER PORTS
  71. if com.isAvailable("br_reactor") then
  72.   reactorAvailable = true
  73. else
  74.   reactorAvailable = false
  75. end
  76.  
  77. if com.isAvailable("br_turbine") then
  78.   turbineAvailable = true
  79. else
  80.   turbineAvailable = false
  81. end
  82.  
  83. if not reactorAvailable and not turbineAvailable then
  84.   term.setCursor(1, 1)
  85.   gpu.set(1, 1, "[ ! ] THIS PROGRAM REQUIRES AT LEAST (1) REACTOR OR (1) TURBINE TO RUN  [ ! ]")
  86.   os.sleep(7)
  87.   captureKeys(q)
  88. end
  89.  
  90. -- FETCH REACTOR AND TURBINE DATA
  91. --Reactors
  92. if reactorAvailable then
  93.   local i = 1
  94.   for address, type in com.list("br_reactor") do
  95.     reactor[i] = com.proxy(address)
  96.     i = i + 1
  97.   end
  98. else
  99.   label(1, 4, "%s", colors.red, "[ ! ] NO REACTORS CONNECTED [ ! ]")
  100. end
  101.  
  102. --Turbines
  103. if turbineAvailable then
  104.   local i = 1
  105.   for address, type in com.list("br_turbine") do
  106.     turbine[i] = com.proxy(address)
  107.     i = i + 1
  108.   end
  109. else
  110.   label(1, 29, "%s", colors.red, "[ ! ] NO TURBINES CONNECTED [ ! ]")
  111. end
  112.  
  113.  
  114. --CHECK FOR A CURRENT REACTOR & TURBINE NUMBER
  115. if not reactorNum then
  116.   reactorNum = 1
  117. end
  118. if not turbineNum then
  119.   turbineNum = 1
  120. end
  121.  
  122. -- FUNCTIONS
  123. function drawInterface(color)
  124.   gpu.setBackground(color)
  125.   gpu.fill(1, 1, w, 1, " ") --Top
  126.   gpu.fill(1, h, w, 1, " ") --Bottom
  127.   gpu.fill(1, 2, 1, h - 2, " ") --Left
  128.   gpu.fill(w, 2, 1, h - 2, " ") --Right
  129.   gpu.fill(2, 3, w - 2, 1, " ") --End Reactor heading
  130.   gpu.fill(2, 8, w - 43, 1, " ") --End Reactor number stats
  131.   gpu.fill(2, 23, w, 1, " ") --End Reactor Information
  132.   gpu.fill(2, 25, w - 2, 1, " ") --End Turbine heading
  133.   gpu.fill(2, 30, w - 43, 1, " ") --End Turbine number stats
  134.   gpu.fill(2, 45, w, 1, " ") --End Turbine Information
  135.   gpu.fill(w - 42, 4, 1, 19, " ") --Reactor side box
  136.   gpu.fill(w - 42, 26, 1, 19, " ") --Turbine side box
  137.   --Start reactor meters
  138.   gpu.set(4, 10, "COOLANT")
  139.   gpu.fill(3, 11, 1, 12, " ") --Coolant level left side
  140.   gpu.fill(11, 11, 1, 12, " ") --Coolant level right side
  141.   gpu.fill(4, 11, 7, 1, " ") --Coolant level top
  142.   gpu.fill(4, 22, 7, 1, " ")--Coolant level bottom
  143.  
  144.   gpu.set(56, 10, " FUEL  ")
  145.   gpu.fill(55, 11, 1, 12, " ") --Fuel level left side
  146.   gpu.fill(63, 11, 1, 12, " ") --Fuel level right side
  147.   gpu.fill(56, 11, 7, 1, " ") --Fuel level top
  148.   gpu.fill(56, 22, 7, 1, " ") --Fuel level bottom
  149.  
  150.   gpu.set(109, 10, " STEAM ")
  151.   gpu.fill(108, 11, 1, 12, " ") --Steam level left side
  152.   gpu.fill(116, 11, 1, 12, " ") --Steam level right side
  153.   gpu.fill(109, 11, 7, 1, " ") --Steam level top
  154.   gpu.fill(109, 22, 7, 1, " ") --Steam level bottom
  155.  
  156.   gpu.fill(119, 13, 2, 10, " ") --Control Rod level Left side
  157.   gpu.fill(158, 13, 2, 10, " ") --Control Rod level Right side
  158.   gpu.fill(119, 12, 41, 1, " ") --Control Rod level Top
  159.  
  160.   --Start turbine meters
  161.   gpu.set(4, 32, " STEAM ")
  162.   gpu.fill(3, 33, 1, 12, " ") --Steam level left side
  163.   gpu.fill(11, 33, 1, 12, " ") --Steam level right side
  164.   gpu.fill(4, 33, 7, 1, " ") --Steam level top
  165.   gpu.fill(4, 44, 7, 1, " ")--Steam level bottom
  166.  
  167.   gpu.set(56, 32, " WATER ")
  168.   gpu.fill(55, 33, 1, 12, " ") --Coolant level left side
  169.   gpu.fill(63, 33, 1, 12, " ") --Coolant level right side
  170.   gpu.fill(56, 33, 7, 1, " ") --Coolant level top
  171.   gpu.fill(56, 44, 7, 1, " ")--Coolant level bottom
  172.  
  173.   gpu.set(109, 32, "  RF   ")
  174.   gpu.fill(108, 33, 1, 12, " ") --RF level left side
  175.   gpu.fill(116, 33, 1, 12, " ") --RF level right side
  176.   gpu.fill(109, 33, 7, 1, " ") --RF level top
  177.   gpu.fill(109, 44, 7, 1, " ")--RF level bottom  
  178.  
  179.   gpu.setBackground(defaultBgColor)
  180. end
  181.  
  182. function indicatorOn(col, row, text, color)
  183.   gpu.setBackground(colors.lime)
  184.   gpu.fill(col + 1, row, 8, 1, " ")
  185.   gpu.fill(col, row + 1, 10, 1, " ")
  186.   gpu.fill(col + 1, row + 2, 8, 1, " ")
  187.   textLen = string.len(text)
  188.   pos = math.floor(5 - (textLen / 2))
  189.   label(col + pos, row + 1, text, color)
  190.   gpu.setBackground(defaultBgColor)
  191. end
  192.  
  193. function indicatorOff(col, row, text, color)
  194.   gpu.setBackground(colors.red)
  195.   gpu.fill(col + 1, row, 8, 1, " ")
  196.   gpu.fill(col, row + 1, 10, 1, " ")
  197.   gpu.fill(col + 1, row + 2, 8, 1, " ")
  198.   textLen = string.len(text)
  199.   pos = math.ceil(5 - (textLen / 2))
  200.   label(col + pos, row + 1, text, color)
  201.   gpu.setBackground(defaultBgColor)
  202. end
  203.  
  204. function crButtons()
  205.   label(133, 4, "CONTROL RODS", colors.yellow)
  206.   label(120, 5, "Insert: [']", colors.orange)
  207.   label(148, 5, "Retract [/]", colors.orange)
  208.   if buttonInsertActive == false then
  209.     indicatorOff(120, 7, "INSERT", colors.yellow)
  210.   else
  211.     indicatorOn(120, 7, "INSERT", colors.yellow)
  212.     buttonInsertActive = false
  213.   end
  214.  
  215.   if buttonRetractActive == false then
  216.     indicatorOff(149, 7, "RETRACT", colors.yellow)
  217.   else
  218.     indicatorOn(149, 7, "RETRACT", colors.yellow)
  219.     buttonRetractActive = false
  220.   end
  221. end
  222.  
  223.  
  224. function percent(curValue, maxValue)
  225.   return (curValue / maxValue) * 100
  226. end
  227.  
  228. function fillMeters(col, row, width, Level, LevelMax, color)
  229.   gpu.setBackground(color)
  230.   gpu.setForeground(colors.purple)
  231.   if math.floor(percent(Level, LevelMax)) >= 10 and math.floor(percent(Level, LevelMax)) <= 19 then
  232.     gpu.fill(col, row, width, 1, " ")
  233.     gpu.set(col + 2, row, "10%")
  234.   end
  235.   if math.floor(percent(Level, LevelMax)) >= 20 and math.floor(percent(Level, LevelMax)) <= 29 then
  236.     currentRow = row - 1
  237.     gpu.fill(col, currentRow, width, 2, " ")
  238.     gpu.set(col + 2, currentRow, "20%")
  239.   end
  240.   if math.floor(percent(Level, LevelMax)) >= 30 and math.floor(percent(Level, LevelMax)) <= 39 then
  241.     currentRow = row - 2
  242.     gpu.fill(col, currentRow, width, 3, " ")
  243.     gpu.set(col + 2, currentRow, "30%")
  244.   end
  245.   if math.floor(percent(Level, LevelMax)) >= 40 and math.floor(percent(Level, LevelMax)) <= 49 then
  246.     currentRow = row - 3
  247.     gpu.fill(col, currentRow, width, 4, " ")
  248.     gpu.set(col + 2, currentRow, "40%")
  249.   end
  250.   if math.floor(percent(Level, LevelMax)) >= 50 and math.floor(percent(Level, LevelMax)) <= 59 then
  251.     currentRow = row - 4
  252.     gpu.fill(col, currentRow, width, 5, " ")
  253.     gpu.set(col + 2, currentRow, "50%")
  254.   end
  255.   if math.floor(percent(Level, LevelMax)) >= 60 and math.floor(percent(Level, LevelMax)) <= 69 then
  256.     currentRow = row - 5
  257.     gpu.fill(col, currentRow, width, 6, " ")
  258.     gpu.set(col + 2, currentRow, "60%")
  259.   end
  260.   if math.floor(percent(Level, LevelMax)) >= 70 and math.floor(percent(Level, LevelMax)) <= 79 then
  261.     currentRow = row - 6
  262.     gpu.fill(col, currentRow, width, 7, " ")
  263.     gpu.set(col + 2, currentRow, "70%")
  264.   end
  265.   if math.floor(percent(Level, LevelMax)) >= 80 and math.floor(percent(Level, LevelMax)) <= 89 then
  266.     currentRow = row - 7
  267.     gpu.fill(col, currentRow, width, 8, " ")
  268.     gpu.set(col + 2, currentRow, "80%")
  269.   end
  270.   if math.floor(percent(Level, LevelMax)) >= 90 and math.floor(percent(Level, LevelMax)) <= 99 then
  271.     currentRow = row - 8
  272.     gpu.fill(col, currentRow, width, 9, " ")
  273.     gpu.set(col + 2, currentRow, "90%")
  274.   end
  275.   if percent(Level, LevelMax) == 100 then
  276.     currentRow = row - 9
  277.     gpu.fill(col, currentRow, width, 10, " ")
  278.     gpu.set(col + 2, currentRow, "100%")
  279.   end
  280.   gpu.setBackground(defaultBgColor)
  281.   gpu.setForeground(defaultFgColor)
  282. end
  283.  
  284. function reactorMainHeader()
  285.   reactorHeader = "REACTOR ["..reactorNum.." of "..#reactor.."] ID: ".. reactor[reactorNum].address .. ""
  286.   gpu.set(2, 2, reactorHeader)
  287.   label(string.len(reactorHeader) + 40, 2, "Fuel Burn Rate: %.3f mB/t", colors.yellow, fuelBurnRate)
  288.   label(string.len(reactorHeader) + 80, 2, "Output Rating: %.0f%%", colors.green, outputRating)
  289.   if reactor[reactorNum].getActive() then
  290.     if reactor[reactorNum].isActivelyCooled() then
  291.       label(string.len(reactorHeader) + 4, 2, "POWERED ON  [ACTIVELY COOLED]", colors.lime)
  292.     elseif not reactor[reactorNum].isActivelyCooled() then
  293.       label(string.len(reactorHeader) + 4, 2, "POWERED ON  [NOT ACTIVELY COOLED]", colors.lime)
  294.     end
  295.   else
  296.     if reactor[reactorNum].isActivelyCooled() then
  297.       label(string.len(reactorHeader) + 4, 2, "POWERED OFF  [ACTIVELY COOLED]", colors.red)
  298.     elseif not reactor[reactorNum].isActivelyCooled() then
  299.       label(string.len(reactorHeader) + 4, 2, "POWERED OFF  [NOT ACTIVELY COOLED]", colors.red)
  300.     end
  301.   end
  302. end
  303.  
  304. function turbineMainHeader()
  305.   turbineHeader = "TURBINE ["..turbineNum.." of "..#turbine.."] ID: "..turbine[turbineNum].address.. ""
  306.   gpu.set(2, 24, turbineHeader)
  307. end
  308.  
  309. function reactorPower()
  310.   if reactor[reactorNum].getActive() then
  311.     reactor[reactorNum].setActive(false)
  312.   elseif not reactor[reactorNum].getActive() then
  313.     reactor[reactorNum].setActive(true)
  314.   end
  315. end
  316.  
  317. function reactorLabels()
  318.   startColumn = 2
  319.   row = 5
  320.   spaces = 6
  321.   for i = 1, #rhTable do
  322.     gpu.set(startColumn, row, rhTable[i])
  323.     startColumn = (startColumn + string.len(rhTable[i])) + spaces
  324.   end
  325. end
  326.  
  327. function turbineLabels()
  328.   startColumn = 2
  329.   row = 27
  330.   spaces = 5
  331.   for i = 1, #thTable do
  332.     gpu.set(startColumn, row, thTable[i])
  333.     startColumn = (startColumn + string.len(thTable[i])) + spaces
  334.     i = i + 1
  335.   end
  336. end
  337.  
  338. function reactorDataTable()
  339.   --REACTOR LABELS ROW 5
  340.   startColumn = 2
  341.   row = 5
  342.   spaces = 6
  343.   for i = 1, #rhTable do
  344.     gpu.set(startColumn, row, rhTable[i])
  345.     if rhTable[i] == "CASING TEMP" then
  346.       label(startColumn, row + 1, "%d °C", colors.red, tostring(reactor[reactorNum].getCasingTemperature()))
  347.       startColumn = (startColumn + string.len(rhTable[i])) + spaces      
  348.     elseif
  349.     rhTable[i] == "FUEL TEMP" then
  350.       label(startColumn, row + 1, "%d °C", colors.red, tostring(reactor[reactorNum].getFuelTemperature()))
  351.       startColumn = (startColumn + string.len(rhTable[i])) + spaces
  352.     elseif
  353.     rhTable[i] == "FUEL CONSUMED" then
  354.       label(startColumn, row + 1, "%d mB/t", nil, tostring(reactor[reactorNum].getFuelConsumedLastTick()))
  355.       startColumn = (startColumn + string.len(rhTable[i])) + spaces
  356.     elseif
  357.     rhTable[i] == "WASTE" then
  358.       label(startColumn, row + 1, "%d mB", nil, tostring(reactor[reactorNum].getWasteAmount()))
  359.       startColumn = (startColumn + string.len(rhTable[i])) + spaces
  360.     elseif
  361.     rhTable[i] == "CONTROL RODS" then
  362.       label(startColumn, row + 1, "%d Rods", nil, tostring(reactor[reactorNum].getNumberOfControlRods()))
  363.       startColumn = (startColumn + string.len(rhTable[i])) + spaces
  364.     elseif
  365.     rhTable[i] == "STEAM OUT" then
  366.       label(startColumn, row + 1, "%d mB/t", nil, tostring(reactor[reactorNum].getHotFluidProducedLastTick()))
  367.       startColumn = (startColumn + string.len(rhTable[i])) + spaces
  368.     elseif
  369.     rhTable[i] == "REACTIVITY" then
  370.       label(startColumn, row + 1, "%.0f%%", colors.yellow, tostring(reactor[reactorNum].getFuelReactivity()))
  371.       startColumn = (startColumn + string.len(rhTable[i])) + spaces
  372.     elseif
  373.     rhTable[i] == "RF OUT" then
  374.       if reactor[reactorNum].isActivelyCooled() then
  375.         label(startColumn, row + 1, "%d RF/t", colors.red, "0")
  376.         startColumn = (startColumn + string.len(rhTable[i])) + spaces
  377.       else
  378.         label(startColumn, row + 1, "%d RF/t", colors.green, tostring(reactor[reactorNum].getEnergyProducedLastTick()))
  379.         startColumn = (startColumn + string.len(rhTable[i])) + spaces
  380.       end
  381.     elseif
  382.     rhTable[i] == "STORED RF" then
  383.       if reactor[reactorNum].isActivelyCooled() then
  384.         label(startColumn, row + 1, "%d RF", colors.red, "0")
  385.         startColumn = (startColumn + string.len(rhTable[i])) + spaces
  386.       else
  387.         label(startColumn, row + 1, "%d RF", colors.green, tostring(reactor[reactorNum].getEnergyStored()))
  388.         startColumn = (startColumn + string.len(rhTable[i])) + spaces
  389.       end
  390.     end
  391.   end
  392. end
  393.  
  394. function turbineDataTable()
  395.   for i = 1, #turbine do
  396.     turbineSteamInput = turbine[turbineNum].getInputAmount()
  397.     turbineCoolantOutput = turbine[turbineNum].getOutputAmount()
  398.     turbineRotorSpeed = turbine[turbineNum].getRotorSpeed()
  399.     turbineRotorMass = turbine[turbineNum].getRotorMass()
  400.     turbineBladeEfficiency = turbine[turbineNum].getBladeEfficiency()
  401.     turbineInductorState = turbine[turbineNum].getInductorEngaged()
  402.     turbineCurrentEnergyOutput = turbine[turbineNum].getEnergyProducedLastTick()
  403.     turbineEnergyStored = turbine[turbineNum].getEnergyStored()
  404.   end
  405.   startColumn = 2
  406.   row = 28
  407.   spaces = 5
  408.   for i = 1, #thTable do
  409.     if thTable[i] == "STEAM IN" then
  410.       label(startColumn, row, "%d mB/t", nil, tostring(turbineSteamInput))
  411.       startColumn = (startColumn + string.len(thTable[i])) + spaces
  412.     elseif
  413.     thTable[i] == "COOLANT OUT" then
  414.       label(startColumn, row, "%d mB/t", nil, tostring(turbineCoolantOutput))
  415.       startColumn = (startColumn + string.len(thTable[i])) + spaces
  416.     elseif
  417.     thTable[i] == "ROTOR SPEED" then
  418.       label(startColumn, row, "%.1d RPM", nil, tostring(turbineRotorSpeed))
  419.       startColumn = (startColumn + string.len(thTable[i])) + spaces
  420.     elseif
  421.     thTable[i] == "ROTOR MASS" then
  422.       label(startColumn, row, "%d", nil, tostring(turbineRotorMass))
  423.       startColumn = (startColumn + string.len(thTable[i])) + spaces
  424.     elseif
  425.     thTable[i] == "EFFICIENCY" then
  426.       label(startColumn, row, "%.1d%%", nil, tostring(turbineBladeEfficiency))
  427.       startColumn = (startColumn + string.len(thTable[i])) + spaces
  428.     elseif
  429.     thTable[i] == "INDUCTOR STATE" then
  430.       if turbineInductorState then
  431.         label(startColumn, row, "%s", colors.lime, "ENGAGED")
  432.         startColumn = (startColumn + string.len(thTable[i])) + spaces
  433.       else
  434.         label(startColumn, row, "%s", colors.red, "DISENGAGED")
  435.         startColumn = (startColumn + string.len(thTable[i])) + spaces
  436.       end
  437.     elseif
  438.     thTable[i] == "RF OUT" then
  439.       label(startColumn, row, "%d", colors.lime, tostring(turbineCurrentEnergyOutput))
  440.       startColumn = (startColumn + string.len(thTable[i])) + spaces
  441.     elseif
  442.     thTable[i] == "STORED RF" then
  443.       label(startColumn, row, "%d", colors.lime, tostring(turbineEnergyStored))
  444.       startColumn = (startColumn + string.len(thTable[i])) + spaces
  445.     end
  446.   end
  447. end
  448.  
  449. --TURBINE AUTO EFFICIENCY MODE
  450. autoEfficiencyEnabled = false
  451. function autoEfficiency()
  452.   if not autoEfficiencyEnabled then
  453.     autoEfficiencyEnabled = false
  454.   else
  455.     autoEfficiencyEnabled = true
  456.     for i = 1, #turbine do
  457.       numBlades = turbine[i].getNumberOfBlades()
  458.       flowRate = numBlades * 25
  459.       turbine[i].setFluidFlowRateMax(flowRate)
  460.     end
  461.   end
  462. end
  463. function cycleEfficiencyMode()
  464.   if autoEfficiencyEnabled then
  465.     autoEfficiencyEnabled = false
  466.   else
  467.     if not autoEfficiencyEnabled then
  468.       autoEfficiencyEnabled = true
  469.     end
  470.   end
  471. end
  472.  
  473. function toggleAutoEfficiencyIndicator()
  474.   if autoEfficiencyEnabled then
  475.     gpu.setBackground(colors.lime)
  476.     gpu.fill(149, 29, 10, 3, " ")
  477.     label(153, 30, "ON", colors.white)
  478.     gpu.setBackground(defaultBgColor)
  479.     gpu.set(149, 28, "Efficiency")
  480.   else
  481.     gpu.setBackground(colors.red)
  482.     gpu.fill(149, 29, 10, 3, " ")
  483.     label(152, 30, "OFF", colors.pink)
  484.     gpu.setBackground(defaultBgColor)
  485.     gpu.set(149, 28, "Efficiency")
  486.   end
  487. end
  488.  
  489.  
  490. function autoPowerCycle()
  491.   if powerCycleActive then
  492.     for i = 1, #turbine do
  493.       if turbine[i].getEnergyStored() == powerOFF then
  494.         turbine[i].setActive(false)
  495.       end
  496.       if turbine[i].getEnergyStored() < powerON then
  497.         turbine[i].setActive(true)
  498.       end
  499.     end
  500.   end
  501. end
  502.  
  503. function autoPowerCycleControl()
  504.   if powerCycleActive then
  505.     powerCycleActive = false
  506.     tpcStatus = false
  507.   else
  508.     powerCycleActive = true
  509.     tpcStatus = true
  510.   end
  511. end
  512.  
  513. function toggleAutoPowerCycleIndicator()
  514.   if powerCycleActive then
  515.     gpu.setBackground(colors.lime)
  516.     gpu.fill(120, 29, 10, 3, " ")
  517.     label(124, 30, "ON", colors.white)
  518.     gpu.setBackground(defaultBgColor)
  519.     gpu.set(120, 28, "Auto Power")
  520.   else
  521.     gpu.setBackground(colors.red)
  522.     gpu.fill(120, 29, 10, 3, " ")
  523.     label(124, 30, "OFF", colors.pink)
  524.     gpu.setBackground(defaultBgColor)
  525.     gpu.set(120, 28, "Auto Power")
  526.   end
  527. end
  528.  
  529.  
  530. function turbinePower()
  531.   if turbine[turbineNum].getActive() then
  532.     turbine[turbineNum].setActive(false)
  533.   else
  534.     turbine[turbineNum].setActive(true)
  535.   end
  536. end
  537.  
  538. function turbineStatus()
  539.   if turbine[turbineNum].getActive() then
  540.     gpu.set(119, 26, "Turbine Status: Active")
  541.   else
  542.     gpu.set(119, 26, "Turbine Status: Inactive")
  543.   end
  544. end
  545.  
  546.  
  547. function totalReactorEnergyStored()
  548.   rCap = 10000000
  549.   reactorTotalEnergyStored = 0
  550.   for i = 1, #reactor do
  551.     reactorTotalEnergyStored = reactorTotalEnergyStored + reactor[i].getEnergyStored()
  552.   end
  553.   reactorEnergyPercentage = percent(reactorTotalEnergyStored, rCap * #reactor)
  554. end
  555.  
  556.  
  557. function totalTurbineEnergyStored()
  558.   tCap = 1000000
  559.   turbineTotalEnergyStored = 0
  560.   for i = 1, #turbine do
  561.     turbineTotalEnergyStored = turbineTotalEnergyStored + turbine[i].getEnergyStored()
  562.   end
  563.   turbineEnergyPercentage = percent(turbineTotalEnergyStored, tCap * #turbine)
  564. end
  565.  
  566. function reactorSideBox() --Starts at column 119 & row 4
  567.   colStart = 119
  568.   rowStart = 4
  569.   boxWidth = 159 - 119
  570.  
  571.   label(colStart + 4 , rowStart + 7, "REACTOR TOTAL CONTROL RODS LEVEL", colors.yellow)
  572.  
  573. end
  574.  
  575. function turbineSideBox() --Starts at column 119 & row 26
  576.   startColumn = 119
  577.   startRow = 27
  578.   boxWidth = 159 - 119
  579.   label(startColumn, startRow + 6, "[COMBINED TURBINE STORED ENERGY]", nil)
  580.   totalTurbineEnergyStored()
  581.   label(startColumn, startRow + 7, "Combined Energy:  " .. turbineTotalEnergyStored .." RF", nil)
  582.   label(startColumn, startRow + 8, "Maximum Storage:  " .. 1000000 * #turbine .. " RF", nil)
  583.   label(startColumn, startRow + 9, "To max capacity:  " .. (1000000 * #turbine) - turbineTotalEnergyStored .. " RF", colors.red)
  584.   label(startColumn, startRow + 16, "All Stored Energy: " .. reactorTotalEnergyStored + turbineTotalEnergyStored .. " RF", colors.lime)
  585.   label(startColumn, startRow + 11, "[While Auto Power Mode Is Active]", nil)
  586.   label(startColumn, startRow + 12, "Auto Power Off: 1000000 Stored RF", colors.red)
  587.   label(startColumn, startRow + 13, "Auto Power On: 975000 Stored RF", colors.lime)
  588.   total_r_RfOutput = 0
  589.   for i = 1, #reactor - 1 do
  590.     if not reactor[i].isActivelyCooled() then
  591.     total_r_RfOutput = total_r_RfOutput + reactor[i].getEnergyProducedLastTick()
  592.     end
  593.     i = i + 1
  594.   end
  595.   total_t_RfOutput = 0
  596.   for i = 1, #turbine do
  597.     total_t_RfOutput = total_t_RfOutput + turbine[i].getEnergyProducedLastTick()
  598.     i = i + 1
  599.   end
  600.   totalRfOutput = math.floor(total_r_RfOutput + total_t_RfOutput)
  601.   label(startColumn, startRow + 17, "Total RF Output: " .. totalRfOutput .. " RF/t", colors.green)
  602. end
  603.  
  604. function controlKeysTable()
  605.   keyCol = 2
  606.   keyRow = 46
  607.   ckList = {"Exit", "Prev Reactor", "Next Reactor", "Prev Turbine", "Next Turbine", "Reactor Power", "Turbine Power", "Auto Power", "Auto Efficiency", "Eject Waste", "Eject Fuel", "Inductor"}
  608.   label(keyCol, keyRow, "-- Program control keys --", colors.orange)
  609.   for i = 1, #ckList do
  610.     label(keyCol, keyRow + 1, ckList[i], colors.orange)
  611.     keyCol = keyCol + string.len(ckList[i]) + 2
  612.   end
  613.   function half(value)
  614.     return math.floor(value / 2)
  615.   end
  616.   keyVal = {"[q]", "[down]", "[up]", "[left]", "[right]", "[r]", "[t]", "[a]", "[e]", "[w]", "[f]", "[i]"}
  617.   keyCol = 2
  618.   keyRow = 46
  619.   for x = 1, #keyVal do
  620.     label((keyCol + half(string.len(ckList[x]))) - 2, keyRow + 2, keyVal[x], nil)
  621.     keyCol = keyCol + string.len(ckList[x]) + 2
  622.   end
  623. end
  624.  
  625. function label(x, y, message, color, ...)
  626.   local color = color or gpu.getForeground()
  627.   local oldColor = gpu.getForeground()
  628.  
  629.   gpu.setForeground(color)
  630.   term.setCursor(x, y)
  631.   print(string.format(message, ...))
  632.   gpu.setForeground(oldColor)
  633. end
  634.  
  635. -- CONTROL RODS --
  636. function crWatchDog()
  637.   rodLevel = 0
  638.   local rods = {}
  639.   local numRods = reactor[reactorNum].getNumberOfControlRods()
  640.   for i = 1, numRods - 1 do
  641.     rods[i] = reactor[reactorNum].getControlRodLevel(i)
  642.   end
  643.   for i = 1, #rods do
  644.     rodLevel = rodLevel + rods[i]
  645.   end
  646.   rodLevel = rodLevel / #rods
  647. end
  648.  
  649. function setControlRodLevel(operation, value)
  650.   local setLevel = 0
  651.   if operation == "addition" then
  652.     if rodLevel < 100 then
  653.       setLevel = rodLevel + value
  654.       reactor[reactorNum].setAllControlRodLevels(0)
  655.       reactor[reactorNum].setAllControlRodLevels(setLevel)
  656.     end
  657.   elseif
  658.   operation == "subtraction" then
  659.     if rodLevel > 0 then
  660.       setLevel = rodLevel - value
  661.       reactor[reactorNum].setAllControlRodLevels(0)
  662.       reactor[reactorNum].setAllControlRodLevels(setLevel)
  663.     end
  664.   end
  665. end
  666.  
  667. -- CAPTURE KEYBOARD EVENTS --
  668. function captureKeys(key)
  669.   local event, address, _, key, _ = event.pull()
  670.   if key == keyboard.keys.q then
  671.     gpu.setBackground(defaultBgColor)
  672.     term.setCursor(1,1)
  673.     gpu.fill(1, 1, w, h, " ")
  674.     running = false
  675.   end
  676.  
  677.   if key == keyboard.keys.apostrophe then
  678.     setControlRodLevel("addition", 10)
  679.     buttonInsertActive = true
  680.   end
  681.  
  682.   if key == keyboard.keys.slash then
  683.     setControlRodLevel("subtraction", 10)
  684.     buttonRetractActive = true
  685.   end
  686.   if key == keyboard.keys.up then
  687.     place = reactorNum + 1
  688.     if place > #reactor then
  689.       reactorNum = 1
  690.     else
  691.       reactorNum = reactorNum + 1
  692.     end
  693.   end
  694.   if key == keyboard.keys.down then
  695.     place = reactorNum - 1
  696.     if place < 1 then
  697.       reactorNum = #reactor
  698.     else
  699.       reactorNum = reactorNum - 1
  700.     end
  701.   end
  702.   if key == keyboard.keys.right then
  703.     place = turbineNum + 1
  704.     if place > #turbine then
  705.       turbineNum = 1
  706.     else
  707.       turbineNum = turbineNum + 1
  708.     end
  709.   end
  710.  
  711.   if key == keyboard.keys.left then
  712.     place = turbineNum - 1
  713.     if place < 1 then
  714.       turbineNum = #turbine
  715.     else
  716.       turbineNum = turbineNum - 1
  717.     end
  718.   end
  719.  
  720.   if key == keyboard.keys.a then
  721.     autoPowerCycleControl()
  722.   end
  723.  
  724.   if key == keyboard.keys.r then
  725.     reactorPower()
  726.   end
  727.  
  728.   if key == keyboard.keys.t then
  729.     turbinePower()
  730.   end
  731.  
  732.  
  733.   if key == keyboard.keys.e then
  734.     cycleEfficiencyMode()    
  735.   end
  736.  
  737.   if key == keyboard.keys.w then
  738.     reactor[reactorNum].doEjectWaste()
  739.   end
  740.  
  741.   if key == keyboard.keys.f then
  742.     reactor[reactorNum].doEjectFuel()
  743.   end
  744.  
  745.   if key == keyboard.keys.i then
  746.     if turbine[turbineNum].getInductorEngaged() then
  747.       turbine[turbineNum].setInductorEngaged(false)
  748.     else
  749.       turbine[turbineNum].setInductorEngaged(true)
  750.     end
  751.   end
  752.   if key == keyboard.keys.numpad0 then
  753.     interfaceColor = colors.orange
  754.   elseif
  755.   key == keyboard.keys.numpad1 then
  756.     interfaceColor = colors.magenta
  757.   elseif
  758.   key == keyboard.keys.numpad2 then
  759.     interfaceColor = colors.lightblue
  760.   elseif
  761.   key == keyboard.keys.numpad3 then
  762.     interfaceColor = colors.silver
  763.   elseif
  764.   key == keyboard.keys.numpad4 then
  765.     interfaceColor = colors.purple
  766.   elseif
  767.   key == keyboard.keys.numpad5 then
  768.     interfaceColor = colors.pink
  769.   elseif
  770.   key == keyboard.keys.numpad6 then
  771.     interfaceColor = colors.gray
  772.   elseif
  773.   key == keyboard.keys.numpad7 then
  774.     interfaceColor = colors.lime
  775.   elseif
  776.   key == keyboard.keys.numpad8 then
  777.     interfaceColor = colors.red
  778.   elseif
  779.   key == keyboard.keys.numpad9 then
  780.     interfaceColor = colors.brown
  781.   end
  782. end
  783. event.listen("key_down", captureKeys)
  784.  
  785. -- START COLLECTING DATA IN REALTIME
  786. while running do
  787.   drawInterface(interfaceColor)
  788.  
  789. -- REACTOR & TURBINE VARIABLES FOR STATS
  790.   if reactorAvailable then
  791.     totalReactorEnergyStored()
  792.     crWatchDog()
  793.     outputRating = (100 - rodLevel)
  794.     currentCoolantLevel = reactor[reactorNum].getCoolantAmount()
  795.     coolantLevelMax = reactor[reactorNum].getCoolantAmountMax()
  796.     curretFuelLevel = reactor[reactorNum].getFuelAmount()
  797.     fuelLevelMax = reactor[reactorNum].getFuelAmountMax()
  798.     fuelBurnRate = reactor[reactorNum].getFuelConsumedLastTick()
  799.     currentSteamLevel = reactor[reactorNum].getHotFluidAmount()
  800.     currentSteamLevelMax = reactor[reactorNum].getHotFluidAmountMax()
  801.     currentSteamOutput = reactor[reactorNum].getHotFluidProducedLastTick()
  802.     currentReactivity = reactor[reactorNum].getFuelReactivity()
  803.     reactorEnergyStoredMax = 10000000
  804.     currentEnergyStored = reactor[reactorNum].getEnergyStored()
  805.     currentEnergyOutput = reactor[reactorNum].getEnergyProducedLastTick()
  806.     reactorMainHeader()
  807.     reactorLabels()
  808.     reactorDataTable()
  809.     reactorSideBox()
  810.     crButtons()
  811. -- REACTOR METERS
  812.     fillMeters(4, 21, 7, currentCoolantLevel, coolantLevelMax, colors.lightblue) --Reactor Coolant Meter
  813.     fillMeters(56, 21, 7, curretFuelLevel, fuelLevelMax, colors.yellow) --Reactor Fuel Meter
  814.     fillMeters(109, 21, 7, currentSteamLevel, currentSteamLevelMax, colors.white) --Reactor Steam Meter
  815.     fillMeters(121, 22, 37, rodLevel, 100, colors.yellow) --Reactor Control Rod Meter  
  816.   end
  817.   if turbineAvailable then
  818.     totalTurbineEnergyStored()    
  819.     t_currentSteamLevel = turbine[turbineNum].getInputAmount()
  820.     t_fluidAmountMax = turbine[turbineNum].getFluidAmountMax()
  821.     t_coolantAmount = turbine[turbineNum].getOutputAmount()
  822.     t_coolantAmountMax = 4000
  823.     t_rfStored = turbineTotalEnergyStored
  824.     t_rfMax = 1000000 * #turbine
  825.     toggleAutoEfficiencyIndicator()
  826.     toggleAutoPowerCycleIndicator()
  827.     autoPowerCycle()
  828.     turbineStatus()
  829.     turbineMainHeader()
  830.     turbineLabels()
  831.     turbineDataTable()
  832.     turbineSideBox()
  833. -- TURBINE AUTO EFFICIENCY MODE SETS THE TURBINE FLOW RATE FOR MAXIMUM EFFICIENCY
  834.     autoEfficiency()
  835. -- TURBINE METERS
  836.     fillMeters(4, 43, 7, t_currentSteamLevel, t_fluidAmountMax, colors.white) --Turbine Steam Meter
  837.     fillMeters(56, 43, 7, t_coolantAmount, t_coolantAmountMax, colors.lightblue) --Turbine Coolant Out Meter
  838.     fillMeters(109, 43, 7, t_rfStored, t_rfMax, colors.green) --Turbine RF Meter
  839.   end
  840.   controlKeysTable()
  841.   os.sleep(refreshRate)                                                                            
  842.   term.clear()
  843. end
  844. event.ignore("key_down", captureKeys)
Add Comment
Please, Sign In to add comment