using System.Numerics; using System.Text.Json; using GamePort.Game; var count = 0; void Check(bool value, string message) { if (!value) throw new Exception(message); } void Near(float actual, float expected, float tolerance, string message) => Check(MathF.Abs(actual - expected) <= tolerance, $"{message}: {actual} != {expected}"); void Test(string name, Action action) { action(); count++; Console.WriteLine($"PASS {name}"); } SupportTriangle Ground(float height = 0, int id = 1) => new(new(-10000, height, -10000), new(10000, height, -10000), new(0, height, 10000), 1, id); SupportHit? Query(Vector3 start, Vector3 end, bool sides) => OriginalBodySupport.Trace([Ground()], start, end, sides); SupportBody Body(float y = 20) => new(new(0, y, 0), new(0, y, 0), new(80, -16, 20), new(2, -800, 3), OriginalBodySupport.JimmyRadius, OriginalBodySupport.JimmyFactor); Test("quantized magnitude axes, signs, permutation and scale", () => { Near(OriginalBodySupport.QuickMagnitude(new(10, 0, 0)), 10, 0, "axis"); Near(OriginalBodySupport.QuickMagnitude(Vector3.Zero), 0, 0, "zero"); var magnitude = OriginalBodySupport.QuickMagnitude(new(3, 4, 0)); Near(magnitude, 5, .002f, "3-4-5 quantization"); Near(OriginalBodySupport.QuickMagnitude(new(-4, 0, -3)), magnitude, 0, "sign/permutation"); Near(OriginalBodySupport.QuickMagnitude(new(30, 40, 0)), magnitude * 10, .00001f, "scale"); var random = new Random(8414); for (var i = 0; i < 1000; i++) { var v = new Vector3(random.Next(-1000, 1001), random.Next(-1000, 1001), random.Next(-1000, 1001)); Near(OriginalBodySupport.QuickMagnitude(v) / v.Length(), 1, .001f, "lookup bound"); } }); Test("isolated original DLL magnitude float-bit references", () => { // Copied OMT2.dll under isolated Wine32, collision_mode2_probe.c,2026-10-24. foreach (var (vector, expected) in new (Vector3, uint)[] { (Vector3.Zero, 0x00000000), (new(10, 0, 0), 0x41200000), (new(3, 4, 0), 0x40a0001e), (new(-4, 0, -3), 0x40a0001e), (new(1, 1, 1), 0x3fddb16e), (new(500, 100, 300), 0x4413eb31), (new(.25f, .125f, .75f), 0x3f4cec46), (new(1.2345f, 678.125f, -90.75f), 0x442b0b21) }) { var bits = BitConverter.SingleToUInt32Bits(OriginalBodySupport.QuickMagnitude(vector)); Check(bits == expected, $"original magnitude bits for{vector}: {bits:x8} != {expected:x8}"); } }); Test("source winding and two-sided override", () => { var hit = Query(new(0, 20, 0), new(0, -20, 0), false); Check(hit.HasValue, "source floor winding rejected"); Near(hit!.Value.Position.Y, 0, .00001f, "floor intersection"); Check(Query(new(0, -20, 0), new(0, 20, 0), false) == null, "backface accepted"); Check(Query(new(0, -20, 0), new(0, 20, 0), true).HasValue, "two-sided ray rejected"); }); Test("strict segment endpoint and zero-length ray", () => { Check(Query(new(0, 20, 0), Vector3.Zero, false) == null, "endpoint must be excluded"); Check(Query(Vector3.Zero, new(0, -20, 0), false).HasValue, "beginning must be included"); Check(Query(Vector3.Zero, Vector3.Zero, false) == null, "zero ray hit"); Check(Query(new(50000, 20, 0), new(50000, -20, 0), false) == null, "outside triangle hit"); }); Test("nearest support retains first equal-distance source identity", () => { var hit = OriginalBodySupport.Trace([Ground(0, 1), Ground(10, 2), Ground(10, 3)], new(0, 20, 0), new(0, -20, 0)); Check(hit?.PolygonId == 2, "nearest/tie source identity changed"); }); Test("support root correction and horizontal state preservation", () => { var result = OriginalBodySupport.Resolve(Body(), false, Query); Check(result.Supported && result.Landed, "support/landing missing"); Near(result.Body.Position.Y, 75.9f, .00001f, "recovered root clearance"); Check(result.Body.Velocity == new Vector3(80, 0, 20), "horizontal velocity changed"); Check(result.Body.Acceleration == new Vector3(2, 0, 3), "horizontal acceleration changed"); Check(result.SupportAnchor == new Vector3(0, result.Body.Position.Y, 0), "support anchor"); Check(!OriginalBodySupport.Resolve(Body(), true, Query).Landed, "repeated grounded landing"); }); Test("prevent-lowering flag compares root height independently of velocity", () => { var body = Body(80); var guarded = OriginalBodySupport.Resolve(body, false, Query); Check(!guarded.Supported && guarded.Body == body, "falling high root snapped down"); body = body with { Velocity = new(0, 550, 0) }; Check(!OriginalBodySupport.Resolve(body, false, Query).Supported, "ascending root grounded"); var unguarded = OriginalBodySupport.Resolve(body with { PreventLowering = false }, false, Query); Check(unguarded.Supported, "disabled guard did not lower root"); Near(unguarded.Body.Position.Y, 75.9f, .00001f, "unguarded offset"); }); Test("factor .1 boundary and distinct query/final offsets", () => { foreach (var (factor, clearance) in new[] { (.099f, 100f), (.1f, 10f), (.3f, 30f), (1f, 100f) }) { var calls = 0; Vector3 secondStart = default, secondEnd = default; SupportHit? Capture(Vector3 start, Vector3 end, bool sides) { Check(!sides, "support requested two-sided"); if (++calls == 1) return null; secondStart = start; secondEnd = end; return new(Vector3.Zero, 4, 8, start.Y); } var result = OriginalBodySupport.Resolve(Body(0) with { Radius = 100, Factor = factor }, false, Capture); Check(calls == 2 && result.Supported, "support query count"); Near(secondStart.Y, clearance, .00001f, "query clearance"); Near(secondEnd.Y, -200, 0, "query lower endpoint"); Near(result.Body.Position.Y, 100 * factor, .00001f, "final offset always product"); } }); Test("crossed geometry raises support query by remaining motion", () => { var body = Body(40) with { Position = new(20, 40, 0), PreviousPosition = new(0, 50, 0) }; var calls = 0; var crossing = new Vector3(10, 45, 0); var expectedTop = 50 + OriginalBodySupport.QuickMagnitude(body.Position - body.PreviousPosition) - OriginalBodySupport.QuickMagnitude(crossing - body.PreviousPosition) + body.Radius * body.Factor; SupportHit? Capture(Vector3 start, Vector3 end, bool sides) { if (++calls == 1) { Check(start == body.PreviousPosition && end == body.Position, "motion segment ordering"); return new(crossing, 1, 1, 0); } Near(start.Y, expectedTop, .0001f, "obstruction lift"); return null; } Check(!OriginalBodySupport.Resolve(body, false, Capture).Supported && calls == 2, "missing floor fabricated support"); }); Test("disabled geometry does not query or mutate state", () => { var body = Body(); SupportHit? Fail(Vector3 start, Vector3 end, bool sides) => throw new Exception("disabled query invoked"); var result = OriginalBodySupport.Resolve(body, true, Fail, false); Check(!result.Supported && !result.Landed && result.Body == body, "disabled body mutation"); }); Test("invalid input rejected", () => { foreach (var body in new[] { Body() with { Radius = 0 }, Body() with { Factor = float.NaN }, Body() with { Position = new(float.PositiveInfinity, 0, 0) } }) { var threw = false; try { OriginalBodySupport.Resolve(body, false, Query); } catch (ArgumentOutOfRangeException) { threw = true; } Check(threw, "invalid body accepted"); } }); Test("flat-floor repeated gravity and airborne jump", () => { var body = Body(75.9f) with { Velocity = Vector3.Zero, Acceleration = Vector3.Zero }; var wasGrounded = true; for (var i = 0; i < 600; i++) { var velocity = body.Velocity + new Vector3(0, -800f / 60, 0); body = body with { PreviousPosition = body.Position, Position = body.Position + velocity / 60, Velocity = velocity }; var result = OriginalBodySupport.Resolve(body, wasGrounded, Query); Check(result.Supported && !result.Landed, "gravity support/landing stability"); body = result.Body; wasGrounded = result.Supported; Near(body.Position.Y, 75.9f, .00001f, "root drift"); } body = body with { PreviousPosition = body.Position, Position = body.Position + new Vector3(0, 550f / 60, 0), Velocity = new(0, 550, 0) }; Check(!OriginalBodySupport.Resolve(body, true, Query).Supported, "jump was grounded"); }); Test("original lab triangles retain invisible geometry and recover spawn support", () => { var path = Path.GetFullPath("artifacts/extracted/level1b/scene.json"); using var document = JsonDocument.Parse(File.ReadAllText(path)); var root = document.RootElement; var vertices = root.GetProperty("vertices").EnumerateArray().Select(v => new Vector3(v[0].GetSingle(), v[1].GetSingle(), v[2].GetSingle())).ToArray(); var indices = root.GetProperty("indices").EnumerateArray().Select(v => v.GetInt32()).ToArray(); var triangles = Enumerable.Range(0, indices.Length / 3).Select(i => new SupportTriangle( vertices[indices[3 * i]], vertices[indices[3 * i + 1]], vertices[indices[3 * i + 2]], 0, i)).ToArray(); Check(triangles.Length == 2869, "source geometry count"); var position = new Vector3(5071.93310546875f, 26.661895751953125f, 824.1039428710938f); SupportHit? Lab(Vector3 start, Vector3 end, bool sides) => OriginalBodySupport.Trace(triangles, start, end, sides); var result = OriginalBodySupport.Resolve(Body() with { Position = position, PreviousPosition = position }, false, Lab); Check(result.Supported, "actual lab spawn has no support"); Near(result.Hit!.Value.Position.Y, .5486332f, .001f, "actual floor hit"); Near(result.Body.Position.Y, 76.448635f, .001f, "PostLoad root support prediction"); }); Console.WriteLine($"{count} original support groups passed; full original body collision/runtime fidelity remain unverified.");