Advertisement
Sanwi

CC: SteamBalancer

Feb 6th, 2015
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.73 KB | None | 0 0
  1. os.setComputerLabel("Power System Controller")
  2.  
  3. --[[
  4. This program interfaces Reactors, turbines, energy storage devices, steam drums, and monitors.
  5.  
  6. Reactors are controlled based on steam level, and turbines are controlled based on power level.
  7.  
  8. ]]
  9.  
  10.  
  11. -- Config
  12. -- Reactor toggle at steam level
  13. local reactorOffLevel = 90
  14. local reactorOnLevel = 10
  15.  
  16. -- Turbine toggle at power level
  17. local maxEnergyPercent = 90
  18. local minEnergyPercent = 10
  19.  
  20. -- Turbine config
  21. local turbineTopSpeed = 1800
  22. local turbineMaxFlow = 2000
  23. local turbineMinFlow = 420
  24.  
  25. -- Drums
  26. local drumSide = "WEST"
  27.  
  28. -- End config
  29.  
  30.  
  31. local function getDevices(deviceType) -- Credit: https://github.com/sandalle/minecraft_bigreactor_control, https://github.com/Sanwi
  32. local deviceName = nil
  33. local deviceIndex = 1
  34. local deviceList, deviceNames = {}, {} -- Empty array, which grows as we need
  35. local peripheralList = peripheral.getNames() -- Get table of connected peripherals
  36. for peripheralIndex = 1, #peripheralList do -- Log every device found
  37. if (string.lower(peripheral.getType(peripheralList[peripheralIndex])) == string.lower(deviceType)) then -- Log devices found which match deviceType and which device index we give them
  38. deviceNames[deviceIndex] = peripheralList[peripheralIndex]
  39. deviceList[deviceIndex] = peripheral.wrap(peripheralList[peripheralIndex])
  40. deviceIndex = deviceIndex + 1
  41. end
  42. end -- for peripheralIndex = 1, #peripheralList do
  43. return deviceList, deviceNames
  44. end
  45.  
  46. local function incC(term, dir, d)
  47. if not term then term = term end
  48. if not dir then dir = "down" end
  49. if not d then d = 1 end
  50. local cX, cY = term.getCursorPos()
  51. local x, y = 1, 1
  52.  
  53. if dir == "up" then
  54. y = cY - d
  55. elseif dir == "down" then
  56. y = cY + d
  57. elseif dir == "right" then
  58. x = cX + d
  59. y = cY
  60. elseif dir == "left" then
  61. x = cX - d
  62. y = cY
  63. end
  64. return x, y
  65. end
  66.  
  67. -- Returns a color based on input value
  68. local function setColor(nVal)
  69. local color = 0
  70. if nVal >= 75 then
  71. color = colors.green
  72. elseif nVal >= 50 then
  73. color = colors.blue
  74. elseif nVal >= 25 then
  75. color = colors.orange
  76. else
  77. color = colors.red
  78. end
  79. return color
  80. end
  81.  
  82. local function setDisplays(argsT)
  83. for i=1, #argsT.monitorList do
  84. local term = argsT.monitorList[i]
  85. term.clear()
  86. term.setCursorPos(1,1)
  87. for tI=1, #argsT.turbineList do
  88. term.write("Turbine "..tostring(tI)..": ")
  89. if argsT.turbineState[tI] == "Idle" then term.setTextColor(colors.lightBlue) elseif argsT.turbineState[tI] == "Spinning up" then term.setTextColor(colors.blue) else term.setTextColor(colors.green) end
  90. term.write(argsT.turbineState[tI])
  91. term.setTextColor(colors.white)
  92.  
  93. term.setCursorPos(incC(term))
  94. term.write("Inductor: ")
  95. local inductorState = argsT.turbineList[tI].getInductorEngaged()
  96. if inductorState then term.setTextColor(colors.green) else term.setTextColor(colors.red) end
  97. term.write(tostring(argsT.turbineList[tI].getInductorEngaged()))
  98. term.setTextColor(colors.white)
  99.  
  100. term.setCursorPos(incC(term))
  101. term.write("Rotor speed: ")
  102. term.write(tostring(math.floor(argsT.turbineList[tI].getRotorSpeed())))
  103.  
  104. term.setCursorPos(incC(term))
  105. term.write("Flow rate: "..tostring(argsT.turbineList[tI].getFluidFlowRateMax()).." mB/t")
  106.  
  107. term.setCursorPos(incC(term))
  108. term.write("Energy rate: "..tostring(math.floor(argsT.turbineList[tI].getEnergyProducedLastTick())).." RF/t")
  109. term.setCursorPos(incC(term,"down",2))
  110. end
  111.  
  112. term.write("Steam: ")
  113. term.setTextColor(setColor(argsT.drumLevelPercent))
  114. term.write(tostring(argsT.drumLevelPercent).."%")
  115. term.setTextColor(colors.white)
  116.  
  117. term.setCursorPos(incC(term))
  118. term.write("Power: ")
  119. term.setTextColor(setColor(argsT.capacitorLevelPercent))
  120. term.write(tostring(argsT.capacitorLevelPercent).."%")
  121. term.setTextColor(colors.white)
  122. term.setTextColor(colors.white)
  123. end
  124. end
  125.  
  126. -- Wrap peripherals
  127. local monitors, monitorNames = getDevices("monitor")
  128. local drums, drumNames = getDevices("drum")
  129. local reactors, reactorNames = getDevices("bigreactors-reactor")
  130. local turbines, turbineNames = getDevices("bigreactors-turbine")
  131. local energyStorages, energyStoragesNames = getDevices("capacitor_bank")
  132.  
  133. -- System check
  134. assert(monitors[1].write, "Connect a monitor")
  135. assert(drums[1].getTankInfo(drumSide), "Connect a steam drum")
  136. assert(reactors[1].setActive, "Connect a reactor")
  137. assert(turbines[1].setFluidFlowRateMax, "Connect a turbine")
  138. assert(energyStorages[1].getMaxEnergyStored, "Connect an energy storage")
  139.  
  140.  
  141. local capacitorMaxLevel = energyStorages[1].getMaxEnergyStored()
  142.  
  143. -- Terminal display
  144. term.clear()
  145. term.setCursorPos(1,1)
  146. term.setTextColor(colors.blue)
  147. term.write("Power System Controller")
  148. term.setCursorPos(1,2)
  149. term.setTextColor(colors.white)
  150.  
  151. -- Default turbine settings
  152. for i=1, #turbines do
  153. turbines[i].setActive(true)
  154. turbines[i].setInductorEngaged(true)
  155. turbines[i].setFluidFlowRateMax(turbineMinFlow)
  156. end
  157.  
  158. local turbineState = {}
  159. local fullSpeed = {}
  160.  
  161. for i=1, #turbines do
  162. table.insert(turbineState, "Idle")
  163. table.insert(fullSpeed, false)
  164. end
  165.  
  166. while true do
  167. -- Energy level
  168. local capacitorLevel = energyStorages[1].getEnergyStored()
  169. local capacitorLevelPercent = math.floor(capacitorLevel / capacitorMaxLevel * 100)
  170.  
  171. for i=1, #turbines do
  172. if fullSpeed[i] then
  173. if turbines[i].getRotorSpeed() >= turbineTopSpeed then -- Wait for full speed, and lock inductor state
  174. turbines[i].setInductorEngaged(true)
  175. turbineState[i] = "Full speed"
  176. end
  177. if capacitorLevelPercent >= 90 then -- Return to idle
  178. turbines[i].setFluidFlowRateMax(turbineMinFlow)
  179. turbineState[i] = "Idle"
  180. fullSpeed[i] = false
  181. end
  182. else
  183. if capacitorLevelPercent <= 10 then
  184. turbines[i].setFluidFlowRateMax(turbineMaxFlow)
  185. turbines[i].setInductorEngaged(false)
  186. turbineState[i] = "Spinning up"
  187. fullSpeed[i] = true
  188. end
  189. end
  190. end
  191. -- Steam level percent
  192. local drumInfo = drums[1].getTankInfo(drumSide)
  193. local cSteam = drumInfo[1].contents.amount
  194. local maxSteam = drumInfo[1].capacity
  195. local drumLevelPercent = math.floor(cSteam / maxSteam * 100)
  196.  
  197. -- Reactor control
  198. if drumLevelPercent >= reactorOffLevel then -- Turn off (peak steam level)
  199. reactors[1].setActive(false)
  200. elseif drumLevelPercent <= reactorOnLevel then -- Turn on (minimum steam level)
  201. reactors[1].setActive(true)
  202. end
  203.  
  204. setDisplays({capacitorLevelPercent=capacitorLevelPercent, drumLevelPercent=drumLevelPercent, turbineState=turbineState, turbineList=turbines, reactorList=reactors, drumList=drums, monitorList=monitors, energyStorageList=energyStorages})
  205. os.sleep(2)
  206. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement