Campaign OVERHAUL S3 chunk 1 fix round 1 (docs/research/2026-09-01-overhaul/
s3-walk-ownership-map.md §11.5), applied on top of 079483bb6 after the
three-lens review. G1-G10, each verified against source and (where the item
adds a pin) mutation-checked.
G1 — the flag-off integration test was vacuous: Collect_TranscriptFlagOff_
ProducesNoConsoleOutput drove ONE frame, and WalkOracleTrace.Parse
structurally discards the last (open) frame, so it passed whether or not
the flag gate worked. Now drives TWO frames and asserts BOTH that Parse
sees zero frames AND that no captured line starts with any of the ten
transcript-kind prefixes (F /P /LS/LC /SC /BLD /DI /DC /EC /OC ) — robust
to unrelated console noise from parallel test classes.
G2 — LC/SC computed LodCellId before testing the flag (~1,300 land-cell
turns/frame of division/modulo/checked-conversion work paid even when the
flag is off). Both OnLandCellTurn and the new OnSortCellTurn now guard the
LodCellId call itself, not just WalkTranscriptDump's internal check.
G3 — the flag-on round trip never exercised LC/SC/BLD (the T2 fixture's
stub 1x1 landscape has zero blocks). New
OutdoorRoot_TranscriptFlagOn_PrintsLandscapeThenLandCellThenSortCellThen
Building_InOrder drives RetailFrameWalk.DrawLandscape directly (the same
deterministic "CY-only" admission technique the existing outdoor LandCell
test uses) over a landscape with one block + one building, and asserts
LS < LC < SC < BLD by position, with the exact cell/building ids.
G4 — the parts/alpha-depth smoke tests asserted FL/PC (Flushes/
DrawCellsSamples) but never AM/PM (MeshAdds/PortalPolyDraws) for the
alpha-depth log, even though PD/DM were already asserted for the parts
log. Added the missing Assert.Contains for MeshAdds and PortalPolyDraws.
G5 — launch-options.md's side-effects column now states the flag off/on
costs explicitly ("one bool read per print site, nothing else" / "one
console line per walk turn, ≈600-800 lines per outdoor frame").
G6 — removed the "DI f4180108... no, a9b4013f" self-correction in
Oh_doorway_still_first_frame_diff's comment; added a one-line note on why
it and Oh_foundry_deep_... stay bespoke Facts rather than folding into the
Theory (no functional reason, just history).
G7 — SC printed from OnLandscapeCellTurn, which RetailFrameWalk.
DrawLandscape calls AFTER DrawBuilding — retail's own order is
LC -> SC -> BLD (terrace-edge.walk.log:2331-2333), not LC -> BLD -> SC.
Fixed by adding IWalkEventSink.OnSortCellTurn, fired at the DrawSortCell
entry point (same alwaysDrawObjects||cellInView gate, before DrawBuilding);
the driver moves its SC print there and OnLandscapeCellTurn no longer
prints one.
G8 — the trailing "OC <viewer land cell>" retail prints once per complete
outdoor frame is GameSky::Draw's weather branch calling
DrawObjCellForDummies(after_sky_cell) @0x005070da, not a walk turn.
RetailPViewPassExecutor.DrawLandscapeSliceLate now prints it at the point
it actually calls RenderWeather; PrintObjectCellTurn's doc, which denied
this line existed, is corrected. New
Oh_terrace_edge_outdoor_frame_ends_with_the_weather_ObjectCellTurn pins
this directly against the OH capture: every complete outdoor frame's last
event is an OC whose cell id equals that same frame's own P-line cell id.
G9 — "EC and OC counts are always exactly equal per pose" was false
(terrace-edge: 12 EC vs 16 OC, the extra 4 being G8's weather OC, which
has no EC counterpart). The visit-scoped/stamp-dedupe conclusion was
right; only the count claim needed fixing. Both doc sites corrected.
G10 — the conformance signature dropped LC/SC/EC/OC entirely, which is
how G7 shipped green: no test ever compared their placement. Recorder now
tracks LC/SC via the new OnLandCellTurn/OnSortCellTurn hooks and derives
EC/OC per DC from PView::DrawCells's own two reverse loops (shell
far-to-near, then object-list far-to-near) — a look-in flood's DC derives
them immediately, the interior root's OWN flood defers to
OnInteriorFloodDrawTurn (verified against cathedral-leak.walk.log:4,1328,
where the root DC's own EC/OC print last). WalkTraceReplayContext.
Signature8 applies the identical derivation to oracle frames and never
produces the trailing weather OC (G8's own dedicated pin covers that
separately, matching the spec's "exclude from the comparison, assert
separately" instruction). Used ONLY for the four OH-capture-rooted
conformance rows (the older FW0 fixtures predate the LC/SC/EC/OC
breakpoints and carry none of those lines).
Applying the eight-kind check surfaced a real, separate bug: WalkLandscape
DatBuilder never set WalkLandBlock.LandblockId, so every LC/SC token any
DAT-built world ever produced read landblockId=0 — latent because the old
four-kind signature filtered LC/SC out. Fixed (one field, test-fixture-
only, no production behavior change). With that fixed, three of the four
OH rows (terrace-edge, cathedral-arrival, foundry-deep) reproduce EXACTLY
at the eight-kind level. Oh_doorway_still_first_frame_diff still diverges
by one extra LC/SC pair (token index 165: replay draws LC:a9c90001|
SC:a9c90001 that retail never does) — a real WalkLandscape.CheckBlocks
visibility question at a ring-2/ring-3 LOD boundary, out of this round's
print-only scope, needing the retail oracle to resolve. Marked
[Trait("Status","KnownFailure")] with the exact position recorded in its
own doc comment, matching TowerAscent's existing pattern — the InstalledDat
lane now shows FOUR known failures (the pre-existing two #383 layout tests
+ TowerAscent, plus this new finding), not three; never weakened the pin
itself.
Mutation checks (all four restored after observing the failure):
- G1: forcing WalkTranscriptDump.Enabled true made
Collect_TranscriptFlagOff_ProducesNoConsoleOutput fail with
"Assert.DoesNotContain() Failure: Filter matched in collection ... Collection:
[\"DI 00000100\", \"DC pv=00000000 ov=1 n=2: 00000100 00000101\", \"LS\", ...]".
- G3: deleting PrintLandCell's Console.WriteLine made the new outdoor test
fail with "expected an LC line".
- G4: making MeshAddPattern unmatchable made all five
Alpha_depth_log_parses_with_nonempty_samples rows fail with
"Assert.Contains() Failure: Filter not matched in collection".
- G7/G10: moving OnSortCellTurn back after DrawBuilding made the
terrace-edge conformance row fail with "walk diverged from retail
(terrace-edge.walk)", first divergence exactly at its first building
(f518002e): expected ...SC:f518002e|BLD:f518002e..., actual
...BLD:f518002e|SC:f518002e....
Gates: hermetic lane 6,816/0 (baseline 6,814 + G3's new test + G8's new
test); InstalledDat lane 244/4 known failures (two pre-existing #383
layout tests + TowerAscent + the new Oh_doorway_still_first_frame_diff
finding, documented above).
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
276 lines
13 KiB
C#
276 lines
13 KiB
C#
using System.Numerics;
|
||
using AcDream.App.Rendering.Walk;
|
||
|
||
namespace AcDream.App.Tests.Rendering.Walk;
|
||
|
||
/// <summary>
|
||
/// The FW1 conformance replay harness: reconstructs the camera state from a
|
||
/// pose-stamped oracle frame and drives the ported walk over
|
||
/// adapter-built world data. Convention notes (adjudicate against the
|
||
/// fixtures, loudly, on any mismatch):
|
||
/// <list type="bullet">
|
||
/// <item>The dumped quaternion is retail Frame storage order w,x,y,z
|
||
/// (q0=w) — unit-norm verified on the captures.</item>
|
||
/// <item>Retail's frame axes: +Y forward, +Z up (the camera looks along
|
||
/// the rotated +Y).</item>
|
||
/// <item>Pose origin is landblock-local, the same space the adapter's
|
||
/// cell transforms produce.</item>
|
||
/// </list>
|
||
/// </summary>
|
||
public sealed class WalkTraceReplayContext : IWalkFrameContext, IRetailFrameWalkContext
|
||
{
|
||
// Retail projection globals, dumped live from the capture client
|
||
// (recon 2026-08-30 evening: Render::bw/bh/xinvscale/yinvscale/tx/ty/vdst).
|
||
public const float RetailViewportWidth = 1024f;
|
||
public const float RetailViewportHeight = 720f;
|
||
public const float RetailXInvScale = 0.00025f;
|
||
public const float RetailYInvScale = 0.00025f;
|
||
public const float RetailTx = 0.127875f;
|
||
public const float RetailTy = 0.089875f;
|
||
public const float RetailVdst = 0.1330766976f;
|
||
|
||
private sealed class RetailRayCaster(
|
||
Vector3 right, Vector3 forward, Vector3 up) : IWalkRayCaster
|
||
{
|
||
// Retail's unproject (copy_view's ray path; equal to
|
||
// ScreenToViewTransform for these globals):
|
||
// u = sx·xinvscale − tx; w = sy·yinvscale − ty
|
||
// ray = Xaxis·u + Yaxis·vdst − Zaxis·w
|
||
public Vector3 RayThrough(float screenX, float screenY)
|
||
{
|
||
float u = screenX * RetailXInvScale - RetailTx;
|
||
float w = screenY * RetailYInvScale - RetailTy;
|
||
return right * u + forward * RetailVdst - up * w;
|
||
}
|
||
}
|
||
|
||
private readonly Dictionary<uint, WalkCell> _cells;
|
||
private readonly Matrix4x4 _viewProjection;
|
||
private readonly IWalkRayCaster _rays;
|
||
|
||
public WalkTraceReplayContext(WalkOraclePose pose, Dictionary<uint, WalkCell> cells)
|
||
{
|
||
_cells = cells;
|
||
WorldViewpoint = pose.Origin;
|
||
var rotation = new Quaternion(pose.Q1, pose.Q2, pose.Q3, pose.Q0);
|
||
// Basis convention RE-pinned 2026-08-30 (second pass): storage
|
||
// w,x,y,z; forward = rotated +Y — the retail Frame convention. The
|
||
// motion sweep briefly favored +X, but the walkabout camera was
|
||
// mouse-turned off the run direction; the terrace-edge fixture's
|
||
// EXTERNAL ground truth (the ledge faces the F518 vista, east)
|
||
// decodes east ONLY under +Y-forward, and +Y makes the street
|
||
// fixture's punch on/off-screen pattern match retail 4-for-4.
|
||
Vector3 forward = Vector3.Transform(Vector3.UnitY, rotation);
|
||
Vector3 up = Vector3.Transform(Vector3.UnitZ, rotation);
|
||
Vector3 right = Vector3.Transform(Vector3.UnitX, rotation);
|
||
|
||
// The exact retail frustum: tan(halfFovY) = ty/vdst, aspect = tx/ty.
|
||
float fovY = 2f * MathF.Atan(RetailTy / RetailVdst);
|
||
Matrix4x4 view = Matrix4x4.CreateLookAt(pose.Origin, pose.Origin + forward, up);
|
||
Matrix4x4 projection = Matrix4x4.CreatePerspectiveFieldOfView(
|
||
fovY, RetailTx / RetailTy, 0.1f, 5000f);
|
||
_viewProjection = view * projection;
|
||
|
||
ViewportWidth = RetailViewportWidth;
|
||
ViewportHeight = RetailViewportHeight;
|
||
_rays = new RetailRayCaster(right, forward, up);
|
||
|
||
// The retail CY near plane: N = forward, d = −dot(eye, forward) − znear.
|
||
CyPlane = new WalkPlane(forward, -Vector3.Dot(pose.Origin, forward) - 0.1f);
|
||
}
|
||
|
||
/// <summary>Building placements (camera-block-local) for the landscape
|
||
/// fixtures; empty for the interior-only ones.</summary>
|
||
public Dictionary<WalkBuilding, WalkWorldDatAdapter.BuildingEntry> Buildings { get; set; }
|
||
= new();
|
||
|
||
private Vector2[] _activeViewVerts = new Vector2[32];
|
||
private int _activeViewVertCount;
|
||
|
||
public Vector3 ViewpointIn(WalkCell cell)
|
||
=> Vector3.Transform(WorldViewpoint, cell.InverseWorldTransform);
|
||
|
||
public Matrix4x4 ObjectToClip(WalkCell cell)
|
||
=> cell.WorldTransform * _viewProjection;
|
||
|
||
public WalkCell? GetVisible(uint cellId) => _cells.GetValueOrDefault(cellId);
|
||
public IWalkRayCaster Rays => _rays;
|
||
public Vector3 WorldViewpoint { get; }
|
||
public float ViewportWidth { get; }
|
||
public float ViewportHeight { get; }
|
||
public WalkPlane CyPlane { get; }
|
||
public IWalkFrameContext CellContext => this;
|
||
|
||
public void SetActiveView(WalkPortalView views, int index)
|
||
{
|
||
WalkViewPoly poly = views.View.Polys[index];
|
||
if (_activeViewVerts.Length < poly.VertexCount)
|
||
_activeViewVerts = new Vector2[poly.VertexCount];
|
||
for (int k = 0; k < poly.VertexCount; k++)
|
||
_activeViewVerts[k] = views.View.Vertices[poly.VertexIndex + k].Point;
|
||
_activeViewVertCount = poly.VertexCount;
|
||
}
|
||
|
||
public Vector3 ViewpointInBuilding(WalkBuilding building)
|
||
=> Vector3.Transform(WorldViewpoint, Buildings[building].InverseWorldTransform);
|
||
|
||
public float ViewerDistanceTo(WalkBuilding building)
|
||
=> Vector3.Distance(
|
||
WorldViewpoint,
|
||
Vector3.Transform(building.SortCenter, Buildings[building].WorldTransform));
|
||
|
||
public int ClipBuildingPolygon(
|
||
WalkBuilding building, WalkPolygon polygon, int side, Span<WalkScreenPoint> output)
|
||
{
|
||
Matrix4x4 objectToClip = Buildings[building].WorldTransform * _viewProjection;
|
||
Span<WalkScreenPoint> projected = stackalloc WalkScreenPoint[polygon.Vertices.Length];
|
||
for (int i = 0; i < polygon.Vertices.Length; i++)
|
||
projected[i] = WalkScreenClip.TransformToScreen(
|
||
polygon.Vertices[i], objectToClip, ViewportWidth, ViewportHeight);
|
||
if (side != 0)
|
||
projected.Reverse();
|
||
return WalkScreenClip.ClipAgainstView(
|
||
projected, _activeViewVerts.AsSpan(0, _activeViewVertCount), output);
|
||
}
|
||
|
||
// ---- signatures for comparing walk output to oracle frames ----
|
||
|
||
public static string Signature(IEnumerable<WalkEvent> events)
|
||
=> string.Join("|", events.Select(e => e.Kind switch
|
||
{
|
||
WalkEventKind.Landscape => "LS",
|
||
WalkEventKind.Building => $"BLD:{e.CellId:x8}",
|
||
WalkEventKind.DrawInside => $"DI:{e.CellId:x8}",
|
||
WalkEventKind.DrawCells =>
|
||
$"DC:ov={e.OutsideViewCount}:{string.Join(',', e.Cells.Select(c => c.ToString("x8")))}",
|
||
_ => "?",
|
||
}));
|
||
|
||
/// <summary>
|
||
/// S3 chunk 1 (§11.2 B3): the OH captures interleave <c>LC</c>/<c>SC</c>/
|
||
/// <c>EC</c>/<c>OC</c> lines the pre-chunk-3 FW0 fixtures never had.
|
||
/// <see cref="RetailFrameWalk"/>'s own <c>WalkEvent</c> vocabulary has
|
||
/// exactly four kinds (Landscape/Building/DrawInside/DrawCells) — LC/SC/
|
||
/// EC/OC are separate <see cref="IWalkEventSink"/> hooks
|
||
/// <see cref="Recorder"/> never overrides, so the replay side of a
|
||
/// signature diff is silent on them by construction. Filtering them out
|
||
/// here (rather than mapping to a "?" placeholder) keeps this
|
||
/// comparison at the SAME DI/DC/BLD/LS level on both sides — S3's own
|
||
/// scope note ("no speculative pins" for LC/SC/EC/OC content) means this
|
||
/// method must not even attempt to compare them, not merely fail to.
|
||
/// </summary>
|
||
public static string Signature(WalkOracleFrame frame)
|
||
=> string.Join("|", frame.Events
|
||
.Where(e => e.Kind is WalkOracleEventKind.Landscape
|
||
or WalkOracleEventKind.Building
|
||
or WalkOracleEventKind.DrawInside
|
||
or WalkOracleEventKind.DrawCells)
|
||
.Select(e => e.Kind switch
|
||
{
|
||
WalkOracleEventKind.Landscape => "LS",
|
||
WalkOracleEventKind.Building => $"BLD:{e.CellId!.Value:x8}",
|
||
WalkOracleEventKind.DrawInside => $"DI:{e.CellId!.Value:x8}",
|
||
WalkOracleEventKind.DrawCells =>
|
||
$"DC:ov={e.OutsideViewCount}:{string.Join(',', e.Cells.Select(c => c.ToString("x8")))}",
|
||
_ => "?",
|
||
}));
|
||
|
||
/// <summary>
|
||
/// S3 chunk 1 fix round 1 (G10): the OH kit-pose captures' full
|
||
/// eight-kind vocabulary — LS/BLD/DI/DC/LC/SC/EC/OC. ONLY the OH-rooted
|
||
/// conformance rows use this (<c>WalkTraceConformanceTests</c>'
|
||
/// <c>OhCaptureRoot</c> rows) — the older FW0 fixtures
|
||
/// (docs/research/2026-08-30-fw-walk-oracle/) predate the LC/SC/EC/OC
|
||
/// cdb breakpoints and carry none of those lines at all, so comparing
|
||
/// them at this level would spuriously diverge on every frame (the
|
||
/// replay always emits LC/SC; the FW0 oracle frame never has any).
|
||
/// <para>
|
||
/// LC/SC come straight from <paramref name="tokens"/>/<paramref
|
||
/// name="frame"/>'s own literal events — real turns with a real position
|
||
/// on both sides. EC/OC do NOT: <see cref="WalkEvent"/>'s four-kind
|
||
/// vocabulary has no EC/OC analogue at all, and retail's own real
|
||
/// position for them depends on which flood a DC belongs to
|
||
/// (<c>PView::DrawCells</c>'s two complete reverse loops — shell
|
||
/// far-to-near, then object-list far-to-near). A look-in flood's DC
|
||
/// draws them IMMEDIATELY, matching <c>RetailFrameWalk.DrawLandscape</c>'s
|
||
/// own recursive <c>DrawCells</c> call; the interior root's OWN flood
|
||
/// DEFERS them past the whole landscape/look-in walk, to
|
||
/// <see cref="IWalkEventSink.OnInteriorFloodDrawTurn"/> — evidence:
|
||
/// docs/research/2026-09-01-overhaul/oh-capture/cathedral-leak.walk.log
|
||
/// lines 4 and 1328: the root DC's own EC/OC print LAST, after every
|
||
/// intervening look-in's own EC/OC pair. Both signature overloads below
|
||
/// derive EC/OC from each DC's own declared cell list under that SAME
|
||
/// rule (reversed order, twice), rather than reading the oracle's
|
||
/// literal EC/OC lines — this ALSO naturally excludes G8's trailing
|
||
/// per-frame weather OC from the comparison: it owns no DC, so no
|
||
/// derivation rule ever produces a token for it (see
|
||
/// <c>WalkOracleTraceTests</c> for a dedicated assertion that a
|
||
/// COMPLETE outdoor frame's raw event list really does end with it).
|
||
/// </para>
|
||
/// </summary>
|
||
public static string Signature8(IReadOnlyList<string> tokens) => string.Join("|", tokens);
|
||
|
||
/// <summary>The oracle-frame half of <see cref="Signature8(IReadOnlyList{string})"/> —
|
||
/// see that overload's doc comment for the shared derivation rule.</summary>
|
||
public static string Signature8(WalkOracleFrame frame)
|
||
{
|
||
var tokens = new List<string>();
|
||
bool? lookIn = null;
|
||
IReadOnlyList<uint>? pendingRootCells = null;
|
||
|
||
foreach (WalkOracleEvent e in frame.Events)
|
||
{
|
||
switch (e.Kind)
|
||
{
|
||
case WalkOracleEventKind.Landscape:
|
||
tokens.Add("LS");
|
||
break;
|
||
case WalkOracleEventKind.Building:
|
||
tokens.Add($"BLD:{e.CellId!.Value:x8}");
|
||
lookIn = true;
|
||
break;
|
||
case WalkOracleEventKind.DrawInside:
|
||
tokens.Add($"DI:{e.CellId!.Value:x8}");
|
||
lookIn = false;
|
||
break;
|
||
case WalkOracleEventKind.DrawCells:
|
||
tokens.Add(
|
||
$"DC:ov={e.OutsideViewCount}:{string.Join(',', e.Cells.Select(c => c.ToString("x8")))}");
|
||
if (lookIn == true)
|
||
AppendFloodTurns(tokens, e.Cells);
|
||
else
|
||
pendingRootCells = e.Cells;
|
||
break;
|
||
case WalkOracleEventKind.LandCell:
|
||
tokens.Add($"LC:{e.CellId!.Value:x8}");
|
||
break;
|
||
case WalkOracleEventKind.SortCell:
|
||
tokens.Add($"SC:{e.CellId!.Value:x8}");
|
||
break;
|
||
}
|
||
}
|
||
|
||
// The interior root's OWN flood: its EC/OC derive last, matching
|
||
// OnInteriorFloodDrawTurn firing as DrawInside's terminal sink call
|
||
// (see class doc) — an outdoor-rooted frame never sets this at all.
|
||
if (pendingRootCells is not null)
|
||
AppendFloodTurns(tokens, pendingRootCells);
|
||
|
||
return string.Join("|", tokens);
|
||
}
|
||
|
||
/// <summary><c>PView::DrawCells</c>'s two complete reverse loops: every
|
||
/// EnvCell shell far-to-near, THEN every object-list turn far-to-near —
|
||
/// the SAME reversed cell order twice (not one reversed EC/OC pair per
|
||
/// cell) — see <see cref="Signature8(WalkOracleFrame)"/>'s own doc
|
||
/// comment for the evidence. Internal (not private): <see
|
||
/// cref="WalkTraceConformanceTests"/>'s own <c>Recorder</c> reuses this
|
||
/// EXACT rule for the replay side rather than re-deriving it.</summary>
|
||
internal static void AppendFloodTurns(List<string> tokens, IReadOnlyList<uint> cells)
|
||
{
|
||
for (int i = cells.Count - 1; i >= 0; i--)
|
||
tokens.Add($"EC:{cells[i]:x8}");
|
||
for (int i = cells.Count - 1; i >= 0; i--)
|
||
tokens.Add($"OC:{cells[i]:x8}");
|
||
}
|
||
}
|