Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- modifiablew variables
- local targetStrength = 50
- local maxTemperature = 8000
- local safeTemperature = 3000
- local lowestFieldPercent = 15
- local targetSaturation = 50
- local activateOnCharged = 1
- -- please leave things untouched from here on
- local component = require("component")
- local fs = require("filesystem")
- local term = require("term")
- local serial = require("serialization")
- local version = "0.1"
- local cfgPath = "/etc/reactor_config.cfg"
- -- toggleable via the monitor, use our algorithm to achieve our target field strength or let the user tweak it
- local autoInputGate = 1
- local autoOutputGate = 1
- local curInputGate = 222000
- -- monitor
- local mon, monitor, monX, monY
- -- peripherals
- local monitor = component.proxy(component.get("46ac1d01", "screen"))
- local reactor = component.proxy(component.list("draconic_reactor")())
- local fluxgate = component.proxy(component.get("a804cf49", "flux_gate"))
- local inputfluxgate = component.proxy(component.get("747f39d9", "flux_gate"))
- -- reactor information
- local ri
- -- last performed action
- local action = "None since reboot"
- local emergencyCharge = false
- local emergencyTemp = false
- if monitor == null then
- error("No valid monitor was found")
- end
- if fluxgate == null then
- error("No valid flux gate was found")
- end
- if reactor == null then
- error("No valid reactor was fouind")
- end
- if inputfluxgate == null then
- error("No valid flux gate was found")
- end
- -- TODO: monitor dimensions
- -- monX, monY = monitor.getSize()
- -- mon = {}
- -- mon.monitor, mon.X, mon.Y = monitor, monX, monY
- -- write settings to config file
- function save_config()
- local cfg = {}
- cfg.version = version
- cfg.autoInputGate = autoInputGate
- cfg.curInputGate = curInputGate
- sw = fs.open(cfgPath, "w")
- sw:write(serial.serialize(cfg))
- sw:close()
- end
- -- read settings from file
- function load_config()
- sw = fs.open(cfgPath, "r")
- local data = sw:read(fs.size(cfgPath))
- local cfg = serial.unserialize(data)
- sw:close()
- version = cfg.version
- autoInputGate = cfg.autoInputGate
- curInputGate = cfg.curInputGate
- end
- -- 1st time? save our settings, if not, load our settings
- if fs.exists(cfgPath) == false then
- save_config()
- else
- load_config()
- end
- function update()
- while true do
- term.clear()
- ri = reactor.getReactorInfo()
- -- print out all the infos from .getReactorInfo() to term
- if ri == nil then
- error("reactor has an invalid setup")
- end
- for k, v in pairs(ri) do
- print(tostring(k).. ": " .. tostring(v))
- end
- print("Output Gate: ", fluxgate.getSignalLowFlow())
- print("Input Gate: ", inputfluxgate.getSignalLowFlow())
- -- actual reactor interaction
- --
- if emergencyCharge == true then
- reactor.chargeReactor()
- end
- -- are we charging? open the floodgates
- if ri.status == "warming_up" then
- inputfluxgate.setSignalLowFlow(900000)
- emergencyCharge = false
- end
- -- are we stopping from a shutdown and our temp is better? activate
- if emergencyTemp == true and ri.status == "cooling" and ri.temperature < safeTemperature then
- reactor.activatereactor()
- emergencyTemp = false
- end
- -- are we charged? lets activate
- if ri.status == "warming_up" and ri.temperature >= 2000 then
- reactor.activateReactor()
- print("Activating!!!")
- end
- -- are we on? regulate the input floodgate to our target field strength
- -- or set it to our saved setting since we are on manual
- if ri.status == "running" then
- if autoInputGate == 1 then
- fluxval = ri.fieldDrainRate / (1 - (targetStrength/100))
- print("Target Gate: " .. fluxval)
- inputfluxgate.setSignalLowFlow(fluxval)
- else
- inputfluxgate.setSignalLowFlow(curInputGate)
- end
- if autoOutputGate == 1 then
- local rate = ((targetSaturation * 10000000) - ri.energySaturation) / ri.temperature
- local target = ri.generationRate - rate
- print("Target output gate: " .. target)
- fluxgate.setSignalLowFlow(target)
- end
- end
- -- safeguards
- --
- -- out of fuel, kill it
- local fuelPercent = 100 - math.ceil(ri.fuelConversion / ri.maxFuelConversion * 10000) * .01
- if fuelPercent <= 10 then
- reactor.stopReactor()
- action = "Fuel below 10%, refuel"
- end
- -- field strength is too dangerous, kill it and try and charge it before it blows
- local fieldPercent = math.ceil(ri.fieldStrength / ri.maxFieldStrength * 10000) * .01
- if fieldPercent <= lowestFieldPercent and ri.status == "running" then
- action = "Field Str < " .. lowestFieldPercent .. "%"
- reactor.stopReactor()
- reactor.chargeReactor()
- emergencyCharge = true
- end
- -- temperature too high, kill it and activate it when its cool
- if ri.temperature > maxTemperature then
- reactor.stopReactor()
- action = "Temp > " .. maxTemperature
- emergencyTemp = true
- end
- os.sleep(0.1)
- end
- end
- update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement