acdream/tests/AcDream.Core.Tests/Physics/ShadowObjectRegistryTests.cs
Erik b49ed904c3 feat(phys): A6.P4 slice 1 — portal-reachable cellSet includes outdoor cells
Closes #99 (run-through doors regression from b3ce505).

The b3ce505 stopgap for #98 gates the outdoor 24m radial sweep on indoor
primary cells. Combined with ShadowObjectRegistry.GetNearbyObjects'
"skip outdoor ids" filter on the cellScope-pass loop, this meant doors
registered at outdoor cells (default cellScope=0u for server-spawned
entities at GameWindow.cs:3139) were invisible to spheres on the indoor
side of a doorway threshold — walk-through.

Pre-flight reads found that CellTransit.FindCellSet already adds
outdoor cells to its candidate set when the sphere straddles an
OtherCellId=0xFFFF exit portal (via AddAllOutsideCells triggered by
exitOutside=true inside the indoor-seed BFS). The fix is to stop
filtering those outdoor ids out before iterating, and rename the param
to portalReachableCells to reflect what the set actually contains.

- Q1: Indoor EnvCell.VisibleCellIds is indoor-only in all 16 cottage
  fixtures (low 16 bits ≥ 0x0100). OtherCellId=0xFFFF on portals
  marks "exit to outdoor world" without naming a specific cellId; the
  specific outdoor cell is computed by AddAllOutsideCells from world
  XY when the sphere straddles the exit portal.
- Q2: GameWindow.cs:3139 ShadowObjects.Register for server-spawned
  entities passes no cellScope → default 0u → outdoor 24m grid
  registration. UpdatePosition (line 145) does the same on movement.
  Doors are confirmed outdoor-registered.

Slice 1 makes a smaller change than the spec proposed (no new
parameter; just drop the existing filter), because FindCellSet's
existing exit-portal logic already exposes the needed outdoor cells.
The retail-faithful registration-side BuildShadowCellSet refactor and
the b3ce505 gate removal stay scheduled for slices 2-3.

Verification:
- 24/24 ShadowObjectRegistryTests pass (incl. two new slice 1 tests:
  IndoorPrimary_OutdoorCellInPortalSet_DoorReturned closes #99;
  IndoorPrimary_IndoorOnlyPortalSet_OutdoorRadialStillSkipped
  regression-pins #98)
- 11/11 CellarUpTrajectoryReplayTests pass (LiveCompare_FirstCap_
  FixClosesCottageFloorCap stays green)
- dotnet build AcDream.slnx: 0 errors, 0 warnings
- Pre-existing 6-8 static-state-leakage failures in serial physics
  suite verified unchanged by stash+retest baseline check

Visual verification pending: walk Holtburg cottage doorway from both
sides; door blocks both directions; cellar still climbable.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-24 08:10:32 +02:00

373 lines
15 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using AcDream.Core.Physics;
using Xunit;
namespace AcDream.Core.Tests.Physics;
/// <summary>
/// Unit tests for <see cref="ShadowObjectRegistry"/> — Task 7 cell-based
/// spatial index for object collision.
/// </summary>
public class ShadowObjectRegistryTests
{
private const uint LbId = 0xA9B40000u; // landblock prefix used throughout
private const float OffX = 0f;
private const float OffY = 0f;
// -----------------------------------------------------------------------
// Register / TotalRegistered
// -----------------------------------------------------------------------
[Fact]
public void Register_SingleEntity_TotalRegisteredIsOne()
{
var reg = new ShadowObjectRegistry();
reg.Register(1u, 0x01000001u, new Vector3(12f, 12f, 50f), Quaternion.Identity, 1f, OffX, OffY, LbId);
Assert.Equal(1, reg.TotalRegistered);
}
[Fact]
public void Register_SameEntityTwice_NoDuplicate()
{
var reg = new ShadowObjectRegistry();
reg.Register(1u, 0x01000001u, new Vector3(12f, 12f, 50f), Quaternion.Identity, 1f, OffX, OffY, LbId);
reg.Register(1u, 0x01000001u, new Vector3(13f, 12f, 50f), Quaternion.Identity, 1f, OffX, OffY, LbId); // re-register (position update)
Assert.Equal(1, reg.TotalRegistered);
}
// -----------------------------------------------------------------------
// GetObjectsInCell
// -----------------------------------------------------------------------
[Fact]
public void GetObjectsInCell_EntityInCenter_ReturnedInExpectedCell()
{
// Entity at local (12, 12) = cell (0,0) = cellId prefix | 1.
var reg = new ShadowObjectRegistry();
reg.Register(42u, 0x01000002u, new Vector3(12f, 12f, 50f), Quaternion.Identity, 1f, OffX, OffY, LbId);
uint cellId = LbId | 1u; // cx=0, cy=0 → 0*8+0+1 = 1
var objs = reg.GetObjectsInCell(cellId);
Assert.Single(objs);
Assert.Equal(42u, objs[0].EntityId);
}
[Fact]
public void GetObjectsInCell_EntitySpanning2Cells_RegisteredInBoth()
{
// Entity at local (24, 12) with radius=2 spans cells cx=0 and cx=1 in X.
// Cell 0,0 = prefix|1; cell 1,0 = prefix|(1*8+0+1)=prefix|9.
var reg = new ShadowObjectRegistry();
reg.Register(7u, 0x01000003u, new Vector3(24f, 12f, 50f), Quaternion.Identity, 2f, OffX, OffY, LbId);
uint cell00 = LbId | 1u; // cx=0, cy=0
uint cell10 = LbId | 9u; // cx=1, cy=0
Assert.Contains(reg.GetObjectsInCell(cell00), e => e.EntityId == 7u);
Assert.Contains(reg.GetObjectsInCell(cell10), e => e.EntityId == 7u);
}
// -----------------------------------------------------------------------
// Deregister
// -----------------------------------------------------------------------
[Fact]
public void Deregister_RemovesFromAllCells()
{
var reg = new ShadowObjectRegistry();
reg.Register(5u, 0x01000004u, new Vector3(24f, 12f, 50f), Quaternion.Identity, 2f, OffX, OffY, LbId);
reg.Deregister(5u);
Assert.Equal(0, reg.TotalRegistered);
Assert.Empty(reg.GetObjectsInCell(LbId | 1u));
Assert.Empty(reg.GetObjectsInCell(LbId | 9u));
}
[Fact]
public void Deregister_NonexistentEntity_NoThrow()
{
var reg = new ShadowObjectRegistry();
// Should not throw.
reg.Deregister(999u);
}
// -----------------------------------------------------------------------
// RemoveLandblock
// -----------------------------------------------------------------------
[Fact]
public void RemoveLandblock_ClearsAllEntitiesForThatBlock()
{
const uint otherLb = 0xAAAA0000u;
var reg = new ShadowObjectRegistry();
reg.Register(1u, 0x01000001u, new Vector3(12f, 12f, 50f), Quaternion.Identity, 1f, OffX, OffY, LbId);
// Entity 2 lives in otherLb whose world origin is at X=192. Place it at
// world X=204 so localX = 204-192 = 12, which maps to cell (0,0) of otherLb.
reg.Register(2u, 0x01000002u, new Vector3(204f, 12f, 50f), Quaternion.Identity, 1f, 192f, 0f, otherLb);
reg.RemoveLandblock(LbId);
Assert.Equal(1, reg.TotalRegistered); // entity 2 (otherLb) survives
Assert.Empty(reg.GetObjectsInCell(LbId | 1u));
}
// -----------------------------------------------------------------------
// GetNearbyObjects
// -----------------------------------------------------------------------
[Fact]
public void GetNearbyObjects_QueryCoversEntity_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);
Assert.Single(results);
Assert.Equal(10u, results[0].EntityId);
}
[Fact]
public void GetNearbyObjects_QueryFarFromEntity_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);
}
[Fact]
public void GetNearbyObjects_EntityInMultipleCells_ReturnedOnce()
{
// Entity spans cells 0,0 and 1,0 (local X≈24, radius=2).
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);
}
// -----------------------------------------------------------------------
// Cell ID formula
// -----------------------------------------------------------------------
[Theory]
[InlineData(0f, 0f, 1u)] // cell (0,0) → index 0*8+0+1 = 1
[InlineData(48f, 0f, 17u)] // cell (2,0) → 2*8+0+1 = 17
[InlineData(0f, 48f, 3u)] // cell (0,2) → 0*8+2+1 = 3
[InlineData(168f, 168f, 64u)] // cell (7,7) → 7*8+7+1 = 64
public void GetObjectsInCell_CellIdFormula_Correct(float lx, float ly, uint expectedLow)
{
var reg = new ShadowObjectRegistry();
// Small radius so entity sits in exactly one cell.
reg.Register(99u, 0x01000008u, new Vector3(lx + 0.5f, ly + 0.5f, 50f), Quaternion.Identity, 0.1f, OffX, OffY, LbId);
uint cellId = LbId | expectedLow;
var objs = reg.GetObjectsInCell(cellId);
Assert.Contains(objs, e => e.EntityId == 99u);
}
// -----------------------------------------------------------------------
// UpdatePosition — Commit A of 2026-04-29 live-entity collision port
// -----------------------------------------------------------------------
[Fact]
public void UpdatePosition_MovedEntity_NewCellOccupied()
{
// Entity starts at local (12, 12) — cell (0,0).
var reg = new ShadowObjectRegistry();
reg.Register(42u, 0x01000010u, new Vector3(12f, 12f, 50f), Quaternion.Identity, 0.5f, OffX, OffY, LbId);
// Move to local (60, 12) — cell (2, 0). Same landblock.
reg.UpdatePosition(42u, new Vector3(60f, 12f, 50f), Quaternion.Identity, OffX, OffY, LbId);
// Old cell empty, new cell holds the entity.
Assert.Empty(reg.GetObjectsInCell(LbId | 1u)); // cell (0,0)
var newCell = reg.GetObjectsInCell(LbId | 17u); // cell (2,0) → 2*8+0+1
Assert.Single(newCell);
Assert.Equal(42u, newCell[0].EntityId);
Assert.Equal(1, reg.TotalRegistered); // not duplicated
}
[Fact]
public void UpdatePosition_PreservesFlags()
{
// Register with PK flags + PhysicsState; UpdatePosition must keep them.
var reg = new ShadowObjectRegistry();
reg.Register(50u, 0x01000011u, new Vector3(12f, 12f, 50f), Quaternion.Identity,
0.5f, OffX, OffY, LbId,
state: 0x4u, // ETHEREAL_PS
flags: EntityCollisionFlags.IsPlayer | EntityCollisionFlags.IsPK);
reg.UpdatePosition(50u, new Vector3(60f, 12f, 50f), Quaternion.Identity, OffX, OffY, LbId);
var newCell = reg.GetObjectsInCell(LbId | 17u);
Assert.Single(newCell);
Assert.Equal(0x4u, newCell[0].State);
Assert.Equal(EntityCollisionFlags.IsPlayer | EntityCollisionFlags.IsPK, newCell[0].Flags);
}
[Fact]
public void UpdatePosition_UnregisteredEntity_NoOp()
{
var reg = new ShadowObjectRegistry();
// Should not throw, should not register a new entity.
reg.UpdatePosition(99u, new Vector3(12f, 12f, 50f), Quaternion.Identity, OffX, OffY, LbId);
Assert.Equal(0, reg.TotalRegistered);
}
[Fact]
public void Register_WithStateAndFlags_StoredOnEntry()
{
var reg = new ShadowObjectRegistry();
reg.Register(60u, 0x01000012u, new Vector3(12f, 12f, 50f), Quaternion.Identity,
0.5f, OffX, OffY, LbId,
state: 0x10u, // IGNORE_COLLISIONS_PS
flags: EntityCollisionFlags.IsImpenetrable);
var entry = reg.GetObjectsInCell(LbId | 1u)[0];
Assert.Equal(0x10u, entry.State);
Assert.Equal(EntityCollisionFlags.IsImpenetrable, entry.Flags);
}
[Fact]
public void Register_DefaultStateAndFlags_AreZeroAndNone()
{
var reg = new ShadowObjectRegistry();
reg.Register(70u, 0x01000013u, new Vector3(12f, 12f, 50f), Quaternion.Identity,
0.5f, OffX, OffY, LbId);
var entry = reg.GetObjectsInCell(LbId | 1u)[0];
Assert.Equal(0u, entry.State);
Assert.Equal(EntityCollisionFlags.None, entry.Flags);
}
// -----------------------------------------------------------------------
// UpdatePhysicsState — L.2g slice 1 (doors flip ETHEREAL post-spawn)
// -----------------------------------------------------------------------
[Fact]
public void UpdatePhysicsState_FlipsEthereal_NextLookupSeesNewBits()
{
var reg = new ShadowObjectRegistry();
const uint doorId = 0x000F4244u;
reg.Register(doorId, 0x020019FFu, new Vector3(12f, 12f, 50f),
Quaternion.Identity, 1f, OffX, OffY, LbId,
state: 0u, flags: EntityCollisionFlags.None);
var before = reg.AllEntriesForDebug().Single(e => e.EntityId == doorId);
Assert.Equal(0u, before.State);
reg.UpdatePhysicsState(doorId, 0x00000004u);
var after = reg.AllEntriesForDebug().Single(e => e.EntityId == doorId);
Assert.Equal(0x00000004u, after.State);
}
[Fact]
public void UpdatePhysicsState_UnregisteredEntity_IsNoOp()
{
var reg = new ShadowObjectRegistry();
reg.UpdatePhysicsState(0xDEADBEEFu, 0x00000004u);
Assert.Equal(0, reg.TotalRegistered);
}
[Fact]
public void UpdatePhysicsState_EntitySpanningMultipleCells_AllCellsUpdated()
{
var reg = new ShadowObjectRegistry();
reg.Register(99u, 0x01000099u, new Vector3(24f, 12f, 50f),
Quaternion.Identity, 2f, OffX, OffY, LbId,
state: 0u);
reg.UpdatePhysicsState(99u, 0x00000004u);
uint cellA = LbId | 1u;
uint cellB = LbId | (1u*8 + 0 + 1);
var inA = reg.GetObjectsInCell(cellA).Single(e => e.EntityId == 99u);
var inB = reg.GetObjectsInCell(cellB).Single(e => e.EntityId == 99u);
Assert.Equal(0x00000004u, inA.State);
Assert.Equal(0x00000004u, inB.State);
}
// -----------------------------------------------------------------------
// A6.P4 slice 1 — portalReachableCells set includes outdoor cells
// when sphere straddles an exit portal (issue #99)
// -----------------------------------------------------------------------
[Fact]
public void GetNearbyObjects_PortalReachableSetIncludesOutdoorCell_IndoorPrimary_DoorReturned()
{
// 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.
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);
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);
}
[Fact]
public void GetNearbyObjects_IndoorPrimary_IndoorOnlyPortalSet_OutdoorRadialStillSkipped()
{
// 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.
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;
// 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);
Assert.DoesNotContain(results, e => e.EntityId == cottageEntityId);
}
}