using System; namespace RynthCore.Plugin.RynthNav; /// /// 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 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 /// 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. /// internal sealed class TripFollow { /// How far along the walk past where you are each re-plan aims (yards). public const double AheadUnits = 28.0; /// Farther than this from the walk is off it: aim back at it. public const double OffUnits = 14.0; /// Off the walk: aim this far past its nearest point. public const double RejoinUnits = 6.0; /// The nearest point on the walk is looked for this far behind the progress so far... public const double BackUnits = 12.0; /// ...and this far ahead of it. 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]; /// How far along the walk you are (yards; never goes back). 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]); } /// /// Where to plan to from (x, y): along the walk past where you are, or (more than /// from it) a little past its nearest point. : yards from the walk. /// : the aim is the walk's end (the step's own point: plan to it as before). /// 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); } /// The point yards along the walk (its height between the points' heights). 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); } /// How far along the walk its nearest point to (x, y) is, within BackUnits behind and ForwardUnits ahead of the progress. 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; } }