Advertisement
SythsGod

Reactor/Turbine Control

Oct 15th, 2023 (edited)
686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.86 KB | Gaming | 0 0
  1. reactor = peripheral.wrap("BigReactors-Reactor_0")
  2. turbine = {peripheral.wrap("BigReactors-Turbine_0"), peripheral.wrap("BigReactors-Turbine_1")}
  3. mon = peripheral.wrap("left")
  4.  
  5. -- STATIC VARS
  6. maxEnergy = 9000000
  7. minEnergy = 1000000
  8. minRotorSpeed = 1820
  9. maxRotorSpeed = 1860
  10.  
  11. -- VARIABLE VARS
  12. energyStoredLastCycle = 0
  13. avgBlockSize = 1
  14. avgConsumption = 0
  15. numberOfContrlRods = 0
  16.  
  17. ------------------------------------------------
  18. --FUNCTIONS-------------------------------------
  19. ------------------------------------------------
  20.  
  21. function round(num, numDecimalPlaces)
  22.     local mult = 10^(numDecimalPlaces or 0)
  23.     return math.floor(num * mult + 0.5) / mult
  24. end
  25.  
  26. function resetTerm()
  27.     term.clear()
  28.     term.setCursorPos(1,1)
  29.     term.setBackgroundColor(colors.black)
  30.     term.setTextColor(colors.white)
  31. end
  32.  
  33. function resetMon()
  34.     mon.clear()
  35.     mon.setCursorPos(1,1)
  36.     mon.setBackgroundColor(colors.black)
  37.     mon.setTextColor(colors.white)
  38. end
  39.  
  40. function nextLine()
  41.     x,y = mon.getCursorPos()
  42.     y = y + 1
  43.     mon.setCursorPos(1,y)
  44. end
  45.  
  46. function initAverages()
  47.     local f = fs.open("reactor_averages", "r")
  48.    
  49.     avgConsumption = tonumber(f.readLine())
  50.     avgBlockSize = tonumber(f.readLine())
  51.    
  52.     f.close()
  53. end
  54.  
  55. function saveAveragesToFile(consumption, size)
  56.     local f = fs.open("reactor_averages", "w")
  57.    
  58.     f.writeLine(consumption)
  59.     f.writeLine(size)
  60.    
  61.     f.close()
  62. end
  63.  
  64. ------------------------------------------------
  65. --OUTSIDE LOOP----------------------------------
  66. ------------------------------------------------
  67.  
  68. initAverages()
  69.  
  70. numberOfContrlRods = reactor.getNumberOfControlRods()
  71.  
  72. ------------------------------------------------
  73. --LOOP------------------------------------------
  74. ------------------------------------------------
  75.  
  76. while true do
  77.  
  78. resetTerm()
  79. resetMon()
  80.  
  81. enabledStatus = reactor.getActive()
  82. mon.write("Status: ")
  83.  
  84. statusColor = enabledStatus and colors.green or colors.red
  85. mon.setTextColor(statusColor)
  86. mon.write(enabledStatus and "Online" or "Offline")
  87.  
  88. mon.setTextColor(colors.white)
  89. nextLine()
  90.  
  91. -- energy = reactor.getEnergyStored()
  92. -- print("Energy: " .. energy)
  93. -- mon.write("Energy: ")
  94. -- if energy < 1500000 then
  95. --     mon.setTextColor(colors.red)
  96. -- elseif energy > 1500000 and energy < 6500000 then
  97. --     mon.setTextColor(colors.yellow)
  98. -- elseif energy > 6500000 then
  99. --     mon.setTextColor(colors.green)
  100. -- end
  101.  
  102. -- mon.write(energy .. " RF")
  103. -- mon.setTextColor(colors.white)
  104. -- nextLine()
  105.  
  106. -- rfChangePerTick = (energy - energyStoredLastCycle) / 20
  107. -- mon.write("RF Change: ")
  108.  
  109. -- if rfChangePerTick < 0 then
  110. --     mon.setTextColor(colors.red)
  111. -- else
  112. --     mon.setTextColor(colors.green)
  113. --     rfChangePerTick = "+" .. rfChangePerTick
  114. -- end
  115.  
  116. -- mon.write(rfChangePerTick .. " RF/t")
  117. -- mon.setTextColor(colors.white)
  118. -- nextLine()
  119.  
  120. -- energyStoredLastCycle = energy
  121.  
  122. fuelConsumedPerTick = reactor.getFuelConsumedLastTick()
  123. fuelConsumedPerSecond = round(fuelConsumedPerTick * 20,2)
  124. print("Fuel consumption (/s): " .. fuelConsumedPerSecond .. " mb")
  125. mon.write("Fuel Consumption: " .. fuelConsumedPerSecond .. " mb/s")
  126. nextLine()
  127.  
  128. ingotsConsumedPerSecond = fuelConsumedPerSecond / 1000
  129. --print("Uranium consumption (/s): " .. ingotsConsumedPerSecond .. " ingots")
  130.  
  131. ingotConsumptionPerHour = round(ingotsConsumedPerSecond * 60 * 60,2)
  132. print("Uranium consumption (/h): " .. ingotConsumptionPerHour .. " ingots")
  133. mon.write("Uranium Consumption: " .. ingotConsumptionPerHour .. " /h")
  134. nextLine()
  135.  
  136. avgConsumption = avgConsumption + (ingotConsumptionPerHour - avgConsumption) / avgBlockSize
  137. avgBlockSize = avgBlockSize + 1
  138.  
  139. saveAveragesToFile(avgConsumption, avgBlockSize)
  140.  
  141. mon.write("Average Uranium Consumption: " .. round(avgConsumption, 3) .. " /h")
  142. nextLine()
  143. nextLine()
  144.  
  145. -- if energy > maxEnergy and enabledStatus then
  146. --     print("Disabling reactor..")
  147. --     reactor.setActive(false)
  148. -- elseif energy < minEnergy and not enabledStatus then
  149. --     print("Enabling reactor..")
  150. --     reactor.setActive(true)
  151. -- end
  152.  
  153. local turb_EnergyStored = turbine[1].getEnergyStored() + turbine[2].getEnergyStored()
  154. local turb_RotorSpeed = {turbine[1].getRotorSpeed(), turbine[2].getRotorSpeed()}
  155. local turb_EnergyProduced = {turbine[1].getEnergyProducedLastTick(), turbine[2].getEnergyProducedLastTick()}
  156. local turb_EnergyProducedTotal = 0
  157.  
  158. for i = 1, 2 do
  159.     if turb_RotorSpeed[i] < minRotorSpeed then
  160.         turbine[i].setInductorEngaged(false)
  161.     elseif turb_RotorSpeed[i] > maxRotorSpeed then
  162.         turbine[i].setInductorEngaged(true)
  163.     end
  164.  
  165.     mon.write("Energy Produced (Turbine " .. i .. "): " .. turb_EnergyProduced[i] .. " RF/t")
  166.     nextLine()
  167.  
  168.     turb_EnergyProducedTotal = turb_EnergyProducedTotal + turb_EnergyProduced[i]
  169. end
  170.  
  171. mon.write("Energy Produced (Total): " .. turb_EnergyProducedTotal .. " RF/t")
  172.  
  173. os.sleep(1)
  174. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement