# Item `physics` — deterministic PEPE RUN engine Status: **done**. Commit `e6bbada` on `main` (one commit; it touches only `game/**` and `package.json`). ## Entry point - `game/engine.mjs`: rules version `peperun-physics/1`. It has no imports and uses integer math only. - `createRun(seed, royal = false)` takes a uint32 seed. Anything else throws `RangeError`. - `stepRun(state, buttons)` takes a held mask (`1` jump, `2` duck). One call is exactly one 1/60 s tick. It is pure: it returns a new state, and a dead state is returned unchanged. - The state exposes `tick`, `distanceMm`, `speedMmPerSec`, `heightPx`, `ducking`, `alive`, `livesLeft` and `obstacles[{id, kind, xPx, widthPx, bottomPx, heightPx, xMm, worldMm, gapTicks}]`. Each obstacle's x is measured from the player's leading edge. `xMm` is the exact value used for collision, and `xPx = floor(xMm/20)`. - Other exports: - `scoreOf`: whole metres. - `replay(seed, royal, masks)`: returns `{state, ticks, trailingInputs}`. - `serializeState` / `deserializeState` / `hashState`: canonical JSON and FNV-1a 64. - `encodeInputs` / `decodeInputs`: `mask:count` run-length format. - `generateCourse(seed, untilMm)`: the course does not depend on the player's input. - `playerHitbox(skinId 0..20, ducking)`: every skin gets the same box. - `speedAt`, `levelAt`, `overlapsPlayer`, plus all the constants. - `game/ENGINE.md` is the spec for other implementations. It covers units, every constant, the mulberry32 generator and `drawInt`, the course-generation algorithm, the exact per-tick order (input edges → vertical → horizontal → course → collision → tick++), the Royal rule, scoring, canonical serialization key order, the hash, the input text format and the rules-version policy. - `game/fixtures/replays.json` holds 7 headless replays, each with its inputs, an expected final summary and hash, and a state hash every 600 ticks: - idle death - Royal double collision - autopilot to 600 m, then idle - autopilot to 1200 m (at the speed cap) - Royal autopilot, then idle - pseudo-random masks - hold-duck death `game/tools/make-fixtures.mjs` (`npm run fixtures:game`) regenerates them. - `game/test/engine.test.mjs`, `replay.test.mjs` and `reachability.test.mjs` are the tests. `game/test/support/autopilot.mjs` is a reference player used only by the tests and the fixture tool. - `package.json` (root) is a private ESM package with `"engines": {"node": ">=22.13"}` and no dependencies. Scripts: - `test` and `test:game`: `node --test "game/test/**/*.test.mjs"` - `fixtures:game` ## Rules in brief - **Speed:** `speedAt(d) = min(18000, 7000 + floor(d/80))` mm/s. Speed depends only on distance, rises quickly and is capped at 18 m/s from 880 m. Distance accumulates in 1/60 mm, so there is no drift. - **Jump:** - A jump starts only on the rising edge of the jump bit. - A held press made up to 6 ticks before landing still jumps (buffer). - Releasing the button early caps the rise (variable height). - A full jump is in the air for 40 ticks and peaks at 100 px. - **Duck:** - Ducking on the ground is immediate, and so is standing up again. - Jump wins if both bits are held on the ground. - Holding duck in the air is a fast fall, and the player is ducking on the landing tick. - **Hitboxes:** the player is 32 px wide and 44 px tall (24 px when ducking), the same for Classic and skins 1–20. Overlap uses half-open intervals, so touching is not a hit. - **Obstacle kinds:** - Ground: `ground_small` 24×30, `ground_tall` 22×46, `ground_wide` 56×28. Each has to be jumped. - `overhead`: 64×120 with its bottom at 30 px. It can only be passed by ducking. - **Course generation:** - Difficulty rises every 150 m (levels 0–6): new kinds unlock, and the minimum gap shrinks from 72 to 46 ticks. - Gaps are set in ticks at the fastest speed possible inside the gap, so the real gap is never shorter than intended. - Obstacles spawn 1000 px ahead, off screen; the screen shows 800 px. - **Royal:** the run starts with `livesLeft = 1`. The first lethal contact decrements it, records `savedAtTick` and removes the obstacles it overlapped. Distance, speed and vertical motion carry on, and the next contact kills. There is no stacking. ## Checks run (measured results) 1. `npm test` (Node v24.21.0 on this machine; the code uses nothing newer than Node 22): **31 tests, 31 pass, 0 fail**, about 1.5 s. Coverage: - seed and button validation - exact 60 Hz distance integration against a reference loop - speed monotonic, bounded and reaching the cap - scoring - jump edge (holding does not re-jump), apex 100 px and 40 airborne ticks, variable jump - jump buffer: 6 ticks jumps, 7 does not - duck transitions and fast fall - skins 0–20 have identical hitboxes and invalid skins throw - exact hitbox boundaries: overhead cannot be jumped, can be ducked, and cannot be passed while airborne - idle death on obstacle #1 - Royal on 5 seeds: the first contact happens on the same tick and at the same distance as the Classic death, the life is consumed once, the hit obstacle is removed, and the second contact kills - purity - all 7 fixture replays, with per-checkpoint hashes - identical replay, tick by tick - inputs after death are reported - serialization round trip and continued play after restore - input encoding round trip and rejection of malformed text - reachability: - static rules over 1500 seeds × 3 km (>100k obstacles): gap ≥ the level's minimum, all kinds appear - every obstacle spawns off screen and is visible ≥ 30 ticks before contact (30 seeds × 1.5 km) - the reference autopilot survives **2.4 km on 200 seeds without Royal**, reaching the 18 m/s cap - the minimum clearing jump window per speed band is **22–27 ticks** (6→18 m/s); the test requires ≥ 8 2. **Independent re-implementation from `game/ENGINE.md` only** (scratch file in `test/scratch/`, not delivered): it reproduced **all 7 fixtures, every checkpoint hash, 0 mismatches**. This shows the spec is complete enough for other implementations (server and verifier). 3. Scratch timing: the autopilot played 100 seeds × 2 km (826,800 ticks) in 431 ms. ## Needs the merged tree - Other items may add scripts to `package.json`. The `test` script here runs only `game/test/**`, and merges should keep it or chain it. - The server, verifier and practice client should import `game/engine.mjs` and store `RULES_VERSION` with each run. Run `npm test` again after the merge. - Not in scope here, and to be done by other items: signing scores, deriving fair seeds and bot heuristics. Bot heuristics can use the `replay` and `trailingInputs` outputs.