Files
Neutron/tests/GamePort.PatrolTests/Program.cs
T

256 lines
14 KiB
C#

using System.Buffers.Binary;
using System.Numerics;
using System.Text;
using GamePort.Assets;
using GamePort.Game;
var original = Path.GetFullPath(args.Length == 1 ? args[0] : "../Original");
{
var inputs = new List<Vector3>();
var rig = Rig([Point("A", new(500, 0, 0), "none")], body: (state, dt) =>
{
inputs.Add(state.Velocity);
return FreeBody(state, dt);
});
rig.Patrol.Step(.1f);
Near(rig.Actor.Position, Vector3.Zero, "First step must consume initial zero velocity");
Near(rig.Patrol.Velocity, new(100, 0, 0), "Next velocity selected after body step");
NearFloat(rig.Actor.Rotation.Y, 0, "First step consumes prior heading");
Check(rig.Actor.Animation == "RUN", "State3 replaces authored walk with Goddard RUN");
rig.Patrol.Step(.1f);
Near(rig.Actor.Position, new(10, 0, 0), "Second step consumes preceding velocity");
NearFloat(rig.Actor.Rotation.Y, -28.8f, "Prior target yaw smoothed by3.2*dt");
Check(inputs.Count == 2 && inputs[0] == Vector3.Zero && inputs[1] == new Vector3(100, 0, 0), "Body integration phase order");
Pass("prior velocity and prior desired-yaw phase, recovered RUN selection");
}
{
var rig = Rig([Point("A", new(50, 0, 0), "B"), Point("B", new(1000, 0, 0), "none")]);
rig.Patrol.Step(.1f);
Check(rig.Patrol.Arrived && rig.Patrol.CurrentWaypoint == "B", "Arrival selects next waypoint once");
Near(rig.Actor.Position, Vector3.Zero, "Arrival must not snap to waypoint");
Near(rig.Patrol.Velocity, new(100, 0, 0), "First arrival retains velocity selected before flag");
rig.Patrol.Step(.1f);
Near(rig.Actor.Position, new(10, 0, 0), "Prior arrival velocity remains for next physical step");
Check(!rig.Patrol.Arrived && rig.Patrol.Velocity == Vector3.Zero && rig.Actor.Animation == "WAG", "Old arrival selects zero/WAG before new distance clears it");
rig.Patrol.Step(.1f);
Near(rig.Actor.Position, new(10, 0, 0), "One-tick pause must remain");
Near(rig.Patrol.Velocity, new(100, 0, 0), "Subsequent update resumes next segment");
Pass("velocity-before-arrival, no snap and the original waypoint-change pause");
}
{
var rig = Rig([Point("A", new(110, 0, 0), "A")], body: HoldBody);
rig.Patrol.Step(.1f);
Check(!rig.Patrol.Arrived, "Initial distance exactly110 must not arrive");
rig.Director.FindActor("A")!.Position = new(109, 0, 0);
rig.Patrol.Step(.1f);
Check(rig.Patrol.Arrived, "Strictly below110 arrives");
rig.Director.FindActor("A")!.Position = new(110, 0, 0);
rig.Patrol.Step(.1f);
Check(rig.Patrol.Arrived, "Exactly110 retains an already-set arrival flag");
rig.Director.FindActor("A")!.Position = new(111, 0, 0);
rig.Patrol.Step(.1f);
Check(!rig.Patrol.Arrived, "Strictly above110 clears arrival");
Pass("strict arrival threshold and equality hysteresis");
}
{
var rig = Rig([Point("A", Vector3.Zero, "B", .25f, "SIT"), Point("B", new(1000, 0, 0), "none")]);
rig.Patrol.Step(.1f);
Check(rig.Actor.AIstate == 7 && rig.Patrol.WaitRemaining == .25f && rig.Actor.Animation == "SIT", "Wait starts with full authored time after arrival");
rig.Patrol.Step(.125f);
Check(rig.Actor.AIstate == 7 && rig.Patrol.WaitRemaining == .125f, "Positive wait remains active");
rig.Patrol.Step(.125f);
Check(rig.Actor.AIstate == 3 && rig.Patrol.WaitRemaining == 0 && rig.Patrol.CurrentWaypoint == "B" && !rig.Patrol.Arrived,
"Timer reaching exactly zero expires and restores state3");
Near(rig.Actor.Position, Vector3.Zero, "Wait expiry must not consume extra movement");
Near(rig.Patrol.Velocity, Vector3.Zero, "Wait expiry selects waypoint without selecting its velocity yet");
rig.Patrol.Step(.1f);
Check(rig.Patrol.Velocity.Length() > 0 && rig.Actor.Position == Vector3.Zero, "First resumed update still consumes preceding zero velocity");
var overshoot = Rig([Point("A", Vector3.Zero, "B", .125f, "SIT"), Point("B", new(1000, 0, 0), "none")]);
overshoot.Patrol.Step(.5f);
overshoot.Patrol.Step(.5f);
Check(overshoot.Actor.AIstate == 3 && overshoot.Patrol.WaitRemaining == -.375f && overshoot.Actor.Position == Vector3.Zero,
"Wait expiry discards elapsed remainder rather than advancing next segment");
var zeroAtEntry = Rig([Point("A", Vector3.Zero, "none")], state: 7);
zeroAtEntry.Patrol.Step(.1f);
Check(zeroAtEntry.Actor.AIstate == 7, "Original state7 positivity guard retains zero-at-entry timer");
Pass("timed wait, exact-zero expiry, zero-entry guard and discarded remainder");
}
{
foreach (var (next, expectedState) in new[] { ("wAiT", 1), ("sCaN", 6), ("none", 3), ("missing", 3) })
{
var rig = Rig([Point("A", Vector3.Zero, next)], body: HoldBody);
rig.Patrol.Step(.1f);
Check(rig.Actor.AIstate == expectedState && rig.Patrol.CurrentWaypoint == null, "Unresolved next-name semantics: " + next);
if (expectedState == 3)
{
rig.Patrol.Step(.1f);
Check(rig.Patrol.PreviousWaypoint == "A" && rig.Patrol.Arrived, "Ordinary missing next names restart initial route");
}
}
var realWaitTag = Rig([Point("A", Vector3.Zero, "WAIT"), Point("WAIT", new(1000, 0, 0), "none")]);
realWaitTag.Patrol.Step(.1f);
Check(realWaitTag.Actor.AIstate == 3 && realWaitTag.Patrol.CurrentWaypoint == "WAIT", "Existing actor tag takes precedence over WAIT specialcase");
var timedWaitName = Rig([Point("A", Vector3.Zero, "WAIT", .25f, "SIT")]);
timedWaitName.Patrol.Step(.1f);
timedWaitName.Patrol.Step(.25f);
Check(timedWaitName.Actor.AIstate == 3 && timedWaitName.Patrol.CurrentWaypoint == null, "Timed wait resolution does not interpret WAIT specially");
Pass("WAIT/SCAN lookup precedence, unresolved-none route restart and timed-wait distinction");
}
{
var rig = Rig([Point("A", new(0, 500, 0), "none")], body: (body, _) => body with { Velocity = new(5, -7, 9) }, initial: "missing");
rig.Patrol.Step(.1f);
Check(rig.Actor.AIstate == 1, "Missing initial waypoint enters waiting state1");
Near(rig.Patrol.Velocity, new(0, -7, 0), "Missing initial waypoint preserves Y and clears XZ");
rig.Patrol.Step(.1f);
Near(rig.Patrol.Velocity, Vector3.Zero, "Ordinary state1 clears XYZ after body callback");
var vertical = Rig([Point("A", new(0, 500, 0), "none")]);
vertical.Patrol.Step(.1f);
Near(vertical.Patrol.Velocity, new(0, 100, 0), "Patrol selects fullXYZ velocity rather than retaining body Y");
Pass("missing initial waypoint preserves Y for its transition; ordinary waiting and fullXYZ patrol");
}
{
var rig = Rig([Point("A", new(500, 0, 0), "none")]);
rig.Patrol.Step(.1f);
rig.Patrol.Step(.1f);
var before = rig.Patrol.Velocity;
rig.Actor.Animation = "SIT";
rig.Patrol.SynchronizeAuthoredState();
Check(rig.Patrol.Velocity == before && rig.Patrol.CurrentWaypoint == "A", "Camera-only animation cannot reset body/route");
rig.Actor.Position = new(400, 50, -20);
rig.Actor.Rotation = new(0, 10, 0);
rig.Actor.AIstate = 1;
rig.Actor.Animation = "SIT";
rig.Patrol.SynchronizeAuthoredState();
Check(rig.Patrol.Velocity == Vector3.Zero && rig.Patrol.CurrentWaypoint == null && !rig.Patrol.Arrived,
"Authored teleport resets port motion and route cache");
rig.Patrol.Step(.1f);
Near(rig.Actor.Position, new(400, 50, -20), "Teleport cannot inherit preceding patrol velocity");
NearFloat(rig.Actor.Rotation.Y, 10, "Teleport authored heading retained");
Check(rig.Actor.Animation == "SIT", "Authored state1 SIT retained");
Pass("authored teleport reset and camera-only animation preservation");
}
{
var rig = Rig([Point("A", new(300, 400, 0), "none")], body: HoldBody, magnitude: _ => 1000);
rig.Patrol.Step(.1f);
Near(rig.Patrol.Velocity, new(30, 40, 0), "Injected original magnitude controls normalization");
NearFloat(rig.Patrol.DestinationDistance, 500, "Arrival still uses exact sqrt rather than injected magnitude");
foreach (var dt in new[] { -1f, float.NaN, float.PositiveInfinity, 1.01f })
{
var threw = false;
try { rig.Patrol.Step(dt); } catch (ArgumentOutOfRangeException) { threw = true; }
Check(threw, "Invalid dt rejected");
}
rig.Actor.Active = false;
var inactive = rig.Actor.Position;
rig.Patrol.Step(.5f);
Check(rig.Actor.Position == inactive, "Inactive actor must not update");
rig.Actor.Active = true;
rig.Actor.AIstate = 6;
Check(!rig.Patrol.Step(.1f), "Unrecovered scan state reported unsupported");
Pass("injected magnitude remains separate from arrival metric; bounded inactive/unsupported state handling");
}
{
var level = GamLevel.Load(AssetPath.Resolve(original, "gam/level1b.gam"));
var director = new SceneDirector(level, new TaskStates([]), (_, _) => 10);
var actor = director.FindActor("C3DGODDARD")!;
director.Trigger("SITGODDARD", "LABEXP1");
var patrol = new ActorPatrol(actor, director.FindActor, FreeBody);
var sawAuthoredSit = false;
director.ActorChanged += changed =>
{
if (!ReferenceEquals(actor, changed)) return;
sawAuthoredSit |= actor.AIstate == 1 && actor.Animation.Equals("sit", StringComparison.OrdinalIgnoreCase);
patrol.SynchronizeAuthoredState();
};
director.Trigger("GOGODDARD", "GOCARL");
Check(actor.AIstate == 3 && actor.Speed == 260 && actor.Patrol == "GODDARDPAT1", "Original authored patrol input");
var start = actor.Position;
patrol.Step(1 / 60f);
Check(actor.Position == start && actor.Animation == "RUN", "Real route begins after velocity selection; source RUN alias");
var sawSecond = false;
var sawUnresolvedNone = false;
for (var i = 0; i < 800; i++)
{
patrol.Step(1 / 60f);
sawSecond |= patrol.CurrentWaypoint == "GODDARDPAT2";
sawUnresolvedNone |= sawSecond && patrol.CurrentWaypoint == null && actor.AIstate == 3;
Check(float.IsFinite(actor.Position.X + actor.Position.Y + actor.Position.Z), "Finite real route state");
}
Check(sawSecond && sawUnresolvedNone, "Real lab route traverses both points and reaches unresolved-none restart");
director.Trigger("GODDARDDIS", "TELGODDARD");
var destination = director.FindActor("phone2")!.Position;
Check(actor.Position == destination && actor.AIstate == 1 && sawAuthoredSit,
"Original authored patrol interruption must precede the following camera's TALK action");
patrol.Step(1 / 60f);
Check(actor.Position == destination && patrol.Velocity == Vector3.Zero, "Authored phone teleport stops port patrol motion");
Pass("actual lab patrol route and source GODDARDDIS interruption (free-body algorithm test)");
}
{
var carl = Entity("3CAR", "carl", ("AIState", 3), ("PatrolPoint", "A"));
var point = Point("A", new(1000, 0, 0), "WAIT");
var director = new SceneDirector(new("TEST", [carl, point]), new TaskStates([]), (_, _) => 1);
var actor = director.FindActor("carl")!;
Check(actor.Speed == 200, "Carl constructor speed200 is independent of Goddard defaults");
var patrol = new ActorPatrol(actor, director.FindActor, FreeBody);
patrol.Step(.1f);
Check(actor.Animation == "WALK" && actor.Position == Vector3.Zero, "Carl uses WALK with the same prior-velocity phase");
patrol.Step(.1f);
Near(actor.Position, new(20,0,0), "Carl source speed and body phase");
NearFloat(actor.Rotation.Y, -27, "Carl inherited yaw factor3 differs from Goddard3.2");
actor.AIstate = 9;
Check(patrol.Step(.1f), "State9 retains the common base phase without an invented patrol command");
Pass("Carl source WALK/STOP profile, speed200, inherited yaw factor and common state9 phase");
}
Console.WriteLine("All10 actor patrol test groups passed. Free/held body fixtures validate AI order, not original collision fidelity.");
static (SceneDirector Director, DirectorActor Actor, ActorPatrol Patrol) Rig(GamEntity[] points,
Func<PatrolBodyState, float, PatrolBodyState>? body = null, int state = 3, string initial = "A",
Func<Vector3, float>? magnitude = null)
{
var god = Entity("3DOG", "god", ("AIState", state), ("PatrolPoint", initial));
var director = new SceneDirector(new("TEST", new[] { god }.Concat(points).ToArray()), new TaskStates([]), (_, _) => 1);
var actor = director.FindActor("god")!;
actor.Speed = 100;
actor.Animation = "walk";
return (director, actor, new(actor, director.FindActor, body ?? FreeBody, magnitude));
}
static PatrolBodyState FreeBody(PatrolBodyState state, float dt) => state with { Position = state.Position + state.Velocity * dt };
static PatrolBodyState HoldBody(PatrolBodyState state, float dt) => state;
static GamEntity Point(string tag, Vector3 position, string next, float wait = 0, string waitAnimation = "none") => Entity("3PAT", tag,
("PositionX", position.X), ("PositionY", position.Y), ("PositionZ", position.Z), ("NextPatrolPoint", next), ("WaitTime", wait), ("WaitAnim", waitAnimation));
static GamEntity Entity(string kind, string tag, params (string Name, object Value)[] values)
{
var properties = new Dictionary<string, GamProperty>(StringComparer.Ordinal);
foreach (var (name, value) in new[] { ("ObjectTag", (object)tag) }.Concat(values))
{
uint type;
byte[] raw;
switch (value)
{
case string s: type = 1; raw = Encoding.Latin1.GetBytes(s); break;
case int i: type = 6; raw = new byte[4]; BinaryPrimitives.WriteInt32BigEndian(raw, i); break;
case float f: type = 3; raw = new byte[4]; BinaryPrimitives.WriteInt32BigEndian(raw, BitConverter.SingleToInt32Bits(f)); break;
default: throw new ArgumentException("Unsupported test property");
}
properties.Add(name, new(name, type, raw));
}
return new(kind, 0, properties, new byte[4]);
}
static void Near(Vector3 actual, Vector3 expected, string message) => Check(Vector3.Distance(actual, expected) < .001f, message + $": {actual} != {expected}");
static void NearFloat(float actual, float expected, string message) => Check(MathF.Abs(actual - expected) < .001f, message + $": {actual} != {expected}");
static void Check(bool value, string message) { if (!value) throw new InvalidOperationException(message); }
static void Pass(string message) => Console.WriteLine("PASS " + message);