Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- I DO NOT take credit for this script it is owned by Electronx3
- -- Youtube https://www.youtube.com/watch?v=Vxf3MHo2sA0
- -- I simple have it here to have it saved
- --peripheral config
- local sideGateOut = "right"
- local sideReactor = "back"
- --other config
- local autoRestart = true
- local maxRestartTrys = 3
- --Do not change anything below
- --program info
- local version = "1.0.0" --by electronx3 for MC 1.7.10 and 1.12.2
- --constants
- local val_1div3 = 1.0/3.0
- --emergency shutdown parameters
- local maxTemp = 8020.0
- local minField = 0.005
- local minFuel = 0.18
- --default parameters
- local defaultTemp = 8000.0
- local defaultField = 0.01
- local maxTempInc = 400.0 --max. temperature increase per tick
- local maxOutflow = 16000000.0
- local restartTemp = 7500.0
- local restartTrys = 0
- local chargeInflow = -1
- local shutDownField = 0.1
- --peripherals
- local reactor
- local gateIn
- local gateOut
- --info
- local info
- local currentField
- local currentFuel
- local stableTicks = 0
- --the program
- function updateInfo()
- info = reactor.getReactorInfo()
- if info == nil then
- error("Reactor has an invalid setup!")
- end
- currentField = info.fieldStrength / info.maxFieldStrength
- currentFuel = 1.0 - (info.fuelConversion / info.maxFuelConversion)
- end
- function isEmergency()
- return (info.status == "running" or info.status == "online" or info.status == "stopping") and (info.temperature > maxTemp or currentField < minField or currentFuel < minFuel)
- end
- function calcInflow(targetStrength, fieldDrainRate)
- return fieldDrainRate / (1.0 - targetStrength)
- end
- --creators of the setupPeripherals function: https://github.com/acidjazz/drmon/blob/master/drmon.lua
- function setupPeripherals()
- reactor = peripheral.wrap(sideReactor)
- gateIn = periphSearch("flux_gate")
- gateOut = peripheral.wrap(sideGateOut)
- --monitor = periphSearch("monitor")
- if reactor == null then
- error("No valid reactor was found!")
- end
- if gateIn == null then
- error("No valid input fluxgate was found!")
- end
- if gateOut == null then
- error("No valid output fluxgate was found!")
- end
- gateIn.setOverrideEnabled(true)
- gateIn.setFlowOverride(0)
- gateOut.setOverrideEnabled(true)
- gateOut.setFlowOverride(0)
- end
- --creators of the periphSearch function: https://github.com/acidjazz/drmon/blob/master/drmon.lua
- function periphSearch(type)
- local names = peripheral.getNames()
- local i, name
- for i, name in pairs(names) do
- if peripheral.getType(name) == type then
- return peripheral.wrap(name)
- end
- end
- return null
- end
- function setup()
- if chargeInflow == -1 then
- chargeInflow = 20000000
- end
- stableTicks = 0
- setupPeripherals()
- end
- function update()
- local newInflow = 0.0
- local newOutflow = 0.0
- while true do
- term.clear()
- updateInfo()
- local isStable = false
- if peripheral.wrap(sideGateOut) == null then
- reactor.stopReactor()
- newInflow = calcInflow(shutDownField, info.fieldDrainRate)
- error("Output gate missing!")
- end
- if isEmergency() == true then
- reactor.stopReactor()
- newInflow = calcInflow(shutDownField*2.0, info.fieldDrainRate)
- newOutflow = 0.0
- elseif info.status == "cold" or info.status == "offline" then
- newInflow = 0.0
- newOutflow = 0.0
- restartTrys = 0
- elseif info.status == "warming_up" or info.status == "charging" or info.status == "charged" then
- newInflow = chargeInflow
- newOutflow = 0.0
- reactor.activateReactor()
- elseif info.status == "running" or info.status == "online" then
- local temp = info.temperature
- if temp > 7994.0 and temp < 8002.0 then
- stableTicks = stableTicks + 1
- if stableTicks > 100 then
- isStable = true
- print("stable")
- end
- else
- stableTicks = 0
- end
- if temp < defaultTemp then
- local tempInc
- if temp < defaultTemp - 50.0 then
- tempInc = math.sqrt(defaultTemp - temp)
- elseif temp < defaultTemp - 0.5 then
- tempInc = math.sqrt(defaultTemp - temp)/2.0
- end
- if tempInc == nil or tempInc < 0.0 then
- tempInc = 0
- elseif tempInc > maxTempInc then
- tempInc = maxTempInc
- end
- local t50 = temp/200.0
- local convLvl = (info.fuelConversion / info.maxFuelConversion)*1.3 - 0.3
- local y = (t50^4)/(100.0-t50)*(1-convLvl) + 1000.0*(tempInc-convLvl) - 444.7
- local dSqrt = math.sqrt((50.0*y)^2 + (y/3.0)^3)
- local x
- local tmpValRoot = dSqrt - 50.0*y
- if tmpValRoot > 0.0 then
- x = math.pow(dSqrt + 50.0*y, val_1div3) - math.pow(tmpValRoot, val_1div3)
- else
- x = math.pow(dSqrt + 50.0*y, val_1div3) + math.pow(0.0-tmpValRoot, val_1div3)
- end
- newOutflow = info.maxEnergySaturation*x/99.0 + info.energySaturation - info.maxEnergySaturation
- if newOutflow > maxOutflow then
- newOutflow = maxOutflow
- end
- else
- newOutflow = 0.0
- end
- if isStable == true then
- if currentField > defaultField + 0.03 then
- newInflow = 0.0
- elseif currentField > defaultField*1.2 then
- newInflow = calcInflow(defaultField*0.98, info.fieldDrainRate)
- elseif currentField > defaultField*0.97 then
- newInflow = calcInflow(defaultField, info.fieldDrainRate)
- else
- newInflow = calcInflow(defaultField*1.5, info.fieldDrainRate)
- end
- else
- if currentField > shutDownField then
- newInflow = calcInflow(shutDownField, info.fieldDrainRate)
- else
- newInflow = calcInflow(shutDownField*2.0, info.fieldDrainRate)
- end
- end
- elseif info.status == "stopping" then
- if currentField > shutDownField then
- newInflow = calcInflow(shutDownField, info.fieldDrainRate)
- else
- newInflow = calcInflow(shutDownField*2.0, info.fieldDrainRate)
- end
- newOutflow = 0.0
- if autoRestart and info.temperature < restartTemp and restartTrys < maxRestartTrys then
- restartTrys = restartTrys + 1
- reactor.activateReactor()
- end
- end
- if newInflow < 0.0 then
- newInflow = 0.0
- end
- if newOutflow < 0.0 then
- newOutflow = 0.0
- end
- if newInflow > 0.0 then
- newInflow = math.floor(newInflow)
- else
- newInflow = 0
- end
- if newOutflow > 0.0 then
- newOutflow = math.floor(newOutflow)
- else
- newOutflow = 0
- end
- gateIn.setFlowOverride(newInflow)
- gateOut.setFlowOverride(newOutflow)
- print("version: " .. version)
- if isStable then
- print("status: " .. info.status .. " (stable)")
- else
- print("status: " .. info.status)
- end
- print("temp: " .. math.floor(info.temperature) .. " K")
- print("inflow: " .. newInflow .. " RF/t")
- print("outflow: " .. newOutflow .. " RF/t")
- print("gain: " .. math.floor((newOutflow - newInflow)/100.0)/10.0 .. " kRF/t")
- sleep(0.02)
- end
- end
- --------------------------
- function main()
- setup()
- update()
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement