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:
@@ -0,0 +1,131 @@
|
||||
extends TestCase
|
||||
|
||||
## Issue N3: drawing other entities between snapshots, a little in the past.
|
||||
|
||||
const DELAY: float = Tuning.NET_INTERP_DELAY_TICKS
|
||||
const STEP: int = Tuning.NET_SNAPSHOT_INTERVAL_TICKS
|
||||
const DT: float = 1.0 / Tuning.TICK_RATE_HZ
|
||||
|
||||
var sim: Simulation
|
||||
var walker: SimEntity
|
||||
var remote: SnapshotInterpolator
|
||||
|
||||
|
||||
func before_each() -> void:
|
||||
sim = Simulation.new(4, SimGround.flat())
|
||||
walker = sim.entities.find(sim.add_player())
|
||||
walker.position = Vector3(100.0, 0.0, 100.0)
|
||||
remote = SnapshotInterpolator.new()
|
||||
|
||||
|
||||
func _push(tick: int) -> void:
|
||||
remote.push(tick, {walker.id: NetSnapshot.record_of(walker)})
|
||||
|
||||
|
||||
func test_an_entity_is_drawn_between_the_snapshots_around_the_moment() -> void:
|
||||
assert_true(remote.sample(walker.id).is_empty(), "nothing known yet")
|
||||
_push(10)
|
||||
assert_near(remote.time, 10.0 - DELAY, 0.0001, "the clock starts a delay behind")
|
||||
assert_eq(remote.sample(walker.id)["position"], Vector3(100.0, 0.0, 100.0), "before the first sample: the first sample")
|
||||
walker.position.x = 104.0
|
||||
walker.yaw = 16384
|
||||
_push(10 + STEP)
|
||||
remote.time = 10.0 + STEP * 0.25
|
||||
var shown: Dictionary = remote.sample(walker.id)
|
||||
assert_near(shown["position"].x, 101.0, 0.001)
|
||||
assert_near(shown["yaw"], PI * 0.125, 0.001)
|
||||
remote.time = 10.0 + STEP
|
||||
assert_near(remote.sample(walker.id)["position"].x, 104.0, 0.001)
|
||||
assert_eq(remote.extrapolated, 0)
|
||||
|
||||
|
||||
func test_yaw_turns_the_short_way_round() -> void:
|
||||
walker.yaw = 65536 - 1820
|
||||
_push(10)
|
||||
walker.yaw = 1820
|
||||
_push(10 + STEP)
|
||||
remote.time = 10.0 + STEP * 0.5
|
||||
var yaw: float = wrapf(remote.sample(walker.id)["yaw"], -PI, PI)
|
||||
assert_near(yaw, 0.0, 0.01, "through north, not all the way round")
|
||||
|
||||
|
||||
func test_past_the_newest_snapshot_it_carries_on_briefly_then_waits() -> void:
|
||||
_push(10)
|
||||
walker.position.x = 102.0
|
||||
_push(10 + STEP)
|
||||
var per_tick: float = 2.0 / STEP
|
||||
remote.time = 10.0 + STEP + 2.0
|
||||
assert_near(remote.sample(walker.id)["position"].x, 102.0 + per_tick * 2.0, 0.001)
|
||||
assert_eq(remote.extrapolated, 1)
|
||||
assert_eq(remote.held, 0)
|
||||
remote.time = 10.0 + STEP + 50.0
|
||||
assert_near(remote.sample(walker.id)["position"].x, 102.0 + per_tick * Tuning.NET_INTERP_EXTRAPOLATE_TICKS, 0.001)
|
||||
assert_eq(remote.held, 1)
|
||||
|
||||
|
||||
func test_a_wrong_guess_is_blended_away_not_shown_at_once() -> void:
|
||||
_push(10)
|
||||
walker.position.x = 102.0
|
||||
_push(10 + STEP)
|
||||
# The next snapshot is lost and the walker stops dead. Two ticks past the newest
|
||||
# snapshot the guess has it still walking.
|
||||
remote.time = 10.0 + STEP + 2.0
|
||||
var guessed: float = remote.sample(walker.id)["position"].x
|
||||
assert_true(guessed > 102.5)
|
||||
_push(10 + STEP * 3)
|
||||
var after: float = remote.sample(walker.id)["position"].x
|
||||
assert_near(after, guessed, 0.001, "no jump on the frame the news arrives")
|
||||
var last: float = after
|
||||
for _frame: int in 90:
|
||||
remote.advance(1.0 / 60.0)
|
||||
remote.time = 10.0 + STEP + 2.0
|
||||
var now: float = remote.sample(walker.id)["position"].x
|
||||
assert_true(now <= last and last - now <= (last - 102.0) * 0.2 + 0.001, "eased from %.3f to %.3f" % [last, now])
|
||||
last = now
|
||||
assert_near(last, 102.0, 0.01, "and ends where the server had it")
|
||||
|
||||
|
||||
func test_something_put_somewhere_new_is_drawn_there_at_once() -> void:
|
||||
_push(10)
|
||||
walker.position = Vector3(400.0, 0.0, 400.0)
|
||||
_push(10 + STEP)
|
||||
remote.time = 10.0 + STEP * 0.5
|
||||
assert_eq(remote.sample(walker.id)["position"], Vector3(400.0, 0.0, 400.0))
|
||||
|
||||
|
||||
func test_entities_that_leave_the_picture_are_forgotten_and_old_news_ignored() -> void:
|
||||
_push(10)
|
||||
walker.position.x = 150.0
|
||||
_push(8)
|
||||
remote.time = 10.0
|
||||
assert_eq(remote.sample(walker.id)["position"].x, 100.0, "a snapshot older than the newest changes nothing")
|
||||
remote.push(12, {})
|
||||
assert_false(remote.has(walker.id))
|
||||
assert_true(remote.sample(walker.id).is_empty())
|
||||
|
||||
|
||||
func test_the_clock_runs_evenly_through_jittery_arrivals() -> void:
|
||||
# Snapshots every STEP ticks, each arriving up to 40 ms early or late, drawn at 60 Hz.
|
||||
var rng: RandomNumberGenerator = RandomNumberGenerator.new()
|
||||
rng.seed = 12
|
||||
var arrivals: Array[float] = []
|
||||
for index: int in 300:
|
||||
arrivals.append(index * STEP * DT + rng.randf_range(-0.04, 0.04))
|
||||
var next: int = 0
|
||||
var slowest: float = INF
|
||||
var fastest: float = 0.0
|
||||
var worst_lag: float = 0.0
|
||||
for frame: int in 1100:
|
||||
var now: float = frame / 60.0
|
||||
while next < arrivals.size() and arrivals[next] <= now:
|
||||
walker.position.x = 100.0 + next
|
||||
_push(next * STEP)
|
||||
next += 1
|
||||
var before: float = remote.time
|
||||
remote.advance(1.0 / 60.0)
|
||||
if frame > 120 and next < arrivals.size():
|
||||
slowest = minf(slowest, remote.time - before)
|
||||
fastest = maxf(fastest, remote.time - before)
|
||||
worst_lag = maxf(worst_lag, absf(now * Tuning.TICK_RATE_HZ - DELAY - remote.time))
|
||||
assert_true(slowest > 0.45 and fastest < 0.55, "ran %.3f to %.3f ticks a frame" % [slowest, fastest])
|
||||
assert_true(worst_lag < 2.0, "strayed %.2f ticks from where it should be" % worst_lag)
|
||||
Reference in New Issue
Block a user