701 lines
39 KiB
C#
701 lines
39 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),
|
|
("physics consumes prior motion; fresh versus retained input order", FrameOrdering),
|
|
("ordinary route load retains scalars independently of body/jump reset", RetainedLevelState),
|
|
("script preparation preserves queued work and uncontrolled physics consumes it", UncontrolledPhysics),
|
|
("uncontrolled fire ticks while movement and grounded-release reset are gated", UncontrolledFire),
|
|
("source root ceiling ray bounces and cancels before control ownership", CeilingWrapper),
|
|
("source camera query origin, hit adjustment, gain and one query per input", CameraQueryConsumer),
|
|
("debug noclip bypasses walls/gravity/camera and exits without queued motion", Noclip),
|
|
("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),
|
|
("RETURN restores exact root with authored start yaw", ReturnRoot),
|
|
("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 FrameOrdering()
|
|
{
|
|
var fresh = FlatSimulation();
|
|
var origin = fresh.Position;
|
|
var forward = new PlayerInput(true, false, false, false, false);
|
|
fresh.Step(forward);
|
|
NearVector(fresh.Position, origin, .0001f, "input cannot move an already integrated body");
|
|
Near(fresh.Speed, 30, 0, "fresh input runs before player callback");
|
|
Near(fresh.Velocity.Z, 30, 0, "player produces velocity for the next physics pass");
|
|
fresh.Step(forward);
|
|
Near(fresh.Position.Z - origin.Z, 30 * FixedStepClock.Step, .0001f, "next physics consumes previous30, not current60");
|
|
|
|
var retained = FlatSimulation();
|
|
retained.InputAfterPlayerUpdate = true;
|
|
retained.Step(forward);
|
|
Near(retained.Speed, 30, 0, "new level game updates retained player's input later");
|
|
Near(retained.Velocity.Z, 0, 0, "retained player callback preceded first destination input");
|
|
retained.Step(forward);
|
|
NearVector(retained.Position, origin, .0001f, "second retained physics still sees old zero velocity");
|
|
Near(retained.Velocity.Z, 30, 0, "retained player now produces prior input speed");
|
|
retained.Step(forward);
|
|
Near(retained.Position.Z - origin.Z, 30 * FixedStepClock.Step, .0001f, "retained third physics consumes first input");
|
|
|
|
var jump = FlatSimulation();
|
|
jump.Step(new(false, false, false, false, true));
|
|
NearVector(jump.Position, origin, .0001f, "queued jump cannot retroactively change physics");
|
|
Near(jump.PendingImpulse.Y, 550, 0, "jump impulse remains pending until next body pass");
|
|
Check(jump.Grounded && jump.Pose == PlayerPose.Jump, "jump animation request does not change physical support");
|
|
jump.Step(default);
|
|
Near(jump.PendingImpulse.Y, 0, 0, "next body pass consumes queued impulse exactly once");
|
|
Near(jump.Velocity.Y, 550 - 800 * 1.33f * FixedStepClock.Step, .0001f,
|
|
"next body pass applies one impulse plus the retained provisional gravity adapter");
|
|
Check(!jump.Grounded && jump.Position.Y > origin.Y, "queued jump takes off on following body pass");
|
|
jump.Step(default);
|
|
Check(jump.Velocity.Y < 550 && jump.Velocity.Y > 500, "later body pass adds acceleration without repeating impulse");
|
|
jump.Reset();
|
|
NearVector(jump.PendingImpulse, Vector3.Zero, 0, "reset clears pending physical work");
|
|
Check(jump.MovementMode == 0, "reset restores stored walking input mode");
|
|
}
|
|
|
|
static void UncontrolledPhysics()
|
|
{
|
|
var cameraQueries = 0;
|
|
var simulation = FlatSimulation(cameraRay: (_, _) => { cameraQueries++; return null; });
|
|
simulation.Step(new(true, false, false, false, true));
|
|
var pending = simulation.PendingImpulse;
|
|
var mode = simulation.MovementMode; var jump = simulation.JumpState;
|
|
var hold = simulation.JumpHoldSeconds; var fire = simulation.JetpackRemaining;
|
|
var before = simulation.Position;
|
|
simulation.PrepareForScript();
|
|
Check(simulation.Speed == 0 && simulation.Velocity == Vector3.Zero && simulation.PendingImpulse == pending,
|
|
"43A750 zeros speed/velocity while queued impulse survives");
|
|
Check(simulation.MovementMode == mode && simulation.JumpState == jump && simulation.JumpHoldSeconds == hold
|
|
&& simulation.JetpackRemaining == fire, "Preparation does not invent a mode/jump/hold/fire reset");
|
|
var queries = cameraQueries;
|
|
for (var tick = 0; tick < 8; tick++) simulation.Step(new(true, false, true, false, true), controlled: false);
|
|
Check(simulation.Position.Y > before.Y && simulation.PendingImpulse == Vector3.Zero,
|
|
"Uncontrolled body consumes the queued impulse once and keeps integrating");
|
|
Check(simulation.Jumps == 1 && simulation.ControllerSteps == 1 && simulation.Speed == 0 && simulation.TurnRate == 0
|
|
&& simulation.JumpState == jump && simulation.MovementMode == mode && simulation.JumpHoldSeconds == hold,
|
|
"Held movement/turn/jump cannot enter the uncontrolled controller");
|
|
Check(simulation.PhysicsSteps == 9 && simulation.EffectSteps == 9 && simulation.Steps == 9,
|
|
"Body and fire callbacks remain distinct from controlled movement count");
|
|
Check(cameraQueries == queries, "Uncontrolled input does not advance the follow-camera query");
|
|
simulation.ApplyAuthoredTransform(simulation.ActorRoot + new Vector3(25, 0, 30), 85);
|
|
Check(simulation.Velocity.Y > 0 && simulation.Jumps == 1,
|
|
"Authored transform write is separate from body preparation");
|
|
}
|
|
|
|
static void UncontrolledFire()
|
|
{
|
|
foreach (var code in new[] { "LV1B", "VR04" })
|
|
{
|
|
var simulation = FlatSimulation(); simulation.LevelCode = code;
|
|
simulation.ApplyRetainedLevelState(new("LV1B", 3, 1, .625f, 6.125f));
|
|
simulation.RestoreReturnRoot(simulation.ActorRoot + Vector3.UnitY * 500);
|
|
var energy = simulation.Energy;
|
|
simulation.PrepareForScript();
|
|
simulation.Step(new(true, false, true, false, true), controlled: false);
|
|
Near(simulation.JetpackRemaining, 6.125f - FixedStepClock.Step, .00001f, "Owned fire time continues during control lock");
|
|
Near(simulation.Energy, code == "VR04" ? energy : energy - FixedStepClock.Step * .5f, .00001f,
|
|
"Fire drain and tutorial immunity do not depend on raw control");
|
|
Check(simulation.ControllerSteps == 0 && simulation.JumpState == 0 && simulation.MovementMode == 1
|
|
&& simulation.JumpHoldSeconds == .625f && simulation.JetpackEffectVisible, "Controller scalars untouched while fire remains visible");
|
|
}
|
|
var grounded = FlatSimulation();
|
|
grounded.ApplyRetainedLevelState(new("LV1B", 3, 1, .625f, 6.125f));
|
|
grounded.Step(default, controlled: false);
|
|
Check(grounded.Grounded && grounded.MovementMode == 1 && grounded.JetpackRemaining < 0
|
|
&& grounded.Energy == 20, "Grounded fire clears independently of the gated grounded-release controller");
|
|
grounded.Step(default);
|
|
Check(grounded.MovementMode == 0 && grounded.ControllerSteps == 1, "Normal grounded-release reset resumes with control");
|
|
}
|
|
|
|
static void RetainedLevelState()
|
|
{
|
|
var state = new PlayerLevelState("LV1B", 3, 1, .625f, 6.125f);
|
|
foreach (var destination in new[] { "VR04", "LV1B", "LEV1" })
|
|
{
|
|
var next = FlatSimulation(); next.LevelCode = destination; next.InputAfterPlayerUpdate = true;
|
|
Check(next.ApplyRetainedLevelState(state), "Proven route accepts supported retained scalars");
|
|
Check(next.MovementMode == 1 && next.JumpState == 0 && next.JumpHoldSeconds == .625f
|
|
&& next.JetpackRemaining == 6.125f, "Jump reset does not imply mode/hold/fire reset");
|
|
Check(next.Speed == 0 && next.TurnRate == 0 && next.Velocity == Vector3.Zero
|
|
&& next.PendingImpulse == Vector3.Zero && !next.JetpackEffectVisible, "Body and effect presentation remain independently initialized");
|
|
next.RestoreReturnRoot(next.ActorRoot + Vector3.UnitY * 500);
|
|
next.Step(default);
|
|
Check(!next.Grounded && next.MovementMode == 1 && next.JumpState == 0,
|
|
"Airborne first destination tick keeps retained input mode after jump state reset");
|
|
Near(next.JetpackRemaining, 6.125f - FixedStepClock.Step, .00001f, "Fire timer advances in its own next-tick phase");
|
|
var carried = next.CaptureLevelState(-1)!;
|
|
Check(carried.SourceLevel == destination && carried.HoldSeconds == .625f, "Ordinary boundary captures current scalars");
|
|
next.Reset();
|
|
Check(next.MovementMode == 0 && next.JumpHoldSeconds == 0 && next.JetpackRemaining == 0, "Explicit modern reset clears transient carry state");
|
|
}
|
|
var grounded = FlatSimulation(); grounded.ApplyRetainedLevelState(state); grounded.Step(default);
|
|
Check(grounded.MovementMode == 0 && grounded.JetpackRemaining < 0,
|
|
"First grounded released controller/fire ticks clear mode and timer independently of loader");
|
|
var unsupported = FlatSimulation();
|
|
Check(!unsupported.ApplyRetainedLevelState(state with { SourceLevel = "LEV6" })
|
|
&& !unsupported.ApplyRetainedLevelState(state with { SelectedGadget = 0 }), "Unproven class cleanup/level states do not acquire carry policy");
|
|
unsupported.SetNoclip(true); Check(unsupported.CaptureLevelState(-1) == null, "Modern noclip does not leak flight into retained gameplay state");
|
|
Throws<ArgumentOutOfRangeException>(() => grounded.ApplyRetainedLevelState(state with { HoldSeconds = float.NaN }));
|
|
Throws<ArgumentOutOfRangeException>(() => grounded.ApplyRetainedLevelState(state with { EffectRemaining = float.PositiveInfinity }));
|
|
}
|
|
|
|
static void CeilingWrapper()
|
|
{
|
|
var queries = 0;
|
|
var start = Vector3.Zero; var end = Vector3.Zero;
|
|
SupportHit? Ceiling(Vector3 a, Vector3 b)
|
|
{ queries++; start = a; end = b; return new((a + b) / 2, 1, 2, 75); }
|
|
var jump = new PlayerInput(false, false, false, false, true);
|
|
var simulation = FlatSimulation(ceilingRay: Ceiling);
|
|
simulation.Step(jump);
|
|
Check(queries == 0 && simulation.PendingImpulse.Y == 550, "Queued impulse is not upward velocity yet");
|
|
simulation.ApplyRetainedLevelState(new("LV1B", 3, 1, .625f, 6.125f));
|
|
var energy = simulation.Energy;
|
|
simulation.Step(jump, controlled: false);
|
|
NearVector(start, simulation.ActorRoot, .0001f, "Upward query uses post-physics actor root");
|
|
NearVector(end - start, Vector3.UnitY * 150, 0, "Upward ray uses authored150 span");
|
|
Check(simulation.Velocity.Y < -300 && simulation.CeilingHits == 1, "Original0.6 velocity reflection");
|
|
Check(simulation.ControllerSteps == 1 && simulation.MovementMode == 1 && simulation.JumpState == 0,
|
|
"Ceiling cancellation runs without controlled movement");
|
|
Near(simulation.JumpHoldSeconds, .625f, 0, "Cancellation preserves hold");
|
|
Near(simulation.JetpackRemaining, -FixedStepClock.Step, 0, "Fire ticks after cancellation zero");
|
|
Near(simulation.Energy, energy, 0, "Cancelled fire does not drain energy");
|
|
Check(simulation.Pose == PlayerPose.Fall && simulation.PendingImpulse == Vector3.Zero,
|
|
"FALL request and independent impulse consumption");
|
|
simulation.Step(default, controlled: false);
|
|
Check(queries == 1, "Non-upward velocity makes no ceiling query");
|
|
|
|
var otherMode = FlatSimulation(ceilingRay: Ceiling);
|
|
otherMode.Step(jump);
|
|
otherMode.ApplyRetainedLevelState(new("LV1B", 3, 0, .625f, 6.125f));
|
|
otherMode.Step(default, controlled: false);
|
|
Check(otherMode.Velocity.Y < 0 && otherMode.MovementMode == 0 && otherMode.JetpackRemaining > 6,
|
|
"Mode0 still reflects velocity but skips mode1 cancellation");
|
|
|
|
var hit = false;
|
|
var powered = FlatSimulation(ceilingRay: (a,b) => hit ? new SupportHit((a+b)/2, 1, 2, 75) : null);
|
|
powered.JetpackInventoryState = () => 1; powered.GadgetMode = 3;
|
|
for (var i = 0; i < 40; i++) powered.Step(jump);
|
|
Check(powered.JumpState == 3 && powered.JetpackRemaining > 0 && powered.Velocity.Y > 0,
|
|
"Powered ceiling fixture has active jump/fire");
|
|
hit = true; powered.Step(default, controlled: false);
|
|
Check(powered.Velocity.Y <= -85 && powered.JumpState == 0 && powered.JetpackRemaining < 0,
|
|
"Ceiling cancels an active powered burst while uncontrolled");
|
|
|
|
foreach (var retained in new[] { false, true })
|
|
{
|
|
var phases = new List<string>();
|
|
var ordered = FlatSimulation(cameraRay: (a,b) => { phases.Add("input"); return null; },
|
|
ceilingRay: (a,b) => { phases.Add("wrapper"); return null; });
|
|
ordered.InputAfterPlayerUpdate = retained;
|
|
ordered.Step(jump); phases.Clear(); ordered.Step(jump);
|
|
Check(phases.SequenceEqual(retained ? ["wrapper", "input"] : new[] { "input", "wrapper" }),
|
|
"Wrapper belongs to Jimmy callback between fresh/retained input boundaries");
|
|
}
|
|
}
|
|
|
|
static void CameraQueryConsumer()
|
|
{
|
|
var enabled = false;
|
|
var queries = 0;
|
|
var lastLook = Vector3.Zero;
|
|
var lastDesired = Vector3.Zero;
|
|
SupportHit? Query(Vector3 look, Vector3 desired)
|
|
{
|
|
queries++; lastLook = look; lastDesired = desired;
|
|
return enabled ? new SupportHit((look + desired) / 2, 7, 9, 0) : null;
|
|
}
|
|
var simulation = FlatSimulation(cameraRay: Query);
|
|
var before = simulation.CameraEye;
|
|
var beforeQueries = queries;
|
|
enabled = true;
|
|
simulation.Step(default);
|
|
NearVector(lastLook, simulation.CameraTarget, .0001f, "ray origin is look target rather than actor root");
|
|
var adjusted = lastLook + (lastDesired - lastLook) * .375f;
|
|
var expected = before + (adjusted - before) * new Vector3(1.6f, 1.2f, 1.6f) * 1.5f * FixedStepClock.Step;
|
|
NearVector(simulation.CameraEye, expected, .0001f, "source hit weighting and follow gain feed interpolation");
|
|
Check(queries == beforeQueries + 1, "no additional camera obstruction query after interpolation");
|
|
}
|
|
|
|
static void Noclip()
|
|
{
|
|
var points = new[] {
|
|
new Vector3(-1000, 0, -1000), new(-1000, 0, 1000), new(1000, 0, 1000),
|
|
new Vector3(-1000, 0, -1000), new(1000, 0, 1000), new(1000, 0, -1000),
|
|
new Vector3(-1000, -1000, 50), new(1000, -1000, 50), new(1000, 1000, 50),
|
|
new Vector3(-1000, -1000, 50), new(1000, 1000, 50), new(-1000, 1000, 50) };
|
|
var mesh = new Mesh(points.Select(p => new MeshVertex(p, Vector2.Zero, Vector3.One)).ToArray(),
|
|
Enumerable.Range(0, points.Length).Select(i => (uint)i).ToArray());
|
|
var standing = new Mesh([new(new(-20, -20, -20), Vector2.Zero, Vector3.One), new(new(20, 80, 20), Vector2.Zero, Vector3.One)], []);
|
|
var collision = new SceneCollision(mesh);
|
|
var rayQueries = 0; var dynamicQueries = 0;
|
|
var simulation = new LabSimulation(collision, standing, new(0, 25, 0), 0,
|
|
(_, _) => { rayQueries++; return null; });
|
|
simulation.DynamicRootResolver = (a, b, r) => { dynamicQueries++; return b; };
|
|
var entry = simulation.Position;
|
|
simulation.Step(new(false, false, false, false, true));
|
|
Check(simulation.PendingImpulse.Y > 0, "Fixture has queued motion before noclip");
|
|
simulation.SetNoclip(true);
|
|
var rays = rayQueries; var bodies = dynamicQueries;
|
|
for (var i = 0; i < 10; i++) simulation.Step(new(true, false, false, false, true));
|
|
Check(simulation.Position.Z > 90 && !simulation.Grounded && simulation.Jumps == 1,
|
|
"Noclip crosses a solid wall without another gameplay jump");
|
|
Near(simulation.PendingImpulse.Length(), 0, 0, "Noclip discards queued impulse");
|
|
Check(rayQueries == rays && dynamicQueries == bodies && simulation.Energy == 20,
|
|
"Noclip bypasses camera/body queries and jetpack drain");
|
|
var height = simulation.Position.Y;
|
|
for (var i = 0; i < 10; i++) simulation.Step(new(false, false, false, false, false, Rise: true));
|
|
Check(simulation.Position.Y > height + 90, "Noclip ascends independently of jumping/gravity");
|
|
simulation.Step(new(false, false, false, false, false, Descend: true, Fast: true));
|
|
Near(simulation.Velocity.Y, -1800, .001f, "Fast descend uses bounded fly speed multiplier");
|
|
var saved = simulation.Position;
|
|
simulation.Step(default); NearVector(simulation.Position, saved, .0001f, "Released noclip has no momentum or gravity");
|
|
Check(!simulation.SetNoclip(false), "Clear exit retains exploration position");
|
|
NearVector(simulation.Position, saved, .0001f, "Disabling in clear space retains position");
|
|
Check(simulation.Velocity == Vector3.Zero && simulation.PendingImpulse == Vector3.Zero && simulation.JumpState == 0,
|
|
"Disabling clears fly velocity and queued gameplay work");
|
|
simulation.Step(default); Check(simulation.Position.Y < saved.Y, "Normal gravity resumes after disabling");
|
|
simulation.ReturnToNoclipEntry(); NearVector(simulation.Position, entry, .001f, "Explicit return restores entry");
|
|
|
|
simulation.Reset(); simulation.SetNoclip(true);
|
|
for (var i = 0; i < 5; i++) simulation.Step(new(true, false, false, false, false));
|
|
Check(!collision.CanOccupy(simulation.Position, LabSimulation.BodyRadius, simulation.BodyHeight), "Exit fixture intersects wall");
|
|
Check(simulation.SetNoclip(false), "Embedded exit restores known entry instead of leaving the capsule trapped");
|
|
NearVector(simulation.Position, entry, .001f, "Embedded exit restores cached position");
|
|
simulation.SetNoclip(true); simulation.Reset();
|
|
Check(!simulation.Noclip && !simulation.HasNoclipEntry, "Reset clears modern noclip state");
|
|
}
|
|
|
|
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.Velocity.X < 0 && right.Velocity.X > 0, "keyboard side-velocity signs for next physics pass");
|
|
Near(left.Position.X, 0, .0001f, "turn input waits for subsequent physics");
|
|
|
|
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.PendingImpulse.Y == 550 && simulation.Jumps == 1 && simulation.Pose == PlayerPose.Jump,
|
|
"jump queues once after physics and requests its animation");
|
|
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.PendingImpulse.Y == 550, "new grounded press queues 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));
|
|
simulation.Step(default); // Consume the queued takeoff before airborne input.
|
|
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));
|
|
var largestAirTurn = 0f;
|
|
for (var i = 0; i < 120 && (i == 0 || !simulation.Grounded); i++)
|
|
{
|
|
simulation.Step(new(true, false, initialLeft, !initialLeft, false));
|
|
largestAirTurn = MathF.Max(largestAirTurn, MathF.Abs(simulation.TurnRate));
|
|
}
|
|
Check(simulation.Grounded && largestAirTurn > 20 && simulation.TurnRate == 0,
|
|
"Physics landing precedes the same-frame player update clearing air turn");
|
|
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 ReturnRoot()
|
|
{
|
|
var simulation = FlatSimulation();
|
|
var destination = simulation.ActorRoot + new Vector3(450, 100, -250);
|
|
var yaw = simulation.Yaw;
|
|
simulation.RestoreReturnRoot(destination);
|
|
NearVector(simulation.ActorRoot, destination, .0001f, "restore stored XYZ without floor snapping");
|
|
Near(simulation.Yaw, yaw, 0, "RETURN has no stored rotation; retain STRT yaw");
|
|
Check(simulation.Velocity == Vector3.Zero && simulation.Steps == 0, "fresh destination keeps initial motion state");
|
|
Check(Vector3.Distance(simulation.CameraEye, simulation.ActorRoot) < 500, "camera follows restored root");
|
|
Throws<ArgumentOutOfRangeException>(() => simulation.RestoreReturnRoot(new(float.NaN, 0, 0)));
|
|
simulation.Step(default);
|
|
Check(simulation.ActorRoot.Y < destination.Y && !simulation.Grounded, "restored unsupported root settles through physics");
|
|
simulation.Reset();
|
|
Check(Vector3.Distance(simulation.ActorRoot, destination) > 100, "ordinary reset still uses the source start");
|
|
}
|
|
|
|
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");
|
|
|
|
var geometry = OriginalCameraRay.FromRouteScene(mesh, level.Code);
|
|
var overhead = new LabSimulation(collision, standing, start.Position, start.Rotation.Y,
|
|
(a,b) => geometry.Trace(a,b), (a,b) => geometry.TraceGeometry(a,b));
|
|
overhead.Step(new(false, false, false, false, true));
|
|
overhead.RestoreReturnRoot(overhead.ActorRoot + Vector3.UnitY * 300);
|
|
geometry.Enabled = false; // Modern camera-only switch must not suppress physical query.
|
|
overhead.Step(default, controlled: false);
|
|
Check(overhead.CeilingHits == 1 && overhead.Velocity.Y <= -85 && overhead.Pose == PlayerPose.Fall,
|
|
"Actual registered lab ceiling reverses upward motion with camera debug query disabled");
|
|
}
|
|
|
|
static LabSimulation FlatSimulation(float yaw = 0, Func<Vector3, Vector3, SupportHit?>? cameraRay = null,
|
|
Func<Vector3, Vector3, SupportHit?>? ceilingRay = null)
|
|
{
|
|
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, cameraRay, ceilingRay);
|
|
}
|
|
|
|
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}");
|
|
}
|