# Tiny Adventure

A seeded tabletop campaign: a Go server owns every rule, a Three.js client in `web/` renders it. API in `docs/api.md`, harness in `docs/verification.md`, player-facing rules in `README.md`.

## Layout

- `cmd/server`: flags and boot.
- `internal/model`: wire and state types only. No logic beyond tiny helpers.
- `internal/rng`: the one RNG (`math/rand/v2` PCG), `Sub(seed, tag)` streams, `Pick`, `Shuffled`, `Between`, six-digit `NewSeed`.
- `internal/world`: hex math, noise, terrain bytes, pathfinding.
- `internal/gen`: builds a `World` from a seed: terrain, factions, POIs, spawns, bands, loot, landmarks, missions, party, encounters.
- `internal/sim`: game rules that are pure functions: classes, roles, ability pools and growth (`classes.go`), the ability catalog and `Draw` (`abilities.go`), items (`BuildItem`, `RollItem`), traits, guards and `EdgeOf`, the battle loop (`combat.go`), damage and healing (`damage.go`), one-turn effects (`effects.go`), targeting and ability choice (`choice.go`), `Forecast` odds, XP.
- `internal/server`: HTTP and the stateful game: move, bands, hazards, landmarks, clock and camping, shops, threat view, fights. `actions.go` is the one switch every POST goes through, `store.go` the per-game action logs and replay, `server.go` players, seats and routes, `limit.go` rate limits, `feedback.go` its gate, `run.go` and `card.go` the share page and PNG card (fonts in `internal/server/fonts`, lining numerals frozen in).
- `web/js`: one concern per file. `main.js` wires the flow. `kit.js` is the canvas-texture UI layer, `frames.js` its widgets, `text.js` fonts and text, `theme.js` colour roles, `layout.js` widget rects. Board and art: `board.js`, `flora.js`, `props.js`, `figures.js`, `minis.js`, `beasts.js`, `sky.js`. Feedback: `combat.js` plays the server log one turn at a time, `levelup.js` celebrates level gains above the hero cards, `route.js` draws the `/api/path` preview, `playback.js` applies one combat event to shown hp (shared with the harness), `abilities.js` names an ability from its id, `medallions.js` renders each hero's mini into its card. `life.js` is wind, banners, smoke, fireflies and birds. `feedback.js` is the feedback form. `screens.js` the title and end cards.
- `deploy`: the systemd unit and Caddy block for tiny-adventure.xonecas.io. Steps in `docs/deploy.md`.
- `scripts`: the harness. `serve.sh` starts a server, `driver.mjs` drives the browser, `bot.mjs` is the careful player policy shared by verify and playthrough, `fights.mjs` replays fight logs through `web/js/playback.js` and checks them against the server, `layout.mjs` checks rects.

## Commands

- Play: `go run ./cmd/server --seed 7`, then open http://localhost:8080.
- Check: `go vet ./... && go test ./...`.
- Harness: `scripts/verify.sh <outdir> [seed]`, `scripts/playthrough.sh <outdir> <seed>`. `AGENT_BROWSER_HEADED=0` keeps the browser off the desktop. Use it when the user is at the machine.
- Frame time: open the game in a normal GPU browser and read `__game.perf()`. The harness uses SwiftShader, so its frame rate means nothing.

## Rules

- No code comments.
- The server decides; the client renders. Odds, wounds, edge, clock, shop stock and every rule live in Go. The client may format and aggregate for display, never re-derive a rule.
- One generator per thing: classes, items and abilities live in `sim`, worlds in `gen`, randomness in `rng`. Do not add a second.
- All game UI is drawn on canvas textures through `kit.js` and `frames.js`: HUD, panels, buttons, menus, title and end screens, floating numbers. The only DOM elements are the canvas and the `#fatal` fallback.
- Text goes through `drawText` with a box height `h`: it centres the cap height in the box. Never place text with magic offsets. MedievalSharp (`display: true`) is for titles, headings, place labels and odds words only; Alegreya with lining numerals is the body. Fonts are vendored in `web/fonts` and load before the first draw.
- Colours come from roles in `theme.js`: a grey ramp, one amber accent for state and the primary action, red only for danger. Radius at most 4. No gradients.
- The one exception to canvas-only UI is the hidden `textarea` in `feedback.js`. It takes the typing, including paste, IME and mobile keyboards, and the form is drawn on canvas. Add no other DOM.
- A game is its seed plus its action log. Every rule must give the same result when the log is replayed: no wall clock, no `math/rand` outside `rng`, no map iteration order in rules. The action's own time is `g.now`.
- Bump `model.Version` in any change that alters what a seed generates or how an action resolves. Saved boards from another version, and any log that fails to replay, give the player a fresh campaign. The old log stays on disk.
- `window.__game` (`web/js/debug.js`) must keep working: `mode`, `busy`, `state()`, `rects()` via `setRects`, `tileScreen`, `focusTile`, `pickAt`, `perf()`. The harness reads layout only from `rects()`.
- Scripts import `web/js/stats.js` and `web/js/hex.js`; keep those free of browser APIs.
- Generated art: the `openrouter` skill can make images when a texture or icon is needed.

## Gotchas

- Instanced board meshes start with zero matrices, so their bounding spheres are degenerate. Keep `frustumCulled = false` on them.
- Defeat ends the campaign. Any harness setup that fights through the API can end the run; pin the seed and check `phase`.
- Never kill processes with `pkill -f`; it matches the agent's own shell. Find a server by port: `ss -ltnpH 'sport = :PORT'`.
- Seeds above 2^53 lose precision in JS. `rng.NewSeed` stays below a million.
- Balance is measured with the careful bot over seeds 1 to 20 through the API. Expect about 19 wins in about 12 days. `TestCarefulPlayWinsMostCampaigns` is the fast Go guard.
- The client walks one hex per `/api/move {steps: 1}` so the player can change course. Keep `track.busy` true for the whole walk; the harness waits on it.
- Every request without a player id gets a new board. The harness sends one id per process as `X-Player` from node and plants it as the `ta` cookie in the browser (`driver.mjs`). A script that calls the API without it plays alone.
- The client reads each combat event's `kind` from the server. Never classify an event from the unit's own ability list: gear grants abilities too, and a heal played as damage shows a hero hit by a teammate.
- Rate limits skip loopback requests without `X-Forwarded-For`. Caddy always sets that header, so public traffic is limited and the harness is not.

## Definition of done

"Done" for any change that touches gameplay, rendering or UI requires all of:

1. `scripts/verify.sh <outdir>` was run on the final code and its output is attached. Fix every failure or list each one as known and explained.
2. A human-style review of the captured frames: open the PNGs (and the recording) with the image reader, and write down what looks broken, ugly, clipped, overlapping, unreadable or empty. Say it plainly. Stating "looks fine" without naming what you checked is not a review.
3. The interaction log in the report shows the game state changing after inputs (party moved, fight resolved, panel opened, end screen reached).

One screenshot is never evidence that something is done. A screenshot of a broken UI that is called a success is a failed task. See `docs/verification.md`.
