GeoDerp

Turbine Controller

Feb 25th, 2021 (edited)
803
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Peripherals --
  2. local modem = peripheral.wrap("back")
  3. local mon1 = peripheral.wrap("right") -- 4x4 monitor to the right side of the computer
  4. local mon2 = peripheral.wrap("left") -- 2x2 monitor to the left side of the computer
  5. local drac = peripheral.find("draconic_rf_storage")
  6. local reactor = peripheral.find("BigReactors-Reactor")
  7. -- Turbine numbers may vary, see output of modems when connecting
  8. local turbine0 = peripheral.wrap("BigReactors-Turbine_0")
  9. local turbine1 = peripheral.wrap("BigReactors-Turbine_1")
  10. local turbine2 = peripheral.wrap("BigReactors-Turbine_2")
  11. local turbine3 = peripheral.wrap("BigReactors-Turbine_3")
  12. local turbine4 = peripheral.wrap("BigReactors-Turbine_4")
  13. local turbine5 = peripheral.wrap("BigReactors-Turbine_5")
  14.  
  15.  
  16. -- Variables --
  17. local autoOff = false
  18. local turbineCount = 0
  19. local powerMax = drac.getMaxEnergyStored()
  20. if powerMax > 100000000000000 then -- Optional check for tier 8 core
  21.     powerMax = 165000000000000  --sets the powerMax to a "reasonable" value so the powerBar will render more than 1%
  22. end
  23. local powerCurrent = drac.getEnergyStored()
  24. local powerIO = drac.getTransferPerTick()
  25.  
  26.  
  27. -- Color Stuff --
  28. mon1.setPaletteColor(colors.red, 0xD32F2F)
  29. mon1.setPaletteColor(colors.orange, 0xBF6E0A)
  30. mon1.setPaletteColor(colors.yellow, 0xAFB42B)
  31. mon1.setPaletteColor(colors.lime, 0x4CAF50)
  32. mon1.setPaletteColor(colors.cyan, 0x00695C)
  33. mon1.setPaletteColor(colors.magenta, 0x880E4F)
  34. mon1.setPaletteColor(colors.purple, 0x4A148C)
  35. mon1.setPaletteColor(colors.lightBlue, 0x2196F3)
  36. mon1.setPaletteColor(colors.green, 0x1B5E20)
  37. mon2.setPaletteColor(colors.red, 0xD32F2F)
  38. mon2.setPaletteColor(colors.orange, 0xBF6E0A)
  39. mon2.setPaletteColor(colors.yellow, 0xAFB42B)
  40. mon2.setPaletteColor(colors.lime, 0x4CAF50)
  41. mon2.setPaletteColor(colors.cyan, 0x00695C)
  42. mon2.setPaletteColor(colors.magenta, 0x880E4F)
  43. mon2.setPaletteColor(colors.purple, 0x4A148C)
  44. mon2.setPaletteColor(colors.lightBlue, 0x2196F3)
  45. mon2.setPaletteColor(colors.green, 0x1B5E20)
  46. local offColor = colors.purple
  47. local onColor = colors.magenta
  48. local btnTextColor = colors.cyan
  49.  
  50.  
  51. -- Window Setup --
  52. local ctrlBor = window.create(mon2,1,1,18,12,true)
  53. local ctrlWin = window.create(mon2,2,2,16,10,true)
  54. local reactBor = window.create(mon1,1,1,21,13,true)
  55. local reactWin = window.create(mon1,2,2,19,11,true)
  56. local dracBor = window.create(mon1,1,14,21,13,true)
  57. local dracWin = window.create(mon1,2,15,19,11,true)
  58. local powerBar = window.create(mon1,3,16,16,2,true)
  59. local powerBarCurr = window.create(mon1,3,16,2,2,true)
  60. local turbBor = window.create(mon1,22,1,18,26,true)
  61. local turbWin = window.create(mon1,23,2,16,24,true)
  62. ctrlBor.setBackgroundColor(colors.magenta)
  63. ctrlBor.setTextColor(colors.purple)
  64. ctrlWin.setBackgroundColor(colors.black)
  65. ctrlWin.setTextColor(colors.lightBlue)
  66. reactBor.setBackgroundColor(colors.magenta)
  67. reactBor.setTextColor(colors.purple)
  68. reactWin.setBackgroundColor(colors.black)
  69. reactWin.setTextColor(colors.lightBlue)
  70. dracBor.setBackgroundColor(colors.magenta)
  71. dracBor.setTextColor(colors.purple)
  72. dracWin.setBackgroundColor(colors.black)
  73. dracWin.setTextColor(colors.lightBlue)
  74. powerBar.setBackgroundColor(colors.red)
  75. powerBarCurr.setBackgroundColor(colors.lime)
  76. turbBor.setBackgroundColor(colors.magenta)
  77. turbBor.setTextColor(colors.purple)
  78. turbWin.setBackgroundColor(colors.black)
  79. turbWin.setTextColor(colors.lightBlue)
  80.  
  81.  
  82. -- Button Stuff --
  83. local buttons = {
  84.     button1 = {xPos = 3, yPos = 4, w = 4, h = 2, color = offColor, isOn = false},
  85.     button2 = {xPos = 8, yPos = 4, w = 4, h = 2, color = offColor, isOn = false},
  86.     button3 = {xPos = 13, yPos = 4, w = 4, h = 2, color = offColor, isOn = false},
  87.     button4 = {xPos = 3, yPos = 9, w = 4, h = 2, color = offColor, isOn = false},
  88.     button5 = {xPos = 8, yPos = 9, w = 4, h = 2, color = offColor, isOn = false},
  89.     button6 = {xPos = 13, yPos = 9, w = 4, h = 2, color = offColor, isOn = false}
  90. }
  91.  
  92. local wnOnBtn = window.create(mon2,buttons.button1.xPos,buttons.button1.yPos,buttons.button1.w,buttons.button1.h,true)
  93. local wnAutoBtn = window.create(mon2,buttons.button2.xPos,buttons.button2.yPos,buttons.button2.w,buttons.button2.h,true)
  94. local wnOffBtn = window.create(mon2,buttons.button3.xPos,buttons.button3.yPos,buttons.button3.w,buttons.button3.h,true)
  95. local wnTwoBtn = window.create(mon2,buttons.button4.xPos,buttons.button4.yPos,buttons.button4.w,buttons.button4.h,true)
  96. local wnFourBtn = window.create(mon2,buttons.button5.xPos,buttons.button5.yPos,buttons.button5.w,buttons.button5.h,true)
  97. local wnSixBtn = window.create(mon2,buttons.button6.xPos,buttons.button6.yPos,buttons.button6.w,buttons.button6.h,true)
  98. wnOnBtn.setBackgroundColor(buttons.button1.color)
  99. wnAutoBtn.setBackgroundColor(buttons.button2.color)
  100. wnOffBtn.setBackgroundColor(buttons.button3.color)
  101. wnTwoBtn.setBackgroundColor(buttons.button4.color)
  102. wnFourBtn.setBackgroundColor(buttons.button5.color)
  103. wnSixBtn.setBackgroundColor(buttons.button6.color)
  104.  
  105.  
  106. -- Component Checks --
  107. function checkComponents()
  108.     if not reactor.getConnected() then
  109.         print("Reactor not valid.")
  110.         return false
  111.     end
  112.     if not turbine0.getConnected() then
  113.         print("Turbine 0 not valid.")
  114.         return false
  115.     end
  116.     if not turbine1.getConnected() then
  117.         print("Turbine 1 not valid.")
  118.         return false
  119.     end
  120.     if not turbine2.getConnected() then
  121.         print("Turbine 2 not valid.")
  122.         return false
  123.     end
  124.     if not turbine3.getConnected() then
  125.         print("Turbine 3 not valid.")
  126.         return false
  127.     end
  128.     if not turbine4.getConnected() then
  129.         print("Turbine 4 not valid.")
  130.         return false
  131.     end
  132.     if not turbine5.getConnected() then
  133.         print("Turbine 5 not valid.")
  134.         return false
  135.     end
  136.     return true
  137. end
  138.  
  139.  
  140. -- Control Stuff (TODO) --
  141. -- Reactor Levels (0, 18, 34, 50, 65, 81)
  142.  
  143.  
  144. -- Calculations Stuff --
  145. function updateValues()
  146.     powerCurrent = drac.getEnergyStored()
  147.     powerIO = drac.getTransferPerTick()
  148. end
  149.  
  150. function round(num, places)
  151.   local mult = 10^(places or 0)
  152.   return math.floor(num * mult + 0.5) / mult
  153. end
  154.  
  155. function getPowerPercent()
  156.     return round(powerCurrent/powerMax*100)
  157. end
  158.  
  159.  
  160. -- Screen Stuff --
  161. function wipeDisplay()
  162.     mon1.setCursorPos(1,1)
  163.     mon2.setCursorPos(1,1)
  164.     ctrlBor.clear()
  165.     ctrlWin.clear()
  166.     reactBor.clear()
  167.     reactWin.clear()
  168.     dracBor.clear()
  169.     dracWin.clear()
  170.     powerBar.clear()
  171.     turbBor.clear()
  172.     turbWin.clear()
  173.     wnOnBtn.clear()
  174.     wnAutoBtn.clear()
  175.     wnOffBtn.clear()
  176.     wnTwoBtn.clear()
  177.     wnFourBtn.clear()
  178.     wnSixBtn.clear()
  179. end
  180.  
  181. function drawStaticText()
  182.     --Window Titles
  183.     ctrlBor.setCursorPos(3,1)
  184.     ctrlBor.write("SYSTEM CONTROL")
  185.     reactBor.setCursorPos(3,1)
  186.     reactBor.write("REACTOR STATUS")
  187.     dracBor.setCursorPos(2,1)
  188.     dracBor.write("ENERGY CORE STATUS")
  189.     turbBor.setCursorPos(3,1)
  190.     turbBor.write("TURBINE STATUS")
  191.  
  192.     --Control Window
  193.     ctrlWin.setTextColor(colors.lightBlue)
  194.     ctrlWin.setCursorPos(2,2)
  195.     ctrlWin.write("System State")
  196.     ctrlWin.setCursorPos(2,7)
  197.     ctrlWin.write("Turbine Count")
  198.  
  199.     --Reactor Window
  200.     reactWin.setTextColor(colors.lightBlue)
  201.     reactWin.setCursorPos(1,6)
  202.     reactWin.write("Steam Produced:")
  203.     reactWin.setCursorPos(1,9)
  204.     reactWin.write("Yellorium Consumed:")
  205.  
  206.     --Draconic Window
  207.     dracWin.setTextColor(colors.lightBlue)
  208.     dracWin.setCursorPos(2,6)
  209.     dracWin.write("Total Stored:")
  210.     dracWin.setCursorPos(2,9)
  211.     dracWin.write("Net Power:")
  212.  
  213.     --Turbine Window
  214.     turbWin.setTextColor(colors.purple)
  215.     turbWin.setCursorPos(1,1)
  216.     turbWin.write("Turbine 0")
  217.     turbWin.setCursorPos(1,5)
  218.     turbWin.write("Turbine 1")
  219.     turbWin.setCursorPos(1,9)
  220.     turbWin.write("Turbine 2")
  221.     turbWin.setCursorPos(1,13)
  222.     turbWin.write("Turbine 3")
  223.     turbWin.setCursorPos(1,17)
  224.     turbWin.write("Turbine 4")
  225.     turbWin.setCursorPos(1,21)
  226.     turbWin.write("Turbine 5")
  227. end
  228.  
  229. function drawPowerBar()
  230.     local percent = getPowerPercent()
  231.     local length = math.floor(((percent/100)*16)+0.5)
  232.     powerBarCurr.clear()
  233.     powerBarCurr.reposition(3,16,length,2)
  234. end
  235.  
  236. function drawTurbine(turbNum)
  237.     local turbplace = turbine0
  238.     local y1 = 2 + (turbNum*4)
  239.     local y2 = 3 + (turbNum*4)
  240.     local y3 = 4 + (turbNum*4)
  241.     if turbNum == 0 then
  242.         turbPlace = turbine0
  243.     elseif turbNum == 1 then
  244.         turbPlace = turbine1
  245.     elseif turbNum == 2 then
  246.         turbPlace = turbine2
  247.     elseif turbNum == 3 then
  248.         turbPlace = turbine3
  249.     elseif turbNum == 4 then
  250.         turbPlace = turbine4
  251.     elseif turbNum == 5 then
  252.         turbPlace = turbine5
  253.     end
  254.  
  255.     --Status
  256.     turbWin.setCursorPos(2,y1)
  257.     turbWin.setTextColor(colors.lightBlue)
  258.     turbWin.write("Status:")
  259.     if not turbPlace.getActive() then
  260.         turbWin.setTextColor(colors.red)
  261.         turbWin.write("OFFLINE")
  262.     elseif turbPlace.getRotorSpeed() < 1600 then
  263.         turbWin.setTextColor(colors.yellow)
  264.         turbWin.write("WARMING")
  265.     else
  266.         turbWin.setTextColor(colors.green)
  267.         turbWin.write("WORKING")
  268.     end
  269.  
  270.     --Blade Speed
  271.     turbWin.setCursorPos(2,y2)
  272.     turbWin.setTextColor(colors.lightBlue)
  273.     turbWin.write("Speed:"..round(turbPlace.getRotorSpeed()).." RPM")
  274.  
  275.     --Power Production
  276.     turbWin.setCursorPos(2,y3)
  277.     turbWin.setTextColor(colors.lightBlue)
  278.     turbWin.write("Power:"..round(turbPlace.getEnergyProducedLastTick()).." RF")
  279. end
  280.  
  281. function drawDynamicText()
  282.     --Reactor Window
  283.         --Reactor Status
  284.         reactWin.setCursorPos(1,2)
  285.         reactWin.setTextColor(colors.lightBlue)
  286.         reactWin.write("Status: ")
  287.         if reactor.getActive() then
  288.             reactWin.setTextColor(colors.green)
  289.             reactWin.write("ONLINE")
  290.         else
  291.             reactWin.setTextColor(colors.red)
  292.             reactWin.write("OFFLINE")
  293.         end
  294.  
  295.         --Fuel Rod level
  296.         reactWin.setCursorPos(1,4)
  297.         reactWin.setTextColor(colors.lightBlue)
  298.         reactWin.write("Fuel Rod Level: "..reactor.getControlRodLevel(1).."%")
  299.        
  300.         --Steam Production
  301.         reactWin.setCursorPos(2,7)
  302.         reactWin.setTextColor(colors.lightGray)
  303.         reactWin.write(round(reactor.getEnergyProducedLastTick(),1).." mB/t")
  304.        
  305.         --Fuel Consumption
  306.         reactWin.setCursorPos(2,10)
  307.         reactWin.setTextColor(colors.yellow)
  308.         reactWin.write(round(reactor.getFuelConsumedLastTick(),3).." mB/t")
  309.  
  310.     --Draconic Window
  311.         --Total Power
  312.         dracWin.setCursorPos(3,7)
  313.         if powerCurrent < 1000 then
  314.             dracWin.write(tostring(round(powerCurrent,2)).." RF ("..getPowerPercent().."%)")
  315.         elseif powerCurrent < 1000000 then
  316.             dracWin.write(tostring(round(powerCurrent/1000,2)).." kRF ("..getPowerPercent().."%)")    
  317.         elseif powerCurrent < 1000000000 then
  318.             dracWin.write(tostring(round(powerCurrent/1000000,2)).." MRF ("..getPowerPercent().."%)")
  319.         elseif powerCurrent < 1000000000000 then
  320.             dracWin.write(tostring(round(powerCurrent/1000000000,2)).." GRF ("..getPowerPercent().."%)")
  321.         elseif powerCurrent < 1000000000000000 then
  322.             dracWin.write(tostring(round(powerCurrent/1000000000000,2)).." TRF ("..getPowerPercent().."%)")
  323.         end
  324.  
  325.         --Power IO
  326.         dracWin.setCursorPos(3,10)
  327.         if powerIO < 0 then
  328.             dracWin.setTextColor(colors.red)
  329.             dracWin.write("-")
  330.         else
  331.             dracWin.setTextColor(colors.green)
  332.             dracWin.write("+")
  333.         end
  334.         if powerIO < 1000 then
  335.             dracWin.write(tostring(round(powerIO,1)).." RF")
  336.         elseif powerIO < 1000000 then
  337.             dracWin.write(tostring(round(powerIO/1000,1)).." kRF")    
  338.         elseif powerIO < 1000000000 then
  339.             dracWin.write(tostring(round(powerIO/1000000,1)).." MRF")
  340.         elseif powerIO < 1000000000000 then
  341.             dracWin.write(tostring(round(powerIO/1000000000,1)).." GRF")
  342.         elseif powerIO < 1000000000000000 then
  343.             dracWin.write(tostring(round(powerIO/1000000000000,1)).." TRF")
  344.         end
  345.  
  346.     --Turbine Window
  347.         drawTurbine(0)
  348.         drawTurbine(1)
  349.         drawTurbine(2)
  350.         drawTurbine(3)
  351.         drawTurbine(4)
  352.         drawTurbine(5)
  353. end
  354.  
  355. -- Main --
  356. while checkComponents() do
  357.     -- Main Loop
  358.     wipeDisplay()
  359.     drawStaticText()
  360.     updateValues()
  361.     drawPowerBar()
  362.     drawPowerBar()
  363.     drawDynamicText()
  364.     sleep(1)
  365. end
RAW Paste Data