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:
+57
-30
@@ -1,6 +1,6 @@
|
||||
## Joins the transport to the match: people who connect become participants, their
|
||||
## input packets become commands through ServerLoop.submit(), and everyone is sent
|
||||
## snapshots of what they may see, 20 times a second.
|
||||
## snapshots of what they may see, 15 times a second.
|
||||
##
|
||||
## Call poll() once a frame before running ticks, and after_tick() after each tick.
|
||||
class_name NetGame
|
||||
@@ -15,7 +15,7 @@ var events: Array[Dictionary] = []
|
||||
## Input packets that couldn't be read, so far.
|
||||
var bad_inputs: int = 0
|
||||
|
||||
## Handle -> the newest command tick taken from them this match.
|
||||
## Handle -> the newest command tick received from them this match.
|
||||
var _last_command: Dictionary = {}
|
||||
|
||||
|
||||
@@ -35,8 +35,7 @@ func poll() -> void:
|
||||
var handle: int = int(event.get("handle", 0))
|
||||
if event["type"] == "joined":
|
||||
if loop.match_flow.join(handle):
|
||||
sender.add_client(handle)
|
||||
_last_command[handle] = -1
|
||||
adopt(handle)
|
||||
_send_match(handle)
|
||||
events.append(event)
|
||||
else:
|
||||
@@ -47,24 +46,36 @@ func poll() -> void:
|
||||
_last_command.erase(handle)
|
||||
events.append(event)
|
||||
for packet: Dictionary in net.packets:
|
||||
var handle: int = packet["handle"]
|
||||
if not sender.has_client(handle):
|
||||
take_input(packet["handle"], packet["bytes"])
|
||||
|
||||
|
||||
## Starts sending snapshots to a participant who is already in the match flow, and
|
||||
## taking their input. poll() does this for everyone who joins over the network.
|
||||
func adopt(handle: int) -> void:
|
||||
sender.add_client(handle)
|
||||
_last_command[handle] = -1
|
||||
|
||||
|
||||
## Takes one INPUT packet from a participant: the snapshot they hold, and commands.
|
||||
func take_input(handle: int, bytes: PackedByteArray) -> void:
|
||||
if not sender.has_client(handle):
|
||||
return
|
||||
var input: Dictionary = NetSnapshot.decode_input(bytes)
|
||||
if input.is_empty():
|
||||
bad_inputs += 1
|
||||
return
|
||||
sender.acknowledge(handle, int(input["ack"]))
|
||||
if not loop.match_flow.in_match():
|
||||
return
|
||||
# Packets repeat recent commands; each tick's command is offered once, whether
|
||||
# or not the simulation takes it. The newest tick received is echoed either way,
|
||||
# so a client whose commands come too late or too early can tell.
|
||||
for command: PlayerCommand in input["commands"]:
|
||||
if command.tick <= int(_last_command[handle]):
|
||||
continue
|
||||
var input: Dictionary = NetSnapshot.decode_input(packet["bytes"])
|
||||
if input.is_empty():
|
||||
bad_inputs += 1
|
||||
continue
|
||||
sender.acknowledge(handle, int(input["ack"]))
|
||||
if not loop.match_flow.in_match():
|
||||
continue
|
||||
# Packets repeat recent commands; each tick's command is offered once, whether
|
||||
# or not the simulation takes it.
|
||||
for command: PlayerCommand in input["commands"]:
|
||||
if command.tick <= int(_last_command[handle]):
|
||||
continue
|
||||
_last_command[handle] = command.tick
|
||||
if loop.submit(handle, command):
|
||||
sender.note_command(handle, command.tick)
|
||||
_last_command[handle] = command.tick
|
||||
sender.note_command(handle, command.tick)
|
||||
loop.submit(handle, command)
|
||||
|
||||
|
||||
## Tells everyone about a change of phase, and sends snapshots when one is due.
|
||||
@@ -83,13 +94,32 @@ func after_tick() -> void:
|
||||
if changed:
|
||||
for handle: int in net.handles():
|
||||
_send_match(handle)
|
||||
if loop.ticks % Tuning.NET_SNAPSHOT_INTERVAL_TICKS != 0:
|
||||
if not snapshot_due():
|
||||
return
|
||||
for handle: int in net.handles():
|
||||
if not sender.has_client(handle):
|
||||
continue
|
||||
var bytes: PackedByteArray = sender.build(handle, loop.sim, viewer_of(handle), loop.ticks, flow.phase)
|
||||
net.send(handle, NetProtocol.CHANNEL_STATE, bytes, false)
|
||||
if sender.has_client(handle):
|
||||
net.send(handle, NetProtocol.CHANNEL_STATE, snapshot_for(handle), false)
|
||||
|
||||
|
||||
## True on the ticks a snapshot goes out.
|
||||
func snapshot_due() -> bool:
|
||||
return loop.ticks % Tuning.NET_SNAPSHOT_INTERVAL_TICKS == 0
|
||||
|
||||
|
||||
## The snapshot for a participant as things stand: what they may see, and their own
|
||||
## block while they are playing.
|
||||
func snapshot_for(handle: int) -> PackedByteArray:
|
||||
var flow: Match = loop.match_flow
|
||||
var own_id: int = int(flow.entity_of.get(handle, 0)) if flow.in_match() else 0
|
||||
return sender.build(handle, loop.sim, viewer_of(handle), loop.ticks, flow.phase, own_id)
|
||||
|
||||
|
||||
## The MATCH notice as things stand: the phase, and the match's index and seed.
|
||||
func match_notice() -> PackedByteArray:
|
||||
var flow: Match = loop.match_flow
|
||||
var started: bool = flow.in_match() or flow.phase == Match.RESULTS
|
||||
var index: int = flow.matches_started - 1 if started else flow.matches_started
|
||||
return NetSnapshot.encode_match(flow.phase, index, flow.current_seed())
|
||||
|
||||
|
||||
## The entity a participant is watching: their own player, the player they spectate
|
||||
@@ -105,7 +135,4 @@ func viewer_of(handle: int) -> int:
|
||||
|
||||
|
||||
func _send_match(handle: int) -> void:
|
||||
var flow: Match = loop.match_flow
|
||||
var started: bool = flow.in_match() or flow.phase == Match.RESULTS
|
||||
var index: int = flow.matches_started - 1 if started else flow.matches_started
|
||||
net.send(handle, NetProtocol.CHANNEL_CONTROL, NetSnapshot.encode_match(flow.phase, index, flow.current_seed()), true)
|
||||
net.send(handle, NetProtocol.CHANNEL_CONTROL, match_notice(), true)
|
||||
|
||||
Reference in New Issue
Block a user