acdream/tests/AcDream.Core.Tests/Physics/FindEnvCollisionsMultiCellTests.cs
Erik dbfbf8506c T6 (BR-7) C3: per-cell shadow architecture - flood registration, building channel, per-cell query; b3ce505 stopgap DELETED (closes #99)
The A6.P4 port, fused into one installment per the BR-2 half-port lesson
(registration and query are co-dependent: flood-registering shells under
the old radial query would re-open #98 through the vestibule).

REGISTRATION (ShadowObjectRegistry rewritten):
- Register/RegisterMultiPart/UpdatePosition compute the cell set via
  CellTransit.BuildShadowCellSet (the C2 find_cell_list flood) seeded by
  the entity's m_position cell id; the private 24m XY-grid rectangle and
  its single-landblock clamp are deleted. Flood spheres follow retail's
  CylSphere rule (base point + cyl radius, cap 10; BSP bounding-sphere
  fallback - Ghidra 0x0052b9f0). Statics flood with the do_not_load
  prune; dynamics (server spawns, isStatic:false) without.
- Keep-when-empty (SetPositionInternal num_cells gate, pc:283540): a
  failed flood leaves the previous registration in place.
- RefloodLandblock: streaming-race hook re-runs the flood when a
  landblock's cells hydrate (retail init_objects -> recalc_cross_cells,
  Ghidra 0x0052b420/0x00515a30); wired at GameWindow's hydration tail.
- GameWindow sites pass the server position's full cell id as the seed
  (spawn + UpdatePosition); the five static sites pass ParentCellId.

BUILDING CHANNEL (CSortCell.building shape):
- Building SHELLS are not shadow objects in retail (only caller of
  find_building_collisions is CSortCell::find_collisions 0x005340aa;
  one building per origin landcell, init_buildings 0x0052fd80 verified
  verbatim + ACE cross-ref). IsBuildingShell entities skip the registry;
  Transition.FindBuildingCollisions runs the shell part-0 BSP off
  cache.GetBuilding(cellId) with bldg_check set around it
  (find_building_collisions 0x006b5300), CollidedWithEnvironment on
  non-Contact non-OK. BuildingPhysics.ModelId = pre-resolved part-0
  GfxObj (0x02 Setups resolved at the CacheBuilding site).
- Placement/ethereal weakening: BSPQuery Path 1 passes center_solid=0
  when BldgCheck && HitsInteriorCell (BSPTREE::find_collisions 0x0053a82e
  + placement_insert 0x005399d8) so doorway crossings don't hard-fail
  against shell solids. SpherePath gains both retail fields;
  HitsInteriorCell is rebuilt at every cell-array build
  (build_cell_array reset 0x00509ef2 + find_cell_list/check_building_
  transit set sites).

QUERY (retail per-cell order, transitional_insert 0x0050b6f0):
- TransitionalInsert per attempt: env -> building (LandCell only) ->
  objects on the PRIMARY cell, then on OK the check_other_cells pass
  (env -> building -> objects per OTHER overlapped cell) + the
  carried-cell advance - the advance now happens AFTER all per-cell
  object passes (the WF1 ordering divergence), with Adjusted/Slid
  feeding the retry exactly like retail's OK_TS case.
- FindObjCollisionsInCell = CObjCell::find_obj_collisions (0x0052b750):
  iterate ONLY the asked cell's list. DELETED: the radial 9-landblock
  sweep, the +5m query pad, the b3ce505 indoor-primary gate, and the
  isViewer exemption (the camera is bounded by interior cell-BSP env
  collision - retail's own channel; CameraCornerSealReplayTests pins it
  against real dat, and the new building-channel camera test pins the
  outdoor stop).

TESTS: Core 1416/0/2 (was 1398 + 4 pre-existing #99-era fails + 1 skip),
App 225, UI 420, Net 294 - all green.
- 3 of the 4 #99-era reds flipped green as designed: the door apparatus
  (Apparatus_Grounded_50cmOffCenter_FrontApproach_Blocks) and tick-13558
  (indoor walkthrough) now assert the door BLOCKS; tick-22760 pins the
  outdoor blocking invariant.
- The 4th (BSPStepUp D4) + 22760's lateral-slide delta are NOT cell-set
  problems (probes prove the door is found + BSP-only dispatched;
  BR-7 left both byte-identical) - filed as issue #116 (slide-response
  family), D4 skipped with the issue reference.
- FindEnvCollisionsMultiCellTests migrated to the public entry (the A4
  multi-cell halt now lives at the retail call site).
- New registry pins: per-cell query surface, outdoor-footprint-never-
  indoor (#98 architectural), door-outdoor-cell membership, reflood.
- CameraCollisionIndoorTests rewritten against the building channel
  (the isViewer-exemption pins died with the exemption).

Closes #99 (doors block both ways via registration-time cell membership
+ the straddle-spanning player cell array). #97 likely closed (the +5m
radial pad that produced phantom-collision candidates is gone) - verify
at T5. #98 stays closed ARCHITECTURALLY (outdoor footprints structurally
cannot reach interior cells; the cellar harness stays green).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 14:37:50 +02:00

149 lines
7.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Numerics;
using DatReaderWriter.Enums;
using DatReaderWriter.Types;
using AcDream.Core.Physics;
using Xunit;
namespace AcDream.Core.Tests.Physics;
/// <summary>
/// End-to-end test that the indoor branch of
/// <see cref="Transition.FindEnvCollisions"/> queries the cells the
/// sphere overlaps, not just the cell whose CellBSP contains the
/// sphere center. This is the core Phase A4 behaviour test — the
/// Holtburg inn vestibule (cell 0xA9B40164) bug reduced to a minimal
/// synthetic fixture.
/// </summary>
public class FindEnvCollisionsMultiCellTests
{
// Indoor cell IDs — both have low-byte ≥ 0x100 to trigger the
// indoor branch of FindEnvCollisions. Vestibule has the lower id so
// CellTransit.FindCellSet's sorted iteration encounters it first.
private const uint VestibuleCellId = 0xA9B40157u;
private const uint InteriorCellId = 0xA9B40164u;
private static CellBSPTree LeafCellBsp() => new CellBSPTree
{
Root = new CellBSPNode { Type = BSPNodeType.Leaf },
};
private static PhysicsBSPTree EmptyLeafBsp() => new PhysicsBSPTree
{
Root = new PhysicsBSPNode
{
Type = BSPNodeType.Leaf,
BoundingSphere = new Sphere { Origin = Vector3.Zero, Radius = 10f },
}
};
[Fact]
public void IndoorSphereOverlappingAdjacentCellWithWall_HaltsTransition()
{
// ── Secondary cell (interior) ─────────────────────────────────────
// Reuse BSPStepUp's TallWall fixture — proven to halt a grounded mover
// that can't scale it (test B2_GroundedMover_TallWall_BlockedOrSlides).
// Wall is at interior-local x=0.5. Translate the interior cell by
// +0.3 in world X so the wall ends up at world x=0.8, within reach
// of a sphere walking from x=0.1 toward x=0.6 (sphere radius 0.2).
var (wallRoot, wallResolved) = BSPStepUpFixtures.TallWall();
var interiorWT = Matrix4x4.CreateTranslation(new Vector3(0.3f, 0f, 0f));
Matrix4x4.Invert(interiorWT, out var interiorInv);
var interior = new CellPhysics
{
BSP = new PhysicsBSPTree { Root = wallRoot },
WorldTransform = interiorWT,
InverseWorldTransform = interiorInv,
Resolved = wallResolved,
CellBSP = LeafCellBsp(),
};
// ── Primary cell (vestibule) ──────────────────────────────────────
// Empty PhysicsBSP — no walls of its own. CellBSP contains a portal
// at world x=0.5 (vestibule-local x=0.5 since vestibule WorldTransform
// = Identity) leading to the interior cell. The sphere's foot reaches
// the portal at world x=0.5 during the sweep — that triggers
// CellTransit.FindCellSet to add the interior to the candidate set.
var portalPoly = new ResolvedPolygon
{
Vertices = new[]
{
new Vector3(0.5f, -2.5f, 0f),
new Vector3(0.5f, 2.5f, 0f),
new Vector3(0.5f, 2.5f, 5f),
new Vector3(0.5f, -2.5f, 5f),
},
Plane = new Plane(new Vector3(1f, 0f, 0f), -0.5f),
NumPoints = 4,
SidesType = CullMode.None,
};
var vestibule = new CellPhysics
{
BSP = EmptyLeafBsp(),
WorldTransform = Matrix4x4.Identity,
InverseWorldTransform = Matrix4x4.Identity,
Resolved = new Dictionary<ushort, ResolvedPolygon>(),
CellBSP = LeafCellBsp(),
PortalPolygons = new Dictionary<ushort, ResolvedPolygon> { [10] = portalPoly },
Portals = new[]
{
new PortalInfo(otherCellId: (ushort)(InteriorCellId & 0xFFFFu),
polygonId: 10, flags: 0),
},
};
// ── Engine + cache ────────────────────────────────────────────────
var engine = new PhysicsEngine();
engine.DataCache = new PhysicsDataCache();
// Provide a flat terrain strip at z=0 so FindEnvCollisions's outdoor
// fall-through has something to sample if it ever fires.
var heights = new byte[81];
Array.Fill(heights, (byte)0);
var ht = new float[256];
for (int i = 0; i < 256; i++) ht[i] = i * 1.0f;
engine.AddLandblock(0xA9B4FFFFu, new TerrainSurface(heights, ht),
Array.Empty<CellSurface>(), Array.Empty<PortalPlane>(),
worldOffsetX: 0f, worldOffsetY: 0f);
engine.DataCache.RegisterCellStructForTest(VestibuleCellId, vestibule);
engine.DataCache.RegisterCellStructForTest(InteriorCellId, interior);
// ── Transition ────────────────────────────────────────────────────
// Grounded mover, foot at world x=0.1 walking to x=0.7. The sphere
// (radius 0.2, center at foot + 0.2 in Z) ends with its center at
// world x=0.7 = interior-local x=0.4 (since interior translation is
// +0.3). The TallWall sits at interior-local x=0.5 with normal -X —
// the sphere reach (0.4 + 0.2 = 0.6) penetrates the wall by 0.1.
// StepUpHeight 0.04 means the mover can't scale the 5m TallWall.
// MakeGroundedTransition seeds Contact + OnWalkable +
// LastKnownContactPlane so Path 5 fires for any wall the BSP query
// encounters.
var from = new Vector3(0.1f, 0f, 0f);
var to = new Vector3(0.7f, 0f, 0f);
var t = BSPStepUpFixtures.MakeGroundedTransition(from, to,
stepUpHeight: 0.04f,
cellId: VestibuleCellId);
// ── Act ───────────────────────────────────────────────────────────
// BR-7 / A6.P4 (2026-06-11): the other-cells pass moved from
// FindEnvCollisions into TransitionalInsert Phase 2.5 (retail
// transitional_insert's OK_TS case, Ghidra 0x0050b756: the primary
// insert runs env → building → objects, THEN check_other_cells).
// Drive the public entry so the full per-attempt order runs.
t.FindTransitionalPosition(engine);
// ── Assert ────────────────────────────────────────────────────────
// Pre-A4: empty vestibule BSP returns OK, interior is never queried,
// and the sphere walks through the wall to x=0.7.
// Post-A4 (now via Phase 2.5's CheckOtherCells): the interior cell's
// TallWall halts/slides the sphere — its center cannot pass
// wall-X (0.8 world) minus the sphere radius (0.2) = 0.6.
Assert.True(t.SpherePath.CurPos.X <= 0.6f + PhysicsGlobals.EPSILON * 20f,
$"Adjacent cell's wall must block the sphere at world x≈0.6; " +
$"CurPos.X={t.SpherePath.CurPos.X:F4} (walked through = A4 regression).");
}
}