neuroticfox

Draconic Control Redux Lib

May 27th, 2026 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.17 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.   local minField     = 0.0
  57.   if info.temperature > 9000 then
  58.     minField = (0.10 + (info.temperature / 500)) / 100
  59.   end
  60.   local effectiveShield = math.max(targetShield, minField)
  61.   local fsError     = (info.maxFieldStrength * effectiveShield) - info.fieldStrength
  62.   local reqInput    = math.min(
  63.     M.safeDiv(info.maxFieldStrength * info.fieldDrainRate, info.maxFieldStrength - info.fieldStrength, 0),
  64.     info.maxFieldStrength - info.fieldStrength
  65.   )
  66.  
  67.   local outputNeeded = math.min(satError, info.maxEnergySaturation / 40) + (info.generationRate or 0)
  68.  
  69.   local inputCmd     = math.min(fsError + reqInput, info.maxFieldStrength)
  70.  
  71.   return {
  72.     satError           = satError,
  73.     fieldStrengthError = fsError,
  74.     requiredInput      = reqInput,
  75.     outputNeeded       = outputNeeded,
  76.     inputCommand       = inputCmd,
  77.     targetCoreSat      = targetCoreSat,
  78.     tempDrainFactor    = tempDrainFactor,
  79.     convLVL            = convLVL,
  80.     targetTempResist   = targetTempResist,
  81.     minField           = minField,
  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.     energySaturation    = tonumber(s.energySaturation)    or 0,
  94.     maxEnergySaturation = tonumber(s.maxEnergySaturation) or 0,
  95.     fuelConversion      = tonumber(s.fuelConversion)      or 0,
  96.     maxFuelConversion   = tonumber(s.maxFuelConversion)   or 0,
  97.     fuelConversionRate  = tonumber(s.fuelConversionRate)  or 0,
  98.     generationRate      = tonumber(s.generationRate)      or 0,
  99.     fieldDrainRate      = tonumber(s.fieldDrainRate)      or 0,
  100.   }
  101. end
  102.  
  103. function M.rawSetGate(g, v)
  104.   if not g then return false end
  105.   v = math.floor(M.clamp(v or 0, 0, M.MAX_GATE_FLOW) + 0.5)
  106.   pcall(function() g.setOverrideEnabled(true) end)
  107.   return pcall(function() g.setFlowOverride(v) end)
  108. end
  109.  
  110. function M.rawGetGate(g)
  111.   if not g then return 0 end
  112.   local ok, v = pcall(function() return g.getFlowOverride() end)
  113.   if ok then return tonumber(v) or 0 end
  114.   ok, v = pcall(function() return g.getFlow() end)
  115.   if ok then return tonumber(v) or 0 end
  116.   return 0
  117. end
  118.  
  119. function M.formatRF(n)
  120.   n = tonumber(n) or 0
  121.   if math.abs(n) >= 1e9 then return string.format("%.2f GRF/t", n/1e9) end
  122.   if math.abs(n) >= 1e6 then return string.format("%.2f MRF/t", n/1e6) end
  123.   if math.abs(n) >= 1e3 then return string.format("%.2f kRF/t", n/1e3) end
  124.   return string.format("%d RF/t", math.floor(n+0.5))
  125. end
  126.  
  127. function M.formatPct(p)
  128.   return string.format("%.1f%%", M.clamp(tonumber(p) or 0, 0, 1)*100)
  129. end
  130.  
  131. function M.formatETA(sec)
  132.   sec = tonumber(sec)
  133.   if not sec or sec ~= sec or sec == math.huge or sec < 0 then return "--:--:--" end
  134.   if sec > 359999 then return ">99h" end
  135.   sec = math.floor(sec + 0.5)
  136.   local h = math.floor(sec / 3600)
  137.   local m = math.floor((sec % 3600) / 60)
  138.   local s = sec % 60
  139.   return string.format("%d:%02d:%02d", h, m, s)
  140. end
  141.  
  142. return M
Advertisement
Add Comment
Please, Sign In to add comment