using System.Collections.Generic; using System.Numerics; using DatReaderWriter.Types; using AcDream.Core.Physics; using Xunit; namespace AcDream.Core.Tests.Physics; public class CellTransitFindTransitCellsSphereTests { private static CellBSPTree SinglePlaneCellBsp() { var leaf = new CellBSPNode { Type = DatReaderWriter.Enums.BSPNodeType.Leaf }; return new CellBSPTree { Root = new CellBSPNode { // Local x >= 0 is inside this synthetic cell. SplittingPlane = new Plane(new Vector3(1f, 0f, 0f), 0f), PosNode = leaf, } }; } private static CellPhysics MakeCellWithPortalAtRightWall( Matrix4x4 worldTransform, uint otherCellId, ushort flags) { // Portal poly at local x=2.5 (right wall), normal +X. var portalPolyA = 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 = DatReaderWriter.Enums.CullMode.None, }; Matrix4x4.Invert(worldTransform, out var inv); return new CellPhysics { WorldTransform = worldTransform, InverseWorldTransform = inv, Resolved = new Dictionary(), PortalPolygons = new Dictionary { [10] = portalPolyA }, Portals = new[] { new PortalInfo(otherCellId: (ushort)otherCellId, polygonId: 10, flags: flags), }, }; } [Fact] public void SphereInsideCellA_NearPortal_AddsCellB() { 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(), }; var cache = new PhysicsDataCache(); cache.RegisterCellStructForTest(0xA9B40100u, cellA); cache.RegisterCellStructForTest(0xA9B40101u, cellB); // Sphere center near portal (local x=2.0, radius=0.5 → reaches x=2.5 = portal plane). var worldSphereCenter = new Vector3(2.0f, 0f, 2.5f); var candidates = new HashSet(); CellTransit.FindTransitCellsSphere( cache, cellA, currentCellId: 0xA9B40100u, worldSphereCenter, sphereRadius: 0.5f, candidates, out bool exitOutside); Assert.Contains(0xA9B40101u, candidates); Assert.False(exitOutside); } [Fact] public void SphereInsideCellA_FarFromPortal_DoesNotAddCellB() { var cellA = MakeCellWithPortalAtRightWall(Matrix4x4.Identity, otherCellId: 0x0101, flags: 0); var cache = new PhysicsDataCache(); cache.RegisterCellStructForTest(0xA9B40100u, cellA); // Sphere far from portal (local x=-1.0, reach to x=-0.5 — nowhere near portal at x=2.5). var worldSphereCenter = new Vector3(-1.0f, 0f, 2.5f); var candidates = new HashSet(); CellTransit.FindTransitCellsSphere( cache, cellA, currentCellId: 0xA9B40100u, worldSphereCenter, sphereRadius: 0.5f, candidates, out bool exitOutside); Assert.DoesNotContain(0xA9B40101u, candidates); } [Fact] public void LoadedNeighbor_SphereIntersectsNeighborCellBsp_AddsEvenWhenPortalHintWouldReject() { // Retail CEnvCell::find_transit_cells uses the loaded neighbour's // CellBSP sphere-overlap test. The portal-plane side test is only // an unloaded-cell hint. flags=2 makes the old heuristic reject // this world position even though the sphere overlaps cell B. var cellA = MakeCellWithPortalAtRightWall(Matrix4x4.Identity, otherCellId: 0x0101, flags: 2); 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(), CellBSP = SinglePlaneCellBsp(), }; var cache = new PhysicsDataCache(); cache.RegisterCellStructForTest(0xA9B40100u, cellA); cache.RegisterCellStructForTest(0xA9B40101u, cellB); // Cell B local center is x=-0.25, radius=0.5, so the sphere // straddles x=0 and intersects the cell volume. var worldSphereCenter = new Vector3(4.75f, 0f, 2.5f); var candidates = new HashSet(); CellTransit.FindTransitCellsSphere( cache, cellA, currentCellId: 0xA9B40100u, worldSphereCenter, sphereRadius: 0.5f, candidates, out bool exitOutside); Assert.Contains(0xA9B40101u, candidates); Assert.False(exitOutside); } [Fact] public void LoadedNeighbor_SphereOutsideNeighborCellBsp_DoesNotUsePortalHintFallback() { // With a loaded neighbour, retail trusts sphere_intersects_cell. // This guards against adding the neighbour merely because the // current-cell portal plane would have accepted the sphere. 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(), CellBSP = SinglePlaneCellBsp(), }; var cache = new PhysicsDataCache(); cache.RegisterCellStructForTest(0xA9B40100u, cellA); cache.RegisterCellStructForTest(0xA9B40101u, cellB); // Current portal-plane heuristic would add this (near x=2.5), but // in cell B local space x=-1.95 with radius=0.5 is fully outside. var worldSphereCenter = new Vector3(3.05f, 0f, 2.5f); var candidates = new HashSet(); CellTransit.FindTransitCellsSphere( cache, cellA, currentCellId: 0xA9B40100u, worldSphereCenter, sphereRadius: 0.5f, candidates, out bool exitOutside); Assert.DoesNotContain(0xA9B40101u, candidates); Assert.False(exitOutside); } [Fact] public void ExitPortal_SphereStraddlesPortalPlane_FlagsCheckOutside() { var exitCell = MakeCellWithPortalAtRightWall(Matrix4x4.Identity, otherCellId: 0xFFFF, flags: 0); var cache = new PhysicsDataCache(); cache.RegisterCellStructForTest(0xA9B40100u, exitCell); var worldSphereCenter = new Vector3(2.0f, 0f, 2.5f); var candidates = new HashSet(); CellTransit.FindTransitCellsSphere( cache, exitCell, currentCellId: 0xA9B40100u, worldSphereCenter, sphereRadius: 0.5f, candidates, out bool exitOutside); Assert.True(exitOutside); } [Fact] public void ExitPortal_SecondSphereStraddlesPortalPlane_FlagsCheckOutside() { // Retail passes the whole SPHEREPATH global_sphere array into // CEnvCell::find_transit_cells. The head sphere can be the one that // overlaps an exit portal while the foot sphere is still clear. var exitCell = MakeCellWithPortalAtRightWall(Matrix4x4.Identity, otherCellId: 0xFFFF, flags: 0); var cache = new PhysicsDataCache(); cache.RegisterCellStructForTest(0xA9B40100u, exitCell); var spheres = new[] { new Sphere { Origin = new Vector3(0.0f, 0f, 2.5f), Radius = 0.5f }, new Sphere { Origin = new Vector3(2.0f, 0f, 3.2f), Radius = 0.5f }, }; var candidates = new HashSet(); CellTransit.FindTransitCellsSphere( cache, exitCell, currentCellId: 0xA9B40100u, spheres, spheres.Length, candidates, out bool exitOutside); Assert.True(exitOutside); } }