fuzzyworld

explodorama / files / src / sim / record.ts

record.ts

1.4 KB · history · blame

1import { newRun } from './state'
2import { step } from './turn'
3import { DEFAULT_OPTIONS, type Action, type ChargeId, type GameEvent, type GameState, type RunOptions } from './types'
4
5/**
6 * Bumped whenever a rule change would make an old record replay differently. Records from
7 * another version can't be trusted to replay (PLAN.md §11).
8 */
9export const SIM_VERSION = 43
10
11/** Everything needed to reproduce a run exactly: mid-run saves, replays, certificates. */
12export interface RunRecord {
13 simVersion: number
14 seed: number
15 startLevel: number
16 /** The charge carried from the start (testing aid, `?charge=`; null in a normal run). */
17 startCharge: ChargeId | null
18 /** Board size and modes, chosen when the run started. */
19 options: RunOptions
20 actions: Action[]
21}
22
23export function newRecord(seed: number, startLevel = 1, startCharge: ChargeId | null = null, options: RunOptions = DEFAULT_OPTIONS): RunRecord {
24 return { simVersion: SIM_VERSION, seed, startLevel, startCharge, options: { ...options }, actions: [] }
25}
26
27/** `onEvent` sees every event on the way, for the UI to rebuild what it remembers about the run. */
28export function replay(record: RunRecord, onEvent?: (e: GameEvent) => void): GameState {
29 let s = newRun(record.seed, record.startLevel, record.startCharge, record.options)
30 for (const a of record.actions) {
31 const result = step(s, a)
32 if (onEvent) result.events.forEach(onEvent)
33 s = result.state
34 }
35 return s
36}