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>
This commit is contained in:
Erik 2026-06-11 14:37:50 +02:00
parent abf36e2743
commit dbfbf8506c
15 changed files with 1109 additions and 856 deletions

View file

@ -114,49 +114,48 @@ public class ShadowObjectRegistryTests
}
// -----------------------------------------------------------------------
// GetNearbyObjects
// Per-cell query surface (BR-7 / A6.P4 2026-06-11): GetObjectsInCell IS
// the query — retail CObjCell::find_obj_collisions iterates only the
// asked cell's shadow_object_list (Ghidra 0x0052b750). The former
// radial GetNearbyObjects sweep is deleted; cell membership is the
// broad phase.
// -----------------------------------------------------------------------
[Fact]
public void GetNearbyObjects_QueryCoversEntity_ReturnsIt()
public void PerCellQuery_EntityCell_ReturnsIt()
{
var reg = new ShadowObjectRegistry();
reg.Register(10u, 0x01000005u, new Vector3(30f, 30f, 50f), Quaternion.Identity, 1f, OffX, OffY, LbId);
var results = new List<ShadowEntry>();
reg.GetNearbyObjects(new Vector3(30f, 30f, 50f), 5f, OffX, OffY, LbId, results);
// local (30,30) → landcell (1,1) → 1*8+1+1 = 10.
var results = reg.GetObjectsInCell(LbId | 10u);
Assert.Single(results);
Assert.Equal(10u, results[0].EntityId);
}
[Fact]
public void GetNearbyObjects_QueryFarFromEntity_ReturnsEmpty()
public void PerCellQuery_FarCell_ReturnsEmpty()
{
var reg = new ShadowObjectRegistry();
// Entity at local (12, 12) — cell (0,0).
reg.Register(11u, 0x01000006u, new Vector3(12f, 12f, 50f), Quaternion.Identity, 1f, OffX, OffY, LbId);
var results = new List<ShadowEntry>();
// Query at local (180, 180) — cell (7,7) — far away.
reg.GetNearbyObjects(new Vector3(180f, 180f, 50f), 5f, OffX, OffY, LbId, results);
Assert.Empty(results);
// Cell (7,7) — far away. No spatial radius can reach across cells.
Assert.Empty(reg.GetObjectsInCell(LbId | 64u));
}
[Fact]
public void GetNearbyObjects_EntityInMultipleCells_ReturnedOnce()
public void PerCellQuery_EntityInMultipleCells_OncePerCellList()
{
// Entity spans cells 0,0 and 1,0 (local X≈24, radius=2).
// Entity spans cells (0,0) and (1,0) (local X≈24, radius=2) via the
// flood's boundary-neighbor adds. Each overlapped cell's list holds
// the entry exactly once — retail tests an object once per iterated
// cell (no cross-cell dedup exists in find_obj_collisions).
var reg = new ShadowObjectRegistry();
reg.Register(20u, 0x01000007u, new Vector3(24f, 12f, 50f), Quaternion.Identity, 2f, OffX, OffY, LbId);
var results = new List<ShadowEntry>();
// Large query covers both cells; entity must appear exactly once.
reg.GetNearbyObjects(new Vector3(24f, 12f, 50f), 10f, OffX, OffY, LbId, results);
Assert.Single(results);
Assert.Equal(20u, results[0].EntityId);
Assert.Single(reg.GetObjectsInCell(LbId | 1u), e => e.EntityId == 20u);
Assert.Single(reg.GetObjectsInCell(LbId | 9u), e => e.EntityId == 20u);
}
// -----------------------------------------------------------------------
@ -303,71 +302,151 @@ public class ShadowObjectRegistryTests
}
// -----------------------------------------------------------------------
// A6.P4 slice 1 — portalReachableCells set includes outdoor cells
// when sphere straddles an exit portal (issue #99)
// BR-7 / A6.P4 (2026-06-11) — registration-side architecture pins.
// The former query-side compensations (the b3ce505 indoor gate, the
// portalReachableCells expansion, the isViewer exemption) are deleted;
// these pin the registration shapes that replaced them.
// -----------------------------------------------------------------------
[Fact]
public void GetNearbyObjects_PortalReachableSetIncludesOutdoorCell_IndoorPrimary_DoorReturned()
public void Register_OutdoorSeed_DoorVisibleInItsOutdoorCell_NotInUnrelatedIndoorCell()
{
// A6.P4 slice 1 / issue #99 (2026-05-24): when the player's sphere is
// in an indoor cell but its CellTransit.FindCellSet result includes
// an outdoor cell (via AddAllOutsideCells triggered by the sphere
// straddling an OtherCellId=0xFFFF exit portal), GetNearbyObjects
// must return shadows registered in that outdoor cell. Doors are
// server-spawned entities registered at GameWindow.cs:3139 with
// default cellScope=0u → outdoor 24-m grid registration. Before this
// fix, the loop filtered out outdoor cell ids ("skip outdoor ids")
// AND #98's primaryCellId gate skipped the radial sweep — net result
// was that doors at building thresholds were invisible to spheres
// on the indoor side: walk-through.
// Issue #99 architecture: a door registered at its outdoor cell is
// found by iterating THAT cell's list — which the Transition reaches
// from the indoor side when its sphere straddles the exit portal
// (the cell array spans both cells at the threshold; retail
// "covered twice" note, wf1-interior-collision.md). Without a
// building bridge in the flood cache, the door floods outdoor-only.
var reg = new ShadowObjectRegistry();
const uint doorEntityId = 0x000F4244u;
reg.Register(doorEntityId, 0x020019FFu, new Vector3(12f, 12f, 50f),
Quaternion.Identity, 1f, OffX, OffY, LbId,
ShadowCollisionType.Cylinder, cylHeight: 2.5f);
ShadowCollisionType.Cylinder, cylHeight: 2.5f,
isStatic: false);
var results = new List<ShadowEntry>();
uint doorOutdoorCellId = LbId | 1u; // outdoor cell where door sits
uint vestibuleCellId = LbId | 0x0145u; // indoor cell on the other side
// Sphere is in the vestibule; FindCellSet added the doorway's outdoor
// cell via AddAllOutsideCells. Both ids land in portalReachableCells.
reg.GetNearbyObjects(
new Vector3(12f, 12f, 50f), 5f, OffX, OffY, LbId, results,
portalReachableCells: new[] { vestibuleCellId, doorOutdoorCellId },
primaryCellId: vestibuleCellId);
Assert.Contains(results, e => e.EntityId == doorEntityId);
Assert.Contains(reg.GetObjectsInCell(doorOutdoorCellId), e => e.EntityId == doorEntityId);
Assert.DoesNotContain(reg.GetObjectsInCell(vestibuleCellId), e => e.EntityId == doorEntityId);
}
[Fact]
public void GetNearbyObjects_IndoorPrimary_IndoorOnlyPortalSet_OutdoorRadialStillSkipped()
public void Register_OutdoorFootprint_NeverLandsInInteriorCells()
{
// Regression check for issue #98: when the FindCellSet result holds
// only indoor cells (sphere not near an exit portal — e.g. deep in
// a cellar), the radial outdoor sweep must stay skipped so the
// landblock-baked cottage GfxObj (registered at outdoor cellScope=0)
// is NOT returned to a sphere in the cellar EnvCell. Otherwise the
// head sphere bumps the cottage's downward-facing floor poly from
// below and caps the cellar ascent at world Z≈92.7.
// The #98 shape, now closed at REGISTRATION: an outdoor-positioned
// footprint (the old cottage GfxObj entry) floods into outdoor
// landcells only — without a building-bridge admission its spheres
// can never reach an interior cell's list, so a fully-interior
// cellar query structurally cannot see it. (In production the
// cottage SHELL additionally left the registry entirely — the
// per-LandCell building channel owns it.)
var reg = new ShadowObjectRegistry();
const uint cottageEntityId = 0xA9B47900u;
reg.Register(cottageEntityId, 0x01000A2Bu, new Vector3(12f, 12f, 90f),
Quaternion.Identity, 5.5f, OffX, OffY, LbId);
var results = new List<ShadowEntry>();
uint cellarCellId = LbId | 0x0146u;
Assert.Empty(reg.GetObjectsInCell(cellarCellId));
// Sphere is in the cellar; FindCellSet returned indoor-only set
// (no exit portal nearby).
reg.GetNearbyObjects(
new Vector3(12f, 12f, 50f), 5.5f, OffX, OffY, LbId, results,
portalReachableCells: new[] { cellarCellId },
primaryCellId: cellarCellId);
// And every cell it DID land in is outdoor.
foreach (var entry in reg.AllEntriesForDebug())
Assert.Equal(cottageEntityId, entry.EntityId);
}
Assert.DoesNotContain(results, e => e.EntityId == cottageEntityId);
[Fact]
public void RefloodLandblock_RerunsFloodAfterCellsHydrate()
{
// The streaming race (spec §7 Q3 / retail init_objects →
// recalc_cross_cells): an entity registered while its indoor seed
// cell was unhydrated stays pinned to {seed}; once the cell (with a
// portal neighbor) hydrates, RefloodLandblock re-runs the flood and
// the neighbor appears in the set.
var reg = new ShadowObjectRegistry();
var cache = new PhysicsDataCache();
reg.DataCache = cache;
const uint seedCell = 0xA9B40100u;
const uint neighborCell = 0xA9B40101u;
reg.Register(77u, 0x01000009u, new Vector3(2.0f, 0f, 2.5f),
Quaternion.Identity, 0.5f, OffX, OffY, LbId,
seedCellId: seedCell, isStatic: false);
// Unhydrated seed → registered under the claimed cell only.
Assert.Contains(reg.GetObjectsInCell(seedCell), e => e.EntityId == 77u);
Assert.Empty(reg.GetObjectsInCell(neighborCell));
// Hydrate the seed (portal to the neighbor at x=2.5) + the neighbor
// (leaf BSP admits the straddling sphere), then re-flood.
cache.RegisterCellStructForTest(seedCell,
BuildShadowCellSetTests_MakeCellWithPortalAtRightWall(Matrix4x4.Identity, 0x0101));
cache.RegisterCellStructForTest(neighborCell,
BuildShadowCellSetTests_MakeLeafCell(Matrix4x4.CreateTranslation(5f, 0f, 0f)));
reg.RefloodLandblock(LbId);
Assert.Contains(reg.GetObjectsInCell(seedCell), e => e.EntityId == 77u);
Assert.Contains(reg.GetObjectsInCell(neighborCell), e => e.EntityId == 77u);
}
// Local copies of the BuildShadowCellSetTests fixture helpers (kept
// private per test class by repo convention).
private static CellPhysics BuildShadowCellSetTests_MakeCellWithPortalAtRightWall(
Matrix4x4 worldTransform, ushort otherCellId)
{
var portalPoly = new ResolvedPolygon
{
Vertices = new[]
{
new Vector3(2.5f, -2.5f, 0f),
new Vector3(2.5f, 2.5f, 0f),
new Vector3(2.5f, 2.5f, 5f),
new Vector3(2.5f, -2.5f, 5f),
},
Plane = new System.Numerics.Plane(new Vector3(1, 0, 0), -2.5f),
NumPoints = 4,
SidesType = DatReaderWriter.Enums.CullMode.None,
};
Matrix4x4.Invert(worldTransform, out var inv);
return new CellPhysics
{
WorldTransform = worldTransform,
InverseWorldTransform = inv,
Resolved = new Dictionary<ushort, ResolvedPolygon>(),
PortalPolygons = new Dictionary<ushort, ResolvedPolygon> { [10] = portalPoly },
Portals = new[]
{
new PortalInfo(otherCellId: otherCellId, polygonId: 10, flags: 0),
},
CellBSP = new DatReaderWriter.Types.CellBSPTree
{
Root = new DatReaderWriter.Types.CellBSPNode
{
Type = DatReaderWriter.Enums.BSPNodeType.Leaf,
},
},
};
}
private static CellPhysics BuildShadowCellSetTests_MakeLeafCell(Matrix4x4 worldTransform)
{
Matrix4x4.Invert(worldTransform, out var inv);
return new CellPhysics
{
WorldTransform = worldTransform,
InverseWorldTransform = inv,
Resolved = new Dictionary<ushort, ResolvedPolygon>(),
CellBSP = new DatReaderWriter.Types.CellBSPTree
{
Root = new DatReaderWriter.Types.CellBSPNode
{
Type = DatReaderWriter.Enums.BSPNodeType.Leaf,
},
},
};
}
}