How NetWars Works Chapter 8 of 13, plus appendices

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

one channel per slot, everyone can hear every channel PLAYER 1sends on red PLAYER 2sends on blue PLAYER 3sends on green PLAYER 4sends on grey spectatorlistens to all four computer in the lobbylistens to all four
One universe (the channel colours are only for this picture). Each player listens on the three channels that are not its own; it never hears its own channel. Spectators and lobby computers only listen.

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.

Weak spot: two players in one slot

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:

FieldSizeMeaning
name16 bytesup to 15 characters
orientation3 axes × 3 × 2 bytesforward, right, "up" (Q14 numbers, chapter 3)
position3 × 8 bytes64-bit fixed point (chapter 3)
velocity3 × 8 bytesper frame; used only to launch copies of its shots
3 hit reports3 × 2 bytes"slot n was hit by my laser this frame" (+ a powered flag), or 0
status2 bytesa number: 0 alive, −1 cloaked, 1 killed, 2 not in play (name screen)
satellite event2 bytes"I collected / shot the satellite of slot n", or 0
score4 bytesits own score
its satellite2 + 24 byteskind (0 none, 1–4, −1 exploding) and position
shot flags8 × 1 bytefor 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

QuestionWho decidesHow the others learn
Where is my ship, which way does it point?memy packets; they show it exactly there, no prediction between packets
Did my laser hit you?me, the shooter, against my copy of your shipa 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, separatelynobody is told; the two sides can disagree
Am I dead, am I cloaked?memy 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 satelliteme (spawning, kind, position)my packets; a collector or shooter tells me with a satellite event, then I remove or explode it
My scorememy 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):

  1. 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).
  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.
  3. 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).
frames of the other players while one of them goes silent (to scale in time) 4 → 33 → 22 → 11 → 0 each frame waits the full 5 ticks (~275 ms) for the missing packet inactive: normal speed again, its ship disappears blue: normal frames (~14 ms at 70 frames/s); red: frames held by the wait. About 1.1 s of slow motion for everyone who listened to the silent player (observed, runtime #24).
Liveness counter of the silent player shown in each held frame.

Coming and going

What the player doesWhat it sendsWhat the others see
joins: takes a slot, types a namepackets with status "not in play"name and score in the table; no ship yet
starts flyingstatus "alive"its ship appears
diesstatus "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 flightstatus "not in play", score 0the ship vanishes (no explosion)
presses Esc on the name screen, quits, crashes, loses the networknothingabout 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.

A note on our test setup

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:

Details for an exact rebuild
For a rebuild in the browser

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:

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.

Exact rules: applying hit reports

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.