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:
@@ -264,3 +264,54 @@ func test_no_viewer_means_an_empty_snapshot() -> void:
|
||||
assert_eq(receiver.entities.size(), 0)
|
||||
assert_eq(receiver.own_id, 0)
|
||||
assert_eq(receiver.match_phase, Match.LOBBY)
|
||||
|
||||
|
||||
func test_the_own_block_goes_only_to_a_living_player_watching_themself() -> void:
|
||||
var other: SimEntity = _other(Vector3(520.0, 0.0, 500.0))
|
||||
viewer.velocity = Vector3(1.5, 0.0, -2.25)
|
||||
sim.sea_level = 1.5
|
||||
tick += STEP
|
||||
var mine: Dictionary = NetSnapshot.decode_snapshot(sender.build(HANDLE, sim, viewer.id, tick, Match.PLAY, viewer.id))
|
||||
assert_eq(mine["self"]["position"], viewer.position)
|
||||
assert_eq(mine["self"]["velocity"], viewer.velocity)
|
||||
assert_eq(mine["self"]["water"], 1.5)
|
||||
assert_eq(mine["self"]["water_mask"], NetSnapshot.WATER_EVERYWHERE, "no flood on a test ground: the sea is everywhere")
|
||||
tick += STEP
|
||||
var watching: Dictionary = NetSnapshot.decode_snapshot(sender.build(HANDLE, sim, other.id, tick, Match.PLAY, viewer.id))
|
||||
assert_true((watching["self"] as Dictionary).is_empty(), "spectating someone else")
|
||||
viewer.eliminated = true
|
||||
tick += STEP
|
||||
var out: Dictionary = NetSnapshot.decode_snapshot(sender.build(HANDLE, sim, viewer.id, tick, Match.PLAY, viewer.id))
|
||||
assert_true((out["self"] as Dictionary).is_empty(), "eliminated")
|
||||
tick += STEP
|
||||
var lobby: Dictionary = NetSnapshot.decode_snapshot(sender.build(HANDLE, sim, 0, tick, Match.LOBBY, 0))
|
||||
assert_true((lobby["self"] as Dictionary).is_empty(), "no player")
|
||||
|
||||
|
||||
func test_the_own_block_says_which_cells_around_the_player_are_flooded() -> void:
|
||||
# A real island: the sea reaches the shore cells and not the land beside them.
|
||||
var island: Simulation = Simulation.new(11)
|
||||
var flood: Flood = island.flood
|
||||
var player: SimEntity = island.entities.find(island.add_player())
|
||||
var found: bool = false
|
||||
for cz: int in range(1, flood.side - 1):
|
||||
for cx: int in range(1, flood.side - 1):
|
||||
var here: bool = flood.flooded[cz * flood.side + cx] != 0
|
||||
var east: bool = flood.flooded[cz * flood.side + cx + 1] != 0
|
||||
if here and not east:
|
||||
player.position = Vector3((cx + 0.5) * flood.cell, 0.0, (cz + 0.5) * flood.cell)
|
||||
found = true
|
||||
break
|
||||
if found:
|
||||
break
|
||||
assert_true(found, "a shore cell")
|
||||
var own: Dictionary = SnapshotSender.own_block(island, player)
|
||||
assert_eq(own["water"], flood.sea_level)
|
||||
var mask: int = own["water_mask"]
|
||||
assert_true(mask & (1 << 4) != 0, "the player's own cell is flooded")
|
||||
assert_true(mask & (1 << 5) == 0, "the cell to the east is dry")
|
||||
for dz: int in range(-1, 2):
|
||||
for dx: int in range(-1, 2):
|
||||
var at: Vector3 = player.position + Vector3(dx * flood.cell, 0.0, dz * flood.cell)
|
||||
var wet: bool = island.water_level_at(at) > Flood.NO_WATER
|
||||
assert_eq(mask & (1 << ((dz + 1) * 3 + dx + 1)) != 0, wet, "cell %d, %d" % [dx, dz])
|
||||
|
||||
Reference in New Issue
Block a user