92 lines
5.4 KiB
C#
92 lines
5.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using RynthCore.Plugin.RynthAi;
|
|
using RynthCore.Plugin.RynthAi.LegacyUi;
|
|
using RynthCore.Plugin.RynthAi.Raycasting;
|
|
using RynthCore.RynthAiTests.Harness;
|
|
|
|
namespace RynthCore.RynthAiHostTests.Tests;
|
|
|
|
// The explore's walk straightened (DungeonPathfinder.Straighten.cs, 2026-11-15). DreamWeave Thread
|
|
// 0x00ED: its zig-zag corridors are cut into many 3-5 cell rooms, the walk had a point at every room
|
|
// boundary (334 points, 182 of them at openings) and the character turned every 2.5-5 m. On the real
|
|
// dats: far fewer points, every leg walkable, and every room the walk was laid into still crossed.
|
|
internal static class StraightenTests
|
|
{
|
|
public static void Register(Runner r)
|
|
{
|
|
r.Add("straighten: Thread 0x00ED's explore on the real dats has far fewer turns, every leg walkable, every room still crossed (if the dats are here)", Real00ED);
|
|
}
|
|
|
|
private static void Real00ED()
|
|
{
|
|
string acDir = DunRouteTests.AcDir;
|
|
if (!Directory.Exists(acDir)) { Console.WriteLine(" (no dats on this PC: skipped)"); return; }
|
|
var loader = new GeometryLoader();
|
|
if (!loader.Initialize(acDir)) { Console.WriteLine(" (geometry loader failed: " + loader.StatusMessage + ")"); return; }
|
|
try
|
|
{
|
|
const uint lb = 0x00ED, start = 0x00ED0350;
|
|
DungeonPathfinder.InvalidateCache();
|
|
var graph = DungeonPathfinder.GetGraph(lb, loader.CellDat);
|
|
var geo = DungeonPathfinder.GetGeometry(lb, loader.CellDat, loader.DungeonLOS)!;
|
|
Check.NotNull(geo, "geometry");
|
|
// Where the character arrives (local 180, -330), and the Thread Exit beside it (as logged 2026-11-15).
|
|
double sx = 180.0, sy = 0xED * 192.0 - 330.0;
|
|
var exit = new PatrolPortal { Id = 0x700ED032, Name = "Thread Exit", Cell = 0x00ED0351, X = 180.0, Y = 45160.0, Z = -0.1 };
|
|
// As laid, for comparison: the rooms that walk crosses.
|
|
DungeonPathfinder.StraightenEnabled = false;
|
|
var laid = DungeonPathfinder.BuildExploreRoute(graph, start, null, geo, null, new[] { exit }, (sx, sy, 0), null, out var laidPlan);
|
|
DungeonPathfinder.StraightenEnabled = true;
|
|
DungeonPathfinder.MainRouteNote = null;
|
|
var route = DungeonPathfinder.BuildExploreRoute(graph, start, null, geo, null, new[] { exit }, (sx, sy, 0), null, out var plan);
|
|
var pts = route.Points;
|
|
Console.WriteLine($" {pts.Count} points (laid in game before: 334), {plan.Order.Count} rooms in order; {DungeonPathfinder.MainRouteNote}");
|
|
// Against the same walk laid unstraightened, waits left out. (2026-11-16: the explore walks into every dead
|
|
// end and keeps those middles, so it has more points than the 334 of 2026-11-15.)
|
|
int moves = pts.Count(p => p.Type == NavPointType.Point), laidMoves = laid.Points.Count(p => p.Type == NavPointType.Point);
|
|
Console.WriteLine($" {moves} moving points straightened, {laidMoves} as laid");
|
|
Check.True(moves > 50 && moves < laidMoves * 0.85, $"far fewer points ({moves}, {laidMoves} as laid)");
|
|
|
|
double X(NavPoint p) => DungeonGeometry.NavToWorld(p.EW);
|
|
double Y(NavPoint p) => DungeonGeometry.NavToWorld(p.NS);
|
|
int bad = 0;
|
|
for (int i = 0; i + 1 < pts.Count; i++)
|
|
if (geo.LegClearance(X(pts[i]), Y(pts[i]), pts[i].Z * 240.0, X(pts[i + 1]), Y(pts[i + 1]), pts[i + 1].Z * 240.0, 1.0, 0.5, out _, out _, out _) < 0.2) bad++;
|
|
Check.Eq(bad, 0, "every leg on walkable floor, off the walls");
|
|
|
|
// Every room the walk was laid into is crossed by it (sampled every 0.5 m) - as the walk as laid did.
|
|
HashSet<int> Crossed(List<NavPoint> pts)
|
|
{
|
|
var crossed = new HashSet<int>();
|
|
for (int i = 0; i + 1 < pts.Count; i++)
|
|
{
|
|
double x0 = X(pts[i]), y0 = Y(pts[i]), z0 = pts[i].Z * 240.0, x1 = X(pts[i + 1]), y1 = Y(pts[i + 1]), z1 = pts[i + 1].Z * 240.0;
|
|
int steps = Math.Max(1, (int)Math.Ceiling(Math.Sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0)) / 0.5));
|
|
double zf = z0;
|
|
for (int s = 0; s <= steps; s++)
|
|
{
|
|
double t = (double)s / steps, x = x0 + (x1 - x0) * t, y = y0 + (y1 - y0) * t;
|
|
if (geo.TryFloorHeight(x, y, zf, 0.6, out double h)) zf = h;
|
|
foreach (var n in graph.Values)
|
|
if (geo.OnCellFloor(n.CellId, x, y, zf)) { int r = plan.RoomOf(n.CellId); if (r >= 0) crossed.Add(r); break; }
|
|
}
|
|
}
|
|
return crossed;
|
|
}
|
|
var before = Crossed(laid.Points);
|
|
var after = Crossed(pts);
|
|
var laidMissed = plan.Order.Where(r => !before.Contains(r)).ToList();
|
|
var missed = plan.Order.Where(r => !after.Contains(r) && before.Contains(r)).ToList();
|
|
Console.WriteLine($" as laid: {laid.Points.Count} points, rooms not crossed {laidMissed.Count} ({string.Join(",", laidMissed.Take(10))})");
|
|
Check.Eq(missed.Count, 0, $"no room the laid walk crossed is left out ({missed.Count}: {string.Join(",", missed.Take(10))})");
|
|
}
|
|
finally
|
|
{
|
|
loader.Dispose();
|
|
DungeonPathfinder.InvalidateCache();
|
|
}
|
|
}
|
|
}
|