feat(physics): Phase 2 — BuildingPhysics + CheckBuildingTransit

Closes the outdoor→indoor entry path. New BuildingPhysics type holds
the per-SortCell BldPortal list + building world transform; PhysicsDataCache
caches it (CacheBuilding + GetBuilding); CellTransit.CheckBuildingTransit
tests each portal's destination cell via PointInsideCellBsp.

PhysicsEngine.ResolveCellId's outdoor branch now hooks CheckBuildingTransit
after the terrain-grid lookup: if the matched landcell has a cached
building stab, check whether the sphere has crossed into one of its
interior EnvCells before returning.

GameWindow at landblock-load time iterates LandBlockInfo.Buildings and
caches each via PhysicsDataCache.CacheBuilding. The landcell-id derivation
uses retail's row-major cell-index formula (gridX * 8 + gridY + 1).

Polish items from Subagent B/C reviews folded in:
- visited HashSet in FindCellList's BFS (avoids O(N^2) re-enqueue)
- ResolveCellId_NoDataCache_ReturnsFallback test (closes coverage gap)
- DataCache-asymmetry comment in PhysicsEngine.ResolveCellId
- Replaced misleading FindCellList outdoor-branch TODO with explicit
  note that ResolveCellId bypasses this branch — wired in ResolveCellId
  directly.
- Removed unused 'using DatReaderWriter.Types;' from CellTransit.cs
- 2 new CellTransitFindCellListTests integration tests
- 1 new CellTransitCheckBuildingTransitTests test (null-CellBSP guard
  case; happy path deferred to visual verification).

Spec: docs/superpowers/specs/2026-05-19-indoor-portal-cell-tracking-design.md
Plan: docs/superpowers/plans/2026-05-19-indoor-portal-cell-tracking.md

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Erik 2026-05-19 17:34:38 +02:00
parent aad697602e
commit 069534a372
8 changed files with 301 additions and 7 deletions

View file

@ -0,0 +1,56 @@
using System.Collections.Generic;
using System.Numerics;
using AcDream.Core.Physics;
using Xunit;
namespace AcDream.Core.Tests.Physics;
public class CellTransitCheckBuildingTransitTests
{
[Fact]
public void SphereInsideBuildingPortalDestination_AddsInteriorCell()
{
// Building at world origin. One portal to interior cell 0xA9B40100.
var building = new BuildingPhysics
{
WorldTransform = Matrix4x4.Identity,
InverseWorldTransform = Matrix4x4.Identity,
Portals = new[]
{
new BldPortalInfo(
otherCellId: 0xA9B40100u,
otherPortalId: 0,
flags: 0),
},
};
// Interior cell with null CellBSP — PointInsideCellBsp(null, _) returns true,
// but CheckBuildingTransit guards on CellBSP?.Root being non-null, so this
// cell is skipped.
var interiorCell = new CellPhysics
{
WorldTransform = Matrix4x4.Identity,
InverseWorldTransform = Matrix4x4.Identity,
Resolved = new Dictionary<ushort, ResolvedPolygon>(),
};
var cache = new PhysicsDataCache();
cache.RegisterCellStructForTest(0xA9B40100u, interiorCell);
var candidates = new HashSet<uint>();
CellTransit.CheckBuildingTransit(
cache, building,
worldSphereCenter: new Vector3(0, 0, 0),
sphereRadius: 0.5f,
candidates);
// CellBSP is null → containment guard (otherCell?.CellBSP?.Root is null)
// skips this cell. No candidate added.
Assert.Empty(candidates);
}
// A second test that uses a synthetic CellBSP whose Root.Type == BSPNodeType.Leaf
// (which PointInsideCellBsp short-circuits as "inside") would verify the
// happy path. Constructing a CellBSPTree by hand from DatReaderWriter
// types is awkward; deferred to integration testing at visual-verify time.
}

View file

@ -0,0 +1,38 @@
using System.Numerics;
using AcDream.Core.Physics;
using Xunit;
namespace AcDream.Core.Tests.Physics;
public class CellTransitFindCellListTests
{
[Fact]
public void IndoorSeed_NoCacheEntry_ReturnsFallback()
{
var cache = new PhysicsDataCache();
// Indoor seed but cell not cached → FindCellList early-returns the fallback.
uint result = CellTransit.FindCellList(
cache,
worldSphereCenter: Vector3.Zero,
sphereRadius: 0.5f,
currentCellId: 0xA9B40100u);
Assert.Equal(0xA9B40100u, result);
}
[Fact]
public void OutdoorSeed_Returns_FallbackWhenNoCellBSPs()
{
var cache = new PhysicsDataCache();
// Outdoor seed: AddAllOutsideCells adds landcell candidates, but they
// have no CellPhysics (only EnvCells get cached) → containment loop
// finds no winner → fall back.
uint result = CellTransit.FindCellList(
cache,
worldSphereCenter: new Vector3(12f, 12f, 0f),
sphereRadius: 0.5f,
currentCellId: 0xA9B40001u);
Assert.Equal(0xA9B40001u, result);
}
}

View file

@ -28,4 +28,17 @@ public class ResolveCellIdTests
Assert.Equal(0xA9B40001u, result);
}
[Fact]
public void ResolveCellId_NoDataCache_ReturnsFallback()
{
// Build a PhysicsEngine without setting DataCache.
var engine = new PhysicsEngine { DataCache = null };
uint result = engine.ResolveCellId(
new Vector3(100, 100, 0),
sphereRadius: 0.5f,
fallbackCellId: 0xA9B40100u); // indoor seed
// Indoor branch falls back when DataCache is null.
Assert.Equal(0xA9B40100u, result);
}
}