namespace XFEEDToolBox.Core.Navigation;
public enum CruiseOverchargeState { Unverified,Idle,Starting,Active,Cooling }
public enum CruiseOverchargeReason { Ready,Unavailable,OutsideSupercruise,Telemetry,Ownership,AssistConflict,Threat,Geometry,TargetClose,Alignment,Heat,Fuel,Cooldown,BurstLimit,AwaitingNative }
public sealed record CruiseOverchargeObservation
{
public DateTimeOffset ObservedAt{get;init;}
public ulong ShipEntity{get;init;}
public ulong ReferenceWorld{get;init;}
public string GameSha256{get;init;}="";
public bool OriginalInterfacesVerified{get;init;}
public bool OriginalCanEngage{get;init;}
public bool NativeFlightConstraintsVerified{get;init;}
public bool NativeGeometryVerified{get;init;}
public bool NativeCollisionCourse{get;init;}
public bool NativeAssistEnabled{get;init;}
public CruiseOverchargeState State{get;init;}
public double HeatPercent{get;init;}=double.NaN;
public double HeatRisePercentPerSecond{get;init;}=double.NaN;
public double FuelMainTonnes{get;init;}=double.NaN;
public double FuelCapacityTonnes{get;init;}=double.NaN;
public double FuelBurnTonnesPerSecond{get;init;}=double.NaN;
public double FuelRateUpperTonnesPerSecond{get;init;}=double.NaN;
public ScoThermalInput? ThermalInput{get;init;}
public ScoExitApproachDecision? ExitApproach{get;init;}
}
public sealed record CruiseOverchargeDecision(bool UseOvercharge,CruiseOverchargeReason Reason,double PredictedHeatPercent=double.NaN,double FuelHeadroomTonnes=double.NaN)
{
public ScoThermalForecast? ThermalForecast{get;init;}
}
/// <summary>Renews SCO while verified approach, thermal and fuel conditions permit; an acknowledgement never replaces an active engine observation.</summary>
public sealed class CruiseOverchargePolicy
{
public const double MaximumHeatPercent=100;
private const double FuelForecastSeconds=2;
private ulong ship,world;
private bool requesting;
private DateTimeOffset? engagementRequestedAt;
private DateTimeOffset blockedUntil;
private SystemNavigationTarget? approachTarget;
private bool finalApproach;
public void ObserveNativeStartRefusal(DateTimeOffset now)
{requesting=false;engagementRequestedAt=null;blockedUntil=now.AddSeconds(1);}
public void ObserveNativeApproachStop(SystemFlightFrame flight)
{
approachTarget=flight.SelectedTarget;ship=flight.ShipEntity;world=flight.ReferenceWorld;finalApproach=true;
}
public CruiseOverchargeDecision Decide(SystemFlightFrame flight,CruiseOverchargeObservation observed,double requiredFuelReserveTonnes,DateTimeOffset now,DateTimeOffset? controlDeadline=null)
{
if(ship!=flight.ShipEntity||world!=flight.ReferenceWorld||approachTarget is {} priorTarget&&flight.SelectedTarget is {} nextTarget&&!priorTarget.SameIdentity(nextTarget))
{
ship=flight.ShipEntity;world=flight.ReferenceWorld;requesting=false;engagementRequestedAt=null;blockedUntil=default;finalApproach=false;
}
approachTarget=flight.SelectedTarget;
ScoThermalForecast? thermalForecast=null;
CruiseOverchargeDecision Off(CruiseOverchargeReason why,double heat=double.NaN,double fuel=double.NaN)
{
if(requesting)blockedUntil=now.AddSeconds(1);
requesting=false;engagementRequestedAt=null;return new(false,why,heat,fuel){ThermalForecast=thermalForecast};
}
if(!flight.InGame||flight.PlayerOverride||flight.ExternalThreat)return Off(CruiseOverchargeReason.Threat);
if(flight.Mode!=LocalFlightMode.Supercruise)return Off(CruiseOverchargeReason.OutsideSupercruise);
if(!observed.OriginalInterfacesVerified||!Enum.IsDefined(observed.State)||observed.State==CruiseOverchargeState.Unverified)return Off(CruiseOverchargeReason.Unavailable);
if(flight.ShipEntity==0||flight.ReferenceWorld==0||flight.ShipEntity!=observed.ShipEntity||flight.ReferenceWorld!=observed.ReferenceWorld
||flight.GameSha256.Length!=64||flight.GameSha256!=observed.GameSha256)return Off(CruiseOverchargeReason.Ownership);
static bool Fresh(DateTimeOffset at,DateTimeOffset current)=>at<=current&¤t-at<=TimeSpan.FromMilliseconds(250);
if(!Fresh(flight.ObservedAt,now)||!Fresh(observed.ObservedAt,now))return Off(CruiseOverchargeReason.Telemetry);
if(!observed.NativeFlightConstraintsVerified)return Off(CruiseOverchargeReason.Telemetry);
if(observed.NativeAssistEnabled)return Off(CruiseOverchargeReason.AssistConflict);
if(!observed.NativeGeometryVerified||observed.NativeCollisionCourse)return Off(CruiseOverchargeReason.Geometry);
if(flight.SupercruiseAssist is NativeAssistState.Requested or NativeAssistState.Aligning or NativeAssistState.Engaged)
return Off(CruiseOverchargeReason.AssistConflict);
if(!flight.GeometryVerified||flight.SelectedTarget is null||flight.SystemAddress<=0||flight.SelectedTarget.SystemAddress!=flight.SystemAddress
||flight.TargetObscured||flight.CollisionCourse||!flight.Position.Finite||!flight.TargetPosition.Finite
||!flight.Forward.Finite||flight.Forward.Length<.9||!double.IsFinite(flight.TargetDistance)||flight.TargetDistance<=0||!double.IsFinite(flight.Speed)||flight.Speed<0)
return Off(CruiseOverchargeReason.Geometry);
var active=observed.State is CruiseOverchargeState.Starting or CruiseOverchargeState.Active;
bool ApproachStop(bool currentlyActive)=>observed.ExitApproach?.RequiresStop(flight.TargetDistance,flight.Speed,currentlyActive,
flight.CruiseParametersVerified,flight.CruiseApproachSeconds,flight.ShipEntity,flight.ReferenceWorld,flight.SelectedTarget.NativeKey,(ulong)now.ToUnixTimeMilliseconds())
??ScoApproachEnvelope.RequiresStop(flight.TargetDistance,flight.Speed,currentlyActive,flight.CruiseParametersVerified,flight.CruiseApproachSeconds);
if(finalApproach||ApproachStop(active))
{
// Failed starts outside the wider engagement buffer must not latch.
// Only reaching the stopping buffer ends boost for this destination.
if(active||ApproachStop(true))finalApproach=true;
return Off(CruiseOverchargeReason.TargetClose);
}
var aim=flight.TargetPosition-flight.Position;
if(!aim.Finite||Math.Abs(aim.Length-flight.TargetDistance)>Math.Max(10,flight.TargetDistance*.01))return Off(CruiseOverchargeReason.Geometry);
if(!aim.Finite||aim.Length<1||Vector3d.AngleDegrees(flight.Forward,aim)>(active?6:3))return Off(CruiseOverchargeReason.Alignment);
var heat=observed.HeatPercent;
if(!double.IsFinite(heat)||heat<0||heat>1000||observed.ThermalInput is not {} thermal)
return Off(CruiseOverchargeReason.Telemetry);
var sampleAge=(now-observed.ObservedAt).TotalSeconds;
thermalForecast=ScoThermalPredictor.Predict(thermal with{HeatPercent=heat,
SourceAgeSeconds=thermal.SourceAgeSeconds+sampleAge,HeatAgeSeconds=thermal.HeatAgeSeconds+sampleAge,
LeaseRemainingSeconds=((controlDeadline??now.AddMilliseconds(250))-now).TotalSeconds});
if(!thermalForecast.Known)return Off(CruiseOverchargeReason.Telemetry);
var predicted=thermalForecast.DeadlineStopPeakPercent;
if(heat>=MaximumHeatPercent||thermalForecast.ShouldStop)return Off(CruiseOverchargeReason.Heat,predicted);
var fuel=observed.FuelMainTonnes;var capacity=observed.FuelCapacityTonnes;var burn=observed.FuelBurnTonnesPerSecond;var upper=observed.FuelRateUpperTonnesPerSecond;
// The first real sample has no measured consumption interval yet.
// Wait for another observation instead of reporting a fuel-shortage boundary.
if(!double.IsFinite(burn))return Off(CruiseOverchargeReason.Telemetry,predicted);
if(!double.IsFinite(fuel)||!double.IsFinite(capacity)||capacity<=0||fuel<0||fuel>capacity+.001||!double.IsFinite(requiredFuelReserveTonnes)
||requiredFuelReserveTonnes<=0||requiredFuelReserveTonnes>=capacity||!double.IsFinite(burn)||burn<0||!double.IsFinite(upper)||upper<=0)
return Off(CruiseOverchargeReason.Fuel,predicted);
// Use the higher of the native bound and measured burn. Keep enough
// for the route, arrival and two seconds of unexpectedly heavy use.
var headroom=fuel-requiredFuelReserveTonnes-Math.Max(burn,upper)*FuelForecastSeconds-Math.Max(.25,capacity*.01);
if(headroom<=0)return Off(CruiseOverchargeReason.Fuel,predicted,headroom);
if(observed.State==CruiseOverchargeState.Cooling||now<blockedUntil)return Off(CruiseOverchargeReason.Cooldown,predicted,headroom);
if(!observed.OriginalCanEngage)return Off(CruiseOverchargeReason.Unavailable,predicted,headroom);
if(active)engagementRequestedAt=null;else engagementRequestedAt??=now;
if(!active&&requesting&&now-engagementRequestedAt>TimeSpan.FromSeconds(1))return Off(CruiseOverchargeReason.AwaitingNative,predicted,headroom);
requesting=true;return new(true,CruiseOverchargeReason.Ready,predicted,headroom){ThermalForecast=thermalForecast};
}
}
namespace XFEEDToolBox.Core.Navigation;
public enum CruiseOverchargeState { Unverified,Idle,Starting,Active,Cooling }
public enum CruiseOverchargeReason { Ready,Unavailable,OutsideSupercruise,Telemetry,Ownership,AssistConflict,Threat,Geometry,TargetClose,Alignment,Heat,Fuel,Cooldown,BurstLimit,AwaitingNative }
public sealed record CruiseOverchargeObservation
{
public DateTimeOffset ObservedAt{get;init;}
public ulong ShipEntity{get;init;}
public ulong ReferenceWorld{get;init;}
public string GameSha256{get;init;}="";
public bool OriginalInterfacesVerified{get;init;}
public bool OriginalCanEngage{get;init;}
public bool NativeFlightConstraintsVerified{get;init;}
public bool NativeGeometryVerified{get;init;}
public bool NativeCollisionCourse{get;init;}
public bool NativeAssistEnabled{get;init;}
public CruiseOverchargeState State{get;init;}
public double HeatPercent{get;init;}=double.NaN;
public double HeatRisePercentPerSecond{get;init;}=double.NaN;
public double FuelMainTonnes{get;init;}=double.NaN;
public double FuelCapacityTonnes{get;init;}=double.NaN;
public double FuelBurnTonnesPerSecond{get;init;}=double.NaN;
public double FuelRateUpperTonnesPerSecond{get;init;}=double.NaN;
public ScoThermalInput? ThermalInput{get;init;}
public ScoExitApproachDecision? ExitApproach{get;init;}
}
public sealed record CruiseOverchargeDecision(bool UseOvercharge,CruiseOverchargeReason Reason,double PredictedHeatPercent=double.NaN,double FuelHeadroomTonnes=double.NaN)
{
public ScoThermalForecast? ThermalForecast{get;init;}
}
/// <summary>Renews SCO while verified approach, thermal and fuel conditions permit; an acknowledgement never replaces an active engine observation.</summary>
public sealed class CruiseOverchargePolicy
{
public const double MaximumHeatPercent=100;
private const double FuelForecastSeconds=2;
private ulong ship,world;
private bool requesting;
private DateTimeOffset? engagementRequestedAt;
private DateTimeOffset blockedUntil;
private SystemNavigationTarget? approachTarget;
private bool finalApproach;
public void ObserveNativeStartRefusal(DateTimeOffset now)
{requesting=false;engagementRequestedAt=null;blockedUntil=now.AddSeconds(1);}
public void ObserveNativeApproachStop(SystemFlightFrame flight)
{
approachTarget=flight.SelectedTarget;ship=flight.ShipEntity;world=flight.ReferenceWorld;finalApproach=true;
}
public CruiseOverchargeDecision Decide(SystemFlightFrame flight,CruiseOverchargeObservation observed,double requiredFuelReserveTonnes,DateTimeOffset now,DateTimeOffset? controlDeadline=null)
{
if(ship!=flight.ShipEntity||world!=flight.ReferenceWorld||approachTarget is {} priorTarget&&flight.SelectedTarget is {} nextTarget&&!priorTarget.SameIdentity(nextTarget))
{
ship=flight.ShipEntity;world=flight.ReferenceWorld;requesting=false;engagementRequestedAt=null;blockedUntil=default;finalApproach=false;
}
approachTarget=flight.SelectedTarget;
ScoThermalForecast? thermalForecast=null;
CruiseOverchargeDecision Off(CruiseOverchargeReason why,double heat=double.NaN,double fuel=double.NaN)
{
if(requesting)blockedUntil=now.AddSeconds(1);
requesting=false;engagementRequestedAt=null;return new(false,why,heat,fuel){ThermalForecast=thermalForecast};
}
if(!flight.InGame||flight.PlayerOverride||flight.ExternalThreat)return Off(CruiseOverchargeReason.Threat);
if(flight.Mode!=LocalFlightMode.Supercruise)return Off(CruiseOverchargeReason.OutsideSupercruise);
if(!observed.OriginalInterfacesVerified||!Enum.IsDefined(observed.State)||observed.State==CruiseOverchargeState.Unverified)return Off(CruiseOverchargeReason.Unavailable);
if(flight.ShipEntity==0||flight.ReferenceWorld==0||flight.ShipEntity!=observed.ShipEntity||flight.ReferenceWorld!=observed.ReferenceWorld
||flight.GameSha256.Length!=64||flight.GameSha256!=observed.GameSha256)return Off(CruiseOverchargeReason.Ownership);
static bool Fresh(DateTimeOffset at,DateTimeOffset current)=>at<=current&¤t-at<=TimeSpan.FromMilliseconds(250);
if(!Fresh(flight.ObservedAt,now)||!Fresh(observed.ObservedAt,now))return Off(CruiseOverchargeReason.Telemetry);
if(!observed.NativeFlightConstraintsVerified)return Off(CruiseOverchargeReason.Telemetry);
if(observed.NativeAssistEnabled)return Off(CruiseOverchargeReason.AssistConflict);
if(!observed.NativeGeometryVerified||observed.NativeCollisionCourse)return Off(CruiseOverchargeReason.Geometry);
if(flight.SupercruiseAssist is NativeAssistState.Requested or NativeAssistState.Aligning or NativeAssistState.Engaged)
return Off(CruiseOverchargeReason.AssistConflict);
if(!flight.GeometryVerified||flight.SelectedTarget is null||flight.SystemAddress<=0||flight.SelectedTarget.SystemAddress!=flight.SystemAddress
||flight.TargetObscured||flight.CollisionCourse||!flight.Position.Finite||!flight.TargetPosition.Finite
||!flight.Forward.Finite||flight.Forward.Length<.9||!double.IsFinite(flight.TargetDistance)||flight.TargetDistance<=0||!double.IsFinite(flight.Speed)||flight.Speed<0)
return Off(CruiseOverchargeReason.Geometry);
var active=observed.State is CruiseOverchargeState.Starting or CruiseOverchargeState.Active;
bool ApproachStop(bool currentlyActive)=>observed.ExitApproach?.RequiresStop(flight.TargetDistance,flight.Speed,currentlyActive,
flight.CruiseParametersVerified,flight.CruiseApproachSeconds,flight.ShipEntity,flight.ReferenceWorld,flight.SelectedTarget.NativeKey,(ulong)now.ToUnixTimeMilliseconds())
??ScoApproachEnvelope.RequiresStop(flight.TargetDistance,flight.Speed,currentlyActive,flight.CruiseParametersVerified,flight.CruiseApproachSeconds);
if(finalApproach||ApproachStop(active))
{
// Failed starts outside the wider engagement buffer must not latch.
// Only reaching the stopping buffer ends boost for this destination.
if(active||ApproachStop(true))finalApproach=true;
return Off(CruiseOverchargeReason.TargetClose);
}
var aim=flight.TargetPosition-flight.Position;
if(!aim.Finite||Math.Abs(aim.Length-flight.TargetDistance)>Math.Max(10,flight.TargetDistance*.01))return Off(CruiseOverchargeReason.Geometry);
if(!aim.Finite||aim.Length<1||Vector3d.AngleDegrees(flight.Forward,aim)>(active?6:3))return Off(CruiseOverchargeReason.Alignment);
var heat=observed.HeatPercent;
if(!double.IsFinite(heat)||heat<0||heat>1000||observed.ThermalInput is not {} thermal)
return Off(CruiseOverchargeReason.Telemetry);
var sampleAge=(now-observed.ObservedAt).TotalSeconds;
thermalForecast=ScoThermalPredictor.Predict(thermal with{HeatPercent=heat,
SourceAgeSeconds=thermal.SourceAgeSeconds+sampleAge,HeatAgeSeconds=thermal.HeatAgeSeconds+sampleAge,
LeaseRemainingSeconds=((controlDeadline??now.AddMilliseconds(250))-now).TotalSeconds});
if(!thermalForecast.Known)return Off(CruiseOverchargeReason.Telemetry);
var predicted=thermalForecast.DeadlineStopPeakPercent;
if(heat>=MaximumHeatPercent||thermalForecast.ShouldStop)return Off(CruiseOverchargeReason.Heat,predicted);
var fuel=observed.FuelMainTonnes;var capacity=observed.FuelCapacityTonnes;var burn=observed.FuelBurnTonnesPerSecond;var upper=observed.FuelRateUpperTonnesPerSecond;
// The first real sample has no measured consumption interval yet.
// Wait for another observation instead of reporting a fuel-shortage boundary.
if(!double.IsFinite(burn))return Off(CruiseOverchargeReason.Telemetry,predicted);
if(!double.IsFinite(fuel)||!double.IsFinite(capacity)||capacity<=0||fuel<0||fuel>capacity+.001||!double.IsFinite(requiredFuelReserveTonnes)
||requiredFuelReserveTonnes<=0||requiredFuelReserveTonnes>=capacity||!double.IsFinite(burn)||burn<0||!double.IsFinite(upper)||upper<=0)
return Off(CruiseOverchargeReason.Fuel,predicted);
// Use the higher of the native bound and measured burn. Keep enough
// for the route, arrival and two seconds of unexpectedly heavy use.
var headroom=fuel-requiredFuelReserveTonnes-Math.Max(burn,upper)*FuelForecastSeconds-Math.Max(.25,capacity*.01);
if(headroom<=0)return Off(CruiseOverchargeReason.Fuel,predicted,headroom);
if(observed.State==CruiseOverchargeState.Cooling||now<blockedUntil)return Off(CruiseOverchargeReason.Cooldown,predicted,headroom);
if(!observed.OriginalCanEngage)return Off(CruiseOverchargeReason.Unavailable,predicted,headroom);
if(active)engagementRequestedAt=null;else engagementRequestedAt??=now;
if(!active&&requesting&&now-engagementRequestedAt>TimeSpan.FromSeconds(1))return Off(CruiseOverchargeReason.AwaitingNative,predicted,headroom);
requesting=true;return new(true,CruiseOverchargeReason.Ready,predicted,headroom){ThermalForecast=thermalForecast};
}
}