Repository navigation
195 netplay 1 the plan
Question: the simulation is deterministic on Linux, Windows and the web (document 178), and the netplay test game plays the same 4000 ticks everywhere (document 194). Document 188 agreed what netplay must be: web first, 2 to 4 players, rollback from the start with lockstep as a setting, computer players online, three ways to connect. What does the engine have already, what is missing, and in which order is it built and proven (issue #43; stages #149 to #156)?
The netplay series: this document starts it. It plans the stages N1 to N8. Each stage gets an issue and a document of its own, as the S0-S7 series did (documents 155 to 194).
A plan, written 2026-12-02, after the user said to start netplay. The design decisions are document 188's, agreed with the user; this document only orders the work and fixes the technical choices that design left open. Where a choice is the user's (the server host), it says so.
| Term | Meaning |
|---|---|
| input word | One player's controls for one tick (InputWord, document 138): one integer. Netplay sends these and nothing else. |
| peer | One running copy of the game in the match: a browser tab or, later, a desktop build. It has up to two people on it, and the host's peer also runs the computer players. |
| input delay d | A word typed on tick t is used on tick t + d. It gives the other peers d ticks to receive it. |
| rollback window w | How many ticks a peer may run past the last tick it has every word for. w = 0 is lockstep. |
| prediction | A word not yet received is guessed: the player's last known word. |
| snapshot | A saved copy of the sim state after a tick, so a peer can go back to it. |
| rollback | A real word differs from its prediction: restore the snapshot of the tick before it, and re-run the ticks since, with the real word. |
| confirmed tick | A tick every peer has every word for. Its state can never change again, so its hash can be compared between peers. |
| transport | What carries bytes between peers: WebRTC data channels in the browser, ENet later on desktop. |
| signalling | Passing the offers WebRTC needs before two peers can talk: by a small server (room code) or by hand (link code). |
- The hard part is done. Rollback needs a deterministic sim, inputs as small words, and a state hash to catch desyncs. S0-S6 built all three, and the game proves them on three platforms in CI.
- What is missing, in order of risk: save and restore of the sim state; a session that runs the match on delayed, predicted and corrected words; the transport; the handshake; the room; signalling; desync reports and disconnects.
- The first stages need no network. N1 (save and restore) and N2 (the rollback session) are proven in one process, with peers joined by a fake transport that delays, reorders and drops packets on purpose. Every later stage then adds a real link to code that is already known to be right.
- The second stage measures the cost on the web. Rollback re-runs up to w + 1 ticks in one frame. N2 measures a tick and a snapshot on the web, with four players, so the default window rests on numbers (document 188's open risk).
- Link codes before room codes. N3 connects two browser tabs by pasted WebRTC codes, with no server: the free fallback, and enough to play a real match between two machines. The signalling server (N6) comes after, once the user has chosen where it runs.
- Each stage adds to CI. N1 and N2 add game checks; N2 extends #142's workflow with a rollback replay on Linux, Windows and the web; N3 adds two headless Chromium pages that play a short match over a real WebRTC link on the runner.
- Engine and game split as before: the netcode, the transport interface, the session and the remote rows are engine code (no game's name in them, document 181); the game's repo holds its settings, its server address and the try-out builds.
| Piece | Where | What netplay uses it for |
|---|---|---|
The fixed step: MatchController.step(), one tick, integers only |
game/match_controller.gd |
Re-running ticks after a rollback |
| Input words, polled once a tick per player by its vehicle |
Vehicle.sim_step -> controls.poll()
|
The only thing peers send |
PlayerControls as a seam: LoggedControls records or replays words |
game/sim/logged_controls.gd |
The session's controls hand each vehicle the tick's word, real or predicted |
InputLog: setup plus every word |
game/sim/input_log.gd |
The CI recordings, and each peer's record of a netplay match (a desync can be replayed offline) |
SimHash: a walk of every sim value, with a digest per part |
game/sim/sim_hash.gd |
Comparing confirmed ticks between peers; the snapshot follows the same walk |
Every sim object a plain RefCounted holding integers; no scene node is sim state |
stages S4-S6 | A snapshot is a copy of plain objects, not of scene nodes |
The match's own random numbers in the sim (MatchController.rng) |
stage S2 | Restored with the state, so re-runs roll the same dice |
Computer players outside the sim (SimHash.SKIP_CLASSES) |
stage S5 | They run on the host's peer only; others receive their words |
MatchSetup: the rows, the sides, the rules, the host settings, apply
|
game/match_setup.gd (document 191) |
What the handshake sends; remote rows join it |
| The recorded match, replayed on three platforms | openfire-nettest (document 194) | The base of every netplay check |
| Stage | What | Proven by | Issue |
|---|---|---|---|
| N1 | Save and restore the sim state. A snapshot of the match after a tick; restoring it and re-running gives the same hashes as the straight run. Presentation (views, sound, effects) reacts to a restore and is not re-triggered by re-run ticks. Measured: snapshot and restore time. | Engine check on the fixture; game check on MAP01 (restore every k ticks, re-run, same 4000 hashes) | #149 |
| N2 |
The rollback session, with no network. NetSession owns the stepping: input delay d and window w, prediction, rollback and re-run, confirmed-tick hashes. Peers in one process joined by a fake transport that delays, reorders and drops. Measured on the web: a tick and a snapshot, four players. |
Game check: 2-4 peers in one process agree on every confirmed hash for several (d, w) pairs, lockstep included; CI: the recording replayed through a session with random delays and mispredictions gives #142's hashes on Linux, Windows and the web | #150 (extends #142's workflow) |
| N3 | The transport, WebRTC first; link codes. A transport interface (send, receive, peers, connection state), a loopback implementation (N2's fake), and WebRTC data channels in the browser. Two tabs connect by pasted codes and play a match. | CI: two headless Chromium pages on one runner connect over WebRTC (no STUN needed there) and play a short match, every confirmed hash equal | #151 |
| N4 |
The handshake. Same build and same pack (the pack identity with gameplay data, #16), then the seed, the MatchSetup, the netplay settings; a countdown starts every peer on the same tick. |
Check: a peer with other gameplay data is refused with a reason; peers start on the same tick | #16 + #152 |
| N5 | The room: remote rows on the side-pick screen. Each peer's people as rows, up to two per peer; computer rows the host adds, run by the host's peer; the host's settings seen by all as they change; Next when every peer is ready. | Check over loopback: rows and settings agree on every peer; try-out in two tabs | #153 |
| N6 | Signalling and room codes. A small reference server in the game's repo (create a room, join it, pass offers along), its address a setting. The user chooses the host from the candidates in document 188 or others. | The server's own test; a CI match through a local copy of the server | #154 |
| N7 |
Desyncs, statistics, disconnects. A confirmed hash that differs stops the match and writes a desync report (both peers' input logs and the part that differs, SimHash.first_difference). Round trips, rollback counts and lengths logged on every peer. Pause, reconnect within a time, and the host's disconnect policy (computer takes over, side out, or match ends). |
Checks over loopback with a forced desync and a forced disconnect | #155 |
| N8 | The try-out over the internet. Two to four people on the published link, comparing settings ((d, w) pairs), with the logs to back it. Then desktop: ENet by IP, and WebRTC on desktop via Godot's WebRTC extension, for cross-play. | The user's try-out | #156 |
A snapshot follows SimHash's walk: the same variables of the same objects, the same skips. It does not hash them; it copies them: for each sim object reached, its script variables, with arrays and dictionaries copied and objects recorded by reference. Restore writes the values back into the same objects, rather than swapping in copies. That keeps each object's identity, so a view that draws a vehicle still draws it after a rollback, and an object made after the snapshot simply drops out of the restored lists.
Two things need care, and N1 finds out how much:
-
Removal cuts links (
SimLinks.cutempties an object's object variables,drop_listenersdisconnects its signals). Restoring the variables brings the links back; signals a view had connected must be reconnected by the view, so views follow a "the match was restored" signal and resync. -
Presentation must not run twice. Sounds, explosions and debris are triggered from sim events. Re-run ticks must not replay a sound that already played, and must play one the correction newly causes. The standard answer, and the plan here: presentation keys each event by (tick, source), ignores a key it has seen, and cancels long effects whose tick was undone. Flying debris is already out of the sim (
DebrisPiece, document 170), so a rollback leaves it alone.
Cost: copying every object each tick could be heavy on the web. A peer only needs a snapshot of the oldest unconfirmed tick, plus one per tick inside the window, so N1 measures the snapshot cost first and N2 chooses: a snapshot every tick, or every few ticks with longer re-runs.
Today MatchController._process steps the match from SimClock. In a netplay match a NetSession does it instead: each frame it reads the local people's words (for tick now + d), sends them, takes in what arrived, rolls back if a received word differs from what was predicted, then steps as far as the window allows. Each slot's controls become a NetControls (a PlayerControls, like LoggedControls) that hands its vehicle the session's word for the current tick, by tick number rather than by count.
Lockstep is the same session with w = 0: it never steps past the confirmed tick, so it waits. The host sets d and w; the game's pack gives defaults.
Computer players run on the host's peer, inside its session, from the confirmed state: the host's brains see the true state, never a predicted one, so their words are as deterministic as a person's typing. The other peers receive those words like any remote player's.
Every few ticks each peer sends the hash of its latest confirmed tick. Peers agree on it or the match has desynced; N7 turns that into a report. The full hash is too slow to take every tick on the web (it walks every object), so the interval is a setting, measured in N2.
PORTING_PLAN.md 2.5.4 stands: the netcode calls an interface, never a socket. The browser has WebRTC data channels built in (WebRTCPeerConnection); a desktop build needs Godot's WebRTC extension, or ENet. The match's words go on an unordered, unreliable channel (a late word is resent with the next packet anyway, since each packet carries every unconfirmed word of its sender); the room and the handshake go on a reliable, ordered one. The engine does not use Godot's high-level multiplayer API (RPCs): the session needs exact control over what is sent and when.
Topology: a star around the host for the room and the handshake, a full mesh of at most four peers for the match (document 188).
Mermaid source
flowchart TB
N1["N1 save and restore<br/>(one process)"] --> N2["N2 rollback session<br/>(fake transport, web timing)"]
N2 --> N3["N3 WebRTC, link codes<br/>(two browser pages in CI)"]
N3 --> N4["N4 handshake<br/>(pack identity #16)"]
N4 --> N5["N5 the room<br/>(remote rows)"]
N5 --> N6["N6 signalling server<br/>(room codes; user picks the host)"]
N5 --> N7["N7 desyncs, statistics,<br/>disconnects"]
N6 --> N8["N8 try-out over the internet,<br/>then desktop"]
N7 --> N8
Every stage keeps what is there green: the engine's determinism workflow and the game's recorded match. A netplay match is a recording too: each peer keeps the InputLog of the confirmed words, so any match, a desync included, can be replayed offline with the tools S0 built.
- Where the signalling server runs (N6): a free host is preferred; the candidates in document 188 are unverified.
- The default input delay and window for the game's pack, after N2's measurements and N8's try-out.
- The disconnect policy's default (N7): the options are the host's setting; which one the game starts with is the user's.
- The try-outs (N3 between two of the user's machines, N8 over the internet).
Next: N1, save and restore (document 196).
Return Fire, rebuilt
Elsewhere