acdream/tests/AcDream.Core.Tests/Physics/PhysicsDiagnosticsTests.cs
Erik 66dc23e087 feat(phys L.2d slice 1): BSP-hit diagnostic probe + plan-of-record correction
Adds ACDREAM_PROBE_BUILDING — a read-only per-shadow-entry probe that
captures full BSP collision evidence whenever TransitionTypes.FindObjCollisions
attributes a hit (via the existing L.2a slice 3 chain). One multi-line
[resolve-bldg] entry per attributed hit: partIdx, hasPhys, bspR vs
vAabbR, world-space entOrigin_lb, and the actual hit polygon's vertices
in both object-local and world space.

Paired with a one-time [entity-source] line at every ShadowObjects.Register
call site in GameWindow so entityId from a probe line is greppable to its
WorldEntity source within a single log file.

Plumbing: BSPQuery writes the resolved hit polygon to a new
PhysicsDiagnostics.LastBspHitPoly side-channel at the 5 SetCollisionNormal
sites in Paths 5/6 + CollideWithPt. TransitionTypes clears that field
before each shadow-entry dispatch and reads it back at the L.2a slice 3
attribution site to emit the probe line.

Spec component 4 originally described an out ResolvedPolygon? parameter
on BSPQuery.FindCollisions; the static side-channel achieves the same
observable behavior without plumbing through BSPQuery's recursive private
methods. Deviation noted in PhysicsDiagnostics.LastBspHitPoly's XML doc.

Reframes the plan-of-record's L.2d sub-direction paragraph: the 2026-05-12
handoff proposed porting CBuildingObj + per-cell walkability, but ACE
BuildingObj.cs:39-52 + named-retail acclient_2013_pseudo_c.txt:701260
show find_building_collisions is one BSP test on Parts[0]. Per-cell
walkability belongs to L.2e, not L.2d. L.2d slice 1 is the diagnostic;
slice 2 is the actual fix scoped from slice 1's evidence (one of three
hypotheses: wrong BSP loaded / over-registered parts / BSPQuery flaw).

Tests: 2 synthetic unit tests in PhysicsDiagnosticsTests.cs pin the
static API contract that the BSPQuery → side-channel → TransitionTypes
emission chain depends on. The multi-line line format itself is verified
by acceptance criterion 2 (live Holtburg-doorway capture) — covering it
here would require a heavy PhysicsEngine + Transition fixture for a
diagnostic-only emission.

Verified: dotnet build green; the 2 new tests pass; the 8 pre-existing
test failures listed in the L.2a handoff (MotionInterpreter GetMaxSpeed_*,
PositionManager.ComputeOffset_BothActive_Combined,
PlayerMovementController.Update_ForwardInput_*, Dispatcher.W_held_*,
BSPStepUpTests.{D4,C3}) remain failing — none introduced by this slice.

Spec: docs/superpowers/specs/2026-05-13-l2d-cbuildingobj-collision-design.md
Conformance anchors:
- acclient_2013_pseudo_c.txt:701260 (CBuildingObj::find_building_collisions)
- acclient_2013_pseudo_c.txt:323725 (BSPTREE::find_collisions)
- ACE references/ACE/Source/ACE.Server/Physics/Common/BuildingObj.cs:39-52

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-12 19:14:34 +02:00

98 lines
3.6 KiB
C#

using AcDream.Core.Physics;
using DatReaderWriter.Enums;
using System.Numerics;
using Xunit;
namespace AcDream.Core.Tests.Physics;
/// <summary>
/// L.2d slice 1 (2026-05-13) — unit coverage for the new
/// <see cref="PhysicsDiagnostics.ProbeBuildingEnabled"/> flag and
/// <see cref="PhysicsDiagnostics.LastBspHitPoly"/> diagnostic
/// side-channel.
///
/// <para>
/// The full multi-line <c>[resolve-bldg]</c> format itself is verified
/// by the slice's acceptance criterion #2 (live Holtburg-doorway
/// capture) — covering it here would require a heavy
/// <c>PhysicsEngine</c> + <c>ShadowObjectRegistry</c> + <c>Transition</c>
/// fixture for what's a diagnostic-only emission. These tests pin the
/// static API contract that the emission code depends on; if either of
/// these tests breaks the emission will start producing stale data or
/// failing to emit at all.
/// </para>
/// </summary>
public class PhysicsDiagnosticsTests
{
// -----------------------------------------------------------------------
// ProbeBuildingEnabled — flag gates the emission path.
// -----------------------------------------------------------------------
[Fact]
public void ProbeBuilding_StaticApi_Roundtrip()
{
bool initial = PhysicsDiagnostics.ProbeBuildingEnabled;
try
{
PhysicsDiagnostics.ProbeBuildingEnabled = true;
Assert.True(PhysicsDiagnostics.ProbeBuildingEnabled);
PhysicsDiagnostics.ProbeBuildingEnabled = false;
Assert.False(PhysicsDiagnostics.ProbeBuildingEnabled);
}
finally
{
// Restore so a process-wide static doesn't leak between tests
// (env-var init was the only thing that set this before).
PhysicsDiagnostics.ProbeBuildingEnabled = initial;
}
}
// -----------------------------------------------------------------------
// LastBspHitPoly — side-channel set by BSPQuery, read by FindObjCollisions.
//
// TransitionTypes.FindObjCollisions clears this to null before each
// shadow-entry dispatch; BSPQuery writes to it on hit when the probe is
// on; the emission site reads it. A failure here means the side-channel
// can't carry data through the call chain.
// -----------------------------------------------------------------------
[Fact]
public void LastBspHitPoly_StaticApi_Roundtrip()
{
ResolvedPolygon? initial = PhysicsDiagnostics.LastBspHitPoly;
try
{
PhysicsDiagnostics.LastBspHitPoly = null;
Assert.Null(PhysicsDiagnostics.LastBspHitPoly);
var synthetic = new ResolvedPolygon
{
Vertices = new[]
{
new Vector3(-1f, 0f, 0f),
new Vector3( 1f, 0f, 0f),
new Vector3( 1f, 0f, 2f),
new Vector3(-1f, 0f, 2f),
},
Plane = new System.Numerics.Plane(0f, 1f, 0f, -94.123f),
NumPoints = 4,
SidesType = CullMode.None,
};
PhysicsDiagnostics.LastBspHitPoly = synthetic;
var read = PhysicsDiagnostics.LastBspHitPoly;
Assert.NotNull(read);
Assert.Equal(4, read!.NumPoints);
Assert.Equal(synthetic.Plane.D, read.Plane.D);
Assert.Same(synthetic, read);
PhysicsDiagnostics.LastBspHitPoly = null;
Assert.Null(PhysicsDiagnostics.LastBspHitPoly);
}
finally
{
PhysicsDiagnostics.LastBspHitPoly = initial;
}
}
}