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,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()
{