using System;
using System.Collections.Generic;
using DotRecast.Core.Numerics;
using DotRecast.Detour;
using RynthCore.Plugin.Shared;
using RynthNav.Routing;
// 12-03, the Yanshi Thread (B470): the hunt stood for minutes in building #3's doorway with thirty monsters
// round it, every one "left out" (the walk "can't be reached" or ending 60-100 yd short); two steps back and it
// hunted again. The doorstep is a crumb of the mesh: three polygons joined to nothing, the street 1.1 u away.
// A walk from a crumb steps off it first; a walk to one steps onto it last (NavPathQuery.CrumbOf/StepOffCrumb).
internal static partial class Program
{
private static void RunStartSnaps()
{
Run(nameof(CrumbStepOffMadeUp), CrumbStepOffMadeUp);
Run(nameof(RealDoorwayStart), RealDoorwayStart);
}
/// A small island beside a big floor: the island is a crumb, the floor isn't, and the step off finds the floor.
private static void CrumbStepOffMadeUp()
{
// A big floor of uneven slabs (so it's many polygons) and a 9x9 island 6 u east of it.
var slabs = new List<(float, float, float, float, float)>();
for (int i = 0; i < 6; i++)
for (int j = 0; j < 6; j++)
slabs.Add((10 + i * 20, 10 + j * 20, 30 + i * 20, 30 + j * 20, ((i + j) % 3) * 0.3f));
slabs.Add((136, 60, 145, 69, 0f));
var q = QueryOn(BakeLayers("excrumb", slabs.ToArray()));
var filter = new DtQueryDefaultFilter();
q.FindNearestPoly(new RcVec3f(140.5f, 0, 64.5f), new RcVec3f(2, 2, 2), filter, out long island, out RcVec3f ip, out _);
q.FindNearestPoly(new RcVec3f(70, 0, 70), new RcVec3f(2, 2, 2), filter, out long floor, out _, out _);
Check(island != 0 && floor != 0, "both pieces baked");
if (island == 0 || floor == 0) return;
HashSet? crumb = NavPathQuery.CrumbOf(q, island);
Check(crumb != null && crumb.Count <= NavPathQuery.CrumbMaxPolys, $"the island is a crumb ({crumb?.Count} polygons)");
Check(NavPathQuery.CrumbOf(q, floor) == null, "the big floor isn't");
if (crumb == null) return;
long none = NavPathQuery.StepOffCrumb(q, ip, crumb, filter, out _);
Check(none == 0, $"no bigger ground within {NavPathQuery.CrumbStepUnits} u: nowhere to step");
var far = Ex(q, 140.5, 64.5, 70, 70, 0);
Check(far.Code == RynthNavPathApiV1.ResultNoPath && far.SteppedOff == 0, $"and the walk off it can't be made, as before ({far.Code}, {far.Note})");
long off = NavPathQuery.StepOffCrumb(q, ip, crumb, filter, out RcVec3f op, 16f);
Check(off != 0 && op.X < 132 && op.X > 120, $"within 16 u: the floor's edge facing it ({op.X:F1}, {op.Z:F1})");
Check(off == 0 || NavPathQuery.CrumbOf(q, off) == null, "on the big floor, not on another crumb");
}
private static void RealDoorwayStart()
{
string dir = Environment.GetEnvironmentVariable("RYNTHNAV_NAVDATA") ?? @"C:\Games\RynthCore\NavData";
DtNavMeshQuery? q = Around(dir, 0xB470);
if (q == null) { Console.WriteLine(" (no real NavData here: skipped)"); return; }
var filter = new DtQueryDefaultFilter();
// Where the character stood (RynthRemote's status, 16:08): cell B4700019, the door at 34633,21527.
double e = 34633.11, n = 21527.03, z = 42.09;
long s = NavPathQuery.SnapStart(q, new RcVec3f((float)e, (float)z, (float)n), filter, out RcVec3f sPt);
double flat = Math.Sqrt((sPt.X - e) * (sPt.X - e) + (sPt.Z - n) * (sPt.Z - n));
Console.WriteLine($" the start snaps to {sPt.X:F1},{sPt.Z:F1} height {sPt.Y:F1} ({flat:F1} u across, {sPt.Y - z:+0.0;-0.0} u up)");
Check(s != 0 && flat < 1 && Math.Abs(sPt.Y - z) < 1, "the walk's start is the ground at the character's feet");
HashSet? crumb = NavPathQuery.CrumbOf(q, s);
Console.WriteLine($" its piece of the mesh: {(crumb == null ? "joined" : crumb.Count + " polygon(s)")}");
Check(crumb != null, "the doorstep is a crumb");
if (crumb == null) return;
long off = NavPathQuery.StepOffCrumb(q, sPt, crumb, filter, out RcVec3f op);
double step = Math.Sqrt((op.X - sPt.X) * (op.X - sPt.X) + (op.Z - sPt.Z) * (op.Z - sPt.Z));
Console.WriteLine($" steps off to {op.X:F1},{op.Z:F1} h {op.Y:F1}, {step:F1} u across");
Check(off != 0 && step < 2 && Math.Abs(op.Y - sPt.Y) < 1, "the street is a short step away, at the same height");
// Every walk from the doorway across the town (as the hunt asks: start, goal at the character's height).
foreach (var (ge, gn, name) in new[] { (34605.2, 21514.1, "town middle"), (34633.0, 21550.0, "23 u north"), (34633.0, 21505.0, "22 u south"),
(34660.0, 21527.0, "27 u east"), (34610.0, 21527.0, "23 u west"), (34580.0, 21600.0, "far NW") })
{
var r = Ex(q, e, n, ge, gn, 0, sz: z, gz: z);
Console.WriteLine($" to {name}: code {r.Code}, {r.Corners.Count} corners, {r.Length:F0} u, ends {r.EndDistance:F1} u from it, steps off {r.SteppedOff:F1} u");
Check(r.Code == RynthNavPathApiV1.ResultOk && r.EndDistance < 1, $"to the {name}: the walk is made ({r.Code}, {r.Note})");
if (r.Corners.Count < 2) continue;
Check(r.SteppedOff > 0 && r.SteppedOff <= NavPathQuery.CrumbStepUnits, $"to the {name}: it steps off the crumb ({r.SteppedOff:F1} u)");
Check(Flat(r.Corners[0], sPt.X, sPt.Z) < 0.1 && Flat(r.Corners[1], op.X, op.Z) < 0.1,
$"to the {name}: it starts where the character stands, then the step off");
}
// With the hunt's flags (portals avoided, walls kept off, partial allowed).
var hunt = Ex(q, e, n, 34605.2, 21514.1, RynthNavPathApiV1.PathAllowPartial | RynthNavPathApiV1.PathAvoidPortals | RynthNavPathApiV1.PathWallClearance,
new AvoidFilter(new PortalAvoid()), sz: z, gz: z);
Check(hunt.Code == RynthNavPathApiV1.ResultOk && hunt.SteppedOff > 0, $"with the hunt's flags too ({hunt.Code}, {hunt.Note})");
// The other way: a walk to the doorstep steps onto it last.
var back = Ex(q, 34605.2, 21514.1, e, n, 0, sz: z, gz: z);
Console.WriteLine($" town middle to the doorstep: code {back.Code}, {back.Length:F0} u, ends {back.EndDistance:F1} u from it, last leg {back.GoalSteppedOff:F1} u");
Check(back.Code == RynthNavPathApiV1.ResultOk && back.GoalSteppedOff > 0 && back.EndDistance < 1,
$"to the doorstep: the walk ends on it ({back.Code}, {back.EndDistance:F1} u short, {back.Note})");
Check(back.Corners.Count >= 2 && Flat(back.Corners[^2], op.X, op.Z) < 2.5, "its last leg is the short step from the street");
// From the street the walk is as it always was.
var street = Ex(q, 34631.0, 21527.0, 34605.2, 21514.1, 0, sz: z, gz: z);
long sw = NavPathQuery.SnapStart(q, new RcVec3f(34631.0f, (float)z, 21527.0f), filter, out _);
Console.WriteLine($" from 2 u west: code {street.Code}, {street.Length:F0} u, steps off {street.SteppedOff:F1} u (its piece {(NavPathQuery.CrumbOf(q, sw) == null ? "joined" : "a crumb")})");
Check(street.Code == RynthNavPathApiV1.ResultOk, "from two steps back the walk is made");
if (NavPathQuery.CrumbOf(q, sw) == null) Check(street.SteppedOff == 0, "and, starting on the joined ground, it steps off nothing");
}
}