Skip to content

197 netplay 3 the rollback session

Alex Diakovsky edited this page Dec 3, 2026 · 2 revisions

197. Netplay 3: the rollback session, with no network (N2, issue #150)

Question: document 196 can copy the match after a tick and put it back. Who decides when to do that? A netplay match needs one place that runs the clock, collects every player's words, runs ahead on guesses when words are late, rolls back when a guess was wrong, and checks that the peers still agree. Does that give every peer exactly the same match, over a network that delays, reorders, drops and doubles packets? Where do the computer players run? And what does it cost in a browser, with four players (issue #150, stage N2; tracking #43)?

The netplay series: document 195 is the plan (stages N1 to N8); document 196 is N1, save and restore. This document is N2, the rollback session. It still needs no network: the peers run in one process, joined by a fake one.

Glossary

Term Meaning
peer One copy of the game in a match: it runs the whole match and owns some players (its people, and for the host its computers).
word A player's input word for one tick (InputWord, document 138): the only thing a player gives the match.
session NetSession: one peer's side of a netplay match. It owns the clock and steps the match.
confirmed tick A tick every player's word is known for. It is run once, for good.
guessed tick A tick run before some word for it has arrived, on a guess: the player's last known word.
input delay d A word sampled now is used d ticks later, so it has d ticks to reach the other peers.
window w How many ticks the match may run past the confirmed tick on guesses. w = 0 is lockstep: it never guesses, so it waits.
rollback Restoring the snapshot of the confirmed tick and running the ticks after it again, with what is now known.
LoopbackNet The fake network of this stage: peers in one process, packets delayed, reordered, lost and doubled on purpose, from a fixed seed.

The answer, up front

  1. NetSession owns the clock. MatchController._process hands it the frame time instead of stepping the match. Each frame the session samples its people's words for tick now + d, takes in the other peers' packets, runs every tick that has become confirmed, then runs ahead on guesses up to w ticks, and sends its packets.
  2. Words are by tick, not by count. Each slot's controls become a NetControls, which hands its vehicle the session's word for the tick being run (mc.sim_tick). The S0 input log (document 155) counts polls; the session numbers ticks, so a late word lands on its own tick whatever happened before it.
  3. One snapshot: the newest confirmed tick. When more ticks are confirmed, or a word arrives that contradicts a guess already run, the session restores that snapshot and runs the ticks again: the confirmed ones for good, then the guessed ones anew. A snapshot costs about five ticks (measured below), so the session takes one per confirmation, never one per guessed tick. When nothing was contradicted and the guessed ticks are now all confirmed, they are kept as they are, with no rollback at all.
  4. Computer players think on confirmed ticks only, on the peer that owns them (the host's). Their brain is polled where it always is (the vehicle polls its controls), but only while the session runs confirmed ticks. On guessed ticks, even on the host, a computer's word is guessed like anyone's. Their words then travel like a person's.
  5. It is exact. 2, 3 and 4 peers, lockstep and windows of 4, 8 and 12, input delays 0 to 3, up to 20% of packets lost and 10% doubled: every peer's every confirmed tick hashes the same as the recording, on the engine's fixture (900 ticks, thousands of guessed ticks and 37 to 129 wrong guesses rolled back per run) and on the game's own four-player match (all 4000 ticks, 4 peers, 1257 wrong guesses rolled back). The game's CI runs the latter on Linux, on Windows and in a browser.
  6. The costs, four players, desktop: a tick 0.21 ms, a snapshot 1.06 ms, a restore 1.23 ms, a full hash 2.67 ms. In a browser: a tick 0.23 ms, a snapshot 1.26 ms, a restore 1.54 ms, a full hash 3.0 ms: within 10 to 25% of the desktop. So: one snapshot per confirmation (as built), a hash every 30 ticks by default, and the default window stays 8 until the try-out (N8) says otherwise.
  7. Two things found on the way. The computer players roll Godot's global dice, which the presentation's FxRand rolls too, so a host's computer play cannot be rebuilt from the seed alone, only from its words (which is all netplay needs). And in lockstep a computer's word for a tick exists only once the host has run that tick, so lockstep with computers waits a round trip per tick.

One frame of a session

A frame: the clock gives ticks, each moves now on and samples this peer's people for now plus d; the packets are taken in; if more ticks are confirmed or a guess was contradicted, the snapshot is restored and the confirmed ticks run for good, with the computers' brains, and a new snapshot is taken; then the match runs ahead on guesses up to the window; then the packets go out

Mermaid source
flowchart TD
  C["clock: whole ticks of the frame<br/>(at most w + 1 past the confirmed tick)"] --> S["each tick: now + 1,<br/>sample this peer's people for now + d"]
  S --> R["take in packets:<br/>words, what they have of ours, hashes"]
  R --> Q{"more ticks confirmed,<br/>or a guess contradicted?"}
  Q -- "yes" --> K{"guessed ticks all right<br/>and now all confirmed?"}
  K -- "yes" --> F
  K -- "no" --> B["restore the confirmed snapshot"]
  B --> F["run the confirmed ticks for good<br/>(computers' brains asked here)<br/>snapshot the newest"]
  Q -- "no" --> G
  F --> G["run ahead on guesses<br/>up to confirmed + w, never past now"]
  G --> P["send each peer: our words it lacks,<br/>what we have of its, our hashes"]
Loading

The session's frame

# addons/openfire_engine/game/net/net_session.gd
func frame(delta: float) -> void:
	for i in clock.advance(delta):
		if now >= confirmed + window + 1:
			stalled_ticks += 1   # too far ahead of what is confirmed: wait for the others
			continue
		now += 1
		for p in local_input:
			_put_word(p, now + input_delay, int(local_input[p].call(now + input_delay)) & 0xFFFFFFFF)
	_receive()
	_advance()
	_send()

The clock is the same SimClock the match used (document 157): 16 ms ticks. now may run at most w + 1 ticks past the confirmed tick; past that the frame's time is dropped, so a peer that is ahead waits for the slowest one. That is also all the time sync there is for now (a real network may want more, N7 and N8).

# addons/openfire_engine/game/net/net_session.gd
func _advance() -> void:
	var target := mini(_confirmable(), now)
	var ran := 0
	if target > confirmed or _mismatch <= head:
		# the guessed ticks can stay when none was contradicted, they reach no further than what is now confirmed, and no computer here
		# waited for them (its brain must think on the confirmed ticks)
		var keep := _mismatch > head and head > confirmed and target >= head and not _runs_computers()
		if keep:
			for t in range(confirmed + 1, head + 1):
				if _spec_hashes.has(t):
					_hash(t, _spec_hashes[t])
		elif head > confirmed:
			if _mismatch <= head:
				mispredictions += 1
			_restore()
		_confirming = true
		while head < target:
			_step(true)
			ran += 1
		_confirming = false
		if target > confirmed:
			confirmed = target
			_save()
		_used.clear()
		_spec_hashes.clear()
		_mismatch = FAR
	var limit := mini(now, confirmed + window)
	while head < limit:
		_step(false)
		ran += 1
	max_frame_ticks = maxi(max_frame_ticks, ran)

A word that arrives for a tick already run on a guess is compared with the word that guess handed out (_used); a difference marks the earliest such tick (_mismatch), and the next _advance rolls back. A guess that turned out right costs nothing.

The word for a tick

# addons/openfire_engine/game/net/net_controls.gd
func poll() -> int:
	prev_word = word
	word = session.poll_word(player)
	return word
# addons/openfire_engine/game/net/net_session.gd
func poll_word(p: int) -> int:
	var t := mc.sim_tick
	var w: int
	if _runs(p) and _confirming:
		var own: PlayerControls = _controls[p].inner
		w = own.poll() if own != null else 0
		_put_word(p, t, w)
		return w
	w = UNKNOWN if _runs(p) else word_at(p, t)
	if w == UNKNOWN:
		w = word_at(p, _through[p]) if _through[p] > 0 else 0
		if w == UNKNOWN:
			w = 0
	...

The vehicle polls once a tick as before (Vehicle.sim_step, document 157), so a computer's brain is asked at exactly the place in the tick it is asked offline: after the slots before it have moved. NetControls.word and prev_word feed the vehicle's decode and the hangar's presses but are not sim state (SimHash skips controls), so the session saves and restores them beside its snapshot.

At the start each peer samples its own people for the first d ticks, the ticks no clock tick comes before. (An earlier version assumed "no keys" for them; the game's recording has its computers steering from tick 1, and the session replay caught it on tick 1.)

Packets

Every packet carries every word its receiver has not acknowledged, so the session needs no reliable channel (document 195: unordered, unreliable). A lost packet's words simply come again in the next.

Field Size Meaning
version u8 1
sender u8 the sending peer
players, then per player u8, u32 each what the sender has: the newest tick of each player's words with none missing before it (the acknowledgement)
players, then per player u8, then u8 + u32 + u16 + u32 each the sender's own players' words the receiver lacks: player, first tick, count, the words (at most 240)
hashes, then per hash u8, then u32 + string the sender's newest confirmed hashes (up to 4): tick and digest

A peer that gets a hash for a tick it also hashed compares them; a difference is a desync (desynced, N7 turns it into a report). The hash interval is the host's setting hash_every (30 ticks by default): a full hash costs about 12 ticks.

Computer players

Document 188 settled that computers play online too and the host's peer runs them. The session makes that exact:

  • a computer's brain is asked only while the session runs confirmed ticks, once a tick, in order; it never sees a guessed state;
  • on guessed ticks its word is guessed like a person's, on every peer including the host;
  • its words go out like a person's, through the packets of the peer that owns it.

Found: the brains roll Godot's global dice (randf, in AIStrategy and AIGunner), and so does the presentation (FxRand: a throw sound's pick, smoke, flying pieces). Ticks run again in a rollback roll them again, so on the host the brains' dice depend on how many rollbacks happened. Netplay does not care: the brain's words are what travel, and every peer runs the same words. But a host's computer play cannot be rebuilt from the seed alone: a replay needs its words (the input log has them). The check below tests what matters instead: the brain is asked once a tick, only on confirmed ticks, and every peer's match equals the offline replay of the words the host sent.

Found: in lockstep (w = 0) a computer's word for a tick exists only once the host has run that tick, and the others need it before they can run it. So lockstep with computers waits a full round trip each tick (in the check: 4588 frames for 900 ticks, against 1143 with people only). With a window the others guess the computer's word and the cost disappears. Lockstep is a corner of the settings; if the try-out wants it with computers, the brain could think for tick t + d at the end of tick t (one tick of reaction lost, online only).

Costs

The game's four-player match (openfire-nettest's recording, MAP01, 34 sim objects at most), averaged over all 4000 ticks (a snapshot, a restore and a hash every 10th tick):

Desktop (Windows, the user's machine) Browser (Chromium, the same machine, the web export) Browser on the CI runner (headless Chromium, Linux)
a tick 0.21 ms 0.23 ms 0.27 ms
a snapshot 1.06 ms 1.26 ms 1.48 ms
a restore 1.23 ms 1.54 ms 1.85 ms
a full hash 2.67 ms 3.00 ms 3.60 ms
four sessions over LoopbackNet, all 4000 ticks, every tick hashed on every peer 115 s 162 s (every hash equal to the straight replay's) 171 s, every hash equal to the recording's

What it means:

  • A snapshot is five ticks; a hash is twelve. So the session snapshots once per confirmation, never per guessed tick, and hashes every 30 ticks, not every tick (the checks hash every tick on purpose; that is what makes them slow).
  • A frame's worst case with w = 8 is a restore, up to 9 ticks and a snapshot: about 4 ms on the desktop and about 4.9 ms in a browser, inside a 16 ms frame. The default window stays 8 (d 2) until the try-out over the internet (N8) measures the round trips it has to cover.

How it was checked

Check What it proves
engine tests/net_session_check.gd The determinism match (document 178) recorded, its words taken by tick, typed on their own peers: (d, w) = (0, 0) perfect network, (2, 0), (0, 8), (2, 8) with 3 peers, (1, 4) with 4 peers and 20% lost, (3, 12) with long delays: every peer's every confirmed tick hashes as the recording, no peer reports a desync, lockstep never guesses, the windows guessed wrong and rolled back. Then a computer on the host against a person: the brain asked once a tick, only on confirmed ticks (900 polls, 0 on guessed ticks), and every peer equal to the offline replay of the host's words
openfire-nettest tests/match_check.gd The game's recorded match (four computers, MAP01, every rule on) through four sessions (d 2, w 8, 15% lost, 5% doubled, delays 0 to 6 frames): the first 1500 ticks in the game's checks; all 4000 in its CI (--session)
openfire-nettest CI (web.yml) The Linux recording through the sessions on Linux, on Windows and in headless Chromium (the "Replay" export's page, window.ofSessionHashes), against the recording: all 4000 ticks agree on all three (first green run: nettest 67854ab); the browser's costs in web_timings.json
engine CI (determinism.yml) net_session_check on Linux

Not done here

  • Old words are never dropped: each player's words are kept for the whole match (about 4 bytes a tick a player, a few MB for a long match). Fine for now; pruning behind the oldest acknowledgement is easy when it matters.
  • No time sync: a peer that runs ahead is held back only by the window. The try-out (N8) shows whether peers drift.
  • The host's settings (d, w, hash_every) are the session's variables, set in code; the side-pick screen gets them in N5 with the room.
  • Desyncs are counted, not reported: N7.
  • The game does not use the session yet: nothing starts one outside the checks until there is a transport (N3) and a handshake (N4).

Where the code is (today)

What Where
The session addons/openfire_engine/game/net/net_session.gd (NetSession); MatchController.session (its _process hands it the frame)
The controls by tick game/net/net_controls.gd (NetControls)
The transport and the fake network game/net/net_transport.gd (NetTransport), game/net/loopback_net.gd (LoopbackNet)
The checks engine tests/net_session_check.gd; openfire-nettest tests/recorded_match/session_replay.gd, tests/match_check.gd (--session), tests/recorded_match/match_web.gd; the engine's tools/ci/web_replay.mjs takes extra name=file pairs

Next: N3, #151, document 198: the transport for real, WebRTC data channels with link codes; two browser pages play a match over it in CI.

Clone this wiki locally