Replays without recordings
A deterministic game loop in the browser
A Pikefall replay link is about 1,900 characters long and holds a twenty-minute battle. It is not a video and it is not a log of positions. It is the battle's inputs: which map, which hero, which seed, and every order the player gave with the moment they gave it. Opening the link plays the battle again from the start. This post is about the three rules that make that possible, and the ways each of them was nearly broken.
Rule one: time is a step count
The simulation advances in fixed steps of a sixtieth of a second and draws once per animation frame. Game speed does not change the step; it changes how many steps run per frame. So a battle at 3× speed is bit-for-bit the same battle as at 1×, it just shows you fewer of the frames in between.
The tempting alternative is to multiply the time delta by the speed. It looks equivalent and is not: a projectile that moves 3 pixels per step three times is not in the same place as one that moves 9 pixels once, and a collision that happens in the first case can be skipped in the second. Nothing in the simulation is allowed to read the wall clock or the frame's delta for the same reason.
Rule two: one random stream
Every random number the battle uses comes from a single seeded generator (mulberry32) that the level installs when it is built. Critical hits, spell scatter, where a health pack lands and every particle's angle and speed all draw from it. The seed rides in the replay link.
The particles are the surprising part. It would be natural to give visual effects their own generator, but the effects are spawned from inside the simulation step, so the number of draws they take is part of the simulation's state. Add one spark to an impact and every later combat roll shifts by one, and the same seed plays a different battle.
That has a consequence for everything drawn between steps. A "Not ready" caption over a spell button, a ring flashed when you buy something: if those use the same particle recipes, they take numbers from the stream at moments the replay knows nothing about. Our first replays desynchronised for exactly that reason. Feedback drawn by the interface now runs through a small wrapper that swaps in an unrelated generator and puts the battle's stream back afterwards. Sound has its own generator for the same reason, because how many sounds play depends on the audio device.
Rule three: every input goes through one door
Originally the interface called straight into the game: the tower menu spent the gold and then called the upgrade, the wave banner banked the early-call bonus itself. Each of those split an action across two places, and a log of calls would have missed half of it.
Now there is exactly one entry point. An action is an array — the step it happened on, a type, and its operands — and the level applies it, records it, and rounds any position to whole units before applying it, so the recorded order and the applied order are the same order. Refused actions are recorded too: proving that every kind of refusal has no side effect is harder than writing it down.
One more lesson from that door: a rule the menu enforces by not drawing a button must also be enforced by the level. The Iron Challenge forbids selling towers, and for a while the menu simply hid the Sell button. A hand-edited replay that sold a tower anyway was accepted, because nothing below the menu said no.
Fitting a battle in a link
The reference run on the longest campaign map is 287 actions. As JSON,
header and actions deflated, that is 2,734 characters. Replacing absolute
steps with deltas and action names with indices brings it to 2,108. A
small binary format — variable-length integers, positions as zigzag
deltas, ids as indices into a string table — compressed with the
browser's built-in CompressionStream gets it to 1,856, and
the whole link with its preview card to under 2,000 characters, which is
what a Discord message allows without a subscription.
The link puts the run in the URL fragment, after the #,
which never reaches a server. That turned out to need care: the page that
unfurls a link preview redirected to the game with a meta refresh, and a
meta refresh drops the fragment. It now hops with a one-line script that
carries it along.
Checking a run
Verification is the replay with a referee. The link claims an outcome — lives left, kills, gold earned, the step the battle ended on — and the game re-simulates the inputs without drawing and compares. On a desktop that takes about half a second for the longest map.
It compares outcomes rather than a hash of the final state, and that was a measured choice: two builds of the same JavaScript engine agreed on 3 of 16 state hashes and on all 16 outcomes. Details nobody can see are allowed to differ; the result a player cares about is not. The same check is what the speedrun verifier runs in your browser.
Seeking in the replay viewer is also just re-simulation. A level is a graph of entities and closures with no serialised form, and it turned out not to need one: rebuilding a level takes about 50 milliseconds, and a slice of re-simulation per frame keeps the screen moving while it catches up.
When the rules change
A replay is a recording against one set of rules. Change a tower's damage and every link in the wild plays as a different battle. So every link carries a rules revision, a check in the repository fails any commit that changes what a battle does without raising it, and each revision gets a few lines in the public changelog. A link from an older revision is refused with an offer to show what changed, instead of playing a battle that never happened.
None of this needed a server. The simulation runs without a page, which is what lets the same code verify a run in a browser tab, in a command line tool, or one day on a leaderboard.
Play a battle and press Copy link on the results card to see one.