feat(render): Phase A8 RR3 — Building + BuildingRegistry + BuildingLoader

New per-landblock data model for WB-style per-building cell scoping:

  Building            — BuildingId, EnvCellIds, ExitPortalPolygons,
                        occlusion-query state (Step 5 lifecycle)
  BuildingRegistry    — two-way indexed (by cellId + by buildingId);
                        single source of truth per landblock
  BuildingLoader      — static factory from LandBlockInfo.Buildings;
                        walks interior portals to expand cell sets;
                        collects exit portal polygons in world space

10 new unit tests cover data invariants + registry indexing + loader
mapping per the algorithm resolved in RR2 findings.

LoadedCell.BuildingId stamping wired in RR4. Render-time consumption
arrives in RR7 (Steps 1-4) + RR9 (Step 5) + RR11 (RenderOutsideIn).

Design: docs/superpowers/specs/2026-05-26-phase-a8-wb-full-port-design.md
Spike: docs/research/2026-05-26-a8-buildings-data-shape.md

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Erik 2026-05-27 11:08:43 +02:00
parent f44a9bf943
commit f125fdb220
6 changed files with 470 additions and 0 deletions

View file

@ -0,0 +1,88 @@
using System.Collections.Generic;
using System.Numerics;
using AcDream.App.Rendering.Wb;
using DatReaderWriter.DBObjs;
using DatReaderWriter.Types;
using Xunit;
namespace AcDream.App.Tests.Rendering.Wb;
public class BuildingLoaderTests
{
// Helper: build a minimal LandBlockInfo with one BuildingInfo per supplied tuple.
// OtherCellId values are 16-bit cell-local ids (low word of full cell id).
private static LandBlockInfo MakeInfo(params (uint modelId, uint[] portalOtherCellIds)[] buildings)
{
var bls = new List<BuildingInfo>();
foreach (var (modelId, portals) in buildings)
{
var portalList = new List<BuildingPortal>();
foreach (var ocid in portals)
{
portalList.Add(new BuildingPortal
{
OtherCellId = (ushort)(ocid & 0xFFFFu),
Flags = 0,
OtherPortalId = 0,
StabList = new List<ushort>(),
});
}
bls.Add(new BuildingInfo
{
ModelId = modelId,
Frame = new Frame { Origin = Vector3.Zero, Orientation = Quaternion.Identity },
Portals = portalList,
});
}
return new LandBlockInfo
{
Objects = new List<Stab>(),
Buildings = bls,
};
}
[Fact]
public void Empty_NoBuildings_EmptyRegistry()
{
var info = new LandBlockInfo { Objects = new List<Stab>(), Buildings = new List<BuildingInfo>() };
var reg = BuildingLoader.Build(info, landblockId: 0xA9B40000u, cellsByCellId: new Dictionary<uint, AcDream.App.Rendering.LoadedCell>());
Assert.Equal(0, reg.Count);
}
[Fact]
public void OneBuilding_OnePortal_MapsToOneCell()
{
// Building points to cell 0x0150 in landblock 0xA9B40000 → full cell id 0xA9B40150
var info = MakeInfo((modelId: 0x02000123u, portalOtherCellIds: new[] { 0x0150u }));
// Pass an empty cell dict — loader seeds from BuildingInfo.Portals only
var reg = BuildingLoader.Build(info, landblockId: 0xA9B40000u, cellsByCellId: new Dictionary<uint, AcDream.App.Rendering.LoadedCell>());
Assert.Equal(1, reg.Count);
var building = System.Linq.Enumerable.First(reg.All());
Assert.Contains(0xA9B40150u, building.EnvCellIds);
}
[Fact]
public void OneBuilding_MultiplePortals_MapsToMultipleCells()
{
var info = MakeInfo((0x02000123u, new[] { 0x0150u, 0x0151u, 0x0152u }));
var reg = BuildingLoader.Build(info, 0xA9B40000u, new Dictionary<uint, AcDream.App.Rendering.LoadedCell>());
var building = System.Linq.Enumerable.First(reg.All());
Assert.Equal(3, building.EnvCellIds.Count);
Assert.Contains(0xA9B40150u, building.EnvCellIds);
Assert.Contains(0xA9B40151u, building.EnvCellIds);
Assert.Contains(0xA9B40152u, building.EnvCellIds);
}
[Fact]
public void TwoBuildings_AllocateSequentialIds()
{
var info = MakeInfo(
(0x02000001u, new[] { 0x0150u }),
(0x02000002u, new[] { 0x0160u }));
var reg = BuildingLoader.Build(info, 0xA9B40000u, new Dictionary<uint, AcDream.App.Rendering.LoadedCell>());
Assert.Equal(2, reg.Count);
var ids = new SortedSet<uint>();
foreach (var b in reg.All()) ids.Add(b.BuildingId);
Assert.Equal(new SortedSet<uint> { 1, 2 }, ids); // sequential 1, 2
}
}

View file

@ -0,0 +1,70 @@
using System.Collections.Generic;
using System.Numerics;
using AcDream.App.Rendering.Wb;
using Xunit;
namespace AcDream.App.Tests.Rendering.Wb;
public class BuildingRegistryTests
{
private static Building B(uint id, params uint[] cellIds) => new()
{
BuildingId = id,
EnvCellIds = new HashSet<uint>(cellIds),
ExitPortalPolygons = new List<Vector3[]>(),
};
[Fact]
public void Empty_NoBuildingsRegistered()
{
var reg = new BuildingRegistry();
Assert.Equal(0, reg.Count);
Assert.Empty(reg.All());
Assert.Empty(reg.GetBuildingsContainingCell(0xA9B40150u));
Assert.Null(reg.GetById(0));
}
[Fact]
public void Add_IndexesBothDirections()
{
var reg = new BuildingRegistry();
var b = B(1, 0xA9B40150u, 0xA9B40151u);
reg.Add(b);
Assert.Equal(1, reg.Count);
Assert.Same(b, reg.GetById(1));
Assert.Single(reg.GetBuildingsContainingCell(0xA9B40150u));
Assert.Single(reg.GetBuildingsContainingCell(0xA9B40151u));
Assert.Same(b, reg.GetBuildingsContainingCell(0xA9B40150u)[0]);
Assert.Empty(reg.GetBuildingsContainingCell(0xDEADBEEFu));
}
[Fact]
public void CellSharedBetweenTwoBuildings_GetBuildingsContainingCellReturnsBoth()
{
var reg = new BuildingRegistry();
var b1 = B(1, 0xA9B40150u, 0xA9B40151u);
var b2 = B(2, 0xA9B40151u, 0xA9B40152u); // shares 0151 with b1
reg.Add(b1);
reg.Add(b2);
var bothAt0151 = reg.GetBuildingsContainingCell(0xA9B40151u);
Assert.Equal(2, bothAt0151.Count);
Assert.Contains(b1, bothAt0151);
Assert.Contains(b2, bothAt0151);
}
[Fact]
public void All_EnumeratesEveryBuilding()
{
var reg = new BuildingRegistry();
reg.Add(B(1, 0xA9B40150u));
reg.Add(B(2, 0xA9B40160u));
reg.Add(B(3, 0xA9B40170u));
var ids = new HashSet<uint>();
foreach (var b in reg.All()) ids.Add(b.BuildingId);
Assert.Equal(new HashSet<uint> { 1, 2, 3 }, ids);
}
}

View file

@ -0,0 +1,44 @@
using System.Collections.Generic;
using System.Numerics;
using AcDream.App.Rendering.Wb;
using Xunit;
namespace AcDream.App.Tests.Rendering.Wb;
public class BuildingTests
{
[Fact]
public void Building_RequiredFields_PopulateCorrectly()
{
var b = new Building
{
BuildingId = 42,
EnvCellIds = new HashSet<uint> { 0xA9B40150u, 0xA9B40151u },
ExitPortalPolygons = new List<Vector3[]>
{
new[] { new Vector3(0, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 1, 0) },
},
};
Assert.Equal(42u, b.BuildingId);
Assert.Equal(2, b.EnvCellIds.Count);
Assert.Contains(0xA9B40150u, b.EnvCellIds);
Assert.Single(b.ExitPortalPolygons);
Assert.Equal(3, b.ExitPortalPolygons[0].Length);
}
[Fact]
public void Building_OcclusionQueryState_DefaultsZero()
{
var b = new Building
{
BuildingId = 0,
EnvCellIds = new HashSet<uint>(),
ExitPortalPolygons = new List<Vector3[]>(),
};
Assert.Equal(0u, b.QueryId);
Assert.False(b.QueryStarted);
Assert.False(b.WasVisible);
}
}