Skip to content
Ayotl

← All notes

October 20, 2026 · In Kazan

An online strategy game on a few bytes per tick

The question was whether a game of cities and armies could run in real time from a phone, with several people on the same map. It can, with an idea from 1996: you don't send the world, you send the orders, and every phone simulates the same thing.

The first instinct is to send the state: where every soldier is, how much health every house has. With two hundred units ten times a second that's hundreds of kilobytes per second per player, and on a phone on mobile data that doesn't hold. The alternative is the one strategy games used in the nineties: every client runs the full simulation, and the only thing that travels is «on tick 1,240, player 2 sent these troops to that tile». A few dozen bytes.

For that to work, the simulation has to be deterministic: same orders, same result for everyone, always. That imposes awkward rules. No Math.random: randomness comes from a generator of our own kept in the state. No Math.sin or Math.atan2, which one engine may round differently from another; only sums, products and Math.sqrt, which the standard forces to round identically everywhere. And always walk the lists in the same order.

// Dos partidas con la misma semilla y las mismas órdenes acaban idénticas,
// y una copia serializada a medio camino sigue igual que el original.
const a = partida(3000, -1), b = partida(3000, -1), c = partida(3000, 1500);
if (a !== b || a !== c) process.exit(1);
The test that runs before every rules change.

The server doesn't simulate for the others: it only keeps time. Every 100 ms it closes a tick, takes the orders that arrived, stamps them with the tick number and hands them out. Every 5 seconds it sends a state digest (a hash of healths, positions and resources); if a client disagrees, it asks for the full state and carries on from there. Whoever joins mid-match receives that full state once. And the computer rivals play from the server: their orders travel like anyone's.

What this buys is more than it seems. While you're connected, it's real time: you see the enemy arrive and send your people. While you're away, your city defends itself, because the simulation doesn't need the player. And one tick of a four-player match costs five hundredths of a millisecond on the server: one process can hold hundreds of islands.

What still costs is what each phone simulates. With four or eight players there's plenty of headroom; for twenty you'd have to stop checking every enemy unit on every tick and split the map into zones. For now the island holds four, and the volcano makes sure no match lasts forever.

All notes