RynthSuite/Tools/RynthCore.RynthAiHostTests/Tests/NavHeadingTests.cs

135 lines
7 KiB
C#

using System;
using RynthCore.Plugin.RynthAi;
using RynthCore.Plugin.RynthAi.LegacyUi;
using RynthCore.RynthAiTests.Harness;
namespace RynthCore.RynthAiHostTests.Tests;
// /ra navheading walks round what's in the way (owner, 2026-11-06: "nav route is not navving around
// obstacles"): RynthNav's corners become a Once route, in nav coordinates, doorways kept.
internal static class NavHeadingTests
{
public static void Register(Runner r)
{
r.Add("navheading: RynthNav's corners become a Once route in nav coordinates, doorways kept", CornersToRoute);
r.Add("navheading: a step that goes takes no detour", PlainStepGoes);
r.Add("navheading: a goal in a rock or cliff face: the same heading, closer", GoalOffMapPulledBack);
r.Add("navheading: no walkable ground ahead at any distance: the way round at 45°", OffMapAheadGoesRound);
r.Add("navheading: no way to the goal (NoPath): closer, not a straight line", NoPathPulledBack);
r.Add("navheading: nothing walkable on any goal: straight, saying why", NothingWalkable);
r.Add("navheading: RynthNav not loaded: straight at once", RynthNavMissing);
r.Add("navheading: a blocked step keeps to the side the last one took", KeepsSide);
}
// A fake RynthNav for PlanHeading: `walkable(goal)` says Ok (a straight path to it), NoMesh, NoPath, or a short
// Partial path that gets nowhere (blocked). Heading 0 = north (+y), 90 = east (+x); the character stands at 0,0.
private static readonly OutdoorHunt.Vec At = new(0, 0, 0);
private static double Yd(OutdoorHunt.Vec v) => Math.Sqrt(v.X * v.X + v.Y * v.Y) / 1.0;
private static double BearingOf(OutdoorHunt.Vec v)
{
double b = Math.Atan2(v.X, v.Y) * 180 / Math.PI;
return b < 0 ? b + 360 : b;
}
private static OutdoorHunt.PathResult Ok(OutdoorHunt.Vec goal) =>
new(OutdoorHunt.PathStatus.Ok, new[] { goal }, Yd(goal), 0);
private static OutdoorHunt.PathResult Fail(OutdoorHunt.PathStatus s) =>
new(s, Array.Empty<OutdoorHunt.Vec>(), 0, 0);
private static OutdoorHunt.PathResult Stuck() =>
new(OutdoorHunt.PathStatus.Partial, new[] { new OutdoorHunt.Vec(0, 1, 0) }, 1, 0);
// PlanHeading works in world units; the tests treat them as yards (only ratios and bearings matter).
private static void PlainStepGoes()
{
var plan = RynthAiPlugin.PlanHeading(At, 0, 50, 0, g => Ok(g));
Check.True(plan.Found, "found");
Check.True(plan.Detour == "", $"no detour note, got '{plan.Detour}'");
Check.True(Math.Abs(Yd(plan.Path.Corners[^1]) - 50) < 0.01, "the full 50");
Check.True(plan.Side == 0, "no side taken");
}
private static void GoalOffMapPulledBack()
{
// A rock face from 40 yd on: goals past it are off the mesh.
int calls = 0;
var plan = RynthAiPlugin.PlanHeading(At, 0, 50, 0, g => { calls++; return Yd(g) > 40 ? Fail(OutdoorHunt.PathStatus.NoMesh) : Ok(g); });
Check.True(plan.Found, "found");
Check.True(Math.Abs(Yd(plan.Path.Corners[^1]) - 30) < 0.01, $"30 yd (60% of 50), got {Yd(plan.Path.Corners[^1]):0.0}");
Check.True(Math.Abs(BearingOf(plan.Path.Corners[^1])) < 0.01, "still north");
Check.True(plan.Detour.Contains("off walkable ground"), $"says why, got '{plan.Detour}'");
Check.True(calls == 2, $"the 50 yd goal, then 30 (calls {calls})");
}
private static void OffMapAheadGoesRound()
{
// A cliff ahead (north, any distance) and to the left: only the north-east is walkable.
var plan = RynthAiPlugin.PlanHeading(At, 0, 50, 0, g =>
{
double b = BearingOf(g);
return b > 30 && b < 60 ? Ok(g) : Fail(OutdoorHunt.PathStatus.NoMesh);
});
Check.True(plan.Found, "found a way round");
Check.True(Math.Abs(BearingOf(plan.Path.Corners[^1]) - 45) < 0.01, "45° right");
Check.True(plan.Detour.Contains("45° right"), $"says so, got '{plan.Detour}'");
Check.True(plan.Side == 1, "the right side is kept from now on");
}
private static void NoPathPulledBack()
{
var plan = RynthAiPlugin.PlanHeading(At, 90, 50, 0, g => Yd(g) > 20 ? Fail(OutdoorHunt.PathStatus.NoPath) : Ok(g));
Check.True(plan.Found, "found");
Check.True(Math.Abs(Yd(plan.Path.Corners[^1]) - 15) < 0.01, $"15 yd (30% of 50), got {Yd(plan.Path.Corners[^1]):0.0}");
Check.True(Math.Abs(BearingOf(plan.Path.Corners[^1]) - 90) < 0.01, "still east");
}
private static void NothingWalkable()
{
int calls = 0;
var plan = RynthAiPlugin.PlanHeading(At, 0, 50, 0, g => { calls++; return Fail(OutdoorHunt.PathStatus.NoMesh); });
Check.True(!plan.Found, "nothing found");
Check.True(plan.Straight.Contains("no walkable ground"), $"says why, got '{plan.Straight}'");
Check.True(calls > 7, $"every goal tried, closer ones too (calls {calls})");
}
private static void RynthNavMissing()
{
int calls = 0;
var plan = RynthAiPlugin.PlanHeading(At, 0, 50, 0, g => { calls++; return Fail(OutdoorHunt.PathStatus.Unavailable); });
Check.True(!plan.Found, "nothing found");
Check.True(plan.Straight.Contains("isn't loaded"), $"says why, got '{plan.Straight}'");
Check.True(calls == 1, $"asked once (calls {calls})");
}
private static void KeepsSide()
{
// Ahead is blocked at every distance (a path that gets nowhere); both 45° sides are open. The last detour
// went left: this one goes left too.
Func<OutdoorHunt.Vec, OutdoorHunt.PathResult> nav = g =>
{
double b = BearingOf(g);
return (b < 1 || b > 359) ? Stuck() : Ok(g);
};
var left = RynthAiPlugin.PlanHeading(At, 0, 50, -1, nav);
Check.True(left.Found && Math.Abs(BearingOf(left.Path.Corners[^1]) - 315) < 0.01, "45° left, as the last step");
Check.True(left.Side == -1, "left kept");
var none = RynthAiPlugin.PlanHeading(At, 0, 50, 0, nav);
Check.True(none.Found && Math.Abs(BearingOf(none.Path.Corners[^1]) - 45) < 0.01, "no side yet: the first way round (right)");
}
private static double World(double navUnits) => (navUnits * 10.0 + 1019.5) * 24.0;
private static void CornersToRoute()
{
var corners = new[]
{
new OutdoorHunt.Vec(World(56.0), World(42.4), 240.0),
new OutdoorHunt.Vec(World(56.1), World(42.5), 480.0, Door: true),
};
var route = RynthAiPlugin.HeadingRoute(corners);
Check.True(route.RouteType == NavRouteType.Once, "a Once route");
Check.True(route.Points.Count == 2, "one point per corner");
Check.True(Math.Abs(route.Points[0].EW - 56.0) < 1e-9 && Math.Abs(route.Points[0].NS - 42.4) < 1e-9, "x is EW, y is NS");
Check.True(Math.Abs(route.Points[1].Z - 2.0) < 1e-9, "z in 240-yard units");
Check.True(!route.Points[0].Doorway && route.Points[1].Doorway, "doorway corners stay doorways");
Check.True(route.Points[0].Type == NavPointType.Point, "plain points");
}
}