neuroticfox

DC17 Redux (Lib)

Nov 22nd, 2026 (edited)
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.46 KB | None | 0 0
  1. --[[
  2.   dclib.lua — shared control math library for dc1 and dcmaster
  3.   Require this from both scripts: local dclib = require("dclib")
  4. ]]
  5.  
  6. local M = {}
  7.  
  8. M.MAX_GATE_FLOW      = 2147000000
  9. M.WARMUP_INPUT_RF_T  = 5000000
  10. M.PROBE_INPUT_RF_T   = 1
  11. M.PROBE_TICK_S       = 0.05
  12. M.MC_TICKS_PER_S     = 20
  13. M.FUEL_CUTOFF        = 0.875
  14. -- EMA_ALPHA removed: output gate is set directly each tick, no smoothing.
  15.  
  16. function M.safeDiv(a, b, fb)
  17.   a, b = tonumber(a) or 0, tonumber(b) or 0
  18.   if b == 0 then return fb or 0 end
  19.   return a / b
  20. end
  21.  
  22. function M.clamp(v, lo, hi)
  23.   v = tonumber(v) or lo
  24.   if v ~= v then return lo end
  25.   return math.max(lo, math.min(hi, v))
  26. end
  27.  
  28. function M.computeControl(info, targetTemp, targetShield, multiplier)
  29.   if not info then return {} end
  30.  
  31.   local targetTemp50  = math.min((targetTemp / 10000) * 50, 99)
  32.   local coreSat       = M.safeDiv(info.energySaturation, info.maxEnergySaturation, 0)
  33.   local convLVL       = (M.safeDiv(info.fuelConversion, info.maxFuelConversion, 0) * 1.3) - 0.3
  34.  
  35.   local targetTempResist = (targetTemp50^4) / (100 - targetTemp50)
  36.   local targetTempExpo   = -(targetTempResist * convLVL) - 1000*convLVL + targetTempResist
  37.  
  38.   local term1      = 1334.1 - (3 * targetTempExpo)
  39.   local term2      = (1200690 - (2700 * targetTempExpo))^2
  40.   local inner      = (-4*term1^3 + term2)
  41.   local sqrtInner  = inner >= 0 and inner^0.5 or 0
  42.   local term3      = ((-1350*targetTempExpo) + sqrtInner/2 + 600345)^(1/3)
  43.   local targetNegCSat = -(term1/(3*term3)) - (term3/3)
  44.   local targetCoreSat = 1 - (targetNegCSat / 99)
  45.   local targetSat     = targetCoreSat * info.maxEnergySaturation
  46.   local satError      = info.energySaturation - targetSat
  47.  
  48.   local tempDrainFactor = 0
  49.   local t = info.temperature
  50.   if     t > 8000 then tempDrainFactor = 1 + (t-8000)^2 * 0.0000025
  51.   elseif t > 2000 then tempDrainFactor = 1
  52.   elseif t > 1000 then tempDrainFactor = (t-1000)/1000
  53.   end
  54.  
  55.   local baseMaxRFt   = (info.maxEnergySaturation / 1000) * multiplier * 1.5
  56.   -- A phase-aligned DE core starts each tick at its previous post-drain field.
  57.   local field = info.fieldStrength
  58.   local capacity = info.maxFieldStrength
  59.   if info.fieldSampleAfterDrain and info.fieldSampleStrength
  60.       and info.fieldSampleCapacity and info.fieldSampleCapacity > 0 then
  61.     field = info.fieldSampleStrength
  62.     capacity = info.fieldSampleCapacity
  63.   end
  64.   local fsError = capacity * targetShield - field
  65.   local headroom = capacity - field
  66.   local reqInput = math.min(M.safeDiv(capacity * info.fieldDrainRate, headroom, 0), headroom)
  67.  
  68.   local outputNeeded = math.min(satError, info.maxEnergySaturation / 40) + (info.generationRate or 0)
  69.  
  70.   local inputCmd     = math.min(fsError + reqInput, capacity)
  71.  
  72.   return {
  73.     satError           = satError,
  74.     fieldStrengthError = fsError,
  75.     requiredInput      = reqInput,
  76.     outputNeeded       = outputNeeded,
  77.     inputCommand       = inputCmd,
  78.     targetCoreSat      = targetCoreSat,
  79.     tempDrainFactor    = tempDrainFactor,
  80.     convLVL            = convLVL,
  81.     targetTempResist   = targetTempResist,
  82.   }
  83. end
  84.  
  85. function M.readInfo(reactor)
  86.   local ok, s = pcall(function() return reactor.getReactorInfo() end)
  87.   if not ok or not s then return nil end
  88.   return {
  89.     status              = tostring(s.status or "unknown"),
  90.     temperature         = tonumber(s.temperature)         or 0,
  91.     fieldStrength       = tonumber(s.fieldStrength)       or 0,
  92.     maxFieldStrength    = tonumber(s.maxFieldStrength)    or 0,
  93.     fieldSampleStrength = tonumber(s.fieldSampleStrength),
  94.     fieldSampleCapacity = tonumber(s.fieldSampleCapacity),
  95.     fieldSampleTick     = tonumber(s.fieldSampleTick),
  96.     fieldSampleAfterDrain = s.fieldSampleAfterDrain == true,
  97.     energySaturation    = tonumber(s.energySaturation)    or 0,
  98.     maxEnergySaturation = tonumber(s.maxEnergySaturation) or 0,
  99.     fuelConversion      = tonumber(s.fuelConversion)      or 0,
  100.     maxFuelConversion   = tonumber(s.maxFuelConversion)   or 0,
  101.     fuelConversionRate  = tonumber(s.fuelConversionRate)  or 0,
  102.     generationRate      = tonumber(s.generationRate)      or 0,
  103.     fieldDrainRate      = tonumber(s.fieldDrainRate)      or 0,
  104.   }
  105. end
  106.  
  107. function M.rawSetGate(g, v)
  108.   if not g then return false end
  109.   v = math.floor(M.clamp(v or 0, 0, M.MAX_GATE_FLOW) + 0.5) + 0.0
  110.   pcall(function() g.setOverrideEnabled(true) end)
  111.   return pcall(function() g.setFlowOverride(v) end)
  112. end
  113.  
  114. function M.rawGetGate(g)
  115.   if not g then return 0 end
  116.   local ok, v = pcall(function() return g.getFlowOverride() end)
  117.   if ok then return tonumber(v) or 0 end
  118.   ok, v = pcall(function() return g.getFlow() end)
  119.   if ok then return tonumber(v) or 0 end
  120.   return 0
  121. end
  122.  
  123. function M.formatRF(n)
  124.   n = tonumber(n) or 0
  125.   if math.abs(n) >= 1e9 then return string.format("%.2f GRF/t", n/1e9) end
  126.   if math.abs(n) >= 1e6 then return string.format("%.2f MRF/t", n/1e6) end
  127.   if math.abs(n) >= 1e3 then return string.format("%.2f kRF/t", n/1e3) end
  128.   return string.format("%d RF/t", math.floor(n+0.5))
  129. end
  130.  
  131. function M.formatPct(p)
  132.   return string.format("%.1f%%", M.clamp(tonumber(p) or 0, 0, 1)*100)
  133. end
  134.  
  135. function M.formatETA(sec)
  136.   sec = tonumber(sec)
  137.   if not sec or sec ~= sec or sec == math.huge or sec < 0 then return "--:--:--" end
  138.   if sec > 359999 then return ">99h" end
  139.   sec = math.floor(sec + 0.5)
  140.   local h = math.floor(sec / 3600)
  141.   local m = math.floor((sec % 3600) / 60)
  142.   local s = sec % 60
  143.   return string.format("%d:%02d:%02d", h, m, s)
  144. end
  145.  
  146. return M
Advertisement
Add Comment
Please, Sign In to add comment