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

XFEEDToolBox

【WinUI】XFE精英危险工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using XFEEDToolBox.Core.Models;

namespace XFEEDToolBox.Core.Galaxy;

public readonly record struct GalaxyFloorCandidate(long? SystemAddress, Coordinates Position);
public readonly record struct GalaxyFloorSelection(Coordinates Position, GalaxyFloorCandidate? Target);

/// <summary>
/// The original map's selector follows the Galactic XZ floor and snaps to a
/// star's vertical intersection, independently of the star's height. Capture
/// distances are supplied by the caller: original pixel thresholds are unverified.
/// </summary>
public static class GalaxyFloorSelector
{
    /// <summary>
    /// Original 3B507D0/3A437C0 settles only the target floor height with
    /// Floor(Y+.5) while neither the crane mode nor a crane axis is active.
    /// This intentionally differs from banker's rounding at half values.
    /// </summary>
    public static Coordinates SettleHeight(Coordinates focus, bool cameraCraning, double craneInput)
    {
        if (!focus.IsValid || !double.IsFinite(craneInput)) throw new ArgumentException("星图地平面坐标无效。");
        return cameraCraning || craneInput != 0 ? focus : new(focus.X, Math.Floor(focus.Y + .5), focus.Z);
    }

    public static Coordinates Intersection(Coordinates star, double floorY)
    {
        if (!star.IsValid || !double.IsFinite(floorY)) throw new ArgumentException("星图地平面坐标无效。");
        return new(star.X, floorY, star.Z);
    }

    /// <summary>
    /// Candidates must already pass the current query/filter. A held target is
    /// retained only while present in that sequence and within the release radius.
    /// The returned position remains on the input selector's floor, never star Y.
    /// </summary>
    public static GalaxyFloorSelection Snap(GalaxyCamera camera, Coordinates selector,
        ReadOnlySpan<GalaxyFloorCandidate> candidates, double capturePixels, double releasePixels,
        GalaxyFloorCandidate? heldSystem = null)
    {
        if (!selector.IsValid || !double.IsFinite(capturePixels) || !double.IsFinite(releasePixels)
            || capturePixels <= 0 || releasePixels < capturePixels)
            throw new ArgumentException("星图选择半径无效。");
        var origin = camera.ProjectForCatalogue(selector);
        if (!origin.IsVisible) return new(selector, null);
        GalaxyFloorCandidate? nearest = null;
        GalaxyFloorCandidate? retained = null;
        var best = capturePixels * capturePixels;
        var release = releasePixels * releasePixels;
        foreach (var candidate in candidates)
        {
            if (candidate.Position is not { IsValid: true }) continue;
            var point = camera.ProjectForCatalogue(Intersection(candidate.Position, selector.Y));
            if (!point.IsVisible) continue;
            var dx = point.X - origin.X;
            var dy = point.Y - origin.Y;
            var distance = dx * dx + dy * dy;
            if (distance <= best)
            {
                // Equal distances retain the first catalogue item deterministically.
                if (nearest is null || distance < best) nearest = candidate;
                best = distance;
            }
            if (heldSystem is { } held && distance <= release && SameSystem(candidate, held))
                retained = candidate;
        }
        var selected = retained ?? nearest;
        return new(selected is { } target ? Intersection(target.Position, selector.Y) : selector, selected);
    }

    private static bool SameSystem(GalaxyFloorCandidate left, GalaxyFloorCandidate right) =>
        left.SystemAddress is { } address && right.SystemAddress is { } other
            ? address == other
            : left.Position == right.Position;

    /// <summary>Intersect a screen ray with an actual Galactic horizontal plane.</summary>
    public static bool TryScreenToPlane(GalaxyCamera camera, double screenX, double screenY,
        double floorY, out Coordinates position)
    {
        position = camera.Focus;
        if (!double.IsFinite(screenX) || !double.IsFinite(screenY) || !double.IsFinite(floorY)) return false;
        var x = (screenX - camera.Width / 2) / camera.FocalLength;
        var y = (camera.Height / 2 - screenY) / camera.FocalLength;
        var ray = camera.Forward + camera.Right * x + camera.Up * y;
        if (Math.Abs(ray.Y) < 1e-12) return false;
        var depth = (floorY - camera.Eye.Y) / ray.Y;
        if (depth < camera.Near || !double.IsFinite(depth)) return false;
        position = new(camera.Eye.X + ray.X * depth, floorY, camera.Eye.Z + ray.Z * depth);
        return position.IsValid;
    }

    /// <summary>
    /// A circle on the real XZ plane, perspective projected and clipped. The
    /// explicit world radius does not claim the original ring's exact size.
    /// </summary>
    public static int WriteRing(Span<MapSegment> output, GalaxyCamera camera, Coordinates selector, double worldRadius)
    {
        if (!selector.IsValid || !double.IsFinite(worldRadius) || worldRadius <= 0 || output.Length < 8) return 0;
        var sides = Math.Min(64, output.Length);
        var count = 0;
        Coordinates At(int index)
        {
            var angle = index * Math.Tau / sides;
            return new(selector.X + Math.Cos(angle) * worldRadius, selector.Y, selector.Z + Math.Sin(angle) * worldRadius);
        }
        var previous = At(0);
        for (var index = 1; index <= sides; index++)
        {
            var next = At(index);
            if (GalaxyMapGeometry.TryProjectSegment(camera, previous, next, out var segment)) output[count++] = segment;
            previous = next;
        }
        return count;
    }
}