Devlog · · Dead Mileage
Under the hood: live streaming, ghosts and races
For the past few days we've been building live spectating into Dead Mileage: watching other drivers' runs as they happen, racing ghosts on the daily road, and races where everyone starts at the same moment. It's running now on a test version of the game with servers of its own, apart from the real one, while we try it out. When it's ready it moves into the game itself, and that day gets a post of its own.
This post is the technical one. It's about how it works, what we measured, what went wrong on the way, and, because all of this means sending things over the internet, exactly what data it gathers, what happens to that data and where it's kept. It's long.
The idea: send the driving, not the picture
The obvious way to let people watch a game is to stream video of it. That costs real money per viewer, and it would show you a flat picture of someone else's screen. We went the other way: the viewer's game plays the run itself.
A run of Dead Mileage is decided by three things: its seed (which builds the road and drives every random roll), what the player had going in (vehicle, workshop perks, difficulty), and what the player does on each tick. Send those, and a viewer's game can play the exact same run: the same monsters doing the same things, in the viewer's own graphics settings. Our first estimate for streaming the full state of every monster on the road was several kilobytes a second. The driving alone is about a quarter of a kilobyte a second.
That only works if the game does exactly the same thing every time it's given the same inputs, on every machine and in every browser. It didn't. So that came first.
Making the game repeat itself
- A fixed clock. The game used to step its world by however long each frame took: 1/144 s on a 144 Hz screen, 1/30 s on a struggling laptop. Now the world steps in ticks of exactly 1/60 s, whatever the screen does. A frame runs as many ticks as the time since the last one holds, and the picture is drawn between the last two ticks so it stays smooth at any refresh rate. A frame that lands within half a millisecond of a tick takes it, so a 60 Hz screen gets one tick every frame instead of zero and then two.
- One input frame per tick. The world no longer reads the keyboard, gamepad, touch screen or mouse directly. Each tick it gets one input frame: throttle and steering, each rounded to 1/127 of full, brake, nitro, tow, presses for the grenade, the ability and the cheer, and the grenade's aim as a point on the ground to 10 cm. A key pressed between two ticks waits for the next one, so one press is one action at any frame rate.
- Seeded randomness. Every run now has a seed; before, only the daily challenges did. Everything that affects the game draws from seeded generators, and anything purely cosmetic (sparks, smoke, the shake of the camera) draws from its own, so a viewer with smoke switched off still gets the same monsters. A check reads the game's files to make sure nothing breaks that rule.
- Our own maths. This was the subtle one. Browsers don't agree on
sin,cos,atan2,expandpow: they can differ in the last digit, and Firefox and Safari use different maths libraries from Chrome and Edge. A last-digit difference in a monster's turn grows into a different horde a minute later. The game now has its own versions of those functions, ported from the standard fdlibm library and built only from adding, multiplying, dividing and square roots, which every browser does identically. Even the positions of gun muzzles and the trailer's hitch, which came from the 3D engine's own maths, are now worked out by the game. - Things that leaked. Hunting down the last differences turned up some odd ones. The order in which a monster's two collisions were added up depended on the graphics preset, because Medium and up draw roadside wrecks in a batch of their own. The buddy's last stand was timed by when its radio line finished playing, so it depended on whether the sound had loaded. A few things carried over from the run before, like which oil slick a monster slipped on. Monsters weren't allowed to appear in view of the camera, but the camera shakes. Each now has its fixed version: collisions sorted by place, the last stand timed by the line's written length, a clean start for every run, and a steady camera of the game's own for deciding where monsters can appear.
The test: a 7-minute run driven by our test bot on Medium (4.2 km, 681 kills, 142 monsters on the road at the end), then played back from its inputs alone on Extreme at 144 frames a second, at 30, with deliberately jittery frames, and in Firefox. The game's state is fingerprinted every second, and every fingerprint matched in every case. Safari isn't tested yet.
Replays: a run as a file
With a repeatable game, a run is just a small file, and every run you play is now recorded on your device. A replay holds:
- A header: the run's id, the game's build, the seed, the rules, and the kit you brought: difficulty, workshop perks, which vehicles you'd opened up, your starting vehicle, your achievements and which buddy rode along.
- The input frames, packed so that a run of identical frames is one entry.
- Commands: what changes a run between ticks without being driving, such as a garage button, leaving the garage, a tow from the pause menu, or the daily road's wrecks arriving from the server.
- The car's track, ten times a second, and a fingerprint of the whole game state, once a second.
The recorder writes a batch every 30 ticks, which takes about three millionths of a second. A run comes to about 240 bytes a second with a keyboard, about 420 with a gamepad's analog steering, and about 730 when you aim the grenade launcher with the mouse. A 6-minute run is 86 KB. My replays keeps your last 20 runs, plus any you star, in your browser's private file storage. A replay only plays on the build that made it, so the game keeps nothing old around.
Live: a relay that doesn't play the game
A live stream is the same file, sent while it's written. It goes through a small server we call the relay, which runs on Cloudflare's Workers and Durable Objects: tiny programs with their own storage, each running in one place at a time. The relay never runs the game. It only passes things on and keeps count.
- A room for each live run. When a run starts with streaming switched on, the game opens a WebSocket to that run's room. It sends the header, and a progress report every 30 seconds for the Live now list. Its driving goes out only while someone is watching: two batches a second.
- Joining halfway. A viewer arriving at minute ten can't play ten minutes of run to catch up. We measured: the game runs about 18 times faster than real time on a fast PC with nothing to draw, which makes 20 to 35 seconds of waiting, and minutes on a phone. So when a viewer joins, the room asks the player's game for a savestate, the whole state of the game at the start of a tick. It's 16 KB with 11 monsters on the road and 147 KB with 250, takes about a millisecond to make, and about 0.4 s to load, mostly rebuilding the terrain. The viewer starts from it and plays on from the batches after it.
- Staying in step. The viewer keeps 150 ticks (2.5 s) in hand and plays at between 0.92 and 1.25 times speed to keep that buffer. Every second it compares the game's fingerprint with the player's. If they ever differ, it asks for a fresh savestate, at most once every ten seconds.
- The lobby. One more Durable Object keeps the Live now list, counts usage, and enforces the limits: 100 viewers a room, 30 new rooms a day from one internet address, and a budget guard that says "unavailable right now" before the day's traffic gets near what Cloudflare's free plan allows. A stream that's refused or dropped never stops the game: you just aren't live.
The free plan's tightest limit turned out to be compute time, not requests. A watched room gets a message every half second, so it never gets to sleep. That's about 460 GB-seconds an hour, which works out at roughly 28 watched hours a day on the free plan. We'll know the real number once more people use it.
Ghosts
A ghost is the car's track from a run, without the game around it: its position five times a second, its heading, pitch and roll, and a few flags (in the air, nitro, rolled, wrecked, guns firing). That's about 11 bytes a sample, so a 15-minute run comes to about 50 KB. On screen, a ghost is placed by tick rather than by the clock, so pauses and garages don't throw it off, and it's drawn smoothly between its samples. Ghosts are see-through and never solid: a run raced against a ghost replays with every fingerprint matching, so the ghost provably didn't touch it.
When a daily run ends, its ghost goes to the relay if it would make that road's top 20, one per driver. Before it's kept, the track is checked. It has to start at the start of the road, never jump more than 2 m in a tick, average no more than 40 m/s over the whole run (the fastest vehicle tops out at 26), and actually go as far as it claims. Your game checks the ghosts it gets back the same way.
Events: everyone starts at once
An event is one room holding many runs. A heat starts when four drivers are in, or a minute after the first if at least two are. The Friday Night Run starts every hour of Friday night, from 11:00 to 04:00 UTC, with up to 16 drivers each time. Everyone in the lobby sees the same countdown, because each game works out its offset from the relay's clock. The seed arrives with GO, so nobody sees the road before the start.
- Racing: everyone's car track is passed to everyone else, and your rivals are ghosts in their own colours. Their samples arrive about a second late, so each one is carried on along its last heading for up to 1.5 s.
- Watching: one runner's game is played for real, from a savestate, and everyone else is a ghost on that road at the same tick. Because they all started at the same GO, their ticks line up. The camera can follow anyone. On AUTO a director picks: the leader, unless someone else has had twice the leader's gunfire for at least a third of the time, and each pick is held for at least 15 seconds.
- The replay: the whole event is one file: every runner's driving, plus a savestate for each runner every 60 seconds. Switching to another driver in the replay starts their game from their nearest savestate and fast-forwards to the moment on screen. A heat of three, gzipped, came to 16 to 18 KB.
Checking every result
A race is only worth winning if everyone drove the same game. Because a run is its inputs, it can be checked by playing it again: if the result isn't what those inputs make, something was changed.
That check needs the real game, and re-playing a 15-minute run takes 30 to 90 seconds of computer time, while a free Cloudflare Worker gets 10 milliseconds per request. So the checks run on a trusted machine of ours. A small program asks the relay which finished events haven't been checked, downloads each replay, plays every run in it in a hidden browser as fast as it goes, and posts a verdict for each runner.
- ✓ checked: every fingerprint matched, and the run ended the way its inputs make it end: the same outcome, distance, kills, cash and time.
- ⚑ flagged, with the reason: out of step (the game it ran was changed), a forged end, a standing it didn't drive, the wrong rules or the wrong road, or test cheats switched on.
- ? unverifiable: made with another build, or with inputs missing because the connection dropped.
In our test heat, one runner played straight and three cheated. One had its game changed to top up its cash and hull every half second from 8 s in: flagged out of step at tick 540, the first fingerprint after the change began at tick 480. One sent an end 1 km further than it got, and one ran in god mode; both were flagged. A flagged run is never removed: it stays in the replay with its ⚑ and the reason, and a person decides what happens next. The check proves a run is what its inputs make on the real game, which also means a bot that plays honestly through the controls would pass. Checking a 16-driver Friday Night Run takes about 15 minutes.
The test round
All of this now runs on a test version with its own servers: its own copy of the game's API and database, its own relay, and its own storage for replays. It isn't linked from anywhere, and search engines are told to stay away, so nothing in the real game is affected while we try it. A Test Run event starts every hour, so we don't have to wait for Friday.
The first round, on two PCs and a phone, found real problems. A heat's drivers weren't shown as live. A run that ended while nobody was watching stayed on the Live now list for a minute and a half, because its end was only sent to viewers; now the end always goes out. And the main menu, built for one start button, didn't do the feature justice. It now has a Live now panel and an Events panel beside the start buttons, and while you're live your HUD shows a red LIVE badge with how many are watching. An automated test now goes through the whole test site in about five minutes, playing as a player would: the servers, a streamed run watched from a second browser, a daily ghost going up and coming back to another driver, a heat with a viewer, and the checks on that heat.
Writing the data section of this post turned up two more. The live rooms kept a few facts about every streamed run, its header among them, with no end date; now each room deletes everything it kept eight days after its run. And the game loaded its 3D engine, three.js, from a public library host, unpkg.com, so unpkg saw the internet address of everyone who played. Now three.js is built into the game itself, and the build checks that nothing is loaded from any other site.
Your data: what's gathered, how it's handled, where it's kept
None of this needs an account, and there are no names, email addresses, ads, cookies or tracking scripts. The game keeps your progress in your browser's own storage, and this site remembers only the language you picked. Everything below is gameplay, and every kind of it has its own switch in the game's Settings › Sharing, with Share nothing to turn them all off.
Your driver number
The first time the game loads, it makes a random number and keeps it in your save. On the daily road, in ghosts, in the live list and in events, that number is all anyone sees of you. It's made on your device and says nothing about you.
What's gathered, and where it goes
- Run stats (on by default): when a run ends, its numbers go to the game's API and into a database table called
runs. That's the difficulty, how far Haven was, how the run ended, distance, time, kills, cash earned and left unspent, vehicle, what did the most damage, garages visited, which run it was for you, whether the buddy was on, whether you played on a touch screen and whether it was a daily. With it go the run's id, the build, the seed, the rules and when it started. Your driver number goes only on a run you streamed, and a key to its replay only on a run someone watched. These are kept with no end date, for balancing the game on real runs instead of guesses. - Daily wrecks (on by default): when a daily run ends in death, the API stores where on that day's road you died, your last 12 seconds of driving, your vehicle, kills, time, driver number and the warning you picked. Tipping your hat to someone's warning stores one anonymous tap. Wrecks are kept, and shown only on their own day's road.
- Ghosts (on by default): a daily run's track goes to the relay, which keeps the best 20 per road, one per driver, in a table called
ghosts. They're deleted after two days. - Live streaming (off until you switch it on): while a run streams, the relay's lobby lists your driver number, vehicle, distance and how many are watching. A run that goes quiet for 90 seconds drops off. While someone watches, your run's inputs, car track and fingerprints pass through its room to the viewers without being stored. The room does keep the run's header (with your kit and driver number) and a few facts about the run, such as when it started, whether anyone watched and where its replay went, in its own storage, so a late viewer can still see how it ended. Eight days after the run started, the room deletes all of it. When a watched run ends, its replay is saved as a file in Cloudflare's R2 storage under
recent/, which deletes files after 7 days. Each day, a scheduled job copies the day's top 10 daily runs tokeep/, where they stay. - Events (joining is the choice): your run is streamed to everyone in the event and anyone watching it. The results go in a table called
events: each runner's slot, driver number, vehicle, distance, how they ended and kills. The whole event is kept as one replay file, for good for scheduled events and for 7 days for heats. The checks' verdicts are stored next to their event, for as long as the event is. - My replays (on by default): kept on your device only. Nothing is sent.
- Copies of the game: if the game is opened on another website, it reports that website's address, not yours, so copies can be found.
Your internet address
Every request carries your internet address, and limits like "30 new rooms a day" need some way to count. Our servers never store the address. They store a one-way scramble of it: a SHA-256 hash of a secret, the address and the date (for a hat tip, the wreck instead of the date). The secret means the scramble can't be undone by trying every possible address, and the date means the same address looks different from one day to the next, so nothing can be followed across days. The relay keeps its scrambles only for the current day. They're used for the spam limits and nothing else: a daily cap on posts to the API, 30 new streams and event places a day, and 3 ghosts per road.
How it's handled
- Nothing from outside is trusted. Every record is checked against size limits before it's read: 64 KB for a header or a batch, 1 MB for a savestate, 8 MB for a replay, 256 KB for a ghost. Malformed data is refused with an error, never half-used. Ghost tracks are checked for driving no car could do.
- Each stream has its own key, made by your game for that run. The relay keeps only a hash of it, so nobody else can send driving as you.
- The servers answer only the game's own addresses, so a copy of the game elsewhere can't use them.
- Usage counts are totals: rooms, connections, messages and bytes per day, kept for 14 days for the budget guard. There's nothing per player in them.
Where it's kept
Everything runs on Cloudflare: the sites and the games as static files, the API and the relay on Workers, the live rooms and the lobby in Durable Objects, the tables in D1 (Cloudflare's SQLite databases), and replay files in R2. The test round has its own copy of each. The game loads nothing from anywhere else: its 3D engine, three.js, is built into the game's own code and comes from the same address as the rest of it.
When all this comes to the real game, this site gets a privacy page with the short version, including how long each thing is kept.
What's next
Once the test round is done, all of this moves into Dead Mileage itself. Then the real game gets its own relay and storage, and the test servers are taken down. The launch gets its own post, with the date of the first Friday Night Run.
See you on the road. Soon, live.