using XFEEDToolBox.Core.Models;
namespace XFEEDToolBox.Core.Navigation;
/// A genuine threat pauses automation; ownership, identity and protocol failures are not retryable through this path.
public class FlightSafetyPauseException(string message):InvalidOperationException(message)
{
internal long? DepartureMarketId{get;set;}
public static bool Present(Exception error)=>error is FlightSafetyPauseException
||error is AggregateException aggregate&&aggregate.InnerExceptions.Any(Present)
||error.InnerException is{} inner&&Present(inner);
public static long? DepartureMarket(Exception error)
{
if(error is FlightSafetyPauseException {DepartureMarketId:>0} pause)return pause.DepartureMarketId;
if(error is AggregateException aggregate)
{
var markets=aggregate.InnerExceptions.Select(DepartureMarket).Where(market=>market is >0).Distinct().ToArray();
return markets.Length==1?markets[0]:null;
}
return error.InnerException is {} inner?DepartureMarket(inner):null;
}
public static bool RequiresDepartureSessionRecovery(Exception error,bool docked,bool alreadyRecovered)
=>!docked&&!alreadyRecovered&&DepartureMarket(error) is >0;
}
public sealed class FlightThreatPauseException():FlightSafetyPauseException("检测到实际攻击或拦截威胁,已释放自动驾驶,等待安全确认。");
public sealed class FlightHealthPauseException():FlightSafetyPauseException("连续监测检测到近期受损,已暂停驾驶输入,等待稳定健康授权。");
/// Separates combat/interdiction from the game's earlier internal-heat warning.
public static class NavigationSafety
{
private const long Danger=1L<<22,Interdiction=1L<<23,HeatWarning=1L<<20;
// A historical low percentage is not evidence of continuing damage.
// Fresh damage is handled by the continuous recovery guard; zero hull
// remains terminal independently of that short-lived damage episode.
public static bool UnhealthyHull(PlayerState player)=>player.Ships.FirstOrDefault(s=>s.ShipId==player.CurrentShipId)?.HullHealth is <=0;
public static bool HasExternalThreat(PlayerState player)=>player.Navigation.ExternalThreat
||player.Status is {} status&&(status.Flags&Interdiction)!=0;
public static void RequireLocalSelection(PlayerState player,long systemAddress)
{
var hull=UnhealthyHull(player);var threat=HasExternalThreat(player);
if(!player.InGame||hull)
throw new InvalidOperationException($"自动驾驶目标选择被安全状态拒绝:inGame={player.InGame}, unhealthyHull={hull}, externalThreat={threat}。");
if(threat)throw new FlightThreatPauseException();
if(player.Navigation.CurrentSystemAddress!=systemAddress)
throw new FlightTelemetryUnavailableException("正在等待跃迁后的原始目录与当前星系一致。",recovery:FlightRecovery.ReselectTarget);
}
public static bool MustStop(PlayerState player,FlightFrame? frame,DateTimeOffset now)
{
// The generic HUD danger marker is not an attack witness. Geometry,
// freshness, thermal prediction, fuel and native charging interlocks
// remain enforced by the flight policy and driver for every input.
return HasExternalThreat(player)||UnhealthyHull(player);
}
}
/// Retains the confirmed thermal cause until the same Status danger episode ends.
public sealed class ThermalDangerTracker
{
private bool explained;
public bool Observe(long? flags,bool externalThreat,bool nativeHeatWarning,bool nearStar)
{
if(externalThreat||flags is not {} status||(status&((1L<<22)|(1L<<20)))==0||(status&(1L<<23))!=0)return explained=false;
if(nativeHeatWarning&&nearStar)explained=true;
// The game's generic danger marker outlives the actual hot reading.
// Cooling below the warning threshold must not stop outward escape.
return explained;
}
}