Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = {...}
- bgColor = colors.black
- txtColor = colors.white
- numColor = colors.red
- flowGates = {}
- inputFlux = nil
- outputFlux = nil
- reactor = nil
- tickCount = 00
- -- Safety Checks
- local deviceList = peripheral.getNames()
- for i = 1, #deviceList do
- local device = peripheral.getType(deviceList[i])
- if device == "flow_gate" then
- table.insert(flowGates, deviceList[i])
- elseif device == "draconic_reactor" then
- reactor = peripheral.wrap(deviceList[i])
- elseif device == "monitor" then
- mon = peripheral.wrap(deviceList[i])
- end
- end
- function clear()
- if mon.isColor() == true then
- mon.setBackgroundColor(bgColor)
- end
- mon.clear()
- mon.setCursorPos(1,1)
- end
- clear()
- local info = reactor.getReactorInfo()
- flux_in = peripheral.wrap(flowGates[1])
- flux_out = peripheral.wrap(flowGates[2])
- -- Set Values
- local targetTemp = tonumber(tArgs[1]) or 8000 -- Temperature at which to hold the reactor
- local targetShield = tonumber(tArgs[2]) or 5 -- Desired shield strength to hold the reactor at. Ranges from 0.01 for 1% shield, to 1 for 100%
- local reactorOutputMultiplier = tonumber(tArgs[3]) or 10 -- Check "Mod Options > Draconic Evolution > Tweaks > reactorOutputMultiplier" to find what it is and change the variable below to match.
- -- Yield
- local function yield()
- os.queueEvent("someFakeEventName") -- queue the event
- os.pullEvent("someFakeEventName") -- pull it
- end
- -- AutoSet Gates
- reactor.chargeReactor()
- satOne = info.energySaturation
- fieldOne = info.fieldStrength
- flux_in.setSignalLowFlow(1.0)
- os.sleep(0.5)
- satTwo = info.energySaturation
- fieldTwo = info.fieldStrength
- flux_in.setSignalLowFlow(0.0)
- reactor.stopReactor()
- if satTwo <= satOne or fieldTwo <= fieldOne then
- flux_in = peripheral.wrap(flowGates[2])
- flux_out = peripheral.wrap(flowGates[1])
- end
- -- Functions
- reactor.chargeReactor()
- function exit_msg(msg)
- term.clear()
- print(msg)
- os.exit()
- end
- function modify_temp(offset)
- local new_temp = ideal_temp + offset
- if new_temp > 8000 then
- new_temp = 8000
- elseif new_temp < 2500 then
- new_temp = 2500
- end
- ideal_temp = new_temp
- end
- function modify_field(offset)
- local new_strength = ideal_strength + offset
- if new_strength > 90 then
- new_strength = 90
- elseif new_strength < 1 then
- new_strength = 1
- end
- ideal_strength = new_strength
- end
- -- main code
- flux_in.setSignalLowFlow(0)
- flux_out.setSignalLowFlow(0)
- local condition = reactor.getReactorInfo()
- if not condition then
- print("Reactor not initialized, please ensure the stabilizers are properly laid out.")
- os.exit()
- end
- ideal_strength = 50
- ideal_temp = 8000
- cutoff_temp = 8100
- -- tweakable pid gains
- inflow_P_gain = 1
- inflow_I_gain = 0.04
- inflow_D_gain = 0.1
- outflow_P_gain = 500
- outflow_I_gain = 0.5
- outflow_II_gain = 0.0000003
- outflow_D_gain = 60000
- -- initialize main loop
- inflow_I_sum = 0
- inflow_D_last = 0
- outflow_I_sum = 0
- outflow_II_sum = 0
- outflow_D_last = 0
- state = "Standby"
- shutting_down = false
- if condition.temperature > 25 then
- state = "Cooling"
- end
- if condition.temperature > 2000 then
- state = "Active"
- end
- -- Possible states:
- --Standby
- --Charging
- --Active
- --Manual Shutdown
- --Emergency Shutdown
- --Cooling
- event_loop = true
- while event_loop do
- reactor.getReactorInfo()
- if info.status == warming_up and info.temperature >= 2000 then
- reactor.activateReactor()
- end
- local info = reactor.getReactorInfo()
- local inflow = 0
- local outflow = 0
- shutting_down = state == "Manual Shutdown" or state == "Emergency Shutdown"
- running = state == "Charging" or state == "Active"
- safe = state == "Standby" or state == "Cooling"
- if state == "Charging" then
- inflow = 200000
- if info.temperature > 2000 then
- reactor.activateReactor()
- state = "Active"
- end
- elseif state == "Cooling" then
- if info.temperature < 25 then
- state = "Standby"
- end
- inflow = 10
- outflow = 20
- elseif state == "Standby" then
- inflow = 10
- outflow = 20
- else
- -- adjust inflow rate based on field strength
- local field_error = (info.maxFieldStrength * (ideal_strength / 100)) - info.fieldStrength
- local proportional_field_error = field_error * inflow_P_gain
- inflow_I_sum = inflow_I_sum + field_error
- local integral_field_error = inflow_I_sum * inflow_I_gain
- local derivative_field_error = (field_error - inflow_D_last) * inflow_D_gain
- inflow_D_last = field_error
- local inflow_correction = proportional_field_error + integral_field_error + derivative_field_error
- if inflow_correction < 0 then
- inflow_I_sum = inflow_I_sum - field_error
- end
- inflow = inflow_correction
- if not shutting_down then
- -- adjust outflow rate based on core temperature
- local temp_error = ideal_temp - info.temperature
- local proportional_temp_error = temp_error * outflow_P_gain
- outflow_I_sum = outflow_I_sum + temp_error
- local integral_temp_error = outflow_I_sum * outflow_I_gain
- if math.abs(temp_error) < 100 then
- outflow_II_sum = outflow_II_sum + integral_temp_error
- else
- outflow_II_sum = 0
- end
- local second_integral_temp_error = outflow_II_sum * outflow_II_gain
- local derivative_temp_error = (temp_error - outflow_D_last) * outflow_D_gain
- outflow_D_last = temp_error
- local outflow_correction = proportional_temp_error + integral_temp_error + second_integral_temp_error + derivative_temp_error
- if outflow_correction < 0 then
- outflow_I_sum = outflow_I_sum - temp_error
- end
- outflow = outflow_correction
- -- cut off reactor in case of emergency
- if info.temperature > cutoff_temp then
- print("Reactor temperature greater than", cutoff_temp, ", failsafe activated, shutting down")
- outflow = 0
- state = "Emergency Shutdown"
- reactor.stopReactor()
- end
- else
- if info.temperature < 2000 then
- state = "Cooling"
- end
- end
- end
- if state ~= "Active" and not shutting_down then
- inflow_I_sum = 0
- inflow_D_last = 0
- outflow_I_sum = 0
- outflow_II_sum = 0
- outflow_D_last = 0
- end
- if inflow < 0 then
- inflow = 0
- end
- if outflow < 0 then
- outflow = 0
- end
- inflow = math.floor(inflow*reactorOutputMultiplier)
- outflow = math.floor(outflow*reactorOutputMultiplier)
- flux_in.setSignalLowFlow(inflow*reactorOutputMultiplier)
- flux_out.setSignalLowFlow(outflow*reactorOutputMultiplier)
- yield()
- end
- term.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement