Files
tidewall/test/net/test_client_clock.gd
claudeandClaude Opus 5.5 4111a285f0 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>
2026-11-12 11:30:00 -05:00

95 lines
3.2 KiB
GDScript

extends TestCase
## Issue N3: the clock that decides which tick a client's next command is for.
const MARGIN: int = Tuning.NET_COMMAND_MARGIN_TICKS
const DT: float = 1.0 / Tuning.TICK_RATE_HZ
var clock: ClientClock
func before_each() -> void:
clock = ClientClock.new()
clock.start(100)
func test_it_starts_ahead_of_the_server_and_keeps_time() -> void:
assert_true(clock.running)
assert_eq(clock.tick(), 100 + Tuning.NET_COMMAND_LEAD_START_TICKS)
for _tick: int in 30:
clock.advance(DT)
assert_eq(clock.tick(), 130 + Tuning.NET_COMMAND_LEAD_START_TICKS)
clock.advance(DT * 0.25)
assert_near(clock.fraction(), 0.25, 0.001)
clock.stop()
clock.advance(1.0)
assert_eq(clock.tick(), 130 + Tuning.NET_COMMAND_LEAD_START_TICKS, "a stopped clock stays put")
func test_commands_arriving_late_twice_running_make_it_jump_ahead() -> void:
var start: int = clock.tick()
for _tick: int in 10:
clock.advance(DT)
# The server was about to run a tick and the newest command it had was for two
# ticks earlier: four short of having MARGIN waiting.
clock.observe(start + 9, start + 7)
assert_eq(clock.tick(), start + 10, "once could be a burst of lost packets")
assert_eq(clock.jumps, 0)
for _tick: int in 2:
clock.advance(DT)
clock.observe(start + 11, start + 9)
assert_eq(clock.jumps, 1)
assert_eq(clock.tick(), start + 12 + MARGIN + 1)
# Echoes of commands sent before the jump say nothing about the clock now.
var after: int = clock.tick()
clock.observe(start + 13, start + 11)
clock.observe(start + 15, start + 12)
assert_eq(clock.tick(), after)
assert_eq(clock.jumps, 1)
# And the first made after it shows the margin is right.
clock.observe(after - MARGIN + 1, after)
clock.observe(after - MARGIN + 3, after + 2)
assert_eq(clock.jumps, 1)
assert_near(clock.margin, MARGIN, 0.01)
func test_one_late_report_between_good_ones_changes_nothing() -> void:
var start: int = clock.tick()
for round: int in 20:
var sim_tick: int = start + 10 + round * 2
var waiting: int = -1 if round % 5 == 2 else MARGIN
clock.observe(sim_tick, sim_tick + waiting - 1)
assert_eq(clock.jumps, 0)
assert_eq(clock.tick(), start)
func test_far_too_early_and_it_waits_for_the_server() -> void:
var start: int = clock.tick()
clock.observe(110, 110 + MARGIN + 20)
assert_eq(clock.jumps, 1)
assert_true(clock.tick() < start - 10, "went back from %d to %d" % [start, clock.tick()])
func test_a_small_error_is_worked_off_by_running_fast_or_slow() -> void:
# One command too many waiting, every snapshot: the clock should lose a tick, slowly.
var sim_tick: int = 110
var truth: float = float(clock.tick())
for _round: int in 600:
clock.observe(sim_tick, sim_tick + MARGIN)
for _tick: int in 2:
clock.advance(DT)
truth += 1.0
sim_tick += 2
assert_eq(clock.jumps, 0)
var lost: float = truth - (float(clock.tick()) + clock.fraction())
assert_true(lost > 0.5 and lost < 30.0, "ran %.2f ticks slow over 40 s" % lost)
assert_true(clock.margin > MARGIN + 0.5, "smoothed margin %.2f" % clock.margin)
func test_no_command_received_yet_says_nothing() -> void:
var start: int = clock.tick()
for round: int in 10:
clock.observe(110 + round * 2, -1)
assert_eq(clock.tick(), start)
assert_eq(clock.jumps, 0)