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,142 @@
|
||||
extends TestCase
|
||||
|
||||
## Issue N3: ClientGame against a real server. Over loopback UDP with a NetClient, and
|
||||
## by hand, packet by packet.
|
||||
|
||||
const HOST: String = "127.0.0.1"
|
||||
const DT: float = 1.0 / Tuning.TICK_RATE_HZ
|
||||
|
||||
var loop: ServerLoop
|
||||
var net: NetServer
|
||||
var game: NetGame
|
||||
|
||||
|
||||
func _rules() -> MatchRules:
|
||||
var rules: MatchRules = MatchRules.new()
|
||||
rules.max_players = 3
|
||||
rules.min_players = 1
|
||||
rules.lobby_ticks = 6
|
||||
rules.countdown_ticks = 6
|
||||
rules.results_ticks = 6
|
||||
return rules
|
||||
|
||||
|
||||
func before_each() -> void:
|
||||
loop = ServerLoop.new(31, 0, 2, _rules())
|
||||
net = NetServer.new()
|
||||
game = NetGame.new(loop, net)
|
||||
|
||||
|
||||
func after_each() -> void:
|
||||
net.close()
|
||||
|
||||
|
||||
func test_a_client_joins_over_udp_generates_the_island_and_plays() -> void:
|
||||
assert_eq(net.listen(0, HOST), OK)
|
||||
var link: NetClient = NetClient.new()
|
||||
var client: ClientGame = ClientGame.new()
|
||||
client.attach(link)
|
||||
client.sample_input = func(tick: int) -> PlayerCommand:
|
||||
return PlayerCommand.from_input(tick, Vector2(0.0, 1.0), 2.0, 0.1)
|
||||
assert_eq(link.connect_to(HOST, net.port(), "Mara"), OK)
|
||||
var started: int = Time.get_ticks_msec()
|
||||
var played: int = 0
|
||||
# A server tick and a client frame of the same length each round, so both clocks
|
||||
# agree however fast the test machine is.
|
||||
while Time.get_ticks_msec() - started < 20000 and played < 150:
|
||||
game.poll()
|
||||
loop.run_tick()
|
||||
game.after_tick()
|
||||
client.poll()
|
||||
client.advance(DT)
|
||||
if client.playing():
|
||||
played += 1
|
||||
OS.delay_msec(1)
|
||||
link.leave()
|
||||
assert_eq(played, 150, "the client got into the match and played")
|
||||
var flow: Match = loop.match_flow
|
||||
assert_true(flow.in_match())
|
||||
assert_eq(client.world_seed, flow.current_seed())
|
||||
assert_eq(client.world.island.heightmap_hash(), loop.sim.island.heightmap_hash(), "the same island on both ends")
|
||||
assert_eq(client.match_phase, flow.phase)
|
||||
var mine: SimEntity = loop.sim.entities.find(flow.entity_of[link.handle])
|
||||
assert_eq(client.prediction.player.id, mine.id)
|
||||
assert_eq(mine.yaw, PlayerCommand.from_input(0, Vector2.ZERO, 2.0, 0.0).yaw, "the server is applying this client's commands")
|
||||
assert_eq(client.prediction.player.drop_state, mine.drop_state)
|
||||
assert_true(client.prediction.reconciles > 50, "%d snapshots compared" % client.prediction.reconciles)
|
||||
assert_true(client.prediction.max_correction < 2.0, "largest correction %.3f m" % client.prediction.max_correction)
|
||||
assert_true(client.own_position().distance_to(mine.position) < 10.0, "drawn %.1f m from where the server has it" % client.own_position().distance_to(mine.position))
|
||||
# The two bots ride the same barge.
|
||||
assert_eq(flow.entity_of.size(), 3)
|
||||
for handle: int in flow.entity_of:
|
||||
assert_true(client.remote.has(flow.entity_of[handle]), "player %d is drawn" % handle)
|
||||
assert_eq(client.bad_packets, 0)
|
||||
assert_eq(game.bad_inputs, 0)
|
||||
|
||||
|
||||
func test_in_the_lobby_it_only_says_which_snapshot_it_holds() -> void:
|
||||
var worlds: Array[int] = []
|
||||
var client: ClientGame = ClientGame.new()
|
||||
client.make_world = func(match_seed: int) -> Simulation:
|
||||
worlds.append(match_seed)
|
||||
return Simulation.new(match_seed, SimGround.flat())
|
||||
loop.match_flow.rules.min_players = 5
|
||||
assert_true(loop.match_flow.join(7))
|
||||
game.adopt(7)
|
||||
client.receive(NetProtocol.CHANNEL_CONTROL, game.match_notice())
|
||||
assert_eq(worlds, [loop.match_flow.current_seed()], "the island is made as soon as the seed is known")
|
||||
for _round: int in 4:
|
||||
loop.run_tick()
|
||||
game.after_tick()
|
||||
if game.snapshot_due():
|
||||
client.receive(NetProtocol.CHANNEL_STATE, game.snapshot_for(7))
|
||||
client.advance(DT)
|
||||
assert_false(client.playing())
|
||||
assert_eq(client.match_phase, Match.LOBBY)
|
||||
assert_eq(client.outbox.size(), 2, "one answer per snapshot")
|
||||
var answer: Dictionary = NetSnapshot.decode_input(client.outbox[1])
|
||||
assert_eq(answer["ack"], client.receiver.tick)
|
||||
assert_eq((answer["commands"] as Array).size(), 0)
|
||||
# Junk is counted and changes nothing.
|
||||
client.receive(NetProtocol.CHANNEL_CONTROL, PackedByteArray([NetSnapshot.MSG_MATCH, 1, 2]))
|
||||
client.receive(NetProtocol.CHANNEL_STATE, PackedByteArray([NetSnapshot.MSG_SNAPSHOT, 9, 9]))
|
||||
assert_eq(client.bad_packets, 1)
|
||||
assert_eq(client.receiver.malformed, 1)
|
||||
assert_eq(worlds.size(), 1)
|
||||
|
||||
|
||||
func test_a_new_match_starts_everything_again() -> void:
|
||||
var worlds: Array[int] = []
|
||||
var client: ClientGame = ClientGame.new()
|
||||
client.make_world = func(match_seed: int) -> Simulation:
|
||||
worlds.append(match_seed)
|
||||
return Simulation.new(match_seed, SimGround.flat())
|
||||
assert_true(loop.match_flow.join(7))
|
||||
game.adopt(7)
|
||||
client.receive(NetProtocol.CHANNEL_CONTROL, game.match_notice())
|
||||
var first_seed: int = loop.match_flow.current_seed()
|
||||
var sent: int = 0
|
||||
for _round: int in 60:
|
||||
for bytes: PackedByteArray in client.outbox:
|
||||
game.take_input(7, bytes)
|
||||
sent += 1
|
||||
client.outbox.clear()
|
||||
loop.run_tick()
|
||||
game.after_tick()
|
||||
for event: Dictionary in loop.match_flow.events:
|
||||
if event["type"] == "phase":
|
||||
client.receive(NetProtocol.CHANNEL_CONTROL, game.match_notice())
|
||||
if game.snapshot_due():
|
||||
client.receive(NetProtocol.CHANNEL_STATE, game.snapshot_for(7))
|
||||
client.advance(DT)
|
||||
assert_true(client.playing(), "phase %s" % loop.match_flow.phase_name())
|
||||
assert_true(sent > 30)
|
||||
assert_true(client.receiver.command_tick >= 0, "the server echoes the commands it gets")
|
||||
assert_eq(worlds, [first_seed], "one world for the whole match")
|
||||
# The server moves on to another match: a notice with the next index and seed.
|
||||
client.receive(NetProtocol.CHANNEL_CONTROL, NetSnapshot.encode_match(Match.LOBBY, 1, first_seed + 1))
|
||||
assert_false(client.playing())
|
||||
assert_false(client.clock.running)
|
||||
assert_false(client.remote.started)
|
||||
assert_eq(worlds, [first_seed, first_seed + 1])
|
||||
assert_eq(client.match_index, 1)
|
||||
Reference in New Issue
Block a user