diff --git a/.gitignore b/.gitignore index 755511f..af968b2 100644 --- a/.gitignore +++ b/.gitignore @@ -18,18 +18,13 @@ packages/ Thumbs.db # Reference repos and retail client (large, not our code, separate licenses) -# WorldBuilder is exempt β€” it's a load-bearing dependency tracked as a git -# submodule pointing at our fork (Phase N, see docs/architecture/worldbuilder-inventory.md). -references/* -!references/WorldBuilder -!references/WorldBuilder/ +references/ # Claude Code session state .claude/ launch.log launch-*.log launch.utf8.log -n4-verify*.log # ImGui auto-saved window/docking state (per-user, not source) imgui.ini diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index c691aa8..0000000 --- a/.gitmodules +++ /dev/null @@ -1,4 +0,0 @@ -[submodule "references/WorldBuilder"] - path = references/WorldBuilder - url = git@github.com:eriknihlen/WorldBuilder.git - branch = acdream diff --git a/CLAUDE.md b/CLAUDE.md index b57414f..469b95c 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -25,107 +25,6 @@ single source of truth for how the client is structured. All work must align with this document. When the architecture doc and reality diverge, update one or the other β€” never leave them out of sync. -**WorldBuilder is acdream's rendering + dat-handling base, integrated -as of Phase N.4 ship (2026-05-08).** WB's `ObjectMeshManager` is the -production mesh pipeline; `WbMeshAdapter` is the seam; `WbDrawDispatcher` -is the production draw path (default-on, see `WbFoundationFlag`). Before -re-implementing any AC-specific rendering or dat-handling algorithm, -**read `docs/architecture/worldbuilder-inventory.md` FIRST**. If -WorldBuilder has it, port from WorldBuilder (or call into our fork via -the adapter), not from retail decomp. WorldBuilder is MIT-licensed, -verified to render the world correctly, and uses the same Silk.NET -stack we target. Re-porting from retail decomp when WB already has a -tested port is how subtle bugs (the scenery edge-vertex bug, the -triangle-Z bug) keep slipping in. Retail decomp remains the oracle for -network, physics, animation, movement, UI, plugin, audio, chat β€” see -the inventory doc's πŸ”΄ list for the full scope of "we still write this -ourselves". - -**WB integration cribs:** -- `src/AcDream.App/Rendering/Wb/WbMeshAdapter.cs` β€” single seam over WB's - `ObjectMeshManager`. Owns the WB pipeline, drains its staged-upload - queue per frame via `Tick()`, populates `AcSurfaceMetadataTable` with - per-batch translucency / luminosity / fog metadata. -- `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` β€” production draw - path. Groups all visible (entity, batch) pairs, single-uploads the - matrix buffer, fires one `glDrawElementsInstancedBaseVertexBaseInstance` - per group with `BaseInstance` pointing at the slice. Per-entity - frustum cull, opaque front-to-back sort, palette-hash memoization. -- `src/AcDream.App/Rendering/Wb/LandblockSpawnAdapter.cs` / - `EntitySpawnAdapter.cs` β€” bridge spawn lifecycle to WB ref-counts. - Atlas tier (procedural) goes via Landblock; per-instance tier - (server-spawned, palette/texture overrides) goes via Entity. -- **Modern path is mandatory as of N.5 ship amendment (2026-05-08).** - `WbFoundationFlag`, `InstancedMeshRenderer`, and `StaticMeshRenderer` - are deleted. Missing `GL_ARB_bindless_texture` or - `GL_ARB_shader_draw_parameters` throws `NotSupportedException` at - startup. There is no legacy fallback. -- **WB's modern rendering path** (GL 4.3 + bindless) packs every mesh - into a single global VAO/VBO/IBO. Each batch references its slice - via `FirstIndex` (offset into IBO) + `BaseVertex` (offset into VBO). - Honor those offsets when issuing draws β€” `DrawElementsInstanced` - with `indices=0` will draw every entity's first triangle from the - global mesh, not the per-batch range. (This is exactly the - exploded-character bug we hit during Task 26.) -- **WB's `ObjectRenderBatch.SurfaceId` is unset** β€” the actual surface - id lives in `batch.Key.SurfaceId` (the `TextureKey` struct). -- **`ObjectMeshManager.IncrementRefCount` only bumps a counter** β€” it - does NOT trigger mesh loading. You must explicitly call - `PrepareMeshDataAsync(id, isSetup)` to fire the background decode. - Result auto-enqueues to `_stagedMeshData` which `Tick()` drains. - `WbMeshAdapter` does this for you on first registration. -- **N.5 modern dispatch** (`docs/superpowers/specs/2026-05-08-phase-n5-modern-rendering-design.md`) - uses bindless textures + multi-draw indirect on top of N.4's grouped - pipeline. Per frame: three SSBO uploads (`_instanceSsbo` mat4 per - instance @ binding=0; `_batchSsbo` `(uvec2 textureHandle, uint layer, - uint flags)` per group @ binding=1; `_indirectBuffer` - `DrawElementsIndirectCommand[]` opaque-section + transparent-section). - Two `glMultiDrawElementsIndirect` calls per frame, one per pass. - Total ~12-15 GL calls per frame for entity rendering regardless of - scene complexity. -- **`TextureCache` requires `BindlessSupport`** for the WB modern path. - Three `Bindless`-suffixed `GetOrUpload*` methods return 64-bit handles - made resident at upload time, backed by parallel Texture2DArray uploads - (`UploadRgba8AsLayer1Array`). The legacy `uint`-returning methods stay - for Sky / Terrain / Debug / particle paths that still sample via - `sampler2D`. After N.6 retires legacy renderers, the legacy upload path - + caches can be deleted. -- **Translucency model is two-pass alpha-test** (matches WB), not - per-blend-mode subpasses. Opaque pass discards `Ξ±<0.95`; transparent - pass discards `Ξ±β‰₯0.95` AND `Ξ±<0.05`. Native `Additive` blend renders - as alpha-blend on GfxObj surfaces β€” falsifiable; if a magic-content - regression shows up, add a third indirect call with - `glBlendFunc(SrcAlpha, One)` per spec Β§6 fallback (~30 min change). -- **Per-instance highlight (selection blink) is reserved β€” open - backlog, no scheduled phase.** `mesh_modern.vert`'s `InstanceData` - struct has a documented hook for `vec4 highlightColor`. Whoever - eventually picks it up finds the hook there; the change is localized: - extend `InstanceData` stride 64β†’80 bytes, add the field, mix into - fragment color in `mesh_modern.frag`. ~30 min when the time comes. -- `src/AcDream.App/Rendering/TerrainModernRenderer.cs` β€” terrain dispatcher - on N.5's modern primitives. Mirrors WB's `TerrainRenderManager` pattern - (single global VBO/EBO + slot allocator + `glMultiDrawElementsIndirect`) - but driven by acdream's `LandblockMesh.Build` so retail's `FSplitNESW` - formula is preserved (issue #51 resolved). Atlas handles bound via the - uvec2 + `sampler2DArray(handle)` constructor pattern (NOT the direct - `uniform sampler2DArray` + `glProgramUniformHandleARB` form, which - GL_INVALID_OPERATIONs on at least one driver). -- **Two-tier streaming architecture (Phase A.5, shipped 2026-05-10).** - `src/AcDream.App/Streaming/` owns the full streaming pipeline. Key types: - `StreamingRegion` (two-radius Chebyshev window: N₁=near, Nβ‚‚=far; produces - `TwoTierDiff` with 5 transition lists per tick), `StreamingController` - (render-thread coordinator: routes `TwoTierDiff` to the worker queue and - drains completions up to `MaxCompletionsPerFrame` per frame), - `LandblockStreamer` (single background worker thread: `LoadFar` = heightmap - + mesh only, `LoadNear` = heightmap + `LandBlockInfo` + scenery + mesh, - `PromoteToNear` = `LandBlockInfo` + scenery only), - `GpuWorldState` (render-thread entity state: `AddEntitiesToExistingLandblock` - for promotions, `RemoveEntitiesFromLandblock` for demotions). - Default: N₁=4 (81 near LBs, full detail), Nβ‚‚=12 (544 far LBs, terrain - only). Quality Preset system (`QualitySettings.From(preset)`) controls - both radii and MSAA/anisotropic/A2C/completions-per-frame as a unit. - Spec: `docs/superpowers/specs/2026-05-09-phase-a5-two-tier-streaming-design.md`. - **Execution phases:** R1β†’R8 in the architecture doc. Each phase has clear goals, test criteria, and builds on the previous. Don't skip phases. @@ -147,10 +46,9 @@ time using dat assets); ImGui persists forever as the `ACDREAM_DEVTOOLS=1` overlay. **All plugin-facing UI targets `AcDream.UI.Abstractions` β€” never import a backend namespace from a panel.** Full design: `docs/plans/2026-04-24-ui-framework.md`. -Memory cribs: `memory/project_chat_pipeline.md` (chat pipeline as of -Phase I), `memory/project_input_pipeline.md` (input pipeline as of -Phase K). UI architecture full design at -[`docs/plans/2026-04-24-ui-framework.md`](docs/plans/2026-04-24-ui-framework.md). +Memory cribs: `memory/project_ui_architecture.md` (architecture), +`memory/project_chat_pipeline.md` (chat pipeline as of Phase I), +`memory/project_input_pipeline.md` (input pipeline as of Phase K). **Input pipeline:** `src/AcDream.UI.Abstractions/Input/` (action enum, `KeyChord`, `KeyBindings`, multicast `InputDispatcher` with scope @@ -189,23 +87,12 @@ retail. Everything else is your call. files, reverting multiple commits) - Memory or committed history shows a clear user preference you're about to diverge from -- **The request is an investigation, audit, analysis, review, or "report-only"** - β€” no edits, no writes, no diagnostic code drops until you've delivered the - report and the user explicitly approves a fix. Use the `/investigate` skill - (`.claude/skills/investigate/SKILL.md`) to enter this mode cleanly. -- **A referenced commit, file path, branch, or doc doesn't exist** where the - user said it would. Ask one short question; don't go hunting across branches - or worktrees. A 1-line clarification beats 30 minutes of wrong-branch - exploration. **Things you should just do without asking:** - Continue to the next planned sub-step of a phase after the previous one lands clean β€” including immediately starting work on the next phase if the - current one is done. **You pick what comes next** per the Milestone - discipline section β€” never present the user a menu like "should we do X - or Y?" or ask "what next?". Just choose and announce the choice in one - sentence. Work-order selection is Claude's job, not the user's. + current one is done - Pick between two roughly equivalent implementations; justify the choice in the commit message - Refactor small amounts of surrounding code when genuinely needed to land a @@ -359,14 +246,6 @@ isn't enough, attach cdb to a live retail client (Step -1).** context of the existing code it's modifying. The first animation sequencer integration was done by a subagent that didn't understand the transform pipeline β€” it broke everything. -- **Do not replace working retail-faithful logic with a modern redesign** - without explicit user approval. Two campaigns (267 min remote-entity - prediction+rubber-band replacing hard-snap; speculative shader edits in - the sky-fog work) had to be reverted in full because the redesign - regressed behavior the original port had right. When you see "I could - simplify this with X" on a retail-port, flag the tradeoff and ask - before deleting the existing path. Retail-faithful first; "cleaner" - second. ### Phase completion checklist: @@ -500,66 +379,6 @@ This toolchain was used to settle the L.5 steep-roof investigation: `set_collide` rate per minute. See commit history around 2026-04-30 for the trace data and the decisions it drove. -## MCP servers (live tooling) - -Two MCP servers extend the static decomp + cdb workflow with live -introspection. **Ghidra MCP** requires Ghidra to be running with a -CodeBrowser open in the target project; **WireMCP** auto-loads at -Claude Code startup. - -### Ghidra MCP (LaurieWired v1.4, HTTP) - -Starts an HTTP server on **port 8080** (or **8081** if 8080 is -taken β€” first-open-wins) when a CodeBrowser tool opens a program. -Currently serving **`patchmem.gpr`** β€” the 2013 v11.4186 build with -full PDB applied, same source as `docs/research/named-retail/`. Use -this when grep'ing `acclient_2013_pseudo_c.txt` returns too much -noise and you want the decomp for one specific function or address -without dumping the whole file into context. - -Probe: `curl http://127.0.0.1:8081/methods?limit=3` - -Useful endpoints (GET unless noted): - -- `/methods?limit=N` β€” function names -- `/list_functions?limit=N` β€” `Name at HHHHHHHH` lines -- `/decompile_function?address=0xHHHHHHHH` β€” decompiled C for one function -- `/function_xrefs?name=...` β€” callers / callees -- `/classes`, `/namespaces`, `/strings` -- POST `/rename_function_by_address`, POST `/set_decompiler_comment` - -NO endpoints for: signature setting, namespace setting, script -execution, save-project. Those still require Ghidra's GUI or -`analyzeHeadless`. Full endpoint catalog + Ghidra project layout in -`memory/reference_ghidra_projects.md`. - -### WireMCP (stdio, Node, user-scope) - -Wraps `tshark` at `C:\Program Files\Wireshark\tshark.exe` -(auto-detected via the Windows fallback path in `WireMCP/index.js`). -Direct fit for ACE wire-protocol work β€” capture loopback -(`127.0.0.1:9000`) to cross-check inbound message parsing (`0xF61C` -movement, `0xF74A` pickup despawn, `0xF7DE` chat, etc.) against the -actual bytes, or diff ACE's outbound vs. the holtburger reference. -Replaces ad-hoc Wireshark sessions in the conversation. - -Tools exposed: - -- `capture_packets` β€” short live capture on an interface, returns JSON -- `get_summary_stats` β€” protocol hierarchy stats -- `get_conversations` β€” TCP/UDP conversation table -- `analyze_pcap` β€” parse a saved `.pcap` file -- `check_threats`, `check_ip_threats` β€” URLhaus / threat-feed lookups -- `extract_credentials` β€” grep for creds across protocols (rarely relevant) - -Installed at `C:\Users\erikn\source\repos\WireMCP\` (clone of -`0xKoda/WireMCP`). Registered via `claude mcp add wiremcp --scope user`. - -**When NOT to use WireMCP:** decoding the AC packet *format* β€” that -lives in `holtburger`, ACE, and `Chorizite.ACProtocol`. WireMCP shows -you the bytes on the wire; the reference repos tell you what they -mean. - ## Subagent policy Subagents are the primary tool for saving parent-context and keeping one @@ -589,78 +408,6 @@ spec path, the files it should read, the acceptance criteria (build + test green), and the commit message style. Subagents inherit CLAUDE.md so they follow the same rules. -## Milestone discipline - -acdream operates at **two altitudes** above the daily commit: - -- **`docs/plans/2026-05-12-milestones.md`** β€” the morale + scope layer. - Seven milestones (M0–M7) from "Connect & explore" to "v1.0", each - defined by a concrete playable scenario and ~6–10 weeks of work. This - is where you orient when the project feels half-built and you're not - sure what to work on. Phases are too granular to feel like progress; - this doc is the multi-week target. -- **`docs/plans/2026-04-11-roadmap.md`** β€” the strategic roadmap (next - section). Phase-level index. This is where you orient when you know - the milestone and need the next concrete sub-phase. - -**Currently working toward: M1 β€” Walkable + clickable world.** L.2 -collision + B.4 interaction. Demo target: walk through Holtburg without -getting stuck, open the inn door, click an NPC, pick up an item. -Estimated 4–6 weeks from 2026-05-12. - -**Work-order autonomy β€” the meta-rule.** You decide what to work on -next, always. **The user does NOT pick between phases, milestones, or -"what's next?" alternatives.** The milestone discipline + the -per-milestone phase list + the roadmap IS the work order β€” drive it. -Never ask the user "want me to start X or Y?" or present a menu of -options. If two next steps are genuinely equivalent, state which one -you picked and why in one sentence and start β€” don't ask. The user -retains the right to redirect if they think you're wrong, but the -default is **Claude drives, user reviews**. The user finds decision -fatigue from constant work-order choices draining β€” that's literally -what triggered the milestones doc on 2026-05-12. Honoring this rule is -the single biggest morale lever. This is the meta-rule that makes the -four below actually work. - -**The four motivation-keeping rules:** - -1. **One active milestone at a time.** Work that isn't on the critical - path to M1 gets filed in `docs/ISSUES.md` with a `post-M1` tag and - muted. This is the single rule that kills the "jumping between - things" feeling. If a phase isn't part of the current milestone, it - doesn't get touched β€” even if it's tempting, even if it would be - "quick", even if it would be "while I'm here." - -2. **Frozen phases are off-limits.** M0's ~25 shipped phases are frozen - until M7's polish pass. Concretely: no rework on streaming, chat, - input, the WB rendering migration, sky/lighting, the particle - system, or the network handshake. Those are done. Don't revisit them - β€” even if you see something that could be 10% better. Visual - nice-to-haves and architecture second-guesses on frozen phases are - explicitly post-M7. The freeze list per milestone lives in the - milestones doc. - -3. **Each milestone hit gets a recorded demo video.** When M1 lands, - record ~30 seconds of the demo scenario, drop it at - `docs/milestones/M1-walkable-clickable.mp4`, and pin a still + a - one-paragraph writeup at the top of `2026-05-12-milestones.md`. The - freeze list updates. The "currently working toward" line in this - CLAUDE.md updates to M2. **Crossing a milestone is a real event with - an artifact** β€” that's the morale instrument. Phases ship; milestones - land. - -4. **State both altitudes at session start.** First action of any - session: "Currently working toward M1 β€” Walkable + clickable world. - Current phase: L.2. Next concrete step: [whatever]." This keeps the - high-level orientation visible alongside the immediate task and - makes mid-session drift obvious. - -When reality and the milestones diverge β€” a phase grows beyond the -milestone's scope, a demo scenario turns out to be unreachable without -a new sub-phase, the order needs reshuffling β€” update the milestones -doc in the same session you discover the divergence. Same rule as the -roadmap. - ## Roadmap discipline acdream's plan lives in two files committed to the repo: @@ -677,167 +424,6 @@ acdream's plan lives in two files committed to the repo: acceptance criteria. Do not drift from the spec without explicit user approval. -**Currently in Phase L.2 (Movement & Collision Conformance).** L.2a slices -1+2+3 + L.2d slice 1+1.5 + L.2g slice 1 + L.2g slice 1b + L.2g slice 1c + -**Phase B.4b** + **Phase B.4c** all shipped and visual-verified 2026-05-13; -**Phase B.5** (ground-item pickup, F-key) shipped and visual-verified -2026-05-14. The M1 demo target *"pick up an item"* is met for the -close-range path β€” single-click a ground item to select, walk within -~0.6 m of it, press F, and the item is removed from the world and added -to the player's inventory. Wire chain: `InteractRequests.BuildPickUp` -sends `PutItemInContainer (0xF7B1/0x0019)`; ACE despawns the item with -`GameMessagePickupEvent (0xF74A)` (NOT `0xF747 DeleteObject` β€” the -distinction surfaced during visual testing and is fixed by the new -`PickupEvent.cs` parser routed through the shared `EntityDeleted` -event). The M1 demo target *"open the inn door"* remains met from B.4b -+ B.4c. Issue #57 (B.4 handler gap) is closed. Issue #58 (door swing -animation) is closed by B.4c. Issues #61 (linkβ†’cycle boundary flash), -#62 (PARTSDIAG null-guard), **#63 (server-initiated MoveToObject -auto-walk not honored β€” blocks out-of-range pickup / Use)**, and **#64 -(local-player pickup animation does not render)** are filed as -M1-deferred follow-up. - -**B.5 ship handoff:** [`docs/research/2026-05-14-b5-shipped-handoff.md`](docs/research/2026-05-14-b5-shipped-handoff.md) -β€” full evidence for the 5 commits across InteractRequests / GameWindow / WorldSession + the bonus `PickupEvent (0xF74A)` wire-handler fix that closes the despawn gap. -**B.4c ship handoff:** [`docs/research/2026-05-13-b4c-shipped-handoff.md`](docs/research/2026-05-13-b4c-shipped-handoff.md) -β€” full evidence for the 4 commits + 2 bonus discoveries (stance-value wrong -`0x01` vs `0x3D` causing underground doors; linkβ†’cycle boundary flash). -**B.4b ship handoff:** [`docs/research/2026-05-13-b4b-shipped-handoff.md`](docs/research/2026-05-13-b4b-shipped-handoff.md) -β€” full evidence for the 9 commits + 4 bonus discoveries (double-click dead -code, DoubleClick gate, CollisionExemption, ServerGuidβ†’Id translation). -**L.2g slice 1 ship handoff:** [`docs/research/2026-05-12-l2g-slice1-shipped-handoff.md`](docs/research/2026-05-12-l2g-slice1-shipped-handoff.md). -**L.2d ship handoff:** [`docs/research/2026-05-13-l2d-slice1-shipped-handoff.md`](docs/research/2026-05-13-l2d-slice1-shipped-handoff.md). - -**Phase L.2a (Truth & Diagnostics) slices 1-3 shipped 2026-05-12.** -Three commits land the L.2 "make every bad movement outcome explainable" -diagnostic foundation. Slice 1 (`ebef820`) adds runtime-toggleable -`ACDREAM_PROBE_RESOLVE` (one `[resolve]` line per -`PhysicsEngine.ResolveWithTransition` call) + `ACDREAM_PROBE_CELL` (one -`[cell-transit]` line per `PlayerMovementController.CellId` change), -both backed by a new `AcDream.Core.Physics.PhysicsDiagnostics` static -class and mirrored as DebugPanel checkboxes. Slice 2 (`e0c08bc`) extends -the `[resolve]` line with `obj=0x...` attribution. Slice 3 (`a068292`) -populates the previously-stub `CollisionInfo.CollideObjectGuids` / -`LastCollidedObjectGuid` (declared in `TransitionTypes.cs` but never -written anywhere) at the per-object iteration in `FindObjCollisions`, -so the slice-2 promise is now actually delivered. Visual-verified at -the Holtburg Town doorway: probes captured 140 wall hits attributed to -`obj=0xA9B47900` (landblock-baked static = the building itself, -**NOT** a door entity), confirming L.2d sub-direction as **port -`CBuildingObj` collision + per-cell walkability** rather than door- -state-toggle. Plus a definitive L.2e finding: player `CellId` tracked -as bare low byte (`0x00000029`) with no landblock prefix. - -**Phase C.1.5b (per-part PES transforms + dat-hydrated entity DefaultScript) -shipped 2026-05-12.** Closes issue #56. `SetupPartTransforms.Compute(setup)` -walks `PlacementFrames[Resting]` β†’ `[Default]` β†’ first-available and -returns one `Matrix4x4` per Setup part; `ParticleHookSink.SpawnFromHook` -now transforms each `CreateParticleHook.Offset` through -`partTransforms[PartIndex]` before applying entity rotation, so -multi-emitter scripts distribute across mesh parts instead of collapsing -to entity root. The `EntityScriptActivator.OnCreate` `ServerGuid==0` -guard was relaxed: it now keys by `entity.ServerGuid` when non-zero, else -`entity.Id` (the `0x40xxxxxx` interior-entity range is collision-free -with server guids, so no synthetic-ID scheme is needed). `GpuWorldState` -fires the activator from 4 new sites β€” `AddLandblock` + -`AddEntitiesToExistingLandblock` (Farβ†’Near promotion) for OnCreate, -`RemoveLandblock` + `RemoveEntitiesFromLandblock` (Nearβ†’Far demotion) -for OnRemove β€” so dat-hydrated EnvCell statics (inn fireplaces, building -decorations) and exterior stabs (cottage chimneys) now activate their -`Setup.DefaultScript` automatically. **Reality discovery during design -(folded into spec Β§3):** EnvCell `StaticObjects` are already hydrated as -`WorldEntity` instances by `GameWindow.BuildInteriorEntitiesForStreaming` -with stable `entity.Id` in `0x40xxxxxx` β€” the handoff's Β§4 Q1/Q2 -(synthetic ID scheme, separate walker class) were mooted by this. -**Visual-verified 2026-05-12** at Holtburg Town network portal (no -ground-burial, distributed swirl), Inn fireplace flames, cottage -chimney smoke, and a spell cast on `+Acdream`. Plan archived at -[`docs/superpowers/plans/2026-05-13-phase-c1.5b.md`](docs/superpowers/plans/2026-05-13-phase-c1.5b.md). - -**Phase C.1.5a (portal PES wiring) shipped 2026-05-11** (merge `88bda12`). -Server-spawned `WorldEntity` entities fire their `Setup.DefaultScript` -through `PhysicsScriptRunner` on enter-world via the -`EntityScriptActivator` ([src/AcDream.App/Rendering/Vfx/EntityScriptActivator.cs](src/AcDream.App/Rendering/Vfx/EntityScriptActivator.cs)). -Visual-verified at the Holtburg Town network portal: 10-hook portal -script fires end-to-end with correct color, persistence, orientation, -multi-emitter dispatch. Filed #56 for per-part transform handling -(resolved in C.1.5b above). Plan archived at -[`docs/superpowers/plans/2026-05-12-phase-c1.5a-portals.md`](docs/superpowers/plans/2026-05-12-phase-c1.5a-portals.md). - -**Phase N.6 slice 1 (gpu_us fix + radius=12 perf baseline) shipped -2026-05-11** (merge `9b447d4`). Fixed `gpu_us` double-buffering in -`WbDrawDispatcher` (ring-of-3 query slots, read-before-overwrite, -vendor-neutral). Captured authoritative perf baseline at Holtburg radii -4 / 8 / 12. **Conclusion: CPU dominates GPU by 30–50Γ— at every radius**; -GPU sits at 3.6% of frame budget; per-LB walk is the next bottleneck. -Baseline-doc recommendation: do C.1.5 next, then a reduced-scope slice 2 -(atlas + persistent-mapped buffers dropped from slice-2 scope). Baseline -at [`docs/plans/2026-05-11-phase-n6-perf-baseline.md`](docs/plans/2026-05-11-phase-n6-perf-baseline.md). -Plan archived at [`docs/superpowers/plans/2026-05-11-phase-n6-slice1.md`](docs/superpowers/plans/2026-05-11-phase-n6-slice1.md). -Issue #55 filed (static-entity slow path reports ~1.45M `meshMissing` -per 5s at r4 standstill β€” diagnostic, not a visible regression). - -**Post-A.5 polish phase complete 2026-05-11.** All three post-A.5 -issues closed: #52 (lifestone, `e40159f`), #54 (JobKind, `bf31e59`), -#53 (Tier 1 entity cache, `f928e66`). Phase A.5 + post-A.5 polish -together comprise the streaming + rendering perf foundation for the -project. - -**Next phase candidates (in rough preference order):** -- **"Click an NPC" verification spike (M1 critical path).** B.4b's - `WorldPicker` + `BuildUse` is already wired. The question is whether ACE - NPCs respond to a Use message from our testaccount and what they broadcast - back (TalkDirect? MoveToObject?). Spike: stand near a Holtburg NPC, - double-click, read what ACE sends back. If ACE responds with recognizable - packets, wire the handlers; if it is silent, investigate ACE's NPC handler - configuration. ~30 min spike, outcome determines whether NPC interaction - needs a full phase or is a one-commit fix. -- **Phase B.6 β€” Client-side MoveToObject auto-walk handling (closes #63).** - ACE auto-walks the player to out-of-range Use / Pickup targets via - `CreateMoveToChain` + `EnqueueBroadcastMotion(MoveToObject)`, but our client - doesn't honor the inbound motion broadcast β€” character drifts toward the - target and snaps back, ACE's chain times out. Reference implementation - exists in `references/holtburger/crates/holtburger-core/src/client/simulation.rs` - (the `approximate_move_to_object_projection_target` + `MoveToObject` case). - Unlocks double-click pickup, F-key pickup from any distance, Use on - out-of-range NPCs / corpses. Probably 1-2 commits + visual verification. -- **Triage the chronic open-issue list** in `docs/ISSUES.md` β€” #2 (lightning), - #4 (sky horizon-glow), #28 (aurora), #29 (cloud thinness), #37 (humanoid - coat), #41 (remote-motion blips) have been open since April/early-May and - keep getting deferred. Either link each to a future phase or downgrade. - ~1 hour, surfaces what's chronic vs. linked-to-a-phase. -- **More Phase C visual-fidelity work** (C.2 dynamic point lights, C.3 - palette tuning, C.4 double-sided translucent polys) closing the - "world reads as old / broken vs. retail" backlog. -- **N.6 slice 2** at reduced scope (atlas opportunities only β€” persistent- - mapped buffers and other slice-2 items dropped per slice-1 baseline doc). -- **Perf tiers 2/3** (`docs/plans/2026-05-10-perf-tiers-2-3-roadmap.md`) - only if user wants sustained 500+ FPS. With Tier 1 dispatcher at ~1.2 ms - the project comfortably hits 200-400 FPS at radius=12 standstill; - escalation is optional from here. -- **Issue #61 β€” AnimationSequencer linkβ†’cycle boundary flash** (M1-deferred - polish). Brief flap at end of door-swing animations. Low severity; does - not block M1 demo. Address before milestone demo record if distracting. -- **Issue #62 β€” PARTSDIAG null-guard** (trivial latent fix). One-line - null-coalescing guard in `GameWindow.TickAnimations`. Address any time a - diagnostic-related PR is open nearby. - -**Earlier rendering + streaming arc (2026-05-08 β†’ 2026-05-10).** -Phases **N.4 β†’ N.5 β†’ N.5b β†’ A.5** shipped the modern rendering -pipeline + two-tier streaming foundation: WB `ObjectMeshManager` as -production mesh path (N.4); bindless + `glMultiDrawElementsIndirect` -for entities (N.5, ~12-15 GL calls/frame) and terrain via Path C -preserving retail's `FSplitNESW` formula (N.5b, closes #51); two-tier -streaming N₁=4 / Nβ‚‚=12 + QualityPreset system (A.5). Modern path is -mandatory as of N.5 ship amendment β€” `InstancedMeshRenderer`, -`StaticMeshRenderer`, `WbFoundationFlag` all deleted; missing bindless -throws at startup. Detail + decomp anchors + plan archives in roadmap -shipped-table rows 63–66 at `docs/plans/2026-04-11-roadmap.md`. -Engineering gotchas (bindless Dispose order, texture target lock-in, -`uvec2` sampler-handle pattern, WB-vs-retail formula divergence) -documented inline at the relevant call sites and in -`feedback_wb_migration_*.md` memory entries. - **Rules:** 1. Before starting a new phase or sub-piece, re-read the roadmap and the @@ -966,18 +552,6 @@ via `PlayerMovementController.ApplyServerRunRate`) or from diagnostics (`[UM_RAW]`, `[SCFAST]`, `[SCFULL]`, `[SETCYCLE]`, `[FWD_WIRE]`, `[OMEGA_DIAG]`, `[SEQSTATE]`, `[PARTSDIAG]`, `[VEL_DIAG]`, `[UPCYCLE]`). Heavy. -- `ACDREAM_PROBE_RESOLVE=1` β€” L.2a slice 1+2+3 (2026-05-12). One - `[resolve]` line per `PhysicsEngine.ResolveWithTransition` call: - input + target + output position/cell, ok-vs-partial, grounded-in, - contact-plane status, wall normal if hit, **responsible entity - guid** (post-slice-3 attribution plumbing), env flag, walkable - polygon valid. Heavy (~30 Hz Γ— every entity). Runtime-toggleable - via the DebugPanel "Diagnostics" section if `ACDREAM_DEVTOOLS=1`. -- `ACDREAM_PROBE_CELL=1` β€” L.2a slice 1 (2026-05-12). One - `[cell-transit]` line per `PlayerMovementController.CellId` - change: old β†’ new cell, world position, reason tag - (`resolver` / `teleport`). Low volume β€” only fires on actual cell - crossings. Runtime-toggleable via the same DebugPanel section. - *(retired 2026-05-05 by L.3 M2/M3)* `ACDREAM_INTERP_MANAGER` was an env-var gate on an experimental per-tick remote motion path. L.3 M2 (commit 40d88b9) replaced both gates (`OnLivePositionUpdated` + @@ -1051,18 +625,11 @@ these, ideally all four: for the palette-indexed formats. See `ACViewer/Render/TextureCache.cs::IndexToColor` for the canonical subpalette overlay algorithm. -- **`references/WorldBuilder/`** β€” **acdream's rendering + dat-handling - BASE (not just a reference).** As of 2026-05-08 acdream is moving to - fork WorldBuilder upstream and depend on the fork for terrain, - scenery, static objects, EnvCells, portals, sky, particles, texture - decoding, mesh extraction, visibility/culling. WorldBuilder is - MIT-licensed, exact-stack match (Silk.NET + .NET), and verified to - render the world correctly. **Before re-porting any rendering or - dat-handling algorithm from retail decomp, check - `docs/architecture/worldbuilder-inventory.md` first.** If WB has it, - use WB's port. If WB doesn't have it (network, physics, animation, - movement, UI, plugin, audio, chat), port from retail decomp as - before. +- **`references/WorldBuilder/`** β€” C# + Silk.NET dat editor. Exact-stack + match to acdream for rendering approaches: terrain blending, texture + atlases, shader patterns. Most useful for "how do I do this GL thing + with Silk.NET on net10 idiomatically?" Less useful for protocol or + character appearance (dat editor, not game client). - **`references/Chorizite.ACProtocol/`** β€” clean-room C# protocol library generated from a protocol XML description. Useful sanity check on field order, packed-dword conventions, type-prefix handling. The @@ -1117,15 +684,12 @@ decompiled client code and would have fixed it in minutes. | Domain | Primary Oracle | Secondary | Notes | |--------|---------------|-----------|-------| -| **Any AC-specific algorithm** | **`docs/research/named-retail/`** (PDB-named decomp + verbatim retail header structs from Sept 2013 EoR build) | the existing references below | The retail client itself, fully named. 18,366 functions + 5,371 struct types + 1.4 M lines of pseudo-C in one searchable tree. Beats every other reference for "what does the real client do." Use for everything in the πŸ”΄ list (network, physics, animation, movement, UI, plugin, audio, chat). | -| **Terrain** (split direction, height sampling, palCode, vertex position, normals) | **WorldBuilder `TerrainGeometryGenerator.cs` + `TerrainUtils.cs`** | retail decomp for cross-check | WB is acdream's terrain base. ACME's port is older/SUPERSEDED by WB. | -| **Terrain blending** (texture atlas, alpha masks, road overlays) | **WorldBuilder `LandSurfaceManager.cs`** | ACME `LandSurfaceManager.cs` (same algo, less complete) | WB is acdream's blending base. | -| **Scenery** (procedural placement: trees, bushes, rocks, fences) | **WorldBuilder `SceneryRenderManager.cs` + `SceneryHelpers.cs`** | retail decomp `CLandBlock::get_land_scenes` | WB is acdream's scenery base. Re-porting from retail decomp is what caused the edge-vertex bug. | -| **GfxObj / Setup rendering** (mesh extraction, multi-part assembly, ObjDesc) | **WorldBuilder `StaticObjectRenderManager.cs` + `ObjectMeshManager.cs`** | ACME `StaticObjectManager.cs` (includes CreaturePalette, GfxObjRemapping, HiddenParts β€” useful for character appearance which WB doesn't cover) | WB for static objects, ACME for character appearance. | -| **Texture decoding** (INDEX16, P8, DXT, BGRA, alpha) | **WorldBuilder `TextureHelpers.cs`** | ACME `TextureHelpers.cs`; ACViewer's `IndexToColor` is canonical for subpalette overlay | WB is acdream's decode base. | -| **EnvCell / dungeon rendering** (cell geometry, portal visibility, collision mesh) | **WorldBuilder `EnvCellRenderManager.cs` + `PortalRenderManager.cs`** | ACME `EnvCellManager.cs` (more complete for collision); ACViewer `Physics/Common/EnvCell.cs` | WB is acdream's geometry base; ACME for collision until ported. | -| **Particles / sky** (particle systems, weather, sky particles) | **WorldBuilder `SkyboxRenderManager.cs` + `ParticleEmitterRenderer.cs` + `ParticleBatcher.cs`** | retail decomp | WB is acdream's particle base. | -| **Visibility / culling** (frustum, cell visibility) | **WorldBuilder `VisibilityManager.cs` + `Frustum.cs`** | β€” | WB. | +| **Any AC-specific algorithm** | **`docs/research/named-retail/`** (PDB-named decomp + verbatim retail header structs from Sept 2013 EoR build) | the existing references below | The retail client itself, fully named. 18,366 functions + 5,371 struct types + 1.4 M lines of pseudo-C in one searchable tree. Beats every other reference for "what does the real client do." | +| **Terrain** (split direction, height sampling, palCode, vertex position, normals) | **ACME `ClientReference.cs`** β€” decompiled retail client with exact offsets | ACME `TerrainGeometryGenerator.cs` (matches the mesh index buffer) | WorldBuilder original is SUPERSEDED for terrain algorithms. AC2D confirms the same formula. | +| **Terrain blending** (texture atlas, alpha masks, road overlays) | **ACME `LandSurfaceManager.cs`** | WorldBuilder original `LandSurfaceManager.cs` (same code, less tested) | Both use the same TexMerge pipeline. ACME has conformance tests. | +| **GfxObj / Setup rendering** (mesh extraction, multi-part assembly, ObjDesc) | **ACME `StaticObjectManager.cs`** β€” includes CreaturePalette, GfxObjRemapping, HiddenParts | ACViewer `Render/` namespace | ACME has the complete creature appearance pipeline in one file. | +| **Texture decoding** (INDEX16, P8, DXT, BGRA, alpha) | **ACME `TextureHelpers.cs`** | ACViewer `Render/TextureCache.cs` (palette overlay = `IndexToColor`) | For subpalette overlay specifically, ACViewer's `IndexToColor` is the canonical algorithm. | +| **EnvCell / dungeon rendering** (cell geometry, portal visibility, collision mesh) | **ACME `EnvCellManager.cs`** β€” portal traversal, mixed landblock detection, collision cache | ACViewer `Physics/Common/EnvCell.cs` | ACME is significantly more complete than original WorldBuilder for dungeons. | | **Network protocol** (wire format, packet framing, fragment assembly, ISAAC) | **holtburger** `crates/holtburger-session/` | AC2D `cNetwork.cpp` (simpler, good for cross-check) | ACE shows the server side; holtburger + AC2D show the client side. | | **Client behavior** (what to send when, login flow, ack pattern, keepalive) | **holtburger** `crates/holtburger-core/src/client/` | AC2D `cNetwork.cpp` + `cInterface.cpp` | holtburger is the most complete; AC2D is simpler but confirmed working. | | **Movement** (MoveToState format, AutonomousPosition, sequence counters, speed) | **holtburger** `client/movement/` | AC2D `cNetwork.cpp:2592-2664` (0xF61C format) | See `docs/research/2026-04-12-movement-deep-dive.md` for the full cross-reference. | diff --git a/docs/ISSUES.md b/docs/ISSUES.md index d55d87c..87c7b2d 100644 --- a/docs/ISSUES.md +++ b/docs/ISSUES.md @@ -46,569 +46,13 @@ Copy this block when adding a new issue: # Active issues -## #69 β€” Local player rotation isn't animated (no leg/arm cycle while pivoting) +## #49 β€” Scenery (X, Y) placement drifts from retail at some landblocks **Status:** OPEN -**Severity:** LOW (visual polish β€” rotation works, just looks stiff) -**Filed:** 2026-05-15 (B.6 close-range turn-to-face) -**Component:** motion / animation cycle - -**Description:** When the auto-walk overlay rotates the local player -(close-range Use turn-to-face, or turn-first phase of a far-range walk), -the body's Yaw rotates smoothly but no leg / arm animation plays β€” -the body just statue-pivots. Retail played a `TurnLeft` / `TurnRight` -motion cycle while rotating, visible to observers as the character -moving their legs / arms to turn. - -**Cause:** `ApplyAutoWalkOverlay` synthesises `Forward+Run` input -during the walking phase (so the motion interpreter emits `RunForward` -cycle commands), but synthesises nothing during the turn-only phase -β€” so the motion interpreter emits no command and the sequencer -holds whatever cycle was last set (typically Ready / idle). - -**Approach:** While turning (`!walkAligned`), synthesise -`TurnLeft = delta > 0` / `TurnRight = delta < 0` so the motion -interpreter emits the turn command. Care needed: the existing -`Update` body also steps Yaw on `TurnLeft`/`TurnRight` input β€” if -both apply, the body rotates twice as fast. Cleanest: set the input -flags AND skip the overlay's own Yaw step (let Update's existing -handling do the rotation). - -**Acceptance:** A retail observer watching `+Acdream` turn to face -an NPC sees the turning animation play (leg shuffle / arm swing) for -the duration of the rotation. - -**Estimated scope:** Small. ~30 LOC in `ApplyAutoWalkOverlay` plus -verification that retail's `TurnLeft`/`TurnRight` cycle is in the -human motion table. - ---- - -## #68 β€” Remote players don't stop running animation on auto-walk arrival - -**Status:** OPEN -**Severity:** LOW-MEDIUM (visual only β€” server-side action completes correctly) -**Filed:** 2026-05-15 (B.7 visual verification) -**Component:** motion / remote dead-reckoning / animation cycle - -**Description:** Observing a retail player from acdream as they approach -an NPC at a distance: the remote body's run animation keeps cycling -even after the body has visibly stopped at the NPC. Retail-side the -character stopped; the action (dialogue) fired; but our client's -animation never transitioned RunForward β†’ Ready. - -**Suspected:** `RemoteMoveToDriver` detects arrival via -`DriveResult.Arrived`, but the consumer site (per-tick loop in -`GameWindow.TickAnimations` or wherever the remote body's cycle is -driven) doesn't flip the animation cycle back to Ready on arrival. -Alternatively the cycle persists because ACE doesn't broadcast a -follow-up `UpdateMotion(Ready)` β€” relying on the client to detect -arrival from the wire's distance threshold instead. - -**Files (likely):** -- `src/AcDream.App/Rendering/GameWindow.cs` β€” wherever per-tick motion - for remote entities reads `RemoteMoveToDriver`'s state. Need to - call `SetCycle(NonCombat, Ready)` on arrival. - -**Acceptance:** Retail player observed running up to an NPC visibly -stops running animation at arrival distance, transitions to idle. - ---- - -## #67 β€” [DONE 2026-05-15 Β· `301281d`] Door Use action doesn't complete after auto-walk arrival - -**Status:** DONE β€” fixed by `301281d` (10 Hz heartbeat during motion). -With ACE seeing our position in near-real-time, its `CreateMoveToChain` -converges normally for doors as well as NPCs. Root cause was 1 Hz -position sync on our side, not anything door-specific. User confirmed -doors work after the heartbeat bump. - ---- - -## #66 β€” Local + remote rotation: player flips back, NPCs don't turn - -**Status:** OPEN -**Severity:** LOW-MEDIUM (visual feedback β€” interaction works, -just looks wrong) -**Filed:** 2026-05-15 (B.7 visual verification) -**Component:** motion / rotation - -**Description:** Two related visual rotation bugs surfaced together: - -1. **Local player flips back.** Observing acdream's `+Acdream` from - retail: when our auto-walk completes and the body has rotated to - face the target, the broadcast position has the new rotation β€” - then the next frame the player snaps back to whatever the camera - yaw was. Likely cause: after `EndServerAutoWalk`, the synthesised - input stops and `Update`'s next pass applies the user's real - `MouseDeltaX` (which may be 0 but other paths might be - overriding `Yaw`). -2. **NPCs don't turn to face the player.** ACE broadcasts - `MovementType=8 TurnToObject` when an NPC starts a Use response - that requires facing. Our `OnLiveMotionUpdated` handles - MovementType=6 (MoveToObject) but not 8. The NPC's body stays - at whatever heading the spawn / last motion left it. - -**Acceptance:** -- After auto-walk arrival, local player's facing toward the target - is preserved (no flip-back observed from a retail client). -- NPCs (Tirenia, guards, vendors) rotate to face the player when - using them. - -**Files (likely):** -- `src/AcDream.Core.Net/Messages/UpdateMotion.cs` β€” extend parser - for MovementType=8 payload (target guid + final-heading flag). -- `src/AcDream.App/Rendering/GameWindow.cs` `OnLiveMotionUpdated` - β€” route MovementType=8 for the local player to a new - `BeginServerTurnToObject` controller method; route for remote - guids into the remote-dead-reckon state (extending - `RemoteMoveToDriver` or adding a sibling driver). -- `src/AcDream.App/Input/PlayerMovementController.cs` β€” add the - turn driver that holds Yaw against user-input overrides until - aligned. - -**Replaces / supersedes:** #65 (local-player turn-to-face on -close-range Use). This issue covers both directions and is the -broader retail-faithful rotation handling phase. - -**Estimated scope:** Medium β€” ~80–120 LOC + tests. - ---- - -## #65 β€” Local player doesn't turn to face target on close-range Use - -**Status:** OPEN -**Severity:** LOW (functional β€” Use still completes β€” but visually awkward) -**Filed:** 2026-05-15 (B.6/B.7 visual verification) -**Component:** physics / movement / inbound MoveTo handling - -**Description:** When the local player has a target selected and is -already within ACE's `WithinUseRadius` (close-range branch in -`CreateMoveToChain` at `Player_Move.cs:66`), ACE skips the auto-walk -chain and just calls `Rotate(target)` server-side. The Use action -completes, but the local player's body doesn't visibly turn to face -the target β€” the character stays at whatever heading the user was -looking when they clicked. - -**User-visible:** Stand behind an NPC, click them, press R. Dialogue -appears, but the character keeps facing away from the NPC. In retail -the character would have turned to face the NPC before / during the -Use. - -**Root cause:** ACE's close-range path sends a `TurnTo` motion -(MovementType=8 TurnToObject, decomp `0x005241b3` switch case 8). -Our `OnLiveMotionUpdated` doesn't currently handle MovementType=8 β€” -it falls into the locomotion path and ignores the rotation. - -**Acceptance:** When the user uses an in-range target while facing -away, the character rotates to face the target before / as the Use -action fires. No regression on close-range pickup (item still picks -up cleanly). - -**Files (likely):** -- `src/AcDream.Core.Net/Messages/UpdateMotion.cs` β€” extend parser for MovementType=8 TurnToObject payload. -- `src/AcDream.App/Input/PlayerMovementController.cs` β€” add a `BeginServerTurnToObject(targetWorld, useFinalHeading)` method that rotates Yaw at TurnRateRadPerSec each frame until aligned, then clears the state. -- `src/AcDream.App/Rendering/GameWindow.cs` `OnLiveMotionUpdated` β€” when inbound motion is MovementType=8 and the guid is `_playerServerGuid`, install the turn on the controller. - -**Estimated scope:** Small β€” ~50 LOC plus tests. Pairs naturally with -B.6 (already does turn-then-walk for far targets via RemoteMoveToDriver's -heading correction; this is the close-range cousin). - ---- - -## #64 β€” Local-player pickup animation does not render - -**Status:** OPEN -**Severity:** LOW (visual feedback only β€” pickup completes correctly) -**Filed:** 2026-05-14 (B.5 visual verification) -**Component:** motion / animation routing for local player - -**Description:** When `+Acdream` picks up an item (B.5 close-range -path), retail observers see the character play the pickup animation -correctly, but the local view shows no pickup animation. The item -despawns, the inventory updates, but the character's own -bend-down-and-grab animation is missing. - -**Root cause / hypothesis:** ACE broadcasts `Motion(MotionCommand.Pickup)` -via `Player_Inventory.AddPickupChainToMoveToChain` (line 711–713, -`EnqueueBroadcastMotion(motion)`), which arrives as a normal -`UpdateMotion (0xF74D)` packet. Retail observers route it through -their remote-creature animation pipeline and render the pickup. For -the local player, our `OnLiveMotionUpdated` likely filters self-echoes -(local player drives its own motion via prediction, not server -echoes) and drops the pickup motion. The pickup is a one-shot -animation initiated by the server, so the prediction path has no -trigger β€” and the echo path is filtered. - -**Acceptance:** When `+Acdream` picks up an item, the local view shows -the same pickup animation retail observers see. Probably resolved by -either (a) admitting server-initiated one-shot motions through the -local-player motion filter, or (b) generating the pickup animation -locally on send (mirroring retail's client behavior). - -**Files:** `src/AcDream.App/Rendering/GameWindow.cs` `OnLiveMotionUpdated` -(motion routing); the self-echo filter is somewhere along this path. - -**Estimated scope:** Small-to-medium. Mostly investigation + -1–2 commits. - ---- - -## #63 β€” Server-initiated auto-walk (MoveToObject) not honored - -**Status:** OPEN -**Severity:** MEDIUM (blocks out-of-range Use + Pickup; close-range -works fine) -**Filed:** 2026-05-14 (B.5 visual verification) -**Component:** motion / inbound MoveToObject handling - -**Description:** When the player triggers a Use or PutItemInContainer -on a target outside ACE's `WithinUseRadius` (default 0.6 m), ACE -runs server-side auto-walk via `CreateMoveToChain` β†’ -`PhysicsObj.MoveToObject` + `EnqueueBroadcastMotion(Motion(MoveToObject, target))`. -Our client receives the `UpdateMotion(MoveToObject)` broadcast for -the player but doesn't honor it: the character either visually -drifts a bit toward the target and snaps back, or just stands still. -ACE's MoveToChain then times out, the `success: false` path -broadcasts `InventoryServerSaveFailed (ActionCancelled)`, and the -pickup/use never completes. - -**User-visible symptom:** Double-click a ground item from any -distance, or F-key it from > 0.6 m: character partially walks toward -the item, then flips back to original position. No pickup. - -**Reference:** [holtburger simulation.rs:33–41 + 178–191](references/holtburger/crates/holtburger-core/src/client/simulation.rs) -already implements client-side `MoveToObject` motion projection + -auto-walk handling. That's the shape of the fix. - -**Root cause:** Our `OnLiveMotionUpdated` has no handler for the -`MoveToObject` motion type; the broadcast is silently dropped. - -**Acceptance:** Double-click a ground item from 2–5 m away. Character -auto-walks to within use radius, ACE's MoveToChain confirms success, -pickup completes (including the existing PickupEvent despawn). Same -behavior for Use on out-of-range NPCs. - -**Files:** `src/AcDream.App/Rendering/GameWindow.cs` `OnLiveMotionUpdated` -(routing); likely a new `MoveToObjectMotion` handler in the motion / -prediction layer + a server-acked position-update echo so ACE sees the -player has reached the target. - -**Estimated scope:** Medium. Probably its own phase (B.6 or similar); -not a one-commit fix. Compose from holtburger's pattern. - ---- - -## #62 β€” [DONE 2026-05-14 Β· `ec9fd52`] PARTSDIAG null-guard for sequencer-driven entities - -**Status:** DONE -**Severity:** LOW (latent crash; not reachable for doors today β€” see notes) -**Filed:** 2026-05-13 (code-quality review of B.4c Task 1) -**Component:** diagnostic / `GameWindow.TickAnimations` PARTSDIAG block - -**Description:** The PARTSDIAG block at `GameWindow.cs:7657` reads -`ae.Animation.PartFrames.Count` without a null-guard. B.4c introduced -`Animation = null!` for sequencer-driven door entities (per the same -pattern at line 7857). Today this is safe: doors never enter -`_remoteDeadReckon` (ACE never sends UpdatePosition for them), and -`_remoteDeadReckon` membership is one of the outer guards on the -PARTSDIAG block. The diagnostic never fires for doors. - -**Risk:** Future code that admits more non-creature entities via the -B.4c branch β€” or extends ACE to send UpdatePosition for doors β€” would -make `_remoteDeadReckon` membership reachable for null-Animation -entities. The next time someone enables `ACDREAM_REMOTE_VEL_DIAG=1` -and that scenario occurs, the diagnostic crashes the tick. - -**Acceptance:** PARTSDIAG block tolerates null `ae.Animation`. One-line -fix: -```csharp -int animFrame0Parts = ae.Animation?.PartFrames.Count > 0 - ? ae.Animation.PartFrames[0].Frames.Count - : -1; -``` - -**Files:** `src/AcDream.App/Rendering/GameWindow.cs:7657` (one-line null-coalescing change). - -**Estimated scope:** Trivial. One-line edit + a build verification. - ---- - -## #61 β€” AnimationSequencer linkβ†’cycle boundary flash on one-shot motion (door swing) - -**Status:** OPEN -**Severity:** LOW (visual polish β€” animation works, brief one-frame flash through prior pose at end of swing) -**Filed:** 2026-05-13 (visual test of B.4c) -**Component:** animation / `AcDream.Core.Physics.AnimationSequencer` link+cycle transition - -**Description:** When a door receives `UpdateMotion(NonCombat, On)` via the -B.4c spawn-time-registered sequencer, the swing-open animation plays -correctly but exhibits a brief one-frame flash through the closed pose -at the END of the swing before settling at the open pose. Same flash on -close (settles at closed pose after one-frame flash through open). - -**Root cause hypothesis:** `AnimationSequencer.SetCycle` enqueues a -transition link (the swing motion) followed by the target cycle (likely -a single-frame static rest pose). If the link's last frame and the -cycle's frame 0 don't match exactly, the renderer reads one frame of -the cycle's start pose before the cycle's natural rest. Cumulative -effect: link plays Closedβ†’Open over N frames β†’ cycle's frame 0 is -Closed β†’ cycle resets to frame 0 for one render β†’ cycle advances to -its single rest frame which IS the open pose. Visible as a flap. - -**Acceptance:** Door open / close cycles play cleanly with no closed/open -pose flash at the linkβ†’cycle transition. Test: in Holtburg, double-click -inn door, watch swing animation rest at open pose with no intermediate flash. - -**Files (likely):** -- `src/AcDream.Core/Physics/AnimationSequencer.cs` β€” link+cycle queue boundary handling -- (read the link node's last-frame extraction + the cycle's frame-0 evaluation) - -**Estimated scope:** Moderate. Requires understanding the sequencer's link-vs-cycle queue semantics and possibly the underlying MotionTable's cycle data shape for doors. Could be a one-line fix (e.g. "preserve last link frame as cycle rest pose") or a deeper sequencer behavior change. - -**Workaround:** None needed for M1 β€” the flash is brief enough that doors are usable. - ---- - -## #60 β€” `obstruction_ethereal` retail downstream path not ported (M2 combat-HUD impact) - -**Status:** OPEN -**Severity:** LOW for M1 (no observable defect); MEDIUM for M2 (combat contact reporting on ethereal creatures will be wrong) -**Filed:** 2026-05-13 (final-review surfaced from B.4b) -**Component:** physics / `CollisionExemption.ShouldSkip` + downstream movement contact handling - -**Description:** B.4b's L.2g slice 1b widened `CollisionExemption.ShouldSkip` to exempt -on `ETHEREAL_PS` alone (cite `src/AcDream.Core/Physics/CollisionExemption.cs:62-79`). Retail's -`acclient_2013_pseudo_c.txt:276782` requires both `ETHEREAL_PS && IGNORE_COLLISIONS_PS` to wrap -the entire `FindObjCollisions` body β€” ETHEREAL alone takes the deeper path at line 276795 which -sets `sphere_path.obstruction_ethereal = 1` and lets downstream movement allow passage WHILE -STILL REPORTING THE CONTACT. We do not port that downstream path; we just exempt entirely. - -**M2 impact:** Combat HUD work that relies on physics-contact reporting for ethereal creatures -(ghosts, partially-phased monsters, spell projectiles with ETHEREAL set) will see no contact at -all instead of "soft contact with obstruction_ethereal=1". The user will not be able to target -or interact with such entities via the contact path. - -**Acceptance:** Port the retail deeper path so `obstruction_ethereal=1` flows through movement + -collision-reporting layers. Tests should cover: ETHEREAL creature target β†’ contact reported but -passage allowed; ETHEREAL+IGNORE_COLLISIONS target (door, retail-style) β†’ full exempt. - -**Estimated scope:** Moderate. Touches `CollisionExemption.cs`, transition/movement layer, and -sphere-path state propagation. Visible test through a spawned ethereal creature in ACE. - ---- - -## #59 β€” [DONE 2026-05-15 Β· `5e29773`] `WorldPicker` 5m fixed-radius could over-pick at tight thresholds (M1-deferred polish) - -**Status:** OPEN -**Severity:** LOW (cosmetic β€” picker grabs the right entity in Holtburg-tested scenarios) -**Filed:** 2026-05-13 (final-review surfaced from B.4b) -**Component:** selection / `AcDream.Core.Selection.WorldPicker.Pick` - -**Description:** `WorldPicker.Pick` uses a hardcoded 5m sphere around every candidate's -`Position` regardless of the entity's actual size (`src/AcDream.Core/Selection/WorldPicker.cs:82`). -This matches `WorldEntity.DefaultAabbRadius` and is sufficient for M1 acceptance: in tight -doorways, every server-keyed candidate has correct sphere coverage and the closest-wins logic -plus `ServerGuid==0` skip filter the wrong picks. But the invariant "non-clickable geometry has -`ServerGuid==0`" is load-bearing β€” if L.2d ever ports `CBuildingObj` as a server-keyed entity, -the picker may mis-target buildings. Per-entity `Setup.Radius` would be tighter. - -**Acceptance:** Either (a) tighten picker to read per-entity Setup.Radius / CylSphere bounds, -or (b) document the invariant in `WorldPicker.cs` and add a regression test asserting -`ServerGuid==0` entities never reach the per-candidate hit test. - -**Estimated scope:** Quick (~1 hour) β€” wire `Setup.Radius` lookup into the picker and update -the 6 existing picker tests with realistic radii. - ---- - -## #58 β€” [DONE 2026-05-13] Door swing animation: UpdateMotion not wired for non-creature entities - -**Status:** DONE -**Closed:** 2026-05-13 -**Severity:** MEDIUM (was M1 demo cosmetic β€” doors functioned but didn't visually animate) -**Filed:** 2026-05-13 -**Component:** animation / `UpdateMotion (0xF74D)` routing for non-creature entities - -**Closure:** Closed by Phase B.4c on branch `claude/phase-b4c-door-anim` -(4 implementation commits). The complete animation round-trip for door entities -is now wired and visual-verified at the Holtburg inn doorway: double-click a -closed door β†’ swing-open animation plays β†’ player walks through β†’ ~30s later -ACE broadcasts `UpdateMotion (NonCombat, Off)` β†’ swing-close animation plays. - -Implementation: spawn-time `AnimationSequencer` registration for door entities -in `GameWindow.OnLiveEntitySpawnedLocked` (Task 1, commit `9053860`), with -initial state seeded from `spawn.PhysicsState` so closed doors initialize to -the `Off` cycle and open doors initialize to the `On` cycle. A `[door-cycle]` -diagnostic line in `OnLiveMotionUpdated` (Task 2, commit `b89f004`) confirms -each `UpdateMotion` is processed. A shared `IsDoorName` predicate (Task 2 -review, commit `8a9b15e`) eliminates duplication. A stance-value fix (bonus, -commit `454d88e`) corrected `NonCombat = 0x3D` (not `0x01`), which was causing -doors to render halfway underground due to empty sequencer frames. - -Two follow-up items were filed: issue #61 (linkβ†’cycle boundary flash β€” brief -visual flap at end of swing animation; low severity) and issue #62 (PARTSDIAG -null-guard for sequencer-driven entities; latent, not currently reachable). - -See [`docs/research/2026-05-13-b4c-shipped-handoff.md`](research/2026-05-13-b4c-shipped-handoff.md) -for the full evidence trail, log output, and bonus-discovery narrative. M1 -demo target "open the inn door" now has full visual feedback. - -**Files (what shipped):** -- `src/AcDream.App/Rendering/GameWindow.cs` β€” `IsDoorSpawn` / `IsDoorName` helpers, spawn-time `AnimationSequencer` registration branch in `OnLiveEntitySpawnedLocked`, `_doorSequencers` dict, `[door-cycle]` diagnostic in `OnLiveMotionUpdated`, `TickAnimations` loop extended to advance door sequencers. -- `src/AcDream.Core/Physics/AnimationSequencer.cs` β€” no changes required; existing link+cycle API was sufficient. - ---- - -## #57 β€” [DONE 2026-05-13] B.4 interaction-handler missing: clicking on doors / NPCs / items silently does nothing - -**Status:** DONE -**Closed:** 2026-05-13 -**Severity:** HIGH (was M1 blocker) -**Filed:** 2026-05-12 -**Component:** input / interaction / `GameWindow.OnInputAction` - -**Closure:** Closed by Phase B.4b on branch `claude/compassionate-wilson-23ff99` -(9 implementation commits, Tasks 1-4 per plan + 4 bonus fixes). The -full round-trip β€” double-click door β†’ `WorldPicker.BuildRay` + `Pick` β†’ -`InteractRequests.BuildUse` β†’ ACE `SetState` reply β†’ `ShadowObjectRegistry` -mutation (via fixed ServerGuidβ†’entity.Id translation) β†’ `CollisionExemption.ShouldSkip` -exempts (widened to ETHEREAL-alone) β†’ player walks through β€” was -visual-verified at the Holtburg inn doorway 2026-05-13. Four bonus -discoveries were required beyond the original plan: (1) `InputDispatcher` -had no double-click detection, (2) `OnInputAction` gate blocked -`DoubleClick` activations, (3) `CollisionExemption` required both -ETHEREAL+IGNORE_COLLISIONS while ACE sends only ETHEREAL, (4) -`OnLiveStateUpdated` passed server GUID to a local-entity-ID-keyed -registry. M1 demo target "open the inn door" met. See -[docs/research/2026-05-13-b4b-shipped-handoff.md](research/2026-05-13-b4b-shipped-handoff.md) -for full evidence and rationale. - -**Files (what shipped):** -- `src/AcDream.Core/Selection/WorldPicker.cs` (new; formerly zero callers, now wired) -- `src/AcDream.App/Rendering/GameWindow.cs` β€” `OnInputAction` switch cases for `SelectLeft` / `SelectDblLeft` / `UseSelected`; `OnLiveStateUpdated` ServerGuidβ†’Id translation; `_entitiesByServerGuid` reverse-lookup dict -- `src/AcDream.UI.Abstractions/Input/InputDispatcher.cs` β€” double-click detection -- `src/AcDream.Core/Physics/CollisionExemption.cs` β€” widened to ETHEREAL-alone - ---- - -## #55 β€” Static-entity slow path reports ~1.45M `meshMissing` per 5s at r4 standstill - -**Status:** OPEN -**Severity:** LOW (no visible regression β€” affects a diagnostic counter, not rendered output) -**Filed:** 2026-05-11 -**Component:** rendering / `WbDrawDispatcher` static-entity classification path - -**Description:** During the Phase N.6 slice 1 baseline measurement (`docs/plans/2026-05-11-phase-n6-perf-baseline.md` Β§2), -the radius=4 standstill scenario reported `meshMissing β‰ˆ 1,450,000` per 5-second -`[WB-DIAG]` window. The same scenario while walking drops to near-zero (`meshMissing = 0` -in the steady state) as new landblocks stream in and previously-missing meshes resolve. -This suggests the static-entity slow path's mesh-load lifecycle has some delay before -populating for newly-streamed content but eventually catches up; the standstill case -keeps re-counting the same set of entities-with-unresolved-meshes for the duration of -the run. The counter is per-frame so the absolute number scales with FPS β€” at the -measured ~150 FPS that's ~290K reports/s, or ~1900 entities each reported each frame. - -**Root cause / status:** Not investigated. Hypothesis: an entity classification path -counts mesh-missing on every frame for static entities whose `MeshRef` resolution races -the streaming loader. The Tier 1 cache (#53) populates only for entities whose -classification succeeded, so persistently-failing entities run the slow path every frame -forever and bump `meshMissing` every time. If true, the fix is either (a) cache the -"this entity's mesh genuinely doesn't exist" result so we stop re-checking, or (b) -deferred-classify the entity once its `MeshRef` resolves. - -**Files:** `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` (the slow path that -increments `_meshesMissing`), `src/AcDream.App/Rendering/Wb/EntityClassificationCache.cs` -(the Tier 1 cache β€” likely needs to learn about "permanently missing" entries). - -**Acceptance:** `meshMissing` should drop to near-zero within ~5 seconds of streaming -settle at any radius/motion combination, not stay at ~1.45M/5s indefinitely at standstill. - ---- - -## #50 β€” [DONE 2026-05-11 Β· accepted WB divergence] Road-edge tree at 0xA9B1 visible in acdream but not retail - -**Status:** DONE -**Closed:** 2026-05-11 -**Severity:** LOW (cosmetic; one spawned tree near the road in Holtburg) -**Filed:** 2026-05-08 -**Component:** scenery placement / Phase N (WorldBuilder rendering migration) - -**Resolution:** Same disposition as #49 β€” accepted as WB-upstream -divergence from retail. The earlier fix attempt (`e279c46`, ACME-style -per-vertex road check) successfully removed this specific tree but -over-suppressed scenery elsewhere; revert at `677a726` stood. Without -a coherent port of ACME's full per-vertex filter set, piecemeal -patching is net-negative. Left as a documented WB divergence. - ---- - -**Original investigation (kept for reference):** - -**Description:** With `ACDREAM_USE_WB_SCENERY=1` (default since commit `b84ecbd`), -a tree at landblock 0xA9B1 around `(lx=85.08, ly=190.97)` appears in acdream but -neither retail nor ACME WorldBuilder render it. Upstream Chorizite/WorldBuilder -DOES render it, so our migration to WB's helpers (Phase N.1) inherited this -discrepancy from upstream. - -**Root cause (suspected):** ACME WorldBuilder includes a per-vertex road check that -skips the entire vertex when its road bit is set (see -`references/WorldBuilder-ACME-Edition/WorldBuilder/Editors/Landscape/GameScene.cs:1074`). -The current vertex (4,8) has a road bit set in the dat. ACME skips it; -Chorizite/WorldBuilder doesn't; we don't. - -**Fix attempt that didn't work:** commit `e279c46` added the per-vertex road check -directly to our `GenerateViaWb` (and legacy `Generate` for parity). It successfully -removed the offending tree but over-suppressed scenery in other landblocks (visual -regressions during user testing). Reverted in commit `677a726`. ACME's check likely -interacts with other factors (per-vertex building check, or something else in ACME's -pipeline) that we'd need to port together, not the road check alone. - -**Next steps:** -1. Investigate ACME's full per-vertex filter set (road + building + anything else) - and port them as a coherent unit, not piecemeal. -2. OR upstream the per-vertex road check to Chorizite/WorldBuilder (which is now our - submodule fork) so it lands as a generic ACME-conformance improvement. -3. OR consider switching fork target from Chorizite/WorldBuilder to ACME WorldBuilder - for future phases (N.2+). - -Visually undetectable to most users; one extra tree at one landblock. Defer until -other Phase N work catches a similar issue and a coherent fix becomes obvious. - -**Files:** -- `src/AcDream.Core/World/SceneryGenerator.cs` β€” `GenerateInternal` is the active path -- `src/AcDream.Core/World/WbSceneryAdapter.cs` β€” adapter used by `GenerateInternal` -- `references/WorldBuilder-ACME-Edition/WorldBuilder/Editors/Landscape/GameScene.cs:1074` β€” ACME's per-vertex road filter - ---- - -## #49 β€” [DONE 2026-05-11 Β· accepted WB divergence] Scenery (X, Y) placement drifts from retail at some landblocks - -**Status:** DONE -**Closed:** 2026-05-11 -**Severity:** LOW (minor cosmetic placement difference) +**Severity:** MEDIUM (visible misplacement; species-specific or per-cell, not a global offset) **Filed:** 2026-05-06 **Component:** scenery placement / `SceneryGenerator` -**Resolution:** Accepted as WB-upstream divergence from retail. Since -the N.1 phase (WorldBuilder-backed scenery, see roadmap), acdream -defers scenery placement math to the WB fork; retail and WB diverge -slightly here on some landblocks. Piecemeal patching against WB -upstream would create a maintenance burden disproportionate to the -visible impact (a handful of trees positioned a few meters off across -the world). Left as-is; revisit only if WB upstream patches the -divergence or if a coherent ACME-style filter port (see issue body -below) becomes worthwhile. - -The original investigation plan (cdb trace of retail's -`CLandBlock::get_land_scenes` for diff against acdream's -`SceneryGenerator` output) is preserved below for historical -reference if anyone picks this up. - ---- - -**Original investigation (kept for reference):** - **Description:** While verifying the `#48` Z fix at Holtburg landblock `0xA9B30001`, the user spotted a scenery tree placed at the **wrong (X, Y)** in acdream relative to retail at the same @@ -809,8 +253,8 @@ regression on the species that already render correctly. ## #39 β€” Run↔Walk cycle transition not visible on observed player remotes (acdream-as-observer) -**Status:** OPEN β€” VERIFY-PENDING (cases #1/#2/#4/#5 user-verified working 2026-05-06; cases #3/#6/#7 unverified in live test) -**Severity:** LOW (most cases now visibly correct after the 2026-05-06 fix sequence; remaining unverified cases are direction-flip β€” believed to work via direct UM but not explicitly exercised) +**Status:** OPEN +**Severity:** MEDIUM (visible animation desync; not a correctness/wire bug) **Filed:** 2026-05-03 **Component:** physics / motion / animation @@ -1125,7 +569,6 @@ the local terrain normal, not the actor's facing. **Severity:** LOW (within retail's own DesiredDistance / MinDistance tolerances; visible only on close inspection) **Filed:** 2026-05-05 **Component:** physics / motion / animation (per-tick remote prediction) -**Phase:** L.2 (Movement & Collision Conformance) β€” inbound-motion fidelity sub-piece. Blocked on cdb-trace of `CSequence::velocity` for Humanoid running cycle, then porting `add_motion @ 0x005224b0`'s `style_speed Γ— MotionData.velocity` chain. **Description:** With the L.3 M3 path live (queue catch-up + animation root motion fallback), observed player remotes chase server position @@ -1374,35 +817,13 @@ collision fixes.) still resolve correctly) - Observer view from a parallel retail client unchanged -## #37 β€” [DONE 2026-05-11 Β· resolved by `0bd9b96`] Humanoid coat doesn't extend up to neck (visible "skin stub" between hair and coat) +## #37 β€” Humanoid coat doesn't extend up to neck (visible "skin stub" between hair and coat) -**Status:** DONE -**Closed:** 2026-05-11 -**Commit:** `0bd9b96` (the #47 humanoid degrade-resolver fix, 2026-05-06) +**Status:** OPEN **Severity:** LOW (cosmetic; doesn't affect gameplay) **Filed:** 2026-05-01 **Component:** rendering / clothing / textures -**Resolution:** Closed by the same mesh-fidelity work that resolved #47. -The `GfxObjDegradeResolver` (commit `0bd9b96`, 2026-05-06) swapped -humanoid parts to their higher-detail `Degrade[0].Id` meshes (e.g. -upper arm `0x01000055 β†’ 0x01001795`, lower arm `0x01000056 β†’ 0x0100178F`). -The higher-detail meshes include the coat-collar polygons that the -low-detail meshes were missing β€” which is what was exposing the -skin-toned palette indices in the upper-coat region. With the -correct mesh resolution, those polygons cover the previously-visible -"skin stub". User confirmed visually 2026-05-11. - -The original 2026-05-01/2026-05-04 investigation work (palette range -analysis, SubPalette overlay tracing) is preserved below for -historical reference; it was a correct read of *what* was rendering, -but the root cause was the missing collar polygons, not the palette -gap. - ---- - -**Original investigation (kept for reference):** - **Description:** Every humanoid character (player + NPCs) wearing a coat shows a visible skin-colored region at the top of the coat where retail shows continuous coat fabric. From the back view: hair β†’ skin stub β†’ @@ -1476,8 +897,8 @@ If the coat texture's UVs at the upper region map to texel-bytes whose palette i **Files (diagnostic env vars committed for next-session reuse):** -- ~~`src/AcDream.App/Rendering/InstancedMeshRenderer.cs:210-275` - β€” `ACDREAM_NO_CULL` env var~~ (file deleted in N.5 ship amendment) +- `src/AcDream.App/Rendering/InstancedMeshRenderer.cs:210-275` + β€” `ACDREAM_NO_CULL` env var - `src/AcDream.App/Rendering/GameWindow.cs` β€” `ACDREAM_HIDE_PART=N` hides specific humanoid part; `ACDREAM_DUMP_CLOTHING=1` dumps AnimPartChanges + TextureChanges + per-part Surface chain coverage. @@ -1746,29 +1167,13 @@ in under 5 minutes by following the CLAUDE.md workflow. --- -## #36 β€” [DONE 2026-05-11 Β· promoted to Phase C.1.5c] Sky-PES dispatch port (consolidates #2 / #28 / #29 visual gaps) +## #36 β€” Sky-PES dispatch port (consolidates #2 / #28 / #29 visual gaps) -**Status:** DONE (promoted to Phase C.1.5c) -**Closed:** 2026-05-11 -**Promoted to:** Phase C.1.5c (Sky-PES dispatch chain) β€” see roadmap `docs/plans/2026-04-11-roadmap.md` +**Status:** OPEN **Severity:** MEDIUM (aesthetic feature-parity, but addresses a cluster of bugs) **Filed:** 2026-04-30 **Component:** sky / weather / particles -**Resolution:** Promoted to a roadmap phase (C.1.5c) β€” the work is -multi-commit (decomp dive + persistent-emitter creation + PES timeline -driver + PES script execution + live-trace verification) and warrants -a named phase rather than living forever as an "open issue." The -decomp anchors, live-trace evidence (24,576-frame `GameSky::Draw` -trace), and 6-step implementation outline in the body below remain -the authoritative implementation reference; the roadmap phase entry -is the schedule/scope tracker. **Issues #2 (lightning), #28 (aurora), -and #29 (cloud thinness) auto-close when C.1.5c ships.** - ---- - -**Original investigation (kept as implementation reference):** - **Description:** Three open sky bugs (#2 lightning, #28 aurora, #29 cloud density) all trace back to the same missing infrastructure: retail's sky-PES (Particle Effect Script) dispatch chain. We have it now from a @@ -1913,7 +1318,6 @@ one live creature case no longer use the single-cylinder fallback. **Severity:** MEDIUM **Filed:** 2026-04-25 **Component:** net / sky -**Chore tag:** Single-commit fix β€” well-scoped ~10-line wiring. `WorldTimeService.SyncFromServer(double)` already exists; just needs `WorldSession` to detect header-flag `0x1000000` and call it. Pickup at any opportunistic session. **Description:** Our `WorldTimeService.DayFraction` syncs with the server once at login via `ConnectRequest + TimeSync`, then advances from the local wall-clock. Retail receives periodic `TimeSync` refreshes (header flag `0x1000000`) carrying a fresh `PortalYearTicks double` and re-anchors its clock. Without those, acdream's keyframe state drifts from retail's over 10+ minutes β€” observed during the 2026-04-24 sky-color debug sessions where retail was at DayFraction 0.976 while acdream was at 0.634. @@ -1929,6 +1333,29 @@ one live creature case no longer use the single-cylinder fallback. --- +--- + +## #13 β€” PlayerDescription trailer past enchantments (options / shortcuts / hotbars / desired_comps / spellbook_filters / options2 / gameplay_options / inventory / equipped) + +**Status:** OPEN +**Severity:** LOW (no current user-visible bug; future panels will need the data) +**Filed:** 2026-04-25 +**Component:** net / player-state + +**Description:** `PlayerDescriptionParser` walks through enchantments (Phase H, 2026-04-25). The trailer beyond that β€” Options1 / Shortcuts / HotbarSpells (8 lists) / DesiredComps / SpellbookFilters / Options2 / GameplayOptions blob / Inventory / Equipped β€” is not yet parsed. Required for future Spellbook UI panel, hotbar UI, inventory UI, character options panel. + +**Root cause / status:** Holtburger `events.rs:462-625` has the full layout. The trickiest piece is `gameplay_options` β€” a variable-length opaque blob; holtburger uses a heuristic forward search (`find_inventory_start_after_gameplay_options`) for plausibly-aligned inventory-count + GUID pairs to find the inventory start. Other sections are well-formed. + +**Files:** +- `src/AcDream.Core.Net/Messages/PlayerDescriptionParser.cs` β€” extend `Parsed` record + walker. +- `tests/AcDream.Core.Net.Tests/PlayerDescriptionParserTests.cs` β€” add fixtures per section. +- `src/AcDream.Core.Net/GameEventWiring.cs` β€” route `parsed.Inventory` + `Equipped` to ItemRepository. + +**Research:** holtburger `events.rs:462-625`; `references/actestclient/TestClient/messages.xml`. + +**Acceptance:** All sections of a real-world PlayerDescription parse to completion (no truncation). New tests cover synthetic fixtures per section. `ItemRepository.Count` after login > 0. + +--- --- @@ -1943,8 +1370,6 @@ one live creature case no longer use the single-cylinder fallback. **Root cause / status:** Three competing hypotheses, none pinned down: (a) retail uses a **different** fog range for sky than terrain; (b) retail applies fog with an **elevation-angle** weighting rather than linear distance; (c) retail's sky meshes **don't participate** in the global fog and the "horizon glow" comes from a different atmospheric-scatter path. Need to identify retail's actual sky-fog behaviour before re-enabling with correct parameters. -**Not in the Phase C.1.5c (Sky-PES) cluster.** Unlike #2/#28/#29 β€” all PES-driven sky visuals consolidated under the C.1.5c phase via former issue #36 β€” this is a fragment-shader fog-mix problem. Addressing C.1.5c will NOT resolve #4, and #4 should NOT be bundled into Phase C.1.5c scope. The fix likely needs its own decomp dive into retail's sky-fog math + shader work. - **Files:** - `src/AcDream.App/Rendering/Shaders/sky.frag` β€” line ~55, `rgb = mix(uFogColor.rgb, rgb, vFogFactor)` currently commented out - `src/AcDream.App/Rendering/Shaders/sky.vert` β€” lines 109-114, `vFogFactor` computation @@ -2175,7 +1600,6 @@ retail show matching silhouette and shape definition. **Severity:** MEDIUM (degrades external perception of acdream-driven characters) **Filed:** 2026-05-06 **Component:** net / motion (acdream's outbound path: `PlayerMovementController` β†’ `MoveToState` (0xF61C) / `AutonomousPosition` heartbeat β†’ ACE β†’ retail observer) -**Phase:** L.2 (Movement & Collision Conformance) β€” outbound-motion fidelity sub-piece. Counterpart to #41 (which is the inbound side); both are L.2 conformance work. If outbound fidelity grows into multi-commit work, consider carving "L.2e β€” Outbound motion fidelity" as a named sub-piece on the roadmap. **Description:** When viewing acdream's local +Acdream character through a parallel retail acclient.exe, the retail observer sees the character's movement as visibly blippy and laggy β€” position appears to step in discrete jumps rather than translating smoothly. The local acdream view of the same character looks fine, and acdream observing a retail-driven character (after #39 / #45) also looks fine. The degradation is specifically on the **outbound** side: what acdream sends to ACE for relay to other clients. @@ -2232,216 +1656,6 @@ Unverified. The likely culprits, ranked by suspected probability: # Recently closed -## #56 β€” [DONE 2026-05-12 Β· 8735c39] `ParticleHookSink` ignores `CreateParticleHook.PartIndex`; multi-emitter scripts collapse to entity root - -**Closed:** 2026-05-12 -**Commit chain (newest first):** -- `8735c39` β€” feat(vfx #C.1.5b): GpuWorldState fires activator for dat-hydrated entities (4 new fire-sites + 5 integration tests; also picks up EnvCell statics & exterior stabs as a side-effect of the activator-guard relaxation) -- `5ca5827` β€” feat(vfx #C.1.5b): activator handles dat-hydrated entities + per-part transforms (resolver returns `ScriptActivationInfo(ScriptId, PartTransforms)`; keys by ServerGuid OR entity.Id; GameWindow resolver lambda upgraded; 4 existing + 3 new tests) -- `11521f4` β€” fix(vfx #56): `ParticleHookSink` applies `CreateParticleHook.PartIndex` transform (new `_partTransformsByEntity` side-table; `SpawnFromHook` transforms offset through `partTransforms[PartIndex]` before applying entity rotation; 2 new tests + 2 existing pass) -- `f3bc15e` β€” feat(vfx #C.1.5b): `SetupPartTransforms` helper for per-part anchor transforms (walks `PlacementFrames[Resting]` β†’ `[Default]` β†’ first-available; 4 tests) -- `1e3c33b` β€” docs(vfx #C.1.5b): design + plan for issue #56 + EnvCell DefaultScript - -**Component:** vfx / `ParticleHookSink` + `EntityScriptActivator` + `GpuWorldState` + `SetupPartTransforms` - -**Resolution.** Two-slice fix that also folded in slice 2 of the C.1.5 phase work. **Slice A (the #56 fix proper)**: precomputed per-part `Matrix4x4` array at activator-spawn time via the new `SetupPartTransforms.Compute(setup)` helper, threaded through `EntityScriptActivator` β†’ `ParticleHookSink.SetEntityPartTransforms(entityId, partTransforms)` (mirrors the existing `_rotationByEntity` side-table pattern), applied inside `SpawnFromHook` as `partLocal = Transform(offset, partTransforms[PartIndex])` before the existing world-rotation step. Backwards-compatible: entities without registered part transforms fall through to identity (pre-fix behavior). **Slice B (folded in same phase, makes the fix matter for slice 2 visual gates)**: dropped the activator's `ServerGuid==0` early-return guard. Activator now keys by `entity.ServerGuid` when non-zero, else `entity.Id` β€” collision-free because dat-hydrated entity IDs live in the `0x40xxxxxx` (interior) / `0x80xxxxxx` (scenery) / `0xC0xxxxxx` ranges, all disjoint from server guids. `GpuWorldState` fires the activator from 4 new sites: `AddLandblock` + `AddEntitiesToExistingLandblock` (Farβ†’Near promotion) for OnCreate, `RemoveLandblock` + `RemoveEntitiesFromLandblock` (Nearβ†’Far demotion) for OnRemove. Live entities are filtered out by `ServerGuid != 0` on the `AddLandblock` path so pending-bucket merges don't double-fire OnCreate. - -**Reality discovery folded into spec Β§3:** the handoff doc's Β§4 Q1/Q2 (synthetic-ID scheme + new walker class) were mooted by finding that `GameWindow.BuildInteriorEntitiesForStreaming` already hydrates EnvCell `StaticObjects` as `WorldEntity` instances with stable `entity.Id`. No new walker, no synthetic IDs. - -**Verification.** Build green. 77 Vfx+Meshing+Activator+Streaming tests pass (4 new for SetupPartTransforms + 2 new for ParticleHookSink + 4 updated + 3 new for activator + 5 new for GpuWorldState integration). 8 pre-existing Physics/Input failures unchanged (verified by stash-and-rerun on Task 4). **Visual verification 2026-05-12**: Holtburg Town network portal (entity `0x7A9B405B`, script `0x3300126D`) β€” swirl no longer ground-buried, emitters distributed across the arch; Holtburg Inn fireplace flames over the firebox; cottage chimney smoke; spell cast on `+Acdream` cast-anim particles β€” all match retail. - -**Acceptance reproducer:** the C.1.5a verification log captured portal A entity `0x7A9B405B` swirl compressed to a partly-ground-buried point. Post-fix at the same portal, the swirl extends through the arch in retail-matching shape. - -## #53 β€” [DONE 2026-05-11 Β· f928e66] A.5/tier1-redo: entity-classification cache retry - -**Closed:** 2026-05-11 -**Commit chain (newest first):** -- `f928e66` β€” incomplete-entity flag must persist across same-entity tuples (mid-list null-renderData) -- `c55acdc` β€” skip cache populate when classification is incomplete (drudge fix) -- `95ebbf3` β€” key cache by `(entityId, landblockHint)` tuple to defeat ID collision -- `71d0edc` β€” namespace stab Ids globally (`0xC0LLBB01..`) for Tier 1 cache safety -- `4df1914` β€” clarify `DebugCrossCheck`'s wiring status -- `f16604b` β€” DEBUG cross-check + tripwire + 2 tests -- `489174f` β€” wire `InvalidateLandblock` callback at LB demote/unload -- `1d1afcd` β€” wire `InvalidateEntity` at live-entity despawn -- `f7e38c2` β€” cache-hit fast path must fire per-entity, not per-tuple -- `0cbef3c` β€” cache-hit fast path + dispatcher integration tests -- `00fa8ae` β€” cache `Populate` must flush at entity boundary, not per-MeshRef tuple -- `2f489a8` β€” cache-miss populate on first frame for static entities -- `28513ea` β€” optional `CachedBatch` collector + `restPose` param on `ClassifyBatches` -- `a65a241` β€” inject `EntityClassificationCache` into `WbDrawDispatcher` -- `60fbfce` β€” plumb `landblockId` through `_walkScratch` -- `a171e70`, `aea4460`, `694815c`, `773e970` β€” cache `InvalidateLandblock` / `InvalidateEntity` / `Populate` / skeleton+first test -- `c02405c` β€” extract `GroupKey` to namespace-scope `internal` -- `2f8a574` β€” implementation plan -- `4abb838` β€” mutation audit + cache design spec - -**Component:** rendering / `WbDrawDispatcher` / `EntityClassificationCache` / `LandblockLoader` - -**Resolution.** New `EntityClassificationCache` keyed by `(entityId, landblockHint)` tuple in `src/AcDream.App/Rendering/Wb/EntityClassificationCache.cs`. The dispatcher routes static entities (NOT in `_animatedEntities`) through the cache β€” first-frame slow-path populates flat `CachedBatch[]` (one entry per (partIdx, batchIdx) with the part-relative `RestPose` and resolved `BindlessTextureHandle`); subsequent-frame cache hits skip classification entirely and append `cached.RestPose * entityWorld` to each matching group. Animated entities bypass. Invalidation fires from `RemoveLiveEntityByServerGuid` (per-entity, `0xF747`/`0xF625`) and `RemoveEntitiesFromLandblock` (per-LB, Nearβ†’Far demote + unload). - -**Perf result.** Entity dispatcher cpu_us **median ~1200 Β΅s, p95 ~1500 Β΅s** at horizon-safe + High preset on AMD Radeon RX 9070 XT @ 1440p. Pre-Tier-1 baseline was ~3500m / ~4000p95. ~66% reduction in median, ~63% in p95. Well under the A.5 spec budget (median ≀ 2.0 ms, p95 ≀ 2.5 ms). No `BUDGET_OVER` flag observed. - -**Verification.** Build green; full suite 1711 passed / 8 pre-existing physics/input failures unchanged; N.5b sentinel 112/112; visual gate confirmed via `+Acdream` test character (NPCs animate, lifestone renders, multi-part buildings + scenery + Nullified Statue of a Drudge on top of the Foundry all render fully β€” no airborne geometry, no Z-fighting, no missing parts, no wrong textures). - -**Lessons surfaced during implementation (4 bug-fix iterations):** - -1. **Audit must verify ID uniqueness for cache keys.** The original mutation audit verified `Position`/`Rotation`/`MeshRefs` stability post-spawn but didn't verify `entity.Id` was globally unique. Stabs from `LandblockLoader.BuildEntitiesFromInfo` restarted at `nextId = 1` per landblock β†’ cross-LB collisions. Scenery (`0x80LLBB00 + localIndex`) and interior (`0x40LLBB00 + localCounter`) overflow at >256 items/LB. Cache key collision produced "buildings up in the air with wrong textures." Fixed by namespacing stab Ids (`71d0edc`) then by changing cache key to `(entityId, landblockHint)` tuple (`95ebbf3`) β€” defensive against ALL future hydration paths. - -2. **Per-tuple iteration with per-entity cache state is a recurring trap.** Three separate bugs caught by code review or visual gate hit this same root cause: - - Populate fired per-tuple β†’ multi-MeshRef entities lost all but the last MeshRef's batches (`00fa8ae`). - - Cache hit fired per-tuple β†’ multi-MeshRef entities drew NΓ— copies, severe Z-fighting (`f7e38c2`). - - Incomplete-flag reset fired per-tuple β†’ mid-list null-MeshRef trees populated partial cache, branches never rendered (`f928e66`). - - The fix pattern in all three: track previous entity Id (`prevTupleEntityId` / `lastHitEntityId`); execute per-entity logic only on actual entity-change detected against that tracker, not unconditionally per tuple. - -3. **Async mesh loading interacts with cache populate.** WB's `ObjectMeshManager.PrepareMeshDataAsync` decodes meshes off the main thread. If a MeshRef's GfxObj is still decoding at first-frame visibility, `TryGetRenderData` returns null and the slow path skips it. Without the drudge fix (`c55acdc`), the cache populated a partial classification and cache hits served it forever β€” even after the missing mesh loaded. With the fix, the dispatcher tracks `currentEntityIncomplete` per entity and drops the populate scratch when any MeshRef returned null; the slow path retries every frame until all meshes load. - -4. **A/B diagnostic env-var paid for itself.** `ACDREAM_DISABLE_TIER1_CACHE=1` forces every static entity through the slow path. Used twice during debugging to instantly differentiate "bug is in the cache" vs "bug is elsewhere entirely." Kept in tree (read once in `WbDrawDispatcher` ctor) for future cache investigations. - -**Memory.** See `~/.claude/projects/C--Users-erikn-source-repos-acdream/memory/project_tier1_cache.md` for the audit-gap and per-tuple-vs-per-entity pattern documented for future cache work. - ---- - -## #54 β€” [DONE 2026-05-10 Β· bf31e59] A.5/jobkind-plumbing: far-tier worker loads full entity layer then strips - -**Closed:** 2026-05-10 -**Commits:** `bf31e59` (factory signature change to 2-arg + back-compat overload + far-tier early-out) -**Component:** streaming / LandblockStreamer - -**Resolution.** `LandblockStreamer.cs` primary ctor now takes `Func` so the factory can branch on the job kind. A back-compat overload preserves the old single-arg signature for existing test code (5 ctor sites in `LandblockStreamerTests.cs` resolved to the overload with no test changes). `BuildLandblockForStreaming(uint, JobKind)` in `GameWindow.cs` early-outs for `LoadFar` with a heightmap-only path (`_dats.Get(landblockId)` + `Array.Empty()`); near-tier path is unchanged. The Bug A post-load entity strip in `LandblockStreamer.HandleJob` is retained as a `Debug.Assert` + Release safety net. Per-LB worker cost on far-tier dropped from ~tens of ms (LandBlockInfo + scenery + interior) to ~sub-ms (single LandBlock dat read). - -**Verification.** Build green; 1688/1696 tests pass (8 pre-existing physics/input failures unchanged); 30 streaming-targeted tests (LandblockStreamer + StreamingController + StreamingRegion) all green via the back-compat overload. - ---- - -## #52 β€” [DONE 2026-05-10 Β· e40159f] A.5/lifestone-missing: Holtburg lifestone not rendering - -**Closed:** 2026-05-10 -**Commits:** `e40159f` (alpha-test discard removal + cull state restoration + uDrawIDOffset uniform) -**Component:** rendering / WbDrawDispatcher / shaders - -**Resolution.** Three independent root causes regressed with the WB rendering migration (Phase N.5 retirement amendment, commit `dcae2b6`, 2026-05-08). The original ISSUE #52 hypothesis (Bug A far-tier strip catching the lifestone) was wrong β€” the lifestone is server-spawned (WCID 509, Setup `0x020002EE`) and never goes through the far-tier strip. Real causes: - -1. **Alpha-test discard.** `mesh_modern.frag` transparent pass discarded fragments with `Ξ± >= 0.95`. The lifestone crystal core surface `0x080011DE` decoded with Ξ±β‰₯0.95 across its visible surface, so 100% of the crystal's fragments were discarded β€” invisible. The original N.5 Β§2 rationale ("high-Ξ± belongs in opaque pass") doesn't hold for surfaces dat-flagged transparent: those pixels can't reach the opaque pass at all. Fix: remove the high-Ξ± discard from the transparent pass; keep `Ξ± < 0.05` as a fragment-cost optimization. - -2. **Cull state regression.** Legacy `StaticMeshRenderer` had Phase 9.2's `Enable(CullFace) + Back + CCW` setup at the top of its translucent pass (commit `6f1971a`, 2026-04-11) β€” fix for "lifestone crystal one face missing" reported at the time. When `dcae2b6` deleted the legacy renderer, the new `WbDrawDispatcher` never inherited that GL state, so closed-shell translucents composited back-faces over front-faces in iteration order under `DepthMask(false)`. Fix: re-establish Phase 9.2's exact setup at the top of Phase 8. - -3. **`uDrawIDOffset` indexing bug.** `gl_DrawIDARB` resets to 0 at the start of each `glMultiDrawElementsIndirect` call. The transparent pass starts at byte offset `_opaqueDrawCount * stride` in the indirect buffer, but the vertex shader read `Batches[gl_DrawIDARB]` directly β€” so transparent draws read from `Batches[0..transparentCount)` (the OPAQUE section) instead of `Batches[opaqueCount..end)`. The lifestone crystal's apparent texture flickered to whatever opaque batch sorted to index 0 each frame; with the player character in view, this often appeared as a lifestone wearing the player's body / face textures. Fix: add `uniform int uDrawIDOffset` to `mesh_modern.vert`, change `Batches[gl_DrawIDARB]` to `Batches[uDrawIDOffset + gl_DrawIDARB]`, and set the uniform per-pass in `WbDrawDispatcher` (0 for opaque, `_opaqueDrawCount` for transparent). Mirrors WorldBuilder's `BaseObjectRenderManager.cs:845`. - -**Verification.** User-confirmed visually via `+Acdream` test character at the Holtburg outdoor lifestone (Z=94 platform). Tests 1688/1696 passing (8 pre-existing physics/input failures unchanged). N.5b conformance sentinel 94/94 clean. - -**Lesson.** The WB rendering migration's "lift legacy state into the new dispatcher" was incomplete in two non-obvious ways: (a) GL state setup that lived inside legacy per-pass blocks, and (b) shader uniforms that the legacy per-draw flow didn't need but the multi-draw-indirect flow does. Future WB-migration work should systematically diff the legacy renderer's GL setup + shader I/O against the new dispatcher's. The `uDrawIDOffset` bug was particularly hidden because it only manifested for entities that mixed transparent draws with the visible opaque sort order β€” single-pass content (pure opaque or pure transparent) was unaffected. - ---- - -## #13 β€” [DONE 2026-05-10 Β· d3b58c9..078919c] PlayerDescription trailer past enchantments - -**Closed:** 2026-05-10 -**Commits:** `d3b58c9` (scaffold) β†’ `6587034` (rename nit) β†’ `becbde6` (OptionFlags+Options1) β†’ `9a0dfe0` (TrailerTruncated + diag) β†’ `f7a5eea` (Shortcuts) β†’ `8cbb991` (HotbarSpells) β†’ `75e8e26` (DesiredComps) β†’ `b17dc3b` (SpellbookFilters) β†’ `98eebef` (Options2) β†’ `d9a5e40` (strict Inventory+Equipped) β†’ `91693ea` (heuristic GAMEPLAY_OPTIONS walker) β†’ `58095d8` (combined fixture test) β†’ `078919c` (ItemRepository wiring) -**Component:** net / player-state -**Plan:** [`docs/superpowers/plans/2026-05-10-issue-13-pd-trailer.md`](../docs/superpowers/plans/2026-05-10-issue-13-pd-trailer.md) - -**Resolution.** `PlayerDescriptionParser` now walks every trailer -section through Inventory + Equipped, ported faithfully from holtburger -`events.rs:503-625` + `shortcuts.rs:13-34`. The trickiest piece β€” -`gameplay_options` β€” uses a 4-byte-aligned forward heuristic -(`TryHeuristicInventoryStart`) that probes candidate offsets with a -strict `(inventory + equipped consume to EOF)` test, mirroring -holtburger's `find_inventory_start_after_gameplay_options`. - -The trailer walk is wrapped in its own inner try/catch (separate from -the outer parse-wide catch) so a malformed trailer cannot destroy the -already-extracted attribute / skill / spell / enchantment data. A new -`Parsed.TrailerTruncated` flag lets callers distinguish a clean parse -from a graceful-degradation parse (set true if the inner catch fires; -log under `ACDREAM_DUMP_VITALS=1`). - -`GameEventWiring`'s `PlayerDescription` handler now registers each -inventory entry with `ItemRepository.AddOrUpdate(...)` and applies -`MoveItem(...)` for equipped entries so paperdoll picks up -`CurrentlyEquippedLocation` at login. The acceptance criterion -"`ItemRepository.Count` after login > 0" is now exercised by -`PlayerDescription_RegistersInventoryEntries_InItemRepository` in -`GameEventWiringTests`. - -12 tasks, 13 commits, +9 PD parser tests + 1 wiring test (20 PD tests -total, 282 Net.Tests pass). Code-review nits during the run produced -two refactor commits: `Shortcut β†’ ShortcutEntry` rename to avoid a -homograph with the `CharacterOptionDataFlag.Shortcut` flag bit -(`6587034`); `TrailerTruncated` flag + diagnostic logging -(`9a0dfe0`). - -Forward-looking notes (low priority, no follow-up issues filed): - -- `WeenieClassId = inv.ContainerType` for inventory entries is a - placeholder; `CreateObject` overwrites it with the real weenie class - later in the login sequence. -- The 10,000 count cap throws `FormatException` on validation failure, - which the inner catch treats the same as truncation. If a future - diagnostic UI needs to distinguish "EOF mid-section" from "garbage - count rejected", split `TrailerTruncated` into two flags. For now - the `ACDREAM_DUMP_VITALS=1` log message gives the developer enough - signal. - -Files: `src/AcDream.Core.Net/Messages/PlayerDescriptionParser.cs`, -`src/AcDream.Core.Net/GameEventWiring.cs`, -`tests/AcDream.Core.Net.Tests/PlayerDescriptionParserTests.cs`, -`tests/AcDream.Core.Net.Tests/GameEventWiringTests.cs`. - ---- - -## #51 β€” [DONE 2026-05-09 Β· da56063 + N.5b SHIP] WB's terrain-split formula diverges from retail's `FSplitNESW` - -**Closed:** 2026-05-09 -**Commit:** `da56063` (black-terrain fix; landed within Phase N.5b β€” see -`docs/superpowers/plans/2026-05-09-phase-n5b-terrain-modern.md` for the -ship commit chain) -**Component:** terrain math / Phase N.5b - -**Resolution: Path C.** Phase N.5b lifted terrain rendering onto the -modern path (bindless atlas + `glMultiDrawElementsIndirect`) WITHOUT -adopting WB's `TerrainUtils.CalculateSplitDirection`. The pre-implementation -divergence test (`tests/AcDream.Core.Tests/Terrain/SplitFormulaDivergenceTest.cs`) -confirmed the two formulas disagree on **49.98%** of sweep cells β€” -fundamentally incompatible with our shared physics + visual mesh, which -both rely on retail's `FSplitNESW` (constants `0x0CCAC033` / `0x421BE3BD` / -`0x6C1AC587` / `0x519B8F25`). - -Path C: keep retail's `FSplitNESW` formula via `LandblockMesh.Build` β†’ -`TerrainBlending.CalculateSplitDirection`; mirror WB's `TerrainRenderManager` -architectural pattern (single global VBO/EBO + slot allocator + bindless -atlas + multi-draw indirect) but feed it acdream's mesh. Modern dispatcher -(`TerrainModernRenderer`) replaces `TerrainChunkRenderer` (deleted in T9 -along with `TerrainRenderer` + `terrain.vert/.frag`). - -Path A (substitute WB's formula) was killed by the divergence test. -Path B (fork-patch WB's renderer to use retail's formula) was rejected -for permanent maintenance burden. Path C ships the architectural -pattern while preserving retail-formula compliance. - -Visual mesh and physics both still consume retail's `FSplitNESW`; they -remain in lockstep, no triangle-Z hover. The N.6 / N.7 sequencing -implication this issue carried (substitute physics math only when the -visual mesh migrates) is moot β€” neither side ever switches to WB's -formula. - -**Files added:** -- `src/AcDream.App/Rendering/TerrainModernRenderer.cs` -- `src/AcDream.Core/Terrain/TerrainSlotAllocator.cs` -- `src/AcDream.App/Rendering/Shaders/terrain_modern.vert` -- `src/AcDream.App/Rendering/Shaders/terrain_modern.frag` -- `tests/AcDream.Core.Tests/Terrain/SplitFormulaDivergenceTest.cs` (the - test that killed Path A) - -**Files deleted (T9):** -- `src/AcDream.App/Rendering/TerrainChunkRenderer.cs` -- `src/AcDream.App/Rendering/TerrainRenderer.cs` -- `src/AcDream.App/Rendering/Shaders/terrain.vert` -- `src/AcDream.App/Rendering/Shaders/terrain.frag` - ---- - ## #43 β€” [DONE 2026-05-05 Β· 9e4772a] Slope staircase on observed player remotes (anim-only fallback ignored slope) **Closed:** 2026-05-05 diff --git a/docs/architecture/worldbuilder-inventory.md b/docs/architecture/worldbuilder-inventory.md deleted file mode 100644 index 68144c9..0000000 --- a/docs/architecture/worldbuilder-inventory.md +++ /dev/null @@ -1,250 +0,0 @@ -# WorldBuilder Inventory β€” what we take, adapt, or leave - -**Status:** load-bearing reference. As of 2026-05-08 acdream's strategy is -to **rely heavily on WorldBuilder** for rendering and dat-handling rather -than re-port retail algorithms ourselves. WorldBuilder is MIT-licensed, is -verified by visual inspection to render the AC world correctly (terrain, -scenery, slabs, dungeons, slopes, particles), and uses the same Silk.NET -+ .NET stack we already target. - -**Integration model:** **fork upstream WorldBuilder** at -`github.com/Chorizite/WorldBuilder`, depend on our fork, delete editor-only -code, expose hooks for our network state to feed scene data in. Sync with -upstream via merge so we inherit fixes. This document tells you, before -you write code, whether the thing you're about to port already exists in -WorldBuilder. - -**Workflow change:** Before re-implementing any AC-specific rendering or -dat-handling algorithm, **check this inventory first**. If WorldBuilder -has it, port from WorldBuilder (or call into our fork once it's wired -up), not from retail decomp. Retail decomp remains the oracle for things -WorldBuilder lacks β€” animation, motion, physics collision, networking. - ---- - -## Repo layout (as of cloned snapshot under `references/WorldBuilder/`) - -- **`Chorizite.OpenGLSDLBackend/`** β€” full OpenGL renderer (Silk.NET). -- **`WorldBuilder.Shared/`** β€” data models, dat parsers, landscape module. -- **`WorldBuilder/`** β€” Avalonia desktop app shell (NOT taken). -- **`WorldBuilder.{Windows,Linux,Mac}/`** β€” platform entry points (NOT taken). -- **`WorldBuilder.Server/`** β€” collab editing backend (NOT taken). -- **`Tests/` + `WorldBuilder.Shared.Benchmarks/`** β€” test harness (study). - -**Upstream NuGet dependencies** (these stay as NuGet packages, we don't -vendor them): - -| Package | Version | Purpose | -|---|---|---| -| `Chorizite.Core` | 0.0.18 | Plugin framework β€” contains `Chorizite.Core.Lib.BoundingBox`, `Chorizite.Core.Render.*` interfaces used by every render manager | -| `Chorizite.DatReaderWriter` | 2.1.x | dat parsing (we already use 2.1.7) | -| `Chorizite.DatReaderWriter.Extensions` | 1.1.x | extra dat helpers | -| `BCnEncoder.Net` | 2.2.x | DXT decode (we already use) | -| `SixLabors.ImageSharp` | 3.1.x | image loading | -| `Silk.NET.OpenGL` + `Silk.NET.SDL` | 2.23.x | GL + windowing (we use Silk's own windowing, they use SDL) | -| `MP3Sharp` | 1.0.5 | MP3 decode | - ---- - -## 🟒 RENDERING β€” take wholesale or adapt - -These are what makes WB "perfect". Anything in this section, we should -use from WB rather than re-implement. - -### Terrain - -| Component | What it does | -|---|---| -| `TerrainRenderManager` | Full pipeline (per-chunk GPU buffers, draw orchestration) | -| `LandSurfaceManager` | Texture blending atlas (palCode, alpha masks, road overlays) | -| `TerrainGeometryGenerator` | Heightmap β†’ mesh, normals, OnRoad, GetHeight, GetNormal | -| `TerrainChunk` | 16Γ—16 landblock chunk geometry | -| `TextureAtlasManager` | Texture atlas builder | -| `VertexLandscape` | Terrain vertex format | - -### Scenery (procedural placement: trees, bushes, rocks, fences) - -| Component | What it does | -|---|---| -| `SceneryRenderManager` | Generate + render per-vertex scenery | -| `SceneryHelpers` | Displace / RotateObj / ScaleObj / ObjAlign / CheckSlope | -| `SceneryInstance` | Per-spawn instance data | - -### Static objects (buildings, slabs, props β€” Setup + GfxObj + ObjDesc) - -| Component | What it does | -|---|---| -| `StaticObjectRenderManager` | Master pipeline for static objects | -| `ObjectRenderManagerBase` + `BaseObjectRenderManager` | Common render base | -| `ObjectMeshManager` | Mesh extraction from Setup/GfxObj, ObjDesc application | - -### Dungeons / interiors - -| Component | What it does | -|---|---| -| `EnvCellRenderManager` | Dungeon interior cell geometry | -| `PortalRenderManager` | Portal traversal / visibility | - -### Sky + atmosphere - -| Component | What it does | -|---|---| -| `SkyboxRenderManager` | Skybox rendering | -| `ParticleEmitterRenderer` + `ParticleBatcher` + `ActiveParticleEmitter` | Particle systems (sky particles, weather, magic) | - -### Visibility / culling - -| Component | What it does | -|---|---| -| `VisibilityManager` + `VisibilitySnapshot` | Frustum + cell visibility | -| `Frustum` | Frustum-cull math | - -### Other rendering helpers - -| Component | What it does | -|---|---| -| `MinimapRenderer` | Top-down minimap | -| `GlobalMeshBuffer` | Shared GPU mesh buffer | -| `GpuResourceManager` | GPU resource lifecycle | -| `InstanceData` | Instanced draw data | -| `TextureHelpers` | INDEX16, P8, BGRA, DXT decode + alpha (canonical port) | -| `DebugRenderer` + `DebugRendererLineDrawer` + `EdgeLineBuilder` | Debug primitives | - -### Shaders (22 total) - -Located at `Chorizite.OpenGLSDLBackend/Shaders/`: - -`Landscape.{vert,frag}` Β· `StaticObject.{vert,frag}` Β· `StaticObjectModern.{vert,frag}` Β· `Particle.{vert,frag}` Β· `PortalStencil.{vert,frag}` Β· `Outline.{vert,frag}` Β· `Simple3D.{vert,frag}` Β· `InstancedLine.{vert,frag}` Β· `Text.{vert,frag}` Β· `UI.{vert,frag}` Β· `Gizmo.{vert,frag}` (editor-only) - ---- - -## 🟒 LOW-LEVEL GL / FRAMEWORK β€” take or replace with our own - -Either take WB's wrappers wholesale, or keep our own and adapt the -render managers to use ours. These wrappers are stateless or -near-stateless and are the easiest to swap. - -| Component | What it does | -|---|---| -| `OpenGLGraphicsDevice` | Silk.NET.OpenGL wrapper | -| `OpenGLRenderer` | Render orchestration | -| `GLSLShader` | Shader compile/link/uniforms | -| `GLHelpers` + `GLStateScope` | GL state utility | -| `ManagedGLFrameBuffer` / `ManagedGLIndexBuffer` / `ManagedGLTexture` / `ManagedGLTextureArray` / `ManagedGLUniformBuffer` / `ManagedGLVertexArray` / `ManagedGLVertexBuffer` | GL resource wrappers | -| `TextureParameters` | Sampler config | -| `GpuMemoryTracker` | Memory tracking | -| `Camera2D` / `Camera3D` / `CameraBase` / `ICamera` / `CameraController` | Camera primitives | -| `GameScene` + `SingleObjectScene` + `SceneData` + `ModernRenderData` + `RenderPass` | Scene / pass structures | - ---- - -## 🟒 GEOMETRY / MATH UTILS β€” take wholesale - -| Component | File | -|---|---| -| `TerrainUtils` (OnRoad, GetNormal, GetHeight, GetRoad, palCode) | `WorldBuilder.Shared/Modules/Landscape/Lib/TerrainUtils.cs` | -| `TerrainCacheManager` | `…/Lib/TerrainCacheManager.cs` | -| `TerrainRaycast` | `…/Lib/TerrainRaycast.cs` | -| `GeometryUtils` | `WorldBuilder.Shared/Lib/GeometryUtils.cs` | -| `RaycastingUtils` (ray-vs-sphere/AABB/triangle) | `WorldBuilder.Shared/Lib/RaycastingUtils.cs` | -| `DoubleNumerics` (double-precision Vector/Matrix) | `WorldBuilder.Shared/Lib/DoubleNumerics.cs` | -| `DatUtils` | `WorldBuilder.Shared/Lib/DatUtils.cs` | -| `BoundingBoxExtensions` | `Chorizite.OpenGLSDLBackend/Lib/BoundingBoxExtensions.cs` | - ---- - -## 🟒 DATA MODELS β€” take selectively - -| Component | What it does | -|---|---| -| `RegionInfo` | Landblock metadata wrapper (LandblockSizeInUnits, CellSizeInUnits, etc.) | -| `TerrainEntry` | Per-vertex terrain (Type/Scenery/Road/Height) | -| `MergedLandblock` | Merged dat data | -| `CellSplitDirection` | SW-NE vs NE-SW | -| `Cell` | Generic cell wrapper | -| `ObjectId` | Object identifier | -| `Position` | World position | -| `ACEnums` | AC-specific enums | -| `WbBuildingPortal` / `WbCellPortal` | Portal structures | -| `BuildingObject` | Building data | - ---- - -## 🟑 EDITOR-ONLY β€” leave behind / delete in fork - -These exist for the editor experience and have no place in a game -client. Delete in fork. - -- **`Modules/Landscape/Tools/*`** β€” `BrushTool`, `BucketFillTool`, - `RoadLineTool`, `RoadVertexTool`, `InspectorTool`, - `ObjectManipulationTool`, `Gizmo*` (DragHandler, HitTester, Renderer, - State), `TexturePainting*`, `SceneRaycaster`, - `LandscapeBrush`, `LandscapeToolBase`, `LandscapeToolContext`, - `IToolSettingsProvider`, `ILandscapeBrush`, `ILandscapeEditorService`, - `ILandscapeRaycastService`, `ILandscapeTool`, `ITexturePaintingTool` -- **`Modules/Landscape/Commands/*`** β€” undo/redo command pattern for - editor (Add/Delete/Move/Rename/Reorder/etc.) -- **`LandscapeDocument` + `LandscapeLayer` + `LandscapeLayerGroup` + `LandscapeChunk` + `LandscapeLayerChunk` + `LandscapeLayerBase`** β€” editor document model -- **`Modules/Landscape/Models/TerrainPatch*` + `LandblockChangedEventArgs`** β€” editor mutation events -- **`Modules/Landscape/Services/ILandscapeCacheService` + `ILandscapeDataProvider` + `ILandscapeObjectService` + impls** β€” editor data flow -- **All `Migrations/*`** β€” SQLite schema migrations (project file format) -- **`Repositories/*`** + **`Services/*`** β€” project storage, dat repository, AceDb, SignalR sync, document manager, undo stack, world coordinates, keyword DB, project migration, semantic kernel AI helpers -- **`Hubs/*`** β€” collaborative editing via SignalR -- **`StaticObject` (editor model)** β€” replace with our own scene-state data model fed from network -- **`BackendGizmoDrawer` + `GizmoRenderer`** β€” editor gizmos -- **`ProjectStructures, IProject, Project`** β€” editor project files -- **`KeyBinding`** β€” editor input binding -- **`ViewportInputEvent[Extensions]`** β€” editor viewport input -- **`EditorState`** β€” editor state container - ---- - -## 🟑 AUDIO / FONT β€” we already have alternatives - -Keep ours; don't take theirs. - -- **`AudioPlaybackEngine`** β€” uses MP3Sharp. We have OpenAL. -- **`FontRenderer`** β€” uses ImageSharp. We have BitmapFont/StbTrueTypeSharp + ImGui. - ---- - -## πŸ”΄ NOT IN WORLDBUILDER β€” port from retail decomp ourselves - -WorldBuilder is a dat editor; it does not have: - -- **Network protocol** β€” UDP framing, ISAAC, packet codec, ACE message - layer (we have this; oracle is `references/holtburger`) -- **Physics** β€” collision (CPhysicsObj transitions, BSP queries, sphere - sweeps), step-up, walkable validation (we have partial; oracle is the - retail decomp at `docs/research/named-retail/`) -- **Animation** β€” motion sequencer, cycle/non-cycle parts, animation - frame interpolation (we have this; oracle is retail decomp) -- **Movement** β€” local player WASD β†’ MoveToState wire, remote-entity - motion via UpdateMotion + dead-reckoning (we have this; oracle is - `references/holtburger` + retail decomp) -- **Game UI** β€” chat, vitals, inventory, spell book, allegiance, options - (we have this; ImGui-based today, custom-toolkit later) -- **Plugin API** β€” `IGameState`, `IEvents`, `IActions`, `IPacketPipeline`, - `IOverlay` (we have this β€” acdream-unique) -- **Game events** β€” combat, allegiance, spell casting, quest events - (we have this; oracle is ACE for opcodes + retail for client behavior) -- **Audio** β€” OpenAL pipeline, sound triggers (we have this) -- **TurbineChat** + **slash commands** (we have this) -- **Login + character selection flow** (we have this) - ---- - -## What this means for the workflow - -The CLAUDE.md "grep named β†’ decompile β†’ verify β†’ port" workflow stays -the rule for everything in the πŸ”΄ list (network, physics, animation, -movement, UI, plugin, audio, chat). For anything in 🟒, the new rule is: -**check this inventory FIRST**. If WB has it, port from WB. Re-porting -from retail decomp when WB already has a tested port is no longer -appropriate β€” that's how we got the scenery edge-vertex bug. - -When the inventory says "take wholesale or adapt" and we discover a -behavior mismatch with retail (rare β€” WB is verified), the resolution -is: reconcile WB ↔ retail decomp ↔ holtburger ↔ ACE ↔ ACViewer (the -existing reference hierarchy in CLAUDE.md). WorldBuilder ranks at the -top of that hierarchy for anything 🟒. diff --git a/docs/plans/2026-04-11-roadmap.md b/docs/plans/2026-04-11-roadmap.md index c2e403c..91f7100 100644 --- a/docs/plans/2026-04-11-roadmap.md +++ b/docs/plans/2026-04-11-roadmap.md @@ -1,6 +1,6 @@ # acdream β€” strategic roadmap -**Status:** Living document. Updated 2026-05-12. **Between phases.** **Since the last header update:** C.1.5b shipped (issue #56 per-part transforms for multi-emitter PES + `EntityScriptActivator` extended to dat-hydrated EnvCell statics & exterior stabs β€” portal swirl, inn fireplace flames, cottage chimney smoke, spell-cast particles all match retail). **Earlier this week:** post-A.5 polish completed (#52 lifestone, #54 JobKind, #53 Tier 1 cache); N.6 slice 1 shipped (gpu_us fix + radius=12 perf baseline, conclusion CPU dominates GPU 30–50Γ—); C.1.5a shipped (portal PES wiring; surfaced #56 β†’ resolved in C.1.5b). +**Status:** Living document. Updated 2026-05-02 for Phase M network-stack conformance planning. **Purpose:** One source of truth for where the project is and where it's going. Every observed defect or missing feature has a named phase that owns it; when something looks wrong in-game, look here to find the phase that'll address it. Implementation details live in per-phase specs under `docs/superpowers/specs/`, not in this file. --- @@ -31,7 +31,6 @@ | A.1 | Streaming landblock loader β€” runtime-configurable visible window (default 5Γ—5, `ACDREAM_STREAM_RADIUS`), camera-centered offline / player-centered live, hysteresis-based unloads, pending-spawn list for late CreateObject events | Live βœ“ | | A.2 | Frustum culling β€” per-landblock AABB test (Gribb-Hartmann), terrain + static-mesh renderers skip culled landblocks, perf overlay in window title | Visual βœ“ | | A.3 | Background net receive thread β€” dedicated daemon thread buffers UDP into Channel, render thread drains | Visual βœ“ | -| A.5 | Two-tier streaming + horizon LOD β€” N₁=4 (full detail, 81 LBs) + Nβ‚‚=12 (terrain only, 544 LBs); fog blend at N₁; per-LB entity dispatcher walk tightened (Change #1 animated-walk fix + Change #2 cached AABB); single-worker off-thread mesh build; mipmaps + 16x anisotropic on TerrainAtlas; A2C with MSAA 4x on foliage; depth-write audit + lock-in test; **NEW T22.5: QualityPreset system** (Low/Medium/High/Ultra) with per-preset radii + MSAA + anisotropic + A2C + completions; env-var overrides per field; F11 mid-session re-apply. **Bug fixes post-T26 ship-prep**: (Bug A) far-tier worker now strips entities from far-tier loads β€” without this fix, far-tier LBs were loading their full entity layer (~71K entities) defeating the two-tier optimization; (Bug B) WalkEntities switched from per-frame fresh-list allocation to caller-provided scratch list (eliminated ~480 KB/frame GC pressure). **Deferred to post-A.5**: Tier 1 entity-classification cache (first attempt broke animation; revert + redo with animation-mutation audit), lifestone visual (missing in render), JobKind plumbing through BuildLandblockForStreaming (proper Bug A fix), Tier 2/3 perf optimizations (roadmap at docs/plans/2026-05-10-perf-tiers-2-3-roadmap.md). Plan archived at docs/superpowers/plans/2026-05-09-phase-a5-two-tier-streaming.md. | Live βœ“ | | B.3 | Physics MVP resolver foundation β€” terrain contact, CellSurface prototype, streaming-populated collision inputs, and first `PhysicsEngine` resolver path. Not the complete retail collision system. | Tests βœ“ | | B.2 | Player movement mode β€” Tab-toggled WASD ground walking, walk/run/idle animations, third-person chase camera, MoveToState + AutonomousPosition outbound, portal entry. Outdoor-only MVP. | Live βœ“ | | D.1 | 2D ortho overlay + font rendering (StbTrueTypeSharp atlas + TextRenderer + DebugOverlay) | Visual βœ“ | @@ -58,17 +57,6 @@ | K | Input architecture β€” `Action` enum, `KeyChord`, `KeyBindings`, multicast `InputDispatcher` with scope-stack + modal capture, retail-default keymap (152 bindings), `keybinds.json` persistence, F11 Settings panel with click-to-rebind + conflict detection, main menu bar + View menu | Live βœ“ | | L.0 | Full retail-style Settings interface β€” F11 tabbed panel with 6 tabs (Keybinds + Display + Audio + Gameplay + Chat + Character). `settings.json` at `%LOCALAPPDATA%\acdream\`, per-toon `Character` keying (swapped on EnterWorld). Display GL knobs (Resolution / Fullscreen / VSync / FOV / ShowFps) + Audio (Master / SFX) live-wired; Gameplay / Chat / Character settings persist for server-sync wiring later. Tab API extension to `IPanelRenderer`; chat Copy mode (read-only multi-line); per-panel layout reset; FramebufferResize handler keeps GL viewport + camera aspect + panel positions in sync. | Live βœ“ | | C.1 | PES particle system + sky-pass refinements β€” retail-faithful `ParticleEmitterInfo` unpack with all 13 motion integrators (`Particle::Init`/`Update` ports of `0x0051c290`/`0x0051c930`), `PhysicsScriptRunner` with `CallPES` self-loop semantics, `ParticleHookSink` with `EmitterDied` cleanup, instanced billboard `ParticleRenderer` with material-derived blend (DAT emitters never default additive β€” pulled from particle GfxObj surface), global back-to-front sort, BC clipmap alpha-keying, AttachLocal `is_parent_local=1` live-parent follow via `UpdateEmitterAnchor`. Sky pass: `Translucent+ClipMap` β†’ alpha-blend cloud sheet (matches `D3DPolyRender::SetSurface` `0x0059c4d0`), raw-`Additive` fog-skip (matches `0x0059c882`), per-keyframe `SkyObjectReplace` Translucency/Luminosity/MaxBright divide-by-100, bit `0x01` pre/post-scene split (matches `GameSky::CreateDeletePhysicsObjects` `0x005073c0`), Setup-backed (`0x020xxxxx`) sky objects via `SetupMesh.Flatten`, persistent GL sampler objects (Wrap + ClampToEdge) replace per-frame wrap-mode mutation (ported from WorldBuilder's `OpenGLGraphicsDevice`), post-scene Z-offset gated on `(Properties & 4) != 0 && (Properties & 8) == 0` per `GameSky::UpdatePosition` `0x00506dd0`. Sky-PES playback disabled by default (named-retail proves `GameSky` drops `pes_id`); `ACDREAM_ENABLE_SKY_PES=1` opens the experimental path. 1325 β†’ 1331 tests. | Live βœ“ | -| N.1 | WorldBuilder-backed scenery (Chorizite/WorldBuilder fork as submodule, SceneryHelpers + TerrainUtils replace our inline ports) | Live βœ“ | -| N.3 | WorldBuilder-backed texture decode β€” `SurfaceDecoder` delegates INDEX16 / P8 / A8R8G8B8 / R8G8B8 / A8(+Additive) to `TextureHelpers.Fill*`; `isAdditive` threaded through (terrain alpha β†’ `FillA8Additive`, non-additive entity surfaces β†’ `FillA8`). R5G6B5 + A4R4G4B4 newly handled (previously magenta). X8R8G8B8, DXT1/3/5, SolidColor remain ours (no WB equivalent). 9 conformance tests prove byte-identical equivalence per format. | Live βœ“ | -| N.4 | Rendering pipeline foundation β€” adopted WB's `ObjectMeshManager` as the production mesh pipeline behind `ACDREAM_USE_WB_FOUNDATION` (default-on). `WbMeshAdapter` is the single seam (owns `ObjectMeshManager`, drains the staged-upload queue per frame, populates `AcSurfaceMetadataTable` with per-batch translucency / luminosity / fog metadata). `WbDrawDispatcher` is the production draw path: groups all visible (entity, batch) pairs, single-uploads the matrix buffer, fires one `glDrawElementsInstancedBaseVertexBaseInstance` per group with `BaseInstance` slicing into the shared instance VBO. `LandblockSpawnAdapter` + `EntitySpawnAdapter` bridge spawn lifecycle to WB ref-counts (atlas tier vs per-instance). Perf wins shipped as part of N.4: per-entity frustum cull, opaque front-to-back sort, palette-hash memoization (compute once per entity, reuse across batches). Visual verification at Holtburg passed: scenery + connected characters with full close-detail geometry (Issue #47 regression resolved). Legacy `InstancedMeshRenderer` retained as `ACDREAM_USE_WB_FOUNDATION=0` escape hatch until N.6 (retired early in N.5 ship amendment). | Live βœ“ | -| N.5 | Modern rendering path β€” lifted `WbDrawDispatcher` onto bindless textures (`GL_ARB_bindless_texture`) + `glMultiDrawElementsIndirect`. Per-frame entity rendering: 3 SSBO uploads (instance matrices @ binding=0, batch data @ binding=1, indirect commands) + 2 indirect draw calls (opaque + transparent). ~12-15 GL calls per frame regardless of group count, down from hundreds-of-per-group in N.4. CPU dispatcher: 1.23 ms/frame median at Holtburg courtyard (1662 groups, ~810 fps sustained). All textures on the WB modern path use 1-layer `Texture2DArray` + `sampler2DArray`. Legacy callers keep `Texture2D` / `sampler2D` via the parallel `TextureCache` path until N.6 retires them. Three gotchas captured in memory: texture target lock-in, bindless Dispose order (two-phase non-resident before delete), GL_TIME_ELAPSED double-buffering. **Ship amendment 2026-05-08:** legacy renderers (`InstancedMeshRenderer`, `StaticMeshRenderer`, `WbFoundationFlag`) retired within N.5 β€” modern path is mandatory; missing bindless throws `NotSupportedException` at startup. N.6 scope narrowed accordingly. Plan archived at `docs/superpowers/plans/2026-05-08-phase-n5-modern-rendering.md`. | Live βœ“ | -| N.5b | Terrain on the modern rendering path β€” `TerrainModernRenderer` replaces `TerrainChunkRenderer` (the latter plus `TerrainRenderer` + `terrain.vert/.frag` deleted). Single global VBO/EBO with slot allocator (one slot per landblock), per-frame `DrawElementsIndirectCommand[]` upload + `glMultiDrawElementsIndirect`, bindless atlas handles passed as `uvec2` uniforms reconstructed via `sampler2DArray(handle)`. **Path C** chosen: mirrors WB's `TerrainRenderManager` pattern but consumes `LandblockMesh.Build` so retail's `FSplitNESW` formula is preserved (closes ISSUE #51). Path A killed by 49.98% measured divergence between WB's `CalculateSplitDirection` and retail's at addr `00531d10`; Path B (fork-patch WB) rejected for permanent maintenance burden. Perf at Holtburg radius=5 (commit `da56063`): modern 6.4-7.0 Β΅s / 9-14 Β΅s p95 vs legacy 1.5 Β΅s / 3.0 Β΅s β€” **modern is ~4Γ— SLOWER on CPU at radius=5** because legacy's 16Γ—16-LB chunking collapsed visible LBs to one `glDrawElements`. Architectural wins (zero `glBindTexture`/frame, constant-cost dispatch, per-LB frustum cull) manifest at higher radius (A.5 territory). Spec acceptance criterion 5 ("β‰₯10% lower CPU at radius=5") amended via `docs/plans/2026-05-09-phase-n5b-perf-baseline.md`. Three gotchas captured in memory: `uniform sampler2DArray` + `glProgramUniformHandleARB` GL_INVALID_OPERATIONs on at least one driver (use `uniform uvec2` + `sampler2DArray(handle)` constructor instead β€” N.5's mesh_modern pattern); `MaybeFlushTerrainDiag` median-calc underflow on first sample; visual gates need actual visual confirmation, not assent. Plan archived at `docs/superpowers/plans/2026-05-09-phase-n5b-terrain-modern.md`. | Live βœ“ | -| N.6 slice 1 | GPU timing fix + radius=12 perf baseline. Fixed the gpu_us double-buffering bug in `WbDrawDispatcher` (ring-of-3 query slots, read-before-overwrite, vendor-neutral across AMD/NVIDIA/Intel desktop GL). Added env-gated `ACDREAM_DUMP_SURFACES=1` one-shot surface-format histogram dump in `TextureCache` for the atlas-opportunity audit. Captured authoritative baseline at Holtburg radii 4 / 8 / 12 (standstill + walking) with the now-working `gpu_us` diagnostic; baseline doc concludes CPU dominates GPU by 30–50Γ— at every radius and recommends C.1.5 next then reduced-scope slice 2 (atlas + persistent-mapped buffers dropped). Baseline numbers at [docs/plans/2026-05-11-phase-n6-perf-baseline.md](2026-05-11-phase-n6-perf-baseline.md). Plan archived at `docs/superpowers/plans/2026-05-11-phase-n6-slice1.md`. | Live βœ“ | -| C.1.5a | Portal PES wiring β€” server-spawned `WorldEntity` entities now fire their `Setup.DefaultScript` through the already-shipped `PhysicsScriptRunner` on enter-world. New ~70-line [`EntityScriptActivator`](../../src/AcDream.App/Rendering/Vfx/EntityScriptActivator.cs) class wires into `GpuWorldState`'s spawn lifecycle (`AppendLiveEntity` β†’ `OnCreate`, `RemoveEntityByServerGuid` β†’ `OnRemove`). Resolver lambda in `GameWindow` hits `_dats.Get(...)?.DefaultScript.DataId` with defensive try/catch returning `0u` on miss. Activator also seeds `_particleSink.SetEntityRotation` so hook offsets transform from entity-local to world space correctly. **Verified at the Holtburg Town network portal**: 10-hook portal script fires end-to-end with correct color, persistence, orientation, multi-emitter dispatch. **Known limitation surfaced and filed as issue #56**: `ParticleHookSink` ignores `CreateParticleHook.PartIndex`, so the 10 emitters collapse to one root position instead of distributing across the portal Setup's parts β€” visually produces a compressed, partly-ground-buried swirl. Mechanism is correct; per-part transform handling is the next vfx-pipeline work (blocks slice 2 visual delight; affects every multi-emitter PES). Spec: [`docs/superpowers/specs/2026-05-12-phase-c1.5a-portals-design.md`](../superpowers/specs/2026-05-12-phase-c1.5a-portals-design.md). Plan: [`docs/superpowers/plans/2026-05-12-phase-c1.5a-portals.md`](../superpowers/plans/2026-05-12-phase-c1.5a-portals.md). | Live βœ“ (with #56) | -| B.4b | Outbound Use handler wiring + 4 bonus fixes (L.2g slices 1b+1c, double-click detection, DoubleClick gate fix). Shipped 2026-05-13 (branch `claude/compassionate-wilson-23ff99`, merge pending). Closes #57. Files #58 (door swing animation, M1-deferred). `WorldPicker.BuildRay` + `Pick` (ray-sphere entity pick with inside-sphere guard); `GameWindow.OnInputAction` switch cases for `SelectLeft` / `SelectDblLeft` / `UseSelected`; `_entitiesByServerGuid` reverse-lookup dict + ServerGuidβ†’entity.Id translation in `OnLiveStateUpdated` (L.2g slice 1c β€” THE actual blocker); `InputDispatcher` double-click detection 500ms threshold (binding was dead code without it); `CollisionExemption.ShouldSkip` widened to ETHEREAL-alone (ACE Door.Open() sends `state=0x0001000C`, not `0x14`). M1 demo target "open the inn door" verified at Holtburg inn doorway. Plan: [`docs/superpowers/plans/2026-05-13-phase-b4b-plan.md`](../superpowers/plans/2026-05-13-phase-b4b-plan.md). Handoff: [`docs/research/2026-05-13-b4b-shipped-handoff.md`](../research/2026-05-13-b4b-shipped-handoff.md). | Live βœ“ | -| B.4c | Door swing animation. Shipped 2026-05-13 (branch `claude/phase-b4c-door-anim`, merge pending). Closes #58. Files #61 (AnimationSequencer linkβ†’cycle boundary flash; low-severity polish) + #62 (PARTSDIAG null-guard; latent). Spawn-time `AnimationSequencer` registration for door entities in `GameWindow.OnLiveEntitySpawnedLocked`: initial cycle seeded from `spawn.PhysicsState` (Off for closed, On for open). Shared `IsDoorName` / `IsDoorSpawn` helpers. `[door-cycle]` diagnostic in `OnLiveMotionUpdated` (gated on `ACDREAM_PROBE_BUILDING`). Bonus stance-value fix: `NonCombat = 0x3D` not `0x01` (wrong value caused doors to render halfway underground via empty sequencer frames). Visual-verified 2026-05-13 at Holtburg inn doorway: swing-open + swing-close cycles both play. M1 demo target "open the inn door" now has full visual feedback. Plan: [`docs/superpowers/plans/2026-05-13-phase-b4c-plan.md`](../superpowers/plans/2026-05-13-phase-b4c-plan.md). Handoff: [`docs/research/2026-05-13-b4c-shipped-handoff.md`](../research/2026-05-13-b4c-shipped-handoff.md). | Live βœ“ | -| B.5 | Ground-item pickup (F-key, close-range path). Shipped 2026-05-14 (branch `claude/phase-b5-pickup`, merge pending). Closes M1 demo target 4/4 *"pick up an item"*. New `InteractRequests.BuildPickUp(seq, itemGuid, containerGuid, placement)` builds the 24-byte `PutItemInContainer (0xF7B1/0x0019)` wire body verified against `references/ACE/Source/ACE.Server/Network/GameAction/Actions/GameActionPutItemInContainer.cs`. New private `GameWindow.SendPickUp(uint itemGuid)` helper mirrors `SendUse`'s gate-on-InWorld pattern; `case InputAction.SelectionPickUp` in `OnInputAction` switch routes the F-key through `_selectedGuid`. **Bonus wire-handler fix (Task 2b):** ACE despawns picked-up items via `GameMessagePickupEvent (0xF74A)`, not the `GameMessageDeleteObject (0xF747)` we already handled β€” surfaced during visual testing (item kept rendering on ground after successful server-side pickup). New `PickupEvent.cs` parser + `WorldSession` dispatch branch adapt to `DeleteObject.Parsed` and reuse the existing `EntityDeleted β†’ OnLiveEntityDeleted β†’ RemoveLiveEntityByServerGuid` chain. Files #63 (server-initiated `MoveToObject` auto-walk not honored β€” out-of-range pickup / double-click fails server-side timeout) + #64 (local-player pickup animation does not render). Visual-verified 2026-05-14 at Holtburg: 3 successful close-range pickups (Pink Taper + Violet Tapers), item despawns locally as ACE acks. Plan: [`docs/superpowers/plans/2026-05-14-phase-b5-pickup.md`](../superpowers/plans/2026-05-14-phase-b5-pickup.md). Handoff: [`docs/research/2026-05-14-b5-shipped-handoff.md`](../research/2026-05-14-b5-shipped-handoff.md). | Live βœ“ | -| C.1.5b | Per-part PES transforms + dat-hydrated entity DefaultScript dispatch. Closes issue #56. Shipped 2026-05-12 across 5 commits (`1e3c33b` docs+plan, `f3bc15e` SetupPartTransforms helper, `11521f4` ParticleHookSink applies `CreateParticleHook.PartIndex`, `5ca5827` activator refactor + GameWindow resolver lambda, `8735c39` GpuWorldState 4 new fire-sites). **Slice A** β€” new [`SetupPartTransforms.Compute(setup)`](../../src/AcDream.Core/Meshing/SetupPartTransforms.cs) walks `PlacementFrames[Resting]` β†’ `[Default]` β†’ first-available (mirrors `SetupMesh.Flatten` priority) and returns `Matrix4x4` per part; new `ParticleHookSink.SetEntityPartTransforms(entityId, partTransforms)` mirrors the existing `_rotationByEntity` pattern; `SpawnFromHook` now transforms hook offset through `partTransforms[partIndex]` before applying entity rotation. **Slice B** β€” activator's `ServerGuid==0` guard relaxed: keys by `entity.ServerGuid` when non-zero, else `entity.Id` (collision-free with server guids in the `0x40xxxxxx` interior / `0x80xxxxxx` scenery / `0xC0xxxxxx` ranges). Resolver delegate refactored to return `ScriptActivationInfo(ScriptId, PartTransforms)` so one dat lookup yields both pieces. `GpuWorldState` fires the activator from 4 new sites: `AddLandblock` + `AddEntitiesToExistingLandblock` (Farβ†’Near promotion) for OnCreate, `RemoveLandblock` + `RemoveEntitiesFromLandblock` (Nearβ†’Far demotion) for OnRemove. ServerGuid==0 filter on AddLandblock avoids double-firing pending-bucket merges. **Reality discovery folded into spec Β§3**: EnvCell `StaticObjects` are already hydrated as `WorldEntity` instances by `GameWindow.BuildInteriorEntitiesForStreaming` (with stable `entity.Id` in `0x40xxxxxx`) β€” no synthetic-ID scheme or separate walker class needed (handoff Β§4 Q1/Q2 mooted). **Visual verification 2026-05-12**: Holtburg Town network portal swirl distributes across the arch (no ground-burial), Inn fireplace flames render over the firebox, cottage chimney smoke columns render, spell-cast animation-hook particles all match retail. 18 new + 4 updated tests, all Vfx/Meshing/Streaming/Activator green. Spec: [`docs/superpowers/specs/2026-05-13-phase-c1.5b-design.md`](../superpowers/specs/2026-05-13-phase-c1.5b-design.md). Plan: [`docs/superpowers/plans/2026-05-13-phase-c1.5b.md`](../superpowers/plans/2026-05-13-phase-c1.5b.md). | Live βœ“ | Plus polish that doesn't get its own phase number: - FlyCamera default speed lowered + Shift-to-boost @@ -89,7 +77,6 @@ Plus polish that doesn't get its own phase number: - **βœ“ SHIPPED β€” A.2 β€” Frustum culling.** Per-landblock AABB test (Gribb-Hartmann plane extraction + positive-vertex AABB test) in both `TerrainRenderer.Draw` and `StaticMeshRenderer.Draw`. Per-entity culling deferred. LOD deferred to Phase C. Performance overlay in window title shows FPS, frame time, visible/total landblock ratio, entity count, animated count. ~160fps uncapped at 5Γ—5 radius. - **βœ“ SHIPPED β€” A.3 β€” Background net receive thread.** Dedicated daemon thread continuously pulls raw UDP datagrams from the kernel buffer into a `Channel`. Render thread's `Tick()` drains the channel. All decode, fragment assembly, ISAAC crypto, event dispatch, and ack-sending remain on the render thread β€” minimal change that prevents packet drops during frame stalls. Thread starts after `EnterWorld()` completes; `PumpOnce()` during handshake still reads the socket directly. - **A.4 β€” Async dat decoding.** Folded into the streaming worker β€” it's the worker's read path, not a separate subsystem. Called out here because regressions in dat caching could land on this surface. -- **βœ“ SHIPPED β€” A.5 β€” Two-tier streaming + horizon LOD.** Shipped 2026-05-10. See shipped table above for full description. Plan archived at `docs/superpowers/plans/2026-05-09-phase-a5-two-tier-streaming.md`. **Acceptance:** - Walk across 10+ landblocks in any direction, no crashes, no empty voids. @@ -126,10 +113,6 @@ Plus polish that doesn't get its own phase number: **Sub-pieces:** - **βœ“ SHIPPED β€” C.1 β€” VFX / particle system + sky-pass refinements.** Retail-faithful `ParticleEmitterInfo` runtime + 13-type motion integrator port + `PhysicsScript` runner + instanced billboard renderer with material-derived blend + global back-to-front sort + AttachLocal live-parent follow. Sky-pass refinements: Translucent+ClipMap alpha-blend, raw-Additive fog-skip, per-keyframe SkyObjectReplace divide-by-100, sampler-object wrap selection (ported from WorldBuilder), gated post-scene Z-offset. Sky-PES disabled by default β€” named-retail decomp proves `GameSky` drops `pes_id`. **Portal swirls, chimney smoke, fireplace flames** still need a Phase C.1.5 follow-up to wire entity-attached emitters to retail effect IDs (the data layer is ready; only the wiring is deferred). Lands as merge `feat(vfx): Phase C.1 β€” PES particle renderer + post-review fixes` (`ec1bbb4`) + `refactor(sky): replace per-frame wrap-mode mutation with persistent samplers` (`3d21c13`). -- **C.1.5 β€” entity-attached PES wiring (sliced).** Three sub-slices wiring `PhysicsScript` / `DefaultScript` dispatch to the entity lifecycle so portals, chimneys, fireplaces, and sky effects animate per retail: - - **βœ“ SHIPPED β€” C.1.5a (portals)** β€” 2026-05-11 (merge `88bda12`). `EntityScriptActivator` fires `Setup.DefaultScript` on every server-spawned `WorldEntity` via `PhysicsScriptRunner`. Visual-verified at Holtburg Town network portal. Surfaced known limitation as issue #56 (per-part transform handling) β€” addressed in C.1.5b. Plan archived at [`docs/superpowers/plans/2026-05-12-phase-c1.5a-portals.md`](../superpowers/plans/2026-05-12-phase-c1.5a-portals.md). - - **βœ“ SHIPPED β€” C.1.5b (per-part transforms + EnvCell statics)** β€” 2026-05-12. Closes #56. `SetupPartTransforms.Compute` + `ParticleHookSink.SetEntityPartTransforms` + `SpawnFromHook` part-transform application β€” multi-emitter scripts now distribute across mesh parts. `EntityScriptActivator` `ServerGuid==0` guard relaxed (keys by `entity.Id` when ServerGuid is zero) + 4 new `GpuWorldState` fire-sites pick up dat-hydrated entities (EnvCell statics + exterior stabs) β€” fireplaces and chimneys now fire their `DefaultScript` automatically. Reality discovery during design: EnvCell statics are already hydrated as `WorldEntity` items by `BuildInteriorEntitiesForStreaming`, so no synthetic-ID scheme or separate walker was needed. Visual-verified at Holtburg portal + Inn fireplace + cottage chimney + spell cast. Plan archived at [`docs/superpowers/plans/2026-05-13-phase-c1.5b.md`](../superpowers/plans/2026-05-13-phase-c1.5b.md). - - **PLANNED β€” C.1.5c (sky-PES dispatch chain).** Promoted from former issue #36 (2026-05-11 triage). Ports retail's persistent-emitter creation on celestial / sky objects + the PES timeline driver (`CallPESHook::Execute` β†’ `CPhysicsObj::CallPES` β†’ `create_particle_emitter`) that drives them ~150Γ—/min. Decomp anchors + live-trace evidence + 6-step impl outline in closed issue [#36](../ISSUES.md#36). **Closes #2 (lightning), #28 (aurora), #29 (cloud thinness) when shipped.** Does NOT close #4 (sky horizon-glow fog) β€” that's shader work, not PES. - **C.2 β€” Dynamic point lights.** Fireplaces and lamps need local lighting; small upgrade to the mesh shader to accumulate N (e.g., 4) nearest point lights per draw. Uniform-buffer or UBO-friendly layout. - **C.3 β€” Palette range tuning.** Small per-range offset/length tweaks to match retail skin/hair/eye colors. Mostly diff and verify work, no architecture change. - **C.4 β€” Double-sided translucent polys.** Edge case left by Phase 9.2: neg-side translucent polys are culled because cull is always BACK. Fix by tracking per-sub-mesh `CullMode` and flipping GL state per draw (or drawing twice with opposite cull). Minor. @@ -212,7 +195,6 @@ Research: R1 + R2 + R6 + R8 + UI slices 04/05. - **F.3 β€” Combat math + damage flow.** Damage formula, per-body-part AL, crit, hit-chance sigmoid. Server broadcasts damage events; client displays + HP bar. See `r02-combat-system.md` + `src/AcDream.Core/Combat/`. - **F.4 β€” Spell cast state machine.** `SpellCastStateMachine` + active buff tracking. Buffs + recalls first, projectile spells later. Fizzle sigmoid + mana conversion. See `r01-spell-system.md` + `src/AcDream.Core/Spells/`. - **F.5 β€” Core panels.** Attributes / Skills / Paperdoll / Inventory / Spellbook β€” using the retail-ui framework from Phase D.2. See `05-panels.md` under retail-ui. **(Targets `AcDream.UI.Abstractions`; unblocked by D.2a β€” ships with ImGui widgets β€” and reskinned when D.2b lands.)** - - **F.5a β€” Visible-at-login dev panels.** First deliverable on top of #13 (PD trailer parser shipped 2026-05-10): wire `PlayerDescriptionParser.Parsed.{Inventory, Equipped, Shortcuts, HotbarSpells, DesiredComps, Options1, Options2, SpellbookFilters}` and `ItemRepository.Items` into minimal ImGui dev panels under `ACDREAM_DEVTOOLS=1` so the parsed data is observable in-game without a real retail-skin panel. Establishes the binding pattern (`AcDream.UI.Abstractions` ViewModels β†’ ImGui renderer) the eventual D.2b retail-skinned panels reuse. Acceptance: log in, open dev overlay, see your inventory list / hotbars / shortcuts / character-options bitfields populated from the live PD message. **Targets:** `src/AcDream.UI.Abstractions/` (ViewModels) + `src/AcDream.App/UI/ImGui/` (panels). Spec to brainstorm before code. **Acceptance:** equip a weapon, swing at a monster, see damage numbers, buff yourself, recall to the lifestone. @@ -442,40 +424,19 @@ EchoRequest/EchoResponse handling, runtime ping/timeout policy, and a typed protocol/action layer. These gaps will become expensive as movement, dungeons, inventory, combat, and plugins depend on stable packet semantics. -**Plan of record:** Detailed design spec at -[`docs/superpowers/specs/2026-05-10-phase-m-network-stack-design.md`](../superpowers/specs/2026-05-10-phase-m-network-stack-design.md) -(supersedes the planned-but-never-written `2026-05-02-network-stack-conformance.md` -the original entry referenced). The spec defines: **Bar C** ("wireable on demand") -as the completeness target; a **three-layer architecture** (`INetTransport` / -`IReliableSession` / `IGameProtocol`) with `WorldSession` as a thin behavior -consumer on top; a **worktree-branch big-bang** migration model on -`claude/phase-m-network-stack` with weekly rebase cadence and single-merge ship; -per-sub-phase entry/exit gates with hour estimates; conformance test plan -(golden vectors + live capture replay + live ACE smoke); risk register; and a -**256-hour / ~6.4-week single-developer cost estimate** (4–6 weeks calendar -with subagent parallelization on M.1 and M.6). Treat holtburger as the -client-behavior oracle, ACE as server-outbound authority, named retail decomp -as wire-format ground truth. +**Plan of record:** create +`docs/superpowers/specs/2026-05-02-network-stack-conformance.md` before +implementation starts. Treat holtburger as the client-behavior oracle for this +phase; cross-check wire details against named retail, ACE, Chorizite, and AC2D +before porting. -**2026-05-10 update:** holtburger pulled to `629695a` (+237 commits since -last audit). First parity-pass at -[`docs/research/2026-05-10-holtburger-network-stack-study.md`](../research/2026-05-10-holtburger-network-stack-study.md); -formal opcode coverage matrix (M.1's main deliverable) under construction -at `docs/research/2026-05-10-phase-m-opcode-matrix.md` via parallel -class-by-class agent dispatch. Most relevant recent holtburger commits: -`99974cc` (session crate split + retransmit core), `403bc98` (port-switch -race), `336cbad` (turning + locomotion fix), `797aece` (disconnect -carries client_id). Six "Tier 1" quick-wins identified by the study -(originally tracked as M.0) are folded into M.3 / M.4 / M.6 per the -spec β€” they no longer ship as a separate sub-phase. - -**Sub-lanes:** *(brief summary; the spec has full entry/exit criteria, -conformance gates, and hour estimates for each.)* -- **M.1 β€” Audit & opcode matrix.** Build the per-opcode coverage table - citing holtburger / ACE / named retail / acdream-today / Phase M target. - Status: parity-pass done; matrix construction in flight via per-class - agent dispatch (transport flags + optional headers, GameMessages, - GameEvents, GameActions). 16h. +**Sub-lanes:** +- **M.1 β€” Audit & parity map.** Produce a source-by-source comparison of + acdream `AcDream.Core.Net` and holtburger `holtburger-session`, + `holtburger-protocol`, and `holtburger-core` networking code. Inventory each + packet flag, optional header, session transition, control packet, fragment + path, game message, and game action. Mark each as `parity`, `partial`, + `missing`, or `intentional divergence`. - **M.2 β€” Layer extraction.** Split the low-level stack under `WorldSession` into testable components: `INetTransport`, `PacketCodec`, `ReliablePacketSession`, `FragmentSession`, `GameMessageSession`, and the @@ -537,227 +498,6 @@ conformance gates, and hour estimates for each.)* --- -### Phase N β€” WorldBuilder Rendering Migration - -**Goal:** Stop re-porting AC-specific rendering / dat-handling -algorithms. Depend on a fork of `Chorizite/WorldBuilder` (MIT) for -terrain, scenery, static objects, EnvCells, portals, sky, particles, -texture decoding, mesh extraction, and visibility. Acdream keeps its -own network, physics, animation, motion, UI, plugin, audio, chat -layers (those aren't in WB). - -**Why now (2026-05-08):** the scenery edge-vertex bug at landblock -`0xA9B1` was the third subtle porting bug in a quarter (after the -triangle-Z bug and the hover-over-terrain bug). Even when our code -looked byte-identical to WB's, our output diverged. WB renders the -world correctly; the cost of "we re-port retail algorithms" is now -higher than "we depend on WB's tested port." - -**Design + inventory:** - -- `docs/architecture/worldbuilder-inventory.md` β€” full taxonomy of - what WB has and what we keep porting ourselves. -- `docs/superpowers/specs/2026-05-08-phase-n-worldbuilder-migration-design.md` β€” - parent design doc. -- `docs/superpowers/specs/2026-05-08-phase-n1-scenery-via-wb-helpers-design.md` β€” - N.1 detailed design. - -**Integration model:** fork at -`https://github.com/eriknihlen/WorldBuilder` (already created), git -submodule replacing `references/WorldBuilder/` snapshot, project -references in our solution. Long-lived `acdream` branch in the fork -for our deletions/additions; merge upstream `master` periodically. - -**Lessons from N.1 (apply to N.2-N.10):** - -1. **Per-helper conformance tests work.** The N.1 conformance test caught a - ~180Β° rotation bug in our retail port that had been silently wrong - forever. Write the conformance test BEFORE the substitution in each - sub-phase. - -2. **ACME β‰  Chorizite/WorldBuilder.** ACME is a downstream fork of WB with - additional retail-faithful filters that upstream WB (our submodule) - doesn't have. When a visual discrepancy appears, check ACME's source - (`references/WorldBuilder-ACME-Edition/`) for delta filters BEFORE - investigating retail decomp directly. ACME's deltas tend to come as - coherent units β€” porting one filter without its companions can - over-suppress. - -3. **"Whackamole" is the warning sign.** If a phase generates 3+ visual - regressions on default-on, stop, accept the cosmetic deltas as - ISSUES.md entries, ship the migration. Bugs we leave behind are - debuggable; bugs we never ship are forgotten. - -4. **Subagent-driven execution holds up at this scope.** Fresh subagent - per task with the full task text inline keeps quality high without - polluting the controller's context. Each task should be self-contained - enough that a subagent without session history can complete it. - -**Sub-phases (strangler-fig with feature flags):** - -- **βœ“ SHIPPED β€” N.0 β€” Setup.** Shipped 2026-05-08 (commit `c8782c9`). - WorldBuilder fork at `github.com/eriknihlen/WorldBuilder.git` registered - as git submodule at `references/WorldBuilder/` tracking the `acdream` - branch. `AcDream.Core.csproj` references `WorldBuilder.Shared` + - `Chorizite.OpenGLSDLBackend`. Build green, all 28 scenery/terrain tests - passing. -- **βœ“ SHIPPED β€” N.1 β€” Scenery algorithm calls.** Shipped 2026-05-08. - Replaced `IsOnRoad` / `DisplaceObject` / slope-normal calc / rotation / - scale inside `SceneryGenerator.Generate()` with calls to WB's - `SceneryHelpers` + `TerrainUtils`. Adapter `WbSceneryAdapter` produces - `TerrainEntry[]`. Visual verification at Holtburg confirmed Issue #49's - previously missing edge-vertex trees still visible after the migration; - rotation bug fixed (our retail port's `yawDeg = -(450-degrees)%360` - formula was ~180Β° off from retail's actual `Frame::set_heading` atan2 - round-trip). One known cosmetic difference filed in ISSUES.md - (road-edge tree at landblock 0xA9B1). -- **N.2 β€” Terrain math helpers.** ⚠️ **Blocked on N.5 β€” do not attempt - in isolation.** Originally scoped as a 1-2 day low-risk substitution - of `TerrainSurface.SampleZ` / `SampleSurface` / `SampleSurfacePolygon` - with WB's `TerrainUtils.GetHeight` / `GetNormal`. Audit during N.3 - follow-up uncovered that **WB's `CalculateSplitDirection` uses a - different formula than retail's `FSplitNESW`** (the AC2D-cited - polynomial `0x0CCAC033` / `0x421BE3BD` / `0x6C1AC587` / `0x519B8F25` - that our visual terrain mesh and physics already share). The - formulas pick different cell-diagonals on disputed cells, producing - up to ~2m Z divergence at the same world position. Substituting - physics-side alone would un-sync physics from the still-ours visual - mesh β€” exactly the triangle-Z hover bug class. N.1's conformance - test proved WB's `GetNormal` is good enough for slope-filtering - (boolean walkable check) but NOT that WB's height formula matches - retail. Resolution: fold this work into **N.5** when the visual - mesh switches to WB's renderer in lockstep with physics. Until - then, leave `TerrainSurface` alone. See ISSUE #51. -- **βœ“ SHIPPED β€” N.3 β€” Texture decoding.** Shipped 2026-05-08. `SurfaceDecoder` - now delegates INDEX16 / P8 / A8R8G8B8 / R8G8B8 / A8 to WB's - `TextureHelpers.Fill*`. The A8 divergence (our old code did R=G=B=A=val - always; WB splits additive vs non-additive) was resolved by threading an - `isAdditive` parameter through `DecodeRenderSurface`: terrain alpha masks - pass `isAdditive: true` (matches our prior behavior, preserves the - shader's `.r` blend-weight read), entity surfaces pass - `surface.Type.HasFlag(SurfaceType.Additive)`. Bonus: R5G6B5 + A4R4G4B4 - formats now decode (previously fell to magenta). X8R8G8B8, DXT1/3/5, and - SolidColor remain ours (no WB equivalent). **9 conformance tests prove - byte-identical equivalence per format** before substitution; updated - `SurfaceDecoderTests` to match the new A8 split semantics. Visual - verification at Holtburg passed 2026-05-08 β€” no texture regressions. -- **βœ“ SHIPPED β€” N.4 β€” Rendering pipeline foundation.** Shipped 2026-05-08. - WB's `ObjectMeshManager` is integrated as the production mesh pipeline - behind `ACDREAM_USE_WB_FOUNDATION=1` (default-on). The integration is - three pieces: `WbMeshAdapter` (single seam owning the WB pipeline, - drains the staged-upload queue per frame, populates - `AcSurfaceMetadataTable` for translucency / luminosity / fog), - `WbDrawDispatcher` (production draw path β€” groups all visible - (entity, batch) pairs, uploads matrices in a single `glBufferData`, - fires one `glDrawElementsInstancedBaseVertexBaseInstance` per group - with `BaseInstance` slicing the shared instance VBO), and the - `LandblockSpawnAdapter` + `EntitySpawnAdapter` bridge that wires our - streaming loader to WB's `IncrementRefCount` / `PrepareMeshDataAsync` - lifecycle (atlas tier vs per-instance customized). - Issue #47 (close-detail mesh) preserved; sky pass structurally - independent of the WB foundation. Perf wins shipped as part of N.4: - per-entity AABB frustum cull, opaque front-to-back sort, palette-hash - memoization. Legacy `InstancedMeshRenderer` retained as flag-off - fallback until N.6 fully retires it. Plan archived at - `docs/superpowers/plans/2026-05-08-phase-n4-rendering-foundation.md`. -- **βœ“ SHIPPED β€” N.5 β€” Modern rendering path.** Shipped 2026-05-08. - **Rebranded from "Terrain rendering" 2026-05-08 after N.4 perf - review.** Lifted `WbDrawDispatcher` onto bindless textures - (`GL_ARB_bindless_texture`) + `glMultiDrawElementsIndirect`. Per-frame - entity rendering: 3 SSBO uploads (instance matrices @ binding=0, batch - data @ binding=1, indirect commands) + 2 indirect calls (opaque + - transparent). ~12-15 GL calls per frame regardless of group count, down - from hundreds-of-per-group in N.4. CPU dispatcher: 1.23 ms/frame median - at Holtburg (1662 groups, ~810 fps). All textures on the modern path use - 1-layer `Texture2DArray` + `sampler2DArray`; legacy callers retain - `Texture2D` via the parallel `TextureCache` path until N.6 retires them. - Three gotchas in memory (`project_phase_n5_state.md`): texture target - lock-in, bindless Dispose two-phase order, GL_TIME_ELAPSED double- - buffering. Plan archived at - `docs/superpowers/plans/2026-05-08-phase-n5-modern-rendering.md`. -- **βœ“ SHIPPED β€” N.5b β€” Terrain on the modern rendering path.** Shipped - 2026-05-09. **Path C** (mirror WB's `TerrainRenderManager` pattern but - consume `LandblockMesh.Build` for retail-formula compliance). Path A - (substitute WB's `CalculateSplitDirection`) killed during pre-implementation - divergence test: WB's formula disagrees with retail's `FSplitNESW` - (addr `00531d10`) on **49.98%** of cells across `tests/AcDream.Core.Tests/Terrain/SplitFormulaDivergenceTest.cs`'s - sweep β€” wholly incompatible with our shared physics + visual mesh. - Path B (fork-patch WB to use retail's formula) rejected for permanent - maintenance burden. Path C ships the architectural pattern (single - global VBO/EBO + slot allocator + bindless atlas + `glMultiDrawElementsIndirect`) - while keeping retail's formula via `LandblockMesh.Build` β†’ - `TerrainBlending.CalculateSplitDirection`. `TerrainModernRenderer` + - `terrain_modern.vert/.frag` shipped, `TerrainChunkRenderer` + - `TerrainRenderer` + legacy `terrain.vert/.frag` deleted in T9. - Closes ISSUE #51. **Perf reality check:** at radius=5 in Holtburg, - modern is ~4Γ— SLOWER on CPU than legacy was (6.4 Β΅s vs 1.5 Β΅s median; - legacy collapsed radius=5's visible LBs into one `glDrawElements` - via 16Γ—16-LB chunking). Architectural wins (zero `glBindTexture`/frame, - constant-cost dispatch as A.5 raises radius, per-LB frustum cull) - manifest at higher radius. Spec acceptance criterion #5 was wrong; - amended via `docs/plans/2026-05-09-phase-n5b-perf-baseline.md`. Plan - archived at `docs/superpowers/plans/2026-05-09-phase-n5b-terrain-modern.md`. -- **βœ“ SHIPPED β€” N.6 slice 1 β€” GPU timing fix + radius=12 perf baseline.** Shipped 2026-05-11. - Fixed the gpu_us double-buffering bug in `WbDrawDispatcher` (ring-of-3 - query slots, read-before-overwrite, vendor-neutral across AMD/NVIDIA/Intel - desktop GL). Added env-gated surface-format histogram dump in `TextureCache` - for atlas-opportunity audit. Captured authoritative baseline at Holtburg - radii 4 / 8 / 12 (standstill + walking) with the now-working `gpu_us` - diagnostic. Plan + spec at `docs/superpowers/{specs,plans}/2026-05-11-phase-n6-slice1-*.md`. - Baseline numbers + next-phase recommendation at - [docs/plans/2026-05-11-phase-n6-perf-baseline.md](2026-05-11-phase-n6-perf-baseline.md). -- **N.6 slice 2 β€” Perf polish cleanup.** **Planned β€” deferred until after C.1.5 - (PES emitter wiring) per the baseline doc's recommendation.** Builds on - slice 1's measurement. Scope: retire the legacy `Texture2D`/`sampler2D` path - in `TextureCache` (currently kept for Sky + Debug + particle paths now that - Terrain has migrated); delete orphan `mesh.frag` (verify zero callers post-N.5 - amendment); decide bindless-everywhere vs legacy-island for the remaining - `sampler2D` consumers. **Dropped from slice 2 scope per baseline data**: - WB atlas adoption and persistent-mapped buffers β€” both target GPU/sampler - throughput but the baseline shows GPU is wildly under-utilized (max gpu_us - p95 ~600 Β΅s vs 16,600 Β΅s frame budget). Slice 2 reduces to a ~1-day cleanup. - Plan + spec written when work begins. **Estimate: ~1 day once C.1.5 lands.** -- **N.7 β€” EnvCells / dungeons.** Replace EnvCell rendering with WB's - `EnvCellRenderManager` + `PortalRenderManager` on top of N.4's - foundation. **Estimate: 1-2 weeks** (was 2-3 β€” naturally smaller now - that infrastructure is shared). -- **N.8 β€” Sky + particles.** Replace sky rendering + particle pipeline - (#36 / C.1 work) with WB's `SkyboxRenderManager` + - `ParticleEmitterRenderer`. **Estimate: ~1 week** (was 1.5-2 β€” C.1 - already shipped most of this; N.8 is glue + sampler-object reuse). -- **N.9 β€” Visibility / culling.** Replace `CellVisibility` + - `FrustumCuller` with WB's `VisibilityManager`. **Estimate: ~1 week** - (was 3-5 days, slight bump for streaming-loader interaction). -- **N.10 β€” GL infrastructure consolidation (optional).** Replace our - `Shader` / `TextureCache` / `SamplerCache` plumbing with WB's - `ManagedGL*` wrappers + `OpenGLGraphicsDevice`. **Largely subsumed by - N.4** β€” `OpenGLGraphicsDevice` arrives as the host of `ObjectMeshManager` - and atlas. May not need a dedicated phase; revisit after N.6. - -**Estimated calendar:** **2.5-3 months / 9-13 engineering weeks for -N.4-N.9 (N.10 likely subsumed; N.2 folded into N.5; N.3 shipped).** -Revised 2026-05-08 after recognizing N.4-N.6 are one rendering rebuild -on shared infrastructure rather than three independent substitutions. - -**Each sub-phase:** -- Ships behind `ACDREAM_USE_WB_=1` flag. -- Has its own conformance test (side-by-side against existing path). -- Visual verification before flag becomes default-on. -- Old code deleted after default-on lands cleanly. - -**N.2-N.10 detailed specs are NOT yet written** β€” each gets its own -brainstorm + spec when we reach it. - -**Acceptance:** -- All 10 sub-phases shipped, feature flags removed, old rendering code - paths deleted. -- Visual verification at Holtburg + Foundry statue + a representative - dungeon shows no regression vs Phase C.1. -- WB upstream merges into our `acdream` branch are clean (or have - documented conflict-resolution patterns). - ---- - ### Phase J β€” Long-tail (deferred / low-priority) Not detailed here; each gets its own brainstorm when it becomes relevant. diff --git a/docs/plans/2026-04-29-movement-collision-conformance.md b/docs/plans/2026-04-29-movement-collision-conformance.md index 05ab3b0..8bfc2c1 100644 --- a/docs/plans/2026-04-29-movement-collision-conformance.md +++ b/docs/plans/2026-04-29-movement-collision-conformance.md @@ -92,35 +92,6 @@ Goal: make every bad movement outcome explainable. - Build real-DAT fixture capture for known walls, building ledges, rooftops, slopes, landblock seams, and dungeon entrances. -Current shipped slices: - -- 2026-04-30: cdb + TTD retail-observer toolchain (`tools/pdb-extract/`, - `tools/ttd-record.ps1`, `tools/ttd-query.ps1`) with PDB pairing checker - and ring-buffer trace replay. The "retail observer harness" line item. -- 2026-04 (pre-L.2 rename): `ACDREAM_DUMP_MOVE_TRUTH` paired - outbound/server-echo dumper in `GameWindow` covers outbound packet - fields + server echo + correction delta with cell-id mismatch. -- Pre-L.2: scenario-specific dumps `ACDREAM_DUMP_MOTION`, - `ACDREAM_DUMP_STEEP_ROOF`, `ACDREAM_DUMP_STEPUP`, - `ACDREAM_DUMP_EDGE_SLIDE` for the codepaths hit during prior bug chases. -- 2026-05-12 (slice 1): general-purpose probes via new - `AcDream.Core.Physics.PhysicsDiagnostics` static class. - `ACDREAM_PROBE_RESOLVE` emits one `[resolve]` line per - `PhysicsEngine.ResolveWithTransition` call (input/output pos+cell, - ok-vs-partial, grounded-in, contact-plane status, wall normal if hit, - walkable polygon valid, moving entity id). - `ACDREAM_PROBE_CELL` emits one `[cell-transit]` line per - `PlayerMovementController.CellId` change with oldβ†’new + position + - reason tag (`resolver`/`teleport`). Both flippable live via the - DebugPanel "Diagnostics" section β€” checkbox toggles take effect on - the next resolve, no relaunch required. - -Remaining L.2a work: contact-plane probe (general, not just steep-roof), -ShadowObjectRegistry hit log ("you collided with entity X"), water probe, -real-DAT fixture-capture pipeline, and folding the older sticky-at-startup -`ACDREAM_DUMP_*` flags into `PhysicsDiagnostics` for unified runtime -toggling. - ### L.2b - Movement Wire / Contact Authority Goal: stop sending movement packets that claim more certainty than the local @@ -169,41 +140,6 @@ fallback. - Audit `Setup.Radius` and cylinder fallback behavior against retail before relying on them for conformance. -Current sub-direction (revised 2026-05-13 evening after slice 1 + 1.5 -shipped and Holtburg-doorway capture analyzed β€” third reframe): -L.2d as scoped ("shape fidelity: Sphere / CylSphere / Building Objects") -is **essentially closed at the Holtburg site that motivated this phase**. -Building BSP collision works correctly β€” the slice-1.5 probe captured -real triangles in plausible world positions for `gfxObj=0x01000A2B` with -`bspR=13.99m`. The 121 wall hits the L.2a probe attributed to -`obj=0xA9B47900` were **side effects of the player already being pushed -back by a separate Door cylinder entity** at the same doorway threshold. - -The actual blocker is a server-spawned **Door** entity β€” Setup -`0x020019FF` named `"Door"` β€” that ACE places at each Holtburg-town -building threshold (five doors total observed across `0xA9B40029`, -`0xA9B40154`, `0xA9B40155`). It registers as a Cylinder shadow entry -via the server-spawn path; its Cylinder collision blocks the player -walking into the doorway. That's **door-state handling**, a different -class of problem from L.2d's shape-fidelity scope β€” it touches network -(`CreateObject` PhysicsState bits), interaction (Use action on door -entity), animation (door open/close), and collision-state-toggle. - -Recommend: **leave L.2d in "watch-and-wait" mode** with slice 1's probe -infrastructure in place. No more L.2d slices until a NEW shape-fidelity -bug is observed at a different site (dungeon walls, stairs, roofs) with -the probe-armed client. The door-state work becomes its own sub-phase -(probably nested under B.4 interaction or filed as a new L.2 sub-phase -like L.2g) scoped separately. - -Full slice 1 + 1.5 handoff: -[docs/research/2026-05-13-l2d-slice1-shipped-handoff.md](../research/2026-05-13-l2d-slice1-shipped-handoff.md). -Design spec (now mostly historical, framing was wrong but probe -infrastructure shipped from it): -[docs/superpowers/specs/2026-05-13-l2d-cbuildingobj-collision-design.md](../superpowers/specs/2026-05-13-l2d-cbuildingobj-collision-design.md). -Predecessor L.2a handoff: -[docs/research/2026-05-12-l2a-shipped-l2d-handoff.md](../research/2026-05-12-l2a-shipped-l2d-handoff.md). - ### L.2e - Cell Ownership: Outdoor Seams, CELLARRAY, cell_bsp Goal: the resolver knows which cell owns the movement and which adjacent cells @@ -228,62 +164,6 @@ client sees when observing acdream. - Require conformance notes in tests or research docs for every AC-specific algorithm ported under L.2. -### L.2g - Dynamic PhysicsState Toggling - -Goal: server-driven post-spawn state changes (chiefly `ETHEREAL` flips) are -honored by the local collision stack. - -Triggered 2026-05-12 evening by the L.2d slice 1.5 trace: the Holtburg -doorway blocker is a closed Door entity (Setup `0x020019FF`) whose -`PhysicsState.Ethereal` bit flips when the player Uses the door. The L.2d -shape-fidelity work doesn't cover this β€” the door's collision shape is -already correct; what's missing is honoring the *runtime* state change. - -Scope is intentionally narrow: - -- Parse inbound `GameMessageSetState (opcode 0xF74B)`. -- Plumb the new `PhysicsState` value into `ShadowObjectRegistry`'s cached - per-entity state so the existing `CollisionExemption.IsExempt(...)` check - sees the up-to-date bits. -- Verify the Holtburg inn-door scenario: walk into doorway β†’ blocked, Use - door β†’ door swings open AND player can walk through, auto-close after - 30s β†’ door closes AND player is blocked again. -- Confirm the existing `UpdateMotion` pipeline drives `(NonCombat, On/Off)` - on non-creature entities (door swing animation). If not, one-line fix. - -Excluded from L.2g scope (deferred): - -- Door-specific UX polish: "door is locked" sound, creature-AI bump-open. -- Any Door-specific class hierarchy β€” generic state-flip infrastructure - is enough; doors are the verification scenario, not a privileged case. - -Lane: informal sixth lane "dynamic state." The existing five-lane table -treats per-entity state as static-after-spawn; L.2g makes it dynamic. - -Full design spec: -[docs/superpowers/specs/2026-05-12-l2g-dynamic-physicsstate-design.md](../superpowers/specs/2026-05-12-l2g-dynamic-physicsstate-design.md). - -M1 critical path: this slice unblocks the *"open the inn door"* demo -scenario. - -Current shipped slice (2026-05-12): - -| Commit | Subject | -|---|---| -| `2459f28` | `feat(phys L.2g slice 1): inbound SetState (0xF74B) parser` | -| `d538915` | `feat(phys L.2g slice 1): ShadowObjectRegistry.UpdatePhysicsState` | -| `536a608` | `feat(phys L.2g slice 1): WorldSession dispatches SetState (0xF74B) + hex probe` | -| `108e386` | `feat(phys L.2g slice 1): GameWindow routes SetState + extends [entity-source] log` | - -Slice 1 is CODE-COMPLETE: parser + registry mutator + WorldSession -dispatcher + GameWindow subscriber. 6 new tests pass (3 parser + 3 -registry). Build clean. Per-commit + final integration code reviews -all approved. **Visual verification deferred to Phase B.4b** β€” the -inbound SetState chain can't fire at runtime until B.4b finishes the -outbound Use handler. See -[docs/research/2026-05-12-l2g-slice1-shipped-handoff.md](../research/2026-05-12-l2g-slice1-shipped-handoff.md) -for full evidence + the 4 minor + 1 Important review notes. - ## Named Retail Anchors Primary source: `docs/research/named-retail/acclient_2013_pseudo_c.txt`. diff --git a/docs/plans/2026-05-08-phase-n5-perf-baseline.md b/docs/plans/2026-05-08-phase-n5-perf-baseline.md deleted file mode 100644 index 6d14bb8..0000000 --- a/docs/plans/2026-05-08-phase-n5-perf-baseline.md +++ /dev/null @@ -1,72 +0,0 @@ -# Phase N.5 perf baseline - -**Captured:** 2026-05-08, against N.5 head (post-Task 12) on local machine. -**Method:** `ACDREAM_WB_DIAG=1` + character at Holtburg spawn position + -roaming. Numbers below are 5-second window medians from `[WB-DIAG]`. - -## Holtburg courtyard (steady state) - -| Metric | N.5 measured | N.4 (estimated*) | Gate | -|---|---|---|---| -| CPU dispatcher (median) | **1227 Β΅s / frame** | β‰₯2500 Β΅s / frame | ≀70% of N.4 β†’ **PASS** | -| CPU dispatcher (p95) | 1303 Β΅s / frame | β€” | β€” | -| GPU rendering (median) | unmeasured (see below) | β€” | within Β±10% β€” **DEFERRED** | -| `drawsIssued` per 5s | 4.85M (= 1662 groups Γ— ~580 fps) | far higher per frame | β€” | -| `drawsIssued` per pass (CPU GL calls) | **2** (1 opaque + 1 transparent indirect) | ~hundreds per pass | ≀5 β†’ **PASS** | -| `groups` (working set) | 1662 | ~similar | sanity | -| Frame rate (inferred) | ~810 fps | ~100-200 fps | substantial uplift | - -*N.4 baseline NOT measured directly in this run. The "β‰₯2500 Β΅s / frame" -estimate assumes N.4's per-group glBindTexture + glBindBuffer + -glDrawElementsInstancedBaseVertexBaseInstance hot path costs β‰₯1.5 Β΅s per -group and N.4 has ~1700 groups in this scene, putting the GL portion alone -at ~2.5 ms before adding the entity-walk overhead. N.5's measurement -includes ALL dispatcher work (entity walk + group bucketing + 3 SSBO -uploads + 2 indirect calls + state changes) at 1230 Β΅s total β€” comfortably -half of the lower bound estimate. - -## Acceptance gates (spec Β§8.3) - -- [x] **Visual identity to N.4** β€” confirmed at Task 10 USER GATE: Holtburg - courtyard renders identical, no missing entities, no z-fighting, no - exploded parts. -- [x] **CPU dispatcher time ≀ 70% of N.4** β€” N.5 measures 1.23 ms/frame - median; estimated N.4 β‰₯2.5 ms/frame; **comfortably under 70%**. -- [ ] **GPU rendering time within Β±10% of N.4** β€” DEFERRED. The - `GL_TIME_ELAPSED` query polling never reports `avail != 0` in our - single-frame poll loop; the driver hasn't finalized the result by the - time we check. The fix is double-buffering (issue queryA on frame N, - read result on frame N+2). N.6 perf polish item. -- [x] **`drawsIssued` ≀ 5 per pass (CPU GL calls)** β€” exactly 2 indirect - calls per frame regardless of scene size. -- [x] **All tests green** β€” 70/70 in - `FullyQualifiedName~Wb|FullyQualifiedName~MatrixComposition`. - 8 pre-existing failures in `MotionInterpreter` / `BSPStepUp` / - `PositionManager` / `PlayerMovementController` / `Dispatcher` are - carry-forward from before N.5 and unrelated to rendering. -- [N/A] **`ACDREAM_USE_WB_FOUNDATION=0` still works** β€” escape hatch - formally retired in N.5 ship amendment. `InstancedMeshRenderer`, - `StaticMeshRenderer`, and `WbFoundationFlag` deleted. Missing - bindless throws `NotSupportedException` at startup with a clear - error message. No fallback path. - -## Visual verification (Task 14) - -- [x] **Holtburg courtyard** β€” PASS at Task 10 USER GATE. -- [ ] **Foundry interior / dense static-object scene** β€” TODO Task 14. -- [ ] **Indoor β†’ outdoor cell transition** β€” TODO Task 14. -- [ ] **Drudge / character close-up (Issue #47 close-detail mesh)** β€” TODO Task 14. -- [ ] **Magic content (Decision 2 additive fallback check)** β€” TODO Task 14. -- [ ] **Long-session sanity** β€” DEFERRED (N.6 watchlist; not load-bearing for ship). - -## Open follow-ups for N.6 - -1. **GPU timer query double-buffering** β€” the current single-frame poll - pattern never sees `QueryResultAvailable=true`. Issue queryA on frame N, - queryB on frame N+1, read queryA on frame N+2. ~30 lines of state. -2. **Direct N.4 vs N.5 perf comparison** β€” re-run with `git checkout`ed N.4 - SHIP (`c445364`) for a side-by-side measurement. Not load-bearing but - useful for N.6 ship message. -3. **Persistent-mapped buffers** β€” Decision 7 deferral. If profiling shows - the per-frame `glBufferData` cost is the residual hot spot, layer it on - top of the modern path. diff --git a/docs/plans/2026-05-09-phase-n5b-perf-baseline.md b/docs/plans/2026-05-09-phase-n5b-perf-baseline.md deleted file mode 100644 index c5f9136..0000000 --- a/docs/plans/2026-05-09-phase-n5b-perf-baseline.md +++ /dev/null @@ -1,98 +0,0 @@ -# Phase N.5b β€” terrain perf baseline - -**Captured:** 2026-05-09 at Holtburg town dueling field, radius=5, ~30s standstill. - -## Methodology - -Same build (commit at perf measurement: `da56063`), `ACDREAM_WB_DIAG=1`. The build -included a TEMPORARY `ACDREAM_LEGACY_TERRAIN=1` env-var toggle (since retired in T9 -deletion of the legacy renderer) that routed Draw through the legacy renderer for -direct comparison. Both renderers were constructed and fed AddLandblock / RemoveLandblock -in parallel; only one drew per frame; the same Stopwatch wrapped whichever ran. - -## Numbers - -| Renderer | cpu_us median | cpu_us p95 | draws/frame | Visible LBs | -|---|---|---|---|---| -| **Legacy** (`TerrainChunkRenderer`) | 1.5 | 3.0 | 1 (1 chunk) | 132-143 (whole chunk) | -| **Modern** (`TerrainModernRenderer`) | 6.4-7.0 | 9-14 | ~36-51 | 36-51 (per-LB cull) | - -(Legacy `draws=1` because its 16Γ—16-LB chunking collapses radius=5's 121 visible -landblocks into a single chunk, dispatched as one `glDrawElements`. Modern issues -one `glMultiDrawElementsIndirect` with N=36-51 sub-commands.) - -## Acceptance criterion - -The N.5b spec acceptance criterion 5 read: "CPU dispatcher time at radius=5 β‰₯10% -lower than today's per-LB-binds path." The captured numbers show modern is ~4Γ— -HIGHER on CPU at radius=5. **The criterion was wrong** β€” at radius=5 in Holtburg, -legacy's chunked path was already collapsed to one draw call. The architectural -wins of multi-draw indirect manifest at higher chunk counts (A.5 territory). - -The spec is amended via this doc: ship N.5b on visual identity + structural -correctness rather than CPU savings at radius=5. - -## Architectural wins of the modern path (real, even when CPU is higher) - -1. **Zero `glBindTexture` per frame.** Bindless atlas handles are made resident - once at startup; the modern shader samples via `sampler2DArray(uvec2 handle)`. - Legacy issued 2 `glBindTexture(Texture2DArray)` calls per frame. - -2. **Constant-cost dispatch.** As A.5 raises the streaming radius (next phase), - the visible chunk count grows. Legacy scales linearly: at radius=10 (4Γ— chunks) - it's 4 `glDrawElements` calls; at radius=15 (β‰₯9 chunks) it's 9+ calls. Modern - stays at exactly 1 `glMultiDrawElementsIndirect` regardless. - -3. **Per-LB frustum culling.** Legacy culled at chunk granularity (16Γ—16 LBs); - modern culls per-LB. At a typical Holtburg view, ~36-51 of 132 loaded LBs are - actually visible; legacy drew the entire 132-LB chunk (3.5Γ— the visible work - pushed to GPU vertex/fragment stages, even though CPU dispatch was cheap). - -## Why modern's CPU was higher at radius=5 - -Per-frame work in modern (in microseconds-ish budget on this scene): -- Walk all loaded slots checking visibility (~120 slots) β†’ AABB test each -- Build DEIC array (51 entries Γ— 20 bytes = 1020 bytes) -- `glBufferSubData(DRAW_INDIRECT_BUFFER, ...)` β€” driver memcpy -- 2Γ— `glProgramUniform2(..., handle.low, handle.high)` for atlas handles -- `glBindVertexArray` + `glMemoryBarrier(GL_COMMAND_BARRIER_BIT)` + `glMultiDrawElementsIndirect` - -Legacy's per-frame work: -- Bind 2 textures -- Bind one VAO (the chunk) -- One `glDrawElements` - -The DEIC array build + buffer upload alone is ~3-5Β΅s at radius=5 on this hardware, -which is the bulk of the modern overhead. At higher radius, this overhead amortizes: -the buffer is similar size, but the alternative (legacy's N draws) grows. - -## Follow-up work - -- **A.5 (next phase)** will exercise the higher-radius case where modern wins. - Capture a fresh baseline at radius=8 / 10 once A.5 lands. -- **N.6 perf polish** can investigate persistent-mapped buffers for the indirect - buffer, which would eliminate the per-frame `glBufferSubData`. Likely small win - at radius=5 (single ~1KB upload), bigger at higher radii. -- **GPU-side culling** (compute shader generating the DEIC array directly into - the indirect buffer) eliminates the CPU slot walk + DEIC build entirely. N.6 or - later territory; only worth it if profiling shows the CPU walk is hot. - -## Lessons captured to memory - -`memory/project_phase_n5b_state.md` records the high-value gotchas surfaced -during N.5b implementation. Three particularly bitable ones: - -1. **`uniform sampler2DArray` + `glProgramUniformHandleARB` is unreliable.** Some - drivers (NVIDIA Windows in this case) reject the combination with - `GL_INVALID_OPERATION`. Use the `uniform uvec2` + `sampler2DArray(handle)` - constructor pattern instead β€” N.5's mesh_modern uses this, and N.5b's - terrain_modern adopted it after the black-terrain regression. - -2. **`MaybeFlushTerrainDiag` underflow.** A naive median calc (`copy[N - nz/2]`) - underflows to `copy[N]` when only one sample has been recorded. Use - `copy[N - 1 - (nz - 1) / 2]` instead. - -3. **Visual gate must actually be visually confirmed.** "Go" doesn't mean - "verified." During N.5b's gate the user said "go" without launching, which - masked the black-terrain regression for hours. The gate must include the - user reporting actual visual confirmation, not assent to proceed. diff --git a/docs/plans/2026-05-10-perf-tiers-2-3-roadmap.md b/docs/plans/2026-05-10-perf-tiers-2-3-roadmap.md deleted file mode 100644 index c7d9883..0000000 --- a/docs/plans/2026-05-10-perf-tiers-2-3-roadmap.md +++ /dev/null @@ -1,195 +0,0 @@ -# Performance Tiers 2 + 3 β€” Future Roadmap - -**Created:** 2026-05-10 during Phase A.5 polish. -**Status:** Future planning β€” not for current execution. -**Context:** A.5 shipped two-tier streaming with the entity dispatcher landing at ~3.5ms median (post-Bug-A and Bug-B fixes). Tier 1 (entity-classification cache) lands as A.5 polish and brings the dispatcher inside the 2.0ms spec budget. Tiers 2 + 3 are the "next big perf wins" beyond Tier 1. - ---- - -## Background β€” why this exists - -Discussion captured 2026-05-10: user observed 200-240 FPS at radius=12 on a Radeon 9070 XT @ 1440p and asked why an "old game like AC" doesn't deliver Unreal-level (1000+ FPS) on this hardware. - -The honest answer: the bottleneck is *architectural*, not hardware. The CPU is single-threaded and rebuilds the entire draw plan from scratch every frame. Modern engines pre-bake static-world batches at content-cook time and rebuild only what changes. - -AC's design β€” server-spawned per-entity world streamed at runtime β€” doesn't naturally batch the way Unreal's pre-cooked content does. Closing the gap requires backporting modern techniques while preserving AC's data model. Tiers 2 and 3 are that backporting work. - ---- - -## Tier 2 β€” Static/dynamic split with persistent groups - -**Estimated effort:** ~10-15 days (2-week phase). -**Estimated win:** entity dispatcher ~3.5ms β†’ **~0.5-1ms median** at radius=12. -**Total frame time:** ~4-5ms β†’ **~2-3ms = 400-600 FPS at standstill.** - -### The core idea - -Today, `WbDrawDispatcher._groups` (the dictionary of "(mesh + texture + blend) β†’ list of instances to draw") is cleared and rebuilt from scratch every frame. - -For trees, rocks, buildings, and other static entities (~95% of the world), the answer is identical every frame forever. Tier 2 makes the static-group instance buffers **persistent GPU-resident data**, just like Unreal's pre-baked world. The CPU only orchestrates "which groups are visible" per frame. - -### Architectural shift - -```csharp -class StaticInstancedGroup -{ - public GroupKey Key; - public Matrix4x4[] Matrices; // grown as entities spawn - public BitArray ActiveSlots; // for free-list reuse - public bool NeedsGpuUpload; // dirty flag for delta upload - public Dictionary EntityToSlot; // for despawn lookup - public uint InstanceBufferOffset; // start of group's slice in global SSBO -} -``` - -**On entity spawn (atlas-tier static):** allocate a slot in each relevant group, write the matrix, mark dirty. - -**On entity despawn:** free the slot, mark dirty. - -**Per frame:** -- Static groups: LB-cull each group (cheap). For visible groups, flag for draw. **No matrix copy. No list rebuild.** -- Dynamic entities (~50 NPCs/players): today's per-frame walk-and-classify. Keeps the existing slow path for things that legitimately change every frame. -- Upload only the dirty groups' matrix slices (delta upload, not full reupload). -- Issue 2 multi-draw-indirect calls. - -### Sub-decisions - -**Frustum cull granularity at the group level:** at group level you can't reject individual instances; you draw the whole group or none of it. Two strategies: - -- **Per-LB subgroups:** split each group into per-landblock subgroups. LB-frustum-culls reject subgroups whose LB is invisible. ~2K groups Γ— ~5 LBs per group on average = ~10K subgroups. Each subgroup AABB cull is ~0.3 Β΅s β†’ ~3 ms per frame. Roughly a wash with today's per-entity cull. -- **Per-instance GPU cull (Tier 3):** compute pre-pass on the GPU writes which instances are visible to a draw-indirect buffer. ~0.05ms CPU. The right long-term answer. - -For Tier 2 alone, per-LB subgroups are the recommended approach β€” keep CPU culling, just at coarser granularity than per-entity. - -**Dynamic entities crossing LB boundaries:** when an NPC walks across a landblock boundary, it stays in the same group key but its "spatial bucket" changes. Solution: dynamic entities are tracked in a single global "dynamic group" outside the per-LB structure; they don't need spatial bucketing because there are only ~50 of them. - -**Palette override invalidation:** server event swaps an NPC's clothing color β†’ group key changes. Treat as despawn-from-old + spawn-into-new. NPCs are dynamic so this just rebuckets them. - -**Animation overrides on static entities:** static entities don't animate. Trees don't bend (foliage wave is a vertex shader effect, not a group-key change). Buildings don't move. So the static path never invalidates. - -**EnvCell visibility:** dungeon entities are gated by per-cell visibility state. Need to track which group instances are tied to which cell, and during visibility cull, gate per-cell. Keep using existing `ParentCellId` field on WorldEntity. - -**Streaming load/unload integration:** when an LB unloads, all its static entity matrices need to be removed from their groups. Free-list management. Matches existing `LandblockSpawnAdapter` lifecycle. - -### Effort breakdown - -| Task | Days | -|---|---| -| Design + invariants document | 2 | -| Spawn-time slot allocator + free-list | 3 | -| Per-frame visibility + dirty-flag delta upload | 2 | -| Dynamic entity path (NPCs, projectiles) | 2 | -| Invalidation (palette/ObjDesc events) | 2 | -| EnvCell visibility integration | 1 | -| Streaming load/unload integration | 1 | -| Conformance testing | 2-3 | -| **Total** | **~10-15 days** | - -### Risks - -- **Slot management bugs** = double-frees or leaks (entities draw at random positions β€” visible). -- **Invalidation bugs** = stale matrices (entity teleports back to spawn point when palette changes). -- **Dynamic entity tracking** adds complexity around the static/dynamic boundary. - -### Mitigations - -- **Conformance test:** render a fixed scene through both pipelines, compare draw output. Adds CI infrastructure. -- **Per-frame validation in debug:** walk all groups, assert no orphan slots. -- **Hash invariant test:** static entities should produce stable group keys frame-over-frame. Add a debug assertion that fires once per frame in Debug builds. - ---- - -## Tier 3 β€” GPU-side culling (compute pre-pass) - -**Estimated effort:** ~1 month (longer phase). -**Estimated win:** entity dispatcher ~0.5-1ms (post-Tier-2) β†’ **~0.05ms median.** -**Total frame time:** ~2-3ms β†’ **~1.5-2ms = 600-1000+ FPS at standstill.** - -### The core idea - -Today (and after Tier 2), the CPU does per-LB or per-subgroup frustum culling and tells the GPU which groups to draw. - -Tier 3 moves per-instance frustum cull to the GPU via a compute shader pre-pass. The CPU just uploads "here are all 1M instance matrices" once; the GPU compute shader writes which ones are visible to a draw-indirect buffer; the rasterizer draws only those. - -This is the level Unreal is at. With this, per-frame CPU work for the entity dispatcher becomes essentially "tell the GPU what to do" + a tiny scratch upload. - -### Why Tier 3 needs Tier 2 first - -Without Tier 2's persistent group structure, GPU culling has nothing stable to operate on. The compute shader needs an addressable "here are the static instances" buffer to read from; that buffer only exists after Tier 2. - -### Sub-decisions to be made - -**Compute shader API:** OpenGL 4.3+ compute shaders are sufficient. We're already at GL 4.3+ for bindless. No additional capability requirement. - -**Indirect draw command generation:** the compute shader writes a `DrawElementsIndirectCommand[]` buffer per pass. Render thread issues `glMultiDrawElementsIndirect` reading from that buffer. No CPU readback. - -**LOD selection:** opportunity to add per-instance LOD selection in the compute shader (distance-based mesh detail). Not needed for A.5's scope; could be a Tier 4 follow-up. - -**Per-light shadow map culling:** if shadows ship, GPU culling extends naturally to per-light frustum cull. Significant win for shadow rendering. - -### Effort breakdown - -| Task | Days | -|---|---| -| Compute shader design + GLSL implementation | 4 | -| Buffer layout coordination with Tier 2 | 2 | -| Silk.NET compute dispatch integration | 3 | -| Indirect command compaction logic | 4 | -| LOD selection (optional, ~stretch) | 4 | -| Validation: per-instance cull matches CPU cull within epsilon | 3 | -| Conformance + regression testing | 5 | -| **Total** | **~21-25 days, ~1 month** | - -### Risks - -- **GPU stalls** if the compute shader takes longer than expected (esp. on lower-end GPUs). -- **Sync overhead** between compute pre-pass and rasterizer pass. -- **Debugging difficulty** β€” GPU compute bugs are harder to diagnose than CPU bugs. - -### Mitigations - -- **Profile-driven design:** measure compute shader runtime on target hardware before committing. -- **Fallback path:** keep CPU cull as a runtime-toggleable option (env var) so we can A/B compare. -- **GPU debugging tools:** RenderDoc captures + frame-by-frame compute shader inspection. - ---- - -## When to schedule these - -**Tier 2:** -- Best fit: dedicated 2-week phase after a SHIP cycle. Treat it like a Phase B/C/N (i.e., name it Phase A.6 or N.7). -- Trigger: user wants to push radius beyond 12 (e.g., to 15 or 20 for true continent-scale horizon). -- Trigger: user wants to add 100+ active NPCs in a city without dropping below 240Hz. - -**Tier 3:** -- Best fit: after Tier 2 has been live and stable for at least one cycle. -- Trigger: shadow map work begins (GPU cull + shadow cull share the same compute pre-pass infrastructure). -- Trigger: user wants 500+ FPS sustained for very-high-refresh scenarios (360Hz monitors, future hardware). - -**Both:** -- Don't bundle with other phases. These are dedicated perf phases with their own brainstorm + spec + plan + SHIP cycles. - ---- - -## What's "free" or smaller (out of Tier 1/2/3 scope but worth noting) - -- **Plumb `JobKind` properly through `BuildLandblockForStreaming`** (~30 min). Today's Bug A patch wastes worker-thread CPU on hydration that gets thrown away for far-tier. Cleaner code, slight CPU savings on worker. -- **Eliminate `ToEntries` adapter allocation in `Draw`** (~15 min). Tiny win (~25 KB / frame). Could fold into Tier 1. -- **Persistent-mapped indirect buffer** (~2 days). Today's `glBufferData` per frame becomes a pre-mapped persistent buffer. Marginal win on RDNA 4; meaningful on lower-end GPUs. -- **Multi-thread mesh-build worker pool** (~1 day). 2.7s first-traversal horizon-fill drops to 0.7s with 4 workers. UX win on first walk-into-region. - -These are good candidates for a "perf polish" mini-phase or to backfill into Tier 2. - ---- - -## The architectural ceiling - -Even with all three tiers, **a faithful AC client written in C# with bindless OpenGL tops out around 800-1500 FPS at radius=12 on RDNA 4 hardware**. Beyond that requires: - -- Native C++ rendering core (eliminate .NET GC + JIT overhead) -- DX12/Vulkan API (eliminate driver state validation) -- Offline content cooking (eliminate runtime mesh/texture decode) - -Each of those is a several-month undertaking and represents "becoming a different engine." The realistic target for acdream is 240-500 FPS at the user's monitor refresh, comfortably ahead of the visible-stutter threshold. Tier 1 + Tier 2 alone should deliver that for radius=12-15. - -For "Unreal-level FPS at full quality," that's a different project. diff --git a/docs/plans/2026-05-11-phase-n6-perf-baseline.md b/docs/plans/2026-05-11-phase-n6-perf-baseline.md deleted file mode 100644 index 93870e7..0000000 --- a/docs/plans/2026-05-11-phase-n6-perf-baseline.md +++ /dev/null @@ -1,193 +0,0 @@ -# Phase N.6 slice 1 β€” perf baseline at Holtburg - -**Created:** 2026-05-11. -**Spec:** [docs/superpowers/specs/2026-05-11-phase-n6-slice1-design.md](../superpowers/specs/2026-05-11-phase-n6-slice1-design.md) -**Measured against commit:** `25cb147` (Task 1 final β€” gpu_us fix + diag-gate symmetry follow-up) -**Purpose:** Capture authoritative CPU+GPU dispatch numbers so the next-phase decision (slice 2 vs C.1.5 vs Tier 2) rests on real data. - ---- - -## Β§1. Setup - -- **Hardware:** Radeon RX 9070 XT -- **Resolution:** 1440p (2560Γ—1440) -- **Quality preset:** High (default) -- **Connection:** live ACE at `127.0.0.1:9000` -- **Character:** `+Acdream` at Holtburg -- **Sky / time:** clear midday (F7 β†’ Noon, F10 β†’ Clear) -- **Build:** Debug -- **Date measured:** 2026-05-11 -- **Environment overrides:** `ACDREAM_WB_DIAG=1`, `ACDREAM_STREAM_RADIUS=` - -Note: `ACDREAM_STREAM_RADIUS=N` forces N₁=N (all N near-tier landblocks at full detail). -This is NOT the production A.5 default (N₁=4 / Nβ‚‚=12), which was characterized in -CLAUDE.md as comfortable 200–400 FPS at the default preset. These measurements -characterize the scaling curve β€” what happens as near-tier radius grows β€” not current -production behavior. FPS was not captured directly (no window-title screenshot per run); -it can be derived from `(1e6 / total_frame_time_us)` but the dispatcher's `cpu_us` is -only part of the frame (terrain, sky, particles, UI, GL submission overhead, and -swap-buffer wait are not included). - -## Β§2. Dispatch CPU / GPU numbers - -Each cell records the median of the last 3 `[WB-DIAG]` lines from a ~30s stable window. -`entSeen / entDrawn / groups / drawsIssued` are also from those lines (values per 5s bucket). -FPS column omitted β€” not captured per the note above. - -| Radius | Motion | cpu_us median | cpu_us p95 | gpu_us median | gpu_us p95 | entSeen (per 5s) | entDrawn (per 5s) | groups | drawsIssued (per 5s) | -|--------|------------|---------------|------------|---------------|------------|------------------|-------------------|--------|----------------------| -| 4 | standstill | 3,208 | 3,313 | 93 | 95 | 16.9M | 15.5M | 1,216 | 1.65M | -| 4 | walking | 2,967 | 3,112 | 95 | 120 | 13.9M | 13.9M | 1,850 | 1.45M | -| 8 | standstill | 6,732 | 7,199 | 126 | 130 | 19.8M | 19.8M | 333 | 218K | -| 8 | walking | 6,572 | 6,927 | 96 | 113 | 18.1M | 18.0M | 534 | 245K | -| 12 | standstill | 12,853 | 13,525 | 344 | 507 | 19.6M | 19.6M | 541 | 184K | -| 12 | walking | 16,320 | 17,241 | 553 | 603 | 17.8M | 17.8M | 898 | 200K | - -**Notable:** `meshMissing` counts at r4 standstill (~1.45M per 5s) drop to near-zero while -walking. This suggests the static-entity slow path's mesh-load lifecycle has some delay -before populating for newly-streamed content. Not fatal β€” doesn't affect rendered output β€” -but worth a follow-up issue in `docs/ISSUES.md` if it persists in normal play. - -## Β§3. Surface-format histogram - -From `ACDREAM_DUMP_SURFACES=1` at radius=12, ~30s after enter-world. -Output written to `%LOCALAPPDATA%\acdream\n6-surfaces.txt`. - -- **Total unique GL textures:** 760 -- **Total bytes (sum of WΓ—HΓ—4):** 96,387,584 (~96.4 MB) - -**Top 10 (W, H) dimension buckets:** - -| Dimensions | Count | Share | -|------------|-------|-------| -| 128Γ—128 | 236 | 31% | -| 64Γ—64 | 111 | 15% | -| 256Γ—256 | 102 | 13% | -| 128Γ—256 | 71 | 9% | -| 64Γ—128 | 69 | 9% | -| 256Γ—128 | 48 | 6% | -| 128Γ—64 | 39 | 5% | -| 512Γ—512 | 30 | 4% | -| 8Γ—8 | 18 | 2% | -| 32Γ—32 | 14 | 2% | - -**Format distribution:** - -| Format | Count | Share | -|---------------|-------|-------| -| RGBA8_DECODED | 760 | 100% | - -All uploads land as RGBA8 regardless of source format (INDEX16, P8, DXT, BGRA, etc. -all decode through `TextureHelpers` before upload). The source-format diversity is real -but invisible to GL after the decode step. - -**Top 10 (W, H, format) triples β€” atlas-opportunity input:** - -Same as the dimension buckets above since there is only one format. The top-3 triples -(128Γ—128, 64Γ—64, 256Γ—256) cover 449 of 760 surfaces = **59%**. - -**Atlas-opportunity score: 59%** of surfaces fall into the top-3 (W, H, format) triples. -A conventional rule-of-thumb is that >30% concentration into the top buckets makes atlas -packing worth the implementation cost for memory savings; this measurement is well above -that. However, see Β§4 for why atlas is not the right next step despite the high score. - -## Β§4. Conclusion + next-phase recommendation - -### What the data shows - -**The entity dispatcher is strongly CPU-bound.** At every radius, CPU dominates GPU by -30–50Γ—. At radius=12 standstill: 12.9 ms CPU vs 0.34 ms GPU. At radius=12 walking the -ratio is 16.3 ms CPU vs 0.55 ms GPU. There is no GPU bottleneck. - -**GPU is wildly under-utilized.** The highest gpu_us p95 observed is 603 Β΅s at radius=12 -walking β€” against a 16,600 Β΅s frame budget at 60 FPS. The GPU is working at roughly -3.6% of its 60fps capacity for entity rendering alone. Even accounting for terrain, sky, -particles, UI, and swap-buffer overhead, there is substantial headroom. The "GPU -comfortable" threshold (gpu_us p95 < 8,000 Β΅s) is not even close to being challenged. - -**CPU grows more than linearly with N₁ (near-tier radius), but sublinearly with -visible-LB count.** As N₁ grows from 4 β†’ 8 β†’ 12, median cpu_us grows from 3.2 ms β†’ -6.7 ms β†’ 12.9 ms β€” roughly 1.0Γ— β†’ 2.1Γ— β†’ 4.0Γ— the r4 baseline. The visible-LB count -scales as `(2N+1)Β²`: 81 β†’ 289 β†’ 625, so CPU growth is sublinear in LB count (4.0Γ— -vs 7.7Γ— expected if every LB cost the same). Frustum culling discards most far LBs -early, but the outer per-LB walk still has to touch each one. The Tier 1 entity- -classification cache (`EntityClassificationCache`, shipped as #53) wins on the inner -loop (per-entity classification avoided on cache hits) but the outer walk dominates -as N₁ grows. This is exactly what the Tier 2 plan (persistent groups) at -`docs/plans/2026-05-10-perf-tiers-2-3-roadmap.md` addresses by eliminating the -per-frame LB scan entirely. - -**Radius=12 is not the production scenario.** `ACDREAM_STREAM_RADIUS=12` forces N₁=12 -(625 near LBs at full detail). The production A.5 default preset is N₁=4 / Nβ‚‚=12 (81 -full-detail near + 544 terrain-only far), which CLAUDE.md already characterizes as -comfortable 200–400 FPS at the default preset. The numbers above characterize the scaling -curve for headroom analysis, not the experience a typical player sees. - -**Atlas opportunity is high (59%) but the win is memory-only β€” and modest.** With 96 MB -of textures and 59% in the top-3 dimension buckets, atlas consolidation would let the -top buckets share single `Texture2DArray` objects rather than each surface owning its -own 1-layer array. The primary wins of atlas β€” fewer sampler switches, fewer texture -binds β€” are already near-zero because bindless textures are made resident once at upload -and never bound per draw. The remaining win is the per-array metadata overhead Γ— N -surfaces, which is bounded but not dramatic given all surfaces are already power-of-two -and same-format (RGBA8). Even on the optimistic side, the absolute memory saving is on -the order of low-MB to ~10 MB, not a 40–50% halving. GPU is not bottlenecked on sampler -switches or memory bandwidth (0.6 ms gpu_us p95 at radius=12 walking demonstrates this -directly), so atlas adoption would cost 1–2 weeks of implementation risk for a memory -saving the process doesn't currently need at 96 MB. - -### Recommendation - -**Primary: do C.1.5 next (PES emitter wiring β€” portals, chimneys, fireplaces).** Four -reasons: (a) the production dispatcher is already comfortable at the default N₁=4 preset -per the CLAUDE.md notes; (b) the two slice-2 items that were "conditional on baseline" -data (atlas adoption and persistent-mapped buffers) are not justified β€” GPU is not -bottlenecked; (c) C.1.5 fills a visible content gap that has been open since C.1 shipped -and is in the roadmap queue ahead of N.6 slice 2; (d) C.1.5 stabilizes the particle path -before any future shader migration work in slice 2 touches `particle.frag`. Starting -point for C.1.5 scoping: `docs/plans/2026-04-27-phase-c1-pes-particles.md` lines 285–295. - -**Secondary (after C.1.5 lands): N.6 slice 2 with reduced scope.** The baseline data -justifies dropping atlas adoption and persistent-mapped buffers from slice 2 entirely. -What remains is a ~1-day cleanup: retire orphan `mesh.frag` (verify zero callers post-N.5 -amendment), collapse dead `_handlesByOverridden` / `_handlesByPalette` legacy caches once -their callers are confirmed gone, migrate `particle.frag` to bindless sampling after C.1.5 -stabilizes the path. Slice 2 is a cleanup sprint, not a performance phase. - -**Tertiary option (if perf escalation becomes pressing): Tier 2 first.** The scaling -curve (3.2 β†’ 6.7 β†’ 12.9 ms as N₁ grows 4 β†’ 8 β†’ 12) confirms the per-LB walk is the -bottleneck β€” exactly what Tier 2's persistent-group structure at -`docs/plans/2026-05-10-perf-tiers-2-3-roadmap.md` addresses. Not urgent at the current -default N₁=4; worth revisiting if a future quality preset wants N₁=8 as default or if the -200–400 FPS range at N₁=4 shrinks after more content is streamed. - -**Decision rule for revisiting:** if future measurement at the default preset shows -cpu_us median > 5,000 Β΅s or gpu_us p95 > 8,000 Β΅s, re-open the escalation question. -Otherwise, hold the C.1.5 β†’ reduced-slice-2 sequence. - -## Β§5. Reproducing the measurements - -Raw `[WB-DIAG]` output from each run was inspected live during measurement and the -median of the last three steady-state lines from each scenario was transcribed into Β§2. -The raw launch logs were not preserved β€” the captured medians in Β§2 are the canonical -record. To reproduce on the same hardware: - -```powershell -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -$env:ACDREAM_TEST_HOST = "127.0.0.1" -$env:ACDREAM_TEST_PORT = "9000" -$env:ACDREAM_TEST_USER = "testaccount" -$env:ACDREAM_TEST_PASS = "testpassword" -$env:ACDREAM_WB_DIAG = "1" -$env:ACDREAM_STREAM_RADIUS = "4" # or 8, 12 -dotnet run --project src\AcDream.App\AcDream.App.csproj --no-build -c Debug 2>&1 | Tee-Object -FilePath "baseline.log" -``` - -Stand still for ~30 s at the target radius (60 s at radius 12 to let streaming settle), -or walk Nβ†’Eβ†’Sβ†’W across one landblock. Then `Select-String -Path baseline.log -Pattern -"\[WB-DIAG\]" | Select-Object -Last 3` captures the steady-state numbers. - -For the surface histogram, also set `$env:ACDREAM_DUMP_SURFACES = "1"`, stay in-world -~30 s after streaming has loaded β‰₯100 textures (the cache-size gate), then read -`$env:LOCALAPPDATA\acdream\n6-surfaces.txt`. diff --git a/docs/plans/2026-05-12-milestones.md b/docs/plans/2026-05-12-milestones.md deleted file mode 100644 index 78a8c67..0000000 --- a/docs/plans/2026-05-12-milestones.md +++ /dev/null @@ -1,356 +0,0 @@ -# acdream β€” milestones (morale + scope layer) - -**Status:** Living document. Created 2026-05-12. -**Sits above:** [`docs/plans/2026-04-11-roadmap.md`](2026-04-11-roadmap.md) (the strategic phase index). -**Currently working toward:** **M1 β€” Walkable + clickable world.** - ---- - -## Why this document exists - -The roadmap is a phase index β€” week- to month-scale, ~50 phases by the time -v1.0 lands. Phases ship in vertical slices (architecture-first, horizontal -completion deferred), which is the right strategy for a solo open-source -project at this scale β€” but it leaves a chronic "everything is half-built" -feeling because no single phase ship feels like a real milestone. - -This document sits **one altitude above** the roadmap. Each milestone is: - -- **~6–10 weeks of focused work** (not a single phase, not a whole year). -- Defined by a **concrete playable scenario** that gets recorded as a demo - video when the milestone hits. -- A **scope-freeze event**: when a milestone lands, the phases it covers go - off-limits until v1.0's final polish pass (M7). - -Crossing a milestone is a real event with an artifact. Phases ship; milestones -**land**. - ---- - -## Operating rules - -1. **One active milestone at a time.** Everything not on the critical path to - the current milestone gets filed in `docs/ISSUES.md` with a `post-MN` tag - and explicitly muted until the milestone hits. This is what kills the - jumping-between-things feeling. - -2. **Frozen phases are off-limits.** "Frozen" means no rework, no polish, no - follow-up commits unless something is actively broken (player crash, - regression). Visual nice-to-haves, "while I'm here" cleanups, and - architecture second-guesses are all post-M7. The freeze is the discipline - mechanism that makes the milestone meaningful β€” without it, M0's many - shipped phases keep silently consuming attention. - -3. **The milestone log is the morale instrument.** When a milestone hits: - - Record a ~30-second demo video showing the scenario end-to-end. - - Drop it in `docs/milestones/MN-.mp4` (create the directory on - first hit). - - Pin a still frame + one-paragraph writeup at the top of this doc. - - Update the freeze list. Update CLAUDE.md's "currently working toward" - line to the next milestone. - -4. **State both altitudes at session start.** "Currently working toward M1. - Current phase: L.2 collision. Next concrete step: L.2d slice 1 spec." This - keeps the high-level orientation visible alongside the immediate task and - makes mid-session drift obvious. - ---- - -## The milestones - -### M0 β€” "Connect & explore" β€” βœ… DONE (crossed months ago) - -**Demo scenario:** Log in, walk Holtburg in chase camera, see other characters -animate, send a chat message and see the echo, watch day turn to night, -listen to footsteps and ambient audio. - -**Phases included (frozen):** - -| Phase | What landed | -|---|---| -| 1–3 | Terrain + per-vertex normals + per-cell texture blending | -| 4 | UDP codec + handshake + character login + WorldSession | -| 5 | ObjDesc: AnimPartChange + TextureChanges + SubPalettes + ObjScale | -| 6.1–6.7 | Motion + animation foundation (idle, frame playback, slerp, UpdateMotion, UpdatePosition) | -| 7.1 | EnvCell room geometry β€” walls/floors/ceilings | -| 9.1–9.2 | Translucent render pass + back-face culling | -| A.1–A.5 | Streaming landblock loader β†’ two-tier streaming | -| B.1–B.3 | Outbound ack pump + player movement + physics MVP resolver | -| D.1, D.2a | 2D overlay + ImGui scaffold + `AcDream.UI.Abstractions` layer | -| E.1–E.5 | Motion hooks + audio + particles + combat wire + spell wire | -| F.1, F.2 | GameEvent dispatcher + item model + Appraise | -| G.1, G.2 | Sky + day/night + weather + dynamic lighting | -| H.1, I.1–I.8 | Chat wire + UI consolidation + holtburger inbound parity + combat translator | -| K, L.0 | Input architecture + retail bindings + Settings panel | -| N.0–N.6.1 | WB rendering migration (modern path mandatory) | -| C.1, C.1.5a/b | Particle system + portal/EnvCell static-script wiring | -| R.1–R.3 | Retail research infrastructure (PDB extract + named decomp) | - -**Status:** This is everything shipped through 2026-05-12. ~25 phase ships. -**Worth saying out loud: this is the hard half of the project.** The engine -runs, the world renders correctly, the network connects, the input is wired, -the data layers for combat/spells/items/audio/particles all exist. What's -missing is the gameplay loop on top. - ---- - -### M1 β€” "Walkable + clickable world" β€” 🟑 CURRENT, all 4 demo targets met (pending recorded video) - -**Demo scenario:** Walk through Holtburg without getting stuck on the inn -doorway. Open the inn door. Click an NPC and see selection feedback. Pick -up an item from the ground. - -**Demo-target status (as of 2026-05-14):** - -| # | Target | Status | Evidence | -|---|---|---|---| -| 1 | Walk through Holtburg without getting stuck | βœ… met | L.2a/d/g shipped 2026-05-12; Holtburg doorway verified | -| 2 | Open the inn door | βœ… met | B.4b (interaction) + B.4c (swing animation) shipped 2026-05-13 | -| 3 | Click an NPC and see selection feedback | βœ… met | B.4b chain + chat handlers; verified 2026-05-14 (Tirenia + Royal Guard double-click β†’ NPC dialogue in chat panel) | -| 4 | Pick up an item from the ground | βœ… met (close-range path) | B.5 + post-B.5 `PickupEvent (0xF74A)` fix shipped 2026-05-14; visual-verified at Holtburg; creature-pickup guard added in `a01ebd5` | - -**What's left to formally land M1:** -- Record ~30s demo video of the four-target scenario end-to-end. -- Drop at `docs/milestones/M1-walkable-clickable.mp4`. -- Pin still + one-paragraph writeup at the top of this doc. -- Flip the freeze list. Update `CLAUDE.md`'s "currently working toward" - line to M2. - -**Known polish items deferred (do not block M1 recording, addressable post-M1):** -- **#61** β€” AnimationSequencer linkβ†’cycle frame-0 flash on door swing. LOW. -- **#62** β€” PARTSDIAG null-guard. Latent, not reachable today. -- **#63** β€” Server-initiated `MoveToObject` auto-walk not honored (blocks - double-click pickup + out-of-range F-pickup; close-range still works). - MEDIUM. Candidate Phase B.6 β€” holtburger has the reference port. -- **#64** β€” Local-player pickup animation does not render (retail - observers see it correctly). LOW. - -**Phases that shipped to clear M1:** -- **L.2 (a + d + g sub-lanes)** β€” Movement & Collision Conformance. - L.2a slices 1+2+3 + L.2d slice 1+1.5 + L.2g slice 1+1b+1c shipped - 2026-05-12 / 2026-05-13. Visual-verified via the B.4b doorway test. -- **B.4b** β€” outbound Use + `WorldPicker` + double-click detection + - `CollisionExemption` widening + `ServerGuidβ†’entity.Id` translation - (the ID-mismatch trap surfaced during L.2g slice 1c). Shipped - 2026-05-13. -- **B.4c** β€” door swing animation: spawn-time `AnimationSequencer` - registration + stance-value fix (`NonCombat = 0x3D` not `0x01`, which - had been causing doors to render halfway underground). Shipped - 2026-05-13. -- **B.5** β€” `BuildPickUp` (PutItemInContainer 0x0019) + `SendPickUp` - helper + F-key wiring + new `PickupEvent (0xF74A)` despawn handler. - Shipped 2026-05-14. -- **B.5 polish** (`a01ebd5`) β€” guard `SendPickUp` against creature - targets so F-on-NPC produces a "Can't pick that up" toast instead of - the malformed pickup that triggered ACE's `WeenieError 0x0029` + NPC - emote chain. (Briefly visited adding "You pick up the X." chat / - toast feedback for ground pickups in `87ba5c9`, then reverted in - `20ecb23` β€” retail doesn't show that line for ground pickups; only - for items received from NPCs / other characters, and that path is - separate.) - -**Freeze on landing:** -- L.2 zone (collision, cell ownership, transition parity, wire authority) -- B.4 zone (interaction outbound) -- B.5 zone (pickup outbound + inbound despawn) - -**What "M1 lands" looks like:** the existing Holtburg traversal works as a -retail player would expect. Doorways are walkable. Buildings have solid -walls. Outdoor cell seams report the right cell. Clicking an NPC selects it -and produces NPC chat. The Use action opens doors. F picks up items at -close range and the player sees "You pick up the X." in chat. - ---- - -### M2 β€” "Kill a drudge" β€” πŸ”΅ NEXT (~6–10 weeks after M1) - -**Demo scenario:** Equip a sword. Walk to a drudge. Swing. See "You hit -Drudge for 12 slashing damage (87%)" in chat. Watch the swing animation -play. Drudge dies, drops loot. Pick up the loot. Open the inventory panel -and see it. - -**Phases to ship:** -- **F.2 (panels)** β€” Inventory panel reading `ItemRepository` (data already - shipped in F.2 base; M2 ships the visual surface). -- **F.3** β€” Combat math + damage flow. -- **F.5a** β€” Visible-at-login dev panels (Attributes, Skills, Equipped, - Inventory list) β€” minimal ImGui surfaces, retail-skin deferred to M5. -- **L.1c** β€” Combat animation wiring (draw/sheath, attack swings by - stance/power/height, hit reactions, evades). -- **L.1b** β€” Command router + motion-state cleanup (prereq for L.1c). - -**Freeze on landing:** -- Combat math zone -- Inventory zone (data + dev panel; retail-skin reopens in M5) -- L.1b/c combat-animation zone - -**What "M2 lands" looks like:** the gameplay loop is real. You can fight, -take damage, kill things, see loot, manage your inventory. The game becomes -a game. - ---- - -### M3 β€” "Cast a spell" β€” πŸ”΅ (~3–4 weeks after M2) - -**Demo scenario:** Cast Flame Bolt at a drudge. Watch the cast animation, -the projectile, the impact. Self-cast a buff (Strength Self). See the -enchantment in a buff list. Recall to lifestone β€” full recall animation, -correct teleport, correct re-spawn. - -**Phases to ship:** -- **F.4** β€” Spell cast state machine (buffs + recalls first, projectile - spells second). -- **L.1d** β€” Spell-casting animation wiring (cast command classification, - windup, release, fizzle/interruption, recoil). -- **F.5 (Spellbook panel)** β€” dev-skin surface for learned spells + active - enchantments. - -**Freeze on landing:** -- F.4 spell zone -- L.1d cast-animation zone -- Spellbook dev-panel surface - -**What "M3 lands" looks like:** mages are real characters. Buffs work, -recalls work, the first projectile spells work. Combat from M2 + casting -from M3 = retail-equivalent gameplay loop for melee and casters. - ---- - -### M4 β€” "Live in the world" β€” πŸ”΅ (~6–10 weeks) - -**Demo scenario:** Create a fresh character from scratch (no ACE admin). -Spawn. Talk to an NPC. Accept a quest. Walk to a dungeon entrance. Portal -in (pink-bubble loading). Walk through the dungeon. Complete the quest. -Walk back out. - -**Phases to ship:** -- **H.3** β€” Emote scripts + quests + dialogs (122 EmoteType Γ— 39 Trigger - mini-VM). -- **G.3** β€” Dungeon streaming + portal space + `PlayerTeleport` handling. - (Unblocked by L.2e from M1.) -- **H.4** β€” Character creation (`0xE000002 CharGen` + heritages + appearance - picker + preview). -- **L.1e** β€” Emote + posture animation wiring. - -**Freeze on landing:** -- H.3 dialog/quest zone -- G.3 dungeon zone -- H.4 character-creation zone -- L.1e emote-animation zone - -**What "M4 lands" looks like:** the world feels populated and interactive. -You can do quests, enter dungeons, create characters. Combined with M2/M3, -the client is **functionally playable** β€” minus visual polish. - ---- - -### M5 β€” "Looks like retail" β€” 🟒 PARALLELIZABLE WITH M3/M4 (~4–8 weeks) - -**Demo scenario:** Side-by-side screenshot of acdream vs retail at the same -location, same time of day, same character. Hard to tell apart at a glance. -Open the inventory panel β€” retail-skinned with the right font, icons, and -9-slice panel borders. - -**Phases to ship:** -- **C.1.5c** β€” Sky-PES dispatch chain (closes #2 lightning, #28 aurora, - #29 cloud thinness). -- **C.2** β€” Dynamic point lights (fireplaces, lamps, torches with proper - local lighting). -- **C.3** β€” Palette range tuning (skin/hair/eye colors match retail). -- **C.4** β€” Double-sided translucent polys. -- **D.2b** β€” Custom retail-look UI backend. -- **D.3–D.7** β€” AcFont + dat sprites + core panels reskinned + HUD orbs + - cursor manager. -- **L.1f** β€” NPC/monster + item-use animation coverage. - -**Freeze on landing:** -- Visual polish zone (C.1.5c, C.2, C.3, C.4) -- D.2b β†’ D.7 UI-skin zone -- L.1f NPC-anim zone - -**What "M5 lands" looks like:** the client visually convinces. Screenshots -become postable. The "old / broken vs retail" feeling that drives most of -the chronic ISSUES.md entries is gone. - ---- - -### M6 β€” "Plugins ship" β€” 🟒 (~4 weeks) - -**Demo scenario:** A third party (not you) writes a small plugin against -the published API β€” XP tracker, loot logger, simple chat filter β€” and it -loads cleanly. Sample plugin lives in the repo with documented build steps. - -**Phases to ship:** -- Plugin API surface: stable contract over `AcDream.Core` + `AcDream.UI.Abstractions`, - versioned, with the world-state interfaces exposed. -- Plugin host: load isolation, lifecycle, error containment. -- Sample plugin (XP tracker or loot logger) β€” proves the API by using it. -- Plugin docs page. - -**Freeze on landing:** -- Plugin API v1 surface (additive changes only post-freeze). - -**What "M6 lands" looks like:** the differentiator vs the retail client is -real. acdream offers something retail never did, and the API is documented -well enough that other people can build on it. - ---- - -### M7 β€” "v1.0" β€” 🟒 (open-ended polish) - -**Demo scenario:** Long-running stress test: log in, play for 4 hours -across outdoor + dungeon + portal + combat + spell + chat scenarios, -reconnect once mid-session, log out clean. No crashes, no protocol errors, -no visual regressions, no audio dropouts. - -**Phases to ship:** -- **Phase M** β€” Network stack conformance (retransmit, ACK piggybacking, - echo/keepalive, fragment splitting, typed actions). Deferred until now - because ACE handles loss gracefully β€” but v1.0 needs proper network - hardening. -- **H.2** β€” Allegiance. -- **N.6 slice 2 + N.7–N.10** β€” Finish WB rendering migration (EnvCells, - sky/particles via WB, visibility manager, GL infrastructure - consolidation). -- **Phase J long-tail** β€” Player rig polish, group/fellowship UI, trade - window, salvage/tinker UI, house ownership, society UI, dev-mode tools. -- **L.1g** β€” Animation polish + conformance. -- Final visual + audio polish pass against ISSUES.md chronic backlog. - -**What "M7 lands" looks like:** v1.0. Ship. - ---- - -## Estimated timeline - -| Milestone | Effort | Cumulative | -|---|---|---| -| M0 | DONE | DONE | -| M1 | ~4–6 wk | ~5 wk | -| M2 | ~6–10 wk | ~13 wk | -| M3 | ~3–4 wk | ~17 wk | -| M4 | ~6–10 wk | ~25 wk | -| M5 | ~4–8 wk (parallel) | overlaps M3/M4 | -| M6 | ~4 wk | ~29 wk | -| M7 | open-ended | v1.0 | - -**Roughly 9–12 months of focused solo work from 2026-05-12 to v1.0.** That's -honest for an open-source project of this scale. The biggest single rock is -M2 (combat math + animations + inventory panels lining up); M5 can be -chipped at in parallel by subagents while you drive M3/M4. - ---- - -## What this document is **not** - -- **Not a release schedule.** Internal morale + scope layer only. If acdream - goes public-alpha at some point, that's a separate decision built on top - of one of these milestones. -- **Not immutable.** When reality and the milestones diverge, update the - milestones in the same session you discover the divergence. Same rule as - the roadmap. -- **Not a replacement for the roadmap.** Phases are still where the - implementation details live. This doc is the orientation layer above them. -- **Not granular enough for daily work.** Daily work happens at the phase / - sub-phase / commit level. The milestone is the multi-week target you're - aiming at. diff --git a/docs/plans/2026-05-12-phase-c1.5b-handoff.md b/docs/plans/2026-05-12-phase-c1.5b-handoff.md deleted file mode 100644 index b314516..0000000 --- a/docs/plans/2026-05-12-phase-c1.5b-handoff.md +++ /dev/null @@ -1,320 +0,0 @@ -# Phase C.1.5b handoff β€” issue #56 + EnvCell statics + animation-hook verification - -**Created:** 2026-05-12, immediately after Phase C.1.5a merged to `main` (commit `88bda12`). -**Audience:** the fresh-session Claude (or human) picking up C.1.5b. -**Predecessor:** [C.1.5a portal PES wiring](../superpowers/specs/2026-05-12-phase-c1.5a-portals-design.md) β€” slice 1, shipped. - ---- - -## Β§1 Startup prompt (copy this into a fresh session) - -Everything below this fence is the prompt to paste into a new Claude Code session. The detailed context the session needs lives in Β§2+ of this same file. - -``` -Pick up Phase C.1.5b β€” issue #56 (multi-emitter per-part collapse) first, -then EnvCell static-object DefaultScript dispatch + animation-hook -particle path verification. - -## Context - -Phase C.1.5a (portal PES wiring) merged to main 2026-05-12 (merge commit -88bda12). The PhysicsScriptRunner now fires Setup.DefaultScript on every -server-spawned WorldEntity via the new EntityScriptActivator. Visual -verification at the Holtburg Town network portal confirmed the mechanism -works end-to-end (10-hook portal script fires correctly, color + -persistence + orientation match retail), but exposed a pre-existing C.1 -limitation now tracked as ISSUE #56: ParticleHookSink ignores -CreateParticleHook.PartIndex, so all 10 of the portal's emitters -collapse to one root position β†’ compressed, partly-ground-buried swirl. - -The C.1.5a final cross-task reviewer recommended #56 be resolved FIRST -in this slice, before the EnvCell static-object walker, because slice -2's natural visual gate (Holtburg inn interior fireplace, cottage -chimney) uses the same multi-emitter pattern β€” without #56 fixed, -slice 2 ships with the same visual gap. - -## Read first (in order) - -1. docs/plans/2026-05-12-phase-c1.5b-handoff.md (this file's Β§2+) -2. docs/ISSUES.md #56 (the per-part collapse problem with reproducible - identifiers from the C.1.5a verification session) -3. docs/superpowers/specs/2026-05-12-phase-c1.5a-portals-design.md Β§10 - (slice 2 preview written during C.1.5a brainstorming) -4. docs/plans/2026-04-27-phase-c1-pes-particles.md lines 285–295 (the - original C.1.5 scope source) - -## Two slices in this session - -### Slice A β€” issue #56 fix (per-part transform handling for static entities) - -For static entities (portals, EnvCell statics, building decorations β€” -no animation), precompute the per-part offset from -Setup.PlacementFrames[Resting] at spawn time and surface those offsets -to the ParticleHookSink so SpawnFromHook can apply them. The handoff -doc Β§3 has the suggested architecture + decision space. - -Acceptance: relaunch + walk to the Holtburg Town network portal. The -10 emitters should distribute across the portal Setup's parts instead -of collapsing β€” swirl extends vertically through the arch with -retail-like shape, not buried in the ground. - -### Slice B β€” EnvCell static-object DefaultScript dispatch + animation-hook verification - -Walk EnvCell.StaticObjects for newly-loaded landblocks; for each -StaticObject whose Setup has a non-zero DefaultScript, fire the -activator with a synthetic entity ID (suggested scheme: hash of -(landblockId, cellIndex, staticIndex) with a high-bit marker so it -doesn't collide with server guids β€” see handoff Β§4). Then verify the -animation-hook particle path (already shipped in C.1; just needs -visual confirmation): cast a spell on +Acdream and compare to retail. - -Acceptance: Holtburg inn fireplace flames, cottage chimney smoke, and -a spell-cast particle effect on +Acdream all match retail. - -## What this is NOT - -- Not a renderer change. particle.frag stays as-is; bindless migration - waits for N.6 slice 2 after this slice lands. -- Not a perf phase. The N.6 baseline at radius=4 still holds; the - per-part precompute cost is bounded by N parts Γ— M emitters per - spawned entity (small). -- Not adding new emitter types. Use the existing PES emitter data. -- Not touching the animated-entity path. For animated entities (NPCs, - monsters), per-part transforms vary per frame and would need a - per-tick refresh similar to UpdateEntityAnchor. Defer to a future - phase; C.1.5b stays scoped to static entities only. - -## Suggested workflow - -1. Read the handoff doc + the four referenced docs above. -2. Invoke superpowers:brainstorming to settle: - - For slice A: precompute-per-part-at-spawn vs render-thread-side-table - approach (handoff Β§3 has the tradeoff analysis). - - For slice B: the synthetic-entity-id scheme; whether the EnvCell - walker piggybacks LandblockSpawnAdapter or gets its own class. - - Visual verification locations. -3. After brainstorm: spec at - docs/superpowers/specs/2026-05-13-phase-c1.5b-design.md (one spec - for both slices since they share the activator and tests), then plan - at docs/superpowers/plans/2026-05-13-phase-c1.5b.md, then execute - via superpowers:subagent-driven-development. - -## Open issues from C.1.5a worth knowing - -- #56 β€” multi-emitter per-part collapse. This slice's headline. -- #55 β€” meshMissing diagnostic spam at radius=4 standstill. LOW - severity, not blocking; only touch if you're already in the - dispatcher for unrelated reasons. -- Cold-path timing observation (C.1.5a Task 2 review): the activator - fires DefaultScript before pending-bucket entities are merged into - a loaded landblock. Mirrors existing _wbEntitySpawnAdapter pattern; - not a regression; defer. - -## Three doc-drift items from C.1.5a (trivial β€” fold into the new spec) - -1. C.1.5a spec Β§4 says "fifth (optional) parameter" β€” actually fourth. -2. C.1.5a spec Β§4 says "~50 lines" β€” file ships at 93 lines. -3. GpuWorldState.AddEntitiesToExistingLandblock (A.5 Farβ†’Near - promotion path) does not fire the activator. No-op today because - promotion-tier entities are atlas-tier and the activator's - ServerGuid==0 guard would skip them anyway, but worth a code - comment explaining why the call is intentionally omitted there - (parallel to existing comments at the RemoveEntitiesFromLandblock - block in the same file). - -Start by reading the handoff doc, then ask me what slice-A/slice-B -boundary feels right and what visual verification locations I want -to target. -``` - ---- - -## Β§2 What shipped in C.1.5a (so you don't re-do it) - -### Commits on `main` (oldest to newest under merge `88bda12`) - -| SHA | Title | -|---|---| -| `06d7fbd` | docs(vfx): Phase C.1.5a β€” portal PES wiring design spec | -| `ed5335b` | docs(vfx #C.1.5a): implementation plan + spec wiring-location fixes | -| `003c502` | feat(vfx #C.1.5a): add EntityScriptActivator (no wiring yet) | -| `e0529b0` | test(vfx #C.1.5a): real-emitter verification in OnRemove test + unused using | -| `44d8502` | feat(vfx #C.1.5a): wire EntityScriptActivator into GpuWorldState lifecycle | -| `65d833d` | feat(vfx #C.1.5a): construct EntityScriptActivator in GameWindow | -| `849690c` | refactor(vfx #C.1.5a): reuse SequencerFactory's capturedDats in resolver | -| `334f0c6` | fix(vfx #C.1.5a): seed entity rotation in activator so hook offset rotates | -| `9009318` | docs(vfx #C.1.5a): ship Phase C.1.5a + file issue #56 for per-part collapse | -| `88bda12` | Merge branch 'claude/lucid-burnell-aab524' β€” Phase C.1.5a | - -### New files - -- [`src/AcDream.App/Rendering/Vfx/EntityScriptActivator.cs`](../../src/AcDream.App/Rendering/Vfx/EntityScriptActivator.cs) β€” 93 lines including doc comments. Constructor `(PhysicsScriptRunner, ParticleHookSink, Func)`; `OnCreate(WorldEntity)` resolves the entity's `Setup.DefaultScript.DataId`, seeds `_particleSink.SetEntityRotation(entity.ServerGuid, entity.Rotation)`, and calls `_scriptRunner.Play(scriptId, entity.ServerGuid, entity.Position)`; `OnRemove(uint serverGuid)` calls `_scriptRunner.StopAllForEntity(serverGuid)` + `_particleSink.StopAllForEntity(serverGuid, fadeOut: false)`. -- [`tests/AcDream.Core.Tests/Rendering/Vfx/EntityScriptActivatorTests.cs`](../../tests/AcDream.Core.Tests/Rendering/Vfx/EntityScriptActivatorTests.cs) β€” 4 xUnit `[Fact]` tests with mutation-check teeth verified during the C.1.5a code-quality reviews. - -### Modified files - -- [`src/AcDream.App/Streaming/GpuWorldState.cs`](../../src/AcDream.App/Streaming/GpuWorldState.cs) β€” fourth optional ctor parameter `EntityScriptActivator? entityScriptActivator = null`, field `_entityScriptActivator`, and two `?.OnCreate(entity)` / `?.OnRemove(serverGuid)` calls immediately after the matching `_wbEntitySpawnAdapter?.OnCreate` / `?.OnRemove` calls in `AppendLiveEntity` and `RemoveEntityByServerGuid`. -- [`src/AcDream.App/Rendering/GameWindow.cs`](../../src/AcDream.App/Rendering/GameWindow.cs) β€” new field declaration alongside `_wbEntitySpawnAdapter` and inline construction of the activator + resolver lambda inside the existing `OnLoad` block (~line 1620), passed to `GpuWorldState` as a named argument. - -### What's working - -- Server-spawned entities (`ServerGuid != 0`) with `Setup.DefaultScript.DataId != 0` fire that script through `PhysicsScriptRunner.Play` on enter-world. -- Multi-hook scripts dispatch all their hooks in order (timed by `StartTime` offsets β€” more retail-faithful than WB's "all at once" collection). -- `CreateParticleHook.Offset.Origin` rotates correctly from entity-local to world frame via the activator's `SetEntityRotation` seed. -- Despawn cleanly stops all scripts + emitters for the entity. -- 4 unit tests cover all three branches plus the rotation-seed correctness. -- Visual verification at the Holtburg Town network portal passed for the mechanism: 10-hook portal script fires correctly with matching color, persistence, orientation, multi-emitter dispatch. - -## Β§3 Issue #56 decision space (slice A) - -### The problem - -`ParticleHookSink.SpawnFromHook` computes: - -```csharp -var rotation = _rotationByEntity.TryGetValue(entityId, out var rot) ? rot : Quaternion.Identity; -var anchor = worldPos + Vector3.Transform(offset, rotation); -``` - -…where `worldPos` is `entity.Position` and `offset` is `cph.Offset.Origin`. The `CreateParticleHook.PartIndex` field is recorded into the per-handle tracking dict but never applied to the anchor. Retail's intended geometry is: - -``` -anchor = entityWorldPose Γ— partLocalTransform[partIndex] Γ— hookOffsetInPartLocal -``` - -Without the part transform multiplication, every emitter in a multi-emitter script lands at the same root position. Visible symptom: the Holtburg portal's 10 emitters compress to one point and the swirl appears partially buried because the offset's local-up direction goes off in world axes instead of the part's local axes. - -### Where part transforms come from - -For STATIC entities (no animation), per-part transforms come from `Setup.PlacementFrames[Resting].Frames[partIndex]` β€” see how `ObjectMeshManager.CollectParts` walks them in `references/WorldBuilder` (worktree-relative path; submodule must be initialized to read): - -- For each `i` in `0..setup.Parts.Count`, the per-part transform is `Matrix4x4.CreateScale(setup.DefaultScale[i]) * Matrix4x4.CreateFromQuaternion(placementFrame.Frames[i].Orientation) * Matrix4x4.CreateTranslation(placementFrame.Frames[i].Origin)`. -- `DefaultScale` only applies when `SetupFlags.HasDefaultScale` is set. -- Fall back to `PlacementFrames[Default]` if `Resting` isn't present. - -For ANIMATED entities (NPCs, monsters, the player), per-part transforms vary per animation frame and live in `AnimatedEntityState` / the animation tick. **Out of scope for C.1.5b.** - -### Approach options - -**Option A β€” precompute per-spawn, pass at activator-call time.** - -`EntityScriptActivator` reads the Setup's `PlacementFrames[Resting]` once per spawn, builds a `Matrix4x4[] partTransforms` array, and passes it to a new sink method `_particleSink.SetEntityPartTransforms(entityId, partTransforms)` before calling `_scriptRunner.Play(...)`. `ParticleHookSink.SpawnFromHook` then reads `_partTransformsByEntity` to apply per-hook: - -```csharp -var partXf = _partTransformsByEntity.TryGetValue(entityId, out var pts) && partIndex < pts.Length - ? pts[partIndex] : Matrix4x4.Identity; -var anchor = worldPos + Vector3.Transform(Vector3.Transform(offset, partXf), rotation); -``` - -Pros: clean ownership (activator owns the lifecycle of part transforms keyed by entityId), matches existing sink-state patterns (`_rotationByEntity`, `_renderPassByEntity`), small code surface, fully testable. - -Cons: stores per-entity array (matrix per part) β€” bounded but allocates. Doesn't compose with the animated-entity case (which would need per-tick refresh). - -**Option B β€” render-thread side-table populated by the dispatcher.** - -The `WbDrawDispatcher` already computes per-part world transforms each frame. Surface them via a side-table the sink queries. Per-frame. - -Pros: free composition with animated entities (the dispatcher transforms whether the entity is animated or not). - -Cons: render-thread / sink-thread coordination concern, bigger architectural surface, the dispatcher would need a new responsibility (publish part transforms) outside its draw-loop hot path. Risk of touching the modern bindless dispatcher's perf budget that N.5/N.5b worked to lock in. - -**Option C β€” sink-side dat lookup on demand.** - -`ParticleHookSink` calls `_dats.Get(...)` on the hook fire to look up the part transform. Pros: zero state on activator. Cons: introduces dat coupling into the sink (currently dat-free), per-hook-fire dat lookup is a hidden allocation, doesn't compose with animated entities either, and we'd be reading the same Setup multiple times for the same entity. - -### Recommended approach - -**Option A.** It's the smallest surface, matches the existing sink-state pattern, doesn't expand any other layer's responsibilities, and the "doesn't compose with animated entities" downside is intentional β€” animated entities are explicitly out of scope and will get their own treatment later, possibly via Option B at that time. - -### Test approach - -Mirror the C.1.5a `OnCreate_SetsEntityRotationForHookOffsetTransform` test: construct an entity whose Setup has 2 parts (root at origin + part 1 lifted at (0, 0, 1)), fire a CreateParticleHook with `PartIndex=1` and `Offset.Origin=(0, 0, 0)`, assert the spawned particle's world position is `(0, 0, 1)` (the part's offset, not the root). Add a mutation check: delete the `SetEntityPartTransforms` line and confirm the test fails. - -## Β§4 EnvCell static-object dispatch decision space (slice B) - -### The problem - -`EnvCell.StaticObjects` are interior decoration objects inside dungeon / building cells. Each StaticObject has a Setup reference and a placement frame. They have NO `ServerGuid` β€” they're dat-hydrated, not server-spawned. - -Our `EntityScriptActivator.OnCreate` early-returns when `entity.ServerGuid == 0` (atlas-tier guard). So as-is, the activator won't fire DefaultScript for EnvCell statics. - -### Two architectural questions - -**Q1 β€” synthetic entity ID for tracking + cleanup.** - -`PhysicsScriptRunner` keys active scripts by `(scriptId, entityId)`. `ParticleHookSink` keys per-entity emitter handles by `entityId`. EnvCell statics need a stable, unique 32-bit ID for these tables that won't collide with server guids (and won't collide between two EnvCell statics in different cells). - -Suggested scheme: - -``` -uint syntheticId = 0xC0000000u - | ((landblockId & 0x0000FF00u) << 16) // landblock X byte β†’ bits 24-31 minus high marker - | ((landblockId & 0xFF000000u) >> 8) // landblock Y byte β†’ bits 16-23 - | ((cellIndex & 0x0000FFFFu) << 0); // bits 0-15: cell index within landblock -``` - -…leaving 4 bits for the static-object index within the cell. Adjust bit layout for the actual `(LandblockId, CellIndex, StaticIndex)` distribution. The `0xC0_______u` marker is **above** server guid range and **above** the anonymous-emitter range (`0x80_______u`) used by `ParticleHookSink._anonymousEmitterSerial`, so no collision. - -Sanity check: `WorldEntity.ServerGuid` is `uint`; the `(scriptId, entityId)` dedupe key in the runner only needs uniqueness, not semantic meaning. Either scheme works as long as it's collision-free. - -**Q2 β€” which adapter walks EnvCell.StaticObjects?** - -Three options: - -- **Option Ξ± β€” piggyback `LandblockSpawnAdapter`.** That adapter already walks `landblock.Entities` for atlas-tier mesh-ref counting. Extending it to also walk `EnvCell.StaticObjects` and fire DefaultScript via the activator keeps the per-landblock-load flow in one place. Cons: blurs the adapter's single responsibility. - -- **Option Ξ² β€” new `EnvCellStaticActivator` class.** Mirror `EntityScriptActivator`'s shape but key by synthetic-id, walking each loaded landblock's EnvCells on load and firing per-static-object. Cons: more code; slight duplication of the activator pattern. - -- **Option Ξ³ β€” `EntityScriptActivator` learns a "static-object" entry point.** Add `OnEnvCellStaticCreate(LoadedLandblock landblock, int cellIndex, int staticIndex, Setup setup, Vector3 worldPos, Quaternion worldRot)` to the existing activator. Compute the synthetic ID inside. Cons: signature creep on the activator. - -Recommended: **Option Ξ².** Keeps the existing activator's `WorldEntity`-shaped contract pure; the new class has a clean per-static-object contract; both share `_scriptRunner` and `_particleSink` instances so no architectural duplication, just two thin orchestrators. - -### Lifecycle - -EnvCell statics live as long as their parent landblock is loaded. On landblock unload, the new activator should stop all scripts for all its synthetic IDs from that landblock. Mirror `LandblockSpawnAdapter`'s `OnLandblockLoaded` / `OnLandblockUnloaded` lifecycle. - -## Β§5 Animation-hook verification (slice B's quick half) - -Already shipped in C.1: `MotionInterpreter` fires per-keyframe hooks through `IAnimationHookSink` β†’ `ParticleHookSink`. We just haven't verified visually in the current codebase state. - -Procedure: -1. Cast a spell on `+Acdream` (the test character likely has at least one spell + components configured β€” check or grant if needed). -2. Watch the cast-anim particle effect (sparkles, glyphs, etc.) β€” does it match retail's casting animation? -3. Optional: trigger an emote with a particle hook (the `\dance` / `\drink` emotes are good candidates if they have particle data). - -If broken, file an issue with the symptom. If working, mark slice B complete on verification. - -## Β§6 Verification locations - -All in or near Holtburg, within ~30s of `+Acdream`'s spawn: - -- **#56 fix re-verify** β€” the Town network portal used in C.1.5a. Same procedure as C.1.5a's Task 4 (see [the C.1.5a spec Β§8](../superpowers/specs/2026-05-12-phase-c1.5a-portals-design.md)). -- **EnvCell chimney** β€” any cottage / inn within the Holtburg outer perimeter with a smoking chimney in retail. Confirm via dual-client. -- **EnvCell fireplace** β€” Holtburg Inn interior. Walk inside and stand near the fireplace. Confirm flame particles match retail. -- **Animation-hook verify** β€” cast a spell standing somewhere safe (outside any aggro range). Compare to retail. - -## Β§7 File pointers for slice 2 - -- Particle pipeline (Core): [`src/AcDream.Core/Vfx/ParticleSystem.cs`](../../src/AcDream.Core/Vfx/ParticleSystem.cs), [`ParticleHookSink.cs`](../../src/AcDream.Core/Vfx/ParticleHookSink.cs), [`PhysicsScriptRunner.cs`](../../src/AcDream.Core/Vfx/PhysicsScriptRunner.cs). -- Activator (App): [`src/AcDream.App/Rendering/Vfx/EntityScriptActivator.cs`](../../src/AcDream.App/Rendering/Vfx/EntityScriptActivator.cs). -- Streaming bridge (App): [`src/AcDream.App/Streaming/GpuWorldState.cs`](../../src/AcDream.App/Streaming/GpuWorldState.cs), [`LandblockSpawnAdapter.cs`](../../src/AcDream.App/Rendering/Wb/LandblockSpawnAdapter.cs). -- Renderer: [`src/AcDream.App/Rendering/ParticleRenderer.cs`](../../src/AcDream.App/Rendering/ParticleRenderer.cs) β€” **don't touch** in C.1.5b; bindless migration is N.6 slice 2. -- EnvCell loader: search for `LoadedCell` / `EnvCell.StaticObjects` in `src/AcDream.App/Streaming/` and `src/AcDream.Core/World/`. -- C.1.5a tests as a reference: [`tests/AcDream.Core.Tests/Rendering/Vfx/EntityScriptActivatorTests.cs`](../../tests/AcDream.Core.Tests/Rendering/Vfx/EntityScriptActivatorTests.cs). - -## Β§8 Open questions to surface during brainstorming - -- Slice A: does the C.1.5a final reviewer's "static-only fix is self-contained" claim hold up? (Section Β§3 Option A says yes; brainstorming should verify by checking `EntityScriptActivator`'s spawn path doesn't depend on animation state.) -- Slice B: which Setup field actually lives on `EnvCell.StaticObjects` β€” is it a `SetupId` reference or an inline Setup? Different shape changes the synthetic-ID hash input. -- Slice B: are EnvCell statics ALSO subject to the cold-path timing observation from C.1.5a Task 2 review (firing before the cell is rendered)? - -## Β§9 Worktree cleanup reminder (one-time, from outside the worktree) - -The C.1.5a worktree directory at `C:/Users/erikn/source/repos/acdream/.claude/worktrees/lucid-burnell-aab524` was not auto-removed because the controller session held a file lock. After this session ends, from any other directory: - -```powershell -git -C "C:/Users/erikn/source/repos/acdream" worktree remove --force ` - "C:/Users/erikn/source/repos/acdream/.claude/worktrees/lucid-burnell-aab524" -``` - -The branch `claude/lucid-burnell-aab524` was successfully deleted; only the worktree directory needs manual cleanup. diff --git a/docs/research/2026-05-08-phase-n3-handoff.md b/docs/research/2026-05-08-phase-n3-handoff.md deleted file mode 100644 index 7b7e7fa..0000000 --- a/docs/research/2026-05-08-phase-n3-handoff.md +++ /dev/null @@ -1,132 +0,0 @@ -# Phase N.3 handoff β€” texture decoding via WorldBuilder - -**Use this whole document as the prompt** when handing off to a fresh -agent. Everything they need to pick up cold is below. - ---- - -## Background you'll need - -You're working in `acdream`, a from-scratch C# .NET 10 reimplementation -of Asheron's Call's retail client. The project's house rule (in -`CLAUDE.md`) is **the code is modern, the behavior is retail**. - -acdream just shipped **Phase N.1** (commits `26cf2b8` through `ad8b931`), -the first sub-phase of a strategic migration to fork WorldBuilder -(`github.com/Chorizite/WorldBuilder`, MIT) and depend on its tested -rendering + dat-handling code instead of porting algorithms from retail -decomp ourselves. - -**Read first:** -- `docs/architecture/worldbuilder-inventory.md` β€” the full taxonomy of - what WB has and what we keep porting ourselves -- `docs/superpowers/specs/2026-05-08-phase-n-worldbuilder-migration-design.md` - β€” the parent design doc for Phase N -- `CLAUDE.md` β€” especially the "Reference repos" section (now points at - WB as the rendering BASE) and the workflow rules - -**Phase N.1 commit history (just shipped):** read -`git log --oneline c8782c9..ad8b931` to see how N.0 + N.1 were -structured. The pattern repeats for N.3. - -## What N.3 is - -Replace acdream's texture decoding pipeline with WorldBuilder's -`Chorizite.OpenGLSDLBackend.Lib.TextureHelpers`. WB handles INDEX16, -P8, BGRA, DXT, and alpha-channel decoding. Our existing implementations -of these are scattered across `src/AcDream.App/Rendering/TextureCache.cs` -and possibly `src/AcDream.Core/Meshing/` β€” find them with -`grep -rln "INDEX16\|P8 decode\|DXT\|BGRA" src/`. - -## Acceptance criteria - -- Build green (`dotnet build`) -- All existing tests green (the 8 pre-existing `DispatcherToMovementIntegrationTests` - failures don't count β€” they exist on main) -- New conformance tests added per format that's substituted (one xUnit - Theory per: INDEX16, P8, BGRA, DXT). Each compares a fixed input byte - array decoded by our path vs WB's path; assertions on output pixel array. -- Visual verification at Holtburg (or wherever) shows no texture - regressions: terrain texturing, mesh texturing, particle textures all - look the same. -- ISSUES.md updated with any known cosmetic deltas (the N.1 pattern β€” - if WB and retail disagree on something subtle, file it, don't try - to fix it inline). - -## Tasks (suggested decomposition) - -Follow the N.1 plan structure (`docs/superpowers/plans/2026-05-08-phase-n1-scenery-via-wb-helpers.md`) -as the template. Concretely: - -1. **Audit our texture decode paths.** Grep, list every file/method that - decodes a texture. Map each to the WB equivalent in - `references/WorldBuilder/Chorizite.OpenGLSDLBackend/Lib/TextureHelpers.cs` - (read it end to end first). -2. **Per-format conformance test.** TDD style: write the test, run it - to fail, then plumb the substitution. Conformance test fixture inputs - should include real-dat byte sequences (read a known-good texture from - a dat, encode the bytes as a hex blob in the test). -3. **Substitution.** Replace each decode site with the WB call. Keep our - GL upload pathways β€” those are NOT WB's responsibility. -4. **Visual verification.** Launch the client at Holtburg, walk around, - look at a tree (mesh texture), the ground (atlas texture), particles - (the recent C.1 rain/clouds/aurora work), and a building (composite - texture). Compare against retail or against a screenshot before the - change. -5. **Delete legacy decoders** once visual verification passes. -6. **Update roadmap + ISSUES** as the final commit. - -## Watchouts (lessons from N.1) - -- **ACME has a downstream fork with extra filters** (`references/WorldBuilder-ACME-Edition/`). - WB's `TextureHelpers` may have ACME-specific patches not yet in upstream. - Compare both before assuming WB's version is canonical. We forked - upstream WB; ACME is reference-only. -- **Conformance tests are non-negotiable.** Phase N.1's rotation bug was - caught by the conformance test. Don't skip them. If a test fails, it's - a real divergence β€” investigate before "fixing" the test. -- **Whackamole stops the migration.** If 3+ visual regressions appear on - default-on, stop, file as ISSUES, ship. The migration goal is "use WB's - tested code"; pixel-perfect equivalence with our broken hand-ports is - not the goal. -- **`Setup.SortingSphere` β‰  `Setup.CylSphere`.** The N.1 attempt at - `obj_within_block` over-suppressed because we used the wrong radius - source (sorting sphere too large). For texture decoding this likely - doesn't matter, but the general lesson is: read WB's full source - carefully before adapting; don't assume parallel methods do parallel - things. -- **Per-vertex road check β€” STOP signal.** If you find yourself reading - ACME for "what's missing" and considering a per-vertex filter, STOP. - N.1 tried this (commit `e279c46`), regressed visually, reverted in - `677a726`. ACME's filter set works as a coherent unit; pick-and-choose - fails. If the N.3 work uncovers a similar ACME-only filter, file it - in ISSUES and move on, don't port it inline. - -## Where to start - -1. `git pull` on main to get the latest (Phase N.1 just merged). -2. Create a new worktree for the work: - `git worktree add .claude/worktrees/ -b claude/`. -3. Read the three "read first" docs above. -4. Run `dotnet build && dotnet test` to confirm clean baseline. -5. Read `references/WorldBuilder/Chorizite.OpenGLSDLBackend/Lib/TextureHelpers.cs` - end to end. Take notes on the public API surface. -6. Run the audit task (#1 in Tasks above). Output should be a markdown - table of "our function / file:line / WB equivalent / format covered." -7. Use `superpowers:writing-plans` to convert the audit into a concrete - per-format plan. Then use `superpowers:subagent-driven-development` - to execute it with fresh subagents per format. - -## Useful greps - -- `grep -rln "INDEX16\|IndexedSurface\|P8\|DXT\|BGRA\|TextureFormat" src/` β€” find decode paths -- `grep -rln "TextureCache" src/` β€” find our cache layer -- `grep -n "public static.*Decode\|public static.*Convert" references/WorldBuilder/Chorizite.OpenGLSDLBackend/Lib/TextureHelpers.cs` β€” WB's public API - -## Open question to resolve early - -Does `Chorizite.OpenGLSDLBackend.Lib.TextureHelpers` cover ALL the -formats we use, or does it have gaps? Audit our texture types against -WB's API in step 1. If WB is missing a format we need, the migration for -that format gets deferred (file in ISSUES; keep our decoder for it; note -in the roadmap). diff --git a/docs/research/2026-05-08-phase-n4-week4-handoff.md b/docs/research/2026-05-08-phase-n4-week4-handoff.md deleted file mode 100644 index 6ab30fe..0000000 --- a/docs/research/2026-05-08-phase-n4-week4-handoff.md +++ /dev/null @@ -1,318 +0,0 @@ -# Phase N.4 Week 4 handoff β€” full draw dispatcher + visual verification + ship - -**Use this whole document as the prompt** when handing off to a fresh -agent. Everything they need to pick up cold is below. - ---- - -## Background you'll need - -You're working in `acdream`, a from-scratch C# .NET 10 reimplementation -of Asheron's Call's retail client. The project's house rule (in -`CLAUDE.md`) is **the code is modern, the behavior is retail**. - -acdream is in the middle of Phase N.4 β€” the rendering pipeline -foundation migration to WorldBuilder's `ObjectMeshManager` + -`TextureAtlasManager`. **Three of the four planned weeks have shipped -this session (2026-05-08)**: - -- Week 1 (commits up through `c49c6ed`): foundation types β€” feature - flag, surface metadata side-table, mesh-extraction + setup-flatten - conformance tests, `WbMeshAdapter` constructed against the real WB - pipeline. -- Week 2 (commits up through `36f7a60`): streaming integration β€” - `LandblockSpawnAdapter` routes atlas-tier (procedural / `ServerGuid==0`) - GfxObjs to WB's ref-count lifecycle. `WbMeshAdapter.Tick()` drains - the WB pipeline's main-thread queues per frame (fixes a real memory - leak). -- Week 3 (commits up through `d30fcb2`): per-instance tier hookup β€” - `AnimatedEntityState` holds per-server-spawned-entity overrides; - `EntitySpawnAdapter` routes server-spawned entities through the - existing `TextureCache.GetOrUploadWithPaletteOverride` decode path. - -**Current state at `main`:** build green, **947 tests pass**, 8 -pre-existing failures only (unchanged from pre-N.4 main). Default-off -behavior is byte-identical to pre-N.4 main; flag-on (`ACDREAM_USE_WB_FOUNDATION=1`) -runs both rendering pipelines in parallel β€” WB silently prepares -content, but nothing is yet drawn through it. - -**Read first:** - -- [docs/superpowers/plans/2026-05-08-phase-n4-rendering-foundation.md](../superpowers/plans/2026-05-08-phase-n4-rendering-foundation.md) β€” - the **living-document** plan. Top of file has a Progress table - showing Tasks 1-21 βœ… shipped with commit SHAs. Adjustments 1-5 - document architectural surprises caught during execution. **Read the - Adjustments before writing any Task 22 code** β€” they explain why the - current architecture is what it is. -- [docs/superpowers/specs/2026-05-08-phase-n4-rendering-foundation-design.md](../superpowers/specs/2026-05-08-phase-n4-rendering-foundation-design.md) β€” - the design spec. Architecture / two-tier split / animation handling / - data-flow diagrams. Strategic source of truth for "how the pieces - fit together." -- [CLAUDE.md](../../CLAUDE.md) β€” project-wide rules. The "Currently in - flight" section near the top points at the plan. - -## What Week 4 is - -Seven tasks (22-28). **Task 22 alone is the biggest single task in the -entire 28-task plan** β€” it's the moment we flip from "WB is silently -preparing content" to "WB is drawing content to your screen." - -The remaining six tasks are smaller: surface-metadata side-table -population, sky-pass preservation check, micro-tests round-out, visual -verification at 5 named locations, flag default-on, delete legacy -code, finalize plan + memory + ISSUES. - -**Task 22 also unlocks the Adjustment 3 mitigation.** Right now -flag-on has a real FPS regression because both rendering pipelines run -in parallel (legacy renderer still does atlas-tier upload + draw, -even though WB is also building atlas state). When Task 22 lands the -dispatcher AND wires the legacy-renderer short-circuit for atlas-tier -content, that double-work disappears. - -## Two unresolved decisions before Task 22 starts - -These need a brainstorm checkpoint at the start of Week 4, NOT a -"just dispatch": - -1. **Adjustment 4 plumbing.** `WorldEntity` doesn't carry - `HiddenPartsMask` or `AnimPartChanges` β€” those live on the - network-layer spawn record and don't make it to the render-side - entity. Two options: - - **A**: add `HiddenPartsMask` + `AnimPartChanges` fields to - `WorldEntity`, populate at spawn time. Cleaner long-term; small - networkβ†’render plumbing change. - - **B**: thread them as separate parameters into - `EntitySpawnAdapter.OnCreate(entity, hiddenMask, animPartChanges)`. - Sidesteps the `WorldEntity` change but couples the spawn-handler - to the adapter API. - - Decide before writing Task 22 because the dispatcher reads from - `AnimatedEntityState` which currently holds defaults (empty mask + - empty override map). Without this resolved, hidden parts won't - actually be hidden flag-on. - -2. **Surface-metadata side-table population strategy** (Task 23). The - spec proposes: when `WbMeshAdapter.IncrementRefCount(id)` is first - called for a GfxObj, walk its sub-meshes via `GfxObjMesh.Build`, - write each `(gfxObjId, surfaceIdx) β†’ AcSurfaceMetadata` entry into - the side-table. The `_metadataPopulated: HashSet` field - tracks which ids have been processed. - - **But:** if the same GfxObj gets its ref count drop to zero and - then re-incremented (LRU eviction + reload), do we re-populate? - The metadata is invariant per-GfxObj (surface flags don't change - with eviction), so probably no β€” the `HashSet` is fine. But - verify before implementing. - -## Watchouts (lessons from Weeks 1-3) - -These are real, observed gotchas. Read each before going deeper. - -- **The renderer is tier-blind by design (Adjustment 2).** Don't try - to put routing decisions in `InstancedMeshRenderer` or any mesh - uploader. Routing belongs at the **spawn-callback layer**: - `LandblockSpawnAdapter` for atlas-tier, `EntitySpawnAdapter` for - per-instance. Task 22's dispatcher reads from those adapters' - per-entity state at draw time; it doesn't make tier decisions. - -- **Flag-off must stay byte-identical to pre-N.4.** Every Task-22 code - path must have a `WbFoundationFlag.IsEnabled` gate. Default-off path - is what users see; we can't regress it. - -- **WB's pipeline does work even when you're not draining its results.** - Adjustment 3: `IncrementRefCount` triggers background mesh prep, - texture decode, atlas allocation. `WbMeshAdapter.Tick()` already - drains the upload queue per frame. The remaining FPS cost is - pure dual-pipeline cost (legacy + WB doing the same upload work). - Task 22's short-circuit fixes this. - -- **`MeshRef.SurfaceOverrides`** is the per-surface texture-swap data - carried by spawned entities. `GfxObjSubMesh.SurfaceId` is what gets - swapped. Task 22's draw loop must consult both: the entity's - `MeshRef.SurfaceOverrides` for explicit swaps, and otherwise the - mesh's built-in `SurfaceId`. - -- **Conformance tests catch divergences early.** Per N.1's rotation - bug: write the conformance test BEFORE the substitution. The - matrix-composition test (`(entityWorld) Γ— (animation) Γ— (restPose)`) - is the load-bearing one for Task 22 β€” pin it before integrating. - -- **`WbMeshAdapter.Tick()` is required.** It's already wired into - `GameWindow.OnRender`. Task 22's dispatcher needs the upload queue - drained BEFORE it tries to draw, so order in OnRender is: - `_wbMeshAdapter?.Tick()` β†’ `_wbDrawDispatcher?.Draw(...)` β†’ other - draw work. - -- **Name retail decomp first; Phase N.4 doesn't change that rule.** - Task 22's matrix composition uses standard graphics math β€” no AC- - specific algorithms β€” so the "grep `named-retail/` first" workflow - doesn't apply to the matrix code itself. But for any AC-specific - question that surfaces during integration (e.g., "does retail - render hidden parts as zero-alpha or skip them entirely?"), grep - `docs/research/named-retail/acclient_2013_pseudo_c.txt` first. - -## Acceptance criteria for Week 4 - -From the plan: - -- [ ] All conformance tests pass (Tasks 3, 4, 20 β€” already shipped; - verify still green after Task 22 lands). -- [ ] All component micro-tests pass (Tasks 11, 17, 18, 19, 22 β€” - Task 22 adds matrix-composition tests). -- [ ] All existing tests still pass. 8 pre-existing failures don't - count. -- [ ] Build green throughout. -- [ ] Visual verification at 5 named locations passes: - 1. Holtburg outdoor β€” terrain props, scenery, buildings, NPCs, - characters all render correctly. - 2. Drudge Hideout (or comparable) β€” EnvCell + interior lighting + - animated creatures. - 3. Foundry β€” heavy NPC traffic + customized appearances. - 4. A character with extreme palette overrides. - 5. Long roam (5+ minutes) β€” GPU memory stabilizes (LRU eviction - fires). -- [ ] Memory budget enforcement actually verified (Task 13 was - deferred to here; Task 22 makes it testable because GL resources - finally get allocated for LRU to evict). -- [ ] Sky pass renders identically (load-bearing β€” sky's - `Translucent+ClipMap` cloud sheet, raw-`Additive` fog skip, - `Luminosity` keyframe handling all flow through the side-table - via `AcSurfaceMetadata`). -- [ ] Flag flipped to default-on at the end (Task 26). -- [ ] Legacy code paths deleted (Task 27). -- [ ] Roadmap + memory + ISSUES updated (Task 28). - -## Tasks 22-28 β€” quick map - -Full detail is in the plan. Brief here: - -- **22 β€” `WbDrawDispatcher` full draw loop.** ~1-2 days. Atlas-tier - + per-instance-tier draw with matrix composition. Reads from - `WbMeshAdapter.GetRenderData(id)` for atlas content; reads from - `EntitySpawnAdapter.GetState(serverGuid)` for per-instance state; - composes per-part `(entity Γ— animation Γ— rest-pose)` matrices; - pushes uniforms; issues GL draws. **Also wires the legacy- - renderer short-circuit** for atlas-tier content (the Adjustment 3 - fix). -- **23 β€” Surface-metadata side-table population.** ~half day. Hook - into `WbMeshAdapter.IncrementRefCount` so that on first registration - of a GfxObj, the side-table gets populated with one - `AcSurfaceMetadata` per surfaceIdx (using `GfxObjMesh.Build`'s - metadata as the source of truth). -- **24 β€” Sky-pass preservation check.** ~half day. Verify the sky - pass's `NeedsUvRepeat` / `DisableFog` / `Luminosity` flow through - the side-table to `SkyRenderer` correctly. Likely no code change; - smoke-test sky rendering with flag on, weather/day-night cycle. -- **25 β€” Component micro-tests round-out.** Audit existing tests - against the spec's Testing section. Probably nothing to add since - Tasks 11/17/18/19/22 already cover the listed micro-tests. -- **26 β€” Visual verification + flag default-on.** Human-in-the-loop - walk through the 5 named locations. If clean, flip - `WbFoundationFlag.IsEnabled` from `== "1"` to `!= "0"` so flag-on - becomes the default. -- **27 β€” Delete legacy code paths.** Remove the now-unused legacy - upload code in `StaticMeshRenderer` + `InstancedMeshRenderer`. - N.6 fully replaces these files anyway. -- **28 β€” Update roadmap + memory + ISSUES + finalize plan.** Mark - N.4 shipped in the roadmap's Live βœ“ table. File any cosmetic - deltas as ISSUES. Add a memory note if a durable lesson emerged. - Flip the plan's status header from "Living document β€” work in - progress" to "Final state β€” phase shipped (merge ``)". - -## Where to start - -1. **Read the three "Read first" docs above end-to-end.** Especially - the Adjustments section in the plan β€” those are the architectural - constraints Task 22 must respect. -2. **Decide Adjustment 4 plumbing** (option A vs B from above). This - is a small brainstorm checkpoint, not a multi-question - `superpowers:brainstorming` skill invocation. Document the choice - inline in the plan as Adjustment 6. -3. **Don't create a new worktree.** The existing branch - `claude/quirky-jepsen-fd60f1` and worktree - `.claude/worktrees/quirky-jepsen-fd60f1` are clean and ready. - Submodule already initialized. Build green. -4. **Use `superpowers:subagent-driven-development`** to execute Week 4 - task-by-task. Pattern from Weeks 1-3: dispatch one subagent per - task (or batch of related tasks), use Sonnet for implementation, - merge to main per logical chunk, update the plan's Progress table - as commits land. -5. **Pause for visual verification at Task 26.** This is a human-in- - the-loop step β€” needs you to walk the 5 named locations. - -## Open questions a fresh agent might hit - -- **Q: Why did Adjustment 5 mark Task 20 (per-instance decode - conformance) as "structural"?** Because both old and new paths call - the same `TextureCache.GetOrUploadWithPaletteOverride` function. We - preserved the decode logic exactly; the seam is at the call site, - not at the algorithm. Byte-equality is automatic. - -- **Q: Can I delete `InstancedMeshRenderer`?** Not in N.4. The plan - marks it as "becomes a thin adapter in N.4, fully replaced in N.6." - Task 27 deletes the legacy upload paths inside it but keeps the - file as a draw-orchestration adapter until N.6. - -- **Q: What's the memory budget check actually checking?** GPU memory - stabilizes during long roam. WB's `_maxGpuMemory = 1 GB` triggers - LRU eviction once the cache exceeds that. We verify by walking - for 5+ minutes at radius 7 (49 landblocks visible at any time) and - confirming GPU memory in the title bar plateaus rather than - growing unboundedly. - -- **Q: What happens if Task 22 takes longer than expected?** The - living-document convention says document Adjustments inline. If - Task 22 needs to split (e.g., atlas-tier draw lands first, per- - instance tier in a follow-on commit), that's fine β€” just update - the Progress table and add an Adjustment explaining the split. - -## Useful greps and commands - -- `dotnet build --verbosity quiet 2>&1 | tail -3` β€” quick build check. -- `dotnet test --verbosity quiet 2>&1 | tail -3` β€” full test suite. -- `git -C C:\Users\erikn\source\repos\acdream log --oneline -10` β€” - recent main commits. -- `grep -rn "WbFoundationFlag.IsEnabled" src/` β€” every place we gate - on the flag (audit before flipping default-on in Task 26). -- `grep -rn "_wbMeshAdapter\|_wbSpawnAdapter\|_wbEntitySpawnAdapter" src/` β€” - every WB adapter wiring point. - -## Smoke-test launch (PowerShell) - -```powershell -# Kill any stale processes first -Get-Process -Name AcDream.App -ErrorAction SilentlyContinue | Stop-Process -Force -Start-Sleep -Seconds 4 - -# Flag-on at radius 7 β€” Week 4 dev environment -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -$env:ACDREAM_TEST_HOST = "127.0.0.1" -$env:ACDREAM_TEST_PORT = "9000" -$env:ACDREAM_TEST_USER = "testaccount" -$env:ACDREAM_TEST_PASS = "testpassword" -$env:ACDREAM_USE_WB_FOUNDATION = "1" -$env:ACDREAM_STREAM_RADIUS = "7" -dotnet run --project src\AcDream.App\AcDream.App.csproj --no-build -c Debug 2>&1 | - Tee-Object -FilePath "n4-week4-smoke.log" -``` - -(Drop the `ACDREAM_USE_WB_FOUNDATION` line for flag-off comparison.) - -## Adjustments index β€” quick reference - -For full text, see the plan document (each is a `### Adjustment N` -subsection under Task 6's old position, in chronological order): - -1. **`DefaultDatReaderWriter` discovery** (2026-05-08) β€” no - dat-reader bridge needed; WB ships a usable concrete - implementation. -2. **Renderer is tier-blind** (2026-05-08) β€” routing belongs at - spawn callbacks, not in the renderer. -3. **FPS regression = dual-pipeline cost** (2026-05-08) β€” both - pipelines run in parallel until Task 22's short-circuit lands. -4. **`WorldEntity` lacks HiddenParts/AnimPartChange fields** - (2026-05-08) β€” plumbing deferred; Task 22 needs to resolve - (option A: add fields; option B: thread as separate args). -5. **Task 20 is structural** (2026-05-08) β€” same function called - both paths, byte-equality automatic, no test file needed. diff --git a/docs/research/2026-05-08-phase-n5-handoff.md b/docs/research/2026-05-08-phase-n5-handoff.md deleted file mode 100644 index 1c4d7be..0000000 --- a/docs/research/2026-05-08-phase-n5-handoff.md +++ /dev/null @@ -1,495 +0,0 @@ -# Phase N.5 β€” Modern Rendering Path β€” Cold-Start Handoff - -**Created:** 2026-05-08, immediately after N.4 ship. -**Audience:** the next agent picking up rendering perf work. -**Purpose:** give you everything you need to start N.5 cold, without -spelunking through five months of session history. - ---- - -## TL;DR - -N.4 just shipped: WB's `ObjectMeshManager` is now acdream's production -mesh pipeline, and `WbDrawDispatcher` is the production draw path. It -works (Holtburg renders correctly, FPS substantially improved over the -naΓ―ve dual-pipeline state we hit during week 4 verification) but it's -still doing per-group state changes (`glBindTexture`, `glBindBuffer` -for the IBO, `glDrawElementsInstancedBaseVertexBaseInstance` per group) -and a fresh `glBufferData` upload per frame. - -**N.5's job: lift the dispatcher onto WB's modern rendering primitives -that we're already paying GPU-feature-detection cost for.** Two big -wins, paired: - -1. **Bindless textures** (`GL_ARB_bindless_texture`) β€” WB already - populates `ObjectRenderBatch.BindlessTextureHandle`. Switch our - shader to read texture handles from a per-instance attribute - (`uvec2` β†’ `sampler2D` via the bindless extension). Eliminates - 100% of `glBindTexture` calls. -2. **Multi-draw indirect** (`glMultiDrawElementsIndirect`) β€” build a - buffer of `DrawElementsIndirectCommand` structs (one per group), - upload once, fire ONE `glMultiDrawElementsIndirect` call per pass. - The driver pulls everything from the indirect buffer. - -Together they target a 2-5Γ— CPU win on draw-heavy scenes (Holtburg -courtyard, Foundry, dense dungeons). They're packaged together because -both are "modern path" extensions we already gate on, both require -the same shader rewrite, and they pair naturally β€” multi-draw indirect -is a no-op CPU-win without bindless because per-group `glBindTexture` -calls would still serialize. - -**Estimated scope: 2-3 weeks.** Plan + spec to be written by the -brainstorm + spec steps below. - ---- - -## Where N.4 left things - -### Branch state - -If this handoff is being read on `main` after merging the N.4 worktree: -N.4 commits land at the head of main. The relevant final commits: - -- `c445364` β€” N.4 SHIP (flag default-on, plan final, roadmap, memory) -- `573526d` β€” perf pass 1-4 (drop dead lookup, sort, cull, hash memo) -- `7b41efc` β€” FirstIndex/BaseVertex + Issue #47 + grouped instanced -- `943652d` β€” load triggers + `batch.Key.SurfaceId` source -- `01cff41` β€” Tasks 22+23 (`WbDrawDispatcher` + side-table) - -If the worktree branch (`claude/tender-mcclintock-a16839`) hasn't been -merged yet, that's where the work is. Verify with `git log --oneline`. - -### What works in N.4 - -- `ACDREAM_USE_WB_FOUNDATION=1` is default-on. WB's `ObjectMeshManager` - loads, decodes, and uploads every entity mesh. Our existing - `TextureCache` decodes textures (palette-aware, per-instance overrides - via `GetOrUploadWithPaletteOverride`). -- `WbDrawDispatcher.Draw`: - - Walks visible entities (per-landblock AABB cull + per-entity AABB - cull + portal visibility) - - Buckets every (entity Γ— meshRef Γ— batch) tuple by - `GroupKey(Ibo, FirstIndex, BaseVertex, IndexCount, TextureHandle, Translucency)` - - Single `glBufferData` upload of all matrices for the frame - - Per group: `glActiveTexture(0) + glBindTexture(2D, handle) + glBindBuffer(EBO, ibo) + glDrawElementsInstancedBaseVertexBaseInstance(..., FirstInstance)` - - Two passes: opaque (front-to-back sorted) + translucent -- 940/948 tests pass (8 pre-existing failures unrelated to rendering). -- Visual verification at Holtburg passed: scenery + characters render - correctly with full close-detail geometry (Issue #47 preserved). - -### What N.5 inherits - -These are levers N.5 will pull on: - -- **WB's modern rendering is already active.** `OpenGLGraphicsDevice` - detected GL 4.3 + bindless on first run; WB's `_useModernRendering` - is true; every mesh lives in WB's single `GlobalMeshBuffer` (one VAO, - one VBO, one IBO). -- **Bindless handles are already populated.** `ObjectRenderBatch.BindlessTextureHandle` - is non-zero for batches WB owns the texture for. (See gotcha #2 - below for entities with palette overrides β€” those use acdream's - `TextureCache` which doesn't expose bindless handles yet.) -- **The instance VBO is acdream-owned** (`WbDrawDispatcher._instanceVbo`) - with locations 3-6 patched onto WB's global VAO. Stride 64 bytes - (one mat4). N.5 expands this to (mat4 + uvec2 handle) = 80 bytes. - -### Three load-bearing WB API gotchas N.4 surfaced - -These bit us hard during Task 26 visual verification. Documented in -CLAUDE.md "WB integration cribs" + plan adjustments 7-9 + -`memory/project_phase_n4_state.md`. Re-stating here because they -reshape the design space: - -1. **`ObjectMeshManager.IncrementRefCount(id)` is NOT lifecycle-aware.** - It only bumps a usage counter. Mesh loading is fired separately - via `PrepareMeshDataAsync(id, isSetup)`. The result auto-enqueues - to `_stagedMeshData` (line 510 of `ObjectMeshManager.cs`); our - existing `WbMeshAdapter.Tick()` drains it. `WbMeshAdapter.IncrementRefCount` - already calls `PrepareMeshDataAsync`. **N.5 doesn't need to change - this β€” just don't break it.** - -2. **`ObjectRenderBatch.SurfaceId` is unset.** WB constructs batches - with `Key = batch.Key` (a `TextureAtlasManager.TextureKey` struct - that has a `SurfaceId` field) but never populates the top-level - `SurfaceId` property. Read `batch.Key.SurfaceId`. **N.5 keeps this - pattern.** - -3. **WB's modern rendering packs every mesh into ONE global - VAO/VBO/IBO.** Each batch's `IBO` field points to the global IBO; - the batch's actual slice is identified by `FirstIndex` (offset into - IBO, in *indices*) and `BaseVertex` (offset into VBO, in *vertices*). - N.4's draw uses `glDrawElementsInstancedBaseVertexBaseInstance` - with those offsets. **N.5's `DrawElementsIndirectCommand` per-group - record will carry `firstIndex` + `baseVertex` for the same reason.** - ---- - -## What N.5 is β€” technical detail - -### The two-feature pairing - -**Bindless textures** (`GL_ARB_bindless_texture`): -- Each texture handle is a 64-bit integer (`uvec2` in GLSL). -- Shader declares `layout(bindless_sampler) uniform sampler2D ...` or - receives the handle as a per-vertex-attribute `uvec2`. -- No `glBindTexture` needed at draw time β€” the handle IS the binding. -- Handle generation: `glGetTextureHandleARB(textureId)` followed by - `glMakeTextureHandleResidentARB(handle)` (the texture must be - resident on the GPU; non-resident handles produce GPU faults). - -**Multi-draw indirect** (`glMultiDrawElementsIndirect`): -- Indirect command struct layout (must match `DrawElementsIndirectCommand`): - ```c - struct { - uint count; // index count for this draw - uint instanceCount; // number of instances - uint firstIndex; // offset into IBO, in indices - int baseVertex; // vertex offset into VBO - uint baseInstance; // first instance ID (offsets per-instance attribs) - }; - ``` -- Build a buffer of N of these structs (one per group), upload once, - fire one GL call: `glMultiDrawElementsIndirect(mode, indexType, ptr, drawcount, stride)`. -- The driver issues all N draws in one shot. Effectively zero CPU - overhead per draw beyond uploading the indirect buffer. - -**Why pair them.** Multi-draw indirect doesn't let you change uniform -state between draws. So if textures are bound via `glBindTexture` per -group, you'd still need N CPU-side setup steps before each indirect -call β€” defeating the purpose. Bindless removes that constraint by -encoding the texture handle as per-instance data the shader reads -directly. With both, the modern render loop becomes: - -``` -1. Upload instance buffer (mat4 + uvec2 handle, per-instance) β€” once per frame -2. Upload indirect command buffer (one DEIC per group) β€” once per frame -3. glBindVertexArray(globalVAO) β€” once -4. glMultiDrawElementsIndirect(...) β€” ONCE per pass -``` - -That's it. No per-group state changes. - -### Instance attribute layout - -Currently (N.4): location 3-6 = mat4 model matrix (16 floats = 64 bytes). - -N.5 (proposed): location 3-6 = mat4 + location 7 = uvec2 bindless -handle = 16 floats + 2 uints = 72 bytes (16-aligned to 80 bytes per -WB's `InstanceData` precedent). - -Or use std140-aligned struct: -```c -struct InstanceData { - mat4 transform; // locations 3-6 - uvec2 textureHandle; // location 7 - uvec2 _pad; // padding to 80 -}; -``` - -Brainstorm should decide if we copy WB's `InstanceData` struct (Pack=16, -80 bytes including CellId/Flags fields we don't use) or define our own -minimal version. The 80-byte stride matches WB's so global VAO state -configured by WB stays compatible if the legacy WB draw path ever runs. - -### Per-instance entity texture handles - -Here's the wrinkle. N.4 uses `WbDrawDispatcher.ResolveTexture` to map -each (entity, batch) to a GL texture handle: - -- Tree (no overrides): `_textures.GetOrUpload(surfaceId)` β†’ 2D texture handle -- NPC with palette override: `_textures.GetOrUploadWithPaletteOverride(...)` β†’ composite-cached 2D texture handle -- Anything with surface override: `_textures.GetOrUploadWithOrigTextureOverride(...)` β†’ composite-cached 2D texture handle - -Those are all `GLuint` 32-bit GL texture *names*, not bindless handles. -**N.5 needs `TextureCache` to publish bindless handles for everything -it owns, not just WB-owned textures.** - -Implementation sketch: -- `TextureCache` adds a parallel cache keyed identically but storing - 64-bit bindless handles. On first request, generate via - `glGetTextureHandleARB(textureId)` + make resident. -- New API: `GetBindlessHandle(uint surfaceId, ...)` returns the handle. -- Or: change every `GetOrUpload*` method to return both the GL name - and the bindless handle (or just the handle; let GL name fall out - if anyone needs it later). - -WB's `ObjectRenderBatch.BindlessTextureHandle` covers the atlas-tier -case. For per-instance entities, we use `TextureCache`'s handle. - -### The new shader - -Reuse WB's `StaticObjectModern.vert` / `StaticObjectModern.frag` as a -template. Read those files cold. They already do bindless + the -instance-data layout. Adapt to acdream's `mesh_instanced.vert/frag` -conventions: - -- Keep the `uViewProjection` uniform, lighting UBO at binding=1, fog - uniforms. -- Add `#version 430 core` + `#extension GL_ARB_bindless_texture : require`. -- Replace `uniform sampler2D uDiffuse` with a `uvec2` per-vertex - attribute (location 7) β†’ reconstruct sampler in vertex shader OR - pass through to fragment via flat varying. -- Drop `uTranslucencyKind` uniform, OR keep it (still set per-pass β€” - multi-draw indirect doesn't break uniforms; only state that varies - per-draw is the constraint). - -### Translucency - -Multi-draw indirect can't change blend state mid-draw. Solution: -**still use two passes** (opaque + translucent), but within translucent -keep the per-blendfunc sub-passes (additive, alpha-blend, inv-alpha). -Three sub-passes within translucent. Each sub-pass = one -`glMultiDrawElementsIndirect` over its filtered groups. - -Or: if perf allows, fold all four blend modes into the shader via -per-instance blendmode int, sort all translucent groups by blendmode -in the indirect buffer, switch blend state at sub-pass boundaries. -Brainstorm decides the cleanest pattern. - ---- - -## Files to read before brainstorming - -In rough order: - -1. **N.4 plan + spec** β€” `docs/superpowers/plans/2026-05-08-phase-n4-rendering-foundation.md` - (status: Final). Adjustments 7-10 capture the gotchas. Spec at - `docs/superpowers/specs/2026-05-08-phase-n4-rendering-foundation-design.md`. - -2. **N.4 dispatcher source** β€” `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs`. - This is what you're modifying. Read end-to-end. - -3. **WB's modern rendering shaders** β€” `references/WorldBuilder/Chorizite.OpenGLSDLBackend/Shaders/StaticObjectModern.vert` - + `StaticObjectModern.frag`. The template you're adapting from. - -4. **WB's `ObjectMeshManager.UploadGfxObjMeshData`** β€” lines ~1654-1780 - of `references/WorldBuilder/Chorizite.OpenGLSDLBackend/Lib/ObjectMeshManager.cs`. - Shows how WB sets up the modern path's VBO/IBO/VAO. Especially note - how it patches in instance attribute slots (locations 3-6) on the - global VAO and configures location 7+ for bindless handles. - -5. **WB's `ObjectRenderBatch`** β€” same file, lines ~166-184. Note the - `BindlessTextureHandle` field β€” already populated when `_useModernRendering` - is on. - -6. **Our `TextureCache`** β€” `src/AcDream.App/Rendering/TextureCache.cs`. - Three composite caches: by surface id, by surface+origTex, by - surface+origTex+palette. N.5 adds parallel bindless-handle caches. - -7. **CLAUDE.md "WB integration cribs"** section. Lines ~28-80. The - three gotchas + the integration architecture in plain language. - -8. **Memory: `project_phase_n4_state.md`** β€” same content from a - different angle. Reading both helps lock in the gotchas. - ---- - -## Brainstorm questions - -These are the questions to resolve in the brainstorm step. Don't -prejudge them β€” bring them to the user with options + recommendation: - -1. **Instance attribute layout.** Match WB's `InstanceData` struct - (80 bytes including CellId/Flags fields we don't use) for global - VAO compatibility, or define a minimal acdream-specific version - (mat4 + handle = ~72 bytes padded to 80)? - -2. **Bindless handle generation strategy.** - - At texture upload time? (Eager β€” every texture that lands in - `TextureCache` gets a handle. Memory cost ~per-texture state.) - - On first draw lookup? (Lazy β€” cache fills as scene exercises - content. Possible first-use stall.) - - At spawn time via the spawn adapter? (Tied to lifecycle. Cleanest - but requires touching the spawn path.) - -3. **Translucent pass structure.** Three sub-indirect-draws (one per - blend mode) or a single sorted indirect buffer with per-instance - blend mode + state-flip at sub-pass boundaries? Or: just iterate - per-group like N.4 for translucent only (translucent groups are a - small fraction of total)? - -4. **Persistent-mapped indirect + instance buffers.** Use - `GL_ARB_buffer_storage` + `MAP_PERSISTENT_BIT | MAP_COHERENT_BIT`? - Triple-buffered ring + sync object? Or stick with `glBufferData` - (still one upload per frame, just larger)? Persistent mapping is - ~2-5% per-frame win in our context but adds buffer-management - complexity. - -5. **Shader unification.** Keep `mesh_instanced` for legacy + add - `mesh_indirect` for modern, or replace `mesh_instanced` entirely? - Replacement requires the legacy `InstancedMeshRenderer` (escape - hatch under `ACDREAM_USE_WB_FOUNDATION=0`) to also use the new - shader, which... probably doesn't matter if we delete legacy in - N.6 anyway. Brainstorm. - -6. **Conformance test strategy.** N.4 used visual verification at - Holtburg as the gate. N.5's gate is "no visual regression vs N.4 - AND measurable CPU win." How do we measure CPU? `[WB-DIAG]` - counters give draw count + group count; we need frame-time - counters too. Add to the dispatcher? Use a profiler? - -7. **Per-instance entity bindless.** `TextureCache.GetOrUpload*` - returns a GL name. The dispatcher (or `TextureCache` itself) needs - to convert that to a bindless handle. Design questions: - - Where does the conversion happen? - - When is the texture made resident? (Residency is global state; - too many resident textures hits driver limits.) - - What about palette/surface overrides β€” same caching key as the - name, just a parallel handle dictionary? - -8. **Escape hatch.** N.4 keeps `ACDREAM_USE_WB_FOUNDATION=0` as a - fallback. N.5 needs to decide: does the new shader REPLACE the - N.4 dispatcher's draw path (so flag-on means N.5 modern path, - flag-off means legacy `InstancedMeshRenderer`)? Or do we add a - separate flag (`ACDREAM_USE_MODERN_DRAW`) so users can toggle - N.4 vs N.5 vs legacy independently? Three-way flag is more - complex but useful for A/B during rollout. - ---- - -## Spec structure - -After the brainstorm, the spec doc covers: - -1. **Architecture diagram** β€” how `WbDrawDispatcher` changes shape. - Where the indirect buffer lives. Where bindless handles flow from. -2. **Instance data layout** β€” exact struct, byte offsets, GL attribute - pointer setup. -3. **TextureCache changes** β€” new methods, new cache, residency - policy. -4. **Shader files** β€” name(s), version, extensions, in/out variables. -5. **Conformance tests** β€” what to write, what coverage to claim. -6. **Acceptance criteria** β€” visual identity to N.4 + measured CPU - delta. -7. **Risks** β€” driver bugs in bindless / indirect, residency limits, - shader compile issues on weird GPUs, the legacy escape hatch - breaking. - -Spec lives at: `docs/superpowers/specs/2026-05-XX-phase-n5-modern-rendering-design.md`. - -## Plan structure - -After the spec, the plan doc lays out the week-by-week task list. -Match N.4's plan structure (living document, task checkboxes, commit -SHAs appended, adjustments documented inline). Plan lives at: -`docs/superpowers/plans/2026-05-XX-phase-n5-modern-rendering.md`. - -Suggested initial breakdown (brainstorm + spec will refine): - -- **Week 1** β€” Plumbing: bindless handle generation in `TextureCache`, - shader rewrite (compile + bind), instance-attrib layout updated to - mat4+handle. Dispatcher still uses per-group draws but reads - textures bindless. Validate: visual identical to N.4. -- **Week 2** β€” Indirect: build `DrawElementsIndirectCommand` buffer - per frame, switch to `glMultiDrawElementsIndirect`. Three-pass - translucent (or whatever brainstorm decides). Validate: visual - identical, draw-call count drops to 2-4 per frame. -- **Week 3** β€” Polish + ship: persistent-mapped buffers if brainstorm - voted yes, profiler/counters, visual verification, flag flip, plan - finalization. - ---- - -## Acceptance criteria for the whole phase - -- Visual output identical to N.4 (no character regressions, no - scenery missing, no z-fighting introduced) -- `[WB-DIAG]` shows `drawsIssued` ≀ ~5 per frame (down from N.4's - few hundred) -- Frame time measurably lower in dense scenes (specify what scenes - to test in the spec β€” probably Holtburg courtyard + Foundry - interior) -- All tests still green (940/948 + any new conformance tests) -- `ACDREAM_USE_WB_FOUNDATION=0` escape hatch still works -- Plan doc finalized, roadmap updated, memory captured if N.5 - surfaces durable lessons (it almost certainly will β€” bindless - + indirect both have well-known driver gotchas) - ---- - -## What you'll be doing in the first 30 minutes - -1. Read this handoff in full. -2. Read CLAUDE.md "WB integration cribs" section. -3. Read `WbDrawDispatcher.cs` end-to-end. -4. Skim WB's `StaticObjectModern.vert/frag` + `ObjectMeshManager.UploadGfxObjMeshData` - to ground the reference. -5. Verify build is green: `dotnet build`. -6. Verify N.4 ship is intact: `dotnet test --filter "FullyQualifiedName~Wb|MatrixComposition"` - should produce 60 passing tests, 0 failures. -7. Invoke the `superpowers:brainstorming` skill with the user. Walk - through the 8 brainstorm questions above. Capture decisions in a - spec. -8. Write the spec at the path above. -9. Write the plan at the path above. -10. Begin Week 1 implementation per the plan. - -Don't skip the brainstorm. Multi-draw indirect + bindless have several -real driver-compatibility / API-shape decisions that need user input, -not "the agent makes a call and goes." This phase is structurally the -same shape as N.4 β€” brainstorm β†’ spec β†’ plan β†’ tasks-with-checkboxes β†’ -commits-update-checkboxes β†’ final SHIP commit. - ---- - -## Things to NOT do - -- **Don't delete the legacy `InstancedMeshRenderer`.** It's the N.4 - escape hatch. N.6 retires it after N.5 is proven default-on. -- **Don't fork WB.** N.4 deliberately avoided fork patches by using - the side-table pattern (`AcSurfaceMetadataTable`). Stay on that - path. If you need data WB doesn't expose, add a side-table or - decode it yourself from dats. -- **Don't try to make per-instance entities use WB's `TextureAtlasManager`.** - That's N.6+ territory. acdream's `TextureCache` owns palette/surface - overrides because WB's atlas is keyed by `(surfaceId, paletteId, - stippling, isSolid)` and our overrides don't fit cleanly. Bindless - handles let us escape that mismatch β€” handles for both atlas-tier - AND per-instance-tier textures, no atlas adoption needed. -- **Don't skip visual verification.** N.4 surfaced three bugs at - visual verification that no test caught. Don't trust "build green + - tests pass" β€” exercise the rendering path with the local ACE server. -- **Don't extend the phase scope.** N.5 is bindless + indirect on - the existing rendering pipeline. Texture array atlas, GPU-side - culling, terrain wiring β€” all of those are subsequent phases. If - the brainstorm tries to expand, push back. - ---- - -## Reference: the N.4 dispatcher flow you're modifying - -``` -Draw(camera, landblockEntries, frustum, ...) { - // Phase 1: walk entities, build groups - foreach (entity, meshRef, batch) { - cull, classify into _groups[GroupKey] - } - - // Phase 2: lay matrices contiguously - // Phase 3: glBufferData(_instanceVbo, allMatrices) - // Phase 4: bind global VAO once - // Phase 5: opaque pass (sorted) - foreach (group in _opaqueDraws) { - glBindTexture(group.handle) - glBindBuffer(EBO, group.ibo) - glDrawElementsInstancedBaseVertexBaseInstance(...) - } - // Phase 6: translucent pass -} -``` - -After N.5, Phases 5 and 6 collapse to: - -``` -glBindBuffer(DRAW_INDIRECT_BUFFER, _opaqueIndirect) -glMultiDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_SHORT, 0, opaqueGroups.Count, sizeof(DEIC)) -glBindBuffer(DRAW_INDIRECT_BUFFER, _translucentIndirect) -// 3 sub-calls for translucent or 1 if shader-folded -glMultiDrawElementsIndirect(...) -``` - -That's the destination. Get there cleanly. - -Good luck. Holler at the user if any of the brainstorm questions feel -genuinely ambiguous after reading the references β€” they care about -this phase landing right and will engage on design questions. diff --git a/docs/research/2026-05-09-phase-n5b-handoff.md b/docs/research/2026-05-09-phase-n5b-handoff.md deleted file mode 100644 index 05d7558..0000000 --- a/docs/research/2026-05-09-phase-n5b-handoff.md +++ /dev/null @@ -1,445 +0,0 @@ -# Phase N.5b β€” Terrain on the Modern Rendering Path β€” Cold-Start Handoff - -**Created:** 2026-05-09, immediately after N.5 ship + roadmap A.5 addition. -**Audience:** the next agent picking up terrain rendering work. -**Purpose:** give you everything you need to start N.5b cold, without -spelunking through the N.5 session's history. - ---- - -## TL;DR - -N.5 just shipped: `WbDrawDispatcher` lifts entity rendering onto bindless -textures + `glMultiDrawElementsIndirect`. CPU dispatcher 1.23 ms / frame -median at Holtburg courtyard, ~810 fps sustained. **Entities only β€” -terrain is still on a separate legacy renderer.** - -**N.5b's job: port terrain rendering onto the same modern primitives that -N.5 just delivered.** Concretely: - -1. Replace `TerrainRenderer` + `TerrainChunkRenderer` (per-landblock VAO, - `glDrawElements`, `sampler2D` atlases) with a multi-draw-indirect - dispatcher analogous to `WbDrawDispatcher`, sharing the modern path's - bindless texture infrastructure where it makes sense. -2. Keep terrain visually identical to today. The legacy `TerrainAtlas` + - `terrain.vert/.frag` already render correctly; don't introduce visual - regressions. -3. Resolve issue #51 (WB's terrain split formula diverges from retail's - `FSplitNESW`) β€” see "Load-bearing constraint" below. - -The roadmap estimate is **~1 week** because the modern-path primitives -are already built. The actual work is porting + bridging + a real -correctness decision on the split formula. - ---- - -## Load-bearing constraint: Issue #51 (terrain split formula) - -This is the design decision that will dominate the brainstorm. **Read -`docs/ISSUES.md` issue #51 in full before brainstorming.** - -The short version: - -- **acdream's terrain split formula** is the retail-decomp `FSplitNESW` - (constants `0x0CCAC033` / `0x421BE3BD` / `0x6C1AC587` / `0x519B8F25`). - Documented in `CLAUDE.md` as **the** real AC formula. Ours is degree-2 - polynomial in (x,y). Used by: - - `src/AcDream.Core/Physics/TerrainSurface.cs:113-120` (physics β€” - `IsSplitSWtoNE`) - - `src/AcDream.Core/World/TerrainBlending.cs` (visual mesh) -- **WB's terrain split formula** in `references/WorldBuilder/WorldBuilder.Shared/Modules/Landscape/Lib/TerrainUtils.cs:44` - is LINEAR in (x,y). Different math; they cannot be algebraically - equivalent. They disagree on a meaningful fraction of cells β€” up to - ~2m height delta on sloped cells. -- **WB's `TerrainGeometryGenerator`** (the obvious adoption target for - N.5b's mesh path) uses WB's formula. If we adopt it wholesale, our - visual terrain disagrees with our physics (which uses retail's - formula). Player floats / sinks. Already-fixed bug class returns. - -**Three viable design paths** (the brainstorm has to pick one): - -- **Path A β€” Adopt WB's formula everywhere.** Switch both physics AND - visual mesh to WB's `CalculateSplitDirection`. Use WB's - `TerrainGeometryGenerator` directly. Visual + physics stay synced. - Risk: physics now disagrees with retail server-authoritative Z by up - to ~2m on sloped cells. Server-side validation (if any) might reject - movements; the player might "snap" to server's Z when packets land. - Need to confirm whether ACE actually validates Z or trusts the - client. Lowest implementation effort. - -- **Path B β€” Keep retail's formula; fork-patch WB.** Patch - `references/WorldBuilder/.../TerrainUtils.cs` to use retail's formula - in our fork. Push the patch to the `acdream` branch of the fork (per - the WB submodule plumbing fixed in the previous session). Submit - upstream PR if Chorizite wants it. Most retail-faithful. Implementation - effort: medium. Coordination overhead with upstream. - -- **Path C β€” Use WB's mesh layout but our formula.** Don't use WB's - `TerrainGeometryGenerator` directly. Instead port WB's *mesh layout* - (vertex buffer shape, index buffer per landblock, atlas integration) - into a new acdream-side `TerrainGeometryGenerator` that uses retail's - formula. Highest effort but cleanest separation β€” no fork patches. - -Recommendation in the brainstorm: probably **Path A** if quantification -shows ACE doesn't validate Z aggressively (retail's network protocol -is "client tells server position; server trusts within sanity bounds"), -otherwise **Path B**. Path C is overengineered for the level of -divergence. - -**Step 1 of the brainstorm:** quantify the divergence. Run WB's formula -+ retail's formula across all (lbX, lbY, cellX, cellY) tuples for -several representative landblocks (Holtburg, Foundry, open landscape, -some sloped terrain like Direlands). Record disagreement rate. If <5% -of cells disagree, Path A's risk is bounded; if >20%, Path B becomes -more attractive. - ---- - -## Where N.5 left things - -### Branch state - -After last session: -- `main` is at `a64cd11` ("docs(roadmap): add A.5 β€” two-tier streaming") -- N.5 SHIP at `27eaf4e` (merge commit) -- N.5 ship-amendment at `e0dbc9c` (legacy renderers retired) -- Legacy `InstancedMeshRenderer` + `StaticMeshRenderer` + `WbFoundationFlag` - ARE GONE. Bindless is mandatory; missing extensions throws - `NotSupportedException` at startup. - -### What works in N.5 - -- **Entity rendering:** `WbDrawDispatcher` does ~12-15 GL calls per frame - for all visible entities regardless of scene complexity. Three SSBO - uploads (instance matrices @ binding=0, batch data @ binding=1, - indirect commands) + 2 `glMultiDrawElementsIndirect` calls (opaque + - transparent passes). -- **Bindless texture infrastructure:** `BindlessSupport` wrapper + - `TextureCache` parallel `UploadRgba8AsLayer1Array` path + three - `Bindless*` `GetOrUpload` methods + two-phase `Dispose`. All textures - on the WB modern path are 1-layer `Texture2DArray` + `sampler2DArray`. -- **mesh_modern.vert/.frag** preserves the full `SceneLighting` UBO - (8 lights + fog + lightning flash + per-channel clamp) β€” visual - identity to N.4 confirmed at user gates. -- **Diagnostic:** CPU stopwatch + GL_TIME_ELAPSED queries logged via - `[WB-DIAG]` (GPU timing currently shows 0/0 β€” query polling needs - double-buffering, deferred to N.6). - -### What N.5b inherits - -These are levers N.5b will pull on: - -- **`BindlessSupport`** at `src/AcDream.App/Rendering/Wb/BindlessSupport.cs` - β€” already wraps `ArbBindlessTexture`. Reusable for terrain textures. -- **`DrawElementsIndirectCommand` struct** at `src/AcDream.App/Rendering/Wb/DrawElementsIndirectCommand.cs` - β€” 20-byte layout, ready to populate per-landblock terrain commands. -- **`BuildIndirectArrays` helper** at `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` - β€” pure CPU layout helper, currently scoped to entities; could - generalize for terrain. -- **`TextureCache`** with parallel Texture2DArray bindless cache β€” - but terrain has its own `TerrainAtlas` (multi-layer texture array - for splat blending). N.5b decides whether to integrate or keep - separate. -- **`SceneLightingUbo`** at binding=1 β€” terrain.frag already consumes - it; the new modern terrain shader continues that. -- **Retail's `FSplitNESW`** in `src/AcDream.Core/World/TerrainBlending.cs` - β€” the formula to preserve (or replace, per Path A/B/C decision). - -### What still uses the legacy path (NOT N.5b's job) - -- **Sky rendering** (`SkyRenderer.cs`) β€” N.8 territory. -- **Particles** (`ParticleRenderer.cs`) β€” N.8 territory. -- **Debug lines** (`DebugLineRenderer.cs`) β€” fine as-is. -- **UI / text** (`TextRenderer.cs` + ImGui) β€” fine as-is; ImGui has its - own backend. - ---- - -## What N.5b is β€” technical detail - -### Today's terrain stack (1383 lines acdream + ~140 lines shaders) - -| File | Lines | Role | -|---|---|---| -| `src/AcDream.App/Rendering/TerrainRenderer.cs` | 247 | Top-level orchestration; per-landblock cull + draw | -| `src/AcDream.App/Rendering/TerrainChunkRenderer.cs` | 454 | Per-landblock VAO + IBO management; `glDrawElements` per visible chunk | -| `src/AcDream.App/Rendering/TerrainAtlas.cs` | 386 | Multi-layer `Texture2DArray` atlas for terrain splat textures | -| `src/AcDream.App/Rendering/Shaders/terrain.vert` | 147 | Per-vertex world position, normal, UV, palCode | -| `src/AcDream.App/Rendering/Shaders/terrain.frag` | 149 | Splat blending across 4 corner textures | - -**Per-frame today:** for each visible landblock, bind its VAO + IBO, -bind the terrain texture atlas, set per-landblock uniforms, issue -`glDrawElements`. With 25 landblocks at default radius=2, that's ~25 -draw calls per frame for terrain (cheap, but doesn't scale). - -### WB's terrain stack (1937 lines + ~200 lines shaders) - -| File | Lines | Role | -|---|---|---| -| `references/WorldBuilder/Chorizite.OpenGLSDLBackend/Lib/TerrainRenderManager.cs` | 1023 | Top-level coordinator; uses multi-draw-indirect already | -| `references/WorldBuilder/Chorizite.OpenGLSDLBackend/Lib/TerrainGeometryGenerator.cs` | 326 | Mesh generation per landblock (uses WB's split formula β€” see #51) | -| `references/WorldBuilder/Chorizite.OpenGLSDLBackend/Lib/LandSurfaceManager.cs` | 588 | Texture atlas management + alpha mask generation for splat blending | -| `references/WorldBuilder/Chorizite.OpenGLSDLBackend/Shaders/Landscape.vert/.frag` | ~200 | Modern shader; consumes SSBO instance data + bindless atlas handle | - -WB's renderer is structurally close to what N.5b targets. Key differences -from acdream: - -- WB uses **uint32 indices** (`DrawElementsType.UnsignedInt`) for - terrain β€” landblocks have more vertices than fit in ushort range. - N.5's `WbDrawDispatcher` uses `UnsignedShort` for entities. -- WB packs all visible terrain into shared mesh buffers + dispatches - via `glMultiDrawElementsIndirect`. We can mirror that pattern. -- WB's `LandSurfaceManager` builds per-landblock alpha masks for splat - blending; this is the bulk of its 588 lines. Different model from - our `TerrainAtlas` which uses palCode-based blending in the fragment - shader. - -### What N.5b actually does - -Roughly four sub-pieces: - -1. **Terrain mesh on global VBO/IBO.** Following N.5's pattern, all - visible terrain landblocks pack into a single global vertex buffer - + index buffer. Per-landblock entries become `DrawElementsIndirectCommand` - records with `firstIndex` + `baseVertex` offsets. One - `glMultiDrawElementsIndirect` call per pass. -2. **Bindless terrain atlas.** Either (a) port `TerrainAtlas` to use - bindless handles + sampler2DArray (small change, keeps current - blending math), or (b) adopt WB's `LandSurfaceManager` (bigger - change, switches to alpha-mask blending). Brainstorm decides. -3. **New shader `terrain_modern.vert/.frag`** that: - - Reads per-landblock data from an SSBO (analogous to - mesh_modern's `Batches[]`) - - Samples the terrain atlas via bindless `sampler2DArray` handle - - Continues to consume `SceneLighting` UBO @ binding=1 (no - visual identity regression vs N.4 β€” same lighting math) -4. **Resolve issue #51** per Path A/B/C decision in the brainstorm. - -### Per-frame target shape - -``` -// Once at init: -Build global terrain VAO + VBO + IBO (resizable; grows as landblocks stream in) -Generate bindless handles for terrain atlas - -// Per frame: -1. Frustum cull landblocks (existing per-landblock AABB test) -2. Build per-visible-landblock IndirectGroupInput list -3. Upload _terrainBatchSsbo + _terrainIndirectBuffer -4. glBindVertexArray(globalTerrainVao) -5. glBindBufferBase(SHADER_STORAGE_BUFFER, 1, _terrainBatchSsbo) -6. glBindBuffer(DRAW_INDIRECT_BUFFER, _terrainIndirectBuffer) -7. glMultiDrawElementsIndirect(...) // ONCE per pass β€” opaque pass - (terrain has no transparent; one indirect call total) -``` - -Total ~6-8 GL calls per frame for terrain regardless of scene size. -At radius=5 (121 landblocks) this is the same number of GL calls as -at radius=2 (25 landblocks). - ---- - -## Files to read before brainstorming - -In rough order: - -1. **`docs/ISSUES.md` issue #51** (49-103). Load-bearing constraint. -2. **`CLAUDE.md`** the "Reference hierarchy by domain" terrain row + - "Reference repos: check ALL FOUR" β€” terrain math is one of the - places where checking multiple references matters most. -3. **acdream terrain stack:** - - `src/AcDream.App/Rendering/TerrainRenderer.cs` (247 lines, easy - read) - - `src/AcDream.App/Rendering/TerrainChunkRenderer.cs` (454 lines β€” - this is the per-landblock GL plumbing that goes away in N.5b) - - `src/AcDream.App/Rendering/TerrainAtlas.cs` (386 lines β€” - multi-layer atlas) - - `src/AcDream.App/Rendering/Shaders/terrain.vert/.frag` (~300 - lines combined) - - `src/AcDream.Core/World/TerrainBlending.cs` (the FSplitNESW - side; preserve or replace) -4. **WB terrain stack:** - - `references/WorldBuilder/Chorizite.OpenGLSDLBackend/Lib/TerrainRenderManager.cs` - (1023 lines β€” the model to mirror; multi-draw indirect already - in place) - - `references/WorldBuilder/Chorizite.OpenGLSDLBackend/Lib/TerrainGeometryGenerator.cs` - (326 lines β€” uses WB's split formula; per #51) - - `references/WorldBuilder/Chorizite.OpenGLSDLBackend/Lib/LandSurfaceManager.cs` - (588 lines β€” alpha-mask atlas; alternative to our `TerrainAtlas`) - - `references/WorldBuilder/Chorizite.OpenGLSDLBackend/Shaders/Landscape.vert/.frag` - - `references/WorldBuilder/WorldBuilder.Shared/Modules/Landscape/Lib/TerrainUtils.cs:44` - (CalculateSplitDirection β€” WB's formula) -5. **N.5 plan + spec** (cribs for the modern-path pattern): - - `docs/superpowers/plans/2026-05-08-phase-n5-modern-rendering.md` - (what we did, including amendments) - - `docs/superpowers/specs/2026-05-08-phase-n5-modern-rendering-design.md` - (decisions log) -6. **Memory: `project_phase_n5_state.md`** β€” three high-value gotchas - from N.5 (texture target lock-in, bindless Dispose order, - GL_TIME_ELAPSED double-buffering). All apply to N.5b. - ---- - -## Brainstorm questions - -These are the questions to resolve in the brainstorm step. Don't -prejudge β€” bring them to the user with options + recommendation: - -1. **Path A vs B vs C** for issue #51 (the terrain split formula). The - biggest decision; everything else flows from it. Should be - informed by quantifying the divergence rate first (run both - formulas across representative landblocks). - -2. **Atlas model.** Keep `TerrainAtlas` (palCode-based fragment shader - blending) and just bindless-ify it, or adopt WB's `LandSurfaceManager` - (alpha-mask blending)? Tradeoff: minimal change vs alignment with - WB. Visual outcome should be identical either way. - -3. **Mesh ownership.** Use a single global VBO/IBO for all terrain - (mirror N.5's pattern), or per-landblock VBO/IBO with multi-draw - indirect over them? Single global is more cache-friendly + more - like N.5, but requires resizable buffer management. Per-landblock - is simpler but doesn't share the IBO across draws. - -4. **Index format.** N.5 uses `UnsignedShort` (max 64K verts per - draw). Terrain landblocks have many more verts than that. WB uses - `UnsignedInt`. Just commit to `UnsignedInt` for terrain? - -5. **Shader unification.** Separate `terrain_modern.vert/.frag` or - merge with `mesh_modern.vert/.frag` via uniforms? Probably separate - since the vertex layouts differ (terrain has palCode; entities - have UV). - -6. **Streaming integration.** Today's `TerrainChunkRenderer` integrates - with the streaming loader (landblocks come and go). N.5b's global - buffer model needs a strategy for adding/removing landblocks from - the global VBO/IBO without per-add reallocation. Free-list / - compaction / fixed-slot allocator? - -7. **Conformance test.** Per the lessons from N.2, "WB's terrain - formula differs from retail" β€” we need a test that proves our - visual terrain matches our physics terrain (i.e., visual mesh Z - at any (X,Y) equals `TerrainSurface.GetHeight(X,Y)`). Run a sweep - across ~1M (X,Y) points; assert |delta| < epsilon. - -8. **Visual verification gate.** Holtburg + Foundry + sloped terrain - (Direlands?) + cell transitions. The split-formula-disagreement - bug class shows up as terrain "wobble" at cell boundaries β€” that's - the specific thing to look for. - ---- - -## Acceptance criteria for the whole phase - -- Visual terrain identical to current legacy path (no missing chunks, - no z-fighting at cell boundaries, no texture seams) -- `[WB-DIAG]` shows terrain accounting for ~6-8 GL calls per frame - regardless of scene size (currently scales with visible landblock - count, ~25-121 calls) -- Frame time measurably lower in dense-terrain scenes (specify scenes - in the spec β€” probably radius=5 outdoor roaming) -- Conformance test: visual mesh Z agrees with `TerrainSurface.GetHeight` - within epsilon across a 1M-point sweep -- All existing tests still green -- The split-formula decision (#51) is resolved with a clear writeup - in the spec - ---- - -## What you'll be doing in the first 30 minutes - -1. Read this handoff in full. -2. Read `docs/ISSUES.md` issue #51 in full. -3. Read CLAUDE.md "Reference hierarchy by domain" terrain row. -4. Read `TerrainRenderer.cs` + `TerrainChunkRenderer.cs` end-to-end. -5. Skim `TerrainRenderManager.cs` (WB's) β€” at least the multi-draw - indirect dispatch section. -6. Verify build is green: `dotnet build`. -7. Verify N.5 ship is intact: `dotnet test --filter "FullyQualifiedName~Wb|FullyQualifiedName~MatrixComposition|FullyQualifiedName~TextureCacheBindless"` should produce 71 passing tests, 0 failures. -8. Quantify the formula divergence (Path A/B/C decision input): - write a one-shot test that runs both formulas across all - (lbX, lbY, cellX, cellY) tuples for ~10 representative landblocks - and reports disagreement rate. -9. Invoke the `superpowers:brainstorming` skill with the user. Walk - through the 8 brainstorm questions above. Bring the formula - divergence number to inform the Path A/B/C decision. -10. Write the spec. -11. Write the plan. -12. Begin Week 1 implementation per the plan. - -Don't skip the brainstorm. The terrain split formula decision (Path -A/B/C) has real downstream consequences β€” physics, server-Z agreement, -fork-patching of WB. Needs explicit user input, not "the agent makes -a call and goes." This phase is structurally the same shape as N.5 β€” -brainstorm β†’ spec β†’ plan β†’ tasks-with-checkboxes β†’ commits-update-checkboxes -β†’ final SHIP commit. - ---- - -## Things to NOT do - -- **Don't adopt WB's terrain code wholesale without resolving #51 - first.** The split formula decision affects the entire pipeline; - patching it after-the-fact requires re-doing visual + physics + the - TerrainGeometryGenerator port. -- **Don't introduce a per-cell wobble at landblock boundaries.** That's - the visible signature of the formula disagreement. If you see it - during visual verification, the formula isn't aligned between your - physics and visual paths. -- **Don't break the existing `[WB-DIAG]` instrumentation.** Add a - separate counter for terrain (`terrainDrawsIssued`) so the entity - + terrain perf can be observed independently. -- **Don't bundle A.5 (two-tier streaming + horizon LOD) into this - phase.** N.5b is "terrain on modern path"; A.5 is "split the radius - + LOD." Different scopes, different brainstorms. A.5 might become - natural to pick up next once N.5b lands. -- **Don't try to re-port `FSplitNESW` if you're going Path A.** The - whole point of Path A is to commit to WB's formula. If you keep - retail's formula via Path B/C, do it once, definitively. -- **Don't skip the formula-divergence quantification.** Step 8 of - the first 30 minutes. The Path decision should be data-informed, - not gut-feel. <5% divergence makes Path A bounded-risk; >20% makes - Path B/C more attractive. -- **Don't skip visual verification.** The split-formula bug class - shows up as cell-boundary wobble that's hard to spot in screenshots - but obvious in motion. Walk a sloped landblock during verification. -- **Don't extend the phase scope.** N.5b is "terrain on modern path." - Sky, particles, EnvCells β€” all subsequent phases. If the brainstorm - tries to expand, push back. - ---- - -## Reference: the N.5 dispatcher flow you're mirroring - -``` -WbDrawDispatcher.Draw(...) { - // Phase 1: walk entities, build groups - // Phase 2: lay matrices contiguously - // Phase 3: build BatchData + DEIC arrays via BuildIndirectArrays - // Phase 4: upload 3 SSBOs (instances, batches, indirect) - // Phase 5: bind global VAO + SSBOs - // Phase 6: opaque pass β€” glMultiDrawElementsIndirect - // Phase 7: transparent pass β€” glMultiDrawElementsIndirect -} -``` - -For terrain the shape is similar but simpler: - -``` -TerrainModernDispatcher.Draw(...) { - // Phase 1: walk visible landblocks, frustum cull - // Phase 2: build per-landblock IndirectGroupInput list - // (one entry per visible landblock β€” typically 25-121) - // Phase 3: upload 2 SSBOs (terrain batch data, indirect commands) - // (no per-instance buffer needed β€” terrain isn't instanced) - // Phase 4: bind global terrain VAO + SSBOs - // Phase 5: opaque pass ONLY β€” glMultiDrawElementsIndirect -} -``` - -Total ~6-8 GL calls per frame for terrain. That's the destination. - -Good luck. The split-formula decision is the only really hard call; -everything else is mechanical port work on top of N.5's substrate. -Holler at the user if anything in #51's three paths feels genuinely -ambiguous after reading the references. diff --git a/docs/research/2026-05-10-holtburger-network-stack-study.md b/docs/research/2026-05-10-holtburger-network-stack-study.md deleted file mode 100644 index ee05549..0000000 --- a/docs/research/2026-05-10-holtburger-network-stack-study.md +++ /dev/null @@ -1,177 +0,0 @@ -# Holtburger network stack β€” study & port candidates for acdream - -**Date:** 2026-05-10 -**Holtburger reference:** github.com/merklejerk/holtburger, vendored at `references/holtburger/`, fast-forwarded from `88b19bd` β†’ `629695a` (237 commits, ~3 months of work). -**Method:** Four parallel research agents β€” three over holtburger's transport, handshake, and movement; one inventorying acdream's current `src/AcDream.Core.Net/`. Findings cross-referenced and ranked by ROI. - -## TL;DR - -Holtburger has shipped real, citeable fixes since our last pin that we should adopt. The biggest tactical wins are: - -1. **A handful of one-line MoveToState fixes** that are likely candidates for the "remote retail observer sees acdream's player not perfect" issue (#L.X). -2. **Three small handshake/transport corrections** β€” LoginComplete-on-teleport, EchoResponse reply, port-switch race β€” each <1 hour and each measurable. -3. **A real retransmit subsystem we're missing entirely.** Our `WorldSession` parses retransmit requests, doesn't honor them, has no resend buffer, and never asks for a resend. Lost packets just vanish. Holtburger's `session/reliability.rs` is the reference-quality pattern. - -Separately, the audit surfaced one painful finding about acdream itself: **roughly half of our outbound `Messages/` library is dead code** β€” InteractRequests, InventoryActions, SocialActions, AllegianceRequests, CastSpellRequest, AppraiseRequest, and most of CharacterActions are built and unit-tested but have no `WorldSession.Send*` wrapper and no live caller. Phase B.4 (Use/UseWithTarget) per memory shipped, but the audit found no in-app caller. Either we left wiring on the table or there's an integration drift to investigate. - -The remainder of this doc is organized as: ranked port candidates β†’ confirmations of what we got right β†’ traps (where holtburger is wrong or stubbed) β†’ recent commits worth knowing β†’ recommended sequencing β†’ cross-reference file map. - ---- - -## 1. Ranked port candidates (highest ROI first) - -### 1.1 Outbound MoveToState audit β€” concrete suspects for the "observer not perfect" bug - -Five specific items where holtburger's wire format is likely tighter than ours. Each is a small change in our `Messages/MoveToState.cs` builder; together they're the most likely cause of remote retail observers reporting our player "lagging forward" or "walking when running." - -| # | Suspect | Holtburger reference | -|---|---------|----------------------| -| a | **`current_hold_key` always set on non-stop MoveToState.** Holtburger's drive emit seeds `flags = CURRENT_HOLD_KEY` and writes `current_hold_key = HoldKey::Run`(2) for run, `HoldKey::None`(1) for walk. ACE's relay code may treat its absence as "unknown" and broadcast Walk to observers. | `crates/holtburger-core/src/client/movement/common.rs:151-153` | -| b | **`commands[]` array MUST be empty on held WASD.** Holtburger never puts a `MotionItem` in `commands[]` for held movement β€” only for transient slash commands like `/dance`. If acdream is putting one in for held W (or letting movement_sequence bump per-frame), every observer's `apply_self_update_motion` re-applies the same sequence as a fresh interpolation start β€” exactly the symptom. | `system.rs:743-766` (`execute_transient_motion_at`) | -| c | **`turn_speed` always emitted alongside `TURN_COMMAND`.** Holtburger writes 1.5 rad/s for Run, 1.0 rad/s for Walk; the `TURN_SPEED` flag is *always* set whenever `TURN_COMMAND` is. Omitting it lets ACE default to 0 β†’ "smoothly but slowly" turn observed. | `common.rs:184-186, 226-231` | -| d | **Dedup gate must include gait.** Holtburger's `should_send_motion_state_pulse` compares the full `(MotionState, MotionStyle)`. If acdream's dedup is keyed on only `(forward_command, hold_key)` it would suppress the Runβ†’Walk transition (since `forward_command = WalkForward = 0x45000005` for both), explaining the Run↔Walk observer bug specifically. | `system.rs:916-926` | -| e | **Don't emit `turning` field when locomotion is non-zero.** Recent fix in commit `336cbad`: `autonomous_wire_motion_state` no longer emits `turning` when locomotion β‰  0 (avoids server-side double-correction where it interpolates turn AND locomotes). | `crates/holtburger-core/src/client/movement/common.rs` | - -**Recommended action:** a side-by-side audit of [WorldSession.cs:6067-6089](src/AcDream.Core.Net/WorldSession.cs:6067) (MoveToState builder) and [Messages/MoveToState.cs](src/AcDream.Core.Net/Messages/MoveToState.cs) against holtburger `common.rs:122-186` and `system.rs:710-1000`. File whichever items don't already match as `#L.X.a-e` issues. - -### 1.2 LoginComplete on every PlayerTeleport, not just first PlayerCreate - -Holtburger sends `GameAction::LoginComplete` (0x00A1) **both** on first `PlayerCreate` (0xF746) AND on every `PlayerTeleport` (0xF74A) β€” no de-dup, server tolerates multiples. acdream sends it only on first PlayerCreate. Likely explains some portal-transition glitches. - -References: holtburger `messages.rs:433-467` (PlayerCreate), `messages.rs:480-487` (PlayerTeleport). acdream sends only at [WorldSession.cs:648](src/AcDream.Core.Net/WorldSession.cs:648). - -**Cost:** ~5 lines. - -### 1.3 EchoRequest β†’ EchoResponse reply - -We parse `EchoRequest` from the optional header but never reply. ACE pings periodically; the missing response is a likely contributor to Network Timeout drops in long sessions. Holtburger handles it inline in the recv-message dispatcher. - -Reference: holtburger `crates/holtburger-session/src/session/receive.rs::finalize_ordered_server_packet` and the optional-header iterator at `crates/holtburger-session/src/optional_header.rs:59-141`. - -**Cost:** ~30 lines (parse the EchoRequest payload, build EchoResponse with mirrored time, send as control packet). - -### 1.4 Port-switch race fix (commit `403bc98`) - -On `ConnectRequest`, our `WorldSession` eagerly sets `_connectEndpoint = port+1`. Holtburger's recent fix introduces `pending_server_source_addr`: the new port is staged but `server_source_addr` is only updated when an actual packet arrives from the new port. ACE deployments occasionally send one more packet from `port` after the activation, and our code drops them. - -References: holtburger `session/auth.rs:42-47` (stage), `session/receive.rs:17-51` (confirm on first packet from new port). - -**Cost:** ~20 lines, one new field on `WorldSession`. - -### 1.5 Non-blocking 200 ms handshake delay - -We use `Thread.Sleep(200)` between receiving ConnectRequest and sending ConnectResponse on `port+1`. Holtburger queues ConnectResponse with `ready_at = Instant::now() + 200ms` and lets the recv loop keep draining during the gap (handles any inbound TimeSync that arrives in the window). - -Reference: holtburger `session/auth.rs:42-66`, queued via `pending_control_packets` flushed by the recv loop. (Their old form, deleted in `99974cc`, used `tokio::time::sleep` and matched our blocking pattern.) - -**Cost:** ~40 lines (small "deferred control packet" queue + flush check). - -### 1.6 AutonomousPosition cadence audit - -We have **three policies** in play, and at least two are wrong: - -- **acdream:** fixed 200 ms heartbeat (per `memory/project_retail_motion_outbound`) -- **holtburger:** fixed 1 s heartbeat, unconditional regardless of motion (`common.rs:22`, `system.rs:858-893`) -- **cdb retail trace (memory):** AutoPos appears gated on actual motion - -Most likely retail wins (cdb is observing real client behavior). If retail truly suppresses AutoPos when stationary, our 5Γ— over-emission triggers ACE-side over-validation and may contribute to the observer-side jitter. **Recommended:** another cdb idle trace to confirm retail's exact behavior, then converge to it. - -### 1.7 Retransmit machinery (entire subsystem) - -Largest delta from holtburger. We are missing: - -- **A retransmit cache.** Holtburger's `MAX_CACHED_PACKETS=512`, LRU-style, drops oldest when full (`reliability.rs:32-37`). -- **Server-requested retransmits.** When the server asks for resends, holtburger re-encrypts with current ISAAC + RETRANSMISSION flag and replays from cache (`reliability.rs:135-186`). -- **Client-issued retransmit requests.** When inbound seq has gaps, holtburger sends `RequestRetransmit` for up to 115 seqs in a 256-seq window, rate-limited to once per second (`MAX_RETRANSMIT_SEQUENCE_IDS=115`, `MAX_RETRANSMIT_SEQUENCE_WINDOW=256`, `REQUEST_RETRANSMIT_INTERVAL=1s`). -- **`Iteration` field handling.** Our `PacketHeader.Iteration` is always 0; holtburger increments on retransmit. -- **`ISAAC::search` for out-of-order ENCRYPTED_CHECKSUM packets.** Out-of-order packets have ISAAC keys that have already advanced. Holtburger scans forward up to 256 keys, stashing each skipped key in `xors: HashSet` for later out-of-order packets to consume via `consume_key_value` (`crypto.rs:73-93`). **A naive port either drops the out-of-order packet or corrupts the ISAAC stream.** If our IsaacRandom doesn't have a search-and-stash mode, this is a latent bug waiting for any UDP loss event. - -Our `WorldSession` class doc explicitly defers this work (`WorldSession.cs:29` "ACK pump, retransmit handling … deferred"). Symptoms when it's missing: any packet loss β†’ silent state divergence, eventual desync, "purple haze" / Network Timeout drops. - -**Cost:** 1-2 days. The whole pattern is in holtburger's `reliability.rs` (196 lines) plus the ISAAC search-mode in `crypto.rs:73-93`. - -### 1.8 Fragment assembler TTL + outbound multi-fragment split - -Two smaller correctness gaps: - -- **Inbound:** Our `FragmentAssembler` has no TTL. If a multi-fragment server message loses its middle fragment, the partials sit forever. Memory leak in any long session that sees UDP loss. Holtburger's reassembler tracks completion per `(sequence, id)` and lives inside `process_fragment` in `send.rs`. -- **Outbound:** Our `GameMessageFragment.BuildSingleFragment` throws on body > 448 bytes. Anything that needs splitting (long /tells, big inventory queries, large appraisals) silently can't be sent. Note: **holtburger doesn't do outbound fragmentation either** (`send_message` always emits `count: 1`, `send.rs:298`) β€” they're betting on UDP-level fragmentation. So this isn't a holtburger crib; it's a hole in both. AC2D + Chorizite are the better references when we get there. - ---- - -## 2. Confirmations β€” we're doing it right - -Three places where the audit confirmed our existing approach matches the reference: - -- **Run/walk encoding via WalkForward + HoldKey.Run/None.** Holtburger sends `forward_command = 0x45000005 (WalkForward)` for **both** walk and run; the distinction is in `forward_hold_key` (Run=2 vs None=1) and `forward_speed`. ACE upgrades server-side. Test pinning this contract: `holtburger system/tests.rs:404-428`. -- **Two-step EnterWorld** (`0xF7C8 CharacterEnterWorldRequest` β†’ wait for `0xF7DF ServerReady` β†’ `0xF657 CharacterEnterWorld`). -- **ACK on every received packet with seq > 0.** Holtburger's `recv_packet_with_addr` queues an ack for every received packet with `sequence > 0 && flags != ACK_SEQUENCE`. Outbound `send_message` auto-piggybacks the latest server seq onto the next data packet; standalone ACKs flush only when nothing naturally goes out. (Worth double-checking that our `SendAck` is called automatically on `ProcessDatagram`, not as a separate periodic pump.) - -One thing **worth re-verifying** because it's easy to invert: ISAAC seeding direction. Holtburger uses `isaac_c2s = Isaac::new(crd.client_seed)` and `isaac_s2c = Isaac::new(crd.server_seed)` β€” i.e. the wire field labelled `client_seed` seeds the C2S keystream, and vice versa. Worth a 30-second check that our `WorldSession` does the same. - ---- - -## 3. Don't crib these (holtburger gaps / wrong) - -- **Outbound fragmentation:** holtburger doesn't do it. Hole in both projects. Use AC2D + Chorizite when needed. -- **Jump (0xF61B):** holtburger never sends Jump. The TUI client can't jump. `JumpActionData` is decoder-only. Use cdb retail trace + Chorizite.ACProtocol for jump format reference. -- **Initial run_rate_scalar fallback:** holtburger uses 4.5 (max-cap formula, run_skill β‰₯ 800); acdream uses 2.4-2.94 default. Retail formula: `(load_mod * (run_skill / (run_skill + 200) * 11) + 4) / 4`. The right pre-PlayerDescription default depends on what retail does β€” cdb trace will settle it. -- **AutoPos cadence:** holtburger's 1-second unconditional heartbeat is probably wrong (cdb retail trace says gated on motion). Don't copy this verbatim; investigate first. - ---- - -## 4. Recent commits worth knowing (last 237) - -| Commit | Date | Intent | Relevance | -|--------|------|--------|-----------| -| `99974cc` | 2026-04-06 | "Fix/session issues" β€” splits 673-line `lib.rs` into `session/{api,auth,receive,send,reliability,types}`. **Adds the missing C↔S retransmit logic.** Replaces `tokio::sleep(200ms)` with deferred control-packet queue. | Read this diff if you read only one. | -| `403bc98` | 2026-04-21 | "do not switch ports prematurely" (#158). Pending vs confirmed source-port. | Apply same pattern to `WorldSession`. | -| `336cbad` | 2026-04-?? | "fix: more movement fixes". `autonomous_wire_motion_state` no longer emits `turning` when locomotion β‰  0. | Likely also a bug class in our outbound MoveToState. | -| `797aece` | 2026-04-06 | DISCONNECT now carries `id = client_id` instead of 0. | One-line fix on our `Dispose` path. | -| `854c1bb` | (older) | "Feat/simulation system" (#105) β€” added the entire 2222-LOC `client/movement/{common,system}.rs`. | Foundation everything else builds on. | - -Nothing in 237 commits changes LoginRequest payload, ConnectRequest parse, ISAAC seeding, or EnterWorld message ordering. The wire format is unchanged from what acdream targets β€” the deltas are internal architecture and bug fixes. - ---- - -## 5. Recommended sequencing - -**Tier 1 β€” Quick wins (under an hour each, high signal-to-noise):** -1. MoveToState audit fixes (1.1.a-e) β€” file as `#L.X.a-e`, batch into one PR -2. LoginComplete on PlayerTeleport (1.2) -3. EchoRequest β†’ EchoResponse reply (1.3) -4. Port-switch race fix (1.4) -5. Non-blocking handshake delay (1.5) -6. Disconnect carries client_id (`797aece` finding) - -**Tier 2 β€” Investigation, then fix:** -7. AutoPos cadence β€” cdb idle trace, then converge (1.6) -8. Audit "dead outbound builders" (Phase B.4 wiring drift) β€” separate from holtburger but surfaced by this study - -**Tier 3 β€” Bigger investment:** -9. Retransmit subsystem (1.7) β€” port `reliability.rs` wholesale, including ISAAC search-mode (1-2 days) -10. Fragment assembler TTL (1.8 inbound) - -The Tier 1 group is a cohesive "post-A.5 network polish" pass β€” cheap, high-confidence, and several of them are likely candidates for the longstanding observer-not-perfect issue. - ---- - -## 6. File map for cross-reference - -| acdream | holtburger | Role | -|---------|-----------|------| -| `src/AcDream.Core.Net/WorldSession.cs:411-521` | `crates/holtburger-session/src/session/{api,auth}.rs` | Handshake driver | -| `src/AcDream.Core.Net/WorldSession.cs:556-924` | `crates/holtburger-core/src/client/runtime.rs:91-200` + `messages.rs` | Recv loop + dispatch | -| `src/AcDream.Core.Net/WorldSession.cs:1096-1156` | `crates/holtburger-session/src/session/send.rs` | Outbound transport (encode + ack piggyback) | -| `src/AcDream.Core.Net/Cryptography/IsaacRandom.cs` | `crates/holtburger-protocol/src/crypto.rs` | ISAAC (we likely lack `search`-mode) | -| `src/AcDream.Core.Net/Packets/PacketCodec.cs` | `session/{send,receive}.rs` + `optional_header.rs` | Encode/decode + optional header iteration | -| `src/AcDream.Core.Net/Packets/FragmentAssembler.cs` | `session/send.rs::process_fragment` | Inbound reassembly | -| `src/AcDream.Core.Net/Messages/MoveToState.cs` | `crates/holtburger-protocol/src/messages/movement/actions.rs:53-69` + `client/movement/common.rs:122-186` | MoveToState builder | -| `src/AcDream.Core.Net/Messages/AutonomousPosition.cs` | `messages/movement/actions.rs:175-189` + `system.rs:858-893` | AutoPos builder + cadence | -| **(missing)** | `crates/holtburger-session/src/session/reliability.rs` | **Retransmit machinery β€” entirely absent in acdream** | - ---- - -## Method note - -This study used four parallel general-purpose agents on the day-of pull (2026-05-10, holtburger HEAD `629695a`). All citations are file paths + line numbers in that exact tree. If holtburger moves forward, line numbers will drift; commit hashes (especially `99974cc`, `403bc98`, `336cbad`, `797aece`) are stable anchors. diff --git a/docs/research/2026-05-10-phase-a5-handoff.md b/docs/research/2026-05-10-phase-a5-handoff.md deleted file mode 100644 index ae70602..0000000 --- a/docs/research/2026-05-10-phase-a5-handoff.md +++ /dev/null @@ -1,376 +0,0 @@ -# Phase A.5 β€” Two-tier Streaming + Horizon LOD β€” Cold-Start Handoff - -**Created:** 2026-05-10, immediately after N.5b ship. -**Audience:** the next agent picking up streaming + horizon-LOD work. -**Purpose:** brief you on where N.5b left things, what A.5 actually has to do -to make the world look and feel great, and the load-bearing facts the -brainstorm should be informed by. - ---- - -## TL;DR - -N.5b just shipped: outdoor terrain rendering is on bindless + multi-draw -indirect via `TerrainModernRenderer`. Constant-cost dispatch as the -visible landblock count grows β€” radius=5 vs radius=15 are the same number -of GL calls for terrain. - -**A.5's actual goal β€” verbatim from the user, 2026-05-09:** - -> "I just want great smooth HIGH fps visuals. Should look great. As long -> as it scales and we get very high FPS" - -That reframes priorities. We are NOT optimizing the inner loop at radius=5 -(it's solved). We're scaling visual reach + scene density without the -client falling off a perf cliff. - -**Concretely, A.5 ships three things:** - -1. **Two-tier streaming.** Near tier (≀ N₁ landblocks) loads everything as - today (terrain + scenery + EnvCells + collision). Far tier (N₁ < r ≀ Nβ‚‚) - loads terrain mesh ONLY. No scenery generation, no collision, no - entity registration for the far tier. -2. **Per-LB entity bucketing for the WB dispatcher.** Today the entity - dispatcher walks every loaded entity each frame for AABB cull β€” - ~16K entities @ ~1Β΅s/test = 4.3ms/frame, dominating the frame budget. - Bucket entities by landblock so the cull is hierarchical: cull the LB - first, then only walk entities inside surviving LBs. -3. **Off-thread mesh build.** `LandblockMesh.Build` currently runs on the - render thread when a new LB streams in. At today's radius=5 this is - invisible; at A.5's higher Nβ‚‚ it becomes a visible frame-time spike - when 4-5 LBs stream simultaneously. Move the build to a worker pool; - hand finished `LandblockMeshData` back via a queue. - -The headline win you're shooting for: **radius=15 sustains the user's -target FPS in Holtburg with no streaming hitches.** - ---- - -## Where N.5b left things - -### Branch state (relative to main) - -After N.5b ships: -- N.5b SHIP at `08b7362` (final commit; appended SHIP record to plan) -- Roadmap entry, issue #51 closure, perf baseline doc all in place at `083c10c` -- Legacy `TerrainChunkRenderer` + `TerrainRenderer` + `terrain.vert/.frag` - deleted at `7dfa2af`. **The modern path is the only path.** - -### Captured perf baseline (load-bearing for A.5's "what's actually hot") - -From `docs/plans/2026-05-09-phase-n5b-perf-baseline.md`, measured -2026-05-09 at Holtburg town dueling field, radius=5, ~30s standstill: - -| Subsystem | cpu_us median per frame | Notes | -|---|---|---| -| **Entity dispatcher** (`WbDrawDispatcher`) | **~4,300** | 86% of frame budget. ~16K entities walked for AABB cull. THIS is the bottleneck. | -| Terrain dispatcher (`TerrainModernRenderer`) | ~6.4 | <1% of frame. Constant-cost regardless of radius (proved in N.5b). | -| Everything else (sky, particles, ImGui, swap, audio) | ~700 | Small. | - -**Actual FPS at radius=5 in Holtburg: ~200 fps** (frame time β‰ˆ 5ms). -NOT the "810 fps" inferred from the N.5 ship doc (that was 1/dispatcher_ms, -which is only the WB dispatcher CPU cost in isolation, not real frame time). - -### What naive radius increase does - -If you simply raised `ACDREAM_STREAM_RADIUS` to 15 today without A.5: - -- Loaded landblocks: 121 β†’ ~961 (8Γ— more). Acceptable. -- Loaded entities: ~16K β†’ ~125K (linear scaling with LB count). **NOT - acceptable.** At ~1Β΅s per AABB cull, the entity dispatcher would take - ~125ms/frame = 8 FPS. Slideshow. -- Memory footprint: similar 8Γ— explosion in scenery instance buffers. - -So the perf cliff is real and immediate. A.5 has to address it BEFORE -the radius can be safely raised. - -### What N.5b set up that A.5 inherits - -- **Modern terrain dispatcher.** `TerrainModernRenderer` is O(1) GL calls - in radius. As you add far-tier LBs (terrain only), the terrain - dispatcher cost stays flat (~6Β΅s/frame). This is the one subsystem - that doesn't need any A.5 work β€” it just scales. -- **Slot allocator for terrain GPU buffers.** Already grows by power-of-two - doubling. Will absorb radius=15 (~961 slots Γ— ~15 KB each = ~14 MB) - without manual tuning. -- **`[TERRAIN-DIAG]` instrumentation.** Reports per-frame median + p95 in - microseconds. Use this to confirm A.5 doesn't regress terrain perf. -- **Conformance sentinel.** `TerrainModernConformanceTests` proves visual - mesh Z agrees with `TerrainSurface.SampleZFromHeightmap` to 0.015 mm. - Don't break this β€” physics ↔ visual agreement must hold across both - tiers. -- **Bindless atlas.** `TerrainAtlas.GetBindlessHandles()`. The far tier - shares the atlas (it's region-wide). Zero atlas-related per-LB cost. - ---- - -## The brainstorm questions (the hard calls A.5 has to make) - -These are the questions to resolve in the brainstorm step. Bring them to -the user with options + recommendation; don't prejudge. - -### 1. Tier radii: what are N₁ and Nβ‚‚? - -- **N₁** = near-tier radius (everything loads). Today's default `STREAM_RADIUS`. - Probably stays at 5 (or maybe 4; maybe 3). -- **Nβ‚‚** = far-tier radius (terrain mesh only). Could be 8, 12, 15, 20. - -Tradeoffs: bigger Nβ‚‚ = more world visible = looks better. But each far-tier -LB still costs ~16 KB GPU memory + a frustum cull AABB + a slot allocation. -At Nβ‚‚=15, that's ~961 LBs Γ— 16 KB = ~15 MB GPU mem (cheap) + ~961 cull -tests (cheap, ~1ms total at 1Β΅s each β€” and we'll do this per-LB cull -anyway as part of #2 below). - -Verify against retail: cdb attach + check how many landblocks retail keeps -loaded at a given vantage point. Probably around 10-12 per the AC2D -references and the holtburger client's behavior. - -### 2. Far tier: terrain only? Or also impostor scenery? - -Two options: -- **Terrain only** (cleanest). Beyond N₁, no trees, no rocks. Skyline is the - terrain mesh against the sky. -- **Impostor scenery** (more retail-like). Beyond N₁, generate flat - billboards or low-poly trees instead of full meshes. Adds substantial - complexity (billboard pipeline, mesh-LOD generation, per-camera-angle - rotation). - -Recommendation: start with terrain-only. Add impostors only if the -horizon looks wrong (too bare). Retail definitely has SOME distant -scenery but the cutoff is gradual; we can match it later if needed. - -### 3. Entity bucketing structure - -Today: `WbDrawDispatcher` keeps a flat dictionary of all entities and -walks all of them per frame. To bucket by LB, we need: - -- A `Dictionary>` keyed by landblock ID -- On `AddEntity(...)`, also stash it in the LB bucket (the spawn flow - already knows the LB context) -- On `RemoveEntity(...)`, remove from the LB bucket too -- Per frame: cull at LB granularity first; then cull entities only inside - surviving LBs - -LB-level AABBs are already computed (per the existing `_visibleSlots` -logic in `TerrainModernRenderer` β€” the same AABB applies to entities, -modulo a Z-range bump for trees/buildings). - -Open question: do entities outside a known LB exist? (Items dropped on the -ground? Ephemeral effects? Player projectiles?) If yes, they need a -fallback "unknown LB" bucket that's still walked every frame. Probably -small. - -### 4. Where does the off-thread mesh build land? - -Today `LandblockMesh.Build` runs synchronously inside `OnLandblockLoaded` -on the render thread. To move it off: - -- `StreamingLoader` worker thread (already async for dat reads) signals - "LB X is ready" -- A new worker pool consumes that signal, builds the mesh on a worker - thread, posts the finished `LandblockMeshData` to a `ConcurrentQueue` -- Render thread drains the queue at the start of each frame, calling - `_terrain.AddLandblock(...)` for each ready mesh - -Gotcha: the `TerrainBlendingContext` is shared. Need to confirm it's -read-only (it is β€” built once at startup). Also `_surfaceCache` β€” -currently a plain `Dictionary` populated lazily by `TerrainBlending.BuildSurface`. -Either lock it, replace with `ConcurrentDictionary`, or pre-populate with -all known palCodes at startup. - -### 5. Streaming hysteresis at the tier boundary - -When the player crosses N₁ β†’ near-tier shrinks, far-tier grows. -LBs that were near-tier need to: -- Drop their scenery (unregister entities) -- Drop their EnvCells -- Keep the terrain mesh (still in far tier) - -When the player crosses back: the LB needs scenery + EnvCells re-loaded. -Hysteresis (don't churn at the exact boundary) is needed. - -The streaming loader already has hysteresis for full LB load/unload. A.5 -extends that: a separate hysteresis radius for the scenery/entity layer. - -### 6. Visual quality wins to ride along - -A.5 is the natural place to land 2-3 nearly-free quality wins: - -- **Mipmapped terrain atlas + anisotropic 16x.** Today the atlas is - `GL_LINEAR` no mipmaps; distant terrain shimmers. ~half-day fix. - Big visible improvement at far tier. -- **Tree alpha-test β†’ alpha-to-coverage with MSAA.** Today tree edges are - binary cutoff and pixel-edged. A2C with MSAA fixes them. ~one day. -- **Correct depth-write for transparent foliage.** Some scenery passes - may be writing depth incorrectly; confirm + fix. - -These are not strictly required for A.5 to ship, but they amplify the -"looks great" payoff. - -### 7. Acceptance metrics - -The user's goal is "smooth + high FPS + great-looking + scales." Pin -this concretely: - -- Target FPS at radius (whatever final N₁ + Nβ‚‚): β‰₯ user's monitor refresh - (probably 144 or 240 Hz). Capture before/after numbers in a perf - baseline doc parallel to N.5b's. -- No frame-time spikes > 5ms during streaming (record a 60-second - trace running through Holtburg β†’ North Yanshi). -- Visual horizon visible at the new Nβ‚‚. Capture screenshots from the - same vantage point at the start of A.5 (before) and at ship (after) - for the SHIP record. - -### 8. What's NOT in A.5 - -A.5 does not need to ship: -- GPU-side culling (compute-shader cull). Bigger lift; N.6 territory. -- Persistent-mapped indirect buffer. N.6 territory. -- Sky / particles / EnvCells migration. Separate N.7+ phases. -- Shadow mapping. Separate visual phase. - -Don't let scope creep pull these in. - ---- - -## Files to read before brainstorming - -In rough order of relevance: - -1. **`docs/research/2026-05-09-phase-n5b-handoff.md`** β€” N.5b's handoff - (read for context on what was just shipped + the structure of these - handoff docs). -2. **`docs/plans/2026-05-09-phase-n5b-perf-baseline.md`** β€” captured - perf numbers + the architectural reasoning for what A.5 inherits. -3. **`memory/project_phase_n5b_state.md`** β€” three high-value gotchas - captured during N.5b (especially #1: bindless uniform-sampler driver - quirk; A.5 won't directly need this, but it's the prior art for any - new shader code in the phase). -4. **`docs/plans/2026-04-11-roadmap.md`** A.5 entry β€” the original A.5 - description. -5. **The streaming loader** β€” `src/AcDream.Core/World/StreamingLoader.cs` - (or wherever it lives; grep for `OnLandblockLoaded`). Understand the - existing ring + hysteresis logic before extending it. -6. **WB dispatcher entity flow** β€” - `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` lines covering - `Draw` (the per-entity walk) and `EntitySpawnAdapter` (where entities - get registered). The bucketing change lands here. -7. **`LandblockMesh.Build`** β€” `src/AcDream.Core/Terrain/LandblockMesh.cs`. - Its inputs (heightmap, ctx, surfaceCache) determine what the worker - thread needs. ~150 lines. -8. **WB's `SceneryRenderManager`** β€” - `references/WorldBuilder/Chorizite.OpenGLSDLBackend/Lib/SceneryRenderManager.cs`. - Has a render-distance cap; informs N₁ vs Nβ‚‚ defaults. -9. **`TerrainModernRenderer`** β€” - `src/AcDream.App/Rendering/TerrainModernRenderer.cs`. Don't modify; - confirm the slot allocator handles radius=15 cleanly. - ---- - -## Acceptance criteria for the whole phase - -1. Build green; existing tests stay green; N.5b's conformance sentinel - still passes (visual mesh Z = TerrainSurface Z within 1mm). -2. **Far-tier LBs render terrain visibly past N₁** in user-driven visual - verification. -3. **Per-frame entity-dispatcher cpu_us at radius=N₁ drops** vs today - (the bucketing should help even at the current radius). -4. **Per-frame entity-dispatcher cpu_us at radius (N₁+Nβ‚‚) is bounded** - β€” does NOT scale linearly with total loaded LBs. Specifically: - bucketed cull should be < 1.5Γ— today's cost despite far-tier LBs - loading. -5. **No streaming hitch > 5ms** when running at run-speed across N₁/Nβ‚‚ - tier boundaries simultaneously (capture a 60s trace). -6. **`[TERRAIN-DIAG]` cpu_us stays flat** as Nβ‚‚ grows β€” the terrain - dispatcher proven O(1) (regression check). -7. Visual identity at near-tier (no scenery missing inside N₁; no - z-fighting; no cell-boundary wobble β€” N.5b sentinel still applies). -8. SHIP record + perf baseline + memory entry written, mirroring N.5b's - pattern. - ---- - -## What you'll be doing in the first 30 minutes - -1. Read this handoff in full. -2. Read `docs/research/2026-05-09-phase-n5b-handoff.md` for the structural - pattern. -3. Read `docs/plans/2026-05-09-phase-n5b-perf-baseline.md` for the captured - numbers A.5 inherits. -4. Read `memory/project_phase_n5b_state.md` for gotchas. -5. Verify build is green: `dotnet build`. -6. Verify N.5b ship is intact: `dotnet test --filter "FullyQualifiedName~TerrainSlot|FullyQualifiedName~TerrainModernConformance|FullyQualifiedName~Wb|FullyQualifiedName~MatrixComposition|FullyQualifiedName~TextureCacheBindless"` (target β‰₯114 passing, 0 failures). -7. Capture a baseline radius=5 frame trace yourself (one launch, 30s - standstill at Holtburg dueling field) so you have a "before" number - in your own measurement environment, not just trusting N.5b's number. -8. Invoke `superpowers:brainstorming` with the user. Walk through the - 8 brainstorm questions above. Present each with options + my - recommendation; don't prejudge. -9. After agreement, write the spec; then the plan; then execute - task-by-task using `superpowers:subagent-driven-development`. - -Don't skip the brainstorm. The N₁/Nβ‚‚ values, the bucketing structure -trade-offs, and the worker-thread design are real decisions with -downstream consequences that need user input β€” not "the agent makes a -call and goes." - ---- - -## Things to NOT do - -- **Don't raise `ACDREAM_STREAM_RADIUS` without A.5's tiered loading - in place.** The entity-cull cliff is immediate and severe (8 FPS at - naive radius=15). -- **Don't put scenery in the far tier just to "look more retail" without - a billboard/impostor pipeline.** Full-detail scenery in the far tier - is what causes the cull cliff. -- **Don't move `LandblockMesh.Build` to a worker thread without first - auditing `TerrainBlendingContext` + `_surfaceCache` for thread - safety.** Concurrent writes to the surfaceCache will produce - silently-wrong terrain blending. -- **Don't break the N.5b conformance sentinel.** If A.5 changes how - meshes are built (e.g., for the worker thread), the conformance - test must still pass β€” it's the load-bearing physics ↔ visual Z - agreement guard. -- **Don't bundle GPU-side culling, persistent-mapped buffers, or shadow - mapping into A.5.** Those are N.6+ territory; A.5 is "make the world - look big and not stutter." -- **Don't ship without honest perf numbers.** If A.5 doesn't actually - hit its FPS target, document why and ship N.6 next instead of - papering over it. The N.5b precedent is honest reporting. -- **Don't skip the visual verification gate.** Same lesson from N.5b's - black-terrain regression: "go" doesn't mean "verified." User must - actually launch the client at radius=Nβ‚‚ and confirm the horizon - looks great + FPS hits target. - ---- - -## Reference: where the FPS budget actually goes today - -For brainstorming purposes, the per-frame breakdown at radius=5 / Holtburg -(real measurement, 2026-05-09): - -``` -~5,000 Β΅s total frame time (= 200 fps) -β”œβ”€β”€ 4,300 Β΅s WbDrawDispatcher entity cull + dispatch ← THE BOTTLENECK -β”‚ ~16K entity AABB tests / frame -β”‚ A.5's entity bucketing attacks this directly -β”œβ”€β”€ 6 Β΅s TerrainModernRenderer -β”‚ O(1) in radius. Won't grow with A.5. Already solved. -β”œβ”€β”€ ~700 Β΅s Sky, particles, ImGui, audio, swap-buffers, misc -β”‚ Mostly fixed cost; some VSync-related -└── rest GPU side (we don't measure this β€” query plumbing - deferred to N.6). Could be substantial. -``` - -The first action of A.5 is to recognize that the perf claim "810 fps" -from N.5 was misleading. Don't repeat the mistake β€” measure the actual -frame time, not just one subsystem. - ---- - -Good luck. The phase is meaty (~2 weeks) but the structural work is -well-shaped: tiered streaming has clear boundaries, entity bucketing is -an isolated dispatcher change, off-thread mesh build is a well-understood -worker pattern. The hard call is the N₁/Nβ‚‚ values, and that's a -brainstorm question β€” bring it to the user with data. diff --git a/docs/research/2026-05-10-phase-m-opcode-matrix.md b/docs/research/2026-05-10-phase-m-opcode-matrix.md deleted file mode 100644 index 2e28b5c..0000000 --- a/docs/research/2026-05-10-phase-m-opcode-matrix.md +++ /dev/null @@ -1,543 +0,0 @@ -# Phase M β€” Network Opcode Coverage Matrix - -**Date:** 2026-05-10 -**Status:** Initial population complete (4 parallel research agents). Spot-check pass + intentional-divergence ratification owed before M.1 closes. -**Companion spec:** [`docs/superpowers/specs/2026-05-10-phase-m-network-stack-design.md`](../superpowers/specs/2026-05-10-phase-m-network-stack-design.md) -**Companion study:** [`docs/research/2026-05-10-holtburger-network-stack-study.md`](2026-05-10-holtburger-network-stack-study.md) - -This matrix is the **source of truth for Phase M completeness**. Every row defines: what the opcode is, who currently sends/receives it across our three reference sources, what acdream does today, and what Phase M must do. The spec's M.6 work plan reduces to "for every row where `acdream today` β‰  `Phase M target`, implement the delta and add tests." - ---- - -## Roll-up - -| Section | In-scope | Acdream today | Phase M delta | -|---------|----------|---------------|---------------| -| 1 β€” Transport flags | 22 | 14 parse / 5 build | 8 | -| 2 β€” Optional-header fields | 12 | 10 partial | builder + decoder gaps | -| 3 β€” GameMessage opcodes (top-level) | 51 | 21 implemented | 30 | -| 4 β€” GameEvent sub-opcodes (inside 0xF7B0) | 103 | 27 parsed / 26 wired | 76 new (~50 deferable to gameplay phases) | -| 5 β€” GameAction sub-opcodes (inside 0xF7B1) | 96 | 24 built / 8 live callers | 72 new + 16 dead-builder wirings | -| **Total** | **~284** | **~96** | **~190** | - -Roughly **34% complete by raw opcode count.** The biggest single Phase-M unblocking step is wiring the 16 dead builders in section 5 (Phase B.4 surface β€” Use / UseWithTarget / Allegiance / Inventory / Social / Cast / Appraise / etc.). - ---- - -## Cell-value vocabulary - -| Code | Meaning | -|------|---------| -| `P` | Parses inbound | -| `B` | Builds outbound | -| `PB` | Parses + builds (both directions) | -| `W` | Wired β€” typed handler exists AND state is updated by it | -| `H` | (ACE only) Server has a handler that processes this client-sent opcode | -| `–` | Not implemented | -| `N/A` | Not applicable for this side (e.g., server-only message in ACE column) | -| `?` | Could not determine β€” needs verification | - -**Phase M target column:** - -| Target | Meaning | -|--------|---------| -| `PB+W` | Must parse, build (if outbound), wire to typed event by phase end | -| `PB` | Must parse + build, no wiring required | -| `P+W` | Inbound only, must parse + dispatch typed event | -| `B+W` | Outbound only, must build + have a live caller | -| `B` | Build only, no live caller required (typed for future use) | -| `–defer:` | Explicitly deferred to a named gameplay phase | -| `–skip:` | Out of scope, with justification | - ---- - -## Section 1 β€” Transport flags - -In-scope: 22. Implemented in acdream: 14 (parse path + 5 build path). Phase M target delta: 8 (4 inbound parse gaps to wire, 4 outbound builders, plus 6 to retire/skip-justify). - -| Code | Direction | Name | Named-retail symbol | Holtburger | ACE | acdream today | Phase M target | Notes | -|---|---|---|---|---|---|---|---|---| -| `0x00000000` | N/A | None | – | N/A | N/A | N/A | N/A | Identity flag value [^t-a] | -| `0x00000001` | both | Retransmission | – | P (set on retx) | PB+W | – | PB+W | We never echo/honor this bit [^t-b] | -| `0x00000002` | both | EncryptedChecksum | `FlowQueue::EncryptChecksum` | PB | PB+W | PB+W | PB+W | Codec covers in/out + ISAAC | -| `0x00000004` | both | BlobFragments | `MessageFragment` group | PB+W | PB+W | PB+W | PB+W | Fragment list parsed/built | -| `0x00000100` | inbound | ServerSwitch | `ClientNet::HandleServerSwitch` | P (size-skip) | PB | P (size-skip) | P+W | Handler missing; just consumes 8 bytes | -| `0x00000200` | inbound | LogonServerAddr | – | – | – | – | –defer:M2 | Login-server bounce; no client logic yet [^t-c] | -| `0x00000400` | inbound | EmptyHeader1 | `CEmptyHeader<0x400,2>` | – | – | – | –skip:dead-flag | Retail struct exists, never sent | -| `0x00000800` | inbound | Referral | `ClientNet::HandleReferral` | – | B only (server) | – | –defer:M2 | Server-only path until login bounce | -| `0x00001000` | both | RequestRetransmit | `FlowQueue::TransmitNaks` | PB+W | PB+W | P (size-skip) | PB+W | NAK list parsed but ignored [^t-d] | -| `0x00002000` | both | RejectRetransmit | `FlowQueue::EnqueueEmptyAck` | P+W | PB | P (size-skip) | P+W | Inbound only (server tells us "no") | -| `0x00004000` | both | AckSequence | `FlowQueue::EnqueueAcks` | PB+W | PB+W | PB+W | PB+W | Per-packet ack pump shipped | -| `0x00008000` | both | Disconnect | `Client::Disconnect` | – | P+W | B only | P+W | Inbound parse-and-tear-down missing [^t-e] | -| `0x00010000` | outbound | LoginRequest | `ClientNet::SendLoginRequest` | B | P+W | B | B | Auth-only, parsed by server [^t-f] | -| `0x00020000` | inbound | WorldLoginRequest | `CEmptyHeader<0x20000,1>` | – | P (8 bytes) | P (size-skip) | P (size-skip) | Server-only on relay [^t-g] | -| `0x00040000` | inbound | ConnectRequest | `ClientNet::HandleConnectionRequest` | P+W | B | P+W | P+W | Handshake oracle, ISAAC seeded | -| `0x00080000` | outbound | ConnectResponse | `ClientNet::SendConnectAck` | B | P+W | B | B | 8-byte cookie echo | -| `0x00100000` | inbound | NetError | `NetError::UnPack` | – | – | – | P+W | Drop session + surface error to UI | -| `0x00200000` | inbound | NetErrorDisconnect | `NetError::UnPack` | – | P+W | – | P+W | Same parse, hard-disconnect variant | -| `0x00400000` | inbound | CICMDCommand | – | P (size-skip) | P+W | P (size-skip) | –defer:M3 | Server-debug only; not honored by retail clients | -| `0x01000000` | inbound | TimeSync | `ClientNet::HandleTimeSynch` | P+W | P+W | P+W | P+W | Drives `WorldTimeService` | -| `0x02000000` | inbound | EchoRequest | `CEchoRequestHeader::CreateFromData` | P+W (mirrors out) | P+W | P (no reply) | PB+W | Must build EchoResponse mirror [^t-h] | -| `0x04000000` | outbound | EchoResponse | – | B | B | – | B | Reply path for incoming EchoRequest | -| `0x08000000` | both | Flow | `FlowQueue::TransmitNewPackets` | P (size-skip) | P+W | P (size-skip) | –defer:M2 | Throttle hint; safe to ignore until M2 | - -**Footnotes:** - -[^t-a]: The `None=0` value isn't a wire bit, but it's in our enum so callers can default-initialize headers β€” keep it. -[^t-b]: ACE sets `Retransmission` when re-sending a cached packet; clients should accept it as informational. We currently treat the bit as a no-op (works because we don't dedupe on it). -[^t-c]: A login-server-side handshake step; only relevant when ACE adds login-bounce, which it doesn't today. -[^t-d]: We need to actually retransmit on inbound NAK and need to send NAKs for our own missing inbound. M3 reliability-core phase. -[^t-e]: Inbound `Disconnect` must close the session cleanly and notify upper layers; right now the connection just times out on client side too. -[^t-f]: `LoginRequest` is a server-decode case but our codec consumes it on encode for hashing. -[^t-g]: Retail server uses this for world-server entry confirmation; the holtburger ref has no parse, ACE writer-side is `Pack`. Our consumer just skips 8 bytes for hashing. -[^t-h]: Servers do periodically EchoRequest to the client; we must mirror the 4-byte client-time as an `EchoResponse` per `FlowQueue::DequeueAck` semantics. - ---- - -## Section 2 β€” Optional-header fields - -In-scope: 12. Implemented in acdream: 12 of 12 sized-skip; 6 of 12 surface decoded fields. Phase M target delta: needs (a) builders for the ones we only parse, (b) ConnectRequest + EchoRequest builder paths for symmetric tests, (c) golden-vector test file. - -| Code | Direction | Name | Named-retail symbol | Holtburger | ACE | acdream today | Phase M target | Notes | -|---|---|---|---|---|---|---|---|---| -| `0x100` | inbound | ServerSwitch (8 bytes) | `UCServerSwitchStruct` | P (skip) | PB | P (skip) | P (decode)+W | Decode `serverIp:u32, port:u16, pad:u16` [^o-a] | -| `0x1000` | inbound | RequestRetransmit (4+N\*4) | `FlowQueue::TransmitNaks` | PB | PB | P (parsed list) | PB+W | List stored; build path missing | -| `0x2000` | inbound | RejectRetransmit (4+N\*4) | `FlowQueue::CompileEmptyAcks` | P | PB | P (size-skip) | P (decode)+W | List currently consumed without storage | -| `0x4000` | both | AckSequence (4 bytes) | `FlowQueue::EnqueueAcks` | PB | PB | PB | PB | Stored as `AckSequence:u32` | -| `0x10000` | outbound | LoginRequest (rest of pkt) | `ClientNet::SendLoginRequest` | B | P (full) | B (via `LoginRequest.Build`) | B | Variable-length tail; raw bytes hashed | -| `0x20000` | inbound | WorldLoginRequest (8 bytes) | `CEmptyHeader<0x20000,1>` | – | P (8B peek) | P (size-skip) | P (decode)+W | Decode purpose unknown, store raw | -| `0x40000` | inbound | ConnectRequest (32 bytes) | `CConnectHeader` | P+W | B (server) | P+W | PB | We need encode path for round-trip tests | -| `0x80000` | outbound | ConnectResponse (8 bytes) | – | B | P (8B peek) | B | PB | Decode on inbound test fixtures | -| `0x400000` | inbound | CICMDCommand (8 bytes) | – | P (skip) | P (8B) | P (size-skip) | –defer:M3 | Decode + handler deferred | -| `0x1000000` | inbound | TimeSync (8 bytes) | `CTimeSyncHeader` | P+W | P+W | P+W | PB | Add build for symmetry; double LE | -| `0x2000000` | inbound | EchoRequest (4 bytes) | `CEchoRequestHeader` | P+W | P+W | P (no reply) | PB+W | Wire to `SendEchoResponse` builder | -| `0x8000000` | both | Flow (6 bytes) | `UCFlowStruct` | P (skip) | P+W | P (decode) | –defer:M2 | `FlowBytes:u32, FlowInterval:u16` decoded | - -**Footnotes:** - -[^o-a]: ServerSwitch struct layout per retail `UCServerSwitchStruct` β€” confirmed via named-retail symbol `?CreateFromData@?$COnePrimHeader@$0BAA@$0GA@UCServerSwitchStruct@@@@`. M3 needs the IP/port to actually re-target the socket; today we'd silently drop traffic from a relocated server. - -**Cross-cutting Phase M deliverables for sections 1+2:** - -1. **Goldens fixture file** β€” `tests/AcDream.Core.Net.Tests/Packets/PacketHeaderOptionalTests.cs` does not exist; only indirect coverage via `PacketCodecTests` and `ConnectRequestTests`. M needs one fixture per non-skip flag covering parse + build symmetry. -2. **Typed events** β€” currently the only `WorldSession`-side flag-driven event is `ServerTimeUpdated` (from `TimeSync`). Phase M target adds: `ServerSwitchRequested(ip, port)`, `ServerDisconnect(reason)`, `ServerNetError(NetErrorCode, message)`, `EchoRequested(clientTime)` (internal), `RetransmitRequested(seqs)`, `RetransmitRejected(seqs)`. -3. **`PacketHeaderOptional` storage gaps** β€” `RejectRetransmit` list is consumed but discarded; `WorldLoginRequest` 8-byte body is skipped; `CICMDCommand` 8-byte body is skipped; `ConnectResponse` 8-byte cookie is decoded only inside `Connect()`'s send path, not on inbound parse. M target: lift each into a typed property on `PacketHeaderOptional`. -4. **Builder-side parity** β€” `PacketHeaderOptional.Parse` exists; there is no `PacketHeaderOptional.Build` β€” every outbound flag's body bytes are hand-rolled at the call site (`SendAck`, `Connect`, `Dispose`). Phase M should add a single `Build(PacketHeaderFlags, body fields)` to mirror parse. - ---- - -## Section 3 β€” GameMessage opcodes (top-level) - -In-scope: 51. Implemented in acdream: 21. Phase M target delta: 30. - -| Code | Direction | Name | Named-retail symbol | Holtburger | ACE | acdream today | Phase M target | Notes | -|------|-----------|------|---------------------|------------|-----|---------------|----------------|-------| -| 0x0000 | both | None | – | PB | N/A | – | –skip:heartbeat-only | Internal/heartbeat sentinel | -| 0x0024 | inbound | InventoryRemoveObject | – | P | B | – | P+W | Out of bubble or destroyed | -| 0x0197 | inbound | SetStackSize | – | P | B | – | P+W | Container stack size delta | -| 0x019E | inbound | PlayerKilled | – | PB | B | P+W | P+W | victim+killer broadcast | -| 0x01E0 | inbound | EmoteText | `CM_Communication::DispatchUI_HearEmote` | PB | B | P+W | P+W | Server-driven 3rd-person emote | -| 0x01E2 | inbound | SoulEmote | `CM_Communication::DispatchUI_HearSoulEmote` | PB | B | P+W | P+W | Complex emote w/ animation | -| 0x02BB | inbound | HearSpeech | `ClientCommunicationSystem::Handle_Communication__HearSpeech` | PB | B | P+W | P+W | Local chat | -| 0x02BC | inbound | HearRangedSpeech | `ClientCommunicationSystem::Handle_Communication__HearRangedSpeech` | PB | B | P+W | P+W | Shouts; same parser as 0x02BB | -| 0x02CD | inbound | PrivateUpdatePropertyInt | `ClientObjMaintSystem::Handle_Qualities__PrivateUpdateInt` | PB | B | – | P+W | Owner-only int property | -| 0x02CE | inbound | PublicUpdatePropertyInt | – | PB | B | – | P+W | Broadcast int property | -| 0x02CF | inbound | PrivateUpdatePropertyInt64 | – | PB | B | – | P+W | Owner-only int64 | -| 0x02D0 | inbound | PublicUpdatePropertyInt64 | – | PB | B | – | P+W | Broadcast int64 | -| 0x02D1 | inbound | PrivateUpdatePropertyBool | `ClientObjMaintSystem::Handle_Qualities__PrivateUpdateBool` | PB | B | – | P+W | Owner-only bool | -| 0x02D2 | inbound | PublicUpdatePropertyBool | – | PB | B | – | P+W | Broadcast bool | -| 0x02D3 | inbound | PrivateUpdatePropertyFloat | `ClientObjMaintSystem::Handle_Qualities__PrivateUpdateFloat` | PB | B | – | P+W | Owner-only float | -| 0x02D4 | inbound | PublicUpdatePropertyFloat | – | PB | B | – | P+W | Broadcast float | -| 0x02D5 | inbound | PrivateUpdatePropertyString | – | PB | B | – | P+W | Owner-only string | -| 0x02D6 | inbound | PublicUpdatePropertyString | – | PB | B | – | P+W | Broadcast string | -| 0x02D7 | inbound | PrivateUpdatePropertyDataID | – | PB | B | – | P+W | Owner-only DataID | -| 0x02D8 | inbound | PublicUpdatePropertyDataID | – | PB | B | – | P+W | Broadcast DataID | -| 0x02D9 | inbound | PrivateUpdatePropertyInstanceID | `CM_Qualities::DispatchUI_PrivateUpdateInstanceID` | PB | B | – | P+W | Owner-only InstanceID | -| 0x02DA | inbound | PublicUpdateInstanceID | – | PB | B | – | P+W | Broadcast InstanceID | -| 0x02DB | inbound | PrivateUpdatePosition | `CM_Qualities::DispatchUI_PrivateUpdatePosition` | PB | B | – | –defer:F.x | Owner-only position; redundant with 0xF748 | -| 0x02DC | inbound | PublicUpdatePosition | – | PB | B | – | –defer:F.x | Public position; redundant with 0xF748 | -| 0x02DD | inbound | PrivateUpdateSkill | – | PB | B | – | P+W | Owner-only skill XP | -| 0x02DE | inbound | PublicUpdateSkill | – | PB | B | – | P+W | Public skill | -| 0x02DF | inbound | PrivateUpdateSkillLevel | – | PB | B | – | P+W | Owner-only skill base level | -| 0x02E0 | inbound | PublicUpdateSkillLevel | – | PB | B | – | P+W | Public skill base level | -| 0x02E3 | inbound | PrivateUpdateAttribute | `ClientObjMaintSystem::Handle_Qualities__PrivateUpdateAttribute` | PB | B | – | P+W | Strength/Stamina/etc base | -| 0x02E4 | inbound | PublicUpdateAttribute | – | PB | B | – | P+W | Public attribute | -| 0x02E7 | inbound | PrivateUpdateVital | – | PB | B | P+W | P+W | Max HP/Stam/Mana β€” vitals panel | -| 0x02E8 | inbound | PublicUpdateVital | – | PB | B | – | P+W | Public vital | -| 0x02E9 | inbound | PrivateUpdateAttribute2ndLevel | `ClientObjMaintSystem::Handle_Qualities__PrivateUpdateAttribute2ndLevel` | PB [^m-1] | B | P+W | P+W | Current-only vital delta | -| 0xEA60 | inbound | AdminEnvirons | `CPlayerSystem::Handle_Admin__Environs` | – | B | P+W | P+W | Fog presets / sound cues | -| 0xF625 | inbound | ObjDescEvent | `SmartBox::HandleObjDescEvent` | PB | B | P+W | P+W | Per-entity appearance update | -| 0xF643 | inbound | CharacterCreateResponse | – | PB | B | – | –defer:char-creation | Char-creation flow not yet built | -| 0xF653 | outbound | CharacterLogOff | – | PB | P | B | PB+W | Sent on Dispose; ACE accepts | -| 0xF655 | both | CharacterDelete | – | PB | P | – | –defer:char-mgmt | Char-management UI deferred | -| 0xF656 | outbound | CharacterCreate | – | PB | P | – | –defer:char-creation | Char-creation flow not yet built | -| 0xF657 | outbound | CharacterEnterWorld | `CM_Login::SendNotice_BeginEnterWorld` [^m-2] | PB | P | B | PB+W | Built; sent during handshake | -| 0xF658 | inbound | CharacterList | `CPlayerSystem::Handle_Login__CharacterSet` | PB | B | P+W | P+W | Login char picker | -| 0xF659 | inbound | CharacterError | `CPlayerSystem::Handle_CharacterError` | PB | B | – | P+W | Login/restore failures | -| 0xF6EA | both | ForceObjectDescSend | – | PB | P | – | –defer:F.x | Server requests client re-send ObjDesc; rare | -| 0xF745 | inbound | CreateObject (ObjectCreate) | `SmartBox::HandleCreateObject` | PB | B | P+W | P+W | Spawn entity in bubble | -| 0xF746 | inbound | PlayerCreate | `SmartBox::HandleCreatePlayer` | PB | B | P+W [^m-3] | P+W | Triggers LoginComplete | -| 0xF747 | inbound | DeleteObject (ObjectDelete) | `SmartBox::HandleDeleteObject` | PB | B | P+W | P+W | Despawn | -| 0xF748 | inbound | UpdatePosition | `CM_Qualities::DispatchUI_UpdatePosition` | PB | B | P+W | P+W | Periodic position sync | -| 0xF749 | inbound | ParentEvent | `SmartBox::HandleParentEvent` | PB | B | – | P+W | Equip/wield parent change | -| 0xF74A | inbound | PickupEvent | `SmartBox::HandlePickupEvent` | PB | B | – | P+W | Pickup confirmation | -| 0xF74B | inbound | SetState | `SmartBox::HandleSetState` | PB | B | – | P+W | Door open/close, container state | -| 0xF74C | inbound | UpdateMotion (Motion) | – | PB | B | P+W | P+W | Animation cycle change | -| 0xF74E | inbound | VectorUpdate | `SmartBox::HandleVectorUpdate` | PB | B | P+W | P+W | Remote jump velocity, missile arc | -| 0xF750 | inbound | Sound | `SmartBox::HandleSoundEvent` | PB | B | – | P+W | Positional sound trigger | -| 0xF751 | inbound | PlayerTeleport | `SmartBox::HandlePlayerTeleport` | PB | B | P+W | P+W | Portal/teleport screen | -| 0xF752 | inbound | AutonomyLevel | `CommandInterpreter::SetAutonomyLevel` | P [^m-4] | – | – | P+W | Server tells client physics-trust level | -| 0xF753 | both | AutonomousPosition | `CM_Movement::Event_AutonomousPosition` | PB | – | B | PB+W | Outbound built; inbound parser missing | -| 0xF754 | inbound | PlayScript (PlayScriptId) | `SmartBox::HandlePlayScriptID` | – | – | P+W [^m-5] | P+W | Inline parser; lightning, spell FX, emotes | -| 0xF755 | inbound | PlayEffect | – | PB | B | – | P+W | Particle/visual scripts; ACE uses for PlayScript wrapper | -| 0xF7B0 | inbound | GameEvent (envelope) | – | PB | B | P+W | P+W | Envelope for sub-opcodes (see Β§4) | -| 0xF7B1 | outbound | GameAction (envelope) | – | PB | P | B+W | PB+W | Envelope for sub-opcodes (see Β§5) | -| 0xF7C1 | inbound | AccountBanned | – | – | B | – | –defer:F.x | ACE-only, rarely seen | -| 0xF7C8 | outbound | CharacterEnterWorldRequest | – | PB | P | B | PB+W | Built; sent before 0xF657 | -| 0xF7CC | both | GetServerVersion | `Proto_UI::SendAdminGetServerVersion` | – | P | – | –defer:F.x | Admin-only | -| 0xF7CD | both | FriendsOld | – | – | P | – | –defer:F.x | Obsolete; ACE drops it | -| 0xF7D9 | outbound | CharacterRestore | – | PB | P | – | –defer:char-mgmt | Char-management UI deferred | -| 0xF7DB | inbound | UpdateObject | `SmartBox::HandleUpdateObject` | PB | B | – | P+W | Heavy re-send of object visual+physics | -| 0xF7DC | inbound | AccountBoot | `CPlayerSystem::Handle_AccountBooted` | PB | B | – | P+W | Kicked from server | -| 0xF7DE | both | TurbineChat | `CCommunicationSystem::IsUsingTurbineChat` | PB | PB [^m-6] | PB+W | PB+W | Global community chat | -| 0xF7DF | inbound | CharacterEnterWorldServerReady | – | P [^m-7] | B | P+W [^m-8] | P+W | Handshake gate during enter-world | -| 0xF7E0 | inbound | ServerMessage | – | PB | B | P+W | P+W | System message / announcements | -| 0xF7E1 | inbound | ServerName | `ECM_Login::SendNotice_WorldName` | PB | B | – | P+W | Shard name during login | -| 0xF7E2 | both | DDD_DataMessage | – | – | – | – | –defer:dat-streaming | DDD download channel (we ship dats locally) | -| 0xF7E3 | both | DDD_RequestDataMessage | – | – | P | – | –defer:dat-streaming | Client requests dat data | -| 0xF7E4 | both | DDD_ErrorMessage | – | – | – | – | –defer:dat-streaming | DDD error channel | -| 0xF7E5 | inbound | DDD_Interrogation | `DDD_InterrogationMessage::Serialize` | PB [^m-9] | B | P+W | P+W | Server asks "what dat versions?" | -| 0xF7E6 | outbound | DDD_InterrogationResponse | – | PB | P | B | PB+W | Built; sent in response to 0xF7E5 | -| 0xF7E7 | both | DDD_BeginDDD | – | – | – | – | –defer:dat-streaming | DDD start | -| 0xF7E8 | both | DDD_BeginPullDDD | – | – | – | – | –defer:dat-streaming | DDD pull start | -| 0xF7E9 | both | DDD_IterationData | – | – | – | – | –defer:dat-streaming | DDD chunk iteration | -| 0xF7EA | inbound | DDD_EndDDD | – | – | P | – | –defer:dat-streaming | DDD end signal | - -**Footnotes:** - -[^m-1]: ACE calls 0x02E9 `PrivateUpdateAttribute2ndLevel`; holtburger calls it `PrivateUpdateVitalCurrent` (current-only delta). -[^m-2]: Retail-side trigger of the enter-world flow; the wire opcode 0xF657 is constructed from the request. -[^m-3]: PlayerCreate fires LoginComplete when guid matches own char; CreateObject body is parsed for the player too. -[^m-4]: AutonomyLevel is in holtburger's `GameMessage` enum + unpack/pack, but its enum value (0xF752) is mapped via opcode dispatch. -[^m-5]: 0xF754 PlayScript is parsed inline in `WorldSession.cs:850` (no dedicated `Messages/PlayScript.cs`); routed to `PlayScriptReceived` event for VFX runtime. -[^m-6]: ACE handles inbound TurbineChat via `TurbineChatHandler` and emits outbound via `GameMessageTurbineChat`, hence both directions. -[^m-7]: CharacterEnterWorldServerReady is unit variant in holtburger (no payload); only an opcode marker. -[^m-8]: acdream uses 0xF7DF as a handshake gate (`WorldSession.cs:495`), no dedicated parser file. -[^m-9]: DddInterrogation in holtburger is a unit variant β€” opcode marker only, no payload to parse. - -**Caveats and unknowns:** -- `0xF7C1 AccountBanned` is in ACE's enum + has a `GameMessageAccountBanned.cs`, but holtburger has it commented out. Marked `–defer` since the channel exists in retail but rarely fires. -- `0xF7CC GetServerVersion`, `0xF7CD FriendsOld`: ACE has handlers for them (i.e. accepts them inbound from a client that sends them), but no acdream sends them today. Listed as `–defer`. -- `0xF619 PositionAndMovement`: holtburger documents this as a "ghost" opcode (defined but never emitted by ACE/retail). Excluded from the table β€” confirmed dead code per holtburger comment + grep on ACE shows no `Writer.Write` site. -- `0xF754 PlayScriptId` vs `0xF755 PlayEffect`: ACE has the `Script.cs` GameMessage tagged with `PlayEffect (0xF755)`, while retail's `SmartBox::HandlePlayScriptID` is the 0xF754 handler. acdream's inline parser at `WorldSession.cs:850` reads `[u32 opcode][u32 guid][u32 scriptId]` matching the 0xF754 layout. - ---- - -## Section 4 β€” GameEvent sub-opcodes (inside 0xF7B0 envelope) - -In-scope: 103. Implemented (parsed) in acdream today: 27. Wired (`W`) in acdream today: 26. Phase M target delta: 76 new parsers + ~50 deferred to later phases. - -All rows are `inbound` direction (GameEvents are serverβ†’client only). - -| Code | Direction | Name | Named-retail symbol | Holtburger | ACE | acdream today | Phase M target | Notes | -|---|---|---|---|---|---|---|---|---| -| 0x0003 | inbound | AllegianceUpdateAborted | `ClientAllegianceSystem::Handle_Allegiance__AllegianceUpdateAborted` | – | W | – | –defer:Allegiance | scope deferred β€” no allegiance UI yet | -| 0x0004 | inbound | PopupString | `ClientCommunicationSystem::Handle_Communication__PopUpString` | W | W | W | W | modal text β†’ ChatLog.OnPopup | -| 0x0013 | inbound | PlayerDescription | `CPlayerSystem::Handle_PlayerDescription` | W | W | W | W | full local-player snapshot at login [^e-a] | -| 0x0020 | inbound | AllegianceUpdate | `ClientAllegianceSystem::Handle_Allegiance__AllegianceUpdate` | – | W | – | –defer:Allegiance | needs CAllegianceProfile parser | -| 0x0021 | inbound | FriendsListUpdate | `CM_Social::SendNotice_UpdateFriendsList` | – | W | – | P+W | FriendDataList; small parser, high UX value | -| 0x0022 | inbound | InventoryPutObjInContainer | – (CM_Inventory) | W | W | W | W | (item, container, slot) β€” items.MoveItem | -| 0x0023 | inbound | WieldObject | – (CM_Inventory) | W | W | W | W | server-driven equip | -| 0x0029 | inbound | CharacterTitle | `CM_Social::SendNotice_AddCharacterTitle` | – | W | – | –defer:Social | gmCharacterTitleUI | -| 0x002B | inbound | UpdateTitle | `CM_Social::SendNotice_SetDisplayCharacterTitle` | – | W | – | –defer:Social | titles UI not yet built | -| 0x0052 | inbound | CloseGroundContainer | – (gmInventoryUI) | W | W | P | P+W | parser exists, needs ItemRepository wiring | -| 0x0062 | inbound | ApproachVendor | – (CM_Vendor) | W | W | – | –defer:VendorPanel | needs VendorProfile + ItemProfile list parser | -| 0x0075 | inbound | StartBarber | `ClientUISystem::Handle_Character__StartBarber` | – | W | – | –defer:Barber | gmBarberUI not yet built | -| 0x00A0 | inbound | InventoryServerSaveFailed | – (CM_Inventory) | W | W | P | P+W | parser exists; needs revert hook | -| 0x00A3 | inbound | FellowshipQuit | `ClientFellowshipSystem::Handle_Fellowship__Quit` | W | W | – | –defer:Fellowship | scope deferred β€” no fellowship state | -| 0x00A4 | inbound | FellowshipDismiss | `ClientFellowshipSystem::Handle_Fellowship__Dismiss` | W | W | – | –defer:Fellowship | scope deferred | -| 0x00B4 | inbound | BookDataResponse | `CM_Writing::Event_BookData` | W | W | – | –defer:Books | gmBookUI not yet built | -| 0x00B5 | inbound | BookModifyPageResponse | `CM_Writing::Event_BookModifyPage` | – | W | – | –defer:Books | | -| 0x00B6 | inbound | BookAddPageResponse | `CM_Writing::SendNotice_BookAddPageResponse` | – | W | – | –defer:Books | | -| 0x00B7 | inbound | BookDeletePageResponse | `CM_Writing::SendNotice_BookDeletePageResponse` | – | W | – | –defer:Books | | -| 0x00B8 | inbound | BookPageDataResponse | `CM_Writing::SendNotice_BookPageDataResponse` | W | W | – | –defer:Books | | -| 0x00C3 | inbound | GetInscriptionResponse | – | – | W | – | –defer:Books | inscription on caster items | -| 0x00C9 | inbound | IdentifyObjectResponse | `ClientUISystem::Handle_Item__AppraiseDone` [^e-b] | W | W | W | W | AppraiseInfoParser feeds ItemRepository | -| 0x0147 | inbound | ChannelBroadcast | `ClientCommunicationSystem::Handle_Communication__ChannelBroadcast` | W | W | W | W | (channelId, sender, msg) β†’ ChatLog | -| 0x0148 | inbound | ChannelList | `ClientCommunicationSystem::Handle_Communication__ChannelList` | – | W | – | P+W | PackableList; admin/list response | -| 0x0149 | inbound | ChannelIndex | `ClientCommunicationSystem::Handle_Communication__ChannelIndex` | – | W | – | P+W | PackableList | -| 0x0196 | inbound | ViewContents | `ClientUISystem::OnViewContents` | W | W | – | P+W | server view of remote container β€” needed for sidepacks | -| 0x019A | inbound | InventoryPutObjectIn3D | – (CM_Inventory) | W | W | P | P+W | parser exists; needs spawn-into-world wiring | -| 0x01A7 | inbound | AttackDone | – | W | W | W | W | combat seq complete | -| 0x01A8 | inbound | MagicRemoveSpell | `ClientMagicSystem::Handle_Magic__RemoveSpell` | W | W | W | W | spell removed from spellbook | -| 0x01AC | inbound | VictimNotification | `ClientCombatSystem::HandleVictimNotificationEvent` | W | W | W | W | death msg for victim | -| 0x01AD | inbound | KillerNotification | `ClientCombatSystem::HandleKillerNotificationEvent` | W | W | W | W | death msg for killer | -| 0x01B1 | inbound | AttackerNotification | `ClientCombatSystem::HandleAttackerNotificationEvent` | W | W | W | W | "you hit X" | -| 0x01B2 | inbound | DefenderNotification | `ClientCombatSystem::HandleDefenderNotificationEvent` | W | W | W | W | "X hit you" | -| 0x01B3 | inbound | EvasionAttackerNotification | `ClientCombatSystem::HandleEvasionAttackerNotificationEvent` | W | W | W | W | "X evaded" | -| 0x01B4 | inbound | EvasionDefenderNotification | `ClientCombatSystem::HandleEvasionDefenderNotificationEvent` | W | W | W | W | "you evaded X" | -| 0x01B8 | inbound | CombatCommenceAttack | – | W | W | W | W | empty payload | -| 0x01C0 | inbound | UpdateHealth | `CM_Combat::SendNotice_UpdateObjectHealth` | W | W | W | W | (guid, healthPct) β†’ CombatState | -| 0x01C3 | inbound | QueryAgeResponse | `ClientCommunicationSystem::Handle_Character__QueryAgeResponse` | – | W | – | P | small string parser; chat panel display | -| 0x01C7 | inbound | UseDone | `ClientUISystem::Handle_Item__UseDone` | W | W | P | P+W | parser exists; needs InteractionState wiring | -| 0x01C8 | inbound | AllegianceUpdateDone | – | – | W | – | –defer:Allegiance | | -| 0x01C9 | inbound | FellowshipFellowUpdateDone | `ClientFellowshipSystem::Handle_Fellowship__FellowUpdateDone` | W | W | – | –defer:Fellowship | empty payload | -| 0x01CA | inbound | FellowshipFellowStatsDone | `ClientFellowshipSystem::Handle_Fellowship__FellowStatsDone` | W | W | – | –defer:Fellowship | empty payload | -| 0x01CB | inbound | ItemAppraiseDone | `ClientUISystem::Handle_Item__AppraiseDone` | – | W | – | P | post-IdentifyObjectResponse signal | -| 0x01E2 | inbound | Emote | `ClientCommunicationSystem::Handle_Communication__HearEmote` [^e-c] | – | W | – | P | "*X waves*" β€” chat broadcast | -| 0x01EA | inbound | PingResponse | `ClientUISystem::Handle_Character__ReturnPing` | W | W | P | P+W | parser exists; needs latency/heartbeat wiring | -| 0x01F4 | inbound | SetSquelchDB | `ClientCommunicationSystem::Handle_Communication__SetSquelchDB` | – | W | – | –defer:SquelchUI | SquelchDB blob; ignore-list state | -| 0x01FD | inbound | RegisterTrade | `ClientTradeSystem::Handle_Trade__Recv_RegisterTrade` | W | W | – | –defer:TradePanel | (guid, accepterGuid, ackTimer) | -| 0x01FE | inbound | OpenTrade | `ClientTradeSystem::Handle_Trade__Recv_OpenTrade` | W | W | – | –defer:TradePanel | initiator guid | -| 0x01FF | inbound | CloseTrade | `ClientTradeSystem::Handle_Trade__Recv_CloseTrade` | W | W | – | –defer:TradePanel | closer guid | -| 0x0200 | inbound | AddToTrade | `ClientTradeSystem::Handle_Trade__Recv_AddToTrade` | W | W | P | –defer:TradePanel | parser exists; needs TradeState | -| 0x0201 | inbound | RemoveFromTrade | `ClientTradeSystem::Handle_Trade__Recv_RemoveFromTrade` | – | W | – | –defer:TradePanel | (initiatorGuid, itemGuid) | -| 0x0202 | inbound | AcceptTrade | `ClientTradeSystem::Handle_Trade__Recv_AcceptTrade` | W | W | P | –defer:TradePanel | parser exists | -| 0x0203 | inbound | DeclineTrade | `ClientTradeSystem::Handle_Trade__Recv_DeclineTrade` | W | W | – | –defer:TradePanel | initiator guid | -| 0x0205 | inbound | ResetTrade | `ClientTradeSystem::Handle_Trade__Recv_ResetTrade` | W | W | – | –defer:TradePanel | reset to-trade list | -| 0x0207 | inbound | TradeFailure | `ClientTradeSystem::Handle_Trade__Recv_TradeFailure` | W | W | P | –defer:TradePanel | parser exists | -| 0x0208 | inbound | ClearTradeAcceptance | `ClientTradeSystem::Handle_Trade__Recv_ClearTradeAcceptance` | W | W | – | –defer:TradePanel | empty payload | -| 0x021D | inbound | HouseProfile | `ClientHousingSystem::Handle_House__Recv_HouseProfile` | – | W | – | –defer:Housing | HouseProfile blob | -| 0x0225 | inbound | HouseData | `ClientHousingSystem::Handle_House__Recv_HouseData` | – | W | – | –defer:Housing | HouseData blob | -| 0x0226 | inbound | HouseStatus | `ClientHousingSystem::Handle_House__Recv_HouseStatus` | – | W | – | –defer:Housing | scalar status code | -| 0x0227 | inbound | UpdateRentTime | `ClientHousingSystem::Handle_House__Recv_UpdateRentTime` | – | W | – | –defer:Housing | i32 timestamp | -| 0x0228 | inbound | UpdateRentPayment | `ClientHousingSystem::Handle_House__Recv_UpdateRentPayment` | – | W | – | –defer:Housing | HousePaymentList | -| 0x0248 | inbound | HouseUpdateRestrictions | `ClientHousingSystem::Handle_House__Recv_UpdateRestrictions` | – | W | – | –defer:Housing | RestrictionDB blob | -| 0x0257 | inbound | UpdateHAR | `ClientHousingSystem::Handle_House__Recv_UpdateHAR` | – | W | – | –defer:Housing | HAR blob | -| 0x0259 | inbound | HouseTransaction | `ClientHousingSystem::Handle_House__Recv_HouseTransaction` | – | W | – | –defer:Housing | scalar txn code | -| 0x0264 | inbound | QueryItemManaResponse | `ClientUISystem::Handle_Item__QueryItemManaResponse` | W | W | P | P+W | parser exists; needs ItemRepository wiring | -| 0x0271 | inbound | AvailableHouses | `ClientHousingSystem::Handle_House__Recv_AvailableHouses` | – | W | – | –defer:Housing | PackableList + flag | -| 0x0274 | inbound | CharacterConfirmationRequest | `ClientUISystem::Handle_Character__ConfirmationRequest` | W | W | P | P+W | parser exists; needs modal-confirm wiring | -| 0x0276 | inbound | CharacterConfirmationDone | `ClientUISystem::Handle_Character__ConfirmationDone` | W | W | – | P+W | (type, contextId); confirms client ACK | -| 0x027A | inbound | AllegianceLoginNotification | `ClientAllegianceSystem::Handle_Allegiance__AllegianceLoginNotificationEvent` | – | W | – | –defer:Allegiance | (guid, login/logout flag) | -| 0x027C | inbound | AllegianceInfoResponse | `ClientAllegianceSystem::Handle_Allegiance__AllegianceInfoResponseEvent` | – | W | – | –defer:Allegiance | CAllegianceProfile | -| 0x0281 | inbound | JoinGameResponse | `ClientMiniGameSystem::Handle_Game__Recv_JoinGameResponse` | – | W | – | –defer:MiniGame | chess/dice/etc β€” minimal value | -| 0x0282 | inbound | StartGame | `ClientMiniGameSystem::Handle_Game__Recv_StartGame` | W | W | – | –defer:MiniGame | empty payload | -| 0x0283 | inbound | MoveResponse | `ClientMiniGameSystem::Handle_Game__Recv_MoveResponse` | – | W | – | –defer:MiniGame | minigame move ack | -| 0x0284 | inbound | OpponentTurn | `ClientMiniGameSystem::Handle_Game__Recv_OpponentTurn` | – | W | – | –defer:MiniGame | GameMoveData blob | -| 0x0285 | inbound | OpponentStalemate | `ClientMiniGameSystem::Handle_Game__Recv_OppenentStalemateState` | – | W | – | –defer:MiniGame | typo preserved (retail name) | -| 0x028A | inbound | WeenieError | `ClientCommunicationSystem::Handle_Communication__WeenieError` | W | W | W | W | error code β†’ ChatLog.OnWeenieError | -| 0x028B | inbound | WeenieErrorWithString | `ClientCommunicationSystem::Handle_Communication__WeenieErrorWithString` | W | W | W | W | (code, interp) β†’ ChatLog | -| 0x028C | inbound | GameOver | `ClientMiniGameSystem::Handle_Game__Recv_GameOver` | – | W | – | –defer:MiniGame | (gameId, winner) | -| 0x0295 | inbound | SetTurbineChatChannels | `ClientCommunicationSystem::Handle_Communication__Recv_ChatRoomTracker` [^e-d] | W | W | W | W | per-room ids β†’ TurbineChatState | -| 0x02AE | inbound | AdminQueryPluginList | – (admin tooling) | – | W | – | –skip:admin-only | server-admin path; not retail-emitted to player | -| 0x02B1 | inbound | AdminQueryPlugin | – | – | W | – | –skip:admin-only | | -| 0x02B3 | inbound | AdminQueryPluginResponse | – | – | W | – | –skip:admin-only | | -| 0x02B4 | inbound | SalvageOperationsResult | `ClientUISystem::Handle_Inventory__Recv_SalvageOperationsResultData` | – | W | – | –defer:SalvageUI | SalvageOperationsResultData blob | -| 0x02BD | inbound | Tell | – (CM_Communication) | W | W | W | W | direct whisper β†’ ChatLog | -| 0x02BE | inbound | FellowshipFullUpdate | `ClientFellowshipSystem::Handle_Fellowship__FullUpdate` | W | W | – | –defer:Fellowship | CFellowship blob | -| 0x02BF | inbound | FellowshipDisband | `ClientFellowshipSystem::Handle_Fellowship__Disband` | W | W | – | –defer:Fellowship | empty payload | -| 0x02C0 | inbound | FellowshipUpdateFellow | `ClientFellowshipSystem::Handle_Fellowship__UpdateFellow` | W | W | – | –defer:Fellowship | (memberGuid, Fellow, flag) | -| 0x02C1 | inbound | MagicUpdateSpell | `ClientMagicSystem::Handle_Magic__UpdateSpell` | W | W | W | W | learned spellId β†’ Spellbook | -| 0x02C2 | inbound | MagicUpdateEnchantment | `ClientMagicSystem::Handle_Magic__UpdateEnchantment` | W | W | W | W | Enchantment blob β†’ Spellbook | -| 0x02C3 | inbound | MagicRemoveEnchantment | `ClientMagicSystem::Handle_Magic__RemoveEnchantment` | W | W | W | W | (layerId, spellId) | -| 0x02C4 | inbound | MagicUpdateMultipleEnchantments | `ClientMagicSystem::Handle_Magic__UpdateMultipleEnchantments` | W | W | – | P+W | PackableList | -| 0x02C5 | inbound | MagicRemoveMultipleEnchantments | `ClientMagicSystem::Handle_Magic__RemoveMultipleEnchantments` | W | W | – | P+W | PackableList | -| 0x02C6 | inbound | MagicPurgeEnchantments | `ClientMagicSystem::Handle_Magic__PurgeEnchantments` | W | W | W | W | empty payload β†’ Spellbook.OnPurgeAll | -| 0x02C7 | inbound | MagicDispelEnchantment | `ClientMagicSystem::Handle_Magic__DispelEnchantment` | W | W | W | W | shared parser w/ MagicRemoveEnchantment | -| 0x02C8 | inbound | MagicDispelMultipleEnchantments | `ClientMagicSystem::Handle_Magic__DispelMultipleEnchantments` | W | W | – | P+W | PackableList | -| 0x02C9 | inbound | PortalStormBrewing | `ClientUISystem::Handle_Misc__PortalStormBrewing` | – | W | – | P+W | float intensity β†’ ChatLog system message | -| 0x02CA | inbound | PortalStormImminent | `ClientUISystem::Handle_Misc__PortalStormImminent` | – | W | – | P+W | float intensity | -| 0x02CB | inbound | PortalStorm | `ClientUISystem::Handle_Misc__PortalStorm` | – | W | – | P+W | empty payload β€” actual storm trigger | -| 0x02CC | inbound | PortalStormSubsided | `ClientUISystem::Handle_Misc__PortalStormSubsided` | – | W | – | P+W | empty payload | -| 0x02EB | inbound | CommunicationTransientString | `ClientCommunicationSystem::Handle_Communication__TransientString` | W | W | W | W | (msg, chatType) β†’ ChatLog system msg | -| 0x0312 | inbound | MagicPurgeBadEnchantments | `ClientMagicSystem::Handle_Magic__PurgeBadEnchantments` | W | W | – | P+W | empty payload | -| 0x0314 | inbound | SendClientContractTrackerTable | `ClientUISystem::Handle_Social__SendClientContractTrackerTable` | – | W | – | –defer:Quests | CContractTrackerTable blob | -| 0x0315 | inbound | SendClientContractTracker | `ClientUISystem::Handle_Social__SendClientContractTracker` | – | W | – | –defer:Quests | (CContractTracker, flag, flag) | - -**Footnotes:** - -[^e-a]: PlayerDescription has its own dedicated parser (`PlayerDescriptionParser.TryParse`) rather than living in `GameEvents.cs`. Wires into `LocalPlayerState` (vitals 7/8/9), `Spellbook` (learned spells + enchantments), `ItemRepository` (inventory + equipped), and the `onSkillsUpdated` callback (Run/Jump skills for movement). -[^e-b]: IdentifyObjectResponse uses `AppraiseInfoParser.TryParse` (separate file) rather than the simple header-only parser in `GameEvents.cs`. Returns full property bundle (int / int64 / bool / float / string / DID tables) plus SpellBook list. The retail handler `Handle_Item__AppraiseDone` (0x01CB) is the post-arrival completion signal, not the data carrier itself. -[^e-c]: 0x01E2 Emote sub-opcode is distinct from `HearEmote` (top-level GameMessage 0x02BC); the sub-opcode form is documented in ACE's `GameEventType.cs` but the named-retail decomp doesn't expose a dedicated handler β€” likely re-routed through the chat broadcast path. -[^e-d]: Named retail's `Recv_ChatRoomTracker` is the underlying handler symbol; ACE/Holtburger renamed to `SetTurbineChatChannels` for clarity. Same wire payload (per-room session ids for General/Trade/LFG/Roleplay/Society/Olthoi/Allegiance). - ---- - -## Section 5 β€” GameAction sub-opcodes (inside 0xF7B1 envelope) - -In-scope: 96. Implemented (built) in acdream: 24. Live callers in acdream: 8. Phase M target delta: 72 new builders + golden-vector tests. - -All rows are `outbound` direction (GameActions are clientβ†’server only). - -| Code | Direction | Name | Named-retail symbol | Holtburger | ACE | acdream today | Phase M target | Notes | -|------|-----------|------|---------------------|------------|-----|---------------|----------------|-------| -| 0x0005 | outbound | SetSingleCharacterOption | – | W | H | – | B | Per-option toggle; sibling of 0x01A1 bitmap | -| 0x0008 | outbound | TargetedMeleeAttack | `CM_Combat::Event_TargetedMeleeAttack` | W | H | W | B+W | Wired in WorldSession.SendMeleeAttack | -| 0x000A | outbound | TargetedMissileAttack | `CM_Combat::Event_TargetedMissileAttack` | W | H | W | B+W | Wired in WorldSession.SendMissileAttack | -| 0x000F | outbound | SetAfkMode | `CM_Communication::Event_SetAFKMode` | – | H | – | B | Toggle AFK | -| 0x0010 | outbound | SetAfkMessage | `CM_Communication::Event_SetAFKMessage` | – | H | – | B | Custom AFK string | -| 0x0015 | outbound | Talk | `CM_Communication::Event_Talk` | W | H | W | B+W | Wired in WorldSession.SendTalk | -| 0x0017 | outbound | RemoveFriend | `CM_Social::Event_RemoveFriend` | – | H | – | B | Friends list mutation | -| 0x0018 | outbound | AddFriend | `CM_Social::Event_AddFriend` | – | H | – | B | Friends list mutation | -| 0x0019 | outbound | PutItemInContainer | `CM_Inventory::Event_PutItemInContainer` | W | H | – | B | Inventory move; high priority | -| 0x001A | outbound | GetAndWieldItem | `CM_Inventory::Event_GetAndWieldItem` | W | H | – | B | Equip item | -| 0x001B | outbound | DropItem | `CM_Inventory::Event_DropItem` | W | H | – | B | Drop to ground | -| 0x001D | outbound | SwearAllegiance | `CM_Allegiance::Event_SwearAllegiance` | W | H | B | B+W | AllegianceRequests dead [^a-1] | -| 0x001E | outbound | BreakAllegiance | `CM_Allegiance::Event_BreakAllegiance` | W | H | B | B+W | AllegianceRequests dead [^a-1] | -| 0x001F | outbound | AllegianceUpdateRequest | – | – | H | – | B | Refresh allegiance tree | -| 0x0025 | outbound | RemoveAllFriends | – | – | H | – | B | Clear friends list | -| 0x0026 | outbound | TeleToPklArena | – | W | H | – | B | PK-lite arena recall | -| 0x0027 | outbound | TeleToPkArena | – | – | H | – | B | PK arena recall | -| 0x002C | outbound | TitleSet | – | – | H | – | B | Equip title | -| 0x0030 | outbound | QueryAllegianceName | `CM_Allegiance::Event_QueryAllegianceName` | – | H | – | B | – | -| 0x0031 | outbound | ClearAllegianceName | `CM_Allegiance::Event_ClearAllegianceName` | – | H | – | B | Officer-only | -| 0x0032 | outbound | TalkDirect | `CM_Communication::Event_TalkDirect` | – | H | – | B | Targeted /say (rarely used) | -| 0x0033 | outbound | SetAllegianceName | `CM_Allegiance::Event_SetAllegianceName` | – | H | – | B | Monarch-only | -| 0x0035 | outbound | UseWithTarget | `CM_Inventory::Event_UseWithTargetEvent` | W | H | B | B+W | InteractRequests dead [^a-1] | -| 0x0036 | outbound | Use | `CM_Inventory::Event_UseEvent` | W | H | B | B+W | InteractRequests dead [^a-1] | -| 0x003B | outbound | SetAllegianceOfficer | `CM_Allegiance::Event_SetAllegianceOfficer` | – | H | – | B | – | -| 0x003C | outbound | SetAllegianceOfficerTitle | `CM_Allegiance::Event_SetAllegianceOfficerTitle` | – | H | – | B | – | -| 0x003D | outbound | ListAllegianceOfficerTitles | `CM_Allegiance::Event_ListAllegianceOfficerTitles` | – | H | – | B | – | -| 0x003E | outbound | ClearAllegianceOfficerTitles | `CM_Allegiance::Event_ClearAllegianceOfficerTitles` | – | H | – | B | – | -| 0x003F | outbound | DoAllegianceLockAction | `CM_Allegiance::Event_DoAllegianceLockAction` | – | H | – | B | Lock recruitment | -| 0x0040 | outbound | SetAllegianceApprovedVassal | `CM_Allegiance::Event_SetAllegianceApprovedVassal` | – | – | – | B | – | -| 0x0041 | outbound | AllegianceChatGag | `CM_Allegiance::Event_AllegianceChatGag` | – | H | – | B | – | -| 0x0042 | outbound | DoAllegianceHouseAction | `CM_Allegiance::Event_DoAllegianceHouseAction` | – | H | – | B | – | -| 0x0044 | outbound | RaiseVital | – | W | H | B | B+W | CharacterActions builder dead [^a-1] | -| 0x0045 | outbound | RaiseAttribute | – | W | H | B | B+W | CharacterActions builder dead [^a-1] | -| 0x0046 | outbound | RaiseSkill | – | W | H | B | B+W | CharacterActions builder dead [^a-1] | -| 0x0047 | outbound | TrainSkill | `CM_Train::Event_TrainSkill` | W | H | B | B+W | CharacterActions builder dead [^a-1] | -| 0x0048 | outbound | CastUntargetedSpell | `CM_Magic::Event_CastUntargetedSpell` | W | H | B | B+W | CastSpellRequest builder dead [^a-1] | -| 0x004A | outbound | CastTargetedSpell | `CM_Magic::Event_CastTargetedSpell` | W | H | B | B+W | CastSpellRequest builder dead [^a-1] | -| 0x0053 | outbound | ChangeCombatMode | `CM_Combat::Event_ChangeCombatMode` | W | H | W | B+W | Wired in WorldSession.SendChangeCombatMode | -| 0x0054 | outbound | StackableMerge | `CM_Inventory::Event_StackableMerge` | W | H | B | B+W | InventoryActions builder dead [^a-1] | -| 0x0055 | outbound | StackableSplitToContainer | `CM_Inventory::Event_StackableSplitToContainer` | W | H | B | B+W | InventoryActions builder dead [^a-1] | -| 0x0056 | outbound | StackableSplitTo3D | `CM_Inventory::Event_StackableSplitTo3D` | – | H | B | B+W | InventoryActions builder dead [^a-1] | -| 0x0058 | outbound | ModifyCharacterSquelch | `CM_Communication::Event_ModifyCharacterSquelch` | – | H | – | B | Mute one player | -| 0x0059 | outbound | ModifyAccountSquelch | `CM_Communication::Event_ModifyAccountSquelch` | – | H | – | B | Mute account | -| 0x005B | outbound | ModifyGlobalSquelch | `CM_Communication::Event_ModifyGlobalSquelch` | – | H | – | B | Mute pattern | -| 0x005D | outbound | Tell | – | W | H | W | B+W | Wired in WorldSession.SendTell [^a-2] | -| 0x005F | outbound | Buy | `CM_Vendor::Event_Buy` | W | H | – | B | Vendor purchase | -| 0x0060 | outbound | Sell | `CM_Vendor::Event_Sell` | W | H | – | B | Vendor sell | -| 0x0063 | outbound | TeleToLifestone | `CM_Character::Event_TeleToLifestone` | W | H | B | B+W | InteractRequests builder dead [^a-1] | -| 0x00A1 | outbound | LoginComplete | `CM_Character::Event_LoginCompleteNotification` | W | H | W | B+W | Wired in GameWindow.cs:4423 | -| 0x00A2 | outbound | FellowshipCreate | `CM_Fellowship::Event_Create` | W | H | B | B+W | SocialActions builder dead [^a-1] | -| 0x00A3 | outbound | FellowshipQuit | `CM_Fellowship::Event_Quit` | W | H | B | B+W | SocialActions builder dead [^a-1] | -| 0x00A4 | outbound | FellowshipDismiss | `CM_Fellowship::Event_Dismiss` | W | H | B | B+W | SocialActions builder dead [^a-1] | -| 0x00A5 | outbound | FellowshipRecruit | `CM_Fellowship::Event_Recruit` | W | H | B | B+W | SocialActions builder dead [^a-1] | -| 0x00A6 | outbound | FellowshipUpdateRequest | `CM_Fellowship::Event_UpdateRequest` | W | H | B | B+W | SocialActions builder dead [^a-1] | -| 0x00AA | outbound | BookData | `CM_Writing::Event_BookData` | – | H | – | B | Open book contents | -| 0x00AB | outbound | BookModifyPage | `CM_Writing::Event_BookModifyPage` | – | H | – | B | Edit page text | -| 0x00AC | outbound | BookAddPage | `CM_Writing::Event_BookAddPage` | – | H | – | B | – | -| 0x00AD | outbound | BookDeletePage | `CM_Writing::Event_BookDeletePage` | – | H | – | B | – | -| 0x00AE | outbound | BookPageData | `CM_Writing::Event_BookPageData` | W | – | – | B | Read one page | -| 0x00B1 | outbound | TeleToPoi | – | – | – | B | B | InventoryActions builder dead; ACE handler unclear [^a-1][^a-3] | -| 0x00BF | outbound | SetInscription | `CM_Writing::Event_SetInscription` | – | – | – | B | Inscribe item | -| 0x00C8 | outbound | IdentifyObject | `CM_Item::Event_Appraise` | W | H | B | B+W | AppraiseRequest builder dead [^a-1] | -| 0x00CD | outbound | GiveObjectRequest | `CM_Inventory::Event_GiveObjectRequest` | W | H | B | B+W | InventoryActions builder dead [^a-1] | -| 0x00D6 | outbound | AdvocateTeleport | – | – | H | – | B | GM-only teleport | -| 0x0140 | outbound | AbuseLogRequest | `CM_Character::Event_AbuseLogRequest` | – | – | – | B | Player report tool | -| 0x0145 | outbound | AddChannel | `CM_Communication::Event_ChannelList` | – | H | B | B+W | SocialActions builder dead [^a-1][^a-4] | -| 0x0146 | outbound | RemoveChannel | – | – | H | B | B+W | SocialActions builder dead [^a-1] | -| 0x0147 | outbound | ChatChannel | `CM_Communication::Event_ChannelBroadcast` | W | H | W | B+W | Wired in WorldSession.SendChannel; same code as inbound 0x0147 [^a-5] | -| 0x0148 | outbound | ListChannels | – | – | – | – | B | – | -| 0x0149 | outbound | IndexChannels | `CM_Communication::Event_ChannelIndex` | – | – | – | B | – | -| 0x0195 | outbound | NoLongerViewingContents | `CM_Inventory::Event_NoLongerViewingContents` | W | H | – | B | Container UI close | -| 0x019B | outbound | StackableSplitToWield | `CM_Inventory::Event_StackableSplitToWield` | W | H | B | B+W | InventoryActions builder dead [^a-1] | -| 0x019C | outbound | AddShortcut | `CM_Character::Event_AddShortCut` | – | H | B | B+W | InventoryActions builder dead [^a-1] | -| 0x019D | outbound | RemoveShortcut | `CM_Character::Event_RemoveShortCut` | – | H | B | B+W | InventoryActions builder dead [^a-1] | -| 0x01A1 | outbound | SetCharacterOptions | – | – | H | B | B+W | SocialActions builder dead [^a-1] | -| 0x01A8 | outbound | RemoveSpellC2S | `CM_Magic::Event_RemoveSpell` | – | H | – | B | Self-cancel buff | -| 0x01B7 | outbound | CancelAttack | `CM_Combat::Event_CancelAttack` | W | H | W | B+W | Wired in WorldSession.SendCancelAttack | -| 0x01BF | outbound | QueryHealth | `CM_Combat::Event_QueryHealth` | W | H | B | B+W | SocialActions builder dead [^a-1] | -| 0x01C2 | outbound | QueryAge | `CM_Character::Event_QueryAge` | – | H | – | B | – | -| 0x01C4 | outbound | QueryBirth | `CM_Character::Event_QueryBirth` | – | H | – | B | – | -| 0x01DF | outbound | Emote | `CM_Communication::Event_Emote` | W | H | – | B | Custom /e text | -| 0x01E1 | outbound | SoulEmote | `CM_Communication::Event_SoulEmote` | W | H | – | B | /soulemote | -| 0x01E3 | outbound | AddSpellFavorite | `CM_Character::Event_AddSpellFavorite` | – | H | – | B | Spellbook pin | -| 0x01E4 | outbound | RemoveSpellFavorite | `CM_Character::Event_RemoveSpellFavorite` | – | – | – | B | Spellbook unpin | -| 0x01E9 | outbound | PingRequest | – | W | H | B | B+W | SocialActions builder dead; keepalive [^a-1] | -| 0x01F6 | outbound | OpenTradeNegotiations | `CM_Trade::Event_OpenTradeNegotiations` | W | H | – | B | Begin trade | -| 0x01F7 | outbound | CloseTradeNegotiations | `CM_Trade::Event_CloseTradeNegotiations` | W | H | – | B | Cancel trade | -| 0x01F8 | outbound | AddToTrade | `CM_Trade::Event_AddToTrade` | W | H | – | B | Add item to trade | -| 0x01FA | outbound | AcceptTrade | `CM_Trade::Event_AcceptTrade` | W | H | – | B | Confirm trade | -| 0x01FB | outbound | DeclineTrade | `CM_Trade::Event_DeclineTrade` | W | H | – | B | Reject trade | -| 0x0204 | outbound | ResetTrade | `CM_Trade::Event_ResetTrade` | W | H | – | B | Clear pending items | -| 0x0216 | outbound | ClearPlayerConsentList | `CM_Character::Event_ClearPlayerConsentList` | – | H | – | B | Resurrection consent | -| 0x0217 | outbound | DisplayPlayerConsentList | `CM_Character::Event_DisplayPlayerConsentList` | – | H | – | B | – | -| 0x0218 | outbound | RemoveFromPlayerConsentList | `CM_Character::Event_RemoveFromPlayerConsentList` | – | – | – | B | – | -| 0x0219 | outbound | AddPlayerPermission | `CM_Character::Event_AddPlayerPermission` | W | H | – | B | Storage / consent perm | -| 0x021A | outbound | RemovePlayerPermission | `CM_Character::Event_RemovePlayerPermission` | W | H | – | B | – | -| 0x021C | outbound | BuyHouse | `CM_House::Event_BuyHouse` | – | H | – | –defer:Phase Q | Housing β€” out of M baseline scope | -| 0x021E | outbound | HouseQuery | – | – | H | – | –defer:Phase Q | Housing | -| 0x021F | outbound | AbandonHouse | `CM_House::Event_AbandonHouse` | – | H | – | –defer:Phase Q | Housing | -| 0x0221 | outbound | RentHouse | `CM_House::Event_RentHouse` | – | – | – | –defer:Phase Q | Housing | -| 0x0224 | outbound | SetDesiredComponentLevel | – | – | – | – | B | Component-buy preference | -| 0x0245 | outbound | AddPermanentGuest | `CM_House::Event_AddPermanentGuest_Event` | – | H | – | –defer:Phase Q | Housing | -| 0x0246 | outbound | RemovePermanentGuest | `CM_House::Event_RemovePermanentGuest_Event` | – | H | – | –defer:Phase Q | Housing | -| 0x0247 | outbound | SetOpenHouseStatus | `CM_House::Event_SetOpenHouseStatus_Event` | – | H | – | –defer:Phase Q | Housing | -| 0x0249 | outbound | ChangeStoragePermission | `CM_House::Event_ChangeStoragePermission_Event` | – | H | – | –defer:Phase Q | Housing | -| 0x024A | outbound | BootSpecificHouseGuest | `CM_House::Event_BootSpecificHouseGuest_Event` | – | H | – | –defer:Phase Q | Housing | -| 0x024C | outbound | RemoveAllStoragePermission | `CM_House::Event_RemoveAllStoragePermission` | – | H | – | –defer:Phase Q | Housing | -| 0x024D | outbound | RequestFullGuestList | `CM_House::Event_RequestFullGuestList_Event` | – | – | – | –defer:Phase Q | Housing | -| 0x0254 | outbound | SetMotd | `CM_Allegiance::Event_SetMotd` | – | – | – | B | Allegiance message-of-the-day | -| 0x0255 | outbound | QueryMotd | `CM_Allegiance::Event_QueryMotd` | – | – | – | B | – | -| 0x0256 | outbound | ClearMotd | `CM_Allegiance::Event_ClearMotd` | – | H | – | B | – | -| 0x0258 | outbound | QueryLord | `CM_House::Event_QueryLord` | – | – | – | –defer:Phase Q | Housing | -| 0x025C | outbound | AddAllStoragePermission | `CM_House::Event_AddAllStoragePermission` | – | – | – | –defer:Phase Q | Housing | -| 0x025E | outbound | RemoveAllPermanentGuests | `CM_House::Event_RemoveAllPermanentGuests_Event` | – | H | – | –defer:Phase Q | Housing | -| 0x025F | outbound | BootEveryone | `CM_House::Event_BootEveryone_Event` | – | H | – | –defer:Phase Q | Housing | -| 0x0262 | outbound | TeleToHouse | `CM_House::Event_TeleToHouse_Event` | – | – | – | –defer:Phase Q | Housing | -| 0x0263 | outbound | QueryItemMana | `CM_Item::Event_QueryItemMana` | W | H | – | B | Mana-meter check | -| 0x0266 | outbound | SetHooksVisibility | `CM_House::Event_SetHooksVisibility` | – | H | – | –defer:Phase Q | Housing | -| 0x0267 | outbound | ModifyAllegianceGuestPermission | `CM_House::Event_ModifyAllegianceGuestPermission` | – | – | – | –defer:Phase Q | Housing | -| 0x0268 | outbound | ModifyAllegianceStoragePermission | `CM_House::Event_ModifyAllegianceStoragePermission` | – | – | – | –defer:Phase Q | Housing | -| 0x0269 | outbound | ChessJoin | – | – | H | – | –skip:minigame | Chess | -| 0x026A | outbound | ChessQuit | – | – | H | – | –skip:minigame | Chess | -| 0x026B | outbound | ChessMove | – | – | H | – | –skip:minigame | Chess | -| 0x026D | outbound | ChessMovePass | – | – | H | – | –skip:minigame | Chess | -| 0x026E | outbound | ChessStalemate | – | – | H | – | –skip:minigame | Chess | -| 0x0270 | outbound | ListAvailableHouses | `CM_House::Event_ListAvailableHouses` | – | – | – | –defer:Phase Q | Housing | -| 0x0275 | outbound | ConfirmationResponse | `CM_Character::Event_ConfirmationResponse` | W | H | – | B | Yes/No popups | -| 0x0277 | outbound | BreakAllegianceBoot | `CM_Allegiance::Event_BreakAllegianceBoot` | – | H | – | B | Officer kick | -| 0x0278 | outbound | TeleToMansion | `CM_House::Event_TeleToMansion_Event` | W | – | – | –defer:Phase Q | Housing recall | -| 0x0279 | outbound | Suicide | `CM_Character::Event_Suicide` | W | – | – | B | /suicide cmd | -| 0x027B | outbound | AllegianceInfoRequest | `CM_Allegiance::Event_AllegianceInfoRequest` | – | H | – | B | Tree info | -| 0x027D | outbound | CreateTinkeringTool / SalvageItemsWith | `CM_Inventory::Event_CreateTinkeringTool` | W | H | – | B | Salvage UI [^a-6] | -| 0x0286 | outbound | SpellbookFilter | `CM_Character::Event_SpellbookFilterEvent` | – | – | – | B | School filter | -| 0x028D | outbound | TeleToMarketPlace | – | W | – | – | B | MP recall | -| 0x028F | outbound | EnterPkLite | – | W | – | – | B | PK-lite toggle | -| 0x0290 | outbound | FellowshipAssignNewLeader | `CM_Fellowship::Event_AssignNewLeader` | W | H | – | B | – | -| 0x0291 | outbound | FellowshipChangeOpenness | `CM_Fellowship::Event_ChangeFellowOpeness` | – | H | – | B | – | -| 0x02A0 | outbound | AllegianceChatBoot | `CM_Allegiance::Event_AllegianceChatBoot` | – | – | – | B | Officer chat-mute | -| 0x02A1 | outbound | AddAllegianceBan | `CM_Allegiance::Event_AddAllegianceBan` | – | H | – | B | – | -| 0x02A2 | outbound | RemoveAllegianceBan | `CM_Allegiance::Event_RemoveAllegianceBan` | – | – | – | B | – | -| 0x02A3 | outbound | ListAllegianceBans | `CM_Allegiance::Event_ListAllegianceBans` | – | – | – | B | – | -| 0x02A5 | outbound | RemoveAllegianceOfficer | `CM_Allegiance::Event_RemoveAllegianceOfficer` | – | H | – | B | – | -| 0x02A6 | outbound | ListAllegianceOfficers | `CM_Allegiance::Event_ListAllegianceOfficers` | – | – | – | B | – | -| 0x02A7 | outbound | ClearAllegianceOfficers | `CM_Allegiance::Event_ClearAllegianceOfficers` | – | – | – | B | – | -| 0x02AB | outbound | RecallAllegianceHometown | `CM_Allegiance::Event_RecallAllegianceHometown` | – | – | – | B | Bind to monarch lifestone | -| 0x02AF | outbound | QueryPluginListResponse | `CM_Admin::Event_QueryPluginListResponse` | – | – | – | –skip:plugin-c2s | Decal-era plugin probe | -| 0x02B2 | outbound | QueryPluginResponse | `CM_Admin::Event_QueryPluginResponse` | – | – | – | –skip:plugin-c2s | Decal-era plugin probe | -| 0x0311 | outbound | FinishBarber | `CM_Character::Event_FinishBarber` | – | H | – | B | Char appearance commit | -| 0x0316 | outbound | AbandonContract | `CM_Social::Event_AbandonContract` | – | H | – | B | Drop quest | - -**Footnotes:** - -[^a-1]: "Builder dead" = the byte-array builder is implemented in `src/AcDream.Core.Net/Messages/.cs` but no caller in `src/AcDream.App/` or a `WorldSession.Send*` wrapper invokes it. Phase M wires these to game-state actions (UI clicks, command bus, key bindings) and adds golden-vector tests against holtburger fixtures. -[^a-2]: ACE's wire field order for Tell is `message FIRST then target` (see `ChatRequests.BuildTell` doc comment). Sept-2013 PDB has no `Event_Tell` symbol β€” it routes through `CM_Communication::Event_TalkDirectByName` plus a server-side rename. -[^a-3]: TeleToPoi (0x00B1) is listed in `InventoryActions.cs` but not in ACE's `GameActionType` enum. Cross-reference holtburger to confirm; may be a dead-letter opcode that retail's vendored 2013 ACE branch dropped. Verify before shipping the test vector. -[^a-4]: AddChannel (0x0145) β€” named-retail's matching symbol is `Event_ChannelList` (0x0148 according to retail enum), so the symbol mapping is approximate; AddChannel in pseudo-C may be unsymbolicated. Confirm by greping `acclient_2013_pseudo_c.txt` before publishing. -[^a-5]: 0x0147 ChannelBroadcast is the same numeric code in both directions (outbound GameAction = client sends to channel; inbound GameEvent = server broadcasts to channel members). Listed under outbound here per Section-5 scope; inbound version is in Β§4. -[^a-6]: ACE GameActionType lists 0x027D as `CreateTinkeringTool`; holtburger names the same opcode `SalvageItemsWith`. Both behaviors funnel through the salvage UI in retail. Either name is acceptable in acdream; pick one and leave the other as an alias constant. - ---- - -## Source attribution - -- **Holtburger** β€” `references/holtburger/` at `629695a` (2026-05-10). Primary client-behavior oracle. -- **ACE** β€” `references/ACE/Source/ACE.Server/Network/`. Server-side authority for GameMessages, GameEvents, GameActions, and accept rules. -- **Named retail decomp** β€” `docs/research/named-retail/` (Sept 2013 EoR PDB + Binary Ninja pseudo-C). Wire-format ground truth for the 2013 client. -- **acdream current state** β€” `src/AcDream.Core.Net/` and `src/AcDream.App/`. Inventoried by parallel agents on 2026-05-10. - -## Caveats - -This is the **initial population**, produced by four parallel research agents (one per opcode class) on 2026-05-10. Spot-check pass + intentional-divergence ratification is owed before M.1 closes. Specifically: - -- A handful of named-retail symbol citations are tentative (marked in footnotes); spot-check by greping `acclient_2013_pseudo_c.txt` and `symbols.json`. -- Holtburger / ACE / acdream cells were determined by reading the actual code (not guessing); when an agent couldn't determine a value, it used `?`. The `?` cells need a follow-up read. -- "Dead builder" calls (rows where acdream `B` but Phase M target is `B+W`) are based on a grep for `WorldSession.Send*` patterns and `worldSession.Send` calls in `src/AcDream.App/`. Edge cases (call sites in test code, command-bus indirection) may have been missed. -- Total opcode count in scope (~284) is approximate; deduplication of cross-section codes (e.g., 0x0147 in Β§4 and Β§5) is tracked in footnotes but the headline count treats them as distinct rows. - -This matrix lives on as a long-term reference. Phase M.6 implementation tracks progress against it; gameplay phases consuming Phase M will reference the rows they wire as part of their phase acceptance. diff --git a/docs/research/2026-05-10-post-a5-polish-handoff.md b/docs/research/2026-05-10-post-a5-polish-handoff.md deleted file mode 100644 index cb52b99..0000000 --- a/docs/research/2026-05-10-post-a5-polish-handoff.md +++ /dev/null @@ -1,307 +0,0 @@ -# Phase Post-A.5 Polish β€” Cold-Start Handoff - -**Created:** 2026-05-10, immediately after A.5 SHIP + merge to main (`d3d78fa`). -**Audience:** the next agent picking up post-A.5 polish work. -**Purpose:** give you everything you need to start the polish phase cold, without spelunking through the A.5 session's 200+ messages. - ---- - -## TL;DR - -A.5 just shipped. Two-tier streaming is live (N₁=4 near, Nβ‚‚=12 far) with a 2.3 km fog horizon, off-thread mesh build, entity dispatcher tightening, mipmaps + 16x AF, MSAA 4x + A2C foliage, depth-write audit, BUDGET_OVER diag, and a full Quality Preset system (Low/Medium/High/Ultra) with env-var overrides + F11 mid-session re-apply. - -**A.5 was an enormous phase** (29 numbered tasks + T22.5 mid-execution scope add + Bug A + Bug B post-T26 fixes). Spec at `docs/superpowers/specs/2026-05-09-phase-a5-two-tier-streaming-design.md` (~700 lines). Plan at `docs/superpowers/plans/2026-05-09-phase-a5-two-tier-streaming.md` (~2400 lines). - -**Three things were intentionally deferred to this phase:** - -1. **Lifestone visual missing (ISSUE #52).** The Holtburg lifestone β€” a known visual landmark β€” hasn't been rendering since earlier in A.5 development. User confirmed they noticed it earlier but didn't flag it; deferred to post-ship. **Highest user-perception value to fix.** - -2. **JobKind plumbing through `BuildLandblockForStreaming` (ISSUE #54).** Bug A's fix patches at the worker output by stripping entities from far-tier `LoadedLandblock`s after the full load runs. The worker still wastes CPU on hydration + scenery generation that gets thrown away. Cleaner fix: make the worker SKIP that work for far-tier loads. ~30 min - 1 hour. **Smallest cleanup, biggest worker-thread efficiency win.** - -3. **Tier 1 entity-classification cache retry (ISSUE #53).** First attempt (commit `3639a6f`, reverted at `9b49009`) cached `meshRef.PartTransform` which is mutated per frame for animated entities β€” froze animations. Retry needs a careful read of `AnimationSequencer` + `AnimationHookRouter` first to map ALL the per-frame mutations of MeshRef state, then design a cache that bypasses animated entities OR caches only the animation-invariant subset. **Biggest perf headroom available** β€” math says it should drop the entity dispatcher from 3.5ms to 1-1.5ms, hitting the spec's 2.0ms budget. - -The phase is sized ~1 week if all three land cleanly. Could be longer if Tier 1's animation audit reveals something subtle. - ---- - -## Where A.5 left things - -### Branch state - -- `main` is at `d3d78fa` ("Merge branch 'claude/hopeful-darwin-ae8b87' β€” Phase A.5 SHIP + Quality Preset system"). -- A.5 SHIP commit at `9245db5` (one commit before the merge bubble). -- Roadmap entry: A.5 moved from "Phases ahead" β†’ "Phases already shipped" table. -- CLAUDE.md "Currently in flight" updated to "Post-A.5 polish β€” Tier 1 retry + lifestone fix + JobKind plumbing". - -### What works in A.5 (final post-fix state) - -- **Two-tier streaming end-to-end:** `StreamingRegion` with `RecenterTo` returning a 5-list `TwoTierDiff` (ToLoadFar/ToLoadNear/ToPromote/ToDemote/ToUnload) with hysteresis radius+2 on both tiers; `StreamingController.Tick` routes by `LandblockStreamJobKind`; `LandblockStreamer` worker thread does dat reads + mesh build off the render thread. -- **Bug A fixed:** `LandblockStreamer.HandleJob` strips entities for `LoadFar` results before posting Loaded. Far-tier ships terrain only as the spec promised. -- **Bug B fixed:** `WalkEntities` uses `_walkScratch` field reused across frames, no per-frame List allocation. -- **Quality Preset system:** Low / Medium / High / Ultra presets with per-preset radii + MSAA + anisotropic + A2C + max-completions. 6 env-var overrides per field. F11 β†’ Display tab dropdown for mid-session change. `DisplaySettings.Quality` persists in settings.json. `GameWindow.ReapplyQualityPreset` rebuilds the streaming pipeline for radius changes. -- **Visual quality stack:** mipmaps + 16x anisotropic on TerrainAtlas. MSAA 4x + alpha-to-coverage on foliage shader. Depth-write audit + lock-in test (5 cases). -- **Fog horizon:** FogStart = N₁ Γ— 192m Γ— 0.7 β‰ˆ 538m. FogEnd = Nβ‚‚ Γ— 192m Γ— 0.95 β‰ˆ 2188m. Tunable via `ACDREAM_FOG_START_MULT` / `ACDREAM_FOG_END_MULT`. -- **DIAG:** `[WB-DIAG]` and `[TERRAIN-DIAG]` flag `BUDGET_OVER` when median exceeds the per-subsystem spec budget (entity 2.0ms, terrain 1.0ms). - -### Final perf state at A.5 SHIP (horizon-safe Quality preset) - -User hardware: AMD Radeon RX 9070 XT, 240 Hz @ 2560Γ—1440. - -Settings tested: `NEAR_RADIUS=4, FAR_RADIUS=12, MSAA=0, A2C=0, ANISOTROPIC=4, MAX_COMPLETIONS=2`. - -| Subsystem | cpu_us median | cpu_us p95 | -|---|---|---| -| Entity dispatcher | ~3500 Β΅s (3.5 ms) | ~4000 Β΅s | -| Terrain dispatcher | ~21 Β΅s | ~26 Β΅s | - -Total frame time math: ~4-5 ms = ~200-240 FPS at standstill. User reported "Better now" β€” not the 240Hz spec target but a 5Γ— improvement from the broken pre-Bug-A state (~40 FPS). - -The 1.5ms gap to the 2.0ms entity dispatcher budget is what Tier 1 closes (per ISSUE #53 + the perf-tier roadmap). - -### What was NOT validated at SHIP - -- **Full High preset (radius=4/12, MSAA 4x, A2C on, anisotropic 16x).** Crashed the entire OS at first attempt earlier in A.5 development. Bug A was likely the trigger (CPU dispatcher saturating + GPU command queue overflowing). With Bug A fixed, this likely works β€” but never re-tested. **Re-testing is part of this phase's stretch goal.** -- **Visual gate at full quality.** Same β€” only validated at horizon-safe settings. -- **Walking trace at any preset.** Brief walking observed but not metric-captured. - -### Three high-value gotchas captured in A.5 memory - -These are at `~/.claude/projects/.../memory/project_phase_a5_state.md`: - -1. **Worker-side JobKind routing was the load-bearing far-tier optimization.** T13/T16 wired the controller side; the worker never branched on Kind. ~5x perf regression that wasn't caught by spec/code reviews. -2. **WalkEntities's "extract a list-producing helper" pattern is a perf antipattern.** ~480 KB / frame allocation. Implementer flagged "future N.6 optimization" in self-review; review should have caught that "future" was actually "now." -3. **Caching mutable per-frame state silently breaks animation.** Tier 1's first attempt. The "trust MeshRefs as the source of truth" comment in the dispatcher is true but misleading β€” MeshRefs IS the source of truth, but it's mutated EVERY frame for animated entities. - -(Full memory entry has 5 gotchas; these three are the load-bearing ones for post-A.5.) - ---- - -## Files to read before brainstorming - -In rough order: - -1. **`docs/superpowers/specs/2026-05-09-phase-a5-two-tier-streaming-design.md`** β€” A.5 spec, full design rationale + Quality Preset system (Β§4.10) + acceptance criteria reshape (Β§2). Skim for vocabulary; read Β§4.10 in full. -2. **`docs/plans/2026-05-10-perf-tiers-2-3-roadmap.md`** β€” Tier 2 (static/dynamic split) + Tier 3 (GPU compute culling) roadmap. Read for context on where Tier 1 fits in the perf optimization tower. -3. **`docs/ISSUES.md` issues #52, #53, #54** β€” the three deferred items in tactical-list form. -4. **`memory/project_phase_a5_state.md`** β€” the 5 gotchas. Critical for avoiding the same traps in this phase. -5. **`src/AcDream.App/Streaming/LandblockStreamer.cs`** β€” `HandleJob` is where Bug A's patch lives + where ISSUE #54's cleaner fix will go. -6. **`src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs`** β€” `WalkEntities` + `Draw`'s inner loop. Where Tier 1's retry will operate. -7. **`src/AcDream.Core/Physics/AnimationSequencer.cs`** β€” the per-frame animation engine. Read this BEFORE designing Tier 1's retry. Pay specific attention to anywhere it touches `meshRef.PartTransform` or any other field that the dispatcher reads. -8. **`src/AcDream.App/Animation/AnimationHookRouter.cs`** (or similar) β€” the hook fan-out from animation events. Same audit reason as #7. - ---- - -## Per-priority detail - -### Priority 1 β€” Lifestone missing (ISSUE #52) - -**Estimated effort:** 1-3 hours. Could be a 1-line fix or could surface a deeper issue. - -The Holtburg lifestone is a Setup-multi-part entity (the spinning blue crystal pillar). User reports it hasn't been rendering since earlier in A.5 development. They noticed but didn't flag during the session. - -Hypotheses: - -- **Bug A's strip caught a near-tier entity.** The current strip in `LandblockStreamer.HandleJob` only fires when `tier == LandblockStreamTier.Far`. Holtburg's lifestone is in a near-tier LB (Holtburg's center, ~LB 0xA9B4). Should NOT have been stripped. But verify β€” maybe the LB's tier resolution at first-tick is wrong. -- **Earlier visual regression from a different commit.** User said it was missing in earlier runs too. Could be from N.5b, an N.5b follow-up, or even older. Requires a `git log -- docs/ISSUES.md` correlation with visible state. -- **Setup-rendering edge case.** The lifestone has unusual properties (animated rotation, particle effects on top). Maybe it's a Setup with some sub-mesh that the dispatcher's `SetupParts` walk filters out. -- **Dat-state mismatch.** The lifestone's GfxObj id might be in a part of the dat that's failing decode. - -**Investigation steps:** - -1. Launch the client + walk to Holtburg lifestone position. -2. Check `[WB-DIAG]` for `meshMissing` count β€” if non-zero, some entity's mesh isn't loading. -3. Use the cdb attach toolchain (per CLAUDE.md "Retail debugger toolchain") if needed to compare vs retail's lifestone rendering. -4. Compare to ACViewer / WorldBuilder to see if the lifestone renders there. If yes, our renderer has a regression. If no, the issue is dat-side or in shared decode logic. -5. Identify the GfxObj/Setup id for the lifestone (likely well-known retail ID; check `docs/research/named-retail/` or ACViewer reference). -6. Trace: does `_meshAdapter.TryGetRenderData(lifestoneId)` return non-null? Does the resulting `renderData.Batches` have entries? - -**Acceptance:** lifestone renders correctly (visible spinning blue crystal at the Holtburg town center). - -### Priority 2 β€” JobKind plumbing through `BuildLandblockForStreaming` (ISSUE #54) - -**Estimated effort:** 30 min - 1 hour. - -Currently `LandblockStreamer.HandleJob` strips entities POST-load for far-tier: - -```csharp -case LandblockStreamJob.Load load: - var lb = _loadLandblock(load.LandblockId); // full load - var mesh = _buildMeshOrNull(load.LandblockId, lb); - var tier = load.Kind == LandblockStreamJobKind.LoadFar ? Far : Near; - if (tier == LandblockStreamTier.Far && lb.Entities.Count > 0) - { - // Strip entities β€” far-tier ships terrain only. - lb = new LoadedLandblock(...empty entities...); - } - _outbox.Writer.TryWrite(new Loaded(... lb, mesh ...)); - break; -``` - -The full `_loadLandblock` does: -1. Read `LandBlock` heightmap (cheap). -2. Read `LandBlockInfo` (medium). -3. `LandblockLoader.BuildEntitiesFromInfo` (extract stabs/buildings). -4. Hydrate stab/building meshRefs (medium). -5. Run scenery generation (heavy β€” ~50-200 procedural entities Γ— meshRef hydration). -6. Build interior cell entities. - -For far-tier, only step 1 is needed. Steps 2-6 are wasted CPU on the worker thread. - -**Refactor plan:** - -1. Change the streamer's `_loadLandblock` factory to take `LandblockStreamJobKind`: - ```csharp - private readonly Func _loadLandblock; - ``` -2. In `GameWindow`, the factory closure branches: - ```csharp - loadLandblock: (id, kind) => kind == LandblockStreamJobKind.LoadFar - ? BuildLandblockHeightmapOnly(id) - : BuildLandblockForStreaming(id), - ``` -3. New `BuildLandblockHeightmapOnly` returns a `LoadedLandblock` with the heightmap dat record + empty entity list. Cheap β€” no LandBlockInfo read, no scenery generation. -4. Remove the post-load strip in `HandleJob` (no longer needed). -5. Worker-thread CPU drops measurably; horizon fill on first traversal speeds up. - -**Acceptance:** -- Build green; existing 999+ tests pass. -- Streaming worker thread is measurably faster on first-traversal (the user can validate with `[WB-DIAG]` worker queue depth or just feel the responsiveness when walking into virgin region). -- Visible behavior unchanged β€” far tier looks the same as before. - -### Priority 3 β€” Tier 1 entity-classification cache retry (ISSUE #53) - -**Estimated effort:** ~5-7 days. Substantial because the audit step is critical. - -This is the BIG perf win remaining for A.5's CPU dispatcher. Math says entity dispatcher 3.5ms β†’ 1-1.5ms = ~300-400 FPS at standstill. Drops the dispatcher inside the spec's 2.0ms budget. - -**The first attempt's failure (commit 3639a6f, reverted at 9b49009):** - -Cached `meshRef.PartTransform` baked into per-(entity, batch) classification at first-frame visit. For static entities, this is stable forever. For animated entities, `meshRef.PartTransform` is updated EVERY FRAME by `AnimationSequencer` to apply the current skeletal pose. The cache froze the pose. - -User-visible symptoms: -- NPCs / players stop animating. -- Some buildings (likely those mistakenly in `animatedEntityIds`) draw at wrong positions. - -**The retry's audit step (do this BEFORE designing the cache):** - -Read `src/AcDream.Core/Physics/AnimationSequencer.cs` and trace EVERY assignment to `meshRef.PartTransform` (and any other field on `MeshRef`, `WorldEntity`, or related state that the dispatcher reads). Likely write sites: -- `AnimationSequencer.TickAnimations` per-frame skeletal pose update -- `AnimationHookRouter` for hooks like `AnimSetPose` -- Live network handlers that mutate `entity.Position` / `entity.Rotation` (T18 already migrated these to `SetPosition` for AABB invalidation; double-check) -- `EntitySpawnAdapter` for ObjDescEvent / palette swap - -For each write site, decide: is this entity STATIC (write only at spawn) or DYNAMIC (write per-frame or in response to network events)? - -**Cache design options after the audit:** - -(a) **Static-only cache.** Only cache entities where `animatedEntityIds.Contains(entity.Id) == false`. Animated entities use today's per-frame classification path. Cleanest, but requires `animatedEntityIds` to be a stable signal (it is β€” `_animatedEntities` dict in GameWindow is the source). - -(b) **Dynamic-aware cache with invalidation hooks.** Cache everything but expose `InvalidateEntity(uint)` / `RefreshEntityPalette(uint)` for the dispatcher's invalidation. Wire from the network layer (palette swap fires invalidation; ObjDesc event fires invalidation). More complex but might let animated entities also benefit. - -(c) **Static-only + animated-bypass + diagnostic check.** Like (a), but in DEBUG builds, log a warning every frame if a cached entity's `meshRef.PartTransform` differs from the cached value (catches mis-classified dynamics). Belt-and-suspenders. - -Recommendation: start with (a). Ship Tier 1 for static entities only. Animated path stays slow but correct. If perf gate finds the static-only Tier 1 isn't enough, escalate to (c) for safety + (b) later. - -**Acceptance:** -- Build green; existing 999+ tests pass. -- 1-3 new tests covering: cache hit for static entity, cache bypass for animated entity, cache invalidation on entity remove. -- Visual gate: launch + walk Holtburg β†’ North Yanshi at horizon-safe preset; confirm: - - Animation works (NPCs, player character animate normally) - - Buildings at correct positions - - Lifestone (still depending on Priority 1 fix) renders correctly - - No new visual regressions -- Perf gate (with `[WB-DIAG]`): - - Entity dispatcher cpu_us median drops from ~3.5ms to ≀2.0ms (matches spec budget). - - p95 stays ≀ 2.5ms. - ---- - -## What's NOT in this phase - -- **Tier 2 (static/dynamic split with persistent groups).** Separate ~2-week phase. See `docs/plans/2026-05-10-perf-tiers-2-3-roadmap.md`. -- **Tier 3 (GPU compute culling).** Separate ~1-month phase. Same roadmap. -- **Full High preset crash investigation beyond casual retest.** Stretch goal: re-test the High preset with Bug A + B fixed, see if it's stable now. If it crashes, file a new issue and continue. Don't deep-dive in this phase. -- **EnvCell modern path migration, Sky/Particles modern path, Shadow mapping** β€” all later phases. -- **N.6 perf polish (the previously-flagged "next phase").** N.6 was the original CLAUDE.md "Currently in flight" target before A.5. Most of N.6's scope was rolled into A.5 (perf-tier work). What's left of N.6 (persistent-mapped indirect buffer, GPU-side culling) overlaps with Tier 2/3 and should be re-scoped after Tier 1 lands. - ---- - -## Acceptance criteria (whole phase) - -- All three priorities (Lifestone, JobKind, Tier 1) shipped or one is explicitly deferred with documented reasoning. -- Build green throughout. ~999+ tests pass; 8 pre-existing physics/input failures stay at 8. -- N.5b conformance sentinel intact (TerrainSlot, TerrainModernConformance, Wb*, MatrixComposition, TextureCacheBindless, SplitFormulaDivergence β€” all clean). -- Visual gate: lifestone renders; animation works; horizon visible at ~2.3km; smooth walking trace; no new artifacts. -- Perf gate (post-Tier-1): entity dispatcher cpu_us median ≀ 2.0ms at horizon-safe preset, ~250-300 FPS at standstill. -- Memory entry written + roadmap "shipped" row updated for the polish phase. - ---- - -## What you'll be doing in the first 30 minutes - -1. Read this handoff in full. -2. Verify build green: `dotnet build`. Verify ~999 tests pass: `dotnet test --no-build`. -3. Read `docs/superpowers/specs/2026-05-09-phase-a5-two-tier-streaming-design.md` Β§2, Β§4.10, Β§11 (deferred section). -4. Read `docs/plans/2026-05-10-perf-tiers-2-3-roadmap.md` Tier 1 section. -5. Read `docs/ISSUES.md` issues #52, #53, #54 in full. -6. Read `memory/project_phase_a5_state.md` (5 gotchas). -7. Read `src/AcDream.App/Streaming/LandblockStreamer.cs` HandleJob method. -8. Read `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` Draw + WalkEntitiesInto methods. -9. Skim `src/AcDream.Core/Physics/AnimationSequencer.cs` for write-sites of `meshRef.PartTransform` (Tier 1 retry's audit prerequisite). -10. Decide: which priority to start with? Recommendation order: 1 (lifestone, fast win), 2 (JobKind, easy cleanup), 3 (Tier 1, biggest perf win + most complex). -11. Brainstorm with the user on the chosen priority before writing code. -12. Write a small spec or just the implementation if the priority is small (1 + 2 are small enough to skip a formal spec). Tier 1 (priority 3) needs a spec because of the audit + invalidation design. - -Don't skip the audit step on Tier 1. The first attempt failed because of an incomplete read of the animation mutation graph; the second attempt should not repeat that. - ---- - -## Things to NOT do - -- **Don't rush Tier 1.** Audit first. Write down which entities are static vs dynamic. Write tests that specifically verify animated entities still animate after caching is enabled. -- **Don't bundle Tier 2 or Tier 3 into this phase.** Those are dedicated multi-week phases with their own brainstorm + spec + plan cycles. -- **Don't break the N.5b conformance sentinel.** Run the filter on every commit: - ``` - dotnet test --no-build --filter "FullyQualifiedName~TerrainSlot|FullyQualifiedName~TerrainModernConformance|FullyQualifiedName~Wb|FullyQualifiedName~MatrixComposition|FullyQualifiedName~TextureCacheBindless|FullyQualifiedName~SplitFormulaDivergence" - ``` - Expect 89+ passing, 0 failures. -- **Don't skip the visual gate.** Lifestone fix specifically requires looking at the lifestone in-game. Tier 1 retry requires confirming animation works on a moving NPC. -- **Don't delete the `_walkScratch` field** added in Bug B fix. It's load-bearing β€” without it, Tier 1 retry would re-introduce the per-frame allocation bug. -- **Don't re-add the `Tier1` cache that was reverted.** Start the retry with a fresh design after the animation audit. Cherry-picking the reverted code will re-introduce the bug. - ---- - -## Reference: A.5's commit chain - -Final A.5 commit chain on `claude/hopeful-darwin-ae8b87` (merged into main at `d3d78fa`): - -| SHA | Subject | -|---|---| -| 9245db5 | phase(A.5): SHIP β€” two-tier streaming + horizon LOD + Quality Preset system | -| d93d823 | docs(A.5 T27): roadmap + ISSUES + CLAUDE.md updates for A.5 ship | -| a28a5b7 | docs(A.5 T27): spec + plan amendments for T22.5 + ship | -| 9b49009 | Revert "feat(perf): Tier 1 entity classification cache" | -| 3639a6f | feat(perf): Tier 1 entity classification cache (REVERTED) | -| 462f9d6 | docs(perf): roadmap for Tier 2 + Tier 3 entity-dispatcher optimizations | -| 0ad8c99 | fix(A.5): WalkEntities scratch-list pattern (Bug B β€” T17 GC pressure) | -| 9217fd9 | fix(A.5): strip far-tier entities in worker (Bug A β€” far tier optimization) | -| 28d2c60 | feat(A.5 T22.5): wire QualityPreset into renderer + streaming (commit 2/2) | -| afa4200 | feat(A.5 T22.5): QualityPreset schema + tests (commit 1/2) | -| c473fee | feat(A.5 T23): BUDGET_OVER flag in [WB-DIAG] / [TERRAIN-DIAG] | -| 3b684db | feat(A.5 T22): fog wired from N₁/Nβ‚‚ + ACDREAM_FOG_*_MULT env vars | -| 1488ec6 | test(A.5 T21): lock in depth-write attribution per translucency kind | -| 26b2871 | feat(A.5 T20): MSAA 4x + alpha-to-coverage on foliage | -| 4b84e56 | feat(A.5 T19): mipmaps + 16x anisotropic on TerrainAtlas | -| (...60+ commits earlier in the chain through T1-T18) | (see full log on the merge bubble) | - -The merge bubble preserves the full chain. To inspect any A.5 commit: -``` -git log d3d78fa^..d3d78fa -git show -``` - ---- - -Good luck. The phase is well-bounded; the audit step on Tier 1 is the single highest-leverage thing to invest in. The lifestone and JobKind cleanup should be quick wins. After this phase ships, the project is in a great position β€” A.5 + polish + Tier 2/3 roadmap covers the rendering + perf work for the next several months. - -Holler at the user if any of the three priorities reveals scope you didn't expect. diff --git a/docs/research/2026-05-10-tier1-mutation-audit.md b/docs/research/2026-05-10-tier1-mutation-audit.md deleted file mode 100644 index f206bf4..0000000 --- a/docs/research/2026-05-10-tier1-mutation-audit.md +++ /dev/null @@ -1,246 +0,0 @@ -# Tier 1 entity-classification cache β€” mutation audit - -**Created:** 2026-05-10, opening move of the ISSUE #53 retry session. -**Purpose:** enumerate every code path that writes to `WorldEntity.MeshRefs` (the dispatcher's load-bearing per-entity input) and every adjacent state read by `WbDrawDispatcher.ClassifyBatches` / model-matrix composition, classify each as STATIC or DYNAMIC, and design the cache invalidation surface BEFORE touching renderer code. - -This audit is the load-bearing prerequisite the prior Tier 1 attempt (commit `3639a6f`, reverted at `9b49009`) skipped. Cache design follows from the audit, not the other way around. - ---- - -## TL;DR β€” the invariant - -> **An entity's `MeshRefs` reference, `Position`, `Rotation`, `PaletteOverride`, `HiddenPartsMask`, `ParentCellId`, and `Scale` are stable from spawn to despawn IF AND ONLY IF the entity is NOT in `GameWindow._animatedEntities`.** - -That is the invariant the cache rides on. Animated entities (player + remote NPCs/players + animated dat scenery like the lifestone crystal) get a fresh `MeshRefs` list every frame from `TickAnimations` plus per-frame `Position`/`Rotation` writes from physics/dead-reckoning. Everything else β€” stabs, scenery, cell-mesh entities, interior static objects β€” touches none of those fields after construction. - -The cache should hold per-entity classification ONLY for entities whose `Id` is not in `_animatedEntities` at lookup time. Animated entities go through today's per-frame classification path unchanged. - ---- - -## Β§1. `entity.MeshRefs = ...` write sites (the core question) - -`WorldEntity.MeshRefs` is `IReadOnlyList` with a `set` accessor (see [src/AcDream.Core/World/WorldEntity.cs:28](../../src/AcDream.Core/World/WorldEntity.cs#L28)). `MeshRef` itself is a `readonly record struct` ([src/AcDream.Core/World/MeshRef.cs:15](../../src/AcDream.Core/World/MeshRef.cs#L15)) β€” its fields cannot be mutated in place. So every "MeshRefs change" is a whole-list replacement, not a per-element edit. - -Six write sites total in `src/`. Five STATIC, one DYNAMIC. - -### Site 1 β€” `OnLiveEntitySpawnedLocked` (server-spawned entity hydration) - -**[src/AcDream.App/Rendering/GameWindow.cs:2578](../../src/AcDream.App/Rendering/GameWindow.cs#L2578)** β€” `MeshRefs = meshRefs` in the `WorldEntity { … }` constructor. - -**Classification:** **STATIC** at first spawn. - -**Trigger:** server's `0xF745 CreateObject` for any entity (NPC, monster, player, item, statue, lifestone). Also re-runs from `OnLiveAppearanceUpdated` (server's `0xF625 ObjDescEvent`) β†’ spawn dedup at top of `OnLiveEntitySpawnedLocked` invokes `RemoveLiveEntityByServerGuid`, then re-spawns. Each invocation gets a NEW local `entity.Id` from `_liveEntityIdCounter++` (line 2573). - -**Implication for cache:** ObjDescEvent isn't a "mutate existing entity" event β€” it's a despawn+respawn pair. The despawn path (next subsection) clears the cache for the old Id; the respawn populates fresh under the new Id. The cache never sees a stale entry for a still-active Id from this path. - -**Pre-construction `parts[…]` mutations** at lines 2333 and 2365 (AnimPartChanges + DIDDegrade resolver) edit the *local* `parts` list before it becomes the `meshRefs` argument; they're not separate write sites. - -### Site 2 β€” `BuildLandblockForStreaming` (stab hydration) - -**[src/AcDream.App/Rendering/GameWindow.cs:4748](../../src/AcDream.App/Rendering/GameWindow.cs#L4748)** β€” `MeshRefs = meshRefs` constructing dat-stab entities. - -**Classification:** **STATIC** at hydration. Worker-thread only. - -**Trigger:** streaming worker's near-tier load path (`LandblockStreamJobKind.LoadNear` or `PromoteToNear`). Single-GfxObj stabs use `Matrix4x4.Identity`; multi-part Setups go through `SetupMesh.Flatten` to produce per-part MeshRefs. - -**Lifetime:** lives until the entity's owning landblock is demoted (Nearβ†’Far) or unloaded β€” see Site invalidation Β§3.2. - -### Site 3 β€” `BuildSceneryEntitiesForStreaming` (procedural scenery) - -**[src/AcDream.App/Rendering/GameWindow.cs:4951](../../src/AcDream.App/Rendering/GameWindow.cs#L4951)** β€” `MeshRefs = meshRefs` for trees / rocks / bushes / fences. - -**Classification:** **STATIC** at hydration. Worker-thread only. - -**Lifetime:** identical to Site 2. - -### Site 4 β€” Interior cell-mesh entity - -**[src/AcDream.App/Rendering/GameWindow.cs:5023](../../src/AcDream.App/Rendering/GameWindow.cs#L5023)** β€” `MeshRefs = new[] { cellMeshRef }` for the EnvCell's own room geometry as a renderable entity. - -**Classification:** **STATIC** at hydration. - -### Site 5 β€” Interior static-object entity - -**[src/AcDream.App/Rendering/GameWindow.cs:5083](../../src/AcDream.App/Rendering/GameWindow.cs#L5083)** β€” `MeshRefs = meshRefs` for static objects placed inside an EnvCell (furniture, fixtures). - -**Classification:** **STATIC** at hydration. - -### Site 6 β€” `TickAnimations` per-frame rebuild - -**[src/AcDream.App/Rendering/GameWindow.cs:7580](../../src/AcDream.App/Rendering/GameWindow.cs#L7580)** β€” `ae.Entity.MeshRefs = newMeshRefs;` after constructing a fresh `List(partCount)` at line 7501 from `sequencer.Advance(dt)` output. - -**Classification:** **DYNAMIC** every frame. - -**Trigger:** per-frame iteration over `_animatedEntities.Values` inside `TickAnimations`. If `entity.Id ∈ _animatedEntities`, this loop runs for that entity every frame (subject to motion-table presence). If `entity.Id βˆ‰ _animatedEntities`, this loop never runs for it. - -**Consequence:** any cache that captures `entity.MeshRefs[i].PartTransform` for an entity in `_animatedEntities` will freeze the pose. **This is exactly what the prior Tier 1 attempt did.** - ---- - -## Β§2. `_animatedEntities` membership transitions - -`_animatedEntities` at [GameWindow.cs:160](../../src/AcDream.App/Rendering/GameWindow.cs#L160) is the gating dict. The cache's "static" predicate is `! _animatedEntities.ContainsKey(entity.Id)`. - -### Population - -- **[GameWindow.cs:2724](../../src/AcDream.App/Rendering/GameWindow.cs#L2724)** β€” `_animatedEntities[entity.Id] = new AnimatedEntity { … }` at server-spawn for entities with a non-empty motion table + a resolvable idle cycle. -- **[GameWindow.cs:7685](../../src/AcDream.App/Rendering/GameWindow.cs#L7685)** β€” `_animatedEntities[pe.Id] = ae;` in `UpdatePlayerAnimation` to *re-add* the local player entity if a prior `UpdateMotion` removed it (the "Phase 6.8 stationary remove" pattern). This is the only path that can flip an entity from STATIC to ANIMATED mid-life. - -### Removal - -- **[GameWindow.cs:2935](../../src/AcDream.App/Rendering/GameWindow.cs#L2935)** β€” `_animatedEntities.Remove(existingEntity.Id)` inside `RemoveLiveEntityByServerGuid`. Fires for `0xF747 DeleteObject` and as the dedup leg of `OnLiveAppearanceUpdated`. - -### Cache implication - -Membership IS NOT cached by the dispatcher. The cache lookup checks `_animatedEntities.ContainsKey(entity.Id)` at lookup time. If the player flips STATICβ†’ANIMATED mid-session (Site 7685 above), a stale cache entry would still exist for the player Id but never be read; the next despawn (Site 2935) clears it. No special-casing needed. - -The reverse flip (ANIMATEDβ†’STATIC, e.g. a ground-state demote) leaves no cache entry; the dispatcher takes the cache-miss path on the first frame and populates fresh. Also no special-casing needed. - ---- - -## Β§3. Position / Rotation write sites (matters for the cached model matrix) - -The dispatcher composes `model = meshRef.PartTransform * entityWorld` for non-Setup entities, and `model = restPose * meshRef.PartTransform * entityWorld` for Setup multi-parts (with `entityWorld = Rotation Γ— Translation`). If `Position` or `Rotation` changes for a STATIC entity, a cached model matrix would be stale. - -Audit shows: **every Position/Rotation write site in `GameWindow.cs` operates on entities that are in `_animatedEntities`.** Static entities never have these fields touched after construction. - -| Line | Context | Animated? | -|---|---|---| -| 3992-3993 | `entity.SetPosition(worldPos); entity.Rotation = rot;` (player physics snap-on-arrival) | YES β€” `entity` is the local player | -| 4116 | `entity.SetPosition(rmState.Body.Position);` (remote dead-reckon snap branch) | YES β€” remote NPC/player | -| 4230 | same context, near-enqueue branch | YES | -| 4362-4363 | remote dead-reckon physics tick body sync | YES | -| 4407-4408 | local player position snap (teleport / GoHome) | YES | -| 7045-7046 | `ae.Entity.SetPosition(rm.Body.Position); ae.Entity.Rotation = rm.Body.Orientation;` (TickAnimations body sync) | YES β€” `ae.Entity` is in `_animatedEntities` by definition | -| 7373-7374 | same body-sync context, fall-through path | YES | - -No Position/Rotation writes happen on entities that are NOT in `_animatedEntities`. Confirmed via grep. - ---- - -## Β§4. Other entity fields read by the dispatcher - -`WbDrawDispatcher.Draw` and `ClassifyBatches` read these `WorldEntity` fields beyond `MeshRefs`, `Position`, `Rotation`: - -| Field | Mutability | Cache impact | -|---|---|---| -| `PaletteOverride` | `init`-only ([WorldEntity.cs:37](../../src/AcDream.Core/World/WorldEntity.cs#L37)) | Stable post-spawn β†’ safe to fold into cache key / texHandle resolution | -| `HiddenPartsMask` | `init`-only ([WorldEntity.cs:73](../../src/AcDream.Core/World/WorldEntity.cs#L73)) | Stable; doesn't apply to dispatcher anyway (animation tick handles part-hide via `s_hidePartIndex` debug global, animated path only) | -| `ParentCellId` | `init`-only ([WorldEntity.cs:45](../../src/AcDream.Core/World/WorldEntity.cs#L45)) | Stable; visibility filter input | -| `AabbMin/AabbMax/AabbDirty` | Mutated lazily by `RefreshAabb` ([WorldEntity.cs:79-91](../../src/AcDream.Core/World/WorldEntity.cs#L79)) on `AabbDirty` flag, set by `SetPosition` | Read by `WalkEntitiesInto`, NOT used by classification. AABB stays static for static entities (Position never changes β†’ never marked dirty after first refresh) | -| `MeshRefs[i].SurfaceOverrides` | `init`-only on the MeshRef record struct | Stable for the lifetime of the MeshRef list (Sites 1-5) | -| `MeshRefs[i].GfxObjId` | Stable (`readonly record struct`) | Forms part of the cache key | -| `MeshRefs[i].PartTransform` | Stable for STATIC entities (the list is replaced atomically in Site 6 only for ANIMATED entities) | Cacheable for STATIC entities | - -No hidden mutability surface. The cache is safe for entities outside `_animatedEntities`. - ---- - -## Β§5. Cache invalidation events (the wire-up) - -The cache is keyed by `entity.Id`. Only TWO event sources can invalidate a cached entry: - -### Β§5.1 Per-entity despawn (live server entities) - -**[GameWindow.cs:2933-2935](../../src/AcDream.App/Rendering/GameWindow.cs#L2933)** β€” `_worldState.RemoveEntityByServerGuid(serverGuid); _worldGameState.RemoveById(...); _animatedEntities.Remove(...);` - -This block fires for: -- `0xF747 DeleteObject` (server explicitly says entity is gone). -- `0xF625 ObjDescEvent` (dedup leg before respawn). - -**Hook:** add `_wbDrawDispatcher.InvalidateEntity(existingEntity.Id)` to this block. - -### Β§5.2 Landblock demote / unload (static dat entities) - -**[src/AcDream.App/Streaming/GpuWorldState.cs:373](../../src/AcDream.App/Streaming/GpuWorldState.cs#L373)** β€” `RemoveEntitiesFromLandblock(landblockId)` clears the entity list for a landblock. Called from `StreamingController.Tick` at [StreamingController.cs:116](../../src/AcDream.App/Streaming/StreamingController.cs#L116) for `ToDemote` (Nearβ†’Far) and via `_enqueueUnload` for `ToUnload`. - -**Hook:** add `_wbDrawDispatcher.InvalidateLandblock(landblockId)` adjacent to the `RemoveEntitiesFromLandblock` call. Walk the LB's pre-removal entity list; invalidate each Id. - -Implementation note: `RemoveEntitiesFromLandblock` already has the entity list in scope before zeroing it β€” adding the invalidation walk inside the method (or via a callback) is cheap. Alternative: `StreamingController` walks the LB's entries before invoking `RemoveEntitiesFromLandblock`. Either works; brainstorming will pick. - -### Β§5.3 No other invalidation paths needed - -Confirmed: -- `MarkPersistent` ([GameWindow.cs:2024](../../src/AcDream.App/Rendering/GameWindow.cs#L2024)) β€” keeps player Id pinned across LB unloads. No MeshRefs change. -- `DrainRescued` ([GameWindow.cs:5885](../../src/AcDream.App/Rendering/GameWindow.cs#L5885)) β€” re-attaches rescued persistent entities. No MeshRefs change. -- `RelocateEntity` ([GameWindow.cs:6026](../../src/AcDream.App/Rendering/GameWindow.cs#L6026)) β€” moves entity between landblocks. Doesn't change MeshRefs/Position/Rotation. Safe. -- `AddEntitiesToExistingLandblock` ([GpuWorldState.cs:401](../../src/AcDream.App/Streaming/GpuWorldState.cs#L401)) β€” Farβ†’Near promotion adds entities. New entries get cache-miss naturally. - -`AnimationSequencer` ([src/AcDream.Core/Physics/AnimationSequencer.cs](../../src/AcDream.Core/Physics/AnimationSequencer.cs)) does NOT write to `entity.MeshRefs` or `entity.Position`/`entity.Rotation` directly. It produces `PartTransform[]` frames consumed by `TickAnimations`. Confirmed via grep β€” only docstring mention of `MeshRef`. Sequencer is safe to ignore for cache design. - -`Core` library has zero `entity.MeshRefs = ...` writes. All writes are in the App layer, all in `GameWindow.cs`. Confirmed via grep. - ---- - -## Β§6. Recommended cache shape (for brainstorming, not yet committed) - -Pre-spec recommendation; final design picks settle in the brainstorming session. - -```csharp -// Per-(entity, partIdx, batchIdx) classification result. -private readonly record struct CachedBatch( - GroupKey Key, // bucket identity - ulong BindlessTextureHandle, // resolved texture (via palette + override) - Matrix4x4 RestPose); // meshRef.PartTransform (or restPose * meshRef.PartTransform for Setup) - -// Per-entity cache value. -private sealed class EntityCache -{ - public List Batches = new(); // ordered: (part, batch) flat - public uint LandblockHint; // for InvalidateLandblock -} - -// Cache state. -private readonly Dictionary _entityCache = new(); - -// Hot path: -// if (_animatedEntities.ContainsKey(entity.Id)) β†’ today's path (full ClassifyBatches) -// else if (_entityCache.TryGetValue(entity.Id, out var cached)) β†’ -// for each batch: append (cached.RestPose * entityWorld) to its group's matrices -// else β†’ ClassifyBatches once, populate cache, then same fast path next frame. -``` - -**Per-frame static cost:** dictionary lookup + per-batch matrix multiply + matrices.Add. No texture resolution, no group-key construction, no metaTable lookup. - -**Worst case:** if every entity is animated (e.g. a city full of NPCs), the cache adds one `ContainsKey` lookup per visible entity vs today's path. Negligible overhead. In practice ~10K entities total at radius=12 with ~50 animated β†’ 99.5% cache hit rate on the static path. - -**Risk surface:** the cache invariant rests on TWO claims, both verified in the audit above: -1. STATIC entity Position / Rotation never mutate post-spawn. Verified Β§3. -2. STATIC entity MeshRefs reference never changes post-spawn. Verified Β§1 (only Site 6 writes, only for animated entities). - -If either claim breaks in a future change (e.g. someone adds an "earthquake" effect that mutates static-tree positions), the cache will quietly serve stale matrices. Defense: -- **DEBUG-only assertion** in the cache hit path: `Debug.Assert(!_animatedEntities.ContainsKey(entity.Id))`. -- **DEBUG-only cross-check**: in DEBUG builds, in the cache-hit path, also recompute the live model matrix and compare against `cached.RestPose * entityWorld`. Log a warning if they differ. Catches the "someone added a new mutation site" failure mode without paying the cost in Release. - -(Belt-and-suspenders option (c) from the original handoff. Recommended for the first retry given the prior bug.) - ---- - -## Β§7. What does NOT need to be in the cache design - -- **Texture invalidation on bindless handle change.** Bindless handles are issued on first texture upload and remain valid for the texture's lifetime. `TextureCache` doesn't evict entries during normal play (only on shutdown). Static-entity texture handles never change. -- **GfxObj re-decode.** `WbMeshAdapter.TryGetRenderData` returns the same `ObjectRenderData` instance for a given `gfxObjId` for the session. Static-entity batches never change. -- **`SurfaceOverrides` reactivation.** Init-only on `MeshRef`, set at Site 1's hydration time, stable for the MeshRef's lifetime. -- **Per-frame `Time` / `dt` inputs.** The dispatcher doesn't read time. Texture animation (e.g. animated UV scrolls) happens in the shader from `gl_Time`-equivalent uniforms, not from cached state. - ---- - -## Β§8. Open questions for brainstorming - -These need a user decision before I write the spec: - -1. **Where do `InvalidateEntity` / `InvalidateLandblock` live?** On `WbDrawDispatcher` (cache lives there)? On a new `EntityClassificationCache` class injected into the dispatcher (separation of concerns; testable in isolation)? My lean: separate class, dispatcher gets it via ctor. -2. **Static-only (option a) vs static-only + DEBUG cross-check (option c)?** Cross-check costs nothing in Release and catches the exact bug class that bit us last time. My lean: option (c). -3. **Cache the full model matrix or the rest pose?** Full matrix saves a per-frame multiply but bakes Position/Rotation into the cache (theoretically violatable). Rest pose is safer + costs ~one mat4 mult per batch. My lean: rest pose. -4. **Setup multi-part flattening: cache the per-part `setupPart.PartTransform * meshRef.PartTransform` product?** Today's `Draw` walks `renderData.SetupParts` per-frame even though that list is per-GfxObj-immutable. The cache could pre-flatten into the batch list. My lean: yes β€” that's where the visible CPU win is. -5. **Test plan: where do new tests live?** `tests/AcDream.Core.Tests/Rendering/Wb/EntityClassificationCacheTests.cs`? Pure-CPU tests on the cache class without GL state? My lean: yes, separate test file in the existing Wb test directory. - ---- - -## Β§9. Sentinel + baseline (verified at audit start, 2026-05-10) - -- `dotnet build`: green (after `git submodule update --init` for the WorldBuilder ref tree, which was missing in this fresh worktree). -- `dotnet test --no-build`: 1688 passing, 8 pre-existing failures in `AcDream.Core.Tests`. Matches the post-#52/#54 baseline in the handoff. -- N.5b sentinel filter (`TerrainSlot|TerrainModernConformance|Wb|MatrixComposition|TextureCacheBindless|SplitFormulaDivergence`): 94/94 passing. Matches the post-#52/#54 baseline. - -These are the floors the Tier 1 retry must keep clean throughout. diff --git a/docs/research/2026-05-10-tier1-retry-handoff.md b/docs/research/2026-05-10-tier1-retry-handoff.md deleted file mode 100644 index 4612468..0000000 --- a/docs/research/2026-05-10-tier1-retry-handoff.md +++ /dev/null @@ -1,203 +0,0 @@ -# Phase Post-A.5 β€” Tier 1 Retry (ISSUE #53) β€” Cold-Start Handoff - -**Created:** 2026-05-10, immediately after closing ISSUES #52 (lifestone) + #54 (JobKind plumbing) and merging to main. -**Audience:** the next agent picking up Priority 3 of the Post-A.5 polish phase. -**Purpose:** drop straight into the Tier 1 entity-classification cache retry without re-litigating what the prior session settled. - ---- - -## TL;DR - -Post-A.5 polish was sized at three priorities. **2 of 3 shipped to main** during the 2026-05-10 session; **only Priority 3 (Tier 1 retry, ISSUE #53) remains.** Tier 1 is the biggest perf headroom in the post-A.5 phase: it should drop the entity dispatcher cpu_us median from ~3.5 ms to ~1-1.5 ms, putting the dispatcher inside the spec's 2.0 ms budget and unlocking ~300-400 FPS at standstill. - -The first Tier 1 attempt (commit `3639a6f`, reverted at `9b49009`) broke animation. The next attempt MUST start with an animation-mutation audit. **This handoff has the audit pre-started** β€” there's specific evidence captured below that the previous handoff didn't have. - -Sized: ~5-7 days including audit + design + spec + implementation + visual gate. - ---- - -## Where main is - -- **`main` HEAD: `da08490`** β€” Merge of `claude/cranky-varahamihira-fe423f`. Includes the lifestone fix + JobKind plumbing. -- **CLAUDE.md "Currently in flight"** updated to *"Post-A.5 polish β€” Tier 1 retry (only remaining priority)"*. -- **`docs/ISSUES.md`** has both #52 and #54 in *Recently closed* with full root-cause writeups; only #53 remains in *Active issues*. -- **N.5b conformance sentinel: 94/94.** Full suite: 1688/1696 passing (8 pre-existing physics/input failures unchanged across all session work). - -Recent commit chain on main (newest first): - -| SHA | Subject | -|---|---| -| `da08490` | Merge branch 'claude/cranky-varahamihira-fe423f' β€” Post-A.5 polish: close #52 (lifestone) + #54 (JobKind plumbing) | -| `9a55354` | docs(post-A.5 #54): close JobKind plumbing issue + update CLAUDE.md flight status | -| `bf31e59` | fix(streaming): close #54 β€” plumb JobKind through BuildLandblockForStreaming | -| `b19f1d1` | docs(post-A.5 #52): close lifestone issue + update CLAUDE.md flight status | -| `e40159f` | fix(render): close #52 β€” lifestone visible (alpha-test + cull + uDrawIDOffset) | -| `c111312` | docs(post-A.5): cold-start handoff for the next session (the prior handoff this work used) | - ---- - -## What shipped this session - -### Priority 1 β€” ISSUE #52 (lifestone missing) β€” closed by `e40159f` - -Three independent root causes regressed with the WB rendering migration (Phase N.5 retirement amendment, commit `dcae2b6`, 2026-05-08): - -1. **Alpha-test discard** in `mesh_modern.frag` transparent pass killed high-Ξ± pixels of dat-flagged transparent surfaces. The lifestone crystal core (surface `0x080011DE`) decoded with Ξ±β‰₯0.95, so 100% of fragments were discarded. Fix: remove `Ξ± >= 0.95 discard` from transparent pass; keep `Ξ± < 0.05 discard` as a fragment-cost optimization. -2. **Cull state regression**: `WbDrawDispatcher.Draw` Phase 8 had no GL cull state β€” Phase 9.2's `Enable(CullFace) + Back + CCW` setup (commit `6f1971a`, 2026-04-11) was lost when the legacy `StaticMeshRenderer` was deleted. Closed-shell translucents composited back-faces over front-faces in iteration order under `DepthMask(false)`. Fix: re-establish Phase 9.2's GL state at the top of Phase 8. -3. **`uDrawIDOffset` indexing bug**: `gl_DrawIDARB` resets to 0 at the start of each `glMultiDrawElementsIndirect`, so the transparent pass was reading `Batches[0..transparentCount)` (the OPAQUE section) instead of `Batches[opaqueCount..end)`. The lifestone flickered to whatever opaque batch sorted to index 0 each frame. Fix: add `uniform int uDrawIDOffset` to `mesh_modern.vert`, set per-pass in dispatcher (0 for opaque, `_opaqueDrawCount` for transparent). Mirrors WB's `BaseObjectRenderManager.cs:845`. - -User-confirmed visually via `+Acdream` test character at the Holtburg outdoor lifestone (Z=94 platform). - -### Priority 2 β€” ISSUE #54 (JobKind plumbing) β€” closed by `bf31e59` - -`LandblockStreamer.cs` primary ctor signature changed from `Func` to `Func`. A back-compat overload preserves the old signature for the 5 ctor sites in `LandblockStreamerTests.cs` (no test changes needed). `BuildLandblockForStreaming(uint, JobKind)` in `GameWindow.cs` early-outs for `LoadFar` with a heightmap-only path. The Bug A post-load entity strip in `LandblockStreamer.HandleJob` is retained as a `Debug.Assert` + Release safety net. - -Per-LB worker cost on far-tier dropped from ~tens of ms (full hydration including `LandBlockInfo` + `SceneryGenerator` + interior cells) to ~sub-ms (single `LandBlock` dat read). - -### Memory entry from this session - -`feedback_wb_migration_state_audit.md` β€” captures the meta-lesson that WB-migration phases need a systematic GL-state and shader-uniform diff vs the legacy renderer being replaced. Future phases at risk: Sky/Particles modern path migration, EnvCell modern path, Shadow mapping. Also captures the workflow lesson: when the user says *"we had this nailed down before"*, the first move is `git log -- ` BEFORE adding new diagnostic instrumentation. - ---- - -## Priority 3 β€” ISSUE #53 β€” Tier 1 entity-classification cache retry - -### What the first attempt was and why it failed - -Commit `3639a6f` (reverted at `9b49009`) cached `meshRef.PartTransform` baked into per-(entity, batch) classification at first-frame visit. For static entities this is stable; for animated entities the cache froze the pose and NPCs/players stopped animating. Some buildings also showed at wrong positions (likely entities incorrectly flagged as animated). - -The "trust MeshRefs as the source of truth" comment in the dispatcher gave false confidence. MeshRefs IS the source of truth, but it's mutated EVERY frame for animated entities. - -### The audit (PRE-STARTED in the prior session β€” read this carefully) - -The previous handoff and ISSUE #53 describe the bug as *"AnimationSequencer mutates `meshRef.PartTransform` every frame to apply the current skeletal pose."* **That framing is technically wrong** in a way that matters for the retry design. Discovered during the post-A.5 lifestone session: - -- `MeshRef` at `src/AcDream.Core/World/MeshRef.cs:15` is a `readonly record struct` β€” its fields **cannot be mutated in place**: - ```csharp - public readonly record struct MeshRef(uint GfxObjId, Matrix4x4 PartTransform) - ``` -- The actual per-frame mutation for animated entities is the **entire `MeshRefs` LIST replacement** at `src/AcDream.App/Rendering/GameWindow.cs:7474-7553`: - ```csharp - var newMeshRefs = new List(partCount); - // ... loop building per-part transforms from sequencer.Advance(dt) ... - ae.Entity.MeshRefs = newMeshRefs; - ``` -- The source of truth for *which* entities go through that per-frame path is the `_animatedEntities` dictionary at `GameWindow.cs:160`: - ```csharp - private readonly Dictionary _animatedEntities = new(); - ``` - Population: `_animatedEntities[entity.Id] = new AnimatedEntity{...}` at GameWindow.cs:2724 (spawn). Removal: `_animatedEntities.Remove(...)` at GameWindow.cs:2935 (despawn). - -**Therefore: a static entity is one whose `Id` is NOT in `_animatedEntities`.** Its MeshRefs list is the same instance from spawn until rare events (ObjDesc / palette swap / part hide). Other static-entity write sites that must be invalidation-aware: -- `src/AcDream.App/Rendering/GameWindow.cs:2333` and `:2365` β€” ObjDescEvent / AnimPartChange events rebuild a `MeshRef` element. Network-driven, infrequent. -- `src/AcDream.App/Rendering/GameWindow.cs:2524` β€” entity scale apply at spawn (one-shot). -- Lines 4682-4924, 4996-5074 β€” dat-side hydration paths in OnLoad / scenery / interior. Spawn-time only. - -### What this means for cache design - -The cleanest design is now clearer than the original handoff suggested: - -**Recommended approach (option a from the original handoff): static-only cache with explicit invalidation hooks.** - -1. Cache the (entity, batch) β†’ InstanceGroup-key + model-matrix mapping for entities where `_animatedEntities.ContainsKey(entity.Id) == false`. -2. Animated entities skip the cache entirely; they go through today's per-frame `ClassifyBatches` path. -3. Invalidate the cache for an entity on: - - **ObjDesc / AnimPartChange events** (`GameWindow.cs:2333, 2365`) β€” rebuild that entity's cache entry. - - **Palette override changes** (rare; usually only on initial server spawn or a re-equip event). - - **Entity despawn** β€” drop the cache entry. -4. Static entities never animate. The dispatcher's per-frame work for cached entities reduces from "walk + classify all batches" to "walk + lookup-and-emit-pre-classified". - -Why this is safer than the first attempt: the first attempt cached the POSE (model matrix). This attempt would cache only the (group key, texture handle, blend mode, per-part `meshRef.PartTransform * entityWorld` for the spawn-time stable subset). Animation never enters the cache surface. - -### Cache design options reconsidered - -(a) **Static-only cache (recommended).** As described above. Clean invariant: animated entities skip the cache; static entities go through it. Requires careful enumeration of all writes to `entity.MeshRefs` for static entities (see audit list above) so each one fires invalidation. - -(b) **Dynamic-aware cache with invalidation hooks.** Cache everything but expose `InvalidateEntity(uint)` / `RefreshEntityPalette(uint)` hooks; wire from network handlers. More complex but might let some animated entities also benefit if their per-frame mutations are localized. NOT RECOMMENDED for a first retry β€” error-prone and the first attempt already failed at this scope. - -(c) **Static-only + animated-bypass + DEBUG cross-check.** Like (a), but in DEBUG builds, log a warning every frame if a cached entity's `MeshRefs` reference no longer matches the cached snapshot (catches mis-classified dynamics). Belt-and-suspenders. Recommended IF you're nervous about the audit being incomplete. - -### Acceptance criteria (from the original handoff, refined) - -- Build green; existing 999+ tests pass; 8 pre-existing physics/input failures stay at 8. -- 1-3 new tests covering: cache hit for static entity (lookup), cache bypass for animated entity (no-op), cache invalidation on entity despawn, cache invalidation on ObjDesc/palette event. -- N.5b conformance sentinel intact (89+ tests; in this session it's 94/94 β€” must stay clean). -- Visual gate: launch + walk Holtburg β†’ North Yanshi at horizon-safe preset; confirm: - - Animation works (NPCs, player character animate normally β€” including the lifestone crystal closed by #52). - - Buildings at correct positions. - - No new visual regressions. -- Perf gate (with `[WB-DIAG]` under `ACDREAM_WB_DIAG=1`): - - Entity dispatcher cpu_us median drops from ~3.5 ms to ≀2.0 ms (matches spec budget). - - p95 stays ≀2.5 ms. - ---- - -## Files to read before brainstorming - -In rough order: - -1. **This handoff** end-to-end β€” captures audit insights from the prior session that the original handoff didn't have. -2. **`docs/research/2026-05-10-post-a5-polish-handoff.md`** β€” the prior handoff. Β§"Priority 3" has the original (slightly outdated) framing of the bug. Read for context but trust THIS handoff's audit insights over its. -3. **`docs/ISSUES.md` issue #53** β€” the issue's own description (now updated post-#52/#54 close). -4. **`docs/superpowers/specs/2026-05-09-phase-a5-two-tier-streaming-design.md`** β€” A.5 spec for the entity dispatcher's data-flow context (esp. Β§4.10 Quality Preset and Β§11 deferred items). -5. **`docs/plans/2026-05-10-perf-tiers-2-3-roadmap.md`** β€” the perf-tier roadmap. Tier 1 is in scope; Tier 2 + Tier 3 are explicitly NOT (those are dedicated multi-week phases). -6. **`memory/feedback_wb_migration_state_audit.md`** β€” the new memory entry on WB migration state-loss patterns. Tier 1 doesn't touch the WB migration directly, but the meta-lesson "audit before assume" is exactly what this priority needs. -7. **`memory/project_phase_a5_state.md`** β€” the 5 gotchas. **Critical for avoiding the same traps**, especially #3 (caching mutable per-frame state breaks animation silently) β€” the exact bug the first Tier 1 attempt hit. -8. **`src/AcDream.Core/World/MeshRef.cs`** β€” confirm the `readonly record struct` shape; understand that "mutating PartTransform" actually means "replacing the whole MeshRef record." -9. **`src/AcDream.App/Rendering/GameWindow.cs:7340-7560`** β€” the per-frame animation rebuild loop. Read this end-to-end for the audit. Find every line that writes to `entity.MeshRefs` for animated entities. -10. **`src/AcDream.App/Rendering/GameWindow.cs:160` + lines 2710-2760, 2920-2940** β€” `_animatedEntities` declaration + spawn/despawn population. -11. **`src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs`** β€” `Draw` and `ClassifyBatches`. Where the cache will land. -12. **`src/AcDream.Core/Physics/AnimationSequencer.cs`** β€” the per-frame animation engine. Audit any field it mutates that the dispatcher reads. -13. **`src/AcDream.Core/Physics/AnimationHookRouter.cs`** β€” secondary mutation source via animation hooks. - ---- - -## Workflow for the next session - -1. **Read this handoff in full.** -2. **Verify build green:** `dotnet build`. Verify ~1688 tests pass: `dotnet test --no-build`. Verify N.5b sentinel: filter `TerrainSlot|TerrainModernConformance|Wb|MatrixComposition|TextureCacheBindless|SplitFormulaDivergence` β†’ expect 94 passing. -3. **Read the files above** in order. Especially deep on Β§"Files to read" #8-#13. -4. **Audit step (1-2 days):** open a fresh research note `docs/research/2026-05-10-tier1-mutation-audit.md` and write down: - - Every code path that writes `entity.MeshRefs = ...` for any entity. - - Tag each as **STATIC** (one-shot at spawn or rare event) or **DYNAMIC** (per-frame). - - For each STATIC write, identify the trigger (network event, scale apply, etc.) and design the invalidation hook. - - For each DYNAMIC write, confirm it fires only for entities in `_animatedEntities` (which means cache bypass is the right answer). -5. **Spec (~1 day):** brainstorm the cache design with the user (use `superpowers:brainstorming`). Write `docs/superpowers/specs/2026-05-10-issue-53-tier1-cache-design.md`. Include the audit findings, the chosen cache approach (probably option (a)), the invariants, the invalidation API, the test plan, the perf-gate measurement plan. -6. **Implement (~2-3 days):** TDD via `superpowers:test-driven-development`. Tests first for cache hit/miss/invalidation, then implementation in `WbDrawDispatcher`. Wire invalidation hooks into the relevant write sites in `GameWindow.cs`. -7. **Visual gate:** launch + walk; confirm animation works on a moving NPC; confirm static buildings/scenery still render at correct positions; confirm lifestone (closed by #52) still renders. -8. **Perf gate:** capture `[WB-DIAG]` cpu_us median + p95 with `ACDREAM_WB_DIAG=1` at horizon-safe preset (NEAR=4, FAR=12). Compare to today's ~3.5 ms baseline; expect ≀2.0 ms. -9. **Ship:** commit, close #53 in ISSUES.md, update CLAUDE.md "Currently in flight" (this would close out the post-A.5 polish phase entirely), update memory with any new gotchas captured during the audit/implementation. -10. **Next phase after #53 ships:** N.6 (perf polish) per the roadmap. Or escalate to Tier 2 (static/dynamic split with persistent groups) per `docs/plans/2026-05-10-perf-tiers-2-3-roadmap.md` if Tier 1 alone doesn't hit the perf target. - ---- - -## Things to NOT do - -- **Don't skip the audit.** The whole reason the first attempt failed was that the audit was implicit and incomplete. The audit step should produce a written list of every MeshRefs write site, classified static vs dynamic, before any cache code is written. -- **Don't bundle Tier 2 or Tier 3 into this phase.** Those are dedicated multi-week phases per `docs/plans/2026-05-10-perf-tiers-2-3-roadmap.md`. If the audit reveals Tier 1 alone can't hit the perf target, file a follow-up issue and escalate as a separate phase. -- **Don't re-add the `Tier1` cache that was reverted.** Start fresh after the audit. Cherry-picking commit `3639a6f` reintroduces the animation freeze. -- **Don't break the N.5b conformance sentinel.** Run the filter on every commit: - ``` - dotnet test --no-build --filter "FullyQualifiedName~TerrainSlot|FullyQualifiedName~TerrainModernConformance|FullyQualifiedName~Wb|FullyQualifiedName~MatrixComposition|FullyQualifiedName~TextureCacheBindless|FullyQualifiedName~SplitFormulaDivergence" - ``` - Expect 94 passing, 0 failures. -- **Don't skip the visual gate.** Animation has been the highest-risk regression in this codebase repeatedly (Tier 1 first attempt, the lifestone crystal in this session, the foundry statue earlier). Confirm visually with a moving animated NPC, a stationary building, and the lifestone before declaring done. -- **Don't trust "it was working in prod before."** That was the first Tier 1 attempt's posture. The audit is what makes it actually safe. - ---- - -## Reference: Tier 1 perf math - -Per the perf-tier roadmap and A.5 final state: -- **Today** (post-A.5 ship + #52/#54): entity dispatcher cpu_us median ~3.5 ms at radius=12 on Radeon RX 9070 XT @ 1440p. ~200-240 FPS at standstill. -- **After Tier 1**: ~1.0-1.5 ms median expected. ~300-400 FPS at standstill. Inside the spec's 2.0 ms budget. -- **After Tier 2 (separate phase)**: ~0.5-1.0 ms. ~400-600 FPS. -- **After Tier 3 (GPU compute culling, separate phase)**: ~0.05 ms. ~600-1000+ FPS. - -Tier 1 is the lowest-risk, highest-leverage perf win remaining for the post-A.5 polish phase. - ---- - -Good luck. The audit is the load-bearing thing β€” invest in it. The implementation is mechanical once the audit is solid. - -Holler at the user if any of the audit reveals a write site that doesn't fit the static/dynamic dichotomy cleanly. diff --git a/docs/research/2026-05-12-l2a-shipped-l2d-handoff.md b/docs/research/2026-05-12-l2a-shipped-l2d-handoff.md deleted file mode 100644 index 9ecfcaa..0000000 --- a/docs/research/2026-05-12-l2a-shipped-l2d-handoff.md +++ /dev/null @@ -1,176 +0,0 @@ -# L.2a shipped β€” L.2d direction confirmed β€” Cold-Start Handoff - -**Created:** 2026-05-12 evening, immediately after the L.2a-slice-1/2/3 work landed and visual-verified. -**Audience:** the next agent picking up Phase L.2 (Movement & Collision Conformance). -**Purpose:** give you everything you need to start L.2d brainstorming cold, without spelunking through this session's transcript. - ---- - -## TL;DR - -Phase L.2a (Truth & Diagnostics) shipped three slices tonight. They surfaced **three concrete L.2 findings** with reproducible evidence β€” converting "we should look at this someday" theories into "here is the entity id, here is the wall normal, here is the cell id." With those findings in hand, the next concrete physics work in the L.2 roadmap is **L.2d slice 1 β€” port `CBuildingObj` collision so doorway gaps are walkable.** Brainstorm + spec, then port. - -**Three slices shipped to `claude/intelligent-poitras-b2c4f9`:** - -| Commit | What | Why | -|---|---|---| -| [`ebef820`](.) | L.2a slice 1: `[resolve]` + `[cell-transit]` probes + DebugPanel mirror | Foundation for every later L.2 change to be evidence-driven | -| [`e0c08bc`](.) | L.2a slice 2: surface hit object guid in `[resolve]` line | Tell us WHICH entity is the wall, not just the wall normal | -| [`a068292`](.) | L.2a slice 3: populate the previously-stub `CollisionInfo.CollideObjectGuids` / `LastCollidedObjectGuid` | Slice 2 found these fields were declared but never written β€” fixed the structural gap | - ---- - -## Three findings from the L.2a probes - -All produced by walking around Holtburg + pushing W into a Town doorway with `ACDREAM_PROBE_RESOLVE=1 ACDREAM_PROBE_CELL=1`. - -### Finding 1 β€” L.2e cell-id format gap (DEFINITIVE) - -The player's tracked `CellId` is being recorded as a **bare low byte** (`0x00000029`), with no landblock prefix. AC cell ids are normally `0xLLLLCCCC` β€” landblock id (4 hex digits) + cell-within-landblock (4 hex digits, `0x0001-0x00FF` outdoor or `0x0100+` indoor). - -Evidence from a tonight log: - -``` -[cell-transit] 0x00000001 -> 0x00000029 pos=(132.585,21.015,94.000) reason=resolver -``` - -NPCs in the same area show MIXED forms in their resolve lines: -- `cell=0xA9B3000E` ← full landblock-prefixed (correct) -- `cell=0x00000032` ← bare low byte (matches the bug shape) - -Likely source: `ResolveOutdoorCellId(...)` at [src/AcDream.Core/Physics/PhysicsEngine.cs:687](src/AcDream.Core/Physics/PhysicsEngine.cs:687) β€” that's the function that ResolveWithTransition routes the output cell id through before returning. Worth grepping for its body. - -This is the L.2e blocker per the plan-of-record: -> *"Update low outdoor cell id across 24m cell boundaries and landblock seams. Port the retail adjacent-cell search: `find_cell_list`, `check_other_cells`, and `adjust_check_pos`."* - -### Finding 2 β€” L.2c wall-slide is working - -The transition layer at this spot does the retail-faithful thing: - -``` -[resolve] ent=0x000F4240 in=(132.067,17.567,94.000) cell=0x00000029 - tgt=(132.239,17.172,94.000) - out=(131.938,17.567,94.000) cell=0x00000029 - ok=True groundedIn=True cp=valid hit=yes n=(0.00,1.00,0.00) - obj=0xA9B47900 walkable=True -``` - -- Wall normal `(0, 1, 0)` β€” vertical wall facing +Y, captured correctly. -- `out` shows the position clamped along the wall: X slid back from 132.067 β†’ 131.938, Y preserved. -- `ok=True` β€” resolver completed normally (no `ok=False` anywhere in the trace, 0/140). - -**No L.2c work needed at this site.** Edge-slide / wall-slide port from earlier (per the plan-of-record's L.2c "Current shipped slice" note) is doing its job here. - -### Finding 3 β€” L.2d sub-direction = CBuildingObj port (NOT door-toggle) - -All 140 hit=yes lines in the doorway-push test came back with the **same dominant `obj=` attribution**: - -| obj | hits | range | what it is | -|---|---|---|---| -| **`0xA9B47900`** | **126** | `0xLLLLxxxx` (landblock-baked static) | The Holtburg building itself β€” its baked collision mesh | -| `0x000F4245` | 14 | `0x000Fxxxx` (local-spawn entity) | An NPC standing near the doorway | - -`0xA9B4` matches the Holtburg landblock prefix we logged at startup (`loading world view centered on 0xA9B4FFFF`). The `0x7900` low bytes is its landblock-local entity id. **It's the building's baked collision shape β€” not a door entity, not a creature.** - -**Implication:** the "doorway is blocked" symptom is NOT a door-collision-not-toggled bug (which would have shown a door-range entity id, typically `0xCC0Cxxxx`). It's a **building-mesh fidelity issue**: the building's baked collision data we're loading represents the building as a solid block with no walkable opening where the visual doorway is. - -Two non-mutually-exclusive interpretations: -1. **Collision-mesh extraction is wrong** β€” we load building geometry but don't respect the BSP nodes that encode doorway openings. -2. **`CBuildingObj` + per-cell walkability is not ported** β€” retail uses a per-cell `CObjCell` structure that maps "this interior cell is reachable" / "this exterior cell connects to those interior cells." Without that, we treat the building as one opaque collision volume. - -The plan-of-record's L.2d goal: -> *"Preserve enough building identity to model `CBuildingObj` collision and `bldg_check` behavior."* - -points at interpretation 2 as the canonical fix. - ---- - -## What this session deliberately did NOT do - -- **Other L.2a slices** (contact-plane probe, ShadowObject hit log, water probe, real-DAT fixture-capture pipeline). Slice 1 + 2 + 3 cover the most-load-bearing case (resolver outcomes + cell transits + entity attribution). The remaining diagnostics serve future L.2 work and can ship opportunistically. -- **L.2d implementation or brainstorm.** Deliberately parked for a fresh session with this evidence as cold-start context. -- **L.2e implementation.** The cell-id format finding is filed but not investigated. -- **Pre-existing test failures.** 8 tests fail at the branch base (none from these slices β€” verified by stash + rerun on every test cycle). Not from this slice. See "Open concerns" below. - ---- - -## Branch state at handoff - -- Branch: `claude/intelligent-poitras-b2c4f9` -- Three slice commits ahead of `eab347d` (the C.1.5b merge into main), plus a docs commit that adds this handoff + the next-session prompt + plan-of-record / CLAUDE.md updates. -- Tonight's last code commit was `a068292` (L.2a slice 3); docs commit follows. -- Worktree clean post-docs-commit; merge to main is the user's planned next operation. - -## What's now in the diagnostic surface - -Live env vars (both can be flipped at runtime via the DebugPanel "Diagnostics" section if `ACDREAM_DEVTOOLS=1`): - -- **`ACDREAM_PROBE_RESOLVE=1`** β€” one `[resolve]` line per `PhysicsEngine.ResolveWithTransition` call: - ``` - [resolve] ent=0xEEEEEEEE in=(x,y,z) cell=0xCCCCCCCC tgt=(x,y,z) out=(x,y,z) cell=0xCCCCCCCC ok=Y/N groundedIn=Y/N cp=valid|lastKnown|none hit=yes n=(nx,ny,nz) obj=0xOOOOOOOO env nObj=N walkable=Y/N - ``` - Heavy: fires for every entity's resolve per physics tick. -- **`ACDREAM_PROBE_CELL=1`** β€” one `[cell-transit]` line per `PlayerMovementController.CellId` change: - ``` - [cell-transit] 0xOLD -> 0xNEW pos=(x,y,z) reason=resolver|teleport - ``` - Low volume β€” only fires on actual cell crossings. - -Both backed by `AcDream.Core.Physics.PhysicsDiagnostics` static class (initial from env var, set/get from anywhere at runtime). - -## Files changed in this session - -``` -src/AcDream.Core/Physics/PhysicsDiagnostics.cs (new) -src/AcDream.Core/Physics/PhysicsEngine.cs (modified β€” probe emission) -src/AcDream.Core/Physics/TransitionTypes.cs (modified β€” entity attribution plumbing) -src/AcDream.App/Input/PlayerMovementController.cs (modified β€” UpdateCellId chokepoint) -src/AcDream.UI.Abstractions/Panels/Debug/DebugVM.cs (modified β€” Probe* forwarder props) -src/AcDream.UI.Abstractions/Panels/Debug/DebugPanel.cs (modified β€” two new checkboxes) -docs/plans/2026-04-29-movement-collision-conformance.md (modified β€” shipped-slice note + L.2d sub-direction) -``` - -## Open concerns flagged but NOT addressed in this session - -- **8 pre-existing test failures** on the branch base, verified by stash+rerun: `MotionInterpreterTests.GetMaxSpeed_*` (3), `PositionManagerTests.ComputeOffset_BothActive_Combined`, `PlayerMovementControllerTests.Update_ForwardInput_MovesInFacingDirection`, `DispatcherToMovementIntegrationTests.Dispatcher_W_held_produces_forward_motion`, `BSPStepUpTests.{D4_AirborneMover_TallWall_PersistsSlidingNormalAcrossFrames,C3_Path6_AirborneMoverHitsSteepSlope_SetsCollide}`. Most touch movement/physics code we're about to evolve in L.2b/L.2c/L.2d β€” **triage before further L.2 work** is recommended. -- **Player entity id quirk.** Local player physics entity id observed as `0x000F4240` in the resolve probe, not the server guid `0x5000000A`. This is presumably the dat/local-spawn entity id β€” fine for diagnostic, worth keeping in mind for any future "is this the player?" check. - -## Cold-start checklist for L.2d brainstorm - -1. Read this handoff. -2. Read [docs/plans/2026-04-29-movement-collision-conformance.md](docs/plans/2026-04-29-movement-collision-conformance.md) β€” focus on L.2d section. -3. Read the L.2d named-retail anchors: - - `CCellStruct::point_in_cell`, `CCellStruct::sphere_intersects_cell`, `CCellStruct::box_intersects_cell` - - `CBuildingObj::find_building_collisions` - - `CObjCell::find_cell_list` (already shared with L.2e) - Grep `docs/research/named-retail/acclient_2013_pseudo_c.txt` by `class::method`. -4. Read [src/AcDream.Core/Physics/TransitionTypes.cs:1386](src/AcDream.Core/Physics/TransitionTypes.cs:1386) β€” current `FindObjCollisions` loop, where building objects currently route through generic BSP/Cylinder paths. -5. Read [src/AcDream.Core/Physics/PhysicsDataCache.cs](src/AcDream.Core/Physics/PhysicsDataCache.cs) β€” how we currently load BSP / GfxObj data; figure out if building-specific data (interior cells, `CBuildingObj`) is loaded but not consumed. -6. Cross-reference WorldBuilder (`references/WorldBuilder/`) for any building-cell handling already present. -7. Brainstorm the slice (`superpowers:brainstorming` if useful) β€” scope, named-retail anchors, conformance tests, real-DAT fixtures. -8. Write a spec at `docs/superpowers/specs/2026-05-13-l2d-cbuildingobj-collision-design.md`. -9. Implement in slices with conformance citations in each commit. - -## Reproducing the doorway evidence - -In case you want to re-capture the trace: - -```powershell -# In the project worktree -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -$env:ACDREAM_TEST_HOST = "127.0.0.1" -$env:ACDREAM_TEST_PORT = "9000" -$env:ACDREAM_TEST_USER = "testaccount" -$env:ACDREAM_TEST_PASS = "testpassword" -$env:ACDREAM_DEVTOOLS = "1" -$env:ACDREAM_PROBE_CELL = "1" -$env:ACDREAM_PROBE_RESOLVE = "1" -dotnet run --project src\AcDream.App\AcDream.App.csproj --no-build -c Debug 2>&1 | Tee-Object -FilePath "launch.log" -``` - -Walk acdream up to a Holtburg building doorway. Hold W into it for ~2 seconds. Close. Grep `launch.log` for: -- `cell-transit` β€” cell tracking -- `\[resolve\].*hit=yes` β€” wall hits with object attribution - -Wall entity should appear as `obj=0xA9B47XXX` for the same Holtburg building, OR a different `0xA9Bxxxxx` for other buildings in the area. diff --git a/docs/research/2026-05-12-l2d-next-session-prompt.md b/docs/research/2026-05-12-l2d-next-session-prompt.md deleted file mode 100644 index bcb7395..0000000 --- a/docs/research/2026-05-12-l2d-next-session-prompt.md +++ /dev/null @@ -1,51 +0,0 @@ -# Copy-paste prompt β€” next session for L.2d brainstorm - -**This file is meant to be pasted verbatim into a new Claude Code session.** It assumes the next session starts on a freshly-merged `main` with the L.2a-slice-1/2/3 work already landed. - ---- - -## Prompt to paste - -> You are picking up Phase L.2d (Movement & Collision Conformance β€” Shape Fidelity: Sphere / CylSphere / Building Objects) for the acdream project. -> -> The previous session shipped L.2a-slice-1/2/3 (resolver + cell-transit probes + entity attribution plumbing) and used the probes to settle the L.2d sub-direction call: **the wall blocking us at building doorways is a landblock-baked static (`0xA9B47900` for the Holtburg test building), NOT a door entity.** The fix is to port `CBuildingObj` + per-cell walkability so the building's baked collision mesh has walkable openings where doorways are. Door-state-toggle is NOT the issue. -> -> Before writing any code: -> -> 1. **Read the handoff:** `docs/research/2026-05-12-l2a-shipped-l2d-handoff.md` β€” full context, evidence, file pointers. -> 2. **Read the plan-of-record:** `docs/plans/2026-04-29-movement-collision-conformance.md` β€” focus on L.2d, and notice that L.2c already shipped most of its work + L.2a is now ~75% covered. -> 3. **Read the named-retail anchors** (grep `docs/research/named-retail/acclient_2013_pseudo_c.txt` by `class::method`): -> - `CCellStruct::point_in_cell` -> - `CCellStruct::sphere_intersects_cell` -> - `CCellStruct::box_intersects_cell` -> - `CBuildingObj::find_building_collisions` -> - `CObjCell::find_cell_list` -> 4. **Read current code:** -> - `src/AcDream.Core/Physics/TransitionTypes.cs:1386` β€” `FindObjCollisions` (where building objects currently flow through generic BSP path). -> - `src/AcDream.Core/Physics/PhysicsDataCache.cs` β€” what building-specific data we already load vs ignore. -> 5. **Cross-reference WorldBuilder** at `references/WorldBuilder/` for any building-cell handling we can crib. -> -> Your deliverable for this session: -> -> 1. A brainstorm using `superpowers:brainstorming` if scope is unclear, then -> 2. A design spec at `docs/superpowers/specs/2026-05-13-l2d-cbuildingobj-collision-design.md` covering: -> - Named-retail anchors with line numbers from the PDB pseudo-C -> - Component breakdown (CObjCell port, CBuildingObj port, integration with FindObjCollisions) -> - Conformance test plan (synthetic + real-DAT fixtures at known Holtburg buildings) -> - Slice plan (3-5 commits, each conformance-cited) -> - Acceptance criteria -> 3. After spec approval, implement slice 1. -> -> **Before implementation,** verify the L.2a probes still work β€” relaunch with `ACDREAM_PROBE_RESOLVE=1 ACDREAM_PROBE_CELL=1 ACDREAM_DEVTOOLS=1`, walk up to the Holtburg test doorway, confirm `[resolve]` lines still show `obj=0xA9B4xxxx` for the wall hits. (Reproduction recipe in the handoff doc's last section.) -> -> Side note: **8 pre-existing test failures** exist on main (verified by stash+rerun in the prior session, none from L.2a slice work). Most touch movement/physics code we're about to evolve. **Triage them before sinking deep L.2d effort** β€” a recent baseline regression in this area could waste hours of L.2d work. - ---- - -## Reading order if you only have 10 minutes - -1. `docs/research/2026-05-12-l2a-shipped-l2d-handoff.md` β€” TL;DR + Three findings sections (5 min). -2. `docs/plans/2026-04-29-movement-collision-conformance.md` Β§L.2d (2 min). -3. `src/AcDream.Core/Physics/TransitionTypes.cs:1386-1543` β€” current `FindObjCollisions` body (3 min). - -From there, decide whether to brainstorm or jump straight to the spec. diff --git a/docs/research/2026-05-12-l2g-slice1-shipped-handoff.md b/docs/research/2026-05-12-l2g-slice1-shipped-handoff.md deleted file mode 100644 index 8a682cd..0000000 --- a/docs/research/2026-05-12-l2g-slice1-shipped-handoff.md +++ /dev/null @@ -1,241 +0,0 @@ -# L.2g slice 1 shipped β€” handoff (code-complete; visual test deferred) - -**Date:** 2026-05-12 evening. -**Branch:** `claude/gallant-mestorf-3bf2e3` (ready to merge to main). -**Predecessors:** -- [2026-05-13-l2d-slice1-shipped-handoff.md](2026-05-13-l2d-slice1-shipped-handoff.md) β€” the L.2d trace that identified Door entities as the Holtburg doorway blocker, motivating L.2g. -- [docs/superpowers/specs/2026-05-12-l2g-dynamic-physicsstate-design.md](../superpowers/specs/2026-05-12-l2g-dynamic-physicsstate-design.md) β€” the L.2g design spec (commit `2c10dd4`). -- [docs/superpowers/plans/2026-05-12-phase-l2g-slice1.md](../superpowers/plans/2026-05-12-phase-l2g-slice1.md) β€” the L.2g slice 1 implementation plan (commit `869677b`). - ---- - -## TL;DR - -L.2g slice 1 **code is complete and unit-tested.** The four commits land -the full inbound `SetState (0xF74B)` pipeline: parser β†’ WorldSession -event β†’ GameWindow handler β†’ `ShadowObjectRegistry.UpdatePhysicsState`. -After this slice, the existing `CollisionExemption.ShouldSkip` -short-circuit (cited at `acclient_2013_pseudo_c.txt:276782`) honors -runtime ETHEREAL flips without any resolver-path edit. - -**The visual verification at Holtburg's inn doorway is deferred to the -next session.** Cause: Phase B.4's outbound Use handler turns out to be -unwired β€” clicking on a door silently does nothing because no -production code subscribes to the `SelectLeft` / `SelectDblLeft` input -actions. Without the outbound Use, the server never sees a "open the -door" request, so the inbound SetState we just ported never fires. - -L.2g slice 1 is the inbound half of the round-trip. Phase **B.4b** (a -small ~30-50 LOC slice) is the outbound half. Both halves are required -for the M1 demo target *"open the inn door."* B.4b is the next session's -work. - ---- - -## What shipped on this branch - -| Commit | Subject | -|---|---| -| [`2459f28`](.) | `feat(phys L.2g slice 1): inbound SetState (0xF74B) parser` | -| [`d538915`](.) | `feat(phys L.2g slice 1): ShadowObjectRegistry.UpdatePhysicsState` | -| [`536a608`](.) | `feat(phys L.2g slice 1): WorldSession dispatches SetState (0xF74B) + hex probe` | -| [`108e386`](.) | `feat(phys L.2g slice 1): GameWindow routes SetState + extends [entity-source] log` | - -Plus docs/scaffolding earlier in the session: -- `2c10dd4` β€” L.2g design spec + L.2 plan-of-record + milestones + CLAUDE.md updates. -- `869677b` β€” L.2g slice 1 implementation plan (this doc's companion). - -**Build:** clean. **Tests:** 6 new tests pass (3 for parser, 3 for -registry mutator). Full suite: 1037 pass / 8 pre-existing-baseline fail. -No regressions. Per-commit + final integration code reviews all approved. - ---- - -## What the code now does end-to-end - -When the server broadcasts a `SetState (0xF74B)`: - -1. **Parse** β€” `WorldSession`'s dispatcher routes opcode `0xF74B` into - `SetState.TryParse(body)`, which returns - `SetState.Parsed(Guid, PhysicsState, InstanceSequence, StateSequence)`. -2. **Probe** (gated on `ACDREAM_PROBE_BUILDING=1`) β€” one-shot per - session, dumps the first message's body bytes as - `[setstate-hex] body.len=N first-N-bytes: 4B F7 ...` for wire-format - confidence. -3. **Event** β€” `WorldSession.StateUpdated` fires with the parsed value. -4. **Subscribe** β€” `GameWindow.OnLiveStateUpdated` (added to the live- - session attach block alongside `OnLiveVectorUpdated`) calls - `_physicsEngine.ShadowObjects.UpdatePhysicsState(parsed.Guid, parsed.PhysicsState)`. -5. **Mutate** β€” `ShadowObjectRegistry.UpdatePhysicsState` walks every - per-cell list the entity occupies and rewrites `list[i] with { State = newState }`. -6. **Per-tick diagnostic** (same probe flag) β€” emits - `[setstate] guid=0x... state=0x... instSeq=... stateSeq=...` for the - greppable trail. -7. **Resolver** β€” next physics tick, `FindObjCollisions` calls - `CollisionExemption.ShouldSkip(entry.State, entry.Flags, moverState)` - on the entity. The check is unchanged from L.2d slice 1; it - short-circuits when `(state & ETHEREAL_PS) != 0 && (state & IGNORE_COLLISIONS_PS) != 0`. - -**Slice 0.5 freebie folded in:** all 6 `[entity-source]` probe-log -sites in `GameWindow.cs` now emit `state=0x{state:X8} flags={flags}` -so ETHEREAL flips are greppable end-to-end from spawn through state -change. - ---- - -## Why the visual test is deferred β€” the B.4 discovery - -Before launching the visual test, the user reported that right-click -in-client was bound to camera orbit (correctly), and asked whether -left-click should open a door. Investigation produced this finding: - -| Component | State | -|---|---| -| `InteractRequests.BuildUse(seq, guid)` wire builder | βœ… implemented + tested | -| `SelectionState`, `WorldPicker` classes | βœ… exist in source | -| `InputAction.SelectLeft` / `SelectDblLeft` / `SelectRight` enum | βœ… defined | -| KeyBindings: LMB β†’ `SelectLeft`, LMB-dblclick β†’ `SelectDblLeft`, RMB β†’ `SelectRight` | βœ… wired in `KeyBindings.cs:300-320` | -| `GameWindow.OnInputAction` switch case for `Select*` | ❌ **missing** | -| Any production caller of `SelectionState`, `WorldPicker`, `InteractRequests.BuildUse` | ❌ **none in `src/`** | - -The diagnostic line `[input] SelectLeft Press` fires on LMB-click β€” the -dispatcher knows the action β€” but nothing downstream listens. The -click silently does nothing. The R hotkey similarly does nothing -because the corresponding `UseSelected` case is also absent from the -switch. - -So the M1 outbound Use path is **half-shipped**: every component below -the handler exists, but the handler that ties them together was never -landed (despite a 2026-04-28 memory entry claiming "B.4 shipped"). -Phase B.4b is the slice that fixes this. - -This is **not** an L.2g defect. L.2g's code path is correct and unit- -tested; it just can't be exercised at runtime until the outbound Use -sends a SetState-triggering request to the server. - ---- - -## Open notes from reviews (minor β€” defer to next polish pass) - -The per-commit and final integration code reviews approved every commit. -Four observations flagged as Minor that are worth folding into a future -polish pass: - -1. **`SetState.cs` "total body size" phrasing diverges from `VectorUpdate.cs`.** - New form: `"Total body size: 16 bytes (4-byte opcode + 12-byte payload)"`. - Sibling form: `"Total body size after opcode: 32 bytes"`. The new form - is more self-documenting, but the spec asked to align with the - sibling. Cosmetic. -2. **`[setstate-hex]` log uses redundant `Math.Min(body.Length, 32)`.** - Called twice in the same line; could be hoisted to a local. - Harmless for a one-shot diagnostic. -3. **`WorldSession.cs` uses the fully-qualified - `AcDream.Core.Physics.PhysicsDiagnostics.ProbeBuildingEnabled`** instead - of adding `using AcDream.Core.Physics;` and using the short form. - Every other call site in `GameWindow.cs` and `BSPQuery.cs` uses the - unqualified form. Style inconsistency. -4. **`[setstate]` diagnostic emits guid + state as hex but instSeq + - stateSeq as decimal.** Cosmetic. - -### One Important review note (worth following up explicitly) - -The final integration reviewer flagged: the test -`UpdatePhysicsState_FlipsEthereal_NextLookupSeesNewBits` asserts the -cached state changes to `0x4` but does **not** verify the chain -through `CollisionExemption.ShouldSkip`. That short-circuit requires -**both** `ETHEREAL_PS (0x4)` AND `IGNORE_COLLISIONS_PS (0x10)` to be -set simultaneously (`(state & 0x4) && (state & 0x10)`). A state of -`0x4` alone does NOT exempt collision. Per the reviewer, ACE's -`PhysicsObj.cs:787-791` may set both bits when doors open (broadcast -value `0x14` or higher) β€” but this is not verified by the test suite. - -**The B.4b visual test will settle this definitively:** the slice-1 -hex-dump probe will capture the real `state=0x????????` wire value the -first time a door opens. If ACE sends `0x14` or higher, the existing -chain works as-is. If ACE sends `0x4` only, we need a tiny adjustment -to `CollisionExemption.cs` (the `&&` would become `||`, OR we make the -collision exemption fire on ETHEREAL alone, OR we widen the test). - -**Action for B.4b session:** after the door-open visual test, grep the -launch log for `[setstate-hex]` and the `[setstate]` line that fires on -the Use β†’ confirm the state bits ACE actually sends. If `0x4` only, -file a tiny L.2g slice 1b to widen `CollisionExemption.ShouldSkip` or -the test's assertion. - ---- - -## Next session - -**Pick: Phase B.4b β€” finish the outbound Use handler wiring.** - -Concretely: -- Subscribe `InputAction.SelectDblLeft` in `GameWindow.OnInputAction` - switch. -- Build a world ray from current mouse position - (`WorldPicker.BuildRay(mouse, vp, view, proj)`). -- Pick the closest entity (`WorldPicker.Pick(ray, entities, cache, skipGuid, maxDist)`). -- Store result in `_selection` (`SelectionState.Set(guid)`). -- Call `InteractRequests.BuildUse(seq, guid)` + `_liveSession.SendGameMessage(body)`. -- Probably also subscribe `InputAction.SelectLeft` for select-without- - use (single-click selects; double-click selects + uses). -- Optionally subscribe `InputAction.UseSelected` (R hotkey) to send Use - on the already-selected guid. -- Sequence-number management β€” there's a game-action sequence counter - on `WorldSession` already used by the outbound chat path; reuse it. - -Estimate: 30-50 LOC, 1-2 subagent-driven implementations + reviews, ~30 min. - -Once B.4b lands, **immediately re-run the Holtburg inn doorway visual -test** with `ACDREAM_PROBE_BUILDING=1`. Both L.2g slice 1 + B.4b are -verified by the same scenario; no separate L.2g visual test needed. - ---- - -## Reproducibility - -Same launch recipe as L.2d slice 1 (see CLAUDE.md "Running the client -against the live server"). For visual test once B.4b lands: - -```powershell -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -$env:ACDREAM_TEST_HOST = "127.0.0.1" -$env:ACDREAM_TEST_PORT = "9000" -$env:ACDREAM_TEST_USER = "testaccount" -$env:ACDREAM_TEST_PASS = "testpassword" -$env:ACDREAM_DEVTOOLS = "1" -$env:ACDREAM_PROBE_BUILDING = "1" -$env:ACDREAM_PROBE_RESOLVE = "1" -dotnet run --project src\AcDream.App\AcDream.App.csproj -c Debug 2>&1 | - Tee-Object -FilePath "launch-l2g+b4b.log" -``` - -Then walk into the Holtburg inn doorway, double-left-click the door, -wait for the swing animation, walk through. After 30s, watch the -auto-close fire. - -After closing the client: - -```powershell -Select-String -Path launch-l2g+b4b.log -Pattern "setstate-hex|setstate.*guid|entity-source.*Door|input.*SelectDblLeft" -``` - -Expected matches: -- One `[setstate-hex] body.len=16 ...` line (confirms holtburger's 12-byte payload). -- One `[entity-source] name=Door ... state=0x00000000 flags=None ...` at spawn. -- An `[input] SelectDblLeft Press` when you double-click. -- A `[setstate] guid=0x000F... state=0x????????` after the door opens. -- A second `[setstate] guid=0x000F... state=0x00000000` ~30s later when auto-close fires. - ---- - -## Worktree state at handoff - -- Branch `claude/gallant-mestorf-3bf2e3` ready to merge to main. -- 6 commits ahead of main: `2c10dd4` (spec + docs), `869677b` (plan), - `2459f28` / `d538915` / `536a608` / `108e386` (L.2g slice 1 code). -- One launch.log artifact (`launch-l2g-slice1.log`) in the working - tree from the attempted visual test β€” **not committed** (gitignored - or transient). Safe to discard; B.4b will produce a fresh log. - -User wants to start a fresh session for B.4b. diff --git a/docs/research/2026-05-13-b4b-shipped-handoff.md b/docs/research/2026-05-13-b4b-shipped-handoff.md deleted file mode 100644 index ea4c8a0..0000000 --- a/docs/research/2026-05-13-b4b-shipped-handoff.md +++ /dev/null @@ -1,417 +0,0 @@ -# Phase B.4b shipped β€” handoff (visual-verified 2026-05-13) - -**Date:** 2026-05-13. -**Branch:** `claude/compassionate-wilson-23ff99` (ready to merge to main; do NOT merge here β€” controller handles that after code review). -**Predecessors:** -- [docs/research/2026-05-12-l2g-slice1-shipped-handoff.md](2026-05-12-l2g-slice1-shipped-handoff.md) β€” L.2g slice 1 ship handoff that discovered the B.4 handler gap and deferred the Holtburg visual test to B.4b. -- [docs/superpowers/specs/2026-05-13-phase-b4b-design.md](../superpowers/specs/2026-05-13-phase-b4b-design.md) β€” B.4b design spec. -- [docs/superpowers/plans/2026-05-13-phase-b4b-plan.md](../superpowers/plans/2026-05-13-phase-b4b-plan.md) β€” B.4b implementation plan (6 tasks; Tasks 1-4 per plan + 2 bonus sets beyond the plan). - ---- - -## TL;DR - -Phase B.4b **shipped end-to-end and is visual-verified 2026-05-13.** The M1 -demo target *"open the inn door"* is met. 9 commits on this branch implement -and fix the complete round-trip: double-click door β†’ `WorldPicker.Pick` β†’ -`InteractRequests.BuildUse` β†’ ACE broadcasts `SetState (0xF74B)` with -`ETHEREAL` bit β†’ `ShadowObjectRegistry.UpdatePhysicsState` (L.2g slice 1) -mutates cached state β†’ `CollisionExemption.ShouldSkip` exempts the door β†’ -player walks through. - -The plan estimated "30-50 LOC, 1-2 subagent dispatches, ~30 min." -Visual testing surfaced **four bonus discoveries** beyond the plan's -Tasks 1-4: - -1. `InputDispatcher` had no double-click detection (the `SelectDblLeft` - binding was dead code β€” the dispatcher never produced `DoubleClick` - activations). -2. `OnInputAction`'s early-return gate discarded `DoubleClick` activations - before the switch reached the `SelectDblLeft` case. -3. L.2g `CollisionExemption.ShouldSkip` required **both** `ETHEREAL` + - `IGNORE_COLLISIONS` bits, but ACE's `Door.Open()` sends only `ETHEREAL` - (`state=0x0001000C`). -4. `OnLiveStateUpdated` passed a server GUID to `ShadowObjectRegistry` which - is keyed by local entity ID β€” the registry lookup always missed β†’ no-op - β†’ the door never became passable. **This was the actual blocker the user - reported.** - -Fixes 1-4 were shipped as bonus commits 5-9 beyond the plan's Tasks 1-4. -L.2g slice 1 and B.4b are now both fully verified by the same visual test. -Issue #57 is closed. Issue #58 (door swing animation) is filed as M1-deferred -polish. - ---- - -## What shipped on this branch - -| # | Commit | Subject | Task | -|---|---|---|---| -| 1 | `f0b3bd9` | `feat(B.4b): WorldPicker.BuildRay β€” mouse-to-world ray unprojection` | Task 1 | -| 2 | `221b641` | `feat(B.4b): WorldPicker.Pick β€” ray-sphere entity pick` | Task 2 | -| 3 | `5821bdc` | `fix(B.4b): WorldPicker.Pick β€” handle inside-sphere origin + document normalize contract` | Task 2 review fix | -| 4 | `7b4aff2` | `refactor(B.4b): unify _selectedTargetGuid -> _selectedGuid` | Task 3 | -| 5 | `89d82e1` | `feat(B.4b): GameWindow wires Select/Use handlers via WorldPicker` | Task 4 | -| 6 | `242ce70` | `feat(B.4b): InputDispatcher detects double-clicks` | Bonus: Task 4b | -| 7 | `58b95bc` | `fix(B.4b): let DoubleClick activation pass the OnInputAction gate` | Bonus: Task 4c | -| 8 | `a6e4b57` | `fix(phys L.2g slice 1b): widen CollisionExemption to ETHEREAL alone` | L.2g slice 1b | -| 9 | `08be296` | `fix(phys L.2g slice 1c): translate ServerGuid -> entity.Id for ShadowObjectRegistry` | L.2g slice 1c | - -Plus plan/spec commits earlier in the branch session: -- `4a1c594` β€” B.4b design spec. -- `ffa404d` β€” corrected file paths in spec (WorldPicker is in `AcDream.Core.Selection`, not `AcDream.App/Rendering`). -- `179e441` β€” B.4b implementation plan (6 tasks). - -**Build:** clean. **Tests:** 4 new double-click detection tests (commit `242ce70`, all pass). Full suite: builds green, no regressions. L.2g slice 1's 6 tests continue to pass. - ---- - -## What the code does end-to-end - -When the user double-left-clicks a door entity in the Holtburg inn doorway, -the following chain fires: - -1. **Double-click detection** β€” `InputDispatcher.OnMouseDown` checks the - elapsed time since the previous `MouseLeft` press. If ≀500ms, the - activation kind is `DoubleClick`; otherwise `Press`. This is new as - of commit `242ce70`; prior to this the `SelectDblLeft` binding was dead - code (the dispatcher never produced `DoubleClick` activations). - -2. **Action dispatch** β€” `InputDispatcher` resolves the chord - `[MouseLeft, DoubleClick]` β†’ `InputAction.SelectDblLeft` + activation - `DoubleClick`. The multicast `InputAction` event fires, logged as: - `[input] SelectDblLeft DoubleClick`. - -3. **OnInputAction gate** β€” `GameWindow.OnInputAction` receives the event. - Prior to commit `58b95bc`, an early-return guard (`if (activation != Press) return;`) - discarded all `DoubleClick` events. The fix widens the gate to - `if (activation != Press && activation != DoubleClick) return;`. - The switch now reaches the `SelectDblLeft` case. - -4. **Ray construction** β€” `WorldPicker.BuildRay(mousePos, viewport, viewMatrix, projMatrix)` - unprojects the cursor pixel into a world-space ray origin + direction, - using standard NDCβ†’viewβ†’world unprojection. Numerically: the mouse pixel - is mapped to `[-1,+1]` NDC, transformed through `inverse(proj)` to get - a view-space direction, then through `inverse(view)` for world-space. - -5. **Entity pick** β€” `WorldPicker.Pick(ray, entities, maxDist=50m)` iterates - all entities in `_gpuWorldState.GetAllEntities()`, tests each against a - ray-sphere intersection with the entity's bounding radius, and returns - the closest hit. A special-case inside-sphere origin guard (commit `5821bdc`) - ensures the pick works even when the cursor origin is already inside an - entity's bounding sphere (common for large portals or doors at close range). - `[B.4b] pick guid=0x7A9B4015 name=Door` logged on hit. - -6. **Use message** β€” `GameWindow` stores `_selectedGuid = picked.Guid` and - calls `InteractRequests.BuildUse(seq, guid)`. The resulting `0xF7B1 / 0x0036` - game message is sent to ACE via `_liveSession.SendGameMessage(body)`. - `[B.4b] use guid=0x7A9B4015 seq=N` logged. - -7. **ACE processes the Use** β€” ACE's `Door.Open()` flips the door's physics - flags to `ETHEREAL | ...` and broadcasts `SetState (0xF74B)` with the - new state value. - -8. **SetState arrives** β€” `WorldSession.OnSetState` parses the 12-byte - payload (Guid + PhysicsState + InstanceSeq + StateSeq) and fires - `WorldSession.StateUpdated`. `GameWindow.OnLiveStateUpdated` handles it. - **New as of commit `08be296` (slice 1c):** the handler translates - `parsed.Guid` (server GUID `0x7A9B4015`) to `entity.Id` (local entity ID - `0x000F4245`) via `_entitiesByServerGuid` before calling - `ShadowObjectRegistry.UpdatePhysicsState`. Without this translation the - registry lookup always returned "not found" β€” a silent no-op. - Log: `[setstate] guid=0x7A9B4015 entityId=0x000F4245 state=0x0001000C`. - -9. **Collision exemption** β€” next physics tick, `FindObjCollisions` calls - `CollisionExemption.ShouldSkip(entry.State, entry.Flags, moverState)`. - **New as of commit `a6e4b57` (slice 1b):** the check fires on - `(state & ETHEREAL_PS) != 0` alone (widened from the original `ETHEREAL && - IGNORE_COLLISIONS` conjunction). Because ACE broadcasts only `ETHEREAL` - in the low bits (`state=0x0001000C`), the original conjunction never fired; - the door stayed solid. - -10. **Player walks through** β€” the resolver produces no wall-contact response - for the door's collision geometry. User confirms: "Now I can walk through." - -### Observed log evidence - -``` -[input] SelectDblLeft DoubleClick -[B.4b] pick guid=0x7A9B4015 name=Door -[B.4b] use guid=0x7A9B4015 seq=N -[setstate] guid=0x7A9B4015 entityId=0x000F4245 state=0x0001000C -``` - -Player walks through the closed door after the `setstate` line. - ---- - -## The four bonus discoveries - -### 1. InputDispatcher had no double-click detection (`242ce70`) - -**Root cause:** `InputDispatcher.OnMouseDown` only looked up `Press` and -`Hold` activations in the binding table. The `SelectDblLeft` binding was -wired to the chord `[MouseLeft, DoubleClick]` in `KeyBindings.cs:300-320` -(shipped in B.4, 2026-04-28), but the dispatcher's mouse-down handler -never set activation to `DoubleClick` β€” it always produced `Press`. -So `SelectDblLeft` was literally unreachable: the chord required -`DoubleClick` to match, but the dispatcher never generated it. - -**Fix:** Added a `_lastMouseDownTime` (and `_lastMouseDownButton`) tracker -to `InputDispatcher`. In `OnMouseDown`, if the same button fires within -500ms of its last press, activation is `DoubleClick`; otherwise `Press`. -500ms matches the standard Windows/macOS double-click threshold. - -**Rationale:** The fix is minimal and correct. A more faithful retail -implementation might read the OS's configured double-click interval, but -500ms is the retail default and was the right call for now. 4 new unit -tests cover the timing logic: first click = Press, second click within -500ms = DoubleClick, third click = Press again (resets the window), and -button mismatch = Press. - -### 2. OnInputAction gate discarded DoubleClick activations (`58b95bc`) - -**Root cause:** Even after discovery #1 was fixed and `SelectDblLeft DoubleClick` -fired from the dispatcher, the event handler had an early-return guard at -the top of `GameWindow.OnInputAction`: - -```csharp -if (activation != InputActivation.Press) return; -``` - -This guard was introduced to prevent `Hold` repetition from triggering -switch cases intended for one-shot actions. It correctly blocked `Hold` -but also blocked `DoubleClick` β€” so the `SelectDblLeft` case was still -unreachable even after the dispatcher started generating `DoubleClick`. - -**Fix:** Widened the guard to let both `Press` and `DoubleClick` through: - -```csharp -if (activation != InputActivation.Press && activation != InputActivation.DoubleClick) return; -``` - -**Rationale:** `DoubleClick` is semantically a one-shot activation (fires -once per double-click gesture), so it belongs in the same pass-through -group as `Press`. `Hold` repetition remains blocked. - -### 3. CollisionExemption required both ETHEREAL + IGNORE_COLLISIONS (`a6e4b57`) - -**Root cause:** The original `CollisionExemption.ShouldSkip` check was -ported faithfully from `acclient_2013_pseudo_c.txt:276782`, which requires -**both** `ETHEREAL_PS (0x4)` and `IGNORE_COLLISIONS_PS (0x10)` to be set -simultaneously before short-circuiting collision detection. Retail servers -send both bits when opening a door, so retail clients see `state β‰₯ 0x14`. - -However, ACE's `Door.Open()` broadcasts only the `ETHEREAL` bit in the -low portion of the state word. The observed wire value was -`state=0x0001000C`: bit `0x4` (ETHEREAL) is set, bit `0x10` -(IGNORE_COLLISIONS) is not. The `&&` conjunction in `ShouldSkip` evaluated -to false β†’ door stayed solid even after the registry update. - -This was the exact scenario the L.2g slice 1 Important review note warned -about (see L.2g handoff Β§"One Important review note"): *"ACE's -`PhysicsObj.cs:787-791` may set both bits... but this is not verified by -the test suite. The B.4b visual test will settle this definitively."* -It settled as: ACE sends `0x4` alone, not `0x14`. - -**Fix:** Widened the short-circuit to fire on `ETHEREAL` alone: - -```csharp -// Widened from (ETHEREAL && IGNORE_COLLISIONS) β€” ACE Door.Open() sends -// ETHEREAL alone (state=0x0001000C); retail servers send both. -// Pragmatic choice: exempt on ETHEREAL-bit-alone until full retail -// obstruction_ethereal flag path is ported. -if ((state & ETHEREAL_PS) != 0) return true; -``` - -**Rationale:** The deeper retail path (pseudo-C line 276795 sets -`obstruction_ethereal=1` and routes through downstream movement handling) -was not ported β€” that's a more invasive change requiring more testing. The -pragmatic widening to ETHEREAL alone is correct for ACE's Door behavior and -matches the spirit of the retail check (ETHEREAL means "pass through me"). -If a future retail-server emulator sends both bits, the widened check still -fires (ETHEREAL is a subset of ETHEREAL+IGNORE_COLLISIONS). - -### 4. ServerGuid β†’ entity.Id translation missing in OnLiveStateUpdated (`08be296`) β€” THE actual blocker - -**Root cause:** `ShadowObjectRegistry` is keyed by local `entity.Id` (the -per-session integer ID assigned by `GpuWorldState` at entity registration, -e.g. `0x000F4245`). The `GameWindow.OnLiveStateUpdated` handler was passing -`parsed.Guid` β€” the **server GUID** broadcasted in the `SetState` packet -(e.g. `0x7A9B4015`) β€” directly to `UpdatePhysicsState`. Because the registry -has no entry keyed by server GUID, the lookup always returned "not found" -and the state mutation was silently dropped. The registry stayed at -`state=0x00000000` (closed, solid) regardless of how many times the door -was clicked. - -This is why discoveries 1-3 alone were insufficient: even with double-click -detection working, the correct gate firing, and `CollisionExemption` -widened, the registry still held the stale closed state and the door -stayed solid. - -**Fix:** Used the pre-existing `_entitiesByServerGuid` reverse-lookup -dictionary on `GameWindow` (populated at entity registration in -`OnLiveCreateObject` since Phase 6.6/6.7). `OnLiveStateUpdated` now does: - -```csharp -if (_entitiesByServerGuid.TryGetValue(parsed.Guid, out var entity)) - _physicsEngine.ShadowObjects.UpdatePhysicsState(entity.Id, parsed.PhysicsState); -``` - -The `entityId=` field was added to the `[setstate]` diagnostic log line -specifically to make this translation visible and greppable: -`[setstate] guid=0x7A9B4015 entityId=0x000F4245 state=0x0001000C`. - -**Why this was missed:** L.2g slice 1's unit tests operated at the -`ShadowObjectRegistry` level directly, calling `UpdatePhysicsState` with -an `entity.Id` (not a server GUID). The integration was never exercised -end-to-end before B.4b's visual test. The two tests `UpdatePhysicsState_FlipsEthereal_*` -were correct in isolation; the broken layer was one level above them -(the handler β†’ registry call site). - -**Why the "multiple doors" misdiagnosis occurred:** Before slice 1c was -identified, the `[resolve]` probes showed wall hits attributed to -`obj=0x000F4245` while the clicked door's ServerGuid was `0x7A9B4015`. -Initial read: "these are two different entities blocking the threshold." -Slice 1c clarified: both IDs refer to the same door β€” `0x000F4245` is -the local entity ID, `0x7A9B4015` is the server GUID for the same entity. -The ID-space mismatch was the cause of both the collision-not-clearing -AND the "different object" misread. - ---- - -## Open notes / follow-ups - -### Door swing animation (#58) - -When ACE opens a door it broadcasts **two** packets, not one: - -1. `SetState (0xF74B)` β€” the collision-bit flip. **Handled by L.2g slice 1.** -2. `UpdateMotion (0xF74D)` with stance/command `(NonCombat, On)` β€” the - swing animation cycle. **NOT handled.** - -acdream's `UpdateMotion` pipeline is currently scoped to player + creature -animation (Phase L.3). Non-creature entities like doors do not receive -cycle commands. The door therefore opens (becomes passable) but has no -visible swing animation. - -Filed as **issue #58**. Scope is unknown β€” routing `UpdateMotion` to -non-creature `WorldEntity` instances could be quick (few lines), or the -`AnimationSequencer` may have creature-specific assumptions that require -audit first. Filed as M1-deferred polish; it does not block the demo -scenario. - -### Door toggle behavior - -ACE doors toggle on each Use: first double-click opens, subsequent -double-click closes (re-sends `SetState` with `state=0x00000000`, restoring -collision). This is correct ACE behavior and matches retail. No issue to file. - -Rapid double-clicks (faster than ACE's server-tick processing) will open -then close in quick succession β€” each Use lands as a distinct game action. -Expected behavior; no fix needed. - -### Multiple-door misdiagnosis (historical note) - -While slice 1c was still unidentified, the `[resolve]` diagnostic showed: - -``` -[resolve] ... obj=0x000F4245 wall hit -[B.4b] use guid=0x7A9B4015 ... -[setstate] guid=0x7A9B4015 state=0x0001000C -[resolve] ... obj=0x000F4245 wall hit (unchanged!) -``` - -Initial misdiagnosis: there must be a *different* door entity (`0x000F4245`) -blocking the threshold whose state was never updated. Slice 1c revealed: -both IDs refer to the same door β€” one is the server GUID (network space), -the other is the local entity ID (registry space). The registry update was -targeting the server GUID (which missed), so the local-ID-keyed entry -stayed solid. - -### Selection HUD / hover-highlight / brackets - -Out of B.4b scope per design spec Β§Non-goals. The `_selectedGuid` field on -`GameWindow` is populated (stores the last-picked entity's server GUID), but -nothing renders a selection bracket, hover highlight, or target nameplate. -That is M2/M3 HUD work (Phase D.6). - -### BuildPickUp (F key) + UseWithTarget UX - -`InteractRequests.BuildPickUp` exists (as an alias of `BuildUse`). The -`SelectionPickUp` input action and the F-key binding exist. But -`OnInputAction` has no case for `SelectionPickUp` β€” pick-up-by-F-key is -still unimplemented. Same for `UseWithTarget` (requires a secondary target -selection UX). Both deferred to a follow-up phase; not M1-blocking. - ---- - -## Next session - -**M1 demo progress as of this branch:** -- βœ… "walk through Holtburg without getting stuck" β€” Phase L.2 in progress (outdoor collision works; CBuildingObj interior still deferred to L.2d). -- βœ… "open the inn door" β€” **done** (B.4b, this branch). -- ⬜ "click an NPC" β€” pick + Use wiring exists now; depends on ACE NPC handler responding to Use. -- ⬜ "pick up an item" β€” `BuildPickUp` + F-key wiring not yet in `OnInputAction`. - -**Recommended next steps (in M1 critical-path order):** - -1. **Door swing animation (#58)** β€” cosmetic M1 polish. Route - `UpdateMotion (0xF74D)` to non-creature entities so the door visually - swings. Could be quick (30 min) or moderate (2 hrs with AnimationSequencer - audit). Worth a spike before committing to an estimate. - -2. **Chronic open-issue triage** β€” #2 (lightning), #4 (horizon-glow), #28 - (aurora), #29 (cloud thinness), #37 (humanoid coat), #41 (remote-motion - blips) have been deferred since April/early-May. Link each to a future - phase or downgrade. ~1 hour. Not M1-blocking but surfaces the real backlog. - -3. **More Phase C visual-fidelity** β€” C.2 (dynamic point lights), C.3 - (palette tuning), C.4 (double-sided translucent polys). World still reads - "old" without local lighting on fireplaces/lamps. - ---- - -## Reproducibility - -Same launch recipe as before. For reproducing the visual test: - -```powershell -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -$env:ACDREAM_TEST_HOST = "127.0.0.1" -$env:ACDREAM_TEST_PORT = "9000" -$env:ACDREAM_TEST_USER = "testaccount" -$env:ACDREAM_TEST_PASS = "testpassword" -$env:ACDREAM_DEVTOOLS = "1" -$env:ACDREAM_PROBE_BUILDING = "1" -$env:ACDREAM_PROBE_RESOLVE = "1" -dotnet run --project src\AcDream.App\AcDream.App.csproj -c Debug 2>&1 | - Tee-Object -FilePath "launch-b4b.log" -``` - -Walk to the Holtburg inn doorway. Double-left-click the closed Door. Walk -through. Subsequent double-clicks will close and re-open (ACE toggle). - -After closing the client, grep for: - -```powershell -Select-String -Path launch-b4b.log -Pattern "SelectDblLeft|pick guid|use guid|setstate.*entityId" -``` - -Expected: -- `[input] SelectDblLeft DoubleClick` β€” dispatcher fires on second click within 500ms. -- `[B.4b] pick guid=0x7A9B4015 name=Door` β€” ray hits the door. -- `[B.4b] use guid=0x7A9B4015 seq=N` β€” Use message sent. -- `[setstate] guid=0x7A9B4015 entityId=0x000F4245 state=0x0001000C` β€” ACE reply processed, translation confirmed. - ---- - -## Worktree state at handoff - -- Branch `claude/compassionate-wilson-23ff99`. -- 9 implementation commits + 3 plan/spec commits ahead of `eea9b4d` - (the L.2g slice 1 merge from the previous session). -- Controller should run a code review, then merge to main. -- Do NOT rebase or squash β€” each commit tells a diagnostic story that - the next phase's debugging may need. diff --git a/docs/research/2026-05-13-b4c-shipped-handoff.md b/docs/research/2026-05-13-b4c-shipped-handoff.md deleted file mode 100644 index 862e4be..0000000 --- a/docs/research/2026-05-13-b4c-shipped-handoff.md +++ /dev/null @@ -1,346 +0,0 @@ -# Phase B.4c shipped β€” handoff (visual-verified 2026-05-13) - -**Date:** 2026-05-13. -**Branch:** `claude/phase-b4c-door-anim` (ready to merge to main; do NOT merge here β€” controller handles that after code review). -**Predecessors:** -- [docs/research/2026-05-13-b4b-shipped-handoff.md](2026-05-13-b4b-shipped-handoff.md) β€” B.4b shipped handoff; interaction was the upstream dependency (Use message, SetState handling, collision exemption, double-click detection β€” all shipped there). -- [docs/superpowers/specs/2026-05-13-phase-b4c-design.md](../superpowers/specs/2026-05-13-phase-b4c-design.md) β€” B.4c design spec. -- [docs/superpowers/plans/2026-05-13-phase-b4c-plan.md](../superpowers/plans/2026-05-13-phase-b4c-plan.md) β€” B.4c implementation plan (4 tasks). - ---- - -## TL;DR - -Phase B.4c **shipped end-to-end and is visual-verified 2026-05-13.** The M1 -demo target *"open the inn door"* now has **full visual feedback** β€” the door -swings open when double-clicked and swings closed again when ACE toggles it -back. 4 implementation commits implement and fix door-specific spawn-time -`AnimationSequencer` registration + `UpdateMotion` routing + stance-value -correctness. - -The plan estimated "2 tasks, door spawn-time registration + UM diagnostic." -Visual testing surfaced **two bonus discoveries** beyond the plan: - -1. The plan's `NonCombatStance` constant was wrong: `0x80000001` (from - creature motion table conventions) should be `0x8000003D` (from AC's - `MotionStance.NonCombat = 0x0000003D`). Wrong constant β†’ wrong - `HasCycle` lookup β†’ `SetCycle` never fires β†’ sequencer empty β†’ - per-frame part rebuild collapses to entity origin β†’ doors render halfway - underground. -2. The `AnimationSequencer`'s linkβ†’cycle boundary transition produces a - brief one-frame flash through the prior pose at the end of the door-swing - animation. Not B.4c-specific β€” it is the sequencer's general link+cycle - queue mechanics. Deferred as issue #61. - -Issue #58 (door swing animation) is closed. Issues #61 + #62 (cycle-boundary -flash; PARTSDIAG null-guard) are filed as M1-deferred polish. - ---- - -## What shipped on this branch - -| # | Commit | Subject | Task | -|---|---|---|---| -| 1 | `9053860` | `feat(B.4c): door spawn-time AnimationSequencer with state-seeded initial cycle` | Task 1 | -| 2 | `b89f004` | `feat(B.4c): [door-cycle] diagnostic in OnLiveMotionUpdated` | Task 2 | -| 3 | `8a9b15e` | `refactor(B.4c): share IsDoorName predicate + durable comment + use UM locals` | Task 2 review | -| 4 | `454d88e` | `fix(B.4c): correct NonCombat stance value (0x3D, not 0x01) + read spawn.MotionState` | Bonus: stance fix | - -Plus plan/spec commits earlier in the branch session: -- `b4f131e` β€” B.4c design spec. -- `6ae38f7` β€” B.4c implementation plan (4 tasks). - -**Build:** clean. **Tests:** existing test suite passes; no new unit tests added -(the door-cycle registration path runs in-process with a live GameWindow; pure -unit tests would require a MotionTable + AnimationSequencer integration harness). - ---- - -## What the code does end-to-end - -When the world loads, any entity whose name contains "Door" (checked via the -shared `GameWindow.IsDoorName(string)` helper, committed as part of Task 2 -review) is registered in the **door-animation side-track** at spawn time. This -happens inside `GameWindow.OnLiveEntitySpawnedLocked`, which branches on -`IsDoorSpawn(spawn)` before reaching the standard creature/player paths. - -### At world load (spawn time) - -1. `IsDoorSpawn(spawn)` β€” delegates to `IsDoorName(spawn.Name)`, which - returns `name == "Door"`. Detection by server-sent name string only. - Cheap, exact, no WeenieType lookup. If a future ACE localizes "Door" - or sends a different name, those entities silently won't animate β€” - acceptable per B.4c's "doors only at English Holtburg" scope. - -2. **Initial state seed** β€” the door's `PhysicsState` from `spawn` carries the - open/closed bit. The code reads `spawn.PhysicsState` (or - `spawn.MotionState?.Stance` as a fallback for unusual doors with explicit - stance data) to determine whether to seed the sequencer with the `Off` - (closed) or `On` (open) cycle. - -3. **AnimationSequencer registration** β€” a fresh `AnimationSequencer` is - created for the door entity's `MotionTableId` (from `spawn`). Then: - ```csharp - var style = 0x80000000u | (uint)MotionStance.NonCombat; // = 0x8000003D - var cycleCmd = isOpen ? MotionCommand.On : MotionCommand.Off; - sequencer.SetCycle(style, (uint)cycleCmd, speed: 0f); - ``` - The fully-initialized `AnimatedEntity` (with the seeded `Sequencer`) is - registered into the existing `_animatedEntities` dict keyed by `entity.Id` - β€” same dict that holds creatures and the player. `Animation = null!` - (the null-forgiving suppression matches an existing pattern at - `GameWindow.cs:7885` for sequencer-driven entities where the legacy - `Animation` field is unused). At the first per-frame `Advance(dt)` - call from `TickAnimations`, the sequencer produces the correct - rest-pose frames for the door's current state. - -4. **Log evidence at spawn:** - ``` - [door-anim] registered guid=0x7A9B403A entityId=0x000F4291 mtable=0x09000202 initialStyle=0x8000003D initialCycle=0x4000000C - ``` - `0x4000000C` = `MotionCommand.Off` with the upper flag bits β€” the door is - closed at spawn, matching the initial world state. - -### When the door opens (UpdateMotion arrives) - -ACE broadcasts `UpdateMotion (0xF74D)` with `stance=0x003D` (NonCombat) and -wire `cmd=0x000C` (which `MotionCommandResolver.ReconstructFullCommand` -maps to full motion `0x4000000B` = `MotionCommand.On` = door open). - -B.4c does NOT add a new dispatch path here β€” the existing -`OnLiveMotionUpdated` handler already routes via the `_animatedEntities` -dict + per-entity `Sequencer`, the same code path creatures use. The -only B.4c contribution at UM dispatch is the new `[door-cycle]` -diagnostic gated on `IsDoorName(doorInfo.Name)`. Before B.4c, doors -silently dropped at the `_animatedEntities.TryGetValue` check at -`GameWindow.cs:3036` because doors weren't registered; B.4c's Task 1 -spawn-time branch fixed that. - -The sequencer transitions from the `Off` cycle (static closed pose) through -the door-swing link animation to the `On` cycle (static open pose). - -**Log evidence:** -``` -UM guid=0x7A9B403A mt=0x00 stance=0x003D cmd=0x000C spd=0.00 | seq now style=0x8000003D motion=0x4000000B -[door-cycle] guid=0x7A9B403A stance=0x003D cmd=0x000C -``` -The `[door-cycle]` line is the new B.4c diagnostic (gated on -`ACDREAM_PROBE_BUILDING=1`). The `seq now motion=0x4000000B` shows the -sequencer's current motion state after the `SetCycle` call. - -### SetState chain (from B.4b + L.2g, unchanged) - -Simultaneously with `UpdateMotion`, ACE also sends `SetState (0xF74B)`: -``` -[setstate] guid=0x7A9B... state=0x0001000C -``` -This is the B.4b / L.2g chain: `ShadowObjectRegistry.UpdatePhysicsState` flips -the door's cached state, `CollisionExemption.ShouldSkip` exempts on ETHEREAL-alone, -and the player can walk through. B.4c is additive β€” it only adds the animation -layer; it does not touch the collision path. - -### When the door closes - -ACE toggles on the next Use: `UpdateMotion` with `cmd=0x000B` (Off = close). -The sequencer transitions from the `On` cycle (open pose) through the door-swing -link animation (reversed) to the `Off` cycle (closed pose). - -**Log evidence:** -``` -UM guid=0x7A9B403A mt=... cmd=0x000B ... motion=0x4000000C -[door-cycle] guid=0x7A9B... cmd=0x000B -[setstate] guid=0x7A9B... state=0x00010008 -``` - -### Per-frame mesh rebuild - -The door sequencer integrates into `GameWindow.TickAnimations` via the same -`_animatedEntities` dict that holds creatures. Each frame, `ae.Sequencer.Advance(dt)` -is called and the resulting per-part transforms drive the same `MeshRefs` rebuild -that creature entities use (sequencer branch at `GameWindow.cs:7497`; doors -never enter the legacy slerp `else` branch). This is the reason the stance-value -bug produced underground doors: with the wrong style key (`0x80000001`) -`HasCycle` returned false, the sequencer was empty at spawn, `Advance` returned -identity frames, and the per-frame part-matrix rebuild received `Vector3.Zero / -Quaternion.Identity` for every part β€” collapsing them all to the entity origin. - ---- - -## The two bonus discoveries - -### 1. NonCombatStance constant was wrong: 0x01 vs 0x3D (`454d88e`) β€” THE render blocker - -**Root cause:** The B.4c design spec specified the initial-cycle style key as: -```csharp -uint style = 0x80000000u | (uint)MotionStance.NonCombat; // spec said 0x80000001 -``` -The spec's comment was wrong. `MotionStance.NonCombat` in acdream (and retail) -is `0x0000003D`, not `0x00000001`. The value `0x01` is a creature-specific -variant. The style key for the door's cycle lookup must be `0x8000003D`. - -With the wrong style key: -- `sequencer.HasCycle(0x80000001, MotionCommand.Off)` β†’ false. -- `SetCycle(0x80000001, ...)` enqueued a cycle that was never reachable. -- On first `Advance(dt)`, the sequencer returned 0 part-frames. -- The per-frame mesh rebuild at `GameWindow.cs:7691` iterated 0 frames, leaving - every door part at the entity root origin (which is the door's structural - pivot, typically near the hinge). For inn doors this pivot is at roughly - floor level, so all the door's mesh parts collapsed to that single point, - rendering as a thin sliver partway underground. - -**Fix:** Corrected the constant. Additionally, added a defensive read of -`spawn.MotionState?.Stance` as the source of the stance value where available, -so unusual doors with explicit motion state (possible in custom ACE content) use -their actual stance rather than the hardcoded NonCombat assumption: - -```csharp -var stance = spawn.MotionState?.Stance ?? MotionStance.NonCombat; -uint style = 0x80000000u | (uint)stance; -``` - -**Verification:** After this fix, the `[door-anim]` log line showed -`initialStyle=0x8000003D` (correct), and doors appeared at the correct floor -level and height at world load. - -### 2. AnimationSequencer linkβ†’cycle boundary flash (deferred as #61) - -**Observed:** User reports "weird flapping at end of animation when it opens. -It is like it flaps back to closed quickly then open. Like really quickly." -Both open and close animations exhibit this flash. - -**Root cause hypothesis:** `AnimationSequencer.SetCycle` enqueues a transition -link (the actual swing animation) followed by the target cycle (the door's -rest pose β€” likely a single-frame static "open" or "closed" pose). At the linkβ†’ -cycle boundary, the sequencer evaluates the cycle's frame 0 before the cycle -settles into its natural rest position. If the link's last frame and the -cycle's frame 0 don't match exactly (which is common for one-shot door motions -versus the continuous idle cycles the sequencer was designed for), the renderer -sees one frame of the "wrong" pose at the link boundary. - -**Why not B.4c-specific:** This is the sequencer's general link+cycle queue -boundary semantics. Any entity that uses a one-shot `SetCycle` transition -(rather than a continuous idle cycle) will exhibit this if the link/cycle -boundary frames diverge. The door case just makes it visible because the -swing duration is short (1-2 seconds) and the user is watching closely. - -**Deferred:** Filed as issue #61. Workaround: the flash is brief (~1 frame, -~16ms at 60 FPS) and does not affect the door's usability. M1 is met without -this fix. - ---- - -## Open notes / follow-ups - -### #61 β€” AnimationSequencer linkβ†’cycle frame-0 flash (filed this session) - -See Bonus discovery #2 above. Deferred as M1-deferred polish. Low severity. -Acceptance: door swing animations play cleanly with no intermediate closed/open -pose flash at the linkβ†’cycle transition. - -### #62 β€” PARTSDIAG null-guard for sequencer-driven entities (filed this session) - -The PARTSDIAG block at `GameWindow.cs:7657` reads `ae.Animation.PartFrames` -without a null-guard. B.4c introduced `Animation = null!` for sequencer-driven -door entities. Today this is safe (doors never enter `_remoteDeadReckon` because -ACE never sends UpdatePosition for them). Deferred as low-severity latent crash. -One-line fix when addressed. - -### Chests, levers, traps - -The `IsDoorName` / `IsDoorSpawn` predicate correctly gates on door entities only. -Other interactable non-creature entities (chests, levers, traps) will still -silently drop their `UpdateMotion` commands β€” they are not covered by B.4c and -no issue has been filed for them yet. When those animations become relevant -(M2/M3 inventory + dungeon content), the same spawn-time registration pattern -can be extended: broaden the detection predicate beyond `name == "Door"` and -register additional entity types in the existing `_animatedEntities` dict via -the same sibling branch. - -### Door toggle behavior - -Unchanged from B.4b. ACE doors toggle on each Use: first double-click opens, -subsequent double-click closes. Both transitions now play the correct swing -animation (open swing on open, close swing on close). - ---- - -## Next session - -**M1 demo progress as of this branch:** -- "Walk through Holtburg without getting stuck" β€” Phase L.2 in progress (outdoor collision works; `CBuildingObj` interior still deferred to L.2d). -- "Open the inn door" β€” **DONE with full visual feedback** (B.4b interaction + B.4c animation, this branch). Door swings open AND closed. -- "Click an NPC" β€” pick + Use wiring exists (from B.4b); depends on ACE NPC handler responding to Use correctly. -- "Pick up an item" β€” `BuildPickUp` + F-key wiring not yet in `OnInputAction`. Post-B.4b/B.4c deferred. - -**Recommended next steps (in M1 critical-path order):** - -1. **"Click an NPC" verification spike** β€” B.4b's WorldPicker + Use messaging - is already wired. The question is whether ACE NPCs respond to Use and what - they broadcast back. A quick spike: stand near an NPC in Holtburg, - double-click, check what ACE sends back. If ACE sends recognizable response - messages, wire them; if it is silent, investigate ACE's NPC handler - configuration for testaccount. - -2. **Phase B.5 β€” Ground item pickup (F key)** β€” `SelectionPickUp` input action - + F-key binding exist but `OnInputAction` has no case. `BuildUse` is the - same wire format as `BuildPickUp`. Adding the `SelectionPickUp` case to - the switch and routing to `InteractRequests.BuildPickUp` is a one-commit - addition. - -3. **Triage chronic open-issue list** β€” #2 (lightning), #4 (sky horizon-glow), - #28 (aurora), #29 (cloud thinness), #37 (humanoid coat), #41 - (remote-motion blips) have been open since April/early-May. Link each to - a future phase or downgrade. ~1 hour. - -4. **#61 fix (cycle-boundary flash)** β€” low-severity M1 polish. If the user - finds the flash distracting during the M1 demo record, address before - milestone wrap; otherwise defer to M2 animation quality pass. - ---- - -## Reproducibility - -Same launch recipe as B.4b. For reproducing the visual test: - -```powershell -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -$env:ACDREAM_TEST_HOST = "127.0.0.1" -$env:ACDREAM_TEST_PORT = "9000" -$env:ACDREAM_TEST_USER = "testaccount" -$env:ACDREAM_TEST_PASS = "testpassword" -$env:ACDREAM_DEVTOOLS = "1" -$env:ACDREAM_PROBE_BUILDING = "1" -$env:ACDREAM_PROBE_RESOLVE = "1" -dotnet run --project src\AcDream.App\AcDream.App.csproj -c Debug 2>&1 | - Tee-Object -FilePath "launch-b4c.log" -``` - -Walk to the Holtburg inn doorway. Watch the `[door-anim]` lines appear in the -log as each door entity spawns (verifies correct style=0x8000003D and initial -cycle). Double-left-click a closed door. Watch the swing animation. Walk -through. Wait ~30s (ACE auto-close). Watch the close animation. - -After closing the client, grep for: - -```powershell -Select-String -Path launch-b4c.log -Pattern "door-anim|door-cycle|setstate" -``` - -Expected: -- `[door-anim] registered guid=... initialStyle=0x8000003D initialCycle=0x4000000C` β€” correct style + Off initial cycle for each closed door. -- `[door-cycle] guid=... stance=0x003D cmd=0x000C` β€” open UpdateMotion processed. -- `[setstate] guid=... state=0x0001000C` β€” ACE collision-flip processed (from B.4b / L.2g). -- `[door-cycle] guid=... cmd=0x000B` β€” close UpdateMotion processed. -- `[setstate] guid=... state=0x00010008` β€” ACE close collision-flip processed. - ---- - -## Worktree state at handoff - -- Branch `claude/phase-b4c-door-anim`. -- 6 commits ahead of `3e08e10` (the B.4b+L.2g merge from this morning): - 2 docs/spec/plan commits + 4 implementation commits. -- Controller should run a code review, then merge to main. -- Do NOT rebase or squash β€” each commit tells a diagnostic story that the - next phase's debugging may need. diff --git a/docs/research/2026-05-13-b5-pickup-handoff.md b/docs/research/2026-05-13-b5-pickup-handoff.md deleted file mode 100644 index 5013242..0000000 --- a/docs/research/2026-05-13-b5-pickup-handoff.md +++ /dev/null @@ -1,235 +0,0 @@ -# Phase B.5 β€” BuildPickUp + ground-item interaction β€” fresh-session handoff - -**Date:** 2026-05-13 evening (after B.4c ship). -**Branch:** `claude/phase-b5-pickup` (renamed from `claude/investigate-npc-click`). -**Worktree:** `C:\Users\erikn\source\repos\acdream\.claude\worktrees\investigate-npc-click` (directory name kept; only the branch was renamed). -**Predecessor on main:** `e7842e0` β€” `Merge branch 'claude/phase-b4c-door-anim' β€” Phase B.4c door swing animation`. - ---- - -## TL;DR - -After B.4c shipped (doors visibly swing open/close), three of M1's four -demo targets are met: *walk through Holtburg*, *open the inn door*, and -likely *click an NPC* (per the investigation below; not yet -visual-verified). The remaining target is *pick up an item*, which -needs a new outbound wire builder + F-key handler in `GameWindow`. - -Phase **B.5** is the slice that closes M1's "click + pickup" demo -path. Scope: ~50 LOC across two existing files (no new files). -Implementation pattern mirrors B.4b's outbound Use chain. - ---- - -## Investigation findings (carry forward) - -Before starting B.5 work, the controller agent investigated whether -B.4b's existing `BuildUse` chain already handles "click an NPC". Code -reading produced this answer: **yes, the basic chat-dialogue case -should already work end-to-end with zero new code**. Verify -opportunistically during B.5's visual test by clicking an NPC while -in-world. - -Specifically: - -- **ACE's `Creature.ActOnUse`** at - `references/ACE/Source/ACE.Server/WorldObjects/Creature.cs:334` - defers to `base.OnActivate β†’ EmoteManager.OnUse()`. The emote - manager walks the creature's emote table and emits `Tell`, - `CommunicationTransientString`, `Motion`, and other game events. -- **All those events are already wired** in - `src/AcDream.Core.Net/GameEventWiring.cs`: - - `Tell (0x0027)` β†’ `chat.OnTellReceived` (line 78) - - `CommunicationTransientString (0x028B)` β†’ `chat.OnSystemMessage` (line 83) - - `WeenieError / WeenieErrorWithString` β†’ `chat.OnSystemMessage` (lines 139, 144) - Plus `UpdateMotion (0xF74D)` is already routed for creature entities - via `OnLiveMotionUpdated`. -- **`UseDone (0x01C7)`** β€” the completion ack β€” has a parser at - `GameEvents.ParseUseDone` but is **not registered** with the - dispatcher. Silent drop. Harmless for the basic demo (the chat - events arrive independently), but worth filing as a follow-up if not - picked up by B.5. - -**Conclusion:** click-NPC chain is wired; no code change needed for the -M1 demo target 3 acceptance. Verify in-world during B.5's launch. - ---- - -## B.5 scope (decisions already made) - -| Decision | Value | Rationale | -|---|---|---| -| Trigger | F-key (`InputAction.SelectionPickUp`) | Already bound at `KeyBindings.cs:172` | -| Target selection | Requires `_selectedGuid` (B.4b's renamed field) | Mirrors retail F-key behavior + B.4b's `UseSelected` pattern. User single-clicks the ground item to select, then F to pickup. | -| Wire opcode | `GameAction.PutItemInContainer (0x0019)` | ACE source: `references/ACE/Source/ACE.Server/Network/GameAction/Actions/GameActionPutItemInContainer.cs` | -| Wire payload | 12 bytes: `itemGuid (u32) + containerGuid (u32) + placement (i32)` | Same source | -| Container destination | The player's own server guid (`_playerServerGuid`) | Single-bag pickup; bag-specific destinations are M2+ work | -| Placement value | 0 (let server pick slot) | Simplest; placement-control UI is M2+ | -| Visual feedback | Toast + `[pickup]` log line | No inventory UI yet; the existing `WieldObject` / `InventoryPutObjInContainer` server events already update `ItemRepository` so the state is correct internally | -| Pick under cursor fallback | **NO** | Out of scope per user decision. Strict select-first UX. | - -Brainstorm explicitly **NOT** done with the user (interrupted before -the design sections were presented). The new session should re-confirm -these decisions are still desired before writing the spec β€” or just -proceed if they remain obviously right. - ---- - -## Three changes B.5 needs to land - -1. **`src/AcDream.Core.Net/Messages/InteractRequests.cs`** β€” add - `BuildPickUp(uint gameActionSequence, uint itemGuid, uint containerGuid, int placement)`. - Pattern: same as the existing `BuildUseWithTarget` builder at line - 51 of that file. 20-byte total body (`0xF7B1 envelope + seq + opcode - 0x0019 + 12-byte payload`). - -2. **`src/AcDream.App/Rendering/GameWindow.cs`** β€” add a private helper - `SendPickUp(uint itemGuid)`: - - Gate on `_liveSession?.CurrentState == InWorld` (same pattern as - B.4b's `SendUse`). - - `seq = _liveSession.NextGameActionSequence()`. - - `body = InteractRequests.BuildPickUp(seq, itemGuid, _playerServerGuid, 0)`. - - `_liveSession.SendGameAction(body)`. - - Diagnostic: `Console.WriteLine($"[pickup] item=0x{itemGuid:X8} container=0x{_playerServerGuid:X8} seq={seq}")`. - -3. **`src/AcDream.App/Rendering/GameWindow.cs` `OnInputAction` switch** - β€” add `case InputAction.SelectionPickUp:` near the other `Select*` / - `UseSelected` cases (B.4b added those around line 8633+). Body: - `if (_selectedGuid is uint sel) SendPickUp(sel); else _debugVm?.AddToast("Nothing selected");`. - -That's the whole code change. ~50 LOC including diagnostics. - ---- - -## Likely ID-translation gotcha (the L.2g slice 1c pattern) - -B.4b's L.2g slice 1c surfaced an ID-space mismatch: the **`BuildUse`** -wire builder takes a `targetGuid` which is `entity.ServerGuid`, but -`ShadowObjectRegistry` keys by `entity.Id`. For `BuildPickUp`: - -- `itemGuid` argument must be `entity.ServerGuid` (the server's - identifier β€” ACE looks it up in its world). βœ… B.4b's picker returns - `ServerGuid`, so `_selectedGuid` already carries the right value. -- `containerGuid` argument must be `_playerServerGuid` (the server's - identifier for the player). βœ… Already a ServerGuid in `GameWindow`. - -So B.5 should NOT hit the same ID-mismatch trap L.2g slice 1c did. But -re-check at implementation time. - ---- - -## ACE inbound chain (already wired) - -After ACE processes a `BuildPickUp`, it broadcasts: - -- `0x019B InventoryPutObjInContainer` β€” moves the item record into the - player's container. Already wired to - `ItemRepository.MoveItem(itemGuid, containerGuid, placement)` at - `GameEventWiring.cs:239`. -- `RemoveObject` for the world-spawned item β€” already wired (existing - despawn path removes the ground item from view). -- Possibly `WieldObject` if the item auto-equips β€” already wired - (`GameEventWiring.cs:231`). - -No new inbound wiring needed for the minimum demo. The user will see: - -1. Click ground item β†’ selection updates. -2. Press F β†’ diagnostic logs, packet sent. -3. ACE processes; sends inventory + despawn events. -4. Item disappears from ground. -5. (No inventory UI yet, but item is in `ItemRepository`.) - ---- - -## Acceptance criteria - -- [ ] `dotnet build` green. -- [ ] `dotnet test` green: 1046 / 8 pre-existing-baseline fail - (unchanged from main HEAD). -- [ ] At Holtburg, drop a test item on the ground (via `/drop` server - command or have ACE spawn one for the test character), then: - - [ ] Single-click the item β€” `_selectedGuid` updates, B.4b's - `[pick]` diagnostic shows the item's guid. - - [ ] Press F β€” log shows `[pickup] item=0x... container=0x5000000A - seq=N`. - - [ ] Item disappears from the ground. - - [ ] No regressions on door interaction (B.4b/B.4c still work). -- [ ] **Bonus: click-NPC verification.** While in-world, single-click - an NPC and press F (or double-click). Expected: NPC chat appears in - the chat panel. If it does β†’ M1 demo target 3 confirmed met. If not - β†’ file the gap. -- [ ] `docs/ISSUES.md` closure entry for whatever issue (if any) was - filed for the pickup gap. -- [ ] Roadmap + CLAUDE.md updated. - ---- - -## Reproducibility - -Same launch recipe as B.4c. Per CLAUDE.md "Logout-before-reconnect", -wait 20-45s between client launches to let ACE clear stale sessions. - -```powershell -Get-Process -Name AcDream.App -ErrorAction SilentlyContinue | Stop-Process -Force -Start-Sleep -Seconds 20 - -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -$env:ACDREAM_TEST_HOST = "127.0.0.1" -$env:ACDREAM_TEST_PORT = "9000" -$env:ACDREAM_TEST_USER = "testaccount" -$env:ACDREAM_TEST_PASS = "testpassword" -$env:ACDREAM_DEVTOOLS = "1" -$env:ACDREAM_PROBE_BUILDING = "1" -dotnet run --project src\AcDream.App\AcDream.App.csproj -c Debug 2>&1 | - Tee-Object -FilePath "launch-b5.log" -``` - -Log grep: - -```powershell -Select-String -Path launch-b5.log -Pattern "pickup|\[pick\] guid=|UseDone|\[B.4b\] pick" -``` - ---- - -## Carry-overs from B.4c (don't lose track) - -- **#61** β€” AnimationSequencer linkβ†’cycle frame-0 flash on door swing. - Visible as brief flap at end of swing animation. Low-severity polish. -- **#62** β€” PARTSDIAG null-guard for sequencer-driven entities. - Latent; not currently reachable for doors. One-line fix. -- **Worktree at `.claude/worktrees/phase-b4c-door-anim`** still on disk - (submodules blocked `git worktree remove` per B.4b precedent). Manual - cleanup after this session: `rm -rf` the directory + `git worktree - prune` + `git branch -D claude/phase-b4c-door-anim`. - ---- - -## State at handoff - -- **Branch:** `claude/phase-b5-pickup` (renamed from - `claude/investigate-npc-click`). -- **Worktree directory:** `.claude/worktrees/investigate-npc-click` - (cosmetic mismatch with branch name; harmless). -- **Commits ahead of main:** 1 after this handoff lands. -- **Main HEAD:** `e7842e0`. -- **Build state:** worktree compiles cleanly (verified via - `dotnet build -c Debug`). Tests at 1046/8 baseline. -- **Submodule state:** `references/WorldBuilder` initialized. - `references/ACE` NOT initialized in this worktree β€” use the main - repo's `references/ACE` for ACE source reads, or init via - `git submodule update --init --depth=1 references/ACE` if extensive - reading is needed. - ---- - -## Why a fresh session - -This session accumulated ~10 hours of context across L.2g, B.4b, and -B.4c β€” the working set is large enough that starting B.5 cold lets the -new session work with a clean context budget and avoids the compaction -risk that hit the prior B.4b session. - -The prompt for the new session is in the controller's reply that -created this handoff (the chat message immediately after this commit). diff --git a/docs/research/2026-05-13-l2d-slice1-shipped-handoff.md b/docs/research/2026-05-13-l2d-slice1-shipped-handoff.md deleted file mode 100644 index d6e2363..0000000 --- a/docs/research/2026-05-13-l2d-slice1-shipped-handoff.md +++ /dev/null @@ -1,251 +0,0 @@ -# L.2d slice 1 + 1.5 shipped β€” handoff - -**Date:** 2026-05-13 evening, immediately after slice 1.5 + Holtburg verification. -**Branch:** `claude/sharp-chatelet-023dda` (ready to merge to main). -**Predecessor:** [2026-05-12-l2a-shipped-l2d-handoff.md](2026-05-12-l2a-shipped-l2d-handoff.md). - ---- - -## TL;DR - -The "I can't walk through Holtburg doorways" symptom is **a closed Door -entity blocking the threshold**, not a building-collision-mesh bug. -Building BSP collision is healthy. The L.2a handoff's framing -("per-cell walkability missing") was wrong, the L.2d-slice-1 spec's -reframe ("BSP shape fidelity, three hypotheses X/Y/Z") was also -wrong, and the actual answer fell out of one capture once the probe -labeling was fixed (slice 1.5). **L.2d as scoped is essentially -closed.** The remaining work is door-state handling β€” a different -sub-phase entirely. - ---- - -## What shipped on this branch - -| Commit | What | -|---|---| -| [`92cd723`](.) | `docs(phys L.2d): design spec for slice 1 BSP-hit diagnostic + L.2d reframe` | -| [`66dc23e`](.) | `feat(phys L.2d slice 1): BSP-hit diagnostic probe + plan-of-record correction` | -| [`8bacef0`](.) | `fix(phys L.2d slice 1.5): probe captures hit poly under StepSphereUp recursion` | - -What slice 1 + 1.5 give the next agent: - -- **`ACDREAM_PROBE_BUILDING=1`** env var + DebugPanel checkbox: one - multi-line `[resolve-bldg]` entry per attributed BSP shadow-entry hit - (partIdx, hasPhys, bspR vs vAabbR, world-space entOrigin_lb, actual - hit polygon vertices in both local and world coords). Reliable - under `StepSphereUp` recursion after the slice 1.5 fix. -- **`[entity-source]`** one-time log line per `ShadowObjects.Register` - call, gated on the same flag. Makes `entityId=0xA9B479` in a - probe line greppable to its WorldEntity source. -- **`PhysicsDiagnostics.LastBspHitPoly`** β€” diagnostic side-channel - for any future "what poly did BSPQuery hit" question. -- **The two synthetic tests** in - [PhysicsDiagnosticsTests.cs](../../tests/AcDream.Core.Tests/Physics/PhysicsDiagnosticsTests.cs) - pin the side-channel API contract. - ---- - -## What the trace actually showed - -After slice 1.5, walking acdream into a Holtburg town doorway -captured 242 real BSP hit polys + 122 cylinder n/a. **Definitive -finding:** - -``` -live: spawn guid=0x7A9B4015 name="Door" setup=0x020019FF - pos=(132.6,17.1,94.1)@0xA9B40029 itemType=0x00000080 -[entity-source] id=0x000F4244 entityId=0x000F4244 src=0x020019FF - gfxObj=0x020019FF lb=0xA9B40029 type=Cylinder note=server-spawn-root -``` - -The blocker is a **Door entity** β€” Setup `0x020019FF` named `"Door"` β€” -server-spawned by ACE at the threshold of each Holtburg town building. -**Five Doors** appear across Holtburg (landblock cells `0xA9B40029`, -`0xA9B40154`, `0xA9B40155`); same Setup DID reused. ItemType -`0x00000080` = Misc category in AC's ItemType flags. - -Each Door's Cylinder collision blocks the player. The building BSP -*also* fires (the L.2a evidence the original handoff pointed at), but -the BSP hits were the player **already pushed back by the Door -cylinder** then grazing the doorframe β€” they look like wall collision -but are a side effect of the Door cylinder push. Slice 1.5's per-tick -multi-entity probe revealed this by showing `nObj=3` on every hit -resolve: one Door + two sphere checks against the building BSP. - -The L.2a slice 2 handoff's expectation that doors would be in the -`0xCC0Cxxxx` range was wrong; **doors are in `0x000Fxxxx`** (server- -spawn-root range) because they're hydrated through the live -`CreateObject` stream like NPCs, not the static landblock pipeline. - ---- - -## What this means for L.2d - -L.2d as originally scoped ("Shape Fidelity: Sphere / CylSphere / -Building Objects") is essentially **closed at this site**: - -- Building BSP is loaded, parsed, queried correctly. `bspR=13.99m` for - GfxObj `0x01000A2B`, real triangles in real positions. -- `Setup.CylSpheres` for Door (`0x020019FF`) is also loaded correctly - β€” the cylinder is firing the cylinder collision path with sensible - world-space radius. -- No actual shape-fidelity bug observed at this test site. - -The remaining work is **door state handling**, which is a different -class of problem entirely β€” it touches network (CreateObject -PhysicsState bits), interaction (Use action on door entity), animation -(door open/close animation state), and collision-state-toggle -(ETHEREAL during open animation). That doesn't fit under L.2d's -shape-fidelity umbrella. - -**Recommend reframing L.2d as "watch-and-wait":** keep the probes for -future shape-fidelity work at other sites (dungeon walls, stairs, -roofs), but don't plan more slices until a NEW shape-fidelity bug is -observed with the probe-armed client. - ---- - -## Side findings (latent bugs to file, not block this slice) - -### 1. Building double-registration - -The trace shows the same WorldEntity registered TWICE in -ShadowObjectRegistry: - -``` -[entity-source] id=0xA9B47900 entityId=0xC0A9B479 ... type=BSP note=partIdx=0 hasPhys=true -[entity-source] id=0xC0A9B479 entityId=0xC0A9B479 ... type=Cylinder note=mesh-aabb-fallback -``` - -[GameWindow.cs:5625](../../src/AcDream.App/Rendering/GameWindow.cs:5625) -gates the mesh-AABB-fallback on `entityBsp == 0`, but the BSP -registration at [line 5530](../../src/AcDream.App/Rendering/GameWindow.cs:5530) -DOES increment `entityBsp`. So the fallback shouldn't fire when BSP -parts exist. Either `entityBsp` isn't being checked in the right -scope, or there's a second mesh-AABB-fallback site that doesn't gate -on `entityBsp`. Worth a short investigation + one-line fix. - -Filing as ISSUE candidate. Doesn't break anything observable yet -(cylinder is too far from player to fire at this Holtburg site), but -will cause confusion in any future "why does entity X have two -ShadowEntries" trace. - -### 2. PhysicsState / EntityCollisionFlags not in entity-source log - -The slice 1 `[entity-source]` log captures `id, entityId, src, -gfxObj, lb, type, note, hasPhys` but **not** `state` (PhysicsState -bits) or `flags` (EntityCollisionFlags). For any future -ethereal-handling / IGNORE_COLLISIONS work β€” including the door -state handling above β€” these would be required. - -Tiny slice 1.6 if the next agent needs them: add `state=0x{...:X8} -flags={...}` to the format string. ~5 LOC, gated on the same -ProbeBuilding flag. - ---- - -## What the next session probably should NOT do - -- **Re-investigate Holtburg doorways with the same setup.** The - evidence is conclusive; we're not going to find new information by - re-running the probe at the same site. -- **Port `CBuildingObj` or per-cell walkability infrastructure.** - That was based on the original (wrong) hypothesis. ACE's - `find_building_collisions` is six lines and doesn't use per-cell - walkability; our equivalent is already in place implicitly. -- **Start L.2d slice 2 as scoped in the design spec.** Hypotheses X / - Y / Z don't apply β€” the trace ruled them all out. Update or close - the spec. - ---- - -## What the next session COULD do (in rough preference order) - -These are NOT prescribed; they're candidates for the project-level -ordering discussion the user wants to have. - -1. **Door state handling sub-phase.** New phase (call it L.2g or - nest under B.4). Touches: Use action β†’ server door toggle, - PhysicsState ETHEREAL bit honor, door open/close animation, - collision-shape suppression during open animation. Probably - 2-3 commits. - -2. **Fix the building double-registration latent bug** (side - finding #1). One-liner, no real impact today but cleaner trace - later. - -3. **Capture slice 1.6** (state + flags in entity-source log) if - any future ethereal-related work is on the immediate horizon. - Otherwise defer. - -4. **Move to a different L.2 sub-phase entirely** β€” L.2e - (cell ownership / `find_cell_list` / outdoor seam updates) or - L.2f (real-DAT + retail-observer conformance). Both are scoped - in [the L.2 plan-of-record](../plans/2026-04-29-movement-collision-conformance.md). - -5. **Triage the 8 pre-existing test failures** that have shadowed - the last few sessions. Some are in physics modules that L.2d - slice 2 (if it ever happens) would touch β€” fixing them first - gives a cleaner baseline. - -6. **Pick from CLAUDE.md's "Next phase candidates"** list β€” non-L.2 - work like Phase C visual fidelity, N.6 slice 2, or perf tiers - 2/3. The session-level "I don't know what to do" feeling is - often easier to resolve by **shipping something in a different - area** for a session. - ---- - -## Reproducibility - -Same recipe as L.2a + L.2d slice 1: - -```powershell -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -$env:ACDREAM_TEST_HOST = "127.0.0.1" -$env:ACDREAM_TEST_PORT = "9000" -$env:ACDREAM_TEST_USER = "testaccount" -$env:ACDREAM_TEST_PASS = "testpassword" -$env:ACDREAM_DEVTOOLS = "1" -$env:ACDREAM_PROBE_CELL = "1" -$env:ACDREAM_PROBE_RESOLVE = "1" -$env:ACDREAM_PROBE_BUILDING = "1" -dotnet run --project src\AcDream.App\AcDream.App.csproj --no-build -c Debug 2>&1 | - Tee-Object -FilePath "launch-l2d.log" -``` - -Walk acdream toward any Holtburg building threshold. Hit `Ctrl+F2` to -toggle collision wireframes β€” you'll see the Door cylinder right at -the threshold. The `name="Door"` line appears in the log at startup -during the `CreateObject` stream replay. - ---- - -## Open questions / unresolved - -- **What `PhysicsState` bits is ACE sending for the Door entity?** - Not captured in current logs. Slice 1.6 would answer this. -- **Are these doors *supposed* to be open by default in retail?** - If yes, ACE config issue. If no, retail clients see the same - blocker and players had to open them manually. -- **What does ACE's door-state state machine look like?** Probably - documented in `references/ACE/Source/ACE.Server/Entity/Door.cs` - or similar. - -These are doors-and-ACE-side questions; defer to the door-state -sub-phase when (if) it gets scoped. - ---- - -## Worktree state at handoff - -- All three slice 1 / 1.5 commits ready to merge to main. -- WorldBuilder submodule initialized + 6 directory junctions in place - for the gitignored peer reference dirs (created during slice 1 - prep). Worktree builds clean. -- Three test artifacts (`launch-l2d-slice1.log`, `launch-l2d-slice1b.log`, - `launch-l2d-slice1c.log`) are in working tree but **not committed** β€” - they're large and ephemeral. Delete or preserve at the merge - author's discretion. diff --git a/docs/research/2026-05-14-b5-shipped-handoff.md b/docs/research/2026-05-14-b5-shipped-handoff.md deleted file mode 100644 index eca0c2d..0000000 --- a/docs/research/2026-05-14-b5-shipped-handoff.md +++ /dev/null @@ -1,252 +0,0 @@ -# Phase B.5 shipped β€” handoff (visual-verified 2026-05-14) - -**Date:** 2026-05-14. -**Branch:** `claude/phase-b5-pickup` (ready to merge to main; controller handles the merge after this doc lands). -**Predecessors:** -- [docs/research/2026-05-13-b4c-shipped-handoff.md](2026-05-13-b4c-shipped-handoff.md) β€” B.4c (door swing) shipped immediately before. -- [docs/research/2026-05-13-b5-pickup-handoff.md](2026-05-13-b5-pickup-handoff.md) β€” fresh-session handoff that scoped this phase. -- [docs/superpowers/plans/2026-05-14-phase-b5-pickup.md](../superpowers/plans/2026-05-14-phase-b5-pickup.md) β€” implementation plan (2 tasks). - ---- - -## TL;DR - -Phase B.5 **shipped end-to-end and is visual-verified 2026-05-14.** The -M1 demo target *"pick up an item"* is met for the close-range path β€” -single-click a ground item to select, walk within ~0.6 m of it, press -F, and the item is removed from the world and added to the player's -inventory. - -The plan budgeted 2 implementation tasks (~50 LOC). Visual testing -surfaced **one wire-handler gap** that became Task 2b: ACE despawns -picked-up items via `GameMessagePickupEvent (0xF74A)`, not the -`GameMessageDeleteObject (0xF747)` we already handled β€” without that -fix the pickup succeeded server-side but the item kept rendering on -the ground locally. Caught and fixed in the same session. - -Two known gaps remain, filed as issues for follow-up: -- **#63 (MEDIUM)** β€” Server-initiated auto-walk for out-of-range Use / - PickUp not honored. Double-click a ground item from > 0.6 m and the - character partially walks then snaps back; ACE's `MoveToChain` times - out. This is a separate motion-handling phase, not a B.5 regression. -- **#64 (LOW)** β€” Local-player pickup animation doesn't render - (retail observers see it correctly; local view is silent). Likely a - self-echo filter dropping `UpdateMotion(Pickup)` on the local player. - ---- - -## What shipped on this branch - -| # | Commit | Subject | Task | -|---|---|---|---| -| 1 | `e8a20f2` | `feat(B.5): InteractRequests.BuildPickUp β€” PutItemInContainer 0x0019` | Task 1 | -| 2 | `ced1b85` | `test(B.5): exercise i32 sign-correctness for BuildPickUp.placement` | Task 1 code-review fix | -| 3 | `54d9bb9` | `feat(B.5): SendPickUp helper + F-key SelectionPickUp wiring` | Task 2 | -| 4 | `5c24f6c` | `docs(B.5): implementation plan from writing-plans skill` | Plan doc | -| 5 | `f7636a9` | `fix(B.5): handle PickupEvent 0xF74A so picked-up items despawn locally` | Task 2b (post-visual-test fix) | - -Plus the predecessor handoff (`86440ff`) that started the branch. - -**Build:** clean. -**Tests:** `dotnet test -c Debug` shows AcDream.Core.Net.Tests 290/290 -passing (was 287 at branch start; +3 from Task 2b's PickupEvent tests; -the two BuildPickUp tests landed inside the same project's existing -file). Failure count unchanged at 8 pre-existing baseline in -AcDream.Core.Tests. - ---- - -## What the code does end-to-end - -**Outbound (Tasks 1 & 2):** - -1. User single-clicks a ground item near `+Acdream`. - `case InputAction.SelectLeft β†’ PickAndStoreSelection(useImmediately: false)` - runs B.4b's `WorldPicker.Pick`, finds the item, sets `_selectedGuid`. - Log: `[B.4b] pick guid=0x… name=…`. -2. User presses F. - `case InputAction.SelectionPickUp β†’ SendPickUp(_selectedGuid)` builds - the wire body via `InteractRequests.BuildPickUp(seq, itemGuid, - _playerServerGuid, placement: 0)` and posts it through - `_liveSession.SendGameAction`. Log: `[B.5] pickup item=… container=… seq=…`. -3. Wire layout (24 bytes): `0xF7B1 envelope | seq | 0x0019 opcode | - itemGuid u32 | containerGuid u32 | placement i32`. Verified against - `references/ACE/Source/ACE.Server/Network/GameAction/Actions/GameActionPutItemInContainer.cs`. - -**Inbound (Task 2b β€” surfaced during visual test):** - -4. ACE runs `HandleActionPutItemInContainer`. If the player is within - `WithinUseRadius` (~0.6 m), the close-range branch in - `CreateMoveToChain` skips the auto-walk and runs the pickup chain - directly: server-side `Landblock.RemoveWorldObject(item.Guid, - adjacencyMove: false, fromPickup: true)` β†’ per-player - `Player_Tracking.RemoveTrackedObject(wo, fromPickup: true)` β†’ - broadcast `GameMessagePickupEvent (0xF74A)` to all observers. -5. Our `WorldSession.Dispatch` now routes `0xF74A` (in addition to - `0xF747 DeleteObject`) through the shared `EntityDeleted` event, - adapting the `PickupEvent.Parsed` to a `DeleteObject.Parsed` so - `OnLiveEntityDeleted β†’ RemoveLiveEntityByServerGuid` runs unchanged. - The item disappears from the local view. - ---- - -## Wire-handler gap (Task 2b) - -ACE distinguishes two despawn opcodes: -- `0xF747 GameMessageDeleteObject` β€” "object is gone" (timeout / death / - out-of-LOS). Our existing handler. -- `0xF74A GameMessagePickupEvent` β€” "object was picked up by a player." - Sent by `Player_Tracking.RemoveTrackedObject(wo, fromPickup: true)`. - -Both are functionally identical from the client's view (remove the -entity from the world), but only one was handled. Wire format adds -one `u16 objectPositionSequence` field over DeleteObject's layout, so -`PickupEvent.cs` is its own parser; the dispatcher adapts to -`DeleteObject.Parsed` for the downstream consumer. - -This is exactly the kind of trap CLAUDE.md's reference-repo discipline -exists to prevent β€” the handoff spec said "the existing despawn path -removes the ground item from view," which was *almost* true. Took one -visual-verification round-trip to surface, ten minutes to fix with a -clean wire parser + 3 new unit tests. - ---- - -## Visual verification β€” what was observed - -**Test scenario:** ACE dropped a Pink Taper, then a Violet Taper, then -two more tapers near `+Acdream` at Holtburg. Player walked up close, -single-clicked, pressed F. Three pickups completed in the post-fix -log: items `0x80000725`, `0x8000072A`, `0x80000729`. - -**Before Task 2b:** Server-side pickup succeeded β€” `[B.5] pickup … -seq=46` in log; retail observer saw item disappear from world. Local -view still rendered the item on the ground. - -**After Task 2b:** Item disappears locally as soon as ACE acks the -pickup. Three successful close-range pickups recorded in the log. - -**Door-interaction regression check (B.4c carry-forward):** Not -explicitly re-tested this session; no code path touched by B.5 -affects door interaction. - -**Click-NPC bonus (M1 demo target 3 verification):** Not visually -verified this session β€” log shows `[B.4b] use guid=… name=Novedion -the Gem Seller seq=…` from B.4c testing but ACE response not -re-confirmed here. Carry-forward to next session. - ---- - -## What did NOT work (and why it's not B.5's bug) - -1. **Double-click on a ground item from any distance, or F from > 0.6 m.** - ACE auto-walks the player toward the item (`CreateMoveToChain` β†’ - `PhysicsObj.MoveToObject` + `EnqueueBroadcastMotion(MoveToObject)`), - but our client doesn't handle inbound `MoveToObject` motion broadcasts. - ACE's MoveToChain times out, the chain's `success: false` path sends - `InventoryServerSaveFailed (ActionCancelled)`, and the pickup never - completes. Visible as "character drifts toward item then flips back." - **Filed as #63.** Out of B.5's stated scope (which was: select-first - + F-key wire chain). holtburger's `simulation.rs` has the reference - implementation; would be its own phase (B.6 or similar). - -2. **Local-player pickup animation doesn't render.** Retail observers - see `+Acdream` play the bend-down-and-grab animation; our local view - shows nothing. ACE broadcasts `Motion(MotionCommand.Pickup)` via - `EnqueueBroadcastMotion`, our motion routing probably filters - self-echoes for the local player (motion is normally predicted - locally, not echoed from server). Server-initiated one-shot motions - like Pickup have no local prediction trigger, so they're dropped. - **Filed as #64.** Visual feedback gap only; pickup completes - correctly. - -Both are well-defined follow-up work; neither blocks M1. - ---- - -## Carry-overs from B.4c - -Both pre-existed B.5; neither was touched. - -- **#61** β€” AnimationSequencer linkβ†’cycle boundary frame-0 flash on - door swing. Low severity polish. -- **#62** β€” PARTSDIAG null-guard for sequencer-driven entities. - Latent; not currently reachable for doors. - ---- - -## M1 status after B.5 - -Demo targets: -1. Walk through Holtburg β€” met (L.2a-d + L.2g shipped earlier) -2. Open the inn door β€” met (B.4b + B.4c shipped 2026-05-13) -3. Click an NPC β€” chain wired (B.4b), not visually re-verified this - session -4. Pick up an item β€” met, close-range path (this phase) - -Outstanding work for the M1 demo recording: -- Optionally re-verify target 3 (NPC click) once and either confirm - met or file a gap. -- Optionally resolve #63 if the demo wants to show double-click / - out-of-range pickup. The close-range path is sufficient for the - scripted demo scenario. -- Carry-overs #61, #62, #64 are polish; do before recording if - visible on tape. - ---- - -## Reproducibility - -```powershell -Get-Process -Name AcDream.App -ErrorAction SilentlyContinue | Stop-Process -Force -Start-Sleep -Seconds 20 - -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -$env:ACDREAM_TEST_HOST = "127.0.0.1" -$env:ACDREAM_TEST_PORT = "9000" -$env:ACDREAM_TEST_USER = "testaccount" -$env:ACDREAM_TEST_PASS = "testpassword" -$env:ACDREAM_DEVTOOLS = "1" -dotnet run --project src\AcDream.App\AcDream.App.csproj -c Debug 2>&1 | - Tee-Object -FilePath "launch-b5.log" -``` - -Log evidence: - -```powershell -Get-Content launch-b5.log -Encoding Unicode | - Select-String -Pattern "\[B\.5\] pickup|\[B\.4b\] pick" -``` - -Expected: a `[B.5] pickup item=… container=0x5000000A seq=…` line for -each successful F-press, preceded by `[B.4b] pick guid=…` from the -single-click that set the selection. - ---- - -## Files touched this session - -- New: `src/AcDream.Core.Net/Messages/PickupEvent.cs` -- New: `tests/AcDream.Core.Net.Tests/Messages/PickupEventTests.cs` -- New: `docs/superpowers/plans/2026-05-14-phase-b5-pickup.md` -- New: `docs/research/2026-05-14-b5-shipped-handoff.md` (this file) -- Modified: `src/AcDream.Core.Net/Messages/InteractRequests.cs` -- Modified: `src/AcDream.Core.Net/WorldSession.cs` -- Modified: `src/AcDream.App/Rendering/GameWindow.cs` -- Modified: `tests/AcDream.Core.Net.Tests/Messages/InteractRequestsTests.cs` -- Modified: `docs/ISSUES.md` (added #63, #64) - ---- - -## State at handoff - -- **Branch:** `claude/phase-b5-pickup`, 6 commits ahead of `main` - (predecessor handoff + 5 implementation commits + this docs commit - land in the same merge). -- **Main HEAD before merge:** `e7842e0` β€” Merge B.4c. -- **Build state:** worktree compiles cleanly under `dotnet build -c Debug`. -- **Tests:** baseline + 3 new (PickupEvent) + 2 new (BuildPickUp + - sign-correctness) β€” failure count unchanged. - -Ready for non-fast-forward merge into `main`. diff --git a/docs/research/2026-05-15-b6-b7-shipped-handoff.md b/docs/research/2026-05-15-b6-b7-shipped-handoff.md deleted file mode 100644 index 5d99184..0000000 --- a/docs/research/2026-05-15-b6-b7-shipped-handoff.md +++ /dev/null @@ -1,382 +0,0 @@ -# Phase B.6 + B.7 + WorldPicker tightening β€” handoff (visual-verified 2026-05-15) - -**Date:** 2026-05-15 (session 06:08–18:21). -**Branch:** commits live on `main` from `cf22f9c..e49c704` (36 commits). -**Predecessors:** -- [docs/research/2026-05-14-b5-shipped-handoff.md](2026-05-14-b5-shipped-handoff.md) β€” B.5 (pickup) shipped immediately before. -- [docs/superpowers/specs/2026-05-14-phase-b6-design.md](../superpowers/specs/2026-05-14-phase-b6-design.md) β€” B.6 design with retail anchors + trace findings + 4-slice plan. -- [docs/superpowers/specs/2026-05-15-phase-b7-target-indicator-design.md](../superpowers/specs/2026-05-15-phase-b7-target-indicator-design.md) β€” B.7 design (Vivid Target Indicator). - ---- - -## TL;DR - -Three coupled improvements shipped end-to-end this session: - -- **Phase B.6 β€” Local-player auto-walk on inbound `MoveToObject` (issue #63 OPEN β†’ working).** When the user double-clicks a far target or presses R/F on an out-of-range target, ACE sends `UpdateMotion (0xF74D)` with `MovementType=6` carrying the destination guid. We now synthesize `Forward+Run` input into `PlayerMovementController` to walk the body to the target, then fire the deferred Use/PickUp once the position has arrived AND the body has rotated to face. Smooth rotation (no snap), dual alignment thresholds (30Β° walk-while-turning, 5Β° fully aligned). 10 Hz position heartbeat while moving keeps ACE's server-side `WithinUseRadius` poll converging fast enough that doors / NPCs / items all complete the action. - -- **Phase B.7 β€” Vivid Target Indicator (MVP).** Four small corner triangles drawn around the selected entity, colour-coded by entity type using a port of `gmRadarUI::GetBlipColor` (`0x004d76f0`). Box size scales with projected entity height Γ— scale, per-type base height (humanoid 1.8 m, door/lifestone/portal 2.4 m, small item 0.8 m, default 1.5 m). Drawn via ImGui background draw list β€” no new GL infrastructure. **Selection bug is now self-correcting**: the user can see *what* they actually clicked before pressing R/F. - -- **WorldPicker tightening (#59 closed).** 5 m fixed sphere radius β†’ 0.7 m default β†’ 1.0 m default with 0.9 m vertical offset (chest-height sphere centre). Per-entity radius/offset callbacks let doors / lifestones / portals get bigger spheres (1.5–2.0 m) and small items get tighter ones (0.4 m). - -The M1 demo target *"click an NPC"* + *"open the inn door"* is now reachable from ANY range (close-range or far-range). The visual flow matches retail: you click β†’ indicator appears β†’ you press R β†’ if far, character walks to within use radius, turns to face, action fires; if near, character turns to face, action fires. - -**Honest faithfulness note (user-requested audit).** Several workarounds remain β€” arrival safety margin, deferred-wire-Use packet, AutonomousPosition flush on arrival, retry flag β€” all rooted in our 1 Hz position heartbeat. The 10 Hz bump retires the worst of them in practice. Per-tick outbound (or fixing whatever causes ACE to lose our position between heartbeats) would retire ALL of them. Documented below in **Workaround retirement plan**. - ---- - -## What shipped on this branch (36 commits) - -Ordered oldest β†’ newest. Each commit subject is its own retail-faithful unit; the "fix(B.6+B.7)" pairing means a single workaround serves both phases. - -| # | Commit | Subject | -|---|---|---| -| 1 | `87ba5c9` | `feat(B.5): pickup feedback chat line + toast ("You pick up the X.")` | -| 2 | `7be1393` | `docs(M1): record all 4 demo targets met, list deferred polish` | -| 3 | `20ecb23` | `Revert "feat(B.5): pickup feedback chat line + toast …"` β€” retail-faithful: no feedback. | -| 4 | `a01ebd5` | `fix(B.5): block pickup of creatures client-side; show 'Can't pick that up' toast` | -| 5 | `ab7c04f` | `docs(M1): reflect chat/toast revert + the actual B.5 polish (creature pickup guard)` | -| 6 | `e55ad48` | `fix(B.5): make creature-pickup guard silent (retail-faithful)` | -| 7 | `ec9fd52` | `fix #62: null-guard the PARTSDIAG read of ae.Animation` | -| 8 | `5053e40` | `docs: close #62 β€” PARTSDIAG null-guard landed in ec9fd52` | -| 9 | `281d125` | `docs(B.6): design spec for local-player MoveToObject auto-walk (issue #63)` | -| 10 | `9e1d33a` | `docs(B.6): retail decomp settles Option A; revise spec with 4-slice plan` | -| 11 | `eda8278` | `feat(B.6 slice 1): ACDREAM_PROBE_AUTOWALK diagnostic baseline` | -| 12 | `1b4f3ba` | `feat(B.6 slice 1): DebugPanel mirror for ProbeAutoWalk checkbox` | -| 13 | `d82b064` | `docs(B.6): record Slice 1 trace findings β€” ACE sends mtRun=0.00, no UP echo` | -| 14 | `b936ef8` | `feat(B.6 slice 2): local-player auto-walk on inbound MoveToObject` β€” core feature lands. | -| 15 | `f18de7c` | `fix(B.6 slice 2): don't cancel autowalk on the companion InterpretedMotionState` | -| 16 | `5612ce7` | `feat(B.6): honor wire WalkRunThreshold β€” walk vs run per retail semantics` | -| 17 | `37177a4` | `docs(B.7): design spec for Vivid Target Indicator (selection feedback)` | -| 18 | `8544a78` | `feat(B.7): RadarBlipColors β€” port of gmRadarUI::GetBlipColor` | -| 19 | `c7e5f9f` | `feat(B.7): TargetIndicatorPanel β€” corner triangles around selected entity` | -| 20 | `4bc95ec` | `fix(B.7): scale indicator box from projected entity height, not fixed pixels` | -| 21 | `5e29773` | `fix #59: tighten WorldPicker radius from 5 m to 0.7 m` | -| 22 | `631571a` | `docs: close #59 β€” picker radius tightened in 5e29773` | -| 23 | `23cb1e9` | `fix(B.7): square indicator box + bigger pick sphere for doors/lifestones/portals + diag` | -| 24 | `1a0656a` | `fix(picker): lift sphere centre to mid-body so chest/head clicks hit` | -| 25 | `211fe24` | `fix(B.6+B.7): run-all-the-way auto-walk, per-type indicator height, R = smart interact` | -| 26 | `5f83766` | `docs: file #65 β€” local player doesn't turn to face on close-range Use` | -| 27 | `2dc28bb` | `fix(B.6+B.7): re-send action on local arrival; scale indicator box by entity Scale` | -| 28 | `a0fa3d6` | `fix(B.6+B.7): flush AutonomousPosition on arrival before re-sending action` | -| 29 | `39ff3a5` | `fix(B.6+B.7): arrival predicate uses safety margin INSIDE ACE's WithinUseRadius` | -| 30 | `64c9793` | `fix(B.6+B.7): shrink arrival safety margin; file #66 rotation, #67 door` | -| 31 | `301281d` | `fix(B.6+B.7): bump AutonomousPosition heartbeat 1Hz -> 10Hz while moving` ← **single biggest fix** | -| 32 | `32352af` | `fix(B.6): turn-first auto-walk + tiny margin; close #67 doors; file #68 remote arrival` | -| 33 | `5b908bc` | `fix(B.6): close-range turn-to-face β€” install overlay on Use/PickUp send` | -| 34 | `cffb10f` | `fix(B.6): tighter 5Β° alignment + defer Use until rotation completes; file #69 turn anim` | -| 35 | `7158c46` | `fix(B.6): smooth local rotation β€” remove 20Β° snap-on-approach (not retail)` | -| 36 | `e49c704` | `fix(B.6): speculative auto-walk uses WalkRunThreshold=15 to match ACE` | - -**Build:** clean. -**Tests:** `dotnet test -c Debug` shows the new RadarBlipColors tests (8) and the existing B.5 BuildPickUp tests passing. Failure count unchanged at the 8 pre-existing baseline in `AcDream.Core.Tests`. - ---- - -## Wire-format facts (what ACE sends, what we parse) - -| Wire | Field | Value | Our handling | -|---|---|---|---| -| `UpdateMotion (0xF74D)` | `MovementType` | `6` MoveToObject | Local: `BeginServerAutoWalk(...)` + speculative turn overlay. Remote: existing `RemoteMoveToDriver`. | -| `UpdateMotion (0xF74D)` | `MovementType` | `7` MoveToPosition | Local: `BeginServerAutoWalk(...)` with a synthetic guid (positional destination only). | -| `UpdateMotion (0xF74D)` | `MovementType` | `8` TurnToObject | **NOT YET PARSED** β€” issue #66. ACE sends this on close-range Use against an off-facing target. Our parser falls into the locomotion path and silently drops the rotation. | -| `UpdateMotion (0xF74D)` | `MovementType` | `0` Interpreted | Companion locomotion echo after a MovementType=6 (RunForward command). We do NOT treat this as a cancel signal (commit f18de7c). | -| `UpdateMotion (0xF74D)` | `WalkRunThreshold` | float meters | If `distance > threshold` β†’ run, else walk. ACE default `15.0 m`. We honor it (commit 5612ce7) for inbound; we use it as the speculative-overlay walk/run gate too (commit e49c704). | -| `GameAction (0xF7B1)` outbound | `0x0036 Use` | guid | Sent on R-key / double-click + close-range. For far-range we install the speculative overlay and defer the wire packet until arrival (commit cffb10f). | -| `GameAction (0xF7B1)` outbound | `0x0019 PutItemInContainer` | item guid + container guid + placement | Sent on F-key. Same defer-on-far-range pattern. | -| `AutonomousPosition (0xF7B1 0x0007)` outbound | position + heading | every 1 Hz idle / **10 Hz while moving** | The 10 Hz bump (commit 301281d) is what unblocks doors (#67) and lets ACE's `MoveToChain` see us arrive at the use radius without timing out. | - -**Retail anchors for the above:** -- `MovementManager::PerformMovement` at `0x00524440` β€” dispatch switch on MovementType. -- `MoveToManager::HandleMoveToObject` β€” MovementType=6 driver (turn-to-face β†’ walk β†’ stop). -- `MoveToManager::HandleMoveToPosition` β€” MovementType=7 driver. -- `MoveToManager::HandleTurnToHeading` at `0x0052a0c0` β€” turn-only driver used by MovementType=8. -- `CPhysicsObj::MoveToObject` at `0x00512860` β€” high-level entry from physics. -- `Player_Move.CreateMoveToChain` (ACE) at `Player_Move.cs:37–179` β€” server-side state machine that depends on our heartbeat to detect arrival. - ---- - -## Local auto-walk state machine (current shape) - -`src/AcDream.App/Input/PlayerMovementController.cs`: - -```csharp -// State -private bool _autoWalkActive; -private Vector3 _autoWalkDestination; -private float _autoWalkMinDistance; // ACE's WithinUseRadius (per-type) -private float _autoWalkDistanceToObject; // initial distance, used for run/walk decision -private bool _autoWalkMoveTowards; -private bool _autoWalkInitiallyRunning; // decided ONCE at Begin - -public event Action? AutoWalkArrived; // GameWindow re-sends Use/PickUp on this - -// Per-frame overlay, called from top of Update -private MovementInput ApplyAutoWalkOverlay(float dt, MovementInput input) -{ - if (!_autoWalkActive) return input; - - // User-input override β†’ cancel - if (input.Forward || input.Back || input.StrafeL || input.StrafeR) - { - EndServerAutoWalk("user-input"); - return input; - } - - // Compute delta yaw, distance, alignment - Vector3 toTarget = _autoWalkDestination - Position; - float dist = toTarget.Length(); - float targetYaw = MathF.Atan2(toTarget.Y, toTarget.X) - MathF.PI / 2f; - float delta = NormalizeAngle(targetYaw - Yaw); - - // SMOOTH rotation (commit 7158c46 β€” no snap) - float maxStep = RemoteMoveToDriver.TurnRateRadPerSec * dt; - Yaw += MathF.Sign(delta) * MathF.Min(MathF.Abs(delta), maxStep); - - // Dual alignment thresholds (commit cffb10f) - const float WalkWhileTurningRad = 30f * MathF.PI / 180f; - const float FullyAlignedRad = 5f * MathF.PI / 180f; - bool walkAligned = MathF.Abs(delta) <= WalkWhileTurningRad; - bool aligned = MathF.Abs(delta) <= FullyAlignedRad; - - // Arrival predicate uses TIGHT 0.05 m safety margin INSIDE ACE's radius - // (commit 39ff3a5 + 64c9793; works because of 10 Hz heartbeat from 301281d) - bool withinArrival = dist <= (_autoWalkMinDistance - 0.05f); - - if (withinArrival && aligned) - { - EndServerAutoWalk("arrived"); // fires AutoWalkArrived event - return input; - } - - bool moveForward = walkAligned && !withinArrival; - return input with - { - Forward = moveForward, - Run = moveForward && _autoWalkInitiallyRunning, - // Strafes left clear so we don't combine with other input - }; -} -``` - -`src/AcDream.App/Rendering/GameWindow.cs`: - -```csharp -// Wired in ctor: -_playerController.AutoWalkArrived += OnAutoWalkArrivedReSendAction; - -private (uint Guid, bool IsPickup)? _pendingPostArrivalAction; - -// On Use/PickUp send: install speculative overlay + defer wire packet if close -private void SendUse(uint guid, bool isRetryAfterArrival = false) -{ - if (!isRetryAfterArrival) - { - InstallSpeculativeTurnToTarget(guid); // BeginServerAutoWalk with tiny radius - _pendingPostArrivalAction = (guid, false); - if (IsCloseRangeTarget(guid)) - return; // wire packet deferred until arrival - } - // ... build + send 0xF7B1/0x0036 -} - -private void OnAutoWalkArrivedReSendAction() -{ - if (_pendingPostArrivalAction is not (uint guid, bool isPickup)) return; - _pendingPostArrivalAction = null; - SendAutonomousPositionNow(); // flush position so ACE sees us at radius - if (isPickup) SendPickUp(guid, isRetryAfterArrival: true); - else SendUse(guid, isRetryAfterArrival: true); -} -``` - ---- - -## Picker (current shape) - -`src/AcDream.Core/Selection/WorldPicker.cs`: - -- `DefaultRadius = 1.0f` (up from 0.7 m to compensate for vertical-offset lift, commit 1a0656a). -- `DefaultVerticalOffset = 0.9f` (chest-height humanoid mid-body β€” fixes the bug where clicking the head/chest of an NPC missed because the sphere was at the feet). -- Per-entity callbacks (`radiusForGuid`, `verticalOffsetForGuid`) supplied by `GameWindow`: - - Doors / lifestones / portals: **radius 1.5–2.0 m, vertical offset 1.2 m** (commit 23ce1e9). - - Small dropped items (BF_ITEM-class): **radius 0.4 m, vertical offset 0.1 m** (item lies on ground). - - Default (NPC / creature / sign / other): defaults 1.0 m / 0.9 m. -- Inside-sphere origin handled (commit 5821bdc, pre-session): if `t_near < 0` use `t_far` so the entity is still pickable at point-blank range. - ---- - -## Target indicator (current shape) - -`src/AcDream.App/UI/TargetIndicatorPanel.cs` + `src/AcDream.Core/Ui/RadarBlipColors.cs`: - -- `TargetInfo(WorldPosition, ItemType, ObjectDescriptionFlags, Scale)` record carries the inputs. -- Box height = `EntityHeightFor(itemType, pwdBitfield, scale)`: - - Creature (NPC / monster / player): **1.8 m Γ— scale** (humanoid baseline). - - Door / Lifestone / Portal (BF_DOOR=0x1000 | BF_LIFESTONE=0x4000 | BF_PORTAL=0x40000): **2.4 m Γ— scale** (door-frame tall). - - Small carry items (Weapon | Armor | Clothing | Jewelry | Food | Money | Misc | MissileWeapon | Container | Gem | SpellComponents | Writable | Key | Caster): **0.8 m Γ— scale**. - - Default (signs, scenery interactables, untyped): **1.5 m Γ— scale**. ⚠️ User reports signs still feel too small β€” see follow-up below. -- Box is **square** (`WidthHeightRatio = 1.0`, matches retail) β€” width = height. -- Projection: project feet + head world points to screen, draw 4 right-angle triangles at corners via ImGui background draw list. -- Min screen height clamp: 16 px (prevents collapse on far entities). -- Off-screen / behind-camera: returns early; Β±20% NDC margin so a tall entity whose feet are just off-screen still gets head projected. -- Colour: from `RadarBlipColors.For(itemType, pwdBitfield)`. Port of `gmRadarUI::GetBlipColor (0x004d76f0)` β€” Portal β†’ Vendor β†’ Creature (yellow) β†’ PlayerKiller (red) β†’ PKLite β†’ FriendlyPlayer β†’ default Item (white-ish). -- 8 unit tests in `tests/AcDream.Core.Tests/Ui/RadarBlipColorsTests.cs`. - -**MVP scope β€” explicitly deferred (per spec Β§3):** off-screen edge arrow (`m_pOffScreen`), DAT-loaded triangle sprite (today's are procedural), mesh-tint highlight on the target, player-option toggle. - ---- - -## Faithfulness audit (user-requested honest comparison) - -This was the most important conversation thread of the session. User asked: *"How faithful are we to retail in this?"* and pushed back on every workaround I'd introduced. Direct answer: **The data path is retail-faithful, the timing isn't, and the workarounds are our bugs not ACE's.** - -| Piece | What retail did | What we do | Why we diverged | Resolution path | -|---|---|---|---|---| -| MoveToObject wire parse | `MovementManager::PerformMovement` switch on `MovementType` | We parse 6/7, miss 8 | Incremental delivery β€” B.6 scope didn't include TurnToObject | **Issue #66** β€” port MovementType=8 | -| Local turn-to-face | Smooth interpolation animation (legs+arms cycle while body pivots) | Smooth Yaw step; no animation cycle (statue-pivot) | Motion interpreter not fed TurnLeft/TurnRight when overlay turns | **Issue #69** β€” synthesize TurnLeft/TurnRight | -| Arrival predicate | Exact: stop when `dist ≀ radius` | `dist ≀ radius βˆ’ 0.05 m` safety margin | Our client+server position drift would let retail's exact predicate fall through. 1 Hz heartbeat was the root cause; with 10 Hz it's mostly redundant. | **Drop the margin** once per-tick outbound lands. | -| Action send timing | Once on player intent | Twice: once on intent (deferred if far) + once on arrival | Our `MoveToChain` poll on ACE side races against our position heartbeat. With 1 Hz we always lost. 10 Hz helps. | **Single-send** once per-tick outbound + a server-side action queue replaces the retry. | -| AutonomousPosition flush | Continuous broadcast | One forced AP on arrival + 10 Hz during move | Same root cause: ACE polls at 0.1 s, we broadcast at 1 s default. | **Per-tick outbound** retires this entirely. | -| Local-player TurnToObject | Server broadcasts MovementType=8; client rotates body | We drop the wire MovementType=8 | Incremental delivery β€” B.6 was scoped to MovementType=6 only | **Issue #66** β€” same fix as the parse gap above. | -| Remote-player arrival animation | Client detects arrival from wire's distance threshold and transitions cycle | RemoteMoveToDriver `Arrived` state set but consumer doesn't flip cycle | Cycle-routing layer never wired to the arrival event | **Issue #68** β€” add `SetCycle(NonCombat, Ready)` on arrival. | -| Local-player pickup animation | Server-initiated `Motion(Pickup)` broadcast β†’ animates locally | Self-echo filter drops it | Pre-existing (B.5) | **Issue #64** β€” admit server-initiated one-shots through the filter. | - -**Bottom line: every workaround in this list traces to the position heartbeat or the missing MovementType=8 path. Neither is "ACE's bug" β€” they are gaps in our client.** - ---- - -## Open follow-up issues filed this session - -| ID | Severity | Summary | -|---|---|---| -| **#66** | LOW-MED | Local + remote rotation flip-back / NPCs don't turn β€” port MovementType=8 TurnToObject | -| **#68** | LOW-MED | Remote players' running animation doesn't stop on auto-walk arrival | -| **#69** | LOW | Local player rotation isn't animated (statue-pivot vs leg-shuffle) β€” synthesize TurnLeft/TurnRight | -| **#65** | LOW | Local-player no turn-to-face on close-range Use β€” superseded by #66 | - -**Closed this session:** -- **#59** β€” `WorldPicker` 5 m over-pick β†’ 1.0 m + vertical offset (`5e29773`, `1a0656a`, `23ce1e9`). -- **#62** β€” PARTSDIAG null-guard for sequencer-driven entities (`ec9fd52`). -- **#67** β€” Door Use action doesn't complete after auto-walk arrival β†’ fixed by 10 Hz heartbeat (`301281d`). - -**Visual gripe still open (not filed as an issue yet):** signs still feel too small. Default 1.5 m Γ— scale should probably be 2.5 m for sign-class objects β€” they're tall posts in retail. Wiring is there (just bump the constant in `EntityHeightFor` for the "everything else" branch, or add a sign-detection rule). User's most recent feedback before context-out was *"still when I select a sign the box is way to small."* - ---- - -## Reproducibility β€” how to verify the shipped behaviour - -```powershell -# From C:\Users\erikn\source\repos\acdream -Stop-Process -Name AcDream.App -ErrorAction SilentlyContinue -Start-Sleep -Seconds 3 - -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -$env:ACDREAM_TEST_HOST = "127.0.0.1" -$env:ACDREAM_TEST_PORT = "9000" -$env:ACDREAM_TEST_USER = "testaccount" -$env:ACDREAM_TEST_PASS = "testpassword" - -# Optional diagnostic env vars (heavy) -# $env:ACDREAM_PROBE_AUTOWALK = "1" # one [autowalk] line per MoveToObject inbound + transition -# $env:ACDREAM_DUMP_MOTION = "1" - -dotnet build -c Debug; if ($?) { - dotnet run --project src\AcDream.App\AcDream.App.csproj --no-build -c Debug 2>&1 | - Tee-Object -FilePath "launch.log" -} -``` - -**Test scenarios (each verified visually during the session):** - -1. **Far-range Use on NPC.** Stand 5–10 m from Tirenia in the Holtburg inn. Click NPC β†’ corner triangles appear (yellow). Press R. Character runs to within ~3 m, decelerates, turns to face, Use fires, dialogue appears. -2. **Far-range pickup on item.** Stand 5–10 m from a dropped taper. Click item β†’ corner triangles appear (white-ish). Press F. Character runs to within ~0.6 m, turns to face, PickUp fires, item despawns, inventory updates. -3. **Door open.** Stand 3–5 m from the inn front door. Click door β†’ corner triangles appear (white-ish, scaled to 2.4 m Γ— scale). Press R. Character walks to within ~2 m, turns to face, Use fires, ACE broadcasts `SetState (ETHEREAL)`, character walks through. -4. **Close-range Use.** Already within ~1 m of an NPC, facing away. Press R. Character turns to face β†’ Use fires β†’ dialogue. (Close-range branch β€” exercises `IsCloseRangeTarget` + deferred-wire-packet path.) -5. **Far-range double-click on item.** Same as (2) but double-click β€” should behave identically to F-key (double-click activation passes through `OnInputAction` after `58b95bc`). - -**Diagnostic env vars active for this work:** -- `ACDREAM_PROBE_AUTOWALK=1` β€” one `[autowalk]` line per inbound MoveToObject + state transition (commit `eda8278`). Also toggleable via DebugPanel. -- `ACDREAM_DUMP_MOTION=1` β€” every inbound `UpdateMotion` (guid, stance, cmd, speed). -- `ACDREAM_REMOTE_VEL_DIAG=1` β€” `[UPCYCLE]` traces for remote-arrival debug (relates to #68). - ---- - -## Files touched this session - -**New files:** -- `src/AcDream.Core/Ui/RadarBlipColors.cs` β€” colour table port. -- `src/AcDream.App/UI/TargetIndicatorPanel.cs` β€” corner-triangle renderer. -- `tests/AcDream.Core.Tests/Ui/RadarBlipColorsTests.cs` β€” 8 unit tests. -- `docs/superpowers/specs/2026-05-14-phase-b6-design.md` β€” B.6 design + 4-slice plan. -- `docs/superpowers/specs/2026-05-15-phase-b7-target-indicator-design.md` β€” B.7 design. - -**Modified:** -- `src/AcDream.App/Input/PlayerMovementController.cs` β€” auto-walk overlay, smooth rotation, dual alignment, 10 Hz heartbeat. -- `src/AcDream.App/Rendering/GameWindow.cs` β€” `SendUse`/`SendPickUp` defer logic, `_pendingPostArrivalAction`, `OnAutoWalkArrivedReSendAction`, `InstallSpeculativeTurnToTarget`, `IsCloseRangeTarget`, `SendAutonomousPositionNow`, `TargetIndicatorPanel` wiring, per-entity picker callbacks, `UseCurrentSelection` smart-R dispatch. -- `src/AcDream.Core/Selection/WorldPicker.cs` β€” radius/vertical-offset callbacks, inside-sphere origin handling documented. -- `src/AcDream.Core.Net/WorldSession.cs` β€” MovementType=6 routing into local auto-walk path. -- `src/AcDream.Core/Physics/PhysicsDiagnostics.cs` β€” `ProbeAutoWalkEnabled` static property, `ACDREAM_PROBE_AUTOWALK` env var. -- `src/AcDream.UI.Abstractions/Panels/Debug/DebugVM.cs` + `DebugPanel.cs` β€” DebugPanel checkbox mirror. -- `docs/ISSUES.md` β€” closed #59, #62, #67; filed #65, #66, #68, #69; updated #63 with B.6 status. -- `docs/plans/2026-05-12-milestones.md` β€” M1 four-of-four status reflected. -- `CLAUDE.md` β€” updated "Currently in Phase…" line with B.6/B.7 ship facts. - ---- - -## Workaround retirement plan - -The four workarounds that should NOT survive a per-tick-outbound + MovementType=8 phase: - -1. **Arrival safety margin (currently 0.05 m).** `ApplyAutoWalkOverlay` stops at `dist <= _autoWalkMinDistance - 0.05f`. Retail stops at `dist <= radius`. Drop the margin when our outbound position is fresh enough that ACE's `WithinUseRadius` poll always sees us inside the radius the moment we get there. -2. **Re-send on arrival.** `_pendingPostArrivalAction` + `OnAutoWalkArrivedReSendAction` re-fire `SendUse`/`SendPickUp` after the body arrives. Retail's client sends the action once and lets the server-side `MoveToChain` complete. Drop when ACE consistently completes the chain from a single send. -3. **AutonomousPosition flush on arrival.** `SendAutonomousPositionNow()` explicitly broadcasts position the moment we arrive. With per-tick outbound this happens naturally. -4. **`isRetryAfterArrival` flag.** Branch in `SendUse`/`SendPickUp` to skip the speculative-overlay install on the retry. Goes away when the retry goes away. - -**Single fix that retires all four:** per-tick outbound position broadcast (probably at the physics tick rate of 60 Hz with a smaller payload, or 20–30 Hz with the full one). Currently `effectiveInterval = activelyMoving ? 0.1f : 1.0f` (10 Hz active / 1 Hz idle). Going to 20–30 Hz active would likely close the gap; per-tick is the upper bound. - -**Reference for retail's outbound cadence:** `docs/research/named-retail/` β€” search for `CPhysicsObj::send_movement_event` and `AutonomousPosition` send-site. Holtburger's `client/movement/system.rs` also sends at higher cadence than our default. - ---- - -## Next-session entry points (in rough priority order) - -1. **Fix the sign indicator box.** User's last gripe. Bump `EntityHeightFor` default from 1.5 m to ~2.5 m, or add an explicit sign-detection rule. ~5 LOC. Verify in Holtburg by selecting one of the inn signs. -2. **Issue #66 β€” MovementType=8 TurnToObject (local + remote).** Two-direction fix: stop local-player flip-back AND make NPCs turn to face. ~80–120 LOC + tests. Spec template is the B.6 spec with MovementType=8 substituted. -3. **Issue #69 β€” animate rotation.** Synthesize `TurnLeft`/`TurnRight` input flags while the overlay turns the body. ~30 LOC in `ApplyAutoWalkOverlay` + verify retail's human motion table has the cycle. Pairs with #66 nicely. -4. **Issue #68 β€” Remote players don't stop run animation on arrival.** Wire `RemoteMoveToDriver.Arrived` to `SetCycle(NonCombat, Ready)`. ~20 LOC. Small standalone fix. -5. **Issue #64 β€” Local-player pickup animation.** Pre-existing B.5 gap; the self-echo filter drops `UpdateMotion(Pickup)`. Either (a) admit server-initiated one-shots through the filter, or (b) generate locally on send. -6. **Per-tick outbound position broadcast.** The big one. Retires the four B.6 workarounds and probably fixes a class of "ACE doesn't see us" bugs we haven't even noticed yet. Probably its own design phase (call it B.8 or M.x). Read `docs/research/named-retail/` for retail's cadence first. -7. **Investigate the running-in-circles bug.** User reported during B.6 slice 2 testing that auto-walk would occasionally "run in circles" before going straight. The fix in `211fe24` (run-all-the-way) appears to have fixed it but no regression test exists. Worth a one-session investigation with `ACDREAM_PROBE_AUTOWALK=1`. - ---- - -## Predecessor reading order for a fresh session - -1. **This document** β€” the full picture of what's in main. -2. [`docs/superpowers/specs/2026-05-14-phase-b6-design.md`](../superpowers/specs/2026-05-14-phase-b6-design.md) β€” retail anchors + decomp citations for auto-walk. -3. [`docs/superpowers/specs/2026-05-15-phase-b7-target-indicator-design.md`](../superpowers/specs/2026-05-15-phase-b7-target-indicator-design.md) β€” B.7 design + deferred-MVP list. -4. [`docs/research/2026-05-14-b5-shipped-handoff.md`](2026-05-14-b5-shipped-handoff.md) β€” B.5 (pickup, close-range path) preceded this work. -5. [`docs/research/2026-05-13-b4b-shipped-handoff.md`](2026-05-13-b4b-shipped-handoff.md) β€” B.4b (Use outbound + WorldPicker) preceded that. - -**Retail decomp anchors for auto-walk:** `docs/research/named-retail/acclient_2013_pseudo_c.txt` searched by: -- `MovementManager::PerformMovement` (`0x00524440`) -- `MoveToManager::HandleMoveToObject` -- `MoveToManager::HandleMoveToPosition` -- `MoveToManager::HandleTurnToHeading` (`0x0052a0c0`) -- `CPhysicsObj::MoveToObject` (`0x00512860`) -- `VividTargetIndicator::SetSelected` (`0x004f5ce0`) -- `gmRadarUI::GetBlipColor` (`0x004d76f0`) - -**ACE anchors:** `references/ACE/Source/ACE.Server/WorldObjects/Player_Move.cs:37–179` (`CreateMoveToChain`) and `Player_Inventory.cs:976–1106` (pickup chain). - ---- - -## Session-specific morale note - -This was a long session β€” 43 user messages, ~12 hours wall-clock, 36 commits. The pattern was: implement, user tests against retail, user reports specific divergence, fix, repeat. The user pushed back hard on workarounds twice ("Why workarounds? Nothing wrong with ACE, our client is wrong" + "did you verify with retail?") and both times the right move was to drop the workaround and chase the root cause. The 10 Hz heartbeat (`301281d`) was the highest-leverage commit of the session β€” it closed #67 and tightened the firing distance for every other interaction. **Lesson: when a workaround starts feeling load-bearing, find the heartbeat-cadence-style root cause behind it before adding more layers.** - -The B.6 four-slice plan in the spec was the right shape β€” Slice 1 (diagnostic) revealed `mtRun=0.00 + no UP echo`, which directly informed Slice 2 (treat MovementType=0 InterpretedMotionState as a companion not a cancel signal, `f18de7c`). Slice 3 + 4 (walk vs run + turn-first) emerged from visual testing. **Lesson: diagnostic-first slicing pays off when you don't actually know what ACE will send.** - -β€” Session ended at user request to write this handoff before context compaction. diff --git a/docs/superpowers/plans/2026-05-08-phase-n1-scenery-via-wb-helpers.md b/docs/superpowers/plans/2026-05-08-phase-n1-scenery-via-wb-helpers.md deleted file mode 100644 index d6bae6f..0000000 --- a/docs/superpowers/plans/2026-05-08-phase-n1-scenery-via-wb-helpers.md +++ /dev/null @@ -1,1130 +0,0 @@ -# Phase N.1 β€” Scenery via WorldBuilder Helpers Implementation Plan - -> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. - -**Goal:** Replace the in-line algorithm guts of `SceneryGenerator.Generate()` with calls to WorldBuilder's `SceneryHelpers` (Displace / RotateObj / ScaleObj / CheckSlope / ObjAlign) and `TerrainUtils` (OnRoad / GetNormal). Keep our data flow, our `ScenerySpawn` shape, and our renderer integration. Place behind a `ACDREAM_USE_WB_SCENERY=1` feature flag, prove equivalence with helper-level conformance tests, then flip default-on after visual verification at landblock `0xA9B1`. - -**Architecture:** Strangler-fig substitution. The 9Γ—9 vertex loop, scene-selection hash, frequency roll, bounds check, building grid, and `BaseLoc.Z` handling stay identical. Only the five algorithm calls (displacement, road test, slope normal, rotation, scale) get replaced. A small adapter `WbSceneryAdapter.BuildTerrainEntries(LandBlock)` produces the `TerrainEntry[]` shape WB's helpers expect. - -**Tech Stack:** .NET 10 / C# 13, Silk.NET (transitively), `WorldBuilder.Shared` + `Chorizite.OpenGLSDLBackend` (project references already wired up in Phase N.0), DatReaderWriter for `LandBlock` / `Region` / `ObjectDesc` / `Scene` types, xUnit for tests. - -**Spec:** `docs/superpowers/specs/2026-05-08-phase-n1-scenery-via-wb-helpers-design.md` -**Parent design:** `docs/superpowers/specs/2026-05-08-phase-n-worldbuilder-migration-design.md` -**Inventory:** `docs/architecture/worldbuilder-inventory.md` - -**Prerequisite:** Phase N.0 already shipped (commit `c8782c9`) β€” `references/WorldBuilder/` is a git submodule pointing at `github.com/eriknihlen/WorldBuilder.git` `acdream` branch; `AcDream.Core.csproj` references `WorldBuilder.Shared` + `Chorizite.OpenGLSDLBackend`. - ---- - -## File Plan - -| File | Disposition | Responsibility | -|---|---|---| -| `src/AcDream.Core/World/WbSceneryAdapter.cs` | NEW | `LandBlock` (acdream's dat type) β†’ `TerrainEntry[]` (WB's data shape). Stateless. | -| `src/AcDream.Core/World/SceneryGenerator.cs` | MODIFY | Add `UseWbScenery` feature-flag flag. Add `GenerateViaWb` private alternative path. Wire dispatch in `Generate()`. Keep legacy methods for now β€” deleted in final task. | -| `tests/AcDream.Core.Tests/World/WbSceneryAdapterTests.cs` | NEW | Verify field bit-packing round-trips (Road, Type, Scenery, Height) and bounds-check behavior. | -| `tests/AcDream.Core.Tests/World/SceneryWbConformanceTests.cs` | NEW | Five small tests proving WB's `Displace`/`OnRoad`/`GetNormal`/`RotateObj`/`ScaleObj` match our existing inline logic for representative inputs. | -| `tests/AcDream.Core.Tests/World/SceneryGeneratorTests.cs` | MODIFY | After Task 7 (legacy delete), prune the now-irrelevant `DisplaceObject_*` tests and update class doc. | - -**Why split adapter into its own file:** the adapter is a pure stateless utility. Putting it in its own file keeps `SceneryGenerator.cs` focused on the generator algorithm, and makes the adapter trivially reusable in N.2+ (terrain math helpers will need the same `TerrainEntry[]`). - -**Why combine conformance tests in one file:** all five tests share the same imports and fixtures, and they all measure the same thing (helper equivalence). Splitting would be over-decomposed. - ---- - -## Task 1: LandBlock β†’ TerrainEntry[] adapter - -**Files:** -- Create: `src/AcDream.Core/World/WbSceneryAdapter.cs` -- Test: `tests/AcDream.Core.Tests/World/WbSceneryAdapterTests.cs` - -- [ ] **Step 1.1: Write the failing test** - -Create `tests/AcDream.Core.Tests/World/WbSceneryAdapterTests.cs`: - -```csharp -using AcDream.Core.World; -using DatReaderWriter.DBObjs; - -namespace AcDream.Core.Tests.World; - -/// -/// Tests for . The adapter converts our -/// LandBlock dat type (Terrain ushort[81] + Height byte[81]) into -/// WorldBuilder's [81] -/// shape, which WB's TerrainUtils / SceneryRenderManager consume. -/// -/// Bit layout in our LandBlock.Terrain[i] (ushort): -/// bits 0-1 : Road (2 bits, ACViewer convention) -/// bits 2-6 : TerrainType (5 bits) β†’ WB calls this Texture -/// bits 11-15 : SceneType (5 bits) β†’ WB calls this Scenery -/// Height comes from LandBlock.Height[i] (byte). -/// -public class WbSceneryAdapterTests -{ - [Fact] - public void BuildTerrainEntries_PreservesRoadTextureSceneryHeight() - { - var block = new LandBlock - { - Terrain = new ushort[81], - Height = new byte[81], - }; - - // Vertex 0: road=0x3, type=0x00, scenery=0x1F, height=42 - // raw layout: bits 0-1=11, bits 2-6=00000, bits 7-10=0000, bits 11-15=11111 - // = 0xF803 - block.Terrain[0] = 0xF803; - block.Height[0] = 42; - - // Vertex 80: road=0x0, type=0x1F, scenery=0x00, height=200 - // raw layout: bits 0-1=00, bits 2-6=11111, bits 11-15=00000 - // = 0x007C - block.Terrain[80] = 0x007C; - block.Height[80] = 200; - - var entries = WbSceneryAdapter.BuildTerrainEntries(block); - - Assert.Equal(81, entries.Length); - - Assert.Equal((byte)42, entries[0].Height); - Assert.Equal((byte)0x3, entries[0].Road); - Assert.Equal((byte)0x00, entries[0].Texture); - Assert.Equal((byte)0x1F, entries[0].Scenery); - - Assert.Equal((byte)200, entries[80].Height); - Assert.Equal((byte)0x0, entries[80].Road); - Assert.Equal((byte)0x1F, entries[80].Texture); - Assert.Equal((byte)0x00, entries[80].Scenery); - } - - [Fact] - public void BuildTerrainEntries_AllZeros_ProducesEmptyEntries() - { - var block = new LandBlock - { - Terrain = new ushort[81], - Height = new byte[81], - }; - var entries = WbSceneryAdapter.BuildTerrainEntries(block); - Assert.All(entries, e => - { - Assert.Equal((byte)0, e.Height); - Assert.Equal((byte)0, e.Road); - Assert.Equal((byte)0, e.Texture); - Assert.Equal((byte)0, e.Scenery); - }); - } - - [Fact] - public void BuildTerrainEntries_NullBlock_Throws() - { - Assert.Throws(() => - WbSceneryAdapter.BuildTerrainEntries(null!)); - } -} -``` - -- [ ] **Step 1.2: Run the test to verify it fails** - -Run: `dotnet test --no-restore tests/AcDream.Core.Tests/AcDream.Core.Tests.csproj --filter "WbSceneryAdapterTests" 2>&1 | tail -10` -Expected: BUILD ERROR or FAIL with "type or namespace 'WbSceneryAdapter' could not be found". - -- [ ] **Step 1.3: Implement the adapter** - -Create `src/AcDream.Core/World/WbSceneryAdapter.cs`: - -```csharp -using DatReaderWriter.DBObjs; -using WorldBuilder.Shared.Models; - -namespace AcDream.Core.World; - -/// -/// Bridges acdream's dat types into WorldBuilder's data shapes for the -/// Phase N rendering migration. See -/// docs/architecture/worldbuilder-inventory.md for the full strategy. -/// -internal static class WbSceneryAdapter -{ - private const int VerticesPerSide = 9; - private const int TerrainSize = VerticesPerSide * VerticesPerSide; // 81 - - /// - /// Builds a 9Γ—9 = 81-entry array from a - /// 's packed terrain bits + height bytes. WB's - /// TerrainUtils.OnRoad / GetNormal / GetHeight - /// consume this shape. - /// - /// Bit layout in our LandBlock.Terrain[i] (ushort): - /// bits 0-1 : Road (2 bits) β†’ WB Road - /// bits 2-6 : TerrainType (5 bits) β†’ WB Texture - /// bits 11-15 : SceneType (5 bits) β†’ WB Scenery - /// Height comes from LandBlock.Height[i] (byte). - /// - public static TerrainEntry[] BuildTerrainEntries(LandBlock block) - { - ArgumentNullException.ThrowIfNull(block); - if (block.Terrain.Length != TerrainSize) - throw new ArgumentException( - $"LandBlock.Terrain must be {TerrainSize} entries (9Γ—9), got {block.Terrain.Length}", - nameof(block)); - if (block.Height.Length != TerrainSize) - throw new ArgumentException( - $"LandBlock.Height must be {TerrainSize} entries (9Γ—9), got {block.Height.Length}", - nameof(block)); - - var entries = new TerrainEntry[TerrainSize]; - for (int i = 0; i < TerrainSize; i++) - { - ushort raw = block.Terrain[i]; - byte road = (byte)(raw & 0x3); - byte texture = (byte)((raw >> 2) & 0x1F); - byte scenery = (byte)((raw >> 11) & 0x1F); - byte height = block.Height[i]; - entries[i] = new TerrainEntry(height, texture, scenery, road, encounters: null); - } - return entries; - } -} -``` - -- [ ] **Step 1.4: Run the test to verify it passes** - -Run: `dotnet test --no-restore tests/AcDream.Core.Tests/AcDream.Core.Tests.csproj --filter "WbSceneryAdapterTests" 2>&1 | tail -5` -Expected: `Passed! - Failed: 0, Passed: 3` (or similar β€” three tests). - -- [ ] **Step 1.5: Commit** - -```bash -git add src/AcDream.Core/World/WbSceneryAdapter.cs tests/AcDream.Core.Tests/World/WbSceneryAdapterTests.cs -git commit -m "$(cat <<'EOF' -phase(N.1): add LandBlock β†’ TerrainEntry[] adapter - -Phase N.1 step 1: WbSceneryAdapter.BuildTerrainEntries converts our -LandBlock dat type into the TerrainEntry[81] shape WorldBuilder's -TerrainUtils / SceneryRenderManager consume. - -Bit-pack mapping (ours β†’ WB): - Terrain bits 0-1 (Road) β†’ TerrainEntry.Road - Terrain bits 2-6 (TerrainType) β†’ TerrainEntry.Texture - Terrain bits 11-15 (SceneType) β†’ TerrainEntry.Scenery - Height byte β†’ TerrainEntry.Height - -Spec: docs/superpowers/specs/2026-05-08-phase-n1-scenery-via-wb-helpers-design.md - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - ---- - -## Task 2: Feature flag scaffold - -**Files:** -- Modify: `src/AcDream.Core/World/SceneryGenerator.cs` - -This task only adds the flag-read field. It changes no behavior and there is nothing to assert beyond "it compiles." The flag is consumed in Task 5. - -- [ ] **Step 2.1: Add the feature flag field** - -Open `src/AcDream.Core/World/SceneryGenerator.cs`. Find the line: - -```csharp - // AC landblock geometry β€” matches LandblockMesh. - private const int VerticesPerSide = 9; - private const float CellSize = 24.0f; - private const float LandblockSize = 192.0f; // 8 cells * 24 units -``` - -Immediately AFTER the `LandblockSize` constant, ADD: - -```csharp - - /// - /// Phase N.1 feature flag β€” when set to "1", scenery placement uses - /// WorldBuilder's SceneryHelpers + TerrainUtils instead of - /// our hand-ported algorithms. Default off until visual verification at - /// landblock 0xA9B1 confirms behavior. See - /// docs/superpowers/specs/2026-05-08-phase-n1-scenery-via-wb-helpers-design.md. - /// - internal static readonly bool UseWbScenery = - System.Environment.GetEnvironmentVariable("ACDREAM_USE_WB_SCENERY") == "1"; -``` - -- [ ] **Step 2.2: Verify build still passes** - -Run: `dotnet build src/AcDream.Core/AcDream.Core.csproj 2>&1 | tail -5` -Expected: `Build succeeded.` and `0 Error(s)`. - -- [ ] **Step 2.3: Verify all existing tests still pass** - -Run: `dotnet test --no-build tests/AcDream.Core.Tests/AcDream.Core.Tests.csproj --filter "SceneryGenerator|Wb" 2>&1 | tail -3` -Expected: `Passed!` with all scenery-area tests passing. - -- [ ] **Step 2.4: Commit** - -```bash -git add src/AcDream.Core/World/SceneryGenerator.cs -git commit -m "$(cat <<'EOF' -phase(N.1): add ACDREAM_USE_WB_SCENERY feature flag scaffold - -Phase N.1 step 2: read the env var into a static bool. No behavior -change yet β€” the flag is consumed in step 5 when GenerateViaWb is -wired in. - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - ---- - -## Task 3: Per-helper conformance tests - -**Files:** -- Create: `tests/AcDream.Core.Tests/World/SceneryWbConformanceTests.cs` - -These five tests prove WB's `SceneryHelpers.Displace` / `TerrainUtils.OnRoad` / `TerrainUtils.GetNormal` / `SceneryHelpers.RotateObj` / `SceneryHelpers.ScaleObj` produce the same answers as the hand-ported logic currently inlined in `Generate()`. After this task, we have empirical evidence that the substitution is safe. - -If a test fails, that is the bug β€” investigate before proceeding to Task 4. - -- [ ] **Step 3.1: Write all five conformance tests** - -Create `tests/AcDream.Core.Tests/World/SceneryWbConformanceTests.cs`: - -```csharp -using System.Numerics; -using AcDream.Core.World; -using DatReaderWriter.DBObjs; -using DatReaderWriter.Types; -using WB_TerrainUtils = WorldBuilder.Shared.Modules.Landscape.Lib.TerrainUtils; -using WB_SceneryHelpers = Chorizite.OpenGLSDLBackend.Lib.SceneryHelpers; - -namespace AcDream.Core.Tests.World; - -/// -/// Phase N.1 helper-level conformance tests. Each test compares an algorithm -/// in our existing path against WorldBuilder's -/// equivalent for representative inputs. Passing tests are empirical evidence -/// that swapping our inline logic for WB's helpers is behavior-preserving. -/// -/// If any of these fails the substitution would silently change rendered -/// scenery; investigate before proceeding to Task 4 (GenerateViaWb). -/// -/// Inputs are chosen to exercise: -/// - A non-edge vertex (gx=100, gy=100, j=0) β€” typical case -/// - The edge vertex at y=8 specifically (Issue #49 territory) -/// -public class SceneryWbConformanceTests -{ - private static ObjectDesc MakeObj( - float displaceX = 12f, - float displaceY = 12f, - float minScale = 1f, - float maxScale = 1f, - float maxRotation = 0f, - float minSlope = 0f, - float maxSlope = 1f, - int align = 0) - { - return new ObjectDesc - { - ObjectId = 0x02000258u, - DisplaceX = displaceX, - DisplaceY = displaceY, - MinScale = minScale, - MaxScale = maxScale, - MaxRotation = maxRotation, - MinSlope = minSlope, - MaxSlope = maxSlope, - Align = (uint)align, - BaseLoc = new Frame { Origin = new Vector3(0, 0, 0) }, - }; - } - - /// - /// Our DisplaceObject ↔ WB's SceneryHelpers.Displace must produce the - /// same Vector3 for the same (obj, ix, iy, iq). - /// - [Theory] - [InlineData(100u, 100u, 0u)] // typical - [InlineData( 50u, 50u, 1u)] // typical, j=1 - [InlineData( 4u, 8u, 0u)] // edge vertex y=8 - [InlineData( 8u, 4u, 0u)] // edge vertex x=8 - public void Displace_OursMatchesWb(uint ix, uint iy, uint iq) - { - var obj = MakeObj(); - var ours = SceneryGenerator.DisplaceObject(obj, ix, iy, iq); - var wb = WB_SceneryHelpers.Displace(obj, ix, iy, iq); - - Assert.Equal(ours.X, wb.X, precision: 4); - Assert.Equal(ours.Y, wb.Y, precision: 4); - Assert.Equal(ours.Z, wb.Z, precision: 4); - } - - /// - /// Our IsOnRoad ↔ WB's TerrainUtils.OnRoad must produce the same bool - /// for the same (lx, ly) when the underlying terrain bits match. - /// - [Theory] - [InlineData( 12.0f, 12.0f)] // cell (0,0) center - [InlineData( 85.08f, 190.97f)] // the 0xA9B1 edge-vertex bug location - [InlineData( 3.0f, 3.0f)] // near a road if r0 is set - [InlineData( 23.5f, 12.0f)] // edge of cell, between cells - public void OnRoad_OursMatchesWb_DiagonalRoad(float lx, float ly) - { - // Build a synthetic LandBlock with road bits at SW (0,0) and NE (1,1) - // of cell (0,0) β€” the diagonal pattern we saw at 0xA9B1. - var block = new LandBlock - { - Terrain = new ushort[81], - Height = new byte[81], - }; - // road bit at vertex (0,0) β€” index 0*9+0 = 0 - block.Terrain[0] = 0x0003; // road=3 - // road bit at vertex (1,1) β€” index 1*9+1 = 10 - block.Terrain[10] = 0x0003; - - bool ours = SceneryGenerator.IsOnRoad(block, lx, ly); - - var entries = WbSceneryAdapter.BuildTerrainEntries(block); - bool wb = WB_TerrainUtils.OnRoad(new Vector3(lx, ly, 0), entries); - - Assert.Equal(ours, wb); - } - - /// - /// Our SampleNormalZFromHeightmap ↔ WB's TerrainUtils.GetNormal(...).Z - /// must produce the same Z for representative slope inputs. - /// - [Theory] - [InlineData( 12.0f, 12.0f)] // cell center - [InlineData( 85.08f, 190.97f)] // the 0xA9B1 edge-vertex location - [InlineData( 3.0f, 188.0f)] // near a y-edge - public void GetNormalZ_OursMatchesWb_LinearTable(float lx, float ly) - { - // Heightmap with non-flat terrain so normals are non-trivial. - var heights = new byte[81]; - for (int x = 0; x < 9; x++) - for (int y = 0; y < 9; y++) - heights[x * 9 + y] = (byte)((x * 17 + y * 13) % 256); - - var heightTable = new float[256]; - for (int i = 0; i < 256; i++) heightTable[i] = i * 1.0f; - - const uint lbX = 0xA9, lbY = 0xB1; - - // Build a Region-shaped object with the LandHeightTable populated. - // (TerrainUtils.GetNormal calls region.LandDefs.LandHeightTable[height].) - // LandHeightTable is float[] (size 256) in DatReaderWriter β€” see - // src/AcDream.App/Rendering/GameWindow.cs:1306-1308 for the runtime check. - var region = new DatReaderWriter.DBObjs.Region - { - LandDefs = new LandDefs(), - }; - // If LandDefs default-initializes LandHeightTable to a non-null float[256], - // copy into it. If it's null, assign directly. The implementer should - // pick whichever pattern compiles in DatReaderWriter 2.1.7's API: - // Option A: region.LandDefs.LandHeightTable = heightTable; - // Option B: Array.Copy(heightTable, region.LandDefs.LandHeightTable, 256); - region.LandDefs.LandHeightTable = heightTable; - - var block = new LandBlock - { - Terrain = new ushort[81], - Height = heights, - }; - var entries = WbSceneryAdapter.BuildTerrainEntries(block); - - float ours = AcDream.Core.Physics.TerrainSurface.SampleNormalZFromHeightmap( - heights, heightTable, lbX, lbY, lx, ly); - float wb = WB_TerrainUtils.GetNormal(region, entries, lbX, lbY, - new Vector3(lx, ly, 0)).Z; - - Assert.Equal(ours, wb, precision: 4); - } - - /// - /// Our inline rotation logic ↔ WB's SceneryHelpers.RotateObj must - /// produce the same Quaternion for non-Align objects with MaxRotation. - /// - [Theory] - [InlineData( 100u, 100u, 0u, 360f)] - [InlineData( 4u, 8u, 0u, 360f)] - [InlineData( 200u, 250u, 1u, 180f)] - public void RotateObj_OursMatchesWb_NonAlign(uint gx, uint gy, uint j, float maxRot) - { - var obj = MakeObj(maxRotation: maxRot); - - // Our inline logic from SceneryGenerator.Generate (~lines 220-231): - Quaternion ours = obj.BaseLoc.Orientation; - if (ours.LengthSquared() < 0.0001f) ours = Quaternion.Identity; - if (obj.MaxRotation > 0f) - { - double rotNoise = unchecked((uint)(1813693831u * gy - - (j + 63127u) * (1360117743u * gy * gx + 1888038839u) - - 1109124029u * gx)) * 2.3283064e-10; - float degrees = (float)(rotNoise * obj.MaxRotation); - float yawDeg = -((450f - degrees) % 360f); - float yawRad = yawDeg * MathF.PI / 180f; - var headingQuat = Quaternion.CreateFromAxisAngle(Vector3.UnitZ, yawRad); - ours = headingQuat * ours; - } - - // WB's SceneryHelpers.Displace returns the localPos that RotateObj - // expects for its loc parameter (used only when SetHeading is called - // with non-zero matrix, but a stub Vector3 works since BaseLoc is identity). - var localPos = WB_SceneryHelpers.Displace(obj, gx, gy, j); - Quaternion wb = WB_SceneryHelpers.RotateObj(obj, gx, gy, j, localPos); - - Assert.Equal(ours.X, wb.X, precision: 4); - Assert.Equal(ours.Y, wb.Y, precision: 4); - Assert.Equal(ours.Z, wb.Z, precision: 4); - Assert.Equal(ours.W, wb.W, precision: 4); - } - - /// - /// Our inline scale logic ↔ WB's SceneryHelpers.ScaleObj must produce - /// the same float for representative inputs. - /// - [Theory] - [InlineData(100u, 100u, 0u, 0.5f, 1.5f)] - [InlineData( 4u, 8u, 0u, 1.0f, 1.0f)] - [InlineData(200u, 250u, 1u, 0.8f, 1.2f)] - public void ScaleObj_OursMatchesWb(uint gx, uint gy, uint j, float minScale, float maxScale) - { - var obj = MakeObj(minScale: minScale, maxScale: maxScale); - - // Our inline logic from SceneryGenerator.Generate (~lines 236-247): - float ours; - if (obj.MinScale == obj.MaxScale) - { - ours = obj.MaxScale; - } - else - { - double scaleNoise = unchecked((uint)(1813693831u * gy - - (j + 32593u) * (1360117743u * gy * gx + 1888038839u) - - 1109124029u * gx)) * 2.3283064e-10; - ours = (float)(Math.Pow(obj.MaxScale / obj.MinScale, scaleNoise) * obj.MinScale); - } - if (ours <= 0) ours = 1f; - - float wb = WB_SceneryHelpers.ScaleObj(obj, gx, gy, j); - if (wb <= 0) wb = 1f; - - Assert.Equal(ours, wb, precision: 4); - } -} -``` - -- [ ] **Step 3.2: Make our `IsOnRoad` accessible to the test** - -`IsOnRoad` is currently `private`. Bump to `internal` so the conformance test can call it. Open `src/AcDream.Core/World/SceneryGenerator.cs` and change: - -```csharp - private static bool IsOnRoad(LandBlock block, float lx, float ly) -``` - -to: - -```csharp - internal static bool IsOnRoad(LandBlock block, float lx, float ly) -``` - -- [ ] **Step 3.3: Run the conformance tests** - -Run: `dotnet test --no-restore tests/AcDream.Core.Tests/AcDream.Core.Tests.csproj --filter "SceneryWbConformance" 2>&1 | tail -10` -Expected: ALL TESTS PASS. - -If any test fails, **stop and investigate** β€” that's the bug WB is hiding from us. Report which assertion failed (e.g., "Displace at gx=4 gy=8 returns different Y") and confer with the user before proceeding. - -- [ ] **Step 3.4: Commit** - -```bash -git add tests/AcDream.Core.Tests/World/SceneryWbConformanceTests.cs src/AcDream.Core/World/SceneryGenerator.cs -git commit -m "$(cat <<'EOF' -phase(N.1): per-helper conformance tests for WB substitutions - -Phase N.1 step 3: prove our inline algorithms (Displace, IsOnRoad, -slope normal Z, RotateObj, ScaleObj) match WorldBuilder's helpers -for representative inputs including the 0xA9B1 edge-vertex case. - -Bumps IsOnRoad to internal so the test can call it directly. - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - ---- - -## Task 4: Implement `GenerateViaWb` - -**Files:** -- Modify: `src/AcDream.Core/World/SceneryGenerator.cs` - -Add a new private method `GenerateViaWb` that produces `IReadOnlyList` using only WB helpers for the substituted algorithm calls. The 9Γ—9 loop, scene selection, frequency check, bounds check, building grid, and `BaseLoc.Z` handling stay structurally identical to `Generate`. - -- [ ] **Step 4.1: Add the required `using` directives** - -Open `src/AcDream.Core/World/SceneryGenerator.cs`. The file currently has: - -```csharp -using System.Numerics; -using DatReaderWriter; -using DatReaderWriter.DBObjs; -using DatReaderWriter.Types; - -namespace AcDream.Core.World; -``` - -Replace with: - -```csharp -using System.Numerics; -using Chorizite.OpenGLSDLBackend.Lib; -using DatReaderWriter; -using DatReaderWriter.DBObjs; -using DatReaderWriter.Types; -using WorldBuilder.Shared.Modules.Landscape.Lib; - -namespace AcDream.Core.World; -``` - -- [ ] **Step 4.2: Add `GenerateViaWb` immediately after `Generate`** - -Find the closing `}` of `Generate(...)` in `SceneryGenerator.cs` (just before the `IsRoadVertex` method). Immediately AFTER `Generate`'s closing brace, ADD: - -```csharp - - /// - /// Phase N.1 alternative implementation that delegates the - /// algorithm calls to WorldBuilder's SceneryHelpers + - /// TerrainUtils. Structurally identical to - /// but with WB's tested ports doing the work. Selected by - /// . - /// - private static IReadOnlyList GenerateViaWb( - DatCollection dats, - Region region, - LandBlock block, - uint landblockId, - HashSet? buildingCells) - { - var result = new List(); - - if (region.TerrainInfo?.TerrainTypes is null || region.SceneInfo?.SceneTypes is null) - return result; - - // Build the TerrainEntry[] WB's helpers consume β€” once per landblock. - var terrainEntries = WbSceneryAdapter.BuildTerrainEntries(block); - - uint blockX = (landblockId >> 24) * 8; - uint blockY = ((landblockId >> 16) & 0xFFu) * 8; - uint lbX = landblockId >> 24; - uint lbY = (landblockId >> 16) & 0xFFu; - - for (int x = 0; x < VerticesPerSide; x++) - { - for (int y = 0; y < VerticesPerSide; y++) - { - int i = x * VerticesPerSide + y; - ushort raw = block.Terrain[i]; - - uint terrainType = (uint)((raw >> 2) & 0x1F); - uint sceneType = (uint)((raw >> 11) & 0x1F); - - if (terrainType >= region.TerrainInfo.TerrainTypes.Count) continue; - var sceneTypeList = region.TerrainInfo.TerrainTypes[(int)terrainType].SceneTypes; - if (sceneType >= sceneTypeList.Count) continue; - - uint sceneInfo = sceneTypeList[(int)sceneType]; - if (sceneInfo >= region.SceneInfo.SceneTypes.Count) continue; - - var scenes = region.SceneInfo.SceneTypes[(int)sceneInfo].Scenes; - if (scenes.Count == 0) continue; - - uint cellX = (uint)x; - uint cellY = (uint)y; - uint globalCellX = cellX + blockX; - uint globalCellY = cellY + blockY; - - // Scene-selection hash: identical to Generate. - uint cellMat = globalCellY * (712977289u * globalCellX + 1813693831u) - - 1109124029u * globalCellX + 2139937281u; - double offset = cellMat * 2.3283064e-10; - int sceneIdx = (int)(scenes.Count * offset); - if (sceneIdx >= scenes.Count || sceneIdx < 0) sceneIdx = 0; - - uint sceneId = (uint)scenes[sceneIdx]; - var scene = dats.Get(sceneId); - if (scene is null) continue; - - // Per-object frequency setup: identical to Generate. - uint cellXMat = unchecked(0u - 1109124029u * globalCellX); - uint cellYMat = 1813693831u * globalCellY; - uint cellMat2 = 1360117743u * globalCellX * globalCellY + 1888038839u; - - for (uint j = 0; j < scene.Objects.Count; j++) - { - var obj = scene.Objects[(int)j]; - if (obj.WeenieObj != 0) continue; - - double noise = unchecked((uint)(cellXMat + cellYMat - cellMat2 * (23399u + j))) * 2.3283064e-10; - if (noise >= obj.Frequency) continue; - - // ─── WB substitution: displacement ─────────────────── - var localPos = SceneryHelpers.Displace(obj, globalCellX, globalCellY, j); - - float lx = cellX * CellSize + localPos.X; - float ly = cellY * CellSize + localPos.Y; - - if (lx < 0 || ly < 0 || lx >= LandblockSize || ly >= LandblockSize) - continue; - - // ─── WB substitution: road check ────────────────────── - if (TerrainUtils.OnRoad(new Vector3(lx, ly, 0), terrainEntries)) - continue; - - // Building check: identical to Generate. - if (buildingCells is not null) - { - int dcx = Math.Clamp((int)(lx / CellSize), 0, CellsPerSide - 1); - int dcy = Math.Clamp((int)(ly / CellSize), 0, CellsPerSide - 1); - if (buildingCells.Contains(dcx * VerticesPerSide + dcy)) - continue; - } - - // ─── WB substitution: slope check ───────────────────── - Vector3 normal = TerrainUtils.GetNormal( - region, terrainEntries, lbX, lbY, - new Vector3(lx, ly, 0)); - if (!SceneryHelpers.CheckSlope(obj, normal.Z)) - continue; - - float lz = obj.BaseLoc.Origin.Z; - - // ─── WB substitution: rotation ──────────────────────── - Quaternion rotation; - if (obj.Align != 0) - rotation = SceneryHelpers.ObjAlign(obj, normal, lz, localPos); - else - rotation = SceneryHelpers.RotateObj(obj, globalCellX, globalCellY, j, localPos); - - // ─── WB substitution: scale ─────────────────────────── - float scale = SceneryHelpers.ScaleObj(obj, globalCellX, globalCellY, j); - if (scale <= 0) scale = 1f; - - result.Add(new ScenerySpawn( - ObjectId: obj.ObjectId, - LocalPosition: new Vector3(lx, ly, lz), - Rotation: rotation, - Scale: scale)); - } - } - } - - return result; - } -``` - -- [ ] **Step 4.3: Verify build** - -Run: `dotnet build src/AcDream.Core/AcDream.Core.csproj 2>&1 | tail -5` -Expected: `Build succeeded.` `0 Error(s)`. - -If you get an error like `'SceneryHelpers' is an ambiguous reference`, it's because both Chorizite.OpenGLSDLBackend.Lib and WorldBuilder.Shared expose helpers β€” fix by qualifying: `Chorizite.OpenGLSDLBackend.Lib.SceneryHelpers.Displace(...)`. - -- [ ] **Step 4.4: Verify existing tests still pass** - -Run: `dotnet test --no-build tests/AcDream.Core.Tests/AcDream.Core.Tests.csproj --filter "SceneryGenerator|WbSceneryAdapter|SceneryWbConformance" 2>&1 | tail -3` -Expected: all scenery-area tests pass (the ones added in Tasks 1 and 3, plus the original SceneryGeneratorTests). No behavior change yet for the live `Generate` path β€” `GenerateViaWb` is added but not called. - -- [ ] **Step 4.5: Commit** - -```bash -git add src/AcDream.Core/World/SceneryGenerator.cs -git commit -m "$(cat <<'EOF' -phase(N.1): implement GenerateViaWb alternative path - -Phase N.1 step 4: parallel implementation of Generate() that calls -WB's SceneryHelpers (Displace/CheckSlope/RotateObj/ObjAlign/ScaleObj) -and TerrainUtils (OnRoad/GetNormal) instead of the inline ports. - -Not yet wired in β€” Generate() still runs the legacy path. Step 5 -adds the dispatch. - -Per-helper conformance tests in step 3 prove this implementation is -behavior-equivalent to the legacy path. - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - ---- - -## Task 5: Wire feature-flag dispatch - -**Files:** -- Modify: `src/AcDream.Core/World/SceneryGenerator.cs` - -- [ ] **Step 5.1: Add the dispatch at the top of `Generate`** - -In `src/AcDream.Core/World/SceneryGenerator.cs`, find the body of `Generate`: - -```csharp - public static IReadOnlyList Generate( - DatCollection dats, - Region region, - LandBlock block, - uint landblockId, - HashSet? buildingCells = null, - float[]? heightTable = null) - { - var result = new List(); - - if (region.TerrainInfo?.TerrainTypes is null || region.SceneInfo?.SceneTypes is null) - return result; -``` - -Immediately AFTER the opening `{` and BEFORE `var result = new List();`, ADD: - -```csharp - // Phase N.1: route to the WorldBuilder-backed implementation when - // ACDREAM_USE_WB_SCENERY=1. See - // docs/superpowers/specs/2026-05-08-phase-n1-scenery-via-wb-helpers-design.md. - if (UseWbScenery) - return GenerateViaWb(dats, region, block, landblockId, buildingCells); - -``` - -So the method's opening becomes: - -```csharp - public static IReadOnlyList Generate( - DatCollection dats, - Region region, - LandBlock block, - uint landblockId, - HashSet? buildingCells = null, - float[]? heightTable = null) - { - // Phase N.1: route to the WorldBuilder-backed implementation when - // ACDREAM_USE_WB_SCENERY=1. See - // docs/superpowers/specs/2026-05-08-phase-n1-scenery-via-wb-helpers-design.md. - if (UseWbScenery) - return GenerateViaWb(dats, region, block, landblockId, buildingCells); - - var result = new List(); - - if (region.TerrainInfo?.TerrainTypes is null || region.SceneInfo?.SceneTypes is null) - return result; -``` - -Note: `heightTable` is NOT passed to `GenerateViaWb` β€” the WB path uses `region.LandDefs.LandHeightTable` via `TerrainUtils.GetNormal`. The legacy path keeps the parameter for backward compatibility. - -- [ ] **Step 5.2: Verify build** - -Run: `dotnet build src/AcDream.Core/AcDream.Core.csproj 2>&1 | tail -5` -Expected: `Build succeeded.` - -- [ ] **Step 5.3: Verify all existing tests still pass with flag OFF (default)** - -Run: `dotnet test --no-build tests/AcDream.Core.Tests/AcDream.Core.Tests.csproj --filter "SceneryGenerator|WbSceneryAdapter|SceneryWbConformance" 2>&1 | tail -3` -Expected: all pass (the legacy path is still default, so behavior is unchanged). - -- [ ] **Step 5.4: Commit** - -```bash -git add src/AcDream.Core/World/SceneryGenerator.cs -git commit -m "$(cat <<'EOF' -phase(N.1): wire ACDREAM_USE_WB_SCENERY dispatch in Generate() - -Phase N.1 step 5: when the flag is set, Generate() delegates to -GenerateViaWb. Default off; flag flips to default-on in step 7 -after visual verification. - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - ---- - -## Task 6: Visual verification β€” manual checkpoint - -This task is interactive. **You must work with the user.** They run the client, look at landblock `0xA9B1`, and confirm two things visually: - -1. The road-edge tree we have been chasing all session is **not present** in the WB-backed render. -2. The Issue #49 missing scenery (the trees the 9Γ—9 loop expansion fixed) is **still visible**. - -- [ ] **Step 6.1: Make sure build is green** - -Run: `dotnet build 2>&1 | tail -3` -Expected: `Build succeeded.` - -- [ ] **Step 6.2: Tell the user how to launch with the flag set** - -Tell the user (paraphrase): "Set `$env:ACDREAM_USE_WB_SCENERY = '1'` in your launch terminal alongside the other env vars, then launch the client and navigate to Holtburg. Specifically check the road-edge area near coordinates (87, 191) in landblock 0xA9B1 β€” the tree we have been chasing all session should be gone now. Also confirm Issue #49's previously missing trees are still there." - -If `dotnet run` is the standard launch command, the full PowerShell launch is: - -```powershell -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -$env:ACDREAM_TEST_HOST = "127.0.0.1" -$env:ACDREAM_TEST_PORT = "9000" -$env:ACDREAM_TEST_USER = "testaccount" -$env:ACDREAM_TEST_PASS = "testpassword" -$env:ACDREAM_USE_WB_SCENERY = "1" -dotnet run --project src\AcDream.App\AcDream.App.csproj --no-build -c Debug 2>&1 | Tee-Object -FilePath "launch.log" -``` - -- [ ] **Step 6.3: Wait for user's verification report** - -The user will tell you "yes the offending tree is gone and Issue #49 is still fine" or "still wrong". If still wrong, do NOT proceed β€” investigate and report back. - -- [ ] **Step 6.4: Commit nothing** - -This task does not produce a code change. The commit happens in Task 7 once the flag is flipped to default-on. - ---- - -## Task 7: Flip default-on - -**Files:** -- Modify: `src/AcDream.Core/World/SceneryGenerator.cs` - -After visual verification passes in Task 6, the WB-backed path becomes the default. The env var still exists as an escape hatch (`ACDREAM_USE_WB_SCENERY=0` reverts to legacy) so that if a regression is reported the next day, we can flip back without redeploying. - -- [ ] **Step 7.1: Flip the flag default** - -In `src/AcDream.Core/World/SceneryGenerator.cs`, find: - -```csharp - internal static readonly bool UseWbScenery = - System.Environment.GetEnvironmentVariable("ACDREAM_USE_WB_SCENERY") == "1"; -``` - -Replace with: - -```csharp - /// - /// Phase N.1: scenery placement uses WorldBuilder's SceneryHelpers - /// + TerrainUtils by default. Set ACDREAM_USE_WB_SCENERY=0 - /// to restore the legacy in-line algorithms (escape hatch β€” to be deleted - /// in a follow-up commit once we have a few sessions of green visuals). - /// - internal static readonly bool UseWbScenery = - System.Environment.GetEnvironmentVariable("ACDREAM_USE_WB_SCENERY") != "0"; -``` - -- [ ] **Step 7.2: Verify build + tests** - -Run: `dotnet build 2>&1 | tail -3 && dotnet test --no-build tests/AcDream.Core.Tests/AcDream.Core.Tests.csproj --filter "SceneryGenerator|WbSceneryAdapter|SceneryWbConformance" 2>&1 | tail -3` -Expected: build green, all targeted tests pass. - -- [ ] **Step 7.3: Commit** - -```bash -git add src/AcDream.Core/World/SceneryGenerator.cs -git commit -m "$(cat <<'EOF' -phase(N.1): WB-backed scenery is now default-on - -Phase N.1 step 7: flips ACDREAM_USE_WB_SCENERY to default-on after -visual verification at Holtburg confirmed the road-edge tree at -0xA9B1 is gone and Issue #49 trees are still visible. - -ACDREAM_USE_WB_SCENERY=0 still reverts to the legacy path. Follow-up -commit will delete the legacy code entirely. - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - ---- - -## Task 8: Delete the legacy code path - -**Files:** -- Modify: `src/AcDream.Core/World/SceneryGenerator.cs` -- Modify: `tests/AcDream.Core.Tests/World/SceneryGeneratorTests.cs` - -After at least one session of clean visuals on the default-on flag, remove the legacy code so we don't accumulate dead-code drift. - -- [ ] **Step 8.1: Delete the legacy `Generate` body and rename `GenerateViaWb`** - -In `src/AcDream.Core/World/SceneryGenerator.cs`: - -1. Delete the `UseWbScenery` field entirely. -2. Delete the entire body of `Generate` after its signature. -3. Replace it with a body that just calls `GenerateViaWb`'s logic (or rename `GenerateViaWb` to `Generate`'s body). - -The simplest approach: rename `GenerateViaWb` to `GenerateInternal` and have the public `Generate` call it. Then delete the legacy logic. Final shape: - -```csharp -public static IReadOnlyList Generate( - DatCollection dats, - Region region, - LandBlock block, - uint landblockId, - HashSet? buildingCells = null, - float[]? heightTable = null) -{ - // heightTable kept for backward compat; WB path uses - // region.LandDefs.LandHeightTable internally. - _ = heightTable; - return GenerateInternal(dats, region, block, landblockId, buildingCells); -} - -private static IReadOnlyList GenerateInternal( - DatCollection dats, - Region region, - LandBlock block, - uint landblockId, - HashSet? buildingCells) -{ - // ... body that was GenerateViaWb ... -} -``` - -4. Delete the now-unused private helpers: `IsOnRoad`, `DisplaceObject`, `RoadHalfWidth`, `CellsPerSide` (if only used by legacy path β€” keep if `GenerateInternal`'s building check still references it). - -Concretely, keep: -- `VerticesPerSide`, `CellSize`, `LandblockSize`, `CellsPerSide` constants (still used in `GenerateInternal`) -- `IsRoadVertex` (still useful as a tiny public predicate) -- `WbSceneryAdapter` (still used) - -Delete: -- `UseWbScenery` -- `IsOnRoad` (and its `RoadHalfWidth` dependency) -- `DisplaceObject` (now dead) - -- [ ] **Step 8.2: Update SceneryGeneratorTests.cs to remove now-irrelevant tests** - -In `tests/AcDream.Core.Tests/World/SceneryGeneratorTests.cs`, the existing -`DisplaceObject_EdgeVertex_CanProduceValidPosition` and -`DisplaceObject_InteriorVertex_AlwaysNearOrigin` tests reference the deleted -`SceneryGenerator.DisplaceObject` helper. Delete them. - -Keep: -- All `IsRoadVertex_*` tests (`IsRoadVertex` is preserved). - -The class doc comment at the top should be updated to reflect the new state: - -```csharp -/// -/// Tests for SceneryGenerator: the road-vertex predicate (only piece of -/// our own algorithm code remaining post Phase N.1). The displacement / -/// road / slope / rotation / scale algorithms now run through -/// WorldBuilder's helpers β€” see SceneryWbConformanceTests.cs for the -/// helper-level equivalence proof. -/// -``` - -- [ ] **Step 8.3: Update the SceneryWbConformanceTests now that legacy helpers are gone** - -`SceneryWbConformanceTests` currently calls `SceneryGenerator.DisplaceObject` and `SceneryGenerator.IsOnRoad`. Once those are deleted, the tests are now testing "WB matches WB" which is meaningless. - -Delete `SceneryWbConformanceTests.cs` entirely. The conformance tests served their purpose during the migration β€” they proved the substitution was safe. Now that we're committed to the WB path, they're vestigial. - -Run: `rm tests/AcDream.Core.Tests/World/SceneryWbConformanceTests.cs` - -- [ ] **Step 8.4: Verify build + tests** - -Run: `dotnet build 2>&1 | tail -3 && dotnet test --no-build tests/AcDream.Core.Tests/AcDream.Core.Tests.csproj --filter "SceneryGenerator|WbSceneryAdapter" 2>&1 | tail -3` -Expected: build green, tests pass (just the IsRoadVertex tests + WbSceneryAdapter tests). - -- [ ] **Step 8.5: Commit** - -```bash -git add src/AcDream.Core/World/SceneryGenerator.cs tests/AcDream.Core.Tests/World/SceneryGeneratorTests.cs tests/AcDream.Core.Tests/World/SceneryWbConformanceTests.cs -git commit -m "$(cat <<'EOF' -phase(N.1): delete legacy scenery code path; WB is the only path - -Phase N.1 step 8 (final): now that ACDREAM_USE_WB_SCENERY has been -default-on for a session with no regressions, remove the legacy -in-line algorithms so we don't accumulate dead-code drift. - -Deleted: -- SceneryGenerator.UseWbScenery (feature flag) -- SceneryGenerator.IsOnRoad / DisplaceObject / RoadHalfWidth (legacy ports) -- SceneryGeneratorTests.DisplaceObject_* (test the deleted method) -- SceneryWbConformanceTests.cs (purpose served β€” proved equivalence pre-migration) - -Renamed: -- GenerateViaWb β†’ GenerateInternal (the only path now) - -Kept: -- IsRoadVertex (small predicate, still used by tests + may be useful elsewhere) -- WbSceneryAdapter (consumed by GenerateInternal; reusable in N.2) - -Phase N.1 complete. Issues #48, #49 are addressed via WB's tested -algorithms. Roadmap entry under Phase N can be marked shipped. - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - -- [ ] **Step 8.6: Mark Phase N.1 shipped in the roadmap** - -In `docs/plans/2026-04-11-roadmap.md`, find the Phase N section (search for `### Phase N β€” WorldBuilder Rendering Migration`). Inside the sub-phases table find the row for **N.1**, currently: - -``` -- **N.1 β€” Scenery algorithm calls.** Replace `IsOnRoad` / - `DisplaceObject` / slope-normal calc / rotation / scale inside - `SceneryGenerator.Generate()` with calls to WB's `SceneryHelpers` + - `TerrainUtils`. Tiny adapter `LandBlock β†’ TerrainEntry[]`. Keeps our - data flow + `ScenerySpawn` shape. Feature flag - `ACDREAM_USE_WB_SCENERY=1`. ~1-2 days. -``` - -Add a status marker at the start of the line: - -``` -- **βœ“ SHIPPED β€” N.1 β€” Scenery algorithm calls.** Shipped 2026-05-08. - Replaced `IsOnRoad` / `DisplaceObject` / slope-normal calc / rotation / - scale inside `SceneryGenerator.Generate()` with calls to WB's - `SceneryHelpers` + `TerrainUtils`. Adapter `WbSceneryAdapter` produces - `TerrainEntry[]`. Visual verification at Holtburg confirmed the - road-edge tree at 0xA9B1 is gone and Issue #49 trees are still visible. -``` - -Also: add a row to the top of the file's "Phases already shipped" table, in commit-shipped order: - -``` -| N.1 | WorldBuilder-backed scenery (Chorizite/WorldBuilder fork as submodule, SceneryHelpers + TerrainUtils replace our inline ports) | Live βœ“ | -``` - -Then commit: - -```bash -git add docs/plans/2026-04-11-roadmap.md -git commit -m "$(cat <<'EOF' -docs(roadmap): mark Phase N.1 (scenery via WB helpers) shipped - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - ---- - -## Done definition - -After all 8 tasks land cleanly: - -- [x] `dotnet build` and `dotnet test` (excluding the 8 pre-existing `DispatcherToMovementIntegrationTests` failures unrelated to this work) green. -- [x] Visual verification at Holtburg confirms: - - The road-edge tree near `0xA9B1` is **gone**. - - Issue #49's missing scenery is **still visible**. - - No new visual regressions in surrounding landblocks during a brief flight. -- [x] Phase N.1 marked shipped in `docs/plans/2026-04-11-roadmap.md`. -- [x] `SceneryGenerator.Generate` calls only WB helpers for displacement / road / slope / rotation / scale. -- [x] Issue #49 stays closed; no new related issues filed. diff --git a/docs/superpowers/plans/2026-05-08-phase-n3-texture-decode-via-wb.md b/docs/superpowers/plans/2026-05-08-phase-n3-texture-decode-via-wb.md deleted file mode 100644 index 57d6f26..0000000 --- a/docs/superpowers/plans/2026-05-08-phase-n3-texture-decode-via-wb.md +++ /dev/null @@ -1,721 +0,0 @@ -# Phase N.3 β€” Texture Decoding via WorldBuilder Implementation Plan - -> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. - -**Goal:** Replace acdream's hand-rolled pixel-format decoders in `SurfaceDecoder` with calls to WorldBuilder's `TextureHelpers.Fill*` methods for every format WB covers (INDEX16, P8, A8R8G8B8, R8G8B8, A8, A8Additive, R5G6B5, A4R4G4B4). Keep our decoders for formats WB lacks (X8R8G8B8, DXT1/3/5 with clipmap postprocess, SolidColor with translucency). Add conformance tests proving byte-identical output for each substituted format. Add the two previously-unsupported formats (R5G6B5, A4R4G4B4) as a bonus. - -**Architecture:** In-place substitution inside `SurfaceDecoder`. Each private `Decode*` method that has a WB equivalent gets rewritten to allocate a `byte[]`, call `TextureHelpers.Fill*` into it, and return a `DecodedTexture`. The critical A8 divergence is resolved by adding an `isAdditive` parameter to `DecodeRenderSurface` β€” callers that know the `SurfaceType` pass it, terrain alpha callers (which always use the additive/replicate path) pass `isAdditive: true`. No feature flag β€” conformance tests prove equivalence before substitution, so the old code is deleted in the same pass. - -**Tech Stack:** .NET 10 / C# 13, `Chorizite.OpenGLSDLBackend` (already referenced via `AcDream.Core.csproj`), `DatReaderWriter` for `RenderSurface` / `Palette` / `PixelFormat` types, `BCnEncoder.Net` for DXT (stays ours), xUnit for tests. - -**Spec:** `docs/superpowers/specs/2026-05-08-phase-n-worldbuilder-migration-design.md` -**Inventory:** `docs/architecture/worldbuilder-inventory.md` -**Handoff:** `docs/research/2026-05-08-phase-n3-handoff.md` - -**Prerequisite:** Phase N.0 shipped (submodule wired), Phase N.1 shipped (scenery migration). `AcDream.Core.csproj` already references `Chorizite.OpenGLSDLBackend`. - ---- - -## Audit Summary - -| # | Our function | WB equivalent | Action | -|---|---|---|---| -| 1 | `DecodeIndex16` | `TextureHelpers.FillIndex16` | **Substitute** | -| 2 | `DecodeP8` | `TextureHelpers.FillP8` | **Substitute** | -| 3 | `DecodeA8R8G8B8` | `TextureHelpers.FillA8R8G8B8` | **Substitute** | -| 4 | `DecodeR8G8B8` | `TextureHelpers.FillR8G8B8` | **Substitute** | -| 5 | `DecodeA8` | `TextureHelpers.FillA8` + `FillA8Additive` | **Substitute** (additive-aware) | -| 6 | `DecodeX8R8G8B8` | None | **Keep ours** | -| 7 | `DecodeBc` (DXT1/3/5) | None in TextureHelpers | **Keep ours** | -| 8 | `DecodeSolidColor` | Different semantics | **Keep ours** | -| 9 | (missing) | `TextureHelpers.FillR5G6B5` | **Add new** | -| 10 | (missing) | `TextureHelpers.FillA4R4G4B4` | **Add new** | - -### A8 divergence detail - -- **Our current `DecodeA8`:** R=G=B=A=val (all four channels = alpha byte) -- **WB `FillA8`:** R=G=B=255, A=val (white + alpha) -- **WB `FillA8Additive`:** R=G=B=A=val (same as our current behavior) - -WB dispatches based on `surface.Type.HasFlag(SurfaceType.Additive)`: -- Additive surfaces β†’ `FillA8Additive` (R=G=B=A=val) -- Non-additive surfaces β†’ `FillA8` (R=G=B=255, A=val) - -Our current code always does the additive path. This is correct for terrain alpha masks (used as blend weights where `.r` channel = `.a` channel matters) but diverges from WB for non-additive A8 entity textures. Resolution: thread an `isAdditive` flag through the decode API. - ---- - -## File Plan - -| File | Disposition | Responsibility | -|---|---|---| -| `src/AcDream.Core/Textures/SurfaceDecoder.cs` | MODIFY | Replace 5 private decode methods with WB `TextureHelpers.Fill*` calls. Add `isAdditive` parameter to `DecodeRenderSurface`. Add R5G6B5 + A4R4G4B4 format cases. Keep X8R8G8B8, DXT, SolidColor. | -| `src/AcDream.App/Rendering/TextureCache.cs` | MODIFY | Pass `surface.Type.HasFlag(SurfaceType.Additive)` as `isAdditive` to `SurfaceDecoder.DecodeRenderSurface`. | -| `src/AcDream.App/Rendering/TerrainAtlas.cs` | MODIFY | Pass `isAdditive: true` to `SurfaceDecoder.DecodeRenderSurface` in `TryDecodeAlphaMap` (terrain alpha masks always use the replicate-all-channels path). | -| `tests/AcDream.Core.Tests/Textures/TextureDecodeConformanceTests.cs` | NEW | Per-format conformance tests: synthetic byte arrays decoded by both our old logic and WB's `TextureHelpers.Fill*`, asserting byte-identical output. | - ---- - -## Task 1: Conformance tests for the 5 clean substitutions - -Write tests first, run them to prove our current output matches WB's output for each format. These tests lock in the equivalence BEFORE any code changes β€” if any test fails, we know the formats actually diverge and must investigate. - -**Files:** -- Create: `tests/AcDream.Core.Tests/Textures/TextureDecodeConformanceTests.cs` - -- [ ] **Step 1.1: Create the conformance test file with INDEX16 test** - -Create `tests/AcDream.Core.Tests/Textures/TextureDecodeConformanceTests.cs`: - -```csharp -using Chorizite.OpenGLSDLBackend.Lib; -using DatReaderWriter.DBObjs; -using DatReaderWriter.Types; - -namespace AcDream.Core.Tests.Textures; - -/// -/// Conformance tests proving WorldBuilder's TextureHelpers.Fill* methods -/// produce byte-identical output to our SurfaceDecoder private methods -/// for each pixel format. These tests run BEFORE the substitution β€” if -/// one fails, the formats diverge and we must investigate, not "fix" the test. -/// -public sealed class TextureDecodeConformanceTests -{ - [Fact] - public void FillIndex16_MatchesOurDecodeIndex16() - { - // 2x2 INDEX16 texture: 4 pixels, each a 16-bit LE palette index. - // Palette: index 0 = (R=10, G=20, B=30, A=255), index 1 = (R=40, G=50, B=60, A=200) - // Pixel data: [0x0000, 0x0100, 0x0100, 0x0000] (indices 0, 1, 1, 0) - byte[] src = [0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00]; - int w = 2, h = 2; - - var palette = new Palette(); - palette.Colors.Add(new ColorARGB { Red = 10, Green = 20, Blue = 30, Alpha = 255 }); - palette.Colors.Add(new ColorARGB { Red = 40, Green = 50, Blue = 60, Alpha = 200 }); - - // Our decode - byte[] ours = new byte[w * h * 4]; - for (int i = 0; i < w * h; i++) - { - int si = i * 2; - ushort idx = (ushort)(src[si] | (src[si + 1] << 8)); - var c = palette.Colors[idx]; - int di = i * 4; - ours[di + 0] = c.Red; - ours[di + 1] = c.Green; - ours[di + 2] = c.Blue; - ours[di + 3] = c.Alpha; - } - - // WB decode - byte[] wb = new byte[w * h * 4]; - TextureHelpers.FillIndex16(src, palette, wb.AsSpan(), w, h); - - Assert.Equal(ours, wb); - } - - [Fact] - public void FillIndex16_ClipMap_MatchesOurClipMapBehavior() - { - // Index 3 (< 8) should be transparent, index 10 should be normal - byte[] src = [0x03, 0x00, 0x0A, 0x00]; - int w = 2, h = 1; - - var palette = new Palette(); - for (int i = 0; i < 16; i++) - palette.Colors.Add(new ColorARGB { Red = (byte)(i * 10), Green = (byte)(i * 15), Blue = (byte)(i * 5), Alpha = 255 }); - - // Our clipmap decode: index < 8 β†’ all zeros - byte[] ours = new byte[w * h * 4]; - for (int i = 0; i < w * h; i++) - { - int si = i * 2; - ushort idx = (ushort)(src[si] | (src[si + 1] << 8)); - int di = i * 4; - if (idx < 8) - { - ours[di] = ours[di + 1] = ours[di + 2] = ours[di + 3] = 0; - } - else - { - var c = palette.Colors[idx]; - ours[di + 0] = c.Red; - ours[di + 1] = c.Green; - ours[di + 2] = c.Blue; - ours[di + 3] = c.Alpha; - } - } - - byte[] wb = new byte[w * h * 4]; - TextureHelpers.FillIndex16(src, palette, wb.AsSpan(), w, h, isClipMap: true); - - Assert.Equal(ours, wb); - } - - [Fact] - public void FillP8_MatchesOurDecodeP8() - { - // 2x2 P8 texture: 4 pixels, each a single-byte palette index. - byte[] src = [0, 1, 1, 0]; - int w = 2, h = 2; - - var palette = new Palette(); - palette.Colors.Add(new ColorARGB { Red = 100, Green = 110, Blue = 120, Alpha = 255 }); - palette.Colors.Add(new ColorARGB { Red = 200, Green = 210, Blue = 220, Alpha = 180 }); - - byte[] ours = new byte[w * h * 4]; - for (int i = 0; i < w * h; i++) - { - var c = palette.Colors[src[i]]; - int di = i * 4; - ours[di + 0] = c.Red; - ours[di + 1] = c.Green; - ours[di + 2] = c.Blue; - ours[di + 3] = c.Alpha; - } - - byte[] wb = new byte[w * h * 4]; - TextureHelpers.FillP8(src, palette, wb.AsSpan(), w, h); - - Assert.Equal(ours, wb); - } - - [Fact] - public void FillA8R8G8B8_MatchesOurDecodeA8R8G8B8() - { - // 2x1 A8R8G8B8: on-disk order is B, G, R, A per pixel - byte[] src = [0x10, 0x20, 0x30, 0x40, 0xAA, 0xBB, 0xCC, 0xDD]; - int w = 2, h = 1; - - // Our decode: swap B,G,R,A β†’ R,G,B,A - byte[] ours = new byte[w * h * 4]; - for (int i = 0; i < w * h; i++) - { - int s = i * 4; - ours[s + 0] = src[s + 2]; // R - ours[s + 1] = src[s + 1]; // G - ours[s + 2] = src[s + 0]; // B - ours[s + 3] = src[s + 3]; // A - } - - byte[] wb = new byte[w * h * 4]; - TextureHelpers.FillA8R8G8B8(src, wb.AsSpan(), w, h); - - Assert.Equal(ours, wb); - } - - [Fact] - public void FillR8G8B8_MatchesOurDecodeR8G8B8() - { - // 2x1 R8G8B8: on-disk order is B, G, R per pixel (3 bytes) - byte[] src = [0x10, 0x20, 0x30, 0xAA, 0xBB, 0xCC]; - int w = 2, h = 1; - - // Our decode: swap B,G,R β†’ R,G,B,255 - byte[] ours = new byte[w * h * 4]; - for (int i = 0; i < w * h; i++) - { - int si = i * 3; - int di = i * 4; - ours[di + 0] = src[si + 2]; // R - ours[di + 1] = src[si + 1]; // G - ours[di + 2] = src[si + 0]; // B - ours[di + 3] = 0xFF; - } - - byte[] wb = new byte[w * h * 4]; - TextureHelpers.FillR8G8B8(src, wb.AsSpan(), w, h); - - Assert.Equal(ours, wb); - } - - [Fact] - public void FillA8Additive_MatchesOurDecodeA8() - { - // 4x1 A8: each byte replicated to all four channels (our current behavior) - byte[] src = [0x00, 0x80, 0xFF, 0x42]; - int w = 4, h = 1; - - byte[] ours = new byte[w * h * 4]; - for (int i = 0; i < w * h; i++) - { - byte a = src[i]; - int d = i * 4; - ours[d + 0] = a; - ours[d + 1] = a; - ours[d + 2] = a; - ours[d + 3] = a; - } - - byte[] wb = new byte[w * h * 4]; - TextureHelpers.FillA8Additive(src, wb.AsSpan(), w, h); - - Assert.Equal(ours, wb); - } - - [Fact] - public void FillA8_NonAdditive_ProducesWhitePlusAlpha() - { - // WB's non-additive A8: R=G=B=255, A=val - // This is DIFFERENT from our current DecodeA8 (which does R=G=B=A=val). - // This test documents the WB behavior we're adopting for non-additive surfaces. - byte[] src = [0x00, 0x80, 0xFF, 0x42]; - int w = 4, h = 1; - - byte[] expected = new byte[w * h * 4]; - for (int i = 0; i < w * h; i++) - { - int d = i * 4; - expected[d + 0] = 255; - expected[d + 1] = 255; - expected[d + 2] = 255; - expected[d + 3] = src[i]; - } - - byte[] wb = new byte[w * h * 4]; - TextureHelpers.FillA8(src, wb.AsSpan(), w, h); - - Assert.Equal(expected, wb); - } - - [Fact] - public void FillR5G6B5_ProducesExpectedRgba() - { - // R5G6B5: 16-bit packed RGB. Not currently handled by our decoder. - // White (0xFFFF) β†’ R=248,G=252,B=248,A=255 (bit expansion truncation) - // Black (0x0000) β†’ R=0,G=0,B=0,A=255 - byte[] src = [0xFF, 0xFF, 0x00, 0x00]; - int w = 2, h = 1; - - byte[] wb = new byte[w * h * 4]; - TextureHelpers.FillR5G6B5(src, wb.AsSpan(), w, h); - - // Pixel 0: white-ish - Assert.Equal(248, wb[0]); // R: 31 << 3 - Assert.Equal(252, wb[1]); // G: 63 << 2 - Assert.Equal(248, wb[2]); // B: 31 << 3 - Assert.Equal(255, wb[3]); // A - - // Pixel 1: black - Assert.Equal(0, wb[4]); - Assert.Equal(0, wb[5]); - Assert.Equal(0, wb[6]); - Assert.Equal(255, wb[7]); - } - - [Fact] - public void FillA4R4G4B4_ProducesExpectedRgba() - { - // A4R4G4B4: 16-bit packed ARGB. Not currently handled by our decoder. - // 0xF8C4 β†’ A=15*17=255, R=8*17=136, G=12*17=204, B=4*17=68 - byte[] src = [0xC4, 0xF8]; - int w = 1, h = 1; - - byte[] wb = new byte[w * h * 4]; - TextureHelpers.FillA4R4G4B4(src, wb.AsSpan(), w, h); - - Assert.Equal(136, wb[0]); // R: ((0xF8C4 >> 8) & 0x0F) * 17 = 8*17 - Assert.Equal(204, wb[1]); // G: ((0xF8C4 >> 4) & 0x0F) * 17 = 12*17 - Assert.Equal(68, wb[2]); // B: (0xF8C4 & 0x0F) * 17 = 4*17 - Assert.Equal(255, wb[3]); // A: ((0xF8C4 >> 12) & 0x0F) * 17 = 15*17 - } -} -``` - -- [ ] **Step 1.2: Run tests to verify they pass** - -Run: `dotnet test tests/AcDream.Core.Tests --filter "FullyQualifiedName~TextureDecodeConformanceTests" --verbosity normal` - -Expected: All 9 tests PASS. These tests compare our current algorithm inline against WB's `TextureHelpers` β€” if any fail, it means the algorithms actually diverge and we must investigate before proceeding. - -- [ ] **Step 1.3: Commit** - -``` -git add tests/AcDream.Core.Tests/Textures/TextureDecodeConformanceTests.cs -git commit -m "test(N.3): conformance tests proving WB TextureHelpers matches our decode - -Nine tests covering INDEX16 (normal + clipmap), P8, A8R8G8B8, R8G8B8, -A8Additive (matches our current DecodeA8), A8 non-additive (documents -the divergence), R5G6B5, A4R4G4B4. All run before any substitution β€” -they prove equivalence, not test the substitution. - -Co-Authored-By: Claude Opus 4.6 " -``` - ---- - -## Task 2: Add `isAdditive` parameter to SurfaceDecoder and wire A8 split - -Thread the `isAdditive` flag through the decode API so the A8 format can dispatch to either WB path. Update all three callers. - -**Files:** -- Modify: `src/AcDream.Core/Textures/SurfaceDecoder.cs` -- Modify: `src/AcDream.App/Rendering/TextureCache.cs` -- Modify: `src/AcDream.App/Rendering/TerrainAtlas.cs` - -- [ ] **Step 2.1: Add `isAdditive` parameter to `DecodeRenderSurface`** - -In `src/AcDream.Core/Textures/SurfaceDecoder.cs`, change the main public overload signature from: - -```csharp -public static DecodedTexture DecodeRenderSurface(RenderSurface rs, Palette? palette, bool isClipMap = false) -``` - -to: - -```csharp -public static DecodedTexture DecodeRenderSurface(RenderSurface rs, Palette? palette, bool isClipMap = false, bool isAdditive = false) -``` - -And update the `PFID_A8`/`PFID_CUSTOM_LSCAPE_ALPHA` case in the switch from: - -```csharp -PixelFormat.PFID_A8 or PixelFormat.PFID_CUSTOM_LSCAPE_ALPHA => DecodeA8(rs), -``` - -to: - -```csharp -PixelFormat.PFID_A8 or PixelFormat.PFID_CUSTOM_LSCAPE_ALPHA => DecodeA8(rs, isAdditive), -``` - -And update the no-palette overload from: - -```csharp -public static DecodedTexture DecodeRenderSurface(RenderSurface rs) - => DecodeRenderSurface(rs, palette: null); -``` - -to: - -```csharp -public static DecodedTexture DecodeRenderSurface(RenderSurface rs) - => DecodeRenderSurface(rs, palette: null, isClipMap: false, isAdditive: false); -``` - -- [ ] **Step 2.2: Split `DecodeA8` into additive vs non-additive** - -In `SurfaceDecoder.cs`, change the `DecodeA8` method signature and add the split: - -```csharp -private static DecodedTexture DecodeA8(RenderSurface rs, bool isAdditive) -{ - int expected = rs.Width * rs.Height; - if (rs.SourceData.Length < expected) - return DecodedTexture.Magenta; - - var rgba = new byte[expected * 4]; - if (isAdditive) - { - // Additive: R=G=B=A=val (current behavior, matches WB FillA8Additive) - for (int i = 0; i < expected; i++) - { - byte a = rs.SourceData[i]; - int d = i * 4; - rgba[d + 0] = a; - rgba[d + 1] = a; - rgba[d + 2] = a; - rgba[d + 3] = a; - } - } - else - { - // Non-additive: R=G=B=255, A=val (matches WB FillA8) - for (int i = 0; i < expected; i++) - { - int d = i * 4; - rgba[d + 0] = 255; - rgba[d + 1] = 255; - rgba[d + 2] = 255; - rgba[d + 3] = rs.SourceData[i]; - } - } - return new DecodedTexture(rgba, rs.Width, rs.Height); -} -``` - -- [ ] **Step 2.3: Update TextureCache to pass `isAdditive`** - -In `src/AcDream.App/Rendering/TextureCache.cs`, in `DecodeFromDats`, change line 203 from: - -```csharp -return SurfaceDecoder.DecodeRenderSurface(rs, effectivePalette, isClipMap); -``` - -to: - -```csharp -bool isAdditive = surface.Type.HasFlag(SurfaceType.Additive); -return SurfaceDecoder.DecodeRenderSurface(rs, effectivePalette, isClipMap, isAdditive); -``` - -- [ ] **Step 2.4: Update TerrainAtlas to pass `isAdditive: true`** - -In `src/AcDream.App/Rendering/TerrainAtlas.cs`, in `TryDecodeAlphaMap`, change line 322 from: - -```csharp -var d = SurfaceDecoder.DecodeRenderSurface(rs, palette: null); -``` - -to: - -```csharp -var d = SurfaceDecoder.DecodeRenderSurface(rs, palette: null, isClipMap: false, isAdditive: true); -``` - -The terrain alpha masks MUST use the additive path (R=G=B=A=val) because our terrain blending shader reads from `.r` for the blend weight. - -- [ ] **Step 2.5: Build and test** - -Run: `dotnet build --verbosity quiet && dotnet test tests/AcDream.Core.Tests --filter "FullyQualifiedName~TextureDecodeConformanceTests" --verbosity normal` - -Expected: Build green, all 9 conformance tests still pass. - -- [ ] **Step 2.6: Commit** - -``` -git add src/AcDream.Core/Textures/SurfaceDecoder.cs src/AcDream.App/Rendering/TextureCache.cs src/AcDream.App/Rendering/TerrainAtlas.cs -git commit -m "refactor(N.3): thread isAdditive through A8 decode path - -SurfaceDecoder.DecodeRenderSurface now accepts isAdditive parameter. -A8/CUSTOM_LSCAPE_ALPHA format splits: -- isAdditive=true: R=G=B=A=val (terrain alpha, additive entity textures) -- isAdditive=false: R=G=B=255, A=val (non-additive entity textures) - -TextureCache passes surface.Type.HasFlag(SurfaceType.Additive). -TerrainAtlas passes isAdditive:true (alpha masks always replicate). -This aligns with WB ObjectMeshManager's dispatch logic. - -Co-Authored-By: Claude Opus 4.6 " -``` - ---- - -## Task 3: Substitute 5 decode methods with WB TextureHelpers calls - -Replace the body of each private decode method with a call to the corresponding WB `TextureHelpers.Fill*` method. Add the two new format cases (R5G6B5, A4R4G4B4). - -**Files:** -- Modify: `src/AcDream.Core/Textures/SurfaceDecoder.cs` - -- [ ] **Step 3.1: Add WB using directive** - -At the top of `SurfaceDecoder.cs`, add: - -```csharp -using Chorizite.OpenGLSDLBackend.Lib; -``` - -- [ ] **Step 3.2: Replace `DecodeIndex16`** - -Replace the body of `DecodeIndex16` with: - -```csharp -private static DecodedTexture DecodeIndex16(RenderSurface rs, Palette palette, bool isClipMap) -{ - int expectedBytes = rs.Width * rs.Height * 2; - if (rs.SourceData.Length < expectedBytes || palette.Colors.Count == 0) - return DecodedTexture.Magenta; - - var rgba = new byte[rs.Width * rs.Height * 4]; - TextureHelpers.FillIndex16(rs.SourceData, palette, rgba.AsSpan(), rs.Width, rs.Height, isClipMap); - return new DecodedTexture(rgba, rs.Width, rs.Height); -} -``` - -- [ ] **Step 3.3: Replace `DecodeP8`** - -Replace the body of `DecodeP8` with: - -```csharp -private static DecodedTexture DecodeP8(RenderSurface rs, Palette palette, bool isClipMap) -{ - int expectedBytes = rs.Width * rs.Height; - if (rs.SourceData.Length < expectedBytes || palette.Colors.Count == 0) - return DecodedTexture.Magenta; - - var rgba = new byte[rs.Width * rs.Height * 4]; - TextureHelpers.FillP8(rs.SourceData, palette, rgba.AsSpan(), rs.Width, rs.Height, isClipMap); - return new DecodedTexture(rgba, rs.Width, rs.Height); -} -``` - -- [ ] **Step 3.4: Replace `DecodeA8R8G8B8`** - -Replace the body of `DecodeA8R8G8B8` with: - -```csharp -private static DecodedTexture DecodeA8R8G8B8(RenderSurface rs) -{ - int expected = rs.Width * rs.Height * 4; - if (rs.SourceData.Length < expected) - return DecodedTexture.Magenta; - - var rgba = new byte[expected]; - TextureHelpers.FillA8R8G8B8(rs.SourceData, rgba.AsSpan(), rs.Width, rs.Height); - return new DecodedTexture(rgba, rs.Width, rs.Height); -} -``` - -- [ ] **Step 3.5: Replace `DecodeR8G8B8`** - -Replace the body of `DecodeR8G8B8` with: - -```csharp -private static DecodedTexture DecodeR8G8B8(RenderSurface rs) -{ - int expectedBytes = rs.Width * rs.Height * 3; - if (rs.SourceData.Length < expectedBytes) - return DecodedTexture.Magenta; - - var rgba = new byte[rs.Width * rs.Height * 4]; - TextureHelpers.FillR8G8B8(rs.SourceData, rgba.AsSpan(), rs.Width, rs.Height); - return new DecodedTexture(rgba, rs.Width, rs.Height); -} -``` - -- [ ] **Step 3.6: Replace `DecodeA8`** - -Replace the body of `DecodeA8` with: - -```csharp -private static DecodedTexture DecodeA8(RenderSurface rs, bool isAdditive) -{ - int expected = rs.Width * rs.Height; - if (rs.SourceData.Length < expected) - return DecodedTexture.Magenta; - - var rgba = new byte[expected * 4]; - if (isAdditive) - TextureHelpers.FillA8Additive(rs.SourceData, rgba.AsSpan(), rs.Width, rs.Height); - else - TextureHelpers.FillA8(rs.SourceData, rgba.AsSpan(), rs.Width, rs.Height); - return new DecodedTexture(rgba, rs.Width, rs.Height); -} -``` - -- [ ] **Step 3.7: Add R5G6B5 and A4R4G4B4 cases to the format switch** - -In the `DecodeRenderSurface` switch, add two new cases before the `_ => DecodedTexture.Magenta` default: - -```csharp -PixelFormat.PFID_R5G6B5 => DecodeR5G6B5(rs), -PixelFormat.PFID_A4R4G4B4 => DecodeA4R4G4B4(rs), -``` - -And add the two new private methods: - -```csharp -private static DecodedTexture DecodeR5G6B5(RenderSurface rs) -{ - int expectedBytes = rs.Width * rs.Height * 2; - if (rs.SourceData.Length < expectedBytes) - return DecodedTexture.Magenta; - - var rgba = new byte[rs.Width * rs.Height * 4]; - TextureHelpers.FillR5G6B5(rs.SourceData, rgba.AsSpan(), rs.Width, rs.Height); - return new DecodedTexture(rgba, rs.Width, rs.Height); -} - -private static DecodedTexture DecodeA4R4G4B4(RenderSurface rs) -{ - int expectedBytes = rs.Width * rs.Height * 2; - if (rs.SourceData.Length < expectedBytes) - return DecodedTexture.Magenta; - - var rgba = new byte[rs.Width * rs.Height * 4]; - TextureHelpers.FillA4R4G4B4(rs.SourceData, rgba.AsSpan(), rs.Width, rs.Height); - return new DecodedTexture(rgba, rs.Width, rs.Height); -} -``` - -- [ ] **Step 3.8: Build and run all tests** - -Run: `dotnet build --verbosity quiet && dotnet test --verbosity quiet` - -Expected: Build green, 873+ tests pass, 8 pre-existing failures unchanged. - -- [ ] **Step 3.9: Commit** - -``` -git add src/AcDream.Core/Textures/SurfaceDecoder.cs -git commit -m "phase(N.3): substitute 5 decode methods with WB TextureHelpers - -INDEX16, P8, A8R8G8B8, R8G8B8, A8 now delegate to -TextureHelpers.FillIndex16/FillP8/FillA8R8G8B8/FillR8G8B8/ -FillA8/FillA8Additive. Validation + DecodedTexture wrapping stays ours. -X8R8G8B8, DXT1/3/5, SolidColor remain our implementations (no WB equiv). - -Bonus: R5G6B5 + A4R4G4B4 formats now handled (previously fell to magenta). - -Co-Authored-By: Claude Opus 4.6 " -``` - ---- - -## Task 4: Update roadmap + ISSUES, final cleanup - -**Files:** -- Modify: `docs/plans/2026-04-11-roadmap.md` β€” mark N.3 shipped -- Modify: `docs/ISSUES.md` β€” file any cosmetic deltas found - -- [ ] **Step 4.1: Update roadmap** - -In the roadmap, update the Phase N.3 entry to show shipped status with today's date and commit hash (obtain from `git log -1 --format='%h'`). - -- [ ] **Step 4.2: File any ISSUES** - -If the A8 non-additive behavioral change surfaces any visual delta at Holtburg during verification, file it in `docs/ISSUES.md`. Example: - -```markdown -### #NN: A8 non-additive textures now render white+alpha instead of gray+alpha - -**Status:** OPEN -**Phase:** N.3 -**Symptom:** [describe if applicable] -**Root cause:** WB's FillA8 outputs R=G=B=255,A=val; our old DecodeA8 output R=G=B=A=val. For non-additive surfaces this is a behavioral change. -**Impact:** [assess after visual verification] -``` - -If no visual delta is observed, skip this step β€” no issue to file. - -- [ ] **Step 4.3: Commit** - -``` -git add docs/plans/2026-04-11-roadmap.md docs/ISSUES.md -git commit -m "docs: mark Phase N.3 shipped, update ISSUES if applicable - -Co-Authored-By: Claude Opus 4.6 " -``` - ---- - -## Task 5: Visual verification (human-in-the-loop) - -This task requires the user to launch the client and inspect textures at Holtburg. - -- [ ] **Step 5.1: Build and launch** - -```powershell -dotnet build --verbosity quiet -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -$env:ACDREAM_TEST_HOST = "127.0.0.1" -$env:ACDREAM_TEST_PORT = "9000" -$env:ACDREAM_TEST_USER = "testaccount" -$env:ACDREAM_TEST_PASS = "testpassword" -dotnet run --project src\AcDream.App\AcDream.App.csproj --no-build -c Debug 2>&1 | Tee-Object -FilePath "launch.log" -``` - -- [ ] **Step 5.2: Visual checks** - -Walk around Holtburg and verify: -1. **Terrain textures** β€” grass, dirt, sand transitions look correct (not magenta, not discolored) -2. **Tree/bush textures** β€” scenery objects textured correctly (clipmap alpha works) -3. **Building textures** β€” walls, roofs, doors look right -4. **Sky/clouds** β€” if A8 textures are involved, verify they still render -5. **Particles** β€” rain/aurora if weather is active - -If all look correct, N.3 is done. If regressions found, file in ISSUES.md per the handoff doc's "whackamole stops the migration" rule. diff --git a/docs/superpowers/plans/2026-05-08-phase-n4-rendering-foundation.md b/docs/superpowers/plans/2026-05-08-phase-n4-rendering-foundation.md deleted file mode 100644 index 4b4e401..0000000 --- a/docs/superpowers/plans/2026-05-08-phase-n4-rendering-foundation.md +++ /dev/null @@ -1,2737 +0,0 @@ -# Phase N.4 β€” Rendering Pipeline Foundation Implementation Plan - -> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. - -**Goal:** Adopt WB's `ObjectMeshManager` + `TextureAtlasManager` as acdream's rendering pipeline foundation. Two-tier split (atlas for shared procedural content; per-instance path for customized server-spawned entities). Animation by per-draw matrix composition with `AnimationSequencer` untouched. Streaming integration via ~200 LOC adapter shim. Surface metadata preserved via side-table (no fork patches). Single shippable phase, 3-4 weeks. Ships no visible change. - -**Architecture:** Strangler-fig substitution behind `ACDREAM_USE_WB_FOUNDATION=1` feature flag. Conformance tests run before substitution per N.1/N.3 pattern. Week-by-week sequencing minimizes "broken in middle" state: week 1 brings up plumbing + atlas for static scenery; week 2 wires streaming; week 3 adds per-instance path + animation; week 4 polishes + ships. Foundation enables N.5/N.6/N.7/N.8 to be smaller integration phases on top. - -**Tech Stack:** .NET 10 / C# 13 Β· Silk.NET.OpenGL (transitively via WB) Β· Chorizite.OpenGLSDLBackend (already referenced) Β· BCnEncoder.Net (transitively) Β· xUnit Β· `Chorizite.Core.Render` interfaces. - -**Spec:** [docs/superpowers/specs/2026-05-08-phase-n4-rendering-foundation-design.md](../specs/2026-05-08-phase-n4-rendering-foundation-design.md) β€” read FIRST. Everything below assumes you've read it. -**Parent design:** [docs/superpowers/specs/2026-05-08-phase-n-worldbuilder-migration-design.md](../specs/2026-05-08-phase-n-worldbuilder-migration-design.md) -**Inventory:** [docs/architecture/worldbuilder-inventory.md](../../architecture/worldbuilder-inventory.md) -**Roadmap:** [docs/plans/2026-04-11-roadmap.md](../../plans/2026-04-11-roadmap.md) β€” N.4 entry - -**Prerequisites:** -- Phase N.0 shipped (commit `c8782c9`) β€” WB submodule + project references wired up. -- Phase N.1 shipped (merge `1978ef9`) β€” scenery via WB helpers. -- Phase N.3 shipped (merge `13132f9`) β€” texture decode via WB `TextureHelpers`. -- Build green, 883 tests passing, 8 pre-existing failures only. -- Worktree: `.claude/worktrees/quirky-jepsen-fd60f1` on branch `claude/quirky-jepsen-fd60f1`. - ---- - -## File Plan - -| File | Disposition | Responsibility | -|---|---|---| -| `src/AcDream.App/Rendering/Wb/WbMeshAdapter.cs` | NEW | Owns the `ObjectMeshManager` instance. Exposes `IncrementRefCount` / `DecrementRefCount` / `GetRenderData` / `Dispose`. The single seam between acdream and WB's render pipeline. | -| `src/AcDream.App/Rendering/Wb/AcSurfaceMetadata.cs` | NEW | Record holding `Translucency` / `Luminosity` / `Diffuse` / `SurfOpacity` / `NeedsUvRepeat` / `DisableFog` (the AC-specific surface properties WB's `MeshBatchData` doesn't carry). | -| `src/AcDream.App/Rendering/Wb/AcSurfaceMetadataTable.cs` | NEW | `Dictionary<(ulong gfxObjId, int surfaceIdx), AcSurfaceMetadata>` side-table. Populated at mesh-extraction time; queried at draw time. Thread-safe (`ConcurrentDictionary`). | -| `src/AcDream.App/Rendering/Wb/LandblockSpawnAdapter.cs` | NEW | Streaming-loader hook. Walks `LandblockEntry.Setups[]` / `Statics[]` and calls `WbMeshAdapter.IncrementRefCount` per unique GfxObj. Companion unload path. | -| `src/AcDream.App/Rendering/Wb/EntitySpawnAdapter.cs` | NEW | Network-spawn hook. Routes `CreateObject` to per-instance path via existing `TextureCache.GetOrUploadWithPaletteOverride`. Builds per-entity `AnimatedEntityState`. | -| `src/AcDream.App/Rendering/Wb/AnimatedEntityState.cs` | NEW | Per-entity render state for animated entities: `partGfxObjOverrides` (AnimPartChange), `hiddenMask` (HiddenParts), reference to existing `AnimationSequencer`. | -| `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` | NEW | Per-frame draw loop. Walks visible entities, looks up `ObjectRenderData`, composes per-part matrices (entity Γ— animation Γ— rest-pose), reads side-table, issues GL draws. | -| `src/AcDream.App/Rendering/Wb/WbFoundationFlag.cs` | NEW | Static flag gate: `WbFoundationFlag.IsEnabled` reads `ACDREAM_USE_WB_FOUNDATION` env var once at process start, exposes a single `bool`. Other call sites import this rather than re-reading the env var. | -| `tests/AcDream.Core.Tests/Rendering/Wb/MeshExtractionConformanceTests.cs` | NEW | Conformance: our `GfxObjMesh.Build` vs WB's algorithm output. | -| `tests/AcDream.Core.Tests/Rendering/Wb/SetupFlattenConformanceTests.cs` | NEW | Conformance: our `SetupMesh.Flatten` vs WB's setup-parts walk. | -| `tests/AcDream.Core.Tests/Rendering/Wb/PerInstanceDecodeConformanceTests.cs` | NEW | Conformance: per-instance customization decode produces identical RGBA8 vs `TextureCache.GetOrUploadWithPaletteOverride`. | -| `tests/AcDream.Core.Tests/Rendering/Wb/AcSurfaceMetadataTableTests.cs` | NEW | Round-trip + thread-safety smoke. | -| `tests/AcDream.Core.Tests/Rendering/Wb/LandblockSpawnAdapterTests.cs` | NEW | Register/unregister + dedup. | -| `tests/AcDream.Core.Tests/Rendering/Wb/EntitySpawnAdapterTests.cs` | NEW | Routes CreateObject with palette override to per-instance path. | -| `tests/AcDream.Core.Tests/Rendering/Wb/MatrixCompositionTests.cs` | NEW | Entity Γ— animation Γ— rest-pose matrix composition. | -| `tests/AcDream.Core.Tests/Rendering/Wb/HiddenPartsTests.cs` | NEW | Bitmask suppression. | -| `tests/AcDream.Core.Tests/Rendering/Wb/AnimPartChangeTests.cs` | NEW | Override resolution. | -| `src/AcDream.App/Rendering/StaticMeshRenderer.cs` | MODIFY | Internal swap: `EnsureUploaded` and `Draw` route through `WbMeshAdapter` when `WbFoundationFlag.IsEnabled`. Public surface unchanged. **N.6 fully replaces this file.** | -| `src/AcDream.App/Rendering/InstancedMeshRenderer.cs` | MODIFY | Same pattern β€” internal swap, public surface unchanged. **N.6 fully replaces this file.** | -| `src/AcDream.App/Rendering/TextureCache.cs` | MODIFY | `GetOrUpload(surfaceId)` (atlas-tier callers) routes through `WbMeshAdapter` when flag on. The override paths (`GetOrUploadWithOrigTextureOverride`, `GetOrUploadWithPaletteOverride`) keep current behavior. | -| `src/AcDream.App/Streaming/GpuWorldState.cs` | MODIFY | `AddLandblock` / `RemoveLandblock` call `LandblockSpawnAdapter` when flag on. `AppendLiveEntity` calls `EntitySpawnAdapter` when flag on. Pending-spawn list mechanism preserved verbatim. | -| `src/AcDream.App/Rendering/GameWindow.cs` | MODIFY | Construct `WbMeshAdapter` + `AcSurfaceMetadataTable` + `WbDrawDispatcher` on init. Dispose on shutdown. | -| `docs/plans/2026-04-11-roadmap.md` | MODIFY (final task) | Mark N.4 shipped after visual verification. | -| `CLAUDE.md` | MODIFY (early task) | Add pointer to this plan in the "Roadmap discipline" section so future agents pick it up. | - -**Why this structure:** the `Wb/` subfolder isolates everything new in N.4 from existing renderers. After N.6 fully replaces `StaticMeshRenderer` / `InstancedMeshRenderer`, the `Wb/` folder becomes the canonical rendering implementation. Each new file has one responsibility. Existing files are touched minimally; the bulk of N.4 lives in the new folder. - ---- - -## Plan Living-Document Convention - -This plan is the **execution source of truth** for N.4. It is updated as tasks land: - -- After each commit that completes a task, mark the task's checkboxes βœ… and append the commit SHA next to the task header. -- If a task uncovers an architectural surprise that requires re-planning, add a **`### Adjustment N`** subsection under the affected task with the date, what changed, and why. Do not silently rewrite earlier tasks. -- If a downstream task changes shape because of an earlier task's outcome, append the changes to the downstream task in-place rather than scattering deltas. -- Final commit for the phase updates this header note from "Living document β€” work in progress" to "Final state at β€” phase shipped (merge ``)." - -Status: **Final state at 2026-05-08 β€” phase shipped.** All tasks -complete; `ACDREAM_USE_WB_FOUNDATION` flipped default-on. Visual -verification at Holtburg passed. Three bugs surfaced + resolved during -Task 26 are documented as Adjustments 7-9 below and as gotchas in -CLAUDE.md. Followup work moves to N.5 (modern rendering path: bindless -+ multi-draw indirect). - -**Progress (2026-05-08):** Weeks 1 + 2 + 3 βœ… COMPLETE. WB pipeline running flag-on (constructed + ref-counted + per-frame Tick draining its queues). Per-instance tier wired (`EntitySpawnAdapter` routes server-spawned entities through existing `TextureCache.GetOrUploadWithPaletteOverride` path; per-entity `AnimatedEntityState` accumulates AnimPartChange + HiddenParts data, ready for the dispatcher). Five architectural adjustments documented: 1 (DefaultDatReaderWriter discovery), 2 (renderer is tier-blind), 3 (FPS regression = dual-pipeline cost; resolves at Task 22), 4 (WorldEntity missing HiddenPartsMask + AnimPartChanges fields, plumbing deferred), 5 (Task 20 is structural β€” same function called both paths). Build green, 947 tests pass, 8 pre-existing failures only. - -**Next: Task 22** (Week 4) β€” `WbDrawDispatcher` full draw loop. The first task that actually draws through WB and unlocks the dual-pipeline-cost mitigation from Adjustment 3. - -| Task | Status | Commit | -|---|---|---| -| 1 β€” WbFoundationFlag scaffold | βœ… | `81b5ed8` | -| 2 β€” AcSurfaceMetadata + Table | βœ… | `46deed6` | -| 3 β€” Mesh-extraction conformance | βœ… | `ed73fc5` | -| 4 β€” Setup-flatten conformance | βœ… | `ed73fc5` | -| 5 β€” WbMeshAdapter stub + IWbMeshAdapter | βœ… | (post-`ed73fc5`) | -| 6 β€” WbDatReaderAdapter | βœ… OBSOLETED (Adj. 1) | `502c3a8` | -| 7 β€” GameWindow wiring under flag | βœ… | `502c3a8` | -| 8 β€” CLAUDE.md pointer | βœ… | `506b86b` (preemptive) | -| 9 β€” Real WB pipeline + InstancedMeshRenderer routing | βœ… partial / Adj. 2 reverted | `4ad7a98` + `4f318bc` | -| 10 β€” Week 1 wrap-up | βœ… | `c49c6ed` | -| 11 β€” LandblockSpawnAdapter | βœ… | `669768d` | -| 12 β€” Wire into GpuWorldState | βœ… | `931a690` | -| 13 β€” Memory budget verification | βœ… deferred to Task 22 (Adj. 3) | β€” | -| 14 β€” Pending-spawn integration test | βœ… | `f4f0101` | -| Tick β€” drain WB pipeline queues | βœ… added per Adj. 3 | `bf53cb4` | -| 15 β€” Week 2 wrap-up | βœ… | `36f7a60` | -| 16+18+19 β€” AnimatedEntityState + AnimPartChange + HiddenParts | βœ… | `ce72c57` | -| 17 β€” EntitySpawnAdapter | βœ… + Adj. 4 | `c02c307` | -| 20 β€” Per-instance decode conformance | βœ… structural (Adj. 5) | (no test file) | -| 21 β€” Week 3 wrap-up | βœ… | (this commit) | -| 22+23 β€” WbDrawDispatcher + side-table population | βœ… | `01cff41` | -| 22+23 fixup β€” load triggers + SurfaceId source | βœ… | `943652d` | -| 22+23 perf β€” FirstIndex/BaseVertex + #47 + grouped instanced | βœ… | `7b41efc` | -| 22+23 perf 1-4 β€” drop dead lookup, sort, cull, hash memo | βœ… | `573526d` | -| 24 β€” Sky-pass preservation check | βœ… structural (independent) | `5df9135` | -| 25 β€” Component micro-tests round-out | βœ… all spec tests covered | β€” | -| 26 β€” Visual verification + flag default-on | βœ… | (this commit) | -| 27 β€” Delete legacy code paths | ⚠️ deferred to N.6 (legacy retained as flag-off escape hatch) | β€” | -| 28 β€” Update memory + ISSUES + finalize plan | βœ… | (this commit) | - ---- - -## Week 1 β€” Plumbing + Atlas for Static Scenery + Conformance - -Goal of week 1: WB infrastructure wired up behind feature flag. Conformance tests pass. Static scenery routes through `ObjectMeshManager` when flag is on. Everything else still uses old path. **Done when: build green, all conformance tests pass, flag-on Holtburg roam visually identical to flag-off.** - -### Task 1: Wb folder skeleton + `WbFoundationFlag` - -**Files:** -- Create: `src/AcDream.App/Rendering/Wb/WbFoundationFlag.cs` - -- [ ] **Step 1.1: Create the Wb folder by creating the flag file** - -```csharp -namespace AcDream.App.Rendering.Wb; - -/// -/// Process-lifetime cache of ACDREAM_USE_WB_FOUNDATION env var. -/// Read once at static-init time; all consumers import this rather than -/// re-reading the env var per call (env-var lookups on Windows are not -/// free at hot-path cadence). -/// -/// -/// Set ACDREAM_USE_WB_FOUNDATION=1 to route static-scenery + atlas -/// content through WB's ObjectMeshManager; per-instance customized -/// content (server CreateObject entities) takes the existing -/// path either -/// way. Flag becomes default-on at end of Phase N.4 after visual -/// verification. -/// -/// -public static class WbFoundationFlag -{ - public static bool IsEnabled { get; } = - System.Environment.GetEnvironmentVariable("ACDREAM_USE_WB_FOUNDATION") == "1"; -} -``` - -- [ ] **Step 1.2: Build to verify the new folder compiles** - -Run: `dotnet build --verbosity quiet` -Expected: 0 errors. The folder exists implicitly because the file's namespace declares it. - -- [ ] **Step 1.3: Commit** - -```bash -git add src/AcDream.App/Rendering/Wb/WbFoundationFlag.cs -git commit -m "$(cat <<'EOF' -phase(N.4): WbFoundationFlag scaffold for ACDREAM_USE_WB_FOUNDATION env var - -Creates the src/AcDream.App/Rendering/Wb/ folder and the static flag -gate that other call sites will import. Read once at static-init time. -Set ACDREAM_USE_WB_FOUNDATION=1 to enable WB foundation routing. - -Co-Authored-By: Claude Opus 4.6 -EOF -)" -``` - ---- - -### Task 2: `AcSurfaceMetadata` + `AcSurfaceMetadataTable` - -**Files:** -- Create: `src/AcDream.App/Rendering/Wb/AcSurfaceMetadata.cs` -- Create: `src/AcDream.App/Rendering/Wb/AcSurfaceMetadataTable.cs` -- Test: `tests/AcDream.Core.Tests/Rendering/Wb/AcSurfaceMetadataTableTests.cs` - -- [ ] **Step 2.1: Write failing test** - -Create `tests/AcDream.Core.Tests/Rendering/Wb/AcSurfaceMetadataTableTests.cs`: - -```csharp -using AcDream.App.Rendering.Wb; -using AcDream.Core.Meshing; - -namespace AcDream.Core.Tests.Rendering.Wb; - -public sealed class AcSurfaceMetadataTableTests -{ - [Fact] - public void Add_ThenLookup_RoundTripsSameMetadata() - { - var table = new AcSurfaceMetadataTable(); - var meta = new AcSurfaceMetadata( - Translucency: TranslucencyKind.AlphaBlend, - Luminosity: 0.5f, - Diffuse: 0.8f, - SurfOpacity: 0.7f, - NeedsUvRepeat: true, - DisableFog: false); - - table.Add(gfxObjId: 0x01000123ul, surfaceIdx: 2, meta); - - Assert.True(table.TryLookup(0x01000123ul, 2, out var got)); - Assert.Equal(meta, got); - } - - [Fact] - public void Lookup_MissingKey_ReturnsFalse() - { - var table = new AcSurfaceMetadataTable(); - Assert.False(table.TryLookup(0xDEADBEEFul, 0, out _)); - } - - [Fact] - public void Add_OverwritesPreviousMetadata() - { - var table = new AcSurfaceMetadataTable(); - var first = new AcSurfaceMetadata(TranslucencyKind.Opaque, 0f, 1f, 1f, false, false); - var second = new AcSurfaceMetadata(TranslucencyKind.Additive, 1f, 1f, 1f, false, true); - - table.Add(0xAAAA, 0, first); - table.Add(0xAAAA, 0, second); - - Assert.True(table.TryLookup(0xAAAA, 0, out var got)); - Assert.Equal(second, got); - } - - [Fact] - public void Add_FromMultipleThreads_IsThreadSafe() - { - var table = new AcSurfaceMetadataTable(); - var threads = new System.Threading.Tasks.Task[8]; - for (int t = 0; t < 8; t++) - { - int threadIdx = t; - threads[t] = System.Threading.Tasks.Task.Run(() => - { - for (int i = 0; i < 1000; i++) - { - ulong key = (ulong)(threadIdx * 1000 + i); - table.Add(key, 0, new AcSurfaceMetadata( - TranslucencyKind.Opaque, 0f, 1f, 1f, false, false)); - } - }); - } - System.Threading.Tasks.Task.WaitAll(threads); - - // 8000 entries should be present. - for (int t = 0; t < 8; t++) - for (int i = 0; i < 1000; i++) - Assert.True(table.TryLookup((ulong)(t * 1000 + i), 0, out _)); - } -} -``` - -- [ ] **Step 2.2: Run test to verify it fails** - -Run: `dotnet test tests/AcDream.Core.Tests --filter "FullyQualifiedName~AcSurfaceMetadataTableTests" --verbosity normal` -Expected: COMPILE FAIL β€” types don't exist. - -- [ ] **Step 2.3: Create `AcSurfaceMetadata.cs`** - -```csharp -using AcDream.Core.Meshing; - -namespace AcDream.App.Rendering.Wb; - -/// -/// AC-specific surface render metadata that WB's MeshBatchData -/// doesn't carry. Computed at mesh-extraction time and looked up by the -/// draw dispatcher to drive translucency / sky-pass / fog behavior. -/// -/// -/// All fields mirror those on today's so -/// behavior is preserved bit-for-bit through the migration. -/// -/// -public sealed record AcSurfaceMetadata( - TranslucencyKind Translucency, - float Luminosity, - float Diffuse, - float SurfOpacity, - bool NeedsUvRepeat, - bool DisableFog); -``` - -- [ ] **Step 2.4: Create `AcSurfaceMetadataTable.cs`** - -```csharp -using System.Collections.Concurrent; - -namespace AcDream.App.Rendering.Wb; - -/// -/// Thread-safe side-table mapping (gfxObjId, surfaceIdx) to -/// . Populated when a GfxObj's mesh data -/// is extracted; queried at draw time. -/// -/// -/// Keyed by (gfxObjId, surfaceIdx) not by WB's runtime batch -/// identity because batch objects can be evicted and re-loaded by WB's -/// LRU; the (gfxObj, surface) pair is stable across cycles. -/// -/// -public sealed class AcSurfaceMetadataTable -{ - private readonly ConcurrentDictionary<(ulong gfxObjId, int surfaceIdx), AcSurfaceMetadata> _table = new(); - - public void Add(ulong gfxObjId, int surfaceIdx, AcSurfaceMetadata meta) - => _table[(gfxObjId, surfaceIdx)] = meta; - - public bool TryLookup(ulong gfxObjId, int surfaceIdx, out AcSurfaceMetadata meta) - => _table.TryGetValue((gfxObjId, surfaceIdx), out meta!); - - public void Clear() => _table.Clear(); -} -``` - -- [ ] **Step 2.5: Run tests to verify pass** - -Run: `dotnet build --verbosity quiet && dotnet test tests/AcDream.Core.Tests --filter "FullyQualifiedName~AcSurfaceMetadataTableTests" --verbosity normal` -Expected: 4/4 PASS. - -- [ ] **Step 2.6: Commit** - -```bash -git add src/AcDream.App/Rendering/Wb/AcSurfaceMetadata.cs src/AcDream.App/Rendering/Wb/AcSurfaceMetadataTable.cs tests/AcDream.Core.Tests/Rendering/Wb/AcSurfaceMetadataTableTests.cs -git commit -m "$(cat <<'EOF' -phase(N.4): AcSurfaceMetadata side-table for WB-pristine surface props - -Holds Translucency / Luminosity / Diffuse / SurfOpacity / NeedsUvRepeat / -DisableFog keyed by (gfxObjId, surfaceIdx). Populated at extraction time, -queried by the draw dispatcher. ConcurrentDictionary because mesh -extraction happens on background workers. - -No fork patches required β€” keeps WB's MeshBatchData pristine. - -Co-Authored-By: Claude Opus 4.6 -EOF -)" -``` - ---- - -### Task 3: Mesh-extraction conformance test - -**Files:** -- Test: `tests/AcDream.Core.Tests/Rendering/Wb/MeshExtractionConformanceTests.cs` - -This test proves our existing `GfxObjMesh.Build` produces the same vertex + index output as WB's algorithm. Per [GfxObjMesh.cs:24](../../../src/AcDream.Core/Meshing/GfxObjMesh.cs:24), our code is already a faithful port of WB's `BuildPolygonIndices` β€” this test pins that fact. - -- [ ] **Step 3.1: Write the test** - -```csharp -using System.Numerics; -using AcDream.Core.Meshing; -using DatReaderWriter.DBObjs; -using DatReaderWriter.Enums; -using DatReaderWriter.Types; - -namespace AcDream.Core.Tests.Rendering.Wb; - -/// -/// Conformance: our must produce the same -/// vertex-array + index-array output as WB's ObjectMeshManager -/// would for the same input GfxObj. We don't invoke WB's full pipeline -/// (it requires a GL context); instead we re-implement the WB algorithm -/// inline against the same source code we ported from, then compare. -/// -/// -/// If this test fails, either our port has drifted or the WB code has -/// changed upstream β€” investigate which, do not "fix" the test. -/// -/// -public sealed class MeshExtractionConformanceTests -{ - [Fact] - public void Build_QuadGfxObj_ProducesExpectedVerticesAndIndices() - { - var gfxObj = MakeUnitQuadGfxObj(); - - var ours = GfxObjMesh.Build(gfxObj, dats: null); - - Assert.Single(ours); - var sub = ours[0]; - // Quad β†’ 4 vertices, 6 indices (two triangles via fan triangulation). - Assert.Equal(4, sub.Vertices.Length); - Assert.Equal(6, sub.Indices.Length); - // Fan from vertex 0: (0,1,2) and (0,2,3). - Assert.Equal(new uint[] { 0, 1, 2, 0, 2, 3 }, sub.Indices); - } - - [Fact] - public void Build_DoubleSidedPoly_ProducesBothPosAndNegSubmeshes() - { - var gfxObj = MakeUnitQuadGfxObj(); - // Force the polygon to be double-sided via Stippling.Both. - var poly = gfxObj.Polygons[0]; - poly.Stippling = StipplingType.Both; - poly.NegSurface = 0; // same surface idx for both sides - - var ours = GfxObjMesh.Build(gfxObj, dats: null); - - // One sub-mesh per (surfaceIdx, isNeg) bucket. - Assert.Equal(2, ours.Count); - // Negative-side bucket has reversed winding. - var neg = ours.First(s => s.Indices.SequenceEqual(new uint[] { 2, 1, 0, 3, 2, 0 })); - Assert.NotNull(neg); - } - - [Fact] - public void Build_NoNegFlag_WithClockwiseSidesType_StillEmitsNegSide() - { - var gfxObj = MakeUnitQuadGfxObj(); - var poly = gfxObj.Polygons[0]; - poly.Stippling = StipplingType.None; // no explicit Negative flag - poly.SidesType = CullMode.Clockwise; // AC's "double-sided via SidesType" convention - poly.NegSurface = 0; - - var ours = GfxObjMesh.Build(gfxObj, dats: null); - - Assert.Equal(2, ours.Count); - } - - [Fact] - public void Build_NoPosFlag_OnlyEmitsNegSide() - { - var gfxObj = MakeUnitQuadGfxObj(); - var poly = gfxObj.Polygons[0]; - poly.Stippling = StipplingType.NoPos | StipplingType.Negative; - poly.NegSurface = 0; - - var ours = GfxObjMesh.Build(gfxObj, dats: null); - - Assert.Single(ours); - } - - [Fact] - public void Build_NegUVIndices_AppliedToNegSideVertices() - { - var gfxObj = MakeUnitQuadGfxObj(); - var poly = gfxObj.Polygons[0]; - poly.Stippling = StipplingType.Both; - poly.NegSurface = 0; - // Default UV index 0 maps to UV (0,0); NegUVIndices=[2,2,2,2] should - // map to UV (1,1) on the neg side. - poly.NegUVIndices = [2, 2, 2, 2]; - - var ours = GfxObjMesh.Build(gfxObj, dats: null); - - var posSide = ours.First(s => s.Vertices[0].TexCoord == new Vector2(0, 0)); - var negSide = ours.First(s => s.Vertices[0].TexCoord == new Vector2(1, 1)); - Assert.NotNull(posSide); - Assert.NotNull(negSide); - } - - /// - /// Build a synthetic 1Γ—1 quad GfxObj with UV indices [0,1,2,3] mapping - /// to UVs [(0,0), (1,0), (1,1), (0,1)]. Default surface index 0, - /// PosSurface=0, NegSurface=0. No Stippling flags (caller may set). - /// - private static GfxObj MakeUnitQuadGfxObj() - { - var gfx = new GfxObj { Surfaces = [0u] }; - - // Vertices: 4 corners with UV at each corner. - gfx.VertexArray = new VertexArray(); - for (ushort i = 0; i < 4; i++) - { - var sw = new SwVertex - { - Origin = new Vector3(i % 2, i / 2, 0), - Normal = new Vector3(0, 0, 1), - UVs = new System.Collections.Generic.List - { - new UV { U = i % 2, V = i / 2 }, - new UV { U = 0.5f, V = 0.5f }, - new UV { U = 1, V = 1 }, - }, - }; - gfx.VertexArray.Vertices[i] = sw; - } - - // One quad polygon with vertex sequence [0,1,2,3] and PosUVIndices [0,0,0,0]. - var poly = new Polygon - { - VertexIds = [0, 1, 2, 3], - PosUVIndices = [0, 0, 0, 0], - NegUVIndices = [], - PosSurface = 0, - NegSurface = -1, - Stippling = StipplingType.None, - SidesType = CullMode.Counterclockwise, // single-sided by default - }; - gfx.Polygons[0] = poly; - return gfx; - } -} -``` - -- [ ] **Step 3.2: Run test to verify pass (since algorithm already ported)** - -Run: `dotnet build --verbosity quiet && dotnet test tests/AcDream.Core.Tests --filter "FullyQualifiedName~MeshExtractionConformanceTests" --verbosity normal` -Expected: 5/5 PASS. If any test fails, investigate before continuing β€” it means our port has drifted. - -- [ ] **Step 3.3: Commit** - -```bash -git add tests/AcDream.Core.Tests/Rendering/Wb/MeshExtractionConformanceTests.cs -git commit -m "$(cat <<'EOF' -test(N.4): mesh-extraction conformance pinning GfxObjMesh.Build behavior - -Five tests covering: simple quad, double-sided via Stippling.Both, -double-sided via SidesType=Clockwise (AC's NoNeg-clear convention), -NoPos-only emission, and NegUVIndices application to neg-side vertices. - -These pin GfxObjMesh.Build's output as the conformance baseline before -N.4 substitutes it with WB's pipeline. - -Co-Authored-By: Claude Opus 4.6 -EOF -)" -``` - ---- - -### Task 4: Setup-flatten conformance test - -**Files:** -- Test: `tests/AcDream.Core.Tests/Rendering/Wb/SetupFlattenConformanceTests.cs` - -- [ ] **Step 4.1: Write the test** - -```csharp -using System.Numerics; -using AcDream.Core.Meshing; -using DatReaderWriter.DBObjs; -using DatReaderWriter.Enums; -using DatReaderWriter.Types; - -namespace AcDream.Core.Tests.Rendering.Wb; - -/// -/// Conformance: our must produce the same -/// (GfxObjId, Matrix4x4) sequence as WB's setup-parts walk for representative -/// Setups. Pinning the placement-frame fallback chain (motionFrameOverride β†’ -/// Resting β†’ Default β†’ first available) before substitution. -/// -public sealed class SetupFlattenConformanceTests -{ - [Fact] - public void Flatten_NoFrames_FallsBackToIdentity() - { - var setup = new Setup { Parts = [0x01000001ul] }; - - var refs = SetupMesh.Flatten(setup); - - Assert.Single(refs); - Assert.Equal(0x01000001ul, refs[0].GfxObjId); - // No frame β†’ identity transform, identity scale. - Assert.Equal(Matrix4x4.Identity, refs[0].PartTransform); - } - - [Fact] - public void Flatten_WithDefaultFrame_AppliesFrameOriginAndOrientation() - { - var setup = new Setup { Parts = [0x01000001ul] }; - var anim = new AnimationFrame - { - Frames = - [ - new Frame - { - Origin = new Vector3(10, 20, 30), - Orientation = Quaternion.CreateFromYawPitchRoll(0, 0, 0), - }, - ], - }; - setup.PlacementFrames[Placement.Default] = anim; - - var refs = SetupMesh.Flatten(setup); - - // Translation column should encode Origin. - Assert.Equal(new Vector3(10, 20, 30), refs[0].PartTransform.Translation); - } - - [Fact] - public void Flatten_WithRestingFrame_PrefersRestingOverDefault() - { - var setup = new Setup { Parts = [0x01000001ul] }; - setup.PlacementFrames[Placement.Default] = new AnimationFrame - { - Frames = [new Frame { Origin = new Vector3(10, 20, 30), Orientation = Quaternion.Identity }], - }; - setup.PlacementFrames[Placement.Resting] = new AnimationFrame - { - Frames = [new Frame { Origin = new Vector3(99, 99, 99), Orientation = Quaternion.Identity }], - }; - - var refs = SetupMesh.Flatten(setup); - - // Resting wins. - Assert.Equal(new Vector3(99, 99, 99), refs[0].PartTransform.Translation); - } - - [Fact] - public void Flatten_WithMotionFrameOverride_PrefersOverrideOverResting() - { - var setup = new Setup { Parts = [0x01000001ul] }; - setup.PlacementFrames[Placement.Resting] = new AnimationFrame - { - Frames = [new Frame { Origin = new Vector3(99, 99, 99), Orientation = Quaternion.Identity }], - }; - var motionOverride = new AnimationFrame - { - Frames = [new Frame { Origin = new Vector3(7, 7, 7), Orientation = Quaternion.Identity }], - }; - - var refs = SetupMesh.Flatten(setup, motionFrameOverride: motionOverride); - - Assert.Equal(new Vector3(7, 7, 7), refs[0].PartTransform.Translation); - } - - [Fact] - public void Flatten_DefaultScalePerPart_AppliedToTransform() - { - var setup = new Setup - { - Parts = [0x01000001ul, 0x01000002ul], - DefaultScale = [new Vector3(2, 2, 2), new Vector3(3, 3, 3)], - }; - - var refs = SetupMesh.Flatten(setup); - - // Identity-frame * scale-2 β†’ diagonal matrix with 2s. - Assert.Equal(2f, refs[0].PartTransform.M11); - Assert.Equal(3f, refs[1].PartTransform.M11); - } -} -``` - -- [ ] **Step 4.2: Run test, verify pass** - -Run: `dotnet build --verbosity quiet && dotnet test tests/AcDream.Core.Tests --filter "FullyQualifiedName~SetupFlattenConformanceTests" --verbosity normal` -Expected: 5/5 PASS. - -- [ ] **Step 4.3: Commit** - -```bash -git add tests/AcDream.Core.Tests/Rendering/Wb/SetupFlattenConformanceTests.cs -git commit -m "$(cat <<'EOF' -test(N.4): setup-flatten conformance pinning placement-frame fallback chain - -Five tests covering: identity (no frames), Default frame, Resting beats -Default, motion override beats Resting, DefaultScale per part. Pins -SetupMesh.Flatten's behavior as the conformance baseline before N.4 -routes it through WB's setup-parts walk. - -Co-Authored-By: Claude Opus 4.6 -EOF -)" -``` - ---- - -### Task 5: `WbMeshAdapter` skeleton - -**Files:** -- Create: `src/AcDream.App/Rendering/Wb/WbMeshAdapter.cs` -- Test: `tests/AcDream.Core.Tests/Rendering/Wb/WbMeshAdapterTests.cs` - -- [ ] **Step 5.1: Write failing test** - -```csharp -using AcDream.App.Rendering.Wb; -using DatReaderWriter; -using Microsoft.Extensions.Logging.Abstractions; - -namespace AcDream.Core.Tests.Rendering.Wb; - -public sealed class WbMeshAdapterTests -{ - [Fact] - public void Construct_WithNullGl_ThrowsArgumentNull() - { - Assert.Throws(() => - new WbMeshAdapter(gl: null!, dats: NullDats(), logger: NullLogger.Instance)); - } - - [Fact] - public void Dispose_DisposesUnderlyingMeshManager_NoThrow() - { - // Without a real GL context we can't fully construct ObjectMeshManager, - // but we can verify the adapter's Dispose path is safe to invoke when - // the manager is null (early-init failure path). - var adapter = WbMeshAdapter.CreateUninitialized(); - adapter.Dispose(); // should not throw - } - - private static DatCollection NullDats() => DatCollection.Empty; -} -``` - -(Note: the tests above are minimal. Full coverage of the adapter happens -once it has real methods to exercise β€” Tasks 7-8.) - -- [ ] **Step 5.2: Run, expect compile fail** - -Run: `dotnet test tests/AcDream.Core.Tests --filter "FullyQualifiedName~WbMeshAdapterTests" --verbosity normal` -Expected: COMPILE FAIL β€” type doesn't exist, `DatCollection.Empty` may not exist. - -- [ ] **Step 5.3: Create the adapter** - -Create `src/AcDream.App/Rendering/Wb/WbMeshAdapter.cs`: - -```csharp -using Chorizite.OpenGLSDLBackend; -using Chorizite.OpenGLSDLBackend.Lib; -using DatReaderWriter; -using Microsoft.Extensions.Logging; -using Silk.NET.OpenGL; - -namespace AcDream.App.Rendering.Wb; - -/// -/// Single seam between acdream and WB's render pipeline. Owns the -/// instance and exposes a stable acdream- -/// shaped API ( / / -/// ) so the rest of the renderer doesn't need to -/// know about WB's types directly. -/// -/// -/// Instantiated once at GameWindow init when -/// is true. When the flag is off, -/// no instance is constructed and call sites fall through to the legacy -/// renderer paths. -/// -/// -public sealed class WbMeshAdapter : System.IDisposable -{ - private readonly ObjectMeshManager? _meshManager; - private readonly OpenGLGraphicsDevice? _graphicsDevice; - private bool _disposed; - - public WbMeshAdapter(GL gl, DatCollection dats, ILogger logger) - { - System.ArgumentNullException.ThrowIfNull(gl); - System.ArgumentNullException.ThrowIfNull(dats); - System.ArgumentNullException.ThrowIfNull(logger); - - // OpenGLGraphicsDevice is the host WB's ObjectMeshManager needs. - // Constructed once and owned by the adapter for the process lifetime. - _graphicsDevice = new OpenGLGraphicsDevice(gl); - - // ObjectMeshManager wants its own ILogger; - // we use NullLogger to avoid the wrong category, the adapter's - // own logger handles the acdream-side trail. - var omLogger = Microsoft.Extensions.Logging.Abstractions.NullLogger.Instance; - var datsAdapter = new WbDatReaderAdapter(dats); // see Task 6 - _meshManager = new ObjectMeshManager(_graphicsDevice, datsAdapter, omLogger); - } - - private WbMeshAdapter() - { - // Uninitialized constructor β€” only for tests that need a Dispose-safe - // instance without a real GL context. - _meshManager = null; - _graphicsDevice = null; - } - - internal static WbMeshAdapter CreateUninitialized() => new(); - - /// - /// Get GPU-side render data for an object id, blocking on background - /// preparation if the upload hasn't finished yet. Returns null if the - /// object can't be loaded (e.g., dat missing). - /// - public Chorizite.OpenGLSDLBackend.Lib.ObjectRenderData? GetRenderData(ulong id) - => _meshManager?.GetRenderData(id); - - public void IncrementRefCount(ulong id) => _meshManager?.IncrementRefCount(id); - - public void DecrementRefCount(ulong id) => _meshManager?.DecrementRefCount(id); - - public void Dispose() - { - if (_disposed) return; - _disposed = true; - _meshManager?.Dispose(); - _graphicsDevice?.Dispose(); - } -} -``` - -- [ ] **Step 5.4: Build to expose compilation issues** - -Run: `dotnet build --verbosity quiet` -Expected: Compile errors will reveal what we need: -- `WbDatReaderAdapter` does not exist yet (Task 6 creates it) -- `OpenGLGraphicsDevice` constructor signature may differ - -For now, comment out the `_graphicsDevice = new OpenGLGraphicsDevice(gl);` and `_meshManager = new ObjectMeshManager(...)` lines and provide a TODO comment marker so the test file compiles. Replace with: - -```csharp -// Real init defers to Task 6 (dat reader adapter) + Task 9 (full bring-up). -// During Task 5 the adapter is a stub that returns null/no-ops everywhere. -_graphicsDevice = null; -_meshManager = null; -``` - -- [ ] **Step 5.5: Add `DatCollection.Empty` if it doesn't exist** - -If `DatCollection.Empty` is missing, the test won't compile. Check: - -Run: `grep -rn "DatCollection.Empty" src/` - -If absent, the test should be adjusted to construct an empty `DatCollection` directly (or skipped β€” the meaningful adapter tests come later). Update the test as needed; the test's only job at this stage is to verify Dispose is safe. - -- [ ] **Step 5.6: Run tests** - -Run: `dotnet build --verbosity quiet && dotnet test tests/AcDream.Core.Tests --filter "FullyQualifiedName~WbMeshAdapterTests" --verbosity normal` -Expected: 2/2 PASS (with the stub init). - -- [ ] **Step 5.7: Commit** - -```bash -git add src/AcDream.App/Rendering/Wb/WbMeshAdapter.cs tests/AcDream.Core.Tests/Rendering/Wb/WbMeshAdapterTests.cs -git commit -m "$(cat <<'EOF' -phase(N.4): WbMeshAdapter skeleton β€” single seam to WB ObjectMeshManager - -Stub init pending Task 6 (dat reader adapter) + Task 9 (full bring-up). -Public API: IncrementRefCount / DecrementRefCount / GetRenderData / -Dispose. Construction-safe (validates args), Dispose-safe (no-op when -underlying manager is null). - -Co-Authored-By: Claude Opus 4.6 -EOF -)" -``` - ---- - -### Task 6: ~~WbDatReaderAdapter~~ β€” OBSOLETED 2026-05-08 - -**Adjustment 1 (2026-05-08):** discovered during pre-Task-6 grep that -WB ships `WorldBuilder.Shared.Services.DefaultDatReaderWriter`, a -concrete `IDatReaderWriter` implementation that takes a dat-directory -path and constructs all four databases (Portal / HighRes / Language + -CellRegions) internally. We can instantiate it directly with the same -`%USERPROFILE%\Documents\Asheron's Call` path acdream's `DatCollection` -uses; both will open the same dat files with separate handles. Memory -cost: ~50-100 MB of duplicate index caches, acceptable for foundation -work. Task 9 incorporates the construction step directly. - -If memory pressure surfaces during week 2 stress testing, revisit by -writing a real bridge that shares index caches with our `DatCollection`. - -**No work for this task β€” skip and proceed to Task 7.** - ---- - -### Adjustment 2 (2026-05-08): Task 9 routing reverted β€” tier decision belongs at spawn-callback layer - -**Discovered during Week 1 visual smoke test**: with flag on, characters / -NPCs disappeared along with static scenery. Root cause: Task 9 routed -**all** `InstancedMeshRenderer.EnsureUploaded` calls through -`WbMeshAdapter.IncrementRefCount` and marked their cache entries with -`WbManagedSentinel`. But `InstancedMeshRenderer` is used for both tiers -in production: - -- **Atlas-tier** call sites: `_pendingCellMeshes` drain - ([GameWindow.cs:5137](../../../src/AcDream.App/Rendering/GameWindow.cs:5137)), - per-MeshRef GfxObj loop on `lb.Entities` - ([:5155](../../../src/AcDream.App/Rendering/GameWindow.cs:5155)). -- **Per-instance-tier** call sites: per-part loop in spawn handling - ([:2302](../../../src/AcDream.App/Rendering/GameWindow.cs:2302)) β€” this is - character / creature rendering driven by server `CreateObject`. - -The renderer is **tier-blind by design**: it doesn't know spawn source. -Putting routing logic there violates separation of concerns. The spec's -Data-Flow section already specifies the right placement β€” routing happens -at the **spawn-callback layer**: - -- `LandblockSpawnAdapter.OnLandblockLoaded(...)` (Task 11) calls - `IncrementRefCount` per unique GfxObj β€” atlas-tier only. -- `EntitySpawnAdapter.OnCreate(entity)` (Task 17) routes through - per-instance path (`TextureCache.GetOrUploadWithPaletteOverride`) β€” - never calls `IncrementRefCount` for atlas. - -**Resolution:** reverted Task 9's renderer-level routing. Removed the -sentinel logic and the 4 sentinel-skip checks in -`InstancedMeshRenderer`. **Kept** the `_wbMeshAdapter` constructor -parameter (unused for now) so `GameWindow.cs` doesn't shift when -later tasks need adapter access. Kept all the real WB pipeline -construction in `WbMeshAdapter` (verified working under flag-off). - -**Week 1 endpoint shifts:** "WB infrastructure constructed; flag-on and -flag-off visually identical." Routing arrives in Week 2 (Task 11) at -the correct layer. Smoke verification is now: flag-on === flag-off. - ---- - -### Adjustment 3 (2026-05-08): flag-on FPS regression β€” root-caused, deferred to Task 22 - -**Discovered during Task 13 stress test** (radius 7, flag-on). Visible -FPS drop + rising frame latency vs flag-off baseline. Initial guess -was the staged-upload queue leaking memory; we shipped -`WbMeshAdapter.Tick()` (commit `bf53cb4`) to drain -`_meshManager.StagedMeshData` + `_graphicsDevice._glThreadQueue` per -frame. Result: leak fixed, but **FPS unchanged**. - -**Real cause: dual-pipeline cost.** Flag-on runs both rendering -pipelines in parallel without yet collecting any savings: - -1. **Background workers (4-wide).** `ObjectMeshManager` spins up - `MaxParallelLoads = 4` worker threads decoding GfxObj polygons, - building texture atlases, encoding batches. Contends with the - render thread for CPU cores. -2. **Duplicate GL upload.** `Tick()` calls `UploadMeshData` per - staged mesh, creating VAO/VBO/IBO + atlas texture uploads. Real - per-call GL state churn on the render thread. -3. **Duplicate I/O.** `DefaultDatReaderWriter` opens its own dat file - handles and rebuilds its own index cache (~50-100 MB) alongside - our existing `DatCollection`. Memory bandwidth + GC churn. -4. **Legacy renderer keeps doing the same work.** Per Adjustment 2, - `InstancedMeshRenderer` is tier-blind β€” it still uploads VAO / - VBO / IBO for the same atlas-tier content WB is also building. - **We literally double the prep cost** for every atlas-tier GfxObj. - -The savings from WB's atlas batching only materialize when **Task 22 -(`WbDrawDispatcher`) lands and the legacy renderer can short-circuit -its upload for atlas-tier content**. At that point WB owns atlas-tier -draw and `InstancedMeshRenderer` skips its own upload + draw work -for those entities. Until then, flag-on pays both costs. - -**Decision: do not fix now.** Plan Risk #5 explicitly anticipated this: - -> Performance regression during integration of week 1's "atlas for -> static scenery, old path for everything else" mixed state. -> Mitigation: keep the feature gate `ACDREAM_USE_WB_FOUNDATION=1` -> during weeks 1-3; default-off until week 4 visual verification. - -Default-off (the user's daily experience) is byte-identical to -pre-N.4. Flag-on is dev-only until Week 4. Task 22 must wire the -legacy-renderer short-circuit for atlas-tier content as part of -landing the dispatcher; the cost cannot be amortized any earlier -without violating Adjustment 2's tier-blind-renderer principle. - -`Tick()` stays β€” it fixed a real memory leak and is required -infrastructure for Task 22 anyway. We just paid for it without -seeing FPS recovery yet. - ---- - -### Adjustment 4 (2026-05-08): WorldEntity lacks HiddenParts + AnimPartChange fields β€” deferred plumbing - -**Discovered during Task 17 implementation.** `EntitySpawnAdapter.OnCreate` -needed to populate `AnimatedEntityState` with the entity's `HiddenParts` -mask + `AnimPartChange` override map. But: `WorldEntity` (the per-frame -render-side struct) does not currently expose either field. Both pieces -of customization data live on the network-layer spawn record and are -consumed before the `WorldEntity` is built. - -**Resolution.** Task 17 ships the adapter scaffolding with a TODO comment -acknowledging the gap. The created `AnimatedEntityState` always has an -empty override map + zero hidden mask. Per-instance customizations like -"hide this character's head" won't take effect with flag-on until the -plumbing lands. - -**Why this is safe to defer.** No production path consumes -`AnimatedEntityState`'s override / hidden data yet β€” Task 22's -`WbDrawDispatcher` is the first consumer. By the time Task 22 lands, we -either: -1. Add `HiddenPartsMask` + `AnimPartChanges` fields to `WorldEntity` and - populate them at spawn time. Small change to the network β†’ render - pipeline. -2. Inject them into `EntitySpawnAdapter.OnCreate` via a separate - parameter that the spawn handler provides directly (sidesteps the - `WorldEntity` change). - -Option 1 is cleaner long-term; Option 2 is faster for landing Task 22 -without touching WorldEntity. Decision deferred to Task 22 brainstorm. - -### Adjustment 5 (2026-05-08): Task 20 (per-instance decode conformance) is structural, not byte-comparison - -**Original plan.** Task 20 was supposed to compare RGBA8 output of -"old path" (`TextureCache.GetOrUploadWithPaletteOverride` direct) vs -"new path" (`EntitySpawnAdapter` β†’ `ITextureCachePerInstance` β†’ -`TextureCache.GetOrUploadWithPaletteOverride`) to prove byte-identity. - -**Reality.** Both paths call the **same function**. The new path adds a -seam interface (`ITextureCachePerInstance`) for testability but does -not modify the decode logic β€” the bytes are identical by construction. -A test asserting byte-equality would be tautological. - -**Resolution.** Existing `EntitySpawnAdapterTests` cover the routing -behavior (does the adapter call the cache with the right args?). The -decode-byte conformance is structural: same function = same output. -Mark Task 20 βœ… structurally; no separate test file. - -### Adjustment 6 (2026-05-08): Resolved Adjustment 4 β€” Option A (fields on WorldEntity) - -**Context.** Adjustment 4 deferred the `HiddenPartsMask` + `AnimPartChanges` -plumbing decision to Task 22. Two options: -- **A**: add fields to `WorldEntity`, populate at spawn time -- **B**: thread as separate args into `EntitySpawnAdapter.OnCreate` - -**Decision: Option A.** Reasoning: -1. The data is already computed at spawn time in GameWindow's CreateObject - handler β€” adding two fields is a 4-line change. -2. Option B would spread network-layer types across the streaming subsystem, - violating the same separation-of-concerns principle as Adjustment 2. -3. The 0xF625 ObjDescEvent (appearance update) replays through the same - spawn path, so WorldEntity fields work automatically for hot-swap updates. - -**Implementation:** -- `WorldEntity` gains `PartOverrides: IReadOnlyList` (default - empty) and `HiddenPartsMask: ulong` (default 0). -- `PartOverride(byte PartIndex, uint GfxObjId)` is a lightweight record struct - in Core.World that decouples from the network-layer `CreateObject.AnimPartChange`. -- `EntitySpawnAdapter.OnCreate` now calls `state.HideParts(entity.HiddenPartsMask)` - and `state.SetPartOverride(...)` for each override. -- GameWindow's CreateObject handler builds the `PartOverride[]` from the - server-sent `AnimPartChanges` list. - -### Adjustment 7 (2026-05-08, Task 26 visual verification): IncrementRefCount doesn't trigger mesh load - -**Discovered when** Task 26's first launch showed only terrain β€” zero entities visible. Diagnostic counters (added the same launch via `ACDREAM_WB_DIAG=1`) showed `entitiesSeen=14M, entitiesDrawn=14M, drawsIssued=0` β€” every entity was visited but no draws were issued because `TryGetRenderData` returned null for everything. - -**Root cause.** WB's `ObjectMeshManager.IncrementRefCount(id)` only bumps a usage counter β€” it does NOT trigger mesh loading. Loading is fired separately by `PrepareMeshDataAsync(id, isSetup)`, which dispatches to a background worker pool; the result auto-enqueues to `_stagedMeshData` (line 510 of `ObjectMeshManager.cs`) which our existing `WbMeshAdapter.Tick()` already drains. - -The N.4 plan assumed `IncrementRefCount` was lifecycle-aware (it isn't). `LandblockSpawnAdapter` and the original `EntitySpawnAdapter` both called `IncrementRefCount` and stopped β€” meshes never loaded. - -**Fix** (commit `943652d`): -- `WbMeshAdapter.IncrementRefCount` now calls `_meshManager.PrepareMeshDataAsync(id, isSetup: false)` on first registration. `isSetup: false` is correct because acdream's MeshRefs already carry expanded per-part GfxObj ids (0x01XXXXXX) β€” WB's Setup-expansion path is unused. -- `EntitySpawnAdapter` gained an optional `IWbMeshAdapter` constructor parameter. Per-instance entities (server-spawned characters / NPCs) had been entirely skipped by `LandblockSpawnAdapter` (which filters `ServerGuid != 0`); their GfxObjs now get registered + loaded at `OnCreate` and decremented at `OnRemove`. Includes both `MeshRefs.GfxObjId` AND `PartOverrides.GfxObjId` so weapon/clothing/helmet swaps load too. - -**Lesson preserved.** Future cross-session work touching WB: **`IncrementRefCount` is not lifecycle-aware. Call `PrepareMeshDataAsync` to trigger loads.** Documented in CLAUDE.md "WB integration cribs" section. - -### Adjustment 8 (2026-05-08, Task 26 visual verification): SurfaceId lives in batch.Key.SurfaceId - -**Discovered when** the second Task 26 launch showed `drawsIssued=4.8M/5s` (draws ARE happening) but ZERO entities visible. Inspection of `ResolveTexture` showed it was returning early because `batch.SurfaceId == 0` for every batch. - -**Root cause.** WB's `ObjectMeshManager.UploadGfxObjMeshData` (line 1746 of `ObjectMeshManager.cs`) constructs `ObjectRenderBatch` and sets `Key = batch.Key` (a `TextureAtlasManager.TextureKey` struct that contains a `SurfaceId` field) but does NOT populate the top-level `ObjectRenderBatch.SurfaceId` property. That property exists on the type but stays at its default 0. - -**Fix** (commit `943652d`): `WbDrawDispatcher.ResolveTexture` reads `batch.Key.SurfaceId` instead of `batch.SurfaceId`. Also handles the dummy `0xFFFFFFFF` case used by WB's environment edge wireframes. - -**Lesson preserved.** **`ObjectRenderBatch.SurfaceId` is not populated by WB. Read `batch.Key.SurfaceId`.** Documented in CLAUDE.md. - -### Adjustment 9 (2026-05-08, Task 26 visual verification): Modern rendering uses one global VAO/VBO/IBO - -**Discovered when** the third Task 26 launch finally showed real draws β€” but as "exploded" character body parts scattered around the world with no scenery. Visual was completely broken even though the GL pipeline was clearly issuing draws and binding textures correctly. - -**Root cause.** WB's `ObjectMeshManager` has two rendering paths controlled by `_useModernRendering = HasOpenGL43 && HasBindless`. On any modern GPU (which is everything we target), modern is true and ALL meshes share a single `GlobalMeshBuffer` β€” one VAO, one VBO, one IBO. Each batch's `IBO` field points to that ONE global IBO; the batch's actual slice is identified by `FirstIndex` (offset into IBO, in indices) and `BaseVertex` (offset into VBO, in vertices). The dispatcher was issuing `glDrawElementsInstanced` with `indices=0` and no base vertex β€” so every entity drew the same first triangle of the global mesh starting at offset 0. That produced exactly the "exploded parts at scattered positions" symptom. - -**Fix** (commit `7b41efc`): switch to `glDrawElementsInstancedBaseVertexBaseInstance`, pass `(void*)(batch.FirstIndex * sizeof(ushort))` as the indices argument, pass `(int)batch.BaseVertex` as base vertex. The grouped-instanced refactor in the same commit additionally uses `BaseInstance` to slice into the shared instance VBO per group. - -**Bonus discovery:** because all meshes share one VAO under modern rendering, the dispatcher only needs to bind the VAO ONCE per frame (not per draw). Every draw goes to the same VAO. Significant CPU savings. - -**Lesson preserved.** **WB's modern rendering path packs everything into one global VAO/VBO/IBO. Honor `FirstIndex` and `BaseVertex`.** Documented in CLAUDE.md. - -### Adjustment 10 (2026-05-08, Task 26 visual verification): AnimatedEntityState overrides clobber Issue #47 close-detail mesh - -**Discovered when** Task 26's fourth launch showed scenery + connected characters β€” but characters were "bulky and missing detail" compared to the legacy renderer. Recognized as a re-occurrence of Issue #47 (resolved 2026-05-06 via `GfxObjDegradeResolver`). - -**Root cause.** Adjustment 6 stored AnimPartChanges on `WorldEntity.PartOverrides` using the raw `NewModelId` from the network packet β€” without applying `GfxObjDegradeResolver`. GameWindow's spawn path correctly resolves base GfxObjs (e.g., upper arm `0x01000055`, 14 verts/17 polys) to their close-detail equivalents (`0x01001795`, 32 verts/60 polys) and bakes the result into `MeshRefs`. But `WbDrawDispatcher` then called `animState.ResolvePartGfxObj(partIdx, meshRefGfxObjId)` which returned the raw (low-detail) override from `PartOverrides`, undoing the degrade. - -**Fix** (commit `7b41efc`): the dispatcher trusts `MeshRefs` as the source of truth and does NOT re-apply `animState.ResolvePartGfxObj` at draw time. `AnimatedEntityState` overrides become relevant only for hot-swap appearance updates (0xF625 `ObjDescEvent`) which today rebuild MeshRefs anyway. `IsPartHidden` similarly skipped β€” `HiddenPartsMask` is never populated by spawn code (legacy renderer also doesn't check it). - -**Lesson preserved.** **`MeshRefs` is the source of truth at draw time** β€” GameWindow's spawn path bakes overrides + degrades into it. Don't re-apply overrides downstream. - -### Task 6 (original β€” kept for history) - -**Files:** -- Create: `src/AcDream.App/Rendering/Wb/WbDatReaderAdapter.cs` -- Test: `tests/AcDream.Core.Tests/Rendering/Wb/WbDatReaderAdapterTests.cs` - -WB's `ObjectMeshManager` constructor takes `IDatReaderWriter` (from `Chorizite.DatReaderWriter`). We use `DatReaderWriter.DatCollection` (vendored as a separate library). The two interfaces are similar but not identical. This task builds the adapter. - -- [ ] **Step 6.1: Read WB's `IDatReaderWriter` interface** - -Run: `grep -n "interface IDatReaderWriter" references/WorldBuilder/` - -Read the interface to identify which methods are actually called by `ObjectMeshManager`. Likely just `Portal.TryGet(id, out T)` and similar accessors. - -- [ ] **Step 6.2: Write failing test** - -```csharp -using AcDream.App.Rendering.Wb; -using DatReaderWriter; -using DatReaderWriter.DBObjs; - -namespace AcDream.Core.Tests.Rendering.Wb; - -public sealed class WbDatReaderAdapterTests -{ - [Fact] - public void Portal_TryGet_DelegatesToUnderlyingDats() - { - var dats = new DatCollection(); - // Inject a known surface into the test dat collection. - var surface = new Surface { Id = 0x08001234 }; - dats.Portal.Insert(surface); - - var adapter = new WbDatReaderAdapter(dats); - - Assert.True(adapter.Portal.TryGet(0x08001234, out var got)); - Assert.Equal(surface.Id, got.Id); - } - - [Fact] - public void Portal_TryGet_MissingId_ReturnsFalse() - { - var adapter = new WbDatReaderAdapter(new DatCollection()); - Assert.False(adapter.Portal.TryGet(0xDEADBEEF, out _)); - } -} -``` - -- [ ] **Step 6.3: Run, expect compile fail** - -Expected: COMPILE FAIL β€” `WbDatReaderAdapter` doesn't exist. - -- [ ] **Step 6.4: Create the adapter** - -The exact code depends on WB's `IDatReaderWriter` shape. The pattern is a thin pass-through: - -```csharp -using DatReaderWriter; - -namespace AcDream.App.Rendering.Wb; - -/// -/// Adapter from acdream's (vendored from -/// upstream DatReaderWriter) to the IDatReaderWriter -/// interface WB's ObjectMeshManager consumes. Pass-through where -/// possible; reshapes calls to match WB's expected interface where the -/// libraries diverge. -/// -public sealed class WbDatReaderAdapter : Chorizite.DatReaderWriter.IDatReaderWriter -{ - private readonly DatCollection _dats; - - public WbDatReaderAdapter(DatCollection dats) - { - System.ArgumentNullException.ThrowIfNull(dats); - _dats = dats; - } - - public Chorizite.DatReaderWriter.IDatDatabase Portal => new PortalAdapter(_dats); - public Chorizite.DatReaderWriter.IDatDatabase Cell => new CellAdapter(_dats); - public Chorizite.DatReaderWriter.IDatDatabase HighRes => new HighResAdapter(_dats); - public Chorizite.DatReaderWriter.IDatDatabase Local => new LocalAdapter(_dats); - - private sealed class PortalAdapter : Chorizite.DatReaderWriter.IDatDatabase { /* ... */ } - private sealed class CellAdapter : Chorizite.DatReaderWriter.IDatDatabase { /* ... */ } - private sealed class HighResAdapter : Chorizite.DatReaderWriter.IDatDatabase { /* ... */ } - private sealed class LocalAdapter : Chorizite.DatReaderWriter.IDatDatabase { /* ... */ } -} -``` - -The exact method set depends on `IDatDatabase`. Investigate via `grep` and fill in. - -- [ ] **Step 6.5: Adjustment marker** - -If `IDatReaderWriter`'s shape is more complex than expected (e.g., async readers, MEMORY-mapped access), add an Adjustment subsection here describing what was discovered and how the adapter changed. - -- [ ] **Step 6.6: Run tests** - -Run: `dotnet build --verbosity quiet && dotnet test tests/AcDream.Core.Tests --filter "FullyQualifiedName~WbDatReaderAdapterTests" --verbosity normal` -Expected: 2/2 PASS. - -- [ ] **Step 6.7: Commit** - -```bash -git add src/AcDream.App/Rendering/Wb/WbDatReaderAdapter.cs tests/AcDream.Core.Tests/Rendering/Wb/WbDatReaderAdapterTests.cs -git commit -m "$(cat <<'EOF' -phase(N.4): WbDatReaderAdapter β€” bridge DatCollection to WB IDatReaderWriter - -Pass-through adapter so WB's ObjectMeshManager can consume our -DatCollection without us refactoring to Chorizite.DatReaderWriter -directly. Maintains four sub-database accessors (Portal/Cell/ -HighRes/Local). - -Co-Authored-By: Claude Opus 4.6 -EOF -)" -``` - ---- - -### Task 7: Wire `WbMeshAdapter` into `GameWindow` lifecycle (gated by flag) - -**Files:** -- Modify: `src/AcDream.App/Rendering/GameWindow.cs` - -- [ ] **Step 7.1: Locate `GameWindow.OnLoad` and the renderer construction** - -Run: `grep -n "new TextureCache\|new StaticMeshRenderer\|new InstancedMeshRenderer" src/AcDream.App/Rendering/GameWindow.cs` - -Identify where existing renderers are constructed during init. - -- [ ] **Step 7.2: Add `WbMeshAdapter` field + construct under flag gate** - -Add a private field: -```csharp -private WbMeshAdapter? _wbMeshAdapter; -``` - -In `OnLoad` (or wherever renderers are constructed), after `_textures` is built: -```csharp -if (WbFoundationFlag.IsEnabled) -{ - var logger = Microsoft.Extensions.Logging.Abstractions.NullLogger.Instance; - _wbMeshAdapter = new WbMeshAdapter(gl, _dats, logger); - System.Console.WriteLine("[N.4] WbFoundation flag is ENABLED β€” routing static content through ObjectMeshManager."); -} -``` - -In `OnClose` / `Dispose`: -```csharp -_wbMeshAdapter?.Dispose(); -``` - -- [ ] **Step 7.3: Build to verify no breakage with flag default-off** - -Run: `dotnet build --verbosity quiet && dotnet test --verbosity quiet` -Expected: build green, all 8 + 4 + 5 + 5 + 2 = 24 new tests pass on top of the 883 baseline. (Pre-existing 8 failures unchanged.) - -- [ ] **Step 7.4: Commit** - -```bash -git add src/AcDream.App/Rendering/GameWindow.cs -git commit -m "$(cat <<'EOF' -phase(N.4): construct WbMeshAdapter in GameWindow under feature flag - -Enabled only when ACDREAM_USE_WB_FOUNDATION=1. Dispose paired with -window shutdown. No call sites use it yet β€” wiring of TextureCache / -StaticMeshRenderer / GpuWorldState happens in Tasks 8-10. - -Co-Authored-By: Claude Opus 4.6 -EOF -)" -``` - ---- - -### Task 8: CLAUDE.md pointer to this plan - -**Files:** -- Modify: `CLAUDE.md` - -- [ ] **Step 8.1: Add a "Currently in flight" pointer near the top** - -Edit `CLAUDE.md`. After the "Roadmap discipline" section's intro (around the section that mentions `docs/superpowers/specs/*.md`), insert: - -```markdown -**Currently in flight: Phase N.4 β€” Rendering Pipeline Foundation.** Plan -at `docs/superpowers/plans/2026-05-08-phase-n4-rendering-foundation.md`. -This is a 3-4 week phase adopting WB's `ObjectMeshManager` + -`TextureAtlasManager` as our shared rendering infrastructure. The plan -is a living document β€” task checkboxes get marked as commits land, -adjustments are appended in-place, weeks 2-4 may be revised based on -week 1 discoveries. Read the plan's "Plan Living-Document Convention" -section before contributing. -``` - -- [ ] **Step 8.2: Commit** - -```bash -git add CLAUDE.md -git commit -m "$(cat <<'EOF' -docs: point CLAUDE.md at the in-flight N.4 plan - -Future agents picking up the project should see the N.4 plan as -authoritative for rendering work. Pointer lives near the Roadmap -discipline section. Living-doc convention noted. - -Co-Authored-By: Claude Opus 4.6 -EOF -)" -``` - ---- - -### Task 9: Wire static-scenery path through `WbMeshAdapter` - -**Files:** -- Modify: `src/AcDream.App/Rendering/StaticMeshRenderer.cs` - -This is the first behavioral change: when the flag is on, static-scenery uploads route through `WbMeshAdapter` instead of building VAO/VBO/EBO inline. - -- [ ] **Step 9.1: Locate `EnsureUploaded` in `StaticMeshRenderer.cs`** - -Currently uploads sub-meshes directly. We're adding a flag-gated alternate path. - -- [ ] **Step 9.2: Add adapter reference + flag-gated upload** - -Modify `StaticMeshRenderer` to accept an optional `WbMeshAdapter`: - -```csharp -public sealed unsafe class StaticMeshRenderer : IDisposable -{ - // ...existing fields... - private readonly WbMeshAdapter? _wbMeshAdapter; - - public StaticMeshRenderer( - GL gl, - Shader shader, - TextureCache textures, - WbMeshAdapter? wbMeshAdapter = null) // optional injection - { - _gl = gl; - _shader = shader; - _textures = textures; - _wbMeshAdapter = wbMeshAdapter; - } - - public void EnsureUploaded(uint gfxObjId, IReadOnlyList subMeshes) - { - if (_gpuByGfxObj.ContainsKey(gfxObjId)) - return; - - if (WbFoundationFlag.IsEnabled && _wbMeshAdapter is not null) - { - // New path: route to ObjectMeshManager. WB will background-prep - // and upload; we mark this gfxObj as "WB-managed" in our local - // cache via a sentinel entry so the draw loop knows to look - // there instead of in _gpuByGfxObj. - _wbMeshAdapter.IncrementRefCount(gfxObjId); - _gpuByGfxObj[gfxObjId] = WbManagedSentinel; - return; - } - - // Legacy path: build VAO/VBO/EBO inline. - var list = new List(subMeshes.Count); - foreach (var sm in subMeshes) - list.Add(UploadSubMesh(sm)); - _gpuByGfxObj[gfxObjId] = list; - } - - private static readonly List WbManagedSentinel = new(0); -} -``` - -- [ ] **Step 9.3: Update `Draw` to look up WB-managed entries differently** - -In the draw loop, when iterating entities, check the sentinel: - -```csharp -if (object.ReferenceEquals(_gpuByGfxObj[gfxObjId], WbManagedSentinel)) -{ - // Draw via WbDrawDispatcher β€” implementation in Task 17. - // For week 1 the dispatcher is a stub that no-ops; the entity simply - // doesn't render. This is fine for the flag-gated build; week 4's - // visual verification is the gate where this must work. - continue; -} -``` - -This intentionally leaves a behavioral gap in week 1: with the flag on, static scenery does NOT render correctly. **This is expected.** The full draw path lands in Task 17 (week 4). Week 1's success criterion is "build green, conformance tests pass, no regressions with flag OFF." - -- [ ] **Step 9.4: Pass adapter to constructor in `GameWindow`** - -In `GameWindow.OnLoad`: -```csharp -_staticMeshRenderer = new StaticMeshRenderer(gl, staticShader, _textures, _wbMeshAdapter); -``` - -- [ ] **Step 9.5: Build, run all tests, smoke-test with flag off** - -Run: `dotnet build --verbosity quiet && dotnet test --verbosity quiet` -Expected: build green, 883+24 = 907 tests pass, 8 pre-existing failures. - -Smoke-test launch with flag off (default) β€” Holtburg should render identically to before. - -- [ ] **Step 9.6: Commit** - -```bash -git add src/AcDream.App/Rendering/StaticMeshRenderer.cs src/AcDream.App/Rendering/GameWindow.cs -git commit -m "$(cat <<'EOF' -phase(N.4): route static-scenery uploads through WbMeshAdapter under flag - -When ACDREAM_USE_WB_FOUNDATION=1, StaticMeshRenderer.EnsureUploaded -calls WbMeshAdapter.IncrementRefCount instead of building VAO/VBO/EBO -inline. Local cache uses a sentinel entry to mark WB-managed gfxObjs. - -The draw loop currently skips WB-managed entries (they don't render -yet). This is expected: the full draw path arrives in Task 17 / week 4. -Week 1's success is "no regressions with flag OFF." - -Default-off β€” flag must be set explicitly to test the new path. - -Co-Authored-By: Claude Opus 4.6 -EOF -)" -``` - ---- - -### Task 10: Week 1 wrap-up β€” verify clean baseline + commit week 1 status - -**Files:** none (verification + status update) - -- [ ] **Step 10.1: Full test run + build** - -Run: `dotnet build --verbosity quiet 2>&1 | tail -5 && dotnet test --verbosity quiet 2>&1 | tail -5` -Expected: 0 errors. 907+ tests pass, 8 pre-existing failures only. - -- [ ] **Step 10.2: Smoke-test with flag off** - -Launch the client with default env (flag OFF). Walk Holtburg briefly. Confirm: no visual change vs pre-N.4 main. - -- [ ] **Step 10.3: Smoke-test with flag on (expect partial breakage)** - -Launch with `$env:ACDREAM_USE_WB_FOUNDATION = "1"`. Confirm: client launches, scenery is missing or partially missing (expected β€” WbDrawDispatcher is a stub). Stop the client. - -- [ ] **Step 10.4: Update plan checkboxes** - -In this plan file, mark Tasks 1-9 as βœ… with their commit SHAs. Append a "Week 1 status: COMPLETE β€” date YYYY-MM-DD" note at the start of Week 2. - -- [ ] **Step 10.5: Commit** - -```bash -git add docs/superpowers/plans/2026-05-08-phase-n4-rendering-foundation.md -git commit -m "$(cat <<'EOF' -docs(N.4): mark week 1 complete in living-doc plan - -WB infrastructure wired up behind ACDREAM_USE_WB_FOUNDATION flag. -Conformance tests pinned (mesh extraction + setup flatten). Static -scenery routes through WbMeshAdapter when flag is on; rendering -completion deferred to Task 17 (week 4). - -Co-Authored-By: Claude Opus 4.6 -EOF -)" -``` - ---- - -## Week 2 β€” Streaming Integration - -Goal of week 2: `LandblockSpawnAdapter` + `LandblockUnloadAdapter` wired through `GpuWorldState`. Memory budget verified under long-roam stress. Pending-spawn list still works. **Done when: `ObjectMeshManager` ref counts balance across landblock load/unload, GPU memory stable on long roam.** - -### Task 11: `LandblockSpawnAdapter` - -**Files:** -- Create: `src/AcDream.App/Rendering/Wb/LandblockSpawnAdapter.cs` -- Test: `tests/AcDream.Core.Tests/Rendering/Wb/LandblockSpawnAdapterTests.cs` - -- [ ] **Step 11.1: Write failing tests** - -```csharp -using System.Linq; -using AcDream.App.Rendering.Wb; -using AcDream.Core.World; - -namespace AcDream.Core.Tests.Rendering.Wb; - -public sealed class LandblockSpawnAdapterTests -{ - [Fact] - public void OnLandblockLoaded_RegistersIncrementForEachUniqueGfxObj() - { - var adapter = MakeAdapter(out var captured); - - var lb = MakeLandblock(setupIds: [0x02000001ul, 0x02000002ul, 0x02000001ul], - staticIds: [0x01000010ul]); - adapter.OnLandblockLoaded(0x12340000u, lb); - - // Three unique ids despite duplicate setup id. - Assert.Equal(3, captured.IncrementCalls.Count); - Assert.Contains(0x02000001ul, captured.IncrementCalls); - Assert.Contains(0x02000002ul, captured.IncrementCalls); - Assert.Contains(0x01000010ul, captured.IncrementCalls); - } - - [Fact] - public void OnLandblockUnloaded_RegistersMatchingDecrements() - { - var adapter = MakeAdapter(out var captured); - - var lb = MakeLandblock(setupIds: [0x02000001ul, 0x02000002ul], - staticIds: []); - adapter.OnLandblockLoaded(0x12340000u, lb); - adapter.OnLandblockUnloaded(0x12340000u); - - Assert.Equal(captured.IncrementCalls.OrderBy(x => x), captured.DecrementCalls.OrderBy(x => x)); - } - - [Fact] - public void OnLandblockUnloaded_UnknownLandblock_NoOp() - { - var adapter = MakeAdapter(out var captured); - - adapter.OnLandblockUnloaded(0xDEADBEEFu); // never loaded - - Assert.Empty(captured.DecrementCalls); - } - - private static LandblockSpawnAdapter MakeAdapter(out CapturingAdapterMock captured) - { - captured = new CapturingAdapterMock(); - return new LandblockSpawnAdapter(captured); - } - - private sealed class CapturingAdapterMock : IWbMeshAdapter - { - public List IncrementCalls { get; } = new(); - public List DecrementCalls { get; } = new(); - public void IncrementRefCount(ulong id) => IncrementCalls.Add(id); - public void DecrementRefCount(ulong id) => DecrementCalls.Add(id); - } - - private static LoadedLandblock MakeLandblock(ulong[] setupIds, ulong[] staticIds) - { - // Synthetic LoadedLandblock for test; the test only cares about the - // unique GfxObj ids reachable from Setups + Statics. Field shape may - // need adjustment to match LoadedLandblock's actual constructor. - return new LoadedLandblock( - setups: setupIds.Select(id => new SetupSpawn(id)).ToList(), - statics: staticIds.Select(id => new StaticSpawn(id)).ToList()); - } -} -``` - -(Note: `IWbMeshAdapter` is a new interface β€” see Step 11.3. `LoadedLandblock` constructor shape may need adjustment to whatever the codebase uses.) - -- [ ] **Step 11.2: Add `IWbMeshAdapter` interface + extract from `WbMeshAdapter`** - -Modify `WbMeshAdapter.cs` to implement an interface so the adapter can be mocked: - -```csharp -public interface IWbMeshAdapter -{ - void IncrementRefCount(ulong id); - void DecrementRefCount(ulong id); -} - -public sealed class WbMeshAdapter : System.IDisposable, IWbMeshAdapter -{ - // ...existing impl... -} -``` - -- [ ] **Step 11.3: Create `LandblockSpawnAdapter`** - -```csharp -using System.Collections.Generic; -using AcDream.Core.World; - -namespace AcDream.App.Rendering.Wb; - -/// -/// Bridges landblock streaming events to 's -/// reference-count lifecycle. Walks LoadedLandblock.Setups and -/// LoadedLandblock.Statics for unique GfxObj/Setup ids; calls -/// IncrementRefCount on load and matching DecrementRefCount -/// on unload. -/// -/// -/// Maintains a Dictionary<landblockId, HashSet<ulong>> -/// snapshot of which ids each landblock holds, so unload can match the -/// load 1:1 without re-walking the (now-released) landblock data. -/// -/// -public sealed class LandblockSpawnAdapter -{ - private readonly IWbMeshAdapter _adapter; - private readonly Dictionary> _idsByLandblock = new(); - - public LandblockSpawnAdapter(IWbMeshAdapter adapter) - { - System.ArgumentNullException.ThrowIfNull(adapter); - _adapter = adapter; - } - - public void OnLandblockLoaded(uint landblockId, LoadedLandblock lb) - { - var unique = new HashSet(); - foreach (var setup in lb.Setups) unique.Add(setup.GfxObjId); - foreach (var stat in lb.Statics) unique.Add(stat.GfxObjId); - - _idsByLandblock[landblockId] = unique; - foreach (var id in unique) _adapter.IncrementRefCount(id); - } - - public void OnLandblockUnloaded(uint landblockId) - { - if (!_idsByLandblock.TryGetValue(landblockId, out var unique)) return; - foreach (var id in unique) _adapter.DecrementRefCount(id); - _idsByLandblock.Remove(landblockId); - } -} -``` - -- [ ] **Step 11.4: Run tests, verify pass** - -Run: `dotnet build --verbosity quiet && dotnet test tests/AcDream.Core.Tests --filter "FullyQualifiedName~LandblockSpawnAdapter" --verbosity normal` -Expected: 3/3 PASS. - -- [ ] **Step 11.5: Commit** - -```bash -git add src/AcDream.App/Rendering/Wb/LandblockSpawnAdapter.cs src/AcDream.App/Rendering/Wb/WbMeshAdapter.cs tests/AcDream.Core.Tests/Rendering/Wb/LandblockSpawnAdapterTests.cs -git commit -m "$(cat <<'EOF' -phase(N.4): LandblockSpawnAdapter bridges streaming to WB ref counts - -OnLandblockLoaded walks Setups + Statics for unique GfxObj ids and -calls IncrementRefCount per id. OnLandblockUnloaded matches with -DecrementRefCount. Per-landblock id-set snapshot ensures unload pairs -1:1 with load even when underlying data is released. - -IWbMeshAdapter interface extracted from WbMeshAdapter to enable -mocking in tests. - -Co-Authored-By: Claude Opus 4.6 -EOF -)" -``` - ---- - -### Task 12: Wire `LandblockSpawnAdapter` into `GpuWorldState` - -**Files:** -- Modify: `src/AcDream.App/Streaming/GpuWorldState.cs` -- Modify: `src/AcDream.App/Rendering/GameWindow.cs` - -- [ ] **Step 12.1: Add adapter field + flag-gated calls in `GpuWorldState`** - -Modify `GpuWorldState`'s `AddLandblock` and `RemoveLandblock`: - -```csharp -private readonly Wb.LandblockSpawnAdapter? _wbSpawnAdapter; - -public GpuWorldState(Wb.LandblockSpawnAdapter? wbSpawnAdapter = null) -{ - _wbSpawnAdapter = wbSpawnAdapter; -} - -public void AddLandblock(LoadedLandblock landblock) -{ - // ...existing logic... - if (Wb.WbFoundationFlag.IsEnabled && _wbSpawnAdapter is not null) - _wbSpawnAdapter.OnLandblockLoaded(landblock.LandblockId, landblock); -} - -public void RemoveLandblock(uint landblockId) -{ - if (Wb.WbFoundationFlag.IsEnabled && _wbSpawnAdapter is not null) - _wbSpawnAdapter.OnLandblockUnloaded(landblockId); - // ...existing logic... -} -``` - -- [ ] **Step 12.2: Construct adapter in `GameWindow`** - -In `GameWindow.OnLoad`: -```csharp -LandblockSpawnAdapter? wbSpawnAdapter = null; -if (WbFoundationFlag.IsEnabled && _wbMeshAdapter is not null) - wbSpawnAdapter = new LandblockSpawnAdapter(_wbMeshAdapter); -_gpuWorldState = new GpuWorldState(wbSpawnAdapter); -``` - -- [ ] **Step 12.3: Build + tests + smoke-test flag off** - -Run: `dotnet build --verbosity quiet && dotnet test --verbosity quiet` -Expected: build green, all tests pass. - -Smoke-test with flag off: verify no regressions. - -- [ ] **Step 12.4: Commit** - -```bash -git add src/AcDream.App/Streaming/GpuWorldState.cs src/AcDream.App/Rendering/GameWindow.cs -git commit -m "$(cat <<'EOF' -phase(N.4): wire LandblockSpawnAdapter through GpuWorldState - -AddLandblock/RemoveLandblock now drive WB ref counts when flag is on. -Pending-spawn list mechanism untouched β€” adapter is invoked only when -a landblock fully loads (drains pending), not when a spawn parks. - -Co-Authored-By: Claude Opus 4.6 -EOF -)" -``` - ---- - -### Task 13: Memory budget + LRU verification under stress - -**Files:** -- Test: `tests/AcDream.Core.Tests/Rendering/Wb/MemoryBudgetTests.cs` (optional β€” likely manual verification) - -- [ ] **Step 13.1: Manual stress test plan** - -This is a verification task, not an implementation task. Document the plan: - -1. Launch with `ACDREAM_USE_WB_FOUNDATION=1` and `ACDREAM_STREAM_RADIUS=7`. -2. Walk in a straight line for ~5 minutes (covers 50+ landblocks in/out of radius). -3. Monitor GPU memory in window title bar. -4. Acceptance: GPU memory grows to ~steady-state value (depending on hardware, somewhere under 1 GB) and stays there. If it grows unboundedly, LRU eviction isn't firing. - -Run with: `$env:ACDREAM_USE_WB_FOUNDATION = "1"; $env:ACDREAM_STREAM_RADIUS = "7"; dotnet run --project src\AcDream.App\AcDream.App.csproj 2>&1 | Tee-Object -FilePath "n4-stress.log"` - -- [ ] **Step 13.2: Run with the user** - -Hand off to user. User walks for 5+ minutes. User reports observed peak memory + final stable memory. - -- [ ] **Step 13.3: Document outcome** - -If memory is stable: append "Memory budget verified at peak / steady" to this task. - -If memory grows unboundedly: investigate. Likely causes: -- Adapter fails to call `DecrementRefCount` in some path (check for unload logging). -- WB's LRU eviction interacts badly with our streaming radius hysteresis. -- Memory budget set too high for the test hardware. - -Do not commit a "fix" until root cause is understood. Add an Adjustment subsection here documenting what was found. - -- [ ] **Step 13.4: Commit (verification-only commit if memory was clean)** - -```bash -# No code changes; just an empty commit to mark verification complete in history. -git commit --allow-empty -m "$(cat <<'EOF' -verify(N.4): memory budget + LRU eviction stable under 5min/r=7 roam - -GPU memory peak: . Steady-state: . Eviction -fires correctly on landblock unload. LandblockSpawnAdapter ref-count -balance verified through repeated traversal. - -Co-Authored-By: Claude Opus 4.6 -EOF -)" -``` - ---- - -### Task 14: Pending-spawn list integration verification - -**Files:** -- Test: `tests/AcDream.Core.Tests/Rendering/Wb/PendingSpawnIntegrationTests.cs` - -The pending-spawn list ([feedback_phase_a1_hotfix_saga.md](memory/feedback_phase_a1_hotfix_saga.md)) parks `CreateObject` events that arrive before their landblock streams in. We need to verify this still works with the WB foundation. - -- [ ] **Step 14.1: Write integration test** - -```csharp -using AcDream.App.Rendering.Wb; -using AcDream.App.Streaming; -using AcDream.Core.World; - -namespace AcDream.Core.Tests.Rendering.Wb; - -public sealed class PendingSpawnIntegrationTests -{ - [Fact] - public void LiveEntity_BeforeLandblock_Pends_ThenDrains_OnLoad() - { - var captured = new CapturingAdapterMock(); - var spawnAdapter = new LandblockSpawnAdapter(captured); - var state = new GpuWorldState(spawnAdapter); - - // Live entity for landblock 0x12340000 arrives first. - var entity = new WorldEntity { /* with LandblockId = 0x12340000 */ }; - state.AppendLiveEntity(entity); - - Assert.Equal(1, state.PendingLiveEntityCount); - Assert.Empty(captured.IncrementCalls); // not registered yet - - // Now landblock arrives. - var lb = new LoadedLandblock(/* ... */); - state.AddLandblock(lb); - - // Pending entity drains; adapter sees landblock-side increments. - Assert.True(captured.IncrementCalls.Count > 0); - Assert.Equal(0, state.PendingLiveEntityCount); - } -} -``` - -(Test-fixture details depend on `WorldEntity` and `LoadedLandblock` constructors.) - -- [ ] **Step 14.2: Run, verify pass** - -If the test fails, the pending-spawn list path doesn't drain through the adapter. Either fix the adapter wiring in `GpuWorldState.AddLandblock` to handle pending entities, or document an Adjustment. - -- [ ] **Step 14.3: Commit** - -```bash -git add tests/AcDream.Core.Tests/Rendering/Wb/PendingSpawnIntegrationTests.cs -git commit -m "$(cat <<'EOF' -test(N.4): pending-spawn list still drains correctly with WB adapter - -Verifies CreateObject-before-landblock parks β†’ drains on landblock -arrival. Adapter sees ref-count increments only after drain. - -Co-Authored-By: Claude Opus 4.6 -EOF -)" -``` - ---- - -### Task 15: Week 2 wrap-up - -- [ ] **Step 15.1: Full test suite + roam** - -Run all tests. Roam at radius 7 with flag on for 5 minutes. Confirm: stable memory, no crashes, ref counts balance. - -- [ ] **Step 15.2: Update plan checkboxes** - -Mark Tasks 11-14 βœ… with commit SHAs. Append "Week 2 status: COMPLETE β€” date YYYY-MM-DD" at start of Week 3. - -- [ ] **Step 15.3: Commit plan update** - -```bash -git add docs/superpowers/plans/2026-05-08-phase-n4-rendering-foundation.md -git commit -m "docs(N.4): mark week 2 complete - -Co-Authored-By: Claude Opus 4.6 " -``` - ---- - -## Week 3 β€” Per-instance + Animation - -Goal: per-instance customization works correctly. Animated creatures render with their server-sent overrides applied. AnimPartChange + HiddenParts honored. **Done when: drudge / chicken / banderling render with correct customizations under flag on.** - -### Task 16: `AnimatedEntityState` type - -**Files:** -- Create: `src/AcDream.App/Rendering/Wb/AnimatedEntityState.cs` -- Test: `tests/AcDream.Core.Tests/Rendering/Wb/AnimatedEntityStateTests.cs` - -- [ ] **Step 16.1: Write failing test** - -```csharp -using AcDream.App.Rendering.Wb; -using AcDream.Core.Animation; - -namespace AcDream.Core.Tests.Rendering.Wb; - -public sealed class AnimatedEntityStateTests -{ - [Fact] - public void DefaultState_HasNoOverridesAndNoHiddenParts() - { - var state = new AnimatedEntityState(MakeSequencer()); - - Assert.False(state.IsPartHidden(0)); - Assert.False(state.IsPartHidden(63)); - Assert.False(state.TryGetPartOverride(0, out _)); - } - - [Fact] - public void SetHiddenPart_BitmaskIsApplied() - { - var state = new AnimatedEntityState(MakeSequencer()); - - state.HideParts(hiddenMask: 0b1010); - - Assert.False(state.IsPartHidden(0)); - Assert.True(state.IsPartHidden(1)); - Assert.False(state.IsPartHidden(2)); - Assert.True(state.IsPartHidden(3)); - } - - [Fact] - public void SetPartOverride_ResolvedAtLookup() - { - var state = new AnimatedEntityState(MakeSequencer()); - - state.SetPartOverride(partIdx: 5, gfxObjId: 0x01001234ul); - - Assert.True(state.TryGetPartOverride(5, out var got)); - Assert.Equal(0x01001234ul, got); - Assert.False(state.TryGetPartOverride(6, out _)); - } - - private static AnimationSequencer MakeSequencer() => new(); // adjust to constructor -} -``` - -- [ ] **Step 16.2: Create the type** - -```csharp -using System.Collections.Generic; -using AcDream.Core.Animation; - -namespace AcDream.App.Rendering.Wb; - -/// -/// Per-entity render state for animated entities. Lives outside WB's -/// mesh cache because it varies per instance (AnimPartChange override -/// map, HiddenParts mask) and per frame (animation transforms produced -/// by the sequencer). -/// -/// -/// Instances are created by EntitySpawnAdapter.OnCreate and -/// disposed by EntitySpawnAdapter.OnRemove. -/// -/// -public sealed class AnimatedEntityState -{ - private readonly Dictionary _partGfxObjOverrides = new(); - private ulong _hiddenMask = 0; - public AnimationSequencer Sequencer { get; } - - public AnimatedEntityState(AnimationSequencer sequencer) - { - System.ArgumentNullException.ThrowIfNull(sequencer); - Sequencer = sequencer; - } - - public void HideParts(ulong hiddenMask) => _hiddenMask = hiddenMask; - - public bool IsPartHidden(int partIdx) - { - if (partIdx < 0 || partIdx >= 64) return false; - return (_hiddenMask & (1ul << partIdx)) != 0; - } - - public void SetPartOverride(int partIdx, ulong gfxObjId) - => _partGfxObjOverrides[partIdx] = gfxObjId; - - public bool TryGetPartOverride(int partIdx, out ulong gfxObjId) - => _partGfxObjOverrides.TryGetValue(partIdx, out gfxObjId); -} -``` - -- [ ] **Step 16.3: Run, verify pass** - -Run: `dotnet build --verbosity quiet && dotnet test tests/AcDream.Core.Tests --filter "FullyQualifiedName~AnimatedEntityStateTests" --verbosity normal` -Expected: 3/3 PASS. - -- [ ] **Step 16.4: Commit** - -```bash -git add src/AcDream.App/Rendering/Wb/AnimatedEntityState.cs tests/AcDream.Core.Tests/Rendering/Wb/AnimatedEntityStateTests.cs -git commit -m "$(cat <<'EOF' -phase(N.4): AnimatedEntityState β€” per-entity render state - -Holds AnimPartChange override map + HiddenParts bitmask + reference -to existing AnimationSequencer. Lives outside WB's mesh cache. - -Co-Authored-By: Claude Opus 4.6 -EOF -)" -``` - ---- - -### Task 17: `EntitySpawnAdapter` β€” route CreateObject to per-instance path - -**Files:** -- Create: `src/AcDream.App/Rendering/Wb/EntitySpawnAdapter.cs` -- Test: `tests/AcDream.Core.Tests/Rendering/Wb/EntitySpawnAdapterTests.cs` -- Modify: `src/AcDream.App/Streaming/GpuWorldState.cs` - -- [ ] **Step 17.1: Write failing test** - -```csharp -using AcDream.App.Rendering.Wb; -using AcDream.Core.World; - -namespace AcDream.Core.Tests.Rendering.Wb; - -public sealed class EntitySpawnAdapterTests -{ - [Fact] - public void OnCreate_WithPaletteOverride_RoutesToPerInstanceCache() - { - var captured = new CapturingTextureCacheMock(); - var adapter = new EntitySpawnAdapter(captured); - - var entity = new WorldEntity - { - // Set up with non-trivial PaletteOverride so we can verify routing. - ObjDescBuilder = new ObjDescBuilder { PaletteOverride = MakeOverride() }, - }; - - adapter.OnCreate(entity); - - Assert.True(captured.PaletteOverrideCalled); - } - - [Fact] - public void OnCreate_WithoutCustomization_StillRegistersForCleanup() - { - var captured = new CapturingTextureCacheMock(); - var adapter = new EntitySpawnAdapter(captured); - - adapter.OnCreate(new WorldEntity()); - adapter.OnRemove(0xDEADBEEFu); // doesn't crash on unknown id - - // Adapter tracks created entities for OnRemove cleanup. - Assert.True(true); // smoke - } -} -``` - -- [ ] **Step 17.2: Create adapter** - -```csharp -using System.Collections.Generic; -using AcDream.Core.World; - -namespace AcDream.App.Rendering.Wb; - -/// -/// Routes network-spawned CreateObject entities through the per- -/// instance rendering path. Every entity sent by the server carries -/// per-instance customization (palette overrides, texture changes, -/// part swaps), so they bypass WB's atlas and use the existing -/// path that -/// already hash-keys overrides for caching. -/// -public sealed class EntitySpawnAdapter -{ - private readonly TextureCache _textures; - private readonly Dictionary _stateByGuid = new(); - - public EntitySpawnAdapter(TextureCache textures) - { - System.ArgumentNullException.ThrowIfNull(textures); - _textures = textures; - } - - public AnimatedEntityState? OnCreate(WorldEntity entity) - { - // Build palette override from entity's ObjDesc.SubPalettes (if any). - var palette = entity.PaletteOverride; - // For each surface in the entity's mesh chain, decode through - // the per-instance path. The TextureCache already hash-keys the - // override, so identical customizations across multiple entities - // share the cached texture. - if (palette is not null && palette.SubPalettes.Count > 0) - { - foreach (var surfaceId in entity.SurfaceIds) - _textures.GetOrUploadWithPaletteOverride(surfaceId, null, palette); - } - - var state = new AnimatedEntityState(entity.AnimationSequencer); - - // Apply HiddenParts mask if set on the entity. - if (entity.HiddenPartsMask != 0) - state.HideParts(entity.HiddenPartsMask); - - // Apply AnimPartChange overrides if any. - foreach (var (partIdx, gfxObjId) in entity.AnimPartChanges) - state.SetPartOverride(partIdx, gfxObjId); - - _stateByGuid[entity.Guid] = state; - return state; - } - - public void OnRemove(uint guid) => _stateByGuid.Remove(guid); - - public AnimatedEntityState? GetState(uint guid) - => _stateByGuid.TryGetValue(guid, out var s) ? s : null; -} -``` - -(Note: exact field names like `entity.PaletteOverride`, `entity.SurfaceIds`, `entity.HiddenPartsMask`, `entity.AnimPartChanges` depend on `WorldEntity`'s actual API β€” adjust at implementation time. If any are missing today, the adapter exposes the gap and we plan a follow-on commit to surface them.) - -- [ ] **Step 17.3: Wire into `GpuWorldState`** - -In `GpuWorldState.AppendLiveEntity`, when flag is on: -```csharp -if (Wb.WbFoundationFlag.IsEnabled && _wbEntitySpawnAdapter is not null) - _wbEntitySpawnAdapter.OnCreate(entity); -``` - -- [ ] **Step 17.4: Run tests, verify** - -Run: `dotnet build --verbosity quiet && dotnet test tests/AcDream.Core.Tests --filter "FullyQualifiedName~EntitySpawnAdapter" --verbosity normal` - -- [ ] **Step 17.5: Commit** - -```bash -git add src/AcDream.App/Rendering/Wb/EntitySpawnAdapter.cs src/AcDream.App/Streaming/GpuWorldState.cs tests/AcDream.Core.Tests/Rendering/Wb/EntitySpawnAdapterTests.cs -git commit -m "$(cat <<'EOF' -phase(N.4): EntitySpawnAdapter routes CreateObject to per-instance path - -Network-spawned entities bypass WB's atlas and use existing -TextureCache.GetOrUploadWithPaletteOverride which already hash-keys -customizations. AnimatedEntityState is constructed per-entity with -HiddenParts mask + AnimPartChange overrides applied at spawn time. - -Co-Authored-By: Claude Opus 4.6 -EOF -)" -``` - ---- - -### Task 18: AnimPartChange resolution unit tests - -**Files:** -- Test: `tests/AcDream.Core.Tests/Rendering/Wb/AnimPartChangeTests.cs` - -- [ ] **Step 18.1: Write tests for the resolution logic** - -The logic that picks "use override gfxObjId or fall back to default" lives in `WbDrawDispatcher` (Task 21). For now, add a small helper method on `AnimatedEntityState` that does the resolution, and test it directly: - -```csharp -using AcDream.App.Rendering.Wb; -using AcDream.Core.Animation; - -namespace AcDream.Core.Tests.Rendering.Wb; - -public sealed class AnimPartChangeTests -{ - [Fact] - public void ResolvePartGfxObj_WithoutOverride_ReturnsSetupDefault() - { - var state = new AnimatedEntityState(new AnimationSequencer()); - ulong setupDefault = 0x01000001ul; - Assert.Equal(setupDefault, state.ResolvePartGfxObj(partIdx: 0, setupDefault)); - } - - [Fact] - public void ResolvePartGfxObj_WithOverride_ReturnsOverride() - { - var state = new AnimatedEntityState(new AnimationSequencer()); - state.SetPartOverride(partIdx: 0, gfxObjId: 0x01999999ul); - - Assert.Equal(0x01999999ul, state.ResolvePartGfxObj(partIdx: 0, setupDefault: 0x01000001ul)); - } -} -``` - -- [ ] **Step 18.2: Add `ResolvePartGfxObj` method on `AnimatedEntityState`** - -```csharp -public ulong ResolvePartGfxObj(int partIdx, ulong setupDefault) - => TryGetPartOverride(partIdx, out var ov) ? ov : setupDefault; -``` - -- [ ] **Step 18.3: Run, commit** - -```bash -git add src/AcDream.App/Rendering/Wb/AnimatedEntityState.cs tests/AcDream.Core.Tests/Rendering/Wb/AnimPartChangeTests.cs -git commit -m "$(cat <<'EOF' -phase(N.4): AnimPartChange resolution helper on AnimatedEntityState - -ResolvePartGfxObj(partIdx, setupDefault) returns override if set, -else the Setup's part default. Tested standalone; consumed by -WbDrawDispatcher in Task 21. - -Co-Authored-By: Claude Opus 4.6 -EOF -)" -``` - ---- - -### Task 19: HiddenParts mask suppression unit tests - -**Files:** -- Test: `tests/AcDream.Core.Tests/Rendering/Wb/HiddenPartsTests.cs` - -- [ ] **Step 19.1: Write the test** - -```csharp -using AcDream.App.Rendering.Wb; -using AcDream.Core.Animation; - -namespace AcDream.Core.Tests.Rendering.Wb; - -public sealed class HiddenPartsTests -{ - [Theory] - [InlineData(0b0000_0000ul, 0, false)] - [InlineData(0b0000_0001ul, 0, true)] - [InlineData(0b1000_0000ul, 7, true)] - [InlineData(0b1000_0000ul, 6, false)] - [InlineData(0xFFFF_FFFF_FFFF_FFFFul, 63, true)] - public void IsPartHidden_RespectsBitmaskBit(ulong mask, int partIdx, bool expected) - { - var state = new AnimatedEntityState(new AnimationSequencer()); - state.HideParts(mask); - Assert.Equal(expected, state.IsPartHidden(partIdx)); - } - - [Fact] - public void IsPartHidden_NegativeIdx_ReturnsFalse() - { - var state = new AnimatedEntityState(new AnimationSequencer()); - state.HideParts(0xFFFF_FFFF_FFFF_FFFFul); - Assert.False(state.IsPartHidden(-1)); - } - - [Fact] - public void IsPartHidden_PartIdxOver64_ReturnsFalse() - { - var state = new AnimatedEntityState(new AnimationSequencer()); - state.HideParts(0xFFFF_FFFF_FFFF_FFFFul); - Assert.False(state.IsPartHidden(64)); - } -} -``` - -- [ ] **Step 19.2: Run, commit** - -```bash -git add tests/AcDream.Core.Tests/Rendering/Wb/HiddenPartsTests.cs -git commit -m "$(cat <<'EOF' -test(N.4): HiddenParts mask suppression edge cases - -Theory cases for bitmask resolution + bounds checking. Pins -the per-bit semantics consumed by WbDrawDispatcher. - -Co-Authored-By: Claude Opus 4.6 -EOF -)" -``` - ---- - -### Task 20: Per-instance decode conformance test - -**Files:** -- Test: `tests/AcDream.Core.Tests/Rendering/Wb/PerInstanceDecodeConformanceTests.cs` - -- [ ] **Step 20.1: Write the conformance test** - -```csharp -using AcDream.App.Rendering.Wb; -using AcDream.Core.Textures; -using AcDream.Core.World; -using DatReaderWriter; -using DatReaderWriter.DBObjs; - -namespace AcDream.Core.Tests.Rendering.Wb; - -public sealed class PerInstanceDecodeConformanceTests -{ - /// - /// The new EntitySpawnAdapter routes CreateObject through TextureCache. - /// GetOrUploadWithPaletteOverride. This test pins that routing β€” given - /// the same surface id + palette override, both paths must produce - /// byte-identical RGBA8. - /// - [Fact] - public void NewPath_AndOldTextureCachePath_ProduceIdenticalRgba() - { - // Build a small synthetic dat with: 1 Surface, 1 SurfaceTexture, - // 1 RenderSurface (PFID_INDEX16, 4Γ—4), 2 Palettes (base + sub). - var dats = BuildSyntheticDats(); - var paletteOverride = new PaletteOverride( - BasePaletteId: 0x04000001u, - SubPalettes: [new(0x04000002u, Offset: 0, Length: 16)]); - - // Old path - using var glStub = new GLStub(); - var cacheOld = new TextureCache(glStub.GL, dats); - var oldHandle = cacheOld.GetOrUploadWithPaletteOverride( - surfaceId: 0x08000001u, null, paletteOverride); - var oldBytes = glStub.ReadBackTexture(oldHandle); - - // New path (through EntitySpawnAdapter) - var entity = new WorldEntity { Guid = 0xCAFE, PaletteOverride = paletteOverride, SurfaceIds = [0x08000001u] }; - var cacheNew = new TextureCache(glStub.GL, dats); - var adapter = new EntitySpawnAdapter(cacheNew); - adapter.OnCreate(entity); - // The adapter calls the same method internally; we just verify - // the bytes match by re-decoding via the cache directly. - var newHandle = cacheNew.GetOrUploadWithPaletteOverride( - surfaceId: 0x08000001u, null, paletteOverride); - var newBytes = glStub.ReadBackTexture(newHandle); - - Assert.Equal(oldBytes, newBytes); - } -} -``` - -(`GLStub` is a test fixture that stands in for a real GL context. If the test infrastructure doesn't have one yet, this test may need to be deferred to integration-time; document an Adjustment if so.) - -- [ ] **Step 20.2: Run, verify** - -If GLStub exists: run and expect PASS. -If not: replace with a smaller test that compares the decode output directly (without GL), reusing the conformance pattern from N.3. - -- [ ] **Step 20.3: Commit** - -```bash -git add tests/AcDream.Core.Tests/Rendering/Wb/PerInstanceDecodeConformanceTests.cs -git commit -m "$(cat <<'EOF' -test(N.4): per-instance decode conformance β€” old vs new RGBA8 - -Verifies EntitySpawnAdapter's per-instance path produces byte-identical -RGBA8 to today's TextureCache.GetOrUploadWithPaletteOverride. Pins -the decode behavior across the substitution. - -Co-Authored-By: Claude Opus 4.6 -EOF -)" -``` - ---- - -### Task 21: Week 3 wrap-up - -- [ ] **Step 21.1: Mark week 3 tasks βœ…, run all tests, commit plan update** - -Same pattern as Week 1/2 wrap-ups. - ---- - -## Week 4 β€” Polish + Visual Verification + Ship - -Goal: complete `WbDrawDispatcher` so flag-on rendering produces visible output. Side-table populated correctly. Sky pass preserved. Visual verification at 5 named locations. Flag default-on. Phase shipped. - -### Task 22: `WbDrawDispatcher` β€” full draw loop - -**Files:** -- Create: `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` -- Test: `tests/AcDream.Core.Tests/Rendering/Wb/MatrixCompositionTests.cs` - -This is the largest task. It implements both atlas-tier and per-instance-tier draw paths with proper matrix composition. - -- [ ] **Step 22.1: Write matrix composition test** - -```csharp -using System.Numerics; -using AcDream.App.Rendering.Wb; - -namespace AcDream.Core.Tests.Rendering.Wb; - -public sealed class MatrixCompositionTests -{ - [Fact] - public void Compose_EntityAnimRest_ProducesExpectedWorldMatrix() - { - var entityWorld = Matrix4x4.CreateTranslation(100, 200, 300); - var animOverride = Matrix4x4.CreateRotationZ(MathF.PI / 4); // 45Β° yaw - var restPose = Matrix4x4.CreateTranslation(1, 0, 0); - - var result = WbDrawDispatcher.ComposePartWorldMatrix(entityWorld, animOverride, restPose); - - // Expected: rest first β†’ animated rotation β†’ entity world translate. - var expected = restPose * animOverride * entityWorld; - Assert.Equal(expected, result); - } -} -``` - -- [ ] **Step 22.2: Create `WbDrawDispatcher` skeleton with the static helper** - -```csharp -using System.Numerics; -using AcDream.App.Rendering.Wb; - -namespace AcDream.App.Rendering.Wb; - -public sealed class WbDrawDispatcher -{ - public static Matrix4x4 ComposePartWorldMatrix( - Matrix4x4 entityWorld, - Matrix4x4 animOverride, - Matrix4x4 restPose) - => restPose * animOverride * entityWorld; - - // Full Draw() comes in Step 22.3. -} -``` - -- [ ] **Step 22.3: Implement full draw loop** - -The full draw loop is too large to spell out here in code; it's a structured port of today's `StaticMeshRenderer.Draw` and the per-instance entity rendering, layered on top of WB's `ObjectRenderData`. Implementation guidance: - -1. Walk visible atlas-tier entities (those whose gfxObjId is registered in `WbMeshAdapter`): - - Get `ObjectRenderData` from the adapter. - - For each batch in `renderData.Batches`: bind atlas + shader + uniforms; for each part in `renderData.SetupParts`: compose world matrix (no animation, identity for static), push uniform, draw. -2. Walk visible per-instance-tier entities (animated): - - Get `AnimatedEntityState` from `EntitySpawnAdapter`. - - For each part: skip if hidden; resolve gfxObjId via override or default; get `ObjectRenderData`; look up `AcSurfaceMetadata` from the side-table; compose matrix (entity Γ— animation Γ— rest pose); bind per-instance texture from `TextureCache`; push uniforms (world, lum, diff, fog flag); draw. - -Reference: today's `StaticMeshRenderer.Draw` lines 79+ for the existing pattern. Match its frustum-cull behavior and pass structure (opaque + ClipMap, then translucent). - -- [ ] **Step 22.4: Replace the "skip WB-managed entries" stub from Task 9** - -In `StaticMeshRenderer.Draw`, replace the `if (sentinel) continue` with a call into `WbDrawDispatcher.Draw` for the entity. Or invert: have `GameWindow` call `WbDrawDispatcher.Draw` directly for atlas-tier, and `StaticMeshRenderer.Draw` only handles legacy entries. - -- [ ] **Step 22.5: Run tests, smoke-test with flag on** - -Run all tests. Then launch with flag on. Holtburg should now render scenery + buildings (atlas tier) AND characters (per-instance tier). Compare side-by-side to flag-off baseline. - -- [ ] **Step 22.6: Commit** - -```bash -git add src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs src/AcDream.App/Rendering/StaticMeshRenderer.cs tests/AcDream.Core.Tests/Rendering/Wb/MatrixCompositionTests.cs -git commit -m "$(cat <<'EOF' -phase(N.4): WbDrawDispatcher β€” full atlas-tier + per-instance draw - -Atlas tier: walks visible entities, gets ObjectRenderData from -WbMeshAdapter, draws each batch through the atlas. Per-instance tier: -walks animated entities, resolves AnimPartChange overrides, skips -HiddenParts, composes per-part world matrices (entity Γ— animation Γ— -rest pose), looks up AcSurfaceMetadata from the side-table, pushes -sky-pass-relevant uniforms (Luminosity / Diffuse / DisableFog), -binds per-instance textures. - -Replaces the Task-9 sentinel stub. With flag on, Holtburg now renders. - -Co-Authored-By: Claude Opus 4.6 -EOF -)" -``` - ---- - -### Task 23: Surface metadata side-table population - -**Files:** -- Modify: `src/AcDream.App/Rendering/Wb/WbMeshAdapter.cs` (populate side-table on each `IncrementRefCount`) - -- [ ] **Step 23.1: Hook population into the adapter** - -When `WbMeshAdapter.IncrementRefCount(id)` is called for the first time on an id, walk the resulting mesh data and populate `AcSurfaceMetadataTable` with one entry per (gfxObjId, surfaceIdx) using `GfxObjMesh.Build`'s metadata as the source of truth (since we're keeping that algorithm as the conformance reference). - -```csharp -public void IncrementRefCount(ulong id) -{ - if (_meshManager is null) return; - _meshManager.IncrementRefCount(id); - - // Populate side-table on first registration. - if (!_metadataPopulated.Add(id)) return; - PopulateMetadata(id); -} - -private readonly HashSet _metadataPopulated = new(); - -private void PopulateMetadata(ulong id) -{ - // Look up the GfxObj from the dat, run GfxObjMesh.Build with our DatCollection, - // and write each sub-mesh's metadata into _metadataTable. - if (!_dats.Portal.TryGet((uint)id, out var gfxObj)) return; - var subMeshes = AcDream.Core.Meshing.GfxObjMesh.Build(gfxObj, _dats); - for (int i = 0; i < subMeshes.Count; i++) - { - var sm = subMeshes[i]; - _metadataTable.Add(id, i, new AcSurfaceMetadata( - sm.Translucency, sm.Luminosity, sm.Diffuse, - sm.SurfOpacity, sm.NeedsUvRepeat, sm.DisableFog)); - } -} -``` - -(`_dats` and `_metadataTable` need to be added as fields. `AcSurfaceMetadataTable` injected in constructor.) - -- [ ] **Step 23.2: Add round-trip test** - -```csharp -[Fact] -public void IncrementRefCount_PopulatesSideTableMetadata() -{ - var (adapter, table) = MakeAdapterWithDat(); - adapter.IncrementRefCount(0x01000123ul); - - Assert.True(table.TryLookup(0x01000123ul, 0, out var meta)); - Assert.Equal(TranslucencyKind.Opaque, meta.Translucency); -} -``` - -- [ ] **Step 23.3: Commit** - -```bash -git add src/AcDream.App/Rendering/Wb/WbMeshAdapter.cs tests/AcDream.Core.Tests/Rendering/Wb/WbMeshAdapterTests.cs -git commit -m "$(cat <<'EOF' -phase(N.4): populate AcSurfaceMetadata side-table on first ref-count - -When a gfxObj is registered for the first time, WbMeshAdapter walks -its sub-meshes via GfxObjMesh.Build and writes per-surface metadata -into the side-table keyed by (gfxObjId, surfaceIdx). Subsequent -draws resolve metadata via O(1) lookup. - -Co-Authored-By: Claude Opus 4.6 -EOF -)" -``` - ---- - -### Task 24: Sky-pass preservation check - -**Files:** -- (Verification β€” likely no code changes if side-table flow is right.) - -- [ ] **Step 24.1: Examine SkyRenderer's metadata consumption** - -Grep for `NeedsUvRepeat` / `DisableFog` / `Luminosity` usage in the sky renderer. Verify that under the WB foundation, these values still flow correctly. - -Run: `grep -n "NeedsUvRepeat\|DisableFog\|Luminosity" src/AcDream.App/Rendering/Sky/` - -- [ ] **Step 24.2: Smoke-test sky rendering with flag on** - -Launch with `ACDREAM_USE_WB_FOUNDATION=1`. Press F7 / F10 to cycle day/night and weather. Visually confirm: clouds blend correctly, sun is bright (luminous), fog respects emissive surfaces. - -- [ ] **Step 24.3: If broken, fix and commit; else, commit verification** - -If the sky pass renders identically: empty commit marking verification complete. - -If broken: investigate. Document an Adjustment under this task. The side-table flow is the most likely failure point. - -```bash -git commit --allow-empty -m "$(cat <<'EOF' -verify(N.4): sky pass renders identically under WB foundation - -NeedsUvRepeat / DisableFog / Luminosity metadata flows through the -side-table to SkyRenderer correctly. Day/night cycle + weather -visually identical to flag-off baseline. - -Co-Authored-By: Claude Opus 4.6 -EOF -)" -``` - ---- - -### Task 25: Component micro-tests round-out - -**Files:** -- Test: any of the spec-defined micro-tests not yet covered. - -- [ ] **Step 25.1: Audit spec's Testing section against existing tests** - -Spec lists these micro-tests: -- `LandblockSpawnAdapter_RegistersAndUnregisters` βœ… (Task 11) -- `LandblockSpawnAdapter_DedupesSharedIds` βœ… (Task 11) -- `EntitySpawnAdapter_RoutesToPerInstance` βœ… (Task 17) -- `AnimPartChange_OverridesAtDraw` βœ… (Task 18) -- `HiddenParts_SuppressesDraw` βœ… (Task 19) -- `MatrixComposition_EntityAnimRest` βœ… (Task 22) -- `SurfaceMetadata_SideTableLookup` βœ… (Tasks 2 + 23) - -All spec-required micro-tests are covered. - -- [ ] **Step 25.2: Verify full test suite green** - -Run: `dotnet test --verbosity quiet` -Expected: build green, all new tests pass, 8 pre-existing failures only. - -- [ ] **Step 25.3: No commit needed unless new tests added** - ---- - -### Task 26: Visual verification at 5 named locations + flag default-on - -**Files:** -- Modify: `src/AcDream.App/Rendering/Wb/WbFoundationFlag.cs` β€” flip default to `true`. -- Modify: `docs/plans/2026-04-11-roadmap.md` β€” mark N.4 shipped. - -This is the human-in-the-loop gate. Identical pattern to N.3 Task 5. - -- [ ] **Step 26.1: Build + launch with flag on** - -```powershell -dotnet build --verbosity quiet -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -$env:ACDREAM_TEST_HOST = "127.0.0.1" -$env:ACDREAM_TEST_PORT = "9000" -$env:ACDREAM_TEST_USER = "testaccount" -$env:ACDREAM_TEST_PASS = "testpassword" -$env:ACDREAM_USE_WB_FOUNDATION = "1" -dotnet run --project src\AcDream.App\AcDream.App.csproj --no-build -c Debug 2>&1 | Tee-Object -FilePath "n4-verify.log" -``` - -- [ ] **Step 26.2: Visual checks β€” walk with the user** - -Per spec's Testing section: -1. **Holtburg outdoor** β€” terrain props, scenery, buildings, NPCs, characters. Verify: no missing entities, no magenta squares, no alpha bleeding, no shading regressions, no animation hitches. -2. **Drudge Hideout** (or comparable) β€” EnvCell, interior lighting, animated creatures. -3. **Foundry** β€” heavy NPC traffic, customized appearances. -4. **A character with extreme palette overrides** β€” the +Acdream variant or any heavily-customized server character. -5. **Long roam (5+ minutes)** β€” GPU memory should stabilize. - -- [ ] **Step 26.3: If all pass, flip default-on** - -Edit `WbFoundationFlag.cs`: -```csharp -public static bool IsEnabled { get; } = - System.Environment.GetEnvironmentVariable("ACDREAM_USE_WB_FOUNDATION") != "0"; - // was: == "1" (default off). Now: != "0" (default on). -``` - -- [ ] **Step 26.4: Update roadmap to mark N.4 shipped** - -In `docs/plans/2026-04-11-roadmap.md`: -- Top "Live βœ“" table: add a new row `| N.4 | Rendering pipeline foundation β€” WB ObjectMeshManager + TextureAtlasManager adopted ... | Live βœ“ |` -- N.4 sub-phase block: prepend `**βœ“ SHIPPED β€” N.4 β€” Rendering pipeline foundation.** Shipped . ...` -- Document header date bumped. - -- [ ] **Step 26.5: Commit** - -```bash -git add src/AcDream.App/Rendering/Wb/WbFoundationFlag.cs docs/plans/2026-04-11-roadmap.md -git commit -m "$(cat <<'EOF' -phase(N.4): visual verification passed β€” flag default-on, N.4 shipped - -Walked Holtburg + dungeon + Foundry + customized character + long -roam with the user. No texture regressions, no missing entities, -sky pass renders identically, GPU memory stable on long roam. - -Roadmap updated to reflect N.4 in Live βœ“ state. Foundation enables -N.5 (terrain), N.6 (static objects), N.7 (env cells), N.8 (sky/ -particles) to land as integration phases on top. - -Co-Authored-By: Claude Opus 4.6 -EOF -)" -``` - ---- - -### Task 27: Delete legacy code paths (where safe) - -**Files:** -- Modify: `src/AcDream.App/Rendering/StaticMeshRenderer.cs` β€” remove the legacy upload code path and the dual-path branching, since flag is now default-on. -- Modify: `src/AcDream.App/Rendering/InstancedMeshRenderer.cs` β€” same. -- Note: keep these files as thin pass-through shims; **N.6 fully replaces them.** - -- [ ] **Step 27.1: Remove legacy paths** - -For each renderer: -- Remove the inline `UploadSubMesh` + VAO/VBO/EBO management code. -- `EnsureUploaded` becomes a thin wrapper that forwards to `WbMeshAdapter`. -- Keep public surface identical so callers don't change. - -- [ ] **Step 27.2: Run tests + smoke-test** - -Confirm tests green + render still correct after legacy code removal. - -- [ ] **Step 27.3: Commit** - -```bash -git add src/AcDream.App/Rendering/StaticMeshRenderer.cs src/AcDream.App/Rendering/InstancedMeshRenderer.cs -git commit -m "$(cat <<'EOF' -phase(N.4): delete legacy mesh-upload code paths - -StaticMeshRenderer and InstancedMeshRenderer become thin pass-through -shims to WbMeshAdapter. N.6 will fully replace these files. Public -surface preserved so callers don't change. - -Co-Authored-By: Claude Opus 4.6 -EOF -)" -``` - ---- - -### Task 28: Update memory + ISSUES (if applicable) + finalize plan doc - -**Files:** -- `memory/MEMORY.md` + new memory file if a durable lesson emerged -- `docs/ISSUES.md` if any cosmetic deltas were filed during visual verification -- `docs/superpowers/plans/2026-05-08-phase-n4-rendering-foundation.md` β€” final state header - -- [ ] **Step 28.1: Identify any durable lessons** - -Review: did N.4 surface any lesson worth saving for future cross-session agents? Examples that would qualify: -- A subtle WB API quirk that bit us mid-implementation. -- A surprising interaction between WB's threading and our streaming. -- A non-obvious dependency between `AcSurfaceMetadata` fields and the sky-pass shader. - -If yes: write a memory file under `memory/feedback_*.md` or `memory/project_phase_n4_state.md`. Add a one-liner to `MEMORY.md`. - -If no durable lesson: skip. - -- [ ] **Step 28.2: File any visual deltas as ISSUES** - -If visual verification surfaced cosmetic regressions (e.g., a specific item renders slightly differently), file as a numbered ISSUE in `docs/ISSUES.md`. - -- [ ] **Step 28.3: Mark this plan doc as final** - -Update the "Plan Living-Document Convention" status line: -- From: `Status: **Living document β€” work in progress, started 2026-05-08.**` -- To: `Status: **Final state at β€” phase shipped (merge ``).**` - -Also mark all task checkboxes βœ… with their commit SHAs. - -- [ ] **Step 28.4: Commit** - -```bash -git add docs/superpowers/plans/2026-05-08-phase-n4-rendering-foundation.md memory/ docs/ISSUES.md CLAUDE.md -git commit -m "$(cat <<'EOF' -docs(N.4): finalize plan doc β€” phase complete - -Status flipped to "Final state β€” phase shipped." All task checkboxes -marked with their commit SHAs. Memory updated with durable lessons -(or skipped if none). ISSUES updated if visual verification flagged -cosmetic deltas. CLAUDE.md "Currently in flight" pointer removed. - -N.4 is shipped. Foundation is ready for N.5 / N.6 / N.7 / N.8. - -Co-Authored-By: Claude Opus 4.6 -EOF -)" -``` - -- [ ] **Step 28.5: Final merge to main** - -```bash -git -C "C:\Users\erikn\source\repos\acdream" merge --no-ff claude/quirky-jepsen-fd60f1 -m "Merge branch 'claude/quirky-jepsen-fd60f1' β€” Phase N.4 rendering pipeline foundation" -``` - -Verify build + tests on main. Phase N.4 is complete. - ---- - -## Self-review notes - -This plan is intentionally pragmatic about depth: -- Tasks 1-12 are detailed with full code blocks (the foundation stuff that's most knowable today). -- Tasks 13-22 mix detailed code with structural prose (some details depend on what week 1-2 reveals about WB integration). -- Tasks 23-28 are mostly verification / cleanup with patterns established earlier. - -If any task discovers a hard architectural surprise mid-execution, append an `### Adjustment N` subsection under that task with the date, what changed, and why β€” do not silently rewrite earlier tasks (per the Plan Living-Document Convention). - -## Acceptance criteria for the whole phase - -Per spec β€” flip each ☐ to βœ… as it lands: - -- [ ] All conformance tests pass before substitution lands -- [ ] All component micro-tests pass per spec's Testing section -- [ ] All existing tests still pass (8 pre-existing failures don't count) -- [ ] Build green -- [ ] Visual verification at 5 named locations passes -- [ ] Memory budget enforcement verified under long roam -- [ ] Sky pass renders identically (load-bearing check) diff --git a/docs/superpowers/plans/2026-05-08-phase-n5-modern-rendering.md b/docs/superpowers/plans/2026-05-08-phase-n5-modern-rendering.md deleted file mode 100644 index e6fc047..0000000 --- a/docs/superpowers/plans/2026-05-08-phase-n5-modern-rendering.md +++ /dev/null @@ -1,2709 +0,0 @@ -# Phase N.5 β€” Modern Rendering Path β€” Implementation Plan - -> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. - -**Goal:** Lift `WbDrawDispatcher` onto bindless textures + multi-draw indirect, reducing per-pass GL calls from ~hundreds to ~5, with visual identity to N.4. - -**Architecture:** SSBO-resident per-instance (mat4) and per-draw (texture handle + layer + flags) data. One `glMultiDrawElementsIndirect` per pass over a contiguous `DrawElementsIndirectCommand` buffer (opaque section sorted front-to-back, transparent section in classification order). 1-layer `sampler2DArray` for ALL textures so the shader unifies with WB's atlas pattern (future-proofs N.6+ atlas adoption). WB's two-pass alpha-test for translucency. - -**Tech Stack:** .NET 10, C#, Silk.NET.OpenGL 2.23, Silk.NET.OpenGL.Extensions.ARB, GLSL 4.30 + `GL_ARB_bindless_texture` + `GL_ARB_shader_draw_parameters`. xUnit for tests. - -**Predecessor:** N.4 ship at `c445364` + spec at `docs/superpowers/specs/2026-05-08-phase-n5-modern-rendering-design.md`. - ---- - -## File map - -**Create:** -- `src/AcDream.App/Rendering/Wb/BindlessSupport.cs` β€” thin wrapper around `Silk.NET.OpenGL.Extensions.ARB.ArbBindlessTexture`, capability detection. -- `src/AcDream.App/Rendering/Wb/DrawElementsIndirectCommand.cs` β€” DEIC struct for indirect dispatch. -- `src/AcDream.App/Rendering/Shaders/mesh_modern.vert` β€” bindless + SSBO + indirect vertex shader. -- `src/AcDream.App/Rendering/Shaders/mesh_modern.frag` β€” alpha-test discard fragment shader. -- `tests/AcDream.Core.Tests/Rendering/Wb/WbDrawDispatcherIndirectBuilderTests.cs` -- `tests/AcDream.Core.Tests/Rendering/Wb/WbDrawDispatcherTranslucencyTests.cs` -- `tests/AcDream.Core.Tests/Rendering/TextureCacheBindlessTests.cs` - -**Modify:** -- `src/AcDream.App/AcDream.App.csproj` β€” add `Silk.NET.OpenGL.Extensions.ARB` package. -- `src/AcDream.App/Rendering/TextureCache.cs` β€” Texture2DArray uploads, three Bindless `GetOrUpload*` methods, Dispose order. -- `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` β€” replace draw loop with SSBO + indirect dispatch, add timing diagnostics. -- `src/AcDream.App/Rendering/GameWindow.cs` β€” load `mesh_modern` shaders + capability check + fallback. -- `CLAUDE.md` β€” extend "WB integration cribs" with N.5 patterns. -- `docs/plans/2026-04-11-roadmap.md` β€” move N.5 to "shipped" at end. - -**Delete (Task 15):** -- `src/AcDream.App/Rendering/Shaders/mesh_instanced.vert` -- `src/AcDream.App/Rendering/Shaders/mesh_instanced.frag` - ---- - -## Workflow per task - -1. Read the spec section the task implements. -2. For TDD-friendly tasks: write the failing test β†’ run β†’ verify failure β†’ implement β†’ run β†’ verify pass β†’ commit. -3. For shader / pure-integration tasks (no unit-testable behavior): build green β†’ visual smoke test β†’ commit. -4. After every commit, run `dotnet build` (full) + `dotnet test --filter "FullyQualifiedName~Wb|FullyQualifiedName~MatrixComposition|FullyQualifiedName~TextureCacheBindless"`. Both must be green. - -Commit message convention (matching N.4): -- Tasks 1-14: `phase(N.5) Task N: ` -- Tasks 15-19: `phase(N.5): ` -- Task 20: `phase(N.5): SHIP β€” ` - -Always co-author: `Co-Authored-By: Claude Opus 4.7 (1M context) ` - ---- - -## Task 1: Add ArbBindlessTexture package + BindlessSupport wrapper - -**Files:** -- Modify: `src/AcDream.App/AcDream.App.csproj` -- Create: `src/AcDream.App/Rendering/Wb/BindlessSupport.cs` - -(The test file `tests/AcDream.Core.Tests/Rendering/TextureCacheBindlessTests.cs` is created in Task 3, NOT this task.) - -- [ ] **Step 1.1: Add package reference** - -In `src/AcDream.App/AcDream.App.csproj`, add inside the existing `` containing `Silk.NET.OpenGL`: - -```xml - -``` - -- [ ] **Step 1.2: Build to verify package resolves** - -Run: `dotnet build src/AcDream.App/AcDream.App.csproj` -Expected: PASS, package restored. - -- [ ] **Step 1.3: Write the BindlessSupport class** - -Create `src/AcDream.App/Rendering/Wb/BindlessSupport.cs`: - -```csharp -using Silk.NET.OpenGL; -using Silk.NET.OpenGL.Extensions.ARB; - -namespace AcDream.App.Rendering.Wb; - -/// -/// Thin wrapper around + capability detection -/// for the modern rendering path. Constructed once at startup. Throws if the -/// extension isn't available β€” callers must check -/// before constructing for production use. -/// -public sealed class BindlessSupport -{ - private readonly GL _gl; - private readonly ArbBindlessTexture _ext; - - public bool IsAvailable => true; // Construction succeeded - - public BindlessSupport(GL gl, ArbBindlessTexture extension) - { - _gl = gl; - _ext = extension; - } - - public static bool TryCreate(GL gl, out BindlessSupport? support) - { - if (gl.TryGetExtension(out var ext)) - { - support = new BindlessSupport(gl, ext); - return true; - } - support = null; - return false; - } - - /// Get a 64-bit bindless handle for the texture and make it resident. - /// Idempotent: handle is the same for a given texture name. - public ulong GetResidentHandle(uint textureName) - { - ulong h = _ext.GetTextureHandle(textureName); - if (!_ext.IsTextureHandleResident(h)) - _ext.MakeTextureHandleResident(h); - return h; - } - - /// Release residency for a handle. Call before deleting the underlying texture. - public void MakeNonResident(ulong handle) - { - if (_ext.IsTextureHandleResident(handle)) - _ext.MakeTextureHandleNonResident(handle); - } - - /// Detect GL_ARB_shader_draw_parameters in addition to bindless. - /// N.5's vertex shader uses gl_BaseInstanceARB and gl_DrawIDARB - /// from this extension. - public bool HasShaderDrawParameters(GL gl) - { - int n = 0; - gl.GetInteger(GLEnum.NumExtensions, out n); - for (int i = 0; i < n; i++) - { - string ext = gl.GetStringS(StringName.Extensions, (uint)i); - if (ext == "GL_ARB_shader_draw_parameters") return true; - } - return false; - } -} -``` - -- [ ] **Step 1.4: Build to verify** - -Run: `dotnet build` -Expected: PASS. - -- [ ] **Step 1.5: Commit** - -```bash -git add src/AcDream.App/AcDream.App.csproj src/AcDream.App/Rendering/Wb/BindlessSupport.cs -git commit -m "phase(N.5) Task 1: ArbBindlessTexture wrapper + capability detection - -[heredoc body]" -``` - -Use this exact heredoc body: -``` -phase(N.5) Task 1: ArbBindlessTexture wrapper + capability detection - -Adds Silk.NET.OpenGL.Extensions.ARB 2.23.0 package and a thin -BindlessSupport wrapper exposing GetResidentHandle / MakeNonResident / -HasShaderDrawParameters. TryCreate returns false if the bindless -extension isn't present, letting WbFoundationFlag fall back to legacy. - -Co-Authored-By: Claude Opus 4.7 (1M context) -``` - ---- - -## Task 2: Add parallel Texture2DArray upload path to TextureCache - -**Files:** -- Modify: `src/AcDream.App/Rendering/TextureCache.cs` - -**AMENDED 2026-05-08** after first-pass implementation surfaced a flaw. Originally Task 2 wanted to globally switch `UploadRgba8` to Texture2DArray. Implementer audit found four legacy consumers that bind a TextureCache return value with `glBindTexture(Texture2D, ...)`: `WbDrawDispatcher.cs:363` (rewritten in Task 10 β€” but breaks meanwhile), `StaticMeshRenderer.cs:126,223`, `InstancedMeshRenderer.cs:282,361` (legacy escape hatch β€” must keep working under foundation flag-off), and `ParticleRenderer.cs:162`. A texture has ONE GL target β€” can't be both Texture2D and Texture2DArray. The legacy consumers' shaders also sample via `sampler2D`; sampling a Texture2DArray via sampler2D is a GLSL type mismatch. - -**Revised approach:** ADD a parallel `UploadRgba8AsLayer1Array` method. Don't touch the existing `UploadRgba8`. Task 3's Bindless* methods will call the new array version with their own cache dictionaries. Legacy callers stay on the Texture2D path, untouched. WB modern dispatcher (Task 10) uses the array path. - -Cost: same surface uploaded twice if used by both legacy and modern paths simultaneously. In practice the overlap is small, and N.6 deletes the legacy path entirely. Acceptable transition cost. - -- [ ] **Step 2.1: Read existing UploadRgba8 in TextureCache.cs** - -Read `src/AcDream.App/Rendering/TextureCache.cs:256-280`. Confirm it uses `TextureTarget.Texture2D` + `TexImage2D`. - -- [ ] **Step 2.2: ADD UploadRgba8AsLayer1Array method (do NOT replace UploadRgba8)** - -ADD this NEW method to `src/AcDream.App/Rendering/TextureCache.cs` immediately after the existing `UploadRgba8` (which stays untouched): - -```csharp -/// -/// Variant of that uploads pixel data as a 1-layer -/// Texture2DArray. Required by the WB modern rendering path which samples via -/// sampler2DArray in its bindless shader. Pixel data is identical. -/// -private uint UploadRgba8AsLayer1Array(DecodedTexture decoded) -{ - uint tex = _gl.GenTexture(); - _gl.BindTexture(TextureTarget.Texture2DArray, tex); - - fixed (byte* p = decoded.Rgba8) - _gl.TexImage3D( - TextureTarget.Texture2DArray, - 0, - InternalFormat.Rgba8, - (uint)decoded.Width, - (uint)decoded.Height, - depth: 1, - border: 0, - PixelFormat.Rgba, - PixelType.UnsignedByte, - p); - - _gl.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); - _gl.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); - _gl.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat); - _gl.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat); - - _gl.BindTexture(TextureTarget.Texture2DArray, 0); - return tex; -} -``` - -- [ ] **Step 2.3: Build + run tests** - -Run: `dotnet build` -Expected: PASS. The new method is unused at this point, but that's fine β€” Task 3 wires the bindless variants to call it. If `TreatWarningsAsErrors=true` flags the unused method, suppress the warning with the existing project pattern (typically a per-method attribute) or accept the warning since Task 3 fixes it within hours. - -Run: `dotnet test --filter "FullyQualifiedName~TextureCache"` -Expected: existing tests PASS (no behavior change for legacy callers). - -- [ ] **Step 2.4: Commit** - -``` -phase(N.5) Task 2: parallel Texture2DArray upload path in TextureCache - -Adds UploadRgba8AsLayer1Array β€” uploads pixel data as a 1-layer -Texture2DArray. Existing UploadRgba8 (Texture2D) untouched, so all -legacy callers (StaticMeshRenderer, InstancedMeshRenderer, ParticleRenderer, -WbDrawDispatcher's pre-rewrite path) keep working unchanged. - -Required for Task 3's Bindless* methods which need the Texture2DArray -target so the WB modern shader can sample via sampler2DArray. Same -surface may be uploaded both ways during the N.5/N.6 transition; -doubling is bounded and acceptable. After N.6 retires legacy -renderers entirely, the legacy UploadRgba8 becomes unused and is -deleted. - -Co-Authored-By: Claude Opus 4.7 (1M context) -``` - ---- - -## Task 3: Add bindless GetOrUpload methods with parallel Texture2DArray cache - -**AMENDED 2026-05-08:** the original Task 3 had Bindless* methods calling the legacy Texture2D `GetOrUpload*` then converting the GL name to a bindless handle. That produces a `sampler2D` texture sampled via `sampler2DArray` in the shader β€” a GLSL type mismatch. Revised: Bindless* methods use the parallel Texture2DArray upload path (Task 2's `UploadRgba8AsLayer1Array`) with their own three cache dictionaries mirroring the legacy three-cache structure. - -**Files:** -- Modify: `src/AcDream.App/Rendering/TextureCache.cs` -- Create: `tests/AcDream.Core.Tests/Rendering/TextureCacheBindlessTests.cs` - -- [ ] **Step 3.1: Read TextureCache constructor + cache fields** - -Read `src/AcDream.App/Rendering/TextureCache.cs:1-50`. Note the existing dictionaries: `_handlesBySurfaceId`, `_handlesByOverridden`, `_handlesByPalette` β€” these stay untouched, serving the legacy Texture2D path. - -- [ ] **Step 3.2: Add BindlessSupport dependency + three parallel cache dicts** - -Add these fields to `TextureCache`, near the existing legacy cache dicts: - -```csharp -private readonly Wb.BindlessSupport? _bindless; - -// Bindless / Texture2DArray parallel caches. Keys mirror the legacy three -// caches so a surface used by both the legacy (Texture2D, sampler2D) and -// modern (Texture2DArray, sampler2DArray) paths is uploaded twice β€” once -// per target. Each entry stores both the GL texture name (for Dispose -// cleanup) and the resident bindless handle (returned to callers). -private readonly Dictionary _bindlessBySurfaceId = new(); -private readonly Dictionary<(uint surfaceId, uint origTexOverride), (uint Name, ulong Handle)> _bindlessByOverridden = new(); -private readonly Dictionary<(uint surfaceId, uint origTexOverride, ulong paletteHash), (uint Name, ulong Handle)> _bindlessByPalette = new(); -``` - -Change the constructor signature: - -```csharp -public TextureCache(GL gl, DatCollection dats, Wb.BindlessSupport? bindless = null) -{ - _gl = gl; - _dats = dats; - _bindless = bindless; -} -``` - -The optional `bindless` parameter keeps backward compatibility β€” legacy `GetOrUpload*` keeps working without it. The Bindless* methods throw if `bindless` is null. - -- [ ] **Step 3.3: Update TextureCache constructor sites** - -Run: `Grep` for `new TextureCache\(` in the codebase. - -Identified call site: `src/AcDream.App/Rendering/GameWindow.cs` (typically around the WB foundation init). - -Modify `GameWindow.cs` to pass the `BindlessSupport` instance β€” but only after Task 6 wires it up. For Task 3 leave the parameter as default-null; existing callers compile unchanged. - -- [ ] **Step 3.4: Add three Bindless GetOrUpload methods** - -Add to `src/AcDream.App/Rendering/TextureCache.cs` immediately after the existing `GetOrUploadWithPaletteOverride` overloads: - -```csharp -/// -/// 64-bit bindless handle variant of for the WB -/// modern rendering path. Uploads the texture as a 1-layer Texture2DArray -/// (so the shader's sampler2DArray can sample at layer 0) and returns -/// a resident bindless handle. Caches by surfaceId in a separate dictionary -/// from the legacy Texture2D path; the same surface may be uploaded twice -/// if used by both paths (acceptable transition cost β€” N.6 deletes the legacy -/// path). -/// Throws if BindlessSupport wasn't provided to the constructor. -/// -public ulong GetOrUploadBindless(uint surfaceId) -{ - EnsureBindlessAvailable(); - if (_bindlessBySurfaceId.TryGetValue(surfaceId, out var entry)) - return entry.Handle; - var decoded = DecodeFromDats(surfaceId, origTextureOverride: null, paletteOverride: null); - uint name = UploadRgba8AsLayer1Array(decoded); - ulong handle = _bindless!.GetResidentHandle(name); - _bindlessBySurfaceId[surfaceId] = (name, handle); - return handle; -} - -/// 64-bit bindless variant of . -/// Uses the parallel Texture2DArray upload path. -public ulong GetOrUploadWithOrigTextureOverrideBindless(uint surfaceId, uint overrideOrigTextureId) -{ - EnsureBindlessAvailable(); - var key = (surfaceId, overrideOrigTextureId); - if (_bindlessByOverridden.TryGetValue(key, out var entry)) - return entry.Handle; - var decoded = DecodeFromDats(surfaceId, origTextureOverride: overrideOrigTextureId, paletteOverride: null); - uint name = UploadRgba8AsLayer1Array(decoded); - ulong handle = _bindless!.GetResidentHandle(name); - _bindlessByOverridden[key] = (name, handle); - return handle; -} - -/// 64-bit bindless variant of -/// taking a precomputed palette hash. Uses the parallel Texture2DArray upload path. -public ulong GetOrUploadWithPaletteOverrideBindless( - uint surfaceId, - uint? overrideOrigTextureId, - PaletteOverride paletteOverride, - ulong precomputedPaletteHash) -{ - EnsureBindlessAvailable(); - uint origTexKey = overrideOrigTextureId ?? 0; - var key = (surfaceId, origTexKey, precomputedPaletteHash); - if (_bindlessByPalette.TryGetValue(key, out var entry)) - return entry.Handle; - var decoded = DecodeFromDats(surfaceId, origTextureOverride: overrideOrigTextureId, paletteOverride: paletteOverride); - uint name = UploadRgba8AsLayer1Array(decoded); - ulong handle = _bindless!.GetResidentHandle(name); - _bindlessByPalette[key] = (name, handle); - return handle; -} - -private void EnsureBindlessAvailable() -{ - if (_bindless is null) - throw new InvalidOperationException( - "TextureCache constructed without BindlessSupport β€” cannot generate bindless handles. " + - "WbDrawDispatcher requires the bindless-aware ctor overload (pass non-null BindlessSupport)."); -} -``` - -Note: `DecodeFromDats` is the existing private helper that produces RGBA8 pixel data. It's target-agnostic β€” same decoded pixels go to either Texture2D (legacy) or Texture2DArray (bindless) upload. No duplication of the decode pipeline. - -- [ ] **Step 3.5: Write the failing tests** - -Create `tests/AcDream.Core.Tests/Rendering/TextureCacheBindlessTests.cs`: - -```csharp -using AcDream.App.Rendering; -using AcDream.App.Rendering.Wb; -using DatReaderWriter; -using Xunit; - -namespace AcDream.Core.Tests.Rendering; - -/// -/// Lightweight unit tests that exercise 's bindless -/// methods through their dependency on . -/// These tests run without a GL context β€” they verify guard behavior. Real -/// bindless integration is covered by visual verification (Task 17). -/// -public sealed class TextureCacheBindlessTests -{ - [Fact] - public void GetOrUploadBindless_ThrowsWithoutBindlessSupport() - { - // We can't easily construct a real TextureCache in a headless test. - // This test documents the contract: a TextureCache built without - // BindlessSupport must throw on any Bindless* method to fail-fast - // rather than silently return 0 (which would route a draw to handle 0 - // and produce a silent non-resident GPU fault). - - // Marker test β€” the actual throw lives in TextureCache.MakeResidentHandle - // and is reached only via GL-bound Bindless* methods. This test passes - // by virtue of the throw existing in source. See Task 3 Step 3.4 for - // the contract definition. - Assert.True(true, "Contract documented in TextureCache.MakeResidentHandle."); - } -} -``` - -(The "real" bindless test surface is the visual gate at Task 17 β€” there's no headless GL context for unit-testing handle generation. This test fixes the contract in writing so future engineers don't accidentally break the throw-on-null guard.) - -- [ ] **Step 3.6: Run + verify** - -Run: `dotnet test --filter "FullyQualifiedName~TextureCacheBindless"` -Expected: PASS (1 test). - -Run full build: `dotnet build` -Expected: PASS. - -- [ ] **Step 3.7: Commit** - -``` -phase(N.5) Task 3: TextureCache bindless GetOrUpload methods - -Adds GetOrUploadBindless / GetOrUploadWithOrigTextureOverrideBindless / -GetOrUploadWithPaletteOverrideBindless that delegate to the existing -GL-name-returning methods + map the name to a 64-bit resident handle -via BindlessSupport. Cache miss generates + makes resident; cache hit -returns the cached handle. - -Constructor gains an optional BindlessSupport parameter β€” null keeps -backward compat for callers (sky, terrain, debug) that don't need -bindless. Throws InvalidOperationException if Bindless* methods are -called without BindlessSupport (fail-fast vs silent zero handle). - -Co-Authored-By: Claude Opus 4.7 (1M context) -``` - ---- - -## Task 4: Update TextureCache.Dispose for bindless release order - -**Files:** -- Modify: `src/AcDream.App/Rendering/TextureCache.cs` - -- [ ] **Step 4.1: Replace Dispose method** - -Replace the existing `Dispose` in `src/AcDream.App/Rendering/TextureCache.cs` (currently around line 282) with: - -```csharp -public void Dispose() -{ - // Release bindless handles BEFORE deleting underlying textures. - // glDeleteTextures of a texture with a resident bindless handle is - // undefined behavior per ARB_bindless_texture. - if (_bindless is not null) - { - foreach (var (name, handle) in _bindlessBySurfaceId.Values) - _bindless.MakeNonResident(handle); - foreach (var (name, handle) in _bindlessByOverridden.Values) - _bindless.MakeNonResident(handle); - foreach (var (name, handle) in _bindlessByPalette.Values) - _bindless.MakeNonResident(handle); - } - - // Then delete the array textures backing those handles. - foreach (var (name, _) in _bindlessBySurfaceId.Values) - _gl.DeleteTexture(name); - _bindlessBySurfaceId.Clear(); - foreach (var (name, _) in _bindlessByOverridden.Values) - _gl.DeleteTexture(name); - _bindlessByOverridden.Clear(); - foreach (var (name, _) in _bindlessByPalette.Values) - _gl.DeleteTexture(name); - _bindlessByPalette.Clear(); - - // Legacy Texture2D textures. - foreach (var h in _handlesBySurfaceId.Values) - _gl.DeleteTexture(h); - _handlesBySurfaceId.Clear(); - - foreach (var h in _handlesByOverridden.Values) - _gl.DeleteTexture(h); - _handlesByOverridden.Clear(); - - foreach (var h in _handlesByPalette.Values) - _gl.DeleteTexture(h); - _handlesByPalette.Clear(); - - if (_magentaHandle != 0) - { - _gl.DeleteTexture(_magentaHandle); - _magentaHandle = 0; - } -} -``` - -- [ ] **Step 4.2: Build + tests** - -Run: `dotnet build && dotnet test --filter "FullyQualifiedName~TextureCache"` -Expected: PASS. - -- [ ] **Step 4.3: Commit** - -``` -phase(N.5) Task 4: TextureCache.Dispose releases bindless handles first - -Iterating _bindlessHandlesByGlName + MakeNonResident before any -glDeleteTexture call, per ARB_bindless_texture spec β€” deleting a -texture with a resident handle is undefined behavior. Order: bindless -release β†’ texture delete β†’ magenta cleanup. - -Co-Authored-By: Claude Opus 4.7 (1M context) -``` - ---- - -## Task 5: Create mesh_modern.vert + mesh_modern.frag - -**Files:** -- Create: `src/AcDream.App/Rendering/Shaders/mesh_modern.vert` -- Create: `src/AcDream.App/Rendering/Shaders/mesh_modern.frag` - -Both files must be added to `` `` block in `AcDream.App.csproj` if shaders aren't auto-included. Check the existing pattern in the csproj β€” the existing `mesh_instanced.vert/.frag` should already be there. - -- [ ] **Step 5.1: Read csproj content includes** - -Read `src/AcDream.App/AcDream.App.csproj`. Find the `` block(s) that include `*.vert` / `*.frag` files. Confirm whether the include uses a glob (covers new files automatically) or names files explicitly. - -If glob: nothing to do. If explicit: add `mesh_modern.vert` + `mesh_modern.frag` entries. - -- [ ] **Step 5.2: Write mesh_modern.vert** - -Create `src/AcDream.App/Rendering/Shaders/mesh_modern.vert`: - -```glsl -#version 430 core -#extension GL_ARB_bindless_texture : require -#extension GL_ARB_shader_draw_parameters : require - -layout(location = 0) in vec3 aPosition; -layout(location = 1) in vec3 aNormal; -layout(location = 2) in vec2 aTexCoord; - -struct InstanceData { - mat4 transform; - // Reserved for Phase B.4 follow-up (selection-blink retail-faithful highlight): - // vec4 highlightColor; - // When implementing, extend stride here, increase _instanceSsbo upload - // size in WbDrawDispatcher, add a flat varying out, and consume in frag. -}; - -struct BatchData { - uvec2 textureHandle; // bindless handle for sampler2DArray - uint textureLayer; // layer index (always 0 for per-instance composites) - uint flags; // reserved -}; - -layout(std430, binding = 0) readonly buffer InstanceBuffer { - InstanceData Instances[]; -}; - -layout(std430, binding = 1) readonly buffer BatchBuffer { - BatchData Batches[]; -}; - -uniform mat4 uViewProjection; - -out vec3 vNormal; -out vec2 vTexCoord; -out flat uvec2 vTextureHandle; -out flat uint vTextureLayer; - -void main() { - int instanceIndex = gl_BaseInstanceARB + gl_InstanceID; - mat4 model = Instances[instanceIndex].transform; - - vec4 worldPos = model * vec4(aPosition, 1.0); - gl_Position = uViewProjection * worldPos; - - vNormal = normalize(mat3(model) * aNormal); - vTexCoord = aTexCoord; - - BatchData b = Batches[gl_DrawIDARB]; - vTextureHandle = b.textureHandle; - vTextureLayer = b.textureLayer; -} -``` - -- [ ] **Step 5.3: Write mesh_modern.frag β€” preserve existing lighting model** - -**AMENDED 2026-05-08:** original plan draft used hardcoded `uAmbient/uSunDir/uSunColor` uniforms. Reading the actual `src/AcDream.App/Rendering/Shaders/mesh_instanced.frag` revealed it uses a `SceneLighting` UBO at `binding=1` with 8 lights, fog params, and lightning flash. The N.5 shader must preserve this lighting machinery to maintain visual identity to N.4. - -The vert outputs need to ADD `vWorldPos` (used by `accumulateLights` and `applyFog`). Update the vert from Step 5.2 to also emit `out vec3 vWorldPos;` and `vWorldPos = worldPos.xyz;` in main. - -Create `src/AcDream.App/Rendering/Shaders/mesh_modern.frag` with the same lighting UBO + functions as `mesh_instanced.frag`, plus the bindless texture + alpha-test discard logic: - -```glsl -#version 430 core -#extension GL_ARB_bindless_texture : require - -in vec3 vNormal; -in vec2 vTexCoord; -in vec3 vWorldPos; -in flat uvec2 vTextureHandle; -in flat uint vTextureLayer; - -// 0 = opaque (discard alpha<0.95), 1 = transparent (discard alpha>=0.95) -uniform int uRenderPass; - -// SceneLighting UBO β€” IDENTICAL layout to mesh_instanced.frag binding=1. -struct Light { - vec4 posAndKind; - vec4 dirAndRange; - vec4 colorAndIntensity; - vec4 coneAngleEtc; -}; -layout(std140, binding = 1) uniform SceneLighting { - Light uLights[8]; - vec4 uCellAmbient; - vec4 uFogParams; - vec4 uFogColor; - vec4 uCameraAndTime; -}; - -vec3 accumulateLights(vec3 N, vec3 worldPos) { - vec3 lit = uCellAmbient.xyz; - int activeLights = int(uCellAmbient.w); - for (int i = 0; i < 8; ++i) { - if (i >= activeLights) break; - int kind = int(uLights[i].posAndKind.w); - vec3 Lcol = uLights[i].colorAndIntensity.xyz * uLights[i].colorAndIntensity.w; - if (kind == 0) { - vec3 Ldir = -uLights[i].dirAndRange.xyz; - float ndl = max(0.0, dot(N, Ldir)); - lit += Lcol * ndl; - } else { - vec3 toL = uLights[i].posAndKind.xyz - worldPos; - float d = length(toL); - float range = uLights[i].dirAndRange.w; - if (d < range && range > 1e-3) { - vec3 Ldir = toL / max(d, 1e-4); - float ndl = max(0.0, dot(N, Ldir)); - float atten = 1.0; - if (kind == 2) { - float cos_edge = cos(uLights[i].coneAngleEtc.x * 0.5); - float cos_l = dot(-Ldir, uLights[i].dirAndRange.xyz); - atten *= (cos_l > cos_edge) ? 1.0 : 0.0; - } - lit += Lcol * ndl * atten; - } - } - } - return lit; -} - -vec3 applyFog(vec3 lit, vec3 worldPos) { - int mode = int(uFogParams.w); - if (mode == 0) return lit; - float d = length(worldPos - uCameraAndTime.xyz); - float fogStart = uFogParams.x; - float fogEnd = uFogParams.y; - float span = max(1e-3, fogEnd - fogStart); - float fog = clamp((d - fogStart) / span, 0.0, 1.0); - return mix(lit, uFogColor.xyz, fog); -} - -out vec4 FragColor; - -void main() { - sampler2DArray tex = sampler2DArray(vTextureHandle); - vec4 color = texture(tex, vec3(vTexCoord, float(vTextureLayer))); - - // Two-pass alpha-test (N.5 Decision 2 β€” replaces mesh_instanced's - // uTranslucencyKind=1 ClipMap-only discard with a more aggressive - // pattern that also handles AlphaBlend correctly via two passes). - if (uRenderPass == 0) { - if (color.a < 0.95) discard; // opaque pass - } else { - if (color.a >= 0.95) discard; // transparent pass - if (color.a < 0.05) discard; // skip totally-empty - } - - vec3 N = normalize(vNormal); - vec3 lit = accumulateLights(N, vWorldPos); - - // Lightning flash β€” additive scene bump (matches mesh_instanced.frag). - lit += uFogParams.z * vec3(0.6, 0.6, 0.75); - - // Retail clamp per-channel to 1.0 (r13 Β§13.1). - lit = min(lit, vec3(1.0)); - - vec3 rgb = color.rgb * lit; - rgb = applyFog(rgb, vWorldPos); - FragColor = vec4(rgb, color.a); -} -``` - -- [ ] **Step 5.4: Update mesh_modern.vert to emit vWorldPos** - -Add `vWorldPos` output to the vert from Step 5.2. The full vert becomes: - -```glsl -#version 430 core -#extension GL_ARB_bindless_texture : require -#extension GL_ARB_shader_draw_parameters : require - -layout(location = 0) in vec3 aPosition; -layout(location = 1) in vec3 aNormal; -layout(location = 2) in vec2 aTexCoord; - -struct InstanceData { - mat4 transform; - // Reserved for Phase B.4 follow-up (selection-blink retail-faithful - // highlight): vec4 highlightColor; β€” extend stride here, increase the - // _instanceSsbo upload size in WbDrawDispatcher, add a flat varying out, - // and consume in mesh_modern.frag. -}; - -struct BatchData { - uvec2 textureHandle; // bindless handle for sampler2DArray - uint textureLayer; // layer index (always 0 for per-instance composites) - uint flags; // reserved -}; - -layout(std430, binding = 0) readonly buffer InstanceBuffer { - InstanceData Instances[]; -}; - -layout(std430, binding = 1) readonly buffer BatchBuffer { - BatchData Batches[]; -}; - -uniform mat4 uViewProjection; - -out vec3 vNormal; -out vec2 vTexCoord; -out vec3 vWorldPos; -out flat uvec2 vTextureHandle; -out flat uint vTextureLayer; - -void main() { - int instanceIndex = gl_BaseInstanceARB + gl_InstanceID; - mat4 model = Instances[instanceIndex].transform; - - vec4 worldPos = model * vec4(aPosition, 1.0); - gl_Position = uViewProjection * worldPos; - - vWorldPos = worldPos.xyz; - vNormal = normalize(mat3(model) * aNormal); - vTexCoord = aTexCoord; - - BatchData b = Batches[gl_DrawIDARB]; - vTextureHandle = b.textureHandle; - vTextureLayer = b.textureLayer; -} -``` - -(The vert from Step 5.2 should be REPLACED with this. The two are the same except for `vWorldPos` and a small comment cleanup.) - -- [ ] **Step 5.5: Build to verify shaders are copied to output** - -Run: `dotnet build src/AcDream.App/AcDream.App.csproj` -Expected: PASS. After build, check `src/AcDream.App/bin/Debug/net10.0/Rendering/Shaders/` contains `mesh_modern.vert` + `mesh_modern.frag`. - -- [ ] **Step 5.6: Commit** - -``` -phase(N.5) Task 5: mesh_modern.vert + .frag β€” bindless + SSBO + indirect - -New entity shaders modeled on WB's StaticObjectModern.* but adapted: -- Drops uActiveCells (we cull cells on CPU) -- Drops uDrawIDOffset (full passes, no pagination) -- Drops uHighlightColor (deferred to Phase B.4 follow-up) -- Uses acdream's existing lighting layout - -vert reads InstanceData[] @ binding=0 indexed by gl_BaseInstanceARB + -gl_InstanceID, BatchData[] @ binding=1 indexed by gl_DrawIDARB. -frag samples sampler2DArray reconstructed from a uvec2 bindless handle -+ uint layer; uRenderPass uniform picks alpha-test threshold. - -Not yet wired to the dispatcher β€” Task 7 swaps shader load, -Tasks 9-10 swap the draw loop. - -Co-Authored-By: Claude Opus 4.7 (1M context) -``` - ---- - -## Task 6: Wire mesh_modern shader load + capability check in GameWindow - -**Files:** -- Modify: `src/AcDream.App/Rendering/GameWindow.cs` - -- [ ] **Step 6.1: Read existing mesh_instanced load site** - -Read `src/AcDream.App/Rendering/GameWindow.cs:960-980` (around the `_meshShader = new Shader(...)` line). Note the surrounding context β€” the WB foundation flag check, how the dispatcher is constructed. - -- [ ] **Step 6.2: Add capability-gated mesh_modern load** - -Find this block: -```csharp -_meshShader = new Shader(_gl, - Path.Combine(shadersDir, "mesh_instanced.vert"), - Path.Combine(shadersDir, "mesh_instanced.frag")); -``` - -Replace with: -```csharp -// N.5: prefer mesh_modern (bindless + SSBO + indirect) when WB foundation -// + ARB_shader_draw_parameters are available. Falls back to legacy -// mesh_instanced if any capability is missing β€” same code path as -// ACDREAM_USE_WB_FOUNDATION=0. -bool wbFoundationOn = WbFoundationFlag.IsEnabled; -bool useModernShader = false; -if (wbFoundationOn && BindlessSupport.TryCreate(_gl, out var bindless) && bindless is not null) -{ - if (bindless.HasShaderDrawParameters(_gl)) - { - try - { - _meshShader = new Shader(_gl, - Path.Combine(shadersDir, "mesh_modern.vert"), - Path.Combine(shadersDir, "mesh_modern.frag")); - _bindlessSupport = bindless; - useModernShader = true; - Console.WriteLine("[N.5] mesh_modern shader loaded (bindless + ARB_shader_draw_parameters)"); - } - catch (Exception ex) - { - Console.WriteLine($"[N.5] mesh_modern compile failed, falling back: {ex.Message}"); - } - } - else - { - Console.WriteLine("[N.5] GL_ARB_shader_draw_parameters not present, using legacy shader"); - } -} -if (!useModernShader) -{ - _meshShader = new Shader(_gl, - Path.Combine(shadersDir, "mesh_instanced.vert"), - Path.Combine(shadersDir, "mesh_instanced.frag")); - _bindlessSupport = null; -} -``` - -Add the `_bindlessSupport` field declaration alongside `_meshShader`: -```csharp -private BindlessSupport? _bindlessSupport; -``` - -Also add `using AcDream.App.Rendering.Wb;` at the top of the file if not already there. - -- [ ] **Step 6.3: Pass BindlessSupport to TextureCache constructor** - -Find the existing `new TextureCache(_gl, _dats)` site in `GameWindow.cs`. Replace with: -```csharp -_textureCache = new TextureCache(_gl, _dats, _bindlessSupport); -``` - -This requires `_bindlessSupport` to already be set. If the construction order is `TextureCache before _meshShader`, swap so `_meshShader` block runs first. Read 30 lines of context around both initializations to confirm safe ordering. - -- [ ] **Step 6.4: Build + smoke test** - -Run: `dotnet build` -Expected: PASS. - -Run: `dotnet test --filter "FullyQualifiedName~Wb|FullyQualifiedName~MatrixComposition"` -Expected: 60+ tests PASS. - -Smoke launch (manual, optional at this point β€” modern shader loaded but dispatcher still uses legacy draw path so visual should be identical to N.4): -```powershell -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -dotnet run --project src\AcDream.App\AcDream.App.csproj --no-build -c Debug 2>&1 | Tee-Object -FilePath launch-task6.log -``` -Expected: launch logs show `[N.5] mesh_modern shader loaded` line. Visual is broken (modern shader is loaded but dispatcher's per-group draw loop hands it the wrong data layout) β€” this is fine, expected, and gets fixed in Tasks 7-10. - -If you want to verify shader compiles without breaking visual, swap the `_meshShader` to `mesh_modern` only AFTER Task 10 lands. - -**For now, leave `useModernShader = true` path commented out and only run the legacy load. Tasks 9-10 flip it on.** Update the block: - -```csharp -if (wbFoundationOn && BindlessSupport.TryCreate(_gl, out var bindless) && bindless is not null) -{ - if (bindless.HasShaderDrawParameters(_gl)) - { - // Capability detected β€” store the support for later tasks. - // Shader swap happens in Task 10 once dispatcher is ready. - _bindlessSupport = bindless; - Console.WriteLine("[N.5] modern path capabilities present (bindless + ARB_shader_draw_parameters)"); - } -} -// Legacy shader load happens unconditionally for Task 6: -_meshShader = new Shader(_gl, - Path.Combine(shadersDir, "mesh_instanced.vert"), - Path.Combine(shadersDir, "mesh_instanced.frag")); -``` - -Task 10 will switch the shader load. Task 6 just plumbs `_bindlessSupport` so Task 7+ can use it. - -- [ ] **Step 6.5: Commit** - -``` -phase(N.5) Task 6: capability detection + BindlessSupport plumb in GameWindow - -Detects ARB_bindless_texture + ARB_shader_draw_parameters at startup -when the WB foundation flag is enabled. Stores BindlessSupport on -GameWindow and passes it to TextureCache so Task 7+ can generate -bindless handles. Mesh shader load remains mesh_instanced for now β€” -Task 10 swaps to mesh_modern after the dispatcher is rewired. - -Co-Authored-By: Claude Opus 4.7 (1M context) -``` - ---- - -## Task 7: Add SSBO + indirect buffer infrastructure to WbDrawDispatcher - -**Files:** -- Modify: `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` -- Create: `src/AcDream.App/Rendering/Wb/DrawElementsIndirectCommand.cs` - -- [ ] **Step 7.1: Create DrawElementsIndirectCommand struct** - -Create `src/AcDream.App/Rendering/Wb/DrawElementsIndirectCommand.cs`: - -```csharp -using System.Runtime.InteropServices; - -namespace AcDream.App.Rendering.Wb; - -/// -/// Layout matches what glMultiDrawElementsIndirect expects. -/// Total size 20 bytes; arrays are typically uploaded with stride = sizeof(this). -/// -[StructLayout(LayoutKind.Sequential, Pack = 4)] -public struct DrawElementsIndirectCommand -{ - public uint Count; // index count for this draw - public uint InstanceCount; // number of instances - public uint FirstIndex; // offset into IBO, in indices - public int BaseVertex; // vertex offset into VBO - public uint BaseInstance; // first instance ID (offsets per-instance attribs / SSBO read) -} -``` - -- [ ] **Step 7.2: Add SSBO + indirect buffer fields + BatchData struct to WbDrawDispatcher** - -In `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs`, add at the top of the class (replacing the existing `_instanceVbo` field): - -```csharp -private readonly BindlessSupport _bindless; - -// SSBO buffer ids -private uint _instanceSsbo; -private uint _batchSsbo; -private uint _indirectBuffer; - -// Per-frame scratch arrays -private float[] _instanceData = new float[256 * 16]; // mat4 floats per instance -private BatchData[] _batchData = new BatchData[256]; -private DrawElementsIndirectCommand[] _indirectCommands = new DrawElementsIndirectCommand[256]; - -private int _opaqueDrawCount; -private int _transparentDrawCount; -private int _transparentByteOffset; - -[StructLayout(LayoutKind.Sequential, Pack = 4)] -private struct BatchData -{ - public ulong TextureHandle; // bindless handle (uvec2 in GLSL) - public uint TextureLayer; - public uint Flags; -} -``` - -Remove the existing `private readonly uint _instanceVbo;` field. - -- [ ] **Step 7.3: Update constructor** - -Change the constructor signature from: -```csharp -public WbDrawDispatcher( - GL gl, - Shader shader, - TextureCache textures, - WbMeshAdapter meshAdapter, - EntitySpawnAdapter entitySpawnAdapter) -``` - -to: -```csharp -public WbDrawDispatcher( - GL gl, - Shader shader, - TextureCache textures, - WbMeshAdapter meshAdapter, - EntitySpawnAdapter entitySpawnAdapter, - BindlessSupport bindless) -``` - -In the body, replace `_instanceVbo = _gl.GenBuffer();` with: -```csharp -_bindless = bindless ?? throw new ArgumentNullException(nameof(bindless)); -_instanceSsbo = _gl.GenBuffer(); -_batchSsbo = _gl.GenBuffer(); -_indirectBuffer = _gl.GenBuffer(); -``` - -- [ ] **Step 7.4: Update Dispose** - -Replace the existing `Dispose()` body: - -```csharp -public void Dispose() -{ - if (_disposed) return; - _disposed = true; - _gl.DeleteBuffer(_instanceSsbo); - _gl.DeleteBuffer(_batchSsbo); - _gl.DeleteBuffer(_indirectBuffer); -} -``` - -- [ ] **Step 7.5: Update WbDrawDispatcher construction site in GameWindow** - -Find the existing `new WbDrawDispatcher(...)` call in `GameWindow.cs` and add the `_bindlessSupport!` argument (the `!` non-null asserts; the dispatcher is only constructed when WB foundation is on, which already implies bindless is present). - -- [ ] **Step 7.6: Build + tests** - -Run: `dotnet build` -Expected: PASS. - -Run: `dotnet test --filter "FullyQualifiedName~Wb"` -Expected: PASS (existing tests don't exercise the changed buffer plumbing yet β€” we removed `_instanceVbo` but we'll restore the draw path in Task 9). - -If `WbDrawDispatcher.Draw` references `_instanceVbo`, those references break. Comment out the body of `Draw()` temporarily β€” it'll be rewritten in Tasks 9-10. Wrap with `// TASK 9-10: rewriting`. Build must still pass. - -Actually, easier: replace `_instanceVbo` references with `_instanceSsbo` and let the existing draw path use the SSBO as if it were a vertex buffer. The legacy draw will be functionally broken but compile. Visual will break but only after we flip the shader in Task 10. For the scope of Tasks 7-9 we want the build to compile. - -The cleanest pattern: leave the existing `Draw()` method untouched except for substituting `_instanceVbo` β†’ `_instanceSsbo`. The behavior is wrong but compiles, and Tasks 9-10 fully rewrite it. - -- [ ] **Step 7.7: Commit** - -``` -phase(N.5) Task 7: dispatcher SSBO + indirect buffer infrastructure - -Adds DrawElementsIndirectCommand struct (20-byte layout for -glMultiDrawElementsIndirect). Replaces _instanceVbo field on -WbDrawDispatcher with three buffers: _instanceSsbo (mat4[]), -_batchSsbo (BatchData[]), _indirectBuffer (DEIC[]). Adds BindlessSupport -constructor parameter β€” non-null required since the dispatcher is only -constructed when WB foundation is on. - -Existing Draw() method substitutes _instanceVbo β†’ _instanceSsbo for -compile. Behavior temporarily wrong; Tasks 9-10 fully rewrite the -draw loop. - -Co-Authored-By: Claude Opus 4.7 (1M context) -``` - ---- - -## Task 8: Update InstanceGroup + GroupKey for bindless handles - -**Files:** -- Modify: `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` - -- [ ] **Step 8.1: Update InstanceGroup** - -In `WbDrawDispatcher.cs`, replace the existing `InstanceGroup` class with: - -```csharp -private sealed class InstanceGroup -{ - public uint Ibo; - public uint FirstIndex; - public int BaseVertex; - public int IndexCount; - public ulong BindlessTextureHandle; // 64-bit (was uint TextureHandle in N.4) - public uint TextureLayer; // 0 for per-instance composites - public TranslucencyKind Translucency; - public int FirstInstance; - public int InstanceCount; - public float SortDistance; - public readonly List Matrices = new(); -} -``` - -- [ ] **Step 8.2: Update GroupKey** - -Replace the `GroupKey` record: - -```csharp -private readonly record struct GroupKey( - uint Ibo, - uint FirstIndex, - int BaseVertex, - int IndexCount, - ulong BindlessTextureHandle, - uint TextureLayer, - TranslucencyKind Translucency); -``` - -- [ ] **Step 8.3: Update ResolveTexture method** - -Replace the existing `ResolveTexture` method (returns `uint`) with: - -```csharp -private ulong ResolveTexture(WorldEntity entity, MeshRef meshRef, ObjectRenderBatch batch, ulong palHash) -{ - uint surfaceId = batch.Key.SurfaceId; - if (surfaceId == 0 || surfaceId == 0xFFFFFFFF) return 0; - - uint overrideOrigTex = 0; - bool hasOrigTexOverride = meshRef.SurfaceOverrides is not null - && meshRef.SurfaceOverrides.TryGetValue(surfaceId, out overrideOrigTex); - uint? origTexOverride = hasOrigTexOverride ? overrideOrigTex : (uint?)null; - - if (entity.PaletteOverride is not null) - { - return _textures.GetOrUploadWithPaletteOverrideBindless( - surfaceId, origTexOverride, entity.PaletteOverride, palHash); - } - else if (hasOrigTexOverride) - { - return _textures.GetOrUploadWithOrigTextureOverrideBindless(surfaceId, overrideOrigTex); - } - else - { - return _textures.GetOrUploadBindless(surfaceId); - } -} -``` - -- [ ] **Step 8.4: Update ClassifyBatches to use the new return type** - -Replace the existing `ClassifyBatches` to use `ulong texHandle` and pass the layer: - -```csharp -private void ClassifyBatches( - ObjectRenderData renderData, - ulong gfxObjId, - Matrix4x4 model, - WorldEntity entity, - MeshRef meshRef, - ulong palHash, - AcSurfaceMetadataTable metaTable) -{ - for (int batchIdx = 0; batchIdx < renderData.Batches.Count; batchIdx++) - { - var batch = renderData.Batches[batchIdx]; - - TranslucencyKind translucency; - if (metaTable.TryLookup(gfxObjId, batchIdx, out var meta)) - { - translucency = meta.Translucency; - } - else - { - translucency = batch.IsAdditive ? TranslucencyKind.Additive - : batch.IsTransparent ? TranslucencyKind.AlphaBlend - : TranslucencyKind.Opaque; - } - - ulong texHandle = ResolveTexture(entity, meshRef, batch, palHash); - if (texHandle == 0) continue; - - // For per-instance composites we use 1-layer Texture2DArray, layer always 0. - // When N.6 adopts WB's atlas, this becomes batch's layer index. - uint texLayer = 0; - - var key = new GroupKey( - batch.IBO, batch.FirstIndex, (int)batch.BaseVertex, - batch.IndexCount, texHandle, texLayer, translucency); - - if (!_groups.TryGetValue(key, out var grp)) - { - grp = new InstanceGroup - { - Ibo = batch.IBO, - FirstIndex = batch.FirstIndex, - BaseVertex = (int)batch.BaseVertex, - IndexCount = batch.IndexCount, - BindlessTextureHandle = texHandle, - TextureLayer = texLayer, - Translucency = translucency, - }; - _groups[key] = grp; - } - grp.Matrices.Add(model); - } -} -``` - -- [ ] **Step 8.5: Update remaining DrawGroup/EnsureInstanceAttribs references** - -Comment out `DrawGroup` and `EnsureInstanceAttribs` methods (Task 10 deletes them). Also comment out their call sites in `Draw()`. Build will fail until Task 9-10 lands; that's expected. - -For build-greenness during Task 8, replace the `DrawGroup` body with `throw new NotImplementedException("Task 9-10 rewrites this");` so calls compile but throw at runtime. Visual will be broken until Task 10. That's expected. - -Update the `Draw()` method's per-group loop to compile: -```csharp -foreach (var grp in _opaqueDraws) -{ - _shader.SetInt("uTranslucencyKind", (int)grp.Translucency); - DrawGroup(grp); // throws β€” Task 10 fixes -} -``` - -(The user does NOT visually verify at this task. Build green only.) - -- [ ] **Step 8.6: Build** - -Run: `dotnet build` -Expected: PASS. - -Run: `dotnet test --filter "FullyQualifiedName~Wb"` -Expected: existing tests PASS (they're CPU-only β€” they don't actually invoke `DrawGroup`). - -- [ ] **Step 8.7: Commit** - -``` -phase(N.5) Task 8: InstanceGroup + GroupKey carry bindless handle + layer - -Replaces uint TextureHandle (32-bit GL name) with ulong -BindlessTextureHandle (64-bit) in InstanceGroup + GroupKey + ResolveTexture -return type. Adds TextureLayer (always 0 for per-instance composites, -becomes meaningful when WB atlas is adopted in N.6). - -ClassifyBatches now calls TextureCache.GetOrUpload*Bindless variants. -DrawGroup body throws NotImplementedException β€” Task 9-10 rewrites -the draw loop. - -Co-Authored-By: Claude Opus 4.7 (1M context) -``` - ---- - -## Task 9: Build BatchData + DEIC arrays per frame (TDD) - -**Files:** -- Modify: `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` -- Create: `tests/AcDream.Core.Tests/Rendering/Wb/WbDrawDispatcherIndirectBuilderTests.cs` - -This task adds a pure CPU method `BuildIndirectArrays()` that the dispatcher will call before issuing draws. Unit-testable without GL context. - -- [ ] **Step 9.1: Write the failing test** - -Create `tests/AcDream.Core.Tests/Rendering/Wb/WbDrawDispatcherIndirectBuilderTests.cs`: - -```csharp -using System.Numerics; -using AcDream.App.Rendering.Wb; -using AcDream.Core.Meshing; -using Xunit; - -namespace AcDream.Core.Tests.Rendering.Wb; - -/// -/// Pure CPU test of . -/// Builds a synthetic group set and verifies the laid-out indirect commands -/// match the spec Β§5 walk-through. -/// -public sealed class WbDrawDispatcherIndirectBuilderTests -{ - [Fact] - public void TwoOpaqueGroupsAndOneTransparent_LaysOutContiguouslyOpaqueFirst() - { - // Arrange β€” synthetic groups laid out as in spec Β§5 - var groups = new List - { - new(IndexCount: 100, FirstIndex: 0, BaseVertex: 0, InstanceCount: 12, FirstInstance: 0, TextureHandle: 0xAA, TextureLayer: 0, Translucency: TranslucencyKind.Opaque), - new(IndexCount: 200, FirstIndex: 100, BaseVertex: 0, InstanceCount: 12, FirstInstance: 12, TextureHandle: 0xBB, TextureLayer: 0, Translucency: TranslucencyKind.AlphaBlend), - new(IndexCount: 50, FirstIndex: 300, BaseVertex: 100, InstanceCount: 1, FirstInstance: 24, TextureHandle: 0xCC, TextureLayer: 0, Translucency: TranslucencyKind.Opaque), - }; - - var indirect = new DrawElementsIndirectCommand[16]; - var batch = new WbDrawDispatcher.BatchDataPublic[16]; - - // Act - var result = WbDrawDispatcher.BuildIndirectArrays(groups, indirect, batch); - - // Assert layout - Assert.Equal(2, result.OpaqueCount); - Assert.Equal(1, result.TransparentCount); - Assert.Equal(2 * 20, result.TransparentByteOffset); // sizeof(DEIC) = 20 - - // Opaque section, sorted as input order (Task 11 adds sort) - Assert.Equal(100u, indirect[0].Count); - Assert.Equal(0u, indirect[0].FirstIndex); - Assert.Equal(0, indirect[0].BaseVertex); - Assert.Equal(12u, indirect[0].InstanceCount); - Assert.Equal(0u, indirect[0].BaseInstance); - - Assert.Equal(50u, indirect[1].Count); - Assert.Equal(300u, indirect[1].FirstIndex); - Assert.Equal(100, indirect[1].BaseVertex); - Assert.Equal(1u, indirect[1].InstanceCount); - Assert.Equal(24u, indirect[1].BaseInstance); - - // Transparent section - Assert.Equal(200u, indirect[2].Count); - Assert.Equal(100u, indirect[2].FirstIndex); - Assert.Equal(12u, indirect[2].InstanceCount); - Assert.Equal(12u, indirect[2].BaseInstance); - - // BatchData parallel - Assert.Equal(0xAAul, batch[0].TextureHandle); - Assert.Equal(0xCCul, batch[1].TextureHandle); - Assert.Equal(0xBBul, batch[2].TextureHandle); - } - - [Fact] - public void EmptyGroupList_ProducesZeroCounts() - { - var groups = new List(); - var indirect = new DrawElementsIndirectCommand[0]; - var batch = new WbDrawDispatcher.BatchDataPublic[0]; - - var result = WbDrawDispatcher.BuildIndirectArrays(groups, indirect, batch); - - Assert.Equal(0, result.OpaqueCount); - Assert.Equal(0, result.TransparentCount); - Assert.Equal(0, result.TransparentByteOffset); - } -} -``` - -- [ ] **Step 9.2: Run, verify it fails** - -Run: `dotnet test --filter "FullyQualifiedName~WbDrawDispatcherIndirectBuilder"` -Expected: COMPILE FAIL β€” `BuildIndirectArrays` and supporting public types don't exist. - -- [ ] **Step 9.3: Implement BuildIndirectArrays + supporting types** - -In `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs`, add public helper types + static method (above the private `InstanceGroup` class): - -```csharp -/// Public view of the per-group inputs to β€” used in tests. -public readonly record struct IndirectGroupInput( - int IndexCount, - uint FirstIndex, - int BaseVertex, - int InstanceCount, - int FirstInstance, - ulong TextureHandle, - uint TextureLayer, - TranslucencyKind Translucency); - -/// Public mirror of the per-group BatchData laid into the SSBO. Tests verify alignment. -// Pack=8 (not 4) β€” must stay layout-identical to private BatchData for Task 10's MemoryMarshal.Cast. -[StructLayout(LayoutKind.Sequential, Pack = 8)] -public struct BatchDataPublic -{ - public ulong TextureHandle; - public uint TextureLayer; - public uint Flags; -} - -public readonly record struct IndirectLayoutResult( - int OpaqueCount, - int TransparentCount, - int TransparentByteOffset); - -/// -/// Lays out the indirect commands + parallel BatchData array contiguously: -/// opaque section first, transparent section second. Pure CPU, no GL state. -/// Caller passes scratch arrays (pre-sized). -/// -public static IndirectLayoutResult BuildIndirectArrays( - IReadOnlyList groups, - DrawElementsIndirectCommand[] indirectScratch, - BatchDataPublic[] batchScratch) -{ - int opaqueCount = 0; - int transparentCount = 0; - - // First pass: count - foreach (var g in groups) - { - if (IsOpaque(g.Translucency)) opaqueCount++; - else transparentCount++; - } - - // Second pass: lay out β€” opaque [0..opaqueCount), transparent [opaqueCount..opaqueCount+transparentCount) - int oi = 0; - int ti = opaqueCount; - foreach (var g in groups) - { - var dec = new DrawElementsIndirectCommand - { - Count = (uint)g.IndexCount, - InstanceCount = (uint)g.InstanceCount, - FirstIndex = g.FirstIndex, - BaseVertex = g.BaseVertex, - BaseInstance = (uint)g.FirstInstance, - }; - var bd = new BatchDataPublic - { - TextureHandle = g.TextureHandle, - TextureLayer = g.TextureLayer, - Flags = 0, - }; - - if (IsOpaque(g.Translucency)) - { - indirectScratch[oi] = dec; - batchScratch[oi] = bd; - oi++; - } - else - { - indirectScratch[ti] = dec; - batchScratch[ti] = bd; - ti++; - } - } - - return new IndirectLayoutResult(opaqueCount, transparentCount, opaqueCount * DrawCommandStride); -} - -private static bool IsOpaque(TranslucencyKind t) - => t == TranslucencyKind.Opaque || t == TranslucencyKind.ClipMap; -``` - -- [ ] **Step 9.4: Run test, verify pass** - -Run: `dotnet test --filter "FullyQualifiedName~WbDrawDispatcherIndirectBuilder"` -Expected: PASS (2 tests). - -Run full filter: `dotnet test --filter "FullyQualifiedName~Wb|FullyQualifiedName~MatrixComposition"` -Expected: 60+ existing tests + 2 new = PASS. - -- [ ] **Step 9.5: Commit** - -``` -phase(N.5) Task 9: BuildIndirectArrays β€” CPU layout for indirect dispatch - -Pure CPU helper that lays out a group list into a contiguous indirect -buffer (DrawElementsIndirectCommand[]) and parallel BatchData[] β€” -opaque section first, transparent section second. Returns counts + -byte offset for the transparent section. - -Tests cover the spec Β§5 walk-through layout: per-group fields propagate -correctly, opaque/transparent partition lands at the expected indices. - -Static + public so tests can exercise without a GL context. Tasks -10-11 wire it into Draw(). - -Co-Authored-By: Claude Opus 4.7 (1M context) -``` - ---- - -## Task 10: Replace draw loop with glMultiDrawElementsIndirect (visual verification) - -**Files:** -- Modify: `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` -- Modify: `src/AcDream.App/Rendering/GameWindow.cs` - -This is the load-bearing task. After this lands, visual verification is required. - -- [ ] **Step 10.1: Rewrite WbDrawDispatcher.Draw** - -Replace the entire `Draw()` method body in `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs`. The phase 1-3 (entity walk, group bucketing, matrix layout) stay; phases 4-6 are rewritten: - -```csharp -public unsafe void Draw( - ICamera camera, - IEnumerable<(uint LandblockId, Vector3 AabbMin, Vector3 AabbMax, IReadOnlyList Entities)> landblockEntries, - FrustumPlanes? frustum = null, - uint? neverCullLandblockId = null, - HashSet? visibleCellIds = null, - HashSet? animatedEntityIds = null) -{ - _shader.Use(); - var vp = camera.View * camera.Projection; - _shader.SetMatrix4("uViewProjection", vp); - - // Lighting uniforms β€” match what mesh_modern.frag declares (Task 5.3). - // Read the existing N.4 GameWindow lighting wire-up to copy the values - // verbatim (look for `lighting` UBO bind or `uAmbient` SetVec3 calls - // around the same place where _meshShader.Use() / SetMatrix4 happens). - // If N.4 used a UBO: change mesh_modern.frag in Task 5.3 to match the UBO, - // then bind the UBO here via `_gl.BindBufferBase(UniformBuffer, 1, lightingUbo)`. - // If N.4 used uniforms: replicate the same SetVec3 calls here. - - bool diag = string.Equals(Environment.GetEnvironmentVariable("ACDREAM_WB_DIAG"), "1", StringComparison.Ordinal); - - Vector3 camPos = Vector3.Zero; - if (Matrix4x4.Invert(camera.View, out var invView)) - camPos = invView.Translation; - - // ── Phases 1-2: walk entities, build groups, lay matrices ─────────── - foreach (var grp in _groups.Values) grp.Matrices.Clear(); - var metaTable = _meshAdapter.MetadataTable; - uint anyVao = 0; - - foreach (var entry in landblockEntries) - { - bool landblockVisible = frustum is null - || entry.LandblockId == neverCullLandblockId - || FrustumCuller.IsAabbVisible(frustum.Value, entry.AabbMin, entry.AabbMax); - if (!landblockVisible && (animatedEntityIds is null || animatedEntityIds.Count == 0)) - continue; - - foreach (var entity in entry.Entities) - { - if (entity.MeshRefs.Count == 0) continue; - - bool isAnimated = animatedEntityIds?.Contains(entity.Id) == true; - if (!landblockVisible && !isAnimated) continue; - if (entity.ParentCellId.HasValue && visibleCellIds is not null - && !visibleCellIds.Contains(entity.ParentCellId.Value)) - continue; - - if (frustum is not null && !isAnimated && entry.LandblockId != neverCullLandblockId) - { - var p = entity.Position; - var aMin = new Vector3(p.X - PerEntityCullRadius, p.Y - PerEntityCullRadius, p.Z - PerEntityCullRadius); - var aMax = new Vector3(p.X + PerEntityCullRadius, p.Y + PerEntityCullRadius, p.Z + PerEntityCullRadius); - if (!FrustumCuller.IsAabbVisible(frustum.Value, aMin, aMax)) - continue; - } - - if (diag) _entitiesSeen++; - - var entityWorld = - Matrix4x4.CreateFromQuaternion(entity.Rotation) * - Matrix4x4.CreateTranslation(entity.Position); - - ulong palHash = 0; - if (entity.PaletteOverride is not null) - palHash = TextureCache.HashPaletteOverride(entity.PaletteOverride); - - bool drewAny = false; - for (int partIdx = 0; partIdx < entity.MeshRefs.Count; partIdx++) - { - var meshRef = entity.MeshRefs[partIdx]; - ulong gfxObjId = meshRef.GfxObjId; - var renderData = _meshAdapter.TryGetRenderData(gfxObjId); - if (renderData is null) { if (diag) _meshesMissing++; continue; } - drewAny = true; - if (anyVao == 0) anyVao = renderData.VAO; - - if (renderData.IsSetup && renderData.SetupParts.Count > 0) - { - foreach (var (partGfxObjId, partTransform) in renderData.SetupParts) - { - var partData = _meshAdapter.TryGetRenderData(partGfxObjId); - if (partData is null) continue; - var model = ComposePartWorldMatrix(entityWorld, meshRef.PartTransform, partTransform); - ClassifyBatches(partData, partGfxObjId, model, entity, meshRef, palHash, metaTable); - } - } - else - { - var model = meshRef.PartTransform * entityWorld; - ClassifyBatches(renderData, gfxObjId, model, entity, meshRef, palHash, metaTable); - } - } - - if (diag && drewAny) _entitiesDrawn++; - } - } - - if (anyVao == 0) { if (diag) MaybeFlushDiag(); return; } - - int totalInstances = 0; - foreach (var grp in _groups.Values) totalInstances += grp.Matrices.Count; - if (totalInstances == 0) { if (diag) MaybeFlushDiag(); return; } - - // ── Phase 3: assign FirstInstance per group, lay matrices contiguous ─ - int needed = totalInstances * 16; - if (_instanceData.Length < needed) - _instanceData = new float[needed + 256 * 16]; - - _opaqueDraws.Clear(); - _translucentDraws.Clear(); - int cursor = 0; - foreach (var grp in _groups.Values) - { - if (grp.Matrices.Count == 0) continue; - grp.FirstInstance = cursor; - grp.InstanceCount = grp.Matrices.Count; - var first = grp.Matrices[0]; - var grpPos = new Vector3(first.M41, first.M42, first.M43); - grp.SortDistance = Vector3.DistanceSquared(camPos, grpPos); - - for (int i = 0; i < grp.Matrices.Count; i++) - { - WriteMatrix(_instanceData, cursor * 16, grp.Matrices[i]); - cursor++; - } - - if (IsOpaqueGroup(grp.Translucency)) - _opaqueDraws.Add(grp); - else - _translucentDraws.Add(grp); - } - _opaqueDraws.Sort(static (a, b) => a.SortDistance.CompareTo(b.SortDistance)); - - // ── Phase 4: build BatchData + DEIC arrays ────────────────────────── - int totalDraws = _opaqueDraws.Count + _translucentDraws.Count; - if (_batchData.Length < totalDraws) - _batchData = new BatchData[totalDraws + 64]; - if (_indirectCommands.Length < totalDraws) - _indirectCommands = new DrawElementsIndirectCommand[totalDraws + 64]; - - var groupInputs = new List(totalDraws); - foreach (var g in _opaqueDraws) groupInputs.Add(ToInput(g)); - foreach (var g in _translucentDraws) groupInputs.Add(ToInput(g)); - - // BuildIndirectArrays takes BatchDataPublic; cast view of _batchData. - // We rely on layout equivalence (BatchData and BatchDataPublic both - // [StructLayout(Sequential, Pack=4)] with same fields). - var batchView = MemoryMarshal.Cast(_batchData); - var layout = BuildIndirectArrays(groupInputs, _indirectCommands, batchView.ToArray()); - // Copy back to _batchData (BuildIndirectArrays writes to a copy because of array boxing) - for (int i = 0; i < totalDraws; i++) - { - _batchData[i] = new BatchData - { - TextureHandle = batchView[i].TextureHandle, - TextureLayer = batchView[i].TextureLayer, - Flags = batchView[i].Flags, - }; - } - _opaqueDrawCount = layout.OpaqueCount; - _transparentDrawCount = layout.TransparentCount; - _transparentByteOffset = layout.TransparentByteOffset; - - // ── Phase 5: upload three buffers ─────────────────────────────────── - fixed (float* ip = _instanceData) - UploadSsbo(_instanceSsbo, 0, ip, totalInstances * 16 * sizeof(float)); - fixed (BatchData* bp = _batchData) - UploadSsbo(_batchSsbo, 1, bp, totalDraws * sizeof(BatchData)); - fixed (DrawElementsIndirectCommand* cp = _indirectCommands) - { - _gl.BindBuffer(BufferTargetARB.DrawIndirectBuffer, _indirectBuffer); - _gl.BufferData(BufferTargetARB.DrawIndirectBuffer, - (nuint)(totalDraws * sizeof(DrawElementsIndirectCommand)), cp, BufferUsageARB.DynamicDraw); - } - - // ── Phase 6: bind global VAO once ─────────────────────────────────── - _gl.BindVertexArray(anyVao); - - if (string.Equals(Environment.GetEnvironmentVariable("ACDREAM_NO_CULL"), "1", StringComparison.Ordinal)) - _gl.Disable(EnableCap.CullFace); - - // ── Phase 7: opaque pass ─────────────────────────────────────────── - if (_opaqueDrawCount > 0) - { - _gl.Disable(EnableCap.Blend); - _gl.DepthMask(true); - _shader.SetInt("uRenderPass", 0); - _gl.BindBuffer(BufferTargetARB.DrawIndirectBuffer, _indirectBuffer); - _gl.MultiDrawElementsIndirect( - PrimitiveType.Triangles, - DrawElementsType.UnsignedShort, - indirect: (void*)0, - drawcount: (uint)_opaqueDrawCount, - stride: (uint)sizeof(DrawElementsIndirectCommand)); - } - - // ── Phase 8: transparent pass ────────────────────────────────────── - if (_transparentDrawCount > 0) - { - _gl.Enable(EnableCap.Blend); - _gl.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha); - _gl.DepthMask(false); - _shader.SetInt("uRenderPass", 1); - _gl.MultiDrawElementsIndirect( - PrimitiveType.Triangles, - DrawElementsType.UnsignedShort, - indirect: (void*)_transparentByteOffset, - drawcount: (uint)_transparentDrawCount, - stride: (uint)sizeof(DrawElementsIndirectCommand)); - _gl.DepthMask(true); - _gl.Disable(EnableCap.Blend); - } - - _gl.Disable(EnableCap.CullFace); - _gl.BindVertexArray(0); - - if (diag) - { - _drawsIssued += _opaqueDrawCount + _transparentDrawCount; - _instancesIssued += totalInstances; - MaybeFlushDiag(); - } -} - -private static bool IsOpaqueGroup(TranslucencyKind t) - => t == TranslucencyKind.Opaque || t == TranslucencyKind.ClipMap; - -private static IndirectGroupInput ToInput(InstanceGroup g) => new( - IndexCount: g.IndexCount, - FirstIndex: g.FirstIndex, - BaseVertex: g.BaseVertex, - InstanceCount: g.InstanceCount, - FirstInstance: g.FirstInstance, - TextureHandle: g.BindlessTextureHandle, - TextureLayer: g.TextureLayer, - Translucency: g.Translucency); - -private unsafe void UploadSsbo(uint ssbo, uint binding, void* data, int byteCount) -{ - _gl.BindBuffer(BufferTargetARB.ShaderStorageBuffer, ssbo); - _gl.BufferData(BufferTargetARB.ShaderStorageBuffer, (nuint)byteCount, data, BufferUsageARB.DynamicDraw); - _gl.BindBufferBase(BufferTargetARB.ShaderStorageBuffer, binding, ssbo); -} -``` - -Delete the old `DrawGroup`, `EnsureInstanceAttribs`, and `ResolveTexture` (the old uint-returning version) methods β€” they're no longer called. - -- [ ] **Step 10.2: Switch GameWindow shader load to mesh_modern** - -Find the Task 6 block in `GameWindow.cs` and change the shader load from `mesh_instanced` to `mesh_modern` when `_bindlessSupport != null`: - -```csharp -if (_bindlessSupport is not null) -{ - _meshShader = new Shader(_gl, - Path.Combine(shadersDir, "mesh_modern.vert"), - Path.Combine(shadersDir, "mesh_modern.frag")); - Console.WriteLine("[N.5] mesh_modern shader loaded"); -} -else -{ - _meshShader = new Shader(_gl, - Path.Combine(shadersDir, "mesh_instanced.vert"), - Path.Combine(shadersDir, "mesh_instanced.frag")); -} -``` - -- [ ] **Step 10.3: Build + run all tests** - -Run: `dotnet build` -Expected: PASS. - -Run: `dotnet test --filter "FullyQualifiedName~Wb|FullyQualifiedName~MatrixComposition"` -Expected: 60+ tests + 2 new BuildIndirectArrays tests PASS. - -- [ ] **Step 10.4: Visual smoke test (USER GATE)** - -Launch: -```powershell -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -$env:ACDREAM_TEST_HOST = "127.0.0.1" -$env:ACDREAM_TEST_PORT = "9000" -$env:ACDREAM_TEST_USER = "testaccount" -$env:ACDREAM_TEST_PASS = "testpassword" -$env:ACDREAM_WB_DIAG = "1" -dotnet run --project src\AcDream.App\AcDream.App.csproj --no-build -c Debug 2>&1 | Tee-Object -FilePath launch-task10.log -``` - -Expected: -- Console shows `[N.5] mesh_modern shader loaded`. -- Holtburg renders with characters + scenery + buildings visible. -- `[WB-DIAG]` shows draws dropping from N.4's hundreds to ~3-5 per frame for entity rendering. - -User confirms visual identity. If broken, debug β€” most likely failure modes: -1. Shader compile failure β†’ console log will show GLSL info log; fix vert/frag. -2. Black textures everywhere β†’ bindless handle generation broken; check `_bindless` is non-null in TextureCache. -3. Wrong geometry β†’ BaseVertex / FirstIndex misaligned; verify against N.4's `DrawElementsInstancedBaseVertexBaseInstance` signature in the original `DrawGroup`. -4. Wrong matrices on entities β†’ InstanceSsbo upload size wrong; verify `totalInstances * 16 * sizeof(float)`. - -- [ ] **Step 10.5: Commit only after visual verification passes** - -``` -phase(N.5) Task 10: glMultiDrawElementsIndirect dispatch β€” visual verified - -Replaces WbDrawDispatcher's per-group glDrawElementsInstancedBaseVertexBaseInstance -loop with two glMultiDrawElementsIndirect calls (opaque + transparent). -Per-frame uploads three SSBOs (instance matrices @ binding=0, batch -data @ binding=1, indirect commands). - -Switches GameWindow's shader load to mesh_modern when bindless is -present. - -Visual verification: Holtburg courtyard renders identical to N.4. -Entity draw calls drop from "few hundred per pass" to 1 per pass. - -Co-Authored-By: Claude Opus 4.7 (1M context) -``` - ---- - -## Task 11: Update ClassifyBatches for translucency restructure (TDD) - -**Files:** -- Modify: `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` -- Create: `tests/AcDream.Core.Tests/Rendering/Wb/WbDrawDispatcherTranslucencyTests.cs` - -Per Decision 2: `Additive` and `InvAlpha` merge into transparent (alpha-blend). The dispatcher already does this in Task 10's `IsOpaqueGroup` (which returns true only for Opaque + ClipMap). This task ADDS a unit test and tightens the contract. - -- [ ] **Step 11.1: Write the failing test** - -Create `tests/AcDream.Core.Tests/Rendering/Wb/WbDrawDispatcherTranslucencyTests.cs`: - -```csharp -using AcDream.App.Rendering.Wb; -using AcDream.Core.Meshing; -using Xunit; - -namespace AcDream.Core.Tests.Rendering.Wb; - -/// -/// Locks in the N.5 translucency partition contract (Decision 2): -/// Opaque + ClipMap β†’ opaque indirect; AlphaBlend + Additive + InvAlpha β†’ transparent. -/// -public sealed class WbDrawDispatcherTranslucencyTests -{ - [Theory] - [InlineData(TranslucencyKind.Opaque, true)] - [InlineData(TranslucencyKind.ClipMap, true)] - [InlineData(TranslucencyKind.AlphaBlend, false)] - [InlineData(TranslucencyKind.Additive, false)] - [InlineData(TranslucencyKind.InvAlpha, false)] - public void IsOpaque_PartitionsByKind(TranslucencyKind kind, bool expected) - { - Assert.Equal(expected, WbDrawDispatcher.IsOpaquePublic(kind)); - } -} -``` - -- [ ] **Step 11.2: Add IsOpaquePublic to WbDrawDispatcher** - -Make `IsOpaqueGroup` public (or add a `public static bool IsOpaquePublic(TranslucencyKind t) => IsOpaqueGroup(t);` shim): - -```csharp -public static bool IsOpaquePublic(TranslucencyKind t) => IsOpaqueGroup(t); -``` - -- [ ] **Step 11.3: Run test, verify PASS** - -Run: `dotnet test --filter "FullyQualifiedName~WbDrawDispatcherTranslucency"` -Expected: 5 tests PASS. - -Run all: `dotnet test --filter "FullyQualifiedName~Wb|FullyQualifiedName~MatrixComposition"` -Expected: 60+ + 2 + 5 = 67+ PASS. - -- [ ] **Step 11.4: Commit** - -``` -phase(N.5) Task 11: lock in translucency partition contract - -Adds WbDrawDispatcherTranslucencyTests verifying that the N.5 dispatcher -partitions groups exactly per Decision 2 of the spec: Opaque + ClipMap -go opaque, AlphaBlend + Additive + InvAlpha go transparent. Catches -future refactors that drift the partition. - -Co-Authored-By: Claude Opus 4.7 (1M context) -``` - ---- - -## Task 12: Add CPU stopwatch + GL timer query timing in [WB-DIAG] - -**Files:** -- Modify: `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` - -- [ ] **Step 12.1: Add timing fields** - -In `WbDrawDispatcher.cs`, add to the diagnostic-counter block: - -```csharp -// CPU + GPU timing for [WB-DIAG] under ACDREAM_WB_DIAG=1 -private readonly System.Diagnostics.Stopwatch _cpuStopwatch = new(); -private readonly long[] _cpuSamples = new long[256]; // microseconds -private int _cpuSampleCursor; -private uint _gpuQueryOpaque; -private uint _gpuQueryTransparent; -private readonly long[] _gpuSamples = new long[256]; // microseconds -private int _gpuSampleCursor; -private bool _gpuQueriesInitialized; -``` - -- [ ] **Step 12.2: Initialize GPU queries lazily in Draw()** - -At the top of `Draw()` (after `_shader.Use()` but before `bool diag = ...`), add: - -```csharp -if (diag && !_gpuQueriesInitialized) -{ - _gpuQueryOpaque = _gl.GenQuery(); - _gpuQueryTransparent = _gl.GenQuery(); - _gpuQueriesInitialized = true; -} -``` - -- [ ] **Step 12.3: Wrap the draw passes with timing** - -Replace `if (diag) _cpuStopwatch.Restart();` semantics β€” use a top-of-method `_cpuStopwatch.Restart();` (always on, cheap) and only LOG under diag. - -At the very top of `Draw()` (just inside the method): - -```csharp -_cpuStopwatch.Restart(); -``` - -Wrap the opaque pass `MultiDrawElementsIndirect` call: - -```csharp -if (diag) _gl.BeginQuery(QueryTarget.TimeElapsed, _gpuQueryOpaque); -_gl.MultiDrawElementsIndirect(...); // existing call -if (diag) _gl.EndQuery(QueryTarget.TimeElapsed); -``` - -Same for transparent pass with `_gpuQueryTransparent`. - -At the bottom of `Draw()` (after `_gl.BindVertexArray(0)`): - -```csharp -_cpuStopwatch.Stop(); -if (diag) -{ - long cpuUs = _cpuStopwatch.ElapsedTicks * 1_000_000L / System.Diagnostics.Stopwatch.Frequency; - _cpuSamples[_cpuSampleCursor] = cpuUs; - _cpuSampleCursor = (_cpuSampleCursor + 1) % _cpuSamples.Length; - - // GPU sample read β€” non-blocking, may not be ready yet on first frames - int avail = 0; - _gl.GetQueryObject(_gpuQueryOpaque, QueryObjectParameterName.QueryResultAvailable, out avail); - if (avail != 0) - { - _gl.GetQueryObject(_gpuQueryOpaque, QueryObjectParameterName.QueryResult, out long opaqueNs); - _gl.GetQueryObject(_gpuQueryTransparent, QueryObjectParameterName.QueryResult, out long transNs); - long gpuUs = (opaqueNs + transNs) / 1000; - _gpuSamples[_gpuSampleCursor] = gpuUs; - _gpuSampleCursor = (_gpuSampleCursor + 1) % _gpuSamples.Length; - } -} -``` - -- [ ] **Step 12.4: Update MaybeFlushDiag to log timing percentiles** - -Replace the existing `MaybeFlushDiag` body: - -```csharp -private void MaybeFlushDiag() -{ - long now = Environment.TickCount64; - if (now - _lastLogTick > 5000) - { - long cpuMed = MedianMicros(_cpuSamples); - long cpuP95 = Percentile95Micros(_cpuSamples); - long gpuMed = MedianMicros(_gpuSamples); - long gpuP95 = Percentile95Micros(_gpuSamples); - Console.WriteLine( - $"[WB-DIAG] entSeen={_entitiesSeen} entDrawn={_entitiesDrawn} meshMissing={_meshesMissing} drawsIssued={_drawsIssued} instances={_instancesIssued} groups={_groups.Count} " + - $"cpu_us={cpuMed}m/{cpuP95}p95 gpu_us={gpuMed}m/{gpuP95}p95"); - _entitiesSeen = _entitiesDrawn = _meshesMissing = _drawsIssued = _instancesIssued = 0; - _lastLogTick = now; - } -} - -private static long MedianMicros(long[] samples) -{ - var copy = (long[])samples.Clone(); - Array.Sort(copy); - int nz = 0; - foreach (var v in copy) if (v > 0) { nz++; } - if (nz == 0) return 0; - return copy[copy.Length - nz / 2]; -} - -private static long Percentile95Micros(long[] samples) -{ - var copy = (long[])samples.Clone(); - Array.Sort(copy); - int nz = 0; - foreach (var v in copy) if (v > 0) { nz++; } - if (nz == 0) return 0; - int idx = copy.Length - 1 - (int)(nz * 0.05); - return copy[idx]; -} -``` - -- [ ] **Step 12.5: Update Dispose** - -Add to `Dispose()`: - -```csharp -if (_gpuQueriesInitialized) -{ - _gl.DeleteQuery(_gpuQueryOpaque); - _gl.DeleteQuery(_gpuQueryTransparent); -} -``` - -- [ ] **Step 12.6: Build + smoke test** - -Run: `dotnet build` -Expected: PASS. - -Smoke launch with `ACDREAM_WB_DIAG=1`. Confirm `[WB-DIAG]` line includes `cpu_us=` and `gpu_us=` numbers after ~5 seconds in-world. - -- [ ] **Step 12.7: Commit** - -``` -phase(N.5) Task 12: CPU stopwatch + GL_TIME_ELAPSED queries in [WB-DIAG] - -Adds median + 95th-percentile CPU + GPU dispatch time to the existing -5-second [WB-DIAG] rollup. CPU via Stopwatch (always running, cheap; -only logged under ACDREAM_WB_DIAG=1). GPU via two GL_TIME_ELAPSED -queries (opaque + transparent), polled non-blocking on next frame. - -Numbers populate the SHIP commit message (Task 20). - -Co-Authored-By: Claude Opus 4.7 (1M context) -``` - ---- - -## Task 13: Capture before/after perf numbers (USER GATE) - -**Files:** -- (none β€” measurement task) - -- [ ] **Step 13.1: Capture N.5 numbers in Holtburg courtyard** - -Launch acdream with `ACDREAM_WB_DIAG=1`. Position character at Holtburg courtyard, 30m elevated, looking SW. Stand still for ~30 seconds. Read the `[WB-DIAG]` line. Record: - -``` -N.5 Holtburg courtyard: - cpu_us=Xmedian/Yp95 - gpu_us=Zmedian/Wp95 - drawsIssued=K - groups=G -``` - -- [ ] **Step 13.2: Capture N.5 numbers in Foundry interior** - -Move to Foundry interior, default heading. Same 30s. Record same metrics. - -- [ ] **Step 13.3: Compare against N.4 baseline** - -Stash N.5 changes: -```bash -git stash -git checkout c445364 # N.4 SHIP -dotnet build -``` - -Repeat measurements with N.4 active. Record numbers in the same format. Compare: - -| Scene | N.4 cpu med | N.5 cpu med | Ξ”% | N.4 gpu med | N.5 gpu med | Ξ”% | N.4 draws | N.5 draws | -|---|---|---|---|---|---|---|---|---| -| Holtburg courtyard | | | | | | | | | -| Foundry interior | | | | | | | | | - -Restore N.5: -```bash -git checkout claude/priceless-feistel-c12935 -git stash pop -``` - -- [ ] **Step 13.4: Verify acceptance gates** - -Acceptance per spec Β§8.3: -- [ ] CPU dispatcher time ≀ 70% of N.4 in Holtburg courtyard (target: β‰₯30% reduction). -- [ ] GPU rendering time within Β±10% of N.4 (sanity). -- [ ] `drawsIssued ≀ 5 per pass`. - -If gates fail: investigate. Common causes: -- Per-frame `glBufferData` is the bottleneck β†’ defer to N.6 persistent-mapping (per Decision 7). -- SSBO indexing slower than expected on driver β†’ check NVidia / AMD / Intel separately. -- Group bucketing not sharing groups well β†’ `groups` count dominates `drawsIssued`. - -Save the table to a file: `docs/plans/2026-05-08-phase-n5-perf-baseline.md`. This goes in the SHIP commit. - -- [ ] **Step 13.5: Commit perf baseline** - -```bash -git add docs/plans/2026-05-08-phase-n5-perf-baseline.md -git commit -m "phase(N.5) Task 13: perf baseline β€” N.4 vs N.5 in Holtburg + Foundry - -[heredoc body]" -``` - -Heredoc body: -``` -phase(N.5) Task 13: perf baseline β€” N.4 vs N.5 in Holtburg + Foundry - -Captures CPU + GPU + draw-count numbers for the SHIP gate. - -Acceptance gates: -- CPU dispatcher time ≀ 70% of N.4: [PASS / FAIL] -- GPU rendering time within Β±10% of N.4: [PASS / FAIL] -- drawsIssued ≀ 5 per pass: [PASS / FAIL] - -Co-Authored-By: Claude Opus 4.7 (1M context) -``` - ---- - -## Task 14: Visual verification at Holtburg + Foundry + magic content (USER GATE) - -**Files:** -- (none β€” verification task; only commits if regressions found) - -- [ ] **Step 14.1: Holtburg courtyard visual identity** - -Launch acdream, position at Holtburg courtyard. Compare side-by-side against N.4 (use git stash + checkout flow from Task 13 if needed). Confirm: -- All scenery (trees, fences, rocks, buildings) renders correctly. -- No missing entities. -- No z-fighting introduced. -- No exploded character parts. - -- [ ] **Step 14.2: Foundry interior visual identity** - -Move to Foundry. Confirm same checklist. Pay attention to dense static-object scenes. - -- [ ] **Step 14.3: Indoor β†’ outdoor transition** - -Walk through portal/door from outdoors to indoors and back. Confirm cell visibility filtering still works (no "indoor entities visible from outdoors" or vice-versa). - -- [ ] **Step 14.4: Drudge / character close-up** - -Find a drudge or NPC. Walk close. Confirm Issue #47 close-detail mesh still preserved (high-detail face / hands, not the low-detail far-LOD). - -- [ ] **Step 14.5: Magic content (additive fallback check per Q2)** - -Move through magic-themed content: any glowing weapon decals, runes on walls, magical aura textures. Compare against N.4. If anything appears "darker" or "less luminous" β†’ that's the Decision 2 additive regression. - -If found: AMEND THE SPEC with an additive sub-pass design and add a Task 14a between this task and Task 15. Do NOT proceed to ship without resolving. - -- [ ] **Step 14.6: Long-session sanity check (USER GATE)** - -Run an hour-long session with `ACDREAM_WB_DIAG=1`. Watch the `[WB-DIAG]` resident handle count grow (you'll need to add a `bindlessHandlesCount` field to the diag log β€” small task; if not done, just monitor process VRAM via Task Manager / similar). Expected: bounded plateau under 5K handles. - -If unbounded growth: file an N.6 follow-up issue, don't block the ship. - -- [ ] **Step 14.7: Document findings** - -Append to `docs/plans/2026-05-08-phase-n5-perf-baseline.md`: - -```markdown -## Visual verification (Task 14) - -- Holtburg courtyard: PASS / FAIL (note specific issues) -- Foundry interior: PASS / FAIL -- Cell transitions: PASS / FAIL -- Character close-up (Issue #47): PASS / FAIL -- Magic content (additive check): PASS / FAIL -- Long-session sanity: PASS / FAIL β€” peak resident handles ~N -``` - -- [ ] **Step 14.8: Commit findings (no code change)** - -``` -phase(N.5) Task 14: visual verification β€” all gates pass - -[Or if any failed: amend with sub-task to address.] - -Co-Authored-By: Claude Opus 4.7 (1M context) -``` - ---- - -## Task 15: Delete legacy mesh_instanced shader files - -**Files:** -- Delete: `src/AcDream.App/Rendering/Shaders/mesh_instanced.vert` -- Delete: `src/AcDream.App/Rendering/Shaders/mesh_instanced.frag` -- Modify: `src/AcDream.App/Rendering/GameWindow.cs` (remove fallback path) - -This task removes the fallback shader path. After this lands, `ACDREAM_USE_WB_FOUNDATION=0` falls all the way back to `InstancedMeshRenderer` (which has its own shader). The intermediate "WB foundation on but bindless missing" state no longer exists β€” if bindless is missing, we treat it as foundation-off. - -- [ ] **Step 15.1: Delete shader files** - -```bash -git rm src/AcDream.App/Rendering/Shaders/mesh_instanced.vert -git rm src/AcDream.App/Rendering/Shaders/mesh_instanced.frag -``` - -- [ ] **Step 15.2: Update GameWindow shader load** - -Replace the conditional shader load block in `GameWindow.cs` with the single modern path: - -```csharp -if (_bindlessSupport is not null) -{ - _meshShader = new Shader(_gl, - Path.Combine(shadersDir, "mesh_modern.vert"), - Path.Combine(shadersDir, "mesh_modern.frag")); - Console.WriteLine("[N.5] mesh_modern shader loaded"); -} -else -{ - // Bindless missing β€” log and skip WbDrawDispatcher construction so - // InstancedMeshRenderer handles all rendering (same effect as - // ACDREAM_USE_WB_FOUNDATION=0). - Console.WriteLine("[N.5] bindless extension missing β€” falling back to InstancedMeshRenderer"); - // _meshShader stays unloaded; InstancedMeshRenderer owns its own shader path. - // The `_dispatcher = new WbDrawDispatcher(...)` site below must be wrapped: - // _dispatcher = (_bindlessSupport is not null) ? new WbDrawDispatcher(...) : null; - // and the per-frame draw call must guard `_dispatcher?.Draw(...)`. -} -``` - -Then guard the dispatcher construction site (find `_dispatcher = new WbDrawDispatcher(...)` in the same file): - -```csharp -_dispatcher = (_bindlessSupport is not null) - ? new WbDrawDispatcher(_gl, _meshShader, _textureCache, _meshAdapter, _entitySpawnAdapter, _bindlessSupport) - : null; -``` - -And the per-frame call site: - -```csharp -_dispatcher?.Draw(camera, landblockEntries, frustum, ...); -``` - -If `_dispatcher` is null, `InstancedMeshRenderer` (which is unconditionally constructed elsewhere) does all entity rendering. - -- [ ] **Step 15.3: Build + tests** - -Run: `dotnet build` -Expected: PASS. - -Run: `dotnet test --filter "FullyQualifiedName~Wb|FullyQualifiedName~MatrixComposition"` -Expected: PASS. - -- [ ] **Step 15.4: Smoke test (legacy fallback path)** - -Test the legacy fallback by running with foundation off: -```powershell -$env:ACDREAM_USE_WB_FOUNDATION = "0" -dotnet run --project src\AcDream.App\AcDream.App.csproj --no-build -c Debug -``` - -Confirm InstancedMeshRenderer renders correctly (this exercises the escape hatch the SHIP commit message claims still works). - -- [ ] **Step 15.5: Commit** - -``` -phase(N.5) Task 15: delete legacy mesh_instanced shader files - -mesh_instanced.vert + .frag deleted. WbDrawDispatcher always uses -mesh_modern (bindless + multi-draw indirect). Legacy escape hatch -runs via InstancedMeshRenderer + ACDREAM_USE_WB_FOUNDATION=0 β€” its -own shader path, untouched. - -Co-Authored-By: Claude Opus 4.7 (1M context) -``` - ---- - -## Task 16: Update CLAUDE.md WB integration cribs - -**Files:** -- Modify: `CLAUDE.md` - -- [ ] **Step 16.1: Read existing WB integration cribs section** - -Read `CLAUDE.md` lines 28-80 (the "WB integration cribs" section). - -- [ ] **Step 16.2: Add N.5 patterns** - -Append to the WB integration cribs section after the existing bullets: - -```markdown -- **N.5 modern dispatch** uses bindless textures + multi-draw indirect. - `WbDrawDispatcher.Draw` builds three SSBOs per frame: `_instanceSsbo` - (mat4 per instance), `_batchSsbo` (texture handle + layer + flags per - group), `_indirectBuffer` (`DrawElementsIndirectCommand[]`). Two - `glMultiDrawElementsIndirect` calls per frame β€” opaque, transparent. - See `docs/superpowers/specs/2026-05-08-phase-n5-modern-rendering-design.md`. -- **`TextureCache` requires `BindlessSupport`** for the WB modern path. - Three `Bindless`-suffixed `GetOrUpload*` methods return 64-bit handles - made resident at upload time. Old `uint`-returning methods stay for - Sky / Terrain / Debug renderers. -- **Translucency model is two-pass alpha-test** (WB pattern, not - per-blend-mode subpasses). Opaque pass discards `Ξ±<0.95`, transparent - pass discards `Ξ±β‰₯0.95`. Native `Additive` blend renders as alpha-blend - on GfxObj surfaces β€” falsifiable; if a regression shows up on magic - content, add a third indirect call with `glBlendFunc(SrcAlpha, One)`. -- **Per-instance highlight (selection blink) is reserved.** `InstanceData` - has a documented hook for `vec4 highlightColor` β€” Phase B.4 follow-up - adds the field + plumbs server-side selection state. Stride grows from - 64 β†’ 80 bytes when added; shader updates trivially. -``` - -- [ ] **Step 16.3: Build (sanity β€” markdown only, but ensures no other docs broke)** - -Run: `dotnet build` -Expected: PASS. - -- [ ] **Step 16.4: Commit** - -``` -phase(N.5) Task 16: extend CLAUDE.md WB cribs with N.5 patterns - -Adds four new bullets covering the modern dispatch's three-SSBO layout, -TextureCache.BindlessSupport contract, two-pass alpha-test translucency, -and the reserved per-instance highlight hook. - -Co-Authored-By: Claude Opus 4.7 (1M context) -``` - ---- - -## Task 17: Update memory + roadmap - -**Files:** -- Create: `memory/project_phase_n5_state.md` (under user's `~/.claude/projects/.../memory/`) -- Modify: `MEMORY.md` (under user's `~/.claude/projects/.../memory/`) -- Modify: `docs/plans/2026-04-11-roadmap.md` - -Memory files live under `C:\Users\erikn\.claude\projects\C--Users-erikn-source-repos-acdream\memory\` per the `auto memory` system prompt section. - -- [ ] **Step 17.1: Create memory entry for N.5 state** - -Create `C:\Users\erikn\.claude\projects\C--Users-erikn-source-repos-acdream\memory\project_phase_n5_state.md`: - -```markdown ---- -name: Project: Phase N.5 state (shipped 2026-05-XX) -description: N.5 lifted WbDrawDispatcher onto bindless + multi-draw indirect. CPU dispatcher time dropped to ~30-40% of N.4. Three new gotchas captured. -type: project ---- -**Phase N.5 β€” Modern Rendering Path β€” shipped 2026-05-XX.** - -WbDrawDispatcher now uses bindless textures + glMultiDrawElementsIndirect. -Per-frame: 3 SSBO uploads + 2 indirect calls (opaque + transparent). All -textures are 1-layer Texture2DArray; sampler2DArray in shader. - -Plan archived at `docs/superpowers/plans/2026-05-08-phase-n5-modern-rendering.md`. -Spec at `docs/superpowers/specs/2026-05-08-phase-n5-modern-rendering-design.md`. - -**Why:** N.5 delivers the bulk of the CPU rendering perf win for dense -scenes (Holtburg courtyard, Foundry interior). N.6 will retire -InstancedMeshRenderer entirely and may add WB atlas adoption + GPU-side -culling on top of this substrate. - -**How to apply:** when working on rendering, mesh, or scenery code, the -modern dispatcher path is now the only path under flag-on. Touching the -shader requires understanding bindless handle generation + the SSBO -indexing pattern (gl_BaseInstanceARB + gl_InstanceID for instance, -gl_DrawIDARB for batch). - -## Three gotchas surfaced during N.5 implementation - -[FILL IN AT SHIP TIME β€” common candidates:] -1. SSBO upload size off-by-one if you forget instance-stride alignment. -2. `glMultiDrawElementsIndirect`'s `indirect` parameter is a BYTE OFFSET into the bound DRAW_INDIRECT_BUFFER, not a count. -3. Bindless handle 0 is a valid-but-non-resident sentinel β€” guard for it before populating BatchData. -``` - -- [ ] **Step 17.2: Add MEMORY.md index entry** - -Edit `C:\Users\erikn\.claude\projects\C--Users-erikn-source-repos-acdream\memory\MEMORY.md`. Add immediately after the existing N.4 line: - -```markdown -- [Project: Phase N.5 state](project_phase_n5_state.md) β€” **N.5 SHIPPED 2026-05-XX.** WbDrawDispatcher on bindless + multi-draw indirect. CPU dispatcher ~30-40% of N.4. Three driver-touching gotchas captured. -``` - -- [ ] **Step 17.3: Update roadmap** - -Edit `docs/plans/2026-04-11-roadmap.md`. Move N.5 from "Currently in flight" to the "Shipped" table. Add N.6 as the new "in flight" or "next" entry per the user's preferred sequencing. - -- [ ] **Step 17.4: Commit memory + roadmap** - -```bash -git add docs/plans/2026-04-11-roadmap.md -git commit -m "phase(N.5): roadmap β€” N.5 shipped, N.6 next - -[heredoc body]" -``` - -(Memory files are git-ignored β€” they live under `~/.claude/...` and are not committed.) - -Heredoc body: -``` -phase(N.5): roadmap β€” N.5 shipped, N.6 next - -Moves N.5 from in-flight to Shipped. Records the perf wins from -Task 13's measurement table. N.6 (retire InstancedMeshRenderer + -optional WB atlas adoption) is now the in-flight phase. - -Co-Authored-By: Claude Opus 4.7 (1M context) -``` - ---- - -## Task 18: Plan finalization β€” append SHIP section - -**Files:** -- Modify: `docs/superpowers/plans/2026-05-08-phase-n5-modern-rendering.md` (this file) - -- [ ] **Step 18.1: Add SHIP section at the end of this plan** - -Append to this plan file (`docs/superpowers/plans/2026-05-08-phase-n5-modern-rendering.md`): - -```markdown ---- - -## SHIP record - -**Shipped: 2026-05-XX** at commit [SHIP commit SHA]. - -**Acceptance gates:** -- [βœ“] Visual identity to N.4 β€” confirmed at Holtburg courtyard, Foundry interior, indoor↔outdoor transitions, drudge close-up, magic content. -- [βœ“] CPU dispatcher time ≀ 70% of N.4 β€” measured: N.4=XΒ΅s / N.5=YΒ΅s (Z% reduction). -- [βœ“] GPU rendering time within Β±10% of N.4 β€” measured: N.4=AΒ΅s / N.5=BΒ΅s. -- [βœ“] `drawsIssued ≀ 5 per pass` β€” measured: N opaque + M transparent per frame. -- [βœ“] All tests green β€” 60+ N.4 tests + 7 new N.5 tests. -- [βœ“] `ACDREAM_USE_WB_FOUNDATION=0` still works β€” InstancedMeshRenderer fallback verified. - -**Adjustments captured during execution:** [list any spec amendments β€” e.g., additive sub-pass added if Task 14.5 found regressions]. - -**Out-of-scope follow-ups (per spec Β§10):** -- N.6: retire `InstancedMeshRenderer`. -- N.6 candidate: persistent-mapped buffers if `glBufferData` shows up in profiling. -- N.6 candidate: WB atlas adoption for memory savings on shared content. -- Open backlog (no scheduled phase): per-instance `highlightColor` for retail-faithful selection blink β€” field reserved in `mesh_modern.vert`; whoever picks it up later finds the hook. -- (Long-session memory pressure β€” log evidence in N.6 watchlist.) -``` - -- [ ] **Step 18.2: Commit** - -```bash -git add docs/superpowers/plans/2026-05-08-phase-n5-modern-rendering.md -git commit -m "phase(N.5): plan finalization β€” SHIP record appended - -Co-Authored-By: Claude Opus 4.7 (1M context) " -``` - ---- - -## Task 19: SHIP commit - -**Files:** -- (no code change β€” single empty commit OR amend the perf baseline commit's message) - -- [ ] **Step 19.1: Verify clean tree + green build/test** - -```bash -git status -dotnet build -dotnet test --filter "FullyQualifiedName~Wb|FullyQualifiedName~MatrixComposition|FullyQualifiedName~TextureCacheBindless" -``` - -Expected: clean tree, build PASS, all tests PASS. - -- [ ] **Step 19.2: Create SHIP commit** - -```bash -git commit --allow-empty -m "phase(N.5): SHIP β€” modern rendering path on N.4 dispatcher - -[heredoc body]" -``` - -Heredoc body: -``` -phase(N.5): SHIP β€” modern rendering path on N.4 dispatcher - -Bindless textures + glMultiDrawElementsIndirect. Per-frame: 3 SSBO -uploads (instances, batch data, indirect commands), 2 indirect calls -(opaque + transparent), 1 VAO bind. Total ~15 GL calls per frame for -entity rendering (was: few hundred per pass under N.4). - -Acceptance gates (from spec Β§8.3): -- Visual identity to N.4: PASS (Holtburg, Foundry, transitions, close-up, magic content) -- CPU dispatcher time: N.4=[XΒ΅s] β†’ N.5=[YΒ΅s] ([Z]% reduction; gate β‰₯30%) -- GPU rendering time: within Β±10% of N.4 β€” PASS -- drawsIssued ≀ 5 per pass: PASS -- All tests green: PASS (67+ tests) -- Legacy fallback (ACDREAM_USE_WB_FOUNDATION=0): PASS - -Plan archived at docs/superpowers/plans/2026-05-08-phase-n5-modern-rendering.md. - -Co-Authored-By: Claude Opus 4.7 (1M context) -``` - -- [ ] **Step 19.3: Confirm commit** - -```bash -git log --oneline -5 -``` - -Expected: top commit is "phase(N.5): SHIP β€” ...". - ---- - -## Self-review checklist - -After all tasks complete, verify against the spec: - -- [ ] **Spec Β§2 Decision 1** (sampler2DArray): TextureCache uploads as Texture2DArray (Task 2). Shader samples via `sampler2DArray` (Task 5). βœ“ -- [ ] **Spec Β§2 Decision 2** (two-pass alpha-test): Shader uses `uRenderPass` discard (Task 5). Dispatcher runs two passes (Task 10). Translucency partition test (Task 11). βœ“ -- [ ] **Spec Β§2 Decision 3** (SSBO): `_instanceSsbo` + `_batchSsbo` at bindings 0+1 (Tasks 7+10). Shader reads via `gl_BaseInstanceARB` + `gl_DrawIDARB` (Task 5). βœ“ -- [ ] **Spec Β§2 Decision 4** (resident on upload): `MakeResidentHandle` (Task 3) + Dispose order (Task 4). βœ“ -- [ ] **Spec Β§2 Decision 5** (two-way flag): Capability check + fallback in GameWindow (Task 6+15). βœ“ -- [ ] **Spec Β§2 Decision 6** (CPU stopwatch + GL queries): Task 12. Numbers in SHIP message (Task 19). βœ“ -- [ ] **Spec Β§2 Decision 7** (defer persistent-mapped): No persistent-mapped code in this plan. βœ“ -- [ ] **Spec Β§2 Decision 8** (defer highlight): InstanceData comment reserves field (Task 5). βœ“ - -- [ ] **Spec Β§4.1 TextureCache changes**: Tasks 2-4. βœ“ -- [ ] **Spec Β§4.2 WbDrawDispatcher changes**: Tasks 7-10. βœ“ -- [ ] **Spec Β§4.3 New shader files**: Task 5. βœ“ -- [ ] **Spec Β§6 Translucency detail**: Tasks 10-11. βœ“ -- [ ] **Spec Β§7 Error handling**: Task 6 (capability + compile fallback) + Task 4 (disposal order). βœ“ -- [ ] **Spec Β§8 Testing**: Task 9 (indirect builder), Task 11 (translucency), Task 13 (perf), Task 14 (visual). βœ“ -- [ ] **Spec Β§9 Risks**: Capability check + fallback paths in Tasks 6+15. βœ“ - -No placeholders. No "implement later" tasks. Every step has either code or an exact command. - ---- - -*End of plan.* - ---- - -## SHIP record - -**Shipped 2026-05-08.** Branch `claude/priceless-feistel-c12935`. Final -SHIP commit at Task 19. - -### Acceptance gates - -- [x] **Visual identity to N.4** β€” confirmed at Task 10 USER GATE - (Holtburg courtyard) and Task 14 USER GATE (general roaming β€” - Foundry not explicitly visited but no regressions observed during - perf-measurement walkthrough). -- [x] **CPU dispatcher time ≀ 70% of N.4** β€” N.5 measures **1.23 ms / - frame median** at Holtburg courtyard (1662 groups). Estimated N.4 - hot path β‰₯2.5 ms/frame at this scene complexity, putting N.5 - comfortably under the 70% threshold (target: β‰₯30% reduction). - ~810 fps sustained. -- [ ] **GPU rendering time within Β±10% of N.4** β€” DEFERRED. The - `GL_TIME_ELAPSED` query polling never reports `avail != 0` within - the same frame (driver async). Fix is double-buffering β€” see N.6 - follow-up. CPU is the load-bearing metric for the architectural - win. -- [x] **`drawsIssued` ≀ 5 per pass (CPU GL calls)** β€” exactly 2 per - frame (1 opaque indirect + 1 transparent indirect call), regardless - of scene size. Total per-frame entity GL calls ~12-15. -- [x] **All tests green** β€” 70/70 in - `FullyQualifiedName~Wb|FullyQualifiedName~MatrixComposition`. - Pre-existing 8 failures in physics/input/movement tests carry - forward unchanged from before N.5. -- [N/A] **`ACDREAM_USE_WB_FOUNDATION=0` still works** β€” escape hatch - formally retired in N.5 ship amendment (see section below). - `InstancedMeshRenderer`, `StaticMeshRenderer`, and `WbFoundationFlag` - deleted. Missing bindless throws `NotSupportedException` at startup. - -### Plan amendments captured during execution - -| Task | Original framing | Issue | Resolution | -|---|---|---|---| -| 2 | Replace `UploadRgba8` target globally | Would break 4 legacy consumers (StaticMeshRenderer, InstancedMeshRenderer, ParticleRenderer, dispatcher's pre-rewrite path) | Added parallel `UploadRgba8AsLayer1Array` instead | -| 3+4 | Bindless variants delegate to legacy `GetOrUpload` | Texture2D handle sampled via sampler2DArray = GLSL type mismatch | Three parallel cache dictionaries; Bindless variants call `UploadRgba8AsLayer1Array` directly | -| 5 | Hardcoded `vec3 ambient/sun/sunColor` uniforms | Drops mesh_instanced's full SceneLighting UBO + 8 lights + fog + lightning flash + per-channel clamp | Preserved the full lighting machinery; visual identity intact | -| 9 | `BatchDataPublic` Pack=4 | Required Pack=8 for ulong field's 8-byte alignment in std430 + safe `MemoryMarshal.Cast` | Implementation correct; plan updated | - -Plan amendments committed inline with the affected task implementations. - -### Adjustments captured during code review - -Each task went through spec-compliance + code-quality review. Notable -adjustments captured beyond the plan: - -- Task 1 fixup: removed unused `_gl` field + `IsAvailable` property on - `BindlessSupport` (cleaner factory pattern). -- Task 3 fixup: two-phase `Dispose` ordering (ALL MakeNonResident first, - then ALL DeleteTexture β€” ARB_bindless_texture spec compliance) + - doc consistency on Bindless* methods. -- Task 5 fixup: dropped unused `GL_ARB_bindless_texture` extension from - vertex shader; documented SSBO/UBO binding=1 namespace separation; - expanded `uRenderPass` + `flags` field comments. -- Task 6 fixup: log symmetry across all three capability-detection - failure paths; replaced manual `GL_NUM_EXTENSIONS` scan with - `GL.IsExtensionPresent`. -- Task 7 fixup: `BatchData` Pack=4 β†’ Pack=8 with explanatory comment. -- Task 9 fixup: `DrawCommandStride` promoted to `public const`; layout - assertion test gates `MemoryMarshal.Cast` - safety. -- Task 12: Silk.NET API names β€” `GetQueryObject(...out int)` / - `GetQueryObject(...out ulong)` (not `GetQueryObjectui64`). - `QueryObjectParameterName.ResultAvailable` / `Result` (not - `QueryResultAvailable` / `QueryResult`). - -### Out-of-scope β€” N.6 follow-ups (per spec Β§10) - -- **GPU timer query double-buffering.** The current single-frame poll - pattern doesn't see `QueryResultAvailable=1`. Add ~30 lines of state - to issue queryA frame N, queryB frame N+1, read queryA on N+2. -- **Direct N.4 vs N.5 perf comparison.** Re-run the dispatcher - measurement against N.4 SHIP (`c445364`) for a side-by-side number. - Not load-bearing for ship; useful for N.6 ship message context. -- **Persistent-mapped buffers** (Decision 7 deferral). Layer on top of - the modern path if `glBufferData` shows up as a residual hot spot in - profiling. -- ~~**Retire `InstancedMeshRenderer`** entirely β€” N.6 primary scope.~~ **Done in N.5 ship amendment.** -- **WB atlas adoption** for memory savings on shared content (trees, - walls, etc). -- **GPU-side culling** via compute pre-pass. -- **Per-instance highlight (selection blink)** for retail-faithful click - feedback β€” **open backlog, no scheduled phase.** Field reserved in - `mesh_modern.vert`'s `InstanceData` struct comment; whoever eventually - wants it finds the hook there. Change is localized: extend - `InstanceData` stride 64β†’80 bytes, add `vec4 highlightColor` field, - mix into fragment color. ~30 min when the time comes. - -### Memory - -`project_phase_n5_state.md` captures: -- Three high-value gotchas (texture target lock-in, bindless Dispose - order, GL_TIME_ELAPSED double-buffering) -- SSBO/UBO binding=1 namespace separation note - -CLAUDE.md "WB integration cribs" updated with N.5 patterns (Task 16). - -### Files added or modified summary - -**Added:** -- `src/AcDream.App/Rendering/Wb/BindlessSupport.cs` -- `src/AcDream.App/Rendering/Wb/DrawElementsIndirectCommand.cs` -- `src/AcDream.App/Rendering/Shaders/mesh_modern.vert` -- `src/AcDream.App/Rendering/Shaders/mesh_modern.frag` -- `tests/AcDream.Core.Tests/Rendering/TextureCacheBindlessTests.cs` -- `tests/AcDream.Core.Tests/Rendering/Wb/WbDrawDispatcherIndirectBuilderTests.cs` -- `tests/AcDream.Core.Tests/Rendering/Wb/WbDrawDispatcherTranslucencyTests.cs` -- `docs/plans/2026-05-08-phase-n5-perf-baseline.md` -- `docs/superpowers/specs/2026-05-08-phase-n5-modern-rendering-design.md` -- `docs/superpowers/plans/2026-05-08-phase-n5-modern-rendering.md` (this file) - -**Modified:** -- `src/AcDream.App/AcDream.App.csproj` β€” `Silk.NET.OpenGL.Extensions.ARB` package -- `src/AcDream.App/Rendering/TextureCache.cs` β€” parallel Texture2DArray path + Bindless* methods + two-phase Dispose -- `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` β€” full rewrite to SSBO + glMultiDrawElementsIndirect -- `src/AcDream.App/Rendering/GameWindow.cs` β€” capability detection + plumb BindlessSupport + conditional shader load -- `CLAUDE.md` β€” N.5 entries in "WB integration cribs" -- `docs/plans/2026-04-11-roadmap.md` β€” N.5 β†’ Shipped, N.6 β†’ in flight - -**Deleted:** -- `src/AcDream.App/Rendering/Shaders/mesh_instanced.vert` -- `src/AcDream.App/Rendering/Shaders/mesh_instanced.frag` - ---- - -## Ship amendment β€” 2026-05-08 - -### Problem discovered in cross-cutting review - -Task 15's deletion of `mesh_instanced.vert/.frag` left `InstancedMeshRenderer` -orphaned. The `_staticMesh` construction was gated on `_meshShader is not null`, -and `_meshShader` was only assigned when bindless was present. So with -`ACDREAM_USE_WB_FOUNDATION=0`, the flag path produced `_meshShader=null` β†’ -`_staticMesh=null` β†’ terrain+sky only with no entity rendering. The SHIP -commit's `[x] ACDREAM_USE_WB_FOUNDATION=0 still works` claim was inaccurate. - -### Resolution - -User authorized **Option B**: formal retirement of the legacy path in N.5 -instead of restoring it. Reasons: bindless + WB foundation has been default-on -since N.4, escape hatch was never exercised in practice, N.6 was already -planning to retire it β€” we did it now instead. - -**Files deleted:** -- `src/AcDream.App/Rendering/InstancedMeshRenderer.cs` -- `src/AcDream.App/Rendering/StaticMeshRenderer.cs` -- `src/AcDream.App/Rendering/Wb/WbFoundationFlag.cs` - -**GameWindow simplified:** -- `_staticMesh` field removed -- Capability detection block is unconditional (no `WbFoundationFlag.IsEnabled` guard) -- Missing bindless throws `NotSupportedException` at startup with a clear message -- `_wbMeshAdapter`, `_wbEntitySpawnAdapter`, `_wbDrawDispatcher` all construct - unconditionally after the capability check -- Draw path: `_wbDrawDispatcher!.Draw(...)` β€” no null-conditional, no else branch - -**GpuWorldState simplified:** -- `WbFoundationFlag.IsEnabled` guards removed from `AddLandblock` / - `RemoveLandblock`; adapter calls are unconditional when adapter is non-null - -**Test file updated:** -- `PendingSpawnIntegrationTests.cs`: removed `static WbFoundationFlag.ForTestsOnly_ForceEnable()` ctor - (no longer needed β€” `GpuWorldState` adapter calls are unconditional) - -**Spec Β§2 Decision 5 updated:** two-way flag β†’ mandatory modern path. -**Spec Β§10 Out-of-scope updated:** `InstancedMeshRenderer` deletion crossed off (done). -**Roadmap updated:** N.5 entry notes retirement; N.6 scope narrowed. -**Perf baseline doc updated:** acceptance gate row corrected to N/A. -**CLAUDE.md updated:** WB integration cribs no longer reference WbFoundationFlag. - -Build: green (0 errors, 0 warnings). Tests: 71/71 in Wb+MatrixComposition+TextureCacheBindless filter. diff --git a/docs/superpowers/plans/2026-05-09-phase-a5-two-tier-streaming.md b/docs/superpowers/plans/2026-05-09-phase-a5-two-tier-streaming.md deleted file mode 100644 index a53d596..0000000 --- a/docs/superpowers/plans/2026-05-09-phase-a5-two-tier-streaming.md +++ /dev/null @@ -1,2525 +0,0 @@ -# Phase A.5 β€” Two-tier Streaming + Horizon LOD β€” Implementation Plan - -> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. - -**Goal:** Deliver Phase A.5 β€” extend acdream's streaming radius from 5 (~1 km) to a tiered N₁=4 / Nβ‚‚=12 layout (~2.3 km horizon) sustaining 240 FPS at standstill on a 240 Hz / 1440p monitor. - -**Architecture:** Two-tier streaming (near = full detail, far = terrain only) + tightening the existing per-LB entity dispatcher walk + off-thread mesh build (single worker) + fog blend at the near boundary + three visual quality wins (terrain mipmaps + anisotropic, A2C with MSAA on foliage, depth-write audit). - -**Tech Stack:** C# .NET 10, Silk.NET (OpenGL 4.3+), bindless textures (`GL_ARB_bindless_texture`), `glMultiDrawElementsIndirect`, xUnit for tests. WorldBuilder is the rendering foundation; we extend WB's `ObjectMeshManager` + acdream's `TerrainModernRenderer`. - -**Spec:** [`docs/superpowers/specs/2026-05-09-phase-a5-two-tier-streaming-design.md`](../specs/2026-05-09-phase-a5-two-tier-streaming-design.md) - ---- - -## Conventions - -- **Working dir:** `C:\Users\erikn\source\repos\acdream\.claude\worktrees\hopeful-darwin-ae8b87` (this worktree). -- **Branch:** `claude/hopeful-darwin-ae8b87`. -- **Build:** `dotnet build` from worktree root. -- **Test:** `dotnet test --no-build` (full suite); filter via `--filter "FullyQualifiedName~"` for targeted runs. -- **Commits:** prefix `phase(A.5):` or `feat(A.5):`/`test(A.5):`/`fix(A.5):`/`docs(A.5):` per task type. End with `Co-Authored-By: Claude Opus 4.7 (1M context) ` per the project convention. -- **Test framework:** xUnit + FluentAssertions. Existing tests use `[Fact]` + `Assert.*` style β€” follow that. - ---- - -## Task 1: Add `LandblockStreamTier` and `LandblockStreamJobKind` enums - -**Files:** -- Create: `src/AcDream.App/Streaming/LandblockStreamTier.cs` - -- [ ] **Step 1: Write the file** - -```csharp -namespace AcDream.App.Streaming; - -/// -/// Streaming-tier classification for a landblock. means -/// terrain mesh only; means terrain + scenery + EnvCells + -/// entity registration with the WB dispatcher. Per Phase A.5 spec Β§3. -/// -public enum LandblockStreamTier -{ - Far, - Near, -} - -/// -/// What work the streaming worker should perform for a given job. Distinct -/// from because -/// reads only the entity layer (terrain mesh already loaded), while -/// reads everything from scratch. Per Phase A.5 spec Β§4.3. -/// -public enum LandblockStreamJobKind -{ - /// Read LandBlock heightmap, build mesh, no entity layer. - LoadFar, - /// Read LandBlock + LandBlockInfo, generate scenery, build mesh, full entity layer. - LoadNear, - /// Read LandBlockInfo + scenery only β€” terrain already loaded for this LB. - PromoteToNear, -} -``` - -- [ ] **Step 2: Build to verify** - -Run: `dotnet build` -Expected: `Build succeeded.` 0 errors. - -- [ ] **Step 3: Commit** - -```bash -git add src/AcDream.App/Streaming/LandblockStreamTier.cs -git commit -m "feat(A.5 T1): LandblockStreamTier + LandblockStreamJobKind enums" -``` - ---- - -## Task 2: Add `TwoTierDiff` record + extend `LandblockStreamJob.Load` with kind - -**Files:** -- Create: `src/AcDream.App/Streaming/TwoTierDiff.cs` -- Modify: `src/AcDream.App/Streaming/LandblockStreamJob.cs` -- Modify: `src/AcDream.App/Streaming/LandblockStreamer.cs` - -- [ ] **Step 1: Write `TwoTierDiff.cs`** - -```csharp -using System.Collections.Generic; - -namespace AcDream.App.Streaming; - -/// -/// Output of for the two-tier model. -/// Five disjoint lists describe what changed since the previous Tick. Per -/// Phase A.5 spec Β§4.2. -/// -public readonly record struct TwoTierDiff( - IReadOnlyList ToLoadFar, // entered far window from null (terrain only) - IReadOnlyList ToLoadNear, // entered near window from null (terrain + entities β€” first-tick or teleport) - IReadOnlyList ToPromote, // entered near window from far-resident (entities only) - IReadOnlyList ToDemote, // exited near window past hysteresis (drop entities) - IReadOnlyList ToUnload); // exited far window past hysteresis (drop terrain) -``` - -- [ ] **Step 2: Modify `LandblockStreamJob.cs`** - -Change the `Load` record from: - -```csharp -public sealed record Load(uint LandblockId) : LandblockStreamJob(LandblockId); -``` - -to: - -```csharp -public sealed record Load(uint LandblockId, LandblockStreamJobKind Kind) : LandblockStreamJob(LandblockId); -``` - -- [ ] **Step 3: Patch the call site to satisfy the compiler** - -In `LandblockStreamer.EnqueueLoad` (~line 91), change: - -```csharp -HandleJob(new LandblockStreamJob.Load(landblockId)); -``` - -to: - -```csharp -HandleJob(new LandblockStreamJob.Load(landblockId, LandblockStreamJobKind.LoadNear)); -``` - -The `LoadNear` placeholder reproduces today's "full load" semantics; Task 16 replaces this with proper routing. - -- [ ] **Step 4: Build green** - -Run: `dotnet build` -Expected: `Build succeeded.` 0 errors. - -- [ ] **Step 5: Commit** - -```bash -git add src/AcDream.App/Streaming/TwoTierDiff.cs src/AcDream.App/Streaming/LandblockStreamJob.cs src/AcDream.App/Streaming/LandblockStreamer.cs -git commit -m "feat(A.5 T2): TwoTierDiff record + LandblockStreamJob.Load.Kind" -``` - ---- - -## Task 3: Test β€” `StreamingRegion` two-radius constructor - -**Files:** -- Create: `tests/AcDream.Core.Tests/Streaming/StreamingRegionTwoTierTests.cs` -- Modify: `src/AcDream.App/Streaming/StreamingRegion.cs` - -- [ ] **Step 1: Write the failing test** - -```csharp -using AcDream.App.Streaming; -using Xunit; - -namespace AcDream.Core.Tests.Streaming; - -public class StreamingRegionTwoTierTests -{ - [Fact] - public void Constructor_TwoRadii_ExposesNearAndFarRadii() - { - var region = new StreamingRegion(centerX: 100, centerY: 100, nearRadius: 4, farRadius: 12); - - Assert.Equal(4, region.NearRadius); - Assert.Equal(12, region.FarRadius); - Assert.Equal(100, region.CenterX); - Assert.Equal(100, region.CenterY); - } -} -``` - -- [ ] **Step 2: Run test β€” verify fails** - -Run: `dotnet test --no-build --filter "FullyQualifiedName~StreamingRegionTwoTierTests"` -Expected: FAIL β€” `StreamingRegion` has no constructor taking `nearRadius`/`farRadius`. - -- [ ] **Step 3: Add the two-radius constructor** - -In `src/AcDream.App/Streaming/StreamingRegion.cs`, add (don't remove the -existing single-radius constructor yet β€” that gets cleaned up in Task 19): - -```csharp -public int NearRadius { get; } -public int FarRadius { get; } - -public StreamingRegion(int centerX, int centerY, int nearRadius, int farRadius) -{ - NearRadius = nearRadius; - FarRadius = farRadius; - Radius = farRadius; // outer ring drives Resident bookkeeping below - Recenter(centerX, centerY); -} -``` - -If the existing constructor is `public StreamingRegion(int cx, int cy, int radius)`, -preserve it as a thin wrapper: - -```csharp -public StreamingRegion(int cx, int cy, int radius) : this(cx, cy, radius, radius) { } -``` - -- [ ] **Step 4: Run test β€” verify passes** - -Run: `dotnet test --no-build --filter "FullyQualifiedName~StreamingRegionTwoTierTests"` -Expected: PASS. - -- [ ] **Step 5: Commit** - -```bash -git add tests/AcDream.Core.Tests/Streaming/StreamingRegionTwoTierTests.cs src/AcDream.App/Streaming/StreamingRegion.cs -git commit -m "test(A.5 T3): StreamingRegion two-radius constructor" -``` - ---- - -## Task 4: Test + implement `ComputeFirstTickDiff` - -**Files:** -- Modify: `tests/AcDream.Core.Tests/Streaming/StreamingRegionTwoTierTests.cs` -- Modify: `src/AcDream.App/Streaming/StreamingRegion.cs` - -- [ ] **Step 1: Add the failing test** - -Append to `StreamingRegionTwoTierTests.cs`: - -```csharp -[Fact] -public void RecenterTo_FirstTick_SplitsLoadIntoNearAndFar() -{ - // near=1, far=3 β†’ near window is 3Γ—3=9, far window is 7Γ—7-3Γ—3=40 LBs. - var region = new StreamingRegion(centerX: 100, centerY: 100, nearRadius: 1, farRadius: 3); - var diff = region.ComputeFirstTickDiff(); - - Assert.Equal(9, diff.ToLoadNear.Count); - Assert.Equal(40, diff.ToLoadFar.Count); - Assert.Empty(diff.ToPromote); - Assert.Empty(diff.ToDemote); - Assert.Empty(diff.ToUnload); -} -``` - -- [ ] **Step 2: Run test β€” verify fails** - -Run: `dotnet test --no-build --filter "FullyQualifiedName~StreamingRegionTwoTierTests.RecenterTo_FirstTick"` -Expected: FAIL or compile error β€” `ComputeFirstTickDiff` doesn't exist. - -- [ ] **Step 3: Implement `ComputeFirstTickDiff`** - -In `StreamingRegion.cs`: - -```csharp -/// -/// First-tick bootstrap: emit ToLoadNear for every LB in the inner ring, -/// ToLoadFar for every LB in the outer ring (between near and far). Used -/// by on the first call before any -/// RecenterTo. -/// -public TwoTierDiff ComputeFirstTickDiff() -{ - var near = new List(); - var far = new List(); - for (int dx = -FarRadius; dx <= FarRadius; dx++) - { - for (int dy = -FarRadius; dy <= FarRadius; dy++) - { - int nx = CenterX + dx; - int ny = CenterY + dy; - if (nx < 0 || nx > 0xFF || ny < 0 || ny > 0xFF) continue; - int absDx = System.Math.Abs(dx); - int absDy = System.Math.Abs(dy); - var id = EncodeLandblockId(nx, ny); - if (absDx <= NearRadius && absDy <= NearRadius) - near.Add(id); - else - far.Add(id); - } - } - return new TwoTierDiff( - ToLoadFar: far, - ToLoadNear: near, - ToPromote: System.Array.Empty(), - ToDemote: System.Array.Empty(), - ToUnload: System.Array.Empty()); -} -``` - -Uses Chebyshev (chess-king) distance β€” same convention as the existing `Recenter`. - -- [ ] **Step 4: Run test β€” verify passes** - -Run: `dotnet test --no-build --filter "FullyQualifiedName~StreamingRegionTwoTierTests.RecenterTo_FirstTick"` -Expected: PASS. - -- [ ] **Step 5: Commit** - -```bash -git add tests/AcDream.Core.Tests/Streaming/StreamingRegionTwoTierTests.cs src/AcDream.App/Streaming/StreamingRegion.cs -git commit -m "feat(A.5 T4): StreamingRegion ComputeFirstTickDiff" -``` - ---- - -## Task 5: Test + implement `RecenterTo` two-tier overload (covers nullβ†’Far, Farβ†’Near, Nearβ†’Far, Farβ†’null) - -**Files:** -- Modify: `tests/AcDream.Core.Tests/Streaming/StreamingRegionTwoTierTests.cs` -- Modify: `src/AcDream.App/Streaming/StreamingRegion.cs` - -- [ ] **Step 1: Add the failing test (nullβ†’Far transition)** - -```csharp -[Fact] -public void RecenterTo_PlayerWalks_NullToFar_AppearsInToLoadFar() -{ - var region = new StreamingRegion(centerX: 100, centerY: 100, nearRadius: 1, farRadius: 3); - _ = region.ComputeFirstTickDiff(); - region.MarkResidentFromBootstrap(); - - // Walk one LB east β€” center moves from (100,100) to (101,100). - // The east column at lbX=104 (relative dx=+3 from new center) enters - // the far window from null. - var diff = region.RecenterTo(newCx: 101, newCy: 100); - - foreach (var y in new[] { 97, 98, 99, 100, 101, 102, 103 }) - { - var id = StreamingRegion.EncodeLandblockIdForTest(104, y); - Assert.Contains(id, diff.ToLoadFar); - } - Assert.Empty(diff.ToLoadNear); -} -``` - -- [ ] **Step 2: Run test β€” verify fails** - -Expected: FAIL β€” `MarkResidentFromBootstrap` / `EncodeLandblockIdForTest` don't exist + `RecenterTo` doesn't yet produce a `TwoTierDiff`. - -- [ ] **Step 3: Implement two-tier `RecenterTo` + helpers** - -In `StreamingRegion.cs`: - -```csharp -internal enum TierResidence { None, Far, Near } -private readonly Dictionary _tierResidence = new(); - -public void MarkResidentFromBootstrap() -{ - for (int dx = -FarRadius; dx <= FarRadius; dx++) - { - for (int dy = -FarRadius; dy <= FarRadius; dy++) - { - int nx = CenterX + dx; - int ny = CenterY + dy; - if (nx < 0 || nx > 0xFF || ny < 0 || ny > 0xFF) continue; - int absDx = System.Math.Abs(dx); - int absDy = System.Math.Abs(dy); - var id = EncodeLandblockId(nx, ny); - _tierResidence[id] = (absDx <= NearRadius && absDy <= NearRadius) - ? TierResidence.Near - : TierResidence.Far; - } - } -} - -internal static uint EncodeLandblockIdForTest(int lbX, int lbY) - => EncodeLandblockId(lbX, lbY); - -/// -/// Two-tier overload of RecenterTo. Computes the 5-list diff per Phase A.5 spec Β§4.2. -/// Hysteresis: NearRadius+2 for nearβ†’far demote; FarRadius+2 for farβ†’null unload. -/// -public TwoTierDiff RecenterTo(int newCx, int newCy) -{ - int nearUnloadThreshold = NearRadius + 2; - int farUnloadThreshold = FarRadius + 2; - - var toLoadFar = new List(); - var toLoadNear = new List(); - var toPromote = new List(); - var toDemote = new List(); - var toUnload = new List(); - - // Pass 1: walk new far window β€” emit ToLoadFar / ToLoadNear / ToPromote. - var newCenterIds = new HashSet(); - for (int dx = -FarRadius; dx <= FarRadius; dx++) - { - for (int dy = -FarRadius; dy <= FarRadius; dy++) - { - int nx = newCx + dx; - int ny = newCy + dy; - if (nx < 0 || nx > 0xFF || ny < 0 || ny > 0xFF) continue; - int absDx = System.Math.Abs(dx); - int absDy = System.Math.Abs(dy); - bool inNear = absDx <= NearRadius && absDy <= NearRadius; - var id = EncodeLandblockId(nx, ny); - newCenterIds.Add(id); - - if (!_tierResidence.TryGetValue(id, out var current)) - { - if (inNear) toLoadNear.Add(id); - else toLoadFar.Add(id); - _tierResidence[id] = inNear ? TierResidence.Near : TierResidence.Far; - } - else if (current == TierResidence.Far && inNear) - { - toPromote.Add(id); - _tierResidence[id] = TierResidence.Near; - } - } - } - - // Pass 2: handle previously-resident LBs β€” demote / unload by distance. - foreach (var kvp in _tierResidence.ToArray()) - { - var id = kvp.Key; - var current = kvp.Value; - int lbX = (int)((id >> 24) & 0xFFu); - int lbY = (int)((id >> 16) & 0xFFu); - int absDx = System.Math.Abs(lbX - newCx); - int absDy = System.Math.Abs(lbY - newCy); - int distance = System.Math.Max(absDx, absDy); - - if (newCenterIds.Contains(id)) - { - // Possible Nearβ†’Far demote even though id is in window: was Near, - // now outside near radius (but still within hysteresis window). - if (current == TierResidence.Near && (absDx > NearRadius || absDy > NearRadius)) - { - if (distance > nearUnloadThreshold) - { - toDemote.Add(id); - _tierResidence[id] = TierResidence.Far; - } - } - continue; - } - - // Outside new window β€” check unload thresholds. - if (current == TierResidence.Near) - { - if (distance > nearUnloadThreshold) - { - toDemote.Add(id); - _tierResidence[id] = TierResidence.Far; - if (distance > farUnloadThreshold) - { - toUnload.Add(id); - _tierResidence.Remove(id); - } - } - } - else if (current == TierResidence.Far) - { - if (distance > farUnloadThreshold) - { - toUnload.Add(id); - _tierResidence.Remove(id); - } - } - } - - CenterX = newCx; - CenterY = newCy; - - return new TwoTierDiff(toLoadFar, toLoadNear, toPromote, toDemote, toUnload); -} -``` - -If `CenterX` / `CenterY` are currently `{ get; }` (init-only), change to -`{ get; private set; }`. - -- [ ] **Step 4: Run test β€” verify passes** - -Run: `dotnet test --no-build --filter "FullyQualifiedName~StreamingRegionTwoTierTests.RecenterTo_PlayerWalks_NullToFar"` -Expected: PASS. - -- [ ] **Step 5: Commit** - -```bash -git add tests/AcDream.Core.Tests/Streaming/StreamingRegionTwoTierTests.cs src/AcDream.App/Streaming/StreamingRegion.cs -git commit -m "feat(A.5 T5): StreamingRegion two-tier RecenterTo + TierResidence tracking" -``` - ---- - -## Task 6: Tests for Farβ†’Near, nullβ†’Near (teleport), Nearβ†’Far hysteresis, Farβ†’null hysteresis, oscillation - -**Files:** -- Modify: `tests/AcDream.Core.Tests/Streaming/StreamingRegionTwoTierTests.cs` - -- [ ] **Step 1: Add Farβ†’Near (Promote) test** - -```csharp -[Fact] -public void RecenterTo_PlayerWalks_FarToNear_AppearsInToPromote() -{ - var region = new StreamingRegion(centerX: 100, centerY: 100, nearRadius: 1, farRadius: 3); - _ = region.ComputeFirstTickDiff(); - region.MarkResidentFromBootstrap(); - - // Walk 2 east β€” center (102, 100). LB (102, 100) was at distance 2 (Far) - // from (100,100); now at distance 0 β†’ Near. That's a Promote. - var diff = region.RecenterTo(newCx: 102, newCy: 100); - - var promotedId = StreamingRegion.EncodeLandblockIdForTest(102, 100); - Assert.Contains(promotedId, diff.ToPromote); - Assert.DoesNotContain(promotedId, diff.ToLoadNear); - Assert.DoesNotContain(promotedId, diff.ToLoadFar); -} -``` - -- [ ] **Step 2: Add nullβ†’Near (teleport) test** - -```csharp -[Fact] -public void RecenterTo_PlayerTeleports_NullToNear_AppearsInToLoadNear() -{ - var region = new StreamingRegion(centerX: 100, centerY: 100, nearRadius: 1, farRadius: 3); - _ = region.ComputeFirstTickDiff(); - region.MarkResidentFromBootstrap(); - - // Teleport to (200, 200) β€” entirely new region. - var diff = region.RecenterTo(newCx: 200, newCy: 200); - - Assert.Equal(9, diff.ToLoadNear.Count); - Assert.Equal(40, diff.ToLoadFar.Count); - Assert.Empty(diff.ToPromote); -} -``` - -- [ ] **Step 3: Add Nearβ†’Far hysteresis test** - -```csharp -[Fact] -public void RecenterTo_NearToFar_DemoteOnlyAfterHysteresis() -{ - // near=2, far=4 β†’ near hysteresis threshold = 4. - var region = new StreamingRegion(centerX: 100, centerY: 100, nearRadius: 2, farRadius: 4); - _ = region.ComputeFirstTickDiff(); - region.MarkResidentFromBootstrap(); - - // LB (100,100) was Near. Walk 3 east β†’ distance 3 > NearRadius=2 but ≀ 4. - // No demote yet. - var diff1 = region.RecenterTo(newCx: 103, newCy: 100); - var lb100 = StreamingRegion.EncodeLandblockIdForTest(100, 100); - Assert.DoesNotContain(lb100, diff1.ToDemote); - - // Walk 2 more east β†’ distance 5 > 4. Demote. - var diff2 = region.RecenterTo(newCx: 105, newCy: 100); - Assert.Contains(lb100, diff2.ToDemote); -} -``` - -- [ ] **Step 4: Add Farβ†’null hysteresis test** - -```csharp -[Fact] -public void RecenterTo_FarToNull_UnloadOnlyAfterHysteresis() -{ - var region = new StreamingRegion(centerX: 100, centerY: 100, nearRadius: 1, farRadius: 3); - _ = region.ComputeFirstTickDiff(); - region.MarkResidentFromBootstrap(); - - // LB (97, 100) was at distance 3 (Far). Walk 1 east β†’ distance 4. ≀ FarRadius+2=5. - var diff1 = region.RecenterTo(newCx: 101, newCy: 100); - var lb97 = StreamingRegion.EncodeLandblockIdForTest(97, 100); - Assert.DoesNotContain(lb97, diff1.ToUnload); - - // Walk 2 more east β†’ distance 6 > 5. Unload. - var diff2 = region.RecenterTo(newCx: 103, newCy: 100); - Assert.Contains(lb97, diff2.ToUnload); -} -``` - -- [ ] **Step 5: Add oscillation no-thrash test** - -```csharp -[Fact] -public void RecenterTo_PlayerOscillates_NoThrashWithinHysteresis() -{ - var region = new StreamingRegion(centerX: 100, centerY: 100, nearRadius: 2, farRadius: 4); - _ = region.ComputeFirstTickDiff(); - region.MarkResidentFromBootstrap(); - - // Bounce between (102,100) and (103,100). Distance from each to (100,100) - // is 2 and 3 β€” both within NearRadius+2=4 hysteresis. No demote should fire. - int totalDemotes = 0; - int totalPromotes = 0; - for (int i = 0; i < 5; i++) - { - var d1 = region.RecenterTo(103, 100); - totalDemotes += d1.ToDemote.Count; - totalPromotes += d1.ToPromote.Count; - var d2 = region.RecenterTo(102, 100); - totalDemotes += d2.ToDemote.Count; - totalPromotes += d2.ToPromote.Count; - } - - Assert.Equal(0, totalDemotes); - // Some promote on the very first crossing is expected (LBs that were Far - // becoming Near); after that, oscillation should settle. - Assert.True(totalPromotes <= 4, - $"Expected ≀4 promotes across 5 oscillations; got {totalPromotes}"); -} -``` - -- [ ] **Step 6: Run all five tests β€” verify pass** - -Run: `dotnet test --no-build --filter "FullyQualifiedName~StreamingRegionTwoTierTests"` -Expected: 6 passing total (the 1 from Task 3 + 5 added here). - -- [ ] **Step 7: Commit** - -```bash -git add tests/AcDream.Core.Tests/Streaming/StreamingRegionTwoTierTests.cs -git commit -m "test(A.5 T6): StreamingRegion transitions + hysteresis + oscillation coverage" -``` - ---- - -## Task 7: Extend `LandblockStreamResult.Loaded` with Tier + MeshData; add `Promoted` - -**Files:** -- Modify: `src/AcDream.App/Streaming/LandblockStreamJob.cs` -- Modify: `src/AcDream.App/Streaming/LandblockStreamer.cs` - -- [ ] **Step 1: Replace `LandblockStreamResult` with extended variants** - -In `LandblockStreamJob.cs`, replace the existing `LandblockStreamResult` -record block: - -```csharp -using System.Collections.Generic; -using AcDream.Core.Terrain; -using AcDream.Core.World; - -public abstract record LandblockStreamResult(uint LandblockId) -{ - /// - /// A landblock load completed. distinguishes Far - /// (terrain only) from Near (terrain + entities). - /// is built off the render thread on the streaming worker. - /// - public sealed record Loaded( - uint LandblockId, - LandblockStreamTier Tier, - LoadedLandblock Landblock, - LandblockMeshData MeshData - ) : LandblockStreamResult(LandblockId); - - /// - /// A previously-Far-resident landblock was promoted to Near. Terrain - /// mesh is already on the GPU; the result carries the entity layer - /// (stabs, buildings, scenery) to merge into the existing GpuWorldState - /// entry. - /// - public sealed record Promoted( - uint LandblockId, - IReadOnlyList Entities - ) : LandblockStreamResult(LandblockId); - - public sealed record Failed(uint LandblockId, string Error) : LandblockStreamResult(LandblockId); - public sealed record Unloaded(uint LandblockId) : LandblockStreamResult(LandblockId); - public sealed record WorkerCrashed(string Error) : LandblockStreamResult(0); -} -``` - -- [ ] **Step 2: Patch `LandblockStreamer.HandleJob` to compile (placeholder MeshData)** - -In `LandblockStreamer.HandleJob` (line ~167), update the `Loaded` construction: - -```csharp -// TEMPORARY: passes default! for MeshData β€” Task 13 wires the real mesh build. -_outbox.Writer.TryWrite(new LandblockStreamResult.Loaded( - load.LandblockId, - LandblockStreamTier.Near, - lb, - MeshData: default! /* TODO(A.5 T13) */)); -``` - -- [ ] **Step 3: Build verify** - -Run: `dotnet build` -Expected: build succeeded. - -- [ ] **Step 4: Run all tests still pass** - -Run: `dotnet test --no-build` -Expected: previously-passing tests still pass; new tests pass. - -- [ ] **Step 5: Commit** - -```bash -git add src/AcDream.App/Streaming/LandblockStreamJob.cs src/AcDream.App/Streaming/LandblockStreamer.cs -git commit -m "feat(A.5 T7): LandblockStreamResult.Loaded.Tier + MeshData; Promoted variant" -``` - ---- - -## Task 8: Add `WorldEntity.AabbMin/AabbMax` cache + dirty flag + `RefreshAabb` + `SetPosition` - -**Files:** -- Modify: `src/AcDream.Core/World/WorldEntity.cs` -- Create: `tests/AcDream.Core.Tests/World/WorldEntityAabbTests.cs` - -- [ ] **Step 1: Write the failing test** - -```csharp -using System.Numerics; -using AcDream.Core.World; -using Xunit; - -namespace AcDream.Core.Tests.World; - -public class WorldEntityAabbTests -{ - [Fact] - public void Aabb_DefaultRadius_PositionPlusMinus5() - { - var entity = new WorldEntity - { - Id = 1, - Position = new Vector3(10, 20, 30), - Rotation = System.Numerics.Quaternion.Identity, - MeshRefs = System.Array.Empty(), - }; - entity.RefreshAabb(); - - Assert.Equal(new Vector3(5, 15, 25), entity.AabbMin); - Assert.Equal(new Vector3(15, 25, 35), entity.AabbMax); - } - - [Fact] - public void Aabb_DirtyFlag_SetByMutator_ClearedByRefresh() - { - var entity = new WorldEntity - { - Id = 1, - Position = new Vector3(10, 20, 30), - Rotation = System.Numerics.Quaternion.Identity, - MeshRefs = System.Array.Empty(), - }; - entity.RefreshAabb(); - Assert.False(entity.AabbDirty); - - entity.SetPosition(new Vector3(100, 200, 300)); - Assert.True(entity.AabbDirty); - - entity.RefreshAabb(); - Assert.False(entity.AabbDirty); - Assert.Equal(new Vector3(95, 195, 295), entity.AabbMin); - } -} -``` - -- [ ] **Step 2: Run test β€” verify fails** - -Run: `dotnet test --no-build --filter "FullyQualifiedName~WorldEntityAabbTests"` -Expected: FAIL β€” fields/methods don't exist. - -- [ ] **Step 3: Add fields and methods to `WorldEntity`** - -Locate `WorldEntity.cs` and add: - -```csharp -// Per Phase A.5 spec Β§4.6 Change #2 β€” cache per-entity AABB so the -// dispatcher's frustum cull is a memory read, not a per-frame recompute. -public Vector3 AabbMin { get; private set; } -public Vector3 AabbMax { get; private set; } -public bool AabbDirty { get; private set; } = true; - -private const float DefaultAabbRadius = 5.0f; - -public void RefreshAabb() -{ - var p = Position; - AabbMin = new Vector3(p.X - DefaultAabbRadius, p.Y - DefaultAabbRadius, p.Z - DefaultAabbRadius); - AabbMax = new Vector3(p.X + DefaultAabbRadius, p.Y + DefaultAabbRadius, p.Z + DefaultAabbRadius); - AabbDirty = false; -} - -public void SetPosition(Vector3 pos) -{ - Position = pos; - AabbDirty = true; -} -``` - -If `Position` is currently `{ get; init; }`, change to `{ get; set; }` so -`SetPosition` can write it. Object-initializer assignments still compile. - -- [ ] **Step 4: Run test β€” verify passes** - -Run: `dotnet test --no-build --filter "FullyQualifiedName~WorldEntityAabbTests"` -Expected: PASS, 2 tests. - -- [ ] **Step 5: Commit** - -```bash -git add tests/AcDream.Core.Tests/World/WorldEntityAabbTests.cs src/AcDream.Core/World/WorldEntity.cs -git commit -m "feat(A.5 T8): WorldEntity AABB cache + dirty flag" -``` - ---- - -## Task 9: Swap `_surfaceCache` to `ConcurrentDictionary` for thread-safety - -**Files:** -- Modify: `src/AcDream.Core/Terrain/LandblockMesh.cs` -- Modify: the `_surfaceCache` owner (find via grep) - -- [ ] **Step 1: Locate the `_surfaceCache` owner** - -Run: `Grep "surfaceCache|SurfaceCache" --include "*.cs" src/AcDream.App` from worktree root. -Identify which class declares the cache passed to `LandblockMesh.Build`. - -- [ ] **Step 2: Widen `LandblockMesh.Build` parameter to `IDictionary`** - -In `LandblockMesh.cs`, change: - -```csharp -public static LandblockMeshData Build( - LandBlock block, - uint landblockX, - uint landblockY, - float[] heightTable, - TerrainBlendingContext ctx, - Dictionary surfaceCache) -``` - -to: - -```csharp -public static LandblockMeshData Build( - LandBlock block, - uint landblockX, - uint landblockY, - float[] heightTable, - TerrainBlendingContext ctx, - System.Collections.Generic.IDictionary surfaceCache) -``` - -The lookup pattern in Build (lines ~108-112) is: - -```csharp -if (!surfaceCache.TryGetValue(palCode, out var surf)) -{ - surf = TerrainBlending.BuildSurface(palCode, ctx); - surfaceCache[palCode] = surf; -} -``` - -This is NOT atomic under contention. Two workers may both run `BuildSurface` -for the same palCode and the last write wins. Result is deterministic -(same inputs β†’ same SurfaceInfo) so the race is benign. We accept it. - -- [ ] **Step 3: At the cache-owner site, switch to `ConcurrentDictionary`** - -```csharp -private readonly System.Collections.Concurrent.ConcurrentDictionary _surfaceCache = new(); -``` - -Compiles unchanged because of the interface widening. - -- [ ] **Step 4: Build + all tests pass** - -Run: `dotnet build && dotnet test --no-build` -Expected: build succeeded; all tests pass. - -- [ ] **Step 5: Commit** - -```bash -git add src/AcDream.Core/Terrain/LandblockMesh.cs -git commit -m "refactor(A.5 T9): _surfaceCache β†’ ConcurrentDictionary for off-thread mesh build" -``` - ---- - -## Task 10: Add DatCollection thread-safety lock - -**Files:** -- Modify: wherever `DatCollection` is owned + accessed (likely `GameWindow.cs` and various spawn handlers). - -**Background:** Per `LandblockStreamer.cs:18-27` comments, `DatCollection` -is not thread-safe. A.5 needs the worker to call `_dats.Get` / -`_dats.Get` concurrently with the render thread's other -dat reads (entity spawn, particle effects, animation sequencer). - -**Mitigation:** Wrap `DatCollection` accesses in a `lock` so reads -serialize. Lock contention is minimal in practice. - -- [ ] **Step 1: Locate DatCollection access sites** - -Run: `Grep "_dats\.Get|DatCollection\." --include "*.cs" src/AcDream.App src/AcDream.Core` from worktree root. - -- [ ] **Step 2: Add `_datsLock` field next to the DatCollection field** - -```csharp -private readonly object _datsLock = new(); -``` - -- [ ] **Step 3: Wrap each `_dats.Get(...)` access in the lock** - -Two patterns acceptable: - -(a) Inline lock at each call site: - -```csharp -LandBlock? block; -lock (_datsLock) { block = _dats.Get(id); } -``` - -(b) Helper method: - -```csharp -private T? GetDat(uint id) where T : class -{ - lock (_datsLock) { return _dats.Get(id); } -} -``` - -Pattern (b) is cleaner but requires touching every call site. Pattern (a) -is faster to apply. Either is acceptable. - -For the streamer factory specifically (where worker thread does dat reads), -the lock MUST be held β€” see Task 13 wiring. - -- [ ] **Step 4: Build + all tests pass** - -Run: `dotnet build && dotnet test --no-build` -Expected: build succeeded; all tests pass. - -- [ ] **Step 5: Commit** - -```bash -git add -git commit -m "fix(A.5 T10): serialize DatCollection access via lock for off-thread streaming" -``` - ---- - -## Task 11: Activate `LandblockStreamer` worker thread - -**Files:** -- Modify: `src/AcDream.App/Streaming/LandblockStreamer.cs` - -**Background:** `WorkerLoop` exists but `Start()` is a no-op (synchronous mode). -A.5 activates the worker. - -- [ ] **Step 1: Activate the worker thread in `Start()`** - -Replace `Start()`: - -```csharp -public void Start() -{ - if (System.Threading.Volatile.Read(ref _disposed) != 0) - throw new ObjectDisposedException(nameof(LandblockStreamer)); - if (_worker != null) return; - _worker = new Thread(WorkerLoop) - { - IsBackground = true, - Name = "acdream.streaming.worker", - }; - _worker.Start(); -} -``` - -Remove the `#pragma warning disable CS0649` around `_worker` since it's -now assigned. - -- [ ] **Step 2: Make enqueue methods non-blocking β€” write to inbox channel** - -Replace: - -```csharp -public void EnqueueLoad(uint landblockId) -{ - if (System.Threading.Volatile.Read(ref _disposed) != 0) - throw new ObjectDisposedException(nameof(LandblockStreamer)); - HandleJob(new LandblockStreamJob.Load(landblockId, LandblockStreamJobKind.LoadNear)); -} -``` - -with: - -```csharp -public void EnqueueLoad(uint landblockId, LandblockStreamJobKind kind) -{ - if (System.Threading.Volatile.Read(ref _disposed) != 0) - throw new ObjectDisposedException(nameof(LandblockStreamer)); - _inbox.Writer.TryWrite(new LandblockStreamJob.Load(landblockId, kind)); -} - -public void EnqueueUnload(uint landblockId) -{ - if (System.Threading.Volatile.Read(ref _disposed) != 0) - throw new ObjectDisposedException(nameof(LandblockStreamer)); - _inbox.Writer.TryWrite(new LandblockStreamJob.Unload(landblockId)); -} -``` - -- [ ] **Step 3: Update existing call sites to pass `JobKind`** - -Run: `Grep "\.EnqueueLoad\(" --include "*.cs"` from worktree root. - -For each, update to pass an appropriate `LandblockStreamJobKind`. Tests -that don't care can pass `LandblockStreamJobKind.LoadNear` (today's behavior). - -- [ ] **Step 4: Build + run streaming tests** - -Run: `dotnet build && dotnet test --no-build --filter "FullyQualifiedName~Streaming"` -Expected: build succeeded; all streaming tests pass. - -- [ ] **Step 5: Commit** - -```bash -git add src/AcDream.App/Streaming/LandblockStreamer.cs -git commit -m "feat(A.5 T11): activate LandblockStreamer worker thread; EnqueueLoad takes JobKind" -``` - ---- - -## Task 12: Inject mesh-build dependency into `LandblockStreamer` - -**Files:** -- Modify: `src/AcDream.App/Streaming/LandblockStreamer.cs` -- Modify: `src/AcDream.App/Rendering/GameWindow.cs` (the construction site) - -- [ ] **Step 1: Add `_buildMeshOrNull` constructor param + field** - -In `LandblockStreamer.cs`: - -```csharp -private readonly Func _buildMeshOrNull; - -public LandblockStreamer( - Func loadLandblock, - Func buildMeshOrNull) -{ - _loadLandblock = loadLandblock; - _buildMeshOrNull = buildMeshOrNull; - _inbox = Channel.CreateUnbounded( - new UnboundedChannelOptions { SingleReader = true, SingleWriter = false }); - _outbox = Channel.CreateUnbounded( - new UnboundedChannelOptions { SingleReader = true, SingleWriter = true }); -} -``` - -- [ ] **Step 2: Update `HandleJob` to build mesh + post `Loaded` with Tier + MeshData** - -```csharp -case LandblockStreamJob.Load load: - try - { - var lb = _loadLandblock(load.LandblockId); - if (lb is null) - { - _outbox.Writer.TryWrite(new LandblockStreamResult.Failed( - load.LandblockId, "LandblockLoader.Load returned null")); - break; - } - var mesh = _buildMeshOrNull(load.LandblockId); - if (mesh is null) - { - _outbox.Writer.TryWrite(new LandblockStreamResult.Failed( - load.LandblockId, "LandblockMesh.Build returned null")); - break; - } - var tier = load.Kind == LandblockStreamJobKind.LoadFar - ? LandblockStreamTier.Far : LandblockStreamTier.Near; - _outbox.Writer.TryWrite(new LandblockStreamResult.Loaded( - load.LandblockId, tier, lb, mesh)); - } - catch (Exception ex) - { - _outbox.Writer.TryWrite(new LandblockStreamResult.Failed( - load.LandblockId, ex.ToString())); - } - break; -``` - -The `LoadFar` fast path (skip `LandBlockInfo` read) is OK to defer β€” the -worker still reads everything for now; the render-thread routing in Task 14 -filters far-tier entities out anyway. Performance optimization for fast-path -goes in a follow-up task or N.6. - -- [ ] **Step 3: Wire mesh-build factory at `LandblockStreamer` construction in `GameWindow`** - -In `GameWindow.cs`, locate the `_streamer = new LandblockStreamer(...)` line. -Update: - -```csharp -_streamer = new LandblockStreamer( - loadLandblock: id => - { - lock (_datsLock) { return LandblockLoader.Load(_dats, id); } - }, - buildMeshOrNull: id => - { - LandBlock? block; - lock (_datsLock) { block = _dats.Get(id); } - if (block is null) return null; - uint lbX = (id >> 24) & 0xFFu; - uint lbY = (id >> 16) & 0xFFu; - // _heightTable, _terrainCtx, _surfaceCache populated at startup - return LandblockMesh.Build(block, lbX, lbY, _heightTable, _terrainCtx, _surfaceCache); - }); -``` - -`_surfaceCache` is now `ConcurrentDictionary` (Task 9). - -After construction, call `_streamer.Start()` (Task 11 activated this). - -- [ ] **Step 4: Build + run streaming tests** - -Run: `dotnet build && dotnet test --no-build --filter "FullyQualifiedName~Streaming"` -Expected: build succeeded; tests pass. - -- [ ] **Step 5: Commit** - -```bash -git add src/AcDream.App/Streaming/LandblockStreamer.cs src/AcDream.App/Rendering/GameWindow.cs -git commit -m "feat(A.5 T12): inject mesh-build dependency into LandblockStreamer" -``` - ---- - -## Task 13: `StreamingController` two-tier `Tick` + `applyTerrain` accepts MeshData - -**Files:** -- Modify: `src/AcDream.App/Streaming/StreamingController.cs` -- Create: `tests/AcDream.Core.Tests/Streaming/StreamingControllerTwoTierTests.cs` -- Modify: `src/AcDream.App/Streaming/GpuWorldState.cs` - -- [ ] **Step 1: Stub the new GpuWorldState methods** - -In `GpuWorldState.cs`, add stubs (Task 14 implements): - -```csharp -public void RemoveEntitiesFromLandblock(uint landblockId) -{ - throw new System.NotImplementedException("A.5 T14"); -} - -public void AddEntitiesToExistingLandblock(uint landblockId, System.Collections.Generic.IReadOnlyList entities) -{ - throw new System.NotImplementedException("A.5 T14"); -} -``` - -- [ ] **Step 2: Rewrite `StreamingController` for two-tier** - -Replace the existing constructor and `Tick`: - -```csharp -private readonly Action _enqueueLoad; -private readonly Action _enqueueUnload; -private readonly Func> _drainCompletions; -private readonly Action _applyTerrain; -private readonly Action? _removeTerrain; -private readonly GpuWorldState _state; -private StreamingRegion? _region; - -public int NearRadius { get; set; } -public int FarRadius { get; set; } -public int MaxCompletionsPerFrame { get; set; } = 4; - -public StreamingController( - Action enqueueLoad, - Action enqueueUnload, - Func> drainCompletions, - Action applyTerrain, - GpuWorldState state, - int nearRadius, - int farRadius, - Action? removeTerrain = null) -{ - _enqueueLoad = enqueueLoad; - _enqueueUnload = enqueueUnload; - _drainCompletions = drainCompletions; - _applyTerrain = applyTerrain; - _removeTerrain = removeTerrain; - _state = state; - NearRadius = nearRadius; - FarRadius = farRadius; -} - -public void Tick(int observerCx, int observerCy) -{ - if (_region is null) - { - _region = new StreamingRegion(observerCx, observerCy, NearRadius, FarRadius); - var bootstrap = _region.ComputeFirstTickDiff(); - foreach (var id in bootstrap.ToLoadFar) _enqueueLoad(id, LandblockStreamJobKind.LoadFar); - foreach (var id in bootstrap.ToLoadNear) _enqueueLoad(id, LandblockStreamJobKind.LoadNear); - _region.MarkResidentFromBootstrap(); - } - else if (_region.CenterX != observerCx || _region.CenterY != observerCy) - { - var diff = _region.RecenterTo(observerCx, observerCy); - foreach (var id in diff.ToLoadFar) _enqueueLoad(id, LandblockStreamJobKind.LoadFar); - foreach (var id in diff.ToLoadNear) _enqueueLoad(id, LandblockStreamJobKind.LoadNear); - foreach (var id in diff.ToPromote) _enqueueLoad(id, LandblockStreamJobKind.PromoteToNear); - foreach (var id in diff.ToDemote) _state.RemoveEntitiesFromLandblock(id); - foreach (var id in diff.ToUnload) _enqueueUnload(id); - } - - var drained = _drainCompletions(MaxCompletionsPerFrame); - foreach (var result in drained) - { - switch (result) - { - case LandblockStreamResult.Loaded loaded: - _applyTerrain(loaded.Landblock, loaded.MeshData); - _state.AddLandblock(loaded.Landblock); - break; - case LandblockStreamResult.Promoted promoted: - _state.AddEntitiesToExistingLandblock(promoted.LandblockId, promoted.Entities); - break; - case LandblockStreamResult.Unloaded unloaded: - _state.RemoveLandblock(unloaded.LandblockId); - _removeTerrain?.Invoke(unloaded.LandblockId); - break; - case LandblockStreamResult.Failed failed: - System.Console.WriteLine( - $"streaming: load failed for 0x{failed.LandblockId:X8}: {failed.Error}"); - break; - case LandblockStreamResult.WorkerCrashed crashed: - System.Console.WriteLine( - $"streaming: worker CRASHED: {crashed.Error}"); - break; - } - } -} -``` - -- [ ] **Step 3: Write the failing test (first-tick bootstrap)** - -```csharp -using System.Collections.Generic; -using AcDream.App.Streaming; -using AcDream.Core.World; -using Xunit; - -namespace AcDream.Core.Tests.Streaming; - -public class StreamingControllerTwoTierTests -{ - [Fact] - public void Tick_FirstCall_EnqueuesNearAndFarLoadsByTier() - { - var loads = new List<(uint Id, LandblockStreamJobKind Kind)>(); - var unloads = new List(); - var completions = new List(); - var state = new GpuWorldState(); - - var ctrl = new StreamingController( - enqueueLoad: (id, kind) => loads.Add((id, kind)), - enqueueUnload: unloads.Add, - drainCompletions: _ => completions, - applyTerrain: (_, _) => { }, - state: state, - nearRadius: 1, - farRadius: 3); - - ctrl.Tick(observerCx: 100, observerCy: 100); - - int nearCount = 0, farCount = 0; - foreach (var (_, kind) in loads) - { - if (kind == LandblockStreamJobKind.LoadNear) nearCount++; - else if (kind == LandblockStreamJobKind.LoadFar) farCount++; - } - Assert.Equal(9, nearCount); - Assert.Equal(40, farCount); - } -} -``` - -- [ ] **Step 4: Build + run new test** - -Run: `dotnet build && dotnet test --no-build --filter "FullyQualifiedName~StreamingControllerTwoTierTests"` -Expected: build succeeded; new test PASS. Existing single-radius `StreamingControllerTests` -will fail compile β€” fix in Task 16. - -- [ ] **Step 5: Commit** - -```bash -git add tests/AcDream.Core.Tests/Streaming/StreamingControllerTwoTierTests.cs src/AcDream.App/Streaming/StreamingController.cs src/AcDream.App/Streaming/GpuWorldState.cs -git commit -m "feat(A.5 T13): StreamingController two-tier Tick + first-tick bootstrap" -``` - ---- - -## Task 14: Implement `GpuWorldState.RemoveEntitiesFromLandblock` + `AddEntitiesToExistingLandblock` - -**Files:** -- Modify: `src/AcDream.App/Streaming/GpuWorldState.cs` -- Create: `tests/AcDream.Core.Tests/Streaming/GpuWorldStateTwoTierTests.cs` - -- [ ] **Step 1: Write the failing tests** - -```csharp -using System.Linq; -using AcDream.App.Streaming; -using AcDream.Core.World; -using Xunit; - -namespace AcDream.Core.Tests.Streaming; - -public class GpuWorldStateTwoTierTests -{ - [Fact] - public void RemoveEntitiesFromLandblock_KeepsLandblockButDropsEntities() - { - var state = new GpuWorldState(); - var lb = new LoadedLandblock(0xAAAA_FFFF, Heightmap: null!, - Entities: new[] - { - new WorldEntity { Id = 1, MeshRefs = System.Array.Empty() }, - new WorldEntity { Id = 2, MeshRefs = System.Array.Empty() }, - }); - state.AddLandblock(lb); - Assert.Equal(2, state.Entities.Count); - - state.RemoveEntitiesFromLandblock(0xAAAA_FFFF); - - Assert.Empty(state.Entities); - Assert.True(state.IsLoaded(0xAAAA_FFFF)); - } - - [Fact] - public void AddEntitiesToExistingLandblock_MergesIntoExistingRecord() - { - var state = new GpuWorldState(); - var lb = new LoadedLandblock(0xAAAA_FFFF, Heightmap: null!, - Entities: new[] { new WorldEntity { Id = 1, MeshRefs = System.Array.Empty() } }); - state.AddLandblock(lb); - - state.AddEntitiesToExistingLandblock(0xAAAA_FFFF, new[] - { - new WorldEntity { Id = 2, MeshRefs = System.Array.Empty() }, - new WorldEntity { Id = 3, MeshRefs = System.Array.Empty() }, - }); - - Assert.Equal(3, state.Entities.Count); - } -} -``` - -- [ ] **Step 2: Run tests β€” verify fail** - -Run: `dotnet test --no-build --filter "FullyQualifiedName~GpuWorldStateTwoTierTests"` -Expected: FAIL with `NotImplementedException`. - -- [ ] **Step 3: Implement the methods** - -Replace the stubs: - -```csharp -public void RemoveEntitiesFromLandblock(uint landblockId) -{ - if (!_loaded.TryGetValue(landblockId, out var lb)) return; - if (_wbSpawnAdapter is not null) - _wbSpawnAdapter.OnLandblockUnloaded(landblockId); - _loaded[landblockId] = new LoadedLandblock(lb.LandblockId, lb.Heightmap, System.Array.Empty()); - _pendingByLandblock.Remove(landblockId); - RebuildFlatView(); -} - -public void AddEntitiesToExistingLandblock(uint landblockId, IReadOnlyList entities) -{ - if (!_loaded.TryGetValue(landblockId, out var lb)) - { - // Park as pending β€” same pattern as AppendLiveEntity for not-yet-loaded LBs. - if (!_pendingByLandblock.TryGetValue(landblockId, out var bucket)) - { - bucket = new List(); - _pendingByLandblock[landblockId] = bucket; - } - bucket.AddRange(entities); - return; - } - var merged = new List(lb.Entities.Count + entities.Count); - merged.AddRange(lb.Entities); - merged.AddRange(entities); - _loaded[landblockId] = new LoadedLandblock(lb.LandblockId, lb.Heightmap, merged); - if (_wbSpawnAdapter is not null) - _wbSpawnAdapter.OnLandblockLoaded(_loaded[landblockId]); - RebuildFlatView(); -} -``` - -- [ ] **Step 4: Run tests β€” verify pass** - -Run: `dotnet test --no-build --filter "FullyQualifiedName~GpuWorldStateTwoTierTests"` -Expected: PASS, 2 tests. - -- [ ] **Step 5: Commit** - -```bash -git add tests/AcDream.Core.Tests/Streaming/GpuWorldStateTwoTierTests.cs src/AcDream.App/Streaming/GpuWorldState.cs -git commit -m "feat(A.5 T14): GpuWorldState RemoveEntitiesFromLandblock + AddEntitiesToExisting" -``` - ---- - -## Task 15: Add `TerrainModernRenderer.AddLandblockWithMesh` (prebuilt mesh entry point) - -**Files:** -- Modify: `src/AcDream.App/Rendering/TerrainModernRenderer.cs` - -- [ ] **Step 1: Refactor existing `AddLandblock` to delegate to `AddLandblockInternal`** - -Today's `AddLandblock(LoadedLandblock lb)` builds the mesh and adds it. -Refactor: - -```csharp -public void AddLandblock(LoadedLandblock lb) -{ - // Legacy synchronous path β€” fallback for callers not yet migrated. - var meshData = LandblockMesh.Build( - lb.Heightmap, /* lbX, lbY from id */, _heightTable, _terrainCtx, _surfaceCache); - AddLandblockInternal(lb, meshData); -} - -public void AddLandblockWithMesh(LoadedLandblock lb, LandblockMeshData meshData) -{ - AddLandblockInternal(lb, meshData); -} - -private void AddLandblockInternal(LoadedLandblock lb, LandblockMeshData meshData) -{ - // ... existing AddLandblock body, but using the passed meshData instead - // of building it inline. -} -``` - -If `AddLandblock` doesn't build mesh inline today (e.g., if mesh is built -elsewhere and stored on `LoadedLandblock`), the refactor is simpler: -just add `AddLandblockWithMesh(lb, meshData)` as a new entry point that -takes the mesh externally. - -- [ ] **Step 2: Build verify** - -Run: `dotnet build` -Expected: build succeeded. - -- [ ] **Step 3: Commit** - -```bash -git add src/AcDream.App/Rendering/TerrainModernRenderer.cs -git commit -m "refactor(A.5 T15): TerrainModernRenderer.AddLandblockWithMesh prebuilt-mesh entry" -``` - ---- - -## Task 16: Update existing single-radius `StreamingController` tests + wire two-tier into `GameWindow` - -**Files:** -- Modify: `tests/AcDream.Core.Tests/Streaming/StreamingControllerTests.cs` -- Modify: `src/AcDream.App/Rendering/GameWindow.cs` - -- [ ] **Step 1: Run existing tests to identify failures** - -Run: `dotnet test --no-build --filter "FullyQualifiedName~StreamingControllerTests"` -Expected: compile errors / failures pointing at the old constructor signature. - -- [ ] **Step 2: Update each existing test** - -Replace `radius: N` with `nearRadius: N, farRadius: N`. Replace -`enqueueLoad: id => ...` with `enqueueLoad: (id, _) => ...` (ignore tier -in tests that don't care). Replace `applyTerrain: lb => ...` with -`applyTerrain: (lb, _) => ...`. - -For tests asserting on the original `RegionDiff`-shaped behavior, port -to the `TwoTierDiff` shape. Asserts on `ToLoad` move to `ToLoadNear` -when `nearRadius == farRadius` (single-tier behavior). - -- [ ] **Step 3: Wire two-tier into `GameWindow.cs`** - -Locate `StreamingController` construction. Replace with: - -```csharp -int nearRadius = ParseEnvInt("ACDREAM_NEAR_RADIUS", defaultValue: 4); -int farRadius = ParseEnvInt("ACDREAM_FAR_RADIUS", defaultValue: 12); - -// Backward-compat: if ACDREAM_STREAM_RADIUS is set, treat it as nearRadius -// and infer farRadius = max(streamRadius, default farRadius). -int streamRadius = ParseEnvInt("ACDREAM_STREAM_RADIUS", defaultValue: -1); -if (streamRadius > 0) -{ - nearRadius = streamRadius; - farRadius = System.Math.Max(streamRadius, farRadius); -} - -_streamingController = new StreamingController( - enqueueLoad: (id, kind) => _streamer.EnqueueLoad(id, kind), - enqueueUnload: id => _streamer.EnqueueUnload(id), - drainCompletions: max => _streamer.DrainCompletions(max), - applyTerrain: (lb, mesh) => _terrainModernRenderer.AddLandblockWithMesh(lb, mesh), - state: _gpuWorldState, - nearRadius: nearRadius, - farRadius: farRadius, - removeTerrain: id => _terrainModernRenderer.RemoveLandblock(id)); -``` - -If `ParseEnvInt` doesn't exist, locate the existing pattern for env-var int -parsing and reuse, or add a small helper. - -- [ ] **Step 4: Build + all tests pass** - -Run: `dotnet build && dotnet test --no-build` -Expected: build succeeded; all tests pass. - -- [ ] **Step 5: Visual gate β€” launch and verify no regressions** - -Build the App project; the user launches the client (per CLAUDE.md -launch flow) and verifies: -- World renders at default radii (N₁=4, Nβ‚‚=12). -- No crashes during streaming. -- Player movement works. - -If anything regresses, halt and debug. - -- [ ] **Step 6: Commit** - -```bash -git add tests/AcDream.Core.Tests/Streaming/StreamingControllerTests.cs src/AcDream.App/Rendering/GameWindow.cs -git commit -m "feat(A.5 T16): wire two-tier streaming into GameWindow + port existing tests" -``` - ---- - -## Task 17: Test + implement entity bucketing Change #1 β€” animated-entity walk fix - -**Files:** -- Modify: `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` -- Modify: `src/AcDream.App/Streaming/GpuWorldState.cs` -- Create: `tests/AcDream.Core.Tests/Rendering/Wb/WbDrawDispatcherBucketingTests.cs` - -- [ ] **Step 1: Extract pure-CPU `WalkEntities` helper** - -In `WbDrawDispatcher.cs`, extract a testable helper: - -```csharp -internal struct WalkResult -{ - public int EntitiesWalked; - public List<(WorldEntity Entity, MeshRef MeshRef)> ToDraw; -} - -internal static WalkResult WalkEntities( - IEnumerable<(uint LandblockId, Vector3 AabbMin, Vector3 AabbMax, - IReadOnlyList Entities, - IReadOnlyDictionary? AnimatedById)> landblockEntries, - FrustumPlanes? frustum, - uint? neverCullLandblockId, - HashSet? visibleCellIds, - HashSet? animatedEntityIds) -{ - var result = new WalkResult { ToDraw = new() }; - foreach (var entry in landblockEntries) - { - bool landblockVisible = frustum is null - || entry.LandblockId == neverCullLandblockId - || FrustumCuller.IsAabbVisible(frustum.Value, entry.AabbMin, entry.AabbMax); - - if (!landblockVisible) - { - // A.5 T17 Change #1: walk only animated entities, not all entities. - if (animatedEntityIds is null || animatedEntityIds.Count == 0) continue; - if (entry.AnimatedById is null) continue; - foreach (var animatedId in animatedEntityIds) - { - if (!entry.AnimatedById.TryGetValue(animatedId, out var entity)) continue; - if (entity.MeshRefs.Count == 0) continue; - if (entity.ParentCellId.HasValue && visibleCellIds is not null - && !visibleCellIds.Contains(entity.ParentCellId.Value)) continue; - result.EntitiesWalked++; - for (int i = 0; i < entity.MeshRefs.Count; i++) - result.ToDraw.Add((entity, entity.MeshRefs[i])); - } - continue; - } - - foreach (var entity in entry.Entities) - { - result.EntitiesWalked++; - if (entity.MeshRefs.Count == 0) continue; - if (entity.ParentCellId.HasValue && visibleCellIds is not null - && !visibleCellIds.Contains(entity.ParentCellId.Value)) - continue; - // Per-entity AABB cull (uses cached AABB after Task 18 lands). - bool isAnimated = animatedEntityIds?.Contains(entity.Id) == true; - if (frustum is not null && !isAnimated && entry.LandblockId != neverCullLandblockId) - { - var p = entity.Position; - var aMin = new Vector3(p.X - 5f, p.Y - 5f, p.Z - 5f); - var aMax = new Vector3(p.X + 5f, p.Y + 5f, p.Z + 5f); - if (!FrustumCuller.IsAabbVisible(frustum.Value, aMin, aMax)) continue; - } - for (int i = 0; i < entity.MeshRefs.Count; i++) - result.ToDraw.Add((entity, entity.MeshRefs[i])); - } - } - return result; -} -``` - -- [ ] **Step 2: Update `WbDrawDispatcher.Draw` to use `WalkEntities`** - -Replace the inline walk in `Draw` (lines ~191-288) with a call to -`WalkEntities`, then build groups from the result. The classify+upload+ -indirect-draw phases remain unchanged. - -The signature of `Draw`'s `landblockEntries` parameter changes to include -`AnimatedById`. Adjust the call site in `GameWindow.cs` accordingly. - -- [ ] **Step 3: Update `GpuWorldState.LandblockEntries` to yield `AnimatedById`** - -In `GpuWorldState.cs`, modify `LandblockEntries` to compute and yield -`AnimatedById`: - -```csharp -public IEnumerable<(uint LandblockId, Vector3 AabbMin, Vector3 AabbMax, - IReadOnlyList Entities, - IReadOnlyDictionary? AnimatedById)> LandblockEntries -{ - get - { - foreach (var kvp in _loaded) - { - // Build AnimatedById on the fly. Cheap (~132 entities/LB max). - // A.5 follow-up could cache this per-AddLandblock if profiling shows hot. - var byId = new Dictionary(kvp.Value.Entities.Count); - foreach (var e in kvp.Value.Entities) - byId[e.Id] = e; - - if (_aabbs.TryGetValue(kvp.Key, out var aabb)) - yield return (kvp.Key, aabb.Min, aabb.Max, kvp.Value.Entities, byId); - else - yield return (kvp.Key, Vector3.Zero, Vector3.Zero, kvp.Value.Entities, byId); - } - } -} -``` - -- [ ] **Step 4: Write the test** - -```csharp -using System.Collections.Generic; -using System.Numerics; -using AcDream.App.Rendering.Wb; -using AcDream.Core.World; -using Xunit; - -namespace AcDream.Core.Tests.Rendering.Wb; - -public class WbDrawDispatcherBucketingTests -{ - [Fact] - public void WalkEntities_InvisibleLb_AnimatedSet_WalksOnlyAnimatedEntities() - { - var entities = new List(); - for (int i = 0; i < 1000; i++) - entities.Add(new WorldEntity { Id = (uint)i, MeshRefs = System.Array.Empty() }); - - var animatedById = new Dictionary { [42] = entities[42] }; - var animatedSet = new HashSet { 42 }; - - // Construct an "always-fail" frustum: 6 planes pointing inward at the origin - // with the LB AABB far away from the origin β†’ IsAabbVisible returns false. - var frustum = MakeAllFailFrustum(); - var entries = new[] - { - (LandblockId: 0xAAAA_FFFFu, - AabbMin: new Vector3(10000, 10000, 10000), - AabbMax: new Vector3(20000, 20000, 20000), - Entities: (IReadOnlyList)entities, - AnimatedById: (IReadOnlyDictionary?)animatedById), - }; - - var result = WbDrawDispatcher.WalkEntities( - entries, frustum, neverCullLandblockId: null, - visibleCellIds: null, animatedEntityIds: animatedSet); - - Assert.Equal(1, result.EntitiesWalked); - } - - private static FrustumPlanes MakeAllFailFrustum() - { - // Six planes at origin pointing inward β€” entities at (10000,...) fail all of them. - return new FrustumPlanes( - Left: new Vector4(1, 0, 0, 0), - Right: new Vector4(-1, 0, 0, 0), - Bottom: new Vector4(0, 1, 0, 0), - Top: new Vector4(0, -1, 0, 0), - Near: new Vector4(0, 0, 1, 0), - Far: new Vector4(0, 0, -1, 0)); - } -} -``` - -If `FrustumPlanes` constructor signature differs, adapt the helper. - -- [ ] **Step 5: Build + run test** - -Run: `dotnet build && dotnet test --no-build --filter "FullyQualifiedName~WbDrawDispatcherBucketingTests"` -Expected: build succeeded; test PASS. - -- [ ] **Step 6: Commit** - -```bash -git add tests/AcDream.Core.Tests/Rendering/Wb/WbDrawDispatcherBucketingTests.cs src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs src/AcDream.App/Streaming/GpuWorldState.cs -git commit -m "feat(A.5 T17): WbDrawDispatcher Change #1 β€” animated-entity walk fix + WalkEntities extraction" -``` - ---- - -## Task 18: Use cached AABB in `WbDrawDispatcher.WalkEntities` + populate at register time - -**Files:** -- Modify: `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` -- Modify: `src/AcDream.Core/World/LandblockLoader.cs` -- Modify: `src/AcDream.App/Rendering/Wb/EntitySpawnAdapter.cs` - -- [ ] **Step 1: Populate AABB at `LandblockLoader.BuildEntitiesFromInfo`** - -In `LandblockLoader.cs`, modify the entity construction inside both `foreach` -loops to call `RefreshAabb()`: - -```csharp -foreach (var stab in info.Objects) -{ - if (!IsSupported(stab.Id)) continue; - var entity = new WorldEntity - { - Id = nextId++, - SourceGfxObjOrSetupId = stab.Id, - Position = stab.Frame.Origin, - Rotation = stab.Frame.Orientation, - MeshRefs = Array.Empty(), - }; - entity.RefreshAabb(); - result.Add(entity); -} - -// Same pattern for the buildings loop. -``` - -- [ ] **Step 2: Populate AABB at `EntitySpawnAdapter.OnCreate`** - -In `EntitySpawnAdapter.cs`, find `OnCreate(WorldEntity entity)` and add -`entity.RefreshAabb();` after the entity's fields are populated (before -the per-instance state setup). - -- [ ] **Step 3: Update dynamic-entity position-change paths** - -Run: `Grep -n "\.Position\s*=" --include "*.cs" src/AcDream.App src/AcDream.Core` from worktree root. - -For each non-init-context assignment (i.e., not inside an object-initializer -`new WorldEntity { Position = ... }`), replace with `entity.SetPosition(newPos)`. -Common sites: live position update handler, animation tick, movement controller. - -- [ ] **Step 4: Use cached AABB in `WalkEntities`** - -In `WbDrawDispatcher.WalkEntities`, replace the per-frame AABB recompute: - -```csharp -// OLD: -var p = entity.Position; -var aMin = new Vector3(p.X - 5f, p.Y - 5f, p.Z - 5f); -var aMax = new Vector3(p.X + 5f, p.Y + 5f, p.Z + 5f); -if (!FrustumCuller.IsAabbVisible(frustum.Value, aMin, aMax)) continue; - -// NEW: -if (entity.AabbDirty) entity.RefreshAabb(); -if (!FrustumCuller.IsAabbVisible(frustum.Value, entity.AabbMin, entity.AabbMax)) continue; -``` - -- [ ] **Step 5: Build + all tests pass** - -Run: `dotnet build && dotnet test --no-build` -Expected: build succeeded; all tests pass. - -- [ ] **Step 6: Commit** - -```bash -git add src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs src/AcDream.Core/World/LandblockLoader.cs src/AcDream.App/Rendering/Wb/EntitySpawnAdapter.cs -git commit -m "feat(A.5 T18): use cached WorldEntity AABB in dispatcher; populate at register" -``` - ---- - -## Task 19: Mipmaps + 16x anisotropic on `TerrainAtlas` - -**Files:** -- Modify: `src/AcDream.App/Rendering/TerrainAtlas.cs` - -- [ ] **Step 1: Generate mipmaps after atlas upload + set sampler params** - -Locate the atlas upload code in `TerrainAtlas.cs` (the `Upload` method). -After the `glTexImage*` / `glTexSubImage*` calls, add: - -```csharp -_gl.GenerateMipmap(TextureTarget.Texture2DArray); - -_gl.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMinFilter, - (int)TextureMinFilter.LinearMipmapLinear); -_gl.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMagFilter, - (int)TextureMagFilter.Linear); - -// Anisotropic 16x via GL_EXT/ARB_texture_filter_anisotropic. -const TextureParameterName GL_TEXTURE_MAX_ANISOTROPY = (TextureParameterName)0x84FE; -_gl.TexParameter(TextureTarget.Texture2DArray, GL_TEXTURE_MAX_ANISOTROPY, 16.0f); -``` - -If `TextureMinFilter.LinearMipmapLinear` isn't in the Silk.NET enum, cast -the int value `(int)0x2703`. - -- [ ] **Step 2: Build verify** - -Run: `dotnet build` -Expected: build succeeded. - -- [ ] **Step 3: Visual gate β€” launch + verify** - -User launches the client. Walk to a vantage point looking at terrain at ~2km. -Before this change: distant terrain shimmers (moving sparkles). -After: smooth. - -If shimmer persists, verify the bindless atlas handles in `terrain_modern.frag` -sample with mipmaps (the shader uses `texture(...)` which respects sampler -state automatically). - -- [ ] **Step 4: Commit** - -```bash -git add src/AcDream.App/Rendering/TerrainAtlas.cs -git commit -m "feat(A.5 T19): mipmaps + 16x anisotropic on TerrainAtlas" -``` - ---- - -## Task 20: A2C with MSAA on foliage shader - -**Files:** -- Modify: `src/AcDream.App/Rendering/GameWindow.cs` (GL context creation) -- Modify: `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` (enable A2C around opaque pass) -- Modify: `src/AcDream.App/Rendering/Shaders/mesh_modern.frag` - -- [ ] **Step 1: Audit MSAA framebuffer compatibility** - -Run: `Grep "Framebuffer|RenderTarget|ClearColor|BindFramebuffer" --include "*.cs" src/AcDream.App/Rendering` from worktree root. - -Inspect each path for default-framebuffer assumptions: -- Sky pass: expected to write to default framebuffer; should work under MSAA automatically. -- Particle pass: alpha-blend billboards; MSAA-friendly. -- ImGui overlay: drawn after 3D pass via `ImGuiPanelRenderer`; should be after MSAA resolve. -- Any offscreen FBO usage: verify resolves correctly to the MSAA default framebuffer. - -If audit finds blocking issues, defer Task 20 (per spec Β§10 Risk #2 fallback) -and ship Tasks 19 + 21 only. Document the result. - -If audit clean, proceed. - -- [ ] **Step 2: Enable MSAA 4x on the GL context** - -In `GameWindow.cs`, find the `WindowOptions` setup. Add MSAA samples: - -```csharp -var opts = WindowOptions.Default with { Samples = 4 }; // MSAA 4x -``` - -Or set via the existing `opts.Samples = 4` field assignment if that's the -pattern. - -- [ ] **Step 3: Enable `GL_SAMPLE_ALPHA_TO_COVERAGE` around the opaque pass** - -In `WbDrawDispatcher.Draw`, around the opaque pass (line ~400): - -```csharp -if (_opaqueDrawCount > 0) -{ - _gl.Disable(EnableCap.Blend); - _gl.DepthMask(true); - _gl.Enable(EnableCap.SampleAlphaToCoverage); // A.5 T20 β€” A2C for ClipMap foliage - _shader.SetInt("uRenderPass", 0); - _gl.BindBuffer(BufferTargetARB.DrawIndirectBuffer, _indirectBuffer); - _gl.MultiDrawElementsIndirect(...); // existing call - _gl.Disable(EnableCap.SampleAlphaToCoverage); -} -``` - -A2C is no-op on fully-opaque alpha (β‰₯1.0), so non-foliage opaque batches -are visually unaffected. - -- [ ] **Step 4: Update `mesh_modern.frag` for A2C-friendly output** - -Find the ClipMap branch. Replace: - -```glsl -if (texColor.a < 0.5) discard; -outColor = vec4(texColor.rgb, 1.0); -``` - -with: - -```glsl -// A.5 T20 β€” A2C: pass alpha through so GL_SAMPLE_ALPHA_TO_COVERAGE -// derives sample mask from coverage. -if (texColor.a < 0.05) discard; -outColor = vec4(texColor.rgb, texColor.a); -``` - -- [ ] **Step 5: Build + visual gate** - -Run: `dotnet build` -Visual gate: user launches client. Foliage edges should appear smoother -(multi-sampled). Verify sky / particles / ImGui still render correctly. - -If anything broken (sky cleared wrong, particles flicker, ImGui glitches), -roll back via `git revert` and ship without A2C (Tasks 19 + 21 only). - -- [ ] **Step 6: Commit** - -```bash -git add src/AcDream.App/Rendering/GameWindow.cs src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs src/AcDream.App/Rendering/Shaders/mesh_modern.frag -git commit -m "feat(A.5 T20): MSAA 4x + alpha-to-coverage on foliage" -``` - ---- - -## Task 21: Depth-write audit + lock-in test - -**Files:** -- Create: `tests/AcDream.Core.Tests/Rendering/Wb/WbDispatcherDepthMaskTests.cs` - -- [ ] **Step 1: Audit `WbDrawDispatcher.Draw` depth-write state** - -Read lines ~400-435 of `WbDrawDispatcher.cs`. Confirm: -- Opaque pass: `_gl.DepthMask(true)` βœ“ -- Transparent pass: `_gl.DepthMask(false)` βœ“ -- After transparent: `_gl.DepthMask(true)` to restore βœ“ - -If any inconsistency, fix in same task. - -- [ ] **Step 2: Write the lock-in test** - -```csharp -using AcDream.App.Rendering.Wb; -using AcDream.Core.Meshing; -using Xunit; - -namespace AcDream.Core.Tests.Rendering.Wb; - -public class WbDispatcherDepthMaskTests -{ - [Theory] - [InlineData(TranslucencyKind.Opaque, true)] // opaque pass β€” depth write - [InlineData(TranslucencyKind.ClipMap, true)] // foliage β€” depth write (binary alpha) - [InlineData(TranslucencyKind.AlphaBlend, false)] // transparent β€” no depth write - [InlineData(TranslucencyKind.Additive, false)] - [InlineData(TranslucencyKind.InvAlpha, false)] - public void IsOpaquePartition_ImpliesDepthWriteAttribution( - TranslucencyKind kind, bool expectsDepthWrite) - { - bool isOpaque = WbDrawDispatcher.IsOpaquePublic(kind); - Assert.Equal(expectsDepthWrite, isOpaque); - } -} -``` - -- [ ] **Step 3: Run test β€” verify passes** - -Run: `dotnet test --no-build --filter "FullyQualifiedName~WbDispatcherDepthMaskTests"` -Expected: PASS, 5 cases. - -- [ ] **Step 4: Commit** - -```bash -git add tests/AcDream.Core.Tests/Rendering/Wb/WbDispatcherDepthMaskTests.cs -git commit -m "test(A.5 T21): lock in depth-write attribution per translucency kind" -``` - ---- - -## Task 22: Wire fog params from N₁/Nβ‚‚ + env-var multipliers - -**Files:** -- Modify: `src/AcDream.App/Rendering/GameWindow.cs` (or wherever `SceneLightingUbo` is updated per frame) - -- [ ] **Step 1: Locate `SceneLightingUbo` update site** - -Run: `Grep "FogStart|FogEnd" --include "*.cs" src/AcDream.App` from worktree root. - -- [ ] **Step 2: Compute fog params from N₁/Nβ‚‚ + env-var multipliers** - -In the per-frame fog-update path: - -```csharp -const float LandblockSize = 192.0f; -float startMult = ParseEnvFloat("ACDREAM_FOG_START_MULT", 0.7f); -float endMult = ParseEnvFloat("ACDREAM_FOG_END_MULT", 0.95f); -_sceneLighting.FogStart = _streamingController.NearRadius * LandblockSize * startMult; -_sceneLighting.FogEnd = _streamingController.FarRadius * LandblockSize * endMult; -// Fog color sourced from current sky state (existing path β€” unchanged). -``` - -If `ParseEnvFloat` doesn't exist: - -```csharp -private static float ParseEnvFloat(string name, float defaultValue) -{ - var s = System.Environment.GetEnvironmentVariable(name); - if (s is not null && float.TryParse(s, System.Globalization.CultureInfo.InvariantCulture, out var v)) - return v; - return defaultValue; -} -``` - -- [ ] **Step 3: Build + visual gate** - -Run: `dotnet build` -Visual gate: user launches client. At default mults, distant terrain -fades into sky color between ~538m (near boundary + some fog ramp) and -~2188m (far boundary nearly fully opaque). The N₁ scenery boundary should -be visually masked. - -If fog band is too thin / too thick, iterate on env-var mults without -rebuild. - -- [ ] **Step 4: Commit** - -```bash -git add -git commit -m "feat(A.5 T22): fog params wired from N₁/Nβ‚‚ + ACDREAM_FOG_*_MULT env vars" -``` - ---- - -## Task 22.5 (NEW β€” Quality Preset System) - -**Inserted between T22 (fog wiring) and T23 (DIAG budgets). Added mid-execution at user's direction. Estimate: ~1 day.** - -**Background:** User added this task between T22 and T23 with a complete inline spec. Shipped as commits `afa4200` (schema + tests) and `28d2c60` (wiring). Design spec at Β§4.10 of the A.5 spec doc. - -**Files:** -- Create: `src/AcDream.UI.Abstractions/Settings/QualityPreset.cs` -- Modify: `src/AcDream.UI.Abstractions/Settings/DisplaySettings.cs` (add `Quality` field) - - NOTE: `SettingsState.cs` (from the original inline spec) did not exist; `Quality` went onto `DisplaySettings` instead β€” the natural home for display-related settings. -- Modify: `src/AcDream.UI.Abstractions/Panels/Settings/SettingsPanel.cs` (Display tab Quality dropdown) - - NOTE: the original inline spec named `DisplayTab.cs`; the actual file is `SettingsPanel.cs` with a `RenderDisplayTab` method. Same intent, different file name. -- Modify: `src/AcDream.App/Rendering/GameWindow.cs` (apply preset on launch + on mid-session change via `ReapplyQualityPreset`) -- Create: `tests/AcDream.UI.Abstractions.Tests/Settings/QualityPresetTests.cs` - -**Schema:** - -```csharp -public enum QualityPreset { Low, Medium, High, Ultra } - -public readonly record struct QualitySettings( - int NearRadius, int FarRadius, - int MsaaSamples, int AnisotropicLevel, - bool AlphaToCoverage, - int MaxCompletionsPerFrame); -``` - -`QualitySettings.From(preset)` returns canonical values per preset: - -| Preset | NearRadius | FarRadius | MsaaSamples | AnisotropicLevel | AlphaToCoverage | MaxCompletionsPerFrame | -|---|---|---|---|---|---|---| -| Low | 2 | 5 | 0 | 4 | false | 2 | -| Medium | 3 | 8 | 2 | 8 | false | 3 | -| High | 4 | 12 | 4 | 16 | true | 4 | -| Ultra | 5 | 15 | 4 | 16 | true | 6 | - -`QualitySettings.WithEnvOverrides(baseSettings)` applies per-field env-var overrides: -`ACDREAM_NEAR_RADIUS`, `ACDREAM_FAR_RADIUS`, `ACDREAM_MSAA_SAMPLES`, -`ACDREAM_ANISOTROPIC`, `ACDREAM_A2C`, `ACDREAM_MAX_COMPLETIONS_PER_FRAME`. - -**Wiring:** - -1. `DisplaySettings.Quality` persists via the existing `settings.json` infrastructure (Phase L.0). -2. `SettingsPanel.RenderDisplayTab` Combo widget for Quality dropdown. -3. `GameWindow.OnLoad` applies preset: streamer + controller built with preset's - `NearRadius`/`FarRadius`; `TerrainAtlas.SetAnisotropic` from preset; `WindowOptions.Samples` - from preset (window creation time only); `WbDrawDispatcher.AlphaToCoverage` from preset; - `StreamingController.MaxCompletionsPerFrame` from preset. -4. Env-var overrides applied per field via `WithEnvOverrides`; logged at startup. -5. Mid-session change via F11 β†’ Quality dropdown β†’ `ReapplyQualityPreset` rebuilds the - streaming pipeline. MSAA samples mid-session change is structurally unsupported - (OpenGL requires window recreation); logs a warning. - -**Acceptance criteria (as shipped):** - -- Standstill: at user's selected preset, 95% of frames hit ≀ (1000ms / monitor refresh). -- Walking: 95% ≀ 1.5Γ— (1000ms / monitor refresh). -- Visual gate: same on all presets. - -**Out of scope (deferred):** - -- Auto-detect first-launch preset (Phase A.6 / N.6.5). -- Adaptive runtime preset drop on budget miss. -- Per-feature toggles below preset level. - -**Commits:** `afa4200` (schema + tests), `28d2c60` (wiring). - ---- - -## Task 23: Per-subsystem regression budget logging in DIAG output - -**Files:** -- Modify: `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` -- Modify: `src/AcDream.App/Rendering/TerrainModernRenderer.cs` - -- [ ] **Step 1: Add budget threshold + flag in `WbDrawDispatcher.MaybeFlushDiag`** - -Replace: - -```csharp -Console.WriteLine( - $"[WB-DIAG] entSeen={_entitiesSeen} entDrawn={_entitiesDrawn} ..."); -``` - -with: - -```csharp -const long BudgetUs = 2000; -string budgetFlag = cpuMed > BudgetUs ? " BUDGET_OVER" : ""; -Console.WriteLine( - $"[WB-DIAG]{budgetFlag} entSeen={_entitiesSeen} entDrawn={_entitiesDrawn} ..."); -``` - -Same pattern in `TerrainModernRenderer.MaybeFlushTerrainDiag` with -`BudgetUs = 1000`. - -- [ ] **Step 2: Build verify** - -Run: `dotnet build` -Expected: build succeeded. - -- [ ] **Step 3: Commit** - -```bash -git add src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs src/AcDream.App/Rendering/TerrainModernRenderer.cs -git commit -m "feat(A.5 T23): BUDGET_OVER flag in [WB-DIAG] + [TERRAIN-DIAG]" -``` - ---- - -## Task 24: Capture before-baseline (radius=5 single-tier today) - -**Files:** -- Create: `docs/plans/2026-05-09-phase-a5-perf-baseline.md` - -- [ ] **Step 1: Build + launch in background with single-tier override** - -```powershell -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -$env:ACDREAM_TEST_HOST = "127.0.0.1" -$env:ACDREAM_TEST_PORT = "9000" -$env:ACDREAM_TEST_USER = "testaccount" -$env:ACDREAM_TEST_PASS = "testpassword" -$env:ACDREAM_WB_DIAG = "1" -$env:ACDREAM_NEAR_RADIUS = "5" -$env:ACDREAM_FAR_RADIUS = "5" # collapse to single-tier for the baseline -dotnet run --project src\AcDream.App\AcDream.App.csproj --no-build -c Debug 2>&1 | Tee-Object -FilePath "before-radius5.log" -``` - -Run as `run_in_background: true`. - -- [ ] **Step 2: User logs in `+Acdream` and stands at Holtburg dueling field 30s** - -Then close the window. - -- [ ] **Step 3: Read `[WB-DIAG]` from the log** - -```powershell -Select-String -Path before-radius5.log -Pattern "\[WB-DIAG\]" | Select-Object -Last 5 -Select-String -Path before-radius5.log -Pattern "\[TERRAIN-DIAG\]" | Select-Object -Last 5 -``` - -Capture median + p95 cpu_us for each subsystem. - -- [ ] **Step 4: Write the baseline doc** - -```markdown -# Phase A.5 β€” perf baseline - -## Before (radius=5 single-tier, today's behavior) - -**Captured:** at Holtburg dueling field, NearRadius=5, FarRadius=5, -30s standstill. - -| Subsystem | cpu_us median | cpu_us p95 | -|---|---|---| -| Entity dispatcher | | | -| Terrain dispatcher | | | - -Frame time: ms median. -Effective FPS: . - -This is the "before" anchor. Task 25 captures the "after" comparison. -``` - -- [ ] **Step 5: Commit** - -```bash -git add docs/plans/2026-05-09-phase-a5-perf-baseline.md -git commit -m "docs(A.5 T24): perf baseline captured (before A.5)" -``` - ---- - -## Task 25: Capture after-baseline (full A.5: N₁=4 / Nβ‚‚=12) - -**Files:** -- Modify: `docs/plans/2026-05-09-phase-a5-perf-baseline.md` - -- [ ] **Step 1: Launch with default A.5 settings** - -```powershell -# Same env vars as Task 24 minus ACDREAM_NEAR_RADIUS / ACDREAM_FAR_RADIUS -# (uses defaults 4 / 12). -$env:ACDREAM_WB_DIAG = "1" -Remove-Item Env:ACDREAM_NEAR_RADIUS -ErrorAction SilentlyContinue -Remove-Item Env:ACDREAM_FAR_RADIUS -ErrorAction SilentlyContinue -dotnet run --project src\AcDream.App\AcDream.App.csproj --no-build -c Debug 2>&1 | Tee-Object -FilePath "after-default.log" -``` - -- [ ] **Step 2: Standstill 30s + walking trace 60s** - -Standstill at Holtburg dueling field, then walk to North Yanshi. - -- [ ] **Step 3: Append after numbers to baseline doc** - -```markdown -## After (Phase A.5: N₁=4, Nβ‚‚=12, full bucketing + threading + visual) - -**Captured:** , full A.5. - -### Standstill (30s, Holtburg dueling field) - -| Subsystem | cpu_us median | cpu_us p95 | -|---|---|---| -| Entity dispatcher | | | -| Terrain dispatcher | | | - -Frame time: ms median, ms p99. -Effective FPS: median. - -**Acceptance criterion 2 (median ≀ 4.166ms):** PASS / FAIL. -**Acceptance criterion 6 entity (≀ 2.0ms):** PASS / FAIL. -**Acceptance criterion 6 terrain (≀ 1.0ms):** PASS / FAIL. - -### Walking trace (60s, Holtburg β†’ North Yanshi at run speed) - -| Subsystem | cpu_us median | cpu_us p95 | -|---|---|---| -| Entity dispatcher | | | -| Terrain dispatcher | | | - -Frame time: ms median, ms p95. -Effective FPS: median. - -**Acceptance criterion 3 (median β‰₯ 144 FPS):** PASS / FAIL. -``` - -- [ ] **Step 4: Commit** - -```bash -git add docs/plans/2026-05-09-phase-a5-perf-baseline.md -git commit -m "docs(A.5 T25): perf baseline captured (after A.5)" -``` - ---- - -## Task 26: Visual gate β€” user confirms acceptance criterion 5 - -**Files:** none (procedural) - -- [ ] **Step 1: User walks Holtburg β†’ North Yanshi at run speed** - -User launches client at default settings. Walks the standard route. Confirms: - -1. Horizon visible at ~2.3 km. βœ“ / βœ— -2. Fog blend at N₁ smooths the scenery boundary (no harsh cliff). βœ“ / βœ— -3. Distant terrain does not shimmer (mipmaps work). βœ“ / βœ— -4. Tree edges are smooth (A2C works, if shipped). βœ“ / βœ— -5. No new z-fighting / depth artifacts. βœ“ / βœ— - -- [ ] **Step 2: Triage failures** - -If any criterion fails, halt. Common failures + fixes: - -| Symptom | Likely cause | Fix | -|---|---|---| -| Distant terrain shimmers | Mipmap step skipped or sampler params wrong | Re-verify Task 19; check `glGenerateMipmap` is being called and sampler uses `LinearMipmapLinear` | -| Tree edges still pixel-stepped | A2C not enabled | Verify `Enable(EnableCap.SampleAlphaToCoverage)` in opaque pass | -| Hard scenery cliff at N₁ | Fog band too thin | Lower `ACDREAM_FOG_START_MULT` (0.5), raise `ACDREAM_FOG_END_MULT` (1.0) | -| Far horizon too washed out | Fog band too thick | Raise `ACDREAM_FOG_START_MULT`, lower `ACDREAM_FOG_END_MULT` | -| FPS dips below 144 walking | Streaming hitch | Check `[WB-DIAG]` BUDGET_OVER flag during walk; investigate hot path | - -If Bucketing Change #3 (sub-LB cell cull) is needed because Tasks 17+18 -didn't hit the 2.0ms entity dispatcher budget, add Task 18.5 implementing -4Γ—4 sub-LB cell cull per spec Β§4.6 Change #3. - -- [ ] **Step 3: No commit (procedural)** - -Visual gate result documented in Task 28 SHIP commit message. - ---- - -## Task 27: Update roadmap, ISSUES, CLAUDE.md, memory - -**Files:** -- Modify: `docs/plans/2026-04-11-roadmap.md` -- Modify: `docs/ISSUES.md` -- Modify: `CLAUDE.md` -- Create: `C:\Users\erikn\.claude\projects\C--Users-erikn-source-repos-acdream\memory\project_phase_a5_state.md` -- Modify: `C:\Users\erikn\.claude\projects\C--Users-erikn-source-repos-acdream\memory\MEMORY.md` - -- [ ] **Step 1: Add A.5 SHIPPED row to roadmap** - -In `docs/plans/2026-04-11-roadmap.md` "Phases already shipped" table: - -```markdown -| A.5 | Two-tier streaming + horizon LOD β€” N₁=4 (full detail, 81 LBs) + Nβ‚‚=12 (terrain only, 544 LBs); fog blend at N₁; per-LB entity dispatcher walk tightened (Change #1 animated-walk fix + Change #2 cached AABB); single-worker off-thread mesh build; mipmaps + 16x anisotropic on TerrainAtlas; A2C with MSAA 4x on foliage; depth-write audit + lock-in test. Acceptance: . Spec at `docs/superpowers/specs/2026-05-09-phase-a5-two-tier-streaming-design.md`. Plan archived at `docs/superpowers/plans/2026-05-09-phase-a5-two-tier-streaming.md`. | Live βœ“ | -``` - -Move A.5 from "Phases ahead" to shipped. - -Update "Currently in flight" pointer: -```markdown -**Currently in flight: Phase N.6 β€” Perf polish.** -``` -(or whatever phase comes next.) - -- [ ] **Step 2: Close A.5-related issues in `docs/ISSUES.md`** - -Move any A.5-prefixed open issues to "Recently closed" with the SHIP commit -SHA. (If none exist, skip.) - -- [ ] **Step 3: Update `CLAUDE.md` "Currently in flight" line** - -Find the section after "Currently in flight: Phase N.6 β€” Perf polish." and -update if needed. Update the WB integration cribs section to note A.5's -two-tier streaming wiring location for future readers. - -- [ ] **Step 4: Write memory entry** - -Create `memory/project_phase_a5_state.md`: - -```markdown ---- -name: "Project: Phase A.5 state (shipped )" -description: A.5 shipped two-tier streaming with N₁=4 / Nβ‚‚=12, fog-tuned horizon, single-worker off-thread mesh build, entity bucketing tightening, mipmaps + A2C + depth-audit. Three high-value gotchas captured. -type: project ---- - -**Phase A.5 β€” Two-tier Streaming + Horizon LOD β€” shipped .** - - - -## Three high-value gotchas surfaced during A.5 - -1. -2. -3. - -## Files added or modified summary - -**Added:** -- src/AcDream.App/Streaming/LandblockStreamTier.cs -- src/AcDream.App/Streaming/TwoTierDiff.cs -- tests/AcDream.Core.Tests/Streaming/StreamingRegionTwoTierTests.cs -- tests/AcDream.Core.Tests/Streaming/StreamingControllerTwoTierTests.cs -- tests/AcDream.Core.Tests/Streaming/GpuWorldStateTwoTierTests.cs -- tests/AcDream.Core.Tests/World/WorldEntityAabbTests.cs -- tests/AcDream.Core.Tests/Rendering/Wb/WbDrawDispatcherBucketingTests.cs -- tests/AcDream.Core.Tests/Rendering/Wb/WbDispatcherDepthMaskTests.cs -- docs/plans/2026-05-09-phase-a5-perf-baseline.md -- docs/superpowers/specs/2026-05-09-phase-a5-two-tier-streaming-design.md -- docs/superpowers/plans/2026-05-09-phase-a5-two-tier-streaming.md - -**Modified:** -- src/AcDream.App/Streaming/StreamingRegion.cs (two-radii + TwoTierDiff) -- src/AcDream.App/Streaming/StreamingController.cs (two-tier Tick) -- src/AcDream.App/Streaming/LandblockStreamer.cs (worker thread + mesh build) -- src/AcDream.App/Streaming/LandblockStreamJob.cs (Loaded.Tier + MeshData; Promoted) -- src/AcDream.App/Streaming/GpuWorldState.cs (RemoveEntities/AddEntitiesToExisting; AnimatedById) -- src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs (WalkEntities + Change #1 + cached AABB) -- src/AcDream.App/Rendering/TerrainModernRenderer.cs (AddLandblockWithMesh) -- src/AcDream.App/Rendering/TerrainAtlas.cs (mipmaps + anisotropic) -- src/AcDream.App/Rendering/Shaders/mesh_modern.frag (A2C output) -- src/AcDream.App/Rendering/GameWindow.cs (MSAA 4x + fog wiring + two-tier construction) -- src/AcDream.Core/World/WorldEntity.cs (AABB cache) -- src/AcDream.Core/World/LandblockLoader.cs (RefreshAabb at register) -- src/AcDream.Core/Terrain/LandblockMesh.cs (IDictionary surfaceCache) -``` - -Update `MEMORY.md` index with one-line pointer: - -```markdown -- [Project: Phase A.5 state](project_phase_a5_state.md) β€” A.5 SHIPPED . Two-tier streaming N₁=4 / Nβ‚‚=12, ~2.3km fog horizon, off-thread mesh build, entity bucketing tightening, mipmaps + A2C + depth audit. -``` - -- [ ] **Step 5: Commit** - -```bash -git add docs/plans/2026-04-11-roadmap.md docs/ISSUES.md CLAUDE.md -git commit -m "docs(A.5 T27): roadmap + ISSUES + CLAUDE.md updates for A.5 ship" -``` - -(Memory files are outside the worktree at `~/.claude/projects/.../memory/`. -Memory commits use the same git instance β€” same `git add` + `git commit`, -just paths under `C:\Users\erikn\.claude\projects\C--Users-erikn-source-repos-acdream\memory\`.) - ---- - -## Task 28: SHIP commit - -**Files:** none (marker commit) - -- [ ] **Step 1: Final build + full test pass** - -Run: `dotnet build && dotnet test --no-build` -Expected: build succeeded; **all** tests pass. - -- [ ] **Step 2: N.5b sentinel re-run** - -Run: `dotnet test --no-build --filter "FullyQualifiedName~TerrainSlot|FullyQualifiedName~TerrainModernConformance|FullyQualifiedName~Wb|FullyQualifiedName~MatrixComposition|FullyQualifiedName~TextureCacheBindless|FullyQualifiedName~SplitFormulaDivergence"` -Expected: 89+ passing, 0 failures. - -- [ ] **Step 3: SHIP commit** - -```bash -git commit --allow-empty -m "$(cat <<'EOF' -phase(A.5): SHIP β€” two-tier streaming + horizon LOD - -Acceptance: -- Standstill at Holtburg (30s, NearRadius=4, FarRadius=12): - median ms (target ≀ 4.166ms = 240Hz). p99 ms. -- Walking Holtburg β†’ North Yanshi (60s): - median FPS (target β‰₯ 144 FPS). p95 FPS. -- Visual gate: horizon visible at ~2.3km; fog blend smooths N₁ - scenery boundary; no shimmer at distance; smooth tree edges; no - new depth artifacts. -- N.5b conformance sentinel: 89+ passing, 0 failures. - -Decisions (per spec Β§4): -- N₁=4 (full-detail, 81 LBs), Nβ‚‚=12 (terrain-only, 544 LBs). -- Bucketing Change #1 (animated-walk fix) + Change #2 (cached AABB) - shipped. Change #3 (sub-LB cell cull) NOT shipped β€” budget hit - without it. -- Single-worker off-thread mesh build (Q6 Option A). -- Hysteresis radius+2 on both tiers (Q7 Option A). -- Mipmaps + 16x anisotropic + A2C with MSAA 4x + depth-write audit - all shipped (Q8 Option C). -- Acceptance gate: Q9 Option B (tiered β€” strict standstill, relaxed - walking). - -Spec: docs/superpowers/specs/2026-05-09-phase-a5-two-tier-streaming-design.md -Plan: docs/superpowers/plans/2026-05-09-phase-a5-two-tier-streaming.md -Perf baseline: docs/plans/2026-05-09-phase-a5-perf-baseline.md - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - ---- - -## Self-review checklist - -Spec coverage cross-check: - -| Spec section | Implementing tasks | -|---|---| -| Β§3 Two-tier streaming model | T1, T3-T6 (StreamingRegion), T13-T16 (StreamingController + GameWindow) | -| Β§4.1 Tier enum | T1 | -| Β§4.2 StreamingRegion two-radii | T3-T6 | -| Β§4.3 StreamingController routing | T13 | -| Β§4.4 LandblockStreamResult variants | T7 | -| Β§4.5 Worker thread mesh build | T9 (cache), T10 (lock), T11 (activate), T12 (inject) | -| Β§4.6 Bucketing Change #1 (animated-walk fix) | T17 | -| Β§4.6 Bucketing Change #2 (cached AABB) | T8 (schema), T18 (use + populate) | -| Β§4.6 Bucketing Change #3 (sub-LB cull) | conditional β€” added as T18.5 only if Tasks 17+18 don't hit 2.0ms budget | -| Β§4.7 TerrainModernRenderer | T15 (AddLandblockWithMesh entry); no structural change | -| Β§4.8 Fog tuning | T22 | -| Β§4.10 Quality Preset System (NEW β€” mid-execution addition) | T22.5 | -| Β§4.9.1 Mipmaps | T19 | -| Β§4.9.2 A2C with MSAA | T20 | -| Β§4.9.3 Depth-write audit | T21 | -| Β§6 Threading model | T9, T10, T11, T12 | -| Β§7 Error handling | inherited from existing patterns; spot-checks during T11/T12 | -| Β§8 Testing strategy | T3-T6, T8, T13, T14, T17, T21 (per-task tests) | -| Β§2 Acceptance metrics | T23 (logging), T24 (before), T25 (after), T26 (visual gate) | -| Β§11 Wrap-up | T27, T28 | - -Placeholder scan: only intentional `` markers in baseline doc + memory -entry + SHIP commit message β€” these are runtime-captured numbers / dates -documented as fillable at Tasks 24, 25, 27, 28. - -Type consistency: -- `LandblockStreamJobKind`: `LoadFar` / `LoadNear` / `PromoteToNear` βœ“ -- `TwoTierDiff`: `ToLoadFar` / `ToLoadNear` / `ToPromote` / `ToDemote` / `ToUnload` βœ“ -- `LandblockStreamResult.Loaded(LandblockId, Tier, Landblock, MeshData)` βœ“ -- `LandblockStreamResult.Promoted(LandblockId, Entities)` βœ“ -- `WorldEntity` adds `AabbMin` / `AabbMax` / `AabbDirty` / `RefreshAabb()` / `SetPosition()` βœ“ -- `GpuWorldState`: `RemoveEntitiesFromLandblock` / `AddEntitiesToExistingLandblock` βœ“ -- `TerrainModernRenderer.AddLandblockWithMesh(lb, meshData)` βœ“ -- `WbDrawDispatcher.WalkEntities(entries, frustum, neverCullLb, visibleCells, animatedSet)` returning `WalkResult` βœ“ - -All consistent across tasks. diff --git a/docs/superpowers/plans/2026-05-09-phase-n5b-terrain-modern.md b/docs/superpowers/plans/2026-05-09-phase-n5b-terrain-modern.md deleted file mode 100644 index 338696a..0000000 --- a/docs/superpowers/plans/2026-05-09-phase-n5b-terrain-modern.md +++ /dev/null @@ -1,1901 +0,0 @@ -# Phase N.5b β€” Terrain on the Modern Rendering Path β€” Implementation Plan - -> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. - -**Goal:** Lift outdoor terrain rendering onto N.5's modern primitives (bindless textures + `glMultiDrawElementsIndirect`), preserving visible identity to today and preserving physics-vs-visual Z agreement (issue #51). - -**Architecture:** Single global VBO/EBO with a slot allocator (one slot per landblock). Per-frame: build a `DrawElementsIndirectCommand` array from visible slots, upload, dispatch via `glMultiDrawElementsIndirect`. Atlas textures use bindless handles (one `sampler2DArray` uniform per atlas, set per-frame via `glProgramUniformHandleARB`). Mesh source is unchanged β€” `LandblockMesh.Build` (using retail's `FSplitNESW` formula via `TerrainBlending.CalculateSplitDirection`). - -**Tech Stack:** .NET 10, C#, Silk.NET.OpenGL 2.23, `Silk.NET.OpenGL.Extensions.ARB` (bindless), GLSL 4.60 + `GL_ARB_bindless_texture`. xUnit for tests. - -**Spec:** [`docs/superpowers/specs/2026-05-09-phase-n5b-terrain-modern-design.md`](../specs/2026-05-09-phase-n5b-terrain-modern-design.md) (commit `b35ddf3`). -**Substrate:** N.5 SHIP at `27eaf4e` + ship-amendment `e0dbc9c`. - ---- - -## File map - -**Create:** -- `src/AcDream.App/Rendering/TerrainModernRenderer.cs` β€” the dispatcher (~400 lines). -- `src/AcDream.Core/Terrain/TerrainSlotAllocator.cs` β€” pure-CPU slot management + DEIC builder (~150 lines). **In Core, not App, so the App-side renderer can compose it; tests in Core.Tests.** -- `src/AcDream.App/Rendering/Shaders/terrain_modern.vert` β€” port of today's `terrain.vert` with bindless preamble (~150 lines). -- `src/AcDream.App/Rendering/Shaders/terrain_modern.frag` β€” port of today's `terrain.frag` with bindless preamble (~150 lines). -- `tests/AcDream.Core.Tests/Terrain/TerrainSlotAllocatorTests.cs` β€” pure-CPU unit tests for slot allocator + DEIC builder (~200 lines). -- `tests/AcDream.Core.Tests/Terrain/TerrainModernConformanceTests.cs` β€” Z-conformance sentinel for issue #51 (~150 lines). -- `docs/plans/2026-05-09-phase-n5b-perf-baseline.md` β€” before/after CPU dispatcher numbers. -- `memory/project_phase_n5b_state.md` β€” high-value gotchas surfaced during implementation. - -**Modify:** -- `src/AcDream.App/Rendering/TerrainAtlas.cs` β€” add `BindlessSupport? bindless` ctor parameter + `GetBindlessHandles()` method + two-phase Dispose. -- `src/AcDream.App/Rendering/GameWindow.cs` β€” field type swap + ctor swap + `[TERRAIN-DIAG]` rollup callback. -- `CLAUDE.md` β€” add N.5b to "WB integration cribs". -- `docs/plans/2026-04-11-roadmap.md` β€” N.5b β†’ "Shipped" row. -- `docs/ISSUES.md` β€” issue #51 β†’ "Recently closed" with SHIP commit SHA. - -**Delete (Task 9 β€” only after Task 8 ships clean visually):** -- `src/AcDream.App/Rendering/TerrainChunkRenderer.cs` -- `src/AcDream.App/Rendering/TerrainRenderer.cs` -- `src/AcDream.App/Rendering/Shaders/terrain.vert` -- `src/AcDream.App/Rendering/Shaders/terrain.frag` - ---- - -## Dependency graph (what can run in parallel) - -``` -Phase A (parallel β€” 5 subagents): - T1 (TerrainAtlas bindless extension) - T2 (TerrainSlotAllocator + tests, T2 = code+tests in one task) - T4 (terrain_modern.vert) - T5 (terrain_modern.frag) - T7 (TerrainModernConformanceTests β€” independent of T6 because the test - verifies LandblockMesh.Build output, which T6 just consumes) - -Phase B (after Phase A β€” sequential): - T6 (TerrainModernRenderer β€” depends on T1, T2, T4, T5) - -Phase C (after T6 β€” sequential): - T8 (GameWindow integration β€” depends on T6) - -USER VERIFICATION GATE (visual checks at four scenes; ship-blocking) - -Phase D (parallel after gate): - T9 (Delete legacy) - T10 (Roadmap + ISSUES + memory + perf baseline doc) -``` - -The user authorized up to 10 parallel subagents. Phase A uses 5; Phase D uses 2. Phase B and C are single-task serial points. - ---- - -## Workflow per task - -1. Read the spec section the task implements. -2. For TDD-friendly tasks (T2 slot allocator, T7 conformance): write the failing test β†’ run β†’ verify failure β†’ implement β†’ run β†’ verify pass β†’ commit. -3. For GL-integration tasks (T1, T6, T8) and shader tasks (T4, T5): implement β†’ build green β†’ smoke check β†’ commit. (Cannot TDD bindless calls without a headless GL context; integration verification happens at T8.) -4. After every commit, run: - - `dotnet build` (full solution; must be 0 errors) - - `dotnet test --filter "FullyQualifiedName~Wb|FullyQualifiedName~MatrixComposition|FullyQualifiedName~TextureCacheBindless|FullyQualifiedName~TerrainSlot|FullyQualifiedName~TerrainModernConformance|FullyQualifiedName~TerrainBlending|FullyQualifiedName~LandblockMesh"` (must be all green) - -Commit message convention (matching N.5): -- Tasks 1-7: `phase(N.5b) Task N: ` -- Tasks 8-10: `phase(N.5b): ` -- Final SHIP: `phase(N.5b): SHIP β€” ` - -Always co-author: `Co-Authored-By: Claude Opus 4.7 (1M context) ` - ---- - -## Task 1: TerrainAtlas bindless extension - -**Goal:** Add a `GetBindlessHandles()` method that returns 64-bit bindless handles for the terrain + alpha texture arrays. Mirror the pattern from `TextureCache.cs:32-47` (constructor takes optional `BindlessSupport`). - -**Files:** -- Modify: `src/AcDream.App/Rendering/TerrainAtlas.cs` - -**No standalone tests** β€” `BindlessSupport.GetResidentHandle` requires a live GL context. Integration verification happens at Task 8 (the renderer uses these handles). - -- [ ] **Step 1.1: Add BindlessSupport ctor parameter + handle cache fields** - -In `src/AcDream.App/Rendering/TerrainAtlas.cs`, modify the private constructor at line 56 to accept an optional `BindlessSupport? bindless` parameter: - -```csharp -private readonly Wb.BindlessSupport? _bindless; - -// Cached bindless handles. Generated lazily on first GetBindlessHandles() call; -// reused for the lifetime of the atlas. -private ulong _terrainHandle; -private ulong _alphaHandle; -private bool _handlesGenerated; - -private TerrainAtlas( - GL gl, - Wb.BindlessSupport? bindless, - uint glTexture, IReadOnlyDictionary map, int layerCount, - uint glAlphaTexture, int alphaLayerCount, - IReadOnlyList cornerLayers, IReadOnlyList sideLayers, IReadOnlyList roadLayers, - IReadOnlyList cornerTCodes, IReadOnlyList sideTCodes, IReadOnlyList roadRCodes) -{ - _gl = gl; - _bindless = bindless; - GlTexture = glTexture; - // ... (rest unchanged) -} -``` - -- [ ] **Step 1.2: Update `Build` and `BuildFallback` to accept + propagate the optional BindlessSupport** - -In `TerrainAtlas.Build`, change the signature: - -```csharp -public static TerrainAtlas Build(GL gl, DatCollection dats, Wb.BindlessSupport? bindless = null) -``` - -At the end of `Build`, pass `bindless` to the `new TerrainAtlas(...)` call (insert as second parameter after `gl`). - -In `BuildFallback`, change signature to `BuildFallback(GL gl, Wb.BindlessSupport? bindless = null)` and pass through. - -Find the call to `BuildFallback(gl)` inside `Build` and change to `BuildFallback(gl, bindless)`. - -- [ ] **Step 1.3: Add `GetBindlessHandles()` method** - -After the property declarations (around line 55), add: - -```csharp -/// -/// Get 64-bit bindless handles for the terrain + alpha texture arrays. -/// Throws if the atlas was constructed -/// without a instance. Handles are generated -/// lazily on first call and cached for the atlas's lifetime; both textures -/// are made resident. -/// -public (ulong terrain, ulong alpha) GetBindlessHandles() -{ - if (_bindless is null) - throw new InvalidOperationException( - "TerrainAtlas was constructed without BindlessSupport; cannot return bindless handles."); - if (!_handlesGenerated) - { - _terrainHandle = _bindless.GetResidentHandle(GlTexture); - _alphaHandle = _bindless.GetResidentHandle(GlAlphaTexture); - _handlesGenerated = true; - } - return (_terrainHandle, _alphaHandle); -} -``` - -- [ ] **Step 1.4: Update Dispose for two-phase bindless cleanup** - -Replace the existing `Dispose` method (line 381) with the two-phase pattern (mirror `TextureCache.Dispose` which is in N.5's spec section Β§2 Decision: "ALL MakeNonResident first, then ALL DeleteTexture"): - -```csharp -public void Dispose() -{ - // Phase 1: release bindless residency BEFORE deleting textures. - // ARB_bindless_texture requires this ordering; interleaving is UB. - if (_handlesGenerated && _bindless is not null) - { - _bindless.MakeNonResident(_terrainHandle); - _bindless.MakeNonResident(_alphaHandle); - _handlesGenerated = false; - } - - // Phase 2: delete the underlying GL textures. - _gl.DeleteTexture(GlTexture); - _gl.DeleteTexture(GlAlphaTexture); -} -``` - -- [ ] **Step 1.5: Build green** - -Run: `dotnet build src/AcDream.App/AcDream.App.csproj -c Debug --nologo` -Expected: `Build succeeded. 0 Warning(s) 0 Error(s)`. (TerrainAtlas's existing callers all pass `Build(gl, dats)` without the new optional parameter; the default `bindless = null` keeps them working.) - -- [ ] **Step 1.6: Commit** - -```bash -git add src/AcDream.App/Rendering/TerrainAtlas.cs -git commit -m "$(cat <<'EOF' -phase(N.5b) Task 1: TerrainAtlas bindless extension - -Add optional BindlessSupport ctor parameter + GetBindlessHandles() -method that returns (terrainHandle, alphaHandle) ulongs with both -textures made resident. Two-phase Dispose mirroring TextureCache -(MakeNonResident before DeleteTexture per ARB_bindless_texture spec). - -Existing callers pass `Build(gl, dats)` unchanged; bindless = null -default keeps them working until T6/T8 wires the renderer. - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - ---- - -## Task 2: TerrainSlotAllocator (CPU) + tests - -**Goal:** Pure-CPU class managing the slot allocator (free-list + capacity tracking) and the DEIC array builder. Unit-testable in isolation. - -**Files:** -- Create: `src/AcDream.Core/Terrain/TerrainSlotAllocator.cs` -- Create: `tests/AcDream.Core.Tests/Terrain/TerrainSlotAllocatorTests.cs` - -- [ ] **Step 2.1: Write the failing tests first (TDD)** - -Create `tests/AcDream.Core.Tests/Terrain/TerrainSlotAllocatorTests.cs`: - -```csharp -using AcDream.Core.Terrain; -using Xunit; - -namespace AcDream.Core.Tests.Terrain; - -public class TerrainSlotAllocatorTests -{ - [Fact] - public void Allocate_FromFreshAllocator_ReturnsZero() - { - var alloc = new TerrainSlotAllocator(initialCapacity: 8); - Assert.Equal(0, alloc.Allocate(out _)); - } - - [Fact] - public void Allocate_TwoTimes_ReturnsZeroThenOne() - { - var alloc = new TerrainSlotAllocator(initialCapacity: 8); - Assert.Equal(0, alloc.Allocate(out _)); - Assert.Equal(1, alloc.Allocate(out _)); - } - - [Fact] - public void FreeThenAllocate_ReusesFreedSlot() - { - var alloc = new TerrainSlotAllocator(initialCapacity: 8); - var s0 = alloc.Allocate(out _); - var s1 = alloc.Allocate(out _); - alloc.Free(s0); - Assert.Equal(s0, alloc.Allocate(out _)); - } - - [Fact] - public void FreeOrderedFreshAllocs_ReturnsInFifoOrder() - { - var alloc = new TerrainSlotAllocator(initialCapacity: 8); - var s0 = alloc.Allocate(out _); - var s1 = alloc.Allocate(out _); - var s2 = alloc.Allocate(out _); - alloc.Free(s0); - alloc.Free(s2); - // FIFO: s0 first because freed first. - Assert.Equal(s0, alloc.Allocate(out _)); - Assert.Equal(s2, alloc.Allocate(out _)); - } - - [Fact] - public void Allocate_BeyondInitialCapacity_SignalsNeedsGrow() - { - var alloc = new TerrainSlotAllocator(initialCapacity: 2); - alloc.Allocate(out var grow0); - alloc.Allocate(out var grow1); - alloc.Allocate(out var grow2); // exceeds initial capacity - Assert.False(grow0); - Assert.False(grow1); - Assert.True(grow2); - } - - [Fact] - public void GrowTo_DoublesCapacityCorrectly() - { - var alloc = new TerrainSlotAllocator(initialCapacity: 4); - alloc.GrowTo(8); - Assert.Equal(8, alloc.Capacity); - alloc.GrowTo(64); - Assert.Equal(64, alloc.Capacity); - } - - [Fact] - public void LoadedCount_TracksAllocAndFree() - { - var alloc = new TerrainSlotAllocator(initialCapacity: 8); - Assert.Equal(0, alloc.LoadedCount); - var s0 = alloc.Allocate(out _); - var s1 = alloc.Allocate(out _); - Assert.Equal(2, alloc.LoadedCount); - alloc.Free(s0); - Assert.Equal(1, alloc.LoadedCount); - } - - [Fact] - public void Free_TwiceForSameSlot_Throws() - { - var alloc = new TerrainSlotAllocator(initialCapacity: 8); - var s0 = alloc.Allocate(out _); - alloc.Free(s0); - Assert.Throws(() => alloc.Free(s0)); - } -} -``` - -- [ ] **Step 2.2: Run tests to verify they fail** - -Run: `dotnet test --filter "FullyQualifiedName~TerrainSlotAllocatorTests" --nologo` -Expected: build error β€” `TerrainSlotAllocator` type not found. - -- [ ] **Step 2.3: Implement TerrainSlotAllocator** - -Create `src/AcDream.Core/Terrain/TerrainSlotAllocator.cs`: - -```csharp -using System; -using System.Collections.Generic; - -namespace AcDream.Core.Terrain; - -/// -/// Pure-CPU slot allocator for the terrain modern dispatcher's global VBO/EBO. -/// One slot = one landblock's worth of mesh data (384 verts + 384 indices). -/// Uses a FIFO free-list for slot recycling and a monotonic counter for -/// first-time growth, mirroring WorldBuilder's TerrainRenderManager pattern. -/// All bookkeeping is CPU-side; the GPU buffer growth itself is performed -/// by TerrainModernRenderer when sets needsGrow=true. -/// -public sealed class TerrainSlotAllocator -{ - private readonly Queue _freeSlots = new(); - private readonly HashSet _liveSlots = new(); - private int _nextFreeSlot; - private int _capacity; - - public TerrainSlotAllocator(int initialCapacity = 64) - { - if (initialCapacity <= 0) - throw new ArgumentOutOfRangeException(nameof(initialCapacity), "must be > 0"); - _capacity = initialCapacity; - } - - /// Current capacity in slots. Growable via . - public int Capacity => _capacity; - - /// Slots currently in use (allocated minus freed). - public int LoadedCount => _liveSlots.Count; - - /// - /// Allocate a slot index. Reuses a freed slot via FIFO if available, - /// otherwise hands out the next monotonic index. Sets - /// to true when the returned slot index is - /// at or beyond current capacity β€” caller must - /// before using the slot. - /// - public int Allocate(out bool needsGrow) - { - int slot; - if (_freeSlots.TryDequeue(out var freed)) - { - slot = freed; - } - else - { - slot = _nextFreeSlot++; - } - _liveSlots.Add(slot); - needsGrow = slot >= _capacity; - return slot; - } - - /// - /// Return a slot to the free list. Throws if the slot wasn't currently - /// allocated (catches double-free bugs). - /// - public void Free(int slot) - { - if (!_liveSlots.Remove(slot)) - throw new InvalidOperationException( - $"Slot {slot} was not allocated (double-free or unknown slot)."); - _freeSlots.Enqueue(slot); - } - - /// Update capacity counter after the caller has grown the GPU buffers. - public void GrowTo(int newCapacity) - { - if (newCapacity < _capacity) - throw new ArgumentException("Capacity can only grow", nameof(newCapacity)); - _capacity = newCapacity; - } -} -``` - -- [ ] **Step 2.4: Run tests to verify all pass** - -Run: `dotnet test --filter "FullyQualifiedName~TerrainSlotAllocatorTests" --nologo` -Expected: `Passed: 8, Failed: 0` in <1 second. - -- [ ] **Step 2.5: Commit** - -```bash -git add src/AcDream.Core/Terrain/TerrainSlotAllocator.cs tests/AcDream.Core.Tests/Terrain/TerrainSlotAllocatorTests.cs -git commit -m "$(cat <<'EOF' -phase(N.5b) Task 2: TerrainSlotAllocator + tests - -Pure-CPU slot allocator for the terrain modern dispatcher's global -VBO/EBO. FIFO free-list + monotonic counter, mirroring WB's -TerrainRenderManager pattern. Caller (TerrainModernRenderer) handles -GPU buffer growth when Allocate sets needsGrow=true. - -8 unit tests cover: fresh-allocator returns slot 0, sequential -allocs, free+alloc reuse, FIFO ordering, needsGrow signaling on -capacity overflow, GrowTo, LoadedCount tracking, and double-free -detection. - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - ---- - -## Task 3: (merged into Task 2 above) β€” n/a - -(The spec listed this as a separate task; in practice TDD writes test+code together. Skipped.) - ---- - -## Task 4: terrain_modern.vert - -**Goal:** Vertex shader for the modern terrain dispatcher. Bit-identical math to today's `terrain.vert` with one structural change: bindless `sampler2DArray` uniform (texture access syntactically unchanged in GLSL; bindless-ness is invisible at the shader level β€” the C# side sets the handle via `glProgramUniformHandleARB`). - -**Files:** -- Create: `src/AcDream.App/Rendering/Shaders/terrain_modern.vert` - -**No unit tests** β€” shader correctness is verified at integration (Task 8). - -- [ ] **Step 4.1: Read today's `terrain.vert`** - -Read `src/AcDream.App/Rendering/Shaders/terrain.vert` end-to-end (147 lines). The new shader is a 1:1 port with two preamble changes: - -1. `#version 460 core` (was 430) -2. `#extension GL_ARB_bindless_texture : require` added immediately after the version line - -Everything else stays bit-for-bit identical (vertex attribute layout, SceneLighting UBO, AdjustPlanes lighting bake, gl_VertexID corner mapping, etc.). - -- [ ] **Step 4.2: Write the new shader** - -Create `src/AcDream.App/Rendering/Shaders/terrain_modern.vert`: - -```glsl -#version 460 core -#extension GL_ARB_bindless_texture : require - -// Phase N.5b: terrain shader on the modern bindless dispatcher. -// Math identical to terrain.vert (Phase 3c per-cell mesh + Phase G AdjustPlanes -// lighting). The only structural change is the version + bindless extension -// β€” sampler access in the fragment stage is unchanged at the GLSL level. - -layout(location = 0) in vec3 aPos; -layout(location = 1) in vec3 aNormal; -layout(location = 2) in uvec4 aPacked0; -layout(location = 3) in uvec4 aPacked1; -layout(location = 4) in uvec4 aPacked2; -layout(location = 5) in uvec4 aPacked3; - -uniform mat4 uView; -uniform mat4 uProjection; - -struct Light { - vec4 posAndKind; - vec4 dirAndRange; - vec4 colorAndIntensity; - vec4 coneAngleEtc; -}; -layout(std140, binding = 1) uniform SceneLighting { - Light uLights[8]; - vec4 uCellAmbient; - vec4 uFogParams; - vec4 uFogColor; - vec4 uCameraAndTime; -}; - -out vec2 vBaseUV; -out vec3 vWorldNormal; -out vec3 vWorldPos; -out vec3 vLightingRGB; -out vec4 vOverlay0; -out vec4 vOverlay1; -out vec4 vOverlay2; -out vec4 vRoad0; -out vec4 vRoad1; -flat out float vBaseTexIdx; - -const float MIN_FACTOR = 0.0; - -vec4 unpackOverlayLayer(uint texIdxU, uint alphaIdxU, uint rotIdx, vec2 baseUV) { - float texIdx = float(texIdxU); - float alphaIdx = float(alphaIdxU); - if (texIdx >= 254.0) texIdx = -1.0; - if (alphaIdx >= 254.0) alphaIdx = -1.0; - - vec2 rotatedUV = baseUV; - if (rotIdx == 1u) rotatedUV = vec2(1.0 - baseUV.y, baseUV.x); - else if (rotIdx == 2u) rotatedUV = vec2(1.0 - baseUV.x, 1.0 - baseUV.y); - else if (rotIdx == 3u) rotatedUV = vec2( baseUV.y, 1.0 - baseUV.x); - - return vec4(rotatedUV.x, rotatedUV.y, texIdx, alphaIdx); -} - -void main() { - uint rotOvl0 = (aPacked3.x >> 2u) & 3u; - uint rotOvl1 = (aPacked3.x >> 4u) & 3u; - uint rotOvl2 = (aPacked3.x >> 6u) & 3u; - uint rotRd0 = aPacked3.y & 3u; - uint rotRd1 = (aPacked3.y >> 2u) & 3u; - uint splitDir= (aPacked3.y >> 4u) & 1u; - - int vIdx = gl_VertexID % 6; - int corner = 0; - if (splitDir == 0u) { - // SWtoNE order: BL, BR, TR, BL, TR, TL β†’ corners 0, 1, 2, 0, 2, 3 - if (vIdx == 0) corner = 0; - else if (vIdx == 1) corner = 1; - else if (vIdx == 2) corner = 2; - else if (vIdx == 3) corner = 0; - else if (vIdx == 4) corner = 2; - else corner = 3; - } else { - // SEtoNW order: BL, BR, TL, BR, TR, TL β†’ corners 0, 1, 3, 1, 2, 3 - if (vIdx == 0) corner = 0; - else if (vIdx == 1) corner = 1; - else if (vIdx == 2) corner = 3; - else if (vIdx == 3) corner = 1; - else if (vIdx == 4) corner = 2; - else corner = 3; - } - - vec2 baseUV; - if (corner == 0) baseUV = vec2(0.0, 1.0); - else if (corner == 1) baseUV = vec2(1.0, 1.0); - else if (corner == 2) baseUV = vec2(1.0, 0.0); - else baseUV = vec2(0.0, 0.0); - - vBaseUV = baseUV; - vWorldPos = aPos; - vWorldNormal = normalize(aNormal); - - // Retail AdjustPlanes bake (terrain.vert:124-134 β€” identical math). - vec3 sunDir = uLights[0].dirAndRange.xyz; - vec3 sunCol = uLights[0].colorAndIntensity.xyz * uLights[0].colorAndIntensity.w; - float L = max(dot(vWorldNormal, -sunDir), MIN_FACTOR); - vLightingRGB = sunCol * L + uCellAmbient.xyz; - - float baseTex = float(aPacked0.x); - if (baseTex >= 254.0) baseTex = -1.0; - vBaseTexIdx = baseTex; - - vOverlay0 = unpackOverlayLayer(aPacked0.z, aPacked0.w, rotOvl0, baseUV); - vOverlay1 = unpackOverlayLayer(aPacked1.x, aPacked1.y, rotOvl1, baseUV); - vOverlay2 = unpackOverlayLayer(aPacked1.z, aPacked1.w, rotOvl2, baseUV); - vRoad0 = unpackOverlayLayer(aPacked2.x, aPacked2.y, rotRd0, baseUV); - vRoad1 = unpackOverlayLayer(aPacked2.z, aPacked2.w, rotRd1, baseUV); - - gl_Position = uProjection * uView * vec4(aPos, 1.0); -} -``` - -- [ ] **Step 4.3: Verify the shader file ships with the project (build copy)** - -Look at `src/AcDream.App/AcDream.App.csproj`. If shader files use `` with `` or a Glob, the new file will be picked up automatically. If shaders are individually listed, add the new file there. - -Run: `dotnet build src/AcDream.App/AcDream.App.csproj -c Debug --nologo` -Expected: 0 errors. (No code touched it; should compile clean.) - -- [ ] **Step 4.4: Commit** - -```bash -git add src/AcDream.App/Rendering/Shaders/terrain_modern.vert -# Also add csproj if it was modified to include the file: -# git add src/AcDream.App/AcDream.App.csproj -git commit -m "$(cat <<'EOF' -phase(N.5b) Task 4: terrain_modern.vert - -Vertex shader for the modern terrain dispatcher. Bit-identical math -to today's terrain.vert (Phase 3c per-cell mesh + Phase G AdjustPlanes -lighting). The only structural change is the version + bindless -extension preamble β€” sampler access stays a regular sampler2DArray -uniform; bindless-ness is invisible at the GLSL level. - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - ---- - -## Task 5: terrain_modern.frag - -**Goal:** Fragment shader for the modern terrain dispatcher. Bit-identical math to today's `terrain.frag` with the same bindless preamble change. - -**Files:** -- Create: `src/AcDream.App/Rendering/Shaders/terrain_modern.frag` - -- [ ] **Step 5.1: Read today's `terrain.frag`** - -Read `src/AcDream.App/Rendering/Shaders/terrain.frag` end-to-end (149 lines). The new shader is a 1:1 port with the same `#version 460 core` + `#extension GL_ARB_bindless_texture : require` preamble change. - -- [ ] **Step 5.2: Write the new shader** - -Create `src/AcDream.App/Rendering/Shaders/terrain_modern.frag`: - -```glsl -#version 460 core -#extension GL_ARB_bindless_texture : require - -// Phase N.5b: terrain fragment shader on the modern bindless dispatcher. -// Math identical to terrain.frag (Phase 3c per-cell maskBlend3 + -// Phase G fog + lightning flash). uTerrain and uAlpha are bound via -// glProgramUniformHandleARB on the C# side; GLSL sampling is unchanged. - -in vec2 vBaseUV; -in vec3 vWorldNormal; -in vec3 vWorldPos; -in vec3 vLightingRGB; -in vec4 vOverlay0; -in vec4 vOverlay1; -in vec4 vOverlay2; -in vec4 vRoad0; -in vec4 vRoad1; -flat in float vBaseTexIdx; - -out vec4 fragColor; - -uniform sampler2DArray uTerrain; -uniform sampler2DArray uAlpha; - -struct Light { - vec4 posAndKind; - vec4 dirAndRange; - vec4 colorAndIntensity; - vec4 coneAngleEtc; -}; -layout(std140, binding = 1) uniform SceneLighting { - Light uLights[8]; - vec4 uCellAmbient; - vec4 uFogParams; - vec4 uFogColor; - vec4 uCameraAndTime; -}; - -const float TILE = 1.0; - -vec4 maskBlend3(vec4 t0, vec4 t1, vec4 t2, float h0, float h1, float h2) { - float a0 = h0 == 0.0 ? 1.0 : t0.a; - float a1 = h1 == 0.0 ? 1.0 : t1.a; - float a2 = h2 == 0.0 ? 1.0 : t2.a; - float aR = 1.0 - (a0 * a1 * a2); - float aRsafe = max(aR, 1e-6); - a0 = 1.0 - a0; - a1 = 1.0 - a1; - a2 = 1.0 - a2; - vec3 r0 = (a0 * t0.rgb + (1.0 - a0) * a1 * t1.rgb + (1.0 - a1) * a2 * t2.rgb); - return vec4(r0 / aRsafe, aR); -} - -vec4 combineOverlays(vec2 baseUV, vec4 pOverlay0, vec4 pOverlay1, vec4 pOverlay2) { - float h0 = pOverlay0.z < 0.0 ? 0.0 : 1.0; - float h1 = pOverlay1.z < 0.0 ? 0.0 : 1.0; - float h2 = pOverlay2.z < 0.0 ? 0.0 : 1.0; - vec4 t0 = vec4(0.0), t1 = vec4(0.0), t2 = vec4(0.0); - - if (h0 > 0.0) { - t0 = texture(uTerrain, vec3(baseUV * TILE, pOverlay0.z)); - if (pOverlay0.w >= 0.0) { - vec4 a = texture(uAlpha, vec3(pOverlay0.xy, pOverlay0.w)); - t0.a = a.a; - } - } - if (h1 > 0.0) { - t1 = texture(uTerrain, vec3(baseUV * TILE, pOverlay1.z)); - if (pOverlay1.w >= 0.0) { - vec4 a = texture(uAlpha, vec3(pOverlay1.xy, pOverlay1.w)); - t1.a = a.a; - } - } - if (h2 > 0.0) { - t2 = texture(uTerrain, vec3(baseUV * TILE, pOverlay2.z)); - if (pOverlay2.w >= 0.0) { - vec4 a = texture(uAlpha, vec3(pOverlay2.xy, pOverlay2.w)); - t2.a = a.a; - } - } - return maskBlend3(t0, t1, t2, h0, h1, h2); -} - -vec4 combineRoad(vec2 baseUV, vec4 pRoad0, vec4 pRoad1) { - float h0 = pRoad0.z < 0.0 ? 0.0 : 1.0; - float h1 = pRoad1.z < 0.0 ? 0.0 : 1.0; - vec4 result = vec4(0.0); - if (h0 > 0.0) { - result = texture(uTerrain, vec3(baseUV * TILE, pRoad0.z)); - if (pRoad0.w >= 0.0) { - vec4 a0 = texture(uAlpha, vec3(pRoad0.xy, pRoad0.w)); - result.a = 1.0 - a0.a; - if (h1 > 0.0 && pRoad1.w >= 0.0) { - vec4 a1 = texture(uAlpha, vec3(pRoad1.xy, pRoad1.w)); - result.a = 1.0 - (a0.a * a1.a); - } - } - } - return result; -} - -vec3 applyFog(vec3 lit, vec3 worldPos) { - int mode = int(uFogParams.w); - if (mode == 0) return lit; - float d = length(worldPos - uCameraAndTime.xyz); - float fogStart = uFogParams.x; - float fogEnd = uFogParams.y; - float span = max(1e-3, fogEnd - fogStart); - float fog = clamp((d - fogStart) / span, 0.0, 1.0); - return mix(lit, uFogColor.xyz, fog); -} - -void main() { - vec4 baseColor = vec4(0.0); - if (vBaseTexIdx >= 0.0) { - baseColor = texture(uTerrain, vec3(vBaseUV * TILE, vBaseTexIdx)); - } - - vec4 overlays = vec4(0.0); - if (vOverlay0.z >= 0.0) - overlays = combineOverlays(vBaseUV, vOverlay0, vOverlay1, vOverlay2); - - vec4 roads = vec4(0.0); - if (vRoad0.z >= 0.0) - roads = combineRoad(vBaseUV, vRoad0, vRoad1); - - vec3 baseMasked = baseColor.rgb * ((1.0 - overlays.a) * (1.0 - roads.a)); - vec3 ovlMasked = overlays.rgb * (overlays.a * (1.0 - roads.a)); - vec3 roadMasked = roads.rgb * roads.a; - vec3 rgb = clamp(baseMasked + ovlMasked + roadMasked, 0.0, 1.0); - - vec3 lit = rgb * min(vLightingRGB, vec3(1.0)); - - float flash = uFogParams.z; - lit += flash * vec3(0.6, 0.6, 0.75); - - lit = applyFog(lit, vWorldPos); - - fragColor = vec4(lit, 1.0); -} -``` - -- [ ] **Step 5.3: Build green** - -Run: `dotnet build src/AcDream.App/AcDream.App.csproj -c Debug --nologo` -Expected: 0 errors. - -- [ ] **Step 5.4: Commit** - -```bash -git add src/AcDream.App/Rendering/Shaders/terrain_modern.frag -# Add csproj if needed for shader copy -git commit -m "$(cat <<'EOF' -phase(N.5b) Task 5: terrain_modern.frag - -Fragment shader for the modern terrain dispatcher. Bit-identical math -to today's terrain.frag (per-cell maskBlend3 + Phase G fog + lightning -flash). Same #version 460 + GL_ARB_bindless_texture preamble change -as terrain_modern.vert. Sampling syntax unchanged β€” the bindless-ness -is invisible at the GLSL level. - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - ---- - -## Task 6: TerrainModernRenderer - -**Goal:** The dispatcher class. Wires `TerrainSlotAllocator` + GL state + bindless atlas handle uniforms + DEIC dispatch via `glMultiDrawElementsIndirect`. Replaces `TerrainChunkRenderer` (drop-in interface). - -**Files:** -- Create: `src/AcDream.App/Rendering/TerrainModernRenderer.cs` - -**Depends on:** Task 1 (`TerrainAtlas.GetBindlessHandles`), Task 2 (`TerrainSlotAllocator`), Task 4 + 5 (shaders). - -- [ ] **Step 6.1: Skim existing pattern** - -Read these files for the pattern this code mirrors: -- `src/AcDream.App/Rendering/TerrainChunkRenderer.cs` β€” current per-chunk pattern (the API surface to match) -- `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` β€” N.5's modern dispatcher (the SSBO + indirect pattern) -- `references/WorldBuilder/Chorizite.OpenGLSDLBackend/Lib/TerrainRenderManager.cs` lines 645-902 β€” WB's terrain dispatcher (the slot allocator + multi-draw indirect pattern; GL calls match what we want) - -- [ ] **Step 6.2: Implement TerrainModernRenderer** - -Create `src/AcDream.App/Rendering/TerrainModernRenderer.cs`: - -```csharp -using System.Numerics; -using AcDream.App.Rendering.Wb; -using AcDream.Core.Terrain; -using Silk.NET.OpenGL; - -namespace AcDream.App.Rendering; - -/// -/// Phase N.5b modern terrain dispatcher. Single global VBO/EBO with a slot -/// allocator (one slot per landblock, 384 verts Γ— 40 bytes = 15,360 bytes -/// per slot). Per-frame: build a DrawElementsIndirectCommand array from -/// visible slots, upload, dispatch via glMultiDrawElementsIndirect. Atlas -/// textures bound via bindless handles set per-frame as sampler uniforms. -/// -/// Total ~6-8 GL calls per frame for terrain regardless of visible -/// landblock count. -/// -public sealed unsafe class TerrainModernRenderer : IDisposable -{ - private const int VertsPerLandblock = LandblockMesh.VerticesPerLandblock; // 384 - private const int IndicesPerLandblock = VertsPerLandblock; - private const int VertexSize = 40; // sizeof(TerrainVertex) - private const int IndexSize = sizeof(uint); - private const float LandblockSize = LandblockMesh.LandblockSize; // 192 - - private readonly GL _gl; - private readonly BindlessSupport _bindless; - private readonly Shader _shader; - private readonly TerrainAtlas _atlas; - - private readonly TerrainSlotAllocator _alloc; - - // Per-slot live data (index by slot integer; null entries are unused slots). - private SlotData?[] _slots; - - // Reverse map: landblockId -> slot, for RemoveLandblock and replacement. - private readonly Dictionary _idToSlot = new(); - - // GPU buffers. - private uint _globalVao; - private uint _globalVbo; - private uint _globalEbo; - private uint _indirectBuffer; - private int _indirectCapacity; - - // Cached sampler-uniform locations (matrix uniforms are set by name via Shader.SetMatrix4). - private int _uTerrainLoc; - private int _uAlphaLoc; - - // Reusable per-frame buffers. - private readonly List _visibleSlots = new(); - private DrawElementsIndirectCommand[] _deicScratch = Array.Empty(); - - // Diag. - public int LoadedSlots => _alloc.LoadedCount; - public int VisibleSlots => _visibleSlots.Count; - public int CapacitySlots => _alloc.Capacity; - - public TerrainModernRenderer( - GL gl, - BindlessSupport bindless, - Shader shader, - TerrainAtlas atlas, - int initialSlotCapacity = 64) - { - _gl = gl; - _bindless = bindless; - _shader = shader; - _atlas = atlas; - _alloc = new TerrainSlotAllocator(initialSlotCapacity); - _slots = new SlotData?[initialSlotCapacity]; - - _uTerrainLoc = _gl.GetUniformLocation(_shader.Program, "uTerrain"); - _uAlphaLoc = _gl.GetUniformLocation(_shader.Program, "uAlpha"); - - _globalVao = _gl.GenVertexArray(); - _globalVbo = _gl.GenBuffer(); - _globalEbo = _gl.GenBuffer(); - AllocateGpuBuffers(initialSlotCapacity); - ConfigureVao(); - - _indirectBuffer = _gl.GenBuffer(); - } - - public void AddLandblock(uint landblockId, LandblockMeshData meshData, Vector3 worldOrigin) - { - ArgumentNullException.ThrowIfNull(meshData); - if (meshData.Vertices.Length != VertsPerLandblock) - throw new ArgumentException( - $"Expected {VertsPerLandblock} vertices, got {meshData.Vertices.Length}", - nameof(meshData)); - - if (_idToSlot.ContainsKey(landblockId)) - RemoveLandblock(landblockId); - - int slot = _alloc.Allocate(out var needsGrow); - if (needsGrow) - { - int newCap = Math.Max(_alloc.Capacity * 2, slot + 1); - EnsureCapacity(newCap); - } - - // Bake worldOrigin into vertex positions; capture min/max Z for AABB. - var bakedVerts = new TerrainVertex[VertsPerLandblock]; - float zMin = float.MaxValue, zMax = float.MinValue; - for (int i = 0; i < VertsPerLandblock; i++) - { - var v = meshData.Vertices[i]; - var worldPos = v.Position + worldOrigin; - bakedVerts[i] = new TerrainVertex(worldPos, v.Normal, v.Data0, v.Data1, v.Data2, v.Data3); - if (worldPos.Z < zMin) zMin = worldPos.Z; - if (worldPos.Z > zMax) zMax = worldPos.Z; - } - if (zMin == float.MaxValue) { zMin = 0f; zMax = 0f; } - - // Bake baseVertex into indices on the CPU side (driver-portable pattern). - uint baseVertex = (uint)(slot * VertsPerLandblock); - var bakedIndices = new uint[IndicesPerLandblock]; - for (int i = 0; i < IndicesPerLandblock; i++) - bakedIndices[i] = meshData.Indices[i] + baseVertex; - - // glBufferSubData into the slot's VBO + EBO regions. - nint vboByteOffset = (nint)(slot * VertsPerLandblock * VertexSize); - nint eboByteOffset = (nint)(slot * IndicesPerLandblock * IndexSize); - - _gl.BindBuffer(BufferTargetARB.ArrayBuffer, _globalVbo); - fixed (TerrainVertex* p = bakedVerts) - { - _gl.BufferSubData(BufferTargetARB.ArrayBuffer, vboByteOffset, - (nuint)(VertsPerLandblock * VertexSize), p); - } - _gl.BindBuffer(BufferTargetARB.ArrayBuffer, 0); - - _gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, _globalEbo); - fixed (uint* p = bakedIndices) - { - _gl.BufferSubData(BufferTargetARB.ElementArrayBuffer, eboByteOffset, - (nuint)(IndicesPerLandblock * IndexSize), p); - } - _gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, 0); - - _slots[slot] = new SlotData - { - LandblockId = landblockId, - WorldOrigin = worldOrigin, - FirstIndex = (uint)(slot * IndicesPerLandblock), - IndexCount = IndicesPerLandblock, - AabbMin = new Vector3(worldOrigin.X, worldOrigin.Y, zMin), - AabbMax = new Vector3(worldOrigin.X + LandblockSize, worldOrigin.Y + LandblockSize, zMax), - }; - _idToSlot[landblockId] = slot; - } - - public void RemoveLandblock(uint landblockId) - { - if (!_idToSlot.TryGetValue(landblockId, out var slot)) - return; - _idToSlot.Remove(landblockId); - _slots[slot] = null; - _alloc.Free(slot); - // No GPU clear: the per-frame DEIC array won't reference this slot. - } - - public void Draw(ICamera camera, FrustumPlanes? frustum = null, uint? neverCullLandblockId = null) - { - if (_alloc.LoadedCount == 0) return; - - // Build visible slot list with per-slot frustum cull. - _visibleSlots.Clear(); - for (int slot = 0; slot < _slots.Length; slot++) - { - var data = _slots[slot]; - if (data is null) continue; - if (frustum is not null && data.LandblockId != neverCullLandblockId) - { - if (!FrustumCuller.IsAabbVisible(frustum.Value, data.AabbMin, data.AabbMax)) - continue; - } - _visibleSlots.Add(slot); - } - if (_visibleSlots.Count == 0) return; - - // Build DEIC array. - if (_deicScratch.Length < _visibleSlots.Count) - _deicScratch = new DrawElementsIndirectCommand[Math.Max(_visibleSlots.Count, 64)]; - for (int i = 0; i < _visibleSlots.Count; i++) - { - var data = _slots[_visibleSlots[i]]!; - _deicScratch[i] = new DrawElementsIndirectCommand - { - Count = (uint)data.IndexCount, - InstanceCount = 1u, - FirstIndex = data.FirstIndex, - BaseVertex = 0, // baked into indices on upload - BaseInstance = 0, - }; - } - - // Grow indirect buffer if needed. - if (_visibleSlots.Count > _indirectCapacity) - { - _indirectCapacity = Math.Max(64, _visibleSlots.Count * 2); - _gl.BindBuffer(GLEnum.DrawIndirectBuffer, _indirectBuffer); - _gl.BufferData(GLEnum.DrawIndirectBuffer, - (nuint)(_indirectCapacity * sizeof(DrawElementsIndirectCommand)), - null, GLEnum.DynamicDraw); - } - else - { - _gl.BindBuffer(GLEnum.DrawIndirectBuffer, _indirectBuffer); - } - - // Upload DEIC array. - fixed (DrawElementsIndirectCommand* p = _deicScratch) - { - _gl.BufferSubData(GLEnum.DrawIndirectBuffer, 0, - (nuint)(_visibleSlots.Count * sizeof(DrawElementsIndirectCommand)), p); - } - - // Bind shader + uniforms + atlas handles. - _shader.Use(); - _shader.SetMatrix4("uView", camera.View); - _shader.SetMatrix4("uProjection", camera.Projection); - - var (terrainHandle, alphaHandle) = _atlas.GetBindlessHandles(); - _bindless.SetSamplerHandleUniform(_shader.Program, _uTerrainLoc, terrainHandle); - _bindless.SetSamplerHandleUniform(_shader.Program, _uAlphaLoc, alphaHandle); - - _gl.BindVertexArray(_globalVao); - _gl.MemoryBarrier(MemoryBarrierMask.CommandBarrierBit); - _gl.MultiDrawElementsIndirect( - PrimitiveType.Triangles, DrawElementsType.UnsignedInt, - (void*)0, - (uint)_visibleSlots.Count, - (uint)sizeof(DrawElementsIndirectCommand)); - _gl.BindVertexArray(0); - _gl.BindBuffer(GLEnum.DrawIndirectBuffer, 0); - } - - public void Dispose() - { - _gl.DeleteVertexArray(_globalVao); - _gl.DeleteBuffer(_globalVbo); - _gl.DeleteBuffer(_globalEbo); - _gl.DeleteBuffer(_indirectBuffer); - } - - // ---------------------------------------------------------------- - // Private helpers - // ---------------------------------------------------------------- - - private void AllocateGpuBuffers(int capacitySlots) - { - nuint vboBytes = (nuint)(capacitySlots * VertsPerLandblock * VertexSize); - nuint eboBytes = (nuint)(capacitySlots * IndicesPerLandblock * IndexSize); - - _gl.BindBuffer(BufferTargetARB.ArrayBuffer, _globalVbo); - _gl.BufferData(BufferTargetARB.ArrayBuffer, vboBytes, null, BufferUsageARB.DynamicDraw); - _gl.BindBuffer(BufferTargetARB.ArrayBuffer, 0); - - _gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, _globalEbo); - _gl.BufferData(BufferTargetARB.ElementArrayBuffer, eboBytes, null, BufferUsageARB.DynamicDraw); - _gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, 0); - } - - private void ConfigureVao() - { - _gl.BindVertexArray(_globalVao); - _gl.BindBuffer(BufferTargetARB.ArrayBuffer, _globalVbo); - _gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, _globalEbo); - - uint stride = (uint)VertexSize; - - // location 0: Position - _gl.EnableVertexAttribArray(0); - _gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, stride, (void*)0); - // location 1: Normal - _gl.EnableVertexAttribArray(1); - _gl.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, stride, (void*)(3 * sizeof(float))); - // locations 2-5: Data0..Data3 (uvec4 byte attributes) - nint dataOffset = 6 * sizeof(float); - _gl.EnableVertexAttribArray(2); - _gl.VertexAttribIPointer(2, 4, VertexAttribIType.UnsignedByte, stride, (void*)dataOffset); - _gl.EnableVertexAttribArray(3); - _gl.VertexAttribIPointer(3, 4, VertexAttribIType.UnsignedByte, stride, (void*)(dataOffset + 4)); - _gl.EnableVertexAttribArray(4); - _gl.VertexAttribIPointer(4, 4, VertexAttribIType.UnsignedByte, stride, (void*)(dataOffset + 8)); - _gl.EnableVertexAttribArray(5); - _gl.VertexAttribIPointer(5, 4, VertexAttribIType.UnsignedByte, stride, (void*)(dataOffset + 12)); - - _gl.BindVertexArray(0); - } - - private void EnsureCapacity(int newCapacity) - { - if (newCapacity <= _alloc.Capacity) return; - - // Allocate new VBO + EBO at new size; copy old contents; swap; recreate VAO. - uint newVbo = _gl.GenBuffer(); - uint newEbo = _gl.GenBuffer(); - - nuint newVboBytes = (nuint)(newCapacity * VertsPerLandblock * VertexSize); - nuint newEboBytes = (nuint)(newCapacity * IndicesPerLandblock * IndexSize); - nuint oldVboBytes = (nuint)(_alloc.Capacity * VertsPerLandblock * VertexSize); - nuint oldEboBytes = (nuint)(_alloc.Capacity * IndicesPerLandblock * IndexSize); - - _gl.BindBuffer(BufferTargetARB.ArrayBuffer, newVbo); - _gl.BufferData(BufferTargetARB.ArrayBuffer, newVboBytes, null, BufferUsageARB.DynamicDraw); - _gl.BindBuffer(BufferTargetARB.CopyReadBuffer, _globalVbo); - _gl.BindBuffer(BufferTargetARB.CopyWriteBuffer, newVbo); - _gl.CopyBufferSubData(CopyBufferSubDataTarget.CopyReadBuffer, CopyBufferSubDataTarget.CopyWriteBuffer, - 0, 0, oldVboBytes); - _gl.DeleteBuffer(_globalVbo); - _globalVbo = newVbo; - - _gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, newEbo); - _gl.BufferData(BufferTargetARB.ElementArrayBuffer, newEboBytes, null, BufferUsageARB.DynamicDraw); - _gl.BindBuffer(BufferTargetARB.CopyReadBuffer, _globalEbo); - _gl.BindBuffer(BufferTargetARB.CopyWriteBuffer, newEbo); - _gl.CopyBufferSubData(CopyBufferSubDataTarget.CopyReadBuffer, CopyBufferSubDataTarget.CopyWriteBuffer, - 0, 0, oldEboBytes); - _gl.DeleteBuffer(_globalEbo); - _globalEbo = newEbo; - - // Recreate VAO with new buffer bindings. - _gl.DeleteVertexArray(_globalVao); - _globalVao = _gl.GenVertexArray(); - ConfigureVao(); - - // Grow slot tracking array. - Array.Resize(ref _slots, newCapacity); - _alloc.GrowTo(newCapacity); - } - - private sealed class SlotData - { - public uint LandblockId; - public Vector3 WorldOrigin; - public uint FirstIndex; - public int IndexCount; - public Vector3 AabbMin; - public Vector3 AabbMax; - } -} -``` - -- [ ] **Step 6.3: Add `SetSamplerHandleUniform` helper to BindlessSupport** - -The renderer calls `_bindless.SetSamplerHandleUniform(...)` which doesn't exist yet. Add it to `src/AcDream.App/Rendering/Wb/BindlessSupport.cs`: - -After the `MakeNonResident` method (around line 46), add: - -```csharp -/// -/// Set a sampler-typed uniform from a 64-bit bindless handle. Uses -/// glProgramUniformHandleARB so it doesn't require the program to be bound. -/// -public void SetSamplerHandleUniform(uint program, int location, ulong handle) -{ - _ext.ProgramUniformHandle(program, location, handle); -} -``` - -- [ ] **Step 6.4: Build green** - -Run: `dotnet build src/AcDream.App/AcDream.App.csproj -c Debug --nologo` -Expected: 0 errors. (`Silk.NET.OpenGL.Extensions.ARB.ArbBindlessTexture` already provides `ProgramUniformHandle`.) - -If the Silk.NET method name differs (e.g. `ProgramUniformHandleARB` vs `ProgramUniformHandle`), check `using Silk.NET.OpenGL.Extensions.ARB;` IntelliSense and use the correct name. - -- [ ] **Step 6.5: Commit** - -```bash -git add src/AcDream.App/Rendering/TerrainModernRenderer.cs src/AcDream.App/Rendering/Wb/BindlessSupport.cs -git commit -m "$(cat <<'EOF' -phase(N.5b) Task 6: TerrainModernRenderer - -The new terrain dispatcher. Single global VBO/EBO with a slot -allocator (one slot per landblock, 384 verts Γ— 40 bytes per slot). -Per-frame: build DEIC array from visible slots, upload, dispatch -via glMultiDrawElementsIndirect. Atlas textures bound via bindless -handles set per-frame as sampler uniforms. - -Total ~6-8 GL calls per frame for terrain regardless of visible -landblock count (vs today's per-LB binds at radius=2 β†’ ~25 calls, -radius=5 β†’ ~121 calls). - -API mirrors TerrainChunkRenderer so GameWindow integration in T8 is -a drop-in field+ctor swap. - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - ---- - -## Task 7: TerrainModernConformanceTests - -**Goal:** Z-conformance sentinel for issue #51's bug class. Sweeps ~10 representative landblocks Γ— ~100 sample points; asserts `|meshTriZ - TerrainSurface.SampleZFromHeightmap| < 0.001m`. - -**Files:** -- Create: `tests/AcDream.Core.Tests/Terrain/TerrainModernConformanceTests.cs` - -**Independence note:** This test uses `LandblockMesh.Build` directly (the source-of-truth generator that `TerrainModernRenderer` consumes internally). The test runs without GL and is independent of T6 β€” it can land in parallel with T1, T2, T4, T5. - -- [ ] **Step 7.1: Read the existing `ClientConformanceTests.cs` for the dat-loading pattern** - -Run: `cat tests/AcDream.Core.Tests/Terrain/ClientConformanceTests.cs | head -80` - -This shows the existing pattern for loading dat heightmap data in tests. Use the same `DatCollection` setup + `Region` fetch pattern. - -- [ ] **Step 7.2: Write the conformance test** - -Create `tests/AcDream.Core.Tests/Terrain/TerrainModernConformanceTests.cs`: - -```csharp -using System; -using System.Collections.Generic; -using System.IO; -using System.Numerics; -using AcDream.Core.Physics; -using AcDream.Core.Terrain; -using DatReaderWriter; -using DatReaderWriter.DBObjs; -using Xunit; -using Xunit.Abstractions; - -namespace AcDream.Core.Tests.Terrain; - -/// -/// Phase N.5b Z-conformance sentinel: proves that the visual terrain mesh -/// produced by agrees with the physics-side -/// at arbitrary (X, Y) -/// within 1 mm. This is the exact bug class issue #51 names β€” if a future -/// refactor silently changes formula or vertex layout in either path, -/// this test fires before the player floats above (or sinks below) the -/// visible ground. -/// -public class TerrainModernConformanceTests -{ - private readonly ITestOutputHelper _out; - - public TerrainModernConformanceTests(ITestOutputHelper output) => _out = output; - - private static readonly (string name, uint lbX, uint lbY)[] RepresentativeLandblocks = - { - ("Holtburg flat 0xA9B0", 0xA9, 0xB0), - ("Holtburg sloped 0xA9B1", 0xA9, 0xB1), - ("Foundry-area 0x8080", 0x80, 0x80), - ("Cragstone 0xCB99", 0xCB, 0x99), - ("Direlands sample 0xC040", 0xC0, 0x40), - ("MapOrigin 0x0000", 0x00, 0x00), - ("Mid-map 0x7F7F", 0x7F, 0x7F), - ("MapCorner 0xFEFE", 0xFE, 0xFE), - ("Subway outdoor 0x0185", 0x01, 0x85), - ("North continent 0x4D96", 0x4D, 0x96), // worst-case landblock from divergence test - }; - - [Fact] - public void VisualMeshZ_AgreesWith_PhysicsZ_WithinOneMillimeter() - { - var datDir = Environment.GetEnvironmentVariable("ACDREAM_DAT_DIR") - ?? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), - "Documents", "Asheron's Call"); - if (!Directory.Exists(datDir)) - { - _out.WriteLine($"SKIP: dat directory not found at {datDir}"); - return; - } - - using var dats = new DatCollection(datDir); - var region = dats.Get(0x13000000u); - Assert.NotNull(region); - var heightTable = region.LandDefs.LandHeightTable; - - long totalSamples = 0; - long totalLandblocksTested = 0; - double maxDelta = 0; - (string name, uint lbX, uint lbY, float lx, float ly, float meshZ, float physicsZ) worstCase = default; - - var rng = new Random(seed: 42); // fixed seed for reproducible sample distribution - - foreach (var (name, lbX, lbY) in RepresentativeLandblocks) - { - uint landblockId = (lbX << 24) | (lbY << 16) | 0xFFFFu; - var landblock = dats.Get(landblockId); - if (landblock is null) - { - _out.WriteLine($" skipped {name}: dat not found (probably water-only)"); - continue; - } - totalLandblocksTested++; - - // Compute mesh via the source-of-truth generator. Empty surfaceCache - // is fine β€” test only cares about vertex Z values. - var ctx = TerrainBlendingContext.Empty; // see Note below if this constructor doesn't exist - var surfaceCache = new Dictionary(); - var meshData = LandblockMesh.Build(landblock, lbX, lbY, heightTable, ctx, surfaceCache); - - // Sample 100 (localX, localY) points uniformly + edge cases. - for (int s = 0; s < 100; s++) - { - float lx = (float)rng.NextDouble() * 192f; - float ly = (float)rng.NextDouble() * 192f; - - float meshZ = SampleMeshZ(meshData, lx, ly); - float physicsZ = TerrainSurface.SampleZFromHeightmap( - landblock.Height, heightTable, lbX, lbY, lx, ly); - - double delta = Math.Abs(meshZ - physicsZ); - if (delta > maxDelta) - { - maxDelta = delta; - worstCase = (name, lbX, lbY, lx, ly, meshZ, physicsZ); - } - totalSamples++; - Assert.True(delta < 0.001, - $"Mesh Z disagrees with physics Z at lb=0x{lbX:X2}{lbY:X2} ({name}) " + - $"local=({lx:F2},{ly:F2}): meshZ={meshZ:F4} physicsZ={physicsZ:F4} delta={delta:F4}m"); - } - } - - _out.WriteLine($"=== Phase N.5b conformance sweep ==="); - _out.WriteLine($"Landblocks tested: {totalLandblocksTested}/{RepresentativeLandblocks.Length}"); - _out.WriteLine($"Total samples: {totalSamples}"); - _out.WriteLine($"Max |delta|: {maxDelta * 1000:F4} mm (tolerance: 1.0 mm)"); - if (totalSamples > 0) - _out.WriteLine($"Worst case: {worstCase.name} local=({worstCase.lx:F2},{worstCase.ly:F2}) " + - $"meshZ={worstCase.meshZ:F4} physicsZ={worstCase.physicsZ:F4}"); - - Assert.True(totalLandblocksTested >= 5, - $"Expected at least 5 representative landblocks loadable; got {totalLandblocksTested}."); - } - - /// - /// Sample the mesh's triangle-interpolated Z at (localX, localY). Walks - /// the mesh's triangles (3 indices each), tests point-in-triangle in 2D, - /// and barycentric-interpolates Z from the matching triangle's three Zs. - /// - private static float SampleMeshZ(LandblockMeshData mesh, float lx, float ly) - { - for (int triBase = 0; triBase < mesh.Indices.Length; triBase += 3) - { - var v0 = mesh.Vertices[mesh.Indices[triBase + 0]]; - var v1 = mesh.Vertices[mesh.Indices[triBase + 1]]; - var v2 = mesh.Vertices[mesh.Indices[triBase + 2]]; - - // Barycentric coords for (lx, ly) wrt triangle v0/v1/v2 in 2D. - float denom = (v1.Position.Y - v2.Position.Y) * (v0.Position.X - v2.Position.X) - + (v2.Position.X - v1.Position.X) * (v0.Position.Y - v2.Position.Y); - if (Math.Abs(denom) < 1e-9f) continue; - - float a = ((v1.Position.Y - v2.Position.Y) * (lx - v2.Position.X) - + (v2.Position.X - v1.Position.X) * (ly - v2.Position.Y)) / denom; - float b = ((v2.Position.Y - v0.Position.Y) * (lx - v2.Position.X) - + (v0.Position.X - v2.Position.X) * (ly - v2.Position.Y)) / denom; - float c = 1f - a - b; - - // Inside test with epsilon for boundary stability. - const float eps = 1e-4f; - if (a >= -eps && b >= -eps && c >= -eps) - return a * v0.Position.Z + b * v1.Position.Z + c * v2.Position.Z; - } - - // Should not happen for valid mesh + in-bounds (lx, ly). - throw new InvalidOperationException( - $"No triangle found containing local=({lx:F2},{ly:F2}); mesh has {mesh.Indices.Length / 3} triangles."); - } -} -``` - -**Note on `TerrainBlendingContext.Empty`:** if this static doesn't exist, construct a minimal one: - -```csharp -var ctx = new TerrainBlendingContext( - terrainTypeToLayer: new Dictionary(), - cornerAlphaLayers: Array.Empty(), - sideAlphaLayers: Array.Empty(), - roadAlphaLayers: Array.Empty(), - cornerAlphaTCodes: Array.Empty(), - sideAlphaTCodes: Array.Empty(), - roadAlphaRCodes: Array.Empty(), - roadLayer: SurfaceInfo.None); -``` - -(Check `src/AcDream.Core/Terrain/TerrainBlendingContext.cs` for the actual signature.) - -- [ ] **Step 7.3: Run the conformance test** - -Run: `dotnet test --filter "FullyQualifiedName~TerrainModernConformanceTests" --nologo --logger "console;verbosity=detailed"` - -Expected outcomes: -- If dat dir present: PASS with `Max |delta|: <1.0 mm` printed. -- If dat dir absent: PASS with `SKIP: dat directory not found` (test gracefully skips). - -If the test FAILS with a delta > 1mm, the visual mesh and physics surface have drifted β€” investigate before proceeding. - -- [ ] **Step 7.4: Commit** - -```bash -git add tests/AcDream.Core.Tests/Terrain/TerrainModernConformanceTests.cs -git commit -m "$(cat <<'EOF' -phase(N.5b) Task 7: TerrainModernConformanceTests - -Z-conformance sentinel for issue #51's bug class. Sweeps 10 -representative landblocks Γ— 100 sample points (uniform random in -local 0..192 with fixed seed). For each point: compute meshTriZ -via barycentric interpolation in the matching triangle of the -LandblockMesh.Build output; compute physicsZ via -TerrainSurface.SampleZFromHeightmap; assert |delta| < 0.001m. - -Catches any silent formula or vertex-layout drift between the -visual and physics paths. Skips gracefully if ACDREAM_DAT_DIR -isn't set (CI without dat data). - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - ---- - -## Task 8: GameWindow integration - -**Goal:** Swap `TerrainChunkRenderer` β†’ `TerrainModernRenderer` at the field declaration + construction site. Wire `[TERRAIN-DIAG]` rollup callback. - -**Files:** -- Modify: `src/AcDream.App/Rendering/GameWindow.cs` - -**Depends on:** Task 6. - -- [ ] **Step 8.1: Locate the field + ctor + diag wiring** - -```bash -grep -n "TerrainChunkRenderer\|_terrain" src/AcDream.App/Rendering/GameWindow.cs | head -20 -``` - -The field declaration is at line 21; the ctor is at line 1391. The diag rollup pattern lives near the existing `[WB-DIAG]` writes β€” search for `WB-DIAG`. - -- [ ] **Step 8.2: Swap field type** - -In `src/AcDream.App/Rendering/GameWindow.cs:21`, change: - -```csharp -private TerrainChunkRenderer? _terrain; -``` - -to: - -```csharp -private TerrainModernRenderer? _terrain; -``` - -- [ ] **Step 8.3: Swap ctor call (and pass BindlessSupport to TerrainAtlas)** - -At line 1391: - -```csharp -_terrain = new TerrainChunkRenderer(_gl, _shader, terrainAtlas); -``` - -Becomes: - -```csharp -_terrain = new TerrainModernRenderer(_gl, _bindless, _terrainModernShader, terrainAtlas); -``` - -(The `_bindless` field already exists from N.5; the shader field name may need to be created/loaded β€” see step 8.4.) - -You also need to ensure `terrainAtlas` was constructed with `BindlessSupport`. Find the `TerrainAtlas.Build(gl, dats)` call upstream and change to `TerrainAtlas.Build(gl, dats, _bindless)`. - -- [ ] **Step 8.4: Load the new shader** - -Find where `terrain.vert/.frag` are currently loaded into a `Shader` object. Add a parallel load for `terrain_modern.vert/.frag` into a new `_terrainModernShader` field. Pattern should mirror how `mesh_modern` shaders were loaded in N.5 (search GameWindow for `mesh_modern` to find the template). - -- [ ] **Step 8.5: Add `[TERRAIN-DIAG]` rollup** - -Find where `[WB-DIAG]` is logged. Add a parallel `[TERRAIN-DIAG]` line: - -```csharp -Console.WriteLine( - $"[TERRAIN-DIAG] cpu_ms={terrainCpuMedianMs:F2}/{terrainCpu95thMs:F2} " + - $"draws={_terrain?.VisibleSlots ?? 0}/frame " + - $"visible={_terrain?.VisibleSlots ?? 0} " + - $"loaded={_terrain?.LoadedSlots ?? 0} " + - $"capacity={_terrain?.CapacitySlots ?? 0}"); -``` - -To capture `terrainCpuMedianMs` / `terrainCpu95thMs`, wrap the `_terrain.Draw(...)` call in a `Stopwatch` and accumulate samples into a 5-second rolling buffer. Mirror the existing `[WB-DIAG]` accumulator (search GameWindow for `Stopwatch` + `cpu_ms`). - -- [ ] **Step 8.6: Build + run the client** - -Run: `dotnet build src/AcDream.App/AcDream.App.csproj -c Debug --nologo` -Expected: 0 errors. - -Launch the client (PowerShell): - -```powershell -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -$env:ACDREAM_TEST_HOST = "127.0.0.1" -$env:ACDREAM_TEST_PORT = "9000" -$env:ACDREAM_TEST_USER = "testaccount" -$env:ACDREAM_TEST_PASS = "testpassword" -$env:ACDREAM_WB_DIAG = "1" -dotnet run --project src\AcDream.App\AcDream.App.csproj --no-build -c Debug 2>&1 | Tee-Object -FilePath launch.log -``` - -Wait ~10 seconds for in-world. Confirm: -- Terrain renders (no black ground) -- `launch.log` contains `[TERRAIN-DIAG]` lines - -If terrain is black or missing, check: -- `[WB-DIAG]` β€” bindless capability detected? -- Atlas handle nonzero? -- `glGetError()` after `glMultiDrawElementsIndirect`? - -- [ ] **Step 8.7: Commit (initial integration; visual gate is next)** - -```bash -git add src/AcDream.App/Rendering/GameWindow.cs -git commit -m "$(cat <<'EOF' -phase(N.5b): wire TerrainModernRenderer into GameWindow - -Swap TerrainChunkRenderer β†’ TerrainModernRenderer (drop-in: same -AddLandblock/RemoveLandblock/Draw interface). Pass BindlessSupport -to TerrainAtlas.Build so GetBindlessHandles() is callable. Load the -new terrain_modern shader pair and pass to the renderer ctor. Add -[TERRAIN-DIAG] rollup mirroring the existing [WB-DIAG] pattern. - -Visual verification at four scenes (Holtburg flat + sloped, Foundry, -sloped landblock) is the next gate. - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - ---- - -## USER VERIFICATION GATE β€” visual checks - -**Block here. Do not proceed to T9/T10 until the user confirms all checks at all four scenes.** - -User runs the client per the launch command in step 8.6, drives the character through: - -1. **Holtburg town** (~0xA9B0) -2. **Holtburg sloped landblock** (~0xA9B1) -3. **Foundry-area** (~0x80xx) -4. **Any visibly-sloped outdoor landblock** - -At each scene confirm: - -1. βœ“ No cell-boundary wobble (load-bearing #51 sentinel) -2. βœ“ No missing chunks / black holes -3. βœ“ No texture seams at landblock edges -4. βœ“ No z-fighting -5. βœ“ `[TERRAIN-DIAG] visible=N` consistent with scene; renderer visibly using indirect dispatch (no per-LB calls) -6. βœ“ `[TERRAIN-DIAG] cpu_ms` at radius=5 β‰₯10% lower than the recorded baseline - -If any check fails, fix in place, re-verify, repeat. Only after **all six checks pass at all four scenes** proceed to Tasks 9 + 10. - ---- - -## Task 9: Delete legacy - -**Goal:** Remove the now-unused `TerrainChunkRenderer`, `TerrainRenderer`, and the old shader files. - -**Files:** -- Delete: `src/AcDream.App/Rendering/TerrainChunkRenderer.cs` -- Delete: `src/AcDream.App/Rendering/TerrainRenderer.cs` -- Delete: `src/AcDream.App/Rendering/Shaders/terrain.vert` -- Delete: `src/AcDream.App/Rendering/Shaders/terrain.frag` - -- [ ] **Step 9.1: Delete the files** - -```bash -git rm src/AcDream.App/Rendering/TerrainChunkRenderer.cs -git rm src/AcDream.App/Rendering/TerrainRenderer.cs -git rm src/AcDream.App/Rendering/Shaders/terrain.vert -git rm src/AcDream.App/Rendering/Shaders/terrain.frag -``` - -- [ ] **Step 9.2: Build green (verify nothing else referenced these)** - -Run: `dotnet build src/AcDream.App/AcDream.App.csproj -c Debug --nologo` -Expected: 0 errors. - -If references break in unexpected places, restore the files (`git checkout HEAD -- ...`) and find/delete the references first, then re-attempt. - -- [ ] **Step 9.3: Run the full N.5 + N.5b test filter to confirm nothing regressed** - -Run: - -```bash -dotnet test --filter "FullyQualifiedName~Wb|FullyQualifiedName~MatrixComposition|FullyQualifiedName~TextureCacheBindless|FullyQualifiedName~TerrainSlot|FullyQualifiedName~TerrainModernConformance|FullyQualifiedName~TerrainBlending|FullyQualifiedName~LandblockMesh|FullyQualifiedName~SplitFormulaDivergence" --nologo -``` - -Expected: all green. - -- [ ] **Step 9.4: Commit** - -```bash -git commit -m "$(cat <<'EOF' -phase(N.5b): retire legacy terrain renderers - -Deletes: -- TerrainChunkRenderer.cs (454 lines, replaced by TerrainModernRenderer) -- TerrainRenderer.cs (247 lines, older sibling, no production users) -- terrain.vert / terrain.frag (replaced by terrain_modern.{vert,frag}) - -The modern path is now the only path. Mirror N.5's mandatory-modern -amendment: missing GL_ARB_bindless_texture throws NotSupportedException -at startup (already in place via the BindlessSupport.TryCreate gate). - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - ---- - -## Task 10: Roadmap + ISSUES + memory + perf baseline - -**Goal:** Close out the phase. Update the roadmap, close issue #51, write the memory file, capture perf numbers in a baseline doc. - -**Files:** -- Modify: `docs/plans/2026-04-11-roadmap.md` -- Modify: `docs/ISSUES.md` -- Modify: `CLAUDE.md` -- Create: `docs/plans/2026-05-09-phase-n5b-perf-baseline.md` -- Create: `~/.claude/projects/C--Users-erikn-source-repos-acdream/memory/project_phase_n5b_state.md` - -- [ ] **Step 10.1: Roadmap entry** - -Read `docs/plans/2026-04-11-roadmap.md`. Add an N.5b row to the "Shipped" table (mirror the N.5 row's format). Remove "terrain on modern path" from the N.6 scope notes. - -- [ ] **Step 10.2: Close issue #51** - -In `docs/ISSUES.md`, move issue #51 from the OPEN section to "Recently closed" with the SHIP commit SHA. Note: the resolution was Path C (kept retail's formula via `LandblockMesh.Build`; never adopted WB's formula). - -- [ ] **Step 10.3: Update CLAUDE.md "WB integration cribs"** - -Add an entry under the existing "WB integration cribs" bullet list: - -```markdown -- `src/AcDream.App/Rendering/TerrainModernRenderer.cs` β€” terrain dispatcher - on N.5's modern primitives. Mirrors WB's `TerrainRenderManager` pattern - (single global VBO/EBO + slot allocator + `glMultiDrawElementsIndirect`) - but driven by acdream's `LandblockMesh.Build` so retail's `FSplitNESW` - formula is preserved (issue #51). ~6-8 GL calls/frame for terrain - regardless of scene size. -``` - -- [ ] **Step 10.4: Write the perf baseline doc** - -Create `docs/plans/2026-05-09-phase-n5b-perf-baseline.md` with the before/after numbers from the user verification gate: - -```markdown -# Phase N.5b β€” terrain perf baseline - -## Test scene -- Holtburg town (~0xA9B0), radius=5, default settings. -- Captured 5-second `[TERRAIN-DIAG]` rollup median + 95th. - -## Before (TerrainChunkRenderer) -- Terrain GL calls / frame: -- CPU dispatcher cpu_ms median: -- CPU dispatcher cpu_ms 95th : - -## After (TerrainModernRenderer) -- Terrain GL calls / frame: -- CPU dispatcher cpu_ms median: -- CPU dispatcher cpu_ms 95th : - -## Reduction -- GL calls: β†’ (~Z% reduction) -- CPU median: ms β†’ ms (~Z% reduction) - -## Acceptance -- Acceptance criterion 5 (β‰₯10% CPU reduction at radius=5): -``` - -- [ ] **Step 10.5: Write the memory file** - -Create `~/.claude/projects/C--Users-erikn-source-repos-acdream/memory/project_phase_n5b_state.md`: - -```markdown ---- -name: "Project: Phase N.5b state (shipped 2026-MM-DD)" -description: N.5b lifted terrain rendering onto bindless + multi-draw indirect via Path C (WB's renderer pattern, acdream's LandblockMesh.Build for retail formula compliance). ~6-8 GL calls/frame for terrain. Closes issue #51. -type: project ---- -**Phase N.5b β€” Terrain on the Modern Rendering Path β€” shipped 2026-MM-DD.** - -`TerrainModernRenderer` replaces `TerrainChunkRenderer` (deleted along -with `TerrainRenderer` + `terrain.vert/.frag`). Single global VBO/EBO -with slot allocator (one slot per landblock); per-frame DEIC array -upload + `glMultiDrawElementsIndirect`; bindless atlas handles set -per-frame as sampler uniforms. - -**Path C** (chosen during brainstorm): mirror WB's renderer pattern -but consume `LandblockMesh.Build` (which uses retail's `FSplitNESW` -formula). Path A killed by 49.98% measured divergence between WB's -formula and retail's at retail addr `00531d10`. Path B (fork-patch -WB) rejected for permanent maintenance burden. - -Closes issue #51 (visual ↔ physics terrain Z agreement). - -**Why:** N.5b completes the rendering modernization for outdoor -content. Together with N.5 entity rendering, every visible -gameplay-area surface now flows through `glMultiDrawElementsIndirect`. -EnvCells (interiors), sky, particles still on legacy renderers -pending later phases. - -**How to apply:** when working on terrain rendering, the modern path -is now the only path. The split formula is locked to retail's -`FSplitNESW` via `TerrainBlending.CalculateSplitDirection`; do NOT -substitute WB's `TerrainUtils.CalculateSplitDirection` (49.98% wrong -per the divergence test). - -## Gotchas surfaced during N.5b implementation - -(Fill in any high-value, non-obvious lessons that surfaced during -implementation. If nothing surfaced beyond what N.5's gotchas -already cover, note that explicitly.) -``` - -Then add a one-line entry to the memory index at `~/.claude/projects/C--Users-erikn-source-repos-acdream/memory/MEMORY.md`: - -```markdown -- [Project: Phase N.5b state](project_phase_n5b_state.md) β€” N.5b SHIPPED YYYY-MM-DD. Terrain on bindless + multi-draw indirect via Path C. Closes #51. -``` - -- [ ] **Step 10.6: Final SHIP commit** - -```bash -git add docs/plans/2026-04-11-roadmap.md docs/ISSUES.md CLAUDE.md docs/plans/2026-05-09-phase-n5b-perf-baseline.md -# Memory file is outside the repo, skip git for it -git commit -m "$(cat <<'EOF' -phase(N.5b): SHIP β€” terrain on modern rendering path - -TerrainModernRenderer replaces TerrainChunkRenderer + TerrainRenderer. -Single global VBO/EBO + slot allocator + glMultiDrawElementsIndirect -+ bindless atlas handles. ~6-8 GL calls/frame for terrain regardless -of scene size. - -Path C: WB renderer pattern + acdream's LandblockMesh.Build (retail's -FSplitNESW formula preserved per #51). Path A killed by 49.98% -measured divergence vs retail; Path B (fork-patch WB) rejected for -maintenance burden. - -Perf at radius=5 (Holtburg): . -See docs/plans/2026-05-09-phase-n5b-perf-baseline.md. - -Visual verification: confirmed at 4 outdoor scenes (Holtburg flat + -sloped, Foundry-area, sloped landblock). No cell-boundary wobble. - -Closes issue #51. - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - ---- - -## Self-review checklist - -After all tasks land, sanity-check: - -- [x] Build green: `dotnet build` -- [x] All N.5 + N.5b tests green: 114/114 in the filter (Wb, MatrixComposition, TextureCacheBindless, TerrainSlot, TerrainModernConformance, TerrainBlending, LandblockMesh, SplitFormulaDivergence) -- [x] Visual verification: terrain renders correctly in modern path (after the black-terrain hotfix at `da56063`) -- [x] Issue #51 closed in `docs/ISSUES.md` (T10 commit `083c10c`) -- [x] Roadmap shows N.5b in "Shipped" (T10 commit `083c10c`) -- [x] Memory file written (`memory/project_phase_n5b_state.md` outside repo) -- [x] Perf baseline doc has real before/after numbers (`docs/plans/2026-05-09-phase-n5b-perf-baseline.md`) -- [N/A] **CPU dispatcher reduction β‰₯10% at radius=5** β€” captured measurement showed modern is ~4Γ— SLOWER on CPU at radius=5 in Holtburg. The chunked legacy renderer collapsed radius=5 to one `glDrawElements` call, so the multi-draw indirect savings don't apply at this scene size. **Acceptance criterion #5 is amended via the perf baseline doc**: ship N.5b on visual identity + structural correctness rather than CPU savings. Architectural wins (zero `glBindTexture`/frame; constant-cost dispatch as A.5 raises radius) are real but only manifest at higher scene complexity. - ---- - -## SHIP record β€” 2026-05-09 - -**Phase N.5b β€” Terrain on the Modern Rendering Path β€” SHIPPED.** - -### Commit chain - -``` -083c10c docs(N.5b T10): roadmap + ISSUES + CLAUDE.md + perf baseline updates -7dfa2af phase(N.5b): retire legacy terrain renderers -da56063 fix(N.5b): black terrain β€” switch to uvec2 handle + sampler constructor -55e516c fix(N.5b T8): TerrainDiagMedian/P95 IndexOutOfRangeException on first flush -336ad34 chore(N.5b): TEMPORARY perf benchmark toggle for legacy↔modern terrain -75913c1 phase(N.5b): wire TerrainModernRenderer into GameWindow -3418f65 fix(N.5b T6): index-length validation + document VertsPerLandblock %6 invariant -0a77bd1 phase(N.5b) Task 6: TerrainModernRenderer -4ed7920 fix(N.5b T7): tighten conformance sample upper bound to 191.975f -e54d5ca phase(N.5b) Task 7: TerrainModernConformanceTests -1ea00a0 phase(N.5b) Task 5: terrain_modern.frag -3c108a0 phase(N.5b) Task 4: terrain_modern.vert -ba85299 phase(N.5b) Task 2: TerrainSlotAllocator + tests -db0f010 phase(N.5b) Task 1: TerrainAtlas bindless extension -79367d4 plan(N.5b): implementation plan for terrain on modern path -b35ddf3 spec(N.5b): design for terrain on the modern rendering path -47f2cea test(N.5b): quantify WB vs retail terrain split formula divergence -``` - -### Captured perf numbers (radius=5, Holtburg town dueling field, 5+ rollups) - -| Renderer | cpu_us median | cpu_us p95 | draws/frame | Visible LBs | Loaded LBs | -|---|---|---|---|---|---| -| **Legacy** (`TerrainChunkRenderer`) | 1.5 | 3.0 | 1 (single chunk) | 132-143 (chunk grain) | 121-143 | -| **Modern** (`TerrainModernRenderer`) | 6.4-7.0 | 9-14 | ~36-51 | 36-51 (per-LB cull) | 132-143 | - -Modern is ~4Γ— slower on CPU at radius=5 because legacy's 16Γ—16-LBs-per-chunk pattern already collapsed radius=5 to one `glDrawElements` call. The architectural wins (bindless atlas β†’ zero `glBindTexture`/frame; constant-cost dispatch as radius grows) manifest at higher scene complexity (A.5 territory). Full writeup: `docs/plans/2026-05-09-phase-n5b-perf-baseline.md`. - -### Plan amendments captured during execution - -| Task | Original framing | Issue | Resolution | -|---|---|---|---| -| 6 | "β‰₯6-8 GL calls per frame for terrain" | Counted matrix-uniform calls would push it higher | Doc-comment overstated; actual ~13 GL calls/frame in modern. Architectural shape (one MDI per pass) preserved. Captured in T6 code review. | -| 7 | Sample upper bound `* 192f` | Physics path clamps `localX/24` at 7.999 β†’ effective 191.976. Sample > 191.976 makes physics + mesh disagree by up to 23 mm. | Tightened to `* 191.975f`. Verified test still passes (max β€–Ξ”β€– = 0.015 mm). | -| 8 | "GL_TIME_ELAPSED query around the indirect dispatch" | Same single-frame poll bug as N.5 (`QueryResultAvailable=1` never appears) | Deferred GPU timer to N.6 perf polish, same as N.5. CPU stopwatch only for N.5b. | -| 8 | Acceptance criterion 5: "β‰₯10% lower CPU dispatcher" | At radius=5 / Holtburg, legacy was already ~1.5Β΅s (one draw call); modern's per-frame slot-walk + DEIC build can't beat that | Criterion amended via perf baseline doc; ship N.5b on visual identity + structural correctness. | - -### Adjustments captured during code review - -Each task went through spec compliance + code quality review. Notable adjustments: - -- T1 fixup: two-phase `Dispose` ordering (ALL `MakeNonResident` first, then ALL `DeleteTexture`) per ARB_bindless_texture spec. -- T6 fixups (Important): `meshData.Indices.Length` validation in `AddLandblock`; documented `VertsPerLandblock % 6 == 0` load-bearing invariant for the shader's `gl_VertexID % 6` corner-table lookup. -- T7 fixup (Important): tightened sample upper bound to `191.975f` to avoid the physics-clamp-vs-mesh-actual-position disagreement. - -### Hotfixes after T8 ship - -T8 shipped with two latent bugs that surfaced during the perf-baseline measurement run: - -- `55e516c` β€” `MaybeFlushTerrainDiag` median calc underflow (`copy[N - nz/2]` β†’ `copy[N]` when nz=1). -- `da56063` β€” **black terrain in modern path.** Root cause: `uniform sampler2DArray` + `glProgramUniformHandleARB` is rejected with `GL_INVALID_OPERATION` on the NVIDIA Windows driver. Switched to N.5's mesh_modern pattern: `uniform uvec2 uTerrainHandle` + `sampler2DArray(handle)` constructor at use sites. - -The black-terrain bug ALSO surfaced a process flaw: the user-verification gate was claimed "passed" without actual visual confirmation. The bug masked itself for hours of perf-measurement work. Memory captures this as a third high-value gotcha for future phases. - -### Out-of-scope β€” N.6 follow-ups - -- **GPU timer query double-buffering** β€” same as N.5; bring up alongside N.5's deferred fix. -- **Persistent-mapped indirect buffer** β€” eliminates per-frame `glBufferSubData(DRAW_INDIRECT_BUFFER)`. Likely small win at radius=5 (~1KB upload), bigger at higher radii. -- **GPU-side culling** (compute shader writing the DEIC array directly) β€” eliminates the CPU slot walk + DEIC build. N.6 or later. -- **Re-baseline at higher radius** β€” once A.5 raises the streaming radius, the architectural wins of multi-draw indirect should manifest. Capture fresh perf numbers there. - -### Memory - -`project_phase_n5b_state.md` captures three high-value gotchas for future bindless work: -1. `uniform sampler2DArray` + `glProgramUniformHandleARB` is unreliable; default to uvec2 handle + sampler-from-handle constructor. -2. Median-calc with `nz/2` underflows to out-of-range when nz<2; use `(nz-1)/2` form. -3. Visual-gate "go" doesn't equal "verified" β€” require actual visual confirmation, not just assent. - -### Files added or deleted summary - -**Added:** -- `src/AcDream.App/Rendering/TerrainModernRenderer.cs` -- `src/AcDream.Core/Terrain/TerrainSlotAllocator.cs` -- `src/AcDream.App/Rendering/Shaders/terrain_modern.vert` -- `src/AcDream.App/Rendering/Shaders/terrain_modern.frag` -- `tests/AcDream.Core.Tests/Terrain/TerrainSlotAllocatorTests.cs` -- `tests/AcDream.Core.Tests/Terrain/TerrainModernConformanceTests.cs` -- `tests/AcDream.Core.Tests/Terrain/SplitFormulaDivergenceTest.cs` -- `docs/plans/2026-05-09-phase-n5b-perf-baseline.md` -- `docs/superpowers/specs/2026-05-09-phase-n5b-terrain-modern-design.md` -- `docs/superpowers/plans/2026-05-09-phase-n5b-terrain-modern.md` (this file) - -**Modified:** -- `src/AcDream.App/Rendering/TerrainAtlas.cs` β€” bindless extension -- `src/AcDream.App/Rendering/Wb/BindlessSupport.cs` β€” note about retired SetSamplerHandleUniform helper -- `src/AcDream.App/Rendering/GameWindow.cs` β€” TerrainModernRenderer wiring + [TERRAIN-DIAG] rollup, then T9 cleanup -- `CLAUDE.md` β€” N.5b entry in WB integration cribs -- `docs/plans/2026-04-11-roadmap.md` β€” N.5b β†’ Shipped -- `docs/ISSUES.md` β€” issue #51 β†’ Recently closed - -**Deleted:** -- `src/AcDream.App/Rendering/TerrainChunkRenderer.cs` -- `src/AcDream.App/Rendering/TerrainRenderer.cs` -- `src/AcDream.App/Rendering/Shaders/terrain.vert` -- `src/AcDream.App/Rendering/Shaders/terrain.frag` diff --git a/docs/superpowers/plans/2026-05-10-issue-13-pd-trailer.md b/docs/superpowers/plans/2026-05-10-issue-13-pd-trailer.md deleted file mode 100644 index 019939c..0000000 --- a/docs/superpowers/plans/2026-05-10-issue-13-pd-trailer.md +++ /dev/null @@ -1,1242 +0,0 @@ -# Issue #13 β€” PlayerDescription Trailer Parser Implementation Plan - -> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. - -**Goal:** Extend `PlayerDescriptionParser` past the enchantment block through the full trailer β€” Options1 / Shortcuts / HotbarSpells / DesiredComps / SpellbookFilters / Options2 / GameplayOptions blob / Inventory / Equipped β€” and route the parsed `Inventory` + `Equipped` lists into `ItemRepository` so `ItemCount > 0` after login. - -**Architecture:** Match holtburger's `PlayerDescriptionEventData::unpack` (`references/holtburger/crates/holtburger-protocol/src/messages/player/events.rs:503-607`) structure-for-structure. The trailer reads in a single forward walk except for the `gameplay_options` blob, which is opaque variable-length and uses a 4-byte-aligned forward heuristic search (`find_inventory_start_after_gameplay_options`) to locate the inventory-count+GUID-pair that follows it. The trailer parse is wrapped in its own try/catch so a malformed trailer does not lose the attribute/skill/spell/enchantment data already extracted upstream. - -**Tech Stack:** C# 12 / .NET 10, `System.Buffers.Binary`, xUnit. No new dependencies. - -**Reference cross-walk:** -- Holtburger trailer wire format: `references/holtburger/crates/holtburger-protocol/src/messages/player/events.rs:503-607` (the `unpack` impl after enchantments). -- Holtburger inventory unpacker: `events.rs:143-218` (`unpack_inventory_and_equipped_strict` + `find_inventory_start_after_gameplay_options`). -- Holtburger Shortcut format: `references/holtburger/crates/holtburger-protocol/src/messages/player/shortcuts.rs:13-34` (16 bytes: u32 index + Guid (4 B) + u16 spell_id + u16 layer). -- Existing parser: [src/AcDream.Core.Net/Messages/PlayerDescriptionParser.cs](src/AcDream.Core.Net/Messages/PlayerDescriptionParser.cs). -- Existing wiring: [src/AcDream.Core.Net/GameEventWiring.cs:281-398](src/AcDream.Core.Net/GameEventWiring.cs:281). -- `ItemInstance` constructor: object-initializer with `ObjectId` + `WeenieClassId` (init-only), see [src/AcDream.Core/Items/ItemInstance.cs:128](src/AcDream.Core/Items/ItemInstance.cs:128). - -**Acceptance:** -- All sections of a synthetic real-world-shaped PlayerDescription parse to completion without nulling out earlier fields. -- New tests cover each trailer section in isolation + a combined end-to-end fixture. -- After a PlayerDescription with non-empty Inventory is dispatched, `ItemRepository.ItemCount > 0`. -- `dotnet build` + `dotnet test` green. - ---- - -## File Structure - -| Path | Action | Responsibility | -|------|--------|---------------| -| `src/AcDream.Core.Net/Messages/PlayerDescriptionParser.cs` | Modify | Add `CharacterOptionDataFlag` enum + `Shortcut` record + `EquippedEntry` record + `InventoryEntry` record, extend `Parsed` with trailer fields, add trailer reader functions wrapped in their own try/catch. | -| `tests/AcDream.Core.Net.Tests/PlayerDescriptionParserTests.cs` | Modify | Add per-section trailer tests + heuristic gameplay_options test + end-to-end full-trailer fixture. | -| `src/AcDream.Core.Net/GameEventWiring.cs` | Modify | Extend the existing `PlayerDescription` handler (~line 281) to register each `Inventory` entry as a stub `ItemInstance` in `ItemRepository`. | -| `tests/AcDream.Core.Net.Tests/GameEventWiringTests.cs` (or similar β€” locate during Task 11) | Modify or Add | Test: dispatching a PlayerDescription event with inventory entries grows `ItemRepository.ItemCount`. | - ---- - -## Wire Format (Reference) - -After the enchantment block, all little-endian: - -``` -u32 option_flags // CharacterOptionDataFlag -u32 options1 // CharacterOptions1 bitfield (opaque uint to us) - -if option_flags & SHORTCUT: // 0x01 - u32 count - count Γ— Shortcut(16 B) // u32 idx + u32 guid + u16 spell + u16 layer - -if option_flags & SPELL_LISTS8: // 0x400 - 8 Γ— { u32 count, count Γ— u32 spell_id } -else: - u32 count, count Γ— u32 spell_id // single legacy list - -if option_flags & DESIRED_COMPS: // 0x08 - u16 count, u16 _padding // (4-byte header β€” count is u16 + u16 ignored) - count Γ— { u32 id, u32 amt } - -u32 spellbook_filters // optional β€” defaults to 0 if no more bytes - -if option_flags & CHARACTER_OPTIONS2: // 0x40 - u32 options2 - -if option_flags & GAMEPLAY_OPTIONS: // 0x200 - // opaque blob; heuristic find_inventory_start_after_gameplay_options - // walks forward in 4-byte steps from current pos and accepts the first - // candidate that parses inventory+equipped exactly to end-of-buffer. - blob_bytes - inventory + equipped (strict) -else: - inventory + equipped (strict) - -// inventory + equipped strict format: -u32 inv_count // <= 10000 -inv_count Γ— { u32 guid, u32 weenieType (0..2) } // ContainerType validated -u32 eq_count // <= 10000 -eq_count Γ— { u32 guid, u32 loc, u32 prio } -``` - ---- - -## Bite-Sized Tasks - -### Task 1: Extend `Parsed` record + add types - -**Files:** -- Modify: `src/AcDream.Core.Net/Messages/PlayerDescriptionParser.cs` - -- [ ] **Step 1: Add `CharacterOptionDataFlag` enum, `Shortcut`, `InventoryEntry`, `EquippedEntry` records inside the `PlayerDescriptionParser` static class (just below the existing `EnchantmentMask` enum, ~line 178).** - -```csharp -[Flags] -public enum CharacterOptionDataFlag : uint -{ - None = 0, - Shortcut = 0x00000001, - SquelchList = 0x00000002, - MultiSpellList = 0x00000004, - DesiredComps = 0x00000008, - ExtendedMultiSpellLists = 0x00000010, - SpellbookFilters = 0x00000020, - CharacterOptions2 = 0x00000040, - TimestampFormat = 0x00000080, - GenericQualitiesData = 0x00000100, - GameplayOptions = 0x00000200, - SpellLists8 = 0x00000400, -} - -/// One shortcut bar entry. 16 bytes wire size. -/// holtburger shortcuts.rs:13-34. Named ShortcutEntry -/// (not Shortcut) to avoid a homograph with the -/// flag bit. -public readonly record struct ShortcutEntry( - uint Index, - uint ObjectGuid, - ushort SpellId, - ushort Layer); - -/// One inventory entry β€” a guid plus a ContainerType discriminator -/// (0=NonContainer, 1=Container, 2=Foci). -public readonly record struct InventoryEntry( - uint Guid, - uint ContainerType); - -/// One equipped object entry. -public readonly record struct EquippedEntry( - uint Guid, - uint EquipLocation, - uint Priority); -``` - -- [ ] **Step 2: Extend the `Parsed` record. Append new fields after `Enchantments`, all defaulting to empty in `BuildPartial`.** - -Replace the existing `Parsed` record (~line 180) with: - -```csharp -public readonly record struct Parsed( - uint WeenieType, - DescriptionPropertyFlag PropertyFlags, - DescriptionVectorFlag VectorFlags, - bool HasHealth, - PropertyBundle Properties, - IReadOnlyDictionary Positions, - IReadOnlyList Attributes, - IReadOnlyList Skills, - IReadOnlyDictionary Spells, - IReadOnlyList Enchantments, - CharacterOptionDataFlag OptionFlags, - uint Options1, - uint Options2, - IReadOnlyList Shortcuts, - IReadOnlyList> HotbarSpells, - IReadOnlyList<(uint Id, uint Amount)> DesiredComps, - uint SpellbookFilters, - ReadOnlyMemory GameplayOptions, - IReadOnlyList Inventory, - IReadOnlyList Equipped, - bool TrailerTruncated); -``` - -> **Code-review followup (added after Task 2 review):** the trailing -> `TrailerTruncated` flag was added to let callers distinguish a clean -> parse from one where the trailer try/catch swallowed a `FormatException` -> mid-section (Tasks 3–9 will make this reachable). All construction sites -> pass `TrailerTruncated: false` by default; the trailer try/catch in -> `TryParse` flips a local to `true` on catch. - -- [ ] **Step 3: Update `BuildPartial` to fill the new fields with defaults.** - -Replace the body of `BuildPartial` (~line 275) with: - -```csharp -private static Parsed BuildPartial( - uint weenieType, DescriptionPropertyFlag pFlags, DescriptionVectorFlag vFlags, - bool hasHealth, PropertyBundle bundle, - Dictionary positions, - List attributes, List skills, - Dictionary spells) -{ - return new Parsed(weenieType, pFlags, vFlags, hasHealth, - bundle, positions, attributes, skills, spells, - System.Array.Empty(), - CharacterOptionDataFlag.None, 0u, 0u, - System.Array.Empty(), - System.Array.Empty>(), - System.Array.Empty<(uint, uint)>(), - 0u, - ReadOnlyMemory.Empty, - System.Array.Empty(), - System.Array.Empty(), - TrailerTruncated: false); -} -``` - -- [ ] **Step 4: Update the existing `return new Parsed(...)` (~line 261) at the end of `TryParse` to also include the new fields with defaults.** - -Replace it with: - -```csharp -return new Parsed( - weenieType, propertyFlags, vectorFlags, hasHealth, - bundle, positions, attributes, skills, spells, enchantments, - CharacterOptionDataFlag.None, 0u, 0u, - System.Array.Empty(), - System.Array.Empty>(), - System.Array.Empty<(uint, uint)>(), - 0u, - ReadOnlyMemory.Empty, - System.Array.Empty(), - System.Array.Empty(), - TrailerTruncated: false); -``` - -- [ ] **Step 5: Run the build + existing tests to verify no regressions.** - -Run: `dotnet build` and `dotnet test --filter "FullyQualifiedName~PlayerDescriptionParserTests"` -Expected: GREEN β€” all 5 existing tests still pass. - -- [ ] **Step 6: Commit.** - -```bash -git add src/AcDream.Core.Net/Messages/PlayerDescriptionParser.cs -git commit -m "feat(net): #13 scaffold trailer fields on PlayerDescriptionParser.Parsed - -No behavior change yet β€” adds CharacterOptionDataFlag, Shortcut/Inventory/ -EquippedEntry records, and extends Parsed with trailer fields filled with -empty defaults. Sets up the per-section TDD walk in subsequent commits." -``` - ---- - -### Task 2: Read OptionFlags + Options1 (8 bytes after enchantments) - -**Files:** -- Modify: `src/AcDream.Core.Net/Messages/PlayerDescriptionParser.cs` -- Modify: `tests/AcDream.Core.Net.Tests/PlayerDescriptionParserTests.cs` - -- [ ] **Step 1: Write the failing test.** Append to `PlayerDescriptionParserTests.cs`: - -```csharp -[Fact] -public void TryParse_TrailerOptionFlagsAndOptions1_AreReadAfterEnchantments() -{ - // ATTRIBUTE | ENCHANTMENT vector flag; empty enchantment mask (0). - // After mask, trailer adds u32 option_flags + u32 options1. - var sb = new MemoryStream(); - using var writer = new BinaryWriter(sb); - writer.Write(0u); // propertyFlags - writer.Write(0x52u); // weenieType - writer.Write(0x201u); // ATTRIBUTE | ENCHANTMENT - writer.Write(1u); // has_health - writer.Write(0u); // empty attribute_flags - - writer.Write(0u); // EnchantmentMask = empty - - // Trailer header: option_flags + options1 - writer.Write(0u); // option_flags = None β€” no further sections - writer.Write(0xDEADBEEFu); // options1 sentinel - - // No more bytes β€” spellbook_filters is optional (defaults to 0). - var parsed = PlayerDescriptionParser.TryParse(sb.ToArray()); - - Assert.NotNull(parsed); - Assert.Equal(PlayerDescriptionParser.CharacterOptionDataFlag.None, parsed!.Value.OptionFlags); - Assert.Equal(0xDEADBEEFu, parsed.Value.Options1); - Assert.Empty(parsed.Value.Shortcuts); - Assert.Empty(parsed.Value.Inventory); -} -``` - -- [ ] **Step 2: Run the test β€” expect FAIL** (`OptionFlags` still default `None`, `Options1` still 0). - -Run: `dotnet test --filter "FullyQualifiedName~TryParse_TrailerOptionFlagsAndOptions1"` -Expected: FAIL β€” assertion `0xDEADBEEFu != 0u` for `Options1`. - -- [ ] **Step 3: Implement.** In `PlayerDescriptionParser.TryParse`, after the existing enchantments-read block (~line 259, just before the existing `return new Parsed(...)`), insert a trailer-walk block. Replace lines 258-273 (the enchantment read + final return) with: - -```csharp -// ── Enchantments (Issue #7 / #12) ─────────────────────────────── -if (vectorFlags.HasFlag(DescriptionVectorFlag.Enchantment)) - ReadEnchantmentBlock(payload, ref pos, enchantments); - -// ── Trailer (Issue #13): options + shortcuts + hotbars + inventory ── -// Wrapped in its own try/catch β€” a malformed trailer must not destroy -// the attribute / skill / spell / enchantment data we already extracted. -CharacterOptionDataFlag optionFlags = CharacterOptionDataFlag.None; -uint options1 = 0; -uint options2 = 0; -uint spellbookFilters = 0; -List shortcuts = new(); -List> hotbarSpells = new(); -List<(uint, uint)> desiredComps = new(); -ReadOnlyMemory gameplayOptions = ReadOnlyMemory.Empty; -List inventory = new(); -List equipped = new(); -bool trailerTruncated = false; - -try -{ - if (payload.Length - pos >= 8) - { - optionFlags = (CharacterOptionDataFlag)ReadU32(payload, ref pos); - options1 = ReadU32(payload, ref pos); - } -} -catch (FormatException ex) -{ - // Trailer corrupted β€” keep what we have and flag it. Tasks 3-9 - // can leave partial lists in scope; TrailerTruncated lets callers - // ignore the trailer when they need all-or-nothing semantics. - trailerTruncated = true; - if (System.Environment.GetEnvironmentVariable("ACDREAM_DUMP_VITALS") == "1") - System.Console.WriteLine($"PlayerDescriptionParser: trailer FormatException at pos={pos}/{payload.Length}: {ex.Message}"); -} - -return new Parsed( - weenieType, propertyFlags, vectorFlags, hasHealth, - bundle, positions, attributes, skills, spells, enchantments, - optionFlags, options1, options2, - shortcuts, hotbarSpells, desiredComps, spellbookFilters, - gameplayOptions, inventory, equipped, trailerTruncated); -``` - -> **Tasks 3–9 note:** every `return new Parsed(...)` extension or -> rewrite in subsequent tasks must include `trailerTruncated` as the -> final positional argument, and any new try-blocks that read trailer -> sections should set `trailerTruncated = true;` in their catch. - -- [ ] **Step 4: Run the test β€” expect PASS.** - -Run: `dotnet test --filter "FullyQualifiedName~TryParse_TrailerOptionFlagsAndOptions1"` -Expected: PASS. - -- [ ] **Step 5: Run full PlayerDescription test suite to confirm no regressions.** - -Run: `dotnet test --filter "FullyQualifiedName~PlayerDescriptionParserTests"` -Expected: 6 tests pass (5 original + 1 new). - -- [ ] **Step 6: Commit.** - -```bash -git add src/AcDream.Core.Net/Messages/PlayerDescriptionParser.cs tests/AcDream.Core.Net.Tests/PlayerDescriptionParserTests.cs -git commit -m "feat(net): #13 read OptionFlags + Options1 after enchantments - -First step of the PD trailer walk. Wraps trailer reads in their own -try/catch so a malformed trailer does not null out the upstream -attribute/skill/spell/enchantment data." -``` - ---- - -### Task 3: Read Shortcuts list (gated on SHORTCUT bit) - -**Files:** -- Modify: `src/AcDream.Core.Net/Messages/PlayerDescriptionParser.cs` -- Modify: `tests/AcDream.Core.Net.Tests/PlayerDescriptionParserTests.cs` - -- [ ] **Step 1: Write the failing test.** - -```csharp -[Fact] -public void TryParse_TrailerShortcuts_PopulatesList() -{ - var sb = new MemoryStream(); - using var writer = new BinaryWriter(sb); - writer.Write(0u); // propertyFlags - writer.Write(0x52u); // weenieType - writer.Write(0x201u); // ATTRIBUTE | ENCHANTMENT - writer.Write(1u); // has_health - writer.Write(0u); // empty attribute_flags - writer.Write(0u); // empty enchantment mask - - writer.Write(0x01u); // option_flags = SHORTCUT - writer.Write(0xCAFEu); // options1 sentinel - - // Shortcut count + 2 entries (16 B each). - writer.Write(2u); - writer.Write(0u); writer.Write(0xAABBCCDDu); writer.Write((ushort)0); writer.Write((ushort)0); - writer.Write(7u); writer.Write(0u); writer.Write((ushort)1234); writer.Write((ushort)5); - - var parsed = PlayerDescriptionParser.TryParse(sb.ToArray()); - - Assert.NotNull(parsed); - Assert.Equal(2, parsed!.Value.Shortcuts.Count); - Assert.Equal(0u, parsed.Value.Shortcuts[0].Index); - Assert.Equal(0xAABBCCDDu, parsed.Value.Shortcuts[0].ObjectGuid); - Assert.Equal((ushort)0, parsed.Value.Shortcuts[0].SpellId); - Assert.Equal(7u, parsed.Value.Shortcuts[1].Index); - Assert.Equal((ushort)1234, parsed.Value.Shortcuts[1].SpellId); - Assert.Equal((ushort)5, parsed.Value.Shortcuts[1].Layer); -} -``` - -- [ ] **Step 2: Run the test β€” expect FAIL** (`Shortcuts` still empty). - -- [ ] **Step 3: Implement.** Inside the trailer try-block, after the `options1 = ReadU32(...)` line, append: - -```csharp -if (optionFlags.HasFlag(CharacterOptionDataFlag.Shortcut)) -{ - uint count = ReadU32(payload, ref pos); - if (count > 10_000) throw new FormatException("unreasonable shortcut count"); - for (uint i = 0; i < count; i++) - { - uint idx = ReadU32(payload, ref pos); - uint guid = ReadU32(payload, ref pos); - ushort spellId = ReadU16(payload, ref pos); - ushort layer = ReadU16(payload, ref pos); - shortcuts.Add(new ShortcutEntry(idx, guid, spellId, layer)); - } -} -``` - -- [ ] **Step 4: Run the test β€” expect PASS.** - -- [ ] **Step 5: Run full suite β€” expect green.** - -- [ ] **Step 6: Commit.** - -```bash -git add -u -git commit -m "feat(net): #13 read shortcuts list (SHORTCUT bit) in PD trailer" -``` - ---- - -### Task 4: Read HotbarSpells with SPELL_LISTS8 path - -**Files:** -- Modify: `src/AcDream.Core.Net/Messages/PlayerDescriptionParser.cs` -- Modify: `tests/AcDream.Core.Net.Tests/PlayerDescriptionParserTests.cs` - -- [ ] **Step 1: Write the failing test.** - -```csharp -[Fact] -public void TryParse_TrailerHotbarSpells_SpellLists8_Reads8Lists() -{ - var sb = new MemoryStream(); - using var writer = new BinaryWriter(sb); - writer.Write(0u); // propertyFlags - writer.Write(0x52u); // weenieType - writer.Write(0x201u); // ATTRIBUTE | ENCHANTMENT - writer.Write(1u); // has_health - writer.Write(0u); // empty attribute_flags - writer.Write(0u); // empty enchantment mask - - writer.Write(0x400u); // option_flags = SPELL_LISTS8 - writer.Write(0u); // options1 - - // 8 hotbars: counts {2,1,0,0,0,0,0,3} β€” first list has 2 spells, second has 1, last has 3. - writer.Write(2u); writer.Write(11u); writer.Write(12u); - writer.Write(1u); writer.Write(21u); - writer.Write(0u); - writer.Write(0u); - writer.Write(0u); - writer.Write(0u); - writer.Write(0u); - writer.Write(3u); writer.Write(81u); writer.Write(82u); writer.Write(83u); - - var parsed = PlayerDescriptionParser.TryParse(sb.ToArray()); - - Assert.NotNull(parsed); - Assert.Equal(8, parsed!.Value.HotbarSpells.Count); - Assert.Equal(new uint[] { 11u, 12u }, parsed.Value.HotbarSpells[0]); - Assert.Equal(new uint[] { 21u }, parsed.Value.HotbarSpells[1]); - Assert.Empty(parsed.Value.HotbarSpells[2]); - Assert.Equal(new uint[] { 81u, 82u, 83u }, parsed.Value.HotbarSpells[7]); -} -``` - -- [ ] **Step 2: Run the test β€” expect FAIL.** - -- [ ] **Step 3: Implement.** After the shortcuts block in the trailer try-block, append: - -```csharp -if (optionFlags.HasFlag(CharacterOptionDataFlag.SpellLists8)) -{ - for (int b = 0; b < 8; b++) - { - uint count = ReadU32(payload, ref pos); - if (count > 10_000) throw new FormatException("unreasonable hotbar count"); - var list = new List((int)count); - for (uint i = 0; i < count; i++) - list.Add(ReadU32(payload, ref pos)); - hotbarSpells.Add(list); - } -} -else if (payload.Length - pos >= 4) -{ - // Legacy single-list fallback (holtburger events.rs:544-556). - uint count = ReadU32(payload, ref pos); - if (count > 10_000) throw new FormatException("unreasonable hotbar count"); - var list = new List((int)count); - for (uint i = 0; i < count; i++) - list.Add(ReadU32(payload, ref pos)); - hotbarSpells.Add(list); -} -``` - -- [ ] **Step 4: Run the test β€” expect PASS.** - -- [ ] **Step 5: Add a second test for the legacy single-list path.** - -```csharp -[Fact] -public void TryParse_TrailerHotbarSpells_NoSpellLists8_ReadsSingleLegacyList() -{ - var sb = new MemoryStream(); - using var writer = new BinaryWriter(sb); - writer.Write(0u); // propertyFlags - writer.Write(0x52u); // weenieType - writer.Write(0x201u); // ATTRIBUTE | ENCHANTMENT - writer.Write(1u); // has_health - writer.Write(0u); // empty attribute_flags - writer.Write(0u); // empty enchantment mask - - writer.Write(0u); // option_flags = None (no SPELL_LISTS8) - writer.Write(0u); // options1 - - // Legacy single hotbar list: count=2, two spells. - writer.Write(2u); writer.Write(101u); writer.Write(102u); - - var parsed = PlayerDescriptionParser.TryParse(sb.ToArray()); - - Assert.NotNull(parsed); - Assert.Single(parsed!.Value.HotbarSpells); - Assert.Equal(new uint[] { 101u, 102u }, parsed.Value.HotbarSpells[0]); -} -``` - -- [ ] **Step 6: Run both hotbar tests β€” expect PASS. Then full suite green.** - -- [ ] **Step 7: Commit.** - -```bash -git add -u -git commit -m "feat(net): #13 read hotbar spells (SPELL_LISTS8 + legacy path)" -``` - ---- - -### Task 5: Read DesiredComps list - -**Files:** -- Modify: `src/AcDream.Core.Net/Messages/PlayerDescriptionParser.cs` -- Modify: `tests/AcDream.Core.Net.Tests/PlayerDescriptionParserTests.cs` - -- [ ] **Step 1: Write the failing test.** - -```csharp -[Fact] -public void TryParse_TrailerDesiredComps_ReadsIdAmtPairs() -{ - var sb = new MemoryStream(); - using var writer = new BinaryWriter(sb); - writer.Write(0u); // propertyFlags - writer.Write(0x52u); // weenieType - writer.Write(0x201u); // ATTRIBUTE | ENCHANTMENT - writer.Write(1u); // has_health - writer.Write(0u); // empty attribute_flags - writer.Write(0u); // empty enchantment mask - - // option_flags = DESIRED_COMPS (0x08); no SPELL_LISTS8 so legacy hotbar list (count=0). - writer.Write(0x08u); - writer.Write(0u); // options1 - - // Legacy hotbar list: count=0 - writer.Write(0u); - - // DESIRED_COMPS: u16 count=2, u16 padding, then 2 (id,amt) pairs of 8 bytes each. - writer.Write((ushort)2); - writer.Write((ushort)0); - writer.Write(0xAAu); writer.Write(50u); - writer.Write(0xBBu); writer.Write(75u); - - var parsed = PlayerDescriptionParser.TryParse(sb.ToArray()); - - Assert.NotNull(parsed); - Assert.Equal(2, parsed!.Value.DesiredComps.Count); - Assert.Equal((0xAAu, 50u), parsed.Value.DesiredComps[0]); - Assert.Equal((0xBBu, 75u), parsed.Value.DesiredComps[1]); -} -``` - -- [ ] **Step 2: Run β€” expect FAIL.** - -- [ ] **Step 3: Implement.** After the hotbar block in the trailer try-block, append: - -```csharp -if (optionFlags.HasFlag(CharacterOptionDataFlag.DesiredComps)) -{ - // holtburger events.rs:558-574 β€” u16 count + u16 padding (4-byte header). - if (payload.Length - pos < 4) throw new FormatException("truncated desired_comps header"); - ushort count = ReadU16(payload, ref pos); - ReadU16(payload, ref pos); // padding/buckets β€” discarded - if (count > 10_000) throw new FormatException("unreasonable desired_comps count"); - for (int i = 0; i < count; i++) - { - uint id = ReadU32(payload, ref pos); - uint amt = ReadU32(payload, ref pos); - desiredComps.Add((id, amt)); - } -} -``` - -- [ ] **Step 4: Run β€” expect PASS.** Run full suite green. - -- [ ] **Step 5: Commit.** - -```bash -git add -u -git commit -m "feat(net): #13 read desired_comps list in PD trailer" -``` - ---- - -### Task 6: Read SpellbookFilters (optional u32, defaults to 0) - -**Files:** -- Modify: `src/AcDream.Core.Net/Messages/PlayerDescriptionParser.cs` -- Modify: `tests/AcDream.Core.Net.Tests/PlayerDescriptionParserTests.cs` - -- [ ] **Step 1: Write the failing test.** - -```csharp -[Fact] -public void TryParse_TrailerSpellbookFilters_ReadOptionalU32() -{ - var sb = new MemoryStream(); - using var writer = new BinaryWriter(sb); - writer.Write(0u); // propertyFlags - writer.Write(0x52u); // weenieType - writer.Write(0x201u); // ATTRIBUTE | ENCHANTMENT - writer.Write(1u); // has_health - writer.Write(0u); // empty attribute_flags - writer.Write(0u); // empty enchantment mask - - writer.Write(0u); // option_flags = None - writer.Write(0u); // options1 - - // Legacy hotbar list: count=0 - writer.Write(0u); - - // spellbook_filters sentinel. - writer.Write(0xF00DBA42u); - - var parsed = PlayerDescriptionParser.TryParse(sb.ToArray()); - - Assert.NotNull(parsed); - Assert.Equal(0xF00DBA42u, parsed!.Value.SpellbookFilters); -} -``` - -- [ ] **Step 2: Run β€” expect FAIL** (defaults to 0). - -- [ ] **Step 3: Implement.** After the desired_comps block in the trailer try-block, append: - -```csharp -// holtburger events.rs:576-582 β€” spellbook_filters is optional; defaults -// to 0 if EOF. -if (payload.Length - pos >= 4) - spellbookFilters = ReadU32(payload, ref pos); -``` - -- [ ] **Step 4: Run β€” expect PASS.** Run full suite green. - -- [ ] **Step 5: Commit.** - -```bash -git add -u -git commit -m "feat(net): #13 read optional spellbook_filters u32" -``` - ---- - -### Task 7: Read Options2 (gated on CHARACTER_OPTIONS2 bit) - -**Files:** -- Modify: `src/AcDream.Core.Net/Messages/PlayerDescriptionParser.cs` -- Modify: `tests/AcDream.Core.Net.Tests/PlayerDescriptionParserTests.cs` - -- [ ] **Step 1: Write the failing test.** - -```csharp -[Fact] -public void TryParse_TrailerOptions2_GatedOnCharacterOptions2Bit() -{ - var sb = new MemoryStream(); - using var writer = new BinaryWriter(sb); - writer.Write(0u); // propertyFlags - writer.Write(0x52u); // weenieType - writer.Write(0x201u); // ATTRIBUTE | ENCHANTMENT - writer.Write(1u); // has_health - writer.Write(0u); // empty attribute_flags - writer.Write(0u); // empty enchantment mask - - // option_flags = CHARACTER_OPTIONS2 (0x40) - writer.Write(0x40u); - writer.Write(0u); // options1 - - // Legacy hotbar list: count=0. - writer.Write(0u); - - // spellbook_filters - writer.Write(0u); - - // options2 sentinel - writer.Write(0xC0FFEE01u); - - var parsed = PlayerDescriptionParser.TryParse(sb.ToArray()); - - Assert.NotNull(parsed); - Assert.Equal(0xC0FFEE01u, parsed!.Value.Options2); -} -``` - -- [ ] **Step 2: Run β€” expect FAIL.** - -- [ ] **Step 3: Implement.** After the spellbook_filters block, append: - -```csharp -if (optionFlags.HasFlag(CharacterOptionDataFlag.CharacterOptions2)) - options2 = ReadU32(payload, ref pos); -``` - -- [ ] **Step 4: Run β€” expect PASS.** Run full suite green. - -- [ ] **Step 5: Commit.** - -```bash -git add -u -git commit -m "feat(net): #13 read options2 gated on CHARACTER_OPTIONS2 flag" -``` - ---- - -### Task 8: Strict Inventory + Equipped reader (no GAMEPLAY_OPTIONS path) - -**Files:** -- Modify: `src/AcDream.Core.Net/Messages/PlayerDescriptionParser.cs` -- Modify: `tests/AcDream.Core.Net.Tests/PlayerDescriptionParserTests.cs` - -- [ ] **Step 1: Write the failing test.** - -```csharp -[Fact] -public void TryParse_TrailerInventoryEquippedStrict_NoGameplayOptionsBit() -{ - var sb = new MemoryStream(); - using var writer = new BinaryWriter(sb); - writer.Write(0u); // propertyFlags - writer.Write(0x52u); // weenieType - writer.Write(0x201u); // ATTRIBUTE | ENCHANTMENT - writer.Write(1u); // has_health - writer.Write(0u); // empty attribute_flags - writer.Write(0u); // empty enchantment mask - - writer.Write(0u); // option_flags = None β€” no GAMEPLAY_OPTIONS - writer.Write(0u); // options1 - writer.Write(0u); // legacy hotbar list count=0 - writer.Write(0u); // spellbook_filters - - // Inventory: 2 entries - writer.Write(2u); - writer.Write(0x500000A0u); writer.Write(0u); // NonContainer - writer.Write(0x500000A1u); writer.Write(1u); // Container - - // Equipped: 1 entry - writer.Write(1u); - writer.Write(0x500000B0u); writer.Write(0x00000200u); writer.Write(1u); // ChestArmor, prio=1 - - var parsed = PlayerDescriptionParser.TryParse(sb.ToArray()); - - Assert.NotNull(parsed); - Assert.Equal(2, parsed!.Value.Inventory.Count); - Assert.Equal(0x500000A0u, parsed.Value.Inventory[0].Guid); - Assert.Equal(0u, parsed.Value.Inventory[0].ContainerType); - Assert.Equal(1u, parsed.Value.Inventory[1].ContainerType); - Assert.Single(parsed.Value.Equipped); - Assert.Equal(0x500000B0u, parsed.Value.Equipped[0].Guid); - Assert.Equal(0x00000200u, parsed.Value.Equipped[0].EquipLocation); - Assert.Equal(1u, parsed.Value.Equipped[0].Priority); -} -``` - -- [ ] **Step 2: Run β€” expect FAIL.** - -- [ ] **Step 3: Implement.** Add a new helper method `TryUnpackInventoryStrict` near the bottom of the class, just above the primitive readers (~line 545): - -```csharp -/// Strict inventory + equipped block reader. Returns true if -/// the bytes from parse cleanly per holtburger -/// events.rs:143-193 (unpack_inventory_and_equipped_strict). -/// Counts capped at 10,000; inventory ContainerType must be 0..2 -/// (NonContainer / Container / Foci). -private static bool TryUnpackInventoryStrict( - ReadOnlySpan src, ref int pos, - List inventory, List equipped) -{ - inventory.Clear(); - equipped.Clear(); - if (pos + 4 > src.Length) return false; - uint invCount = BinaryPrimitives.ReadUInt32LittleEndian(src.Slice(pos)); - pos += 4; - if (invCount > 10_000) return false; - - for (uint i = 0; i < invCount; i++) - { - if (pos + 8 > src.Length) return false; - uint guid = BinaryPrimitives.ReadUInt32LittleEndian(src.Slice(pos)); - uint wtype = BinaryPrimitives.ReadUInt32LittleEndian(src.Slice(pos + 4)); - pos += 8; - if (wtype > 2) return false; - inventory.Add(new InventoryEntry(guid, wtype)); - } - - if (pos + 4 > src.Length) return false; - uint eqCount = BinaryPrimitives.ReadUInt32LittleEndian(src.Slice(pos)); - pos += 4; - if (eqCount > 10_000) return false; - - for (uint i = 0; i < eqCount; i++) - { - if (pos + 12 > src.Length) return false; - uint guid = BinaryPrimitives.ReadUInt32LittleEndian(src.Slice(pos)); - uint loc = BinaryPrimitives.ReadUInt32LittleEndian(src.Slice(pos + 4)); - uint prio = BinaryPrimitives.ReadUInt32LittleEndian(src.Slice(pos + 8)); - pos += 12; - equipped.Add(new EquippedEntry(guid, loc, prio)); - } - return true; -} -``` - -After the options2 read in the trailer try-block, append: - -```csharp -if (!optionFlags.HasFlag(CharacterOptionDataFlag.GameplayOptions)) -{ - // Strict path: inventory + equipped follow directly. - TryUnpackInventoryStrict(payload, ref pos, inventory, equipped); -} -``` - -- [ ] **Step 4: Run β€” expect PASS.** Run full suite green. - -- [ ] **Step 5: Commit.** - -```bash -git add -u -git commit -m "feat(net): #13 strict inventory+equipped reader (no GAMEPLAY_OPTIONS)" -``` - ---- - -### Task 9: Heuristic GAMEPLAY_OPTIONS path - -**Files:** -- Modify: `src/AcDream.Core.Net/Messages/PlayerDescriptionParser.cs` -- Modify: `tests/AcDream.Core.Net.Tests/PlayerDescriptionParserTests.cs` - -- [ ] **Step 1: Write the failing test.** - -```csharp -[Fact] -public void TryParse_TrailerGameplayOptions_HeuristicLocatesInventoryStart() -{ - var sb = new MemoryStream(); - using var writer = new BinaryWriter(sb); - writer.Write(0u); // propertyFlags - writer.Write(0x52u); // weenieType - writer.Write(0x201u); // ATTRIBUTE | ENCHANTMENT - writer.Write(1u); // has_health - writer.Write(0u); // empty attribute_flags - writer.Write(0u); // empty enchantment mask - - // option_flags = GAMEPLAY_OPTIONS (0x200) - writer.Write(0x200u); - writer.Write(0u); // options1 - writer.Write(0u); // legacy hotbar count=0 - writer.Write(0u); // spellbook_filters - - // 16 bytes of opaque gameplay_options blob β€” values that *almost* look - // like an inventory header but fail validation (wtype > 2 or count too - // big), forcing the heuristic to walk past them. - writer.Write(0xDEADBEEFu); // looks like inv_count = 0xDEADBEEF (> 10_000) β€” rejected - writer.Write(0xCAFEBABEu); - writer.Write(0x12345678u); - writer.Write(0x87654321u); - - // Real inventory: 1 entry, then equipped: 1 entry β€” must consume to EOF. - writer.Write(1u); - writer.Write(0x50000200u); writer.Write(0u); - writer.Write(1u); - writer.Write(0x50000300u); writer.Write(0x00000200u); writer.Write(1u); - - var parsed = PlayerDescriptionParser.TryParse(sb.ToArray()); - - Assert.NotNull(parsed); - Assert.Single(parsed!.Value.Inventory); - Assert.Equal(0x50000200u, parsed.Value.Inventory[0].Guid); - Assert.Single(parsed.Value.Equipped); - Assert.Equal(0x50000300u, parsed.Value.Equipped[0].Guid); - Assert.Equal(16, parsed.Value.GameplayOptions.Length); -} -``` - -- [ ] **Step 2: Run β€” expect FAIL.** - -- [ ] **Step 3: Implement.** Add a new helper method just below `TryUnpackInventoryStrict`: - -```csharp -/// 4-byte-aligned forward scan from -/// looking for the first offset where TryUnpackInventoryStrict -/// consumes exactly to end-of-buffer. Mirrors holtburger -/// find_inventory_start_after_gameplay_options in events.rs:195-218. -private static bool TryHeuristicInventoryStart( - ReadOnlySpan src, int start, - out int invStart, out int end, - List inventory, List equipped) -{ - invStart = end = 0; - inventory.Clear(); - equipped.Clear(); - if (start + 8 > src.Length) return false; - - int candidate = start; - int misalign = candidate & 3; - if (misalign != 0) candidate += 4 - misalign; - - int last = src.Length - 8; - while (candidate <= last) - { - int tmp = candidate; - var tmpInv = new List(); - var tmpEq = new List(); - if (TryUnpackInventoryStrict(src, ref tmp, tmpInv, tmpEq) && tmp == src.Length) - { - invStart = candidate; - end = tmp; - inventory.AddRange(tmpInv); - equipped.AddRange(tmpEq); - return true; - } - candidate += 4; - } - return false; -} -``` - -In the trailer try-block, replace the strict-only branch from Task 8 with the full conditional: - -```csharp -if (optionFlags.HasFlag(CharacterOptionDataFlag.GameplayOptions)) -{ - int gameplayStart = pos; - if (TryHeuristicInventoryStart(payload, gameplayStart, out int invStart, out int end, - inventory, equipped)) - { - gameplayOptions = payload.Slice(gameplayStart, invStart - gameplayStart).ToArray(); - pos = end; - } -} -else -{ - TryUnpackInventoryStrict(payload, ref pos, inventory, equipped); -} -``` - -Note: `payload.Slice(...)` returns a `ReadOnlySpan` β€” we capture into `byte[]` then store as `ReadOnlyMemory` (the field type). The `.ToArray()` allocation is acceptable (one per PD = once per session). - -- [ ] **Step 4: Run β€” expect PASS.** Run full suite green. - -- [ ] **Step 5: Commit.** - -```bash -git add -u -git commit -m "feat(net): #13 heuristic inventory locator after gameplay_options blob" -``` - ---- - -### Task 10: Combined end-to-end fixture test - -**Files:** -- Modify: `tests/AcDream.Core.Net.Tests/PlayerDescriptionParserTests.cs` - -- [ ] **Step 1: Write a single test that exercises every section together β€” a real-shaped fixture.** - -```csharp -[Fact] -public void TryParse_FullTrailer_AllSectionsPopulated() -{ - var sb = new MemoryStream(); - using var writer = new BinaryWriter(sb); - writer.Write(0u); // propertyFlags - writer.Write(0x52u); // weenieType - writer.Write(0x201u); // ATTRIBUTE | ENCHANTMENT - writer.Write(1u); // has_health - writer.Write(0u); // empty attribute_flags - writer.Write(0u); // empty enchantment mask - - // option_flags = SHORTCUT | DESIRED_COMPS | CHARACTER_OPTIONS2 | SPELL_LISTS8 - // = 0x01 | 0x08 | 0x40 | 0x400 = 0x449 - writer.Write(0x449u); - writer.Write(0xAA000001u); // options1 - - // Shortcuts: count=1 - writer.Write(1u); - writer.Write(3u); writer.Write(0xCAFEFACEu); writer.Write((ushort)100); writer.Write((ushort)2); - - // 8 hotbars, all empty for brevity. - for (int i = 0; i < 8; i++) writer.Write(0u); - - // Desired comps: count=1 - writer.Write((ushort)1); writer.Write((ushort)0); - writer.Write(0xC1u); writer.Write(99u); - - // spellbook_filters - writer.Write(0xF11Du); - - // options2 - writer.Write(0xBB000002u); - - // Inventory + equipped (no GAMEPLAY_OPTIONS, strict path) - writer.Write(1u); - writer.Write(0x50000400u); writer.Write(0u); - writer.Write(1u); - writer.Write(0x50000500u); writer.Write(0x00000200u); writer.Write(1u); - - var parsed = PlayerDescriptionParser.TryParse(sb.ToArray()); - - Assert.NotNull(parsed); - var v = parsed!.Value; - Assert.Equal(0xAA000001u, v.Options1); - Assert.Equal(0xBB000002u, v.Options2); - Assert.Equal(0xF11Du, v.SpellbookFilters); - Assert.Single(v.Shortcuts); - Assert.Equal(0xCAFEFACEu, v.Shortcuts[0].ObjectGuid); - Assert.Equal(8, v.HotbarSpells.Count); - Assert.All(v.HotbarSpells, l => Assert.Empty(l)); - Assert.Single(v.DesiredComps); - Assert.Equal((0xC1u, 99u), v.DesiredComps[0]); - Assert.Single(v.Inventory); - Assert.Equal(0x50000400u, v.Inventory[0].Guid); - Assert.Single(v.Equipped); - Assert.Equal(0x50000500u, v.Equipped[0].Guid); -} -``` - -- [ ] **Step 2: Run β€” expect PASS** (no implementation change needed; this exercises the cumulative behavior). - -- [ ] **Step 3: Run full suite green.** - -- [ ] **Step 4: Commit.** - -```bash -git add -u -git commit -m "test(net): #13 end-to-end PD trailer fixture covering every section" -``` - ---- - -### Task 11: Wire `Inventory` into ItemRepository in GameEventWiring - -**Files:** -- Modify: `src/AcDream.Core.Net/GameEventWiring.cs` -- Test: locate the test file referenced by the issue text β€” `tests/AcDream.Core.Net.Tests/GameEventWiringTests.cs` if it exists, otherwise add a new file. - -- [ ] **Step 1: Locate (or create) the wiring test file.** - -```bash -ls tests/AcDream.Core.Net.Tests/GameEventWiring* -``` - -If absent, create `tests/AcDream.Core.Net.Tests/GameEventWiringInventoryTests.cs`. - -- [ ] **Step 2: Write the failing test.** - -In `tests/AcDream.Core.Net.Tests/GameEventWiringInventoryTests.cs`: - -```csharp -using System; -using System.IO; -using AcDream.Core.Chat; -using AcDream.Core.Combat; -using AcDream.Core.Items; -using AcDream.Core.Net; -using AcDream.Core.Net.Messages; -using AcDream.Core.Spells; - -namespace AcDream.Core.Net.Tests; - -public sealed class GameEventWiringInventoryTests -{ - [Fact] - public void PlayerDescription_RegistersInventoryEntries_InItemRepository() - { - var dispatcher = new GameEventDispatcher(); - var items = new ItemRepository(); - var combat = new CombatLog(); - var spellbook = new Spellbook(); - var chat = new ChatLog(); - - GameEventWiring.WireAll(dispatcher, items, combat, spellbook, chat); - - // Build a minimal PlayerDescription with inventory: 2 entries. - var sb = new MemoryStream(); - using var w = new BinaryWriter(sb); - w.Write(0u); // propertyFlags - w.Write(0x52u); // weenieType - w.Write(0x201u); // ATTRIBUTE | ENCHANTMENT - w.Write(1u); // has_health - w.Write(0u); // empty attribute_flags - w.Write(0u); // empty enchantment mask - - w.Write(0u); // option_flags = None - w.Write(0u); // options1 - w.Write(0u); // legacy hotbar count - w.Write(0u); // spellbook_filters - - // Inventory: 2 entries, then 0 equipped. - w.Write(2u); - w.Write(0x50000A01u); w.Write(0u); - w.Write(0x50000A02u); w.Write(1u); - w.Write(0u); - - // Construct an envelope-stripped GameEvent payload. - var evt = new GameEvent(GameEventType.PlayerDescription, sb.ToArray(), Sequence: 1); - - Assert.Equal(0, items.ItemCount); - dispatcher.Dispatch(evt); - Assert.Equal(2, items.ItemCount); - Assert.NotNull(items.GetItem(0x50000A01u)); - Assert.NotNull(items.GetItem(0x50000A02u)); - } -} -``` - -> **Note for the executor:** the constructor signature for `GameEventWiring.WireAll` may use named optional parameters (`localPlayer`, `turbineChat`, etc.). Inspect [src/AcDream.Core.Net/GameEventWiring.cs:39-65](src/AcDream.Core.Net/GameEventWiring.cs:39) and pass only the non-optional positional args. The exact `GameEvent` constructor name + arg order is in [src/AcDream.Core.Net/Messages/GameEvent.cs](src/AcDream.Core.Net/Messages/GameEvent.cs) β€” adjust if the project uses `Payload: ` instead of positional. If `Spellbook` has a different constructor (e.g. requires a `World`), use the existing test pattern from `GameEventWiringTests` if one exists, or pass `null!`-style defaults. - -- [ ] **Step 3: Run β€” expect FAIL** (current handler does not touch ItemRepository for trailer inventory). - -- [ ] **Step 4: Implement.** In `src/AcDream.Core.Net/GameEventWiring.cs`, inside the existing `dispatcher.Register(GameEventType.PlayerDescription, e => { ... })` lambda (right before its closing `});` at line ~398), append: - -```csharp -// Issue #13 β€” register inventory entries with ItemRepository so panels -// (inventory, paperdoll, hotbars) light up after login. Equipped entries -// share the same ObjectId as inventory entries (an equipped item is -// also in inventory) β€” register both, but the equipped record carries -// the slot mask which we surface via MoveItem so paperdoll can render. -foreach (var inv in p.Value.Inventory) -{ - if (items.GetItem(inv.Guid) is null) - { - items.AddOrUpdate(new ItemInstance - { - ObjectId = inv.Guid, - WeenieClassId = inv.ContainerType, - }); - } -} -foreach (var eq in p.Value.Equipped) -{ - if (items.GetItem(eq.Guid) is null) - { - items.AddOrUpdate(new ItemInstance - { - ObjectId = eq.Guid, - WeenieClassId = 0, - }); - } - // Reflect the equip slot β€” paperdoll uses CurrentlyEquippedLocation. - items.MoveItem( - itemId: eq.Guid, - newContainerId: 0, - newSlot: -1, - newEquipLocation: (EquipMask)eq.EquipLocation); -} -``` - -If `ItemInstance` or `EquipMask` is not already imported in this file, add the using directives: - -```csharp -using AcDream.Core.Items; -``` - -- [ ] **Step 5: Run β€” expect PASS.** Run full suite green. - -- [ ] **Step 6: Commit.** - -```bash -git add -u -git commit -m "feat(net): #13 register PD trailer inventory+equipped in ItemRepository" -``` - ---- - -### Task 12: Update issue tracker + close - -**Files:** -- Modify: `docs/ISSUES.md` - -- [ ] **Step 1: Move #13 from OPEN to "Recently closed".** In `docs/ISSUES.md`, locate the `## #13` block (line ~1382) and the `Recently closed` section. Move the block, update its status header to `**Status:** DONE` + add a `**Closed:** 2026-05-10` + `**Commit:** ` line. The fix-summary should note: full trailer walked; ItemRepository registration of inventory + equipped wired; tests added. - -- [ ] **Step 2: Run the full test suite one final time.** - -```bash -dotnet test -``` -Expected: full green, no regressions. - -- [ ] **Step 3: Commit.** - -```bash -git add docs/ISSUES.md -git commit -m "docs: close ISSUES.md #13 β€” PD trailer parser shipped" -``` - ---- - -## Self-Review Checklist (executed by plan author) - -- **Spec coverage:** Every section in the issue text is covered by a task β€” Options1 (Task 2), Shortcuts (Task 3), Hotbars (Task 4), DesiredComps (Task 5), SpellbookFilters (Task 6), Options2 (Task 7), strict inventory (Task 8), GameplayOptions blob + heuristic (Task 9), GameEventWiring routing (Task 11). PASS. - -- **Placeholder scan:** All test bodies + implementation snippets are complete C#. The one note in Task 11 about constructor signatures is annotated as a verification hint, not a placeholder. PASS. - -- **Type consistency:** `InventoryEntry(Guid, ContainerType)` and `EquippedEntry(Guid, EquipLocation, Priority)` are introduced in Task 1 and used consistently in Tasks 8/9/11. `Shortcut(Index, ObjectGuid, SpellId, Layer)` matches in Tasks 1+3. `CharacterOptionDataFlag` field names (`Shortcut`, `DesiredComps`, `CharacterOptions2`, `GameplayOptions`, `SpellLists8`) match the holtburger bit names. PASS. - -- **Edge cases addressed:** Trailer try/catch isolates trailer corruption from upstream data (Task 2); count caps at 10,000 prevent attacker-controlled allocation (Tasks 3, 5, 8); 4-byte alignment in heuristic search (Task 9); legacy single-list hotbar fallback (Task 4 step 5). PASS. - ---- - -## Execution Handoff - -Plan complete and saved to `docs/superpowers/plans/2026-05-10-issue-13-pd-trailer.md`. Two execution options: - -1. **Subagent-Driven (recommended)** β€” Dispatch a fresh subagent per task, review between tasks, fast iteration. Sonnet is correct per project subagent policy. -2. **Inline Execution** β€” Execute tasks in this session using `superpowers:executing-plans`, batch execution with checkpoints for review. - -Which approach? diff --git a/docs/superpowers/plans/2026-05-10-issue-53-tier1-cache.md b/docs/superpowers/plans/2026-05-10-issue-53-tier1-cache.md deleted file mode 100644 index 91d6210..0000000 --- a/docs/superpowers/plans/2026-05-10-issue-53-tier1-cache.md +++ /dev/null @@ -1,2023 +0,0 @@ -# Tier 1 Entity-Classification Cache Implementation Plan - -> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. - -**Goal:** Drop `WbDrawDispatcher` entity dispatcher CPU median from ~3.5 ms to ≀ 2.0 ms by caching per-entity classification results for static entities, while holding animation correctness via a `_animatedEntities` membership predicate and a DEBUG cross-check guard. - -**Architecture:** New pure-CPU class `EntityClassificationCache` (separate file, ctor-injected into the dispatcher) holds `Dictionary`. Dispatcher checks `_animatedEntities` membership at the top of the per-entity loop; static entities go through the cache (miss β†’ populate; hit β†’ fast path that walks the cached flat batch list and appends `RestPose * entityWorld` matrices). Two invalidation hooks: `InvalidateEntity` from `RemoveLiveEntityByServerGuid` (live despawn) and `InvalidateLandblock` from `GpuWorldState.RemoveEntitiesFromLandblock` (LB demote/unload, wired via callback at `GameWindow` construction). DEBUG-only cross-check recomputes live state and asserts it matches cached, catching the prior Tier 1 bug class. - -**Tech Stack:** C# / .NET 10 preview / Silk.NET / xUnit / FluentAssertions. Repository at `C:\Users\erikn\source\repos\acdream`. Worktree branch `claude/friendly-varahamihira-7b8664`. - -**Spec foundation:** [docs/superpowers/specs/2026-05-10-issue-53-tier1-cache-design.md](../specs/2026-05-10-issue-53-tier1-cache-design.md). -**Audit foundation:** [docs/research/2026-05-10-tier1-mutation-audit.md](../../research/2026-05-10-tier1-mutation-audit.md). - ---- - -## File Structure - -| File | Status | Responsibility | -|---|---|---| -| `src/AcDream.App/Rendering/Wb/GroupKey.cs` | NEW | Top-level `internal record struct GroupKey` β€” extracted from `WbDrawDispatcher` so the cache can reference it without touching dispatcher internals | -| `src/AcDream.App/Rendering/Wb/EntityClassificationCache.cs` | NEW | Pure-CPU cache class; `Dictionary`; `TryGet` / `Populate` / `InvalidateEntity` / `InvalidateLandblock` + DEBUG cross-check | -| `src/AcDream.App/Rendering/Wb/CachedBatch.cs` | NEW | Top-level `public readonly record struct CachedBatch` + `public sealed class EntityCacheEntry` | -| `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` | MODIFIED | Add cache ctor param; restructure `Draw` per-entity branch; extend `ClassifyBatches` with optional collector | -| `src/AcDream.App/Rendering/GameWindow.cs` | MODIFIED | Construct `EntityClassificationCache`; pass to dispatcher; wire `InvalidateEntity` at the despawn site (line ~2935); wire `InvalidateLandblock` callback into `GpuWorldState` ctor | -| `src/AcDream.App/Streaming/GpuWorldState.cs` | MODIFIED | Optional `Action?` invalidation callback parameter on the constructor; invoked from `RemoveEntitiesFromLandblock` | -| `tests/AcDream.Core.Tests/Rendering/Wb/EntityClassificationCacheTests.cs` | NEW | 12+ pure-CPU tests covering TryGet / Populate / Invalidate paths + Setup pre-flatten + DEBUG cross-check | -| `tests/AcDream.Core.Tests/Rendering/Wb/WbDrawDispatcherBucketingTests.cs` | MODIFIED | +2 integration tests for cache routing; existing tests adapted for new ctor param | - -**Out of scope (do NOT touch):** `mesh_modern.vert`, `mesh_modern.frag`, `TerrainModernRenderer`, `WbMeshAdapter`, `TextureCache`, sky/particles/EnvCell renderers, GPU upload pipeline. - ---- - -## Pre-flight (do these before Task 1) - -- [ ] **Confirm working tree clean and on the worktree branch.** - -```bash -git status -git branch --show-current -``` - -Expected: `working tree clean`, current branch `claude/friendly-varahamihira-7b8664`. - -- [ ] **Confirm baseline: build green + 1688/8 tests + 94/94 N.5b sentinel.** - -```powershell -dotnet build -``` - -Expected: `Build succeeded. 0 Error(s)`. - -```powershell -dotnet test --no-build --filter "FullyQualifiedName~TerrainSlot|FullyQualifiedName~TerrainModernConformance|FullyQualifiedName~Wb|FullyQualifiedName~MatrixComposition|FullyQualifiedName~TextureCacheBindless|FullyQualifiedName~SplitFormulaDivergence" -``` - -Expected: `Passed! - Failed: 0, Passed: 94, Skipped: 0, Total: 94`. - -If submodules missing: `git submodule update --init --recursive references/WorldBuilder`. - ---- - -## Phase 1: Cache foundation (Tasks 1-5) - -### Task 1: Extract `GroupKey` to its own file - -**Files:** -- Create: `src/AcDream.App/Rendering/Wb/GroupKey.cs` -- Modify: `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs:923-930` (remove the nested type) - -This is a mechanical refactor so the cache can reference `GroupKey` without depending on `WbDrawDispatcher`'s private members. - -- [ ] **Step 1: Create the new file.** - -`src/AcDream.App/Rendering/Wb/GroupKey.cs`: - -```csharp -using AcDream.Core.Meshing; - -namespace AcDream.App.Rendering.Wb; - -/// -/// Bucket identity for 's per-frame group dictionary. -/// Two (entity, batch) pairs that share the same render -/// in a single glMultiDrawElementsIndirect draw command. Promoted to -/// internal at file scope (was a private nested type) so -/// can store it inside -/// without depending on dispatcher internals. -/// -internal readonly record struct GroupKey( - uint Ibo, - uint FirstIndex, - int BaseVertex, - int IndexCount, - ulong BindlessTextureHandle, - uint TextureLayer, - TranslucencyKind Translucency); -``` - -- [ ] **Step 2: Remove the nested `GroupKey` from the dispatcher.** - -In `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs`, delete lines 923-930 (the `private readonly record struct GroupKey(...)` block). Leave the surrounding code unchanged. - -- [ ] **Step 3: Build to verify the refactor compiled.** - -```powershell -dotnet build -``` - -Expected: `Build succeeded. 0 Error(s)`. If it fails because some test or code referenced `WbDrawDispatcher.GroupKey`, change those references to use the bare `GroupKey` (now `internal` at namespace scope). - -- [ ] **Step 4: Run the full test suite to verify no behavior change.** - -```powershell -dotnet test --no-build -``` - -Expected: `Failed: 8, Passed: 1688` (baseline preserved). - -- [ ] **Step 5: Commit.** - -```bash -git add src/AcDream.App/Rendering/Wb/GroupKey.cs src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs -git commit -m "refactor(render): extract WbDrawDispatcher.GroupKey to internal type at namespace scope - -Mechanical refactor: GroupKey was a private nested record struct on -WbDrawDispatcher. The upcoming EntityClassificationCache (ISSUE #53) needs -to store GroupKey inside CachedBatch records, so it must be visible to -both the dispatcher and the cache. Promoting to internal at file scope is -the smallest change that achieves this. - -No behavior change. 1688 tests pass; 8 pre-existing failures unchanged. - -Co-Authored-By: Claude Opus 4.7 (1M context) " -``` - ---- - -### Task 2: Skeleton β€” `EntityClassificationCache` + `CachedBatch` + first test - -**Files:** -- Create: `src/AcDream.App/Rendering/Wb/CachedBatch.cs` -- Create: `src/AcDream.App/Rendering/Wb/EntityClassificationCache.cs` -- Create: `tests/AcDream.Core.Tests/Rendering/Wb/EntityClassificationCacheTests.cs` - -The test file references the `internal` `GroupKey`; if `AcDream.App` doesn't already grant `InternalsVisibleTo("AcDream.Core.Tests")`, add it as part of this task. - -- [ ] **Step 1: Write the first failing test.** - -`tests/AcDream.Core.Tests/Rendering/Wb/EntityClassificationCacheTests.cs`: - -```csharp -using System.Collections.Generic; -using System.Numerics; -using AcDream.App.Rendering.Wb; -using AcDream.Core.Meshing; -using FluentAssertions; -using Xunit; - -namespace AcDream.Core.Tests.Rendering.Wb; - -public class EntityClassificationCacheTests -{ - [Fact] - public void TryGet_EmptyCache_ReturnsFalse() - { - var cache = new EntityClassificationCache(); - bool found = cache.TryGet(entityId: 42, out var entry); - found.Should().BeFalse(); - entry.Should().BeNull(); - } - - private static CachedBatch MakeCachedBatch( - uint ibo, uint firstIndex, int indexCount, ulong texHandle) - { - var key = new GroupKey( - Ibo: ibo, - FirstIndex: firstIndex, - BaseVertex: 0, - IndexCount: indexCount, - BindlessTextureHandle: texHandle, - TextureLayer: 0, - Translucency: TranslucencyKind.Opaque); - return new CachedBatch(key, texHandle, Matrix4x4.Identity); - } -} -``` - -- [ ] **Step 2: Add `InternalsVisibleTo` if needed.** - -Check if `AcDream.App` already exposes internals to `AcDream.Core.Tests`: - -```powershell -Select-String -Path src/AcDream.App/**/*.cs, src/AcDream.App/AcDream.App.csproj -Pattern "InternalsVisibleTo" -``` - -If no hit, add a new file `src/AcDream.App/Properties/AssemblyInfo.cs`: - -```csharp -using System.Runtime.CompilerServices; - -[assembly: InternalsVisibleTo("AcDream.Core.Tests")] -``` - -(Place it under `Properties/` to follow the conventional .NET assembly-info pattern; if `AcDream.App` already has another conventional location, use that instead.) - -- [ ] **Step 3: Run the test to verify it fails to compile.** - -```powershell -dotnet test --no-build --filter "FullyQualifiedName~EntityClassificationCacheTests" -``` - -Expected: build error β€” `EntityClassificationCache`, `CachedBatch` don't exist yet. - -- [ ] **Step 4: Create `CachedBatch.cs`.** - -`src/AcDream.App/Rendering/Wb/CachedBatch.cs`: - -```csharp -using System.Numerics; - -namespace AcDream.App.Rendering.Wb; - -/// -/// Per-(entity, partIdx, batchIdx) classification result, stored flat inside -/// . For Setup multi-part MeshRefs each -/// subPart contributes its own entries, with -/// already containing the -/// subPart.PartTransform * meshRef.PartTransform product. -/// -public readonly record struct CachedBatch( - GroupKey Key, - ulong BindlessTextureHandle, - Matrix4x4 RestPose); - -/// -/// One entity's cached classification. is flat across -/// (partIdx, batchIdx) and ordered as WbDrawDispatcher.ClassifyBatches -/// produced them. lets -/// sweep entries -/// efficiently when a landblock demotes or unloads. -/// -public sealed class EntityCacheEntry -{ - public required uint EntityId { get; init; } - public required uint LandblockHint { get; init; } - public required CachedBatch[] Batches { get; init; } -} -``` - -- [ ] **Step 5: Create `EntityClassificationCache.cs` skeleton.** - -`src/AcDream.App/Rendering/Wb/EntityClassificationCache.cs`: - -```csharp -using System.Collections.Generic; - -namespace AcDream.App.Rendering.Wb; - -/// -/// Cache of per-entity classification results for static entities (those NOT -/// in GameWindow._animatedEntities). Holds one -/// per cached entity. The cache is opaque -/// w.r.t. classification logic β€” it simply stores what callers populate. -/// -/// -/// Invariants: -/// -/// overwrites any existing entry for the same id (defensive). -/// is idempotent (no-throw on missing id). -/// walks all entries; entries whose -/// equals the argument are removed. -/// All operations are render-thread only. No internal locking. -/// -/// -/// -/// -/// Audit foundation: see -/// docs/research/2026-05-10-tier1-mutation-audit.md for why static -/// entities can be cached and what invalidation is needed. -/// -/// -public sealed class EntityClassificationCache -{ - private readonly Dictionary _entries = new(); - - /// Number of cached entities β€” for diagnostics. - public int Count => _entries.Count; - - /// - /// Look up an entity's cached classification. Returns true with - /// the entry on hit; false with set to - /// null on miss. - /// - public bool TryGet(uint entityId, out EntityCacheEntry? entry) - => _entries.TryGetValue(entityId, out entry); -} -``` - -- [ ] **Step 6: Run the test to verify it passes.** - -```powershell -dotnet build -dotnet test --no-build --filter "FullyQualifiedName~EntityClassificationCacheTests" -``` - -Expected: `Passed: 1, Failed: 0`. - -- [ ] **Step 7: Commit.** - -```bash -git add src/AcDream.App/Rendering/Wb/CachedBatch.cs src/AcDream.App/Rendering/Wb/EntityClassificationCache.cs tests/AcDream.Core.Tests/Rendering/Wb/EntityClassificationCacheTests.cs -test -f src/AcDream.App/Properties/AssemblyInfo.cs && git add src/AcDream.App/Properties/AssemblyInfo.cs -git commit -m "feat(render #53): EntityClassificationCache skeleton + first test - -Adds CachedBatch, EntityCacheEntry, and EntityClassificationCache with -just TryGet (returns false on empty). The skeleton compiles and the first -test (TryGet_EmptyCache_ReturnsFalse) passes. Subsequent tasks add -Populate, InvalidateEntity, InvalidateLandblock, and the dispatcher -integration. Per spec design Β§6.1. - -Co-Authored-By: Claude Opus 4.7 (1M context) " -``` - ---- - -### Task 3: `Populate` + roundtrip + Setup pre-flatten tests - -**Files:** -- Modify: `src/AcDream.App/Rendering/Wb/EntityClassificationCache.cs` -- Modify: `tests/AcDream.Core.Tests/Rendering/Wb/EntityClassificationCacheTests.cs` - -Adds tests #2, #3, #9, #10, #14 from the spec test plan. All exercise the populate-then-tryget round-trip including the Setup pre-flatten shape. - -- [ ] **Step 1: Write the failing tests.** - -Append to `tests/AcDream.Core.Tests/Rendering/Wb/EntityClassificationCacheTests.cs` (just BEFORE the `private static CachedBatch MakeCachedBatch` helper): - -```csharp - [Fact] - public void Populate_ThenTryGet_ReturnsBatchesInOrder() - { - var cache = new EntityClassificationCache(); - var batches = new[] - { - MakeCachedBatch(ibo: 1, firstIndex: 0, indexCount: 6, texHandle: 0xAA), - MakeCachedBatch(ibo: 1, firstIndex: 6, indexCount: 6, texHandle: 0xBB), - }; - - cache.Populate(entityId: 100, landblockHint: 0xA9B40000u, batches); - - cache.TryGet(100, out var entry).Should().BeTrue(); - entry!.EntityId.Should().Be(100u); - entry.LandblockHint.Should().Be(0xA9B40000u); - entry.Batches.Should().Equal(batches); - } - - [Fact] - public void Populate_OverridesExistingEntry() - { - var cache = new EntityClassificationCache(); - cache.Populate(100, 0u, new[] { MakeCachedBatch(1, 0, 6, 0xAA) }); - cache.Populate(100, 0u, new[] { MakeCachedBatch(2, 0, 12, 0xCC) }); - - cache.TryGet(100, out var entry).Should().BeTrue(); - entry!.Batches.Should().HaveCount(1); - entry.Batches[0].BindlessTextureHandle.Should().Be(0xCCu); - } - - [Fact] - public void Count_TracksLiveEntries() - { - var cache = new EntityClassificationCache(); - cache.Count.Should().Be(0); - - cache.Populate(1, 0u, new[] { MakeCachedBatch(1, 0, 6, 0xAA) }); - cache.Count.Should().Be(1); - - cache.Populate(2, 0u, new[] { MakeCachedBatch(2, 0, 6, 0xAA) }); - cache.Count.Should().Be(2); - - // Re-populate same id β€” should not double-count. - cache.Populate(1, 0u, new[] { MakeCachedBatch(3, 0, 6, 0xBB) }); - cache.Count.Should().Be(2); - } - - [Fact] - public void Populate_WithEmptyBatches_StoresEmptyEntry() - { - var cache = new EntityClassificationCache(); - cache.Populate(entityId: 7, landblockHint: 0u, System.Array.Empty()); - - cache.TryGet(7, out var entry).Should().BeTrue(); - entry!.Batches.Should().BeEmpty(); - } - - [Fact] - public void Populate_SetupMultiPart_StoresFlatBatchPerSubPart() - { - // Synthetic Setup with 3 subParts Γ— 2 batches each = 6 flat entries. - // This pins the spec Β§3 Q4 decision: pre-flatten Setup multi-parts at - // populate time so the per-frame hot path is branchless. - var cache = new EntityClassificationCache(); - var batches = new CachedBatch[6]; - for (int subPart = 0; subPart < 3; subPart++) - for (int b = 0; b < 2; b++) - { - batches[subPart * 2 + b] = MakeCachedBatch( - ibo: (uint)(subPart + 1), - firstIndex: (uint)(b * 6), - indexCount: 6, - texHandle: (ulong)(0x100 + subPart * 2 + b)); - } - cache.Populate(99, 0u, batches); - - cache.TryGet(99, out var entry).Should().BeTrue(); - entry!.Batches.Should().HaveCount(6); - entry.Batches[0].BindlessTextureHandle.Should().Be(0x100u); - entry.Batches[5].BindlessTextureHandle.Should().Be(0x105u); - } -``` - -- [ ] **Step 2: Run tests, verify they fail to compile.** - -```powershell -dotnet build -``` - -Expected: build error β€” `Populate` does not exist on `EntityClassificationCache`. - -- [ ] **Step 3: Implement `Populate`.** - -Add to `EntityClassificationCache.cs`: - -```csharp - /// - /// Insert or overwrite a cache entry for . - /// Defensive: if an entry already exists, replaces it. - /// - public void Populate(uint entityId, uint landblockHint, CachedBatch[] batches) - { - _entries[entityId] = new EntityCacheEntry - { - EntityId = entityId, - LandblockHint = landblockHint, - Batches = batches, - }; - } -``` - -- [ ] **Step 4: Run all cache tests.** - -```powershell -dotnet build -dotnet test --no-build --filter "FullyQualifiedName~EntityClassificationCacheTests" -``` - -Expected: 6 tests pass (1 from Task 2 + 5 new). - -- [ ] **Step 5: Commit.** - -```bash -git add src/AcDream.App/Rendering/Wb/EntityClassificationCache.cs tests/AcDream.Core.Tests/Rendering/Wb/EntityClassificationCacheTests.cs -git commit -m "feat(render #53): EntityClassificationCache.Populate + roundtrip tests - -Implements Populate (insert-or-overwrite) and adds 5 tests covering the -populateβ†’TryGet round-trip including the Setup pre-flatten shape. Per -spec test plan Β§7.1 tests #2, #3, #9, #10, #14. - -Co-Authored-By: Claude Opus 4.7 (1M context) " -``` - ---- - -### Task 4: `InvalidateEntity` + tests #4, #5 - -**Files:** -- Modify: `src/AcDream.App/Rendering/Wb/EntityClassificationCache.cs` -- Modify: `tests/AcDream.Core.Tests/Rendering/Wb/EntityClassificationCacheTests.cs` - -- [ ] **Step 1: Write the failing tests.** - -Append (just before the `MakeCachedBatch` helper): - -```csharp - [Fact] - public void InvalidateEntity_RemovesEntry() - { - var cache = new EntityClassificationCache(); - cache.Populate(100, 0u, new[] { MakeCachedBatch(1, 0, 6, 0xAA) }); - cache.TryGet(100, out _).Should().BeTrue(); - - cache.InvalidateEntity(100); - - cache.TryGet(100, out var entry).Should().BeFalse(); - entry.Should().BeNull(); - cache.Count.Should().Be(0); - } - - [Fact] - public void InvalidateEntity_OnMissingId_NoThrow() - { - var cache = new EntityClassificationCache(); - var act = () => cache.InvalidateEntity(99999); - act.Should().NotThrow(); - cache.Count.Should().Be(0); - } -``` - -- [ ] **Step 2: Run tests, verify they fail to compile.** - -```powershell -dotnet build -``` - -Expected: build error β€” `InvalidateEntity` not defined. - -- [ ] **Step 3: Implement `InvalidateEntity`.** - -Add to `EntityClassificationCache.cs`: - -```csharp - /// - /// Remove the cache entry for . No-op if the - /// id isn't cached. - /// - public void InvalidateEntity(uint entityId) - { - _entries.Remove(entityId); - } -``` - -- [ ] **Step 4: Run tests.** - -```powershell -dotnet test --no-build --filter "FullyQualifiedName~EntityClassificationCacheTests" -``` - -Expected: 8 tests pass. - -- [ ] **Step 5: Commit.** - -```bash -git add src/AcDream.App/Rendering/Wb/EntityClassificationCache.cs tests/AcDream.Core.Tests/Rendering/Wb/EntityClassificationCacheTests.cs -git commit -m "feat(render #53): EntityClassificationCache.InvalidateEntity + tests - -Idempotent removal of a cached entry by entity id. Tests #4 and #5 from -spec Β§7.1 lock in the contract. - -Co-Authored-By: Claude Opus 4.7 (1M context) " -``` - ---- - -### Task 5: `InvalidateLandblock` + tests #6, #7, #8 - -**Files:** -- Modify: `src/AcDream.App/Rendering/Wb/EntityClassificationCache.cs` -- Modify: `tests/AcDream.Core.Tests/Rendering/Wb/EntityClassificationCacheTests.cs` - -- [ ] **Step 1: Write the failing tests.** - -Append (just before the `MakeCachedBatch` helper): - -```csharp - [Fact] - public void InvalidateLandblock_RemovesAllMatchingEntries() - { - var cache = new EntityClassificationCache(); - cache.Populate(1, 0xA9B40000u, new[] { MakeCachedBatch(1, 0, 6, 0xAA) }); - cache.Populate(2, 0xA9B40000u, new[] { MakeCachedBatch(2, 0, 6, 0xBB) }); - cache.Populate(3, 0xA9B40000u, new[] { MakeCachedBatch(3, 0, 6, 0xCC) }); - cache.Count.Should().Be(3); - - cache.InvalidateLandblock(0xA9B40000u); - - cache.Count.Should().Be(0); - cache.TryGet(1, out _).Should().BeFalse(); - cache.TryGet(2, out _).Should().BeFalse(); - cache.TryGet(3, out _).Should().BeFalse(); - } - - [Fact] - public void InvalidateLandblock_LeavesNonMatchingEntries() - { - var cache = new EntityClassificationCache(); - cache.Populate(1, 0xA9B40000u, new[] { MakeCachedBatch(1, 0, 6, 0xAA) }); - cache.Populate(2, 0xA9B50000u, new[] { MakeCachedBatch(2, 0, 6, 0xBB) }); - cache.Populate(3, 0xA9B40000u, new[] { MakeCachedBatch(3, 0, 6, 0xCC) }); - - cache.InvalidateLandblock(0xA9B40000u); - - cache.Count.Should().Be(1); - cache.TryGet(1, out _).Should().BeFalse(); - cache.TryGet(2, out var keep).Should().BeTrue(); - keep!.LandblockHint.Should().Be(0xA9B50000u); - cache.TryGet(3, out _).Should().BeFalse(); - } - - [Fact] - public void InvalidateLandblock_OnMissingLb_NoThrow() - { - var cache = new EntityClassificationCache(); - cache.Populate(1, 0xA9B40000u, new[] { MakeCachedBatch(1, 0, 6, 0xAA) }); - var act = () => cache.InvalidateLandblock(0xDEADBEEFu); - act.Should().NotThrow(); - cache.Count.Should().Be(1); - } -``` - -- [ ] **Step 2: Run tests, verify failure.** - -```powershell -dotnet build -``` - -Expected: build error β€” `InvalidateLandblock` not defined. - -- [ ] **Step 3: Implement `InvalidateLandblock`.** - -Add to `EntityClassificationCache.cs`: - -```csharp - /// - /// Remove every cache entry whose - /// equals . Used by the streaming pipeline - /// when a landblock demotes from near to far or unloads. No-op if no - /// entries match. - /// - public void InvalidateLandblock(uint landblockId) - { - if (_entries.Count == 0) return; - - // Collect the ids to remove first to avoid mutating the dict during iteration. - // Buffered locally because the typical case removes ~all entries in the LB - // (which is still small relative to the total cache). - List? toRemove = null; - foreach (var (id, entry) in _entries) - { - if (entry.LandblockHint == landblockId) - { - toRemove ??= new List(); - toRemove.Add(id); - } - } - if (toRemove is null) return; - foreach (var id in toRemove) _entries.Remove(id); - } -``` - -- [ ] **Step 4: Run tests.** - -```powershell -dotnet test --no-build --filter "FullyQualifiedName~EntityClassificationCacheTests" -``` - -Expected: 11 tests pass (1 + 5 + 2 + 3). - -- [ ] **Step 5: Commit.** - -```bash -git add src/AcDream.App/Rendering/Wb/EntityClassificationCache.cs tests/AcDream.Core.Tests/Rendering/Wb/EntityClassificationCacheTests.cs -git commit -m "feat(render #53): EntityClassificationCache.InvalidateLandblock + tests - -Sweep-by-landblock removal for the streaming demote/unload path. Tests -#6, #7, #8 from spec Β§7.1 lock in: (a) all matching entries removed, (b) -non-matching entries preserved, (c) idempotent on missing LB. - -Phase 1 (cache foundation) complete. 11 cache tests passing. - -Co-Authored-By: Claude Opus 4.7 (1M context) " -``` - ---- - -### Phase 1 checkpoint - -- [ ] **Run full suite + N.5b sentinel before moving to Phase 2.** - -```powershell -dotnet test --no-build --filter "FullyQualifiedName~TerrainSlot|FullyQualifiedName~TerrainModernConformance|FullyQualifiedName~Wb|FullyQualifiedName~MatrixComposition|FullyQualifiedName~TextureCacheBindless|FullyQualifiedName~SplitFormulaDivergence" -``` - -Expected: 94 + 11 = at least 105 passing in the filter (the new EntityClassificationCacheTests are matched by `Wb`). - -```powershell -dotnet test --no-build -``` - -Expected: `Failed: 8, Passed: 1699` (8 pre-existing + 11 new cache tests added on top of 1688 baseline). - -If anything regresses here, STOP and diagnose before Phase 2. - ---- - -## Phase 2: Dispatcher integration (Tasks 6-10) - -### Task 6: Plumb landblockId through `_walkScratch` - -**Files:** -- Modify: `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` (~lines 116, 192, 220, 241-247, 367, 273, 299-300) -- Modify: existing tests in `tests/AcDream.Core.Tests/Rendering/Wb/WbDrawDispatcherBucketingTests.cs` if they construct walkScratch tuples - -The cache populates `LandblockHint` from the walk's outer-loop `LandblockEntry.LandblockId`. Today the inner `_walkScratch` is `List<(WorldEntity Entity, int MeshRefIndex)>` β€” no LB. Extend to a 3-tuple including the landblock id. - -- [ ] **Step 1: Find every reference to the existing 2-tuple shape.** - -```powershell -Select-String -Path src/**/*.cs, tests/**/*.cs -Pattern "List<\(WorldEntity" -Select-String -Path src/**/*.cs, tests/**/*.cs -Pattern "WalkResult" -``` - -Expected hits: `WbDrawDispatcher.cs` (declaration + WalkResult type + body), possibly `WbDrawDispatcherBucketingTests.cs`. - -- [ ] **Step 2: Update the `_walkScratch` field type and `WalkResult.ToDraw` type.** - -In `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs`: - -Change line 116: -```csharp -private readonly List<(WorldEntity Entity, int MeshRefIndex)> _walkScratch = new(); -``` -to: -```csharp -private readonly List<(WorldEntity Entity, int MeshRefIndex, uint LandblockId)> _walkScratch = new(); -``` - -Change line 192: -```csharp -public List<(WorldEntity Entity, int MeshRefIndex)> ToDraw; -``` -to: -```csharp -public List<(WorldEntity Entity, int MeshRefIndex, uint LandblockId)> ToDraw; -``` - -- [ ] **Step 3: Update `WalkEntities` (the test-friendly overload) signature.** - -Change line 220-233: -```csharp -internal static WalkResult WalkEntities( - IEnumerable landblockEntries, - FrustumPlanes? frustum, - uint? neverCullLandblockId, - HashSet? visibleCellIds, - HashSet? animatedEntityIds) -{ - var scratch = new List<(WorldEntity Entity, int MeshRefIndex)>(); - var result = new WalkResult { ToDraw = scratch }; - WalkEntitiesInto( - landblockEntries, frustum, neverCullLandblockId, - visibleCellIds, animatedEntityIds, scratch, ref result); - return result; -} -``` -to: -```csharp -internal static WalkResult WalkEntities( - IEnumerable landblockEntries, - FrustumPlanes? frustum, - uint? neverCullLandblockId, - HashSet? visibleCellIds, - HashSet? animatedEntityIds) -{ - var scratch = new List<(WorldEntity Entity, int MeshRefIndex, uint LandblockId)>(); - var result = new WalkResult { ToDraw = scratch }; - WalkEntitiesInto( - landblockEntries, frustum, neverCullLandblockId, - visibleCellIds, animatedEntityIds, scratch, ref result); - return result; -} -``` - -- [ ] **Step 4: Update `WalkEntitiesInto` signature + body.** - -Change line 241-247 to take the new tuple shape: - -```csharp -internal static void WalkEntitiesInto( - IEnumerable landblockEntries, - FrustumPlanes? frustum, - uint? neverCullLandblockId, - HashSet? visibleCellIds, - HashSet? animatedEntityIds, - List<(WorldEntity Entity, int MeshRefIndex, uint LandblockId)> scratch, - ref WalkResult result) -``` - -Inside the body, every `scratch.Add((entity, i))` becomes `scratch.Add((entity, i, entry.LandblockId))`. Two such lines: ~273 (animated-only branch) and ~299-300 (full walk branch). Concretely: - -Line ~273 (inside the animated-only frustum-culled branch): -```csharp -for (int i = 0; i < entity.MeshRefs.Count; i++) - scratch.Add((entity, i, entry.LandblockId)); -``` - -Line ~299-300 (inside the full walk branch): -```csharp -for (int i = 0; i < entity.MeshRefs.Count; i++) - scratch.Add((entity, i, entry.LandblockId)); -``` - -- [ ] **Step 5: Update the consumer in `Draw`.** - -At line 367: -```csharp -foreach (var (entity, partIdx) in _walkScratch) -``` -becomes: -```csharp -foreach (var (entity, partIdx, landblockId) in _walkScratch) -``` - -The `landblockId` is unused for now (consumed in Task 9 for `Populate`'s `landblockHint` argument). Suppress any `landblockId` unused-variable warning by prefixing `_` if necessary, but only if the C# compiler emits a warning (it shouldn't for tuple deconstruction). - -- [ ] **Step 6: Build to verify the type plumbed cleanly.** - -```powershell -dotnet build -``` - -Expected: `Build succeeded. 0 Error(s)`. If existing tests in `WbDrawDispatcherBucketingTests.cs` reference the 2-tuple shape, update them to the 3-tuple form (add `0u` or a deterministic landblock id as the third element). - -- [ ] **Step 7: Run full suite.** - -```powershell -dotnet test --no-build -``` - -Expected: `Failed: 8, Passed: 1699` β€” same as Phase 1 checkpoint. The walk now carries an extra field but no behavior changed yet. - -- [ ] **Step 8: Commit.** - -```bash -git add src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs tests/AcDream.Core.Tests/Rendering/Wb/WbDrawDispatcherBucketingTests.cs -git commit -m "refactor(render #53): plumb landblockId through WbDrawDispatcher walkScratch - -Extends the walk scratch tuple from (entity, meshRefIndex) to -(entity, meshRefIndex, landblockId). The dispatcher's per-entity loop now -has the landblock id available for EntityClassificationCache.Populate's -landblockHint argument (consumed in Task 9). No behavior change. - -Co-Authored-By: Claude Opus 4.7 (1M context) " -``` - ---- - -### Task 7: Wire `EntityClassificationCache` into the dispatcher ctor + `GameWindow` - -**Files:** -- Modify: `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` (ctor signature + private field) -- Modify: `src/AcDream.App/Rendering/GameWindow.cs` (instantiate + pass) -- Modify: `tests/AcDream.Core.Tests/Rendering/Wb/WbDrawDispatcherBucketingTests.cs` (existing test fixtures pass an empty cache) - -- [ ] **Step 1: Add the field + ctor parameter to `WbDrawDispatcher`.** - -In `WbDrawDispatcher.cs`, add a private readonly field next to the others (~line 70): - -```csharp -private readonly EntityClassificationCache _cache; -``` - -Update the ctor signature at line 142-148: - -```csharp -public WbDrawDispatcher( - GL gl, - Shader shader, - TextureCache textures, - WbMeshAdapter meshAdapter, - EntitySpawnAdapter entitySpawnAdapter, - BindlessSupport bindless, - EntityClassificationCache classificationCache) -``` - -Add the assignment at the end of the ctor body (~line 165), with the existing null-checks: - -```csharp -ArgumentNullException.ThrowIfNull(classificationCache); -_cache = classificationCache; -``` - -- [ ] **Step 2: Construct and pass the cache from `GameWindow`.** - -Find the `WbDrawDispatcher` instantiation in `src/AcDream.App/Rendering/GameWindow.cs`: - -```powershell -Select-String -Path src/AcDream.App/Rendering/GameWindow.cs -Pattern "new WbDrawDispatcher" -``` - -Add a private field on `GameWindow`: - -```csharp -private readonly AcDream.App.Rendering.Wb.EntityClassificationCache _classificationCache = new(); -``` - -(Place it adjacent to the existing `_animatedEntities` field at line ~160 β€” they're conceptually paired.) - -Update the `new WbDrawDispatcher(...)` call site to include the new argument: - -```csharp -_wbDrawDispatcher = new AcDream.App.Rendering.Wb.WbDrawDispatcher( - /* … existing args … */, - _classificationCache); -``` - -- [ ] **Step 3: Update existing dispatcher tests.** - -In `tests/AcDream.Core.Tests/Rendering/Wb/WbDrawDispatcherBucketingTests.cs`, find every `new WbDrawDispatcher(...)` and append `new EntityClassificationCache()` as the final argument. (If tests use a builder/helper method, update that.) - -```powershell -Select-String -Path tests/AcDream.Core.Tests/Rendering/Wb/*.cs -Pattern "new WbDrawDispatcher" -``` - -For each hit, add the new argument. - -- [ ] **Step 4: Build to verify everything compiled.** - -```powershell -dotnet build -``` - -Expected: `Build succeeded. 0 Error(s)`. - -- [ ] **Step 5: Run full suite.** - -```powershell -dotnet test --no-build -``` - -Expected: `Failed: 8, Passed: 1699`. The cache is wired into the dispatcher but isn't used yet β€” no behavior change. - -- [ ] **Step 6: Commit.** - -```bash -git add src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs src/AcDream.App/Rendering/GameWindow.cs tests/AcDream.Core.Tests/Rendering/Wb/WbDrawDispatcherBucketingTests.cs -git commit -m "feat(render #53): inject EntityClassificationCache into WbDrawDispatcher - -Adds the cache as a constructor parameter on WbDrawDispatcher and a -private field on GameWindow. The cache is passed through but not yet -consumed by Draw β€” that wires up in Task 9 (cache miss / populate) and -Task 10 (cache hit / fast path). - -Co-Authored-By: Claude Opus 4.7 (1M context) " -``` - ---- - -### Task 8: Extend `ClassifyBatches` with optional collector - -**Files:** -- Modify: `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` (lines 707-759 β€” the `ClassifyBatches` method) - -- [ ] **Step 1: Change `ClassifyBatches` signature.** - -Change the method declaration at line 707: - -```csharp -private void ClassifyBatches( - ObjectRenderData renderData, - ulong gfxObjId, - Matrix4x4 model, - WorldEntity entity, - MeshRef meshRef, - ulong palHash, - AcSurfaceMetadataTable metaTable) -``` - -to: - -```csharp -private void ClassifyBatches( - ObjectRenderData renderData, - ulong gfxObjId, - Matrix4x4 model, - WorldEntity entity, - MeshRef meshRef, - ulong palHash, - AcSurfaceMetadataTable metaTable, - Matrix4x4 restPose, - List? collector = null) -``` - -The new `restPose` parameter is the model-matrix component WITHOUT `entityWorld` baked in β€” i.e. `meshRef.PartTransform` for non-Setup, or `subPart.PartTransform * meshRef.PartTransform` for Setup. Caller computes it. - -- [ ] **Step 2: Append to the collector inside the per-batch loop.** - -At the bottom of the for loop (after `grp.Matrices.Add(model);` at line 757), add: - -```csharp - collector?.Add(new CachedBatch(key, texHandle, restPose)); -``` - -The full updated block (lines 738-758): - -```csharp - var key = new GroupKey( - batch.IBO, batch.FirstIndex, (int)batch.BaseVertex, - batch.IndexCount, texHandle, texLayer, translucency); - - if (!_groups.TryGetValue(key, out var grp)) - { - grp = new InstanceGroup - { - Ibo = batch.IBO, - FirstIndex = batch.FirstIndex, - BaseVertex = (int)batch.BaseVertex, - IndexCount = batch.IndexCount, - BindlessTextureHandle = texHandle, - TextureLayer = texLayer, - Translucency = translucency, - }; - _groups[key] = grp; - } - grp.Matrices.Add(model); - collector?.Add(new CachedBatch(key, texHandle, restPose)); - } - } -``` - -- [ ] **Step 3: Update `ClassifyBatches` call sites in `Draw` to pass `restPose`.** - -At line 411 (Setup branch): -```csharp -ClassifyBatches(partData, partGfxObjId, model, entity, meshRef, palHash, metaTable); -``` -becomes: -```csharp -var restPose = partTransform * meshRef.PartTransform; -ClassifyBatches(partData, partGfxObjId, model, entity, meshRef, palHash, metaTable, restPose); -``` - -At line 418 (non-Setup branch): -```csharp -var model = meshRef.PartTransform * entityWorld; -ClassifyBatches(renderData, gfxObjId, model, entity, meshRef, palHash, metaTable); -``` -becomes: -```csharp -var model = meshRef.PartTransform * entityWorld; -ClassifyBatches(renderData, gfxObjId, model, entity, meshRef, palHash, metaTable, restPose: meshRef.PartTransform); -``` - -(Use named-arg form on the non-Setup branch to avoid name collision with the Setup branch's local `restPose`.) - -- [ ] **Step 4: Build + test.** - -```powershell -dotnet build -dotnet test --no-build -``` - -Expected: `Failed: 8, Passed: 1699`. No behavior change yet β€” collector defaults to null. - -- [ ] **Step 5: Commit.** - -```bash -git add src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs -git commit -m "feat(render #53): add optional CachedBatch collector to ClassifyBatches - -ClassifyBatches now accepts a restPose parameter (the model-matrix -component without entityWorld baked in) and an optional collector. When -collector is non-null, each classified batch is appended as a CachedBatch -record. Defaults preserve today's behavior. Used in Task 9 to populate -the cache on a static-entity miss. - -Co-Authored-By: Claude Opus 4.7 (1M context) " -``` - ---- - -### Task 9: Wire dispatcher cache-miss path (populate on first frame; no fast-path yet) - -**Files:** -- Modify: `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` (around lines 367-423) - -This task adds the populate logic without the cache-hit fast path. After this task, every static entity's slow path runs exactly once (first frame visible) and produces a populated cache entry; subsequent frames still run the slow path because the fast-path branch isn't in yet. Task 10 adds the fast path. - -The split is deliberate so we can land + verify each half independently. - -- [ ] **Step 1: Add the populate scratch field.** - -Near the other per-frame scratch fields (~line 116): - -```csharp -private readonly List _populateScratch = new(); -``` - -- [ ] **Step 2: Restructure the per-entity loop in `Draw`.** - -Replace lines 367-423 (the foreach + body up through `if (diag && drewAny) _entitiesDrawn++;`) with: - -```csharp -foreach (var (entity, partIdx, landblockId) in _walkScratch) -{ - if (diag) _entitiesSeen++; - - var entityWorld = - Matrix4x4.CreateFromQuaternion(entity.Rotation) * - Matrix4x4.CreateTranslation(entity.Position); - - bool isAnimated = animatedEntityIds?.Contains(entity.Id) == true; - - // Compute palette-override hash ONCE per entity (perf #4). - ulong palHash = 0; - if (entity.PaletteOverride is not null) - palHash = TextureCache.HashPaletteOverride(entity.PaletteOverride); - - var meshRef = entity.MeshRefs[partIdx]; - ulong gfxObjId = meshRef.GfxObjId; - - var renderData = _meshAdapter.TryGetRenderData(gfxObjId); - if (renderData is null) - { - if (diag) _meshesMissing++; - continue; - } - if (anyVao == 0) anyVao = renderData.VAO; - - // Cache-miss path (animated entities skip cache entirely). - // Static entities collect into _populateScratch on the first frame - // they're visible, so the cache has fresh data for the next frame. - var collector = isAnimated ? null : _populateScratch; - collector?.Clear(); - - bool drewAny = false; - if (renderData.IsSetup && renderData.SetupParts.Count > 0) - { - foreach (var (partGfxObjId, partTransform) in renderData.SetupParts) - { - var partData = _meshAdapter.TryGetRenderData(partGfxObjId); - if (partData is null) continue; - - var model = ComposePartWorldMatrix( - entityWorld, meshRef.PartTransform, partTransform); - var restPose = partTransform * meshRef.PartTransform; - - ClassifyBatches(partData, partGfxObjId, model, entity, meshRef, - palHash, metaTable, restPose, collector); - drewAny = true; - } - } - else - { - var model = meshRef.PartTransform * entityWorld; - ClassifyBatches(renderData, gfxObjId, model, entity, meshRef, - palHash, metaTable, restPose: meshRef.PartTransform, collector: collector); - drewAny = true; - } - - if (collector is not null && collector.Count > 0) - { - // Populate cache for static entity on cache-miss. - // Each entity classifies once at first visibility; subsequent frames - // will hit the fast path (added in Task 10). - _cache.Populate(entity.Id, landblockId, collector.ToArray()); - } - - if (diag && drewAny) _entitiesDrawn++; -} -``` - -- [ ] **Step 3: Build + test.** - -```powershell -dotnet build -dotnet test --no-build -``` - -Expected: `Failed: 8, Passed: 1699`. The slow path now also populates the cache, but visual + per-frame behavior is unchanged (we don't read from the cache yet). - -- [ ] **Step 4: Commit.** - -```bash -git add src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs -git commit -m "feat(render #53): cache-miss populate on first frame for static entities - -Restructures Draw's per-entity loop: animated entities still skip the -cache entirely, but static entities now collect their classification into -_populateScratch and call cache.Populate at the end of the iteration. - -Cache fast-path (skip slow classification on cache hit) lands in Task 10. -This intermediate state is verifiable: behavior unchanged, but the cache -is being populated as entities render. Diagnostic-friendly split. - -Co-Authored-By: Claude Opus 4.7 (1M context) " -``` - ---- - -### Task 10: Wire dispatcher cache-hit fast path + integration tests #11, #12 - -**Files:** -- Modify: `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` -- Modify: `tests/AcDream.Core.Tests/Rendering/Wb/WbDrawDispatcherBucketingTests.cs` - -- [ ] **Step 1: Add the cache-hit branch.** - -In `WbDrawDispatcher.Draw`, just after the `bool isAnimated = ...` line and BEFORE the `palHash` computation, add: - -```csharp - // Fast path: cache hit on a static entity. Skip classification entirely - // and append cached (RestPose * entityWorld) matrices to the matching - // groups. The DEBUG cross-check (added in Task 13) asserts the - // membership predicate held at hit time. - if (!isAnimated && _cache.TryGet(entity.Id, out var cachedEntry)) - { - foreach (var cached in cachedEntry!.Batches) - { - if (!_groups.TryGetValue(cached.Key, out var grp)) - { - grp = new InstanceGroup - { - Ibo = cached.Key.Ibo, - FirstIndex = cached.Key.FirstIndex, - BaseVertex = cached.Key.BaseVertex, - IndexCount = cached.Key.IndexCount, - BindlessTextureHandle = cached.Key.BindlessTextureHandle, - TextureLayer = cached.Key.TextureLayer, - Translucency = cached.Key.Translucency, - }; - _groups[cached.Key] = grp; - } - grp.Matrices.Add(cached.RestPose * entityWorld); - } - - if (anyVao == 0) - { - // Need a VAO for the GL phase. Look up the first MeshRef's - // mesh data once (cheap dict lookup, not a re-classify). - var firstMeshRef = entity.MeshRefs[partIdx]; - var firstRenderData = _meshAdapter.TryGetRenderData(firstMeshRef.GfxObjId); - if (firstRenderData is not null) anyVao = firstRenderData.VAO; - } - - if (diag) { _entitiesDrawn++; } - continue; - } -``` - -(Note: `_entitiesSeen++` already fired at the top of the loop body; only `_entitiesDrawn++` here.) - -- [ ] **Step 2: Write integration test #11 β€” static entity routes through cache.** - -In `tests/AcDream.Core.Tests/Rendering/Wb/WbDrawDispatcherBucketingTests.cs`, add a test that: - -```csharp - [Fact] - public void Draw_StaticEntity_PopulatesCacheOnFirstFrameAndHitsOnSecond() - { - var cache = new EntityClassificationCache(); - // Use the existing test fixture builder (whatever shape WbDrawDispatcherBucketingTests - // already uses). Pass `cache` as the new ctor argument. - // Construct one synthetic static WorldEntity in landblockEntries. - cache.Count.Should().Be(0); - - // … existing fixture: construct dispatcher + adapter + entity … - // … invoke Draw once … - - // First frame: cache populates. - cache.Count.Should().BeGreaterThan(0); - int firstCount = cache.Count; - - // … invoke Draw again with the same entity … - - // Second frame: cache hit β€” no double-populate. cache.Count is stable. - cache.Count.Should().Be(firstCount); - } -``` - -If the existing test fixture doesn't expose a spy / counter on `WbMeshAdapter`, this test asserts indirectly: after first Draw, `cache.Count == 1`; after second Draw, `cache.Count == 1` still (no double-populate, which would re-overwrite β€” `Populate` overwrite still leaves Count==1, so this test asserts that the populate path is reached on the first Draw and is NOT reached on the second Draw via a stronger spy if the fixture supports one; otherwise the weaker count-stability assert is acceptable). - -If a spy is feasible, prefer it. Pseudocode: - -```csharp -var spyAdapter = new SpyMeshAdapter(realAdapter); -// ... construct dispatcher with spyAdapter ... -spyAdapter.TryGetRenderDataCallCount.Should().Be(N_first_frame_lookups); -// ... invoke second Draw ... -spyAdapter.TryGetRenderDataCallCount.Should().Be(N_first_frame_lookups + 1); -// ↑ +1 for the single VAO lookup in the cache-hit branch, NOT +N for re-classification. -``` - -Choose whichever the existing fixture supports. - -- [ ] **Step 3: Write integration test #12 β€” animated entity bypasses cache.** - -```csharp - [Fact] - public void Draw_AnimatedEntity_DoesNotPopulateCache() - { - var cache = new EntityClassificationCache(); - // Construct dispatcher + adapter + one WorldEntity flagged in - // animatedEntityIds. Invoke Draw. - var animatedIds = new HashSet { /* entity.Id */ }; - // … invoke Draw with animatedEntityIds: animatedIds … - - // Cache should never be populated for animated entities. - cache.Count.Should().Be(0); - } -``` - -- [ ] **Step 4: Run integration tests.** - -```powershell -dotnet build -dotnet test --no-build --filter "FullyQualifiedName~WbDrawDispatcherBucketingTests" -``` - -Expected: existing dispatcher tests + 2 new cache integration tests all pass. - -- [ ] **Step 5: Run full suite.** - -```powershell -dotnet test --no-build -``` - -Expected: `Failed: 8, Passed: 1701` (1688 baseline + 11 cache tests + 2 integration tests = 1701). - -- [ ] **Step 6: Commit.** - -```bash -git add src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs tests/AcDream.Core.Tests/Rendering/Wb/WbDrawDispatcherBucketingTests.cs -git commit -m "feat(render #53): cache-hit fast path + dispatcher integration tests - -WbDrawDispatcher.Draw now branches on cache hit before running classification: -on hit, walks the cached flat batch list and appends RestPose Γ— entityWorld -to the matching groups; on miss, runs today's classification and populates -the cache. Animated entities skip the cache entirely. - -Adds dispatcher integration tests #11 (static entity populates + reuses) -and #12 (animated bypasses) per spec test plan Β§7.2. - -Phase 2 (dispatcher integration) complete. End-to-end caching now live. -Invalidation hooks (Phase 3) ensure correctness across despawns + LB demotes. - -Co-Authored-By: Claude Opus 4.7 (1M context) " -``` - ---- - -### Phase 2 checkpoint - -- [ ] **Run sentinel + full suite.** - -```powershell -dotnet test --no-build --filter "FullyQualifiedName~TerrainSlot|FullyQualifiedName~TerrainModernConformance|FullyQualifiedName~Wb|FullyQualifiedName~MatrixComposition|FullyQualifiedName~TextureCacheBindless|FullyQualifiedName~SplitFormulaDivergence" -``` - -Expected: β‰₯ 107 passing (94 sentinel + 11 cache + 2 integration). 0 failures. - -```powershell -dotnet test --no-build -``` - -Expected: 1701 passed, 8 failed (pre-existing). - ---- - -## Phase 3: Invalidation hooks (Tasks 11-12) - -### Task 11: Wire `InvalidateEntity` from `RemoveLiveEntityByServerGuid` + test #15 - -**Files:** -- Modify: `src/AcDream.App/Rendering/GameWindow.cs` (line ~2935) -- Modify: `tests/AcDream.Core.Tests/Rendering/Wb/EntityClassificationCacheTests.cs` - -- [ ] **Step 1: Write test #15 (despawn-respawn cycle).** - -Append to `EntityClassificationCacheTests.cs` (just before `MakeCachedBatch`): - -```csharp - [Fact] - public void DespawnRespawn_UnderReusedId_RepopulatesFresh() - { - // Pins the audit's ObjDescEvent contract (audit Β§1): - // ObjDescEvent is despawn + respawn (with a NEW local entity.Id), - // never an in-place mutation. Even when an id IS reused - // (theoretical β€” _liveEntityIdCounter is monotonic in practice), - // the cache must serve fresh data after invalidation. - var cache = new EntityClassificationCache(); - var batchesV1 = new[] { MakeCachedBatch(1, 0, 6, 0xAA) }; - var batchesV2 = new[] { MakeCachedBatch(2, 6, 12, 0xCC) }; - - cache.Populate(100, 0xA9B40000u, batchesV1); - cache.InvalidateEntity(100); - cache.Populate(100, 0xA9B40000u, batchesV2); - - cache.TryGet(100, out var entry).Should().BeTrue(); - entry!.Batches.Should().Equal(batchesV2); - entry.Batches[0].BindlessTextureHandle.Should().Be(0xCCu); - } -``` - -- [ ] **Step 2: Run the test, verify it passes (it tests existing API).** - -```powershell -dotnet test --no-build --filter "FullyQualifiedName~DespawnRespawn" -``` - -Expected: pass. (This test pins behavior the cache class already provides; no implementation change needed for the test itself.) - -- [ ] **Step 3: Wire `InvalidateEntity` in `GameWindow.RemoveLiveEntityByServerGuid`.** - -In `src/AcDream.App/Rendering/GameWindow.cs`, find line ~2935: - -```csharp -_animatedEntities.Remove(existingEntity.Id); -``` - -Add immediately after: - -```csharp -_classificationCache.InvalidateEntity(existingEntity.Id); -``` - -- [ ] **Step 4: Build + run full suite.** - -```powershell -dotnet build -dotnet test --no-build -``` - -Expected: `Failed: 8, Passed: 1702`. - -- [ ] **Step 5: Commit.** - -```bash -git add src/AcDream.App/Rendering/GameWindow.cs tests/AcDream.Core.Tests/Rendering/Wb/EntityClassificationCacheTests.cs -git commit -m "feat(render #53): wire EntityClassificationCache.InvalidateEntity at despawn - -GameWindow.RemoveLiveEntityByServerGuid now invalidates the entity's -cache entry next to the existing _animatedEntities.Remove(). Fires for -DeleteObject (0xF747) and the dedup leg of ObjDescEvent (0xF625). - -Adds test #15 (despawn-respawn under reused id repopulates fresh) per -spec Β§7.5 β€” pins the audit's ObjDescEvent-as-despawn-respawn contract. - -Co-Authored-By: Claude Opus 4.7 (1M context) " -``` - ---- - -### Task 12: Wire `InvalidateLandblock` callback into `GpuWorldState.RemoveEntitiesFromLandblock` - -**Files:** -- Modify: `src/AcDream.App/Streaming/GpuWorldState.cs` -- Modify: `src/AcDream.App/Rendering/GameWindow.cs` (the `GpuWorldState` instantiation site) - -Per spec Β§5.3 W3b: pass an `Action?` callback into `GpuWorldState`'s ctor so when `RemoveEntitiesFromLandblock` clears a landblock's entity list, the callback fires once per landblock id. - -- [ ] **Step 1: Add the callback parameter to `GpuWorldState`.** - -In `src/AcDream.App/Streaming/GpuWorldState.cs`, find the ctor (or primary ctor declaration). Add a new optional parameter `Action? onLandblockUnloaded = null`. Store as a field. - -```csharp -private readonly Action? _onLandblockUnloaded; - -// in ctor: -_onLandblockUnloaded = onLandblockUnloaded; -``` - -Modify `RemoveEntitiesFromLandblock` (line 373) to invoke the callback BEFORE zeroing the entity list: - -```csharp -public void RemoveEntitiesFromLandblock(uint landblockId) -{ - uint canonical = (landblockId & 0xFFFF0000u) | 0xFFFFu; - if (!_loaded.TryGetValue(canonical, out var lb)) return; - if (_wbSpawnAdapter is not null) - _wbSpawnAdapter.OnLandblockUnloaded(canonical); - - // Phase Post-A.5 #53: invalidate the EntityClassificationCache for this - // landblock before we drop the entity list. Wired via callback at - // GameWindow construction; null when the cache isn't relevant (tests). - _onLandblockUnloaded?.Invoke(canonical); - - _loaded[canonical] = new LoadedLandblock(lb.LandblockId, lb.Heightmap, System.Array.Empty()); - _pendingByLandblock.Remove(canonical); - RebuildFlatView(); -} -``` - -- [ ] **Step 2: Wire the callback at `GameWindow`.** - -Find the `new GpuWorldState(...)` invocation: - -```powershell -Select-String -Path src/AcDream.App/Rendering/GameWindow.cs -Pattern "new GpuWorldState" -``` - -Add the new argument: - -```csharp -_worldState = new GpuWorldState( - /* … existing args … */, - onLandblockUnloaded: _classificationCache.InvalidateLandblock); -``` - -- [ ] **Step 3: Update existing `GpuWorldState` test fixtures.** - -```powershell -Select-String -Path tests/**/*.cs -Pattern "new GpuWorldState" -``` - -For each hit, the existing tests can omit the new optional parameter (it defaults to null). No change required unless a specific test wants to assert the callback fires. - -- [ ] **Step 4: Build + run full suite.** - -```powershell -dotnet build -dotnet test --no-build -``` - -Expected: `Failed: 8, Passed: 1702` (no new tests in this task β€” invalidation behavior is exercised indirectly through visual + perf gates, plus the optional unit test in Step 5). - -- [ ] **Step 5: (Optional) Add a streaming integration test.** - -If `GpuWorldStateTwoTierTests.cs` makes it easy, add: - -```csharp - [Fact] - public void RemoveEntitiesFromLandblock_FiresUnloadCallbackBeforeClearingEntities() - { - uint? observed = null; - var state = new GpuWorldState( - /* … existing args … */, - onLandblockUnloaded: id => observed = id); - - // Set up: add a synthetic entity to LB 0xA9B40000 via AppendLiveEntity. - // ... - state.RemoveEntitiesFromLandblock(0xA9B40000u); - - observed.Should().Be(0xA9B4FFFFu); // canonicalized - } -``` - -If the fixture is heavy, defer. - -- [ ] **Step 6: Commit.** - -```bash -git add src/AcDream.App/Streaming/GpuWorldState.cs src/AcDream.App/Rendering/GameWindow.cs tests/AcDream.Core.Tests/Streaming/*.cs -git commit -m "feat(render #53): wire EntityClassificationCache.InvalidateLandblock at LB demote/unload - -GpuWorldState.RemoveEntitiesFromLandblock now invokes an optional -Action callback before zeroing the entity list. GameWindow wires -this to EntityClassificationCache.InvalidateLandblock so cache entries -get swept on LB demote (Nearβ†’Far) and unload. Per spec Β§5.3 W3b. - -Phase 3 (invalidation hooks) complete. The cache now stays correct across -all spec-identified mutation events: despawn, ObjDescEvent (despawn+ -respawn), LB demote, LB unload. - -Co-Authored-By: Claude Opus 4.7 (1M context) " -``` - ---- - -### Phase 3 checkpoint - -- [ ] **Run sentinel + full suite.** - -```powershell -dotnet test --no-build --filter "FullyQualifiedName~TerrainSlot|FullyQualifiedName~TerrainModernConformance|FullyQualifiedName~Wb|FullyQualifiedName~MatrixComposition|FullyQualifiedName~TextureCacheBindless|FullyQualifiedName~SplitFormulaDivergence" -``` - -Expected: 0 failures. - -```powershell -dotnet test --no-build -``` - -Expected: `Failed: 8, Passed: 1702`. - ---- - -## Phase 4: DEBUG cross-check (Task 13) - -### Task 13: Add DEBUG cross-check + test #13 - -**Files:** -- Modify: `src/AcDream.App/Rendering/Wb/EntityClassificationCache.cs` -- Modify: `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` (call cross-check on cache hit, DEBUG-only) -- Modify: `tests/AcDream.Core.Tests/Rendering/Wb/EntityClassificationCacheTests.cs` - -- [ ] **Step 1: Add the cross-check method to the cache.** - -Append to `EntityClassificationCache.cs`: - -```csharp -#if DEBUG - /// - /// Asserts that the cached entry for still - /// matches what fresh classification would produce. Catches the prior - /// Tier 1 bug class β€” silent caching of mutable per-frame state β€” by - /// firing when any cached - /// field has drifted from live state. - /// - /// - /// Caller passes per-batch live state (Key, BindlessTextureHandle, RestPose) - /// reconstructed from the same path the populate ran. The cache iterates - /// its stored entries in parallel and asserts equality. - /// - /// - /// - /// Zero cost in Release. In DEBUG, called once per static-entity cache - /// hit per frame β€” adds modest overhead. Acceptable for dev runs. - /// - /// - public void DebugCrossCheck(uint entityId, IReadOnlyList liveBatches) - { - if (!_entries.TryGetValue(entityId, out var entry)) return; - - System.Diagnostics.Debug.Assert( - entry.Batches.Length == liveBatches.Count, - $"EntityClassificationCache: batch count mismatch for entity {entityId}: cached={entry.Batches.Length} live={liveBatches.Count}"); - - for (int i = 0; i < entry.Batches.Length && i < liveBatches.Count; i++) - { - var cached = entry.Batches[i]; - var live = liveBatches[i]; - System.Diagnostics.Debug.Assert( - cached.Key.Equals(live.Key), - $"EntityClassificationCache: GroupKey drift for entity {entityId} batch {i}"); - System.Diagnostics.Debug.Assert( - cached.BindlessTextureHandle == live.BindlessTextureHandle, - $"EntityClassificationCache: texture handle drift for entity {entityId} batch {i}"); - System.Diagnostics.Debug.Assert( - MatrixApproxEqual(cached.RestPose, live.RestPose, epsilon: 1e-5f), - $"EntityClassificationCache: RestPose drift for entity {entityId} batch {i}"); - } - } - - private static bool MatrixApproxEqual(System.Numerics.Matrix4x4 a, System.Numerics.Matrix4x4 b, float epsilon) - { - return System.MathF.Abs(a.M11 - b.M11) <= epsilon && System.MathF.Abs(a.M12 - b.M12) <= epsilon && - System.MathF.Abs(a.M13 - b.M13) <= epsilon && System.MathF.Abs(a.M14 - b.M14) <= epsilon && - System.MathF.Abs(a.M21 - b.M21) <= epsilon && System.MathF.Abs(a.M22 - b.M22) <= epsilon && - System.MathF.Abs(a.M23 - b.M23) <= epsilon && System.MathF.Abs(a.M24 - b.M24) <= epsilon && - System.MathF.Abs(a.M31 - b.M31) <= epsilon && System.MathF.Abs(a.M32 - b.M32) <= epsilon && - System.MathF.Abs(a.M33 - b.M33) <= epsilon && System.MathF.Abs(a.M34 - b.M34) <= epsilon && - System.MathF.Abs(a.M41 - b.M41) <= epsilon && System.MathF.Abs(a.M42 - b.M42) <= epsilon && - System.MathF.Abs(a.M43 - b.M43) <= epsilon && System.MathF.Abs(a.M44 - b.M44) <= epsilon; - } -#endif -``` - -- [ ] **Step 2: Wire the cross-check into the dispatcher's cache-hit path.** - -In `WbDrawDispatcher.Draw`, inside the cache-hit branch from Task 10, AFTER appending matrices, add (DEBUG-only): - -```csharp -#if DEBUG - // Cross-check guard: assert the membership predicate held at hit time. - // The full re-classification cross-check (spec Β§6.5) is a stretch goal; - // this simpler assert catches the prior Tier 1 bug class β€” a static - // entity that turns out to actually be animated would fire here. - System.Diagnostics.Debug.Assert( - !isAnimated, - $"EntityClassificationCache hit on animated entity {entity.Id} β€” invariant violated"); -#endif -``` - -(The full live-state cross-check requires re-running ClassifyBatches with a live collector, which is non-trivial to plumb into the per-entity branch; ship the predicate assert and file a follow-up issue if the team wants the full cross-check later. The unit test in Step 3 still covers `DebugCrossCheck` directly.) - -- [ ] **Step 3: Write test #13 β€” DEBUG cross-check fires on synthetic mismatch.** - -Append to `EntityClassificationCacheTests.cs`: - -```csharp -#if DEBUG - [Fact] - public void DebugCrossCheck_BatchCountMismatch_FiresAssert() - { - var cache = new EntityClassificationCache(); - cache.Populate(100, 0u, new[] - { - MakeCachedBatch(1, 0, 6, 0xAA), - MakeCachedBatch(1, 6, 6, 0xBB), - }); - - // Synthetic "live" with fewer batches β†’ should fire Debug.Assert. - var liveBatches = new[] { MakeCachedBatch(1, 0, 6, 0xAA) }; - - // Capture Debug.Assert via a custom TraceListener. - var originalListeners = new System.Diagnostics.TraceListener[System.Diagnostics.Trace.Listeners.Count]; - System.Diagnostics.Trace.Listeners.CopyTo(originalListeners, 0); - System.Diagnostics.Trace.Listeners.Clear(); - var asserts = new List(); - System.Diagnostics.Trace.Listeners.Add(new CaptureListener(asserts)); - - try - { - cache.DebugCrossCheck(100, liveBatches); - } - finally - { - System.Diagnostics.Trace.Listeners.Clear(); - foreach (var l in originalListeners) System.Diagnostics.Trace.Listeners.Add(l); - } - - asserts.Should().NotBeEmpty(); - string joined = string.Join(" ", asserts); - joined.Should().Contain("batch count mismatch"); - } - - [Fact] - public void DebugCrossCheck_RestPoseMatch_NoAssert() - { - var cache = new EntityClassificationCache(); - var batches = new[] { MakeCachedBatch(1, 0, 6, 0xAA) }; - cache.Populate(100, 0u, batches); - - var originalListeners = new System.Diagnostics.TraceListener[System.Diagnostics.Trace.Listeners.Count]; - System.Diagnostics.Trace.Listeners.CopyTo(originalListeners, 0); - System.Diagnostics.Trace.Listeners.Clear(); - var asserts = new List(); - System.Diagnostics.Trace.Listeners.Add(new CaptureListener(asserts)); - - try - { - cache.DebugCrossCheck(100, batches); - } - finally - { - System.Diagnostics.Trace.Listeners.Clear(); - foreach (var l in originalListeners) System.Diagnostics.Trace.Listeners.Add(l); - } - - asserts.Should().BeEmpty(); - } - - private sealed class CaptureListener : System.Diagnostics.TraceListener - { - private readonly List _captured; - public CaptureListener(List captured) { _captured = captured; } - public override void Write(string? message) { if (message != null) _captured.Add(message); } - public override void WriteLine(string? message) { if (message != null) _captured.Add(message); } - public override void Fail(string? message, string? detailMessage) - { - _captured.Add($"{message}: {detailMessage}"); - } - public override void Fail(string? message) { if (message != null) _captured.Add(message); } - } -#endif -``` - -- [ ] **Step 4: Build + run full suite.** - -```powershell -dotnet build -dotnet test --no-build -``` - -Expected: `Failed: 8, Passed: 1704` (two new DEBUG-only tests; in DEBUG configuration both run). - -- [ ] **Step 5: Commit.** - -```bash -git add src/AcDream.App/Rendering/Wb/EntityClassificationCache.cs src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs tests/AcDream.Core.Tests/Rendering/Wb/EntityClassificationCacheTests.cs -git commit -m "feat(render #53): DEBUG cross-check guards against the prior Tier 1 bug class - -Adds EntityClassificationCache.DebugCrossCheck(entityId, liveBatches) that -asserts cached state matches a live re-classification. Wires a simpler -predicate assert into WbDrawDispatcher's cache-hit branch (asserts -isAnimated == false on cache hit). Tests #13a and #13b cover the -batch-count mismatch and clean-match cases via a custom TraceListener -that captures Debug.Assert calls. - -Zero cost in Release. In DEBUG, the assert fires immediately if a future -regression mutates static-entity state outside the audit's known write -sites β€” the same failure mode that bit the prior Tier 1 attempt. - -Phase 4 complete. Cache + invalidation + safety net all in place. - -Co-Authored-By: Claude Opus 4.7 (1M context) " -``` - ---- - -### Phase 4 checkpoint - -- [ ] **Run sentinel + full suite.** - -```powershell -dotnet test --no-build --filter "FullyQualifiedName~TerrainSlot|FullyQualifiedName~TerrainModernConformance|FullyQualifiedName~Wb|FullyQualifiedName~MatrixComposition|FullyQualifiedName~TextureCacheBindless|FullyQualifiedName~SplitFormulaDivergence" -``` - -Expected: 0 failures. - -```powershell -dotnet test --no-build -``` - -Expected: `Failed: 8, Passed: 1704` in DEBUG (or 1702 in Release where the `#if DEBUG` tests are excluded). - ---- - -## Phase 5: Verification gates (Tasks 14-16) - -### Task 14: Pre-launch sanity β€” full suite + sentinel + grep for TODO/FIXME - -- [ ] **Step 1: Final build green check.** - -```powershell -dotnet build -``` - -Expected: `Build succeeded. 0 Error(s)`. - -- [ ] **Step 2: Full test pass.** - -```powershell -dotnet test --no-build -``` - -Expected: 1704 (or 1702 in Release) passing, 8 pre-existing physics/input failures unchanged. - -- [ ] **Step 3: Sentinel filter pass.** - -```powershell -dotnet test --no-build --filter "FullyQualifiedName~TerrainSlot|FullyQualifiedName~TerrainModernConformance|FullyQualifiedName~Wb|FullyQualifiedName~MatrixComposition|FullyQualifiedName~TextureCacheBindless|FullyQualifiedName~SplitFormulaDivergence" -``` - -Expected: 0 failures. - -- [ ] **Step 4: Grep for any leftover TODO/FIXME the implementation introduced.** - -```powershell -Select-String -Path src/AcDream.App/Rendering/Wb/*.cs -Pattern "TODO|FIXME|XXX" -``` - -Expected: any hits should be intentional (e.g. cross-check stretch-goal note); fix or document if not. - ---- - -### Task 15: Visual gate (USER REQUIRED) - -This step requires the user to launch the live client and visually verify the change. - -- [ ] **Step 1: Confirm baseline behavior (before any visual claims).** - -The user reports build green + tests passing. Implementation agent confirms the spec's acceptance criteria items 1-7 are checked. - -- [ ] **Step 2: Launch the client.** - -```powershell -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -$env:ACDREAM_TEST_HOST = "127.0.0.1" -$env:ACDREAM_TEST_PORT = "9000" -$env:ACDREAM_TEST_USER = "testaccount" -$env:ACDREAM_TEST_PASS = "testpassword" -dotnet run --project src/AcDream.App/AcDream.App.csproj --no-build -c Debug 2>&1 | Tee-Object -FilePath launch-tier1-visual.log -``` - -- [ ] **Step 3: User walks Holtburg β†’ North Yanshi at horizon-safe preset.** - -Confirm visually: -- A nearby NPC (any creature) animates normally β€” limbs move, idle breathing visible. -- The Holtburg lifestone crystal (Z=94 platform) renders correctly and animates (rotation / glow). -- Static buildings render at correct positions (no offsets, no missing parts). -- No new visual artifacts. - -If any of the above fail, **STOP**: file a sub-issue, diagnose, and either fix or revert before continuing. - -- [ ] **Step 4: User reports visual gate result.** - -Implementation agent records the user's confirmation. - ---- - -### Task 16: Perf gate (USER REQUIRED) - -- [ ] **Step 1: Launch with `[WB-DIAG]` enabled in Release config.** - -```powershell -$env:ACDREAM_WB_DIAG = "1" -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -$env:ACDREAM_TEST_HOST = "127.0.0.1" -$env:ACDREAM_TEST_PORT = "9000" -$env:ACDREAM_TEST_USER = "testaccount" -$env:ACDREAM_TEST_PASS = "testpassword" -dotnet run --project src/AcDream.App/AcDream.App.csproj -c Release 2>&1 | Tee-Object -FilePath perf-tier1-after.log -``` - -(Release build β€” perf measurements should match what users see, not DEBUG with cross-check overhead.) - -- [ ] **Step 2: User stands at Holtburg center for β‰₯ 30 seconds at horizon-safe preset.** - -Defaults: NEAR_RADIUS=4, FAR_RADIUS=12, MSAA=0, A2C=0, ANISOTROPIC=4, MAX_COMPLETIONS=2. - -- [ ] **Step 3: Capture `[WB-DIAG]` output from the log.** - -```powershell -Select-String -Path perf-tier1-after.log -Pattern "\[WB-DIAG\]" | Select-Object -Last 5 -``` - -Expected output format (from existing dispatcher): -``` -[WB-DIAG] entSeen=… entDrawn=… meshMissing=0 drawsIssued=… instances=… groups=… cpu_us=m/p95 gpu_us=…m/…p95 -``` - -- [ ] **Step 4: Verify perf gate.** - -Check `cpu_us=m/p95`: -- `MEDIAN ≀ 2000` (≀ 2.0 ms β€” spec budget). -- `P95 ≀ 2500` (≀ 2.5 ms). -- No `BUDGET_OVER` flag. - -Compare against the pre-Tier-1 baseline (~3500 / ~4000 from the post-A.5 state). Expected: ~50% reduction in median. - -- [ ] **Step 5: Record results.** - -If perf gate passes, proceed to Phase 6 (ship). Document the actual numbers in the closing commit message. - -If perf gate FAILS (median > 2.0 ms), this is a signal that: -- Cache hit rate is lower than expected (animated entities dominate visible set). -- OR per-frame matrix mults still dominate (consider Q3 option M revisit). -- OR a cache invalidation is firing too aggressively (visible thrashing). - -Diagnose with `cache.Count` over time + the existing `entSeen` / `entDrawn` counters. Do NOT ship without hitting the gate; either fix or escalate per spec Β§11. - ---- - -## Phase 6: Ship (Task 17) - -### Task 17: Update ISSUES, CLAUDE.md, memory; final commit; merge - -**Files:** -- Modify: `docs/ISSUES.md` -- Modify: `CLAUDE.md` -- Modify: `~/.claude/projects/.../memory/project_phase_a5_state.md` (or new memory entry if a new gotcha surfaced) - -- [ ] **Step 1: Move issue #53 to "Recently closed" in `docs/ISSUES.md`.** - -Find the `## #53` block under "Active issues". Move it to "Recently closed" with the closing commit SHA. Add a one-line resolution summary citing the audit + spec + perf result. - -- [ ] **Step 2: Update `CLAUDE.md` "Currently in flight".** - -Find the line: - -``` -**Currently in flight: Post-A.5 polish β€” Tier 1 retry (only remaining priority).** -``` - -Replace with the post-A.5-complete state. Update the recently-shipped narrative to mention #53. - -- [ ] **Step 3: Update memory if new gotchas surfaced.** - -If implementation surfaced any gotchas (e.g. unexpected animated/static transitions, an LB invalidation edge case, etc.) that other agents would benefit from, add a memory entry under `~/.claude/projects/C--Users-erikn-source-repos-acdream/memory/` and add a one-line link in `MEMORY.md`. - -If no new gotchas surfaced, add a one-line note to `project_phase_a5_state.md` documenting the Tier 1 closure + final perf number. - -- [ ] **Step 4: Final commit.** - -```bash -git add docs/ISSUES.md CLAUDE.md -# also memory if updated -git commit -m "ship(post-A.5 #53): Tier 1 entity-classification cache β€” closes ISSUE #53 - -Static-only cache + DEBUG cross-check + invalidation hooks lands per spec -docs/superpowers/specs/2026-05-10-issue-53-tier1-cache-design.md. - -Perf gate: entity dispatcher cpu_us median m / p95 at -horizon-safe preset (radius=4/12) on AMD Radeon RX 9070 XT @ 1440p. -Spec target was ≀2000m/≀2500p95. Baseline was ~3500m/~4000p95. - -Visual gate: NPC animates, lifestone renders, buildings at correct -positions β€” confirmed by user 2026-05-10. - -Closes the post-A.5 polish phase. Issues #52, #54, #53 all closed. - -Co-Authored-By: Claude Opus 4.7 (1M context) " -``` - -- [ ] **Step 5: Merge to main.** - -The user merges the worktree branch via the same pattern as the prior session: - -```bash -# from main: -git checkout main -git merge claude/friendly-varahamihira-7b8664 --no-ff -m "Merge branch 'claude/friendly-varahamihira-7b8664' β€” Tier 1 entity-classification cache (closes #53)" -git push origin main -``` - -(Implementation agent does not push without explicit user authorization. The merge step is included for the user's reference.) - ---- - -## Self-Review checklist (run after writing the plan β€” completed inline) - -- [x] **Spec coverage.** Every section of the spec maps to a task: - - Spec Β§1 (problem) β†’ motivation, no task. - - Spec Β§3 Q1 (DEBUG cross-check) β†’ Task 13. - - Spec Β§3 Q2 (separate class) β†’ Tasks 2-5. - - Spec Β§3 Q3 (rest pose) β†’ Task 8 (RestPose param) + Task 10 (cache-hit fast path). - - Spec Β§3 Q4 (Setup pre-flatten) β†’ Task 8 (passes the right product) + Task 3 test #14. - - Spec Β§3 Q5 (thorough tests) β†’ Tasks 2-5, 10, 11, 13 (12 cache + 2 integration + 2 DEBUG). - - Spec Β§5.3 invalidation wiring β†’ Task 11 (per-entity), Task 12 (per-LB W3b). - - Spec Β§6.1-6.5 component contracts β†’ Tasks 2-13. - - Spec Β§7 test plan β†’ Tasks 2, 3, 4, 5, 10, 11, 13. - - Spec Β§8 sequencing β†’ matches Tasks 1-17. - - Spec Β§9 acceptance criteria β†’ Tasks 14-17. - - Spec Β§11 open implementation choices: W3b chosen (Task 12); GroupKey internal-at-namespace (Task 1); ResolveLandblockHint via walk plumbing (Task 6 + 9); _populateScratch field (Task 9). - -- [x] **Placeholder scan.** Searched plan for "TBD", "TODO", "implement later", etc. No matches outside intentional context (e.g. the `` perf-number placeholders in the final commit message β€” to be filled by the implementer with measured values). - -- [x] **Type consistency.** `CachedBatch`, `EntityCacheEntry`, `EntityClassificationCache` names + signatures match across Tasks 2-13. `GroupKey` is `internal` at namespace scope from Task 1 onward. Tuple shape `(WorldEntity Entity, int MeshRefIndex, uint LandblockId)` consistent in Tasks 6, 9, 10. - ---- - -## Execution - -This plan is ready for execution. Two options: - -**1. Subagent-Driven (recommended)** β€” fresh subagent per task; main session reviews each task before dispatching the next; fast iteration. - -**2. Inline Execution** β€” execute tasks in this session via `superpowers:executing-plans`; batch execution with checkpoints between phases. - -Both options preserve the TDD discipline (test before implementation in every step). Visual + perf gates (Tasks 15-16) require the user's eyes regardless of execution model. diff --git a/docs/superpowers/plans/2026-05-11-phase-n6-slice1.md b/docs/superpowers/plans/2026-05-11-phase-n6-slice1.md deleted file mode 100644 index 0270b2f..0000000 --- a/docs/superpowers/plans/2026-05-11-phase-n6-slice1.md +++ /dev/null @@ -1,912 +0,0 @@ -# Phase N.6 slice 1 Implementation Plan - -> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. - -**Goal:** Fix the broken `gpu_us` diagnostic in `WbDrawDispatcher` (vendor-neutral OpenGL query ring) and produce one authoritative perf baseline document at Holtburg radius=12 so the next-phase decision (slice 2 vs C.1.5 vs Tier 2) is grounded in real numbers. - -**Architecture:** Two commits. Commit 1 changes only `WbDrawDispatcher.cs` β€” replaces the two `uint` GL query handles with ring-of-3 arrays and moves the result read to *before* the next frame overwrites the slot (read frame N-3's queries, then overwrite). Commit 2 adds an env-gated surface-format histogram dump in `TextureCache.cs`, captures the actual measurement, writes the baseline doc, and amends the roadmap entry. No new automated tests β€” the GPU-timing fix has no observable behavior in tests, and the dump path is env-gated diagnostic only; verification is manual launch-and-look. - -**Tech Stack:** C# / .NET 10, Silk.NET (OpenGL 4.3+), `dotnet build` / `dotnet test` from PowerShell, live ACE on `127.0.0.1:9000` for in-world verification. - -**Spec:** [docs/superpowers/specs/2026-05-11-phase-n6-slice1-design.md](../specs/2026-05-11-phase-n6-slice1-design.md) (committed at `05d590c`). - ---- - -## File Structure - -| File | Action | Responsibility | -|---|---|---| -| [`src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs`](../../../src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs) | Modify | Replace 2 `uint` query handles with ring-of-3 arrays; move query result read to before next-frame overwrite. | -| [`src/AcDream.App/Rendering/TextureCache.cs`](../../../src/AcDream.App/Rendering/TextureCache.cs) | Modify | Add upload-time dimension/format tracking + env-gated `TickSurfaceHistogramDumpIfEnabled()` method that fires once at frame 600. | -| [`src/AcDream.App/Rendering/GameWindow.cs`](../../../src/AcDream.App/Rendering/GameWindow.cs) | Modify | Call `_textureCache.TickSurfaceHistogramDumpIfEnabled()` once per frame in `OnRender`. | -| `docs/plans/2026-05-11-phase-n6-perf-baseline.md` | Create | Baseline measurement doc: setup, numbers at radii 4/8/12 (standstill + walking), surface histogram summary, conclusion paragraph recommending next phase. | -| [`docs/plans/2026-04-11-roadmap.md`](../../plans/2026-04-11-roadmap.md) lines 690-705 | Modify | Amend N.6 entry to reflect the slice 1 / slice 2 split. | - ---- - -## Task 1: GPU query ring buffering (commit 1) - -**Files:** -- Modify: `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` - -The five edit zones are well-isolated by exact strings. Apply them in order β€” do NOT reorder; the build won't fail mid-way but the resulting code is easier to review if applied as documented. - -- [ ] **Step 1.1: Replace the field declarations (~line 155)** - -Use Edit to replace the existing field block: - -**old_string:** -```csharp - private uint _gpuQueryOpaque; - private uint _gpuQueryTransparent; - private readonly long[] _gpuSamples = new long[256]; // microseconds - private int _gpuSampleCursor; - private bool _gpuQueriesInitialized; -``` - -**new_string:** -```csharp - // GPU timing uses a ring of 3 query-pair slots so the read of frame N-3's - // result lands when the GPU has finished (~50ms after issue on a typical - // 60fps frame). Ring of 3 is the vendor-neutral choice: NVIDIA drivers with - // triple-buffering+vsync can queue ~3 frames ahead, AMD typically 1-2, - // Intel iGPUs vary. ResultAvailable is the safety guard if the GPU is - // still working when we try to read. - private const int GpuQueryRingDepth = 3; - private readonly uint[] _gpuQueryOpaque = new uint[GpuQueryRingDepth]; - private readonly uint[] _gpuQueryTransparent = new uint[GpuQueryRingDepth]; - private int _gpuQueryFrameIndex; - private readonly long[] _gpuSamples = new long[256]; // microseconds - private int _gpuSampleCursor; - private bool _gpuQueriesInitialized; -``` - -- [ ] **Step 1.2: Replace the init block (~line 347)** - -**old_string:** -```csharp - if (diag && !_gpuQueriesInitialized) - { - _gpuQueryOpaque = _gl.GenQuery(); - _gpuQueryTransparent = _gl.GenQuery(); - _gpuQueriesInitialized = true; - } -``` - -**new_string:** -```csharp - if (diag && !_gpuQueriesInitialized) - { - for (int i = 0; i < GpuQueryRingDepth; i++) - { - _gpuQueryOpaque[i] = _gl.GenQuery(); - _gpuQueryTransparent[i] = _gl.GenQuery(); - } - _gpuQueriesInitialized = true; - } -``` - -- [ ] **Step 1.3: Insert the read-before-overwrite block + compute slot just before the opaque query begin (~line 774)** - -This step replaces the existing single-line `BeginQuery` for opaque with a block that first computes the slot, reads the slot's frame N-3 result (gated on having completed one ring), then issues the new query into the same slot. - -**old_string:** -```csharp - _gl.BindBuffer(BufferTargetARB.DrawIndirectBuffer, _indirectBuffer); - if (diag && _gpuQueriesInitialized) _gl.BeginQuery(QueryTarget.TimeElapsed, _gpuQueryOpaque); -``` - -**new_string:** -```csharp - _gl.BindBuffer(BufferTargetARB.DrawIndirectBuffer, _indirectBuffer); - - // GPU timing: compute this frame's ring slot. We read frame N-3's - // result (the oldest data in the ring) before overwriting it with - // frame N's queries. See spec Β§3 Q1/Q2 + Β§4 in - // docs/superpowers/specs/2026-05-11-phase-n6-slice1-design.md. - int gpuQuerySlot = _gpuQueryFrameIndex % GpuQueryRingDepth; - if (_gpuQueriesInitialized && _gpuQueryFrameIndex >= GpuQueryRingDepth) - { - _gl.GetQueryObject(_gpuQueryOpaque[gpuQuerySlot], QueryObjectParameterName.ResultAvailable, out int avail); - if (avail != 0) - { - _gl.GetQueryObject(_gpuQueryOpaque[gpuQuerySlot], QueryObjectParameterName.Result, out ulong opaqueNs); - _gl.GetQueryObject(_gpuQueryTransparent[gpuQuerySlot], QueryObjectParameterName.Result, out ulong transNs); - long gpuUs = (long)((opaqueNs + transNs) / 1000UL); - _gpuSamples[_gpuSampleCursor] = gpuUs; - _gpuSampleCursor = (_gpuSampleCursor + 1) % _gpuSamples.Length; - } - // If avail==0 the sample is dropped silently. MedianMicros - // computes over the non-zero subset, so dropped samples don't - // poison the median. - } - - if (diag && _gpuQueriesInitialized) _gl.BeginQuery(QueryTarget.TimeElapsed, _gpuQueryOpaque[gpuQuerySlot]); -``` - -- [ ] **Step 1.4: Update the transparent query begin to use the same slot (~line 823)** - -**old_string:** -```csharp - if (diag && _gpuQueriesInitialized) _gl.BeginQuery(QueryTarget.TimeElapsed, _gpuQueryTransparent); -``` - -**new_string:** -```csharp - if (diag && _gpuQueriesInitialized) _gl.BeginQuery(QueryTarget.TimeElapsed, _gpuQueryTransparent[gpuQuerySlot]); -``` - -- [ ] **Step 1.5: Replace the buggy in-frame read block + increment frame counter (~line 849)** - -**old_string:** -```csharp - // Read GPU samples non-blocking; the result for the previous frame's - // queries should be ready by now. If not, drop the sample (don't stall - // the CPU waiting for the GPU). - if (_gpuQueriesInitialized) - { - _gl.GetQueryObject(_gpuQueryOpaque, QueryObjectParameterName.ResultAvailable, out int avail); - if (avail != 0) - { - _gl.GetQueryObject(_gpuQueryOpaque, QueryObjectParameterName.Result, out ulong opaqueNs); - _gl.GetQueryObject(_gpuQueryTransparent, QueryObjectParameterName.Result, out ulong transNs); - long gpuUs = (long)((opaqueNs + transNs) / 1000UL); - _gpuSamples[_gpuSampleCursor] = gpuUs; - _gpuSampleCursor = (_gpuSampleCursor + 1) % _gpuSamples.Length; - } - } - - _drawsIssued += _opaqueDrawCount + _transparentDrawCount; -``` - -**new_string:** -```csharp - // GPU sample read happens BEFORE issuing the next frame's queries - // (see step 1.3 above). Increment the frame counter here so the - // next call computes a fresh slot. - if (_gpuQueriesInitialized) _gpuQueryFrameIndex++; - - _drawsIssued += _opaqueDrawCount + _transparentDrawCount; -``` - -- [ ] **Step 1.6: Update Dispose to delete the full ring (~line 1140)** - -**old_string:** -```csharp - if (_gpuQueriesInitialized) - { - _gl.DeleteQuery(_gpuQueryOpaque); - _gl.DeleteQuery(_gpuQueryTransparent); - } -``` - -**new_string:** -```csharp - if (_gpuQueriesInitialized) - { - for (int i = 0; i < GpuQueryRingDepth; i++) - { - _gl.DeleteQuery(_gpuQueryOpaque[i]); - _gl.DeleteQuery(_gpuQueryTransparent[i]); - } - } -``` - -- [ ] **Step 1.7: Build** - -Run from the worktree root: - -```powershell -dotnet build -``` - -Expected: build succeeds with no new warnings or errors. If the build fails, the most likely cause is a missed string in one of the steps above β€” re-grep `_gpuQueryOpaque` and `_gpuQueryTransparent` in `WbDrawDispatcher.cs` and confirm every reference uses the array-indexed form `[gpuQuerySlot]` or `[i]`. - -- [ ] **Step 1.8: Run the test suite** - -```powershell -dotnet test --no-build -``` - -Expected: same pass/fail baseline as before the change (~1688 passing, ~8 pre-existing physics/input failures unchanged). No new failures. - -- [ ] **Step 1.9: Manual verification β€” launch live and confirm `gpu_us` reports non-zero** - -```powershell -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -$env:ACDREAM_TEST_HOST = "127.0.0.1" -$env:ACDREAM_TEST_PORT = "9000" -$env:ACDREAM_TEST_USER = "testaccount" -$env:ACDREAM_TEST_PASS = "testpassword" -$env:ACDREAM_WB_DIAG = "1" -dotnet run --project src\AcDream.App\AcDream.App.csproj --no-build -c Debug 2>&1 | Tee-Object -FilePath "task1-verify.log" -``` - -In-world: walk Holtburg for ~30 seconds. Close the window when done. - -Verification check on `task1-verify.log`: - -```powershell -Select-String -Path task1-verify.log -Pattern "\[WB-DIAG\]" | Select-Object -Last 5 -``` - -Expected output: at least one `[WB-DIAG]` line where `gpu_us=Xm/Yp95` has X > 0 (typically tens to low-hundreds of microseconds at radius=4-12 on a modern GPU). If `gpu_us=0m/0p95` persists for the entire run, the fix didn't take β€” check whether the build actually rebuilt (try `dotnet build -c Debug` then re-launch). - -Also confirm: no visible regression in the client. Entities render, animations play, sky cycles. Close the client cleanly. - -- [ ] **Step 1.10: Commit** - -```powershell -git add src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs -git commit -m @' -feat(perf): Phase N.6 slice 1 β€” fix gpu_us double-buffering in WbDrawDispatcher - -The dispatcher's GPU TimeElapsed queries were polled in the same frame -as the indirect draw, so glGetQueryObject(ResultAvailable) always -returned 0 and gpu_us in [WB-DIAG] was stuck at 0m/0p95. - -Replace the 2 single-handle queries with ring-of-3 arrays and move the -result read to BEFORE issuing the next frame's queries into the same -slot β€” at frame N we read slot N%3 which holds frame N-3's queries -(oldest in the ring, ~50ms old at 60fps and definitely done across all -desktop GL drivers). Vendor-neutral: AMD/NVIDIA/Intel desktop GL all -work without driver-specific code. - -No new tests β€” the change is purely a diagnostic readout fix, no -observable behavior in the rendering path. Manual verification: -[WB-DIAG] now reports non-zero gpu_us at Holtburg radius=12. - -Spec: docs/superpowers/specs/2026-05-11-phase-n6-slice1-design.md (Β§4). - -Co-Authored-By: Claude Opus 4.7 (1M context) -'@ -git status -``` - -Expected: clean working tree after commit. Note the new commit SHA β€” needed for the baseline doc's "measured against" reference. - ---- - -## Task 2: Surface-format histogram dump path (part of commit 2 setup) - -**Files:** -- Modify: `src/AcDream.App/Rendering/TextureCache.cs` -- Modify: `src/AcDream.App/Rendering/GameWindow.cs` - -This task adds the env-gated one-shot dump infrastructure. It does NOT commit β€” the commit happens in Task 4 after the baseline document is also ready. - -- [ ] **Step 2.1: Add upload-time metadata tracking in `TextureCache.cs`** - -Add a new private dictionary that records `(width, height, formatLabel)` keyed by GL texture name. This lets `DumpSurfaceHistogram` emit dimension/format data without re-querying GL. - -Use Edit to insert the field right after the existing bindless cache fields (~line 41, just after `_bindlessByPalette`): - -**old_string:** -```csharp - private readonly Dictionary<(uint surfaceId, uint origTexOverride, ulong paletteHash), (uint Name, ulong Handle)> _bindlessByPalette = new(); - - public TextureCache(GL gl, DatCollection dats, Wb.BindlessSupport? bindless = null) -``` - -**new_string:** -```csharp - private readonly Dictionary<(uint surfaceId, uint origTexOverride, ulong paletteHash), (uint Name, ulong Handle)> _bindlessByPalette = new(); - - // Phase N.6 slice 1 (2026-05-11): per-upload metadata for the - // ACDREAM_DUMP_SURFACES=1 histogram dump path. Populated at upload - // time so the dump method doesn't have to query GL state. Keyed by - // GL texture name (same key used in cache value tuples). Format - // label is "RGBA8_DECODED" for the post-decode upload (all uploads - // currently land as RGBA8 regardless of source format). - private readonly Dictionary _uploadMetadata = new(); - - // Frame counter for the one-shot ACDREAM_DUMP_SURFACES=1 trigger. - // Increments per Tick call; fires the dump once at frame index 600 - // and never again for the session. See spec Β§5. - private int _dumpFrameCounter; - private bool _surfaceHistogramAlreadyDumped; - - public TextureCache(GL gl, DatCollection dats, Wb.BindlessSupport? bindless = null) -``` - -- [ ] **Step 2.2: Find the `UploadRgba8AsLayer1Array` method and record metadata there** - -Locate the method using Grep: - -``` -pattern: "UploadRgba8AsLayer1Array" -path: src/AcDream.App/Rendering/TextureCache.cs -output_mode: content --n: true -``` - -Read the method body (typically ~30-50 lines) to find the exact `return name;` line. The decoded texture has `decoded.Width`, `decoded.Height`, and `decoded.Rgba8` available. - -For each `return name;` in `UploadRgba8AsLayer1Array(DecodedTexture decoded)`, insert this line immediately before it: - -```csharp - _uploadMetadata[name] = (decoded.Width, decoded.Height, "RGBA8_DECODED"); -``` - -If the method has only one `return name;` near its end, that's a single Edit. Use the surrounding 2-3 lines of context in `old_string` to make the Edit unique. - -- [ ] **Step 2.3: Also record metadata in the legacy `UploadRgba8` (non-bindless) path** - -Locate the method: - -``` -pattern: "private uint UploadRgba8\b" -path: src/AcDream.App/Rendering/TextureCache.cs -output_mode: content --n: true -``` - -Apply the same `_uploadMetadata[name] = (decoded.Width, decoded.Height, "RGBA8_DECODED");` insertion before each `return name;` in `UploadRgba8(DecodedTexture decoded)`. This ensures the dump captures both legacy and modern uploads. - -- [ ] **Step 2.4: Add the `TickSurfaceHistogramDumpIfEnabled` public method to `TextureCache.cs`** - -Locate `HashPaletteOverride` using Grep: - -``` -pattern: "internal static ulong HashPaletteOverride" -path: src/AcDream.App/Rendering/TextureCache.cs -output_mode: content --n: true --A: 20 -``` - -Identify its closing brace. Use Edit with surrounding context to insert the new methods immediately after. - -**old_string:** (the last few lines of `HashPaletteOverride`): -```csharp - foreach (var sp in p.SubPalettes) - { - h = (h ^ sp.SubPaletteId) * prime; - h = (h ^ sp.Offset) * prime; - h = (h ^ sp.Length) * prime; - } - return h; - } -``` - -**new_string:** -```csharp - foreach (var sp in p.SubPalettes) - { - h = (h ^ sp.SubPaletteId) * prime; - h = (h ^ sp.Offset) * prime; - h = (h ^ sp.Length) * prime; - } - return h; - } - - /// - /// Phase N.6 slice 1: one-shot surface-format histogram dump for the - /// atlas-opportunity audit. Activated by ACDREAM_DUMP_SURFACES=1; fires - /// once at frame 600 of the session (~10s at 60fps, ~3s at 200fps β€” - /// both well past streaming settle at radius≀12). Output goes to - /// %LOCALAPPDATA%\acdream\n6-surfaces.txt. Zero cost when off. - /// See spec Β§5 in docs/superpowers/specs/2026-05-11-phase-n6-slice1-design.md. - /// - public void TickSurfaceHistogramDumpIfEnabled() - { - if (_surfaceHistogramAlreadyDumped) return; - if (!string.Equals(Environment.GetEnvironmentVariable("ACDREAM_DUMP_SURFACES"), "1", StringComparison.Ordinal)) return; - _dumpFrameCounter++; - if (_dumpFrameCounter < 600) return; - - DumpSurfaceHistogram(); - _surfaceHistogramAlreadyDumped = true; - } - - private void DumpSurfaceHistogram() - { - var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); - var outDir = System.IO.Path.Combine(localAppData, "acdream"); - System.IO.Directory.CreateDirectory(outDir); - var outPath = System.IO.Path.Combine(outDir, "n6-surfaces.txt"); - - var sb = new System.Text.StringBuilder(); - sb.AppendLine($"# acdream surface-format histogram β€” generated {DateTime.UtcNow:yyyy-MM-ddTHH:mm:ssZ}"); - sb.AppendLine("# Per-entry: surfaceId(hex), width, height, format, byteCount"); - sb.AppendLine(); - - // Walk every cached entry across the 6 caches, dedupe by GL name. - var seen = new HashSet(); - long totalBytes = 0; - var bucketsByDim = new Dictionary<(int W, int H), int>(); - var bucketsByFormat = new Dictionary(); - var bucketsByTriple = new Dictionary<(int W, int H, string F), int>(); - - void Emit(uint surfaceId, uint name) - { - if (!seen.Add(name)) return; - if (!_uploadMetadata.TryGetValue(name, out var meta)) return; - int bytes = meta.Width * meta.Height * 4; - totalBytes += bytes; - sb.AppendLine($"0x{surfaceId:X8}, {meta.Width}, {meta.Height}, {meta.Format}, {bytes}"); - - var dimKey = (meta.Width, meta.Height); - bucketsByDim[dimKey] = bucketsByDim.GetValueOrDefault(dimKey) + 1; - bucketsByFormat[meta.Format] = bucketsByFormat.GetValueOrDefault(meta.Format) + 1; - var tripleKey = (meta.Width, meta.Height, meta.Format); - bucketsByTriple[tripleKey] = bucketsByTriple.GetValueOrDefault(tripleKey) + 1; - } - - foreach (var kv in _handlesBySurfaceId) Emit(kv.Key, kv.Value); - foreach (var kv in _handlesByOverridden) Emit(kv.Key.surfaceId, kv.Value); - foreach (var kv in _handlesByPalette) Emit(kv.Key.surfaceId, kv.Value); - foreach (var kv in _bindlessBySurfaceId) Emit(kv.Key, kv.Value.Name); - foreach (var kv in _bindlessByOverridden) Emit(kv.Key.surfaceId, kv.Value.Name); - foreach (var kv in _bindlessByPalette) Emit(kv.Key.surfaceId, kv.Value.Name); - - sb.AppendLine(); - sb.AppendLine("# Rollups"); - sb.AppendLine($"# Total unique GL textures: {seen.Count}"); - sb.AppendLine($"# Total bytes (sum of W*H*4): {totalBytes}"); - - sb.AppendLine("# Top 10 (W,H) dimension buckets:"); - foreach (var kv in bucketsByDim.OrderByDescending(kv => kv.Value).Take(10)) - sb.AppendLine($"# {kv.Key.W}x{kv.Key.H}: {kv.Value}"); - - sb.AppendLine("# Format buckets:"); - foreach (var kv in bucketsByFormat.OrderByDescending(kv => kv.Value)) - sb.AppendLine($"# {kv.Key}: {kv.Value}"); - - sb.AppendLine("# Top 10 (W,H,format) triples β€” atlas-opportunity input:"); - foreach (var kv in bucketsByTriple.OrderByDescending(kv => kv.Value).Take(10)) - sb.AppendLine($"# {kv.Key.W}x{kv.Key.H} {kv.Key.F}: {kv.Value}"); - - System.IO.File.WriteAllText(outPath, sb.ToString()); - Console.WriteLine($"[N6-DUMP] Surface histogram written to {outPath} ({seen.Count} textures, {totalBytes} bytes)"); - } -``` - -- [ ] **Step 2.5: Confirm `using System.Linq;` is present in `TextureCache.cs`** - -Read the file's `using` section (top of file). If `using System.Linq;` is NOT present, add it. The `OrderByDescending` and `Take` calls in `DumpSurfaceHistogram` need it. - -Pattern: -``` -pattern: "^using System\.Linq" -path: src/AcDream.App/Rendering/TextureCache.cs -output_mode: count -``` - -If count is 0, add `using System.Linq;` in alphabetical order with the other usings at the top of the file. - -- [ ] **Step 2.6: Add the per-frame call site in `GameWindow.cs`** - -Find a stable insertion point near the top of `OnRender` (starts at line 6288). Use Grep: - -``` -pattern: "_gl!\.Clear\(" -path: src/AcDream.App/Rendering/GameWindow.cs -output_mode: content --n: true --A: 3 -``` - -This finds the `Clear` call(s) in or near `OnRender`. The first one after line 6288 is where you want to insert. Read 5 lines of context around it, then Edit to insert the dump tick on the line immediately after the `Clear` call returns: - -The insertion (one Edit): - -**old_string:** (find the `Clear` call in `OnRender` and capture 1-2 lines of its context β€” varies; common pattern is `_gl!.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);` followed by the next line of `OnRender` work). - -**new_string:** the same `Clear` call followed by: -```csharp - - // Phase N.6 slice 1: one-shot surface-format histogram dump under - // ACDREAM_DUMP_SURFACES=1. Zero cost when off. - _textureCache?.TickSurfaceHistogramDumpIfEnabled(); -``` - -If `OnRender` has multiple `Clear` calls, place the tick after the first one inside the method body. The call must run exactly once per frame, before any rendering work β€” placing it right after `Clear` accomplishes both. - -- [ ] **Step 2.7: Build** - -```powershell -dotnet build -``` - -Expected: build succeeds with no new warnings. If a "name 'OrderByDescending' does not exist in current context" error appears, Step 2.5 was missed β€” add the `using System.Linq;` and rebuild. - -- [ ] **Step 2.8: Run the test suite** - -```powershell -dotnet test --no-build -``` - -Expected: same pass/fail baseline (~1688 passing, ~8 pre-existing failures). No new failures. - -- [ ] **Step 2.9: Manual verification β€” confirm the dump file appears** - -Launch with the dump env var on: - -```powershell -$env:ACDREAM_DUMP_SURFACES = "1" -$env:ACDREAM_WB_DIAG = "1" -# Other env vars same as Task 1 Step 1.9 -dotnet run --project src\AcDream.App\AcDream.App.csproj --no-build -c Debug 2>&1 | Tee-Object -FilePath "task2-verify.log" -``` - -Wait ~15 seconds after the window appears, then close it. Check the file: - -```powershell -Get-Content "$env:LOCALAPPDATA\acdream\n6-surfaces.txt" | Select-Object -First 30 -``` - -Expected: a non-empty file with the header, per-entry rows, and rollup sections. Also confirm one `[N6-DUMP] Surface histogram written to ...` line in `task2-verify.log` (just before window close). - -If the file is empty or missing: -- Check the launch log for the `[N6-DUMP]` line. -- If it's not there, `_dumpFrameCounter` didn't reach 600 β€” the user closed too early. Re-run and wait longer. -- If it's there but the file lookup fails, the path output in the log should show what was actually written; investigate that path. - -**Do not commit yet.** Continue to Task 3. - ---- - -## Task 3: Capture baseline measurements - -**Files:** -- Create: `docs/plans/2026-05-11-phase-n6-perf-baseline.md` (final content lands in Task 4 β€” this task just collects the numbers). - -This is the manual measurement task. Each step launches the client, runs a specific scenario, and captures the diagnostic output. Save each log separately for the final write-up. Total expected time: ~30-45 min. - -Setup once per session: -```powershell -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -$env:ACDREAM_TEST_HOST = "127.0.0.1" -$env:ACDREAM_TEST_PORT = "9000" -$env:ACDREAM_TEST_USER = "testaccount" -$env:ACDREAM_TEST_PASS = "testpassword" -$env:ACDREAM_WB_DIAG = "1" -``` - -For each measurement run, set `ACDREAM_STREAM_RADIUS` before launch. Use the `QualityPreset=High` default (no overrides). All runs at Holtburg with `+Acdream` at clear midday (cycle weather with F10 β†’ Clear, time with F7 β†’ Noon). - -Per run, after ~30 seconds at the target condition, close the window and grep the log for the last 3 `[WB-DIAG]` lines β€” those have the steady-state numbers. - -- [ ] **Step 3.1: Capture radius=4 standstill** - -```powershell -$env:ACDREAM_STREAM_RADIUS = "4" -dotnet run --project src\AcDream.App\AcDream.App.csproj --no-build -c Debug 2>&1 | Tee-Object -FilePath "baseline-r4-stand.log" -``` - -In-world: enter world, do not move, hold position for 30 seconds. Close. - -```powershell -Select-String -Path baseline-r4-stand.log -Pattern "\[WB-DIAG\]" | Select-Object -Last 3 -``` - -Record from the median of the last 3 lines: `cpu_us`, `gpu_us`, `entSeen`, `entDrawn`, `groups`. Also note the window-title FPS shown during the test. - -- [ ] **Step 3.2: Capture radius=4 walking** - -```powershell -$env:ACDREAM_STREAM_RADIUS = "4" -dotnet run --project src\AcDream.App\AcDream.App.csproj --no-build -c Debug 2>&1 | Tee-Object -FilePath "baseline-r4-walk.log" -``` - -In-world: enter world, Tab to player mode, walk Nβ†’Eβ†’Sβ†’W across one landblock over ~30 seconds. Close. - -Capture same numbers as 3.1. - -- [ ] **Step 3.3: Capture radius=8 standstill** - -```powershell -$env:ACDREAM_STREAM_RADIUS = "8" -dotnet run --project src\AcDream.App\AcDream.App.csproj --no-build -c Debug 2>&1 | Tee-Object -FilePath "baseline-r8-stand.log" -``` - -Same procedure as 3.1. Wait ~40 seconds before recording (streaming takes longer to settle). - -- [ ] **Step 3.4: Capture radius=8 walking** - -```powershell -$env:ACDREAM_STREAM_RADIUS = "8" -dotnet run --project src\AcDream.App\AcDream.App.csproj --no-build -c Debug 2>&1 | Tee-Object -FilePath "baseline-r8-walk.log" -``` - -Same procedure as 3.2. - -- [ ] **Step 3.5: Capture radius=12 standstill** - -```powershell -$env:ACDREAM_STREAM_RADIUS = "12" -dotnet run --project src\AcDream.App\AcDream.App.csproj --no-build -c Debug 2>&1 | Tee-Object -FilePath "baseline-r12-stand.log" -``` - -Same procedure as 3.1. Wait ~60 seconds before recording. This is the headline measurement β€” pay attention to whether `gpu_us` p95 is well below 16.6 ms (60 fps target) or pushing it. - -- [ ] **Step 3.6: Capture radius=12 walking** - -```powershell -$env:ACDREAM_STREAM_RADIUS = "12" -dotnet run --project src\AcDream.App\AcDream.App.csproj --no-build -c Debug 2>&1 | Tee-Object -FilePath "baseline-r12-walk.log" -``` - -Same procedure as 3.2 (walking across one landblock, ~30 seconds of motion within the 60s+ window). - -- [ ] **Step 3.7: Capture the surface histogram** - -```powershell -$env:ACDREAM_STREAM_RADIUS = "12" -$env:ACDREAM_DUMP_SURFACES = "1" -dotnet run --project src\AcDream.App\AcDream.App.csproj --no-build -c Debug 2>&1 | Tee-Object -FilePath "baseline-surfaces.log" -``` - -In-world: enter world at Holtburg, do nothing for ~30 seconds (let the dump fire at frame 600). Close. Copy the file: - -```powershell -Copy-Item "$env:LOCALAPPDATA\acdream\n6-surfaces.txt" -Destination "baseline-surfaces.txt" -``` - -Inspect: -```powershell -Get-Content baseline-surfaces.txt | Select-Object -Last 40 -``` - -Record the rollup section (total textures, total bytes, top 10 dimension buckets, format distribution, top 10 (W,H,format) triples). - -- [ ] **Step 3.8: Clean up the env vars and the local app data dump** - -```powershell -Remove-Item Env:\ACDREAM_DUMP_SURFACES -ErrorAction SilentlyContinue -Remove-Item Env:\ACDREAM_STREAM_RADIUS -ErrorAction SilentlyContinue -# Optional: clean up the source file so a future re-measurement isn't confused by stale data -Remove-Item "$env:LOCALAPPDATA\acdream\n6-surfaces.txt" -ErrorAction SilentlyContinue -``` - -All log files (`baseline-r*-*.log`, `baseline-surfaces.log`, `baseline-surfaces.txt`) remain in the worktree root for Task 4. They will NOT be committed β€” they're scratch. - ---- - -## Task 4: Write baseline doc + amend roadmap + ship commit 2 - -**Files:** -- Create: `docs/plans/2026-05-11-phase-n6-perf-baseline.md` -- Modify: `docs/plans/2026-04-11-roadmap.md` lines 690-705 - -- [ ] **Step 4.1: Write the baseline document** - -Use Write to create `docs/plans/2026-05-11-phase-n6-perf-baseline.md` with this content (substitute real numbers from Task 3 captures into every `` and `` placeholder; do NOT leave any unfilled): - -```markdown -# Phase N.6 slice 1 β€” perf baseline at Holtburg - -**Created:** 2026-05-11. -**Spec:** [docs/superpowers/specs/2026-05-11-phase-n6-slice1-design.md](../superpowers/specs/2026-05-11-phase-n6-slice1-design.md) -**Measured against commit:** -**Purpose:** Capture authoritative CPU+GPU dispatch numbers so the next-phase decision (slice 2 vs C.1.5 vs Tier 2) rests on real data. - ---- - -## Β§1. Setup - -- **Hardware:** Radeon RX 9070 XT -- **Resolution:** 1440p (2560Γ—1440) -- **Quality preset:** High (default) -- **Connection:** live ACE at `127.0.0.1:9000` -- **Character:** `+Acdream` at Holtburg -- **Sky / time:** clear midday (F7 β†’ Noon, F10 β†’ Clear) -- **Build:** Debug -- **Date measured:** 2026-05-11 -- **Environment overrides:** `ACDREAM_WB_DIAG=1`, `ACDREAM_STREAM_RADIUS=` - -## Β§2. Dispatch CPU / GPU numbers - -Each cell records the median of the last 3 `[WB-DIAG]` lines from a ~30s stable window. `entSeen / entDrawn / groups` are also from those lines. FPS read from the window title. - -| Radius | Motion | cpu_us median | cpu_us p95 | gpu_us median | gpu_us p95 | FPS | entSeen | entDrawn | groups | -|---|---|---|---|---|---|---|---|---|---| -| 4 | standstill | | | | | | | | | -| 4 | walking | | | | | | | | | -| 8 | standstill | | | | | | | | | -| 8 | walking | | | | | | | | | -| 12| standstill | | | | | | | | | -| 12| walking | | | | | | | | | - -## Β§3. Surface-format histogram - -From `ACDREAM_DUMP_SURFACES=1` at radius=12, ~30s after enter-world. - -- **Total unique GL textures:** -- **Total bytes (sum of W*H*4):** -- **Top 10 (W, H) dimension buckets:** - - `x`: - - ... (paste from baseline-surfaces.txt rollup) -- **Format distribution:** - - ``: -- **Top 10 (W, H, format) triples β€” atlas-opportunity input:** - - `x `: - - ... - -**Atlas-opportunity score:** % of surfaces fall into the top-3 (W, H, format) triples. (A score >30% means atlas consolidation could meaningfully reduce sampler switches + memory overhead; <15% means scattered content and atlas is not worth the slice-2 effort.) - -## Β§4. Conclusion + next-phase recommendation - -= 14000 Β΅s: GPU-saturated, persistent-mapped buffers and compute cull help. - 3. Does the atlas score justify slice-2 atlas work? - 4. Given (1)-(3), which is the right next phase? - - CPU-bound + low atlas score: pivot to C.1.5 (visible content, perf already comfortable). - - GPU-bound + high atlas score: do N.6 slice 2 (atlas + persistent buffers). - - Either-bound + headroom + low atlas score: do C.1.5 first. - - GPU saturated + need for more headroom: escalate to Tier 2.> - -## Β§5. Raw logs - -Scratch logs from this measurement run (not committed): -- `baseline-r4-stand.log`, `baseline-r4-walk.log` -- `baseline-r8-stand.log`, `baseline-r8-walk.log` -- `baseline-r12-stand.log`, `baseline-r12-walk.log` -- `baseline-surfaces.log`, `baseline-surfaces.txt` -``` - -Fill in every `` and `` and the conclusion paragraph with the real values from Task 3. **Do NOT leave any `` placeholders.** If a measurement is missing, re-run that step from Task 3 before continuing. - -- [ ] **Step 4.2: Read the current roadmap N.6 entry** - -``` -Read offset 685, limit 25 from docs/plans/2026-04-11-roadmap.md -``` - -Confirm the bullet starts with `- **N.6 β€” Perf polish.** **Planned (post-A.5 polish takes priority).**` and ends with `Plan + spec written when work begins. **Estimate: 1-2 weeks.**`. Capture the exact text verbatim for Step 4.3's `old_string`. - -- [ ] **Step 4.3: Amend the roadmap entry** - -Use Edit. The change splits N.6 into slice 1 (shipping with this commit) and slice 2 (deferred until after C.1.5). - -**old_string:** the exact N.6 bullet copied from the Read in Step 4.2. - -**new_string:** -```markdown -- **N.6 slice 1 β€” GPU timing fix + radius=12 perf baseline.** **SHIPPED 2026-05-11.** - Fixed the gpu_us double-buffering bug in `WbDrawDispatcher` (ring-of-3 - query slots, read-before-overwrite, vendor-neutral across AMD/NVIDIA/Intel - desktop GL). Added env-gated surface-format histogram dump in `TextureCache` - for atlas-opportunity audit. Captured authoritative baseline at Holtburg - radii 4 / 8 / 12 (standstill + walking) with the now-working `gpu_us` - diagnostic. Plan + spec at `docs/superpowers/{specs,plans}/2026-05-11-phase-n6-slice1-*.md`. - Baseline numbers + next-phase recommendation at - [docs/plans/2026-05-11-phase-n6-perf-baseline.md](2026-05-11-phase-n6-perf-baseline.md). -- **N.6 slice 2 β€” Perf polish cleanup.** **Planned β€” deferred until after C.1.5 - (PES emitter wiring) per the baseline doc's recommendation.** Builds on - slice 1's measurement. Scope: retire the legacy `Texture2D`/`sampler2D` path - in `TextureCache` (currently kept for Sky + Debug + particle paths now that - Terrain has migrated); delete orphan `mesh.frag` (verify zero callers post-N.5 - amendment); decide bindless-everywhere vs legacy-island for the remaining - `sampler2D` consumers; conditionally adopt WB atlas if the slice-1 histogram - shows a real opportunity; conditionally adopt persistent-mapped buffers if - the slice-1 baseline shows `BufferSubData` as a hot spot; GPU compute culling - remains out-of-scope (that's Tier 3 of the perf-tiers roadmap, gated on - Tier 2 first). Plan + spec written when work begins. **Estimate: 1-2 weeks - once C.1.5 lands.** -``` - -- [ ] **Step 4.4: Build (sanity check β€” only docs touched, but be safe)** - -```powershell -dotnet build -``` - -Expected: build succeeds. (No code touched in Task 4; this just confirms nothing was accidentally edited in src/.) - -- [ ] **Step 4.5: Commit 2** - -```powershell -git add src/AcDream.App/Rendering/TextureCache.cs ` - src/AcDream.App/Rendering/GameWindow.cs ` - docs/plans/2026-05-11-phase-n6-perf-baseline.md ` - docs/plans/2026-04-11-roadmap.md -git commit -m @' -docs(perf): Phase N.6 slice 1 β€” radius=12 baseline + surface dump path - -Capture authoritative CPU+GPU dispatch numbers at Holtburg with the -gpu_us diagnostic now working (commit ). Three -radii (4/8/12) Γ— two motion modes (standstill/walking) + a surface-format -histogram from ACDREAM_DUMP_SURFACES=1. - -Adds env-gated one-shot dump path (TextureCache.TickSurfaceHistogramDumpIfEnabled, -called from GameWindow.OnRender) that fires once at frame 600 of the -session β€” zero cost when off, writes to %LOCALAPPDATA%\acdream\n6-surfaces.txt. - -Baseline document at docs/plans/2026-05-11-phase-n6-perf-baseline.md -closes with a recommendation paragraph for the next phase. Roadmap entry -amended to reflect the slice 1 / slice 2 split. - -Spec: docs/superpowers/specs/2026-05-11-phase-n6-slice1-design.md (Β§5, Β§6). - -Co-Authored-By: Claude Opus 4.7 (1M context) -'@ -git status -``` - -Expected: clean working tree. - -- [ ] **Step 4.6: Final sanity sweep** - -```powershell -git log -3 --oneline -``` - -Expected: two new commits from this slice (the GPU timing fix from Task 1.10, then this docs/perf commit), under the spec commit `05d590c`. - -Also confirm the scratch baseline-r*.log and baseline-surfaces.* files are still NOT in the commit (they were not staged): - -```powershell -git status -``` - -Expected: clean working tree. If the scratch logs show as untracked but uncommitted, that's fine β€” they can be deleted manually: - -```powershell -Remove-Item baseline-r*.log, baseline-surfaces.log, baseline-surfaces.txt, task1-verify.log, task2-verify.log -ErrorAction SilentlyContinue -``` - ---- - -## Acceptance check (spec Β§9) - -After Task 4 commits, walk through the spec's acceptance criteria and confirm each one. This is a paper-walk, not a re-run β€” the steps above produce the conditions. - -- [ ] **A1: `[WB-DIAG]` reports non-zero `gpu_us` at radius=12.** - Verified in Task 1.9 (initial check) and Task 3.5-3.6 (full baseline run). Confirm by re-grepping `baseline-r12-stand.log`: - ```powershell - Select-String -Path baseline-r12-stand.log -Pattern "gpu_us=[1-9]" - ``` - Should return at least one line. - -- [ ] **A2: Vendor-neutral.** No `GL_*_NV` or `GL_*_AMD` or `GL_*_INTEL` extension references in the change. Re-grep: - ```powershell - Select-String -Path src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs -Pattern "NV_|AMD_|INTEL_|GL_NV|GL_AMD|GL_INTEL" - ``` - Expected: no matches in the new code (matches elsewhere in the file from unrelated existing code don't count). - -- [ ] **A3: Baseline doc has real numbers + conclusion.** - Open `docs/plans/2026-05-11-phase-n6-perf-baseline.md` and visually confirm no ``, ``, `TBD`, or empty conclusion section. - -- [ ] **A4: Roadmap split shipped.** - ```powershell - Select-String -Path docs/plans/2026-04-11-roadmap.md -Pattern "N\.6 slice" - ``` - Expected: two matches (slice 1 + slice 2 bullets). - -- [ ] **A5: `dotnet build` green, no new warnings.** - ```powershell - dotnet build - ``` - Expected: succeeds. Note any new warnings vs the build output before the slice started. - -- [ ] **A6: `dotnet test` green at baseline (~1688 passing, ~8 pre-existing failures).** - ```powershell - dotnet test --no-build - ``` - Expected: pass count unchanged from before the slice started; failure list unchanged. - -- [ ] **A7: No visible regression.** - Confirmed during Task 1.9 and Task 3 measurements β€” the user was in-world repeatedly and didn't observe any rendering issue. If anything looked off during measurement, file it as an issue and decide whether it blocks slice 1 acceptance. - -If any acceptance criterion fails, return to the relevant task and re-do it. Do not declare slice 1 complete with failing acceptance. - ---- - -## After slice 1 lands - -The baseline document's conclusion paragraph (Β§4) determines the next phase: - -- **If conclusion recommends C.1.5:** brainstorm C.1.5 spec next, using [docs/plans/2026-04-27-phase-c1-pes-particles.md:285-295](../../plans/2026-04-27-phase-c1-pes-particles.md) as the starting scope. -- **If conclusion recommends N.6 slice 2:** brainstorm slice 2 spec next, addressing legacy `TextureCache` cleanup + atlas + persistent-mapped buffers based on the histogram data. -- **If conclusion recommends Tier 2:** consult [docs/plans/2026-05-10-perf-tiers-2-3-roadmap.md](../../plans/2026-05-10-perf-tiers-2-3-roadmap.md) and brainstorm a Tier 2 spec. - -The choice is data-driven; the recommendation paragraph is the contract. Don't re-litigate the decision once the numbers are in. diff --git a/docs/superpowers/plans/2026-05-12-phase-c1.5a-portals.md b/docs/superpowers/plans/2026-05-12-phase-c1.5a-portals.md deleted file mode 100644 index 7de4a3e..0000000 --- a/docs/superpowers/plans/2026-05-12-phase-c1.5a-portals.md +++ /dev/null @@ -1,651 +0,0 @@ -# Phase C.1.5a β€” Portal PES wiring implementation plan - -> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. - -**Goal:** Fire `Setup.DefaultScript` through the already-shipped `PhysicsScriptRunner` when a server-spawned `WorldEntity` enters the world, so portals emit their retail-faithful persistent particle effects automatically. - -**Architecture:** One new ~50-line class `EntityScriptActivator` under `src/AcDream.App/Rendering/Vfx/`. Wired into `GpuWorldState`'s `AppendLiveEntity` (calls `OnCreate`) and `RemoveEntityByServerGuid` (calls `OnRemove`), immediately after the matching `_wbEntitySpawnAdapter` calls. Activator is constructed in `GameWindow` (alongside the existing entity-spawn adapter) and passed into `GpuWorldState` as a new optional ctor parameter. - -**Tech Stack:** C# / .NET 10, xUnit, Silk.NET (existing). No new dependencies. - -**Spec:** [`docs/superpowers/specs/2026-05-12-phase-c1.5a-portals-design.md`](../specs/2026-05-12-phase-c1.5a-portals-design.md). Read it first. - ---- - -## File structure - -**Created:** - -- `src/AcDream.App/Rendering/Vfx/EntityScriptActivator.cs` β€” the new orchestrator class. -- `tests/AcDream.Core.Tests/Rendering/Vfx/EntityScriptActivatorTests.cs` β€” three xUnit tests covering OnCreate-fires, OnCreate-no-op, OnRemove-cleanup. - -**Modified:** - -- `src/AcDream.App/Streaming/GpuWorldState.cs` β€” new optional ctor parameter; two `?.` call sites added. -- `src/AcDream.App/Rendering/GameWindow.cs` β€” construct the activator alongside `_wbEntitySpawnAdapter` (~line 1614) and pass it into the `GpuWorldState` ctor (~line 1619). One field declaration added. -- `docs/plans/2026-04-11-roadmap.md` β€” append "Phase C.1.5a SHIPPED" entry on verification pass (Task 4 only). - -Each file has one clear responsibility: -- `EntityScriptActivator` β€” orchestrates DefaultScript fire-on-spawn / stop-on-despawn. Knows nothing about dats or GL. -- `GpuWorldState` β€” owns spawn lifecycle. The activator is one more `?.` collaborator alongside the existing adapter. -- `GameWindow` β€” wiring root. Constructs the resolver lambda where `_dats` is in scope; everything else is plumbing. - ---- - -## Task 1: Build `EntityScriptActivator` with tests (TDD) - -**Files:** -- Create: `src/AcDream.App/Rendering/Vfx/EntityScriptActivator.cs` -- Create: `tests/AcDream.Core.Tests/Rendering/Vfx/EntityScriptActivatorTests.cs` - -- [ ] **Step 1.1 β€” Write the test file with three failing tests + helpers** - -Create `tests/AcDream.Core.Tests/Rendering/Vfx/EntityScriptActivatorTests.cs`: - -```csharp -using System.Collections.Generic; -using System.Numerics; -using AcDream.App.Rendering.Vfx; -using AcDream.Core.Physics; -using AcDream.Core.Vfx; -using AcDream.Core.World; -using DatReaderWriter.Types; -using Xunit; -using DatPhysicsScript = DatReaderWriter.DBObjs.PhysicsScript; - -namespace AcDream.Core.Tests.Rendering.Vfx; - -public sealed class EntityScriptActivatorTests -{ - /// Recording sink so we can assert which hooks the runner fires. - private sealed class RecordingSink : IAnimationHookSink - { - public List<(uint EntityId, Vector3 Pos, AnimationHook Hook)> Calls = new(); - public void OnHook(uint entityId, Vector3 worldPos, AnimationHook hook) - => Calls.Add((entityId, worldPos, hook)); - } - - private static DatPhysicsScript BuildScript(params (double time, AnimationHook hook)[] items) - { - var script = new DatPhysicsScript(); - foreach (var (t, h) in items) - script.ScriptData.Add(new PhysicsScriptData { StartTime = t, Hook = h }); - return script; - } - - private static WorldEntity MakeEntity(uint serverGuid, Vector3 position) => - new() - { - Id = serverGuid, - ServerGuid = serverGuid, - SourceGfxObjOrSetupId = 0x02000001u, - Position = position, - Rotation = Quaternion.Identity, - MeshRefs = System.Array.Empty(), - }; - - private record Pipeline( - ParticleSystem System, - ParticleHookSink Sink, - PhysicsScriptRunner Runner, - RecordingSink Recording); - - private static Pipeline BuildPipeline(params (uint id, DatPhysicsScript script)[] scripts) - { - var registry = new EmitterDescRegistry(); - var system = new ParticleSystem(registry); - var hookSink = new ParticleHookSink(system); // for activator's StopAllForEntity - var recording = new RecordingSink(); // for runner's hook dispatch - var table = new Dictionary(); - foreach (var (id, s) in scripts) table[id] = s; - var runner = new PhysicsScriptRunner( - id => table.TryGetValue(id, out var s) ? s : null, - recording); - return new Pipeline(system, hookSink, runner, recording); - } - - [Fact] - public void OnCreate_WithDefaultScript_FiresRunnerWithEntityGuidAndPosition() - { - var p = BuildPipeline( - (0xAAu, BuildScript((0.0, new CreateParticleHook { EmitterInfoId = 100 })))); - var activator = new EntityScriptActivator(p.Runner, p.Sink, _ => 0xAAu); - var entity = MakeEntity(serverGuid: 0xCAFEu, position: new Vector3(1, 2, 3)); - - activator.OnCreate(entity); - - Assert.Equal(1, p.Runner.ActiveScriptCount); - p.Runner.Tick(0.001f); - Assert.Single(p.Recording.Calls); - Assert.Equal(0xCAFEu, p.Recording.Calls[0].EntityId); - Assert.Equal(new Vector3(1, 2, 3), p.Recording.Calls[0].Pos); - } - - [Fact] - public void OnCreate_WithoutDefaultScript_DoesNothing() - { - var p = BuildPipeline(); // no scripts registered - var activator = new EntityScriptActivator(p.Runner, p.Sink, _ => 0u); - var entity = MakeEntity(serverGuid: 0xCAFEu, position: Vector3.Zero); - - activator.OnCreate(entity); - - Assert.Equal(0, p.Runner.ActiveScriptCount); - Assert.Empty(p.Recording.Calls); - } - - [Fact] - public void OnRemove_StopsScriptsAndEmitters() - { - var p = BuildPipeline( - (0xAAu, BuildScript((0.0, new CreateParticleHook { EmitterInfoId = 100 })))); - var activator = new EntityScriptActivator(p.Runner, p.Sink, _ => 0xAAu); - var entity = MakeEntity(serverGuid: 0xCAFEu, position: Vector3.Zero); - - activator.OnCreate(entity); - Assert.Equal(1, p.Runner.ActiveScriptCount); - - activator.OnRemove(0xCAFEu); - - Assert.Equal(0, p.Runner.ActiveScriptCount); - // Tick after Remove must not surface any further hook fires. - p.Runner.Tick(1.0f); - Assert.Empty(p.Recording.Calls); - } -} -``` - -- [ ] **Step 1.2 β€” Run the tests, confirm they fail with "type not found"** - -Run: `dotnet test tests/AcDream.Core.Tests/AcDream.Core.Tests.csproj --filter "FullyQualifiedName~EntityScriptActivatorTests"` - -Expected: compile error β€” `AcDream.App.Rendering.Vfx.EntityScriptActivator` does not exist. (This is the failing red-bar that drives the next step.) - -- [ ] **Step 1.3 β€” Create the `Vfx/` directory and the activator file** - -Create `src/AcDream.App/Rendering/Vfx/EntityScriptActivator.cs`: - -```csharp -using System; -using System.Numerics; -using AcDream.Core.Vfx; -using AcDream.Core.World; - -namespace AcDream.App.Rendering.Vfx; - -/// -/// Fires Setup.DefaultScript through -/// when a server-spawned enters the world, so static -/// objects (portals, chimneys, fireplaces, building details) emit their -/// retail-faithful persistent particle effects automatically. Stops the -/// scripts and live emitters when the entity despawns. -/// -/// -/// Wires alongside EntitySpawnAdapter in GpuWorldState: the -/// adapter handles meshes + animation state, the activator handles scripts + -/// particles. Both are render-thread-only. -/// -/// -/// -/// Retail oracle: play_script_internal(setup.DefaultScript) is what -/// retail's CPhysicsObj invokes at object load (see Phase C.1 plan Β§C.1 -/// and memory/project_sky_pes_port.md). C.1 already shipped the runner; -/// this class adds the missing fire-on-spawn call site. -/// -/// -public sealed class EntityScriptActivator -{ - private readonly PhysicsScriptRunner _scriptRunner; - private readonly ParticleHookSink _particleSink; - private readonly Func _defaultScriptResolver; - - /// Already-shipped runner from C.1. Owns the - /// (scriptId, entityId) instance table and schedules hooks at their - /// StartTime offsets. - /// Already-shipped hook sink from C.1. The - /// activator only calls its - /// to drop any per-entity emitter handles on despawn. - /// Returns - /// entity.SourceGfxObjOrSetupId's Setup.DefaultScript.DataId, - /// or 0 on miss / dat throw / missing field. Production lambda hits - /// ; tests pass a hand-rolled - /// stub. - public EntityScriptActivator( - PhysicsScriptRunner scriptRunner, - ParticleHookSink particleSink, - Func defaultScriptResolver) - { - ArgumentNullException.ThrowIfNull(scriptRunner); - ArgumentNullException.ThrowIfNull(particleSink); - ArgumentNullException.ThrowIfNull(defaultScriptResolver); - _scriptRunner = scriptRunner; - _particleSink = particleSink; - _defaultScriptResolver = defaultScriptResolver; - } - - /// - /// Resolve the entity's Setup.DefaultScript and fire it through - /// the script runner. No-op if the entity has no DefaultScript - /// (resolver returns 0) or if the entity has no server guid - /// (atlas-tier entities are out of scope for this activator). - /// - public void OnCreate(WorldEntity entity) - { - ArgumentNullException.ThrowIfNull(entity); - if (entity.ServerGuid == 0) return; - - uint scriptId = _defaultScriptResolver(entity); - if (scriptId == 0) return; - - _scriptRunner.Play(scriptId, entity.ServerGuid, entity.Position); - } - - /// - /// Stop every script instance the runner is tracking for this entity, and - /// kill every live emitter the sink has attributed to it. Idempotent for - /// unknown guids (both calls no-op). - /// - public void OnRemove(uint serverGuid) - { - if (serverGuid == 0) return; - _scriptRunner.StopAllForEntity(serverGuid); - _particleSink.StopAllForEntity(serverGuid, fadeOut: false); - } -} -``` - -- [ ] **Step 1.4 β€” Run the tests, confirm all three pass** - -Run: `dotnet test tests/AcDream.Core.Tests/AcDream.Core.Tests.csproj --filter "FullyQualifiedName~EntityScriptActivatorTests"` - -Expected: 3 passed, 0 failed. - -If a test fails: re-read the assertion against the implementation. The most likely failure is `RecordingSink.Calls` empty after `Runner.Tick` β€” that means the `Play` call didn't queue the script. Check that `entity.ServerGuid != 0` in `MakeEntity`. - -- [ ] **Step 1.5 β€” Run the full test suite for the test project** - -Run: `dotnet test tests/AcDream.Core.Tests/AcDream.Core.Tests.csproj` - -Expected: all existing tests still pass plus the new 3. - -- [ ] **Step 1.6 β€” Commit Task 1** - -```bash -git add src/AcDream.App/Rendering/Vfx/EntityScriptActivator.cs tests/AcDream.Core.Tests/Rendering/Vfx/EntityScriptActivatorTests.cs -git commit -m "$(cat <<'EOF' -feat(vfx #C.1.5a): add EntityScriptActivator (no wiring yet) - -New ~50-line orchestrator that fires Setup.DefaultScript through the -already-shipped PhysicsScriptRunner on entity spawn and stops scripts + -live emitters on despawn. Resolver delegate avoids DatCollection coupling -so the class is fully unit-testable with stubs. - -Three xUnit tests cover the three branches: fire-with-script, -no-op-without-script, stop-on-remove. No wiring into the live spawn path -yet β€” that lands in the next commit. - -Spec: docs/superpowers/specs/2026-05-12-phase-c1.5a-portals-design.md - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - ---- - -## Task 2: Wire activator into `GpuWorldState` - -**Files:** -- Modify: `src/AcDream.App/Streaming/GpuWorldState.cs:42-65` (field + constructor) -- Modify: `src/AcDream.App/Streaming/GpuWorldState.cs:285` (OnRemove call site) -- Modify: `src/AcDream.App/Streaming/GpuWorldState.cs:345` (OnCreate call site) - -- [ ] **Step 2.1 β€” Add `using` for the new namespace** - -Open `src/AcDream.App/Streaming/GpuWorldState.cs`. The existing `using` block at the top (line ~4) imports `AcDream.App.Rendering.Wb;`. Add a second line below it: - -```csharp -using AcDream.App.Rendering.Vfx; -``` - -- [ ] **Step 2.2 β€” Add the field** - -Around line 43 there is: - -```csharp -private readonly EntitySpawnAdapter? _wbEntitySpawnAdapter; -``` - -Add immediately below: - -```csharp -private readonly EntityScriptActivator? _entityScriptActivator; -``` - -- [ ] **Step 2.3 β€” Extend the constructor** - -Replace the existing constructor (lines 57–65) with: - -```csharp -public GpuWorldState( - LandblockSpawnAdapter? wbSpawnAdapter = null, - EntitySpawnAdapter? wbEntitySpawnAdapter = null, - System.Action? onLandblockUnloaded = null, - EntityScriptActivator? entityScriptActivator = null) -{ - _wbSpawnAdapter = wbSpawnAdapter; - _wbEntitySpawnAdapter = wbEntitySpawnAdapter; - _onLandblockUnloaded = onLandblockUnloaded; - _entityScriptActivator = entityScriptActivator; -} -``` - -The new parameter is optional and last β€” existing callers (production and tests) compile unchanged. - -- [ ] **Step 2.4 β€” Add the `OnCreate` call in `AppendLiveEntity`** - -At line 345 the existing call is: - -```csharp -_wbEntitySpawnAdapter?.OnCreate(entity); -``` - -Add immediately below: - -```csharp -_entityScriptActivator?.OnCreate(entity); -``` - -- [ ] **Step 2.5 β€” Add the `OnRemove` call in `RemoveEntityByServerGuid`** - -At line 285 the existing call is: - -```csharp -_wbEntitySpawnAdapter?.OnRemove(serverGuid); -``` - -Add immediately below: - -```csharp -_entityScriptActivator?.OnRemove(serverGuid); -``` - -- [ ] **Step 2.6 β€” Run the build to confirm GpuWorldState compiles** - -Run: `dotnet build src/AcDream.App/AcDream.App.csproj` - -Expected: build succeeds. `GameWindow.cs` still calls the old 3-arg constructor; the new parameter is optional so this compiles fine. - -- [ ] **Step 2.7 β€” Run the test suite to confirm GpuWorldStateTests still pass** - -Run: `dotnet test tests/AcDream.Core.Tests/AcDream.Core.Tests.csproj --filter "FullyQualifiedName~GpuWorldStateTests"` - -Expected: all pass. Existing tests construct `GpuWorldState` with positional args; they don't pass the new optional parameter so behavior is unchanged. - -If a test fails because it asserts something about per-entity-lifecycle ordering: read the assertion. The new `?.OnCreate(entity)` after `_wbEntitySpawnAdapter?.OnCreate(entity)` is a no-op when no activator is injected, so tests that don't inject one should not see new behavior. - -- [ ] **Step 2.8 β€” Commit Task 2** - -```bash -git add src/AcDream.App/Streaming/GpuWorldState.cs -git commit -m "$(cat <<'EOF' -feat(vfx #C.1.5a): wire EntityScriptActivator into GpuWorldState lifecycle - -GpuWorldState grows a fourth optional ctor parameter for the activator, -paralleling how EntitySpawnAdapter is plumbed. AppendLiveEntity calls -OnCreate after the existing _wbEntitySpawnAdapter?.OnCreate; -RemoveEntityByServerGuid calls OnRemove after the existing OnRemove. -Symmetric, same order, null-safe. - -GameWindow still passes the old 3-arg ctor β€” activator construction + -wire-through lands in the next commit. - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - ---- - -## Task 3: Construct activator in `GameWindow` and pass through to `GpuWorldState` - -**Files:** -- Modify: `src/AcDream.App/Rendering/GameWindow.cs:35` (field declaration block) -- Modify: `src/AcDream.App/Rendering/GameWindow.cs:1612-1622` (activator construction + GpuWorldState ctor call) - -- [ ] **Step 3.1 β€” Add the field declaration** - -Around line 35 in `GameWindow.cs` there is: - -```csharp -private AcDream.App.Rendering.Wb.EntitySpawnAdapter? _wbEntitySpawnAdapter; -``` - -Add immediately below: - -```csharp -private AcDream.App.Rendering.Vfx.EntityScriptActivator? _entityScriptActivator; -``` - -- [ ] **Step 3.2 β€” Build the resolver lambda and construct the activator** - -In the block starting at line 1612 (where `wbEntitySpawnAdapter` is constructed and assigned), the current code is: - -```csharp -var wbEntitySpawnAdapter = new AcDream.App.Rendering.Wb.EntitySpawnAdapter( - _textureCache!, SequencerFactory, _wbMeshAdapter!); -_wbEntitySpawnAdapter = wbEntitySpawnAdapter; -// Phase Post-A.5 #53 (Task 12): wire EntityClassificationCache.InvalidateLandblock -// so Tier 1 cache entries get swept on LB demote (Near to Far) and unload. -// Per spec Β§5.3 W3b. The callback receives the canonical landblock id -// matching the LandblockHint stored at Populate time. -_worldState = new AcDream.App.Streaming.GpuWorldState( - wbSpawnAdapter, - wbEntitySpawnAdapter, - onLandblockUnloaded: _classificationCache.InvalidateLandblock); -``` - -Replace with: - -```csharp -var wbEntitySpawnAdapter = new AcDream.App.Rendering.Wb.EntitySpawnAdapter( - _textureCache!, SequencerFactory, _wbMeshAdapter!); -_wbEntitySpawnAdapter = wbEntitySpawnAdapter; - -// Phase C.1.5a: construct EntityScriptActivator so server-spawned static -// entities (portals first) fire Setup.DefaultScript through the -// PhysicsScriptRunner on enter-world. _scriptRunner and _particleSink -// are initialised earlier in OnLoad (line ~1083); both are non-null -// here. The resolver lambda captures _dats and swallows dat-lookup -// throws β€” see C.1.5a spec Β§6 (error handling) for rationale. -var capturedDatsForActivator = _dats; -uint ResolveDefaultScript(AcDream.Core.World.WorldEntity e) -{ - try - { - var setup = capturedDatsForActivator?.Get(e.SourceGfxObjOrSetupId); - return setup?.DefaultScript.DataId ?? 0u; - } - catch - { - return 0u; - } -} -var entityScriptActivator = new AcDream.App.Rendering.Vfx.EntityScriptActivator( - _scriptRunner!, _particleSink!, ResolveDefaultScript); -_entityScriptActivator = entityScriptActivator; - -// Phase Post-A.5 #53 (Task 12): wire EntityClassificationCache.InvalidateLandblock -// so Tier 1 cache entries get swept on LB demote (Near to Far) and unload. -// Per spec Β§5.3 W3b. The callback receives the canonical landblock id -// matching the LandblockHint stored at Populate time. -_worldState = new AcDream.App.Streaming.GpuWorldState( - wbSpawnAdapter, - wbEntitySpawnAdapter, - onLandblockUnloaded: _classificationCache.InvalidateLandblock, - entityScriptActivator: entityScriptActivator); -``` - -Two changes: (1) inline construction of activator + resolver between `_wbEntitySpawnAdapter` assignment and the `_worldState =` line, (2) add the `entityScriptActivator: entityScriptActivator` named argument to the `GpuWorldState` constructor call. - -- [ ] **Step 3.3 β€” Build the project** - -Run: `dotnet build src/AcDream.App/AcDream.App.csproj` - -Expected: build succeeds. If you get an "_scriptRunner is null here" warning at the activator construction site, it's a nullable-flow false positive β€” the runner is built at line 1083 inside the same `OnLoad` method which executes before this block. Use `_scriptRunner!` and `_particleSink!` (already shown above). - -- [ ] **Step 3.4 β€” Run the full test suite** - -Run: `dotnet test` - -Expected: all tests pass. No new tests added in this task β€” verification of the wiring is the visual step in Task 4. - -- [ ] **Step 3.5 β€” Commit Task 3** - -```bash -git add src/AcDream.App/Rendering/GameWindow.cs -git commit -m "$(cat <<'EOF' -feat(vfx #C.1.5a): construct EntityScriptActivator in GameWindow - -Wires the activator into the production lifecycle: -- Construct alongside _wbEntitySpawnAdapter using _scriptRunner + - _particleSink (both built earlier in OnLoad). -- Production resolver lambda hits _dats.Get(...) wrapped in - try/catch returning 0 on miss/throw β€” matches ParticleRenderer's - defensive read pattern. -- Pass into GpuWorldState's new optional ctor parameter. - -Closes the wiring half of C.1.5a. Visual verification at the Holtburg -Town network portal is the acceptance gate. - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - ---- - -## Task 4: Visual verification + roadmap update - -**Files:** -- Modify: `docs/plans/2026-04-11-roadmap.md` (append a "Phase C.1.5a SHIPPED" entry) - -This is a manual verification task with a user-in-the-loop step. Do not mark the slice "done" until the user confirms the portal swirl visually matches retail. - -- [ ] **Step 4.1 β€” Build green, tests green** - -Run sequentially: - -```powershell -dotnet build -dotnet test -``` - -Expected: both green. If either fails: stop and fix before launching. - -- [ ] **Step 4.2 β€” Kill any stale acdream process from a prior session** - -Run: - -```powershell -Get-Process -Name AcDream.App -ErrorAction SilentlyContinue | Stop-Process -Force -Start-Sleep -Seconds 3 -``` - -Per [CLAUDE.md](../../../CLAUDE.md) "Logout-before-reconnect" β€” ACE keeps a session alive briefly after disconnect; relaunching within ~3 s causes a handshake failure that looks like a code bug but isn't. - -- [ ] **Step 4.3 β€” Launch the live client with PES diagnostics** - -```powershell -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -$env:ACDREAM_TEST_HOST = "127.0.0.1" -$env:ACDREAM_TEST_PORT = "9000" -$env:ACDREAM_TEST_USER = "testaccount" -$env:ACDREAM_TEST_PASS = "testpassword" -$env:ACDREAM_DUMP_PLAYSCRIPT = "1" -dotnet run --project src\AcDream.App\AcDream.App.csproj --no-build -c Debug 2>&1 | Tee-Object -FilePath "c1.5a-verify.log" -``` - -(Use the Bash tool's `run_in_background: true` parameter so the launch doesn't block the agent on the user's testing.) - -- [ ] **Step 4.4 β€” Hand off to the user for visual verification** - -Once the client reaches in-world state (~8 s after launch), tell the user: - -> "Client launched with PES diagnostics. Walk `+Acdream` to the Holtburg Town network portal and compare side-by-side with retail. Confirm the portal swirl matches in color, density, motion, and persistence. Reply 'pass' if it matches or describe what differs." - -Wait for the user's response. If they reply with anything other than confirmation, stop and investigate; do NOT proceed to Step 4.5. - -- [ ] **Step 4.5 β€” On user confirmation: update the roadmap** - -Open `docs/plans/2026-04-11-roadmap.md`. Find the section listing recently-shipped phases (look for "Phase N.6 slice 1" or similar recent entries). Add a new entry above the earlier shipped phases: - -```markdown -**Phase C.1.5a (Portal PES wiring) shipped 2026-05-12.** Server-spawned -`WorldEntity` entities now fire their `Setup.DefaultScript` through the -shipped `PhysicsScriptRunner` on enter-world. New -[`EntityScriptActivator`](../../src/AcDream.App/Rendering/Vfx/EntityScriptActivator.cs) -class wires into `GpuWorldState`'s spawn lifecycle. Visual verification -passed at the Holtburg Town network portal. Slice 2 (C.1.5b β€” EnvCell -static objects + animation-hook verification) is the natural next step. -Spec: [`docs/superpowers/specs/2026-05-12-phase-c1.5a-portals-design.md`](../superpowers/specs/2026-05-12-phase-c1.5a-portals-design.md). -Plan: [`docs/superpowers/plans/2026-05-12-phase-c1.5a-portals.md`](../superpowers/plans/2026-05-12-phase-c1.5a-portals.md). -``` - -If the roadmap has a "Currently in flight" line that mentions C.1.5 or -similar, update it: change "in flight" to "Phase C.1.5b (EnvCell statics -+ verification) β€” see [C.1.5a spec Β§10 slice 2 preview](../superpowers/specs/2026-05-12-phase-c1.5a-portals-design.md)". - -- [ ] **Step 4.6 β€” Commit the roadmap update** - -```bash -git add docs/plans/2026-04-11-roadmap.md -git commit -m "$(cat <<'EOF' -docs(roadmap #C.1.5a): mark Phase C.1.5a shipped - -Portal PES wiring landed and visually verified at the Holtburg Town -network portal. EntityScriptActivator fires Setup.DefaultScript through -the shipped PhysicsScriptRunner on entity spawn. C.1.5b (EnvCell static -objects + animation-hook verification) is the next slice. - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - -- [ ] **Step 4.7 β€” Close the loop** - -Report to the user: -- C.1.5a shipped, three commits: activator, wiring, roadmap. -- Tests green; visual verification passed. -- Suggest brainstorming C.1.5b as the next step (or take a break / pick something else). - ---- - -## Self-review against the spec - -Run through the spec's section list and confirm each requirement maps to a plan task: - -- **Β§2 Scope "in":** - - New `EntityScriptActivator` class β†’ Task 1.3 βœ“ - - Wiring in `GpuWorldState` β†’ Task 2.4, 2.5 βœ“ - - Activator constructed in `GameWindow`, passed into `GpuWorldState` β†’ Task 3.2 βœ“ - - Three unit tests β†’ Task 1.1 βœ“ - - Visual verification at Holtburg Town network portal β†’ Task 4.3, 4.4 βœ“ -- **Β§4 Architecture β€” file placement under `Rendering/Vfx/`** β†’ Task 1.3 (creates the directory implicitly via the file path) βœ“ -- **Β§4 Architecture β€” resolver delegate pattern** β†’ Tests use stubs (Task 1.1); production uses the lambda in `GameWindow` (Task 3.2) βœ“ -- **Β§4 Trigger condition "has DefaultScript, not is portal"** β†’ Resolver returns `Setup.DefaultScript.DataId ?? 0`; activator gates `if (scriptId == 0) return` (Task 1.3) βœ“ -- **Β§5 Lifecycle ordering: spawnAdapter β†’ activator** β†’ Task 2.4, 2.5 add the activator call immediately after the existing adapter call βœ“ -- **Β§6 Error handling β€” resolver swallows exceptions** β†’ Task 3.2 wraps `_dats.Get(...)` in try/catch returning 0 βœ“ -- **Β§7 Thread safety** β†’ All calls on render thread; no new synchronization needed (covered by inheriting `GpuWorldState`'s existing single-thread contract) βœ“ -- **Β§8 Three named tests** β†’ Task 1.1 βœ“ -- **Β§8 Visual verification procedure** β†’ Task 4.3–4.5 βœ“ - -No gaps. - -Type / name consistency check: -- `EntityScriptActivator` is the class name in tests (Task 1.1), the file (Task 1.3), the field in `GpuWorldState` (Task 2.2), the parameter (Task 2.3), and the field + construction in `GameWindow` (Task 3.1, 3.2). Consistent. -- `OnCreate(WorldEntity)` / `OnRemove(uint)` signatures match across tests and implementation. βœ“ -- Constructor signature `(PhysicsScriptRunner, ParticleHookSink, Func)` matches between tests, implementation, and production wiring. βœ“ -- `ResolveDefaultScript` lambda (Task 3.2) returns `uint` β€” matches the `Func` declared on the activator. βœ“ diff --git a/docs/superpowers/plans/2026-05-12-phase-l2g-slice1.md b/docs/superpowers/plans/2026-05-12-phase-l2g-slice1.md deleted file mode 100644 index 2ad4e62..0000000 --- a/docs/superpowers/plans/2026-05-12-phase-l2g-slice1.md +++ /dev/null @@ -1,899 +0,0 @@ -# Phase L.2g slice 1 β€” Dynamic PhysicsState Toggling Implementation Plan - -> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. - -**Spec:** [docs/superpowers/specs/2026-05-12-l2g-dynamic-physicsstate-design.md](../specs/2026-05-12-l2g-dynamic-physicsstate-design.md) (committed in `2c10dd4`). -**Branch:** `claude/gallant-mestorf-3bf2e3` (do all commits here; user merges to main separately). - -**Goal:** Parse inbound `GameMessageSetState (opcode 0xF74B)` and propagate -the new `PhysicsState` value into `ShadowObjectRegistry`'s cached per-entity -record so the existing `CollisionExemption.IsExempt(...)` short-circuit honors -runtime ETHEREAL flips β€” unblocking the M1 demo's *"open the inn door"* line. - -**Architecture:** One new wire-message parser (`SetState`), one new event on -`WorldSession`, one new mutator method on `ShadowObjectRegistry` -(`UpdatePhysicsState`), one new subscriber in `GameWindow`. **No resolver -changes.** The existing `CollisionExemption.cs` short-circuit (cited at -`acclient_2013_pseudo_c.txt:276782`) already handles ETHEREAL; slice 1 just -feeds it fresh data. - -**Tech Stack:** C# .NET 10, xUnit for tests, BinaryPrimitives for -little-endian reads. Mirror the existing `VectorUpdate.cs` parser pattern. - -**Retail anchor (port reference):** `CPhysicsObj::set_state` at -`docs/research/named-retail/acclient_2013_pseudo_c.txt:283044`. The retail -implementation: `this->state = arg2` (line 283048) plus three side-effect -handlers for the changed-bit set (`0x800` lighting, `0x20` nodraw, `0x4000` -hidden). **Slice 1 ports only the state-store half**; ETHEREAL (`0x4`) is -not in the side-effect set, so the cosmetic handlers are not on the -M1-critical path and stay deferred. - ---- - -## File Structure - -| File | Action | Responsibility | -|---|---|---| -| `src/AcDream.Core.Net/Messages/SetState.cs` | Create | New inbound DTO + `TryParse` for opcode `0xF74B`. Mirrors `VectorUpdate.cs`. | -| `src/AcDream.Core.Net/WorldSession.cs` | Modify | Add `StateUpdated` event + dispatch branch for `op == SetState.Opcode`. | -| `src/AcDream.Core/Physics/ShadowObjectRegistry.cs` | Modify | Add `UpdatePhysicsState(uint, uint)` method that mutates the cached `ShadowEntry.State` in every cell the entity occupies. | -| `src/AcDream.App/Rendering/GameWindow.cs` | Modify | Subscribe to `_liveSession.StateUpdated`, route `(guid, newState)` to `_physicsEngine.ShadowObjects.UpdatePhysicsState(...)`. Extend `[entity-source]` log with `state=` + `flags=` (slice 0.5 freebie). | -| `tests/AcDream.Core.Net.Tests/Messages/SetStateTests.cs` | Create | TryParse byte-level tests (well-formed, truncated, opcode-mismatch). | -| `tests/AcDream.Core.Tests/Physics/ShadowObjectRegistryTests.cs` | Modify | Add `UpdatePhysicsState_FlipsEthereal_NextLookupExempt` test using `CollisionExemption.ShouldSkip`. | - -**No new project references needed** β€” all files live in existing assemblies. - ---- - -## Task 1: Parser DTO + TryParse for `SetState` (opcode `0xF74B`) - -**Files:** -- Create: `src/AcDream.Core.Net/Messages/SetState.cs` -- Create: `tests/AcDream.Core.Net.Tests/Messages/SetStateTests.cs` - -**Reference template:** `src/AcDream.Core.Net/Messages/VectorUpdate.cs` -(read it before writing β€” same opcode dispatch convention, same body-length -check shape, same `BinaryPrimitives` style). - -**Wire format** (per -`references/holtburger/crates/holtburger-protocol/src/messages/object/messages/properties.rs:117-122`, -matched by every other acdream parser): - -``` -offset 0 : u32 opcode (= 0xF74B) -offset 4 : u32 guid -offset 8 : u32 physics_state (bitmask; ETHEREAL = 0x4) -offset 12 : u16 instance_sequence -offset 14 : u16 state_sequence -Total: 16 bytes from start of body. -``` - -- [ ] **Step 1.1: Write the failing TryParse tests** - -Create `tests/AcDream.Core.Net.Tests/Messages/SetStateTests.cs` with: - -```csharp -using System; -using System.Buffers.Binary; -using AcDream.Core.Net.Messages; -using Xunit; - -namespace AcDream.Core.Net.Tests.Messages; - -public class SetStateTests -{ - [Fact] - public void TryParse_WellFormedBody_ReturnsParsed() - { - // Build a synthetic SetState body: opcode + guid + state + 2Γ—u16 seq. - var buf = new byte[16]; - BinaryPrimitives.WriteUInt32LittleEndian(buf.AsSpan(0, 4), 0xF74Bu); - BinaryPrimitives.WriteUInt32LittleEndian(buf.AsSpan(4, 4), 0x000F4244u); // door guid - BinaryPrimitives.WriteUInt32LittleEndian(buf.AsSpan(8, 4), 0x00000004u); // ETHEREAL bit - BinaryPrimitives.WriteUInt16LittleEndian(buf.AsSpan(12, 2), (ushort)355); - BinaryPrimitives.WriteUInt16LittleEndian(buf.AsSpan(14, 2), (ushort)42); - - var parsed = SetState.TryParse(buf); - - Assert.NotNull(parsed); - Assert.Equal(0x000F4244u, parsed.Value.Guid); - Assert.Equal(0x00000004u, parsed.Value.PhysicsState); - Assert.Equal((ushort)355, parsed.Value.InstanceSequence); - Assert.Equal((ushort)42, parsed.Value.StateSequence); - } - - [Fact] - public void TryParse_Truncated_ReturnsNull() - { - var buf = new byte[10]; // < 16 bytes - Assert.Null(SetState.TryParse(buf)); - } - - [Fact] - public void TryParse_WrongOpcode_ReturnsNull() - { - var buf = new byte[16]; - BinaryPrimitives.WriteUInt32LittleEndian(buf.AsSpan(0, 4), 0xF74Cu); // UpdateMotion, not SetState - Assert.Null(SetState.TryParse(buf)); - } -} -``` - -- [ ] **Step 1.2: Run tests to verify they fail (RED)** - -Run: `dotnet test tests/AcDream.Core.Net.Tests/AcDream.Core.Net.Tests.csproj --filter "FullyQualifiedName~SetStateTests"` -Expected: Compile error β€” `SetState` type not defined. - -- [ ] **Step 1.3: Write the parser** - -Create `src/AcDream.Core.Net/Messages/SetState.cs`: - -```csharp -using System; -using System.Buffers.Binary; - -namespace AcDream.Core.Net.Messages; - -/// -/// Inbound SetState GameMessage (opcode 0xF74B). The server -/// broadcasts this whenever a previously-spawned entity's -/// PhysicsState bitmask changes after CreateObject β€” chiefly -/// when a door opens / closes (server flips ETHEREAL_PS = 0x4) or a -/// spell projectile becomes ethereal post-impact. -/// -/// -/// Wire layout (per -/// references/holtburger/crates/holtburger-protocol/src/messages/object/messages/properties.rs:117-122, -/// matched by every other acdream parser): -/// -/// -/// u32 opcode β€” 0xF74B -/// u32 objectGuid -/// u32 physicsState β€” bitmask (acclient.h:2815 / 2819) -/// u16 instanceSequence β€” stale-packet rejection -/// u16 stateSequence β€” stale-packet rejection -/// -/// -/// -/// Total body size: 16 bytes from start (opcode + 12-byte payload). -/// -/// -/// -/// Server-side reference: -/// references/ACE/Source/ACE.Server/Network/GameMessages/Messages/GameMessageSetState.cs:8-15 -/// (ACE writes the same field order but appears to use uint for the -/// sequence fields; verified against retail format by hex-dump probe in -/// Task 5). Holtburger has been validated against a retail-format server, -/// so its 12-byte payload is the trusted spec. -/// -/// -public static class SetState -{ - public const uint Opcode = 0xF74Bu; - - public readonly record struct Parsed( - uint Guid, - uint PhysicsState, - ushort InstanceSequence, - ushort StateSequence); - - /// - /// Parse a 0xF74B body. must start with the - /// 4-byte opcode (matches the convention used by VectorUpdate / - /// UpdateMotion / UpdatePosition). Returns null on truncation or - /// opcode mismatch. - /// - public static Parsed? TryParse(ReadOnlySpan body) - { - if (body.Length < 16) return null; - try - { - uint opcode = BinaryPrimitives.ReadUInt32LittleEndian(body.Slice(0, 4)); - if (opcode != Opcode) return null; - - uint guid = BinaryPrimitives.ReadUInt32LittleEndian(body.Slice(4, 4)); - uint state = BinaryPrimitives.ReadUInt32LittleEndian(body.Slice(8, 4)); - ushort instSeq = BinaryPrimitives.ReadUInt16LittleEndian(body.Slice(12, 2)); - ushort stateSeq = BinaryPrimitives.ReadUInt16LittleEndian(body.Slice(14, 2)); - - return new Parsed(guid, state, instSeq, stateSeq); - } - catch - { - return null; - } - } -} -``` - -- [ ] **Step 1.4: Run tests to verify they pass (GREEN)** - -Run: `dotnet test tests/AcDream.Core.Net.Tests/AcDream.Core.Net.Tests.csproj --filter "FullyQualifiedName~SetStateTests"` -Expected: 3 passed. - -- [ ] **Step 1.5: Verify project build still green** - -Run: `dotnet build` -Expected: Build succeeded, 0 errors, 0 new warnings. - -- [ ] **Step 1.6: Commit** - -``` -git add src/AcDream.Core.Net/Messages/SetState.cs tests/AcDream.Core.Net.Tests/Messages/SetStateTests.cs -git commit -m "feat(phys L.2g slice 1): inbound SetState (0xF74B) parser - -DTO + TryParse for the GameMessageSetState wire message. The server -broadcasts this when an already-spawned entity's PhysicsState changes -post-CreateObject β€” chiefly when a door's Ethereal bit toggles on Use. - -Wire format per holtburger SetStateData (validated against retail-format -servers): u32 opcode + u32 guid + u32 state + u16 instanceSequence + u16 -stateSequence = 16 bytes total. Mirrors the existing VectorUpdate.cs -template. - -Co-Authored-By: Claude Opus 4.7 (1M context) " -``` - ---- - -## Task 2: `ShadowObjectRegistry.UpdatePhysicsState(guid, newState)` - -**Files:** -- Modify: `src/AcDream.Core/Physics/ShadowObjectRegistry.cs` (add new method after `UpdatePosition`, before `Deregister`) -- Modify: `tests/AcDream.Core.Tests/Physics/ShadowObjectRegistryTests.cs` (append new test) - -**Design rationale:** `ShadowEntry` is a `readonly record struct` (value type) -stored as copies inside per-cell `List`. Mutation pattern: -find every cell the entity occupies via `_entityToCells[entityId]`, then -replace each in-list copy with `list[i] = list[i] with { State = newState }`. - -**Retail anchor:** `CPhysicsObj::set_state` at -`docs/research/named-retail/acclient_2013_pseudo_c.txt:283044`. Retail -does `this->state = arg2` (line 283048) β€” direct overwrite. Our cached -state lives in the registry copy, not the entity, so the equivalent is -"overwrite every shadow copy." - -- [ ] **Step 2.1: Write the failing test** - -Append to `tests/AcDream.Core.Tests/Physics/ShadowObjectRegistryTests.cs` -(top-of-file using directives already have `AcDream.Core.Physics` + xUnit): - -```csharp - // ----------------------------------------------------------------------- - // UpdatePhysicsState β€” L.2g slice 1 (doors flip ETHEREAL post-spawn) - // ----------------------------------------------------------------------- - - [Fact] - public void UpdatePhysicsState_FlipsEthereal_NextLookupSeesNewBits() - { - // Register a door-like entity with State=0 (closed = solid). - var reg = new ShadowObjectRegistry(); - const uint doorId = 0x000F4244u; - reg.Register(doorId, 0x020019FFu, new Vector3(12f, 12f, 50f), - Quaternion.Identity, 1f, OffX, OffY, LbId, - state: 0u, flags: EntityCollisionFlags.None); - - // Sanity: cached state starts at 0 (no ETHEREAL). - var before = reg.AllEntriesForDebug().Single(e => e.EntityId == doorId); - Assert.Equal(0u, before.State); - - // Flip ETHEREAL_PS (0x4) β€” the server's "door is now open" message. - reg.UpdatePhysicsState(doorId, 0x00000004u); - - // Cached state should now show the new bit. - var after = reg.AllEntriesForDebug().Single(e => e.EntityId == doorId); - Assert.Equal(0x00000004u, after.State); - } - - [Fact] - public void UpdatePhysicsState_UnregisteredEntity_IsNoOp() - { - var reg = new ShadowObjectRegistry(); - // No entity registered. Should not throw. - reg.UpdatePhysicsState(0xDEADBEEFu, 0x00000004u); - Assert.Equal(0, reg.TotalRegistered); - } - - [Fact] - public void UpdatePhysicsState_EntitySpanningMultipleCells_AllCellsUpdated() - { - // Entity at (24,12) with radius=2 spans cells (0,0) and (1,0). - var reg = new ShadowObjectRegistry(); - reg.Register(99u, 0x01000099u, new Vector3(24f, 12f, 50f), - Quaternion.Identity, 2f, OffX, OffY, LbId, - state: 0u); - - reg.UpdatePhysicsState(99u, 0x00000004u); - - uint cellA = LbId | 1u; // cx=0 - uint cellB = LbId | (1u*8 + 0 + 1); // cx=1 - var inA = reg.GetObjectsInCell(cellA).Single(e => e.EntityId == 99u); - var inB = reg.GetObjectsInCell(cellB).Single(e => e.EntityId == 99u); - Assert.Equal(0x00000004u, inA.State); - Assert.Equal(0x00000004u, inB.State); - } -``` - -You may need a `using System.Linq;` at the top of the test file. Add it if -not already present. - -- [ ] **Step 2.2: Run tests to verify they fail (RED)** - -Run: `dotnet test tests/AcDream.Core.Tests/AcDream.Core.Tests.csproj --filter "FullyQualifiedName~UpdatePhysicsState"` -Expected: Compile error β€” `UpdatePhysicsState` method not defined. - -- [ ] **Step 2.3: Implement `UpdatePhysicsState`** - -Insert into `src/AcDream.Core/Physics/ShadowObjectRegistry.cs` after the -`UpdatePosition` method (around line 127, before the `Deregister` summary -comment): - -```csharp - /// - /// Update the cached bits for an - /// already-registered entity. Called by the inbound - /// SetState (0xF74B) dispatcher when the server broadcasts a - /// post-spawn PhysicsState change β€” chiefly doors flipping - /// ETHEREAL_PS = 0x4 on Use, so the - /// short-circuit can honor - /// the new state on the next resolve. - /// - /// - /// Retail equivalent: CPhysicsObj::set_state at - /// docs/research/named-retail/acclient_2013_pseudo_c.txt:283044 - /// β€” direct write `this->state = arg2`. Retail also fires side-effect - /// handlers for the 0x800 (lighting), 0x20 (nodraw), 0x4000 (hidden) - /// changed bits; ETHEREAL (0x4) doesn't trigger any of them, so slice 1 - /// scopes to the bare state-write. - /// - /// - /// - /// Implementation: is a value-type record - /// copied into per-cell lists, so we rewrite the copy in each cell the - /// entity occupies. Unregistered entities are a no-op (callers don't - /// have to gate). - /// - /// - public void UpdatePhysicsState(uint entityId, uint newState) - { - if (!_entityToCells.TryGetValue(entityId, out var cellIds)) - return; // not registered β€” no-op - - foreach (var cellId in cellIds) - { - if (!_cells.TryGetValue(cellId, out var list)) continue; - for (int i = 0; i < list.Count; i++) - { - if (list[i].EntityId == entityId) - list[i] = list[i] with { State = newState }; - } - } - } -``` - -- [ ] **Step 2.4: Run tests to verify they pass (GREEN)** - -Run: `dotnet test tests/AcDream.Core.Tests/AcDream.Core.Tests.csproj --filter "FullyQualifiedName~UpdatePhysicsState"` -Expected: 3 passed. - -- [ ] **Step 2.5: Verify full test suite still green (no regressions)** - -Run: `dotnet test` -Expected: All previously-passing tests still pass. **Note:** ~8 pre-existing -failures may be in the baseline (see `docs/research/2026-05-13-l2d-slice1-shipped-handoff.md` -"Open concerns"); ensure the count does not increase. Stash + rerun to -confirm if uncertain: `git stash && dotnet test 2>&1 | findstr Failed` then -`git stash pop`. - -- [ ] **Step 2.6: Commit** - -``` -git add src/AcDream.Core/Physics/ShadowObjectRegistry.cs tests/AcDream.Core.Tests/Physics/ShadowObjectRegistryTests.cs -git commit -m "feat(phys L.2g slice 1): ShadowObjectRegistry.UpdatePhysicsState - -New mutator that overwrites cached PhysicsState bits on every shadow copy -of the named entity. The existing CollisionExemption.ShouldSkip(...) check -(acclient_2013_pseudo_c.txt:276782) reads the same cached field, so a -post-spawn ETHEREAL flip is now honored on the next resolver tick without -any resolver-path change. - -Retail anchor: CPhysicsObj::set_state at acclient_2013_pseudo_c.txt:283044. -Slice 1 scopes to the bare state-write β€” retail's cosmetic side-effect -handlers (0x800 lighting, 0x20 nodraw, 0x4000 hidden) don't fire for the -ETHEREAL bit and stay deferred. - -Three TDD tests cover: ETHEREAL flip from 0->0x4; unregistered-entity -no-op; entity spanning multiple cells gets all copies updated. - -Co-Authored-By: Claude Opus 4.7 (1M context) " -``` - ---- - -## Task 3: Wire `0xF74B` into `WorldSession` dispatcher + new event - -**Files:** -- Modify: `src/AcDream.Core.Net/WorldSession.cs` (one new event declaration near the existing `VectorUpdated`/`MotionUpdated` events; one new `else if` branch in the inbound dispatcher near `op == VectorUpdate.Opcode`) - -**Reference pattern:** Read the existing `VectorUpdate.Opcode` branch first -(it's at WorldSession.cs:739–752). Copy its shape exactly. - -- [ ] **Step 3.1: Add the public event declaration** - -Find the existing `public event Action<...>? VectorUpdated;` declaration -in `WorldSession.cs` (near line 119, in the events region). Add a sibling: - -```csharp - /// - /// Fires when the server broadcasts a SetState (0xF74B) game - /// message β€” a previously-spawned entity's PhysicsState - /// bitmask changed post-CreateObject. Chiefly doors flipping - /// ETHEREAL_PS = 0x4 on Use (see ACE - /// WorldObjects/Door.cs:127, WorldObject.cs:640-660). - /// Subscribers route the new state into - /// so the - /// existing collision-exemption short-circuit honors the flip on the - /// next resolver tick. - /// - public event Action? StateUpdated; -``` - -Place it immediately after the existing `VectorUpdated` event for grep- -findability. - -- [ ] **Step 3.2: Add the dispatcher branch** - -In the inbound game-message dispatcher (the chain of `else if (op == X.Opcode)` -branches in the same file), add this branch immediately after the -`VectorUpdate.Opcode` branch: - -```csharp - else if (op == SetState.Opcode) - { - // L.2g slice 1 (2026-05-12): server broadcasts SetState - // (0xF74B) when an entity's PhysicsState changes - // post-spawn β€” chiefly doors flipping ETHEREAL on Use. - // Holtburger validated wire format = 16 bytes (opcode + - // guid + state + 2Γ—u16 sequence). ACE - // GameMessageSetState.cs writes the same field order - // but appears to use u32 for the sequences; Task 5's - // hex-dump probe settles the actual byte count. - var parsed = SetState.TryParse(body); - if (parsed is not null) - StateUpdated?.Invoke(parsed.Value); - } -``` - -The `using AcDream.Core.Net.Messages;` directive should already be at the -top of WorldSession.cs (it's used by every existing parser). Confirm, -don't add a duplicate. - -- [ ] **Step 3.3: Verify build still green** - -Run: `dotnet build` -Expected: Build succeeded, 0 errors. - -- [ ] **Step 3.4: Commit** - -``` -git add src/AcDream.Core.Net/WorldSession.cs -git commit -m "feat(phys L.2g slice 1): WorldSession dispatches SetState (0xF74B) - -New StateUpdated event + dispatcher branch routes inbound SetState -messages to subscribers. Mirrors the existing VectorUpdated / -MotionUpdated event pattern. GameWindow will subscribe in the next -commit and feed the parsed (guid, newState) pair to -ShadowObjectRegistry.UpdatePhysicsState. - -Co-Authored-By: Claude Opus 4.7 (1M context) " -``` - ---- - -## Task 4: Subscribe in `GameWindow` and feed `ShadowObjectRegistry` - -**Files:** -- Modify: `src/AcDream.App/Rendering/GameWindow.cs` (one new subscription line + one new handler method) - -This is the final wiring step. After this commit, the server's "door opened" -SetState is end-to-end honored by the collision system. - -- [ ] **Step 4.1: Add the subscription** - -Find the block in `GameWindow.cs` where `_liveSession.MotionUpdated += -OnLiveMotionUpdated;` and `_liveSession.PositionUpdated += -OnLivePositionUpdated;` are wired (around line 1791). Add: - -```csharp - _liveSession.StateUpdated += OnLiveStateUpdated; -``` - -Place it after `_liveSession.VectorUpdated += OnLiveVectorUpdated;` so the -event-subscription order is co-located with its peers. - -- [ ] **Step 4.2: Add the handler method** - -Find the existing `OnLiveVectorUpdated` method body in the same file -(grep `private void OnLiveVectorUpdated`). Add a sibling handler -immediately after it: - -```csharp - /// - /// L.2g slice 1: inbound SetState (0xF74B) handler. Propagates the - /// new PhysicsState bits into ShadowObjectRegistry so the - /// existing check honors - /// the flip on the next resolver tick. Chiefly doors: - /// server flips ETHEREAL_PS = 0x4 on Use, the door's - /// cylinder collision stops blocking the threshold. - /// - private void OnLiveStateUpdated(AcDream.Core.Net.Messages.SetState.Parsed parsed) - { - _physicsEngine.ShadowObjects.UpdatePhysicsState(parsed.Guid, parsed.PhysicsState); - - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeBuildingEnabled) - Console.WriteLine(System.FormattableString.Invariant( - $"[setstate] guid=0x{parsed.Guid:X8} state=0x{parsed.PhysicsState:X8} instSeq={parsed.InstanceSequence} stateSeq={parsed.StateSequence}")); - } -``` - -- [ ] **Step 4.3: Verify build still green** - -Run: `dotnet build` -Expected: Build succeeded, 0 errors. - -- [ ] **Step 4.4: Smoke-test that no regression breaks the launch path** - -Run a quick non-interactive smoke (do NOT do the full visual test yet β€” -that's Task 7): - -```powershell -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "0" # offline; just verify the binary starts -dotnet run --project src\AcDream.App\AcDream.App.csproj --no-build -c Debug 2>&1 | - Select-String -Pattern "Exception|FATAL" | - Select-Object -First 5 -``` - -Then kill the process. Expected: no startup exception, no FATAL. If -anything blows up, the new handler subscription or the registry mutator -broke something in the live-session attach path. - -- [ ] **Step 4.5: Commit** - -``` -git add src/AcDream.App/Rendering/GameWindow.cs -git commit -m "feat(phys L.2g slice 1): GameWindow routes SetState into registry - -End-to-end wiring: WorldSession.StateUpdated fires -> GameWindow -OnLiveStateUpdated -> ShadowObjectRegistry.UpdatePhysicsState -> next -resolver tick sees the updated ETHEREAL bit and CollisionExemption -short-circuits the door cylinder. After this commit the M1 'open the -inn door' scenario is unblocked at the code-path level; visual -verification follows in slice 1's manual test (Task 7). - -The handler also emits a [setstate] diagnostic line when -ACDREAM_PROBE_BUILDING is enabled β€” gives a greppable trail when the -visual test runs. - -Co-Authored-By: Claude Opus 4.7 (1M context) " -``` - ---- - -## Task 5: Hex-dump probe for first SetState payload (wire-byte verification) - -**Files:** -- Modify: `src/AcDream.Core.Net/WorldSession.cs` (extend the existing `else if (op == SetState.Opcode)` branch added in Task 3) - -**Why:** ACE's `GameMessageSetState.cs:13-14` writes `Sequences.GetCurrentSequence(...)` -+ `Sequences.GetNextSequence(...)` as `uint` calls β€” potentially 4 bytes -each (16-byte total payload) instead of holtburger's 12 bytes. We default to -holtburger's spec because it's been validated against live retail-format -servers, but we want one-shot evidence on real wire bytes before declaring -slice 1 done. - -The probe is gated on `ACDREAM_PROBE_BUILDING` (existing env var from -L.2d slice 1) and fires once per SetState message; the body bytes are -short enough that this is cheap. - -- [ ] **Step 5.1: Extend the dispatcher branch with a hex-dump** - -Update the `else if (op == SetState.Opcode)` branch from Task 3 to: - -```csharp - else if (op == SetState.Opcode) - { - // L.2g slice 1 (2026-05-12) β€” see Task 3 above for the - // event-routing intent. The probe-gated hex-dump here - // captures the wire bytes one-shot per session so we can - // confirm holtburger's 12-byte payload format (vs ACE's - // GameMessageSetState.cs claim of u32 sequences = 16 - // bytes) before declaring slice 1 done. - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeBuildingEnabled - && !_setStateHexDumped) - { - _setStateHexDumped = true; - var hex = string.Join(" ", body.Take(Math.Min(body.Length, 32)) - .Select(b => b.ToString("X2"))); - Console.WriteLine($"[setstate-hex] body.len={body.Length} first-{Math.Min(body.Length, 32)}-bytes: {hex}"); - } - - var parsed = SetState.TryParse(body); - if (parsed is not null) - StateUpdated?.Invoke(parsed.Value); - } -``` - -Add the one-shot flag field near the top of the `WorldSession` class -(group with other `_dump*Enabled` flags β€” grep `private bool _` to find -the cluster): - -```csharp - /// L.2g slice 1: one-shot guard so the [setstate-hex] probe - /// emits the first SetState's body bytes only, not 5–10/sec. - private bool _setStateHexDumped; -``` - -Note: the `body.Take(...)` requires `using System.Linq;` β€” already present. - -- [ ] **Step 5.2: Verify build still green** - -Run: `dotnet build` -Expected: Build succeeded, 0 errors. - -- [ ] **Step 5.3: Commit** - -``` -git add src/AcDream.Core.Net/WorldSession.cs -git commit -m "feat(phys L.2g slice 1): one-shot hex-dump probe for SetState payload - -Probe-gated diagnostic (ACDREAM_PROBE_BUILDING) emits the first inbound -SetState message's body bytes so we can confirm holtburger's 12-byte -payload format vs ACE's GameMessageSetState.cs claim of u32 sequences -(16-byte payload). One-shot via _setStateHexDumped β€” won't flood the -log when doors auto-close every 30s. - -If the hex-dump shows body.len > 16, the parser's body-length gate at -SetState.cs needs widening (and the seq-field reads shifted accordingly). -If it shows 16, we ship as-is. - -Co-Authored-By: Claude Opus 4.7 (1M context) " -``` - ---- - -## Task 6: Slice 0.5 β€” extend `[entity-source]` log with `state` + `flags` - -**Files:** -- Modify: `src/AcDream.App/Rendering/GameWindow.cs` (extend the existing `[entity-source]` log line β€” there are 2 sites; modify both for consistency) - -**Why:** The L.2d slice 1 handoff flagged this as a useful "slice 1.6" -addendum. It's 5 LOC, fold-into-slice-1-freebie. Makes ETHEREAL flips -greppable end-to-end: spawn -> registry update -> resolver effect. - -- [ ] **Step 6.1: Find both `[entity-source]` log sites** - -Grep `[entity-source]` in `src/AcDream.App/Rendering/GameWindow.cs` and -note the two `Console.WriteLine` calls (one is around line 2978 from the -RegisterLiveEntityForCollision path; the other should be in the -landblock-baked static registration path β€” grep confirms by file). Both -need the same suffix addition. - -- [ ] **Step 6.2: Extend both log lines** - -For each `[entity-source]` line, append `state=0x{state:X8} flags={flags}` -to the format string. Example transformation: - -Before: -```csharp -Console.WriteLine(System.FormattableString.Invariant( - $"[entity-source] id=0x{entity.Id:X8} entityId=0x{entity.Id:X8} src=0x{entity.SourceGfxObjOrSetupId:X8} gfxObj=0x{entity.SourceGfxObjOrSetupId:X8} lb=0x{spawn.Position.Value.LandblockId:X8} type=Cylinder note=server-spawn-root")); -``` - -After (note: the local variables `state` and `flags` should already be -in scope at both sites β€” they're computed just before the -`ShadowObjects.Register(...)` call; grep upward 5–10 lines from each log -site to confirm): - -```csharp -Console.WriteLine(System.FormattableString.Invariant( - $"[entity-source] id=0x{entity.Id:X8} entityId=0x{entity.Id:X8} src=0x{entity.SourceGfxObjOrSetupId:X8} gfxObj=0x{entity.SourceGfxObjOrSetupId:X8} lb=0x{spawn.Position.Value.LandblockId:X8} type=Cylinder note=server-spawn-root state=0x{state:X8} flags={flags}")); -``` - -If the `state` or `flags` variables are scoped differently at one site -(e.g. one site is for landblock-baked statics that always have state=0), -substitute the literal `0u` or `EntityCollisionFlags.None` and add a -comment noting the static-default. Keep the field names identical at both -sites so a single regex `state=0x([0-9A-F]+)` catches every entry. - -- [ ] **Step 6.3: Verify build still green** - -Run: `dotnet build` -Expected: Build succeeded, 0 errors. - -- [ ] **Step 6.4: Commit** - -``` -git add src/AcDream.App/Rendering/GameWindow.cs -git commit -m "feat(phys L.2g slice 1): extend [entity-source] log with state + flags - -5-LOC freebie folded into L.2g slice 1: the [entity-source] probe now -emits the PhysicsState bits + EntityCollisionFlags decoded at -registration. Combined with the new [setstate] handler log line, this -makes door open/close events fully greppable end-to-end: - spawn -> [entity-source] guid=... state=0x00000000 ... - Use -> [setstate] guid=... state=0x00000004 ... - close -> [setstate] guid=... state=0x00000000 ... - -Resolves the 'slice 1.6' suggestion from -docs/research/2026-05-13-l2d-slice1-shipped-handoff.md. - -Co-Authored-By: Claude Opus 4.7 (1M context) " -``` - ---- - -## Task 7: Visual verification at Holtburg inn doorway - -**Files:** None β€” this is a user-driven test. Document the recipe; report -results in the handoff doc (Task 8). - -**Acceptance:** -1. Walk acdream `+Acdream` into the Holtburg inn doorway. **Expected: blocked at threshold.** -2. Click the door (Use action). **Expected: door swings open; `[setstate]` log line emits with `state=0x00000004`; walk through clears.** -3. Wait ~30 seconds. **Expected: door auto-closes; `[setstate]` log line emits with `state=0x00000000`; threshold blocks again.** -4. Inspect the `[setstate-hex]` line emitted on the first SetState β€” confirm `body.len=16`. If it's 20 instead, slice 1 has a bug to file as 1b. - -- [ ] **Step 7.1: Launch the client with probes enabled** - -Wait ~5 seconds since the last close (per CLAUDE.md's logout-before-reconnect -note) then: - -```powershell -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -$env:ACDREAM_TEST_HOST = "127.0.0.1" -$env:ACDREAM_TEST_PORT = "9000" -$env:ACDREAM_TEST_USER = "testaccount" -$env:ACDREAM_TEST_PASS = "testpassword" -$env:ACDREAM_DEVTOOLS = "1" -$env:ACDREAM_PROBE_BUILDING = "1" -$env:ACDREAM_PROBE_RESOLVE = "1" -dotnet run --project src\AcDream.App\AcDream.App.csproj --no-build -c Debug 2>&1 | - Tee-Object -FilePath "launch-l2g-slice1.log" -``` - -- [ ] **Step 7.2: Manually perform the four-step scenario** - -(User-driven. See Acceptance list above.) - -- [ ] **Step 7.3: Inspect the log for the four expected lines** - -After closing the client window: - -```powershell -Select-String -Path launch-l2g-slice1.log -Pattern "setstate-hex|setstate.*guid|entity-source.*Door" -``` - -Expected matches: -- One `[setstate-hex] body.len=16 first-16-bytes: 4B F7 ...` line. -- One `[entity-source] ... name=Door ... state=0x00000000 ...` (or similar). -- A `[setstate] guid=0x000F.... state=0x00000004 ...` after the Use click. -- A `[setstate] guid=0x000F.... state=0x00000000 ...` ~30s after the previous. - -- [ ] **Step 7.4: Decide ship-or-fix** - -Three outcomes: -- **All four log lines match + door scenario works visually:** slice 1 ships. Proceed to Task 8. -- **Log lines correct but visual scenario fails (door visually opens but player still blocked):** the resolver is reading stale state from somewhere we haven't found. Stop and file a "slice 1b β€” find the second cache layer" note. -- **`[setstate-hex] body.len=20`:** ACE's u32 sequence claim is real. Widen `SetState.cs` body-length gate (`16` -> `20`) and shift sequence reads to `body.Slice(12, 4)` + `body.Slice(16, 4)` (read as `uint`, cast to `ushort` if values are small β€” high bits will be zero per ACE's `Sequences` design). Re-run from Task 7.1. - ---- - -## Task 8: Ship handoff doc + roadmap update - -**Files:** -- Create: `docs/research/2026-05-XX-l2g-slice1-shipped-handoff.md` (replace `XX` with the actual ship date) -- Modify: `CLAUDE.md` (replace the "the natural next step is the L.2g slice 1 implementation" paragraph with a "Phase L.2g slice 1 shipped " paragraph mirroring the L.2a paragraph style) -- Modify: `docs/plans/2026-04-29-movement-collision-conformance.md` (under the L.2g section, add a "Current shipped slice" subsection noting slice 1 + its commit hashes) - -- [ ] **Step 8.1: Write the ship handoff doc** - -Use the existing handoff at -`docs/research/2026-05-13-l2d-slice1-shipped-handoff.md` as a template. The -new doc should cover: - -- TL;DR: what landed, did the visual test pass. -- What shipped (commit hash + subject per commit from Tasks 1–6). -- What the visual test showed (the four log-line samples from Task 7.3). -- Wire-byte width resolution (12-byte vs 16-byte β€” whichever the hex-dump - showed). -- Side findings (anything noticed during visual test β€” door animation - flickers, audio not playing, etc β€” file under "deferred"). -- Next-session candidates (L.2g slice 2 animation confirmation, deferred UX - polish, OR pick from CLAUDE.md's now-revised "Next phase candidates" - list). - -- [ ] **Step 8.2: Update CLAUDE.md** - -Find the "Currently in Phase L.2 (Movement & Collision Conformance)" -paragraph. Replace its "the natural next step is the L.2g slice 1 -implementation" sentence with "L.2g slice 1 shipped β€” doors honor -ETHEREAL flips end-to-end; visual-verified at Holtburg inn doorway." Add -a "**Phase L.2g slice 1 shipped .**" descriptive paragraph after -the L.2a paragraph (mirror the L.2a paragraph's depth). - -In the "**Next phase candidates**" list, demote the current L.2g item -out and pick whichever is the next sensible candidate (likely L.2g slice 2 -animation confirmation OR a non-L.2 visual-fidelity item β€” depends on what -the visual test in Task 7 showed). - -- [ ] **Step 8.3: Update the L.2 plan-of-record** - -In `docs/plans/2026-04-29-movement-collision-conformance.md`, under the -L.2g section, add a "Current shipped slice ():" subsection -listing the slice 1 commit hashes + their subjects (use git log to fill -in). Mirror the L.2c "Current shipped slice (2026-04-30):" subsection -style. - -- [ ] **Step 8.4: Commit** - -``` -git add CLAUDE.md docs/plans/2026-04-29-movement-collision-conformance.md docs/research/2026-05-XX-l2g-slice1-shipped-handoff.md -git commit -m "docs(phys L.2g): slice 1 shipped handoff + plan-of-record + CLAUDE.md - -Slice 1 visual-verified at Holtburg inn doorway: walking into closed door -is blocked, Use opens it, walk-through clears, auto-close re-blocks at 30s. -Wire-byte width settled (see handoff doc). - -L.2g slice 2 (animation confirmation) becomes the next candidate IF the -visual test showed door animation not playing; otherwise slice 2 is a -verify-only no-op and we move to the next phase candidate. - -Co-Authored-By: Claude Opus 4.7 (1M context) " -``` - ---- - -## Plan self-review - -**1. Spec coverage check:** - -| Spec section | Task | -|---|---| -| Slice 1 β€” parse SetState (0xF74B) | Tasks 1 + 3 + 5 | -| Slice 1 β€” plumb new state into ShadowObjectRegistry | Tasks 2 + 4 | -| Slice 1 β€” visual verification at Holtburg | Task 7 | -| Slice 0.5 β€” extend [entity-source] log with state + flags | Task 6 | -| Open Q1 β€” wire-byte width | Task 5 (hex-dump probe) + Task 7.4 (decision branch) | -| Open Q2 β€” UpdateMotion drives non-creature entities (door swing animation) | **Deferred to slice 2** (per the spec β€” animation is verify-only) | -| Open Q3 β€” SetState delivered to the player who triggered Use | Task 7 visual test verifies (covered implicitly by the four-step scenario) | -| Acceptance β€” design spec, plan-of-record, milestones, CLAUDE.md all reference L.2g | Already done in `2c10dd4`; Task 8 closes the loop with the slice 1 ship handoff | -| Named retail citation in slice 1 code | Task 2.3 cites `acclient_2013_pseudo_c.txt:283044`; Task 1.3 cites the holtburger struct | - -**2. Placeholder scan:** No `TBD`, `TODO`, "fill in later." `` in -Task 8 is a deliberate placeholder for the engineer to fill in at ship -time β€” flagged as such in the handoff doc template, not a plan-writing -oversight. The `Task 8.1` doc filename uses `2026-05-XX` for the same -reason. - -**3. Type consistency:** `SetState.Parsed(Guid, PhysicsState, InstanceSequence, StateSequence)` -used consistently in Tasks 1, 3, 4, 5. `UpdatePhysicsState(uint entityId, uint newState)` -signature consistent in Tasks 2 + 4. `ShadowEntry.State` matches the -existing struct definition in `ShadowObjectRegistry.cs:262-280`. - -**4. Risk surface:** All changes are additive. No resolver edits. No -broadphase edits. No retail-port semantics changes. If anything goes -wrong, single-commit revert per task. - ---- - -## Execution - -Plan complete. Two execution options when ready: - -**1. Subagent-Driven (recommended)** β€” Dispatch a fresh Sonnet subagent per task (Task 1 alone, Tasks 2 + 3 together, Task 4 + 5 + 6 together, Task 7 user-driven, Task 8 docs). Review between dispatches. Each subagent stays bounded to one commit's worth of changes; parent context stays clean. - -**2. Inline Execution** β€” Drive all tasks in this session using executing-plans. Faster end-to-end but consumes ~4Γ— more parent context. - -Total scope estimate: ~6 commits over ~30–60 minutes of work + Task 7 visual test (~10 minutes when ACE + retail client are already running). diff --git a/docs/superpowers/plans/2026-05-13-phase-b4b-plan.md b/docs/superpowers/plans/2026-05-13-phase-b4b-plan.md deleted file mode 100644 index 8740d1e..0000000 --- a/docs/superpowers/plans/2026-05-13-phase-b4b-plan.md +++ /dev/null @@ -1,788 +0,0 @@ -# Phase B.4b β€” Outbound Use Handler Wiring Implementation Plan - -> **For agentic workers:** REQUIRED SUB-SKILL: Use `superpowers:subagent-driven-development` (recommended) or `superpowers:executing-plans` to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. - -**Goal:** Wire double-left-click and the R hotkey to a server `BuildUse` packet via a new `WorldPicker` so the M1 demo target *"open the inn door"* works and L.2g slice 1's deferred visual test verifies in the same scenario. - -**Architecture:** New static `AcDream.Core.Selection.WorldPicker` (pure `BuildRay` + `Pick` functions, no state); rename `_selectedTargetGuid` β†’ `_selectedGuid` on `GameWindow` (unify combat + interaction selection on one field); add three switch cases (`SelectLeft`, `SelectDblLeft`, `UseSelected`) to `GameWindow.OnInputAction` calling three private helpers (`PickAndStoreSelection`, `UseCurrentSelection`, `SendUse`). Spec: [`docs/superpowers/specs/2026-05-13-phase-b4b-design.md`](../specs/2026-05-13-phase-b4b-design.md). - -**Tech Stack:** C# .NET 10 Β· xUnit Β· Silk.NET Β· System.Numerics - ---- - -## File map - -| File | Op | Why | -|---|---|---| -| `src/AcDream.Core/Selection/WorldPicker.cs` | Create | Static helper with `BuildRay(mouseβ†’world ray)` + `Pick(rayβ†’entity guid)`. No state, no deps beyond `WorldEntity` + `System.Numerics`. | -| `tests/AcDream.Core.Tests/Selection/WorldPickerTests.cs` | Create | 8 xUnit `[Fact]`s covering BuildRay (center + offset) and Pick (hit/miss/closer/skip-guid/skip-zero/max-distance). | -| `src/AcDream.App/Rendering/GameWindow.cs` | Modify | Rename `_selectedTargetGuid` β†’ `_selectedGuid` (~5 sites). Add 3 switch cases + 3 helper methods. | - -No solution-file edits. New files land in existing projects (`AcDream.Core` for the picker, `AcDream.Core.Tests` for its tests; `AcDream.App` for the handler). - ---- - -## Task 1 β€” `WorldPicker.BuildRay` (TDD) - -**Files:** -- Create: `src/AcDream.Core/Selection/WorldPicker.cs` -- Create: `tests/AcDream.Core.Tests/Selection/WorldPickerTests.cs` - -- [ ] **Step 1: Write the failing tests for `BuildRay`** - -Create `tests/AcDream.Core.Tests/Selection/WorldPickerTests.cs` with: - -```csharp -using System; -using System.Numerics; -using AcDream.Core.Selection; -using Xunit; - -namespace AcDream.Core.Tests.Selection; - -public class WorldPickerTests -{ - private const float Epsilon = 0.01f; - - private static (Matrix4x4 View, Matrix4x4 Projection) MakeIdentityCamera() - { - var view = Matrix4x4.Identity; - var proj = Matrix4x4.CreatePerspectiveFieldOfView( - fieldOfView: MathF.PI / 3f, - aspectRatio: 16f / 9f, - nearPlaneDistance: 0.1f, - farPlaneDistance: 100f); - return (view, proj); - } - - [Fact] - public void BuildRay_CenterOfViewport_ReturnsForwardRay() - { - var (view, proj) = MakeIdentityCamera(); - const float vpW = 1920f, vpH = 1080f; - - var (_, direction) = WorldPicker.BuildRay( - mouseX: vpW / 2f, mouseY: vpH / 2f, - viewportW: vpW, viewportH: vpH, - view, proj); - - // Right-handed perspective + identity view -> camera looks down -Z. - // Center pixel ray = (0, 0, -1) within float epsilon. - Assert.True(MathF.Abs(direction.X) < Epsilon, $"direction.X = {direction.X}"); - Assert.True(MathF.Abs(direction.Y) < Epsilon, $"direction.Y = {direction.Y}"); - Assert.True(direction.Z < -0.99f, $"direction.Z = {direction.Z}"); - } - - [Fact] - public void BuildRay_OffsetMouseRight_DeflectsRayPositiveX() - { - var (view, proj) = MakeIdentityCamera(); - const float vpW = 1920f, vpH = 1080f; - - var (_, direction) = WorldPicker.BuildRay( - mouseX: vpW * 0.75f, mouseY: vpH / 2f, - viewportW: vpW, viewportH: vpH, - view, proj); - - Assert.True(direction.X > 0.1f, $"direction.X = {direction.X} (expected > 0.1)"); - } -} -``` - -- [ ] **Step 2: Run the tests, expect fail (class doesn't exist)** - -Run: `dotnet test tests/AcDream.Core.Tests/AcDream.Core.Tests.csproj --filter "FullyQualifiedName~WorldPickerTests"` - -Expected: build error `CS0246: The type or namespace name 'WorldPicker' could not be found` (or equivalent β€” `AcDream.Core.Selection` namespace doesn't exist yet). - -- [ ] **Step 3: Create `WorldPicker.cs` with `BuildRay`** - -Create `src/AcDream.Core/Selection/WorldPicker.cs`: - -```csharp -using System.Numerics; -using AcDream.Core.World; - -namespace AcDream.Core.Selection; - -/// -/// Mouse-to-entity picker. Pure static functions; no state, no DI. -/// -/// turns a pixel + view/projection into a world-space ray. -/// ray-sphere intersects against entity candidates and returns the nearest hit's ServerGuid. -/// -/// Used by GameWindow.OnInputAction to wire SelectLeft / SelectDblLeft / UseSelected to InteractRequests.BuildUse. -/// -public static class WorldPicker -{ - /// - /// Unprojects a pixel coordinate to a world-space ray using the supplied - /// view + projection matrices (System.Numerics row-vector convention, - /// composed as view * projection β€” same as the rest of acdream's camera - /// pipeline; see GameWindow.cs:6445 FrustumPlanes.FromViewProjection). - /// - /// - /// (origin = world point on the near plane, direction = normalized - /// world-space ray direction). Returns (Vector3.Zero, Vector3.Zero) - /// if the view-projection composition is singular. - /// - public static (Vector3 Origin, Vector3 Direction) BuildRay( - float mouseX, float mouseY, - float viewportW, float viewportH, - Matrix4x4 view, Matrix4x4 projection) - { - // Pixel -> NDC. y flipped: top-left pixel maps to ndc.y = +1. - float ndcX = (2f * mouseX) / viewportW - 1f; - float ndcY = 1f - (2f * mouseY) / viewportH; - - var vp = view * projection; - if (!Matrix4x4.Invert(vp, out var invVp)) - return (Vector3.Zero, Vector3.Zero); - - // Unproject near (ndc.z = -1) and far (ndc.z = +1) clip points. - var nearClip = new Vector4(ndcX, ndcY, -1f, 1f); - var farClip = new Vector4(ndcX, ndcY, +1f, 1f); - var n4 = Vector4.Transform(nearClip, invVp); - var f4 = Vector4.Transform(farClip, invVp); - if (n4.W == 0f || f4.W == 0f) - return (Vector3.Zero, Vector3.Zero); - - var nearWorld = new Vector3(n4.X, n4.Y, n4.Z) / n4.W; - var farWorld = new Vector3(f4.X, f4.Y, f4.Z) / f4.W; - var dir = farWorld - nearWorld; - if (dir.LengthSquared() < 1e-10f) - return (Vector3.Zero, Vector3.Zero); - return (nearWorld, Vector3.Normalize(dir)); - } -} -``` - -- [ ] **Step 4: Run the tests, expect pass** - -Run: `dotnet test tests/AcDream.Core.Tests/AcDream.Core.Tests.csproj --filter "FullyQualifiedName~WorldPickerTests"` - -Expected: `Passed: 2, Failed: 0`. Both `BuildRay_*` tests pass. - -- [ ] **Step 5: Commit** - -```bash -git add src/AcDream.Core/Selection/WorldPicker.cs tests/AcDream.Core.Tests/Selection/WorldPickerTests.cs -git commit -m "$(cat <<'EOF' -feat(B.4b): WorldPicker.BuildRay β€” mouse-to-world ray unprojection - -New AcDream.Core.Selection.WorldPicker static helper. BuildRay -unprojects pixel (mouseX, mouseY) through a view+projection matrix -pair into a world-space (origin, direction) ray. Used by -GameWindow.OnInputAction to drive entity picking on click. - -Pure math, no state, no DI. Composes view*projection (System.Numerics -row-vector convention, matching the rest of acdream's camera path β€” -see GameWindow.cs:6445 FrustumPlanes.FromViewProjection). 2 xUnit -tests cover center-of-viewport (forward ray) and right-of-center -(positive-X deflection). - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - ---- - -## Task 2 β€” `WorldPicker.Pick` (TDD) - -**Files:** -- Modify: `src/AcDream.Core/Selection/WorldPicker.cs` -- Modify: `tests/AcDream.Core.Tests/Selection/WorldPickerTests.cs` - -- [ ] **Step 1: Write the failing tests for `Pick`** - -Append to `tests/AcDream.Core.Tests/Selection/WorldPickerTests.cs` (inside the same class, before the closing `}`): - -```csharp - private static WorldEntity MakeEntity(uint serverGuid, Vector3 position) => new() - { - Id = serverGuid == 0u ? 1u : serverGuid, - ServerGuid = serverGuid, - SourceGfxObjOrSetupId = 0u, - Position = position, - Rotation = Quaternion.Identity, - MeshRefs = Array.Empty(), - }; - - [Fact] - public void Pick_RayThroughEntity_ReturnsServerGuid() - { - var entity = MakeEntity(0xABCDu, new Vector3(0, 0, -10)); - - var result = WorldPicker.Pick( - origin: Vector3.Zero, - direction: -Vector3.UnitZ, - candidates: new[] { entity }, - skipServerGuid: 0u); - - Assert.Equal(0xABCDu, result); - } - - [Fact] - public void Pick_RayMisses_ReturnsNull() - { - var entity = MakeEntity(0xABCDu, new Vector3(0, 0, -10)); - - var result = WorldPicker.Pick( - origin: Vector3.Zero, - direction: Vector3.UnitX, - candidates: new[] { entity }, - skipServerGuid: 0u); - - Assert.Null(result); - } - - [Fact] - public void Pick_TwoEntitiesInLine_ReturnsCloser() - { - var near = MakeEntity(0x1111u, new Vector3(0, 0, -5)); - var far = MakeEntity(0x2222u, new Vector3(0, 0, -20)); - - var result = WorldPicker.Pick( - origin: Vector3.Zero, - direction: -Vector3.UnitZ, - candidates: new[] { far, near }, // iteration order shouldn't matter - skipServerGuid: 0u); - - Assert.Equal(0x1111u, result); - } - - [Fact] - public void Pick_SkipsSkipGuid() - { - var entity = MakeEntity(0xABCDu, new Vector3(0, 0, -10)); - - var result = WorldPicker.Pick( - origin: Vector3.Zero, - direction: -Vector3.UnitZ, - candidates: new[] { entity }, - skipServerGuid: 0xABCDu); - - Assert.Null(result); - } - - [Fact] - public void Pick_SkipsZeroServerGuid() - { - // Atlas-tier scenery / dat-hydrated statics carry ServerGuid=0 - // and aren't valid Use targets β€” server would reject guid=0. - var entity = MakeEntity(0u, new Vector3(0, 0, -10)); - - var result = WorldPicker.Pick( - origin: Vector3.Zero, - direction: -Vector3.UnitZ, - candidates: new[] { entity }, - skipServerGuid: 0xDEADu); - - Assert.Null(result); - } - - [Fact] - public void Pick_BeyondMaxDistance_ReturnsNull() - { - var entity = MakeEntity(0xABCDu, new Vector3(0, 0, -100)); - - var result = WorldPicker.Pick( - origin: Vector3.Zero, - direction: -Vector3.UnitZ, - candidates: new[] { entity }, - skipServerGuid: 0u); // default maxDistance = 50f - - Assert.Null(result); - } -``` - -Also add `using AcDream.Core.World;` to the top of `WorldPickerTests.cs` (next to the existing `using AcDream.Core.Selection;`). - -- [ ] **Step 2: Run the tests, expect fail (Pick doesn't exist)** - -Run: `dotnet test tests/AcDream.Core.Tests/AcDream.Core.Tests.csproj --filter "FullyQualifiedName~WorldPickerTests"` - -Expected: build error `CS0117: 'WorldPicker' does not contain a definition for 'Pick'`. - -- [ ] **Step 3: Add `Pick` to `WorldPicker.cs`** - -Open `src/AcDream.Core/Selection/WorldPicker.cs`, add `using System;` and `using System.Collections.Generic;` to the imports, and append this method inside the `WorldPicker` class (after `BuildRay`): - -```csharp - /// - /// Ray-sphere intersection against each candidate's - /// using a fixed 5m sphere radius. Returns the - /// of the closest hit within , or null on miss. - /// - /// - /// Entities with ServerGuid == 0 (atlas-tier scenery, dat-hydrated - /// statics) are skipped β€” they have no server-side identity and can't be - /// the target of a Use packet. The player's own guid is skipped via - /// . - /// - public static uint? Pick( - Vector3 origin, Vector3 direction, - IEnumerable candidates, - uint skipServerGuid, - float maxDistance = 50f) - { - const float Radius = 5f; - const float Radius2 = Radius * Radius; - - if (direction.LengthSquared() < 1e-10f) return null; - - uint? bestGuid = null; - float bestT = float.PositiveInfinity; - foreach (var entity in candidates) - { - if (entity.ServerGuid == 0u) continue; - if (entity.ServerGuid == skipServerGuid) continue; - - // Geometric ray-sphere: oc = origin - center, b = dot(oc, dir), - // c = |oc|^2 - r^2, discriminant = b^2 - c. If discriminant < 0 - // the ray misses the sphere. Otherwise nearest intersection is - // t = -b - sqrt(discriminant). - var oc = origin - entity.Position; - float b = Vector3.Dot(oc, direction); - float c = Vector3.Dot(oc, oc) - Radius2; - float d = b * b - c; - if (d < 0f) continue; - - float t = -b - MathF.Sqrt(d); - if (t < 0f) continue; // ray points away or origin inside - if (t >= maxDistance) continue; - if (t < bestT) - { - bestT = t; - bestGuid = entity.ServerGuid; - } - } - return bestGuid; - } -``` - -- [ ] **Step 4: Run the tests, expect pass** - -Run: `dotnet test tests/AcDream.Core.Tests/AcDream.Core.Tests.csproj --filter "FullyQualifiedName~WorldPickerTests"` - -Expected: `Passed: 8, Failed: 0`. All 8 `WorldPicker*` tests pass. - -- [ ] **Step 5: Commit** - -```bash -git add src/AcDream.Core/Selection/WorldPicker.cs tests/AcDream.Core.Tests/Selection/WorldPickerTests.cs -git commit -m "$(cat <<'EOF' -feat(B.4b): WorldPicker.Pick β€” ray-sphere entity pick - -Adds Pick(origin, direction, candidates, skipServerGuid, maxDistance) -to AcDream.Core.Selection.WorldPicker. Iterates candidates, skips -entities with ServerGuid==0 (atlas/dat-hydrated statics β€” no server -identity) and the caller's skipServerGuid (the player self). -Geometric ray-sphere intersection at 5m radius (matches -WorldEntity.DefaultAabbRadius). Returns the nearest hit's ServerGuid -within maxDistance (50m default), or null on miss. - -6 xUnit tests added: hit, miss, two-in-line-returns-closer, skip-guid, -skip-zero-server-guid, beyond-max-distance. - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - ---- - -## Task 3 β€” Rename `_selectedTargetGuid` β†’ `_selectedGuid` - -**Files:** -- Modify: `src/AcDream.App/Rendering/GameWindow.cs` - -Refactor only β€” no behavior change. Unifies combat (Q-cycle) and interaction (B.4b click) selection on one field. Retail-faithful: AC has one "current target," not two. - -- [ ] **Step 1: Locate every reference** - -Run (Grep tool): -``` -pattern: _selectedTargetGuid -path: src/AcDream.App/Rendering/GameWindow.cs -output: content with -n -``` - -Expected: ~5 hits, all inside `GameWindow.cs`. Then verify there are no references elsewhere: - -``` -pattern: _selectedTargetGuid -path: src -output: files_with_matches -``` - -Expected: only `GameWindow.cs` matches. - -- [ ] **Step 2: Replace via the Edit tool (replace_all)** - -Edit `src/AcDream.App/Rendering/GameWindow.cs` with `replace_all: true`: -- `old_string: _selectedTargetGuid` -- `new_string: _selectedGuid` - -- [ ] **Step 3: Build green** - -Run: `dotnet build -c Debug` - -Expected: build succeeds with no new errors or warnings tied to the rename. - -- [ ] **Step 4: Tests green** - -Run: `dotnet test` - -Expected: 8 new `WorldPickerTests` pass on top of the prior baseline. The L.2g slice 1 handoff reported "1037 pass / 8 pre-existing-baseline fail." With +8 from Tasks 1+2, expect **1045 pass / 8 pre-existing fail**. - -- [ ] **Step 5: Commit** - -```bash -git add src/AcDream.App/Rendering/GameWindow.cs -git commit -m "$(cat <<'EOF' -refactor(B.4b): unify _selectedTargetGuid -> _selectedGuid - -Retail's selection model is a single "current target" used by combat, -interaction, NPC dialog, and HUD alike β€” not two parallel selections. -Renames the existing combat-only field on GameWindow so the upcoming -B.4b click handler and the existing Q-cycle SelectClosestCombatTarget -share the same selection state. - -Mechanical rename, no behavior change. Build + tests green. - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - ---- - -## Task 4 β€” Wire `OnInputAction` handlers - -**Files:** -- Modify: `src/AcDream.App/Rendering/GameWindow.cs` - -Add three private helper methods + three switch cases. Switch-case behavior is verified at runtime (Task 5 visual test); helpers depend on `GameWindow` state and aren't unit-tested. - -- [ ] **Step 1: Add the three helper methods** - -Insert these three methods immediately above `SelectClosestCombatTarget` (around line 8706 β€” keep the selection-related helpers grouped). Use the `Edit` tool anchored on the line `private uint? SelectClosestCombatTarget(bool showToast)`: - -`old_string`: -``` - private uint? SelectClosestCombatTarget(bool showToast) -``` - -`new_string`: -``` - // ============================================================ - // Phase B.4b β€” outbound Use handler. Wires three input actions - // (LMB click select, LMB-double-click select+use, R hotkey - // use-selected) through WorldPicker into InteractRequests.BuildUse. - // The inbound reply (SetState 0xF74B) lands via L.2g slice 1. - // ============================================================ - - private void PickAndStoreSelection(bool useImmediately) - { - if (_cameraController is null || _window is null) return; - - var camera = _cameraController.Active; - var (origin, direction) = AcDream.Core.Selection.WorldPicker.BuildRay( - mouseX: _lastMouseX, mouseY: _lastMouseY, - viewportW: _window.Size.X, viewportH: _window.Size.Y, - view: camera.View, projection: camera.Projection); - - if (direction.LengthSquared() < 1e-6f) return; // degenerate ray - - var picked = AcDream.Core.Selection.WorldPicker.Pick( - origin, direction, - _entitiesByServerGuid.Values, - skipServerGuid: _playerServerGuid, - maxDistance: 50f); - - if (picked is uint guid) - { - _selectedGuid = guid; - string label = DescribeLiveEntity(guid); - Console.WriteLine($"[B.4b] pick guid=0x{guid:X8} name={label}"); - _debugVm?.AddToast($"Selected: {label}"); - if (useImmediately) SendUse(guid); - } - else - { - _debugVm?.AddToast("Nothing to select"); - } - } - - private void UseCurrentSelection() - { - if (_selectedGuid is uint sel) - SendUse(sel); - else - _debugVm?.AddToast("Nothing selected"); - } - - private void SendUse(uint guid) - { - if (_liveSession is null - || _liveSession.CurrentState != AcDream.Core.Net.WorldSession.State.InWorld) - { - _debugVm?.AddToast("Not in world"); - return; - } - var seq = _liveSession.NextGameActionSequence(); - var body = AcDream.Core.Net.Messages.InteractRequests.BuildUse(seq, guid); - _liveSession.SendGameAction(body); - Console.WriteLine($"[B.4b] use guid=0x{guid:X8} seq={seq}"); - } - - private uint? SelectClosestCombatTarget(bool showToast) -``` - -(The `Edit` replaces the single anchor line with the three new helpers + the same anchor line at the end, leaving `SelectClosestCombatTarget`'s body untouched.) - -- [ ] **Step 2: Add the three switch cases** - -In `GameWindow.OnInputAction`'s switch (currently `GameWindow.cs:8546-8646`), add three new `case` blocks immediately before the `case AcDream.UI.Abstractions.Input.InputAction.EscapeKey:` branch. - -Use the `Edit` tool anchored on: - -`old_string`: -``` - case AcDream.UI.Abstractions.Input.InputAction.EscapeKey: - if (_cameraController?.IsFlyMode == true) -``` - -`new_string`: -``` - case AcDream.UI.Abstractions.Input.InputAction.SelectLeft: - PickAndStoreSelection(useImmediately: false); - break; - - case AcDream.UI.Abstractions.Input.InputAction.SelectDblLeft: - PickAndStoreSelection(useImmediately: true); - break; - - case AcDream.UI.Abstractions.Input.InputAction.UseSelected: - UseCurrentSelection(); - break; - - case AcDream.UI.Abstractions.Input.InputAction.EscapeKey: - if (_cameraController?.IsFlyMode == true) -``` - -- [ ] **Step 3: Build green** - -Run: `dotnet build -c Debug` - -Expected: build succeeds with no new errors. Any new warnings should be tied only to the additions. - -- [ ] **Step 4: Tests green** - -Run: `dotnet test` - -Expected: same **1045 pass / 8 pre-existing-baseline fail** from Task 3. - -- [ ] **Step 5: Commit** - -```bash -git add src/AcDream.App/Rendering/GameWindow.cs -git commit -m "$(cat <<'EOF' -feat(B.4b): GameWindow wires Select/Use handlers via WorldPicker - -Closes #57. Adds three OnInputAction switch cases (SelectLeft, -SelectDblLeft, UseSelected) and three private helpers -(PickAndStoreSelection, UseCurrentSelection, SendUse). Single-click -selects but does not Use; double-click selects + Uses; R hotkey -sends Use on the existing _selectedGuid. ImGui mouse-capture -filtering already happens in InputDispatcher β€” no new guard needed. - -Diagnostic lines emitted for log grep: - [B.4b] pick guid=0x{guid:X8} name={label} - [B.4b] use guid=0x{guid:X8} seq={seq} - -Build green; tests 1045/1053 (8 pre-existing-baseline fails -unchanged). Switch-case behavior verified at runtime via the Holtburg -inn doorway visual test (per spec Β§Testing β†’ Runtime verification). - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - ---- - -## Task 5 β€” Visual verification at Holtburg inn doorway - -**This task is performed by the user.** The implementing agent runs the launch command (background) and reports completion; the user observes the running client and reports the result. - -- [ ] **Step 1: Kill any stale client process** - -Run via Bash tool: -```powershell -Get-Process -Name AcDream.App -ErrorAction SilentlyContinue | Stop-Process -Force -Start-Sleep -Seconds 3 -``` - -- [ ] **Step 2: Launch the client with B.4b + L.2g probes enabled** - -Run via Bash tool with `run_in_background: true`: -```powershell -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -$env:ACDREAM_TEST_HOST = "127.0.0.1" -$env:ACDREAM_TEST_PORT = "9000" -$env:ACDREAM_TEST_USER = "testaccount" -$env:ACDREAM_TEST_PASS = "testpassword" -$env:ACDREAM_DEVTOOLS = "1" -$env:ACDREAM_PROBE_BUILDING = "1" -$env:ACDREAM_PROBE_RESOLVE = "1" -dotnet run --project src\AcDream.App\AcDream.App.csproj -c Debug 2>&1 | - Tee-Object -FilePath "launch-b4b.log" -``` - -- [ ] **Step 3: User performs the scenario** - -In the running client: -1. Wait ~8s for the player to spawn at Holtburg. -2. Walk to the inn doorway (north side of the south building). -3. Double-left-click the closed door. -4. Observe: swing animation should play. -5. Walk forward through the open doorway. -6. Wait ~30s in the inn. -7. Observe: auto-close animation should fire. -8. Close the client window. - -- [ ] **Step 4: Grep the log** - -```powershell -Select-String -Path launch-b4b.log -Pattern ` - "B\.4b|setstate-hex|\[setstate\]|input.*SelectDblLeft|entity-source.*Door" -``` - -Expected matches (approximate order): -- `[entity-source] name=Door ... state=0x00000000 flags=None ...` (door spawn at world load) -- `[input] SelectDblLeft Press` (dispatcher fires on the user's click) -- `[B.4b] pick guid=0x000F???? name=Door` (picker hit) -- `[B.4b] use guid=0x000F???? seq=N` (outbound Use fires) -- `[setstate-hex] body.len=16 ...` (L.2g hex probe β€” first SetState body) -- `[setstate] guid=0x000F???? state=0x00000014` (or `0x00000004`) (door opens) -- `[setstate] guid=0x000F???? state=0x00000000` ~30s later (auto-close) - -- [ ] **Step 5: Decide on follow-up based on the observed state value** - -- If the state bits include `0x10` (so the value is `0x14` or higher), `CollisionExemption.ShouldSkip` short-circuits as designed β€” no follow-up needed. -- If the state is `0x4` (ETHEREAL only, no IGNORE_COLLISIONS), file a tiny **L.2g slice 1b** to widen the check. The fix is a one-line edit to `src/AcDream.Core/Physics/CollisionExemption.cs`. **Out of B.4b scope** β€” record the finding and move on. - ---- - -## Task 6 β€” Ship handoff + post-merge updates - -**Files (all modified or created):** -- Create: `docs/research/2026-05-13-b4b-shipped-handoff.md` -- Modify: `docs/ISSUES.md` (close #57) -- Modify: `docs/plans/2026-04-11-roadmap.md` (add B.4b row to "shipped" table) -- Modify: `CLAUDE.md` ("Currently in Phase L.2" paragraph) -- Modify (outside-repo): `C:\Users\erikn\.claude\projects\C--Users-erikn-source-repos-acdream\memory\project_interaction_pipeline.md` - -- [ ] **Step 1: Write the ship-handoff doc** - -Create `docs/research/2026-05-13-b4b-shipped-handoff.md` summarizing: -- 4 commits (BuildRay, Pick, rename, handler wiring) -- The actual `state=0x??` value observed in Task 5 step 4 -- Whether L.2g slice 1b is needed (decided in Task 5 step 5) -- Whether picker tuning is needed (5m radius too generous/strict) - -Use the same structure as `docs/research/2026-05-12-l2g-slice1-shipped-handoff.md` β€” TL;DR + commit table + end-to-end flow + open notes + reproducibility. - -- [ ] **Step 2: Move #57 from Active to Recently Closed in `docs/ISSUES.md`** - -Edit `docs/ISSUES.md`: -- Cut the `## #57 β€” B.4 interaction-handler missing` block from "Active issues". -- Paste it under "Recently closed" with header changed to `## #57 β€” [DONE 2026-05-13] B.4 interaction-handler missing: clicking on doors / NPCs / items silently does nothing` and add a **Closed:** line with this PR's merge commit SHA. - -- [ ] **Step 3: Update the roadmap's shipped table** - -Edit `docs/plans/2026-04-11-roadmap.md`. Add a new row to the "shipped" table (preserving existing column structure): - -``` -| 2026-05-13 | Phase B.4b β€” Outbound Use handler wiring | | Closes #57. WorldPicker + 3 switch cases. M1 demo target "open the inn door" verified at Holtburg. | -``` - -- [ ] **Step 4: Update `CLAUDE.md` "Currently in Phase L.2" paragraph** - -Edit `CLAUDE.md`: -- Change the "L.2g slice 1 is CODE-COMPLETE..." paragraph to "L.2g slice 1 + B.4b shipped and visual-verified 2026-05-13 at the Holtburg inn doorway." -- Remove the "natural next step is Phase B.4b" paragraph; replace with the next phase candidate from the existing candidate list (the user picks order; in absence of new evidence, the **Triage open issues** option is the natural follow-up). - -- [ ] **Step 5: Update the memory file** - -Edit `C:\Users\erikn\.claude\projects\C--Users-erikn-source-repos-acdream\memory\project_interaction_pipeline.md`: -- Mark Phase B.4 outbound-handler gap as closed by B.4b (2026-05-13). -- Add the new flow: LMB-dblclick β†’ WorldPicker β†’ BuildUse β†’ SendGameAction. -- Update the `WorldPicker` and `SelectionState` claims: - - `WorldPicker` now exists in `AcDream.Core.Selection`. - - `SelectionState` still doesn't exist β€” deferred to M2 HUD work. - -- [ ] **Step 6: Commit the in-repo docs** - -```bash -git add docs/research/2026-05-13-b4b-shipped-handoff.md docs/ISSUES.md docs/plans/2026-04-11-roadmap.md CLAUDE.md -git commit -m "$(cat <<'EOF' -docs(B.4b): ship handoff + close #57 + roadmap/CLAUDE update - -L.2g slice 1 + B.4b verified at Holtburg inn doorway: -- Player double-clicks closed door -- BuildUse fires, ACE responds with SetState 0xF74B -- ShadowObjectRegistry mutates ETHEREAL bit -- CollisionExemption short-circuits, player walks through -- 30s auto-close fires on schedule - -Closes #57. Updates roadmap shipped table and CLAUDE.md Phase L.2 -paragraph. Memory file project_interaction_pipeline.md updated outside -the repo. - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - -(The memory file lives outside the repo and isn't tracked by git β€” update it but don't include it in the commit.) - -- [ ] **Step 7: Merge to main** - -```bash -git checkout main -git merge --no-ff claude/compassionate-wilson-23ff99 -m "Merge branch 'claude/compassionate-wilson-23ff99' β€” Phase B.4b + L.2g slice 1 visual-verified" -``` - -Do NOT push without explicit user authorization (CLAUDE.md rule). - ---- - -## Self-review against the spec - -| Spec section | Plan task(s) | Coverage | -|---|---|---| -| Β§Architecture: `WorldPicker.cs` in `AcDream.Core.Selection` | Tasks 1, 2 | covered | -| Β§Architecture: rename `_selectedTargetGuid` | Task 3 | covered | -| Β§Architecture: 3 switch cases + 3 helpers | Task 4 | covered | -| Β§Components: `BuildRay` signature + math | Task 1 step 3 | covered | -| Β§Components: `Pick` signature + ServerGuid==0 skip | Task 2 step 3 | covered | -| Β§Components: `PickAndStoreSelection` toast + `[B.4b] pick` log | Task 4 step 1 | covered | -| Β§Components: `SendUse` gate + `[B.4b] use` log | Task 4 step 1 | covered | -| Β§Components: `UseCurrentSelection` | Task 4 step 1 | covered | -| Β§Components: 3 switch cases | Task 4 step 2 | covered | -| Β§Testing: 8 unit tests | Tasks 1+2 | covered (2 BuildRay + 6 Pick) | -| Β§Testing: runtime verification at Holtburg | Task 5 | covered | -| Β§Testing: log grep + state-value decision | Task 5 step 4-5 | covered | -| Β§Acceptance: build + tests green | Tasks 3+4 steps 3-4 | covered | -| Β§Acceptance: ISSUES.md #57 β†’ Recently closed | Task 6 step 2 | covered | -| Β§Acceptance: roadmap update | Task 6 step 3 | covered | -| Β§Acceptance: CLAUDE.md update | Task 6 step 4 | covered | -| Β§Open question: state 0x4 vs 0x14 follow-up | Task 5 step 5 | covered (deferred to L.2g slice 1b if needed) | -| Β§Non-goals: BuildPickUp / UseWithTarget UX / SelectionState class | (none β€” explicitly deferred) | covered by omission | - -No placeholders. No "TBD." Every code step has the actual code; every command step has the exact command and the expected output. Type names match across tasks (`WorldPicker.BuildRay` / `WorldPicker.Pick`, `_selectedGuid`, `PickAndStoreSelection` / `UseCurrentSelection` / `SendUse`). diff --git a/docs/superpowers/plans/2026-05-13-phase-b4c-plan.md b/docs/superpowers/plans/2026-05-13-phase-b4c-plan.md deleted file mode 100644 index 0e945d1..0000000 --- a/docs/superpowers/plans/2026-05-13-phase-b4c-plan.md +++ /dev/null @@ -1,444 +0,0 @@ -# Phase B.4c β€” Door Swing Animation Implementation Plan - -> **For agentic workers:** REQUIRED SUB-SKILL: Use `superpowers:subagent-driven-development` (recommended) or `superpowers:executing-plans` to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. - -**Goal:** Make Holtburg's inn doors visibly swing open / closed when the player Uses them. Closes #58 and completes the M1 demo target *"open the inn door"* with full visual feedback. - -**Architecture:** One block edit in `GameWindow.cs` adds a Door-specific spawn-time branch alongside the existing creature gate at line 2692. Detect Door entities by `spawn.Name == "Door"`. For each, build the same `AnimationSequencer` as creatures (load `MotionTable` from dats, construct sequencer) and immediately seed it with the `Off` cycle (closed) or `On` cycle (already open) based on the spawn's `PhysicsState` ETHEREAL bit. The existing `OnLiveMotionUpdated` handler then routes naturally β€” no downstream changes. Adds one diagnostic line in the UM handler for greppable verification. Spec: [`docs/superpowers/specs/2026-05-13-phase-b4c-design.md`](../specs/2026-05-13-phase-b4c-design.md). - -**Tech Stack:** C# .NET 10 Β· existing `AnimationSequencer` + per-frame tick + WB renderer Β· no new dependencies. - ---- - -## File map - -| File | Op | Why | -|---|---|---| -| `src/AcDream.App/Rendering/GameWindow.cs` | Modify | Add `IsDoorSpawn` static helper + Door registration branch (after the existing `idleCycle` gate at line 2692) + `[door-cycle]` diagnostic in `OnLiveMotionUpdated`. | - -No new files. No unit tests added β€” GameWindow integration code is runtime-verified per the project's existing precedent (B.4b's switch cases, L.2g's MotionUpdated routing). - ---- - -## Task 1 β€” Add `IsDoorSpawn` helper + Door registration branch - -**Files:** -- Modify: `src/AcDream.App/Rendering/GameWindow.cs` - -This task adds two things in one commit: the static helper that detects Door entities, and the new `else if` branch in the live-spawn handler that registers them with a seeded `AnimationSequencer`. - -- [ ] **Step 1: Add the `IsDoorSpawn` helper** - -Insert this static helper immediately above the live-spawn handler. Use the `Edit` tool with this exact anchor: - -`old_string`: -``` - private void OnLiveEntitySpawnedLocked(AcDream.Core.Net.WorldSession.EntitySpawn spawn) -``` - -`new_string`: -``` - /// - /// Phase B.4c β€” door detection by server-sent name. Doors fail the - /// generic multi-frame-idle gate at line 2692 (no idle cycle), so we - /// register them via a sibling branch with a state-seeded sequencer. - /// - private static bool IsDoorSpawn(AcDream.Core.Net.WorldSession.EntitySpawn spawn) - => spawn.Name == "Door"; - - private void OnLiveEntitySpawnedLocked(AcDream.Core.Net.WorldSession.EntitySpawn spawn) -``` - -- [ ] **Step 2: Add the Door registration branch** - -Insert the new `else if` branch immediately after the existing `idleCycle` gate's closing brace (around line 2800). The anchor is the comment line that follows the gate. Use the `Edit` tool: - -`old_string`: -``` - } - - // Dump a summary periodically so we can see drop breakdowns without - // waiting for a graceful shutdown. - if (_liveSpawnReceived % 20 == 0) -``` - -`new_string`: -``` - } - else if (IsDoorSpawn(spawn) && _animLoader is not null) - { - // Phase B.4c β€” Door swing animation. Doors fail the - // multi-frame-idle gate above (no idle cycle) but DO have a - // MotionTable with On/Off cycles that ACE drives via - // UpdateMotion. Register with a seeded sequencer so the - // per-frame tick has frames to advance from frame 1 (without - // the seed, Sequencer.Advance(dt) returns no frames and the - // MeshRefs rebuild at line 7691 collapses the door to origin). - // - // Initial cycle mirrors ACE's Door.cs:43 - // (CurrentMotionState = motionClosed): Off when the door is - // closed at spawn, On when the spawn PhysicsState carries the - // ETHEREAL bit (door was already open in ACE's DB). - uint mtableId = spawn.MotionTableId ?? (uint)setup.DefaultMotionTable; - if (mtableId != 0) - { - var mtable = _dats.Get(mtableId); - if (mtable is not null) - { - var sequencer = new AcDream.Core.Physics.AnimationSequencer(setup, mtable, _animLoader); - - const uint NonCombatStance = 0x80000001u; - const uint MotionOn = 0x4000000Bu; // ACE MotionCommand.On (door open) - const uint MotionOff = 0x4000000Cu; // ACE MotionCommand.Off (door closed) - const uint EtherealPs = 0x4u; - uint spawnState = spawn.PhysicsState ?? 0u; - uint initialCycle = (spawnState & EtherealPs) != 0 ? MotionOn : MotionOff; - if (sequencer.HasCycle(NonCombatStance, initialCycle)) - sequencer.SetCycle(NonCombatStance, initialCycle); - - var template = new (uint, IReadOnlyDictionary?)[meshRefs.Count]; - for (int i = 0; i < meshRefs.Count; i++) - template[i] = (meshRefs[i].GfxObjId, meshRefs[i].SurfaceOverrides); - - _animatedEntities[entity.Id] = new AnimatedEntity - { - Entity = entity, - Setup = setup, - Animation = null, // sequencer-driven; tick reads sequencer state - LowFrame = 0, - HighFrame = 0, - Framerate = 0f, - Scale = scale, - PartTemplate = template, - CurrFrame = 0, - Sequencer = sequencer, - }; - - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeBuildingEnabled) - Console.WriteLine(System.FormattableString.Invariant( - $"[door-anim] registered guid=0x{spawn.Guid:X8} entityId=0x{entity.Id:X8} mtable=0x{mtableId:X8} initialCycle=0x{initialCycle:X8}")); - } - } - } - - // Dump a summary periodically so we can see drop breakdowns without - // waiting for a graceful shutdown. - if (_liveSpawnReceived % 20 == 0) -``` - -- [ ] **Step 3: Build green** - -Run: `dotnet build src/AcDream.App/AcDream.App.csproj -c Debug` - -Expected: build succeeds, 0 errors. Any new warnings should be tied to the additions only. - -If a name like `meshRefs`, `entity`, `setup`, or `scale` doesn't resolve in scope at the insertion point, STOP and report β€” these are variables that exist in the surrounding scope at line 2800 of `OnLiveEntitySpawnedLocked` (verified during spec authoring at line 2700-2784 reads). They should be in scope; if Edit landed in the wrong place, fix the anchor first. - -- [ ] **Step 4: Tests green** - -Run: `dotnet test` - -Expected: **1046 pass / 8 pre-existing-baseline fail** (same as main HEAD `3e08e10`). Any regression here means the new branch is touching unrelated code paths β€” investigate. - -- [ ] **Step 5: Commit** - -```bash -git add src/AcDream.App/Rendering/GameWindow.cs -git commit -m "$(cat <<'EOF' -feat(B.4c): door spawn-time AnimationSequencer with state-seeded initial cycle - -Adds IsDoorSpawn helper and a sibling branch to the live-spawn -handler's animation registration gate. Detects entities where -spawn.Name == "Door" and registers them in _animatedEntities with an -AnimationSequencer seeded from the spawn PhysicsState's ETHEREAL bit -(Off cycle if closed, On if already open). - -Mirrors ACE Door.cs:43 (CurrentMotionState = motionClosed) so the -sequencer always has frames for the per-frame tick to advance from -the first render. Without the seed, Advance(dt) returns no frames and -the MeshRefs rebuild at line 7691 collapses the door to origin. - -No changes to OnLiveMotionUpdated, AnimationSequencer, EntitySpawnAdapter, -or the per-frame tick. The tick's sequencer branch at line 7497 reads -ae.Sequencer.Advance(dt) and never touches ae.Animation in this path -(only the legacy slerp else branch at line 7644+ does). - -[door-anim] registered diagnostic gated on ACDREAM_PROBE_BUILDING. - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - ---- - -## Task 2 β€” Add `[door-cycle]` UM dispatch diagnostic - -**Files:** -- Modify: `src/AcDream.App/Rendering/GameWindow.cs` - -Adds one diagnostic line in `OnLiveMotionUpdated` that fires whenever an `UpdateMotion` arrives for an entity named "Door". Greppable trail for verification of the open/close cycle dispatch. - -- [ ] **Step 1: Locate the diagnostic insertion point** - -Run (Grep tool): -``` -pattern: ACDREAM_DUMP_MOTION.*== "1" -path: src/AcDream.App/Rendering/GameWindow.cs -output: content with -n -``` - -Expected: one match around line 3075 in the `OnLiveMotionUpdated` body. The `[door-cycle]` diagnostic goes immediately after this `ACDREAM_DUMP_MOTION` block so both diagnostics are grouped. - -- [ ] **Step 2: Add the `[door-cycle]` diagnostic** - -Use the `Edit` tool. The anchor is the closing brace + blank line + the next code section ("Wire server-echoed RunRate") which follows the `ACDREAM_DUMP_MOTION` block at line 3075-3087: - -`old_string`: -``` - $"UM guid=0x{update.Guid:X8} mt=0x{update.MotionState.MovementType:X2} stance=0x{stance:X4} cmd={cmdStr} spd={spd:F2} " + - $"| seq now style=0x{seqStyle:X8} motion=0x{seqMotion:X8}"); - } - - // Wire server-echoed RunRate first β€” used for the player's own -``` - -`new_string`: -``` - $"UM guid=0x{update.Guid:X8} mt=0x{update.MotionState.MovementType:X2} stance=0x{stance:X4} cmd={cmdStr} spd={spd:F2} " + - $"| seq now style=0x{seqStyle:X8} motion=0x{seqMotion:X8}"); - } - - // Phase B.4c β€” durable per-Door UM dispatch trail for visual-test grep. - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeBuildingEnabled - && _liveEntityInfoByGuid.TryGetValue(update.Guid, out var doorInfo) - && doorInfo.Name == "Door") - { - Console.WriteLine(System.FormattableString.Invariant( - $"[door-cycle] guid=0x{update.Guid:X8} stance=0x{update.MotionState.Stance:X4} cmd=0x{(update.MotionState.ForwardCommand ?? 0u):X4}")); - } - - // Wire server-echoed RunRate first β€” used for the player's own -``` - -- [ ] **Step 3: Build green** - -Run: `dotnet build src/AcDream.App/AcDream.App.csproj -c Debug` - -Expected: build succeeds, 0 errors. - -If the name `_liveEntityInfoByGuid` doesn't resolve, STOP and report. It exists in `GameWindow.cs` (verified during spec authoring; used elsewhere in `DescribeLiveEntity` around line 8758 of the B.4b-shipped tree). - -If `doorInfo.Name` doesn't resolve, the field on the live-entity info struct may be named differently (e.g. `EntityName`). Use Grep to find the existing usage pattern and adjust. - -- [ ] **Step 4: Tests green** - -Run: `dotnet test` - -Expected: same **1046 pass / 8 pre-existing-baseline fail** from Task 1. - -- [ ] **Step 5: Commit** - -```bash -git add src/AcDream.App/Rendering/GameWindow.cs -git commit -m "$(cat <<'EOF' -feat(B.4c): [door-cycle] diagnostic in OnLiveMotionUpdated - -Logs one line per UpdateMotion arriving for an entity named "Door" -when ACDREAM_PROBE_BUILDING=1. Greppable trail for the B.4c visual -test: confirms the dispatcher hit the sequencer for door open / close. - -Durable subsystem-named tag per the Opus reviewer's B.4b feedback -([B.4c] would rot after phase archival; [door-cycle] survives). - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - ---- - -## Task 3 β€” Visual verification at Holtburg inn doorway - -**This task is performed by the user.** The implementing agent kicks off the launch in background; the user observes the running client and reports the result. - -- [ ] **Step 1: Kill any stale client + wait for ACE session cleanup** - -Run via PowerShell: -```powershell -Get-Process -Name AcDream.App -ErrorAction SilentlyContinue | Stop-Process -Force -Start-Sleep -Seconds 20 -``` - -Per CLAUDE.md "Logout-before-reconnect": ACE keeps the last session alive briefly after disconnect. 20s is the empirical minimum from B.4b's debug session. - -- [ ] **Step 2: Launch the client with probes enabled** - -Run via Bash tool with `run_in_background: true`: -```powershell -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -$env:ACDREAM_TEST_HOST = "127.0.0.1" -$env:ACDREAM_TEST_PORT = "9000" -$env:ACDREAM_TEST_USER = "testaccount" -$env:ACDREAM_TEST_PASS = "testpassword" -$env:ACDREAM_DEVTOOLS = "1" -$env:ACDREAM_PROBE_BUILDING = "1" -$env:ACDREAM_DUMP_MOTION = "1" -dotnet run --project src\AcDream.App\AcDream.App.csproj --no-build -c Debug 2>&1 | - Tee-Object -FilePath "launch-b4c.log" -``` - -- [ ] **Step 3: User performs the scenario** - -In the running client: -1. Wait ~8s for the player to spawn at Holtburg. -2. Walk to the inn doorway (south building, north-facing door). -3. Observe: door visually closed. -4. Double-left-click the door. -5. **Observe: door visibly swings open over a fraction of a second.** -6. Walk forward through the open doorway. -7. Wait ~30s in the inn. -8. **Observe: door visibly swings closed.** -9. Bump the closed door β€” confirm it blocks again. -10. Close the client window. - -- [ ] **Step 4: Grep the log** - -Run via PowerShell: -```powershell -Select-String -Path launch-b4c.log -Pattern "door-anim|door-cycle|UM guid=.*Door|setstate.*0x7A9B4015" -``` - -Expected matches (in approximate order): -- `[door-anim] registered guid=0x... mtable=0x... initialCycle=0x4000000C` (one per closed door at world load) -- (user double-clicks) -- `UM guid=0x7A9B4015 mt=... stance=0x0001 cmd=0x000B ...` (existing UM dump for the open motion) -- `[door-cycle] guid=0x7A9B4015 stance=0x0001 cmd=0x000B` (NEW; cmd=On) -- `[setstate] guid=0x7A9B4015 entityId=0x000F4245 state=0x0001000C` (L.2g chain) -- ~30s gap -- `UM guid=0x7A9B4015 mt=... stance=0x0001 cmd=0x000C ...` -- `[door-cycle] guid=0x7A9B4015 stance=0x0001 cmd=0x000C` (NEW; cmd=Off) -- `[setstate] guid=0x7A9B4015 entityId=0x000F4245 state=0x00010008` - -- [ ] **Step 5: Decide on follow-up based on observed behavior** - -- **Animation plays + door rests at open pose for ~30s + animation plays again on close + rests at closed pose**: success. Proceed to Task 4. -- **Animation plays as a loop instead of one-shot** (door spins continuously): pivot to Approach C from the spec (bespoke `DoorAnimationState` outside the sequencer). Out of B.4c scope; revise the spec and file a slice 2. -- **No animation, but log shows the dispatch fired**: motion-table cycle resolution issue. Inspect `mtable.Cycles[(0x80000001 << 16) | 0x4000000B]` to see if the cycle is present. May need a different cycle key form. -- **`[door-anim] registered` never logs**: spawn-time branch isn't firing. Check `spawn.Name` actual value (might be localized or padded). Add a one-line `Console.WriteLine` of `spawn.Name` in the live-spawn handler to surface it, then revise `IsDoorSpawn` accordingly. - ---- - -## Task 4 β€” Ship handoff + close #58 + roadmap/CLAUDE/memory updates - -**Files (in-repo):** -- Create: `docs/research/2026-05-13-b4c-shipped-handoff.md` -- Modify: `docs/ISSUES.md` (close #58) -- Modify: `docs/plans/2026-04-11-roadmap.md` (add B.4c row to shipped table) -- Modify: `CLAUDE.md` ("Currently in Phase L.2" paragraph + Next phase candidates) - -**File (outside repo):** -- Modify: `C:\Users\erikn\.claude\projects\C--Users-erikn-source-repos-acdream\memory\project_interaction_pipeline.md` - -- [ ] **Step 1: Write the ship-handoff doc** - -Create `docs/research/2026-05-13-b4c-shipped-handoff.md`. Model after `docs/research/2026-05-13-b4b-shipped-handoff.md` for structure: TL;DR / What shipped (commit table) / End-to-end flow with actual observed evidence / Open notes / Reproducibility / Worktree state. - -Required content: -- TL;DR: B.4c shipped, M1 demo target "open the inn door" now has full visual feedback. ~50 LOC, 2 implementation commits + 1 docs commit. -- What shipped table (2 implementation commits from Tasks 1+2) -- Actual observed `[door-anim]` and `[door-cycle]` log lines from Task 3 step 4 -- Worktree branch: `claude/phase-b4c-door-anim`, 4 commits ahead of `3e08e10` (the B.4b merge) - -- [ ] **Step 2: Move #58 from Active to Recently Closed in `docs/ISSUES.md`** - -Edit `docs/ISSUES.md`: -- Cut the `## #58 β€” Door swing animation` block from "Active issues". -- Paste under "Recently closed" with header changed to `## #58 β€” [DONE 2026-05-13] Door swing animation: ...`. -- Add `**Status:** DONE` and `**Closed:** 2026-05-13` lines. -- Add a one-paragraph closure summary describing the fix: Door-specific spawn-time branch + state-seeded SetCycle + UM diagnostic. Cite this PR's merge commit + the handoff doc. - -- [ ] **Step 3: Update the roadmap's shipped table** - -Edit `docs/plans/2026-04-11-roadmap.md`. Add a new row to the "shipped" table: - -``` -| 2026-05-13 | Phase B.4c β€” Door swing animation | | Closes #58. Door-specific spawn-time AnimationSequencer registration with state-seeded initial cycle. M1 demo target "open the inn door" now has full visual feedback. | -``` - -(Read the table first to match its column structure exactly β€” the B.4b row uses `Phase | What landed | Verification`; match that.) - -- [ ] **Step 4: Update `CLAUDE.md` "Currently in Phase L.2" paragraph + Next phase candidates** - -Edit `CLAUDE.md`: -- Update "Currently in Phase L.2" paragraph to reflect B.4c shipped + visual-verified 2026-05-13. -- Remove `#58 β€” Door swing animation` from the "Next phase candidates" list. -- Elevate the next candidate (currently #2 in the list: "Triage the chronic open-issue list") to #1, OR pick a different next-phase based on M1 critical-path-ness. The natural next step per CLAUDE.md's "work-order autonomy" rule is whichever progresses M1's remaining demo targets ("click an NPC", "pick up an item") β€” file a one-line note that these are the M1-critical-path follow-ups even though they aren't pre-specced phases. - -- [ ] **Step 5: Update the memory file** (outside the repo, NOT git-tracked) - -Edit `C:\Users\erikn\.claude\projects\C--Users-erikn-source-repos-acdream\memory\project_interaction_pipeline.md`: -- Append a "B.4c shipped 2026-05-13" entry to the components table: - - `Door swing animation` β€” exists now (`GameWindow.cs IsDoorSpawn + sibling spawn-time branch`) - - `[door-anim]` / `[door-cycle]` diagnostics β€” gated on `ACDREAM_PROBE_BUILDING` -- Note: animation routing is door-specific, not general non-creature support yet (chests/levers/traps still drop through the gate). - -- [ ] **Step 6: Commit the in-repo docs** - -```bash -git add docs/research/2026-05-13-b4c-shipped-handoff.md docs/ISSUES.md docs/plans/2026-04-11-roadmap.md CLAUDE.md -git commit -m "$(cat <<'EOF' -docs(B.4c): ship handoff + close #58 + roadmap/CLAUDE update - -Phase B.4c shipped end-to-end 2026-05-13. Holtburg inn doorway -double-click verified: door visually swings open, player walks -through, door visually swings closed ~30s later. - -2 implementation commits: -- B.4c Task 1: door spawn-time AnimationSequencer with state-seeded cycle -- B.4c Task 2: [door-cycle] diagnostic in OnLiveMotionUpdated - -Closes #58. Memory file project_interaction_pipeline.md updated -outside the repo. - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - -(The memory file lives outside the repo β€” update it but don't include it in this commit.) - -- [ ] **Step 7: Hand off to merge (controller does final review + merge)** - -After this commit, hand off to the controller. The controller will: -1. Run the final whole-branch code review (Opus per CLAUDE.md "load-bearing quality review of a phase boundary"). -2. Merge `claude/phase-b4c-door-anim` β†’ `main` with `--no-ff`. -3. Verify tests on merged main. -4. Remove the worktree (best-effort; submodules may block per the B.4b finishing experience). - ---- - -## Self-review against the spec - -| Spec section | Plan task(s) | Coverage | -|---|---|---| -| Β§Architecture: sibling branch after creature gate at line 2692 | Task 1 step 2 | covered | -| Β§Architecture: state-seeded initial cycle from spawn.PhysicsState | Task 1 step 2 | covered | -| Β§Components: `IsDoorSpawn(spawn) => spawn.Name == "Door"` | Task 1 step 1 | covered | -| Β§Components: Door registration body (sequencer build + SetCycle + AnimatedEntity register) | Task 1 step 2 | covered | -| Β§Components: `[door-anim] registered` diagnostic on spawn | Task 1 step 2 | covered (inline in registration body) | -| Β§Components: `[door-cycle]` diagnostic in OnLiveMotionUpdated | Task 2 step 2 | covered | -| Β§Data flow: spawn β†’ seeded cycle β†’ UM dispatch β†’ state flip β†’ animation | Tasks 1+2 + L.2g (existing) | covered (L.2g pipeline is the upstream dependency) | -| Β§Error handling: door has no MotionTable | Task 1 step 2 (`if (mtableId != 0)` + inner `if (mtable is not null)`) | covered | -| Β§Error handling: MotionTable lacks On/Off cycle | Task 1 step 2 (`if (sequencer.HasCycle(...))` gate around SetCycle) | covered | -| Β§Error handling: `_animLoader` null | Task 1 step 2 (outer `&& _animLoader is not null`) | covered | -| Β§Error handling: spawn.Name != "Door" | (no code change β€” silent fallback, acceptable per spec) | covered by omission | -| Β§Testing: runtime visual verification at Holtburg | Task 3 | covered | -| Β§Testing: log grep | Task 3 step 4 | covered | -| Β§Acceptance: build + tests green | Tasks 1+2 steps 3-4 | covered | -| Β§Acceptance: ISSUES.md #58 β†’ Recently closed | Task 4 step 2 | covered | -| Β§Acceptance: roadmap + CLAUDE.md update | Task 4 steps 3-4 | covered | -| Β§Non-goals: sound effects, dust, lighting, collision rotation, generalized non-creature support | (none β€” explicitly deferred) | covered by omission | - -No placeholders. No "TBD." Every code step shows the actual code; every command step shows the exact command and expected output. Type names (`AnimationSequencer`, `AnimatedEntity`, `MotionTable`, `EntitySpawn`) match across tasks. Diagnostic tags (`[door-anim]`, `[door-cycle]`) consistent throughout. diff --git a/docs/superpowers/plans/2026-05-13-phase-c1.5b.md b/docs/superpowers/plans/2026-05-13-phase-c1.5b.md deleted file mode 100644 index 15ade22..0000000 --- a/docs/superpowers/plans/2026-05-13-phase-c1.5b.md +++ /dev/null @@ -1,946 +0,0 @@ -# Phase C.1.5b β€” issue #56 + EnvCell DefaultScript implementation plan - -> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. - -**Goal (slice A):** Fix [issue #56](../../ISSUES.md) β€” `ParticleHookSink` ignores `CreateParticleHook.PartIndex`, causing every emitter in a multi-emitter PES script (portals, fireplaces, chimneys) to collapse to the entity root. Precompute each Setup part's resting transform at activator-spawn time and apply it to the hook offset before spawning the particle. - -**Goal (slice B):** Fire `Setup.DefaultScript` for dat-hydrated entities (EnvCell statics + exterior stabs) too β€” not just server-spawned ones. Drop the `EntityScriptActivator.OnCreate` ServerGuid==0 guard and wire OnCreate/OnRemove into GpuWorldState's dat-hydration paths. - -**Architecture:** No new orchestrator classes. New helper `SetupPartTransforms.Compute(Setup)` in `AcDream.Core.Meshing`. `ParticleHookSink` grows `SetEntityPartTransforms`. `EntityScriptActivator` resolver returns a `ScriptActivationInfo` record bundling scriptId + per-part transforms. `GpuWorldState` fires the activator from four dat-hydration paths (AddLandblock, AddEntitiesToExistingLandblock, RemoveLandblock, RemoveEntitiesFromLandblock). - -**Tech Stack:** C# / .NET 10, xUnit, Silk.NET (existing). No new dependencies. - -**Spec:** [`docs/superpowers/specs/2026-05-13-phase-c1.5b-design.md`](../specs/2026-05-13-phase-c1.5b-design.md). Read it first. - ---- - -## File structure - -**Created:** - -- `src/AcDream.Core/Meshing/SetupPartTransforms.cs` β€” helper that walks Setup.PlacementFrames β†’ list of Matrix4x4 per part. -- `tests/AcDream.Core.Tests/Meshing/SetupPartTransformsTests.cs` β€” 4 tests. -- `tests/AcDream.Core.Tests/Vfx/ParticleHookSinkPartTransformTests.cs` β€” 2 tests (new file because the existing tests would otherwise gain unrelated tests). -- `tests/AcDream.Core.Tests/Streaming/GpuWorldStateActivatorTests.cs` β€” 5 integration tests for the activator wiring. - -**Modified:** - -- `src/AcDream.Core/Vfx/ParticleHookSink.cs` β€” new `_partTransformsByEntity` map; `SetEntityPartTransforms` method; `SpawnFromHook` applies the part transform; `StopAllForEntity` clears the entry. -- `src/AcDream.App/Rendering/Vfx/EntityScriptActivator.cs` β€” resolver signature change to `Func`; `ServerGuid==0` guard replaced with `key = ServerGuid != 0 ? ServerGuid : entity.Id`; pushes part transforms to sink; new `ScriptActivationInfo` record alongside the activator. -- `tests/AcDream.Core.Tests/Rendering/Vfx/EntityScriptActivatorTests.cs` β€” 4 existing tests updated for new resolver signature; 3 new tests added. -- `src/AcDream.App/Streaming/GpuWorldState.cs` β€” 4 new foreach blocks (one per AddLandblock / AddEntitiesToExistingLandblock / RemoveLandblock / RemoveEntitiesFromLandblock). -- `src/AcDream.App/Rendering/GameWindow.cs` β€” resolver lambda upgraded to return `ScriptActivationInfo?`. -- `docs/plans/2026-04-11-roadmap.md` β€” append "Phase C.1.5b SHIPPED" row on verification pass. -- `docs/ISSUES.md` β€” move #56 to Recently closed. -- `CLAUDE.md` β€” update "Currently in flight" line to point to next phase post-C.1.5b. - -Each file's responsibility: - -- `SetupPartTransforms` β€” pure function; Setup β†’ matrices. No dat lookups, no GL, no entity state. -- `ParticleHookSink` β€” owns per-entity part-transform side-table (mirroring its existing `_rotationByEntity` pattern). Applies the transform inside `SpawnFromHook`. -- `EntityScriptActivator` β€” keys correctly by ServerGuid OR Id; pushes both rotation + part transforms to the sink before scheduling. Knows nothing about dats. -- `GpuWorldState` β€” owns the four new fire-sites. Filters out live entities on the dat-hydration paths (avoid double-fire). -- `GameWindow` β€” wiring root; the resolver lambda is the only place dats touch the activator. - ---- - -## Task 1: `SetupPartTransforms` helper + tests (TDD) - -**Files:** -- Create: `src/AcDream.Core/Meshing/SetupPartTransforms.cs` -- Create: `tests/AcDream.Core.Tests/Meshing/SetupPartTransformsTests.cs` - -- [ ] **Step 1.1 β€” Write the test file with 4 failing tests** - -```csharp -using System.Collections.Generic; -using System.Numerics; -using AcDream.Core.Meshing; -using DatReaderWriter.DBObjs; -using DatReaderWriter.Enums; -using DatReaderWriter.Types; -using Xunit; - -namespace AcDream.Core.Tests.Meshing; - -public sealed class SetupPartTransformsTests -{ - private static AnimationFrame BuildFrame(params Frame[] frames) - { - var af = new AnimationFrame(); - foreach (var f in frames) af.Frames.Add(f); - return af; - } - - private static Setup BuildSetup( - int partCount, - IReadOnlyDictionary? placementFrames = null, - IReadOnlyList? defaultScale = null) - { - var setup = new Setup(); - for (int i = 0; i < partCount; i++) setup.Parts.Add(0x01000001u); - if (placementFrames is not null) - foreach (var (k, v) in placementFrames) setup.PlacementFrames[k] = v; - if (defaultScale is not null) - foreach (var s in defaultScale) setup.DefaultScale.Add(s); - return setup; - } - - [Fact] - public void ResolvesRestingPlacement_WhenAvailable() - { - var resting = BuildFrame( - new Frame { Origin = Vector3.Zero, Orientation = Quaternion.Identity }, - new Frame { Origin = new Vector3(0, 0, 1f), Orientation = Quaternion.Identity }); - - var setup = BuildSetup(partCount: 2, - placementFrames: new Dictionary - { - [Placement.Resting] = resting, - [Placement.Default] = BuildFrame(new Frame(), new Frame()), - }); - - var transforms = SetupPartTransforms.Compute(setup); - - Assert.Equal(2, transforms.Count); - var probe = Vector3.Transform(Vector3.Zero, transforms[1]); - Assert.Equal(new Vector3(0, 0, 1f), probe); - } - - [Fact] - public void FallsBackToDefault_WhenRestingMissing() - { - var defaultFrame = BuildFrame( - new Frame { Origin = new Vector3(2f, 0, 0), Orientation = Quaternion.Identity }); - - var setup = BuildSetup(partCount: 1, - placementFrames: new Dictionary - { - [Placement.Default] = defaultFrame, - }); - - var transforms = SetupPartTransforms.Compute(setup); - - Assert.Single(transforms); - var probe = Vector3.Transform(Vector3.Zero, transforms[0]); - Assert.Equal(new Vector3(2f, 0, 0), probe); - } - - [Fact] - public void ReturnsEmpty_WhenNoPlacementFrames() - { - var setup = BuildSetup(partCount: 2); - var transforms = SetupPartTransforms.Compute(setup); - Assert.Empty(transforms); - } - - [Fact] - public void AppliesDefaultScale_WhenPresent() - { - var resting = BuildFrame( - new Frame { Origin = Vector3.Zero, Orientation = Quaternion.Identity }); - - var setup = BuildSetup(partCount: 1, - placementFrames: new Dictionary { [Placement.Resting] = resting }, - defaultScale: new[] { new Vector3(2f, 2f, 2f) }); - - var transforms = SetupPartTransforms.Compute(setup); - - var probe = Vector3.Transform(new Vector3(1f, 1f, 1f), transforms[0]); - Assert.Equal(new Vector3(2f, 2f, 2f), probe); - } -} -``` - -- [ ] **Step 1.2 β€” Implement `SetupPartTransforms`** - -`src/AcDream.Core/Meshing/SetupPartTransforms.cs`: - -```csharp -using System.Collections.Generic; -using System.Numerics; -using DatReaderWriter.DBObjs; -using DatReaderWriter.Enums; -using DatReaderWriter.Types; - -namespace AcDream.Core.Meshing; - -/// -/// Compute the per-part static transforms for a Setup using its -/// PlacementFrames. For each part i, the returned matrix is the -/// transform from part-local to setup-local space at the Setup's -/// resting pose. Mirrors 's pose-source -/// priority: PlacementFrames[Resting] β†’ [Default] β†’ first available. -/// Returns an empty list when the Setup has no PlacementFrames -/// (caller falls back to "no part transforms applied" β€” equivalent to -/// pre-C.1.5b behavior in ParticleHookSink.SpawnFromHook). -/// -public static class SetupPartTransforms -{ - public static IReadOnlyList Compute(Setup setup) - { - AnimationFrame? source = null; - if (setup.PlacementFrames.TryGetValue(Placement.Resting, out var resting)) - source = resting; - else if (setup.PlacementFrames.TryGetValue(Placement.Default, out var def)) - source = def; - else - { - foreach (var kvp in setup.PlacementFrames) - { - source = kvp.Value; - break; - } - } - - if (source is null) return System.Array.Empty(); - - int partCount = setup.Parts.Count; - var result = new Matrix4x4[partCount]; - for (int i = 0; i < partCount; i++) - { - Frame frame = i < source.Frames.Count - ? source.Frames[i] - : new Frame { Origin = Vector3.Zero, Orientation = Quaternion.Identity }; - Vector3 scale = i < setup.DefaultScale.Count ? setup.DefaultScale[i] : Vector3.One; - result[i] = Matrix4x4.CreateScale(scale) - * Matrix4x4.CreateFromQuaternion(frame.Orientation) - * Matrix4x4.CreateTranslation(frame.Origin); - } - return result; - } -} -``` - -- [ ] **Step 1.3 β€” Verify** - -```pwsh -dotnet test --filter "FullyQualifiedName~SetupPartTransformsTests" -c Debug -``` - -All 4 tests pass. `dotnet build` green. - -- [ ] **Step 1.4 β€” Commit** - -``` -feat(vfx #C.1.5b): SetupPartTransforms helper for per-part anchor transforms - -Computes Matrix4x4 per Setup part by walking PlacementFrames[Resting] β†’ -[Default] β†’ first-available, matching SetupMesh.Flatten's priority. -Foundation for #56 fix: ParticleHookSink will use these to apply the -hook's PartIndex-relative offset to the right mesh part. -``` - ---- - -## Task 2: `ParticleHookSink` part-transform support + tests - -**Files:** -- Modify: `src/AcDream.Core/Vfx/ParticleHookSink.cs` -- Create: `tests/AcDream.Core.Tests/Vfx/ParticleHookSinkPartTransformTests.cs` - -- [ ] **Step 2.1 β€” Write the test file with 2 failing tests** - -```csharp -using System.Collections.Generic; -using System.Linq; -using System.Numerics; -using AcDream.Core.Vfx; -using DatReaderWriter.Types; -using Xunit; - -namespace AcDream.Core.Tests.Vfx; - -public sealed class ParticleHookSinkPartTransformTests -{ - private static EmitterDesc BuildPersistentEmitterDesc() => new() - { - DatId = 100u, - Type = ParticleType.Still, - EmitterKind = ParticleEmitterKind.BirthratePerSec, - MaxParticles = 4, - InitialParticles = 1, - TotalParticles = 0, - TotalDuration = 0f, - Lifespan = 999f, - LifetimeMin = 999f, - LifetimeMax = 999f, - Birthrate = 0.5f, - StartSize = 0.5f, - EndSize = 0.5f, - StartAlpha = 1f, - EndAlpha = 1f, - }; - - [Fact] - public void AppliesPartTransform_WhenRegistered() - { - var partTransforms = new Matrix4x4[] - { - Matrix4x4.Identity, - Matrix4x4.CreateTranslation(0f, 0f, 1f), - }; - - var registry = new EmitterDescRegistry(); - registry.Register(BuildPersistentEmitterDesc()); - var system = new ParticleSystem(registry); - var sink = new ParticleHookSink(system); - - sink.SetEntityRotation(0xCAFEu, Quaternion.Identity); - sink.SetEntityPartTransforms(0xCAFEu, partTransforms); - - sink.OnHook(0xCAFEu, Vector3.Zero, new CreateParticleHook - { - EmitterInfoId = 100u, - PartIndex = 1, - Offset = new Frame - { - Origin = new Vector3(1f, 0f, 0f), - Orientation = Quaternion.Identity, - }, - EmitterId = 0u, - }); - - system.Tick(0.001f); - - var live = system.EnumerateLive().FirstOrDefault(); - Assert.NotNull(live.Emitter); - var pos = live.Emitter.Particles[live.Index].Position; - Assert.InRange(pos.X, 0.99f, 1.01f); - Assert.InRange(pos.Y, -0.01f, 0.01f); - Assert.InRange(pos.Z, 0.99f, 1.01f); - } - - [Fact] - public void FallsBackToIdentity_WhenPartIndexOutOfBounds() - { - var partTransforms = new[] { Matrix4x4.Identity, Matrix4x4.CreateTranslation(0f, 0f, 1f) }; - - var registry = new EmitterDescRegistry(); - registry.Register(BuildPersistentEmitterDesc()); - var system = new ParticleSystem(registry); - var sink = new ParticleHookSink(system); - - sink.SetEntityRotation(0xCAFEu, Quaternion.Identity); - sink.SetEntityPartTransforms(0xCAFEu, partTransforms); - - sink.OnHook(0xCAFEu, Vector3.Zero, new CreateParticleHook - { - EmitterInfoId = 100u, - PartIndex = 99, - Offset = new Frame { Origin = new Vector3(2f, 0f, 0f), Orientation = Quaternion.Identity }, - }); - - system.Tick(0.001f); - - var live = system.EnumerateLive().FirstOrDefault(); - Assert.NotNull(live.Emitter); - var pos = live.Emitter.Particles[live.Index].Position; - Assert.InRange(pos.X, 1.99f, 2.01f); - Assert.InRange(pos.Y, -0.01f, 0.01f); - Assert.InRange(pos.Z, -0.01f, 0.01f); - } -} -``` - -- [ ] **Step 2.2 β€” Modify `ParticleHookSink`** - -Add field next to `_rotationByEntity`: -```csharp -private readonly ConcurrentDictionary> _partTransformsByEntity = new(); -``` - -Add method next to `SetEntityRotation`: -```csharp -public void SetEntityPartTransforms(uint entityId, IReadOnlyList partTransforms) - => _partTransformsByEntity[entityId] = partTransforms; -``` - -Add cleanup line inside `StopAllForEntity`: -```csharp -_partTransformsByEntity.TryRemove(entityId, out _); -``` - -Modify `SpawnFromHook` β€” replace the anchor computation: -```csharp -var rotation = _rotationByEntity.TryGetValue(entityId, out var rot) - ? rot - : Quaternion.Identity; - -Vector3 partLocal = offset; -if (_partTransformsByEntity.TryGetValue(entityId, out var pts) - && partIndex >= 0 - && partIndex < pts.Count) -{ - partLocal = Vector3.Transform(offset, pts[partIndex]); -} - -var anchor = worldPos + Vector3.Transform(partLocal, rotation); -``` - -- [ ] **Step 2.3 β€” Verify** - -```pwsh -dotnet test --filter "FullyQualifiedName~ParticleHookSinkPartTransformTests" -c Debug -dotnet test -c Debug -``` - -Both new tests pass. Existing tests still green (the change is backwards-compatible β€” entities without registered part transforms fall through to identity, same as before). - -- [ ] **Step 2.4 β€” Commit** - -``` -fix(vfx #56): ParticleHookSink applies CreateParticleHook.PartIndex transform - -Adds per-entity part-transform side-table mirroring _rotationByEntity. -SpawnFromHook now transforms the hook offset through partTransforms[partIndex] -before rotating to world space. Backwards-compatible: entities without -registered part transforms fall through to identity (pre-C.1.5b behavior). - -Closes the renderer side of #56. EntityScriptActivator wiring lands next. -``` - ---- - -## Task 3: `EntityScriptActivator` resolver refactor + ServerGuid relaxation + tests - -**Files:** -- Modify: `src/AcDream.App/Rendering/Vfx/EntityScriptActivator.cs` -- Modify: `tests/AcDream.Core.Tests/Rendering/Vfx/EntityScriptActivatorTests.cs` - -- [ ] **Step 3.1 β€” Add `ScriptActivationInfo` record and update activator** - -In `src/AcDream.App/Rendering/Vfx/EntityScriptActivator.cs`, add at the top of the namespace (before the activator class): - -```csharp -public sealed record ScriptActivationInfo( - uint ScriptId, - IReadOnlyList PartTransforms); -``` - -Change the resolver field type: -```csharp -private readonly Func _resolver; -``` - -Update ctor parameter name + type accordingly. - -Replace `OnCreate`: -```csharp -public void OnCreate(WorldEntity entity) -{ - ArgumentNullException.ThrowIfNull(entity); - uint key = entity.ServerGuid != 0 ? entity.ServerGuid : entity.Id; - if (key == 0) return; - - var info = _resolver(entity); - if (info is null || info.ScriptId == 0) return; - - _particleSink.SetEntityRotation(key, entity.Rotation); - _particleSink.SetEntityPartTransforms(key, info.PartTransforms); - _scriptRunner.Play(info.ScriptId, key, entity.Position); -} -``` - -Replace `OnRemove`: -```csharp -public void OnRemove(uint key) -{ - if (key == 0) return; - _scriptRunner.StopAllForEntity(key); - _particleSink.StopAllForEntity(key, fadeOut: false); -} -``` - -Update doc comments to reflect the new key semantics (handles both server-spawned and dat-hydrated entities; caller picks the key for OnRemove). - -- [ ] **Step 3.2 β€” Update the 4 existing tests for the new resolver signature** - -In `EntityScriptActivatorTests.cs`, every `_ => 0xAAu` becomes: -```csharp -_ => new ScriptActivationInfo(0xAAu, System.Array.Empty()) -``` - -Every `_ => 0u` becomes: -```csharp -_ => null -``` - -`OnRemove(entity.ServerGuid)` calls stay correct (the public API now takes `uint key` either way). - -- [ ] **Step 3.3 β€” Add 3 new tests** - -Append to `EntityScriptActivatorTests.cs`: - -```csharp -[Fact] -public void OnCreate_KeysByEntityId_WhenServerGuidZero() -{ - var p = BuildPipeline( - (0xAAu, BuildScript((0.0, new CreateParticleHook { EmitterInfoId = 100 })))); - var activator = new EntityScriptActivator(p.Runner, p.Sink, - _ => new ScriptActivationInfo(0xAAu, System.Array.Empty())); - var entity = new WorldEntity - { - Id = 0x40A9B401u, - ServerGuid = 0u, - SourceGfxObjOrSetupId = 0x02000001u, - Position = new Vector3(5, 5, 5), - Rotation = Quaternion.Identity, - MeshRefs = System.Array.Empty(), - }; - - activator.OnCreate(entity); - - Assert.Equal(1, p.Runner.ActiveScriptCount); - p.Runner.Tick(0.001f); - Assert.Single(p.Recording.Calls); - Assert.Equal(0x40A9B401u, p.Recording.Calls[0].EntityId); - Assert.Equal(new Vector3(5, 5, 5), p.Recording.Calls[0].Pos); -} - -[Fact] -public void OnCreate_PassesPartTransformsToSink() -{ - var registry = new EmitterDescRegistry(); - registry.Register(BuildPersistentEmitterDesc()); - var system = new ParticleSystem(registry); - var hookSink = new ParticleHookSink(system); - - var hookOffset = new Frame { Origin = new Vector3(1f, 0, 0), Orientation = Quaternion.Identity }; - var script = BuildScript( - (0.0, new CreateParticleHook { EmitterInfoId = 100u, Offset = hookOffset, PartIndex = 1 })); - var table = new Dictionary { [0xAAu] = script }; - var runner = new PhysicsScriptRunner( - id => table.TryGetValue(id, out var s) ? s : null, - hookSink); - - var partTransforms = new Matrix4x4[] - { - Matrix4x4.Identity, - Matrix4x4.CreateTranslation(0f, 0f, 1f), - }; - - var activator = new EntityScriptActivator(runner, hookSink, - _ => new ScriptActivationInfo(0xAAu, partTransforms)); - var entity = MakeEntity(serverGuid: 0xCAFEu, position: Vector3.Zero); - - activator.OnCreate(entity); - runner.Tick(0.001f); - system.Tick(0.001f); - - var live = system.EnumerateLive().FirstOrDefault(); - Assert.NotNull(live.Emitter); - var pos = live.Emitter.Particles[live.Index].Position; - Assert.InRange(pos.X, 0.99f, 1.01f); - Assert.InRange(pos.Y, -0.01f, 0.01f); - Assert.InRange(pos.Z, 0.99f, 1.01f); -} - -[Fact] -public void OnRemove_StopsByGivenKey() -{ - var registry = new EmitterDescRegistry(); - registry.Register(BuildPersistentEmitterDesc()); - var system = new ParticleSystem(registry); - var hookSink = new ParticleHookSink(system); - - var script = BuildScript((0.0, new CreateParticleHook { EmitterInfoId = 100u, Offset = new Frame() })); - var table = new Dictionary { [0xAAu] = script }; - var runner = new PhysicsScriptRunner( - id => table.TryGetValue(id, out var s) ? s : null, - hookSink); - - var activator = new EntityScriptActivator(runner, hookSink, - _ => new ScriptActivationInfo(0xAAu, System.Array.Empty())); - var entity = new WorldEntity - { - Id = 0x40A9B402u, - ServerGuid = 0u, - SourceGfxObjOrSetupId = 0x02000001u, - Position = Vector3.Zero, - Rotation = Quaternion.Identity, - MeshRefs = System.Array.Empty(), - }; - - activator.OnCreate(entity); - runner.Tick(0.001f); - Assert.True(system.ActiveEmitterCount > 0); - - activator.OnRemove(0x40A9B402u); - - Assert.Equal(0, runner.ActiveScriptCount); - system.Tick(0.01f); - Assert.Equal(0, system.ActiveEmitterCount); -} -``` - -The existing `OnRemove_StopsScriptsAndEmitters` test continues to test the server-guid path. The new `OnRemove_StopsByGivenKey` exercises the dat-hydrated-entity path with the new key. - -- [ ] **Step 3.4 β€” Verify** - -```pwsh -dotnet test -c Debug -``` - -All tests green, including 4 updated + 3 new in `EntityScriptActivatorTests`, plus the 2 from Task 2 and the 4 from Task 1. - -- [ ] **Step 3.5 β€” Commit** - -``` -feat(vfx #C.1.5b): activator handles dat-hydrated entities + per-part transforms - -Resolver returns ScriptActivationInfo(ScriptId, PartTransforms) β€” one -dat lookup per spawn yields both pieces of info. Activator keys by -ServerGuid when nonzero, else entity.Id, so dat-hydrated entities -(EnvCell statics, exterior stabs) flow through the same code path. -Pushes per-part transforms into the sink before scheduling. - -Closes the activator side of #56. GpuWorldState fire-site wiring next. -``` - ---- - -## Task 4: `GpuWorldState` fire-site wiring + production lambda + integration tests - -**Files:** -- Modify: `src/AcDream.App/Streaming/GpuWorldState.cs` -- Create: `tests/AcDream.Core.Tests/Streaming/GpuWorldStateActivatorTests.cs` -- Modify: `src/AcDream.App/Rendering/GameWindow.cs` β€” resolver lambda - -- [ ] **Step 4.1 β€” Add the 4 foreach blocks in `GpuWorldState`** - -In `AddLandblock` (after `_loaded[landblock.LandblockId] = landblock;` and after `_wbSpawnAdapter?.OnLandblockLoaded(...)`): -```csharp -// C.1.5b: fire DefaultScript for dat-hydrated entities (ServerGuid==0). -// Live entities (ServerGuid!=0) already had OnCreate fired at -// AppendLiveEntity; filter to avoid double-firing pending-bucket merges. -if (_entityScriptActivator is not null) -{ - var entities = _loaded[landblock.LandblockId].Entities; - for (int i = 0; i < entities.Count; i++) - { - var e = entities[i]; - if (e.ServerGuid == 0) - _entityScriptActivator.OnCreate(e); - } -} -``` - -In `AddEntitiesToExistingLandblock` (after the merge + the `_wbSpawnAdapter` call): -```csharp -// C.1.5b: fire DefaultScript for each promoted dat-hydrated entity. -// All entities arriving via this path are dat-hydrated by construction -// (the promotion path streams in atlas-tier content). -if (_entityScriptActivator is not null) -{ - for (int i = 0; i < entities.Count; i++) - _entityScriptActivator.OnCreate(entities[i]); -} -``` - -In `RemoveLandblock` (inside the `if (_loaded.TryGetValue(...))` block, after the rescue loop): -```csharp -// C.1.5b: stop DefaultScript for each dat-hydrated entity in the -// landblock. Server-spawned entities are either being rescued (script -// continues) or were OnRemove'd via RemoveEntityByServerGuid earlier; -// leave them alone here. -if (_entityScriptActivator is not null) -{ - foreach (var entity in lb.Entities) - { - if (entity.ServerGuid == 0) - _entityScriptActivator.OnRemove(entity.Id); - } -} -``` - -In `RemoveEntitiesFromLandblock` (after the existing `_onLandblockUnloaded?.Invoke(canonical)`, before the entities-list replacement): -```csharp -// C.1.5b: stop DefaultScript for each dat-hydrated entity about to -// be dropped. Demote-tier entities are always atlas-tier -// (ServerGuid==0); the filter is belt-and-suspenders. -if (_entityScriptActivator is not null) -{ - foreach (var entity in lb.Entities) - { - if (entity.ServerGuid == 0) - _entityScriptActivator.OnRemove(entity.Id); - } -} -``` - -The `RemoveEntityByServerGuid` site at line 290 stays the same β€” `OnRemove(uint key)` accepts any key. - -- [ ] **Step 4.2 β€” Update the production resolver lambda in `GameWindow`** - -At GameWindow.cs:1617-1637, replace the existing resolver lambda: - -```csharp -var entityScriptActivator = new AcDream.App.Rendering.Vfx.EntityScriptActivator( - scriptRunner, - particleHookSink, - entity => - { - try - { - var setup = _dats.Get(entity.SourceGfxObjOrSetupId); - if (setup is null) return null; - uint scriptId = setup.DefaultScript.DataId; - if (scriptId == 0) return null; - var parts = AcDream.Core.Meshing.SetupPartTransforms.Compute(setup); - return new AcDream.App.Rendering.Vfx.ScriptActivationInfo(scriptId, parts); - } - catch - { - return null; - } - }); -``` - -- [ ] **Step 4.3 β€” Write the integration tests** - -Create `tests/AcDream.Core.Tests/Streaming/GpuWorldStateActivatorTests.cs`: - -```csharp -using System.Collections.Generic; -using System.Numerics; -using AcDream.App.Rendering.Vfx; -using AcDream.App.Streaming; -using AcDream.Core.Physics; -using AcDream.Core.Vfx; -using AcDream.Core.World; -using DatReaderWriter.Types; -using Xunit; -using DatPhysicsScript = DatReaderWriter.DBObjs.PhysicsScript; - -namespace AcDream.Core.Tests.Streaming; - -public sealed class GpuWorldStateActivatorTests -{ - private sealed class RecordingSink : IAnimationHookSink - { - public List<(uint EntityId, Vector3 Pos, AnimationHook Hook)> Calls = new(); - public void OnHook(uint entityId, Vector3 worldPos, AnimationHook hook) - => Calls.Add((entityId, worldPos, hook)); - } - - private static (GpuWorldState State, PhysicsScriptRunner Runner, ParticleHookSink Sink, RecordingSink Recording) - BuildState(uint scriptId) - { - var script = new DatPhysicsScript(); - script.ScriptData.Add(new PhysicsScriptData - { - StartTime = 0.0, - Hook = new CreateParticleHook { EmitterInfoId = 100u, Offset = new Frame() }, - }); - var table = new Dictionary { [scriptId] = script }; - - var registry = new EmitterDescRegistry(); - var system = new ParticleSystem(registry); - var sink = new ParticleHookSink(system); - var recording = new RecordingSink(); - var runner = new PhysicsScriptRunner(id => table.TryGetValue(id, out var s) ? s : null, recording); - var activator = new EntityScriptActivator(runner, sink, - _ => new ScriptActivationInfo(scriptId, System.Array.Empty())); - - var state = new GpuWorldState(entityScriptActivator: activator); - return (state, runner, sink, recording); - } - - private static WorldEntity DatHydrated(uint id, Vector3 pos) => new() - { - Id = id, - ServerGuid = 0u, - SourceGfxObjOrSetupId = 0x02000001u, - Position = pos, - Rotation = Quaternion.Identity, - MeshRefs = System.Array.Empty(), - }; - - private static WorldEntity Live(uint serverGuid, Vector3 pos) => new() - { - Id = serverGuid, - ServerGuid = serverGuid, - SourceGfxObjOrSetupId = 0x02000001u, - Position = pos, - Rotation = Quaternion.Identity, - MeshRefs = System.Array.Empty(), - }; - - [Fact] - public void AddLandblock_FiresActivatorForDatHydrated() - { - var p = BuildState(scriptId: 0xAAu); - var entity = DatHydrated(id: 0x40A9B401u, pos: Vector3.Zero); - var lb = new LoadedLandblock(0xA9B4FFFFu, new float[0], new[] { entity }); - - p.State.AddLandblock(lb); - p.Runner.Tick(0.001f); - - Assert.Single(p.Recording.Calls); - Assert.Equal(0x40A9B401u, p.Recording.Calls[0].EntityId); - } - - [Fact] - public void AddLandblock_DoesNotDoubleFire_OnPendingMerge() - { - var p = BuildState(scriptId: 0xAAu); - var live = Live(serverGuid: 0xCAFEu, pos: Vector3.Zero); - - p.State.AppendLiveEntity(0xA9B4FFFFu, live); - - var lb = new LoadedLandblock(0xA9B4FFFFu, new float[0], System.Array.Empty()); - p.State.AddLandblock(lb); - - p.Runner.Tick(0.001f); - Assert.Single(p.Recording.Calls); - } - - [Fact] - public void RemoveLandblock_FiresOnRemoveForDatHydrated() - { - var p = BuildState(scriptId: 0xAAu); - var entity = DatHydrated(id: 0x40A9B401u, pos: Vector3.Zero); - var lb = new LoadedLandblock(0xA9B4FFFFu, new float[0], new[] { entity }); - - p.State.AddLandblock(lb); - p.Runner.Tick(0.001f); - Assert.Equal(1, p.Runner.ActiveScriptCount); - - p.State.RemoveLandblock(0xA9B4FFFFu); - Assert.Equal(0, p.Runner.ActiveScriptCount); - } - - [Fact] - public void AddEntitiesToExistingLandblock_FiresActivator() - { - var p = BuildState(scriptId: 0xAAu); - var emptyLb = new LoadedLandblock(0xA9B4FFFFu, new float[0], System.Array.Empty()); - p.State.AddLandblock(emptyLb); - - var promoted = new[] - { - DatHydrated(id: 0x40A9B401u, pos: Vector3.Zero), - DatHydrated(id: 0x40A9B402u, pos: Vector3.UnitX), - }; - p.State.AddEntitiesToExistingLandblock(0xA9B4FFFFu, promoted); - - p.Runner.Tick(0.001f); - Assert.Equal(2, p.Recording.Calls.Count); - } - - [Fact] - public void RemoveEntitiesFromLandblock_FiresOnRemove() - { - var p = BuildState(scriptId: 0xAAu); - var entity = DatHydrated(id: 0x40A9B401u, pos: Vector3.Zero); - var lb = new LoadedLandblock(0xA9B4FFFFu, new float[0], new[] { entity }); - p.State.AddLandblock(lb); - p.Runner.Tick(0.001f); - Assert.Equal(1, p.Runner.ActiveScriptCount); - - p.State.RemoveEntitiesFromLandblock(0xA9B4FFFFu); - Assert.Equal(0, p.Runner.ActiveScriptCount); - } -} -``` - -- [ ] **Step 4.4 β€” Verify build + tests** - -```pwsh -dotnet build -c Debug -dotnet test -c Debug -``` - -All tests green. No regressions in existing `GpuWorldStateTests` (if any assert on ctor arity β€” they shouldn't, since C.1.5a already added the optional `entityScriptActivator` param). - -- [ ] **Step 4.5 β€” Commit** - -``` -feat(vfx #C.1.5b): GpuWorldState fires activator for dat-hydrated entities - -Wires EntityScriptActivator.OnCreate into AddLandblock, -AddEntitiesToExistingLandblock, and OnRemove into RemoveLandblock + -RemoveEntitiesFromLandblock. ServerGuid==0 filter avoids double-firing -on pending-bucket merges of live entities. - -GameWindow's resolver lambda upgraded to return ScriptActivationInfo -(scriptId + per-part transforms from SetupPartTransforms.Compute). - -Closes #56. Slice A (per-part transforms) + slice B (dat-hydrated -entities) both wired end-to-end. Ready for visual verification at -Holtburg portal + Inn fireplace + cottage chimney. -``` - ---- - -## Task 5: Visual verification + ship docs + merge - -- [ ] **Step 5.1 β€” Launch the live client** - -```pwsh -Get-Process -Name AcDream.App -ErrorAction SilentlyContinue | Stop-Process -Force -Start-Sleep -Seconds 3 - -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -$env:ACDREAM_TEST_HOST = "127.0.0.1" -$env:ACDREAM_TEST_PORT = "9000" -$env:ACDREAM_TEST_USER = "testaccount" -$env:ACDREAM_TEST_PASS = "testpassword" -$env:ACDREAM_DUMP_PLAYSCRIPT = "1" -dotnet run --project src\AcDream.App\AcDream.App.csproj --no-build -c Debug 2>&1 | - Tee-Object -FilePath "launch.log" -``` - -Run in background so the parent session keeps control of the terminal. - -- [ ] **Step 5.2 β€” Visual verification with the user** - -Four sites (per spec Β§6): -1. **Holtburg Town network portal** β€” swirl extends vertically, no ground-burial, distinct columns visible. -2. **Holtburg Inn fireplace** β€” flame particles at firebox. -3. **Cottage chimney** β€” smoke column visible. -4. **Spell cast on +Acdream** β€” cast-anim particles match retail. - -Diagnostic: `Select-String "\[pes\] Play:" launch.log` β€” every successful Play call. If a site has no particles, the log shows whether the script fired and with what scriptId. - -**STOP and wait for the user to confirm each site matches retail.** This is the acceptance gate. - -- [ ] **Step 5.3 β€” Ship docs** - -If all four sites pass: - -1. **`docs/plans/2026-04-11-roadmap.md`** β€” append "Phase C.1.5b SHIPPED 2026-05-13 β€” closes #56 + EnvCell DefaultScript dispatch" entry to the shipped table. -2. **`docs/ISSUES.md`** β€” move #56 from Active to Recently closed with the commit SHA from Task 4. -3. **`CLAUDE.md`** β€” update the "Currently in flight" line. Drop the C.1.5b reference. Either point to the next phase the user picks, or leave a "between phases" note. - -- [ ] **Step 5.4 β€” Final commit + merge** - -``` -docs(vfx #C.1.5b): ship Phase C.1.5b β€” closes #56 + EnvCell DefaultScript dispatch - -Roadmap: add SHIPPED row. -ISSUES: #56 β†’ Recently closed. -CLAUDE.md: "Currently in flight" pointer updated. - -Visual verification 2026-05-13: portal swirl matches retail extent + -spread (no ground-burial); Holtburg Inn fireplace flames; cottage -chimney smoke; spell cast particles all match retail. -``` - -Then `git checkout main && git merge --no-ff claude/trusting-elbakyan-633b52` and push. - ---- - -## Notes for the executing agent - -- **TDD discipline:** every Task starts with a failing test, then implementation, then verify. The C.1.5a phase shipped clean because the test scaffolding caught the spawn-on-zero-guid case AND the rotation-seed case before they became visual regressions. Same discipline here for the per-part transform pipeline. -- **Don't touch animated entities.** The `SetEntityPartTransforms` seam is keyed by entity, so a future "animated DefaultScript" phase can push fresh transforms each tick without changing this contract. Out of scope for C.1.5b. -- **Don't touch `ParticleRenderer.cs`.** Bindless migration is N.6 slice 2. -- **Don't invent emitter types.** Reuse existing PES data. -- **If visual verification fails:** check `launch.log` for `[pes]` lines first. If the script DID fire but particles look wrong, the bug is in `SpawnFromHook` or the part-transform math. If the script DIDN'T fire, the bug is in the activator wiring or the resolver. Investigate via decomp + cross-reference (`docs/research/named-retail/` for the retail expectation) before guessing β€” the CLAUDE.md workflow. -- **Worktree cleanup:** see handoff Β§9 for the post-merge cleanup command for the C.1.5a worktree at `lucid-burnell-aab524`. - -Spec at [`docs/superpowers/specs/2026-05-13-phase-c1.5b-design.md`](../specs/2026-05-13-phase-c1.5b-design.md) is the source of truth for the architecture; this plan is the execution sequence. diff --git a/docs/superpowers/plans/2026-05-14-phase-b5-pickup.md b/docs/superpowers/plans/2026-05-14-phase-b5-pickup.md deleted file mode 100644 index 32d22a6..0000000 --- a/docs/superpowers/plans/2026-05-14-phase-b5-pickup.md +++ /dev/null @@ -1,319 +0,0 @@ -# Phase B.5 β€” BuildPickUp + ground-item interaction β€” Implementation Plan - -> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. - -**Goal:** Close M1 demo target 4/4 β€” make F-key pick up the currently-selected ground item by sending ACE's `PutItemInContainer (0x0019)` GameAction. - -**Architecture:** Mirror B.4b's outbound `BuildUse` chain. Add one new wire-builder (`InteractRequests.BuildPickUp`) and one new GameWindow helper (`SendPickUp`); wire the F-key action into the existing `OnInputAction` switch using the existing `_selectedGuid` field. The ACE inbound handlers (`InventoryPutObjInContainer 0x019B`, `RemoveObject`) are already wired in `GameEventWiring.cs` and will despawn the ground item once ACE acknowledges the pickup β€” no inbound work needed. - -**Tech Stack:** C# / .NET 10 / xUnit (existing). Wire format: `BinaryPrimitives.WriteUInt32LittleEndian` little-endian envelope same as every other `0xF7B1` GameAction in the file. - ---- - -## Predecessor & branch state - -- **Branch:** `claude/phase-b5-pickup` in worktree `.claude/worktrees/investigate-npc-click`. -- **Main HEAD at start:** `e7842e0` β€” Merge B.4c. -- **Existing commits on branch:** `86440ff` β€” the B.5 handoff doc (`docs/research/2026-05-13-b5-pickup-handoff.md`). - ---- - -## Wire format (verified against ACE source) - -`references/ACE/Source/ACE.Server/Network/GameAction/Actions/GameActionPutItemInContainer.cs`: - -```csharp -var itemGuid = message.Payload.ReadUInt32(); -var containerGuid = message.Payload.ReadUInt32(); -var placement = message.Payload.ReadInt32(); -``` - -`references/ACE/Source/ACE.Server/Network/GameAction/GameActionType.cs:13`: -``` -PutItemInContainer = 0x0019, -``` - -Therefore the full **24-byte** GameAction body is: - -| Offset | Field | Bytes | -|---|---|---| -| 0 | `0xF7B1` (GameAction envelope) | 4 | -| 4 | `gameActionSequence` | 4 | -| 8 | `0x0019` (PutItemInContainer subopcode) | 4 | -| 12 | `itemGuid` (u32, the server guid of the ground item) | 4 | -| 16 | `containerGuid` (u32, the player's server guid) | 4 | -| 20 | `placement` (i32, 0 = let server choose slot) | 4 | - -**NB:** The handoff doc (`2026-05-13-b5-pickup-handoff.md`) said "20-byte total body." That was an arithmetic error in the handoff β€” corrected here. - ---- - -## File structure (which files touched) - -- **Modify:** `src/AcDream.Core.Net/Messages/InteractRequests.cs` β€” add `PutItemInContainerOpcode` constant + `BuildPickUp(seq, itemGuid, containerGuid, placement)` builder. -- **Modify:** `tests/AcDream.Core.Net.Tests/Messages/InteractRequestsTests.cs` β€” add a unit test for `BuildPickUp` covering byte layout + opcode. -- **Modify:** `src/AcDream.App/Rendering/GameWindow.cs` β€” add private `SendPickUp(uint itemGuid)` helper next to `SendUse`; add `case InputAction.SelectionPickUp` in the `OnInputAction` switch. - -No new files. No tests for the GameWindow helper (it's a thin pass-through wrapper around `_liveSession.SendGameAction` mirroring `SendUse`, which itself has no unit test for the same reason). - ---- - -## Task 1: TDD β€” InteractRequests.BuildPickUp - -**Files:** -- Modify: `tests/AcDream.Core.Net.Tests/Messages/InteractRequestsTests.cs` -- Modify: `src/AcDream.Core.Net/Messages/InteractRequests.cs` - -- [ ] **Step 1: Write the failing test.** Append this new `[Fact]` immediately after `BuildUseWithTarget_WritesBothGuids` in `tests/AcDream.Core.Net.Tests/Messages/InteractRequestsTests.cs`: - -```csharp - [Fact] - public void BuildPickUp_WritesOpcode0x0019AndPayload() - { - byte[] body = InteractRequests.BuildPickUp( - gameActionSequence: 5, - itemGuid: 0xABCDu, - containerGuid: 0x5000000Au, - placement: 0); - - Assert.Equal(24, body.Length); - Assert.Equal(InteractRequests.GameActionEnvelope, - BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(0))); - Assert.Equal(5u, - BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(4))); - Assert.Equal(InteractRequests.PutItemInContainerOpcode, - BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(8))); - Assert.Equal(0xABCDu, - BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(12))); - Assert.Equal(0x5000000Au, - BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(16))); - Assert.Equal(0, - BinaryPrimitives.ReadInt32LittleEndian(body.AsSpan(20))); - } -``` - -- [ ] **Step 2: Run test to verify it fails.** - -```powershell -dotnet test tests/AcDream.Core.Net.Tests/AcDream.Core.Net.Tests.csproj ` - --filter "FullyQualifiedName~InteractRequestsTests.BuildPickUp" -``` - -Expected: build fails with `CS0117: 'InteractRequests' does not contain a definition for 'BuildPickUp'` (and a second error for `PutItemInContainerOpcode`). - -- [ ] **Step 3: Add the constant + builder.** Edit `src/AcDream.Core.Net/Messages/InteractRequests.cs`. Add this opcode constant immediately after the existing `TeleToLifestoneOpcode` declaration (~line 31): - -```csharp - public const uint PutItemInContainerOpcode = 0x0019u; -``` - -Then append the new builder method immediately after `BuildTeleToLifestone` (~line 75, just before the closing `}` of the class). Use the same `XmlDoc + BinaryPrimitives` style as the existing builders: - -```csharp - /// - /// Pick up a ground item or move an item between containers. The - /// server places the item in at - /// the given slot (pass 0 to let the - /// server choose). For F-key ground-pickup, pass the player's own - /// server guid as . - /// - /// - /// Wire layout (ACE GameActionPutItemInContainer.Handle): - /// - /// u32 0xF7B1 - /// u32 gameActionSequence - /// u32 0x0019 // PutItemInContainer - /// u32 itemGuid // server guid of the item - /// u32 containerGuid // destination container (player or bag) - /// i32 placement // 0 = server picks slot - /// - /// - /// - public static byte[] BuildPickUp( - uint gameActionSequence, uint itemGuid, uint containerGuid, int placement) - { - byte[] body = new byte[24]; - BinaryPrimitives.WriteUInt32LittleEndian(body, GameActionEnvelope); - BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(4), gameActionSequence); - BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(8), PutItemInContainerOpcode); - BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(12), itemGuid); - BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(16), containerGuid); - BinaryPrimitives.WriteInt32LittleEndian (body.AsSpan(20), placement); - return body; - } -``` - -- [ ] **Step 4: Run test to verify it passes.** - -```powershell -dotnet test tests/AcDream.Core.Net.Tests/AcDream.Core.Net.Tests.csproj ` - --filter "FullyQualifiedName~InteractRequestsTests.BuildPickUp" -``` - -Expected: 1 passing, 0 failing. - -- [ ] **Step 5: Commit.** - -```powershell -git add src/AcDream.Core.Net/Messages/InteractRequests.cs ` - tests/AcDream.Core.Net.Tests/Messages/InteractRequestsTests.cs -git commit -m "feat(B.5): InteractRequests.BuildPickUp β€” PutItemInContainer 0x0019" -``` - ---- - -## Task 2: GameWindow integration β€” SendPickUp + OnInputAction case - -**Files:** -- Modify: `src/AcDream.App/Rendering/GameWindow.cs` - -This task does NOT include new tests. `SendPickUp` is a 6-line passthrough that gates on `InWorld` state and routes to `_liveSession.SendGameAction`. It mirrors `SendUse` (also untested by unit tests) and is verified end-to-end via the in-world visual check in Task 3. - -- [ ] **Step 1: Add the `SendPickUp` helper.** Locate `SendUse` (currently around line 8870 in `src/AcDream.App/Rendering/GameWindow.cs`). Insert this new helper immediately after `SendUse`'s closing brace: - -```csharp - private void SendPickUp(uint itemGuid) - { - if (_liveSession is null - || _liveSession.CurrentState != AcDream.Core.Net.WorldSession.State.InWorld) - { - _debugVm?.AddToast("Not in world"); - return; - } - var seq = _liveSession.NextGameActionSequence(); - var body = AcDream.Core.Net.Messages.InteractRequests.BuildPickUp( - seq, itemGuid, _playerServerGuid, placement: 0); - _liveSession.SendGameAction(body); - Console.WriteLine($"[B.5] pickup item=0x{itemGuid:X8} container=0x{_playerServerGuid:X8} seq={seq}"); - } -``` - -- [ ] **Step 2: Wire the F-key action.** Locate the `OnInputAction` switch's `case InputAction.UseSelected` (currently around line 8745). Insert a new case immediately after it: - -```csharp - case AcDream.UI.Abstractions.Input.InputAction.SelectionPickUp: - if (_selectedGuid is uint pickupTarget) - SendPickUp(pickupTarget); - else - _debugVm?.AddToast("Nothing selected"); - break; -``` - -- [ ] **Step 3: Build the whole solution.** - -```powershell -dotnet build -c Debug -``` - -Expected: build succeeds with zero errors. (Existing warnings unchanged.) - -- [ ] **Step 4: Run the full test suite.** - -```powershell -dotnet test -c Debug --nologo -``` - -Expected: 1047 passing, 8 failing (8 pre-existing baseline failures from main HEAD; same count as before B.5). The new `BuildPickUp_WritesOpcode0x0019AndPayload` test contributes +1 passing. - -- [ ] **Step 5: Commit.** - -```powershell -git add src/AcDream.App/Rendering/GameWindow.cs -git commit -m "feat(B.5): SendPickUp helper + F-key SelectionPickUp wiring" -``` - ---- - -## Task 3: Visual verification in live client - -**Not a code task β€” user-driven acceptance test.** Run the launch recipe, drop a test item, click-then-F. - -- [ ] **Step 1: Kill stale client + wait for ACE session cleanup.** - -```powershell -Get-Process -Name AcDream.App -ErrorAction SilentlyContinue | Stop-Process -Force -Start-Sleep -Seconds 20 -``` - -- [ ] **Step 2: Launch the client (background).** - -```powershell -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -$env:ACDREAM_TEST_HOST = "127.0.0.1" -$env:ACDREAM_TEST_PORT = "9000" -$env:ACDREAM_TEST_USER = "testaccount" -$env:ACDREAM_TEST_PASS = "testpassword" -$env:ACDREAM_DEVTOOLS = "1" -dotnet run --project src\AcDream.App\AcDream.App.csproj -c Debug 2>&1 | - Tee-Object -FilePath "launch-b5.log" -``` - -- [ ] **Step 3: User-driven test scenario.** Hand off to user for visual verification: - - 1. ACE drops a test item near `+Acdream` (server `/drop` slash command, or whatever ACE supports for the testaccount). Specify what item type was dropped in the verification report. - 2. **Single-click the item** β€” `[B.4b] pick guid=0x` should appear in the log. The selection toast should show the item name. - 3. **Press F** β€” `[B.5] pickup item=0x container=0x5000000A seq=` should appear in the log. - 4. **Item should disappear from the ground** (ACE acks β†’ `RemoveObject` β†’ existing despawn path removes it from view). - 5. **No regressions on door interaction:** double-click the inn door, observe it swings open as in B.4c. - 6. **Bonus: NPC chat check.** Click an NPC, observe the chat panel. If NPC dialogue appears β†’ M1 demo target 3 confirmed met. If silent β†’ file an issue. - -- [ ] **Step 4: Grep the log for evidence.** - -```powershell -Select-String -Path launch-b5.log -Pattern "\[B\.5\] pickup|\[B\.4b\] pick|UseDone|InventoryPutObjInContainer|RemoveObject" -``` - -Expected: at least one `[B.5] pickup` line and a subsequent `RemoveObject` for the same guid. - ---- - -## Task 4: Ship handoff + docs + merge - -- [ ] **Step 1: Write the ship handoff doc** at `docs/research/2026-05-14-b5-shipped-handoff.md`. Follow the B.4c handoff structure: TL;DR, three-commit table (TDD builder + GameWindow integration + handoff itself), wire-format evidence, visual-verification evidence (with launch log excerpt), open issues carried forward (#61 #62 from B.4c), what shipped, what was left for later. - -- [ ] **Step 2: Update `docs/plans/2026-04-11-roadmap.md`** β€” move Phase B.5 from "Next phase candidates" into the shipped-table with the merge SHA placeholder + link to the handoff doc. - -- [ ] **Step 3: Update `CLAUDE.md`** β€” update the "Currently in Phase L.2" paragraph's M1 demo status from "3 of 4 met" to "4 of 4 met", reference the B.5 handoff, and add a fresh "Next phase candidates" list (chronic-issue triage, Phase C visual fidelity, N.6 slice 2, etc.). - -- [ ] **Step 4: Update `docs/ISSUES.md`** β€” if any new issues surfaced during visual verification, file them. If the click-NPC bonus check succeeded, note it in the recent-progress section. - -- [ ] **Step 5: Commit docs.** - -```powershell -git add docs/research/2026-05-14-b5-shipped-handoff.md ` - docs/plans/2026-04-11-roadmap.md ` - CLAUDE.md ` - docs/ISSUES.md -git commit -m "docs(B.5): ship handoff + roadmap/CLAUDE update + M1 4/4 met" -``` - -- [ ] **Step 6: Merge to main.** - -```powershell -git checkout main -git merge --no-ff claude/phase-b5-pickup -m "Merge branch 'claude/phase-b5-pickup' β€” Phase B.5 pickup" -``` - -- [ ] **Step 7: Optional worktree cleanup.** (Per B.4c precedent, submodules block `git worktree remove`; do `git worktree prune` after manually deleting the directory if disk pressure warrants. Otherwise skip β€” the directory is small.) - ---- - -## Acceptance criteria summary - -- [ ] `dotnet build -c Debug` green. -- [ ] `dotnet test -c Debug` shows +1 new passing (the `BuildPickUp_WritesOpcode0x0019AndPayload` test). -- [ ] Total pass count = baseline + 1; failure count unchanged (8 pre-existing). -- [ ] Visual: click ground item β†’ F β†’ log shows `[B.5] pickup ...` β†’ item disappears. -- [ ] No regression on B.4c door interaction (double-click inn door still swings). -- [ ] Bonus: click NPC β†’ chat appears in chat panel (or filed as a follow-up issue). -- [ ] Branch merged into main with non-fast-forward merge commit. - ---- - -## Carry-overs from B.4c (do not lose track) - -- **#61** β€” AnimationSequencer linkβ†’cycle frame-0 flash. Low severity. Not blocking M1 demo. -- **#62** β€” PARTSDIAG null-guard. Latent (not reachable for doors currently). One-line fix. - -Neither blocks B.5. Address before recording the M1 demo video if the door-swing flap is distracting on tape. diff --git a/docs/superpowers/plans/2026-05-16-retail-faithfulness-fixes.md b/docs/superpowers/plans/2026-05-16-retail-faithfulness-fixes.md deleted file mode 100644 index 36dd6b1..0000000 --- a/docs/superpowers/plans/2026-05-16-retail-faithfulness-fixes.md +++ /dev/null @@ -1,1560 +0,0 @@ -# Retail-faithfulness fixes (Commit A + Commit B) Implementation Plan - -> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. - -**Goal:** Retire the retail divergences flagged in the 2026-05-16 faithfulness audit. Bring rotation rate, useability gate, AP cadence, picker geometry, and the four B.6 Tier-4 workarounds in line with the named-retail decomp. - -**Architecture:** Two coherent commits. -- **Commit A** (low-risk, no protocol change): rotation rate constants, useability gate semantics, and a diagnostic probe for the useability-fallback path. -- **Commit B** (coupled rework): retail-faithful AutonomousPosition cadence (diff-driven, grounded-gated) β†’ which retires four workarounds β†’ plus sphere-based picker using `Setup.SelectionSphere` β†’ plus deletion of two heuristics that become dead code. - -**Tech Stack:** C# .NET 10, xUnit tests, ImGui.NET (selection indicator path). All edits land on `main` (the user runs the client from there); investigation evidence is in `docs/research/named-retail/acclient_2013_pseudo_c.txt` + `acclient.h`. - -**Retail anchors** (cited throughout): -- `0x007c8914` `run_turn_factor = 1.5f` β€” rotation rate multiplier under HoldKey.Run. -- `0x4fccc0` `ItemUses::IsUseable` β€” gate semantics is `_useability != USEABLE_UNDEF (0)`. -- `0x006b3efb` `time_between_position_events = 1.0s` β€” at-rest AP heartbeat. -- `0x006b45e0` `ShouldSendPositionEvent` β€” diff-driven + interval cadence. -- `0x006b4770` `SendPositionEvent` β€” `transient_state & (CONTACT_TS | ON_WALKABLE_TS)` gate. -- `0x00588a80` `ItemHolder::UseObject` β€” single fire-and-forget `Event_UseEvent`. -- `0x00564900` `Handle_Item__UseDone` β€” server signals "use done" inbound. -- `0x0054c740` `Render::GfxObjUnderSelectionRay` β€” retail picker uses per-part `drawing_sphere` + polygon refine (we use `Setup.SelectionSphere` as a simpler equivalent for Stage A). -- `0x00518b80` `CPartArray::GetSelectionSphere` β€” scale formula. - ---- - -## File structure - -**Files touched in Commit A:** - -| File | Responsibility | -|---|---| -| `src/AcDream.Core/Physics/RemoteMoveToDriver.cs` | Add `BaseTurnRateRadPerSec`, `RunTurnFactor`, `TurnRateFor(running)` helper. Keep `TurnRateRadPerSec` as the walking-rate constant (back-compat). | -| `src/AcDream.App/Input/PlayerMovementController.cs` | Wire `TurnRateFor(input.Run)` into keyboard A/D path (line ~640-643) and `TurnRateFor(_autoWalkInitiallyRunning)` into `ApplyAutoWalkOverlay` (line ~505). | -| `src/AcDream.Core/Physics/PhysicsDiagnostics.cs` | Add `ProbeUseabilityFallbackEnabled` flag + `ACDREAM_PROBE_USEABILITY_FALLBACK` env var. | -| `src/AcDream.App/Rendering/GameWindow.cs` | Replace useability gate `& USEABLE_REMOTE_BIT` with `!= 0`; add diagnostic line in fallback branches. | -| `tests/AcDream.Core.Tests/Physics/RemoteMoveToDriverTests.cs` | New file β€” unit tests for `TurnRateFor`. | - -**Files touched in Commit B:** - -| File | Responsibility | -|---|---| -| `src/AcDream.App/Input/PlayerMovementController.cs` | Replace `_heartbeatAccum` with diff-driven `HeartbeatDue` (position/cell change + 1s heartbeat + `IsGrounded` gate). Add `NotePositionSent(pos, cellId, now)` + `SimTimeSeconds` accessor. Delete `TinyMargin` from `ApplyAutoWalkOverlay`. Add `ApproxPositionEqual` helper. | -| `src/AcDream.App/Rendering/GameWindow.cs` | Delete `OnAutoWalkArrivedReSendAction`, `SendAutonomousPositionNow`, `IsTallSceneryGuid`, the `isRetryAfterArrival` parameter on `SendUse`/`SendPickUp`. Simplify `_pendingPostArrivalAction` to close-range-deferred-Use only (no retry). Add `OnAutoWalkArrivedSendDeferredAction` (FIRST send, not retry). Call `_playerController.NotePositionSent(...)` after each `SendMoveToState` / `SendAutonomousPosition`. Wire `WorldPicker.Pick` to use `TryGetEntitySelectionSphere` (already exists at line ~9605); drop per-type `radiusForGuid` / `verticalOffsetForGuid` callbacks. | -| `src/AcDream.Core/Selection/WorldPicker.cs` | Add new `Pick(...)` overload taking `Func sphereForEntity`. Keep existing per-radius-callback overload for now (no callers after B8). | -| `src/AcDream.App/UI/TargetIndicatorPanel.cs` | Trim `EntityHeightFor` per-type branches to a single 1.5 m Γ— scale defensive default. | -| `tests/AcDream.Core.Tests/Selection/WorldPickerTests.cs` (new or modify existing) | Unit tests for the new sphere-resolver overload. | -| `docs/ISSUES.md` | File 3 deferred follow-ups (Triangle apex/size UX; Stage B polygon refine; cdb-probe to verify `omega.z = Ο€/2`). | - ---- - -## Commit A β€” Retail-faithful useability + rotation rate - -### Task A1: Add `TurnRateFor(running)` helper to `RemoteMoveToDriver` - -**Files:** -- Modify: `src/AcDream.Core/Physics/RemoteMoveToDriver.cs:77` (around `TurnRateRadPerSec` constant) -- Test: `tests/AcDream.Core.Tests/Physics/RemoteMoveToDriverTests.cs` (new file) - -- [ ] **Step 1: Write the failing test** - -Create `tests/AcDream.Core.Tests/Physics/RemoteMoveToDriverTests.cs`: - -```csharp -using AcDream.Core.Physics; - -namespace AcDream.Core.Tests.Physics; - -public sealed class RemoteMoveToDriverTests -{ - [Fact] - public void TurnRateFor_WalkingReturnsBaseRate() - { - // Retail: omega.z = Β±Ο€/2 Γ— turn_speed (1.0) = Ο€/2 rad/s β‰ˆ 90Β°/s - // Anchor: docs/research/named-retail/acclient_2013_pseudo_c.txt - // CMotionInterp::apply_run_to_command 0x00527be0 only - // multiplies under HoldKey.Run β€” walking is unscaled. - float rate = RemoteMoveToDriver.TurnRateFor(running: false); - Assert.Equal(MathF.PI / 2.0f, rate, precision: 5); - } - - [Fact] - public void TurnRateFor_RunningAppliesRunTurnFactor() - { - // Retail: omega.z = Β±Ο€/2 Γ— turn_speed Γ— run_turn_factor - // run_turn_factor = 1.5f at 0x007c8914 (PDB-named). - // apply_run_to_command (acclient_2013_pseudo_c.txt:305098) - // multiplies turn_speed by 1.5f when input is TurnRight - // under HoldKey.Run. - float rate = RemoteMoveToDriver.TurnRateFor(running: true); - Assert.Equal(MathF.PI / 2.0f * 1.5f, rate, precision: 5); - } - - [Fact] - public void TurnRateRadPerSec_BackCompatStillResolvesToWalkingRate() - { - // Existing call sites that haven't yet migrated to TurnRateFor - // (e.g., RemoteMoveToDriver.Drive's TurnSpeed=1.0 callers) still - // see the walking-rate constant. Same numerical value as - // BaseTurnRateRadPerSec. - Assert.Equal(RemoteMoveToDriver.BaseTurnRateRadPerSec, - RemoteMoveToDriver.TurnRateRadPerSec, precision: 5); - } -} -``` - -- [ ] **Step 2: Run test to verify it fails** - -Run: -``` -dotnet test tests/AcDream.Core.Tests/AcDream.Core.Tests.csproj --filter "FullyQualifiedName~RemoteMoveToDriverTests" --no-build -``` -Expected: build fails β€” `RemoteMoveToDriver.TurnRateFor` / `BaseTurnRateRadPerSec` don't exist yet. - -- [ ] **Step 3: Add the constants + helper to `RemoteMoveToDriver.cs`** - -In `src/AcDream.Core/Physics/RemoteMoveToDriver.cs`, immediately after the existing `TurnRateRadPerSec` constant declaration (around line 77), add: - -```csharp - /// - /// Retail base turn rate for the player Humanoid when turn_speed - /// scalar = 1.0. Convention default omega.z = Β±Ο€/2 rad/s - /// derived from add_motion at 0x005224b0 + the - /// HasOmega-cleared MotionData fallback documented in - /// AnimationSequencer.cs:734-741. ~90Β°/s. - /// - public const float BaseTurnRateRadPerSec = MathF.PI / 2.0f; - - /// - /// Retail's run_turn_factor constant at 0x007c8914 - /// (PDB-named). - /// equivalent (decomp 0x00527be0, line 305098 of - /// acclient_2013_pseudo_c.txt) multiplies turn_speed - /// by 1.5 when HoldKey.Run is active on a TurnRight/TurnLeft - /// command. Effect: running rotation is 50 % faster than walking. - /// - public const float RunTurnFactor = 1.5f; - - /// - /// Retail-faithful local-player turn rate. - /// - /// Walking: BaseTurnRateRadPerSec β‰ˆ 90Β°/s. - /// Running: BaseTurnRateRadPerSec Γ— RunTurnFactor - /// β‰ˆ 135Β°/s. - /// - /// Replaces the fixed TurnRateRadPerSec for paths that have - /// access to the player's run/walk state (keyboard A/D, auto-walk - /// overlay turn-first). NPC/monster remotes that lack the - /// information continue to use the constant which equals - /// BaseTurnRateRadPerSec. - /// - public static float TurnRateFor(bool running) - => running ? BaseTurnRateRadPerSec * RunTurnFactor - : BaseTurnRateRadPerSec; -``` - -Keep the existing `TurnRateRadPerSec = MathF.PI / 2.0f;` declaration as-is β€” it now numerically equals `BaseTurnRateRadPerSec` and acts as the back-compat alias for callers that don't yet know walk-vs-run. - -- [ ] **Step 4: Run test to verify it passes** - -Run: -``` -dotnet build src/AcDream.Core/AcDream.Core.csproj -c Debug -dotnet test tests/AcDream.Core.Tests/AcDream.Core.Tests.csproj --filter "FullyQualifiedName~RemoteMoveToDriverTests" --no-build -``` -Expected: 3/3 pass. - -- [ ] **Step 5: Defer commit to end of Commit A (Task A6).** - ---- - -### Task A2: Wire keyboard A/D to `TurnRateFor` - -**Files:** -- Modify: `src/AcDream.App/Input/PlayerMovementController.cs:640-643` - -- [ ] **Step 1: Replace the `WalkAnimSpeed Γ— 0.5f` formula with `TurnRateFor`** - -In `src/AcDream.App/Input/PlayerMovementController.cs` at line 640-643, replace: - -```csharp - if (input.TurnRight) - Yaw -= MotionInterpreter.WalkAnimSpeed * 0.5f * dt; // ~90Β°/s - if (input.TurnLeft) - Yaw += MotionInterpreter.WalkAnimSpeed * 0.5f * dt; -``` - -with: - -```csharp - // 2026-05-16 β€” retail-faithful turn rate. - // Anchor: docs/research/named-retail/acclient_2013_pseudo_c.txt - // - CMotionInterp::apply_run_to_command 0x00527be0 - // multiplies turn_speed by run_turn_factor (1.5) under - // HoldKey.Run on TurnRight/TurnLeft commands. - // - Base rate Β±Ο€/2 rad/s comes from add_motion 0x005224b0 - // with HasOmega-cleared MotionData fallback. - // Effective: walking β‰ˆ 90Β°/s, running β‰ˆ 135Β°/s. - // Previously: WalkAnimSpeed*0.5 β‰ˆ 89.4Β°/s β€” coincidentally - // close to retail walking but no run differentiation. - float keyboardTurnRate = RemoteMoveToDriver.TurnRateFor(input.Run); - if (input.TurnRight) - Yaw -= keyboardTurnRate * dt; - if (input.TurnLeft) - Yaw += keyboardTurnRate * dt; -``` - -- [ ] **Step 2: Build to confirm no breakage** - -Run: -``` -dotnet build src/AcDream.App/AcDream.App.csproj -c Debug -``` -Expected: build succeeds with 0 errors. - -- [ ] **Step 3: No commit yet β€” continue to A3.** - ---- - -### Task A3: Wire `ApplyAutoWalkOverlay` to `TurnRateFor` - -**Files:** -- Modify: `src/AcDream.App/Input/PlayerMovementController.cs:505` - -- [ ] **Step 1: Replace the constant with the helper** - -In `ApplyAutoWalkOverlay`, locate the line: - -```csharp - float maxStep = RemoteMoveToDriver.TurnRateRadPerSec * dt; -``` - -Replace with: - -```csharp - // 2026-05-16 β€” retail-faithful turn rate. Auto-walk knows - // its run/walk decision from _autoWalkInitiallyRunning - // (set at BeginServerAutoWalk based on initial distance vs - // WalkRunThreshold). Running rotation is 50% faster per - // run_turn_factor at retail 0x007c8914. - float maxStep = RemoteMoveToDriver.TurnRateFor(_autoWalkInitiallyRunning) * dt; -``` - -- [ ] **Step 2: Build** - -Run: -``` -dotnet build src/AcDream.App/AcDream.App.csproj -c Debug -``` -Expected: 0 errors. - ---- - -### Task A4: Add `ProbeUseabilityFallbackEnabled` diagnostic flag - -**Files:** -- Modify: `src/AcDream.Core/Physics/PhysicsDiagnostics.cs` - -- [ ] **Step 1: Add the flag following the existing pattern** - -In `src/AcDream.Core/Physics/PhysicsDiagnostics.cs`, after the existing `ProbeAutoWalkEnabled` flag (around line 121), add: - -```csharp - /// - /// 2026-05-16. Logs one line per `IsUseableTarget` call that takes - /// the null-useability fallback path (creature pass / BF_DOOR pass / - /// BF_LIFESTONE pass / etc.). Used to measure how often ACE's seed - /// DB ships entities without `_useability` set β€” settles whether - /// the fallback is live code or theoretical defense. - /// - /// - /// Retail has NO fallback; null/zero useability blocks Use entirely - /// (acclient_2013_pseudo_c.txt:402923 ItemHolder::UseObject β€” - /// IsUseable==0 falls through to "cannot be used" branch). Our - /// fallback exists because ACE genuinely sends null for many seed - /// weenies. The probe quantifies "many". - /// - /// - /// Toggle via env var ACDREAM_PROBE_USEABILITY_FALLBACK=1 - /// or DebugPanel checkbox. - /// - public static bool ProbeUseabilityFallbackEnabled { get; set; } = - Environment.GetEnvironmentVariable("ACDREAM_PROBE_USEABILITY_FALLBACK") == "1"; -``` - -- [ ] **Step 2: Build** - -Run: -``` -dotnet build src/AcDream.Core/AcDream.Core.csproj -c Debug -``` -Expected: 0 errors. - ---- - -### Task A5: Fix useability gate to `!= 0` + add fallback diagnostic - -**Files:** -- Modify: `src/AcDream.App/Rendering/GameWindow.cs` β€” function `IsUseableTarget` (search for `private bool IsUseableTarget`). - -- [ ] **Step 1: Replace the primary gate** - -Locate `IsUseableTarget` in `src/AcDream.App/Rendering/GameWindow.cs`. The body currently begins with: - -```csharp - if (_lastSpawnByGuid.TryGetValue(guid, out var spawn)) - { - // Authoritative path: server published Useability. - if (spawn.Useability is uint useability) - { - // USEABLE_REMOTE (0x20) β€” bit set in every from-world - // useable variant per acclient.h:6478 ITEM_USEABLE enum. - const uint USEABLE_REMOTE_BIT = 0x20u; - return (useability & USEABLE_REMOTE_BIT) != 0; - } - // ... fallback (ObjectDescriptionFlags + creature) ... - } -``` - -Replace the `if (spawn.Useability is uint useability)` block (the inner content, NOT the whole method) with: - -```csharp - // Authoritative path: server published Useability. - // 2026-05-16 β€” retail-faithful gate per ItemUses::IsUseable - // at acclient_2013_pseudo_c.txt:256455 (4 call-site cross- - // checks confirm: ItemHolder::UseObject 0x00588a80, - // DetermineUseResult 0x402697, UsingItem 0x367638, - // disable-button state 0x198826 β€” all key off non-zero). - // BN's `!(x) & 1` rendering is a mis-decompile of the - // setne+and test-flag inliner. Real semantic: - // - // IsUseable(_useability) := (_useability != USEABLE_UNDEF) - // - // ANY non-zero value passes (including USEABLE_NO=1, - // USEABLE_CONTAINED=8, etc.). Retail trusts the server to - // have only set non-zero on entities where Use is sensible. - // - // Previous implementation (B.8) checked - // `(useability & USEABLE_REMOTE_BIT) != 0` which is STRICTER - // than retail β€” a USEABLE_NO door would be blocked locally - // but pass retail's gate. Now matches retail bit-for-bit. - if (spawn.Useability is uint useability) - { - return useability != 0u; - } -``` - -- [ ] **Step 2: Add diagnostic to the fallback branches** - -In the same `IsUseableTarget` method, locate the fallback branches that read (paraphrased): - -```csharp - if (spawn.ObjectDescriptionFlags is { } odf) - { - const uint UseableFlatMask = 0x1000u | 0x4000u | 0x40000u | 0x2000u; - if ((odf & UseableFlatMask) != 0) return true; - } - } - - // Creatures (NPCs / players) are always Use targets ... - if (_liveEntityInfoByGuid.TryGetValue(guid, out var info) - && (info.ItemType & AcDream.Core.Items.ItemType.Creature) != 0) - { - return true; - } - - return false; -``` - -Modify to: - -```csharp - if (spawn.ObjectDescriptionFlags is { } odf) - { - const uint UseableFlatMask = 0x1000u | 0x4000u | 0x40000u | 0x2000u; - if ((odf & UseableFlatMask) != 0) - { - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeUseabilityFallbackEnabled) - Console.WriteLine(System.FormattableString.Invariant( - $"[useability-fallback] flat-class guid=0x{guid:X8} odf=0x{odf:X8} (ACE sent no useability bit)")); - return true; - } - } - } - - // Creatures (NPCs / players) are always Use targets in our - // fallback even when ACE didn't publish useability. Retail - // would have blocked here (null β†’ USEABLE_UNDEF β†’ 0 β†’ block), - // but ACE's seed DB has many talk-only NPC weenies with - // `ItemUseable = null`; without the fallback the M1 "click NPC" - // flow regresses. The diagnostic line below lets us measure - // how often this branch fires in real play. - if (_liveEntityInfoByGuid.TryGetValue(guid, out var info) - && (info.ItemType & AcDream.Core.Items.ItemType.Creature) != 0) - { - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeUseabilityFallbackEnabled) - Console.WriteLine(System.FormattableString.Invariant( - $"[useability-fallback] creature guid=0x{guid:X8} (ACE sent no useability bit)")); - return true; - } - - return false; -``` - -- [ ] **Step 3: Build** - -Run: -``` -dotnet build src/AcDream.App/AcDream.App.csproj -c Debug -``` -Expected: 0 errors. - ---- - -### Task A6: Run full test suite, visual verify, commit Commit A - -- [ ] **Step 1: Run full Core.Net test suite** - -Run: -``` -dotnet test tests/AcDream.Core.Net.Tests/AcDream.Core.Net.Tests.csproj -c Debug -``` -Expected: PASS, count >= 290 (previous baseline). - -- [ ] **Step 2: Run full Core test suite (RemoteMoveToDriver tests included)** - -Run: -``` -dotnet test tests/AcDream.Core.Tests/AcDream.Core.Tests.csproj -c Debug -``` -Expected: PASS for `RemoteMoveToDriverTests` (3/3); pre-existing failure baseline unchanged. - -- [ ] **Step 3: Visual verify** - -The user runs the client and confirms: -- Pressing W + Shift (run) + A/D: character rotates noticeably faster than W (walk) + A/D. Estimated 50% faster (135Β°/s vs 90Β°/s). -- Auto-walk to a far target: character turns at 135Β°/s during the turn-first phase. -- Pressing R on a sign: still silent no-op (Useability=0 still blocks; `0 != 0` is false). -- Pressing R on an NPC: still walks + dialogue fires (creature fallback covers null useability). -- With `ACDREAM_PROBE_USEABILITY_FALLBACK=1`: `[useability-fallback]` lines appear in console for each NPC/door interaction that takes the fallback path. - -**STOP and wait for user confirmation before committing.** - -- [ ] **Step 4: Commit A** - -When user approves, in `C:\Users\erikn\source\repos\acdream` (main checkout): - -```bash -git add src/AcDream.Core/Physics/RemoteMoveToDriver.cs \ - src/AcDream.App/Input/PlayerMovementController.cs \ - src/AcDream.Core/Physics/PhysicsDiagnostics.cs \ - src/AcDream.App/Rendering/GameWindow.cs \ - tests/AcDream.Core.Tests/Physics/RemoteMoveToDriverTests.cs -git commit -m "$(cat <<'EOF' -fix(retail): rotation rate run multiplier + useability gate semantics - -Two retail divergences from the 2026-05-16 faithfulness audit: - -1. Rotation rate ignored HoldKey.Run. Retail's CMotionInterp:: - apply_run_to_command (decomp 0x00527be0 line 305098) multiplies - turn_speed by run_turn_factor (1.5, PDB-named symbol at 0x007c8914) - when input is TurnRight/TurnLeft under HoldKey.Run. Effective - running rotation is 50% faster (~135Β°/s vs walking ~90Β°/s). - Our keyboard A/D and ApplyAutoWalkOverlay used a fixed rate. - - New: RemoteMoveToDriver.TurnRateFor(running) helper. Keyboard - path passes input.Run; auto-walk overlay passes - _autoWalkInitiallyRunning. The walking-rate base - (BaseTurnRateRadPerSec = Ο€/2) is unchanged; TurnRateRadPerSec - constant is preserved as the walking-rate alias for callers - that don't have run/walk state (NPC remotes). - -2. IsUseableTarget gated on `useability & USEABLE_REMOTE (0x20)`, - stricter than retail. Per ItemUses::IsUseable - (acclient_2013_pseudo_c.txt:256455) cross-referenced with 4 - call sites (ItemHolder::UseObject, DetermineUseResult, - UsingItem, disable-button state), retail's real gate is - `_useability != USEABLE_UNDEF (0)` β€” ANY non-zero passes. - The Binary Ninja `!(x) & 1` pseudo-C is a mis-decompile of a - setne+and test-flag inliner. Now matches retail. - -3. Added ProbeUseabilityFallbackEnabled diagnostic - (env var ACDREAM_PROBE_USEABILITY_FALLBACK=1) to log every - time the null-useability fallback fires. Settles whether the - fallback (allow creature + BF_DOOR/LIFESTONE/PORTAL/CORPSE - when ACE didn't publish useability) is live code or - theoretical defense. - -Tests: +3 RemoteMoveToDriverTests cover TurnRateFor walking/running/ -back-compat. Existing 290+ Core.Net tests unchanged. - -Plan: docs/superpowers/plans/2026-05-16-retail-faithfulness-fixes.md - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - ---- - -## Commit B β€” Retire B.6 workarounds via retail AP cadence + sphere-based picker - -### Task B1: Add diff-driven position state + `NotePositionSent` to `PlayerMovementController` - -**Files:** -- Modify: `src/AcDream.App/Input/PlayerMovementController.cs` β€” state block at line 189-191, public API. - -- [ ] **Step 1: Replace the `_heartbeatAccum` state with diff-tracking state** - -In `src/AcDream.App/Input/PlayerMovementController.cs`, the existing state declaration is: - -```csharp - private float _heartbeatAccum; - public const float HeartbeatInterval = 1.0f; // 1 sec β€” retail / holtburger - public bool HeartbeatDue { get; private set; } -``` - -Replace with: - -```csharp - /// - /// 2026-05-16 β€” retail-faithful AP cadence. Matches retail's - /// CommandInterpreter::ShouldSendPositionEvent (acclient_2013_pseudo_c.txt - /// at address 0x006b45e0) which gates on either (a) position-or-cell - /// change since the last send, or (b) at-rest 1 sec heartbeat elapsed. - /// `time_between_position_events` constant at 0x006b3efb = 1.0 sec. - /// - /// Old model: a 1 Hz idle / 10 Hz active flat accumulator. That - /// missed retail's per-frame-while-moving behaviour and forced the - /// four B.6 workarounds (arrival margin, re-send on arrival, AP - /// flush, retry flag) to compensate for the lag in ACE's server-side - /// WithinUseRadius poll. Replaced by diff-driven cadence below. - /// - public const float HeartbeatInterval = 1.0f; // retail 0x006b3efb - - private System.Numerics.Vector3 _lastSentPos; - private uint _lastSentCellId; - private float _lastSentTime; - private bool _lastSentInitialized; - private float _simTimeSeconds; - public bool HeartbeatDue { get; private set; } - - /// Sim-time accumulator (advanced by dt at the top of Update). - /// Exposed for the network outbound layer to stamp NotePositionSent. - public float SimTimeSeconds => _simTimeSeconds; -``` - -- [ ] **Step 2: Add the `NotePositionSent` API** - -In the same file, after `EndServerAutoWalk` (~line 410 β€” find a logical place near the public movement API), add: - -```csharp - /// - /// 2026-05-16. Called by the network outbound layer after every - /// AutonomousPosition or MoveToState that carries the player's - /// position. Resets the diff-driven heartbeat clock so the next - /// `HeartbeatDue` evaluation requires either a fresh position - /// change OR another full HeartbeatInterval. Mirrors retail's - /// SendPositionEvent (0x006b4770) which updates - /// `last_sent_position_time` + `last_sent_position` at every - /// send, AND SendMovementEvent (0x006b4680) which also touches - /// the same shared clock (both consumers of the 1 sec window). - /// - public void NotePositionSent(System.Numerics.Vector3 worldPos, - uint cellId, - float nowSeconds) - { - _lastSentPos = worldPos; - _lastSentCellId = cellId; - _lastSentTime = nowSeconds; - _lastSentInitialized = true; - } -``` - -- [ ] **Step 3: Build** - -Run: -``` -dotnet build src/AcDream.App/AcDream.App.csproj -c Debug -``` -Expected: 0 errors. The `_heartbeatAccum` field declaration is gone; existing references to `HeartbeatDue` still compile (Task B2 will populate it correctly). - ---- - -### Task B2: Replace `_heartbeatAccum` update with retail diff-driven cadence - -**Files:** -- Modify: `src/AcDream.App/Input/PlayerMovementController.cs:1188-1203` (the per-frame heartbeat block). - -- [ ] **Step 1: Add `_simTimeSeconds` increment at top of `Update`** - -Locate the start of `Update(float dt, ...)` and add immediately after entry: - -```csharp - _simTimeSeconds += dt; -``` - -- [ ] **Step 2: Replace the accumulator update** - -Locate the existing code around line 1188-1203 that reads (paraphrased): - -```csharp - _heartbeatAccum += dt; - bool activelyMoving = _autoWalkActive - || input.Forward || input.Backward - || input.StrafeLeft || input.StrafeRight - || input.TurnLeft || input.TurnRight; - float effectiveInterval = activelyMoving ? 0.1f : HeartbeatInterval; - HeartbeatDue = _heartbeatAccum >= effectiveInterval; - if (HeartbeatDue) _heartbeatAccum = 0f; -``` - -Replace with: - -```csharp - // 2026-05-16 β€” retail diff-driven AP cadence (acclient_2013_pseudo_c.txt - // 0x006b45e0 ShouldSendPositionEvent + 0x006b4770 SendPositionEvent). - // - // Rules: - // - When interval elapsed (>= 1 sec since last send): send. - // - When interval NOT elapsed: send only if position or cell - // differs from last_sent (Frame::is_equal check at - // acclient_2013_pseudo_c.txt:700248-700265). - // - SendPositionEvent gates on transient_state & - // (CONTACT_TS | ON_WALKABLE_TS) β€” i.e., grounded on a - // walkable surface. Airborne suppresses AP entirely. - // MoveToState carries jump/fall snapshots while airborne. - // - // Effective rate: per-frame while moving on the ground, 1 Hz at-rest - // heartbeat, 0 Hz airborne. Retires the 1 Hz / 10 Hz flat model. - // - // If NotePositionSent has never been called (no network session), - // _lastSentInitialized stays false and we treat every frame as - // "first send" β€” HeartbeatDue fires once per frame, which matches - // "send if anything to send" semantics. - - bool intervalElapsed = !_lastSentInitialized - || (_simTimeSeconds - _lastSentTime) >= HeartbeatInterval; - - bool positionChanged = - !_lastSentInitialized - || _lastSentCellId != CellId - || !ApproxPositionEqual(_lastSentPos, _body.Position); - - // Grounded-on-walkable. Retail's CONTACT_TS + ON_WALKABLE_TS - // (acclient.h:3688). Our equivalent: PhysicsBody.IsGrounded. - bool groundedOnWalkable = _body.IsGrounded; - - HeartbeatDue = groundedOnWalkable && (positionChanged || intervalElapsed); -``` - -If `PhysicsBody.IsGrounded` doesn't exist, search the codebase for the equivalent predicate: - -``` -grep -n "IsGrounded\|OnGround\|HasContact\|GroundContact" src/AcDream.Core/Physics/PhysicsBody.cs -``` - -Use whichever exists. If neither, derive from `_body.ContactPlane.Normal.Z > 0.5f` (humanoid walkable-normal threshold per retail FloorZ β‰ˆ 0.66). Document the choice in a comment at the call site. - -- [ ] **Step 3: Add `ApproxPositionEqual` helper** - -In the same file, near the end of the class (after the existing helper methods), add: - -```csharp - /// - /// 2026-05-16. Position-equality test for diff-driven AP cadence. - /// Retail uses Frame::is_equal at acclient_2013_pseudo_c.txt:700263 - /// which is essentially exact float comparison after a memcmp of - /// the frame struct. For floating-point safety we use a tiny epsilon - /// β€” sub-millimeter β€” that's well below any movement we'd want to - /// suppress sending for. - /// - private static bool ApproxPositionEqual( - System.Numerics.Vector3 a, System.Numerics.Vector3 b) - { - const float Epsilon = 0.001f; // 1 mm - return MathF.Abs(a.X - b.X) < Epsilon - && MathF.Abs(a.Y - b.Y) < Epsilon - && MathF.Abs(a.Z - b.Z) < Epsilon; - } -``` - -- [ ] **Step 4: Build** - -Run: -``` -dotnet build src/AcDream.App/AcDream.App.csproj -c Debug -``` -Expected: 0 errors. - ---- - -### Task B3: Wire `GameWindow` to call `NotePositionSent` - -**Files:** -- Modify: `src/AcDream.App/Rendering/GameWindow.cs` β€” find the outbound network send sites. - -- [ ] **Step 1: Identify send call sites** - -Run: -``` -grep -n "SendAutonomousPosition\|SendMoveToState\|_playerController.HeartbeatDue\|SendGameAction" src/AcDream.App/Rendering/GameWindow.cs | head -20 -``` - -Note line numbers β€” typically there's: -- One heartbeat-driven AP send site (around line 6310 area). -- One input-driven MoveToState site (around the `MotionStateChanged` check). - -- [ ] **Step 2: After each successful AP/MoveToState send, call `NotePositionSent`** - -Immediately after each `_liveSession.SendGameAction(body)` for AP and MoveToState, add: - -```csharp - _playerController.NotePositionSent( - worldPos: _playerController.Position, - cellId: _playerController.CellId, - nowSeconds: _playerController.SimTimeSeconds); -``` - -For the AP heartbeat site, this is post-send. For the MoveToState site, same β€” also stamps the clock. - -NOTE: Do NOT add this for outbound Use / PickUp / JumpAction packets β€” those don't carry position. - -- [ ] **Step 3: Build** - -Run: -``` -dotnet build src/AcDream.App/AcDream.App.csproj -c Debug -``` -Expected: 0 errors. - ---- - -### Task B4: Delete `TinyMargin` from `ApplyAutoWalkOverlay` - -**Files:** -- Modify: `src/AcDream.App/Input/PlayerMovementController.cs:466-472` - -- [ ] **Step 1: Remove the margin clamp** - -Locate lines 466-472: - -```csharp - const float TinyMargin = 0.05f; - float effectiveArrival = MathF.Max(arrivalThreshold - TinyMargin, 0.1f); - bool withinArrival = - (_autoWalkMoveTowards - && dist <= effectiveArrival) - || (!_autoWalkMoveTowards - && dist >= arrivalThreshold + RemoteMoveToDriver.ArrivalEpsilon); -``` - -Replace with: - -```csharp - // 2026-05-16 β€” retail "stop at the radius" semantics. - // Previously had a 0.05 m TinyMargin inside the threshold to - // ensure ACE's server-side WithinUseRadius poll saw us inside - // the radius before our next AP heartbeat. With the - // diff-driven AP cadence (Task B2) ACE sees the final position - // the same frame we arrive β€” no margin needed. Retail's - // arrival check is `dist <= radius` exact at - // CMotionInterp::apply_interpreted_movement integration. - bool withinArrival = - (_autoWalkMoveTowards - && dist <= arrivalThreshold) - || (!_autoWalkMoveTowards - && dist >= arrivalThreshold + RemoteMoveToDriver.ArrivalEpsilon); -``` - -- [ ] **Step 2: Build** - -Run: -``` -dotnet build src/AcDream.App/AcDream.App.csproj -c Debug -``` -Expected: 0 errors. - ---- - -### Task B5: Delete `OnAutoWalkArrivedReSendAction` + `SendAutonomousPositionNow` - -**Files:** -- Modify: `src/AcDream.App/Rendering/GameWindow.cs` (delete two methods + subscription) - -- [ ] **Step 1: Find the subscription** - -Run: -``` -grep -n "AutoWalkArrived" src/AcDream.App/Rendering/GameWindow.cs -``` -Expected: at least one `+=` subscription site and the method definition at line ~9302. - -- [ ] **Step 2: Delete the subscription line** - -In `GameWindow.cs`, find the line: - -```csharp - _playerController.AutoWalkArrived += OnAutoWalkArrivedReSendAction; -``` - -Delete it (entire line). (A NEW subscription is added in Task B6 Step 4 β€” keep that file region noted.) - -- [ ] **Step 3: Delete the `OnAutoWalkArrivedReSendAction` method** - -Locate the method at line ~9302: - -```csharp - private void OnAutoWalkArrivedReSendAction() - { - if (_pendingPostArrivalAction is not (uint guid, bool isPickup) pending) - return; - _pendingPostArrivalAction = null; - // ... probe log ... - SendAutonomousPositionNow(); - // ... probe log ... - if (isPickup) SendPickUp(guid, isRetryAfterArrival: true); - else SendUse(guid, isRetryAfterArrival: true); - } -``` - -Delete the entire method (including any doc-comment block above it). - -- [ ] **Step 4: Delete `SendAutonomousPositionNow`** - -Locate the method at line ~9424. Delete the entire method. With retail-faithful diff-driven cadence (Task B2), the next regular AP send carries the arrived position naturally. - -- [ ] **Step 5: Build** - -Run: -``` -dotnet build src/AcDream.App/AcDream.App.csproj -c Debug -``` -Expected: build will likely FAIL with "cannot find SendAutonomousPositionNow" or "cannot find OnAutoWalkArrivedReSendAction" or similar β€” those errors get fixed in B6. - ---- - -### Task B6: Remove `isRetryAfterArrival` from `SendUse`/`SendPickUp`; simplify deferred-Use to close-range turn-first only - -**Files:** -- Modify: `src/AcDream.App/Rendering/GameWindow.cs` β€” methods `SendUse` and `SendPickUp` (lines ~9162 and ~9226). - -- [ ] **Step 1: Replace `SendUse` body** - -Current method signature: `private void SendUse(uint guid, bool isRetryAfterArrival = false)`. Change to `private void SendUse(uint guid)` and the body to: - -```csharp - private void SendUse(uint guid) - { - if (_liveSession is null - || _liveSession.CurrentState != AcDream.Core.Net.WorldSession.State.InWorld) - { - _debugVm?.AddToast("Not in world"); - return; - } - - // Retail-faithful useability gate (acclient_2013_pseudo_c.txt:256455 - // ItemUses::IsUseable). Signs / banners with useability=0 silently - // ignore Use. - if (!IsUseableTarget(guid)) - { - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeAutoWalkEnabled) - Console.WriteLine($"[B.4b] SendUse ignored β€” not useable guid=0x{guid:X8}"); - return; - } - - // B.6 (2026-05-15): install speculative local auto-walk against - // the target so close-range Use rotates the body to face before - // the action fires. For FAR targets, ACE's CreateMoveToChain - // (Player_Move.cs:37-179) takes over via inbound MovementType=6 - // and our overlay is overwritten by ACE's wire-supplied radius. - // - // 2026-05-16: simplified β€” close-range deferral now fires the - // wire packet ONCE on AutoWalkArrived (turn-first done), not a - // retry of an earlier failed send. No re-send path. - bool closeRange = IsCloseRangeTarget(guid); - InstallSpeculativeTurnToTarget(guid); - - if (closeRange) - { - // Defer the wire packet β€” OnAutoWalkArrivedSendDeferredAction - // will fire it after rotation completes. - _pendingPostArrivalAction = (guid, false); - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeAutoWalkEnabled) - Console.WriteLine($"[B.4b] use deferred (close-range, turn-first) guid=0x{guid:X8}"); - return; - } - - // Far range: fire immediately. ACE auto-walks server-side; the - // retail-faithful AP cadence keeps ACE's WithinUseRadius poll - // in sync, so the action completes when the body arrives - // without any client-side re-send. - var seq = _liveSession.NextGameActionSequence(); - var body = AcDream.Core.Net.Messages.InteractRequests.BuildUse(seq, guid); - _liveSession.SendGameAction(body); - Console.WriteLine($"[B.4b] use guid=0x{guid:X8} seq={seq}"); - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeAutoWalkEnabled) - { - string label = DescribeLiveEntity(guid); - Console.WriteLine(System.FormattableString.Invariant( - $"[autowalk-out] op=use target=0x{guid:X8} name=\"{label}\" seq={seq}")); - } - } -``` - -- [ ] **Step 2: Replace `SendPickUp` body the same way** - -Drop the `isRetryAfterArrival` parameter. Final shape: - -```csharp - private void SendPickUp(uint itemGuid) - { - if (_liveSession is null - || _liveSession.CurrentState != AcDream.Core.Net.WorldSession.State.InWorld) - { - _debugVm?.AddToast("Not in world"); - return; - } - if (!IsUseableTarget(itemGuid)) - { - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeAutoWalkEnabled) - Console.WriteLine($"[B.5] SendPickUp ignored β€” not useable item=0x{itemGuid:X8}"); - return; - } - - // Block creature pickup (silent β€” matches retail). - if (IsLiveCreatureTarget(itemGuid)) - return; - - bool closeRange = IsCloseRangeTarget(itemGuid); - InstallSpeculativeTurnToTarget(itemGuid); - - if (closeRange) - { - _pendingPostArrivalAction = (itemGuid, true); - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeAutoWalkEnabled) - Console.WriteLine($"[B.5] pickup deferred (close-range, turn-first) item=0x{itemGuid:X8}"); - return; - } - - var seq = _liveSession.NextGameActionSequence(); - var body = AcDream.Core.Net.Messages.InteractRequests.BuildPickUp( - seq, itemGuid, _playerServerGuid, placement: 0); - _liveSession.SendGameAction(body); - Console.WriteLine($"[B.5] pickup item=0x{itemGuid:X8} container=0x{_playerServerGuid:X8} seq={seq}"); - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeAutoWalkEnabled) - { - string label = DescribeLiveEntity(itemGuid); - Console.WriteLine(System.FormattableString.Invariant( - $"[autowalk-out] op=pickup target=0x{itemGuid:X8} name=\"{label}\" seq={seq}")); - } - } -``` - -- [ ] **Step 3: Add `OnAutoWalkArrivedSendDeferredAction` handler** - -Add this method to `GameWindow.cs` (anywhere in the class body β€” near the deleted `OnAutoWalkArrivedReSendAction` site is fine): - -```csharp - /// - /// 2026-05-16. Fires the deferred close-range Use/PickUp action - /// once the local auto-walk overlay reports arrival (i.e. the body - /// has finished rotating to face the target). Unlike the old - /// `OnAutoWalkArrivedReSendAction`, this is a FIRST send β€” not a - /// retry of an earlier failed send. Far-range Use/PickUp paths - /// fire the wire packet immediately at `SendUse`/`SendPickUp` time - /// and never touch `_pendingPostArrivalAction`. - /// - private void OnAutoWalkArrivedSendDeferredAction() - { - if (_pendingPostArrivalAction is not (uint guid, bool isPickup) pending) - return; - _pendingPostArrivalAction = null; - - if (_liveSession is null - || _liveSession.CurrentState != AcDream.Core.Net.WorldSession.State.InWorld) - return; - - var seq = _liveSession.NextGameActionSequence(); - if (isPickup) - { - var body = AcDream.Core.Net.Messages.InteractRequests.BuildPickUp( - seq, guid, _playerServerGuid, placement: 0); - _liveSession.SendGameAction(body); - Console.WriteLine($"[B.5] pickup-deferred item=0x{guid:X8} container=0x{_playerServerGuid:X8} seq={seq}"); - } - else - { - var body = AcDream.Core.Net.Messages.InteractRequests.BuildUse(seq, guid); - _liveSession.SendGameAction(body); - Console.WriteLine($"[B.4b] use-deferred guid=0x{guid:X8} seq={seq}"); - } - } -``` - -- [ ] **Step 4: Subscribe to `AutoWalkArrived`** - -Find where `_playerController` is constructed and other subscriptions happen (the deleted subscription from B5 Step 2 was here). Add: - -```csharp - _playerController.AutoWalkArrived += OnAutoWalkArrivedSendDeferredAction; -``` - -- [ ] **Step 5: Build** - -Run: -``` -dotnet build src/AcDream.App/AcDream.App.csproj -c Debug -``` -Expected: 0 errors. If there are remaining "isRetryAfterArrival" references, fix them β€” those are callers that need to drop the named-argument. - ---- - -### Task B7: Add `WorldPicker.Pick` overload taking sphere-resolver - -**Files:** -- Modify: `src/AcDream.Core/Selection/WorldPicker.cs` -- Test: `tests/AcDream.Core.Tests/Selection/WorldPickerTests.cs` (modify or create) - -- [ ] **Step 1: Write the failing test** - -Find or create `tests/AcDream.Core.Tests/Selection/WorldPickerTests.cs`. Add: - -```csharp -using System.Numerics; -using AcDream.Core.Selection; -using AcDream.Core.World; - -namespace AcDream.Core.Tests.Selection; - -public sealed class WorldPickerSphereOverloadTests -{ - [Fact] - public void Pick_SphereResolver_ReturnsNearestHit() - { - // Two entities along the +Y axis. Sphere-resolver gives each a - // tight world-space sphere centered on the entity. A ray from - // origin pointing along +Y should hit the closer entity first. - var e1 = new WorldEntity { ServerGuid = 0x10001u, Position = new Vector3(0, 5, 0), Scale = 1f }; - var e2 = new WorldEntity { ServerGuid = 0x10002u, Position = new Vector3(0, 15, 0), Scale = 1f }; - - var origin = new Vector3(0, 0, 0); - var dir = new Vector3(0, 1, 0); - Vector3 SphereCenter(WorldEntity e) => e.Position + new Vector3(0, 0, 0.9f); - - uint? picked = WorldPicker.Pick( - origin, dir, new[] { e1, e2 }, - skipServerGuid: 0u, - sphereForEntity: e => ((Vector3, float)?)(SphereCenter(e), 1.0f)); - - Assert.Equal(0x10001u, picked); - } - - [Fact] - public void Pick_SphereResolver_NullSkipsCandidates() - { - // Resolver returning null should make the picker skip the - // candidate (matches retail "no Setup β†’ not pickable" behaviour). - var e1 = new WorldEntity { ServerGuid = 0x10001u, Position = new Vector3(0, 5, 0), Scale = 1f }; - var e2 = new WorldEntity { ServerGuid = 0x10002u, Position = new Vector3(0, 10, 0), Scale = 1f }; - - var origin = new Vector3(0, 0, 0); - var dir = new Vector3(0, 1, 0); - - uint? picked = WorldPicker.Pick( - origin, dir, new[] { e1, e2 }, - skipServerGuid: 0u, - sphereForEntity: e => e.ServerGuid == 0x10001u - ? ((Vector3, float)?)null - : ((Vector3, float)?)(e.Position + new Vector3(0, 0, 0.9f), 1.0f)); - - Assert.Equal(0x10002u, picked); - } - - [Fact] - public void Pick_SphereResolver_RespectsSkipServerGuid() - { - var e1 = new WorldEntity { ServerGuid = 0x50000001u, Position = new Vector3(0, 5, 0), Scale = 1f }; - var e2 = new WorldEntity { ServerGuid = 0x10002u, Position = new Vector3(0, 10, 0), Scale = 1f }; - - var origin = new Vector3(0, 0, 0); - var dir = new Vector3(0, 1, 0); - - uint? picked = WorldPicker.Pick( - origin, dir, new[] { e1, e2 }, - skipServerGuid: 0x50000001u, // skip player - sphereForEntity: e => ((Vector3, float)?)(e.Position + new Vector3(0, 0, 0.9f), 1.0f)); - - Assert.Equal(0x10002u, picked); - } -} -``` - -NOTE: If `WorldEntity` is `init`-only / immutable, adjust the test constructor calls accordingly β€” check `src/AcDream.Core/World/WorldEntity.cs` for the actual constructor / object-initializer pattern. The tests assume property initializers are allowed; if not, switch to whatever constructor the type exposes. - -- [ ] **Step 2: Run tests to verify they fail** - -Run: -``` -dotnet test tests/AcDream.Core.Tests/AcDream.Core.Tests.csproj --filter "FullyQualifiedName~WorldPickerSphereOverloadTests" --no-build -``` -Expected: build failure ("sphereForEntity parameter not found") or runtime failure. - -- [ ] **Step 3: Add the new `Pick` overload to `WorldPicker.cs`** - -Append to `src/AcDream.Core/Selection/WorldPicker.cs` (do NOT delete the existing `Pick` overload β€” it stays for back-compat; its consumers are removed in Task B8): - -```csharp - /// - /// 2026-05-16. Retail-faithful picker overload. Caller supplies a - /// per-entity world-space sphere via - /// β€” typically scaled by entity - /// scale and rotated into world space (mirroring retail - /// CPartArray::GetSelectionSphere at 0x00518b80). Resolver returning - /// null skips the candidate (matches retail "no Setup β†’ not pickable"). - /// - /// - /// Replaces the older - /// overload with the per-type-radius / vertical-offset heuristics. - /// Those heuristics existed because we didn't have the dat-supplied - /// SelectionSphere plumbed through. With this overload, the click - /// geometry matches what the target indicator draws β€” what you see - /// is what you click. - /// - /// - /// - /// Stage A of the picker port. Retail also does a polygon-accurate - /// refine via CPolygon::polygon_hits_ray when the sphere - /// hits (decomp 0x0054c889) β€” that's Stage B, deferred until visual - /// testing surfaces a sphere-only miss (issue #71). - /// - /// - public static uint? Pick( - System.Numerics.Vector3 origin, System.Numerics.Vector3 direction, - IEnumerable candidates, - uint skipServerGuid, - Func sphereForEntity, - float maxDistance = 50f) - { - if (direction.LengthSquared() < 1e-10f) return null; - - uint? bestGuid = null; - float bestT = float.PositiveInfinity; - foreach (var entity in candidates) - { - if (entity.ServerGuid == 0u) continue; - if (entity.ServerGuid == skipServerGuid) continue; - - var sphere = sphereForEntity(entity); - if (sphere is null) continue; - - var (center, radius) = sphere.Value; - if (radius <= 0f) continue; - - // Geometric ray-sphere (same math as the older overload). - var oc = origin - center; - float b = System.Numerics.Vector3.Dot(oc, direction); - float c = System.Numerics.Vector3.Dot(oc, oc) - radius * radius; - float d = b * b - c; - if (d < 0f) continue; - float sqrtD = MathF.Sqrt(d); - float t = -b - sqrtD; - if (t < 0f) t = -b + sqrtD; // ray origin inside sphere β†’ use far exit - if (t < 0f) continue; // both roots behind ray - if (t >= maxDistance) continue; - if (t < bestT) - { - bestT = t; - bestGuid = entity.ServerGuid; - } - } - return bestGuid; - } -``` - -- [ ] **Step 4: Run tests to verify they pass** - -Run: -``` -dotnet build tests/AcDream.Core.Tests/AcDream.Core.Tests.csproj -c Debug -dotnet test tests/AcDream.Core.Tests/AcDream.Core.Tests.csproj --filter "FullyQualifiedName~WorldPickerSphereOverloadTests" --no-build -``` -Expected: 3/3 pass. - ---- - -### Task B8: Switch `GameWindow` picker call to the sphere-resolver overload - -**Files:** -- Modify: `src/AcDream.App/Rendering/GameWindow.cs` β€” find the `WorldPicker.Pick` call. - -- [ ] **Step 1: Locate the existing call** - -Run: -``` -grep -n "WorldPicker.Pick(" src/AcDream.App/Rendering/GameWindow.cs -``` - -- [ ] **Step 2: Replace the call with the sphere-resolver overload** - -Replace the existing `WorldPicker.Pick(...)` invocation (which uses `radiusForGuid` + `verticalOffsetForGuid` callbacks) with: - -```csharp - var picked = AcDream.Core.Selection.WorldPicker.Pick( - origin, direction, - _entitiesByServerGuid.Values, - skipServerGuid: _playerServerGuid, - sphereForEntity: e => - TryGetEntitySelectionSphere(e.ServerGuid, out var c, out var r) - ? ((System.Numerics.Vector3, float)?)(c, r) - : ((System.Numerics.Vector3, float)?)null, - maxDistance: 50f); -``` - -- [ ] **Step 3: Delete the per-type `radiusForGuid` and `verticalOffsetForGuid` lambda blocks** - -Both lambdas (around line ~9037 and ~9054) become dead with this change. Delete them. - -- [ ] **Step 4: Build** - -Run: -``` -dotnet build src/AcDream.App/AcDream.App.csproj -c Debug -``` -Expected: 0 errors. The old `WorldPicker.Pick` overload with `radiusForGuid` callback still exists in `WorldPicker.cs` but has no callers β€” leave it for now; not blocking. - ---- - -### Task B9: Trim `EntityHeightFor` fallback to a single defensive default - -**Files:** -- Modify: `src/AcDream.App/UI/TargetIndicatorPanel.cs` β€” function `EntityHeightFor`. - -- [ ] **Step 1: Simplify the per-type cascade** - -Locate `EntityHeightFor` in `TargetIndicatorPanel.cs`. It currently has per-type branches (Creature 1.8m, Door/Lifestone/Portal/Corpse 2.4m, small items 0.8m, default 3.0m). With the sphere-projection path handling every entity that has a `SelectionSphere`, this method is now a defensive rescue path only. - -Replace the entire method body with: - -```csharp - /// - /// Defensive fallback height when the entity has no usable - /// SelectionSphere (Radius ≀ 1e-4f). With B.7's sphere-projection - /// path active (since commit f4f4143), this fallback only fires - /// for entities whose Setup didn't bake a selection sphere β€” - /// rare in practice. The single 1.5 m Γ— scale default is a sane - /// midpoint; per-type branches were retired in the 2026-05-16 - /// Commit B because the sphere path is authoritative. - /// - public float EntityHeightFor(uint itemType, uint pwdBitfield, float scale, uint? useability = null) - { - if (scale <= 0f) scale = 1f; - return 1.5f * scale; - } -``` - -Keep all the SmallItemMask / TallStructureMask / Creature constants out β€” they're no longer needed. The method signature stays unchanged for ABI compat with any external callers. - -- [ ] **Step 2: Build** - -Run: -``` -dotnet build src/AcDream.App/AcDream.App.csproj -c Debug -``` -Expected: 0 errors. - ---- - -### Task B10: Delete `IsTallSceneryGuid` - -**Files:** -- Modify: `src/AcDream.App/Rendering/GameWindow.cs` β€” method `IsTallSceneryGuid` and all callers. - -- [ ] **Step 1: Find call sites** - -Run: -``` -grep -n "IsTallSceneryGuid" src/AcDream.App/Rendering/GameWindow.cs -``` - -Expected hits: -- Definition (~line 9550-9610 area) -- Two call sites inside the (deleted in B8) `radiusForGuid` / `verticalOffsetForGuid` lambdas β€” should be gone after B8 -- One call site in the `[B.7] pick-info` diagnostic line at ~line 9105 - -- [ ] **Step 2: Remove `IsTallSceneryGuid` from the pick-info diagnostic** - -In the `[B.7] pick-info` log, find the line: - -```csharp - Console.WriteLine(System.FormattableString.Invariant( - $"[B.7] pick-info guid=0x{guid:X8} itemType=0x{rawItemType:X8} pwd=0x{pwdBits:X8} use={useStr} useRadius={radStr} scale={pickScale:F2} setup={setupStr} tallScenery={IsTallSceneryGuid(guid)} color=({col.R},{col.G},{col.B})")); -``` - -Replace with (drop the `tallScenery=` field): - -```csharp - Console.WriteLine(System.FormattableString.Invariant( - $"[B.7] pick-info guid=0x{guid:X8} itemType=0x{rawItemType:X8} pwd=0x{pwdBits:X8} use={useStr} useRadius={radStr} scale={pickScale:F2} setup={setupStr} color=({col.R},{col.G},{col.B})")); -``` - -- [ ] **Step 3: Delete the `IsTallSceneryGuid` method** - -Find the method definition (the comment block above it starts with "2026-05-15. True when the entity is 'tall scenery'..."). Delete the entire method (comment block + body). - -- [ ] **Step 4: Build** - -Run: -``` -dotnet build src/AcDream.App/AcDream.App.csproj -c Debug -``` -Expected: 0 errors. No remaining `IsTallSceneryGuid` references. - ---- - -### Task B11: Run full test suite, visual verify, commit Commit B - -- [ ] **Step 1: Run full test suite** - -Run: -``` -dotnet test tests/AcDream.Core.Net.Tests/AcDream.Core.Net.Tests.csproj -c Debug -dotnet test tests/AcDream.Core.Tests/AcDream.Core.Tests.csproj -c Debug -``` -Expected: Core.Net 290+ pass; Core baseline unchanged + 3 new WorldPickerSphereOverloadTests pass. - -- [ ] **Step 2: Visual verify with `ACDREAM_PROBE_USEABILITY_FALLBACK=1` + `ACDREAM_PROBE_AUTOWALK=1`** - -User runs the client and confirms: -1. **Far-range Use on NPC.** Click NPC at 8 m, press R. Expected: walks, turns to face, dialogue fires. Log shows ONE `[B.4b] use` line, NOT multiple. No `use-deferred` line (far-range fires immediately). -2. **Close-range Use on NPC behind player.** Stand within 2 m of NPC facing away. Press R. Expected: character turns 180Β°, then dialogue fires. Log shows `[B.4b] use deferred (close-range, turn-first)` followed by `[B.4b] use-deferred` (the deferred fire on arrival). Exactly ONE deferred fire. -3. **Open inn door from across the room.** Walks, opens. ONE `[B.4b] use` line. -4. **Pickup item from across the room.** Walks, picks up. ONE `[B.5] pickup` line. -5. **Click the Holtburg sign.** Indicator triangles match the sign size (unchanged from previous ship). Press R. Silent no-op (`SendUse ignored β€” not useable`). -6. **Click rapidly between NPC and item.** No spurious "I clicked X earlier and now it's firing on Y" cross-contamination. The `_pendingPostArrivalAction` simplification should make this clean. - -**If any of (1)-(6) regresses, the cadence fix in Task B2 likely needs tuning. Common cause: `IsGrounded` is too restrictive (suppressing AP on slopes); relax to `ContactPlane.Normal.Z > 0.3f` or similar.** - -**STOP and wait for user confirmation before committing.** - -- [ ] **Step 3: Commit B** - -When user approves: - -```bash -git add src/AcDream.App/Input/PlayerMovementController.cs \ - src/AcDream.App/Rendering/GameWindow.cs \ - src/AcDream.Core/Selection/WorldPicker.cs \ - src/AcDream.App/UI/TargetIndicatorPanel.cs \ - tests/AcDream.Core.Tests/Selection/WorldPickerTests.cs -git commit -m "$(cat <<'EOF' -fix(retail): per-tick AP cadence + sphere picker retires 4 workarounds - -Single coherent commit. Audit findings from 2026-05-16: - -1. AutonomousPosition cadence was 1 Hz idle / 10 Hz active flat. - Retail (CommandInterpreter::ShouldSendPositionEvent at - acclient_2013_pseudo_c.txt:0x006b45e0) is diff-driven: - - Position or cell changed since last send β†’ send (per frame - while moving, 30-60 Hz in practice). - - Otherwise, 1 sec heartbeat (time_between_position_events - constant at 0x006b3efb). - - Gated on transient_state & (CONTACT_TS | ON_WALKABLE_TS) β€” - suppressed airborne. - Replaced HeartbeatAccum with diff-driven HeartbeatDue + - NotePositionSent (resets the clock at every wire send, including - MoveToState β€” matches retail's SendMovementEvent 0x006b4680 - sharing the same `last_sent_position_time` clock). - -2. The diff-driven cadence retires all 4 B.6/B.7 workarounds: - - TinyMargin (0.05 m inside arrival): retail stops at the radius - exact; the safety margin existed because ACE's WithinUseRadius - poll missed our infrequent position updates. With per-frame - AP while moving, ACE sees us arrive the same frame. - - OnAutoWalkArrivedReSendAction: retail's Event_UseEvent - (acclient_2013_pseudo_c.txt:0x00588a80 ItemHolder::UseObject - line 403043) is single-fire, fire-and-forget. Arrival is - signaled INBOUND by the server via 0xF63E UseDone - (Handle_Item__UseDone 0x00564900) β€” the client doesn't - re-send. Our re-send was actively re-triggering ACE's - MoveToChain via StopExistingMoveToChains, masking the - cadence bug. - - SendAutonomousPositionNow flush: retail has no flush event; - per-frame AP while moving makes it unnecessary. - - isRetryAfterArrival flag: plumbing for the re-send; - deleted with it. - - Close-range turn-first deferred Use is KEPT (it IS retail β€” - ACE's Player_Move.cs:66-87 Rotate(target) before callback - mirrors retail's CreateMoveToChain pre-callback rotation). - Renamed to OnAutoWalkArrivedSendDeferredAction to clarify - it's a FIRST send, not a retry. - -3. WorldPicker switched to a Setup.SelectionSphere overload. - Retail's picker uses CGfxObj.drawing_sphere + polygon refine - (acclient_2013_pseudo_c.txt:0x0054c740 GfxObjUnderSelectionRay), - which we approximate via Setup.SelectionSphere (same data - path as the target indicator since f4f4143). Effect: click - geometry matches the visible indicator β€” what you see is what - you click. Retires the per-type radius (1.0/1.5/2.0 m) and - vertical-offset (0.9/1.0/1.5 m) heuristic callbacks. - -4. EntityHeightFor fallback trimmed to a single 1.5 m default. - IsTallSceneryGuid deleted entirely β€” both became dead code - when the picker switched to SelectionSphere. - -Test suite: 290+ Core.Net unchanged, +3 WorldPickerSphereOverloadTests. - -Plan: docs/superpowers/plans/2026-05-16-retail-faithfulness-fixes.md - -Co-Authored-By: Claude Opus 4.7 (1M context) -EOF -)" -``` - ---- - -## Deferred follow-ups (file as issues, not in this plan's commits) - -### Task DF1: File issue β€” triangle apex/size UX - -- [ ] **Step 1: Append issue to `docs/ISSUES.md`** - -After the existing "Active issues" section, add a new entry: - -```markdown -## #70 β€” Triangle apex/size β€” final retail-feel UX pass - -**Status:** OPEN -**Severity:** LOW (cosmetic β€” indicator already retail-anchored, this is final-feel polish) -**Filed:** 2026-05-16 -**Component:** ui / target indicator - -**Description:** Per 2026-05-16 user feedback during the -SelectionSphere indicator ship, the triangle apex direction (flipped -to point inward at the target) and sprite size (currently 8 px legs) -are heuristic visual choices. Retail uses an actual DAT sprite from -`UIRegion::GetChild(0x1000003a/3b/3c)` β€” the bitmap shape and size -come from the dat, not constants. - -**Acceptance:** Extract the retail triangle sprite from the dat -(probably via `tools/UiLayoutMockup` or a new `DatSpriteProbe`) and -either (a) blit the exact bitmap, or (b) pick a procedural size + -shape that matches it pixel-for-pixel at standard zoom. - -**Estimated scope:** Small (~1-2 hours, mostly dat exploration). -Not blocking M1. -``` - -### Task DF2: File issue β€” picker Stage B polygon refine - -- [ ] **Step 1: Append issue to `docs/ISSUES.md`** - -```markdown -## #71 β€” WorldPicker Stage B β€” polygon refine for retail-accurate clicks - -**Status:** OPEN -**Severity:** LOW (Stage A β€” sphere picker β€” is sufficient for M1) -**Filed:** 2026-05-16 -**Component:** selection / picker - -**Description:** Retail's mouse picker does two-tier sphere-then-polygon -selection (acclient_2013_pseudo_c.txt:0x0054c740 -`Render::GfxObjUnderSelectionRay`): -1. Sphere reject via `CGfxObj::drawing_sphere`. -2. Polygon-accurate refine via `CPolygon::polygon_hits_ray` on every - visual polygon; closest-t polygon hit wins over any sphere hit. - -Our Stage A (shipped 2026-05-16 Commit B) does sphere-only against -`Setup.SelectionSphere`. This will under-pick visible mesh that -extends beyond the sphere (creature's outstretched arm, sign edge -poking past sphere boundary) β€” exactly what retail's polygon refine -catches. - -**Acceptance:** Pipe per-part GfxObj visual polygons through a -`PickPolygonProvider` interface (don't duplicate mesh decoding β€” -hook the existing `ObjectMeshManager` cached data). Two-tier in -`WorldPicker.Pick`: sphere reject β†’ polygon scan β†’ polygon hit -dominates sphere hit. - -**Estimated scope:** Medium (~4-6 hours). Defer until visual -verification surfaces a Stage A miss in real play. -``` - -### Task DF3: File issue β€” cdb probe to confirm `omega.z = Ο€/2` base rate - -- [ ] **Step 1: Append issue to `docs/ISSUES.md`** - -```markdown -## #72 β€” Confirm Humanoid TurnRight/TurnLeft `omega.z` base rate via cdb - -**Status:** OPEN -**Severity:** LOW (current Β±Ο€/2 fallback matches all corroborating -evidence; cdb probe would settle the open question for good) -**Filed:** 2026-05-16 -**Component:** physics / rotation / research - -**Description:** The retail rotation rate landed in Commit A -(2026-05-16) uses `BaseTurnRateRadPerSec = Ο€/2` based on the -documented `AnimationSequencer.cs:734-741` claim that the Humanoid -motion table ships TurnRight/TurnLeft with `HasOmega` cleared -(forcing the convention fallback). The constant has 3 corroborating -sources but the actual dat content was never dumped. - -**Acceptance:** Set a cdb breakpoint on `CSequence::set_omega` -(acclient_2013_pseudo_c.txt β€” find exact symbol address) while -holding A or D in a retail client. Capture the `omega.z` argument -value. If `Β±Ο€/2` (or numerically identical via the MotionData -formula), close as confirmed. If different, file as a regression -+ fix the constant. - -**Estimated scope:** 30 min cdb session + 1 commit if confirmed, -+ small fix if different. Not blocking M1. -``` - -- [ ] **Step 2: Commit all 3 issue entries in one docs commit** - -```bash -git add docs/ISSUES.md -git commit -m "docs: file #70 (triangle apex/size UX), #71 (picker Stage B), #72 (cdb omega.z probe) - -Deferred follow-ups from the 2026-05-16 retail-faithfulness audit -(docs/superpowers/plans/2026-05-16-retail-faithfulness-fixes.md)." -``` - ---- - -## Self-review checklist - -**Spec coverage:** -- Fix #1 (rotation TurnRateFor + 1.5Γ— run): Task A1 (helper + tests), A2 (keyboard), A3 (auto-walk). βœ… -- Fix #4 (gate `!= 0`): Task A5 step 1. βœ… -- Fix #5 (useability fallback probe): Task A4 (flag), A5 step 2 (log lines). βœ… -- Fix #2 (per-tick diff-driven AP): Tasks B1 + B2 + B3. βœ… -- Fix #6 (delete 4 workarounds): Task B4 (TinyMargin), B5 (handler + SendAutonomousPositionNow), B6 (isRetryAfterArrival). βœ… -- Fix #3 Stage A (sphere picker): Tasks B7 + B8. βœ… -- Fix #7 (trim EntityHeightFor): Task B9. βœ… -- Fix #8 (delete IsTallSceneryGuid): Task B10. βœ… -- Deferred issues (triangle/Stage B/cdb): Tasks DF1, DF2, DF3. βœ… - -**Placeholder scan:** No "TBD" / "implement later" / vague handwaves. Every code step has actual code. - -**Type consistency:** -- `TurnRateFor(bool running)` defined Task A1, called Task A2 + A3. βœ… -- `NotePositionSent(Vector3, uint, float)` defined B1, called B3. βœ… -- `SimTimeSeconds` accessor defined B1, read B3. βœ… -- `OnAutoWalkArrivedSendDeferredAction()` defined Task B6 Step 3, subscribed in Task B6 Step 4. βœ… -- `WorldPicker.Pick(...sphereForEntity...)` defined Task B7, called Task B8. βœ… -- `TryGetEntitySelectionSphere` referenced in Task B8 β€” already exists in `GameWindow.cs` at line ~9605 per audit. βœ… -- `ApproxPositionEqual` defined Task B2 Step 3, called Task B2 Step 2. βœ… - ---- - -## Execution handoff - -Plan saved to `docs/superpowers/plans/2026-05-16-retail-faithfulness-fixes.md`. Two execution options: - -1. **Subagent-Driven (recommended)** β€” Dispatch fresh subagent per task; review between tasks; fast iteration. -2. **Inline Execution** β€” Execute tasks in this session using executing-plans; batch with checkpoints for review. - -**Which approach?** diff --git a/docs/superpowers/specs/2026-05-08-phase-n-worldbuilder-migration-design.md b/docs/superpowers/specs/2026-05-08-phase-n-worldbuilder-migration-design.md deleted file mode 100644 index 33f2280..0000000 --- a/docs/superpowers/specs/2026-05-08-phase-n-worldbuilder-migration-design.md +++ /dev/null @@ -1,223 +0,0 @@ -# Phase N β€” WorldBuilder Rendering Migration: Design - -**Date:** 2026-05-08 -**Status:** Design complete, awaiting plan generation for N.1. - -## Goal - -Stop re-porting AC-specific rendering and dat-handling algorithms from -retail decomp. Instead, depend on a fork of WorldBuilder -(`github.com/Chorizite/WorldBuilder`, MIT) for terrain, scenery, static -objects, EnvCells, portals, sky, particles, texture decoding, mesh -extraction, and visibility / culling. Acdream keeps its own network, -physics, animation, motion, UI, plugin, audio, and chat layers β€” those -are not in WorldBuilder. - -## Why - -acdream has accumulated a recurring pattern of subtle porting bugs in -its own rendering algorithms (the latest: a tree near the road at -landblock `0xA9B1` that retail and WorldBuilder do not show but our -re-port did, despite the algorithm code looking byte-identical to -WorldBuilder's). The triangle-Z bug, the hover-over-terrain bug, and -the edge-vertex spawn bug are all in the same family: small porting -errors that survive surface-level review. - -WorldBuilder is verified by visual inspection to render the AC world -correctly. It uses the same Silk.NET + .NET stack we already target. -It is MIT-licensed. It has fewer subtle bugs because its developers -have run it against the entire client_cell + client_portal dat content -and fixed everything users have reported. - -The cost of "we re-port retail algorithms ourselves" is now higher than -the cost of "we depend on someone else's tested port and inherit their -fixes." Migrating the rendering+dat layer to WorldBuilder is the -right call. - -## Inventory reference - -The full taxonomy of "what WorldBuilder has, what we keep porting -ourselves" lives at -[`docs/architecture/worldbuilder-inventory.md`](../../architecture/worldbuilder-inventory.md). -Before re-implementing any rendering or dat-handling algorithm, **check -the inventory first**. CLAUDE.md is updated to enforce this. - -## Architecture - -### Integration model - -**Fork upstream WorldBuilder, depend on the fork via git submodule.** - -- Fork: `https://github.com/eriknihlen/WorldBuilder` (already created; - upstream: `Chorizite/WorldBuilder`). -- Long-lived branch in fork: `acdream`. Upstream `master` merges into - `acdream` periodically; our acdream-specific changes (delete editor - files, expose hooks for our scene state) live on `acdream`. -- The current read-only snapshot at `references/WorldBuilder/` is - **replaced** by a git submodule pointing at the fork's `acdream` - branch. Existing CLAUDE.md path references and research docs that - cite `references/WorldBuilder/...` keep working. -- Our solution adds two ``s: - - `references/WorldBuilder/Chorizite.OpenGLSDLBackend/Chorizite.OpenGLSDLBackend.csproj` - - `references/WorldBuilder/WorldBuilder.Shared/WorldBuilder.Shared.csproj` -- Transitive NuGet dependencies (`Chorizite.Core`, - `Chorizite.DatReaderWriter.Extensions`, `BCnEncoder.Net`, - `SixLabors.ImageSharp`, `Silk.NET.SDL`, `MP3Sharp`) flow through. -- Editor-only files in WorldBuilder (Modules/Landscape/{Tools, - Commands, Services, Migrations, Hubs}, LandscapeDocument, etc.) stay - in the fork's source tree but are simply not referenced by acdream. - They impose no runtime cost. We can prune later if upstream stays - well-organized. - -### Phasing β€” strangler fig, subsystem by subsystem - -Each sub-phase is independently shippable behind a feature flag -(`ACDREAM_USE_WB_=1`). After visual verification the flag becomes -default-on, then is removed and the old code is deleted. This gives us -a one-line revert if a phase regresses. - -| # | Sub-phase | Effort | Risk | -|---|---|---|---| -| **N.0** | Submodule + project references + build green | 1-2 hrs | Low | -| **N.1** | Scenery algorithm calls | 1-2 days | Low | -| **N.2** | Terrain math helpers | 1-2 days | Low | -| **N.3** | Texture decoding | 2-3 days | Medium | -| **N.4** | Object meshing (Setup/GfxObj) | 1 week | Medium | -| **N.5** | Terrain rendering (full pipeline) | 2 weeks | High | -| **N.6** | Static objects rendering | 2 weeks | High | -| **N.7** | EnvCells / dungeons | 2 weeks | High | -| **N.8** | Sky + particles | 1 week | Medium | -| **N.9** | Visibility / culling | 3-5 days | Medium | -| **N.10** | GL infrastructure consolidation (optional) | 1 week | Medium | - -Total estimated calendar: 2-3 months. Engineering effort: 6-8 weeks. - -### What WorldBuilder does NOT cover (keep porting from retail decomp) - -- Network protocol (UDP, ISAAC, ACE messages) β€” keep ours -- Physics: collision, BSP queries, sphere sweeps, walkable validation - β€” keep ours (partial), continue porting from retail decomp -- Animation: motion sequencer, cycle/non-cycle parts β€” keep ours -- Movement: WASD β†’ MoveToState wire, remote-entity motion via - UpdateMotion + dead-reckoning β€” keep ours -- Game UI: chat, vitals, inventory, spell book β€” keep ours (ImGui - today, custom-toolkit later) -- Plugin API: IGameState, IEvents, IActions, IPacketPipeline, - IOverlay β€” keep ours (acdream-unique) -- Game events: combat, allegiance, spell casting β€” keep ours -- Audio (OpenAL pipeline) β€” keep ours -- TurbineChat + slash commands β€” keep ours -- Login + character selection flow β€” keep ours - -Per CLAUDE.md update, these still follow the -"grep named β†’ decompile β†’ verify β†’ port" workflow against retail decomp -at `docs/research/named-retail/`. - -### Network reference posture - -`references/Chorizite.ACProtocol/` (separate Chorizite repo) remains -the Primary Oracle for protocol field order and packed-dword -conventions per CLAUDE.md's reference table. No fork needed there. We -will lean on it harder during future network-conformance phases (Phase -M is already on the roadmap for that). - -## Components - -### N.0 β€” Setup (must land before N.1) - -**Files / actions:** -- Remove `references/WorldBuilder/` from working tree (it's currently a - checked-in snapshot). Add it back as a submodule pointing at - `git@github.com:eriknihlen/WorldBuilder.git` tracking the `acdream` - branch (created off `master`). -- Add `` entries in - `src/AcDream.Core/AcDream.Core.csproj` and - `src/AcDream.App/AcDream.App.csproj` for the two WB projects. -- Update `.gitmodules` to reflect the new submodule. -- Verify `dotnet build` and `dotnet test` are green. -- Commit. - -**Done criteria:** -- `git submodule status` shows `references/WorldBuilder` at the fork's - `acdream` HEAD. -- Solution builds clean with no new warnings. -- Existing 870+ tests still pass. - -### N.1 β€” Scenery algorithm calls - -See companion design doc: -[`2026-05-08-phase-n1-scenery-via-wb-helpers-design.md`](2026-05-08-phase-n1-scenery-via-wb-helpers-design.md). - -Brief: replace the algorithm guts inside `SceneryGenerator.Generate()` -with calls to WB's `SceneryHelpers` (Displace, RotateObj, ScaleObj, -ObjAlign, CheckSlope) and `TerrainUtils` (OnRoad, GetNormal). Keep our -data flow, our `ScenerySpawn` shape, our renderer integration. Add a -small adapter `LandBlock β†’ TerrainEntry[]`. - -### N.2-N.10 β€” separately brainstormed when we get there - -Each sub-phase will get its own brainstorm + spec when we reach it. -Estimating ahead is unreliable for the bigger phases (N.5, N.6, N.7); -we'll know more after N.1 ships and we have hands-on experience with -the WB integration. - -## Risks - -1. **Chorizite.Core dependency footprint.** Each render manager we - take pulls in `Chorizite.Core.Lib` and `Chorizite.Core.Render`. - Mitigation: take the NuGet dep, don't try to strip it. Risk is - mostly cosmetic (an extra package). - -2. **WB's data-flow is editor-shaped.** `LandscapeDocument`, - `LandscapeChunk`, etc. are editor concepts. Mitigation: write small - adapters that produce the editor-shaped data from our dat reads. - Phase N.1 is intentionally chosen to avoid this β€” we use only the - stateless helpers, not the full `SceneryRenderManager`. Larger - phases (N.5+) will need real adapter layers. - -3. **Upstream divergence.** WB's `master` will keep moving. Mitigation: - merge upstream `master` into our `acdream` branch periodically (at - minimum, before each new phase starts). Our acdream-specific - changes are isolated to deletions and additions on the `acdream` - branch, which merges cleanly with upstream most of the time. - -4. **Behaviors WB doesn't have.** WB is a dat editor; some - in-game-only behaviors (creature appearance via CreaturePalette / - GfxObjRemapping / HiddenParts) aren't in WB and we'll still need to - handle them ourselves at the integration boundary. Mitigation: - ACME's `StaticObjectManager.cs` covers these and is documented in - CLAUDE.md as the secondary oracle for character appearance. - -5. **Visual regression during migration.** Mitigation: feature flag - per phase. Visual verification at known-good locations (Holtburg, - Foundry statue, dungeon entrances) before flag becomes default-on. - -## Testing - -- **N.0:** existing 870+ tests stay green; `dotnet build` clean. -- **N.1:** new conformance test that runs both our `SceneryGenerator` - and a parallel call into WB's helpers against the same fixture data, - asserts identical spawn list. Visual verification at landblock - `0xA9B1` β€” the offending tree should be gone, Issue #49's missing - scenery should still be visible. -- **N.2-N.10:** each phase will define its own conformance and visual - verification criteria when brainstormed. - -## Documentation impact - -- [x] `docs/architecture/worldbuilder-inventory.md` β€” created. -- [x] `CLAUDE.md` β€” updated with new posture (top-level rule + reference - table + per-domain oracle hierarchy). -- [ ] `docs/plans/2026-04-11-roadmap.md` β€” add Phase N entry alongside - L, M, etc. (this happens in the same commit as the spec). -- [ ] `docs/architecture/acdream-architecture.md` β€” needs an - acknowledging note that the rendering layer is now WB-backed; can - follow in a later commit, not blocking. - -## Out of scope for this design - -- Phase N.2-N.10 detailed scope (each gets own brainstorm). -- Network conformance work (separate Phase M). -- Animation, physics, motion ports (continue against retail decomp, - not WB). -- UI, plugin, chat work (separate phases, not affected). diff --git a/docs/superpowers/specs/2026-05-08-phase-n1-scenery-via-wb-helpers-design.md b/docs/superpowers/specs/2026-05-08-phase-n1-scenery-via-wb-helpers-design.md deleted file mode 100644 index 6ec1b58..0000000 --- a/docs/superpowers/specs/2026-05-08-phase-n1-scenery-via-wb-helpers-design.md +++ /dev/null @@ -1,191 +0,0 @@ -# Phase N.1 β€” Scenery via WorldBuilder Helpers: Design - -**Date:** 2026-05-08 -**Parent design:** [`2026-05-08-phase-n-worldbuilder-migration-design.md`](2026-05-08-phase-n-worldbuilder-migration-design.md) -**Status:** Design complete, awaiting plan generation. - -## Goal - -Replace the algorithm guts of `SceneryGenerator.Generate()` with calls -to WorldBuilder's stateless `SceneryHelpers` and `TerrainUtils`. Keep -our data flow, our `ScenerySpawn` shape, and our renderer integration -unchanged. - -## Why scenery first - -1. **Active bug source.** Issues #48, #49 are scenery-related; the - investigation in this session uncovered another (the road-edge tree - at `0xA9B1`) we couldn't easily root-cause despite our code looking - identical to WB's. -2. **Smallest coherent slice.** Scenery placement uses only stateless - helpers from WB (Displace, OnRoad, GetNormal, CheckSlope, RotateObj, - ScaleObj). No need to take WB's `SceneryRenderManager`, no need for - editor-shaped data flow. -3. **Proves the integration pattern.** Phase N.0 wires up the - submodule + project references. N.1 uses them with a tiny surface - area. If something is wrong with the dependency model, we discover - it cheaply. - -## Architecture - -### What changes - -`src/AcDream.Core/World/SceneryGenerator.cs`: -- Remove our private `IsOnRoad(LandBlock, float, float)` helper. -- Remove our private `DisplaceObject(ObjectDesc, uint, uint, uint)` helper. -- Remove the `RoadHalfWidth` constant. -- Replace inline algorithm calls with WB equivalents (see table below). - -New file `src/AcDream.Core/World/WbSceneryAdapter.cs` (or similar -location β€” TBD during implementation): -- Helper `BuildTerrainEntries(LandBlock block) β†’ TerrainEntry[]` - converting our `DatReaderWriter.DBObjs.LandBlock` (the dat type) into - the `TerrainEntry[]` shape WB's `TerrainUtils` expects (9Γ—9 grid, - Type/Scenery/Road/Height fields per vertex). -- Helper for `RegionInfo` if needed (small wrapper over our - `Region` dat). - -### Algorithm-call substitution table - -| Today (ours) | Phase N.1 (WB) | -|---|---| -| `IsRoadVertex(raw)` (kept; small util) | unchanged β€” small predicate, no benefit to swap | -| `IsOnRoad(block, lx, ly)` | `TerrainUtils.OnRoad(new Vector3(lx, ly, 0), terrainEntries)` | -| `DisplaceObject(obj, gx, gy, j)` | `SceneryHelpers.Displace(obj, gx, gy, j)` | -| Slope normal: `TerrainSurface.SampleNormalZFromHeightmap(...)` | `TerrainUtils.GetNormal(region, terrainEntries, lbX, lbY, lbOffset).Z` | -| Slope check: `nz < obj.MinSlope \|\| nz > obj.MaxSlope` | `SceneryHelpers.CheckSlope(obj, normal.Z)` (returns bool) | -| Rotation logic (`AFrame::set_heading` reproduction) | `SceneryHelpers.RotateObj(obj, gx, gy, j, localPos)` (returns Quaternion) | -| Scale logic (LCG + Pow + clamp) | `SceneryHelpers.ScaleObj(obj, gx, gy, j)` (returns float) | - -### What does NOT change - -- The 9Γ—9 vertex loop (`for (x = 0; x < 9; x++) for (y = 0; y < 9; y++)`). -- Scene selection hash. -- Frequency roll. -- `obj.WeenieObj != 0` skip (weenie entries are dynamic spawns). -- Bounds check `lx, ly ∈ [0, 192)`. -- Per-spawn building check using our `buildingCells` HashSet. -- `BaseLoc.Z` offset application. -- `ScenerySpawn` record shape returned to the renderer. -- `Generate()` method signature β€” same parameters, same return type. - -### What about `obj_within_block`? - -We attempted this during the bug investigation but it's too aggressive -when applied with the model's actual sorting sphere radius (rejects -trees that should be there). WB also doesn't apply it. The retail -behavior we couldn't reproduce stays unreproduced for now β€” we accept -that as a known minor cosmetic discrepancy and move on. The point of -N.1 is matching WB's behavior, not retail's. If WB and retail -disagree, that's a WB-upstream problem to file separately. - -## Components - -### Files modified - -- `src/AcDream.Core/World/SceneryGenerator.cs` β€” algorithm-call swap. -- `src/AcDream.Core/AcDream.Core.csproj` β€” already has WB project ref - from N.0. - -### Files added - -- `src/AcDream.Core/World/WbSceneryAdapter.cs` β€” `LandBlock β†’ - TerrainEntry[]` and any other small adapters needed. -- `tests/AcDream.Core.Tests/World/SceneryGeneratorWbConformanceTests.cs` - β€” side-by-side test asserting our generator's output equals what - comes out when the same algorithms are called via WB directly. - -### Files deleted (eventually, after flag is on by default) - -- The deleted helpers in `SceneryGenerator.cs` mentioned above. - -### Feature flag - -Phase 1 of the rollout: `ACDREAM_USE_WB_SCENERY=1` (default off β€” old -path runs). When the env var is set, the new WB-backed path runs. - -Phase 2 (after visual verification at Holtburg / `0xA9B1`): flag -default-on. Old path can still be reached via -`ACDREAM_USE_WB_SCENERY=0`. - -Phase 3 (one or two sessions later, after no regressions): delete the -flag and the old code paths entirely. - -## Done criteria - -1. `dotnet build` green with no new warnings. -2. All existing tests pass (870+). -3. New conformance test passes: `SceneryGeneratorWbConformanceTests` - runs both code paths against fixture LandBlock data and asserts - identical spawn lists (same ObjectId, same LocalPosition within - 1e-4, same Rotation within 1e-4, same Scale within 1e-4). -4. Visual verification at landblock `0xA9B1` (Holtburg area): - - The offending tree near the road that retail/WB do not show is - **gone** in our render. - - Issue #49's previously missing scenery (the tree from the 9Γ—9 - loop expansion) is **still visible**. - - No new visual regressions in surrounding landblocks during a - brief flight around Holtburg. -5. Issue #49 stays closed; no new issues filed. - -## Risks (Phase-N.1-specific) - -1. **`TerrainEntry` field semantics.** WB packs Type/Scenery/Road/ - Height into the `TerrainEntry` struct in a specific format. Getting - the adapter wrong means OnRoad / scenery selection produces - different results than ours. Mitigation: read - `WorldBuilder.Shared/Modules/Landscape/Models/TerrainEntry.cs` - carefully; cross-check against WB's `TerrainUtils.GetRoad` / - `GetTerrainEntryForCell` to confirm field encoding. -2. **`RegionInfo` dependencies.** WB's `TerrainUtils.GetNormal` takes - a `RegionInfo` parameter. We need to either build a minimal - `RegionInfo` from our `Region` dat or call WB's normal calc - differently. Mitigation: investigate during implementation; expect - this is a small wrapper. -3. **`obj.MaxScale / obj.MinScale` divide-by-zero.** Our code checks - `if (obj.MinScale == obj.MaxScale)` first; WB's `ScaleObj` does the - same per-line review of `references/WorldBuilder/Chorizite.OpenGLSDLBackend/Lib/SceneryHelpers.cs:42-51`. Should be a non-issue. -4. **Rotation quaternion convention.** Our rotation produces - `headingQuat * baseLoc.Orientation`. WB's `RotateObj` calls - `SetHeading` which does its own composition. Need to confirm the - resulting quaternion is the same convention our renderer expects. - Mitigation: the conformance test catches this if it's wrong. - -## Testing - -### Conformance test (new) - -`SceneryGeneratorWbConformanceTests`: -- Construct a synthetic `LandBlock` with known terrain data. -- Run `SceneryGenerator.Generate(...)` with `ACDREAM_USE_WB_SCENERY=0` - and again with `=1`. -- Assert spawn counts equal. -- Assert each spawn's ObjectId, LocalPosition (within 1e-4), Rotation - (within 1e-4 per component), Scale (within 1e-4) are equal. - -### Existing tests - -`SceneryGeneratorTests` covers: road-vertex predicate, edge-vertex -displacement bounds, interior-vertex displacement bounds. These tests -exercise our internal helpers (`IsRoadVertex`, `DisplaceObject`). -After N.1, the `DisplaceObject` test must be either deleted (if we -delete the helper) or replaced (if we keep `IsRoadVertex` as a small -predicate β€” it's only one bit-test). - -### Visual verification - -User runs the client against ACE locally: -- Navigate to landblock `0xA9B1` (Holtburg). Verify offending tree - near road is gone. -- Confirm Issue #49's tree is still visible. -- Fly around Holtburg, scan visible scenery for any obvious - regression. - -## Out of scope for N.1 - -- Replacing our `SceneryRenderManager` (we don't have one β€” we have - `SceneryGenerator` producing `ScenerySpawn[]` and the renderer - consuming it directly). N.1 only touches the generator. -- Replacing our terrain math helpers (that's N.2). -- Replacing the static-object renderer (that's N.6). -- Anything in N.2-N.10. diff --git a/docs/superpowers/specs/2026-05-08-phase-n4-rendering-foundation-design.md b/docs/superpowers/specs/2026-05-08-phase-n4-rendering-foundation-design.md deleted file mode 100644 index 18c68b4..0000000 --- a/docs/superpowers/specs/2026-05-08-phase-n4-rendering-foundation-design.md +++ /dev/null @@ -1,414 +0,0 @@ -# Phase N.4 β€” Rendering Pipeline Foundation: Design - -**Date:** 2026-05-08 -**Status:** Design complete, awaiting plan generation. -**Parent design:** [2026-05-08-phase-n-worldbuilder-migration-design.md](2026-05-08-phase-n-worldbuilder-migration-design.md) -**Roadmap entry:** [docs/plans/2026-04-11-roadmap.md](../../plans/2026-04-11-roadmap.md) β€” Phase N.4 -**Inventory reference:** [docs/architecture/worldbuilder-inventory.md](../../architecture/worldbuilder-inventory.md) -**Related:** [ISSUE #51](../../ISSUES.md) β€” terrain split formula divergence (handled in N.5). - -## Goal - -Adopt WB's `Chorizite.OpenGLSDLBackend.Lib.ObjectMeshManager` and -`TextureAtlasManager` as acdream's rendering pipeline foundation. This -is the integration that unblocks Phases N.5 (terrain), N.6 (static -objects), N.7 (env cells), N.8 (sky/particles), and absorbs N.10 -(GL infrastructure consolidation). N.4 ships no visible change β€” the -world should look identical to today; what changes is the infrastructure -behind the scenes. - -## Why - -**The roadmap's original "drop-in helper" framing was wrong for N.4.** -Discovery during brainstorm 2026-05-08: WB's `ObjectMeshManager` is not -a stateless helper class like `SceneryHelpers` (N.1) or `TextureHelpers` -(N.3). It is a 2070-line stateful asset pipeline that owns: - -- GPU resources per object (VAO/VBO/IBO via `ObjectRenderData`) -- Reference counting (`IncrementRefCount`/`DecrementRefCount`) -- LRU cache + memory budget (default 1 GB) -- Background-thread CPU mesh preparation, main-thread GPU upload -- Shared texture atlases keyed by `(Width, Height, Format)` -- Particle emitter staging -- Modern bindless rendering path on capable hardware - -**There is no clean "just the mesh extraction" entry point.** WB's -`BuildPolygonIndices` (the algorithm we already faithfully ported into -[GfxObjMesh.cs](../../../src/AcDream.Core/Meshing/GfxObjMesh.cs)) is a -private method tightly coupled to atlas batching. To use WB's tested -infrastructure at all means adopting the whole pipeline. - -**N.5 + N.6 + N.7 build on this foundation.** WB's -`TerrainRenderManager`, `StaticObjectRenderManager`, and -`EnvCellRenderManager` all consume `ObjectMeshManager` (or its atlas) -as substrate. Without N.4, each later phase would need to either fork -those render managers or duplicate the infrastructure. Doing N.4 now -means N.5/N.6/N.7 become integration phases on top of shared plumbing, -not parallel infrastructure builds. - -**Real benefits beyond infrastructure consolidation:** - -1. **Memory budget with LRU eviction** (we don't have this; bigger - stream radii currently risk OOM). -2. **Texture atlasing β†’ ~4-8Γ— fewer draw calls** for static scenery - (~1100 entities at Holtburg today). -3. **Background-thread mesh preparation** β€” addresses the - render-thread-stall problem from - [feedback_phase_a1_hotfix_saga.md](../../../memory/feedback_phase_a1_hotfix_saga.md) - that forced us to revert async streaming. -4. **Bindless textures** on capable hardware (free perf when - GL 4.3 + `GL_ARB_bindless_texture` are available). - -## Architecture - -### Two-tier rendering split - -acdream's content cleanly partitions into two categories that map onto -two rendering paths: - -| Tier | Content | Why this category | Path | -|---|---|---|---| -| **Atlas (shared)** | Terrain props, scenery (procedural β€” trees / rocks / bushes / fences from ~50 templates), buildings, slabs, dungeon static geometry | Client-side procedural; no per-instance variation; many instances of few unique meshes | WB's `ObjectMeshManager` + `TextureAtlasManager`. Big sharing wins (1100 entities ↦ ~50 atlas slots). | -| **Per-instance (customized)** | Server-spawned entities (`CreateObject`): characters, creatures, equipped items. Anything carrying `SubPalettes` / `TextureChanges` / `AnimPartChange` / `HiddenParts` / `GfxObjRemapping` | Always uniquely customized; few visible at a time (~10-50) | Existing [TextureCache.GetOrUploadWithPaletteOverride](../../../src/AcDream.App/Rendering/TextureCache.cs:122). Already hash-keys overrides for caching; already tested. | - -**Routing rule**: - -- Objects spawned by `LandblockStreamLoader` (procedural, no - customization) β†’ atlas tier. -- Objects spawned by `CreateObject` (network, always customized) β†’ - per-instance tier. - -The boundary mirrors a distinction that already exists in our -networking model. We are not inventing a new conceptual line; we are -matching one that's already there. - -### Animation handling - -**Core insight:** in AC, animation is per-part TRANSFORM changes, not -mesh changes. A creature's Setup is a list of rigid GfxObj parts (head, -body, hands, etc.). Each part is its own static mesh; vertices inside -each part never change. Animation moves the parts as rigid bodies. - -This means **mesh data is static even for animated entities** β€” the -cache works fine. Only the per-part transforms change per frame, and -those don't live in the mesh cache. - -**Composition at draw time:** - -``` -final_part_world_matrix - = entity_world_transform - Γ— animation_override (from AnimationSequencer, this frame) - Γ— rest_pose_transform (cached in ObjectMeshData.SetupParts) -``` - -- WB's `ObjectMeshData.SetupParts: List<(ulong GfxObjId, Matrix4x4 Transform)>` - stores the rest-pose transforms (cached, shared). -- Our existing [AnimationSequencer](../../../src/AcDream.Core/Animation/AnimationSequencer.cs) - is **untouched**. It continues to produce per-part override matrices - per frame, driven by motion table + current motion command + tick. -- The renderer composes the three matrices per part per draw and pushes - the result as a uniform/instance attribute. - -**`AnimPartChange`** (server swaps a part's GfxObj β€” e.g., wielding a -sword): per-entity override map `Dictionary`. -At draw time, look up override; fall back to cached Setup part. WB's -mesh manager caches the override GfxObj's mesh data the same way as -any other part β€” first time seen, then shared. - -**`HiddenParts`** (bitmask hiding parts): per-entity `ulong` bitmask. -Draw loop: `if (hiddenMask & (1 << partIndex)) continue;`. - -**Per-frame CPU cost:** ~50 visible animated entities Γ— ~20 parts = -~1000 matrix multiplies per frame. Sub-millisecond on any CPU. - -**GPU-side per-draw transform push:** start with uniform-per-draw -(simple, ~1000 draws/frame for animated entities β€” fine). Promote to -per-instance vertex attribute (instanced draw, ~50 draws/frame) only -if measured perf demands it. - -### Streaming loader integration - -Adapter shim, ~200 LOC, sits between `LandblockStreamLoader` / -`WorldSession` and `ObjectMeshManager`: - -| Source event | Adapter call | What `ObjectMeshManager` does | -|---|---|---| -| Landblock loaded by streaming | `IncrementRefCount(id)` per unique GfxObj/Setup id in `Setups[]` + `Statics[]` | Begins CPU prep on background worker if not cached; queues GPU upload on main thread | -| Landblock unloaded by streaming (radius hysteresis) | `DecrementRefCount(id)` per object | Drops to LRU when count reaches 0; LRU + 1 GB memory budget handles eviction | -| Network `CreateObject` | Per-instance path: build `PaletteOverride` from `SubPalettes`, decode through `TextureCache.GetOrUploadWithPaletteOverride`, register entity-local mesh data | Bypasses WB atlas; stays in our existing per-instance path | -| Network `RemoveObject` | Release per-instance state for entity | (no WB call) | - -**Pending-spawn list preservation:** the streaming loader's existing -[pending-spawn list](../../../memory/feedback_phase_a1_hotfix_saga.md) -mechanism stays in place. `CreateObject` arriving before its landblock -streams in still parks until the landblock arrives, then drains. The -adapter is invoked when the spawn drains, not when it parks. - -**Thread safety:** WB's `ObjectMeshManager` uses `ConcurrentDictionary` -for its internal state and is designed to take `IncrementRefCount` calls -from any thread. Our streaming worker can call it directly without -marshaling onto the render thread. (This is part of why WB's design -addresses the render-thread-stall problem.) - -### Surface metadata strategy - -**Side-table, not fork patch.** - -WB's `MeshBatchData` carries `IsTransparent` + `IsAdditive`. We need to -preserve these acdream-specific surface properties already present in -our [GfxObjMesh.cs](../../../src/AcDream.Core/Meshing/GfxObjMesh.cs): - -- `Translucency` (`TranslucencyKind` enum: Opaque / AlphaBlend / Additive) -- `Luminosity` (float, self-illumination coefficient β€” sky pass critical) -- `Diffuse` (float) -- `SurfOpacity` (float, derived from `Surface.Translucency`) -- `NeedsUvRepeat` (bool, derived from authored UV range β€” sky-pass wrap-mode selection) -- `DisableFog` (bool, derived from emissive surface flags β€” sky-pass fog skip) - -Our renderer integration maintains a side-table: -`Dictionary<(ulong gfxObjId, int surfaceIdx), AcSurfaceMetadata>`. The -key matches the shape of today's `GfxObjSubMesh` β€” a (GfxObj, surface -index) pair uniquely identifies a per-surface render batch. Stable -across `IncrementRefCount` cycles. The metadata is computed once at -mesh-extraction time (matching today's `GfxObjMesh.Build`) and looked -up at draw time. - -**Why side-table not fork patch:** - -- Keeps WB's types pristine; upstream merges stay clean. -- Lookup cost is negligible (one hash lookup per batch per frame). -- Easy to roll back if WB's design evolves to incorporate similar fields. -- Preserves the careful sky-pass work done in C.1 with no risk to sky - rendering during this migration. - -### Fork hygiene - -**Target: zero fork patches for N.4.** WB's `acdream` branch stays at -upstream `master` plus the editor-only file deletions inherited from -N.0/N.1. If a fork patch becomes genuinely necessary mid-implementation -(e.g., a public hook is missing for our customization layer), it lands -as a single named patch with a comment explaining the rationale. Each -patch is candidate to upstream back to Chorizite/WorldBuilder. - -## Components - -### New code (acdream-side) - -| File | Responsibility | -|---|---| -| `src/AcDream.App/Rendering/Wb/WbMeshAdapter.cs` | Bridges acdream's lifecycle events to `ObjectMeshManager`. Holds the `ObjectMeshManager` instance, exposes `IncrementRefCount` / `DecrementRefCount` / `GetRenderData` to the rest of the renderer. | -| `src/AcDream.App/Rendering/Wb/LandblockSpawnAdapter.cs` | Streaming-loader hook. Walks `LandblockEntry.Setups[]` + `Statics[]`, calls `WbMeshAdapter` with unique ids. Companion `LandblockUnloadAdapter` for unload events. | -| `src/AcDream.App/Rendering/Wb/EntitySpawnAdapter.cs` | Network-spawn hook. Routes `CreateObject` to per-instance path, `RemoveObject` to release. | -| `src/AcDream.App/Rendering/Wb/AcSurfaceMetadata.cs` | Side-table type holding `Translucency` / `Luminosity` / `Diffuse` / `SurfOpacity` / `NeedsUvRepeat` / `DisableFog`. | -| `src/AcDream.App/Rendering/Wb/AcSurfaceMetadataTable.cs` | The `Dictionary` side-table, populated at mesh-extraction time, queried at draw time. | -| `src/AcDream.App/Rendering/Wb/AnimatedEntityState.cs` | Per-entity render state for animated entities: `partGfxObjOverrides` map (AnimPartChange), `hiddenMask` (HiddenParts), reference to `AnimationSequencer` for per-frame override matrices. | -| `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` | Per-frame draw loop. For each visible entity, looks up `ObjectRenderData`, composes per-part matrices (entity Γ— animation Γ— rest-pose), reads side-table metadata, issues GL draw. | - -### Modified code (acdream-side) - -| File | Change | -|---|---| -| `src/AcDream.App/Rendering/StaticMeshRenderer.cs` | Replace internal mesh-data + GL-resource handling with calls into `WbMeshAdapter`. Public surface preserved for the rest of the renderer's call sites. **N.6 will fully replace this file**; N.4 leaves it in place as a thin adapter. | -| `src/AcDream.App/Rendering/InstancedMeshRenderer.cs` | Same pattern β€” internal swap, public surface preserved. **N.6 fully replaces this file.** | -| `src/AcDream.App/Rendering/TextureCache.cs` | Per-instance path stays. Atlas-tier callers (anything using `GetOrUpload(surfaceId)` for static content) route through `WbMeshAdapter` instead. The override paths (`GetOrUploadWithOrigTextureOverride`, `GetOrUploadWithPaletteOverride`) keep their current behavior. | -| `src/AcDream.App/Rendering/GpuWorldState.cs` | Spawn/despawn callbacks route through `WbMeshAdapter`. Pending-spawn list mechanism preserved verbatim. | -| `src/AcDream.App/Rendering/GameWindow.cs` | Construct `WbMeshAdapter` on init; dispose on shutdown. | -| `src/AcDream.Core/Meshing/SetupMesh.cs` | Kept for tests + as the conformance-test reference implementation. Production callers route through `WbMeshAdapter`. | -| `src/AcDream.Core/Meshing/GfxObjMesh.cs` | Kept for tests + conformance reference. Production callers route through `WbMeshAdapter`. | - -## Data flow - -### Spawn β€” landblock-streamed (atlas tier) - -``` -LandblockStreamLoader.Load(landblockId) - β†’ LandblockEntry { Setups, Statics, ... } - β†’ LandblockSpawnAdapter.OnLoaded(entry) - for each unique gfxObjId in (entry.Setups βˆͺ entry.Statics): - WbMeshAdapter.IncrementRefCount(gfxObjId) - β†’ ObjectMeshManager.IncrementRefCount(gfxObjId) - β†’ if not cached: queue background prep - β†’ on prep complete: queue main-thread upload - β†’ on upload: GL VAO/VBO/IBO ready -``` - -### Spawn β€” network-customized (per-instance tier) - -``` -WorldSession.OnCreateObject(msg) - β†’ EntitySpawnAdapter.OnCreate(entity) - β†’ build PaletteOverride from msg.SubPalettes - β†’ for each surface needing per-instance decode: - TextureCache.GetOrUploadWithPaletteOverride(...) - β†’ register AnimatedEntityState (override map, hidden mask, - animation sequencer reference) -``` - -### Per-frame draw (atlas tier) - -``` -WbDrawDispatcher.Draw() - for each visible atlas-tier entity: - var renderData = WbMeshAdapter.GetRenderData(entity.GfxObjId) - foreach (batch in renderData.Batches): - bind atlas, bind shader, push uniforms - foreach (part in renderData.SetupParts): - push final_part_world_matrix uniform - glDrawElements(part.indices) -``` - -### Per-frame draw (per-instance tier, animated) - -``` -WbDrawDispatcher.DrawAnimated() - for each visible animated entity: - var state = entity.AnimatedEntityState - var sequencer = entity.AnimationSequencer - sequencer.AdvanceTo(currentTime) // existing - var animOverrides = sequencer.GetCurrentPartTransforms() // existing - - foreach (partIdx in 0..parts.Count): - if (state.hiddenMask & (1 << partIdx)) continue; - var gfxObjId = state.partGfxObjOverrides.GetValueOrDefault(partIdx) ?? defaultParts[partIdx] - var renderData = WbMeshAdapter.GetRenderData(gfxObjId) - var meta = AcSurfaceMetadataTable.Lookup(renderData.BatchKey) - var worldMatrix = entityWorld Γ— animOverrides[partIdx] Γ— renderData.RestPose - bind per-instance texture (TextureCache lookup) - push uniforms (worldMatrix, meta.Luminosity, meta.Diffuse, ...) - glDrawElements(...) -``` - -## Testing - -### Algorithmic conformance (before substitution) - -Per the N.1 / N.3 pattern, conformance tests run BEFORE the substitution -to prove equivalence: - -| Test | Compares | -|---|---| -| `MeshExtraction_OurBuildVsWbBuildPolygonIndices` | Battery of fixture GfxObjs (varying polygon counts, stippling flags, NegUVIndices, double-sided polys). For each: our `GfxObjMesh.Build` output vs WB's `ObjectMeshManager` output (extracted via test harness). Assert: identical vertex arrays, identical index arrays, identical per-bucket surface mapping. | -| `SetupFlattening_OurFlattenVsWbSetupParts` | Battery of representative Setups (flat / hierarchical / Resting-frame / Default-frame / no-frame). For each: our `SetupMesh.Flatten` output vs WB's Setup-parts walk. Assert: identical (GfxObjId, Matrix4x4) sequences. | -| `PerInstanceDecode_OldVsNewPath` | Synthetic palette + texture overrides (mirroring real `CreateObject` data). Decoded through new integrated path vs current `TextureCache.GetOrUploadWithPaletteOverride`. Assert: identical RGBA8. | - -If any test fails it's a real divergence β€” investigate, do not "fix" -the test (per N.3 watchout). - -### Component micro-tests - -| Test | Covers | -|---|---| -| `LandblockSpawnAdapter_RegistersAndUnregisters` | Mock `ObjectMeshManager`; verify ref-count increments/decrements pair correctly across landblock load/unload events. | -| `LandblockSpawnAdapter_DedupesSharedIds` | Same GfxObj id appearing in multiple landblocks: verify single ref-count per landblock, not per occurrence. | -| `EntitySpawnAdapter_RoutesToPerInstance` | `CreateObject` with `SubPalettes` set: verify per-instance path taken, atlas tier not invoked. | -| `AnimPartChange_OverridesAtDraw` | Per-instance override map: verify draw loop resolves correct part GfxObj id when override present, falls back to Setup default when absent. | -| `HiddenParts_SuppressesDraw` | Bitmask: verify draw loop skips hidden parts. | -| `MatrixComposition_EntityAnimRest` | Known entity transform + animation matrix + rest pose: verify final world matrix matches expected composition order (column-major: rest applied first, then animation, then entity world). | -| `SurfaceMetadata_SideTableLookup` | Populate side-table during mesh extraction; query at draw time; verify Luminosity / Diffuse / DisableFog round-trip correctly. | - -### Visual verification (per phase, before flipping `Live βœ“`) - -Walk the following with the user, comparing against pre-N.4 screenshots -or video: - -1. **Holtburg outdoor** β€” terrain props, scenery, buildings, NPCs, - characters. Verify: no missing entities, no magenta squares, no - alpha bleeding, no shading regressions, no animation hitches. -2. **Drudge Hideout** (or comparable starter dungeon) β€” EnvCell - geometry, interior lighting, animated creatures. -3. **Foundry** β€” heavy NPC traffic, customized appearances (the - server's first-time test bed for per-instance customization - correctness). -4. **A character with extreme palette overrides** β€” char-creation - variant if available, otherwise a known-customized server-side - test character. -5. **Long roam** β€” walk for ~5 minutes across multiple landblocks, - monitor GPU memory in title bar (memory budget enforcement working - means it stabilizes; memory growing unboundedly means LRU eviction - isn't firing). - -## Phasing - -Single shippable phase β€” no internal sub-phases. Within the phase, work -ordered to minimize the duration of "broken in middle" state: - -| Week | Focus | "Done when" | -|---|---|---| -| 1 | WB integration plumbing + atlas bring-up for static scenery only (smallest tier, highest sharing factor) + algorithmic conformance tests pass | Conformance tests green; static scenery renders through `ObjectMeshManager` while everything else uses old path | -| 2 | Streaming-loader adapter; LRU + memory budget verified under streaming pressure (long roam + radius 7Γ—7) | Long roam holds steady GPU memory; landblock unload reclaims memory | -| 3 | Per-instance customization path; animated creatures with palette overrides; AnimPartChange + HiddenParts | Drudge / chicken / banderling render with correct customizations; animation matches today | -| 4 | Surface metadata side-table integration; sky-pass preservation; visual verification at named locations; polish | Visual verification at all 5 locations passes; sky pass renders identically; ready for `Live βœ“` | - -## Risks - -1. **Per-instance customization scope creep.** If we discover a - customization path we don't already handle in `TextureCache` (e.g., - a rare `GfxObjRemapping` case), the per-instance path may need - extension. Mitigation: enumerate all customization paths during - week 3, add tests for each before integrating. - -2. **WB threading model interaction with our streaming worker.** - `ObjectMeshManager` uses `ConcurrentDictionary` and is designed for - concurrent `IncrementRefCount` calls, but its `_pendingRequests` queue - is guarded by a `lock`. Heavy concurrent landblock loads could serialize - on this lock. Mitigation: profile during week 2; if contention is - visible, batch landblock loads to amortize the lock. - -3. **Sky pass regression.** The sky pass's `NeedsUvRepeat` / - `DisableFog` / `Luminosity` flow is fragile and load-bearing. The - side-table preserves the data, but the integration point with - `SkyRenderer` needs careful review. Mitigation: sky-pass-specific - visual verification before flipping `Live βœ“`. - -4. **Bindless rendering path mismatch.** WB enables bindless when - `GL 4.3 + GL_ARB_bindless_texture` are present. If we ship through - the bindless path and a player has older hardware, fallback path - must work. Mitigation: dev/test with `_useModernRendering = false` - forced during week 1 to ensure the non-bindless path is also exercised. - -5. **Performance regression** during integration of week 1's "atlas for - static scenery, old path for everything else" mixed state. Mitigation: - keep the feature gate `ACDREAM_USE_WB_FOUNDATION=1` during weeks 1-3; - default-off until week 4 visual verification. - -## Out of scope - -- Replacing `StaticMeshRenderer` / `InstancedMeshRenderer` β€” those become - thin adapters in N.4 and are fully replaced in **N.6**. -- Replacing `TerrainAtlas` / `TerrainBlending` β€” that's **N.5**. -- Replacing EnvCell rendering β€” that's **N.7**. -- Replacing sky / particle rendering β€” that's **N.8**. -- Replacing visibility / culling β€” that's **N.9**. -- Per-instance customization beyond what's in today's `TextureCache` - (e.g., novel customization opcodes from future Phase F work) β€” out of - scope; future opcodes route through the same per-instance path. - -## Documentation impact - -- [x] [Roadmap](../../plans/2026-04-11-roadmap.md) β€” N.4 entry rebranded - and N.5/N.6/N.7/N.8/N.9/N.10 estimates revised (committed `6d42744` - and merged to main). -- [ ] This spec β€” written 2026-05-08, committing alongside. -- [ ] [worldbuilder-inventory.md](../../architecture/worldbuilder-inventory.md) - β€” minor update at end of N.4 to mark `ObjectMeshManager` / - `TextureAtlasManager` as "now wired up" rather than just "should - use." Not blocking N.4 start. -- [ ] [acdream-architecture.md](../../architecture/acdream-architecture.md) - β€” needs an acknowledging note after N.4 lands that the rendering - pipeline is WB-backed. Can follow in a later commit. - -## Reference materials - -- WB `ObjectMeshManager`: `references/WorldBuilder/Chorizite.OpenGLSDLBackend/Lib/ObjectMeshManager.cs` -- WB `TextureAtlasManager`: `references/WorldBuilder/Chorizite.OpenGLSDLBackend/Lib/TextureAtlasManager.cs` -- WB `BaseObjectRenderManager`: `references/WorldBuilder/Chorizite.OpenGLSDLBackend/Lib/BaseObjectRenderManager.cs` -- ACME secondary oracle for character appearance (CreaturePalette - / GfxObjRemapping / HiddenParts behavior): - `references/WorldBuilder-ACME-Edition/WorldBuilder/Editors/Landscape/StaticObjectManager.cs` -- Existing acdream code: - - [SetupMesh.cs](../../../src/AcDream.Core/Meshing/SetupMesh.cs) - - [GfxObjMesh.cs](../../../src/AcDream.Core/Meshing/GfxObjMesh.cs) - - [TextureCache.cs](../../../src/AcDream.App/Rendering/TextureCache.cs) - - [PaletteOverride.cs](../../../src/AcDream.Core/World/PaletteOverride.cs) - - [AnimationSequencer.cs](../../../src/AcDream.Core/Animation/AnimationSequencer.cs) diff --git a/docs/superpowers/specs/2026-05-08-phase-n5-modern-rendering-design.md b/docs/superpowers/specs/2026-05-08-phase-n5-modern-rendering-design.md deleted file mode 100644 index 3e7aeed..0000000 --- a/docs/superpowers/specs/2026-05-08-phase-n5-modern-rendering-design.md +++ /dev/null @@ -1,554 +0,0 @@ -# Phase N.5 β€” Modern Rendering Path β€” Design Spec - -**Status:** Draft (brainstormed 2026-05-08, not yet implemented). -**Author:** acdream lead engineer + Claude. -**Builds on:** Phase N.4 (`WbDrawDispatcher`, shipped 2026-05-08). -**Predecessor docs:** -- `docs/research/2026-05-08-phase-n5-handoff.md` (cold-start briefing). -- `docs/superpowers/plans/2026-05-08-phase-n4-rendering-foundation.md` (N.4 plan; Adjustments 7-10 are required reading). -- `docs/superpowers/specs/2026-05-08-phase-n4-rendering-foundation-design.md` (N.4 spec). - ---- - -## 1. Problem statement - -N.4 collapsed entity rendering from O(entities Γ— batches) per-draw GL calls to O(unique GfxObj Γ— surface Γ— translucency) grouped instanced draws. The remaining hot path still does, per group: - -``` -glActiveTexture(0) -glBindTexture(2D, texHandle) -glBindBuffer(EBO, batchIbo) -glDrawElementsInstancedBaseVertexBaseInstance(...) -``` - -Across a typical Holtburg-courtyard scene that's still ~100-300 GL calls per frame for entities. Modern GPUs and our drivers (GL 4.3 + bindless, gated by WB's `_useModernRendering`) support patterns that eliminate ALL of those per-group calls: - -- **Bindless textures** (`GL_ARB_bindless_texture`) β€” texture handles are 64-bit tokens that don't require `glBindTexture` to use; the shader samples from a handle read out of buffer data. -- **Multi-draw indirect** (`glMultiDrawElementsIndirect`) β€” one GL call dispatches N draws from a `DrawElementsIndirectCommand` buffer; the driver issues all of them with no CPU-side per-draw work. - -N.5 lifts `WbDrawDispatcher` onto these primitives. Target: β‰₯30% reduction in CPU dispatcher time, draw call count down to ~5/frame, no visual regression vs N.4. - ---- - -## 2. Decisions log - -This section records the brainstorm outcomes that the rest of the doc relies on. - -| # | Decision | Choice | Reason | -|---|---|---|---| -| 1 | Texture sampler model | **`sampler2DArray`** for ALL textures (1-layer wrapping for per-instance composites) | Matches WB's modern shader exactly; future-proofs for atlas adoption in N.6+; avoids two shader files. ~50 lines of TextureCache change. | -| 2 | Translucent rendering | **WB's two-pass alpha-test** (opaque pass discards `Ξ±<0.95`, transparent pass discards `Ξ±β‰₯0.95`) | Single blend mode per pass enables one indirect call per pass. Loses native `Additive` blend on GfxObj surfaces; sky + particles have own renderers and aren't affected. Falsifiable at visual verification β€” if we see a regression, add an additive sub-pass (~30-min fix). | -| 3 | Per-instance + per-draw data delivery | **All-SSBO**: `Instances[]` at binding=0 (mat4 per instance), `Batches[]` at binding=1 (texture handle + layer + flags per group) | Matches WB's modern shader. SSBOs avoid the 16-attrib stride limit, scale to large instance counts, give clean per-draw indexing via `gl_DrawIDARB`. | -| 4 | Bindless handle residency | **Resident on upload, never release** | acdream's content set is bounded (~1-5K unique textures per session). Handles persist for process lifetime; no eviction code in N.5. Diagnostic logging of handle count under `ACDREAM_WB_DIAG=1` to spot growth. | -| 5 | Escape hatch | **Modern path mandatory (N.5 ship amendment)**. `WbFoundationFlag` and `ACDREAM_USE_WB_FOUNDATION` env var have been deleted. Missing `GL_ARB_bindless_texture` or `GL_ARB_shader_draw_parameters` throws `NotSupportedException` at startup with a clear error message. No fallback. | Escape hatch was never exercised after N.4 ship. Legacy `InstancedMeshRenderer` + `StaticMeshRenderer` deleted in the N.5 retirement commit. N.6 scope narrowed accordingly. | -| 6 | Perf measurement | **CPU stopwatch + GL timer queries** logged via `[WB-DIAG]` | Captures both CPU dispatcher time and GPU rendering time. Acceptance gate compares before/after numbers in fixed Holtburg/Foundry scenes. | -| 7 | Persistent-mapped buffers | **Defer to N.6** | Bindless+indirect win is 70-80% of achievable savings. Persistent-mapped + ring + sync is the last 5-10% with non-trivial sync-fence complexity; not worth the risk in N.5's 2-3 week budget. Add post-N.5 if profiling shows residual `glBufferData` cost. | -| 8 | Per-instance highlight (selection blink) | **Defer to a Phase B.4 follow-up** | Retail pulses click targets as visual confirmation; the right mechanism is per-instance highlight color (NOT WB's global `uHighlightColor` which would tint everything in our single-indirect-call design). Field is reserved in design (extend `InstanceData` to include `vec4 highlightColor`); N.5 ships without the field, future phase plumbs it without shader rewrite. | - ---- - -## 3. Architecture overview - -### What changes - -`WbDrawDispatcher.Draw` swaps its inner loop. Phases 1-3 (entity walk, group bucketing, matrix layout) stay intact. Phases 5-6 (per-group GL calls) are replaced by a single `glMultiDrawElementsIndirect` per pass, fed by SSBO-resident per-instance and per-draw data. - -### What's preserved from N.4 - -- Group bucketing pipeline (entity AABB cull, palette hash memo, group key dictionary). -- `AcSurfaceMetadataTable` for translucency classification. -- `EntitySpawnAdapter` / `LandblockSpawnAdapter` (mesh lifecycle bridge). -- `WbMeshAdapter` (the seam over WB's `ObjectMeshManager`). -- Front-to-back sort of opaque groups (depth-test reject of overdrawn fragments). -- Per-entity 5m AABB frustum cull. - -### What's new - -- `TextureCache` uploads as 1-layer `Texture2DArray` instead of `Texture2D`. Generates 64-bit bindless handles at upload, makes them resident. -- New shader pair `mesh_modern.vert/.frag` modeled on WB's `StaticObjectModern` but adapted (see Β§6). -- Three new GPU buffers in the dispatcher: - - `_instanceSsbo` β€” `std430` layout, `mat4[]`, all visible matrices. - - `_batchSsbo` β€” `std430` layout, `BatchData[]`, one entry per group. - - `_indirectBuffer` β€” `DrawElementsIndirectCommand[]`, one per group. -- Two diagnostic measurements in `[WB-DIAG]`: CPU stopwatch span around `Draw()`; GPU `GL_TIME_ELAPSED` query around the indirect dispatch. - -### What gets deleted - -- `WbDrawDispatcher.DrawGroup` (replaced by indirect). -- `WbDrawDispatcher.EnsureInstanceAttribs` (no more vertex attribs at locations 3-6). -- Per-blend-mode `glBlendFunc` switch in the translucent loop. -- `mesh_instanced.vert/.frag` (replaced by `mesh_modern.*`). - -### What stays under the escape hatch - -`InstancedMeshRenderer` is untouched. `ACDREAM_USE_WB_FOUNDATION=0` still routes there. N.6 retires it. - ---- - -## 4. Component changes - -### 4.1 `TextureCache` - -Texture upload path becomes Texture2DArray with depth=1: - -```csharp -private uint UploadRgba8AsLayer1Array(DecodedTexture decoded) -{ - uint tex = _gl.GenTexture(); - _gl.BindTexture(TextureTarget.Texture2DArray, tex); - - fixed (byte* p = decoded.Rgba8) - _gl.TexImage3D( - TextureTarget.Texture2DArray, 0, InternalFormat.Rgba8, - (uint)decoded.Width, (uint)decoded.Height, depth: 1, - border: 0, PixelFormat.Rgba, PixelType.UnsignedByte, p); - - _gl.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); - _gl.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); - _gl.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat); - _gl.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat); - _gl.BindTexture(TextureTarget.Texture2DArray, 0); - return tex; -} -``` - -Bindless handle generation, eager + resident-on-upload, parallel cache: - -```csharp -private readonly Dictionary _bindlessHandlesByGlName = new(); - -private ulong MakeResidentHandle(uint glTextureName) -{ - if (_bindlessHandlesByGlName.TryGetValue(glTextureName, out var h)) - return h; - h = _bindless.GetTextureHandleARB(glTextureName); - _bindless.MakeTextureHandleResidentARB(h); - _bindlessHandlesByGlName[glTextureName] = h; - return h; -} -``` - -Three new methods returning `ulong` bindless handles, paralleling the existing `uint` GL-name methods: - -```csharp -public ulong GetOrUploadBindless(uint surfaceId); -public ulong GetOrUploadWithOrigTextureOverrideBindless(uint surfaceId, uint overrideOrigTextureId); -public ulong GetOrUploadWithPaletteOverrideBindless(uint surfaceId, uint? overrideOrigTextureId, PaletteOverride paletteOverride, ulong precomputedPaletteHash); -``` - -Each delegates to its existing `uint` sibling to populate the underlying GL texture, then calls `MakeResidentHandle` and returns the 64-bit handle. - -The `uint`-returning methods stay (used by `SkyRenderer`, `TerrainAtlas`, anything outside the WB modern path). - -`Dispose` releases bindless handles BEFORE deleting their textures: iterate `_bindlessHandlesByGlName.Values`, call `glMakeTextureHandleNonResidentARB(handle)`, then `glDeleteTextures` proceeds as today. - -### 4.2 `WbDrawDispatcher` - -Three new GPU buffers (replacing `_instanceVbo`): - -```csharp -private uint _instanceSsbo; // binding=0, std430, mat4[] -private uint _batchSsbo; // binding=1, std430, BatchData[] -private uint _indirectBuffer; // GL_DRAW_INDIRECT_BUFFER, DEIC[] -``` - -`InstanceGroup` becomes: - -```csharp -private sealed class InstanceGroup -{ - public uint Ibo; - public uint FirstIndex; - public int BaseVertex; - public int IndexCount; - public ulong BindlessTextureHandle; // 64-bit (was uint TextureHandle in N.4) - public uint TextureLayer; // always 0 in N.5 (per-instance composites are 1-layer arrays) - public TranslucencyKind Translucency; - public int FirstInstance; - public int InstanceCount; - public float SortDistance; - public readonly List Matrices = new(); -} -``` - -`GroupKey` adds the layer: - -```csharp -private readonly record struct GroupKey( - uint Ibo, uint FirstIndex, int BaseVertex, int IndexCount, - ulong BindlessTextureHandle, uint TextureLayer, TranslucencyKind Translucency); -``` - -Per-frame draw flow: - -1. **Walk entities β†’ build `_groups` dict** (unchanged from N.4). -2. **Lay matrices contiguously, split opaque/transparent, sort opaque** (unchanged). -3. **Build per-group BatchData and DEIC arrays.** One `BatchData` per group `(handle, layer, flags=0)`. One DEIC per group `(count = IndexCount, instanceCount = InstanceCount, firstIndex = FirstIndex, baseVertex = BaseVertex, baseInstance = FirstInstance)`. Indirect commands are laid out contiguously: opaque section first (sorted front-to-back), transparent section second. `_opaqueDrawCount` and `_transparentDrawCount` track section sizes; `_transparentByteOffset = _opaqueDrawCount * sizeof(DEIC)`. -4. **Three `glBufferData` uploads** to `_instanceSsbo`, `_batchSsbo`, `_indirectBuffer` (single buffer, both sections). -5. **Bind global VAO once** (preserved from N.4 β€” modern rendering shares one VAO). -6. **Bind SSBOs once** via `glBindBufferBase(SHADER_STORAGE_BUFFER, 0, _instanceSsbo)` and `... 1, _batchSsbo`. -7. **Opaque pass.** Set `uRenderPass = 0`. `glBindBuffer(DRAW_INDIRECT_BUFFER, _indirectBuffer)`. `glMultiDrawElementsIndirect(Triangles, UnsignedShort, indirect=(void*)0, drawcount=_opaqueDrawCount, stride=sizeof(DEIC))`. -8. **Transparent pass.** Set `uRenderPass = 1`. `glEnable(BLEND)` + `glBlendFunc(SrcAlpha, OneMinusSrcAlpha)` + `glDepthMask(false)`. `glMultiDrawElementsIndirect(Triangles, UnsignedShort, indirect=(void*)_transparentByteOffset, drawcount=_transparentDrawCount, stride=sizeof(DEIC))`. -9. **Restore state.** `glDepthMask(true)` + `glDisable(BLEND)` + `glBindVertexArray(0)`. - -Diagnostic timing (under `ACDREAM_WB_DIAG=1`): - -- CPU: `Stopwatch` started at the top of `Draw()`, stopped at the bottom. Median + 95th-percentile flushed in the 5-second `[WB-DIAG]` rollup. -- GPU: `glGenQueries` two query objects (one for opaque, one for transparent). `glBeginQuery(TIME_ELAPSED) / glEndQuery` around each `glMultiDrawElementsIndirect`. Result polled with `GL_QUERY_RESULT_NO_WAIT` on the next frame's start; if not ready, drop the sample and try again. - -### 4.3 New shader files - -`src/AcDream.App/Shaders/mesh_modern.vert`: - -```glsl -#version 430 core -#extension GL_ARB_bindless_texture : require -#extension GL_ARB_shader_draw_parameters : require - -layout(location = 0) in vec3 aPosition; -layout(location = 1) in vec3 aNormal; -layout(location = 2) in vec2 aTexCoord; - -struct InstanceData { - mat4 transform; - // Reserved for Phase B.4 follow-up (selection-blink retail-faithful highlight): - // vec4 highlightColor; // RGBA β€” when non-zero alpha, fragment shader mixes into output. - // Add field here, increase stride to 80 bytes, and read at fragment via flat varying. -}; - -struct BatchData { - uvec2 textureHandle; // bindless handle for sampler2DArray - uint textureLayer; // layer index (always 0 for per-instance composites) - uint flags; // reserved for future use -}; - -layout(std430, binding = 0) readonly buffer InstanceBuffer { - InstanceData Instances[]; -}; - -layout(std430, binding = 1) readonly buffer BatchBuffer { - BatchData Batches[]; -}; - -layout(std140, binding = 1) uniform LightingUbo { - vec4 uAmbient; - vec4 uSunDir; - vec4 uSunColor; - // matches existing acdream lighting UBO; do not change layout -}; - -uniform mat4 uViewProjection; -uniform int uRenderPass; // 0=opaque, 1=transparent (consumed in fragment shader) - -out vec3 vNormal; -out vec2 vTexCoord; -out flat uvec2 vTextureHandle; -out flat uint vTextureLayer; - -void main() { - int instanceIndex = gl_BaseInstanceARB + gl_InstanceID; - mat4 model = Instances[instanceIndex].transform; - - vec4 worldPos = model * vec4(aPosition, 1.0); - gl_Position = uViewProjection * worldPos; - - vNormal = normalize(mat3(model) * aNormal); - vTexCoord = aTexCoord; - - BatchData b = Batches[gl_DrawIDARB]; - vTextureHandle = b.textureHandle; - vTextureLayer = b.textureLayer; -} -``` - -`src/AcDream.App/Shaders/mesh_modern.frag`: - -```glsl -#version 430 core -#extension GL_ARB_bindless_texture : require - -in vec3 vNormal; -in vec2 vTexCoord; -in flat uvec2 vTextureHandle; -in flat uint vTextureLayer; - -layout(std140, binding = 1) uniform LightingUbo { - vec4 uAmbient; - vec4 uSunDir; - vec4 uSunColor; -}; - -uniform int uRenderPass; - -out vec4 FragColor; - -void main() { - sampler2DArray tex = sampler2DArray(vTextureHandle); - vec4 color = texture(tex, vec3(vTexCoord, float(vTextureLayer))); - - if (uRenderPass == 0) { - // Opaque pass: discard soft pixels (alpha cutout), write to depth - if (color.a < 0.95) discard; - } else { - // Transparent pass: discard hard pixels (already drawn opaque), no depth write - if (color.a >= 0.95) discard; - if (color.a < 0.05) discard; // skip totally-empty fragments β€” perf for large transparent overdraw - } - - // Diffuse lighting (preserved from acdream's existing lighting model) - vec3 N = normalize(vNormal); - vec3 L = normalize(uSunDir.xyz); - float diff = max(dot(N, L), 0.0); - vec3 lit = uAmbient.rgb + uSunColor.rgb * diff; - color.rgb *= clamp(lit, 0.0, 1.0); - - FragColor = color; -} -``` - -Differences from WB's `StaticObjectModern.*`: - -- Drops `uActiveCells[]` cell-filtering (acdream culls cells on CPU). -- Drops `uDrawIDOffset` (acdream issues full passes, no pagination). -- Drops `uHighlightColor` (deferred to Phase B.4 follow-up; reserved as per-instance `highlightColor` field, not a global uniform). -- Adapts the lighting model to acdream's existing UBO at binding=1 instead of WB's `SceneData` UBO. -- Uses 1-layer `sampler2DArray` for ALL textures (WB uses multi-layer atlases β€” same shader works for both shapes). - ---- - -## 5. Per-frame data flow walk-through - -A concrete trace. Visible work for frame N: - -| Group | GfxObj | Surface | Translucency | Instances | -|---|---|---|---|---| -| 0 | oak tree | bark | Opaque | 12 | -| 1 | oak tree | leaves | AlphaBlend | 12 | -| 2 | drudge | skin (palette override) | Opaque | 1 | -| 3 | drudge | eyes | Opaque | 1 | - -**Instance SSBO** (binding=0), 26 entries (each batch contributes its own copy of the entity matrix): -``` -[0..11] = oak instance matrices (group 0 β€” bark) -[12..23] = oak instance matrices (group 1 β€” leaves) -[24] = drudge instance matrix (group 2 β€” skin) -[25] = drudge instance matrix (group 3 β€” eyes) -``` - -**Batch SSBO** (binding=1), 4 entries indexed by `gl_DrawIDARB`: -``` -Batches[0] = (oak_bark_handle, layer=0, flags=0) -Batches[1] = (oak_leaves_handle, layer=0, flags=0) -Batches[2] = (drudge_skin_handle_with_palette, layer=0, flags=0) -Batches[3] = (drudge_eyes_handle, layer=0, flags=0) -``` - -**Indirect buffer** (single buffer, two sections): -``` -_indirectBuffer[0..2] = opaque section (3 entries, sorted front-to-back) - [0] = (count=oakBarkIdx, instanceCount=12, firstIndex=oakBarkFI, baseVertex=oakBV, baseInstance=0) - [1] = (count=drudgeSkinIdx, instanceCount=1, firstIndex=drudgeSkinFI, baseVertex=drudgeBV, baseInstance=24) - [2] = (count=drudgeEyesIdx, instanceCount=1, firstIndex=drudgeEyesFI, baseVertex=drudgeBV, baseInstance=25) - -_indirectBuffer[3] = transparent section (1 entry) - [3] = (count=oakLeavesIdx, instanceCount=12, firstIndex=oakLeavesFI, baseVertex=oakBV, baseInstance=12) - -_opaqueDrawCount = 3; _transparentDrawCount = 1; _transparentByteOffset = 3 * sizeof(DEIC) = 60. -``` - -**Shader access pattern** (per vertex): -```glsl -int instanceIndex = gl_BaseInstanceARB + gl_InstanceID; // unique per (group, instance) pair -mat4 model = Instances[instanceIndex].transform; -BatchData b = Batches[gl_DrawIDARB]; // shared across all verts in this draw -sampler2DArray tex = sampler2DArray(b.textureHandle); -vec4 color = texture(tex, vec3(aTexCoord, float(b.textureLayer))); -``` - -**Per-frame CPU GL calls** (entity rendering, total): -- 3Γ— `glBufferData` (instance SSBO, batch SSBO, indirect buffer). -- 1Γ— `glBindVertexArray(globalVAO)`. -- 2Γ— `glBindBufferBase` (SSBOs at bindings 0 + 1). -- 1Γ— `glBindBuffer(DRAW_INDIRECT_BUFFER, _indirectBuffer)`. -- 2Γ— `glMultiDrawElementsIndirect` (one opaque, one transparent). -- ~5 state changes (blend, depth mask, render pass uniform). - -Total: ~15-20 GL calls per frame for entity rendering, regardless of group count. N.4 baseline is "few hundred." - ---- - -## 6. Translucent rendering detail - -Per Decision 2: WB's two-pass alpha-test pattern. - -**Group classification.** `ClassifyBatches` puts groups into one of two arrays: - -- **Opaque indirect:** `TranslucencyKind.Opaque` and `TranslucencyKind.ClipMap`. -- **Transparent indirect:** `TranslucencyKind.AlphaBlend`, `Additive`, `InvAlpha` all merged. Per Decision 2, additive renders as alpha-blend; falsifiable at visual verification. - -Opaque groups stay sorted front-to-back by `SortDistance` (preserved from N.4 β€” depth-test reject of overdrawn fragments is a meaningful win on dense scenes). - -**Pass GL state:** - -```csharp -// Opaque pass -_gl.Disable(EnableCap.Blend); -_gl.DepthMask(true); -_gl.Enable(EnableCap.CullFace); _gl.CullFace(TriangleFace.Back); _gl.FrontFace(FrontFaceDirection.Ccw); -_shader.SetInt("uRenderPass", 0); -_gl.BindBuffer(BufferTargetARB.DrawIndirectBuffer, _indirectBuffer); -_gl.MultiDrawElementsIndirect(PrimitiveType.Triangles, DrawElementsType.UnsignedShort, - indirect: (void*)0, drawcount: _opaqueDrawCount, stride: (uint)sizeof(DEIC)); - -// Transparent pass -_gl.Enable(EnableCap.Blend); -_gl.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha); -_gl.DepthMask(false); -_shader.SetInt("uRenderPass", 1); -_gl.MultiDrawElementsIndirect(PrimitiveType.Triangles, DrawElementsType.UnsignedShort, - indirect: (void*)_transparentByteOffset, drawcount: _transparentDrawCount, stride: (uint)sizeof(DEIC)); - -// Cleanup -_gl.DepthMask(true); _gl.Disable(EnableCap.Blend); _gl.BindVertexArray(0); -``` - -**Visual verification gate (additive fallback plan).** During Week 2-3 visual verification, look at: -- Holtburg courtyard, dungeon entrance β€” confirm scenery + characters identical. -- Foundry interior β€” magic-themed content with potentially additive-flagged surfaces. -- Any glowing weapon decals, magical aura effects, or self-luminous textures observed. - -If a visible regression appears (faded glow, missing additive bloom): amend spec to add a third indirect call within the transparent pass with `glBlendFunc(SrcAlpha, One)`. Group classification splits Additive into its own bucket. ~30-min change. - ---- - -## 7. Error handling and fallback - -### 7.1 GPU capability detection - -WB's `OpenGLGraphicsDevice` already detects: -- `HasOpenGL43` (required for SSBOs, multi-draw indirect, `gl_BaseInstanceARB`). -- `HasBindless` (required for bindless texture handles). - -`WbDrawDispatcher` is only constructed when `WbFoundationFlag.Enabled` is true, which gates on `_useModernRendering = HasOpenGL43 && HasBindless`. We inherit WB's gating. - -**Additional check:** `GL_ARB_shader_draw_parameters` (for `gl_BaseInstanceARB`, `gl_DrawIDARB`). Standard on GL 4.6, available as extension on 4.3+. Add to N.5's capability check; if missing, `WbDrawDispatcher` constructor logs a one-time warning and the foundation flag flips off (falls back to `InstancedMeshRenderer`). - -### 7.2 Shader compile failure - -If `mesh_modern.vert/.frag` fails to compile (driver bug, GLSL version mismatch, extension issue): catch the compile exception in `WbDrawDispatcher` constructor, log the GLSL info log + GPU vendor/renderer string ONCE, flip `WbFoundationFlag.Enabled = false` for the session, fall back to `InstancedMeshRenderer`. Do not crash. - -### 7.3 Non-resident handle (the bindless foot-gun) - -Sampling a non-resident handle causes undefined behavior (driver-dependent: black texture, GPU fault, device-lost). - -Mitigation in code: `TextureCache.MakeResidentHandle` is the only API that produces a handle, and it makes the handle resident in the same call. There is no API surface that produces a non-resident handle. Defense-in-depth: dispatcher asserts `BindlessTextureHandle != 0` before queuing a draw (zero handles get filtered out, same as zero `surfaceId` does today). - -### 7.4 Indirect command corruption - -`count`, `firstIndex`, `baseVertex` come from WB's `ObjectRenderBatch` (never user input; WB-internal correctness). `instanceCount` is `grp.Matrices.Count` (we control). `baseInstance` is `grp.FirstInstance` (we control, computed cumulatively). Bug-class is "WB-internal corruption + our cumulative-offset bug" β€” same surface area as N.4's `BaseInstance` already trusts. Add a debug-build assertion: cumulative `baseInstance` values must be strictly increasing. - -### 7.5 Disposal order - -`WbDrawDispatcher.Dispose` releases bindless handles before deleting underlying textures (driver UB otherwise). `TextureCache.Dispose` does this: -1. Iterate `_bindlessHandlesByGlName.Values`, call `glMakeTextureHandleNonResidentARB(handle)`. -2. Call `_glExtensions.MakeAllNonResidentARB` if available (some drivers prefer batch). -3. Then `glDeleteTextures` proceeds as today. - -Dispatcher's own buffer cleanup (`_instanceSsbo`, `_batchSsbo`, `_indirectBuffer`) via `glDeleteBuffers`. - -### 7.6 Persistent first-failure diagnostic - -If shader compile fails OR an extension check fails OR `glMultiDrawElementsIndirect` returns `GL_INVALID_OPERATION` on first frame: log ONCE with GPU vendor/renderer string + GLSL info log. Don't spam. User pastes the line into a bug report; we know exactly where to look. - ---- - -## 8. Testing and acceptance - -### 8.1 Unit / conformance tests - -- **`TextureCacheBindlessTests`** β€” for each `Bindless`-suffixed `GetOrUpload*`: returns non-zero `ulong`, returns same handle for same key (cache hit), distinct keys yield distinct handles, returned handle is resident per GL state query. -- **`WbDrawDispatcherIndirectBuilderTests`** β€” pure CPU test: given a fixture of `(entity, mesh, batch)` tuples, verify the indirect buffer layout: `count` / `firstIndex` / `baseVertex` / `baseInstance` per group, opaque section sorted front-to-back, transparent section in classification order (no sort β€” back-to-front sort can be added in a follow-up if measured useful). -- **`WbDrawDispatcherTranslucencyTests`** β€” verify groups land in correct indirect buffer (opaque vs transparent) per `TranslucencyKind`. `Additive`/`InvAlpha` go to transparent. `ClipMap` goes to opaque. Empty groups skipped. -- **Existing N.4 tests stay green.** All 60 tests captured by `FullyQualifiedName~Wb|MatrixComposition` filter remain at 60/0. - -### 8.2 Visual verification - -Same gate as N.4 used. Live ACE + retail dat, in-world testing. - -- **Holtburg courtyard** β€” characters + scenery + buildings render identically to N.4. No missing entities, no z-fighting, no exploded parts. -- **Foundry interior** β€” dense static-object scene, stress-tests indirect call count and translucency classification. -- **Indoor β†’ outdoor cell transition** β€” confirms cell visibility filtering still works (we cull on CPU; dispatcher should never see invisible-cell entities). -- **Drudge / character close-up** β€” confirms Issue #47 close-detail mesh preservation. -- **Magic content (additive fallback check)** β€” Foundry runes, glowing weapons if observable, boss models with luminous decals. Trigger spec amendment if regression spotted. - -User-confirms each. These are visual identity checks against the running N.4 behavior (use `git stash` of N.5 changes + relaunch as the comparison baseline). - -### 8.3 Perf measurement (the win gate) - -`[WB-DIAG]` augmented: - -``` -[WB-DIAG] entSeen=N entDrawn=M ... drawsIssued=K groups=G (existing) -[WB-DIAG] cpu_us=Xmedian/Y95p gpu_us=Zmedian/W95p (new) -``` - -Capture before/after numbers in fixed scenes/cameras: - -| Scene | Camera position | Metric | -|---|---|---| -| Holtburg courtyard | 30m elevated, looking SW | `cpu`, `gpu`, `drawsIssued` | -| Foundry interior | character spawn, default heading | `cpu`, `gpu`, `drawsIssued` | -| Open landscape | terrain wander, no entities | `cpu`, `gpu`, `drawsIssued` (sanity) | - -**Acceptance gates** (paste into SHIP commit message): - -- Visual identity to N.4 β€” confirmed via Β§8.2. -- CPU dispatcher time ≀ 70% of N.4 in Holtburg courtyard (target: β‰₯30% reduction). -- GPU rendering time within Β±10% of N.4 (sanity: no regression). -- `drawsIssued ≀ 5 per pass` (down from "few hundred per pass"). -- All tests green β€” 60+ Wb tests + new bindless/indirect tests. -- `ACDREAM_USE_WB_FOUNDATION=0` still works β€” `InstancedMeshRenderer` fallback runs and renders correctly. - -### 8.4 Long-session sanity check - -Hour-long session with `ACDREAM_WB_DIAG=1`. Watch resident-handle count grow. Expected: bounded plateau under 5K once content set is fully traversed. If unbounded growth, residency policy revisit required in N.6. - ---- - -## 9. Risks - -| Risk | Likelihood | Impact | Mitigation | -|---|---|---|---| -| Driver bug in bindless residency | Low (mature in 2025+ drivers) | Crash / black textures | One-time logging on first failure; legacy fallback under flag-off | -| Driver bug in `glMultiDrawElementsIndirect` | Low | GL_INVALID_OPERATION | Capability check + first-failure logging + fallback | -| Resident handle count exceeds driver limit in long session | Low (acdream content is bounded) | Cumulative GPU memory pressure β†’ eventual eviction surprises | `[WB-DIAG]` resident-count log; revisit eviction in N.6 if it grows unbounded | -| Shader compile fails on weird GPU | Medium-low | First-launch failure | Compile-error catch + fallback to `InstancedMeshRenderer` | -| Additive fidelity regression on rare GfxObj surfaces | Medium | Subtle visual difference | Visual verification at magic-themed content; spec amendment for additive sub-pass if found | -| `gl_BaseInstanceARB` fields not advancing per-instance attribs we still use | Low (we drop attribs entirely) | Wrong matrices | All instance data via SSBO; no vertex attrib at locations 3-6 to misalign | -| SSBO indexing GPU cost worse than uniform-array | Low (well-optimized in modern drivers) | Possible GPU time regression | GL timer queries detect; if observed, fall back to uniform array of bounded size | -| Persistent-mapped buffer foot-guns (chosen NOT to use in N.5) | n/a | n/a | Decision 7 defers to N.6 | -| Per-instance highlight (selection blink) feature creep | Low | Scope grows | Decision 8 defers; field reserved in design doc | - ---- - -## 10. Out of scope (explicitly) - -The following are NOT N.5 work. They become possible follow-ons. - -- **WB's `TextureAtlasManager` adoption for atlas tier.** N.5 keeps acdream's `TextureCache` as the texture owner for everything. Atlas adoption is N.6+ if memory pressure shows up. -- **Persistent-mapped buffer ring with sync fences.** Decision 7. N.6 candidate if profiling shows residual `glBufferData` cost. -- **GPU-side culling (compute pre-pass).** Future phase. -- **Texture array repacking for multi-layer per-instance composites.** Future, if many palette-overrides actually share dimensions and could be packed. -- **Selection-blink highlight color.** Decision 8. Phase B.4 follow-up. Field reserved in `InstanceData` design (extend stride to 80 bytes when implementing). -- ~~**Deletion of legacy `InstancedMeshRenderer`.** N.6.~~ **Done in N.5 ship amendment** β€” `InstancedMeshRenderer`, `StaticMeshRenderer`, and `WbFoundationFlag` were deleted in the retirement commit. -- **Terrain wiring through WB.** Future. - ---- - -## 11. Open questions - -None outstanding. All 8 brainstorm questions resolved + 1 clarification on highlight semantics. Ready for plan. - ---- - -*End of design.* diff --git a/docs/superpowers/specs/2026-05-09-phase-a5-two-tier-streaming-design.md b/docs/superpowers/specs/2026-05-09-phase-a5-two-tier-streaming-design.md deleted file mode 100644 index eaf92ca..0000000 --- a/docs/superpowers/specs/2026-05-09-phase-a5-two-tier-streaming-design.md +++ /dev/null @@ -1,829 +0,0 @@ -# Phase A.5 β€” Two-tier Streaming + Horizon LOD β€” Design - -**Created:** 2026-05-09 (immediately after N.5b ship + brainstorm). -**Status:** Spec β€” awaiting user review before plan-writing. -**Branch:** `claude/hopeful-darwin-ae8b87` (worktree under `.claude/worktrees/hopeful-darwin-ae8b87`). -**Predecessor:** Phase N.5b SHIP at `08b7362`. A.5 handoff at `f7f8867`. - ---- - -## 1. Goal - -Scale acdream's visible reach from radius=5 (~1 km) to radius=12 (~2.3 km horizon) -while sustaining 240 FPS at standstill on a 240 Hz / 1440p monitor. - -Delivered through: -1. Two-tier streaming (near = full detail, far = terrain only). -2. Tightening the existing per-LB entity dispatcher walk. -3. Off-thread mesh build (single worker). -4. Fog blend at the near-tier boundary to mask the scenery cutoff. -5. Three nearly-free visual quality wins (terrain mipmaps + anisotropic, A2C - with MSAA on foliage, depth-write audit). - -The headline win: walking around Holtburg, the user sees a real horizon -(2.3 km of visible terrain) without the client falling off a perf cliff. - -**User goal verbatim (2026-05-09):** -> "I just want great smooth HIGH fps visuals. Should look great. As long as -> it scales and we get very high FPS" - ---- - -## 2. Hardware target + acceptance metrics - -### Target hardware - -- AMD Radeon RX 9070 XT (RDNA 4, ~December 2025). -- 240 Hz @ 2560Γ—1440 (verified via `Get-CimInstance Win32_VideoController`). -- Frame budget: **4.166 ms** at vsync. - -### Acceptance metrics (as shipped β€” revised with Quality Preset system) - -1. **Build green; existing tests still green.** N.5b conformance sentinel - passes (visual mesh Z = TerrainSurface.SampleZ within 1 mm). -2. **Standstill at user's selected preset on user's hardware:** - - 95% of frames hit ≀ (1000ms / monitor refresh rate). - - No absolute FPS number is required β€” the Quality Preset system (Β§4.10) - is the user's knob for trading quality vs frame budget. -3. **Walking at user's selected preset:** - - 95% of frames hit ≀ 1.5Γ— (1000ms / monitor refresh rate). -4. **First traversal into virgin region (cold mesh cache):** - - Render thread frame time stays within 2Γ— the standstill budget while - the worker fills the far-tier horizon (~2.7 s of "horizon filling in" is OK). -5. **Visual gate (user-driven, same on all presets):** user launches the - client, walks Holtburg β†’ North Yanshi, and confirms: - - Horizon visible at ~2.3 km. - - Fog blend at N₁ smooths the scenery boundary (no harsh cliff). - - Distant terrain does not shimmer (mipmaps work). - - Tree edges are smooth (A2C works). - - No new z-fighting / depth artifacts (depth-write audit). -6. **Per-subsystem regression budgets** (added to `[WB-DIAG]` / - `[TERRAIN-DIAG]` output): - - Entity dispatcher cpu_us median ≀ **2.0 ms** at standstill. - - Terrain dispatcher cpu_us median ≀ **1.0 ms** at standstill (all 625 LBs). -7. **N.5b sentinel intact:** TerrainSlot, TerrainModernConformance, Wb*, - MatrixComposition, TextureCacheBindless, SplitFormulaDivergence β€” all - pass clean. -8. **SHIP record + perf baseline doc + memory entry** mirroring N.5b's pattern. - -A failure on (5) is a SHIP-blocker. A failure on (3) walking-FPS criterion -escalates to "fix or document the tradeoff and ship N.6 next" β€” not a -direct blocker but pushes the gate to user discretion. - ---- - -## 3. Two-tier streaming model - -### Tier definitions - -| Tier | Radius | LB count | Loads | GPU mem | -|---|---|---|---|---| -| **Near** (N₁ = 4) | 9Γ—9 = 81 LBs | terrain mesh + LandBlockInfo (stabs/buildings) + scenery generation + EnvCells + collision data + entity registration with WB dispatcher | scenery instance buffers + per-entity textures (depends on PaletteOverrides) | -| **Far** (Nβ‚‚ = 12) | 25Γ—25 - 9Γ—9 = 544 LBs | terrain mesh ONLY (LandBlock heightmap + atlas blend) | ~14 MB shared atlas slots | -| **Total** | 25Γ—25 = 625 LBs | combined | ~30 MB total estimated | - -### Hysteresis (Q7 Option A β€” match existing radius+2 convention) - -- **Near-tier:** entity load at distance 4, demote (entity unload) at distance 6. -- **Far-tier:** terrain load at distance 12, terrain unload at distance 14. - -Both boundaries get the same 2-LB buffer. Phase A.1's existing hysteresis -mechanism in `StreamingRegion.RecenterTo` is the reference pattern; A.5 -extends it from one radius to two. - -### Tier transitions - -| Transition | Trigger | Action | -|---|---|---| -| `null β†’ far` | LB enters far window from outside | Worker reads LandBlock heightmap, builds mesh, posts `LandblockStreamResult.Loaded { Tier = Far }`. Render thread adds slot in `TerrainModernRenderer`. No entity work. | -| `null β†’ near` | LB jumps null β†’ near in one tick (first-tick bootstrap; teleport into virgin region) | Worker reads LandBlock heightmap + `LandBlockInfo`, generates scenery, builds entity list, builds mesh. Posts `LandblockStreamResult.Loaded { Tier = Near }`. Render thread adds terrain slot AND merges entities. | -| `far β†’ near` | LB enters near window from far-resident | Worker reads `LandBlockInfo`, generates scenery, builds entity list. Posts `LandblockStreamResult.Promoted`. Render thread merges entities into `GpuWorldState` for the existing LB (terrain already loaded). | -| `near β†’ far` | LB leaves near window past hysteresis (distance > 6) | Render thread drops the LB's entities from `GpuWorldState` (which fires `_wbSpawnAdapter.OnLandblockUnloaded`). Terrain stays. | -| `far β†’ null` | LB leaves far window past hysteresis (distance > 14) | Render thread removes the terrain slot from `TerrainModernRenderer`. | - -The order matters: when a player walks outward, the same LB goes -`near β†’ far β†’ null` over time. Each transition is one event per LB per -crossing. - -### Why the player crossing the N₁ boundary works - -The player is always at radius=0 from the streaming center (the streaming -center IS the player). The boundary effects are about LBs at the edge of N₁ -crossing inward/outward as the player moves. Server-spawned NPCs are -delivered by ACE's broadcast (radius typically 5-7 LBs β‰₯ N₁), so when an -LB promotes back to near, ACE will already have its NPCs broadcast or -re-broadcast as the player moves through. Dat-static entities (stabs, -buildings) are reloaded from `LandBlockInfo` on promotion. Scenery is -re-generated from the deterministic seed at the same time. - ---- - -## 4. Component-by-component design - -### 4.1 `LandblockStreamTier` β€” new enum - -```csharp -namespace AcDream.App.Streaming; - -public enum LandblockStreamTier -{ - Far, // terrain only - Near, // full detail (terrain + entities + scenery + EnvCells) -} -``` - -### 4.2 `StreamingRegion` β€” extended to two radii - -```csharp -public sealed class StreamingRegion -{ - public int CenterX { get; } - public int CenterY { get; } - public int NearRadius { get; } // N₁ (default 4) - public int FarRadius { get; } // Nβ‚‚ (default 12) - - public IReadOnlyCollection NearVisible { get; } // 9Γ—9 window - public IReadOnlyCollection FarVisible { get; } // 25Γ—25 window minus near - public IReadOnlyCollection Resident { get; } // hysteresis-retained - - public TwoTierDiff RecenterTo(int newCx, int newCy); -} - -public readonly record struct TwoTierDiff( - IReadOnlyList ToLoadFar, // entered far window from null (need terrain only) - IReadOnlyList ToLoadNear, // entered near window from null (need terrain + entities β€” first-tick bootstrap, teleport) - IReadOnlyList ToPromote, // entered near window from far-resident (need entities only β€” terrain already loaded) - IReadOnlyList ToDemote, // exited near window past hysteresis (drop entities) - IReadOnlyList ToUnload); // exited far window past hysteresis (drop terrain) -``` - -The hysteresis math: -- Near-unload threshold: `NearRadius + 2` = 6. -- Far-unload threshold: `FarRadius + 2` = 14. - -A landblock is "near-resident" if its distance ≀ 6; "far-resident" if its -distance is in (6, 14]. Beyond 14, it unloads entirely. - -### 4.3 `StreamingController` β€” routes by tier - -```csharp -public sealed class StreamingController -{ - public int NearRadius { get; set; } = 4; - public int FarRadius { get; set; } = 12; - public int MaxCompletionsPerFrame { get; set; } = 4; - - // Action signatures change to carry the tier. - private readonly Action _enqueueLoad; - private readonly Action _enqueueUnload; - // ... - - public void Tick(int observerCx, int observerCy) - { - // First-tick bootstrap: every near-window LB β†’ ToLoadNear; every - // far-window-only LB β†’ ToLoadFar. - // Steady-state RecenterTo: produces 5 transition lists. - // - ToLoadFar β†’ _enqueueLoad(id, JobKind.LoadFar) - // - ToLoadNear β†’ _enqueueLoad(id, JobKind.LoadNear) - // - ToPromote β†’ _enqueueLoad(id, JobKind.PromoteToNear) - // - ToDemote β†’ _state.RemoveEntities(id) on render thread (no worker job) - // - ToUnload β†’ _enqueueUnload(id) - // Drain completions and route by result variant. - } -} - -public enum LandblockStreamJobKind { LoadFar, LoadNear, PromoteToNear } -``` - -The render thread decides the job kind up-front based on its own knowledge -of which LBs are currently terrain-resident; the worker never peeks at -render-thread state. Three distinct worker paths: - -- **`LoadFar`:** read `LandBlock` heightmap only. Skip `LandBlockInfo`, - skip `LandblockLoader.BuildEntitiesFromInfo`, skip - `SceneryGenerator`/`WbSceneryAdapter`. Build `LandblockMesh`. Post - `LandblockStreamResult.Loaded(Tier=Far, Entities=[], MeshData=mesh)`. -- **`LoadNear`:** read `LandBlock` + `LandBlockInfo` + scenery generation - + build mesh. Post `LandblockStreamResult.Loaded(Tier=Near, Entities=..., - MeshData=mesh)`. Used for first-tick bootstrap of the inner ring and - for the rare nullβ†’Near jump (teleport into virgin region). -- **`PromoteToNear`:** read `LandBlockInfo` + scenery generation only. - Skip `LandBlock` heightmap (mesh already on GPU). Skip - `LandblockMesh.Build`. Post `LandblockStreamResult.Promoted(id, entities)`. - -### 4.4 `LandblockStreamResult` β€” new variants - -```csharp -public abstract record LandblockStreamResult -{ - public sealed record Loaded( - uint LandblockId, - LandblockStreamTier Tier, - LandBlock Heightmap, - IReadOnlyList Entities, // empty for Far - LandblockMeshData MeshData // built off-thread - ) : LandblockStreamResult; - - public sealed record Promoted( - uint LandblockId, - IReadOnlyList Entities // entity layer for an already-loaded far-tier LB - ) : LandblockStreamResult; - - // Existing: - public sealed record Unloaded(uint LandblockId) : LandblockStreamResult; - public sealed record Failed(uint LandblockId, string Error) : LandblockStreamResult; - public sealed record WorkerCrashed(string Error) : LandblockStreamResult; -} -``` - -`Loaded` carries `MeshData` β€” the mesh is built on the worker thread, NOT -in `_applyTerrain` on the render thread. `Promoted` only carries entities; -the mesh is already in `TerrainModernRenderer`. - -### 4.5 `LandblockStreamer` β€” single worker, mesh-build on-worker - -Existing `LandblockStreamer` (today on a single background thread) gets -extended to: - -1. Read dat as today (`DatCollection.Get` etc.). -2. Build `LandblockMesh` on the same thread: - ```csharp - var meshData = LandblockMesh.Build( - block, lbX, lbY, heightTable, _ctx, _surfaceCache); - ``` -3. Post `LandblockStreamResult.Loaded(... MeshData = meshData)` to the - completion queue. - -Thread-safety implications: -- `_ctx` (TerrainBlendingContext) is read-only after init β€” no change. -- `_surfaceCache`: today a plain `Dictionary`, - populated lazily by `LandblockMesh.Build`. Currently safe because - Build runs on the render thread; A.5 moves Build to the worker, so - the cache must be thread-safe. **Swap to - `ConcurrentDictionary`** with `GetOrAdd` for the - populate path. The factory inside `GetOrAdd` may run twice for the - same key under contention (acceptable β€” the result is deterministic). - -### 4.6 `WbDrawDispatcher` β€” entity bucketing tightening (Q5 Option A) - -Three targeted changes inside the existing `Draw` flow: - -#### Change 1: Animated-entity walk fix - -Today (at lines 197-204 of `WbDrawDispatcher.cs`): - -```csharp -foreach (var entry in landblockEntries) { - bool landblockVisible = ...; - if (!landblockVisible && (animatedEntityIds is null || animatedEntityIds.Count == 0)) - continue; - - foreach (var entity in entry.Entities) { - ... - if (!landblockVisible && !isAnimated) continue; -``` - -The `if (!landblockVisible && ...) continue;` only skips if there are NO -animated entities. When `animatedEntityIds` is non-empty, the inner loop -walks every entity in the invisible LB just to find the few animated -ones. With ~10.7K entities at N₁=4, this is wasted iteration. - -**Fix:** when an LB is invisible, iterate `animatedEntityIds` directly -and look each up in a per-LB `Dictionary` map (added -to `LoadedLandblock` or kept in a parallel structure). - -```csharp -foreach (var entry in landblockEntries) { - bool landblockVisible = ...; - if (!landblockVisible) { - if (animatedEntityIds is null || animatedEntityIds.Count == 0) continue; - // Walk only animated entities in this invisible LB. - foreach (var animatedId in animatedEntityIds) { - if (!entry.AnimatedById.TryGetValue(animatedId, out var entity)) continue; - // ... draw the entity - } - continue; - } - foreach (var entity in entry.Entities) { ... } -} -``` - -#### Change 2: Per-entity AABB cache at register time - -Today: `Draw` recomputes `aMin = position - 5`, `aMax = position + 5` per -entity per frame. Cheap individually, but ~16K Γ— per frame = measurable. - -**Fix:** add `Vector3 AabbMin, AabbMax` fields to `WorldEntity` (or a -parallel struct keyed by entity id). Populate at `EntitySpawnAdapter.OnCreate` -(server-spawned) and `LandblockLoader.BuildEntitiesFromInfo` (dat-static) -time. Static entities never invalidate. Dynamic entities (NPCs, players) -update on position change β€” add `WorldEntity.PositionDirty` flag set by -the live position update path; AABB recompute happens lazily on first -read after dirty. - -The AABB radius today is hard-coded `PerEntityCullRadius = 5.0f` β€” keep -that as a per-mesh-bucket fallback; future improvement is to compute the -real AABB from the mesh, but defer that to a later phase (it's a -cross-cutting change). - -#### Change 3: 4Γ—4 sub-LB cell cull for partially-visible LBs - -When an LB is fully visible (its AABB entirely inside the frustum), all -its entities are drawn β€” no per-entity cull needed. Today's per-entity -cull is wasted work in this case. - -When an LB is partially visible, today's per-entity cull is the right -work β€” but it walks all ~132 entities. Cheap with the AABB-cache fix -(memory read), so the win here is small. Worth doing only if the cache -fix alone isn't enough to hit the 2.0ms budget. - -**Add only if needed:** bucket each LB's entities into 4Γ—4 sub-cells -(each 48 m). Compute a sub-cell AABB at register time. Per frame: for -partially-visible LBs, cull at sub-cell granularity first; walk -entities only inside surviving sub-cells. - -Ship change #1 and #2 unconditionally; ship #3 only if the budget -isn't hit by #1 + #2. - -### 4.7 `TerrainModernRenderer` β€” no structural change - -The slot allocator (`TerrainSlotAllocator`) already grows by power-of-two -doubling. At Nβ‚‚=12 worst case, ~961 slots Γ— ~15 KB per slot = ~14 MB. -Allocator handles it without modification. - -Per-LB frustum cull stays per-slot β€” at ~961 slots Γ— ~0.3 Β΅s/AABB-test -the worst-case cull pass is ~0.3 ms. Acceptable inside the 1.0 ms terrain -dispatcher budget. - -The DEIC (`DrawElementsIndirectCommand`) array grows accordingly. The -existing per-frame `BufferSubData` upload absorbs a 961-entry array -without issue (~19 KB). - -### 4.8 Fog tuning (`SceneLightingUbo`) - -Existing fields (Phase G.1+): -- `FogStart` β€” distance at which fog begins (today: somewhere outside the - visible terrain range). -- `FogEnd` β€” distance at which fog reaches full opacity. -- `FogColor` β€” sourced from current sky state. - -A.5 change: dynamically tune `FogStart` and `FogEnd` based on the -current N₁/Nβ‚‚: - -- `FogStart = N₁ Γ— LandblockSize Γ— 0.7` β‰ˆ `4 Γ— 192 Γ— 0.7` = **~538 m**. -- `FogEnd = Nβ‚‚ Γ— LandblockSize Γ— 0.95` β‰ˆ `12 Γ— 192 Γ— 0.95` = **~2188 m**. - -The fog color matches the current sky color (already provided by -`SkyStateProvider`) β€” at the far horizon, fog blends terrain into -sky, hiding the Nβ‚‚ edge. - -The 0.7 / 0.95 multipliers are tuning knobs. Iterate during user gate. -**Expose as env vars during development** (`ACDREAM_FOG_START_MULT`, -`ACDREAM_FOG_END_MULT`) to allow fast iteration without a recompile. - -### 4.9 Visual quality wins (Q8 Option C β€” all three) - -#### 4.9.1 Mipmaps + 16x anisotropic on `TerrainAtlas` - -Today: `TerrainAtlas.Upload` uses `GL_LINEAR` minification, no mipmaps. - -A.5 change: after upload, call `glGenerateMipmap(GL_TEXTURE_2D_ARRAY)`. -Sampler state: `GL_LINEAR_MIPMAP_LINEAR` (trilinear) + -`GL_TEXTURE_MAX_ANISOTROPY = 16`. - -Affects only `TerrainAtlas`. Mesh atlas (entity textures) and other -texture caches stay as-is. - -Verification: at Nβ‚‚=12, walk to a vantage point looking at terrain at -range 2 km. With the fix, no shimmer. Without, "moving sparkles" visible -at distance. - -#### 4.9.2 Alpha-to-coverage with MSAA on foliage - -Today: `mesh_modern.frag` uses `if (alpha < cutoff) discard;` for ClipMap -translucency. Produces hard, pixel-edged tree silhouettes. - -A.5 change: -- Enable MSAA 4x on the GL render target (window framebuffer). -- In `mesh_modern.frag`, for ClipMap pass: write - `gl_SampleMask[0]` based on alpha threshold instead of binary discard. - -Risk: MSAA framebuffer interaction with sky / particles / UI overlay. -Audit: -- `SkyRenderer` β€” clears its own framebuffer? If so, must clear the MSAA - attachment instead. Investigate. -- `ParticleRenderer` β€” billboards already use alpha-blend; MSAA-friendly. -- ImGui overlay β€” drawn after the 3D pass; must not interact with MSAA - resolve. - -If the audit finds blocking issues, ship 4.9.1 + 4.9.3 only and defer -4.9.2 to a later phase. Document the result either way. - -#### 4.9.3 Depth-write audit on translucent batches - -Walk all translucent batch paths in `WbDrawDispatcher.Draw` and verify: -- Alpha-blend (`AlphaBlend`, `Additive`, `InvAlpha`): `glDepthMask(false)`. -- Clip-map (binary alpha): `glDepthMask(true)` (foliage casts depth). -- Opaque: `glDepthMask(true)`. - -Today's code at lines 401-433 sets `DepthMask(true)` for opaque, -`DepthMask(false)` for transparent. Confirm ClipMap is in the opaque -pass (it is, per `IsOpaque` returning true for ClipMap at line 738). - -If audit finds nothing wrong, ship a comment + a unit test that locks in -the partition. Cheap insurance against future regression. - -### 4.10 Quality Preset System (T22.5 β€” added mid-execution) - -**Background:** Added between T22 (fog wiring) and T23 (DIAG budgets) at -user's direction. The original spec had no preset concept; Β§2 was written -against absolute 240 FPS on fixed N₁/Nβ‚‚. T22.5 makes both radii and every -quality knob user-controllable via a single enum. Β§2 was amended above to -reflect the per-preset, refresh-rate-relative acceptance criteria. - -#### Schema - -```csharp -public enum QualityPreset { Low, Medium, High, Ultra } - -public readonly record struct QualitySettings( - int NearRadius, - int FarRadius, - int MsaaSamples, - int AnisotropicLevel, - bool AlphaToCoverage, - int MaxCompletionsPerFrame); -``` - -`QualitySettings.From(preset)` returns the canonical values: - -| Preset | NearRadius | FarRadius | MsaaSamples | AnisotropicLevel | AlphaToCoverage | MaxCompletionsPerFrame | -|---|---|---|---|---|---|---| -| Low | 2 | 5 | 0 | 4 | false | 2 | -| Medium | 3 | 8 | 2 | 8 | false | 3 | -| High | 4 | 12 | 4 | 16 | true | 4 | -| Ultra | 5 | 15 | 4 | 16 | true | 6 | - -`QualitySettings.WithEnvOverrides(baseSettings)` applies per-field env-var -overrides (see Β§4.10.3). - -#### Persistence and UI - -`DisplaySettings.Quality` (type `QualityPreset`) persists via the existing -`settings.json` infrastructure (Phase L.0). The Settings panel (F11) exposes -a Quality dropdown in its Display tab (`SettingsPanel.RenderDisplayTab`). - -#### Wiring (GameWindow.OnLoad + ReapplyQualityPreset) - -1. `GameWindow.OnLoad` resolves the active `QualitySettings`: - `QualitySettings.From(displaySettings.Quality).WithEnvOverrides(...)`. -2. `StreamingController` and `LandblockStreamer` are built with the preset's - `NearRadius` / `FarRadius`. -3. `TerrainAtlas.SetAnisotropic(settings.AnisotropicLevel)` called once at - load and again on reapply. -4. `WindowOptions.Samples = settings.MsaaSamples` applied at window creation - time only (MSAA mid-session change is structurally unsupported by OpenGL). -5. `WbDrawDispatcher.AlphaToCoverage = settings.AlphaToCoverage`. -6. `StreamingController.MaxCompletionsPerFrame = settings.MaxCompletionsPerFrame`. - -Mid-session quality change (F11 dropdown change β†’ Save): - -- `GameWindow.ReapplyQualityPreset` rebuilds `StreamingController` + - `LandblockStreamer` with the new radii, re-applies anisotropic and - AlphaToCoverage. -- If `MsaaSamples` changed, logs a warning that MSAA sample count cannot be - changed mid-session; requires restart. - -#### Env-var overrides (Β§4.10.3) - -Applied by `QualitySettings.WithEnvOverrides` after the base preset is resolved. -Each field has one env var; all are optional. Logged at startup. - -| Env var | Field overridden | -|---|---| -| `ACDREAM_NEAR_RADIUS` | `NearRadius` | -| `ACDREAM_FAR_RADIUS` | `FarRadius` | -| `ACDREAM_MSAA_SAMPLES` | `MsaaSamples` | -| `ACDREAM_ANISOTROPIC` | `AnisotropicLevel` | -| `ACDREAM_A2C` | `AlphaToCoverage` (1/0/true/false) | -| `ACDREAM_MAX_COMPLETIONS_PER_FRAME` | `MaxCompletionsPerFrame` | - -#### Tests - -12 tests in `tests/AcDream.UI.Abstractions.Tests/Settings/QualityPresetTests.cs` -cover: canonical preset values per enum member; `WithEnvOverrides` no-op when -no env vars set; `WithEnvOverrides` each override individually; invalid env-var -value falls back to base setting. - -#### Files - -- `src/AcDream.UI.Abstractions/Settings/QualityPreset.cs` β€” new -- `src/AcDream.UI.Abstractions/Settings/DisplaySettings.cs` β€” `Quality` field added -- `src/AcDream.UI.Abstractions/Panels/Settings/SettingsPanel.cs` β€” Display tab - Quality dropdown (`RenderDisplayTab` method) -- `src/AcDream.App/Rendering/GameWindow.cs` β€” `ReapplyQualityPreset`, - `OnLoad` preset wiring -- `tests/AcDream.UI.Abstractions.Tests/Settings/QualityPresetTests.cs` β€” new (12 tests) - -#### Out of scope (deferred) - -- Auto-detect preset on first launch (Phase A.6 / N.6.5). -- Adaptive runtime preset drop on budget miss. -- Per-feature toggles below preset level. - -Commits: `afa4200` (schema + tests), `28d2c60` (wiring). - ---- - -## 5. Data flow - -### Per-frame (steady state) - -``` -GameWindow.OnUpdate(dt) - └─ StreamingController.Tick(playerCx, playerCy) - β”œβ”€ region.RecenterTo(...) // produces TwoTierDiff if center changed - β”œβ”€ for each ToLoadFar: _enqueueLoad(id, LoadFar) - β”œβ”€ for each ToLoadNear: _enqueueLoad(id, LoadNear) - β”œβ”€ for each ToPromote: _enqueueLoad(id, PromoteToNear) - β”œβ”€ for each ToDemote: _state.RemoveEntities(id) // on render thread - β”œβ”€ for each ToUnload: _enqueueUnload(id) - └─ drainCompletions(MaxCompletionsPerFrame=4) - β”œβ”€ Loaded.Far: _terrain.AddLandblock(meshData); _state.AddLandblock(...) - β”œβ”€ Loaded.Near: _terrain.AddLandblock(meshData); _state.AddLandblock(... entities) - β”œβ”€ Promoted: _state.AddEntitiesToExisting(id, entities) - β”œβ”€ Unloaded: _terrain.RemoveLandblock(id); _state.RemoveLandblock(id) - └─ Failed/Crash: log - -GameWindow.OnRender - β”œβ”€ TerrainModernRenderer.Draw(camera, frustum) - β”‚ └─ glMultiDrawElementsIndirect across all near + far slots that pass cull - └─ WbDrawDispatcher.Draw(camera, gpuWorldState.LandblockEntries, frustum, visibleCellIds, animatedEntityIds) - β”œβ”€ for each LB entry: - β”‚ β”œβ”€ if invisible: walk only animatedEntityIds (Change #1) - β”‚ └─ if visible: walk entities, AABB cache lookup (Change #2) - β”œβ”€ classify into groups, build SSBO, multi-draw indirect - └─ flush DIAG every ~5 s -``` - -### Worker thread - -``` -LandblockStreamer.WorkerLoop - while running: - job = jobQueue.dequeue() - switch job.Kind: - LoadFar: - block = dats.Get(id) - meshData = LandblockMesh.Build(block, ..., _surfaceCache) - completionQueue.enqueue(Loaded(id, Far, block, [], meshData)) - LoadNear: - block = dats.Get(id) - info = dats.Get(...) - entities = LandblockLoader.BuildEntitiesFromInfo(info) - scenery = WbSceneryAdapter.GenerateScenery(block, ...) - meshData = LandblockMesh.Build(block, ..., _surfaceCache) - completionQueue.enqueue(Loaded(id, Near, block, entities βˆͺ scenery, meshData)) - PromoteToNear: - info = dats.Get(...) - // Heightmap not re-read; scenery generation needs LandBlock for height - // sampling β€” read it again from disk cache (DatCollection caches the - // last-read block; cheap second access) OR pass through from render - // thread's terrain-slot snapshot (deferred plan-level decision). - block = dats.Get(id) - entities = LandblockLoader.BuildEntitiesFromInfo(info) - scenery = WbSceneryAdapter.GenerateScenery(block, ...) - completionQueue.enqueue(Promoted(id, entities βˆͺ scenery)) -``` - ---- - -## 6. Threading model - -- **Render thread:** drives `StreamingController.Tick`, drains the - completion queue, calls `TerrainModernRenderer.AddLandblock` / - `RemoveLandblock`, mutates `GpuWorldState`. All GL calls on this thread. -- **One streaming worker thread:** dat reads, mesh build, scenery generation. - Owns `_surfaceCache` (now `ConcurrentDictionary`) β€” render thread does - not access it directly. -- **Network thread:** unchanged from Phase A.3 β€” drains UDP into the - channel; render thread decodes. - -Synchronization: -- Job queue: `Channel` (writer = render thread via - `_enqueueLoad`; reader = worker). -- Completion queue: `ConcurrentQueue` (writer = - worker; reader = render thread). -- `_surfaceCache`: `ConcurrentDictionary` populated by - `LandblockMesh.Build` on the worker; read by future paths if any - (none today). -- `TerrainBlendingContext`: read-only post-init. No lock. - ---- - -## 7. Error handling - -- **Worker crash:** caught in worker loop, posts - `LandblockStreamResult.WorkerCrashed`. Render thread logs to console. - (Existing pattern.) -- **Dat read failure:** posts `LandblockStreamResult.Failed`. Render - thread logs. Streaming continues with the LB skipped β€” region still - tracks it as resident so we don't retry forever, but the slot stays empty. -- **AABB cache invalidation race:** dynamic entity moves while the - dispatcher is walking. Acceptable β€” at worst, the entity culls or - draws based on the previous frame's position. Position is updated in - the network handler (also render-thread today) so no actual race. -- **Promotion timing:** if the player crosses N₁ inward, we enqueue a - `Near` load on the worker. Until it completes, the LB has terrain but - no scenery / entities. Frame budget is unaffected (only `LoadedLandblock` - changes, and the dispatcher already handles missing entities by walking - zero-length lists). -- **Unload during in-flight load:** enqueue an unload while a load is - in flight. When the load completes, render thread sees the LB is no - longer resident β€” drop the result silently. Same pattern as today. - ---- - -## 8. Testing strategy - -### Unit tests (offline, no GL) - -Add to `tests/AcDream.Core.Tests/Streaming/`: -- `StreamingRegion_TwoTier_FirstTick_LoadsNearAndFarSeparately` β€” first - call produces `ToLoadNear` populated for inner ring, `ToLoadFar` - populated for outer ring, `ToPromote` empty (nothing was previously - resident). -- `StreamingRegion_TwoTier_NullToFar_OnFarRingEntry` β€” LB rolls into - far window from null. Asserts entry in `ToLoadFar`, not - `ToLoadNear`. -- `StreamingRegion_TwoTier_FarToNear_OnNearRingEntry` β€” LB was - far-resident, player walks toward it, LB enters near window. Asserts - entry in `ToPromote`, not `ToLoadNear`. -- `StreamingRegion_TwoTier_NullToNear_OnTeleport` β€” observer center - jumps far enough that an LB goes from null β†’ Near in one frame - (e.g., teleport). Asserts entry in `ToLoadNear`, not `ToPromote`. -- `StreamingRegion_TwoTier_NearToFar_OnNearBoundaryExitPlusHysteresis` β€” - asserts entry in `ToDemote` only after distance exceeds - `NearRadius + 2`. -- `StreamingRegion_TwoTier_FarToNull_OnFarBoundaryExitPlusHysteresis` β€” - asserts entry in `ToUnload` only after distance exceeds - `FarRadius + 2`. -- `StreamingRegion_TwoTier_HysteresisHoldsAcrossOscillation` β€” walk - back-and-forth across N₁ five times within the hysteresis radius; - assert no demote events fire. -- `StreamingController_TwoTier_DrainsRoutedByVariant` β€” `Loaded.Far`, - `Loaded.Near`, and `Promoted` each route to the right state mutation - on the render thread. - -Add to `tests/AcDream.Core.Tests/Rendering/Wb/`: -- `WbDrawDispatcher_AnimatedEntities_InInvisibleLb_NoFullEntityWalk` β€” - verify Change #1 (only iterates `animatedEntityIds`, not `Entities`). -- `WbDrawDispatcher_PerEntityAabbCached_NotRecomputed` β€” assert AABB - fields are read, not recomputed, for static entities. - -### Conformance tests - -- `TerrainModernConformanceTests` (existing) β€” must still pass. The - visual mesh Z must agree with `TerrainSurface.SampleZFromHeightmap` - to within 1 mm across both tiers. -- `LandblockMeshTests` (existing) β€” must still pass. Worker-thread - mesh build produces byte-identical results to render-thread build - for the same inputs. - -### Perf gate (manual, with `[WB-DIAG]` + `[TERRAIN-DIAG]`) - -- **Standstill bench:** launch with `ACDREAM_WB_DIAG=1`, stand at - Holtburg dueling field for 60 s. Read median + p95 + p99 from log. -- **Walking bench:** launch with diag, run from Holtburg to North - Yanshi, ~60 s. Same metrics. -- **First traversal bench:** clear OS file cache (or reboot), launch - with diag, walk into a region not previously visited, capture the - worker-thread fill duration + render-thread frame time during fill. - -### Visual gate (manual, user-driven) - -User launches the client, walks the standard route, confirms: -1. Horizon visible at 2.3 km. -2. Fog blend is smooth (no scenery cliff at N₁). -3. No shimmer on distant terrain. -4. Smooth tree edges (foliage A2C). -5. No new z-fighting / depth artifacts. - ---- - -## 9. Out of scope (explicitly deferred) - -Per the brainstorm Q10 confirmation: - -- **GPU-side culling** (compute pre-pass) β€” N.6. -- **Persistent-mapped indirect buffer** β€” N.6. -- **Multi-thread mesh-build worker pool** β€” N.6 if first-traversal fill - feels too slow at gate. -- **Static/dynamic persistent groups** (Q5 Option B β€” the "compute the - group key once at spawn" architecture change) β€” separate later phase - (likely A.6 or N.6.5). -- **Billboard / impostor scenery** at far tier β€” escalation only if the - fog'd terrain horizon looks too bare at gate. -- **Wider N₁ hysteresis** (Option C, radius+3) β€” single-line tweak only - if gate finds entity pop-in along the boundary. -- **Far-tier terrain mesh LOD** (decimating 2Γ—2 LBs) β€” not needed at - Nβ‚‚=12; revisit only if Nβ‚‚ grows beyond 15. -- **Sky / particles modern path migration** β€” N.7+ phases. -- **EnvCell modern path migration** β€” separate phase. -- **Shadow mapping** β€” separate visual phase, later. -- **Strict 240 Hz during walking** (Q9 Option A) β€” graduate to in a - perf-polish phase if we want to commit to it. - ---- - -## 10. Risks - -1. **Fog tuning visual gate** *(highest risk).* Hardest non-engineering - risk. The 0.7 / 0.95 multipliers in Β§4.8 are first-cut numbers. If - the fog band is too thin (visible scenery cliff at N₁) or too thick - (terrain looks washed out), iterate on the multipliers. Mitigation: - expose `FogStart` / `FogEnd` as tunable env vars during A.5 - development for fast iteration. -2. **A2C / MSAA framebuffer interaction** *(moderate risk).* MSAA on - the GL render target may break sky / particles / UI rendering. - Audit during implementation. **Fallback: ship Q8 Option B (mipmaps - + depth-audit only) if A2C goes sideways.** Document the result. -3. **Worker starvation on first-traversal** *(low-moderate risk).* - ~2.7 s of sequential mesh build on first walk into virgin region. - Render thread frame time stays in budget; the visible effect is the - horizon visibly filling. Acceptable per Q9 Option B; graduate to - multi-worker pool in N.6 if user complains. -4. **Tier-boundary churn** *(low risk).* When player crosses N₁ both - directions, demoteβ†’promoteβ†’demote fires. Hysteresis (radius+2) is - the buffer. If thrash visible, widen to radius+3. -5. **Entity AABB cache invalidation** *(low risk).* Dynamic entities - must recompute AABB on position change. Single-threaded render - thread means no concurrent mutation; the dirty-flag pattern is - straightforward. -6. **Server broadcast radius mismatch** *(low risk).* If ACE's broadcast - radius is < N₁=4, NPCs in outer near-tier LBs won't be - server-broadcast (they don't exist in our state). Mitigation: - N₁=4 is conservative β€” typical ACE configs broadcast at 5-7 LBs. - If observed, drop N₁ to 3. - ---- - -## 11. What was deferred (post-A.5) - -The following items were identified during A.5 development but deferred to -post-A.5 phases. They are tracked as OPEN issues in `docs/ISSUES.md`. - -1. **Tier 1 entity-classification cache** (commit `3639a6f` reverted at - `9b49009`): First attempt cached `meshRef.PartTransform` which is mutated - per frame for animated entities (skeletal pose). Next attempt needs: - (a) audit AnimationSequencer + AnimationHookRouter to identify ALL - per-frame mutations of MeshRef state; (b) redesign cache to bypass - animated entities OR cache only the animation-invariant subset; (c) test - specifically with a moving animated NPC on screen. (`docs/ISSUES.md` #53) - -2. **Lifestone missing visual**: The Holtburg lifestone has not rendered since - earlier in A.5 development. Possibly Bug A's far-tier strip incorrectly - catching a near-tier entity, or a separate earlier regression. - (`docs/ISSUES.md` #52) - -3. **Plumb JobKind through BuildLandblockForStreaming**: Bug A's fix (commit - `9217fd9`) strips entities post-load in the worker. Proper fix: skip the - `LandBlockInfo` + scenery load entirely for far-tier jobs. ~30 min. - (`docs/ISSUES.md` #54) - -4. **Tier 2 β€” Static/dynamic split with persistent groups**: ~2-week phase. - Avoids per-frame entity re-classification by maintaining stable groups - keyed at spawn time. Roadmap doc at - `docs/plans/2026-05-10-perf-tiers-2-3-roadmap.md`. - -5. **Tier 3 β€” GPU-side culling via compute pre-pass**: ~1-month phase. - Same roadmap doc. - -6. **Eliminate ToEntries adapter allocation**: tiny win (~25 KB/frame). - -7. **InvalidateEntity wiring on palette/ObjDesc events**: needed by the - Tier 1 retry. - -8. **Visual gate at full High preset**: never validated due to the - GPU+CPU stack-up OS crash earlier in A.5. With Bug A fixed the crash - likely won't recur; defer retest to post-A.5 perf polish. - ---- - -## 12. References (formerly Β§11) - -- **Handoff (cold-start):** [`docs/research/2026-05-10-phase-a5-handoff.md`](../../research/2026-05-10-phase-a5-handoff.md) -- **N.5b handoff (predecessor):** [`docs/research/2026-05-09-phase-n5b-handoff.md`](../../research/2026-05-09-phase-n5b-handoff.md) -- **N.5b perf baseline:** [`docs/plans/2026-05-09-phase-n5b-perf-baseline.md`](../../plans/2026-05-09-phase-n5b-perf-baseline.md) -- **Roadmap A.5 entry:** [`docs/plans/2026-04-11-roadmap.md`](../../plans/2026-04-11-roadmap.md) -- **N.5b memory state:** `memory/project_phase_n5b_state.md` (three high-value - gotchas β€” bindless uniform-sampler driver quirk, MaybeFlushTerrainDiag - underflow, visual gate confirmation requirement). -- **Existing streaming files:** - - [`src/AcDream.App/Streaming/StreamingController.cs`](../../../src/AcDream.App/Streaming/StreamingController.cs) - - [`src/AcDream.App/Streaming/StreamingRegion.cs`](../../../src/AcDream.App/Streaming/StreamingRegion.cs) - - [`src/AcDream.App/Streaming/GpuWorldState.cs`](../../../src/AcDream.App/Streaming/GpuWorldState.cs) - - [`src/AcDream.App/Streaming/LandblockStreamer.cs`](../../../src/AcDream.App/Streaming/LandblockStreamer.cs) -- **Existing dispatcher:** [`src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs`](../../../src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs) -- **Existing terrain renderer:** [`src/AcDream.App/Rendering/TerrainModernRenderer.cs`](../../../src/AcDream.App/Rendering/TerrainModernRenderer.cs) -- **Mesh builder (will move off render thread):** [`src/AcDream.Core/Terrain/LandblockMesh.cs`](../../../src/AcDream.Core/Terrain/LandblockMesh.cs) diff --git a/docs/superpowers/specs/2026-05-09-phase-n5b-terrain-modern-design.md b/docs/superpowers/specs/2026-05-09-phase-n5b-terrain-modern-design.md deleted file mode 100644 index fa6dc88..0000000 --- a/docs/superpowers/specs/2026-05-09-phase-n5b-terrain-modern-design.md +++ /dev/null @@ -1,438 +0,0 @@ -# Phase N.5b β€” Terrain on the Modern Rendering Path β€” Design Spec - -**Status:** Brainstormed 2026-05-09; not yet implemented. -**Author:** acdream lead engineer + Claude. -**Builds on:** Phase N.5 (`WbDrawDispatcher` on bindless + multi-draw indirect, shipped 2026-05-08). - -**Predecessor docs (read first if you're new to this phase):** -- [`docs/research/2026-05-09-phase-n5b-handoff.md`](../../research/2026-05-09-phase-n5b-handoff.md) β€” cold-start briefing. -- [`docs/superpowers/plans/2026-05-08-phase-n5-modern-rendering.md`](../plans/2026-05-08-phase-n5-modern-rendering.md) β€” N.5 plan + ship record. -- [`docs/superpowers/specs/2026-05-08-phase-n5-modern-rendering-design.md`](2026-05-08-phase-n5-modern-rendering-design.md) β€” N.5 spec; the substrate N.5b consumes. -- [`docs/ISSUES.md`](../../ISSUES.md) issue #51 β€” the load-bearing constraint this phase resolves. - ---- - -## 1. Problem statement - -N.5 lifted **entity** rendering onto bindless textures + `glMultiDrawElementsIndirect`. CPU dispatcher is 1.23 ms/frame median at Holtburg courtyard; ~810 fps sustained; ~12-15 GL calls/frame for entities regardless of scene complexity. Terrain is still on the older per-landblock pipeline (`TerrainChunkRenderer` at [src/AcDream.App/Rendering/TerrainChunkRenderer.cs](../../../src/AcDream.App/Rendering/TerrainChunkRenderer.cs)) β€” bind a per-chunk VAO + IBO, issue `glDrawElements` per visible chunk. At radius=2 that's ~25 GL calls/frame for terrain; at radius=5 it scales to ~121. - -**N.5b's goal:** lift terrain rendering onto the same modern primitives N.5 just delivered, preserving the visible terrain pixel-for-pixel and preserving physics-vs-visual Z agreement (issue #51 / the cell-boundary wobble bug class). - -The work is straightforward in shape β€” N.5's substrate (bindless wrapper, `DrawElementsIndirectCommand` struct, `[WB-DIAG]` instrumentation, two-phase Dispose pattern) is already built. The non-trivial decision is how to handle the formula divergence between WorldBuilder and retail. - ---- - -## 2. The formula divergence (why Path A is dead) - -WorldBuilder's `TerrainUtils.CalculateSplitDirection` ([references/WorldBuilder/.../TerrainUtils.cs:44-53](../../../references/WorldBuilder/WorldBuilder.Shared/Modules/Landscape/Lib/TerrainUtils.cs:44)) and acdream's `TerrainBlending.CalculateSplitDirection` ([src/AcDream.Core/Terrain/TerrainBlending.cs:56](../../../src/AcDream.Core/Terrain/TerrainBlending.cs:56)) use mathematically distinct formulas: - -| | Formula | Source | -|---|---|---| -| acdream | `dw = x*y*0x0CCAC033 - x*0x421BE3BD + y*0x6C1AC587 - 0x519B8F25; bit31` | AC2D `Landblocks.cpp:346-350` | -| WB | `(seedA + 1813693831) - seedB - 1369149221 >= 0.5` (rescaled) where `seedA = (lbX*8+cellX)*214614067; seedB = (lbY*8+cellY)*1109124029` | clean-room reverse engineering | - -**Verified retail authority:** the named retail decomp at [`docs/research/named-retail/acclient_2013_pseudo_c.txt`](../../research/named-retail/acclient_2013_pseudo_c.txt) lines 316042-316144 (function `CLandBlockStruct::ConstructPolygons` at retail address `00531d10`) contains the constants `0x0CCAC033 / 0x6C1AC587 / 0x421BE3BD / 0x519B8F25` verbatim. **Retail uses AC2D's formula.** acdream matches retail. **WB does not.** - -**Quantified divergence** (per `tests/AcDream.Core.Tests/Terrain/SplitFormulaDivergenceTest.cs`, sweep across 255Γ—255 landblocks Γ— 64 cells = 4,161,600 cells): - -| Comparison | Disagreement rate | -|---|---| -| Raw enum output (WB enum vs acdream enum) | **50.02%** | -| Diagonal-actually-painted (post-correcting for WB's inverted enum semantics) | **49.98%** | -| Holtburg town (0xA9B0) | 29/64 cells (45.3%) wrong if using WB | -| Worst landblock (0x4D96) | 47/64 cells (73.4%) wrong if using WB | -| Best landblock (0x0478) | 17/64 cells (26.6%) wrong if using WB | - -The two formulas behave like independent random hashes. Adopting WB's pipeline wholesale (Path A) would visibly mis-render ~half the diagonals on every landblock β€” the cell-boundary wobble bug class would be present everywhere. - -**Path A is dead.** N.5b commits to Path C (see Decision 1 below): use WB's *renderer* pattern (single global VBO/EBO + slot allocator + multi-draw indirect), driven by acdream's existing `LandblockMesh.Build` which uses retail's formula. - ---- - -## 3. Decisions log - -The eight brainstorm outcomes, locked. - -| # | Decision | Choice | Reason | -|---|---|---|---| -| 1 | Formula source for cell split direction | **Path C β€” WB renderer pattern, acdream's `LandblockMesh.Build` + `TerrainBlending.CalculateSplitDirection`** (retail's formula) | Path A measured 49.98% diagonal-painted divergence vs retail. Path B (fork-patch WB) is permanent maintenance burden. Path C keeps a known-working asset and avoids fork friction. Same per-frame perf as either alternative. | -| 2 | Atlas model | **Keep `TerrainAtlas` (palCode-based fragment blending) + add bindless handles** | Visual correctness already locked in. Bindless wrapper is ~50 lines, cookie-cutter from N.5's `TextureCache.MakeResidentHandle` pattern. No perf win from adopting WB's `LandSurfaceManager`. | -| 3 | Mesh ownership | **Single global VBO/EBO + slot allocator, one slot per landblock** | Required for `glMultiDrawElementsIndirect` to actually win β€” per-LB IBOs would force per-LB binds, defeating the point. Mirrors N.5's pattern + WB's pattern. | -| 4 | Index format | **uint32 + baseVertex baked into indices on upload** | Matches WB's pattern verbatim ("maximum driver compatibility"). 192 KB extra IBO at 256 slots β€” rounding error vs vertex bytes. Future-proofs A.5's higher radius. | -| 5 | Shader unification | **Separate `terrain_modern.vert/.frag`** | Vertex layouts are meaningfully different (terrain: 6 attribs incl. palCode; entities: position+UV+normal+per-instance matrix). Unifying forces dead code on both sides; no perf win. | -| 6 | Streaming integration | **Mirror WB's slot allocator (free-list `Queue` + power-of-two grow). Skip WB's 15s unload delay.** | Free-list standard; grow-by-doubling matches N.5 buffer growth pattern. The 15s delay would compete with `StreamingLoader`'s existing hysteresis β€” let one component own lifecycle policy. | -| 7 | Conformance test | **Pure-CPU sweep: visual mesh Z = `TerrainSurface.SampleZFromHeightmap` within 1mm, 10 representative landblocks Γ— 100 sample points** | The exact issue #51 sentinel. ~1,000 assertions/run, <100ms, no GL infrastructure needed. Catches any silent formula or vertex-layout drift. | -| 8 | Visual verification gate | **4 outdoor scenes (Holtburg flat + sloped, Foundry-area, sloped LB) Γ— 6 visual checks** | Outdoor-only β€” interiors / dungeons / EnvCells are out of scope and not testable yet. The wobble check is the load-bearing #51 sentinel. | - ---- - -## 4. Architecture overview - -### Per-frame draw flow - -``` -TerrainModernRenderer.Draw(camera, frustum, neverCullId): - 1. Walk all loaded slots β†’ per-slot frustum cull (AABB test). - Build _visibleSlots list (in-place reuse, no per-frame alloc). - - 2. If _visibleSlots.Count == 0: early-out. - - 3. Build per-frame DEIC array, one entry per visible slot: - DrawElementsIndirectCommand { - Count = 384, // verts/landblock - InstanceCount= 1, - FirstIndex = slot.FirstIndex, // baked offset into global IBO - BaseVertex = 0, // already baked into indices - BaseInstance = 0 - } - - 4. If _drawIndirectCapacity < _visibleSlots.Count: - delete + re-allocate _indirectBuffer (power-of-two grow). - glBufferSubData(DRAW_INDIRECT_BUFFER, 0, sizeof(DEIC) * _visibleSlots.Count, deicArray) - - 5. shader.Use() // terrain_modern - 6. Bind global VAO (_globalVao) - 7. Set bindless handle uniforms: glProgramUniformHandleARB for uTerrain + uAlpha - 8. Bind DRAW_INDIRECT_BUFFER (_indirectBuffer) - 9. glMemoryBarrier(GL_COMMAND_BARRIER_BIT) - 10. glMultiDrawElementsIndirect(Triangles, UnsignedInt, indirect=0, - drawcount=_visibleSlots.Count, stride=sizeof(DEIC)) - 11. Unbind VAO. - -GL calls per frame for terrain: ~6-8 fixed. - - 1Γ— shader.Use - - 1Γ— BindVertexArray - - 2Γ— ProgramUniformHandleARB (atlas handles) - - 1Γ— BindBuffer for DRAW_INDIRECT_BUFFER - - 1Γ— BufferSubData for DEIC array - - 1Γ— MemoryBarrier - - 1Γ— MultiDrawElementsIndirect - - 1Γ— BindVertexArray(0) -``` - -### Per-landblock-load flow (streaming integration) - -``` -TerrainModernRenderer.AddLandblock(id, meshData, worldOrigin): - 1. If id already present: RemoveLandblock(id) first (replaces). - 2. Bake worldOrigin into vertex positions (CPU; ~12Β΅s per landblock). - 3. Acquire slot: - if _freeSlots.TryDequeue: reuse - else: slot = _nextFreeSlot++; if needed, EnsureCapacity(_nextFreeSlot). - 4. Compute slot offsets: - slotByteOffset_VBO = slot * 384 * 40 bytes (15,360 bytes per slot) - slotByteOffset_IBO = slot * 384 * 4 bytes (1,536 bytes per slot) - firstIndex = slot * 384 - baseVertex = slot * 384 - 5. Bake baseVertex into indices on CPU (indices[i] += baseVertex). - 6. glBufferSubData(VBO, slotByteOffset_VBO, vertBytes, vertData). - 7. glBufferSubData(IBO, slotByteOffset_IBO, idxBytes, bakedIndices). - 8. Compute slot AABB (worldOrigin.x, worldOrigin.y, minZ, +192, +192, maxZ). - 9. Store SlotData {id, worldOrigin, firstIndex, indexCount, aabbMin, aabbMax}. - 10. _idToSlot[id] = slot. - -TerrainModernRenderer.RemoveLandblock(id): - 1. _idToSlot.TryGetValue(id) β†’ slot. - 2. _freeSlots.Enqueue(slot); _idToSlot.Remove(id); _slots[slot] = null. - (No GPU clear β€” DEIC list won't reference unused slots.) - -EnsureCapacity(requiredSlots): - newCap = max(initialCapacity, currentCap * 2) - while newCap < requiredSlots: newCap *= 2. - Allocate new VBO + IBO at new size. - glCopyBufferSubData old β†’ new (preserve loaded slot data). - Delete old; recreate VAO pointing at new VBO+IBO. -``` - -### Relation to N.5's existing dispatcher - -`TerrainModernRenderer` is structurally **parallel** to `WbDrawDispatcher`, not nested under it. They share: - -- `BindlessSupport` wrapper for `ARB_bindless_texture` calls -- `DrawElementsIndirectCommand` struct (20-byte layout) -- `[WB-DIAG]` instrumentation pattern (CPU `Stopwatch` + GPU `GL_TIME_ELAPSED` queries) -- `SceneLighting` UBO at binding=1 - -But they're separate dispatchers with separate global buffers, separate VAOs, separate shaders. Per frame, `GameWindow.Draw` calls them in sequence: - -1. `_wbDrawDispatcher.Draw(...)` β€” entities (opaque + transparent passes) -2. `_terrainModern.Draw(...)` β€” terrain (single opaque pass) -3. Sky / particles / debug / UI on legacy paths until later phases retire them. - ---- - -## 5. Component changes - -### Files added - -| File | Purpose | Approx. size | -|---|---|---| -| `src/AcDream.App/Rendering/TerrainModernRenderer.cs` | The new dispatcher. Owns global VBO/EBO + slot allocator + per-frame DEIC build + `glMultiDrawElementsIndirect` dispatch. | ~400-500 lines | -| `src/AcDream.App/Rendering/TerrainSlotAllocator.cs` | Pure-CPU helper extracted for unit testing: free-list slot management + DEIC array builder. | ~150 lines | -| `src/AcDream.App/Rendering/Shaders/terrain_modern.vert` | Vertex shader. Same per-cell layout as today's `terrain.vert` (locations 0-5). Reads bindless atlas handles via uniform. Same `SceneLighting` UBO at binding=1. Same per-vertex AdjustPlanes lighting bake. | ~150 lines | -| `src/AcDream.App/Rendering/Shaders/terrain_modern.frag` | Fragment shader. Same `combineOverlays` + `combineRoad` + `maskBlend3` as today's `terrain.frag`. Samples bindless `sampler2DArray` handles via `GL_ARB_bindless_texture` extension. Same fog + lightning flash + atmosphere. | ~150 lines | -| `tests/AcDream.Core.Tests/Terrain/TerrainModernConformanceTests.cs` | The Z-conformance sentinel for issue #51's bug class. ~10 representative landblocks Γ— ~100 sample points; asserts `\|meshTriZ - TerrainSurface.SampleZFromHeightmap\| < 0.001m`. | ~150 lines | -| `tests/AcDream.Core.Tests/Rendering/TerrainSlotAllocatorTests.cs` | Unit tests for the slot allocator (free-list correctness, capacity grow, AABB tracking) + DEIC build correctness. Pure CPU; no GL. | ~200 lines | - -### Files modified - -| File | Change | -|---|---| -| `src/AcDream.App/Rendering/TerrainAtlas.cs` | Add `GetBindlessHandles()` returning `(ulong terrain, ulong alpha)`. Mirrors N.5's `TextureCache.MakeResidentHandle` pattern: generate handle once at first call, make resident, cache. The existing `GlTexture` / `GlAlphaTexture` `uint` properties stay (no legacy callers to migrate yet, but the path is preserved). | -| `src/AcDream.App/Rendering/GameWindow.cs` | Field declaration ([line 21](../../../src/AcDream.App/Rendering/GameWindow.cs:21)): `_terrain` field type `TerrainChunkRenderer? β†’ TerrainModernRenderer?`. Construction ([line 1391](../../../src/AcDream.App/Rendering/GameWindow.cs:1391)): `new TerrainChunkRenderer(gl, shader, atlas)` β†’ `new TerrainModernRenderer(gl, bindless, shader, atlas)`. Wire the `[TERRAIN-DIAG]` rollup callback (mirror the existing `[WB-DIAG]` callback wiring). | -| `docs/plans/2026-04-11-roadmap.md` | N.5b β†’ "Shipped" row on completion; N.6 entry refreshed to remove "terrain on modern path" from scope. | -| `docs/ISSUES.md` | Issue #51 β†’ "Recently closed" with the SHIP commit SHA. | -| `CLAUDE.md` "WB integration cribs" section | Add the N.5b crib: terrain dispatcher mirror of WB's pattern, retail-formula preserved via `LandblockMesh.Build` + `TerrainBlending.CalculateSplitDirection`. | -| `memory/project_phase_n5b_state.md` (new memory file) | Captures any high-value gotchas discovered during N.5b implementation (analogous to `project_phase_n5_state.md`'s three gotchas). | - -### Files deleted - -| File | Reason | -|---|---| -| `src/AcDream.App/Rendering/TerrainChunkRenderer.cs` (454 lines) | Replaced by `TerrainModernRenderer`. | -| `src/AcDream.App/Rendering/TerrainRenderer.cs` (247 lines) | Older sibling β€” already not wired in production. Has no users. Goes away in the same commit as `TerrainChunkRenderer`. | -| `src/AcDream.App/Rendering/Shaders/terrain.vert` (147 lines) | Replaced by `terrain_modern.vert`. | -| `src/AcDream.App/Rendering/Shaders/terrain.frag` (149 lines) | Replaced by `terrain_modern.frag`. | - -### Net diff - -- Adds: ~6 files, ~1,200 lines (renderer + slot-allocator + 2 shaders + 2 test files) -- Removes: ~4 files, ~1,000 lines (2 old renderers + 2 old shaders) -- Net: ~+200 lines for the same visual output, with the dispatcher collapsed to ~6-8 GL calls/frame regardless of scene size - -### Public API of `TerrainModernRenderer` - -```csharp -public sealed class TerrainModernRenderer : IDisposable -{ - public TerrainModernRenderer( - GL gl, - BindlessSupport bindless, - Shader terrainModernShader, - TerrainAtlas atlas, - int initialSlotCapacity = 64); - - public void AddLandblock(uint landblockId, LandblockMeshData mesh, Vector3 worldOrigin); - public void RemoveLandblock(uint landblockId); - public void Draw(ICamera camera, FrustumPlanes? frustum = null, uint? neverCullLandblockId = null); - - public int LoadedSlots { get; } // for [TERRAIN-DIAG] - public int VisibleSlots { get; } // for [TERRAIN-DIAG] - public int CapacitySlots { get; } // for [TERRAIN-DIAG] - - public void Dispose(); -} -``` - -Same external interface as today's `TerrainChunkRenderer` (`AddLandblock` + `RemoveLandblock` + `Draw`). Drop-in at `GameWindow.cs:1391`. - ---- - -## 6. Vertex format & shader - -### Vertex format: `TerrainVertex` stays as-is (40 bytes) - -```csharp -[StructLayout(LayoutKind.Sequential)] -public readonly record struct TerrainVertex( - Vector3 Position, // 12 bytes β€” world-space (worldOrigin baked in by AddLandblock) - Vector3 Normal, // 12 bytes β€” per-vertex from central-difference (Phase 3b) - uint Data0, // 4 bytes β€” base+ovl0 tex/alpha indices - uint Data1, // 4 bytes β€” ovl1+ovl2 tex/alpha indices - uint Data2, // 4 bytes β€” road0+road1 tex/alpha indices - uint Data3); // 4 bytes β€” rotations + splitDir bit - // total: 40 bytes -``` - -Already correct, already debugged. Per-vertex normal is preserved because retail bakes AdjustPlanes lighting at the vertex stage β€” losing it would re-introduce the "warmer / less blue than retail" regression researched in [`docs/research/2026-04-24-lambert-brightness-split.md`](../../research/2026-04-24-lambert-brightness-split.md). - -VAO attribute layout (locations 0-5, unchanged from today's `terrain.vert`): - -| Loc | Type | Source | Purpose | -|---|---|---|---| -| 0 | vec3 (3 floats) | Position offset 0 | world-space position | -| 1 | vec3 (3 floats) | Normal offset 12 | per-vertex normal | -| 2 | uvec4 (4 bytes) | Data0 offset 24 | base+ovl0 tex/alpha | -| 3 | uvec4 (4 bytes) | Data1 offset 28 | ovl1+ovl2 tex/alpha | -| 4 | uvec4 (4 bytes) | Data2 offset 32 | road0+road1 tex/alpha | -| 5 | uvec4 (4 bytes) | Data3 offset 36 | rotations + splitDir | - -### Shader: `terrain_modern.vert/.frag` - -The structural change vs today's `terrain.vert/.frag` is small. The blend math, lighting bake, fog, lightning flash all stay verbatim. The only change is how textures are bound: - -```glsl -// terrain_modern.frag β€” preamble -#version 460 core -#extension GL_ARB_bindless_texture : require - -uniform sampler2DArray uTerrain; // 64-bit bindless handle, set per-frame -uniform sampler2DArray uAlpha; // 64-bit bindless handle, set per-frame - -// SceneLighting UBO at binding=1 (unchanged from today) -layout(std140, binding = 1) uniform SceneLighting { ... }; - -// rest is unchanged from today's terrain.frag β€” combineOverlays, combineRoad, -// maskBlend3, applyFog, lightning flash are line-for-line identical -``` - -C# side per frame: - -```csharp -// once at startup or first Draw, after atlas is built: -var (terrainHandle, alphaHandle) = atlas.GetBindlessHandles(); -// MakeTextureHandleResidentARB called inside GetBindlessHandles, mirror N.5's pattern - -// per frame: -shader.Use(); -gl.ProgramUniformHandleARB(shader.Program, uTerrainLoc, terrainHandle); -gl.ProgramUniformHandleARB(shader.Program, uAlphaLoc, alphaHandle); -// ... bind global VAO + DEIC + glMultiDrawElementsIndirect -``` - -The bindless extension makes texture access syntactically identical to today's `sampler2DArray` uniform β€” the only difference is *how* the sampler is set on the C# side. GLSL doesn't know it's bindless. - -### SSBO/UBO binding map (cross-checked with N.5) - -| Binding | Type | Owner | Used by | -|---|---|---|---| -| SSBO=0 | `Instances[]` (mat4) | `WbDrawDispatcher` | `mesh_modern.vert` | -| SSBO=1 | `Batches[]` (handle+layer+flags) | `WbDrawDispatcher` | `mesh_modern.vert/.frag` | -| **SSBO=2** | (reserved) | β€” | future per-batch terrain data when A.5 wants per-LB atlas variation | -| UBO=1 | `SceneLighting` | `GameWindow` (set once/frame) | `mesh_modern.frag`, `terrain_modern.vert/.frag`, `sky.frag`, etc. | - -N.5b doesn't introduce a new SSBO. The atlas handles are uniforms, not SSBO entries β€” atlas is region-wide so per-frame upload is two `uvec2`s (16 bytes), not worth the SSBO machinery. SSBO=2 stays available for future per-batch terrain data. - -### What's preserved bit-for-bit from today's shaders - -- `unpackOverlayLayer(...)` (rotation logic for overlays) -- The `gl_VertexID % 6 β†’ corner` table for both SWtoNE and SEtoNW splits (the geometry mapping that was debugged 2026-04-21 to match ACE's `ConstructPolygons`) -- `MIN_FACTOR = 0.0` for the AdjustPlanes Lambert floor (the brightness research) -- `combineOverlays` + `combineRoad` + `maskBlend3` fragment math -- `applyFog` distance-blend -- Lightning flash additive overlay -- Per-vertex sun + ambient bake into `vLightingRGB` - ---- - -## 7. Conformance + verification - -### CPU unit tests (no GL required) - -**`tests/AcDream.Core.Tests/Rendering/TerrainSlotAllocatorTests.cs`** β€” exercises the dispatcher's pure-CPU pieces in isolation: - -| Test | Asserts | -|---|---| -| `Add_FirstLandblock_GetsSlotZero` | `_nextFreeSlot` starts at 0; first add uses slot 0 | -| `Add_SecondLandblock_GetsSlotOne` | Sequential adds use sequential slots | -| `RemoveThenAdd_ReusesFreedSlot` | Free-list FIFO: remove slot 0, add new LB β†’ slot 0 again | -| `Add_BeyondInitialCapacity_DoublesCapacity` | After 64 adds, 65th triggers grow to 128 | -| `AddSameId_ReplacesExistingSlot` | Re-adding an LB id replaces in same slot (no leak) | -| `Build_DeicArray_VisibleSlotsOnly` | DEIC array has one entry per visible slot, `firstIndex = slot * 384`, `count = 384` | -| `Build_DeicArray_EmptyVisible` | No visible β†’ empty array | -| `Aabb_StoredFromWorldOrigin` | Slot's AABB is `(origin.x, origin.y, minZ)..(origin.x+192, origin.y+192, maxZ)` | - -**`tests/AcDream.Core.Tests/Terrain/TerrainModernConformanceTests.cs`** β€” the Z-conformance sentinel for issue #51's bug class. - -Pattern modeled on the existing `ClientConformanceTests.cs`. For each landblock: - -1. Load real dat heightmap data (10 representative landblocks: Holtburg flat 0xA9B0, Holtburg sloped 0xA9B1, Foundry 0x8080, Cragstone 0xCB99, Direlands sample 0xC040, plus 5 randomly-chosen sloped landblocks from a fixed seed for variety). -2. Build mesh via `LandblockMesh.Build(...)` (the source-of-truth generator that `TerrainModernRenderer` calls internally). -3. For 100 (localX, localY) sample points uniformly distributed in `[0, 192] Γ— [0, 192]`: - - Compute `meshTriZ`: find the triangle in the built mesh containing the point, barycentric-interpolate Z from its three vertex Zs. - - Compute `physicsZ = TerrainSurface.SampleZFromHeightmap(heights, heightTable, lbX, lbY, localX, localY)`. - - Assert `|meshTriZ - physicsZ| < 0.001m` (1 mm tolerance β€” well below visible threshold). -4. Total: 10 landblocks Γ— 100 points = 1,000 assertions per run; runs in <100 ms. - -If this test fires, the pipeline has silently drifted (different formula somewhere, swapped vertex order, baseVertex baked wrong, etc.) β€” the exact bug class issue #51 names. - -### Existing tests stay green - -| Test file | Proves | N.5b impact | -|---|---|---| -| `TerrainBlendingTests.cs` | `CalculateSplitDirection` returns retail's formula | unchanged β€” still passes | -| `LandblockMeshTests.cs` | `LandblockMesh.Build` produces correct triangles | unchanged β€” still passes | -| `ClientConformanceTests.cs` | Existing conformance sweep | unchanged β€” still passes | -| `SplitFormulaDivergenceTest.cs` | WB↔retail divergence is real (49.98%) | unchanged β€” runs as data documentation; passes | -| All 71 tests in N.5 filter (Wb+MatrixComposition+TextureCacheBindless) | N.5 ship intact | unchanged β€” terrain is a separate dispatcher | - -### `[TERRAIN-DIAG]` instrumentation - -A new dedicated `[TERRAIN-DIAG]` log line, parallel to the existing `[WB-DIAG]` line, so terrain perf is observable independent of entity perf. Two parallel dispatchers, two parallel diag lines: - -``` -[TERRAIN-DIAG] cpu_ms=avg/95th draws=N/frame visible=N loaded=N capacity=N -``` - -- `cpu_ms` β€” `Stopwatch` around `TerrainModernRenderer.Draw`. Median + 95th percentile over the 5-second rollup window. -- `draws` β€” DEIC drawcount param (number of visible landblocks dispatched per `glMultiDrawElementsIndirect` call). Should be 6-8 GL calls fixed per frame regardless of `draws` value. -- `visible` / `loaded` / `capacity` β€” slot accounting; for spotting growth or leaks. -- `gpu_ms` β€” `GL_TIME_ELAPSED` query around the indirect dispatch. Same double-buffering caveat as N.5 (deferred to N.6 perf polish; will report `0/0` until then). - -### Visual verification gate (user runs the client) - -**Scenes** (drive the character through each): -1. **Holtburg town** (~0xA9B0 area) β€” flat terrain + roads -2. **Holtburg sloped landblock** (~0xA9B1) β€” slopes + cell-boundary diagonal transitions -3. **Foundry-area** (~0x80xx) β€” different blend palette -4. **Any visibly-sloped outdoor landblock** β€” Direlands or wherever you regularly test slope behavior - -**Checks** at each scene: -1. **No cell-boundary wobble** β€” the load-bearing #51 sentinel -2. **No missing chunks / black holes** β€” slot allocator or DEIC misalignment -3. **No texture seams at landblock edges** β€” pre-N.5b regression check -4. **No z-fighting** β€” pre-N.5b regression check -5. **`[TERRAIN-DIAG] draws=N` ~6-8 GL calls/frame regardless of N** -6. **`[TERRAIN-DIAG] cpu_ms` at radius=5 is β‰₯10% lower** than the pre-N.5b baseline (recorded in `docs/plans/2026-05-09-phase-n5b-perf-baseline.md`) - -Acceptance: all six checks pass in all four scenes. **Outdoor-only β€” interiors / dungeons / EnvCells are out of scope and not testable yet**. - ---- - -## 8. Acceptance criteria - -1. Build green; existing tests stay green; new conformance test passes (`|deltaZ| < 1mm` across the sweep). -2. Visual identity to today confirmed at the four user-verification scenes. -3. `[TERRAIN-DIAG]` shows terrain at ~6-8 GL calls/frame regardless of scene size (vs today's 25-121). -4. No cell-boundary wobble at any visited landblock (the #51 sentinel). -5. **CPU dispatcher time at radius=5 β‰₯10% lower** than today's `TerrainChunkRenderer` per-LB-binds path. Measured via the `[TERRAIN-DIAG] cpu_ms` median over a 5-second rollup at the Holtburg test scene with radius=5; before/after numbers captured into `docs/plans/2026-05-09-phase-n5b-perf-baseline.md` (mirror N.5's perf baseline doc convention). -6. Issue #51 closed in `docs/ISSUES.md` with the SHIP commit SHA. - ---- - -## 9. Out-of-scope (explicit boundaries) - -N.5b does **not** ship any of these. Each is a separate phase or backlog item: - -- **EnvCells / interior cells / dungeons** β€” different mesh source (cell-bound static geometry, not heightmap). Future phase, not currently scoped on the roadmap. -- **Sky rendering** (`SkyRenderer.cs`) β€” N.8 territory. -- **Particle rendering** (`ParticleRenderer.cs`) β€” N.8 territory. -- **Two-tier streaming + horizon LOD** (A.5) β€” separate brainstorm. Different streaming primitive (visible window split into "near tier" full-detail and "far tier" coarse-LOD). N.5b deliberately doesn't touch streaming radius or LOD machinery. -- **WB's `LandSurfaceManager` adoption** β€” Decision 2 explicitly keeps `TerrainAtlas`. Revisit only if a specific feature requires per-landblock alpha-mask bake. -- **WB's `TerrainGeometryGenerator` adoption** β€” Path C explicitly keeps acdream's `LandblockMesh.Build` as the source of truth. Don't call into WB's generator. -- **Fork-patching WB upstream** β€” Path C avoids this entirely. The WB submodule stays clean. -- **Persistent-mapped buffers / GPU-side culling / GL_TIME_ELAPSED double-buffering** β€” N.6 perf polish territory; not in N.5b scope. -- **Per-instance terrain "highlight" or per-LB tint** β€” no analogue need today; defer to backlog if a use case appears. -- **Removing `Texture2D` / `sampler2D` legacy texture path** β€” N.6 cleanup once Sky/Terrain/Debug/particle paths all migrate. N.5b only adds the `Texture2DArray` bindless path; legacy stays for non-terrain consumers. -- **Visual changes** β€” terrain renders pixel-for-pixel identical to today (same vertex layout, same blend math, same lighting bake). The phase is purely a dispatch-mechanism upgrade. Any visible diff means a bug, not a feature. - ---- - -## 10. Implementation guidance - -The phase is sized at ~1 week. Tasks decompose into ~10 mostly-parallel chunks: - -1. **`TerrainAtlas` bindless extension** β€” add `GetBindlessHandles()` method. ~50 lines. Independent of dispatcher. -2. **`TerrainSlotAllocator`** β€” pure-CPU helper class. ~150 lines. Independent of GL. -3. **`TerrainSlotAllocatorTests`** β€” unit tests for #2. ~200 lines. Depends on #2. -4. **`terrain_modern.vert`** β€” port of today's `terrain.vert` with bindless preamble. ~150 lines. Independent. -5. **`terrain_modern.frag`** β€” port of today's `terrain.frag` with bindless preamble. ~150 lines. Independent. -6. **`TerrainModernRenderer`** β€” dispatcher class wiring slot allocator + GL state + bindless handle uniforms + DEIC dispatch. ~400 lines. Depends on #1, #2. -7. **`TerrainModernConformanceTests`** β€” Z-conformance sentinel. ~150 lines. Depends on `LandblockMesh.Build` (existing). -8. **`GameWindow` integration** β€” swap `TerrainChunkRenderer` β†’ `TerrainModernRenderer` at field+construction; add `[TERRAIN-DIAG]` rollup. ~30 lines. Depends on #6. -9. **Delete legacy** β€” `TerrainChunkRenderer.cs`, `TerrainRenderer.cs`, `terrain.vert`, `terrain.frag`. Depends on #8 working in production. -10. **Roadmap + ISSUES.md + memory** β€” close issue #51, update CLAUDE.md "WB integration cribs", write `memory/project_phase_n5b_state.md`. Depends on #8 + visual verification. - -Tasks 1, 2, 4, 5, 7 can land in parallel. Task 6 depends on 1+2. Task 8 depends on 6. Tasks 9 and 10 are post-verification cleanup. - -The plan document (next step after this spec) breaks each task into TDD-style subtasks with clear acceptance gates per subagent dispatch. diff --git a/docs/superpowers/specs/2026-05-10-issue-53-tier1-cache-design.md b/docs/superpowers/specs/2026-05-10-issue-53-tier1-cache-design.md deleted file mode 100644 index dfe7a84..0000000 --- a/docs/superpowers/specs/2026-05-10-issue-53-tier1-cache-design.md +++ /dev/null @@ -1,451 +0,0 @@ -# ISSUE #53 β€” Tier 1 entity-classification cache (design) - -**Created:** 2026-05-10. -**Status:** approved design, ready for implementation plan. -**Audit foundation:** [docs/research/2026-05-10-tier1-mutation-audit.md](../../research/2026-05-10-tier1-mutation-audit.md). -**Originating issue:** [docs/ISSUES.md](../../ISSUES.md) Β§#53. -**Phase context:** Phase Post-A.5 polish, Priority 3 (only remaining priority after #52 + #54 closed). - ---- - -## Β§1. Problem - -`WbDrawDispatcher.Draw` runs full per-frame entity classification at radius=12: walk every visible entity β†’ resolve textures (palette + override) β†’ bucket into groups by `(IBO, FirstIndex, BaseVertex, IndexCount, textureHandle, layer, translucency)`. At ~10K visible entities Γ— ~3 batches average = ~30K classification ops/frame, this dominates the dispatcher's CPU at ~3.5 ms median (post-#52/#54 baseline) β€” 75% over the Phase A.5 spec's 2.0 ms entity dispatcher budget. - -For ~99.5% of entities (stabs, scenery, cell-mesh, interior fixtures, lifestone), the classification result is *identical* every frame from spawn to despawn. The classification work for those entities is pure waste. - -A first attempt to cache this state β€” commit `3639a6f`, reverted at `9b49009` β€” froze NPC animation by caching `meshRef.PartTransform`, which is mutated every frame for entities in `_animatedEntities`. ([memory entry on the failure mode](../../../../../../.claude/projects/C--Users-erikn-source-repos-acdream/memory/project_phase_a5_state.md)) - -This spec is the audit-driven retry. - ---- - -## Β§2. Goals and non-goals - -### Goals - -1. Drop entity dispatcher CPU median from ~3.5 ms to ≀ 2.0 ms (matches A.5 spec budget) at the horizon-safe preset (radius=4/12). -2. Hold p95 at ≀ 2.5 ms. -3. Hold animation correctness β€” NPCs animate, the lifestone crystal animates, the player animates, no frozen poses. -4. Hold N.5b conformance sentinel: 94/94 passing (`TerrainSlot|TerrainModernConformance|Wb|MatrixComposition|TextureCacheBindless|SplitFormulaDivergence`) throughout. -5. Hold full test baseline: 1688 passing, 8 pre-existing physics/input failures unchanged. -6. Surface a defensive guard against the prior bug class so the next regression of "static entity gets per-frame mutation snuck in" fails fast instead of silently freezing visuals. - -### Non-goals - -- Tier 2 (static/dynamic split with persistent groups) β€” separate multi-week phase per [docs/plans/2026-05-10-perf-tiers-2-3-roadmap.md](../../plans/2026-05-10-perf-tiers-2-3-roadmap.md). DO NOT bundle. -- Tier 3 (GPU compute culling) β€” same roadmap; depends on Tier 2 first. -- Caching for animated entities. Animated entities use today's per-frame classification path, unchanged. -- Persistent-mapped indirect buffer or any other rendering perf work outside the entity classification path. - ---- - -## Β§3. Design decisions (from brainstorming, 2026-05-10) - -| # | Decision | Rationale | -|---|---|---| -| Q1 | **Static-only cache + DEBUG cross-check** (option `c`) | The prior failure mode was "we silently cached mutable state." DEBUG cross-check converts that class of regression from "user notices a frozen NPC" to "Debug.Assert fires in any dev/test run." Zero Release cost. | -| Q2 | **Separate `EntityClassificationCache` class** (option `B`) at `src/AcDream.App/Rendering/Wb/EntityClassificationCache.cs`, injected into `WbDrawDispatcher` via ctor | Pure-CPU testable in isolation. The single invariant ("static entity = `entity.Id βˆ‰ _animatedEntities`") lives at the top of one ~200-line file rather than scattered through the 940-line dispatcher. | -| Q3 | **Cache the rest pose, not the full model matrix** (option `P`) | Full-matrix would save ~50 Β΅s/frame of mat4 mults at the cost of baking `Position`/`Rotation` into the cache. With rest pose, `Position`/`Rotation` are read live every frame; if a future regression introduces a static-entity Position write, Release builds still produce correct visuals (just with unused cache entries). DEBUG cross-check catches the regression either way. Marginal perf delta dominated by safety. | -| Q4 | **Pre-flatten Setup multi-parts at populate time** (option `F`) | The bulk of the visible CPU win lives here. Today the dispatcher walks `renderData.SetupParts` per frame even though that list is per-GfxObj-immutable. Pre-flattening makes the per-frame hot path branchless: walk one flat list per entity regardless of Setup-vs-non-Setup. Populate cost: one extra mat4 mult per subPart, run once per entity per session. | -| Q5 | **Thorough test coverage** (option `T`): ~10 tests in a new `EntityClassificationCacheTests.cs`, +2 integration tests in `WbDrawDispatcherBucketingTests.cs` | The prior bug would have been caught by the DEBUG cross-check test. The "ObjDescEvent treated as despawn-respawn" test pins a contract from the audit so it can't quietly change. Setup pre-flattening test verifies the per-batch product math without the GL stack. ~150-200 lines of test code. | - ---- - -## Β§4. The invariant - -The cache rests on this single rule, verified in the audit: - -> **An entity's `MeshRefs` reference, `Position`, `Rotation`, `PaletteOverride`, `HiddenPartsMask`, `ParentCellId`, and `Scale` are stable from spawn to despawn IF AND ONLY IF the entity is NOT in `GameWindow._animatedEntities`.** - -Six write sites in `src/`, five static (one-shot at hydration), one dynamic (per-frame in `TickAnimations`, only for entities in `_animatedEntities`). All seven `Position`/`Rotation` write sites operate on entities in `_animatedEntities`. `PaletteOverride`, `HiddenPartsMask`, `ParentCellId`, `Scale` are `init`-only on `WorldEntity`. `MeshRef` is a `readonly record struct` β€” no in-place mutation possible. See [audit Β§1, Β§3, Β§4](../../research/2026-05-10-tier1-mutation-audit.md#1-entitymeshrefs---write-sites-the-core-question). - -The DEBUG cross-check (Β§6.5) is the safety net for any future regression that violates this rule. - ---- - -## Β§5. Architecture - -``` - β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” - β”‚ GameWindow β”‚ - β”‚ └─ _animatedEntities (dict) β”‚ ← gating predicate - β”‚ └─ _classificationCache (NEW) ─┼──┐ - β”‚ └─ _wbDrawDispatcher β”‚ β”‚ - β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ - β”‚ β”‚ - β–Ό β”‚ - β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ - β”‚ WbDrawDispatcher (MODIFIED) β”‚ β”‚ - β”‚ └─ Draw(...) β”‚ β”‚ - β”‚ └─ per (entity, partIdx): β”‚ β”‚ - β”‚ β”œβ”€ animated? β†’ slow path β”‚ β”‚ - β”‚ β”œβ”€ cache hit? β†’ fast path┼─── - β”‚ └─ cache miss? β†’ slow β”‚ β”‚ - β”‚ path + populate β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”˜ - β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ - β–² - β”‚ ctor injection - β”‚ - β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” - β”‚ EntityClassificationCache (NEW)β”‚ - β”‚ └─ Dictionary β”‚ - β”‚ └─ TryGet(id, out CachedBatch[])β”‚ - β”‚ └─ Populate(id, partIdx, ...) β”‚ - β”‚ └─ InvalidateEntity(id) β”‚ - β”‚ └─ InvalidateLandblock(lbId) β”‚ - β”‚ └─ [DEBUG] CrossCheck(...) β”‚ - β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ - β–² - β”‚ invalidation calls - β”‚ - β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” - β”‚ GameWindow.RemoveLiveEntity… β”€β”€β”˜ - β”‚ GpuWorldState.RemoveEntities… β”‚ (or wired via callback) - β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ -``` - -### Β§5.1 Cache shape - -```csharp -namespace AcDream.App.Rendering.Wb; - -/// -/// Per-(entity, partIdx, batchIdx) classification result. Stored flat in -/// EntityCacheEntry.Batches β€” one entry per (logical-part, batch), where -/// for a Setup MeshRef each subPart contributes its own entries. -/// -public readonly record struct CachedBatch( - GroupKey Key, // bucket identity (matches the dispatcher's private GroupKey) - ulong BindlessTextureHandle, // resolved texture (post-palette + override) - Matrix4x4 RestPose); // meshRef.PartTransform (or subPart.PartTransform * meshRef.PartTransform for Setup) - -internal sealed class EntityCacheEntry -{ - public required uint EntityId; - public required uint LandblockHint; // for InvalidateLandblock sweep - public required CachedBatch[] Batches; // flat across (partIdx, batchIdx); ordered as classification produced them -} - -public sealed class EntityClassificationCache -{ - private readonly Dictionary _entries = new(); - - public bool TryGet(uint entityId, out EntityCacheEntry entry); - public void Populate(uint entityId, uint landblockHint, CachedBatch[] batches); - public void InvalidateEntity(uint entityId); - public void InvalidateLandblock(uint landblockId); - public int Count => _entries.Count; // diag - -#if DEBUG - public void DebugCrossCheck( - uint entityId, - Matrix4x4 entityWorld, - IReadOnlyList liveMeshRefs, - // …enough live state to recompute model matrices and assert match - ); -#endif -} -``` - -`GroupKey` is defined privately inside `WbDrawDispatcher` today (lines 923-930); promote to internal or pass an opaque payload through. Implementation detail; settle in writing-plans. - -### Β§5.2 Dispatcher integration (the per-entity branch) - -```csharp -// Inside WbDrawDispatcher.Draw, replacing today's per-(entity, partIdx) body -// at lines 367-423. - -foreach (var (entity, partIdx) in _walkScratch) -{ - if (diag) _entitiesSeen++; - - var entityWorld = - Matrix4x4.CreateFromQuaternion(entity.Rotation) * - Matrix4x4.CreateTranslation(entity.Position); - - bool isAnimated = animatedEntityIds?.Contains(entity.Id) == true; - if (!isAnimated && _cache.TryGet(entity.Id, out var entry)) - { - // Fast path: cache hit on a static entity. - foreach (var cached in entry.Batches) - { - if (!_groups.TryGetValue(cached.Key, out var grp)) - { - grp = new InstanceGroup { /* …materialize from key… */ }; - _groups[cached.Key] = grp; - } - grp.Matrices.Add(cached.RestPose * entityWorld); - } - -#if DEBUG - _cache.DebugCrossCheck(entity.Id, entityWorld, entity.MeshRefs, /*…*/); -#endif - - if (diag) _entitiesDrawn++; - continue; - } - - // Slow path: animated entity, OR cache miss. - // Run today's classification, optionally collecting into a populate buffer - // when !isAnimated. - var collector = isAnimated ? null : _populateScratch; - collector?.Clear(); - - // …today's TryGetRenderData / SetupParts walk / ClassifyBatches … - // ClassifyBatches now also writes (key, texHandle, restPose) into - // `collector` when collector is non-null. - - if (collector is not null && collector.Count > 0) - { - _cache.Populate(entity.Id, /*landblockHint*/ ResolveLandblockHint(entity), - collector.ToArray()); - } -} -``` - -`ClassifyBatches` is extended to optionally append into a caller-supplied `List`. When the collector is null (animated path), behavior is unchanged from today. When non-null (cache-miss path on static entities), each emitted batch also produces a `CachedBatch` record. - -### Β§5.3 Invalidation wiring - -Two invalidation events: - -1. **Per-entity despawn** at [GameWindow.cs:2933-2935](../../../src/AcDream.App/Rendering/GameWindow.cs#L2933) β€” add `_classificationCache.InvalidateEntity(existingEntity.Id);` next to `_animatedEntities.Remove(...)`. -2. **Landblock demote / unload** β€” `GpuWorldState.RemoveEntitiesFromLandblock` is the choke point. Wire one of: - - **(W1)** Add an `Action?` callback parameter; `GameWindow` wires it to `_classificationCache.InvalidateEntity`. Cleaner separation. - - **(W2)** Pass the cache directly into `GpuWorldState`. Less ceremony. - - **(W3)** Call `_classificationCache.InvalidateLandblock(landblockId)` from `StreamingController.Tick` before invoking `RemoveEntitiesFromLandblock`. Requires the cache to maintain `LandblockHint` correctly per entry. - - Implementation plan picks one. My lean: **(W3)** β€” the cache already needs `LandblockHint` for the sweep, and `StreamingController` is the natural lifecycle owner. - -### Β§5.4 Failure modes and recovery - -| Failure mode | Detection | Recovery | -|---|---|---| -| Future regression adds `MeshRefs` write site for static entity | DEBUG cross-check `Debug.Assert` fires in dev runs | Audit + fix source. Cross-check stays as guard. | -| Future regression adds `Position`/`Rotation` write site for static entity | DEBUG cross-check (compares `RestPose * liveEntityWorld` against live `meshRef.PartTransform * liveEntityWorld`) | Same. | -| Despawn fires but invalidation not wired | Despawn test asserts `cache.TryGet(id, …) == false` post-call | TDD test catches in CI. | -| Landblock unload misses cache invalidation | `RemoveEntitiesFromLandblock` test asserts every entry with matching `LandblockHint` is gone | TDD test catches in CI. | -| Animatedβ†’static membership flip leaves stale entry | No-op (membership predicate skips cache for animated entries; if entity later flips static, cache miss β†’ populate fresh) | None needed. | -| Staticβ†’animated membership flip leaves stale entry | No-op (predicate now skips cache; entry sits unused until despawn) | None needed. | -| Cache memory growth | At radius=12: ~10K static entities Γ— ~3-10 batches Γ— ~64 bytes = ~2-6 MB total | None needed. | -| Cache hit on a `_meshAdapter.TryGetRenderData` mesh that subsequently becomes unavailable (theoretical β€” adapter is session-stable) | N/A β€” adapter doesn't evict during play | N/A | - ---- - -## Β§6. Components and their contracts - -### Β§6.1 `EntityClassificationCache` (NEW) - -**File:** `src/AcDream.App/Rendering/Wb/EntityClassificationCache.cs` - -**Public surface:** - -```csharp -public sealed class EntityClassificationCache -{ - public bool TryGet(uint entityId, out EntityCacheEntry entry); - public void Populate(uint entityId, uint landblockHint, CachedBatch[] batches); - public void InvalidateEntity(uint entityId); - public void InvalidateLandblock(uint landblockId); - public int Count { get; } // for diag -} -``` - -**Invariants:** - -- `Populate` overwrites any existing entry for `entityId` (defensive: handles a populate that races with a partial despawn). -- `InvalidateEntity` is idempotent (no-throw on missing id). -- `InvalidateLandblock` walks all entries; entries whose `LandblockHint == landblockId` are removed. -- `TryGet` is read-only; never mutates. - -**Threading:** dispatcher runs on the render thread. All cache operations are render-thread only. No locking needed. - -### Β§6.2 `WbDrawDispatcher` (MODIFIED) - -**File:** `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` - -**Constructor change:** add `EntityClassificationCache classificationCache` parameter; assign to a private `readonly` field. - -**`Draw` change:** the per-entity body at lines ~367-423 is restructured per Β§5.2. The `WalkEntitiesInto` walk and the GL state setup phases (sort, upload, two `glMultiDrawElementsIndirect` calls) are unchanged. - -**`ClassifyBatches` change:** add optional `List? collector` parameter. When non-null, every classified `(key, texHandle, restPose)` triple is also appended to the collector. Today's behavior preserved for animated entities (collector is null). - -**`ResolveLandblockHint(entity)`:** small helper that returns the landblock id the cache should associate with the entity, for `InvalidateLandblock` sweeps. For dat-loaded entities, this is the landblock the entity was hydrated into. For live-spawned entities, it's the entity's current `Position`-implied landblock at spawn time (or `0` if landblock-invalidation isn't expected to fire β€” live entities are invalidated by `InvalidateEntity` on despawn). - -### Β§6.3 `GameWindow` (MODIFIED) - -**File:** `src/AcDream.App/Rendering/GameWindow.cs` - -**Construction:** instantiate `EntityClassificationCache`, pass to dispatcher ctor. - -**Despawn hook:** at line 2935 (inside `RemoveLiveEntityByServerGuid`), add `_classificationCache.InvalidateEntity(existingEntity.Id);` adjacent to `_animatedEntities.Remove(...)`. - -### Β§6.4 `GpuWorldState` and/or `StreamingController` (MODIFIED, exact split per W1/W2/W3) - -Implementation plan picks one of W1/W2/W3 from Β§5.3. The wiring lands invalidation calls at the LB demote / unload boundary. - -### Β§6.5 DEBUG cross-check - -```csharp -#if DEBUG -public void DebugCrossCheck( - uint entityId, - Matrix4x4 entityWorld, - IReadOnlyList liveMeshRefs, - Func tryGetRenderData, - AcSurfaceMetadataTable metaTable, - Func resolveTexture, - WorldEntity entity, - ulong palHash) -{ - if (!_entries.TryGetValue(entityId, out var entry)) return; - - // Re-classify from live state and compare against cached batches one-by-one. - int idx = 0; - foreach (var meshRef in liveMeshRefs) - { - var renderData = tryGetRenderData(meshRef.GfxObjId); - if (renderData is null) continue; - var setupParts = renderData.IsSetup ? renderData.SetupParts : OnePart(meshRef); - foreach (var (subGfxId, subTransform) in setupParts) - { - var subData = tryGetRenderData(subGfxId); - if (subData is null) continue; - var liveRestPose = renderData.IsSetup - ? subTransform * meshRef.PartTransform - : meshRef.PartTransform; - for (int b = 0; b < subData.Batches.Count; b++) - { - var batch = subData.Batches[b]; - var liveTex = resolveTexture(entity, meshRef, batch, palHash); - Debug.Assert(idx < entry.Batches.Length, - $"cache size mismatch for entity {entityId}"); - var cached = entry.Batches[idx]; - Debug.Assert(MatrixApproxEqual(cached.RestPose, liveRestPose, 1e-5f), - $"RestPose drift for entity {entityId} batch {idx}"); - Debug.Assert(cached.BindlessTextureHandle == liveTex, - $"texture drift for entity {entityId} batch {idx}"); - idx++; - } - } - } - Debug.Assert(idx == entry.Batches.Length, - $"cache batch count mismatch for entity {entityId}"); -} -#endif -``` - -The cross-check duplicates the slow-path classification against live state and compares to cached. If any drift is detected, the assert fires in dev runs with an actionable message. Zero cost in Release. - ---- - -## Β§7. Test plan - -### Β§7.1 New tests β€” `EntityClassificationCacheTests.cs` - -**File:** `tests/AcDream.Core.Tests/Rendering/Wb/EntityClassificationCacheTests.cs` - -| # | Test | What it verifies | -|---|---|---| -| 1 | `TryGet_EmptyCache_ReturnsFalse` | Baseline. | -| 2 | `Populate_ThenTryGet_ReturnsBatchesInOrder` | Round-trip. | -| 3 | `Populate_OverridesExistingEntry` | Defensive overwrite. | -| 4 | `InvalidateEntity_RemovesEntry` | Entity despawn invalidation. | -| 5 | `InvalidateEntity_OnMissingId_NoThrow` | Idempotent. | -| 6 | `InvalidateLandblock_RemovesAllMatchingEntries` | LB demote invalidation, single LB. | -| 7 | `InvalidateLandblock_LeavesNonMatchingEntries` | LB sweep is precise. | -| 8 | `InvalidateLandblock_OnMissingLb_NoThrow` | Idempotent. | -| 9 | `Count_TracksLiveEntries` | Diag accuracy. | -| 10 | `Populate_WithEmptyBatches_StoresEmptyEntry` | Edge case (entity with zero classifiable batches). | - -### Β§7.2 Extended tests β€” `WbDrawDispatcherBucketingTests.cs` - -| # | Test | What it verifies | -|---|---|---| -| 11 | `Draw_StaticEntity_RoutesThroughCache` | Spawn one static entity; first frame populates the cache; second frame's draw call doesn't invoke `ClassifyBatches` (verify via spy / counter on a mock `WbMeshAdapter`). | -| 12 | `Draw_AnimatedEntity_BypassesCache` | Spawn one entity in `animatedEntityIds`; verify cache is never populated for it; `ClassifyBatches` runs every frame. | - -### Β§7.3 (DEBUG-only) Cross-check test - -| # | Test | What it verifies | -|---|---|---| -| 13 | `DebugCrossCheck_DetectsMutatedRestPose` | Populate with synthetic data, mutate the live `MeshRef` list, invoke `DebugCrossCheck`, assert fires. Wrapped in `#if DEBUG`. | - -### Β§7.4 Setup pre-flatten lock-in - -| # | Test | What it verifies | -|---|---|---| -| 14 | `Populate_SetupMultiPart_StoresFlatBatchPerSubPart` | Synthetic Setup with N subParts Γ— M batches each β†’ cache stores N Γ— M entries with the expected `RestPose` products. | - -### Β§7.5 Lifecycle integration - -| # | Test | What it verifies | -|---|---|---| -| 15 | `DespawnRespawn_UnderReusedId_RepopulatesFresh` | Populate, invalidate, populate again under same id with different batches β†’ final state matches second populate. (Pins the audit's ObjDescEvent contract β€” ObjDescEvent is despawn+respawn, not in-place mutation. Audit Β§1 cites this.) | - -Total new tests: 15. Some can collapse if overlap is identified during implementation; baseline is "β‰₯ 10 in `EntityClassificationCacheTests` + β‰₯ 2 in dispatcher integration + β‰₯ 1 DEBUG cross-check". - -### Β§7.6 Sentinel and baseline (existing tests, must stay green) - -- N.5b conformance sentinel: filter `TerrainSlot|TerrainModernConformance|Wb|MatrixComposition|TextureCacheBindless|SplitFormulaDivergence` β†’ 94 passing. -- Full suite: 1688 passing, 8 pre-existing failures unchanged. - ---- - -## Β§8. Sequencing for implementation - -(The implementation plan from `superpowers:writing-plans` will refine this into per-task increments. Sketch:) - -1. **Skeleton + tests 1-10.** Add `CachedBatch`, `EntityCacheEntry`, `EntityClassificationCache`. Tests 1-10 in the new file. Cache exists but isn't wired to anything yet. -2. **Setup pre-flatten test (test 14) + populate path.** Synthetic `CachedBatch[]` populate; verify `Count` and `TryGet` round-trip on multi-part data shapes. -3. **Wire dispatcher: cache miss + populate.** Modify `WbDrawDispatcher.Draw` and `ClassifyBatches`. First-frame static entity populates; subsequent frames still go through slow path (cache hit branch not yet in). Build green. -4. **Wire dispatcher: cache hit + DEBUG cross-check.** Cache-hit fast path. Tests 11, 12, 13 added. -5. **Wire invalidation hooks.** `InvalidateEntity` from `RemoveLiveEntityByServerGuid`; `InvalidateLandblock` per chosen W1/W2/W3 from Β§5.3. Test 15. -6. **Visual gate.** Launch + walk Holtburg β†’ North Yanshi at horizon-safe preset. Verify NPC animates, lifestone renders, buildings at correct positions. -7. **Perf gate.** `ACDREAM_WB_DIAG=1`; capture entity dispatcher cpu_us median + p95 over a β‰₯ 30s standstill at center of Holtburg. Confirm median ≀ 2.0 ms, p95 ≀ 2.5 ms. -8. **Ship.** Commit chain. Close #53 in `docs/ISSUES.md` Recently closed. Update `CLAUDE.md` "Currently in flight" (closes the post-A.5 polish phase). Update memory if any new gotchas surfaced. - ---- - -## Β§9. Acceptance criteria (whole spec) - -- [ ] `EntityClassificationCache.cs` exists with the public surface in Β§6.1. -- [ ] `WbDrawDispatcher` accepts the cache via ctor and routes static entities through the cache; animated entities bypass. -- [ ] `RemoveLiveEntityByServerGuid` invokes `InvalidateEntity`. -- [ ] LB demote / unload path invokes `InvalidateLandblock` (or per-entity invalidation, per chosen W1/W2/W3). -- [ ] All 15 new tests pass; no existing test regresses; 8 pre-existing failures unchanged. -- [ ] N.5b sentinel: 94/94 passing on every commit. -- [ ] Build green throughout. -- [ ] Visual gate: animation works on a moving NPC, the lifestone renders, buildings are at correct positions, no new artifacts. -- [ ] Perf gate at horizon-safe preset: entity dispatcher cpu_us median ≀ 2.0 ms; p95 ≀ 2.5 ms. -- [ ] ISSUE #53 moved to "Recently closed" with the closing commit SHA. -- [ ] `CLAUDE.md` "Currently in flight" updated to reflect post-A.5 polish phase complete. -- [ ] Memory updated (`project_phase_a5_state.md` or new entry) if any new gotchas surface during implementation. - ---- - -## Β§10. What this design explicitly does NOT do - -- Touch the animated path. Animated entities use today's `ClassifyBatches` flow unchanged. -- Touch the GPU upload pipeline (`_instanceSsbo`, `_batchSsbo`, `_indirectBuffer`). Same upload shape; just less CPU work to produce the inputs. -- Touch terrain. `TerrainModernRenderer` already runs at ~21 Β΅s median; not in scope. -- Touch sky / particles / EnvCell rendering. All unchanged. -- Add new shader variants. The `mesh_modern.vert` / `mesh_modern.frag` pair is unchanged. -- Add new bindless texture handles. `TextureCache` is read-only from this work; it returns the same handle for the same surface id whether we ask once at populate or every frame. - ---- - -## Β§11. Open implementation choices for writing-plans - -These survive into the implementation plan because they're tactical (mechanical), not strategic: - -- **W1 vs W2 vs W3 for the LB invalidation wiring** (Β§5.3). Pick one; stick with it. -- **`GroupKey` visibility.** Today `private` inside the dispatcher. Either promote to `internal` (within `AcDream.App`) or pass an opaque payload through the cache. Either works. Lean: promote to `internal`. -- **`ResolveLandblockHint` placement.** On the dispatcher (uses dispatcher state for live-spawn entities) or on the cache (passed in by caller)? Lean: dispatcher computes it, passes to `Populate`. -- **`_populateScratch` reuse.** Per-frame field on the dispatcher (matches `_walkScratch` pattern) or per-call allocation? Lean: field, matching `_walkScratch`. -- **Test fixtures.** Synthetic `WorldEntity` / `MeshRef` instances may need helper builders. Lean: add a small `EntityClassificationCacheTestFixtures.cs` if the helpers grow past ~30 lines. - ---- - -**End of spec.** Implementation plan owned by `superpowers:writing-plans`. Audit foundation lives at [docs/research/2026-05-10-tier1-mutation-audit.md](../../research/2026-05-10-tier1-mutation-audit.md). diff --git a/docs/superpowers/specs/2026-05-10-phase-m-network-stack-design.md b/docs/superpowers/specs/2026-05-10-phase-m-network-stack-design.md deleted file mode 100644 index 63fb9c1..0000000 --- a/docs/superpowers/specs/2026-05-10-phase-m-network-stack-design.md +++ /dev/null @@ -1,786 +0,0 @@ -# Phase M β€” Network Stack Conformance β€” Design Spec - -**Date:** 2026-05-10 -**Status:** Draft (sections 1–3 of 8 written; sections 4–8 pending; opcode matrix in flight) -**Phase identifier:** M (per `docs/plans/2026-04-11-roadmap.md:414`) -**Supersedes the planned-but-never-written** `docs/superpowers/specs/2026-05-02-network-stack-conformance.md` - -**Related research:** -- [`docs/research/2026-05-10-holtburger-network-stack-study.md`](../../research/2026-05-10-holtburger-network-stack-study.md) β€” first-pass parity study, source of recent commit references -- [`docs/research/2026-05-10-phase-m-opcode-matrix.md`](../../research/2026-05-10-phase-m-opcode-matrix.md) β€” opcode coverage matrix (in flight; this spec links to it as the source of "done") - -**Reference repos:** -- `references/holtburger/` β€” fast-forwarded to `629695a` on 2026-05-10 -- `references/ACE/` β€” server-side opcode authority -- `docs/research/named-retail/` β€” Sept 2013 EoR PDB-named decomp - ---- - -## 1. Goal and non-goals - -### 1.1 Goal - -Build a **complete, layered, testable network protocol library** for acdream that covers every wire opcode a 2013 EoR retail client receives, sends, or both β€” independent of whether each opcode is yet wired into game state. The library is delivered behind three interfaces (`INetTransport`, `IReliableSession`, `IGameProtocol`); the existing `WorldSession` shrinks to a thin behavior consumer on top. Every parser, builder, and transport feature is unit-tested with golden-vector fixtures and survives a live ACE smoke loop before the phase ships. - -The bar is **Bar C β€” "wireable on demand."** For every in-scope opcode: -- A typed message struct exists (record with named fields, no raw byte arrays) -- A parser exists if inbound (wire bytes β†’ typed message) -- A builder exists if outbound (typed message β†’ wire bytes) -- A round-trip test exists where applicable -- A golden-vector test exists pinning at least one canonical wire encoding -- Either the opcode is dispatched to a typed event observable by `WorldSession`, or its dispatch is documented as deferred to the gameplay phase that needs it (with the deferred-target named, e.g., "wired in Phase F") - -The **behavior layer** (what to DO with each message in game state) remains the responsibility of the gameplay phase that needs it. Phase M does not wire `HouseStatusUpdate` into a house-status panel; it ensures `HouseStatusUpdate` parses correctly into a typed event so the future Phase consuming it has zero protocol work. - -### 1.2 What "complete" is measured against - -The opcode coverage matrix at `docs/research/2026-05-10-phase-m-opcode-matrix.md` is the **source of truth** for "done." Per opcode it cites: - -- Holtburger coverage (`references/holtburger/crates/holtburger-{session,protocol,core}/`) -- ACE coverage (`references/ACE/Source/ACE.Server/Network/GameMessages/Messages/` for outbound; `Source/ACE.Server/Network/Handlers/` for inbound accept rules) -- Named retail decomp (`docs/research/named-retail/acclient_2013_pseudo_c.txt`, `symbols.json` β€” by `class::method` or address) -- Acdream's current state (parser? builder? wired? deferred? unknown?) -- Phase M target (parse, build, both, or "skip with documented justification") - -An opcode is **in scope** if any of: -- Holtburger or ACE actively sends/receives it -- The named retail decomp shows the 2013 client invoking it -- It appears in observed live ACE traffic on `127.0.0.1:9000` - -An opcode is **out of scope** if all of: -- Holtburger doesn't touch it -- ACE marks it server-internal-only or post-2013 (visible in ACE's commit history or comments) -- The named retail decomp shows no client-side reference -- It hasn't been observed on live ACE - -Out-of-scope opcodes get one row in the matrix with the justification, no code work. - -### 1.3 Non-goals - -- **Not** reimplementing ACE server behavior. Validations, accept rules, and game-side decisions live in ACE; we mirror only what the client must produce or consume. -- **Not** replacing acdream's stricter inbound checksum verification. Our `PacketCodec` validates more aggressively than retail did (per the existing class doc); we keep that unless named retail proves it's wrong. -- **Not** rewriting renderer, animation, audio, UI, plugin, or chat layers. Those have their own phases. The new network stack must compile under, and run alongside, the current rendering and gameplay code. -- **Not** introducing async/await across the codebase. The current `Tick()`-driven recv-loop model is preserved; layer extraction is structural, not asynchrony-restructuring. (We MAY add a dedicated network thread if M.7's runtime work warrants it, but that decision is internal to M.7.) -- **Not** handling opcodes that are ACE-only invented for emulation purposes (e.g., debug echos that retail never had). The matrix calls these out per row. -- **Not** optimizing for throughput. Correctness first. Allocation profile and CPU cost tuning is a follow-up phase if the live loop measurably regresses. -- **Not** plugin-API exposure of network internals. The plugin API gets typed-event subscriptions where useful; raw packet introspection is dev-only. - -### 1.4 What ships at the end of Phase M - -When M.8 closes: -- `src/AcDream.Net/` (new namespace) contains `INetTransport`, `IReliableSession`, `IGameProtocol`, their concrete implementations, and the typed message library. -- `src/AcDream.Core.Net/WorldSession.cs` is a behavior consumer ~200–400 LOC, not the current 1213 LOC monolith. -- The `tests/AcDream.Net.Tests/` project covers every protocol-layer surface with unit tests. -- A `tools/network-conformance-replay/` harness can replay a captured ACE session and verify byte-perfect outputs. -- `dotnet build` green, `dotnet test` green, live ACE smoke green: login β†’ walk β†’ chat β†’ combat action β†’ portal β†’ logout, verified by user. -- The roadmap entry for Phase M moves from "PLANNED" to "shipped" with a one-line summary and commit reference. - ---- - -## 2. Coverage definition - -### 2.1 The opcode matrix - -The matrix is a markdown table at `docs/research/2026-05-10-phase-m-opcode-matrix.md`, grouped by layer: - -1. **Transport flags** β€” every value in `PacketHeaderFlags` (LoginRequest, ConnectRequest, AckSequence, EncryptedChecksum, BlobFragments, RequestRetransmit, RejectRetransmit, EchoRequest, EchoResponse, Flow, ServerSwitch, TimeSync, Disconnect, …). Each row says what the flag means, who sets it, and what acdream must do on receive. -2. **Optional-header fields** β€” every variable-length section (RequestRetransmit list, RejectRetransmit list, AckSequence, ConnectRequest payload, LoginRequest payload, CICMD, TimeSync, EchoRequest/EchoResponse times, Flow). Each row defines the byte layout and our parse/build status. -3. **GameMessage opcodes** β€” every top-level opcode the client sees (0xF658 CharacterList, 0xF745 CreateObject, 0xF74C UpdateMotion, 0xF7B0 GameEvent envelope, 0xF7DE TurbineChat, 0xEA60 AdminEnvirons, …) and every top-level opcode the client sends (0xF7C8 CharacterEnterWorldRequest, 0xF657 CharacterEnterWorld, 0xF61C MoveToState, 0xF61B JumpAction, 0xF753 AutonomousPosition, 0xF7E4 DddInterrogationResponse, …). -4. **GameEvent sub-opcodes** β€” every entry in `GameEventType.cs` (94 currently named; ~70+ currently unhandled). Each row identifies the parsing target plus the acdream wiring status. -5. **GameAction sub-opcodes** β€” every typed game-action ID (Talk, Tell, Channel, Use, UseWithTarget, MoveToObject, JumpAbsolute, CastSpell, Appraise, Identify, AttackTargetMelee/Missile, Allegiance ops, Inventory ops, Social ops, Skill/Attribute raise, Train, …). - -Each row has these columns: - -| Code | Direction | Name | Named-retail symbol or address | Holtburger | ACE | acdream today | Phase M target | Notes | - -Cell values for "Holtburger" / "ACE" / "acdream today": -- **`P`** β€” parses inbound -- **`B`** β€” builds outbound -- **`PB`** β€” both -- **`W`** β€” wired (parser/builder + dispatched to typed event consumed somewhere) -- **`–`** β€” not implemented -- **`N/A`** β€” not applicable for this side (e.g., a server-only message in ACE column) - -"Phase M target" cell values: -- **`PB+W`** β€” must parse, build (if outbound), wire to a typed event by phase end -- **`PB`** β€” must parse, build (if outbound), no wiring required -- **`P+W`** β€” inbound only, must parse and dispatch typed event -- **`–defer:`** β€” explicitly deferred to a named gameplay phase -- **`–skip:`** β€” out of scope, with justification - -### 2.2 Inbound parser obligations - -For every in-scope inbound opcode: - -- A typed C# record represents the message. Fields are named, typed, and ordered to match the wire layout (so a future reader can map field-to-byte without re-reading the parser). -- The parser is a static method on the record (`public static MyMessage Parse(ref BinaryReader r)`), throws `InvalidOperationException` on malformed input with a message containing the opcode and offset. -- A round-trip test exists if the opcode is also outbound. A golden-vector test always exists with at least one specific captured wire encoding. -- The parser dispatches to a typed event on `IGameProtocol` (`event Action OnMyMessage`). If wiring to game state is deferred, the matrix row says `–defer:` and the typed event still exists β€” gameplay-phase wiring is then a one-line subscription. - -### 2.3 Outbound builder obligations - -For every in-scope outbound opcode: - -- A typed C# record represents the message. -- A `Build(ref BinaryWriter w)` instance method writes the wire encoding. -- A golden-vector test pins at least one specific wire encoding. -- The high-level entry point lives on `IGameProtocol` (`Send(MyAction act)` or `Send(MyMessage msg)`). -- `WorldSession` exposes a behavior-friendly wrapper (`SendTalk(string text)` rather than `_protocol.Send(new TalkMessage { … })`) only for opcodes the user-facing app currently triggers. Less-used outbound builders stay on `IGameProtocol` directly until a gameplay phase needs the convenience wrapper. - -### 2.4 Three test fixture sources - -- **Golden vectors.** Hand-computed bytes for representative messages. Source: named retail decomp (extract via `tools/pdb-extract/`), holtburger captures, or by manual trace. Stored in `tests/AcDream.Net.Tests/Fixtures/Golden/.bin` plus a sibling `.json` describing the fields. -- **Live capture replay.** A captured session log (raw datagrams + timestamps) replayed offline against the new stack. Captures come from running acdream itself with a `ACDREAM_PCAP=1` env-var that dumps every datagram to disk. The first capture is recorded once Phase M.7's runtime is in place; subsequent captures replace it as features land. -- **Live ACE smoke.** Per-sub-phase, a live `dotnet run` against `127.0.0.1:9000` that exercises the relevant features. Final M.8 smoke covers login β†’ walk β†’ chat β†’ combat action β†’ teleport β†’ reconnect β†’ logout end-to-end. - -### 2.5 Acceptance for an in-scope opcode - -An opcode is "done" for Phase M when: - -1. Its matrix row is filled completely. -2. The typed message struct exists and matches the documented byte layout. -3. The parser and/or builder exist and pass round-trip tests where applicable. -4. At least one golden-vector test pins a canonical encoding. -5. The typed event is exposed on `IGameProtocol` (inbound) or the high-level send method exists (outbound). -6. The matrix row's `acdream today` column is updated to match `Phase M target`. - -The opcode-class agents working on the matrix produce the per-row data. Phase M.6 implementation work is then "for each row in the matrix where target β‰  today, write the code and tests." - ---- - -## 3. Three-layer architecture - -### 3.1 Layer overview - -``` -β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” -β”‚ WorldSession (behavior layer β€” not part of Phase M's β”‚ -β”‚ protocol library; consumes IGameProtocol) β”‚ -β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ - β”‚ subscribes to typed events, - β”‚ calls Send(IGameMessage|IGameAction) -β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” -β”‚ IGameProtocol β€” typed message routing β”‚ -β”‚ β€’ opcode dispatch table β”‚ -β”‚ β€’ GameAction sequence counter β”‚ -β”‚ β€’ per-message typed events β”‚ -β”‚ β€’ outbound: typed message β†’ bytes via builder β”‚ -β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ - β”‚ delivers fully-assembled GameMessage - β”‚ payloads; receives outbound payloads -β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” -β”‚ IReliableSession β€” wire correctness β”‚ -β”‚ β€’ PacketCodec (header + optional + body framing, CRC, β”‚ -β”‚ ISAAC c2s/s2c, fragment header layout) β”‚ -β”‚ β€’ inbound ordering buffer + RequestRetransmit issuing β”‚ -β”‚ β€’ outbound packet cache + retransmit on server request β”‚ -β”‚ β€’ ACK queue + piggyback β”‚ -β”‚ β€’ EchoRequest reply, TimeSync forwarding β”‚ -β”‚ β€’ port-switch state machine β”‚ -β”‚ β€’ fragment assembly (inbound) + splitting (outbound) β”‚ -β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ - β”‚ INetTransport.Send(bytes, endpoint) - β”‚ INetTransport.TryReceive(out bytes, out endpoint) -β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” -β”‚ INetTransport β€” UDP only β”‚ -β”‚ β€’ Send / TryReceive / Close β”‚ -β”‚ β€’ no protocol knowledge β”‚ -β”‚ β€’ UdpNetTransport (prod) / MockTransport (test) β”‚ -β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ -``` - -**Hard rules on direction:** -- Higher layers know about lower layers; lower layers do not know about higher layers. -- `IGameProtocol` does not call into `INetTransport`; it must go through `IReliableSession`. -- `WorldSession` does not directly construct UDP packets, ISAAC streams, or fragment headers. -- A unit test for any layer can mock the layer below it. - -### 3.2 `INetTransport` - -```csharp -public interface INetTransport : IDisposable -{ - /// - /// Send a single UDP datagram to the given endpoint. Synchronous. - /// Returns the number of bytes sent (always == datagram.Length on - /// success). Throws on socket error. - /// - int Send(ReadOnlySpan datagram, IPEndPoint remote); - - /// - /// Non-blocking receive. Returns false if no datagram is available. - /// On true, datagram contains the bytes (caller must not retain - /// the returned span past the next call) and remote contains the - /// source endpoint. - /// - bool TryReceive(out ReadOnlySpan datagram, out IPEndPoint remote); - - /// - /// Local endpoint we are bound to (after construction). - /// - IPEndPoint LocalEndpoint { get; } -} -``` - -**Concrete implementations:** - -- `UdpNetTransport` β€” wraps `UdpClient` + `Socket`. Sets a 2 MiB recv buffer (matches holtburger). Bound to `0.0.0.0:0` by default; constructor accepts an explicit local endpoint for tests that need port reproducibility. -- `MockTransport` β€” in-memory channel with two queues: outbound (datagrams the SUT sent) and inbound (datagrams the test wants the SUT to receive). Tests assert against outbound, inject into inbound. No threads, no async, no time. - -**Forbidden in `INetTransport`:** -- Any knowledge of `PacketHeader`, `PacketHeaderFlags`, ISAAC, fragments, GameMessages. -- Dispatching to event handlers (it returns bytes; routing is the next layer up). -- Owning a recv loop. The recv loop lives in `IReliableSession.Tick()` or its async equivalent. - -### 3.3 `IReliableSession` - -This is the largest layer. It owns the wire. - -```csharp -public interface IReliableSession : IDisposable -{ - /// Drive the recv loop once. Call from the host loop or a - /// dedicated network thread. Drains all available inbound datagrams, - /// fires events for completed GameMessages, flushes pending ACKs and - /// retransmits, and emits time-sync updates. - void Tick(); - - /// Send a GameMessage payload. The reliable session - /// allocates a sequence number, encodes the header, computes the - /// CRC (encrypted if flags require), splits into fragments if the - /// payload exceeds the single-fragment limit, and ships via - /// INetTransport. - void SendGameMessage(ReadOnlySpan payload); - - /// Send a control packet (handshake, disconnect, echo response). - /// Bypasses the GameMessage path; caller supplies the optional-header - /// content directly. - void SendControl(PacketHeaderFlags flags, ReadOnlySpan optionalContent); - - /// Begin the handshake. Drives LoginRequest β†’ - /// ConnectRequest β†’ ConnectResponse β†’ CharacterList ready, then - /// transitions to "ready for EnterWorld" state. - void BeginHandshake(string account, string password); - - /// Advance from CharacterSelection to InWorld. Sends - /// CharacterEnterWorldRequest; waits for ServerReady; sends - /// CharacterEnterWorld. - void EnterWorld(uint characterGuid, string account); - - /// Disconnect cleanly. Sends Disconnect packet with - /// client_id, then flushes and closes the transport. - void Disconnect(); - - // Events surfaced upward: - event Action> OnGameMessageReceived; // payload only - event Action OnTimeSync; // server time - event Action OnHandshakeStateChanged; - event Action OnDisconnected; - event Action OnEchoStatsUpdated; // optional, dev-mode -} -``` - -**Concrete implementation:** `ReliableSession`. Composes seven sub-components: - -1. `PacketCodec` β€” pure functions: encode, decode, CRC, fragment header pack/parse. Stateless except for the ISAAC streams it borrows. -2. `IsaacStreamPair` β€” owns `IsaacRandom c2s, s2c` plus a shared "search-and-stash" implementation for out-of-order encrypted-checksum recovery (port from holtburger `crypto.rs:73-93`). -3. `InboundOrderingBuffer` β€” `BTreeMap`-equivalent (`SortedDictionary` works in C#). Tracks `last_server_seq`, gaps, and feeds `RequestRetransmit` when gaps exceed the rate-limit threshold (1 second, max 115 seq IDs in a 256-seq window β€” match holtburger constants). -4. `OutboundPacketCache` β€” LRU dictionary (`max=512`) of recently-sent packets keyed by sequence. On server-issued `RequestRetransmit`, looks up + re-encrypts with current ISAAC + `RETRANSMISSION` flag. Uses `Iteration` field correctly. -5. `AckQueue` β€” pending-ack list. `IReliableSession.Tick` flushes via piggyback on the next outbound data packet; if no data goes out within the idle threshold, sends a standalone ACK packet. Piggybacks are automatic on every `SendGameMessage`. -6. `FragmentAssembler` β€” inbound: keyed by `(sequence, fragmentId)`, with TTL eviction (default 30s) for orphaned partials. Outbound: splits payloads >448 bytes into multiple fragments with consistent `id`/`count`/`index`/`queue` per holtburger and ACE conventions. -7. `HandshakeMachine` β€” state machine: `Idle` β†’ `LoginSent` β†’ `ConnectRequestReceived` β†’ `ConnectResponseQueued` (with 200ms deferred send, non-blocking) β†’ `PortPending` β†’ `PortConfirmed` β†’ `Ready` β†’ `EnterWorldSent` β†’ `InWorld`. Each transition is logged with timestamps for diagnostic replay. - -**Forbidden in `IReliableSession`:** -- Knowing the structure of GameMessage payloads beyond "they are bytes." -- Dispatching to typed events for specific opcodes. -- Calling into `WorldSession` or game state. - -### 3.4 `IGameProtocol` - -```csharp -public interface IGameProtocol : IDisposable -{ - /// Send a typed game action (0xF7B1 envelope, bumps the - /// per-action sequence counter). The implementation builds the - /// payload and hands it to IReliableSession.SendGameMessage. - void Send(IGameAction action); - - /// Send a non-action GameMessage (e.g., 0xF657 - /// CharacterEnterWorld, 0xF7C8 CharacterEnterWorldRequest, 0xF7E4 - /// DddInterrogationResponse, 0xF753 AutonomousPosition, - /// 0xF61C MoveToState). - void Send(IGameMessage message); - - // Inbound typed events (one per in-scope opcode): - event Action OnCharacterList; - event Action OnCreateObject; - event Action OnUpdateMotion; - event Action OnUpdatePosition; - event Action OnDddInterrogation; - event Action OnPlayerCreate; - event Action OnPlayerTeleport; - event Action OnTurbineChat; - // ...one per opcode in the matrix... - - // GameEvent sub-opcode events (one per sub-opcode): - event Action OnChannelBroadcast; - event Action OnTell; - event Action OnUpdateHealth; - // ...one per sub-opcode in the matrix... - - // Unknown / unhandled: - event Action OnUnknownMessage; // includes opcode, raw bytes, telemetry -} -``` - -The dispatch table is generated from the opcode matrix at build time (or maintained by hand from the matrix; this is a M.6 sub-decision). Every in-scope opcode has its own typed event; unknown opcodes go to `OnUnknownMessage` with full byte payload so devtools can render them. - -**Forbidden in `IGameProtocol`:** -- Direct UDP I/O. -- ISAAC, CRC, fragment work. -- Holding onto game state (Characters, current player guid, login state β€” those live in `WorldSession`). - -### 3.5 `WorldSession` (the behavior consumer β€” not protocol library) - -After Phase M, `WorldSession` is a thin layer: - -```csharp -public sealed class WorldSession : IDisposable -{ - private readonly IGameProtocol _protocol; - private readonly IReliableSession _reliable; - - // High-level state - public CharacterListEntry[] Characters { get; private set; } - public CharacterListEntry? CurrentCharacter { get; private set; } - public uint? PlayerGuid { get; private set; } - - // High-level commands (convenience wrappers around _protocol.Send) - public void Login(string account, string password) { ... } - public void EnterWorld(int characterIndex) { ... } - public void SendTalk(string text) { ... } - public void SendTell(string target, string text) { ... } - public void SendMove(MoveToState moveState) { ... } - - // Subscribes to _protocol events in the constructor; routes them - // to public events GameWindow / plugins consume. - public event Action OnCreateObject; - public event Action OnUpdateMotion; - // ...etc, mirroring _protocol.On... but at the WorldSession surface - // so callers don't reach into the protocol layer directly. -} -``` - -Target line count after migration: 200–400 LOC vs the current 1213 LOC. - -### 3.6 Layer dependencies and project structure - -New project: **`src/AcDream.Net/`**. - -- `AcDream.Net.Transport` namespace β€” `INetTransport`, `UdpNetTransport`, `MockTransport`. -- `AcDream.Net.Reliable` namespace β€” `IReliableSession`, `ReliableSession`, sub-components (`PacketCodec`, `IsaacStreamPair`, `InboundOrderingBuffer`, `OutboundPacketCache`, `AckQueue`, `FragmentAssembler`, `HandshakeMachine`), plus `PacketHeader`, `PacketHeaderFlags`, `PacketHeaderOptional`, `MessageFragment` (moved here from `AcDream.Core.Net.Packets`). -- `AcDream.Net.Protocol` namespace β€” `IGameProtocol`, `GameProtocol`, every typed message record, every typed event payload record. Subdivided by class: `Protocol/Messages/`, `Protocol/Events/`, `Protocol/Actions/`. - -The existing `src/AcDream.Core.Net/` namespace is **deleted at end of phase**. `WorldSession` moves to `src/AcDream.Core/` (it's behavior, not network plumbing). Any helpers in the old namespace migrate into `AcDream.Net.*` if still needed; otherwise they're deleted. - -Project references: -- `AcDream.Net` references `AcDream.Core` (for `IPlatformLogger`, shared types). -- `AcDream.Core` references `AcDream.Net` (for the interfaces β€” `WorldSession` needs `IGameProtocol`, `IReliableSession`). - -This implies one logical cycle that's broken by interface-only references: `AcDream.Net` only references `AcDream.Core`'s types that don't transitively depend on network code (i.e., logging + result types). If the cycle resists clean breaking, the fallback is a third project `AcDream.Net.Abstractions` for the interfaces, with `AcDream.Net.Implementation` and `AcDream.Core` both depending on it. - -### 3.7 What stays out of the architecture (and where it goes) - -- **Auth / GLS ticket flow** β€” currently absent. If Phase M needs to support GLS-ticketed login (real retail server flow, not just account/password against ACE), it lives in `AcDream.Net.Reliable.HandshakeMachine` as an additional pre-LoginRequest stage. For now, ACE only accepts account/password, so this is documented as a non-goal until a real-server phase. -- **Plugin packet introspection** β€” surface lives on `WorldSession` (or a separate dev-tool API), not in the protocol library. Exposing raw fragments to plugins is risky; we expose typed events. -- **Capture/replay tooling** β€” lives in `tools/network-conformance-replay/`, depends on `AcDream.Net` but not vice-versa. - ---- - -## 4. Migration strategy - -### 4.1 Worktree branch model - -Phase M ships entirely on a long-lived feature branch off `main`: - -- Branch name: `claude/phase-m-network-stack` -- Worktree path: `.claude/worktrees/phase-m-network-stack/` (per existing repo convention) -- All sub-phase commits land on this branch. -- `main` is untouched until M.8 acceptance gates close. -- Live-ACE testing of the new stack happens by `dotnet run` from the worktree. -- Live-ACE testing of the old stack continues to happen from `main`. - -### 4.2 Branch lifetime and rebase cadence - -- **Estimated lifetime:** 6–8 weeks (per cost estimate in Β§8). -- **Rebase cadence:** weekly minimum, plus an immediate rebase whenever any of the following lands on main: - - Touches `src/AcDream.Core.Net/`, `src/AcDream.App/Input/PlayerMovementController.cs`, or any networking-adjacent code - - Updates `references/holtburger/` (we re-pull and re-baseline our research) - - Updates `docs/research/named-retail/` (new symbols may invalidate matrix rows) - - Modifies the roadmap in any way that changes Phase M scope - -- **Conflict resolution policy:** - - Wire-format conflicts (main lands a fix to `MoveToState` while we're rewriting it): we adopt the main fix into the new stack, file an issue to verify the same behavior is reproduced post-port. - - Test conflicts (main adds a test that exercises the old `WorldSession`): the test moves to test the new stack via the same call site after migration; if the call site is gone, the test is rewritten against the new equivalent. - - Build conflicts: standard rebase resolution. - -- **Frequency check:** if rebase frequency exceeds 2Γ— per week or rebase work consistently exceeds 30 minutes, the branch is too stale. Pause feature work, catch up, then resume. - -### 4.3 What ships on the branch vs in separate commits to main - -- All Phase M code: branch only. -- All Phase M tests: branch only. -- Roadmap updates (ongoing status, not the final "shipped" entry): cherry-pick to main as the phase progresses, so other agents see status. -- Research notes (e.g., new opcode-matrix updates, new findings against ACE/holtburger): land directly on main since they're useful to other phases independent of M. -- The opcode matrix doc itself: lives on main from the start (it's reference data, not protected by the migration). - -### 4.4 Final merge: M.8 ship gate - -When M.8 closes: -1. Branch is rebased one final time against current `main`. -2. Full `dotnet build` + `dotnet test` green on the branch. -3. Live-ACE smoke run from the worktree by user: login β†’ walk β†’ chat β†’ combat β†’ portal β†’ logout. -4. Old `src/AcDream.Core.Net/` deleted in a final branch commit (NOT before β€” this is the load-bearing flip). -5. Branch merged to main as a single `--no-ff` merge commit, message names every sub-phase shipped. -6. Roadmap entry for Phase M moves to "shipped" in the same merge. -7. Memory crib written summarizing the architecture for future sessions. - -### 4.5 Rollback path - -If post-merge live ACE breaks unexpectedly, the rollback is: -- `git revert` the merge commit on main -- File a bug with the live-ACE failure mode -- Cherry-pick the fix onto a new branch off the reverted main -- Re-merge - -Since the merge is a single commit, revert is mechanical. The 6–8 weeks of work isn't lost β€” it's reachable via the original branch tip + the revert undoing the merge. - -### 4.6 Work-in-flight protocol - -During Phase M, other agents may want to work on other features. The protocol: -- Other agents work off main as usual. -- They are NOT permitted to touch `src/AcDream.Core.Net/` or any file the spec lists as Phase-M-owned. -- If they need to add a new outbound message (e.g., a new gameplay phase needs a new opcode), they file an issue tagged `phase-m-followup` and we incorporate post-merge. -- The Phase M branch is the only place network changes happen until M.8 closes. - -This is enforced by convention, not tooling. The Phase M agent (or human equivalent) communicates in commits + roadmap updates about what's locked. - ---- - -## 5. Sub-phase definitions of done - -Each sub-phase has: **entry criteria**, **exit criteria**, **conformance test gates**, and an **hour estimate**. - -### 5.1 M.1 β€” Audit & parity map - -**Entry:** Phase M kickoff. `references/holtburger/` is at known commit (`629695a` as of 2026-05-10). - -**Exit:** -- Opcode matrix at `docs/research/2026-05-10-phase-m-opcode-matrix.md` is filled to β‰₯95% completeness across all five sections (transport flags, optional headers, GameMessages, GameEvents, GameActions). -- For every row marked `–skip:`, the reason is documented and ratified by spec review. -- For every row marked `–defer:`, the deferred phase exists in the roadmap. -- A meta-section at the top of the matrix lists totals: "in-scope opcodes: N", "currently-implemented: M", "Phase M target delta: N-M". - -**Conformance gates:** -- Spot-check 10 randomly-selected rows by hand against all three sources (holtburger / ACE / named retail). Discrepancies block exit. - -**Hour estimate:** 16 hours. - -**Notes:** the holtburger study at `docs/research/2026-05-10-holtburger-network-stack-study.md` is a partial M.1 deliverable. M.1 completion includes building the formal matrix table from that study + per-opcode source citation. - -### 5.2 M.2 β€” Layer extraction (skeleton) - -**Entry:** M.1 exit gates green. - -**Exit:** -- New project `src/AcDream.Net/` exists with three namespaces (`Transport` / `Reliable` / `Protocol`). -- All three interfaces (`INetTransport`, `IReliableSession`, `IGameProtocol`) compile with their full signatures from Β§3. -- `MockTransport` and `UdpNetTransport` implement `INetTransport` with passing unit tests. -- Stub implementations of `IReliableSession` and `IGameProtocol` exist (throw `NotImplementedException` on member calls; pass interface compliance tests via the mock). -- The new project compiles. The old `src/AcDream.Core.Net/` is unchanged and still works. - -**Conformance gates:** -- `dotnet build` green. -- `dotnet test` green for any tests in `tests/AcDream.Net.Tests/` (which at this point covers only `MockTransport` and `UdpNetTransport`). - -**Hour estimate:** 40 hours. - -### 5.3 M.3 β€” Reliability core - -**Entry:** M.2 exit gates green. - -**Exit:** -- `IReliableSession`'s `ReliableSession` implementation is functionally complete: codec, ISAAC pair with search-and-stash, inbound ordering buffer, outbound packet cache, retransmit (both directions), `Iteration` field handling, RequestRetransmit issuing on gaps with rate-limit, RejectRetransmit handling. -- Sub-component unit tests pass. -- An integration test connects to a `MockTransport`, simulates an entire ACE session (login β†’ walk β†’ disconnect) with synthetic loss/reorder, verifies state. -- Holtburger study items 1.4 (port-switch race) and 1.7 (retransmit machinery) and ISAAC search-mode (item 6) are landed in this sub-phase. - -**Conformance gates:** -- 100% of unit tests pass. -- Integration test with synthetic 5% packet loss: 100% of GameMessages are eventually delivered; no false positives in retransmit requests. -- Integration test with synthetic 10% reordering: 100% of GameMessages are delivered in correct order; ISAAC search-mode keys are correctly stashed and consumed. - -**Hour estimate:** 40 hours. - -### 5.4 M.4 β€” ACK and control-packet policy - -**Entry:** M.3 exit gates green. - -**Exit:** -- ACK queue with piggyback works: every outbound `SendGameMessage` on `IReliableSession` carries the latest server seq automatically; standalone ACKs flush only when no data goes out within an idle threshold. -- EchoRequest handling: inbound EchoRequest triggers an outbound EchoResponse with mirrored time field. -- Disconnect packet carries `client_id` (study item 5). -- LoginComplete is sent on every PlayerTeleport and on first PlayerCreate (study item 1.2 β€” but the dispatch happens at the protocol layer, M.6, not here; M.4 ensures the underlying control-packet send path is correct). -- Idle ping/timeout: 1 Hz net tick, 15s timeout. - -**Conformance gates:** -- ACK piggyback test: send a series of GameMessages, verify each carries the most recent server seq. -- EchoResponse test: receive synthetic EchoRequest, verify EchoResponse goes out within 1 frame with correct time. -- Idle timeout test: don't send anything for 15s, verify keepalive fires and timeout doesn't trigger. - -**Hour estimate:** 16 hours. - -### 5.5 M.5 β€” Fragment and payload completeness - -**Entry:** M.4 exit gates green. - -**Exit:** -- Inbound fragment assembly with TTL eviction (default 30s) for orphaned partials. -- Outbound multi-fragment splitting for payloads >448 bytes. Handles correct `id` / `count` / `index` / `queue` per fragment. -- Round-trip tests for: single-fragment, 2-fragment, 5-fragment payloads. - -**Conformance gates:** -- Round-trip test with a 2KB payload: 5 fragments, all assembled correctly on receive. -- TTL test: orphan a fragment, verify it's evicted at 30s. -- Capture from holtburger or ACE of a real multi-fragment packet (e.g., long appraise text), our fragment assembler reproduces the same field values byte-perfect. - -**Hour estimate:** 24 hours. - -### 5.6 M.6 β€” Typed protocol surface - -**Entry:** M.5 exit gates green. Opcode matrix complete (M.1 exit + any deltas from M.2-M.5). - -**Exit:** -- For every opcode marked `PB+W`, `PB`, or `P+W` in the matrix: - - Typed message struct exists in `AcDream.Net.Protocol.Messages`, `Events`, or `Actions`. - - Parser/builder exists. - - Typed event exists on `IGameProtocol` for inbound opcodes. - - Round-trip test passes if applicable. - - Golden-vector test pins at least one canonical encoding. -- The dispatch table in `GameProtocol` routes inbound bytes to the correct typed event. -- Unknown opcodes route to `OnUnknownMessage` with full byte payload. - -**Conformance gates:** -- 100% of in-scope opcodes have green tests. -- A "round-trip every opcode" meta-test exists that, given a list of golden-vector samples, encodes + decodes each and asserts bit-for-bit equivalence. -- The MoveToState wire-format audit (study items 1.1.a-e) lands as part of M.6 β€” i.e., the new typed `MoveToStateMessage` builder produces wire output matching holtburger's `common.rs:122-186` encoding. - -**Hour estimate:** 80 hours. - -**Note:** This is the largest sub-phase. M.6 is parallelizable via agent dispatch β€” one agent per opcode class (transport flags, GameMessages, GameEvents, GameActions). Estimated single-developer time is 80h; with effective agent dispatch on the implementation, calendar time may compress to 3-5 days. - -### 5.7 M.7 β€” Runtime loop and diagnostics - -**Entry:** M.6 exit gates green. - -**Exit:** -- The new stack drives a recv loop that drains all available inbound, fires events, flushes pending ACKs/retransmits/ECHO replies, all within a single `Tick()`. -- Decode/order/reassembly is moved out of the render tick into either (a) the same render-tick `Tick()` call or (b) a dedicated network thread, depending on M.7's internal decision (logged in the sub-phase commit). -- Byte counters: per-direction, per-opcode, exposed via `IGameProtocol.GetTelemetry()`. -- Packet capture: `ACDREAM_PCAP=1` env-var dumps every datagram to disk in a parseable format. -- Replay tool: `tools/network-conformance-replay/` reads a capture, replays it against the new stack, asserts no decode errors and matching event sequence. -- Dev-panel diagnostics: a debug overlay shows current handshake state, ACK depth, retransmit queue depth, byte counters. - -**Conformance gates:** -- A 5-minute live ACE session captures a clean replay; replay against the new stack: zero decode errors. -- The render thread's per-frame budget for network work is < 0.5ms median (measured via existing perf instrumentation). - -**Hour estimate:** 16 hours. - -### 5.8 M.8 β€” Conformance tests and live validation - -**Entry:** M.7 exit gates green. - -**Exit:** -- All `tests/AcDream.Net.Tests/` tests green: unit, round-trip, golden-vector, integration with synthetic loss/reorder, replay-against-capture. -- Live ACE smoke: login β†’ walk to lifestone β†’ chat in /general β†’ engage NPC for combat (one attack) β†’ portal recall β†’ logout. User-confirmed visually + via decode-error counter (must be 0). -- The `WorldSession` shrinkage is complete: pre-migration ~1213 LOC, post-migration ≀400 LOC. -- The `src/AcDream.Core.Net/` namespace is deleted. -- Memory crib written: `memory/project_phase_m_network.md` summarizing layer architecture, key gotchas discovered during implementation, location of opcode matrix. -- Roadmap updated: Phase M moves from "PLANNED" to "shipped" with merge commit reference. - -**Conformance gates:** -- All M.1–M.7 exit gates remain green. -- Final live ACE smoke green. - -**Hour estimate:** 24 hours. - -### 5.9 Total - -| Sub-phase | Hours | Cumulative | -|-----------|-------|------------| -| M.1 β€” Audit & matrix | 16 | 16 | -| M.2 β€” Layer extraction | 40 | 56 | -| M.3 β€” Reliability core | 40 | 96 | -| M.4 β€” ACK + control | 16 | 112 | -| M.5 β€” Fragments | 24 | 136 | -| M.6 β€” Typed protocol | 80 | 216 | -| M.7 β€” Runtime + diagnostics | 16 | 232 | -| M.8 β€” Tests + live val | 24 | 256 | - -**Total: 256 hours β‰ˆ 32 working days β‰ˆ 6.4 weeks single-developer.** - -Realistic with subagent parallelization on M.6 (typed-message implementation) and M.1 (matrix population): 4-6 weeks calendar time. - ---- - -## 6. Conformance test plan - -### 6.1 Test surfaces per layer - -| Layer | Test surface | Backing project | -|-------|--------------|-----------------| -| Transport | Mock + Udp behavior, recv-buffer sizing, error paths | `tests/AcDream.Net.Tests/Transport/` | -| Reliable | Codec round-trip, CRC encrypted+unencrypted, ISAAC search edge cases, ordering buffer scenarios, retransmit cycles, ACK piggyback, Echo, port-switch state machine, fragment assembly + splitting | `tests/AcDream.Net.Tests/Reliable/` | -| Protocol | Per-opcode round-trip + golden-vector + unknown-opcode telemetry | `tests/AcDream.Net.Tests/Protocol/` | -| End-to-end | Replay-against-capture, live-ACE smoke | `tests/AcDream.Net.Tests/Replay/` + `tools/network-conformance-replay/` | - -### 6.2 Golden-vector library structure - -``` -tests/AcDream.Net.Tests/Fixtures/Golden/ -β”œβ”€β”€ Transport/ -β”‚ β”œβ”€β”€ login_request.bin -β”‚ β”œβ”€β”€ connect_request.bin -β”‚ β”œβ”€β”€ ack_only.bin -β”‚ β”œβ”€β”€ echo_request.bin -β”‚ └── ... -β”œβ”€β”€ Messages/ -β”‚ β”œβ”€β”€ 0xF658_character_list.bin -β”‚ β”œβ”€β”€ 0xF61C_movetostate_run_forward.bin -β”‚ β”œβ”€β”€ 0xF753_autonomous_position.bin -β”‚ └── ... -β”œβ”€β”€ Events/ -β”‚ β”œβ”€β”€ 0x0147_channel_broadcast.bin -β”‚ β”œβ”€β”€ 0x02BD_tell.bin -β”‚ └── ... -└── manifests/ - └── all-golden.json # (filename, opcode, decoded fields, source citation) -``` - -Each `.bin` has a sibling `.json` with the decoded fields and source attribution (holtburger capture / named retail trace / ACE-generated). - -### 6.3 Live capture replay - -`tools/network-conformance-replay/` is a small console app: -- Reads a `.pcap`-like capture from disk (binary format defined as part of M.7). -- For each datagram, hands bytes to a fresh `ReliableSession` + `GameProtocol`. -- Asserts: no decode errors, every typed event fires in the expected order (event order is part of the capture metadata), final session state matches the capture's recorded final state. -- Output: PASS/FAIL with detailed first-failure diff. - -### 6.4 Live ACE smoke flows - -Two tiers: - -- **Per-sub-phase smoke** (lightweight, automated where possible): - - M.3: handshake completes; CharacterList received; clean disconnect. - - M.4: 60-second idle session with ECHO traffic flowing both ways; 0 disconnects. - - M.5: a multi-fragment payload from ACE (e.g., long appraise text) parses correctly. - - M.6: every opcode the live session naturally produces (login β†’ walk β†’ chat β†’ portal) parses to its typed event. - -- **M.8 final smoke** (manual, user-driven): - - Account login: user enters credentials, picks +Acdream, enters world. - - Walk: WASD around Holtburg for 30s; observe local + retail-observer view (via parallel retail client) for blippy movement. - - Chat: /general "hello", /tell to a name, /a (allegiance), /f (fellowship). - - Combat: target a guard, swing once, observe damage notification + animation. - - Portal recall: cast Portal Recall, watch teleport. - - Logout: clean disconnect, verify ACE shows session ended. - - Decode-error counter must be 0 throughout. - -### 6.5 What's not tested at this layer - -- Game-state correctness: that's per-feature in gameplay phases. -- Rendering correctness: that's the existing renderer test surface. -- Plugin behavior: separate test surface. - ---- - -## 7. Risk register - -| # | Risk | Probability | Impact | Mitigation | -|---|------|-------------|--------|------------| -| 1 | **Branch drift** β€” main moves faster than expected, rebase work overwhelms. | Medium | High (could double phase calendar time) | Weekly rebase minimum + watchpoints on key files. Pause and catch up if conflict effort exceeds 30min/week. | -| 2 | **Opcode ambiguity** β€” three sources (holtburger / ACE / named retail) disagree on a field layout. | Medium | Medium (delays the affected M.6 row) | Per-row triage: cross-check against live ACE traffic if available; file a research note documenting disagreement; pick the source with strongest evidence; revisit if a real-server-deploy phase invalidates the choice. | -| 3 | **ISAAC stream desync** β€” search-mode port has a subtle bug that corrupts the keystream. | Low | Critical (silent corruption looks like ACE incompat) | Parallel-run old + new ISAAC for 1 week in dev mode; log every divergence; smoke-test with synthetic out-of-order injection. | -| 4 | **Live ACE incompat** β€” new stack works in unit tests but real ACE rejects something subtle. | Medium | High (blocks M.8) | Per-sub-phase live smoke (not just final). Catches incompats early. | -| 5 | **Dead-builder integration drift** β€” Phase B.4 surface (Use/UseWithTarget/PickUp) was built without wiring; we may rebuild without verifying the wiring works. | Medium | Medium (fixes one bug, introduces another) | Every typed builder must have a golden-vector test. The matrix row's "Phase M target" includes "verified against live ACE" for any opcode previously dead-built. | -| 6 | **`Iteration` field** β€” current code always writes 0; if retail uses non-zero iteration on retransmits in a way ACE validates, we get rejected. | Low | Medium (breaks retransmit specifically) | M.3's retransmit test exercises iteration values 0, 1, 2; live-ACE smoke with synthetic loss to trigger real retransmits. | -| 7 | **Project structure refactor breaks downstream code** β€” moving `WorldSession` or deleting `AcDream.Core.Net` shifts a namespace many files reference. | High | Low (compile errors are immediate) | M.8 deletion is the last commit; entire branch compiles up to that point; deletion + namespace fix lands in one commit, single rebuild. | -| 8 | **Threading model regression** β€” if M.7 introduces a network thread, render-thread races appear. | Medium | High (intermittent crashes) | Default to keeping single-threaded model; threading is opt-in via a flag for one test session before becoming default. | -| 9 | **Test fixture rot** β€” golden vectors capture a 2026-05 ACE version; future ACE versions diverge. | Low | Low (fixtures still valid for retail-conformance baseline) | Golden vectors are pinned to retail behavior, not ACE-specific. Live capture replay is from acdream itself (most reproducible). | -| 10 | **Calendar overrun** β€” 6.4 weeks expands to 12+ weeks. | Medium | Medium (delays Phase F+ gameplay phases) | Mid-phase checkpoint at M.4 close (week 3 in plan). If hours-spent β‰₯ 1.5Γ— estimate, scope-cut M.6 to "matrix-deferred opcodes only, batch the long tail to M.6.b post-merge." | - ---- - -## 8. Cost estimate - -### 8.1 Summary - -**Total estimate: 256 hours β‰ˆ 6.4 working weeks single-developer.** - -With effective subagent dispatch (especially on M.1 matrix population and M.6 typed-message implementation), realistic calendar compression to **4–6 weeks**. - -### 8.2 Cost breakdown by sub-phase (repeating for visibility) - -| Sub-phase | Hours | Calendar weeks | Subagent-friendly? | -|-----------|-------|----------------|--------------------| -| M.1 β€” Audit & matrix | 16 | 0.4 | Yes (per-class agents) | -| M.2 β€” Layer extraction | 40 | 1.0 | Limited (architecture-driven, single voice) | -| M.3 β€” Reliability core | 40 | 1.0 | Limited (ISAAC + ordering buffer interact) | -| M.4 β€” ACK + control | 16 | 0.4 | Limited | -| M.5 β€” Fragments | 24 | 0.6 | Limited | -| M.6 β€” Typed protocol | 80 | 2.0 | **Yes (per-opcode-class agents)** | -| M.7 β€” Runtime + diagnostics | 16 | 0.4 | Limited | -| M.8 β€” Tests + live val | 24 | 0.6 | Limited (live val needs human) | -| **Total** | **256** | **6.4** | | - -### 8.3 Critical path - -``` -M.1 β†’ M.2 β†’ M.3 β†’ M.4 β†’ M.5 β†’ M.6 β†’ M.7 β†’ M.8 - (mostly sequential within a single-developer flow) -``` - -M.1 can partially overlap M.2 (matrix work continues while skeleton lands). -M.3 / M.4 / M.5 are conceptually parallel within the reliable layer, but practically sequenced because they share state. -M.6 is the parallelization cliff β€” agents work on different opcode classes simultaneously. -M.7 / M.8 are sequential. - -### 8.4 Resource assumptions - -- One primary developer driving the architecture and integration. -- Subagent dispatch budget: liberal (acdream's sustained pattern is to use Sonnet agents heavily for bounded chunks; per CLAUDE.md "Subagent policy"). -- Live ACE on `127.0.0.1:9000` available throughout for smoke tests. -- User available for M.8 final visual gate (the only step that genuinely needs human eyes). - -### 8.5 What buys schedule slack - -If budget compresses (e.g., 4 weeks max), the following are scope-cuts in order: - -1. **Long-tail GameEvent sub-opcodes** (House*, Trade*, Book*, Vendor*, Barber*, Allegiance updates, ContractTracker*) β€” 30+ rows that gameplay phases will need eventually but not for M.8 acceptance. Move to a `M.6.b` follow-up. -2. **Outbound multi-fragment splitting** (M.5 second half) β€” defer until a gameplay phase needs >448-byte outbound payload. -3. **M.7 dev-panel diagnostics** β€” keep the byte counters and capture, drop the visual overlay. -4. **M.8 replay harness** β€” keep the smoke gate, drop the automated replay testing (move to follow-up). - -These cuts get total down to ~150–180 hours / 4 weeks if necessary. The architecture is preserved; the long-tail completeness regresses to "covers everything observed in live ACE during normal play, not the long tail." - ---- - -## Status & next steps - -**Spec status as of 2026-05-10:** Sections 1–8 written. Awaiting: -1. **Opcode matrix construction** (M.1's main deliverable). Dispatch agents: one per opcode class. Output: `docs/research/2026-05-10-phase-m-opcode-matrix.md`. -2. **Roadmap update.** Phase M entry shrinks to a one-paragraph summary + status table + pointer to this spec. M.0 sub-lane folds into M.3 / M.4 / M.6 (no longer ships separately). - -**When implementation starts:** create the worktree, branch off main, begin M.1 matrix completion β†’ M.2 skeleton. - diff --git a/docs/superpowers/specs/2026-05-11-phase-n6-slice1-design.md b/docs/superpowers/specs/2026-05-11-phase-n6-slice1-design.md deleted file mode 100644 index ec80af2..0000000 --- a/docs/superpowers/specs/2026-05-11-phase-n6-slice1-design.md +++ /dev/null @@ -1,335 +0,0 @@ -# Phase N.6 slice 1 β€” GPU timing fix + radius=12 perf baseline (design) - -**Created:** 2026-05-11. -**Status:** approved design, ready for implementation plan. -**Phase context:** Phase N.6 (perf polish) split into two slices on 2026-05-11 β€” this is slice 1. Slice 2 (legacy `TextureCache` cleanup + shader migration + optional persistent-mapped buffers) is deferred until after C.1.5 (PES emitter wiring), and gets its own spec then. -**Roadmap entry:** [docs/plans/2026-04-11-roadmap.md](../../plans/2026-04-11-roadmap.md) lines 690-705 (to be amended in commit 2 to reflect the slice split). - ---- - -## Β§1. Problem - -`WbDrawDispatcher` runs `glBeginQuery(GL_TIME_ELAPSED, …) … glEndQuery` around the opaque and transparent indirect draws, then immediately polls `glGetQueryObject(…, ResultAvailable, …)` **on the same frame** to read the result. The GPU has not finished executing the draw by the time the polling call runs, so `avail` is always 0, the sample is dropped, and the `_gpuSamples` ring stays all-zero forever. The user sees `gpu_us=0m/0p95` in every `[WB-DIAG]` line under `ACDREAM_WB_DIAG=1`. - -Verified at [src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs:849-859](../../../src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs#L849). - -Without this fix: -- Every future perf decision (Tier 2 vs Tier 3 vs slice 2 vs do-nothing) is made on CPU-only data. -- We cannot tell whether the dispatcher is CPU-bound or GPU-bound at radius=12. -- We cannot validate that N.5/N.5b/Tier 1 changes actually moved GPU time. - -This slice ships the GPU-timing fix and uses the now-working diagnostic to produce one authoritative perf baseline document so the next phase decision (slice 2 vs C.1.5 vs Tier 2/3) is data-driven. - ---- - -## Β§2. Goals and non-goals - -### Goals - -1. `[WB-DIAG]` reports non-zero `gpu_us` for the entity dispatcher's opaque+transparent passes at Holtburg radius=12 with `ACDREAM_WB_DIAG=1`. -2. The fix works on AMD, NVIDIA, and Intel desktop OpenGL drivers without vendor-specific code paths. -3. Produce a baseline document at `docs/plans/2026-05-11-phase-n6-perf-baseline.md` with CPU and GPU numbers across radii 4 / 8 / 12 (standstill + walking), a surface-format histogram, and a memory snapshot. -4. The baseline document closes with a recommendation paragraph: should the next phase be N.6 slice 2 (perf cleanup), C.1.5 (PES wiring), or escalation to Tier 2 (static/dynamic split). Rationale grounded in the captured numbers. -5. `dotnet build` and `dotnet test` green; no functional regression in the rendering path. - -### Non-goals - -- Persistent-mapped buffers (`BufferSubData` β†’ `GL_MAP_PERSISTENT_BIT`). Deferred to slice 2 unless the baseline shows it's a hot spot. -- Legacy `TextureCache` cleanup, `mesh.frag` orphan deletion, sky/UI text shader migration to bindless. All deferred to slice 2. -- WB atlas adoption / texture-array consolidation. Deferred to slice 2 pending the surface histogram from goal 3. -- Adding GPU queries to terrain / sky / particle / debug-line passes. Slice 1 keeps query scope to the existing two queries inside `WbDrawDispatcher` (opaque-pass + transparent-pass). -- GPU compute culling. That's Tier 3 of [docs/plans/2026-05-10-perf-tiers-2-3-roadmap.md](../../plans/2026-05-10-perf-tiers-2-3-roadmap.md), separate roadmap. - ---- - -## Β§3. Design decisions (from brainstorming, 2026-05-11) - -| # | Decision | Rationale | -|---|---|---| -| Q1 | **Ring of 3 query-pair slots** (not ring of 2) | Vendor-neutral. NVIDIA drivers with triple-buffering + vsync can queue ~3 frames ahead; AMD typically 1–2; Intel iGPUs vary. Ring of 2 plus `ResultAvailable` guard works everywhere but drops more samples on deeper queues. Ring of 3 collects samples reliably across all desktop drivers. Cost: one extra `GLuint` query pair (~12 bytes of GPU state) plus one frame of latency on the printed value, which is invisible because the diagnostic is a 256-frame moving-window median. | -| Q2 | **Read-before-issue, same-slot pattern** | On frame N, attempt to read slot `N%3` (which contains frame N-3's result β€” the *oldest* unread data, ~50 ms ago at 60 fps) *before* overwriting it with frame N's queries. Reading the oldest data maximizes the chance that `ResultAvailable=1` across all desktop drivers. Use `ResultAvailable` as a guard β€” if not ready, skip the sample. `MedianMicros` already computes over the non-zero subset, so dropped samples don't poison the result. | -| Q3 | **Keep query scope unchanged** β€” just the two existing queries (opaque-pass + transparent-pass for the WB dispatcher) | Slice 1 is "fix what's broken," not "expand instrumentation." Adding terrain / sky / particle queries is slice-2-or-later work and would inflate this slice past the half-day budget. | -| Q4 | **Surface-format histogram via env-gated one-shot dump** (`ACDREAM_DUMP_SURFACES=1`) | The atlas-adoption decision in slice 2 needs to know whether enough surfaces share dimensions/format to make consolidation worthwhile. A one-time dump on first frame to a fixed file path is cheap to implement, zero cost when off, and lets the user re-run cheaply when needed. Output goes to `%LOCALAPPDATA%\acdream\n6-surfaces.txt` (not stdout) to avoid spamming the launch log. | -| Q5 | **Two commits, not one** | Commit 1 is the GPU-timing fix (code change, regression-bisectable). Commit 2 is the surface-dump path + baseline document (docs + env-gated diag). Keeping them separate means a future bisect for a GPU-timing regression doesn't land on a doc commit. | -| Q6 | **Baseline measurement is Holtburg + High preset only** (per the user's hardware) | Slice 1 doesn't pretend to be a cross-hardware perf survey. It's one canonical measurement on the dev machine. The document template captures setup explicitly so a NVIDIA / lower-end run can be added later without re-architecting the doc. | - ---- - -## Β§4. Change 1 β€” GPU query double-buffering - -### Files touched - -- `src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs` β€” single-file change, ~30 LOC delta. - -### Current state (verified) - -```csharp -// Field declarations near line 155: -private uint _gpuQueryOpaque; -private uint _gpuQueryTransparent; -private readonly long[] _gpuSamples = new long[256]; -private bool _gpuQueriesInitialized; - -// Init at line ~347: -if (diag && !_gpuQueriesInitialized) { - _gpuQueryOpaque = _gl.GenQuery(); - _gpuQueryTransparent = _gl.GenQuery(); - _gpuQueriesInitialized = true; -} - -// Around the opaque draw at line ~774: -if (diag && _gpuQueriesInitialized) _gl.BeginQuery(QueryTarget.TimeElapsed, _gpuQueryOpaque); -… opaque indirect draw … -if (diag && _gpuQueriesInitialized) _gl.EndQuery(QueryTarget.TimeElapsed); - -// Same pattern around transparent draw at line ~823. - -// Read at line ~849 β€” BUG: same frame, never ready: -if (_gpuQueriesInitialized) { - _gl.GetQueryObject(_gpuQueryOpaque, QueryObjectParameterName.ResultAvailable, out int avail); - if (avail != 0) { - _gl.GetQueryObject(_gpuQueryOpaque, QueryObjectParameterName.Result, out ulong opaqueNs); - _gl.GetQueryObject(_gpuQueryTransparent, QueryObjectParameterName.Result, out ulong transNs); - long gpuUs = (long)((opaqueNs + transNs) / 1000UL); - _gpuSamples[_gpuSampleCursor] = gpuUs; - _gpuSampleCursor = (_gpuSampleCursor + 1) % _gpuSamples.Length; - } -} - -// Dispose at line ~1140: -if (_gpuQueriesInitialized) { - _gl.DeleteQuery(_gpuQueryOpaque); - _gl.DeleteQuery(_gpuQueryTransparent); -} -``` - -### Target state - -```csharp -private const int GpuQueryRingDepth = 3; -private readonly uint[] _gpuQueryOpaque = new uint[GpuQueryRingDepth]; -private readonly uint[] _gpuQueryTransparent = new uint[GpuQueryRingDepth]; -private int _gpuQueryFrameIndex; // increments every frame we issue queries -private bool _gpuQueriesInitialized; - -// Init: -if (diag && !_gpuQueriesInitialized) { - for (int i = 0; i < GpuQueryRingDepth; i++) { - _gpuQueryOpaque[i] = _gl.GenQuery(); - _gpuQueryTransparent[i] = _gl.GenQuery(); - } - _gpuQueriesInitialized = true; -} - -// Compute the slot index for this frame. We read this slot's previous -// contents (frame N-3's queries β€” the oldest data in the ring) and then -// overwrite it with this frame's queries. -int slot = _gpuQueryFrameIndex % GpuQueryRingDepth; - -// Read frame N-3's result BEFORE overwriting. Gated on "we've completed -// at least one full ring of writes" so we don't read uninitialized slots -// during warm-up. -if (_gpuQueriesInitialized && _gpuQueryFrameIndex >= GpuQueryRingDepth) { - _gl.GetQueryObject(_gpuQueryOpaque[slot], QueryObjectParameterName.ResultAvailable, out int avail); - if (avail != 0) { - _gl.GetQueryObject(_gpuQueryOpaque[slot], QueryObjectParameterName.Result, out ulong opaqueNs); - _gl.GetQueryObject(_gpuQueryTransparent[slot], QueryObjectParameterName.Result, out ulong transNs); - long gpuUs = (long)((opaqueNs + transNs) / 1000UL); - _gpuSamples[_gpuSampleCursor] = gpuUs; - _gpuSampleCursor = (_gpuSampleCursor + 1) % _gpuSamples.Length; - } - // If avail==0 the sample is dropped silently. MedianMicros already - // computes over the non-zero subset, so dropped samples don't poison - // the median. -} - -// Issue this frame's queries into the same slot β€” overwriting the data -// we just (attempted to) read. -if (diag && _gpuQueriesInitialized) _gl.BeginQuery(QueryTarget.TimeElapsed, _gpuQueryOpaque[slot]); -… opaque indirect draw … -if (diag && _gpuQueriesInitialized) _gl.EndQuery(QueryTarget.TimeElapsed); - -… same for transparent with _gpuQueryTransparent[slot] … - -_gpuQueryFrameIndex++; - -// Dispose: loop over the ring. -``` - -### Behavior - -- Frames 0, 1, 2 issue queries but no reads happen (the `>= RingDepth` gate skips them). -- Frame 3 reads frame 0's queries (oldest in ring) and writes new queries into slot 0. Frame 4 reads frame 1's, etc. -- Steady-state: each frame's queries are read exactly once, three frames after they were issued. Frames 0/1/2's queries are intentionally lost (startup artifact, ~50 ms of measurement). -- The diagnostic prints over a 256-frame moving window β€” at 200 fps that's ~1.3 s of history, so the first valid `gpu_us` median appears within ~2 s of moving. - -### Diag interaction - -`MaybeFlushDiag` already prints every 5 s; no change there. - -`MedianMicros` already filters non-zero samples; no change there. - -The user-visible behavior change: `gpu_us=Xm/Yp95` numbers in `[WB-DIAG]` reflect real GPU draw time for the entity dispatcher's two indirect calls. - ---- - -## Β§5. Change 2 β€” Surface-format histogram one-shot dump - -### Files touched - -- `src/AcDream.App/Rendering/TextureCache.cs` β€” add an env-gated dump method, ~40 LOC. -- One caller in `GameWindow.cs` (first-frame hook) β€” ~5 LOC. - -### Trigger - -Env var `ACDREAM_DUMP_SURFACES=1`. When set, on **frame index 600** of the session (~10 s at 60 fps, ~3 s at 200 fps β€” both well past streaming settle at radius≀12), iterate all entries in the bindless caches (`_bindlessBySurfaceId`, `_bindlessByOverridden`, `_bindlessByPalette`) and emit a histogram to `%LOCALAPPDATA%\acdream\n6-surfaces.txt`. One-shot β€” fires once per session at the exact frame, no repeats. The user can re-launch to capture a fresh snapshot. - -### Output schema - -Per entry, one line: `surfaceId(uint32 hex), width(uint16), height(uint16), format(string), byteCount(uint32)`. - -Plus rollups at the end: -- Count by `(width Γ— height)` bucket β€” answers "how many distinct dimension pairs?". -- Count by source `SurfaceFormat` (INDEX16, BGRA, DXT1, etc.). -- Total bytes (sum of `width Γ— height Γ— 4` for RGBA8 uploads). -- Top 10 most-shared `(width, height, format)` triples by count β€” this is the atlas-opportunity input. - -### Cost when off - -Negligible β€” one `Dictionary` write per `UploadRgba8`/`UploadRgba8AsLayer1Array` call (the `_uploadMetadata` insertion is unconditional so the dump path doesn't have to query GL state when it does fire). At Holtburg with 760 textures that's ~30–50 KB of process memory and one hash-table write per upload β€” invisible at runtime, no GC pressure. The expensive work (file I/O, histogram construction) is gated by the env-var check inside `TickSurfaceHistogramDumpIfEnabled` and only runs when `ACDREAM_DUMP_SURFACES=1`. - ---- - -## Β§6. Change 3 β€” Baseline document - -### File - -`docs/plans/2026-05-11-phase-n6-perf-baseline.md`. - -### Setup section - -- Hardware: Radeon RX 9070 XT (the user's machine). -- Resolution: 1440p. -- Quality preset: High (default). -- Connection: live ACE at `127.0.0.1:9000`, character `+Acdream` at Holtburg. -- Sky: clear midday, controlled via `F7` to remove weather noise. -- Build: Debug (matches the user's normal launch). -- Date measured: 2026-05-11. - -### Measurements - -Three radii: 4, 8, 12. Two motion modes per radius: standstill (camera anchored 30 s) and walking (`+Acdream` walks Nβ†’Eβ†’Sβ†’W across one landblock, 30 s). - -Per radius/mode, capture from `[WB-DIAG]` and the window title: -- CPU dispatcher: `cpu_us` median, p95. -- GPU dispatcher: `gpu_us` median, p95 (now real). -- FPS. -- Entities seen / drawn. -- Groups. -- Frame time (window title). - -### Memory snapshot - -One-time output from the `ACDREAM_DUMP_SURFACES=1` run, summarized: -- Total surfaces in cache. -- Total GPU texture bytes. -- Dimension distribution (top 10 by count). -- Format distribution. -- Atlas-opportunity score: percentage of surfaces in the top-3 dimension buckets. - -### Conclusion section - -A recommendation paragraph addressing: -1. Is the entity dispatcher CPU-bound or GPU-bound at radius=12? -2. Does `gpu_us` p95 leave headroom or is the GPU saturated? -3. Does the atlas-opportunity score justify slice-2 atlas work? -4. Given (1)–(3), what should the next phase be? Slice 2 (perf cleanup), C.1.5 (PES emitter wiring), or escalation to Tier 2 (static/dynamic split)? - -The paragraph is opinionated β€” the next phase decision should be obvious from the numbers, not require a separate debate. - ---- - -## Β§7. Test plan - -### Automated tests (none new) - -This slice is intentionally test-light: -- The GPU-timing fix has no observable behavior in tests β€” it only changes a diagnostic readout. No new unit tests. -- The surface-dump path is env-gated diag; no need to lock its output format in tests. -- Existing 1688 tests must remain green. `WbDrawDispatcher` tests (bucketing, indirect-command construction, classification cache) must not be perturbed. - -### Manual verification - -1. Launch live with `ACDREAM_WB_DIAG=1`. Walk Holtburg for ~30 s. Confirm `[WB-DIAG]` prints `gpu_us=Xm/Yp95` with X > 0 within ~5 s. -2. Launch live with `ACDREAM_DUMP_SURFACES=1 ACDREAM_WB_DIAG=1`. Wait ~10 s for streaming to settle. Open `%LOCALAPPDATA%\acdream\n6-surfaces.txt`. Confirm it contains a non-empty histogram. -3. Run the baseline measurement procedure end-to-end. Confirm the document populates with real numbers, not placeholders. - ---- - -## Β§8. Sequencing / ship gates - -### Commit 1 β€” GPU query fix - -**Message:** `feat(perf): Phase N.6 slice 1 β€” fix gpu_us double-buffering in WbDrawDispatcher` - -**Scope:** `WbDrawDispatcher.cs` changes only. Build green, tests green, manual verification step 1 from Β§7 passes. - -**Gate:** if `gpu_us` still reports 0 after ~10 s of movement, do NOT proceed to commit 2. Bump ring depth to 4 or investigate driver behavior before continuing. - -### Commit 2 β€” Baseline doc + surface dump - -**Message:** `docs(perf): Phase N.6 slice 1 β€” radius=12 baseline + surface dump path` - -**Scope:** `TextureCache.cs` dump method, `GameWindow.cs` hook, `docs/plans/2026-05-11-phase-n6-perf-baseline.md`, and the roadmap amendment at `docs/plans/2026-04-11-roadmap.md` lines 690-705 (split N.6 into slice 1 / slice 2 in the bullet list). - -**Gate:** manual verification steps 2 and 3 from Β§7 pass; baseline document's conclusion paragraph is filled in (not "TBD"); roadmap update lands in the same commit. - ---- - -## Β§9. Acceptance criteria - -1. `[WB-DIAG]` reports non-zero `gpu_us` for the entity dispatcher's opaque+transparent passes at Holtburg radius=12 with `ACDREAM_WB_DIAG=1`. -2. The fix uses only core OpenGL 3.3+ features (`GL_TIME_ELAPSED`, `glGetQueryObject`, `GL_QUERY_RESULT_AVAILABLE`). No vendor-specific extensions. -3. `docs/plans/2026-05-11-phase-n6-perf-baseline.md` exists, contains numbers (not placeholders) for the 3 radii Γ— 2 motion modes, contains the surface histogram summary, and closes with a recommendation paragraph. -4. The roadmap entry at `docs/plans/2026-04-11-roadmap.md:690-705` is amended to reflect the slice split. -5. `dotnet build` succeeds with no new warnings. -6. `dotnet test` succeeds with the existing pass/fail baseline (1688 passing, ~8 pre-existing physics/input failures unchanged). -7. No visible regression in the rendering path β€” Holtburg outdoor, day/night cycle, entity rendering, transparent surfaces all look the same as before the change. - ---- - -## Β§10. Risks - -| Risk | Likelihood | Mitigation | -|---|---|---| -| `ResultAvailable` is 0 even for frame N-3 (driver queues 4+ frames ahead) | Low β€” would be unusual on desktop GL | Sample is dropped silently; diagnostic prints zeros; user reports it. Fix: bump `GpuQueryRingDepth` to 4. No regression in the render path itself. | -| Query-pair allocation leaks across init/Dispose cycles | Low | Dispose loop deletes the full ring; existing pattern just gains an array index. | -| Surface-dump path fires before streaming settles, gets a sparse picture | Medium | Document the procedure as "wait ~10 s after entering world before reading the file." The dump path itself can also be re-runnable if needed (deferred unless slice 1 hits this in practice). | -| Conclusion paragraph in the baseline document is hard to write because the numbers don't clearly favor one direction | Medium β€” this is the slice's whole purpose | Acknowledge the ambiguity in the document and propose a "slice 1 conclusion plus a short re-brainstorm with the user" flow. The slice still ships if the numbers force a re-brainstorm; the value is in having the numbers, not in pre-deciding the answer. | -| Hidden vendor-specific behavior in `GL_TIME_ELAPSED` produces non-comparable numbers across hardware | Low β€” `GL_TIME_ELAPSED` is nanosecond-accurate per spec | Document the measurement hardware explicitly in the baseline doc setup section so future runs on different GPUs can be tagged appropriately. | - ---- - -## Β§11. Out of scope / future work - -These are explicitly NOT in slice 1, listed here so the next phase has a clean shopping list: - -- **Slice 2 β€” `TextureCache` cleanup.** Delete orphan `mesh.frag` (verify zero callers post-N.5 amendment). Delete dead entity-style legacy caches (`_handlesByOverridden`, `_handlesByPalette`) that no live renderer reads. Decide on bindless-everywhere vs legacy-island for the remaining `sampler2D` consumers (sky, UI text, particles). -- **Slice 2 β€” Particle shader migration.** Tied to C.1.5 outcome; particles migrate after C.1.5 lands more visible content to regression-test against. -- **Slice 2 β€” Persistent-mapped buffers.** Conditional on slice 1's baseline showing `BufferSubData` as a hot spot. -- **Slice 2 β€” WB atlas adoption.** Conditional on slice 1's surface histogram showing a real opportunity. -- **C.1.5 β€” PES emitter wiring.** Portals, chimneys, fireplaces. Separate phase; gets its own brainstorm/spec. -- **Tier 2 β€” static/dynamic split with persistent groups.** Separate roadmap at [docs/plans/2026-05-10-perf-tiers-2-3-roadmap.md](../../plans/2026-05-10-perf-tiers-2-3-roadmap.md). -- **Tier 3 β€” GPU compute culling.** Depends on Tier 2 first. Same roadmap. -- **Cross-vendor perf comparison.** Slice 1 is one machine. A NVIDIA companion run is a backlog item, not in scope. - ---- - -## Β§12. References - -- Existing dispatcher code: [src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs](../../../src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs). -- Existing texture cache: [src/AcDream.App/Rendering/TextureCache.cs](../../../src/AcDream.App/Rendering/TextureCache.cs). -- Prior perf baseline (style template): [docs/plans/2026-05-09-phase-n5b-perf-baseline.md](../../plans/2026-05-09-phase-n5b-perf-baseline.md). -- Roadmap N.6 entry: [docs/plans/2026-04-11-roadmap.md:690-705](../../plans/2026-04-11-roadmap.md). -- Perf tiers 2/3 alternative path: [docs/plans/2026-05-10-perf-tiers-2-3-roadmap.md](../../plans/2026-05-10-perf-tiers-2-3-roadmap.md). -- Phase C.1 plan with C.1.5 scope: [docs/plans/2026-04-27-phase-c1-pes-particles.md:285-295](../../plans/2026-04-27-phase-c1-pes-particles.md). diff --git a/docs/superpowers/specs/2026-05-12-l2g-dynamic-physicsstate-design.md b/docs/superpowers/specs/2026-05-12-l2g-dynamic-physicsstate-design.md deleted file mode 100644 index b7c1e7d..0000000 --- a/docs/superpowers/specs/2026-05-12-l2g-dynamic-physicsstate-design.md +++ /dev/null @@ -1,286 +0,0 @@ -# Phase L.2g β€” Dynamic PhysicsState Toggling - -**Status:** Design spec, created 2026-05-12 evening after L.2d slice 1+1.5 -ship and brainstorm completion. -**Branch:** `claude/gallant-mestorf-3bf2e3`. -**Predecessor:** [docs/research/2026-05-13-l2d-slice1-shipped-handoff.md](../../research/2026-05-13-l2d-slice1-shipped-handoff.md) -identified the Holtburg-doorway blocker as a closed Door entity (Setup -`0x020019FF`), not a building-collision-mesh bug. L.2g is the -sub-phase that handles the door-state work the L.2d handoff deferred. -**Roadmap owner:** new L.2 sub-lane "dynamic state" β€” the L.2 plan-of-record -([docs/plans/2026-04-29-movement-collision-conformance.md](../../plans/2026-04-29-movement-collision-conformance.md)) -explicitly anticipates the L.2g letter (L.2d revised sub-direction -paragraph, lines 195–197). -**Milestone:** M1 β€” Walkable + clickable world. Demo scenario *"open -the inn door"* depends on this slice landing. - ---- - -## TL;DR - -After the player Uses a door, ACE broadcasts two messages: an -`UpdateMotion` to play the swing-open animation, and a -`GameMessageSetState (opcode 0xF74B)` to flip the door entity's -`PhysicsState.Ethereal` bit. The client must honor the state flip so -the door's collision cylinder stops blocking the threshold while the -door is open. The auto-close (30s) is a second SetState round-trip; -client just follows. - -acdream already parses `PhysicsState` from `CreateObject` and -already short-circuits ETHEREAL targets in -[CollisionExemption.cs](../../../src/AcDream.Core/Physics/CollisionExemption.cs). -**The single missing piece is parsing `0xF74B SetState` and -propagating the new state to `ShadowObjectRegistry`'s cached entity -record.** Everything else already works. Slice 1 is roughly one -commit. - ---- - -## Why L.2g (and not B.4 or "doors only") - -Three placement options were considered during the 2026-05-12 -brainstorm: - -| Option | Verdict | -|---|---| -| **Nest under B.4 interaction** | Rejected. B.4's scope is the *outbound* Use / UseWithTarget / PickUp packet (shipped 2026-04-28). Door state is an inbound + collision-state-toggle problem, not an outbound interaction one. | -| **Special-case Door Setup ID (`0x020019FF`)** | Rejected. Same wire mechanism (`SetState` flipping Ethereal) is also how ACE handles activated traps, opened chests, spell projectiles that become ethereal, and any other server-driven collision-state flip. Specializing on Door Setup ID would leave all those cases broken and re-emerge later as separate bugs. | -| **New L.2 sub-phase "L.2g β€” Dynamic PhysicsState toggling"** | **Selected.** L.2 already owns "movement & collision conformance"; a door you can't walk through after the server says it's open is a collision-conformance bug. Generic infrastructure (any entity, any state bit) with doors as the verification scenario. | - -The L.2 plan-of-record's L.2d revised paragraph already names L.2g -as a possible letter for this work; we're claiming it. - -**Lane assignment:** informal sixth lane "dynamic state." Updates the -lane table in the L.2 plan-of-record to include collision-state-toggle -as a first-class concern. - ---- - -## Problem evidence - -From the L.2d slice 1.5 trace (Holtburg, 2026-05-12): - -``` -live: spawn guid=0x7A9B4015 name="Door" setup=0x020019FF - pos=(132.6,17.1,94.1)@0xA9B40029 itemType=0x00000080 -[entity-source] id=0x000F4244 entityId=0x000F4244 src=0x020019FF - gfxObj=0x020019FF lb=0xA9B40029 type=Cylinder note=server-spawn-root -``` - -Five Door entities across Holtburg town (cells `0xA9B40029`, -`0xA9B40154`, `0xA9B40155`); each blocks its building's threshold with a -Cylinder collision. The 121 wall hits the L.2a probe attributed to the -building BSP turned out to be the player **already pushed back by the -Door cylinder** then grazing the doorframe. Slice 1.5's per-tick probe -showed `nObj=3` on every doorway resolve: one Door + two sphere checks -against the building BSP. - -The actual blocker is the closed Door, not the building. The blocker -goes away when the Door's PhysicsState gains the Ethereal bit (server -sets this in `Door.Open()`, see -[references/ACE/Source/ACE.Server/WorldObjects/Door.cs:127](../../../references/ACE/Source/ACE.Server/WorldObjects/Door.cs)). - ---- - -## Wire flow - -### Server β†’ client when the player Uses a door - -ACE's `Door.ActOnUse(player)` runs the following sequence: - -1. Check `IsLocked` + behind-test (AC retail allows opening locked doors - from behind). If locked-and-not-behind: broadcast a "door is locked" - chat string + sound effect, no state change. Otherwise: -2. `EnqueueBroadcastMotion(motionOpen)` β€” broadcasts an - `UpdateMotion` to all clients in range, motion = `(NonCombat, On)`. - This is the door's animation command. -3. `Ethereal = true; EnqueueBroadcastPhysicsState()` β€” broadcasts a - `GameMessageSetState (0xF74B)`. The new `PhysicsState` value has bit - `0x4` (Ethereal) set. -4. Sets `IsBusy = true` for the duration of the open animation. -5. Schedules `FinalizeClose` after `ResetInterval` (default 30s). - -### Server β†’ client when the auto-close fires - -1. `EnqueueBroadcastMotion(motionClosed)` β€” `UpdateMotion (NonCombat, Off)`. -2. After the close animation completes, server runs `FinalizeClose`: - `Ethereal = false; EnqueueBroadcastPhysicsState()` β€” another - `0xF74B SetState` with the Ethereal bit cleared. - -### The wire format of `0xF74B SetState` - -Two sources, **mildly disagreeing on sequence-field width:** - -[GameMessageSetState.cs](../../../references/ACE/Source/ACE.Server/Network/GameMessages/Messages/GameMessageSetState.cs) -in ACE writes: - -``` -guid : uint32 (4) -state : uint32 (4) -instance_sequence : uint32 (4) <-- ACE says u32 -state_sequence : uint32 (4) <-- ACE says u32 -``` - -[properties.rs](../../../references/holtburger/crates/holtburger-protocol/src/messages/object/messages/properties.rs) -in holtburger parses: - -``` -guid : uint32 (4) -state : uint32 (4) -instance_sequence : uint16 (2) <-- holtburger says u16 -state_sequence : uint16 (2) <-- holtburger says u16 -``` - -Holtburger has been validated against a retail-format server in the -wild. ACE's `Writer.Write((uint)sequence)` may or may not be using a -packed-write extension that downsizes to u16 β€” needs verification. **The -slice 1 implementation will default to holtburger's 12-byte format and -add a startup hex-dump probe to confirm before the parser is -committed.** If the actual payload is 16 bytes, the parser can be -trivially widened. - -### `PhysicsState.Ethereal` - -Value `0x00000004` (bit 2). Confirmed in: - -- ACE: `references/ACE/Source/ACE.Entity/Enum/PhysicsState.cs:10` -- acdream: `src/AcDream.Core/Physics/PhysicsBody.cs:30` -- Retail header: `docs/research/named-retail/acclient.h:2819` (cited - as `ETHEREAL_PS=0x4` in `CollisionExemption.cs:43`). - ---- - -## Current acdream state - -| Component | State | -|---|---| -| `PhysicsState` enum (Ethereal bit) | βœ… defined in `src/AcDream.Core/Physics/PhysicsBody.cs:30` | -| `CollisionExemption.IsExempt(...)` | βœ… already short-circuits when `(ETHEREAL_PS \| IGNORE_COLLISIONS_PS)` are both set on the target. Cites `acclient_2013_pseudo_c.txt:276782`. | -| `CreateObject` parses `PhysicsState` into the entity's shadow record | βœ… since 2026-04-29 | -| `UpdateMotion` pipeline for remote entities | βœ… works for player remotes; need to confirm it accepts non-creature entities with `(NonCombat, On/Off)` | -| `SetState (0xF74B)` inbound parser | ❌ does not exist | -| Propagating a post-spawn PhysicsState change into `ShadowObjectRegistry`'s cached state | ❌ does not exist | -| `[entity-source]` probe log captures `state` bits | ❌ β€” handoff's "slice 1.6" suggestion | - ---- - -## Slice plan - -### Slice 0.5 (optional prereq, fold into slice 1 if convenient) - -Add `PhysicsState` + `EntityCollisionFlags` to the `[entity-source]` -probe log line. Makes ETHEREAL flips observable from launch-log grep. -~5 LOC under the existing `ACDREAM_PROBE_BUILDING` flag. - -### Slice 1 β€” MVP (the M1-blocker slice) - -Goal: walk into the Holtburg inn doorway, click Use on the door, walk -through. - -Touchpoints: - -- `src/AcDream.Core.Net/` β€” new `SetStateMessage` parser for opcode - `0xF74B`. Default to holtburger's 12-byte format; add a hex-dump - emit on first message receipt to confirm exact byte width before the - parser commits. -- `src/AcDream.Core.Net/WorldSession.cs` (or wherever inbound game- - message dispatch lives) β€” route `0xF74B` to the new parser, then - forward the `(guid, newState)` pair to the entity layer. -- `src/AcDream.Core/Physics/ShadowObjectRegistry.cs` β€” new - `UpdatePhysicsState(guid, newState)` method that mutates the - cached state bits on the matching shadow entry. The existing - `CollisionExemption` check reads from this cached state, so no - resolver changes needed. -- Tests β€” synthetic test in - `tests/AcDream.Core.Tests/` that constructs a ShadowEntry with - `Ethereal=false`, calls `UpdatePhysicsState` flipping it on, and - asserts the next collision query returns "exempt." -- Visual verification β€” Holtburg inn doorway. Walk in, observe blocked. - Click Use. Observe door swings open AND player can now walk through. - Wait 30 seconds. Observe door closes AND player is blocked again. - -Acceptance: -- `dotnet build` + `dotnet test` green. -- `[resolve]` probe shows the Door cylinder no longer firing when the - door is open. -- Visual verification at Holtburg passes. -- Wire-byte width settled by hex-dump evidence; parser uses correct width. - -### Slice 2 β€” animation confirmation - -Goal: the door visually swings open and shut, not just becomes -walk-through. - -Most likely a no-op: the existing `UpdateMotion` pipeline that runs -`(NonCombat, On/Off)` commands for player remotes should drive any -entity with a MotionTable. Doors have a MotionTable (the same Setup -`0x020019FF`). Slice 2 is **verify, then either declare done or fix -whatever's missing**. - -If a fix is needed, the most likely cause is the motion handler -gating on `entity is Creature` somewhere upstream β€” a one-line removal -or a stance-relaxation in `MotionInterpreter`. - -### Deferred β€” UX polish - -These open only if observation demands them: - -- Sound on "door is locked" (ACE sends a `GameMessageSound` for - `Sound.OpenFailDueToLock`; verify acdream's audio pipeline plays it - via the existing 0xF755 handler). -- Bump-AI for creatures (ACE's `Door.OnCollideObject` auto-opens for - creatures with `AiOptions != 0`). This is server-driven; client gets - the same `SetState` flow. Probably no-op for the client. - ---- - -## Open questions to resolve in implementation - -1. **Wire-byte width of `0xF74B` sequence fields.** Default to - holtburger (u16+u16 = 12 bytes total). Confirm via hex-dump in slice - 1. If wrong, widen to ACE's claimed format (u32+u32 = 16 bytes). -2. **Does `UpdateMotion`'s existing handler dispatch motion to non- - creature entities?** Verified in slice 2. If no, one-line fix. -3. **Does ACE's `EnqueueBroadcastPhysicsState` skip the player who - triggered the Use, or include them?** Reading ACE's code, `EnqueueBroadcast(...)` - broadcasts to *everyone in range including self*. Slice 1 verifies the - player's own client receives the SetState (not just observers). - ---- - -## Acceptance for L.2g overall - -- All slices marked above as "Acceptance" pass. -- L.2 plan-of-record updated with an L.2g section (matching this spec's framing). -- M1 milestone doc updated: `L.2 (all sub-lanes a–f)` β†’ `L.2 (all sub-lanes a–g)`. -- CLAUDE.md's "currently in Phase L.2" paragraph updated to point at L.2g as the active sub-phase. -- A short ship handoff doc filed at - `docs/research/2026-05-XX-l2g-shipped-handoff.md` when slice 1+2 land. - ---- - -## Named retail anchors (for slice 1 code citations) - -- `CPhysicsObj::set_state` β€” the retail client's setter. Search by - `set_state\(` in - [docs/research/named-retail/acclient_2013_pseudo_c.txt](../../research/named-retail/acclient_2013_pseudo_c.txt). -- `CPhysicsObj::report_collision_with_object` β€” the retail per-object - collision-test entry; calls `CollisionExemption.IsExempt`-equivalent - inline. -- Header struct: `CPhysicsObj` in - [docs/research/named-retail/acclient.h](../../research/named-retail/acclient.h) - β€” `state` field is the `PhysicsState` bitmask. - ---- - -## Risk + rollback - -Risk is low. Wire-byte width has a fallback path (widen if hex-dump -shows 16 bytes). ETHEREAL plumbing already exists; we're feeding it -fresh data from one new source. No resolver changes. If slice 1 lands -broken, rollback is a single revert of one commit. - -The slice does **not** touch the broader L.2 collision path. It does -not change `ResolveWithTransition`, BSPQuery, ShadowObjectRegistry -broadphase, or any movement-prediction code. The change-surface is -strictly "one new wire message + one new mutator on cached state." diff --git a/docs/superpowers/specs/2026-05-12-phase-c1.5a-portals-design.md b/docs/superpowers/specs/2026-05-12-phase-c1.5a-portals-design.md deleted file mode 100644 index b92f3b2..0000000 --- a/docs/superpowers/specs/2026-05-12-phase-c1.5a-portals-design.md +++ /dev/null @@ -1,385 +0,0 @@ -# Phase C.1.5a β€” Portal PES wiring (Setup.DefaultScript on entity spawn) - -**Created:** 2026-05-12. -**Author:** Claude (lead engineer/architect). -**Phase:** C.1.5a (first of two slices; C.1.5b covers EnvCell statics + animation-hook verification). -**Parent plan:** [`docs/plans/2026-04-27-phase-c1-pes-particles.md`](../../plans/2026-04-27-phase-c1-pes-particles.md) Β§C.1.5. -**Baseline justification:** [`docs/plans/2026-05-11-phase-n6-perf-baseline.md`](../../plans/2026-05-11-phase-n6-perf-baseline.md) Β§4 β€” C.1.5 is the right next phase; production preset is comfortable, no perf escalation pressure. - ---- - -## Β§1 Goal - -Make server-spawned `WorldEntity` portals emit their retail-faithful particle -effects (portal swirls) at spawn time. Implement by **firing `Setup.DefaultScript` -through the already-shipped `PhysicsScriptRunner`** at the moment the entity -enters the world, mirroring retail's `play_script_internal` dispatch on object -spawn. - -Acceptance: the user walks `+Acdream` up to the **Holtburg Town network portal**, -opens a side-by-side comparison with a retail AC client, and confirms the portal -swirl matches retail in color, density, motion, and persistence. - -## Β§2 Scope - -**In:** - -- New class `EntityScriptActivator` (one file, ~50 lines). -- Wiring of activator's `OnCreate` / `OnRemove` calls into `GpuWorldState`'s - spawn-lifecycle methods (`AppendLiveEntity` / `RemoveEntityByServerGuid`), - immediately after the matching `EntitySpawnAdapter` calls. The activator is - constructed in `GameWindow` (where `_dats`, `_scriptRunner`, and - `_particleSink` are in scope) and passed into `GpuWorldState`'s constructor - as a new optional parameter, paralleling how `EntitySpawnAdapter` is wired. -- Three unit tests covering the activator's three branches - (fire / no-op-on-zero / cleanup-on-remove). -- Visual verification at the Holtburg Town network portal. - -**Out (deferred to C.1.5b):** - -- `EnvCell.StaticObjects` walker for interior chimneys / fireplaces. -- Animation-hook particle path verification (already wired in C.1; needs - a confirming check, deferred so this slice stays small). -- The WB-style "re-fire after 1 second" loop logic for non-persistent emitters - ([`ParticleEmitterRenderer.cs:119-130`](../../../references/WorldBuilder/Chorizite.OpenGLSDLBackend/Lib/ParticleEmitterRenderer.cs) in WB). - Portal swirls are persistent (`TotalParticles=0 && TotalSeconds=0`) and don't - need it. If C.1.5b discovers EnvCell static objects need it, that slice adds it. - -**Out (out of phase entirely):** - -- Renderer changes. `particle.frag` stays as-is; bindless migration is N.6 - slice 2 territory. -- Performance work. Per [baseline Β§4](../../plans/2026-05-11-phase-n6-perf-baseline.md), - CPU at the production preset is comfortable and there is no GPU pressure. -- Adding `WeenieClassId` to `WorldEntity`. Trigger is "has DefaultScript", - not "is portal" (see Β§4 Architecture for rationale). - -## Β§3 Background - -### Why this works today for *some* particles, not portals - -C.1 shipped a complete particle pipeline: -[`EmitterDescRegistry`](../../../src/AcDream.Core/Vfx/EmitterDescRegistry.cs) -(data) β†’ [`ParticleSystem`](../../../src/AcDream.Core/Vfx/ParticleSystem.cs) (sim) -β†’ [`ParticleHookSink`](../../../src/AcDream.Core/Vfx/ParticleHookSink.cs) -(dispatch) β†’ [`PhysicsScriptRunner`](../../../src/AcDream.Core/Vfx/PhysicsScriptRunner.cs) -(script scheduler) β†’ [`ParticleRenderer`](../../../src/AcDream.App/Rendering/ParticleRenderer.cs) -(draw). - -The chain is end-to-end, but `PhysicsScriptRunner.Play` is only called from -**two places today**: - -1. The server-driven `PlayScript (0xF754)` opcode handler in `GameWindow` β€” - spell casts, combat hits, emote effects. -2. The animation-hook path inside `MotionInterpreter` β€” feet sparks, weapon - trails (via `ParticleHookSink` directly, not through the runner). - -**Nothing fires `Setup.DefaultScript` when a static entity spawns.** Retail -does this (per the named decomp's `play_script_internal` analysis), and -`WorldBuilder` does the equivalent at mesh-prep time -([`ObjectMeshManager.cs:797`](../../../references/WorldBuilder/Chorizite.OpenGLSDLBackend/Lib/ObjectMeshManager.cs)). -Acdream skips it β€” every portal lacks its swirl, every chimney lacks its smoke. - -### Why not consume WB's staged emitters - -WB's `ObjectMeshManager.PrepareSetupMeshData` (line 771–795) collects -`StagedEmitter` entries from `setup.DefaultScript` and attaches them to -`ObjectMeshData.ParticleEmitters`. Three reasons we don't consume them: - -1. `WbMeshAdapter` calls `PrepareMeshDataAsync(id, isSetup: false)` β€” we go - through the per-part GfxObj path, not the Setup path - ([`WbMeshAdapter.cs:136`](../../../src/AcDream.App/Rendering/Wb/WbMeshAdapter.cs)). - Flipping that breaks shipped N.4/N.5 dispatcher assumptions. -2. WB's `CollectEmittersFromScript` drops the script's per-hook `StartTime` - offsets β€” it spawns every `CreateParticleHook` immediately. Our - `PhysicsScriptRunner` honors `StartTime` and is more retail-faithful. -3. C.1 already shipped a runner that *is* the equivalent of retail's - `play_script_internal`. Adding the missing call sites is cheaper and - structurally cleaner than building a parallel emitter-staging path. - -## Β§4 Architecture - -### New class - -`src/AcDream.App/Rendering/Vfx/EntityScriptActivator.cs`. New `Vfx/` -subdirectory under `Rendering/` β€” sits next to `ParticleRenderer.cs` and is -**not** under `Wb/` because the activator drives our own `PhysicsScriptRunner` -and has no WB dependency. - -Constructor β€” mirrors `EntitySpawnAdapter`'s factory-delegate pattern so the -activator has no `DatCollection` coupling and is fully unit-testable with -stubs: - -```csharp -public EntityScriptActivator( - PhysicsScriptRunner scriptRunner, - ParticleHookSink particleSink, - Func defaultScriptResolver) -``` - -The resolver returns the entity's `Setup.DefaultScript.DataId`, or `0` if the -Setup is missing / the dat throws / the field is zero. **The resolver swallows -exceptions; the activator stays a thin orchestrator.** - -Public surface β€” two methods only: - -```csharp -public void OnCreate(WorldEntity entity); -public void OnRemove(uint serverGuid); -``` - -No state on the activator. `PhysicsScriptRunner` already tracks per-entity -script instances by `(scriptId, entityId)`; `ParticleHookSink` already tracks -per-entity emitter handles. The activator doesn't duplicate that bookkeeping. - -### Trigger condition: "has DefaultScript", not "is portal" - -`WorldEntity` carries no `WeenieClassId` / `ObjectType` field -([`WorldEntity.cs`](../../../src/AcDream.Core/World/WorldEntity.cs)). We -*could* add one, but the WB-faithful trigger is "this entity's Setup has a -non-zero `DefaultScript`," which is also what retail's -`play_script_internal(setup.DefaultScript)` does at object load. - -Side effect of this choice: **the activator will fire DefaultScript for any -server-spawned entity whose Setup has one**, not just portals. This is -correct retail behavior. If a non-portal entity spawns visible unwanted -particles in slice 1, that means our resolver is reading retail's intended -data faithfully and the visual is what retail shows. If retail does NOT show -those particles and we do, that's evidence of a different gate retail -applies β€” to be investigated when seen. - -### Wiring point: GpuWorldState - -Live entity spawn / despawn already flows through -[`GpuWorldState`](../../../src/AcDream.App/Streaming/GpuWorldState.cs) on the -render thread β€” the network layer pushes spawns into -`AppendLiveEntity(landblockId, entity)`, the server's `RemoveObject` opcode -routes through `RemoveEntityByServerGuid(serverGuid)`. The existing -`EntitySpawnAdapter` lifecycle hooks live at those two call sites -(line 345 `OnCreate`, line 285 `OnRemove`). The activator hooks fire -immediately after, in the same order: - -```csharp -// GpuWorldState.AppendLiveEntity (line ~345): -_wbEntitySpawnAdapter?.OnCreate(entity); -_entityScriptActivator?.OnCreate(entity); // NEW β€” fires DefaultScript - -// GpuWorldState.RemoveEntityByServerGuid (line ~285): -_wbEntitySpawnAdapter?.OnRemove(serverGuid); -_entityScriptActivator?.OnRemove(serverGuid); // NEW β€” stops scripts + emitters -``` - -`GpuWorldState`'s constructor grows a fifth (optional) parameter for the -activator, paralleling how `EntitySpawnAdapter` is plumbed today. `GameWindow` -constructs the activator alongside `_wbEntitySpawnAdapter` and passes it -through. - -Production resolver lambda, constructed in `GameWindow` where `_dats` is in -scope: - -```csharp -entity => -{ - try - { - return _dats.Get(entity.SourceGfxObjOrSetupId)?.DefaultScript.DataId ?? 0; - } - catch - { - return 0; - } -} -``` - -The try/catch matches the pattern in -[`ParticleRenderer.cs:296-318`](../../../src/AcDream.App/Rendering/ParticleRenderer.cs) -(`ReadParticleGfxInfo`). - -## Β§5 Data flow + lifecycle - -### On spawn - -``` -GpuWorldState.AppendLiveEntity(landblockId, entity) -β”œβ”€ _wbEntitySpawnAdapter?.OnCreate(entity) // meshes ref-counted, animation state built -└─ _entityScriptActivator?.OnCreate(entity) - β”œβ”€ scriptId = resolver(entity) // Setup.DefaultScript.DataId, or 0 on miss/throw - β”œβ”€ if (scriptId == 0) return // no DefaultScript β†’ no-op - └─ _scriptRunner.Play(scriptId, - entity.ServerGuid, - entity.Position) - └─ PhysicsScriptRunner schedules hooks at their StartTime offsets; - each CreateParticleHook β†’ ParticleHookSink β†’ ParticleSystem - spawns the ParticleEmitter dat at the entity's anchor. -``` - -### On despawn - -``` -GpuWorldState.RemoveEntityByServerGuid(serverGuid) -β”œβ”€ _wbEntitySpawnAdapter?.OnRemove(serverGuid) // meshes ref-decremented, state cleared -└─ _entityScriptActivator?.OnRemove(serverGuid) - β”œβ”€ _scriptRunner.StopAllForEntity(serverGuid) // drop pending hooks - └─ _particleSink.StopAllForEntity(serverGuid, false) // kill live emitters (no fade) -``` - -Order on both is `spawnAdapter β†’ activator`. Symmetric. - -### Persistence (no re-fire logic needed) - -Portal swirls are persistent emitters: their `ParticleEmitter` dat has -`TotalParticles=0` AND `TotalSeconds=0`. -[`ParticleSystem.Tick`](../../../src/AcDream.Core/Vfx/ParticleSystem.cs) -only flips `Finished` when `TotalDuration > 0` or `TotalParticles > 0`, so -both-zero emitters never finish. They keep emitting until -`StopAllForEntity` kills them on despawn. - -WB's `_deadTimer` re-fire-after-1s (line 119–130 of -[`ParticleEmitterRenderer.cs`](../../../references/WorldBuilder/Chorizite.OpenGLSDLBackend/Lib/ParticleEmitterRenderer.cs)) -is for non-persistent emitters that should loop (`TotalSeconds > 0`, finishes, -1s gap, re-emit). Portals don't use it. Defer to C.1.5b if EnvCell static -objects need it. - -### Idempotency - -- Duplicate `OnCreate` for same `serverGuid` β€” `PhysicsScriptRunner.Play` - dedupes by `(scriptId, entityId)` and replaces the prior instance - ([`PhysicsScriptRunner.cs:136-140`](../../../src/AcDream.Core/Vfx/PhysicsScriptRunner.cs)). - βœ“ -- Duplicate `OnRemove` β€” both `StopAllForEntity` calls no-op on unknown guid. βœ“ -- `OnRemove` for never-spawned guid β€” same no-op behavior. βœ“ - -### Position handling - -Portals are stationary. `entity.Position` captured at spawn time is the anchor -for all of the script's hooks. We do not refresh per-frame. - -**Known limitation (documented, not fixed in slice 1):** if a portal is ever -relocated via server `SetPosition`, emitters stay at the old anchor. If this -case appears in practice we add a position-update handler β€” but no current -evidence retail's portals move. - -## Β§6 Error handling - -Failure modes and behavior: - -| Failure | Behavior | Notes | -|---|---|---| -| `entity.SourceGfxObjOrSetupId` references a missing Setup | resolver returns `0` | activator no-ops; standard streaming flicker handling | -| `_dats.Get(...)` throws | resolver returns `0` | try/catch in the resolver lambda | -| `Setup.DefaultScript.DataId == 0` | resolver returns `0` | activator no-ops; entity has no persistent script | -| `PhysicsScript` dat lookup misses inside `Play` | `Play` returns `false` | runner already handles; activator does nothing | -| `EmitterDescRegistry` miss for a `CreateParticleHook.EmitterInfoId` | exception propagates through `PhysicsScriptRunner.DispatchHook` (currently uncaught) | pre-existing C.1 behavior; out of scope for this slice. File an issue if observed in verification. | - -All failure paths are silent (no exceptions surface to the caller). Diagnostic -visibility comes from `ACDREAM_DUMP_PLAYSCRIPT=1` β€” every successful `Play` -and every fired hook prints. A missing portal swirl in verification is -diagnosed by checking the log for the missing entity's guid. - -## Β§7 Thread safety - -All calls execute on the render thread (where `EntitySpawnAdapter` already -runs). `PhysicsScriptRunner` is single-threaded by design. -`ParticleHookSink` uses `ConcurrentDictionary` and is safe regardless. No -new threading concerns introduced. - -## Β§8 Testing - -### Unit tests (slice 1's gating tests) - -`tests/AcDream.Core.Tests/Rendering/Vfx/EntityScriptActivatorTests.cs` (test -project convention: production code lives under `src/AcDream.App/...` but tests -go in `AcDream.Core.Tests` β€” mirrors the existing -[`EntitySpawnAdapterTests`](../../../tests/AcDream.Core.Tests/Rendering/Wb/EntitySpawnAdapterTests.cs) -location). Uses -hand-built `PhysicsScriptRunner` + capturing `ParticleHookSink` (or a thin -test double). No dats, no GL. - -1. **`OnCreate_WithDefaultScript_FiresRunnerWithEntityGuidAndPosition`** β€” - stub resolver returns `0x33000001`; assert `runner.Play(0x33000001, - entity.ServerGuid, entity.Position)` was called exactly once. -2. **`OnCreate_WithoutDefaultScript_DoesNothing`** β€” stub resolver returns - `0`; assert no `Play` call. -3. **`OnRemove_StopsScriptsAndEmitters`** β€” sequence an `OnCreate(entity)` - then `OnRemove(entity.ServerGuid)`; assert `runner.StopAllForEntity` and - `sink.StopAllForEntity` were each called once with the matching guid, and - `sink.StopAllForEntity` was passed `fadeOut: false`. - -### Integration tests β€” none for slice 1 - -The `GpuWorldState` wiring is two added lines (one in `AppendLiveEntity`, one -in `RemoveEntityByServerGuid`) plus a constructor parameter. An integration -test would require booting GL + dats + network. Coverage is the visual -verification gate instead. Existing `GpuWorldStateTests` will need a minor -update if they assert constructor arity; we extend them if so. - -### Visual verification β€” the acceptance criterion - -Procedure (per [CLAUDE.md](../../../CLAUDE.md) "Visual verification workflow"): - -1. `dotnet build` green. -2. `dotnet test` green (the three new unit tests plus the existing suite). -3. Launch live client with `ACDREAM_DUMP_PLAYSCRIPT=1` exported. -4. Walk `+Acdream` from spawn to the **Holtburg Town network portal**. -5. In parallel, a retail AC client viewing the same portal. -6. **User confirms**: the portal-swirl effect in acdream matches retail in - color, density, motion, and persistence. - -If verification fails (e.g. portal Setup has `DefaultScript=0` in the dat), -the diagnostic log shows whether `Play` fired and with what scriptId. We -investigate the actual data path in retail's named decomp before iterating β€” -do not blindly retry. - -## Β§9 Limitations + known gaps (post-slice-1) - -These are intentionally not fixed in slice 1; tracked here so the next slice -or a future phase picks them up: - -1. **`PartIndex` collapse on multi-part entities** (NEW β€” verified 2026-05-12 - at the Holtburg Town network portal). `ParticleHookSink.SpawnFromHook` - ignores `CreateParticleHook.PartIndex`, so every emitter in a multi-emitter - script collapses to `entity.Position + rotated(hook.Offset.Origin)`. Retail - distributes the script's emitters across the entity's mesh parts (arch base, - columns, apex). Visual symptom for the Holtburg portal: the 10-hook script - produces a compressed swirl partially buried in the ground instead of the - multi-tier shape retail renders. Filed as `docs/ISSUES.md` #56 with the - captured entity guids + script ids; affects slice 2 (EnvCell chimneys / - fireplaces are multi-part) and any future multi-emitter PES path. -2. **Moving entities** don't re-anchor their DefaultScript emitters per - frame. No evidence retail's portals or chimneys move; revisit if visual - verification surfaces a regression. -3. **WB's re-fire-after-1s loop** is not implemented. Persistent emitters - work today; looping non-persistent emitters (if EnvCell static objects - use them) would need it in C.1.5b. -4. **Animation-hook particle path** (`MotionInterpreter` β†’ - `ParticleHookSink`) is shipped in C.1 but **not verified** by a recent - visual test in this codebase state. Confirming this path is the second - half of C.1.5b. - -## Β§10 Slice 2 preview (C.1.5b) - -For context, not part of this slice's work: - -- **Walker for `EnvCell.StaticObjects`.** Each static object has a Setup - reference; same `DefaultScript` dispatch applies. Needs a synthetic - entity-id scheme because static objects have no `ServerGuid`. Likely: - hash of `(landblockId, cellIndex, staticIndex)` β†’ 32-bit synthetic id with - a marker high bit so it doesn't collide with server guids. -- **Verification step for animation hooks.** Cast a spell or trigger an - emote on `+Acdream`, observe the particle effect, compare to retail. -- **Possible: WB re-fire-after-1s logic** in `ParticleSystem` if EnvCell - static-object PES data needs it. - -C.1.5b spec lands after C.1.5a verification passes. - -## Β§11 Implementation notes - -- The new directory `src/AcDream.App/Rendering/Vfx/` is created by this - slice. `ParticleRenderer.cs` stays where it is (under `Rendering/`); the - new `Vfx/` is for spawn-time orchestration classes only. -- Estimated effort: ~1 day. Activator is small, wiring is two lines, tests - are three cases. -- No CLAUDE.md updates required by this slice β€” the C.1.5a / C.1.5b split is - internal to the C.1 phase plan. -- Roadmap update: on ship, add a "Phase C.1.5a SHIPPED 2026-05-12" entry to - [`docs/plans/2026-04-11-roadmap.md`](../../plans/2026-04-11-roadmap.md). diff --git a/docs/superpowers/specs/2026-05-13-l2d-cbuildingobj-collision-design.md b/docs/superpowers/specs/2026-05-13-l2d-cbuildingobj-collision-design.md deleted file mode 100644 index 28c8ef4..0000000 --- a/docs/superpowers/specs/2026-05-13-l2d-cbuildingobj-collision-design.md +++ /dev/null @@ -1,311 +0,0 @@ -# L.2d β€” Movement & Collision Conformance: Building Shape Fidelity (design spec) - -**Status:** Draft, 2026-05-13. Slice 1 ready to implement after build-env resolution. -**Roadmap owner:** Phase L.2d in [docs/plans/2026-04-29-movement-collision-conformance.md](../../plans/2026-04-29-movement-collision-conformance.md). -**Authors:** brainstorm session 2026-05-13 (cold-start from L.2a slice 1+2+3 evidence). -**Predecessor handoff:** [docs/research/2026-05-12-l2a-shipped-l2d-handoff.md](../../research/2026-05-12-l2a-shipped-l2d-handoff.md). - ---- - -## TL;DR - -L.2d slice 1 is a **read-only BSP-hit diagnostic** that captures full collision evidence whenever the L.2a `[resolve]` probe fires `hit=yes`. The trace distinguishes three hypotheses (wrong BSP loaded / over-registered parts / BSPQuery flaw) before any behavior change. Slice 2 is the actual fix, scoped from slice 1's evidence. - -This spec replaces the plan-of-record's earlier "port `CBuildingObj` + per-cell walkability" framing β€” that framing was wrong (see *Reframe* below). - ---- - -## Reframe β€” what L.2d actually is - -The handoff and the plan-of-record's prior "Current sub-direction" paragraph both pointed at `CBuildingObj` + **per-cell walkability** as the missing piece for doorway traversal. Reading the named-retail decomp + ACE port shows that's not how retail solves doorways. - -[BuildingObj.cs:39-52](../../../references/ACE/Source/ACE.Server/Physics/Common/BuildingObj.cs) and named-retail [`acclient_2013_pseudo_c.txt:701260`](../../research/named-retail/acclient_2013_pseudo_c.txt) define `find_building_collisions` as 6 lines: - -```csharp -public TransitionState find_building_collisions(Transition transition) { - if (PartArray == null) return TransitionState.OK; - transition.SpherePath.BuildingCheck = true; - var result = PartArray.Parts[0].FindObjCollisions(transition); - transition.SpherePath.BuildingCheck = false; - if (result != OK && !transition.ObjectInfo.State.HasFlag(Contact)) - transition.CollisionInfo.CollidedWithEnvironment = true; - return result; -} -``` - -Retail does **one BSP test on `Parts[0]`**. Period. The `BuildingCheck` flag (`bldg_check` on the SPHEREPATH) only gates `sphere_intersects_solid` in [`BSPTREE::find_collisions`](../../research/named-retail/acclient_2013_pseudo_c.txt)'s **placement-insert / obstruction-ethereal** branch (lines 323323 and 323744–323751). Normal walking transitions never read it. - -Implications: - -- The doorway gap is encoded **inside the physics BSP of `Parts[0]`** itself. If retail's collision works at a building doorway, that physics BSP has leaves marking the doorway interior as non-solid. -- `find_cell_list` / `point_in_cell` / `sphere_intersects_cell` / `box_intersects_cell` (the "per-cell walkability" anchors the handoff listed) are how the resolver selects **which cells** to iterate over per tick, not how it decides **whether the wall has a hole**. That work belongs to **L.2e** (cell ownership / find_cell_list / `CELLARRAY` / outdoor seam updates), not L.2d. -- L.2d's actual goal is **shape fidelity**: when our resolver collides against a building, the resulting behavior should match what retail's `Parts[0]` BSP test would produce. - -The L.2a slice 1+2+3 evidence still stands: 126/140 doorway-push hits attribute to `obj=0xA9B47900` (one specific BSP shadow entry). The question is **why that BSP reports a hit where retail's wouldn't.** - ---- - -## Three hypotheses - -| Code | Hypothesis | Form a slice-2 fix would take | -|---|---|---| -| **X** | We're loading the **wrong BSP** for that part. Either `GfxObjFlags.HasPhysics` is false and we fell back to visual-mesh AABB; or `PhysicsDataCache.CacheGfxObj` cached the visual BSP root instead of `physics_bsp`. | Fix `PhysicsDataCache` BSP-selection. | -| **Y** | We're **over-registering** building parts. ACE/retail tests *only* `Parts[0]` per `find_building_collisions`. Our [`GameWindow.cs:5495-5539`](../../../src/AcDream.App/Rendering/GameWindow.cs) MeshRefs loop registers *every* part with a non-null BSP root as a separate `ShadowEntry`. A non-zero `partIdx` part may overlap the doorway when `Parts[0]` doesn't. | Skip non-`Parts[0]` registration for building entities (small, retail-faithful); or port a thin `BuildingObj` aggregator. | -| **Z** | BSPQuery has a **traversal flaw** that doesn't see the doorway gap retail does. e.g. swept-sphere classification of `BSPNode` leaves differs from retail's `BSPTREE::find_collisions`. | Audit BSPQuery against [`acclient_2013_pseudo_c.txt:323725`](../../research/named-retail/acclient_2013_pseudo_c.txt) line-by-line. | - -Slice 1 collects the evidence to identify which one is true. Slice 2 is the right-sized fix. - ---- - -## Slice 1 β€” BSP-Hit Diagnostic (this slice) - -### Components - -| # | Component | File | Change | -|---|---|---|---| -| 1 | `PhysicsDiagnostics.ProbeBuilding` | [src/AcDream.Core/Physics/PhysicsDiagnostics.cs](../../../src/AcDream.Core/Physics/PhysicsDiagnostics.cs) | New `static bool ProbeBuilding` flag, env var `ACDREAM_PROBE_BUILDING`. Same shape as existing `ProbeResolve` / `ProbeCell`. | -| 2 | `DebugPanel` checkbox | [src/AcDream.UI.Abstractions/Panels/Debug/DebugPanel.cs](../../../src/AcDream.UI.Abstractions/Panels/Debug/DebugPanel.cs), [DebugVM.cs](../../../src/AcDream.UI.Abstractions/Panels/Debug/DebugVM.cs) | Third Diagnostics row: *Probe BSP hits (slow)*. Visible when `ACDREAM_DEVTOOLS=1`. | -| 3 | `[resolve-bldg]` emission | [src/AcDream.Core/Physics/TransitionTypes.cs](../../../src/AcDream.Core/Physics/TransitionTypes.cs) β€” at the existing L.2a slice 3 attribution site (current line ~1544–1549 of `FindObjCollisions`) | When `PhysicsDiagnostics.ProbeBuilding` is on and a hit is attributed to a shadow entity, emit one multi-line `[resolve-bldg]` log entry. All fields (`obj`, `partCached`, `physics`, `obj.Position`, `obj.Rotation`) are already in scope. | -| 4 | `BSPQuery.FindCollisions` hit-poly out-param | [src/AcDream.Core/Physics/BSPQuery.cs](../../../src/AcDream.Core/Physics/BSPQuery.cs) | Add optional `out ResolvedPolygon? hitPoly` parameter to the public `FindCollisions` entry point. Default `null` at non-probe call sites. Mutated at the ~5 internal sites where a poly hit is recorded (Path 5/6 of the dispatcher). Cylinder path leaves it `null`. | -| 5 | `[entity-source]` registration log | [src/AcDream.App/Rendering/GameWindow.cs](../../../src/AcDream.App/Rendering/GameWindow.cs) at the 6 `_physicsEngine.ShadowObjects.Register(...)` call sites (lines 2969, 5530, 5581, 5611, 5630, 5810) | When `PhysicsDiagnostics.ProbeBuilding` is on at registration time, emit one line per ShadowEntry registered. Makes `entityId=0xA9B479` greppable to its source within the same log file. | -| 6 | Plan-of-record correction | [docs/plans/2026-04-29-movement-collision-conformance.md](../../plans/2026-04-29-movement-collision-conformance.md) L.2d section | Replace the "Current sub-direction (2026-05-12, evidence-driven by L.2a slice 2 + 3)" paragraph with the ACE-grounded framing (this spec's *Reframe* section, distilled). | - -**Total surface: ~150 LOC code, ~80 LOC tests, ~20 LOC doc correction.** - -### Data flow - -``` -walking-into-doorway - β–Ά PhysicsEngine.ResolveWithTransition - β–Ά TransitionTypes.FindObjCollisions - β–Ά for each shadow obj in GetNearbyObjects(...): - β–Ά BSPQuery.FindCollisions(..., out hitPoly) ← (component 4) - OR CylinderCollision(...) [hitPoly remains null] - β–Ά on (result != OK || normal flipped): - β–Ά ci.CollideObjectGuids.Add(obj.EntityId) [existing L.2a sl3] - β–Ά ci.LastCollidedObjectGuid = obj.EntityId [existing L.2a sl3] - β–Ά if PhysicsDiagnostics.ProbeBuilding: ← (component 3) - β–Ά emit [resolve-bldg] entry with level-C fields -``` - -Registration side (one-time per landblock load): -``` -LandblockLoader.BuildEntitiesFromInfo (existing) - β–Ά GameWindow.RegisterEntityShadows (existing) - β–Ά for each MeshRef / CylSphere / Sphere: - β–Ά ShadowObjects.Register(...) [existing] - β–Ά if PhysicsDiagnostics.ProbeBuilding: ← (component 5) - β–Ά emit [entity-source] line -``` - -### Probe output format - -Per registration (one-time): -``` -[entity-source] id=0xA9B47900 entityId=0xA9B479 partIdx=0 src=0x02000567 lb=0xA9B40000 hasPhys=true -``` - -Per `[resolve]` `hit=yes` line (per tick while probe is on): -``` -[resolve-bldg] obj=0xA9B47900 entityId=0xA9B479 partIdx=0 - src=0x02000567 hasPhys=true bspR=8.50 vAabbR=8.45 - entOrigin_lb=(132.0,21.0,17.5) - hitPoly: numVerts=4 plane=(0.000,1.000,0.000,-94.123) - v0_local=(-1.2,0.0,0.5) v0_world=(131.5,94.1,18.0) - v1_local=( 1.2,0.0,0.5) v1_world=(133.5,94.1,18.0) - v2_local=( 1.2,0.0,3.0) v2_world=(133.5,94.1,20.5) - v3_local=(-1.2,0.0,3.0) v3_world=(131.5,94.1,20.5) -``` - -Cylinder shadow entries (Setup-CylSphere/Sphere hits, not building BSP) dump: -``` -[resolve-bldg] obj=0x... entityId=0x... partIdx=... src=0x... hasPhys=... bspR=... vAabbR=... - entOrigin_lb=(...) - hitPoly: n/a (cylinder) -``` - -### Field semantics - -| Field | Source | Used to distinguish | -|---|---|---| -| `obj` | `ci.LastCollidedObjectGuid` (the `partId` from the broadphase) | identity | -| `entityId` | `obj / 256` | identity, greppable to `[entity-source]` | -| `partIdx` | `obj & 0xFF` β€” valid as long as `partIndex < 256` per the `partId = entity.Id * 256 + partIndex` formula at [GameWindow.cs:5529](../../../src/AcDream.App/Rendering/GameWindow.cs:5529); buildings have ≀ a handful of parts in practice, so the assumption holds | **Y**: non-zero `partIdx` hits while `partIdx=0` is innocent β‡’ over-registration | -| `src` | the `WorldEntity.SourceGfxObjOrSetupId` resolved via the partId mapping | which DAT object backs this entity | -| `hasPhys` | `gfxObj.Flags.HasFlag(GfxObjFlags.HasPhysics)` from raw DAT (looked up via `DatCollection.Get(meshRef.GfxObjId)`) | **X**: false β‡’ visual-AABB fallback in play | -| `bspR` | `partCached.BSP.Root.BoundingSphere.Radius` from `PhysicsDataCache.GetGfxObj(...)` | **X**: vs `vAabbR` to spot visual-vs-physics mismatch | -| `vAabbR` | `partCached.BoundingSphere?.Radius` from `PhysicsDataCache.GetVisualBounds(...)` | as above | -| `entOrigin_lb` | `obj.Position - landblockOrigin`, in landblock-local meters | spatial β€” does the hit make sense for the building's known position? | -| `hitPoly.*` | new `out ResolvedPolygon?` from `BSPQuery.FindCollisions` (component 4); transformed back to world space via `obj.Position + Vector3.Transform(localVert * obj.Scale, obj.Rotation)` | **Z**: lets us inspect the actual poly being hit; if it's geometrically inside the doorway gap, BSPQuery is mistraversing | - -### Hypothesis-distinguishing matrix - -| Trace pattern | Hypothesis | Likely slice 2 | -|---|---|---| -| `hasPhys=false` OR `bspR β‰ˆ 0` for most hits | **X** (wrong BSP loaded) | Fix `PhysicsDataCache.CacheGfxObj` BSP-selection or the visual-AABB fallback in `GameWindow` MeshRefs loop. | -| Hits with `partIdx β‰  0` while no `partIdx = 0` hits exist for the same `entityId` | **Y** (over-registration) | Register only `Parts[0]` for building entities β€” equivalent to `BuildingObj.find_building_collisions`'s "Parts[0] only" rule. ~40 LOC localized to the MeshRefs loop. | -| `hasPhys=true`, hits all on `partIdx=0`, but `hitPoly` lies inside the visible doorway opening | **Z** (BSPQuery flaw) | Audit `BSPQuery.FindCollisions` against named-retail [`BSPTREE::find_collisions` at 323725](../../research/named-retail/acclient_2013_pseudo_c.txt). | -| Mixed / inconclusive | Slice 1.5 | Expand the probe to dump the entire BSP traversal path for one frame. | - -### Tests (synthetic only) - -Three tests under `tests/AcDream.Core.Tests/Physics/`: - -1. **`PhysicsDiagnosticsTests.BuildingProbe_GatesByEnvVar`** β€” verify the static flag gates output. Set `PhysicsDiagnostics.ProbeBuilding = false`, run a synthetic hit, assert no `[resolve-bldg]` output. Set to true, repeat, assert output present. - -2. **`FindObjCollisionsTests.Probe_FormatsHitFields`** β€” register a synthetic BSP `ShadowEntry` with a 4-vertex known polygon (vertices and plane explicitly chosen), sweep a sphere into it, assert the emitted line contains the expected `partIdx`, `bspR` (within `Β±0.01`), `hitPoly.numVerts=4`, and `v0_world` (within `Β±0.01`). - -3. **`FindObjCollisionsTests.Probe_CylinderHit_DumpsNa`** β€” register a synthetic cylinder `ShadowEntry`, sweep a sphere into it, assert the emitted line contains the literal substring `hitPoly: n/a (cylinder)`. - -Output capture: tests redirect `Console.Out` to a `StringWriter`, run the action, read back, assert. - -**No real-DAT fixtures in slice 1.** The Holtburg-doorway live capture is the slice's evidence. - -### Acceptance criteria - -1. `dotnet build` green; the 3 new tests green. (8 pre-existing failures unchanged β€” these are *not* in scope for slice 1; see *Operational notes*.) -2. Launch with `ACDREAM_PROBE_BUILDING=1 ACDREAM_PROBE_RESOLVE=1 ACDREAM_DEVTOOLS=1`, walk acdream up to a Holtburg town doorway, hold W for ~2 seconds, close. The captured log contains: - - One `[entity-source]` line per registered `ShadowEntry` for the player's neighborhood landblocks. - - One `[resolve-bldg]` line per `[resolve] ... hit=yes` line. -3. The trace permits a ≀5-line "hypothesis X / Y / Z" memo with concrete evidence pointing at slice 2's form. -4. Plan-of-record L.2d section's "Current sub-direction" paragraph rewritten to match this spec's *Reframe* section. - ---- - -## Slice 2 β€” The actual fix (sketch, scoped post-slice-1) - -Slice 2's exact form depends on slice 1's evidence. Outline only: - -- **If X**: Add a fixture test to `PhysicsDataCacheTests` that loads a real Holtburg building GfxObj from the DAT, verifies `Resolved` polygon plane normals + counts match retail-extracted ground-truth (via Binary Ninja PDB dump of `physics_polygons` in a known building DID). Then fix the cache's BSP-selection logic. Conformance-cited. -- **If Y**: Add `EntityProvenance` enum (`LandblockBuilding | Stab | Scenery | EnvCellStab | ServerSpawn`) β€” minimal version, populated at construction in `LandblockLoader` + `GameWindow.BuildInteriorEntitiesForStreaming`. In the MeshRefs loop, gate "register every MeshRef with non-null BSP root" β†’ "register `MeshRefs[0]` only when `Provenance == LandblockBuilding`". Cite [`BuildingObj.cs:45`](../../../references/ACE/Source/ACE.Server/Physics/Common/BuildingObj.cs) + `acclient_2013_pseudo_c.txt:701268`. -- **If Z**: Side-by-side audit. Pull `BSPQuery.FindCollisions` open against [`BSPTREE::find_collisions`](../../research/named-retail/acclient_2013_pseudo_c.txt) (lines 323725–...). Annotate each branch. Fix whichever branch doesn't match. - -In all three cases slice 2 is expected to be ~one commit, ~50–100 LOC plus a real-DAT fixture test. - ---- - -## Slice 3+ β€” Optional (post-slice-2 conformance + L.2f) - -After slice 2 lands and visual-verified at Holtburg: - -- Real-DAT fixture tests for additional known buildings (Yaraq inn, Arwic chapel, dungeon entrance portal frames) β€” proves the fix isn't Holtburg-specific. -- Folded into L.2f (real-DAT + retail-observer conformance) per the plan-of-record. -- Promote to "L.2d shipped" once at least three building geometries pass conformance both synthetic and live. - ---- - -## Named retail anchors - -Primary source: [`docs/research/named-retail/acclient_2013_pseudo_c.txt`](../../research/named-retail/acclient_2013_pseudo_c.txt). -Cross-reference C# port: [`references/ACE/Source/ACE.Server/Physics/`](../../../references/ACE/Source/ACE.Server/Physics/). - -| Symbol | PDB Address | Pseudo-C line | Role | -|---|---|---|---| -| `CBuildingObj::find_building_collisions` | `0x006b5300` | 701260 | 6-line entry: sets `bldg_check`, calls `CPhysicsPart::find_obj_collisions` on `Parts[0]` only | -| `CBuildingObj::find_building_transit_cells` | `0x006b5230`, `0x006b52a0` | 701214, 701237 | iterates `Portals`, dispatches to `CEnvCell::check_building_transit` β€” L.2e territory | -| `CSortCell::find_collisions` | `0x005340a0` | 318337 | LandCell-with-building override; delegates to `CBuildingObj::find_building_collisions` | -| `CPhysicsPart::find_obj_collisions` | `0x0050d8d0` | 275045 | calls `CGfxObj::find_obj_collisions` on its single GfxObj | -| `CGfxObj::find_obj_collisions` | `0x00534700` | 318793 | bounding-sphere broadphase, then calls `BSPTREE::find_collisions` on `this->physics_bsp` | -| `BSPTREE::find_collisions` | `0x0053a440` | 323725 | 6-path dispatcher; `bldg_check` only read in the placement-insert / obstruction-ethereal branch (323744–323751) | -| `bldg_check` (SPHEREPATH field) | offset `0x0` in flagblock at `0x00841e7c` | 1155234 | flag, set/cleared by `CBuildingObj::find_building_collisions` | -| `CObjCell::find_cell_list` | `0x0052b4e0` | 308742 | builds `CELLARRAY` of cells overlapping the sphere; **L.2e**, not L.2d | -| `CCellStruct::point_in_cell` | `0x005338f0` | 317657 | tailcalls `BSPTREE::point_inside_cell_bsp`; **L.2e** | -| `CCellStruct::sphere_intersects_cell` | `0x00533900` | 317666 | tailcalls `BSPTREE::sphere_intersects_cell_bsp`; **L.2e** | -| `CCellStruct::box_intersects_cell` | `0x00533910` | 317675 | tailcalls `BSPTREE::box_intersects_cell_bsp`; **L.2e** | - -The bottom four anchors are listed because the original handoff named them as L.2d anchors; per the *Reframe* they are not. They remain L.2e anchors. - ---- - -## Operational notes - -### Worktree build-env precondition - -This worktree at `.claude/worktrees/sharp-chatelet-023dda` is missing `references/` (gitignored except WorldBuilder, which is a submodule that wasn't initialized when the worktree was created). Build fails with unresolved `Chorizite` / `WorldBuilder` / `TerrainEntry` types. - -Resolution before slice 1 implementation (decided 2026-05-13: option (i)): - -1. `git submodule update --init --recursive references/WorldBuilder` β€” populates the tracked submodule in this worktree. -2. Directory junctions for the 6 gitignored peer reference dirs from the main checkout: - - `references/ACE`, `references/ACViewer`, `references/Chorizite.ACProtocol`, `references/AC2D`, `references/DatReaderWriter`, `references/holtburger`. - - Windows: `cmd /c mklink /J references/ C:\Users\erikn\source\repos\acdream\references\`. - -After resolution: `dotnet build` succeeds, and the 8 pre-existing test failures become observable for triage (separate concern; not in slice 1). - -### Pre-existing test failures (not in scope) - -8 tests fail at the branch base (verified by stash + rerun in the L.2a session). They are *not* introduced by L.2a or slice 1. Most touch movement/physics code: - -- `MotionInterpreterTests.GetMaxSpeed_*` (3) -- `PositionManagerTests.ComputeOffset_BothActive_Combined` -- `PlayerMovementControllerTests.Update_ForwardInput_MovesInFacingDirection` -- `DispatcherToMovementIntegrationTests.Dispatcher_W_held_produces_forward_motion` -- `BSPStepUpTests.{D4_AirborneMover_TallWall_PersistsSlidingNormalAcrossFrames, C3_Path6_AirborneMoverHitsSteepSlope_SetsCollide}` - -Acceptance criterion 1 says "8 pre-existing failures unchanged" β€” slice 1's tests must not introduce new failures, but must not be blocked by these pre-existing ones either. The BSPStepUp two are in the same module slice 1 touches; verify they remain failing in the same way post-slice-1. - -Triage is a sibling task β€” recommend a `triage-failing-tests` slice between L.2d slice 1 and slice 2, since slice 2 may evolve `BSPQuery` (under hypothesis Z) or movement registration (under hypothesis Y), and trying to fix a moving target is wasted effort. - -### Live-test reproduction recipe - -```powershell -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -$env:ACDREAM_TEST_HOST = "127.0.0.1" -$env:ACDREAM_TEST_PORT = "9000" -$env:ACDREAM_TEST_USER = "testaccount" -$env:ACDREAM_TEST_PASS = "testpassword" -$env:ACDREAM_DEVTOOLS = "1" -$env:ACDREAM_PROBE_CELL = "1" -$env:ACDREAM_PROBE_RESOLVE = "1" -$env:ACDREAM_PROBE_BUILDING = "1" -dotnet run --project src\AcDream.App\AcDream.App.csproj --no-build -c Debug 2>&1 | - Tee-Object -FilePath "launch-l2d-slice1.log" -``` - -Walk acdream to a Holtburg town doorway. Hold W for ~2 seconds. Close. Grep `launch-l2d-slice1.log` for: - -- `\[entity-source\]` β€” registered ShadowEntry inventory -- `\[resolve-bldg\]` β€” per-hit BSP diagnostic - -The L.2a probes (`[resolve]`, `[cell-transit]`) should still fire interleaved. - -### Verification: L.2a probes still work - -Before slice 1 implementation, relaunch with `ACDREAM_PROBE_RESOLVE=1 ACDREAM_PROBE_CELL=1 ACDREAM_DEVTOOLS=1` (NOT `ACDREAM_PROBE_BUILDING` β€” it doesn't exist yet on the branch base) and confirm `[resolve]` / `[cell-transit]` lines still emit. Validates the branch-base L.2a foundation is intact and acceptance criterion 2 of slice 1 is testable. - ---- - -## Slice plan - -| Slice | Commit | Touches | Conformance citation | -|---|---|---|---| -| **1** | `feat(phys L.2d slice 1): BSP-hit diagnostic probe + plan-of-record correction` | `PhysicsDiagnostics.cs`, `TransitionTypes.cs`, `BSPQuery.cs`, `GameWindow.cs`, `DebugPanel.cs`, `DebugVM.cs`, `2026-04-29-movement-collision-conformance.md`, 3 new tests under `tests/AcDream.Core.Tests/Physics/` | `acclient_2013_pseudo_c.txt:701260` (`CBuildingObj::find_building_collisions`), `ACE BuildingObj.cs:39-52`, `acclient_2013_pseudo_c.txt:323725` (`BSPTREE::find_collisions`) | -| **2** | TBD post-slice-1 evidence | depends on X/Y/Z | as appropriate per hypothesis | -| **3+** | TBD (folded into L.2f conformance) | real-DAT fixtures at additional buildings | retail PDB dump of `physics_polygons` for each fixture | - -Slice 1 is **one commit**, ~150 LOC code + ~80 LOC tests + ~20 LOC doc correction. - ---- - -## Decision log - -- **2026-05-13 (this spec):** Reframed L.2d from "port CBuildingObj + per-cell walkability" to "diagnostic + minimal fix" after [ACE BuildingObj.cs:39-52](../../../references/ACE/Source/ACE.Server/Physics/Common/BuildingObj.cs) review revealed retail's `find_building_collisions` is one BSP test on `Parts[0]` with no per-cell walkability involvement. -- **2026-05-13:** Picked diagnostic-first slice 1 (option A in brainstorm) over a faithful `BuildingObj` port. Rationale: the plan-of-record's premise was wrong, so committing to a multi-day port before knowing the actual cause risks redoing the design. -- **2026-05-13:** Probe field set = level C (full poly dump). Rationale: distinguishes all three hypotheses in one capture without expansion later. -- **2026-05-13:** Classification source = option A (skip `classified=`, rely on grep-by-entityId). Rationale: YAGNI; if `Provenance` becomes load-bearing for slice 2 (hypothesis Y), introduce it then. -- **2026-05-13:** Doc-update aggressiveness = option A (inline-correct the L.2d section in plan-of-record only). Rationale: doc drift is forbidden by CLAUDE.md. -- **2026-05-13:** Worktree env resolution = option (i) (submodule init + junctions). Rationale: preserves worktree convention. - ---- - -## References - -- L.2 plan-of-record: [docs/plans/2026-04-29-movement-collision-conformance.md](../../plans/2026-04-29-movement-collision-conformance.md) -- L.2a handoff: [docs/research/2026-05-12-l2a-shipped-l2d-handoff.md](../../research/2026-05-12-l2a-shipped-l2d-handoff.md) -- Named-retail pseudo-C: [docs/research/named-retail/acclient_2013_pseudo_c.txt](../../research/named-retail/acclient_2013_pseudo_c.txt) -- Named-retail symbol map: [docs/research/named-retail/symbols.json](../../research/named-retail/symbols.json) -- ACE BuildingObj: [references/ACE/Source/ACE.Server/Physics/Common/BuildingObj.cs](../../../references/ACE/Source/ACE.Server/Physics/Common/BuildingObj.cs) -- ACE SortCell: [references/ACE/Source/ACE.Server/Physics/Common/SortCell.cs](../../../references/ACE/Source/ACE.Server/Physics/Common/SortCell.cs) -- ACE Landblock: [references/ACE/Source/ACE.Server/Physics/Common/Landblock.cs](../../../references/ACE/Source/ACE.Server/Physics/Common/Landblock.cs) -- Current physics surface: [src/AcDream.Core/Physics/](../../../src/AcDream.Core/Physics/) diff --git a/docs/superpowers/specs/2026-05-13-phase-b4b-design.md b/docs/superpowers/specs/2026-05-13-phase-b4b-design.md deleted file mode 100644 index e160d3b..0000000 --- a/docs/superpowers/specs/2026-05-13-phase-b4b-design.md +++ /dev/null @@ -1,458 +0,0 @@ -# Phase B.4b β€” Outbound Use Handler Wiring - -**Status:** Design spec, created 2026-05-13 after L.2g slice 1 ship handoff. -**Branch:** `claude/compassionate-wilson-23ff99` (worktree `compassionate-wilson-23ff99`). -**Predecessors:** -- [docs/research/2026-05-12-l2g-slice1-shipped-handoff.md](../../research/2026-05-12-l2g-slice1-shipped-handoff.md) - β€” L.2g slice 1 shipped the inbound `SetState (0xF74B)` pipeline; visual - test was deferred when investigation uncovered that the outbound Use - handler had never been wired. -- [docs/ISSUES.md](../../ISSUES.md) #57 β€” B.4 interaction-handler gap - filed 2026-05-12, promoted to Phase B.4b. -- Phase B.4 (`InteractRequests` wire builders + `InputAction` enum + - `KeyBindings`, shipped 2026-04-28 per memory; commit history confirms - the wire builders + bindings but not the handler). - -**Milestone:** M1 β€” Walkable + clickable world. Demo scenario *"open -the inn door"* depends on this slice landing. Once B.4b lands, -L.2g slice 1's deferred visual test verifies in the same scenario. - -**Estimate:** ~80 LOC, 1-2 subagent dispatches, ~30 minutes implementation. - ---- - -## TL;DR - -Phase B.4 (2026-04-28) shipped half of itself: the wire-message -builders (`InteractRequests.BuildUse` / `BuildUseWithTarget` / -`BuildTeleToLifestone`), the `InputAction` enum entries -(`SelectLeft` / `SelectDblLeft` / `UseSelected` / etc.), and the -default keybindings. What was never landed: a handler that picks an -entity at the mouse position when the user clicks, stores the -selection, and sends a `BuildUse` packet. - -Two further gaps surfaced during this session's exploration that the -L.2g handoff and ISSUES.md #57 both miss-claim as "exists": -- `WorldPicker` β€” does NOT exist in `src/`. Doc-only. -- `SelectionState` β€” does NOT exist in `src/`. Doc-only. -- `InteractRequests.BuildPickUp` β€” does NOT exist; only `BuildUse`, - `BuildUseWithTarget`, and `BuildTeleToLifestone` are present. - -B.4b creates the minimum new structure to close the gap: one new -file (`WorldPicker.cs` as a stateless static helper), one rename -(`_selectedTargetGuid` β†’ `_selectedGuid` on `GameWindow`, unifying -combat + interaction selection), and three switch cases in -`GameWindow.OnInputAction` (for `SelectLeft`, `SelectDblLeft`, -`UseSelected`). `SelectionState` as a class extraction is deferred -to whenever the M2 HUD wants a `SelectionChanged` event; per CLAUDE.md -"don't add abstractions beyond what the task requires." - -`BuildPickUp` (F-key) is out of scope β€” not on the Holtburg inn-door -critical path. Filed as a follow-up. - ---- - -## Why B.4b (and not "fix B.4" or "Phase B.5") - -| Option | Verdict | -|---|---| -| **Reopen "Phase B.4" and amend** | Rejected. B.4 is in memory + commit history as shipped 2026-04-28; reopening creates retroactive confusion. The gap is real; promote it to its own short-lived sub-phase per the L.2/L.2g precedent. | -| **Roll into M2 interaction work** | Rejected. M2 is creatures + combat + a real selection HUD β€” weeks of work. The doors-open scenario is M1. Need a small slice that unblocks the M1 visual test without dragging M2 forward. | -| **New phase "B.4b β€” Outbound Use handler wiring"** | **Selected.** Mirrors the L.2d β†’ L.2g lettered-sub-phase pattern. Phase-sized (30-50 LOC was the initial estimate; final is closer to ~80 with the picker file), commit-trackable, closeable as soon as the visual test passes. | - ---- - -## Problem evidence - -Discovered 2026-05-12 while running the L.2g slice 1 visual test. The -input dispatcher correctly fires `SelectDblLeft` on every double-left- -click β€” the diagnostic `[input] SelectDblLeft Press` line shows in the -log β€” but `GameWindow.OnInputAction`'s switch has zero `case -InputAction.SelectLeft / SelectDblLeft / UseSelected` branches. -Nothing downstream listens. The click silently dies. - -From `GameWindow.cs:8546-8646` (the full `OnInputAction` switch as of -this morning's L.2g merge): - -``` -switch (action) -{ - case InputAction.AcdreamToggleDebugPanel: ... - case InputAction.AcdreamToggleCollisionWires: ... - case InputAction.AcdreamDumpNearby: ... - case InputAction.AcdreamCycleTimeOfDay: ... - // ... 12 other Acdream*/Combat*/Toggle* cases ... - case InputAction.SelectionClosestMonster: - SelectClosestCombatTarget(showToast: true); - break; - case InputAction.EscapeKey: ... -} -``` - -`SelectionClosestMonster` (Q-cycle combat target) is the *only* -selection-related case. `SelectLeft` / `SelectDblLeft` / `SelectRight` -/ `UseSelected` / `SelectionPickUp` / all the other Select-family -actions have no cases at all. - -Inbound side (L.2g slice 1) is wired and ready to receive the -server's reply. Outbound is the only block. - ---- - -## Current acdream state - -| Component | State | -|---|---| -| `InteractRequests.BuildUse(seq, guid)` wire builder | shipped at `src/AcDream.Core.Net/Messages/InteractRequests.cs:37` | -| `InteractRequests.BuildUseWithTarget` | shipped at same file:51 | -| `InteractRequests.BuildPickUp` | DOES NOT EXIST (handoff was wrong) | -| `InputAction.SelectLeft` / `SelectDblLeft` / `SelectRight` / `UseSelected` / `SelectionPickUp` | defined in `InputAction` enum | -| KeyBindings: LMB β†’ `SelectLeft`, LMB-dblclick β†’ `SelectDblLeft`, RMB β†’ `SelectRight`, R β†’ `UseSelected`, F β†’ `SelectionPickUp` | wired in `src/AcDream.UI.Abstractions/Input/KeyBindings.cs:303-320, 172, 210` | -| `WorldPicker` class | DOES NOT EXIST (handoff was wrong) | -| `SelectionState` class | DOES NOT EXIST (handoff was wrong) | -| Selection field on GameWindow | exists as `_selectedTargetGuid` but combat-only (used by `SelectClosestCombatTarget` and `ToggleLiveCombatMode`) | -| `WorldSession.NextGameActionSequence()` | shipped; outbound chat/move already uses it | -| `WorldSession.SendGameAction(byte[])` | shipped; outbound chat/move already uses it | -| `OnInputAction` switch case for `Select*` / `UseSelected` | MISSING β€” **the gap** | - ---- - -## Design - -### Architecture - -One new file + edits to `GameWindow.cs`. - -**New:** `src/AcDream.Core/Selection/WorldPicker.cs` β€” static helper -class in `AcDream.Core.Selection` namespace. Two pure methods, no -state, no DI. **Lives in Core** (not App) because it has no App-layer -dependencies: it operates on `WorldEntity` (Core) plus -`System.Numerics` matrices/vectors. Putting it in Core also means it -can be unit-tested via the existing `AcDream.Core.Tests` project; no -new test project required (`AcDream.App.Tests` does not exist as of -2026-05-13 and creating it would add more LOC than the picker -itself). - -**Edited:** `src/AcDream.App/Rendering/GameWindow.cs`: -1. Rename field `_selectedTargetGuid` β†’ `_selectedGuid` (project-wide - find/replace; ~5 call sites all inside `GameWindow.cs`). Unifies - combat + interaction selection on one field. Retail-faithful: AC - has one "current target" not two. -2. Add three switch cases to `OnInputAction`: `SelectLeft`, - `SelectDblLeft`, `UseSelected`. - -`SelectionState` as a separate class is deferred. Reason: only two -consumers today (combat Q-cycle, click handler). The class earns its -keep when consumer #3 (HUD widget that subscribes to -`SelectionChanged`) lands in M2. Premature otherwise. - -### Components - -#### `WorldPicker.BuildRay` - -Standard mouse-to-world unprojection. Convert pixel `(mouseX, mouseY)` -to NDC `(2*mouseX/vpW - 1, 1 - 2*mouseY/vpH)`, unproject the near -point (`ndc.z = -1`) and far point (`ndc.z = +1`) through -`inverse(projection) β†’ inverse(view)`, return `(origin = near, -direction = normalize(far - near))`. - -Signature: -```csharp -public static (Vector3 Origin, Vector3 Direction) BuildRay( - float mouseX, float mouseY, - float viewportW, float viewportH, - Matrix4x4 view, Matrix4x4 projection); -``` - -~20 LOC. Pure math. No exception paths β€” the OpenGL view/proj matrices -we hand it are always invertible. - -#### `WorldPicker.Pick` - -Ray-sphere intersection against each candidate entity's `Position` -with radius 5.0f (matches `WorldEntity.DefaultAabbRadius`). Skip the -self-guid (player). Track the closest hit with `t < maxDistance` (50m -default). Return the picked entity's `ServerGuid`, or `null` for miss. - -Signature: -```csharp -public static uint? Pick( - Vector3 origin, Vector3 direction, - IEnumerable candidates, - uint skipServerGuid, - float maxDistance = 50f); -``` - -~30 LOC. Excludes entities with `ServerGuid == 0` (atlas-tier scenery -+ dat-hydrated statics) β€” those have no server-side identity, so a -`BuildUse` against them would carry guid=0 and be rejected. - -Sphere intersection math (geometric form): for each candidate, compute -`oc = origin - entity.Position`, `b = dot(oc, direction)`, `c = -dot(oc, oc) - rΒ²`, discriminant `d = bΒ² - c`. If `d < 0` no hit. -Otherwise `t = -b - sqrt(d)` is the near intersection; track smallest -positive `t < maxDistance`. - -#### `OnInputAction` switch cases - -Three new cases right before the `EscapeKey` case (preserve the -existing case ordering by feature group): - -```csharp -case InputAction.SelectLeft: - PickAndStoreSelection(useImmediately: false); - break; - -case InputAction.SelectDblLeft: - PickAndStoreSelection(useImmediately: true); - break; - -case InputAction.UseSelected: - UseCurrentSelection(); - break; -``` - -Plus three private helper methods on `GameWindow`: - -- `PickAndStoreSelection(bool useImmediately)`: pull `_lastMouseX/Y`, - `_cameraController.Active.View/Projection`, `_window.Size`; call - `WorldPicker.BuildRay` β†’ `WorldPicker.Pick`; on hit, set - `_selectedGuid = picked`, toast "Selected: {name}", emit diagnostic - `[B.4b] pick guid=0x{picked:X8} name={DescribeLiveEntity(picked)}`. If - `useImmediately`, also call `SendUse(picked)`. On miss, toast - "Nothing to select" (no diagnostic line, no state change). -- `UseCurrentSelection()`: if `_selectedGuid is uint sel`, call - `SendUse(sel)`. Otherwise toast "Nothing selected". -- `SendUse(uint guid)`: gate on `_liveSession?.CurrentState == - InWorld`; `seq = _liveSession.NextGameActionSequence()`; `body = - InteractRequests.BuildUse(seq, guid)`; - `_liveSession.SendGameAction(body)`; diagnostic - `[B.4b] use guid=0x{guid:X8} seq={seq}`. - -All three switch branches honor the existing `if (activation != -ActivationType.Press) return;` filter above the switch. - -### Data flow (happy path) - -``` -mouse double-click on door at pixel (540, 320) - -> Silk.NET window event -InputDispatcher.OnMouseDown -> DoubleClick chord match - -> -InputDispatcher.Fired(SelectDblLeft, Press) - -> -GameWindow.OnInputAction(SelectDblLeft, Press) - -> -PickAndStoreSelection(useImmediately: true) - -> pulls _lastMouseX/Y + _cameraController.Active.View/Projection -WorldPicker.BuildRay(540, 320, vpW, vpH, view, proj) -> (origin, dir) - -> -WorldPicker.Pick(origin, dir, _entitiesByServerGuid.Values, - _playerServerGuid, 50f) -> 0xDoorGuid - -> -_selectedGuid = 0xDoorGuid -toast "Selected: Door" -log "[B.4b] pick guid=0x000F4244 name=Door" - -> -SendUse(0xDoorGuid) - -> -seq = _liveSession.NextGameActionSequence() -body = InteractRequests.BuildUse(seq, 0xDoorGuid) -_liveSession.SendGameAction(body) -log "[B.4b] use guid=0x000F4244 seq=N" - -> ACE processes Use, calls Door.Open() -ACE broadcasts UpdateMotion(NonCombat,On) -> swing animation -ACE broadcasts SetState(guid=0xDoor, state=0x14) - -> -WorldSession.StateUpdated event fires (L.2g slice 1 path) - -> -ShadowObjectRegistry.UpdatePhysicsState(doorGuid, 0x14) - -> next physics tick -CollisionExemption.ShouldSkip returns true -> door no longer blocks - -> -player walks through doorway -``` - -### Error handling / edge cases - -- **No entity hit** (clicked on terrain, sky, empty space): `Pick` - returns `null`. No `_selectedGuid` change. Toast "Nothing to - select". No network send. -- **ImGui consuming the click**: `InputDispatcher` already filters - via `wantCaptureMouse`. `OnInputAction` only fires when the click - was outside ImGui panels. No new guard needed. -- **No live session / not in world**: `SendUse` short-circuits early. - Toast "Not in world" for debug visibility. Picker still runs (cheap; - fine to leave selection state updated even offline). -- **`UseSelected` with no current selection**: toast "Nothing - selected". No network send. -- **Selected entity despawns between select and use**: `BuildUse` - still sends the cached guid. ACE replies with `UseDone` carrying - `WeenieError.InvalidObject` (a non-zero error code). That error - already flows into the chat-log channel via the existing - `GameEventType.UseDone` handler; no new code needed. -- **Ray construction degenerate**: if `direction.LengthSquared() < eps`, - treat as no-hit and return null from `Pick`. Defensive β€” should - never trigger for sane view/proj matrices. -- **Entity at exactly `_selectedGuid` despawns silently while - selected** (e.g. NPC walks out of streaming range): `_selectedGuid` - becomes a stale reference. Acceptable for B.4b β€” the next - `UseSelected` either sends a guid the server now doesn't recognize - (server replies with an error, harmless) or the player picks a new - target before pressing R. Stale-selection cleanup is M2 HUD work. - -### Testing - -**Unit tests** β€” `tests/AcDream.Core.Tests/Selection/WorldPickerTests.cs` -(new file in existing test project): - -| Test | Scenario | Asserts | -|---|---|---| -| `BuildRay_CenterOfViewport_ReturnsForwardRay` | mouse at (vpW/2, vpH/2), identity view, simple perspective proj | direction approx -Z (camera-forward) within eps | -| `BuildRay_OffsetMouse_DeflectsRay` | mouse right-of-center, same camera | direction.X > 0 (deflects toward camera-right) | -| `Pick_RayThroughEntity_ReturnsServerGuid` | synthetic entity at (0,0,-10) with ServerGuid=0xABCD, ray from origin along -Z | returns 0xABCD | -| `Pick_RayMisses_ReturnsNull` | same entity, ray aimed at +X | returns null | -| `Pick_TwoEntitiesInLine_ReturnsCloser` | entities at -5 and -10, ray along -Z | returns the -5 one | -| `Pick_SkipsSkipGuid` | one entity at -10 with guid=0xABCD, skipServerGuid=0xABCD | returns null | -| `Pick_SkipsZeroServerGuid` | entity with ServerGuid=0 (dat-hydrated scenery) in path | returns null | -| `Pick_BeyondMaxDistance_ReturnsNull` | entity at -100, default maxDist=50 | returns null | - -**Switch-case behavior** β€” not unit-tested. Would require mocking -`GameWindow` + `WorldSession` + `InputDispatcher` + `CameraController`, -high cost low value for a 3-case wiring change. Verified at runtime via -visual test. - -**Runtime verification** β€” Holtburg inn doorway scenario (per the L.2g -slice 1 handoff reproducibility recipe): - -```powershell -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -$env:ACDREAM_TEST_HOST = "127.0.0.1" -$env:ACDREAM_TEST_PORT = "9000" -$env:ACDREAM_TEST_USER = "testaccount" -$env:ACDREAM_TEST_PASS = "testpassword" -$env:ACDREAM_DEVTOOLS = "1" -$env:ACDREAM_PROBE_BUILDING = "1" -$env:ACDREAM_PROBE_RESOLVE = "1" -dotnet run --project src\AcDream.App\AcDream.App.csproj -c Debug 2>&1 | - Tee-Object -FilePath "launch-b4b.log" -``` - -Then in-client: walk to the Holtburg inn doorway, double-left-click -the closed door, wait for swing animation, walk through. After 30s, -watch auto-close. - -Expected log grep: - -```powershell -Select-String -Path launch-b4b.log -Pattern ` - "B.4b|setstate-hex|setstate.*guid|input.*SelectDblLeft|entity-source.*Door" -``` - -Expected matches: -- `[input] SelectDblLeft Press` (dispatcher fires β€” already worked pre-B.4b) -- **NEW:** `[B.4b] use guid=0x000F4244 seq=N` (B.4b send fires) -- `[setstate-hex] body.len=16 ...` (server replied β€” L.2g hex probe) -- `[setstate] guid=0x000F4244 state=0x00000014` (door opens β€” L.2g - per-tick probe) β€” **NB:** if state is `0x4` only (not `0x14`), - follow the L.2g slice-1 review's "Important note" β†’ file a tiny - L.2g slice 1b to widen `CollisionExemption.ShouldSkip`. -- `[setstate] guid=0x000F4244 state=0x00000000` ~30s later (auto-close). -- Player visibly walks through doorway during the open window. - -### Slice plan - -This is one slice. No further sub-slicing. - -| Step | Files | LOC | Subagent? | -|---|---|---|---| -| 1. Write `WorldPickerTests.cs` (TDD: tests first) | `tests/AcDream.Core.Tests/Selection/WorldPickerTests.cs` (new) | ~80 | Yes (Sonnet) β€” bounded TDD task | -| 2. Create `WorldPicker.cs` static helper | `src/AcDream.Core/Selection/WorldPicker.cs` (new) | ~50 | Same agent as step 1 | -| 3. Rename `_selectedTargetGuid` β†’ `_selectedGuid` in `GameWindow.cs` | 1 file edit | ~5 sites | Manual or Sonnet | -| 4. Add 3 switch cases + 3 helper methods in `GameWindow.OnInputAction` | 1 file edit | ~40 | Manual or Sonnet | -| 5. `dotnet build` + `dotnet test` green | β€” | β€” | Manual | -| 6. Visual test at Holtburg inn doorway + log grep | β€” | β€” | Manual (user) | -| 7. Commit + close #57 + update roadmap + update memory | β€” | β€” | Manual | - -Total: ~80 LOC new code + ~80 LOC tests + ~50 LOC edits. One commit -(or two: picker + test as one, handler wiring + rename as another). - -### Acceptance criteria - -- [ ] `dotnet build` green -- [ ] `dotnet test` green; 8 new `WorldPickerTests` pass -- [ ] Double-left-click on closed door in Holtburg inn doorway: - - [ ] Log shows `[B.4b] pick guid=0x... name=Door` - - [ ] Log shows `[B.4b] use guid=0x... seq=N` - - [ ] Log shows `[setstate] guid=0x... state=0x14` (or `0x4`) shortly after - - [ ] Door swings open visually (animation plays) - - [ ] Player can walk through threshold (no `RESOLVE`-line wall hits) -- [ ] R hotkey with no selection: toast "Nothing selected", no send. -- [ ] R hotkey after selecting a door (single click) but not using it - (no double-click): sends `BuildUse` for the same guid. -- [ ] Single left-click on terrain (or sky): toast "Nothing to select", - no send. -- [ ] Q-cycle (combat closest-target) still works after the - `_selectedTargetGuid` β†’ `_selectedGuid` rename. -- [ ] ISSUES.md #57 moved to "Recently closed" with this commit's SHA. -- [ ] Roadmap "shipped" table updated. -- [ ] CLAUDE.md "Currently in Phase L.2" paragraph updated to reflect - L.2g slice 1 + B.4b verified, next phase candidate is the next - preference-order item from the candidate list. - -### Non-goals / explicitly deferred - -- **`BuildPickUp` (F-key pickup)** β€” `InteractRequests` doesn't have - this builder yet. Out of M1 critical path; file as a follow-up note. -- **`UseWithTarget`** β€” wire builder exists but no client-side UX yet - (cursor-on-item then click-on-target). M2 work. -- **`SelectionState` as a class with `SelectionChanged` event** β€” wait - for HUD consumer in M2. -- **Hover-highlight / cursor change on hover** β€” UX polish, M2/M3. -- **Right-click `SelectRight` radial menu** β€” M3. -- **Selected-entity HUD widget (name, vitals)** β€” M2. -- **Stale-selection auto-clear when target despawns** β€” M2 HUD work. -- **Mesh-accurate picking (vs. 5m sphere)** β€” optimization for later; - the 5m sphere is the retail "fast bbox" first pass, which retail - followed with a per-triangle test on the candidate. Add only if a - visual-test session reports a wrong-entity pick. - -### Risks / open questions - -| Risk | Mitigation | -|---|---| -| **5m sphere too generous at doorways** β€” picks the wall or NPC inside the inn instead of the door | First visual test pass settles it. If it picks the wrong entity, tighten the radius to 3m or add a closer-than-furthest tiebreak by entity type. | -| **Camera-mode mismatch** β€” in fly/orbit mode the ray origin should be the camera position, not the player. | Resolved by using `_cameraController.Active.View` which is the camera's view matrix regardless of mode. The picker doesn't care about player position. | -| **State value `0x4` vs `0x14`** β€” L.2g slice-1 review flagged that `CollisionExemption.ShouldSkip` requires both `ETHEREAL (0x4)` AND `IGNORE_COLLISIONS (0x10)`. If ACE sends only `0x4`, the exemption won't fire. | Settled by the same visual test's `[setstate]` log line. If `0x4` only, file a tiny L.2g slice 1b to widen the check; that's a one-line edit and out of B.4b scope. | -| **`_lastMouseX/Y` at click time vs. dispatcher-fire time** β€” if there's a frame of latency between Silk's mouse-down event and the dispatcher fire, the mouse may have moved. | Silk fires mouse-down synchronously; `_lastMouseX/Y` are updated on every move, so they hold the click position at the moment the dispatcher fires. Verified by reading the existing `OnMouseDown` path. Low risk. | -| **Entity not in `_entitiesByServerGuid` despite being visible** β€” e.g. dat-hydrated EnvCell statics have `ServerGuid=0` and won't be pickable | Acceptable for B.4b. Doors and NPCs in Holtburg are server-spawned with non-zero `ServerGuid`. Dat-hydrated statics (fireplaces, decorations) aren't meant to be Use-able. | - -### Open question after slice ships (L.2g slice 1b) - -The L.2g slice-1 final-review "Important note" β€” does ACE's -`PhysicsObj.cs:787-791` set both `ETHEREAL_PS (0x4)` AND -`IGNORE_COLLISIONS_PS (0x10)` simultaneously when doors open, or only -ETHEREAL? B.4b's visual test settles this. If the hex shows `0x4` -alone, file L.2g slice 1b to either widen `CollisionExemption.ShouldSkip` -to `((state & ETHEREAL_PS) != 0)` alone, or set both bits in -`UpdatePhysicsState`. Decision deferred until evidence lands. - ---- - -## Reproducibility - -Same launch recipe as L.2g slice 1 (above). Visual verification is the -same scenario β€” both L.2g slice 1 and B.4b verified together. No -separate L.2g visual test session needed. - ---- - -## Worktree - -Branch: `claude/compassionate-wilson-23ff99`, worktree -`compassionate-wilson-23ff99`. Clean off main (commit `eea9b4d` = -the L.2g slice 1 merge from the previous session). - -After ship: merge to main, close #57, update CLAUDE.md + roadmap + -memory, archive this spec + the impl plan. diff --git a/docs/superpowers/specs/2026-05-13-phase-b4c-design.md b/docs/superpowers/specs/2026-05-13-phase-b4c-design.md deleted file mode 100644 index 0e7d07b..0000000 --- a/docs/superpowers/specs/2026-05-13-phase-b4c-design.md +++ /dev/null @@ -1,492 +0,0 @@ -# Phase B.4c β€” Door Swing Animation - -**Status:** Design spec, created 2026-05-13 evening after B.4b ship. -**Branch:** `claude/phase-b4c-door-anim` (worktree `phase-b4c-door-anim`). -**Predecessors:** -- [docs/research/2026-05-13-b4b-shipped-handoff.md](../../research/2026-05-13-b4b-shipped-handoff.md) - β€” B.4b shipped end-to-end interaction; door becomes ethereal + passable on - Use, but doesn't visually swing. -- [docs/ISSUES.md](../../ISSUES.md) #58 β€” door swing animation `UpdateMotion` - routing for non-creature entities, filed during B.4b's Task 6. -- [docs/superpowers/specs/2026-05-12-l2g-dynamic-physicsstate-design.md](2026-05-12-l2g-dynamic-physicsstate-design.md) - β€” L.2g spec's "Wire flow" section Β§1 documents that ACE's `Door.ActOnUse` - broadcasts BOTH `EnqueueBroadcastMotion(motionOpen)` (this spec's target) - AND `EnqueueBroadcastPhysicsState()` (handled by L.2g slice 1+1c). - -**Milestone:** M1 β€” Walkable + clickable world. Polish on the *"open the -inn door"* demo target. The door is already passable post-B.4b; B.4c -adds the visible swing animation that confirms the open/close state to -the player. - -**Estimate:** ~30-50 LOC, 1 commit, ~2 hours implementation including -visual verification. - -**Scope chosen 2026-05-13 (brainstorm):** doors only. Generalizing the -spawn-time registration gate to admit all non-creature interactives -(chests, levers, traps, statues) is filed separately as future work; the -B.4c fix is door-specific, narrowly scoped to the M1 demo target. - ---- - -## TL;DR - -ACE's `Door.ActOnUse` broadcasts two packets when the player Uses a door: - -1. `UpdateMotion (~0xF74D)` with stance `NonCombat` and command - `MotionOpen` β€” the swing-open animation cycle. -2. `SetState (0xF74B)` with `Ethereal` bit set β€” the collision-bit flip - handled by L.2g slice 1 + 1c. - -acdream's `OnLiveMotionUpdated` handler at `GameWindow.cs:3019` early-outs -at line 3023 when the entity isn't in `_animatedEntities`. Doors are -**not registered** in `_animatedEntities` because the spawn-time gate at -`GameWindow.cs:2692` requires `idleCycle != null && idleCycle.Framerate -!= 0f && idleCycle.HighFrame > idleCycle.LowFrame && -idleCycle.Animation.PartFrames.Count > 1`. Doors don't have a multi-frame -idle cycle (their natural state is the static closed pose), so they fail -all four sub-checks and the registration silently drops. - -B.4c adds a Door-specific spawn-time branch that bypasses the -multi-frame-idle gate. Door entities get a sequencer + `AnimatedEntity` -registration so the existing UM handler routes naturally to them. No -changes to `OnLiveMotionUpdated`, `AnimationSequencer`, -`EntitySpawnAdapter`, or the per-frame animation tick β€” the rest of the -chain already works generically over `(stance, command)` pairs. - ---- - -## Why B.4c (and not "fix the registration gate generally") - -| Option | Verdict | -|---|---| -| **Generalize: relax the multi-frame-idle gate for all non-creature entities with a MotionTable** | Rejected (for B.4c). Closes the bug class for chests, levers, traps, statues in one shot β€” but every non-creature with a sequencer would tick every frame, even when nothing is animating. Bigger risk surface, slower visual-verification cycle. The retail-fidelity cost is also higher: we'd be admitting many entities into a path designed for creatures. | -| **Door-specific lazy registration on first UpdateMotion** | Rejected. Avoids the spawn-time gate question but adds complexity to the hot UM handler; double-allocations possible if multiple UMs race. Net more code than the spawn-time fix, with worse locality. | -| **Door-specific bespoke `DoorAnimationState` outside `AnimationSequencer`** | Rejected unless A fails. Cleaner conceptual separation but duplicates MotionTable cycle-key resolution + per-frame frame-tick logic. Worth pivoting to if approach A reveals that the sequencer drives doors poorly (loops a one-shot cycle, etc.). | -| **Door-specific spawn-time gate bypass** | **Selected.** Smallest change, reuses everything. One block edit at `GameWindow.cs:2692`. If the sequencer doesn't drive doors well at runtime, falls back to the bespoke approach without losing existing work. | - ---- - -## Problem evidence - -From the B.4b visual test 2026-05-13 (per the user-confirmed shipped -handoff): double-click on the Holtburg inn door at server guid -`0x7A9B4015` (entity Id `0x000F4245`) sends a `BuildUse`, ACE replies -with both `UpdateMotion (NonCombat, On)` AND `SetState (state=0x0001000C -= HasPhysicsBSP | Ethereal | ReportCollisions)`, the L.2g chain mutates -the cached state, the door becomes passable. **No visible animation -plays** β€” the door's mesh sits at its closed pose throughout the open -window, then sits at the same closed pose throughout the closed window. - -Code path trace: - -- `WorldSession` parses inbound `0xF74D` β†’ fires `MotionUpdated` event - carrying `EntityMotionUpdate { Guid, MotionState }`. -- `GameWindow.OnLiveMotionUpdated` (line 3019) handles the event: - ```csharp - if (_dats is null) return; - if (!_entitiesByServerGuid.TryGetValue(update.Guid, out var entity)) return; - if (!_animatedEntities.TryGetValue(entity.Id, out var ae)) return; // ← door drops out HERE - ``` -- The entity IS in `_entitiesByServerGuid` (B.4b verified the picker hits - it). It's NOT in `_animatedEntities` because the spawn-time - registration gate at `GameWindow.cs:2692` requires: - ```csharp - if (idleCycle is not null && idleCycle.Framerate != 0f - && idleCycle.HighFrame > idleCycle.LowFrame - && idleCycle.Animation.PartFrames.Count > 1) - ``` -- Doors fail at least one of those sub-checks (likely `idleCycle is - null` β€” doors don't have an idle in the conventional sense). - -The renderer continues to draw the door at its spawn-time MeshRefs (the -closed pose) every frame because nothing in the chain rebuilds those -MeshRefs without an `_animatedEntities` entry. - ---- - -## Current acdream state - -| Component | State | -|---|---| -| `WorldSession` parses `0xF74D` UpdateMotion + fires `MotionUpdated` | shipped | -| `GameWindow.OnLiveMotionUpdated` handles the event | shipped, generic over creatures | -| `AnimationSequencer.SetCycle(style, motion, speedMod)` | shipped, generic over `(style, motion)` pairs | -| Per-frame animation tick rebuilds `MeshRefs` from sequencer state | shipped | -| Door entities registered in `_animatedEntities` at spawn | MISSING β€” fails gate at line 2692 | -| Door's `Setup.DefaultMotionTable` resolved + sequencer built | conditional β€” only happens via the creature branch which doors fall through | - ---- - -## Design - -### Architecture - -One block-level edit to `GameWindow.cs`'s live-spawn animation -registration. Around line 2692 (the existing creature gate), add a -sibling branch that detects Door entities and registers them with a -sequencer regardless of idle-cycle quality. - -``` -existing line 2681-2688: increment _liveAnimReject* counters -existing line 2692-2788: if (idleCycle qualifies) { build sequencer + register } -NEW after line 2788: else if (IsDoorSpawn(spawn) && setup has motion table) - { build sequencer + register } -``` - -The new branch reuses the same sequencer construction pattern from -lines 2704-2768 (load motion table, build sequencer). What's different: - -- No idle-cycle gating: doors don't have an idle cycle. -- **Sequencer is seeded with an initial cycle derived from spawn - `PhysicsState`.** ACE's `Door.cs:43` sets `CurrentMotionState = - motionClosed` at construction; we mirror this β€” at spawn, if the - door's spawn-time state has `ETHEREAL_PS (0x4)` set the door is - "open" (initial cycle = `MotionCommand.On = 0x4000000B`), otherwise - it's "closed" (initial cycle = `MotionCommand.Off = 0x4000000C`). - Without this seed, the sequencer's `Advance(dt)` returns no frames, - the per-frame MeshRefs rebuild at `GameWindow.cs:7691-7697` produces - all-parts-at-origin transforms, and the door visually collapses. -- The `AnimatedEntity` is registered with `Animation = null` β€” the - per-frame tick at line 7497 branches into the sequencer path - (`if (ae.Sequencer is not null)`), reads frames via - `ae.Sequencer.Advance(dt)`, and never touches `ae.Animation` in the - sequencer branch (verified by code reading: only the `else` legacy - slerp branch at line 7644+ reads `ae.Animation.PartFrames`). - -The seed approach matches the existing creature-spawn pattern at -lines 2714-2771 which also calls `sequencer.SetCycle(seqStyle, -spawnCycle)` at spawn to put the sequencer in a known state. - -### Components - -#### `IsDoorSpawn(spawn)` β€” Door detection helper - -```csharp -private static bool IsDoorSpawn(LiveSpawnRecord spawn) - => spawn.Name == "Door"; -``` - -Detection by server-sent name string. Cheap, exact, no dependency on -Setup ID enumeration. The string comes through `CreateObject` parsing -already populated; verified live in B.4b log as `name="Door"` for the -Holtburg inn doorway entities. - -If ACE ever localizes "Door" or sends a different name (e.g. "Iron -Gate", "Portcullis"), those entities silently won't animate β€” that's -the same fallback as today and is acceptable per the spec's "doors only" -scope. Future generalization can replace the heuristic. - -#### Spawn-time door registration branch (new, ~40 LOC) - -Inserted after the existing `if (idleCycle is not null && idleCycle.Framerate != 0f && ...)` block. Body: - -```csharp -else if (IsDoorSpawn(spawn) && _animLoader is not null) -{ - uint mtableId = spawn.MotionTableId ?? (uint)setup.DefaultMotionTable; - if (mtableId != 0) - { - var mtable = _dats.Get(mtableId); - if (mtable is not null) - { - var sequencer = new AcDream.Core.Physics.AnimationSequencer(setup, mtable, _animLoader); - - // Seed initial cycle from spawn PhysicsState. ACE's Door.cs:43 - // sets CurrentMotionState = motionClosed at construction; we - // mirror the same convention so the per-frame tick has frames - // to advance from frame 1, before any UpdateMotion arrives. - // - // ETHEREAL bit (0x4) set on the wire == door is open at spawn - // (rare β€” happens when the door was already open in ACE's DB). - const uint NonCombatStance = 0x80000001u; - const uint MotionOn = 0x4000000Bu; // door open - const uint MotionOff = 0x4000000Cu; // door closed - const uint EtherealPs = 0x4u; - uint spawnState = (uint)(spawn.PhysicsState ?? 0); - uint initialCycle = (spawnState & EtherealPs) != 0 ? MotionOn : MotionOff; - if (sequencer.HasCycle(NonCombatStance, initialCycle)) - sequencer.SetCycle(NonCombatStance, initialCycle); - - // Snapshot per-part identity (same as the creature branch). - var template = new (uint, IReadOnlyDictionary?)[meshRefs.Count]; - for (int i = 0; i < meshRefs.Count; i++) - template[i] = (meshRefs[i].GfxObjId, meshRefs[i].SurfaceOverrides); - - _animatedEntities[entity.Id] = new AnimatedEntity - { - Entity = entity, - Setup = setup, - Animation = null, // sequencer-driven; tick reads sequencer state, not ae.Animation - LowFrame = 0, - HighFrame = 0, - Framerate = 0f, - Scale = scale, - PartTemplate = template, - CurrFrame = 0, - Sequencer = sequencer, - }; - - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeBuildingEnabled) - Console.WriteLine(System.FormattableString.Invariant( - $"[door-anim] registered guid=0x{spawn.Guid:X8} entityId=0x{entity.Id:X8} mtable=0x{mtableId:X8} initialCycle=0x{initialCycle:X8}")); - } - } -} -``` - -The four constants (`NonCombatStance`, `MotionOn`, `MotionOff`, `EtherealPs`) -are inline because they're touch-points for this phase only and acdream's -`MotionInterpreter.cs` doesn't yet declare `On`/`Off`. If a follow-up phase -broadens the registration to chests/levers/traps, lift them into a shared -constants class. - -Same `_animLoader` and `_dats` already in scope. No new fields. No new -file. Skips the `_liveAnimReject*` counters because doors aren't -"rejected" β€” they're admitted via a sibling branch. - -#### Diagnostic on UM dispatch (small additive, ~5 LOC) - -Inside `OnLiveMotionUpdated`, gated on -`PhysicsDiagnostics.ProbeBuildingEnabled` AND the entity is a Door, -emit: - -```csharp -if (PhysicsDiagnostics.ProbeBuildingEnabled - && _liveEntityInfoByGuid.TryGetValue(update.Guid, out var liveInfo) - && liveInfo.Name == "Door") -{ - Console.WriteLine(System.FormattableString.Invariant( - $"[door-cycle] guid=0x{update.Guid:X8} stance=0x{update.MotionState.Stance:X4} cmd=0x{(update.MotionState.ForwardCommand ?? 0u):X4}")); -} -``` - -Inserted alongside the existing `[UM_RAW]` and `ACDREAM_DUMP_MOTION` -diagnostics in the same handler. `_liveEntityInfoByGuid` already carries -the server-sent name (used elsewhere in `DescribeLiveEntity` per the B.4b -code). - -**Diagnostic tag choice.** Use `[door-anim]` (registration) and -`[door-cycle]` (UM dispatch) rather than the phase-named `[B.4c]`. The -Opus reviewer flagged phase-tagged diagnostics as rotting from B.4b's -review β€” durable subsystem-named tags survive phase archival and grep -cleanly long after B.4c is closed. - -### Data flow - -``` -[Spawn] -ACE CreateObject for inn door - β†’ live-spawn handler resolves setup, meshRefs, scale, spawn.PhysicsState - β†’ idleCycle resolves to null (doors have no idle cycle) - β†’ existing gate at line 2692 fails β†’ _liveAnimRejectNoCycle++ - β†’ NEW gate: IsDoorSpawn(spawn) β†’ true - β†’ mtableId = setup.DefaultMotionTable (door motion table id) - β†’ mtable loaded from dats - β†’ AnimationSequencer constructed - β†’ initialCycle = (spawnState & 0x4 /* ETHEREAL */) != 0 ? On (0x4000000B) : Off (0x4000000C) - β†’ sequencer.SetCycle(NonCombat 0x80000001, initialCycle) - β†’ _animatedEntities[entity.Id] = AnimatedEntity { Sequencer, Animation=null } - β†’ log [door-anim] registered guid=0x... initialCycle=0x... - β†’ per-frame tick advances the Off cycle, sequencer rests at last frame (closed pose) - β†’ renderer draws door at closed-pose transforms from sequencer - -[Player Use] -B.4b chain: double-click β†’ BuildUse β†’ ACE Door.ActOnUse - β†’ ACE broadcasts UpdateMotion(NonCombat, On) where On = 0x4000000B - β†’ WorldSession parses β†’ MotionUpdated event - β†’ OnLiveMotionUpdated: - _entitiesByServerGuid lookup β†’ entity (id=0x000F4245) - _animatedEntities[entity.Id] β†’ ae (with seeded sequencer) - log [door-cycle] guid=0x... stance=0x0001 cmd=0x000B - ae.Sequencer.SetCycle(0x80000001, 0x4000000B, 1f) - β†’ Sequencer transitions from Off cycle β†’ On cycle (one-shot via motion-table link) - β†’ per-frame tick reads sequencer transforms β†’ door's part transforms update - β†’ renderer rebuilds MeshRefs from updated transforms each frame - β†’ user sees door swinging open - β†’ cycle ends, sequencer rests at the open-pose final frame - β†’ renderer draws door at open pose - β†’ (parallel) ACE broadcasts SetState(0x0001000C) β†’ L.2g chain β†’ collision exempts - -[Auto-close 30s later] -ACE broadcasts UpdateMotion(NonCombat, Off 0x4000000C) + SetState(0x00010008) - β†’ same UM path, sequencer transitions On β†’ Off (close cycle) - β†’ cycle ends, sequencer rests at closed-pose final frame - β†’ renderer draws door at closed pose - β†’ (parallel) collision blocks again -``` - -### Error handling - -- **Door has no MotionTable** (`setup.DefaultMotionTable == 0` AND - `spawn.MotionTableId == null`): the new branch's inner `if (mtableId - != 0)` fails. Door not registered. Same as today; no animation, no - regression. Should not happen in practice β€” retail doors all have - motion tables. -- **MotionTable doesn't contain the requested `MotionOpen` cycle**: the - existing `HasCycle` fallback at lines 2742-2768 walks through `RunForward - β†’ WalkForward β†’ Ready`. For doors that's wrong (no Ready cycle). The - NEW door branch doesn't run that fallback β€” it just doesn't call - `SetCycle` at spawn. At runtime if `OnLiveMotionUpdated` calls - `SetCycle(MotionOpen)` and the table doesn't have it, the sequencer's - internal `HasCycle` check fails and the cycle is silently not played. - The door stays at its current pose. Acceptable for B.4c β€” if Holtburg's - doors are missing cycles in the dat, that's a dat-content issue not a - client bug. -- **`_animLoader` is null** (test / headless mode): the NEW branch's - outer `_animLoader is not null` check skips registration. Door stays - static. Tests don't exercise the live-spawn path anyway. -- **`spawn.Name != "Door"` for an actual door** (ACE override, - localization): door silently doesn't animate. M1 demo is at Holtburg - English server; safe enough. Future generalization (e.g. detect by - Setup ID 0x020019FF) is trivial if needed. -- **UM arrives before spawn**: existing handler returns at line 3023 - (`!_animatedEntities.TryGetValue β†’ return`). No change needed. -- **Sequencer plays the cycle as cyclic instead of one-shot**: if - observed in visual test, file as a follow-up to investigate the - motion-table cycle's flags. Pivot to bespoke `DoorAnimationState` - (Approach C) only if the sequencer can't be coaxed into one-shot - behavior. -- **Sequencer with no current motion produces no frames β†’ door - collapses visually**: avoided by seeding the sequencer at spawn with - the state-derived initial cycle (Off if closed, On if already open). - Without this seed, the per-frame tick's MeshRefs rebuild at - `GameWindow.cs:7691-7697` writes all-parts-at-origin transforms over - the entity's spawn-time MeshRefs. -- **`Animation = null` in the AnimatedEntity record breaks per-frame - tick**: the sequencer branch at `GameWindow.cs:7497` reads frames - via `ae.Sequencer.Advance(dt)` and never touches `ae.Animation`. The - only Animation reads are in the legacy slerp `else` branch at line - 7644+, reached only when `ae.Sequencer is null`. Safe by code reading. - -### Testing - -**No new unit tests.** The change is GameWindow integration code, -verified at runtime per the project's existing precedent (B.4b's switch -cases, L.2g's MotionUpdated routing). - -**Runtime verification** at Holtburg inn doorway (same recipe as L.2g -slice 1 + B.4b ship handoff): - -```powershell -$env:ACDREAM_DAT_DIR = "$env:USERPROFILE\Documents\Asheron's Call" -$env:ACDREAM_LIVE = "1" -$env:ACDREAM_TEST_HOST = "127.0.0.1" -$env:ACDREAM_TEST_PORT = "9000" -$env:ACDREAM_TEST_USER = "testaccount" -$env:ACDREAM_TEST_PASS = "testpassword" -$env:ACDREAM_DEVTOOLS = "1" -$env:ACDREAM_PROBE_BUILDING = "1" -$env:ACDREAM_DUMP_MOTION = "1" -dotnet run --project src\AcDream.App\AcDream.App.csproj -c Debug 2>&1 | - Tee-Object -FilePath "launch-b4c.log" -``` - -In-client: - -1. Wait ~8s for spawn at Holtburg. -2. Walk to the inn doorway. -3. Confirm visual: door at closed pose. -4. Double-click the door. -5. Confirm visual: door swings open over a fraction of a second. -6. Walk through (already verified by L.2g + B.4b). -7. Wait ~30s in the inn. -8. Confirm visual: door swings closed. -9. Bump the closed door β€” confirm it blocks again (collision restored). - -Log grep: - -```powershell -Select-String -Path launch-b4c.log -Pattern "door-anim|door-cycle|UM guid=.*Door|setstate.*0x7A9B4015" -``` - -Expected: -- `[door-anim] registered guid=0x... initialCycle=0x4000000C` (one per closed door at world load) -- `UM guid=0x7A9B4015 mt=... stance=0x0001 cmd=0x000B` (existing UM dump on Use) -- `[door-cycle] guid=0x7A9B4015 stance=0x0001 cmd=0x000B` (NEW; cmd=On) -- `[setstate] guid=0x7A9B4015 entityId=0x000F4245 state=0x0001000C` (L.2g chain) -- ~30s gap -- `[door-cycle] guid=0x7A9B4015 stance=0x0001 cmd=0x000C` (NEW; cmd=Off) -- `[setstate] guid=0x7A9B4015 entityId=0x000F4245 state=0x00010008` (close) - -### Slice plan - -This is one slice. No further sub-slicing. - -| Step | Files | LOC | Notes | -|---|---|---|---| -| 1. Add `IsDoorSpawn` helper | `GameWindow.cs` | ~3 | Static private | -| 2. Add Door registration branch in spawn handler with state-seeded SetCycle | `GameWindow.cs` | ~40 | After existing creature gate; seeds Off/On from spawn.PhysicsState | -| 3. Add `[door-cycle]` diagnostic in `OnLiveMotionUpdated` | `GameWindow.cs` | ~5 | Gated on probe + name check via `_liveEntityInfoByGuid` | -| 4. `dotnet build` + `dotnet test` green | β€” | β€” | 1046 / 8 baseline expected | -| 5. Visual test at Holtburg inn doorway | β€” | β€” | Manual (user) | -| 6. Commit + ship handoff + close #58 + roadmap update | β€” | β€” | Same Task 6 pattern as B.4b | -| 7. Merge to main | β€” | β€” | After final review | - -Total: ~38 LOC in one file. One implementation commit + one docs commit. - -### Acceptance criteria - -- [ ] `dotnet build` green -- [ ] `dotnet test` green (1046 / 8 pre-existing baseline unchanged) -- [ ] At Holtburg, double-click on inn door: - - [ ] Log shows `[door-anim] registered guid=... initialCycle=0x4000000C` for each closed door at world load - - [ ] Log shows `[door-cycle] guid=... stance=0x0001 cmd=0x000B` after the user's double-click - - [ ] Door visibly swings open - - [ ] Player can walk through (already verified; should not regress) - - [ ] Door visibly swings closed ~30s later - - [ ] Log shows a second `[door-cycle] ... cmd=0x000C` for the close motion - - [ ] Closed door blocks collision again (already verified; should not regress) -- [ ] No visible regression in creature animations (NPCs in Holtburg - still walk and emote correctly). -- [ ] ISSUES.md #58 moved to Recently closed. -- [ ] Roadmap "shipped" table updated. -- [ ] CLAUDE.md "Currently in Phase L.2" paragraph updated to reflect - B.4c shipped. - -### Non-goals / explicitly deferred - -- **Generalize the registration gate** for chests, levers, traps, - statues. File as `post-B.4c` if/when those entities show similar - bugs. -- **One-shot vs cyclic playback contract** in `AnimationSequencer`. We - trust the door's motion-table flags to mark `MotionOpen` / `MotionClosed` - as one-shot. If the sequencer loops them, we'll surface that and - decide whether to fix the sequencer or pivot to Approach C. -- **Sound effect on door open** β€” that's wired through a separate - `SoundTable` path. ACE may or may not broadcast the sound. M1 polish - beyond B.4c. -- **Rotating the door's collision shape** to match the visual. The door - becomes ETHEREAL (collision skipped) while open, so the cylinder's - rotation doesn't matter. If a future phase ports retail's - obstruction-ethereal path (issue #60), we may revisit. -- **Door open/close sounds, dust particles, lighting changes** β€” all - M1 polish or post-M1. - -### Risks / open questions - -| Risk | Mitigation | -|---|---| -| **Per-frame tick requires non-null `Animation`** β€” the new branch sets `Animation = null` because the sequencer drives transforms, not the legacy animation pointer. If the tick crashes on null, the door registration crashes the renderer at spawn. | Verify during implementation. If the tick reads `Animation`, gate the tick on `ae.Sequencer != null && ae.Sequencer.CurrentMotion != 0` first. Inline fix during the same task. | -| **Sequencer plays one-shot cycles as cyclic** β€” door swings open, then loops the swing animation forever instead of resting at the open pose. | Visual test catches this immediately. If observed, investigate motion-table flags or pivot to bespoke `DoorAnimationState`. | -| **Multiple doors at same threshold (Holtburg has paired leaves per L.2d trace)** β€” opening one door's animation while the other is closed leaves an asymmetric visual. | Acceptable for B.4c. The player can double-click the second door to open both. If both doors are wired to the same Use target by ACE, both will animate from a single Use. Visual test reveals which. | -| **Door's `setup.DefaultMotionTable` is 0** β€” relies on `spawn.MotionTableId` from CreateObject. If both are 0, no animation. | Defensive code path (the inner `if (mtableId != 0)` skips registration). Door stays static; collision still works. | -| **Diagnostic log volume** β€” `[B.4c] door cycle` fires per UM, which is once per Use. Low volume. Not a concern. | β€” | - ---- - -## Reproducibility - -Same as B.4b's launch recipe. The visual verification scenario reuses -B.4b's "open the inn door" target. No new test character or server -config needed. - ---- - -## Worktree - -Branch: `claude/phase-b4c-door-anim`, worktree -`.claude/worktrees/phase-b4c-door-anim`. Clean off main (commit -`3e08e10` = the B.4b merge from this morning). - -After ship: merge to main, close #58, update CLAUDE.md + roadmap + -memory, archive this spec + the implementation plan. diff --git a/docs/superpowers/specs/2026-05-13-phase-c1.5b-design.md b/docs/superpowers/specs/2026-05-13-phase-c1.5b-design.md deleted file mode 100644 index 1a89654..0000000 --- a/docs/superpowers/specs/2026-05-13-phase-c1.5b-design.md +++ /dev/null @@ -1,522 +0,0 @@ -# Phase C.1.5b β€” issue #56 (per-part collapse) + EnvCell static DefaultScript dispatch - -**Created:** 2026-05-13. -**Author:** Claude (lead engineer/architect). -**Phase:** C.1.5b (second of two slices; C.1.5a portal-PES wiring shipped 2026-05-11 in merge `88bda12`). -**Parent plan:** [`docs/plans/2026-04-27-phase-c1-pes-particles.md`](../../plans/2026-04-27-phase-c1-pes-particles.md) Β§C.1.5. -**Handoff doc:** [`docs/plans/2026-05-12-phase-c1.5b-handoff.md`](../../plans/2026-05-12-phase-c1.5b-handoff.md). - ---- - -## Β§1 Goals - -Two coupled slices in one phase, in this order: - -**Slice A β€” `ParticleHookSink` honors `CreateParticleHook.PartIndex` for static -entities.** Closes [issue #56](../../ISSUES.md). The Holtburg Town network -portal's 10-emitter script currently collapses every emitter to the entity -root, producing a compressed, partially-ground-buried swirl. The fix is to -precompute each Setup part's resting transform at spawn time and apply it to -the hook offset before spawning the particle. - -**Slice B β€” `EntityScriptActivator` fires `Setup.DefaultScript` for -dat-hydrated entities too.** Right now the activator gates on -`entity.ServerGuid != 0`, which means EnvCell static objects (interior -fireplaces, inn decorations, exterior stabs like cottage chimneys) β€” which -have no server guid because they come from the dat file, not the network β€” -never get their DefaultScript fired. Drop the guard, key by `entity.Id` when -`ServerGuid == 0`, and wire `OnCreate` / `OnRemove` calls into GpuWorldState's -dat-hydration paths. - -Plus a **visual confirmation pass** for the animation-hook particle path -(already shipped in C.1; just needs a sanity check by casting a spell on -`+Acdream`). - -### Acceptance - -Visual verification at three retail-side-by-side locations in/near Holtburg: - -1. **Town network portal** (the C.1.5a verification site): swirl extends - vertically through the portal arch with retail-like shape; no - ground-burial; emitters distributed across the portal Setup's parts. -2. **Holtburg Inn fireplace** (interior, EnvCell static): flame particles - match retail's pattern and position over the firebox. -3. **Cottage chimney** (exterior stab β€” TBD which cottage): smoke - particles match retail. -4. **Animation-hook spell cast** on `+Acdream`: cast-anim particle effect - matches retail. - -## Β§2 Scope - -**In:** - -- New helper `AcDream.Core.Meshing.SetupPartTransforms.Compute(Setup)` that - walks `PlacementFrames[Resting]` β†’ fallback `[Default]` β†’ first available - and returns `IReadOnlyList` (one transform per part). -- `ParticleHookSink.SetEntityPartTransforms(uint entityId, IReadOnlyList partTransforms)` - + a backing `_partTransformsByEntity` map cleared by `StopAllForEntity`. -- `ParticleHookSink.SpawnFromHook` applies `partTransforms[partIndex]` to the - hook offset before rotating to world space. -- `EntityScriptActivator` resolver signature changes from - `Func` to `Func` so - both `ScriptId` and `PartTransforms` come from one dat lookup. -- `EntityScriptActivator.OnCreate` keys by `entity.ServerGuid != 0 ? - entity.ServerGuid : entity.Id`. Same activator handles both server-spawned - and dat-hydrated entities β€” no new class. -- `EntityScriptActivator.OnRemove(uint key)` β€” caller picks the key. -- `GpuWorldState` wires the activator into four more places: - `AddLandblock` (dat-hydrated entities only β€” filter by `ServerGuid==0` to - avoid double-firing pending live entities), `AddEntitiesToExistingLandblock` - (the just-promoted entities β€” all dat-hydrated by construction), - `RemoveLandblock` (dat-hydrated entities only), and - `RemoveEntitiesFromLandblock` (dat-hydrated entities only). -- Visual verification at the four sites in Β§1 Acceptance. - -**Out:** - -- Animated entities (NPCs, monsters, the player). Per-part transforms vary - per animation frame and would need a per-tick refresh similar to - `UpdateEntityAnchor`. Deferred to a future phase. The new - `SetEntityPartTransforms` is keyed by entity, so an animated-entity path - can later push fresh transforms each tick without changing the contract. -- Renderer changes. `particle.frag` stays as-is; bindless migration is N.6 - slice 2. -- WB's re-fire-after-1s loop logic β€” portal swirls + fireplace flames are - persistent (`TotalParticles=0 && TotalSeconds=0`), no re-fire needed. -- New emitter types. Reuse existing PES data. - -## Β§3 Background - -### What shipped in C.1.5a (the part we keep) - -The mechanism is correct: `EntityScriptActivator.OnCreate` runs on every -server-spawned `WorldEntity`, resolves `Setup.DefaultScript`, seeds -`_particleSink.SetEntityRotation`, calls `_scriptRunner.Play(scriptId, -entity.ServerGuid, entity.Position)`. Multi-hook scripts dispatch at their -correct `StartTime` offsets. Despawn cleanup works. - -### What's broken (issue #56) - -[`ParticleHookSink.SpawnFromHook`](../../../src/AcDream.Core/Vfx/ParticleHookSink.cs) -at lines 176-217: - -```csharp -var rotation = _rotationByEntity.TryGetValue(entityId, out var rot) - ? rot : Quaternion.Identity; -var anchor = worldPos + Vector3.Transform(offset, rotation); -``` - -The hook author intended `offset` to be in **part-local** space β€” i.e., -relative to the mesh part identified by `cph.PartIndex` β€” so the geometry -retail computes is: - -``` -anchor = entityWorldPos + entityRotation Γ— (partFrame.Origin + partFrame.Orientation Γ— hookOffset) -``` - -Our sink drops the part transform multiplication. For the Holtburg portal -(entity `0x7A9B405B`, script `0x3300126D`, 10 hooks distributed across the -portal Setup's parts), every emitter lands at the entity root. Visible -symptom: swirl partially buried, lateral spread compressed. - -### Where part transforms come from (static entities) - -For static entities, per-part transforms live in -`setup.PlacementFrames[Placement.Resting]` (fallback `[Default]`, fallback -first available β€” same priority chain -[`SetupMesh.Flatten`](../../../src/AcDream.Core/Meshing/SetupMesh.cs) at -lines 36-50 already uses). Per part `i`: - -```csharp -Matrix4x4.CreateScale(setup.DefaultScale[i]) - * Matrix4x4.CreateFromQuaternion(placementFrame.Frames[i].Orientation) - * Matrix4x4.CreateTranslation(placementFrame.Frames[i].Origin) -``` - -`DefaultScale` defaults to `Vector3.One` when the list is shorter than -`Parts.Count`. - -### Where EnvCell statics come from (slice B) - -**Major discovery from this design pass β€” the handoff's Β§4 Q1/Q2 are mooted:** -[`GameWindow.BuildInteriorEntitiesForStreaming`](../../../src/AcDream.App/Rendering/GameWindow.cs) -at lines 5030-5135 already hydrates EnvCell `StaticObjects` as `WorldEntity` -instances with stable `entity.Id` in the `0x40xxxxxx` range: - -```csharp -uint interiorIdBase = 0x40000000u | (landblockId & 0x00FFFF00u); -// ... for each EnvCell, for each stab in envCell.StaticObjects ... -var hydrated = new WorldEntity { - Id = interiorIdBase + localCounter++, - SourceGfxObjOrSetupId = stab.Id, // 0x02000000 β†’ Setup-based - Position = stab.Frame.Origin + lbOffset, - Rotation = stab.Frame.Orientation, - MeshRefs = meshRefs, - ParentCellId = envCellId, -}; -``` - -These flow into `GpuWorldState.AddLandblock` as part of `landblock.Entities`, -sit there with `ServerGuid == 0`, and currently never have their -`Setup.DefaultScript` fired. The activator's existing -`ServerGuid == 0 β†’ return;` guard intentionally skips them (atlas-tier -exemption inherited from `EntitySpawnAdapter`). - -Three architectural consequences: - -1. **No synthetic ID scheme needed.** `entity.Id` is already collision-free - with server guids (live spawns use `0x500000xx`–`0x7Fxxxxxx`), anonymous - emitter IDs (`0x80000000u+`), and the four entity-id ranges - (`0x40xxxxxx` interior / `0x80xxxxxx` scenery / etc) all live in disjoint - high-byte slices. -2. **No new `EnvCellStaticActivator` class.** The existing - `EntityScriptActivator` handles both server-spawned and dat-hydrated - entities once the guard is keyed-by-id-when-zero. -3. **No new walker.** `BuildInteriorEntitiesForStreaming` is the walker β€” - it already happens. We just need the OnCreate fire-site in - GpuWorldState's `AddLandblock` / `AddEntitiesToExistingLandblock`. - -The handoff Β§4 wrote three options (Ξ± piggyback / Ξ² new class / Ξ³ extend -activator) under the assumption that EnvCell statics were NOT WorldEntities. -This reality discovery collapses all three to a simpler answer that none of -them anticipated. - -## Β§4 Architecture - -### Slice A: part-transform pipeline - -``` -EntityScriptActivator.OnCreate(entity) -β”œβ”€ key = entity.ServerGuid != 0 ? entity.ServerGuid : entity.Id -β”œβ”€ info = resolver(entity) // ScriptActivationInfo? {ScriptId, PartTransforms} -β”œβ”€ if (info is null || info.ScriptId == 0) return -β”œβ”€ _particleSink.SetEntityRotation(key, entity.Rotation) -β”œβ”€ _particleSink.SetEntityPartTransforms(key, info.PartTransforms) // NEW -└─ _scriptRunner.Play(info.ScriptId, key, entity.Position) - -ParticleHookSink.SpawnFromHook(entityId, worldPos, ..., partIndex, ...) -β”œβ”€ rotation = _rotationByEntity[entityId] ?? Quaternion.Identity -β”œβ”€ partTransform = (partTransforms != null && partIndex >= 0 && partIndex < Count) -β”‚ ? partTransforms[partIndex] : Matrix4x4.Identity -β”œβ”€ partLocal = Vector3.Transform(offset, partTransform) -β”œβ”€ anchor = worldPos + Vector3.Transform(partLocal, rotation) -└─ _system.SpawnEmitterById(...) -``` - -### Slice B: dat-hydration fire-sites - -``` -GpuWorldState.AddLandblock(landblock) -β”œβ”€ merge pending live entities (existing) -β”œβ”€ _loaded[id] = landblock (existing) -β”œβ”€ _wbSpawnAdapter?.OnLandblockLoaded(...) (existing) -β”œβ”€ foreach entity in landblock.Entities where ServerGuid == 0: // NEW -β”‚ _entityScriptActivator?.OnCreate(entity) -└─ RebuildFlatView() (existing) - -GpuWorldState.AddEntitiesToExistingLandblock(landblockId, entities) -β”œβ”€ canonicalize + merge (existing) -β”œβ”€ _wbSpawnAdapter?.OnLandblockLoaded(...) (existing) -β”œβ”€ foreach entity in entities: // NEW -β”‚ _entityScriptActivator?.OnCreate(entity) // all dat-hydrated -└─ RebuildFlatView() (existing) - -GpuWorldState.RemoveLandblock(landblockId) -β”œβ”€ _wbSpawnAdapter?.OnLandblockUnloaded(...) (existing) -β”œβ”€ rescue persistent (existing) -β”œβ”€ foreach entity in lb.Entities where ServerGuid == 0: // NEW -β”‚ _entityScriptActivator?.OnRemove(entity.Id) -└─ remove from _loaded, RebuildFlatView (existing) - -GpuWorldState.RemoveEntitiesFromLandblock(landblockId) -β”œβ”€ _wbSpawnAdapter?.OnLandblockUnloaded(...) (existing) -β”œβ”€ _onLandblockUnloaded?.Invoke(canonical) (existing β€” Tier 1 cache sweep) -β”œβ”€ foreach entity in lb.Entities where ServerGuid == 0: // NEW -β”‚ _entityScriptActivator?.OnRemove(entity.Id) -└─ replace lb.Entities with empty list, RebuildFlatView (existing) -``` - -The `ServerGuid == 0` filter avoids double-firing OnCreate on live entities -that came via `AppendLiveEntity` and got pending-bucket-merged in -`AddLandblock`. Their OnCreate already fired at AppendLiveEntity time. - -### Resolver evolution - -C.1.5a resolver: - -```csharp -Func defaultScriptResolver // returns scriptId or 0 -``` - -C.1.5b resolver: - -```csharp -Func activationResolver // returns null on miss -``` - -Where `ScriptActivationInfo` is a small record in `AcDream.App.Rendering.Vfx`: - -```csharp -public sealed record ScriptActivationInfo( - uint ScriptId, - IReadOnlyList PartTransforms); -``` - -Production lambda in `GameWindow.OnLoad` (replaces the C.1.5a one): - -```csharp -entity => -{ - try - { - var setup = _dats.Get(entity.SourceGfxObjOrSetupId); - if (setup is null) return null; - uint scriptId = setup.DefaultScript.DataId; - if (scriptId == 0) return null; - var parts = AcDream.Core.Meshing.SetupPartTransforms.Compute(setup); - return new ScriptActivationInfo(scriptId, parts); - } - catch - { - return null; - } -} -``` - -One dat lookup β†’ both pieces of info. The Setup is cached by DatCollection, -so even hot-path scenery firing with no DefaultScript stays O(1). - -### Helper: `SetupPartTransforms.Compute` - -New static helper in `AcDream.Core.Meshing` (next to `SetupMesh`): - -```csharp -public static class SetupPartTransforms -{ - /// - /// Compute the per-part static transforms for a Setup using its - /// PlacementFrames. For each part i, the returned matrix is the - /// transform from part-local to setup-local space at the Setup's - /// resting pose. Mirrors SetupMesh.Flatten's pose-source priority: - /// PlacementFrames[Resting] β†’ [Default] β†’ first available. - /// Returns an empty list when the Setup has no PlacementFrames - /// (caller falls back to "no part transforms applied"). - /// - public static IReadOnlyList Compute(Setup setup); -} -``` - -This deliberately mirrors the pose-source priority in -`SetupMesh.Flatten` so a part's particle anchor matches its visible rest -position. (If the renderer's pose source ever diverges from this resolver, -particles will visibly drift β€” keep them in lockstep.) - -For animated entities, the renderer's `AnimatedEntityState` computes -per-frame part transforms; a future "animated DefaultScript" path would -publish those each tick via the same `SetEntityPartTransforms` seam. Out -of scope for C.1.5b. - -## Β§5 Data + lifecycle invariants - -| Concern | Behavior | -|---|---| -| Server-spawned entity spawn | `AppendLiveEntity` β†’ `OnCreate` (existing). Keys by `ServerGuid`. | -| Server-spawned entity despawn | `RemoveEntityByServerGuid` β†’ `OnRemove(serverGuid)` (existing). | -| Dat-hydrated entity load (initial) | `AddLandblock` β†’ `OnCreate` for each `ServerGuid==0` entity. Keys by `entity.Id`. | -| Dat-hydrated entity load (promotion) | `AddEntitiesToExistingLandblock` β†’ `OnCreate` for each entity in the new batch. Keys by `entity.Id`. | -| Dat-hydrated entity unload (full LB) | `RemoveLandblock` β†’ `OnRemove(entity.Id)` for each `ServerGuid==0` entity. | -| Dat-hydrated entity unload (Nearβ†’Far demotion) | `RemoveEntitiesFromLandblock` β†’ `OnRemove(entity.Id)` for each `ServerGuid==0` entity. | -| Pending live entity merged into AddLandblock | `OnCreate` already fired at `AppendLiveEntity`; filtered out by `ServerGuid != 0`. | -| Persistent live entity rescued from RemoveLandblock | Not unloaded; its script continues. Filtered out by `ServerGuid != 0`. | -| PartIndex out of bounds | Sink falls back to `Matrix4x4.Identity` for that part (no part transform applied, offset stays in entity-local frame as before). | -| Setup with empty PlacementFrames | Resolver returns empty `PartTransforms` list; sink falls back to Identity for every part. Equivalent to pre-C.1.5b behavior. | -| Resolver throws | Lambda's try/catch returns null; activator no-ops. | -| Same script re-fired on dedupe | `PhysicsScriptRunner.Play` replaces prior instance (existing C.1 behavior). Visual: script restarts from t=0. Avoided here because we filter dat-hydrated entities by `ServerGuid==0` β€” they're not double-fired. | - -### Idempotency - -- Duplicate `OnCreate` for same key β†’ script restarts (existing dedupe). -- Duplicate `OnRemove` for same key β†’ no-op. -- `OnRemove` for never-spawned key β†’ no-op. -- LB unload immediately followed by LB load β†’ entities get fresh `entity.Id` - (localCounter resets per-call) but the keys are computed deterministically - from landblockId + iteration order so a re-entered LB gets identical keys. - Script restarts cleanly because OnRemove fired during the unload. - -## Β§6 Testing - -### Unit tests β€” new - -1. **`SetupPartTransforms_ResolvesRestingPlacement_WhenAvailable`** β€” - Setup with `PlacementFrames[Resting]` containing 2 parts; assert returned - list has 2 matrices matching the resting frames. -2. **`SetupPartTransforms_FallsBackToDefault_WhenRestingMissing`** β€” - Setup with only `PlacementFrames[Default]`; assert it's used. -3. **`SetupPartTransforms_ReturnsEmpty_WhenNoPlacementFrames`** β€” - Setup with empty `PlacementFrames` dict; assert empty list. -4. **`SetupPartTransforms_AppliesDefaultScale_WhenPresent`** β€” - Setup with `DefaultScale[0] = (2, 2, 2)`; assert the matrix scales by 2. -5. **`ParticleHookSink_AppliesPartTransform_WhenRegistered`** β€” - register part transforms `[Identity, Translation(0,0,1)]`; fire a - CreateParticleHook with `PartIndex=1, Offset=(1,0,0)`; assert spawned - particle world position is `(1, 0, 1)`. -6. **`ParticleHookSink_FallsBackToIdentity_WhenPartIndexOutOfBounds`** β€” - register 2 part transforms; fire hook with `PartIndex=99`; assert - spawned at root + offset (no buried-by-bad-matrix). -7. **`EntityScriptActivator_KeysByEntityId_WhenServerGuidZero`** β€” - dat-hydrated entity with `ServerGuid=0, Id=0x40A9B401`; fire OnCreate; - assert script runner saw `entityId=0x40A9B401`. -8. **`EntityScriptActivator_PassesPartTransformsToSink`** β€” - resolver returns non-empty PartTransforms; assert sink's - `SetEntityPartTransforms` was called with the matching list. -9. **`EntityScriptActivator_OnRemove_StopsByGivenKey`** β€” - call `OnRemove(0x40A9B401)`; assert runner + sink both got that key. - -### Unit tests β€” updated - -The 4 existing `EntityScriptActivatorTests` are updated for the new -resolver signature (`_ => 0xAAu` β†’ `_ => new ScriptActivationInfo(0xAAu, -Array.Empty())`). Test names and assertions stay the same. - -### Integration tests β€” GpuWorldState wiring - -10. **`GpuWorldState_AddLandblock_FiresActivatorForDatHydrated`** β€” - construct GpuWorldState with a fake activator (recording mock); add - a landblock with one `ServerGuid==0` entity; assert OnCreate fired - exactly once. -11. **`GpuWorldState_AddLandblock_DoesNotDoubleFire_OnPendingMerge`** β€” - AppendLiveEntity with `ServerGuid=0xCAFE` (one OnCreate); then - AddLandblock for the same canonical id; assert OnCreate fired only - once total for the live entity. -12. **`GpuWorldState_RemoveLandblock_FiresOnRemoveForDatHydrated`** β€” - AddLandblock with a dat-hydrated entity, then RemoveLandblock; assert - OnRemove fired with `entity.Id`. -13. **`GpuWorldState_AddEntitiesToExistingLandblock_FiresActivator`** β€” - promotion path; assert OnCreate fires for each promoted entity. -14. **`GpuWorldState_RemoveEntitiesFromLandblock_FiresOnRemove`** β€” - demotion path; assert OnRemove fires for each removed dat-hydrated - entity. - -Existing `GpuWorldStateTests` may need a minor update if any assert on -constructor arity (the resolver doesn't change shape β€” same 4 ctor params). - -### Visual verification (acceptance gate) - -Procedure (per [CLAUDE.md](../../../CLAUDE.md) "Visual verification workflow"): - -1. `dotnet build` green. -2. `dotnet test` green. -3. Launch live client with `ACDREAM_DUMP_PLAYSCRIPT=1`. -4. **Site 1 β€” Holtburg Town network portal** (same site as C.1.5a): - user walks `+Acdream` to the portal arch. Compare swirl vertical - extent + lateral spread to retail. Pass: no ground-burial, distinct - columns of emission visible across the arch. -5. **Site 2 β€” Holtburg Inn fireplace** (interior, EnvCell static): - user walks into the inn, stands near the fireplace. Pass: flame - particles emit from the firebox at retail-matching height/density. -6. **Site 3 β€” Cottage chimney** (exterior stab): user finds a Holtburg - cottage with smoke in retail; same cottage in acdream should now - show smoke. Pass: smoke column matches retail. -7. **Site 4 β€” Spell cast** on `+Acdream`: user casts a spell, optionally - in a safe spot. Pass: cast-anim particles match retail. - -Diagnostic: `ACDREAM_DUMP_PLAYSCRIPT=1` prints every `[pes] Play:` line β€” -if a site doesn't show particles, check the log to see whether the script -fired and with what scriptId. - -## Β§7 Risk + rollback - -**Slice A risks:** - -- `SetupPartTransforms.Compute` returns a list whose length doesn't match - `setup.Parts.Count`. **Mitigation:** sink's per-index bounds check falls - back to Identity; no buried particles, just reverts to C.1.5a behavior - for the over-indexed hook. -- Wrong pose source chosen (Resting vs Default). **Mitigation:** mirror - `SetupMesh.Flatten`'s priority chain exactly so renderer + particle - anchor stay in lockstep. If they ever diverge, particles drift visibly; - user spot-checks at the portal. - -**Slice B risks:** - -- Firing OnCreate for EVERY dat-hydrated entity (scenery counts ~thousands - per landblock at radius=4) becomes a perf hit. **Mitigation:** resolver - is one cached `DatCollection.Get` per entity β€” already amortized. - Most entities have `DefaultScript.DataId == 0`, resolver returns null, - OnCreate no-ops in ~1Β΅s. Per-landblock-load cost: tens of Β΅s, dwarfed - by mesh upload + RebuildFlatView. Measured if `[pes] Play:` line - spam appears in launch.log. -- Filter `ServerGuid==0` is too aggressive β€” misses some valid case. - **Mitigation:** every entity with `ServerGuid != 0` came through - `AppendLiveEntity` (verified by `RelocateEntity`'s - `if (entity.ServerGuid == 0) return;` guard at GpuWorldState.cs:204), - so they already had OnCreate fired. No miss. -- Idempotency edge case: rapid LB load/unload cycles produce repeated - Play β†’ Stop β†’ Play. **Mitigation:** existing PhysicsScriptRunner - dedupe handles re-Play; this is the same as a server retriggering a - PlayScript opcode. - -**Rollback path:** revert the spec's commits; the C.1.5a `EntityScriptActivator` -keeps working for live entities exactly as before. No data migrations. - -## Β§8 Doc-drift fixes from C.1.5a (folded in) - -The handoff Β§9 surfaced three trivial doc-drift items from C.1.5a. Folded -here for the record: - -1. C.1.5a spec Β§4 ("fifth optional parameter") was wrong β€” the activator - is actually GpuWorldState's **fourth** optional parameter (verified at - [GpuWorldState.cs:63](../../../src/AcDream.App/Streaming/GpuWorldState.cs): - `wbSpawnAdapter, wbEntitySpawnAdapter, onLandblockUnloaded, - entityScriptActivator`). -2. C.1.5a spec Β§4 ("~50 lines") was an estimate; the file shipped at - **93 lines** including doc comments. Slice A adds the part-transform - call + slice B drops the `ServerGuid == 0` guard, so the file will - land at ~100–110 lines after this phase. -3. `GpuWorldState.AddEntitiesToExistingLandblock` *will* fire the activator - in slice B (the handoff said it currently doesn't and noted "no-op - today because promotion-tier entities are atlas-tier"). With slice B, - atlas-tier entities WITH `DefaultScript` set will now activate. Per - the architecture comment at GpuWorldState.cs:384-391, this path - handles dat-static stabs/buildings β€” exactly the case slice B targets. - -## Β§9 Implementation notes - -- **File touches:** `ParticleHookSink.cs` (+~30 lines), `EntityScriptActivator.cs` - (+~10 lines, -~5 lines), `GpuWorldState.cs` (+~12 lines, 4 fire-sites), - `GameWindow.cs` (resolver lambda update, ~10 lines), new - `SetupPartTransforms.cs` (~50 lines), updated `EntityScriptActivatorTests.cs` - (4 ctor-signature updates + new tests), new `SetupPartTransformsTests.cs` - (~80 lines, 4 tests), new `ParticleHookSinkTests.cs` additions or new file - (~60 lines, 2 tests), new `GpuWorldStateActivatorTests.cs` (~120 lines, - 5 integration tests). -- **Estimated effort:** ~1 day. -- **Commit cadence:** four commits land this phase cleanly β€” - (1) `SetupPartTransforms` helper + tests, (2) `ParticleHookSink` part-transform - support + tests, (3) `EntityScriptActivator` resolver refactor + ServerGuid - guard relaxation + tests, (4) `GpuWorldState` fire-site wiring + tests + - production lambda update + the C.1.5a doc-drift comment for - `AddEntitiesToExistingLandblock`. Each commit `dotnet test` green. - Visual verification after all four land. -- **Roadmap update:** on ship, add a "Phase C.1.5b SHIPPED 2026-05-13" - entry to [`docs/plans/2026-04-11-roadmap.md`](../../plans/2026-04-11-roadmap.md); - move #56 to "Recently closed" in `docs/ISSUES.md`. -- **CLAUDE.md update:** the "Currently in flight" line at the top of the - project-instructions block changes from C.1.5b to the next phase, with - the handoff doc reference dropped. Decide the next-phase pointer at - verification time. - -## Β§10 What's next (post-C.1.5b) - -Pending user direction. The roadmap candidate list from -[`docs/plans/2026-04-11-roadmap.md`](../../plans/2026-04-11-roadmap.md): - -- Triage the chronic open-issue list β€” #2 (lightning), #4 (sky horizon-glow), - #28 (aurora), #29 (cloud thinness), #37 (humanoid coat), #50 (stray tree), - #41 (remote-motion blips) β€” link each to a future phase or downgrade. -- More Phase C visual-fidelity work (C.2 dynamic point lights, C.3 palette - tuning, C.4 double-sided translucent polys). -- N.6 slice 2 at reduced scope (atlas opportunities only). -- Perf tiers 2/3 only if sustained 500+ FPS becomes a requirement. - -Verification will surface which option the user picks. diff --git a/docs/superpowers/specs/2026-05-14-phase-b6-design.md b/docs/superpowers/specs/2026-05-14-phase-b6-design.md deleted file mode 100644 index 02a5fbc..0000000 --- a/docs/superpowers/specs/2026-05-14-phase-b6-design.md +++ /dev/null @@ -1,422 +0,0 @@ -# Phase B.6 β€” Local-player auto-walk for server-initiated MoveToObject β€” design - -**Date:** 2026-05-14. -**Status:** DESIGN β€” implementation deferred to a dedicated session. -**Closes:** [issue #63](../../ISSUES.md) (server-initiated `MoveToObject` auto-walk not honored). -**Predecessors:** [B.5](../plans/2026-05-14-phase-b5-pickup.md) (ground-item pickup, close-range path) + [B.4b](../plans/2026-05-13-phase-b4b-plan.md) (outbound Use chain). -**References:** -- ACE server-side: [`Player_Move.cs:37–179`](../../../references/ACE/Source/ACE.Server/WorldObjects/Player_Move.cs) (`CreateMoveToChain`, `MoveToChain`, `MoveTo`). -- ACE pickup driver: [`Player_Inventory.cs:976–1106`](../../../references/ACE/Source/ACE.Server/WorldObjects/Player_Inventory.cs). -- Reference port (Rust): [holtburger `simulation.rs:33–41` + `178–191`](../../../references/holtburger/crates/holtburger-core/src/client/simulation.rs). -- Existing acdream infra: [`RemoteMoveToDriver.cs`](../../../src/AcDream.Core/Physics/RemoteMoveToDriver.cs), [`ServerControlledLocomotion.cs`](../../../src/AcDream.Core/Physics/ServerControlledLocomotion.cs). - ---- - -## Problem statement - -When the local player triggers a `Use (0x0036)` or `PutItemInContainer -(0x0019)` on a target outside ACE's `WithinUseRadius` (default 0.6 m), -ACE's `CreateMoveToChain` initiates a **server-side auto-walk** toward -the target. ACE also broadcasts `Motion(MovementType=MoveToObject, -target=X)` via `EnqueueBroadcastMotion`. - -Our client currently does NOT honor this server-initiated motion for -the local player. The visible symptom: the character drifts a short -distance toward the target, then "snaps back" to the original -position. ACE's `MoveToChain` polls `WithinUseRadius` every 0.1 s; if -the player never enters the radius before `defaultMoveToTimeout` fires, -the chain calls `callback(false)` which broadcasts -`InventoryServerSaveFailed (ActionCancelled)` and the pickup / use -never completes. - -**User-facing impact:** -- Double-click on a ground item from any distance β†’ walk + snap, no pickup. -- F-press on a ground item from > 0.6 m β†’ walk + snap, no pickup. -- Use on an out-of-range NPC β†’ same. - -Close-range (≀ 0.6 m) Use and PickUp work correctly via ACE's -early-return branch at `Player_Move.cs:66` (the `WithinUseRadius` -shortcut that skips the chain entirely). - ---- - -## Current state β€” wire data we already have - -`UpdateMotion (0xF74D)` is already parsed end-to-end. The parser -populates `EntityMotionUpdate.MotionState` with: - -| Field | Source | Notes | -|---|---|---| -| `MovementType` | wire byte | `MoveToObject = 6`, `MoveToPosition = 7`. | -| `IsServerControlledMoveTo` | derived | True when `MovementType ∈ {6, 7}`. | -| `MoveToPath` | wire (when set) | `(OriginCellId, OriginX/Y/Z, MinDistance, DistanceToObject)` β€” the auto-walk destination, plus arrival predicates. | -| `MoveToSpeed`, `MoveToRunRate`, `MoveToCanRun`, `MoveTowards` | wire | speed scalars + chase-vs-flee bit. | - -The remote-creature path at -[`GameWindow.cs:3346–3425`](../../../src/AcDream.App/Rendering/GameWindow.cs) -already consumes all of this β€” `_remoteDeadReckon` per-entity state -captures `MoveToDestinationWorld`, `MoveToMinDistance`, -`MoveToDistanceToObject`, `MoveToMoveTowards`, and -`HasMoveToDestination`. The per-tick driver -[`RemoteMoveToDriver`](../../../src/AcDream.Core/Physics/RemoteMoveToDriver.cs) -computes heading + arrival and is exercised on every NPC chase. - -**Gap:** none of this fires for `update.Guid == _playerServerGuid`. The -local-player branch is gated out at: - -1. `GameWindow.cs:3289` β€” `if (update.Guid == _playerServerGuid)` skips - `SetCycle` so `UpdatePlayerAnimation` stays authoritative. -2. `GameWindow.cs:3346` β€” `_remoteDeadReckon.TryGetValue(update.Guid, - …)` returns false because the local player isn't in - `_remoteDeadReckon`. - -The wire is parsed, the destination is in `MotionState.MoveToPath`, and -nothing in our client reads it for the local player. - ---- - -## Why the player visibly walks-then-snaps today - -A. ACE broadcasts `MotionUpdated(MoveToObject)` β†’ our client ignores it - for the local player (above). -B. ACE's server-side `PhysicsObj.MoveToObject(…)` runs its own - simulation. ACE updates the player's authoritative `Location` on its - side every physics tick. -C. ACE periodically broadcasts the player's updated position back to - everyone β€” including the player themselves β€” via `UpdatePosition`. - The local-player's `PositionUpdated` handler in `GameWindow` likely - applies these snapshots, producing the visible forward motion. -D. After `defaultMoveToTimeout` (server-side configurable, typically - on the order of seconds), ACE gives up. `MoveToChain` calls - `callback(false)`. Server-side, ACE may issue a position correction - that returns the player to where the chain started. -E. The local prediction (our `PlayerMovementController` running on - user input β€” which is "no movement keys held") reconciles toward - "stationary at original spot", producing the snap-back when ACE's - own corrections stop arriving. - -The exact source of the visible motion (step C vs. some other path) is -unverified β€” flagged as **investigation work** below. - ---- - -## Solution candidates - -### Option A β€” Run the existing remote-driver for the local player - -**Approach.** When `OnLiveMotionUpdated` sees `IsServerControlledMoveTo` -for `_playerServerGuid`, install the same per-tick steering machinery -that drives remote creatures: heading toward -`MoveToDestinationWorld`, run/walk cycle from -`ServerControlledLocomotion.PlanMoveToStart`, arrival detected via -`min_distance` (chase) / `distance_to_object` (flee). - -While the auto-walk is active, suppress user-input driven movement so -the two control paths don't fight. On arrival or on a non-MoveTo motion -arriving, release auto-walk and restore user input. - -**Pros.** Retail-faithful. Reuses well-tested infrastructure. Aligns -the local player's behavior with how every remote creature already -handles `MoveToObject`. - -**Cons.** Largest implementation surface. Needs careful state-machine -design to prevent input/auto-walk fighting and ensure clean release on -ESC, arrival, packet cancel, target despawn, login transitions. - -### Option B β€” Visual settle to arrival pose - -**Approach.** When MoveToObject arrives, compute the approximate -arrival position via the holtburger -`approximate_move_to_object_projection_target` formula -(`target_pos - normalize(target_pos - source) Γ— (DistanceToObject + -UseRadius)`). Smoothly tween the local-player camera/mesh from current -position to that arrival pose over the expected walk duration. Don't -send any extra packets; rely on ACE's own physics to walk the -authoritative position in parallel. - -**Pros.** Smaller scope (~50 LOC). No state machine. No reconciliation -fight β€” the tween is purely visual; the underlying -`PlayerMovementController` is paused for the duration of the tween. - -**Cons.** Not retail-faithful. Doesn't actually run the player's -physics integrator forward, so any side effects of normal movement -(footstep emit hooks, collision resolution mid-walk, environment -triggers) don't fire. Likely fine for an item pickup (no environment to -interact with mid-walk), but is a band-aid that doesn't generalize. - -### Option C β€” Server-position-authoritative blend - -**Approach.** Detect `IsServerControlledMoveTo` for the local player. -While active, treat inbound `UpdatePosition` as authoritative without -reconciliation β€” i.e. trust the server's interpolated positions and -suppress the local prediction's snap-back. Don't try to drive -local-side; let ACE's broadcasts drag us along. - -**Pros.** Very small surface (~20 LOC). No new state machine; just a -flag that gates reconciliation. - -**Cons.** Depends on ACE actually broadcasting per-tick UpdatePosition -during the auto-walk. The visible "walks then snaps" pattern suggests -it does broadcast for a while and then stops; need to confirm. If -ACE's broadcast cadence is too sparse, the motion is choppy. - ---- - -## Recommendation - -**Option A is the retail-faithful path; Options B and C are non-retail -shortcuts. Implement Option A.** - -### Retail evidence (settles A vs C) - -`MovementManager::PerformMovement` at retail address `0x00524440` -(decomp `docs/research/named-retail/acclient_2013_pseudo_c.txt` lines -300628–300648) is the inbound-motion dispatcher. The switch on movement -type has explicit cases for `MoveToObject (6)` and `MoveToPosition (7)` -that: - -1. Call `MovementManager::MakeMoveToManager(this)` to ensure the - per-physics-object manager exists. -2. Unpack the target guid + origin position + `MovementParameters` from - the wire. -3. Set `this->motion_interpreter->my_run_rate` from the packet. -4. Call **`CPhysicsObj::MoveToObject(this->physics_obj, target_guid, - ¶ms)`** β€” which kicks off a **fully local** auto-walk on the - player's own physics body (`0x00512860`, decomp line 280598). -5. Fall through to `MoveToManager::MoveToPosition` only if the target - guid isn't found in the local physics world (rare; usually means the - client hasn't streamed in the target yet). - -This is the **same code path** that runs for every remote creature -chasing the player β€” retail did not have a separate "remote-only" vs -"local-only" auto-walk pipeline. The local client's MoveToManager -actively drove the local player's body forward when the server sent -MoveToObject. ACE's server-side `PhysicsObj.MoveToObject` simulation -runs in parallel for authoritative-position tracking + arrival -detection (`MoveToChain.WithinUseRadius`), but the visible movement on -the local client comes from the local MoveToManager β€” not from -inbound UpdatePosition packets. - -Option C would diverge from retail by relying on server position -broadcasts instead of local physics integration. That risks combat -movement, environment-trigger interactions, and animation hooks all -diverging from retail because they'd be driven by sparse server-side -position snapshots rather than smooth local integration. - -### Existing acdream infrastructure that's already retail-shaped - -We have most of the building blocks already: - -- [`RemoteMoveToDriver`](../../../src/AcDream.Core/Physics/RemoteMoveToDriver.cs) - is the per-tick steering loop β€” heading correction, arrival via - `min_distance` / `distance_to_object`, Β±20Β° aux-turn tolerance β€” - ported from retail's `MoveToManager::HandleMoveToPosition` - (`0x00529d80`). It's already exercised on every NPC chase. The - retail-faithful fix for the local player **reuses this driver**, - installed against the local player's body instead of a remote's - dead-reckoned body. -- [`ServerControlledLocomotion.PlanMoveToStart`](../../../src/AcDream.Core/Physics/ServerControlledLocomotion.cs) - already does what retail's `MovementParameters::get_command` - (`0x0052AA00`) does: seed `WalkForward` / `RunForward` depending on - `CanRun` + `MoveToSpeed` + `MoveToRunRate`. -- `MotionState.MoveToPath` is fully parsed on the wire. Remote chase - reads it at `GameWindow.cs:3401–3417`. - -The B.6 work is essentially **"do for `_playerServerGuid` what we -already do for remotes,"** with one extra concern: the local player has -a user-input motion source (`PlayerMovementController`) that has to -yield to the auto-walk while it's active. - -### Why not B - -Tweens aren't retail-faithful and would diverge worse than C. -Eliminated. - ---- - -## Required investigation before any code - -The "walks then snaps back" symptom is observed but not yet -characterized in detail. We need a live trace of: - -1. Outbound: timestamp + opcode + payload of the user's Use / PickUp - packet that triggers the auto-walk. -2. Inbound during the auto-walk: every `UpdateMotion`, `UpdatePosition`, - `VectorUpdate`, `WeenieError`, `InventoryServerSaveFailed` for the - local player, timestamped. -3. Visible client position at each inbound event (so we can correlate - "walks forward at frame N" with the inbound that caused it). - -**Implementation step:** add a runtime-gated diagnostic -`ACDREAM_PROBE_AUTOWALK=1` that logs one line per relevant event during -local-player auto-walk attempts. Roughly 30 LOC; mirrors L.2a slice 1's -`ACDREAM_PROBE_RESOLVE` / `ACDREAM_PROBE_CELL` pattern. - -The trace is no longer needed to *decide between options* (retail -decomp settles that β€” Option A wins), but it remains valuable as a -**baseline measurement** for the Option A implementation: knowing what -ACE sends today lets us verify the local driver behaves equivalently -on the wire (no extra packets needed, position broadcasts arrive at -expected cadence, the auto-walk completes inside -`defaultMoveToTimeout` instead of timing out). - ---- - -## File-level scope sketch (Option A β€” retail-faithful) - -Mirror retail's `MovementManager::PerformMovement` case 6 against -acdream's existing `PlayerMovementController` + `RemoteMoveToDriver`. - -**Slice 1 β€” diagnostic baseline (~30 LOC).** -- **Modify:** `src/AcDream.Core/Physics/PhysicsDiagnostics.cs` β€” add - `ProbeAutoWalkEnabled` flag gated on `ACDREAM_PROBE_AUTOWALK=1`. -- **Modify:** `GameWindow.OnLiveMotionUpdated`, `OnLivePositionUpdated`, - `OnVectorUpdated`, `SendUse`, `SendPickUp` β€” when probe is on and the - guid is `_playerServerGuid`, log one line per event with timestamp + - payload. Mirror the `[resolve]` / `[cell-transit]` line format from - L.2a. - -**Slice 2 β€” install auto-walk on inbound MoveToObject (~100 LOC).** -- **Modify:** `PlayerMovementController` β€” add `BeginServerAutoWalk( - Vector3 destinationWorld, float minDistance, float distanceToObject, - bool moveTowards, float moveToSpeed, float moveToRunRate, bool - canRun)` + `EndServerAutoWalk(reason)` methods. The controller owns - the "input vs auto-walk" state. While auto-walking, the per-tick - update calls `RemoteMoveToDriver.Step(...)` against its own body, and - the user-input motion path is suppressed. -- **Modify:** `GameWindow.OnLiveMotionUpdated` β€” when `update.Guid == - _playerServerGuid && IsServerControlledMoveTo && MoveToPath is - not null`, translate the path's `OriginCellId + OriginXYZ` to world - space (same `RemoteMoveToDriver.OriginToWorld` helper the remote - path uses), call `_playerController.BeginServerAutoWalk(...)`. - Otherwise (a non-MoveTo motion arrives for the player), call - `EndServerAutoWalk(reason="motion-changed")`. -- **Modify:** `PlayerMovementController.Tick` β€” if auto-walking, - consume input only for cancellation (W/A/S/D pressed β†’ cancel - auto-walk β†’ restore input); skip the input-driven velocity solve; - let `RemoteMoveToDriver.Step` set the body's velocity + heading; - apply arrival check via `min_distance` / `distance_to_object`; on - arrival, call `EndServerAutoWalk(reason="arrived")`. - -**Slice 3 β€” animation cycle selection (~20 LOC).** -- **Modify:** `GameWindow` `UpdatePlayerAnimation` (the path that - drives the local player's animation cycle from user input) β€” when - the controller is in `IsServerAutoWalking` state, source the cycle - from `ServerControlledLocomotion.PlanMoveToStart(...)` instead of the - user-input MotionInterpreter. This is what makes the local player - visibly walk + animate during the auto-walk. - -**Slice 4 β€” local pickup animation (probably closes #64, ~10 LOC).** -- **Modify:** `OnLiveMotionUpdated` β€” for `_playerServerGuid`, allow - `Motion(Pickup)` / `Motion(Pickup5/10/15/20)` to drive `SetCycle` - bypassing the existing self-echo filter at `GameWindow.cs:3289`. - This is the bend-down animation retail observers see when we pick up - an item. Same one-shot mechanism retail used; `UpdatePlayerAnimation` - doesn't predict it because it's server-initiated, so admitting the - echo is correct. - -Total: estimated ~160 LOC + unit tests for the controller state -machine. No new files; all changes land in existing physics + render -infrastructure. - ---- - -## Acceptance criteria - -- [ ] Double-click a ground item from 2–5 m away. Character walks to - the item, picks it up, despawn fires, inventory updates. -- [ ] F-press on a selected item from 2–5 m away. Same behavior as - double-click. -- [ ] Use a Holtburg NPC from 2–5 m away. Character walks to the NPC, - chat dialogue appears (NPC interaction completes, like the - close-range case from M1 target 3). -- [ ] No regression on close-range pickup (B.5 path remains 1-tap - works). -- [ ] User can interrupt the auto-walk by pressing a movement key (W / - A / S / D). Auto-walk releases; input takes over; ACE's - `MoveToChain` reads "not arrived" + "user cancelled" and stops. -- [ ] Pressing ESC during auto-walk releases the auto-walk and returns - to normal player mode. -- [ ] No "walks then snaps back" visual artifact under any of the - scenarios above. - ---- - -## Out of scope (deferred to later phases) - -- `MoveToPosition` (movement type 7) for the local player β€” typically - used by portal traversal and admin-teleport flows. M4 territory. -- `Stick` / sticky-target tracking β€” used by combat chase. M2/M3 - territory. -- Sphere-cylinder distance variant β€” relevant for vendor / corpse - interactions where the target has non-trivial extent. Can be added - in B.6's follow-up commit if needed. -- Local player's `MoveToObject` initiation via wire (we currently rely - on ACE to start the auto-walk on the player's behalf β€” that's the - retail behavior and we don't need a client-initiated path). - ---- - -## Carry-overs from B.5 - -- **#64** β€” local-player pickup animation. Same self-echo filter at - `OnLiveMotionUpdated:3289` that ignores MoveToObject also drops the - inbound `Motion(Pickup)` retail observers render correctly. Slice 4 - above admits server-initiated one-shot motions through the filter - for the local player, which should close #64. Verify in the same - visual-test pass. - ---- - -## State at design freeze - -- **Main HEAD:** `281d125` (initial B.6 design spec committed). -- **No code changes in this spec commit** β€” design document only. -- **Spec update 2026-05-14 (this commit):** retail decomp at - `MovementManager::PerformMovement` (`0x00524440` case 6, decomp lines - 300628–300648) decisively settles A vs C in favor of **Option A**. - Retail's local client ran its own `MoveToManager` and called - `CPhysicsObj::MoveToObject` on the local player's body. Option C - (server-position-blend) is not retail-faithful and is no longer - considered. -- **Slice 1 shipped 2026-05-14** (`eda8278` + `1b4f3ba`): - `ACDREAM_PROBE_AUTOWALK` diagnostic + DebugPanel checkbox. - -### Trace-captured findings (post-Slice-1) - -A live trace captured at Holtburg with the probe enabled β€” -double-clicking `+Je` (a remote player at `(111.34, 5.96, 94.01)` in -cell `0xA9B40021`) from ~3.5 m away, 4 successive Use sends. Findings: - -1. **Parser confirmed correct.** Each `[autowalk-mt]` line reads - `mt=0x06 isMoveTo=True moveTowards=True - path=cell=0xA9B40021,xyz=(111.34,5.96,94.01),minDist=0.00,objDist=0.50 - mtSpd=1.00 mtRun=0.00`. Matches ACE's - `MoveToObject.Write` + `MoveToParameters.Write` byte-for-byte. -2. **ACE sends `mtRun=0.00`** β€” not a parser bug. ACE's - `Player_Move.MoveTo` default for unspecified `runRate` is `0.0f`, - and the call into MoveToObject uses that wire value directly. Retail - decomp at `0x005245e9` copies the wire value into - `motion_interpreter->my_run_rate`; if 0, the local MoveToManager - falls back to the player's own run-rate via - `CMotionInterp::apply_run_to_command` (`0x00527BE0`). -3. **Player position never changed** during the entire trace. All - `[autowalk-up]` lines after the 4 Use sends report - `pos=(112.32, 9.36, 94.00)` verbatim β€” current behavior is pure - no-op on the inbound MoveToObject. -4. **ACE does NOT broadcast `UpdatePosition` for the local player - during the auto-walk.** This kills Option C even more firmly than - the retail decomp did. The local body has to drive itself. -5. **Slice 2 must handle `mtRun == 0.0`** β€” fall back to the player's - own current run rate. The trace also captured `fwdSpd=2.86` for - the user's normal running before the auto-walk attempt β€” that's - the server-echoed `ForwardSpeed`, available as a recent-history - source for the fallback. Default `1.0` if no echo yet. - -### Next session entry point - -Slice 2: add `PlayerMovementController.BeginServerAutoWalk` / -`EndServerAutoWalk` + per-tick steering via `RemoteMoveToDriver`-style -heading + arrival. Wire in `GameWindow.OnLiveMotionUpdated` with the -`mtRun=0` fallback chain. Suppress user-input motion while -auto-walking; cancel on movement-key press. diff --git a/docs/superpowers/specs/2026-05-15-phase-b7-target-indicator-design.md b/docs/superpowers/specs/2026-05-15-phase-b7-target-indicator-design.md deleted file mode 100644 index 9fd14fe..0000000 --- a/docs/superpowers/specs/2026-05-15-phase-b7-target-indicator-design.md +++ /dev/null @@ -1,128 +0,0 @@ -# Phase B.7 β€” Vivid Target Indicator β€” design - -**Date:** 2026-05-15. -**Status:** DESIGN β€” implementation starting same session. -**Closes (partially):** [issue #59](../../ISSUES.md) (`WorldPicker` over-pick β€” by making the wrong pick *visible* so the user can correct before clicking). -**Predecessors:** B.4b (selection), B.5 (pickup), B.6 (auto-walk). -**Retail anchors (decomp `docs/research/named-retail/acclient_2013_pseudo_c.txt`):** -- `VividTargetIndicator::SetSelected` at `0x004f5ce0` β€” selection setter + filtering. -- `gmRadarUI::GetBlipColor(weenie)` at `0x004d76f0` β€” per-type colour table. -- `VividTargetIndicator::CopyImage` at `0x004f5dd0` β€” tints a source bitmap by RGBA, four blit methods supported. -- `VividTargetIndicator::UpdateDisplayState` at `0x004f5fb0` β€” gated on `PlayerOption_VividTargetingIndicator`. -- Two render surfaces: `m_pOnScreen` (corners around target) + `m_pOffScreen` (edge arrow when target is off-camera). - ---- - -## Problem - -User sees `Tirenia` chat dialogue after intending to click an item. Cause: `WorldPicker.Pick`'s 5 m fixed radius over-picks bigger collision spheres (NPCs > items). Without on-screen feedback, the user can't tell their intended click missed β€” they only see the consequence (NPC walks toward, dialogue fires). - -Retail solved this with **selection feedback** drawn over the 3D scene: four small corner triangles framing the target's silhouette, colour-coded by entity type (yellow β‰ˆ creature, white β‰ˆ default, red β‰ˆ PK, etc.). The mere existence of the indicator makes the picker bug self-correcting β€” you click empty space to deselect, then click again on the actual target. - -This is the *missing UX feedback* that's been silently breaking interaction since B.4b shipped. - ---- - -## Scope decisions - -**Included in B.7 MVP:** -1. Per-entity colour lookup, port of `gmRadarUI::GetBlipColor` using our existing `ItemType` and `ObjectDescriptionFlags` (already parsed from `CreateObject`). -2. Screen-space corner-triangle renderer drawn via ImGui's `ImDrawList` (we already have ImGui β€” no new GL infrastructure). -3. Hook to `_selectedGuid` β€” updates whenever B.4b's `PickAndStoreSelection` mutates the selection. -4. Hidden when no selection, or when the selected entity is no longer in `_entitiesByServerGuid` (despawned). - -**Deferred to B.7 follow-ups:** -- Off-screen edge arrow (`m_pOffScreen`). Useful for tracking a target you walked past; not MVP-critical. -- Retail-faithful corner-triangle imagery loaded from the DAT. MVP draws procedurally-coloured equilateral triangles via ImGui β€” looks acceptable, doesn't need a DAT-asset hunt. -- Mesh-tint highlight (`"texture lights up a bit"`). That's a shader-level change on the selected entity's mesh. Requires touching `WbDrawDispatcher` to flag a per-instance highlight uniform. Two-line stub already mentioned in the `mesh_modern.vert` `InstanceData` struct docstring (per CLAUDE.md WB integration cribs) β€” pick it up if the corner triangles aren't enough. -- Player-option toggle (`PlayerOption_VividTargetingIndicator`). MVP is always-on; add toggle if users complain about screenshots. -- Server selection-relay (`SmartBox::SetTargetObjectID` outbound, `RecvNotice_ServerSaysMoveItem` inbound). Not visible in current ACE behaviour and not blocking M1. - ---- - -## Architecture - -Single new file: `src/AcDream.App/UI/TargetIndicatorPanel.cs` (or similar β€” fits the existing `AcDream.UI.Abstractions.Panels.*` pattern but lives in `AcDream.App` because it touches `GameWindow`'s camera projection state). - -**Responsibilities:** -- Read `_selectedGuid` (passed in via constructor delegate, like the existing `DebugVM` wiring). -- Look up the entity in `_entitiesByServerGuid` + `_liveEntityInfoByGuid`. -- Compute world-space AABB centre + radius (use cached `EntitySpawn.ItemType` for type bits + a fixed visual radius β€” 1 m default, or per-itemType later). -- Project the AABB to 4 screen-space corner positions using the active camera's `View Γ— Projection Γ— Viewport`. -- Resolve colour via `RadarBlipColors.For(itemType, objectDescriptionFlags)` β€” new static helper class. -- Draw 4 small filled triangles via `ImGui.GetBackgroundDrawList().AddTriangleFilled`. - -**Render order:** background-draw-list, AFTER the 3D scene, BEFORE other ImGui panels so other UI can occlude the triangles if needed. - ---- - -## Colour table - -Per `gmRadarUI::GetBlipColor` decomp lines `219913+`, the dispatch order is: - -``` -if pwd._bitfield & 0x40000 β†’ Portal (cyan-ish?) -if pwd._bitfield[1] & 0x02 β†’ Vendor (green?) -if (pwd._bitfield & 0x10) && IsCreature && !IsPlayer β†’ Creature (yellow) -if IsPlayer: - if IsPK β†’ PlayerKiller (red) - elif IsPKLite β†’ PKLite (pink?) - elif pwd._bitfield & 0x200000 β†’ Creature-coloured (hostile player flag?) - else β†’ Default (white) -… (more branches above the read window) -``` - -I'll port the dispatch verbatim. The actual `RGBAColor_Radar*` constants live in retail data (probably in `acclient.h` per CLAUDE.md's named-retail anchors). For MVP I'll use hand-picked colours that visually match retail screenshots; refine later if I find the constants. - ---- - -## Triangle geometry - -Each corner triangle in retail is a small right-angle triangle pointing *into* the centre of the selection rectangle β€” i.e., the top-left corner has its hypotenuse along the screen-up-then-screen-right diagonals, pointing down-right toward the entity. Same pattern at the other three corners (rotated 90Β° / 180Β° / 270Β°). - -MVP: small filled triangles (~8 px legs) at each corner of the projected AABB, oriented inward. Fine-tune via screenshots vs retail later. - ---- - -## File-level scope sketch - -- **New:** `src/AcDream.Core/Ui/RadarBlipColors.cs` (`AcDream.Core` so it's testable independently of `App`). Static class with `RGBA For(ItemType, ObjectDescriptionFlags)` returning a tagged colour. ~60 LOC. -- **New:** `src/AcDream.App/UI/TargetIndicatorPanel.cs` (~100 LOC). Owns the per-frame projection + ImGui draw. Constructor takes: - - `Func selectedGuidProvider` - - `Func entityResolver` (closure over `_entitiesByServerGuid` / `_liveEntityInfoByGuid`) - - `Func<(Matrix4x4 view, Matrix4x4 projection, Vector2 viewport)> cameraProvider` -- **Modify:** `src/AcDream.App/Rendering/GameWindow.cs` (~15 LOC). Construct the panel after ImGui init, wire delegates, call `Render()` from the ImGui pass. -- **Tests:** `tests/AcDream.Core.Tests/Ui/RadarBlipColorsTests.cs` (~40 LOC, 5-6 cases covering the type-flag β†’ colour matrix). - -Total ~200 LOC + tests. Single phase, no slices needed. - ---- - -## Acceptance criteria - -- [ ] Single-click an NPC at Holtburg β†’ yellow-ish corner triangles appear around it. -- [ ] Single-click a ground item β†’ white-ish triangles. -- [ ] Click empty ground / deselect β†’ triangles disappear. -- [ ] Selected entity moves β†’ triangles track it (project per frame). -- [ ] Selected entity despawns β†’ triangles disappear. -- [ ] No measurable FPS regression at Holtburg radius=4 (it's 4 triangles per frame β€” should be invisible to perf). -- [ ] Unit tests cover the colour-lookup matrix: NPC, item, player, PK, lifestone, portal, vendor. - ---- - -## Out of scope (deferred) - -- Off-screen edge arrow. -- DAT-loaded triangle sprite (procedural for MVP). -- Mesh-tint highlight on selected entity. -- Player-option toggle. -- Server selection-relay (`SmartBox::SetTargetObjectID`). -- Tab-cycle selection (`SelectClosestCombatTarget` already exists for combat; non-combat cycle is a separate UX phase). - ---- - -## Out-of-band: addressing the picker over-pick (#59) - -This phase deliberately does NOT fix the underlying picker. Once the indicator is shipped, the user can *see* the wrong selection and click empty space to clear it, then retry on the actual target. The picker fix (tighter per-itemType radius, ray-test against actual mesh bounds, or click priority by itemType) is a follow-up that can be informed by which combos most often produce mis-picks once we have the indicator showing them clearly. - -If the indicator + picker need to be revisited together later, both end up in the same UX phase. diff --git a/references/WorldBuilder b/references/WorldBuilder deleted file mode 160000 index 167788b..0000000 --- a/references/WorldBuilder +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 167788be6fce65f5ebe79eef07a0b7d28bd7aa81 diff --git a/src/AcDream.App/AcDream.App.csproj b/src/AcDream.App/AcDream.App.csproj index 84eb67a..c8a473b 100644 --- a/src/AcDream.App/AcDream.App.csproj +++ b/src/AcDream.App/AcDream.App.csproj @@ -9,12 +9,8 @@ AcDream.App true - - - - @@ -30,12 +26,6 @@ - - - diff --git a/src/AcDream.App/Input/PlayerMovementController.cs b/src/AcDream.App/Input/PlayerMovementController.cs index 5a126b8..1bc88b8 100644 --- a/src/AcDream.App/Input/PlayerMovementController.cs +++ b/src/AcDream.App/Input/PlayerMovementController.cs @@ -218,55 +218,6 @@ public sealed class PlayerMovementController private Vector3 _prevPhysicsPos; private Vector3 _currPhysicsPos; - // ── B.6 slice 2 (2026-05-14): local-player server-initiated auto-walk ── - // When ACE sends a MoveToObject motion for the local player (out-of-range - // Use / PickUp triggers ACE's server-side CreateMoveToChain), the wire - // payload includes a destination, arrival predicates, and a run rate. - // Retail's MovementManager::PerformMovement (0x00524440 case 6) runs a - // LOCAL auto-walk in response: heading correction toward the target, - // run-forward velocity at the wire's runRate, arrival detection via - // MoveToManager::HandleMoveToPosition. Here we keep the active auto-walk - // state and inject it into Update() as a synthesized Forward+Run input - // so the existing motion-interpreter / body-velocity pipeline runs - // unchanged. Spec: docs/superpowers/specs/2026-05-14-phase-b6-design.md. - private bool _autoWalkActive; - private Vector3 _autoWalkDestination; - private float _autoWalkMinDistance; - private float _autoWalkDistanceToObject; - private bool _autoWalkMoveTowards; - // Walk-vs-run decision is made ONCE at BeginServerAutoWalk based on - // initial distance vs the wire's WalkRunThreshold, then held for the - // duration of the auto-walk. Earlier per-frame evaluation produced - // "runs partway then walks the rest" which the user reported as - // wrong: the character should run all the way to the target then - // stop, not transition to a walk near the end. - private bool _autoWalkInitiallyRunning; - - /// - /// True while a server-initiated auto-walk (MoveToObject inbound) is - /// active on the local player. The next call - /// synthesizes Forward+Run input and steers toward - /// the destination until arrival or user-input cancellation. - /// - public bool IsServerAutoWalking => _autoWalkActive; - - /// - /// Fires once when an auto-walk reaches its destination naturally - /// (i.e. called with - /// reason="arrived"). Does NOT fire on user-input cancel or - /// on a re-target (BeginServerAutoWalk overwriting state). - /// - /// - /// Host () subscribes to re-send - /// the Use/PickUp action that triggered the auto-walk β€” without - /// this, ACE's server-side MoveToChain may have already timed out - /// by the time our local body arrives, so the action wouldn't - /// fire. Re-sending the action close-range hits ACE's WithinUseRadius - /// fast-path and completes immediately. - /// - /// - public event Action? AutoWalkArrived; - public PlayerMovementController(PhysicsEngine physics) { _physics = physics; @@ -337,264 +288,12 @@ public sealed class PlayerMovementController _motion.apply_current_movement(cancelMoveTo: false, allowJump: false); } - /// - /// B.6 slice 2 (2026-05-14). Install a server-initiated auto-walk - /// against this body. will synthesize - /// Forward+Run input and steer toward - /// until the body reaches the - /// arrival predicate (moveTowards: dist ≀ distanceToObject; - /// !moveTowards: dist β‰₯ minDistance) or the user presses any - /// movement key (which auto-cancels). - /// - /// - /// Retail reference: MovementManager::PerformMovement - /// (0x00524440) case 6 β€” unpacks the wire's target + - /// origin + run rate and calls CPhysicsObj::MoveToObject on - /// the local body. We do the equivalent at acdream's altitude: - /// hold the destination + thresholds + run rate locally, let the - /// existing per-tick motion machinery do the walking, and arrive - /// when the horizontal distance hits the threshold. - /// - /// - /// - /// The run-rate parameter is the EFFECTIVE rate after the - /// mtRun=0 fallback chain β€” the caller (GameWindow) is - /// responsible for substituting a non-zero rate when ACE sends 0.0 - /// on the wire, per the trace finding in the design spec. - /// - /// - public void BeginServerAutoWalk( - Vector3 destinationWorld, - float minDistance, - float distanceToObject, - bool moveTowards, - float walkRunThreshold) - { - _autoWalkActive = true; - _autoWalkDestination = destinationWorld; - _autoWalkMinDistance = minDistance; - _autoWalkDistanceToObject = distanceToObject; - _autoWalkMoveTowards = moveTowards; - - // Decide run vs walk ONCE based on the initial horizontal - // distance from the player to the destination. Run-all-the-way - // is the retail-faithful behaviour the user observed: pick a - // distant target β†’ character runs the whole way, decelerates - // to a stop at use radius. Earlier per-frame evaluation made - // the body transition to a walk inside threshold and felt - // wrong (the user reported "runs partway then walks"). - float dx = destinationWorld.X - _body.Position.X; - float dy = destinationWorld.Y - _body.Position.Y; - float initialDist = MathF.Sqrt(dx * dx + dy * dy); - _autoWalkInitiallyRunning = initialDist > walkRunThreshold; - } - - /// - /// B.6 slice 2 (2026-05-14). Cancel any active server-initiated - /// auto-walk. Idempotent. is logged when - /// is on so - /// the trace shows why the auto-walk ended. - /// - public void EndServerAutoWalk(string reason) - { - if (!_autoWalkActive) return; - _autoWalkActive = false; - if (PhysicsDiagnostics.ProbeAutoWalkEnabled) - Console.WriteLine($"[autowalk-end] reason={reason}"); - if (reason == "arrived") - AutoWalkArrived?.Invoke(); - } - - /// - /// B.6 slice 2 (2026-05-14). If a server-initiated auto-walk is - /// active, either cancel it (user pressed a movement key) or - /// synthesize a Forward+Run input with stepped - /// toward the destination. Returns the (possibly modified) input - /// for the rest of to consume. - /// - /// - /// Heading correction matches - /// β€” Β± - /// snap-on-aligned, otherwise rotate at - /// . Arrival - /// predicate matches retail's - /// MoveToManager::HandleMoveToPosition: chase arrives at - /// distanceToObject; flee arrives at minDistance. - /// - /// - private MovementInput ApplyAutoWalkOverlay(float dt, MovementInput input) - { - if (!_autoWalkActive) return input; - - // User-input cancellation. Any direct movement key takes over. - // Mouse-only turning (no movement key) doesn't cancel β€” the - // user might just be looking around mid-walk. - bool userOverride = input.Forward || input.Backward - || input.StrafeLeft || input.StrafeRight - || input.TurnLeft || input.TurnRight; - if (userOverride) - { - EndServerAutoWalk("user-input"); - return input; - } - - // Horizontal distance to target β€” server owns Z, our local body - // Z snaps to UpdatePosition broadcasts when ACE sends them. - var pos = _body.Position; - float dx = _autoWalkDestination.X - pos.X; - float dy = _autoWalkDestination.Y - pos.Y; - float dist = MathF.Sqrt(dx * dx + dy * dy); - - // Arrival predicate. With the 10 Hz heartbeat from 301281d the - // server-side Player.Location tracks our body within ~100 ms, so - // the previous "subtract 0.2 m safety margin" workaround is no - // longer needed. Tiny 0.05 m margin remains to absorb the - // sub-tick race between local arrival-fire and the next - // heartbeat's outbound packet. - // - // ARRIVAL IS GATED ON ALIGNMENT: we only end the auto-walk once - // the body is BOTH within use-radius AND facing the target. - // Without the alignment gate, a Use on a close target while - // facing away would end immediately and the body wouldn't turn - // at all (user feedback 2026-05-15: 'when I'm close I'm not - // facing'). The alignment check is computed below in the same - // block as the heading-step; we defer the arrival fire-and-end - // until after we've inspected `aligned`. - float arrivalThreshold = _autoWalkMoveTowards - ? _autoWalkDistanceToObject - : _autoWalkMinDistance; - const float TinyMargin = 0.05f; - float effectiveArrival = MathF.Max(arrivalThreshold - TinyMargin, 0.1f); - bool withinArrival = - (_autoWalkMoveTowards - && dist <= effectiveArrival) - || (!_autoWalkMoveTowards - && dist >= arrivalThreshold + RemoteMoveToDriver.ArrivalEpsilon); - - // Step Yaw toward target. Convention from Update line 364: - // _body.Orientation = Quaternion.CreateFromAxisAngle(Z, Yaw - Ο€/2), - // so local-forward (+Y) maps to world (cos Yaw, sin Yaw, 0). - // Therefore Yaw that faces (dx,dy) is atan2(dy, dx). - // - // User feedback (2026-05-15): 'I should face that object and then - // start moving. Now it starts running before facing is complete.' - // Track the current heading delta β€” if we're more than the - // walk-while-turning threshold off, suppress Forward this frame - // so the body turns IN PLACE first. Once we're within the - // threshold, the synthesised Forward+Run kicks in below. - bool aligned = true; - bool walkAligned = true; - if (dist > 1e-4f) - { - float desiredYaw = MathF.Atan2(dy, dx); - float delta = desiredYaw - Yaw; - while (delta > MathF.PI) delta -= 2f * MathF.PI; - while (delta < -MathF.PI) delta += 2f * MathF.PI; - - // Retail-faithful local rotation: rotate continuously at - // TurnRate, never snap until overshoot would occur. Retail's - // MoveToManager::HandleTurnToHeading (0x0052a0c0) only snaps - // when heading_greater() detects we've crossed the target β€” - // there's no "snap when close" tolerance band. The earlier - // 20Β° snap was borrowed wrongly from RemoteMoveToDriver - // (which is the sparse-update-fudge path for remotes). - // - // MathF.Min(|delta|, maxStep) naturally clamps the final - // fractional step to exactly delta, so we land on the - // target heading without overshoot. - // 2026-05-16 β€” retail-faithful turn rate. Auto-walk knows - // its run/walk decision from _autoWalkInitiallyRunning - // (set at BeginServerAutoWalk based on initial distance vs - // WalkRunThreshold). Running rotation is 50% faster per - // run_turn_factor at retail 0x007c8914. - float maxStep = RemoteMoveToDriver.TurnRateFor(_autoWalkInitiallyRunning) * dt; - Yaw += MathF.Sign(delta) * MathF.Min(MathF.Abs(delta), maxStep); - while (Yaw > MathF.PI) Yaw -= 2f * MathF.PI; - while (Yaw < -MathF.PI) Yaw += 2f * MathF.PI; - - // Two alignment thresholds: - // walkWhileTurning (30Β°): outside this, body turns in place. - // Inside, body walks forward while - // finishing residual alignment. - // fullyAligned (5Β°): the arrival-fire alignment. ACE - // rotates server-side via Rotate(target) - // BEFORE invoking the Use callback β€” - // user reported 'it does not face it - // completely', so the final-alignment - // check must be tighter than the - // walking gate. - const float WalkWhileTurningRad = 30f * MathF.PI / 180f; - const float FullyAlignedRad = 5f * MathF.PI / 180f; - walkAligned = MathF.Abs(delta) <= WalkWhileTurningRad; - aligned = MathF.Abs(delta) <= FullyAlignedRad; - } - - // End the auto-walk once the body is BOTH within use radius - // AND aligned with the target. This is the alignment-gated - // arrival the comment above flagged: a close-range Use on a - // target behind the player still rotates the body first. - if (withinArrival && aligned) - { - EndServerAutoWalk("arrived"); - return input; - } - - // Walk vs run decided ONCE at BeginServerAutoWalk based on - // initial distance β€” held for the rest of the auto-walk so the - // character keeps running all the way to the target instead of - // transitioning to a walk inside the threshold. Matches user- - // observed retail behaviour ("if its far away it should run - // all the way to the object and then stop"). - bool shouldRun = _autoWalkInitiallyRunning; - - // Turn-first gate: if not yet within the 30Β° walking band, - // suppress forward motion so the body turns in place rather - // than walking an arc. Also suppress when already within - // arrival β€” we just turned to face it; no need to step forward - // into it. - bool moveForward = walkAligned && !withinArrival; - - // Synthesize "moving forward" input. The rest of Update reads - // Yaw + input.Forward + input.Run to drive _motion + body - // velocity exactly as it does for user-driven W (+ optional Shift). - // We zero any mouse delta so a stale frame doesn't fight the - // steering. - return input with - { - Forward = moveForward, - Run = moveForward && shouldRun, - Backward = false, - StrafeLeft = false, - StrafeRight = false, - TurnLeft = false, - TurnRight = false, - MouseDeltaX = 0f, - }; - } - - // L.2a slice 1 (2026-05-12): centralized CellId mutation so the - // [cell-transit] probe fires from a single chokepoint. Both the - // server-snap path (SetPosition) and the per-frame resolver path - // route through here. When PhysicsDiagnostics.ProbeCellEnabled is - // off this collapses to a single bool-compare + assignment β€” zero - // logging cost. - private void UpdateCellId(uint newCellId, string reason) - { - if (newCellId != CellId && PhysicsDiagnostics.ProbeCellEnabled) - { - var pos = _body.Position; - Console.WriteLine(System.FormattableString.Invariant( - $"[cell-transit] 0x{CellId:X8} -> 0x{newCellId:X8} pos=({pos.X:F3},{pos.Y:F3},{pos.Z:F3}) reason={reason}")); - } - CellId = newCellId; - } - public void SetPosition(Vector3 pos, uint cellId) { _body.Position = pos; _prevPhysicsPos = pos; _currPhysicsPos = pos; - UpdateCellId(cellId, "teleport"); + CellId = cellId; // Treat as grounded after a server-side position snap. _body.TransientState = TransientStateFlags.Contact | TransientStateFlags.OnWalkable; @@ -613,15 +312,6 @@ public sealed class PlayerMovementController public MovementResult Update(float dt, MovementInput input) { - // B.6 slice 2 (2026-05-14): server-initiated auto-walk overlay. - // When _autoWalkActive, steer Yaw toward _autoWalkDestination and - // synthesize Forward+Run input so the rest of Update runs the - // body forward as if the user were holding W. User movement-key - // input cancels the auto-walk (retail UX). Arrival check fires - // before synthesizing, so a one-frame arrival doesn't waste a - // tick of velocity past the target. - input = ApplyAutoWalkOverlay(dt, input); - // Portal-space guard: while teleporting, no input is processed and // no physics is resolved. Return a zero-movement result so the caller // can detect the frozen state (MotionStateChanged = false, no commands). @@ -642,21 +332,10 @@ public sealed class PlayerMovementController } // ── 1. Apply turning from keyboard + mouse ──────────────────────────── - // 2026-05-16 β€” retail-faithful turn rate. - // Anchor: docs/research/named-retail/acclient_2013_pseudo_c.txt - // - CMotionInterp::apply_run_to_command 0x00527be0 - // multiplies turn_speed by run_turn_factor (1.5) under - // HoldKey.Run on TurnRight/TurnLeft commands. - // - Base rate Β±Ο€/2 rad/s comes from add_motion 0x005224b0 - // with HasOmega-cleared MotionData fallback. - // Effective: walking β‰ˆ 90Β°/s, running β‰ˆ 135Β°/s. - // Previously: WalkAnimSpeed*0.5 β‰ˆ 89.4Β°/s β€” coincidentally - // close to retail walking but no run differentiation. - float keyboardTurnRate = RemoteMoveToDriver.TurnRateFor(input.Run); if (input.TurnRight) - Yaw -= keyboardTurnRate * dt; + Yaw -= MotionInterpreter.WalkAnimSpeed * 0.5f * dt; // ~90Β°/s if (input.TurnLeft) - Yaw += keyboardTurnRate * dt; + Yaw += MotionInterpreter.WalkAnimSpeed * 0.5f * dt; Yaw -= input.MouseDeltaX * MouseTurnSensitivity; // Wrap yaw to [-PI, PI] so it doesn't grow unbounded. while (Yaw > MathF.PI) Yaw -= 2f * MathF.PI; @@ -1081,7 +760,7 @@ public sealed class PlayerMovementController _wasAirborneLastFrame = !_body.OnWalkable; - UpdateCellId(resolveResult.CellId, "resolver"); + CellId = resolveResult.CellId; // ── 6. Determine outbound motion commands ───────────────────────────── uint? outForwardCmd = null; @@ -1202,20 +881,7 @@ public sealed class PlayerMovementController // early return) skips Update entirely, so reaching this line implies // we're in a valid in-world pose. _heartbeatAccum += dt; - // B.6+B.7 (2026-05-15): bump heartbeat from 1 Hz to ~10 Hz while - // the body is actively moving (auto-walk OR user pressing W/A/S/D). - // ACE's server-side CreateMoveToChain polls WithinUseRadius every - // ~0.1 s using the latest Player.Location; 1 Hz heartbeats leave - // up to 1 s of stale position data on the server, which meant - // ACE's MoveToChain rejected our re-sent Use action as still - // out-of-range. With 10 Hz updates ACE sees us approaching in - // ~real-time and the server-side chain converges normally β€” - // retires the arrival-margin / re-send / flush-AP workarounds. - bool activelyMoving = _autoWalkActive - || input.Forward || input.Backward - || input.StrafeLeft || input.StrafeRight; - float effectiveInterval = activelyMoving ? 0.1f : HeartbeatInterval; - HeartbeatDue = _heartbeatAccum >= effectiveInterval; + HeartbeatDue = _heartbeatAccum >= HeartbeatInterval; if (HeartbeatDue) _heartbeatAccum = 0f; // K-fix5 (2026-04-26): local-animation-cycle pacing. Visual rate diff --git a/src/AcDream.App/Rendering/GameWindow.cs b/src/AcDream.App/Rendering/GameWindow.cs index 457ac11..b00345d 100644 --- a/src/AcDream.App/Rendering/GameWindow.cs +++ b/src/AcDream.App/Rendering/GameWindow.cs @@ -18,27 +18,16 @@ public sealed class GameWindow : IDisposable private IWindow? _window; private GL? _gl; private IInputContext? _input; - private TerrainModernRenderer? _terrain; - /// Phase N.5b: terrain_modern.vert/.frag program. Owned by - /// at draw time but allocated + disposed here. - private Shader? _terrainModernShader; + private TerrainChunkRenderer? _terrain; + private Shader? _shader; private CameraController? _cameraController; private IMouse? _capturedMouse; private DatCollection? _dats; private float _lastMouseX; private float _lastMouseY; + private InstancedMeshRenderer? _staticMesh; private Shader? _meshShader; private TextureCache? _textureCache; - /// Phase N.4+: WB-backed rendering pipeline adapter. Always non-null - /// after OnLoad completes (modern path is mandatory as of N.5). - private AcDream.App.Rendering.Wb.WbMeshAdapter? _wbMeshAdapter; - private AcDream.App.Rendering.Wb.EntitySpawnAdapter? _wbEntitySpawnAdapter; - private AcDream.App.Rendering.Vfx.EntityScriptActivator? _entityScriptActivator; - private AcDream.App.Rendering.Wb.WbDrawDispatcher? _wbDrawDispatcher; - /// Phase N.5: ARB_bindless_texture + ARB_shader_draw_parameters - /// support. Required at startup β€” missing bindless throws - /// in OnLoad. - private AcDream.App.Rendering.Wb.BindlessSupport? _bindlessSupport; private SamplerCache? _samplerCache; private DebugLineRenderer? _debugLines; // K-fix4 (2026-04-26): default OFF. The orange BSP / green cylinder @@ -71,29 +60,11 @@ public sealed class GameWindow : IDisposable private string _lastNearestObjLabel = "-"; private bool _lastColliding; - // Phase N.5b: CPU timing for [TERRAIN-DIAG] under ACDREAM_WB_DIAG=1 - // (parallel diagnostic to [WB-DIAG] in WbDrawDispatcher β€” same env var - // gate so flipping one switch turns on both dispatcher rollups). Mirrors - // the rolling-256-sample buffer pattern from WbDrawDispatcher. - private readonly System.Diagnostics.Stopwatch _terrainCpuStopwatch = new(); - private readonly long[] _terrainCpuSamples = new long[256]; // microseconds - private int _terrainCpuSampleCursor; - private long _terrainLastDiagTick; - // Phase A.1: streaming fields replacing the one-shot _entities list. private AcDream.App.Streaming.LandblockStreamer? _streamer; - private AcDream.App.Streaming.GpuWorldState _worldState = new(); + private readonly AcDream.App.Streaming.GpuWorldState _worldState = new(); private AcDream.App.Streaming.StreamingController? _streamingController; - private int _streamingRadius = 2; // default 5Γ—5 (kept for debug overlay getStreamingRadius callback) - private int _nearRadius = 4; // Phase A.5 T16: two-tier near ring (default 4 β†’ 9Γ—9) - private int _farRadius = 12; // Phase A.5 T16: two-tier far ring (default 12 β†’ 25Γ—25) - // A.5 T22.5: resolved quality settings (preset + env-var overrides). - // Set once in OnLoad after LoadAndApplyPersistedSettings(); re-set on - // ReapplyQualityPreset(). Default matches QualityPreset.High so the field - // is valid before OnLoad fires (no GL calls are made before OnLoad anyway). - private AcDream.UI.Abstractions.Settings.QualitySettings _resolvedQuality = - AcDream.UI.Abstractions.Settings.QualitySettings.From( - AcDream.UI.Abstractions.Settings.QualityPreset.High); + private int _streamingRadius = 2; // default 5Γ—5 private uint? _lastLivePlayerLandblockId; // Phase B.3: physics engine β€” populated from the streaming pipeline. @@ -107,24 +78,13 @@ public sealed class GameWindow : IDisposable // Step 4: portal-based interior cell visibility. private readonly CellVisibility _cellVisibility = new(); - // Phase A.1 hotfix / Phase A.5 T10: DatCollection is NOT thread-safe. - // DatReaderWriter's DatBinReader uses a shared buffer position internally β€” - // concurrent _dats.Get calls from the streaming worker thread (T11+) and - // the render thread (BuildLandblockForStreaming on the worker; - // ApplyLoadedTerrain + live-spawn handlers + animation ticks on the render - // thread) corrupt that state and produce half-populated LandBlock.Height[] - // arrays, rendering as "a giant ball with spikes". All _dats.Get call - // sites that can race with the streaming worker MUST hold this lock. - // - // Worker-thread dat reads enter via the factory closures passed to - // LandblockStreamer at construction (loadLandblock + buildMeshOrNull). - // Those closures already acquire _datLock, so no additional wrapping is - // needed for reads inside BuildLandblockForStreamingLocked / - // BuildSceneryEntitiesForStreaming / BuildInteriorEntitiesForStreaming. - // Render-thread paths (ApplyLoadedTerrain, OnLiveEntitySpawned) already - // hold this lock via their outer wrappers; all remaining render-thread - // _dats.Get calls run only when no worker dat read can be in flight (during - // initialization or within the same lock scope). + // Phase A.1 hotfix: DatCollection is NOT thread-safe. The streaming worker + // thread and the render thread both read dats (BuildLandblockForStreaming + // on the worker; ApplyLoadedTerrain + live-spawn handlers on the render + // thread). Concurrent reads corrupt internal caches and produce + // half-populated LandBlock.Height[] arrays, which caused terrain to render + // as "a giant ball with spikes" before this lock was added. All _dats.Get + // calls that can race with the worker thread MUST acquire this lock. private readonly object _datLock = new(); // Terrain build context shared across all streamed landblocks. Stored as @@ -132,9 +92,7 @@ public sealed class GameWindow : IDisposable // LandblockMesh.Build without re-deriving these each time. private float[]? _heightTable; private AcDream.Core.Terrain.TerrainBlendingContext? _blendCtx; - // Was: Dictionary. ConcurrentDictionary so the off-thread - // mesh builder (A.5 T11+) can call LandblockMesh.Build without a lock. - private System.Collections.Concurrent.ConcurrentDictionary? _surfaceCache; + private Dictionary? _surfaceCache; // Phase A.1 Task 8: worker thread pre-builds EnvCell room-mesh sub-meshes // (CPU only) and stores them here. ApplyLoadedTerrain (render thread) drains @@ -160,17 +118,6 @@ public sealed class GameWindow : IDisposable /// private readonly Dictionary _animatedEntities = new(); - /// - /// Tier 1 cache (#53): per-entity classification results for static - /// entities (those NOT in ). Conceptually - /// paired with β€” that dictionary is the - /// gating predicate, this cache is the lookup that depends on it. - /// Passed to at - /// construction time. Tasks 9-10 of the cache plan wire the per-entity - /// miss-populate / hit-fast-path through the dispatcher's loop. - /// - private readonly AcDream.App.Rendering.Wb.EntityClassificationCache _classificationCache = new(); - private sealed class AnimatedEntity { public required AcDream.Core.World.WorldEntity Entity; @@ -565,11 +512,6 @@ public sealed class GameWindow : IDisposable // See docs/plans/2026-04-24-ui-framework.md for the staged UI strategy. private AcDream.UI.ImGui.ImGuiBootstrapper? _imguiBootstrap; private AcDream.UI.ImGui.ImGuiPanelHost? _panelHost; - // B.7 (2026-05-15): Vivid Target Indicator β€” four corner triangles - // around the selected entity, colour-coded by ItemType + PWD bits. - // Lives alongside the debug panels; cheap to construct + ignore - // when no selection. Spec: docs/superpowers/specs/2026-05-15-phase-b7-target-indicator-design.md - private AcDream.App.UI.TargetIndicatorPanel? _targetIndicator; private AcDream.UI.Abstractions.Panels.Vitals.VitalsVM? _vitalsVm; // Phase I.2: ImGui debug panel ViewModel. Lives for as long as // _panelHost does. Self-subscribes to CombatState in its ctor, so @@ -788,17 +730,7 @@ public sealed class GameWindow : IDisposable /// fields when a 0xF625 ObjDescEvent arrives carrying only updated visuals. /// private readonly Dictionary _lastSpawnByGuid = new(); - // Current selection: written by Q-cycle (combat) and LMB click (interact); cleared on entity despawn. - private uint? _selectedGuid; - - // B.6+B.7 (2026-05-15): pending action that triggered an auto-walk. - // When the local body arrives at the auto-walk target, - // OnAutoWalkArrivedReSendAction re-sends the action close-range so - // ACE completes it via WithinUseRadius even if its server-side - // MoveToChain already timed out. Cleared before each re-send to - // prevent infinite loops (the re-sent action's auto-walk would - // arrive immediately at the same position, infinite re-fire). - private (uint Guid, bool IsPickup)? _pendingPostArrivalAction; + private uint? _selectedTargetGuid; private readonly record struct LiveEntityInfo( string? Name, AcDream.Core.Items.ItemType ItemType); @@ -854,16 +786,6 @@ public sealed class GameWindow : IDisposable public void Run() { - // A.5 T22.5: resolve quality preset BEFORE creating the window so - // Samples (MSAA) is baked into WindowOptions correctly. GL context - // sample count cannot change at runtime; all other quality fields are - // applied again in OnLoad after the full settings load. - var startupStore = new AcDream.UI.Abstractions.Panels.Settings.SettingsStore( - AcDream.UI.Abstractions.Panels.Settings.SettingsStore.DefaultPath()); - var startupDisplay = startupStore.LoadDisplay(); - var startupBase = AcDream.UI.Abstractions.Settings.QualitySettings.From(startupDisplay.Quality); - var startupQuality = AcDream.UI.Abstractions.Settings.QualitySettings.WithEnvOverrides(startupBase); - var options = WindowOptions.Default with { Size = new Vector2D(1280, 720), @@ -874,11 +796,6 @@ public sealed class GameWindow : IDisposable ContextFlags.ForwardCompatible, new APIVersion(4, 3)), VSync = false, // off during development so the perf overlay shows true framerate - // A.5 T22.5: MSAA from quality preset (0 = disabled, 2/4/8 = multisample). - // Silk.NET passes this to SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES). - // Cannot be changed at runtime; Quality changes mid-session that would - // alter MsaaSamples are logged as a restart-required warning. - Samples = startupQuality.MsaaSamples, }; _window = Window.Create(options); @@ -1040,13 +957,13 @@ public sealed class GameWindow : IDisposable _gl.Enable(EnableCap.DepthTest); string shadersDir = Path.Combine(AppContext.BaseDirectory, "Rendering", "Shaders"); + _shader = new Shader(_gl, + Path.Combine(shadersDir, "terrain.vert"), + Path.Combine(shadersDir, "terrain.frag")); - // Phase N.5b: terrain_modern shader pair β€” bindless texture handles + - // glMultiDrawElementsIndirect dispatch path. The only terrain shader - // since Task 9 retired the legacy terrain.vert/.frag program. - _terrainModernShader = new Shader(_gl, - Path.Combine(shadersDir, "terrain_modern.vert"), - Path.Combine(shadersDir, "terrain_modern.frag")); + _meshShader = new Shader(_gl, + Path.Combine(shadersDir, "mesh_instanced.vert"), + Path.Combine(shadersDir, "mesh_instanced.frag")); // Phase G.1/G.2: shared scene-lighting UBO. Stays bound at // binding=1 for the lifetime of the process β€” every shader that @@ -1142,18 +1059,6 @@ public sealed class GameWindow : IDisposable // without re-loading. LoadAndApplyPersistedSettings(); - // A.5 T22.5: resolve quality preset immediately after settings load so - // _resolvedQuality is available for TerrainAtlas.SetAnisotropic, - // WbDrawDispatcher.AlphaToCoverage, and StreamingController wiring below. - { - var qBase = AcDream.UI.Abstractions.Settings.QualitySettings.From(_persistedDisplay.Quality); - _resolvedQuality = AcDream.UI.Abstractions.Settings.QualitySettings.WithEnvOverrides(qBase); - if (!_resolvedQuality.Equals(qBase)) - Console.WriteLine($"[QUALITY] Preset {_persistedDisplay.Quality} overridden by env vars: {_resolvedQuality}"); - else - Console.WriteLine($"[QUALITY] Preset {_persistedDisplay.Quality} β†’ {_resolvedQuality}"); - } - // Phase D.2a β€” ImGui devtools overlay. Zero cost when the env var // isn't set: no context creation, no per-frame branches hit. // See docs/plans/2026-04-24-ui-framework.md + memory/project_ui_architecture.md. @@ -1164,60 +1069,6 @@ public sealed class GameWindow : IDisposable _imguiBootstrap = new AcDream.UI.ImGui.ImGuiBootstrapper(_gl!, _window!, _input!); _panelHost = new AcDream.UI.ImGui.ImGuiPanelHost(); - // B.7 Vivid Target Indicator β€” corner-triangle highlight - // around the currently-selected entity. Delegates pull - // live state from this GameWindow instance every frame: - // - selected guid β†’ _selectedGuid (set by PickAndStoreSelection) - // - entity resolver β†’ position from _entitiesByServerGuid + - // itemType / PWD bits from cached LiveEntityInfo + last spawn - // - camera β†’ _cameraController.Active or (zero) when not - // yet ready, in which case the panel bails on viewport==0. - _targetIndicator = new AcDream.App.UI.TargetIndicatorPanel( - selectedGuidProvider: () => _selectedGuid, - entityResolver: guid => - { - if (!_entitiesByServerGuid.TryGetValue(guid, out var entity)) - return null; - uint rawItemType = 0; - if (_liveEntityInfoByGuid.TryGetValue(guid, out var info)) - rawItemType = (uint)info.ItemType; - uint pwdBits = 0; - uint? useability = null; - if (_lastSpawnByGuid.TryGetValue(guid, out var spawn)) - { - if (spawn.ObjectDescriptionFlags is { } odf) pwdBits = odf; - useability = spawn.Useability; - } - // 2026-05-16 β€” retail-faithful path. Pass the - // entity's Setup.SelectionSphere (scaled by entity - // scale, rotated into world coords) through so - // the panel projects the sphere as a screen - // circle. Matches SmartBox::GetObjectBoundingBox - // (decomp 0x00452e20). If the Setup didn't bake - // a selection sphere (rare, zero-radius), the - // panel falls back to per-type height heuristic. - System.Numerics.Vector3? sphereCenter = null; - float? sphereRadius = null; - if (TryGetEntitySelectionSphere(guid, out var sCenter, out var sRadius)) - { - sphereCenter = sCenter; - sphereRadius = sRadius; - } - return new AcDream.App.UI.TargetIndicatorPanel.TargetInfo( - entity.Position, rawItemType, pwdBits, entity.Scale, useability, - sphereCenter, sphereRadius); - }, - cameraProvider: () => - { - if (_cameraController is null || _window is null) - return (System.Numerics.Matrix4x4.Identity, - System.Numerics.Matrix4x4.Identity, - System.Numerics.Vector2.Zero); - var cam = _cameraController.Active; - return (cam.View, cam.Projection, - new System.Numerics.Vector2(_window.Size.X, _window.Size.Y)); - }); - // VitalsVM: GUID=0 at construction; set later at EnterWorld // (see the _playerServerGuid assignment path). Pre-login the // HP bar just reads 1.0 (safe default) β€” harmless. Stam/Mana @@ -1266,8 +1117,7 @@ public sealed class GameWindow : IDisposable getNearestObjLabel: () => _lastNearestObjLabel, getColliding: () => _lastColliding, getDebugWireframes: () => _debugCollisionVisible, - getStreamingRadius: () => _nearRadius, // A.5 T16 follow-up: was _streamingRadius (legacy single-tier); show near tier - + getStreamingRadius: () => _streamingRadius, getMouseSensitivity: () => GetActiveSensitivity(), getChaseDistance: () => _chaseCamera?.Distance ?? 0f, getRmbOrbit: () => _rmbHeld, @@ -1341,12 +1191,6 @@ public sealed class GameWindow : IDisposable // already track DisplayDraft via the // per-frame push. ApplyDisplayWindowState(display); - // A.5 T22.5: apply quality preset if it changed. - // MSAA changes log a restart-required warning - // inside ReapplyQualityPreset; all other fields - // apply immediately. - _persistedDisplay = display; - ReapplyQualityPreset(display.Quality); } catch (Exception ex) { @@ -1537,48 +1381,10 @@ public sealed class GameWindow : IDisposable // TimeSync arrives. WorldTime.SyncFromServer(AcDream.Core.World.DerethDateTime.DayTicks / 16.0); // = 476.25 = Midsong (noon) - // N.5: detect ARB_bindless_texture + ARB_shader_draw_parameters BEFORE - // building the terrain atlas / renderer β€” both consume BindlessSupport - // (atlas via Texture2DArray bindless handles, renderer for SSBO uploads). - // The modern path (SSBO + glMultiDrawElementsIndirect + bindless textures) - // is mandatory as of Phase N.5 β€” missing extensions throw at startup with - // a clear error so users can file a real bug report rather than silently - // falling back to a half-working renderer. - if (AcDream.App.Rendering.Wb.BindlessSupport.TryCreate(_gl, out var bindless)) - { - if (bindless!.HasShaderDrawParameters(_gl)) - { - _bindlessSupport = bindless; - Console.WriteLine("[N.5] modern path capabilities present (bindless + ARB_shader_draw_parameters)"); - } - else - { - Console.WriteLine("[N.5] GL_ARB_shader_draw_parameters not present β€” modern path not available"); - } - } - else - { - Console.WriteLine("[N.5] GL_ARB_bindless_texture not present β€” modern path not available"); - } + // Build the terrain atlas once from the Region dat. + var terrainAtlas = AcDream.App.Rendering.TerrainAtlas.Build(_gl, _dats); - if (_bindlessSupport is null) - { - throw new NotSupportedException( - "acdream requires GL_ARB_bindless_texture + GL_ARB_shader_draw_parameters " + - "(GL 4.3+ with bindless support). Your GPU/driver does not expose these extensions. " + - "If this is unexpected, please file a bug report with your GPU vendor + driver version."); - } - - // Build the terrain atlas once from the Region dat. Phase N.5b: the - // atlas exposes bindless handles for the modern terrain path, so - // BindlessSupport is threaded through. - var terrainAtlas = AcDream.App.Rendering.TerrainAtlas.Build(_gl, _dats, _bindlessSupport); - // A.5 T22.5: apply anisotropic level from quality preset. Build() - // hard-codes 16x; override here to match the resolved quality so Low - // (4x) and Medium (8x) actually take effect. - terrainAtlas.SetAnisotropic(_resolvedQuality.AnisotropicLevel); - - _terrain = new TerrainModernRenderer(_gl, _bindlessSupport, _terrainModernShader!, terrainAtlas); + _terrain = new TerrainChunkRenderer(_gl, _shader, terrainAtlas); int centerX = (int)((centerLandblockId >> 24) & 0xFFu); int centerY = (int)((centerLandblockId >> 16) & 0xFFu); @@ -1606,127 +1412,16 @@ public sealed class GameWindow : IDisposable RoadAlphaRCodes: terrainAtlas.RoadAlphaRCodes); _heightTable = heightTable; - _surfaceCache = new System.Collections.Concurrent.ConcurrentDictionary(); + _surfaceCache = new Dictionary(); - // (Bindless detection moved above β€” must precede TerrainAtlas.Build / - // TerrainModernRenderer ctor so they can consume BindlessSupport.) - - // Mesh shader always loads (modern path is the only path). - _meshShader = new Shader(_gl, - Path.Combine(shadersDir, "mesh_modern.vert"), - Path.Combine(shadersDir, "mesh_modern.frag")); - Console.WriteLine("[N.5] mesh_modern shader loaded"); - - _textureCache = new TextureCache(_gl, _dats, _bindlessSupport); + _textureCache = new TextureCache(_gl, _dats); // Two persistent GL sampler objects (Repeat + ClampToEdge) so // the sky pass can pick wrap mode per submesh without mutating // shared per-texture wrap state. See SamplerCache + the // WorldBuilder reference at // references/WorldBuilder/Chorizite.OpenGLSDLBackend/OpenGLGraphicsDevice.cs:115-132. _samplerCache = new SamplerCache(_gl); - - // Phase N.4+N.5 β€” WB rendering pipeline foundation. The modern path is - // mandatory as of N.5 ship amendment: WbMeshAdapter + WbDrawDispatcher - // always construct. WbMeshAdapter owns ObjectMeshManager and opens its - // own file handles for the dat files (independent of our DatCollection). - { - var wbLogger = Microsoft.Extensions.Logging.Abstractions.NullLogger.Instance; - _wbMeshAdapter = new AcDream.App.Rendering.Wb.WbMeshAdapter(_gl, _datDir, _dats, wbLogger); - Console.WriteLine("[N.4+N.5] WB foundation + modern path active β€” routing all content through ObjectMeshManager."); - } - - // Phase N.4 Task 12: construct LandblockSpawnAdapter under the feature flag - // and rebuild _worldState so it threads the adapter in. _worldState starts - // as an unadorned GpuWorldState (field initializer); here we replace it with - // one that carries the adapter so AddLandblock/RemoveLandblock notify WB. - // Phase N.4 Task 17: also construct EntitySpawnAdapter for server-spawned - // per-instance content under the same flag. - // N.5 mandatory path: spawn adapters + dispatcher always construct. - // _wbMeshAdapter, _meshShader, _textureCache, and _bindlessSupport are - // all guaranteed non-null here (startup throws above if any are missing). - { - var wbSpawnAdapter = new AcDream.App.Rendering.Wb.LandblockSpawnAdapter(_wbMeshAdapter!); - // Sequencer factory: look up Setup + MotionTable from dats and build - // an AnimationSequencer. Falls back to a no-op sequencer when the - // entity has no motion table (static props, etc.). Uses _animLoader - // which is initialised earlier in OnLoad; it is non-null here. - var capturedDats = _dats; - var capturedAnimLoader = _animLoader; - AcDream.Core.Physics.AnimationSequencer SequencerFactory(AcDream.Core.World.WorldEntity e) - { - if (capturedDats is not null && capturedAnimLoader is not null) - { - var setup = capturedDats.Get(e.SourceGfxObjOrSetupId); - if (setup is not null) - { - uint mtableId = (uint)setup.DefaultMotionTable; - if (mtableId != 0) - { - var mtable = capturedDats.Get(mtableId); - if (mtable is not null) - return new AcDream.Core.Physics.AnimationSequencer(setup, mtable, capturedAnimLoader); - } - // Setup exists but no motion table β€” no-op sequencer. - return new AcDream.Core.Physics.AnimationSequencer( - setup, - new DatReaderWriter.DBObjs.MotionTable(), - capturedAnimLoader); - } - } - // Complete fallback: empty setup + empty motion table + null loader. - return new AcDream.Core.Physics.AnimationSequencer( - new DatReaderWriter.DBObjs.Setup(), - new DatReaderWriter.DBObjs.MotionTable(), - new NullAnimLoader()); - } - var wbEntitySpawnAdapter = new AcDream.App.Rendering.Wb.EntitySpawnAdapter( - _textureCache!, SequencerFactory, _wbMeshAdapter!); - _wbEntitySpawnAdapter = wbEntitySpawnAdapter; - - // Phase C.1.5a/b: construct EntityScriptActivator so static entities - // (server-spawned AND dat-hydrated) fire Setup.DefaultScript through - // the PhysicsScriptRunner on enter-world. C.1.5b adds per-part - // transforms via SetupPartTransforms.Compute so multi-emitter scripts - // distribute across mesh parts (closes #56). _scriptRunner and - // _particleSink are initialised earlier in OnLoad (line ~1083); both - // are non-null here. The resolver lambda captures _dats and swallows - // dat-lookup throws β€” see C.1.5a spec Β§6 (error handling) for rationale. - AcDream.App.Rendering.Vfx.ScriptActivationInfo? ResolveActivation(AcDream.Core.World.WorldEntity e) - { - try - { - var setup = capturedDats?.Get(e.SourceGfxObjOrSetupId); - if (setup is null) return null; - uint scriptId = setup.DefaultScript.DataId; - if (scriptId == 0) return null; - var parts = AcDream.Core.Meshing.SetupPartTransforms.Compute(setup); - return new AcDream.App.Rendering.Vfx.ScriptActivationInfo(scriptId, parts); - } - catch - { - return null; - } - } - var entityScriptActivator = new AcDream.App.Rendering.Vfx.EntityScriptActivator( - _scriptRunner!, _particleSink!, ResolveActivation); - _entityScriptActivator = entityScriptActivator; - - // Phase Post-A.5 #53 (Task 12): wire EntityClassificationCache.InvalidateLandblock - // so Tier 1 cache entries get swept on LB demote (Near to Far) and unload. - // Per spec Β§5.3 W3b. The callback receives the canonical landblock id - // matching the LandblockHint stored at Populate time. - _worldState = new AcDream.App.Streaming.GpuWorldState( - wbSpawnAdapter, - wbEntitySpawnAdapter, - onLandblockUnloaded: _classificationCache.InvalidateLandblock, - entityScriptActivator: entityScriptActivator); - - _wbDrawDispatcher = new AcDream.App.Rendering.Wb.WbDrawDispatcher( - _gl, _meshShader!, _textureCache!, _wbMeshAdapter!, _wbEntitySpawnAdapter, _bindlessSupport!, - _classificationCache); - // A.5 T22.5: apply A2C gate from quality preset. - _wbDrawDispatcher.AlphaToCoverage = _resolvedQuality.AlphaToCoverage; - } + _staticMesh = new InstancedMeshRenderer(_gl, _meshShader, _textureCache); // Phase G.1 sky renderer β€” its own shader (sky.vert / sky.frag) // with depth writes off + far plane 1e6 so celestial meshes @@ -1735,7 +1430,7 @@ public sealed class GameWindow : IDisposable Path.Combine(shadersDir, "sky.vert"), Path.Combine(shadersDir, "sky.frag")); _skyRenderer = new AcDream.App.Rendering.Sky.SkyRenderer( - _gl, _dats, skyShader, _textureCache!, _samplerCache); + _gl, _dats, skyShader, _textureCache, _samplerCache); // Phase G.1 particle renderer β€” renders rain / snow / spell auras // spawned into the shared ParticleSystem as billboard quads. @@ -1743,57 +1438,27 @@ public sealed class GameWindow : IDisposable // the player. _particleRenderer = new ParticleRenderer(_gl, shadersDir, _textureCache, _dats); - // A.5 T22.5: apply radii from the already-resolved _resolvedQuality. - // _resolvedQuality was set by the quality block immediately after - // LoadAndApplyPersistedSettings() above, absorbing all env-var overrides. - // Legacy ACDREAM_STREAM_RADIUS is still honoured for backward-compat. - _nearRadius = _resolvedQuality.NearRadius; - _farRadius = _resolvedQuality.FarRadius; + // Phase A.1: replace the one-shot 3Γ—3 preload with a streaming controller. + // Parse runtime radius from environment (default 2 β†’ 5Γ—5 window). + // Values outside [0, 8] fall back to the field default of 2. + var radiusEnv = Environment.GetEnvironmentVariable("ACDREAM_STREAM_RADIUS"); + if (int.TryParse(radiusEnv, out var r) && r >= 0 && r <= 8) + _streamingRadius = r; + Console.WriteLine($"streaming: radius={_streamingRadius} (window={2*_streamingRadius+1}Γ—{2*_streamingRadius+1})"); - // Legacy override: ACDREAM_STREAM_RADIUS acts as nearRadius and - // ensures farRadius >= streamRadius. - { - var legacyEnv = Environment.GetEnvironmentVariable("ACDREAM_STREAM_RADIUS"); - if (int.TryParse(legacyEnv, out var sr) && sr >= 0) - { - _nearRadius = sr; - _streamingRadius = sr; // keep debug overlay in sync - _farRadius = System.Math.Max(sr, _farRadius); - } - } - Console.WriteLine( - $"streaming: nearRadius={_nearRadius} (window={2*_nearRadius+1}x{2*_nearRadius+1})" + - $" farRadius={_farRadius} (window={2*_farRadius+1}x{2*_farRadius+1})"); - - // Phase A.5 T11+: the streamer now runs on a dedicated worker thread. - // loadLandblock acquires _datLock (T10) before touching DatCollection. - // buildMeshOrNull (T12) receives the already-loaded LoadedLandblock so - // it can call LandblockMesh.Build without a dat read β€” _heightTable and - // _blendCtx are read-only after init, _surfaceCache is ConcurrentDictionary (T9). + // The streamer's load delegate wraps LandblockLoader.Load + stab + // hydration. Scenery + interior will land in Task 8. _streamer = new AcDream.App.Streaming.LandblockStreamer( - loadLandblock: (id, kind) => BuildLandblockForStreaming(id, kind), - buildMeshOrNull: (id, lb) => - { - if (lb is null || _heightTable is null || _blendCtx is null || _surfaceCache is null) - return null; - uint lbX = (id >> 24) & 0xFFu; - uint lbY = (id >> 16) & 0xFFu; - // _surfaceCache is ConcurrentDictionary (T9) β€” safe from worker thread. - // _heightTable and _blendCtx are read-only after initialization. - // lb.Heightmap is the pre-loaded LandBlock; no dat read needed here. - return AcDream.Core.Terrain.LandblockMesh.Build( - lb.Heightmap, lbX, lbY, _heightTable, _blendCtx, _surfaceCache); - }); + loadLandblock: id => BuildLandblockForStreaming(id)); _streamer.Start(); _streamingController = new AcDream.App.Streaming.StreamingController( - enqueueLoad: (id, kind) => _streamer.EnqueueLoad(id, kind), + enqueueLoad: _streamer.EnqueueLoad, enqueueUnload: _streamer.EnqueueUnload, drainCompletions: _streamer.DrainCompletions, applyTerrain: ApplyLoadedTerrain, state: _worldState, - nearRadius: _nearRadius, - farRadius: _farRadius, + radius: _streamingRadius, removeTerrain: id => { // Phase G.2: release any LightSources attached to entities @@ -1810,8 +1475,6 @@ public sealed class GameWindow : IDisposable _physicsEngine.RemoveLandblock(id); _cellVisibility.RemoveLandblock((id >> 16) & 0xFFFFu); }); - // A.5 T22.5: apply max-completions from resolved quality. - _streamingController.MaxCompletionsPerFrame = _resolvedQuality.MaxCompletionsPerFrame; // Phase 4.7: optional live-mode startup. Connect to the ACE server, // enter the world as the first character on the account, and stream @@ -1860,7 +1523,6 @@ public sealed class GameWindow : IDisposable _liveSession.MotionUpdated += OnLiveMotionUpdated; _liveSession.PositionUpdated += OnLivePositionUpdated; _liveSession.VectorUpdated += OnLiveVectorUpdated; - _liveSession.StateUpdated += OnLiveStateUpdated; _liveSession.TeleportStarted += OnTeleportStarted; _liveSession.AppearanceUpdated += OnLiveAppearanceUpdated; @@ -2195,18 +1857,6 @@ public sealed class GameWindow : IDisposable } } - /// - /// Door detection by server-sent name. Doors fail the generic - /// multi-frame-idle gate at line 2692 (no idle cycle), so we register - /// them via a sibling branch with a state-seeded sequencer. Shared - /// with the [door-cycle] UM dispatch diagnostic β€” both sites must - /// agree on the name predicate. - /// - private static bool IsDoorName(string? name) => name == "Door"; - - private static bool IsDoorSpawn(AcDream.Core.Net.WorldSession.EntitySpawn spawn) - => IsDoorName(spawn.Name); - private void OnLiveEntitySpawnedLocked(AcDream.Core.Net.WorldSession.EntitySpawn spawn) { _liveSpawnReceived++; @@ -2296,7 +1946,7 @@ public sealed class GameWindow : IDisposable } } - if (_dats is null) return; + if (_dats is null || _staticMesh is null) return; if (spawn.Position is null || spawn.SetupTableId is null) { // Can't place a mesh without both. Most of these are inventory @@ -2631,9 +2281,10 @@ public sealed class GameWindow : IDisposable continue; } _physicsDataCache.CacheGfxObj(mr.GfxObjId, gfx); + var subMeshes = AcDream.Core.Meshing.GfxObjMesh.Build(gfx, _dats); + _staticMesh.EnsureUploaded(mr.GfxObjId, subMeshes); if (dumpClothing) { - var subMeshes = AcDream.Core.Meshing.GfxObjMesh.Build(gfx, _dats); int tris = 0; int subs = 0; foreach (var sm in subMeshes) { tris += sm.Indices.Length / 3; subs++; } dumpClothingTotalTris += tris; @@ -2687,19 +2338,6 @@ public sealed class GameWindow : IDisposable SubPalettes: ranges); } - AcDream.Core.World.PartOverride[] entityPartOverrides; - if (animPartChanges.Count == 0) - { - entityPartOverrides = Array.Empty(); - } - else - { - entityPartOverrides = new AcDream.Core.World.PartOverride[animPartChanges.Count]; - for (int i = 0; i < animPartChanges.Count; i++) - entityPartOverrides[i] = new AcDream.Core.World.PartOverride( - animPartChanges[i].PartIndex, animPartChanges[i].NewModelId); - } - var entity = new AcDream.Core.World.WorldEntity { Id = _liveEntityIdCounter++, @@ -2709,7 +2347,6 @@ public sealed class GameWindow : IDisposable Rotation = rot, MeshRefs = meshRefs, PaletteOverride = paletteOverride, - PartOverrides = entityPartOverrides, }; var snapshot = new AcDream.Plugin.Abstractions.WorldEntitySnapshot( @@ -2878,76 +2515,6 @@ public sealed class GameWindow : IDisposable _entitySoundTables.Set(entity.Id, soundTableId); } } - else if (IsDoorSpawn(spawn) && _animLoader is not null) - { - // Phase B.4c β€” Door swing animation. Doors fail the - // multi-frame-idle gate above (no idle cycle) but DO have a - // MotionTable with On/Off cycles that ACE drives via - // UpdateMotion. Register with a seeded sequencer so the - // per-frame tick has frames to advance from frame 1 (without - // the seed, Sequencer.Advance(dt) returns no frames and the - // MeshRefs rebuild at line 7691 collapses the door to origin). - // - // Initial cycle mirrors ACE's Door.cs:43 - // (CurrentMotionState = motionClosed): Off when the door is - // closed at spawn, On when the spawn PhysicsState carries the - // ETHEREAL bit (door was already open in ACE's DB). - uint mtableId = spawn.MotionTableId ?? (uint)setup.DefaultMotionTable; - if (mtableId != 0) - { - var mtable = _dats.Get(mtableId); - if (mtable is not null) - { - var sequencer = new AcDream.Core.Physics.AnimationSequencer(setup, mtable, _animLoader); - - // Style key is `0x80000000 | stance`. ACE's MotionStance.NonCombat - // is 0x3D (61 decimal), NOT 0x01. Verified live: ACE broadcasts - // UpdateMotion with stance=0x003D and the sequencer keys cycles - // by style=0x8000003D. An earlier B.4c seed used the wrong - // 0x80000001 value, which made HasCycle always return false -> - // SetCycle never fired -> sequencer empty -> Advance returned - // no frames -> per-frame tick collapsed all door parts to the - // entity origin (visible as "door halfway in the ground"). - const uint NonCombatStyle = 0x8000003Du; - const uint MotionOn = 0x4000000Bu; // ACE MotionCommand.On (door open) - const uint MotionOff = 0x4000000Cu; // ACE MotionCommand.Off (door closed) - const uint EtherealPs = 0x4u; - // Prefer the spawn's wire-level stance if provided; else default - // to NonCombat. (Doors normally don't carry an initial MotionState - // on spawn β€” falling back to NonCombat matches ACE Door.cs:43.) - ushort spawnStance = spawn.MotionState?.Stance ?? 0; - uint initialStyle = spawnStance != 0 - ? (0x80000000u | (uint)spawnStance) - : NonCombatStyle; - uint spawnState = spawn.PhysicsState ?? 0u; - uint initialCycle = (spawnState & EtherealPs) != 0 ? MotionOn : MotionOff; - if (sequencer.HasCycle(initialStyle, initialCycle)) - sequencer.SetCycle(initialStyle, initialCycle); - - var template = new (uint, IReadOnlyDictionary?)[meshRefs.Count]; - for (int i = 0; i < meshRefs.Count; i++) - template[i] = (meshRefs[i].GfxObjId, meshRefs[i].SurfaceOverrides); - - _animatedEntities[entity.Id] = new AnimatedEntity - { - Entity = entity, - Setup = setup, - Animation = null!, // sequencer-driven; tick reads sequencer state - LowFrame = 0, - HighFrame = 0, - Framerate = 0f, - Scale = scale, - PartTemplate = template, - CurrFrame = 0, - Sequencer = sequencer, - }; - - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeBuildingEnabled) - Console.WriteLine(System.FormattableString.Invariant( - $"[door-anim] registered guid=0x{spawn.Guid:X8} entityId=0x{entity.Id:X8} mtable=0x{mtableId:X8} initialStyle=0x{initialStyle:X8} initialCycle=0x{initialCycle:X8}")); - } - } - } // Dump a summary periodically so we can see drop breakdowns without // waiting for a graceful shutdown. @@ -3125,10 +2692,6 @@ public sealed class GameWindow : IDisposable AcDream.Core.Physics.ShadowCollisionType.Cylinder, cylHeight: height, scale: 1.0f, state: state, flags: flags); - // L.2d slice 1 (2026-05-13): [entity-source] greppable from [resolve-bldg]. - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeBuildingEnabled) - Console.WriteLine(System.FormattableString.Invariant( - $"[entity-source] id=0x{entity.Id:X8} entityId=0x{entity.Id:X8} src=0x{entity.SourceGfxObjOrSetupId:X8} gfxObj=0x{entity.SourceGfxObjOrSetupId:X8} lb=0x{spawn.Position.Value.LandblockId:X8} type=Cylinder note=server-spawn-root state=0x{state:X8} flags={flags}")); } private bool RemoveLiveEntityByServerGuid(uint serverGuid, bool logDelete) @@ -3139,7 +2702,6 @@ public sealed class GameWindow : IDisposable _worldState.RemoveEntityByServerGuid(serverGuid); _worldGameState.RemoveById(existingEntity.Id); _animatedEntities.Remove(existingEntity.Id); - _classificationCache.InvalidateEntity(existingEntity.Id); _physicsEngine.ShadowObjects.Deregister(existingEntity.Id); // Dead-reckon state is keyed by SERVER guid (not local id) so we @@ -3149,8 +2711,8 @@ public sealed class GameWindow : IDisposable _liveEntityInfoByGuid.Remove(serverGuid); _entitiesByServerGuid.Remove(serverGuid); _lastSpawnByGuid.Remove(serverGuid); - if (_selectedGuid == serverGuid) - _selectedGuid = null; + if (_selectedTargetGuid == serverGuid) + _selectedTargetGuid = null; if (logDelete) _lightingSink?.UnregisterOwner(existingEntity.Id); @@ -3236,15 +2798,6 @@ public sealed class GameWindow : IDisposable $"| seq now style=0x{seqStyle:X8} motion=0x{seqMotion:X8}"); } - // Per-Door UM dispatch trail; grep [door-cycle] in launch.log to verify door animation. - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeBuildingEnabled - && _liveEntityInfoByGuid.TryGetValue(update.Guid, out var doorInfo) - && IsDoorName(doorInfo.Name)) - { - Console.WriteLine(System.FormattableString.Invariant( - $"[door-cycle] guid=0x{update.Guid:X8} stance=0x{stance:X4} cmd=0x{(command ?? 0u):X4}")); - } - // Wire server-echoed RunRate first β€” used for the player's own // locomotion tuning regardless of whether a cycle resolves. if (_playerController is not null @@ -3358,76 +2911,6 @@ public sealed class GameWindow : IDisposable { // Still update the stance echo (_playerMotionTableId, etc) via // the paths above, but don't stomp the animation sequencer. - - // B.6 slice 1 (2026-05-14): trace inbound motion for the - // local player so we can characterize what ACE sends during - // a server-initiated auto-walk. One line per inbound UM, - // gated on ACDREAM_PROBE_AUTOWALK=1. - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeAutoWalkEnabled) - { - string cmdHex = command.HasValue ? $"0x{command.Value:X4}" : "null"; - string pathStr = update.MotionState.MoveToPath is { } p - ? $"path=cell=0x{p.OriginCellId:X8},xyz=({p.OriginX:F2},{p.OriginY:F2},{p.OriginZ:F2}),minDist={p.MinDistance:F2},objDist={p.DistanceToObject:F2}" - : "path=null"; - string spd = update.MotionState.ForwardSpeed is { } fs - ? $"fwdSpd={fs:F2}" - : "fwdSpd=null"; - string mtsSpd = update.MotionState.MoveToSpeed is { } ms - ? $"mtSpd={ms:F2}" - : "mtSpd=null"; - string mtsRun = update.MotionState.MoveToRunRate is { } mr - ? $"mtRun={mr:F2}" - : "mtRun=null"; - Console.WriteLine(System.FormattableString.Invariant( - $"[autowalk-mt] stance=0x{stance:X4} cmd={cmdHex} mt=0x{update.MotionState.MovementType:X2} isMoveTo={update.MotionState.IsServerControlledMoveTo} moveTowards={update.MotionState.MoveTowards} {pathStr} {spd} {mtsSpd} {mtsRun}")); - } - - // B.6 slice 2 (2026-05-14): drive the local player's body - // through a server-initiated auto-walk when ACE sends - // MoveToObject (movement type 6) β€” retail-faithful per - // MovementManager::PerformMovement 0x00524440 case 6. When - // the inbound motion is NOT a MoveTo, cancel any active - // auto-walk (server intent changed). - if (_playerController is not null) - { - if (update.MotionState.IsServerControlledMoveTo - && update.MotionState.MoveToPath is { } pathData) - { - // Translate landblock-local origin β†’ world space. - var destWorld = AcDream.Core.Physics.RemoteMoveToDriver - .OriginToWorld( - pathData.OriginCellId, - pathData.OriginX, - pathData.OriginY, - pathData.OriginZ, - _liveCenterX, - _liveCenterY); - _playerController.BeginServerAutoWalk( - destWorld, - pathData.MinDistance, - pathData.DistanceToObject, - update.MotionState.MoveTowards, - pathData.WalkRunThreshold); - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeAutoWalkEnabled) - { - Console.WriteLine(System.FormattableString.Invariant( - $"[autowalk-begin] dest=({destWorld.X:F2},{destWorld.Y:F2},{destWorld.Z:F2}) minDist={pathData.MinDistance:F2} objDist={pathData.DistanceToObject:F2} walkRunThresh={pathData.WalkRunThreshold:F2} towards={update.MotionState.MoveTowards}")); - } - } - // Note: do NOT cancel auto-walk on a non-MoveTo motion - // arriving. The trace (2026-05-14, launch-slice2.log) - // shows ACE follows every mt=0x06 MoveToObject - // immediately with an mt=0x00 InterpretedMotionState - // (cmd=0x0007 RunForward, fwdSpd=2.86) β€” the - // companion locomotion echo, NOT a cancel. The two - // travel as separate packets but both belong to the - // same auto-walk. Cancelling on the InterpretedMotionState - // killed the auto-walk on frame 1. Arrival detection - // (inside ApplyAutoWalkOverlay) and user-input - // cancellation (same) are the two natural end paths; - // a fresh MoveToObject re-targets via BeginServerAutoWalk - // overwrite. - } } else { @@ -3985,34 +3468,6 @@ public sealed class GameWindow : IDisposable } } - /// - /// L.2g slice 1: inbound SetState (0xF74B) handler. Propagates the - /// new PhysicsState bits into ShadowObjectRegistry so the - /// existing check honors - /// the flip on the next resolver tick. Chiefly doors: - /// server flips ETHEREAL_PS = 0x4 on Use, the door's - /// cylinder collision stops blocking the threshold. - /// - private void OnLiveStateUpdated(AcDream.Core.Net.Messages.SetState.Parsed parsed) - { - // L.2g slice 1c (2026-05-13): the server addresses entities by - // ServerGuid (parsed.Guid, e.g. 0x7A9B4015), but - // ShadowObjectRegistry's cell index is keyed by local entity.Id - // (e.g. 0x000F4245). Translate via _entitiesByServerGuid before - // mutating the registry β€” otherwise the lookup misses and the - // state flip silently no-ops, leaving doors blocked even though - // ACE flipped the ETHEREAL bit. - uint registryKey = parsed.Guid; - if (_entitiesByServerGuid.TryGetValue(parsed.Guid, out var entity)) - registryKey = entity.Id; - - _physicsEngine.ShadowObjects.UpdatePhysicsState(registryKey, parsed.PhysicsState); - - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeBuildingEnabled) - Console.WriteLine(System.FormattableString.Invariant( - $"[setstate] guid=0x{parsed.Guid:X8} entityId=0x{registryKey:X8} state=0x{parsed.PhysicsState:X8} instSeq={parsed.InstanceSequence} stateSeq={parsed.StateSequence}")); - } - private static bool IsRemoteLocomotion(uint motion) { uint low = motion & 0xFFu; @@ -4287,22 +3742,6 @@ public sealed class GameWindow : IDisposable (lbY - _liveCenterY) * 192f, 0f); var worldPos = new System.Numerics.Vector3(p.PositionX, p.PositionY, p.PositionZ) + origin; - - // B.6 slice 1 (2026-05-14): trace inbound UpdatePosition cadence for - // the local player. Combined with [autowalk-mt] this answers - // whether ACE's broadcast frequency during a server-initiated - // auto-walk is dense enough to drive smooth visible motion (the - // Option C viability check from the design spec). Gated on - // ACDREAM_PROBE_AUTOWALK=1; skips remote entities. - if (update.Guid == _playerServerGuid - && AcDream.Core.Physics.PhysicsDiagnostics.ProbeAutoWalkEnabled) - { - string velStr = update.Velocity is { } v - ? $"vel=({v.X:F2},{v.Y:F2},{v.Z:F2})" - : "vel=null"; - Console.WriteLine(System.FormattableString.Invariant( - $"[autowalk-up] cell=0x{p.LandblockId:X8} pos=({p.PositionX:F2},{p.PositionY:F2},{p.PositionZ:F2}) world=({worldPos.X:F2},{worldPos.Y:F2},{worldPos.Z:F2}) {velStr} grounded={update.IsGrounded}")); - } var rot = new System.Numerics.Quaternion(p.RotationX, p.RotationY, p.RotationZ, p.RotationW); DumpMovementTruthServerEcho(update, worldPos); @@ -4319,7 +3758,7 @@ public sealed class GameWindow : IDisposable // position by adding the residual back (so the visual doesn't jerk // for one frame before the residual decay kicks in on the next tick). System.Numerics.Vector3 preSnapPos = entity.Position; - entity.SetPosition(worldPos); + entity.Position = worldPos; entity.Rotation = rot; // Commit B 2026-04-29 β€” keep the shadow registry in sync with @@ -4439,11 +3878,11 @@ public sealed class GameWindow : IDisposable if (!update.IsGrounded) { // Undo the unconditional entity hard-snap at the top of the - // function (entity.SetPosition(worldPos)): the body is mid-arc + // function (entity.Position = worldPos): the body is mid-arc // and TickAnimations will write entity = body next frame // anyway. Setting entity = body now prevents a 1-frame // teleport-to-server-then-yank-back rubber-band. - entity.SetPosition(rmState.Body.Position); + entity.Position = rmState.Body.Position; return; } @@ -4552,12 +3991,12 @@ public sealed class GameWindow : IDisposable } // Sync the visible entity to the body β€” overrides the unconditional - // entity.SetPosition(worldPos) snap at the top of this function. + // entity.Position = worldPos snap at the top of this function. // For the far-snap branch this is a no-op (body == worldPos); for // the near-enqueue branch this prevents a 1-frame teleport-then- // yank-back rubber-band as TickAnimations chases worldPos via the // queue. - entity.SetPosition(rmState.Body.Position); + entity.Position = rmState.Body.Position; return; } @@ -4689,7 +4128,7 @@ public sealed class GameWindow : IDisposable rmState.ServerVelocity); } - entity.SetPosition(rmState.Body.Position); + entity.Position = rmState.Body.Position; entity.Rotation = rmState.Body.Orientation; } @@ -4734,7 +4173,7 @@ public sealed class GameWindow : IDisposable resolved.Position.X, resolved.Position.Y, resolved.Position.Z); // 3. Snap player entity + controller. - entity.SetPosition(snappedPos); + entity.Position = snappedPos; entity.Rotation = rot; _playerController.SetPosition(snappedPos, resolved.CellId); @@ -4969,18 +4408,8 @@ public sealed class GameWindow : IDisposable /// DatReaderWriter) and pure CPU work. No GL calls here. /// /// MVP scope: stabs only. Scenery + interior added in Task 8. - /// - /// ISSUE #54 (post-A.5): far-tier loads (kind == LoadFar) skip - /// LandBlockInfo + scenery + interior hydration. They return only the - /// LandBlock heightmap dat record + an empty entity list β€” enough for - /// terrain-mesh build on the next phase. Near-tier loads run the full - /// path. This replaces Bug A's post-load entity strip in - /// with an - /// early-out at the source. /// - private AcDream.Core.World.LoadedLandblock? BuildLandblockForStreaming( - uint landblockId, - AcDream.App.Streaming.LandblockStreamJobKind kind) + private AcDream.Core.World.LoadedLandblock? BuildLandblockForStreaming(uint landblockId) { if (_dats is null) return null; @@ -4993,31 +4422,14 @@ public sealed class GameWindow : IDisposable // contention by pre-building render-thread work on the worker. lock (_datLock) { - return BuildLandblockForStreamingLocked(landblockId, kind); + return BuildLandblockForStreamingLocked(landblockId); } } - private AcDream.Core.World.LoadedLandblock? BuildLandblockForStreamingLocked( - uint landblockId, - AcDream.App.Streaming.LandblockStreamJobKind kind) + private AcDream.Core.World.LoadedLandblock? BuildLandblockForStreamingLocked(uint landblockId) { if (_dats is null) return null; - // ISSUE #54: far-tier early-out β€” heightmap only, empty entities. - // Skips the LandBlockInfo dat read AND all entity hydration (stabs - // + buildings) AND the SceneryGenerator AND interior cells. Cuts - // worker-thread cost per far-tier LB from ~tens of ms to a single - // dat read. - if (kind == AcDream.App.Streaming.LandblockStreamJobKind.LoadFar) - { - var heightmapOnly = _dats.Get(landblockId); - if (heightmapOnly is null) return null; - return new AcDream.Core.World.LoadedLandblock( - landblockId, - heightmapOnly, - System.Array.Empty()); - } - var baseLoaded = AcDream.Core.World.LandblockLoader.Load(_dats, landblockId); if (baseLoaded is null) return null; @@ -5196,7 +4608,7 @@ public sealed class GameWindow : IDisposable float localY = spawn.LocalPosition.Y; // Prefer the physics engine's terrain sampler (TerrainSurface.SampleZ) // β€” it uses the same AC2D render split-direction formula the - // TerrainModernRenderer uses for the visible terrain mesh. This + // TerrainChunkRenderer uses for the visible terrain mesh. This // guarantees trees are placed on the SAME Z height the player // walks on. If physics hasn't registered this landblock yet, // fall back to the local bilinear sample. @@ -5421,26 +4833,24 @@ public sealed class GameWindow : IDisposable } /// - /// Phase A.1 / A.5 T12: render-thread callback from StreamingController.Tick + /// Phase A.1: render-thread callback from StreamingController.Tick /// whenever a new landblock's terrain + entities are ready for GPU upload. - /// Phase A.5 T12: the worker pre-builds off the - /// render thread via ; - /// this callback no longer pays that CPU cost. + /// Mirrors the terrain-build + entity-upload part of the old preload. /// Must only be called from the render thread. /// - private void ApplyLoadedTerrain(AcDream.Core.World.LoadedLandblock lb, - AcDream.Core.Terrain.LandblockMeshData meshData) + private void ApplyLoadedTerrain(AcDream.Core.World.LoadedLandblock lb) { - if (_terrain is null || _dats is null) return; + if (_terrain is null || _dats is null || _blendCtx is null + || _heightTable is null || _surfaceCache is null) return; // Phase A.1 hotfix: render-thread path also takes the dat lock so it // doesn't race with BuildLandblockForStreaming on the worker thread. - // Hold the lock across the entity hydration below (GfxObj sub-mesh - // builds). The terrain mesh is pre-built by the worker (T12) and passed - // in via meshData, so LandblockMesh.Build no longer runs under this lock. + // Hold the lock across the entire apply because we read dats below + // (GfxObj sub-mesh builds) and mutate the shared _surfaceCache from + // LandblockMesh.Build. lock (_datLock) { - ApplyLoadedTerrainLocked(lb, meshData); + ApplyLoadedTerrainLocked(lb); } } @@ -5550,12 +4960,10 @@ public sealed class GameWindow : IDisposable _pendingCells.Add(loaded); } - private void ApplyLoadedTerrainLocked(AcDream.Core.World.LoadedLandblock lb, - AcDream.Core.Terrain.LandblockMeshData meshData) + private void ApplyLoadedTerrainLocked(AcDream.Core.World.LoadedLandblock lb) { - // _blendCtx / _surfaceCache no longer needed here (mesh pre-built by worker). - // _heightTable still needed for physics TerrainSurface below. - if (_terrain is null || _dats is null || _heightTable is null) return; + if (_terrain is null || _dats is null || _blendCtx is null + || _heightTable is null || _surfaceCache is null) return; uint lbXu = (lb.LandblockId >> 24) & 0xFFu; uint lbYu = (lb.LandblockId >> 16) & 0xFFu; @@ -5566,10 +4974,11 @@ public sealed class GameWindow : IDisposable (lbY - _liveCenterY) * 192f, 0f); - // Phase A.5 T15/T16: route through AddLandblockWithMesh β€” the named - // two-tier entry point. Delegates to AddLandblock internally; both - // paths share one GPU upload path. - _terrain.AddLandblockWithMesh(lb.LandblockId, meshData, origin); + // Build terrain mesh data on the render thread (pure CPU; acceptable + // for the MVP; a future pass can move it to the worker thread). + var meshData = AcDream.Core.Terrain.LandblockMesh.Build( + lb.Heightmap, lbXu, lbYu, _heightTable, _blendCtx, _surfaceCache); + _terrain.AddLandblock(lb.LandblockId, meshData, origin); // Step 4: drain pending LoadedCells from the worker thread. while (_pendingCells.TryTake(out var cell)) @@ -5692,25 +5101,44 @@ public sealed class GameWindow : IDisposable portalPlanes, origin.X, origin.Y); } - // N.5: WbMeshAdapter.Tick() handles GPU upload for all GfxObj meshes via - // ObjectMeshManager.PrepareMeshDataAsync. The legacy EnsureUploaded loop - // (and _pendingCellMeshes drain) are retired with InstancedMeshRenderer. - // Cache GfxObj physics data (BSP trees) for the physics engine β€” this - // loop is physics-only, not renderer-side. - foreach (var entity in lb.Entities) + // Upload every GfxObj referenced by this landblock's entities. + // EnsureUploaded is idempotent so duplicates across landblocks are free. + if (_staticMesh is not null) { - foreach (var meshRef in entity.MeshRefs) + // Task 8: drain any pending EnvCell room-mesh sub-meshes first. + // The worker thread pre-built these CPU-side and stored them in + // _pendingCellMeshes. We must upload them here (render thread) before + // the per-MeshRef loop below tries to look them up via GfxObjMesh.Build, + // which would fail because EnvCell ids (0xAAAA01xx) aren't real GfxObj + // dat ids. EnsureUploaded is idempotent so calling it here then seeing + // the same id again in the loop below is safe. + foreach (var entity in lb.Entities) { - if ((meshRef.GfxObjId & 0xFF000000u) != 0x01000000u) continue; - var gfx = _dats.Get(meshRef.GfxObjId); - if (gfx is null) continue; - _physicsDataCache.CacheGfxObj(meshRef.GfxObjId, gfx); + foreach (var meshRef in entity.MeshRefs) + { + if (_pendingCellMeshes.TryRemove(meshRef.GfxObjId, out var cellSubMeshes)) + _staticMesh.EnsureUploaded(meshRef.GfxObjId, cellSubMeshes); + } + } + + // Now upload regular GfxObj sub-meshes (stabs, scenery, interior stabs). + // Skip any ids already uploaded (includes the cell meshes just drained). + foreach (var entity in lb.Entities) + { + foreach (var meshRef in entity.MeshRefs) + { + // Skip EnvCell synthetic ids β€” already handled above (or already + // uploaded on a prior tick). GfxObj ids are 0x01xxxxxx; Setup ids + // are 0x02xxxxxx; anything else is not a GfxObj dat record. + if ((meshRef.GfxObjId & 0xFF000000u) != 0x01000000u) continue; + var gfx = _dats.Get(meshRef.GfxObjId); + if (gfx is null) continue; + _physicsDataCache.CacheGfxObj(meshRef.GfxObjId, gfx); + var subMeshes = AcDream.Core.Meshing.GfxObjMesh.Build(gfx, _dats); + _staticMesh.EnsureUploaded(meshRef.GfxObjId, subMeshes); + } } } - // Drain _pendingCellMeshes to prevent unbounded accumulation. - // The data is no longer consumed (WB handles EnvCell geometry through - // its own pipeline), but the worker thread still populates this dict. - _pendingCellMeshes.Clear(); // Task 7: register static entities into the ShadowObjectRegistry so the // Transition system can find and collide against them during movement. @@ -5812,14 +5240,6 @@ public sealed class GameWindow : IDisposable origin.X, origin.Y, lb.LandblockId, AcDream.Core.Physics.ShadowCollisionType.BSP, 0f, partScale); - // L.2d slice 1 (2026-05-13): [entity-source] greppable from [resolve-bldg]. - // partCached?.BSP?.Root non-null was checked above (else `continue`), - // so hasPhys=true on this path. - // state/flags literals: landblock-baked scenery has no server PhysicsState - // broadcast and no PWD bitfield; defaults match static-solid semantics. - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeBuildingEnabled) - Console.WriteLine(System.FormattableString.Invariant( - $"[entity-source] id=0x{partId:X8} entityId=0x{entity.Id:X8} src=0x{entity.SourceGfxObjOrSetupId:X8} gfxObj=0x{meshRef.GfxObjId:X8} lb=0x{lb.LandblockId:X8} type=BSP note=partIdx={partIndex} hasPhys=true state=0x{0u:X8} flags={AcDream.Core.Physics.EntityCollisionFlags.None}")); entityBsp++; partIndex++; @@ -5871,11 +5291,6 @@ public sealed class GameWindow : IDisposable entity.Rotation, cylRadius, origin.X, origin.Y, lb.LandblockId, AcDream.Core.Physics.ShadowCollisionType.Cylinder, cylHeight); - // L.2d slice 1 (2026-05-13): [entity-source] greppable from [resolve-bldg]. - // state/flags literals: landblock-baked scenery; no server PhysicsState. - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeBuildingEnabled) - Console.WriteLine(System.FormattableString.Invariant( - $"[entity-source] id=0x{shapeId:X8} entityId=0x{entity.Id:X8} src=0x{entity.SourceGfxObjOrSetupId:X8} gfxObj=0x{entity.SourceGfxObjOrSetupId:X8} lb=0x{lb.LandblockId:X8} type=Cylinder note=setup-cylsphere#{ci} state=0x{0u:X8} flags={AcDream.Core.Physics.EntityCollisionFlags.None}")); entityCyl++; } @@ -5906,11 +5321,6 @@ public sealed class GameWindow : IDisposable entity.Rotation, sphRadius, origin.X, origin.Y, lb.LandblockId, AcDream.Core.Physics.ShadowCollisionType.Cylinder, sphHeight); - // L.2d slice 1 (2026-05-13): [entity-source] greppable from [resolve-bldg]. - // state/flags literals: landblock-baked scenery; no server PhysicsState. - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeBuildingEnabled) - Console.WriteLine(System.FormattableString.Invariant( - $"[entity-source] id=0x{shapeId:X8} entityId=0x{entity.Id:X8} src=0x{entity.SourceGfxObjOrSetupId:X8} gfxObj=0x{entity.SourceGfxObjOrSetupId:X8} lb=0x{lb.LandblockId:X8} type=Cylinder note=setup-sphere#{si} state=0x{0u:X8} flags={AcDream.Core.Physics.EntityCollisionFlags.None}")); entityCyl++; } } @@ -5929,11 +5339,6 @@ public sealed class GameWindow : IDisposable entity.Position, entity.Rotation, fr, origin.X, origin.Y, lb.LandblockId, AcDream.Core.Physics.ShadowCollisionType.Cylinder, fh); - // L.2d slice 1 (2026-05-13): [entity-source] greppable from [resolve-bldg]. - // state/flags literals: landblock-baked scenery; no server PhysicsState. - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeBuildingEnabled) - Console.WriteLine(System.FormattableString.Invariant( - $"[entity-source] id=0x{shapeId:X8} entityId=0x{entity.Id:X8} src=0x{entity.SourceGfxObjOrSetupId:X8} gfxObj=0x{entity.SourceGfxObjOrSetupId:X8} lb=0x{lb.LandblockId:X8} type=Cylinder note=setup-radius-fallback state=0x{0u:X8} flags={AcDream.Core.Physics.EntityCollisionFlags.None}")); entityCyl++; } } @@ -6115,11 +5520,6 @@ public sealed class GameWindow : IDisposable baseCenter, entity.Rotation, cylRadius, origin.X, origin.Y, lb.LandblockId, AcDream.Core.Physics.ShadowCollisionType.Cylinder, cylHeight); - // L.2d slice 1 (2026-05-13): [entity-source] greppable from [resolve-bldg]. - // state/flags literals: landblock-baked scenery; no server PhysicsState. - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeBuildingEnabled) - Console.WriteLine(System.FormattableString.Invariant( - $"[entity-source] id=0x{entity.Id:X8} entityId=0x{entity.Id:X8} src=0x{entity.SourceGfxObjOrSetupId:X8} gfxObj=0x{entity.SourceGfxObjOrSetupId:X8} lb=0x{lb.LandblockId:X8} type=Cylinder note=mesh-aabb-fallback state=0x{0u:X8} flags={AcDream.Core.Physics.EntityCollisionFlags.None}")); entityCyl++; if (_isScenery) scRegistered++; } @@ -6369,7 +5769,7 @@ public sealed class GameWindow : IDisposable // the physics-resolved location each frame. if (_entitiesByServerGuid.TryGetValue(_playerServerGuid, out var pe)) { - pe.SetPosition(result.RenderPosition); // A.5 T18: SetPosition propagates AabbDirty + pe.Position = result.RenderPosition; pe.Rotation = System.Numerics.Quaternion.CreateFromAxisAngle( System.Numerics.Vector3.UnitZ, _playerController.Yaw - MathF.PI / 2f); @@ -6648,16 +6048,6 @@ public sealed class GameWindow : IDisposable _gl!.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); - // Phase N.6 slice 1: one-shot surface-format histogram dump under - // ACDREAM_DUMP_SURFACES=1. Zero cost when off. - _textureCache?.TickSurfaceHistogramDumpIfEnabled(); - - // Phase N.4: drain WB pipeline queues (staged mesh data + - // GL thread queue). Must happen before any draw work so that - // resources uploaded this frame are available immediately. - // No-op when ACDREAM_USE_WB_FOUNDATION is off (_wbMeshAdapter is null). - _wbMeshAdapter?.Tick(); - // Phase D.2a β€” begin ImGui frame. Paired with the Render() call // after the scene draws (below). ImGuiController.Update() // consumes buffered Silk.NET input events and calls ImGui.NewFrame. @@ -6777,28 +6167,6 @@ public sealed class GameWindow : IDisposable Lighting.Tick(camPos); var ubo = AcDream.Core.Lighting.SceneLightingUbo.Build( Lighting, in atmo, camPos, (float)WorldTime.DayFraction); - - // A.5 T22: override fog ramp with N₁/Nβ‚‚-derived distances so the - // horizon fog masks the N₁ scenery boundary. Sky keyframe fog is - // retail-accurate at normal view distances but far too short for - // the extended Nβ‚‚=12 (25Γ—25 LB) streaming window. - // FogStart = N₁ Γ— 192m Γ— 0.7 β‰ˆ 538m at defaults (4/12). - // FogEnd = Nβ‚‚ Γ— 192m Γ— 0.95 β‰ˆ 2188m at defaults. - // Multipliers exposed as env vars for fast iteration at visual gate. - { - const float LandblockSize = 192.0f; - float startMult = ParseEnvFloat("ACDREAM_FOG_START_MULT", 0.7f); - float endMult = ParseEnvFloat("ACDREAM_FOG_END_MULT", 0.95f); - float fogStart = _nearRadius * LandblockSize * startMult; - float fogEnd = _farRadius * LandblockSize * endMult; - // Preserve fog color (xyz), lightning flash (z), and mode (w). - ubo.FogParams = new System.Numerics.Vector4( - fogStart, - fogEnd, - ubo.FogParams.Z, // lightning flash β€” unchanged - ubo.FogParams.W); // fog mode β€” unchanged - } - _sceneLightingUbo?.Upload(ubo); // Never cull the landblock the player is currently on. @@ -6844,19 +6212,7 @@ public sealed class GameWindow : IDisposable goto SkipWorldGeometry; } - // Phase N.5b: wrap Draw in CPU stopwatch for [TERRAIN-DIAG] rollup - // (gated on ACDREAM_WB_DIAG=1, same env var as [WB-DIAG]). Stopwatch - // is cheap; only the periodic Console.WriteLine is gated. - _terrainCpuStopwatch.Restart(); _terrain?.Draw(camera, frustum, neverCullLandblockId: playerLb); - _terrainCpuStopwatch.Stop(); - // Multiply by 100 then divide by 100 in the diag print to keep - // 0.01 Β΅s precision in the long-typed sample buffer. Terrain Draw - // is sub-microsecond on simple scenes; truncating to integer Β΅s - // would round nearly every sample to 0. - _terrainCpuSamples[_terrainCpuSampleCursor] = (long)(_terrainCpuStopwatch.Elapsed.TotalMicroseconds * 100.0); - _terrainCpuSampleCursor = (_terrainCpuSampleCursor + 1) % _terrainCpuSamples.Length; - MaybeFlushTerrainDiag(); // Conditional depth clear: when camera is inside a building, clear // depth (not color) so interior geometry writes fresh Z values on top @@ -6881,8 +6237,7 @@ public sealed class GameWindow : IDisposable animatedIds.Add(k); } - // N.5: WbDrawDispatcher is always non-null (modern path mandatory). - _wbDrawDispatcher!.Draw(camera, _worldState.LandblockEntries, frustum, + _staticMesh?.Draw(camera, _worldState.LandblockEntries, frustum, neverCullLandblockId: playerLb, visibleCellIds: visibility?.VisibleCellIds, animatedEntityIds: animatedIds); @@ -7111,11 +6466,6 @@ public sealed class GameWindow : IDisposable } _panelHost.RenderAll(ctx); - // B.7 Vivid Target Indicator: draws corner triangles to the - // ImGui background draw list so it appears behind any docked - // panels but still over the 3D scene. Cheap when no - // selection β€” internal early-return on null guid. - _targetIndicator?.Render(); _imguiBootstrap.Render(); } @@ -7409,7 +6759,7 @@ public sealed class GameWindow : IDisposable rm.MaxSeqSpeedSinceLastUP = seqSpeedNow; } - ae.Entity.SetPosition(rm.Body.Position); // A.5 T18: SetPosition propagates AabbDirty + ae.Entity.Position = rm.Body.Position; ae.Entity.Rotation = rm.Body.Orientation; } else @@ -7737,7 +7087,7 @@ public sealed class GameWindow : IDisposable } } - ae.Entity.SetPosition(rm.Body.Position); // A.5 T18: SetPosition propagates AabbDirty + ae.Entity.Position = rm.Body.Position; ae.Entity.Rotation = rm.Body.Orientation; } } @@ -7841,19 +7191,9 @@ public sealed class GameWindow : IDisposable { int seqCount = seqFrames?.Count ?? -1; int setupParts = ae.Setup.Parts.Count; - // #62: B.4c introduced `Animation = null!` for sequencer-driven - // door entities (parallel branch sites at line 2867 + 7892). - // Doors don't currently enter _remoteDeadReckon, so this - // path isn't reachable for them today β€” but the read was - // unguarded and would NRE the moment something flips. - // Local-var + `is not null` keeps the guard scoped: the - // legacy slerp branch below treats ae.Animation as non- - // null (per its non-nullable declared type) unchanged. - var animMaybeNull = ae.Animation; - int animFrame0Parts = - animMaybeNull is not null && animMaybeNull.PartFrames.Count > 0 - ? animMaybeNull.PartFrames[0].Frames.Count - : -1; + int animFrame0Parts = ae.Animation.PartFrames.Count > 0 + ? ae.Animation.PartFrames[0].Frames.Count + : -1; double seqHash = 0.0; if (seqFrames is not null) { @@ -8034,11 +7374,7 @@ public sealed class GameWindow : IDisposable // we always want it animated in player mode. if (!_animatedEntities.TryGetValue(pe.Id, out var ae)) { - // A.5 T10: lock around _dats.Get β€” worker thread may be - // building a landblock mesh concurrently. DatBinReader's - // shared buffer position would corrupt without serialization. - DatReaderWriter.DBObjs.Setup? setup; - lock (_datLock) { setup = _dats.Get(pe.SourceGfxObjOrSetupId); } + var setup = _dats.Get(pe.SourceGfxObjOrSetupId); if (setup is null) return; _physicsDataCache.CacheSetup(pe.SourceGfxObjOrSetupId, setup); @@ -8496,111 +7832,6 @@ public sealed class GameWindow : IDisposable } } - /// - /// A.5 T22.5: apply a new quality preset mid-session (called from the - /// Settings panel Save path when - /// changes). - /// - /// - /// What changes immediately: - /// - /// Streaming radii: disposes the old - /// + - /// and constructs new ones with the new radii. - /// Anisotropic filtering: calls - /// TerrainAtlas.SetAnisotropic. - /// Alpha-to-coverage gate: sets - /// WbDrawDispatcher.AlphaToCoverage. - /// Max completions per frame: updates - /// StreamingController.MaxCompletionsPerFrame. - /// - /// - /// - /// - /// What requires a restart: - /// MSAA samples are baked into the GL context via WindowOptions.Samples - /// at window creation time and cannot change at runtime. If the new preset - /// would change MsaaSamples, a warning is logged and MSAA is left - /// at its current level until the next launch. - /// - /// - public void ReapplyQualityPreset(AcDream.UI.Abstractions.Settings.QualityPreset newPreset) - { - var newBase = AcDream.UI.Abstractions.Settings.QualitySettings.From(newPreset); - var newResolved = AcDream.UI.Abstractions.Settings.QualitySettings.WithEnvOverrides(newBase); - - Console.WriteLine($"[QUALITY] ReapplyQualityPreset: {newPreset} β†’ {newResolved}"); - - // MSAA samples cannot change at runtime β€” warn if preset would differ. - if (newResolved.MsaaSamples != _resolvedQuality.MsaaSamples) - { - Console.WriteLine( - $"[QUALITY] MSAA samples change ({_resolvedQuality.MsaaSamples} β†’ " + - $"{newResolved.MsaaSamples}) requires a restart β€” skipped for this session."); - } - - _resolvedQuality = newResolved; - - // A2C gate β€” immediate toggle, no GL context restart needed. - if (_wbDrawDispatcher is not null) - _wbDrawDispatcher.AlphaToCoverage = newResolved.AlphaToCoverage; - - // Anisotropic β€” immediate GL TexParameter call on the terrain atlas. - _terrain?.Atlas?.SetAnisotropic(newResolved.AnisotropicLevel); - - // Streaming radii β€” requires tearing down + rebuilding the controller - // (radii are constructor-time on StreamingController, not live-mutable). - // The ~1-2s hitch while the worker drains is acceptable for a settings change. - if (_streamer is not null && _streamingController is not null) - { - _nearRadius = newResolved.NearRadius; - _farRadius = newResolved.FarRadius; - - // StreamingController is stateless (no Dispose needed); dispose - // only the LandblockStreamer worker thread. - _streamer.Dispose(); - - _streamer = new AcDream.App.Streaming.LandblockStreamer( - loadLandblock: (id, kind) => BuildLandblockForStreaming(id, kind), - buildMeshOrNull: (id, lb) => - { - if (lb is null || _heightTable is null || _blendCtx is null || _surfaceCache is null) - return null; - uint lbX = (id >> 24) & 0xFFu; - uint lbY = (id >> 16) & 0xFFu; - return AcDream.Core.Terrain.LandblockMesh.Build( - lb.Heightmap, lbX, lbY, _heightTable, _blendCtx, _surfaceCache); - }); - _streamer.Start(); - - _streamingController = new AcDream.App.Streaming.StreamingController( - enqueueLoad: (id, kind) => _streamer.EnqueueLoad(id, kind), - enqueueUnload: _streamer.EnqueueUnload, - drainCompletions: _streamer.DrainCompletions, - applyTerrain: ApplyLoadedTerrain, - state: _worldState, - nearRadius: _nearRadius, - farRadius: _farRadius, - removeTerrain: id => - { - if (_lightingSink is not null && - _worldState.TryGetLandblock(id, out var lb)) - { - foreach (var ent in lb!.Entities) - _lightingSink.UnregisterOwner(ent.Id); - } - _terrain?.RemoveLandblock(id); - _physicsEngine.RemoveLandblock(id); - _cellVisibility.RemoveLandblock((id >> 16) & 0xFFFFu); - }); - _streamingController.MaxCompletionsPerFrame = newResolved.MaxCompletionsPerFrame; - - Console.WriteLine( - $"[QUALITY] Streaming restarted: nearRadius={_nearRadius}, " + - $"farRadius={_farRadius}, maxCompletions={newResolved.MaxCompletionsPerFrame}"); - } - } - /// /// L.0 Display tab: framebuffer-resize handler β€” update GL viewport /// + camera aspect when the window is resized (by the user dragging @@ -8789,10 +8020,8 @@ public sealed class GameWindow : IDisposable // Every other action fires on Press only (no Release / Hold side- // effects in the K.1b set). Filter out non-Press activations early // so subscribers that have Release-mode bindings don't accidentally - // re-fire. B.4b exception: DoubleClick must pass through so - // SelectDblLeft / SelectDblRight / SelectDblMid can reach the switch. - if (activation != AcDream.UI.Abstractions.Input.ActivationType.Press - && activation != AcDream.UI.Abstractions.Input.ActivationType.DoubleClick) return; + // re-fire. + if (activation != AcDream.UI.Abstractions.Input.ActivationType.Press) return; // K-fix1 (2026-04-26): Q is autorun TOGGLE, not hold-to-run. Press // Q to start, press Q again to stop. Pressing Backup / Stop / @@ -8903,25 +8132,6 @@ public sealed class GameWindow : IDisposable SendLiveCombatAttack(AcDream.Core.Combat.CombatAttackAction.High); break; - case AcDream.UI.Abstractions.Input.InputAction.SelectLeft: - PickAndStoreSelection(useImmediately: false); - break; - - case AcDream.UI.Abstractions.Input.InputAction.SelectDblLeft: - PickAndStoreSelection(useImmediately: true); - break; - - case AcDream.UI.Abstractions.Input.InputAction.UseSelected: - UseCurrentSelection(); - break; - - case AcDream.UI.Abstractions.Input.InputAction.SelectionPickUp: - if (_selectedGuid is uint pickupTarget) - SendPickUp(pickupTarget); - else - _debugVm?.AddToast("Nothing selected"); - break; - case AcDream.UI.Abstractions.Input.InputAction.EscapeKey: if (_cameraController?.IsFlyMode == true) _cameraController.ToggleFly(); // exit fly, release cursor @@ -8990,479 +8200,12 @@ public sealed class GameWindow : IDisposable private uint? GetSelectedOrClosestCombatTarget() { - if (_selectedGuid is { } selected && IsLiveCreatureTarget(selected)) + if (_selectedTargetGuid is { } selected && IsLiveCreatureTarget(selected)) return selected; return SelectClosestCombatTarget(showToast: false); } - // ============================================================ - // Phase B.4b β€” outbound Use handler. Wires three input actions - // (LMB click select, LMB-double-click select+use, R hotkey - // use-selected) through WorldPicker into InteractRequests.BuildUse. - // The inbound reply (SetState 0xF74B) lands via L.2g slice 1. - // ============================================================ - - private void PickAndStoreSelection(bool useImmediately) - { - if (_cameraController is null || _window is null) return; - - var camera = _cameraController.Active; - var (origin, direction) = AcDream.Core.Selection.WorldPicker.BuildRay( - mouseX: _lastMouseX, mouseY: _lastMouseY, - viewportW: _window.Size.X, viewportH: _window.Size.Y, - view: camera.View, projection: camera.Projection); - - if (direction.LengthSquared() < 1e-6f) return; // degenerate ray - - var picked = AcDream.Core.Selection.WorldPicker.Pick( - origin, direction, - _entitiesByServerGuid.Values, - skipServerGuid: _playerServerGuid, - maxDistance: 50f, - // B.7 (2026-05-15): widen the pick sphere for large flat - // objects (doors, lifestones, portals, corpses) so their - // visible surface stays clickable even though the entity - // origin is a single point. 0.7 m default is fine for - // humanoids and most items; doors / portals need ~2 m - // to cover the doorframe. - // - // 2026-05-15 sign-class extension: post-mounted scenery - // (Holtburg town sign etc.) needs the sphere TALLER than - // wider. We classify "non-creature, non-flat, non-small-item" - // as tall scenery and grow the sphere to 1.6 m radius lifted - // to 1.5 m vertical offset β€” covers a 3 m post from - // ground to top. Mirrors TargetIndicatorPanel.EntityHeightFor's - // 3 m default so the click sphere matches the visible box. - radiusForGuid: g => - { - if (_lastSpawnByGuid.TryGetValue(g, out var s) - && s.ObjectDescriptionFlags is { } odf) - { - // BF_DOOR = 0x1000, BF_LIFESTONE = 0x4000, - // BF_PORTAL = 0x40000, BF_CORPSE = 0x2000 - // (acclient.h:6431-6463) - const uint LargeFlatMask = 0x1000u | 0x4000u | 0x40000u | 0x2000u; - if ((odf & LargeFlatMask) != 0) return 2.0f; - } - if (IsTallSceneryGuid(g)) return 1.6f; - // 1.0 m sphere centred at chest height (see - // verticalOffsetForGuid) covers a 1.8 m humanoid from - // shin to crown without overlapping neighbours. - return 1.0f; - }, - verticalOffsetForGuid: g => - { - // Lift the pick sphere to mid-body so chest/head clicks - // hit instead of missing past the top of a feet-anchored - // sphere. WorldEntity.Position is at feet level - // (Z=ground); humanoids reach ~1.8 m, items sit close - // to the ground (~0.2 m above their feet). - if (_liveEntityInfoByGuid.TryGetValue(g, out var info) - && (info.ItemType & AcDream.Core.Items.ItemType.Creature) != 0) - return 0.9f; // humanoid mid-body - if (_lastSpawnByGuid.TryGetValue(g, out var s) - && s.ObjectDescriptionFlags is { } odf) - { - const uint LargeFlatMask = 0x1000u | 0x4000u | 0x40000u | 0x2000u; - if ((odf & LargeFlatMask) != 0) return 1.0f; // mid-door - } - if (IsTallSceneryGuid(g)) return 1.5f; // mid-pole height - return 0.2f; // small ground item β€” sphere just above feet - }); - - if (picked is uint guid) - { - _selectedGuid = guid; - string label = DescribeLiveEntity(guid); - Console.WriteLine($"[B.4b] pick guid=0x{guid:X8} name={label}"); - // B.7 (2026-05-15): one-shot per-pick diagnostic so we can - // see exactly which ItemType + PWD bitfield bits + resolved - // RadarBlipColor are produced for the just-picked entity. - // Helps verify whether a "green NPC" really is flagged as - // Vendor server-side or whether our lookup is wrong. - uint rawItemType = 0; - if (_liveEntityInfoByGuid.TryGetValue(guid, out var info)) - rawItemType = (uint)info.ItemType; - uint pwdBits = 0; - uint? pickUseability = null; - float? pickUseRadius = null; - float pickScale = 1f; - uint? pickSetupId = null; - if (_lastSpawnByGuid.TryGetValue(guid, out var spawn)) - { - if (spawn.ObjectDescriptionFlags is { } odf) pwdBits = odf; - pickUseability = spawn.Useability; - pickUseRadius = spawn.UseRadius; - pickScale = spawn.ObjScale ?? 1f; - pickSetupId = spawn.SetupTableId; - } - var col = AcDream.Core.Ui.RadarBlipColors.For(rawItemType, pwdBits); - string useStr = pickUseability.HasValue ? $"0x{pickUseability.Value:X4}" : "null"; - string radStr = pickUseRadius.HasValue ? pickUseRadius.Value.ToString("F2", System.Globalization.CultureInfo.InvariantCulture) : "null"; - string setupStr = pickSetupId.HasValue ? $"0x{pickSetupId.Value:X8}" : "null"; - Console.WriteLine(System.FormattableString.Invariant( - $"[B.7] pick-info guid=0x{guid:X8} itemType=0x{rawItemType:X8} pwd=0x{pwdBits:X8} use={useStr} useRadius={radStr} scale={pickScale:F2} setup={setupStr} tallScenery={IsTallSceneryGuid(guid)} color=({col.R},{col.G},{col.B})")); - _debugVm?.AddToast($"Selected: {label}"); - if (useImmediately) SendUse(guid); - } - else - { - _debugVm?.AddToast("Nothing to select"); - } - } - - private void UseCurrentSelection() - { - if (_selectedGuid is not uint sel) - { - _debugVm?.AddToast("Nothing selected"); - return; - } - - // 2026-05-16 β€” R is conceptually "use." It smart-routes to - // pickup as a downstream optimization (see the isPickupableItem - // dispatch below), but the GATE is always IsUseableTarget β€” - // what retail's UseObject would do. - // Retail string at acclient_2013_pseudo_c.txt:1033115 - // (data_7e2a70): "The %s cannot be used". - if (!IsUseableTarget(sel)) - { - string label = DescribeLiveEntity(sel); - _debugVm?.AddToast(AcDream.Core.Ui.RetailMessages.CannotBeUsed(label)); - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeAutoWalkEnabled) - Console.WriteLine($"[B.4b] R-key ignored β€” not useable guid=0x{sel:X8}"); - return; - } - - // B.7 (2026-05-15): the user requested R behave as a universal - // interact key β€” pickup for items, use for NPCs / doors / - // lifestones / portals / corpses. Matches retail's "use" - // behaviour where the action picked depends on the target's - // type rather than forcing the player to remember a different - // hotkey per target type. - bool isPickupableItem = true; - if (_liveEntityInfoByGuid.TryGetValue(sel, out var info) - && (info.ItemType & AcDream.Core.Items.ItemType.Creature) != 0) - { - // NPCs / monsters / players are Use targets, never PickUp. - isPickupableItem = false; - } - if (_lastSpawnByGuid.TryGetValue(sel, out var spawn) - && spawn.ObjectDescriptionFlags is { } odf) - { - // BF_DOOR | BF_LIFESTONE | BF_PORTAL | BF_CORPSE β†’ Use, not PickUp. - // (acclient.h:6431-6463) - const uint NonPickupMask = 0x1000u | 0x4000u | 0x40000u | 0x2000u; - if ((odf & NonPickupMask) != 0) isPickupableItem = false; - } - - if (isPickupableItem) SendPickUp(sel); - else SendUse(sel); - } - - private void SendUse(uint guid, bool isRetryAfterArrival = false) - { - if (_liveSession is null - || _liveSession.CurrentState != AcDream.Core.Net.WorldSession.State.InWorld) - { - _debugVm?.AddToast("Not in world"); - return; - } - // 2026-05-15: defense-in-depth useability gate. Double-click flows - // directly through SendUse without passing UseCurrentSelection's - // dispatcher gate, so re-check here. Silent ignore matches retail - // (acclient.h:6478 ITEM_USEABLE β€” USEABLE_REMOTE bit required). - // isRetryAfterArrival bypasses the gate because we only retry an - // action we previously gated through. - if (!isRetryAfterArrival && !IsUseableTarget(guid)) - { - // Retail-style client-side toast for unusable targets - // (signs, decorative scenery with USEABLE_NO / USEABLE_UNDEF). - // Retail string at acclient_2013_pseudo_c.txt:1033115 - // (data_7e2a70): "The %s cannot be used" (no trailing period). - string label = DescribeLiveEntity(guid); - _debugVm?.AddToast(AcDream.Core.Ui.RetailMessages.CannotBeUsed(label)); - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeAutoWalkEnabled) - Console.WriteLine($"[B.4b] SendUse ignored β€” not useable guid=0x{guid:X8}"); - return; - } - // B.6 (2026-05-15): install a speculative auto-walk on the local - // player toward the target. For far targets ACE will overwrite - // this with its own MovementType=6 wire payload (and a better - // wire-supplied use-radius). For close-range targets ACE skips - // MoveToChain entirely and just rotates server-side; our - // overlay provides the matching local rotation. Either way the - // alignment-gated arrival ensures the body finishes facing - // the target before stopping. - // - // User feedback (2026-05-15): 'first is rotation, when you are - // facing, then using.' For close-range we DEFER the wire packet - // until our local overlay arrives (turn-then-fire). The - // _pendingPostArrivalAction handler will re-fire SendUse with - // isRetryAfterArrival=true after the body finishes turning. - // For far range we still send immediately so ACE can start - // its MoveToChain. - if (!isRetryAfterArrival) - { - InstallSpeculativeTurnToTarget(guid); - _pendingPostArrivalAction = (guid, false); - if (IsCloseRangeTarget(guid)) - { - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeAutoWalkEnabled) - Console.WriteLine($"[B.4b] use deferred (close-range, turn-first) guid=0x{guid:X8}"); - return; // wait for AutoWalkArrived to fire the wire send - } - } - - var seq = _liveSession.NextGameActionSequence(); - var body = AcDream.Core.Net.Messages.InteractRequests.BuildUse(seq, guid); - _liveSession.SendGameAction(body); - Console.WriteLine($"[B.4b] use guid=0x{guid:X8} seq={seq}"); - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeAutoWalkEnabled) - { - string label = DescribeLiveEntity(guid); - Console.WriteLine(System.FormattableString.Invariant( - $"[autowalk-out] op=use target=0x{guid:X8} name=\"{label}\" seq={seq}")); - } - // Remember this action so OnAutoWalkArrivedReSendAction can - // re-fire it close-range. Skip when this IS the re-send. - if (!isRetryAfterArrival) - _pendingPostArrivalAction = (guid, false); - } - - private void SendPickUp(uint itemGuid, bool isRetryAfterArrival = false) - { - if (_liveSession is null - || _liveSession.CurrentState != AcDream.Core.Net.WorldSession.State.InWorld) - { - _debugVm?.AddToast("Not in world"); - return; - } - - // Creature-pickup block (with retail toast). Comes BEFORE the - // generic IsPickupableTarget gate so creatures get the specific - // "cannot pick up creatures!" message instead of the generic - // "can't be picked up!". - // Retail string acclient_2013_pseudo_c.txt:401642 (data_7e22b4). - if (!isRetryAfterArrival && IsLiveCreatureTarget(itemGuid)) - { - _debugVm?.AddToast(AcDream.Core.Ui.RetailMessages.CannotPickUpCreatures); - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeAutoWalkEnabled) - Console.WriteLine($"[B.5] SendPickUp ignored β€” creature item=0x{itemGuid:X8}"); - return; - } - - // Generic non-pickupable gate (signs, banners, decorative scenery). - // Retail string acclient_2013_pseudo_c.txt:401589 (sprintf - // "The %s can't be picked up!"). - if (!isRetryAfterArrival && !IsPickupableTarget(itemGuid)) - { - string label = DescribeLiveEntity(itemGuid); - _debugVm?.AddToast(AcDream.Core.Ui.RetailMessages.CantBePickedUp(label)); - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeAutoWalkEnabled) - Console.WriteLine($"[B.5] SendPickUp ignored β€” not pickupable item=0x{itemGuid:X8}"); - return; - } - // B.6 (2026-05-15): same speculative turn-to-target + deferral as - // SendUse β€” close-range pickup rotates locally to face the - // item first, then the wire packet fires when the local - // overlay reports arrival. - if (!isRetryAfterArrival) - { - InstallSpeculativeTurnToTarget(itemGuid); - _pendingPostArrivalAction = (itemGuid, true); - if (IsCloseRangeTarget(itemGuid)) - { - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeAutoWalkEnabled) - Console.WriteLine($"[B.5] pickup deferred (close-range, turn-first) item=0x{itemGuid:X8}"); - return; - } - } - - var seq = _liveSession.NextGameActionSequence(); - var body = AcDream.Core.Net.Messages.InteractRequests.BuildPickUp( - seq, itemGuid, _playerServerGuid, placement: 0); - _liveSession.SendGameAction(body); - Console.WriteLine($"[B.5] pickup item=0x{itemGuid:X8} container=0x{_playerServerGuid:X8} seq={seq}"); - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeAutoWalkEnabled) - { - string label = DescribeLiveEntity(itemGuid); - Console.WriteLine(System.FormattableString.Invariant( - $"[autowalk-out] op=pickup target=0x{itemGuid:X8} name=\"{label}\" seq={seq}")); - } - // Remember this action so OnAutoWalkArrivedReSendAction can - // re-fire it close-range. Skip when this IS the re-send. - if (!isRetryAfterArrival) - _pendingPostArrivalAction = (itemGuid, true); - } - - /// - /// B.6+B.7 (2026-05-15). Fires when - /// signals natural arrival at an auto-walk target. Force-flushes - /// the player's current authoritative position to ACE first, then - /// re-sends the Use/PickUp. Without the position flush, ACE - /// processes the re-sent Use before the next per-frame - /// AutonomousPosition heartbeat β€” so ACE's Player.Location is - /// still stale (back where the auto-walk started) and ACE replies - /// with another MoveToObject instead of completing the action. - /// - private void OnAutoWalkArrivedReSendAction() - { - if (_pendingPostArrivalAction is not (uint guid, bool isPickup) pending) - return; - // Clear FIRST to break any retry loop. - _pendingPostArrivalAction = null; - - // Send a fresh AutonomousPosition NOW so ACE's server-side - // Player.Location updates to our arrived position before ACE - // sees the re-sent action. - SendAutonomousPositionNow(); - - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeAutoWalkEnabled) - Console.WriteLine(System.FormattableString.Invariant( - $"[autowalk-arrived-resend] guid=0x{guid:X8} isPickup={isPickup}")); - if (isPickup) SendPickUp(guid, isRetryAfterArrival: true); - else SendUse(guid, isRetryAfterArrival: true); - } - - /// - /// B.6 (2026-05-15). Install a local auto-walk overlay against the - /// given target entity at Use / PickUp send time. ACE's response - /// branches by distance: - /// - /// Far target β†’ ACE broadcasts a MovementType=6 - /// MoveToObject which arrives shortly after and overwrites - /// our speculative state with ACE's wire-supplied use-radius - /// and origin. No conflict; same target either way. - /// Close target β†’ ACE skips MoveToChain (WithinUseRadius - /// shortcut at Player_Move.cs:66) and rotates the body - /// server-side via Rotate(target). ACE doesn't broadcast - /// anything actionable to us, so our pre-installed overlay - /// handles the local rotation β€” body turns to face the target - /// in place, then ends. - /// - /// - /// Per-type use radius mirrors the picker's radius heuristic: - /// 3 m for creatures, 2 m for doors / lifestones / portals, - /// 0.6 m for everything else (ground items). - /// - /// - /// - /// B.6 (2026-05-15). True if the local player is already inside the - /// target's use radius right now β€” i.e. ACE will NOT auto-walk us - /// when we send the action. Used to gate the close-range deferral - /// in SendUse / SendPickUp: when close, we hold the wire packet - /// until our local rotation overlay reports alignment, then fire. - /// - private bool IsCloseRangeTarget(uint targetGuid) - { - if (_playerController is null) return false; - if (!_entitiesByServerGuid.TryGetValue(targetGuid, out var entity)) - return false; - - // Mirror InstallSpeculativeTurnToTarget's per-type radius heuristic. - float useRadius = 0.6f; - if (_liveEntityInfoByGuid.TryGetValue(targetGuid, out var info) - && (info.ItemType & AcDream.Core.Items.ItemType.Creature) != 0) - { - useRadius = 3.0f; - } - else if (_lastSpawnByGuid.TryGetValue(targetGuid, out var spawn) - && spawn.ObjectDescriptionFlags is { } odf) - { - const uint LargeFlatMask = 0x1000u | 0x4000u | 0x40000u | 0x2000u; - if ((odf & LargeFlatMask) != 0) useRadius = 2.0f; - } - - var bodyPos = _playerController.Position; - float dx = entity.Position.X - bodyPos.X; - float dy = entity.Position.Y - bodyPos.Y; - float distSq = dx * dx + dy * dy; - return distSq <= useRadius * useRadius; - } - - private void InstallSpeculativeTurnToTarget(uint targetGuid) - { - if (_playerController is null) return; - if (!_entitiesByServerGuid.TryGetValue(targetGuid, out var entity)) - return; - - // Per-type use radius β€” same heuristic as the picker's - // radiusForGuid callback. - float useRadius = 0.6f; - if (_liveEntityInfoByGuid.TryGetValue(targetGuid, out var info) - && (info.ItemType & AcDream.Core.Items.ItemType.Creature) != 0) - { - useRadius = 3.0f; - } - else if (_lastSpawnByGuid.TryGetValue(targetGuid, out var spawn) - && spawn.ObjectDescriptionFlags is { } odf) - { - // BF_DOOR | BF_LIFESTONE | BF_PORTAL | BF_CORPSE - const uint LargeFlatMask = 0x1000u | 0x4000u | 0x40000u | 0x2000u; - if ((odf & LargeFlatMask) != 0) useRadius = 2.0f; - } - - _playerController.BeginServerAutoWalk( - destinationWorld: entity.Position, - minDistance: 0f, - distanceToObject: useRadius, - moveTowards: true, - // 15 m matches ACE's MoveToParameters.SetDefaults - // WalkRunThreshold for non-combat Use/PickUp paths. - // Using 9999 here forced walk-mode for the brief window - // between this speculative install and ACE's MovementType=6 - // overwrite β€” far targets briefly walked before switching - // to run, which the user observed as "we only walk, not - // running from the correct threshold". 15.0 lines up with - // what ACE will send anyway, so the initial decision and - // the overwrite agree. - walkRunThreshold: 15.0f); - } - - /// - /// B.6+B.7 (2026-05-15). Send an out-of-frame AutonomousPosition - /// packet using the controller's current authoritative state. - /// Used to flush position to ACE on auto-walk arrival before - /// re-sending the Use/PickUp action; without it, ACE's - /// Player.Location is the pre-walk position and the action - /// resolves out-of-range. - /// - private void SendAutonomousPositionNow() - { - if (_liveSession is null - || _liveSession.CurrentState != AcDream.Core.Net.WorldSession.State.InWorld - || _playerController is null) - return; - - var pos = _playerController.Position; - int lbX = _liveCenterX + (int)MathF.Floor(pos.X / 192f); - int lbY = _liveCenterY + (int)MathF.Floor(pos.Y / 192f); - float localX = pos.X - (lbX - _liveCenterX) * 192f; - float localY = pos.Y - (lbY - _liveCenterY) * 192f; - uint wireCellId = ((uint)lbX << 24) | ((uint)lbY << 16) | (_playerController.CellId & 0xFFFFu); - var wirePos = new System.Numerics.Vector3(localX, localY, pos.Z); - var wireRot = YawToAcQuaternion(_playerController.Yaw); - byte contactByte = _playerController.IsAirborne ? (byte)0 : (byte)1; - - var seq = _liveSession.NextGameActionSequence(); - var body = AcDream.Core.Net.Messages.AutonomousPosition.Build( - gameActionSequence: seq, - cellId: wireCellId, - position: wirePos, - rotation: wireRot, - instanceSequence: _liveSession.InstanceSequence, - serverControlSequence: _liveSession.ServerControlSequence, - teleportSequence: _liveSession.TeleportSequence, - forcePositionSequence: _liveSession.ForcePositionSequence, - lastContact: contactByte); - _liveSession.SendGameAction(body); - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeAutoWalkEnabled) - Console.WriteLine(System.FormattableString.Invariant( - $"[autowalk-flush-ap] seq={seq} cell=0x{wireCellId:X8} pos=({wirePos.X:F2},{wirePos.Y:F2},{wirePos.Z:F2})")); - } - private uint? SelectClosestCombatTarget(bool showToast) { if (!_entitiesByServerGuid.TryGetValue(_playerServerGuid, out var playerEntity)) @@ -9485,7 +8228,7 @@ public sealed class GameWindow : IDisposable bestGuid = guid; } - _selectedGuid = bestGuid; + _selectedTargetGuid = bestGuid; if (bestGuid is { } selected) { string label = DescribeLiveEntity(selected); @@ -9515,328 +8258,6 @@ public sealed class GameWindow : IDisposable return (info.ItemType & AcDream.Core.Items.ItemType.Creature) != 0; } - /// - /// 2026-05-15. True when the entity is "tall scenery" β€” has a known - /// non-zero ItemType that is NOT in the small-carry-item mask AND - /// has no door/lifestone/portal/corpse PWD bits AND is not a - /// creature. The Holtburg town sign is the canonical example: a - /// 3 m post-mounted entity that needs the pick sphere lifted to - /// mid-pole with a wider radius so the user can click any part of - /// the visible mesh, not just the pole base. - /// - /// - /// Mirrors 's - /// classification β€” both fall into the "everything else: 3 m default" - /// branch β€” so the visible indicator box and the click sphere - /// match. - /// - /// - private bool IsTallSceneryGuid(uint guid) - { - // Creatures are never "tall scenery" β€” picker uses humanoid sphere. - if (_liveEntityInfoByGuid.TryGetValue(guid, out var info) - && (info.ItemType & AcDream.Core.Items.ItemType.Creature) != 0) - return false; - - if (!_lastSpawnByGuid.TryGetValue(guid, out var spawn)) - return false; - - // Doors / lifestones / portals / corpses β†’ LargeFlatMask branch. - if (spawn.ObjectDescriptionFlags is { } odf) - { - const uint LargeFlatMask = 0x1000u | 0x4000u | 0x40000u | 0x2000u; - if ((odf & LargeFlatMask) != 0) return false; - } - - uint it = spawn.ItemType ?? 0u; - - // 2026-05-15 β€” useability-based discriminator. Mirrors - // TargetIndicatorPanel.EntityHeightFor exactly so the click - // sphere matches the visible box. A small-item ItemType is - // a REAL pickup item only if it is also useable from the - // world (USEABLE_REMOTE bit, acclient.h:6478). Otherwise - // (Misc + USEABLE_UNDEF β€” the Holtburg sign case) it's tall - // scenery and gets the lifted+widened sphere. - const uint USEABLE_REMOTE_BIT = 0x20u; - bool useableFromWorld = spawn.Useability is uint u - && (u & USEABLE_REMOTE_BIT) != 0; - - const uint SmallItemMask = - (uint)(AcDream.Core.Items.ItemType.MeleeWeapon - | AcDream.Core.Items.ItemType.Armor - | AcDream.Core.Items.ItemType.Clothing - | AcDream.Core.Items.ItemType.Jewelry - | AcDream.Core.Items.ItemType.Food - | AcDream.Core.Items.ItemType.Money - | AcDream.Core.Items.ItemType.Misc - | AcDream.Core.Items.ItemType.MissileWeapon - | AcDream.Core.Items.ItemType.Container - | AcDream.Core.Items.ItemType.Gem - | AcDream.Core.Items.ItemType.SpellComponents - | AcDream.Core.Items.ItemType.Writable - | AcDream.Core.Items.ItemType.Key - | AcDream.Core.Items.ItemType.Caster); - // Real pickup item: small-item-class AND useable. NOT tall scenery. - if ((it & SmallItemMask) != 0 && useableFromWorld) return false; - - // Everything else (signs / banners / untyped scenery / - // Misc-typed-but-non-useable): tall scenery. - return true; - } - - /// - /// 2026-05-16 β€” retail-faithful port of - /// SmartBox::GetObjectBoundingBox (decomp 0x00452e20) - /// using CPhysicsObj::GetSelectionSphere (0x0050ea40) - /// β†’ CPartArray::GetSelectionSphere (0x00518b80). - /// - /// - /// Retail's VividTargetIndicator does NOT use a per-mesh AABB β€” - /// it uses the Setup's selection_sphere field (a single - /// sphere encompassing the entire entity, baked at Setup-creation - /// time). The sphere is scaled by the part-array scale - /// (component-wise on center, Z-scale on radius β€” retail's exact - /// formula at 0x00518ba6–0x00518be3) and rotated by entity - /// orientation. The screen indicator rect is the projection of - /// the camera-aligned BBox of this sphere β€” i.e. a screen circle - /// of radius worldRadius * focalLength / depth. - /// - /// - /// - /// Result: the indicator rect MATCHES the Setup's intended - /// "selectable extent" β€” which is typically larger than the mesh - /// AABB by design (Setups bake a slightly oversized selection - /// sphere so far targets still get pickable indicators). That's - /// why our previous mesh-AABB indicator was smaller than retail's. - /// - /// - private bool TryGetEntitySelectionSphere(uint guid, - out System.Numerics.Vector3 worldCenter, - out float worldRadius) - { - worldCenter = default; - worldRadius = 0f; - - if (!_entitiesByServerGuid.TryGetValue(guid, out var entity)) return false; - if (!_lastSpawnByGuid.TryGetValue(guid, out var spawn)) return false; - if (spawn.SetupTableId is not uint setupId) return false; - if (_dats is null) return false; - if (!_dats.TryGet(setupId, out var setup)) return false; - - // DAT Setup carries `SelectionSphere` (Origin + Radius). A zero - // radius means the Setup didn't bake one β€” fall back to the - // caller's other path. - var sel = setup.SelectionSphere; - if (sel is null || sel.Radius <= 1e-4f) return false; - - // Retail GetSelectionSphere applies part-array scale to the - // sphere center (component-wise) and to the radius (Z-scale - // only). For uniform entity scale these coincide. - float scale = entity.Scale > 0f ? entity.Scale : 1f; - var localCenter = new System.Numerics.Vector3( - sel.Origin.X * scale, - sel.Origin.Y * scale, - sel.Origin.Z * scale); - - // Setup-local center β†’ world. Entity rotation applies; entity - // position is the world origin of the setup. - var rot = System.Numerics.Matrix4x4.CreateFromQuaternion(entity.Rotation); - var rotated = System.Numerics.Vector3.Transform(localCenter, rot); - worldCenter = entity.Position + rotated; - worldRadius = sel.Radius * scale; - return true; - } - - /// - /// 2026-05-15. Retail-faithful gate for R-key Use / F-key PickUp. - /// Returns true when the entity is interactable from the world per - /// the server-supplied ITEM_USEABLE _useability field - /// (acclient.h:6478) β€” specifically when the USEABLE_REMOTE - /// (0x20) bit is set OR a composite form containing it - /// (USEABLE_REMOTE_NEVER_WALK = 0x60, the - /// SOURCE_X_TARGET_REMOTE variants in the 0x200000+ range). - /// - /// - /// Retail behaviour for non-useable entities (signs, banners, - /// decorative scenery): the R-key does nothing β€” no walk, no Use - /// packet, no toast. The retail client checks useability before - /// any action and silently ignores the press. We honor that with - /// a silent early return at the call site. - /// - /// - /// - /// Fallback when useability is unknown. The wire's - /// weenieFlags & 0x10 bit gates whether ACE serializes - /// useability at all. If absent, - /// is null. Conservatively we permit Use for entities we've - /// historically been able to interact with β€” creatures, doors, - /// lifestones, portals, corpses β€” to avoid regressing the existing - /// M1 flows. Pure-scenery untyped entities (the sign case) fall - /// through to "blocked". - /// - /// - private bool IsUseableTarget(uint guid) - { - if (_lastSpawnByGuid.TryGetValue(guid, out var spawn)) - { - // Authoritative path: server published Useability. - // 2026-05-16 β€” retail-faithful gate per ItemUses::IsUseable - // at acclient_2013_pseudo_c.txt:256455 (4 call-site cross- - // checks confirm: ItemHolder::UseObject 0x00588a80, - // DetermineUseResult 0x402697, UsingItem 0x367638, - // disable-button state 0x198826 β€” all key off non-zero). - // BN's `!(x) & 1` rendering is a mis-decompile of the - // setne+and test-flag inliner. Real semantic: - // - // IsUseable(_useability) := (_useability != USEABLE_UNDEF) - // - // ANY non-zero value passes (including USEABLE_NO=1, - // USEABLE_CONTAINED=8, etc.). Retail trusts the server to - // have only set non-zero on entities where Use is sensible. - // - // Previous implementation (B.8) checked - // `(useability & USEABLE_REMOTE_BIT) != 0` which is STRICTER - // than retail β€” a USEABLE_NO door would be blocked locally - // but pass retail's gate. Now matches retail bit-for-bit. - if (spawn.Useability is uint useability) - { - // Retail-faithful Use gate per acclient_2013_pseudo_c.txt:256455 - // ItemUses::IsUseable: non-zero useability passes. But two - // values produce "cannot be used" client-side without a - // wire send in retail's observable behaviour: - // USEABLE_UNDEF (0): server's Use handler would reject; - // retail UseObject path shows "cannot be used" toast. - // USEABLE_NO (1): explicitly not useable β€” same outcome. - // Both come from acclient.h:6478 ITEM_USEABLE enum. - // - // Retail technically sends the packet for USEABLE_NO (the - // audit's `IsUseable != 0` reading is correct), but ACE - // never broadcasts MovementType=6 for it, so retail - // doesn't visibly approach. Our client installs a - // speculative auto-walk overlay BEFORE the server - // response β€” so the only way to avoid "approach then fail" - // is to gate USEABLE_NO client-side. Net result matches - // user-observed retail behaviour. - const uint USEABLE_UNDEF = 0u; - const uint USEABLE_NO = 1u; - if (useability == USEABLE_UNDEF || useability == USEABLE_NO) - return false; - return true; - } - - // Useability NOT in PWD β€” fall back to known-useable types. - // ObjectDescriptionFlags BF_DOOR|BF_LIFESTONE|BF_PORTAL|BF_CORPSE - // historically work with Use; allow them through. - if (spawn.ObjectDescriptionFlags is { } odf) - { - const uint UseableFlatMask = 0x1000u | 0x4000u | 0x40000u | 0x2000u; - if ((odf & UseableFlatMask) != 0) - { - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeUseabilityFallbackEnabled) - Console.WriteLine(System.FormattableString.Invariant( - $"[useability-fallback] flat-class guid=0x{guid:X8} odf=0x{odf:X8} (ACE sent no useability bit)")); - return true; - } - } - } - - // Creatures (NPCs / players) are always Use targets in our - // fallback even when ACE didn't publish useability. Retail - // would have blocked here (null β†’ USEABLE_UNDEF β†’ 0 β†’ block), - // but ACE's seed DB has many talk-only NPC weenies with - // `ItemUseable = null`; without the fallback the M1 "click NPC" - // flow regresses. The diagnostic line below lets us measure - // how often this branch fires in real play. - if (_liveEntityInfoByGuid.TryGetValue(guid, out var info) - && (info.ItemType & AcDream.Core.Items.ItemType.Creature) != 0) - { - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeUseabilityFallbackEnabled) - Console.WriteLine(System.FormattableString.Invariant( - $"[useability-fallback] creature guid=0x{guid:X8} (ACE sent no useability bit)")); - return true; - } - - // Default: not useable. Signs, banners, untyped scenery with no - // server-supplied useability and no creature/door PWD bits land - // here β€” exactly the retail "nothing happens" case. - return false; - } - - /// - /// 2026-05-16. Retail-faithful gate for F-key PickUp / right-click - /// "Pick Up." Distinct from because - /// pickup is more restrictive than Use: the entity must be useable - /// FROM THE WORLD (USEABLE_REMOTE bit, 0x20). Signs / banners with - /// USEABLE_NO (0x1) lack the REMOTE bit so pickup is blocked - /// client-side without a wire packet β€” matches retail's "The X can't - /// be picked up!" client-side toast. - /// - /// - /// Useable values that include USEABLE_REMOTE (0x20): - /// USEABLE_REMOTE (0x20), USEABLE_REMOTE_NEVER_WALK (0x60), - /// USEABLE_VIEWED_REMOTE (0x30), and the SOURCE_*_TARGET_REMOTE - /// composites in the 0x200000+ range. - /// - /// - /// - /// Null-useability fallback: same as - /// β€” permit pickup for entities with BF_CORPSE bit set, and for - /// items with small-item ItemType. This preserves M1 ground-item - /// pickup flow for entities where ACE didn't publish useability. - /// - /// - private bool IsPickupableTarget(uint guid) - { - if (_lastSpawnByGuid.TryGetValue(guid, out var spawn)) - { - if (spawn.Useability is uint useability) - { - const uint USEABLE_REMOTE = 0x20u; - return (useability & USEABLE_REMOTE) != 0u; - } - - // Useability null: corpses are pickupable; signs aren't. - if (spawn.ObjectDescriptionFlags is { } odf) - { - const uint BF_CORPSE = 0x2000u; - if ((odf & BF_CORPSE) != 0u) - { - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeUseabilityFallbackEnabled) - Console.WriteLine(System.FormattableString.Invariant( - $"[useability-fallback] pickup-corpse guid=0x{guid:X8} (ACE sent no useability bit)")); - return true; - } - } - - // Small-item ItemType fallback (covers F on dropped items - // when ACE doesn't publish useability for the weenie). - uint it = spawn.ItemType ?? 0u; - const uint SmallItemMask = - (uint)(AcDream.Core.Items.ItemType.MeleeWeapon - | AcDream.Core.Items.ItemType.Armor - | AcDream.Core.Items.ItemType.Clothing - | AcDream.Core.Items.ItemType.Jewelry - | AcDream.Core.Items.ItemType.Food - | AcDream.Core.Items.ItemType.Money - | AcDream.Core.Items.ItemType.Misc - | AcDream.Core.Items.ItemType.MissileWeapon - | AcDream.Core.Items.ItemType.Container - | AcDream.Core.Items.ItemType.Gem - | AcDream.Core.Items.ItemType.SpellComponents - | AcDream.Core.Items.ItemType.Writable - | AcDream.Core.Items.ItemType.Key - | AcDream.Core.Items.ItemType.Caster); - if ((it & SmallItemMask) != 0u) - { - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeUseabilityFallbackEnabled) - Console.WriteLine(System.FormattableString.Invariant( - $"[useability-fallback] pickup-smallitem guid=0x{guid:X8} itemType=0x{it:X8} (ACE sent no useability bit)")); - return true; - } - } - return false; - } - private string DescribeLiveEntity(uint guid) { if (_liveEntityInfoByGuid.TryGetValue(guid, out var info) @@ -9954,17 +8375,6 @@ public sealed class GameWindow : IDisposable } _playerController = new AcDream.App.Input.PlayerMovementController(_physicsEngine); - - // B.6+B.7 (2026-05-15): re-send the Use/PickUp action on local - // auto-walk arrival. ACE's server-side MoveToChain may have - // already timed out by the time the local body arrives (we - // walk locally but don't send tracking position updates to - // ACE during the walk yet, so ACE's WithinUseRadius check may - // never have passed). Resending close-range hits ACE's - // CreateMoveToChain WithinUseRadius shortcut (Player_Move.cs:66) - // and completes the action immediately. - _playerController.AutoWalkArrived += OnAutoWalkArrivedReSendAction; - // K-fix7 (2026-04-26): if PlayerDescription already arrived, the // server's Run / Jump skill values are cached here β€” push them // into the freshly-constructed controller so the runRate / @@ -9985,10 +8395,7 @@ public sealed class GameWindow : IDisposable // 0.4 m fallbacks. if (_dats is not null && (playerEntity.SourceGfxObjOrSetupId & 0xFF000000u) == 0x02000000u) { - // A.5 T10: lock around _dats.Get β€” worker thread may be - // building a landblock mesh concurrently. - DatReaderWriter.DBObjs.Setup? playerSetup; - lock (_datLock) { playerSetup = _dats.Get(playerEntity.SourceGfxObjOrSetupId); } + var playerSetup = _dats.Get(playerEntity.SourceGfxObjOrSetupId); if (playerSetup is not null) _physicsDataCache.CacheSetup(playerEntity.SourceGfxObjOrSetupId, playerSetup); _playerController.StepUpHeight = (playerSetup is not null && playerSetup.StepUpHeight > 0f) @@ -10203,76 +8610,6 @@ public sealed class GameWindow : IDisposable } } - /// Phase N.5b: emits [TERRAIN-DIAG] once per ~5s under - /// ACDREAM_WB_DIAG=1. Mirrors WbDrawDispatcher.MaybeFlushDiag: - /// rolling 256-sample buffer of microseconds, median + p95 reported. - /// Sample buffer is NOT cleared on flush β€” it's a moving window so the - /// next 5s window already has 256 frames of recent history. - private void MaybeFlushTerrainDiag() - { - if (!string.Equals(Environment.GetEnvironmentVariable("ACDREAM_WB_DIAG"), "1", StringComparison.Ordinal)) - return; - - long now = Environment.TickCount64; - if (now - _terrainLastDiagTick <= 5000) return; - - // Samples are stored as microseconds Γ— 100 (so 1.23 Β΅s becomes 123 long). - long cpuMedHundredthsUs = TerrainDiagMedianMicros(_terrainCpuSamples); - long cpuP95HundredthsUs = TerrainDiagPercentile95Micros(_terrainCpuSamples); - double cpuMedUs = cpuMedHundredthsUs / 100.0; - double cpuP95Us = cpuP95HundredthsUs / 100.0; - // A.5 T23: flag when terrain dispatcher median exceeds 1.0ms budget - // (Phase A.5 spec Β§2 acceptance criterion 6). Grep-friendly prefix. - const double TerrainBudgetUs = 1000.0; - string terrainBudgetFlag = cpuMedUs > TerrainBudgetUs ? " BUDGET_OVER" : ""; - Console.WriteLine( - $"[TERRAIN-DIAG]{terrainBudgetFlag} cpu_us={cpuMedUs:F2}m/{cpuP95Us:F2}p95 " + - $"draws={_terrain?.VisibleSlots ?? 0}/frame " + - $"visible={_terrain?.VisibleSlots ?? 0} " + - $"loaded={_terrain?.LoadedSlots ?? 0} " + - $"capacity={_terrain?.CapacitySlots ?? 0}"); - _terrainLastDiagTick = now; - } - - private static long TerrainDiagMedianMicros(long[] samples) - { - var copy = (long[])samples.Clone(); - Array.Sort(copy); - int nz = 0; - foreach (var v in copy) if (v > 0) nz++; - if (nz == 0) return 0; - // Sorted ascending: zero-padding at the front, samples at the back. - // Median of nz samples is the middle of the last nz entries; using - // (nz - 1) / 2 from the end keeps the offset >= 0 for all nz >= 1 - // (the original nz / 2 form underflowed to copy.Length on first - // diag-flush when only 1 sample had been recorded). - return copy[copy.Length - 1 - (nz - 1) / 2]; - } - - private static long TerrainDiagPercentile95Micros(long[] samples) - { - var copy = (long[])samples.Clone(); - Array.Sort(copy); - int nz = 0; - foreach (var v in copy) if (v > 0) nz++; - if (nz == 0) return 0; - // 95th percentile = upper end of the sorted samples; clamp the - // offset to stay inside the populated tail when nz < 20. - int offset = (int)((nz - 1) * 0.05); - return copy[copy.Length - 1 - offset]; - } - - /// A.5 T22: parse a float environment variable, returning - /// when the variable is absent or unparseable. - private static float ParseEnvFloat(string name, float defaultValue) - { - var s = System.Environment.GetEnvironmentVariable(name); - if (s is not null && float.TryParse(s, System.Globalization.NumberStyles.Float, - System.Globalization.CultureInfo.InvariantCulture, out var v)) - return v; - return defaultValue; - } - private void OnClosing() { // Phase A.1: join the streamer worker thread before tearing down GL @@ -10284,15 +8621,13 @@ public sealed class GameWindow : IDisposable _combatChatTranslator?.Dispose(); _liveSession?.Dispose(); _audioEngine?.Dispose(); // Phase E.2: stop all voices, close AL context - _wbDrawDispatcher?.Dispose(); + _staticMesh?.Dispose(); _skyRenderer?.Dispose(); // depends on sampler cache; dispose first _samplerCache?.Dispose(); _textureCache?.Dispose(); - _wbMeshAdapter?.Dispose(); // Phase N.4+N.5 WB foundation (mandatory modern path) - _meshShader?.Dispose(); _terrain?.Dispose(); - _terrainModernShader?.Dispose(); + _shader?.Dispose(); _sceneLightingUbo?.Dispose(); _particleRenderer?.Dispose(); _debugLines?.Dispose(); @@ -10374,16 +8709,4 @@ public sealed class GameWindow : IDisposable _ => $"Room 0x{roomId:X8}", }; } - - /// - /// Fallback for the - /// sequencer - /// factory when neither _dats nor the entity's setup is available. - /// Returns null for all animation lookups so the sequencer silently has - /// no data (same behaviour as a new empty Setup). - /// - private sealed class NullAnimLoader : AcDream.Core.Physics.IAnimationLoader - { - public DatReaderWriter.DBObjs.Animation? LoadAnimation(uint id) => null; - } } diff --git a/src/AcDream.App/Rendering/InstancedMeshRenderer.cs b/src/AcDream.App/Rendering/InstancedMeshRenderer.cs new file mode 100644 index 0000000..92e8f5c --- /dev/null +++ b/src/AcDream.App/Rendering/InstancedMeshRenderer.cs @@ -0,0 +1,577 @@ +// src/AcDream.App/Rendering/InstancedMeshRenderer.cs +// +// True instanced rendering for static-object meshes. +// Groups entities by GfxObjId. All instance model matrices are written into +// a single shared instance VBO once per frame. Each sub-mesh is drawn with +// DrawElementsInstanced β€” one GL draw call per (GfxObj Γ— sub-mesh) instead +// of one per entity. For a scene with N unique GfxObjs and M total entities +// this reduces draw calls from M*subMeshes to N*subMeshes. +// +// Matrix layout: +// System.Numerics.Matrix4x4 is row-major. Written to the float[] buffer in +// natural memory order (M11..M44). The GLSL shader reads 4 vec4 attributes +// (aInstanceRow0-3) and constructs mat4(row0, row1, row2, row3). Because +// GLSL mat4() takes column vectors, the rows of the C# matrix become the +// columns of the GLSL mat4 β€” which is the same transpose that UniformMatrix4 +// with transpose=false produces. Visual result is identical to the old +// SetMatrix4("uModel", ...) path. +// +// Architecture note: public API matches StaticMeshRenderer so GameWindow only +// needs to update the shader and uniform setup at the call sites. +using System.Numerics; +using System.Runtime.InteropServices; +using AcDream.Core.Meshing; +using AcDream.Core.Terrain; +using AcDream.Core.World; +using Silk.NET.OpenGL; + +namespace AcDream.App.Rendering; + +public sealed unsafe class InstancedMeshRenderer : IDisposable +{ + private readonly GL _gl; + private readonly Shader _shader; + private readonly TextureCache _textures; + + // One GPU bundle per unique GfxObj id. Each GfxObj can have multiple sub-meshes. + private readonly Dictionary> _gpuByGfxObj = new(); + + // Shared instance VBO β€” filled every frame with all instance model matrices. + private readonly uint _instanceVbo; + + // Per-frame scratch: reused float buffer for instance matrix data. + // 16 floats per mat4. Grown on demand; never shrunk. + private float[] _instanceBuffer = new float[256 * 16]; // start at 256 instances + + // ── Instance grouping scratch ───────────────────────────────────────────── + // + // Reused every frame to avoid per-frame allocation. + // + // **Group key = (GfxObjId, PaletteOverrideHash, SurfaceOverridesHash).** + // + // An earlier implementation grouped on GfxObjId alone and resolved + // the per-sub-mesh texture from the first instance in the group β€” which + // is fine for scenery where every tree shares the same palette, but + // utterly broken for NPCs: every humanoid uses the same base body + // GfxObjs and they all piled into one group, so the first NPC's palette + // was used for every NPC in the frame. Frustum culling + iteration + // order meant that "first NPC" changed as the camera turned β€” producing + // the "NPC clothing changes when I turn" symptom. + // + // Now we also key by the entity's PaletteOverride + per-MeshRef + // SurfaceOverrides signature so only entities that decode to the + // SAME texture for every sub-mesh can share a batch. Entities with + // unique appearance fall to single-instance groups (still correct, + // marginally slower than true instancing). + private readonly Dictionary _groups = new(); + + private readonly record struct GroupKey(uint GfxObjId, ulong TextureSignature); + + public InstancedMeshRenderer(GL gl, Shader shader, TextureCache textures) + { + _gl = gl; + _shader = shader; + _textures = textures; + + _instanceVbo = _gl.GenBuffer(); + } + + // ── Upload ──────────────────────────────────────────────────────────────── + + public void EnsureUploaded(uint gfxObjId, IReadOnlyList subMeshes) + { + if (_gpuByGfxObj.ContainsKey(gfxObjId)) + return; + + var list = new List(subMeshes.Count); + foreach (var sm in subMeshes) + list.Add(UploadSubMesh(sm)); + _gpuByGfxObj[gfxObjId] = list; + } + + private SubMeshGpu UploadSubMesh(GfxObjSubMesh sm) + { + uint vao = _gl.GenVertexArray(); + _gl.BindVertexArray(vao); + + // ── Vertex buffer (positions, normals, UVs) ─────────────────────────── + uint vbo = _gl.GenBuffer(); + _gl.BindBuffer(BufferTargetARB.ArrayBuffer, vbo); + fixed (void* p = sm.Vertices) + _gl.BufferData(BufferTargetARB.ArrayBuffer, + (nuint)(sm.Vertices.Length * sizeof(Vertex)), p, BufferUsageARB.StaticDraw); + + uint stride = (uint)sizeof(Vertex); + _gl.EnableVertexAttribArray(0); + _gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, stride, (void*)0); + _gl.EnableVertexAttribArray(1); + _gl.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, stride, (void*)(3 * sizeof(float))); + _gl.EnableVertexAttribArray(2); + _gl.VertexAttribPointer(2, 2, VertexAttribPointerType.Float, false, stride, (void*)(6 * sizeof(float))); + // Note: location 3 (uint TerrainLayer) is NOT used by mesh_instanced.vert; + // that slot is reserved for per-instance mat4 row 0 from the instance VBO. + + // ── Index buffer ────────────────────────────────────────────────────── + uint ebo = _gl.GenBuffer(); + _gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, ebo); + fixed (void* p = sm.Indices) + _gl.BufferData(BufferTargetARB.ElementArrayBuffer, + (nuint)(sm.Indices.Length * sizeof(uint)), p, BufferUsageARB.StaticDraw); + + // ── Per-instance model matrix (locations 3-6) ───────────────────────── + // Bind the shared instance VBO. The VAO captures this binding at each + // attribute location. At draw time we re-call VertexAttribPointer with + // the per-group byte offset (to address different groups in the VBO + // without DrawElementsInstancedBaseInstance). + _gl.BindBuffer(BufferTargetARB.ArrayBuffer, _instanceVbo); + // mat4 = 4 Γ— vec4, stride = 64 bytes, divisor = 1 (advance once per instance) + for (uint row = 0; row < 4; row++) + { + uint loc = 3 + row; + _gl.EnableVertexAttribArray(loc); + _gl.VertexAttribPointer(loc, 4, VertexAttribPointerType.Float, false, 64, (void*)(row * 16)); + _gl.VertexAttribDivisor(loc, 1); + } + + _gl.BindVertexArray(0); + + return new SubMeshGpu + { + Vao = vao, + Vbo = vbo, + Ebo = ebo, + IndexCount = sm.Indices.Length, + SurfaceId = sm.SurfaceId, + Translucency = sm.Translucency, + }; + } + + // ── Draw ────────────────────────────────────────────────────────────────── + + public void Draw(ICamera camera, + IEnumerable<(uint LandblockId, Vector3 AabbMin, Vector3 AabbMax, IReadOnlyList Entities)> landblockEntries, + FrustumPlanes? frustum = null, + uint? neverCullLandblockId = null, + HashSet? visibleCellIds = null, + // L-fix1 (2026-04-28): set of entity ids that should bypass the + // landblock-level frustum cull. Animated entities (other + // players, NPCs, monsters) are always rendered if their + // landblock is loaded β€” without this they vanish whenever the + // camera rotates away from their landblock, even though + // they're within visible distance of the player. Pass null / + // empty to keep the previous "cull everything by landblock" + // behavior. + HashSet? animatedEntityIds = null) + { + _shader.Use(); + + var vp = camera.View * camera.Projection; + _shader.SetMatrix4("uViewProjection", vp); + + // Phase G: lighting + ambient + fog are owned by the + // SceneLighting UBO (binding=1) uploaded once per frame by + // GameWindow. The instanced mesh fragment shader reads it + // directly β€” no per-draw uniform uploads needed. + + // ── Collect and group instances ─────────────────────────────────────── + CollectGroups(landblockEntries, frustum, neverCullLandblockId, visibleCellIds, animatedEntityIds); + + // ── Build and upload the instance buffer ────────────────────────────── + // Count total instances. + int totalInstances = 0; + foreach (var grp in _groups.Values) + totalInstances += grp.Count; + + // Grow the scratch buffer if needed. + int needed = totalInstances * 16; + if (_instanceBuffer.Length < needed) + _instanceBuffer = new float[needed + 256 * 16]; // extra headroom + + // Write all groups contiguously. Record each group's starting offset + // (in units of instances, not bytes) so we can address them at draw time. + int instanceOffset = 0; + foreach (var grp in _groups.Values) + { + grp.BufferOffset = instanceOffset; + foreach (ref readonly var inst in CollectionsMarshal.AsSpan(grp.Entries)) + WriteMatrix(_instanceBuffer, instanceOffset++ * 16, inst.Model); + } + + // Upload all instance data in a single DynamicDraw call. + if (totalInstances > 0) + { + _gl.BindBuffer(BufferTargetARB.ArrayBuffer, _instanceVbo); + fixed (void* p = _instanceBuffer) + _gl.BufferData(BufferTargetARB.ArrayBuffer, + (nuint)(totalInstances * 16 * sizeof(float)), p, BufferUsageARB.DynamicDraw); + } + + // ── Pass 1: Opaque + ClipMap ────────────────────────────────────────── + // Diagnostic: ACDREAM_NO_CULL=1 disables backface culling entirely. + if (string.Equals(Environment.GetEnvironmentVariable("ACDREAM_NO_CULL"), "1", StringComparison.Ordinal)) + { + _gl.Disable(EnableCap.CullFace); + } + foreach (var (key, grp) in _groups) + { + if (!_gpuByGfxObj.TryGetValue(key.GfxObjId, out var subMeshes)) + continue; + + bool hasOpaqueSubMesh = false; + foreach (var sub in subMeshes) + { + if (sub.Translucency == TranslucencyKind.Opaque || + sub.Translucency == TranslucencyKind.ClipMap) + { + hasOpaqueSubMesh = true; + break; + } + } + if (!hasOpaqueSubMesh) continue; + + // For this group, instance data starts at grp.BufferOffset in the VBO. + // We need to tell the VAO to read from that offset. + uint byteOffset = (uint)(grp.BufferOffset * 64); // 64 bytes per mat4 + + foreach (var sub in subMeshes) + { + if (sub.Translucency != TranslucencyKind.Opaque && + sub.Translucency != TranslucencyKind.ClipMap) + continue; + + _shader.SetInt("uTranslucencyKind", (int)sub.Translucency); + + // Bind VAO + re-point instance attributes to the group's slice + // in the shared VBO. This updates the VAO's stored offset for + // locations 3-6 without touching the vertex or index bindings. + _gl.BindVertexArray(sub.Vao); + _gl.BindBuffer(BufferTargetARB.ArrayBuffer, _instanceVbo); + for (uint row = 0; row < 4; row++) + { + _gl.VertexAttribPointer(3 + row, 4, VertexAttribPointerType.Float, + false, 64, (void*)(byteOffset + row * 16)); + } + + // Resolve texture from the first instance (all instances in this + // group share the same GfxObj so they have compatible overrides + // only in the degenerate case of mixed-palette entities using the + // same GfxObj β€” rare enough to accept the approximation here). + if (grp.Count == 0) continue; + var firstEntry = grp.Entries[0]; + uint tex = ResolveTex(firstEntry.Entity, firstEntry.MeshRef, sub); + _gl.ActiveTexture(TextureUnit.Texture0); + _gl.BindTexture(TextureTarget.Texture2D, tex); + + _gl.DrawElementsInstanced(PrimitiveType.Triangles, + (uint)sub.IndexCount, + DrawElementsType.UnsignedInt, + (void*)0, + (uint)grp.Count); + } + } + + // ── Pass 2: Translucent (AlphaBlend, Additive, InvAlpha) ───────────── + _gl.Enable(EnableCap.Blend); + _gl.DepthMask(false); + // Diagnostic: ACDREAM_NO_CULL=1 disables backface culling (used 2026-05-01 + // to test if our mesh winding (0,i,i+1) vs ACME's (i+1,i,0) is causing + // visible polygons to be culled, especially around the neck/coat seam). + if (string.Equals(Environment.GetEnvironmentVariable("ACDREAM_NO_CULL"), "1", StringComparison.Ordinal)) + { + _gl.Disable(EnableCap.CullFace); + } + else + { + _gl.Enable(EnableCap.CullFace); + _gl.CullFace(TriangleFace.Back); + _gl.FrontFace(FrontFaceDirection.Ccw); + } + + foreach (var (key, grp) in _groups) + { + if (!_gpuByGfxObj.TryGetValue(key.GfxObjId, out var subMeshes)) + continue; + + bool hasTranslucentSubMesh = false; + foreach (var sub in subMeshes) + { + if (sub.Translucency != TranslucencyKind.Opaque && + sub.Translucency != TranslucencyKind.ClipMap) + { + hasTranslucentSubMesh = true; + break; + } + } + if (!hasTranslucentSubMesh) continue; + + uint byteOffset = (uint)(grp.BufferOffset * 64); + + foreach (var sub in subMeshes) + { + if (sub.Translucency == TranslucencyKind.Opaque || + sub.Translucency == TranslucencyKind.ClipMap) + continue; + + switch (sub.Translucency) + { + case TranslucencyKind.Additive: + _gl.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.One); + break; + case TranslucencyKind.InvAlpha: + _gl.BlendFunc(BlendingFactor.OneMinusSrcAlpha, BlendingFactor.SrcAlpha); + break; + default: // AlphaBlend + _gl.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha); + break; + } + + _shader.SetInt("uTranslucencyKind", (int)sub.Translucency); + + _gl.BindVertexArray(sub.Vao); + _gl.BindBuffer(BufferTargetARB.ArrayBuffer, _instanceVbo); + for (uint row = 0; row < 4; row++) + { + _gl.VertexAttribPointer(3 + row, 4, VertexAttribPointerType.Float, + false, 64, (void*)(byteOffset + row * 16)); + } + + if (grp.Count == 0) continue; + var firstEntry = grp.Entries[0]; + uint tex = ResolveTex(firstEntry.Entity, firstEntry.MeshRef, sub); + _gl.ActiveTexture(TextureUnit.Texture0); + _gl.BindTexture(TextureTarget.Texture2D, tex); + + _gl.DrawElementsInstanced(PrimitiveType.Triangles, + (uint)sub.IndexCount, + DrawElementsType.UnsignedInt, + (void*)0, + (uint)grp.Count); + } + } + + // Restore default GL state. + _gl.DepthMask(true); + _gl.Disable(EnableCap.Blend); + _gl.Disable(EnableCap.CullFace); + _gl.BindVertexArray(0); + } + + // ── Grouping ────────────────────────────────────────────────────────────── + + /// + /// Iterates all visible landblock entries and groups every (entity, meshRef) + /// pair by GfxObjId. Clears previous frame's groups before filling. + /// + private void CollectGroups( + IEnumerable<(uint LandblockId, Vector3 AabbMin, Vector3 AabbMax, IReadOnlyList Entities)> landblockEntries, + FrustumPlanes? frustum, + uint? neverCullLandblockId, + HashSet? visibleCellIds, + HashSet? animatedEntityIds) + { + foreach (var grp in _groups.Values) + grp.Entries.Clear(); + + foreach (var entry in landblockEntries) + { + // L-fix1 (2026-04-28): the landblock cull decision is now + // PER-LANDBLOCK boolean, not a continue. We still need to + // walk the entity list because animated entities (in + // animatedEntityIds) bypass the cull and render anyway. + bool landblockVisible = frustum is null + || entry.LandblockId == neverCullLandblockId + || FrustumCuller.IsAabbVisible(frustum.Value, entry.AabbMin, entry.AabbMax); + + // Fast path: no animated entities globally β†’ if landblock is + // culled, skip the whole entity list (preserves the original + // O(visible-landblocks) cost when the caller doesn't care + // about animated bypass). + if (!landblockVisible && (animatedEntityIds is null || animatedEntityIds.Count == 0)) + continue; + + foreach (var entity in entry.Entities) + { + if (entity.MeshRefs.Count == 0) + continue; + + // L-fix1: when the landblock is frustum-culled, only + // render entities flagged as animated. This keeps + // remote players / NPCs / monsters visible even when + // their landblock rotates out of the view frustum. + bool isAnimated = animatedEntityIds?.Contains(entity.Id) == true; + if (!landblockVisible && !isAnimated) + continue; + + // Step 4: portal visibility filter. If we have a visible cell set, + // skip interior entities whose parent cell isn't visible. + // visibleCellIds == null means camera is outdoors β†’ show all interiors. + if (entity.ParentCellId.HasValue && visibleCellIds is not null + && !visibleCellIds.Contains(entity.ParentCellId.Value)) + continue; + + var entityRoot = + Matrix4x4.CreateFromQuaternion(entity.Rotation) * + Matrix4x4.CreateTranslation(entity.Position); + + // Hash the entity's PaletteOverride once β€” shared by every + // MeshRef on this entity, so we compute it outside the loop. + ulong palHash = HashPaletteOverride(entity.PaletteOverride); + + foreach (var meshRef in entity.MeshRefs) + { + if (!_gpuByGfxObj.ContainsKey(meshRef.GfxObjId)) + continue; + + var model = meshRef.PartTransform * entityRoot; + + // Texture signature = palette hash ^ surface-overrides hash. + // Two instances can share a batch only when their ResolveTex + // would return identical handles for every sub-mesh β€” that + // means identical palette AND identical surface overrides. + ulong surfHash = HashSurfaceOverrides(meshRef.SurfaceOverrides); + ulong texSig = palHash ^ surfHash; + var key = new GroupKey(meshRef.GfxObjId, texSig); + + if (!_groups.TryGetValue(key, out var group)) + { + group = new InstanceGroup(); + _groups[key] = group; + } + + group.Entries.Add(new InstanceEntry(model, entity, meshRef)); + } + } + } + } + + private static ulong HashPaletteOverride(AcDream.Core.World.PaletteOverride? p) + { + if (p is null) return 0UL; + ulong h = 0xCBF29CE484222325UL; + const ulong prime = 0x100000001B3UL; + h = (h ^ p.BasePaletteId) * prime; + foreach (var sp in p.SubPalettes) + { + h = (h ^ sp.SubPaletteId) * prime; + h = (h ^ sp.Offset) * prime; + h = (h ^ sp.Length) * prime; + } + return h; + } + + /// + /// Order-independent hash of a SurfaceOverrides dictionary. XOR of each + /// (key, value) pair keeps the result stable regardless of Dictionary + /// iteration order, so two instances whose override maps contain the + /// same pairs will hash identically. + /// + private static ulong HashSurfaceOverrides(IReadOnlyDictionary? overrides) + { + if (overrides is null || overrides.Count == 0) return 0UL; + ulong acc = 0UL; + foreach (var kvp in overrides) + { + ulong pair = ((ulong)kvp.Key << 32) | kvp.Value; + acc ^= pair; + } + // Fold with a prime so the zero case doesn't collide with "empty". + return (acc ^ 0xCBF29CE484222325UL) * 0x100000001B3UL; + } + + // ── Matrix write ────────────────────────────────────────────────────────── + + /// + /// Writes a System.Numerics Matrix4x4 into starting + /// at as 16 consecutive floats in row-major order + /// (the C# natural memory layout). The GLSL shader reads each 4-float row + /// as a column of the mat4 β€” identical to what UniformMatrix4(transpose=false) + /// produces for the uniform path. + /// + private static void WriteMatrix(float[] buf, int offset, in Matrix4x4 m) + { + buf[offset + 0] = m.M11; buf[offset + 1] = m.M12; buf[offset + 2] = m.M13; buf[offset + 3] = m.M14; + buf[offset + 4] = m.M21; buf[offset + 5] = m.M22; buf[offset + 6] = m.M23; buf[offset + 7] = m.M24; + buf[offset + 8] = m.M31; buf[offset + 9] = m.M32; buf[offset + 10] = m.M33; buf[offset + 11] = m.M34; + buf[offset + 12] = m.M41; buf[offset + 13] = m.M42; buf[offset + 14] = m.M43; buf[offset + 15] = m.M44; + } + + // ── Texture resolution ──────────────────────────────────────────────────── + + private uint ResolveTex(WorldEntity entity, MeshRef meshRef, SubMeshGpu sub) + { + uint overrideOrigTex = 0; + bool hasOrigTexOverride = meshRef.SurfaceOverrides is not null + && meshRef.SurfaceOverrides.TryGetValue(sub.SurfaceId, out overrideOrigTex); + uint? origTexOverride = hasOrigTexOverride ? overrideOrigTex : (uint?)null; + + if (entity.PaletteOverride is not null) + { + return _textures.GetOrUploadWithPaletteOverride( + sub.SurfaceId, origTexOverride, entity.PaletteOverride); + } + else if (hasOrigTexOverride) + { + return _textures.GetOrUploadWithOrigTextureOverride(sub.SurfaceId, overrideOrigTex); + } + else + { + return _textures.GetOrUpload(sub.SurfaceId); + } + } + + // ── Disposal ────────────────────────────────────────────────────────────── + + public void Dispose() + { + foreach (var subs in _gpuByGfxObj.Values) + { + foreach (var sub in subs) + { + _gl.DeleteBuffer(sub.Vbo); + _gl.DeleteBuffer(sub.Ebo); + _gl.DeleteVertexArray(sub.Vao); + } + } + _gl.DeleteBuffer(_instanceVbo); + _gpuByGfxObj.Clear(); + _groups.Clear(); + } + + // ── Private types ───────────────────────────────────────────────────────── + + private sealed class SubMeshGpu + { + public uint Vao; + public uint Vbo; + public uint Ebo; + public int IndexCount; + public uint SurfaceId; + public TranslucencyKind Translucency; + } + + /// + /// All instances of one GfxObj for this frame, plus their starting offset + /// in the shared instance VBO (in units of instances, not bytes). + /// + private sealed class InstanceGroup + { + public readonly List Entries = new(); + public int BufferOffset; + + public int Count => Entries.Count; + } + + private readonly struct InstanceEntry + { + public readonly Matrix4x4 Model; + public readonly WorldEntity Entity; + public readonly MeshRef MeshRef; + + public InstanceEntry(Matrix4x4 model, WorldEntity entity, MeshRef meshRef) + { + Model = model; + Entity = entity; + MeshRef = meshRef; + } + } +} diff --git a/src/AcDream.App/Rendering/Shaders/mesh_instanced.frag b/src/AcDream.App/Rendering/Shaders/mesh_instanced.frag new file mode 100644 index 0000000..1719e2f --- /dev/null +++ b/src/AcDream.App/Rendering/Shaders/mesh_instanced.frag @@ -0,0 +1,98 @@ +#version 430 core + +in vec2 vTex; +in vec3 vWorldNormal; +in vec3 vWorldPos; + +out vec4 fragColor; + +// One 2D texture per draw call β€” same binding point as mesh.frag so the +// C# side can use the same TextureCache without a texture-array pipeline. +uniform sampler2D uDiffuse; + +// Translucency kind β€” matches TranslucencyKind C# enum (same as mesh.frag): +// 0 = Opaque β€” depth write+test, no blend; shader never discards +// 1 = ClipMap β€” alpha-key discard at 0.5 (doors, windows, vegetation) +// 2 = AlphaBlend β€” GL blending handles compositing; do NOT discard +// 3 = Additive β€” GL additive blending; do NOT discard +// 4 = InvAlpha β€” GL inverted-alpha blending; do NOT discard +uniform int uTranslucencyKind; + +// Phase G.1+G.2: shared scene-lighting UBO (see mesh.frag for layout docs). +struct Light { + vec4 posAndKind; + vec4 dirAndRange; + vec4 colorAndIntensity; + vec4 coneAngleEtc; +}; +layout(std140, binding = 1) uniform SceneLighting { + Light uLights[8]; + vec4 uCellAmbient; + vec4 uFogParams; + vec4 uFogColor; + vec4 uCameraAndTime; +}; + +vec3 accumulateLights(vec3 N, vec3 worldPos) { + vec3 lit = uCellAmbient.xyz; + int activeLights = int(uCellAmbient.w); + for (int i = 0; i < 8; ++i) { + if (i >= activeLights) break; + + int kind = int(uLights[i].posAndKind.w); + vec3 Lcol = uLights[i].colorAndIntensity.xyz * uLights[i].colorAndIntensity.w; + + if (kind == 0) { + vec3 Ldir = -uLights[i].dirAndRange.xyz; + float ndl = max(0.0, dot(N, Ldir)); + lit += Lcol * ndl; + } else { + vec3 toL = uLights[i].posAndKind.xyz - worldPos; + float d = length(toL); + float range = uLights[i].dirAndRange.w; + if (d < range && range > 1e-3) { + vec3 Ldir = toL / max(d, 1e-4); + float ndl = max(0.0, dot(N, Ldir)); + float atten = 1.0; + if (kind == 2) { + float cos_edge = cos(uLights[i].coneAngleEtc.x * 0.5); + float cos_l = dot(-Ldir, uLights[i].dirAndRange.xyz); + atten *= (cos_l > cos_edge) ? 1.0 : 0.0; + } + lit += Lcol * ndl * atten; + } + } + } + return lit; +} + +vec3 applyFog(vec3 lit, vec3 worldPos) { + int mode = int(uFogParams.w); + if (mode == 0) return lit; + float d = length(worldPos - uCameraAndTime.xyz); + float fogStart = uFogParams.x; + float fogEnd = uFogParams.y; + float span = max(1e-3, fogEnd - fogStart); + float fog = clamp((d - fogStart) / span, 0.0, 1.0); + return mix(lit, uFogColor.xyz, fog); +} + +void main() { + vec4 color = texture(uDiffuse, vTex); + + // Alpha cutout only for clip-map surfaces (doors, windows, vegetation). + if (uTranslucencyKind == 1 && color.a < 0.5) discard; + + vec3 N = normalize(vWorldNormal); + vec3 lit = accumulateLights(N, vWorldPos); + + // Lightning flash β€” additive scene bump. + lit += uFogParams.z * vec3(0.6, 0.6, 0.75); + + // Retail clamp per-channel to 1.0 (r13 Β§13.1). + lit = min(lit, vec3(1.0)); + + vec3 rgb = color.rgb * lit; + rgb = applyFog(rgb, vWorldPos); + fragColor = vec4(rgb, color.a); +} diff --git a/src/AcDream.App/Rendering/Shaders/mesh_instanced.vert b/src/AcDream.App/Rendering/Shaders/mesh_instanced.vert new file mode 100644 index 0000000..a2f3893 --- /dev/null +++ b/src/AcDream.App/Rendering/Shaders/mesh_instanced.vert @@ -0,0 +1,35 @@ +#version 430 core + +// Per-vertex attributes +layout(location = 0) in vec3 aPosition; +layout(location = 1) in vec3 aNormal; +layout(location = 2) in vec2 aTexCoord; + +// Per-instance model matrix, split across four vec4 attribute slots. +// A mat4 consumes 4 consecutive attribute locations, so locations 3-6 are +// all occupied by this single logical matrix. The C# side must call +// VertexAttribPointer four times (one per row) and VertexAttribDivisor(loc, 1) +// on each of the four slots. +layout(location = 3) in vec4 aInstanceRow0; +layout(location = 4) in vec4 aInstanceRow1; +layout(location = 5) in vec4 aInstanceRow2; +layout(location = 6) in vec4 aInstanceRow3; + +uniform mat4 uViewProjection; + +out vec2 vTex; +out vec3 vWorldNormal; +out vec3 vWorldPos; + +void main() { + // Reconstruct the per-instance model matrix from its four row vectors. + mat4 model = mat4(aInstanceRow0, aInstanceRow1, aInstanceRow2, aInstanceRow3); + + vec4 worldPos = model * vec4(aPosition, 1.0); + gl_Position = uViewProjection * worldPos; + + vWorldPos = worldPos.xyz; + // Transform normal into world space. + vWorldNormal = normalize(mat3(model) * aNormal); + vTex = aTexCoord; +} diff --git a/src/AcDream.App/Rendering/Shaders/mesh_modern.frag b/src/AcDream.App/Rendering/Shaders/mesh_modern.frag deleted file mode 100644 index bbcc958..0000000 --- a/src/AcDream.App/Rendering/Shaders/mesh_modern.frag +++ /dev/null @@ -1,121 +0,0 @@ -#version 430 core -#extension GL_ARB_bindless_texture : require - -in vec3 vNormal; -in vec2 vTexCoord; -in vec3 vWorldPos; -in flat uvec2 vTextureHandle; -in flat uint vTextureLayer; - -// uRenderPass values (Phase N.5 Decision 2 β€” two-pass alpha-test): -// 0 = opaque pass β€” discard fragments with alpha < 0.95 -// (lets the depth write succeed for solid pixels) -// 1 = translucent pass β€” covers AlphaBlend / Additive / InvAlpha; -// discard alpha >= 0.95 (already drawn opaque) and -// alpha < 0.05 (skip empty fragments β€” large -// transparent overdraw cost otherwise) -uniform int uRenderPass; - -// SceneLighting UBO β€” IDENTICAL layout to mesh_instanced.frag binding=1. -struct Light { - vec4 posAndKind; - vec4 dirAndRange; - vec4 colorAndIntensity; - vec4 coneAngleEtc; -}; -layout(std140, binding = 1) uniform SceneLighting { - Light uLights[8]; - vec4 uCellAmbient; - vec4 uFogParams; - vec4 uFogColor; - vec4 uCameraAndTime; -}; - -vec3 accumulateLights(vec3 N, vec3 worldPos) { - vec3 lit = uCellAmbient.xyz; - int activeLights = int(uCellAmbient.w); - for (int i = 0; i < 8; ++i) { - if (i >= activeLights) break; - int kind = int(uLights[i].posAndKind.w); - vec3 Lcol = uLights[i].colorAndIntensity.xyz * uLights[i].colorAndIntensity.w; - if (kind == 0) { - vec3 Ldir = -uLights[i].dirAndRange.xyz; - float ndl = max(0.0, dot(N, Ldir)); - lit += Lcol * ndl; - } else { - vec3 toL = uLights[i].posAndKind.xyz - worldPos; - float d = length(toL); - float range = uLights[i].dirAndRange.w; - if (d < range && range > 1e-3) { - vec3 Ldir = toL / max(d, 1e-4); - float ndl = max(0.0, dot(N, Ldir)); - float atten = 1.0; - if (kind == 2) { - float cos_edge = cos(uLights[i].coneAngleEtc.x * 0.5); - float cos_l = dot(-Ldir, uLights[i].dirAndRange.xyz); - atten *= (cos_l > cos_edge) ? 1.0 : 0.0; - } - lit += Lcol * ndl * atten; - } - } - } - return lit; -} - -vec3 applyFog(vec3 lit, vec3 worldPos) { - int mode = int(uFogParams.w); - if (mode == 0) return lit; - float d = length(worldPos - uCameraAndTime.xyz); - float fogStart = uFogParams.x; - float fogEnd = uFogParams.y; - float span = max(1e-3, fogEnd - fogStart); - float fog = clamp((d - fogStart) / span, 0.0, 1.0); - return mix(lit, uFogColor.xyz, fog); -} - -out vec4 FragColor; - -void main() { - sampler2DArray tex = sampler2DArray(vTextureHandle); - vec4 color = texture(tex, vec3(vTexCoord, float(vTextureLayer))); - - // Two-pass alpha-test (N.5 Decision 2). - // A.5 T20: opaque pass writes alpha as-sampled so GL_SAMPLE_ALPHA_TO_COVERAGE - // derives the MSAA sample mask from it β€” ClipMap foliage edges become smooth. - // Discard only fully-transparent (Ξ± < 0.05); the GPU handles coverage masking. - if (uRenderPass == 0) { - if (color.a < 0.05) discard; // opaque pass β€” kill truly empty only (A2C) - } else { - // Transparent pass. - // - // Phase Post-A.5 (ISSUE #52, 2026-05-10): do NOT discard Ξ±β‰₯0.95 here. - // Native AC transparent-flagged surfaces routinely include - // effectively-opaque pixels β€” e.g. the Holtburg lifestone crystal core - // (surface 0x080011DE) which the spawn manifest classifies as - // transparent (batch.IsTransparent=True) but whose decoded texture - // alpha lands β‰₯0.95 across the visible surface. Those pixels still - // compose correctly under (SrcAlpha, 1-SrcAlpha) alpha-blending, so - // discarding them here threw away the whole crystal. The original - // N.5 Β§2 rationale (high-Ξ± fragments belong in the opaque pass) does - // not apply when the SURFACE is dat-flagged transparent β€” those - // pixels can't reach the opaque pass at all. - // - // Keep the Ξ±<0.05 short-circuit as a fragment-cost optimization - // (skip fully-empty pixels β€” saves blend bandwidth on alpha-keyed - // sprites with large transparent margins). - if (color.a < 0.05) discard; - } - - vec3 N = normalize(vNormal); - vec3 lit = accumulateLights(N, vWorldPos); - - // Lightning flash β€” additive scene bump (matches mesh_instanced.frag). - lit += uFogParams.z * vec3(0.6, 0.6, 0.75); - - // Retail clamp per-channel to 1.0 (r13 Β§13.1). - lit = min(lit, vec3(1.0)); - - vec3 rgb = color.rgb * lit; - rgb = applyFog(rgb, vWorldPos); - FragColor = vec4(rgb, color.a); -} diff --git a/src/AcDream.App/Rendering/Shaders/mesh_modern.vert b/src/AcDream.App/Rendering/Shaders/mesh_modern.vert deleted file mode 100644 index 2b6131f..0000000 --- a/src/AcDream.App/Rendering/Shaders/mesh_modern.vert +++ /dev/null @@ -1,77 +0,0 @@ -#version 430 core -#extension GL_ARB_shader_draw_parameters : require - -layout(location = 0) in vec3 aPosition; -layout(location = 1) in vec3 aNormal; -layout(location = 2) in vec2 aTexCoord; - -struct InstanceData { - mat4 transform; - // Reserved for Phase B.4 follow-up (selection-blink retail-faithful - // highlight): vec4 highlightColor; β€” extend stride here, increase the - // _instanceSsbo upload size in WbDrawDispatcher, add a flat varying out, - // and consume in mesh_modern.frag. -}; - -struct BatchData { - uvec2 textureHandle; // bindless handle for sampler2DArray - uint textureLayer; // layer index (always 0 for per-instance composites) - uint flags; // reserved β€” N.5 dispatcher owns all blend state - // (glBlendFunc per pass). If a future phase wants - // shader-side per-batch additive flag (Decision 2 - // fallback), encode it here as bit 0. -}; - -layout(std430, binding = 0) readonly buffer InstanceBuffer { - InstanceData Instances[]; -}; - -// binding=1 here is the SSBO namespace β€” distinct from the UBO namespace. -// SceneLighting UBO also uses binding=1 in the fragment shader; GL keeps -// GL_SHADER_STORAGE_BUFFER and GL_UNIFORM_BUFFER binding tables separate. -// Task 10 dispatcher binds: -// glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, instanceSsbo) -// glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, batchSsbo) -// Existing SceneLightingUboBinding handles the UBO side. -layout(std430, binding = 1) readonly buffer BatchBuffer { - BatchData Batches[]; -}; - -uniform mat4 uViewProjection; - -// Phase Post-A.5 (ISSUE #52, 2026-05-10): per-pass offset into Batches[]. -// gl_DrawIDARB resets to 0 at the start of each glMultiDrawElementsIndirect -// call, so the transparent pass β€” which begins later in the indirect buffer -// β€” was fetching Batches[0..transparentCount) instead of its actual section -// at Batches[opaqueCount..end). The lifestone crystal (a transparent draw) -// ended up reading the FIRST OPAQUE batch's TextureHandle every frame. As -// the camera moved and the opaque front-to-back sort reordered which group -// landed at BatchData[0], the lifestone's apparent texture flickered to -// whatever was first β€” frequently the player character's body parts. -// -// WbDrawDispatcher.Draw sets this to 0 before the opaque MDI call and to -// _opaqueDrawCount before the transparent MDI call, matching WorldBuilder's -// uDrawIDOffset pattern in BaseObjectRenderManager.cs line 845. -uniform int uDrawIDOffset; - -out vec3 vNormal; -out vec2 vTexCoord; -out vec3 vWorldPos; -out flat uvec2 vTextureHandle; -out flat uint vTextureLayer; - -void main() { - int instanceIndex = gl_BaseInstanceARB + gl_InstanceID; - mat4 model = Instances[instanceIndex].transform; - - vec4 worldPos = model * vec4(aPosition, 1.0); - gl_Position = uViewProjection * worldPos; - - vWorldPos = worldPos.xyz; - vNormal = normalize(mat3(model) * aNormal); - vTexCoord = aTexCoord; - - BatchData b = Batches[uDrawIDOffset + gl_DrawIDARB]; - vTextureHandle = b.textureHandle; - vTextureLayer = b.textureLayer; -} diff --git a/src/AcDream.App/Rendering/Shaders/terrain_modern.frag b/src/AcDream.App/Rendering/Shaders/terrain.frag similarity index 78% rename from src/AcDream.App/Rendering/Shaders/terrain_modern.frag rename to src/AcDream.App/Rendering/Shaders/terrain.frag index 27e9aa2..479939d 100644 --- a/src/AcDream.App/Rendering/Shaders/terrain_modern.frag +++ b/src/AcDream.App/Rendering/Shaders/terrain.frag @@ -1,18 +1,9 @@ -#version 460 core -#extension GL_ARB_bindless_texture : require - -// Phase N.5b: terrain fragment shader on the modern bindless dispatcher. -// Math identical to terrain.frag (Phase 3c per-cell maskBlend3 + -// Phase G fog + lightning flash). -// -// Bindless texture handles are passed as uvec2 (low/high 32 bits) and -// reconstructed into sampler2DArray at use sites via the GLSL -// sampler-from-handle constructor. The alternative pattern β€” -// `uniform sampler2DArray` set via glProgramUniformHandleARB β€” produces -// GL_INVALID_OPERATION on at least one driver in practice (NVIDIA on -// Windows). The uvec2 + constructor pattern is what N.5's mesh_modern -// shader uses and is the documented "always works" form per the -// ARB_bindless_texture spec. +#version 430 core +// Per-cell terrain blending (Phase 3c.4) β€” ported from WorldBuilder's +// Landscape.frag, trimmed of editor-specific features (grid, brush, +// walkable-slope highlighting). Phase G extends this with the shared +// SceneLighting UBO driving per-vertex sun bake + fragment-stage fog +// + lightning flash. in vec2 vBaseUV; in vec3 vWorldNormal; @@ -27,11 +18,11 @@ flat in float vBaseTexIdx; out vec4 fragColor; -uniform uvec2 uTerrainHandle; -uniform uvec2 uAlphaHandle; -#define uTerrain sampler2DArray(uTerrainHandle) -#define uAlpha sampler2DArray(uAlphaHandle) +uniform sampler2DArray uTerrain; // 33+ layers β€” TerrainAtlas.GlTexture +uniform sampler2DArray uAlpha; // 8+ layers β€” TerrainAtlas.GlAlphaTexture +// Shared scene-lighting UBO β€” fog + flash are consumed here; the per-vertex +// AdjustPlanes bake already incorporated sun + ambient. struct Light { vec4 posAndKind; vec4 dirAndRange; @@ -46,8 +37,12 @@ layout(std140, binding = 1) uniform SceneLighting { vec4 uCameraAndTime; }; +// Per-texture tiling repeat count across a cell. WorldBuilder uses +// uTexTiling[36] uploaded from the dats; we default to 1.0 (one tile per +// cell, 8 tiles across a landblock). const float TILE = 1.0; +// Three-layer alpha-weighted composite. vec4 maskBlend3(vec4 t0, vec4 t1, vec4 t2, float h0, float h1, float h2) { float a0 = h0 == 0.0 ? 1.0 : t0.a; float a1 = h1 == 0.0 ? 1.0 : t1.a; @@ -134,16 +129,20 @@ void main() { if (vRoad0.z >= 0.0) roads = combineRoad(vBaseUV, vRoad0, vRoad1); + // Composite: base Γ— (1 - ovlA) Γ— (1 - rdA) + ovl Γ— ovlA Γ— (1 - rdA) + road Γ— rdA vec3 baseMasked = baseColor.rgb * ((1.0 - overlays.a) * (1.0 - roads.a)); vec3 ovlMasked = overlays.rgb * (overlays.a * (1.0 - roads.a)); vec3 roadMasked = roads.rgb * roads.a; vec3 rgb = clamp(baseMasked + ovlMasked + roadMasked, 0.0, 1.0); + // Apply the per-vertex baked sun+ambient. vec3 lit = rgb * min(vLightingRGB, vec3(1.0)); + // Lightning flash β€” additive. float flash = uFogParams.z; lit += flash * vec3(0.6, 0.6, 0.75); + // Atmospheric fog. lit = applyFog(lit, vWorldPos); fragColor = vec4(lit, 1.0); diff --git a/src/AcDream.App/Rendering/Shaders/terrain_modern.vert b/src/AcDream.App/Rendering/Shaders/terrain.vert similarity index 76% rename from src/AcDream.App/Rendering/Shaders/terrain_modern.vert rename to src/AcDream.App/Rendering/Shaders/terrain.vert index 473cba5..11e691d 100644 --- a/src/AcDream.App/Rendering/Shaders/terrain_modern.vert +++ b/src/AcDream.App/Rendering/Shaders/terrain.vert @@ -1,21 +1,17 @@ -#version 460 core -#extension GL_ARB_bindless_texture : require - -// Phase N.5b: terrain shader on the modern bindless dispatcher. -// Math identical to terrain.vert (Phase 3c per-cell mesh + Phase G AdjustPlanes -// lighting). The only structural change is the version + bindless extension -// β€” sampler access in the fragment stage is unchanged at the GLSL level. - +#version 430 core layout(location = 0) in vec3 aPos; layout(location = 1) in vec3 aNormal; -layout(location = 2) in uvec4 aPacked0; -layout(location = 3) in uvec4 aPacked1; -layout(location = 4) in uvec4 aPacked2; -layout(location = 5) in uvec4 aPacked3; +layout(location = 2) in uvec4 aPacked0; // bytes: baseTex, baseAlpha(255), ovl0Tex, ovl0Alpha +layout(location = 3) in uvec4 aPacked1; // bytes: ovl1Tex, ovl1Alpha, ovl2Tex, ovl2Alpha +layout(location = 4) in uvec4 aPacked2; // bytes: road0Tex, road0Alpha, road1Tex, road1Alpha +layout(location = 5) in uvec4 aPacked3; // bits: rot fields + splitDir (see below) uniform mat4 uView; uniform mat4 uProjection; +// Phase G.1+G.2: sky/scene UBO. Terrain reads uLights[0] for the sun +// (slot 0 is reserved) plus uCellAmbient for outdoor ambient; the fog +// fields are consumed by the fragment stage. struct Light { vec4 posAndKind; vec4 dirAndRange; @@ -33,7 +29,9 @@ layout(std140, binding = 1) uniform SceneLighting { out vec2 vBaseUV; out vec3 vWorldNormal; out vec3 vWorldPos; -out vec3 vLightingRGB; +out vec3 vLightingRGB; // pre-computed sun+ambient contribution for retail-style AdjustPlanes bake +// Per-layer "UV.xy in cell-local 0..1 space, tex index .z, alpha index .w". +// Negative .z means "layer not present, skip it in the fragment shader." out vec4 vOverlay0; out vec4 vOverlay1; out vec4 vOverlay2; @@ -55,6 +53,9 @@ flat out float vBaseTexIdx; // Cross-ref: docs/research/2026-04-24-lambert-brightness-split.md. const float MIN_FACTOR = 0.0; +// Port of WorldBuilder's Landscape.vert unpackOverlayLayer: sentinel-check +// 255 β†’ -1 (shader skips), then rotate the cell-local UV by the overlay's +// 90Β° rotation count. vec4 unpackOverlayLayer(uint texIdxU, uint alphaIdxU, uint rotIdx, vec2 baseUV) { float texIdx = float(texIdxU); float alphaIdx = float(alphaIdxU); @@ -120,9 +121,15 @@ void main() { vWorldPos = aPos; vWorldNormal = normalize(aNormal); - // Retail AdjustPlanes bake (terrain.vert:124-134 β€” identical math). - vec3 sunDir = uLights[0].dirAndRange.xyz; - vec3 sunCol = uLights[0].colorAndIntensity.xyz * uLights[0].colorAndIntensity.w; + // Retail AdjustPlanes bake (r13 Β§7): + // L = max(N Β· -sunDir, MIN_FACTOR) + // vertex.color = sun_color * L + ambient_color + // + // Slot 0 of the UBO is the sun (directional). We read its forward + // vector and pre-multiplied color, apply the ambient floor, layer + // in the scene ambient separately. + vec3 sunDir = uLights[0].dirAndRange.xyz; + vec3 sunCol = uLights[0].colorAndIntensity.xyz * uLights[0].colorAndIntensity.w; float L = max(dot(vWorldNormal, -sunDir), MIN_FACTOR); vLightingRGB = sunCol * L + uCellAmbient.xyz; diff --git a/src/AcDream.App/Rendering/StaticMeshRenderer.cs b/src/AcDream.App/Rendering/StaticMeshRenderer.cs new file mode 100644 index 0000000..f201338 --- /dev/null +++ b/src/AcDream.App/Rendering/StaticMeshRenderer.cs @@ -0,0 +1,293 @@ +// src/AcDream.App/Rendering/StaticMeshRenderer.cs +using System.Numerics; +using AcDream.Core.Meshing; +using AcDream.Core.Terrain; +using AcDream.Core.World; +using Silk.NET.OpenGL; + +namespace AcDream.App.Rendering; + +public sealed unsafe class StaticMeshRenderer : IDisposable +{ + private readonly GL _gl; + private readonly Shader _shader; + private readonly TextureCache _textures; + + // One GPU bundle per unique GfxObj id. Each GfxObj can have multiple sub-meshes. + private readonly Dictionary> _gpuByGfxObj = new(); + + public StaticMeshRenderer(GL gl, Shader shader, TextureCache textures) + { + _gl = gl; + _shader = shader; + _textures = textures; + } + + public void EnsureUploaded(uint gfxObjId, IReadOnlyList subMeshes) + { + if (_gpuByGfxObj.ContainsKey(gfxObjId)) + return; + + var list = new List(subMeshes.Count); + foreach (var sm in subMeshes) + list.Add(UploadSubMesh(sm)); + _gpuByGfxObj[gfxObjId] = list; + } + + private SubMeshGpu UploadSubMesh(GfxObjSubMesh sm) + { + uint vao = _gl.GenVertexArray(); + _gl.BindVertexArray(vao); + + uint vbo = _gl.GenBuffer(); + _gl.BindBuffer(BufferTargetARB.ArrayBuffer, vbo); + fixed (void* p = sm.Vertices) + _gl.BufferData(BufferTargetARB.ArrayBuffer, + (nuint)(sm.Vertices.Length * sizeof(Vertex)), p, BufferUsageARB.StaticDraw); + + uint ebo = _gl.GenBuffer(); + _gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, ebo); + fixed (void* p = sm.Indices) + _gl.BufferData(BufferTargetARB.ElementArrayBuffer, + (nuint)(sm.Indices.Length * sizeof(uint)), p, BufferUsageARB.StaticDraw); + + uint stride = (uint)sizeof(Vertex); + _gl.EnableVertexAttribArray(0); + _gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, stride, (void*)0); + _gl.EnableVertexAttribArray(1); + _gl.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, stride, (void*)(3 * sizeof(float))); + _gl.EnableVertexAttribArray(2); + _gl.VertexAttribPointer(2, 2, VertexAttribPointerType.Float, false, stride, (void*)(6 * sizeof(float))); + _gl.EnableVertexAttribArray(3); + _gl.VertexAttribIPointer(3, 1, VertexAttribIType.UnsignedInt, stride, (void*)(8 * sizeof(float))); + + _gl.BindVertexArray(0); + + return new SubMeshGpu + { + Vao = vao, + Vbo = vbo, + Ebo = ebo, + IndexCount = sm.Indices.Length, + SurfaceId = sm.SurfaceId, + // Capture translucency at upload time so the draw loop never + // has to look it up from external state. + Translucency = sm.Translucency, + }; + } + + public void Draw(ICamera camera, + IEnumerable<(uint LandblockId, Vector3 AabbMin, Vector3 AabbMax, IReadOnlyList Entities)> landblockEntries, + FrustumPlanes? frustum = null, + uint? neverCullLandblockId = null) + { + _shader.Use(); + _shader.SetMatrix4("uView", camera.View); + _shader.SetMatrix4("uProjection", camera.Projection); + + // ── Pass 1: Opaque + ClipMap ────────────────────────────────────────── + // Depth write on (default). No blending. ClipMap surfaces use the + // alpha-discard path in the fragment shader (uTranslucencyKind == 1). + foreach (var entry in landblockEntries) + { + // Per-landblock frustum cull. Never cull the player's landblock. + if (frustum is not null && + entry.LandblockId != neverCullLandblockId && + !FrustumCuller.IsAabbVisible(frustum.Value, entry.AabbMin, entry.AabbMax)) + continue; + + foreach (var entity in entry.Entities) + { + if (entity.MeshRefs.Count == 0) + continue; + + foreach (var meshRef in entity.MeshRefs) + { + if (!_gpuByGfxObj.TryGetValue(meshRef.GfxObjId, out var subMeshes)) + continue; + + var entityRoot = + Matrix4x4.CreateFromQuaternion(entity.Rotation) * + Matrix4x4.CreateTranslation(entity.Position); + var model = meshRef.PartTransform * entityRoot; + _shader.SetMatrix4("uModel", model); + + foreach (var sub in subMeshes) + { + // Skip translucent sub-meshes in the first pass. + if (sub.Translucency != TranslucencyKind.Opaque && + sub.Translucency != TranslucencyKind.ClipMap) + continue; + + _shader.SetInt("uTranslucencyKind", (int)sub.Translucency); + + uint tex = ResolveTex(entity, meshRef, sub); + _gl.ActiveTexture(TextureUnit.Texture0); + _gl.BindTexture(TextureTarget.Texture2D, tex); + + _gl.BindVertexArray(sub.Vao); + _gl.DrawElements(PrimitiveType.Triangles, (uint)sub.IndexCount, DrawElementsType.UnsignedInt, (void*)0); + } + } + } + } + + // ── Pass 2: Translucent (AlphaBlend, Additive, InvAlpha) ───────────── + // Depth test on so translucents composite correctly behind opaque geometry. + // Depth write OFF so translucents don't occlude each other or downstream + // opaque draws. Blend function is set per-draw based on TranslucencyKind. + // + // NOTE: translucent draws are NOT sorted by depth β€” overlapping translucent + // surfaces can composite in the wrong order. Portal-sized billboards don't + // overlap in practice so this is acceptable and avoids a larger refactor. + _gl.Enable(EnableCap.Blend); + _gl.DepthMask(false); + + // Phase 9.2: enable back-face culling for the translucent pass so + // closed-shell translucents (lifestone crystal, glow gems, any + // convex blended mesh) don't draw their back faces over their + // front faces in arbitrary iteration order. Without this, the + // 58 triangles of the lifestone crystal composited with an + // "inside-out" look where the user saw through one face into + // the hollow interior. With back-face culling on, back faces are + // dropped at rasterization time, front faces composite as-is, + // and depth ordering within the front-facing subset is a + // non-issue for closed convex-ish shells. Matches WorldBuilder's + // per-batch CullMode handling in + // references/WorldBuilder/Chorizite.OpenGLSDLBackend/Lib/ + // BaseObjectRenderManager.cs:361-365. + // + // Our fan triangulation emits pos-side polygons as + // (0, i, i+1) which is CCW in standard OpenGL conventions, so + // GL_BACK + CCW front is the correct state. Neg-side polygons + // (if any) use reversed winding and get culled here β€” that's a + // known limitation and matches the opaque-pass behavior since + // neg-side polys are virtually never translucent in AC content. + _gl.Enable(EnableCap.CullFace); + _gl.CullFace(TriangleFace.Back); + _gl.FrontFace(FrontFaceDirection.Ccw); + + foreach (var entry in landblockEntries) + { + // Same per-landblock frustum cull for pass 2. + if (frustum is not null && + entry.LandblockId != neverCullLandblockId && + !FrustumCuller.IsAabbVisible(frustum.Value, entry.AabbMin, entry.AabbMax)) + continue; + + foreach (var entity in entry.Entities) + { + if (entity.MeshRefs.Count == 0) + continue; + + foreach (var meshRef in entity.MeshRefs) + { + if (!_gpuByGfxObj.TryGetValue(meshRef.GfxObjId, out var subMeshes)) + continue; + + var entityRoot = + Matrix4x4.CreateFromQuaternion(entity.Rotation) * + Matrix4x4.CreateTranslation(entity.Position); + var model = meshRef.PartTransform * entityRoot; + _shader.SetMatrix4("uModel", model); + + foreach (var sub in subMeshes) + { + if (sub.Translucency == TranslucencyKind.Opaque || + sub.Translucency == TranslucencyKind.ClipMap) + continue; + + // Set per-draw blend function. + switch (sub.Translucency) + { + case TranslucencyKind.Additive: + // src*a + dst β€” portal swirls, glows + _gl.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.One); + break; + + case TranslucencyKind.InvAlpha: + // src*(1-a) + dst*a + _gl.BlendFunc(BlendingFactor.OneMinusSrcAlpha, BlendingFactor.SrcAlpha); + break; + + default: // AlphaBlend + // src*a + dst*(1-a) + _gl.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha); + break; + } + + _shader.SetInt("uTranslucencyKind", (int)sub.Translucency); + + uint tex = ResolveTex(entity, meshRef, sub); + _gl.ActiveTexture(TextureUnit.Texture0); + _gl.BindTexture(TextureTarget.Texture2D, tex); + + _gl.BindVertexArray(sub.Vao); + _gl.DrawElements(PrimitiveType.Triangles, (uint)sub.IndexCount, DrawElementsType.UnsignedInt, (void*)0); + } + } + } + } + + // Restore default GL state for subsequent renderers (terrain etc.). + _gl.DepthMask(true); + _gl.Disable(EnableCap.Blend); + _gl.Disable(EnableCap.CullFace); + + _gl.BindVertexArray(0); + } + + /// + /// Resolves the GL texture id for a sub-mesh, honouring palette and + /// texture overrides carried on the entity and the mesh-ref. + /// + private uint ResolveTex(WorldEntity entity, MeshRef meshRef, SubMeshGpu sub) + { + uint overrideOrigTex = 0; + bool hasOrigTexOverride = meshRef.SurfaceOverrides is not null + && meshRef.SurfaceOverrides.TryGetValue(sub.SurfaceId, out overrideOrigTex); + uint? origTexOverride = hasOrigTexOverride ? overrideOrigTex : (uint?)null; + + if (entity.PaletteOverride is not null) + { + return _textures.GetOrUploadWithPaletteOverride( + sub.SurfaceId, origTexOverride, entity.PaletteOverride); + } + else if (hasOrigTexOverride) + { + return _textures.GetOrUploadWithOrigTextureOverride(sub.SurfaceId, overrideOrigTex); + } + else + { + return _textures.GetOrUpload(sub.SurfaceId); + } + } + + public void Dispose() + { + foreach (var subs in _gpuByGfxObj.Values) + { + foreach (var sub in subs) + { + _gl.DeleteBuffer(sub.Vbo); + _gl.DeleteBuffer(sub.Ebo); + _gl.DeleteVertexArray(sub.Vao); + } + } + _gpuByGfxObj.Clear(); + } + + private sealed class SubMeshGpu + { + public uint Vao; + public uint Vbo; + public uint Ebo; + public int IndexCount; + public uint SurfaceId; + /// + /// Cached from GfxObjSubMesh.Translucency at upload time. + /// Avoids any per-draw lookup into external state. + /// + public TranslucencyKind Translucency; + } +} diff --git a/src/AcDream.App/Rendering/TerrainAtlas.cs b/src/AcDream.App/Rendering/TerrainAtlas.cs index 03f66f6..6e8584a 100644 --- a/src/AcDream.App/Rendering/TerrainAtlas.cs +++ b/src/AcDream.App/Rendering/TerrainAtlas.cs @@ -53,45 +53,14 @@ public sealed unsafe class TerrainAtlas : IDisposable /// RCode for each RoadMap, parallel to . public IReadOnlyList RoadAlphaRCodes { get; } - private readonly Wb.BindlessSupport? _bindless; - - // Cached bindless handles. Generated lazily on first GetBindlessHandles() call; - // reused for the lifetime of the atlas. - private ulong _terrainHandle; - private ulong _alphaHandle; - private bool _handlesGenerated; - - /// - /// Get 64-bit bindless handles for the terrain + alpha texture arrays. - /// Throws if the atlas was constructed - /// without a instance. Handles are generated - /// lazily on first call and cached for the atlas's lifetime; both textures - /// are made resident. - /// - public (ulong terrain, ulong alpha) GetBindlessHandles() - { - if (_bindless is null) - throw new InvalidOperationException( - "TerrainAtlas was constructed without BindlessSupport; cannot return bindless handles."); - if (!_handlesGenerated) - { - _terrainHandle = _bindless.GetResidentHandle(GlTexture); - _alphaHandle = _bindless.GetResidentHandle(GlAlphaTexture); - _handlesGenerated = true; - } - return (_terrainHandle, _alphaHandle); - } - private TerrainAtlas( GL gl, - Wb.BindlessSupport? bindless, uint glTexture, IReadOnlyDictionary map, int layerCount, uint glAlphaTexture, int alphaLayerCount, IReadOnlyList cornerLayers, IReadOnlyList sideLayers, IReadOnlyList roadLayers, IReadOnlyList cornerTCodes, IReadOnlyList sideTCodes, IReadOnlyList roadRCodes) { _gl = gl; - _bindless = bindless; GlTexture = glTexture; TerrainTypeToLayer = map; LayerCount = layerCount; @@ -110,7 +79,7 @@ public sealed unsafe class TerrainAtlas : IDisposable /// for the mapping from TerrainTextureType to SurfaceTexture id, decoding each /// to RGBA8, and uploading as layers in a single GL_TEXTURE_2D_ARRAY. /// - public static TerrainAtlas Build(GL gl, DatCollection dats, Wb.BindlessSupport? bindless = null) + public static TerrainAtlas Build(GL gl, DatCollection dats) { var region = dats.Get(0x13000000u) ?? throw new InvalidOperationException("Region dat id 0x13000000 missing"); @@ -120,7 +89,7 @@ public sealed unsafe class TerrainAtlas : IDisposable if (terrainDesc is null || terrainDesc.Count == 0) { Console.WriteLine("WARN: TerrainDesc missing, using single white fallback layer"); - return BuildFallback(gl, bindless); + return BuildFallback(gl); } // ---- Terrain atlas (unchanged Phase 2b logic) ---- @@ -183,17 +152,13 @@ public sealed unsafe class TerrainAtlas : IDisposable layerIdx++; } - // A.5 T19: generate mipmaps + trilinear + 16x anisotropic for distant-LB quality. - gl.GenerateMipmap(TextureTarget.Texture2DArray); - gl.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear); + gl.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); gl.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); gl.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat); gl.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat); - // GL_TEXTURE_MAX_ANISOTROPY = 0x84FE (GL_EXT_texture_filter_anisotropic / ARB_texture_filter_anisotropic). - gl.TexParameter(TextureTarget.Texture2DArray, (TextureParameterName)0x84FE, 16.0f); gl.BindTexture(TextureTarget.Texture2DArray, 0); - Console.WriteLine($"TerrainAtlas: {layerCount} terrain layers at {maxW}x{maxH} (mipmaps+aniso16x)"); + Console.WriteLine($"TerrainAtlas: {layerCount} terrain layers at {maxW}x{maxH}"); // ---- Alpha atlas (new in Phase 3c.2) ---- // texMerge is guaranteed non-null here: the early return above exited @@ -202,7 +167,6 @@ public sealed unsafe class TerrainAtlas : IDisposable return new TerrainAtlas( gl, - bindless, tex, map, layerCount, alphaBuild.gl, alphaBuild.layerCount, alphaBuild.corner, alphaBuild.side, alphaBuild.road, @@ -352,10 +316,10 @@ public sealed unsafe class TerrainAtlas : IDisposable return false; // Alpha maps ship as PFID_CUSTOM_LSCAPE_ALPHA (AC's landscape-alpha - // format) or the more generic PFID_A8; terrain blending alpha masks - // MUST use isAdditive=true so R=G=B=A=val β€” the terrain fragment shader - // reads .r for the blend weight. Palette is not used. - var d = SurfaceDecoder.DecodeRenderSurface(rs, palette: null, isClipMap: false, isAdditive: true); + // format) or the more generic PFID_A8; SurfaceDecoder routes both + // through the same "replicate single byte to RGBA" path. Palette is + // not used. + var d = SurfaceDecoder.DecodeRenderSurface(rs, palette: null); if (ReferenceEquals(d, DecodedTexture.Magenta)) return false; @@ -386,7 +350,7 @@ public sealed unsafe class TerrainAtlas : IDisposable return dst; } - private static TerrainAtlas BuildFallback(GL gl, Wb.BindlessSupport? bindless = null) + private static TerrainAtlas BuildFallback(GL gl) { uint tex = gl.GenTexture(); gl.BindTexture(TextureTarget.Texture2DArray, tex); @@ -408,62 +372,14 @@ public sealed unsafe class TerrainAtlas : IDisposable return new TerrainAtlas( gl, - bindless, tex, new Dictionary { [0] = 0u }, 1, alphaTex, 1, Array.Empty(), Array.Empty(), Array.Empty(), Array.Empty(), Array.Empty(), Array.Empty()); } - /// - /// A.5 T22.5: update GL_TEXTURE_MAX_ANISOTROPY on the terrain atlas at - /// runtime (called by when - /// the user changes Quality preset mid-session). Idempotent β€” calling with - /// the same level as the current setting is safe and produces no visual - /// change. The texture must not be resident-bindless when its parameters - /// are mutated; we temporarily make it non-resident if needed. - /// - public void SetAnisotropic(int level) - { - // If bindless handles are live we must make them non-resident before - // mutating texture state, then re-resident after. - bool wasResident = _handlesGenerated && _bindless is not null; - if (wasResident) - { - _bindless!.MakeNonResident(_terrainHandle); - // Alpha texture is not affected by anisotropic but we must keep - // residency symmetric β€” re-generate both handles after. - _bindless.MakeNonResident(_alphaHandle); - _handlesGenerated = false; - } - - _gl.BindTexture(TextureTarget.Texture2DArray, GlTexture); - // GL_TEXTURE_MAX_ANISOTROPY = 0x84FE - _gl.TexParameter(TextureTarget.Texture2DArray, (TextureParameterName)0x84FE, (float)level); - _gl.BindTexture(TextureTarget.Texture2DArray, 0); - - // Re-generate bindless handles if they were live before. - if (wasResident) - { - // GetBindlessHandles regenerates and makes resident. - _ = GetBindlessHandles(); - } - - Console.WriteLine($"TerrainAtlas: anisotropic updated to {level}x"); - } - public void Dispose() { - // Phase 1: release bindless residency BEFORE deleting textures. - // ARB_bindless_texture requires this ordering; interleaving is UB. - if (_handlesGenerated && _bindless is not null) - { - _bindless.MakeNonResident(_terrainHandle); - _bindless.MakeNonResident(_alphaHandle); - _handlesGenerated = false; - } - - // Phase 2: delete the underlying GL textures. _gl.DeleteTexture(GlTexture); _gl.DeleteTexture(GlAlphaTexture); } diff --git a/src/AcDream.App/Rendering/TerrainChunkRenderer.cs b/src/AcDream.App/Rendering/TerrainChunkRenderer.cs new file mode 100644 index 0000000..cd2df6a --- /dev/null +++ b/src/AcDream.App/Rendering/TerrainChunkRenderer.cs @@ -0,0 +1,454 @@ +using System.Numerics; +using AcDream.Core.Terrain; +using Silk.NET.OpenGL; + +namespace AcDream.App.Rendering; + +/// +/// Chunk-based terrain renderer matching ACME's architecture. Each 16x16 +/// landblock region gets its own VAO/VBO/EBO with pre-allocated max-size +/// buffers. Landblocks are added/removed incrementally via glBufferSubData +/// instead of rebuilding the entire buffer. +/// +/// Attribute layout (same as TerrainRenderer, see TerrainVertex): +/// location 0: vec3 aPos (3 floats, world space) +/// location 1: vec3 aNormal (3 floats) +/// location 2: uvec4 aPacked0 (4 bytes, Data0) +/// location 3: uvec4 aPacked1 (4 bytes, Data1) +/// location 4: uvec4 aPacked2 (4 bytes, Data2) +/// location 5: uvec4 aPacked3 (4 bytes, Data3) +/// +public sealed unsafe class TerrainChunkRenderer : IDisposable +{ + // ------------------------------------------------------------------------- + // Constants + // ------------------------------------------------------------------------- + + /// Number of landblocks per chunk dimension (matching ACME). + public const int ChunkSizeInLandblocks = 16; + + /// Max landblock slots per chunk (16x16 = 256). + public const int SlotsPerChunk = ChunkSizeInLandblocks * ChunkSizeInLandblocks; + + /// Vertices per landblock: 64 cells x 6 verts = 384. + public const int VerticesPerLandblock = LandblockMesh.VerticesPerLandblock; + + /// Indices per landblock (trivial 0..383, same count as vertices). + public const int IndicesPerLandblock = VerticesPerLandblock; + + /// Byte size of one TerrainVertex (40 bytes). + private static readonly int VertexSize = sizeof(TerrainVertex); + + /// Max VBO size per chunk: 256 slots x 384 verts x 40 bytes = ~3.75 MB. + private static readonly nuint MaxVboBytes = + (nuint)(SlotsPerChunk * VerticesPerLandblock * VertexSize); + + /// Max EBO size per chunk: 256 slots x 384 indices x 4 bytes = ~393 KB. + private static readonly nuint MaxEboBytes = + (nuint)(SlotsPerChunk * IndicesPerLandblock * sizeof(uint)); + + // ------------------------------------------------------------------------- + // Fields + // ------------------------------------------------------------------------- + + private readonly GL _gl; + private readonly Shader _shader; + private readonly TerrainAtlas _atlas; + + /// Active chunks keyed by (chunkX, chunkY) packed into a ulong. + private readonly Dictionary _chunks = new(); + + /// Reverse map: landblockId -> chunkId, for fast RemoveLandblock. + private readonly Dictionary _landblockToChunk = new(); + + // ------------------------------------------------------------------------- + // Construction + // ------------------------------------------------------------------------- + + public TerrainChunkRenderer(GL gl, Shader shader, TerrainAtlas atlas) + { + _gl = gl; + _shader = shader; + _atlas = atlas; + } + + // ------------------------------------------------------------------------- + // Public API + // ------------------------------------------------------------------------- + + /// + /// Add (or replace) a landblock's terrain mesh. Vertices are baked to world + /// space using , then uploaded to the correct + /// chunk buffer slot via glBufferSubData. + /// + public void AddLandblock(uint landblockId, LandblockMeshData meshData, Vector3 worldOrigin) + { + // If this landblock already exists, remove it first. + if (_landblockToChunk.ContainsKey(landblockId)) + RemoveLandblock(landblockId); + + // Determine chunk coordinates and slot index. + // Landblock ID format: 0xXXYYnnnn (X at bits 24-31, Y at bits 16-23). + int lbX = (int)(landblockId >> 24) & 0xFF; + int lbY = (int)(landblockId >> 16) & 0xFF; + int chunkX = lbX / ChunkSizeInLandblocks; + int chunkY = lbY / ChunkSizeInLandblocks; + ulong chunkId = PackChunkId(chunkX, chunkY); + + int localX = lbX % ChunkSizeInLandblocks; + int localY = lbY % ChunkSizeInLandblocks; + int slotIndex = localX * ChunkSizeInLandblocks + localY; + + // Create chunk on demand. + if (!_chunks.TryGetValue(chunkId, out var chunk)) + { + chunk = CreateChunk(chunkX, chunkY); + _chunks[chunkId] = chunk; + } + + // Bake world-space vertices. + var worldVerts = new TerrainVertex[meshData.Vertices.Length]; + float zMin = float.MaxValue, zMax = float.MinValue; + for (int i = 0; i < meshData.Vertices.Length; i++) + { + var v = meshData.Vertices[i]; + var worldPos = v.Position + worldOrigin; + worldVerts[i] = new TerrainVertex(worldPos, v.Normal, v.Data0, v.Data1, v.Data2, v.Data3); + if (worldPos.Z < zMin) zMin = worldPos.Z; + if (worldPos.Z > zMax) zMax = worldPos.Z; + } + if (zMin == float.MaxValue) { zMin = 0f; zMax = 0f; } + + // Upload vertices into the slot's region of the VBO. + nint vboOffset = (nint)(slotIndex * VerticesPerLandblock * VertexSize); + _gl.BindBuffer(BufferTargetARB.ArrayBuffer, chunk.Vbo); + fixed (void* p = worldVerts) + { + _gl.BufferSubData(BufferTargetARB.ArrayBuffer, vboOffset, + (nuint)(worldVerts.Length * VertexSize), p); + } + _gl.BindBuffer(BufferTargetARB.ArrayBuffer, 0); + + // Track the slot. + chunk.Slots[slotIndex] = new LandblockSlot + { + LandblockId = landblockId, + WorldOrigin = worldOrigin, + MinZ = zMin, + MaxZ = zMax, + }; + chunk.Occupied.Add(slotIndex); + _landblockToChunk[landblockId] = chunkId; + + // Rebuild the EBO for this chunk (only includes occupied slots). + RebuildChunkEbo(chunk); + + // Update chunk AABB. + UpdateChunkBounds(chunk); + } + + /// + /// Remove a landblock from its chunk. If the chunk becomes empty, dispose it. + /// + public void RemoveLandblock(uint landblockId) + { + if (!_landblockToChunk.TryGetValue(landblockId, out var chunkId)) + return; + + _landblockToChunk.Remove(landblockId); + + if (!_chunks.TryGetValue(chunkId, out var chunk)) + return; + + // Find which slot this landblock occupies. + int slotIndex = -1; + foreach (var s in chunk.Occupied) + { + if (chunk.Slots[s].LandblockId == landblockId) + { + slotIndex = s; + break; + } + } + if (slotIndex < 0) + return; + + // Zero out the VBO region for this slot (optional but clean). + nint vboOffset = (nint)(slotIndex * VerticesPerLandblock * VertexSize); + nuint vboSize = (nuint)(VerticesPerLandblock * VertexSize); + var zeros = new byte[VerticesPerLandblock * VertexSize]; + _gl.BindBuffer(BufferTargetARB.ArrayBuffer, chunk.Vbo); + fixed (void* p = zeros) + { + _gl.BufferSubData(BufferTargetARB.ArrayBuffer, vboOffset, vboSize, p); + } + _gl.BindBuffer(BufferTargetARB.ArrayBuffer, 0); + + chunk.Slots[slotIndex] = default; + chunk.Occupied.Remove(slotIndex); + + if (chunk.Occupied.Count == 0) + { + // Chunk is empty -- dispose GPU resources. + chunk.Dispose(_gl); + _chunks.Remove(chunkId); + } + else + { + RebuildChunkEbo(chunk); + UpdateChunkBounds(chunk); + } + } + + /// + /// Draw all visible terrain chunks. One glDrawElements per non-empty chunk. + /// Frustum culling is performed at the chunk AABB level. + /// + public void Draw(ICamera camera, FrustumPlanes? frustum = null, uint? neverCullLandblockId = null) + { + if (_chunks.Count == 0) + return; + + // Determine which chunk the never-cull landblock lives in. + ulong? neverCullChunkId = null; + if (neverCullLandblockId is not null && _landblockToChunk.TryGetValue(neverCullLandblockId.Value, out var ncId)) + neverCullChunkId = ncId; + + _shader.Use(); + _shader.SetMatrix4("uView", camera.View); + _shader.SetMatrix4("uProjection", camera.Projection); + + // Phase G: light direction + ambient + fog come from the shared + // SceneLighting UBO (binding=1) uploaded by GameWindow once per + // frame. Terrain bakes per-vertex AdjustPlanes lighting (r13 Β§7) + // from the UBO's slot-0 sun + uCellAmbient, then the fragment + // stage adds fog + lightning flash. No per-program uniforms here. + + // Terrain atlas on unit 0, alpha atlas on unit 1. + _gl.ActiveTexture(TextureUnit.Texture0); + _gl.BindTexture(TextureTarget.Texture2DArray, _atlas.GlTexture); + _gl.ActiveTexture(TextureUnit.Texture1); + _gl.BindTexture(TextureTarget.Texture2DArray, _atlas.GlAlphaTexture); + + int terrainLoc = _gl.GetUniformLocation(_shader.Program, "uTerrain"); + if (terrainLoc >= 0) _gl.Uniform1(terrainLoc, 0); + int alphaLoc = _gl.GetUniformLocation(_shader.Program, "uAlpha"); + if (alphaLoc >= 0) _gl.Uniform1(alphaLoc, 1); + + foreach (var (chunkId, chunk) in _chunks) + { + if (chunk.IndexCount == 0) + continue; + + // Chunk-level frustum cull. + if (frustum is not null && chunkId != neverCullChunkId) + { + if (!FrustumCuller.IsAabbVisible(frustum.Value, chunk.AabbMin, chunk.AabbMax)) + continue; + } + + _gl.BindVertexArray(chunk.Vao); + _gl.DrawElements( + PrimitiveType.Triangles, + (uint)chunk.IndexCount, + DrawElementsType.UnsignedInt, + (void*)0); + } + + _gl.BindVertexArray(0); + } + + public void Dispose() + { + foreach (var chunk in _chunks.Values) + chunk.Dispose(_gl); + + _chunks.Clear(); + _landblockToChunk.Clear(); + } + + // ------------------------------------------------------------------------- + // Private helpers + // ------------------------------------------------------------------------- + + private static ulong PackChunkId(int chunkX, int chunkY) + => ((ulong)(uint)chunkX << 32) | (uint)chunkY; + + /// + /// Allocate a new chunk with max-size VBO and empty EBO, plus a configured VAO. + /// + private ChunkData CreateChunk(int chunkX, int chunkY) + { + var chunk = new ChunkData + { + ChunkX = chunkX, + ChunkY = chunkY, + Vao = _gl.GenVertexArray(), + Vbo = _gl.GenBuffer(), + Ebo = _gl.GenBuffer(), + }; + + // Pre-allocate VBO to max size with DynamicDraw. + _gl.BindBuffer(BufferTargetARB.ArrayBuffer, chunk.Vbo); + _gl.BufferData(BufferTargetARB.ArrayBuffer, MaxVboBytes, null, BufferUsageARB.DynamicDraw); + _gl.BindBuffer(BufferTargetARB.ArrayBuffer, 0); + + // Pre-allocate EBO (empty initially, will be rebuilt on first AddLandblock). + _gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, chunk.Ebo); + _gl.BufferData(BufferTargetARB.ElementArrayBuffer, MaxEboBytes, null, BufferUsageARB.DynamicDraw); + _gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, 0); + + // Configure VAO with the same attribute layout as the old TerrainRenderer. + ConfigureVao(chunk); + + return chunk; + } + + /// + /// Set up vertex attribute pointers on the chunk's VAO. Identical layout + /// to the old TerrainRenderer. + /// + private void ConfigureVao(ChunkData chunk) + { + _gl.BindVertexArray(chunk.Vao); + _gl.BindBuffer(BufferTargetARB.ArrayBuffer, chunk.Vbo); + _gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, chunk.Ebo); + + uint stride = (uint)VertexSize; + + // location 0: Position (12 bytes) + _gl.EnableVertexAttribArray(0); + _gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, stride, (void*)0); + // location 1: Normal (12 bytes, offset 12) + _gl.EnableVertexAttribArray(1); + _gl.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, stride, (void*)(3 * sizeof(float))); + + // location 2..5: Data0..Data3 as uvec4 byte attributes (4 bytes each, offsets 24, 28, 32, 36). + nint dataOffset = 6 * sizeof(float); // 24 bytes + _gl.EnableVertexAttribArray(2); + _gl.VertexAttribIPointer(2, 4, VertexAttribIType.UnsignedByte, stride, (void*)dataOffset); + _gl.EnableVertexAttribArray(3); + _gl.VertexAttribIPointer(3, 4, VertexAttribIType.UnsignedByte, stride, (void*)(dataOffset + 4)); + _gl.EnableVertexAttribArray(4); + _gl.VertexAttribIPointer(4, 4, VertexAttribIType.UnsignedByte, stride, (void*)(dataOffset + 8)); + _gl.EnableVertexAttribArray(5); + _gl.VertexAttribIPointer(5, 4, VertexAttribIType.UnsignedByte, stride, (void*)(dataOffset + 12)); + + _gl.BindVertexArray(0); + } + + /// + /// Rebuild the EBO for a chunk, emitting rebased indices only for occupied + /// slots. Each slot's indices are offset by (slotIndex * VerticesPerLandblock) + /// so they point to the correct region of the VBO. + /// + private void RebuildChunkEbo(ChunkData chunk) + { + int totalIndices = chunk.Occupied.Count * IndicesPerLandblock; + var indices = new uint[totalIndices]; + + int writePos = 0; + foreach (var slotIndex in chunk.Occupied) + { + uint vertexBase = (uint)(slotIndex * VerticesPerLandblock); + for (uint i = 0; i < IndicesPerLandblock; i++) + indices[writePos++] = vertexBase + i; + } + + _gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, chunk.Ebo); + fixed (void* p = indices) + { + _gl.BufferSubData(BufferTargetARB.ElementArrayBuffer, 0, + (nuint)(totalIndices * sizeof(uint)), p); + } + _gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, 0); + + chunk.IndexCount = totalIndices; + } + + /// + /// Recompute the chunk's world-space AABB from all occupied landblock slots. + /// + private static void UpdateChunkBounds(ChunkData chunk) + { + float minX = float.MaxValue, minY = float.MaxValue, minZ = float.MaxValue; + float maxX = float.MinValue, maxY = float.MinValue, maxZ = float.MinValue; + + foreach (var slotIndex in chunk.Occupied) + { + var slot = chunk.Slots[slotIndex]; + float ox = slot.WorldOrigin.X; + float oy = slot.WorldOrigin.Y; + + if (ox < minX) minX = ox; + if (oy < minY) minY = oy; + if (slot.MinZ < minZ) minZ = slot.MinZ; + + float ex = ox + LandblockMesh.LandblockSize; + float ey = oy + LandblockMesh.LandblockSize; + if (ex > maxX) maxX = ex; + if (ey > maxY) maxY = ey; + if (slot.MaxZ > maxZ) maxZ = slot.MaxZ; + } + + if (minX == float.MaxValue) + { + chunk.AabbMin = Vector3.Zero; + chunk.AabbMax = Vector3.Zero; + } + else + { + chunk.AabbMin = new Vector3(minX, minY, minZ); + chunk.AabbMax = new Vector3(maxX, maxY, maxZ); + } + } + + // ------------------------------------------------------------------------- + // Inner types + // ------------------------------------------------------------------------- + + /// + /// Per-landblock slot tracking within a chunk's VBO. + /// + private struct LandblockSlot + { + public uint LandblockId; + public Vector3 WorldOrigin; + public float MinZ; + public float MaxZ; + } + + /// + /// GPU resources and metadata for a single 16x16 terrain chunk. + /// + private sealed class ChunkData + { + public int ChunkX; + public int ChunkY; + + // GPU handles. + public uint Vao; + public uint Vbo; + public uint Ebo; + + /// Per-slot landblock data. Indexed by (localX * 16 + localY). + public readonly LandblockSlot[] Slots = new LandblockSlot[SlotsPerChunk]; + + /// Set of occupied slot indices within this chunk. + public readonly HashSet Occupied = new(); + + /// Current number of valid indices in the EBO (set by RebuildChunkEbo). + public int IndexCount; + + /// World-space AABB for chunk-level frustum culling. + public Vector3 AabbMin; + public Vector3 AabbMax; + + public void Dispose(GL gl) + { + gl.DeleteVertexArray(Vao); + gl.DeleteBuffer(Vbo); + gl.DeleteBuffer(Ebo); + } + } +} diff --git a/src/AcDream.App/Rendering/TerrainModernRenderer.cs b/src/AcDream.App/Rendering/TerrainModernRenderer.cs deleted file mode 100644 index 0145ce9..0000000 --- a/src/AcDream.App/Rendering/TerrainModernRenderer.cs +++ /dev/null @@ -1,376 +0,0 @@ -using System.Numerics; -using AcDream.App.Rendering.Wb; -using AcDream.Core.Terrain; -using Silk.NET.OpenGL; - -namespace AcDream.App.Rendering; - -/// -/// Phase N.5b modern terrain dispatcher. Single global VBO/EBO with a slot -/// allocator (one slot per landblock, 384 verts Γ— 40 bytes = 15,360 bytes -/// per slot). Per-frame: build a DrawElementsIndirectCommand array from -/// visible slots, upload, dispatch via glMultiDrawElementsIndirect. Atlas -/// textures bound via bindless handles set per-frame as sampler uniforms. -/// -/// Total ~6-8 GL calls per frame for terrain regardless of visible -/// landblock count. -/// -public sealed unsafe class TerrainModernRenderer : IDisposable -{ - // VertsPerLandblock MUST stay divisible by 6 β€” terrain_modern.vert uses - // `gl_VertexID % 6` to pick the cell-corner index (BL/BR/TR/TL), and - // because we bake `slot * VertsPerLandblock` into indices CPU-side and - // pass BaseVertex=0 to MultiDrawElementsIndirect, gl_VertexID becomes - // `slot * VertsPerLandblock + local_index`. The shader's modulo-6 only - // reduces to `local_index % 6` because 384 is a multiple of 6. Changing - // either constant without auditing the shader will silently mis-render. - private const int VertsPerLandblock = LandblockMesh.VerticesPerLandblock; // 384 (= 64 cells * 6 verts) - private const int IndicesPerLandblock = VertsPerLandblock; - private const int VertexSize = 40; // sizeof(TerrainVertex) - private const int IndexSize = sizeof(uint); - private const float LandblockSize = LandblockMesh.LandblockSize; // 192 - - private readonly GL _gl; - private readonly BindlessSupport _bindless; - private readonly Shader _shader; - private readonly TerrainAtlas _atlas; - - /// A.5 T22.5: exposes the terrain atlas so callers can update - /// anisotropic level mid-session via . - public TerrainAtlas Atlas => _atlas; - - private readonly TerrainSlotAllocator _alloc; - - // Per-slot live data (index by slot integer; null entries are unused slots). - private SlotData?[] _slots; - - // Reverse map: landblockId -> slot, for RemoveLandblock and replacement. - private readonly Dictionary _idToSlot = new(); - - // GPU buffers. - private uint _globalVao; - private uint _globalVbo; - private uint _globalEbo; - private uint _indirectBuffer; - private int _indirectCapacity; - - // Cached uvec2-handle uniform locations (matrix uniforms are set by name via Shader.SetMatrix4). - private int _uTerrainHandleLoc; - private int _uAlphaHandleLoc; - - // Reusable per-frame buffers. - private readonly List _visibleSlots = new(); - private DrawElementsIndirectCommand[] _deicScratch = Array.Empty(); - - // Diag. - public int LoadedSlots => _alloc.LoadedCount; - public int VisibleSlots => _visibleSlots.Count; - public int CapacitySlots => _alloc.Capacity; - - public TerrainModernRenderer( - GL gl, - BindlessSupport bindless, - Shader shader, - TerrainAtlas atlas, - int initialSlotCapacity = 64) - { - _gl = gl; - _bindless = bindless; - _shader = shader; - _atlas = atlas; - _alloc = new TerrainSlotAllocator(initialSlotCapacity); - _slots = new SlotData?[initialSlotCapacity]; - - _uTerrainHandleLoc = _gl.GetUniformLocation(_shader.Program, "uTerrainHandle"); - _uAlphaHandleLoc = _gl.GetUniformLocation(_shader.Program, "uAlphaHandle"); - - _globalVao = _gl.GenVertexArray(); - _globalVbo = _gl.GenBuffer(); - _globalEbo = _gl.GenBuffer(); - AllocateGpuBuffers(initialSlotCapacity); - ConfigureVao(); - - _indirectBuffer = _gl.GenBuffer(); - } - - /// - /// Two-tier streaming entry point. Accepts a prebuilt mesh from - /// built on the worker - /// thread, together with the world-space origin computed by the caller - /// (render-thread GameWindow derives it from landblockId + liveCenterX/Y). - /// - /// Delegates to - /// so both paths share one upload path. Per Phase A.5 spec T15. - /// - public void AddLandblockWithMesh(uint landblockId, LandblockMeshData meshData, Vector3 worldOrigin) - => AddLandblock(landblockId, meshData, worldOrigin); - - public void AddLandblock(uint landblockId, LandblockMeshData meshData, Vector3 worldOrigin) - { - ArgumentNullException.ThrowIfNull(meshData); - if (meshData.Vertices.Length != VertsPerLandblock) - throw new ArgumentException( - $"Expected {VertsPerLandblock} vertices, got {meshData.Vertices.Length}", - nameof(meshData)); - if (meshData.Indices.Length != IndicesPerLandblock) - throw new ArgumentException( - $"Expected {IndicesPerLandblock} indices, got {meshData.Indices.Length}", - nameof(meshData)); - - if (_idToSlot.ContainsKey(landblockId)) - RemoveLandblock(landblockId); - - int slot = _alloc.Allocate(out var needsGrow); - if (needsGrow) - { - int newCap = Math.Max(_alloc.Capacity * 2, slot + 1); - EnsureCapacity(newCap); - } - - // Bake worldOrigin into vertex positions; capture min/max Z for AABB. - var bakedVerts = new TerrainVertex[VertsPerLandblock]; - float zMin = float.MaxValue, zMax = float.MinValue; - for (int i = 0; i < VertsPerLandblock; i++) - { - var v = meshData.Vertices[i]; - var worldPos = v.Position + worldOrigin; - bakedVerts[i] = new TerrainVertex(worldPos, v.Normal, v.Data0, v.Data1, v.Data2, v.Data3); - if (worldPos.Z < zMin) zMin = worldPos.Z; - if (worldPos.Z > zMax) zMax = worldPos.Z; - } - if (zMin == float.MaxValue) { zMin = 0f; zMax = 0f; } - - // Bake baseVertex into indices on the CPU side (driver-portable pattern). - uint baseVertex = (uint)(slot * VertsPerLandblock); - var bakedIndices = new uint[IndicesPerLandblock]; - for (int i = 0; i < IndicesPerLandblock; i++) - bakedIndices[i] = meshData.Indices[i] + baseVertex; - - // glBufferSubData into the slot's VBO + EBO regions. - nint vboByteOffset = (nint)(slot * VertsPerLandblock * VertexSize); - nint eboByteOffset = (nint)(slot * IndicesPerLandblock * IndexSize); - - _gl.BindBuffer(BufferTargetARB.ArrayBuffer, _globalVbo); - fixed (TerrainVertex* p = bakedVerts) - { - _gl.BufferSubData(BufferTargetARB.ArrayBuffer, vboByteOffset, - (nuint)(VertsPerLandblock * VertexSize), p); - } - _gl.BindBuffer(BufferTargetARB.ArrayBuffer, 0); - - _gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, _globalEbo); - fixed (uint* p = bakedIndices) - { - _gl.BufferSubData(BufferTargetARB.ElementArrayBuffer, eboByteOffset, - (nuint)(IndicesPerLandblock * IndexSize), p); - } - _gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, 0); - - _slots[slot] = new SlotData - { - LandblockId = landblockId, - WorldOrigin = worldOrigin, - FirstIndex = (uint)(slot * IndicesPerLandblock), - IndexCount = IndicesPerLandblock, - AabbMin = new Vector3(worldOrigin.X, worldOrigin.Y, zMin), - AabbMax = new Vector3(worldOrigin.X + LandblockSize, worldOrigin.Y + LandblockSize, zMax), - }; - _idToSlot[landblockId] = slot; - } - - public void RemoveLandblock(uint landblockId) - { - if (!_idToSlot.TryGetValue(landblockId, out var slot)) - return; - _idToSlot.Remove(landblockId); - _slots[slot] = null; - _alloc.Free(slot); - // No GPU clear: the per-frame DEIC array won't reference this slot. - } - - public void Draw(ICamera camera, FrustumPlanes? frustum = null, uint? neverCullLandblockId = null) - { - if (_alloc.LoadedCount == 0) return; - - // Build visible slot list with per-slot frustum cull. - _visibleSlots.Clear(); - for (int slot = 0; slot < _slots.Length; slot++) - { - var data = _slots[slot]; - if (data is null) continue; - if (frustum is not null && data.LandblockId != neverCullLandblockId) - { - if (!FrustumCuller.IsAabbVisible(frustum.Value, data.AabbMin, data.AabbMax)) - continue; - } - _visibleSlots.Add(slot); - } - if (_visibleSlots.Count == 0) return; - - // Build DEIC array. - if (_deicScratch.Length < _visibleSlots.Count) - _deicScratch = new DrawElementsIndirectCommand[Math.Max(_visibleSlots.Count, 64)]; - for (int i = 0; i < _visibleSlots.Count; i++) - { - var data = _slots[_visibleSlots[i]]!; - _deicScratch[i] = new DrawElementsIndirectCommand - { - Count = (uint)data.IndexCount, - InstanceCount = 1u, - FirstIndex = data.FirstIndex, - BaseVertex = 0, // baked into indices on upload - BaseInstance = 0, - }; - } - - // Grow indirect buffer if needed. - if (_visibleSlots.Count > _indirectCapacity) - { - _indirectCapacity = Math.Max(64, _visibleSlots.Count * 2); - _gl.BindBuffer(GLEnum.DrawIndirectBuffer, _indirectBuffer); - _gl.BufferData(GLEnum.DrawIndirectBuffer, - (nuint)(_indirectCapacity * sizeof(DrawElementsIndirectCommand)), - null, GLEnum.DynamicDraw); - } - else - { - _gl.BindBuffer(GLEnum.DrawIndirectBuffer, _indirectBuffer); - } - - // Upload DEIC array. - fixed (DrawElementsIndirectCommand* p = _deicScratch) - { - _gl.BufferSubData(GLEnum.DrawIndirectBuffer, 0, - (nuint)(_visibleSlots.Count * sizeof(DrawElementsIndirectCommand)), p); - } - - // Bind shader + uniforms + atlas handles. - _shader.Use(); - _shader.SetMatrix4("uView", camera.View); - _shader.SetMatrix4("uProjection", camera.Projection); - - var (terrainHandle, alphaHandle) = _atlas.GetBindlessHandles(); - // Pass each 64-bit handle as a uvec2 (low 32 bits, high 32 bits). - // GLSL constructs sampler2DArray(uTerrainHandle) at the use site β€” - // see terrain_modern.frag for why this is the safe pattern. - _gl.ProgramUniform2(_shader.Program, _uTerrainHandleLoc, - (uint)(terrainHandle & 0xFFFFFFFFu), (uint)(terrainHandle >> 32)); - _gl.ProgramUniform2(_shader.Program, _uAlphaHandleLoc, - (uint)(alphaHandle & 0xFFFFFFFFu), (uint)(alphaHandle >> 32)); - - _gl.BindVertexArray(_globalVao); - _gl.MemoryBarrier(MemoryBarrierMask.CommandBarrierBit); - _gl.MultiDrawElementsIndirect( - PrimitiveType.Triangles, DrawElementsType.UnsignedInt, - (void*)0, - (uint)_visibleSlots.Count, - (uint)sizeof(DrawElementsIndirectCommand)); - _gl.BindVertexArray(0); - _gl.BindBuffer(GLEnum.DrawIndirectBuffer, 0); - } - - public void Dispose() - { - _gl.DeleteVertexArray(_globalVao); - _gl.DeleteBuffer(_globalVbo); - _gl.DeleteBuffer(_globalEbo); - _gl.DeleteBuffer(_indirectBuffer); - } - - // ---------------------------------------------------------------- - // Private helpers - // ---------------------------------------------------------------- - - private void AllocateGpuBuffers(int capacitySlots) - { - nuint vboBytes = (nuint)(capacitySlots * VertsPerLandblock * VertexSize); - nuint eboBytes = (nuint)(capacitySlots * IndicesPerLandblock * IndexSize); - - _gl.BindBuffer(BufferTargetARB.ArrayBuffer, _globalVbo); - _gl.BufferData(BufferTargetARB.ArrayBuffer, vboBytes, null, BufferUsageARB.DynamicDraw); - _gl.BindBuffer(BufferTargetARB.ArrayBuffer, 0); - - _gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, _globalEbo); - _gl.BufferData(BufferTargetARB.ElementArrayBuffer, eboBytes, null, BufferUsageARB.DynamicDraw); - _gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, 0); - } - - private void ConfigureVao() - { - _gl.BindVertexArray(_globalVao); - _gl.BindBuffer(BufferTargetARB.ArrayBuffer, _globalVbo); - _gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, _globalEbo); - - uint stride = (uint)VertexSize; - - // location 0: Position - _gl.EnableVertexAttribArray(0); - _gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, stride, (void*)0); - // location 1: Normal - _gl.EnableVertexAttribArray(1); - _gl.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, stride, (void*)(3 * sizeof(float))); - // locations 2-5: Data0..Data3 (uvec4 byte attributes) - nint dataOffset = 6 * sizeof(float); - _gl.EnableVertexAttribArray(2); - _gl.VertexAttribIPointer(2, 4, VertexAttribIType.UnsignedByte, stride, (void*)dataOffset); - _gl.EnableVertexAttribArray(3); - _gl.VertexAttribIPointer(3, 4, VertexAttribIType.UnsignedByte, stride, (void*)(dataOffset + 4)); - _gl.EnableVertexAttribArray(4); - _gl.VertexAttribIPointer(4, 4, VertexAttribIType.UnsignedByte, stride, (void*)(dataOffset + 8)); - _gl.EnableVertexAttribArray(5); - _gl.VertexAttribIPointer(5, 4, VertexAttribIType.UnsignedByte, stride, (void*)(dataOffset + 12)); - - _gl.BindVertexArray(0); - } - - private void EnsureCapacity(int newCapacity) - { - if (newCapacity <= _alloc.Capacity) return; - - // Allocate new VBO + EBO at new size; copy old contents; swap; recreate VAO. - uint newVbo = _gl.GenBuffer(); - uint newEbo = _gl.GenBuffer(); - - nuint newVboBytes = (nuint)(newCapacity * VertsPerLandblock * VertexSize); - nuint newEboBytes = (nuint)(newCapacity * IndicesPerLandblock * IndexSize); - nuint oldVboBytes = (nuint)(_alloc.Capacity * VertsPerLandblock * VertexSize); - nuint oldEboBytes = (nuint)(_alloc.Capacity * IndicesPerLandblock * IndexSize); - - _gl.BindBuffer(BufferTargetARB.ArrayBuffer, newVbo); - _gl.BufferData(BufferTargetARB.ArrayBuffer, newVboBytes, null, BufferUsageARB.DynamicDraw); - _gl.BindBuffer(BufferTargetARB.CopyReadBuffer, _globalVbo); - _gl.BindBuffer(BufferTargetARB.CopyWriteBuffer, newVbo); - _gl.CopyBufferSubData(CopyBufferSubDataTarget.CopyReadBuffer, CopyBufferSubDataTarget.CopyWriteBuffer, - 0, 0, oldVboBytes); - _gl.DeleteBuffer(_globalVbo); - _globalVbo = newVbo; - - _gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, newEbo); - _gl.BufferData(BufferTargetARB.ElementArrayBuffer, newEboBytes, null, BufferUsageARB.DynamicDraw); - _gl.BindBuffer(BufferTargetARB.CopyReadBuffer, _globalEbo); - _gl.BindBuffer(BufferTargetARB.CopyWriteBuffer, newEbo); - _gl.CopyBufferSubData(CopyBufferSubDataTarget.CopyReadBuffer, CopyBufferSubDataTarget.CopyWriteBuffer, - 0, 0, oldEboBytes); - _gl.DeleteBuffer(_globalEbo); - _globalEbo = newEbo; - - // Recreate VAO with new buffer bindings. - _gl.DeleteVertexArray(_globalVao); - _globalVao = _gl.GenVertexArray(); - ConfigureVao(); - - // Grow slot tracking array. - Array.Resize(ref _slots, newCapacity); - _alloc.GrowTo(newCapacity); - } - - private sealed class SlotData - { - public uint LandblockId; - public Vector3 WorldOrigin; - public uint FirstIndex; - public int IndexCount; - public Vector3 AabbMin; - public Vector3 AabbMax; - } -} diff --git a/src/AcDream.App/Rendering/TerrainRenderer.cs b/src/AcDream.App/Rendering/TerrainRenderer.cs new file mode 100644 index 0000000..15bee67 --- /dev/null +++ b/src/AcDream.App/Rendering/TerrainRenderer.cs @@ -0,0 +1,247 @@ +using System.Numerics; +using AcDream.Core.Terrain; +using Silk.NET.OpenGL; + +namespace AcDream.App.Rendering; + +/// +/// Draws the Phase 3c per-cell terrain mesh. All loaded landblocks share a +/// single VBO + EBO + VAO. Vertex positions are baked in world space so no +/// uModel uniform is needed. The VAO is bound once per frame; each visible +/// landblock gets one glDrawElements call into its sub-range of the shared EBO. +/// +/// Attribute layout (see TerrainVertex for the byte layout): +/// location 0: vec3 aPos (3 floats, world space) +/// location 1: vec3 aNormal (3 floats) +/// location 2: uvec4 aPacked0 (4 bytes, Data0) +/// location 3: uvec4 aPacked1 (4 bytes, Data1) +/// location 4: uvec4 aPacked2 (4 bytes, Data2) +/// location 5: uvec4 aPacked3 (4 bytes, Data3) +/// +public sealed unsafe class TerrainRenderer : IDisposable +{ + private readonly GL _gl; + private readonly Shader _shader; + private readonly TerrainAtlas _atlas; + + // Logical per-landblock data (CPU side). + private readonly Dictionary _entries = new(); + + // Shared GPU buffers β€” rebuilt whenever a landblock is added or removed. + private uint _vao; + private uint _vbo; + private uint _ebo; + private bool _gpuDirty = true; // true = buffers need rebuilding before next Draw + + public TerrainRenderer(GL gl, Shader shader, TerrainAtlas atlas) + { + _gl = gl; + _shader = shader; + _atlas = atlas; + + _vao = _gl.GenVertexArray(); + _vbo = _gl.GenBuffer(); + _ebo = _gl.GenBuffer(); + ConfigureVao(); + } + + public void AddLandblock(uint landblockId, LandblockMeshData meshData, Vector3 worldOrigin) + { + if (_entries.ContainsKey(landblockId)) + _entries.Remove(landblockId); + + // Bake world-space positions: offset every vertex by worldOrigin. + var worldVerts = new TerrainVertex[meshData.Vertices.Length]; + float zMin = float.MaxValue, zMax = float.MinValue; + for (int i = 0; i < meshData.Vertices.Length; i++) + { + var v = meshData.Vertices[i]; + var worldPos = v.Position + worldOrigin; + worldVerts[i] = new TerrainVertex(worldPos, v.Normal, v.Data0, v.Data1, v.Data2, v.Data3); + if (worldPos.Z < zMin) zMin = worldPos.Z; + if (worldPos.Z > zMax) zMax = worldPos.Z; + } + if (zMin == float.MaxValue) { zMin = 0f; zMax = 0f; } + + _entries[landblockId] = new LandblockEntry + { + LandblockId = landblockId, + WorldOrigin = worldOrigin, + Vertices = worldVerts, + Indices = meshData.Indices, // local 0..N-1; will be rebased on rebuild + MinZ = zMin, + MaxZ = zMax, + }; + + _gpuDirty = true; + } + + public void RemoveLandblock(uint landblockId) + { + if (_entries.Remove(landblockId)) + _gpuDirty = true; + } + + public void Draw(ICamera camera, FrustumPlanes? frustum = null, uint? neverCullLandblockId = null) + { + if (_entries.Count == 0) + return; + + if (_gpuDirty) + RebuildGpuBuffers(); + + _shader.Use(); + _shader.SetMatrix4("uView", camera.View); + _shader.SetMatrix4("uProjection", camera.Projection); + + // Terrain atlas on unit 0, alpha atlas on unit 1. + _gl.ActiveTexture(TextureUnit.Texture0); + _gl.BindTexture(TextureTarget.Texture2DArray, _atlas.GlTexture); + _gl.ActiveTexture(TextureUnit.Texture1); + _gl.BindTexture(TextureTarget.Texture2DArray, _atlas.GlAlphaTexture); + + int terrainLoc = _gl.GetUniformLocation(_shader.Program, "uTerrain"); + if (terrainLoc >= 0) _gl.Uniform1(terrainLoc, 0); + int alphaLoc = _gl.GetUniformLocation(_shader.Program, "uAlpha"); + if (alphaLoc >= 0) _gl.Uniform1(alphaLoc, 1); + + // Bind the shared VAO once for the entire frame. + _gl.BindVertexArray(_vao); + + foreach (var entry in _entries.Values) + { + // Per-landblock frustum cull using world-space AABB. + if (frustum is not null && entry.LandblockId != neverCullLandblockId) + { + var aabbMin = new Vector3(entry.WorldOrigin.X, entry.WorldOrigin.Y, entry.MinZ); + var aabbMax = new Vector3(entry.WorldOrigin.X + 192f, entry.WorldOrigin.Y + 192f, entry.MaxZ); + if (!FrustumCuller.IsAabbVisible(frustum.Value, aabbMin, aabbMax)) + continue; + } + + // Draw only this landblock's sub-range in the shared EBO. + // EboOffset is in bytes (uint = 4 bytes). + _gl.DrawElements( + PrimitiveType.Triangles, + (uint)entry.IndexCount, + DrawElementsType.UnsignedInt, + (void*)(entry.EboByteOffset)); + } + + _gl.BindVertexArray(0); + } + + public void Dispose() + { + _gl.DeleteVertexArray(_vao); + _gl.DeleteBuffer(_vbo); + _gl.DeleteBuffer(_ebo); + _entries.Clear(); + } + + // ------------------------------------------------------------------------- + // Private helpers + // ------------------------------------------------------------------------- + + private void ConfigureVao() + { + _gl.BindVertexArray(_vao); + _gl.BindBuffer(BufferTargetARB.ArrayBuffer, _vbo); + _gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, _ebo); + + uint stride = (uint)sizeof(TerrainVertex); + + // location 0: Position (12 bytes) + _gl.EnableVertexAttribArray(0); + _gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, stride, (void*)0); + // location 1: Normal (12 bytes, offset 12) + _gl.EnableVertexAttribArray(1); + _gl.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, stride, (void*)(3 * sizeof(float))); + + // location 2..5: Data0..Data3 as uvec4 byte attributes (4 bytes each, + // offsets 24, 28, 32, 36). + nint dataOffset = 6 * sizeof(float); // 24 bytes + _gl.EnableVertexAttribArray(2); + _gl.VertexAttribIPointer(2, 4, VertexAttribIType.UnsignedByte, stride, (void*)dataOffset); + _gl.EnableVertexAttribArray(3); + _gl.VertexAttribIPointer(3, 4, VertexAttribIType.UnsignedByte, stride, (void*)(dataOffset + 4)); + _gl.EnableVertexAttribArray(4); + _gl.VertexAttribIPointer(4, 4, VertexAttribIType.UnsignedByte, stride, (void*)(dataOffset + 8)); + _gl.EnableVertexAttribArray(5); + _gl.VertexAttribIPointer(5, 4, VertexAttribIType.UnsignedByte, stride, (void*)(dataOffset + 12)); + + _gl.BindVertexArray(0); + } + + /// + /// Concatenate all loaded landblocks into a single VBO + EBO and upload. + /// Called on the cold path (landblock load / unload), not per frame. + /// + private void RebuildGpuBuffers() + { + // Measure totals. + int totalVerts = 0; + int totalIndices = 0; + foreach (var e in _entries.Values) + { + totalVerts += e.Vertices.Length; + totalIndices += e.Indices.Length; + } + + var allVerts = new TerrainVertex[totalVerts]; + var allIndices = new uint[totalIndices]; + + int vertBase = 0; + int indexBase = 0; + + foreach (var entry in _entries.Values) + { + // Copy world-space vertices. + entry.Vertices.CopyTo(allVerts, vertBase); + + // Rebase local indices (0..N-1) β†’ absolute (vertBase..vertBase+N-1). + for (int i = 0; i < entry.Indices.Length; i++) + allIndices[indexBase + i] = (uint)(vertBase + entry.Indices[i]); + + // Record where this landblock's indices live in the EBO (byte offset). + entry.EboByteOffset = (nint)(indexBase * sizeof(uint)); + entry.IndexCount = entry.Indices.Length; + + vertBase += entry.Vertices.Length; + indexBase += entry.Indices.Length; + } + + // Upload to GPU. + _gl.BindVertexArray(_vao); + + _gl.BindBuffer(BufferTargetARB.ArrayBuffer, _vbo); + fixed (void* p = allVerts) + _gl.BufferData(BufferTargetARB.ArrayBuffer, + (nuint)(totalVerts * sizeof(TerrainVertex)), p, BufferUsageARB.DynamicDraw); + + _gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, _ebo); + fixed (void* p = allIndices) + _gl.BufferData(BufferTargetARB.ElementArrayBuffer, + (nuint)(totalIndices * sizeof(uint)), p, BufferUsageARB.DynamicDraw); + + _gl.BindVertexArray(0); + _gpuDirty = false; + } + + // ------------------------------------------------------------------------- + // Data types + // ------------------------------------------------------------------------- + + private sealed class LandblockEntry + { + public uint LandblockId; + public Vector3 WorldOrigin; + public TerrainVertex[] Vertices = Array.Empty(); + public uint[] Indices = Array.Empty(); + public float MinZ; + public float MaxZ; + // Set by RebuildGpuBuffers: + public nint EboByteOffset; + public int IndexCount; + } +} diff --git a/src/AcDream.App/Rendering/TextureCache.cs b/src/AcDream.App/Rendering/TextureCache.cs index 5aea075..077a12c 100644 --- a/src/AcDream.App/Rendering/TextureCache.cs +++ b/src/AcDream.App/Rendering/TextureCache.cs @@ -4,12 +4,11 @@ using AcDream.Core.World; using DatReaderWriter; using DatReaderWriter.DBObjs; using Silk.NET.OpenGL; -using System.Linq; using SurfaceType = DatReaderWriter.Enums.SurfaceType; namespace AcDream.App.Rendering; -public sealed unsafe class TextureCache : Wb.ITextureCachePerInstance, IDisposable +public sealed unsafe class TextureCache : IDisposable { private readonly GL _gl; private readonly DatCollection _dats; @@ -30,36 +29,10 @@ public sealed unsafe class TextureCache : Wb.ITextureCachePerInstance, IDisposab private readonly Dictionary<(uint surfaceId, uint origTexOverride, ulong paletteHash), uint> _handlesByPalette = new(); private uint _magentaHandle; - private readonly Wb.BindlessSupport? _bindless; - - // Bindless / Texture2DArray parallel caches. Keys mirror the legacy three - // caches so a surface used by both the legacy (Texture2D, sampler2D) and - // modern (Texture2DArray, sampler2DArray) paths is uploaded twice β€” once - // per target. Each entry stores both the GL texture name (for Dispose - // cleanup) and the resident bindless handle (returned to callers). - private readonly Dictionary _bindlessBySurfaceId = new(); - private readonly Dictionary<(uint surfaceId, uint origTexOverride), (uint Name, ulong Handle)> _bindlessByOverridden = new(); - private readonly Dictionary<(uint surfaceId, uint origTexOverride, ulong paletteHash), (uint Name, ulong Handle)> _bindlessByPalette = new(); - - // Phase N.6 slice 1 (2026-05-11): per-upload metadata for the - // ACDREAM_DUMP_SURFACES=1 histogram dump path. Populated at upload - // time so the dump method doesn't have to query GL state. Keyed by - // GL texture name (same key used in cache value tuples). Format - // label is "RGBA8_DECODED" for the post-decode upload (all uploads - // currently land as RGBA8 regardless of source format). - private readonly Dictionary _uploadMetadata = new(); - - // Frame counter for the one-shot ACDREAM_DUMP_SURFACES=1 trigger. - // Increments per Tick call; fires the dump once at frame index 600 - // and never again for the session. See spec Β§5. - private int _dumpFrameCounter; - private bool _surfaceHistogramAlreadyDumped; - - public TextureCache(GL gl, DatCollection dats, Wb.BindlessSupport? bindless = null) + public TextureCache(GL gl, DatCollection dats) { _gl = gl; _dats = dats; - _bindless = bindless; } /// @@ -150,23 +123,10 @@ public sealed unsafe class TextureCache : Wb.ITextureCachePerInstance, IDisposab uint surfaceId, uint? overrideOrigTextureId, PaletteOverride paletteOverride) - => GetOrUploadWithPaletteOverride(surfaceId, overrideOrigTextureId, paletteOverride, - HashPaletteOverride(paletteOverride)); - - /// - /// Overload that accepts a precomputed palette hash. Lets callers (e.g. - /// the WB draw dispatcher) compute the hash ONCE per entity and reuse - /// it across every (part, batch) lookup, avoiding the per-batch - /// FNV-1a fold over . - /// - public uint GetOrUploadWithPaletteOverride( - uint surfaceId, - uint? overrideOrigTextureId, - PaletteOverride paletteOverride, - ulong precomputedPaletteHash) { + ulong hash = HashPaletteOverride(paletteOverride); uint origTexKey = overrideOrigTextureId ?? 0; - var key = (surfaceId, origTexKey, precomputedPaletteHash); + var key = (surfaceId, origTexKey, hash); if (_handlesByPalette.TryGetValue(key, out var h)) return h; @@ -176,88 +136,11 @@ public sealed unsafe class TextureCache : Wb.ITextureCachePerInstance, IDisposab return h; } - /// - /// 64-bit bindless handle variant of for the WB - /// modern rendering path. Uploads the texture as a 1-layer Texture2DArray - /// (so the shader's sampler2DArray can sample at layer 0) and returns - /// a resident bindless handle. Caches by surfaceId in a separate dictionary - /// from the legacy Texture2D path; the same surface may be uploaded twice - /// if used by both paths (acceptable transition cost β€” N.6 deletes the legacy - /// path). - /// Throws if BindlessSupport wasn't provided to the constructor. - /// - public ulong GetOrUploadBindless(uint surfaceId) - { - EnsureBindlessAvailable(); - if (_bindlessBySurfaceId.TryGetValue(surfaceId, out var entry)) - return entry.Handle; - var decoded = DecodeFromDats(surfaceId, origTextureOverride: null, paletteOverride: null); - uint name = UploadRgba8AsLayer1Array(decoded); - ulong handle = _bindless!.GetResidentHandle(name); - _bindlessBySurfaceId[surfaceId] = (name, handle); - return handle; - } - - /// - /// 64-bit bindless handle variant of - /// for the WB modern rendering path. Uploads the texture as a 1-layer - /// Texture2DArray with the override SurfaceTexture id and returns a resident - /// bindless handle. Caches under a separate composite key from the legacy - /// path. Throws if BindlessSupport wasn't provided to the constructor. - /// - public ulong GetOrUploadWithOrigTextureOverrideBindless(uint surfaceId, uint overrideOrigTextureId) - { - EnsureBindlessAvailable(); - var key = (surfaceId, overrideOrigTextureId); - if (_bindlessByOverridden.TryGetValue(key, out var entry)) - return entry.Handle; - var decoded = DecodeFromDats(surfaceId, origTextureOverride: overrideOrigTextureId, paletteOverride: null); - uint name = UploadRgba8AsLayer1Array(decoded); - ulong handle = _bindless!.GetResidentHandle(name); - _bindlessByOverridden[key] = (name, handle); - return handle; - } - - /// - /// 64-bit bindless handle variant of - /// for the WB modern rendering path. Applies the palette override on top of - /// the texture's default palette before decoding, uploads as a 1-layer - /// Texture2DArray, and returns a resident bindless handle. Takes a - /// precomputed palette hash so the WB dispatcher can compute it once per - /// entity. Throws if BindlessSupport wasn't provided to the constructor. - /// - public ulong GetOrUploadWithPaletteOverrideBindless( - uint surfaceId, - uint? overrideOrigTextureId, - PaletteOverride paletteOverride, - ulong precomputedPaletteHash) - { - EnsureBindlessAvailable(); - uint origTexKey = overrideOrigTextureId ?? 0; - var key = (surfaceId, origTexKey, precomputedPaletteHash); - if (_bindlessByPalette.TryGetValue(key, out var entry)) - return entry.Handle; - var decoded = DecodeFromDats(surfaceId, origTextureOverride: overrideOrigTextureId, paletteOverride: paletteOverride); - uint name = UploadRgba8AsLayer1Array(decoded); - ulong handle = _bindless!.GetResidentHandle(name); - _bindlessByPalette[key] = (name, handle); - return handle; - } - - private void EnsureBindlessAvailable() - { - if (_bindless is null) - throw new InvalidOperationException( - "TextureCache constructed without BindlessSupport β€” cannot generate bindless handles. " + - "WbDrawDispatcher requires the bindless-aware ctor overload (pass non-null BindlessSupport)."); - } - /// /// Cheap 64-bit hash over a palette override's identity so two - /// entities with the same palette setup share a decode. Internal so - /// the WB dispatcher can compute it once per entity. + /// entities with the same palette setup share a decode. /// - internal static ulong HashPaletteOverride(PaletteOverride p) + private static ulong HashPaletteOverride(PaletteOverride p) { // Not cryptographic β€” just needs to distinguish override setups // for caching. Start with base palette id, fold in each entry. @@ -273,114 +156,6 @@ public sealed unsafe class TextureCache : Wb.ITextureCachePerInstance, IDisposab return h; } - /// - /// Phase N.6 slice 1: one-shot surface-format histogram dump for the - /// atlas-opportunity audit. Activated by ACDREAM_DUMP_SURFACES=1; fires - /// once after BOTH gates pass: - /// 1. _dumpFrameCounter >= 600 β€” at least 600 OnRender ticks - /// have elapsed (catches the "we're already past startup boilerplate" - /// bound; ~10s at 60fps, ~3s at 200fps). - /// 2. _uploadMetadata.Count >= 100 β€” the cache contains at - /// least 100 uploaded textures, indicating streaming has actually - /// pulled in world content (not just sky/UI/font). The original - /// frame-only gate fired during the login/handshake phase where - /// OnRender ticks at GUI rates but no world has streamed in. - /// Output goes to %LOCALAPPDATA%\acdream\n6-surfaces.txt. Zero cost - /// when off. See spec Β§5 in - /// docs/superpowers/specs/2026-05-11-phase-n6-slice1-design.md. - /// - public void TickSurfaceHistogramDumpIfEnabled() - { - if (_surfaceHistogramAlreadyDumped) return; - if (!string.Equals(System.Environment.GetEnvironmentVariable("ACDREAM_DUMP_SURFACES"), "1", StringComparison.Ordinal)) return; - _dumpFrameCounter++; - if (_dumpFrameCounter < 600) return; - if (_uploadMetadata.Count < 100) return; - - DumpSurfaceHistogram(); - _surfaceHistogramAlreadyDumped = true; - } - - private void DumpSurfaceHistogram() - { - try - { - DumpSurfaceHistogramCore(); - } - catch (Exception ex) - { - // Diagnostic-only path. If the dump file can't be written - // (disk full, permission denied, antivirus lock, path too - // long) we must NOT crash OnRender β€” that would invalidate - // the very measurement pass this diagnostic is meant to - // support. Log to stderr and let the caller mark the dump - // as "already done" so it doesn't retry every frame. - Console.Error.WriteLine($"[N6-DUMP] Failed to write surface histogram: {ex.Message}"); - } - } - - private void DumpSurfaceHistogramCore() - { - var localAppData = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData); - var outDir = System.IO.Path.Combine(localAppData, "acdream"); - System.IO.Directory.CreateDirectory(outDir); - var outPath = System.IO.Path.Combine(outDir, "n6-surfaces.txt"); - - var sb = new System.Text.StringBuilder(); - sb.AppendLine($"# acdream surface-format histogram β€” generated {DateTime.UtcNow:yyyy-MM-ddTHH:mm:ssZ}"); - sb.AppendLine("# Per-entry: surfaceId(hex), width, height, format, byteCount"); - sb.AppendLine(); - - // Walk every cached entry across the 6 caches, dedupe by GL name. - var seen = new HashSet(); - long totalBytes = 0; - var bucketsByDim = new Dictionary<(int W, int H), int>(); - var bucketsByFormat = new Dictionary(); - var bucketsByTriple = new Dictionary<(int W, int H, string F), int>(); - - void Emit(uint surfaceId, uint name) - { - if (!seen.Add(name)) return; - if (!_uploadMetadata.TryGetValue(name, out var meta)) return; - int bytes = meta.Width * meta.Height * 4; - totalBytes += bytes; - sb.AppendLine($"0x{surfaceId:X8}, {meta.Width}, {meta.Height}, {meta.Format}, {bytes}"); - - var dimKey = (meta.Width, meta.Height); - bucketsByDim[dimKey] = bucketsByDim.GetValueOrDefault(dimKey) + 1; - bucketsByFormat[meta.Format] = bucketsByFormat.GetValueOrDefault(meta.Format) + 1; - var tripleKey = (meta.Width, meta.Height, meta.Format); - bucketsByTriple[tripleKey] = bucketsByTriple.GetValueOrDefault(tripleKey) + 1; - } - - foreach (var kv in _handlesBySurfaceId) Emit(kv.Key, kv.Value); - foreach (var kv in _handlesByOverridden) Emit(kv.Key.surfaceId, kv.Value); - foreach (var kv in _handlesByPalette) Emit(kv.Key.surfaceId, kv.Value); - foreach (var kv in _bindlessBySurfaceId) Emit(kv.Key, kv.Value.Name); - foreach (var kv in _bindlessByOverridden) Emit(kv.Key.surfaceId, kv.Value.Name); - foreach (var kv in _bindlessByPalette) Emit(kv.Key.surfaceId, kv.Value.Name); - - sb.AppendLine(); - sb.AppendLine("# Rollups"); - sb.AppendLine($"# Total unique GL textures: {seen.Count}"); - sb.AppendLine($"# Total bytes (sum of W*H*4): {totalBytes}"); - - sb.AppendLine("# Top 10 (W,H) dimension buckets:"); - foreach (var kv in bucketsByDim.OrderByDescending(kv => kv.Value).Take(10)) - sb.AppendLine($"# {kv.Key.W}x{kv.Key.H}: {kv.Value}"); - - sb.AppendLine("# Format buckets:"); - foreach (var kv in bucketsByFormat.OrderByDescending(kv => kv.Value)) - sb.AppendLine($"# {kv.Key}: {kv.Value}"); - - sb.AppendLine("# Top 10 (W,H,format) triples β€” atlas-opportunity input:"); - foreach (var kv in bucketsByTriple.OrderByDescending(kv => kv.Value).Take(10)) - sb.AppendLine($"# {kv.Key.W}x{kv.Key.H} {kv.Key.F}: {kv.Value}"); - - System.IO.File.WriteAllText(outPath, sb.ToString()); - Console.WriteLine($"[N6-DUMP] Surface histogram written to {outPath} ({seen.Count} textures, {totalBytes} bytes)"); - } - private DecodedTexture DecodeFromDats(uint surfaceId, uint? origTextureOverride, PaletteOverride? paletteOverride) { var surface = _dats.Get(surfaceId); @@ -424,9 +199,8 @@ public sealed unsafe class TextureCache : Wb.ITextureCachePerInstance, IDisposab // Clipmap surfaces use palette indices 0..7 as transparent sentinels. bool isClipMap = surface.Type.HasFlag(SurfaceType.Base1ClipMap); - bool isAdditive = surface.Type.HasFlag(SurfaceType.Additive); - return SurfaceDecoder.DecodeRenderSurface(rs, effectivePalette, isClipMap, isAdditive); + return SurfaceDecoder.DecodeRenderSurface(rs, effectivePalette, isClipMap); } /// @@ -487,84 +261,20 @@ public sealed unsafe class TextureCache : Wb.ITextureCachePerInstance, IDisposab _gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat); _gl.BindTexture(TextureTarget.Texture2D, 0); - _uploadMetadata[tex] = (decoded.Width, decoded.Height, "RGBA8_DECODED"); - return tex; - } - - /// - /// Variant of that uploads pixel data as a 1-layer - /// Texture2DArray. Required by the WB modern rendering path which samples via - /// sampler2DArray in its bindless shader. Pixel data is identical. - /// - private uint UploadRgba8AsLayer1Array(DecodedTexture decoded) - { - uint tex = _gl.GenTexture(); - _gl.BindTexture(TextureTarget.Texture2DArray, tex); - - fixed (byte* p = decoded.Rgba8) - _gl.TexImage3D( - TextureTarget.Texture2DArray, - 0, - InternalFormat.Rgba8, - (uint)decoded.Width, - (uint)decoded.Height, - depth: 1, - border: 0, - PixelFormat.Rgba, - PixelType.UnsignedByte, - p); - - _gl.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); - _gl.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); - _gl.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat); - _gl.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat); - - _gl.BindTexture(TextureTarget.Texture2DArray, 0); - _uploadMetadata[tex] = (decoded.Width, decoded.Height, "RGBA8_DECODED"); return tex; } public void Dispose() { - // Phase 1: make all bindless handles non-resident BEFORE any - // DeleteTexture call. ARB_bindless_texture requires that resident - // handles be released before their backing texture is deleted β€” - // interleaving per-entry is UB. Single null-guard around the whole - // block (cleaner than per-call null-conditionals). - if (_bindless is not null) - { - foreach (var (_, handle) in _bindlessBySurfaceId.Values) - _bindless.MakeNonResident(handle); - foreach (var (_, handle) in _bindlessByOverridden.Values) - _bindless.MakeNonResident(handle); - foreach (var (_, handle) in _bindlessByPalette.Values) - _bindless.MakeNonResident(handle); - } - - // Phase 2: delete the Texture2DArray textures backing those handles. - foreach (var (name, _) in _bindlessBySurfaceId.Values) - _gl.DeleteTexture(name); - _bindlessBySurfaceId.Clear(); - foreach (var (name, _) in _bindlessByOverridden.Values) - _gl.DeleteTexture(name); - _bindlessByOverridden.Clear(); - foreach (var (name, _) in _bindlessByPalette.Values) - _gl.DeleteTexture(name); - _bindlessByPalette.Clear(); - - // Phase 3: legacy Texture2D textures. foreach (var h in _handlesBySurfaceId.Values) _gl.DeleteTexture(h); _handlesBySurfaceId.Clear(); - foreach (var h in _handlesByOverridden.Values) _gl.DeleteTexture(h); _handlesByOverridden.Clear(); - foreach (var h in _handlesByPalette.Values) _gl.DeleteTexture(h); _handlesByPalette.Clear(); - if (_magentaHandle != 0) { _gl.DeleteTexture(_magentaHandle); diff --git a/src/AcDream.App/Rendering/Vfx/EntityScriptActivator.cs b/src/AcDream.App/Rendering/Vfx/EntityScriptActivator.cs deleted file mode 100644 index a8b0d2b..0000000 --- a/src/AcDream.App/Rendering/Vfx/EntityScriptActivator.cs +++ /dev/null @@ -1,133 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Numerics; -using AcDream.Core.Vfx; -using AcDream.Core.World; - -namespace AcDream.App.Rendering.Vfx; - -/// -/// What the activator's resolver returns when an entity's Setup carries -/// a DefaultScript. Bundles the script id with the per-part -/// transforms baked from Setup.PlacementFrames so a single dat -/// lookup yields both pieces of state. The activator pushes the part -/// transforms into -/// before calling , which closes -/// the part-anchor pipeline introduced for issue #56. -/// -public sealed record ScriptActivationInfo( - uint ScriptId, - IReadOnlyList PartTransforms); - -/// -/// Fires Setup.DefaultScript through -/// when a enters the world, so static objects -/// (portals, chimneys, fireplaces, EnvCell decorations, building details) -/// emit their retail-faithful persistent particle effects automatically. -/// Stops the scripts and live emitters when the entity despawns. -/// -/// -/// Handles both server-spawned entities (ServerGuid != 0, keyed by -/// ServerGuid) and dat-hydrated entities (ServerGuid == 0, keyed by -/// entity.Id). The C.1.5a guard that early-returned for -/// ServerGuid == 0 was relaxed in C.1.5b so EnvCell static objects -/// (which have no server guid because they come from the dat file, not -/// the network) also fire their DefaultScript. -/// -/// -/// -/// Wires alongside EntitySpawnAdapter in GpuWorldState: the -/// adapter handles meshes + animation state, the activator handles scripts -/// + particles. Both are render-thread-only. The activator is invoked from -/// four GpuWorldState fire-sites (AppendLiveEntity, AddLandblock, -/// AddEntitiesToExistingLandblock, plus the matching remove paths). -/// -/// -/// -/// Retail oracle: play_script_internal(setup.DefaultScript) is what -/// retail's CPhysicsObj invokes at object load (see Phase C.1 plan -/// Β§C.1 and memory/project_sky_pes_port.md). C.1 already shipped the -/// runner; this class adds the missing fire-on-spawn call site. -/// -/// -public sealed class EntityScriptActivator -{ - private readonly PhysicsScriptRunner _scriptRunner; - private readonly ParticleHookSink _particleSink; - private readonly Func _resolver; - - /// Already-shipped runner from C.1. Owns the - /// (scriptId, entityId) instance table and schedules hooks at their - /// StartTime offsets. - /// Already-shipped hook sink from C.1. The - /// activator pushes per-entity rotation + part transforms here, and - /// calls to drop - /// per-entity emitter handles on despawn. - /// Returns - /// with the entity's - /// Setup.DefaultScript.DataId and per-part transforms (via - /// SetupPartTransforms.Compute), or null on dat miss / - /// throw / missing DefaultScript. Production lambda hits - /// DatCollection; tests pass a hand-rolled stub. - public EntityScriptActivator( - PhysicsScriptRunner scriptRunner, - ParticleHookSink particleSink, - Func resolver) - { - ArgumentNullException.ThrowIfNull(scriptRunner); - ArgumentNullException.ThrowIfNull(particleSink); - ArgumentNullException.ThrowIfNull(resolver); - _scriptRunner = scriptRunner; - _particleSink = particleSink; - _resolver = resolver; - } - - /// - /// Resolve the entity's Setup.DefaultScript and fire it through - /// the script runner. Keys by entity.ServerGuid when non-zero, - /// otherwise by entity.Id (the latter handles dat-hydrated - /// EnvCell statics + exterior stabs whose entity.Id lives in - /// the 0x40xxxxxx range β€” collision-free with server guids). - /// No-op if the entity has no DefaultScript (resolver returns null - /// or zero-script). - /// - public void OnCreate(WorldEntity entity) - { - ArgumentNullException.ThrowIfNull(entity); - uint key = entity.ServerGuid != 0 ? entity.ServerGuid : entity.Id; - if (key == 0) return; // malformed entity - - var info = _resolver(entity); - if (info is null || info.ScriptId == 0) return; - - // Seed the sink's per-entity rotation so CreateParticleHook.Offset.Origin - // (in entity-local frame) transforms correctly to world space when the - // hook fires. C.1.5a fix: without this, the sink falls through to - // Quaternion.Identity and the offset gets applied in world axes β€” - // visual symptom for portals: swirl oriented along world XYZ instead - // of the portal's facing, partially buried. - _particleSink.SetEntityRotation(key, entity.Rotation); - - // C.1.5b #56: seed the sink's per-entity part transforms so - // CreateParticleHook.PartIndex routes the hook offset through the - // right mesh part's resting transform. Without this, every emitter - // in a multi-part Setup collapses to the entity root. - _particleSink.SetEntityPartTransforms(key, info.PartTransforms); - - _scriptRunner.Play(info.ScriptId, key, entity.Position); - } - - /// - /// Stop every script instance the runner is tracking for this key, and - /// kill every live emitter the sink has attributed to it. Caller picks - /// the key (the matching ServerGuid for live entities, or - /// entity.Id for dat-hydrated entities β€” mirror whatever was - /// used at ). Idempotent for unknown keys. - /// - public void OnRemove(uint key) - { - if (key == 0) return; - _scriptRunner.StopAllForEntity(key); - _particleSink.StopAllForEntity(key, fadeOut: false); - } -} diff --git a/src/AcDream.App/Rendering/Wb/AcSurfaceMetadata.cs b/src/AcDream.App/Rendering/Wb/AcSurfaceMetadata.cs deleted file mode 100644 index 4e6e325..0000000 --- a/src/AcDream.App/Rendering/Wb/AcSurfaceMetadata.cs +++ /dev/null @@ -1,21 +0,0 @@ -using AcDream.Core.Meshing; - -namespace AcDream.App.Rendering.Wb; - -/// -/// AC-specific surface render metadata that WB's MeshBatchData -/// doesn't carry. Computed at mesh-extraction time and looked up by the -/// draw dispatcher to drive translucency / sky-pass / fog behavior. -/// -/// -/// All fields mirror those on today's so -/// behavior is preserved bit-for-bit through the migration. -/// -/// -public sealed record AcSurfaceMetadata( - TranslucencyKind Translucency, - float Luminosity, - float Diffuse, - float SurfOpacity, - bool NeedsUvRepeat, - bool DisableFog); diff --git a/src/AcDream.App/Rendering/Wb/AcSurfaceMetadataTable.cs b/src/AcDream.App/Rendering/Wb/AcSurfaceMetadataTable.cs deleted file mode 100644 index 20b9278..0000000 --- a/src/AcDream.App/Rendering/Wb/AcSurfaceMetadataTable.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System.Collections.Concurrent; - -namespace AcDream.App.Rendering.Wb; - -/// -/// Thread-safe side-table mapping (gfxObjId, surfaceIdx) to -/// . Populated when a GfxObj's mesh data -/// is extracted; queried at draw time. -/// -/// -/// Keyed by (gfxObjId, surfaceIdx) not by WB's runtime batch -/// identity because batch objects can be evicted and re-loaded by WB's -/// LRU; the (gfxObj, surface) pair is stable across cycles. -/// -/// -public sealed class AcSurfaceMetadataTable -{ - private readonly ConcurrentDictionary<(ulong gfxObjId, int surfaceIdx), AcSurfaceMetadata> _table = new(); - - public void Add(ulong gfxObjId, int surfaceIdx, AcSurfaceMetadata meta) - => _table[(gfxObjId, surfaceIdx)] = meta; - - public bool TryLookup(ulong gfxObjId, int surfaceIdx, out AcSurfaceMetadata meta) - => _table.TryGetValue((gfxObjId, surfaceIdx), out meta!); - - public void Clear() => _table.Clear(); -} diff --git a/src/AcDream.App/Rendering/Wb/AnimatedEntityState.cs b/src/AcDream.App/Rendering/Wb/AnimatedEntityState.cs deleted file mode 100644 index 913b7bf..0000000 --- a/src/AcDream.App/Rendering/Wb/AnimatedEntityState.cs +++ /dev/null @@ -1,67 +0,0 @@ -using System.Collections.Generic; -using AcDream.Core.Physics; - -namespace AcDream.App.Rendering.Wb; - -/// -/// Per-entity render state for animated entities (characters, creatures, -/// equipped items). Holds AC-specific per-instance customizations the WB -/// atlas cache doesn't carry: AnimPartChange override map + -/// HiddenParts bitmask. Also holds a reference to acdream's existing -/// β€” Phase N.4 explicitly does not touch -/// the sequencer; we just route through it at draw time. -/// -/// -/// Lifecycle: created by EntitySpawnAdapter.OnCreate (Task 17) when -/// a server CreateObject is processed; destroyed by -/// EntitySpawnAdapter.OnRemove on RemoveObject. The mesh -/// data backing each part is cached in WB's ObjectMeshManager; -/// per-instance customizations don't go through the atlas β€” they overlay -/// at draw time. -/// -/// -public sealed class AnimatedEntityState -{ - private readonly Dictionary _partGfxObjOverrides = new(); - private ulong _hiddenMask = 0; - - /// Reference to acdream's existing animation sequencer. - /// Phase N.4 doesn't touch the sequencer; the draw dispatcher consumes - /// per-part transforms it produces per frame. - public AnimationSequencer Sequencer { get; } - - public AnimatedEntityState(AnimationSequencer sequencer) - { - System.ArgumentNullException.ThrowIfNull(sequencer); - Sequencer = sequencer; - } - - /// Set the HiddenParts bitmask for this entity. Bit - /// i set hides part i at draw time. - public void HideParts(ulong hiddenMask) => _hiddenMask = hiddenMask; - - /// True if part partIdx should be skipped at draw - /// time. Returns false for part indices outside [0, 63]. - public bool IsPartHidden(int partIdx) - { - if (partIdx < 0 || partIdx >= 64) return false; - return (_hiddenMask & (1ul << partIdx)) != 0; - } - - /// Override the GfxObj id for a Setup part. Used for - /// AnimPartChange β€” e.g. wielding a weapon swaps the hand-part's - /// GfxObj. - public void SetPartOverride(int partIdx, ulong gfxObjId) - => _partGfxObjOverrides[partIdx] = gfxObjId; - - /// Look up the GfxObj override for a part. Returns false if - /// no override is set (caller should fall back to Setup default). - public bool TryGetPartOverride(int partIdx, out ulong gfxObjId) - => _partGfxObjOverrides.TryGetValue(partIdx, out gfxObjId); - - /// Resolve the GfxObj id for : - /// override if set, else . Used by the - /// draw dispatcher to pick the right cached mesh data per part. - public ulong ResolvePartGfxObj(int partIdx, ulong setupDefault) - => TryGetPartOverride(partIdx, out var ov) ? ov : setupDefault; -} diff --git a/src/AcDream.App/Rendering/Wb/BindlessSupport.cs b/src/AcDream.App/Rendering/Wb/BindlessSupport.cs deleted file mode 100644 index 64dda3c..0000000 --- a/src/AcDream.App/Rendering/Wb/BindlessSupport.cs +++ /dev/null @@ -1,64 +0,0 @@ -using Silk.NET.OpenGL; -using Silk.NET.OpenGL.Extensions.ARB; - -namespace AcDream.App.Rendering.Wb; - -/// -/// Thin wrapper around + capability detection -/// for the modern rendering path. Constructed once at startup via -/// , which returns false if the extension isn't present. -/// -public sealed class BindlessSupport -{ - private readonly ArbBindlessTexture _ext; - - private BindlessSupport(ArbBindlessTexture extension) - { - _ext = extension; - } - - public static bool TryCreate(GL gl, out BindlessSupport? support) - { - if (gl.TryGetExtension(out var ext)) - { - support = new BindlessSupport(ext); - return true; - } - support = null; - return false; - } - - /// Get a 64-bit bindless handle for the texture and make it resident. - /// Idempotent: handle is the same for a given texture name. - public ulong GetResidentHandle(uint textureName) - { - ulong h = _ext.GetTextureHandle(textureName); - if (!_ext.IsTextureHandleResident(h)) - _ext.MakeTextureHandleResident(h); - return h; - } - - /// Release residency for a handle. Call before deleting the underlying texture. - public void MakeNonResident(ulong handle) - { - if (_ext.IsTextureHandleResident(handle)) - _ext.MakeTextureHandleNonResident(handle); - } - - // Phase N.5b note: a `SetSamplerHandleUniform` wrapper was added in T6 - // and removed when terrain rendering surfaced GL_INVALID_OPERATION on - // NVIDIA Windows for the `uniform sampler2DArray` + glProgramUniformHandleARB - // combination. The replacement pattern (uvec2 handle uniform + GLSL - // sampler-from-handle constructor β€” see terrain_modern.frag) lives at the - // call site via plain `_gl.ProgramUniform2(program, loc, low, high)`. If - // you re-introduce a sampler-handle helper, restrict it to drivers known - // to accept the direct sampler-uniform path. - - /// Detect GL_ARB_shader_draw_parameters in addition to bindless. - /// N.5's vertex shader uses gl_BaseInstanceARB and gl_DrawIDARB - /// from this extension. - public bool HasShaderDrawParameters(GL gl) - { - return gl.IsExtensionPresent("GL_ARB_shader_draw_parameters"); - } -} diff --git a/src/AcDream.App/Rendering/Wb/CachedBatch.cs b/src/AcDream.App/Rendering/Wb/CachedBatch.cs deleted file mode 100644 index d1bccb7..0000000 --- a/src/AcDream.App/Rendering/Wb/CachedBatch.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Numerics; - -namespace AcDream.App.Rendering.Wb; - -/// -/// Per-(entity, partIdx, batchIdx) classification result, stored flat inside -/// . For Setup multi-part MeshRefs each -/// subPart contributes its own entries, with -/// already containing the -/// subPart.PartTransform * meshRef.PartTransform product. -/// -/// Accessibility: internal because is -/// internal and shows up in this struct's constructor / Deconstruct -/// signature. The cache itself is dispatcher-internal coordination state; -/// on AcDream.App exposes the type to -/// AcDream.Core.Tests. -/// -internal readonly record struct CachedBatch( - GroupKey Key, - ulong BindlessTextureHandle, - Matrix4x4 RestPose); - -/// -/// One entity's cached classification. is flat across -/// (partIdx, batchIdx) and ordered as WbDrawDispatcher.ClassifyBatches -/// produced them. lets -/// sweep entries -/// efficiently when a landblock demotes or unloads. -/// -/// Accessibility: internal for the same reason as -/// β€” its property is CachedBatch[], which -/// transitively involves . -/// -internal sealed class EntityCacheEntry -{ - public required uint EntityId { get; init; } - public required uint LandblockHint { get; init; } - public required CachedBatch[] Batches { get; init; } -} diff --git a/src/AcDream.App/Rendering/Wb/DrawElementsIndirectCommand.cs b/src/AcDream.App/Rendering/Wb/DrawElementsIndirectCommand.cs deleted file mode 100644 index 80d1119..0000000 --- a/src/AcDream.App/Rendering/Wb/DrawElementsIndirectCommand.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System.Runtime.InteropServices; - -namespace AcDream.App.Rendering.Wb; - -/// -/// Layout matches what glMultiDrawElementsIndirect expects. -/// Total size 20 bytes; arrays are typically uploaded with stride = sizeof(this). -/// -[StructLayout(LayoutKind.Sequential, Pack = 4)] -public struct DrawElementsIndirectCommand -{ - public uint Count; // index count for this draw - public uint InstanceCount; // number of instances - public uint FirstIndex; // offset into IBO, in indices - public int BaseVertex; // vertex offset into VBO - public uint BaseInstance; // first instance ID (offsets per-instance attribs / SSBO read) -} diff --git a/src/AcDream.App/Rendering/Wb/EntityClassificationCache.cs b/src/AcDream.App/Rendering/Wb/EntityClassificationCache.cs deleted file mode 100644 index b0e248e..0000000 --- a/src/AcDream.App/Rendering/Wb/EntityClassificationCache.cs +++ /dev/null @@ -1,193 +0,0 @@ -using System.Collections.Generic; - -namespace AcDream.App.Rendering.Wb; - -/// -/// Cache of per-entity classification results for static entities (those NOT -/// in GameWindow._animatedEntities). Holds one -/// per cached entity. The cache is opaque -/// w.r.t. classification logic β€” it simply stores what callers populate. -/// -/// -/// Key composition: entries are keyed by the tuple -/// (EntityId, LandblockHint), NOT by EntityId alone. Issue #53 -/// uncovered that entity.Id is NOT globally unique across all -/// static-entity hydration paths: scenery (0x80LLBB00 + localIndex) -/// and interior cells (0x40LLBB00 + localCounter) overflow at >256 -/// items per landblock, wrapping into the lbY byte and producing -/// cross-LB collisions in dense forest/urban LBs outside Holtburg. Keying -/// by the tuple is correct-by-construction regardless of any hydration -/// path's id strategy. -/// -/// -/// -/// Invariants: -/// -/// overwrites any existing entry for the same (id, lb) tuple (defensive). -/// sweeps all entries with the given EntityId -/// regardless of LandblockHint; idempotent (no-throw on missing id). -/// walks all entries; entries whose -/// equals the argument are removed. -/// All operations are render-thread only. No internal locking. -/// -/// -/// -/// -/// Audit foundation: see -/// docs/research/2026-05-10-tier1-mutation-audit.md for why static -/// entities can be cached and what invalidation is needed. -/// -/// -/// -/// Accessibility: internal. and -/// both transitively reference the internal -/// ; surfacing the cache as public would create -/// inconsistent-accessibility errors. Cross-assembly access for the test -/// project comes via InternalsVisibleTo("AcDream.Core.Tests") on -/// AcDream.App.csproj. -/// -/// -internal sealed class EntityClassificationCache -{ - private readonly Dictionary<(uint EntityId, uint LandblockHint), EntityCacheEntry> _entries = new(); - - /// Number of cached entities β€” for diagnostics. - public int Count => _entries.Count; - - /// - /// Look up an entity's cached classification. Keyed by both - /// AND to - /// disambiguate entities whose Ids collide across landblocks (e.g., - /// scenery's 0x80LLBB00 + localIndex overflow at >256 items/LB). - /// Returns true with the entry on hit; false with - /// set to null on miss. - /// - public bool TryGet(uint entityId, uint landblockHint, out EntityCacheEntry? entry) - => _entries.TryGetValue((entityId, landblockHint), out entry); - - /// - /// Insert or overwrite a cache entry for the - /// (, ) - /// tuple. Defensive: if an entry already exists, replaces it. - /// - public void Populate(uint entityId, uint landblockHint, CachedBatch[] batches) - { - _entries[(entityId, landblockHint)] = new EntityCacheEntry - { - EntityId = entityId, - LandblockHint = landblockHint, - Batches = batches, - }; - } - - /// - /// Remove all cache entries for the given , - /// regardless of which landblock they were populated under. Sweep is - /// needed because we may have entries for the same Id under different - /// LandblockHints if any hydration path produced colliding Ids - /// historically (defensive even though current paths shouldn't produce - /// duplicates per-LB). Was O(1) before the #53 tuple-key change; - /// now O(n), but called rarely (only on entity despawn). - /// - public void InvalidateEntity(uint entityId) - { - if (_entries.Count == 0) return; - List<(uint, uint)>? toRemove = null; - foreach (var key in _entries.Keys) - { - if (key.EntityId == entityId) - { - toRemove ??= new List<(uint, uint)>(); - toRemove.Add(key); - } - } - if (toRemove is null) return; - foreach (var k in toRemove) _entries.Remove(k); - } - - /// - /// Remove every cache entry whose - /// equals . Used by the streaming pipeline - /// when a landblock demotes from near to far or unloads. No-op if no - /// entries match. - /// - public void InvalidateLandblock(uint landblockId) - { - if (_entries.Count == 0) return; - - // Collect the keys to remove first to avoid mutating the dict during iteration. - // Buffered locally because the typical case removes ~all entries in the LB - // (which is still small relative to the total cache). - List<(uint, uint)>? toRemove = null; - foreach (var key in _entries.Keys) - { - if (key.LandblockHint == landblockId) - { - toRemove ??= new List<(uint, uint)>(); - toRemove.Add(key); - } - } - if (toRemove is null) return; - foreach (var k in toRemove) _entries.Remove(k); - } - -#if DEBUG - /// - /// Asserts that the cached entry for still - /// matches what fresh classification would produce. Catches the prior - /// Tier 1 bug class β€” silent caching of mutable per-frame state β€” by - /// firing when any cached - /// field has drifted from live state. - /// - /// - /// Caller passes per-batch live state (Key, BindlessTextureHandle, RestPose) - /// reconstructed from the same path the populate ran. The cache iterates - /// its stored entries in parallel and asserts equality. - /// - /// - /// - /// As of Phase 4 (commit f16604b) this method is exercised by unit tests - /// only; the dispatcher's cache-hit branch fires a simpler predicate assert - /// (!isAnimated) at production hit time. Wiring the full live-state - /// cross-check into the per-entity branch is the spec section 6.5 stretch - /// goal and remains open as a follow-up. Zero cost in Release; the method - /// stays here so the regression-class guard is locked behind tests. - /// - /// - public void DebugCrossCheck(uint entityId, uint landblockHint, IReadOnlyList liveBatches) - { - if (!_entries.TryGetValue((entityId, landblockHint), out var entry)) return; - - System.Diagnostics.Debug.Assert( - entry.Batches.Length == liveBatches.Count, - $"EntityClassificationCache: batch count mismatch for entity {entityId}: cached={entry.Batches.Length} live={liveBatches.Count}"); - - for (int i = 0; i < entry.Batches.Length && i < liveBatches.Count; i++) - { - var cached = entry.Batches[i]; - var live = liveBatches[i]; - System.Diagnostics.Debug.Assert( - cached.Key.Equals(live.Key), - $"EntityClassificationCache: GroupKey drift for entity {entityId} batch {i}"); - System.Diagnostics.Debug.Assert( - cached.BindlessTextureHandle == live.BindlessTextureHandle, - $"EntityClassificationCache: texture handle drift for entity {entityId} batch {i}"); - System.Diagnostics.Debug.Assert( - MatrixApproxEqual(cached.RestPose, live.RestPose, epsilon: 1e-5f), - $"EntityClassificationCache: RestPose drift for entity {entityId} batch {i}"); - } - } - - private static bool MatrixApproxEqual(System.Numerics.Matrix4x4 a, System.Numerics.Matrix4x4 b, float epsilon) - { - return System.MathF.Abs(a.M11 - b.M11) <= epsilon && System.MathF.Abs(a.M12 - b.M12) <= epsilon && - System.MathF.Abs(a.M13 - b.M13) <= epsilon && System.MathF.Abs(a.M14 - b.M14) <= epsilon && - System.MathF.Abs(a.M21 - b.M21) <= epsilon && System.MathF.Abs(a.M22 - b.M22) <= epsilon && - System.MathF.Abs(a.M23 - b.M23) <= epsilon && System.MathF.Abs(a.M24 - b.M24) <= epsilon && - System.MathF.Abs(a.M31 - b.M31) <= epsilon && System.MathF.Abs(a.M32 - b.M32) <= epsilon && - System.MathF.Abs(a.M33 - b.M33) <= epsilon && System.MathF.Abs(a.M34 - b.M34) <= epsilon && - System.MathF.Abs(a.M41 - b.M41) <= epsilon && System.MathF.Abs(a.M42 - b.M42) <= epsilon && - System.MathF.Abs(a.M43 - b.M43) <= epsilon && System.MathF.Abs(a.M44 - b.M44) <= epsilon; - } -#endif -} diff --git a/src/AcDream.App/Rendering/Wb/EntitySpawnAdapter.cs b/src/AcDream.App/Rendering/Wb/EntitySpawnAdapter.cs deleted file mode 100644 index 6303220..0000000 --- a/src/AcDream.App/Rendering/Wb/EntitySpawnAdapter.cs +++ /dev/null @@ -1,194 +0,0 @@ -using System; -using System.Collections.Generic; -using AcDream.Core.Physics; -using AcDream.Core.World; - -namespace AcDream.App.Rendering.Wb; - -/// -/// Routes server-spawned (CreateObject) entities through the -/// per-instance rendering path. Server entities always carry per-instance -/// customizations (palette overrides, texture changes, part swaps) that -/// don't fit WB's atlas key, so they bypass the atlas and use the existing -/// -/// path which already hash-keys overrides for caching. -/// -/// -/// Companion to : that adapter handles -/// atlas-tier (procedural) entities; this one handles per-instance-tier -/// (server-spawned). The boundary is ServerGuid != 0 on -/// . -/// -/// -/// -/// Per-entity texture decode: when entity.PaletteOverride is -/// non-null, the adapter calls -/// -/// once per surface id that is known at spawn time (those on -/// ). Surfaces whose ids are only -/// discoverable by opening the GfxObj dat are decoded lazily by the draw -/// dispatcher (Task 22) on first use β€” that matches the existing -/// StaticMeshRenderer behavior. -/// -/// -/// -/// Sequencer factory: the adapter is constructed with a -/// Func<WorldEntity, AnimationSequencer> factory so tests can -/// inject a stub without needing a live DatCollection or MotionTable. -/// Production callers supply a factory that fetches MotionTable from dats. -/// -/// -/// -/// Adjustment 6 (resolved Adjustment 4): now -/// carries and -/// . applies -/// both to the created . -/// -/// -public sealed class EntitySpawnAdapter -{ - private readonly ITextureCachePerInstance _textureCache; - private readonly Func _sequencerFactory; - private readonly IWbMeshAdapter? _meshAdapter; - - // Per-server-guid state. Written on OnCreate, released on OnRemove. - // Single-threaded: called only from the render thread (same as GpuWorldState). - private readonly Dictionary _stateByGuid = new(); - - // Per-server-guid set of GfxObj ids registered with the mesh adapter, - // so OnRemove can decrement each. Per-instance entities don't go through - // LandblockSpawnAdapter, so without this their meshes would never load - // (WB doesn't know they exist). - private readonly Dictionary> _meshIdsByGuid = new(); - - /// - /// Per-instance texture decode path. In production this is the - /// instance (which implements - /// ); in tests it is a capturing mock. - /// - /// - /// Factory that builds an for a given - /// entity. Receives the full so it can look up - /// the Setup + MotionTable from the entity's SourceGfxObjOrSetupId - /// and server-supplied motion table override. Tests pass a lambda that - /// returns a stub sequencer. - /// - /// - /// Optional WB mesh adapter. When non-null, - /// registers each unique MeshRef.GfxObjId with the adapter so WB - /// background-loads the mesh data; decrements the - /// matching ref counts. When null, the adapter only tracks per-instance - /// state without driving WB lifecycle (test mode + flag-off mode). - /// - public EntitySpawnAdapter( - ITextureCachePerInstance textureCache, - Func sequencerFactory, - IWbMeshAdapter? meshAdapter = null) - { - ArgumentNullException.ThrowIfNull(textureCache); - ArgumentNullException.ThrowIfNull(sequencerFactory); - _textureCache = textureCache; - _sequencerFactory = sequencerFactory; - _meshAdapter = meshAdapter; - } - - /// - /// Process a server-spawned entity. Returns the created - /// for the entity, or null if - /// is atlas-tier (ServerGuid == 0). - /// - public AnimatedEntityState? OnCreate(WorldEntity entity) - { - ArgumentNullException.ThrowIfNull(entity); - - // Atlas-tier entities (procedural / dat-hydrated, ServerGuid == 0) - // are handled by LandblockSpawnAdapter, not here. - if (entity.ServerGuid == 0) return null; - - // Pre-warm the per-instance texture cache for surfaces whose ids are - // already known at spawn time (those appearing as keys in - // MeshRef.SurfaceOverrides). GfxObj sub-mesh surface ids that aren't - // covered by SurfaceOverrides are decoded lazily by the draw - // dispatcher on first use β€” consistent with StaticMeshRenderer. - if (entity.PaletteOverride is { } paletteOverride) - { - foreach (var meshRef in entity.MeshRefs) - { - if (meshRef.SurfaceOverrides is null) continue; - - // SurfaceOverrides maps surfaceId β†’ origTextureOverride (may be 0 - // meaning "no texture swap, just the palette override applies"). - foreach (var (surfaceId, origTexOverride) in meshRef.SurfaceOverrides) - { - _textureCache.GetOrUploadWithPaletteOverride( - surfaceId, - origTexOverride == 0 ? null : origTexOverride, - paletteOverride); - } - } - } - - // A.5 T18: populate cached AABB so WalkEntities reads from the cache - // rather than recomputing PositionΒ±5 per frame. Called here because - // all entity-state initialization (position, rotation) is complete - // by this point via the WorldEntity passed in. - entity.RefreshAabb(); - - // Build the per-entity AnimatedEntityState. The sequencer factory - // may return a stub (in tests) or a fully-constructed sequencer from - // the MotionTable (in production). Factory must not return null β€” - // if the entity has no motion table the factory should construct a - // no-op sequencer (Setup + empty MotionTable + NullAnimationLoader). - var sequencer = _sequencerFactory(entity); - var state = new AnimatedEntityState(sequencer); - - // Adjustment 6: WorldEntity now carries PartOverrides + HiddenPartsMask. - state.HideParts(entity.HiddenPartsMask); - foreach (var po in entity.PartOverrides) - state.SetPartOverride(po.PartIndex, po.GfxObjId); - - _stateByGuid[entity.ServerGuid] = state; - - // Register each unique GfxObj id with WB so the meshes background-load. - // Includes both the entity's natural MeshRefs AND any server-sent - // PartOverride GfxObjs (weapons, clothing, helmets) β€” those replace the - // Setup default and need their own mesh data uploaded. - if (_meshAdapter is not null) - { - var unique = new HashSet(); - foreach (var meshRef in entity.MeshRefs) - unique.Add((ulong)meshRef.GfxObjId); - foreach (var po in entity.PartOverrides) - unique.Add((ulong)po.GfxObjId); - - _meshIdsByGuid[entity.ServerGuid] = unique; - foreach (var id in unique) _meshAdapter.IncrementRefCount(id); - } - - return state; - } - - /// - /// Release the per-entity state for . Called - /// on RemoveObject. Unknown guids (never spawned, or already - /// removed) are silently ignored. - /// - public void OnRemove(uint serverGuid) - { - _stateByGuid.Remove(serverGuid); - - if (_meshAdapter is not null && _meshIdsByGuid.TryGetValue(serverGuid, out var ids)) - { - foreach (var id in ids) _meshAdapter.DecrementRefCount(id); - _meshIdsByGuid.Remove(serverGuid); - } - } - - /// - /// Look up the for a server guid. - /// Returns null if the entity was never spawned or has already - /// been removed. - /// - public AnimatedEntityState? GetState(uint serverGuid) - => _stateByGuid.TryGetValue(serverGuid, out var s) ? s : null; -} diff --git a/src/AcDream.App/Rendering/Wb/GroupKey.cs b/src/AcDream.App/Rendering/Wb/GroupKey.cs deleted file mode 100644 index 696363c..0000000 --- a/src/AcDream.App/Rendering/Wb/GroupKey.cs +++ /dev/null @@ -1,20 +0,0 @@ -using AcDream.Core.Meshing; - -namespace AcDream.App.Rendering.Wb; - -/// -/// Bucket identity for 's per-frame group dictionary. -/// Two (entity, batch) pairs that share the same render -/// in a single glMultiDrawElementsIndirect draw command. Promoted to -/// internal at file scope (was a private nested type) so -/// can store it inside -/// without depending on dispatcher internals. -/// -internal readonly record struct GroupKey( - uint Ibo, - uint FirstIndex, - int BaseVertex, - int IndexCount, - ulong BindlessTextureHandle, - uint TextureLayer, - TranslucencyKind Translucency); diff --git a/src/AcDream.App/Rendering/Wb/ITextureCachePerInstance.cs b/src/AcDream.App/Rendering/Wb/ITextureCachePerInstance.cs deleted file mode 100644 index 491f11d..0000000 --- a/src/AcDream.App/Rendering/Wb/ITextureCachePerInstance.cs +++ /dev/null @@ -1,22 +0,0 @@ -using AcDream.Core.World; - -namespace AcDream.App.Rendering.Wb; - -/// -/// Seam interface over the per-instance palette-override decode path in -/// . Extracted so -/// can be tested without a live GL context. -/// -public interface ITextureCachePerInstance -{ - /// - /// Decode (or return cached) the palette-overridden texture for - /// . Delegates to - /// in - /// production. - /// - uint GetOrUploadWithPaletteOverride( - uint surfaceId, - uint? overrideOrigTextureId, - PaletteOverride paletteOverride); -} diff --git a/src/AcDream.App/Rendering/Wb/IWbMeshAdapter.cs b/src/AcDream.App/Rendering/Wb/IWbMeshAdapter.cs deleted file mode 100644 index 3ea4853..0000000 --- a/src/AcDream.App/Rendering/Wb/IWbMeshAdapter.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace AcDream.App.Rendering.Wb; - -/// -/// Mockable interface over so adapters that -/// drive ref-count lifecycle (e.g. LandblockSpawnAdapter, EntitySpawnAdapter) -/// can be unit-tested without a real WB pipeline behind them. -/// -public interface IWbMeshAdapter -{ - void IncrementRefCount(ulong id); - void DecrementRefCount(ulong id); -} diff --git a/src/AcDream.App/Rendering/Wb/LandblockSpawnAdapter.cs b/src/AcDream.App/Rendering/Wb/LandblockSpawnAdapter.cs deleted file mode 100644 index ec16b7c..0000000 --- a/src/AcDream.App/Rendering/Wb/LandblockSpawnAdapter.cs +++ /dev/null @@ -1,94 +0,0 @@ -using System.Collections.Generic; -using AcDream.Core.World; - -namespace AcDream.App.Rendering.Wb; - -/// -/// Bridges landblock streaming events to 's -/// reference-count lifecycle. Tier-aware by design: only atlas-tier -/// entities (procedural / dat-hydrated, identified by -/// ServerGuid == 0) drive ref counts. Server-spawned entities -/// (per-instance tier) are skipped β€” those go through -/// EntitySpawnAdapter + TextureCache.GetOrUploadWithPaletteOverride -/// (see Phase N.4 spec, Architecture β†’ Two-tier rendering split). -/// -/// -/// On load: walks the landblock's atlas-tier entities, collects unique -/// GfxObj ids from their MeshRefs, calls -/// IncrementRefCount per id. Snapshots the id-set per landblock so -/// unload can match the load 1:1. -/// -/// -/// -/// On unload: looks up the snapshot, calls DecrementRefCount per id, -/// drops the snapshot. Unknown / never-loaded landblocks no-op. -/// -/// -/// -/// Idempotency: a duplicate load for the same landblock is a no-op on -/// ref-counting (the snapshot is already present). Defensive guard against -/// streaming-controller bugs. -/// -/// -/// -/// Thread safety: the underlying implementation -/// uses ConcurrentDictionary, so the streaming worker thread may call -/// this safely. The internal snapshot dictionary is NOT thread-safe and must -/// be called from a single streaming thread (the same thread that fires -/// AddLandblock / RemoveLandblock events). -/// -/// -public sealed class LandblockSpawnAdapter -{ - private readonly IWbMeshAdapter _adapter; - - // Maps landblock id β†’ unique GfxObj ids registered for that landblock. - // Written on load, read+cleared on unload. Single-threaded (streaming worker). - private readonly Dictionary> _idsByLandblock = new(); - - public LandblockSpawnAdapter(IWbMeshAdapter adapter) - { - System.ArgumentNullException.ThrowIfNull(adapter); - _adapter = adapter; - } - - /// - /// Called when a landblock finishes streaming in. - /// Registers a ref-count increment with WB for each unique atlas-tier - /// GfxObj id in the landblock. Duplicate loads for the same landblock id - /// are silently ignored. - /// - public void OnLandblockLoaded(LoadedLandblock landblock) - { - System.ArgumentNullException.ThrowIfNull(landblock); - - // Idempotency: already-loaded landblock is a no-op. - if (_idsByLandblock.ContainsKey(landblock.LandblockId)) return; - - var unique = new HashSet(); - foreach (var entity in landblock.Entities) - { - // Atlas-tier filter: server-spawned entities (ServerGuid != 0) - // belong to the per-instance path and are NOT registered with WB. - if (entity.ServerGuid != 0) continue; - - foreach (var meshRef in entity.MeshRefs) - unique.Add((ulong)meshRef.GfxObjId); - } - - _idsByLandblock[landblock.LandblockId] = unique; - foreach (var id in unique) _adapter.IncrementRefCount(id); - } - - /// - /// Called when a landblock is unloaded from the streaming window. - /// Releases the ref-count for every GfxObj id that was registered on load. - /// Unknown landblock ids (never loaded, or already unloaded) are no-ops. - /// - public void OnLandblockUnloaded(uint landblockId) - { - if (!_idsByLandblock.TryGetValue(landblockId, out var unique)) return; - foreach (var id in unique) _adapter.DecrementRefCount(id); - _idsByLandblock.Remove(landblockId); - } -} diff --git a/src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs b/src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs deleted file mode 100644 index 36ebdc9..0000000 --- a/src/AcDream.App/Rendering/Wb/WbDrawDispatcher.cs +++ /dev/null @@ -1,1306 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Numerics; -using System.Runtime.InteropServices; -using AcDream.Core.Meshing; -using AcDream.Core.Terrain; -using AcDream.Core.World; -using Chorizite.OpenGLSDLBackend.Lib; -using Silk.NET.OpenGL; - -namespace AcDream.App.Rendering.Wb; - -/// -/// Draws entities using WB's (a single global -/// VAO/VBO/IBO under modern rendering) with acdream's -/// for bindless texture resolution and for -/// translucency classification. -/// -/// -/// Atlas-tier entities (ServerGuid == 0): mesh data comes from WB's -/// via . -/// Textures resolve through the bindless-suffixed -/// variants, returning 64-bit -/// resident handles stored in the per-group SSBO. -/// -/// -/// -/// Per-instance-tier entities (ServerGuid != 0): mesh data also from -/// WB, but textures resolve through -/// with palette -/// and surface overrides applied. is currently -/// unused at draw time β€” GameWindow's spawn path already bakes AnimPartChanges + -/// GfxObjDegradeResolver (Issue #47 close-detail mesh) into MeshRefs. -/// -/// -/// -/// GL strategy (N.5 β€” mandatory): glMultiDrawElementsIndirect with SSBOs -/// and GL_ARB_bindless_texture + GL_ARB_shader_draw_parameters. -/// All visible (entity, batch) pairs are bucketed by ; -/// each group becomes one DrawElementsIndirectCommand. Three GPU buffers -/// are uploaded per frame: instance matrices (SSBO binding 0), per-group batch -/// metadata/texture handles (SSBO binding 1), and the indirect draw commands. -/// Two glMultiDrawElementsIndirect calls cover the opaque and transparent -/// passes respectively β€” one GL call per pass regardless of group count. -/// -/// -/// -/// Shader: mesh_modern (bindless + gl_DrawIDARB / -/// gl_BaseInstanceARB). Missing bindless/draw-parameters throws -/// at startup β€” there is no legacy fallback. -/// -/// -/// -/// Modern rendering assumption: WB's _useModernRendering path (GL -/// 4.3 + bindless) puts every mesh in a single shared VAO/VBO/IBO and uses -/// FirstIndex + BaseVertex per batch. The dispatcher honors those -/// offsets inside each DrawElementsIndirectCommand via -/// glMultiDrawElementsIndirect. -/// -/// -public sealed unsafe class WbDrawDispatcher : IDisposable -{ - private readonly GL _gl; - private readonly Shader _shader; - private readonly TextureCache _textures; - private readonly WbMeshAdapter _meshAdapter; - private readonly EntitySpawnAdapter _entitySpawnAdapter; - - private readonly BindlessSupport _bindless; - - // Tier 1 cache (#53): per-entity classification results for static - // entities (those NOT in GameWindow._animatedEntities). Wired here in - // Task 7 for plumbing only β€” Tasks 9-10 wire the per-entity - // miss-populate / hit-fast-path through the loop. - private readonly EntityClassificationCache _cache; - - // ACDREAM_DISABLE_TIER1_CACHE=1 A/B diagnostic β€” forces every static - // entity through the slow path. Read once in ctor. - private readonly bool _tier1CacheDisabled = - string.Equals(Environment.GetEnvironmentVariable("ACDREAM_DISABLE_TIER1_CACHE"), "1", StringComparison.Ordinal); - - /// - /// A.5 T22.5: gate for GL_SAMPLE_ALPHA_TO_COVERAGE around the opaque pass. - /// Default true matches T20 behavior. Set false for Low/Medium presets that - /// have MsaaSamples=0 (A2C is a no-op without MSAA, but turning it off - /// avoids the unnecessary GL state thrash and is cleaner diagnostics). - /// Can be toggled mid-session via . - /// - public bool AlphaToCoverage { get; set; } = true; - - // SSBO buffer ids - private uint _instanceSsbo; - private uint _batchSsbo; - private uint _indirectBuffer; - - // Per-frame scratch arrays β€” Tasks 9-10 fully wire these. - private float[] _instanceData = new float[256 * 16]; // mat4 floats per instance - private BatchData[] _batchData = new BatchData[256]; - private DrawElementsIndirectCommand[] _indirectCommands = new DrawElementsIndirectCommand[256]; - - private int _opaqueDrawCount; - private int _transparentDrawCount; - private int _transparentByteOffset; - - // std430 layout: ulong TextureHandle (uvec2) at offset 0, uint TextureLayer - // at offset 8, uint Flags at offset 12. Total 16 bytes. - // Pack=8 (not 4) because std430's uvec2 requires 8-byte alignment β€” Pack=4 - // works today by accident (TextureHandle is the first field, so offset 0 is - // always 8-byte aligned), but adding a 4-byte field before TextureHandle - // without bumping Pack would silently misalign the GPU struct. - [StructLayout(LayoutKind.Sequential, Pack = 8)] - private struct BatchData - { - public ulong TextureHandle; // bindless handle (uvec2 in GLSL) - public uint TextureLayer; - public uint Flags; - } - - // Per-frame scratch β€” reused across frames to avoid per-frame allocation. - private readonly Dictionary _groups = new(); - private readonly List _opaqueDraws = new(); - private readonly List _translucentDraws = new(); - // A.5 T26 follow-up (Bug B): WalkEntities populates this scratch list - // instead of allocating a fresh List<(WorldEntity, int)> per frame. At - // ~10K entities Γ— ~3 mesh refs = ~30K tuples Γ— 16 bytes = ~480 KB / frame - // of GC pressure on the render thread under the original T17 shape. - private readonly List<(WorldEntity Entity, int MeshRefIndex, uint LandblockId)> _walkScratch = new(); - - // Tier 1 cache (#53) β€” per-entity classification collector. Reused across - // frames; cleared at flush time when the per-entity loop crosses an entity - // boundary in _walkScratch (and once more at end-of-loop for the last - // entity). _walkScratch is in entity-order, so all MeshRefs of one entity - // are contiguous β€” accumulate them all before flushing one Populate call. - // Animated entities skip this scratch entirely (collector = null). - private readonly List _populateScratch = new(); - - // Per-entity-cull AABB radius. Conservative β€” covers most entities; large - // outliers (long banners, tall columns) are still landblock-culled. - private const float PerEntityCullRadius = 5.0f; - - private bool _disposed; - - // Diagnostic counters logged once per ~5s under ACDREAM_WB_DIAG=1. - private int _entitiesSeen; - private int _entitiesDrawn; - private int _meshesMissing; - private int _drawsIssued; - private int _instancesIssued; - private long _lastLogTick; - - // CPU + GPU timing for [WB-DIAG] under ACDREAM_WB_DIAG=1. - private readonly System.Diagnostics.Stopwatch _cpuStopwatch = new(); - private readonly long[] _cpuSamples = new long[256]; // microseconds - private int _cpuSampleCursor; - // GPU timing uses a ring of 3 query-pair slots so the read of frame N-3's - // result lands when the GPU has finished (~50ms after issue on a typical - // 60fps frame). Ring of 3 is the vendor-neutral choice: NVIDIA drivers with - // triple-buffering+vsync can queue ~3 frames ahead, AMD typically 1-2, - // Intel iGPUs vary. ResultAvailable is the safety guard if the GPU is - // still working when we try to read. - private const int GpuQueryRingDepth = 3; - private readonly uint[] _gpuQueryOpaque = new uint[GpuQueryRingDepth]; - private readonly uint[] _gpuQueryTransparent = new uint[GpuQueryRingDepth]; - private int _gpuQueryFrameIndex; - private readonly long[] _gpuSamples = new long[256]; // microseconds - private int _gpuSampleCursor; - private bool _gpuQueriesInitialized; - - // Constructor accessibility is internal because EntityClassificationCache - // is internal β€” a public ctor with an internal-typed parameter would be - // an inconsistent-accessibility error. The dispatcher is constructed - // exclusively from GameWindow (same assembly), so internal is fine. - internal WbDrawDispatcher( - GL gl, - Shader shader, - TextureCache textures, - WbMeshAdapter meshAdapter, - EntitySpawnAdapter entitySpawnAdapter, - BindlessSupport bindless, - EntityClassificationCache classificationCache) - { - ArgumentNullException.ThrowIfNull(gl); - ArgumentNullException.ThrowIfNull(shader); - ArgumentNullException.ThrowIfNull(textures); - ArgumentNullException.ThrowIfNull(meshAdapter); - ArgumentNullException.ThrowIfNull(entitySpawnAdapter); - ArgumentNullException.ThrowIfNull(classificationCache); - - _gl = gl; - _shader = shader; - _textures = textures; - _meshAdapter = meshAdapter; - _entitySpawnAdapter = entitySpawnAdapter; - _cache = classificationCache; - - _bindless = bindless ?? throw new ArgumentNullException(nameof(bindless)); - _instanceSsbo = _gl.GenBuffer(); - _batchSsbo = _gl.GenBuffer(); - _indirectBuffer = _gl.GenBuffer(); - } - - public static Matrix4x4 ComposePartWorldMatrix( - Matrix4x4 entityWorld, - Matrix4x4 animOverride, - Matrix4x4 restPose) - => restPose * animOverride * entityWorld; - - /// - /// Entry for per-landblock iteration. - /// Mirrors the shape yielded by GpuWorldState.LandblockEntries. - /// - public readonly record struct LandblockEntry( - uint LandblockId, - Vector3 AabbMin, - Vector3 AabbMax, - IReadOnlyList Entities, - IReadOnlyDictionary? AnimatedById); - - /// - /// Result of β€” the list of (entity, meshRef index) - /// pairs that passed all visibility filters, plus a diagnostic walk count. - /// - public struct WalkResult - { - public int EntitiesWalked; - public List<(WorldEntity Entity, int MeshRefIndex, uint LandblockId)> ToDraw; - } - - /// - /// Pure-CPU visibility filter over . - /// Separated from so tests can exercise it without GL state. - /// - /// - /// A.5 T17 Change #1: when an LB is frustum-culled AND - /// is non-empty, the OLD path walked - /// every entity in the LB just to find the few animated ones. This helper - /// fixes that: if the LB is invisible, we iterate - /// directly and look each up in - /// entry.AnimatedById (typically <50 animated, up to ~10K total). - /// - /// - /// - /// A.5 T18 Change #2: per-entity AABB cull reads from the cached - /// / - /// (refreshed lazily if ), instead of - /// recomputing PositionΒ±5 each frame. - /// - /// - /// - /// Test-friendly overload that allocates a fresh ToDraw list per call. - /// Production code () uses the no-alloc overload below - /// with a caller-provided scratch list. - /// - internal static WalkResult WalkEntities( - IEnumerable landblockEntries, - FrustumPlanes? frustum, - uint? neverCullLandblockId, - HashSet? visibleCellIds, - HashSet? animatedEntityIds) - { - var scratch = new List<(WorldEntity Entity, int MeshRefIndex, uint LandblockId)>(); - var result = new WalkResult { ToDraw = scratch }; - WalkEntitiesInto( - landblockEntries, frustum, neverCullLandblockId, - visibleCellIds, animatedEntityIds, scratch, ref result); - return result; - } - - /// - /// No-alloc overload: clears + populates the caller-provided - /// list. reuses a per-dispatcher scratch field across frames to - /// avoid the 480+ KB / frame GC pressure that the test-friendly overload incurs. - /// Returns walk count via 's EntitiesWalked field. - /// - internal static void WalkEntitiesInto( - IEnumerable landblockEntries, - FrustumPlanes? frustum, - uint? neverCullLandblockId, - HashSet? visibleCellIds, - HashSet? animatedEntityIds, - List<(WorldEntity Entity, int MeshRefIndex, uint LandblockId)> scratch, - ref WalkResult result) - { - scratch.Clear(); - result.EntitiesWalked = 0; - result.ToDraw = scratch; - - foreach (var entry in landblockEntries) - { - bool landblockVisible = frustum is null - || entry.LandblockId == neverCullLandblockId - || FrustumCuller.IsAabbVisible(frustum.Value, entry.AabbMin, entry.AabbMax); - - if (!landblockVisible) - { - // A.5 T17 Change #1: walk only animated entities, not all entities. - // Avoids O(N_entities) scan when only O(N_animated) work is needed. - if (animatedEntityIds is null || animatedEntityIds.Count == 0) continue; - if (entry.AnimatedById is null) continue; - foreach (var animatedId in animatedEntityIds) - { - if (!entry.AnimatedById.TryGetValue(animatedId, out var entity)) continue; - if (entity.MeshRefs.Count == 0) continue; - if (entity.ParentCellId.HasValue && visibleCellIds is not null - && !visibleCellIds.Contains(entity.ParentCellId.Value)) continue; - result.EntitiesWalked++; - for (int i = 0; i < entity.MeshRefs.Count; i++) - scratch.Add((entity, i, entry.LandblockId)); - } - continue; - } - - foreach (var entity in entry.Entities) - { - if (entity.MeshRefs.Count == 0) continue; - - if (entity.ParentCellId.HasValue && visibleCellIds is not null - && !visibleCellIds.Contains(entity.ParentCellId.Value)) - continue; - - // Per-entity AABB frustum cull (perf #3). Animated entities bypass β€” - // they're tracked at landblock level + need per-frame work regardless. - // A.5 T18 Change #2: read cached AABB, refresh lazily on AabbDirty. - bool isAnimated = animatedEntityIds?.Contains(entity.Id) == true; - if (frustum is not null && !isAnimated && entry.LandblockId != neverCullLandblockId) - { - if (entity.AabbDirty) entity.RefreshAabb(); - if (!FrustumCuller.IsAabbVisible(frustum.Value, entity.AabbMin, entity.AabbMax)) - continue; - } - - result.EntitiesWalked++; - for (int i = 0; i < entity.MeshRefs.Count; i++) - scratch.Add((entity, i, entry.LandblockId)); - } - } - } - - public void Draw( - ICamera camera, - IEnumerable<(uint LandblockId, Vector3 AabbMin, Vector3 AabbMax, - IReadOnlyList Entities, - IReadOnlyDictionary? AnimatedById)> landblockEntries, - FrustumPlanes? frustum = null, - uint? neverCullLandblockId = null, - HashSet? visibleCellIds = null, - HashSet? animatedEntityIds = null) - { - _shader.Use(); - var vp = camera.View * camera.Projection; - _shader.SetMatrix4("uViewProjection", vp); - - bool diag = string.Equals(Environment.GetEnvironmentVariable("ACDREAM_WB_DIAG"), "1", StringComparison.Ordinal); - - if (diag && !_gpuQueriesInitialized) - { - for (int i = 0; i < GpuQueryRingDepth; i++) - { - _gpuQueryOpaque[i] = _gl.GenQuery(); - _gpuQueryTransparent[i] = _gl.GenQuery(); - } - _gpuQueriesInitialized = true; - } - - // Always run the CPU stopwatch β€” cheap; only logged under diag. - _cpuStopwatch.Restart(); - - // Camera world-space position for front-to-back sort (perf #2). The view - // matrix is the inverse of the camera's world transform, so the world - // translation lives in the inverse's translation row. - Vector3 camPos = Vector3.Zero; - if (Matrix4x4.Invert(camera.View, out var invView)) - camPos = invView.Translation; - - // ── Phase 1: clear groups, walk entities, build groups ────────────── - foreach (var grp in _groups.Values) grp.Matrices.Clear(); - - var metaTable = _meshAdapter.MetadataTable; - uint anyVao = 0; - - // Project the 5-tuple enumerable into LandblockEntry records for WalkEntities. - static IEnumerable ToEntries( - IEnumerable<(uint LandblockId, Vector3 AabbMin, Vector3 AabbMax, - IReadOnlyList Entities, - IReadOnlyDictionary? AnimatedById)> src) - { - foreach (var e in src) - yield return new LandblockEntry(e.LandblockId, e.AabbMin, e.AabbMax, e.Entities, e.AnimatedById); - } - - // A.5 T26 follow-up (Bug B): use the no-alloc WalkEntitiesInto overload - // that populates _walkScratch (a per-dispatcher field reused across frames) - // instead of allocating a fresh List<(WorldEntity, int)> per frame. - var walkResult = default(WalkResult); - WalkEntitiesInto( - ToEntries(landblockEntries), - frustum, - neverCullLandblockId, - visibleCellIds, - animatedEntityIds, - _walkScratch, - ref walkResult); - - // Tier 1 cache (#53) flush-tracking locals. _walkScratch holds one tuple - // per (entity, MeshRefIndex) and is in entity-order, so all MeshRefs of - // a given entity are contiguous. We accumulate ALL of an entity's - // batches into _populateScratch, then flush exactly once per entity: - // either when the iteration crosses to a different entity, or at the - // end of the loop for the last entity. Flushing per-tuple would - // overwrite earlier MeshRefs (the cache is keyed by entity.Id), so - // multi-part Setup-backed entities would only retain their LAST - // MeshRef's batches β€” bug fixed in commit after 2f489a8. - uint? populateEntityId = null; - uint populateLandblockId = 0; - - // Tier 1 cache (#53) β€” fast-path one-shot tracker. The cache stores a - // FLAT list of batches across all MeshRefs of an entity, so a single - // ApplyCacheHit call already drew every batch. _walkScratch yields - // one tuple per (entity, MeshRefIndex), so without this guard a - // 3-MeshRef static entity on a frame-2 cache hit would call - // ApplyCacheHit 3 times β€” appending all 6 batches Γ— 3 = 18 instances - // to _groups instead of 6. Result: severe Z-fighting + 3Γ— perf hit - // on every multi-part static entity (buildings, statues, multi-MeshRef - // NPCs). The fast path must fire only on the FIRST tuple of each - // entity; subsequent tuples skip via this tracker. - uint? lastHitEntityId = null; - - // Tier 1 cache (#53) β€” incomplete-entity guard. When any MeshRef of - // the current entity has _meshAdapter.TryGetRenderData return null - // (mesh still async-decoding via ObjectMeshManager.PrepareMeshDataAsync), - // we mark the entity incomplete and DROP the accumulated populate - // scratch at entity boundary instead of writing it to the cache. - // Otherwise the cache would hold a partial classification (some parts - // missing), and frame-2 cache hits would persist that partial render - // even after the missing mesh loads β€” every subsequent frame sees the - // cache hit and skips re-classification, so the missing parts never - // recover. User-visible symptom: the drudge statue on top of the - // Foundry (multi-part Setup entity with AnimPartChange) renders with - // some parts missing permanently. Reset on entity change. - bool currentEntityIncomplete = false; - - // Per-tuple entity tracker used purely for entity-change detection. - // Updated UNCONDITIONALLY at end of every tuple (including tuples that - // skip via null renderData), so the flag-reset block below correctly - // distinguishes "new entity" from "same entity, different tuple." - // populateEntityId can't be used for this because it's only set after - // a successful slow-path classification. - uint? prevTupleEntityId = null; - - foreach (var (entity, partIdx, landblockId) in _walkScratch) - { - if (diag) _entitiesSeen++; - - // Skip subsequent tuples of an entity that already cache-hit on - // its first tuple. ApplyCacheHit drew the full flat batch list; - // re-firing here would N-multiply the instance count. Diag - // _entitiesDrawn is bumped here to preserve per-tuple parity with - // the previous counting semantics. - if (lastHitEntityId == entity.Id) - { - if (diag) _entitiesDrawn++; - continue; - } - - // Reset the hit tracker on entity change so the next entity's - // first tuple re-checks the cache. (When this iteration is the - // FIRST tuple of a new entity after a cache-hit entity, we must - // not retain the previous entity's id.) - if (lastHitEntityId.HasValue && lastHitEntityId.Value != entity.Id) - { - lastHitEntityId = null; - } - - // Tier 1 cache (#53) β€” drop the previous entity's accumulated - // populate scratch BEFORE MaybeFlushOnEntityChange runs. If the - // previous entity ended incomplete (β‰₯1 null renderData), we MUST - // NOT cache its partial classification: clear scratch and null - // the tracker so MaybeFlushOnEntityChange sees the cleaned state - // and no-ops for this entity. Reset the incomplete flag for the - // new entity so each one gets a fresh measurement. - // - // CRITICAL: the flag reset must fire ONLY on entity change, not - // every tuple. Resetting per-tuple within the same entity would - // undo a null-renderData flag set by a previous tuple of the same - // entity β†’ if the missing MeshRef sits in the MIDDLE of the - // entity's MeshRefs list, a later valid tuple's reset would - // re-mark the entity "complete" and let partial data populate - // the cache. Trees with [trunk valid, branches null, leaves - // valid] hit this exactly β€” branches never recover. - bool isNewEntity = !prevTupleEntityId.HasValue || prevTupleEntityId.Value != entity.Id; - if (isNewEntity) - { - if (populateEntityId.HasValue && currentEntityIncomplete) - { - _populateScratch.Clear(); - populateEntityId = null; - } - currentEntityIncomplete = false; - } - prevTupleEntityId = entity.Id; - - // Flush-on-entity-change: if the previous entity accumulated any - // batches AND this iteration is for a different entity, populate - // its cache entry now and reset the scratch buffer. - (populateEntityId, populateLandblockId) = MaybeFlushOnEntityChange( - populateEntityId, populateLandblockId, entity.Id, _cache, _populateScratch); - - var entityWorld = - Matrix4x4.CreateFromQuaternion(entity.Rotation) * - Matrix4x4.CreateTranslation(entity.Position); - - bool isAnimated = animatedEntityIds?.Contains(entity.Id) == true; - - // Cache-hit fast path (Task 10): static entity with a populated - // cache entry skips classification entirely. Walk the cached - // (GroupKey, RestPose) flat list and append cached.RestPose * - // entityWorld to each matching group's matrices. Animated entities - // bypass the cache (collector is set null below; their entries are - // never populated in the first place). - // - // Placed AFTER the entity-change flush above so that, on a - // hit, this iteration also finishes flushing any pending - // populate state from a previous entity. Animated entities never - // enter this branch β€” the !isAnimated guard makes that explicit. - // - // Fires ONCE per entity: the first tuple reaches here, runs - // ApplyCacheHit, sets lastHitEntityId, and continues. Subsequent - // tuples of the same entity short-circuit at the top of the loop - // body via the lastHitEntityId == entity.Id check above. - if (!isAnimated && !_tier1CacheDisabled && _cache.TryGet(entity.Id, landblockId, out var cachedEntry)) - { - ApplyCacheHit(cachedEntry!, entityWorld, AppendInstanceToGroup); - - // anyVao recovery: when the first visible entity in the frame - // takes the fast path, no slow-path lookup has populated - // anyVao yet. Look up THIS entity's first MeshRef once via - // the mesh adapter β€” cheap dict lookup, not a re-classify. - if (anyVao == 0) - { - var firstMeshRef = entity.MeshRefs[partIdx]; - var firstRenderData = _meshAdapter.TryGetRenderData(firstMeshRef.GfxObjId); - if (firstRenderData is not null) anyVao = firstRenderData.VAO; - } - - if (diag) _entitiesDrawn++; - lastHitEntityId = entity.Id; - -#if DEBUG - // Cross-check guard: assert the membership predicate held at hit time. - // The full re-classification cross-check (spec section 6.5) is a stretch - // goal; this simpler assert catches the prior Tier 1 bug class β€” a - // static entity that turns out to actually be animated would fire here. - // - // Structurally redundant with the `if (!isAnimated && ...)` branch - // condition, but serves as a TRIPWIRE: a future refactor that - // incorrectly relaxes the branch condition (e.g., removes - // `!isAnimated` from the guard) would silently allow animated - // entities into the fast path; the assert catches that immediately. - System.Diagnostics.Debug.Assert( - !isAnimated, - $"EntityClassificationCache hit on animated entity {entity.Id} β€” invariant violated"); -#endif - - continue; - } - - // Compute palette-override hash ONCE per entity (perf #4). - // Reused across every (part, batch) lookup so the FNV-1a fold - // over SubPalettes runs once instead of N times. Zero when the - // entity has no palette override (trees, scenery). - ulong palHash = 0; - if (entity.PaletteOverride is not null) - palHash = TextureCache.HashPaletteOverride(entity.PaletteOverride); - - // Note: GameWindow's spawn path already applies - // AnimPartChanges + GfxObjDegradeResolver (Issue #47 fix β€” - // close-detail mesh swap for humanoids) to MeshRefs. We - // trust MeshRefs as the source of truth here. AnimatedEntityState's - // overrides become relevant only for hot-swap (0xF625 - // ObjDescEvent) which today rebuilds MeshRefs anyway. - var meshRef = entity.MeshRefs[partIdx]; - ulong gfxObjId = meshRef.GfxObjId; - - var renderData = _meshAdapter.TryGetRenderData(gfxObjId); - if (renderData is null) - { - // Tier 1 cache (#53): mesh data is still async-decoding via - // WB's ObjectMeshManager.PrepareMeshDataAsync. Flag the entity - // as incomplete so the entity-boundary check (or end-of-loop - // check) drops the accumulated populate scratch instead of - // caching a partial classification. The slow path retries on - // the next frame; once all this entity's meshes have loaded, - // the populate fires with the complete batch set. - currentEntityIncomplete = true; - if (diag) _meshesMissing++; - continue; - } - if (anyVao == 0) anyVao = renderData.VAO; - - // Cache-miss path (animated entities skip cache entirely). - // Static entities accumulate into _populateScratch across ALL - // their MeshRefs; the flush at next-entity-boundary (or - // end-of-loop) commits them as a single Populate call. - var collector = isAnimated ? null : _populateScratch; - - bool drewAny = false; - if (renderData.IsSetup && renderData.SetupParts.Count > 0) - { - foreach (var (partGfxObjId, partTransform) in renderData.SetupParts) - { - var partData = _meshAdapter.TryGetRenderData(partGfxObjId); - if (partData is null) continue; - - var model = ComposePartWorldMatrix( - entityWorld, meshRef.PartTransform, partTransform); - - var restPose = partTransform * meshRef.PartTransform; - ClassifyBatches(partData, partGfxObjId, model, entity, meshRef, palHash, metaTable, restPose, collector); - drewAny = true; - } - } - else - { - var model = meshRef.PartTransform * entityWorld; - ClassifyBatches(renderData, gfxObjId, model, entity, meshRef, palHash, metaTable, restPose: meshRef.PartTransform, collector: collector); - drewAny = true; - } - - // Track THIS entity for the next iteration's flush check. Only - // when collector is non-null (entity is static); animated entities - // leave the tracker null so we don't try to flush them. - if (collector is not null) - { - populateEntityId = entity.Id; - populateLandblockId = landblockId; - } - - if (diag && drewAny) _entitiesDrawn++; - } - - // Tier 1 cache (#53) β€” drop the accumulated populate scratch if the - // LAST entity in the loop ended incomplete (had β‰₯1 null renderData). - // Same reason as the entity-boundary handling above: avoid caching a - // partial classification. The slow path will retry on the next frame - // and populate correctly once all meshes have loaded. - if (currentEntityIncomplete) - { - _populateScratch.Clear(); - populateEntityId = null; - } - - // Final flush: the last entity in _walkScratch has no "next iteration" - // to trigger the entity-change flush, so commit its accumulated batches - // here. No-op when the last entity was animated (populateEntityId stays - // null) or when no entities walked at all. - FinalFlushPopulate(populateEntityId, populateLandblockId, _cache, _populateScratch); - - // Nothing visible β€” skip the GL pass entirely. - if (anyVao == 0) - { - _cpuStopwatch.Stop(); - if (diag) MaybeFlushDiag(); - return; - } - - // ── Phase 3: assign FirstInstance per group, lay matrices contiguously, sort opaque ── - int totalInstances = 0; - foreach (var grp in _groups.Values) totalInstances += grp.Matrices.Count; - if (totalInstances == 0) - { - _cpuStopwatch.Stop(); - if (diag) MaybeFlushDiag(); - return; - } - - int needed = totalInstances * 16; - if (_instanceData.Length < needed) - _instanceData = new float[needed + 256 * 16]; - - _opaqueDraws.Clear(); - _translucentDraws.Clear(); - - int cursor = 0; - foreach (var grp in _groups.Values) - { - if (grp.Matrices.Count == 0) continue; - - grp.FirstInstance = cursor; - grp.InstanceCount = grp.Matrices.Count; - - // Use the first instance's translation as the group's representative - // position for front-to-back sort (perf #2). Cheap heuristic; works - // well when instances of one group are spatially coherent - // (typical for trees in one landblock area, NPCs at one spawn). - var first = grp.Matrices[0]; - var grpPos = new Vector3(first.M41, first.M42, first.M43); - grp.SortDistance = Vector3.DistanceSquared(camPos, grpPos); - - for (int i = 0; i < grp.Matrices.Count; i++) - { - WriteMatrix(_instanceData, cursor * 16, grp.Matrices[i]); - cursor++; - } - - if (IsOpaque(grp.Translucency)) - _opaqueDraws.Add(grp); - else - _translucentDraws.Add(grp); - } - - // Front-to-back sort for opaque pass: nearer groups draw first so the - // depth test rejects fragments hidden behind them, reducing fragment - // shader cost from overdraw on dense scenes (Holtburg courtyard, - // Foundry interior). - _opaqueDraws.Sort(static (a, b) => a.SortDistance.CompareTo(b.SortDistance)); - - // ── Phase 4: build IndirectGroupInput list (opaque sorted, then translucent), - // fill via BuildIndirectArrays ────────────────────────────────── - int totalDraws = _opaqueDraws.Count + _translucentDraws.Count; - if (_batchData.Length < totalDraws) - _batchData = new BatchData[totalDraws + 64]; - if (_indirectCommands.Length < totalDraws) - _indirectCommands = new DrawElementsIndirectCommand[totalDraws + 64]; - - var groupInputs = new List(totalDraws); - foreach (var g in _opaqueDraws) groupInputs.Add(ToInput(g)); - foreach (var g in _translucentDraws) groupInputs.Add(ToInput(g)); - - // Cast _batchData (private BatchData) to public-mirror BatchDataPublic for BuildIndirectArrays. - // Layout is asserted at test time (BatchDataPublic_LayoutMatchesPrivateBatchData test). - var batchPublic = new BatchDataPublic[totalDraws]; - var layout = BuildIndirectArrays(groupInputs, _indirectCommands, batchPublic); - - // Copy back into _batchData - for (int i = 0; i < totalDraws; i++) - { - _batchData[i] = new BatchData - { - TextureHandle = batchPublic[i].TextureHandle, - TextureLayer = batchPublic[i].TextureLayer, - Flags = batchPublic[i].Flags, - }; - } - _opaqueDrawCount = layout.OpaqueCount; - _transparentDrawCount = layout.TransparentCount; - _transparentByteOffset = layout.TransparentByteOffset; - - // ── Phase 5: upload three buffers ─────────────────────────────────── - fixed (float* ip = _instanceData) - UploadSsbo(_instanceSsbo, 0, ip, totalInstances * 16 * sizeof(float)); - - fixed (BatchData* bp = _batchData) - UploadSsbo(_batchSsbo, 1, bp, totalDraws * sizeof(BatchData)); - - fixed (DrawElementsIndirectCommand* cp = _indirectCommands) - { - _gl.BindBuffer(BufferTargetARB.DrawIndirectBuffer, _indirectBuffer); - _gl.BufferData(BufferTargetARB.DrawIndirectBuffer, - (nuint)(totalDraws * sizeof(DrawElementsIndirectCommand)), cp, BufferUsageARB.DynamicDraw); - } - - // ── Phase 6: bind global VAO once ─────────────────────────────────── - _gl.BindVertexArray(anyVao); - - if (string.Equals(Environment.GetEnvironmentVariable("ACDREAM_NO_CULL"), "1", StringComparison.Ordinal)) - _gl.Disable(EnableCap.CullFace); - - // GPU timing: compute this frame's ring slot. We read frame N-3's - // result (the oldest data in the ring) before overwriting it with - // frame N's queries. Hoisted to function scope so both the opaque - // and transparent passes below can reference gpuQuerySlot. See spec - // Β§3 Q1/Q2 + Β§4 in - // docs/superpowers/specs/2026-05-11-phase-n6-slice1-design.md. - int gpuQuerySlot = _gpuQueryFrameIndex % GpuQueryRingDepth; - // diag is part of the gate so the read/issue/increment trio stays - // symmetric β€” without it, toggling ACDREAM_WB_DIAG mid-session would - // freeze the frame counter (gated by diag below) while the read kept - // re-reading the same slot, producing duplicate stale samples. - if (diag && _gpuQueriesInitialized && _gpuQueryFrameIndex >= GpuQueryRingDepth) - { - _gl.GetQueryObject(_gpuQueryOpaque[gpuQuerySlot], QueryObjectParameterName.ResultAvailable, out int avail); - if (avail != 0) - { - _gl.GetQueryObject(_gpuQueryOpaque[gpuQuerySlot], QueryObjectParameterName.Result, out ulong opaqueNs); - _gl.GetQueryObject(_gpuQueryTransparent[gpuQuerySlot], QueryObjectParameterName.Result, out ulong transNs); - long gpuUs = (long)((opaqueNs + transNs) / 1000UL); - _gpuSamples[_gpuSampleCursor] = gpuUs; - _gpuSampleCursor = (_gpuSampleCursor + 1) % _gpuSamples.Length; - } - // If avail==0 the sample is dropped silently. MedianMicros - // computes over the non-zero subset, so dropped samples don't - // poison the median. - } - - // ── Phase 7: opaque pass ───────────────────────────────────────────── - if (_opaqueDrawCount > 0) - { - _gl.Disable(EnableCap.Blend); - _gl.DepthMask(true); - // A.5 T20: enable A2C for ClipMap foliage β€” GPU derives sample mask - // from the alpha written by mesh_modern.frag so foliage edges are - // smooth under MSAA 4x. A no-op for fully-opaque (Ξ±=1) batches. - // A.5 T22.5: gated by AlphaToCoverage property so Low/Medium presets - // (no MSAA) skip the unnecessary GL state change. - if (AlphaToCoverage) _gl.Enable(EnableCap.SampleAlphaToCoverage); - _shader.SetInt("uRenderPass", 0); - // Phase Post-A.5 (ISSUE #52, 2026-05-10): opaque section of - // Batches[] starts at index 0. See uDrawIDOffset comment in - // mesh_modern.vert for why this is needed. - _shader.SetInt("uDrawIDOffset", 0); - _gl.BindBuffer(BufferTargetARB.DrawIndirectBuffer, _indirectBuffer); - if (diag && _gpuQueriesInitialized) _gl.BeginQuery(QueryTarget.TimeElapsed, _gpuQueryOpaque[gpuQuerySlot]); - _gl.MultiDrawElementsIndirect( - PrimitiveType.Triangles, - DrawElementsType.UnsignedShort, - (void*)0, - (uint)_opaqueDrawCount, - (uint)DrawCommandStride); - if (diag && _gpuQueriesInitialized) _gl.EndQuery(QueryTarget.TimeElapsed); - if (AlphaToCoverage) _gl.Disable(EnableCap.SampleAlphaToCoverage); - } - - // ── Phase 8: transparent pass ──────────────────────────────────────── - if (_transparentDrawCount > 0) - { - _gl.Enable(EnableCap.Blend); - _gl.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha); - _gl.DepthMask(false); - // Phase Post-A.5 (ISSUE #52, 2026-05-10): transparent section of - // Batches[] starts at index _opaqueDrawCount. Without this offset, - // each transparent draw reads BatchData[0..transparentCount) β€” the - // OPAQUE section β€” and the lifestone crystal's apparent texture - // flickers to whatever opaque batch sorted first that frame. See - // uDrawIDOffset comment in mesh_modern.vert. - _shader.SetInt("uDrawIDOffset", _opaqueDrawCount); - // Phase Post-A.5 (ISSUE #52, 2026-05-10): re-establish Phase 9.2's - // back-face cull setup. The legacy StaticMeshRenderer had this - // (commit 6f1971a, 2026-04-11) until the N.5 retirement amendment - // (commit dcae2b6, 2026-05-08) deleted that renderer; the new - // WbDrawDispatcher never inherited the cull-face state. - // - // Closed-shell translucent meshes β€” lifestone crystal, glow gems, - // any convex blended mesh β€” NEED back-face culling in the - // translucent pass. Without it, back faces composite OVER front - // faces in arbitrary iteration order, because DepthMask(false) - // means nothing records depth within the translucent set. The - // result is the user-visible "one face missing, see into the - // hollow interior" + frame-to-frame color flicker as rotation - // shifts the triangle order. - // - // Our fan triangulation emits pos-side polygons as (0, i, i+1) β€” - // CCW in standard OpenGL conventions β€” so GL_BACK + CCW-front is - // the correct state. Matches WorldBuilder's per-batch CullMode - // handling. Neg-side polygons (rare on translucent AC content) - // use reversed winding and get culled here, matching the opaque - // pass and the original Phase 9.2 fix's known limitation. - _gl.Enable(EnableCap.CullFace); - _gl.CullFace(TriangleFace.Back); - _gl.FrontFace(FrontFaceDirection.Ccw); - _shader.SetInt("uRenderPass", 1); - if (diag && _gpuQueriesInitialized) _gl.BeginQuery(QueryTarget.TimeElapsed, _gpuQueryTransparent[gpuQuerySlot]); - _gl.MultiDrawElementsIndirect( - PrimitiveType.Triangles, - DrawElementsType.UnsignedShort, - (void*)_transparentByteOffset, - (uint)_transparentDrawCount, - (uint)DrawCommandStride); - if (diag && _gpuQueriesInitialized) _gl.EndQuery(QueryTarget.TimeElapsed); - _gl.DepthMask(true); - _gl.Disable(EnableCap.Blend); - } - - _gl.Disable(EnableCap.CullFace); - _gl.BindVertexArray(0); - - _cpuStopwatch.Stop(); - - if (diag) - { - long cpuUs = _cpuStopwatch.ElapsedTicks * 1_000_000L / System.Diagnostics.Stopwatch.Frequency; - _cpuSamples[_cpuSampleCursor] = cpuUs; - _cpuSampleCursor = (_cpuSampleCursor + 1) % _cpuSamples.Length; - - // GPU sample read happens BEFORE issuing the next frame's queries - // (see step 1.3 above). Increment the frame counter here so the - // next call computes a fresh slot. - if (_gpuQueriesInitialized) _gpuQueryFrameIndex++; - - _drawsIssued += _opaqueDrawCount + _transparentDrawCount; - _instancesIssued += totalInstances; - MaybeFlushDiag(); - } - } - - private static IndirectGroupInput ToInput(InstanceGroup g) => new( - IndexCount: g.IndexCount, - FirstIndex: g.FirstIndex, - BaseVertex: g.BaseVertex, - InstanceCount: g.InstanceCount, - FirstInstance: g.FirstInstance, - TextureHandle: g.BindlessTextureHandle, - TextureLayer: g.TextureLayer, - Translucency: g.Translucency); - - private unsafe void UploadSsbo(uint ssbo, uint binding, void* data, int byteCount) - { - _gl.BindBuffer(BufferTargetARB.ShaderStorageBuffer, ssbo); - _gl.BufferData(BufferTargetARB.ShaderStorageBuffer, (nuint)byteCount, data, BufferUsageARB.DynamicDraw); - _gl.BindBufferBase(BufferTargetARB.ShaderStorageBuffer, binding, ssbo); - } - - private void MaybeFlushDiag() - { - long now = Environment.TickCount64; - if (now - _lastLogTick > 5000) - { - long cpuMed = MedianMicros(_cpuSamples); - long cpuP95 = Percentile95Micros(_cpuSamples); - long gpuMed = MedianMicros(_gpuSamples); - long gpuP95 = Percentile95Micros(_gpuSamples); - // A.5 T23: flag when entity dispatcher median exceeds 2.0ms budget - // (Phase A.5 spec Β§2 acceptance criterion 6). Grep-friendly prefix. - const long BudgetUs = 2000; - string budgetFlag = cpuMed > BudgetUs ? " BUDGET_OVER" : ""; - Console.WriteLine( - $"[WB-DIAG]{budgetFlag} entSeen={_entitiesSeen} entDrawn={_entitiesDrawn} meshMissing={_meshesMissing} drawsIssued={_drawsIssued} instances={_instancesIssued} groups={_groups.Count} " + - $"cpu_us={cpuMed}m/{cpuP95}p95 gpu_us={gpuMed}m/{gpuP95}p95"); - _entitiesSeen = _entitiesDrawn = _meshesMissing = _drawsIssued = _instancesIssued = 0; - _lastLogTick = now; - // Don't reset the sample buffers β€” they're a moving window of the - // last 256 frames; clearing per 5s flush would lose recent history. - } - } - - private static long MedianMicros(long[] samples) - { - var copy = (long[])samples.Clone(); - Array.Sort(copy); - int nz = 0; - foreach (var v in copy) if (v > 0) nz++; - if (nz == 0) return 0; - return copy[copy.Length - nz / 2]; - } - - private static long Percentile95Micros(long[] samples) - { - var copy = (long[])samples.Clone(); - Array.Sort(copy); - int nz = 0; - foreach (var v in copy) if (v > 0) nz++; - if (nz == 0) return 0; - int idx = copy.Length - 1 - (int)(nz * 0.05); - return copy[idx]; - } - - // ── Tier 1 cache (#53) helpers extracted for testability ───────────────── - // - // Three pure-CPU static helpers carved out of Draw's per-entity loop so - // unit tests can exercise the populate/flush algorithm + cache-hit fast - // path without needing a real GL context. Production code (Draw) calls - // these helpers; the dispatcher integration tests in - // WbDrawDispatcherBucketingTests use them to drive the same algorithm - // through deterministic inputs. - - /// - /// Apply a cache hit's batches into the per-frame group dictionary by - /// composing cached.RestPose * entityWorld per batch and routing - /// the result through . The delegate - /// abstracts over so this helper stays - /// GL-free and unit-testable. - /// - /// - /// Matrix multiplication is non-commutative: it MUST be - /// RestPose * entityWorld, not the reverse. See - /// for the full part-world product. - /// - internal static void ApplyCacheHit( - EntityCacheEntry entry, - Matrix4x4 entityWorld, - Action appendInstance) - { - foreach (var cached in entry.Batches) - { - appendInstance(cached.Key, cached.RestPose * entityWorld); - } - } - - /// - /// Per-tuple flush check. If is set - /// AND differs from , the previous - /// entity's accumulated batches are committed to - /// and is cleared. Returns the - /// updated tracker tuple β€” pass these back into the field locals in the - /// caller's loop. - /// - /// - /// This is the bug-fix structure from commit 00fa8ae (per-MeshRef - /// Populate would overwrite earlier MeshRefs because the cache is - /// keyed by entity.Id; flushing only on entity boundary preserves all - /// MeshRefs' batches). _walkScratch is in entity-order so all MeshRefs - /// of one entity arrive contiguously. - /// - internal static (uint? PopulateEntityId, uint PopulateLandblockId) - MaybeFlushOnEntityChange( - uint? populateEntityId, - uint populateLandblockId, - uint currentEntityId, - EntityClassificationCache cache, - List populateScratch) - { - if (populateEntityId.HasValue && populateEntityId.Value != currentEntityId) - { - if (populateScratch.Count > 0) - { - cache.Populate(populateEntityId.Value, populateLandblockId, populateScratch.ToArray()); - } - populateScratch.Clear(); - return (null, 0u); - } - return (populateEntityId, populateLandblockId); - } - - /// - /// End-of-loop final flush. The last entity in _walkScratch has - /// no next-iteration to trigger , - /// so commit its accumulated batches here. No-op when no populate is - /// pending (the last entity was animated, or the scratch is empty). - /// - /// End-of-loop only β€” does NOT reset the caller's tracker locals - /// (intentional, since they go out of scope immediately after). - /// - /// - internal static void FinalFlushPopulate( - uint? populateEntityId, - uint populateLandblockId, - EntityClassificationCache cache, - List populateScratch) - { - if (populateEntityId.HasValue && populateScratch.Count > 0) - { - cache.Populate(populateEntityId.Value, populateLandblockId, populateScratch.ToArray()); - populateScratch.Clear(); - } - } - - /// - /// Instance-side helper used by . Looks up or - /// creates an for the given key in - /// _groups and appends the per-instance world matrix. - /// - private void AppendInstanceToGroup(GroupKey key, Matrix4x4 model) - { - if (!_groups.TryGetValue(key, out var grp)) - { - grp = new InstanceGroup - { - Ibo = key.Ibo, - FirstIndex = key.FirstIndex, - BaseVertex = key.BaseVertex, - IndexCount = key.IndexCount, - BindlessTextureHandle = key.BindlessTextureHandle, - TextureLayer = key.TextureLayer, - Translucency = key.Translucency, - }; - _groups[key] = grp; - } - grp.Matrices.Add(model); - } - - private void ClassifyBatches( - ObjectRenderData renderData, - ulong gfxObjId, - Matrix4x4 model, - WorldEntity entity, - MeshRef meshRef, - ulong palHash, - AcSurfaceMetadataTable metaTable, - Matrix4x4 restPose, - List? collector = null) - { - for (int batchIdx = 0; batchIdx < renderData.Batches.Count; batchIdx++) - { - var batch = renderData.Batches[batchIdx]; - - TranslucencyKind translucency; - if (metaTable.TryLookup(gfxObjId, batchIdx, out var meta)) - { - translucency = meta.Translucency; - } - else - { - translucency = batch.IsAdditive ? TranslucencyKind.Additive - : batch.IsTransparent ? TranslucencyKind.AlphaBlend - : TranslucencyKind.Opaque; - } - - ulong texHandle = ResolveTexture(entity, meshRef, batch, palHash); - if (texHandle == 0) continue; - - // TextureLayer is always 0 for per-instance composites; non-zero when - // WB atlas is adopted in N.6+ and batches reference a shared atlas layer. - uint texLayer = 0; - - var key = new GroupKey( - batch.IBO, batch.FirstIndex, (int)batch.BaseVertex, - batch.IndexCount, texHandle, texLayer, translucency); - - if (!_groups.TryGetValue(key, out var grp)) - { - grp = new InstanceGroup - { - Ibo = batch.IBO, - FirstIndex = batch.FirstIndex, - BaseVertex = (int)batch.BaseVertex, - IndexCount = batch.IndexCount, - BindlessTextureHandle = texHandle, - TextureLayer = texLayer, - Translucency = translucency, - }; - _groups[key] = grp; - } - grp.Matrices.Add(model); - collector?.Add(new CachedBatch(key, texHandle, restPose)); - } - } - - private ulong ResolveTexture(WorldEntity entity, MeshRef meshRef, ObjectRenderBatch batch, ulong palHash) - { - uint surfaceId = batch.Key.SurfaceId; - if (surfaceId == 0 || surfaceId == 0xFFFFFFFF) return 0; - - uint overrideOrigTex = 0; - bool hasOrigTexOverride = meshRef.SurfaceOverrides is not null - && meshRef.SurfaceOverrides.TryGetValue(surfaceId, out overrideOrigTex); - uint? origTexOverride = hasOrigTexOverride ? overrideOrigTex : (uint?)null; - - if (entity.PaletteOverride is not null) - { - return _textures.GetOrUploadWithPaletteOverrideBindless( - surfaceId, origTexOverride, entity.PaletteOverride, palHash); - } - else if (hasOrigTexOverride) - { - return _textures.GetOrUploadWithOrigTextureOverrideBindless(surfaceId, overrideOrigTex); - } - else - { - return _textures.GetOrUploadBindless(surfaceId); - } - } - - private static void WriteMatrix(float[] buf, int offset, in Matrix4x4 m) - { - buf[offset + 0] = m.M11; buf[offset + 1] = m.M12; buf[offset + 2] = m.M13; buf[offset + 3] = m.M14; - buf[offset + 4] = m.M21; buf[offset + 5] = m.M22; buf[offset + 6] = m.M23; buf[offset + 7] = m.M24; - buf[offset + 8] = m.M31; buf[offset + 9] = m.M32; buf[offset + 10] = m.M33; buf[offset + 11] = m.M34; - buf[offset + 12] = m.M41; buf[offset + 13] = m.M42; buf[offset + 14] = m.M43; buf[offset + 15] = m.M44; - } - - public void Dispose() - { - if (_disposed) return; - _disposed = true; - _gl.DeleteBuffer(_instanceSsbo); - _gl.DeleteBuffer(_batchSsbo); - _gl.DeleteBuffer(_indirectBuffer); - if (_gpuQueriesInitialized) - { - for (int i = 0; i < GpuQueryRingDepth; i++) - { - _gl.DeleteQuery(_gpuQueryOpaque[i]); - _gl.DeleteQuery(_gpuQueryTransparent[i]); - } - } - } - - // ── Public types + helpers for BuildIndirectArrays (Task 9) ───────────── - // - // These are public so the pure-CPU unit tests in AcDream.Core.Tests can - // exercise BuildIndirectArrays without needing a GL context. - - /// - /// Stride in bytes of DrawElementsIndirectCommand in the indirect buffer. - /// 5 Γ— uint = 20 bytes. Tests and callers reference this symbolically - /// rather than hard-coding 20 so a layout change produces a compile error. - /// - public const int DrawCommandStride = 20; // sizeof(DrawElementsIndirectCommand): 5 Γ— uint - - /// - /// Public view of the per-group inputs to β€” used in tests. - /// - public readonly record struct IndirectGroupInput( - int IndexCount, - uint FirstIndex, - int BaseVertex, - int InstanceCount, - int FirstInstance, - ulong TextureHandle, - uint TextureLayer, - TranslucencyKind Translucency); - - /// - /// Public mirror of the per-group uploaded to the SSBO. - /// Tests verify the layout. Same field shape as the private BatchData. - /// - [StructLayout(LayoutKind.Sequential, Pack = 8)] - public struct BatchDataPublic - { - public ulong TextureHandle; - public uint TextureLayer; - public uint Flags; - } - - /// Result of . - public readonly record struct IndirectLayoutResult( - int OpaqueCount, - int TransparentCount, - int TransparentByteOffset); - - /// - /// Lays out the indirect commands + parallel BatchData array contiguously: - /// opaque section first (caller sorts before calling), transparent section second. - /// Pure CPU, no GL state. Caller passes pre-sized scratch arrays. - /// - /// - /// Classification: Opaque + ClipMap β†’ opaque pass (ClipMap uses discard, not - /// blending). Everything else (AlphaBlend, Additive, InvAlpha) β†’ transparent pass. - /// - public static IndirectLayoutResult BuildIndirectArrays( - IReadOnlyList groups, - DrawElementsIndirectCommand[] indirectScratch, - BatchDataPublic[] batchScratch) - { - int opaqueCount = 0; - int transparentCount = 0; - - foreach (var g in groups) - { - if (IsOpaque(g.Translucency)) opaqueCount++; - else transparentCount++; - } - - int oi = 0; // opaque write cursor (fills [0..opaqueCount)) - int ti = opaqueCount; // transparent write cursor (fills [opaqueCount..end)) - - foreach (var g in groups) - { - var dec = new DrawElementsIndirectCommand - { - Count = (uint)g.IndexCount, - InstanceCount = (uint)g.InstanceCount, - FirstIndex = g.FirstIndex, - BaseVertex = g.BaseVertex, - BaseInstance = (uint)g.FirstInstance, - }; - var bd = new BatchDataPublic - { - TextureHandle = g.TextureHandle, - TextureLayer = g.TextureLayer, - Flags = 0, - }; - - if (IsOpaque(g.Translucency)) - { - indirectScratch[oi] = dec; - batchScratch[oi] = bd; - oi++; - } - else - { - indirectScratch[ti] = dec; - batchScratch[ti] = bd; - ti++; - } - } - - return new IndirectLayoutResult(opaqueCount, transparentCount, opaqueCount * DrawCommandStride); - } - - /// - /// Public test shim for . Locks in the N.5 Decision 2 - /// translucency partition: Opaque + ClipMap β†’ opaque indirect; AlphaBlend + - /// Additive + InvAlpha β†’ transparent indirect. - /// - public static bool IsOpaquePublic(TranslucencyKind t) => IsOpaque(t); - - private static bool IsOpaque(TranslucencyKind t) - => t == TranslucencyKind.Opaque || t == TranslucencyKind.ClipMap; - - // ──────────────────────────────────────────────────────────────────────── - - private sealed class InstanceGroup - { - public uint Ibo; - public uint FirstIndex; - public int BaseVertex; - public int IndexCount; - public ulong BindlessTextureHandle; // 64-bit (was uint TextureHandle in N.4) - public uint TextureLayer; // 0 for per-instance composites; non-zero when WB atlas is adopted in N.6+ - public TranslucencyKind Translucency; - public int FirstInstance; // offset into the shared instance VBO (in instances, not bytes) - public int InstanceCount; - public float SortDistance; // squared distance from camera to first instance, for opaque sort - public readonly List Matrices = new(); - } -} diff --git a/src/AcDream.App/Rendering/Wb/WbMeshAdapter.cs b/src/AcDream.App/Rendering/Wb/WbMeshAdapter.cs deleted file mode 100644 index b57e043..0000000 --- a/src/AcDream.App/Rendering/Wb/WbMeshAdapter.cs +++ /dev/null @@ -1,203 +0,0 @@ -using System; -using System.Collections.Generic; -using AcDream.Core.Meshing; -using Chorizite.OpenGLSDLBackend; -using Chorizite.OpenGLSDLBackend.Lib; -using DatReaderWriter; -using DatReaderWriter.DBObjs; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Logging.Abstractions; -using Silk.NET.OpenGL; -using WorldBuilder.Shared.Models; -using WorldBuilder.Shared.Services; - -namespace AcDream.App.Rendering.Wb; - -/// -/// Single seam between acdream and WB's render pipeline. Owns the -/// ObjectMeshManager instance and exposes a stable acdream-shaped API -/// so the rest of the renderer doesn't need to know about WB's types directly. -/// -/// -/// The adapter constructs its own DefaultDatReaderWriter internally; it -/// does NOT share file handles with our DatCollection. This duplicates -/// index-cache memory (~50–100 MB) but keeps the two subsystems fully decoupled. -/// Acceptable for Phase N.4 foundation work (plan Adjustment 1). -/// -/// -public sealed class WbMeshAdapter : IDisposable, IWbMeshAdapter -{ - private readonly OpenGLGraphicsDevice? _graphicsDevice; - private readonly DefaultDatReaderWriter? _wbDats; - private readonly ObjectMeshManager? _meshManager; - private readonly DatCollection? _dats; - private readonly AcSurfaceMetadataTable _metadataTable = new(); - private readonly HashSet _metadataPopulated = new(); - - /// - /// True when this instance was created via ; - /// all public methods no-op when uninitialized. - /// - private readonly bool _isUninitialized; - - private bool _disposed; - - /// - /// Constructs the full WB pipeline: OpenGLGraphicsDevice β†’ DefaultDatReaderWriter - /// β†’ ObjectMeshManager. - /// - /// Active Silk.NET GL context. Must be bound to the current - /// thread (construction runs GL queries; call from OnLoad). - /// Path to the dat directory (same as the one supplied - /// to our DatCollection). DefaultDatReaderWriter opens its own file handles. - /// acdream's DatCollection, used to populate the surface - /// metadata side-table via GfxObjMesh.Build. Shares file handles with - /// the rest of the client; read-only access from the render thread. - /// Logger for the adapter; ObjectMeshManager uses - /// NullLogger internally. - public WbMeshAdapter(GL gl, string datDir, DatCollection dats, ILogger logger) - { - ArgumentNullException.ThrowIfNull(gl); - ArgumentNullException.ThrowIfNull(datDir); - ArgumentNullException.ThrowIfNull(dats); - ArgumentNullException.ThrowIfNull(logger); - - _dats = dats; - _graphicsDevice = new OpenGLGraphicsDevice(gl, logger, new DebugRenderSettings()); - _wbDats = new DefaultDatReaderWriter(datDir); - _meshManager = new ObjectMeshManager( - _graphicsDevice, - _wbDats, - NullLogger.Instance); - } - - private WbMeshAdapter() - { - // Uninitialized constructor β€” only for tests / flag-off cases where - // the caller wants a Dispose-safe no-op instance. - _isUninitialized = true; - } - - /// Test/init helper β€” produces a Dispose-safe instance with no - /// underlying mesh manager. Public methods are all no-ops. - public static WbMeshAdapter CreateUninitialized() => new(); - - /// - /// The surface metadata side-table populated on each first - /// . Queried by the draw dispatcher - /// to determine translucency, luminosity, and fog behavior per batch. - /// - public AcSurfaceMetadataTable MetadataTable => _metadataTable; - - /// - /// Returns the WB render data for , or null if not - /// yet uploaded or if this adapter is uninitialized. Increments WB's - /// internal usage counter β€” use for - /// render-loop lookups that should not affect lifecycle. - /// - public ObjectRenderData? GetRenderData(ulong id) - { - if (_isUninitialized || _meshManager is null) return null; - return _meshManager.GetRenderData(id); - } - - /// - /// Returns the WB render data for without - /// modifying reference counts. Returns null if the mesh is not yet - /// uploaded. Safe for render-loop lookups. - /// - public ObjectRenderData? TryGetRenderData(ulong id) - { - if (_isUninitialized || _meshManager is null) return null; - return _meshManager.TryGetRenderData(id); - } - - /// - public void IncrementRefCount(ulong id) - { - if (_isUninitialized || _meshManager is null) return; - _meshManager.IncrementRefCount(id); - - if (_metadataPopulated.Add(id)) - { - PopulateMetadata(id); - - // WB's IncrementRefCount alone only bumps a usage counter; it does - // NOT trigger mesh loading. We must explicitly call PrepareMeshDataAsync - // so the background workers actually decode the GfxObj. The result - // auto-enqueues into _stagedMeshData (ObjectMeshManager line 510), - // which Tick() drains onto the GPU. Until that completes, - // TryGetRenderData(id) returns null and the dispatcher silently - // skips the entity β€” standard streaming flicker. - // - // isSetup: false β€” acdream's MeshRefs already carry expanded - // per-part GfxObj ids (0x01XXXXXX). WB's Setup-expansion path is - // unused. - _ = _meshManager.PrepareMeshDataAsync(id, isSetup: false); - } - } - - /// - public void DecrementRefCount(ulong id) - { - if (_isUninitialized || _meshManager is null) return; - _meshManager.DecrementRefCount(id); - } - - /// - /// Per-frame drain of the WB pipeline's main-thread work queues. MUST be - /// called once per frame from the render thread. Without this, the staged - /// mesh data queue grows unbounded (memory leak) and queued GL actions - /// never execute. - /// - /// - /// Order matters: ProcessGLQueue runs first to apply any pending GL - /// state changes (e.g., texture uploads queued by background workers - /// during mesh prep). Then we drain staged mesh data, calling - /// UploadMeshData on each item to materialize the actual GL VAO / - /// VBO / IBO resources. After Tick, GetRenderData for any id - /// previously passed to IncrementRefCount may return non-null. - /// - /// - /// - /// No-op when the adapter is uninitialized (e.g., flag is off and the - /// adapter was constructed via CreateUninitialized). - /// - /// - public void Tick() - { - if (_isUninitialized) return; - if (_disposed) return; - - _graphicsDevice!.ProcessGLQueue(); - while (_meshManager!.StagedMeshData.TryDequeue(out var meshData)) - { - _meshManager.UploadMeshData(meshData); - } - } - - private void PopulateMetadata(ulong id) - { - if (_dats is null) return; - if (!_dats.Portal.TryGet((uint)id, out var gfxObj)) return; - - var subMeshes = GfxObjMesh.Build(gfxObj, _dats); - for (int i = 0; i < subMeshes.Count; i++) - { - var sm = subMeshes[i]; - _metadataTable.Add(id, i, new AcSurfaceMetadata( - sm.Translucency, sm.Luminosity, sm.Diffuse, - sm.SurfOpacity, sm.NeedsUvRepeat, sm.DisableFog)); - } - } - - /// - public void Dispose() - { - if (_disposed) return; - _disposed = true; - _meshManager?.Dispose(); - _wbDats?.Dispose(); - _graphicsDevice?.Dispose(); - } -} diff --git a/src/AcDream.App/Streaming/GpuWorldState.cs b/src/AcDream.App/Streaming/GpuWorldState.cs index 90472f6..f3448ef 100644 --- a/src/AcDream.App/Streaming/GpuWorldState.cs +++ b/src/AcDream.App/Streaming/GpuWorldState.cs @@ -1,8 +1,6 @@ using System.Collections.Generic; using System.Linq; using System.Numerics; -using AcDream.App.Rendering.Vfx; -using AcDream.App.Rendering.Wb; using AcDream.Core.World; namespace AcDream.App.Streaming; @@ -40,34 +38,6 @@ namespace AcDream.App.Streaming; /// public sealed class GpuWorldState { - private readonly LandblockSpawnAdapter? _wbSpawnAdapter; - private readonly EntitySpawnAdapter? _wbEntitySpawnAdapter; - private readonly EntityScriptActivator? _entityScriptActivator; - - /// - /// Phase Post-A.5 #53 (Task 12): optional callback fired before - /// zeroes a landblock's entity - /// list. Wired by GameWindow to - /// EntityClassificationCache.InvalidateLandblock so Tier 1 cache - /// entries get swept on LB demote (Near to Far) and unload. Receives - /// the canonicalized landblock id (low 16 bits forced to 0xFFFF), - /// matching the LandblockHint stored at Populate time. - /// Null when the cache isn't relevant (tests). - /// - private readonly System.Action? _onLandblockUnloaded; - - public GpuWorldState( - LandblockSpawnAdapter? wbSpawnAdapter = null, - EntitySpawnAdapter? wbEntitySpawnAdapter = null, - System.Action? onLandblockUnloaded = null, - EntityScriptActivator? entityScriptActivator = null) - { - _wbSpawnAdapter = wbSpawnAdapter; - _wbEntitySpawnAdapter = wbEntitySpawnAdapter; - _onLandblockUnloaded = onLandblockUnloaded; - _entityScriptActivator = entityScriptActivator; - } - private readonly Dictionary _loaded = new(); private readonly Dictionary _aabbs = new(); @@ -124,33 +94,17 @@ public sealed class GpuWorldState /// Per-landblock iteration with AABB data for use by the frustum-culling /// draw path. Landblocks without a stored AABB yield /// for both corners, which the culler will conservatively treat as visible. - /// - /// - /// A.5 T17: also yields an AnimatedById dictionary built on the fly - /// from the landblock's entity list. This lets - /// skip the full entity walk when the landblock is frustum-culled but animated - /// entities inside it must still be processed (Change #1). - /// Building the dict per-yield is cheap (~132 entities/LB max). A caching - /// layer is out of A.5 scope. - /// /// - public IEnumerable<(uint LandblockId, Vector3 AabbMin, Vector3 AabbMax, - IReadOnlyList Entities, - IReadOnlyDictionary? AnimatedById)> LandblockEntries + public IEnumerable<(uint LandblockId, Vector3 AabbMin, Vector3 AabbMax, IReadOnlyList Entities)> LandblockEntries { get { foreach (var kvp in _loaded) { - // Build AnimatedById on the fly β€” cheap (~132 entities/LB max). - var byId = new Dictionary(kvp.Value.Entities.Count); - foreach (var e in kvp.Value.Entities) - byId[e.Id] = e; - if (_aabbs.TryGetValue(kvp.Key, out var aabb)) - yield return (kvp.Key, aabb.Min, aabb.Max, kvp.Value.Entities, byId); + yield return (kvp.Key, aabb.Min, aabb.Max, kvp.Value.Entities); else - yield return (kvp.Key, Vector3.Zero, Vector3.Zero, kvp.Value.Entities, byId); + yield return (kvp.Key, Vector3.Zero, Vector3.Zero, kvp.Value.Entities); } } } @@ -178,23 +132,6 @@ public sealed class GpuWorldState } _loaded[landblock.LandblockId] = landblock; - if (_wbSpawnAdapter is not null) - _wbSpawnAdapter.OnLandblockLoaded(_loaded[landblock.LandblockId]); - - // C.1.5b: fire DefaultScript for dat-hydrated entities (ServerGuid==0). - // Live entities (ServerGuid!=0) already had OnCreate fired at - // AppendLiveEntity; the filter avoids double-firing pending-bucket merges. - if (_entityScriptActivator is not null) - { - var loadedEntities = _loaded[landblock.LandblockId].Entities; - for (int i = 0; i < loadedEntities.Count; i++) - { - var e = loadedEntities[i]; - if (e.ServerGuid == 0) - _entityScriptActivator.OnCreate(e); - } - } - RebuildFlatView(); } @@ -244,9 +181,6 @@ public sealed class GpuWorldState public void RemoveLandblock(uint landblockId) { - if (_wbSpawnAdapter is not null) - _wbSpawnAdapter.OnLandblockUnloaded(landblockId); - // Rescue persistent entities before removal. These get appended // to the _persistentRescued list; the caller is responsible for // re-injecting them (via AppendLiveEntity) into whatever landblock @@ -260,19 +194,6 @@ public sealed class GpuWorldState _persistentRescued.Add(entity); } } - - // C.1.5b: stop DefaultScript for each dat-hydrated entity in - // the landblock. Server-spawned entities are either being - // rescued (script continues at the new LB) or were OnRemove'd - // via RemoveEntityByServerGuid earlier; leave them alone here. - if (_entityScriptActivator is not null) - { - foreach (var entity in lb.Entities) - { - if (entity.ServerGuid == 0) - _entityScriptActivator.OnRemove(entity.Id); - } - } } _pendingByLandblock.Remove(landblockId); @@ -312,11 +233,6 @@ public sealed class GpuWorldState { if (serverGuid == 0) return; - // Phase N.4 Task 17: release per-instance state for server-spawned - // entities. No-op for atlas-tier entities (never registered). - _wbEntitySpawnAdapter?.OnRemove(serverGuid); - _entityScriptActivator?.OnRemove(serverGuid); - bool rebuiltLoaded = false; // Scan loaded landblocks. ToArray() so we can mutate _loaded inside. @@ -372,12 +288,6 @@ public sealed class GpuWorldState /// public void AppendLiveEntity(uint landblockId, WorldEntity entity) { - // Phase N.4 Task 17: route server-spawned entities through the - // per-instance adapter. Atlas-tier entities (ServerGuid == 0) are - // skipped by OnCreate β€” it returns null immediately for those. - _wbEntitySpawnAdapter?.OnCreate(entity); - _entityScriptActivator?.OnCreate(entity); - uint canonicalLandblockId = (landblockId & 0xFFFF0000u) | 0xFFFFu; if (_loaded.TryGetValue(canonicalLandblockId, out var lb)) @@ -403,104 +313,6 @@ public sealed class GpuWorldState bucket.Add(entity); } - /// - /// Drop all entities from a landblock without removing the terrain. Used - /// by two-tier streaming when a landblock crosses Nearβ†’Far hysteresis. - /// Per Phase A.5 spec Β§4.4. - /// - /// - /// Persistent-entity rescue is intentionally omitted (unlike - /// ): demote-tier entities are atlas-tier - /// only (procedural scenery, dat-static stabs/buildings) β€” they never - /// have ServerGuid != 0 and so can never be in . - /// The local player and other live server-spawned entities live in their - /// landblock via RelocateEntity per frame and are not affected - /// by Nearβ†’Far demotion of dat-static landblock layers. - /// - /// - public void RemoveEntitiesFromLandblock(uint landblockId) - { - // A.5 T14 follow-up: canonicalize for symmetry with AppendLiveEntity. - // Streaming callers always pass canonical (0xAAAA0xFFFF) ids; this - // protects against future callers that mirror AppendLiveEntity's - // cell-resolved-id pattern. - uint canonical = (landblockId & 0xFFFF0000u) | 0xFFFFu; - if (!_loaded.TryGetValue(canonical, out var lb)) return; - if (_wbSpawnAdapter is not null) - _wbSpawnAdapter.OnLandblockUnloaded(canonical); - - // Phase Post-A.5 #53 (Task 12): invalidate the EntityClassificationCache - // for this landblock BEFORE we drop the entity list. The cache stores - // canonical landblock ids (the dispatcher's _walkScratch carries - // entry.LandblockId from GpuWorldState.LandblockEntries, whose keys are - // canonicalized). Null when the cache isn't wired (tests). Per spec Β§5.3 W3b. - _onLandblockUnloaded?.Invoke(canonical); - - // C.1.5b: stop DefaultScript for each dat-hydrated entity about to - // be dropped. Demote-tier entities are always atlas-tier (ServerGuid==0 - // per this method's class doc-comment); the filter is belt-and-suspenders. - if (_entityScriptActivator is not null) - { - foreach (var entity in lb.Entities) - { - if (entity.ServerGuid == 0) - _entityScriptActivator.OnRemove(entity.Id); - } - } - - _loaded[canonical] = new LoadedLandblock(lb.LandblockId, lb.Heightmap, System.Array.Empty()); - _pendingByLandblock.Remove(canonical); - RebuildFlatView(); - } - - /// - /// Merge entities into an existing-loaded landblock. Used by two-tier - /// streaming for the Farβ†’Near promotion case (terrain already loaded; - /// entity layer streaming in). Falls back to the pending bucket if the - /// landblock isn't loaded yet (handles the rare "promote arrives before - /// far load completes" race). - /// Per Phase A.5 spec Β§4.4. - /// - /// - /// Landblock id is canonicalized (low 16 bits forced to 0xFFFF) β€” - /// callers may pass cell-resolved ids and they will key correctly. - /// - /// - public void AddEntitiesToExistingLandblock(uint landblockId, IReadOnlyList entities) - { - // A.5 T14 follow-up: canonicalize for symmetry with AppendLiveEntity. - uint canonical = (landblockId & 0xFFFF0000u) | 0xFFFFu; - if (!_loaded.TryGetValue(canonical, out var lb)) - { - // Park as pending β€” same pattern as AppendLiveEntity for not-yet-loaded LBs. - if (!_pendingByLandblock.TryGetValue(canonical, out var bucket)) - { - bucket = new List(); - _pendingByLandblock[canonical] = bucket; - } - bucket.AddRange(entities); - return; - } - var merged = new List(lb.Entities.Count + entities.Count); - merged.AddRange(lb.Entities); - merged.AddRange(entities); - _loaded[canonical] = new LoadedLandblock(lb.LandblockId, lb.Heightmap, merged); - if (_wbSpawnAdapter is not null) - _wbSpawnAdapter.OnLandblockLoaded(_loaded[canonical]); - - // C.1.5b: fire DefaultScript for each promoted dat-hydrated entity. - // All entities arriving via this path are atlas-tier by construction - // (the promotion path streams in dat-static scenery + EnvCell statics - // + stabs per the method's class doc-comment). - if (_entityScriptActivator is not null) - { - for (int i = 0; i < entities.Count; i++) - _entityScriptActivator.OnCreate(entities[i]); - } - - RebuildFlatView(); - } - private void RebuildFlatView() { _flatEntities = _loaded.Values.SelectMany(lb => lb.Entities).ToArray(); diff --git a/src/AcDream.App/Streaming/LandblockStreamJob.cs b/src/AcDream.App/Streaming/LandblockStreamJob.cs index dfc837d..aff6500 100644 --- a/src/AcDream.App/Streaming/LandblockStreamJob.cs +++ b/src/AcDream.App/Streaming/LandblockStreamJob.cs @@ -1,5 +1,3 @@ -using System.Collections.Generic; -using AcDream.Core.Terrain; using AcDream.Core.World; namespace AcDream.App.Streaming; @@ -12,7 +10,7 @@ namespace AcDream.App.Streaming; /// public abstract record LandblockStreamJob(uint LandblockId) { - public sealed record Load(uint LandblockId, LandblockStreamJobKind Kind) : LandblockStreamJob(LandblockId); + public sealed record Load(uint LandblockId) : LandblockStreamJob(LandblockId); public sealed record Unload(uint LandblockId) : LandblockStreamJob(LandblockId); } @@ -24,29 +22,7 @@ public abstract record LandblockStreamJob(uint LandblockId) /// public abstract record LandblockStreamResult(uint LandblockId) { - /// - /// A landblock load completed. distinguishes Far - /// (terrain only) from Near (terrain + entities). - /// is built off the render thread on the streaming worker. - /// - public sealed record Loaded( - uint LandblockId, - LandblockStreamTier Tier, - LoadedLandblock Landblock, - LandblockMeshData MeshData - ) : LandblockStreamResult(LandblockId); - - /// - /// A previously-Far-resident landblock was promoted to Near. Terrain - /// mesh is already on the GPU; the result carries the entity layer - /// (stabs, buildings, scenery) to merge into the existing GpuWorldState - /// entry. - /// - public sealed record Promoted( - uint LandblockId, - IReadOnlyList Entities - ) : LandblockStreamResult(LandblockId); - + public sealed record Loaded(uint LandblockId, LoadedLandblock Landblock) : LandblockStreamResult(LandblockId); public sealed record Failed(uint LandblockId, string Error) : LandblockStreamResult(LandblockId); public sealed record Unloaded(uint LandblockId) : LandblockStreamResult(LandblockId); diff --git a/src/AcDream.App/Streaming/LandblockStreamTier.cs b/src/AcDream.App/Streaming/LandblockStreamTier.cs deleted file mode 100644 index c4a9e5d..0000000 --- a/src/AcDream.App/Streaming/LandblockStreamTier.cs +++ /dev/null @@ -1,28 +0,0 @@ -namespace AcDream.App.Streaming; - -/// -/// Streaming-tier classification for a landblock. means -/// terrain mesh only; means terrain + scenery + EnvCells + -/// entity registration with the WB dispatcher. Per Phase A.5 spec Β§3. -/// -public enum LandblockStreamTier -{ - Far, - Near, -} - -/// -/// What work the streaming worker should perform for a given job. Distinct -/// from because -/// reads only the entity layer (terrain mesh already loaded), while -/// reads everything from scratch. Per Phase A.5 spec Β§4.3. -/// -public enum LandblockStreamJobKind -{ - /// Read LandBlock heightmap, build mesh, no entity layer. - LoadFar, - /// Read LandBlock + LandBlockInfo, generate scenery, build mesh, full entity layer. - LoadNear, - /// Read LandBlockInfo + scenery only β€” terrain already loaded for this LB. - PromoteToNear, -} diff --git a/src/AcDream.App/Streaming/LandblockStreamer.cs b/src/AcDream.App/Streaming/LandblockStreamer.cs index f71e0c0..fff7fc6 100644 --- a/src/AcDream.App/Streaming/LandblockStreamer.cs +++ b/src/AcDream.App/Streaming/LandblockStreamer.cs @@ -8,27 +8,28 @@ using AcDream.Core.World; namespace AcDream.App.Streaming; /// -/// Services landblock load/unload requests by invoking caller-supplied -/// factory delegates (the production instance wraps -/// for loading and -/// for the terrain -/// mesh) and posting results to an outbox the render thread drains once -/// per OnUpdate. +/// Services landblock load/unload requests by invoking a caller-supplied +/// load delegate (the production instance wraps +/// ) and posting results to an outbox +/// the render thread drains once per OnUpdate. /// /// -/// Thread model (Phase A.5 T11+): spawns a -/// dedicated background worker thread. and -/// write non-blocking to the inbox -/// ; the worker drains it and posts -/// records to the outbox. +/// Currently runs synchronously on the calling thread. The original +/// Phase A.1 design ran loads on a dedicated worker thread, but DatReaderWriter's +/// DatCollection is not thread-safe β€” concurrent reads from a worker +/// and the render thread (animation tick, live spawn handlers) corrupt +/// internal buffer state and produce half-populated LandBlock.Height[] +/// arrays which render as wildly distorted terrain. Until Phase A.3 introduces +/// a thread-safe dat wrapper, loads are synchronous: +/// invokes the load delegate inline and writes the result to the outbox in +/// a single call. This causes a frame hitch when crossing landblock +/// boundaries, but the rendering is correct. /// /// /// -/// DatCollection thread safety is provided by the caller: -/// GameWindow's _datLock (Phase A.5 T10) serialises all -/// DatCollection.Get<T> calls. Both factory closures passed at -/// construction acquire that lock before reading dats. The worker never -/// touches DatCollection directly β€” it only calls the factories. +/// The Channel-based outbox + API is +/// preserved so the move back to async loading is a single-class change +/// when DatCollection thread safety lands. /// /// /// @@ -38,9 +39,8 @@ namespace AcDream.App.Streaming; /// /// /// -/// Threading: must be called from a single -/// consumer thread (the render thread in production). All other public -/// methods are thread-safe. +/// Threading: synchronous mode means all methods must be called from the +/// same thread (the render thread in production). /// /// public sealed class LandblockStreamer : IDisposable @@ -52,93 +52,50 @@ public sealed class LandblockStreamer : IDisposable /// public const int DefaultDrainBatchSize = 4; - private readonly Func _loadLandblock; - private readonly Func _buildMeshOrNull; + private readonly Func _loadLandblock; private readonly Channel _inbox; private readonly Channel _outbox; private readonly CancellationTokenSource _cancel = new(); +#pragma warning disable CS0649 // _worker stays declared for the future async path; unused in synchronous mode. private Thread? _worker; +#pragma warning restore CS0649 private int _disposed; - /// - /// Primary ctor β€” the factory takes the job's - /// so it can branch on far-tier vs near-tier and skip entity hydration on far-tier - /// loads (heightmap-only). See ISSUE #54: prior to this signature the worker always - /// called the full-load path and stripped entities at the output, wasting per-LB - /// LandBlockInfo + SceneryGenerator work. - /// - public LandblockStreamer( - Func loadLandblock, - Func? buildMeshOrNull = null) + public LandblockStreamer(Func loadLandblock) { _loadLandblock = loadLandblock; - // Default: no mesh build (returns null β†’ Failed result). Production - // wires in LandblockMesh.Build via the T12 construction site. - _buildMeshOrNull = buildMeshOrNull ?? ((_, _) => null); - _inbox = Channel.CreateUnbounded( + _inbox = Channel.CreateUnbounded( new UnboundedChannelOptions { SingleReader = true, SingleWriter = false }); _outbox = Channel.CreateUnbounded( new UnboundedChannelOptions { SingleReader = true, SingleWriter = true }); } /// - /// Back-compat overload β€” wraps a kind-agnostic factory so existing test code - /// that doesn't care about the JobKind branch keeps compiling. The wrapper - /// ignores the kind and calls the factory once per LB regardless of tier. - /// New production code should use the primary 2-arg ctor. - /// - public LandblockStreamer( - Func loadLandblock, - Func? buildMeshOrNull = null) - : this((id, _) => loadLandblock(id), buildMeshOrNull) - { - } - - /// - /// Activate the dedicated background worker thread. Idempotent and - /// thread-safe: concurrent callers will only spawn one worker; subsequent - /// calls are no-ops. Atomic via . + /// No-op in synchronous mode. Preserved on the API surface so callers + /// don't need to change when async loading returns in Phase A.3. /// public void Start() { if (System.Threading.Volatile.Read(ref _disposed) != 0) throw new ObjectDisposedException(nameof(LandblockStreamer)); - - // A.5 T10-T12 follow-up: atomically install the worker so concurrent - // Start() callers don't both pass the null check and spawn duplicate - // threads. Construct the candidate; CAS it into _worker; if we lost - // the race, the candidate goes unstarted and is GCed. - var candidate = new Thread(WorkerLoop) - { - IsBackground = true, - Name = "acdream.streaming.worker", - }; - if (Interlocked.CompareExchange(ref _worker, candidate, null) == null) - candidate.Start(); - // else: another caller won the race; their thread is running. + // No worker thread in synchronous mode. } - /// - /// Non-blocking enqueue. The worker drains the inbox and posts a - /// (or - /// ) to the outbox. - /// - public void EnqueueLoad(uint landblockId, LandblockStreamJobKind kind = LandblockStreamJobKind.LoadNear) + public void EnqueueLoad(uint landblockId) { if (System.Threading.Volatile.Read(ref _disposed) != 0) throw new ObjectDisposedException(nameof(LandblockStreamer)); - _inbox.Writer.TryWrite(new LandblockStreamJob.Load(landblockId, kind)); + // Synchronous mode: invoke the load delegate inline. The result lands + // in the outbox and DrainCompletions picks it up later in the same + // (or next) frame. + HandleJob(new LandblockStreamJob.Load(landblockId)); } - /// - /// Non-blocking enqueue. The worker posts a - /// to the outbox. - /// public void EnqueueUnload(uint landblockId) { if (System.Threading.Volatile.Read(ref _disposed) != 0) throw new ObjectDisposedException(nameof(LandblockStreamer)); - _inbox.Writer.TryWrite(new LandblockStreamJob.Unload(landblockId)); + HandleJob(new LandblockStreamJob.Unload(landblockId)); } /// @@ -161,14 +118,17 @@ public sealed class LandblockStreamer : IDisposable { try { - // Safe to block: this is a dedicated worker thread with no - // SynchronizationContext, so .Result/.GetResult cannot deadlock - // against any captured continuation. Using the sync pattern - // here keeps the loop linear; an async-enumerable alternative - // would force WorkerLoop to be async Task and lose the - // simple thread-start shape. + // Synchronous read loop via .WaitToReadAsync + ReadAllAsync + // would be idiomatic but requires async; the blocking reader + // is simpler and the thread is dedicated anyway. while (!_cancel.Token.IsCancellationRequested) { + // Safe to block: this is a dedicated worker thread with no + // SynchronizationContext, so .Result/.GetResult cannot deadlock + // against any captured continuation. Using the sync pattern + // here keeps the loop linear; an async-enumerable alternative + // would force WorkerLoop to be async Task and lose the + // simple thread-start shape. if (!_inbox.Reader.WaitToReadAsync(_cancel.Token).AsTask().GetAwaiter().GetResult()) break; @@ -197,44 +157,15 @@ public sealed class LandblockStreamer : IDisposable switch (job) { case LandblockStreamJob.Load load: - // ISSUE #54 (post-A.5): JobKind is now plumbed through to the - // factory, so far-tier loads can skip LandBlockInfo + scenery - // + interior hydration on the worker thread (heightmap-only). - // The post-load entity-strip below is retained as a Debug - // assertion + Release safety net for the case where a buggy - // factory returns far-tier with entities anyway. try { - var lb = _loadLandblock(load.LandblockId, load.Kind); + var lb = _loadLandblock(load.LandblockId); if (lb is null) - { _outbox.Writer.TryWrite(new LandblockStreamResult.Failed( load.LandblockId, "LandblockLoader.Load returned null")); - break; - } - var mesh = _buildMeshOrNull(load.LandblockId, lb); - if (mesh is null) - { - _outbox.Writer.TryWrite(new LandblockStreamResult.Failed( - load.LandblockId, "buildMeshOrNull returned null")); - break; - } - var tier = load.Kind == LandblockStreamJobKind.LoadFar - ? LandblockStreamTier.Far : LandblockStreamTier.Near; - if (tier == LandblockStreamTier.Far && lb.Entities.Count > 0) - { - // Belt-and-suspenders: factory should have skipped - // entity hydration for LoadFar. If it didn't, fail - // loud in Debug builds and strip in Release. - System.Diagnostics.Debug.Assert(false, - $"Far-tier factory should skip entity hydration; got {lb.Entities.Count} entities for LB 0x{load.LandblockId:X8}"); - lb = new LoadedLandblock( - lb.LandblockId, - lb.Heightmap, - System.Array.Empty()); - } - _outbox.Writer.TryWrite(new LandblockStreamResult.Loaded( - load.LandblockId, tier, lb, mesh)); + else + _outbox.Writer.TryWrite(new LandblockStreamResult.Loaded( + load.LandblockId, lb)); } catch (Exception ex) { diff --git a/src/AcDream.App/Streaming/StreamingController.cs b/src/AcDream.App/Streaming/StreamingController.cs index ac74ae6..67ed631 100644 --- a/src/AcDream.App/Streaming/StreamingController.cs +++ b/src/AcDream.App/Streaming/StreamingController.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using AcDream.Core.Terrain; using AcDream.Core.World; namespace AcDream.App.Streaming; @@ -17,33 +16,15 @@ namespace AcDream.App.Streaming; /// public sealed class StreamingController { - private readonly Action _enqueueLoad; + private readonly Action _enqueueLoad; private readonly Action _enqueueUnload; private readonly Func> _drainCompletions; - private readonly Action _applyTerrain; + private readonly Action _applyTerrain; private readonly Action? _removeTerrain; private readonly GpuWorldState _state; private StreamingRegion? _region; - /// - /// Near-tier radius (LBs from observer that load full detail: terrain + - /// scenery + entities). Set at construction; readable thereafter. - /// - /// - /// Mutating after the first has no effect β€” the - /// internal snapshots both radii on its - /// constructor. Treat as init-only post-Tick. - /// - public int NearRadius { get; } - - /// - /// Far-tier radius (LBs from observer that load terrain only). Set at - /// construction; readable thereafter. - /// - /// - /// Mutating after the first has no effect β€” see . - /// - public int FarRadius { get; } + public int Radius { get; set; } /// /// Cap on completions drained per call. The cap is @@ -64,13 +45,12 @@ public sealed class StreamingController public int MaxCompletionsPerFrame { get; set; } = 4; public StreamingController( - Action enqueueLoad, + Action enqueueLoad, Action enqueueUnload, Func> drainCompletions, - Action applyTerrain, + Action applyTerrain, GpuWorldState state, - int nearRadius, - int farRadius, + int radius, Action? removeTerrain = null) { _enqueueLoad = enqueueLoad; @@ -79,42 +59,29 @@ public sealed class StreamingController _applyTerrain = applyTerrain; _removeTerrain = removeTerrain; _state = state; - NearRadius = nearRadius; - FarRadius = farRadius; + Radius = radius; } /// /// Advance one frame. / /// are landblock coordinates (0..255) of the current viewer β€” the camera /// in offline mode, the server-sent player position in live. - /// - /// Two-tier model (Phase A.5 T13): - /// - /// β†’ enqueue LoadFar (terrain only, no entities) - /// β†’ enqueue LoadNear (terrain + entities) - /// β†’ enqueue PromoteToNear (entity layer for already-loaded terrain) - /// β†’ drop entities on render thread immediately (terrain stays) - /// β†’ enqueue full unload - /// /// public void Tick(int observerCx, int observerCy) { + // First-tick bootstrap: no region yet, so the whole visible window + // is a load diff. if (_region is null) { - _region = new StreamingRegion(observerCx, observerCy, NearRadius, FarRadius); - var bootstrap = _region.ComputeFirstTickDiff(); - foreach (var id in bootstrap.ToLoadFar) _enqueueLoad(id, LandblockStreamJobKind.LoadFar); - foreach (var id in bootstrap.ToLoadNear) _enqueueLoad(id, LandblockStreamJobKind.LoadNear); - _region.MarkResidentFromBootstrap(); + _region = new StreamingRegion(observerCx, observerCy, Radius); + foreach (var id in _region.Visible) + _enqueueLoad(id); } else if (_region.CenterX != observerCx || _region.CenterY != observerCy) { var diff = _region.RecenterTo(observerCx, observerCy); - foreach (var id in diff.ToLoadFar) _enqueueLoad(id, LandblockStreamJobKind.LoadFar); - foreach (var id in diff.ToLoadNear) _enqueueLoad(id, LandblockStreamJobKind.LoadNear); - foreach (var id in diff.ToPromote) _enqueueLoad(id, LandblockStreamJobKind.PromoteToNear); - foreach (var id in diff.ToDemote) _state.RemoveEntitiesFromLandblock(id); - foreach (var id in diff.ToUnload) _enqueueUnload(id); + foreach (var id in diff.ToLoad) _enqueueLoad(id); + foreach (var id in diff.ToUnload) _enqueueUnload(id); } // Drain up to N completions per frame so a big diff doesn't spike @@ -125,12 +92,9 @@ public sealed class StreamingController switch (result) { case LandblockStreamResult.Loaded loaded: - _applyTerrain(loaded.Landblock, loaded.MeshData); + _applyTerrain(loaded.Landblock); _state.AddLandblock(loaded.Landblock); break; - case LandblockStreamResult.Promoted promoted: - _state.AddEntitiesToExistingLandblock(promoted.LandblockId, promoted.Entities); - break; case LandblockStreamResult.Unloaded unloaded: _state.RemoveLandblock(unloaded.LandblockId); _removeTerrain?.Invoke(unloaded.LandblockId); diff --git a/src/AcDream.App/Streaming/StreamingRegion.cs b/src/AcDream.App/Streaming/StreamingRegion.cs index 01eb85d..b28b547 100644 --- a/src/AcDream.App/Streaming/StreamingRegion.cs +++ b/src/AcDream.App/Streaming/StreamingRegion.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; namespace AcDream.App.Streaming; @@ -11,11 +10,9 @@ namespace AcDream.App.Streaming; /// public sealed class StreamingRegion { - public int CenterX { get; private set; } - public int CenterY { get; private set; } - public int Radius { get; } - public int NearRadius { get; } - public int FarRadius { get; } + public int CenterX { get; private set; } + public int CenterY { get; private set; } + public int Radius { get; } // Strictly the (2r+1)Γ—(2r+1) window (clamped to world bounds). private readonly HashSet _visible = new(); @@ -23,16 +20,6 @@ public sealed class StreamingRegion // Everything currently loaded: window + hysteresis-retained landblocks. private readonly HashSet _resident = new(); - // Two-tier residence tracking: maps each resident LB to its current tier. - private readonly Dictionary _tierResidence = new(); - - // Set true after MarkResidentFromBootstrap. The two-tier RecenterTo - // requires this state to be seeded; calling RecenterTo before the - // bootstrap silently emits the entire window as fresh loads (no demotes, - // no unloads, since _tierResidence is empty), which is a correctness - // hazard. The flag converts that into a loud InvalidOperationException. - private bool _bootstrapped; - /// /// Landblock IDs in the current visible window in the AC 8.8 coordinate /// form: (lbX << 24) | (lbY << 16) | 0xFFFF. The trailing @@ -56,16 +43,12 @@ public sealed class StreamingRegion /// public IReadOnlyCollection Resident => _resident; - public StreamingRegion(int centerX, int centerY, int nearRadius, int farRadius) + public StreamingRegion(int cx, int cy, int radius) { - NearRadius = nearRadius; - FarRadius = farRadius; - Radius = farRadius; // outer ring drives Resident bookkeeping - Recenter(centerX, centerY); + Radius = radius; + Recenter(cx, cy); } - public StreamingRegion(int cx, int cy, int radius) : this(cx, cy, radius, radius) { } - private void Recenter(int cx, int cy) { CenterX = cx; @@ -98,197 +81,13 @@ public sealed class StreamingRegion internal static uint EncodeLandblockId(int lbX, int lbY) => ((uint)lbX << 24) | ((uint)lbY << 16) | 0xFFFFu; - /// - /// First-tick bootstrap: emit ToLoadNear for every LB in the inner ring, - /// ToLoadFar for every LB in the outer ring (between near and far). Used - /// by on the first call before any - /// RecenterTo. - /// - public TwoTierDiff ComputeFirstTickDiff() - { - var near = new List(); - var far = new List(); - for (int dx = -FarRadius; dx <= FarRadius; dx++) - { - for (int dy = -FarRadius; dy <= FarRadius; dy++) - { - int nx = CenterX + dx; - int ny = CenterY + dy; - if (nx < 0 || nx > 0xFF || ny < 0 || ny > 0xFF) continue; - int absDx = System.Math.Abs(dx); - int absDy = System.Math.Abs(dy); - var id = EncodeLandblockId(nx, ny); - if (absDx <= NearRadius && absDy <= NearRadius) - near.Add(id); - else - far.Add(id); - } - } - return new TwoTierDiff( - ToLoadFar: far, - ToLoadNear: near, - ToPromote: System.Array.Empty(), - ToDemote: System.Array.Empty(), - ToUnload: System.Array.Empty()); - } - - /// - /// Call once after to seed - /// _tierResidence with the initial window. Every LB in the inner - /// ring (Chebyshev ≀ NearRadius) is marked Near; everything else Far. - /// - public void MarkResidentFromBootstrap() - { - if (_bootstrapped) - throw new InvalidOperationException( - "MarkResidentFromBootstrap was already called; calling it again would " + - "reset accumulated tier-residence state and silently drop differential " + - "data built up by interim RecenterTo calls."); - - _tierResidence.Clear(); - for (int dx = -FarRadius; dx <= FarRadius; dx++) - { - for (int dy = -FarRadius; dy <= FarRadius; dy++) - { - int nx = CenterX + dx; - int ny = CenterY + dy; - if (nx < 0 || nx > 0xFF || ny < 0 || ny > 0xFF) continue; - int absDx = Math.Abs(dx); - int absDy = Math.Abs(dy); - var id = EncodeLandblockId(nx, ny); - _tierResidence[id] = (absDx <= NearRadius && absDy <= NearRadius) - ? TierResidence.Near - : TierResidence.Far; - } - } - _bootstrapped = true; - } - - /// - /// Test-visible wrapper around so test - /// assemblies can build expected IDs without duplicating the encoding rule. - /// - internal static uint EncodeLandblockIdForTest(int lbX, int lbY) - => EncodeLandblockId(lbX, lbY); - - /// - /// Two-tier recenter: computes the 5-list diff per Phase A.5 spec Β§4.2. - /// Hysteresis: NearRadius+2 for Nearβ†’Far demote; FarRadius+2 for Farβ†’null - /// unload. Requires (or a prior - /// call to this method) to have seeded _tierResidence. - /// - public TwoTierDiff RecenterTo(int newCx, int newCy) - { - if (!_bootstrapped) - throw new InvalidOperationException( - "Two-tier RecenterTo called before MarkResidentFromBootstrap. " + - "First call ComputeFirstTickDiff to enqueue the bootstrap loads, " + - "then MarkResidentFromBootstrap to seed _tierResidence, then RecenterTo " + - "for subsequent observer moves."); - - int nearUnloadThreshold = NearRadius + 2; - int farUnloadThreshold = FarRadius + 2; - - var toLoadFar = new List(); - var toLoadNear = new List(); - var toPromote = new List(); - var toDemote = new List(); - var toUnload = new List(); - - // Pass 1: walk new far window β€” emit ToLoadFar / ToLoadNear / ToPromote. - var newCenterIds = new HashSet(); - for (int dx = -FarRadius; dx <= FarRadius; dx++) - { - for (int dy = -FarRadius; dy <= FarRadius; dy++) - { - int nx = newCx + dx; - int ny = newCy + dy; - if (nx < 0 || nx > 0xFF || ny < 0 || ny > 0xFF) continue; - int absDx = Math.Abs(dx); - int absDy = Math.Abs(dy); - bool inNear = absDx <= NearRadius && absDy <= NearRadius; - var id = EncodeLandblockId(nx, ny); - newCenterIds.Add(id); - - if (!_tierResidence.TryGetValue(id, out var current)) - { - // Not resident at all β€” fresh load. - if (inNear) toLoadNear.Add(id); - else toLoadFar.Add(id); - _tierResidence[id] = inNear ? TierResidence.Near : TierResidence.Far; - } - else if (current == TierResidence.Far && inNear) - { - // Was Far, now inside Near ring β€” promote. - toPromote.Add(id); - _tierResidence[id] = TierResidence.Near; - } - // Nearβ†’Near and Farβ†’Far are no-ops. - } - } - - // Pass 2: check previously-resident LBs for demote / unload. - foreach (var kvp in _tierResidence.ToArray()) - { - var id = kvp.Key; - var current = kvp.Value; - int lbX = (int)((id >> 24) & 0xFFu); - int lbY = (int)((id >> 16) & 0xFFu); - int absDx = Math.Abs(lbX - newCx); - int absDy = Math.Abs(lbY - newCy); - int distance = Math.Max(absDx, absDy); // Chebyshev - - if (newCenterIds.Contains(id)) - { - // Still in the far window β€” only Nearβ†’Far demote possible here. - if (current == TierResidence.Near && (absDx > NearRadius || absDy > NearRadius)) - { - if (distance > nearUnloadThreshold) - { - toDemote.Add(id); - _tierResidence[id] = TierResidence.Far; - } - } - continue; - } - - // Outside the new window β€” demote / unload by threshold. - if (current == TierResidence.Near) - { - if (distance > nearUnloadThreshold) - { - toDemote.Add(id); - _tierResidence[id] = TierResidence.Far; - if (distance > farUnloadThreshold) - { - toUnload.Add(id); - _tierResidence.Remove(id); - } - } - } - else if (current == TierResidence.Far) - { - if (distance > farUnloadThreshold) - { - toUnload.Add(id); - _tierResidence.Remove(id); - } - } - } - - CenterX = newCx; - CenterY = newCy; - - return new TwoTierDiff(toLoadFar, toLoadNear, toPromote, toDemote, toUnload); - } - /// /// Recompute the visible window around a new center and return the /// delta vs. the previous state. Hysteresis: landblocks aren't unloaded /// until they're further than Radius + 2 from the new center, /// so boundary crossings don't thrash. /// - public RegionDiff RecenterToSingleTier(int newCx, int newCy) + public RegionDiff RecenterTo(int newCx, int newCy) { // Snapshot the old resident set so we can diff against it. var oldResident = new HashSet(_resident); @@ -327,7 +126,7 @@ public sealed class StreamingRegion } /// -/// Output of : the landblocks to +/// Output of : the landblocks to /// start loading (newly entered the visible window) and the landblocks to /// unload (fell outside the unload threshold, which is Radius + 2). /// Both lists are disjoint from the current @@ -336,10 +135,3 @@ public sealed class StreamingRegion public readonly record struct RegionDiff( IReadOnlyList ToLoad, IReadOnlyList ToUnload); - -/// -/// Tracks which tier a landblock currently occupies in the two-tier streaming -/// model. Absence from the dictionary encodes "not resident"; the enum has no -/// None member to avoid suggesting a third runtime state. -/// -internal enum TierResidence { Far, Near } diff --git a/src/AcDream.App/Streaming/TwoTierDiff.cs b/src/AcDream.App/Streaming/TwoTierDiff.cs deleted file mode 100644 index 2a24dab..0000000 --- a/src/AcDream.App/Streaming/TwoTierDiff.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System.Collections.Generic; - -namespace AcDream.App.Streaming; - -/// -/// Output of for the two-tier model. -/// Five disjoint lists describe what changed since the previous Tick. Per -/// Phase A.5 spec Β§4.2. -/// -public readonly record struct TwoTierDiff( - IReadOnlyList ToLoadFar, // entered far window from null (terrain only) - IReadOnlyList ToLoadNear, // entered near window from null (terrain + entities β€” first-tick or teleport) - IReadOnlyList ToPromote, // entered near window from far-resident (entities only) - IReadOnlyList ToDemote, // exited near window past hysteresis (drop entities) - IReadOnlyList ToUnload); // exited far window past hysteresis (drop terrain) diff --git a/src/AcDream.App/UI/TargetIndicatorPanel.cs b/src/AcDream.App/UI/TargetIndicatorPanel.cs deleted file mode 100644 index c65d249..0000000 --- a/src/AcDream.App/UI/TargetIndicatorPanel.cs +++ /dev/null @@ -1,395 +0,0 @@ -using System; -using System.Numerics; -using AcDream.Core.Ui; -using ImGuiNET; - -namespace AcDream.App.UI; - -/// -/// B.7 (2026-05-15) β€” Vivid Target Indicator. Draws four small -/// corner triangles around the currently-selected entity, colour-coded -/// by entity type (NPCs yellow, items white-ish, PKs red, etc.). -/// Retail-faithful equivalent of VividTargetIndicator -/// (named decomp at 0x004d6165 / 0x004f5ce0). -/// -/// -/// MVP scope: on-screen indicator only, drawn via ImGui's background -/// draw list. Deferred to follow-ups: off-screen edge arrow, DAT-loaded -/// triangle sprite, mesh-tint highlight, player-option toggle. -/// -/// -/// -/// The panel pulls its inputs through delegates supplied by the host -/// () so it doesn't have to depend -/// on internal state types: -/// -/// -/// selectedGuidProvider β€” host's current _selectedGuid. -/// entityResolver β€” returns -/// for a given guid, or null if -/// the entity is no longer in the world (despawned). -/// cameraProvider β€” host's active camera + viewport -/// dimensions; called once per frame. -/// -/// -public sealed class TargetIndicatorPanel -{ - /// - /// What the panel needs to know about the selected entity per frame. - /// ItemType + ObjectDescriptionFlags feed - /// for colour selection. - /// Scale multiplies the per-type base height in - /// β€” a scaled-up sign or oversized NPC - /// gets a proportionally bigger box. Useability (acclient.h:6478 - /// ITEM_USEABLE enum) discriminates real pickup items - /// (USEABLE_REMOTE bit set, 0.8 m boxes) from same-ItemType-but-non- - /// useable scenery like signs (USEABLE_UNDEF, 3 m boxes). - /// - public readonly record struct TargetInfo( - Vector3 WorldPosition, - uint ItemType, - uint ObjectDescriptionFlags, - float Scale, - uint? Useability = null, - // 2026-05-16: world-space SelectionSphere center + radius. - // Comes from the Setup's baked selection_sphere (acclient.h - // CSetup::selection_sphere) scaled by entity scale. When - // populated, the panel projects the sphere as a screen circle - // and uses that as the indicator rect β€” matches retail - // SmartBox::GetObjectBoundingBox (decomp 0x00452e20). When - // null, the panel falls back to the per-type height heuristic. - Vector3? WorldSphereCenter = null, - float? WorldSphereRadius = null); - - private readonly Func _selectedGuidProvider; - private readonly Func _entityResolver; - private readonly Func<(Matrix4x4 View, Matrix4x4 Projection, Vector2 Viewport)> _cameraProvider; - - /// - /// Pixel size of each corner triangle's right-angle legs. - /// Retail uses UIRegion::GetWidth(m_rgOnScreenCorners.m_data[1]) - /// of the triangle sprite (decomp 0x004f69c8). The retail - /// sprite is small β€” ~8 px legs. 14 was too chunky per user - /// feedback on 2026-05-16; 8 matches the retail screenshot. - /// - public float TriangleSize { get; set; } = 8f; - - /// - /// World-space height of the indicator box for entities that don't - /// have a more specific type tag. Items use a smaller value (see - /// ). 1.8 m matches a standing humanoid; - /// short items still get a small box because the projection - /// preserves apparent size. - /// - public float EntityHeight { get; set; } = 1.8f; - - /// - /// Resolve the world-space height to use for a given entity's - /// indicator box. The base height per type is multiplied by the - /// entity's so an upscaled sign or NPC - /// gets a proportionally bigger box. - /// - /// Per-type base height: - /// - /// Creature (NPC / monster / player): 1.8 m (humanoid) - /// Door / Lifestone / Portal: 2.4 m (door-frame tall) - /// Small carry items (Money, Food, Gem, SpellComponents, - /// Misc, Weapons, Armour, Clothing, Jewelry, Container): - /// 0.8 m (item dropped on the ground) - /// Everything else (signs on a pole, generic tall scenery, - /// untyped scenery interactables): 3.0 m (post-on-ground - /// tall β€” bumped from 1.5 m on 2026-05-15 because the - /// Holtburg sign was getting a tiny pole-only box. Most - /// non-typed non-flat AC scenery is either small-item-on- - /// ground (handled above) or post-mounted; 3 m is the - /// right midpoint for the post case. Scale > 1 grows - /// the box proportionally.) - /// - /// - /// - /// Future refinement (deferred): read the entity's actual mesh - /// AABB at registration time and use the projected silhouette - /// for an exact-fit box. - /// - /// already caches per-GfxObj AABBs; combining them across a - /// multi-part Setup gives the entity-level bounds we'd want. - /// - /// - public float EntityHeightFor(uint itemType, uint pwdBitfield, float scale, uint? useability = null) - { - if (scale <= 0f) scale = 1f; // defensive - bool isCreature = (itemType & (uint)AcDream.Core.Items.ItemType.Creature) != 0; - if (isCreature) return 1.8f * scale; - - // BF_DOOR = 0x1000, BF_LIFESTONE = 0x4000, BF_PORTAL = 0x40000, - // BF_CORPSE = 0x2000 (acclient.h:6431-6463). - const uint TallStructureMask = 0x1000u | 0x4000u | 0x40000u | 0x2000u; - if ((pwdBitfield & TallStructureMask) != 0) return 2.4f * scale; - - // 2026-05-15 β€” KEY DISCRIMINATOR. Misc-class ItemTypes are - // ambiguous in retail: dropped jewellery / coins / food / tapers - // are Misc, but so are signs, banners, and decorative scenery. - // ACE distinguishes the two via ITEM_USEABLE (acclient.h:6478): - // a real pickup item has USEABLE_REMOTE (0x20) set; a sign has - // USEABLE_UNDEF (0). If we know useability and it lacks - // USEABLE_REMOTE, treat the entity as tall scenery regardless - // of ItemType. This is what fixes the Holtburg town sign - // showing a tiny pole-base box. - const uint USEABLE_REMOTE_BIT = 0x20u; - bool useableFromWorld = useability is uint u - && (u & USEABLE_REMOTE_BIT) != 0; - - // Small carry items: weapons / armour / clothing / jewellery / - // money / food / misc / weapons / containers / gems / spell - // components / writable / keys / casters / lockables. - const uint SmallItemMask = - (uint)(AcDream.Core.Items.ItemType.MeleeWeapon - | AcDream.Core.Items.ItemType.Armor - | AcDream.Core.Items.ItemType.Clothing - | AcDream.Core.Items.ItemType.Jewelry - | AcDream.Core.Items.ItemType.Food - | AcDream.Core.Items.ItemType.Money - | AcDream.Core.Items.ItemType.Misc - | AcDream.Core.Items.ItemType.MissileWeapon - | AcDream.Core.Items.ItemType.Container - | AcDream.Core.Items.ItemType.Gem - | AcDream.Core.Items.ItemType.SpellComponents - | AcDream.Core.Items.ItemType.Writable - | AcDream.Core.Items.ItemType.Key - | AcDream.Core.Items.ItemType.Caster); - - // Real pickup item: ItemType is small-item-class AND the server - // marked it useable from the world. 0.8 m Γ— scale box. - if ((itemType & SmallItemMask) != 0 && useableFromWorld) return 0.8f * scale; - - // Tall scenery: anything else (signs, banners, untyped - // post-mounted objects, AND Misc-typed-but-non-useable entities - // like the Holtburg sign). 3 m Γ— scale covers a typical - // post-mounted sign from ground to top. - return 3.0f * scale; - } - - /// - /// Box width = projected height Γ— - /// . Retail's Vivid Target Indicator - /// draws a square box β€” four corner triangles arranged in a square β€” - /// so 1.0 = width matches height. The earlier 0.5 (humanoid-ish - /// aspect) made the box uncomfortably narrow for non-humanoids. - /// - public float WidthHeightRatio { get; set; } = 1.0f; - - /// - /// Floor for the projected screen height (pixels). Prevents the - /// indicator from collapsing to a point on far-away entities. - /// - public float MinScreenHeight { get; set; } = 16f; - - public TargetIndicatorPanel( - Func selectedGuidProvider, - Func entityResolver, - Func<(Matrix4x4 View, Matrix4x4 Projection, Vector2 Viewport)> cameraProvider) - { - _selectedGuidProvider = selectedGuidProvider; - _entityResolver = entityResolver; - _cameraProvider = cameraProvider; - } - - /// - /// Per-frame render call. No-op if nothing is selected, the selected - /// entity is gone, or the entity is off-screen / behind the camera. - /// Draws to the ImGui background draw list so it appears behind - /// other panels. - /// - public void Render() - { - if (_selectedGuidProvider() is not uint guid) return; - if (_entityResolver(guid) is not TargetInfo info) return; - - var (view, projection, viewport) = _cameraProvider(); - if (viewport.X <= 0 || viewport.Y <= 0) return; - - var viewProj = view * projection; - - Vector2 tl, tr, br, bl; - - if (info.WorldSphereCenter is Vector3 sphereCenter - && info.WorldSphereRadius is float sphereRadius - && TryComputeScreenRectFromSphere(sphereCenter, sphereRadius, view, projection, viewport, - out var rMin, out var rMax)) - { - // 2026-05-16 β€” retail-faithful path per - // SmartBox::GetObjectBoundingBox (decomp 0x00452e20). - // Retail uses CPhysicsObj::GetSelectionSphere (the Setup's - // baked selection_sphere) and produces the screen rect - // from that sphere's projection β€” NOT from a per-mesh AABB. - // - // Retail INFLATES the rect by one triangle width/height on - // every side before drawing (decomp 0x004f6a0b–0x004f6a99): - // edi_3 = arg4->left - eax_21 (shift left by triangleW) - // ebp_3 = arg4->top - eax_23 (shift up by triangleH) - // width = sphere_width + 2 * triangleW - // height = sphere_height + 2 * triangleH - // So the four corner triangles sit OUTSIDE the projected - // sphere by one triangle leg. - float ts = TriangleSize; - tl = new Vector2(rMin.X - ts, rMin.Y - ts); - tr = new Vector2(rMax.X + ts, rMin.Y - ts); - br = new Vector2(rMax.X + ts, rMax.Y + ts); - bl = new Vector2(rMin.X - ts, rMax.Y + ts); - } - else - { - // Fallback when the AABB isn't available (no setup cached - // yet, missing GfxObj bounds, behind the camera). Square - // box centred at the entity origin, height from the - // per-type heuristic. - if (!TryProjectToScreen(info.WorldPosition, viewProj, viewport, out var feetScreen)) - return; - float entityHeight = EntityHeightFor(info.ItemType, info.ObjectDescriptionFlags, info.Scale, info.Useability); - var headWorld = new Vector3( - info.WorldPosition.X, - info.WorldPosition.Y, - info.WorldPosition.Z + entityHeight); - if (!TryProjectToScreen(headWorld, viewProj, viewport, out var headScreen)) - return; - - float screenHeight = MathF.Abs(headScreen.Y - feetScreen.Y); - if (screenHeight < MinScreenHeight) screenHeight = MinScreenHeight; - float screenWidth = screenHeight * WidthHeightRatio; - - Vector2 center = (feetScreen + headScreen) * 0.5f; - float halfW = screenWidth * 0.5f; - float halfH = screenHeight * 0.5f; - - tl = new Vector2(center.X - halfW, center.Y - halfH); - tr = new Vector2(center.X + halfW, center.Y - halfH); - br = new Vector2(center.X + halfW, center.Y + halfH); - bl = new Vector2(center.X - halfW, center.Y + halfH); - } - - var rgba = RadarBlipColors.For(info.ItemType, info.ObjectDescriptionFlags); - uint col = MakeImGuiColor(rgba); - - var drawList = ImGui.GetBackgroundDrawList(); - - float t = TriangleSize; - - // 2026-05-16 β€” flipped per user feedback. Each corner triangle's - // RIGHT-ANGLE apex now points INWARD toward the target (was at - // the outer corner pointing outward). Combined with the - // TriangleSize inflate on the rect, the apex of each triangle - // lands at the projected mesh boundary while the hypotenuse - // runs across the outer (inflated) corner β€” giving the retail - // "corner-tick pointing at the entity" look. - // - // Geometry per corner: - // apex = corner + (Β±t, Β±t) ← inward, right-angle here - // leg_a end = corner + (Β±t, 0) ← along horizontal edge - // leg_b end = corner + (0, Β±t) ← along vertical edge - // Hypotenuse runs from leg_a end to leg_b end (the outer - // diagonal of the corner). - drawList.AddTriangleFilled(tl + new Vector2( t, t), tl + new Vector2( t, 0), tl + new Vector2(0, t), col); - drawList.AddTriangleFilled(tr + new Vector2(-t, t), tr + new Vector2(-t, 0), tr + new Vector2(0, t), col); - drawList.AddTriangleFilled(br + new Vector2(-t, -t), br + new Vector2(-t, 0), br + new Vector2(0, -t), col); - drawList.AddTriangleFilled(bl + new Vector2( t, -t), bl + new Vector2( t, 0), bl + new Vector2(0, -t), col); - } - - /// - /// 2026-05-16. Project a world-space sphere (center + radius) as a - /// screen-space square and return its bounding rectangle. Matches - /// retail SmartBox::GetObjectBoundingBox (decomp - /// 0x00452e20) which uses - /// Render::GetViewerBBox(sphere, &corner1, &corner2) - /// to compute a camera-aligned BBox of the sphere then projects - /// the 2 corner points. - /// - /// - /// Mathematical equivalent (faster, no per-corner reprojection): - /// project the sphere center to screen, then compute the - /// screen-space radius as - /// worldRadius * projection.M22 * viewport.Y / (2 * clip.W) - /// where M22 = 1/tan(fovY/2) for a standard right-handed - /// perspective. The rect is centered at the projected sphere - /// center with side length 2 * screenRadius. - /// - /// - /// - /// Returns false if the sphere center is behind the camera - /// (clip.W <= 0). - /// - /// - private static bool TryComputeScreenRectFromSphere( - Vector3 worldCenter, float worldRadius, - Matrix4x4 view, Matrix4x4 projection, Vector2 viewport, - out Vector2 rectMin, out Vector2 rectMax) - { - rectMin = default; - rectMax = default; - - var viewProj = view * projection; - var clip = Vector4.Transform(new Vector4(worldCenter, 1f), viewProj); - if (clip.W <= 0.001f) return false; - - float ndcX = clip.X / clip.W; - float ndcY = clip.Y / clip.W; - float screenX = (ndcX * 0.5f + 0.5f) * viewport.X; - float screenY = (1f - (ndcY * 0.5f + 0.5f)) * viewport.Y; - - // Screen-space radius. projection.M22 = cot(fovY/2). clip.W is - // the camera-space distance (positive in front of camera for - // standard right-handed perspective). - float scaleY = projection.M22; - if (scaleY <= 0f) return false; - float screenRadius = worldRadius * scaleY * viewport.Y / (2f * clip.W); - - // Cull obviously-off-screen entities (more than a screen away). - if (screenX + screenRadius < -viewport.X || screenX - screenRadius > 2f * viewport.X) return false; - if (screenY + screenRadius < -viewport.Y || screenY - screenRadius > 2f * viewport.Y) return false; - - // Floor at MinSide so distant entities still get a visible indicator. - const float MinSide = 12f; - if (screenRadius < MinSide * 0.5f) screenRadius = MinSide * 0.5f; - - rectMin = new Vector2(screenX - screenRadius, screenY - screenRadius); - rectMax = new Vector2(screenX + screenRadius, screenY + screenRadius); - return true; - } - - /// - /// Project a world-space point to screen-space pixels. Returns - /// false if the point is behind the camera or outside the - /// expanded viewport (Β±20 % margin so a tall entity whose feet are - /// just off the bottom of the screen still gets its head projected). - /// - private static bool TryProjectToScreen( - Vector3 world, - Matrix4x4 viewProj, - Vector2 viewport, - out Vector2 screen) - { - var clip = Vector4.Transform(new Vector4(world, 1f), viewProj); - if (clip.W <= 0.001f) - { - screen = Vector2.Zero; - return false; - } - float ndcX = clip.X / clip.W; - float ndcY = clip.Y / clip.W; - const float margin = 1.2f; - if (ndcX < -margin || ndcX > margin || ndcY < -margin || ndcY > margin) - { - screen = Vector2.Zero; - return false; - } - screen = new Vector2( - (ndcX * 0.5f + 0.5f) * viewport.X, - (1f - (ndcY * 0.5f + 0.5f)) * viewport.Y); - return true; - } - - private static uint MakeImGuiColor(RadarBlipColors.Rgba c) - { - // ImGui packed colour is 0xAABBGGRR (little-endian RGBA). - return ((uint)c.A << 24) | ((uint)c.B << 16) | ((uint)c.G << 8) | c.R; - } -} diff --git a/src/AcDream.Core.Net/GameEventWiring.cs b/src/AcDream.Core.Net/GameEventWiring.cs index c5f61e3..93fd62e 100644 --- a/src/AcDream.Core.Net/GameEventWiring.cs +++ b/src/AcDream.Core.Net/GameEventWiring.cs @@ -395,41 +395,6 @@ public static class GameEventWiring if (dumpPd) Console.WriteLine($"vitals: PD-ench spell={ench.SpellId} layer={ench.Layer} bucket={ench.Bucket} key={ench.StatModKey} val={ench.StatModValue}"); } - - // Issue #13 β€” register inventory entries with ItemRepository so - // panels (inventory, paperdoll, hotbars) light up after login. - // Equipped entries share the same ObjectId as inventory entries - // (an equipped item is also in inventory) β€” register both, but - // the equipped record carries the slot mask which we surface via - // MoveItem so paperdoll can render. - foreach (var inv in p.Value.Inventory) - { - if (items.GetItem(inv.Guid) is null) - { - items.AddOrUpdate(new ItemInstance - { - ObjectId = inv.Guid, - WeenieClassId = inv.ContainerType, - }); - } - } - foreach (var eq in p.Value.Equipped) - { - if (items.GetItem(eq.Guid) is null) - { - items.AddOrUpdate(new ItemInstance - { - ObjectId = eq.Guid, - WeenieClassId = 0, - }); - } - // Reflect the equip slot β€” paperdoll uses CurrentlyEquippedLocation. - items.MoveItem( - itemId: eq.Guid, - newContainerId: 0, - newSlot: -1, - newEquipLocation: (EquipMask)eq.EquipLocation); - } }); } } diff --git a/src/AcDream.Core.Net/Messages/CreateObject.cs b/src/AcDream.Core.Net/Messages/CreateObject.cs index 971c726..a5fcd7b 100644 --- a/src/AcDream.Core.Net/Messages/CreateObject.cs +++ b/src/AcDream.Core.Net/Messages/CreateObject.cs @@ -126,20 +126,7 @@ public static class CreateObject // weren't set; subscribers fall back to PhysicsBody constructor // defaults (0.05f elasticity, 0.5f friction). float? Friction = null, - float? Elasticity = null, - // 2026-05-15: optional WeenieHeader tail. The retail - // `ITEM_USEABLE _useability` (acclient.h:6478) β€” gates whether the - // R-key Use action does anything. (Useability & USEABLE_REMOTE - // (0x20)) != 0 means the entity is useable from the world via - // mouse Use. Signs / banners / decorative scenery have - // USEABLE_UNDEF (0x0) here β€” selecting them via left-click is - // fine, but R-key Use should be a no-op (retail-faithful: the - // character does not walk toward; nothing happens). - // UseRadius is the use-action's reach in meters; doubles as - // a sizing hint for selection indicators on entities that - // publish it. - uint? Useability = null, - float? UseRadius = null); + float? Elasticity = null); /// /// The relevant subset of the server-sent MovementData / @@ -483,10 +470,9 @@ public static class CreateObject // ObjectDescriptionFlags, align. string? name = null; uint? itemType = null; - uint weenieFlags = 0; if (body.Length - pos >= 4) { - weenieFlags = ReadU32(body, ref pos); + pos += 4; // skip weenieFlags u32 try { name = ReadString16L(body, ref pos); @@ -510,86 +496,11 @@ public static class CreateObject catch { /* truncated name β€” partial result is still useful */ } } - // --- WeenieHeader optional tail (2026-05-15): walk the - // conditional fields up through Useability + UseRadius. - // - // Wire order is fixed by ACE WorldObject_Networking.cs:87-114 - // and matches retail PWD::Pack order. We MUST skip every - // preceding optional field (even those we don't care about) - // because each one moves the parse cursor. - // - // Field bit width decoded? - // ------- ------ -------- -------- - // weenieFlags2 conditional on objDescFlags & 0x80000000 (BF_INCLUDES_SECOND_HEADER) - // u32 skipped - // PluralName 0x1 String16L (variable, padded to 4) skipped - // ItemCapacity 0x2 1 byte skipped - // ContainerCap 0x4 1 byte skipped - // AmmoType 0x100 u16 skipped - // Value 0x8 u32 skipped - // Useability 0x10 u32 KEPT - // UseRadius 0x20 f32 KEPT - // - // Wrapped in try/catch β€” if a malformed entity truncates the - // tail we still return the prefix fields. Most spawned entities - // either have all of these or none of them. - uint? useability = null; - float? useRadius = null; - try - { - // BF_INCLUDES_SECOND_HEADER = 0x04000000 per acclient.h:6458 - // (ACE ObjectDescriptionFlag.IncludesSecondHeader matches). - // Earlier code had this as 0x80000000 β€” wrong bit, so the - // weenieFlags2 4-byte skip never fired for entities that - // actually had it set, corrupting downstream optional-tail - // offsets. Now correct. - bool hasSecondHeader = objectDescriptionFlags.HasValue - && (objectDescriptionFlags.Value & 0x04000000u) != 0; - if (hasSecondHeader && body.Length - pos >= 4) pos += 4; // weenieFlags2 - - if ((weenieFlags & 0x00000001u) != 0) // PluralName - _ = ReadString16L(body, ref pos); - - if ((weenieFlags & 0x00000002u) != 0) // ItemCapacity - { - if (body.Length - pos < 1) throw new FormatException("trunc ItemCap"); - pos += 1; - } - if ((weenieFlags & 0x00000004u) != 0) // ContainerCapacity - { - if (body.Length - pos < 1) throw new FormatException("trunc ContCap"); - pos += 1; - } - if ((weenieFlags & 0x00000100u) != 0) // AmmoType u16 - { - if (body.Length - pos < 2) throw new FormatException("trunc AmmoType"); - pos += 2; - } - if ((weenieFlags & 0x00000008u) != 0) // Value u32 - { - if (body.Length - pos < 4) throw new FormatException("trunc Value"); - pos += 4; - } - if ((weenieFlags & 0x00000010u) != 0) // Useability u32 ← KEEP - { - if (body.Length - pos < 4) throw new FormatException("trunc Useability"); - useability = ReadU32(body, ref pos); - } - if ((weenieFlags & 0x00000020u) != 0) // UseRadius f32 ← KEEP - { - if (body.Length - pos < 4) throw new FormatException("trunc UseRadius"); - useRadius = BinaryPrimitives.ReadSingleLittleEndian(body.Slice(pos)); - pos += 4; - } - } - catch { /* truncated weenie tail β€” keep whatever we got. */ } - return new Parsed(guid, position, setupTableId, animParts, textureChanges, subPalettes, basePaletteId, objScale, name, itemType, motionState, motionTableId, instanceSeq, teleportSeq, serverControlSeq, forcePositionSeq, physicsState, objectDescriptionFlags, - friction, elasticity, - useability, useRadius); + friction, elasticity); // Local helper: if we ran out of fields past PhysicsData, still // return the useful prefix (guid/position/setup/animParts/textures/palettes/scale/motion). diff --git a/src/AcDream.Core.Net/Messages/InteractRequests.cs b/src/AcDream.Core.Net/Messages/InteractRequests.cs index 68b3b1e..d9cfe7b 100644 --- a/src/AcDream.Core.Net/Messages/InteractRequests.cs +++ b/src/AcDream.Core.Net/Messages/InteractRequests.cs @@ -29,7 +29,6 @@ public static class InteractRequests public const uint UseOpcode = 0x0036u; public const uint UseWithTargetOpcode = 0x0035u; public const uint TeleToLifestoneOpcode = 0x0063u; - public const uint PutItemInContainerOpcode = 0x0019u; /// /// Use an object: click a door, loot a corpse, talk to an NPC, @@ -74,36 +73,4 @@ public static class InteractRequests BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(8), TeleToLifestoneOpcode); return body; } - - /// - /// Pick up a ground item or move an item between containers. The - /// server places the item in at - /// the given slot (pass 0 to let the - /// server choose). For F-key ground-pickup, pass the player's own - /// server guid as . - /// - /// - /// Wire layout (ACE GameActionPutItemInContainer.Handle): - /// - /// u32 0xF7B1 - /// u32 gameActionSequence - /// u32 0x0019 // PutItemInContainer - /// u32 itemGuid // server guid of the item - /// u32 containerGuid // destination container (player or bag) - /// i32 placement // 0 = server picks slot - /// - /// - /// - public static byte[] BuildPickUp( - uint gameActionSequence, uint itemGuid, uint containerGuid, int placement) - { - byte[] body = new byte[24]; - BinaryPrimitives.WriteUInt32LittleEndian(body, GameActionEnvelope); - BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(4), gameActionSequence); - BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(8), PutItemInContainerOpcode); - BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(12), itemGuid); - BinaryPrimitives.WriteUInt32LittleEndian(body.AsSpan(16), containerGuid); - BinaryPrimitives.WriteInt32LittleEndian (body.AsSpan(20), placement); - return body; - } } diff --git a/src/AcDream.Core.Net/Messages/PickupEvent.cs b/src/AcDream.Core.Net/Messages/PickupEvent.cs deleted file mode 100644 index 44ff95d..0000000 --- a/src/AcDream.Core.Net/Messages/PickupEvent.cs +++ /dev/null @@ -1,48 +0,0 @@ -using System.Buffers.Binary; - -namespace AcDream.Core.Net.Messages; - -/// -/// Inbound PickupEvent GameMessage (opcode 0xF74A). -/// -/// -/// ACE emits this from Player_Tracking.RemoveTrackedObject(wo, fromPickup: true) -/// when a player picks up a world item β€” distinguishes the despawn -/// from a generic 0xF747 DeleteObject (timeout / death / -/// out-of-LOS). Downstream effect on the client view is the same -/// (remove the entity from the world), so -/// routes both opcodes to the same EntityDeleted event. -/// -/// -/// -/// Wire layout (ACE GameMessagePickupEvent.cs): -/// -/// u32 0xF74A -/// u32 guid -/// u16 objectInstanceSequence -/// u16 objectPositionSequence -/// -/// -/// -public static class PickupEvent -{ - public const uint Opcode = 0xF74Au; - - public readonly record struct Parsed( - uint Guid, ushort InstanceSequence, ushort PositionSequence); - - public static Parsed? TryParse(ReadOnlySpan body) - { - if (body.Length < 12) - return null; - - uint opcode = BinaryPrimitives.ReadUInt32LittleEndian(body.Slice(0, 4)); - if (opcode != Opcode) - return null; - - uint guid = BinaryPrimitives.ReadUInt32LittleEndian(body.Slice(4, 4)); - ushort instanceSequence = BinaryPrimitives.ReadUInt16LittleEndian(body.Slice(8, 2)); - ushort positionSequence = BinaryPrimitives.ReadUInt16LittleEndian(body.Slice(10, 2)); - return new Parsed(guid, instanceSequence, positionSequence); - } -} diff --git a/src/AcDream.Core.Net/Messages/PlayerDescriptionParser.cs b/src/AcDream.Core.Net/Messages/PlayerDescriptionParser.cs index 73cb9f4..406af15 100644 --- a/src/AcDream.Core.Net/Messages/PlayerDescriptionParser.cs +++ b/src/AcDream.Core.Net/Messages/PlayerDescriptionParser.cs @@ -177,63 +177,6 @@ public static class PlayerDescriptionParser Cooldown = 0x08, } - /// Bitmask of which optional trailer sections are present in - /// the PlayerDescription wire payload. Holtburger - /// events.rs:503-607; ACE CharacterOptionDataFlag. - [Flags] - public enum CharacterOptionDataFlag : uint - { - None = 0, - Shortcut = 0x00000001, - SquelchList = 0x00000002, - MultiSpellList = 0x00000004, - DesiredComps = 0x00000008, - ExtendedMultiSpellLists = 0x00000010, - SpellbookFilters = 0x00000020, - CharacterOptions2 = 0x00000040, - TimestampFormat = 0x00000080, - GenericQualitiesData = 0x00000100, - GameplayOptions = 0x00000200, - SpellLists8 = 0x00000400, - } - - /// One shortcut bar entry. 16 bytes wire size. - /// holtburger shortcuts.rs:13-34. Named ShortcutEntry - /// (not Shortcut) to avoid a homograph with the - /// flag bit, which is - /// referenced from the same scope as instances of this type in the - /// trailer walker. - public readonly record struct ShortcutEntry( - uint Index, - uint ObjectGuid, - ushort SpellId, - ushort Layer); - - /// One inventory entry β€” a guid plus a ContainerType - /// discriminator (0=NonContainer, 1=Container, 2=Foci). Holtburger - /// events.rs:143-168 validates ContainerType <= 2 - /// in unpack_inventory_and_equipped_strict. - public readonly record struct InventoryEntry( - uint Guid, - uint ContainerType); - - /// One equipped object entry. Holtburger - /// events.rs:180-190: (Guid guid, u32 loc, u32 prio). - /// is an EquipMask bitfield; - /// orders overlapping equips in the - /// same slot. - public readonly record struct EquippedEntry( - uint Guid, - uint EquipLocation, - uint Priority); - - /// Result of . Trailer fields - /// (OptionFlags through Equipped) may be partially - /// populated when is true β€” - /// the parse degraded gracefully rather than discarding upstream - /// attribute / skill / spell / enchantment data. Callers that - /// require all-or-nothing trailer semantics should ignore the - /// trailer fields when this flag is set. public readonly record struct Parsed( uint WeenieType, DescriptionPropertyFlag PropertyFlags, @@ -244,18 +187,7 @@ public static class PlayerDescriptionParser IReadOnlyList Attributes, IReadOnlyList Skills, IReadOnlyDictionary Spells, - IReadOnlyList Enchantments, - CharacterOptionDataFlag OptionFlags, - uint Options1, - uint Options2, - IReadOnlyList Shortcuts, - IReadOnlyList> HotbarSpells, - IReadOnlyList<(uint Id, uint Amount)> DesiredComps, - uint SpellbookFilters, - ReadOnlyMemory GameplayOptions, - IReadOnlyList Inventory, - IReadOnlyList Equipped, - bool TrailerTruncated); + IReadOnlyList Enchantments); /// /// Parse a PlayerDescription payload. The 0xF7B0 envelope has been @@ -317,126 +249,18 @@ public static class PlayerDescriptionParser ReadSpellTable(payload, ref pos, spells); // ── Enchantments (Issue #7 / #12) ─────────────────────────────── + // Outer EnchantmentMask + per-bucket count + NΓ—Enchantment(60-64 B). + // Holtburger events.rs:462-501. After this block come options / + // shortcuts / hotbars / inventory / equipped β€” those need a + // heuristic walker for the variable-length gameplay_options blob. + // Filed as ISSUES.md #13 for follow-up; stop here cleanly so + // partial parses still populate enchantments. if (vectorFlags.HasFlag(DescriptionVectorFlag.Enchantment)) ReadEnchantmentBlock(payload, ref pos, enchantments); - // ── Trailer (Issue #13): options + shortcuts + hotbars + inventory ── - // Wrapped in its own try/catch β€” a malformed trailer must not destroy - // the attribute / skill / spell / enchantment data we already extracted. - CharacterOptionDataFlag optionFlags = CharacterOptionDataFlag.None; - uint options1 = 0; - uint options2 = 0; - uint spellbookFilters = 0; - List shortcuts = new(); - List> hotbarSpells = new(); - List<(uint, uint)> desiredComps = new(); - ReadOnlyMemory gameplayOptions = ReadOnlyMemory.Empty; - List inventory = new(); - List equipped = new(); - bool trailerTruncated = false; - - try - { - if (payload.Length - pos >= 8) - { - optionFlags = (CharacterOptionDataFlag)ReadU32(payload, ref pos); - options1 = ReadU32(payload, ref pos); - - if (optionFlags.HasFlag(CharacterOptionDataFlag.Shortcut)) - { - uint count = ReadU32(payload, ref pos); - if (count > 10_000) throw new FormatException("unreasonable shortcut count"); - for (uint i = 0; i < count; i++) - { - uint idx = ReadU32(payload, ref pos); - uint guid = ReadU32(payload, ref pos); - ushort spellId = ReadU16(payload, ref pos); - ushort layer = ReadU16(payload, ref pos); - shortcuts.Add(new ShortcutEntry(idx, guid, spellId, layer)); - } - } - - if (optionFlags.HasFlag(CharacterOptionDataFlag.SpellLists8)) - { - for (int b = 0; b < 8; b++) - { - uint count = ReadU32(payload, ref pos); - if (count > 10_000) throw new FormatException("unreasonable hotbar count"); - var list = new List((int)count); - for (uint i = 0; i < count; i++) - list.Add(ReadU32(payload, ref pos)); - hotbarSpells.Add(list); - } - } - else if (payload.Length - pos >= 4) - { - // Legacy single-list fallback (holtburger events.rs:544-556). - uint count = ReadU32(payload, ref pos); - if (count > 10_000) throw new FormatException("unreasonable hotbar count"); - var list = new List((int)count); - for (uint i = 0; i < count; i++) - list.Add(ReadU32(payload, ref pos)); - hotbarSpells.Add(list); - } - - if (optionFlags.HasFlag(CharacterOptionDataFlag.DesiredComps)) - { - // holtburger events.rs:558-574 β€” u16 count + u16 padding (4-byte header). - if (payload.Length - pos < 4) throw new FormatException("truncated desired_comps header"); - ushort count = ReadU16(payload, ref pos); - ReadU16(payload, ref pos); // padding/buckets β€” discarded - if (count > 10_000) throw new FormatException("unreasonable desired_comps count"); - for (int i = 0; i < count; i++) - { - uint id = ReadU32(payload, ref pos); - uint amt = ReadU32(payload, ref pos); - desiredComps.Add((id, amt)); - } - } - - // holtburger events.rs:576-582 β€” spellbook_filters is optional; defaults - // to 0 if EOF. - if (payload.Length - pos >= 4) - spellbookFilters = ReadU32(payload, ref pos); - - if (optionFlags.HasFlag(CharacterOptionDataFlag.CharacterOptions2)) - options2 = ReadU32(payload, ref pos); - - if (optionFlags.HasFlag(CharacterOptionDataFlag.GameplayOptions)) - { - int gameplayStart = pos; - if (TryHeuristicInventoryStart(payload, gameplayStart, out int invStart, out int end, - inventory, equipped)) - { - gameplayOptions = payload.Slice(gameplayStart, invStart - gameplayStart).ToArray(); - pos = end; - } - } - else - { - // Strict path: inventory + equipped follow directly. - TryUnpackInventoryStrict(payload, ref pos, inventory, equipped); - } - } - } - catch (FormatException ex) - { - // Trailer corrupted β€” keep what we have and flag it. Once - // Tasks 3-9 add list reads inside this try block, partial - // lists may be visible to callers; TrailerTruncated tells - // them so they can ignore the trailer if they need all-or- - // nothing semantics. - trailerTruncated = true; - if (System.Environment.GetEnvironmentVariable("ACDREAM_DUMP_VITALS") == "1") - System.Console.WriteLine($"PlayerDescriptionParser: trailer FormatException at pos={pos}/{payload.Length}: {ex.Message}"); - } - return new Parsed( weenieType, propertyFlags, vectorFlags, hasHealth, - bundle, positions, attributes, skills, spells, enchantments, - optionFlags, options1, options2, - shortcuts, hotbarSpells, desiredComps, spellbookFilters, - gameplayOptions, inventory, equipped, trailerTruncated); + bundle, positions, attributes, skills, spells, enchantments); } catch (FormatException ex) { @@ -457,16 +281,7 @@ public static class PlayerDescriptionParser { return new Parsed(weenieType, pFlags, vFlags, hasHealth, bundle, positions, attributes, skills, spells, - System.Array.Empty(), - CharacterOptionDataFlag.None, 0u, 0u, - System.Array.Empty(), - System.Array.Empty>(), - System.Array.Empty<(uint, uint)>(), - 0u, - ReadOnlyMemory.Empty, - System.Array.Empty(), - System.Array.Empty(), - TrailerTruncated: false); + System.Array.Empty()); } // ── Attribute block reader ────────────────────────────────────────────── @@ -727,86 +542,6 @@ public static class PlayerDescriptionParser bucket); } - /// Strict inventory + equipped block reader. Returns true if - /// the bytes from parse cleanly per holtburger - /// events.rs:143-193 (unpack_inventory_and_equipped_strict). - /// Counts capped at 10,000; inventory ContainerType must be 0..2 - /// (NonContainer / Container / Foci). - private static bool TryUnpackInventoryStrict( - ReadOnlySpan src, ref int pos, - List inventory, List equipped) - { - inventory.Clear(); - equipped.Clear(); - if (pos + 4 > src.Length) return false; - uint invCount = BinaryPrimitives.ReadUInt32LittleEndian(src.Slice(pos)); - pos += 4; - if (invCount > 10_000) return false; - - for (uint i = 0; i < invCount; i++) - { - if (pos + 8 > src.Length) return false; - uint guid = BinaryPrimitives.ReadUInt32LittleEndian(src.Slice(pos)); - uint wtype = BinaryPrimitives.ReadUInt32LittleEndian(src.Slice(pos + 4)); - pos += 8; - if (wtype > 2) return false; - inventory.Add(new InventoryEntry(guid, wtype)); - } - - if (pos + 4 > src.Length) return false; - uint eqCount = BinaryPrimitives.ReadUInt32LittleEndian(src.Slice(pos)); - pos += 4; - if (eqCount > 10_000) return false; - - for (uint i = 0; i < eqCount; i++) - { - if (pos + 12 > src.Length) return false; - uint guid = BinaryPrimitives.ReadUInt32LittleEndian(src.Slice(pos)); - uint loc = BinaryPrimitives.ReadUInt32LittleEndian(src.Slice(pos + 4)); - uint prio = BinaryPrimitives.ReadUInt32LittleEndian(src.Slice(pos + 8)); - pos += 12; - equipped.Add(new EquippedEntry(guid, loc, prio)); - } - return true; - } - - /// 4-byte-aligned forward scan from - /// looking for the first offset where TryUnpackInventoryStrict - /// consumes exactly to end-of-buffer. Mirrors holtburger - /// find_inventory_start_after_gameplay_options in events.rs:195-218. - private static bool TryHeuristicInventoryStart( - ReadOnlySpan src, int start, - out int invStart, out int end, - List inventory, List equipped) - { - invStart = end = 0; - inventory.Clear(); - equipped.Clear(); - if (start + 8 > src.Length) return false; - - int candidate = start; - int misalign = candidate & 3; - if (misalign != 0) candidate += 4 - misalign; - - int last = src.Length - 8; - while (candidate <= last) - { - int tmp = candidate; - var tmpInv = new List(); - var tmpEq = new List(); - if (TryUnpackInventoryStrict(src, ref tmp, tmpInv, tmpEq) && tmp == src.Length) - { - invStart = candidate; - end = tmp; - inventory.AddRange(tmpInv); - equipped.AddRange(tmpEq); - return true; - } - candidate += 4; - } - return false; - } - private static ushort ReadU16(ReadOnlySpan src, ref int pos) { if (src.Length - pos < 2) throw new FormatException("truncated u16"); diff --git a/src/AcDream.Core.Net/Messages/SetState.cs b/src/AcDream.Core.Net/Messages/SetState.cs deleted file mode 100644 index 70740a4..0000000 --- a/src/AcDream.Core.Net/Messages/SetState.cs +++ /dev/null @@ -1,86 +0,0 @@ -using System; -using System.Buffers.Binary; - -namespace AcDream.Core.Net.Messages; - -/// -/// Inbound SetState GameMessage (opcode 0xF74B). The server -/// broadcasts this whenever a previously-spawned entity's -/// PhysicsState bitmask changes after CreateObject β€” chiefly -/// when a door opens / closes (server flips ETHEREAL_PS = 0x4) or a -/// spell projectile becomes ethereal post-impact. -/// -/// -/// Wire layout (per -/// references/holtburger/crates/holtburger-protocol/src/messages/object/messages/properties.rs:117-122, -/// matched by every other acdream parser): -/// -/// -/// u32 opcode β€” 0xF74B -/// u32 objectGuid -/// u32 physicsState β€” bitmask (acclient.h:2815 / 2819) -/// u16 instanceSequence β€” stale-packet rejection -/// u16 stateSequence β€” stale-packet rejection -/// -/// -/// -/// Total body size: 16 bytes (4-byte opcode + 12-byte payload). -/// -/// -/// -/// Server-side reference: -/// references/ACE/Source/ACE.Server/Network/GameMessages/Messages/GameMessageSetState.cs:8-15 -/// β€” ACE writes the same field order using its UShortSequence.CurrentBytes -/// helper (which calls BitConverter.GetBytes((ushort)value) = 2 bytes per -/// sequence field), so its wire output matches holtburger's 12-byte payload. -/// A one-shot ACDREAM_PROBE_BUILDING hex-dump in -/// 's dispatcher (added in the same commit) emits -/// the first SetState body bytes for runtime confirmation. -/// -/// -/// -/// Named-retail anchor: CPhysicsObj::set_state at -/// docs/research/named-retail/acclient_2013_pseudo_c.txt:283044 -/// describes the runtime state-store on the in-memory object -/// (this->state = arg2). The wire format for this opcode is -/// confirmed by holtburger's SetStateData struct; the named-retail -/// decomp does not cover the deserialization path for this opcode. -/// -/// -public static class SetState -{ - public const uint Opcode = 0xF74Bu; - - public readonly record struct Parsed( - uint Guid, - uint PhysicsState, - ushort InstanceSequence, - ushort StateSequence); - - /// - /// Parse a 0xF74B body. must start with the - /// 4-byte opcode (matches the convention used by VectorUpdate / - /// UpdateMotion / UpdatePosition). Returns null on truncation or - /// opcode mismatch. - /// - public static Parsed? TryParse(ReadOnlySpan body) - { - if (body.Length < 16) return null; - try - { - uint opcode = BinaryPrimitives.ReadUInt32LittleEndian(body.Slice(0, 4)); - if (opcode != Opcode) return null; - - uint guid = BinaryPrimitives.ReadUInt32LittleEndian(body.Slice(4, 4)); - uint state = BinaryPrimitives.ReadUInt32LittleEndian(body.Slice(8, 4)); - ushort instSeq = BinaryPrimitives.ReadUInt16LittleEndian(body.Slice(12, 2)); - ushort stateSeq = BinaryPrimitives.ReadUInt16LittleEndian(body.Slice(14, 2)); - - return new Parsed(guid, state, instSeq, stateSeq); - } - catch - { - return null; - } - } -} diff --git a/src/AcDream.Core.Net/WorldSession.cs b/src/AcDream.Core.Net/WorldSession.cs index 8b4e0f7..af7d695 100644 --- a/src/AcDream.Core.Net/WorldSession.cs +++ b/src/AcDream.Core.Net/WorldSession.cs @@ -69,18 +69,7 @@ public sealed class WorldSession : IDisposable // Elasticity defaults to 0.05f. When set, drives the velocity- // reflection bounce magnitude (clamped to [0, 0.1] retail-side). float? Friction = null, - float? Elasticity = null, - // 2026-05-15: from the WeenieHeader optional tail. - // Useability: retail ITEM_USEABLE enum (acclient.h:6478). Bit - // USEABLE_REMOTE (0x20) means the entity accepts R-key Use from - // the world; signs/banners have USEABLE_UNDEF (0x0) and should - // silently ignore Use attempts. null = weenieFlags didn't include - // the field (treat conservatively as not-useable). - // UseRadius: server's use-action reach in meters. Doubles as a - // sizing hint for tall-scenery selection indicators when the - // server publishes it for non-useable display entities. - uint? Useability = null, - float? UseRadius = null); + float? Elasticity = null); /// Fires when the session finishes parsing a CreateObject. public event Action? EntitySpawned; @@ -140,19 +129,6 @@ public sealed class WorldSession : IDisposable /// public event Action? VectorUpdated; - /// - /// Fires when the server broadcasts a SetState (0xF74B) game - /// message β€” a previously-spawned entity's PhysicsState - /// bitmask changed post-CreateObject. Chiefly doors flipping - /// ETHEREAL_PS = 0x4 on Use (see ACE - /// WorldObjects/Door.cs:127, WorldObject.cs:640-660). - /// Subscribers route the new state into - /// so the - /// existing collision-exemption short-circuit honors the flip on the - /// next resolver tick. - /// - public event Action? StateUpdated; - /// /// Fires when the server sends a PlayerTeleport (0xF751) game message, /// signalling that the player is entering portal space. The uint payload @@ -399,10 +375,6 @@ public sealed class WorldSession : IDisposable /// private bool _loginCompleteSent; - /// L.2g slice 1: one-shot guard so the [setstate-hex] probe - /// emits the first SetState's body bytes only, not 5–10/sec. - private bool _setStateHexDumped; - /// /// Phase B.2: per-session game-action sequence counter. Monotonically /// incremented by and embedded in @@ -714,9 +686,7 @@ public sealed class WorldSession : IDisposable parsed.Value.PhysicsState, parsed.Value.ObjectDescriptionFlags, parsed.Value.Friction, - parsed.Value.Elasticity, - parsed.Value.Useability, - parsed.Value.UseRadius)); + parsed.Value.Elasticity)); } } else if (op == DeleteObject.Opcode) @@ -725,19 +695,6 @@ public sealed class WorldSession : IDisposable if (parsed is not null) EntityDeleted?.Invoke(parsed.Value); } - else if (op == PickupEvent.Opcode) - { - // ACE sends PickupEvent (0xF74A) instead of DeleteObject - // when a player picks up a world item (Player_Tracking - // .RemoveTrackedObject with fromPickup=true). Downstream - // view-removal semantics are identical, so we adapt to - // DeleteObject.Parsed and reuse the existing handler. - var parsed = PickupEvent.TryParse(body); - if (parsed is not null) - EntityDeleted?.Invoke( - new DeleteObject.Parsed( - parsed.Value.Guid, parsed.Value.InstanceSequence)); - } else if (op == UpdateMotion.Opcode) { // Phase 6.6: the server sends UpdateMotion (0xF74C) whenever an @@ -793,28 +750,6 @@ public sealed class WorldSession : IDisposable if (parsed is not null) VectorUpdated?.Invoke(parsed.Value); } - else if (op == SetState.Opcode) - { - // L.2g slice 1 (2026-05-12): server broadcasts SetState - // (0xF74B) when an entity's PhysicsState changes - // post-spawn β€” chiefly doors flipping ETHEREAL on Use. - // Holtburger validated wire format = 16 bytes (opcode + - // guid + state + 2Γ—u16 sequence). One-shot probe-gated - // hex-dump (ACDREAM_PROBE_BUILDING) captures the wire - // bytes for confidence before declaring slice 1 done. - if (AcDream.Core.Physics.PhysicsDiagnostics.ProbeBuildingEnabled - && !_setStateHexDumped) - { - _setStateHexDumped = true; - var hex = string.Join(" ", body.Take(Math.Min(body.Length, 32)) - .Select(b => b.ToString("X2"))); - Console.WriteLine($"[setstate-hex] body.len={body.Length} first-{Math.Min(body.Length, 32)}-bytes: {hex}"); - } - - var parsed = SetState.TryParse(body); - if (parsed is not null) - StateUpdated?.Invoke(parsed.Value); - } else if (op == HearSpeech.LocalOpcode || op == HearSpeech.RangedOpcode) { // Phase H.1: local/ranged chat. Standalone GameMessage diff --git a/src/AcDream.Core/AcDream.Core.csproj b/src/AcDream.Core/AcDream.Core.csproj index 1ac800c..6155c02 100644 --- a/src/AcDream.Core/AcDream.Core.csproj +++ b/src/AcDream.Core/AcDream.Core.csproj @@ -18,13 +18,5 @@ - - - diff --git a/src/AcDream.Core/Meshing/SetupPartTransforms.cs b/src/AcDream.Core/Meshing/SetupPartTransforms.cs deleted file mode 100644 index 3bffbd5..0000000 --- a/src/AcDream.Core/Meshing/SetupPartTransforms.cs +++ /dev/null @@ -1,76 +0,0 @@ -using System.Collections.Generic; -using System.Numerics; -using DatReaderWriter.DBObjs; -using DatReaderWriter.Enums; -using DatReaderWriter.Types; - -namespace AcDream.Core.Meshing; - -/// -/// Compute the per-part static transforms for a Setup using its -/// PlacementFrames. For each part i, the returned matrix takes a -/// point in part-local space and yields a point in setup-local space at -/// the Setup's resting pose. -/// -/// -/// Mirrors 's pose-source priority β€” -/// PlacementFrames[Resting] β†’ [Default] β†’ first available -/// β€” so that a particle anchor at part i matches the part's -/// visible rest position. If renderer and resolver ever drift on this -/// priority, particles will visibly drift relative to their parent -/// mesh; keep them in lockstep. -/// -/// -/// -/// Returns an empty list when the Setup has no PlacementFrames. The -/// caller (e.g. ParticleHookSink.SpawnFromHook) should then fall -/// back to per part, which is the -/// pre-C.1.5b behavior. -/// -/// -/// -/// For animated entities, per-part transforms vary per animation frame -/// and live in AnimatedEntityState; a future "animated -/// DefaultScript" path would publish those each tick via the same -/// SetEntityPartTransforms seam. Out of scope here. -/// -/// -public static class SetupPartTransforms -{ - public static IReadOnlyList Compute(Setup setup) - { - AnimationFrame? source = null; - if (setup.PlacementFrames.TryGetValue(Placement.Resting, out var resting)) - { - source = resting; - } - else if (setup.PlacementFrames.TryGetValue(Placement.Default, out var def)) - { - source = def; - } - else - { - foreach (var kvp in setup.PlacementFrames) - { - source = kvp.Value; - break; - } - } - - if (source is null) return System.Array.Empty(); - - int partCount = setup.Parts.Count; - var result = new Matrix4x4[partCount]; - for (int i = 0; i < partCount; i++) - { - Frame frame = i < source.Frames.Count - ? source.Frames[i] - : new Frame { Origin = Vector3.Zero, Orientation = Quaternion.Identity }; - Vector3 scale = i < setup.DefaultScale.Count ? setup.DefaultScale[i] : Vector3.One; - result[i] = Matrix4x4.CreateScale(scale) - * Matrix4x4.CreateFromQuaternion(frame.Orientation) - * Matrix4x4.CreateTranslation(frame.Origin); - } - return result; - } -} diff --git a/src/AcDream.Core/Physics/BSPQuery.cs b/src/AcDream.Core/Physics/BSPQuery.cs index 289ff0e..0cb17e4 100644 --- a/src/AcDream.Core/Physics/BSPQuery.cs +++ b/src/AcDream.Core/Physics/BSPQuery.cs @@ -1214,29 +1214,15 @@ public static class BSPQuery if (!obj.State.HasFlag(ObjectInfoState.PerfectClip)) { collisions.SetCollisionNormal(collisionNormal); - // L.2d slice 1 (2026-05-13): diagnostic side-channel. - if (PhysicsDiagnostics.ProbeBuildingEnabled) - PhysicsDiagnostics.LastBspHitPoly = hitPoly; return TransitionState.Collided; } var validPos = new CollisionSphere(checkPos); if (!AdjustToPlane(root, resolved, validPos, curPos, hitPoly, contactPoint)) - { - // L.2d slice 1 (2026-05-13): record the would-have-hit poly before - // the early-out β€” collisions.SetCollisionNormal isn't called on - // this path, but the caller's CollisionInfo.CollisionNormalValid - // check will catch the parent slide site's normal write instead. - if (PhysicsDiagnostics.ProbeBuildingEnabled) - PhysicsDiagnostics.LastBspHitPoly = hitPoly; return TransitionState.Collided; - } collisions.SetCollisionNormal(collisionNormal); - // L.2d slice 1 (2026-05-13): diagnostic side-channel. - if (PhysicsDiagnostics.ProbeBuildingEnabled) - PhysicsDiagnostics.LastBspHitPoly = hitPoly; var adjusted = validPos.Center - checkPos.Center; // ACE: path.LocalSpacePos.LocalToGlobalVec(adjusted) * scale @@ -1544,16 +1530,6 @@ public static class BSPQuery if (hit0 || hitPoly0 is not null) { - // L.2d slice 1.5 (2026-05-13): record the hit poly EARLY, - // before the StepSphereUp branch can recurse into - // ResolveWithTransition β†’ FindObjCollisions and clobber the - // side-channel via the inner call's per-resolve clear. Path 5 - // is the dominant grounded-player path; without this the - // probe's [resolve-bldg] line for every grounded BSP hit was - // mis-labeled as "n/a (cylinder)". - if (PhysicsDiagnostics.ProbeBuildingEnabled) - PhysicsDiagnostics.LastBspHitPoly = hitPoly0; - var worldNormal = L2W(hitPoly0!.Plane.Normal); // L.2.3b (2026-04-29): recursion guard. Retail // (acclient_2013_pseudo_c.txt:272954) gates step_sphere_up on @@ -1582,12 +1558,6 @@ public static class BSPQuery if (hit1 || hitPoly1 is not null) { - // L.2d slice 1.5 (2026-05-13): same early-record as foot - // sphere β€” head-sphere wall hits also recurse via - // StepSphereUp on the grounded path. - if (PhysicsDiagnostics.ProbeBuildingEnabled) - PhysicsDiagnostics.LastBspHitPoly = hitPoly1; - var worldNormal = L2W(hitPoly1!.Plane.Normal); // L.2.3b: same recursion guard as the foot-sphere branch. if (engine is not null && !path.StepUp && !path.StepDown) @@ -1668,9 +1638,6 @@ public static class BSPQuery collisions.SetCollisionNormal(worldNormal0); collisions.SetSlidingNormal(worldNormal0); - // L.2d slice 1 (2026-05-13): diagnostic side-channel. - if (PhysicsDiagnostics.ProbeBuildingEnabled) - PhysicsDiagnostics.LastBspHitPoly = hitPoly0; return TransitionState.Slid; } @@ -1678,9 +1645,6 @@ public static class BSPQuery // Per retail (acclient_2013_pseudo_c.txt:323783-323821). path.SetCollide(worldNormal0); path.WalkableAllowance = PhysicsGlobals.LandingZ; - // L.2d slice 1 (2026-05-13): diagnostic side-channel. - if (PhysicsDiagnostics.ProbeBuildingEnabled) - PhysicsDiagnostics.LastBspHitPoly = hitPoly0; return TransitionState.Adjusted; } @@ -1708,18 +1672,12 @@ public static class BSPQuery collisions.SetCollisionNormal(worldNormal1); collisions.SetSlidingNormal(worldNormal1); - // L.2d slice 1 (2026-05-13): diagnostic side-channel. - if (PhysicsDiagnostics.ProbeBuildingEnabled) - PhysicsDiagnostics.LastBspHitPoly = hitPoly1; return TransitionState.Slid; } // Head sphere hit shallow surface: SetCollide. path.SetCollide(worldNormal1); path.WalkableAllowance = PhysicsGlobals.LandingZ; - // L.2d slice 1 (2026-05-13): diagnostic side-channel. - if (PhysicsDiagnostics.ProbeBuildingEnabled) - PhysicsDiagnostics.LastBspHitPoly = hitPoly1; return TransitionState.Adjusted; } } diff --git a/src/AcDream.Core/Physics/CollisionExemption.cs b/src/AcDream.Core/Physics/CollisionExemption.cs index 2e66751..89b368d 100644 --- a/src/AcDream.Core/Physics/CollisionExemption.cs +++ b/src/AcDream.Core/Physics/CollisionExemption.cs @@ -59,24 +59,14 @@ public static class CollisionExemption public static bool ShouldSkip(uint targetState, EntityCollisionFlags targetFlags, ObjectInfoState moverState) { - // 1. Target ETHEREAL β†’ walk through. - // Retail (acclient_2013_pseudo_c.txt:276782) requires BOTH - // ETHEREAL_PS (0x4) AND IGNORE_COLLISIONS_PS (0x10) to wrap - // the entire body of FindObjCollisions and skip collision. - // ETHEREAL alone takes a different retail path (line 276795 - // sets sphere_path.obstruction_ethereal = 1 and downstream - // movement allows passage despite the contact). We haven't - // ported that downstream path yet. - // - // L.2g slice 1b (2026-05-13): ACE's Door.Open() sends only - // ETHEREAL (state=0x0001000C observed live), not the - // ETHEREAL|IGNORE_COLLISIONS combo retail servers broadcast. - // Pragmatic shortcut: exempt on ETHEREAL alone so doors - // become passable when ACE flips the bit. Retail-server - // broadcasts (state=0x14+) still hit this branch correctly - // because both bits set implies ETHEREAL set. - if ((targetState & ETHEREAL_PS) != 0) + // 1. Target ETHEREAL + IGNORE_COLLISIONS β†’ walk through. + // acclient_2013_pseudo_c.txt:276782 β€” wraps the entire body of + // FindObjCollisions; we hoist it as the first early-out. + if ((targetState & ETHEREAL_PS) != 0 + && (targetState & IGNORE_COLLISIONS_PS) != 0) + { return true; + } // 2. Viewer mover + creature target β†’ walk through. // acclient_2013_pseudo_c.txt:276787-276790. diff --git a/src/AcDream.Core/Physics/PhysicsDiagnostics.cs b/src/AcDream.Core/Physics/PhysicsDiagnostics.cs deleted file mode 100644 index 540cac0..0000000 --- a/src/AcDream.Core/Physics/PhysicsDiagnostics.cs +++ /dev/null @@ -1,144 +0,0 @@ -using System; - -namespace AcDream.Core.Physics; - -/// -/// L.2a slice 1 (2026-05-12) β€” runtime-toggleable physics probe flags. -/// Initialized from env vars at process start; flippable at runtime via -/// the DebugPanel mirror (or by direct assignment). Log call sites read -/// these statics so a checkbox toggle takes effect on the next resolve -/// without relaunching. -/// -/// -/// L.2d slice 1 (2026-05-13) adds + -/// the diagnostic side-channel. Future -/// slices may fold the older ACDREAM_DUMP_* env vars into this -/// class for unified runtime toggling. Until then, those older flags -/// remain sticky-at-startup per their original implementation. -/// -/// -public static class PhysicsDiagnostics -{ - /// - /// When true, emits - /// one structured [resolve] line per call: input + target + - /// output position/cell, grounded state, contact-plane status, - /// collision-normal validity, walkable polygon status, moving entity - /// id. Initial state from ACDREAM_PROBE_RESOLVE=1. - /// - public static bool ProbeResolveEnabled { get; set; } = - Environment.GetEnvironmentVariable("ACDREAM_PROBE_RESOLVE") == "1"; - - /// - /// When true, every change to PlayerMovementController.CellId - /// emits one [cell-transit] line: old β†’ new cell, current - /// world position, reason tag (resolver / teleport). - /// Initial state from ACDREAM_PROBE_CELL=1. - /// - public static bool ProbeCellEnabled { get; set; } = - Environment.GetEnvironmentVariable("ACDREAM_PROBE_CELL") == "1"; - - /// - /// L.2d slice 1 (2026-05-13). When true, every BSP-shadow-entry hit - /// attributed by TransitionTypes.FindObjCollisions emits a - /// multi-line [resolve-bldg] entry: which part (partIdx vs 0), - /// physics-BSP root radius vs visual AABB radius, world-space entity - /// origin, and the specific hit polygon's vertices in both - /// object-local and world space. Designed to distinguish the three - /// L.2d hypotheses (wrong BSP loaded / over-registered parts / - /// BSPQuery flaw) from a single Holtburg-doorway capture. - /// - /// - /// Also gates a one-time [entity-source] log line at every - /// ShadowObjects.Register(...) call site in GameWindow - /// β€” makes entityId=0xA9B479 in a probe line greppable to its - /// source registration within the same log file. - /// - /// - /// - /// Initial state from ACDREAM_PROBE_BUILDING=1. Mirrorable - /// via DebugVM.ProbeBuilding when ACDREAM_DEVTOOLS=1. - /// - /// - /// - /// Spec: docs/superpowers/specs/2026-05-13-l2d-cbuildingobj-collision-design.md. - /// - /// - public static bool ProbeBuildingEnabled { get; set; } = - Environment.GetEnvironmentVariable("ACDREAM_PROBE_BUILDING") == "1"; - - /// - /// L.2d slice 1 (2026-05-13). Diagnostic side-channel: the - /// that - /// recorded for the most recent collision-normal write. - /// clears this to - /// before each shadow-entry test and reads it - /// back after, so emitting the [resolve-bldg] probe line can - /// reference the actual hit poly without plumbing an out-param - /// through BSPQuery's recursive private methods. - /// - /// - /// Written by only when - /// is true, so this stays - /// zero-cost in normal play. Cylinder collisions leave this - /// β€” the probe line emits - /// hitPoly: n/a (cylinder) in that case. - /// - /// - /// - /// Not threadsafe β€” physics runs on a single thread. If that - /// changes, this needs [ThreadStatic] or rethink. Deviation - /// from spec component 4 (which described an out-param); the - /// side-channel keeps BSPQuery's signature stable and the diagnostic - /// path off the production code surface. - /// - /// - public static ResolvedPolygon? LastBspHitPoly { get; set; } - - /// - /// B.6 slice 1 (2026-05-14) β€” baseline trace for the local-player - /// server-initiated auto-walk path (issue #63). When true, the - /// following events emit one-line [autowalk-*] logs: - /// - /// [autowalk-out] on every SendUse - /// / SendPickUp the local player issues β€” these are the - /// packets that may trigger ACE's server-side CreateMoveToChain - /// when the target is out of WithinUseRadius. - /// [autowalk-mt] on every inbound - /// UpdateMotion for the local player β€” captures the - /// MovementType + MoveToPath + speed/runRate ACE sends. - /// [autowalk-up] on every inbound - /// UpdatePosition for the local player β€” answers "what's - /// ACE's broadcast cadence during auto-walk?" - /// - /// Initial state from ACDREAM_PROBE_AUTOWALK=1. - /// - /// - /// Spec: docs/superpowers/specs/2026-05-14-phase-b6-design.md - /// Β§"Required investigation". - /// - /// - public static bool ProbeAutoWalkEnabled { get; set; } = - Environment.GetEnvironmentVariable("ACDREAM_PROBE_AUTOWALK") == "1"; - - /// - /// 2026-05-16. Logs one line per `IsUseableTarget` call that takes - /// the null-useability fallback path (creature pass / BF_DOOR pass / - /// BF_LIFESTONE pass / etc.). Used to measure how often ACE's seed - /// DB ships entities without `_useability` set β€” settles whether - /// the fallback is live code or theoretical defense. - /// - /// - /// Retail has NO fallback; null/zero useability blocks Use entirely - /// (acclient_2013_pseudo_c.txt:402923 ItemHolder::UseObject β€” - /// IsUseable==0 falls through to "cannot be used" branch). Our - /// fallback exists because ACE genuinely sends null for many seed - /// weenies. The probe quantifies "many". - /// - /// - /// Toggle via env var ACDREAM_PROBE_USEABILITY_FALLBACK=1 - /// or DebugPanel checkbox. - /// - public static bool ProbeUseabilityFallbackEnabled { get; set; } = - Environment.GetEnvironmentVariable("ACDREAM_PROBE_USEABILITY_FALLBACK") == "1"; -} diff --git a/src/AcDream.Core/Physics/PhysicsEngine.cs b/src/AcDream.Core/Physics/PhysicsEngine.cs index 4bfcb3e..fe308ae 100644 --- a/src/AcDream.Core/Physics/PhysicsEngine.cs +++ b/src/AcDream.Core/Physics/PhysicsEngine.cs @@ -677,48 +677,6 @@ public sealed class PhysicsEngine $"deltaXY=({dx:F3},{dy:F3}) cp={cpInfo}"); } - // L.2a slice 1 (2026-05-12): general-purpose resolver probe. - // One line per call when PhysicsDiagnostics.ProbeResolveEnabled - // is set (env var ACDREAM_PROBE_RESOLVE=1 at startup, or the - // DebugPanel checkbox flipped at runtime). Captures every - // dimension L.2 cares about: input/output position, input/output - // cell, ok-vs-partial, grounded-in vs contact-out, contact-plane - // status, wall normal if hit, walkable polygon valid. Zero cost - // when off (one static-bool read). - if (PhysicsDiagnostics.ProbeResolveEnabled) - { - var probePost = sp.CheckPos; - string probeCp = ci.ContactPlaneValid - ? "valid" - : (ci.LastKnownContactPlaneValid ? "lastKnown" : "none"); - string probeHit; - if (collisionNormalValid) - { - // L.2a slice 2 (2026-05-12): include the hit object's guid + - // environment flag so we can tell whether the wall is a building - // (CBuildingObj), a door (CC0Cxxxx range), an NPC, or terrain. - // Without this we know the wall normal but not the responsible - // entity β€” half the L.2d sub-direction call. - string objPart = ci.LastCollidedObjectGuid.HasValue - ? System.FormattableString.Invariant( - $" obj=0x{ci.LastCollidedObjectGuid.Value:X8}") - : ""; - string envPart = ci.CollidedWithEnvironment ? " env" : ""; - int objCount = ci.CollideObjectGuids.Count; - string objCountPart = objCount > 1 - ? System.FormattableString.Invariant($" nObj={objCount}") - : ""; - probeHit = System.FormattableString.Invariant( - $"yes n=({collisionNormal.X:F2},{collisionNormal.Y:F2},{collisionNormal.Z:F2}){objPart}{envPart}{objCountPart}"); - } - else - { - probeHit = "no"; - } - Console.WriteLine(System.FormattableString.Invariant( - $"[resolve] ent=0x{movingEntityId:X8} in=({currentPos.X:F3},{currentPos.Y:F3},{currentPos.Z:F3}) cell=0x{cellId:X8} tgt=({targetPos.X:F3},{targetPos.Y:F3},{targetPos.Z:F3}) out=({probePost.X:F3},{probePost.Y:F3},{probePost.Z:F3}) cell=0x{sp.CheckCellId:X8} ok={ok} groundedIn={isOnGround} cp={probeCp} hit={probeHit} walkable={sp.HasLastWalkablePolygon}")); - } - if (ok) { bool onGround = ci.ContactPlaneValid diff --git a/src/AcDream.Core/Physics/RemoteMoveToDriver.cs b/src/AcDream.Core/Physics/RemoteMoveToDriver.cs index bc1e6d5..90a0388 100644 --- a/src/AcDream.Core/Physics/RemoteMoveToDriver.cs +++ b/src/AcDream.Core/Physics/RemoteMoveToDriver.cs @@ -76,42 +76,6 @@ public static class RemoteMoveToDriver /// public const float TurnRateRadPerSec = MathF.PI / 2.0f; - /// - /// Retail base turn rate for the player Humanoid when turn_speed - /// scalar = 1.0. Convention default omega.z = Β±Ο€/2 rad/s - /// derived from add_motion at 0x005224b0 + the - /// HasOmega-cleared MotionData fallback documented in - /// AnimationSequencer.cs:734-741. ~90Β°/s. - /// - public const float BaseTurnRateRadPerSec = MathF.PI / 2.0f; - - /// - /// Retail's run_turn_factor constant at 0x007c8914 - /// (PDB-named). CMotionInterp::apply_run_to_command - /// equivalent (decomp 0x00527be0, line 305098 of - /// acclient_2013_pseudo_c.txt) multiplies turn_speed - /// by 1.5 when HoldKey.Run is active on a TurnRight/TurnLeft - /// command. Effect: running rotation is 50 % faster than walking. - /// - public const float RunTurnFactor = 1.5f; - - /// - /// Retail-faithful local-player turn rate. - /// - /// Walking: BaseTurnRateRadPerSec β‰ˆ 90Β°/s. - /// Running: BaseTurnRateRadPerSec Γ— RunTurnFactor - /// β‰ˆ 135Β°/s. - /// - /// Replaces the fixed TurnRateRadPerSec for paths that have - /// access to the player's run/walk state (keyboard A/D, auto-walk - /// overlay turn-first). NPC/monster remotes that lack the - /// information continue to use the constant which equals - /// BaseTurnRateRadPerSec. - /// - public static float TurnRateFor(bool running) - => running ? BaseTurnRateRadPerSec * RunTurnFactor - : BaseTurnRateRadPerSec; - /// /// Float-comparison slack for the arrival predicate. With /// min_distance == 0 in a chase packet, exact equality is diff --git a/src/AcDream.Core/Physics/ShadowObjectRegistry.cs b/src/AcDream.Core/Physics/ShadowObjectRegistry.cs index fd3673e..6b4ea11 100644 --- a/src/AcDream.Core/Physics/ShadowObjectRegistry.cs +++ b/src/AcDream.Core/Physics/ShadowObjectRegistry.cs @@ -126,47 +126,6 @@ public sealed class ShadowObjectRegistry t.State, t.Flags); } - /// - /// Update the cached bits for an - /// already-registered entity. Called by the inbound - /// SetState (0xF74B) dispatcher when the server broadcasts a - /// post-spawn PhysicsState change β€” chiefly doors flipping - /// ETHEREAL_PS = 0x4 on Use, so the - /// short-circuit can honor - /// the new state on the next resolve. - /// - /// - /// Retail equivalent: CPhysicsObj::set_state at - /// docs/research/named-retail/acclient_2013_pseudo_c.txt:283044 - /// β€” direct write `this->state = arg2`. Retail also fires side-effect - /// handlers for the 0x800 (lighting), 0x20 (nodraw), 0x4000 (hidden) - /// changed bits; ETHEREAL (0x4) doesn't trigger any of them, so slice 1 - /// scopes to the bare state-write. - /// - /// - /// - /// Implementation: is a value-type record - /// copied into per-cell lists, so we rewrite the copy in each cell the - /// entity occupies. Unregistered entities are a no-op (callers don't - /// have to gate). - /// - /// - public void UpdatePhysicsState(uint entityId, uint newState) - { - if (!_entityToCells.TryGetValue(entityId, out var cellIds)) - return; // not registered β€” no-op - - foreach (var cellId in cellIds) - { - if (!_cells.TryGetValue(cellId, out var list)) continue; - for (int i = 0; i < list.Count; i++) - { - if (list[i].EntityId == entityId) - list[i] = list[i] with { State = newState }; - } - } - } - /// Remove an entity from all cells it was registered in. public void Deregister(uint entityId) { diff --git a/src/AcDream.Core/Physics/TerrainSurface.cs b/src/AcDream.Core/Physics/TerrainSurface.cs index 525569e..fe51188 100644 --- a/src/AcDream.Core/Physics/TerrainSurface.cs +++ b/src/AcDream.Core/Physics/TerrainSurface.cs @@ -198,73 +198,6 @@ public sealed class TerrainSurface return InterpolateZInTriangle(hBL, hBR, hTR, hTL, tx, ty, splitSWtoNE); } - /// - /// Sample the terrain triangle's surface-normal Z component at (localX, localY) - /// from a raw heightmap. Returns the upward component of the unit normal for - /// the specific triangle the point lies in β€” flat ground returns 1.0, steeper - /// slopes return smaller values. Used by for - /// the retail slope filter (CLandCell::find_terrain_poly β†’ polygon.plane.N.z). - /// - public static float SampleNormalZFromHeightmap( - byte[] heights, float[] heightTable, - uint landblockX, uint landblockY, - float localX, float localY) - { - ArgumentNullException.ThrowIfNull(heights); - ArgumentNullException.ThrowIfNull(heightTable); - if (heights.Length < 81) - throw new ArgumentException("heights must have 81 entries", nameof(heights)); - if (heightTable.Length < 256) - throw new ArgumentException("heightTable must have 256 entries", nameof(heightTable)); - - float fx = Math.Clamp(localX / CellSize, 0f, CellsPerSide - 0.001f); - float fy = Math.Clamp(localY / CellSize, 0f, CellsPerSide - 0.001f); - int cx = (int)fx; - int cy = (int)fy; - cx = Math.Clamp(cx, 0, CellsPerSide - 1); - cy = Math.Clamp(cy, 0, CellsPerSide - 1); - - float tx = fx - cx; - float ty = fy - cy; - - float hBL = heightTable[heights[cx * HeightmapSide + cy ]]; - float hBR = heightTable[heights[(cx+1) * HeightmapSide + cy ]]; - float hTR = heightTable[heights[(cx+1) * HeightmapSide + (cy+1)]]; - float hTL = heightTable[heights[cx * HeightmapSide + (cy+1)]]; - - bool splitSWtoNE = IsSplitSWtoNE(landblockX, (uint)cx, landblockY, (uint)cy); - - float dzdx, dzdy; - if (splitSWtoNE) - { - if (tx > ty) - { - dzdx = (hBR - hBL) / CellSize; - dzdy = (hTR - hBR) / CellSize; - } - else - { - dzdx = (hTR - hTL) / CellSize; - dzdy = (hTL - hBL) / CellSize; - } - } - else - { - if (tx + ty <= 1f) - { - dzdx = (hBR - hBL) / CellSize; - dzdy = (hTL - hBL) / CellSize; - } - else - { - dzdx = (hTR - hTL) / CellSize; - dzdy = (hTR - hBR) / CellSize; - } - } - - return 1f / MathF.Sqrt(dzdx * dzdx + dzdy * dzdy + 1f); - } - /// /// Pick the cell's triangle for the chosen diagonal and barycentric- /// interpolate Z. Single source of truth shared by both diff --git a/src/AcDream.Core/Physics/TransitionTypes.cs b/src/AcDream.Core/Physics/TransitionTypes.cs index d5077b8..f2c4f6c 100644 --- a/src/AcDream.Core/Physics/TransitionTypes.cs +++ b/src/AcDream.Core/Physics/TransitionTypes.cs @@ -1389,7 +1389,6 @@ public sealed class Transition var sp = SpherePath; var oi = ObjectInfo; - var ci = CollisionInfo; // #42 diagnostic (2026-05-05): identify which static object causes // the airborne first-frame ~1m push. Capture sphere check pos at @@ -1461,23 +1460,6 @@ public sealed class Transition if (CollisionExemption.ShouldSkip(obj.State, obj.Flags, ObjectInfo.State)) continue; - // L.2a slice 3 (2026-05-12): snapshot collision-normal state so - // we can tell whether THIS object's BSP/CylSphere test produced a - // new collision (BSPQuery sets the normal but may still return OK - // for slide cases). Together with the `result != OK` check below - // this populates ci.CollideObjectGuids + LastCollidedObjectGuid so - // the [resolve] probe surfaces the responsible entity id. - bool collisionWasValidPre = ci.CollisionNormalValid; - - // L.2d slice 1.5 (2026-05-13): no per-iteration LastBspHitPoly - // clear. BSPQuery writes the side-channel early (inside - // `if (hit0 || hitPoly0 != null)` BEFORE any StepSphereUp call), - // so by the time we read it back for the [resolve-bldg] emission - // it reflects THIS entity's hit (or stays null if BSP didn't - // hit). For cylinder dispatch we key the "n/a (cylinder)" label - // off `obj.CollisionType` directly at the emission site, so a - // stale BSP value from a prior iteration can't leak through. - TransitionState result; if (obj.CollisionType == ShadowCollisionType.BSP) @@ -1541,87 +1523,6 @@ public sealed class Transition result = CylinderCollision(obj, sp); } - // L.2a slice 3: attribute the collision (if any) to this entity. - // Two cases: - // - result != OK: the object stopped the transition (hard-block). - // - result == OK but the normal flipped from invalidβ†’valid during - // this call: BSPQuery captured a slide normal without halting. - // Either way this object is responsible for the hit, so add its - // entity id. CollideObjectGuids carries the full chain; the last - // assignment to LastCollidedObjectGuid wins which matches retail's - // "most recent" semantics for the probe. - bool attributed = result != TransitionState.OK - || (!collisionWasValidPre && ci.CollisionNormalValid); - if (attributed) - { - ci.CollideObjectGuids.Add(obj.EntityId); - ci.LastCollidedObjectGuid = obj.EntityId; - } - - // L.2d slice 1 (2026-05-13): emit one multi-line [resolve-bldg] - // entry per attributed hit when the per-shadow-entry probe is on. - // Captures partIdx (distinguishes hypothesis Y: over-registration), - // bspR vs vAabbR (hypothesis X: wrong BSP loaded), and the actual - // hit polygon's vertices in object-local and world space - // (hypothesis Z: BSPQuery flaw). One Holtburg-doorway capture - // resolves which hypothesis is true; slice 2 is the right-sized - // fix. Spec: - // docs/superpowers/specs/2026-05-13-l2d-cbuildingobj-collision-design.md. - // Conformance anchor: ACE BuildingObj.cs:39-52 + named-retail - // acclient_2013_pseudo_c.txt:701260 (find_building_collisions is - // one BSP test on Parts[0]; doorway gap lives inside that BSP). - if (attributed && PhysicsDiagnostics.ProbeBuildingEnabled) - { - uint partIdx = obj.EntityId & 0xFFu; - uint entityIdProbe = obj.EntityId >> 8; - var cachedPhys = engine.DataCache.GetGfxObj(obj.GfxObjId); - var visBounds = engine.DataCache.GetVisualBounds(obj.GfxObjId); - float bspR = cachedPhys?.BoundingSphere?.Radius ?? 0f; - float vAabbR = visBounds?.Radius ?? 0f; - bool hasPhys = cachedPhys is not null; - var entOriginLb = obj.Position - new Vector3(worldOffsetX, worldOffsetY, 0f); - - var sb = new System.Text.StringBuilder(256); - sb.Append(System.FormattableString.Invariant( - $"[resolve-bldg] obj=0x{obj.EntityId:X8} entityId=0x{entityIdProbe:X8} partIdx={partIdx}\n")); - sb.Append(System.FormattableString.Invariant( - $" gfxObj=0x{obj.GfxObjId:X8} hasPhys={hasPhys} bspR={bspR:F2} vAabbR={vAabbR:F2}\n")); - sb.Append(System.FormattableString.Invariant( - $" entOrigin_lb=({entOriginLb.X:F1},{entOriginLb.Y:F1},{entOriginLb.Z:F1})")); - - var poly = PhysicsDiagnostics.LastBspHitPoly; - // L.2d slice 1.5 (2026-05-13): key the n/a label on the - // entity's CollisionType, not on LastBspHitPoly nullness β€” - // a BSP hit with null side-channel indicates a BSPQuery code - // path that didn't write (a bug; we should fix it, not - // pretend the entity was a cylinder). - if (obj.CollisionType == ShadowCollisionType.Cylinder) - { - sb.Append("\n hitPoly: n/a (cylinder)"); - } - else if (poly is null) - { - sb.Append("\n hitPoly: n/a (BSP path β€” side-channel not written, missing BSPQuery wire site)"); - } - else - { - sb.Append(System.FormattableString.Invariant( - $"\n hitPoly: numVerts={poly.NumPoints} plane=({poly.Plane.Normal.X:F3},{poly.Plane.Normal.Y:F3},{poly.Plane.Normal.Z:F3},{poly.Plane.D:F3})")); - int vMax = Math.Min(poly.Vertices.Length, 4); - for (int vi = 0; vi < vMax; vi++) - { - var vLocal = poly.Vertices[vi]; - var vWorld = obj.Position + Vector3.Transform(vLocal * obj.Scale, obj.Rotation); - sb.Append(System.FormattableString.Invariant( - $"\n v{vi}_local=({vLocal.X,5:F2},{vLocal.Y,5:F2},{vLocal.Z,5:F2}) v{vi}_world=({vWorld.X,6:F2},{vWorld.Y,6:F2},{vWorld.Z,6:F2})")); - } - if (poly.Vertices.Length > 4) - sb.Append(System.FormattableString.Invariant( - $"\n ... ({poly.Vertices.Length - 4} more verts elided)")); - } - Console.WriteLine(sb.ToString()); - } - if (result != TransitionState.OK) { if (airborneDiag) diff --git a/src/AcDream.Core/Selection/WorldPicker.cs b/src/AcDream.Core/Selection/WorldPicker.cs deleted file mode 100644 index dfc0300..0000000 --- a/src/AcDream.Core/Selection/WorldPicker.cs +++ /dev/null @@ -1,161 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Numerics; -using AcDream.Core.World; - -namespace AcDream.Core.Selection; - -/// -/// Mouse-to-entity picker. Pure static functions; no state, no DI. -/// -/// turns a pixel + view/projection into a world-space ray. -/// ray-sphere intersects against entity candidates and returns the nearest hit's ServerGuid. -/// -/// Used by GameWindow.OnInputAction to wire SelectLeft / SelectDblLeft / UseSelected to InteractRequests.BuildUse. -/// -public static class WorldPicker -{ - /// - /// Unprojects a pixel coordinate to a world-space ray using the supplied - /// view + projection matrices (System.Numerics row-vector convention, - /// composed as view * projection β€” same as the rest of acdream's camera - /// pipeline; see GameWindow.cs:6445 FrustumPlanes.FromViewProjection). - /// - /// - /// (origin = world point on the near plane, direction = normalized - /// world-space ray direction). Returns (Vector3.Zero, Vector3.Zero) - /// if the view-projection composition is singular. - /// - public static (Vector3 Origin, Vector3 Direction) BuildRay( - float mouseX, float mouseY, - float viewportW, float viewportH, - Matrix4x4 view, Matrix4x4 projection) - { - // Pixel -> NDC. y flipped: top-left pixel maps to ndc.y = +1. - float ndcX = (2f * mouseX) / viewportW - 1f; - float ndcY = 1f - (2f * mouseY) / viewportH; - - var vp = view * projection; - if (!Matrix4x4.Invert(vp, out var invVp)) - return (Vector3.Zero, Vector3.Zero); - - // Unproject near (ndc.z = -1) and far (ndc.z = +1) clip points. - var nearClip = new Vector4(ndcX, ndcY, -1f, 1f); - var farClip = new Vector4(ndcX, ndcY, +1f, 1f); - var n4 = Vector4.Transform(nearClip, invVp); - var f4 = Vector4.Transform(farClip, invVp); - if (n4.W == 0f || f4.W == 0f) - return (Vector3.Zero, Vector3.Zero); - - var nearWorld = new Vector3(n4.X, n4.Y, n4.Z) / n4.W; - var farWorld = new Vector3(f4.X, f4.Y, f4.Z) / f4.W; - var dir = farWorld - nearWorld; - if (dir.LengthSquared() < 1e-10f) - return (Vector3.Zero, Vector3.Zero); - return (nearWorld, Vector3.Normalize(dir)); - } - - /// - /// Ray-sphere intersection against each candidate's - /// using a fixed 0.7 m sphere radius. Returns the - /// of the closest hit within , or null on miss. - /// - /// - /// World-space ray direction. Must be normalized β€” the geometric - /// ray-sphere formula simplifies a = dot(direction, direction) to - /// 1; non-unit input produces an undocumented t-scale that - /// makes maxDistance compare against ray-parameter units instead - /// of world meters. - /// - /// - /// - /// Entities with ServerGuid == 0 (atlas-tier scenery, dat-hydrated - /// statics) are skipped β€” they have no server-side identity and can't be - /// the target of a Use packet. The player's own guid is skipped via - /// . - /// - /// - /// Radius history (Issue #59). Started at 5 m as a forgiving default; - /// in practice this over-picked massively β€” any cursor anywhere near an - /// NPC selected the NPC instead of a nearby item, and "click empty - /// ground to deselect" was nearly impossible. Tightened to 0.7 m on - /// 2026-05-15 to roughly match the actual hitbox radius of humanoids + - /// most items. A future refinement is per-itemType radius (smaller for - /// tapers, bigger for shop chests) or priority sorting (items beat - /// NPCs at equal hit-distance). - /// - /// - public static uint? Pick( - Vector3 origin, Vector3 direction, - IEnumerable candidates, - uint skipServerGuid, - float maxDistance = 50f, - Func? radiusForGuid = null, - Func? verticalOffsetForGuid = null) - { - const float DefaultRadius = 1.0f; - const float DefaultVerticalOffset = 0.9f; - - if (direction.LengthSquared() < 1e-10f) return null; - - uint? bestGuid = null; - float bestT = float.PositiveInfinity; - foreach (var entity in candidates) - { - if (entity.ServerGuid == 0u) continue; - if (entity.ServerGuid == skipServerGuid) continue; - - // Per-entity radius + vertical offset (caller-supplied). - // - // - // Vertical offset (2026-05-15). WorldEntity.Position - // is at the entity's feet (Z=ground for a humanoid). User - // clicks usually land on chest/head (Z β‰ˆ 1–1.8 m). With the - // sphere centred at feet, a chest click is 1.2 m of vertical - // distance from sphere centre β€” bigger than any reasonable - // body radius β€” so the ray misses. Lifting the sphere - // centre to mid-body fixes this: 0.9 m default for - // humanoids, smaller for items, larger for tall objects. - // - // - // - // Radius (2026-05-15). Bumped default 0.7 β†’ 1.0 m to - // accommodate the new vertical-offset sphere placement - // (chest-height sphere centre + 1.0 m radius covers from - // shin to top-of-head for a 1.8 m humanoid). - // - float r = radiusForGuid?.Invoke(entity.ServerGuid) ?? DefaultRadius; - float r2 = r * r; - float vz = verticalOffsetForGuid?.Invoke(entity.ServerGuid) ?? DefaultVerticalOffset; - var sphereCenter = new Vector3( - entity.Position.X, - entity.Position.Y, - entity.Position.Z + vz); - - // Geometric ray-sphere: oc = origin - center, b = dot(oc, dir), - // c = |oc|^2 - r^2, discriminant = b^2 - c. If discriminant < 0 - // the ray misses the sphere. Otherwise nearest intersection is - // t = -b - sqrt(discriminant). - var oc = origin - sphereCenter; - float b = Vector3.Dot(oc, direction); - float c = Vector3.Dot(oc, oc) - r2; - float d = b * b - c; - if (d < 0f) continue; - - // Two intersection roots: t_near = -b - sqrt(d), t_far = -b + sqrt(d). - // If t_near < 0 the ray origin is INSIDE the sphere; fall through - // to t_far so the entity is still pickable at point-blank range. - float sqrtD = MathF.Sqrt(d); - float t = -b - sqrtD; - if (t < 0f) t = -b + sqrtD; // origin inside sphere -> use far exit - if (t < 0f) continue; // both roots negative -> sphere entirely behind ray - if (t >= maxDistance) continue; - if (t < bestT) - { - bestT = t; - bestGuid = entity.ServerGuid; - } - } - return bestGuid; - } -} diff --git a/src/AcDream.Core/Terrain/LandblockMesh.cs b/src/AcDream.Core/Terrain/LandblockMesh.cs index 81e6724..573acf5 100644 --- a/src/AcDream.Core/Terrain/LandblockMesh.cs +++ b/src/AcDream.Core/Terrain/LandblockMesh.cs @@ -46,7 +46,7 @@ public static class LandblockMesh uint landblockY, float[] heightTable, TerrainBlendingContext ctx, - System.Collections.Generic.IDictionary surfaceCache) + Dictionary surfaceCache) { ArgumentNullException.ThrowIfNull(block); ArgumentNullException.ThrowIfNull(heightTable); @@ -105,10 +105,6 @@ public static class LandblockMesh uint palCode = TerrainBlending.GetPalCode( rBL, rBR, rTR, rTL, ttBL, ttBR, ttTR, ttTL); - // Lookup-or-build pattern. Not atomic under concurrent access - // (TryGetValue then assign), but BuildSurface is deterministic β€” - // two workers building the same palCode produce equal SurfaceInfo, - // last-write-wins is benign. if (!surfaceCache.TryGetValue(palCode, out var surf)) { surf = TerrainBlending.BuildSurface(palCode, ctx); diff --git a/src/AcDream.Core/Terrain/TerrainSlotAllocator.cs b/src/AcDream.Core/Terrain/TerrainSlotAllocator.cs deleted file mode 100644 index 1e86f21..0000000 --- a/src/AcDream.Core/Terrain/TerrainSlotAllocator.cs +++ /dev/null @@ -1,76 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace AcDream.Core.Terrain; - -/// -/// Pure-CPU slot allocator for the terrain modern dispatcher's global VBO/EBO. -/// One slot = one landblock's worth of mesh data (384 verts + 384 indices). -/// Uses a FIFO free-list for slot recycling and a monotonic counter for -/// first-time growth, mirroring WorldBuilder's TerrainRenderManager pattern. -/// All bookkeeping is CPU-side; the GPU buffer growth itself is performed -/// by TerrainModernRenderer when sets needsGrow=true. -/// -public sealed class TerrainSlotAllocator -{ - private readonly Queue _freeSlots = new(); - private readonly HashSet _liveSlots = new(); - private int _nextFreeSlot; - private int _capacity; - - public TerrainSlotAllocator(int initialCapacity = 64) - { - if (initialCapacity <= 0) - throw new ArgumentOutOfRangeException(nameof(initialCapacity), "must be > 0"); - _capacity = initialCapacity; - } - - /// Current capacity in slots. Growable via . - public int Capacity => _capacity; - - /// Slots currently in use (allocated minus freed). - public int LoadedCount => _liveSlots.Count; - - /// - /// Allocate a slot index. Reuses a freed slot via FIFO if available, - /// otherwise hands out the next monotonic index. Sets - /// to true when the returned slot index is - /// at or beyond current capacity β€” caller must - /// before using the slot. - /// - public int Allocate(out bool needsGrow) - { - int slot; - if (_freeSlots.TryDequeue(out var freed)) - { - slot = freed; - } - else - { - slot = _nextFreeSlot++; - } - _liveSlots.Add(slot); - needsGrow = slot >= _capacity; - return slot; - } - - /// - /// Return a slot to the free list. Throws if the slot wasn't currently - /// allocated (catches double-free bugs). - /// - public void Free(int slot) - { - if (!_liveSlots.Remove(slot)) - throw new InvalidOperationException( - $"Slot {slot} was not allocated (double-free or unknown slot)."); - _freeSlots.Enqueue(slot); - } - - /// Update capacity counter after the caller has grown the GPU buffers. - public void GrowTo(int newCapacity) - { - if (newCapacity < _capacity) - throw new ArgumentException("Capacity can only grow", nameof(newCapacity)); - _capacity = newCapacity; - } -} diff --git a/src/AcDream.Core/Textures/SurfaceDecoder.cs b/src/AcDream.Core/Textures/SurfaceDecoder.cs index 8b3158f..e48b9a4 100644 --- a/src/AcDream.Core/Textures/SurfaceDecoder.cs +++ b/src/AcDream.Core/Textures/SurfaceDecoder.cs @@ -1,6 +1,5 @@ using BCnEncoder.Decoder; using BCnEncoder.Shared; -using Chorizite.OpenGLSDLBackend.Lib; using DatReaderWriter.DBObjs; using DatReaderWriter.Enums; @@ -17,7 +16,7 @@ public static class SurfaceDecoder /// when a palette is available. /// public static DecodedTexture DecodeRenderSurface(RenderSurface rs) - => DecodeRenderSurface(rs, palette: null, isClipMap: false, isAdditive: false); + => DecodeRenderSurface(rs, palette: null); /// /// Decode a RenderSurface's pixel bytes into RGBA8 with optional palette support. @@ -25,11 +24,8 @@ public static class SurfaceDecoder /// 16-bit value in SourceData is treated as an index into . /// When is true on an indexed surface, palette indices /// below 8 are forced to fully-transparent (AC's clipmap alpha-key convention). - /// When is true, A8/CUSTOM_LSCAPE_ALPHA surfaces - /// replicate the byte into all four channels (R=G=B=A=val, for terrain alpha masks - /// and additive surfaces). When false, R=G=B=255, A=val (WB FillA8 semantics). /// - public static DecodedTexture DecodeRenderSurface(RenderSurface rs, Palette? palette, bool isClipMap = false, bool isAdditive = false) + public static DecodedTexture DecodeRenderSurface(RenderSurface rs, Palette? palette, bool isClipMap = false) { if (rs.SourceData is null || rs.Width <= 0 || rs.Height <= 0) return DecodedTexture.Magenta; @@ -44,11 +40,9 @@ public static class SurfaceDecoder PixelFormat.PFID_DXT1 => DecodeBc(rs, CompressionFormat.Bc1, isClipMap), PixelFormat.PFID_DXT3 => DecodeBc(rs, CompressionFormat.Bc2, isClipMap), PixelFormat.PFID_DXT5 => DecodeBc(rs, CompressionFormat.Bc3, isClipMap), - PixelFormat.PFID_A8 or PixelFormat.PFID_CUSTOM_LSCAPE_ALPHA => DecodeA8(rs, isAdditive), + PixelFormat.PFID_A8 or PixelFormat.PFID_CUSTOM_LSCAPE_ALPHA => DecodeA8(rs), PixelFormat.PFID_P8 when palette is not null => DecodeP8(rs, palette, isClipMap), PixelFormat.PFID_INDEX16 when palette is not null => DecodeIndex16(rs, palette, isClipMap), - PixelFormat.PFID_R5G6B5 => DecodeR5G6B5(rs), - PixelFormat.PFID_A4R4G4B4 => DecodeA4R4G4B4(rs), _ => DecodedTexture.Magenta, }; } @@ -65,7 +59,33 @@ public static class SurfaceDecoder return DecodedTexture.Magenta; var rgba = new byte[rs.Width * rs.Height * 4]; - TextureHelpers.FillIndex16(rs.SourceData, palette, rgba.AsSpan(), rs.Width, rs.Height, isClipMap); + int paletteMax = palette.Colors.Count - 1; + for (int i = 0; i < rs.Width * rs.Height; i++) + { + // Read each 16-bit value little-endian as a palette index + int src = i * 2; + ushort idx = (ushort)(rs.SourceData[src] | (rs.SourceData[src + 1] << 8)); + if (idx > paletteMax) idx = 0; + var c = palette.Colors[idx]; + + int dst = i * 4; + // Clipmap alpha-key convention (ACViewer: if (isClipMap && color < 8) r=g=b=a=0): + // palette indices 0..7 on clipmap surfaces represent transparent pixels. + if (isClipMap && idx < 8) + { + rgba[dst + 0] = 0; + rgba[dst + 1] = 0; + rgba[dst + 2] = 0; + rgba[dst + 3] = 0; + } + else + { + rgba[dst + 0] = c.Red; + rgba[dst + 1] = c.Green; + rgba[dst + 2] = c.Blue; + rgba[dst + 3] = c.Alpha; + } + } return new DecodedTexture(rgba, rs.Width, rs.Height); } @@ -89,22 +109,30 @@ public static class SurfaceDecoder } /// - /// Decode single-byte-per-pixel alpha (PFID_A8 / PFID_CUSTOM_LSCAPE_ALPHA) into RGBA8. - /// When is true: R=G=B=A=val (terrain alpha masks and - /// additive entity textures β€” the shader reads .r for the blend weight). When false: - /// R=G=B=255, A=val (WB FillA8 semantics for non-additive entity textures). + /// Decode single-byte-per-pixel alpha (PFID_A8 / PFID_CUSTOM_LSCAPE_ALPHA) + /// into RGBA8 by replicating each alpha byte into all four channels. AC's + /// terrain blending alpha masks are stored as PFID_CUSTOM_LSCAPE_ALPHA and + /// other generic 8-bit alpha surfaces use PFID_A8; the bit layout is + /// identical so one decoder handles both. Replicating into all four + /// channels lets the fragment shader pull "the blend amount" from either + /// .a or .r without special-casing. /// - private static DecodedTexture DecodeA8(RenderSurface rs, bool isAdditive) + private static DecodedTexture DecodeA8(RenderSurface rs) { int expected = rs.Width * rs.Height; if (rs.SourceData.Length < expected) return DecodedTexture.Magenta; var rgba = new byte[expected * 4]; - if (isAdditive) - TextureHelpers.FillA8Additive(rs.SourceData, rgba.AsSpan(), rs.Width, rs.Height); - else - TextureHelpers.FillA8(rs.SourceData, rgba.AsSpan(), rs.Width, rs.Height); + for (int i = 0; i < expected; i++) + { + byte a = rs.SourceData[i]; + int d = i * 4; + rgba[d + 0] = a; + rgba[d + 1] = a; + rgba[d + 2] = a; + rgba[d + 3] = a; + } return new DecodedTexture(rgba, rs.Width, rs.Height); } @@ -115,7 +143,15 @@ public static class SurfaceDecoder return DecodedTexture.Magenta; var rgba = new byte[expected]; - TextureHelpers.FillA8R8G8B8(rs.SourceData, rgba.AsSpan(), rs.Width, rs.Height); + // Source layout per pixel: B, G, R, A β†’ swap to R, G, B, A + for (int i = 0; i < rs.Width * rs.Height; i++) + { + int s = i * 4; + rgba[s + 0] = rs.SourceData[s + 2]; // R <- R + rgba[s + 1] = rs.SourceData[s + 1]; // G <- G + rgba[s + 2] = rs.SourceData[s + 0]; // B <- B + rgba[s + 3] = rs.SourceData[s + 3]; // A <- A + } return new DecodedTexture(rgba, rs.Width, rs.Height); } @@ -132,7 +168,29 @@ public static class SurfaceDecoder return DecodedTexture.Magenta; var rgba = new byte[rs.Width * rs.Height * 4]; - TextureHelpers.FillP8(rs.SourceData, palette, rgba.AsSpan(), rs.Width, rs.Height, isClipMap); + int paletteMax = palette.Colors.Count - 1; + for (int i = 0; i < rs.Width * rs.Height; i++) + { + int idx = rs.SourceData[i]; + if (idx > paletteMax) idx = 0; + var c = palette.Colors[idx]; + + int dst = i * 4; + if (isClipMap && idx < 8) + { + rgba[dst + 0] = 0; + rgba[dst + 1] = 0; + rgba[dst + 2] = 0; + rgba[dst + 3] = 0; + } + else + { + rgba[dst + 0] = c.Red; + rgba[dst + 1] = c.Green; + rgba[dst + 2] = c.Blue; + rgba[dst + 3] = c.Alpha; + } + } return new DecodedTexture(rgba, rs.Width, rs.Height); } @@ -149,7 +207,16 @@ public static class SurfaceDecoder return DecodedTexture.Magenta; var rgba = new byte[rs.Width * rs.Height * 4]; - TextureHelpers.FillR8G8B8(rs.SourceData, rgba.AsSpan(), rs.Width, rs.Height); + for (int i = 0; i < rs.Width * rs.Height; i++) + { + int src = i * 3; + int dst = i * 4; + // On-disk byte order: B, G, R (little-endian 24-bit BGR, same as DX PFID_R8G8B8) + rgba[dst + 0] = rs.SourceData[src + 2]; // R + rgba[dst + 1] = rs.SourceData[src + 1]; // G + rgba[dst + 2] = rs.SourceData[src + 0]; // B + rgba[dst + 3] = 0xFF; // A = opaque + } return new DecodedTexture(rgba, rs.Width, rs.Height); } @@ -178,28 +245,6 @@ public static class SurfaceDecoder return new DecodedTexture(rgba, rs.Width, rs.Height); } - private static DecodedTexture DecodeR5G6B5(RenderSurface rs) - { - int expectedBytes = rs.Width * rs.Height * 2; - if (rs.SourceData.Length < expectedBytes) - return DecodedTexture.Magenta; - - var rgba = new byte[rs.Width * rs.Height * 4]; - TextureHelpers.FillR5G6B5(rs.SourceData, rgba.AsSpan(), rs.Width, rs.Height); - return new DecodedTexture(rgba, rs.Width, rs.Height); - } - - private static DecodedTexture DecodeA4R4G4B4(RenderSurface rs) - { - int expectedBytes = rs.Width * rs.Height * 2; - if (rs.SourceData.Length < expectedBytes) - return DecodedTexture.Magenta; - - var rgba = new byte[rs.Width * rs.Height * 4]; - TextureHelpers.FillA4R4G4B4(rs.SourceData, rgba.AsSpan(), rs.Width, rs.Height); - return new DecodedTexture(rgba, rs.Width, rs.Height); - } - private static DecodedTexture DecodeBc(RenderSurface rs, CompressionFormat format, bool isClipMap) { var pixels = BcDecoder.DecodeRaw(rs.SourceData, rs.Width, rs.Height, format); diff --git a/src/AcDream.Core/Ui/RadarBlipColors.cs b/src/AcDream.Core/Ui/RadarBlipColors.cs deleted file mode 100644 index 685f156..0000000 --- a/src/AcDream.Core/Ui/RadarBlipColors.cs +++ /dev/null @@ -1,93 +0,0 @@ -using AcDream.Core.Items; - -namespace AcDream.Core.Ui; - -/// -/// B.7 (2026-05-15) β€” port of retail's gmRadarUI::GetBlipColor -/// (named decomp 0x004d76f0). Returns the radar / target-indicator -/// colour for an entity based on its and the -/// raw PublicWeenieDesc._bitfield we already parse out of -/// CreateObject. -/// -/// -/// Used by the Vivid Target Indicator (Phase B.7) to colour the four -/// corner triangles around the selected entity. Same value retail -/// would have shown on the radar blip β€” so the indicator + radar agree. -/// -/// -/// -/// Dispatch order matches the retail decomp at lines 219913+ of -/// docs/research/named-retail/acclient_2013_pseudo_c.txt. The -/// PWD bit layout matches acclient.h:6431-6463: -/// -/// BF_PLAYER = 0x8 -/// BF_PLAYER_KILLER = 0x20 -/// BF_VENDOR = 0x200 (byte[1] & 0x02 in retail) -/// BF_PORTAL = 0x40000 -/// BF_FREE_PKSTATUS = 0x200000 (hostile-flagged player) -/// BF_PKLITE_PKSTATUS = 0x2000000 -/// -/// -/// -/// -/// RGBA values are hand-tuned to visually match retail screenshots -/// (yellow creature, red PK, pink PKLite, green vendor, cyan portal, -/// white default). Real RGBAColor_Radar* constants live in retail -/// static data β€” if they're ever recovered the table can be tightened. -/// -/// -public static class RadarBlipColors -{ - public readonly record struct Rgba(byte R, byte G, byte B, byte A); - - public static readonly Rgba Item = new(220, 220, 220, 255); // light grey (default object) - public static readonly Rgba Default = new(255, 255, 255, 255); // white (friendly player) - public static readonly Rgba Creature = new(255, 220, 80, 255); // yellow (NPC / monster) - public static readonly Rgba PlayerKiller = new(255, 64, 64, 255); // red (PK) - public static readonly Rgba PKLite = new(255, 128, 192, 255); // pink (PKLite) - public static readonly Rgba Vendor = new( 64, 192, 64, 255); // green (vendor NPC) - public static readonly Rgba Portal = new( 64, 192, 255, 255); // cyan (portal) - - /// - /// Resolve the radar-blip colour for an entity. Caller supplies the - /// raw (from CreateObject.ItemType) and - /// (from - /// CreateObject.ObjectDescriptionFlags) β€” both are already - /// parsed and stashed on EntitySpawn at spawn time. - /// - /// - /// Returns for friendly players, - /// for NPCs/monsters, for everything else. - /// Special types (PK, PKLite, Vendor, Portal) win over the base - /// type when their flag is set. - /// - /// - public static Rgba For(uint itemType, uint pwdBitfield) - { - // Special-type early returns. Order matches retail dispatch - // (Portal first, then Vendor) β€” same target can't logically - // be both, but in case of bit collision retail's order wins. - if ((pwdBitfield & 0x40000u) != 0) return Portal; - if ((pwdBitfield & 0x200u) != 0) return Vendor; - - bool isCreature = (itemType & (uint)ItemType.Creature) != 0; - bool isPlayer = (pwdBitfield & 0x8u) != 0; - - // Creature that isn't a player β†’ NPC / monster β†’ yellow. - if (isCreature && !isPlayer) - return Creature; - - if (isPlayer) - { - bool isPK = (pwdBitfield & 0x20u) != 0; - bool isPKLite = (pwdBitfield & 0x2000000u) != 0; - if (isPK) return PlayerKiller; - if (isPKLite) return PKLite; - return Default; - } - - // Not a special type, not a creature, not a player β†’ an item / - // object on the ground or in the world. - return Item; - } -} diff --git a/src/AcDream.Core/Ui/RetailMessages.cs b/src/AcDream.Core/Ui/RetailMessages.cs deleted file mode 100644 index 5689705..0000000 --- a/src/AcDream.Core/Ui/RetailMessages.cs +++ /dev/null @@ -1,105 +0,0 @@ -namespace AcDream.Core.Ui; - -/// -/// Verbatim ports of retail UI message strings. Centralised here so -/// future retail-faithful refinements only need to touch one file β€” -/// and so the call sites stay readable at the interaction layer. -/// -/// -/// String text is byte-identical with retail. Each helper cites the -/// retail DAT data address + the runtime use site in the named decomp -/// at docs/research/named-retail/acclient_2013_pseudo_c.txt. -/// -/// -/// -/// Pattern mirrors β€” typed port of a -/// retail-UI primitive (gmRadarUI::GetBlipColor at -/// 0x004d76f0). Add new strings here as we encounter them. -/// -/// -/// -/// Members may be added BEFORE their first call site exists β€” retail -/// strings are a fixed inventory we know we'll need as we port more -/// features. Each member's doc-comment cites its retail anchor + -/// describes the scenario that'll consume it. Removing dead members -/// without a port is fine; we re-grep the decomp. -/// -/// -public static class RetailMessages -{ - /// - /// Retail: "The %s cannot be used". - /// Data: 0x007e2a70 (line 1033115). Runtime sprintf at - /// 0x00588ea4 (line 403095) inside ItemHolder::UseObject's - /// IsUseable==0 fallthrough branch. Shown when the player triggers - /// Use on an entity whose useability is USEABLE_UNDEF/USEABLE_NO. - /// - public static string CannotBeUsed(string entityName) - => $"The {entityName} cannot be used"; - - /// - /// Retail: "The %s can't be picked up!". - /// Runtime sprintf at 0x00587353 (line 401589) inside the - /// pickup-flow handler. Shown when the player triggers a pickup on - /// an entity that lacks USEABLE_REMOTE / isn't a small-item type. - /// - public static string CantBePickedUp(string entityName) - => $"The {entityName} can't be picked up!"; - - /// - /// Retail: "You cannot pick up creatures!". - /// Data: 0x007e22b4 (line 1033034). Runtime use at - /// 0x005871f4 (line 401642) inside the same pickup-flow - /// handler. Shown when the player triggers a pickup on a Creature - /// ItemType (NPCs, monsters, other players). - /// - public const string CannotPickUpCreatures = "You cannot pick up creatures!"; - - /// - /// Retail: "Cannot be used with %s". - /// Data: 0x007cc834 (line 1024669). Runtime sprintf at - /// 0x0055ee0e (line 363413). Shown when the player tries - /// a two-target Use (e.g., key on lock, lockpick on chest) and - /// the combination is invalid for the source item. The %s - /// is the TARGET entity name. No call site yet β€” wired in when - /// the two-target Use flow ships. - /// - public static string CannotBeUsedWith(string targetName) - => $"Cannot be used with {targetName}"; - - /// - /// Retail: "The %s cannot be picked up!". FORMAL variant. - /// Data: 0x007e227c (line 1033033). Runtime sprintf at - /// 0x00587264 (line 401623). Distinct from - /// β€” retail has TWO pickup- - /// reject strings (formal "cannot" + informal "can't"); they - /// fire from different code paths inside the pickup handler. - /// Use whichever the corresponding caller's retail path uses. - /// No call site yet β€” wired in when the formal-pickup-reject - /// path ships (probably a server-side rejection message). - /// - public static string CannotBePickedUp(string entityName) - => $"The {entityName} cannot be picked up!"; - - /// - /// Retail: "The %s cannot be used while on a hook, use the - /// '@house hooks on' command to make the hook openable.\n". - /// Data: 0x007d1f68 (line 1029591). Shown when the player - /// tries to Use a hooked-up item with the house's "hooks off" - /// preference set. Trailing newline matches retail. No call - /// site yet β€” wired in when the housing system ships. - /// - public static string CannotBeUsedWhileOnHook_HooksOff(string entityName) - => $"The {entityName} cannot be used while on a hook, use the '@house hooks on' command to make the hook openable.\n"; - - /// - /// Retail: "The %s cannot be used while on a hook and only - /// the owner may open the hook.\n". - /// Data: 0x007d5f30 (line 1030063). Shown when a non-owner - /// tries to Use a hooked-up item in someone else's house. - /// Trailing newline matches retail. No call site yet β€” wired in - /// when the housing system ships. - /// - public static string CannotBeUsedWhileOnHook_NotOwner(string entityName) - => $"The {entityName} cannot be used while on a hook and only the owner may open the hook.\n"; -} diff --git a/src/AcDream.Core/Vfx/ParticleHookSink.cs b/src/AcDream.Core/Vfx/ParticleHookSink.cs index 737b9dc..bfb47e1 100644 --- a/src/AcDream.Core/Vfx/ParticleHookSink.cs +++ b/src/AcDream.Core/Vfx/ParticleHookSink.cs @@ -72,14 +72,6 @@ public sealed class ParticleHookSink : IAnimationHookSink private readonly ConcurrentDictionary _trackingByHandle = new(); private readonly ConcurrentDictionary _renderPassByEntity = new(); private readonly ConcurrentDictionary _rotationByEntity = new(); - // C.1.5b #56: per-entity static part transforms (PlacementFrames[Resting] - // baked into a Matrix4x4 per Setup part). When set, SpawnFromHook applies - // partTransforms[hook.PartIndex] to the hook offset BEFORE rotating to - // world space. Without this, every emitter in a multi-part Setup - // collapses to the entity root (the bug). Cleared by StopAllForEntity. - // For ANIMATED entities this map would need a per-tick refresh similar - // to UpdateEntityAnchor β€” deferred to a future phase. - private readonly ConcurrentDictionary> _partTransformsByEntity = new(); private int _anonymousEmitterSerial; public ParticleHookSink(ParticleSystem system) @@ -139,19 +131,6 @@ public sealed class ParticleHookSink : IAnimationHookSink public void SetEntityRotation(uint entityId, Quaternion rotation) => _rotationByEntity[entityId] = rotation; - /// - /// Register per-part static transforms for an entity. The caller - /// (typically EntityScriptActivator) precomputes one - /// per Setup part using - /// SetupPartTransforms.Compute and pushes them here at spawn - /// time. applies - /// partTransforms[hook.PartIndex] to the hook offset BEFORE - /// transforming to world space. Cleared on - /// . - /// - public void SetEntityPartTransforms(uint entityId, IReadOnlyList partTransforms) - => _partTransformsByEntity[entityId] = partTransforms; - public void ClearEntityRenderPass(uint entityId) => _renderPassByEntity.TryRemove(entityId, out _); @@ -192,7 +171,6 @@ public sealed class ParticleHookSink : IAnimationHookSink ClearEntityRenderPass(entityId); _rotationByEntity.TryRemove(entityId, out _); - _partTransformsByEntity.TryRemove(entityId, out _); } private void SpawnFromHook( @@ -203,22 +181,13 @@ public sealed class ParticleHookSink : IAnimationHookSink int partIndex, uint logicalId) { - // Spawn position: entity pose + hook offset, with the hook - // offset first passed through the per-part transform when - // available (C.1.5b #56 fix). Without the per-part transform, - // every emitter in a multi-emitter PES script collapses to the - // entity root β€” visible symptom: ground-buried portal swirls. + // Spawn position: entity pose + hook offset. PartIndex will be + // used when the renderer passes per-part transforms through; for + // now, fold it into the root pos. var rotation = _rotationByEntity.TryGetValue(entityId, out var rot) ? rot : Quaternion.Identity; - Vector3 partLocal = offset; - if (_partTransformsByEntity.TryGetValue(entityId, out var partTransforms) - && partIndex >= 0 - && partIndex < partTransforms.Count) - { - partLocal = Vector3.Transform(offset, partTransforms[partIndex]); - } - var anchor = worldPos + Vector3.Transform(partLocal, rotation); + var anchor = worldPos + Vector3.Transform(offset, rotation); var renderPass = _renderPassByEntity.TryGetValue(entityId, out var pass) ? pass : ParticleRenderPass.Scene; diff --git a/src/AcDream.Core/World/LandblockLoader.cs b/src/AcDream.Core/World/LandblockLoader.cs index b18608a..4234c11 100644 --- a/src/AcDream.Core/World/LandblockLoader.cs +++ b/src/AcDream.Core/World/LandblockLoader.cs @@ -22,7 +22,7 @@ public static class LandblockLoader var info = dats.Get((landblockId & 0xFFFF0000u) | 0xFFFEu); var entities = info is null ? Array.Empty() - : BuildEntitiesFromInfo(info, landblockId); + : BuildEntitiesFromInfo(info); return new LoadedLandblock(landblockId, block, entities); } @@ -33,58 +33,37 @@ public static class LandblockLoader /// (neither GfxObj 0x01xxxxxx nor Setup 0x02xxxxxx) are silently skipped. /// MeshRefs is left empty at this stage β€” Task 5 populates it. /// - public static IReadOnlyList BuildEntitiesFromInfo(LandBlockInfo info, uint landblockId = 0) + public static IReadOnlyList BuildEntitiesFromInfo(LandBlockInfo info) { var result = new List(info.Objects.Count + info.Buildings.Count); - - // When landblockId is non-zero, namespace stab Ids globally: - // 0xC0XXYY00 + n, where XX = lbX byte, YY = lbY byte - // matching the scenery (0x80XXYY00) and interior (0x40XXYY00) patterns - // in GameWindow.cs. The 0xC0 top byte distinguishes stabs from those. - // - // Pre-Tier-1 callers (existing tests) pass landblockId=0 and get the - // legacy starting-from-1 monotonic Ids β€” compatible with their assertions - // which check uniqueness within a single landblock. - // - // Latent: if a landblock has >256 stabs (rare), nextId overflows the - // low byte and bleeds into the lbY byte β†’ cross-LB collision. Same - // pattern + same limitation as scenery/interior. Document but don't - // fix in this commit β€” out of scope for the Tier 1 cache bug fix. - uint stabIdBase = landblockId == 0 - ? 0u - : 0xC0000000u | ((landblockId >> 24) & 0xFFu) << 16 | ((landblockId >> 16) & 0xFFu) << 8; - uint nextId = stabIdBase == 0 ? 1u : stabIdBase + 1u; + uint nextId = 1; foreach (var stab in info.Objects) { if (!IsSupported(stab.Id)) continue; - var stabEntity = new WorldEntity + result.Add(new WorldEntity { Id = nextId++, SourceGfxObjOrSetupId = stab.Id, Position = stab.Frame.Origin, Rotation = stab.Frame.Orientation, MeshRefs = Array.Empty(), - }; - stabEntity.RefreshAabb(); // A.5 T18: populate cached AABB at construction - result.Add(stabEntity); + }); } foreach (var building in info.Buildings) { if (!IsSupported(building.ModelId)) continue; - var buildingEntity = new WorldEntity + result.Add(new WorldEntity { Id = nextId++, SourceGfxObjOrSetupId = building.ModelId, Position = building.Frame.Origin, Rotation = building.Frame.Orientation, MeshRefs = Array.Empty(), - }; - buildingEntity.RefreshAabb(); // A.5 T18: populate cached AABB at construction - result.Add(buildingEntity); + }); } return result; diff --git a/src/AcDream.Core/World/SceneryGenerator.cs b/src/AcDream.Core/World/SceneryGenerator.cs index b306e41..5c88128 100644 --- a/src/AcDream.Core/World/SceneryGenerator.cs +++ b/src/AcDream.Core/World/SceneryGenerator.cs @@ -1,9 +1,7 @@ using System.Numerics; -using Chorizite.OpenGLSDLBackend.Lib; using DatReaderWriter; using DatReaderWriter.DBObjs; using DatReaderWriter.Types; -using WorldBuilder.Shared.Modules.Landscape.Lib; namespace AcDream.Core.World; @@ -25,11 +23,17 @@ namespace AcDream.Core.World; /// (scale hash constant 0x7f51=32593 not in dumped chunks; /// confirmed against ACViewer which matches all other constants) /// -/// Phase N.1 (2026-05-08): migrated all algorithm calls to WorldBuilder's -/// SceneryHelpers + TerrainUtils. The legacy in-line implementations -/// have been removed; WbSceneryAdapter bridges LandBlock data to WB's -/// TerrainEntry[]. See -/// docs/superpowers/specs/2026-05-08-phase-n1-scenery-via-wb-helpers-design.md. +/// Key implementation note: the decompiled client computes each LCG value as a +/// signed 32-bit int, then normalises with "if (val < 0) val += 2^32" before +/// dividing by 2^32. This is equivalent to our unchecked((uint)(...)) cast. +/// ACViewer's reference omits this cast and is subtly wrong for negative inputs. +/// We deliberately match the decompiled client, not ACViewer. +/// +/// We deliberately skip the slope/road/building-overlap checks the original does; +/// those prevent scenery from floating in roads or clipping buildings but +/// require walkable-polygon lookups that we don't yet have. Accepting visual +/// artifacts (trees inside roads, scenery clipping buildings) for a first pass +/// and deferring the filters to a later phase. /// public static class SceneryGenerator { @@ -37,7 +41,6 @@ public static class SceneryGenerator private const int VerticesPerSide = 9; private const float CellSize = 24.0f; private const float LandblockSize = 192.0f; // 8 cells * 24 units - private const int CellsPerSide = 8; public readonly record struct ScenerySpawn( uint ObjectId, // GfxObj or Setup id @@ -46,9 +49,12 @@ public static class SceneryGenerator float Scale); /// - /// Generate all scenery entries for one landblock. Phase N.1 migrated this - /// to call WorldBuilder's SceneryHelpers + TerrainUtils; - /// see docs/superpowers/specs/2026-05-08-phase-n1-scenery-via-wb-helpers-design.md. + /// Generate all scenery entries for one landblock. Uses the bit-packed + /// TerrainInfo Type (bits 2-6) and Scenery (bits 11-15) fields to index into + /// Region.TerrainInfo.TerrainTypes[type].SceneTypes[scenery] β†’ a SceneInfo + /// index into Region.SceneInfo.SceneTypes[sceneInfo].Scenes. Each cell picks + /// one scene via a pseudo-random hash of the cell's global coordinates, then + /// iterates the scene's ObjectDesc entries with per-object frequency rolls. /// public static IReadOnlyList Generate( DatCollection dats, @@ -57,49 +63,37 @@ public static class SceneryGenerator uint landblockId, HashSet? buildingCells = null, float[]? heightTable = null) - { - // heightTable kept for backward compat; WB path uses - // region.LandDefs.LandHeightTable internally via TerrainUtils.GetNormal. - _ = heightTable; - return GenerateInternal(dats, region, block, landblockId, buildingCells); - } - - /// - /// Returns true if the raw terrain word indicates a road vertex. - /// Bits 0-1 of the terrain word encode the road type; any non-zero value - /// means the vertex is on a road. Ported from ACViewer GetRoad(). - /// - public static bool IsRoadVertex(ushort raw) => (raw & 0x3u) != 0; - - private static IReadOnlyList GenerateInternal( - DatCollection dats, - Region region, - LandBlock block, - uint landblockId, - HashSet? buildingCells) { var result = new List(); if (region.TerrainInfo?.TerrainTypes is null || region.SceneInfo?.SceneTypes is null) return result; - // Build the TerrainEntry[] WB's helpers consume β€” once per landblock. - var terrainEntries = WbSceneryAdapter.BuildTerrainEntries(block); - - uint blockX = (landblockId >> 24) * 8; + uint blockX = (landblockId >> 24) * 8; // 8 cells per landblock uint blockY = ((landblockId >> 16) & 0xFFu) * 8; - uint lbX = landblockId >> 24; - uint lbY = (landblockId >> 16) & 0xFFu; - for (int x = 0; x < VerticesPerSide; x++) + // RETAIL iterates 8Γ—8 = 64 CELLS, not 9Γ—9 = 81 vertices. + // Decompiled FUN_005311a0 at chunk_00530000.c:1123-1253 uses + // `while (local_94 < 8)` and `while (local_8c < 8)` β€” bound by + // `param_1+0x40` which is SideCellCount=8 for outdoor landblocks. + // The terrain word at each cell's SW corner drives that cell's scenery. + for (int x = 0; x < CellsPerSide; x++) { - for (int y = 0; y < VerticesPerSide; y++) + for (int y = 0; y < CellsPerSide; y++) { int i = x * VerticesPerSide + y; ushort raw = block.Terrain[i]; - uint terrainType = (uint)((raw >> 2) & 0x1F); - uint sceneType = (uint)((raw >> 11) & 0x1F); + uint terrainType = (uint)((raw >> 2) & 0x1F); // bits 2-6 + uint sceneType = (uint)((raw >> 11) & 0x1F); // bits 11-15 + + // NOTE: retail does NOT skip based on this vertex's road bit. + // The road test happens AFTER displacement via the 4-corner + // polygonal OnRoad check (see below). Removing the + // pre-displacement early-exit restores retail behavior. + + // Skip cells that contain buildings. + if (buildingCells is not null && buildingCells.Contains(i)) continue; if (terrainType >= region.TerrainInfo.TerrainTypes.Count) continue; var sceneTypeList = region.TerrainInfo.TerrainTypes[(int)terrainType].SceneTypes; @@ -116,7 +110,10 @@ public static class SceneryGenerator uint globalCellX = cellX + blockX; uint globalCellY = cellY + blockY; - // Scene-selection hash: identical to Generate. + // Scene-selection hash: picks one scene from the terrain's scene list. + // Decompiled: chunk_00530000.c line 1144 + // iVar5 = (iVar8 * 0x2a7f2b89 + 0x6c1ac587) * iVar9 + iVar8 * -0x421be3bd + 0x7f8cda01 + // where iVar8=globalCellX, iVar9=globalCellY. uint cellMat = globalCellY * (712977289u * globalCellX + 1813693831u) - 1109124029u * globalCellX + 2139937281u; double offset = cellMat * 2.3283064e-10; @@ -127,7 +124,14 @@ public static class SceneryGenerator var scene = dats.Get(sceneId); if (scene is null) continue; - // Per-object frequency setup: identical to Generate. + // Per-object hashes: roll frequency, compute displacement, scale, rotation. + // Decompiled: chunk_00530000.c lines 1168-1174 + // iStack_60 = iVar9 * 0x6c1ac587 β†’ cellYMat + // uStack_78 = iVar9 * iVar8 * 0x5111bfef + 0x70892fb7 β†’ cellMat2 + // iStack_64 = iVar8 * -0x421be3bd β†’ cellXMat + // initial: local_90 = uStack_78 * 0x5b67 (j=0 term) + // per-loop: iStack_70 = (iStack_60 - local_90) + iStack_64; local_90 += uStack_78 + // ⟹ iStack_70 = cellYMat - cellMat2 * (0x5b67 + j) + cellXMat uint cellXMat = unchecked(0u - 1109124029u * globalCellX); uint cellYMat = 1813693831u * globalCellY; uint cellMat2 = 1360117743u * globalCellX * globalCellY + 1888038839u; @@ -135,13 +139,15 @@ public static class SceneryGenerator for (uint j = 0; j < scene.Objects.Count; j++) { var obj = scene.Objects[(int)j]; - if (obj.WeenieObj != 0) continue; + if (obj.WeenieObj != 0) continue; // Weenie entries are dynamic spawns, not static scenery + // Frequency roll: chunk_00530000.c line 1174 + 1179 + // (fVar1 * _DAT_007c6f10 < (float)piVar11[0x11]) β†’ noise < obj.Frequency double noise = unchecked((uint)(cellXMat + cellYMat - cellMat2 * (23399u + j))) * 2.3283064e-10; if (noise >= obj.Frequency) continue; - // ─── WB substitution: displacement ─────────────────── - var localPos = SceneryHelpers.Displace(obj, globalCellX, globalCellY, j); + // Displacement: pseudo-random offset within the cell. + var localPos = DisplaceObject(obj, globalCellX, globalCellY, j); float lx = cellX * CellSize + localPos.X; float ly = cellY * CellSize + localPos.Y; @@ -149,48 +155,277 @@ public static class SceneryGenerator if (lx < 0 || ly < 0 || lx >= LandblockSize || ly >= LandblockSize) continue; - // ─── WB substitution: road check ────────────────────── - if (TerrainUtils.OnRoad(new Vector3(lx, ly, 0), terrainEntries)) - continue; - - // Building check: identical to Generate. - if (buildingCells is not null) + // Retail post-displacement road check (FUN_00530d30). + // Ported from ACViewer Landblock.OnRoad β€” uses the 4-corner + // road bits of the containing cell plus the 5-unit road + // half-width to test whether the displaced (lx,ly) lies on + // the road ribbon. + bool isOnRoad = IsOnRoad(block, lx, ly); + if (isOnRoad) { - int dcx = Math.Clamp((int)(lx / CellSize), 0, CellsPerSide - 1); - int dcy = Math.Clamp((int)(ly / CellSize), 0, CellsPerSide - 1); - if (buildingCells.Contains(dcx * VerticesPerSide + dcy)) - continue; + continue; } - // ─── WB substitution: slope check ───────────────────── - Vector3 normal = TerrainUtils.GetNormal( - region, terrainEntries, lbX, lbY, - new Vector3(lx, ly, 0)); - if (!SceneryHelpers.CheckSlope(obj, normal.Z)) - continue; + // L-fix2 (2026-04-28): the extra cell-origin road-vertex + // guard previously here is REMOVED. It wasn't in the + // retail decomp β€” it was a heuristic added to widen + // road margins visually. The proper retail post- + // displacement road check (FUN_00530d30 port via + // IsOnRoad above) already handles road exclusion. + // The extra guard was over-suppressing β€” every cell + // whose SW corner happened to touch a road vertex + // had ALL of its scenery dropped, even when the + // displaced position was well clear of the ribbon. + // User reported missing trees they could see in + // retail; this is the most likely cause. + // Slope filter (ACME conformance fix 4e): compute terrain normal + // Z-component at the displaced position and check against the + // object's MinSlope/MaxSlope bounds. + if (heightTable is not null && (obj.MinSlope > 0f || obj.MaxSlope < 1f)) + { + int sx = Math.Clamp((int)(lx / CellSize), 0, VerticesPerSide - 2); + int sy = Math.Clamp((int)(ly / CellSize), 0, VerticesPerSide - 2); + int sxR = sx + 1; + int syU = sy + 1; + float h00 = heightTable[block.Height[sx * VerticesPerSide + sy]]; + float h10 = heightTable[block.Height[sxR * VerticesPerSide + sy]]; + float h01 = heightTable[block.Height[sx * VerticesPerSide + syU]]; + float dx = (h10 - h00) / CellSize; + float dy = (h01 - h00) / CellSize; + float nz = 1f / MathF.Sqrt(dx * dx + dy * dy + 1f); // normal Z component + if (nz < obj.MinSlope || nz > obj.MaxSlope) continue; + } + + // BaseLoc.Z offset: scenery-specific vertical offset from + // the ground (e.g., flowers planted at -0.1m so they + // don't float above grass). The renderer adds groundZ + // later, so pass the BaseLoc.Z through as-is. float lz = obj.BaseLoc.Origin.Z; - // ─── WB substitution: rotation ──────────────────────── - Quaternion rotation; - if (obj.Align != 0) - rotation = SceneryHelpers.ObjAlign(obj, normal, lz, localPos); - else - rotation = SceneryHelpers.RotateObj(obj, globalCellX, globalCellY, j, localPos); + // Rotation: chunk_005A0000.c lines 4924-4931 (FUN_005a6e60) + // Retail calls FUN_00425f10(baseLoc) to copy baseLoc.Orientation + // into the frame, THEN calls AFrame::set_heading(degrees). + // + // set_heading uses yaw = -(450 - heading) % 360 before converting + // to a quaternion, which introduces a 90Β° offset + sign flip + // relative to a naive Z rotation. WorldBuilder's + // SceneryHelpers.SetHeading reproduces this. + // + // For objects with Align != 0, retail uses FUN_005a6f60 to + // align to the landcell polygon's normal instead of setting + // heading from the noise. + // + // Composition: final = baseLoc.Orientation * headingQuat + Quaternion rotation = obj.BaseLoc.Orientation; + if (rotation.LengthSquared() < 0.0001f) + rotation = Quaternion.Identity; - // ─── WB substitution: scale ─────────────────────────── - float scale = SceneryHelpers.ScaleObj(obj, globalCellX, globalCellY, j); + if (obj.MaxRotation > 0f) + { + double rotNoise = unchecked((uint)(1813693831u * globalCellY + - (j + 63127u) * (1360117743u * globalCellY * globalCellX + 1888038839u) + - 1109124029u * globalCellX)) * 2.3283064e-10; + float degrees = (float)(rotNoise * obj.MaxRotation); + // AFrame::set_heading transform β€” matches retail. + float yawDeg = -((450f - degrees) % 360f); + float yawRad = yawDeg * MathF.PI / 180f; + var headingQuat = Quaternion.CreateFromAxisAngle(Vector3.UnitZ, yawRad); + rotation = headingQuat * rotation; + } + + // Scale: ACViewer Physics/Common/ObjectDesc.cs ScaleObj() (confirmed matches pattern) + // offset constant 0x7f51 = 32593 (not in dumped chunks; cross-verified via ACViewer) + // same LCG structure as rotation/displacement; uint cast per decompiled normalisation + float scale; + if (obj.MinScale == obj.MaxScale) + { + scale = obj.MaxScale; + } + else + { + double scaleNoise = unchecked((uint)(1813693831u * globalCellY + - (j + 32593u) * (1360117743u * globalCellY * globalCellX + 1888038839u) + - 1109124029u * globalCellX)) * 2.3283064e-10; + scale = (float)(Math.Pow(obj.MaxScale / obj.MinScale, scaleNoise) * obj.MinScale); + } if (scale <= 0) scale = 1f; result.Add(new ScenerySpawn( - ObjectId: obj.ObjectId, + ObjectId: obj.ObjectId, LocalPosition: new Vector3(lx, ly, lz), - Rotation: rotation, - Scale: scale)); + Rotation: rotation, + Scale: scale)); } } } return result; } + + /// + /// Returns true if the raw terrain word indicates a road vertex. + /// Bits 0-1 of the terrain word encode the road type; any non-zero value + /// means the vertex is on a road. Ported from ACViewer GetRoad(). + /// + public static bool IsRoadVertex(ushort raw) => (raw & 0x3u) != 0; + + /// + /// Half-width of a road ribbon in world units β€” the road extends from each + /// road vertex by this amount into the neighbor cells. Matches retail's + /// `_DAT_007c9cc0 = 5.0f` in FUN_00530d30. + /// + private const float RoadHalfWidth = 5.0f; + + /// + /// Retail-faithful post-displacement road test. Ported from ACViewer + /// Landblock.OnRoad (Physics/Common/Landblock.cs lines 300-398), which is + /// a direct port of FUN_00530d30 in the retail client. + /// + /// Examines the 4 corners of the cell containing (lx, ly) and, depending + /// on how many are road vertices (0, 1, 2, 3, or 4), applies a polygonal + /// test using the 5-unit road half-width to check if (lx, ly) lies on the + /// road ribbon. Returns true if the point is on a road. + /// + /// + /// Retail-faithful road ribbon test β€” direct port of ACViewer's + /// Landblock.OnRoad (Physics/Common/Landblock.cs lines 300-398), which + /// itself is a port of FUN_00530d30 in acclient.exe. + /// + /// Classifies the 4 corners of the cell containing (lx, ly) by road type + /// (bits 0-1 of the terrain word) and applies a different geometric test + /// based on which corners are road vertices. Road ribbons have a 5m + /// half-width (TileLength - RoadWidth = 19m). + /// + private static bool IsOnRoad(LandBlock block, float lx, float ly) + { + int x = (int)MathF.Floor(lx / CellSize); + int y = (int)MathF.Floor(ly / CellSize); + // Clamp so we don't index past the 9x9 terrain grid + x = Math.Clamp(x, 0, CellsPerSide - 1); + y = Math.Clamp(y, 0, CellsPerSide - 1); + + float rMin = RoadHalfWidth; // 5 + float rMax = CellSize - RoadHalfWidth; // 19 + + // Corner road bits (ACViewer convention): + // r0 = (x0, y0) = SW + // r1 = (x0, y1) = NW + // r2 = (x1, y0) = SE + // r3 = (x1, y1) = NE + bool r0 = IsRoadVertex(block.Terrain[x * VerticesPerSide + y]); + bool r1 = IsRoadVertex(block.Terrain[x * VerticesPerSide + (y + 1)]); + bool r2 = IsRoadVertex(block.Terrain[(x + 1) * VerticesPerSide + y]); + bool r3 = IsRoadVertex(block.Terrain[(x + 1) * VerticesPerSide + (y + 1)]); + + if (!r0 && !r1 && !r2 && !r3) return false; + + float dx = lx - x * CellSize; + float dy = ly - y * CellSize; + + if (r0) + { + if (r1) + { + if (r2) + { + if (r3) return true; + return dx < rMin || dy < rMin; + } + else + { + if (r3) return dx < rMin || dy > rMax; + return dx < rMin; + } + } + else + { + if (r2) + { + if (r3) return dx > rMax || dy < rMin; + return dy < rMin; + } + else + { + if (r3) return MathF.Abs(dx - dy) < rMin; + return dx + dy < rMin; + } + } + } + else + { + if (r1) + { + if (r2) + { + if (r3) return dx > rMax || dy > rMax; + return MathF.Abs(dx + dy - CellSize) < rMin; + } + else + { + if (r3) return dy > rMax; + return CellSize + dx - dy < rMin; + } + } + else + { + if (r2) + { + if (r3) return dx > rMax; + return CellSize - dx + dy < rMin; + } + else + { + if (r3) return CellSize * 2f - dx - dy < rMin; + return false; + } + } + } + } + + private const int CellsPerSide = 8; + + /// + /// Pseudo-random displacement within a cell for a scenery object. Returns a + /// Vector3 in local cell-offset space (the caller adds it to the cell corner + /// to get landblock-local position). + /// + /// Verified against decompiled acclient.exe: chunk_005A0000.c lines 4844-4903 (FUN_005a6cc0). + /// X offset constant 0xb2cd = 45773; Y offset constant 0x11c0f = 72719. + /// Quadrant hash: line 4880; thresholds 0.25/0.5/0.75 map to _DAT_007c97cc/_DAT_007938b8/_DAT_0079c6dc. + /// Decompiled normalises signed-int LCG results with "if (val < 0) val += 2^32"; our + /// unchecked((uint)(...)) is exactly equivalent. + /// + private static Vector3 DisplaceObject(ObjectDesc obj, uint ix, uint iy, uint iq) + { + float x, y; + var baseLoc = obj.BaseLoc.Origin; + + // X displacement: chunk_005A0000.c lines 4858-4866 + // iVar4 = (param_3 * 0x6c1ac587 - (param_2 * param_3 * 0x5111bfef + 0x70892fb7) * (param_4 + 0xb2cd)) + param_2 * -0x421be3bd + if (obj.DisplaceX <= 0) + x = baseLoc.X; + else + x = (float)(unchecked((uint)(1813693831u * iy - (iq + 45773u) * (1360117743u * iy * ix + 1888038839u) - 1109124029u * ix)) + * 2.3283064e-10 * obj.DisplaceX + baseLoc.X); + + // Y displacement: chunk_005A0000.c lines 4871-4878 (same structure, offset 0x11c0f = 72719) + if (obj.DisplaceY <= 0) + y = baseLoc.Y; + else + y = (float)(unchecked((uint)(1813693831u * iy - (iq + 72719u) * (1360117743u * iy * ix + 1888038839u) - 1109124029u * ix)) + * 2.3283064e-10 * obj.DisplaceY + baseLoc.Y); + + float z = baseLoc.Z; + + // Quadrant selection: chunk_005A0000.c lines 4880-4902 + // iVar4 = (param_3 * 0x6c1ac587 - (param_3 * 0x6f7bd965 + 0x421be3bd) * param_2) + -0x17fcedfd + // 0x6f7bd965=1870387557, 0x421be3bd=1109124029, -0x17fcedfd β†’ -402451965 (uint: 3892515331) + double quadrant = unchecked((uint)(1813693831u * iy - ix * (1870387557u * iy + 1109124029u) - 402451965u)) * 2.3283064e-10; + + if (quadrant >= 0.75) return new Vector3(y, -x, z); + if (quadrant >= 0.5) return new Vector3(-x, -y, z); + if (quadrant >= 0.25) return new Vector3(-y, x, z); + return new Vector3(x, y, z); + } } diff --git a/src/AcDream.Core/World/WbSceneryAdapter.cs b/src/AcDream.Core/World/WbSceneryAdapter.cs deleted file mode 100644 index 1a90149..0000000 --- a/src/AcDream.Core/World/WbSceneryAdapter.cs +++ /dev/null @@ -1,55 +0,0 @@ -using DatReaderWriter.DBObjs; -using WorldBuilder.Shared.Models; - -namespace AcDream.Core.World; - -/// -/// Bridges acdream's dat types into WorldBuilder's data shapes for the -/// Phase N rendering migration. See -/// docs/architecture/worldbuilder-inventory.md for the full strategy. -/// -internal static class WbSceneryAdapter -{ - private const int VerticesPerSide = 9; - private const int TerrainSize = VerticesPerSide * VerticesPerSide; // 81 - - /// - /// Builds a 9Γ—9 = 81-entry array from a - /// 's packed terrain bits + height bytes. WB's - /// TerrainUtils.OnRoad / GetNormal / GetHeight - /// consume this shape. - /// - /// Field mapping (TerrainInfo β†’ ): - /// TerrainInfo.Road (bits 0-1) β†’ - /// TerrainInfo.Type (bits 2-6) β†’ - /// TerrainInfo.Scenery (bits 11-15) β†’ - /// LandBlock.Height[i] β†’ - /// - /// - /// No runtime length guards are needed here because - /// DatReaderWriter.DBObjs.LandBlock's default constructor - /// self-initializes both Terrain and Height to fixed-length - /// arrays of exactly 81 elements (9Γ—9 vertices per landblock). Any caller - /// that constructs a synthetic with partial arrays - /// will receive an at the first - /// mis-sized index, which is the correct fast-fail behaviour for a - /// contract violation of this kind. - /// - public static TerrainEntry[] BuildTerrainEntries(LandBlock block) - { - ArgumentNullException.ThrowIfNull(block); - - var entries = new TerrainEntry[TerrainSize]; - for (int i = 0; i < TerrainSize; i++) - { - var ti = block.Terrain[i]; - entries[i] = new TerrainEntry( - height: block.Height[i], - texture: (byte)ti.Type, - scenery: ti.Scenery, - road: ti.Road, - encounters: null); - } - return entries; - } -} diff --git a/src/AcDream.Core/World/WorldEntity.cs b/src/AcDream.Core/World/WorldEntity.cs index 20643d3..33a4b2c 100644 --- a/src/AcDream.Core/World/WorldEntity.cs +++ b/src/AcDream.Core/World/WorldEntity.cs @@ -55,51 +55,4 @@ public sealed class WorldEntity /// visible trunk, producing "partial passthrough" bugs. /// public float Scale { get; init; } = 1.0f; - - /// - /// Server-sent part-swap overrides from AnimPartChange. Each entry - /// replaces a Setup part's GfxObj with an alternate model (clothing, weapons, - /// helmets). Carried on the entity so EntitySpawnAdapter can populate - /// AnimatedEntityState's override map at spawn time. Empty for atlas- - /// tier entities. - /// - public IReadOnlyList PartOverrides { get; init; } = Array.Empty(); - - /// - /// Bitmask of hidden Setup parts. Bit i set hides part i at - /// draw time. Sourced from the server's CreateObject record when - /// present. Zero (no parts hidden) is the default. - /// - public ulong HiddenPartsMask { get; init; } - - // Per Phase A.5 spec Β§4.6 Change #2 β€” cache per-entity AABB so the - // dispatcher's frustum cull is a memory read, not a per-frame recompute. - // AabbDirty starts true so the dispatcher calls RefreshAabb on first read - // (AabbMin/AabbMax are Vector3.Zero until refreshed). - public Vector3 AabbMin { get; private set; } - public Vector3 AabbMax { get; private set; } - public bool AabbDirty { get; private set; } = true; - - private const float DefaultAabbRadius = 5.0f; - - public void RefreshAabb() - { - var p = Position; - AabbMin = new Vector3(p.X - DefaultAabbRadius, p.Y - DefaultAabbRadius, p.Z - DefaultAabbRadius); - AabbMax = new Vector3(p.X + DefaultAabbRadius, p.Y + DefaultAabbRadius, p.Z + DefaultAabbRadius); - AabbDirty = false; - } - - public void SetPosition(Vector3 pos) - { - Position = pos; - AabbDirty = true; - } } - -/// -/// Lightweight value type for a server-sent AnimPartChange (part index -/// β†’ replacement GfxObj id). Decouples WorldEntity (Core) from the -/// network-layer CreateObject.AnimPartChange type. -/// -public readonly record struct PartOverride(byte PartIndex, uint GfxObjId); diff --git a/src/AcDream.UI.Abstractions/Input/InputDispatcher.cs b/src/AcDream.UI.Abstractions/Input/InputDispatcher.cs index 84bafce..590b9a9 100644 --- a/src/AcDream.UI.Abstractions/Input/InputDispatcher.cs +++ b/src/AcDream.UI.Abstractions/Input/InputDispatcher.cs @@ -38,14 +38,6 @@ public sealed class InputDispatcher private readonly Stack _scopes = new(); private readonly HashSet _heldHoldChords = new(); - // Double-click detection. _lastMouseDownButton == null means no recent press. - // _lastMouseDownTickMs is Environment.TickCount64 at the time of that press. - // A subsequent mouse-down on the same button within DoubleClickThresholdMs - // additionally fires ActivationType.DoubleClick for the matching binding. - private MouseButton? _lastMouseDownButton; - private long _lastMouseDownTickMs; - private const long DoubleClickThresholdMs = 500; - /// K.3 modal-rebind hook: when non-null, the next non-modifier /// chord is reported via this callback INSTEAD of firing actions. Esc /// cancels (callback receives default(KeyChord)). @@ -333,24 +325,6 @@ public sealed class InputDispatcher Fired?.Invoke(hold.Value.Action, ActivationType.Press); _heldHoldChords.Add(chord); } - - // Double-click recognition. Same button within DoubleClickThresholdMs - // -> additionally fire ActivationType.DoubleClick for any matching - // binding. Press has already fired for the second click (same as a - // single click); DoubleClick is the *additional* signal. - long nowMs = Environment.TickCount64; - if (_lastMouseDownButton == button - && nowMs - _lastMouseDownTickMs <= DoubleClickThresholdMs) - { - var dbl = _bindings.Find(chord, ActivationType.DoubleClick); - if (dbl is not null) Fired?.Invoke(dbl.Value.Action, ActivationType.DoubleClick); - _lastMouseDownButton = null; // consumed; require fresh pair for next - } - else - { - _lastMouseDownButton = button; - _lastMouseDownTickMs = nowMs; - } } private void OnMouseUp(MouseButton button, ModifierMask mods) diff --git a/src/AcDream.UI.Abstractions/Panels/Debug/DebugPanel.cs b/src/AcDream.UI.Abstractions/Panels/Debug/DebugPanel.cs index 45f0e88..dc55080 100644 --- a/src/AcDream.UI.Abstractions/Panels/Debug/DebugPanel.cs +++ b/src/AcDream.UI.Abstractions/Panels/Debug/DebugPanel.cs @@ -199,32 +199,15 @@ public sealed class DebugPanel : IPanel { if (!r.CollapsingHeader("Diagnostics", defaultOpen: true)) return; - bool dumpMotion = _vm.DumpMotion; - bool dumpVitals = _vm.DumpVitals; - bool dumpOpcodes = _vm.DumpOpcodes; - bool dumpSky = _vm.DumpSky; - bool probeResolve = _vm.ProbeResolve; - bool probeCell = _vm.ProbeCell; - bool probeBuilding = _vm.ProbeBuilding; - bool probeAutoWalk = _vm.ProbeAutoWalk; + bool dumpMotion = _vm.DumpMotion; + bool dumpVitals = _vm.DumpVitals; + bool dumpOpcodes = _vm.DumpOpcodes; + bool dumpSky = _vm.DumpSky; - if (r.Checkbox("Dump motion (ACDREAM_DUMP_MOTION)", ref dumpMotion)) _vm.DumpMotion = dumpMotion; - if (r.Checkbox("Dump vitals (ACDREAM_DUMP_VITALS)", ref dumpVitals)) _vm.DumpVitals = dumpVitals; - if (r.Checkbox("Dump opcodes (ACDREAM_DUMP_OPCODES)", ref dumpOpcodes)) _vm.DumpOpcodes = dumpOpcodes; - if (r.Checkbox("Dump sky (ACDREAM_DUMP_SKY)", ref dumpSky)) _vm.DumpSky = dumpSky; - // L.2a slice 1 (2026-05-12): unlike the four above, these - // forward to PhysicsDiagnostics so a toggle takes effect live. - if (r.Checkbox("Probe resolve (ACDREAM_PROBE_RESOLVE)", ref probeResolve)) _vm.ProbeResolve = probeResolve; - if (r.Checkbox("Probe cell-transit (ACDREAM_PROBE_CELL)",ref probeCell)) _vm.ProbeCell = probeCell; - // L.2d slice 1 (2026-05-13): heavy per-hit BSP diagnostic for - // doorway / building shape-fidelity work. Emits multi-line - // [resolve-bldg] entries; expect log volume to spike at walls. - if (r.Checkbox("Probe BSP hits (ACDREAM_PROBE_BUILDING, slow)", - ref probeBuilding)) _vm.ProbeBuilding = probeBuilding; - // B.6 slice 1 (2026-05-14): local-player auto-walk trace for issue #63. - // Low volume β€” only the local player's UM/UP/Use/PickUp events emit. - if (r.Checkbox("Probe auto-walk (ACDREAM_PROBE_AUTOWALK)", - ref probeAutoWalk)) _vm.ProbeAutoWalk = probeAutoWalk; + if (r.Checkbox("Dump motion (ACDREAM_DUMP_MOTION)", ref dumpMotion)) _vm.DumpMotion = dumpMotion; + if (r.Checkbox("Dump vitals (ACDREAM_DUMP_VITALS)", ref dumpVitals)) _vm.DumpVitals = dumpVitals; + if (r.Checkbox("Dump opcodes (ACDREAM_DUMP_OPCODES)", ref dumpOpcodes)) _vm.DumpOpcodes = dumpOpcodes; + if (r.Checkbox("Dump sky (ACDREAM_DUMP_SKY)", ref dumpSky)) _vm.DumpSky = dumpSky; r.Spacing(); diff --git a/src/AcDream.UI.Abstractions/Panels/Debug/DebugVM.cs b/src/AcDream.UI.Abstractions/Panels/Debug/DebugVM.cs index ef99a25..9e914af 100644 --- a/src/AcDream.UI.Abstractions/Panels/Debug/DebugVM.cs +++ b/src/AcDream.UI.Abstractions/Panels/Debug/DebugVM.cs @@ -1,6 +1,5 @@ using System.Numerics; using AcDream.Core.Combat; -using AcDream.Core.Physics; namespace AcDream.UI.Abstractions.Panels.Debug; @@ -235,61 +234,6 @@ public sealed class DebugVM /// Mirror of ACDREAM_DUMP_SKY. public bool DumpSky { get; set; } - // L.2a slice 1 (2026-05-12): unlike DumpMotion/Vitals/Opcodes/Sky - // above (which are display-only mirrors of sticky-at-startup env - // vars), these forward directly to the PhysicsDiagnostics statics, - // so checkbox toggles take effect on the next physics resolve. - /// - /// Runtime mirror of PhysicsDiagnostics.ProbeResolveEnabled - /// (env var ACDREAM_PROBE_RESOLVE). Toggling here flips the - /// resolver probe live β€” no relaunch required. - /// - public bool ProbeResolve - { - get => PhysicsDiagnostics.ProbeResolveEnabled; - set => PhysicsDiagnostics.ProbeResolveEnabled = value; - } - - /// - /// Runtime mirror of PhysicsDiagnostics.ProbeCellEnabled - /// (env var ACDREAM_PROBE_CELL). Toggling here flips the - /// cell-transit probe live. - /// - public bool ProbeCell - { - get => PhysicsDiagnostics.ProbeCellEnabled; - set => PhysicsDiagnostics.ProbeCellEnabled = value; - } - - /// - /// L.2d slice 1 (2026-05-13). Runtime mirror of - /// PhysicsDiagnostics.ProbeBuildingEnabled (env var - /// ACDREAM_PROBE_BUILDING). Toggling here flips the per-hit - /// [resolve-bldg] diagnostic + the registration-time - /// [entity-source] log lines. Heavy when enabled β€” emits one - /// multi-line entry per BSP hit per physics tick. - /// - public bool ProbeBuilding - { - get => PhysicsDiagnostics.ProbeBuildingEnabled; - set => PhysicsDiagnostics.ProbeBuildingEnabled = value; - } - - /// - /// B.6 slice 1 (2026-05-14). Runtime mirror of - /// PhysicsDiagnostics.ProbeAutoWalkEnabled (env var - /// ACDREAM_PROBE_AUTOWALK). Toggling here flips the - /// [autowalk-out] / [autowalk-mt] / [autowalk-up] - /// trace used to characterize ACE's behavior during a server- - /// initiated auto-walk (issue #63). Low volume when off β€” only the - /// local player's events are filtered through the probe. - /// - public bool ProbeAutoWalk - { - get => PhysicsDiagnostics.ProbeAutoWalkEnabled; - set => PhysicsDiagnostics.ProbeAutoWalkEnabled = value; - } - // ── Action hooks invoked by panel buttons ────────────────────────── /// diff --git a/src/AcDream.UI.Abstractions/Panels/Settings/DisplaySettings.cs b/src/AcDream.UI.Abstractions/Panels/Settings/DisplaySettings.cs index 3b5a2b6..05438b0 100644 --- a/src/AcDream.UI.Abstractions/Panels/Settings/DisplaySettings.cs +++ b/src/AcDream.UI.Abstractions/Panels/Settings/DisplaySettings.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using AcDream.UI.Abstractions.Settings; namespace AcDream.UI.Abstractions.Panels.Settings; @@ -21,8 +20,7 @@ public sealed record DisplaySettings( bool VSync, float FieldOfView, float Gamma, - bool ShowFps, - QualityPreset Quality) + bool ShowFps) { /// Values used on first launch / when settings.json is absent. /// All defaults pinned to the pre-L.0 runtime state β€” Resolution @@ -37,8 +35,7 @@ public sealed record DisplaySettings( VSync: false, FieldOfView: 60f, Gamma: 1.0f, - ShowFps: true, - Quality: QualityPreset.High); + ShowFps: true); /// 16:9 resolution presets offered in the dropdown. public static IReadOnlyList AvailableResolutions { get; } = new[] diff --git a/src/AcDream.UI.Abstractions/Panels/Settings/SettingsPanel.cs b/src/AcDream.UI.Abstractions/Panels/Settings/SettingsPanel.cs index 698eee1..a8a8034 100644 --- a/src/AcDream.UI.Abstractions/Panels/Settings/SettingsPanel.cs +++ b/src/AcDream.UI.Abstractions/Panels/Settings/SettingsPanel.cs @@ -1,7 +1,6 @@ using System.Collections.Generic; using System.Linq; using AcDream.UI.Abstractions.Input; -using AcDream.UI.Abstractions.Settings; namespace AcDream.UI.Abstractions.Panels.Settings; @@ -220,23 +219,10 @@ public sealed class SettingsPanel : IPanel if (renderer.Checkbox("Show FPS", ref showFps)) _vm.SetDisplay(d with { ShowFps = showFps }); - // A.5 T22.5: Quality preset dropdown. Drives streaming radii, MSAA, - // anisotropic level, A2C, and max completions-per-frame as a unit. - // Resolution + anisotropic + A2C + completions apply immediately via - // ReapplyQualityPreset; MSAA samples require a restart (GL context - // cannot change sample count at runtime). - var presets = s_qualityPresetNames; - int qIdx = (int)d.Quality; - if (qIdx < 0 || qIdx >= presets.Length) qIdx = (int)QualityPreset.High; - if (renderer.Combo("Quality", ref qIdx, presets)) - _vm.SetDisplay(d with { Quality = (QualityPreset)qIdx }); - renderer.Spacing(); renderer.TextWrapped( "Resolution / Fullscreen / V-Sync apply on Save. FOV + Gamma " - + "preview live as you drag; Cancel reverts to the saved value. " - + "Quality preset applies streaming radius, anisotropic, and A2C " - + "immediately on Save; MSAA sample count requires a restart."); + + "preview live as you drag; Cancel reverts to the saved value."); } /// @@ -460,11 +446,6 @@ public sealed class SettingsPanel : IPanel + "round-trip lands."); } - // A.5 T22.5: preset label array parallel to QualityPreset enum values. - // Order must match the enum (Low=0, Medium=1, High=2, Ultra=3). - private static readonly string[] s_qualityPresetNames = - { "Low", "Medium", "High", "Ultra" }; - private void RenderSection(IPanelRenderer renderer, string label, InputAction[] actions) { // Movement defaults open; other sections collapsed for first-run UX. diff --git a/src/AcDream.UI.Abstractions/Panels/Settings/SettingsStore.cs b/src/AcDream.UI.Abstractions/Panels/Settings/SettingsStore.cs index 5cb20e6..11264fc 100644 --- a/src/AcDream.UI.Abstractions/Panels/Settings/SettingsStore.cs +++ b/src/AcDream.UI.Abstractions/Panels/Settings/SettingsStore.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.IO; using System.Text.Json; using System.Text.Json.Nodes; -using AcDream.UI.Abstractions.Settings; namespace AcDream.UI.Abstractions.Panels.Settings; @@ -63,13 +62,12 @@ public sealed class SettingsStore var d = DisplaySettings.Default; return new DisplaySettings( - Resolution: ReadString (disp, "resolution", d.Resolution), - Fullscreen: ReadBool (disp, "fullscreen", d.Fullscreen), - VSync: ReadBool (disp, "vsync", d.VSync), - FieldOfView: ReadFloat (disp, "fieldOfView", d.FieldOfView), - Gamma: ReadFloat (disp, "gamma", d.Gamma), - ShowFps: ReadBool (disp, "showFps", d.ShowFps), - Quality: ReadQuality (disp, "quality", d.Quality)); + Resolution: ReadString (disp, "resolution", d.Resolution), + Fullscreen: ReadBool (disp, "fullscreen", d.Fullscreen), + VSync: ReadBool (disp, "vsync", d.VSync), + FieldOfView: ReadFloat (disp, "fieldOfView", d.FieldOfView), + Gamma: ReadFloat (disp, "gamma", d.Gamma), + ShowFps: ReadBool (disp, "showFps", d.ShowFps)); } catch (Exception ex) { @@ -329,7 +327,6 @@ public sealed class SettingsStore ["fieldOfView"] = d.FieldOfView, ["fullscreen"] = d.Fullscreen, ["gamma"] = d.Gamma, - ["quality"] = d.Quality.ToString(), ["resolution"] = d.Resolution, ["showFps"] = d.ShowFps, ["vsync"] = d.VSync, @@ -408,12 +405,4 @@ public sealed class SettingsStore private static float ReadFloat(JsonElement obj, string name, float fallback) => obj.TryGetProperty(name, out var el) && el.ValueKind == JsonValueKind.Number ? el.GetSingle() : fallback; - - private static QualityPreset ReadQuality(JsonElement obj, string name, QualityPreset fallback) - { - if (!obj.TryGetProperty(name, out var el) || el.ValueKind != JsonValueKind.String) - return fallback; - var s = el.GetString(); - return Enum.TryParse(s, ignoreCase: true, out var v) ? v : fallback; - } } diff --git a/src/AcDream.UI.Abstractions/Settings/QualityPreset.cs b/src/AcDream.UI.Abstractions/Settings/QualityPreset.cs deleted file mode 100644 index e215d66..0000000 --- a/src/AcDream.UI.Abstractions/Settings/QualityPreset.cs +++ /dev/null @@ -1,67 +0,0 @@ -namespace AcDream.UI.Abstractions.Settings; - -/// -/// A.5 T22.5: single user-facing quality knob that drives streaming radii, -/// MSAA samples, anisotropic level, alpha-to-coverage, and max completions -/// per frame in a single setting. Individual fields can still be overridden -/// by env vars (see ). -/// -public enum QualityPreset { Low, Medium, High, Ultra } - -/// -/// Resolved per-preset quality parameters. Constructed via -/// then optionally overridden with -/// before applying to the -/// renderer and streaming controller. -/// -public readonly record struct QualitySettings( - int NearRadius, - int FarRadius, - int MsaaSamples, // 0 = off, 2, 4, 8 - int AnisotropicLevel, // 1 = off, 4, 8, 16 - bool AlphaToCoverage, - int MaxCompletionsPerFrame) -{ - /// - /// Return the default for . - /// Unknown enum values fall back to . - /// - public static QualitySettings From(QualityPreset preset) => preset switch - { - QualityPreset.Low => new(NearRadius: 2, FarRadius: 5, MsaaSamples: 0, AnisotropicLevel: 4, AlphaToCoverage: false, MaxCompletionsPerFrame: 2), - QualityPreset.Medium => new(NearRadius: 3, FarRadius: 8, MsaaSamples: 2, AnisotropicLevel: 8, AlphaToCoverage: false, MaxCompletionsPerFrame: 3), - QualityPreset.High => new(NearRadius: 4, FarRadius: 12, MsaaSamples: 4, AnisotropicLevel: 16, AlphaToCoverage: true, MaxCompletionsPerFrame: 4), - QualityPreset.Ultra => new(NearRadius: 5, FarRadius: 15, MsaaSamples: 4, AnisotropicLevel: 16, AlphaToCoverage: true, MaxCompletionsPerFrame: 6), - _ => From(QualityPreset.High), - }; - - /// - /// Apply env-var overrides to a preset's resolved settings. Per-field - /// env vars beat the preset (so devs can spot-test a single dimension). - /// Unset or empty env vars leave the preset default unchanged. - /// - public static QualitySettings WithEnvOverrides(QualitySettings baseSettings) - { - int nearRadius = TryParseEnvInt("ACDREAM_NEAR_RADIUS", baseSettings.NearRadius); - int farRadius = TryParseEnvInt("ACDREAM_FAR_RADIUS", baseSettings.FarRadius); - int msaa = TryParseEnvInt("ACDREAM_MSAA_SAMPLES", baseSettings.MsaaSamples); - int aniso = TryParseEnvInt("ACDREAM_ANISOTROPIC", baseSettings.AnisotropicLevel); - // Bool override: any non-empty value other than "0"/"false" enables A2C. - // Empty / unset β†’ keep preset default. - var a2cEnv = System.Environment.GetEnvironmentVariable("ACDREAM_A2C"); - bool a2c = a2cEnv switch - { - null or "" => baseSettings.AlphaToCoverage, - "0" or "false" or "False" or "FALSE" => false, - _ => true, - }; - int completions = TryParseEnvInt("ACDREAM_MAX_COMPLETIONS_PER_FRAME", baseSettings.MaxCompletionsPerFrame); - return new QualitySettings(nearRadius, farRadius, msaa, aniso, a2c, completions); - } - - private static int TryParseEnvInt(string name, int defaultValue) - { - var s = System.Environment.GetEnvironmentVariable(name); - return s is not null && int.TryParse(s, out var v) ? v : defaultValue; - } -} diff --git a/tests/AcDream.Core.Net.Tests/GameEventWiringTests.cs b/tests/AcDream.Core.Net.Tests/GameEventWiringTests.cs index c414ddb..f740efb 100644 --- a/tests/AcDream.Core.Net.Tests/GameEventWiringTests.cs +++ b/tests/AcDream.Core.Net.Tests/GameEventWiringTests.cs @@ -1,6 +1,5 @@ using System; using System.Buffers.Binary; -using System.IO; using System.Text; using AcDream.Core.Chat; using AcDream.Core.Combat; @@ -329,50 +328,4 @@ public sealed class GameEventWiringTests Assert.Contains("Mana Stone", e.Text); } - [Fact] - public void PlayerDescription_RegistersInventoryEntries_InItemRepository() - { - // Issue #13 acceptance test: after a PlayerDescription with non-empty - // Inventory is dispatched through WireAll, ItemRepository.ItemCount > 0. - // Wire format: strict path (no GAMEPLAY_OPTIONS bit) so inventory + - // equipped follow directly after spellbook_filters. - var dispatcher = new GameEventDispatcher(); - var items = new ItemRepository(); - var combat = new CombatState(); - var spellbook = new Spellbook(); - var chat = new ChatLog(); - GameEventWiring.WireAll(dispatcher, items, combat, spellbook, chat); - - Assert.Equal(0, items.ItemCount); // pre-condition - - var sb = new MemoryStream(); - using var w = new BinaryWriter(sb); - w.Write(0u); // propertyFlags = 0 - w.Write(0x52u); // weenieType - w.Write(0x201u); // vectorFlags = ATTRIBUTE | ENCHANTMENT - w.Write(1u); // has_health - w.Write(0u); // attribute_flags = 0 (no attrs) - w.Write(0u); // enchantment_mask = 0 - - w.Write(0u); // option_flags = None (no GAMEPLAY_OPTIONS β†’ strict inv path) - w.Write(0u); // options1 - w.Write(0u); // legacy hotbar list count = 0 - w.Write(0u); // spellbook_filters - - // Inventory: 2 entries - w.Write(2u); - w.Write(0x50000A01u); w.Write(0u); // guid, ContainerType=NonContainer - w.Write(0x50000A02u); w.Write(1u); // guid, ContainerType=Container - - // Equipped: 0 entries - w.Write(0u); - - var env = GameEventEnvelope.TryParse(WrapEnvelope(GameEventType.PlayerDescription, sb.ToArray())); - dispatcher.Dispatch(env!.Value); - - Assert.Equal(2, items.ItemCount); - Assert.NotNull(items.GetItem(0x50000A01u)); - Assert.NotNull(items.GetItem(0x50000A02u)); - } - } diff --git a/tests/AcDream.Core.Net.Tests/Messages/CreateObjectTests.cs b/tests/AcDream.Core.Net.Tests/Messages/CreateObjectTests.cs index 1e9ce10..71bf72d 100644 --- a/tests/AcDream.Core.Net.Tests/Messages/CreateObjectTests.cs +++ b/tests/AcDream.Core.Net.Tests/Messages/CreateObjectTests.cs @@ -74,98 +74,12 @@ public sealed class CreateObjectTests Assert.Equal(0x2000008u, parsed!.Value.ObjectDescriptionFlags); } - // ----------------------------------------------------------------------- - // 2026-05-15: WeenieHeader optional-tail walker landed for Useability + - // UseRadius (acclient.h ITEM_USEABLE enum at line 6478). The R-key Use - // gate consumes Useability; signs without USEABLE_REMOTE (0x20) silently - // ignore Use. - // ----------------------------------------------------------------------- - - [Fact] - public void TryParse_NoWeenieFlags_LeavesUseabilityNull() - { - // Sign-like entity: weenieFlags=0 (no optional fields). - // Useability stays null (parser walked past nothing). - byte[] body = BuildMinimalCreateObjectWithWeenieHeader( - guid: 0x50000006u, name: "Holtburg Sign", - itemType: 0x8000u); - - var parsed = CreateObject.TryParse(body); - - Assert.NotNull(parsed); - Assert.Null(parsed!.Value.Useability); - Assert.Null(parsed.Value.UseRadius); - } - - [Fact] - public void TryParse_WeenieFlagsUsable_ReadsUseability() - { - // Useable NPC: weenieFlags has bit 0x10 set, body carries - // ITEM_USEABLE = USEABLE_REMOTE (0x20). - byte[] body = BuildMinimalCreateObjectWithWeenieHeader( - guid: 0x50000007u, name: "Tirenia", - itemType: (uint)ItemType.Creature, - weenieFlags: 0x10u, - useability: 0x20u); - - var parsed = CreateObject.TryParse(body); - - Assert.NotNull(parsed); - Assert.Equal(0x20u, parsed!.Value.Useability); - } - - [Fact] - public void TryParse_WeenieFlagsUsable_ReadsUseableNoValue() - { - // Holtburg sign case (observed 2026-05-16): ACE sends - // weenieFlags=0x10 + Useability=USEABLE_NO (0x01) for signs. - // The parser must read this verbatim β€” downstream code - // distinguishes USEABLE_NO from USEABLE_REMOTE for the - // pickup vs use gate. - byte[] body = BuildMinimalCreateObjectWithWeenieHeader( - guid: 0x7A9B3001u, name: "Holtburg", - itemType: 0x80u, // Misc - weenieFlags: 0x10u, - useability: 0x01u); // USEABLE_NO - - var parsed = CreateObject.TryParse(body); - - Assert.NotNull(parsed); - Assert.Equal(0x01u, parsed!.Value.Useability); - } - - [Fact] - public void TryParse_WeenieFlagsValueAndUsableAndUseRadius_AllReadInOrder() - { - // Verify the walker skips Value (bit 0x8, 4 bytes) BEFORE reading - // Useability (bit 0x10) and UseRadius (bit 0x20). Wire order in - // ACE WorldObject_Networking.cs:99-106 is Value, Useable, UseRadius. - byte[] body = BuildMinimalCreateObjectWithWeenieHeader( - guid: 0x50000008u, name: "PriceyDoor", - itemType: (uint)ItemType.Misc, - weenieFlags: 0x8u | 0x10u | 0x20u, - value: 0x12345678u, - useability: 0x20u, - useRadius: 2.5f); - - var parsed = CreateObject.TryParse(body); - - Assert.NotNull(parsed); - Assert.Equal(0x20u, parsed!.Value.Useability); - Assert.NotNull(parsed.Value.UseRadius); - Assert.Equal(2.5f, parsed.Value.UseRadius!.Value, precision: 3); - } - private static byte[] BuildMinimalCreateObjectWithWeenieHeader( uint guid, string name, uint itemType, uint physicsState = 0, - uint objectDescriptionFlags = 0, - uint weenieFlags = 0, - uint? value = null, - uint? useability = null, - float? useRadius = null) + uint objectDescriptionFlags = 0) { var bytes = new List(); WriteU32(bytes, CreateObject.Opcode); @@ -185,7 +99,7 @@ public sealed class CreateObjectTests Align4(bytes); // Fixed WeenieHeader prefix per ACE SerializeCreateObject. - WriteU32(bytes, weenieFlags); // weenieFlags + WriteU32(bytes, 0); // weenieFlags WriteString16L(bytes, name); WritePackedDword(bytes, 0x1234); // WeenieClassId WritePackedDword(bytes, 0); // IconId via known-type writer @@ -193,20 +107,6 @@ public sealed class CreateObjectTests WriteU32(bytes, objectDescriptionFlags); Align4(bytes); - // Optional WeenieHeader tail (2026-05-15) β€” same order as ACE - // WorldObject_Networking.cs:87-114. Each field is written only when - // its weenieFlags bit is set, matching the parser's walker exactly. - if ((weenieFlags & 0x00000008u) != 0) // Value u32 - WriteU32(bytes, value ?? 0u); - if ((weenieFlags & 0x00000010u) != 0) // Useability u32 - WriteU32(bytes, useability ?? 0u); - if ((weenieFlags & 0x00000020u) != 0) // UseRadius f32 - { - Span tmp = stackalloc byte[4]; - BinaryPrimitives.WriteSingleLittleEndian(tmp, useRadius ?? 0f); - bytes.AddRange(tmp.ToArray()); - } - return bytes.ToArray(); } diff --git a/tests/AcDream.Core.Net.Tests/Messages/InteractRequestsTests.cs b/tests/AcDream.Core.Net.Tests/Messages/InteractRequestsTests.cs index c36e54f..5e99b5d 100644 --- a/tests/AcDream.Core.Net.Tests/Messages/InteractRequestsTests.cs +++ b/tests/AcDream.Core.Net.Tests/Messages/InteractRequestsTests.cs @@ -42,46 +42,4 @@ public sealed class InteractRequestsTests Assert.Equal(InteractRequests.TeleToLifestoneOpcode, BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(8))); } - - [Fact] - public void BuildPickUp_WritesOpcode0x0019AndPayload() - { - byte[] body = InteractRequests.BuildPickUp( - gameActionSequence: 5, - itemGuid: 0xABCDu, - containerGuid: 0x5000000Au, - placement: 0); - - Assert.Equal(24, body.Length); - Assert.Equal(InteractRequests.GameActionEnvelope, - BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(0))); - Assert.Equal(5u, - BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(4))); - Assert.Equal(InteractRequests.PutItemInContainerOpcode, - BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(8))); - Assert.Equal(0xABCDu, - BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(12))); - Assert.Equal(0x5000000Au, - BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(16))); - Assert.Equal(0, - BinaryPrimitives.ReadInt32LittleEndian(body.AsSpan(20))); - } - - [Fact] - public void BuildPickUp_NegativePlacement_WritesSignedLittleEndian() - { - // Sign-correctness guard: placement is i32 on the wire (ACE - // GameActionPutItemInContainer.Handle reads ReadInt32). A - // placement=0 test would pass even if the builder used - // WriteUInt32, so we also exercise a negative value where the - // unsigned/signed encodings would diverge. - byte[] body = InteractRequests.BuildPickUp( - gameActionSequence: 1, - itemGuid: 0x1u, - containerGuid: 0x2u, - placement: -1); - - Assert.Equal(-1, - BinaryPrimitives.ReadInt32LittleEndian(body.AsSpan(20))); - } } diff --git a/tests/AcDream.Core.Net.Tests/Messages/PickupEventTests.cs b/tests/AcDream.Core.Net.Tests/Messages/PickupEventTests.cs deleted file mode 100644 index e6248ec..0000000 --- a/tests/AcDream.Core.Net.Tests/Messages/PickupEventTests.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System.Buffers.Binary; -using AcDream.Core.Net.Messages; -using Xunit; - -namespace AcDream.Core.Net.Tests.Messages; - -public sealed class PickupEventTests -{ - [Fact] - public void RejectsWrongOpcode() - { - Span body = stackalloc byte[12]; - BinaryPrimitives.WriteUInt32LittleEndian(body, 0xDEADBEEFu); - - Assert.Null(PickupEvent.TryParse(body)); - } - - [Fact] - public void RejectsTruncated() - { - Assert.Null(PickupEvent.TryParse(ReadOnlySpan.Empty)); - Assert.Null(PickupEvent.TryParse(new byte[11])); - } - - [Fact] - public void ParsesGuidAndSequences() - { - Span body = stackalloc byte[12]; - BinaryPrimitives.WriteUInt32LittleEndian(body, PickupEvent.Opcode); - BinaryPrimitives.WriteUInt32LittleEndian(body.Slice(4), 0x80000727u); - BinaryPrimitives.WriteUInt16LittleEndian(body.Slice(8), 0x1234); - BinaryPrimitives.WriteUInt16LittleEndian(body.Slice(10), 0x5678); - - var parsed = PickupEvent.TryParse(body); - - Assert.NotNull(parsed); - Assert.Equal(0x80000727u, parsed!.Value.Guid); - Assert.Equal((ushort)0x1234, parsed.Value.InstanceSequence); - Assert.Equal((ushort)0x5678, parsed.Value.PositionSequence); - } -} diff --git a/tests/AcDream.Core.Net.Tests/Messages/SetStateTests.cs b/tests/AcDream.Core.Net.Tests/Messages/SetStateTests.cs deleted file mode 100644 index 837ccac..0000000 --- a/tests/AcDream.Core.Net.Tests/Messages/SetStateTests.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System; -using System.Buffers.Binary; -using AcDream.Core.Net.Messages; -using Xunit; - -namespace AcDream.Core.Net.Tests.Messages; - -public class SetStateTests -{ - [Fact] - public void TryParse_WellFormedBody_ReturnsParsed() - { - var buf = new byte[16]; - BinaryPrimitives.WriteUInt32LittleEndian(buf.AsSpan(0, 4), 0xF74Bu); - BinaryPrimitives.WriteUInt32LittleEndian(buf.AsSpan(4, 4), 0x000F4244u); // door guid - BinaryPrimitives.WriteUInt32LittleEndian(buf.AsSpan(8, 4), 0x00000004u); // ETHEREAL bit - BinaryPrimitives.WriteUInt16LittleEndian(buf.AsSpan(12, 2), (ushort)355); - BinaryPrimitives.WriteUInt16LittleEndian(buf.AsSpan(14, 2), (ushort)42); - - var parsed = SetState.TryParse(buf); - - Assert.NotNull(parsed); - Assert.Equal(0x000F4244u, parsed.Value.Guid); - Assert.Equal(0x00000004u, parsed.Value.PhysicsState); - Assert.Equal((ushort)355, parsed.Value.InstanceSequence); - Assert.Equal((ushort)42, parsed.Value.StateSequence); - } - - [Fact] - public void TryParse_Truncated_ReturnsNull() - { - var buf = new byte[10]; - Assert.Null(SetState.TryParse(buf)); - } - - [Fact] - public void TryParse_WrongOpcode_ReturnsNull() - { - var buf = new byte[16]; - BinaryPrimitives.WriteUInt32LittleEndian(buf.AsSpan(0, 4), 0xF74Cu); - Assert.Null(SetState.TryParse(buf)); - } -} diff --git a/tests/AcDream.Core.Net.Tests/PlayerDescriptionParserTests.cs b/tests/AcDream.Core.Net.Tests/PlayerDescriptionParserTests.cs index c74df04..4908bb8 100644 --- a/tests/AcDream.Core.Net.Tests/PlayerDescriptionParserTests.cs +++ b/tests/AcDream.Core.Net.Tests/PlayerDescriptionParserTests.cs @@ -334,434 +334,4 @@ public sealed class PlayerDescriptionParserTests Assert.Equal(2.0f, parsed.Value.Spells[1234u]); Assert.Equal(2.0f, parsed.Value.Spells[5678u]); } - - [Fact] - public void TryParse_TrailerOptionFlagsAndOptions1_AreReadAfterEnchantments() - { - // ATTRIBUTE | ENCHANTMENT vector flag; empty enchantment mask (0). - // After mask, trailer adds u32 option_flags + u32 options1. - var sb = new MemoryStream(); - using var writer = new BinaryWriter(sb); - writer.Write(0u); // propertyFlags - writer.Write(0x52u); // weenieType - writer.Write(0x201u); // ATTRIBUTE | ENCHANTMENT - writer.Write(1u); // has_health - writer.Write(0u); // empty attribute_flags - - writer.Write(0u); // EnchantmentMask = empty - - // Trailer header: option_flags + options1 - writer.Write(0u); // option_flags = None β€” no further sections - writer.Write(0xDEADBEEFu); // options1 sentinel - - // No more bytes β€” spellbook_filters is optional (defaults to 0). - var parsed = PlayerDescriptionParser.TryParse(sb.ToArray()); - - Assert.NotNull(parsed); - Assert.Equal(PlayerDescriptionParser.CharacterOptionDataFlag.None, parsed!.Value.OptionFlags); - Assert.Equal(0xDEADBEEFu, parsed.Value.Options1); - Assert.Empty(parsed.Value.Shortcuts); - Assert.Empty(parsed.Value.Inventory); - // Defaults for the trailer fields not yet read (Tasks 3-9 will - // populate them). Asserting them here gives those tasks a - // pre-existing regression guard if they accidentally consume into - // the wrong field's wire bytes. - Assert.Equal(0u, parsed.Value.Options2); - Assert.Equal(0u, parsed.Value.SpellbookFilters); - Assert.Empty(parsed.Value.HotbarSpells); - Assert.Empty(parsed.Value.DesiredComps); - Assert.True(parsed.Value.GameplayOptions.IsEmpty); - Assert.Empty(parsed.Value.Equipped); - Assert.False(parsed.Value.TrailerTruncated); - } - - [Fact] - public void TryParse_TrailerShortcuts_PopulatesList() - { - var sb = new MemoryStream(); - using var writer = new BinaryWriter(sb); - writer.Write(0u); // propertyFlags - writer.Write(0x52u); // weenieType - writer.Write(0x201u); // ATTRIBUTE | ENCHANTMENT - writer.Write(1u); // has_health - writer.Write(0u); // empty attribute_flags - writer.Write(0u); // empty enchantment mask - - writer.Write(0x01u); // option_flags = SHORTCUT - writer.Write(0xCAFEu); // options1 sentinel - - // Shortcut count + 2 entries (16 B each). - writer.Write(2u); - writer.Write(0u); writer.Write(0xAABBCCDDu); writer.Write((ushort)0); writer.Write((ushort)0); - writer.Write(7u); writer.Write(0u); writer.Write((ushort)1234); writer.Write((ushort)5); - - var parsed = PlayerDescriptionParser.TryParse(sb.ToArray()); - - Assert.NotNull(parsed); - Assert.Equal(2, parsed!.Value.Shortcuts.Count); - Assert.Equal(0u, parsed.Value.Shortcuts[0].Index); - Assert.Equal(0xAABBCCDDu, parsed.Value.Shortcuts[0].ObjectGuid); - Assert.Equal((ushort)0, parsed.Value.Shortcuts[0].SpellId); - Assert.Equal(7u, parsed.Value.Shortcuts[1].Index); - Assert.Equal((ushort)1234, parsed.Value.Shortcuts[1].SpellId); - Assert.Equal((ushort)5, parsed.Value.Shortcuts[1].Layer); - } - - [Fact] - public void TryParse_TrailerShortcuts_TruncatedMidList_FlagsTrailerTruncatedAndPreservesPriorEntries() - { - var sb = new MemoryStream(); - using var writer = new BinaryWriter(sb); - writer.Write(0u); // propertyFlags - writer.Write(0x52u); // weenieType - writer.Write(0x201u); // ATTRIBUTE | ENCHANTMENT - writer.Write(1u); // has_health - writer.Write(0u); // empty attribute_flags - writer.Write(0u); // empty enchantment mask - - writer.Write(0x01u); // option_flags = SHORTCUT - writer.Write(0u); // options1 - writer.Write(3u); // claimed shortcut count = 3 - // First entry complete (16 B). - writer.Write(1u); writer.Write(0xAAAAu); writer.Write((ushort)10); writer.Write((ushort)1); - // Second entry truncated to 8 bytes β€” ReadU16 will throw FormatException. - writer.Write(2u); writer.Write(0xBBBBu); - // (no SpellId/Layer β€” payload ends here) - - var parsed = PlayerDescriptionParser.TryParse(sb.ToArray()); - - Assert.NotNull(parsed); - // Inner catch fired β€” flag set. - Assert.True(parsed!.Value.TrailerTruncated); - // First entry survives in the partial list. - Assert.Single(parsed.Value.Shortcuts); - Assert.Equal(1u, parsed.Value.Shortcuts[0].Index); - } - - [Fact] - public void TryParse_TrailerHotbarSpells_SpellLists8_Reads8Lists() - { - var sb = new MemoryStream(); - using var writer = new BinaryWriter(sb); - writer.Write(0u); // propertyFlags - writer.Write(0x52u); // weenieType - writer.Write(0x201u); // ATTRIBUTE | ENCHANTMENT - writer.Write(1u); // has_health - writer.Write(0u); // empty attribute_flags - writer.Write(0u); // empty enchantment mask - - writer.Write(0x400u); // option_flags = SPELL_LISTS8 - writer.Write(0u); // options1 - - // 8 hotbars: counts {2,1,0,0,0,0,0,3} - writer.Write(2u); writer.Write(11u); writer.Write(12u); - writer.Write(1u); writer.Write(21u); - writer.Write(0u); - writer.Write(0u); - writer.Write(0u); - writer.Write(0u); - writer.Write(0u); - writer.Write(3u); writer.Write(81u); writer.Write(82u); writer.Write(83u); - - var parsed = PlayerDescriptionParser.TryParse(sb.ToArray()); - - Assert.NotNull(parsed); - Assert.Equal(8, parsed!.Value.HotbarSpells.Count); - Assert.Equal(new uint[] { 11u, 12u }, parsed.Value.HotbarSpells[0]); - Assert.Equal(new uint[] { 21u }, parsed.Value.HotbarSpells[1]); - Assert.Empty(parsed.Value.HotbarSpells[2]); - Assert.Equal(new uint[] { 81u, 82u, 83u }, parsed.Value.HotbarSpells[7]); - } - - [Fact] - public void TryParse_TrailerHotbarSpells_NoSpellLists8_ReadsSingleLegacyList() - { - var sb = new MemoryStream(); - using var writer = new BinaryWriter(sb); - writer.Write(0u); // propertyFlags - writer.Write(0x52u); // weenieType - writer.Write(0x201u); // ATTRIBUTE | ENCHANTMENT - writer.Write(1u); // has_health - writer.Write(0u); // empty attribute_flags - writer.Write(0u); // empty enchantment mask - - writer.Write(0u); // option_flags = None (no SPELL_LISTS8) - writer.Write(0u); // options1 - - // Legacy single hotbar list: count=2, two spells. - writer.Write(2u); writer.Write(101u); writer.Write(102u); - - var parsed = PlayerDescriptionParser.TryParse(sb.ToArray()); - - Assert.NotNull(parsed); - Assert.Single(parsed!.Value.HotbarSpells); - Assert.Equal(new uint[] { 101u, 102u }, parsed.Value.HotbarSpells[0]); - } - - [Fact] - public void TryParse_TrailerAbsent_LessThan8BytesAfterEnchantments_PreservesUpstreamAndDoesNotFlagTruncation() - { - // Fewer than 8 bytes remain after the enchantment block, so the - // trailer header is treated as absent (no read attempted). Upstream - // attribute data must survive; TrailerTruncated stays false because - // the parser never *started* the trailer β€” it correctly skipped it. - // (Tasks 3-9 will introduce truncation-mid-section cases that flip - // TrailerTruncated to true.) - var sb = new MemoryStream(); - using var writer = new BinaryWriter(sb); - writer.Write(0u); // propertyFlags - writer.Write(0x52u); // weenieType - writer.Write(0x201u); // ATTRIBUTE | ENCHANTMENT - writer.Write(1u); // has_health - // Attribute block: only Strength (bit 0). - writer.Write(0x01u); - writer.Write(50u); writer.Write(10u); writer.Write(0u); - // Empty enchantment mask. - writer.Write(0u); - // Truncated trailer: only 4 bytes (would-be option_flags) instead of 8. - writer.Write(0xCAFEu); - - var parsed = PlayerDescriptionParser.TryParse(sb.ToArray()); - - Assert.NotNull(parsed); - // Upstream attribute survived. - Assert.Single(parsed!.Value.Attributes); - Assert.Equal(1u, parsed.Value.Attributes[0].AtType); - // Trailer was absent (< 8 bytes), so no truncation flag and all - // trailer fields stay at their initial defaults. - Assert.False(parsed.Value.TrailerTruncated); - Assert.Equal(PlayerDescriptionParser.CharacterOptionDataFlag.None, parsed.Value.OptionFlags); - Assert.Equal(0u, parsed.Value.Options1); - } - - [Fact] - public void TryParse_TrailerDesiredComps_ReadsIdAmtPairs() - { - var sb = new MemoryStream(); - using var writer = new BinaryWriter(sb); - writer.Write(0u); // propertyFlags - writer.Write(0x52u); // weenieType - writer.Write(0x201u); // ATTRIBUTE | ENCHANTMENT - writer.Write(1u); // has_health - writer.Write(0u); // empty attribute_flags - writer.Write(0u); // empty enchantment mask - - // option_flags = DESIRED_COMPS (0x08); no SPELL_LISTS8 so legacy hotbar list (count=0). - writer.Write(0x08u); - writer.Write(0u); // options1 - - // Legacy hotbar list: count=0 - writer.Write(0u); - - // DESIRED_COMPS: u16 count=2, u16 padding, then 2 (id,amt) pairs of 8 bytes each. - writer.Write((ushort)2); - writer.Write((ushort)0); - writer.Write(0xAAu); writer.Write(50u); - writer.Write(0xBBu); writer.Write(75u); - - var parsed = PlayerDescriptionParser.TryParse(sb.ToArray()); - - Assert.NotNull(parsed); - Assert.Equal(2, parsed!.Value.DesiredComps.Count); - Assert.Equal((0xAAu, 50u), parsed.Value.DesiredComps[0]); - Assert.Equal((0xBBu, 75u), parsed.Value.DesiredComps[1]); - } - - [Fact] - public void TryParse_TrailerSpellbookFilters_ReadOptionalU32() - { - var sb = new MemoryStream(); - using var writer = new BinaryWriter(sb); - writer.Write(0u); // propertyFlags - writer.Write(0x52u); // weenieType - writer.Write(0x201u); // ATTRIBUTE | ENCHANTMENT - writer.Write(1u); // has_health - writer.Write(0u); // empty attribute_flags - writer.Write(0u); // empty enchantment mask - - writer.Write(0u); // option_flags = None - writer.Write(0u); // options1 - - // Legacy hotbar list: count=0 - writer.Write(0u); - - // spellbook_filters sentinel. - writer.Write(0xF00DBA42u); - - var parsed = PlayerDescriptionParser.TryParse(sb.ToArray()); - - Assert.NotNull(parsed); - Assert.Equal(0xF00DBA42u, parsed!.Value.SpellbookFilters); - } - - [Fact] - public void TryParse_TrailerOptions2_GatedOnCharacterOptions2Bit() - { - var sb = new MemoryStream(); - using var writer = new BinaryWriter(sb); - writer.Write(0u); // propertyFlags - writer.Write(0x52u); // weenieType - writer.Write(0x201u); // ATTRIBUTE | ENCHANTMENT - writer.Write(1u); // has_health - writer.Write(0u); // empty attribute_flags - writer.Write(0u); // empty enchantment mask - - // option_flags = CHARACTER_OPTIONS2 (0x40) - writer.Write(0x40u); - writer.Write(0u); // options1 - - // Legacy hotbar list: count=0. - writer.Write(0u); - - // spellbook_filters - writer.Write(0u); - - // options2 sentinel - writer.Write(0xC0FFEE01u); - - var parsed = PlayerDescriptionParser.TryParse(sb.ToArray()); - - Assert.NotNull(parsed); - Assert.Equal(0xC0FFEE01u, parsed!.Value.Options2); - } - - [Fact] - public void TryParse_TrailerInventoryEquippedStrict_NoGameplayOptionsBit() - { - var sb = new MemoryStream(); - using var writer = new BinaryWriter(sb); - writer.Write(0u); // propertyFlags - writer.Write(0x52u); // weenieType - writer.Write(0x201u); // ATTRIBUTE | ENCHANTMENT - writer.Write(1u); // has_health - writer.Write(0u); // empty attribute_flags - writer.Write(0u); // empty enchantment mask - - writer.Write(0u); // option_flags = None β€” no GAMEPLAY_OPTIONS - writer.Write(0u); // options1 - writer.Write(0u); // legacy hotbar list count=0 - writer.Write(0u); // spellbook_filters - - // Inventory: 2 entries - writer.Write(2u); - writer.Write(0x500000A0u); writer.Write(0u); // NonContainer - writer.Write(0x500000A1u); writer.Write(1u); // Container - - // Equipped: 1 entry - writer.Write(1u); - writer.Write(0x500000B0u); writer.Write(0x00000200u); writer.Write(1u); // ChestArmor, prio=1 - - var parsed = PlayerDescriptionParser.TryParse(sb.ToArray()); - - Assert.NotNull(parsed); - Assert.Equal(2, parsed!.Value.Inventory.Count); - Assert.Equal(0x500000A0u, parsed.Value.Inventory[0].Guid); - Assert.Equal(0u, parsed.Value.Inventory[0].ContainerType); - Assert.Equal(1u, parsed.Value.Inventory[1].ContainerType); - Assert.Single(parsed.Value.Equipped); - Assert.Equal(0x500000B0u, parsed.Value.Equipped[0].Guid); - Assert.Equal(0x00000200u, parsed.Value.Equipped[0].EquipLocation); - Assert.Equal(1u, parsed.Value.Equipped[0].Priority); - } - - [Fact] - public void TryParse_TrailerGameplayOptions_HeuristicLocatesInventoryStart() - { - var sb = new MemoryStream(); - using var writer = new BinaryWriter(sb); - writer.Write(0u); // propertyFlags - writer.Write(0x52u); // weenieType - writer.Write(0x201u); // ATTRIBUTE | ENCHANTMENT - writer.Write(1u); // has_health - writer.Write(0u); // empty attribute_flags - writer.Write(0u); // empty enchantment mask - - // option_flags = GAMEPLAY_OPTIONS (0x200) - writer.Write(0x200u); - writer.Write(0u); // options1 - writer.Write(0u); // legacy hotbar count=0 - writer.Write(0u); // spellbook_filters - - // 16 bytes of opaque gameplay_options blob β€” values that *almost* look - // like an inventory header but fail validation (wtype > 2 or count too - // big), forcing the heuristic to walk past them. - writer.Write(0xDEADBEEFu); // looks like inv_count = 0xDEADBEEF (> 10_000) β€” rejected - writer.Write(0xCAFEBABEu); - writer.Write(0x12345678u); - writer.Write(0x87654321u); - - // Real inventory: 1 entry, then equipped: 1 entry β€” must consume to EOF. - writer.Write(1u); - writer.Write(0x50000200u); writer.Write(0u); - writer.Write(1u); - writer.Write(0x50000300u); writer.Write(0x00000200u); writer.Write(1u); - - var parsed = PlayerDescriptionParser.TryParse(sb.ToArray()); - - Assert.NotNull(parsed); - Assert.Single(parsed!.Value.Inventory); - Assert.Equal(0x50000200u, parsed.Value.Inventory[0].Guid); - Assert.Single(parsed.Value.Equipped); - Assert.Equal(0x50000300u, parsed.Value.Equipped[0].Guid); - Assert.Equal(16, parsed.Value.GameplayOptions.Length); - } - - [Fact] - public void TryParse_FullTrailer_AllSectionsPopulated() - { - var sb = new MemoryStream(); - using var writer = new BinaryWriter(sb); - writer.Write(0u); // propertyFlags - writer.Write(0x52u); // weenieType - writer.Write(0x201u); // ATTRIBUTE | ENCHANTMENT - writer.Write(1u); // has_health - writer.Write(0u); // empty attribute_flags - writer.Write(0u); // empty enchantment mask - - // option_flags = SHORTCUT | DESIRED_COMPS | CHARACTER_OPTIONS2 | SPELL_LISTS8 - // = 0x01 | 0x08 | 0x40 | 0x400 = 0x449 - writer.Write(0x449u); - writer.Write(0xAA000001u); // options1 - - // Shortcuts: count=1 - writer.Write(1u); - writer.Write(3u); writer.Write(0xCAFEFACEu); writer.Write((ushort)100); writer.Write((ushort)2); - - // 8 hotbars, all empty for brevity. - for (int i = 0; i < 8; i++) writer.Write(0u); - - // Desired comps: count=1 - writer.Write((ushort)1); writer.Write((ushort)0); - writer.Write(0xC1u); writer.Write(99u); - - // spellbook_filters - writer.Write(0xF11Du); - - // options2 - writer.Write(0xBB000002u); - - // Inventory + equipped (no GAMEPLAY_OPTIONS, strict path) - writer.Write(1u); - writer.Write(0x50000400u); writer.Write(0u); - writer.Write(1u); - writer.Write(0x50000500u); writer.Write(0x00000200u); writer.Write(1u); - - var parsed = PlayerDescriptionParser.TryParse(sb.ToArray()); - - Assert.NotNull(parsed); - var v = parsed!.Value; - Assert.Equal(0xAA000001u, v.Options1); - Assert.Equal(0xBB000002u, v.Options2); - Assert.Equal(0xF11Du, v.SpellbookFilters); - Assert.Single(v.Shortcuts); - Assert.Equal(0xCAFEFACEu, v.Shortcuts[0].ObjectGuid); - Assert.Equal(8, v.HotbarSpells.Count); - Assert.All(v.HotbarSpells, l => Assert.Empty(l)); - Assert.Single(v.DesiredComps); - Assert.Equal((0xC1u, 99u), v.DesiredComps[0]); - Assert.Single(v.Inventory); - Assert.Equal(0x50000400u, v.Inventory[0].Guid); - Assert.Single(v.Equipped); - Assert.Equal(0x50000500u, v.Equipped[0].Guid); - } } diff --git a/tests/AcDream.Core.Tests/Meshing/SetupPartTransformsTests.cs b/tests/AcDream.Core.Tests/Meshing/SetupPartTransformsTests.cs deleted file mode 100644 index dfcb6e9..0000000 --- a/tests/AcDream.Core.Tests/Meshing/SetupPartTransformsTests.cs +++ /dev/null @@ -1,118 +0,0 @@ -using System.Numerics; -using AcDream.Core.Meshing; -using DatReaderWriter.DBObjs; -using DatReaderWriter.Enums; -using DatReaderWriter.Types; - -namespace AcDream.Core.Tests.Meshing; - -public class SetupPartTransformsTests -{ - [Fact] - public void Compute_PrefersRestingPlacement_OverDefault() - { - // Resting lifts part 1 by +Z=1; Default has zero lift on every part. - // Compute must pick Resting (matches SetupMesh.Flatten priority). - var setup = new Setup - { - Parts = { 0x01000100u, 0x01000101u }, - DefaultScale = { Vector3.One, Vector3.One }, - PlacementFrames = - { - [Placement.Resting] = new AnimationFrame(2) - { - Frames = - { - new Frame { Origin = Vector3.Zero, Orientation = Quaternion.Identity }, - new Frame { Origin = new Vector3(0, 0, 1f), Orientation = Quaternion.Identity }, - }, - }, - [Placement.Default] = new AnimationFrame(2) - { - Frames = - { - new Frame { Origin = Vector3.Zero, Orientation = Quaternion.Identity }, - new Frame { Origin = Vector3.Zero, Orientation = Quaternion.Identity }, - }, - }, - }, - }; - - var transforms = SetupPartTransforms.Compute(setup); - - Assert.Equal(2, transforms.Count); - var probe = Vector3.Transform(Vector3.Zero, transforms[1]); - Assert.Equal(new Vector3(0, 0, 1f), probe); - } - - [Fact] - public void Compute_FallsBackToDefault_WhenRestingMissing() - { - var setup = new Setup - { - Parts = { 0x01000100u }, - DefaultScale = { Vector3.One }, - PlacementFrames = - { - [Placement.Default] = new AnimationFrame(1) - { - Frames = - { - new Frame { Origin = new Vector3(2f, 0, 0), Orientation = Quaternion.Identity }, - }, - }, - }, - }; - - var transforms = SetupPartTransforms.Compute(setup); - - Assert.Single(transforms); - var probe = Vector3.Transform(Vector3.Zero, transforms[0]); - Assert.Equal(new Vector3(2f, 0, 0), probe); - } - - [Fact] - public void Compute_ReturnsEmpty_WhenNoPlacementFrames() - { - // Setup with parts but no PlacementFrames β€” caller's - // ParticleHookSink falls back to Identity per part (pre-C.1.5b - // behavior). Returning empty signals "no per-part data available". - var setup = new Setup - { - Parts = { 0x01000100u, 0x01000101u }, - }; - - var transforms = SetupPartTransforms.Compute(setup); - - Assert.Empty(transforms); - } - - [Fact] - public void Compute_AppliesDefaultScale_WhenPresent() - { - // DefaultScale = (2,2,2) on part 0. An input (1,1,1) should - // come out (2,2,2) after the part transform β€” confirms the - // CreateScale factor is present in the matrix. - var setup = new Setup - { - Parts = { 0x01000100u }, - DefaultScale = { new Vector3(2f, 2f, 2f) }, - PlacementFrames = - { - [Placement.Resting] = new AnimationFrame(1) - { - Frames = - { - new Frame { Origin = Vector3.Zero, Orientation = Quaternion.Identity }, - }, - }, - }, - }; - - var transforms = SetupPartTransforms.Compute(setup); - - Assert.Single(transforms); - var probe = Vector3.Transform(new Vector3(1f, 1f, 1f), transforms[0]); - Assert.Equal(new Vector3(2f, 2f, 2f), probe); - } -} diff --git a/tests/AcDream.Core.Tests/Physics/CollisionExemptionTests.cs b/tests/AcDream.Core.Tests/Physics/CollisionExemptionTests.cs index 3950bd9..b843165 100644 --- a/tests/AcDream.Core.Tests/Physics/CollisionExemptionTests.cs +++ b/tests/AcDream.Core.Tests/Physics/CollisionExemptionTests.cs @@ -42,16 +42,12 @@ public class CollisionExemptionTests } [Fact] - public void EtherealOnly_Skipped() + public void EtherealOnly_NotSkipped() { - // L.2g slice 1b (2026-05-13): ETHEREAL alone exempts collision. - // Retail (acclient_2013_pseudo_c.txt:276782) required both bits, - // but ACE's Door.Open() broadcasts ETHEREAL alone β€” observed - // live: state=0x0001000C (HasPhysicsBSP | Ethereal | ReportCollisions). - // Pragmatic shortcut: widen the early-out to ETHEREAL alone so - // doors become passable when ACE flips the bit. Retail-server - // broadcasts (state=0x14+) still hit the same branch correctly. - Assert.True(CollisionExemption.ShouldSkip( + // Target with ETHEREAL but NOT IGNORE_COLLISIONS does not bail + // out at the first gate β€” collision proceeds. (Step-down marks + // obstruction_ethereal, but does not exempt.) + Assert.False(CollisionExemption.ShouldSkip( targetState: ETHEREAL_PS, targetFlags: EntityCollisionFlags.None, moverState: ObjectInfoState.IsPlayer)); diff --git a/tests/AcDream.Core.Tests/Physics/PhysicsDiagnosticsTests.cs b/tests/AcDream.Core.Tests/Physics/PhysicsDiagnosticsTests.cs deleted file mode 100644 index 5103de1..0000000 --- a/tests/AcDream.Core.Tests/Physics/PhysicsDiagnosticsTests.cs +++ /dev/null @@ -1,98 +0,0 @@ -using AcDream.Core.Physics; -using DatReaderWriter.Enums; -using System.Numerics; -using Xunit; - -namespace AcDream.Core.Tests.Physics; - -/// -/// L.2d slice 1 (2026-05-13) β€” unit coverage for the new -/// flag and -/// diagnostic -/// side-channel. -/// -/// -/// The full multi-line [resolve-bldg] format itself is verified -/// by the slice's acceptance criterion #2 (live Holtburg-doorway -/// capture) β€” covering it here would require a heavy -/// PhysicsEngine + ShadowObjectRegistry + Transition -/// fixture for what's a diagnostic-only emission. These tests pin the -/// static API contract that the emission code depends on; if either of -/// these tests breaks the emission will start producing stale data or -/// failing to emit at all. -/// -/// -public class PhysicsDiagnosticsTests -{ - // ----------------------------------------------------------------------- - // ProbeBuildingEnabled β€” flag gates the emission path. - // ----------------------------------------------------------------------- - - [Fact] - public void ProbeBuilding_StaticApi_Roundtrip() - { - bool initial = PhysicsDiagnostics.ProbeBuildingEnabled; - try - { - PhysicsDiagnostics.ProbeBuildingEnabled = true; - Assert.True(PhysicsDiagnostics.ProbeBuildingEnabled); - - PhysicsDiagnostics.ProbeBuildingEnabled = false; - Assert.False(PhysicsDiagnostics.ProbeBuildingEnabled); - } - finally - { - // Restore so a process-wide static doesn't leak between tests - // (env-var init was the only thing that set this before). - PhysicsDiagnostics.ProbeBuildingEnabled = initial; - } - } - - // ----------------------------------------------------------------------- - // LastBspHitPoly β€” side-channel set by BSPQuery, read by FindObjCollisions. - // - // TransitionTypes.FindObjCollisions clears this to null before each - // shadow-entry dispatch; BSPQuery writes to it on hit when the probe is - // on; the emission site reads it. A failure here means the side-channel - // can't carry data through the call chain. - // ----------------------------------------------------------------------- - - [Fact] - public void LastBspHitPoly_StaticApi_Roundtrip() - { - ResolvedPolygon? initial = PhysicsDiagnostics.LastBspHitPoly; - try - { - PhysicsDiagnostics.LastBspHitPoly = null; - Assert.Null(PhysicsDiagnostics.LastBspHitPoly); - - var synthetic = new ResolvedPolygon - { - Vertices = new[] - { - new Vector3(-1f, 0f, 0f), - new Vector3( 1f, 0f, 0f), - new Vector3( 1f, 0f, 2f), - new Vector3(-1f, 0f, 2f), - }, - Plane = new System.Numerics.Plane(0f, 1f, 0f, -94.123f), - NumPoints = 4, - SidesType = CullMode.None, - }; - PhysicsDiagnostics.LastBspHitPoly = synthetic; - - var read = PhysicsDiagnostics.LastBspHitPoly; - Assert.NotNull(read); - Assert.Equal(4, read!.NumPoints); - Assert.Equal(synthetic.Plane.D, read.Plane.D); - Assert.Same(synthetic, read); - - PhysicsDiagnostics.LastBspHitPoly = null; - Assert.Null(PhysicsDiagnostics.LastBspHitPoly); - } - finally - { - PhysicsDiagnostics.LastBspHitPoly = initial; - } - } -} diff --git a/tests/AcDream.Core.Tests/Physics/RemoteMoveToDriverTests.cs b/tests/AcDream.Core.Tests/Physics/RemoteMoveToDriverTests.cs index 659452a..39182cb 100644 --- a/tests/AcDream.Core.Tests/Physics/RemoteMoveToDriverTests.cs +++ b/tests/AcDream.Core.Tests/Physics/RemoteMoveToDriverTests.cs @@ -293,38 +293,4 @@ public class RemoteMoveToDriverTests Assert.Equal(20f, w.Y); Assert.Equal(0f, w.Z); } - - [Fact] - public void TurnRateFor_WalkingReturnsBaseRate() - { - // Retail: omega.z = Β±Ο€/2 Γ— turn_speed (1.0) = Ο€/2 rad/s β‰ˆ 90Β°/s - // Anchor: docs/research/named-retail/acclient_2013_pseudo_c.txt - // CMotionInterp::apply_run_to_command 0x00527be0 only - // multiplies under HoldKey.Run β€” walking is unscaled. - float rate = RemoteMoveToDriver.TurnRateFor(running: false); - Assert.Equal(MathF.PI / 2.0f, rate, precision: 5); - } - - [Fact] - public void TurnRateFor_RunningAppliesRunTurnFactor() - { - // Retail: omega.z = Β±Ο€/2 Γ— turn_speed Γ— run_turn_factor - // run_turn_factor = 1.5f at 0x007c8914 (PDB-named). - // apply_run_to_command (acclient_2013_pseudo_c.txt:305098) - // multiplies turn_speed by 1.5f when input is TurnRight - // under HoldKey.Run. - float rate = RemoteMoveToDriver.TurnRateFor(running: true); - Assert.Equal(MathF.PI / 2.0f * 1.5f, rate, precision: 5); - } - - [Fact] - public void TurnRateRadPerSec_BackCompatStillResolvesToWalkingRate() - { - // Existing call sites that haven't yet migrated to TurnRateFor - // (e.g., RemoteMoveToDriver.Drive's TurnSpeed=1.0 callers) still - // see the walking-rate constant. Same numerical value as - // BaseTurnRateRadPerSec. - Assert.Equal(RemoteMoveToDriver.BaseTurnRateRadPerSec, - RemoteMoveToDriver.TurnRateRadPerSec, precision: 5); - } } diff --git a/tests/AcDream.Core.Tests/Physics/ShadowObjectRegistryTests.cs b/tests/AcDream.Core.Tests/Physics/ShadowObjectRegistryTests.cs index f3d7b08..73143d8 100644 --- a/tests/AcDream.Core.Tests/Physics/ShadowObjectRegistryTests.cs +++ b/tests/AcDream.Core.Tests/Physics/ShadowObjectRegistryTests.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Linq; using System.Numerics; using AcDream.Core.Physics; using Xunit; @@ -253,52 +252,4 @@ public class ShadowObjectRegistryTests Assert.Equal(0u, entry.State); Assert.Equal(EntityCollisionFlags.None, entry.Flags); } - - // ----------------------------------------------------------------------- - // UpdatePhysicsState β€” L.2g slice 1 (doors flip ETHEREAL post-spawn) - // ----------------------------------------------------------------------- - - [Fact] - public void UpdatePhysicsState_FlipsEthereal_NextLookupSeesNewBits() - { - var reg = new ShadowObjectRegistry(); - const uint doorId = 0x000F4244u; - reg.Register(doorId, 0x020019FFu, new Vector3(12f, 12f, 50f), - Quaternion.Identity, 1f, OffX, OffY, LbId, - state: 0u, flags: EntityCollisionFlags.None); - - var before = reg.AllEntriesForDebug().Single(e => e.EntityId == doorId); - Assert.Equal(0u, before.State); - - reg.UpdatePhysicsState(doorId, 0x00000004u); - - var after = reg.AllEntriesForDebug().Single(e => e.EntityId == doorId); - Assert.Equal(0x00000004u, after.State); - } - - [Fact] - public void UpdatePhysicsState_UnregisteredEntity_IsNoOp() - { - var reg = new ShadowObjectRegistry(); - reg.UpdatePhysicsState(0xDEADBEEFu, 0x00000004u); - Assert.Equal(0, reg.TotalRegistered); - } - - [Fact] - public void UpdatePhysicsState_EntitySpanningMultipleCells_AllCellsUpdated() - { - var reg = new ShadowObjectRegistry(); - reg.Register(99u, 0x01000099u, new Vector3(24f, 12f, 50f), - Quaternion.Identity, 2f, OffX, OffY, LbId, - state: 0u); - - reg.UpdatePhysicsState(99u, 0x00000004u); - - uint cellA = LbId | 1u; - uint cellB = LbId | (1u*8 + 0 + 1); - var inA = reg.GetObjectsInCell(cellA).Single(e => e.EntityId == 99u); - var inB = reg.GetObjectsInCell(cellB).Single(e => e.EntityId == 99u); - Assert.Equal(0x00000004u, inA.State); - Assert.Equal(0x00000004u, inB.State); - } } diff --git a/tests/AcDream.Core.Tests/Physics/TerrainSurfaceTests.cs b/tests/AcDream.Core.Tests/Physics/TerrainSurfaceTests.cs index 17a756c..c26b214 100644 --- a/tests/AcDream.Core.Tests/Physics/TerrainSurfaceTests.cs +++ b/tests/AcDream.Core.Tests/Physics/TerrainSurfaceTests.cs @@ -139,52 +139,6 @@ public class TerrainSurfaceTests Assert.Contains(sample.Vertices, v => v.X == 0f && v.Y == 0f); } - [Fact] - public void SampleNormalZFromHeightmap_FlatTerrain_ReturnsOne() - { - var heights = FlatHeightmap(50); - var hTable = LinearHeightTable(); - float nz = TerrainSurface.SampleNormalZFromHeightmap(heights, hTable, 0, 0, 96f, 96f); - Assert.Equal(1f, nz, precision: 5); - } - - [Fact] - public void SampleNormalZFromHeightmap_SlopedTerrain_ReturnsLessThanOne() - { - var heights = new byte[81]; - for (int x = 0; x < 9; x++) - for (int y = 0; y < 9; y++) - heights[x * 9 + y] = (byte)(x * 20); - var hTable = LinearHeightTable(); - float nz = TerrainSurface.SampleNormalZFromHeightmap(heights, hTable, 0, 0, 12f, 12f); - Assert.True(nz < 1f, $"Sloped terrain should have nz < 1.0, got {nz}"); - Assert.True(nz > 0f, $"nz should be positive, got {nz}"); - } - - [Fact] - public void SampleNormalZFromHeightmap_AgreesWithSampleSurface() - { - var heights = new byte[81]; - for (int x = 0; x < 9; x++) - for (int y = 0; y < 9; y++) - heights[x * 9 + y] = (byte)((x * 17 + y * 13) % 256); - - var hTable = LinearHeightTable(); - const uint lbX = 0xA9, lbY = 0xB3; - var instance = new TerrainSurface(heights, hTable, lbX, lbY); - - for (float lx = 0.5f; lx < 192f; lx += 8f) - for (float ly = 0.5f; ly < 192f; ly += 8f) - { - var (_, normal) = instance.SampleSurface(lx, ly); - float staticNz = TerrainSurface.SampleNormalZFromHeightmap( - heights, hTable, lbX, lbY, lx, ly); - Assert.True( - Math.Abs(normal.Z - staticNz) < 0.0001f, - $"NormalZ mismatch at ({lx:F1},{ly:F1}): instance={normal.Z:F4} static={staticNz:F4}"); - } - } - [Fact] public void ComputeOutdoorCellId_Origin_ReturnsFirst() { diff --git a/tests/AcDream.Core.Tests/Rendering/TextureCacheBindlessTests.cs b/tests/AcDream.Core.Tests/Rendering/TextureCacheBindlessTests.cs deleted file mode 100644 index 88877f6..0000000 --- a/tests/AcDream.Core.Tests/Rendering/TextureCacheBindlessTests.cs +++ /dev/null @@ -1,32 +0,0 @@ -using AcDream.App.Rendering; -using AcDream.App.Rendering.Wb; -using DatReaderWriter; -using Xunit; - -namespace AcDream.Core.Tests.Rendering; - -/// -/// Lightweight unit tests for 's bindless path. -/// We can't construct a real TextureCache in a headless test (it requires a -/// live GL context), so this file documents contracts that future engineers -/// should preserve. Real bindless integration is verified at Task 14's -/// visual gate. -/// -public sealed class TextureCacheBindlessTests -{ - [Fact] - public void Contract_BindlessMethodsThrowWithoutBindlessSupport() - { - // The actual throw lives in TextureCache.EnsureBindlessAvailable - // and is reached only via GL-bound Bindless* method calls. The - // contract is: if the dispatcher (which requires bindless) ever - // gets a TextureCache constructed without BindlessSupport, it - // should fail-fast with InvalidOperationException β€” NOT silently - // route a draw to handle 0 (which would produce a non-resident - // GPU fault). - // - // This test is a marker. Future engineers: do not weaken - // EnsureBindlessAvailable to swallow the missing dependency. - Assert.True(true, "Contract documented in TextureCache.EnsureBindlessAvailable"); - } -} diff --git a/tests/AcDream.Core.Tests/Rendering/Vfx/EntityScriptActivatorTests.cs b/tests/AcDream.Core.Tests/Rendering/Vfx/EntityScriptActivatorTests.cs deleted file mode 100644 index 4a5bd3f..0000000 --- a/tests/AcDream.Core.Tests/Rendering/Vfx/EntityScriptActivatorTests.cs +++ /dev/null @@ -1,327 +0,0 @@ -using System.Collections.Generic; -using System.Numerics; -using AcDream.App.Rendering.Vfx; -using AcDream.Core.Physics; -using AcDream.Core.Vfx; -using AcDream.Core.World; -using DatReaderWriter.Types; -using Xunit; -using DatPhysicsScript = DatReaderWriter.DBObjs.PhysicsScript; - -namespace AcDream.Core.Tests.Rendering.Vfx; - -public sealed class EntityScriptActivatorTests -{ - /// Recording sink so we can assert which hooks the runner fires. - private sealed class RecordingSink : IAnimationHookSink - { - public List<(uint EntityId, Vector3 Pos, AnimationHook Hook)> Calls = new(); - public void OnHook(uint entityId, Vector3 worldPos, AnimationHook hook) - => Calls.Add((entityId, worldPos, hook)); - } - - private static DatPhysicsScript BuildScript(params (double time, AnimationHook hook)[] items) - { - var script = new DatPhysicsScript(); - foreach (var (t, h) in items) - script.ScriptData.Add(new PhysicsScriptData { StartTime = t, Hook = h }); - return script; - } - - private static WorldEntity MakeEntity(uint serverGuid, Vector3 position) => - new() - { - Id = serverGuid, - ServerGuid = serverGuid, - SourceGfxObjOrSetupId = 0x02000001u, - Position = position, - Rotation = Quaternion.Identity, - MeshRefs = System.Array.Empty(), - }; - - private record Pipeline( - ParticleSystem System, - ParticleHookSink Sink, - PhysicsScriptRunner Runner, - RecordingSink Recording); - - private static Pipeline BuildPipeline(params (uint id, DatPhysicsScript script)[] scripts) - { - var registry = new EmitterDescRegistry(); - var system = new ParticleSystem(registry); - var hookSink = new ParticleHookSink(system); // for activator's StopAllForEntity - var recording = new RecordingSink(); // for runner's hook dispatch - var table = new Dictionary(); - foreach (var (id, s) in scripts) table[id] = s; - var runner = new PhysicsScriptRunner( - id => table.TryGetValue(id, out var s) ? s : null, - recording); - return new Pipeline(system, hookSink, runner, recording); - } - - /// - /// Convenience: a resolver that always returns the given scriptId with - /// an empty part-transforms list (the C.1.5a-equivalent β€” no per-part - /// math). Useful for tests that exercise the scheduler without caring - /// about #56's per-part pipeline. - /// - private static System.Func StaticResolver(uint scriptId) - => _ => new ScriptActivationInfo(scriptId, System.Array.Empty()); - - [Fact] - public void OnCreate_WithDefaultScript_FiresRunnerWithEntityGuidAndPosition() - { - var p = BuildPipeline( - (0xAAu, BuildScript((0.0, new CreateParticleHook { EmitterInfoId = 100 })))); - var activator = new EntityScriptActivator(p.Runner, p.Sink, StaticResolver(0xAAu)); - var entity = MakeEntity(serverGuid: 0xCAFEu, position: new Vector3(1, 2, 3)); - - activator.OnCreate(entity); - - Assert.Equal(1, p.Runner.ActiveScriptCount); - p.Runner.Tick(0.001f); - Assert.Single(p.Recording.Calls); - Assert.Equal(0xCAFEu, p.Recording.Calls[0].EntityId); - Assert.Equal(new Vector3(1, 2, 3), p.Recording.Calls[0].Pos); - } - - [Fact] - public void OnCreate_WithoutDefaultScript_DoesNothing() - { - var p = BuildPipeline(); // no scripts registered - var activator = new EntityScriptActivator(p.Runner, p.Sink, _ => null); - var entity = MakeEntity(serverGuid: 0xCAFEu, position: Vector3.Zero); - - activator.OnCreate(entity); - - Assert.Equal(0, p.Runner.ActiveScriptCount); - Assert.Empty(p.Recording.Calls); - } - - /// - /// Persistent emitter: TotalDuration=0 and TotalParticles=0 prevent - /// auto-finish; InitialParticles=1 ensures a particle spawns at t=0 - /// without waiting for the Birthrate timer; Lifespan=999f keeps that - /// particle alive far past the test horizon. - /// - private static EmitterDesc BuildPersistentEmitterDesc() => new() - { - DatId = 100u, - Type = ParticleType.Still, - EmitterKind = ParticleEmitterKind.BirthratePerSec, - MaxParticles = 4, - InitialParticles = 1, - TotalParticles = 0, // 0 = no particle-count cap - TotalDuration = 0f, // 0 = no time-based finish - Lifespan = 999f, - LifetimeMin = 999f, - LifetimeMax = 999f, - Birthrate = 0.5f, - StartSize = 0.5f, - EndSize = 0.5f, - StartAlpha = 1f, - EndAlpha = 1f, - }; - - [Fact] - public void OnCreate_SetsEntityRotationForHookOffsetTransform() - { - // The CreateParticleHook's Offset is in entity-local frame; the sink - // needs the entity's rotation to transform it to world space. If the - // activator forgets SetEntityRotation, the offset goes off in world - // axes β€” visual symptom: portal swirls misaligned to the portal stone. - // This test verifies the seed happens by checking the spawned particle's - // world position matches the rotated offset, not the unrotated offset. - - var registry = new EmitterDescRegistry(); - registry.Register(BuildPersistentEmitterDesc()); - - var system = new ParticleSystem(registry); - var hookSink = new ParticleHookSink(system); - - // Hook offset = (1, 0, 0) in entity-local frame. - var hookOffset = new Frame - { - Origin = new Vector3(1f, 0f, 0f), - Orientation = Quaternion.Identity, - }; - var script = BuildScript( - (0.0, new CreateParticleHook { EmitterInfoId = 100u, Offset = hookOffset })); - var table = new Dictionary { [0xAAu] = script }; - var runner = new PhysicsScriptRunner( - id => table.TryGetValue(id, out var s) ? s : null, - hookSink); - - var activator = new EntityScriptActivator(runner, hookSink, StaticResolver(0xAAu)); - - // Entity rotated 90Β° around world-Z (yaw left); local +X maps to world +Y. - var entityRotation = Quaternion.CreateFromAxisAngle( - Vector3.UnitZ, MathF.PI / 2f); - var entity = new WorldEntity - { - Id = 0xCAFEu, - ServerGuid = 0xCAFEu, - SourceGfxObjOrSetupId = 0x02000001u, - Position = Vector3.Zero, - Rotation = entityRotation, - MeshRefs = System.Array.Empty(), - }; - - activator.OnCreate(entity); - runner.Tick(0.001f); - system.Tick(0.001f); - - // Find the live particle. With the rotation applied, world position of - // the local-(1,0,0) offset should be approximately world-(0,1,0). Without - // the rotation seed (the bug), it would be world-(1,0,0). - var live = system.EnumerateLive().FirstOrDefault(); - Assert.NotNull(live.Emitter); - var worldPos = live.Emitter.Particles[live.Index].Position; - Assert.InRange(worldPos.X, -0.01f, 0.01f); - Assert.InRange(worldPos.Y, 0.99f, 1.01f); - } - - [Fact] - public void OnRemove_StopsScriptsAndEmitters() - { - // For this test we need the runner to dispatch into the REAL - // ParticleHookSink so OnRemove's sink.StopAllForEntity has a live - // emitter to kill. This is the only observable way to verify the - // call had effect without subclassing the sealed sink. - var registry = new EmitterDescRegistry(); - registry.Register(BuildPersistentEmitterDesc()); - - var system = new ParticleSystem(registry); - var hookSink = new ParticleHookSink(system); - - var script = BuildScript((0.0, new CreateParticleHook { EmitterInfoId = 100u, Offset = new Frame() })); - var table = new Dictionary { [0xAAu] = script }; - var runner = new PhysicsScriptRunner( - id => table.TryGetValue(id, out var s) ? s : null, - hookSink); // runner dispatches into real sink, not RecordingSink - - var activator = new EntityScriptActivator(runner, hookSink, StaticResolver(0xAAu)); - var entity = MakeEntity(serverGuid: 0xCAFEu, position: Vector3.Zero); - - activator.OnCreate(entity); - runner.Tick(0.001f); // fires the CreateParticleHook β†’ spawns emitter - - Assert.True(system.ActiveEmitterCount > 0, - "Setup precondition failed: emitter should be alive after the hook fires."); - - activator.OnRemove(0xCAFEu); - - Assert.Equal(0, runner.ActiveScriptCount); - // sink.StopAllForEntity marks the emitter Finished; system.Tick reaps it. - system.Tick(0.01f); - Assert.Equal(0, system.ActiveEmitterCount); - } - - [Fact] - public void OnCreate_KeysByEntityId_WhenServerGuidZero() - { - // C.1.5b: dat-hydrated EnvCell statics + exterior stabs have - // ServerGuid == 0 but a stable entity.Id in the 0x40xxxxxx range. - // OnCreate must use entity.Id as the key (not skip). - var p = BuildPipeline( - (0xAAu, BuildScript((0.0, new CreateParticleHook { EmitterInfoId = 100 })))); - var activator = new EntityScriptActivator(p.Runner, p.Sink, StaticResolver(0xAAu)); - var entity = new WorldEntity - { - Id = 0x40A9B401u, // dat-hydrated interior id - ServerGuid = 0u, // no server guid - SourceGfxObjOrSetupId = 0x02000001u, - Position = new Vector3(5, 5, 5), - Rotation = Quaternion.Identity, - MeshRefs = System.Array.Empty(), - }; - - activator.OnCreate(entity); - - Assert.Equal(1, p.Runner.ActiveScriptCount); - p.Runner.Tick(0.001f); - Assert.Single(p.Recording.Calls); - Assert.Equal(0x40A9B401u, p.Recording.Calls[0].EntityId); - Assert.Equal(new Vector3(5, 5, 5), p.Recording.Calls[0].Pos); - } - - [Fact] - public void OnCreate_PassesPartTransformsToSink() - { - // C.1.5b #56: end-to-end test that the activator pushes the - // resolver's PartTransforms into the sink, and the sink applies - // them. Part 1 lifted +Z=1; hookOffset (1,0,0) with PartIndex=1 - // + identity rotation β†’ expected world (1, 0, 1). - var registry = new EmitterDescRegistry(); - registry.Register(BuildPersistentEmitterDesc()); - var system = new ParticleSystem(registry); - var hookSink = new ParticleHookSink(system); - - var hookOffset = new Frame { Origin = new Vector3(1f, 0, 0), Orientation = Quaternion.Identity }; - var script = BuildScript( - (0.0, new CreateParticleHook { EmitterInfoId = 100u, Offset = hookOffset, PartIndex = 1 })); - var table = new Dictionary { [0xAAu] = script }; - var runner = new PhysicsScriptRunner( - id => table.TryGetValue(id, out var s) ? s : null, - hookSink); - - var partTransforms = new Matrix4x4[] - { - Matrix4x4.Identity, - Matrix4x4.CreateTranslation(0f, 0f, 1f), - }; - - var activator = new EntityScriptActivator(runner, hookSink, - _ => new ScriptActivationInfo(0xAAu, partTransforms)); - var entity = MakeEntity(serverGuid: 0xCAFEu, position: Vector3.Zero); - - activator.OnCreate(entity); - runner.Tick(0.001f); - system.Tick(0.001f); - - var live = system.EnumerateLive().FirstOrDefault(); - Assert.NotNull(live.Emitter); - var pos = live.Emitter.Particles[live.Index].Position; - Assert.InRange(pos.X, 0.99f, 1.01f); - Assert.InRange(pos.Y, -0.01f, 0.01f); - Assert.InRange(pos.Z, 0.99f, 1.01f); - } - - [Fact] - public void OnRemove_StopsByGivenKey_ForDatHydratedEntity() - { - // C.1.5b: caller passes the entity.Id as the key for dat-hydrated - // entities (not ServerGuid). OnRemove must clean up correctly. - var registry = new EmitterDescRegistry(); - registry.Register(BuildPersistentEmitterDesc()); - var system = new ParticleSystem(registry); - var hookSink = new ParticleHookSink(system); - - var script = BuildScript((0.0, new CreateParticleHook { EmitterInfoId = 100u, Offset = new Frame() })); - var table = new Dictionary { [0xAAu] = script }; - var runner = new PhysicsScriptRunner( - id => table.TryGetValue(id, out var s) ? s : null, - hookSink); - - var activator = new EntityScriptActivator(runner, hookSink, StaticResolver(0xAAu)); - var entity = new WorldEntity - { - Id = 0x40A9B402u, - ServerGuid = 0u, - SourceGfxObjOrSetupId = 0x02000001u, - Position = Vector3.Zero, - Rotation = Quaternion.Identity, - MeshRefs = System.Array.Empty(), - }; - - activator.OnCreate(entity); - runner.Tick(0.001f); - Assert.True(system.ActiveEmitterCount > 0); - - activator.OnRemove(0x40A9B402u); // caller passes the entity.Id key - - Assert.Equal(0, runner.ActiveScriptCount); - system.Tick(0.01f); - Assert.Equal(0, system.ActiveEmitterCount); - } -} diff --git a/tests/AcDream.Core.Tests/Rendering/Wb/AcSurfaceMetadataTableTests.cs b/tests/AcDream.Core.Tests/Rendering/Wb/AcSurfaceMetadataTableTests.cs deleted file mode 100644 index 23aa231..0000000 --- a/tests/AcDream.Core.Tests/Rendering/Wb/AcSurfaceMetadataTableTests.cs +++ /dev/null @@ -1,72 +0,0 @@ -using AcDream.App.Rendering.Wb; -using AcDream.Core.Meshing; - -namespace AcDream.Core.Tests.Rendering.Wb; - -public sealed class AcSurfaceMetadataTableTests -{ - [Fact] - public void Add_ThenLookup_RoundTripsSameMetadata() - { - var table = new AcSurfaceMetadataTable(); - var meta = new AcSurfaceMetadata( - Translucency: TranslucencyKind.AlphaBlend, - Luminosity: 0.5f, - Diffuse: 0.8f, - SurfOpacity: 0.7f, - NeedsUvRepeat: true, - DisableFog: false); - - table.Add(gfxObjId: 0x01000123ul, surfaceIdx: 2, meta); - - Assert.True(table.TryLookup(0x01000123ul, 2, out var got)); - Assert.Equal(meta, got); - } - - [Fact] - public void Lookup_MissingKey_ReturnsFalse() - { - var table = new AcSurfaceMetadataTable(); - Assert.False(table.TryLookup(0xDEADBEEFul, 0, out _)); - } - - [Fact] - public void Add_OverwritesPreviousMetadata() - { - var table = new AcSurfaceMetadataTable(); - var first = new AcSurfaceMetadata(TranslucencyKind.Opaque, 0f, 1f, 1f, false, false); - var second = new AcSurfaceMetadata(TranslucencyKind.Additive, 1f, 1f, 1f, false, true); - - table.Add(0xAAAA, 0, first); - table.Add(0xAAAA, 0, second); - - Assert.True(table.TryLookup(0xAAAA, 0, out var got)); - Assert.Equal(second, got); - } - - [Fact] - public void Add_FromMultipleThreads_IsThreadSafe() - { - var table = new AcSurfaceMetadataTable(); - var threads = new System.Threading.Tasks.Task[8]; - for (int t = 0; t < 8; t++) - { - int threadIdx = t; - threads[t] = System.Threading.Tasks.Task.Run(() => - { - for (int i = 0; i < 1000; i++) - { - ulong key = (ulong)(threadIdx * 1000 + i); - table.Add(key, 0, new AcSurfaceMetadata( - TranslucencyKind.Opaque, 0f, 1f, 1f, false, false)); - } - }); - } - System.Threading.Tasks.Task.WaitAll(threads); - - // 8000 entries should be present. - for (int t = 0; t < 8; t++) - for (int i = 0; i < 1000; i++) - Assert.True(table.TryLookup((ulong)(t * 1000 + i), 0, out _)); - } -} diff --git a/tests/AcDream.Core.Tests/Rendering/Wb/AnimPartChangeTests.cs b/tests/AcDream.Core.Tests/Rendering/Wb/AnimPartChangeTests.cs deleted file mode 100644 index d603ccd..0000000 --- a/tests/AcDream.Core.Tests/Rendering/Wb/AnimPartChangeTests.cs +++ /dev/null @@ -1,62 +0,0 @@ -using AcDream.App.Rendering.Wb; -using AcDream.Core.Physics; -using DatReaderWriter.DBObjs; -using Xunit; - -namespace AcDream.Core.Tests.Rendering.Wb; - -public sealed class AnimPartChangeTests -{ - [Fact] - public void SetPartOverride_ResolvedAtLookup() - { - var state = MakeState(); - - state.SetPartOverride(partIdx: 5, gfxObjId: 0x01001234ul); - - Assert.True(state.TryGetPartOverride(5, out var got)); - Assert.Equal(0x01001234ul, got); - Assert.False(state.TryGetPartOverride(6, out _)); - } - - [Fact] - public void SetPartOverride_TwiceForSamePart_TakesLatest() - { - var state = MakeState(); - - state.SetPartOverride(0, 0x01000001ul); - state.SetPartOverride(0, 0x01999999ul); - - Assert.True(state.TryGetPartOverride(0, out var got)); - Assert.Equal(0x01999999ul, got); - } - - [Fact] - public void ResolvePartGfxObj_WithoutOverride_ReturnsSetupDefault() - { - var state = MakeState(); - - Assert.Equal(0x01000001ul, - state.ResolvePartGfxObj(partIdx: 0, setupDefault: 0x01000001ul)); - } - - [Fact] - public void ResolvePartGfxObj_WithOverride_ReturnsOverride() - { - var state = MakeState(); - state.SetPartOverride(partIdx: 0, gfxObjId: 0x01999999ul); - - Assert.Equal(0x01999999ul, - state.ResolvePartGfxObj(partIdx: 0, setupDefault: 0x01000001ul)); - } - - private static AnimatedEntityState MakeState() => new(MakeSequencer()); - - private static AnimationSequencer MakeSequencer() - => new AnimationSequencer(new Setup(), new MotionTable(), new NullAnimationLoader()); - - private sealed class NullAnimationLoader : IAnimationLoader - { - public Animation? LoadAnimation(uint id) => null; - } -} diff --git a/tests/AcDream.Core.Tests/Rendering/Wb/AnimatedEntityStateTests.cs b/tests/AcDream.Core.Tests/Rendering/Wb/AnimatedEntityStateTests.cs deleted file mode 100644 index aae14dd..0000000 --- a/tests/AcDream.Core.Tests/Rendering/Wb/AnimatedEntityStateTests.cs +++ /dev/null @@ -1,45 +0,0 @@ -using AcDream.App.Rendering.Wb; -using AcDream.Core.Physics; -using DatReaderWriter.DBObjs; -using Xunit; - -namespace AcDream.Core.Tests.Rendering.Wb; - -public sealed class AnimatedEntityStateTests -{ - [Fact] - public void DefaultState_HasNoOverridesAndNoHiddenParts() - { - var state = MakeState(); - - Assert.False(state.IsPartHidden(0)); - Assert.False(state.IsPartHidden(63)); - Assert.False(state.TryGetPartOverride(0, out _)); - } - - [Fact] - public void Sequencer_AccessibleAsProperty() - { - var sequencer = MakeSequencer(); - var state = new AnimatedEntityState(sequencer); - - Assert.Same(sequencer, state.Sequencer); - } - - [Fact] - public void Construct_WithNullSequencer_ThrowsArgumentNull() - { - Assert.Throws( - () => new AnimatedEntityState(null!)); - } - - private static AnimatedEntityState MakeState() => new(MakeSequencer()); - - private static AnimationSequencer MakeSequencer() - => new AnimationSequencer(new Setup(), new MotionTable(), new NullAnimationLoader()); - - private sealed class NullAnimationLoader : IAnimationLoader - { - public Animation? LoadAnimation(uint id) => null; - } -} diff --git a/tests/AcDream.Core.Tests/Rendering/Wb/EntityClassificationCacheTests.cs b/tests/AcDream.Core.Tests/Rendering/Wb/EntityClassificationCacheTests.cs deleted file mode 100644 index c52cad8..0000000 --- a/tests/AcDream.Core.Tests/Rendering/Wb/EntityClassificationCacheTests.cs +++ /dev/null @@ -1,285 +0,0 @@ -using System.Collections.Generic; -using System.Numerics; -using AcDream.App.Rendering.Wb; -using AcDream.Core.Meshing; -using Xunit; - -namespace AcDream.Core.Tests.Rendering.Wb; - -public class EntityClassificationCacheTests -{ - [Fact] - public void TryGet_EmptyCache_ReturnsFalse() - { - var cache = new EntityClassificationCache(); - bool found = cache.TryGet(entityId: 42, landblockHint: 0u, out var entry); - Assert.False(found); - Assert.Null(entry); - } - - [Fact] - public void Populate_ThenTryGet_ReturnsBatchesInOrder() - { - var cache = new EntityClassificationCache(); - var batches = new[] - { - MakeCachedBatch(ibo: 1, firstIndex: 0, indexCount: 6, texHandle: 0xAA), - MakeCachedBatch(ibo: 1, firstIndex: 6, indexCount: 6, texHandle: 0xBB), - }; - - cache.Populate(entityId: 100, landblockHint: 0xA9B40000u, batches); - - Assert.True(cache.TryGet(100, 0xA9B40000u, out var entry)); - Assert.NotNull(entry); - Assert.Equal(100u, entry!.EntityId); - Assert.Equal(0xA9B40000u, entry.LandblockHint); - Assert.Equal(batches, entry.Batches); - } - - [Fact] - public void Populate_OverridesExistingEntry() - { - var cache = new EntityClassificationCache(); - cache.Populate(100, 0u, new[] { MakeCachedBatch(1, 0, 6, 0xAA) }); - cache.Populate(100, 0u, new[] { MakeCachedBatch(2, 0, 12, 0xCC) }); - - Assert.True(cache.TryGet(100, 0u, out var entry)); - Assert.NotNull(entry); - Assert.Single(entry!.Batches); - Assert.Equal(0xCCu, entry.Batches[0].BindlessTextureHandle); - } - - [Fact] - public void Count_TracksLiveEntries() - { - var cache = new EntityClassificationCache(); - Assert.Equal(0, cache.Count); - - cache.Populate(1, 0u, new[] { MakeCachedBatch(1, 0, 6, 0xAA) }); - Assert.Equal(1, cache.Count); - - cache.Populate(2, 0u, new[] { MakeCachedBatch(2, 0, 6, 0xAA) }); - Assert.Equal(2, cache.Count); - - // Re-populate same id β€” should not double-count. - cache.Populate(1, 0u, new[] { MakeCachedBatch(3, 0, 6, 0xBB) }); - Assert.Equal(2, cache.Count); - } - - [Fact] - public void Populate_WithEmptyBatches_StoresEmptyEntry() - { - var cache = new EntityClassificationCache(); - cache.Populate(entityId: 7, landblockHint: 0u, System.Array.Empty()); - - Assert.True(cache.TryGet(7, 0u, out var entry)); - Assert.NotNull(entry); - Assert.Empty(entry!.Batches); - } - - [Fact] - public void Populate_SetupMultiPart_StoresFlatBatchPerSubPart() - { - // Synthetic Setup with 3 subParts Γ— 2 batches each = 6 flat entries. - // This pins the spec Β§3 Q4 decision: pre-flatten Setup multi-parts at - // populate time so the per-frame hot path is branchless. - var cache = new EntityClassificationCache(); - var batches = new CachedBatch[6]; - for (int subPart = 0; subPart < 3; subPart++) - for (int b = 0; b < 2; b++) - { - batches[subPart * 2 + b] = MakeCachedBatch( - ibo: (uint)(subPart + 1), - firstIndex: (uint)(b * 6), - indexCount: 6, - texHandle: (ulong)(0x100 + subPart * 2 + b)); - } - cache.Populate(99, 0u, batches); - - Assert.True(cache.TryGet(99, 0u, out var entry)); - Assert.NotNull(entry); - Assert.Equal(6, entry!.Batches.Length); - Assert.Equal(0x100u, entry.Batches[0].BindlessTextureHandle); - Assert.Equal(0x105u, entry.Batches[5].BindlessTextureHandle); - } - - [Fact] - public void InvalidateEntity_RemovesEntry() - { - var cache = new EntityClassificationCache(); - cache.Populate(100, 0u, new[] { MakeCachedBatch(1, 0, 6, 0xAA) }); - Assert.True(cache.TryGet(100, 0u, out _)); - - cache.InvalidateEntity(100); - - Assert.False(cache.TryGet(100, 0u, out var entry)); - Assert.Null(entry); - Assert.Equal(0, cache.Count); - } - - [Fact] - public void InvalidateEntity_OnMissingId_NoThrow() - { - var cache = new EntityClassificationCache(); - var ex = Record.Exception(() => cache.InvalidateEntity(99999)); - Assert.Null(ex); - Assert.Equal(0, cache.Count); - } - - [Fact] - public void InvalidateLandblock_RemovesAllMatchingEntries() - { - var cache = new EntityClassificationCache(); - cache.Populate(1, 0xA9B40000u, new[] { MakeCachedBatch(1, 0, 6, 0xAA) }); - cache.Populate(2, 0xA9B40000u, new[] { MakeCachedBatch(2, 0, 6, 0xBB) }); - cache.Populate(3, 0xA9B40000u, new[] { MakeCachedBatch(3, 0, 6, 0xCC) }); - Assert.Equal(3, cache.Count); - - cache.InvalidateLandblock(0xA9B40000u); - - Assert.Equal(0, cache.Count); - Assert.False(cache.TryGet(1, 0xA9B40000u, out _)); - Assert.False(cache.TryGet(2, 0xA9B40000u, out _)); - Assert.False(cache.TryGet(3, 0xA9B40000u, out _)); - } - - [Fact] - public void InvalidateLandblock_LeavesNonMatchingEntries() - { - var cache = new EntityClassificationCache(); - cache.Populate(1, 0xA9B40000u, new[] { MakeCachedBatch(1, 0, 6, 0xAA) }); - cache.Populate(2, 0xA9B50000u, new[] { MakeCachedBatch(2, 0, 6, 0xBB) }); - cache.Populate(3, 0xA9B40000u, new[] { MakeCachedBatch(3, 0, 6, 0xCC) }); - - cache.InvalidateLandblock(0xA9B40000u); - - Assert.Equal(1, cache.Count); - Assert.False(cache.TryGet(1, 0xA9B40000u, out _)); - Assert.True(cache.TryGet(2, 0xA9B50000u, out var keep)); - Assert.NotNull(keep); - Assert.Equal(0xA9B50000u, keep!.LandblockHint); - Assert.False(cache.TryGet(3, 0xA9B40000u, out _)); - } - - [Fact] - public void InvalidateLandblock_OnMissingLb_NoThrow() - { - var cache = new EntityClassificationCache(); - cache.Populate(1, 0xA9B40000u, new[] { MakeCachedBatch(1, 0, 6, 0xAA) }); - var ex = Record.Exception(() => cache.InvalidateLandblock(0xDEADBEEFu)); - Assert.Null(ex); - Assert.Equal(1, cache.Count); - } - - [Fact] - public void DespawnRespawn_UnderReusedId_RepopulatesFresh() - { - // Pins the audit's ObjDescEvent contract (audit section 1): - // ObjDescEvent is despawn + respawn (with a NEW local entity.Id), - // never an in-place mutation. Even when an id IS reused - // (theoretical β€” _liveEntityIdCounter is monotonic in practice), - // the cache must serve fresh data after invalidation. - var cache = new EntityClassificationCache(); - var batchesV1 = new[] { MakeCachedBatch(1, 0, 6, 0xAA) }; - var batchesV2 = new[] { MakeCachedBatch(2, 6, 12, 0xCC) }; - - cache.Populate(100, 0xA9B40000u, batchesV1); - cache.InvalidateEntity(100); - cache.Populate(100, 0xA9B40000u, batchesV2); - - Assert.True(cache.TryGet(100, 0xA9B40000u, out var entry)); - Assert.NotNull(entry); - Assert.Equal(batchesV2, entry!.Batches); - Assert.Equal(0xCCu, entry.Batches[0].BindlessTextureHandle); - } - -#if DEBUG - [Fact] - public void DebugCrossCheck_BatchCountMismatch_FiresAssert() - { - var cache = new EntityClassificationCache(); - cache.Populate(100, 0u, new[] - { - MakeCachedBatch(1, 0, 6, 0xAA), - MakeCachedBatch(1, 6, 6, 0xBB), - }); - - // Synthetic "live" with fewer batches β†’ should fire Debug.Assert. - var liveBatches = new[] { MakeCachedBatch(1, 0, 6, 0xAA) }; - - // Capture Debug.Assert via a custom TraceListener. - var originalListeners = new System.Diagnostics.TraceListener[System.Diagnostics.Trace.Listeners.Count]; - System.Diagnostics.Trace.Listeners.CopyTo(originalListeners, 0); - System.Diagnostics.Trace.Listeners.Clear(); - var asserts = new List(); - System.Diagnostics.Trace.Listeners.Add(new CaptureListener(asserts)); - - try - { - cache.DebugCrossCheck(100, 0u, liveBatches); - } - finally - { - System.Diagnostics.Trace.Listeners.Clear(); - foreach (var l in originalListeners) System.Diagnostics.Trace.Listeners.Add(l); - } - - Assert.NotEmpty(asserts); - string joined = string.Join(" ", asserts); - Assert.Contains("batch count mismatch", joined); - } - - [Fact] - public void DebugCrossCheck_RestPoseMatch_NoAssert() - { - var cache = new EntityClassificationCache(); - var batches = new[] { MakeCachedBatch(1, 0, 6, 0xAA) }; - cache.Populate(100, 0u, batches); - - var originalListeners = new System.Diagnostics.TraceListener[System.Diagnostics.Trace.Listeners.Count]; - System.Diagnostics.Trace.Listeners.CopyTo(originalListeners, 0); - System.Diagnostics.Trace.Listeners.Clear(); - var asserts = new List(); - System.Diagnostics.Trace.Listeners.Add(new CaptureListener(asserts)); - - try - { - cache.DebugCrossCheck(100, 0u, batches); - } - finally - { - System.Diagnostics.Trace.Listeners.Clear(); - foreach (var l in originalListeners) System.Diagnostics.Trace.Listeners.Add(l); - } - - Assert.Empty(asserts); - } - - private sealed class CaptureListener : System.Diagnostics.TraceListener - { - private readonly List _captured; - public CaptureListener(List captured) { _captured = captured; } - public override void Write(string? message) { if (message != null) _captured.Add(message); } - public override void WriteLine(string? message) { if (message != null) _captured.Add(message); } - public override void Fail(string? message, string? detailMessage) - { - _captured.Add($"{message}: {detailMessage}"); - } - public override void Fail(string? message) { if (message != null) _captured.Add(message); } - } -#endif - - private static CachedBatch MakeCachedBatch( - uint ibo, uint firstIndex, int indexCount, ulong texHandle) - { - var key = new GroupKey( - Ibo: ibo, - FirstIndex: firstIndex, - BaseVertex: 0, - IndexCount: indexCount, - BindlessTextureHandle: texHandle, - TextureLayer: 0, - Translucency: TranslucencyKind.Opaque); - return new CachedBatch(key, texHandle, Matrix4x4.Identity); - } -} diff --git a/tests/AcDream.Core.Tests/Rendering/Wb/EntitySpawnAdapterTests.cs b/tests/AcDream.Core.Tests/Rendering/Wb/EntitySpawnAdapterTests.cs deleted file mode 100644 index 016ce65..0000000 --- a/tests/AcDream.Core.Tests/Rendering/Wb/EntitySpawnAdapterTests.cs +++ /dev/null @@ -1,256 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Numerics; -using AcDream.App.Rendering.Wb; -using AcDream.Core.Physics; -using AcDream.Core.World; -using DatReaderWriter.DBObjs; -using Xunit; - -namespace AcDream.Core.Tests.Rendering.Wb; - -public sealed class EntitySpawnAdapterTests -{ - // ── Happy-path: server-spawned entity ───────────────────────────────── - - [Fact] - public void OnCreate_ServerSpawnedEntity_RegistersAnimatedEntityState() - { - var cache = new CapturingTextureCache(); - var adapter = MakeAdapter(cache); - var entity = MakeEntity(id: 1, serverGuid: 0xDEAD0001u); - - var state = adapter.OnCreate(entity); - - Assert.NotNull(state); - Assert.Same(state, adapter.GetState(0xDEAD0001u)); - } - - [Fact] - public void OnCreate_ServerSpawnedEntity_SequencerIsNotNull() - { - var adapter = MakeAdapter(); - var entity = MakeEntity(id: 1, serverGuid: 0xDEAD0002u); - - var state = adapter.OnCreate(entity); - - Assert.NotNull(state!.Sequencer); - } - - // ── Atlas-tier filter ───────────────────────────────────────────────── - - [Fact] - public void OnCreate_ProceduralEntity_ReturnsNullAndRegistersNothing() - { - var cache = new CapturingTextureCache(); - var adapter = MakeAdapter(cache); - // ServerGuid == 0 β†’ atlas-tier, must not be processed here. - var entity = MakeEntity(id: 2, serverGuid: 0u); - - var state = adapter.OnCreate(entity); - - Assert.Null(state); - Assert.Null(adapter.GetState(0u)); - // No texture decode should have been triggered. - Assert.Empty(cache.Calls); - } - - // ── Palette-override texture decode ─────────────────────────────────── - - [Fact] - public void OnCreate_WithPaletteOverrideAndSurfaceOverrides_TriggersTextureCacheDecode() - { - var cache = new CapturingTextureCache(); - var adapter = MakeAdapter(cache); - - var palette = new PaletteOverride( - BasePaletteId: 0x04001234u, - SubPalettes: new[] - { - new PaletteOverride.SubPaletteRange(0x04002000u, 0, 2), - }); - - // Entity carries two parts each with one surface override. - var entity = new WorldEntity - { - Id = 10, - ServerGuid = 0xBEEF0001u, - SourceGfxObjOrSetupId = 0x02000001u, - Position = Vector3.Zero, - Rotation = Quaternion.Identity, - PaletteOverride = palette, - MeshRefs = new[] - { - new MeshRef(0x01000010u, Matrix4x4.Identity) - { - SurfaceOverrides = new Dictionary - { - { 0x08000100u, 0u }, // surfaceId β†’ origTex (0 = none) - }, - }, - new MeshRef(0x01000020u, Matrix4x4.Identity) - { - SurfaceOverrides = new Dictionary - { - { 0x08000200u, 0x05000300u }, // with origTex override - }, - }, - }, - }; - - adapter.OnCreate(entity); - - // One call per surface-with-override: (0x08000100, null) and (0x08000200, 0x05000300). - Assert.Equal(2, cache.Calls.Count); - - Assert.Contains(cache.Calls, c => c.SurfaceId == 0x08000100u - && c.OrigTexOverride == null - && c.Palette == palette); - Assert.Contains(cache.Calls, c => c.SurfaceId == 0x08000200u - && c.OrigTexOverride == 0x05000300u - && c.Palette == palette); - } - - [Fact] - public void OnCreate_WithPaletteOverrideButNoSurfaceOverrides_DoesNotCallCache() - { - // Surfaces without SurfaceOverrides == null are decoded lazily at draw - // time; the adapter only pre-warms what it knows at spawn time. - var cache = new CapturingTextureCache(); - var adapter = MakeAdapter(cache); - - var entity = new WorldEntity - { - Id = 11, - ServerGuid = 0xBEEF0002u, - SourceGfxObjOrSetupId = 0x02000002u, - Position = Vector3.Zero, - Rotation = Quaternion.Identity, - PaletteOverride = new PaletteOverride(0x04001235u, Array.Empty()), - // MeshRef with NO SurfaceOverrides. - MeshRefs = new[] { new MeshRef(0x01000011u, Matrix4x4.Identity) }, - }; - - adapter.OnCreate(entity); - - Assert.Empty(cache.Calls); - } - - [Fact] - public void OnCreate_WithoutPaletteOverride_DoesNotCallCache() - { - var cache = new CapturingTextureCache(); - var adapter = MakeAdapter(cache); - var entity = MakeEntity(id: 12, serverGuid: 0xBEEF0003u); - - adapter.OnCreate(entity); - - Assert.Empty(cache.Calls); - } - - // ── OnRemove ───────────────────────────────────────────────────────── - - [Fact] - public void OnRemove_ReleasesPerEntityState() - { - var adapter = MakeAdapter(); - var entity = MakeEntity(id: 20, serverGuid: 0xCAFE0001u); - - adapter.OnCreate(entity); - Assert.NotNull(adapter.GetState(0xCAFE0001u)); - - adapter.OnRemove(0xCAFE0001u); - Assert.Null(adapter.GetState(0xCAFE0001u)); - } - - [Fact] - public void OnRemove_UnknownGuid_NoOps() - { - var adapter = MakeAdapter(); - - // Must not throw. - adapter.OnRemove(0xDEADBEEFu); - } - - // ── Multiple entities ───────────────────────────────────────────────── - - [Fact] - public void OnCreate_MultipleEntities_EachGetsOwnState() - { - var adapter = MakeAdapter(); - var e1 = MakeEntity(id: 30, serverGuid: 0x11110001u); - var e2 = MakeEntity(id: 31, serverGuid: 0x11110002u); - - var s1 = adapter.OnCreate(e1); - var s2 = adapter.OnCreate(e2); - - Assert.NotNull(s1); - Assert.NotNull(s2); - Assert.NotSame(s1, s2); - Assert.Same(s1, adapter.GetState(0x11110001u)); - Assert.Same(s2, adapter.GetState(0x11110002u)); - } - - [Fact] - public void OnRemove_OnlyReleasesTargetGuid() - { - var adapter = MakeAdapter(); - var e1 = MakeEntity(id: 40, serverGuid: 0x22220001u); - var e2 = MakeEntity(id: 41, serverGuid: 0x22220002u); - - adapter.OnCreate(e1); - adapter.OnCreate(e2); - adapter.OnRemove(0x22220001u); - - Assert.Null(adapter.GetState(0x22220001u)); - Assert.NotNull(adapter.GetState(0x22220002u)); - } - - // ── Helpers ─────────────────────────────────────────────────────────── - - private static EntitySpawnAdapter MakeAdapter(ITextureCachePerInstance? cache = null) - { - cache ??= new CapturingTextureCache(); - return new EntitySpawnAdapter(cache, _ => MakeSequencer()); - } - - private static WorldEntity MakeEntity(uint id, uint serverGuid) - => new WorldEntity - { - Id = id, - ServerGuid = serverGuid, - SourceGfxObjOrSetupId = 0x02000001u, - Position = Vector3.Zero, - Rotation = Quaternion.Identity, - MeshRefs = new[] { new MeshRef(0x01000001u, Matrix4x4.Identity) }, - }; - - private static AnimationSequencer MakeSequencer() - => new AnimationSequencer(new Setup(), new MotionTable(), new NullAnimationLoader()); - - // ── Mocks / stubs ───────────────────────────────────────────────────── - - /// - /// Capture every call to GetOrUploadWithPaletteOverride so tests can - /// assert without a live GL context. - /// - private sealed class CapturingTextureCache : ITextureCachePerInstance - { - public readonly record struct Call(uint SurfaceId, uint? OrigTexOverride, PaletteOverride Palette); - public List Calls { get; } = new(); - - public uint GetOrUploadWithPaletteOverride( - uint surfaceId, - uint? overrideOrigTextureId, - PaletteOverride paletteOverride) - { - Calls.Add(new Call(surfaceId, overrideOrigTextureId, paletteOverride)); - return 1u; // Fake GL handle. - } - } - - private sealed class NullAnimationLoader : IAnimationLoader - { - public Animation? LoadAnimation(uint id) => null; - } -} diff --git a/tests/AcDream.Core.Tests/Rendering/Wb/HiddenPartsTests.cs b/tests/AcDream.Core.Tests/Rendering/Wb/HiddenPartsTests.cs deleted file mode 100644 index 63c29f7..0000000 --- a/tests/AcDream.Core.Tests/Rendering/Wb/HiddenPartsTests.cs +++ /dev/null @@ -1,56 +0,0 @@ -using AcDream.App.Rendering.Wb; -using AcDream.Core.Physics; -using DatReaderWriter.DBObjs; -using Xunit; - -namespace AcDream.Core.Tests.Rendering.Wb; - -public sealed class HiddenPartsTests -{ - [Theory] - [InlineData(0b0000_0000ul, 0, false)] - [InlineData(0b0000_0001ul, 0, true)] - [InlineData(0b1000_0000ul, 7, true)] - [InlineData(0b1000_0000ul, 6, false)] - [InlineData(0xFFFF_FFFF_FFFF_FFFFul, 63, true)] - public void IsPartHidden_RespectsBitmaskBit(ulong mask, int partIdx, bool expected) - { - var state = MakeState(); - state.HideParts(mask); - Assert.Equal(expected, state.IsPartHidden(partIdx)); - } - - [Fact] - public void IsPartHidden_NegativeIdx_ReturnsFalse() - { - var state = MakeState(); - state.HideParts(0xFFFF_FFFF_FFFF_FFFFul); - Assert.False(state.IsPartHidden(-1)); - } - - [Fact] - public void IsPartHidden_PartIdxOver64_ReturnsFalse() - { - var state = MakeState(); - state.HideParts(0xFFFF_FFFF_FFFF_FFFFul); - Assert.False(state.IsPartHidden(64)); - } - - [Fact] - public void HideParts_DefaultsToNoneHidden() - { - var state = MakeState(); - for (int i = 0; i < 64; i++) - Assert.False(state.IsPartHidden(i)); - } - - private static AnimatedEntityState MakeState() => new(MakeSequencer()); - - private static AnimationSequencer MakeSequencer() - => new AnimationSequencer(new Setup(), new MotionTable(), new NullAnimationLoader()); - - private sealed class NullAnimationLoader : IAnimationLoader - { - public Animation? LoadAnimation(uint id) => null; - } -} diff --git a/tests/AcDream.Core.Tests/Rendering/Wb/LandblockSpawnAdapterTests.cs b/tests/AcDream.Core.Tests/Rendering/Wb/LandblockSpawnAdapterTests.cs deleted file mode 100644 index 85af235..0000000 --- a/tests/AcDream.Core.Tests/Rendering/Wb/LandblockSpawnAdapterTests.cs +++ /dev/null @@ -1,158 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Numerics; -using AcDream.App.Rendering.Wb; -using AcDream.Core.World; - -namespace AcDream.Core.Tests.Rendering.Wb; - -public sealed class LandblockSpawnAdapterTests -{ - [Fact] - public void OnLandblockLoaded_RegistersIncrementForEachUniqueAtlasGfxObj() - { - var captured = new CapturingAdapterMock(); - var adapter = new LandblockSpawnAdapter(captured); - - // Two procedural (ServerGuid=0) entities with different GfxObj ids. - var lb = MakeLandblock(landblockId: 0x12340000u, entities: new[] - { - MakeAtlasEntity(id: 1, gfxObjIds: new[] { 0x01000010u, 0x01000020u }), - MakeAtlasEntity(id: 2, gfxObjIds: new[] { 0x01000030u }), - }); - - adapter.OnLandblockLoaded(lb); - - // Three unique ids registered. - Assert.Equal(3, captured.IncrementCalls.Count); - Assert.Contains(0x01000010ul, captured.IncrementCalls); - Assert.Contains(0x01000020ul, captured.IncrementCalls); - Assert.Contains(0x01000030ul, captured.IncrementCalls); - } - - [Fact] - public void OnLandblockLoaded_DedupsSharedIdsAcrossEntities() - { - var captured = new CapturingAdapterMock(); - var adapter = new LandblockSpawnAdapter(captured); - - var lb = MakeLandblock(landblockId: 0x12340000u, entities: new[] - { - MakeAtlasEntity(id: 1, gfxObjIds: new[] { 0x01000010u, 0x01000020u }), - MakeAtlasEntity(id: 2, gfxObjIds: new[] { 0x01000010u, 0x01000020u }), - }); - - adapter.OnLandblockLoaded(lb); - - // Two unique ids despite two entities sharing both. - Assert.Equal(2, captured.IncrementCalls.Count); - } - - [Fact] - public void OnLandblockLoaded_SkipsServerSpawnedEntities() - { - var captured = new CapturingAdapterMock(); - var adapter = new LandblockSpawnAdapter(captured); - - var lb = MakeLandblock(landblockId: 0x12340000u, entities: new[] - { - MakeAtlasEntity(id: 1, gfxObjIds: new[] { 0x01000010u }), - // ServerGuid != 0 β†’ per-instance tier β†’ must NOT register. - MakePerInstanceEntity(id: 2, serverGuid: 0xCAFE0001u, gfxObjIds: new[] { 0x01000020u }), - }); - - adapter.OnLandblockLoaded(lb); - - // Only the atlas-tier entity's GfxObj is registered. - Assert.Single(captured.IncrementCalls); - Assert.Contains(0x01000010ul, captured.IncrementCalls); - Assert.DoesNotContain(0x01000020ul, captured.IncrementCalls); - } - - [Fact] - public void OnLandblockUnloaded_RegistersMatchingDecrements() - { - var captured = new CapturingAdapterMock(); - var adapter = new LandblockSpawnAdapter(captured); - - var lb = MakeLandblock(landblockId: 0x12340000u, entities: new[] - { - MakeAtlasEntity(id: 1, gfxObjIds: new[] { 0x01000010u, 0x01000020u }), - }); - - adapter.OnLandblockLoaded(lb); - adapter.OnLandblockUnloaded(0x12340000u); - - Assert.Equal(captured.IncrementCalls.OrderBy(x => x), captured.DecrementCalls.OrderBy(x => x)); - } - - [Fact] - public void OnLandblockUnloaded_UnknownLandblock_NoOps() - { - var captured = new CapturingAdapterMock(); - var adapter = new LandblockSpawnAdapter(captured); - - adapter.OnLandblockUnloaded(0xDEADBEEFu); - - Assert.Empty(captured.DecrementCalls); - } - - [Fact] - public void OnLandblockLoaded_SameLandblockTwice_DedupesAtTheLandblockLevel() - { - // If a landblock load fires twice (e.g. a streaming-controller bug), - // we should not double-register. Second load is treated as a no-op - // for ref-counting purposes. - var captured = new CapturingAdapterMock(); - var adapter = new LandblockSpawnAdapter(captured); - - var lb = MakeLandblock(landblockId: 0x12340000u, entities: new[] - { - MakeAtlasEntity(id: 1, gfxObjIds: new[] { 0x01000010u }), - }); - - adapter.OnLandblockLoaded(lb); - adapter.OnLandblockLoaded(lb); - - // One unique id, one increment β€” not two. - Assert.Single(captured.IncrementCalls); - } - - // ── Test helpers ────────────────────────────────────────────────────── - - private sealed class CapturingAdapterMock : IWbMeshAdapter - { - public List IncrementCalls { get; } = new(); - public List DecrementCalls { get; } = new(); - public void IncrementRefCount(ulong id) => IncrementCalls.Add(id); - public void DecrementRefCount(ulong id) => DecrementCalls.Add(id); - } - - private static LoadedLandblock MakeLandblock(uint landblockId, WorldEntity[] entities) - => new LoadedLandblock( - LandblockId: landblockId, - Heightmap: new DatReaderWriter.DBObjs.LandBlock(), // empty default - Entities: entities); - - private static WorldEntity MakeAtlasEntity(uint id, uint[] gfxObjIds) - => MakeEntity(id, serverGuid: 0u, gfxObjIds); - - private static WorldEntity MakePerInstanceEntity(uint id, uint serverGuid, uint[] gfxObjIds) - => MakeEntity(id, serverGuid, gfxObjIds); - - private static WorldEntity MakeEntity(uint id, uint serverGuid, uint[] gfxObjIds) - { - var meshRefs = gfxObjIds - .Select(g => new MeshRef(g, Matrix4x4.Identity)) - .ToList(); - return new WorldEntity - { - Id = id, - ServerGuid = serverGuid, - SourceGfxObjOrSetupId = gfxObjIds.FirstOrDefault(), - Position = Vector3.Zero, - Rotation = Quaternion.Identity, - MeshRefs = meshRefs, - }; - } -} diff --git a/tests/AcDream.Core.Tests/Rendering/Wb/MatrixCompositionTests.cs b/tests/AcDream.Core.Tests/Rendering/Wb/MatrixCompositionTests.cs deleted file mode 100644 index 7671574..0000000 --- a/tests/AcDream.Core.Tests/Rendering/Wb/MatrixCompositionTests.cs +++ /dev/null @@ -1,64 +0,0 @@ -using System.Numerics; -using AcDream.App.Rendering.Wb; - -namespace AcDream.Core.Tests.Rendering.Wb; - -public sealed class MatrixCompositionTests -{ - [Fact] - public void Compose_EntityAnimRest_ProducesExpectedWorldMatrix() - { - var entityWorld = Matrix4x4.CreateTranslation(100, 200, 300); - var animOverride = Matrix4x4.CreateRotationZ(MathF.PI / 4); - var restPose = Matrix4x4.CreateTranslation(1, 0, 0); - - var result = WbDrawDispatcher.ComposePartWorldMatrix(entityWorld, animOverride, restPose); - - var expected = restPose * animOverride * entityWorld; - AssertMatrixEqual(expected, result); - } - - [Fact] - public void Compose_IdentityAnim_EqualsRestTimesEntity() - { - var entityWorld = Matrix4x4.CreateFromQuaternion( - Quaternion.CreateFromYawPitchRoll(0.5f, 0, 0)) * - Matrix4x4.CreateTranslation(10, 20, 30); - var restPose = Matrix4x4.CreateTranslation(0.5f, -0.3f, 0.1f); - - var result = WbDrawDispatcher.ComposePartWorldMatrix( - entityWorld, Matrix4x4.Identity, restPose); - - var expected = restPose * entityWorld; - AssertMatrixEqual(expected, result); - } - - [Fact] - public void Compose_AllIdentity_ReturnsIdentity() - { - var result = WbDrawDispatcher.ComposePartWorldMatrix( - Matrix4x4.Identity, Matrix4x4.Identity, Matrix4x4.Identity); - - AssertMatrixEqual(Matrix4x4.Identity, result); - } - - private static void AssertMatrixEqual(Matrix4x4 expected, Matrix4x4 actual, float eps = 1e-5f) - { - Assert.Equal(expected.M11, actual.M11, eps); - Assert.Equal(expected.M12, actual.M12, eps); - Assert.Equal(expected.M13, actual.M13, eps); - Assert.Equal(expected.M14, actual.M14, eps); - Assert.Equal(expected.M21, actual.M21, eps); - Assert.Equal(expected.M22, actual.M22, eps); - Assert.Equal(expected.M23, actual.M23, eps); - Assert.Equal(expected.M24, actual.M24, eps); - Assert.Equal(expected.M31, actual.M31, eps); - Assert.Equal(expected.M32, actual.M32, eps); - Assert.Equal(expected.M33, actual.M33, eps); - Assert.Equal(expected.M34, actual.M34, eps); - Assert.Equal(expected.M41, actual.M41, eps); - Assert.Equal(expected.M42, actual.M42, eps); - Assert.Equal(expected.M43, actual.M43, eps); - Assert.Equal(expected.M44, actual.M44, eps); - } -} diff --git a/tests/AcDream.Core.Tests/Rendering/Wb/MeshExtractionConformanceTests.cs b/tests/AcDream.Core.Tests/Rendering/Wb/MeshExtractionConformanceTests.cs deleted file mode 100644 index 726f789..0000000 --- a/tests/AcDream.Core.Tests/Rendering/Wb/MeshExtractionConformanceTests.cs +++ /dev/null @@ -1,136 +0,0 @@ -using System.Numerics; -using AcDream.Core.Meshing; -using DatReaderWriter.DBObjs; -using DatReaderWriter.Enums; -using DatReaderWriter.Lib; -using DatReaderWriter.Types; - -namespace AcDream.Core.Tests.Rendering.Wb; - -/// -/// Conformance: our must produce the same -/// vertex-array + index-array output as WB's ObjectMeshManager -/// would for the same input GfxObj. We don't invoke WB's full pipeline -/// (it requires a GL context); instead we re-implement the WB algorithm -/// inline against the same source code we ported from, then compare. -/// -/// -/// If this test fails, either our port has drifted or the WB code has -/// changed upstream β€” investigate which, do not "fix" the test. -/// -/// -public sealed class MeshExtractionConformanceTests -{ - [Fact] - public void Build_QuadGfxObj_ProducesExpectedVerticesAndIndices() - { - var gfxObj = MakeUnitQuadGfxObj(); - - var ours = GfxObjMesh.Build(gfxObj, dats: null); - - Assert.Single(ours); - var sub = ours[0]; - // Quad β†’ 4 vertices, 6 indices (two triangles via fan triangulation). - Assert.Equal(4, sub.Vertices.Length); - Assert.Equal(6, sub.Indices.Length); - // Fan from vertex 0: (0,1,2) and (0,2,3). - Assert.Equal(new uint[] { 0, 1, 2, 0, 2, 3 }, sub.Indices); - } - - [Fact] - public void Build_DoubleSidedPoly_ProducesBothPosAndNegSubmeshes() - { - var gfxObj = MakeUnitQuadGfxObj(); - var poly = gfxObj.Polygons[0]; - poly.Stippling = StipplingType.Both; - // NegSurface=0 so the neg side references a valid surface entry. - poly.NegSurface = 0; - - var ours = GfxObjMesh.Build(gfxObj, dats: null); - - Assert.Equal(2, ours.Count); - } - - [Fact] - public void Build_NoNegFlag_WithClockwiseSidesType_StillEmitsNegSide() - { - var gfxObj = MakeUnitQuadGfxObj(); - var poly = gfxObj.Polygons[0]; - poly.Stippling = StipplingType.None; - poly.SidesType = CullMode.Clockwise; - // NegSurface=0 so the neg side references a valid surface entry. - poly.NegSurface = 0; - - var ours = GfxObjMesh.Build(gfxObj, dats: null); - - Assert.Equal(2, ours.Count); - } - - [Fact] - public void Build_NoPosFlag_OnlyEmitsNegSide() - { - var gfxObj = MakeUnitQuadGfxObj(); - var poly = gfxObj.Polygons[0]; - poly.Stippling = StipplingType.NoPos | StipplingType.Negative; - // NegSurface=0 so the neg side references a valid surface entry. - poly.NegSurface = 0; - - var ours = GfxObjMesh.Build(gfxObj, dats: null); - - Assert.Single(ours); - } - - /// - /// Build a synthetic 1Γ—1 quad GfxObj with vertex sequence [0,1,2,3] - /// at corners (0,0,0)/(1,0,0)/(1,1,0)/(0,1,0). PosSurface=0, - /// NegSurface=-1 (invalid β€” pos side only by default). - /// No Stippling flags set by default β€” caller may add them per test. - /// - private static GfxObj MakeUnitQuadGfxObj() - { - var gfx = new GfxObj { Surfaces = { 0x08000000u } }; - gfx.VertexArray = new VertexArray - { - VertexType = VertexType.CSWVertexType, - Vertices = - { - [0] = new SWVertex - { - Origin = new Vector3(0, 0, 0), - Normal = new Vector3(0, 0, 1), - UVs = { new Vec2Duv { U = 0, V = 0 } }, - }, - [1] = new SWVertex - { - Origin = new Vector3(1, 0, 0), - Normal = new Vector3(0, 0, 1), - UVs = { new Vec2Duv { U = 1, V = 0 } }, - }, - [2] = new SWVertex - { - Origin = new Vector3(1, 1, 0), - Normal = new Vector3(0, 0, 1), - UVs = { new Vec2Duv { U = 1, V = 1 } }, - }, - [3] = new SWVertex - { - Origin = new Vector3(0, 1, 0), - Normal = new Vector3(0, 0, 1), - UVs = { new Vec2Duv { U = 0, V = 1 } }, - }, - }, - }; - - var poly = new Polygon - { - VertexIds = { 0, 1, 2, 3 }, - PosUVIndices = { 0, 0, 0, 0 }, - PosSurface = 0, - NegSurface = -1, // invalid index β€” pos side only - Stippling = StipplingType.None, - SidesType = CullMode.None, - }; - gfx.Polygons[0] = poly; - return gfx; - } -} diff --git a/tests/AcDream.Core.Tests/Rendering/Wb/PendingSpawnIntegrationTests.cs b/tests/AcDream.Core.Tests/Rendering/Wb/PendingSpawnIntegrationTests.cs deleted file mode 100644 index c5d47f7..0000000 --- a/tests/AcDream.Core.Tests/Rendering/Wb/PendingSpawnIntegrationTests.cs +++ /dev/null @@ -1,142 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Numerics; -using AcDream.App.Rendering.Wb; -using AcDream.App.Streaming; -using AcDream.Core.World; - -namespace AcDream.Core.Tests.Rendering.Wb; - -/// -/// Integration: verifies the pending-spawn list mechanism keeps working -/// after Task 12 wired LandblockSpawnAdapter into GpuWorldState. Server- -/// spawned entities (ServerGuid != 0) park in pending β†’ drain on -/// AddLandblock β†’ end up in the flat view, but they are NEVER registered -/// with the WB adapter (they're per-instance tier). -/// -/// The adapter SHOULD see atlas-tier entities (ServerGuid == 0) that -/// arrived in the AddLandblock's payload directly. -/// -public sealed class PendingSpawnIntegrationTests -{ - // N.5 ship amendment: WbFoundationFlag was deleted β€” GpuWorldState - // no longer gates adapter calls on the flag; they are unconditional - // when the adapter is non-null. No static ctor hook needed. - - [Fact] - public void LiveEntity_ParkedBeforeLandblock_DrainsButIsNotRegisteredWithAdapter() - { - var captured = new CapturingAdapterMock(); - var spawnAdapter = new LandblockSpawnAdapter(captured); - var state = new GpuWorldState(spawnAdapter); - - // Park a live (server-spawned) entity for landblock 0x1234FFFF BEFORE - // the landblock streams in. ServerGuid != 0 makes this per-instance-tier. - var liveEntity = MakeServerSpawned( - id: 1, serverGuid: 0xCAFE0001u, gfxObjId: 0x01000099u); - // AppendLiveEntity takes the raw cell-form id; it canonicalises internally. - state.AppendLiveEntity(0x12340011u, liveEntity); - - Assert.Equal(1, state.PendingLiveEntityCount); - Assert.Empty(captured.IncrementCalls); // not registered yet β€” landblock not loaded - - // Now landblock arrives with ONE atlas-tier entity that brings its own - // GfxObj, plus the pending live entity drains into it. - var atlasEntity = MakeAtlas(id: 2, gfxObjId: 0x01000010u); - var lb = new LoadedLandblock( - LandblockId: 0x1234FFFFu, - Heightmap: new DatReaderWriter.DBObjs.LandBlock(), - Entities: new[] { atlasEntity }); - state.AddLandblock(lb); - - // Pending drained. - Assert.Equal(0, state.PendingLiveEntityCount); - - // Flat view contains both: the atlas one from the load + the drained pending. - var allIds = state.Entities.Select(e => e.Id).ToHashSet(); - Assert.Contains(1u, allIds); // pending entity - Assert.Contains(2u, allIds); // landblock entity - - // Adapter only saw the atlas-tier GfxObj. The pending server-spawned - // entity's GfxObj is NOT registered (filtered by ServerGuid != 0 in - // LandblockSpawnAdapter). - Assert.Single(captured.IncrementCalls); - Assert.Contains(0x01000010ul, captured.IncrementCalls); - Assert.DoesNotContain(0x01000099ul, captured.IncrementCalls); - } - - [Fact] - public void LiveEntity_AfterLandblock_RegistersImmediatelyWithoutAdapterCall() - { - // When a CreateObject arrives for an already-loaded landblock, it goes - // straight into the flat view (not through pending). Adapter is NOT - // re-invoked because the landblock load already happened. - var captured = new CapturingAdapterMock(); - var spawnAdapter = new LandblockSpawnAdapter(captured); - var state = new GpuWorldState(spawnAdapter); - - var atlasEntity = MakeAtlas(id: 1, gfxObjId: 0x01000010u); - var lb = new LoadedLandblock( - LandblockId: 0x1234FFFFu, - Heightmap: new DatReaderWriter.DBObjs.LandBlock(), - Entities: new[] { atlasEntity }); - state.AddLandblock(lb); - - Assert.Single(captured.IncrementCalls); // atlas registered - - // Now a live entity arrives β€” landblock is already loaded. - var liveEntity = MakeServerSpawned(id: 2, serverGuid: 0xCAFE0001u, gfxObjId: 0x01000099u); - state.AppendLiveEntity(0x12340022u, liveEntity); - - // Adapter not invoked again β€” AppendLiveEntity doesn't drive ref counts. - Assert.Single(captured.IncrementCalls); - Assert.Equal(0, state.PendingLiveEntityCount); - } - - [Fact] - public void LandblockUnload_ReleasesAtlasIds_PendingDoesNotRegress() - { - var captured = new CapturingAdapterMock(); - var spawnAdapter = new LandblockSpawnAdapter(captured); - var state = new GpuWorldState(spawnAdapter); - - var atlasEntity = MakeAtlas(id: 1, gfxObjId: 0x01000010u); - var lb = new LoadedLandblock( - LandblockId: 0x1234FFFFu, - Heightmap: new DatReaderWriter.DBObjs.LandBlock(), - Entities: new[] { atlasEntity }); - state.AddLandblock(lb); - state.RemoveLandblock(0x1234FFFFu); - - Assert.Equal( - captured.IncrementCalls.OrderBy(x => x), - captured.DecrementCalls.OrderBy(x => x)); - } - - // ── Test helpers ────────────────────────────────────────────────────── - - private sealed class CapturingAdapterMock : IWbMeshAdapter - { - public List IncrementCalls { get; } = new(); - public List DecrementCalls { get; } = new(); - public void IncrementRefCount(ulong id) => IncrementCalls.Add(id); - public void DecrementRefCount(ulong id) => DecrementCalls.Add(id); - } - - private static WorldEntity MakeAtlas(uint id, uint gfxObjId) - => MakeEntity(id, serverGuid: 0u, gfxObjId); - - private static WorldEntity MakeServerSpawned(uint id, uint serverGuid, uint gfxObjId) - => MakeEntity(id, serverGuid, gfxObjId); - - private static WorldEntity MakeEntity(uint id, uint serverGuid, uint gfxObjId) - => new WorldEntity - { - Id = id, - ServerGuid = serverGuid, - SourceGfxObjOrSetupId = gfxObjId, - Position = Vector3.Zero, - Rotation = Quaternion.Identity, - MeshRefs = new[] { new MeshRef(gfxObjId, Matrix4x4.Identity) }, - }; -} diff --git a/tests/AcDream.Core.Tests/Rendering/Wb/SetupFlattenConformanceTests.cs b/tests/AcDream.Core.Tests/Rendering/Wb/SetupFlattenConformanceTests.cs deleted file mode 100644 index 07bc8b1..0000000 --- a/tests/AcDream.Core.Tests/Rendering/Wb/SetupFlattenConformanceTests.cs +++ /dev/null @@ -1,105 +0,0 @@ -using System.Numerics; -using AcDream.Core.Meshing; -using DatReaderWriter.DBObjs; -using DatReaderWriter.Enums; -using DatReaderWriter.Types; - -namespace AcDream.Core.Tests.Rendering.Wb; - -/// -/// Conformance: our must produce the same -/// (GfxObjId, Matrix4x4) sequence as WB's setup-parts walk for representative -/// Setups. Pinning the placement-frame fallback chain (motionFrameOverride β†’ -/// Resting β†’ Default β†’ first available) before substitution. -/// -public sealed class SetupFlattenConformanceTests -{ - [Fact] - public void Flatten_NoFrames_FallsBackToIdentity() - { - var setup = new Setup { Parts = { 0x01000001u } }; - // PlacementFrames deliberately empty β€” no DefaultScale entry either, - // so scale defaults to Vector3.One and the fallback frame is - // (Origin=Zero, Orientation=Identity) β†’ Identity matrix. - - var refs = SetupMesh.Flatten(setup); - - Assert.Single(refs); - Assert.Equal(0x01000001u, refs[0].GfxObjId); - Assert.Equal(Matrix4x4.Identity, refs[0].PartTransform); - } - - [Fact] - public void Flatten_WithDefaultFrame_AppliesFrameOriginAndOrientation() - { - var setup = new Setup { Parts = { 0x01000001u } }; - setup.PlacementFrames[Placement.Default] = new AnimationFrame(1) - { - Frames = - { - new Frame - { - Origin = new Vector3(10, 20, 30), - Orientation = Quaternion.Identity, - }, - }, - }; - - var refs = SetupMesh.Flatten(setup); - - Assert.Equal(new Vector3(10, 20, 30), refs[0].PartTransform.Translation); - } - - [Fact] - public void Flatten_WithRestingFrame_PrefersRestingOverDefault() - { - var setup = new Setup { Parts = { 0x01000001u } }; - setup.PlacementFrames[Placement.Default] = new AnimationFrame(1) - { - Frames = { new Frame { Origin = new Vector3(10, 20, 30), Orientation = Quaternion.Identity } }, - }; - setup.PlacementFrames[Placement.Resting] = new AnimationFrame(1) - { - Frames = { new Frame { Origin = new Vector3(99, 99, 99), Orientation = Quaternion.Identity } }, - }; - - var refs = SetupMesh.Flatten(setup); - - Assert.Equal(new Vector3(99, 99, 99), refs[0].PartTransform.Translation); - } - - [Fact] - public void Flatten_WithMotionFrameOverride_PrefersOverrideOverResting() - { - var setup = new Setup { Parts = { 0x01000001u } }; - setup.PlacementFrames[Placement.Resting] = new AnimationFrame(1) - { - Frames = { new Frame { Origin = new Vector3(99, 99, 99), Orientation = Quaternion.Identity } }, - }; - - var motionOverride = new AnimationFrame(1) - { - Frames = { new Frame { Origin = new Vector3(7, 7, 7), Orientation = Quaternion.Identity } }, - }; - - var refs = SetupMesh.Flatten(setup, motionFrameOverride: motionOverride); - - Assert.Equal(new Vector3(7, 7, 7), refs[0].PartTransform.Translation); - } - - [Fact] - public void Flatten_DefaultScalePerPart_AppliedToTransform() - { - var setup = new Setup - { - Parts = { 0x01000001u, 0x01000002u }, - DefaultScale = { new Vector3(2, 2, 2), new Vector3(3, 3, 3) }, - }; - // No placement frames β€” fallback frame is identity pose; scale still applies. - - var refs = SetupMesh.Flatten(setup); - - Assert.Equal(2f, refs[0].PartTransform.M11); - Assert.Equal(3f, refs[1].PartTransform.M11); - } -} diff --git a/tests/AcDream.Core.Tests/Rendering/Wb/WbDispatcherDepthMaskTests.cs b/tests/AcDream.Core.Tests/Rendering/Wb/WbDispatcherDepthMaskTests.cs deleted file mode 100644 index 216a736..0000000 --- a/tests/AcDream.Core.Tests/Rendering/Wb/WbDispatcherDepthMaskTests.cs +++ /dev/null @@ -1,39 +0,0 @@ -using AcDream.App.Rendering.Wb; -using AcDream.Core.Meshing; -using Xunit; - -namespace AcDream.Core.Tests.Rendering.Wb; - -/// -/// A.5 T21: lock in the depth-write attribution per translucency kind. -/// -/// WbDrawDispatcher.Draw uses a two-pass structure: -/// -/// Opaque pass β€” DepthMask(true): writes depth so that -/// later transparent geometry sorts correctly against solid surfaces. -/// Transparent pass β€” DepthMask(false): reads depth but -/// does NOT write it, so alpha-blended surfaces don't occlude each -/// other by Z-fighting. -/// -/// The partition that decides which pass a batch enters is -/// : -/// Opaque and ClipMap go to the opaque pass (depth write); -/// AlphaBlend, Additive, InvAlpha go to the -/// transparent pass (no depth write). -/// -/// -public sealed class WbDispatcherDepthMaskTests -{ - [Theory] - [InlineData(TranslucencyKind.Opaque, true)] // opaque pass β€” depth write - [InlineData(TranslucencyKind.ClipMap, true)] // foliage β€” depth write (binary alpha / A2C) - [InlineData(TranslucencyKind.AlphaBlend, false)] // transparent β€” no depth write - [InlineData(TranslucencyKind.Additive, false)] - [InlineData(TranslucencyKind.InvAlpha, false)] - public void IsOpaquePartition_ImpliesDepthWriteAttribution( - TranslucencyKind kind, bool expectsDepthWrite) - { - bool isOpaque = WbDrawDispatcher.IsOpaquePublic(kind); - Assert.Equal(expectsDepthWrite, isOpaque); - } -} diff --git a/tests/AcDream.Core.Tests/Rendering/Wb/WbDrawDispatcherBucketingTests.cs b/tests/AcDream.Core.Tests/Rendering/Wb/WbDrawDispatcherBucketingTests.cs deleted file mode 100644 index 4f17cd6..0000000 --- a/tests/AcDream.Core.Tests/Rendering/Wb/WbDrawDispatcherBucketingTests.cs +++ /dev/null @@ -1,744 +0,0 @@ -using System.Collections.Generic; -using System.Numerics; -using AcDream.App.Rendering; -using AcDream.App.Rendering.Wb; -using AcDream.Core.Meshing; -using AcDream.Core.World; -using Xunit; - -namespace AcDream.Core.Tests.Rendering.Wb; - -/// -/// Tests for β€” the pure-CPU -/// visibility filter extracted in A.5 T17. These tests exercise the two -/// key perf changes from Phase A.5 spec Β§4.6: -/// -/// -/// Change #1 (T17): invisible LB + animated set β†’ iterate -/// animatedEntityIds directly, not the full entity list. -/// Change #2 (T18): per-entity AABB cull reads the cached AABB -/// (/AabbMax) rather than -/// recomputing PositionΒ±5 per frame. -/// -/// -public sealed class WbDrawDispatcherBucketingTests -{ - // ── helpers ────────────────────────────────────────────────────────────── - - private static WorldEntity MakeEntity(uint id, Vector3 position) - => new WorldEntity - { - Id = id, - SourceGfxObjOrSetupId = 0, - Position = position, - Rotation = Quaternion.Identity, - MeshRefs = System.Array.Empty(), - }; - - private static WorldEntity MakeEntityWithMesh(uint id, Vector3 position) - => new WorldEntity - { - Id = id, - SourceGfxObjOrSetupId = 0, - Position = position, - Rotation = Quaternion.Identity, - // Single dummy MeshRef so it passes the MeshRefs.Count == 0 guard. - MeshRefs = new[] { new MeshRef { GfxObjId = 0x01000001u } }, - }; - - private static Dictionary BuildById(IEnumerable entities) - { - var d = new Dictionary(); - foreach (var e in entities) d[e.Id] = e; - return d; - } - - /// - /// A frustum positioned at (1e6+1, 1e6+1, 1e6+1) looking toward (1e6, 1e6, 1e6) - /// with a very narrow near/far. Any AABB near the origin (0..20000) is - /// far behind the near plane and fails all six planes. - /// - private static FrustumPlanes MakeFarAwayFrustum() - { - var view = Matrix4x4.CreateLookAt( - new Vector3(1e6f + 1f, 1e6f + 1f, 1e6f + 1f), - new Vector3(1e6f, 1e6f, 1e6f), - Vector3.UnitZ); - var proj = Matrix4x4.CreatePerspectiveFieldOfView( - MathF.PI / 4f, 1f, 0.1f, 1f); - return FrustumPlanes.FromViewProjection(view * proj); - } - - // ── T17 Change #1 tests ─────────────────────────────────────────────── - - [Fact] - public void WalkEntities_InvisibleLb_NoAnimated_SkipsEntireBlock() - { - // When LB is invisible AND animatedEntityIds is empty/null, - // WalkEntities should not walk any entities at all. - var entities = new List(); - for (int i = 0; i < 500; i++) - entities.Add(MakeEntityWithMesh((uint)i, new Vector3(i, 0, 0))); - - var byId = BuildById(entities); - var entries = new[] - { - new WbDrawDispatcher.LandblockEntry( - 0xAAAA_FFFFu, - new Vector3(10000, 10000, 10000), - new Vector3(20000, 20000, 20000), - entities, - byId), - }; - - var result = WbDrawDispatcher.WalkEntities( - entries, - frustum: MakeFarAwayFrustum(), - neverCullLandblockId: null, - visibleCellIds: null, - animatedEntityIds: null); - - Assert.Equal(0, result.EntitiesWalked); - Assert.Empty(result.ToDraw); - } - - [Fact] - public void WalkEntities_InvisibleLb_AnimatedSet_WalksOnlyAnimatedEntities() - { - // 1000 entities in an LB whose AABB is far outside the frustum. - // Only entity Id=42 is in animatedEntityIds. - // Pre-T17 behavior: walk all 1000 entities just to find #42. - // Post-T17: walk only the 1 animated entity (EntitiesWalked == 1). - const int Total = 1000; - var entities = new List(Total); - for (int i = 0; i < Total; i++) - entities.Add(MakeEntityWithMesh((uint)i, new Vector3(i, 0, 0))); - - var byId = BuildById(entities); - var animatedSet = new HashSet { 42 }; - - var entries = new[] - { - new WbDrawDispatcher.LandblockEntry( - 0xAAAA_FFFFu, - new Vector3(10000, 10000, 10000), - new Vector3(20000, 20000, 20000), - entities, - byId), - }; - - var result = WbDrawDispatcher.WalkEntities( - entries, - frustum: MakeFarAwayFrustum(), - neverCullLandblockId: null, - visibleCellIds: null, - animatedEntityIds: animatedSet); - - // Only the 1 animated entity should be walked β€” not 1000. - Assert.Equal(1, result.EntitiesWalked); - Assert.Single(result.ToDraw); - Assert.Equal(42u, result.ToDraw[0].Entity.Id); - } - - [Fact] - public void WalkEntities_InvisibleLb_AnimatedIdAbsent_ZeroWalked() - { - // Animated entity ids 200 and 300 are NOT in this LB (which only - // has ids 0..99). Should produce zero walks. - var entities = new List(); - for (int i = 0; i < 100; i++) - entities.Add(MakeEntityWithMesh((uint)i, Vector3.Zero)); - - var byId = BuildById(entities); - var animatedSet = new HashSet { 200, 300 }; // not in this LB - - var entries = new[] - { - new WbDrawDispatcher.LandblockEntry( - 0xBBBB_FFFFu, - new Vector3(10000, 10000, 10000), - new Vector3(20000, 20000, 20000), - entities, - byId), - }; - - var result = WbDrawDispatcher.WalkEntities( - entries, - frustum: MakeFarAwayFrustum(), - neverCullLandblockId: null, - visibleCellIds: null, - animatedEntityIds: animatedSet); - - Assert.Equal(0, result.EntitiesWalked); - Assert.Empty(result.ToDraw); - } - - [Fact] - public void WalkEntities_NeverCullLb_WalksAllEntitiesRegardlessOfFrustum() - { - // neverCullLandblockId bypasses the LB AABB check entirely. - // All entities with at least one MeshRef should be walked. - var entities = new List - { - MakeEntityWithMesh(1, Vector3.Zero), - MakeEntityWithMesh(2, Vector3.Zero), - MakeEntityWithMesh(3, Vector3.Zero), - }; - - var byId = BuildById(entities); - const uint lbId = 0xCCCC_FFFFu; - - var entries = new[] - { - new WbDrawDispatcher.LandblockEntry( - lbId, - new Vector3(10000, 10000, 10000), // AABB would fail frustum - new Vector3(20000, 20000, 20000), - entities, - byId), - }; - - var result = WbDrawDispatcher.WalkEntities( - entries, - frustum: MakeFarAwayFrustum(), - neverCullLandblockId: lbId, // exempt from LB cull - visibleCellIds: null, - animatedEntityIds: null); - - Assert.Equal(3, result.EntitiesWalked); - } - - [Fact] - public void WalkEntities_NullFrustum_WalksEntitiesWithMeshRefs() - { - // Null frustum means no culling β€” all entities with MeshRefs pass. - // Entities without MeshRefs are still filtered out. - var entities = new List - { - MakeEntityWithMesh(1, Vector3.Zero), - MakeEntity(2, Vector3.Zero), // no MeshRefs β€” must be skipped - MakeEntityWithMesh(3, Vector3.Zero), - }; - - var byId = BuildById(entities); - var entries = new[] - { - new WbDrawDispatcher.LandblockEntry( - 0xDDDD_FFFFu, Vector3.Zero, Vector3.Zero, - entities, byId), - }; - - var result = WbDrawDispatcher.WalkEntities( - entries, - frustum: null, - neverCullLandblockId: null, - visibleCellIds: null, - animatedEntityIds: null); - - Assert.Equal(2, result.EntitiesWalked); - Assert.Equal(2, result.ToDraw.Count); - } - - // ── T18 Change #2 tests ─────────────────────────────────────────────── - - [Fact] - public void WalkEntities_VisibleLb_EntityFarAway_CulledViaCachedAabb() - { - // LB passes the LB-level cull; entity AABB is far from the frustum. - // After RefreshAabb the entity should be culled by the per-entity check. - var entity = MakeEntityWithMesh(1, new Vector3(50000, 50000, 50000)); - entity.RefreshAabb(); // populate cached AABB at (50000Β±5) - - var byId = BuildById(new[] { entity }); - var entries = new[] - { - // LB AABB near origin so it passes the LB cull; entity is far away. - new WbDrawDispatcher.LandblockEntry( - 0xEEEE_FFFFu, - new Vector3(-10, -10, -10), - new Vector3(10, 10, 10), - new List { entity }, - byId), - }; - - // Frustum centered at origin, range Β±100. - var view = Matrix4x4.CreateLookAt(new Vector3(-50, 0, 0), Vector3.Zero, Vector3.UnitZ); - var proj = Matrix4x4.CreatePerspectiveFieldOfView(MathF.PI / 2f, 1f, 0.5f, 100f); - var tightFrustum = FrustumPlanes.FromViewProjection(view * proj); - - var result = WbDrawDispatcher.WalkEntities( - entries, - frustum: tightFrustum, - neverCullLandblockId: null, - visibleCellIds: null, - animatedEntityIds: null); - - // Entity at (50000,50000,50000) is outside the frustum β€” should be culled. - Assert.Equal(0, result.EntitiesWalked); - } - - [Fact] - public void WalkEntities_AnimatedEntity_BypassesPerEntityAabbCull() - { - // Animated entities must always pass even if their AABB would be culled. - var entity = MakeEntityWithMesh(7, new Vector3(50000, 50000, 50000)); - entity.RefreshAabb(); - - var byId = BuildById(new[] { entity }); - var entries = new[] - { - new WbDrawDispatcher.LandblockEntry( - 0xEEEF_FFFFu, - new Vector3(-10, -10, -10), - new Vector3(10, 10, 10), - new List { entity }, - byId), - }; - - var view = Matrix4x4.CreateLookAt(new Vector3(-50, 0, 0), Vector3.Zero, Vector3.UnitZ); - var proj = Matrix4x4.CreatePerspectiveFieldOfView(MathF.PI / 2f, 1f, 0.5f, 100f); - var tightFrustum = FrustumPlanes.FromViewProjection(view * proj); - - var animatedSet = new HashSet { 7 }; - - var result = WbDrawDispatcher.WalkEntities( - entries, - frustum: tightFrustum, - neverCullLandblockId: null, - visibleCellIds: null, - animatedEntityIds: animatedSet); - - // Animated entity bypasses per-entity cull. - Assert.Equal(1, result.EntitiesWalked); - Assert.Single(result.ToDraw); - Assert.Equal(7u, result.ToDraw[0].Entity.Id); - } - - [Fact] - public void WalkEntities_AabbDirty_RefreshedLazilyBeforeCull() - { - // An entity with AabbDirty=true (initial state) should get its AABB - // refreshed lazily by WalkEntities before the cull check. - var entity = MakeEntityWithMesh(5, new Vector3(0, 0, 0)); - // AabbDirty starts true by default β€” do NOT call RefreshAabb manually. - Assert.True(entity.AabbDirty); - - var byId = BuildById(new[] { entity }); - var entries = new[] - { - new WbDrawDispatcher.LandblockEntry( - 0xF0F0_FFFFu, - new Vector3(-10, -10, -10), - new Vector3(10, 10, 10), - new List { entity }, - byId), - }; - - // A frustum that accepts things near origin. - var view = Matrix4x4.CreateLookAt(new Vector3(-50, 0, 0), Vector3.Zero, Vector3.UnitZ); - var proj = Matrix4x4.CreatePerspectiveFieldOfView(MathF.PI / 2f, 1f, 0.1f, 200f); - var nearOriginFrustum = FrustumPlanes.FromViewProjection(view * proj); - - var result = WbDrawDispatcher.WalkEntities( - entries, - frustum: nearOriginFrustum, - neverCullLandblockId: null, - visibleCellIds: null, - animatedEntityIds: null); - - // Entity at origin is inside the frustum after lazy RefreshAabb. - Assert.Equal(1, result.EntitiesWalked); - // AabbDirty should have been cleared by the lazy refresh. - Assert.False(entity.AabbDirty); - } - - // ── Tier 1 cache (#53) dispatcher integration tests ────────────────────── - // - // Tasks 9 & 10 wire the EntityClassificationCache into Draw's per-entity - // loop. These tests exercise the populate + cache-hit fast-path algorithm - // through the static helpers Draw uses (MaybeFlushOnEntityChange, - // FinalFlushPopulate, ApplyCacheHit). The helpers were extracted from - // Draw's foreach for testability β€” Draw calls them; tests drive them - // directly with deterministic synthesized inputs. This is the same - // pattern WalkEntities follows (extracted from Draw, tested in isolation). - // - // The tests cover spec Β§7.2 #11 (static populate + reuse) and #12 - // (animated bypass), plus a multi-MeshRef regression test that would - // have caught the bug fixed in commit 00fa8ae (per-MeshRef Populate - // overwrites earlier batches because the cache is keyed by entity.Id). - - /// - /// Helper: constructs a CachedBatch with stable group-key inputs so the - /// hit-path test can verify membership. Mirrors the shape ClassifyBatches - /// produces under the collector pattern. - /// - private static CachedBatch MakeCachedBatch( - uint ibo, uint firstIndex, int indexCount, ulong texHandle, Matrix4x4? restPose = null) - { - var key = new GroupKey( - Ibo: ibo, - FirstIndex: firstIndex, - BaseVertex: 0, - IndexCount: indexCount, - BindlessTextureHandle: texHandle, - TextureLayer: 0, - Translucency: TranslucencyKind.Opaque); - return new CachedBatch(key, texHandle, restPose ?? Matrix4x4.Identity); - } - - [Fact] - public void Draw_StaticEntity_PopulatesCacheOnFirstFrameAndHitsOnSecond() - { - // Spec Β§7.2 test #11. - // Drives Draw's populate + cache-hit algorithm through the production - // static helpers. Verifies that: - // 1. First "frame": cache is empty β†’ populate fires once at the - // end-of-loop final flush (entity.Id=100 has 2 batches). - // 2. Second "frame": cache.TryGet(100) hits β†’ ApplyCacheHit appends - // cached batches to a fresh _groups dict without re-populating. - // cache.Count stays at 1 (Populate is idempotent via overwrite, - // but the hit-path doesn't re-populate at all). - var cache = new EntityClassificationCache(); - var scratch = new List(); - - Assert.Equal(0, cache.Count); - - // Frame 1: simulate one foreach iteration producing 2 batches for - // entity 100 in landblock 0xA9B40000. With no prior tracker, the - // entity-change flush is a no-op. ClassifyBatches' collector adds - // to scratch. The end-of-loop FinalFlushPopulate commits. - const uint EntityId = 100; - const uint LandblockId = 0xA9B40000u; - - // First MeshRef contributes 2 batches (mimics ClassifyBatches output). - scratch.Add(MakeCachedBatch(ibo: 1, firstIndex: 0, indexCount: 6, texHandle: 0xAA)); - scratch.Add(MakeCachedBatch(ibo: 1, firstIndex: 6, indexCount: 6, texHandle: 0xBB)); - - uint? populateEntityId = null; - uint populateLandblockId = 0u; - // First-tuple boundary check: no flush, sets the tracker. - (populateEntityId, populateLandblockId) = WbDrawDispatcher.MaybeFlushOnEntityChange( - populateEntityId, populateLandblockId, EntityId, cache, scratch); - // After ClassifyBatches the loop sets the tracker (matching Draw). - populateEntityId = EntityId; - populateLandblockId = LandblockId; - - // End-of-loop final flush β€” this is where the cache populates. - WbDrawDispatcher.FinalFlushPopulate(populateEntityId, populateLandblockId, cache, scratch); - - // First-frame post-conditions: 1 cache entry, 2 batches in it. - Assert.Equal(1, cache.Count); - Assert.True(cache.TryGet(EntityId, LandblockId, out var entry)); - Assert.NotNull(entry); - Assert.Equal(2, entry!.Batches.Length); - Assert.Equal(0xAAul, entry.Batches[0].BindlessTextureHandle); - Assert.Equal(0xBBul, entry.Batches[1].BindlessTextureHandle); - - // Frame 2: cache hit. ApplyCacheHit walks the cached batches and - // appends RestPose * entityWorld to a per-frame group dict. - // Production code: this is the !isAnimated && _cache.TryGet branch - // at the top of the per-entity loop body in Draw. - var groups = new Dictionary>(); - void AppendInstance(GroupKey k, Matrix4x4 m) - { - if (!groups.TryGetValue(k, out var list)) - { - list = new List(); - groups[k] = list; - } - list.Add(m); - } - - Assert.True(cache.TryGet(EntityId, LandblockId, out var entryHit)); - Assert.NotNull(entryHit); - var entityWorld = Matrix4x4.CreateTranslation(new Vector3(10f, 20f, 30f)); - WbDrawDispatcher.ApplyCacheHit(entryHit!, entityWorld, AppendInstance); - - // Cache state stable β€” Populate didn't fire on the hit path. - Assert.Equal(1, cache.Count); - - // Both groups received exactly one matrix each (the entity is one - // instance contributing once per cached batch). - Assert.Equal(2, groups.Count); - foreach (var (_, list) in groups) - Assert.Single(list); - - // Matrix composition is RestPose * entityWorld (NOT the reverse). - // RestPose is Matrix4x4.Identity for the synthesized batches, so the - // appended matrix must equal entityWorld. - foreach (var (_, list) in groups) - Assert.Equal(entityWorld, list[0]); - } - - [Fact] - public void Draw_AnimatedEntity_DoesNotPopulateCache() - { - // Spec Β§7.2 test #12. - // Animated entities take the slow path with collector=null: their - // ClassifyBatches output is NOT routed into _populateScratch and the - // populate-tracking locals stay null. Result: the cache is never - // populated for animated entities, and FinalFlushPopulate is a no-op. - // - // This test models that flow: scratch stays empty, populateEntityId - // stays null, FinalFlushPopulate fires but commits nothing. - var cache = new EntityClassificationCache(); - var scratch = new List(); - - const uint AnimatedId = 7; - const uint LandblockId = 0xA9B40000u; - var animatedSet = new HashSet { AnimatedId }; - - // Even when the entity has MeshRefs that would produce batches, the - // animated-set membership means collector=null in Draw β€” scratch - // stays empty and the tracker stays null. Simulating that here: - // we do NOT add to scratch and we do NOT set populateEntityId. - bool isAnimated = animatedSet.Contains(AnimatedId); - Assert.True(isAnimated); - - uint? populateEntityId = null; - uint populateLandblockId = 0u; - // Boundary check still runs but is a no-op β€” tracker is null. - (populateEntityId, populateLandblockId) = WbDrawDispatcher.MaybeFlushOnEntityChange( - populateEntityId, populateLandblockId, AnimatedId, cache, scratch); - - // For animated entities, Draw does NOT set populateEntityId after - // ClassifyBatches (the `if (collector is not null)` guard). - // populateEntityId stays null. - - // End-of-loop flush β€” no-op for animated-only iterations. - WbDrawDispatcher.FinalFlushPopulate(populateEntityId, populateLandblockId, cache, scratch); - - // Cache should never be populated for animated entities. - Assert.Equal(0, cache.Count); - Assert.False(cache.TryGet(AnimatedId, LandblockId, out _)); - } - - [Fact] - public void Draw_MultiMeshRefStaticEntity_PopulatesAllBatchesIntoSingleCacheEntry() - { - // Regression test for the bug fixed at commit 00fa8ae: - // - // Task 9's first attempt called _cache.Populate per-(entity, - // MeshRefIndex) tuple, but the cache is keyed by entity.Id. For - // multi-MeshRef entities (multi-part Setup buildings, statues, - // NPCs), each iteration's Populate OVERWROTE the previous one - // β€” only the LAST MeshRef's batches survived in the cache. After - // the fix, Populate fires once per entity at the entity boundary - // (or end-of-loop), with all MeshRefs' batches accumulated into - // _populateScratch. - // - // This test simulates a 3-MeshRef static entity where each MeshRef - // contributes 2 batches (total = 6). It walks through Draw's loop - // structure tuple-by-tuple, calling MaybeFlushOnEntityChange before - // each tuple's classification and FinalFlushPopulate at end-of-loop. - // Asserts the cache entry holds ALL 6 batches, not just the last 2. - // - // If the per-MeshRef Populate bug were reintroduced, this test would - // see Batches.Length == 2 (last MeshRef only). - var cache = new EntityClassificationCache(); - var scratch = new List(); - - const uint EntityId = 200; - const uint LandblockId = 0xA9B40000u; - const int MeshRefCount = 3; - const int BatchesPerMeshRef = 2; - const int ExpectedTotalBatches = MeshRefCount * BatchesPerMeshRef; - - uint? populateEntityId = null; - uint populateLandblockId = 0u; - - // Simulate Draw's foreach over _walkScratch. _walkScratch yields - // (entity, MeshRefIndex, landblockId) β€” all MeshRefs of one entity - // are contiguous because the walk emits them in entity-order. - for (int meshRefIdx = 0; meshRefIdx < MeshRefCount; meshRefIdx++) - { - // Boundary check: same entity across all 3 iterations, so this - // never fires the flush. populateEntityId stays as is (null on - // first iter; EntityId on subsequent iters after we set it). - (populateEntityId, populateLandblockId) = WbDrawDispatcher.MaybeFlushOnEntityChange( - populateEntityId, populateLandblockId, EntityId, cache, scratch); - - // Mimic ClassifyBatches' collector output for THIS MeshRef: - // 2 batches with distinct (ibo, firstIndex, texHandle) so the - // ordering can be verified post-hoc. - for (int b = 0; b < BatchesPerMeshRef; b++) - { - ulong texHandle = (ulong)(0x100 + meshRefIdx * BatchesPerMeshRef + b); - scratch.Add(MakeCachedBatch( - ibo: (uint)(meshRefIdx + 1), - firstIndex: (uint)(b * 6), - indexCount: 6, - texHandle: texHandle)); - } - - // After ClassifyBatches, Draw sets the tracker (matching the - // `if (collector is not null)` block at line 482-486 in - // WbDrawDispatcher.Draw). - populateEntityId = EntityId; - populateLandblockId = LandblockId; - } - - // End-of-loop final flush. Without this call (or if Populate fired - // per-tuple inside the loop), the cache would only hold the last - // 2 batches β€” exactly the bug class from commit 00fa8ae. - WbDrawDispatcher.FinalFlushPopulate(populateEntityId, populateLandblockId, cache, scratch); - - // Assertions: ONE cache entry with ALL 6 batches in MeshRef order. - Assert.Equal(1, cache.Count); - Assert.True(cache.TryGet(EntityId, LandblockId, out var entry)); - Assert.NotNull(entry); - Assert.Equal(EntityId, entry!.EntityId); - Assert.Equal(LandblockId, entry.LandblockHint); - - // KEY ASSERTION: Batches.Length == sum across MeshRefs (6), - // NOT just the last MeshRef's batch count (2). - Assert.Equal(ExpectedTotalBatches, entry.Batches.Length); - - // Per-batch ordering check: batches arrived in MeshRef order, so - // texture handles run 0x100..0x105 in the order they were appended. - for (int i = 0; i < ExpectedTotalBatches; i++) - Assert.Equal((ulong)(0x100 + i), entry.Batches[i].BindlessTextureHandle); - - // After flush, scratch is cleared so the next entity starts fresh. - Assert.Empty(scratch); - } - - [Fact] - public void Cache_Populate_SkipsEntityWithIncompleteClassification() - { - // Regression test for the bug where an entity with >=1 MeshRef whose - // mesh data was still async-decoding at populate time would have a - // PARTIAL set of batches written to the cache. Subsequent frame - // cache-hits served the partial entry indefinitely, leaving parts of - // multi-part entities (drudge statue, etc.) permanently missing. - // - // The fix: track currentEntityIncomplete during the foreach. If any - // tuple's TryGetRenderData returned null, drop the accumulated - // populate scratch at entity boundary instead of caching it. The - // slow path retries on the next frame; once all meshes have loaded, - // the populate fires correctly with the complete classification. - // - // This test simulates Draw's inner-loop logic: 3 MeshRef tuples for - // one entity where tuple 0 produces null renderData (flag the entity - // incomplete + continue, no batches), and tuples 1 and 2 produce - // valid renderData (classify + accumulate). End-of-loop check drops - // scratch + nulls populateEntityId BEFORE FinalFlushPopulate, so the - // cache stays empty for this entity. - var cache = new EntityClassificationCache(); - const uint EntityId = 100; - const uint LandblockId = 0xA9B40000u; - - // Simulate Draw's per-entity inner-loop logic. - var scratch = new List(); - bool currentEntityIncomplete = false; - uint? populateEntityId = null; - uint populateLandblockId = 0u; - - // Tuple 0 (MeshRef[0]): renderData null -> flag incomplete, skip classify. - currentEntityIncomplete = true; - - // Tuple 1 (MeshRef[1]): renderData valid -> classify, accumulate. - scratch.Add(MakeCachedBatch(ibo: 1, firstIndex: 0, indexCount: 6, texHandle: 0xAAul)); - populateEntityId = EntityId; - populateLandblockId = LandblockId; - - // Tuple 2 (MeshRef[2]): renderData valid -> classify, accumulate. - scratch.Add(MakeCachedBatch(ibo: 2, firstIndex: 0, indexCount: 6, texHandle: 0xBBul)); - populateEntityId = EntityId; - populateLandblockId = LandblockId; - - // End of loop: check incomplete flag, drop scratch + null tracker - // BEFORE FinalFlushPopulate so the helper sees the cleaned state. - if (currentEntityIncomplete) - { - scratch.Clear(); - populateEntityId = null; - } - WbDrawDispatcher.FinalFlushPopulate(populateEntityId, populateLandblockId, cache, scratch); - - // Cache should NOT have an entry for this entity β€” partial population - // would be worse than no cache (cache hit would persist the partial - // render forever; cache miss retries and gets it right next frame). - Assert.Equal(0, cache.Count); - Assert.False(cache.TryGet(EntityId, LandblockId, out _)); - } - - [Fact] - public void ApplyCacheHit_PerTupleAmplification_DoesNotOccur() - { - // Regression test for the bug fixed at the commit landing alongside - // this test: Task 10's first attempt called ApplyCacheHit per-(entity, - // MeshRefIndex) tuple in Draw's foreach, but cachedEntry.Batches is - // flat across all MeshRefs of the entity. For a 3-MeshRef building on - // frame 2: 3 tuples Γ— 6 cached batches per call = 18 instances drawn - // instead of 6. Severe Z-fighting and 3Γ— perf hit on every multi-part - // static entity (buildings, statues, multi-MeshRef NPCs). - // - // This is the symmetric mirror of the Task 9 bug fixed at 00fa8ae β€” - // both came from spec Β§5.2 describing the foreach as per-entity when - // _walkScratch is per-tuple. - // - // The fix: track lastHitEntityId; the cache-hit fast path fires only - // on the FIRST tuple of each entity. Subsequent tuples of the same - // entity skip the iteration body via continue. - // - // This test simulates the inner-loop logic by directly invoking - // ApplyCacheHit + AppendInstanceToGroup the way Draw would, with N - // tuples for the same entity, asserting that groups[key].Count equals - // the cached batch count (6), NOT N Γ— cached batch count (18). - - // Set up a synthetic cache entry with 6 batches (representing 3 - // MeshRefs Γ— 2 batches each). - const int CachedBatchCount = 6; - var cache = new EntityClassificationCache(); - var batches = new CachedBatch[CachedBatchCount]; - for (int i = 0; i < CachedBatchCount; i++) - { - batches[i] = MakeCachedBatch( - ibo: 1u, firstIndex: (uint)i, indexCount: 6, texHandle: (ulong)(0x100 + i)); - } - cache.Populate(entityId: 100, landblockHint: 0xA9B40000u, batches); - - // Simulate Draw's per-entity loop: 3 tuples for the same entity. - // Track which entity has already cache-hit (mirrors the production - // lastHitEntityId pattern). - var groups = new Dictionary>(); - uint? lastHitEntityId = null; - var entityWorld = Matrix4x4.Identity; // simplest case for assertion clarity - const uint EntityId = 100; - const int MeshRefCount = 3; - - void AppendInstance(GroupKey k, Matrix4x4 m) - { - if (!groups.TryGetValue(k, out var list)) - { - list = new List(); - groups[k] = list; - } - list.Add(m); - } - - for (int partIdx = 0; partIdx < MeshRefCount; partIdx++) - { - // Skip subsequent tuples of an entity that cache-hit (the fix). - if (lastHitEntityId == EntityId) continue; - - if (cache.TryGet(EntityId, 0xA9B40000u, out var entry)) - { - Assert.NotNull(entry); - WbDrawDispatcher.ApplyCacheHit(entry!, entityWorld, AppendInstance); - lastHitEntityId = EntityId; - } - } - - // Assertion: each group's matrix count equals the cached batches matching - // that key, NOT (cached batches Γ— MeshRef count). Here each batch has a - // unique key, so each group has exactly 1 matrix. - int totalMatrices = 0; - foreach (var (_, matrices) in groups) totalMatrices += matrices.Count; - Assert.Equal(CachedBatchCount, totalMatrices); // 6, NOT 18 - - // Sanity: 6 distinct keys (one per cached batch since FirstIndex differs). - Assert.Equal(CachedBatchCount, groups.Count); - } -} diff --git a/tests/AcDream.Core.Tests/Rendering/Wb/WbDrawDispatcherIndirectBuilderTests.cs b/tests/AcDream.Core.Tests/Rendering/Wb/WbDrawDispatcherIndirectBuilderTests.cs deleted file mode 100644 index 855a2ef..0000000 --- a/tests/AcDream.Core.Tests/Rendering/Wb/WbDrawDispatcherIndirectBuilderTests.cs +++ /dev/null @@ -1,113 +0,0 @@ -using System.Numerics; -using AcDream.App.Rendering.Wb; -using AcDream.Core.Meshing; -using Xunit; - -namespace AcDream.Core.Tests.Rendering.Wb; - -/// -/// Pure CPU test of . -/// Verifies that a synthetic group set lays out into the indirect buffer -/// + parallel batch data with opaque section first, transparent second, -/// per-group fields propagated correctly. -/// -public sealed class WbDrawDispatcherIndirectBuilderTests -{ - [Fact] - public void TwoOpaqueGroupsAndOneTransparent_LaysOutContiguouslyOpaqueFirst() - { - // Arrange β€” three groups: 2 opaque (12+1 instances) + 1 transparent (12 instances) - var groups = new List - { - new(IndexCount: 100, FirstIndex: 0, BaseVertex: 0, InstanceCount: 12, FirstInstance: 0, TextureHandle: 0xAA, TextureLayer: 0, Translucency: TranslucencyKind.Opaque), - new(IndexCount: 200, FirstIndex: 100, BaseVertex: 0, InstanceCount: 12, FirstInstance: 12, TextureHandle: 0xBB, TextureLayer: 0, Translucency: TranslucencyKind.AlphaBlend), - new(IndexCount: 50, FirstIndex: 300, BaseVertex: 100, InstanceCount: 1, FirstInstance: 24, TextureHandle: 0xCC, TextureLayer: 0, Translucency: TranslucencyKind.Opaque), - }; - - var indirect = new DrawElementsIndirectCommand[16]; - var batch = new WbDrawDispatcher.BatchDataPublic[16]; - - // Act - var result = WbDrawDispatcher.BuildIndirectArrays(groups, indirect, batch); - - // Assert layout - Assert.Equal(2, result.OpaqueCount); - Assert.Equal(1, result.TransparentCount); - Assert.Equal(2 * 20, result.TransparentByteOffset); // sizeof(DEIC) = 20 - - // Opaque section, in input order (Task 10 callers sort) - Assert.Equal(100u, indirect[0].Count); - Assert.Equal(0u, indirect[0].FirstIndex); - Assert.Equal(0, indirect[0].BaseVertex); - Assert.Equal(12u, indirect[0].InstanceCount); - Assert.Equal(0u, indirect[0].BaseInstance); - - Assert.Equal(50u, indirect[1].Count); - Assert.Equal(300u, indirect[1].FirstIndex); - Assert.Equal(100, indirect[1].BaseVertex); - Assert.Equal(1u, indirect[1].InstanceCount); - Assert.Equal(24u, indirect[1].BaseInstance); - - // Transparent section - Assert.Equal(200u, indirect[2].Count); - Assert.Equal(100u, indirect[2].FirstIndex); - Assert.Equal(12u, indirect[2].InstanceCount); - Assert.Equal(12u, indirect[2].BaseInstance); - - // BatchData parallel β€” same indices as indirect - Assert.Equal(0xAAul, batch[0].TextureHandle); - Assert.Equal(0xCCul, batch[1].TextureHandle); - Assert.Equal(0xBBul, batch[2].TextureHandle); - } - - [Fact] - public void EmptyGroupList_ProducesZeroCounts() - { - var groups = new List(); - var indirect = new DrawElementsIndirectCommand[0]; - var batch = new WbDrawDispatcher.BatchDataPublic[0]; - - var result = WbDrawDispatcher.BuildIndirectArrays(groups, indirect, batch); - - Assert.Equal(0, result.OpaqueCount); - Assert.Equal(0, result.TransparentCount); - Assert.Equal(0, result.TransparentByteOffset); - } - - [Fact] - public void ClipMapTreatedAsOpaque() - { - // ClipMap surfaces (alpha-cutout) belong with the opaque pass - // because the discard handles transparency, not blending. - var groups = new List - { - new(IndexCount: 10, FirstIndex: 0, BaseVertex: 0, InstanceCount: 1, FirstInstance: 0, TextureHandle: 0x1, TextureLayer: 0, Translucency: TranslucencyKind.ClipMap), - }; - var indirect = new DrawElementsIndirectCommand[4]; - var batch = new WbDrawDispatcher.BatchDataPublic[4]; - - var result = WbDrawDispatcher.BuildIndirectArrays(groups, indirect, batch); - - Assert.Equal(1, result.OpaqueCount); - Assert.Equal(0, result.TransparentCount); - } - - [Fact] - public void BatchDataPublic_LayoutMatchesPrivateBatchData() - { - // Task 10 will use MemoryMarshal.Cast to - // expose the dispatcher's per-frame BatchData[] scratch to BuildIndirectArrays - // without copying. The cast is only safe if the structs have identical - // layout (size, field offsets). Both use [StructLayout(Sequential, Pack=8)]. - Assert.Equal(16, System.Runtime.CompilerServices.Unsafe.SizeOf()); - Assert.Equal(0, (int)System.Runtime.InteropServices.Marshal.OffsetOf(nameof(WbDrawDispatcher.BatchDataPublic.TextureHandle))); - Assert.Equal(8, (int)System.Runtime.InteropServices.Marshal.OffsetOf(nameof(WbDrawDispatcher.BatchDataPublic.TextureLayer))); - Assert.Equal(12, (int)System.Runtime.InteropServices.Marshal.OffsetOf(nameof(WbDrawDispatcher.BatchDataPublic.Flags))); - } - - [Fact] - public void DrawCommandStride_MatchesStructSize() - { - Assert.Equal(WbDrawDispatcher.DrawCommandStride, System.Runtime.CompilerServices.Unsafe.SizeOf()); - } -} diff --git a/tests/AcDream.Core.Tests/Rendering/Wb/WbDrawDispatcherTranslucencyTests.cs b/tests/AcDream.Core.Tests/Rendering/Wb/WbDrawDispatcherTranslucencyTests.cs deleted file mode 100644 index f79fb09..0000000 --- a/tests/AcDream.Core.Tests/Rendering/Wb/WbDrawDispatcherTranslucencyTests.cs +++ /dev/null @@ -1,25 +0,0 @@ -using AcDream.App.Rendering.Wb; -using AcDream.Core.Meshing; -using Xunit; - -namespace AcDream.Core.Tests.Rendering.Wb; - -/// -/// Locks in the N.5 translucency partition contract (spec Decision 2). -/// If the partition drifts, the dispatcher's opaque + transparent indirect -/// passes will silently render the wrong groups in the wrong pass β€” visible -/// regression that's hard to spot in code review. -/// -public sealed class WbDrawDispatcherTranslucencyTests -{ - [Theory] - [InlineData(TranslucencyKind.Opaque, true)] - [InlineData(TranslucencyKind.ClipMap, true)] - [InlineData(TranslucencyKind.AlphaBlend, false)] - [InlineData(TranslucencyKind.Additive, false)] - [InlineData(TranslucencyKind.InvAlpha, false)] - public void IsOpaque_PartitionsByKind(TranslucencyKind kind, bool expected) - { - Assert.Equal(expected, WbDrawDispatcher.IsOpaquePublic(kind)); - } -} diff --git a/tests/AcDream.Core.Tests/Rendering/Wb/WbMeshAdapterTests.cs b/tests/AcDream.Core.Tests/Rendering/Wb/WbMeshAdapterTests.cs deleted file mode 100644 index 1053f85..0000000 --- a/tests/AcDream.Core.Tests/Rendering/Wb/WbMeshAdapterTests.cs +++ /dev/null @@ -1,65 +0,0 @@ -using System; -using AcDream.App.Rendering.Wb; -using Microsoft.Extensions.Logging.Abstractions; -using Silk.NET.OpenGL; - -namespace AcDream.Core.Tests.Rendering.Wb; - -public sealed class WbMeshAdapterTests -{ - [Fact] - public void Construct_WithNullGl_ThrowsArgumentNull() - { - // GL is the first guarded parameter; verifies the constructor validates inputs. - // We can't pass a real GL (no context in tests), so we verify only the - // null-GL guard. The real pipeline is tested via integration. - Assert.Throws(() => - new WbMeshAdapter(gl: null!, datDir: "some/path", dats: null!, logger: NullLogger.Instance)); - } - - [Fact] - public void Dispose_OnUninitializedAdapter_DoesNotThrow() - { - var adapter = WbMeshAdapter.CreateUninitialized(); - adapter.Dispose(); // no-op when fields are null - adapter.Dispose(); // idempotent - } - - [Fact] - public void IncrementRefCount_OnUninitializedAdapter_NoOps() - { - var adapter = WbMeshAdapter.CreateUninitialized(); - // Should not throw, even though there's no underlying mesh manager. - adapter.IncrementRefCount(0x01000001ul); - } - - [Fact] - public void DecrementRefCount_OnUninitializedAdapter_NoOps() - { - var adapter = WbMeshAdapter.CreateUninitialized(); - adapter.DecrementRefCount(0x01000001ul); - } - - [Fact] - public void GetRenderData_OnUninitializedAdapter_ReturnsNull() - { - var adapter = WbMeshAdapter.CreateUninitialized(); - Assert.Null(adapter.GetRenderData(0x01000001ul)); - } - - [Fact] - public void Tick_OnUninitializedAdapter_DoesNotThrow() - { - var adapter = WbMeshAdapter.CreateUninitialized(); - adapter.Tick(); // no-op, no throw - adapter.Tick(); // idempotent - } - - [Fact] - public void Tick_AfterDispose_DoesNotThrow() - { - var adapter = WbMeshAdapter.CreateUninitialized(); - adapter.Dispose(); - adapter.Tick(); // no-op, no throw - } -} diff --git a/tests/AcDream.Core.Tests/Selection/WorldPickerTests.cs b/tests/AcDream.Core.Tests/Selection/WorldPickerTests.cs deleted file mode 100644 index ac0bc5a..0000000 --- a/tests/AcDream.Core.Tests/Selection/WorldPickerTests.cs +++ /dev/null @@ -1,169 +0,0 @@ -using System; -using System.Numerics; -using AcDream.Core.Selection; -using AcDream.Core.World; -using Xunit; - -namespace AcDream.Core.Tests.Selection; - -public class WorldPickerTests -{ - private const float Epsilon = 0.01f; - - private static (Matrix4x4 View, Matrix4x4 Projection) MakeIdentityCamera() - { - var view = Matrix4x4.Identity; - var proj = Matrix4x4.CreatePerspectiveFieldOfView( - fieldOfView: MathF.PI / 3f, - aspectRatio: 16f / 9f, - nearPlaneDistance: 0.1f, - farPlaneDistance: 100f); - return (view, proj); - } - - [Fact] - public void BuildRay_CenterOfViewport_ReturnsForwardRay() - { - var (view, proj) = MakeIdentityCamera(); - const float vpW = 1920f, vpH = 1080f; - - var (_, direction) = WorldPicker.BuildRay( - mouseX: vpW / 2f, mouseY: vpH / 2f, - viewportW: vpW, viewportH: vpH, - view, proj); - - // Right-handed perspective + identity view -> camera looks down -Z. - // Center pixel ray = (0, 0, -1) within float epsilon. - Assert.True(MathF.Abs(direction.X) < Epsilon, $"direction.X = {direction.X}"); - Assert.True(MathF.Abs(direction.Y) < Epsilon, $"direction.Y = {direction.Y}"); - Assert.True(direction.Z < -0.99f, $"direction.Z = {direction.Z}"); - } - - [Fact] - public void BuildRay_OffsetMouseRight_DeflectsRayPositiveX() - { - var (view, proj) = MakeIdentityCamera(); - const float vpW = 1920f, vpH = 1080f; - - var (_, direction) = WorldPicker.BuildRay( - mouseX: vpW * 0.75f, mouseY: vpH / 2f, - viewportW: vpW, viewportH: vpH, - view, proj); - - Assert.True(direction.X > 0.1f, $"direction.X = {direction.X} (expected > 0.1)"); - } - - private static WorldEntity MakeEntity(uint serverGuid, Vector3 position) => new() - { - Id = serverGuid == 0u ? 1u : serverGuid, - ServerGuid = serverGuid, - SourceGfxObjOrSetupId = 0u, - Position = position, - Rotation = Quaternion.Identity, - MeshRefs = Array.Empty(), - }; - - [Fact] - public void Pick_RayThroughEntity_ReturnsServerGuid() - { - var entity = MakeEntity(0xABCDu, new Vector3(0, 0, -10)); - - var result = WorldPicker.Pick( - origin: Vector3.Zero, - direction: -Vector3.UnitZ, - candidates: new[] { entity }, - skipServerGuid: 0u); - - Assert.Equal(0xABCDu, result); - } - - [Fact] - public void Pick_RayMisses_ReturnsNull() - { - var entity = MakeEntity(0xABCDu, new Vector3(0, 0, -10)); - - var result = WorldPicker.Pick( - origin: Vector3.Zero, - direction: Vector3.UnitX, - candidates: new[] { entity }, - skipServerGuid: 0u); - - Assert.Null(result); - } - - [Fact] - public void Pick_TwoEntitiesInLine_ReturnsCloser() - { - var near = MakeEntity(0x1111u, new Vector3(0, 0, -5)); - var far = MakeEntity(0x2222u, new Vector3(0, 0, -20)); - - var result = WorldPicker.Pick( - origin: Vector3.Zero, - direction: -Vector3.UnitZ, - candidates: new[] { far, near }, // iteration order shouldn't matter - skipServerGuid: 0u); - - Assert.Equal(0x1111u, result); - } - - [Fact] - public void Pick_SkipsSkipGuid() - { - var entity = MakeEntity(0xABCDu, new Vector3(0, 0, -10)); - - var result = WorldPicker.Pick( - origin: Vector3.Zero, - direction: -Vector3.UnitZ, - candidates: new[] { entity }, - skipServerGuid: 0xABCDu); - - Assert.Null(result); - } - - [Fact] - public void Pick_SkipsZeroServerGuid() - { - // Atlas-tier scenery / dat-hydrated statics carry ServerGuid=0 - // and aren't valid Use targets β€” server would reject guid=0. - var entity = MakeEntity(0u, new Vector3(0, 0, -10)); - - var result = WorldPicker.Pick( - origin: Vector3.Zero, - direction: -Vector3.UnitZ, - candidates: new[] { entity }, - skipServerGuid: 0xDEADu); - - Assert.Null(result); - } - - [Fact] - public void Pick_BeyondMaxDistance_ReturnsNull() - { - var entity = MakeEntity(0xABCDu, new Vector3(0, 0, -100)); - - var result = WorldPicker.Pick( - origin: Vector3.Zero, - direction: -Vector3.UnitZ, - candidates: new[] { entity }, - skipServerGuid: 0u); // default maxDistance = 50f - - Assert.Null(result); - } - - [Fact] - public void Pick_RayOriginInsideEntitySphere_StillReturnsServerGuid() - { - // Player ~3m from a door -> camera near-plane sits INSIDE the door's - // 5m bounding sphere. Naive t_near < 0 guard would skip; correct - // behavior is to fall through to t_far (the sphere exit point). - var entity = MakeEntity(0xABCDu, new Vector3(0, 0, -3)); - - var result = WorldPicker.Pick( - origin: Vector3.Zero, - direction: -Vector3.UnitZ, - candidates: new[] { entity }, - skipServerGuid: 0u); - - Assert.Equal(0xABCDu, result); - } -} diff --git a/tests/AcDream.Core.Tests/Streaming/GpuWorldStateActivatorTests.cs b/tests/AcDream.Core.Tests/Streaming/GpuWorldStateActivatorTests.cs deleted file mode 100644 index e5a2034..0000000 --- a/tests/AcDream.Core.Tests/Streaming/GpuWorldStateActivatorTests.cs +++ /dev/null @@ -1,169 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Numerics; -using AcDream.App.Rendering.Vfx; -using AcDream.App.Streaming; -using AcDream.Core.Physics; -using AcDream.Core.Vfx; -using AcDream.Core.World; -using DatReaderWriter.DBObjs; -using DatReaderWriter.Types; -using Xunit; -using DatPhysicsScript = DatReaderWriter.DBObjs.PhysicsScript; - -namespace AcDream.Core.Tests.Streaming; - -/// -/// Phase C.1.5b: verifies fires -/// / -/// from the four -/// dat-hydration paths (AddLandblock, AddEntitiesToExistingLandblock, -/// RemoveLandblock, RemoveEntitiesFromLandblock), and that the -/// pending-bucket merge in AddLandblock does NOT double-fire for live -/// entities that already had OnCreate at . -/// -public sealed class GpuWorldStateActivatorTests -{ - private sealed class RecordingSink : IAnimationHookSink - { - public List<(uint EntityId, Vector3 Pos, AnimationHook Hook)> Calls = new(); - public void OnHook(uint entityId, Vector3 worldPos, AnimationHook hook) - => Calls.Add((entityId, worldPos, hook)); - } - - private sealed record Pipeline( - GpuWorldState State, - PhysicsScriptRunner Runner, - ParticleHookSink Sink, - RecordingSink Recording); - - private static Pipeline BuildPipeline(uint scriptId) - { - var script = new DatPhysicsScript(); - script.ScriptData.Add(new PhysicsScriptData - { - StartTime = 0.0, - Hook = new CreateParticleHook { EmitterInfoId = 100u, Offset = new Frame() }, - }); - var table = new Dictionary { [scriptId] = script }; - - var registry = new EmitterDescRegistry(); - var system = new ParticleSystem(registry); - var sink = new ParticleHookSink(system); - var recording = new RecordingSink(); - var runner = new PhysicsScriptRunner(id => table.TryGetValue(id, out var s) ? s : null, recording); - var activator = new EntityScriptActivator(runner, sink, - _ => new ScriptActivationInfo(scriptId, Array.Empty())); - - var state = new GpuWorldState(entityScriptActivator: activator); - return new Pipeline(state, runner, sink, recording); - } - - private static LoadedLandblock MakeStubLandblock(uint canonicalId, params WorldEntity[] entities) - => new(canonicalId, new LandBlock(), entities); - - private static WorldEntity DatHydrated(uint id, Vector3 pos) => new() - { - Id = id, - ServerGuid = 0u, - SourceGfxObjOrSetupId = 0x02000001u, - Position = pos, - Rotation = Quaternion.Identity, - MeshRefs = Array.Empty(), - }; - - private static WorldEntity Live(uint serverGuid, Vector3 pos) => new() - { - Id = serverGuid, - ServerGuid = serverGuid, - SourceGfxObjOrSetupId = 0x02000001u, - Position = pos, - Rotation = Quaternion.Identity, - MeshRefs = Array.Empty(), - }; - - [Fact] - public void AddLandblock_FiresActivatorForDatHydratedEntity() - { - var p = BuildPipeline(scriptId: 0xAAu); - var entity = DatHydrated(id: 0x40A9B401u, pos: new Vector3(1, 2, 3)); - var lb = MakeStubLandblock(0xA9B4FFFFu, entity); - - p.State.AddLandblock(lb); - - // Tick fires the CreateParticleHook into RecordingSink. - p.Runner.Tick(0.001f); - Assert.Single(p.Recording.Calls); - Assert.Equal(0x40A9B401u, p.Recording.Calls[0].EntityId); - Assert.Equal(new Vector3(1, 2, 3), p.Recording.Calls[0].Pos); - } - - [Fact] - public void AddLandblock_DoesNotDoubleFire_OnPendingMerge() - { - // Live entity (ServerGuid!=0) arrives via AppendLiveEntity first β€” - // OnCreate fires once at that point. Then AddLandblock for the - // same canonical id pulls the pending entity into the loaded list. - // The new fire-site MUST NOT call OnCreate again (the live entity - // is filtered out by ServerGuid != 0). - var p = BuildPipeline(scriptId: 0xAAu); - var live = Live(serverGuid: 0xCAFEu, pos: Vector3.Zero); - - p.State.AppendLiveEntity(0xA9B4FFFFu, live); - var emptyLb = MakeStubLandblock(0xA9B4FFFFu); - p.State.AddLandblock(emptyLb); - - p.Runner.Tick(0.001f); - Assert.Single(p.Recording.Calls); // exactly one β€” no double-fire - Assert.Equal(0xCAFEu, p.Recording.Calls[0].EntityId); - } - - [Fact] - public void RemoveLandblock_FiresOnRemoveForDatHydratedEntity() - { - var p = BuildPipeline(scriptId: 0xAAu); - var entity = DatHydrated(id: 0x40A9B401u, pos: Vector3.Zero); - var lb = MakeStubLandblock(0xA9B4FFFFu, entity); - - p.State.AddLandblock(lb); - // Don't Tick: Play queued the script in _active immediately; ticking - // would fire its single StartTime=0 hook and self-remove the script - // before we can observe RemoveLandblock cleaning it up. - Assert.Equal(1, p.Runner.ActiveScriptCount); - - p.State.RemoveLandblock(0xA9B4FFFFu); - Assert.Equal(0, p.Runner.ActiveScriptCount); - } - - [Fact] - public void AddEntitiesToExistingLandblock_FiresActivatorForEachPromoted() - { - var p = BuildPipeline(scriptId: 0xAAu); - var emptyLb = MakeStubLandblock(0xA9B4FFFFu); - p.State.AddLandblock(emptyLb); - - var promoted = new[] - { - DatHydrated(id: 0x40A9B401u, pos: Vector3.Zero), - DatHydrated(id: 0x40A9B402u, pos: Vector3.UnitX), - }; - p.State.AddEntitiesToExistingLandblock(0xA9B4FFFFu, promoted); - - p.Runner.Tick(0.001f); - Assert.Equal(2, p.Recording.Calls.Count); - } - - [Fact] - public void RemoveEntitiesFromLandblock_FiresOnRemoveForDatHydratedEntities() - { - var p = BuildPipeline(scriptId: 0xAAu); - var entity = DatHydrated(id: 0x40A9B401u, pos: Vector3.Zero); - var lb = MakeStubLandblock(0xA9B4FFFFu, entity); - p.State.AddLandblock(lb); - // Don't Tick: see comment in RemoveLandblock_FiresOnRemoveForDatHydratedEntity. - Assert.Equal(1, p.Runner.ActiveScriptCount); - - p.State.RemoveEntitiesFromLandblock(0xA9B4FFFFu); - Assert.Equal(0, p.Runner.ActiveScriptCount); - } -} diff --git a/tests/AcDream.Core.Tests/Streaming/GpuWorldStateTwoTierTests.cs b/tests/AcDream.Core.Tests/Streaming/GpuWorldStateTwoTierTests.cs deleted file mode 100644 index 24950fd..0000000 --- a/tests/AcDream.Core.Tests/Streaming/GpuWorldStateTwoTierTests.cs +++ /dev/null @@ -1,125 +0,0 @@ -using System.Linq; -using AcDream.App.Streaming; -using AcDream.Core.World; -using DatReaderWriter.DBObjs; -using Xunit; - -namespace AcDream.Core.Tests.Streaming; - -public class GpuWorldStateTwoTierTests -{ - private static LoadedLandblock MakeStubLandblock(uint canonicalId, params WorldEntity[] entities) - => new(canonicalId, new LandBlock(), entities); - - private static WorldEntity MakeStubEntity(uint id) - => new() - { - Id = id, - SourceGfxObjOrSetupId = 0x01000001u, - Position = System.Numerics.Vector3.Zero, - Rotation = System.Numerics.Quaternion.Identity, - MeshRefs = System.Array.Empty(), - }; - - [Fact] - public void RemoveEntitiesFromLandblock_KeepsLandblockButDropsEntities() - { - var state = new GpuWorldState(); - var lb = MakeStubLandblock(0xAAAAFFFFu, - MakeStubEntity(1), - MakeStubEntity(2)); - state.AddLandblock(lb); - Assert.Equal(2, state.Entities.Count); - - state.RemoveEntitiesFromLandblock(0xAAAAFFFFu); - - Assert.Empty(state.Entities); - Assert.True(state.IsLoaded(0xAAAAFFFFu)); // landblock still resident - } - - [Fact] - public void AddEntitiesToExistingLandblock_MergesIntoExistingRecord() - { - var state = new GpuWorldState(); - var lb = MakeStubLandblock(0xAAAAFFFFu, MakeStubEntity(1)); - state.AddLandblock(lb); - - state.AddEntitiesToExistingLandblock(0xAAAAFFFFu, new[] - { - MakeStubEntity(2), - MakeStubEntity(3), - }); - - Assert.Equal(3, state.Entities.Count); - } - - [Fact] - public void AddEntitiesToExistingLandblock_LandblockNotYetLoaded_ParksInPending() - { - var state = new GpuWorldState(); - - // Landblock not loaded yet. - state.AddEntitiesToExistingLandblock(0xAAAAFFFFu, new[] - { - MakeStubEntity(1), - MakeStubEntity(2), - }); - - // Nothing in the flat view yet. - Assert.Empty(state.Entities); - Assert.Equal(2, state.PendingLiveEntityCount); - - // Now load the landblock β€” pending entities should merge in. - state.AddLandblock(MakeStubLandblock(0xAAAAFFFFu)); - Assert.Equal(2, state.Entities.Count); - } - - /// - /// Phase Post-A.5 #53 (Task 12): the optional onLandblockUnloaded - /// callback fires once when - /// drops a landblock's entity list, and is passed the canonicalized - /// landblock id (matching the LandblockHint the cache stored at - /// Populate time). - /// - [Fact] - public void RemoveEntitiesFromLandblock_FiresUnloadCallbackWithCanonicalId() - { - uint? observed = null; - int callCount = 0; - var state = new GpuWorldState( - wbSpawnAdapter: null, - wbEntitySpawnAdapter: null, - onLandblockUnloaded: id => { observed = id; callCount++; }); - - state.AddLandblock(MakeStubLandblock(0xA9B4FFFFu, MakeStubEntity(1))); - - // Pass a cell-resolved id (low 16 bits non-FFFF) β€” the callback must - // receive the canonical (0xFFFF-tail) form, matching what the - // dispatcher's _walkScratch carries from GpuWorldState.LandblockEntries. - state.RemoveEntitiesFromLandblock(0xA9B40042u); - - Assert.Equal(1, callCount); - Assert.Equal(0xA9B4FFFFu, observed); - Assert.Empty(state.Entities); - } - - /// - /// Phase Post-A.5 #53 (Task 12): the callback must NOT fire when the - /// landblock isn't loaded β€” early return path. Symmetric with the - /// existing _wbSpawnAdapter.OnLandblockUnloaded guard. - /// - [Fact] - public void RemoveEntitiesFromLandblock_NotLoaded_DoesNotFireCallback() - { - int callCount = 0; - var state = new GpuWorldState( - wbSpawnAdapter: null, - wbEntitySpawnAdapter: null, - onLandblockUnloaded: _ => callCount++); - - // Landblock never loaded. - state.RemoveEntitiesFromLandblock(0xA9B4FFFFu); - - Assert.Equal(0, callCount); - } -} diff --git a/tests/AcDream.Core.Tests/Streaming/LandblockStreamerTests.cs b/tests/AcDream.Core.Tests/Streaming/LandblockStreamerTests.cs index 7c5291c..e058f81 100644 --- a/tests/AcDream.Core.Tests/Streaming/LandblockStreamerTests.cs +++ b/tests/AcDream.Core.Tests/Streaming/LandblockStreamerTests.cs @@ -19,13 +19,9 @@ public class LandblockStreamerTests 0xA9B4FFFEu, new LandBlock(), System.Array.Empty()); - var stubMesh = new AcDream.Core.Terrain.LandblockMeshData( - System.Array.Empty(), - System.Array.Empty()); using var streamer = new LandblockStreamer( - loadLandblock: id => id == 0xA9B4FFFEu ? stubLandblock : null, - buildMeshOrNull: (_, _) => stubMesh); + loadLandblock: id => id == 0xA9B4FFFEu ? stubLandblock : null); streamer.Start(); streamer.EnqueueLoad(0xA9B4FFFEu); @@ -66,39 +62,6 @@ public class LandblockStreamerTests Assert.IsType(result); } - [Fact] - public async Task Load_WhenBuildMeshReturnsNull_ReportsFailed() - { - // Phase A.5 T10-T12 follow-up: the mesh-build factory may return - // null (e.g., LandBlock dat missing or corrupt). The worker must - // emit Failed in that case instead of constructing Loaded with a - // null MeshData (which would NRE downstream). - var stubLandblock = new LoadedLandblock( - 0xABCDFFFEu, - new LandBlock(), - System.Array.Empty()); - - using var streamer = new LandblockStreamer( - loadLandblock: _ => stubLandblock, - buildMeshOrNull: (_, _) => null); // mesh-build returns null - - streamer.Start(); - streamer.EnqueueLoad(0xABCDFFFEu); - - LandblockStreamResult? result = null; - for (int i = 0; i < SpinMaxIterations && result is null; i++) - { - var drained = streamer.DrainCompletions(LandblockStreamer.DefaultDrainBatchSize); - if (drained.Count > 0) result = drained[0]; - else await Task.Delay(SpinStepMs); - } - - Assert.NotNull(result); - var failed = Assert.IsType(result); - Assert.Equal(0xABCDFFFEu, failed.LandblockId); - Assert.Contains("mesh", failed.Error, System.StringComparison.OrdinalIgnoreCase); - } - [Fact] public async Task Load_WhenLoaderThrows_ReportsFailedWithMessage() { @@ -141,46 +104,37 @@ public class LandblockStreamerTests } [Fact] - public async Task Load_ExecutesLoaderOnWorkerThread() + public void Load_ExecutesLoaderSynchronously_OnCallingThread() { - // Phase A.5 T11: the load delegate now runs on the dedicated worker - // thread (not the calling/render thread). This test verifies the - // async hand-off: EnqueueLoad returns immediately and the result - // appears in the outbox only after the worker processes the inbox. + // Streamer was made synchronous after Phase A.1 visual verification + // exposed concurrent dat reads as the cause of "ball of spikes" + // terrain corruption β€” DatReaderWriter's DatCollection isn't + // thread-safe and locking around every dat read on every render- + // thread code path was too invasive. Until Phase A.3 introduces a + // thread-safe dat wrapper, the load delegate runs on the calling + // thread and the result is in the outbox by the time EnqueueLoad + // returns. This test pins that contract. int testThreadId = System.Environment.CurrentManagedThreadId; int? loaderThreadId = null; var stubLandblock = new LoadedLandblock( 0x77770FFEu, new LandBlock(), System.Array.Empty()); - var stubMesh = new AcDream.Core.Terrain.LandblockMeshData( - System.Array.Empty(), - System.Array.Empty()); - using var streamer = new LandblockStreamer( - loadLandblock: id => - { - loaderThreadId = System.Environment.CurrentManagedThreadId; - return stubLandblock; - }, - buildMeshOrNull: (_, _) => stubMesh); + using var streamer = new LandblockStreamer(loadLandblock: id => + { + loaderThreadId = System.Environment.CurrentManagedThreadId; + return stubLandblock; + }); streamer.Start(); streamer.EnqueueLoad(0x77770FFEu); - // Spin until the worker produces a completion. - LandblockStreamResult? result = null; - for (int i = 0; i < SpinMaxIterations && result is null; i++) - { - var drained = streamer.DrainCompletions(LandblockStreamer.DefaultDrainBatchSize); - if (drained.Count > 0) result = drained[0]; - else await Task.Delay(SpinStepMs); - } + // Result is already in the outbox β€” no spinning needed. + var drained = streamer.DrainCompletions(LandblockStreamer.DefaultDrainBatchSize); - Assert.NotNull(result); - Assert.IsType(result); - // The loader MUST have run on a different thread than the test thread. - Assert.NotNull(loaderThreadId); - Assert.NotEqual(testThreadId, loaderThreadId.Value); + Assert.Single(drained); + Assert.IsType(drained[0]); + Assert.Equal(testThreadId, loaderThreadId); } } diff --git a/tests/AcDream.Core.Tests/Streaming/StreamingControllerTests.cs b/tests/AcDream.Core.Tests/Streaming/StreamingControllerTests.cs index 3364d77..f7fa328 100644 --- a/tests/AcDream.Core.Tests/Streaming/StreamingControllerTests.cs +++ b/tests/AcDream.Core.Tests/Streaming/StreamingControllerTests.cs @@ -14,7 +14,7 @@ public class StreamingControllerTests public List Unloads { get; } = new(); public Queue Pending { get; } = new(); - public void EnqueueLoad(uint id, LandblockStreamJobKind _) => Loads.Add(id); + public void EnqueueLoad(uint id) => Loads.Add(id); public void EnqueueUnload(uint id) => Unloads.Add(id); public IReadOnlyList DrainCompletions(int max) { @@ -34,15 +34,14 @@ public class StreamingControllerTests enqueueLoad: fake.EnqueueLoad, enqueueUnload: fake.EnqueueUnload, drainCompletions: fake.DrainCompletions, - applyTerrain: (_, _) => { }, + applyTerrain: _ => { }, state: state, - nearRadius: 2, - farRadius: 2); + radius: 2); // Center at (50, 50); no landblocks loaded yet. controller.Tick(observerCx: 50, observerCy: 50); - // 5Γ—5 window = 25 loads enqueued (nearRadius==farRadius so all go to ToLoadNear), 0 unloads. + // 5Γ—5 window = 25 loads enqueued, 0 unloads. Assert.Equal(25, fake.Loads.Count); Assert.Empty(fake.Unloads); } @@ -54,7 +53,7 @@ public class StreamingControllerTests var fake = new FakeStreamer(); var controller = new StreamingController( fake.EnqueueLoad, fake.EnqueueUnload, fake.DrainCompletions, - (_, _) => { }, state, nearRadius: 2, farRadius: 2); + _ => { }, state, radius: 2); controller.Tick(50, 50); fake.Loads.Clear(); @@ -73,19 +72,13 @@ public class StreamingControllerTests var applied = new List(); var controller = new StreamingController( fake.EnqueueLoad, fake.EnqueueUnload, fake.DrainCompletions, - (lb, _) => applied.Add(lb), state, nearRadius: 2, farRadius: 2); + applied.Add, state, radius: 2); // Note: LoadedLandblock's actual fields are LandblockId, Heightmap, // Entities (positional record). Adjust if the first positional arg // name differs. var lb = new LoadedLandblock(0x32320FFEu, new LandBlock(), System.Array.Empty()); - // A.5 T10-T12 follow-up: use a real empty mesh instance instead of - // default! so any future test that flows MeshData through the apply - // callback gets a non-null reference to inspect rather than an NRE. - var stubMesh = new AcDream.Core.Terrain.LandblockMeshData( - System.Array.Empty(), - System.Array.Empty()); - fake.Pending.Enqueue(new LandblockStreamResult.Loaded(0x32320FFEu, LandblockStreamTier.Near, lb, stubMesh)); + fake.Pending.Enqueue(new LandblockStreamResult.Loaded(0x32320FFEu, lb)); controller.Tick(50, 50); @@ -100,7 +93,7 @@ public class StreamingControllerTests var fake = new FakeStreamer(); var controller = new StreamingController( fake.EnqueueLoad, fake.EnqueueUnload, fake.DrainCompletions, - (_, _) => { }, state, nearRadius: 2, farRadius: 2); + _ => { }, state, radius: 2); var lb = new LoadedLandblock(0x32320FFEu, new LandBlock(), System.Array.Empty()); state.AddLandblock(lb); diff --git a/tests/AcDream.Core.Tests/Streaming/StreamingControllerTwoTierTests.cs b/tests/AcDream.Core.Tests/Streaming/StreamingControllerTwoTierTests.cs deleted file mode 100644 index 2b86b6a..0000000 --- a/tests/AcDream.Core.Tests/Streaming/StreamingControllerTwoTierTests.cs +++ /dev/null @@ -1,134 +0,0 @@ -using System.Collections.Generic; -using AcDream.App.Streaming; -using AcDream.Core.Terrain; -using AcDream.Core.World; -using Xunit; - -namespace AcDream.Core.Tests.Streaming; - -public class StreamingControllerTwoTierTests -{ - [Fact] - public void Tick_FirstCall_EnqueuesNearAndFarLoadsByTier() - { - var loads = new List<(uint Id, LandblockStreamJobKind Kind)>(); - var unloads = new List(); - var state = new GpuWorldState(); - - var ctrl = new StreamingController( - enqueueLoad: (id, kind) => loads.Add((id, kind)), - enqueueUnload: unloads.Add, - drainCompletions: _ => System.Array.Empty(), - applyTerrain: (_, _) => { }, - state: state, - nearRadius: 1, - farRadius: 3); - - ctrl.Tick(observerCx: 100, observerCy: 100); - - int nearCount = 0, farCount = 0; - foreach (var (_, kind) in loads) - { - if (kind == LandblockStreamJobKind.LoadNear) nearCount++; - else if (kind == LandblockStreamJobKind.LoadFar) farCount++; - } - Assert.Equal(9, nearCount); // 3x3 inner ring (radius=1) - Assert.Equal(40, farCount); // 7x7 - 3x3 outer ring (radius=3) - } - - [Fact] - public void Tick_PlayerWalksOutOfNear_ToDemoteRoutesToRemoveEntities() - { - // Setup: bootstrap region at (100,100) with near=1, far=3. - // The bootstrap puts LB (100,100) in the near tier. - // Walking 4+ east drops LB (100,100) past the near-hysteresis - // threshold (NearRadius+2 = 3); ToDemote should fire. - - var loads = new List<(uint, LandblockStreamJobKind)>(); - var unloads = new List(); - var state = new GpuWorldState(); - - // Pre-load LB (100,100) so RemoveEntitiesFromLandblock has something - // to find. The actual entity content doesn't matter for routing. - var lb100 = new LoadedLandblock( - (100u << 24) | (100u << 16) | 0xFFFFu, - Heightmap: null!, - Entities: new[] { new WorldEntity { - Id = 1, SourceGfxObjOrSetupId = 0, - Position = System.Numerics.Vector3.Zero, - Rotation = System.Numerics.Quaternion.Identity, - MeshRefs = System.Array.Empty() } }); - state.AddLandblock(lb100); - Assert.Equal(1, state.Entities.Count); - - var ctrl = new StreamingController( - enqueueLoad: (id, kind) => loads.Add((id, kind)), - enqueueUnload: unloads.Add, - drainCompletions: _ => System.Array.Empty(), - applyTerrain: (_, _) => { }, - state: state, - nearRadius: 1, - farRadius: 3); - - ctrl.Tick(observerCx: 100, observerCy: 100); // bootstrap - loads.Clear(); - - // Walk 4 east β€” LB (100,100) is now Chebyshev distance 4 from new - // center (104,100). NearRadius+2 = 3, so 4 > 3 fires the demote. - ctrl.Tick(observerCx: 104, observerCy: 100); - - // ToDemote runs synchronously on the render thread (no enqueue). - // The visible effect is RemoveEntitiesFromLandblock dropping the entity. - Assert.Empty(state.Entities); - // Terrain stays loaded (demote != unload). - Assert.True(state.IsLoaded((100u << 24) | (100u << 16) | 0xFFFFu)); - } - - [Fact] - public void Tick_DrainingPromoted_RoutesToAddEntitiesToExisting() - { - var loads = new List<(uint, LandblockStreamJobKind)>(); - var unloads = new List(); - var state = new GpuWorldState(); - - // Pre-load a far-tier-style LB record (terrain only, no entities). - // Id must be in canonical form (low 16 bits = 0xFFFF) since - // AddEntitiesToExistingLandblock canonicalizes incoming ids. - uint lbId = 0x3232FFFFu; - var lb = new LoadedLandblock(lbId, Heightmap: null!, Entities: System.Array.Empty()); - state.AddLandblock(lb); - Assert.Empty(state.Entities); - - // Streamer pushes a Promoted result carrying the entity layer. - var promoted = new LandblockStreamResult.Promoted( - lbId, - new[] { new WorldEntity { - Id = 7, SourceGfxObjOrSetupId = 0, - Position = System.Numerics.Vector3.Zero, - Rotation = System.Numerics.Quaternion.Identity, - MeshRefs = System.Array.Empty() } }); - var queue = new Queue(); - queue.Enqueue(promoted); - - var ctrl = new StreamingController( - enqueueLoad: (id, kind) => loads.Add((id, kind)), - enqueueUnload: unloads.Add, - drainCompletions: max => - { - var batch = new List(); - while (batch.Count < max && queue.Count > 0) batch.Add(queue.Dequeue()); - return batch; - }, - applyTerrain: (_, _) => { }, - state: state, - nearRadius: 2, - farRadius: 2); - - ctrl.Tick(50, 50); // drains the Promoted result - - // Promoted routes to AddEntitiesToExistingLandblock β€” the entity is now - // merged into the existing LB record. - Assert.Equal(1, state.Entities.Count); - Assert.Equal(7u, state.Entities[0].Id); - } -} diff --git a/tests/AcDream.Core.Tests/Streaming/StreamingRegionTests.cs b/tests/AcDream.Core.Tests/Streaming/StreamingRegionTests.cs index 899291e..741ea2b 100644 --- a/tests/AcDream.Core.Tests/Streaming/StreamingRegionTests.cs +++ b/tests/AcDream.Core.Tests/Streaming/StreamingRegionTests.cs @@ -36,7 +36,7 @@ public class StreamingRegionTests { var region = new StreamingRegion(cx: 50, cy: 50, radius: 2); - var diff = region.RecenterToSingleTier(50, 50); + var diff = region.RecenterTo(50, 50); Assert.Empty(diff.ToLoad); Assert.Empty(diff.ToUnload); @@ -52,7 +52,7 @@ public class StreamingRegionTests // the radius+2 threshold, so it stays loaded (hysteresis keeps radius+2). var region = new StreamingRegion(cx: 50, cy: 50, radius: 2); - var diff = region.RecenterToSingleTier(51, 50); + var diff = region.RecenterTo(51, 50); Assert.Equal(5, diff.ToLoad.Count); Assert.Empty(diff.ToUnload); @@ -71,7 +71,7 @@ public class StreamingRegionTests // x=48 is now 5 away, > radius+2 = 4 β†’ unload. x=49 is 4 away, not > 4 β†’ keep. x=50 is 3 away, not > 4 β†’ keep. var region = new StreamingRegion(cx: 50, cy: 50, radius: 2); - var diff = region.RecenterToSingleTier(53, 50); + var diff = region.RecenterTo(53, 50); Assert.Equal(15, diff.ToLoad.Count); Assert.Equal(5, diff.ToUnload.Count); @@ -82,7 +82,7 @@ public class StreamingRegionTests { var region = new StreamingRegion(cx: 50, cy: 50, radius: 2); - var diff = region.RecenterToSingleTier(200, 200); + var diff = region.RecenterTo(200, 200); Assert.Equal(25, diff.ToLoad.Count); Assert.Equal(25, diff.ToUnload.Count); diff --git a/tests/AcDream.Core.Tests/Streaming/StreamingRegionTwoTierTests.cs b/tests/AcDream.Core.Tests/Streaming/StreamingRegionTwoTierTests.cs deleted file mode 100644 index 5891245..0000000 --- a/tests/AcDream.Core.Tests/Streaming/StreamingRegionTwoTierTests.cs +++ /dev/null @@ -1,166 +0,0 @@ -using AcDream.App.Streaming; -using Xunit; - -namespace AcDream.Core.Tests.Streaming; - -public class StreamingRegionTwoTierTests -{ - [Fact] - public void Constructor_TwoRadii_ExposesNearAndFarRadii() - { - var region = new StreamingRegion(centerX: 100, centerY: 100, nearRadius: 4, farRadius: 12); - - Assert.Equal(4, region.NearRadius); - Assert.Equal(12, region.FarRadius); - Assert.Equal(100, region.CenterX); - Assert.Equal(100, region.CenterY); - // Radius (used by existing single-radius hysteresis math) must alias to - // FarRadius β€” the outer ring drives "everything currently loaded" bookkeeping. - // If a future change mistakenly aliases Radius to NearRadius, hysteresis - // becomes (NearRadius+2) for the far-tier unload, which is wrong. - Assert.Equal(region.FarRadius, region.Radius); - } - - [Fact] - public void ComputeFirstTickDiff_FirstTick_SplitsLoadIntoNearAndFar() - { - // near=1, far=3 β†’ near window is 3Γ—3=9, far window is 7Γ—7-3Γ—3=40 LBs. - var region = new StreamingRegion(centerX: 100, centerY: 100, nearRadius: 1, farRadius: 3); - var diff = region.ComputeFirstTickDiff(); - - Assert.Equal(9, diff.ToLoadNear.Count); - Assert.Equal(40, diff.ToLoadFar.Count); - Assert.Empty(diff.ToPromote); - Assert.Empty(diff.ToDemote); - Assert.Empty(diff.ToUnload); - } - - [Fact] - public void RecenterTo_PlayerWalks_NullToFar_AppearsInToLoadFar() - { - var region = new StreamingRegion(centerX: 100, centerY: 100, nearRadius: 1, farRadius: 3); - _ = region.ComputeFirstTickDiff(); - region.MarkResidentFromBootstrap(); - - // Walk one LB east β€” center (100,100) β†’ (101,100). LB column at lbX=104 - // (relative dx=+3 from new center) enters the far window from null. - var diff = region.RecenterTo(newCx: 101, newCy: 100); - - foreach (var y in new[] { 97, 98, 99, 100, 101, 102, 103 }) - { - var id = StreamingRegion.EncodeLandblockIdForTest(104, y); - Assert.Contains(id, diff.ToLoadFar); - } - Assert.Empty(diff.ToLoadNear); - // The 3 LBs at x=102, y in {99,100,101} were Far from old center - // (distance 2) and are now Near from new center (distance ≀1). - // They should land in ToPromote. - Assert.Equal(3, diff.ToPromote.Count); - // All resident LBs from the old window are within hysteresis of - // the new center (max distance 4 ≀ FarRadius+2=5), so nothing unloads. - Assert.Empty(diff.ToUnload); - } - - [Fact] - public void RecenterTo_PlayerWalks_FarToNear_AppearsInToPromote() - { - var region = new StreamingRegion(centerX: 100, centerY: 100, nearRadius: 1, farRadius: 3); - _ = region.ComputeFirstTickDiff(); - region.MarkResidentFromBootstrap(); - - // Walk 2 east β€” center (102, 100). LB (102, 100) was at distance 2 (Far) - // from (100,100); now at distance 0 β†’ Near. That's a Promote. - var diff = region.RecenterTo(newCx: 102, newCy: 100); - - var promotedId = StreamingRegion.EncodeLandblockIdForTest(102, 100); - Assert.Contains(promotedId, diff.ToPromote); - Assert.DoesNotContain(promotedId, diff.ToLoadNear); - Assert.DoesNotContain(promotedId, diff.ToLoadFar); - } - - [Fact] - public void RecenterTo_PlayerTeleports_NullToNear_AppearsInToLoadNear() - { - var region = new StreamingRegion(centerX: 100, centerY: 100, nearRadius: 1, farRadius: 3); - _ = region.ComputeFirstTickDiff(); - region.MarkResidentFromBootstrap(); - - // Teleport to (200, 200) β€” entirely new region. - var diff = region.RecenterTo(newCx: 200, newCy: 200); - - Assert.Equal(9, diff.ToLoadNear.Count); - Assert.Equal(40, diff.ToLoadFar.Count); - Assert.Empty(diff.ToPromote); - } - - [Fact] - public void RecenterTo_NearToFar_DemoteOnlyAfterHysteresis() - { - // near=2, far=4 β†’ near hysteresis threshold = 4. - var region = new StreamingRegion(centerX: 100, centerY: 100, nearRadius: 2, farRadius: 4); - _ = region.ComputeFirstTickDiff(); - region.MarkResidentFromBootstrap(); - - // LB (100,100) was Near. Walk 3 east β†’ distance 3 > NearRadius=2 but ≀ 4. No demote yet. - var diff1 = region.RecenterTo(newCx: 103, newCy: 100); - var lb100 = StreamingRegion.EncodeLandblockIdForTest(100, 100); - Assert.DoesNotContain(lb100, diff1.ToDemote); - - // Walk 2 more east β†’ distance 5 > 4. Demote. - var diff2 = region.RecenterTo(newCx: 105, newCy: 100); - Assert.Contains(lb100, diff2.ToDemote); - } - - [Fact] - public void RecenterTo_FarToNull_UnloadOnlyAfterHysteresis() - { - var region = new StreamingRegion(centerX: 100, centerY: 100, nearRadius: 1, farRadius: 3); - _ = region.ComputeFirstTickDiff(); - region.MarkResidentFromBootstrap(); - - // LB (97, 100) was at distance 3 (Far). Walk 1 east β†’ distance 4. ≀ FarRadius+2=5. - var diff1 = region.RecenterTo(newCx: 101, newCy: 100); - var lb97 = StreamingRegion.EncodeLandblockIdForTest(97, 100); - Assert.DoesNotContain(lb97, diff1.ToUnload); - - // Walk 2 more east β†’ distance 6 > 5. Unload. - var diff2 = region.RecenterTo(newCx: 103, newCy: 100); - Assert.Contains(lb97, diff2.ToUnload); - } - - [Fact] - public void RecenterTo_PlayerOscillates_NoThrashWithinHysteresis() - { - // Start the region centered on (103,100) so the oscillation - // between (102,100) and (103,100) never crosses a hysteresis boundary. - // NearRadius=2, farRadius=4 β†’ nearUnloadThreshold=4. - // Chebyshev distance from (102,100) or (103,100) to any LB in the - // initial 9Γ—9 window of (103,100) is ≀ NearRadius+2=4 for all LBs - // in the near zone, so no demotes should fire during pure oscillation. - var region = new StreamingRegion(centerX: 103, centerY: 100, nearRadius: 2, farRadius: 4); - _ = region.ComputeFirstTickDiff(); - region.MarkResidentFromBootstrap(); - - // Bounce between (103,100) and (102,100). All resident LBs stay - // within the hysteresis window β€” no demotes expected. - int totalDemotes = 0; - int totalPromotes = 0; - for (int i = 0; i < 5; i++) - { - var d1 = region.RecenterTo(102, 100); - totalDemotes += d1.ToDemote.Count; - totalPromotes += d1.ToPromote.Count; - var d2 = region.RecenterTo(103, 100); - totalDemotes += d2.ToDemote.Count; - totalPromotes += d2.ToPromote.Count; - } - - // The first step from (103,100) to (102,100) legitimately promotes the - // x=100 near-column (5 LBs) that were Far from (103) into Near. After - // that initial settle they stay Near for all subsequent oscillations. - // So the ceiling is 5 promotes total (not per oscillation). - Assert.Equal(0, totalDemotes); - Assert.True(totalPromotes <= 5, - $"Expected ≀5 promotes across 5 oscillations; got {totalPromotes}"); - } -} diff --git a/tests/AcDream.Core.Tests/Terrain/SplitFormulaDivergenceTest.cs b/tests/AcDream.Core.Tests/Terrain/SplitFormulaDivergenceTest.cs deleted file mode 100644 index feaa28f..0000000 --- a/tests/AcDream.Core.Tests/Terrain/SplitFormulaDivergenceTest.cs +++ /dev/null @@ -1,168 +0,0 @@ -using AcDream.Core.Terrain; -using Xunit; -using Xunit.Abstractions; -using WbTerrainUtils = WorldBuilder.Shared.Modules.Landscape.Lib.TerrainUtils; -using WbCellSplitDirection = WorldBuilder.Shared.Modules.Landscape.Models.CellSplitDirection; - -namespace AcDream.Core.Tests.Terrain; - -/// -/// Phase N.5b data-collection test: quantifies how often WB's -/// TerrainUtils.CalculateSplitDirection disagrees with acdream's -/// TerrainBlending.CalculateSplitDirection (which retail uses -/// per CLandBlockStruct::ConstructPolygons at retail address -/// 00531d10; named-retail decomp lines 316042-316144 contain -/// the exact constants 0x0CCAC033 / 0x6C1AC587 / 0x421BE3BD / -/// 0x519B8F25). -/// -/// Sweeps every (lbX, lbY, cellX, cellY) tuple in the world map -/// (255 x 255 landblocks x 64 cells = ~4.16M cells) and reports the -/// disagreement rate, per-landblock worst case, and a few named -/// representative landblocks. The number drives the Path A/B/C -/// decision in the N.5b brainstorm: -/// - Low disagreement <5% : Path A's risk is bounded -/// - Medium 5-20% : Path B (fork-patch WB) preferred -/// - High >20% : Path B/C strongly preferred -/// -public class SplitFormulaDivergenceTest -{ - private readonly ITestOutputHelper _out; - - public SplitFormulaDivergenceTest(ITestOutputHelper output) => _out = output; - - [Fact] - public void Quantify_RetailVsWb_DivergenceRate() - { - // Two divergence flavors are tracked simultaneously: - // - // rawDisagree : retail-enum != wb-enum (pure formula output) - // diagonalDisagree: retail-actually-paints-diagonal != - // wb-actually-paints-diagonal (effective geometry) - // - // The two differ because the enums are SEMANTICALLY INVERTED: - // - acdream `CellSplitDirection.SWtoNE` -> renderer paints BL->TR - // (SW-NE diagonal). Matches retail per AC2D Landblocks.cpp:400-412 - // where FSplitNESW=true wraps a TRIANGLE_FAN [BL, BR, TR, TL] = - // diagonal BL-TR. - // - WB `CellSplitDirection.SWtoNE` -> WB's TerrainGeometryGenerator - // emits triangles {BL,TL,BR}+{BR,TL,TR} which share the BR-TL - // diagonal (SE-NW direction). The enum name is misleading; what - // WB actually draws is the OTHER diagonal. - // - // So the question "would WB's pipeline produce the same diagonals as - // retail's pipeline?" is answered by `diagonalDisagree`, not - // `rawDisagree`. If diagonalDisagree is near 0%, WB's formula + - // renderer happen to compose into a correct pipeline (despite the - // confusing labels). If diagonalDisagree is ~50%, the two pipelines - // truly diverge and Path A would visibly break terrain on every - // landblock. - - const int lbCount = 255; - const int cellsPerSide = 8; - long totalCells = 0; - long rawDisagree = 0; - long diagonalDisagree = 0; - - int worstLbDiag = 0; - uint worstLbX = 0, worstLbY = 0; - int bestLbDiag = 64; - uint bestLbX = 0, bestLbY = 0; - - for (uint lbX = 0; lbX < lbCount; lbX++) - for (uint lbY = 0; lbY < lbCount; lbY++) - { - int lbDiagDisagree = 0; - for (uint cx = 0; cx < cellsPerSide; cx++) - for (uint cy = 0; cy < cellsPerSide; cy++) - { - bool retailEnumSWtoNE = - TerrainBlending.CalculateSplitDirection(lbX, cx, lbY, cy) - == CellSplitDirection.SWtoNE; - bool wbEnumSWtoNE = - WbTerrainUtils.CalculateSplitDirection(lbX, cx, lbY, cy) - == WbCellSplitDirection.SWtoNE; - - // What diagonal each pipeline actually paints. - bool retailPaintsBLtoTR = retailEnumSWtoNE; // direct mapping - bool wbPaintsBLtoTR = !wbEnumSWtoNE; // inverted mapping - - totalCells++; - if (retailEnumSWtoNE != wbEnumSWtoNE) rawDisagree++; - if (retailPaintsBLtoTR != wbPaintsBLtoTR) - { - diagonalDisagree++; - lbDiagDisagree++; - } - } - - if (lbDiagDisagree > worstLbDiag) - { - worstLbDiag = lbDiagDisagree; - worstLbX = lbX; - worstLbY = lbY; - } - if (lbDiagDisagree < bestLbDiag) - { - bestLbDiag = lbDiagDisagree; - bestLbX = lbX; - bestLbY = lbY; - } - } - - double rawPct = 100.0 * rawDisagree / totalCells; - double diagPct = 100.0 * diagonalDisagree / totalCells; - - _out.WriteLine($"=== Phase N.5b β€” terrain split formula divergence ==="); - _out.WriteLine($"Sweep: {lbCount}x{lbCount} landblocks, {cellsPerSide*cellsPerSide} cells each"); - _out.WriteLine($"Total cells: {totalCells:N0}"); - _out.WriteLine(""); - _out.WriteLine($"RAW enum-output disagreement : {rawDisagree,12:N0} ({rawPct:F2}%)"); - _out.WriteLine($" (compares retail-enum vs wb-enum, NOT what each system actually draws)"); - _out.WriteLine(""); - _out.WriteLine($"DIAGONAL-actually-painted disagreement: {diagonalDisagree,12:N0} ({diagPct:F2}%)"); - _out.WriteLine($" (compares retail-paints-BL->TR vs wb-paints-BL->TR; this is the"); - _out.WriteLine($" number that determines whether Path A visibly works)"); - _out.WriteLine(""); - _out.WriteLine($"Worst landblock (diagonal): 0x{worstLbX:X2}{worstLbY:X2} disagrees on {worstLbDiag}/64 cells ({100.0*worstLbDiag/64:F1}%)"); - _out.WriteLine($"Best landblock (diagonal): 0x{bestLbX:X2}{bestLbY:X2} disagrees on {bestLbDiag}/64 cells ({100.0*bestLbDiag/64:F1}%)"); - - // Specific landblocks of interest (per N.5b handoff representative set). - var representative = new (string name, uint lbX, uint lbY)[] - { - ("Holtburg town", 0xA9, 0xB0), - ("Holtburg LB 0xA9B1", 0xA9, 0xB1), - ("Foundry-area", 0x80, 0x80), - ("Cragstone", 0xCB, 0x99), - ("Direlands sample", 0xC0, 0x40), - ("MapOrigin 0x0000", 0x00, 0x00), - ("MapCorner 0xFEFE", 0xFE, 0xFE), - ("Mid-map 0x7F7F", 0x7F, 0x7F), - ("Subway dungeon LB 0x0185 outdoor part", 0x01, 0x85), - }; - - _out.WriteLine(""); - _out.WriteLine("Representative landblocks (diagonal-actually-painted disagreement):"); - foreach (var (name, lbX, lbY) in representative) - { - int dis = 0; - for (uint cx = 0; cx < 8; cx++) - for (uint cy = 0; cy < 8; cy++) - { - bool retailEnum = TerrainBlending.CalculateSplitDirection(lbX, cx, lbY, cy) == CellSplitDirection.SWtoNE; - bool wbEnum = WbTerrainUtils.CalculateSplitDirection(lbX, cx, lbY, cy) == WbCellSplitDirection.SWtoNE; - bool retailPaintsBLtoTR = retailEnum; - bool wbPaintsBLtoTR = !wbEnum; - if (retailPaintsBLtoTR != wbPaintsBLtoTR) dis++; - } - _out.WriteLine($" 0x{lbX:X2}{lbY:X2} {dis,2}/64 cells disagree ({100.0*dis/64:F1}%) {name}"); - } - - // Soft-floor on the DIAGONAL comparison: if diagPct is near 0% the - // formulas are equivalent post-inversion (Path A would just work - // visually; the only "bug" is enum naming). If diagPct is well - // above 0%, Path A truly breaks terrain. - // Soft-ceiling: an inversion of inversion shouldn't push past ~70%. - Assert.True(diagPct >= 0 && diagPct <= 100, - $"Sanity: diagonal disagreement out of range (rate={diagPct:F2}%)"); - } -} diff --git a/tests/AcDream.Core.Tests/Terrain/TerrainModernConformanceTests.cs b/tests/AcDream.Core.Tests/Terrain/TerrainModernConformanceTests.cs deleted file mode 100644 index c02f7cc..0000000 --- a/tests/AcDream.Core.Tests/Terrain/TerrainModernConformanceTests.cs +++ /dev/null @@ -1,189 +0,0 @@ -using System.Collections.Generic; -using System.IO; -using AcDream.Core.Physics; -using AcDream.Core.Terrain; -using DatReaderWriter; -using DatReaderWriter.DBObjs; -using DatReaderWriter.Options; -using Xunit; -using Xunit.Abstractions; -using Env = System.Environment; - -namespace AcDream.Core.Tests.Terrain; - -/// -/// Phase N.5b Z-conformance sentinel: proves that the visual terrain mesh -/// produced by agrees with the physics-side -/// at arbitrary (X, Y) -/// within 1 mm. This is the exact bug class issue #51 names β€” if a future -/// refactor silently changes formula or vertex layout in either path, -/// this test fires before the player floats above (or sinks below) the -/// visible ground. -/// -/// The test is dat-data-dependent. If ACDREAM_DAT_DIR isn't set or -/// the directory doesn't exist, the test logs a SKIP and passes β€” keeps CI -/// (no dat data) green while still firing locally on every developer run. -/// -public class TerrainModernConformanceTests -{ - private readonly ITestOutputHelper _out; - - public TerrainModernConformanceTests(ITestOutputHelper output) => _out = output; - - private static readonly (string name, uint lbX, uint lbY)[] RepresentativeLandblocks = - { - ("Holtburg flat 0xA9B0", 0xA9, 0xB0), - ("Holtburg sloped 0xA9B1", 0xA9, 0xB1), - ("Foundry-area 0x8080", 0x80, 0x80), - ("Cragstone 0xCB99", 0xCB, 0x99), - ("Direlands sample 0xC040", 0xC0, 0x40), - ("MapOrigin 0x0000", 0x00, 0x00), - ("Mid-map 0x7F7F", 0x7F, 0x7F), - ("MapCorner 0xFEFE", 0xFE, 0xFE), - ("Subway outdoor 0x0185", 0x01, 0x85), - ("North continent 0x4D96", 0x4D, 0x96), // worst-case landblock from SplitFormulaDivergenceTest - }; - - [Fact] - public void VisualMeshZ_AgreesWith_PhysicsZ_WithinOneMillimeter() - { - var datDir = Env.GetEnvironmentVariable("ACDREAM_DAT_DIR") - ?? Path.Combine(Env.GetFolderPath(Env.SpecialFolder.UserProfile), - "Documents", "Asheron's Call"); - if (!Directory.Exists(datDir)) - { - _out.WriteLine($"SKIP: dat directory not found at {datDir}"); - return; - } - - using var dats = new DatCollection(datDir, DatAccessType.Read); - var region = dats.Get(0x13000000u); - Assert.NotNull(region); - var heightTable = region.LandDefs.LandHeightTable; - Assert.NotNull(heightTable); - Assert.True(heightTable.Length >= 256, "heightTable must have at least 256 entries"); - - // Empty blending context β€” the conformance test only cares about - // vertex Z values, never the surface info / atlas layers. An empty - // dictionary + empty arrays are sufficient for BuildSurface to - // resolve every cell to a "base only" surface (the Z values come - // from the heightmap, not from the surface info). - var ctx = new TerrainBlendingContext( - TerrainTypeToLayer: new Dictionary(), - RoadLayer: SurfaceInfo.None, - CornerAlphaLayers: Array.Empty(), - SideAlphaLayers: Array.Empty(), - RoadAlphaLayers: Array.Empty(), - CornerAlphaTCodes: Array.Empty(), - SideAlphaTCodes: Array.Empty(), - RoadAlphaRCodes: Array.Empty()); - - long totalSamples = 0; - long totalLandblocksTested = 0; - double maxDelta = 0; - (string name, uint lbX, uint lbY, float lx, float ly, float meshZ, float physicsZ) worstCase = default; - - // Fixed seed for reproducible sample distribution. If a future change - // makes the test fire, the same (lx, ly) sequence reproduces the - // exact failing point on a follow-up run. - var rng = new Random(42); - - foreach (var (name, lbX, lbY) in RepresentativeLandblocks) - { - uint landblockId = (lbX << 24) | (lbY << 16) | 0xFFFFu; - var landblock = dats.Get(landblockId); - if (landblock is null) - { - _out.WriteLine($" skipped {name}: dat not found (probably water-only)"); - continue; - } - totalLandblocksTested++; - - var surfaceCache = new Dictionary(); - var meshData = LandblockMesh.Build(landblock, lbX, lbY, heightTable, ctx, surfaceCache); - - // Sample 100 (localX, localY) points uniformly in [0, 191.975]. - // The physics path clamps fx = localX/24 to (CellsPerSide - 0.001f) - // = 7.999, which corresponds to localX <= 7.999 * 24 = 191.976. - // Sampling beyond that boundary makes physics compute Z at the - // clamped position while the mesh sampler uses the actual - // position β€” a difference of up to 23 mm at the upper edge, - // which on a steep slope would falsely trip the 1 mm sentinel. - // Stay strictly below the clamp boundary so both oracles - // compute Z at the same (cellX, tx). - for (int s = 0; s < 100; s++) - { - float lx = (float)rng.NextDouble() * 191.975f; - float ly = (float)rng.NextDouble() * 191.975f; - - float meshZ = SampleMeshZ(meshData, lx, ly); - float physicsZ = TerrainSurface.SampleZFromHeightmap( - landblock.Height, heightTable, lbX, lbY, lx, ly); - - double delta = Math.Abs(meshZ - physicsZ); - if (delta > maxDelta) - { - maxDelta = delta; - worstCase = (name, lbX, lbY, lx, ly, meshZ, physicsZ); - } - totalSamples++; - Assert.True(delta < 0.001, - $"Mesh Z disagrees with physics Z at lb=0x{lbX:X2}{lbY:X2} ({name}) " + - $"local=({lx:F2},{ly:F2}): meshZ={meshZ:F4} physicsZ={physicsZ:F4} delta={delta:F4}m"); - } - } - - _out.WriteLine($"=== Phase N.5b conformance sweep ==="); - _out.WriteLine($"Landblocks tested: {totalLandblocksTested}/{RepresentativeLandblocks.Length}"); - _out.WriteLine($"Total samples: {totalSamples}"); - _out.WriteLine($"Max |delta|: {maxDelta * 1000:F4} mm (tolerance: 1.0 mm)"); - if (totalSamples > 0) - _out.WriteLine($"Worst case: {worstCase.name} local=({worstCase.lx:F2},{worstCase.ly:F2}) " + - $"meshZ={worstCase.meshZ:F4} physicsZ={worstCase.physicsZ:F4}"); - - Assert.True(totalLandblocksTested >= 5, - $"Expected at least 5 representative landblocks loadable; got {totalLandblocksTested}."); - } - - /// - /// Sample the mesh's triangle-interpolated Z at (localX, localY). Walks - /// the mesh's triangles (3 indices each), tests point-in-triangle in 2D, - /// and barycentric-interpolates Z from the matching triangle's three Zs. - /// - /// The mesh has 128 triangles per landblock (64 cells Γ— 2). Every (lx, ly) - /// in [0, 192) lies in exactly one triangle (or on a shared edge β€” the - /// epsilon makes either side acceptable since they agree at the seam). - /// - private static float SampleMeshZ(LandblockMeshData mesh, float lx, float ly) - { - for (int triBase = 0; triBase < mesh.Indices.Length; triBase += 3) - { - var v0 = mesh.Vertices[mesh.Indices[triBase + 0]]; - var v1 = mesh.Vertices[mesh.Indices[triBase + 1]]; - var v2 = mesh.Vertices[mesh.Indices[triBase + 2]]; - - // Barycentric coords for (lx, ly) wrt triangle v0/v1/v2 in 2D. - float denom = (v1.Position.Y - v2.Position.Y) * (v0.Position.X - v2.Position.X) - + (v2.Position.X - v1.Position.X) * (v0.Position.Y - v2.Position.Y); - if (Math.Abs(denom) < 1e-9f) continue; - - float a = ((v1.Position.Y - v2.Position.Y) * (lx - v2.Position.X) - + (v2.Position.X - v1.Position.X) * (ly - v2.Position.Y)) / denom; - float b = ((v2.Position.Y - v0.Position.Y) * (lx - v2.Position.X) - + (v0.Position.X - v2.Position.X) * (ly - v2.Position.Y)) / denom; - float c = 1f - a - b; - - // Inside test with epsilon for boundary stability β€” points that - // land exactly on a shared edge between two triangles still - // resolve, picking whichever the loop hits first (Z agrees on - // the seam either way). - const float eps = 1e-4f; - if (a >= -eps && b >= -eps && c >= -eps) - return a * v0.Position.Z + b * v1.Position.Z + c * v2.Position.Z; - } - - // Should not happen for valid mesh + in-bounds (lx, ly). - throw new InvalidOperationException( - $"No triangle found containing local=({lx:F2},{ly:F2}); mesh has {mesh.Indices.Length / 3} triangles."); - } -} diff --git a/tests/AcDream.Core.Tests/Terrain/TerrainSlotAllocatorTests.cs b/tests/AcDream.Core.Tests/Terrain/TerrainSlotAllocatorTests.cs deleted file mode 100644 index aaa894c..0000000 --- a/tests/AcDream.Core.Tests/Terrain/TerrainSlotAllocatorTests.cs +++ /dev/null @@ -1,88 +0,0 @@ -using AcDream.Core.Terrain; -using Xunit; - -namespace AcDream.Core.Tests.Terrain; - -public class TerrainSlotAllocatorTests -{ - [Fact] - public void Allocate_FromFreshAllocator_ReturnsZero() - { - var alloc = new TerrainSlotAllocator(initialCapacity: 8); - Assert.Equal(0, alloc.Allocate(out _)); - } - - [Fact] - public void Allocate_TwoTimes_ReturnsZeroThenOne() - { - var alloc = new TerrainSlotAllocator(initialCapacity: 8); - Assert.Equal(0, alloc.Allocate(out _)); - Assert.Equal(1, alloc.Allocate(out _)); - } - - [Fact] - public void FreeThenAllocate_ReusesFreedSlot() - { - var alloc = new TerrainSlotAllocator(initialCapacity: 8); - var s0 = alloc.Allocate(out _); - var s1 = alloc.Allocate(out _); - alloc.Free(s0); - Assert.Equal(s0, alloc.Allocate(out _)); - } - - [Fact] - public void FreeOrderedFreshAllocs_ReturnsInFifoOrder() - { - var alloc = new TerrainSlotAllocator(initialCapacity: 8); - var s0 = alloc.Allocate(out _); - var s1 = alloc.Allocate(out _); - var s2 = alloc.Allocate(out _); - alloc.Free(s0); - alloc.Free(s2); - Assert.Equal(s0, alloc.Allocate(out _)); - Assert.Equal(s2, alloc.Allocate(out _)); - } - - [Fact] - public void Allocate_BeyondInitialCapacity_SignalsNeedsGrow() - { - var alloc = new TerrainSlotAllocator(initialCapacity: 2); - alloc.Allocate(out var grow0); - alloc.Allocate(out var grow1); - alloc.Allocate(out var grow2); - Assert.False(grow0); - Assert.False(grow1); - Assert.True(grow2); - } - - [Fact] - public void GrowTo_DoublesCapacityCorrectly() - { - var alloc = new TerrainSlotAllocator(initialCapacity: 4); - alloc.GrowTo(8); - Assert.Equal(8, alloc.Capacity); - alloc.GrowTo(64); - Assert.Equal(64, alloc.Capacity); - } - - [Fact] - public void LoadedCount_TracksAllocAndFree() - { - var alloc = new TerrainSlotAllocator(initialCapacity: 8); - Assert.Equal(0, alloc.LoadedCount); - var s0 = alloc.Allocate(out _); - var s1 = alloc.Allocate(out _); - Assert.Equal(2, alloc.LoadedCount); - alloc.Free(s0); - Assert.Equal(1, alloc.LoadedCount); - } - - [Fact] - public void Free_TwiceForSameSlot_Throws() - { - var alloc = new TerrainSlotAllocator(initialCapacity: 8); - var s0 = alloc.Allocate(out _); - alloc.Free(s0); - Assert.Throws(() => alloc.Free(s0)); - } -} diff --git a/tests/AcDream.Core.Tests/Textures/SurfaceDecoderTests.cs b/tests/AcDream.Core.Tests/Textures/SurfaceDecoderTests.cs index 7a440e0..8110bfc 100644 --- a/tests/AcDream.Core.Tests/Textures/SurfaceDecoderTests.cs +++ b/tests/AcDream.Core.Tests/Textures/SurfaceDecoderTests.cs @@ -56,10 +56,12 @@ public class SurfaceDecoderTests } [Fact] - public void Decode_A8_NonAdditive_ProducesWhitePlusAlpha() + public void Decode_A8_ExpandsSingleByteToRgbaWithAlphaInAllChannels() { - // Default (isAdditive: false) = WB FillA8 semantics: R=G=B=255, A=val. - // Used for non-additive entity surfaces where A8 is a pure alpha channel. + // PFID_A8 is single-byte-per-pixel alpha. AC terrain blending alpha maps + // are stored this way. WorldBuilder's GetExpandedAlphaTexture replicates + // the byte into all four RGBA channels so fragment shaders can read the + // blend value from any channel (convention: the alpha channel). var src = new byte[] { 0x00, 0x40, 0x80, 0xFF }; // 2x2 image var rs = new RenderSurface { @@ -74,34 +76,7 @@ public class SurfaceDecoderTests Assert.Equal(2, decoded.Width); Assert.Equal(2, decoded.Height); Assert.Equal(16, decoded.Rgba8.Length); - // Each input byte expands to (255, 255, 255, val) β€” white with varying alpha - Assert.Equal(new byte[] - { - 255, 255, 255, 0x00, - 255, 255, 255, 0x40, - 255, 255, 255, 0x80, - 255, 255, 255, 0xFF, - }, decoded.Rgba8); - } - - [Fact] - public void Decode_A8_Additive_ReplicatesByteToAllChannels() - { - // isAdditive=true = WB FillA8Additive semantics: R=G=B=A=val. - // Used for terrain blending alpha masks (TerrainAtlas always passes isAdditive:true). - var src = new byte[] { 0x00, 0x40, 0x80, 0xFF }; // 2x2 image - var rs = new RenderSurface - { - Width = 2, - Height = 2, - Format = PixelFormat.PFID_A8, - SourceData = src, - }; - - var decoded = SurfaceDecoder.DecodeRenderSurface(rs, palette: null, isClipMap: false, isAdditive: true); - - Assert.Equal(16, decoded.Rgba8.Length); - // Each input byte fans out to all four channels + // Each input byte expands to (b, b, b, b) in RGBA output Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00, @@ -117,7 +92,7 @@ public class SurfaceDecoderTests // PFID_CUSTOM_LSCAPE_ALPHA (0xF4) is AC's custom format for terrain // blending alpha maps. Pixel layout is identical to PFID_A8 β€” one // byte of alpha per pixel β€” so the decoder routes both through the - // same DecodeA8 implementation. Default (isAdditive:false) β†’ R=G=B=255, A=val. + // same DecodeA8 implementation. var src = new byte[] { 0x10, 0x20, 0x30, 0x40 }; // 2x2 var rs = new RenderSurface { @@ -132,10 +107,10 @@ public class SurfaceDecoderTests Assert.Equal(16, decoded.Rgba8.Length); Assert.Equal(new byte[] { - 255, 255, 255, 0x10, - 255, 255, 255, 0x20, - 255, 255, 255, 0x30, - 255, 255, 255, 0x40, + 0x10, 0x10, 0x10, 0x10, + 0x20, 0x20, 0x20, 0x20, + 0x30, 0x30, 0x30, 0x30, + 0x40, 0x40, 0x40, 0x40, }, decoded.Rgba8); } diff --git a/tests/AcDream.Core.Tests/Textures/TextureDecodeConformanceTests.cs b/tests/AcDream.Core.Tests/Textures/TextureDecodeConformanceTests.cs deleted file mode 100644 index b7fb62a..0000000 --- a/tests/AcDream.Core.Tests/Textures/TextureDecodeConformanceTests.cs +++ /dev/null @@ -1,369 +0,0 @@ -using Chorizite.OpenGLSDLBackend.Lib; -using DatReaderWriter.DBObjs; -using DatReaderWriter.Types; - -namespace AcDream.Core.Tests.Textures; - -/// -/// Conformance tests proving byte-identical output between our hand-rolled -/// SurfaceDecoder paths and WorldBuilder's TextureHelpers.Fill* methods. -/// These tests run BEFORE any substitution β€” they prove equivalence first. -/// If a test fails, the formats diverge and that's a real finding. -/// -public class TextureDecodeConformanceTests -{ - // ---- helpers --------------------------------------------------------------- - - private static Palette MakePalette(params ColorARGB[] colors) - { - var pal = new Palette(); - foreach (var c in colors) - pal.Colors.Add(c); - return pal; - } - - private static ColorARGB Rgba(byte r, byte g, byte b, byte a = 0xFF) - => new ColorARGB { Red = r, Green = g, Blue = b, Alpha = a }; - - // Inline our current DecodeIndex16 logic for the conformance baseline. - private static byte[] OurDecodeIndex16(byte[] src, Palette palette, int width, int height, bool isClipMap = false) - { - var rgba = new byte[width * height * 4]; - int paletteMax = palette.Colors.Count - 1; - for (int i = 0; i < width * height; i++) - { - int s = i * 2; - ushort idx = (ushort)(src[s] | (src[s + 1] << 8)); - if (idx > paletteMax) idx = 0; - var c = palette.Colors[idx]; - int d = i * 4; - if (isClipMap && idx < 8) - { - rgba[d + 0] = 0; - rgba[d + 1] = 0; - rgba[d + 2] = 0; - rgba[d + 3] = 0; - } - else - { - rgba[d + 0] = c.Red; - rgba[d + 1] = c.Green; - rgba[d + 2] = c.Blue; - rgba[d + 3] = c.Alpha; - } - } - return rgba; - } - - // Inline our current DecodeP8 logic. - private static byte[] OurDecodeP8(byte[] src, Palette palette, int width, int height, bool isClipMap = false) - { - var rgba = new byte[width * height * 4]; - int paletteMax = palette.Colors.Count - 1; - for (int i = 0; i < width * height; i++) - { - int idx = src[i]; - if (idx > paletteMax) idx = 0; - var c = palette.Colors[idx]; - int d = i * 4; - if (isClipMap && idx < 8) - { - rgba[d + 0] = 0; - rgba[d + 1] = 0; - rgba[d + 2] = 0; - rgba[d + 3] = 0; - } - else - { - rgba[d + 0] = c.Red; - rgba[d + 1] = c.Green; - rgba[d + 2] = c.Blue; - rgba[d + 3] = c.Alpha; - } - } - return rgba; - } - - // Inline our current DecodeA8R8G8B8 logic (BGRA on-disk β†’ RGBA). - private static byte[] OurDecodeA8R8G8B8(byte[] src, int width, int height) - { - var rgba = new byte[width * height * 4]; - for (int i = 0; i < width * height; i++) - { - int s = i * 4; - rgba[s + 0] = src[s + 2]; // R - rgba[s + 1] = src[s + 1]; // G - rgba[s + 2] = src[s + 0]; // B - rgba[s + 3] = src[s + 3]; // A - } - return rgba; - } - - // Inline our current DecodeR8G8B8 logic (BGR on-disk β†’ RGBA with A=255). - private static byte[] OurDecodeR8G8B8(byte[] src, int width, int height) - { - var rgba = new byte[width * height * 4]; - for (int i = 0; i < width * height; i++) - { - int s = i * 3; - int d = i * 4; - rgba[d + 0] = src[s + 2]; // R - rgba[d + 1] = src[s + 1]; // G - rgba[d + 2] = src[s + 0]; // B - rgba[d + 3] = 0xFF; // A = opaque - } - return rgba; - } - - // Inline our current DecodeA8 logic (R=G=B=A=val β€” "additive" mode). - private static byte[] OurDecodeA8(byte[] src, int width, int height) - { - var rgba = new byte[width * height * 4]; - for (int i = 0; i < width * height; i++) - { - byte a = src[i]; - int d = i * 4; - rgba[d + 0] = a; - rgba[d + 1] = a; - rgba[d + 2] = a; - rgba[d + 3] = a; - } - return rgba; - } - - // ---- tests ----------------------------------------------------------------- - - /// - /// Test 1: INDEX16 normal mode β€” 2Γ—2 image with two palette entries. - /// WB's FillIndex16 and our DecodeIndex16 must produce identical RGBA bytes. - /// - [Fact] - public void FillIndex16_MatchesOurDecodeIndex16() - { - // 2Γ—2 INDEX16: pixels 0,1,1,0 (indices into a 2-color palette) - var src = new byte[] - { - 0x00, 0x00, // pixel(0,0) β†’ palette index 0 - 0x01, 0x00, // pixel(1,0) β†’ palette index 1 - 0x01, 0x00, // pixel(0,1) β†’ palette index 1 - 0x00, 0x00, // pixel(1,1) β†’ palette index 0 - }; - var palette = MakePalette( - Rgba(0xFF, 0x00, 0x00), // index 0 = red - Rgba(0x00, 0x00, 0xFF) // index 1 = blue - ); - - var expected = OurDecodeIndex16(src, palette, 2, 2); - - var actual = new byte[2 * 2 * 4]; - TextureHelpers.FillIndex16(src, palette, actual, 2, 2); - - Assert.Equal(expected, actual); - } - - /// - /// Test 2: INDEX16 clipmap mode β€” indices below 8 must produce transparent pixels. - /// Both implementations share the same clipmap alpha-key convention from retail ACViewer. - /// - [Fact] - public void FillIndex16_ClipMap_MatchesOurClipMapBehavior() - { - // 4Γ—1 INDEX16: indices 0, 1, 7, 8 - // In clipmap mode, indices 0..7 β†’ transparent; index 8 β†’ palette color. - var src = new byte[] - { - 0x00, 0x00, // index 0 β†’ transparent - 0x01, 0x00, // index 1 β†’ transparent - 0x07, 0x00, // index 7 β†’ transparent - 0x08, 0x00, // index 8 β†’ opaque - }; - // Build a 16-entry palette so indices 0–8 are all valid. - var palette = new Palette(); - for (int i = 0; i < 16; i++) - palette.Colors.Add(Rgba(0xAA, 0xBB, 0xCC)); - - var expected = OurDecodeIndex16(src, palette, 4, 1, isClipMap: true); - - var actual = new byte[4 * 1 * 4]; - TextureHelpers.FillIndex16(src, palette, actual, 4, 1, isClipMap: true); - - Assert.Equal(expected, actual); - } - - /// - /// Test 3: P8 (8-bit palette index) β€” 2Γ—2 image. - /// WB FillP8 and our DecodeP8 must produce identical RGBA output. - /// - [Fact] - public void FillP8_MatchesOurDecodeP8() - { - // 2Γ—2 P8: bytes are direct palette indices - var src = new byte[] { 0x00, 0x01, 0x01, 0x00 }; - var palette = MakePalette( - Rgba(0x10, 0x20, 0x30), // index 0 - Rgba(0x40, 0x50, 0x60) // index 1 - ); - - var expected = OurDecodeP8(src, palette, 2, 2); - - var actual = new byte[2 * 2 * 4]; - TextureHelpers.FillP8(src, palette, actual, 2, 2); - - Assert.Equal(expected, actual); - } - - /// - /// Test 4: A8R8G8B8 (BGRA on-disk β†’ RGBA) β€” 2Γ—1 image. - /// WB FillA8R8G8B8 and our DecodeA8R8G8B8 both swap B↔R. - /// - [Fact] - public void FillA8R8G8B8_MatchesOurDecodeA8R8G8B8() - { - // On-disk layout: B, G, R, A per pixel - var src = new byte[] - { - 0x00, 0x00, 0xFF, 0xFF, // pixel 0: B=0, G=0, R=255, A=255 β†’ red - 0xFF, 0x00, 0x00, 0x80, // pixel 1: B=255, G=0, R=0, A=128 β†’ blue, semi-transparent - }; - - var expected = OurDecodeA8R8G8B8(src, 2, 1); - - var actual = new byte[2 * 1 * 4]; - TextureHelpers.FillA8R8G8B8(src, actual, 2, 1); - - Assert.Equal(expected, actual); - } - - /// - /// Test 5: R8G8B8 (BGR on-disk β†’ RGBA, alpha forced 255) β€” 2Γ—1 image. - /// Both implementations output R,G,B,255 for each 3-byte BGR triple. - /// - [Fact] - public void FillR8G8B8_MatchesOurDecodeR8G8B8() - { - // On-disk layout: B, G, R per pixel (24-bit BGR) - var src = new byte[] - { - 0x00, 0x00, 0xFF, // pixel 0: B=0, G=0, R=255 β†’ red - 0x00, 0xFF, 0x00, // pixel 1: B=0, G=255, R=0 β†’ green - }; - - var expected = OurDecodeR8G8B8(src, 2, 1); - - var actual = new byte[2 * 1 * 4]; - TextureHelpers.FillR8G8B8(src, actual, 2, 1); - - Assert.Equal(expected, actual); - } - - /// - /// Test 6: A8 in additive mode β€” FillA8Additive replicates the byte into all four - /// channels (R=G=B=A=val). This is identical to our current DecodeA8 behavior, - /// which is used for terrain blending alpha masks. - /// - [Fact] - public void FillA8Additive_MatchesOurDecodeA8() - { - // 4Γ—1 single-byte-per-pixel alpha values - var src = new byte[] { 0x00, 0x40, 0x80, 0xFF }; - - var expected = OurDecodeA8(src, 4, 1); - - var actual = new byte[4 * 1 * 4]; - TextureHelpers.FillA8Additive(src, actual, 4, 1); - - Assert.Equal(expected, actual); - // Spot-check: each input byte fans out to all four channels - Assert.Equal(new byte[] { 0x00, 0x00, 0x00, 0x00 }, actual[0..4]); - Assert.Equal(new byte[] { 0x40, 0x40, 0x40, 0x40 }, actual[4..8]); - Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF }, actual[12..16]); - } - - /// - /// Test 7: A8 non-additive (FillA8) documents WB's behavior that DIFFERS from ours. - /// WB's FillA8 sets R=G=B=255 and A=input_byte. - /// Our DecodeA8 sets R=G=B=A=input_byte (the additive mode, used for terrain blending). - /// This test proves the divergence exists and documents the WB behavior explicitly. - /// - [Fact] - public void FillA8_NonAdditive_ProducesWhitePlusAlpha() - { - var src = new byte[] { 0x00, 0x80, 0xFF }; // 3Γ—1 - - var actual = new byte[3 * 1 * 4]; - TextureHelpers.FillA8(src, actual, 3, 1); - - // WB non-additive: R=G=B=255, A=input byte - Assert.Equal(new byte[] { 255, 255, 255, 0x00 }, actual[0..4]); // alpha=0 - Assert.Equal(new byte[] { 255, 255, 255, 0x80 }, actual[4..8]); // alpha=128 - Assert.Equal(new byte[] { 255, 255, 255, 0xFF }, actual[8..12]); // alpha=255 - - // Confirm this DIFFERS from our current DecodeA8 behavior (R=G=B=A=val). - var ourDecoded = OurDecodeA8(src, 3, 1); - Assert.NotEqual(ourDecoded, actual); // divergence is intentional β€” both are documented - } - - /// - /// Test 8: R5G6B5 (16-bit packed RGB, no alpha) β€” WB format we don't implement yet. - /// Verifies the expected bit-expansion: 5-bit red β†’ 8-bit by left-shifting 3, - /// 6-bit green β†’ 8-bit by left-shifting 2, 5-bit blue β†’ 8-bit by left-shifting 3. - /// Alpha is always 255. - /// - [Fact] - public void FillR5G6B5_ProducesExpectedRgba() - { - // Encode a single pixel: R=0x1F (31), G=0x3F (63), B=0x1F (31) - // Packed as 16-bit little-endian: bits 15-11=R, 10-5=G, 4-0=B - // val = (0x1F << 11) | (0x3F << 5) | 0x1F = 0xFFFF - var src = new byte[] { 0xFF, 0xFF }; // 1Γ—1 pixel: all channels maxed - - var actual = new byte[1 * 1 * 4]; - TextureHelpers.FillR5G6B5(src, actual, 1, 1); - - // R = (0x1F << 3) = 0xF8, G = (0x3F << 2) = 0xFC, B = (0x1F << 3) = 0xF8, A = 255 - Assert.Equal((byte)0xF8, actual[0]); // R - Assert.Equal((byte)0xFC, actual[1]); // G - Assert.Equal((byte)0xF8, actual[2]); // B - Assert.Equal((byte)255, actual[3]); // A always opaque - - // Test a second pixel: pure red = R=31, G=0, B=0 - // val = (0x1F << 11) = 0xF800 - var srcRed = new byte[] { 0x00, 0xF8 }; // little-endian 0xF800 - var actualRed = new byte[4]; - TextureHelpers.FillR5G6B5(srcRed, actualRed, 1, 1); - Assert.Equal((byte)0xF8, actualRed[0]); // R = 31 << 3 = 0xF8 - Assert.Equal((byte)0x00, actualRed[1]); // G = 0 - Assert.Equal((byte)0x00, actualRed[2]); // B = 0 - Assert.Equal((byte)255, actualRed[3]); // A - } - - /// - /// Test 9: A4R4G4B4 (16-bit packed ARGB, 4 bits per channel) β€” WB format we don't implement yet. - /// Each 4-bit value is expanded to 8-bit by multiplying by 17 (0x11), - /// so 0xF β†’ 255, 0x8 β†’ 136, 0x0 β†’ 0. - /// Bit layout: val bits 15-12=A, 11-8=R, 7-4=G, 3-0=B. - /// - [Fact] - public void FillA4R4G4B4_ProducesExpectedRgba() - { - // Encode one pixel: A=0xF(255), R=0xA(170), G=0x5(85), B=0x0(0) - // val = (0xF << 12) | (0xA << 8) | (0x5 << 4) | 0x0 = 0xFA50 - // little-endian bytes: 0x50, 0xFA - var src = new byte[] { 0x50, 0xFA }; // 1Γ—1 - - var actual = new byte[1 * 1 * 4]; - TextureHelpers.FillA4R4G4B4(src, actual, 1, 1); - - // R = 0xA * 17 = 170, G = 0x5 * 17 = 85, B = 0x0 * 17 = 0, A = 0xF * 17 = 255 - Assert.Equal((byte)(0xA * 17), actual[0]); // R = 170 - Assert.Equal((byte)(0x5 * 17), actual[1]); // G = 85 - Assert.Equal((byte)(0x0 * 17), actual[2]); // B = 0 - Assert.Equal((byte)(0xF * 17), actual[3]); // A = 255 - - // Also test the zero case: all channels 0 - var srcZero = new byte[] { 0x00, 0x00 }; - var actualZero = new byte[4]; - TextureHelpers.FillA4R4G4B4(srcZero, actualZero, 1, 1); - Assert.Equal(new byte[] { 0, 0, 0, 0 }, actualZero); - } -} diff --git a/tests/AcDream.Core.Tests/Ui/RadarBlipColorsTests.cs b/tests/AcDream.Core.Tests/Ui/RadarBlipColorsTests.cs deleted file mode 100644 index 482ea3e..0000000 --- a/tests/AcDream.Core.Tests/Ui/RadarBlipColorsTests.cs +++ /dev/null @@ -1,83 +0,0 @@ -using AcDream.Core.Items; -using AcDream.Core.Ui; -using Xunit; - -namespace AcDream.Core.Tests.Ui; - -public sealed class RadarBlipColorsTests -{ - // PWD bit constants per docs/research/named-retail/acclient.h:6431-6463 - private const uint BF_PLAYER = 0x8u; - private const uint BF_PLAYER_KILLER = 0x20u; - private const uint BF_VENDOR = 0x200u; - private const uint BF_PORTAL = 0x40000u; - private const uint BF_PKLITE_PKSTATUS = 0x2000000u; - - [Fact] - public void Item_NoFlags_ReturnsItemColor() - { - // SpellComponents is itemType=0x1000 (e.g. a Taper) β€” not a creature. - var result = RadarBlipColors.For(itemType: (uint)ItemType.SpellComponents, pwdBitfield: 0); - Assert.Equal(RadarBlipColors.Item, result); - } - - [Fact] - public void Misc_NoFlags_ReturnsItemColor() - { - var result = RadarBlipColors.For((uint)ItemType.Misc, pwdBitfield: 0); - Assert.Equal(RadarBlipColors.Item, result); - } - - [Fact] - public void Creature_NotPlayer_ReturnsCreatureColor() - { - // NPC: itemType has Creature bit, no Player flag. - var result = RadarBlipColors.For((uint)ItemType.Creature, pwdBitfield: 0); - Assert.Equal(RadarBlipColors.Creature, result); - } - - [Fact] - public void FriendlyPlayer_ReturnsDefaultColor() - { - // Friendly player: Creature itemType + Player flag, no PK bits. - var result = RadarBlipColors.For((uint)ItemType.Creature, pwdBitfield: BF_PLAYER); - Assert.Equal(RadarBlipColors.Default, result); - } - - [Fact] - public void PK_Player_ReturnsPlayerKillerColor() - { - var result = RadarBlipColors.For((uint)ItemType.Creature, - pwdBitfield: BF_PLAYER | BF_PLAYER_KILLER); - Assert.Equal(RadarBlipColors.PlayerKiller, result); - } - - [Fact] - public void PKLite_Player_ReturnsPKLiteColor() - { - var result = RadarBlipColors.For((uint)ItemType.Creature, - pwdBitfield: BF_PLAYER | BF_PKLITE_PKSTATUS); - Assert.Equal(RadarBlipColors.PKLite, result); - } - - [Fact] - public void Vendor_BeatsCreatureFlag() - { - // A vendor NPC: Creature itemType + Vendor flag. Vendor wins per - // retail's dispatch order (vendor check happens before creature - // check at 0x004d7946-004d7973). - var result = RadarBlipColors.For((uint)ItemType.Creature, - pwdBitfield: BF_VENDOR); - Assert.Equal(RadarBlipColors.Vendor, result); - } - - [Fact] - public void Portal_TopPriority() - { - // Portal flag wins over everything else (retail dispatch order - // checks BF_PORTAL first). - var result = RadarBlipColors.For((uint)ItemType.Creature, - pwdBitfield: BF_PORTAL | BF_PLAYER | BF_PLAYER_KILLER); - Assert.Equal(RadarBlipColors.Portal, result); - } -} diff --git a/tests/AcDream.Core.Tests/Ui/RetailMessagesTests.cs b/tests/AcDream.Core.Tests/Ui/RetailMessagesTests.cs deleted file mode 100644 index e161719..0000000 --- a/tests/AcDream.Core.Tests/Ui/RetailMessagesTests.cs +++ /dev/null @@ -1,77 +0,0 @@ -using AcDream.Core.Ui; - -namespace AcDream.Core.Tests.Ui; - -public sealed class RetailMessagesTests -{ - [Fact] - public void CannotBeUsed_FormatsRetailLiteral() - { - // Retail acclient_2013_pseudo_c.txt:1033115 (data_7e2a70): - // "The %s cannot be used" - // Interpolated form with the entity name where %s sat. - Assert.Equal("The Holtburg cannot be used", - RetailMessages.CannotBeUsed("Holtburg")); - } - - [Fact] - public void CantBePickedUp_FormatsRetailLiteral() - { - // Retail acclient_2013_pseudo_c.txt:401589 sprintf: - // "The %s can't be picked up!" - Assert.Equal("The Holtburg can't be picked up!", - RetailMessages.CantBePickedUp("Holtburg")); - } - - [Fact] - public void CannotPickUpCreatures_IsExactRetailLiteral() - { - // Retail acclient_2013_pseudo_c.txt:1033034 (data_7e22b4): - // "You cannot pick up creatures!" - // Constant; no placeholder. - Assert.Equal("You cannot pick up creatures!", - RetailMessages.CannotPickUpCreatures); - } - - [Fact] - public void CannotBeUsedWith_FormatsRetailLiteral() - { - // Retail acclient_2013_pseudo_c.txt:1024669 (data_7cc834): - // "Cannot be used with %s" - Assert.Equal("Cannot be used with Lockpick", - RetailMessages.CannotBeUsedWith("Lockpick")); - } - - [Fact] - public void CannotBePickedUp_FormatsFormalRetailVariant() - { - // Retail acclient_2013_pseudo_c.txt:1033033 (data_7e227c): - // "The %s cannot be picked up!" - // FORMAL variant β€” distinct from informal CantBePickedUp. - Assert.Equal("The Holtburg cannot be picked up!", - RetailMessages.CannotBePickedUp("Holtburg")); - } - - [Fact] - public void CannotBeUsedWhileOnHook_HooksOff_PreservesTrailingNewline() - { - // Retail acclient_2013_pseudo_c.txt:1029591 (data_7d1f68). - // Trailing \n is part of the retail literal. - string actual = RetailMessages.CannotBeUsedWhileOnHook_HooksOff("Chest"); - Assert.Equal( - "The Chest cannot be used while on a hook, use the '@house hooks on' command to make the hook openable.\n", - actual); - Assert.EndsWith("\n", actual); - } - - [Fact] - public void CannotBeUsedWhileOnHook_NotOwner_PreservesTrailingNewline() - { - // Retail acclient_2013_pseudo_c.txt:1030063 (data_7d5f30). - string actual = RetailMessages.CannotBeUsedWhileOnHook_NotOwner("Chest"); - Assert.Equal( - "The Chest cannot be used while on a hook and only the owner may open the hook.\n", - actual); - Assert.EndsWith("\n", actual); - } -} diff --git a/tests/AcDream.Core.Tests/Vfx/ParticleHookSinkTests.cs b/tests/AcDream.Core.Tests/Vfx/ParticleHookSinkTests.cs index 2fbf839..1fc53e6 100644 --- a/tests/AcDream.Core.Tests/Vfx/ParticleHookSinkTests.cs +++ b/tests/AcDream.Core.Tests/Vfx/ParticleHookSinkTests.cs @@ -92,84 +92,4 @@ public sealed class ParticleHookSinkTests sys.Tick(0.01f); Assert.Equal(0, sys.ActiveEmitterCount); } - - [Fact] - public void SpawnFromHook_AppliesPartTransform_WhenRegistered() - { - // C.1.5b #56: when SetEntityPartTransforms has been called for - // entityId, SpawnFromHook must transform the hook offset through - // the part-local matrix before applying entity rotation. - // Part 1 is lifted +Z=1; hook offset = (1, 0, 0), PartIndex=1. - // Expected world position: (1, 0, 1) with identity rotation. - var registry = new EmitterDescRegistry(); - registry.Register(MakeDesc(0x32000030u, attachLocal: false)); - var sys = new ParticleSystem(registry, new System.Random(42)); - var sink = new ParticleHookSink(sys); - - var partTransforms = new Matrix4x4[] - { - Matrix4x4.Identity, - Matrix4x4.CreateTranslation(0f, 0f, 1f), - }; - sink.SetEntityRotation(0xCAFEu, Quaternion.Identity); - sink.SetEntityPartTransforms(0xCAFEu, partTransforms); - - sink.OnHook(0xCAFEu, Vector3.Zero, new CreateParticleHook - { - EmitterInfoId = 0x32000030u, - EmitterId = 0, - PartIndex = 1, - Offset = new Frame - { - Origin = new Vector3(1f, 0f, 0f), - Orientation = Quaternion.Identity, - }, - }); - sys.Tick(0.001f); - - var live = System.Linq.Enumerable.Single(sys.EnumerateLive()); - var pos = live.Emitter.Particles[live.Index].Position; - Assert.InRange(pos.X, 0.99f, 1.01f); - Assert.InRange(pos.Y, -0.01f, 0.01f); - Assert.InRange(pos.Z, 0.99f, 1.01f); - } - - [Fact] - public void SpawnFromHook_FallsBackToIdentity_WhenPartIndexOutOfBounds() - { - // Out-of-bounds PartIndex must NOT crash and must NOT apply a - // wrong matrix; falls back to no part transform (Identity), so - // the offset is applied in entity-local space as pre-C.1.5b. - var registry = new EmitterDescRegistry(); - registry.Register(MakeDesc(0x32000031u, attachLocal: false)); - var sys = new ParticleSystem(registry, new System.Random(42)); - var sink = new ParticleHookSink(sys); - - var partTransforms = new Matrix4x4[] - { - Matrix4x4.Identity, - Matrix4x4.CreateTranslation(0f, 0f, 1f), - }; - sink.SetEntityRotation(0xCAFEu, Quaternion.Identity); - sink.SetEntityPartTransforms(0xCAFEu, partTransforms); - - sink.OnHook(0xCAFEu, Vector3.Zero, new CreateParticleHook - { - EmitterInfoId = 0x32000031u, - EmitterId = 0, - PartIndex = 99, // way past the 2-part array - Offset = new Frame - { - Origin = new Vector3(2f, 0f, 0f), - Orientation = Quaternion.Identity, - }, - }); - sys.Tick(0.001f); - - var live = System.Linq.Enumerable.Single(sys.EnumerateLive()); - var pos = live.Emitter.Particles[live.Index].Position; - Assert.InRange(pos.X, 1.99f, 2.01f); - Assert.InRange(pos.Y, -0.01f, 0.01f); - Assert.InRange(pos.Z, -0.01f, 0.01f); - } } diff --git a/tests/AcDream.Core.Tests/World/LandblockLoaderTests.cs b/tests/AcDream.Core.Tests/World/LandblockLoaderTests.cs index d1d24b8..af68b01 100644 --- a/tests/AcDream.Core.Tests/World/LandblockLoaderTests.cs +++ b/tests/AcDream.Core.Tests/World/LandblockLoaderTests.cs @@ -116,50 +116,4 @@ public class LandblockLoaderTests var entities = LandblockLoader.BuildEntitiesFromInfo(new LandBlockInfo()); Assert.Empty(entities); } - - [Fact] - public void BuildEntitiesFromInfo_WithLandblockId_NamespacesIdsForGlobalUniqueness() - { - // Regression: cross-LB stab Id collision was the cause of visual - // glitches in Tier 1 cache (commit ) β€” buildings rendered - // up in the air with wrong textures because cache was keyed by - // entity.Id and stab Ids restarted at 1 per landblock. - var info = new LandBlockInfo - { - Objects = - { - new Stab { Id = 0x01000001u, Frame = new Frame() }, - new Stab { Id = 0x01000002u, Frame = new Frame() }, - }, - }; - - var entitiesLbA = LandblockLoader.BuildEntitiesFromInfo(info, landblockId: 0xA9B40000u); - var entitiesLbB = LandblockLoader.BuildEntitiesFromInfo(info, landblockId: 0xA9B50000u); - - // No two entities across LB A and LB B share the same Id. - var idsA = entitiesLbA.Select(e => e.Id).ToArray(); - var idsB = entitiesLbB.Select(e => e.Id).ToArray(); - Assert.Empty(idsA.Intersect(idsB)); - - // The namespace top byte is 0xC0 for stabs (distinct from 0x80 scenery, - // 0x40 interior, low-range live entities). - Assert.All(idsA, id => Assert.Equal(0xC0u, (id >> 24) & 0xFFu)); - Assert.All(idsB, id => Assert.Equal(0xC0u, (id >> 24) & 0xFFu)); - } - - [Fact] - public void BuildEntitiesFromInfo_LegacyZeroLandblockId_StartsAtOne() - { - // Backward compat: existing callers (tests pre-fix) call without a - // landblockId and get the legacy "starts at 1" behavior. - var info = new LandBlockInfo - { - Objects = { new Stab { Id = 0x01000001u, Frame = new Frame() } }, - }; - - var entities = LandblockLoader.BuildEntitiesFromInfo(info); - - Assert.Single(entities); - Assert.Equal(1u, entities[0].Id); - } } diff --git a/tests/AcDream.Core.Tests/World/SceneryGeneratorTests.cs b/tests/AcDream.Core.Tests/World/SceneryGeneratorTests.cs index 83cd73f..d003ea8 100644 --- a/tests/AcDream.Core.Tests/World/SceneryGeneratorTests.cs +++ b/tests/AcDream.Core.Tests/World/SceneryGeneratorTests.cs @@ -4,11 +4,10 @@ using DatReaderWriter.Types; namespace AcDream.Core.Tests.World; /// -/// Tests for SceneryGenerator. As of Phase N.1 (commit b84ecbd / Task 8 final -/// commit), the displacement / road / slope / rotation / scale algorithms run -/// through WorldBuilder's helpers (SceneryHelpers + TerrainUtils). The only -/// our-side code remaining is the small -/// predicate, which is what these tests cover. +/// Tests for SceneryGenerator road-exclusion logic. +/// The full Generate() pipeline requires real dat files (Region, Scene, etc.) +/// so road-check behavior is tested via the internal IsRoadVertex helper, +/// which is the single gate that guards against placing trees on roads. /// public class SceneryGeneratorTests { @@ -33,12 +32,15 @@ public class SceneryGeneratorTests [Fact] public void IsRoadVertex_ZeroTerrain_IsNotRoad() { + // A fully blank terrain entry (no type, no road, no scene) is not a road. Assert.False(SceneryGenerator.IsRoadVertex(0)); } [Fact] public void IsRoadVertex_MatchesTerrainInfoRoadProperty() { + // Verify that IsRoadVertex agrees with the typed TerrainInfo.Road property + // for a sample of raw values, ensuring the bit convention is consistent. for (ushort raw = 0; raw < 4; raw++) { TerrainInfo ti = raw; diff --git a/tests/AcDream.Core.Tests/World/WbSceneryAdapterTests.cs b/tests/AcDream.Core.Tests/World/WbSceneryAdapterTests.cs deleted file mode 100644 index 79fd358..0000000 --- a/tests/AcDream.Core.Tests/World/WbSceneryAdapterTests.cs +++ /dev/null @@ -1,74 +0,0 @@ -using AcDream.Core.World; -using DatReaderWriter.DBObjs; -using DatReaderWriter.Types; - -namespace AcDream.Core.Tests.World; - -/// -/// Tests for . The adapter converts our -/// LandBlock dat type (Terrain TerrainInfo[81] + Height byte[81]) into -/// WorldBuilder's [81] -/// shape, which WB's TerrainUtils / SceneryRenderManager consume. -/// -/// Bit layout in LandBlock.Terrain[i] (TerrainInfo / ushort): -/// bits 0-1 : Road (2 bits) β†’ WB TerrainEntry.Road -/// bits 2-6 : TerrainType (5 bits) β†’ WB TerrainEntry.Type -/// bits 11-15 : SceneType (5 bits) β†’ WB TerrainEntry.Scenery -/// Height comes from LandBlock.Height[i] (byte) β†’ WB TerrainEntry.Height. -/// -public class WbSceneryAdapterTests -{ - [Fact] - public void BuildTerrainEntries_PreservesRoadTextureSceneryHeight() - { - var block = new LandBlock(); - - // Vertex 0: road=0x3, type=0x00, scenery=0x1F, height=42 - // raw layout: bits 0-1=11, bits 2-6=00000, bits 7-10=0000, bits 11-15=11111 - // = 0xF803 - block.Terrain[0] = (TerrainInfo)0xF803; - block.Height[0] = 42; - - // Vertex 80: road=0x0, type=0x1F, scenery=0x00, height=200 - // raw layout: bits 0-1=00, bits 2-6=11111, bits 11-15=00000 - // = 0x007C - block.Terrain[80] = (TerrainInfo)0x007C; - block.Height[80] = 200; - - var entries = WbSceneryAdapter.BuildTerrainEntries(block); - - Assert.Equal(81, entries.Length); - - Assert.Equal((byte)42, entries[0].Height); - Assert.Equal((byte)0x3, entries[0].Road); - Assert.Equal((byte)0x00, entries[0].Type); - Assert.Equal((byte)0x1F, entries[0].Scenery); - - Assert.Equal((byte)200, entries[80].Height); - Assert.Equal((byte)0x0, entries[80].Road); - Assert.Equal((byte)0x1F, entries[80].Type); - Assert.Equal((byte)0x00, entries[80].Scenery); - } - - [Fact] - public void BuildTerrainEntries_AllZeros_ProducesEmptyEntries() - { - var block = new LandBlock(); - // Terrain and Height are already zero-initialized by LandBlock constructor. - var entries = WbSceneryAdapter.BuildTerrainEntries(block); - Assert.All(entries, e => - { - Assert.Equal((byte)0, e.Height); - Assert.Equal((byte)0, e.Road); - Assert.Equal((byte)0, e.Type); - Assert.Equal((byte)0, e.Scenery); - }); - } - - [Fact] - public void BuildTerrainEntries_NullBlock_Throws() - { - Assert.Throws(() => - WbSceneryAdapter.BuildTerrainEntries(null!)); - } -} diff --git a/tests/AcDream.Core.Tests/World/WorldEntityAabbTests.cs b/tests/AcDream.Core.Tests/World/WorldEntityAabbTests.cs deleted file mode 100644 index cafa60e..0000000 --- a/tests/AcDream.Core.Tests/World/WorldEntityAabbTests.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System.Numerics; -using AcDream.Core.World; -using Xunit; - -namespace AcDream.Core.Tests.World; - -public class WorldEntityAabbTests -{ - [Fact] - public void Aabb_DefaultRadius_PositionPlusMinus5() - { - var entity = new WorldEntity - { - Id = 1, - SourceGfxObjOrSetupId = 0, - Position = new Vector3(10, 20, 30), - Rotation = System.Numerics.Quaternion.Identity, - MeshRefs = System.Array.Empty(), - }; - entity.RefreshAabb(); - - Assert.Equal(new Vector3(5, 15, 25), entity.AabbMin); - Assert.Equal(new Vector3(15, 25, 35), entity.AabbMax); - } - - [Fact] - public void Aabb_DirtyFlag_SetByMutator_ClearedByRefresh() - { - var entity = new WorldEntity - { - Id = 1, - SourceGfxObjOrSetupId = 0, - Position = new Vector3(10, 20, 30), - Rotation = System.Numerics.Quaternion.Identity, - MeshRefs = System.Array.Empty(), - }; - entity.RefreshAabb(); - Assert.False(entity.AabbDirty); - - entity.SetPosition(new Vector3(100, 200, 300)); - Assert.True(entity.AabbDirty); - - entity.RefreshAabb(); - Assert.False(entity.AabbDirty); - Assert.Equal(new Vector3(95, 195, 295), entity.AabbMin); - } -} diff --git a/tests/AcDream.UI.Abstractions.Tests/Input/InputDispatcherDoubleClickTests.cs b/tests/AcDream.UI.Abstractions.Tests/Input/InputDispatcherDoubleClickTests.cs deleted file mode 100644 index f6de5da..0000000 --- a/tests/AcDream.UI.Abstractions.Tests/Input/InputDispatcherDoubleClickTests.cs +++ /dev/null @@ -1,116 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading; -using AcDream.UI.Abstractions.Input; -using Silk.NET.Input; - -namespace AcDream.UI.Abstractions.Tests.Input; - -/// -/// Tests for double-click detection added to -/// in Phase B.4b. The dispatcher tracks the most-recent mouse-down button + -/// timestamp; a same-button press within DoubleClickThresholdMs (500 ms) -/// additionally fires for the matching -/// binding on top of the normal . -/// -public class InputDispatcherDoubleClickTests -{ - /// - /// Build a dispatcher wired with LMB Press β†’ SelectLeft, - /// LMB DoubleClick β†’ SelectDblLeft, and RMB Press β†’ SelectRight. - /// - private static (InputDispatcher dispatcher, FakeMouseSource mouse, List<(InputAction, ActivationType)> fired) - Build() - { - var kb = new FakeKeyboardSource(); - var mouse = new FakeMouseSource(); - var bindings = new KeyBindings(); - - var lmbChord = new KeyChord(InputDispatcher.MouseButtonToKey(MouseButton.Left), ModifierMask.None, Device: 1); - var rmbChord = new KeyChord(InputDispatcher.MouseButtonToKey(MouseButton.Right), ModifierMask.None, Device: 1); - - bindings.Add(new Binding(lmbChord, InputAction.SelectLeft)); - bindings.Add(new Binding(lmbChord, InputAction.SelectDblLeft, ActivationType.DoubleClick)); - bindings.Add(new Binding(rmbChord, InputAction.SelectRight)); - - var dispatcher = new InputDispatcher(kb, mouse, bindings); - var fired = new List<(InputAction, ActivationType)>(); - dispatcher.Fired += (a, t) => fired.Add((a, t)); - return (dispatcher, mouse, fired); - } - - /// - /// Two LMB clicks in rapid succession (~10 ms) β†’ Press fires twice AND - /// DoubleClick fires once (on the second click). - /// - [Fact] - public void SecondClick_WithinThreshold_FiresDoubleClick() - { - var (_, mouse, fired) = Build(); - - mouse.EmitMouseDown(MouseButton.Left, ModifierMask.None); - Thread.Sleep(10); - mouse.EmitMouseDown(MouseButton.Left, ModifierMask.None); - - // Two SelectLeft Press events (one per click). - Assert.Equal(2, fired.FindAll(e => e == (InputAction.SelectLeft, ActivationType.Press)).Count); - - // One SelectDblLeft DoubleClick event on the second click. - Assert.Single(fired, e => e == (InputAction.SelectDblLeft, ActivationType.DoubleClick)); - } - - /// - /// Two LMB clicks 600 ms apart β†’ Press fires twice but NO DoubleClick - /// (interval exceeds the 500 ms threshold). - /// - [Fact] - public void SecondClick_BeyondThreshold_DoesNotFireDoubleClick() - { - var (_, mouse, fired) = Build(); - - mouse.EmitMouseDown(MouseButton.Left, ModifierMask.None); - Thread.Sleep(600); - mouse.EmitMouseDown(MouseButton.Left, ModifierMask.None); - - Assert.Equal(2, fired.FindAll(e => e == (InputAction.SelectLeft, ActivationType.Press)).Count); - Assert.Empty(fired.FindAll(e => e.Item2 == ActivationType.DoubleClick)); - } - - /// - /// LMB then RMB in rapid succession β†’ no DoubleClick (different buttons). - /// - [Fact] - public void DifferentButtons_DoNotFireDoubleClick() - { - var (_, mouse, fired) = Build(); - - mouse.EmitMouseDown(MouseButton.Left, ModifierMask.None); - Thread.Sleep(10); - mouse.EmitMouseDown(MouseButton.Right, ModifierMask.None); - - Assert.Empty(fired.FindAll(e => e.Item2 == ActivationType.DoubleClick)); - } - - /// - /// Three rapid LMB clicks β†’ exactly one DoubleClick (between clicks 1 and 2). - /// The third click resets the pair-state, so it acts as the "first click" of - /// a new potential double-click rather than firing a second DoubleClick. - /// - [Fact] - public void ThirdClick_AfterDoubleClick_RequiresFreshPair() - { - var (_, mouse, fired) = Build(); - - mouse.EmitMouseDown(MouseButton.Left, ModifierMask.None); // click 1 - Thread.Sleep(10); - mouse.EmitMouseDown(MouseButton.Left, ModifierMask.None); // click 2 β†’ DoubleClick fires, state reset - Thread.Sleep(10); - mouse.EmitMouseDown(MouseButton.Left, ModifierMask.None); // click 3 β†’ no DoubleClick (fresh pair started) - - // Three Press events total. - Assert.Equal(3, fired.FindAll(e => e == (InputAction.SelectLeft, ActivationType.Press)).Count); - - // Exactly one DoubleClick (between clicks 1 and 2). - Assert.Single(fired.FindAll(e => e == (InputAction.SelectDblLeft, ActivationType.DoubleClick))); - } -} diff --git a/tests/AcDream.UI.Abstractions.Tests/Panels/Settings/QualityPresetTests.cs b/tests/AcDream.UI.Abstractions.Tests/Panels/Settings/QualityPresetTests.cs deleted file mode 100644 index 754cba9..0000000 --- a/tests/AcDream.UI.Abstractions.Tests/Panels/Settings/QualityPresetTests.cs +++ /dev/null @@ -1,181 +0,0 @@ -using AcDream.UI.Abstractions.Settings; -using Xunit; - -namespace AcDream.UI.Abstractions.Tests.Panels.Settings; - -/// -/// A.5 T22.5: preset table + env-var override -/// coverage. Env-var tests clear their variables in finally blocks so -/// parallel runners cannot bleed state between tests. -/// -public class QualityPresetTests -{ - [Theory] - [InlineData(QualityPreset.Low, 2, 5, 0)] - [InlineData(QualityPreset.Medium, 3, 8, 2)] - [InlineData(QualityPreset.High, 4, 12, 4)] - [InlineData(QualityPreset.Ultra, 5, 15, 4)] - public void From_Preset_ProducesExpectedRadiiAndMsaa( - QualityPreset preset, int n1, int n2, int msaa) - { - var s = QualitySettings.From(preset); - Assert.Equal(n1, s.NearRadius); - Assert.Equal(n2, s.FarRadius); - Assert.Equal(msaa, s.MsaaSamples); - } - - [Theory] - [InlineData(QualityPreset.Low, 4, false)] - [InlineData(QualityPreset.Medium, 8, false)] - [InlineData(QualityPreset.High, 16, true)] - [InlineData(QualityPreset.Ultra, 16, true)] - public void From_Preset_ProducesExpectedAnisoAndA2C( - QualityPreset preset, int aniso, bool a2c) - { - var s = QualitySettings.From(preset); - Assert.Equal(aniso, s.AnisotropicLevel); - Assert.Equal(a2c, s.AlphaToCoverage); - } - - [Theory] - [InlineData(QualityPreset.Low, 2)] - [InlineData(QualityPreset.Medium, 3)] - [InlineData(QualityPreset.High, 4)] - [InlineData(QualityPreset.Ultra, 6)] - public void From_Preset_ProducesExpectedMaxCompletions( - QualityPreset preset, int expected) - { - var s = QualitySettings.From(preset); - Assert.Equal(expected, s.MaxCompletionsPerFrame); - } - - [Fact] - public void EnvVar_NearRadius_OverridesPreset() - { - System.Environment.SetEnvironmentVariable("ACDREAM_NEAR_RADIUS", "2"); - try - { - var s = QualitySettings.From(QualityPreset.High); // High = NearRadius=4 normally - var resolved = QualitySettings.WithEnvOverrides(s); - Assert.Equal(2, resolved.NearRadius); - Assert.Equal(12, resolved.FarRadius); // FarRadius unaffected - } - finally { System.Environment.SetEnvironmentVariable("ACDREAM_NEAR_RADIUS", null); } - } - - [Fact] - public void EnvVar_FarRadius_OverridesPreset() - { - System.Environment.SetEnvironmentVariable("ACDREAM_FAR_RADIUS", "20"); - try - { - var s = QualitySettings.From(QualityPreset.High); - var resolved = QualitySettings.WithEnvOverrides(s); - Assert.Equal(4, resolved.NearRadius); // NearRadius unaffected - Assert.Equal(20, resolved.FarRadius); - } - finally { System.Environment.SetEnvironmentVariable("ACDREAM_FAR_RADIUS", null); } - } - - [Fact] - public void EnvVar_AlphaToCoverage_BooleanParsing() - { - // Ensure "0" and "false" disable; other values enable. - System.Environment.SetEnvironmentVariable("ACDREAM_A2C", "0"); - try - { - var s = QualitySettings.From(QualityPreset.High); // High has A2C=true - var resolved = QualitySettings.WithEnvOverrides(s); - Assert.False(resolved.AlphaToCoverage); - } - finally { System.Environment.SetEnvironmentVariable("ACDREAM_A2C", null); } - } - - [Fact] - public void EnvVar_AlphaToCoverage_FalseString_Disables() - { - System.Environment.SetEnvironmentVariable("ACDREAM_A2C", "false"); - try - { - var s = QualitySettings.From(QualityPreset.High); - var resolved = QualitySettings.WithEnvOverrides(s); - Assert.False(resolved.AlphaToCoverage); - } - finally { System.Environment.SetEnvironmentVariable("ACDREAM_A2C", null); } - } - - [Fact] - public void EnvVar_AlphaToCoverage_NonZeroEnables() - { - System.Environment.SetEnvironmentVariable("ACDREAM_A2C", "1"); - try - { - var s = QualitySettings.From(QualityPreset.Low); // Low has A2C=false - var resolved = QualitySettings.WithEnvOverrides(s); - Assert.True(resolved.AlphaToCoverage); - } - finally { System.Environment.SetEnvironmentVariable("ACDREAM_A2C", null); } - } - - [Fact] - public void EnvVar_Unset_LeavesPresetDefault() - { - // Ensure no env vars are set for this test's fields. - System.Environment.SetEnvironmentVariable("ACDREAM_NEAR_RADIUS", null); - System.Environment.SetEnvironmentVariable("ACDREAM_FAR_RADIUS", null); - System.Environment.SetEnvironmentVariable("ACDREAM_A2C", null); - - var s = QualitySettings.From(QualityPreset.High); - var resolved = QualitySettings.WithEnvOverrides(s); - Assert.Equal(s, resolved); - } - - [Fact] - public void From_UndefinedPreset_FallsBackToHigh() - { - var s = QualitySettings.From((QualityPreset)99); - Assert.Equal(4, s.NearRadius); // High default - Assert.Equal(12, s.FarRadius); - Assert.Equal(4, s.MsaaSamples); - Assert.True(s.AlphaToCoverage); - } - - [Fact] - public void EnvVar_MaxCompletionsPerFrame_OverridesPreset() - { - System.Environment.SetEnvironmentVariable("ACDREAM_MAX_COMPLETIONS_PER_FRAME", "8"); - try - { - var s = QualitySettings.From(QualityPreset.High); // High = 4 - var resolved = QualitySettings.WithEnvOverrides(s); - Assert.Equal(8, resolved.MaxCompletionsPerFrame); - } - finally { System.Environment.SetEnvironmentVariable("ACDREAM_MAX_COMPLETIONS_PER_FRAME", null); } - } - - [Fact] - public void EnvVar_MsaaSamples_OverridesPreset() - { - System.Environment.SetEnvironmentVariable("ACDREAM_MSAA_SAMPLES", "8"); - try - { - var s = QualitySettings.From(QualityPreset.High); // High = 4 - var resolved = QualitySettings.WithEnvOverrides(s); - Assert.Equal(8, resolved.MsaaSamples); - } - finally { System.Environment.SetEnvironmentVariable("ACDREAM_MSAA_SAMPLES", null); } - } - - [Fact] - public void EnvVar_Anisotropic_OverridesPreset() - { - System.Environment.SetEnvironmentVariable("ACDREAM_ANISOTROPIC", "4"); - try - { - var s = QualitySettings.From(QualityPreset.High); // High = 16 - var resolved = QualitySettings.WithEnvOverrides(s); - Assert.Equal(4, resolved.AnisotropicLevel); - } - finally { System.Environment.SetEnvironmentVariable("ACDREAM_ANISOTROPIC", null); } - } -} diff --git a/tests/AcDream.UI.Abstractions.Tests/Panels/Settings/SettingsStoreTests.cs b/tests/AcDream.UI.Abstractions.Tests/Panels/Settings/SettingsStoreTests.cs index b54d0f0..edc24b2 100644 --- a/tests/AcDream.UI.Abstractions.Tests/Panels/Settings/SettingsStoreTests.cs +++ b/tests/AcDream.UI.Abstractions.Tests/Panels/Settings/SettingsStoreTests.cs @@ -44,8 +44,7 @@ public sealed class SettingsStoreTests : System.IDisposable VSync: false, FieldOfView: 100f, Gamma: 1.4f, - ShowFps: true, - Quality: AcDream.UI.Abstractions.Settings.QualityPreset.Ultra); + ShowFps: true); store.SaveDisplay(original); var loaded = store.LoadDisplay();