Snapshot for release 2026.11.2.1 (internal 03dbb32)

This commit is contained in:
Tom Bohar 2026-11-02 03:33:34 -05:00
commit 79cbb84d61
77 changed files with 8622 additions and 224 deletions

View file

@ -8,6 +8,7 @@ using DotRecast.Recast.Toolset;
using DotRecast.Recast.Toolset.Builder;
using RynthNav.Baker;
using RynthNav.Routing;
using RynthCore.Plugin.Shared;
internal static class Program
{
@ -36,8 +37,20 @@ internal static class Program
Run(nameof(RouteFollower), RouteFollower);
Run(nameof(EndToEndAroundAWall), EndToEndAroundAWall);
Run(nameof(LandmassesAndSameLand), LandmassesAndSameLand);
Run(nameof(FencedInGoalMovesToReachableGround), FencedInGoalMovesToReachableGround);
Run(nameof(RealFencedInGoal), RealFencedInGoal);
Run(nameof(PathGoesRoundAPortal), PathGoesRoundAPortal);
Run(nameof(TripWalkFromTileFiles), TripWalkFromTileFiles);
Run(nameof(PathInterfaceVersions), PathInterfaceVersions);
Run(nameof(PathTileBox), PathTileBox);
Run(nameof(PathExOpenFloorAndInfo), PathExOpenFloorAndInfo);
Run(nameof(PathExRoundAPortal), PathExRoundAPortal);
Run(nameof(PathExPartial), PathExPartial);
Run(nameof(PathExNoMeshAndStartSnap), PathExNoMeshAndStartSnap);
Run(nameof(PathExWallsAndBuffer), PathExWallsAndBuffer);
Run(nameof(RaycastOnTheMesh), RaycastOnTheMesh);
Run(nameof(RealPathEx), RealPathEx);
Run(nameof(RealDoorsteps), RealDoorsteps);
Run(nameof(RealTripWalk), RealTripWalk);
Run(nameof(MeshViewFloorAndWalls), MeshViewFloorAndWalls);
Run(nameof(MeshViewEmitBudgetAndFrame), MeshViewEmitBudgetAndFrame);
@ -63,6 +76,10 @@ internal static class Program
/// <summary>Flat rectangles (x0, z0, x1, z1) at height 0, plus a corner marker so the grid starts at (0,0).</summary>
private static string Bake(string name, params (float x0, float z0, float x1, float z1)[] floors)
=> BakeLayers(name, floors.Select(f => (f.x0, f.z0, f.x1, f.z1, 0f)).ToArray());
/// <summary>Flat rectangles (x0, z0, x1, z1) each at its own height y: a floor with a roof over part of it.</summary>
private static string BakeLayers(string name, params (float x0, float z0, float x1, float z1, float y)[] floors)
{
var verts = new List<float>(); var faces = new List<int>();
void Quad(float x0, float z0, float x1, float z1, float y)
@ -72,7 +89,7 @@ internal static class Program
// Both windings: Recast keeps the upward-facing one as walkable.
faces.AddRange(new[] { b, b + 1, b + 2, b, b + 2, b + 3, b, b + 2, b + 1, b, b + 3, b + 2 });
}
foreach (var f in floors) Quad(f.x0, f.z0, f.x1, f.z1, 0);
foreach (var f in floors) Quad(f.x0, f.z0, f.x1, f.z1, f.y);
// Pin the bounds to the landblock grid (unused vertices, as the baker does).
float maxX = floors.Max(f => f.x1), maxZ = floors.Max(f => f.z1);
verts.AddRange(new[] { 0f, -1f, 0f, (float)Math.Ceiling(maxX / 192) * 192f, 1f, (float)Math.Ceiling(maxZ / 192) * 192f });
@ -311,6 +328,78 @@ internal static class Program
Check(m != null && m.Count == 2 && m.Cost >= c.Cost - 1, $"with walked end legs too ({m?.Cost:F0})");
}
private static void FencedInGoalMovesToReachableGround()
{
// 11-01: a /loc on ground fenced in by a building's walls. Two tiles of open ground; in tile 1
// a small floor (300..320, 80..100) with a 10 m moat round it: a pocket region of its own,
// with no border links. Every route search to it found nothing (0 expanded), and the walk
// headed straight for the spot instead. The goal end now moves to the nearest ground a
// walk can end on.
string dir = Bake("pocket", (0, 10, 384, 70), (0, 110, 384, 180), (0, 70, 290, 110), (330, 70, 384, 110), (300, 80, 320, 100));
BuildGraph(dir, out NavGraph g);
g.BuildComponents();
var nav = new DtNavMesh();
var prm = new DtNavMeshParams { orig = new RcVec3f(0, 0, 0), tileWidth = 192f, tileHeight = 192f, maxTiles = 4, maxPolys = 1 << 16 };
nav.Init(prm, 6);
var regions = new Dictionary<uint, int[]>();
foreach (uint lb in new uint[] { 0x0000, 0x0100 })
{
DtMeshData md = ReadTile(dir, lb);
nav.AddTile(md, 0, 0, out _);
regions[lb] = NavRegions.Build(md, out _);
}
var q = new DtNavMeshQuery(nav);
NavCoarse.Probe? start = NavCoarse.ProbeNear(g, nav, q, 40, 0, 40, 12, regions);
Check(start != null && g.RegionPortals(start.Region).Length > 0, "the start is on open ground with border links");
NavCoarse.Probe? pocket = NavCoarse.ProbeNear(g, nav, q, 310, 0, 90, 40, regions);
Check(pocket != null && g.RegionPortals(pocket.Region).Length == 0, "the spot's own polygon is a pocket with no border links");
var none = g.FindRoute(start!.Region, 40, 40, pocket!.Region, 310, 90, out _, out int expanded);
Check(none == null && expanded == 0, $"to the pocket: no route, nothing expanded (the 11-01 log: '0 expanded') (expanded {expanded})");
Check(!NavCoarse.Reachable(g, start.Region, pocket.Region) && NavCoarse.Reachable(g, start.Region, start.Region), "a pocket isn't reachable; the start's own region is");
NavCoarse.Probe? goal = NavCoarse.ProbeReachable(g, nav, q, 310, 0, 90, 40, regions, start.Region, out bool moved);
double off = goal == null ? double.NaN : Math.Sqrt((goal.Pt.X - 310) * (goal.Pt.X - 310) + (goal.Pt.Z - 90) * (goal.Pt.Z - 90));
Check(goal != null && moved && goal.Region != pocket.Region && NavCoarse.Reachable(g, start.Region, goal.Region),
$"the goal moves to linked ground (region {goal?.Region}, moved {moved})");
Check(off < 25, $"...the nearest such ground, beside the moat ({off:F1} u from the spot)");
NavCoarse? c = NavCoarse.Plan(g, start, goal!, out _);
Check(c != null && c.Count >= 1 && Math.Abs(c.GoalEw - goal!.Pt.X) < 0.01 && !double.IsNaN(c.GoalUp), "a route to it, ending on that ground (with its height)");
NavCoarse.Probe? fromFile = NavCoarse.ProbeFile(g, dir, 310, 0, 90, 40, start.Region, out bool movedFile);
Check(fromFile != null && movedFile && fromFile.Region == goal!.Region, "the same from the tile file (the target's tile not loaded)");
NavCoarse.Probe? open = NavCoarse.ProbeReachable(g, nav, q, 250, 0, 150, 40, regions, start.Region, out bool movedOpen);
Check(open != null && !movedOpen && Math.Abs(open.Pt.X - 250) < 0.5 && Math.Abs(open.Pt.Z - 150) < 0.5, "a spot on open ground stays where it is");
}
private static void RealFencedInGoal()
{
// The 11-01 goto to 43.6N, 85.4W on the real navmesh: the spot probes onto a fenced-in
// polygon (a region of its own, no border links); every search from 41.8N, 82.2W (and from
// everywhere else on the way) found no route.
string dir = Environment.GetEnvironmentVariable("RYNTHNAV_NAVDATA") ?? @"C:\Games\RynthCore\NavData";
string graphFile = Path.Combine(dir, NavGraph.FileName);
if (!File.Exists(graphFile)) { Console.WriteLine(" (no real NavData here: skipped)"); return; }
NavGraph g = NavGraph.Load(graphFile);
g.BuildComponents();
static double W(double map) => (map * 10.0 + 1019.5) * 24.0;
double sx = W(-82.2), sy = W(41.8), tx = W(-85.4), ty = W(43.6);
NavCoarse.Probe? start = NavCoarse.ProbeFile(g, dir, sx, 22, sy, 12);
if (start == null) { Console.WriteLine(" (these tiles aren't the ones this test was written for: skipped)"); return; }
NavCoarse.Probe? old = NavCoarse.ProbeFile(g, dir, tx, 22, ty, 40);
NavCoarse.Probe? goal = NavCoarse.ProbeFile(g, dir, tx, 22, ty, 40, start.Region, out bool moved);
Check(old != null && goal != null, "both probes find ground");
if (old == null || goal == null) return;
bool oldRoute = NavCoarse.Plan(g, start, old, out int oldExpanded) != null;
NavCoarse? c = NavCoarse.Plan(g, start, goal, out int expanded);
double off = Math.Sqrt((goal.Pt.X - tx) * (goal.Pt.X - tx) + (goal.Pt.Z - ty) * (goal.Pt.Z - ty));
Console.WriteLine($" 43.6N 85.4W: the spot's own region {old.Region} ({g.RegionPortals(old.Region).Length} border links, route {(oldRoute ? "yes" : "none")}, {oldExpanded} expanded); "
+ $"moved {moved} to region {goal.Region}, {off:F1} u off; route {(c == null ? "none" : $"{c.Count} crossings, ~{c.Cost:F0} u")} ({expanded} expanded)");
Check(c != null, "a route to the ground beside the fenced-in spot");
Check(off < 12, $"that ground is beside the spot ({off:F1} u)");
}
private static void LandmassesAndSameLand()
{
// Two joined tiles (the mainland) and a tile two landblocks away (an island).
@ -431,6 +520,407 @@ internal static class Program
Check(own != null && Math.Abs(own[^1].X - 95) < 3, "walking into that portal: it's the goal, not avoided");
}
// ── "RynthNav.Path" v2: FindPathEx and Raycast (NavPathQuery, the plugin's interface) ──
/// <summary>Every tile file of a bake in one navmesh, as the plugin has its loaded tiles.</summary>
private static DtNavMeshQuery QueryOn(string dir)
{
var nav = new DtNavMesh();
var prm = new DtNavMeshParams { orig = new RcVec3f(0, 0, 0), tileWidth = 192f, tileHeight = 192f, maxTiles = 64, maxPolys = 1 << 16 };
nav.Init(prm, 6);
foreach (string f in Directory.GetFiles(dir, "nav_*.tile"))
{
uint lb = Convert.ToUInt32(Path.GetFileNameWithoutExtension(f).Substring(4), 16);
nav.AddTile(ReadTile(dir, lb), 0, 0, out _);
}
return new DtNavMeshQuery(nav);
}
private static PathExResult Ex(DtNavMeshQuery q, double sx, double sy, double gx, double gy, int flags,
AvoidFilter? portals = null, double clearance = 1.0, int maxCorners = 64, double sz = 0, double gz = 0)
{
var r = new PathExResult();
int code = NavPathQuery.FindPathEx(q, sx, sy, sz, gx, gy, gz, flags, maxCorners, portals, clearance, r);
if (code != r.Code) throw new InvalidOperationException("the returned code and the result's differ");
return r;
}
/// <summary>The path's closest pass to (x, y), flat.</summary>
private static double Closest(PathExResult r, double x, double y)
{
double best = double.MaxValue;
for (int i = 1; i < r.Corners.Count; i++)
best = Math.Min(best, PortalAvoid.SegmentDistance(x, y, r.Corners[i - 1].X, r.Corners[i - 1].Y, r.Corners[i].X, r.Corners[i].Y));
return best;
}
private static double Flat((double X, double Y, double Z) a, double x, double y) => Math.Sqrt((a.X - x) * (a.X - x) + (a.Y - y) * (a.Y - y));
private static double Legs(PathExResult r)
{
double len = 0;
for (int i = 1; i < r.Corners.Count; i++)
{
var a = r.Corners[i - 1]; var b = r.Corners[i];
len += Math.Sqrt((b.X - a.X) * (b.X - a.X) + (b.Y - a.Y) * (b.Y - a.Y) + (b.Z - a.Z) * (b.Z - a.Z));
}
return len;
}
/// <summary>
/// The table is handed to v1 callers too (older RynthAi's stuck recovery asks for 1) and to v2 and
/// v3 callers, never for 0 or a newer version; and the layout only ever grew at its end. v3
/// (2026-10-25) added no field, only the PathStartOutdoors flag (RynthAi's hunt plans from a
/// building's doorstep while the player is inside).
/// </summary>
private static unsafe void PathInterfaceVersions()
{
const string name = RynthNavPathApiV1.InterfaceName;
Check(RynthNavPathApiV1.InterfaceVersion == 3, "the contract is version 3");
Check(!NavPathQuery.ServesInterface(name, 0), "version 0: refused");
Check(NavPathQuery.ServesInterface(name, 1), "version 1 (FindPath callers): served the same table");
Check(NavPathQuery.ServesInterface(name, 2), "version 2: served");
Check(NavPathQuery.ServesInterface(name, 3), "version 3: served");
Check(!NavPathQuery.ServesInterface(name, 4) && !NavPathQuery.ServesInterface(name, uint.MaxValue), "a version newer than the one built: refused");
Check(RynthNavPathApiV1.PathStartOutdoors == 16
&& (RynthNavPathApiV1.PathStartOutdoors & (RynthNavPathApiV1.PathAvoidPortals | RynthNavPathApiV1.PathWallClearance
| RynthNavPathApiV1.PathAllowPartial | RynthNavPathApiV1.PathQuiet)) == 0,
"the v3 flag is a bit of its own");
Check(!NavPathQuery.ServesInterface("RynthNav.path", 1) && !NavPathQuery.ServesInterface("", 2) && !NavPathQuery.ServesInterface(null, 2)
&& !NavPathQuery.ServesInterface("RynthAi.Script", 1), "another interface's name: refused");
RynthNavPathApiV1 t = default;
byte* b = (byte*)&t;
int p = IntPtr.Size;
Check((byte*)&t.Version - b == 0 && (byte*)&t.StructSize - b == 4 && (byte*)&t.FindPath - b == 8, "v1's fields where v1 callers read them");
Check((byte*)&t.FindPathEx - b == 8 + p && (byte*)&t.Raycast - b == 8 + 2 * p && sizeof(RynthNavPathApiV1) == 8 + 3 * p,
$"v2 appended after FindPath (size {sizeof(RynthNavPathApiV1)} with {p}-byte pointers)");
Check(RynthNavPathApiV1.InfoLength == 6 && RynthNavPathApiV1.ResultPartial == 2 && RynthNavPathApiV1.RayClear == 1 && RynthNavPathApiV1.RayBlocked == 0,
"the v2 constants");
}
private static void PathTileBox()
{
Check(NavPathQuery.ClampMaxTiles(0) == 25 && NavPathQuery.ClampMaxTiles(-4) == 25, "maxTiles 0: the default 25");
Check(NavPathQuery.ClampMaxTiles(9) == 9 && NavPathQuery.ClampMaxTiles(49) == 49 && NavPathQuery.ClampMaxTiles(1000) == 49, "else as asked, at most 49");
double hx = 0xA9 * 192 + 96, hy = 0xB4 * 192 + 96; // the middle of Holtburg's landblock
Check(NavPathQuery.TileBox(hx, hy, hx + 50, hy + 50, 25, false, out int x0, out int y0, out int x1, out int y1)
&& x0 == 0xA8 && x1 == 0xAA && y0 == 0xB3 && y1 == 0xB5, "both ends in one landblock: it and the ring round it");
Check(!NavPathQuery.TileBox(hx, hy, hx + 192, hy, 9, false, out _, out _, out _, out _), "FindPath (v1): 4x3 with the margin doesn't fit 9, no smaller box");
Check(NavPathQuery.TileBox(hx, hy, hx + 192, hy, 9, true, out x0, out y0, out x1, out y1) && x0 == 0xA9 && x1 == 0xAA && y0 == 0xB4 && y1 == 0xB4,
"FindPathEx: then the two landblocks alone");
Check(NavPathQuery.TileBox(hx, hy, hx + 192, hy, 12, true, out x0, out _, out x1, out _) && x0 == 0xA8 && x1 == 0xAB, "with room for it, the margin stays");
Check(NavPathQuery.TileBox(hx, hy, hx + 192 * 8, hy, 49, true, out _, out _, out _, out _), "nine landblocks in a row fit 49 without the margin (9 tiles)");
Check(!NavPathQuery.TileBox(hx, hy, hx + 192 * 8, hy + 192 * 8, 49, true, out _, out _, out _, out _), "nine apart both ways (81 tiles): too far even for 49");
Check(NavPathQuery.TileBox(10, 10, 20, 20, 25, false, out x0, out y0, out x1, out y1) && x0 == 0 && y0 == 0 && x1 == 1 && y1 == 1, "the box stops at the map's edge");
Check(!NavPathQuery.TileBox(1e12, 0, 0, 0, 49, true, out _, out _, out _, out _), "a position far off the map: no box (and no overflow)");
}
private static void PathExOpenFloorAndInfo()
{
var q = QueryOn(Bake("exfloor", (0, 0, 192, 192)));
var r = Ex(q, 30, 96, 160, 96, 0);
Check(r.Code == RynthNavPathApiV1.ResultOk && !r.Partial, $"open ground: ResultOk ({r.Code}, {r.Note})");
Check(r.Corners.Count >= 2 && Flat(r.Corners[0], 30, 96) < 0.5 && Flat(r.Corners[^1], 160, 96) < 0.5, "start first, goal last");
Check(Math.Abs(r.Length - Legs(r)) < 1e-9 && Math.Abs(r.Length - 130) < 1, $"info[0]: the sum of the legs ({r.Length:F2})");
Check(r.EndDistance < 0.5, $"info[5]: at the goal ({r.EndDistance:F2})");
Check(r.Corners.TrueForAll(c => Math.Abs(c.Z) < 0.5), "heights from the mesh (the floor's)");
var up = Ex(q, 30, 96, 160, 96, 0, gz: 5);
Check(up.Code == RynthNavPathApiV1.ResultOk && Math.Abs(up.EndDistance - 5) < 0.6, $"info[5] is 3D: a goal 5 u above the ground is ~5 u away ({up.EndDistance:F2})");
var same = Ex(q, 50, 50, 50, 50, 0);
Check(same.Code == RynthNavPathApiV1.ResultOk && same.Corners.Count == 2 && same.Length < 1e-6, "start = goal: two corners, length 0");
var quiet = Ex(q, 30, 96, 160, 96, RynthNavPathApiV1.PathQuiet);
Check(quiet.Code == r.Code && quiet.Corners.Count == r.Corners.Count && Math.Abs(quiet.Length - r.Length) < 1e-9, "PathQuiet changes only the log");
}
private static void PathExRoundAPortal()
{
var q = QueryOn(Bake("exportal", (0, 0, 192, 192)));
var spots = new PortalAvoid();
spots.Set(new[] { new PortalAvoid.Spot(95, 96, PortalAvoid.LiveRadius, "Unwanted Portal") }, Array.Empty<PortalAvoid.Spot>());
var filter = new AvoidFilter(spots);
var over = Ex(q, 30, 96, 160, 96, 0, filter);
Check(over.Code == RynthNavPathApiV1.ResultOk && Closest(over, 95, 96) < 1 && over.Portals == 0, "without PathAvoidPortals it walks over the portal");
var round = Ex(q, 30, 96, 160, 96, RynthNavPathApiV1.PathAvoidPortals, filter);
double gap = Closest(round, 95, 96);
Console.WriteLine($" portal on the line: {over.Corners.Count} corners {over.Length:F1}u straight; avoided {round.Corners.Count} corners {round.Length:F1}u, closest {gap:F2}u, bent {round.Bent}");
Check(round.Code == RynthNavPathApiV1.ResultOk && gap >= PortalAvoid.LiveRadius - 0.01, $"PathAvoidPortals: keeps out of the circle ({gap:F2} u)");
Check(round.Portals == 1 && round.Corners.Count > over.Corners.Count && round.Length > over.Length, "bent round it, a little longer");
Check(Flat(round.Corners[^1], 160, 96) < 0.5 && Math.Abs(round.Length - Legs(round)) < 1e-9, "still to the goal; the length counts the bend");
Check(round.Corners.TrueForAll(c => double.IsFinite(c.Z) && Math.Abs(c.Z) < 0.5), "a bend corner gets the mesh's height");
var into = Ex(q, 30, 96, 95, 96, RynthNavPathApiV1.PathAvoidPortals, filter);
Check(into.Code == RynthNavPathApiV1.ResultOk && Flat(into.Corners[^1], 95, 96) < 1, "a goal inside a circle is still reached (its polygon is allowed)");
var empty = new AvoidFilter(new PortalAvoid());
Check(Ex(q, 30, 96, 160, 96, RynthNavPathApiV1.PathAvoidPortals, empty).Corners.Count == over.Corners.Count, "no portals near: the plain path");
}
private static void PathExPartial()
{
var q = QueryOn(Bake("exisland", (10, 10, 70, 180), (110, 10, 180, 180)));
var no = Ex(q, 40, 96, 150, 96, 0);
Check(no.Code == RynthNavPathApiV1.ResultNoPath && no.Corners.Count == 0, "the other island without PathAllowPartial: ResultNoPath (as FindPath)");
var part = Ex(q, 40, 96, 150, 96, RynthNavPathApiV1.PathAllowPartial);
Check(part.Code == RynthNavPathApiV1.ResultPartial && part.Partial, $"with it: ResultPartial ({part.Code}, {part.Note})");
if (part.Corners.Count == 0) return;
var end = part.Corners[^1];
Console.WriteLine($" partial: {part.Corners.Count} corners, ends at ({end.X:F1}, {end.Y:F1}), {part.EndDistance:F1}u from the goal");
Check(end.X > 60 && end.X < 70 && Math.Abs(end.Y - 96) < 8, $"it ends on the near island's edge, facing the goal ({end.X:F1}, {end.Y:F1})");
Check(Math.Abs(part.EndDistance - Math.Sqrt((150 - end.X) * (150 - end.X) + (96 - end.Y) * (96 - end.Y) + end.Z * end.Z)) < 1e-9 && part.EndDistance > 70,
$"info[5]: what's left to the goal ({part.EndDistance:F1})");
var all = Ex(q, 40, 96, 150, 96, RynthNavPathApiV1.PathAllowPartial | RynthNavPathApiV1.PathAvoidPortals | RynthNavPathApiV1.PathWallClearance | RynthNavPathApiV1.PathQuiet,
new AvoidFilter(new PortalAvoid()));
Check(all.Code == RynthNavPathApiV1.ResultPartial, "with every flag at once too");
var reach = Ex(q, 40, 96, 60, 150, RynthNavPathApiV1.PathAllowPartial);
Check(reach.Code == RynthNavPathApiV1.ResultOk && !reach.Partial, "a goal it can reach is ResultOk, flag or not");
}
private static void PathExNoMeshAndStartSnap()
{
var q = QueryOn(Bake("exsnap", (10, 10, 70, 180), (110, 10, 180, 180)));
var far = Ex(q, 40, 96, 2000, 2000, 0);
Check(far.Code == RynthNavPathApiV1.ResultNoMesh && far.Note.Contains("goal"), $"no tile at the goal: ResultNoMesh ({far.Note})");
var gap = Ex(q, 90, 96, 40, 96, 0);
Check(gap.Code == RynthNavPathApiV1.ResultNoMesh && gap.Note.Contains("start"), $"a start 20 u from any ground: ResultNoMesh ({gap.Note})");
var off = Ex(q, 73, 96, 40, 96, 0);
Check(off.Code == RynthNavPathApiV1.ResultOk && off.Corners[0].X < 69 && off.Corners[0].X > 60, $"a start a few yards off the mesh: the wide search finds the ground beside it ({(off.Corners.Count > 0 ? off.Corners[0].X : 0):F1})");
// A roof over part of a floor (3.5 u up: room to walk under it).
var rq = QueryOn(BakeLayers("exroof", (0, 0, 192, 192, 0f), (60, 60, 130, 130, 3.5f)));
var under = Ex(rq, 95, 95, 20, 95, 0, sz: 0.1);
Check(under.Code == RynthNavPathApiV1.ResultOk && Math.Abs(under.Corners[0].Z) < 1, $"a start under a roof starts on the floor ({(under.Corners.Count > 0 ? under.Corners[0].Z : double.NaN):F2})");
var onTop = Ex(rq, 95, 95, 100, 100, 0, sz: 3.6, gz: 3.6);
Check(onTop.Code == RynthNavPathApiV1.ResultOk && Math.Abs(onTop.Corners[0].Z - 3.5) < 1, "one standing on the roof starts on the roof");
var down = Ex(rq, 95, 95, 20, 95, RynthNavPathApiV1.PathAllowPartial, sz: 3.6, gz: 0);
Check(down.Code == RynthNavPathApiV1.ResultPartial, "and the roof has no way down: partial");
}
private static void PathExWallsAndBuffer()
{
// The wall: from the south of tile 0 to the south of tile 2, the only way is round the north.
var q = QueryOn(Bake("exwall", (0, 0, 192, 192), (192, 150, 384, 192), (384, 0, 576, 192)));
var bare = Ex(q, 100, 20, 480, 20, 0);
Check(bare.Code == RynthNavPathApiV1.ResultOk && bare.Corners.Count >= 4 && bare.Corners.Exists(c => c.Y > 150), $"round the wall through the north passage ({bare.Corners.Count} corners)");
Check(bare.Length > 380 * 1.3 && bare.Length < 380 * 1.5, $"the length goes north and back ({bare.Length:F0}u, 380u straight)");
var walls = Ex(q, 100, 20, 480, 20, RynthNavPathApiV1.PathWallClearance, clearance: 1.0);
Check(walls.Code == RynthNavPathApiV1.ResultOk && walls.Pushed > 0 && walls.Corners.Count == bare.Corners.Count, $"PathWallClearance: corners pushed off the walls ({walls.Pushed})");
int moved = 0; bool byClearance = true, away = true, onMesh = true;
var plain = new DtQueryDefaultFilter();
for (int i = 1; i + 1 < walls.Corners.Count && i < bare.Corners.Count; i++)
{
var a = bare.Corners[i]; var b = walls.Corners[i];
double d = Flat(a, b.X, b.Y);
onMesh &= NavPathQuery.OnMesh(q, plain, b.X, b.Y, (float)b.Z);
if (d < 0.01) continue;
moved++;
byClearance &= Math.Abs(d - 1.0) < 0.01;
// Away from the wall's own corner (the void is x 192..384, y 0..150).
double ca = Math.Min(Flat(a, 192, 150), Flat(a, 384, 150)), cb = Math.Min(Flat(b, 192, 150), Flat(b, 384, 150));
away &= cb > ca + 0.5;
}
Check(moved == walls.Pushed && byClearance, $"each moved corner moved the clearance, 1 u ({moved} moved)");
Check(away, "out from the wall it turns round, not into it");
Check(onMesh, "every corner on walkable ground");
var zero = Ex(q, 100, 20, 480, 20, RynthNavPathApiV1.PathWallClearance, clearance: 0);
Check(zero.Pushed == 0 && Math.Abs(zero.Length - bare.Length) < 1e-9, "a clearance of 0: nothing moves");
// More corners than the caller has room for.
Check(Ex(q, 100, 20, 480, 20, 0, maxCorners: 2).Code == RynthNavPathApiV1.ResultNoPath, "no room for the corners: ResultNoPath (FindPath's rule)");
var cut = Ex(q, 100, 20, 480, 20, RynthNavPathApiV1.PathAllowPartial, maxCorners: 3);
Check(cut.Code == RynthNavPathApiV1.ResultPartial && cut.Corners.Count == 3 && cut.Truncated && cut.EndDistance > 6,
$"with PathAllowPartial: the first stretch, partial ({cut.Corners.Count} corners, {cut.EndDistance:F0}u left)");
}
private static void RaycastOnTheMesh()
{
var open = QueryOn(Bake("rayfloor", (0, 0, 192, 192)));
Check(NavPathQuery.Raycast(open, 30, 96, 0, 160, 96, 0, out float t1) == RynthNavPathApiV1.RayClear && t1 >= 1, "open ground: clear");
Check(NavPathQuery.Raycast(open, 30, 96, 0, 30, 96, 0, out _) == RynthNavPathApiV1.RayClear, "no distance at all: clear");
var isl = QueryOn(Bake("rayisland", (10, 10, 70, 180), (110, 10, 180, 180)));
Check(NavPathQuery.Raycast(isl, 40, 96, 0, 150, 96, 0, out float t2) == RynthNavPathApiV1.RayBlocked && t2 > 0.2f && t2 < 0.4f,
$"across the gap: blocked at the island's edge (t {t2:F2})");
Check(NavPathQuery.Raycast(isl, 90, 96, 0, 40, 96, 0, out _) == RynthNavPathApiV1.ResultNoMesh, "a start off the mesh: ResultNoMesh");
Check(NavPathQuery.Raycast(isl, 40, 96, 0, 2000, 2000, 0, out _) == RynthNavPathApiV1.ResultNoMesh, "no mesh at the goal: ResultNoMesh");
var wall = QueryOn(Bake("raywall", (0, 0, 192, 192), (192, 150, 384, 192), (384, 0, 576, 192)));
Check(NavPathQuery.Raycast(wall, 100, 20, 0, 480, 20, 0, out _) == RynthNavPathApiV1.RayBlocked, "straight through the wall: blocked");
Check(NavPathQuery.Raycast(wall, 100, 20, 0, 100, 170, 0, out _) == RynthNavPathApiV1.RayClear, "north on open ground: clear");
Check(NavPathQuery.Raycast(wall, 100, 170, 0, 480, 170, 0, out _) == RynthNavPathApiV1.RayClear, "along the passage, over two tile seams: clear");
}
/// <summary>
/// The real map (read only, when it's installed): FindPathEx and Raycast round Holtburg on the tiles
/// a short query loads, a ring of goals at 40/80/150 u; the answers agree, and how long they take.
/// </summary>
/// <summary>
/// RynthAi's hunt into buildings (2026-10-25) walks to a door's step out (BuildingMap: 1.5 m out of
/// the door, at its foot) and asks RynthNav where the ground is there (a path from the step to
/// itself): within 3 yd across and 1.5 in height it is a door from the ground. The steps of
/// Holtburg's doors (A9B4, as BuildingMap finds them on the real dats) on the real tiles.
/// </summary>
private static void RealDoorsteps()
{
string dir = Environment.GetEnvironmentVariable("RYNTHNAV_NAVDATA") ?? @"C:\Games\RynthCore\NavData";
if (!File.Exists(Path.Combine(dir, "nav_A9B4.tile"))) { Console.WriteLine(" (no real NavData here: skipped)"); return; }
var nav = new DtNavMesh();
nav.Init(new DtNavMeshParams { orig = new RcVec3f(0, 0, 0), tileWidth = 192f, tileHeight = 192f, maxTiles = 64, maxPolys = 1 << 16 }, 6);
Check(NavPathQuery.TileBox(0xA9 * 192 + 96, 0xB4 * 192 + 96, 0xA9 * 192 + 96, 0xB4 * 192 + 96, NavPathQuery.DefaultMaxTiles, true, out int x0, out int y0, out int x1, out int y1), "A9B4's box");
for (int x = x0; x <= x1; x++)
for (int y = y0; y <= y1; y++)
{
uint lb = (uint)((x << 8) | y);
if (File.Exists(Path.Combine(dir, $"nav_{lb:X4}.tile"))) nav.AddTile(ReadTile(dir, lb), 0, 0, out _);
}
var q = new DtNavMeshQuery(nav);
// (door cell, step out x, y, z): every A9B4 door BuildingMap offers (its log, 2026-10-25).
var steps = new (string Cell, double X, double Y, double Z)[]
{
("A9B40102", 32537.1, 34698.0, 67.0), ("A9B40107", 32527.6, 34700.5, 66.8), ("A9B4010B", 32519.6, 34691.5, 66.0),
("A9B40113", 32474.7, 34714.7, 66.0), ("A9B40114", 32473.7, 34721.9, 67.0), ("A9B40115", 32482.7, 34714.6, 66.0),
("A9B40116", 32598.0, 34688.1, 67.0), ("A9B40118", 32595.5, 34690.6, 66.0), ("A9B40119", 32608.8, 34695.0, 66.0),
("A9B4011F", 32560.2, 34695.8, 70.0), ("A9B40120", 32555.4, 34702.6, 67.0), ("A9B40122", 32549.0, 34702.6, 67.0),
("A9B40124", 32560.2, 34693.4, 66.0), ("A9B40125", 32552.2, 34702.6, 66.0), ("A9B4012B", 32499.3, 34694.9, 66.0),
("A9B4012C", 32505.3, 34701.4, 66.0), ("A9B40133", 32506.9, 34701.4, 70.0), ("A9B40139", 32575.2, 34719.3, 66.0),
("A9B4013D", 32585.8, 34708.7, 66.0), ("A9B4014C", 32577.6, 34578.6, 95.0), ("A9B4014F", 32587.4, 34581.0, 94.8),
("A9B40150", 32580.5, 34578.6, 94.0), ("A9B40164", 32568.0, 34596.0, 94.0), ("A9B40165", 32544.0, 34596.0, 94.0),
("A9B4016C", 32522.7, 34592.7, 94.0), ("A9B4016D", 32521.7, 34599.9, 95.0), ("A9B4016E", 32530.7, 34592.6, 94.0),
("A9B40170", 32603.4, 34577.5, 94.0), ("A9B40177", 32508.8, 34716.5, 66.2), ("A9B40179", 32603.9, 34595.0, 96.0),
("A9B4017F", 32605.3, 34667.5, 66.0), ("A9B40184", 32478.7, 34614.6, 78.0), ("A9B40189", 32556.1, 34649.0, 80.0),
};
int flags = RynthNavPathApiV1.PathWallClearance | RynthNavPathApiV1.PathAllowPartial | RynthNavPathApiV1.PathQuiet;
var r = new PathExResult();
int ground = 0;
var off = new List<string>();
foreach (var (cell, x, y, z) in steps)
{
int code = NavPathQuery.FindPathEx(q, x, y, z, x, y, z, flags, 64, null, 1.0, r);
bool ok = code is RynthNavPathApiV1.ResultOk or RynthNavPathApiV1.ResultPartial && r.Corners.Count > 0;
double flat = 99, dz = 99;
if (ok)
{
var c = r.Corners[^1];
flat = Math.Sqrt((c.X - x) * (c.X - x) + (c.Y - y) * (c.Y - y));
dz = c.Z - z;
}
if (ok && flat <= 3 && Math.Abs(dz) <= 1.5) ground++;
else off.Add($"{cell} ({(ok ? $"{flat:F1} across, {dz:+0.0;-0.0} high" : $"code {code}")})");
}
Console.WriteLine($" A9B4 doorsteps on RynthNav's ground: {ground} of {steps.Length}; not: {string.Join(", ", off)}");
// 2026-10-25: 32 of 33; A9B4011F's step is 3.8 above the ground (an upper door with no floor under its step).
Check(ground >= 30, $"Holtburg's door steps are on the ground ({ground} of {steps.Length})");
Check(off.Any(o => o.StartsWith("A9B4011F")), "the upper door A9B4011F's isn't (the hunt leaves it alone)");
}
private static void RealPathEx()
{
string dir = Environment.GetEnvironmentVariable("RYNTHNAV_NAVDATA") ?? @"C:\Games\RynthCore\NavData";
if (!File.Exists(Path.Combine(dir, "nav_A9B4.tile"))) { Console.WriteLine(" (no real NavData here: skipped)"); return; }
var centre = RynthCore.Plugin.RynthNav.WorldPt.FromMap(42.08, 33.6);
var sw = System.Diagnostics.Stopwatch.StartNew();
var nav = new DtNavMesh();
var prm = new DtNavMeshParams { orig = new RcVec3f(0, 0, 0), tileWidth = 192f, tileHeight = 192f, maxTiles = 64, maxPolys = 1 << 16 };
nav.Init(prm, 6);
int loaded = 0;
Check(NavPathQuery.TileBox(centre.X, centre.Y, centre.X, centre.Y, NavPathQuery.DefaultMaxTiles, true, out int x0, out int y0, out int x1, out int y1), "Holtburg's box");
for (int x = x0; x <= x1; x++)
for (int y = y0; y <= y1; y++)
{
uint lb = (uint)((x << 8) | y);
if (!File.Exists(Path.Combine(dir, $"nav_{lb:X4}.tile"))) continue;
if (nav.AddTile(ReadTile(dir, lb), 0, 0, out _).Succeeded()) loaded++;
}
long loadMs = sw.ElapsedMilliseconds;
var q = new DtNavMeshQuery(nav);
// The start: walkable ground at the town's centre (it's under a roof in places; the snap finds the ground).
var plain = new DtQueryDefaultFilter();
q.FindNearestPoly(new RcVec3f((float)centre.X, 0, (float)centre.Y), new RcVec3f(30, 600, 30), plain, out long sRef, out RcVec3f s0, out _);
Check(sRef != 0, "ground at Holtburg's centre");
if (sRef == 0) return;
double sx = s0.X, sy = s0.Z, sz = s0.Y;
int flags = RynthNavPathApiV1.PathWallClearance | RynthNavPathApiV1.PathAllowPartial | RynthNavPathApiV1.PathQuiet;
var counts = new Dictionary<int, int>();
int rays = 0, clear = 0, agree = 0, compared = 0, queries = 0;
double worst = 1, pathMs = 0, rayMs = 0;
bool infoOk = true;
var r = new PathExResult();
var paths = new List<(double gx, double gy, double gz, double len)>();
foreach (double dist in new[] { 40.0, 80.0, 150.0 })
for (int k = 0; k < 8; k++)
{
double a = k * Math.PI / 4;
double gx = sx + Math.Cos(a) * dist, gy = sy + Math.Sin(a) * dist;
// A goal on the ground there (a monster stands on it), else the raw spot.
q.FindNearestPoly(new RcVec3f((float)gx, (float)sz, (float)gy), new RcVec3f(2, 64, 2), plain, out long gr, out RcVec3f gp, out _);
double gz = gr != 0 ? gp.Y : sz;
long t0 = System.Diagnostics.Stopwatch.GetTimestamp();
int code = NavPathQuery.FindPathEx(q, sx, sy, sz, gx, gy, gz, flags, 64, null, 1.0, r);
pathMs += System.Diagnostics.Stopwatch.GetElapsedTime(t0).TotalMilliseconds; queries++;
counts[code] = counts.GetValueOrDefault(code) + 1;
if (code is RynthNavPathApiV1.ResultOk or RynthNavPathApiV1.ResultPartial)
{
infoOk &= Math.Abs(r.Length - Legs(r)) < 1e-6 && r.Corners.Count >= 2 && r.Corners.Count <= 64
&& (code == RynthNavPathApiV1.ResultPartial) == r.Partial;
if (code == RynthNavPathApiV1.ResultOk) paths.Add((gx, gy, gz, r.Length));
}
t0 = System.Diagnostics.Stopwatch.GetTimestamp();
int ray = NavPathQuery.Raycast(q, sx, sy, sz, gx, gy, gz, out _);
rayMs += System.Diagnostics.Stopwatch.GetElapsedTime(t0).TotalMilliseconds; rays++;
if (ray == RynthNavPathApiV1.RayClear)
{
clear++;
// A clear straight walk: the planned path is (about) the straight line.
if (code == RynthNavPathApiV1.ResultOk)
{
compared++;
double straight = Math.Sqrt((r.Corners[^1].X - sx) * (r.Corners[^1].X - sx) + (r.Corners[^1].Y - sy) * (r.Corners[^1].Y - sy));
double ratio = straight < 1 ? 1 : r.Length / straight;
worst = Math.Max(worst, ratio);
if (ratio < 1.25) agree++;
else Console.WriteLine($" clear ray, path x{ratio:F2}: goal ({gx - sx:F0},{gy - sy:F0}) from the start, {r.Corners.Count} corners, {r.Polys} polys, pushed {r.Pushed}: " + string.Join(" ", r.Corners.Select(c => $"({c.X - sx:F1},{c.Y - sy:F1},{c.Z - sz:F1})")));
}
}
}
// Portals: one on the way of each of the longer paths, kept out of (or the plain path when there's no way round).
int portalTried = 0, keptOut = 0, fellBack = 0;
foreach (var p in paths)
{
if (p.len < 30) continue;
NavPathQuery.FindPathEx(q, sx, sy, sz, p.gx, p.gy, p.gz, 0, 64, null, 1.0, r);
if (r.Corners.Count < 2) continue;
var mid = (X: (r.Corners[0].X + r.Corners[1].X) / 2, Y: (r.Corners[0].Y + r.Corners[1].Y) / 2);
if (Flat(r.Corners[0], mid.X, mid.Y) < PortalAvoid.LiveRadius + 3) continue; // the start would be inside it
var spots = new PortalAvoid();
spots.Set(new[] { new PortalAvoid.Spot(mid.X, mid.Y, PortalAvoid.LiveRadius, "test portal") }, Array.Empty<PortalAvoid.Spot>());
int code = NavPathQuery.FindPathEx(q, sx, sy, sz, p.gx, p.gy, p.gz, flags | RynthNavPathApiV1.PathAvoidPortals, 64, new AvoidFilter(spots), 1.0, r);
if (code is not (RynthNavPathApiV1.ResultOk or RynthNavPathApiV1.ResultPartial)) continue;
portalTried++;
if (Closest(r, mid.X, mid.Y) >= PortalAvoid.LiveRadius - 0.01) keptOut++;
if (r.FellBack) fellBack++;
}
// A typical short query, many times (the tiles already loaded, as in a hunt).
var pick = paths.Count > 0 ? paths[0] : (sx + 40, sy, sz, 0.0);
int reps = 500;
long tr = System.Diagnostics.Stopwatch.GetTimestamp();
for (int i = 0; i < reps; i++) NavPathQuery.FindPathEx(q, sx, sy, sz, pick.Item1, pick.Item2, pick.Item3, flags | RynthNavPathApiV1.PathAvoidPortals, 64, null, 1.0, r);
double perPath = System.Diagnostics.Stopwatch.GetElapsedTime(tr).TotalMilliseconds / reps;
tr = System.Diagnostics.Stopwatch.GetTimestamp();
for (int i = 0; i < reps; i++) NavPathQuery.Raycast(q, sx, sy, sz, pick.Item1, pick.Item2, pick.Item3, out _);
double perRay = System.Diagnostics.Stopwatch.GetElapsedTime(tr).TotalMilliseconds / reps;
string tally = string.Join(", ", counts.OrderBy(kv => -kv.Key).Select(kv => $"{(kv.Key switch { 1 => "ok", 2 => "partial", 0 => "no path", _ => "no mesh" })} {kv.Value}"));
Console.WriteLine($" Holtburg: {loaded} tiles read in {loadMs} ms; {queries} FindPathEx ({tally}), {pathMs / queries:F2} ms each cold; "
+ $"Raycast {clear}/{rays} clear, {rayMs / rays:F3} ms each; clear rays vs paths {agree}/{compared} near-straight (worst x{worst:F2}); "
+ $"portal on the way: kept out {keptOut}/{portalTried} ({fellBack} plain fallback); a short query {perPath:F3} ms, its ray {perRay:F3} ms");
Check(loaded >= 9, $"Holtburg's 3x3 tiles ({loaded})");
Check(counts.GetValueOrDefault(RynthNavPathApiV1.ResultOk) >= 12, "most of the ring is reached");
Check(infoOk, "info consistent with the corners on every path");
Check(compared == 0 || agree >= compared - 1, $"a clear ray's path is the straight line (or near it): {agree}/{compared}");
Check(portalTried == 0 || keptOut + fellBack >= portalTried, $"a portal on the way is kept out of, or the plain path is taken ({keptOut}+{fellBack}/{portalTried})");
Check(perPath < 20 && perRay < 5, $"fast enough to ask several times a second ({perPath:F3} / {perRay:F3} ms)");
}
// ── The navmesh view (NavMeshView) ───────────────────────────────────────────
private sealed class TriSink : RynthCore.Plugin.Shared.INav3DSink