Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.setComputerLabel("Power System Controller")
- --[[
- This program interfaces Reactors, turbines, energy storage devices, steam drums, and monitors.
- Reactors are controlled based on steam level, and turbines are controlled based on power level.
- ]]
- -- Config
- -- Reactor toggle at steam level
- local reactorOffLevel = 90
- local reactorOnLevel = 10
- -- Turbine toggle at power level
- local maxEnergyPercent = 90
- local minEnergyPercent = 10
- -- Turbine config
- local turbineTopSpeed = 1800
- local turbineMaxFlow = 2000
- local turbineMinFlow = 420
- -- Drums
- local drumSide = "WEST"
- -- End config
- local function getDevices(deviceType) -- Credit: https://github.com/sandalle/minecraft_bigreactor_control, https://github.com/Sanwi
- local deviceName = nil
- local deviceIndex = 1
- local deviceList, deviceNames = {}, {} -- Empty array, which grows as we need
- local peripheralList = peripheral.getNames() -- Get table of connected peripherals
- for peripheralIndex = 1, #peripheralList do -- Log every device found
- if (string.lower(peripheral.getType(peripheralList[peripheralIndex])) == string.lower(deviceType)) then -- Log devices found which match deviceType and which device index we give them
- deviceNames[deviceIndex] = peripheralList[peripheralIndex]
- deviceList[deviceIndex] = peripheral.wrap(peripheralList[peripheralIndex])
- deviceIndex = deviceIndex + 1
- end
- end -- for peripheralIndex = 1, #peripheralList do
- return deviceList, deviceNames
- end
- local function incC(term, dir, d)
- if not term then term = term end
- if not dir then dir = "down" end
- if not d then d = 1 end
- local cX, cY = term.getCursorPos()
- local x, y = 1, 1
- if dir == "up" then
- y = cY - d
- elseif dir == "down" then
- y = cY + d
- elseif dir == "right" then
- x = cX + d
- y = cY
- elseif dir == "left" then
- x = cX - d
- y = cY
- end
- return x, y
- end
- -- Returns a color based on input value
- local function setColor(nVal)
- local color = 0
- if nVal >= 75 then
- color = colors.green
- elseif nVal >= 50 then
- color = colors.blue
- elseif nVal >= 25 then
- color = colors.orange
- else
- color = colors.red
- end
- return color
- end
- local function setDisplays(argsT)
- for i=1, #argsT.monitorList do
- local term = argsT.monitorList[i]
- term.clear()
- term.setCursorPos(1,1)
- for tI=1, #argsT.turbineList do
- term.write("Turbine "..tostring(tI)..": ")
- 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
- term.write(argsT.turbineState[tI])
- term.setTextColor(colors.white)
- term.setCursorPos(incC(term))
- term.write("Inductor: ")
- local inductorState = argsT.turbineList[tI].getInductorEngaged()
- if inductorState then term.setTextColor(colors.green) else term.setTextColor(colors.red) end
- term.write(tostring(argsT.turbineList[tI].getInductorEngaged()))
- term.setTextColor(colors.white)
- term.setCursorPos(incC(term))
- term.write("Rotor speed: ")
- term.write(tostring(math.floor(argsT.turbineList[tI].getRotorSpeed())))
- term.setCursorPos(incC(term))
- term.write("Flow rate: "..tostring(argsT.turbineList[tI].getFluidFlowRateMax()).." mB/t")
- term.setCursorPos(incC(term))
- term.write("Energy rate: "..tostring(math.floor(argsT.turbineList[tI].getEnergyProducedLastTick())).." RF/t")
- term.setCursorPos(incC(term,"down",2))
- end
- term.write("Steam: ")
- term.setTextColor(setColor(argsT.drumLevelPercent))
- term.write(tostring(argsT.drumLevelPercent).."%")
- term.setTextColor(colors.white)
- term.setCursorPos(incC(term))
- term.write("Power: ")
- term.setTextColor(setColor(argsT.capacitorLevelPercent))
- term.write(tostring(argsT.capacitorLevelPercent).."%")
- term.setTextColor(colors.white)
- term.setTextColor(colors.white)
- end
- end
- -- Wrap peripherals
- local monitors, monitorNames = getDevices("monitor")
- local drums, drumNames = getDevices("drum")
- local reactors, reactorNames = getDevices("bigreactors-reactor")
- local turbines, turbineNames = getDevices("bigreactors-turbine")
- local energyStorages, energyStoragesNames = getDevices("capacitor_bank")
- -- System check
- assert(monitors[1].write, "Connect a monitor")
- assert(drums[1].getTankInfo(drumSide), "Connect a steam drum")
- assert(reactors[1].setActive, "Connect a reactor")
- assert(turbines[1].setFluidFlowRateMax, "Connect a turbine")
- assert(energyStorages[1].getMaxEnergyStored, "Connect an energy storage")
- local capacitorMaxLevel = energyStorages[1].getMaxEnergyStored()
- -- Terminal display
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.blue)
- term.write("Power System Controller")
- term.setCursorPos(1,2)
- term.setTextColor(colors.white)
- -- Default turbine settings
- for i=1, #turbines do
- turbines[i].setActive(true)
- turbines[i].setInductorEngaged(true)
- turbines[i].setFluidFlowRateMax(turbineMinFlow)
- end
- local turbineState = {}
- local fullSpeed = {}
- for i=1, #turbines do
- table.insert(turbineState, "Idle")
- table.insert(fullSpeed, false)
- end
- while true do
- -- Energy level
- local capacitorLevel = energyStorages[1].getEnergyStored()
- local capacitorLevelPercent = math.floor(capacitorLevel / capacitorMaxLevel * 100)
- for i=1, #turbines do
- if fullSpeed[i] then
- if turbines[i].getRotorSpeed() >= turbineTopSpeed then -- Wait for full speed, and lock inductor state
- turbines[i].setInductorEngaged(true)
- turbineState[i] = "Full speed"
- end
- if capacitorLevelPercent >= 90 then -- Return to idle
- turbines[i].setFluidFlowRateMax(turbineMinFlow)
- turbineState[i] = "Idle"
- fullSpeed[i] = false
- end
- else
- if capacitorLevelPercent <= 10 then
- turbines[i].setFluidFlowRateMax(turbineMaxFlow)
- turbines[i].setInductorEngaged(false)
- turbineState[i] = "Spinning up"
- fullSpeed[i] = true
- end
- end
- end
- -- Steam level percent
- local drumInfo = drums[1].getTankInfo(drumSide)
- local cSteam = drumInfo[1].contents.amount
- local maxSteam = drumInfo[1].capacity
- local drumLevelPercent = math.floor(cSteam / maxSteam * 100)
- -- Reactor control
- if drumLevelPercent >= reactorOffLevel then -- Turn off (peak steam level)
- reactors[1].setActive(false)
- elseif drumLevelPercent <= reactorOnLevel then -- Turn on (minimum steam level)
- reactors[1].setActive(true)
- end
- setDisplays({capacitorLevelPercent=capacitorLevelPercent, drumLevelPercent=drumLevelPercent, turbineState=turbineState, turbineList=turbines, reactorList=reactors, drumList=drums, monitorList=monitors, energyStorageList=energyStorages})
- os.sleep(2)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement