N3: Client prediction, reconciliation and interpolation

The client's side of a networked match, in src/net/:

- ClientGame takes the server's notices and snapshots, generates the island
  from the seed and answers with commands. It works on packets, so it runs
  over a NetClient or a test's simulated link.
- ClientClock decides which tick the next command is for, from the server's
  echo of the newest command tick it has received, holding three commands
  waiting at the server.
- ClientPrediction runs each command on a copy of the client's own player
  with the server's movement code, and compares every snapshot with what it
  predicted. A difference is replayed from the server's state and eased in.
- SnapshotInterpolator draws everyone else four ticks in the past, between
  snapshots, on a clock steered gently by arrivals.

Snapshots now carry the client's own block: exact position and velocity,
the water around the player, stamina and state flags, so a prediction can
match the server to the bit. The command echo is now the newest tick
received rather than taken. Protocol version 2.

Snapshots go out every second tick. The interval was three ticks, which is
10 a second, not the 20 the documents claimed; 15 a second is the nearest
even spacing and what interpolation is tuned for.

Drop.ride() holds the barge ride for the server and the client alike, and
NetGame's joining, input and snapshot steps can be called without a socket.

Measured in test/net/test_prediction_link.gd over a simulated link with
100 ms each way, 20 ms jitter and 2% loss: no corrections to own movement
in about 1,400 snapshots per client, and the other player drawn 0.4 mm on
average from where a perfect link would draw them.

Closes #34

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-12 11:30:00 -05:00
co-authored by Claude Opus 5.5
parent b4548d91f5
commit 4111a285f0
32 changed files with 1932 additions and 89 deletions
+23 -3
View File
@@ -11,10 +11,10 @@ const MAX_COMMAND_LEAD_TICKS: int = 60
const WORLD_SIZE_METRES: float = 1024.0
# Network. See docs/ARCHITECTURE.md, "Snapshot format".
## Server ticks between snapshots: 20 a second.
const NET_SNAPSHOT_INTERVAL_TICKS: int = 3
## Server ticks between snapshots: 15 a second.
const NET_SNAPSHOT_INTERVAL_TICKS: int = 2
## The most bytes in one snapshot. Keeps a packet inside one UDP datagram on any
## ordinary link, and a client under 24 KB/s before packet headers.
## ordinary link, and a client under 18 KB/s before packet headers.
const NET_SNAPSHOT_MAX_BYTES: int = 1200
## A client is sent other players and shots within this many metres on the map of the
## player it is watching: a little further than the longest weapon reaches.
@@ -28,6 +28,26 @@ const NET_SNAPSHOT_HISTORY: int = 40
const NET_STALE_METRES_PER_SNAPSHOT: float = 6.0
## Commands a client repeats in each input packet, so a lost packet costs nothing.
const NET_INPUT_REDUNDANCY: int = 3
## Commands a client aims to have waiting at the server when each tick runs. Enough
## that ordinary jitter, or a lost packet whose commands the next one repeats, still
## gets every command there in time.
const NET_COMMAND_MARGIN_TICKS: int = 3
## How far ahead of the first snapshot's tick a client starts stamping commands, before
## the server's echo tells it how early they really arrive.
const NET_COMMAND_LEAD_START_TICKS: int = 6
## How far in the past a client draws other players, in ticks: two snapshots, so there
## is nearly always one on each side of the moment drawn.
const NET_INTERP_DELAY_TICKS: float = 4.0
## When snapshots are late or lost, others carry on along their last heading for at
## most this many ticks, then wait.
const NET_INTERP_EXTRAPOLATE_TICKS: float = 3.0
## Something that moved faster than this between two snapshots was put somewhere new,
## and is drawn there at once rather than sliding across.
const NET_INTERP_TELEPORT_METRES_PER_SECOND: float = 80.0
## A correction to the client's own predicted position is blended away at this rate
## (per second), and one larger than the snap distance is shown at once.
const NET_CORRECTION_SMOOTH_RATE: float = 10.0
const NET_CORRECTION_SNAP_METRES: float = 3.0
# Match sizes
const SOLO_PLAYERS: int = 24