merge: bring main into claude/hopeful-maxwell-214a12 (LayoutDesc importer branch)

main was 65 commits ahead of this branch's fork point. Only conflict was the
divergence register: both sides appended an 'AP-32' row. Resolved by keeping
main's AP-32..AP-36 (cell-shell lift, look-in cells, alpha deferral, dungeon
streaming, point lights) and renumbering the importer's row to AP-37; AP header
count -> 37. GameWindow.cs auto-merged cleanly. Verified: AcDream.App builds
0/0; AcDream.App.Tests 354 passed / 1 skipped / 0 failed.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Erik 2026-06-15 16:19:15 +02:00
commit 5ac9d8c19c
53 changed files with 6691 additions and 439 deletions

View file

@ -0,0 +1,153 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using AcDream.App.Rendering;
using DatReaderWriter;
using DatReaderWriter.Options;
using Xunit;
using Xunit.Abstractions;
namespace AcDream.App.Tests.Rendering;
/// <summary>
/// #124 — far-building interiors under an INTERIOR root. Retail seeds the
/// look-in flood by clipping a building's aperture against the CURRENTLY
/// INSTALLED view (PView::GetClip 0x005a4320 inside ConstructView(CBldPortal)
/// 0x005a59a0): full screen outdoors, the accumulated doorway (outside) view
/// when looked into from inside. These tests pin BuildFromExterior's
/// seedRegion parameter — the port of that installed-view clip — against the
/// real Holtburg corner-building door.
/// </summary>
public class Issue124LookInSeedRegionTests
{
private readonly ITestOutputHelper _out;
public Issue124LookInSeedRegionTests(ITestOutputHelper output) => _out = output;
private const uint ExitCellId = CornerFloodReplayTests.Landblock | 0x0170u;
private static Matrix4x4 ViewProjFor(Vector3 eye, Vector3 lookAt)
{
var view = Matrix4x4.CreateLookAt(eye, lookAt, Vector3.UnitZ);
var proj = Matrix4x4.CreatePerspectiveFieldOfView(1.2f, 1280f / 720f, 1f, 5000f);
return view * proj;
}
private static (Dictionary<uint, LoadedCell> cells, LoadedCell exitCell, int exitIdx,
Vector3 centroid, Vector3 outward) LoadFixture(DatCollection dats)
{
var cells = CornerFloodReplayTests.LoadBuilding(dats);
var exitCell = cells[ExitCellId];
int exitIdx = -1;
for (int i = 0; i < exitCell.Portals.Count; i++)
{
if (exitCell.Portals[i].OtherCellId == 0xFFFF && i < exitCell.PortalPolygons.Count
&& exitCell.PortalPolygons[i].Length >= 3)
{ exitIdx = i; break; }
}
Assert.True(exitIdx >= 0);
var localPoly = exitCell.PortalPolygons[exitIdx];
Vector3 centroid = Vector3.Zero;
foreach (var lp in localPoly)
centroid += Vector3.Transform(lp, exitCell.WorldTransform);
centroid /= localPoly.Length;
var plane = exitCell.ClipPlanes[exitIdx];
var normal = Vector3.TransformNormal(plane.Normal, exitCell.WorldTransform);
var cellCenter = Vector3.Transform(
(exitCell.LocalBoundsMin + exitCell.LocalBoundsMax) * 0.5f, exitCell.WorldTransform);
// outward = away from the cell interior.
if (Vector3.Dot(normal, cellCenter - centroid) > 0)
normal = -normal;
return (cells, exitCell, exitIdx, centroid, Vector3.Normalize(normal));
}
private static Vector4 ApertureNdcAabb(LoadedCell cell, int idx, Matrix4x4 viewProj)
{
float minX = float.MaxValue, minY = float.MaxValue, maxX = float.MinValue, maxY = float.MinValue;
foreach (var lp in cell.PortalPolygons[idx])
{
var w = Vector3.Transform(lp, cell.WorldTransform);
var c = Vector4.Transform(new Vector4(w, 1f), viewProj);
Assert.True(c.W > 0.05f, "fixture eye must keep the aperture fully in front");
minX = MathF.Min(minX, c.X / c.W); maxX = MathF.Max(maxX, c.X / c.W);
minY = MathF.Min(minY, c.Y / c.W); maxY = MathF.Max(maxY, c.Y / c.W);
}
return new Vector4(minX, minY, maxX, maxY);
}
private static ViewPolygon Quad(float minX, float minY, float maxX, float maxY) =>
new(new[]
{
new Vector2(minX, minY), new Vector2(maxX, minY),
new Vector2(maxX, maxY), new Vector2(minX, maxY),
});
[Fact]
public void SeedRegion_ContainingAperture_Floods_DisjointRegion_DoesNot()
{
var datDir = CornerFloodReplayTests.ResolveDatDir();
if (datDir is null) { _out.WriteLine("SKIP: dats unavailable"); return; }
using var dats = new DatCollection(datDir, DatAccessType.Read);
var (cells, exitCell, exitIdx, centroid, outward) = LoadFixture(dats);
LoadedCell? Lookup(uint id) => cells.TryGetValue(id, out var c) ? c : null;
// Eye OUTSIDE the building, 3 m in front of the exit door, gaze at it
// — the look-in geometry of a viewer peering at this building through
// some other opening.
var eye = centroid + outward * 3f;
var viewProj = ViewProjFor(eye, centroid);
var ap = ApertureNdcAabb(exitCell, exitIdx, viewProj);
_out.WriteLine(FormattableString.Invariant(
$"aperture ndc=({ap.X:F3},{ap.Y:F3},{ap.Z:F3},{ap.W:F3})"));
// Sanity: the full-screen (outdoor-root) seed floods.
var full = PortalVisibilityBuilder.BuildFromExterior(
cells.Values, eye, Lookup, viewProj);
Assert.True(full.OrderedVisibleCells.Count > 0, "full-screen seed must flood");
// A region containing the aperture floods — and never MORE than the
// full-screen seed (region-restricting can only shrink the flood).
var containing = new[] { Quad(ap.X - 0.05f, ap.Y - 0.05f, ap.Z + 0.05f, ap.W + 0.05f) };
var seeded = PortalVisibilityBuilder.BuildFromExterior(
cells.Values, eye, Lookup, viewProj, float.PositiveInfinity, containing);
Assert.True(seeded.OrderedVisibleCells.Count > 0, "containing region must flood");
Assert.True(seeded.OrderedVisibleCells.Count <= full.OrderedVisibleCells.Count);
// A region strictly disjoint from the aperture must not flood — the
// doorway doesn't show this building, so its interior never builds
// (retail: GetClip vs the installed view returns empty → no look-in).
Assert.True(ap.Z < 0.70f || ap.X > -0.70f, "fixture aperture unexpectedly fills the screen");
var disjoint = ap.Z < 0.70f
? new[] { Quad(0.75f, 0.75f, 0.99f, 0.99f) }
: new[] { Quad(-0.99f, -0.99f, -0.75f, -0.75f) };
var none = PortalVisibilityBuilder.BuildFromExterior(
cells.Values, eye, Lookup, viewProj, float.PositiveInfinity, disjoint);
Assert.True(none.OrderedVisibleCells.Count == 0,
FormattableString.Invariant($"disjoint region flooded {none.OrderedVisibleCells.Count} cells"));
}
[Fact]
public void EyeOnInteriorSide_ExitDoorNeverSeeds()
{
// The root's own doorway must not look-in on itself: the seed eye-side
// test (retail ConstructView's sidedness vs portal_side) excludes any
// aperture the eye is on the interior side of — this is what lets the
// interior-root gather pass ALL nearby buildings including the
// viewer's own without special-casing.
var datDir = CornerFloodReplayTests.ResolveDatDir();
if (datDir is null) { _out.WriteLine("SKIP: dats unavailable"); return; }
using var dats = new DatCollection(datDir, DatAccessType.Read);
var (cells, exitCell, _, centroid, outward) = LoadFixture(dats);
LoadedCell? Lookup(uint id) => cells.TryGetValue(id, out var c) ? c : null;
var eye = centroid - outward * 2f; // 2 m INSIDE the doorway
var viewProj = ViewProjFor(eye, centroid);
var frame = PortalVisibilityBuilder.BuildFromExterior(
new[] { exitCell }, eye, Lookup, viewProj);
Assert.True(frame.OrderedVisibleCells.Count == 0,
"an interior-side eye must not seed its own cell's exit portal");
}
}

View file

@ -218,4 +218,133 @@ public class Issue127FloodFlipReplayTests
Assert.Fail($"flood admission differs across the captured 4 cm pair (preGate={preGate}, fov={fov:F2}) — see output for the flipping cells");
}
}
// Centre of a building group's exit-portal AABB (world space).
private static (bool Has, Vector3 Center) PortalCenterFor(List<LoadedCell> group)
{
var (has, min, max) = PortalBoundsFor(group);
return (has, (min + max) * 0.5f);
}
// Per-building admitted cells (this group only) at one (eye, gaze) — the
// production per-building flood + optional PortalBounds frustum pre-gate.
private static HashSet<uint> BuildingAdmits(
World w, List<LoadedCell> group, Vector3 eye, Matrix4x4 viewProj,
FrustumPlanes frustum, bool withPreGate)
{
var result = new HashSet<uint>();
if (withPreGate)
{
var (has, min, max) = PortalBoundsFor(group);
if (has && !FrustumCuller.IsAabbVisible(frustum, min, max))
return result;
}
var bf = PortalVisibilityBuilder.ConstructViewBuilding(group, eye, w.Lookup, viewProj);
foreach (uint id in bf.OrderedVisibleCells)
result.Add(id);
return result;
}
/// <summary>
/// #127 distant-building churn detector. The captured 4 cm pair is now
/// stable (the near-eye W=0 clip port), but the user symptom is buildings
/// flickering when RUNNING PAST at a distance. This strafes the eye past
/// each loaded building at several distances in 1 mm steps with the gaze
/// fixed forward (the run-past geometry) and counts, per building cell, how
/// many times its admission toggles over the monotone strafe. A stable
/// flood toggles a cell AT MOST ONCE along a monotone eye path (it enters
/// or leaves the view a single time); >=2 toggles is churn — the building
/// flickers. preGate off vs on separates flood-math churn from the
/// PortalBounds frustum pre-gate.
///
/// RESULT (2026-06-12, HEAD post-W=0-clip-port + #120 containment): ZERO
/// churning cases across all 21 building groups x {10,30,60,120,190} m x
/// 100 mm-steps, both preGate states. The near-eye knife-edge class the
/// W=0 polyClipFinish port (987313a) killed was the distant-building
/// flicker too; the user re-gate ("Seems to have been fixed") agrees.
/// Now the REGRESSION PIN — it asserts zero churn.
/// </summary>
[Fact]
public void DistantBuildingStrafe_NoAdmissionChurn()
{
var w = LoadWorld();
if (w is null) return;
const float fovY = MathF.PI / 3f;
const float eyeHeight = 1.8f;
const float strafeSpanM = 0.10f; // 10 cm strafe
const int strafeSteps = 100; // 1 mm/step
var distances = new[] { 10f, 30f, 60f, 120f, 190f };
int totalChurn = 0;
foreach (bool preGate in new[] { false, true })
{
int worstToggles = 0;
string worstDesc = "(none)";
int churningCases = 0;
for (int gi = 0; gi < w.BuildingGroups.Count; gi++)
{
var group = w.BuildingGroups[gi];
var (has, center) = PortalCenterFor(group);
if (!has) continue;
foreach (float dist in distances)
{
// Eye south of the building at eye height, gaze NORTH toward
// the building centre; strafe along world +X (run-past).
var gaze = Vector3.Normalize(new Vector3(0f, 1f, -0.05f));
var strafeDir = Vector3.Normalize(Vector3.Cross(Vector3.UnitZ, gaze)); // ~world +X
var eyeBase = new Vector3(center.X, center.Y - dist, center.Z + eyeHeight)
- strafeDir * (strafeSpanM * 0.5f);
var toggleCount = new Dictionary<uint, int>();
var prevIn = new Dictionary<uint, bool>();
for (int s = 0; s <= strafeSteps; s++)
{
var eye = eyeBase + strafeDir * (strafeSpanM * s / strafeSteps);
var vp = ViewProjFor(eye, gaze, fovY);
var frustum = FrustumPlanes.FromViewProjection(vp);
var admits = BuildingAdmits(w, group, eye, vp, frustum, preGate);
var seen = new HashSet<uint>(admits);
foreach (uint id in seen)
{
bool wasIn = prevIn.TryGetValue(id, out var p) && p;
if (!wasIn && prevIn.ContainsKey(id))
toggleCount[id] = toggleCount.GetValueOrDefault(id) + 1;
prevIn[id] = true;
}
foreach (var id in new List<uint>(prevIn.Keys))
if (!seen.Contains(id))
{
if (prevIn[id])
toggleCount[id] = toggleCount.GetValueOrDefault(id) + 1;
prevIn[id] = false;
}
}
foreach (var (id, toggles) in toggleCount)
{
if (toggles < 2) continue; // <=1 = clean enter/leave
churningCases++;
if (toggles > worstToggles)
{
worstToggles = toggles;
worstDesc = FormattableString.Invariant(
$"group#{gi} dist={dist:F0}m cell=0x{id:X8} toggles={toggles}");
}
}
}
}
_out.WriteLine(FormattableString.Invariant(
$"preGate={preGate}: churningCases={churningCases} worst={worstDesc} (worstToggles={worstToggles})"));
totalChurn += churningCases;
}
Assert.True(totalChurn == 0,
$"{totalChurn} distant-building admission churn case(s) — a building's cells toggle >=2x " +
"over a monotone run-past strafe (the #127 flicker); see output for the worst building/distance/cell");
}
}

View file

@ -0,0 +1,68 @@
using System;
using AcDream.App.Rendering;
using Xunit;
namespace AcDream.App.Tests.Rendering;
/// <summary>
/// #129 — doors/doorways leak through terrain and houses from over a landblock
/// away. The punch's mark pass (#117, AD-18) biased the aperture fan toward
/// the viewer by a CONSTANT 0.0005 NDC. NDC depth is non-linear: a constant
/// NDC bias b spans ≈ b·d²·(fn)/(f·n) meters of eye depth at eye distance d
/// — 0.125 m at 5 m but ~190 m at a landblock (znear 0.1), so distant
/// occluders in front of an aperture passed the mark and were far-Z punched:
/// the door-shaped leak. The fix caps the bias's eye-space span
/// (PortalDepthMaskRenderer.MarkBiasNdc): identical to the validated constant
/// below the ~10 m crossover, never more than the cap beyond it.
/// </summary>
public class Issue129PunchBiasTests
{
private const float Near = PortalDepthMaskRenderer.CameraNearPlaneMeters; // 0.1 (retail znear)
private const float Far = 5000f;
/// <summary>Eye-depth span (meters) covered by an NDC depth bias b at eye
/// distance d: ndc(d) = f(dn)/((fn)d) ⇒ d(ndc) inverse ⇒
/// span = b·d²·(fn)/(f·n) (exact for small b via the derivative).</summary>
private static float EyeSpanMeters(float biasNdc, float d) =>
biasNdc * d * d * (Far - Near) / (Far * Near);
[Fact]
public void OldConstantBias_SpansMetersAtALandblock_TheLeak()
{
// The refuted form (documentation of WHY the constant was wrong):
// 0.0005 NDC at ~one landblock spans far more eye depth than any
// occluder separation — everything in front got punched.
Assert.True(EyeSpanMeters(0.0005f, 192f) > 100f);
// ...while at close range it was a sane sliver:
Assert.InRange(EyeSpanMeters(0.0005f, 5f), 0.05f, 0.30f);
}
[Fact]
public void CappedBias_MatchesValidatedConstant_AtCloseRange()
{
// Below the crossover the T5-validated constant must win unchanged —
// this preserves the #108 grass coverage bit-for-bit.
foreach (float d in new[] { 0.5f, 1f, 3f, 5f, 8f, 9.9f })
Assert.Equal(0.0005f, PortalDepthMaskRenderer.MarkBiasNdc(d), 6);
}
[Fact]
public void CappedBias_EyeSpanNeverExceedsCap_AtAnyDistance()
{
for (float d = 1f; d <= 400f; d += 1f)
{
float span = EyeSpanMeters(PortalDepthMaskRenderer.MarkBiasNdc(d), d);
Assert.True(span <= PortalDepthMaskRenderer.PunchMarkBiasEyeCapMeters * 1.02f,
FormattableString.Invariant($"bias spans {span:F2} m of eye depth at d={d} m"));
}
}
[Fact]
public void CappedBias_At200m_CannotReachOccluders()
{
// The reported #129 distance: occluder separations are tens of
// meters; the punch reach must stay under the 0.5 m cap.
float span = EyeSpanMeters(PortalDepthMaskRenderer.MarkBiasNdc(200f), 200f);
Assert.True(span <= 0.51f, FormattableString.Invariant($"span {span:F3} m at 200 m"));
}
}

View file

@ -0,0 +1,435 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using AcDream.App.Rendering;
using DatReaderWriter;
using DatReaderWriter.Options;
using Xunit;
using Xunit.Abstractions;
namespace AcDream.App.Tests.Rendering;
/// <summary>
/// #130 — background-color strip along the TOP outer edge of a doorway when
/// looking out from inside. Mechanism model (2026-06-12 evidence sweep): for
/// an interior root the SEAL stamps the FULL raw dat portal polygon at true
/// depth (PortalDepthMaskRenderer, root-cell slice = full screen), while
/// terrain/sky COLOR is gated per fragment by the OutsideView region — the
/// same dat polygon run through ProjectToClip → ClipToRegion (1-px
/// MergeSubPixelVertices) → ClipPlaneSet.From (0.5° collinear merge) → planes,
/// with a Floor/Ceil pixel scissor (BeginDoorwayScissor) on the slice AABB on
/// top. Every one of those passes can only SHRINK the gate, so any shave shows
/// as a strip of clear color between the gate's top edge and the aperture's
/// rasterized top edge (the shell wall starts above it; the seal z-kills
/// everything beyond; nothing re-covers).
///
/// This harness measures that gap headlessly at the real Holtburg corner
/// building exit door (A9B4 0x0170, the HouseExitWalkReplay door): project the
/// aperture, run the production flood + assembler, then walk sample points
/// just inside the aperture's top edge downward until the gate admits them.
/// Plane-gap and scissor-gap are measured separately (mechanism attribution).
///
/// VERDICT (2026-06-12, 147 eye/gaze combos): the CPU polygon pipeline is
/// sub-pixel exact (worst 0.54 px) — the W=0 clip port 987313a and both merge
/// passes are EXONERATED. The strip was the scissor box: the old
/// Floor(origin)+Ceiling(size) form cut up to 1 px off the TOP/RIGHT edges at
/// unlucky fractional alignments (captured live by this harness: top edge
/// y=0.7938 at 1080p → row 968 cut; right edge x=0.3503 at 1920 → column 1296
/// cut). Fixed by the conservative NdcScissorRect bound; the assertions below
/// pin both properties.
/// </summary>
public class Issue130DoorwayStripTests
{
private readonly ITestOutputHelper _out;
public Issue130DoorwayStripTests(ITestOutputHelper output) => _out = output;
private const uint ExitCellId = CornerFloodReplayTests.Landblock | 0x0170u;
// Production projection convention (CornerFloodReplayTests.ViewProjFor):
// FovY 1.2 rad, 1280x720 viewport, near 1, far 5000. The flood clip is
// near-independent so near/far exactness is not load-bearing.
private static Matrix4x4 ViewProjFor(Vector3 eye, Vector3 lookAt)
{
var view = Matrix4x4.CreateLookAt(eye, lookAt, Vector3.UnitZ);
var proj = Matrix4x4.CreatePerspectiveFieldOfView(1.2f, 1280f / 720f, 1f, 5000f);
return view * proj;
}
[Fact]
public void Diagnostic_ExitDoorTopEdge_GateVsAperture()
{
var datDir = CornerFloodReplayTests.ResolveDatDir();
if (datDir is null) { _out.WriteLine("SKIP: dats unavailable"); return; }
using var dats = new DatCollection(datDir, DatAccessType.Read);
var cells = CornerFloodReplayTests.LoadBuilding(dats);
var root = cells[ExitCellId];
LoadedCell? Lookup(uint id) => cells.TryGetValue(id, out var c) ? c : null;
// Find the exit portal (OtherCellId == 0xFFFF) and its world polygon.
int exitIdx = -1;
for (int i = 0; i < root.Portals.Count; i++)
{
if (root.Portals[i].OtherCellId == 0xFFFF && i < root.PortalPolygons.Count
&& root.PortalPolygons[i].Length >= 3)
{ exitIdx = i; break; }
}
Assert.True(exitIdx >= 0, "0x0170 has no exit portal polygon");
var localPoly = root.PortalPolygons[exitIdx];
// DRAWN space: the shell that rasterizes the aperture (and the seal fan)
// draws +ShellDrawLiftZ above the physics transform — the gate must be
// compared against the drawn hole, not the physics polygon (#130: the
// unlifted gate left a 2 cm background strip under the drawn lintel).
var worldPoly = new Vector3[localPoly.Length];
for (int i = 0; i < localPoly.Length; i++)
{
worldPoly[i] = Vector3.Transform(localPoly[i], root.WorldTransform);
worldPoly[i].Z += PortalVisibilityBuilder.ShellDrawLiftZ;
}
Vector3 centroid = Vector3.Zero;
foreach (var w in worldPoly) centroid += w;
centroid /= worldPoly.Length;
// Inward direction: the portal plane normal signed toward the cell
// interior (ClipPlanes carries InsideSide from the load).
var plane = root.ClipPlanes[exitIdx];
var worldNormal = Vector3.TransformNormal(plane.Normal, root.WorldTransform);
var cellCenterWorld = Vector3.Transform(
(root.LocalBoundsMin + root.LocalBoundsMax) * 0.5f, root.WorldTransform);
if (Vector3.Dot(worldNormal, cellCenterWorld - centroid) < 0)
worldNormal = -worldNormal;
worldNormal = Vector3.Normalize(worldNormal);
_out.WriteLine(FormattableString.Invariant(
$"exit portal idx={exitIdx} verts={localPoly.Length} centroid=({centroid.X:F2},{centroid.Y:F2},{centroid.Z:F2}) inward=({worldNormal.X:F2},{worldNormal.Y:F2},{worldNormal.Z:F2})"));
for (int i = 0; i < worldPoly.Length; i++)
_out.WriteLine(FormattableString.Invariant(
$" poly[{i}] world=({worldPoly[i].X:F3},{worldPoly[i].Y:F3},{worldPoly[i].Z:F3})"));
float worstPlaneGapPx = 0f, worstScissorGapPx = 0f;
string worstDesc = "(none)";
// Eye sweep: back off the doorway along the inward normal at several
// distances/heights/lateral offsets; gaze at the centroid plus raised /
// lowered targets (NDC alignment of the top edge varies with gaze).
var lateral = Vector3.Normalize(Vector3.Cross(worldNormal, Vector3.UnitZ));
float[] dists = { 0.6f, 1.0f, 1.6f, 2.4f, 3.5f };
float[] heights = { 0.9f, 1.4f, 1.7f };
float[] laterals = { -0.8f, 0f, 0.8f };
float[] gazeRaise = { -0.4f, 0f, 0.4f, 0.9f };
int evaluated = 0;
foreach (float d in dists)
foreach (float h in heights)
foreach (float lat in laterals)
foreach (float gz in gazeRaise)
{
var eye = centroid + worldNormal * d + lateral * lat;
eye.Z = centroid.Z - 1.0f + h; // door centroid sits mid-opening; bias to floor-ish
var look = centroid + new Vector3(0, 0, gz);
var viewProj = ViewProjFor(eye, look);
// Aperture truth: the seal's footprint = the raw polygon's projection.
var clip = new Vector4[worldPoly.Length];
float minW = float.MaxValue;
for (int i = 0; i < worldPoly.Length; i++)
{
clip[i] = Vector4.Transform(new Vector4(worldPoly[i], 1f), viewProj);
minW = MathF.Min(minW, clip[i].W);
}
if (minW <= 0.05f) continue; // eye in/behind the door plane — out of #130's scenario
var aperture = new Vector2[clip.Length];
for (int i = 0; i < clip.Length; i++)
aperture[i] = new Vector2(clip[i].X / clip[i].W, clip[i].Y / clip[i].W);
var pv = PortalVisibilityBuilder.Build(root, eye, Lookup, viewProj,
buildingMembership: null, drawLiftZ: PortalVisibilityBuilder.ShellDrawLiftZ);
var asm = ClipFrameAssembler.Assemble(ClipFrame.NoClip(), pv);
if (asm.OutsideViewSlices.Length == 0)
{
_out.WriteLine(FormattableString.Invariant(
$"d={d} h={h} lat={lat} gz={gz}: NO outside slice (outPolys={pv.OutsideView.Polygons.Count})"));
continue;
}
evaluated++;
(float planeGapPx, float scissorGapPx, float atX) =
MeasureTopEdgeGap(aperture, asm.OutsideViewSlices, 1920, 1080);
if (planeGapPx > worstPlaneGapPx || scissorGapPx > worstScissorGapPx)
{
worstDesc = FormattableString.Invariant(
$"d={d} h={h} lat={lat} gz={gz} minW={minW:F2} atX={atX:F3} slices={asm.OutsideViewSlices.Length} mode={asm.TerrainMode} outVerts={DescribePolys(pv.OutsideView)} apVerts={aperture.Length}");
worstPlaneGapPx = MathF.Max(worstPlaneGapPx, planeGapPx);
worstScissorGapPx = MathF.Max(worstScissorGapPx, scissorGapPx);
}
if (planeGapPx > 0.55f || scissorGapPx > 0.55f)
{
_out.WriteLine(FormattableString.Invariant(
$"GAP d={d} h={h} lat={lat} gz={gz}: planeGap={planeGapPx:F2}px scissorGap={scissorGapPx:F2}px atX={atX:F3} mode={asm.TerrainMode} outVerts={DescribePolys(pv.OutsideView)}"));
float apTop = TopBoundaryY(aperture, atX);
foreach (var slice in asm.OutsideViewSlices)
_out.WriteLine(FormattableString.Invariant(
$" slice slot={slice.Slot} planes={slice.Planes.Length} aabb=({slice.NdcAabb.X:F4},{slice.NdcAabb.Y:F4},{slice.NdcAabb.Z:F4},{slice.NdcAabb.W:F4}) apTopAtX={apTop:F4}"));
foreach (var poly in pv.OutsideView.Polygons)
{
var sb = new System.Text.StringBuilder(" outPoly:");
foreach (var v in poly.Vertices)
sb.Append(FormattableString.Invariant($" ({v.X:F4},{v.Y:F4})"));
_out.WriteLine(sb.ToString());
}
}
}
_out.WriteLine(FormattableString.Invariant(
$"evaluated={evaluated} worstPlaneGapPx={worstPlaneGapPx:F2} worstScissorGapPx={worstScissorGapPx:F2} @ {worstDesc}"));
Assert.True(evaluated > 100, $"sweep degenerated: only {evaluated} eye/gaze combos evaluated");
// PIN 1 (#130): the scissor box never cuts a fragment the plane gate
// admits — conservative containment (AD-17's over-include doctrine).
// One probe step is ~0.11 px; anything beyond it is a real cut row.
Assert.True(worstScissorGapPx <= 0.15f, FormattableString.Invariant(
$"scissor under-covers the plane-admitted region by {worstScissorGapPx:F2}px @ {worstDesc}"));
// PIN 2 (canary): the CPU polygon pipeline (ProjectToClip → ClipToRegion
// merges → ClipPlaneSet planes) stays sub-pixel exact against the raw
// aperture projection. Observed 0.54 px worst (2026-06-12); the
// production vertex-merge floor is ~1 px — beyond 1.2 px means a new
// under-inclusion shaver entered the pipeline.
Assert.True(worstPlaneGapPx <= 1.2f, FormattableString.Invariant(
$"plane gate under-covers the aperture top edge by {worstPlaneGapPx:F2}px @ {worstDesc}"));
}
/// <summary>Sensitivity proof + regression documentation: a gate built in
/// PHYSICS space (drawLiftZ 0) against the DRAWN (lifted) aperture shows a
/// multi-pixel strip at a close doorway — the user-visible #130 strip
/// (f35cb8b split the lift out of the visibility transform; the OutsideView
/// kept gating drawn color in unlifted space). If this stops failing-by-gap,
/// the lift is gone and the production drawLiftZ plumbing can go too.</summary>
[Fact]
public void UnliftedGate_LeavesTheStripAtTheDrawnTopEdge()
{
var datDir = CornerFloodReplayTests.ResolveDatDir();
if (datDir is null) { _out.WriteLine("SKIP: dats unavailable"); return; }
using var dats = new DatCollection(datDir, DatAccessType.Read);
var cells = CornerFloodReplayTests.LoadBuilding(dats);
var root = cells[ExitCellId];
LoadedCell? Lookup(uint id) => cells.TryGetValue(id, out var c) ? c : null;
int exitIdx = -1;
for (int i = 0; i < root.Portals.Count; i++)
{
if (root.Portals[i].OtherCellId == 0xFFFF && i < root.PortalPolygons.Count
&& root.PortalPolygons[i].Length >= 3)
{ exitIdx = i; break; }
}
Assert.True(exitIdx >= 0);
var localPoly = root.PortalPolygons[exitIdx];
var worldPoly = new Vector3[localPoly.Length];
Vector3 centroid = Vector3.Zero;
for (int i = 0; i < localPoly.Length; i++)
{
worldPoly[i] = Vector3.Transform(localPoly[i], root.WorldTransform);
worldPoly[i].Z += PortalVisibilityBuilder.ShellDrawLiftZ; // drawn space
centroid += worldPoly[i];
}
centroid /= worldPoly.Length;
var plane = root.ClipPlanes[exitIdx];
var worldNormal = Vector3.TransformNormal(plane.Normal, root.WorldTransform);
var cellCenterWorld = Vector3.Transform(
(root.LocalBoundsMin + root.LocalBoundsMax) * 0.5f, root.WorldTransform);
if (Vector3.Dot(worldNormal, cellCenterWorld - centroid) < 0)
worldNormal = -worldNormal;
worldNormal = Vector3.Normalize(worldNormal);
// d=2.4 m, eye low (0.9 m above the opening's base), gaze at the
// centroid — the main sweep's clean case, where the aperture top edge
// projects ON SCREEN (y≈0.79; a closer/higher eye pushes the lintel
// past the screen top and the seam becomes unmeasurable).
var eye = centroid + worldNormal * 2.4f;
eye.Z = centroid.Z - 1.0f + 0.9f;
var viewProj = ViewProjFor(eye, centroid);
var clip = new Vector4[worldPoly.Length];
for (int i = 0; i < worldPoly.Length; i++)
clip[i] = Vector4.Transform(new Vector4(worldPoly[i], 1f), viewProj);
var aperture = new Vector2[clip.Length];
for (int i = 0; i < clip.Length; i++)
aperture[i] = new Vector2(clip[i].X / clip[i].W, clip[i].Y / clip[i].W);
var pvUnlifted = PortalVisibilityBuilder.Build(root, eye, Lookup, viewProj); // drawLiftZ 0
var asmUnlifted = ClipFrameAssembler.Assemble(ClipFrame.NoClip(), pvUnlifted);
Assert.True(asmUnlifted.OutsideViewSlices.Length > 0);
(float unliftedGapPx, _, _) = MeasureTopEdgeGap(aperture, asmUnlifted.OutsideViewSlices, 1920, 1080);
var pvLifted = PortalVisibilityBuilder.Build(root, eye, Lookup, viewProj,
buildingMembership: null, drawLiftZ: PortalVisibilityBuilder.ShellDrawLiftZ);
var asmLifted = ClipFrameAssembler.Assemble(ClipFrame.NoClip(), pvLifted);
Assert.True(asmLifted.OutsideViewSlices.Length > 0);
(float liftedGapPx, _, _) = MeasureTopEdgeGap(aperture, asmLifted.OutsideViewSlices, 1920, 1080);
_out.WriteLine(FormattableString.Invariant(
$"top-edge gap vs the DRAWN aperture at d=2.4 m: unliftedGate={unliftedGapPx:F2}px liftedGate={liftedGapPx:F2}px"));
var dbg = new System.Text.StringBuilder(" aperture(LIFTED):");
foreach (var v in aperture) dbg.Append(FormattableString.Invariant($" ({v.X:F4},{v.Y:F4})"));
_out.WriteLine(dbg.ToString());
foreach (var poly in pvUnlifted.OutsideView.Polygons)
{
var sb = new System.Text.StringBuilder(" unliftedGatePoly:");
foreach (var v in poly.Vertices) sb.Append(FormattableString.Invariant($" ({v.X:F4},{v.Y:F4})"));
_out.WriteLine(sb.ToString());
}
foreach (var poly in pvLifted.OutsideView.Polygons)
{
var sb = new System.Text.StringBuilder(" liftedGatePoly:");
foreach (var v in poly.Vertices) sb.Append(FormattableString.Invariant($" ({v.X:F4},{v.Y:F4})"));
_out.WriteLine(sb.ToString());
}
// The strip the user saw: physics-space gate vs drawn hole, several px.
Assert.True(unliftedGapPx > 2.0f, FormattableString.Invariant(
$"expected the unlifted gate to show the strip (>2px), got {unliftedGapPx:F2}px"));
// The fix: a gate in drawn space covers the drawn hole.
Assert.True(liftedGapPx <= 1.2f, FormattableString.Invariant(
$"lifted gate still under-covers by {liftedGapPx:F2}px"));
}
private static string DescribePolys(CellView view)
{
var parts = new List<string>();
foreach (var p in view.Polygons) parts.Add(p.Vertices.Length.ToString());
return $"[{string.Join(",", parts)}]";
}
/// <summary>
/// For sample x positions across the aperture's projected top edge, find the
/// aperture boundary's top y, then walk downward until the gate admits the
/// point. Returns the worst gaps in 1080p pixels (plane gate and modeled
/// scissor gate measured independently), and the x of the worst plane gap.
/// </summary>
private static (float planeGapPx, float scissorGapPx, float atX) MeasureTopEdgeGap(
Vector2[] aperture, ClipViewSlice[] slices, int fbW, int fbH,
ITestOutputHelper? debug = null)
{
const float Inset = 1e-4f; // dodge exact-boundary ambiguity
const float StepY = 0.0002f; // ~0.1 px at 1080p
const float CapY = 0.02f; // stop searching beyond ~10 px
float minX = float.MaxValue, maxX = float.MinValue;
foreach (var v in aperture) { minX = MathF.Min(minX, v.X); maxX = MathF.Max(maxX, v.X); }
float span = maxX - minX;
if (span <= 0.01f) return (0, 0, 0);
float worstPlane = 0, worstScissor = 0, atX = 0;
const int Samples = 160;
for (int s = 0; s <= Samples; s++)
{
float x = minX + span * (0.01f + 0.98f * s / Samples);
if (MathF.Abs(x) > 0.98f) continue; // off screen — no pixel exists there
float topY = TopBoundaryY(aperture, x);
if (float.IsNaN(topY) || MathF.Abs(topY) > 0.98f) continue; // off screen / no boundary
var p = new Vector2(x, topY - Inset);
float planeGap = GapBelow(p, q => AnySliceAdmitsPlanes(slices, q), StepY, CapY);
// The scissor question is "does the box cut pixels the PLANES would
// draw" — measure it from the planes-admitted top, not the aperture
// top (at slanted corners the aperture top can sit legitimately
// outside the gate polygon's column).
var pPlanes = new Vector2(p.X, p.Y - planeGap - Inset);
float scissorGap = GapBelow(pPlanes, q => AnySliceAdmitsScissor(slices, q, fbW, fbH), StepY, CapY);
if (debug is not null && scissorGap > 0.005f)
debug.WriteLine(FormattableString.Invariant(
$" sample x={x:F4} apTop={topY:F4} planeGap={planeGap * fbH / 2f:F2}px pPlanes=({pPlanes.X:F4},{pPlanes.Y:F4}) scissorGap={scissorGap * fbH / 2f:F2}px"));
if (planeGap > worstPlane) { worstPlane = planeGap; atX = x; }
worstScissor = MathF.Max(worstScissor, scissorGap);
}
// NDC y → pixels at the given framebuffer height.
return (worstPlane * fbH / 2f, worstScissor * fbH / 2f, atX);
}
private static float GapBelow(Vector2 start, Func<Vector2, bool> admitted, float step, float cap)
{
if (admitted(start)) return 0f;
for (float dy = step; dy <= cap; dy += step)
{
if (admitted(new Vector2(start.X, start.Y - dy)))
return dy;
}
return cap;
}
// Production semantics: each OutsideView polygon is one slice; the union of
// slices is drawn. A slice with planes gates per fragment via
// gl_ClipDistance (dot((nx,ny,0,d),(x,y,z,1)) >= 0 for an NDC point);
// a planeless slice (scissor fallback) admits its whole NDC AABB.
private static bool AnySliceAdmitsPlanes(ClipViewSlice[] slices, Vector2 p)
{
foreach (var slice in slices)
{
if (slice.Planes.Length == 0)
{
if (p.X >= slice.NdcAabb.X && p.Y >= slice.NdcAabb.Y
&& p.X <= slice.NdcAabb.Z && p.Y <= slice.NdcAabb.W)
return true;
continue;
}
bool inside = true;
foreach (var pl in slice.Planes)
{
if (pl.X * p.X + pl.Y * p.Y + pl.W < 0f) { inside = false; break; }
}
if (inside) return true;
}
return false;
}
// Production scissor (BeginDoorwayScissor → NdcScissorRect.ToPixels): a
// point is admitted when its pixel falls inside some slice's scissor box.
private static bool AnySliceAdmitsScissor(ClipViewSlice[] slices, Vector2 p, int fbW, int fbH)
{
int pixX = (int)MathF.Floor((p.X * 0.5f + 0.5f) * fbW);
int pixY = (int)MathF.Floor((p.Y * 0.5f + 0.5f) * fbH);
foreach (var slice in slices)
{
var box = NdcScissorRect.ToPixels(slice.NdcAabb, fbW, fbH);
if (pixX >= box.X && pixX < box.X + box.Width
&& pixY >= box.Y && pixY < box.Y + box.Height)
return true;
}
return false;
}
/// <summary>Highest boundary y of the polygon at vertical line x (NaN when
/// the line misses the polygon).</summary>
private static float TopBoundaryY(Vector2[] poly, float x)
{
float best = float.NaN;
for (int i = 0; i < poly.Length; i++)
{
var a = poly[i];
var b = poly[(i + 1) % poly.Length];
if (MathF.Abs(a.X - b.X) < 1e-9f)
{
if (MathF.Abs(a.X - x) < 1e-6f)
{
float hi = MathF.Max(a.Y, b.Y);
if (float.IsNaN(best) || hi > best) best = hi;
}
continue;
}
float t = (x - a.X) / (b.X - a.X);
if (t < 0f || t > 1f) continue;
float y = a.Y + t * (b.Y - a.Y);
if (float.IsNaN(best) || y > best) best = y;
}
return best;
}
}

View file

@ -0,0 +1,112 @@
using System;
using DatReaderWriter;
using DatReaderWriter.Options;
using Xunit;
using Xunit.Abstractions;
using DatSetup = DatReaderWriter.DBObjs.Setup;
using DatGfxObj = DatReaderWriter.DBObjs.GfxObj;
namespace AcDream.App.Tests.Rendering;
/// <summary>
/// #131 diagnostic (throwaway): identify the Holtburg portal among the
/// outside-stage setup ids captured by the [outstage] probe, by dumping each
/// candidate setup's parts + bounds from the dat. The portal's setup is the
/// translucent swirl; lamp posts / creatures / signs identify by part shape.
/// </summary>
public class Issue131SetupProbeTests
{
private readonly ITestOutputHelper _out;
public Issue131SetupProbeTests(ITestOutputHelper output) => _out = output;
/// <summary>#131: from the captured cottage-interior frame (the user's
/// portal-missing viewpoint), does the look-in flood admit the hall's
/// PORCH cell 0xA9B4017A (the portal's owner cell, pinned by the teleport
/// pCell flip)? If not admitted, no pass can draw the swirl regardless of
/// the emitter plumbing.</summary>
[Fact]
public void Diagnostic_LookInFlood_AdmitsHallPorchFromCottage()
{
var datDir = CornerFloodReplayTests.ResolveDatDir();
if (datDir is null) { _out.WriteLine("SKIP: dats unavailable"); return; }
using var dats = new DatCollection(datDir, DatAccessType.Read);
var cells = Issue120ReciprocalPingPongTests.LoadAllInteriorCells(dats, 0xA9B40000u);
_out.WriteLine(FormattableString.Invariant($"loaded {cells.Count} A9B4 interior cells; hasPorch017A={cells.ContainsKey(0xA9B4017Au)}"));
AcDream.App.Rendering.LoadedCell? Lookup(uint id) => cells.TryGetValue(id, out var c) ? c : null;
// The captured frame: [viewer] root=0xA9B40171 eye=(155.255,14.533,96.074)
// fwd=(0.0702,0.9554,-0.2869) (portal-owner-verdicts.log:135118).
var eye = new System.Numerics.Vector3(155.255f, 14.533f, 96.074f);
var fwd = new System.Numerics.Vector3(0.0702f, 0.9554f, -0.2869f);
var view = System.Numerics.Matrix4x4.CreateLookAt(eye, eye + fwd, System.Numerics.Vector3.UnitZ);
var proj = System.Numerics.Matrix4x4.CreatePerspectiveFieldOfView(1.2f, 1280f / 720f, 1f, 5000f);
var viewProj = view * proj;
var root = cells[0xA9B40171u];
var pv = AcDream.App.Rendering.PortalVisibilityBuilder.Build(
root, eye, Lookup, viewProj,
buildingMembership: null,
drawLiftZ: AcDream.App.Rendering.PortalVisibilityBuilder.ShellDrawLiftZ);
_out.WriteLine(FormattableString.Invariant(
$"main flood={pv.OrderedVisibleCells.Count} outPolys={pv.OutsideView.Polygons.Count}"));
var lookIn = AcDream.App.Rendering.PortalVisibilityBuilder.BuildFromExterior(
cells.Values, eye, Lookup, viewProj,
float.PositiveInfinity, pv.OutsideView.Polygons);
var sb = new System.Text.StringBuilder("look-in admitted:");
foreach (uint id in lookIn.OrderedVisibleCells)
sb.Append(FormattableString.Invariant($" 0x{id & 0xFFFFu:X4}"));
_out.WriteLine(sb.ToString());
_out.WriteLine(FormattableString.Invariant(
$"porch 0x017A admitted: {lookIn.OrderedVisibleCells.Contains(0xA9B4017Au)}"));
}
[Fact]
public void Diagnostic_DumpOutstageCandidateSetups()
{
var datDir = CornerFloodReplayTests.ResolveDatDir();
if (datDir is null) { _out.WriteLine("SKIP: dats unavailable"); return; }
using var dats = new DatCollection(datDir, DatAccessType.Read);
uint[] candidates =
{
0x020010AC, // 0x7A9B4050 PASS r=11.9 — portal candidate A
0x02000B8E, // 0x7A9B403B PASS r=11.6 — portal candidate B
0x020019FF, // many instances (lamp posts?)
0x02000290,
0x02000001, // baseline (human?)
0x02000E08,
};
foreach (uint setupId in candidates)
{
var setup = dats.Get<DatSetup>(setupId);
if (setup is null)
{
_out.WriteLine(FormattableString.Invariant($"setup 0x{setupId:X8}: NOT FOUND"));
continue;
}
_out.WriteLine(FormattableString.Invariant(
$"setup 0x{setupId:X8}: parts={setup.Parts.Count}"));
int shown = 0;
foreach (uint partId in setup.Parts)
{
if (shown++ >= 4) { _out.WriteLine(" ..."); break; }
var gfx = dats.Get<DatGfxObj>(partId);
if (gfx is null) { _out.WriteLine(FormattableString.Invariant($" part 0x{partId:X8}: not found")); continue; }
var sb = new System.Text.StringBuilder();
sb.Append(FormattableString.Invariant(
$" part 0x{partId:X8}: polys={gfx.Polygons.Count} verts={gfx.VertexArray.Vertices.Count} surfaces=["));
int sShown = 0;
foreach (uint surfId in gfx.Surfaces)
{
if (sShown++ >= 6) { sb.Append(" ..."); break; }
sb.Append(FormattableString.Invariant($" 0x{surfId:X8}"));
}
sb.Append(" ]");
_out.WriteLine(sb.ToString());
}
}
}
}

View file

@ -0,0 +1,198 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using AcDream.App.Rendering;
using DatReaderWriter;
using DatReaderWriter.Options;
using Xunit;
using Xunit.Abstractions;
using DatLandBlockInfo = DatReaderWriter.DBObjs.LandBlockInfo;
namespace AcDream.App.Tests.Rendering;
/// <summary>
/// #95 MEASUREMENT (2026-06-13): entering the 0x0007 dungeon (Town Network) explodes
/// WB-DIAG to ~9.1M instances/frame. Suspected cause: <see cref="PortalVisibilityBuilder.Build"/>
/// floods the dungeon's portal graph WITHOUT the retail grab_visible_cells stab_list bounding
/// (decomp:311878). A dungeon cell has <c>seen_outside==0</c>; retail's PVS for it is just the
/// cell's <c>stab_list</c> (<see cref="LoadedCell.VisibleCells"/>) — typically a small bounded
/// set. If our flood instead visits ~all cells of the landblock, that is the blowup.
///
/// This is a DIAGNOSTIC, not a fix: it loads the real 0x0007 interior cells, runs the real
/// production flood from representative dungeon-cell roots, and PRINTS the ground-truth numbers —
/// flood visited-cell-set size (<see cref="PortalVisibilityFrame.OrderedVisibleCells"/>) vs the
/// root's stab_list size (<see cref="LoadedCell.VisibleCells"/>), plus how many visited cells
/// cross landblocks. The single assertion just guarantees the test ran; the VALUE is the output.
/// </summary>
public class Issue95DungeonFloodDiagnosticTests
{
private const uint TownNetwork = 0x00070000u;
private readonly ITestOutputHelper _out;
public Issue95DungeonFloodDiagnosticTests(ITestOutputHelper output) => _out = output;
// Production-ish projection (mirrors the sibling harnesses): FovY ~1.2, 1280x720,
// near 0.1, far 5000. The flood's clip is near-independent, so exactness is not
// load-bearing for cell-count measurement.
private static Matrix4x4 ViewProjFor(Vector3 eye, Vector3 lookAt)
{
var view = Matrix4x4.CreateLookAt(eye, lookAt, Vector3.UnitZ);
var proj = Matrix4x4.CreatePerspectiveFieldOfView(1.2f, 1280f / 720f, 0.1f, 5000f);
return view * proj;
}
[Fact]
public void Measure_DungeonFlood_VisibleCellCount()
{
var datDir = CornerFloodReplayTests.ResolveDatDir();
if (datDir is null)
{
_out.WriteLine("SKIP: dat dir did not resolve (ACDREAM_DAT_DIR unset and "
+ "%USERPROFILE%\\Documents\\Asheron's Call absent). No numbers measured.");
// Diagnostic test: do not hard-fail when dats are absent (matches sibling harnesses).
return;
}
_out.WriteLine($"dat dir resolved: {datDir}");
using var dats = new DatCollection(datDir, DatAccessType.Read);
// 1) LandBlockInfo header — NumCells for 0x0007.
var lbi = dats.Get<DatLandBlockInfo>(TownNetwork | 0xFFFEu);
if (lbi is null)
{
_out.WriteLine($"SKIP: LandBlockInfo 0x{TownNetwork | 0xFFFEu:X8} not found in the dat "
+ "(0x0007 may not exist in this client_cell_1.dat).");
return;
}
_out.WriteLine($"=== 0x0007 (Town Network) LandBlockInfo ===");
_out.WriteLine($"NumCells (DatLandBlockInfo.NumCells) = {lbi.NumCells}");
// 2) Load ALL interior cells (sparse ids tolerated — see LoadAllInteriorCells).
var loaded = Issue120ReciprocalPingPongTests.LoadAllInteriorCells(dats, TownNetwork);
_out.WriteLine($"cells actually loaded = {loaded.Count}");
Assert.True(loaded.Count > 0, "no interior cells loaded for 0x0007 — cannot measure");
Func<uint, LoadedCell?> lookup = id => loaded.TryGetValue(id, out var c) ? c : null;
// 3) Per-cell stab_list (VisibleCells) distribution across ALL loaded cells.
// This is the bounded retail PVS size we expect the flood to roughly match.
var stabSizes = loaded.Values.Select(c => c.VisibleCells.Count).ToList();
int seenOutsideCount = loaded.Values.Count(c => c.SeenOutside);
int interiorCount = loaded.Count - seenOutsideCount;
_out.WriteLine("");
_out.WriteLine("=== stab_list (LoadedCell.VisibleCells) distribution over ALL loaded cells ===");
_out.WriteLine($"cells with SeenOutside==true (entrance/exterior-facing) = {seenOutsideCount}");
_out.WriteLine($"cells with SeenOutside==false (interior dungeon) = {interiorCount}");
if (stabSizes.Count > 0)
_out.WriteLine(FormattableString.Invariant(
$"VisibleCells.Count min={stabSizes.Min()} max={stabSizes.Max()} avg={stabSizes.Average():F1} sum={stabSizes.Sum()}"));
int emptyStab = stabSizes.Count(s => s == 0);
_out.WriteLine($"cells with EMPTY stab_list (no dat PVS) = {emptyStab}");
// 4) Pick representative DUNGEON roots: the first interior (SeenOutside==false) cells in
// ascending id order. If none exist, fall back to 0x00070100 and report that.
var interiorRoots = loaded
.Where(kv => !kv.Value.SeenOutside)
.OrderBy(kv => kv.Key)
.Select(kv => kv.Value)
.Take(5)
.ToList();
if (interiorRoots.Count == 0)
{
_out.WriteLine("");
_out.WriteLine("NOTE: NO cell has SeenOutside==false (all cells see the exterior). "
+ "Falling back to root 0x00070100 for the flood measurement.");
if (loaded.TryGetValue(TownNetwork | 0x0100u, out var fallback))
interiorRoots.Add(fallback);
else
{
_out.WriteLine("WARN: 0x00070100 not loaded either; using the lowest-id loaded cell.");
interiorRoots.Add(loaded.OrderBy(kv => kv.Key).First().Value);
}
}
_out.WriteLine("");
_out.WriteLine("=== PER-ROOT FLOOD MEASUREMENT (PortalVisibilityBuilder.Build) ===");
_out.WriteLine("property read for the visited-cell set: PortalVisibilityFrame.OrderedVisibleCells");
_out.WriteLine("root | seenOut | stab(VisibleCells) | flood(OrderedVisibleCells) | crossLB | dir");
var floodSizes = new List<int>();
foreach (var root in interiorRoots)
{
// Eye at the root cell's world origin, looking toward its first portal (or +X if none),
// so the flood actually fires through an opening. Sweep all 6 axis directions and KEEP
// the maximum visited-set — the blowup is a worst-case-over-orientation quantity.
var eye = root.WorldPosition;
int bestFlood = -1;
string bestDir = "?";
int bestCrossLb = -1;
List<uint>? bestVisited = null;
// Direction candidates: toward each portal's polygon centroid (the natural look-through),
// plus the 6 cardinal axes as a fallback sweep.
var lookTargets = new List<(Vector3 target, string label)>();
for (int pi = 0; pi < root.Portals.Count && pi < root.PortalPolygons.Count; pi++)
{
var poly = root.PortalPolygons[pi];
if (poly is { Length: >= 1 })
{
var cl = Vector3.Zero;
foreach (var v in poly) cl += v;
cl /= poly.Length;
lookTargets.Add((Vector3.Transform(cl, root.WorldTransform),
$"portal{pi}->0x{root.Portals[pi].OtherCellId:X4}"));
}
}
foreach (var (d, lbl) in new (Vector3, string)[]
{
(Vector3.UnitX, "+X"), (-Vector3.UnitX, "-X"),
(Vector3.UnitY, "+Y"), (-Vector3.UnitY, "-Y"),
(Vector3.UnitZ, "+Z"), (-Vector3.UnitZ, "-Z"),
})
lookTargets.Add((eye + d * 5f, lbl));
foreach (var (target, label) in lookTargets)
{
if (Vector3.DistanceSquared(target, eye) < 1e-6f) continue;
var frame = PortalVisibilityBuilder.Build(root, eye, lookup, ViewProjFor(eye, target));
int floodN = frame.OrderedVisibleCells.Count;
if (floodN > bestFlood)
{
bestFlood = floodN;
bestDir = label;
bestVisited = frame.OrderedVisibleCells;
bestCrossLb = frame.OrderedVisibleCells.Count(id => (id & 0xFFFF0000u) != TownNetwork);
}
}
floodSizes.Add(bestFlood);
_out.WriteLine(FormattableString.Invariant(
$"0x{root.CellId:X8} | {(root.SeenOutside ? "Y" : "N"),5} | {root.VisibleCells.Count,18} | {bestFlood,26} | {bestCrossLb,7} | {bestDir}"));
// For the FIRST root, also print the actual visited set + stab set for eyeballing.
if (ReferenceEquals(root, interiorRoots[0]) && bestVisited is not null)
{
_out.WriteLine(" first-root visited (OrderedVisibleCells, low ids): "
+ string.Join(" ", bestVisited.Select(id => $"{id & 0xFFFFu:X4}")));
_out.WriteLine(" first-root stab_list (VisibleCells, low ids): "
+ string.Join(" ", root.VisibleCells.Select(id => $"{id & 0xFFFFu:X4}")));
}
}
// 5) Aggregate flood-size stats across the sampled roots — the headline numbers.
_out.WriteLine("");
_out.WriteLine("=== AGGREGATE over sampled roots ===");
if (floodSizes.Count > 0)
_out.WriteLine(FormattableString.Invariant(
$"flood visited-set size (OrderedVisibleCells): min={floodSizes.Min()} max={floodSizes.Max()} avg={floodSizes.Average():F1} (NumCells={lbi.NumCells}, loaded={loaded.Count})"));
var sampledStab = interiorRoots.Select(r => r.VisibleCells.Count).ToList();
if (sampledStab.Count > 0)
_out.WriteLine(FormattableString.Invariant(
$"sampled roots' stab_list size (VisibleCells): min={sampledStab.Min()} max={sampledStab.Max()} avg={sampledStab.Average():F1}"));
_out.WriteLine("");
_out.WriteLine("INTERPRETATION: if flood max ~= loaded.Count (visits ~all cells) while stab "
+ "is small, that is the #95 blowup — the flood is unbounded by the retail stab_list PVS.");
}
}

View file

@ -0,0 +1,80 @@
using System;
using System.Numerics;
using AcDream.App.Rendering;
using Xunit;
namespace AcDream.App.Tests.Rendering;
/// <summary>
/// #130: the doorway-slice scissor must be a CONSERVATIVE outer bound of its
/// NDC AABB (AD-17: over-inclusion safe, under-inclusion is the bug class).
/// The old Floor(origin)+Ceiling(size) form put the far edge at
/// floor(min)+ceil(maxmin), up to one pixel short of the true max edge —
/// the doorway top-edge background strip.
/// </summary>
public class NdcScissorRectTests
{
/// <summary>Containment property: every pixel whose CENTER lies inside the
/// NDC box is inside the scissor box, across a dense grid of fractional
/// alignments at two framebuffer sizes.</summary>
[Theory]
[InlineData(1920, 1080)]
[InlineData(2560, 1440)]
public void EveryCenterInsidePixel_IsInsideTheBox(int fbW, int fbH)
{
for (int i = 0; i < 251; i++)
{
// Sweep fractional alignments of all four edges.
float f = i / 251f;
float minX = -0.83f + f * 0.0031f;
float minY = -0.71f + f * 0.0047f;
float maxX = 0.339f + f * 0.0043f;
float maxY = 0.7938f + f * 0.0029f;
var box = NdcScissorRect.ToPixels(new Vector4(minX, minY, maxX, maxY), fbW, fbH);
// Pixel-space extremes of center-inside pixels.
float x0 = (minX * 0.5f + 0.5f) * fbW, x1 = (maxX * 0.5f + 0.5f) * fbW;
float y0 = (minY * 0.5f + 0.5f) * fbH, y1 = (maxY * 0.5f + 0.5f) * fbH;
int loX = (int)MathF.Ceiling(x0 - 0.5f), hiX = (int)MathF.Floor(x1 - 0.5f);
int loY = (int)MathF.Ceiling(y0 - 0.5f), hiY = (int)MathF.Floor(y1 - 0.5f);
Assert.True(box.X <= loX, $"left cut: box.X={box.X} > loX={loX} (minX={minX})");
Assert.True(box.Y <= loY, $"bottom cut: box.Y={box.Y} > loY={loY} (minY={minY})");
Assert.True(box.X + box.Width > hiX, $"right cut: box ends {box.X + box.Width} <= hiX={hiX} (maxX={maxX})");
Assert.True(box.Y + box.Height > hiY, $"top cut: box ends {box.Y + box.Height} <= hiY={hiY} (maxY={maxY})");
// Over-inclusion stays bounded (≤1 px per edge).
Assert.True(box.X >= loX - 1 && box.Y >= loY - 1);
Assert.True(box.X + box.Width <= hiX + 2 && box.Y + box.Height <= hiY + 2);
}
}
[Fact]
public void CapturedRegression_TopEdgeRow968_At1080p()
{
// Issue130DoorwayStripTests live capture: aperture top y=0.7938 →
// pixel row 968 (center 968.5 < 968.65). The old formula ended the box
// at row 967 — the visible strip.
var box = NdcScissorRect.ToPixels(new Vector4(-0.339f, -0.743f, 0.339f, 0.7938f), 1920, 1080);
Assert.True(box.Y + box.Height > 968, $"top row 968 cut: box ends at {box.Y + box.Height}");
}
[Fact]
public void CapturedRegression_RightColumn1296_At1920()
{
// Issue130DoorwayStripTests live capture: gate right edge x=0.3507 →
// pixel column 1296 admitted by the plane gate; the old formula ended
// the box at column 1295.
var box = NdcScissorRect.ToPixels(new Vector4(-0.2845f, -1.0f, 0.3507f, 0.2630f), 1920, 1080);
Assert.True(box.X + box.Width > 1296, $"right column 1296 cut: box ends at {box.X + box.Width}");
}
[Fact]
public void DegenerateAndOffscreenBoxes_StayValid()
{
// Past-the-edge regions clamp to the screen and keep min 1 px size.
var box = NdcScissorRect.ToPixels(new Vector4(0.999f, 0.999f, 1.5f, 1.5f), 1920, 1080);
Assert.True(box.Width >= 1 && box.Height >= 1);
var inverted = NdcScissorRect.ToPixels(new Vector4(1f, 1f, -1f, -1f), 1920, 1080);
Assert.True(inverted.Width >= 1 && inverted.Height >= 1);
}
}

View file

@ -0,0 +1,82 @@
using System;
using System.Numerics;
using Xunit;
namespace AcDream.App.Tests.Rendering;
/// <summary>
/// #108-residual orientation pin: TerrainModernRenderer culls terrain back
/// faces with FrontFace(Ccw) — the GL port of retail's single-sided terrain
/// (ACRender::landPolysDraw 0x006b7040: a land triangle draws ONLY when the
/// camera is on the POSITIVE side of its plane via Plane::which_side2).
///
/// The FrontFace choice rests on one mapping fact: under the production
/// camera convention (Matrix4x4.CreateLookAt with up = world +Z, Numerics
/// CreatePerspectiveFieldOfView — RetailChaseCamera.cs:203 / :52), an
/// UP-FACING terrain triangle that LandblockMesh emits CCW in world XY
/// rasterizes
/// · CCW in NDC/window space when the eye is ABOVE its plane (kept), and
/// · CW when the eye is BELOW (culled — retail draws nothing there: from
/// a below-grade cellar eye the door aperture shows sky, never grass).
/// This test pins that mapping in pure CPU math so a projection-convention
/// change (handedness, Y-flip) can't silently invert the cull and either
/// resurrect the #108 grass window or cull terrain from above.
/// </summary>
public class TerrainCullOrientationTests
{
// An up-facing triangle, CCW in world XY viewed from above — the exact
// emission convention pinned by LandblockMeshTests (crossZ > 0).
private static readonly Vector3[] Triangle =
{
new(-1f, 10f, 94f),
new( 1f, 10f, 94f),
new( 1f, 12f, 94f),
};
private static float NdcSignedArea2(Vector3 eye, Vector3 forward)
{
// The production camera shape: look-at with world-Z up
// (RetailChaseCamera.cs:203), Numerics perspective with the retail
// znear 0.1 (RetailChaseCamera.cs:52).
var view = Matrix4x4.CreateLookAt(eye, eye + forward, Vector3.UnitZ);
var proj = Matrix4x4.CreatePerspectiveFieldOfView(MathF.PI / 3f, 16f / 9f, 0.1f, 5000f);
var viewProj = view * proj;
Span<Vector2> ndc = stackalloc Vector2[3];
for (int i = 0; i < 3; i++)
{
var c = Vector4.Transform(new Vector4(Triangle[i], 1f), viewProj);
Assert.True(c.W > 1e-3f, "test triangle must be in front of the eye");
ndc[i] = new Vector2(c.X / c.W, c.Y / c.W);
}
// Twice the signed area: > 0 = CCW in NDC (GL window space keeps the
// orientation — NDC y up maps to window y up, no flip).
return (ndc[1].X - ndc[0].X) * (ndc[2].Y - ndc[0].Y)
- (ndc[1].Y - ndc[0].Y) * (ndc[2].X - ndc[0].X);
}
[Fact]
public void EyeAboveTerrainPlane_WindsCcw_FrontFaceKept()
{
// Eye above grade looking forward-down at the triangle (the normal
// outdoor view). Retail: which_side2 = POSITIVE → drawn.
float area = NdcSignedArea2(new Vector3(0f, 5f, 96.5f), new Vector3(0f, 1f, -0.3f));
Assert.True(area > 0f,
$"above-plane eye must see the terrain triangle CCW (area2={area}) — " +
"FrontFace(Ccw)+Cull(Back) would otherwise cull terrain from above");
}
[Fact]
public void EyeBelowTerrainPlane_WindsCw_BackfaceCulled()
{
// Eye below grade (the cellar-stairwell window) looking up-forward at
// the underside. Retail: which_side2 = NEGATIVE → not drawn at all —
// the #108 grass that covered the exit door was exactly this
// underside rasterizing when culling was left disabled.
float area = NdcSignedArea2(new Vector3(0f, 5f, 92.5f), new Vector3(0f, 1f, 0.2f));
Assert.True(area < 0f,
$"below-plane eye must see the terrain triangle CW (area2={area}) — " +
"it must backface-cull like retail's which_side2 eye-side gate");
}
}

View file

@ -0,0 +1,147 @@
using System.Collections.Generic;
using System.Numerics;
using AcDream.App.World;
using Xunit;
namespace AcDream.App.Tests.World;
public class TeleportArrivalControllerTests
{
// Records each Place(destPos, destCell, forced) call.
private sealed record PlaceCall(Vector3 Pos, uint Cell, bool Forced);
private static TeleportArrivalController Make(
ArrivalReadiness verdict,
List<PlaceCall> placed,
int maxHoldFrames = TeleportArrivalController.DefaultMaxHoldFrames)
=> new(
readiness: (_, _) => verdict,
place: (pos, cell, forced) => placed.Add(new PlaceCall(pos, cell, forced)),
maxHoldFrames: maxHoldFrames);
[Fact]
public void BeginArrival_EntersHolding()
{
var placed = new List<PlaceCall>();
var c = Make(ArrivalReadiness.NotReady, placed);
c.BeginArrival(new Vector3(1, 2, 3), 0x01250126u);
Assert.Equal(TeleportArrivalPhase.Holding, c.Phase);
Assert.Empty(placed);
}
[Fact]
public void Tick_WhenIdle_IsNoOp()
{
var placed = new List<PlaceCall>();
var c = Make(ArrivalReadiness.Ready, placed);
c.Tick(); // never began
Assert.Equal(TeleportArrivalPhase.Idle, c.Phase);
Assert.Empty(placed);
}
[Fact]
public void Tick_NotReady_KeepsHolding_DoesNotPlace()
{
var placed = new List<PlaceCall>();
var c = Make(ArrivalReadiness.NotReady, placed);
c.BeginArrival(new Vector3(1, 2, 3), 0x01250126u);
c.Tick();
c.Tick();
Assert.Equal(TeleportArrivalPhase.Holding, c.Phase);
Assert.Empty(placed);
}
[Fact]
public void Tick_Ready_PlacesUnforced_AndIdles()
{
var placed = new List<PlaceCall>();
var c = Make(ArrivalReadiness.Ready, placed);
c.BeginArrival(new Vector3(30, -60, 6.005f), 0x01250126u);
c.Tick();
Assert.Equal(TeleportArrivalPhase.Idle, c.Phase);
var call = Assert.Single(placed);
Assert.False(call.Forced);
Assert.Equal(0x01250126u, call.Cell);
Assert.Equal(new Vector3(30, -60, 6.005f), call.Pos);
}
[Fact]
public void Tick_Impossible_PlacesForced_AndIdles()
{
var placed = new List<PlaceCall>();
var c = Make(ArrivalReadiness.Impossible, placed);
c.BeginArrival(new Vector3(1, 2, 3), 0x0125FF00u);
c.Tick();
Assert.Equal(TeleportArrivalPhase.Idle, c.Phase);
var call = Assert.Single(placed);
Assert.True(call.Forced);
}
[Fact]
public void Tick_Timeout_PlacesForced_AfterMaxHoldFrames()
{
var placed = new List<PlaceCall>();
var c = Make(ArrivalReadiness.NotReady, placed, maxHoldFrames: 3);
c.BeginArrival(new Vector3(1, 2, 3), 0x01250126u);
c.Tick(); // 1
c.Tick(); // 2
Assert.Empty(placed);
Assert.Equal(TeleportArrivalPhase.Holding, c.Phase);
c.Tick(); // 3 -> timeout
var call = Assert.Single(placed);
Assert.True(call.Forced);
Assert.Equal(TeleportArrivalPhase.Idle, c.Phase);
}
[Fact]
public void BeginArrival_AfterPlace_ReArms()
{
var placed = new List<PlaceCall>();
var c = Make(ArrivalReadiness.Ready, placed);
c.BeginArrival(new Vector3(1, 0, 0), 0x01250126u);
c.Tick(); // places #1, idle
c.BeginArrival(new Vector3(2, 0, 0), 0x01250127u);
c.Tick(); // places #2, idle
Assert.Equal(2, placed.Count);
Assert.Equal(0x01250127u, placed[1].Cell);
}
[Fact]
public void BeginArrival_DuringHold_ResetsTimeoutCounter()
{
var placed = new List<PlaceCall>();
var c = Make(ArrivalReadiness.NotReady, placed, maxHoldFrames: 3);
c.BeginArrival(new Vector3(1, 0, 0), 0x01250126u);
c.Tick(); // held=1
c.Tick(); // held=2 (one short of the timeout)
// Re-arm mid-hold with a fresh destination: the counter must restart.
c.BeginArrival(new Vector3(2, 0, 0), 0x01250199u);
c.Tick(); // held=1 again (NOT 3 -> no placement yet)
c.Tick(); // held=2
Assert.Empty(placed);
Assert.Equal(TeleportArrivalPhase.Holding, c.Phase);
c.Tick(); // held=3 -> timeout, forced place of the SECOND destination
var call = Assert.Single(placed);
Assert.True(call.Forced);
Assert.Equal(0x01250199u, call.Cell);
Assert.Equal(new Vector3(2, 0, 0), call.Pos);
}
}

View file

@ -0,0 +1,76 @@
using System.Linq;
using DatReaderWriter;
using DatReaderWriter.Options;
using DatLandBlock = DatReaderWriter.DBObjs.LandBlock;
using DatLandBlockInfo = DatReaderWriter.DBObjs.LandBlockInfo;
using DatEnvCell = DatReaderWriter.DBObjs.EnvCell;
using Xunit;
using Xunit.Abstractions;
namespace AcDream.Core.Tests.Conformance;
/// <summary>
/// G.3 dungeon-support research probe (2026-06-13): resolve the pivotal
/// terrain-less-vs-ocean ambiguity for the meeting-hall dungeon landblock
/// 0x0125 (the teleport this session went to cell 0x01250126). Does a dungeon
/// landblock have a LandBlock (0xXXYYFFFF) terrain record at all, or only
/// LandBlockInfo + EnvCells? Output-only — no assertions.
/// </summary>
public sealed class DungeonLandblockDatProbeTests
{
private readonly ITestOutputHelper _out;
public DungeonLandblockDatProbeTests(ITestOutputHelper output) => _out = output;
[Fact]
public void Probe_Dungeon0125_vs_Holtburg_A9B4()
{
var datDir = ConformanceDats.ResolveDatDir();
if (datDir is null) { _out.WriteLine("SKIP: dats unavailable"); return; }
using var dats = new DatCollection(datDir, DatAccessType.Read);
foreach (uint lb in new uint[] { 0x0125u, 0xA9B4u })
{
_out.WriteLine($"=== landblock 0x{lb:X4} ===");
uint terrainId = (lb << 16) | 0xFFFFu;
var block = dats.Get<DatLandBlock>(terrainId);
if (block is null)
{
_out.WriteLine($" LandBlock 0x{terrainId:X8}: NULL (no terrain record)");
}
else
{
var heights = block.Height;
bool allZero = heights is not null && heights.All(h => h == 0);
int distinct = heights is null ? 0 : heights.Distinct().Count();
_out.WriteLine($" LandBlock 0x{terrainId:X8}: present, Height[{heights?.Length ?? 0}] allZero={allZero} distinctIndices={distinct} first8=[{(heights is null ? "" : string.Join(",", heights.Take(8)))}]");
}
uint infoId = (lb << 16) | 0xFFFEu;
var info = dats.Get<DatLandBlockInfo>(infoId);
if (info is null)
{
_out.WriteLine($" LandBlockInfo 0x{infoId:X8}: NULL");
}
else
{
_out.WriteLine($" LandBlockInfo 0x{infoId:X8}: NumCells={info.NumCells} Buildings={info.Buildings?.Count ?? 0} Objects={info.Objects?.Count ?? 0}");
}
// probe the first few EnvCells
int found = 0;
for (uint low = 0x0100u; low < 0x0110u; low++)
{
uint cellId = (lb << 16) | low;
var cell = dats.Get<DatEnvCell>(cellId);
if (cell is not null)
{
found++;
if (found <= 3)
_out.WriteLine($" EnvCell 0x{cellId:X8}: present, CellStructure={cell.CellStructure} Portals={cell.CellPortals?.Count ?? 0} pos=({cell.Position.Origin.X:F1},{cell.Position.Origin.Y:F1},{cell.Position.Origin.Z:F1})");
}
}
_out.WriteLine($" EnvCells 0x0100..0x010F present: {found}");
}
}
}

View file

@ -0,0 +1,109 @@
using System;
using System.Numerics;
using AcDream.Core.Lighting;
using Xunit;
namespace AcDream.Core.Tests.Lighting;
/// <summary>
/// Conformance tests for the per-vertex static-light burn-in
/// (<see cref="LightBake"/>), ported from retail <c>calc_point_light</c>
/// (0x0059c8b0). Golden values are hand-derived from the decompiled equation:
/// wrap = (1/1.5)·(N·D + 0.5·dist); norm = distsq&gt;1 ? distsq·dist : dist;
/// scale = (1 dist/Range)·intensity·(wrap/norm); contrib = min(scale·color, color).
/// </summary>
public sealed class LightBakeTests
{
private static LightSource Torch(Vector3 pos, float intensity = 100f, float range = 10f)
=> new LightSource
{
Kind = LightKind.Point,
WorldPosition = pos,
ColorLinear = Vector3.One,
Intensity = intensity,
Range = range,
IsLit = true,
};
[Fact]
public void NearTorch_FacingIt_SaturatesToColor()
{
// Vertex at origin facing up (+Z); torch 2 m above.
// dist=2, distsq=4, wrap=(1/1.5)(2+1)=2, norm=4·2=8,
// scale=(1-0.2)·100·(2/8)=20 → min(20·1,1)=1 per channel.
var c = LightBake.PointContribution(
Vector3.Zero, new Vector3(0, 0, 1), Torch(new Vector3(0, 0, 2)));
Assert.Equal(1f, c.X, 4);
Assert.Equal(1f, c.Y, 4);
Assert.Equal(1f, c.Z, 4);
}
[Fact]
public void FarTorch_FallsOffSmoothly()
{
// Torch 8 m above (still within Range 10). scale=(1-0.8)·100·(8/512)=0.3125.
var c = LightBake.PointContribution(
Vector3.Zero, new Vector3(0, 0, 1), Torch(new Vector3(0, 0, 8)));
Assert.Equal(0.3125f, c.X, 4);
Assert.Equal(0.3125f, c.Y, 4);
Assert.Equal(0.3125f, c.Z, 4);
}
[Fact]
public void OutOfRange_ContributesNothing()
{
// Torch 11 m above, Range 10 → dist >= falloff_eff, skipped.
var c = LightBake.PointContribution(
Vector3.Zero, new Vector3(0, 0, 1), Torch(new Vector3(0, 0, 11)));
Assert.Equal(Vector3.Zero, c);
}
[Fact]
public void FacingAway_BeyondWrap_ContributesNothing()
{
// Normal points away (Z) from a torch above: N·D=2, wrap=(1/1.5)(2+1)<0.
var c = LightBake.PointContribution(
Vector3.Zero, new Vector3(0, 0, -1), Torch(new Vector3(0, 0, 2)));
Assert.Equal(Vector3.Zero, c);
}
[Fact]
public void HalfLambertWrap_LightsSurfaceAngledPast90Degrees()
{
// Normal at ~100° from the light direction still gets light (Lambert would not).
// Light straight above (+Z 2 m); normal tilted to (sin100°, 0, cos100°).
double t = 100.0 * Math.PI / 180.0;
var n = new Vector3((float)Math.Sin(t), 0, (float)Math.Cos(t)); // cos100° < 0
var c = LightBake.PointContribution(Vector3.Zero, n, Torch(new Vector3(0, 0, 2)));
Assert.True(c.X > 0f, "half-Lambert wrap should light a surface angled past 90°");
}
[Fact]
public void ComputeVertexColor_SumsLightsAndClampsToOne()
{
// Two saturating torches → sum clamps to 1, never overflows.
var lights = new[]
{
Torch(new Vector3(0, 0, 2)),
Torch(new Vector3(0, 0, 2)),
};
var c = LightBake.ComputeVertexColor(Vector3.Zero, new Vector3(0, 0, 1), lights);
Assert.Equal(1f, c.X, 4);
Assert.Equal(1f, c.Y, 4);
Assert.Equal(1f, c.Z, 4);
}
[Fact]
public void ComputeVertexColor_SkipsDirectionalAndUnlit()
{
var lights = new[]
{
new LightSource { Kind = LightKind.Directional, WorldPosition = new Vector3(0,0,2),
ColorLinear = Vector3.One, Intensity = 100f, Range = 10f, IsLit = true },
new LightSource { Kind = LightKind.Point, WorldPosition = new Vector3(0,0,2),
ColorLinear = Vector3.One, Intensity = 100f, Range = 10f, IsLit = false },
};
var c = LightBake.ComputeVertexColor(Vector3.Zero, new Vector3(0, 0, 1), lights);
Assert.Equal(Vector3.Zero, c);
}
}

View file

@ -60,21 +60,29 @@ public sealed class LightManagerTests
}
[Fact]
public void Tick_DropsLightsOutsideRangeWithSlack()
public void Tick_SelectsByDistance_RegardlessOfViewerRange()
{
// Retail D3D-style: candidacy is distance-only (the nearest 8). A torch
// lights its OWN surfaces — the shader applies the hard `d < range` cutoff
// PER FRAGMENT (mesh_modern.frag) — so a torch the VIEWER is standing
// outside the range of is still selected; it lights the wall it sits on.
// Replaces the old viewer-range candidacy filter that suppressed it, which
// left dungeon rooms (2227 registered torches) at activeLights≈1 / flat 0.2
// ambient — the "dungeon lighting off" report (#133 A7).
var mgr = new LightManager();
mgr.Register(MakePoint(new Vector3(20, 0, 0), range: 5f)); // far outside its own range
mgr.Register(MakePoint(new Vector3(20, 0, 0), range: 5f)); // viewer outside the torch's range
mgr.Tick(viewerWorldPos: Vector3.Zero);
Assert.Equal(0, mgr.ActiveCount);
Assert.Equal(1, mgr.ActiveCount); // selected by distance; the shader culls per-surface
}
[Fact]
public void Tick_IncludesLightsNearRangeEdge_WithSlack()
public void Tick_IncludesNearbyLight()
{
var mgr = new LightManager();
// Light at distance 5.0, range 5.0: distSq=25, rangeSq*1.1^2 = 25*1.21 = 30.25 → included.
// A nearby point light is selected (distance-only candidacy; the shader
// applies the per-fragment range cutoff).
mgr.Register(MakePoint(new Vector3(5, 0, 0), range: 5f));
mgr.Tick(viewerWorldPos: Vector3.Zero);

View file

@ -93,7 +93,7 @@ public sealed class LightInfoLoaderTests
var light = result[0];
Assert.Equal(LightKind.Point, light.Kind);
Assert.Equal(77u, light.OwnerId);
Assert.Equal(8f, light.Range);
Assert.Equal(10.4f, light.Range, 3); // Falloff 8 × static_light_factor 1.3 (calc_point_light 0x00820e24)
Assert.Equal(0.8f, light.Intensity);
Assert.Equal(new Vector3(101, 202, 303), light.WorldPosition);
Assert.InRange(light.ColorLinear.X, 0.99f, 1.01f);

View file

@ -179,4 +179,89 @@ public class GfxObjDegradeResolverTests
Assert.Equal(baseId, resolvedId);
Assert.Null(resolvedGfx);
}
// ── #136: editor-only placement marker detection ──────────────────────────
/// <summary>
/// The #136 dungeon "cone": its degrade table's slot 0 is visible ONLY at distance 0
/// (MaxDist=0) and the table degrades to GfxObj id 0 (= nothing) at real distance.
/// Retail's distance degrade never draws it in the live client; we must skip it.
/// </summary>
[Fact]
public void IsRuntimeHiddenMarker_EditorMarkerDegradingToNothing_True()
{
const uint markerGfx = 0x010028CAu;
const uint degradeId = 0x11000118u;
var gfx = new GfxObj { Flags = GfxObjFlags.HasDIDDegrade, DIDDegrade = degradeId };
var info = new GfxObjDegradeInfo
{
Degrades =
{
new GfxObjInfo { Id = markerGfx, MaxDist = 0f },
new GfxObjInfo { Id = 0u, MaxDist = float.MaxValue },
},
};
var gfxObjs = new Dictionary<uint, GfxObj> { [markerGfx] = gfx };
var infos = new Dictionary<uint, GfxObjDegradeInfo> { [degradeId] = info };
Assert.True(GfxObjDegradeResolver.IsRuntimeHiddenMarker(
id => gfxObjs.GetValueOrDefault(id), id => infos.GetValueOrDefault(id), markerGfx));
}
/// <summary>A real LOD object — slot 0 visible out to a real distance (MaxDist&gt;0) —
/// is NOT a marker, even though it degrades further.</summary>
[Fact]
public void IsRuntimeHiddenMarker_NormalLodObject_False()
{
const uint baseId = 0x01000055u;
const uint degradeId = 0x110006D0u;
var gfx = new GfxObj { Flags = GfxObjFlags.HasDIDDegrade, DIDDegrade = degradeId };
var info = new GfxObjDegradeInfo
{
Degrades =
{
new GfxObjInfo { Id = 0x01001795u, MaxDist = 25f },
new GfxObjInfo { Id = 0u, MaxDist = float.MaxValue },
},
};
var gfxObjs = new Dictionary<uint, GfxObj> { [baseId] = gfx };
var infos = new Dictionary<uint, GfxObjDegradeInfo> { [degradeId] = info };
Assert.False(GfxObjDegradeResolver.IsRuntimeHiddenMarker(
id => gfxObjs.GetValueOrDefault(id), id => infos.GetValueOrDefault(id), baseId));
}
/// <summary>No degrade table at all → not a marker.</summary>
[Fact]
public void IsRuntimeHiddenMarker_NoDegradeTable_False()
{
const uint baseId = 0x01001212u;
var gfx = new GfxObj { Flags = 0, DIDDegrade = 0 };
var gfxObjs = new Dictionary<uint, GfxObj> { [baseId] = gfx };
Assert.False(GfxObjDegradeResolver.IsRuntimeHiddenMarker(
id => gfxObjs.GetValueOrDefault(id), _ => null, baseId));
}
/// <summary>slot 0 is editor-only (MaxDist=0) but degrades to a REAL mesh (no id-0
/// entry) — a genuine close-only LOD, not an invisible marker. Do NOT skip.</summary>
[Fact]
public void IsRuntimeHiddenMarker_EditorSlotButDegradesToRealMesh_False()
{
const uint baseId = 0x01002000u;
const uint degradeId = 0x11002000u;
var gfx = new GfxObj { Flags = GfxObjFlags.HasDIDDegrade, DIDDegrade = degradeId };
var info = new GfxObjDegradeInfo
{
Degrades =
{
new GfxObjInfo { Id = baseId, MaxDist = 0f },
new GfxObjInfo { Id = 0x01002001u, MaxDist = float.MaxValue },
},
};
var gfxObjs = new Dictionary<uint, GfxObj> { [baseId] = gfx };
var infos = new Dictionary<uint, GfxObjDegradeInfo> { [degradeId] = info };
Assert.False(GfxObjDegradeResolver.IsRuntimeHiddenMarker(
id => gfxObjs.GetValueOrDefault(id), id => infos.GetValueOrDefault(id), baseId));
}
}

View file

@ -546,14 +546,17 @@ public class BSPStepUpTests
/// every frame replays the same hard stop and the character hangs in falling
/// animation until another correction breaks the loop.
/// </summary>
[Fact(Skip = "Issue #116 — slide-response divergence family (P1-era " +
"slide_sphere work made the first airborne wall frame slide in-frame " +
"to Z=1.92 instead of the L.2c-pinned hard stop at Z=2.0; the cached " +
"sliding-normal mechanism retail seeds via get_object_info " +
"(pc:279992, transient bit 4 → init_sliding_normal) only governs the " +
"NEXT frame, so which first-frame response is retail-faithful needs " +
"its own oracle read. NOT a cell-set problem — BR-7/A6.P4 left this " +
"byte-identical. See docs/ISSUES.md #116.")]
[Fact(Skip = "Issue #116 shape-2 — the engine slides IN-FRAME to Z=1.92 " +
"on the first airborne wall frame; this pin expects an L.2c hard stop " +
"at Z=2.0. Ghidra (2026-06-12) confirms retail CSphere::slide_sphere " +
"(0x00537440) applies the slide IN-FRAME (add_offset_to_check_pos → " +
"SLID_TS), so our 1.92 is faithful TO slide_sphere and the Z=2.0 " +
"expectation is the SUSPECT half — but whether retail's first " +
"airborne frame REACHES slide_sphere (→1.92) or hard-stops upstream " +
"(collide_with_environment dispatch / no last-known plane) needs a " +
"cdb trace of an airborne wall hit before flipping the assertion. The " +
"#116 threshold fix (EpsilonSq→F_EPSILON) did NOT change this — the D4 " +
"offset is a real slide, not degenerate. See docs/ISSUES.md #116.")]
public void D4_AirborneMover_TallWall_PersistsSlidingNormalAcrossFrames()
{
var (root, resolved) = BSPStepUpFixtures.TallWall();

View file

@ -0,0 +1,344 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using AcDream.Core.Physics;
using AcDream.Core.Tests.Conformance;
using DatReaderWriter;
using DatReaderWriter.Options;
using Xunit;
using Xunit.Abstractions;
namespace AcDream.Core.Tests.Physics;
/// <summary>
/// #108-residual vertical exit-walk harness (2026-06-12): the cellar-ascent
/// grass window. Climbing out of the Holtburg corner-building cellar
/// (0xA9B40174 room, floor z≈90 → 0x0175 staircase/lip → 0x0171 main floor at
/// z=94 = outdoor grade), the upstairs exit door is covered with grass until
/// the eye pops above grade. Punch/seal are exonerated (BR-2 experiment +
/// #117); the grass requires the frame to render through the OUTDOOR root —
/// i.e. the VIEWER-CELL resolution demotes to outdoor/null while the eye is
/// still below terrain grade inside the stairwell.
///
/// This harness drives the PRODUCTION viewer-resolution stack headlessly per
/// step of a kinematic ascent (the #118 HouseExitWalkReplayTests pattern,
/// turned vertical):
/// player cell — CellTransit.FindCellList on the foot-sphere center (the
/// production controller pick),
/// viewer cell — the PhysicsCameraCollisionProbe.SweepEye chain mirrored
/// verbatim (CameraCornerSealReplayTests provenance):
/// AdjustPosition at the head pivot → ResolveWithTransition
/// (IsViewer|PathClipped|FreeRotate|PerfectClip, 0.3 m
/// viewer_sphere) → fallback 1 AdjustPosition at the sought
/// eye → fallback 2 (player_pos, cell 0).
/// Each step records WHICH branch produced the viewer cell, so a demote
/// self-attributes:
/// A. sweep Ok=false → fallback chain (AdjustPosition's SeenOutside
/// fall-through is an XY-only grid snap — no Z test — so an in-dirt
/// below-grade eye can return an OUTDOOR cell with found=true);
/// B. sweep end-cell pick demotes (exterior-portal straddle + containment
/// miss at the stopped eye);
/// C. the start-cell AdjustPosition at the pivot demotes;
/// D. all healthy here → the bug is upstream (App camera damping /
/// GameWindow TryGetCell consumption).
///
/// Ascent path: fitted from the live captures (cellar-up-capture*.jsonl band
/// centroids, analyze_108_stairline.py): stairs at x≈153.9 ascending +Y,
/// z = 90.0 (y≤5.7) → 0.836·(y5.73)+90.25 (stairs) → lip 93.25→94 over
/// y 9.3→10.4 → main floor 94.0. The boom (retail defaults: distance 2.61,
/// pitch 0.291, pivot feet+1.5) trails SOUTH into the stairwell — mid-stairs
/// the desired eye sits beyond the cellar's south wall (y≈4.87) and above its
/// ceiling: in no-cell dirt below grade. Stub terrain (1000) — the membership
/// pick never reads terrain height (XY-column only), which is exactly the
/// mechanism under test.
///
/// ── RESULT (2026-06-12): the MEMBERSHIP/VIEWER LAYER IS EXONERATED ──────
/// 0 grass-window steps, 0 sweep failures, 0 fallback branches across boom
/// distance {2.61, 5.0} × damping lag {0, 0.3 m}. The viewer resolves
/// 0x0174 → 0x0175 (eye z 93.65, below grade) → 0x0171 at eye z 94.01 —
/// the viewer enters the main-floor room EXACTLY as the head pops above
/// grade (the stairwell portal sits at grade), matching the user's wording.
/// The handoff's "it is MEMBERSHIP/VIEWER-side" diagnosis is therefore
/// REFUTED for the current pipeline; #108-residual is RENDER-side: the
/// landscape slice clips terrain by 2D NDC planes only ((nx,ny,0,dw) —
/// ClipFrame.cs:178, terrain_modern.vert:173), so terrain BETWEEN the eye
/// and the exit portal (the grade sheet at z≈94, which from a below-grade
/// eye projects into the aperture band at y 9.817) paints the doorway.
/// These tests stay as the characterization pin for the healthy layer.
/// </summary>
public class Issue108CellarAscentViewerReplayTests
{
private readonly ITestOutputHelper _out;
public Issue108CellarAscentViewerReplayTests(ITestOutputHelper output) => _out = output;
private const float ViewerSphereRadius = 0.3f; // retail viewer_sphere (acclient :93314)
private const float PivotHeight = 1.5f; // RetailChaseCamera.PivotHeight
private const float FootRadius = 0.48f; // player foot sphere
private const float BoomDistance = 2.61f; // retail viewer_offset length
private const float BoomPitch = 0.291f; // retail default pitch (16.7°)
private const float GradeZ = 94.0f; // cottage floor == door sill ≈ outdoor terrain grade
private const uint Lb = 0xA9B40000u; // ConformanceDats.HoltburgLandblock
private const uint CellarRoom = Lb | 0x0174u; // floor z≈90.0
private const uint MainFloor = Lb | 0x0171u; // z=94.0
// ── fixture ─────────────────────────────────────────────────────────
private static (PhysicsEngine engine, PhysicsDataCache cache,
Dictionary<uint, AcDream.Core.World.Cells.EnvCell> envCells)
BuildEngine(DatCollection dats)
{
var cache = new PhysicsDataCache();
var engine = new PhysicsEngine { DataCache = cache };
var envCells = new Dictionary<uint, AcDream.Core.World.Cells.EnvCell>();
// Full A9B4 interior set (Issue112MembershipTests.LoadLandblockInteriors
// pattern) — the ascent's pick walk may reach cells outside the corner
// building's 0x016F-0x0175 range.
for (uint low = 0x0100u; low <= 0x01FFu; low++)
{
try { envCells[Lb | low] = ConformanceDats.LoadEnvCell(dats, cache, Lb | low); }
catch { }
}
// Buildings exactly as production registers them (Issue112MembershipTests.
// RegisterBuildings provenance): portals → BldPortalInfo with sign-extended
// OtherPortalId; landcell id from the building Frame.Origin (retail
// row-major grid).
var lbInfo = dats.Get<DatReaderWriter.DBObjs.LandBlockInfo>(Lb | 0xFFFEu);
Assert.NotNull(lbInfo);
foreach (var building in lbInfo!.Buildings)
{
if (building.Portals.Count == 0) continue;
var portals = new List<BldPortalInfo>(building.Portals.Count);
foreach (var bp in building.Portals)
portals.Add(new BldPortalInfo(
otherCellId: Lb | (uint)bp.OtherCellId,
otherPortalId: unchecked((short)bp.OtherPortalId),
flags: (ushort)bp.Flags));
var transform =
Matrix4x4.CreateFromQuaternion(building.Frame.Orientation) *
Matrix4x4.CreateTranslation(building.Frame.Origin);
int gridX = (int)(building.Frame.Origin.X / 24f);
int gridY = (int)(building.Frame.Origin.Y / 24f);
uint landcellLow = (uint)(gridX * 8 + gridY + 1);
cache.CacheBuilding(Lb | landcellLow, portals, transform);
}
var heights = new byte[81];
var heightTable = new float[256];
for (int i = 0; i < 256; i++) heightTable[i] = -1000f;
engine.AddLandblock(Lb, new TerrainSurface(heights, heightTable),
Array.Empty<CellSurface>(), Array.Empty<PortalPlane>(), 0f, 0f);
return (engine, cache, envCells);
}
// ── the probe mirror (PhysicsCameraCollisionProbe.SweepEye, verbatim) ──
private enum ViewerBranch { Sweep, AdjustFallback, NullFallback }
private sealed record ViewerResolve(
Vector3 Eye, uint ViewerCellId, ViewerBranch Branch,
uint StartCell, bool PivotAdjustFound, ResolveResult Sweep);
private static ViewerResolve ResolveViewer(
PhysicsEngine engine, Vector3 pivot, Vector3 desiredEye, uint cellId, Vector3 playerPos)
{
// update_viewer (pc:92775): no player cell → snap to player, viewer_cell null.
if (cellId == 0u)
return new ViewerResolve(playerPos, 0u, ViewerBranch.NullFallback, 0u, false, default);
uint startCell = cellId;
bool pivotFound = false;
if ((cellId & 0xFFFFu) >= 0x0100u)
{
var (pivotCell, found) = engine.AdjustPosition(cellId, pivot);
pivotFound = found;
if (found) startCell = pivotCell;
}
Vector3 begin = pivot - new Vector3(0f, 0f, ViewerSphereRadius);
Vector3 end = desiredEye - new Vector3(0f, 0f, ViewerSphereRadius);
var r = engine.ResolveWithTransition(
currentPos: begin,
targetPos: end,
cellId: startCell,
sphereRadius: ViewerSphereRadius,
sphereHeight: 0f,
stepUpHeight: 0f,
stepDownHeight: 0f,
isOnGround: false,
body: null,
moverFlags: ObjectInfoState.IsViewer | ObjectInfoState.PathClipped
| ObjectInfoState.FreeRotate | ObjectInfoState.PerfectClip,
movingEntityId: 0);
Vector3 eye = r.Position + new Vector3(0f, 0f, ViewerSphereRadius);
if (r.Ok)
return new ViewerResolve(eye, r.CellId, ViewerBranch.Sweep, startCell, pivotFound, r);
var (eyeCell, eyeFound) = engine.AdjustPosition(cellId, desiredEye);
if (eyeFound)
return new ViewerResolve(desiredEye, eyeCell, ViewerBranch.AdjustFallback, startCell, pivotFound, r);
return new ViewerResolve(playerPos, 0u, ViewerBranch.NullFallback, startCell, pivotFound, r);
}
// ── the ascent ──────────────────────────────────────────────────────
/// <summary>Stair-line feet Z for a path y (fitted from the capture bands).</summary>
private static float FeetZ(float y)
{
if (y < 5.73f) return 90.0f;
if (y < 9.30f) return MathF.Min(90.25f + 0.836f * (y - 5.73f), 93.25f);
if (y < 10.40f) return 93.25f + (y - 9.30f) * (0.75f / 1.10f);
return 94.0f;
}
private sealed record Step(
int Index, Vector3 Feet, uint PlayerCell,
ViewerResolve Viewer, uint EyeContainedIn, bool EyeBelowGrade)
{
public bool ViewerOutdoorOrNull =>
Viewer.ViewerCellId == 0u || (Viewer.ViewerCellId & 0xFFFFu) < 0x0100u;
}
private List<Step>? RunAscent(float boomDistance, float pathLagMeters)
{
var datDir = ConformanceDats.ResolveDatDir();
if (datDir is null) { _out.WriteLine("SKIP: dats unavailable"); return null; }
using var dats = new DatCollection(datDir, DatAccessType.Read);
var (engine, _, envCells) = BuildEngine(dats);
const float yStart = 5.2f, yEnd = 16.0f;
const float stepLen = 0.02f; // 2 cm/frame ≈ 1.2 m/s at 60 Hz
var fwd = new Vector3(0f, 1f, 0f); // facing up the stairs / at the exit door
float cosP = MathF.Cos(BoomPitch), sinP = MathF.Sin(BoomPitch);
// Stairs run at x≈153.9; past the lip the real walk line bends to the
// exit-door approach at x≈155 (corner-seal capture S1: player
// (154.93, 16.45)) — walking straight north at 153.9 ends in the wall
// beside the 0x0170 doorway, which a live player cannot do.
static float FeetX(float y) =>
y <= 10.4f ? 153.9f
: y >= 14.0f ? 155.0f
: 153.9f + (y - 10.4f) / (14.0f - 10.4f) * (155.0f - 153.9f);
var steps = new List<Step>();
uint playerCell = CellarRoom;
int count = (int)MathF.Round((yEnd - yStart) / stepLen);
for (int i = 0; i <= count; i++)
{
float y = yStart + i * stepLen;
var feet = new Vector3(FeetX(y), y, FeetZ(y));
// production controller pick: foot-sphere CENTER, seeded with the carried cell
playerCell = CellTransit.FindCellList(
engine.DataCache!, feet + new Vector3(0f, 0f, FootRadius), FootRadius, playerCell);
// boom target — optionally computed from a lagged path point to model the
// exponential damping trail (≈0.27 m at climb speed; 0 = converged target)
float yBoom = MathF.Max(yStart, y - pathLagMeters);
var boomFeet = new Vector3(FeetX(yBoom), yBoom, FeetZ(yBoom));
var pivot = feet + new Vector3(0f, 0f, PivotHeight);
var boomPivot = boomFeet + new Vector3(0f, 0f, PivotHeight);
var desiredEye = boomPivot - fwd * (boomDistance * cosP)
+ new Vector3(0f, 0f, boomDistance * sinP);
var viewer = ResolveViewer(engine, pivot, desiredEye, playerCell, feet);
uint containedIn = 0u;
foreach (var (id, env) in envCells)
if (env.PointInCell(viewer.Eye)) { containedIn = id; break; }
steps.Add(new Step(i, feet, playerCell, viewer,
containedIn, viewer.Eye.Z < GradeZ - 0.05f));
}
return steps;
}
private void DumpStep(Step s)
{
var v = s.Viewer;
string line = FormattableString.Invariant(
$"step={s.Index,3} feet=({s.Feet.X:F2},{s.Feet.Y:F2},{s.Feet.Z:F2}) pCell=0x{s.PlayerCell & 0xFFFFu:X4} start=0x{v.StartCell & 0xFFFFu:X4}{(v.PivotAdjustFound ? "" : "!")} branch={v.Branch} ok={v.Sweep.Ok} eye=({v.Eye.X:F2},{v.Eye.Y:F2},{v.Eye.Z:F2}) viewer=0x{v.ViewerCellId & 0xFFFFu:X4} eyeIn=0x{s.EyeContainedIn & 0xFFFFu:X4} belowGrade={(s.EyeBelowGrade ? "Y" : "n")}");
if (s.EyeBelowGrade && s.ViewerOutdoorOrNull) line += " << GRASS-WINDOW";
_out.WriteLine(line);
}
// ── diagnostics + pins ──────────────────────────────────────────────
/// <summary>
/// Full per-step table of the ascent at retail boom defaults (converged
/// boom, no lag). Read this first — the GRASS-WINDOW marks name the steps
/// where the production stack resolves an outdoor/null viewer with the eye
/// below grade, and the branch column attributes the demote site.
/// </summary>
[Fact]
public void Diagnostic_CellarAscent_PerStepTable()
{
var steps = RunAscent(BoomDistance, pathLagMeters: 0f);
if (steps is null) return;
uint lastPlayer = 0; uint lastViewer = 0xFFFFFFFFu; var lastBranch = (ViewerBranch)(-1);
int suspicious = 0;
foreach (var s in steps)
{
bool grass = s.EyeBelowGrade && s.ViewerOutdoorOrNull;
if (grass) suspicious++;
if (s.PlayerCell != lastPlayer || s.Viewer.ViewerCellId != lastViewer
|| s.Viewer.Branch != lastBranch || grass || s.Index % 50 == 0)
DumpStep(s);
lastPlayer = s.PlayerCell; lastViewer = s.Viewer.ViewerCellId; lastBranch = s.Viewer.Branch;
}
_out.WriteLine(FormattableString.Invariant(
$"--- {suspicious}/{steps.Count} steps in the grass window (viewer outdoor/null while eye below grade) ---"));
}
/// <summary>Boom-distance + damping-lag sweep: how wide is the window across poses?</summary>
[Fact]
public void Diagnostic_CellarAscent_PoseSweep()
{
foreach (float dist in new[] { 2.61f, 5.0f })
foreach (float lag in new[] { 0f, 0.30f })
{
var steps = RunAscent(dist, lag);
if (steps is null) return;
int grass = steps.FindAll(s => s.EyeBelowGrade && s.ViewerOutdoorOrNull).Count;
int okFalse = steps.FindAll(s => !s.Viewer.Sweep.Ok).Count;
int fb = steps.FindAll(s => s.Viewer.Branch != ViewerBranch.Sweep).Count;
_out.WriteLine(FormattableString.Invariant(
$"dist={dist:F2} lag={lag:F2}: grassWindow={grass}/{steps.Count} sweepOkFalse={okFalse} fallbackBranch={fb}"));
}
}
/// <summary>
/// THE PIN: while the eye is below terrain grade on the cellar ascent, the
/// viewer must resolve INTERIOR — an outdoor/null viewer cell roots the
/// frame at the landscape and sweeps grass across the exit door (#108).
/// Retail's viewer rides the stairwell cells here (the cellar camera works
/// in retail); below grade inside the building footprint there is no
/// legitimate outdoor viewer.
/// </summary>
[Fact]
public void CellarAscent_ViewerStaysInterior_WhileEyeBelowGrade()
{
var steps = RunAscent(BoomDistance, pathLagMeters: 0f);
if (steps is null) return;
var failures = steps.FindAll(s => s.EyeBelowGrade && s.ViewerOutdoorOrNull);
if (failures.Count > 0)
{
_out.WriteLine($"--- {failures.Count} grass-window steps ---");
foreach (var s in failures) DumpStep(s);
}
Assert.True(failures.Count == 0,
$"{failures.Count}/{steps.Count} ascent steps resolve an outdoor/null viewer cell while the eye " +
"is below grade — the #108 grass window (see output for the branch attribution)");
}
}

View file

@ -0,0 +1,142 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using AcDream.Core.Physics;
using DatReaderWriter.Enums;
using DatReaderWriter.Types;
using Xunit;
namespace AcDream.Core.Tests.Physics;
/// <summary>
/// #133 (Bug A) — the validated-claim placement branch of
/// <see cref="PhysicsEngine.Resolve"/> must return the VALIDATED claim's own
/// full cell id, NOT <c>lbPrefix | (cellId &amp; 0xFFFF)</c>.
///
/// <para>
/// <c>lbPrefix</c> is found by scanning resident landblocks for one whose
/// <c>[0,192)</c> local bounds contain the candidate XY. A dungeon EnvCell's
/// local Y can be NEGATIVE relative to its own landblock (the live capture:
/// server teleport to dungeon cell <c>0x00070143</c> at local <c>(70,-60,0.01)</c>).
/// The dungeon landblock fails the <c>localY &gt;= 0</c> bounds test, so the loop
/// instead matches a still-resident NEIGHBOURING block (a Holtburg landblock
/// whose world bounds happen to contain the same XY) and sets
/// <c>lbPrefix = 0xA9B30000</c>. The old code then returned
/// <c>0xA9B30000 | 0x0143 = 0xA9B30143</c>, re-stamping the validated dungeon
/// claim with the wrong landblock — the client mis-resolved the player into
/// Holtburg and spammed ACE with rejected moves
/// (<c>movement pre-validation failed from 00070143 to A9B30143</c>).
/// </para>
///
/// <para>
/// The validated claim's prefix is authoritative; a position falling in a
/// neighbouring resident landblock must not re-stamp it. This test reproduces
/// the exact geometry of the capture (dungeon claim in landblock <c>0x0007</c>,
/// candidate XY also inside resident Holtburg <c>0xA9B3</c>) and asserts the
/// returned cell keeps its <c>0x0007</c> prefix.
/// </para>
/// </summary>
public class Issue133DungeonTeleportPrefixTests
{
private const uint DungeonLandblock = 0x00070000u;
private const uint DungeonCellId = 0x00070143u; // indoor (low 0x0143 ≥ 0x0100)
private const uint HoltburgLandblock = 0xA9B30000u; // a neighbouring resident block
// The capture: dungeon cell 0x00070143 at dungeon-local (70, -60, 0.01).
// We place the Holtburg block at world origin so its [0,192) bounds contain
// the candidate XY, and the dungeon block at world Y-offset 130 so the SAME
// world XY lands at dungeon-local Y = 70 - 130 = -60 (the captured negative).
private static readonly Vector3 SpawnPos = new(70f, 70f, 0.01f);
[Fact]
public void ValidatedDungeonClaim_KeepsItsLandblockPrefix_NotTheNeighbour()
{
var engine = BuildEngine();
// Zero delta = the snap shape (teleport arrival). cellId is the dungeon
// claim; the candidate XY also falls inside the resident Holtburg block.
var result = engine.Resolve(SpawnPos, DungeonCellId, delta: Vector3.Zero, stepUpHeight: 0.5f);
Assert.True(result.IsOnGround);
// The validated claim's prefix is authoritative — high word stays 0x0007,
// NOT re-stamped to the neighbouring Holtburg 0xA9B3.
Assert.Equal(DungeonCellId, result.CellId);
Assert.Equal(DungeonLandblock, result.CellId & 0xFFFF0000u);
}
// ── fixture ──────────────────────────────────────────────────────────────
private static PhysicsEngine BuildEngine()
{
var cache = new PhysicsDataCache();
var engine = new PhysicsEngine { DataCache = cache };
// The dungeon cell: a Leaf CellBSP contains any point, so AdjustPosition
// validates the claim (returns it with found=true). Its Resolved set has
// one walkable floor polygon at z=0 under the spawn XY so the #111
// validated-claim branch grounds onto it.
cache.RegisterCellStructForTest(DungeonCellId, MakeDungeonCell());
// Resident Holtburg block at world origin: its [0,192) bounds CONTAIN the
// candidate XY (70,70). This is the block the lbPrefix loop wrongly matched.
engine.AddLandblock(
landblockId: HoltburgLandblock,
terrain: FlatTerrain(),
cells: Array.Empty<CellSurface>(),
portals: Array.Empty<PortalPlane>(),
worldOffsetX: 0f,
worldOffsetY: 0f);
// The dungeon's own landblock, offset so the candidate XY produces a
// NEGATIVE dungeon-local Y (70 - 130 = -60) → it FAILS the [0,192) bounds
// test, which is exactly why the old code fell through to the Holtburg
// prefix. Registered so the scenario is faithful (a resident dungeon block
// whose local bounds don't cover the EnvCell's negative-Y position).
engine.AddLandblock(
landblockId: DungeonLandblock,
terrain: FlatTerrain(),
cells: Array.Empty<CellSurface>(),
portals: Array.Empty<PortalPlane>(),
worldOffsetX: 0f,
worldOffsetY: 130f);
return engine;
}
/// <summary>Flat 81-vertex stub terrain (all zero heights).</summary>
private static TerrainSurface FlatTerrain() => new(new byte[81], new float[256]);
private static CellPhysics MakeDungeonCell()
{
// One floor polygon: a 200×200 square at z=0 centred so it covers the
// spawn XY. Normal (0,0,1) → normal.Z = 1 ≥ FloorZ (0.6642) → walkable.
// Identity transform: cell-local == world, so the plane d = 0 (z + d = 0).
var floor = new ResolvedPolygon
{
Vertices = new[]
{
new Vector3(-100f, -100f, 0f),
new Vector3( 200f, -100f, 0f),
new Vector3( 200f, 200f, 0f),
new Vector3(-100f, 200f, 0f),
},
Plane = new Plane(new Vector3(0f, 0f, 1f), 0f),
NumPoints = 4,
SidesType = CullMode.None,
};
return new CellPhysics
{
BSP = new PhysicsBSPTree { Root = new PhysicsBSPNode { Type = BSPNodeType.Leaf } },
WorldTransform = Matrix4x4.Identity,
InverseWorldTransform = Matrix4x4.Identity,
Resolved = new Dictionary<ushort, ResolvedPolygon> { [0] = floor },
// Leaf root → point_in_cell true for any point → AdjustPosition
// validates the claim (found=true, cell unchanged).
CellBSP = new CellBSPTree { Root = new CellBSPNode { Type = BSPNodeType.Leaf } },
Portals = Array.Empty<PortalInfo>(),
PortalPolygons = new Dictionary<ushort, ResolvedPolygon>(),
VisibleCellIds = new HashSet<uint>(),
};
}
}

View file

@ -845,15 +845,14 @@ public sealed class MotionInterpreterTests
[InlineData(MotionCommand.RunForward)]
public void GetMaxSpeed_IgnoresForwardCommand_AlwaysReturnsRunRate(uint command)
{
// GetMaxSpeed is the InterpolationManager.AdjustOffset catch-up speed — it deliberately
// returns RunAnimSpeed × run-rate REGARDLESS of the current ForwardCommand (see GetMaxSpeed's
// doc comment: the bare run rate × RunAnimSpeed, ACE MotionInterp.cs:670-678, retail-verified
// — the slow catch-up is intentional, it fixed the 1-Hz remote-blip). It does NOT branch
// per-command. These previously asserted a REMOVED command-branching design (WalkForward →
// WalkAnimSpeed, WalkBackward → ×0.65, Idle → 0); that contract no longer exists, so they are
// consolidated here to PIN the no-branch contract across commands (Phase W green-tests triage).
var interp = MakeInterp();
interp.MyRunRate = 1.75f;
// GetMaxSpeed is the InterpolationManager.AdjustOffset catch-up speed — it
// returns RunAnimSpeed × run-rate REGARDLESS of the current ForwardCommand
// (retail 0x00527cb0 never reads interpreted_state; UN-2 byte verification
// 2026-06-12, tools/verify_un2_fmul.py). These previously asserted a REMOVED
// command-branching design (WalkForward → WalkAnimSpeed, WalkBackward →
// ×0.65, Idle → 0); they PIN the no-branch contract across commands.
var weenie = new FakeWeenie { RunRate = 1.75f };
var interp = MakeInterp(weenie: weenie);
interp.InterpretedState.ForwardCommand = command;
float speed = interp.GetMaxSpeed();
@ -862,17 +861,33 @@ public sealed class MotionInterpreterTests
}
[Fact]
public void GetMaxSpeed_RunForward_NoWeenie_FallsBackToMyRunRate()
public void GetMaxSpeed_NoWeenie_ReturnsLiteralOneTimesRunAnimSpeed()
{
// WeenieObj is null (MakeInterp with no weenie argument); MyRunRate
// is set explicitly. GetMaxSpeed must use MyRunRate as the run-rate
// source when InqRunRate is unavailable.
// Retail 0x00527cb0 weenie_obj == null path: fld 1.0 (.rdata 0x007928B0),
// fmul 4.0 (.rdata 0x007C8918) — the LITERAL 1.0, NOT my_run_rate (UN-2
// byte verification 2026-06-12). MyRunRate is set to a different value to
// prove it is not consulted on this path.
var interp = MakeInterp();
interp.MyRunRate = 1.75f;
interp.InterpretedState.ForwardCommand = MotionCommand.RunForward;
float speed = interp.GetMaxSpeed();
Assert.Equal(MotionInterpreter.RunAnimSpeed * 1.0f, speed, precision: 4);
}
[Fact]
public void GetMaxSpeed_InqRunRateFails_FallsBackToMyRunRate()
{
// Retail 0x00527cb0 InqRunRate-failure path: fld [esi+0x7c] (my_run_rate),
// fmul 4.0. The InqRunRate out-value is discarded on failure.
var weenie = new FakeWeenie { RunRate = 9.9f, InqRunRateResult = false };
var interp = MakeInterp(weenie: weenie);
interp.MyRunRate = 1.75f;
interp.InterpretedState.ForwardCommand = MotionCommand.RunForward;
float speed = interp.GetMaxSpeed();
Assert.Equal(MotionInterpreter.RunAnimSpeed * 1.75f, speed, precision: 4);
}
}

View file

@ -0,0 +1,257 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AcDream.App.Streaming;
using AcDream.Core.World;
using Xunit;
namespace AcDream.Core.Tests.Streaming;
/// <summary>
/// The dungeon streaming gate (#133 FPS). AC dungeons have no adjacent
/// landblocks (ACE <c>LandblockManager.GetAdjacentIDs</c> returns empty for a
/// dungeon); they sit packed in the ocean grid, so the normal 25×25 window
/// pulls in ~129 unrelated neighbor dungeons + their emitters. When the player
/// is inside a sealed dungeon cell, <c>Tick(insideDungeon: true)</c> collapses
/// streaming to the single dungeon landblock and unloads the neighbors.
/// </summary>
public class StreamingControllerDungeonGateTests
{
private static uint Encode(int x, int y) => ((uint)x << 24) | ((uint)y << 16) | 0xFFFFu;
private static LoadedLandblock MakeLb(int x, int y) => new LoadedLandblock(
Encode(x, y),
Heightmap: null!,
Entities: Array.Empty<WorldEntity>());
private sealed record Harness(
StreamingController Ctrl,
List<(uint Id, LandblockStreamJobKind Kind)> Loads,
List<uint> Unloads,
Func<int> ClearCalls,
GpuWorldState State);
private static Harness Make()
{
var loads = new List<(uint, LandblockStreamJobKind)>();
var unloads = new List<uint>();
int clearCalls = 0;
var state = new GpuWorldState();
var ctrl = new StreamingController(
enqueueLoad: (id, kind) => loads.Add((id, kind)),
enqueueUnload: unloads.Add,
drainCompletions: _ => Array.Empty<LandblockStreamResult>(),
applyTerrain: (_, _) => { },
state: state,
nearRadius: 4,
farRadius: 12,
clearPendingLoads: () => clearCalls++);
return new Harness(ctrl, loads, unloads, () => clearCalls, state);
}
[Fact]
public void EntersDungeon_CancelsPending_UnloadsNeighbors_KeepsCenter()
{
var h = Make();
uint center = Encode(0, 7);
h.State.AddLandblock(MakeLb(0, 7)); // the dungeon landblock
h.State.AddLandblock(MakeLb(0, 8)); // a neighbor ocean dungeon
h.State.AddLandblock(MakeLb(1, 7)); // another neighbor
h.Ctrl.Tick(observerCx: 0, observerCy: 7, insideDungeon: true);
Assert.Equal(1, h.ClearCalls()); // in-flight window load cancelled
Assert.Contains(Encode(0, 8), h.Unloads); // neighbor unloaded
Assert.Contains(Encode(1, 7), h.Unloads); // neighbor unloaded
Assert.DoesNotContain(center, h.Unloads); // dungeon landblock kept
Assert.DoesNotContain(h.Loads, l => l.Id == center); // already loaded → no reload
}
[Fact]
public void EntersDungeon_CenterNotLoaded_EnqueuesCenterLoad()
{
var h = Make(); // empty state — the dungeon landblock isn't resident yet
h.Ctrl.Tick(observerCx: 0, observerCy: 7, insideDungeon: true);
Assert.Equal(1, h.ClearCalls());
Assert.Contains(h.Loads, l => l.Id == Encode(0, 7)
&& l.Kind == LandblockStreamJobKind.LoadNear);
}
[Fact]
public void StayingCollapsed_SweepsStragglerThatFinishedAfterTheEdge()
{
var h = Make();
h.State.AddLandblock(MakeLb(0, 7));
h.Ctrl.Tick(0, 7, insideDungeon: true); // collapse edge
h.Unloads.Clear();
// A Load the worker had already dequeued before ClearLoads now completes.
h.State.AddLandblock(MakeLb(0, 8));
h.Ctrl.Tick(0, 7, insideDungeon: true); // sweep
Assert.Contains(Encode(0, 8), h.Unloads);
Assert.DoesNotContain(Encode(0, 7), h.Unloads);
}
[Fact]
public void StayingCollapsed_DoesNotReClearOrReloadCenter()
{
var h = Make();
h.State.AddLandblock(MakeLb(0, 7));
h.Ctrl.Tick(0, 7, insideDungeon: true); // collapse (clear #1)
h.Loads.Clear();
h.Ctrl.Tick(0, 7, insideDungeon: true); // stay collapsed
Assert.Equal(1, h.ClearCalls()); // clear only fired on the edge
Assert.Empty(h.Loads); // no spurious center reloads
}
[Fact]
public void Collapsed_CurrCellFlickersToAdjacentOffByOne_DoesNotExpand()
{
// Regression: the live run broke because a dungeon cell's negative local-Y
// makes the position-derived observer landblock land one row off (0,7→0,6).
// When CurrCell flickers null mid-frame, GameWindow stops overriding to the
// cell landblock and passes that adjacent (0,6). The Chebyshev>1 guard must
// treat that as a flicker and HOLD — never expand (which would unload the
// real dungeon and re-stream the 25×25 neighbor window).
var h = Make();
h.State.AddLandblock(MakeLb(0, 7));
h.Ctrl.Tick(0, 7, insideDungeon: true); // collapse onto the dungeon (0,7)
h.Loads.Clear();
h.Unloads.Clear();
h.Ctrl.Tick(0, 6, insideDungeon: false); // flicker → adjacent off-by-one
Assert.Empty(h.Loads); // NO full-window reload
Assert.Empty(h.Unloads); // dungeon (0,7) preserved; nothing else resident
}
[Fact]
public void ExitsDungeon_RebuildsFullWindow_UnloadsStaleDungeonLandblock()
{
var h = Make();
h.State.AddLandblock(MakeLb(0, 7));
h.Ctrl.Tick(0, 7, insideDungeon: true); // collapse
h.Loads.Clear();
h.Unloads.Clear();
// Exit through a portal to an outdoor location far from the dungeon block.
h.Ctrl.Tick(observerCx: 100, observerCy: 100, insideDungeon: false);
Assert.Contains(h.Loads, l => l.Kind == LandblockStreamJobKind.LoadNear);
Assert.Contains(h.Loads, l => l.Kind == LandblockStreamJobKind.LoadFar);
Assert.Contains(Encode(0, 7), h.Unloads); // stale dungeon block, outside new window
}
[Fact]
public void PreCollapse_BeforeAnyTick_LoadsOnlyDungeon_NeverBootstrapsWindow()
{
// #135: at a dungeon login/teleport we pre-collapse the instant we recenter,
// BEFORE the first Tick. The full 25×25 neighbor window must NEVER be enqueued
// — only the single dungeon landblock loads.
var h = Make(); // empty state — nothing resident, _region is null
h.Ctrl.PreCollapseToDungeon(0, 7);
Assert.Single(h.Loads); // exactly one load
Assert.Equal(Encode(0, 7), h.Loads[0].Id); // the dungeon landblock
Assert.Equal(LandblockStreamJobKind.LoadNear, h.Loads[0].Kind);
Assert.DoesNotContain(h.Loads, l => l.Kind == LandblockStreamJobKind.LoadFar);
}
[Fact]
public void PreCollapse_AfterBootstrapTick_CancelsWindow_UnloadsResidentNeighbors_KeepsDungeon()
{
// The REAL runtime ordering at a dungeon login: the per-frame streaming Tick
// runs FIRST and bootstraps the full 25×25 window, THEN the spawn handler fires
// PreCollapseToDungeon. The pre-collapse must cancel the queued window loads
// (_clearPendingLoads) and unload any neighbor that already finished streaming.
var h = Make();
h.Ctrl.Tick(0, 7, insideDungeon: false); // frame 1: NormalTick bootstraps the window
Assert.True(h.Loads.Count > 1); // the full window was enqueued
// Simulate neighbor landblocks that finished loading during the bootstrap,
// before the collapse edge.
h.State.AddLandblock(MakeLb(0, 7)); // the dungeon landblock itself
h.State.AddLandblock(MakeLb(0, 8)); // a neighbor ocean dungeon that loaded
h.State.AddLandblock(MakeLb(1, 7)); // another neighbor
h.Loads.Clear();
h.Unloads.Clear();
h.Ctrl.PreCollapseToDungeon(0, 7);
Assert.Equal(1, h.ClearCalls()); // queued window loads cancelled
Assert.Contains(Encode(0, 8), h.Unloads); // resident neighbor unloaded
Assert.Contains(Encode(1, 7), h.Unloads);
Assert.DoesNotContain(Encode(0, 7), h.Unloads); // dungeon landblock kept
}
[Fact]
public void PreCollapse_ThenHoldTicksWithStaleObserver_StaysCollapsed()
{
// After pre-collapse the player is held (CurrCell still null → insideDungeon
// false) while the dungeon hydrates. A stale observer that is the SAME dungeon
// landblock must keep streaming collapsed — no full-window reload.
var h = Make();
h.Ctrl.PreCollapseToDungeon(0, 7);
h.Loads.Clear();
h.Unloads.Clear();
h.Ctrl.Tick(0, 7, insideDungeon: false); // hold frame: not placed yet
Assert.Empty(h.Loads); // no neighbor window
Assert.Empty(h.Unloads);
}
[Fact]
public void PreCollapse_IsIdempotent_OnSameLandblock()
{
// A re-sent player spawn / a same-frame double call must not re-clear or
// re-enqueue.
var h = Make();
h.Ctrl.PreCollapseToDungeon(0, 7);
h.Loads.Clear();
h.Ctrl.PreCollapseToDungeon(0, 7);
Assert.Equal(1, h.ClearCalls()); // clear fired only on the first collapse
Assert.Empty(h.Loads); // no second dungeon load
}
[Fact]
public void PreCollapse_ThenPlaced_InsideDungeonTick_StaysCollapsed()
{
// When placement finally fires, the per-frame Tick(insideDungeon: true) sees
// the same collapsed landblock and holds — no re-collapse churn.
var h = Make();
h.State.AddLandblock(MakeLb(0, 7)); // dungeon landblock finished loading
h.Ctrl.PreCollapseToDungeon(0, 7);
h.Loads.Clear();
h.Unloads.Clear();
h.Ctrl.Tick(0, 7, insideDungeon: true); // placed: gate now fires
Assert.Equal(1, h.ClearCalls()); // no second clear
Assert.Empty(h.Loads);
Assert.DoesNotContain(Encode(0, 7), h.Unloads);
}
[Fact]
public void NormalOutdoorTick_Unchanged_NoCollapseNoClear()
{
var h = Make();
h.Ctrl.Tick(observerCx: 100, observerCy: 100); // default insideDungeon: false
Assert.Equal(0, h.ClearCalls());
Assert.Empty(h.Unloads);
// 9 near (9×9? no — nearRadius 4 → 9×9=81) + far ring loads enqueued.
Assert.Contains(h.Loads, l => l.Kind == LandblockStreamJobKind.LoadNear);
Assert.Contains(h.Loads, l => l.Kind == LandblockStreamJobKind.LoadFar);
}
}

View file

@ -169,6 +169,41 @@ public class LandblockMeshTests
Assert.True(cache.Count >= 2, $"Expected mix of palette codes, got {cache.Count}");
}
[Fact]
public void Build_AllTriangles_WindCounterClockwiseInWorldXY()
{
// #108-residual winding pin: TerrainModernRenderer enables backface
// culling with FrontFace(Ccw) — the GL port of retail's single-sided
// terrain (ACRender::landPolysDraw 0x006b7040 draws a land triangle
// only when the eye is on the POSITIVE side of its plane). That cull
// is only correct if EVERY emitted triangle winds the same way:
// counter-clockwise in world XY viewed from above (+Z toward the
// viewer), i.e. cross2D(v1-v0, v2-v0) > 0. Varied heights + several
// landblock coords exercise both FSplitNESW split directions across
// the 64 cells. A future emission-order change that flips any
// triangle would silently punch terrain holes under culling.
var block = BuildFlatLandBlock();
for (int i = 0; i < 81; i++)
block.Height[i] = (byte)((i * 37) % 64); // varied, deterministic slopes
foreach (var (lbx, lby) in new[] { (0u, 0u), (0xA9u, 0xB4u), (3u, 7u) })
{
var cache = new Dictionary<uint, SurfaceInfo>();
var mesh = LandblockMesh.Build(block, lbx, lby, IdentityHeightTable, MakeContext(), cache);
for (int t = 0; t < mesh.Indices.Length; t += 3)
{
var p0 = mesh.Vertices[mesh.Indices[t + 0]].Position;
var p1 = mesh.Vertices[mesh.Indices[t + 1]].Position;
var p2 = mesh.Vertices[mesh.Indices[t + 2]].Position;
float crossZ = (p1.X - p0.X) * (p2.Y - p0.Y) - (p1.Y - p0.Y) * (p2.X - p0.X);
Assert.True(crossZ > 0f,
$"lb=({lbx},{lby}) triangle {t / 3} winds CW in world XY (crossZ={crossZ}) — " +
"backface culling in TerrainModernRenderer would cull its TOP side");
}
}
}
[Fact]
public void Build_HeightmapPackedAsXMajor_NotYMajor()
{