Add controllable lab walking checkpoint with collision and chase camera
This commit is contained in:
1 parent
37fa18094f
commit
58424120bb
26 files changed
+1848
-110
No files matched your search
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType><TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings><Nullable>enable</Nullable>
|
||||
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="../../src/GamePort/Game/SceneCollision.cs" Link="SceneCollision.cs" />
|
||||
<Compile Include="../../src/GamePort/Assets/Mesh.cs" Link="Mesh.cs" />
|
||||
<Compile Include="../../src/GamePort/Assets/PngImage.cs" Link="PngImage.cs" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,288 @@
|
||||
using System.Diagnostics;
|
||||
using System.Numerics;
|
||||
using GamePort.Assets;
|
||||
using GamePort.Game;
|
||||
|
||||
var tests = new (string Name, Action Check)[]
|
||||
{
|
||||
("flat support and ground-height interval", FlatFloor),
|
||||
("continuous high-speed floor and wall collision", HighSpeed),
|
||||
("wall sliding, departure and corner", Walls),
|
||||
("ceiling and underside", Ceiling),
|
||||
("sloped floor and landing", Slope),
|
||||
("walking over an edge releases support", Edge),
|
||||
("triangle edge and vertex sweep", TriangleEdges),
|
||||
("camera sphere sweep", Camera),
|
||||
("original-scale translated room and repeated seam crossings", TranslatedRoom),
|
||||
("invisible source triangles and invalid input", SourcePolicy),
|
||||
("original lab spawn, traversal, jump and deterministic replay", OriginalLab),
|
||||
};
|
||||
foreach (var (name, check) in tests)
|
||||
{
|
||||
check();
|
||||
Console.WriteLine($"PASS {name}");
|
||||
}
|
||||
Console.WriteLine($"Collision checks: {tests.Length} passed");
|
||||
|
||||
static void FlatFloor()
|
||||
{
|
||||
var mesh = new Builder().Floor(0, -100, 100, -100, 100).Floor(10, -100, 100, -100, 100).Build();
|
||||
var scene = new SceneCollision(mesh);
|
||||
Near(scene.GroundHeight(new Vector3(0, 5, 0), 20)!.Value, 0, .0001f, "floor below queried point");
|
||||
Near(scene.GroundHeight(new Vector3(0, 5, 0), 20, 6)!.Value, 10, .0001f, "highest permitted surface");
|
||||
Check(scene.GroundHeight(new Vector3(0, 5, 0), 4) is null, "floor outside drop interval");
|
||||
var standing = scene.Move(Vector3.Zero, Vector3.Zero, 1, 4);
|
||||
Check(standing.Grounded, "stationary support");
|
||||
Near(standing.Position.Y, SceneCollision.Skin, .01f, "surface skin");
|
||||
var moved = scene.Move(standing.Position, new Vector3(30, 0, 20), 1, 4);
|
||||
Check(moved.Grounded && !moved.HitWall && !moved.HitCeiling, "tangent floor motion");
|
||||
Near(moved.Position.X, 30, .01f, "free horizontal x");
|
||||
Near(moved.Position.Z, 20, .01f, "free horizontal z");
|
||||
}
|
||||
|
||||
static void HighSpeed()
|
||||
{
|
||||
var scene = new SceneCollision(new Builder().Floor(0, -20000, 20000, -20000, 20000)
|
||||
.WallX(5, 0, 100, -100, 100).Build());
|
||||
var landing = scene.Move(new Vector3(0, 1000, 0), new Vector3(0, -10000, 0), 1, 4);
|
||||
Check(landing.Grounded, "fast fall lands");
|
||||
Near(landing.Position.Y, SceneCollision.Skin, .01f, "no floor tunneling");
|
||||
var wall = scene.Move(new Vector3(0, 1, 0), new Vector3(10000, 0, 0), 1, 4);
|
||||
Check(wall.HitWall, "fast horizontal hit");
|
||||
Near(wall.Position.X, 4 - SceneCollision.Skin, .02f, "no wall tunneling");
|
||||
var diagonal = scene.Move(new Vector3(-100, 100, 0), new Vector3(10000, -10000, 20), 1, 4);
|
||||
Check(diagonal.Grounded && diagonal.HitWall, $"fast diagonal hits floor and wall: {diagonal}");
|
||||
Check(diagonal.Position.X <= 4 && diagonal.Position.Y >= -.01f, "diagonal safe position");
|
||||
}
|
||||
|
||||
static void Walls()
|
||||
{
|
||||
var scene = new SceneCollision(new Builder().Floor(0, -100, 100, -100, 100)
|
||||
.WallX(5, 0, 20, -100, 100).WallZ(5, 0, 20, -100, 100).Build());
|
||||
var slide = scene.Move(new Vector3(0, .1f, -10), new Vector3(10, 0, 10), 1, 4);
|
||||
Near(slide.Position.X, 4 - SceneCollision.Skin, .02f, "wall x limit");
|
||||
Near(slide.Position.Z, 0, .02f, "wall preserves tangent travel");
|
||||
var leave = scene.Move(slide.Position, new Vector3(-3, 0, 0), 1, 4);
|
||||
Near(leave.Position.X, slide.Position.X - 3, .02f, "depart contact freely");
|
||||
var corner = scene.Move(new Vector3(0, .1f, 0), new Vector3(100, 0, 100), 1, 4);
|
||||
Near(corner.Position.X, 4 - SceneCollision.Skin, .02f, "corner x");
|
||||
Near(corner.Position.Z, 4 - SceneCollision.Skin, .02f, "corner z");
|
||||
var resting = corner.Position;
|
||||
for (var i = 0; i < 100; i++) resting = scene.Move(resting, new Vector3(1, -.2f, 1), 1, 4).Position;
|
||||
Check(Vector3.Distance(resting, new Vector3(4 - SceneCollision.Skin, SceneCollision.Skin, 4 - SceneCollision.Skin)) < .03f,
|
||||
"corner remains stable under sustained input");
|
||||
}
|
||||
|
||||
static void Ceiling()
|
||||
{
|
||||
var scene = new SceneCollision(new Builder().Floor(0, -100, 100, -100, 100)
|
||||
.Floor(6, -100, 100, -100, 100).Build());
|
||||
var rise = scene.Move(new Vector3(0, .1f, 0), new Vector3(0, 100, 0), 1, 4);
|
||||
Check(rise.HitCeiling && !rise.Grounded, "ceiling hit classification");
|
||||
Near(rise.Position.Y, 2 - SceneCollision.Skin, .02f, "head stops below underside");
|
||||
var descend = scene.Move(rise.Position, new Vector3(0, -20, 0), 1, 4);
|
||||
Check(descend.Grounded, "leaves underside and lands");
|
||||
Near(descend.Position.Y, SceneCollision.Skin, .02f, "underside does not trap actor");
|
||||
}
|
||||
|
||||
static void Slope()
|
||||
{
|
||||
var builder = new Builder().Quad(new(-30, -15, -30), new(-30, -15, 30), new(30, 15, 30), new(30, 15, -30));
|
||||
var scene = new SceneCollision(builder.Build());
|
||||
Near(scene.GroundHeight(new Vector3(4, 10, 0), 100)!.Value, 2, .0001f, "slope height");
|
||||
var landed = scene.Move(new Vector3(0, 20, 0), new Vector3(0, -30, 0), 1, 4);
|
||||
Check(landed.Grounded, "sloped landing");
|
||||
// Sliding projects the remaining downward displacement onto the slope.
|
||||
Check(landed.Position.Y >= landed.Position.X * .5f - .01f, "slope cannot penetrate");
|
||||
var position = scene.Move(Vector3.Zero, Vector3.Zero, 1, 4).Position;
|
||||
for (var i = 0; i < 20; i++)
|
||||
{
|
||||
var move = scene.Move(position, new Vector3(.25f, -.01f, 0), 1, 4);
|
||||
Check(move.Grounded, "slope support while climbing");
|
||||
Check(move.Position.Y >= move.Position.X * .5f, "climb above plane");
|
||||
position = move.Position;
|
||||
}
|
||||
Check(position.X > 3 && position.Y > 1.5f, "walk uphill");
|
||||
}
|
||||
|
||||
static void Edge()
|
||||
{
|
||||
var scene = new SceneCollision(new Builder().Floor(0, -10, 0, -10, 10).Build());
|
||||
var standing = scene.Move(new Vector3(-2, 0, 0), Vector3.Zero, 1, 4);
|
||||
var moved = scene.Move(standing.Position, new Vector3(5, 0, 0), 1, 4);
|
||||
Check(!moved.Grounded, "support is evaluated at final position");
|
||||
var falling = scene.Move(moved.Position, new Vector3(0, -5, 0), 1, 4);
|
||||
Check(!falling.Grounded && falling.Position.Y < -4, "fall beyond ledge");
|
||||
}
|
||||
|
||||
static void TriangleEdges()
|
||||
{
|
||||
var scene = new SceneCollision(new Builder().Triangle(new(5, 0, 0), new(5, 8, 0), new(5, 0, 8)).Build());
|
||||
var edge = scene.Move(new Vector3(0, 0, -.5f), new Vector3(1000, 0, 0), 1, 4);
|
||||
Check(edge.HitWall && edge.Position.Z < -.5f, $"capsule catches and slides around triangle edge with center outside face: {edge}");
|
||||
var miss = scene.Move(new Vector3(0, 0, -2), new Vector3(10, 0, 0), 1, 4);
|
||||
Near(miss.Position.X, 10, .0001f, "clears isolated edge");
|
||||
var camera = scene.ClipCamera(new Vector3(0, -.5f, -.5f), new Vector3(10, -.5f, -.5f), 1);
|
||||
Check(camera.X < 5, "sphere catches triangle vertex");
|
||||
}
|
||||
|
||||
static void Camera()
|
||||
{
|
||||
var scene = new SceneCollision(new Builder().WallX(5, -10, 10, -10, 10).Build());
|
||||
var clipped = scene.ClipCamera(Vector3.Zero, new Vector3(1000, 0, 0), .5f);
|
||||
Near(clipped.X, 4.5f - SceneCollision.Skin, .02f, "camera clips wall");
|
||||
var free = scene.ClipCamera(Vector3.Zero, new Vector3(-100, 0, 0), .5f);
|
||||
Near(free.X, -100, .001f, "camera clear path");
|
||||
}
|
||||
|
||||
static void TranslatedRoom()
|
||||
{
|
||||
var scene = new SceneCollision(new Builder().Floor(.5486332f, 4000, 6000, -2000, 3000)
|
||||
.WallX(5430, .5486332f, 1000, -2000, 3000)
|
||||
.WallZ(2433, .5486332f, 1000, 4000, 6000).Build());
|
||||
var start = new Vector3(5071.933f, .5486332f, 824.10394f);
|
||||
var fast = scene.Move(start, new Vector3(100000, -100000, 100000), 34.7f, 209.4361f);
|
||||
Check(fast.Grounded && fast.HitWall, $"translated room fast collision: {fast}");
|
||||
Check(fast.Position.X <= 5430 - 34.7f + .02f && fast.Position.Z <= 2433 - 34.7f + .02f, "translated room walls");
|
||||
Near(fast.Position.Y, .5486332f + SceneCollision.Skin, .02f, "translated room floor");
|
||||
var position = scene.Move(start, Vector3.Zero, 34.7f, 209.4361f).Position;
|
||||
for (var i = 0; i < 2000; i++)
|
||||
{
|
||||
var movement = new Vector3(i % 400 < 200 ? 2 : -2, -.3f, i % 200 < 100 ? 2 : -2);
|
||||
var moved = scene.Move(position, movement, 34.7f, 209.4361f);
|
||||
Check(moved.Grounded, "translated floor repeated support");
|
||||
Near(moved.Position.Y, .5486332f + SceneCollision.Skin, .02f, "translated floor repeated height");
|
||||
position = moved.Position;
|
||||
}
|
||||
}
|
||||
|
||||
static void SourcePolicy()
|
||||
{
|
||||
var mesh = new Builder().WallX(5, 0, 20, -20, 20).Build() with { HasExplicitDraws = true, Draws = [] };
|
||||
var scene = new SceneCollision(mesh);
|
||||
Check(scene.TriangleCount == 2, "unrendered source proxy retained");
|
||||
Check(scene.Move(Vector3.Zero, new Vector3(100, 0, 0), 1, 4).HitWall, "proxy blocks under provisional policy");
|
||||
var degenerate = new SceneCollision(new Builder().Triangle(Vector3.Zero, Vector3.One, Vector3.One).Build());
|
||||
Check(degenerate.TriangleCount == 0, "degenerate triangles skipped");
|
||||
Throws<ArgumentOutOfRangeException>(() => scene.Move(Vector3.Zero, Vector3.Zero, 0, 4));
|
||||
Throws<ArgumentOutOfRangeException>(() => scene.Move(Vector3.Zero, Vector3.Zero, 3, 4));
|
||||
Throws<ArgumentOutOfRangeException>(() => scene.Move(new Vector3(float.NaN, 0, 0), Vector3.Zero, 1, 4));
|
||||
Throws<ArgumentException>(() => new SceneCollision(new Mesh([], [0, 1, 2])));
|
||||
Throws<ArgumentException>(() => new SceneCollision(new Builder().Triangle(Vector3.Zero,
|
||||
new Vector3(float.PositiveInfinity, 0, 0), Vector3.One).Build()));
|
||||
Throws<ArgumentException>(() => new SceneCollision(new Builder().Triangle(Vector3.Zero,
|
||||
new Vector3(1e30f, 0, 0), new Vector3(0, 1e30f, 0)).Build()));
|
||||
}
|
||||
|
||||
static void OriginalLab()
|
||||
{
|
||||
var root = FindRoot();
|
||||
var file = Path.Combine(root, "artifacts/extracted/level1b/scene.json");
|
||||
Check(File.Exists(file), "prepared original lab required (run extractors/prepare.py)");
|
||||
var scene = new SceneCollision(Mesh.LoadSceneJson(file));
|
||||
Check(scene.TriangleCount == 2869, "all original lab triangles included");
|
||||
var originalRoot = new Vector3(5071.933105f, 26.661896f, 824.103943f);
|
||||
var floor = scene.GroundHeight(originalRoot, 100)!.Value;
|
||||
Near(floor, .5486332f, .001f, "actual lab floor below GAM root");
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
foreach (var radius in new[] { 24f, 34.7f })
|
||||
{
|
||||
var feet = new Vector3(originalRoot.X, floor, originalRoot.Z);
|
||||
var standing = scene.Move(feet, Vector3.Zero, radius, 209.4361f);
|
||||
Check(standing.Grounded, $"actual lab standing radius {radius}");
|
||||
Check(Vector3.Distance(standing.Position, feet) < .2f, $"lab spawn does not need horizontal relocation radius {radius}");
|
||||
var first = Traverse(scene, standing.Position, radius);
|
||||
var second = Traverse(scene, standing.Position, radius);
|
||||
Check(first == second, "same input stream replays identically");
|
||||
Check(Vector3.Distance(first, standing.Position) > 100, $"actual lab traversal radius {radius}");
|
||||
// This direction reaches original geometry near the east side of the
|
||||
// room, rather than an invented room-sized bounding box.
|
||||
var east = standing.Position;
|
||||
var hitOriginalWall = false;
|
||||
for (var i = 0; i < 600; i++)
|
||||
{
|
||||
var moved = scene.Move(east, new Vector3(3, -.3f, 0), radius, 209.4361f);
|
||||
hitOriginalWall |= moved.HitWall;
|
||||
Check(Finite(moved.Position), "original wall traversal finite");
|
||||
Check(moved.Position.Y > floor - .05f, "original wall traversal above lab floor");
|
||||
east = moved.Position;
|
||||
}
|
||||
Check(hitOriginalWall && east.X < standing.Position.X + 1750, $"original east wall blocks radius {radius}");
|
||||
var position = standing.Position;
|
||||
var velocity = 550f;
|
||||
var reachedAir = false;
|
||||
var landed = false;
|
||||
for (var i = 0; i < 180; i++)
|
||||
{
|
||||
velocity -= 1064f / 60f;
|
||||
var moved = scene.Move(position, new Vector3(0, velocity / 60f, 0), radius, 209.4361f);
|
||||
Check(Finite(moved.Position), "lab jump finite");
|
||||
if (moved.HitCeiling && velocity > 0) velocity = 0;
|
||||
if (!moved.Grounded) reachedAir = true;
|
||||
if (reachedAir && moved.Grounded && velocity < 0) { landed = true; position = moved.Position; break; }
|
||||
position = moved.Position;
|
||||
}
|
||||
Check(reachedAir && landed, $"lab jump rises and lands radius {radius}");
|
||||
Near(position.Y, floor + SceneCollision.Skin, .03f, "lab landing above floor");
|
||||
Console.WriteLine($" lab radius={radius}: start={standing.Position}, traverse={first}, eastWall={east}, jumpLand={position}");
|
||||
}
|
||||
Console.WriteLine($" lab replay and jump checks {stopwatch.ElapsedMilliseconds} ms");
|
||||
}
|
||||
|
||||
static Vector3 Traverse(SceneCollision scene, Vector3 position, float radius)
|
||||
{
|
||||
var angle = 155f * MathF.PI / 180;
|
||||
var forward = new Vector3(-MathF.Sin(angle), 0, MathF.Cos(angle));
|
||||
for (var i = 0; i < 240; i++)
|
||||
{
|
||||
var moved = scene.Move(position, forward * (180f / 60) - Vector3.UnitY * .3f, radius, 209.4361f);
|
||||
Check(Finite(moved.Position), "lab traversal finite");
|
||||
var below = scene.GroundHeight(moved.Position + Vector3.UnitY * .01f, 1000);
|
||||
if (below.HasValue) Check(moved.Position.Y >= below.Value - .02f, "lab traverse stays above nearby floor");
|
||||
position = moved.Position;
|
||||
}
|
||||
return position;
|
||||
}
|
||||
|
||||
static string FindRoot()
|
||||
{
|
||||
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 p) => float.IsFinite(p.X) && float.IsFinite(p.Y) && float.IsFinite(p.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 Throws<T>(Action check) where T : Exception
|
||||
{
|
||||
try { check(); } catch (T) { return; }
|
||||
throw new Exception($"Expected {typeof(T).Name}");
|
||||
}
|
||||
|
||||
sealed class Builder
|
||||
{
|
||||
private readonly List<MeshVertex> vertices = [];
|
||||
private readonly List<uint> indices = [];
|
||||
public Builder Triangle(Vector3 a, Vector3 b, Vector3 c)
|
||||
{
|
||||
var offset = (uint)vertices.Count;
|
||||
vertices.Add(new(a, Vector2.Zero, Vector3.One));
|
||||
vertices.Add(new(b, Vector2.Zero, Vector3.One));
|
||||
vertices.Add(new(c, Vector2.Zero, Vector3.One));
|
||||
indices.AddRange([offset, offset + 1, offset + 2]);
|
||||
return this;
|
||||
}
|
||||
public Builder Quad(Vector3 a, Vector3 b, Vector3 c, Vector3 d) => Triangle(a, b, c).Triangle(a, c, d);
|
||||
public Builder Floor(float y, float left, float right, float back, float front) =>
|
||||
Quad(new(left, y, back), new(left, y, front), new(right, y, front), new(right, y, back));
|
||||
public Builder WallX(float x, float bottom, float top, float back, float front) =>
|
||||
Quad(new(x, bottom, back), new(x, top, back), new(x, top, front), new(x, bottom, front));
|
||||
public Builder WallZ(float z, float bottom, float top, float left, float right) =>
|
||||
Quad(new(left, bottom, z), new(left, top, z), new(right, top, z), new(right, bottom, z));
|
||||
public Mesh Build() => new(vertices.ToArray(), indices.ToArray());
|
||||
}
|
||||
@@ -14,7 +14,7 @@
|
||||
<Compile Include="../../src/GamePort/Assets/AseMesh.cs" Link="AseMesh.cs" />
|
||||
<Compile Include="../../src/GamePort/Assets/AssetPath.cs" Link="AssetPath.cs" />
|
||||
<Compile Include="../../src/GamePort/Assets/MorphAnimation.cs" Link="MorphAnimation.cs" />
|
||||
<Compile Include="../../src/GamePort/Game/RecoveredMotion.cs" Link="RecoveredMotion.cs" />
|
||||
<Compile Include="../../src/GamePort/Game/*.cs" />
|
||||
<Compile Include="../../src/GamePort/Application/PreviewCharacterPlacement.cs" Link="PreviewCharacterPlacement.cs" />
|
||||
<Compile Include="../../src/GamePort/Application/PreviewAnimationLibrary.cs" Link="PreviewAnimationLibrary.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="../../src/GamePort/Assets/*.cs" Link="Assets/%(Filename)%(Extension)" />
|
||||
<Compile Include="../../src/GamePort/Game/*.cs" Link="Game/%(Filename)%(Extension)" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,313 @@
|
||||
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 mode ignores arrow steering", AirControl),
|
||||
("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 AirControl()
|
||||
{
|
||||
var idleInput = FlatSimulation();
|
||||
var arrowInput = FlatSimulation();
|
||||
for (var i = 0; i < 20; i++)
|
||||
{
|
||||
idleInput.Step(new(true, false, false, false, false));
|
||||
arrowInput.Step(new(true, false, false, false, false));
|
||||
}
|
||||
idleInput.Step(new(true, false, false, false, true));
|
||||
arrowInput.Step(new(true, false, false, false, true));
|
||||
for (var i = 0; i < 24; i++)
|
||||
{
|
||||
Check(!idleInput.Grounded && !arrowInput.Grounded, "comparison remains airborne");
|
||||
idleInput.Step(default);
|
||||
arrowInput.Step(new(true, true, true, true, false));
|
||||
SameState(arrowInput, idleInput, "ordinary arrows ignored while airborne");
|
||||
}
|
||||
Near(arrowInput.Speed, 600, 0, "takeoff speed retained in air");
|
||||
}
|
||||
|
||||
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");
|
||||
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}");
|
||||
}
|
||||
Reference in new issue
Block a user