using XFEEDToolBox.Core.Models;
namespace XFEEDToolBox.Core.Navigation;
/// <summary>
/// A short straight segment actually flown by this original launch duty. It is
/// continuation authority only outside the original station body envelope;
/// neither an invented exit normal nor proof that mass lock has cleared.
/// </summary>
public sealed class StationDeparturePathEvidence
{
public const double ContinuationThrottle=.5;
private static readonly double Alignment=Math.Cos(5*Math.PI/180);
private static readonly TimeSpan SegmentSampleGap=TimeSpan.FromSeconds(1),ReacquisitionGap=TimeSpan.FromSeconds(5);
private SystemFlightFrame? previous;
private Vector3d? direction,segmentDirection;
private DateTimeOffset? segmentStarted,lastMotion;
private Vector3d lastRelative;
private int segments;
private bool activeSeen,naturallyCompleted,revoked;
private string commander="",fingerprint="";
private long? ship;
private DateTimeOffset? session;
public bool OriginalDutyObserved=>activeSeen;
public bool OriginalDutyNaturallyEnded=>naturallyCompleted;
public bool PathVerified=>direction is not null&&!revoked;
public string? WithdrawalReason{get;private set;}
public void Observe(SystemFlightFrame frame,SystemNavigationTarget origin,PlayerState player,DateTimeOffset requestedJournalAt,DateTimeOffset now,
bool nativeStateVerified,bool launchActive,bool launchRequested,bool cancellationIssued)
{
if(revoked)return;
if(cancellationIssued||frame.PlayerOverride||frame.ExternalThreat){Revoke("cancelled-manual-or-threat");return;}
if(NavigationSafety.UnhealthyHull(player)){Revoke("ship-damaged");return;}
if(!nativeStateVerified||!StationDepartureTakeoverPolicy.CurrentDepartureFrame(frame,origin,player,requestedJournalAt,now))
{if(activeSeen)Revoke("departure-frame-unverified");return;}
if(!activeSeen&&!launchActive)return;
if(previous is {} prior&&(prior.ShipEntity!=frame.ShipEntity||prior.ReferenceWorld!=frame.ReferenceWorld||fingerprint!=frame.LoadoutFingerprint
||commander!=player.CommanderId||ship!=player.CurrentShipId||session!=player.LoadGameAt))
{Revoke("ship-session-or-reference-changed");return;}
var relative=frame.Position-frame.TargetPosition;
if(previous is {} old)
{
// The reader can return the same still-fresh physics publication.
// It supplies no new motion or active -> inactive evidence. A
// conflicting pose under that identity is not a harmless repeat.
if(frame.Sequence==old.Sequence&&frame.ObservedAt==old.ObservedAt)
{
if(frame.Position!=old.Position||frame.TargetPosition!=old.TargetPosition||frame.Forward!=old.Forward
||frame.Velocity!=old.Velocity||frame.TargetVelocity!=old.TargetVelocity||frame.Mode!=old.Mode)
Revoke("conflicting-duplicate-frame");
return;
}
var gap=frame.ObservedAt-old.ObservedAt;
if(frame.Sequence<=old.Sequence||gap<=TimeSpan.Zero){Revoke("observation-gap-or-reordered-frame");return;}
if(gap>SegmentSampleGap)
{
// Never bridge a missing interval into the exit proof. Only
// the still-active original duty can provide a new continuous
// segment afterwards; a cold/stationary endpoint cannot.
if(gap>ReacquisitionGap||!launchActive||naturallyCompleted)
{Revoke("observation-gap-or-reordered-frame");return;}
var oldRelative=old.Position-old.TargetPosition;
if(direction is not null&&old.TargetExclusionRadius is {} oldRadius&&oldRadius>0&&oldRelative.Length>oldRadius
&&frame.TargetExclusionRadius is {} radius&&radius>0&&relative.Length>radius&&!ContinuationGeometry(frame,relative))
{Revoke("unsafe-outside-path-across-observation-gap");return;}
ResetSegment();previous=frame;lastRelative=relative;return;
}
}
if(launchActive)
{
if(naturallyCompleted){Revoke("native-duty-resumed");return;}
activeSeen=true;
if(previous is {} oldFrame)
{
var step=relative-(oldFrame.Position-oldFrame.TargetPosition);
if(step.Length>.05)
{
var motion=step.Unit();var outward=Vector3d.Dot(relative,motion)>0&&relative.Length>(oldFrame.Position-oldFrame.TargetPosition).Length;
var straight=outward&&Vector3d.Dot(motion,frame.Forward.Unit())>=Alignment
&&(segmentDirection is null||Vector3d.Dot(motion,segmentDirection.Value)>=Alignment);
if(straight)
{
segmentStarted??=oldFrame.ObservedAt;segmentDirection??=motion;lastMotion=frame.ObservedAt;segments++;
if(segments>=3&&frame.ObservedAt-segmentStarted>=TimeSpan.FromMilliseconds(500))direction=motion;
}
else ResetSegment();
}
if(direction is {} heading&&Vector3d.Dot(frame.Forward.Unit(),heading)<Alignment)
ResetSegment();
}
}
else if(!naturallyCompleted)
{
// Only the observed active -> inactive transition can end our duty.
// A cold inactive snapshot or a cancellation is never enough.
naturallyCompleted=activeSeen&&!launchRequested&&direction is not null&&previous is not null
&&lastMotion is {} moved&&frame.ObservedAt-moved<=TimeSpan.FromSeconds(5);
if(!naturallyCompleted){Revoke("native-exit-segment-not-confirmed");return;}
}
if(naturallyCompleted&&(launchRequested||!ContinuationGeometry(frame,relative))){Revoke("outside-envelope-or-outward-continuation-not-confirmed");return;}
if(previous is null){commander=player.CommanderId;ship=player.CurrentShipId;session=player.LoadGameAt;fingerprint=frame.LoadoutFingerprint;}
previous=frame;lastRelative=relative;
}
public bool CanContinue(SystemFlightFrame frame,DateTimeOffset now)=>!revoked&&naturallyCompleted&&previous is {} sample&&sample.Sequence==frame.Sequence
&&sample.ObservedAt==frame.ObservedAt&&frame.ObservedAt<=now&&now-frame.ObservedAt<=TimeSpan.FromMilliseconds(300)
&&ContinuationGeometry(frame,frame.Position-frame.TargetPosition);
private bool ContinuationGeometry(SystemFlightFrame frame,Vector3d relative)
{
if(direction is not {} heading||frame.TargetExclusionRadius is not {} radius||radius<=0||!double.IsFinite(radius)
||relative.Length<=radius||Vector3d.Dot(relative,heading)<=0||Vector3d.Dot(frame.Forward.Unit(),heading)<Alignment)return false;
var step=relative-lastRelative;var forward=Vector3d.Dot(step,heading);
return forward>=-.5&&(step-heading*forward).Length<=2
&&Vector3d.Dot(frame.Velocity-frame.TargetVelocity,heading)>=-1;
}
private void Revoke(string reason){revoked=true;direction=null;WithdrawalReason=reason;}
private void ResetSegment(){direction=segmentDirection=null;segmentStarted=null;lastMotion=null;segments=0;}
}
using XFEEDToolBox.Core.Models;
namespace XFEEDToolBox.Core.Navigation;
/// <summary>
/// A short straight segment actually flown by this original launch duty. It is
/// continuation authority only outside the original station body envelope;
/// neither an invented exit normal nor proof that mass lock has cleared.
/// </summary>
public sealed class StationDeparturePathEvidence
{
public const double ContinuationThrottle=.5;
private static readonly double Alignment=Math.Cos(5*Math.PI/180);
private static readonly TimeSpan SegmentSampleGap=TimeSpan.FromSeconds(1),ReacquisitionGap=TimeSpan.FromSeconds(5);
private SystemFlightFrame? previous;
private Vector3d? direction,segmentDirection;
private DateTimeOffset? segmentStarted,lastMotion;
private Vector3d lastRelative;
private int segments;
private bool activeSeen,naturallyCompleted,revoked;
private string commander="",fingerprint="";
private long? ship;
private DateTimeOffset? session;
public bool OriginalDutyObserved=>activeSeen;
public bool OriginalDutyNaturallyEnded=>naturallyCompleted;
public bool PathVerified=>direction is not null&&!revoked;
public string? WithdrawalReason{get;private set;}
public void Observe(SystemFlightFrame frame,SystemNavigationTarget origin,PlayerState player,DateTimeOffset requestedJournalAt,DateTimeOffset now,
bool nativeStateVerified,bool launchActive,bool launchRequested,bool cancellationIssued)
{
if(revoked)return;
if(cancellationIssued||frame.PlayerOverride||frame.ExternalThreat){Revoke("cancelled-manual-or-threat");return;}
if(NavigationSafety.UnhealthyHull(player)){Revoke("ship-damaged");return;}
if(!nativeStateVerified||!StationDepartureTakeoverPolicy.CurrentDepartureFrame(frame,origin,player,requestedJournalAt,now))
{if(activeSeen)Revoke("departure-frame-unverified");return;}
if(!activeSeen&&!launchActive)return;
if(previous is {} prior&&(prior.ShipEntity!=frame.ShipEntity||prior.ReferenceWorld!=frame.ReferenceWorld||fingerprint!=frame.LoadoutFingerprint
||commander!=player.CommanderId||ship!=player.CurrentShipId||session!=player.LoadGameAt))
{Revoke("ship-session-or-reference-changed");return;}
var relative=frame.Position-frame.TargetPosition;
if(previous is {} old)
{
// The reader can return the same still-fresh physics publication.
// It supplies no new motion or active -> inactive evidence. A
// conflicting pose under that identity is not a harmless repeat.
if(frame.Sequence==old.Sequence&&frame.ObservedAt==old.ObservedAt)
{
if(frame.Position!=old.Position||frame.TargetPosition!=old.TargetPosition||frame.Forward!=old.Forward
||frame.Velocity!=old.Velocity||frame.TargetVelocity!=old.TargetVelocity||frame.Mode!=old.Mode)
Revoke("conflicting-duplicate-frame");
return;
}
var gap=frame.ObservedAt-old.ObservedAt;
if(frame.Sequence<=old.Sequence||gap<=TimeSpan.Zero){Revoke("observation-gap-or-reordered-frame");return;}
if(gap>SegmentSampleGap)
{
// Never bridge a missing interval into the exit proof. Only
// the still-active original duty can provide a new continuous
// segment afterwards; a cold/stationary endpoint cannot.
if(gap>ReacquisitionGap||!launchActive||naturallyCompleted)
{Revoke("observation-gap-or-reordered-frame");return;}
var oldRelative=old.Position-old.TargetPosition;
if(direction is not null&&old.TargetExclusionRadius is {} oldRadius&&oldRadius>0&&oldRelative.Length>oldRadius
&&frame.TargetExclusionRadius is {} radius&&radius>0&&relative.Length>radius&&!ContinuationGeometry(frame,relative))
{Revoke("unsafe-outside-path-across-observation-gap");return;}
ResetSegment();previous=frame;lastRelative=relative;return;
}
}
if(launchActive)
{
if(naturallyCompleted){Revoke("native-duty-resumed");return;}
activeSeen=true;
if(previous is {} oldFrame)
{
var step=relative-(oldFrame.Position-oldFrame.TargetPosition);
if(step.Length>.05)
{
var motion=step.Unit();var outward=Vector3d.Dot(relative,motion)>0&&relative.Length>(oldFrame.Position-oldFrame.TargetPosition).Length;
var straight=outward&&Vector3d.Dot(motion,frame.Forward.Unit())>=Alignment
&&(segmentDirection is null||Vector3d.Dot(motion,segmentDirection.Value)>=Alignment);
if(straight)
{
segmentStarted??=oldFrame.ObservedAt;segmentDirection??=motion;lastMotion=frame.ObservedAt;segments++;
if(segments>=3&&frame.ObservedAt-segmentStarted>=TimeSpan.FromMilliseconds(500))direction=motion;
}
else ResetSegment();
}
if(direction is {} heading&&Vector3d.Dot(frame.Forward.Unit(),heading)<Alignment)
ResetSegment();
}
}
else if(!naturallyCompleted)
{
// Only the observed active -> inactive transition can end our duty.
// A cold inactive snapshot or a cancellation is never enough.
naturallyCompleted=activeSeen&&!launchRequested&&direction is not null&&previous is not null
&&lastMotion is {} moved&&frame.ObservedAt-moved<=TimeSpan.FromSeconds(5);
if(!naturallyCompleted){Revoke("native-exit-segment-not-confirmed");return;}
}
if(naturallyCompleted&&(launchRequested||!ContinuationGeometry(frame,relative))){Revoke("outside-envelope-or-outward-continuation-not-confirmed");return;}
if(previous is null){commander=player.CommanderId;ship=player.CurrentShipId;session=player.LoadGameAt;fingerprint=frame.LoadoutFingerprint;}
previous=frame;lastRelative=relative;
}
public bool CanContinue(SystemFlightFrame frame,DateTimeOffset now)=>!revoked&&naturallyCompleted&&previous is {} sample&&sample.Sequence==frame.Sequence
&&sample.ObservedAt==frame.ObservedAt&&frame.ObservedAt<=now&&now-frame.ObservedAt<=TimeSpan.FromMilliseconds(300)
&&ContinuationGeometry(frame,frame.Position-frame.TargetPosition);
private bool ContinuationGeometry(SystemFlightFrame frame,Vector3d relative)
{
if(direction is not {} heading||frame.TargetExclusionRadius is not {} radius||radius<=0||!double.IsFinite(radius)
||relative.Length<=radius||Vector3d.Dot(relative,heading)<=0||Vector3d.Dot(frame.Forward.Unit(),heading)<Alignment)return false;
var step=relative-lastRelative;var forward=Vector3d.Dot(step,heading);
return forward>=-.5&&(step-heading*forward).Length<=2
&&Vector3d.Dot(frame.Velocity-frame.TargetVelocity,heading)>=-1;
}
private void Revoke(string reason){revoked=true;direction=null;WithdrawalReason=reason;}
private void ResetSegment(){direction=segmentDirection=null;segmentStarted=null;lastMotion=null;segments=0;}
}