acdream/tests/AcDream.Core.Tests/World/LandblockLoaderTests.cs
Erik 35b37dfb5f chore(phys): A6.P3 #98 triage — revert neg-poly + bldg-check experiments
Triage step from the plan at C:\Users\erikn\.claude\plans\
i-did-some-work-sharded-acorn.md. Four sessions on issue #98 left the
worktree dirty with ~1352 LOC of mixed work. This commit splits the
work into "keep" (defensible + diagnostic) and "drop" (failed
experiments), then commits the keep set with the drops removed.

Plan asked for three commits (diag / fix / revert); consolidated to one
because the diagnostic emits in TransitionTypes.cs are tightly
interleaved with the multi-sphere CellTransit calls and the CellId
switch. Hunk-level splitting in those files for marginal bisect
granularity didn't justify the misclick risk.

Reverted entirely (failed experiments per slice 7 handoff):
- src/AcDream.Core/Physics/PhysicsDataCache.cs — neg-poly storage
  fields (Stippling, PosSurface, NegSurface, HasNegativeSide,
  IsNegativeSide, NegativeSide).
- src/AcDream.Core/Physics/ShadowObjectRegistry.cs — isBuilding flag
  propagation through Register / ShadowEntry.
- tests/AcDream.Core.Tests/Physics/BSPQueryTests.cs — 165 lines of
  PolygonWithNegativeSide_* tests.
- tests/AcDream.Core.Tests/Physics/ShadowObjectRegistryTests.cs —
  isBuilding propagation tests.
- src/AcDream.Core/World/WorldEntity.cs — IsLandblockBuilding field
  (no consumer once ShadowObjectRegistry.isBuilding is gone).
- src/AcDream.Core/World/LandblockLoader.cs — IsLandblockBuilding=true
  setter on building entities (kept BuildBuildingTerrainCells).
- src/AcDream.App/Rendering/GameWindow.cs — isBuilding: arg passed to
  ShadowObjects.Register.
- src/AcDream.Core/Physics/BSPQuery.cs — TryAdjustWalkableSide /
  IsWalkableAt helpers, their callers, the Path 5 / Path 6 neg-poly
  branch split, the BldgCheck-tied clearCell conditional, and the
  neg-poly ResolveCellPolygons writes.
- src/AcDream.Core/Physics/PhysicsDiagnostics.cs — neg-poly fields
  in the poly-dump format.
- src/AcDream.Core/Physics/TransitionTypes.cs — SpherePath.BldgCheck +
  SpherePath.HitsInteriorCell fields and every consumer, the
  savedBldgCheck try/finally around FindCollisions, and the neg-poly
  format additions to the dump-on-error helper.
- src/AcDream.Core/Physics/CellTransit.cs — FindCellSet overloads
  with hitsInteriorCell out-param and the BuildCellSetAndPickContaining
  out-param threading.

Kept (defensible correctness fixes + diagnostic infrastructure):
- src/AcDream.App/Rendering/GameWindow.cs — render-vs-physics cell
  origin split: the 0.02m render lift no longer leaks into physics
  BSP caching. lb.BuildingTerrainCells threaded into LandblockMesh.Build.
- src/AcDream.Core/World/LoadedLandblock.cs — BuildingTerrainCells
  record field.
- src/AcDream.Core/World/LandblockLoader.cs — BuildBuildingTerrainCells
  (cy*8+cx from LandBlockInfo.Buildings).
- src/AcDream.Core/Terrain/LandblockMesh.cs — hiddenTerrainCells
  param that collapses owned-cell triangles to a zero-area degenerate.
- src/AcDream.App/Streaming/{GpuWorldState,LandblockStreamer}.cs —
  mechanical BuildingTerrainCells threading through LoadedLandblock
  reconstructions.
- src/AcDream.Core/Physics/CellTransit.cs — multi-sphere
  FindTransitCellsSphere variant + multi-sphere AddAllOutsideCells +
  FindCellSet(IReadOnlyList<Sphere>, …) overload + the
  BSPQuery.SphereIntersectsCellBsp call for loaded neighbours. Matches
  retail CObjCell::find_cell_list / CEnvCell::find_transit_cells.
- src/AcDream.Core/Physics/TransitionTypes.cs — multi-sphere FindCellSet
  call site, retail-faithful CellId switch after CheckOtherCells, the
  outdoor-landcell terrain-walkable fallback in CheckOtherCells, and
  the full diagnostic suite ([step-walk], [walkable-nearest],
  [issue98-walkable-detail], [cell-set-summary], LastBspHitPoly
  emits).
- src/AcDream.Core/Physics/PhysicsDiagnostics.cs — ProbeStepWalkEnabled
  gate (ACDREAM_PROBE_STEP_WALK=1) + LogStepWalk helper + FormatVector
  / FormatPlane utilities. All emit-gated.
- src/AcDream.Core/Physics/BSPQuery.cs — diagnostic emits to
  LastBspHitPoly at four sites in SphereIntersectsPolyInternal /
  the placement adjustment path.
- Test files for the kept work: CellTransitFindCellSetTests,
  CellTransitFindTransitCellsSphereTests, PhysicsDiagnosticsTests,
  TransitionCheckOtherCellsTests, LandblockMeshTests,
  LandblockLoaderTests.

Verification:
- dotnet build: green, 0 errors, 3 pre-existing warnings.
- dotnet test: 1156 passed + 8 failed (baseline was 1148 + 8 pre-
  existing; the +8 passing are the new tests for the kept defensible
  work). Same 8 pre-existing failures, no new regressions.

Backup of pre-triage worktree state in stash@{0}.

A6.P3 #98 is still open; this is the apparatus-prep step, not a fix.
Next: cell-dump probe (Step 2 of the plan).
2026-05-23 15:11:49 +02:00

194 lines
6.3 KiB
C#

using System.Numerics;
using AcDream.Core.World;
using DatReaderWriter.DBObjs;
using DatReaderWriter.Types;
namespace AcDream.Core.Tests.World;
public class LandblockLoaderTests
{
private static LandBlock BuildFlatLandBlock()
{
var block = new LandBlock
{
HasObjects = true,
Terrain = new TerrainInfo[81],
Height = new byte[81],
};
for (int i = 0; i < 81; i++)
{
block.Terrain[i] = (ushort)0;
block.Height[i] = 0;
}
return block;
}
[Fact]
public void BuildEntitiesFromInfo_StabsAndBuildings_AreMappedToEntities()
{
var info = new LandBlockInfo
{
Objects =
{
new Stab
{
Id = 0x01000042u, // GfxObj id
Frame = new Frame
{
Origin = new Vector3(10, 20, 5),
Orientation = Quaternion.Identity,
},
},
new Stab
{
Id = 0x02000099u, // Setup id
Frame = new Frame
{
Origin = new Vector3(30, 40, 10),
Orientation = Quaternion.Identity,
},
},
},
Buildings =
{
new BuildingInfo
{
ModelId = 0x020000AAu, // Setup for a building
Frame = new Frame
{
Origin = new Vector3(50, 60, 0),
Orientation = Quaternion.Identity,
},
},
},
};
var entities = LandblockLoader.BuildEntitiesFromInfo(info);
Assert.Equal(3, entities.Count);
Assert.Contains(entities, e => e.SourceGfxObjOrSetupId == 0x01000042u && e.Position == new Vector3(10, 20, 5));
Assert.Contains(entities, e => e.SourceGfxObjOrSetupId == 0x02000099u && e.Position == new Vector3(30, 40, 10));
Assert.Contains(entities, e => e.SourceGfxObjOrSetupId == 0x020000AAu && e.Position == new Vector3(50, 60, 0));
}
[Fact]
public void BuildEntitiesFromInfo_AssignsMonotonicIds()
{
var info = new LandBlockInfo
{
Objects =
{
new Stab { Id = 0x01000001u, Frame = new Frame() },
new Stab { Id = 0x01000002u, Frame = new Frame() },
new Stab { Id = 0x01000003u, Frame = new Frame() },
},
};
var entities = LandblockLoader.BuildEntitiesFromInfo(info);
var ids = entities.Select(e => e.Id).OrderBy(i => i).ToArray();
Assert.Equal(3, ids.Distinct().Count()); // all unique
}
[Fact]
public void BuildEntitiesFromInfo_UnsupportedIdType_IsSkipped()
{
// 0x03xxxxxx is neither GfxObj (0x01) nor Setup (0x02).
var info = new LandBlockInfo
{
Objects =
{
new Stab { Id = 0x01000001u, Frame = new Frame() },
new Stab { Id = 0x03000002u, Frame = new Frame() }, // skipped
new Stab { Id = 0x02000003u, Frame = new Frame() },
},
};
var entities = LandblockLoader.BuildEntitiesFromInfo(info);
Assert.Equal(2, entities.Count);
Assert.DoesNotContain(entities, e => e.SourceGfxObjOrSetupId == 0x03000002u);
}
[Fact]
public void BuildEntitiesFromInfo_Empty_ReturnsEmpty()
{
var entities = LandblockLoader.BuildEntitiesFromInfo(new LandBlockInfo());
Assert.Empty(entities);
}
[Fact]
public void BuildBuildingTerrainCells_UsesBuildingsOnlyAndMapsToMeshCellIndex()
{
var info = new LandBlockInfo
{
Objects =
{
new Stab
{
Id = 0x02000001u,
Frame = new Frame { Origin = new Vector3(120, 72, 0) },
},
},
Buildings =
{
new BuildingInfo
{
ModelId = 0x020000AAu,
Frame = new Frame { Origin = new Vector3(141.5f, 7.2f, 94f) },
},
},
};
var cells = LandblockLoader.BuildBuildingTerrainCells(info);
Assert.Single(cells);
Assert.Contains(5, cells); // cy=0, cx=5 => mesh index cy * 8 + cx.
}
[Fact]
public void BuildEntitiesFromInfo_WithLandblockId_NamespacesIdsForGlobalUniqueness()
{
// Regression: cross-LB stab Id collision was the cause of visual
// glitches in Tier 1 cache (commit <THIS_COMMIT>) — buildings rendered
// up in the air with wrong textures because cache was keyed by
// entity.Id and stab Ids restarted at 1 per landblock.
var info = new LandBlockInfo
{
Objects =
{
new Stab { Id = 0x01000001u, Frame = new Frame() },
new Stab { Id = 0x01000002u, Frame = new Frame() },
},
};
var entitiesLbA = LandblockLoader.BuildEntitiesFromInfo(info, landblockId: 0xA9B40000u);
var entitiesLbB = LandblockLoader.BuildEntitiesFromInfo(info, landblockId: 0xA9B50000u);
// No two entities across LB A and LB B share the same Id.
var idsA = entitiesLbA.Select(e => e.Id).ToArray();
var idsB = entitiesLbB.Select(e => e.Id).ToArray();
Assert.Empty(idsA.Intersect(idsB));
// The namespace top byte is 0xC0 for stabs (distinct from 0x80 scenery,
// 0x40 interior, low-range live entities).
Assert.All(idsA, id => Assert.Equal(0xC0u, (id >> 24) & 0xFFu));
Assert.All(idsB, id => Assert.Equal(0xC0u, (id >> 24) & 0xFFu));
}
[Fact]
public void BuildEntitiesFromInfo_LegacyZeroLandblockId_StartsAtOne()
{
// Backward compat: existing callers (tests pre-fix) call without a
// landblockId and get the legacy "starts at 1" behavior.
var info = new LandBlockInfo
{
Objects = { new Stab { Id = 0x01000001u, Frame = new Frame() } },
};
var entities = LandblockLoader.BuildEntitiesFromInfo(info);
Assert.Single(entities);
Assert.Equal(1u, entities[0].Id);
}
}