DevLog #10 – A replay is just orders and a seed. Then a soldier takes a step

Every RTS player knows the replay button. Classic RTS replays aren’t videos. They’re a short list: the map, the settings, a random seed, and every order every player gave, each stamped with the moment it was given. Press play and the game simulates the whole match again from scratch, feeding in the same orders at the same moments. If the simulation is deterministic, you get the same match, battle and all, from a small text file. Our seven-minute test match is 23 KB.

The catch is that one word, deterministic. Byzen’s skirmish replays have been exact since this week. This devlog is about everything between “we have the orders” and “it plays back the same”.

dl10 hero
The end of a seven-minute skirmish, rebuilt from 119 orders and a seed. The replay bar (from the game’s Spanish UI) reads 7:00 / 7:00, finished.

Where we started: not deterministic at all

In September we audited the code to see whether replays were possible. They weren’t. We found about a hundred calls to Unity’s global random number generator in gameplay code: AI decisions, fire, wildlife, weather, terrain. Every system drew from the same global stream, so one extra roll anywhere shifted every roll after it. And almost all the logic ran in Update() with the frame’s delta time, so the outcome depended on your frame rate.

So the replay design was put on hold behind two foundation pieces.

  • A fixed simulation clock. The game now advances in ticks of 1/30 of a second, whatever the frame rate. Orders take effect on a tick, never “sometime this frame”.
  • Seeded randomness, one stream per system. Combat, the AI planner, wildlife, weather and so on each get their own generator, seeded from the match seed. By the time we finished, 180 random calls in 54 files had moved over. Each file now carries a header saying which stream it uses.

The AI also had to start playing by the same rules as the player. It used to place buildings and train units by calling functions directly. Now it issues orders like everyone else, so its moves are recorded in the replay too.

How we know it’s the same match

“It looks the same” doesn’t count. Once per second of simulated time, every 30 ticks, the game computes a 64-bit FNV-1a hash of the world state. It walks every entity in a fixed order (faction, then id) and feeds in positions rounded to millimetres and hit points rounded to hundredths. The rounding matters: float noise that changes nothing shouldn’t change the hash.

The recording stores that hash series alongside the orders. During playback, each new hash is compared with the recorded one for the same tick. If one differs, the replay tells you right away. It shows the tick and both hashes, and warns that from that point on, what you’re watching isn’t exactly what happened.

dl10 tickOne tick, in a fixed order. Rendering happens between ticks and only interpolates.

Close isn’t the same: the one-metre drift

By 9 November everything above was done, plus a replay player with play, pause and speeds from ×1 to ×8. Every order was applied. The economy matched. A side that stood still matched.

Units that walked ended up about a metre off.

The culprit was Unity’s NavMeshAgent, the component that walks a unit along its path. It’s good at its job, but it moves units every rendered frame, with its own local avoidance between agents, and none of that runs on our tick. A metre doesn’t sound like much. But a metre decides whether a lancer is in range this second or the next, and from there the whole battle goes its own way.

Our own feet: UnitMotor

So we replaced the agent as the owner of movement. UnitMotor exposes the same interface the agent did (set a destination, has a path, remaining distance, velocity), so the unit code and the animation code didn’t change. Underneath, everything runs on the tick:

  • Paths are requested from the NavMesh inside the tick. At most 48 are solved per tick, always in the order they were asked for. The rest wait their turn.
  • Steps use the fixed delta time. Units stay glued to the NavMesh, with our own braking and a turning limit for cavalry and artillery.
  • Units don’t walk through each other thanks to our own separation pass, on a 4-metre grid. Each overlapping pair is pushed apart by half the overlap, shared out by priority: a unit on the march gives way more than one standing still. The order is always the same.
  • Between ticks the transform interpolates from the previous pose to the current one. You see smooth motion at 144 fps, and the simulation only ever sees ticks.

Dropping Unity’s avoidance brought back an old RTS problem. Send twenty men to the same point and they arrive, push each other, and keep pushing forever, lined up against a wall. The rule now is simple: if a unit is within 4 metres of its target and hasn’t got any closer for a second, it has arrived. Then it lets go of its path, so a crowd at rest stays at rest.

Obstacles get baked, not carved

Buildings, fences, trees and ore veins used to cut holes in the NavMesh at runtime with Unity’s carving, which also happens outside our tick. Now each one registers as a blocker and the NavMesh is rebaked synchronously, inside the tick, when something changes. That costs about 40 ms per rebake. Felled trees and exhausted veins are batched every 10 seconds, so a woodcutter doesn’t trigger a rebake per tree. When the NavMesh changes, every walking unit asks for its path again, in creation order.

The world has to tick too

Units weren’t the only thing living on the frame. The clock and weather, surface water and mud, the river, fire, cattle and herds, boars, fish, wildlife, the night watch and the AI’s resource trickle all ran in Update(). Rain that falls a little faster on a fast machine makes a little more mud, and mud slows units down. So all of them moved onto the tick too, through one SimWorld step. Lighting, shaders and footprints stayed on the frame, because they’re presentation.

That move turned up a small bug. Fish and wildlife share a random stream, and both are created in the same frame when a map loads. Unity doesn’t guarantee which of two objects starts first, so whichever went first took the first numbers. Now the world steps in an order sorted by type and by where each thing was spawned, and that order comes from the seed.

Cattle and wildlife now also simulate across the whole map, not only on screen. That was the only way to keep them in step.

The bug that only happened once per session

With all of that in place, one more difference remained, and it only showed up in the second match of a session. That happens to be exactly where a replay lives: you finish a match, then you watch it.

The cause was one static field. Our round-map code remembers a signature of the deep-water volumes the NavMesh was baked with, so it knows when to rebake. Static fields outlive a match. In the second match of a session, the code compared against the first match’s water, made a different decision, and the NavMesh came out slightly different. Same map, same seed, a different set of walkable triangles. It’s now reset with everything else when a match starts.

It’s the kind of bug determinism work is full of: nothing is random, but something from outside the match’s inputs leaks into the match.

The result

The test: a 420-second skirmish against the AI, with 119 orders and a real fight. Played back, all 420 of 420 hashes match, with no desync and no dropped orders. A 150-second match gives 150 of 150. The whole EditMode test suite passes, apart from one MOBA protocol test that was already failing before this work.

The match was recorded on 10 November. We played the same file again while writing this, three days and several commits later: 420 of 420 hashes, 119 of 119 orders, still no desync.

dl10 replays screenReplays, from the Skirmish screen. Each match is saved automatically when it ends. These are our test runs on one map: about 7 KB for two and a half minutes, 21–23 KB for seven.

What changes when you play

We want to be upfront about this, because it’s more than an invisible engine change:

  • Movement feels different. Without Unity’s avoidance, large groups, cavalry and narrow passes behave in new ways, and they still need proper hands-on testing.
  • Placing a building can cause a ~40 ms hitch, from the synchronous rebake.
  • Cattle and wildlife simulate everywhere, not just where you’re looking.

What’s still missing

  • Ships still move with Unity’s agent, so maps with fleets aren’t exact yet.
  • Networked skirmish isn’t covered. Multiplayer still runs on the agent.
  • Campaign replays don’t exist yet. Missions spawn units from markers whose order isn’t fixed, and that has to be solved first.
  • Two different machines. Everything above is measured on the same machine. Floating-point results can differ between CPUs and builds, and we haven’t proven yet that a replay recorded on one PC plays back exactly on another. That’s the next test.