8. Multiplayer without a server
Four people fly in one space, shoot each other, come and go as they like, and no computer is in charge. This chapter explains how: who talks to whom, what each computer says every frame, who decides what, how the frames stay in step, and what goes wrong. The network details of 1993 (Novell's IPX) are left out where they do not matter; the ideas carry over to any network, including WebRTC in a browser.
Every computer is equal
There is no server, no host and no "match". Each player's computer runs the full game for its own ship and simply tells everyone else, every frame, what its ship is doing. It never asks anybody for permission and never waits for a reply to a question (it only waits, briefly, until it has heard from everyone this frame, see "Keeping in step"), and it never sends a message to one particular computer: everything is a broadcast to all machines on the network.
Universes, slots, channels
- A network can host 8 universes: completely separate games that ignore each other. You pick one in the lobby's Universe menu (universe 1 by default).
- A universe has 4 slots, PLAYER 1 to 4. Each slot has its own broadcast channel, like a radio frequency: anyone can tune in, and only that slot's player transmits on it (in the original: an IPX socket number computed from the universe and the slot).
- A player sends only on its own slot's channel and listens on the other three.
- A spectator and a computer sitting in the lobby listen on all four and never send anything.
The lobby: taking a slot
A computer in the lobby only listens. Whenever it hears a packet on a slot's channel it sets a counter for that slot to 32; each lobby frame without a packet counts it down by one. A slot whose counter is 0, about half a second after the last packet, is shown as "Free"; otherwise it shows the name in the packets (live, letter by letter, while that player types it; blank until the first letter).
Taking a slot is a purely local decision: Enter on a slot that this computer sees as Free takes it. Nothing is announced; the others learn about the new player from its first packet, which it sends right away on the name screen that follows. Pressing Enter on a slot that is not Free does nothing.
If two computers press Enter on the same Free slot before either one's first packet has reached the other (a frame or two), both become that slot D. They never notice each other (a player does not listen to its own channel). Everyone else receives packets from both on the same channel and shows whichever arrived first each frame: one ship, name and score that jump between the two. A hit on that slot is applied by both machines.
The name screen ("Type your name", with the scores of all four slots) is already part of the game on the network: it sends packets every frame with the status "not in play", which keeps the slot taken and lets others see the name being typed. Enter starts flying; Esc stops sending and returns to the lobby. A spectator skips the name screen.
What a computer says every frame
Once per frame (at the start of the frame, if the previous send has finished) each player broadcasts one state packet of 130 bytes of game data (160 with the network header). It describes the state at the end of the previous frame:
| Field | Size | Meaning |
|---|---|---|
| name | 16 bytes | up to 15 characters |
| orientation | 3 axes × 3 × 2 bytes | forward, right, "up" (Q14 numbers, chapter 3) |
| position | 3 × 8 bytes | 64-bit fixed point (chapter 3) |
| velocity | 3 × 8 bytes | per frame; used only to launch copies of its shots |
| 3 hit reports | 3 × 2 bytes | "slot n was hit by my laser this frame" (+ a powered flag), or 0 |
| status | 2 bytes | a number: 0 alive, −1 cloaked, 1 killed, 2 not in play (name screen) |
| satellite event | 2 bytes | "I collected / shot the satellite of slot n", or 0 |
| score | 4 bytes | its own score |
| its satellite | 2 + 24 bytes | kind (0 none, 1–4, −1 exploding) and position |
| shot flags | 8 × 1 byte | for each of its 8 laser slots: none, flying, flying powered, exploding |
Not in the packet: shot positions, shields, throttle, cloak time, who killed whom, any frame number, sequence number or acknowledgement. The receiver copies the fields straight into its copies of that player's ship, satellite, shots and score (chapter 5). The packets are sent whether or not anything changed: in the recorded sessions about 70 per second per player, three quarters of them identical to the one before (runtime notes, capture).
Who decides what
| Question | Who decides | How the others learn |
|---|---|---|
| Where is my ship, which way does it point? | me | my packets; they show it exactly there, no prediction between packets |
| Did my laser hit you? | me, the shooter, against my copy of your ship | a hit report in my next packet; you lose 1 shield (2 if powered) when it arrives; I score 50 at once |
| Did we collide? | each of us, separately | nobody is told; the two sides can disagree |
| Am I dead, am I cloaked? | me | my status word |
| Who killed me? | me (from the hit report, or the collision) | nobody: the others only see "name has been killed"; the killer gets no bonus, only the 50 points per hit |
| My satellite | me (spawning, kind, position) | my packets; a collector or shooter tells me with a satellite event, then I remove or explode it |
| My score | me | my packets (shown on the name screen's table) |
So each computer is the authority (the computer whose word counts) for its own ship, shots, satellite and score, and the shooter is the authority for hits. Shot copies drawn for other players are only pictures and never hit anything. One consequence: a shot can look like a miss on your screen and still count, or the other way round, because the shooter judged it against where it saw you, one or two frames earlier.
Keeping in step
Each frame ends with a short wait (net_sync_frame, src/net.c):
- Wait until a packet from every live other player has arrived this frame, but at most 5 timer ticks (about 275 ms). So the game runs at the pace of the slowest live player: a loose kind of lockstep (everyone advances one frame together). A player alone in its universe never waits. Without a timer cap, multiplayer frames run as fast as the screen refresh allows (about 70 per second), and since all motion is per frame, multiplayer plays almost four times as fast as single player (chapter 2).
- Liveness ("is this player still sending?"). Each other player has a counter from 0 to 4. A packet this frame sets it to 4; a frame without one lowers it by 1. At 0 that player is inactive: no longer waited for, its ship, shot copies and satellite copy disappear. Its next packet brings it straight back to 4.
- Apply what the packets brought: hit reports aimed at my slot (shields, flash, cloak lost, maybe death), and each player's status (start its explosion, show it, hide it).
Coming and going
| What the player does | What it sends | What the others see |
|---|---|---|
| joins: takes a slot, types a name | packets with status "not in play" | name and score in the table; no ship yet |
| starts flying | status "alive" | its ship appears |
| dies | status "killed" until it presses Enter | "name has been killed", explosion; the ship reappears when it respawns, near where it died (chapter 4) |
| presses Esc in flight | status "not in play", score 0 | the ship vanishes (no explosion) |
| presses Esc on the name screen, quits, crashes, loses the network | nothing | about 1.1 s of slow motion; a flying ship (crash) vanishes then, one that left via Esc was already hidden; its name and last score stay in this computer's table until a packet on that slot arrives again (or this computer returns to the title screen) |
What if the player who "started" the game leaves? Nothing special. Nobody started it: the order in which players joined plays no role, and the others go on as if any other player had left.
To run several copies of the original under the DOSBox-X emulator, one emulator acts as a tunnel server that relays the emulated network traffic over the internet protocol. Closing that one emulator cuts everybody off. That is a property of the emulator's tunnel, not of the game; on a real network, and in a peer-to-peer rebuild, there is no such single point.
What goes wrong
The design is simple and robust in one way (nothing can "host-migrate" because there is no host), but it has weak spots a rebuild should know:
- Events are a state, not a message. A hit report is just a value in a packet. Each receiver has one buffer per player, re-armed once per frame; it catches the first packet that arrives after that (later ones in the same frame are dropped), and every frame the receiver applies whatever that buffer holds, whether or not a new packet arrived. There are no sequence numbers: a late packet simply overwrites the state, a repeated one repeats its hits. If a shooter goes silent right after a hit, the victim applies that hit again every frame: one hit took 20 shields in about 20 frames in a test, and the death countdown restarted every frame, so the victim stayed dead with no "Press ENTER" for as long as the shooter was silent (runtime #24). A hit scored in the very last frame before the shooter presses Esc is repeated the same way while it sits on the name screen D; a stale "shot your satellite" event would likewise keep the owner's satellite exploding, so it never gets a new one D.
- Events can be lost. There are no acknowledgements. If a packet is not sent (the previous one still busy) or arrives when the receiver is not listening, the hit in it is gone. Two hits by the same shooter on the same player in one frame count once.
- Everyone trusts everyone. Any computer may claim any hit, score or name; nothing is checked.
- Two players can share a slot (above).
- Speed is tied to the slowest computer, and a vanishing player stutters everyone for about a second.
- No smoothing. Other ships jump to each new packet's position; nothing predicts where they are between packets.
- Collisions disagree. Each side judges its own collision from its own, slightly old copy of the other.
- Order within a frame: re-arm the listens → send the previous frame's state → simulate and draw (hits found now go out in the next frame's packet) → wait, then apply incoming hits and statuses. The name screen runs the same network steps, so a player typing a name is part of the lockstep too.
- Peer numbers vs slots: each computer numbers the other players 0–2 in ascending slot order (a spectator numbers all four 0–3). Hit report number k is about its sender's peer k, but its value is the victim's slot + 1, and the receiver only checks the value. Names and scores are stored by slot.
- Fixed per slot: ship model i for slot i; start corner and heading (chapter 3); the collision push direction (chapter 4); on the radar slots 1–2 are light green and 3–4 light blue for every viewer (no teams). A multiplayer life starts with 32 shields and no missiles.
- Spectator: chosen via the Game menu ("Spectate current game"); starts at the origin facing +z and can fly around; no score or shields shown; cannot be hit and has no satellite; never sends, so it never slows the players down, but it waits for them; Esc returns to the lobby.
- Scores: 50 per laser hit is the only source of points (kills, collisions and satellites give none). The score survives deaths; it is reset on joining and by Esc in flight. There is no pause (F2 is ignored) and the game never ends by itself.
The original maps onto a WebRTC full mesh almost one to one: "broadcast on my slot's channel" is "send to every peer in the room, tagged with my slot"; "listen on the other channels" is "accept messages tagged with other slots"; spectators and lobby visitors are receive-only members. A small signalling service only introduces the browsers to each other; no game logic runs there, so the game survives anyone leaving, including whoever sent the invitation.
To keep the feel, keep: a fixed simulation step shared by all players (about 70 per second matches the original multiplayer; drawing is decoupled from it, chapter 2), one state message per step, the shooter deciding hits, each side deciding its own collisions. On the internet, replace the 4-frame liveness (only 57 ms at 70 steps per second) by a time-based timeout of about half a second to a second, or ordinary jitter will make ships blink out.
Worth fixing on purpose:
- send the state on an unreliable, unordered data channel (latest wins, with a sequence number to drop late messages), and hit reports and satellite events with an id on a reliable channel (or repeat them until acknowledged); apply each id once;
- claim a slot with a short handshake: on a conflict the lower peer id keeps it and the other goes back to the lobby (or let the signalling service publish who holds which slot, so lobby visitors need not join the mesh just to see names);
- do not stall your own simulation waiting for slow peers; draw their ships interpolated between messages. This changes the feel a little: the original shows other ships exactly where their last packet put them, and its hit checks use those positions;
- with real internet delay (50–150 ms rather than one frame), "the shooter decides" means a victim will sometimes be hit after it reached cover on its own screen; whether to add lag compensation is a separate decision.
Every player also sends to each spectator and lobby visitor in the mesh; at 130 bytes 70 times a second this is small, but it grows with the room. Chapter 13 goes into all of this.
At the end of each frame, for each other player in peer order, each of its three hit words whose low byte equals my slot + 1 (spectators skip this): −1 shield (−2 if the word also has 0x100), light-red (yellow if powered) flash; at 0 shields or below, shields become 0, my status becomes "killed", the killer becomes that player's slot (a later player in the same frame overwrites it) and the death countdown restarts at 64; if I have cloak time it is set to 0 with "Cloak Destroyed!". Nothing else changes: a non-lethal hit leaves my status as it was (a cloaked status stays until my own update recomputes it next frame). Hits are applied even while I am already dead.