acdream/tests/AcDream.Core.Tests/Physics/RemoveCellsForLandblockTests.cs
Erik a9f8775110 fix(physics): D8 — RemoveCellsForLandblock; cell transforms rebase per apply
Mirrors RemoveBuildingsForLandblock (#146) for indoor CellPhysics entries.
_cellStruct is first-wins (CacheCellStruct's ContainsKey guard); without
eviction a dungeon's BSP WorldTransform is permanently locked to the
_liveCenter value at first streaming — a teleport recenter leaves cells at a
stale offset (~source↔dest distance), and foot-sphere collision queries miss
the geometry that visually renders correctly.

PhysicsDataCache.RemoveCellsForLandblock iterates _cellStruct.Keys and
TryRemove-s every entry whose high-word matches the evicted landblock prefix.
PhysicsEngine.RemoveLandblock now calls it alongside ShadowObjects.RemoveLandblock
so cell BSPs rebase on the next CacheCellStruct pass, same as buildings.

No divergence register row needed: this closes a gap introduced when the #146
building-eviction pattern was created without the symmetric cell eviction.

Tests: RemoveCellsForLandblockTests (3 cases): evicts-matching-prefix,
empty-cache no-throw, no-matching-cells leaves-others. Core suite: 1587 pass / 0 fail.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-24 19:34:16 +02:00

66 lines
2.3 KiB
C#

using System.Collections.Generic;
using AcDream.Core.Physics;
using Xunit;
namespace AcDream.Core.Tests.Physics;
/// <summary>
/// D8 (2026-06-24): verify that <see cref="PhysicsDataCache.RemoveCellsForLandblock"/>
/// evicts every cached cell belonging to the given landblock prefix and leaves
/// cells belonging to other landblocks untouched — symmetric with
/// <see cref="PhysicsDataCache.RemoveBuildingsForLandblock"/> (#146).
/// </summary>
public class RemoveCellsForLandblockTests
{
/// <summary>
/// Build the absolute minimum <see cref="CellPhysics"/> that satisfies the
/// <c>required</c> init property. BSP is left null because the test exercises
/// only the cache-eviction key logic, not BSP traversal.
/// </summary>
private static CellPhysics BuildMinimalCell() => new()
{
Resolved = new Dictionary<ushort, ResolvedPolygon>(),
};
[Fact]
public void RemoveCellsForLandblock_EvictsOnlyMatchingPrefix()
{
var cache = new PhysicsDataCache();
// Two cells from landblock 0xAAAA0000 and one from 0xBBBB0000.
cache.RegisterCellStructForTest(0xAAAA0100u, BuildMinimalCell());
cache.RegisterCellStructForTest(0xAAAA0200u, BuildMinimalCell());
cache.RegisterCellStructForTest(0xBBBB0100u, BuildMinimalCell());
cache.RemoveCellsForLandblock(0xAAAA0000u);
// Both AAAA cells must be gone.
Assert.Null(cache.GetCellStruct(0xAAAA0100u));
Assert.Null(cache.GetCellStruct(0xAAAA0200u));
// The BBBB cell must survive.
Assert.NotNull(cache.GetCellStruct(0xBBBB0100u));
}
[Fact]
public void RemoveCellsForLandblock_EmptyCache_DoesNotThrow()
{
// Must be a no-op on an empty cache.
var cache = new PhysicsDataCache();
cache.RemoveCellsForLandblock(0xAAAA0000u);
Assert.Equal(0, cache.CellStructCount);
}
[Fact]
public void RemoveCellsForLandblock_NoMatchingCells_LeavesOthersIntact()
{
var cache = new PhysicsDataCache();
cache.RegisterCellStructForTest(0xBBBB0100u, BuildMinimalCell());
// Evicting a prefix that has no cached entries should not disturb others.
cache.RemoveCellsForLandblock(0xAAAA0000u);
Assert.NotNull(cache.GetCellStruct(0xBBBB0100u));
Assert.Equal(1, cache.CellStructCount);
}
}