namespace XFEEDToolBox.Core.Navigation;
public sealed record ScoopThermalAdjustment(double Radius,bool MovingOutward,bool Interrupted,bool CanSkipFullTank,double? HeatRate);
/// Tracks the nearby thermal boundary instead of commanding a radial escape.
public sealed class ScoopThermalController
{
private long system,body;
private DateTimeOffset? previousAt,lastStep;
private double previousHeat,radiusFloor,targetRadius;
private double? rate;
private bool interrupted,moving;
public ScoopThermalAdjustment Observe(FlightFrame frame,double? predictedCoastPeak=null)
{
var star=frame.CurrentStar??throw new ArgumentException("Missing current star.");
var heat=frame.HeatPercent??throw new ArgumentException("Missing heat observation.");
var radius=(frame.Position-star.Centre).Length;
if(!double.IsFinite(heat)||!double.IsFinite(radius)||radius<=star.SafeRadius)throw new ArgumentException("Invalid scoop thermal geometry.");
if(system!=frame.SystemAddress||body!=star.BodyId)
{system=frame.SystemAddress;body=star.BodyId;previousAt=lastStep=null;rate=null;radiusFloor=targetRadius=0;interrupted=moving=false;}
var elapsed=previousAt is {} at?(frame.ObservedAt-at).TotalSeconds:0;
var nonRising=false;
if(elapsed is >0 and <=1)
{
var measured=(heat-previousHeat)/elapsed;nonRising=measured<=0;
rate=rate is {} old?old+(measured-old)*(1-Math.Exp(-elapsed/.3)):measured;
// Do not continue moving outward while a smoothed rate catches up
// with an actual non-rising temperature sample.
if(nonRising&&moving){moving=false;targetRadius=radius;radiusFloor=Math.Max(radiusFloor,radius);}
}
else if(elapsed>1){rate=null;moving=false;}
previousAt=frame.ObservedAt;previousHeat=heat;
var refilling=frame.ScoopActive||frame.RefuellingToFull||frame.FuelMain.08);
// Reserve a turning margin: waiting until 94-97% while the nose is
// still inward keeps adding stellar heat throughout the turn. This
// applies to departure as well as an actively running fuel scoop.
var turnAllowance=4d;
if(frame.Forward is {Finite:true,Length:>.9})
{
var turningDegrees=Math.Max(0,Vector3d.AngleDegrees(frame.Forward,frame.Position-star.Centre)-75);
var turnRate=frame.AlignmentRateLowerDegreesPerSecond is >0 and var lower?lower:4;
turnAllowance=Math.Clamp(turningDegrees/turnRate+2,4,30);
}
var nearLimit=heat>=90||heat>=80&&rate is {} trend&&heat+Math.Max(0,trend)*turnAllowance>=92||predictedCoastPeak is >=96;
if((refilling||heat>=80||frame.NativeHeatWarning)&&rising&&nearLimit)
{
interrupted=true;
radiusFloor=Math.Max(radiusFloor,radius);
var step=Math.Clamp(frame.SpeedMetresPerSecond*.35,star.SafeRadius*.002,star.SafeRadius*.01);
if(!moving){moving=true;targetRadius=Math.Max(radius,radiusFloor)+step;lastStep=frame.ObservedAt;}
else if(frame.ObservedAt-lastStep>=TimeSpan.FromMilliseconds(500)&&radius>=targetRadius-step*.4)
{targetRadius=Math.Max(targetRadius,radius)+step;lastStep=frame.ObservedAt;}
}
var required=frame.MinimumFuelToNextRefuel??frame.FuelNeededToNextRefuel;
var canSkip=interrupted&&required is >0&&frame.FuelMain>=required&&frame.FuelMain>=frame.FuelRequiredForJump;
var baseline=frame.PreferredOrbitRadius??radius;
// A separate scoop-band estimate may retain a much larger radius from
// an earlier heat spike. Once temperature has stopped rising, hold the
// observed boundary instead of continuing that stale outward demand.
if(nonRising&&heat<85){radiusFloor=0;moving=false;}
var held=interrupted&&heat>=85?Math.Max(radiusFloor,radius):Math.Max(baseline,radiusFloor);
return new(moving?targetRadius:held,moving,interrupted,canSkip,rate);
}
}