This commit is contained in:
Navid Sassan 2025-12-09 23:31:36 +01:00
parent 89a5f6d02e
commit d1c2f57891

View File

@ -47,16 +47,19 @@ local CONFIG = {
-- Pros: Absolute maximum output
-- Cons: Very easy to explode, not worth the risk
-- =======================================================================
target_field_strength = 0.30,
target_field_strength = 0.25,
-- Safety thresholds
min_field_strength = 0.20, -- Emergency shutdown below this (8%)
min_field_strength = 0.15, -- Emergency shutdown below this
max_temperature = 8000, -- Emergency shutdown above this (Kelvin)
critical_temperature = 7500, -- Start reducing output at this temp
critical_temperature = 7800, -- Start reducing output at this temp
-- Saturation control for optimal fuel conversion
min_saturation = 0.20, -- Below this, reactor produces less power
target_saturation = 0.45, -- Target saturation for optimal output (lower = more power)
target_saturation = 0.30, -- Target saturation for optimal output (lower = more power)
-- Temperature targeting for power optimization
target_temperature = 6000, -- Optimal temperature for power output
-- Fuel monitoring
fuel_warning_threshold = 0.85, -- Warn when fuel is 90% used
@ -118,6 +121,9 @@ local lastOutputRate = -1
-- Fuel warning throttle
local lastFuelWarning = 0
-- Optimization status (for display)
local optimizationStatus = "OFFLINE"
-- ============================================================================
-- UTILITY FUNCTIONS
-- ============================================================================
@ -410,48 +416,68 @@ local function calculateOutputRate()
result = math.floor(generationRate * 0.8)
reason = state .. " - 80% of generation"
else
-- Normal operation with saturation targeting
-- Normal operation with temperature and saturation targeting
local targetTemp = CONFIG.target_temperature
local targetSat = CONFIG.target_saturation
local limitingFactor = "OPTIMAL"
-- Temperature factor: controls output based on temp vs target
local tempFactor = 1.0
local tempCritical = false
if temp > CONFIG.critical_temperature then
-- Above critical: reduce output significantly for safety
local overTemp = temp - CONFIG.critical_temperature
local tempRange = CONFIG.max_temperature - CONFIG.critical_temperature
tempFactor = 1.0 - (overTemp / tempRange) * 0.5
tempFactor = 1.0 - (overTemp / tempRange) * 0.8
tempCritical = true
limitingFactor = "TEMP_CRITICAL"
elseif temp > targetTemp then
-- Above target but below critical: increase output to cool down
local overTarget = temp - targetTemp
local targetRange = CONFIG.critical_temperature - targetTemp
tempFactor = 1.0 + (overTarget / targetRange) * 0.5 -- Up to 1.5x
limitingFactor = "TEMP_HIGH"
elseif temp < targetTemp * 0.8 then
-- Well below target: reduce output to let temp rise
local underTarget = targetTemp - temp
tempFactor = 0.5 + (temp / targetTemp) * 0.5 -- 0.5x to 1.0x
limitingFactor = "TEMP_LOW"
end
-- Saturation-based output adjustment
-- Target saturation for optimal fuel conversion (lower sat = more power)
local targetSat = CONFIG.target_saturation
-- Saturation-based output adjustment (more aggressive: 0.3x to 2.0x)
local satError = saturation - targetSat -- positive = too high, negative = too low
-- Scale factor: adjust output by up to 50% based on saturation error
local satFactor = 1.0 + satError
satFactor = math.max(0.5, math.min(1.5, satFactor)) -- Clamp to 50%-150%
-- Quadratic scaling for more aggressive response when far from target
local satFactor = 1.0 + satError * 2.0
satFactor = math.max(0.3, math.min(2.0, satFactor)) -- Clamp to 30%-200%
-- IMPORTANT: If temperature is critical, don't increase output to drain saturation
-- Temperature safety takes priority over saturation optimization
-- Track if saturation is the limiting factor
if satError > 0.1 and limitingFactor == "OPTIMAL" then
limitingFactor = "SAT_HIGH"
elseif satError < -0.1 and limitingFactor == "OPTIMAL" then
limitingFactor = "SAT_LOW"
end
-- SAFETY: If temperature is critical, don't increase output
if tempCritical and satFactor > 1.0 then
satFactor = 1.0 -- Cap at 100%, don't try to drain saturation when hot
satFactor = 1.0
end
-- SAFETY: If temp is low, don't increase output even if sat is high
if temp < targetTemp * 0.8 and satFactor > 1.0 then
satFactor = 1.0
end
local outputRate = generationRate * tempFactor * satFactor
-- Build reason string
local satPct = string.format("%.0f%%", saturation * 100)
local targetPct = string.format("%.0f%%", targetSat * 100)
if tempCritical then
reason = "TEMP CRITICAL " .. formatTemp(temp) .. ", factor=" .. string.format("%.2f", tempFactor)
if satFactor < 1.0 then
reason = reason .. ", sat " .. satPct .. " low"
end
elseif satError > 0.05 then
reason = "sat " .. satPct .. " > " .. targetPct .. " target, +" .. string.format("%.0f%%", (satFactor - 1) * 100) .. " output"
elseif satError < -0.05 then
reason = "sat " .. satPct .. " < " .. targetPct .. " target, " .. string.format("%.0f%%", (satFactor - 1) * 100) .. " output"
else
reason = "sat " .. satPct .. " at target"
end
local tempPct = string.format("%.0fK", temp)
local factors = string.format("temp=%.2fx sat=%.2fx", tempFactor, satFactor)
reason = limitingFactor .. " | " .. tempPct .. " " .. satPct .. " | " .. factors
-- Update global optimization status for display
optimizationStatus = limitingFactor
result = math.max(0, math.floor(outputRate))
end
@ -1014,6 +1040,24 @@ local function updateDisplay()
end
y = y + 2
-- Optimization status
mon.setCursorPos(1, y)
mon.setTextColor(colors.white)
mon.write("Optimization: ")
local statusColor = colors.lime
if optimizationStatus == "TEMP_CRITICAL" then
statusColor = colors.red
elseif optimizationStatus == "TEMP_HIGH" or optimizationStatus == "TEMP_LOW" then
statusColor = colors.orange
elseif optimizationStatus == "SAT_HIGH" or optimizationStatus == "SAT_LOW" then
statusColor = colors.yellow
elseif optimizationStatus == "OFFLINE" then
statusColor = colors.gray
end
mon.setTextColor(statusColor)
mon.write(optimizationStatus)
y = y + 2
-- Instructions
mon.setCursorPos(1, y)
mon.setTextColor(colors.gray)