406 lines
20 KiB
C#
406 lines
20 KiB
C#
using System.Numerics;
|
|
using GamePort.Assets;
|
|
using GamePort.Game;
|
|
|
|
var project = FindProject();
|
|
var originals = args.Length == 1 ? Path.GetFullPath(args[0]) : Path.GetFullPath(Path.Combine(project, "../Original"));
|
|
var tests = new (string Name, Action Check)[]
|
|
{
|
|
("60 simulation calls across 60/120/144 Hz presentation", ClockRates),
|
|
("clock remainder, stall bound, reset and invalid intervals", ClockBoundaries),
|
|
("turn-only speed, forward acceleration/cap and release", SpeedLimits),
|
|
("backpedal cap and release", Backpedal),
|
|
("yaw sign, forward half-turn and left precedence", Turning),
|
|
("fixed-step input replay independent of presentation rate", ReplayRates),
|
|
("jump rises, falls and returns to support", JumpLanding),
|
|
("held zero-burn jump repeats and release stops repetition", HeldJump),
|
|
("airborne jump re-press rearms landing without a double jump", JumpRepress),
|
|
("air input accelerates, brakes, turns and coasts at source limits", AirArithmetic),
|
|
("ordinary jump responds to airborne arrows", AirControl),
|
|
("powered jetpack starts moving and turns from stationary takeoff", JetpackControl),
|
|
("powered steering remains independent of presentation rate", JetpackReplayRates),
|
|
("landing clears residual air turn before ground steering", LandingTurn),
|
|
("reset restores spawn, camera and counters", Reset),
|
|
("ten-minute simulation remains finite and supported", LongRun),
|
|
("original lab placement, floor, walking and jump checkpoint", () => OriginalLab(project, originals)),
|
|
};
|
|
foreach (var (name, check) in tests)
|
|
{
|
|
check();
|
|
Console.WriteLine($"PASS {name}");
|
|
}
|
|
Console.WriteLine($"Movement checks: {tests.Length} passed. Static reconstruction/port-policy regressions; no original-runtime comparison.");
|
|
|
|
static void ClockRates()
|
|
{
|
|
foreach (var rate in new[] { 60, 120, 144 })
|
|
{
|
|
var clock = new FixedStepClock();
|
|
for (var second = 0; second < 20; second++)
|
|
{
|
|
var steps = 0;
|
|
for (var frame = 0; frame < rate; frame++) steps += clock.Advance(1.0 / rate);
|
|
Check(steps == 60, $"{rate} Hz second {second}: {steps} simulation calls");
|
|
}
|
|
}
|
|
}
|
|
|
|
static void ClockBoundaries()
|
|
{
|
|
var clock = new FixedStepClock();
|
|
Check(clock.Advance(0) == 0, "zero elapsed interval");
|
|
Check(clock.Advance(1.0 / 120) == 0 && clock.Advance(1.0 / 120) == 1, "fractional frame remainder retained");
|
|
Check(clock.Advance(4) == 15, "stalled presentation advances at most a quarter second");
|
|
clock.Advance(1.0 / 120);
|
|
clock.Reset();
|
|
Check(clock.Advance(1.0 / 120) == 0 && clock.Advance(1.0 / 120) == 1, "reset discards old remainder");
|
|
foreach (var elapsed in new[] { -0.01, double.NaN, double.PositiveInfinity, double.NegativeInfinity })
|
|
Throws<ArgumentOutOfRangeException>(() => clock.Advance(elapsed));
|
|
}
|
|
|
|
static void SpeedLimits()
|
|
{
|
|
var turn = FlatSimulation();
|
|
for (var i = 0; i < 40; i++) turn.Step(new(false, false, true, false, false));
|
|
Near(turn.Speed, 0, 0, "turn-only +30 then decay35 clamps at zero");
|
|
Check(turn.Grounded && turn.Pose == PlayerPose.Idle, "turn-only remains supported and idle");
|
|
|
|
var walk = FlatSimulation();
|
|
walk.Step(new(true, false, false, false, false));
|
|
Near(walk.Speed, 30, 0, "first forward invocation");
|
|
for (var i = 1; i < 40; i++) walk.Step(new(true, false, false, false, false));
|
|
Near(walk.Speed, 600, 0, "forward speed cap");
|
|
Check(walk.Position.Z > 250 && walk.Pose == PlayerPose.Run, "forward speed produces visible travel");
|
|
for (var i = 0; i < 20; i++) walk.Step(default);
|
|
Near(walk.Speed, 0, 0, "release decays to zero");
|
|
var stopped = walk.Position;
|
|
for (var i = 0; i < 30; i++) walk.Step(default);
|
|
NearVector(walk.Position, stopped, .001f, "released standing position remains stable");
|
|
}
|
|
|
|
static void Backpedal()
|
|
{
|
|
var simulation = FlatSimulation();
|
|
for (var i = 0; i < 180; i++) simulation.Step(new(false, true, false, false, false));
|
|
Near(simulation.Speed, -300, 0, "reverse cap remains -300 without an invented turn-around timer");
|
|
Check(simulation.Grounded && simulation.Pose == PlayerPose.Backpedal && simulation.Position.Z < -500,
|
|
"backpedal moves opposite forward and stays supported");
|
|
for (var i = 0; i < 20; i++) simulation.Step(default);
|
|
Near(simulation.Speed, 0, 0, "reverse release clamps toward zero");
|
|
}
|
|
|
|
static void Turning()
|
|
{
|
|
var left = FlatSimulation();
|
|
var right = FlatSimulation();
|
|
var forwardLeft = FlatSimulation();
|
|
left.Step(new(false, false, true, false, false));
|
|
right.Step(new(false, false, false, true, false));
|
|
forwardLeft.Step(new(true, false, true, false, false));
|
|
Check(left.Yaw > 0 && right.Yaw < 0, "left/right yaw signs");
|
|
Near(left.Yaw, -right.Yaw, .00001f, "opposed keyboard yaw symmetry");
|
|
Near(forwardLeft.Yaw, left.Yaw / 2, .00001f, "Up halves target turn offset");
|
|
Near(forwardLeft.Speed, 60, 0, "Up plus turn performs both speed increments");
|
|
Check(left.Position.X < 0 && right.Position.X > 0, "keyboard side-velocity signs");
|
|
|
|
var both = FlatSimulation();
|
|
var leftOnly = FlatSimulation();
|
|
for (var i = 0; i < 10; i++)
|
|
{
|
|
both.Step(new(true, false, true, true, false));
|
|
leftOnly.Step(new(true, false, true, false, false));
|
|
}
|
|
SameState(both, leftOnly, "Left has precedence when Left and Right are held together");
|
|
}
|
|
|
|
static void ReplayRates()
|
|
{
|
|
LabSimulation? reference = null;
|
|
foreach (var rate in new[] { 60, 120, 144 })
|
|
{
|
|
var simulation = FlatSimulation();
|
|
var clock = new FixedStepClock();
|
|
for (var frame = 0; frame < rate * 4; frame++)
|
|
{
|
|
var steps = clock.Advance(1.0 / rate);
|
|
for (var step = 0; step < steps; step++) simulation.Step(LabSimulation.SmokeInput(simulation.Steps));
|
|
}
|
|
Check(simulation.Steps == 240, $"{rate} Hz replay duration");
|
|
Check(simulation.SmokePassed, $"{rate} Hz replay walks, jumps and lands: {simulation.Report}");
|
|
if (reference is not null) SameState(simulation, reference, $"{rate} Hz replay");
|
|
reference = simulation;
|
|
}
|
|
}
|
|
|
|
static void JumpLanding()
|
|
{
|
|
var simulation = FlatSimulation();
|
|
var floor = simulation.Position.Y;
|
|
simulation.Step(new(false, false, false, false, true));
|
|
Check(!simulation.Grounded && simulation.Velocity.Y > 500 && simulation.Jumps == 1 && simulation.Pose == PlayerPose.Jump,
|
|
"jump takes off with one queued impulse");
|
|
var sawFall = false;
|
|
var largestHeight = simulation.Position.Y;
|
|
for (var i = 0; i < 120; i++)
|
|
{
|
|
simulation.Step(default);
|
|
largestHeight = Math.Max(largestHeight, simulation.Position.Y);
|
|
sawFall |= simulation.Pose == PlayerPose.Fall;
|
|
}
|
|
Check(sawFall && largestHeight - floor > 100, "jump rises and enters descending phase");
|
|
Check(simulation.Grounded && simulation.Jumps == 1 && simulation.Landings == 1, "released jump lands once");
|
|
Near(simulation.Position.Y, floor, .01f, "landing returns feet to supporting floor");
|
|
Near(simulation.Velocity.Y, 0, 0, "landing clears downward velocity");
|
|
}
|
|
|
|
static void HeldJump()
|
|
{
|
|
var simulation = FlatSimulation();
|
|
for (var i = 0; i < 300; i++) simulation.Step(new(false, false, false, false, true));
|
|
Check(simulation.Jumps >= 3 && simulation.Landings >= 2,
|
|
$"zero-burn long hold rearms and repeats after landing: {simulation.Report}");
|
|
var jumpCount = simulation.Jumps;
|
|
for (var i = 0; i < 150; i++) simulation.Step(default);
|
|
Check(simulation.Jumps == jumpCount && simulation.Grounded, "release stops held repetition and lands");
|
|
simulation.Step(new(false, false, false, false, true));
|
|
Check(simulation.Jumps == jumpCount + 1 && !simulation.Grounded, "new grounded press starts another jump");
|
|
}
|
|
|
|
static void AirArithmetic()
|
|
{
|
|
var input = new PlayerInput(true, false, true, false, true);
|
|
var first = AirSteering.Step(0, 0, input, 1f / 60);
|
|
Near(first.Speed, 30, 0, "air forward adds source30 per invocation");
|
|
Near(first.TurnRate, 100f / 60, .00001f, "air turn ramps by100*dt");
|
|
var both = AirSteering.Step(0, 0, input with { Right = true }, 1f / 60);
|
|
Check(both == first, "Left wins over Right in air");
|
|
var turn = 0f;
|
|
for (var i = 0; i < 90; i++) turn = AirSteering.Step(0, turn, input, 1f / 60).TurnRate;
|
|
Near(turn, 30, 0, "normal airborne turn limit30");
|
|
for (var i = 0; i < 30; i++) turn = AirSteering.Step(0, turn, default, 1f / 60).TurnRate;
|
|
Near(turn, 0, 0, "released turn decays to0 without crossing");
|
|
var coast = 600f;
|
|
for (var i = 0; i < 10; i++) coast = AirSteering.Step(coast, 0, default, 1f / 60).Speed;
|
|
Near(coast, 530, 0, "unpowered input coasts after half-decay crosses max*10/11");
|
|
var braking = new PlayerInput(false, true, false, false, false);
|
|
for (var i = 0; i < 40; i++) coast = AirSteering.Step(coast, 0, braking, 1f / 60).Speed;
|
|
Near(coast, 0, 0, "Down brakes to0 without reversing");
|
|
Near(AirSteering.Step(100, 0, input with { Backward = true }, 1f / 60).Speed, 112.5f, 0,
|
|
"air forward increment precedes Down braking");
|
|
Near(AirSteering.Step(-10, 0, braking, 1f / 60).Speed, 0, 0, "negative takeoff speed brakes toward0");
|
|
Throws<ArgumentOutOfRangeException>(() => AirSteering.Step(0, 0, default, float.NaN));
|
|
}
|
|
|
|
static void AirControl()
|
|
{
|
|
var simulation = FlatSimulation();
|
|
simulation.Step(new(false, false, false, false, true));
|
|
var origin = simulation.Position;
|
|
for (var i = 0; i < 20; i++)
|
|
{
|
|
Check(!simulation.Grounded, "ordinary jump steering fixture stays in air");
|
|
simulation.Step(new(true, false, true, false, false));
|
|
}
|
|
Check(simulation.Speed == 600 && simulation.Yaw > 5 && Vector2.Distance(
|
|
new(origin.X, origin.Z), new(simulation.Position.X, simulation.Position.Z)) > 75,
|
|
"airborne arrows must accelerate and turn an initially stationary jump");
|
|
}
|
|
|
|
static void JetpackControl()
|
|
{
|
|
var straight = FlatSimulation();
|
|
var left = FlatSimulation();
|
|
var right = FlatSimulation();
|
|
foreach (var simulation in new[] { straight, left, right })
|
|
{
|
|
simulation.JetpackInventoryState = () => 1;
|
|
for (var i = 0; i < 40; i++) simulation.Step(new(false, false, false, false, true));
|
|
Check(simulation.JetpackEffectVisible && !simulation.Grounded && simulation.Bursts == 1,
|
|
"stationary takeoff reaches an active powered burst before directional input");
|
|
}
|
|
var initialHeight = left.Position.Y;
|
|
for (var i = 0; i < 60; i++)
|
|
{
|
|
straight.Step(new(true, false, false, false, true));
|
|
left.Step(new(true, false, true, false, true));
|
|
right.Step(new(true, false, false, true, true));
|
|
Check(!left.Grounded && left.JetpackEffectVisible, "steering occurs during powered flight");
|
|
}
|
|
Check(straight.Position.Z > 400 && left.Yaw > 20 && right.Yaw < -20,
|
|
"powered Up moves from rest and Left/Right turn while airborne");
|
|
Check(left.Position.X < -50 && right.Position.X > 50 && left.Position.Y > initialHeight,
|
|
"powered paths change direction and retain vertical lift");
|
|
var beforeBrake = left.Speed;
|
|
for (var i = 0; i < 40; i++) left.Step(new(false, true, false, false, true));
|
|
Check(beforeBrake > 0 && left.Speed == 0 && left.TurnRate == 0 && !left.Grounded,
|
|
"Down brakes during active burst and released turn settles");
|
|
left.Reset();
|
|
Near(left.TurnRate, 0, 0, "reset clears accumulated airborne turn");
|
|
}
|
|
|
|
static void JetpackReplayRates()
|
|
{
|
|
LabSimulation? reference = null;
|
|
foreach (var rate in new[] { 60, 120, 144 })
|
|
{
|
|
var simulation = FlatSimulation();
|
|
simulation.LevelCode = "VR04";
|
|
var clock = new FixedStepClock();
|
|
for (var frame = 0; frame < rate * 3; frame++)
|
|
for (var step = clock.Advance(1.0 / rate); step > 0; step--)
|
|
simulation.Step(new(simulation.Steps >= 40, false, simulation.Steps >= 80, false, true));
|
|
Check(simulation.Bursts == 1 && simulation.Travel > 500, "render-rate replay exercises powered steering");
|
|
if (reference != null) SameState(simulation, reference, "powered steering presentation rate");
|
|
reference = simulation;
|
|
}
|
|
}
|
|
|
|
static void LandingTurn()
|
|
{
|
|
foreach (var initialLeft in new[] { false, true })
|
|
{
|
|
var simulation = FlatSimulation();
|
|
simulation.Step(new(true, false, false, false, true));
|
|
for (var i = 0; i < 120 && !simulation.Grounded; i++)
|
|
simulation.Step(new(true, false, initialLeft, !initialLeft, false));
|
|
Check(simulation.Grounded && MathF.Abs(simulation.TurnRate) > 20,
|
|
"Landing fixture retains an opposing air turn before outer grounded reset");
|
|
var before = simulation.Yaw;
|
|
simulation.Step(new(true, false, !initialLeft, initialLeft, false));
|
|
Near(simulation.TurnRate, 0, 0, "grounded released-jump reset clears shared air-turn field");
|
|
var yawChange = MathF.IEEERemainder(simulation.Yaw - before, 360);
|
|
Near(yawChange, initialLeft ? -10 * FixedStepClock.Step * 3.7f : 10 * FixedStepClock.Step * 3.7f,
|
|
.0001f, "prior jump turn must not cancel or clamp opposing ground turn");
|
|
}
|
|
}
|
|
|
|
static void JumpRepress()
|
|
{
|
|
var simulation = FlatSimulation();
|
|
simulation.Step(new(false, false, false, false, true));
|
|
for (var i = 0; i < 5; i++) simulation.Step(default);
|
|
Check(!simulation.Grounded && simulation.Jumps == 1, "released first jump remains airborne");
|
|
var velocityBeforeRepress = simulation.Velocity.Y;
|
|
simulation.Step(new(false, false, false, false, true));
|
|
Check(simulation.Jumps == 1 && simulation.Velocity.Y < velocityBeforeRepress,
|
|
"re-press cannot consume a second impulse while airborne");
|
|
for (var i = 0; i < 120; i++) simulation.Step(new(false, false, false, false, true));
|
|
Check(simulation.Jumps >= 2 && simulation.Landings >= 1,
|
|
$"released-then-held jump rearms for the next landing: {simulation.Report}");
|
|
var jumps = simulation.Jumps;
|
|
for (var i = 0; i < 150; i++) simulation.Step(default);
|
|
Check(simulation.Jumps == jumps && simulation.Grounded, "release after re-press repeat settles without more jumps");
|
|
}
|
|
|
|
static void Reset()
|
|
{
|
|
var simulation = FlatSimulation(155);
|
|
var fresh = FlatSimulation(155);
|
|
for (var i = 0; i < 240; i++) simulation.Step(LabSimulation.SmokeInput(i));
|
|
Check(simulation.Travel > 100 && simulation.Jumps > 0, "reset fixture has nontrivial history");
|
|
simulation.Reset();
|
|
SameState(simulation, fresh, "reset restores a fresh spawn");
|
|
Check(simulation.Steps == 0 && simulation.Jumps == 0 && simulation.Landings == 0 && simulation.WallHits == 0
|
|
&& simulation.Travel == 0 && simulation.MaximumRise == 0, "reset clears telemetry counters");
|
|
for (var i = 0; i < 90; i++)
|
|
{
|
|
var input = new PlayerInput(i < 60, false, i >= 30, false, i >= 20 && i < 50);
|
|
simulation.Step(input);
|
|
fresh.Step(input);
|
|
}
|
|
SameState(simulation, fresh, "reset also clears hidden jump/turn/hold state");
|
|
}
|
|
|
|
static void LongRun()
|
|
{
|
|
var simulation = FlatSimulation();
|
|
for (var i = 0; i < 36000; i++)
|
|
{
|
|
var phase = i % 600;
|
|
simulation.Step(new(phase < 400, phase >= 500, phase is >= 200 and < 400, false, phase is >= 80 and < 180));
|
|
Check(Finite(simulation.Position) && Finite(simulation.Velocity) && Finite(simulation.CameraEye)
|
|
&& Finite(simulation.CameraTarget) && float.IsFinite(simulation.Yaw) && float.IsFinite(simulation.Speed),
|
|
$"simulation remains finite at step {i}");
|
|
Check(simulation.Position.Y >= SceneCollision.Skin - .01f, $"no floor penetration at step {i}");
|
|
}
|
|
Check(simulation.Steps == 36000 && simulation.Jumps > 50 && simulation.Landings > 50, "sustained repeated movement/jump coverage");
|
|
for (var i = 0; i < 120; i++) simulation.Step(default);
|
|
Check(simulation.Grounded, "long run settles to support");
|
|
}
|
|
|
|
static void OriginalLab(string project, string originals)
|
|
{
|
|
var sceneFile = Path.Combine(project, "artifacts/extracted/level1b/scene.json");
|
|
Check(File.Exists(sceneFile), "prepared original lab required (run tools/extractors/prepare.py)");
|
|
var mesh = Mesh.LoadSceneJson(sceneFile);
|
|
var standing = AseMesh.Load(AssetPath.Resolve(originals, "ase/JIMSTOP.ASE"));
|
|
var level = GamLevel.Load(AssetPath.Resolve(originals, "gam/level1b.gam"));
|
|
var start = level.FindStart() ?? throw new Exception("Missing LV1B GAM startpoint");
|
|
var collision = new SceneCollision(mesh);
|
|
Near(collision.GroundHeight(start.Position, 2500)!.Value, .5486332f, .001f, "original floor below GAM placement");
|
|
var simulation = new LabSimulation(collision, standing, start.Position, start.Rotation.Y);
|
|
Near(simulation.Position.X, 5071.933105f, .001f, "GAM X retained");
|
|
Near(simulation.Position.Z, 824.103943f, .001f, "GAM Z retained");
|
|
Near(simulation.Position.Y, .5486332f + SceneCollision.Skin, .001f, "spawn feet on original floor");
|
|
Near(simulation.Yaw, 155, .001f, "GAM yaw retained");
|
|
Near(simulation.BodyHeight, 209.4361f, .01f, "capsule height from original standing bounds");
|
|
var sole = standing.Vertices.Min(vertex => Vector3.Transform(vertex.Position, simulation.Model).Y);
|
|
Near(sole, simulation.Position.Y, .001f, "rendered standing sole agrees with simulation feet");
|
|
for (var i = 0; i < 240; i++) simulation.Step(LabSimulation.SmokeInput(i));
|
|
Check(simulation.SmokePassed, $"original lab checkpoint: {simulation.Report}");
|
|
Check(Finite(simulation.Position) && Finite(simulation.CameraEye), "original scene simulation/camera finite");
|
|
Console.WriteLine($" {simulation.Report}");
|
|
|
|
simulation.Reset();
|
|
simulation.Step(new(false, false, false, false, true));
|
|
for (var i = 0; i < 120; i++) simulation.Step(default);
|
|
Check(simulation.Jumps == 1 && simulation.Landings == 1 && simulation.Grounded, "stationary original-lab jump lands once");
|
|
Near(simulation.Position.Y, .5486332f + SceneCollision.Skin, .03f, "original lab jump returns to floor");
|
|
}
|
|
|
|
static LabSimulation FlatSimulation(float yaw = 0)
|
|
{
|
|
var positions = new[]
|
|
{
|
|
new Vector3(-100000, 0, -100000), new Vector3(-100000, 0, 100000), new Vector3(100000, 0, 100000),
|
|
new Vector3(-100000, 0, -100000), new Vector3(100000, 0, 100000), new Vector3(100000, 0, -100000),
|
|
};
|
|
var floor = new Mesh(positions.Select(p => new MeshVertex(p, Vector2.Zero, Vector3.One)).ToArray(), [0, 1, 2, 3, 4, 5]);
|
|
var standing = new Mesh([new(new(-20, -20, -20), Vector2.Zero, Vector3.One),
|
|
new(new(20, 80, 20), Vector2.Zero, Vector3.One)], []);
|
|
return new LabSimulation(new SceneCollision(floor), standing, new Vector3(0, 25, 0), yaw);
|
|
}
|
|
|
|
static void SameState(LabSimulation actual, LabSimulation expected, string message)
|
|
{
|
|
NearVector(actual.Position, expected.Position, .0001f, $"{message}: position");
|
|
NearVector(actual.Velocity, expected.Velocity, .0001f, $"{message}: velocity");
|
|
NearVector(actual.CameraEye, expected.CameraEye, .0001f, $"{message}: camera");
|
|
Near(actual.Yaw, expected.Yaw, .0001f, $"{message}: yaw");
|
|
Near(actual.Speed, expected.Speed, 0, $"{message}: speed");
|
|
Near(actual.TurnRate, expected.TurnRate, 0, $"{message}: turn rate");
|
|
Check(actual.Grounded == expected.Grounded && actual.Pose == expected.Pose && actual.Jumps == expected.Jumps
|
|
&& actual.Landings == expected.Landings, $"{message}: movement state/counters");
|
|
}
|
|
static string FindProject()
|
|
{
|
|
var directory = new DirectoryInfo(Environment.CurrentDirectory);
|
|
while (directory is not null)
|
|
{
|
|
if (File.Exists(Path.Combine(directory.FullName, "GamePort.slnx"))) return directory.FullName;
|
|
directory = directory.Parent;
|
|
}
|
|
throw new InvalidOperationException("Run from within GamePort");
|
|
}
|
|
static bool Finite(Vector3 value) => float.IsFinite(value.X) && float.IsFinite(value.Y) && float.IsFinite(value.Z);
|
|
static void Check(bool condition, string message) { if (!condition) throw new Exception(message); }
|
|
static void Near(float actual, float expected, float tolerance, string message) =>
|
|
Check(MathF.Abs(actual - expected) <= tolerance, $"{message}: actual={actual}, expected={expected}, tolerance={tolerance}");
|
|
static void NearVector(Vector3 actual, Vector3 expected, float tolerance, string message) =>
|
|
Check(Vector3.Distance(actual, expected) <= tolerance, $"{message}: actual={actual}, expected={expected}, tolerance={tolerance}");
|
|
static void Throws<T>(Action check) where T : Exception
|
|
{
|
|
try { check(); } catch (T) { return; }
|
|
throw new Exception($"Expected {typeof(T).Name}");
|
|
}
|