Gate-2 fallout chain: session 1's bare-id wedge poisoned ACE's save (the client reported garbage cells while walking through walls), so session 2 logged in with (cell=0xA9B4013F inn interior, pos=(91.4,32.7)) — a position that cell does not contain. Probe evidence: exactly one [cell-transit] line all session; the player free-fell into an empty world. Two holes, both fixed at the root: 1. CellTransit pick escape hatch — restores the #83/A1.7 + #90 verification that lived in PhysicsEngine.ResolveCellId before the collide-then-pick rewrite moved membership into BuildCellSetAndPickContaining: an indoor current cell that IS hydrated but whose CellBSP no longer overlaps ANY part of the foot sphere is a bogus claim (corrupt save, or walked out through an unblocked gap). The portal BFS can never reach an exit portal from a cell the sphere isn't in, so no candidates exist and the claim held forever — wedging collision (ShadowObjectRegistry's #98 gate reads "indoor primary" -> outdoor object sweep skipped), wall BSP, terrain, and the render root. The pick now demotes to the outdoor column under the sphere centre (the LandDefs.AdjustToOutside result already computed for the pick — cross-block safe). Sphere-overlap (BSPQuery.SphereIntersectsCellBsp, pseudo_c:317666 -> :323267), NOT point-in: doorway push-back leaves the centre a few cm outside while the sphere still overlaps — no demotion, #90's ping-pong stays dead. An unhydrated cell cannot be verified — stale beats null while streaming hydrates (retail-equivalent: stale curr_cell kept when the pick finds nothing). 2. PlayerModeAutoEntry spawn-ground hold — player-mode entry now waits for the terrain under the spawn position to stream in (isSpawnGroundReady predicate, K.2 pattern). Entering earlier integrates gravity against an empty world: indoor-claimed spawns got no floor from any source and free-fell into the void; outdoor spawns raced hydration by ~1s every login. Retail never has this state (it loads cells synchronously) — the hold is the async-streaming equivalent of that invariant. With the hold, the entry snap (Resolve, stepUp=100) runs against hydrated cell floors + terrain and re-seats a corrupt save's claim immediately. Tests: IndoorSeed_SphereFullyOutsideHydratedCell_DemotesToOutdoorColumn (the gate-2 wedge shape, red pre-fix), straddle + no-BSP guards (the #90 hysteresis and stale-beats-null), TryEnter_Armed_SpawnGroundNotReady_ DoesNotFire. Full suite: 294+218+420 green; Core 1375 green + the same 4 pre-existing door/#99-era failures + 1 skip. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
418 lines
19 KiB
C#
418 lines
19 KiB
C#
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Numerics;
|
||
using DatReaderWriter.Enums;
|
||
using DatReaderWriter.Types;
|
||
using AcDream.Core.Physics;
|
||
using Xunit;
|
||
|
||
namespace AcDream.Core.Tests.Physics;
|
||
|
||
public class CellTransitFindCellSetTests
|
||
{
|
||
// ──────────────────────────────────────────────────────────────────
|
||
// Helpers — mirror CellTransitFindTransitCellsSphereTests.cs pattern
|
||
// ──────────────────────────────────────────────────────────────────
|
||
|
||
private static CellPhysics MakeCellWithPortalAtRightWall(
|
||
Matrix4x4 worldTransform, uint otherCellId, ushort flags)
|
||
{
|
||
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 Plane(new Vector3(1, 0, 0), -2.5f), // x = 2.5
|
||
NumPoints = 4,
|
||
SidesType = 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: (ushort)otherCellId, polygonId: 10, flags: flags),
|
||
},
|
||
CellBSP = new CellBSPTree
|
||
{
|
||
Root = new CellBSPNode { Type = BSPNodeType.Leaf },
|
||
}
|
||
};
|
||
}
|
||
|
||
// ──────────────────────────────────────────────────────────────────
|
||
// Tests
|
||
// ──────────────────────────────────────────────────────────────────
|
||
|
||
[Fact]
|
||
public void Sphere_FullyInsidePrimaryCell_ReturnsOnlyPrimary()
|
||
{
|
||
var cellA = MakeCellWithPortalAtRightWall(Matrix4x4.Identity, otherCellId: 0x0101, flags: 0);
|
||
var cache = new PhysicsDataCache();
|
||
cache.RegisterCellStructForTest(0xA9B40100u, cellA);
|
||
|
||
// Sphere far from any portal — local x=-1, reach to x=-0.5; portal at x=2.5.
|
||
var sphereCenter = new Vector3(-1.0f, 0f, 2.5f);
|
||
|
||
uint containing = CellTransit.FindCellSet(
|
||
cache, sphereCenter, sphereRadius: 0.5f,
|
||
currentCellId: 0xA9B40100u,
|
||
out var cellSet);
|
||
|
||
Assert.Equal(0xA9B40100u, containing);
|
||
Assert.Single(cellSet);
|
||
Assert.Contains(0xA9B40100u, cellSet);
|
||
}
|
||
|
||
[Fact]
|
||
public void Sphere_StraddlingPortal_ReturnsBothCells()
|
||
{
|
||
var cellA = MakeCellWithPortalAtRightWall(Matrix4x4.Identity, otherCellId: 0x0101, flags: 0);
|
||
var cellBT = Matrix4x4.CreateTranslation(new Vector3(5f, 0f, 0f));
|
||
Matrix4x4.Invert(cellBT, out var cellBInv);
|
||
var cellB = new CellPhysics
|
||
{
|
||
WorldTransform = cellBT,
|
||
InverseWorldTransform = cellBInv,
|
||
Resolved = new Dictionary<ushort, ResolvedPolygon>(),
|
||
CellBSP = new CellBSPTree
|
||
{
|
||
Root = new CellBSPNode { Type = BSPNodeType.Leaf },
|
||
}
|
||
};
|
||
|
||
var cache = new PhysicsDataCache();
|
||
cache.RegisterCellStructForTest(0xA9B40100u, cellA);
|
||
cache.RegisterCellStructForTest(0xA9B40101u, cellB);
|
||
|
||
// Sphere center at local x=2.0, radius=0.5 → reaches x=2.5 = portal plane.
|
||
var sphereCenter = new Vector3(2.0f, 0f, 2.5f);
|
||
|
||
uint containing = CellTransit.FindCellSet(
|
||
cache, sphereCenter, sphereRadius: 0.5f,
|
||
currentCellId: 0xA9B40100u,
|
||
out var cellSet);
|
||
|
||
Assert.Contains(0xA9B40100u, cellSet);
|
||
Assert.Contains(0xA9B40101u, cellSet);
|
||
}
|
||
|
||
[Fact]
|
||
public void FindCellSet_OutdoorSeed_IncludesNeighbourLandcells()
|
||
{
|
||
var cache = new PhysicsDataCache();
|
||
// A6.P4 (2026-05-24): sphere coords are LANDBLOCK-LOCAL (X/Y in
|
||
// [0, 192]). Place the sphere center near the east boundary of
|
||
// landcell grid(0,0) (i.e., near local X=24) so AddAllOutsideCells
|
||
// adds the east neighbour grid(1,0).
|
||
var sphereCenter = new Vector3(23.8f, 12f, 0f);
|
||
|
||
uint containing = CellTransit.FindCellSet(
|
||
cache, sphereCenter, sphereRadius: 0.5f,
|
||
currentCellId: 0xA9B40001u, // outdoor cell, low byte < 0x100
|
||
out var cellSet);
|
||
|
||
Assert.Equal(0xA9B40001u, containing);
|
||
Assert.True(cellSet.Count >= 2, $"Expected ≥2 cells in set (primary + east neighbour), got {cellSet.Count}");
|
||
}
|
||
|
||
[Fact]
|
||
public void IndoorSeed_ExitPortalTouchedOnlyBySecondSphere_AddsOutdoorLandcell()
|
||
{
|
||
// A6.P4 (2026-05-24): landblock-local sphere coords. Cell sits at
|
||
// the landblock origin (identity transform), so cell-local == world.
|
||
// Sphere head at local (2, 12, 3.2) reaches the cell's exit portal
|
||
// plane at local X=2.5 → AddAllOutsideCells fires.
|
||
var exitCell = MakeCellWithPortalAtRightWall(
|
||
Matrix4x4.Identity,
|
||
otherCellId: 0xFFFF,
|
||
flags: 0);
|
||
|
||
var cache = new PhysicsDataCache();
|
||
cache.RegisterCellStructForTest(0xA9B40100u, exitCell);
|
||
|
||
var spheres = new[]
|
||
{
|
||
// Foot sphere is not near the exit portal plane at local x=2.5.
|
||
new Sphere { Origin = new Vector3(0f, 12f, 2.5f), Radius = 0.5f },
|
||
// Head sphere reaches the exit portal plane and should trigger
|
||
// outdoor landcell expansion.
|
||
new Sphere { Origin = new Vector3(2f, 12f, 3.2f), Radius = 0.5f },
|
||
};
|
||
|
||
uint containing = CellTransit.FindCellSet(
|
||
cache, spheres, spheres.Length,
|
||
currentCellId: 0xA9B40100u,
|
||
out var cellSet);
|
||
|
||
Assert.Equal(0xA9B40100u, containing);
|
||
Assert.Contains(0xA9B40100u, cellSet);
|
||
Assert.Contains(0xA9B40001u, cellSet);
|
||
}
|
||
|
||
// ──────────────────────────────────────────────────────────────────
|
||
// #106 — outdoor membership across landblock boundaries.
|
||
// Retail's add_all_outside_cells + the find_cell_list pick run in the
|
||
// GLOBAL landcell grid (LandDefs lcoords); crossing a landblock boundary
|
||
// is inherent. The pre-#106 port clamped both to the current block's 8×8
|
||
// grid → zero candidates one step over the line → membership frozen
|
||
// (the 10,449-frame playerCell freeze in flap-105-capture.log).
|
||
// ──────────────────────────────────────────────────────────────────
|
||
|
||
[Fact]
|
||
public void OutdoorSeed_CrossesLandblockBoundary_South()
|
||
{
|
||
// The #106 acceptance golden: walking south out of A9B4, the outdoor
|
||
// cell must advance to the southern neighbour block's cell. Anchor
|
||
// frame (no registered terrain → origin Zero): world y = -0.2 is
|
||
// 0.2 m into A9B3's row 7 under x=150 → cell 0xA9B30038.
|
||
var cache = new PhysicsDataCache();
|
||
|
||
uint containing = CellTransit.FindCellSet(
|
||
cache, new Vector3(150f, -0.2f, 0f), sphereRadius: 0.5f,
|
||
currentCellId: 0xA9B40031u,
|
||
out var cellSet);
|
||
|
||
Assert.Equal(0xA9B30038u, containing);
|
||
Assert.Contains(0xA9B30038u, cellSet);
|
||
Assert.Contains(0xA9B40031u, cellSet); // +Y neighbour still in the set
|
||
}
|
||
|
||
[Fact]
|
||
public void OutdoorSeed_NearBoundaryButInside_StaysCurrent()
|
||
{
|
||
// 0.2 m NORTH of the boundary: the candidate set includes the A9B3
|
||
// neighbour (sphere overlaps it) but the centre column is still the
|
||
// current cell — membership must NOT flip early (single clean flip
|
||
// at the line, matching the capture's 96/96 within-block behaviour).
|
||
var cache = new PhysicsDataCache();
|
||
|
||
uint containing = CellTransit.FindCellSet(
|
||
cache, new Vector3(150f, 0.2f, 0f), sphereRadius: 0.5f,
|
||
currentCellId: 0xA9B40031u,
|
||
out var cellSet);
|
||
|
||
Assert.Equal(0xA9B40031u, containing);
|
||
Assert.Contains(0xA9B30038u, cellSet);
|
||
}
|
||
|
||
[Fact]
|
||
public void OutdoorSeed_NonAnchorBlock_UsesRegisteredTerrainOrigin()
|
||
{
|
||
// Northbound return: the player's current cell is in A9B3 (origin
|
||
// (0, -192) registered via CellGraph terrain — the production path).
|
||
// World y = +1 is 1 m back into the anchor block A9B4; the pick must
|
||
// convert through A9B3's origin (block-local y = 193) and advance to
|
||
// 0xA9B40031. Pre-#106 the world frame was silently assumed
|
||
// block-local, which is wrong for every non-anchor block.
|
||
var cache = new PhysicsDataCache();
|
||
cache.CellGraph.RegisterTerrain(
|
||
0xA9B30000u,
|
||
new TerrainSurface(new byte[81], new float[256]),
|
||
new Vector3(0f, -192f, 0f));
|
||
|
||
uint containing = CellTransit.FindCellSet(
|
||
cache, new Vector3(150f, 1f, 0f), sphereRadius: 0.5f,
|
||
currentCellId: 0xA9B30038u,
|
||
out _);
|
||
|
||
Assert.Equal(0xA9B40031u, containing);
|
||
}
|
||
|
||
// ──────────────────────────────────────────────────────────────────
|
||
// #106 gate-2 escape hatch — bogus indoor claim recovery.
|
||
// Restores the #83/A1.7 + #90 verification (formerly in ResolveCellId,
|
||
// lost in the collide-then-pick rewrite): a HYDRATED indoor current
|
||
// cell whose CellBSP no longer overlaps ANY part of the foot sphere is
|
||
// a bogus claim (corrupt server save pairing an indoor cell with a
|
||
// position far outside it, or walked out through an unblocked gap).
|
||
// Staying on it wedges everything: the BFS can't reach an exit portal
|
||
// from a cell the sphere isn't in (no outdoor candidates, membership
|
||
// frozen), GetNearbyObjects' #98 gate reads "indoor primary" (no
|
||
// object collision), no wall BSP and no terrain (void fall). The pick
|
||
// must demote to the outdoor column under the sphere centre.
|
||
// Sphere-overlap (not point-in) preserves the #90 doorway-pushback
|
||
// hysteresis.
|
||
// ──────────────────────────────────────────────────────────────────
|
||
|
||
/// <summary>Cell whose CellBSP half-space is x ≥ 0 (cell-local).</summary>
|
||
private static CellPhysics MakeCellWithBoundedBsp(Matrix4x4 worldTransform)
|
||
{
|
||
Matrix4x4.Invert(worldTransform, out var inv);
|
||
return new CellPhysics
|
||
{
|
||
WorldTransform = worldTransform,
|
||
InverseWorldTransform = inv,
|
||
Resolved = new Dictionary<ushort, ResolvedPolygon>(),
|
||
CellBSP = new CellBSPTree
|
||
{
|
||
Root = new CellBSPNode
|
||
{
|
||
SplittingPlane = new Plane(new Vector3(1f, 0f, 0f), 0f),
|
||
PosNode = new CellBSPNode { Type = BSPNodeType.Leaf },
|
||
},
|
||
}
|
||
};
|
||
}
|
||
|
||
[Fact]
|
||
public void IndoorSeed_SphereFullyOutsideHydratedCell_DemotesToOutdoorColumn()
|
||
{
|
||
// The gate-2 live wedge shape: claimed cell 0xA9B40150, sphere far
|
||
// outside its volume (x = -10, fully behind the x≥0 half-space).
|
||
// The BFS finds no portals (sphere nowhere near them), so no outdoor
|
||
// candidates exist — pre-fix this returned the bogus claim forever.
|
||
// Expected: the global outdoor column under the centre. x=-10 is one
|
||
// cell WEST of the A9B4 block edge → block 0xA8B4, lcoord (1351,1440)
|
||
// → cell (7,0) → low 0x39.
|
||
var cache = new PhysicsDataCache();
|
||
cache.RegisterCellStructForTest(0xA9B40150u, MakeCellWithBoundedBsp(Matrix4x4.Identity));
|
||
|
||
uint containing = CellTransit.FindCellSet(
|
||
cache, new Vector3(-10f, 12f, 0f), sphereRadius: 0.5f,
|
||
currentCellId: 0xA9B40150u,
|
||
out _);
|
||
|
||
Assert.Equal(0xA8B40039u, containing);
|
||
}
|
||
|
||
[Fact]
|
||
public void IndoorSeed_SphereStraddlesCellBoundary_StaysCurrent()
|
||
{
|
||
// #90 hysteresis guard: centre just outside the half-space (x=-0.3)
|
||
// but the 0.5 radius still reaches back in → sphere OVERLAPS → the
|
||
// claim is legitimate (doorway push-back shape) → NO demotion.
|
||
var cache = new PhysicsDataCache();
|
||
cache.RegisterCellStructForTest(0xA9B40150u, MakeCellWithBoundedBsp(Matrix4x4.Identity));
|
||
|
||
uint containing = CellTransit.FindCellSet(
|
||
cache, new Vector3(-0.3f, 12f, 0f), sphereRadius: 0.5f,
|
||
currentCellId: 0xA9B40150u,
|
||
out _);
|
||
|
||
Assert.Equal(0xA9B40150u, containing);
|
||
}
|
||
|
||
[Fact]
|
||
public void IndoorSeed_CellWithoutBsp_CannotVerify_StaysCurrent()
|
||
{
|
||
// Stale-beats-null while streaming hydrates: a registered cell with
|
||
// no CellBSP yet cannot be verified — trust the claim (no demotion).
|
||
Matrix4x4.Invert(Matrix4x4.Identity, out var inv);
|
||
var cellNoBsp = new CellPhysics
|
||
{
|
||
WorldTransform = Matrix4x4.Identity,
|
||
InverseWorldTransform = inv,
|
||
Resolved = new Dictionary<ushort, ResolvedPolygon>(),
|
||
};
|
||
var cache = new PhysicsDataCache();
|
||
cache.RegisterCellStructForTest(0xA9B40150u, cellNoBsp);
|
||
|
||
uint containing = CellTransit.FindCellSet(
|
||
cache, new Vector3(-10f, 12f, 0f), sphereRadius: 0.5f,
|
||
currentCellId: 0xA9B40150u,
|
||
out _);
|
||
|
||
Assert.Equal(0xA9B40150u, containing);
|
||
}
|
||
|
||
// ──────────────────────────────────────────────────────────────────
|
||
// Membership hysteresis — the R1-flap root cause.
|
||
// Retail CObjCell::find_cell_list adds the CURRENT cell at index 0
|
||
// (add_cell, pc:308766) and the pick loop iterates from index 0 with
|
||
// interior-wins-break (pc:308791-308819) — so when two cells' BSPs both
|
||
// contain the sphere center at a boundary, the CURRENT cell wins and the
|
||
// membership does NOT flip. acdream's unordered-HashSet pick dropped that
|
||
// ordering, producing the stair/room/doorway ping-pong (cell flips every
|
||
// tick while straddling). This guards the current-cell-first behaviour.
|
||
// ──────────────────────────────────────────────────────────────────
|
||
|
||
[Theory]
|
||
[InlineData(0xA9B40100u, 0xA9B40101u)]
|
||
[InlineData(0xA9B40101u, 0xA9B40100u)]
|
||
public void TwoOverlappingCells_CurrentCellWinsTheStraddle(uint currentCellId, uint otherCellId)
|
||
{
|
||
// Both cells use a Leaf BSP (contains any point) and are reciprocally portalled at the same
|
||
// plane (local x=2.5), so the candidate set is {both} from EITHER seed and the sphere center
|
||
// is inside BOTH cells' BSPs. The pick must return the CURRENT cell (hysteresis), not flip to
|
||
// the neighbour. Testing both seed directions guarantees a deterministic failure on the
|
||
// unordered pick (it returns the same enumeration-first cell regardless of which is current).
|
||
ushort currentLow = (ushort)(currentCellId & 0xFFFF);
|
||
ushort otherLow = (ushort)(otherCellId & 0xFFFF);
|
||
|
||
var current = MakeCellWithPortalAtRightWall(Matrix4x4.Identity, otherLow, flags: 0);
|
||
var other = MakeCellWithPortalAtRightWall(Matrix4x4.Identity, currentLow, flags: 0);
|
||
|
||
var cache = new PhysicsDataCache();
|
||
cache.RegisterCellStructForTest(currentCellId, current);
|
||
cache.RegisterCellStructForTest(otherCellId, other);
|
||
|
||
// Center straddles the portal plane at local x=2.5 (both cells), inside both Leaf BSPs.
|
||
var sphereCenter = new Vector3(2.0f, 0f, 2.5f);
|
||
|
||
uint containing = CellTransit.FindCellSet(
|
||
cache, sphereCenter, sphereRadius: 0.5f,
|
||
currentCellId: currentCellId,
|
||
out var cellSet);
|
||
|
||
Assert.Contains(currentCellId, cellSet);
|
||
Assert.Contains(otherCellId, cellSet);
|
||
Assert.Equal(currentCellId, containing); // retail current-cell-first hysteresis
|
||
}
|
||
|
||
// The ordered-CELLARRAY contract: FindCellSet returns the candidate set in
|
||
// retail add-order with the CURRENT cell at index 0 (retail add_cell @308766).
|
||
// This is the invariant the verbatim pick relies on; the unordered HashSet
|
||
// could not guarantee it.
|
||
[Fact]
|
||
public void FindCellSet_CurrentCellIsFirstInTheSet()
|
||
{
|
||
var cellA = MakeCellWithPortalAtRightWall(Matrix4x4.Identity, otherCellId: 0x0101, flags: 0);
|
||
var cellBT = Matrix4x4.CreateTranslation(new Vector3(5f, 0f, 0f));
|
||
Matrix4x4.Invert(cellBT, out var cellBInv);
|
||
var cellB = new CellPhysics
|
||
{
|
||
WorldTransform = cellBT,
|
||
InverseWorldTransform = cellBInv,
|
||
Resolved = new Dictionary<ushort, ResolvedPolygon>(),
|
||
CellBSP = new CellBSPTree { Root = new CellBSPNode { Type = BSPNodeType.Leaf } },
|
||
};
|
||
var cache = new PhysicsDataCache();
|
||
cache.RegisterCellStructForTest(0xA9B40100u, cellA);
|
||
cache.RegisterCellStructForTest(0xA9B40101u, cellB);
|
||
|
||
// Straddle the portal plane so both cells are in the set.
|
||
var sphereCenter = new Vector3(2.0f, 0f, 2.5f);
|
||
|
||
CellTransit.FindCellSet(cache, sphereCenter, 0.5f, 0xA9B40100u, out var cellSet);
|
||
Assert.Equal(0xA9B40100u, cellSet.First()); // current cell at index 0
|
||
}
|
||
|
||
// Interior-wins over the outdoor fallback: while an interior cell still
|
||
// contains the centre, it wins even though the exit portal also added the
|
||
// outdoor landcell to the set (retail interior-wins-break, pc:308814-308819).
|
||
[Fact]
|
||
public void IndoorWithExitPortal_InteriorWinsWhileItContainsCentre()
|
||
{
|
||
// Interior cell at the landblock origin with an exit portal at local x=2.5;
|
||
// Leaf BSP contains any point. Centre at local (0,12,2.5) is INSIDE the cell
|
||
// and NOT across the exit plane, so interior must win even though the head
|
||
// sphere / exit logic may add the outdoor landcell.
|
||
var exitCell = MakeCellWithPortalAtRightWall(Matrix4x4.Identity, otherCellId: 0xFFFF, flags: 0);
|
||
var cache = new PhysicsDataCache();
|
||
cache.RegisterCellStructForTest(0xA9B40100u, exitCell);
|
||
|
||
var sphereCenter = new Vector3(0f, 12f, 2.5f);
|
||
uint containing = CellTransit.FindCellSet(cache, sphereCenter, 0.5f, 0xA9B40100u, out _);
|
||
Assert.Equal(0xA9B40100u, containing); // interior-wins, not the outdoor landcell
|
||
}
|
||
}
|