XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEEDToolBox

【WinUI】XFE精英危险工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Globalization;
using System.Text.Json;

namespace XFEEDToolBox.Core.Navigation;

/// <summary>Match an original physical station body to the selected catalogue target and local inertial centre.</summary>
public static class StationDepartureEnvelope
{
    public static double? ReadRadius(JsonElement geometry,ulong referenceWorld,ulong shipEntity,SystemNavigationTarget target,Vector3d targetPosition,DateTimeOffset now)
    {
        if(target.Kind!=SystemTargetKind.Station||!NativeDockingTargetKinds.SupportsDocking((uint)target.NativeKind)||shipEntity==0||!targetPosition.Finite)return null;
        try
        {
            static ulong Address(JsonElement value,string name)=>ulong.Parse(value.GetProperty(name).GetString()!,CultureInfo.InvariantCulture);
            static Vector3d Vector(JsonElement value,string name){var data=value.GetProperty(name);return new(data[0].GetDouble(),data[1].GetDouble(),data[2].GetDouble());}
            if(!geometry.GetProperty("fresh").GetBoolean()||!geometry.GetProperty("referenceFrameVerified").GetBoolean()
                ||Address(geometry,"worldAddress")!=referenceWorld)return null;
            var origin=geometry.GetProperty("referenceOrigin");
            if(!origin.GetProperty("verified").GetBoolean()||Address(origin,"worldAddress")!=referenceWorld||Address(origin,"entityId")!=shipEntity)return null;
            var observedAt=DateTimeOffset.FromUnixTimeMilliseconds(origin.GetProperty("observedAtUnixMilliseconds").GetInt64());
            if(observedAt>now||now-observedAt>TimeSpan.FromMilliseconds(300))return null;
            var offset=Vector(origin,"position");if(!offset.Finite)return null;
            var matches=geometry.GetProperty("nearbyBodies").EnumerateArray().Where(body=>Address(body,"bodyAddress")==target.BodyAddress).ToArray();
            if(matches.Length!=1)return null;var body=matches[0];
            // Native catalogue kind is deliberately not used as physical kind.
            if(body.GetProperty("bodyKind").GetInt32()!=6)return null;
            var radius=body.GetProperty("exclusionRadius").GetDouble();var centre=Vector(body,"centre")+offset;
            return double.IsFinite(radius)&&radius>0&&radius<=1e14&&centre.Finite&&(centre-targetPosition).Length<=Math.Max(1,radius*1e-6)?radius:null;
        }
        catch(Exception error)when(error is InvalidOperationException or KeyNotFoundException or FormatException or OverflowException or IndexOutOfRangeException or ArgumentOutOfRangeException)
        {return null;}
    }
}