Init Homebase

This commit is contained in:
2026-06-02 18:28:23 -07:00
parent 2ee30c01fd
commit dd0064c377
48 changed files with 1934 additions and 12 deletions
@@ -0,0 +1,52 @@
---
date: 2026-06-02
type: session
tags: [session, dots, netcode, home-base, rpc, ghost, m5, m4]
permalink: gamevault/07-sessions/2026/2026-06-02-m5-homebase-baselayer
---
# Session 2026-06-02 — M5: home-base base-layer + shared storage
## Goal
Close out M4 and take the next milestone via `/dots-dev`. Operator closed **M4** (multi-client co-op validated via Unity **Multiplayer Play Mode** — two controllable characters in-scene; full handshake not exercised end-to-end, Relay still deferred). For **M5**, clarified what "home base" means for this game and built the operator-chosen **Option B**: a fixed Base Anchor + planar build-grid coordinate space + spawn re-root, **plus** one shared storage container ghost (deposit/withdraw via RPC). Locked in [[DR-008_M5_HomeBase_BaseLayer_Storage]].
## Process
- **Scoping research workflow** (4 parallel readers → synthesis): design/genre meaning, DOTS/Netcode subscene+streaming feasibility, codebase scene state, persistence/world-arch → a grounded "home base" definition + 3 scoped options (A/B/C). Operator picked **B**; grid **1.0u × 32²** planar; storage = **(itemId, count)**; persistence = in-session only (no disk); one shared base; manifest reconcile folded in.
- **Design/verify workflow** (3 parallel → I synthesized the plan): extracted exact in-repo templates (RPC `GoInGameRequest`, `StatModifier` ghost buffer, `UpgradePickup` server-spawned interpolated ghost, ring spawn, baked-singleton bakers); **context7 was unreachable** so Netcode 1.13.2 shapes were verified by reflection on the installed assemblies + the project's own proven usages; locked the deterministic grid math.
- **Implementation** (sequential via MCP `create_script`/`apply_text_edits` — single editor, domain-reload-ordered; NOT a parallel swarm): components+math → tests → server systems+spawn re-root → client send → authoring → prefab/subscene wiring → runtime validation.
## Done
- **New (`ProjectM.Simulation/HomeBase`):** `BaseAnchor`; `BaseGridMath` (WorldToCell/CellToWorld/IsCellInPlot/IsPointInPlot/ClampCell/PlotCenter — corner-origin, center-returning, half-open, floor); `StorageEntry` (`[GhostField]` buffer, ownerless); `SharedStorageContainer` tag; `StorageSpawner`; `StorageOpRequest` + `StorageOp` byte consts; `StorageMath` (deposit-merge / withdraw-clamp-drop).
- **New (`ProjectM.Server/HomeBase`):** `SharedStorageSpawnSystem` (one-shot, spawns the container ghost at `CellToWorld(cell)`); `StorageOpReceiveSystem` (server RPC apply via `StorageMath`, singleton container, outside the predicted loop).
- **New (`ProjectM.Client/HomeBase`):** `StorageOpSendSystem` (managed `SystemBase`; E/Q keyboard edge + editor-only `Deposit`/`Withdraw` statics → `StorageOpRequest` RPC to the server connection).
- **New (`ProjectM.Authoring/HomeBase`):** `BaseAnchorAuthoring` (bakes `BaseAnchor` from the GameObject position + plot gizmo); `SharedStorageContainerAuthoring`; `StorageSpawnerAuthoring`.
- **Modified:** `GoInGameServerSystem` — spawn ring re-rooted on `BaseGridMath.PlotCenter(BaseAnchor)` with a `TryGetSingleton` fallback. `Packages/manifest.json` — stale 6.6-era pins reconciled to the resolved 6.4.7 lock.
- **Assets:** `Storage.prefab` (duplicated from `UpgradePickup.prefab` → swapped to `SharedStorageContainerAuthoring`, kept the ownerless-interpolated `GhostAuthoringComponent`). `Gameplay.unity` subscene — added `BaseAnchor` (at (0,1,0)) + `StorageSpawner` (→ Storage.prefab, cell (19,19)) authoring GameObjects.
- **Tests:** `BaseGridMathTests` (7) + `StorageMathTests` (8). **EditMode 62/62.**
## Validation
- **EditMode 62/62 green** (+15 vs M5b's 47).
- **Runtime (single in-editor client, 6.4.7):** `BaseAnchor` baked **identically into both worlds**; player **spawn re-rooted** onto the anchor (spawned at (2.5,1,0)); storage ghost **server-spawned + replicated** to the client at (3.5,1,3.5) = `CellToWorld(19,19)`; **deposit (1×5, 2×3)** then **withdraw (decrement + clamp + drop-empty)****server == client buffer** every time, driven through the real RPC→server→replication path; RPCs survived the tick-batching artifact. Console clean of code/Burst/ghost/RPC errors — only the known unfocused-editor tick-batching warning.
## Decisions
- [[DR-008_M5_HomeBase_BaseLayer_Storage]] — home base = baked ghost-free `BaseAnchor` + locked deterministic `BaseGridMath` grid (M6 builds on it) + spawn re-root + one ownerless-interpolated shared-storage ghost mutated by a server-authoritative RPC (singleton-resolved, byte op, outside the predicted loop). Streaming + disk persistence deferred.
## Diagnosis notes (for future me)
- **`execute_code` runs as a method body** — no `using` directives allowed (they parse as statements → "Identifier expected"); **fully-qualify** all types (`Unity.Entities.World`, `ProjectM.Simulation.BaseAnchor`, …).
- **Ownerless interpolated ghost ≠ owner-predicted ghost for buffer replication:** a server-spawned ownerless chest needs **no `OwnerSendType`/`GhostOwner`**`[GhostField]` alone replicates server mutations to all. `OwnerSendType.All` (per `StatModifier`) is only for the predicting *owner* to recompute.
- **RPC > predicted InputEvent for one-off shared-state actions:** reliable delivery meant deposit/withdraw landed even while the editor tick-batched (the M2 one-shot `Fire` InputEvent drops under batching).
- **The editor hung mid-session** (unresponsive bridge: queued commands accepted, pings unanswered) while unfocused — Edit-mode throttles to near-idle when the window lacks OS focus (`Application.runInBackground` only helps in Play mode). Operator **restarted the editor**; it recovered clean. Avoid piling `refresh_unity` calls onto a blocked main thread; wait or ask to focus/restart.
- **`scope=all force` refresh is heavy** — fine on a fresh editor, but it (plus an unfocused throttle) likely contributed to the hang. Prefer `scope=scripts` for code-only changes.
## Open / deferred
- **Option C** (base/expedition subscene split + async `SceneSystem` streaming) — own world-architecture milestone; M6/M7 don't need it.
- **Disk persistence** — nothing to save until M6 produces structures; thin host-only slice afterward.
- **Storage polish** — proximity gate (container has `HitRadius`), real item/UI model, multi-writer ordering beyond first-come.
- **Multi-client storage** — validate two clients see identical shared state (pairs with the deferred M5b multi-client interpolation + M4 two-build tests).
## Next
Begin **M6 — server-authoritative grid build placement via RPC**, reusing `BaseGridMath` (legality + snap) and the runtime-ghost-into-base-cell spawn path from this slice.
@@ -0,0 +1,45 @@
---
id: DR-008
title: M5 — Home-base base-layer (BaseAnchor + build grid) + shared storage container
status: accepted
date: 2026-06-02
tags:
- decision
- netcode
- home-base
- rpc
- ghost
- m5
permalink: gamevault/07-sessions/decisions/dr-008-m5-homebase-baselayer-storage
---
# DR-008 — M5 Home-base base-layer + shared storage (Option B)
## Context
M5's physics half (predicted physics [[DR-006_M5_Physics_In_Prediction]] + Character Controller [[DR-007_M5b_Character_Controller_Package]]) was done; the remaining half — sketched loosely as "persistent home-base subscene that streams in/out" — needed its meaning pinned down. A read-only research workflow grounded "home base" in the [[Pillars]] (co-op ARPG + V Rising-style shared persistent buildable base + Factorio automation; base vs instanced expeditions) and produced three scoped options: **A** anchor + claimed plot + build-grid config + spawn re-root; **B** = A + one shared-storage ghost; **C** = A + base/expedition subscene split + async streaming. The operator chose **Option B** with: planar `int2` build grid, **CellSize 1.0**, **32×32** plot; storage items as **(itemId, count)** pairs; in-session ("survives logout") persistence only — **no disk save/load**; one **shared** world-owned base. Subscene split + streaming (**C**) explicitly deferred — M6/M7 need only the anchor + grid, not streaming.
## Decision
1. **Home base = a baked, ghost-free config singleton + one runtime shared-storage ghost — NOT a building/save/streaming system.** `BaseAnchor` (`IComponentData`, flat/blittable, no entity refs) carries `AnchorPos`, `GridOrigin` (min-XZ corner of cell (0,0)), `CellSize`, `GridDims`. Baked into the gameplay subscene via `BaseAnchorAuthoring` (`TransformUsageFlags.None`; reads the GameObject position as `AnchorPos`, derives `GridOrigin` centered on it). Present identically in both worlds (baked, not replicated) — the same pattern as `PlayerSpawner`/`AbilityDatabase`/`NetCodePhysicsConfig`.
2. **`BaseGridMath` is the locked, deterministic, unit-tested coordinate space M6 builds on.** Corner-origin, center-returning, **half-open** cell bounds, `math.floor` world→cell (negative-correct). `WorldToCell`/`CellToWorld`/`IsCellInPlot`/`IsPointInPlot`/`ClampCell`/`PlotCenter`. M6's server placement handler will call `IsPointInPlot` (legality) + `CellToWorld` (snap) directly. **CellSize/PlotSize are treated as a locked coordinate space** — changing them after M6 builds invalidates placement.
3. **Spawn is re-rooted onto the anchor.** `GoInGameServerSystem` now uses `BaseGridMath.PlotCenter(BaseAnchor)` as the ring center when the anchor singleton is present, falling back to `PlayerSpawner.SpawnPoint` if the base subscene hasn't streamed in yet (`SystemAPI.TryGetSingleton`). Ring radius/slots stay on `PlayerSpawner`. (Anchor placed at the existing spawn plane (0,1,0) so the re-root is behaviour-preserving.)
4. **Shared storage = an ownerless INTERPOLATED ghost with a replicated `[GhostField]` buffer.** `StorageEntry` (`IBufferElementData`, `[GhostField] ushort ItemId` + `int Count`, `[InternalBufferCapacity(16)]`) on a `SharedStorageContainer`-tagged ghost. The container is **ownerless****no `OwnerSendType`, no `GhostOwner`** (those are only for owner-predicted ghosts like the player's `StatModifier` buffer); server mutations auto-replicate to all clients. The container is **server-spawned at runtime** (one-shot `SharedStorageSpawnSystem` reads a baked `StorageSpawner` + `BaseAnchor`, instantiates the ghost prefab at `CellToWorld(cell)`, destroys the spawner) — NOT baked into the subscene (keeps the subscene ghost-free, dodges the prespawn section-ack handshake). It is **not** added to any connection's `LinkedEntityGroup`, so it survives player disconnects.
5. **Deposit/withdraw = an `IRpcCommand`, applied server-authoritatively outside the predicted loop.** `StorageOpRequest { byte Op; ushort ItemId; int Count }` — a one-off action, so an RPC, not a per-tick predicted input. **Op is a byte** (not an enum) to keep the generated serializer trivial / dodge the cross-assembly enum-Burst hazard; **plain blittable fields, no `[GhostField]`** (RPC payloads auto-serialize). **No target entity is carried** — M5 has a single shared container the server resolves as a **singleton** (entity refs aren't stable cross-world; this avoids the ghost-id+spawn-tick `SpawnedGhostEntityMap` lookup, which also keeps the handler Burst-clean). `StorageOpReceiveSystem` runs in the server `SimulationSystemGroup` (NOT the prediction loop → no rollback double-apply), applies via the pure `StorageMath.Deposit/Withdraw`, and destroys the request. `StorageOpSendSystem` (client, managed `SystemBase`) sends on a keyboard edge (E/Q, fully-qualified Input System to dodge the `PlayerInput` name collision) or an editor-only static (`Deposit`/`Withdraw`) for headless validation.
## Consequences
- **Validated at runtime on 6.4.7 (single in-editor client).** `BaseAnchor` baked identically into **both** worlds (AnchorPos (0,1,0), GridOrigin (-16,1,-16), 1.0u × 32²). Player **spawn re-rooted** onto the anchor → spawned at (2.5,1,0) (PlotCenter + ring slot 0). Storage ghost **server-spawned and replicated to the client** at (3.5,1,3.5) = `CellToWorld(cell 19,19)`. **Deposit** (1×5, 2×3) and **Withdraw** (decrement + clamp-to-available + drop-empty-row) applied server-side and replicated — **server == client buffer** in every case. The RPCs propagated correctly **despite the in-editor tick-batching artifact** (RPCs are reliable, unlike the one-shot `Fire` InputEvent that can drop under batching). EditMode **62/62** (+15: 7 `BaseGridMath`, 8 `StorageMath`). Console clean of code/Burst/ghost/RPC errors — only the known unfocused-editor tick-batching warning.
- **Foundation for M6/M7.** M6 grid placement reuses `BaseGridMath` (legality + snap) and the runtime-ghost-into-base-cell spawn path verbatim; M7 production machines generalise `StorageEntry` into input/output buffers + a server tick. The flat, entity-ref-free `BaseAnchor`/`StorageEntry` shapes are serialization-ready for the deferred disk-persistence slice.
- **No new asmdefs.** Everything fits the existing Simulation/Server/Client/Authoring split; all needed references (`Unity.NetCode`, `Unity.Physics`, `Unity.Transforms`) were already declared. New code lives under `…/HomeBase/` in each assembly.
- **Housekeeping:** stale `manifest.json` pins from the brief 6.6 upgrade (entities/entities.graphics 6.5.0, URP 17.6.0, test-framework 1.8.0, ugui 2.6.0, multiplayer.center 2.0.0) **reconciled to the resolved 6.4.7 lock** (a no-op re-resolve; lock unchanged, console clean) — closes the [[CLAUDE]] pending-reconcile TODO.
## Open / deferred
- **Base/expedition subscene split + async streaming (Option C)** — the persistent-space split the locked world design ultimately needs (`SceneSystem.LoadSceneAsync`/`UnloadScene`, per-world load on the listen-server). Deferred to its own world-architecture milestone; M6/M7 don't depend on it.
- **Disk persistence** — runtime structures don't exist until M6, so there's nothing to save yet. A thin host-only per-structure serialization slice (replayed through M6's placement path) comes after M6.
- **Storage interaction polish** — deposit/withdraw is non-proximity-gated and uses a fixed test item; add an interact-range check (the container carries `HitRadius`) and a real item/UI model later. Multi-writer ordering is currently first-come server apply (fine for the prototype).
- **Plot footprint / base visuals** — only an editor gizmo (plot bounds) + a placeholder primitive container; real ground/walls/structures arrive with M6.
- **Multi-client storage** — validated single-client; a live two-build / Multiplayer-Play-Mode check that two clients see identical shared-storage state pairs with the deferred M5b multi-client interpolation test.
Mirrors the server-authoritative + deterministic + co-op pillars from [[Pillars]]. Builds on [[DR-006_M5_Physics_In_Prediction]] / [[DR-007_M5b_Character_Controller_Package]] (kept infra), [[DR-005_M4_Connection_Model_Direct_IP]] (spawn/co-op), [[DR-004_M3_DataDriven_Abilities_Modifiers]] (the replicated-buffer + byte-enum patterns reused here).