# Add Meta-progression hub to my game

Add the game element below to MY existing game, which you have not seen. It is a finished module file given with this prompt:

- `meta-progression.js` (one global, `GameDNA_MetaProgression`), API reference `meta-progression.README.md`

README example names are placeholders.

## Where it goes in my game (my choice; follow it exactly)

- **Meta-progression hub:** The hall opens at: X on the title and results screens, between runs only. Nothing else opens it except the autoplay path on the results screen.

## Shared premises

1. **Include each module file verbatim; never edit it.** Load it with its own `<script src>` before the game's script, or paste it unchanged into its own `<script>` tag. If a module seems to need a change, adapt my game around it and tell me.
2. **Resolution:** keep my game's native resolution and integer scaling as they are. Call module draw functions on my 2D context inside the same transform as my sprites (one module pixel = one game pixel), and keep my game's own image-smoothing setting (off for pixel art, on for smooth or HD games; never switch it).
3. **Time:** drive every module from one fixed 60 Hz step (1000 / 60 ms). If my loop uses variable delta time, accumulate it and run whole steps (at most 250 ms per frame).
4. **Coordinates:** native screen pixels (world minus camera) unless a section says tiles.
5. **Art and names:** anything new you draw or name is original (no real game, character, place or brand names); English UI text; do not restyle my game.
6. **Hook, don't replace:** add calls or change bodies; never delete or rename a function.
7. **Keep every other caller working.** Find every caller and reader of what you change (turn order, input, moves, menus, HUD): autopilot/AI, test hooks, URL parameters such as `?auto=1`, `?t=` and `?freeze=1`, keyboard shortcuts, end-turn buttons. Update each one. My game's own autoplay must still finish a whole game. Keep existing HUD info (HP bars, max HP) and existing commands.

## Order of work

1. **Read the game first.** Write down (file, function) where these live: loop/update step; draw pass and its order (background, actors, effects, HUD); input; where damage/HP changes are applied; where turns, actions or selections are chosen and played; HUD windows and their rects; camera; battle/scene start and end; and every caller from premise 7.
2. **Check the prerequisites** below against those notes. If one is missing, add the smallest working version and tell me.
3. **Integrate one element at a time** (foundations, systems, UI, effects): (1) Meta-progression hub [ui].
4. **After each element, check the game still runs:** `node --check` each touched script (inline: copy to a temp .js), reload if you have a browser, run its self-check. Then the next.
5. Then the cross-element wiring and the merged checks.

## Prerequisites my game must already have

- **Meta-progression hub:** the one place a run starts: beginRun(), applying the returned bonus only when fresh (base + bonus); the one place a run ends: endRun(lines) with the run's own numbers (kills, level, win); a between-runs screen and key (title / results) that calls openHub(); keys and pointers routed to it while isOpen(); a fixed 60 Hz update step.

## Element 1: Meta-progression hub (`meta-progression.js`, `GameDNA_MetaProgression`)

The between-runs layer of a roguelite: currency earned every run from the run's own numbers, a hall between runs with a permanent unlock tree (costs, prerequisites, ranks, a lit-up animation when a node is bought), the summed bonuses handed to the next run exactly once (`beginRun().fresh`), a run-end summary that counts every line, the total and the wallet up, and a run-start banner. The module enforces the rules and saves progress (own checksummed storage key, export / import code, or the game's save via `serialize()`); the game only applies the bonus and reports the numbers.

**The hall opens at: X on the title and results screens, between runs only. Nothing else opens it except the autoplay path on the results screen.**

**Parameters chosen:** `theme` = ember, `costScale` = 1, `layout` = auto.

**Where it goes** (by role; find these places in my game):

1. *First, read the game:* Find every path that starts a run (start key, restart, autoplay timer; new-game code that ALSO runs at load does not count), the one place a run ends with its numbers (kills, level, win), the title / results screens, the fixed step, the draw pass, the key and pointer handlers, the autoplay flag and the HUD rects.
2. *Design the tree (data):* 6-8 nodes `{id, name, icon, cost, col, row, requires: [ids], effects: {key: amount | [per rank] | 'id'}, text: '+{value} ...'}` with original names: effects the game can apply at a run start (max HP, speed, reach, a starting item or weapon, a deck change) and one `earn` node (reserved key: +% currency, applied by the module). Root cost about what one run earns x 1, rising along the tree.
3. *Once, after the canvas exists:* `meta = GameDNA_MetaProgression.create({view: {x, y, w, h} (logical), key: '<short-unique-game-name>', nodes, labels: {maxHp: 'max HP', ...}, params: {title: 'THE EMBER HALL', currency: 'Embers', buyVerb: 'KINDLE', theme: 'ember', costScale: 1, layout: 'auto'}, autoplay: () => <autoplay flag>, uiRects: () => <HUD and panels>})`.
4. *Where a run starts (the one real place):* `const r = meta.beginRun(); if (r.fresh) <apply r.bonus as base + bonus>`. If the run setup reads its config before that point, write that config from `meta.bonuses()` as absolute values before the run is created (and after each purchase), and still call `beginRun()` at the start.
5. *Where a run ends (once):* `meta.endRun([{label: 'Foes felled', count: kills, rate: 0.05}, {label: won ? 'Victory' : 'Survived', amount: won ? 30 : 10}, ...])`.
6. *Between runs:* X on the title and results screens -> `meta.openHub()`; `meta.showEntry(true, {x, y})` on those screens (hide it in a run). While `meta.isOpen()`: every key to `meta.key(k)` (keep the autoplay toggle), pointer downs to `meta.pointer('down', x, y)`, then return. Under autoplay: open it once on the results screen after `meta.summaryDone()`; its bot spends and closes it before the game's autoplay leaves that screen. Hide the summary (`meta.hideSummary()`) when leaving the results screen.
7. *Fixed step and draw:* `meta.update(stepMs)` once per step in every scene (fast-forwards included); `meta.draw(ctx)` last in the draw pass.

**Common mistakes** (symptom -> fix):

- The bonus doubles or grows each run -> applied with `+=` on something that survives the run, or on every `beginRun()`: apply inside `if (r.fresh)` as base + bonus.
- Run 1 after a reload has no bonus -> `beginRun()` also ran at load (new-game code before the title), so the real start got `fresh: false`.
- Currency paid twice or not at all -> `endRun()` in two places, or no `beginRun()` for that run (it returns null).
- The next run starts behind the open hall, or the hall eats the start key -> open it only between runs; `beginRun()` closes it; route keys to it only while `isOpen()`.
- Autoplay stalls on the results screen -> the hall was opened without autoplay passed to `create`; its bot only runs when `autoplay()` is true.
- The summary covers the result screen -> pass `uiRects()` with the result panel; the module places the summary clear of it (narrower when crowded).
- Stills change between loads -> do not read the progress yourself on a `?t=&freeze=1` page (the module never does).

## Merged acceptance checks (screenshot if you can; otherwise describe the trace)

- [Meta-progression hub] A run ends: the summary counts each line, the total and the wallet up; X on the title and results screens opens the hall; a sealed node shows its requirements, a short one says how much is missing, buying lights it and its links and charges exactly the cost.
- [Meta-progression hub] The next run starts with the bonus once (base + bonus, not base + 2 x bonus); reload keeps the progress; export / import a code works.
- [Meta-progression hub] `?auto=1` still plays to its end (and spends between runs); `?t=<ms>&freeze=1` unchanged; no console errors.
- [combined] My game's autoplay (if any) still finishes a whole game; a full battle or turn cycle and a restart run with no console errors; at 500 px wide, no horizontal scroll.

## Self-check without a browser (report each)

- Each module file is byte-identical to the one given: `meta-progression.js` sha256 `6a1aab04db15247b...` (`shasum -a 256`).
- No function removed or renamed (compare name lists before/after).
- Every identifier you call exists where called; each instance exists before first use.
- [Meta-progression hub] Grep `beginRun(`: one call, where every run starts, bonus applied inside `if (r.fresh)`; grep `endRun(`: one call per run end.
- [Meta-progression hub] Worked example: 1044 kills x 0.05 + 30 = 82 (102 with a +25 % earn node); buy a 20-cost root -> wallet - 20; next run max HP = base + 20.
- [Meta-progression hub] `update(` every step, `draw(` last, `openHub(` only between runs, keys to `key()` while `isOpen()`.
- **Mandatory traces** (write each step: function, values): (1) one full autoplay battle or turn cycle through the new code, from start to the next turn or the end; (2) one enemy phase or enemy action. If my game has no autoplay, trace the same path from player input.
- Say how each caller listed in step 1 still works.
- Report which of my functions you changed for each element, and anything you could not do.
