CCCPaste

Draconic Reactor Controller

-- draconic_reactor_controller.lua
-- Works *only* with getReactorInfo() — safe & stable controller

--------------- CONFIG ----------------
local TARGET_TEMP = 7600
local EMERGENCY_TEMP = 7900
local MIN_FIELD = 10                 -- percent
local SAFETY_FACTOR = 0.85           -- usable fraction of available power
local KP = 25000                     -- RF/t per degree of error (tune if needed)
local INJECTOR_FROM_OUTPUT = true    -- set false if injector is externally powered
local LOOP_DELAY = 0.2
----------------------------------------

-- Find peripherals
local reactor = peripheral.find("draconic_reactor") 
    or error("No draconic_reactor peripheral found!")

local fluxOut = peripheral.find("flux_gate") 
    or error("No flux_gate peripheral found!")

if not fluxOut.setFlowOverride then
    error("Flux gate missing setFlowOverride()!")
end


-- Main loop
while true do
    local info = reactor.getReactorInfo()

    if not info or not info.temperature then
        print("Reactor not providing valid info.")
        sleep(1)
    else
        local T = info.temperature
        local G = info.generation or 0
        local F = info.fieldDrainRate or 0
        local fieldPct = info.fieldStrength or 100

        -- Emergency shutdown
        if T >= EMERGENCY_TEMP or fieldPct <= MIN_FIELD then
            fluxOut.setFlowOverride(0)
            print("!!! EMERGENCY SHUTDOWN !!!")
            print("Temp: ", T, " Field: ", fieldPct)
            if reactor.stopReactor then reactor.stopReactor() end
            sleep(99999)
        end

        -- Calculate available power
        local A
        if INJECTOR_FROM_OUTPUT then
            A = G - F
        else
            A = G
        end

        if A < 0 then A = 0 end

        -- Apply proportional control
        local errorTemp = (T - TARGET_TEMP)
        local correction = KP * errorTemp

        local limit = SAFETY_FACTOR * A - correction
        if limit < 0 then limit = 0 end

        fluxOut.setFlowOverride(math.floor(limit))

        -- Display
        term.clear()
        term.setCursorPos(1,1)
        print("=== Draconic Reactor Controller ===")
        print("Temperature:      ", T)
        print("Generation:       ", G)
        print("Field Drain:      ", F)
        print("Field %:          ", fieldPct)
        print("Available Power:  ", A)
        print("-------------------------------------")
        print("Output Limit Set: ", limit)
        print("Temp Error:       ", errorTemp)
        print("-------------------------------------")

    end

    sleep(LOOP_DELAY)
end