104 lines
5 KiB
C#
104 lines
5 KiB
C#
using System;
|
|
|
|
namespace RynthCore.Plugin.RynthNav;
|
|
|
|
/// <summary>
|
|
/// Keeps a goto on its trip's own path (2026-11-27). The trip's ground (TripPath: the walk the path markers draw
|
|
/// and "save as nav route" writes) is worked out once per step, but the walker never used it: every re-plan aimed
|
|
/// at the route graph's next border crossing (or straight at the step's point), so the walk took its own way,
|
|
/// across a field or round the far side of a building, away from the markers.
|
|
///
|
|
/// Now each re-plan aims <see cref="AheadUnits"/> along the step's walk past where you are on it, and the navmesh
|
|
/// plans just those yards, so walls and the portal circles are kept out of as before. More than
|
|
/// <see cref="OffUnits"/> off the walk (knocked off it, stepped round something), the aim is the walk again, a
|
|
/// little past its nearest point. How far along you are only ever grows, and the nearest point is looked for only
|
|
/// a little behind it, so a walk that comes back close to itself is never cut across.
|
|
/// Host-free: the route tests drive it. Spotted by SilentKelpie (Forgejo rynth PR #12); re-implemented here.
|
|
/// </summary>
|
|
internal sealed class TripFollow
|
|
{
|
|
/// <summary>How far along the walk past where you are each re-plan aims (yards).</summary>
|
|
public const double AheadUnits = 28.0;
|
|
/// <summary>Farther than this from the walk is off it: aim back at it.</summary>
|
|
public const double OffUnits = 14.0;
|
|
/// <summary>Off the walk: aim this far past its nearest point.</summary>
|
|
public const double RejoinUnits = 6.0;
|
|
/// <summary>The nearest point on the walk is looked for this far behind the progress so far...</summary>
|
|
public const double BackUnits = 12.0;
|
|
/// <summary>...and this far ahead of it.</summary>
|
|
public const double ForwardUnits = 90.0;
|
|
|
|
private readonly WorldPt[] _pts;
|
|
private readonly double[] _along; // yards from the start to each point
|
|
private double _progress;
|
|
|
|
public int TripId { get; }
|
|
public int Step { get; }
|
|
public double Length => _along[^1];
|
|
/// <summary>How far along the walk you are (yards; never goes back).</summary>
|
|
public double Progress => _progress;
|
|
|
|
public TripFollow(int tripId, int step, WorldPt[] pts)
|
|
{
|
|
if (pts == null || pts.Length < 2) throw new ArgumentException("a walk needs two points", nameof(pts));
|
|
TripId = tripId; Step = step;
|
|
_pts = pts;
|
|
_along = new double[pts.Length];
|
|
for (int i = 1; i < pts.Length; i++) _along[i] = _along[i - 1] + pts[i].FlatDistance(pts[i - 1]);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Where to plan to from (x, y): <see cref="AheadUnits"/> along the walk past where you are, or (more than
|
|
/// <see cref="OffUnits"/> from it) a little past its nearest point. <paramref name="off"/>: yards from the walk.
|
|
/// <paramref name="atEnd"/>: the aim is the walk's end (the step's own point: plan to it as before).
|
|
/// </summary>
|
|
public WorldPt Aim(double x, double y, out double off, out bool atEnd)
|
|
{
|
|
double s = NearestAlong(x, y, out off);
|
|
double aim;
|
|
if (off > OffUnits) aim = s + RejoinUnits;
|
|
else
|
|
{
|
|
if (s > _progress) _progress = s;
|
|
aim = _progress + AheadUnits;
|
|
}
|
|
atEnd = aim >= Length;
|
|
return atEnd ? _pts[^1] : At(aim);
|
|
}
|
|
|
|
/// <summary>The point <paramref name="s"/> yards along the walk (its height between the points' heights).</summary>
|
|
public WorldPt At(double s)
|
|
{
|
|
s = Math.Clamp(s, 0, Length);
|
|
int i = 1;
|
|
while (i < _pts.Length - 1 && _along[i] < s) i++;
|
|
double seg = _along[i] - _along[i - 1];
|
|
double t = seg < 1e-9 ? 0 : (s - _along[i - 1]) / seg;
|
|
WorldPt a = _pts[i - 1], b = _pts[i];
|
|
return new WorldPt(a.X + (b.X - a.X) * t, a.Y + (b.Y - a.Y) * t, a.H + (b.H - a.H) * t);
|
|
}
|
|
|
|
/// <summary>How far along the walk its nearest point to (x, y) is, within BackUnits behind and ForwardUnits ahead of the progress.</summary>
|
|
private double NearestAlong(double x, double y, out double dist)
|
|
{
|
|
double lo = Math.Max(0, _progress - BackUnits), hi = _progress + ForwardUnits;
|
|
double best = double.MaxValue, bestS = _progress;
|
|
for (int i = 1; i < _pts.Length; i++)
|
|
{
|
|
double s0 = _along[i - 1], s1 = _along[i];
|
|
if (s1 < lo) continue;
|
|
if (s0 > hi) break;
|
|
double ax = _pts[i - 1].X, ay = _pts[i - 1].Y, vx = _pts[i].X - ax, vy = _pts[i].Y - ay;
|
|
double len = s1 - s0;
|
|
double t = len < 1e-9 ? 0 : Math.Clamp(((x - ax) * vx + (y - ay) * vy) / (len * len), 0, 1);
|
|
// Only the part of the segment inside the window counts.
|
|
double tLo = len < 1e-9 ? 0 : Math.Clamp((lo - s0) / len, 0, 1), tHi = len < 1e-9 ? 1 : Math.Clamp((hi - s0) / len, 0, 1);
|
|
t = Math.Clamp(t, tLo, tHi);
|
|
double px = ax + vx * t - x, py = ay + vy * t - y;
|
|
double d = Math.Sqrt(px * px + py * py);
|
|
if (d < best) { best = d; bestS = s0 + len * t; }
|
|
}
|
|
dist = best;
|
|
return bestS;
|
|
}
|
|
}
|