Games rebuilt for the browser · Part 1 of 2
Replaying a village raid
A replay of a raid in Crown & Clan is under 10 KB. It isn’t a video, and it isn’t a list of where every unit stood. It’s the village, your taps, and the length of every simulation step, and the game plays the whole battle again from scratch. That only works if the simulation comes out identical every time, in every browser, down to the last bit of every number.
Crown & Clan is a village-building and raiding game in the mould of Clash of Clans, written in TypeScript and Phaser with no server behind it. Every battle in the figures below was simulated by the game’s own engine in Node, on the twelve campaign villages the project designed. The figures draw plain shapes instead of game art.
A simulation with no screen
One rule governs the whole 66,000-line codebase: GameModel may never import Phaser, a scene class, or any rendering data. Fifty-three files in the repository touch Phaser, and none of them can be reached from the simulation.
During a battle, the Phaser scene steps GameModel forward in short time steps, normally 0.05 seconds (twenty a second), and draws units part-way between two steps so movement looks smooth at any refresh rate. The model never knows frames exist. Because it’s plain TypeScript, the same class runs live raids in the browser, checks replays in CI, and produced every figure on this page from a Node script.
Where you attack from
Once deployed outside the shaded zone, every unit follows a greedy rule for choosing a target. Barbarians and Archers march to the nearest building of any kind. Giants ignore resource huts and head for the nearest defense. That makes your landing point decisive: it decides which defenses your Giants meet first and how long the damage dealers behind them survive.
Deploy the same 42-troop army (6 Giants, 4 Wall Breakers, 16 Barbarians, 16 Archers) from twelve points around Ember Keep and the outcomes split completely. From the northeast, the army strings out into the defenses one at a time and dies at 37% destruction with no stars. From lower down the same side, four of the six Giants survive into the core and the raid takes all three stars in 42 seconds. The stars are scored like this:
b.stars = Number(b.destruction >= 50)
+ Number(townHallDead)
+ Number(b.destruction === 100);
Routes through walls
findPath. Red: walls a route breaks through.Ground units find their way across the 48×48 tile grid with A* search, moving in four directions. Buildings are solid. Walls are passable at a price: a step onto grass costs 1, and a step through a wall tile costs 7. So a troop will walk up to six extra tiles around a wall before deciding to stop and batter through it, and it plans again whenever a building or wall segment falls.
Giants only target defenses, and Ember Keep’s defenses sit inside stone compartments, so 20 of the 64 starting points send Giants straight through a wall. Barbarians settle for the closest building of any kind, which from the edge of the map is always an outside building, so their first routes never break a wall at all.
One army, twelve villages
Because GameModel runs without a screen, balancing the twelve-stage campaign is a loop in Node: send test armies at every village and read the scores. Deployed from the west, the small starter army fades after the fourth village, while the medium ground force holds three stars through village eight. The air army of Balloons and Dragons flies over every wall and clears the first eight villages as easily as the ground army does, then falls off sharply on the Expert stages, as soon as villages gain a second Air Defense.
Every step, recorded
Back to the replay. It saves the starting setup, your timestamped taps, and the size of every simulation step: 45 actions and 317 steps for the 16-second raid above, under 10 KB in all.
Why record step sizes, if the scene normally steps 0.05 seconds? Because they aren’t always the same (a practice tutorial, for one, slows a battle down by shrinking them), and a different step size gives a different battle. The two boards above run the same taps at 0.05 s and 0.1 s steps. Halfway through the raid, the two battles have already gone different ways.
Steps are the easy part. The harder part is making each step produce the same numbers on every machine:
- No ambient randomness. Combat never calls
Math.random. When a rule needs chance, it hashes a fixed battle seed with a counter, so the “random” result is the same on every replay. - No browser-dependent math. Distances are computed with explicit arithmetic instead of
Math.hypot, which browsers are allowed to round differently in the last few bits. That’s enough for a projectile in Safari to land one tick later than in Chrome, and from there the whole raid forks.
Get those right and a 10 KB log is a complete battle. It’s also why everything else on this page was cheap to make: the landing points, the 64 routes, and the campaign sweep are all the same simulation, run in a loop with no screen.
Crown & Clan is an independent fan project. As its README notes, "Clash of Clans assets and trademarks belong to Supercell," so the figures here use plain shapes instead of game art.