RynthSuite as of 2026-11-08 (internal 6ec9b64): release 2026.11.8.1
This commit is contained in:
parent
7166d256ce
commit
f8fa62b64c
196 changed files with 24980 additions and 633 deletions
|
|
@ -16,6 +16,13 @@ namespace RynthNav.Routing;
|
|||
/// except the leg's own. Its own navmesh, filled from the tile files a few tiles at a time, so it
|
||||
/// runs on a worker and never touches the walker's (tick-thread) navmesh. One instance per worker.
|
||||
/// Without a route graph only short legs (a few landblocks) are planned; longer ones get none.
|
||||
///
|
||||
/// The route maker (LoopMaker, RynthNavPlugin.RouteMaker.cs) sets the stricter options: no way
|
||||
/// through a portal's circle at all (<see cref="StrictPortals"/>), no straight line over a navmesh gap
|
||||
/// (<see cref="NoGaps"/>), corners pushed off walls (<see cref="WallClearance"/>, RynthNav's own walks
|
||||
/// do this as they go), building floors left out where there's another way (<see cref="AvoidArea"/>);
|
||||
/// and it asks this navmesh for ground (<see cref="Snap"/>, <see cref="WallDistance"/>, <see cref="Leg"/>).
|
||||
/// With none of them set it plans exactly as it always has.
|
||||
/// </summary>
|
||||
internal sealed class TilePathFinder
|
||||
{
|
||||
|
|
@ -27,27 +34,41 @@ internal sealed class TilePathFinder
|
|||
|
||||
private readonly string _dir;
|
||||
private readonly NavGraph? _graph;
|
||||
private readonly IReadOnlyList<PortalAvoid.Spot> _portals;
|
||||
private readonly IReadOnlyList<PortalAvoid.Spot> _portals, _live;
|
||||
private readonly DtNavMesh _nav;
|
||||
private DtNavMeshQuery _query;
|
||||
private readonly HashSet<uint> _loaded = new(), _bad = new();
|
||||
private readonly PortalAvoid _avoid = new();
|
||||
private readonly AvoidFilter _filter;
|
||||
private readonly AreaFilter _area;
|
||||
private readonly DtQueryDefaultFilter _plain = new();
|
||||
private string _pieceNote = "";
|
||||
|
||||
/// <summary>Tile files read so far (for the log).</summary>
|
||||
public int TilesRead { get; private set; }
|
||||
|
||||
public TilePathFinder(string navDataDir, NavGraph? graph, IReadOnlyList<PortalAvoid.Spot>? portals)
|
||||
/// <summary>Never through a portal's circle: no plan through one when there's no way round, and a bent path that still crosses one is refused.</summary>
|
||||
public bool StrictPortals { get; set; }
|
||||
/// <summary>A stretch the navmesh can't plan fails the walk, instead of becoming a straight line.</summary>
|
||||
public bool NoGaps { get; set; }
|
||||
/// <summary>Each corner pushed this far out from the wall it turns round, where the ground is walkable (0 = not).</summary>
|
||||
public double WallClearance { get; set; }
|
||||
/// <summary>Ground kept off where there is another way (east, north: true = keep off); null = none.</summary>
|
||||
public Func<double, double, bool>? AvoidArea { get; set; }
|
||||
|
||||
public TilePathFinder(string navDataDir, NavGraph? graph, IReadOnlyList<PortalAvoid.Spot>? portals,
|
||||
IReadOnlyList<PortalAvoid.Spot>? livePortals = null)
|
||||
{
|
||||
_dir = navDataDir;
|
||||
_graph = graph;
|
||||
_portals = portals ?? Array.Empty<PortalAvoid.Spot>();
|
||||
_live = livePortals ?? Array.Empty<PortalAvoid.Spot>();
|
||||
_nav = new DtNavMesh();
|
||||
var p = new DtNavMeshParams { orig = new RcVec3f(0, 0, 0), tileWidth = 192f, tileHeight = 192f, maxTiles = MaxTiles, maxPolys = 1 << 16 };
|
||||
_nav.Init(p, 6);
|
||||
_query = new DtNavMeshQuery(_nav);
|
||||
_filter = new AvoidFilter(_avoid);
|
||||
_area = new AreaFilter(_filter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -88,6 +109,12 @@ internal sealed class TilePathFinder
|
|||
{
|
||||
bool last = k + 2 == anchors.Count;
|
||||
List<WorldPt>? piece = Piece(anchors[k], anchors[k + 1], last && ownPortal ? anchors[k + 1] : null);
|
||||
if (piece == null && NoGaps)
|
||||
{
|
||||
note = $"no navmesh path from {NavCoords.Fmt(anchors[k].Ns, anchors[k].Ew)} to {NavCoords.Fmt(anchors[k + 1].Ns, anchors[k + 1].Ew)}"
|
||||
+ (_pieceNote.Length > 0 ? ": " + _pieceNote : "");
|
||||
return null;
|
||||
}
|
||||
if (piece == null) { gaps++; piece = new List<WorldPt> { anchors[k], anchors[k + 1] }; }
|
||||
for (int i = 1; i < piece.Count; i++) path.Add(piece[i]);
|
||||
}
|
||||
|
|
@ -129,35 +156,51 @@ internal sealed class TilePathFinder
|
|||
return (Math.Abs(ax - bx) + 1) * (Math.Abs(ay - by) + 1);
|
||||
}
|
||||
|
||||
/// <summary>The Detour path a→b (both ends' tiles and those between loaded); null when there's none.</summary>
|
||||
/// <summary>The Detour path a→b (both ends' tiles and those between loaded); null when there's none (_pieceNote: why).</summary>
|
||||
private List<WorldPt>? Piece(WorldPt a, WorldPt b, WorldPt? own)
|
||||
{
|
||||
if (!LoadBox(a, b)) return null;
|
||||
_pieceNote = "";
|
||||
if (!LoadBox(a, b)) { _pieceNote = "no navmesh tiles there"; return null; }
|
||||
var half = new RcVec3f(8, 600, 8);
|
||||
var pa = new RcVec3f((float)a.X, double.IsNaN(a.H) ? 0f : (float)a.H, (float)a.Y);
|
||||
var pb = new RcVec3f((float)b.X, double.IsNaN(b.H) ? 0f : (float)b.H, (float)b.Y);
|
||||
_query.FindNearestPoly(pa, half, _plain, out long sRef, out RcVec3f sPt, out _);
|
||||
_query.FindNearestPoly(pb, half, _plain, out long gRef, out RcVec3f gPt, out _);
|
||||
if (sRef == 0 || gRef == 0) return null;
|
||||
if (sRef == 0 || gRef == 0) { _pieceNote = $"no walkable ground at its {(sRef == 0 ? "start" : "end")}"; return null; }
|
||||
|
||||
// Portals near this piece, except the leg's own.
|
||||
var near = new List<PortalAvoid.Spot>();
|
||||
var nearLive = new List<PortalAvoid.Spot>();
|
||||
double minX = Math.Min(a.X, b.X) - AvoidNearUnits, maxX = Math.Max(a.X, b.X) + AvoidNearUnits;
|
||||
double minY = Math.Min(a.Y, b.Y) - AvoidNearUnits, maxY = Math.Max(a.Y, b.Y) + AvoidNearUnits;
|
||||
foreach (PortalAvoid.Spot sp in _portals)
|
||||
if (sp.X >= minX && sp.X <= maxX && sp.Y >= minY && sp.Y <= maxY) near.Add(sp);
|
||||
if (own is WorldPt ownAt) _avoid.Set(Array.Empty<PortalAvoid.Spot>(), near, ownAt.X, ownAt.Y, OwnPortalUnits);
|
||||
else _avoid.Set(Array.Empty<PortalAvoid.Spot>(), near);
|
||||
foreach (PortalAvoid.Spot sp in _live)
|
||||
if (sp.X >= minX && sp.X <= maxX && sp.Y >= minY && sp.Y <= maxY) nearLive.Add(sp);
|
||||
if (own is WorldPt ownAt) _avoid.Set(nearLive, near, ownAt.X, ownAt.Y, OwnPortalUnits);
|
||||
else _avoid.Set(nearLive, near);
|
||||
_filter.AllowA = sRef; _filter.AllowB = gRef;
|
||||
|
||||
Span<long> polys = new long[1024];
|
||||
_query.FindPath(sRef, gRef, sPt, gPt, _filter, polys, out int pc, 1024);
|
||||
int pc = 0;
|
||||
if (AvoidArea != null)
|
||||
{
|
||||
// Off the floors kept off (buildings) when there's a way; else as without.
|
||||
_area.Inside = AvoidArea; _area.AllowA = sRef; _area.AllowB = gRef;
|
||||
_query.FindPath(sRef, gRef, sPt, gPt, _area, polys, out pc, 1024);
|
||||
if (pc == 0 || polys[pc - 1] != gRef) pc = 0;
|
||||
}
|
||||
if (pc == 0) _query.FindPath(sRef, gRef, sPt, gPt, _filter, polys, out pc, 1024);
|
||||
if ((pc == 0 || polys[pc - 1] != gRef) && _avoid.Count > 0)
|
||||
{
|
||||
_query.FindPath(sRef, gRef, sPt, gPt, _plain, polys, out pc, 1024);
|
||||
if (pc == 0 || polys[pc - 1] != gRef) return null;
|
||||
// Strict: a way that only goes through a portal's circle is no way (and that is why).
|
||||
if (StrictPortals && pc > 0 && polys[pc - 1] == gRef) { _pieceNote = "no way round " + PortalsNamed(_avoid.Spots); return null; }
|
||||
}
|
||||
if (pc == 0 || polys[pc - 1] != gRef) { _pieceNote = "the navmesh doesn't join them (water, a cliff or a wall between)"; return null; }
|
||||
Span<DtStraightPath> sp2 = new DtStraightPath[256];
|
||||
_query.FindStraightPath(sPt, gPt, polys[..pc], pc, sp2, out int spc, 256, 0);
|
||||
if (spc < 2) return null;
|
||||
if (spc < 2) { _pieceNote = "no straight path along the navmesh"; return null; }
|
||||
|
||||
var corners = new List<(double X, double Y)>(spc);
|
||||
List<double>? heights = new List<double>(spc);
|
||||
|
|
@ -182,6 +225,20 @@ internal sealed class TilePathFinder
|
|||
corners = _avoid.Bend(corners, OnMesh, 12, LegOnMesh);
|
||||
if (corners.Count != before) heights = null; // bend corners: heights filled from the neighbours
|
||||
}
|
||||
if (WallClearance > 0 && corners.Count > 2)
|
||||
{
|
||||
// As RynthNav's own walks (Replan): each corner pushed out along the turn's outside, onto ground.
|
||||
var pushed = new List<(double X, double Y)>(corners);
|
||||
PushFromWalls(pushed, heights, pa.Y);
|
||||
if (!StrictPortals || Crosses(pushed) < 0) corners = pushed; // a push into a portal's circle isn't taken
|
||||
}
|
||||
int hit = StrictPortals ? Crosses(corners) : -1;
|
||||
if (hit >= 0)
|
||||
{
|
||||
string name = _avoid.Spots[hit].Name;
|
||||
_pieceNote = $"it would cross {(name.Length > 0 ? "the portal " + name : "a portal")}";
|
||||
return null;
|
||||
}
|
||||
var o = new List<WorldPt>(corners.Count) { a with { H = heights != null ? heights[0] : (double.IsNaN(a.H) ? sPt.Y : a.H) } };
|
||||
for (int i = 1; i < corners.Count; i++)
|
||||
o.Add(new WorldPt(corners[i].X, corners[i].Y, heights != null ? heights[i] : double.NaN));
|
||||
|
|
@ -189,6 +246,95 @@ internal sealed class TilePathFinder
|
|||
return o;
|
||||
}
|
||||
|
||||
/// <summary>"the portal X" / "the portals X and Y" / "3 portals" for a refusal.</summary>
|
||||
private static string PortalsNamed(IReadOnlyList<PortalAvoid.Spot> spots)
|
||||
{
|
||||
var names = new List<string>();
|
||||
foreach (PortalAvoid.Spot s in spots) if (s.Name.Length > 0 && !names.Contains(s.Name)) names.Add(s.Name);
|
||||
if (names.Count == 0) return spots.Count == 1 ? "a portal" : $"{spots.Count} portals";
|
||||
if (names.Count > 2) return $"{spots.Count} portals ({names[0]}, {names[1]}, ...)";
|
||||
return names.Count == 1 ? "the portal " + names[0] : $"the portals {names[0]} and {names[1]}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The first kept-out circle a leg of <paramref name="pts"/> enters (or its end lies in), or -1. A circle
|
||||
/// the start is already in doesn't count: that's where the walk begins (the walker steers out of it).
|
||||
/// </summary>
|
||||
private int Crosses(IReadOnlyList<(double X, double Y)> pts)
|
||||
{
|
||||
IReadOnlyList<PortalAvoid.Spot> spots = _avoid.Spots;
|
||||
if (pts.Count == 0) return -1;
|
||||
for (int k = 0; k < spots.Count; k++)
|
||||
{
|
||||
PortalAvoid.Spot s = spots[k];
|
||||
if (Math.Sqrt((pts[0].X - s.X) * (pts[0].X - s.X) + (pts[0].Y - s.Y) * (pts[0].Y - s.Y)) < s.R) continue;
|
||||
for (int i = 1; i < pts.Count; i++)
|
||||
if (PortalAvoid.SegmentDistance(s.X, s.Y, pts[i - 1].X, pts[i - 1].Y, pts[i].X, pts[i].Y) < s.R - 0.05) return k;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Walls: each corner the path turns round sits on the mesh's edge; push it <see cref="WallClearance"/>
|
||||
/// out along the turn's outside bisector where the ground is walkable (inside a polygon, not merely
|
||||
/// near one), as the walker's Replan does. The ends never move.
|
||||
/// </summary>
|
||||
private void PushFromWalls(List<(double X, double Y)> wps, List<double>? heights, float startH)
|
||||
{
|
||||
for (int i = 1; i < wps.Count - 1; i++)
|
||||
{
|
||||
var (px, py) = wps[i]; var (ax, ay) = wps[i - 1]; var (bx, by) = wps[i + 1];
|
||||
double ux = ax - px, uy = ay - py, ul = Math.Sqrt(ux * ux + uy * uy);
|
||||
double vx = bx - px, vy = by - py, vl = Math.Sqrt(vx * vx + vy * vy);
|
||||
if (ul < 0.5 || vl < 0.5) continue;
|
||||
double ox = -(ux / ul + vx / vl), oy = -(uy / ul + vy / vl), ol = Math.Sqrt(ox * ox + oy * oy);
|
||||
if (ol < 0.05) continue; // straight on: no corner to clear
|
||||
double nx = px + ox / ol * WallClearance, ny = py + oy / ol * WallClearance;
|
||||
float h = heights != null && i < heights.Count ? (float)heights[i] : startH;
|
||||
_query.FindNearestPoly(new RcVec3f((float)nx, h, (float)ny), new RcVec3f(0.3f, 24, 0.3f), _plain, out long rr, out RcVec3f rp, out _);
|
||||
if (rr != 0 && Math.Abs(rp.X - nx) < 0.05 && Math.Abs(rp.Z - ny) < 0.05) wps[i] = (nx, ny);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Ground for the route maker (LoopMaker) ───────────────────────────
|
||||
|
||||
/// <summary>Loads the tiles under the box (world units); false when it's more than this navmesh holds at once, or there are none.</summary>
|
||||
public bool LoadArea(double minX, double minY, double maxX, double maxY) =>
|
||||
LoadBox(new WorldPt(minX, minY, double.NaN), new WorldPt(maxX, maxY, double.NaN));
|
||||
|
||||
/// <summary>
|
||||
/// Walkable ground within <paramref name="reach"/> yards (across) of <paramref name="p"/>, the nearest
|
||||
/// to its height (NaN: to height 0, so the lowest floor); its tiles must be loaded. False when none.
|
||||
/// </summary>
|
||||
public bool Snap(WorldPt p, double reach, out WorldPt ground, out long poly)
|
||||
{
|
||||
ground = p;
|
||||
var at = new RcVec3f((float)p.X, double.IsNaN(p.H) ? 0f : (float)p.H, (float)p.Y);
|
||||
_query.FindNearestPoly(at, new RcVec3f((float)reach, 600, (float)reach), _plain, out poly, out RcVec3f pt, out _);
|
||||
if (poly == 0) return false;
|
||||
ground = new WorldPt(pt.X, pt.Z, pt.Y);
|
||||
if (ground.FlatDistance(p) > reach) { poly = 0; return false; } // the search box's corner: farther than asked
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>How far ground found by <see cref="Snap"/> is from the navmesh's edge (a wall, a drop, water), at most <paramref name="max"/>.</summary>
|
||||
public double WallDistance(WorldPt ground, long poly, double max)
|
||||
{
|
||||
DtStatus st = _query.FindDistanceToWall(poly, new RcVec3f((float)ground.X, (float)ground.H, (float)ground.Y), (float)max, _plain, out float d, out _, out _);
|
||||
return st.Failed() ? 0 : Math.Min(d, max);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A leg between two points on ground already loaded (no route graph: a loop's legs are short), with
|
||||
/// the options set; null when there is none, and <paramref name="note"/> says why.
|
||||
/// </summary>
|
||||
public List<WorldPt>? Leg(WorldPt from, WorldPt to, out string note)
|
||||
{
|
||||
List<WorldPt>? piece = Piece(from, to, null);
|
||||
note = piece == null ? _pieceNote : "";
|
||||
return piece;
|
||||
}
|
||||
|
||||
/// <summary>Loads the tiles of the box a..b (evicting others when full); false when it's too big or none loads.</summary>
|
||||
private bool LoadBox(WorldPt a, WorldPt b)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue