Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---user input required
- ---connect both flow gates via a modem, right click modem to activate and see name
- local injector = "flow_gate_3"
- local output = "flow_gate_2"
- ---define constants
- local maxTemp = 10000
- local targetTemp = 7000
- local fieldStrengthMin = 15
- local fieldStrengthMax = 25
- local energySaturationMin = 15
- local energySaturationMax = 25
- -- initialize variables
- local reactor = nil
- local reactorName = nil
- local monitor = nil
- local inCheck = false
- local outCheck = false
- local warmup = false
- local readyForActivation = false
- local inputEnergy = 1300000
- local outputEnergy = 3000000
- local tempStable = false
- local satStable = false
- local shieldStable = false
- ---get API
- if fs.exists("API") == false then
- shell.run("pastebin", "get", "EzkfU5ZM", "API")
- end
- shaka = require("API")
- ---connect monitor and reactor
- availableConnections = peripheral.getNames()
- for k, v in pairs(availableConnections) do
- if shaka.stringFind(v, "reactor") then
- reactorName = v
- reactor = peripheral.wrap(v)
- end
- if shaka.stringFind(v, "monitor") then
- monitor = peripheral.wrap(v)
- end
- end
- shaka.changeColors(colors.black, colors.white)
- shaka.clearScreen()
- ---check monitor and reactor connection success
- if monitor == nil then
- printError("No monitor connected, connect one via modem and hit any key.")
- os.pullEvent("key")
- os.reboot()
- else
- shaka.changeColors(colors.black, colors.green)
- print("> Monitor connected")
- end
- if reactor == nil then
- printError("No reactor connected, connect one via modem and hit any key.")
- os.pullEvent("key")
- os.reboot()
- else
- shaka.changeColors(colors.black, colors.green)
- print("> Reactor initialized.")
- end
- monitor.setBackgroundColor(colors.black)
- monitor.clear()
- ---check if names were entered correctly and connections are established
- for k, v in pairs(availableConnections) do
- if v == injector then
- inCheck = true
- elseif v == output then
- outCheck = true
- end
- end
- if inCheck == false or outCheck == false then
- term.clear()
- shaka.changeColors(colors.black, colors.red)
- shaka.centerText("Please configure! Opening file..", 3, term)
- for i = 1, 3 do
- shaka.changeColors(colors.black, colors.yellow)
- shaka.centerText("in " ..4-i, 5, term)
- sleep(1)
- end
- shell.run("edit", "startup")
- end
- --wrap remaining peripherals
- local energyIn = peripheral.wrap(injector)
- local energyOut = peripheral.wrap(output)
- -- for k, v in pairs(peripheral.getMethods(reactorName)) do
- -- print(v)
- -- end
- local function updateMonitor(message, colorBG, line, colorFG)
- monitor.setCursorPos(1, line)
- if colorFG == nil then
- colorFG = colors.black
- end
- shaka.changeColors(colorBG, colorFG, monitor)
- monitor.clearLine()
- monitor.write(message)
- end
- local function checkReadyToStart()
- local running = false
- ---check if reactor is ready to activate
- for k, v in pairs(reactor.getReactorInfo()) do
- -- print(k, v)
- if k == "failSafe" and v == false then
- reactor.toggleFailSafe()
- elseif k == "status" and v == "warming_up" then
- warmup = true
- energyOut.setSignalLowFlow(outputEnergy)
- elseif k == "status" and v =="running" then
- local currEnergy = energyOut.getSignalLowFlow()
- local currInput = energyIn.getSignalLowFlow()
- if currEnergy > outputEnergy then
- -- energyOut.setSignalLowFlow(currEnergy)
- outputEnergy = currEnergy
- end
- if currInput > inputEnergy then
- inputEnergy = currInput
- end
- return true
- elseif k == "temperature" and v >= 2000 then
- readyForActivation = true
- end
- end
- if warmup and readyForActivation then
- shaka.changeColors(colors.black, colors.green)
- print("Ready to start reactor")
- return true
- else
- return false
- end
- end
- checkReadyToStart()
- energyIn.setSignalLowFlow(inputEnergy)
- local function checkStability()
- local initialStatus = reactor.getReactorInfo()
- local initialSat = initialStatus.energySaturation
- local initialTemp = initialStatus.temperature
- local initialShield = initialStatus.fieldStrength
- sleep(0.5)
- local newStatus = reactor.getReactorInfo()
- local newSat = newStatus.energySaturation
- local newTemp = newStatus.temperature
- local newShield = newStatus.fieldStrength
- if initialSat < newSat then
- satStable = true
- else
- satStable = false
- end
- if initialShield < newShield then
- shieldStable = true
- else
- shieldStable = false
- end
- if initialTemp > newTemp and newTemp < targetTemp then
- tempStable = true
- else
- tempStable = false
- end
- if satStable and shieldStable and tempStable then
- return true
- else
- return false
- end
- end
- local function changePower(amount)
- outputEnergy = outputEnergy * amount
- energyOut.setSignalLowFlow(outputEnergy)
- end
- local function changeInput(amount)
- inputEnergy = inputEnergy * amount
- energyIn.setSignalLowFlow(inputEnergy)
- end
- function round(num)
- return tonumber(string.format("%.1f", num))
- end
- function doitall()
- while true do
- local stable = false
- local data = reactor.getReactorInfo()
- local fillPercent = (round(data.energySaturation/10000000))
- -- local shieldPercent = (round(data.fieldStrength/1000000))
- local shield = (data.fieldStrength/data.maxFieldStrength) * 100
- local shieldPercent = (round(shield))
- ----safety stuff
- if data.status == "running" and (shieldPercent < 10 or fillPercent < 10 or data.temperature > maxTemp) then
- reactor.stopReactor()
- print("EMERGENCY STOP")
- end
- ----state dependant scripts
- if data.status == "running" then
- if checkStability() then stable = true end
- if stable and fillPercent >= energySaturationMax and shieldPercent >= fieldStrengthMax and data.status == "running" then
- updateMonitor("Output ++", colors.gray, 8, colors.cyan)
- changePower(1.05)
- print("raised output to "..outputEnergy)
- end
- -- if stable and fillPercent - shieldPercent > 1 then
- -- print("Added input")
- -- updateMonitor("Input +", colors.gray, 8, colors.cyan)
- -- changeInput(1.01)
- -- elseif stable and shieldPercent - fillPercent > 1 then
- -- updateMonitor("Input -", colors.gray, 8, colors.cyan)
- -- changeInput(0.99)
- -- print("Reduced input")
- -- end
- -- if stable and shieldPercent < fillPercent and fillPercent > energySaturationMax and shieldPercent > fieldStrengthMax then
- -- changePower(1.01)
- -- changeInput(1.005)
- -- print("Raised output slightly")
- -- updateMonitor("Output +", colors.gray, 8, colors.cyan)
- -- end
- local en = math.floor(outputEnergy)
- local niceNumber = shaka.formatNumber(en)
- local x, y = monitor.getSize()
- updateMonitor("+ "..niceNumber, colors.gray, 1, colors.yellow)
- updateMonitor("= ".. shaka.formatNumber(math.floor(en - inputEnergy)), colors.gray, 3, colors.green)
- updateMonitor("- "..shaka.formatNumber(math.floor(inputEnergy)), colors.gray, 2, colors.red)
- -- updateMonitor(" ", colors.blue, 4, colors.black)
- updateMonitor("Shield "..(shieldPercent).."/"..fieldStrengthMax.."%", colors.black, y-2, colors.blue)
- monitor.setCursorPos(x, y-2)
- if shieldStable then
- monitor.setTextColor(colors.green)
- monitor.write("^")
- else
- monitor.setTextColor(colors.red)
- monitor.write("v")
- end
- updateMonitor("Energy "..fillPercent.."/"..energySaturationMax.. "%", colors.black, y-1, colors.purple)
- monitor.setCursorPos(x, y-1)
- if satStable then
- monitor.setTextColor(colors.green)
- monitor.write("^")
- else
- monitor.setTextColor(colors.red)
- monitor.write("v")
- end
- updateMonitor("Temp: "..math.floor(data.temperature), colors.black, y, colors.orange)
- monitor.setCursorPos(1, 6)
- if stable == false then
- updateMonitor("Stabilizing", colors.lightGray, 6, colors.black)
- elseif fillPercent <= energySaturationMax then
- updateMonitor("Waiting: Energy", colors.gray, 6, colors.purple)
- elseif shieldPercent <= fieldStrengthMax then
- updateMonitor("Waiting: Shield", colors.gray, 6, colors.blue)
- end
- elseif data.status == "cooling" or data.status == "stopping" then
- monitor.clear()
- updateMonitor("Reactor cooling down", colors.blue, 6)
- elseif data.status == "cold" then
- monitor.clear()
- updateMonitor("Reactor stopped", colors.white, 6)
- elseif data.status == "warming_up" then
- monitor.clear()
- if data.temperature < 2000 then
- updateMonitor("Reactor warming up", colors.orange, 6)
- else
- updateMonitor("Reactor ready to start", colors.red, 6)
- end
- end
- -- print(data.temperature)
- sleep(1)
- end
- end
- doitall()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement