namespace XFEEDToolBox.Core.Navigation;
// Rates are percent of this ship's calibrated heat capacity per second, never degrees Celsius.
// Bounds must come from native/observed calibration for this exact build, loadout and star environment.
public sealed record ThermalDistanceBand(double InnerRadius,double OuterRadius,double AmbientGainUpperPerSecond,double CoolingLowerPerSecond);
// Percent/second and percent thresholds, converted from the verified native capacity/configuration.
public sealed record ThermalCoolingCurve(double PassiveLowerPerSecond,double MaximumLowerPerSecond,double HeatLower,double HeatUpper,double Exponent);
public sealed record ShipThermalCalibration
{
public string GameSha256 { get; init; }="";
public string LoadoutFingerprint { get; init; }="";
public long StarBodyId { get; init; }
public bool Verified { get; init; }
public DateTimeOffset ExpiresAt { get; init; }
public double MinimumHeat { get; init; }
public double MaximumHeat { get; init; }=100;
public double ChargingGainUpperPerSecond { get; init; }
public double ChargedHoldGainUpperPerSecond { get; init; }
public double UncertaintyPercent { get; init; }
public double ChargeDurationUpperSeconds { get; init; }
public IReadOnlyList<ThermalDistanceBand> DistanceBands { get; init; }=[];
public ThermalCoolingCurve? CoolingCurve { get; init; }
public string Evidence { get; init; }="";
}
public sealed record HeatForecast(bool Known,double? PeakPercent,bool AllowsCharging,string Reason);
public static class ThermalPredictor
{
public static HeatForecast Predict(ShipThermalCalibration? model,string build,string loadout,long star,double initialHeat,double minimumDistance,double maximumDistance,double chargeSeconds,double holdSeconds,DateTimeOffset now,double hardLimit=100,double margin=3)
{
static HeatForecast Unknown(string reason)=>new(false,null,false,reason);
if(model is null||!model.Verified||model.Evidence.Length==0)return Unknown("尚无经过核对的飞船产热与散热模型。");
if(model.GameSha256!=build||model.LoadoutFingerprint!=loadout||model.StarBodyId!=star||model.ExpiresAt<now)return Unknown("热模型的构建、装配、恒星或有效期不匹配。");
var numbers=new[]{initialHeat,minimumDistance,maximumDistance,chargeSeconds,holdSeconds,hardLimit,margin,model.MinimumHeat,model.MaximumHeat,model.ChargingGainUpperPerSecond,model.ChargedHoldGainUpperPerSecond,model.UncertaintyPercent,model.ChargeDurationUpperSeconds};
if(numbers.Any(v=>!double.IsFinite(v))||initialHeat<0||minimumDistance<0||maximumDistance<minimumDistance||chargeSeconds<0||holdSeconds<0||hardLimit<=0||margin<0||margin>=hardLimit||model.ChargingGainUpperPerSecond<0||model.ChargedHoldGainUpperPerSecond<0||model.UncertaintyPercent<0||model.ChargeDurationUpperSeconds<=0)return Unknown("热模型或预测参数无效。");
if(initialHeat<model.MinimumHeat||initialHeat>model.MaximumHeat)return Unknown("当前热量超出热模型校验区间。");
if(model.MinimumHeat<0||model.MaximumHeat<=model.MinimumHeat)return Unknown("热模型的校验区间无效。");
if(chargeSeconds+holdSeconds>(model.ExpiresAt-now).TotalSeconds)return Unknown("热模型有效期不能覆盖完整充能和等待阶段。");
var covered=model.DistanceBands.Where(b=>b.InnerRadius<=maximumDistance&&b.OuterRadius>=minimumDistance).OrderBy(b=>b.InnerRadius).ToArray();
var edge=minimumDistance;
foreach(var band in covered)
{
if(!double.IsFinite(band.InnerRadius)||!double.IsFinite(band.OuterRadius)||band.InnerRadius<0||band.OuterRadius<=band.InnerRadius||!double.IsFinite(band.AmbientGainUpperPerSecond)||!double.IsFinite(band.CoolingLowerPerSecond)||band.AmbientGainUpperPerSecond<0||band.CoolingLowerPerSecond<0||band.InnerRadius>edge+1e-6)return Unknown("恒星距离区间尚无连续的产热上界与散热下界。");
edge=Math.Max(edge,band.OuterRadius);
}
if(covered.Length==0||edge<maximumDistance)return Unknown("预计飞行距离超出已校准的热模型范围。");
// Worst environmental input and least cooling across the entire planned distance interval.
// Do not assume an inverse-square game heating law or substitute real-world stellar physics.
var ambient=covered.Max(b=>b.AmbientGainUpperPerSecond);var cooling=covered.Min(b=>b.CoolingLowerPerSecond);
var curve=model.CoolingCurve;
if(curve is not null&&(new[]{curve.PassiveLowerPerSecond,curve.MaximumLowerPerSecond,curve.HeatLower,curve.HeatUpper,curve.Exponent}.Any(v=>!double.IsFinite(v))
||curve.PassiveLowerPerSecond<0||curve.MaximumLowerPerSecond<curve.PassiveLowerPerSecond||curve.HeatLower<0||curve.HeatUpper<=curve.HeatLower||curve.Exponent<1||curve.Exponent>4))return Unknown("原生散热曲线参数无效。");
if(curve is not null&&chargeSeconds+holdSeconds>600)return Unknown("散热曲线预测时长超过已支持的 600 秒范围。");
var heat=initialHeat;var peak=heat;
foreach(var (seconds,fsd) in new[]{(chargeSeconds,model.ChargingGainUpperPerSecond),(holdSeconds,model.ChargedHoldGainUpperPerSecond)})
{
if(curve is null)heat=Math.Max(0,heat+(ambient+fsd-cooling)*seconds);
else
{
// Native radiator cooling increases monotonically with heat. During heating the solution
// is concave, so Euler steps give an upper bound; the slope bound preserves ordering.
// If the upper trajectory would cool, retaining its current value is conservative.
var slope=curve.Exponent*(curve.MaximumLowerPerSecond-curve.PassiveLowerPerSecond)/(curve.HeatUpper-curve.HeatLower);
var step=slope>0?Math.Min(.05,.5/slope):.05;
if(step<.001)return Unknown("散热曲线过陡,无法在已验证步长内预测。");
for(var remaining=seconds;remaining>1e-9;)
{
var fraction=Math.Clamp((heat-curve.HeatLower)/(curve.HeatUpper-curve.HeatLower),0,1);
var curveCooling=curve.PassiveLowerPerSecond+(curve.MaximumLowerPerSecond-curve.PassiveLowerPerSecond)*Math.Pow(fraction,curve.Exponent);
var gain=ambient+fsd-Math.Max(cooling,curveCooling);
if(gain<=0)break;
var duration=Math.Min(step,remaining);heat+=gain*duration;remaining-=duration;
if(heat>model.MaximumHeat)return Unknown("预测轨迹超出已校准的热量区间,禁止外推。");
}
}
peak=Math.Max(peak,heat);
if(heat<model.MinimumHeat||heat>model.MaximumHeat)return Unknown("预测轨迹超出已校准的热量区间,禁止外推。");
}
peak+=model.UncertaintyPercent;
var allowed=peak<=hardLimit-margin;
return new(true,peak,allowed,allowed?$"计入充能、等待对齐与散热后,上界 {peak:F1}%。":$"充能及等待对齐预计可达 {peak:F1}%,先脱离恒星高温区域。");
}
}
namespace XFEEDToolBox.Core.Navigation;
// Rates are percent of this ship's calibrated heat capacity per second, never degrees Celsius.
// Bounds must come from native/observed calibration for this exact build, loadout and star environment.
public sealed record ThermalDistanceBand(double InnerRadius,double OuterRadius,double AmbientGainUpperPerSecond,double CoolingLowerPerSecond);
// Percent/second and percent thresholds, converted from the verified native capacity/configuration.
public sealed record ThermalCoolingCurve(double PassiveLowerPerSecond,double MaximumLowerPerSecond,double HeatLower,double HeatUpper,double Exponent);
public sealed record ShipThermalCalibration
{
public string GameSha256 { get; init; }="";
public string LoadoutFingerprint { get; init; }="";
public long StarBodyId { get; init; }
public bool Verified { get; init; }
public DateTimeOffset ExpiresAt { get; init; }
public double MinimumHeat { get; init; }
public double MaximumHeat { get; init; }=100;
public double ChargingGainUpperPerSecond { get; init; }
public double ChargedHoldGainUpperPerSecond { get; init; }
public double UncertaintyPercent { get; init; }
public double ChargeDurationUpperSeconds { get; init; }
public IReadOnlyList<ThermalDistanceBand> DistanceBands { get; init; }=[];
public ThermalCoolingCurve? CoolingCurve { get; init; }
public string Evidence { get; init; }="";
}
public sealed record HeatForecast(bool Known,double? PeakPercent,bool AllowsCharging,string Reason);
public static class ThermalPredictor
{
public static HeatForecast Predict(ShipThermalCalibration? model,string build,string loadout,long star,double initialHeat,double minimumDistance,double maximumDistance,double chargeSeconds,double holdSeconds,DateTimeOffset now,double hardLimit=100,double margin=3)
{
static HeatForecast Unknown(string reason)=>new(false,null,false,reason);
if(model is null||!model.Verified||model.Evidence.Length==0)return Unknown("尚无经过核对的飞船产热与散热模型。");
if(model.GameSha256!=build||model.LoadoutFingerprint!=loadout||model.StarBodyId!=star||model.ExpiresAt<now)return Unknown("热模型的构建、装配、恒星或有效期不匹配。");
var numbers=new[]{initialHeat,minimumDistance,maximumDistance,chargeSeconds,holdSeconds,hardLimit,margin,model.MinimumHeat,model.MaximumHeat,model.ChargingGainUpperPerSecond,model.ChargedHoldGainUpperPerSecond,model.UncertaintyPercent,model.ChargeDurationUpperSeconds};
if(numbers.Any(v=>!double.IsFinite(v))||initialHeat<0||minimumDistance<0||maximumDistance<minimumDistance||chargeSeconds<0||holdSeconds<0||hardLimit<=0||margin<0||margin>=hardLimit||model.ChargingGainUpperPerSecond<0||model.ChargedHoldGainUpperPerSecond<0||model.UncertaintyPercent<0||model.ChargeDurationUpperSeconds<=0)return Unknown("热模型或预测参数无效。");
if(initialHeat<model.MinimumHeat||initialHeat>model.MaximumHeat)return Unknown("当前热量超出热模型校验区间。");
if(model.MinimumHeat<0||model.MaximumHeat<=model.MinimumHeat)return Unknown("热模型的校验区间无效。");
if(chargeSeconds+holdSeconds>(model.ExpiresAt-now).TotalSeconds)return Unknown("热模型有效期不能覆盖完整充能和等待阶段。");
var covered=model.DistanceBands.Where(b=>b.InnerRadius<=maximumDistance&&b.OuterRadius>=minimumDistance).OrderBy(b=>b.InnerRadius).ToArray();
var edge=minimumDistance;
foreach(var band in covered)
{
if(!double.IsFinite(band.InnerRadius)||!double.IsFinite(band.OuterRadius)||band.InnerRadius<0||band.OuterRadius<=band.InnerRadius||!double.IsFinite(band.AmbientGainUpperPerSecond)||!double.IsFinite(band.CoolingLowerPerSecond)||band.AmbientGainUpperPerSecond<0||band.CoolingLowerPerSecond<0||band.InnerRadius>edge+1e-6)return Unknown("恒星距离区间尚无连续的产热上界与散热下界。");
edge=Math.Max(edge,band.OuterRadius);
}
if(covered.Length==0||edge<maximumDistance)return Unknown("预计飞行距离超出已校准的热模型范围。");
// Worst environmental input and least cooling across the entire planned distance interval.
// Do not assume an inverse-square game heating law or substitute real-world stellar physics.
var ambient=covered.Max(b=>b.AmbientGainUpperPerSecond);var cooling=covered.Min(b=>b.CoolingLowerPerSecond);
var curve=model.CoolingCurve;
if(curve is not null&&(new[]{curve.PassiveLowerPerSecond,curve.MaximumLowerPerSecond,curve.HeatLower,curve.HeatUpper,curve.Exponent}.Any(v=>!double.IsFinite(v))
||curve.PassiveLowerPerSecond<0||curve.MaximumLowerPerSecond<curve.PassiveLowerPerSecond||curve.HeatLower<0||curve.HeatUpper<=curve.HeatLower||curve.Exponent<1||curve.Exponent>4))return Unknown("原生散热曲线参数无效。");
if(curve is not null&&chargeSeconds+holdSeconds>600)return Unknown("散热曲线预测时长超过已支持的 600 秒范围。");
var heat=initialHeat;var peak=heat;
foreach(var (seconds,fsd) in new[]{(chargeSeconds,model.ChargingGainUpperPerSecond),(holdSeconds,model.ChargedHoldGainUpperPerSecond)})
{
if(curve is null)heat=Math.Max(0,heat+(ambient+fsd-cooling)*seconds);
else
{
// Native radiator cooling increases monotonically with heat. During heating the solution
// is concave, so Euler steps give an upper bound; the slope bound preserves ordering.
// If the upper trajectory would cool, retaining its current value is conservative.
var slope=curve.Exponent*(curve.MaximumLowerPerSecond-curve.PassiveLowerPerSecond)/(curve.HeatUpper-curve.HeatLower);
var step=slope>0?Math.Min(.05,.5/slope):.05;
if(step<.001)return Unknown("散热曲线过陡,无法在已验证步长内预测。");
for(var remaining=seconds;remaining>1e-9;)
{
var fraction=Math.Clamp((heat-curve.HeatLower)/(curve.HeatUpper-curve.HeatLower),0,1);
var curveCooling=curve.PassiveLowerPerSecond+(curve.MaximumLowerPerSecond-curve.PassiveLowerPerSecond)*Math.Pow(fraction,curve.Exponent);
var gain=ambient+fsd-Math.Max(cooling,curveCooling);
if(gain<=0)break;
var duration=Math.Min(step,remaining);heat+=gain*duration;remaining-=duration;
if(heat>model.MaximumHeat)return Unknown("预测轨迹超出已校准的热量区间,禁止外推。");
}
}
peak=Math.Max(peak,heat);
if(heat<model.MinimumHeat||heat>model.MaximumHeat)return Unknown("预测轨迹超出已校准的热量区间,禁止外推。");
}
peak+=model.UncertaintyPercent;
var allowed=peak<=hardLimit-margin;
return new(true,peak,allowed,allowed?$"计入充能、等待对齐与散热后,上界 {peak:F1}%。":$"充能及等待对齐预计可达 {peak:F1}%,先脱离恒星高温区域。");
}
}