N2: Snapshot replication and interest management
NetSnapshot defines the game traffic after the handshake: delta snapshots of quantized entity records, the client's input packet and a reliable match notice. SnapshotSender builds each client's snapshot against the newest one it acknowledged, tells it only about entities near the player it is watching, and keeps every snapshot under 1,200 bytes, nearest first, with a staleness bonus so nothing starves. SnapshotReceiver rebuilds the picture, ignores old and repeated snapshots and asks for a fresh start if it is ever sent a delta on something it doesn't hold. NetGame joins the transport to the match: people who connect become participants, their inputs go through ServerLoop.submit() once per tick, and snapshots go out 20 times a second. `--server --listen` makes the headless server take network players and wait in the lobby for the first of them. Measured by test on a 24-player match start with every player a client, a tenth of packets lost and late acknowledgements: 2.4 KB/s mean and 2.5 KB/s for the worst client, packet headers included, against a 40 KB/s limit. Closes #33 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,266 @@
|
||||
extends TestCase
|
||||
|
||||
## Issue N2: SnapshotSender and SnapshotReceiver together, over a link that loses,
|
||||
## delays and repeats packets. Interest management and the size budget.
|
||||
|
||||
const HANDLE: int = 1
|
||||
const STEP: int = Tuning.NET_SNAPSHOT_INTERVAL_TICKS
|
||||
|
||||
var sim: Simulation
|
||||
var viewer: SimEntity
|
||||
var sender: SnapshotSender
|
||||
var receiver: SnapshotReceiver
|
||||
var tick: int = 0
|
||||
## Every packet built for the client, delivered or not.
|
||||
var packets: Array[PackedByteArray] = []
|
||||
|
||||
|
||||
func before_each() -> void:
|
||||
sim = Simulation.new(5, SimGround.flat())
|
||||
viewer = sim.entities.find(sim.add_player())
|
||||
viewer.position = Vector3(500.0, 0.0, 500.0)
|
||||
sender = SnapshotSender.new()
|
||||
sender.add_client(HANDLE)
|
||||
receiver = SnapshotReceiver.new()
|
||||
tick = 0
|
||||
packets.clear()
|
||||
|
||||
|
||||
func _other(at: Vector3) -> SimEntity:
|
||||
var player: SimEntity = sim.entities.find(sim.add_player())
|
||||
player.position = at
|
||||
return player
|
||||
|
||||
|
||||
## Builds the next snapshot. Delivers it and returns the acknowledgement unless told not to.
|
||||
func _send(deliver: bool = true, acknowledge: bool = true) -> PackedByteArray:
|
||||
tick += STEP
|
||||
var bytes: PackedByteArray = sender.build(HANDLE, sim, viewer.id, tick, Match.PLAY)
|
||||
packets.append(bytes)
|
||||
assert_true(bytes.size() <= Tuning.NET_SNAPSHOT_MAX_BYTES, "snapshot of %d bytes" % bytes.size())
|
||||
if deliver:
|
||||
receiver.apply(bytes)
|
||||
if acknowledge:
|
||||
sender.acknowledge(HANDLE, receiver.ack_tick())
|
||||
return bytes
|
||||
|
||||
|
||||
## Fails unless the client's picture is exactly what it may see, as it is now.
|
||||
func _assert_in_step(context: String) -> void:
|
||||
var visible: Array[int] = SnapshotSender.interest(sim, viewer)
|
||||
assert_eq(receiver.entities.size(), visible.size(), "%s: entity count" % context)
|
||||
for entity_id: int in visible:
|
||||
if not receiver.entities.has(entity_id):
|
||||
fail("%s: entity %d is missing" % [context, entity_id])
|
||||
continue
|
||||
assert_eq(receiver.entities[entity_id], NetSnapshot.record_of(sim.entities.find(entity_id)), "%s: entity %d" % [context, entity_id])
|
||||
|
||||
|
||||
func test_first_snapshot_is_full_and_later_ones_carry_only_changes() -> void:
|
||||
var other: SimEntity = _other(Vector3(520.0, 0.0, 500.0))
|
||||
var first: PackedByteArray = _send()
|
||||
assert_eq(NetSnapshot.decode_snapshot(first)["baseline"], 0)
|
||||
assert_eq(receiver.own_id, viewer.id)
|
||||
_assert_in_step("first")
|
||||
var quiet: Dictionary = NetSnapshot.decode_snapshot(_send())
|
||||
assert_eq(quiet["baseline"], tick - STEP)
|
||||
assert_eq((quiet["records"] as Array).size(), 0, "nothing moved")
|
||||
other.position.x += 1.0
|
||||
var moved: Dictionary = NetSnapshot.decode_snapshot(_send())
|
||||
assert_eq((moved["records"] as Array).size(), 1)
|
||||
assert_eq(moved["records"][0][2], 0b1, "only the position group")
|
||||
_assert_in_step("moved")
|
||||
assert_eq(sender.stats(HANDLE)["full"], 1)
|
||||
|
||||
|
||||
func test_header_carries_the_tide_the_clock_and_the_command_echo() -> void:
|
||||
sim.tide = Tide.new(0)
|
||||
sim.sea_level = 3.25
|
||||
sim.tick = 77
|
||||
sender.note_command(HANDLE, 75)
|
||||
sender.note_command(HANDLE, 60)
|
||||
_send()
|
||||
assert_near(receiver.sea_level, 3.25, 0.001)
|
||||
assert_eq(receiver.sim_tick, 77)
|
||||
assert_eq(receiver.command_tick, 75)
|
||||
assert_eq(receiver.match_phase, Match.PLAY)
|
||||
|
||||
|
||||
func test_the_client_recovers_from_lost_snapshots_and_lost_acknowledgements() -> void:
|
||||
var others: Array[SimEntity] = []
|
||||
for index: int in 12:
|
||||
others.append(_other(Vector3(480.0 + index * 4.0, 0.0, 510.0)))
|
||||
var rng: RandomNumberGenerator = RandomNumberGenerator.new()
|
||||
rng.seed = 2026
|
||||
var delivered: int = 0
|
||||
for round: int in 400:
|
||||
for other: SimEntity in others:
|
||||
if rng.randf() < 0.6:
|
||||
other.position += Vector3(rng.randf_range(-1.0, 1.0), 0.0, rng.randf_range(-1.0, 1.0))
|
||||
other.yaw = rng.randi_range(0, 65535)
|
||||
if rng.randf() < 0.05:
|
||||
other.health = rng.randi_range(1, 100)
|
||||
if round == 150:
|
||||
sim.remove_player(others.pop_back().id)
|
||||
var arrives: bool = rng.randf() >= 0.4
|
||||
_send(arrives, rng.randf() >= 0.4)
|
||||
if arrives:
|
||||
delivered += 1
|
||||
_assert_in_step("round %d" % round)
|
||||
assert_true(delivered > 150 and delivered < 300, "the link lost a real share: %d of 400 arrived" % delivered)
|
||||
assert_eq(receiver.missing_baseline, 0, "a delta never builds on something the client didn't confirm")
|
||||
assert_eq(receiver.malformed, 0)
|
||||
assert_eq(sender.stats(HANDLE)["full"], 1, "loss alone never needs a second full snapshot")
|
||||
|
||||
|
||||
func test_duplicates_and_stale_snapshots_are_ignored() -> void:
|
||||
var other: SimEntity = _other(Vector3(520.0, 0.0, 500.0))
|
||||
var old: PackedByteArray = _send()
|
||||
other.position.x += 5.0
|
||||
var newer: PackedByteArray = _send()
|
||||
assert_false(receiver.apply(old), "an older snapshot arriving late")
|
||||
assert_false(receiver.apply(newer), "the same snapshot twice")
|
||||
assert_eq(receiver.ignored_old, 2)
|
||||
_assert_in_step("after repeats")
|
||||
|
||||
|
||||
func test_a_long_silence_falls_back_to_a_full_snapshot() -> void:
|
||||
var other: SimEntity = _other(Vector3(520.0, 0.0, 500.0))
|
||||
_send()
|
||||
# Longer than the server remembers: everything it could build on is forgotten.
|
||||
for _round: int in Tuning.NET_SNAPSHOT_HISTORY + 5:
|
||||
other.position.x += 0.1
|
||||
_send(false, false)
|
||||
var bytes: PackedByteArray = _send()
|
||||
assert_eq(NetSnapshot.decode_snapshot(bytes)["baseline"], 0)
|
||||
_assert_in_step("after the silence")
|
||||
assert_true(sender.stats(HANDLE)["full"] >= 2)
|
||||
|
||||
|
||||
func test_a_client_that_lost_its_baseline_asks_again_and_recovers() -> void:
|
||||
_other(Vector3(520.0, 0.0, 500.0))
|
||||
_send()
|
||||
_send()
|
||||
# A client that restarted its picture: it holds nothing the server builds on.
|
||||
receiver = SnapshotReceiver.new()
|
||||
_send()
|
||||
assert_eq(receiver.missing_baseline, 1)
|
||||
assert_eq(receiver.ack_tick(), 0)
|
||||
assert_eq(receiver.entities.size(), 0)
|
||||
_send()
|
||||
_assert_in_step("after asking again")
|
||||
|
||||
|
||||
func test_no_data_about_entities_outside_the_interest_area() -> void:
|
||||
var range: float = Tuning.NET_INTEREST_PLAYERS_METRES
|
||||
var near: SimEntity = _other(viewer.position + Vector3(range - 5.0, 0.0, 0.0))
|
||||
var far: SimEntity = _other(viewer.position + Vector3(range + 5.0, 0.0, 0.0))
|
||||
var far_item: int = Inventory.spawn_pickup(sim, viewer.position + Vector3(0.0, 0.0, Tuning.NET_INTEREST_PICKUPS_METRES + 3.0), 1, 1)
|
||||
var near_item: int = Inventory.spawn_pickup(sim, viewer.position + Vector3(0.0, 0.0, Tuning.NET_INTEREST_PICKUPS_METRES - 3.0), 1, 1)
|
||||
for _round: int in 10:
|
||||
far.position.z += 1.0
|
||||
far.health -= 1
|
||||
_send()
|
||||
assert_true(receiver.entities.has(near.id) and receiver.entities.has(near_item))
|
||||
for bytes: PackedByteArray in packets:
|
||||
var snapshot: Dictionary = NetSnapshot.decode_snapshot(bytes)
|
||||
for entry: Array in snapshot["records"]:
|
||||
assert_true(entry[0] != far.id and entry[0] != far_item, "a record for an entity out of range")
|
||||
assert_false((snapshot["removed"] as PackedInt32Array).has(far.id), "even its ID stays unsaid")
|
||||
# It walks into range: now the client hears of it, in full.
|
||||
far.position = viewer.position + Vector3(range - 1.0, 0.0, 0.0)
|
||||
_send()
|
||||
assert_true(receiver.entities.has(far.id))
|
||||
assert_eq(Array(receiver.added), [far.id])
|
||||
# And out again: it is removed, and nothing more is said about it.
|
||||
far.position = viewer.position + Vector3(range + 1.0, 0.0, 0.0)
|
||||
_send()
|
||||
assert_false(receiver.entities.has(far.id))
|
||||
assert_eq(Array(receiver.removed), [far.id])
|
||||
var before: int = packets.size()
|
||||
for _round: int in 5:
|
||||
far.position.z -= 1.0
|
||||
_send()
|
||||
for index: int in range(before, packets.size()):
|
||||
assert_eq((NetSnapshot.decode_snapshot(packets[index])["records"] as Array).size(), 0)
|
||||
_assert_in_step("at the end")
|
||||
|
||||
|
||||
func test_height_does_not_hide_a_player_overhead() -> void:
|
||||
var above: SimEntity = _other(viewer.position + Vector3(10.0, Tuning.BARGE_ALTITUDE, 0.0))
|
||||
_send()
|
||||
assert_true(receiver.entities.has(above.id))
|
||||
|
||||
|
||||
func test_a_crowd_is_spread_over_several_snapshots_nearest_first() -> void:
|
||||
var ids: Array[int] = []
|
||||
for index: int in 600:
|
||||
var angle: float = index * 0.37
|
||||
var reach: float = 2.0 + float(index) / 600.0 * (Tuning.NET_INTEREST_PICKUPS_METRES - 4.0)
|
||||
ids.append(Inventory.spawn_pickup(sim, viewer.position + Vector3(cos(angle) * reach, 0.0, sin(angle) * reach), 1, 1))
|
||||
var first: PackedByteArray = _send()
|
||||
assert_true(first.size() > Tuning.NET_SNAPSHOT_MAX_BYTES - 16, "the first snapshot is full to the brim")
|
||||
assert_true(receiver.entities.has(viewer.id), "the client's own player always travels")
|
||||
var got: int = receiver.entities.size() - 1
|
||||
assert_true(got > 50 and got < 600, "%d of 600 fitted" % got)
|
||||
var furthest_got: float = 0.0
|
||||
var nearest_missing: float = 1.0e9
|
||||
for entity_id: int in ids:
|
||||
var distance: float = sim.entities.find(entity_id).position.distance_to(viewer.position)
|
||||
if receiver.entities.has(entity_id):
|
||||
furthest_got = maxf(furthest_got, distance)
|
||||
else:
|
||||
nearest_missing = minf(nearest_missing, distance)
|
||||
assert_true(furthest_got <= nearest_missing + 0.1, "nearest first: got out to %.1f m, missing from %.1f m" % [furthest_got, nearest_missing])
|
||||
for _round: int in 12:
|
||||
_send()
|
||||
_assert_in_step("after a dozen snapshots")
|
||||
assert_true(sender.stats(HANDLE)["deferred"] > 0)
|
||||
|
||||
|
||||
func test_a_distant_entity_is_not_starved_by_a_busy_foreground() -> void:
|
||||
# More near entities changing every snapshot than one snapshot can hold.
|
||||
var busy: Array[SimEntity] = []
|
||||
for index: int in 200:
|
||||
busy.append(_other(viewer.position + Vector3(5.0 + index * 0.5, 0.0, 3.0)))
|
||||
var distant: SimEntity = _other(viewer.position + Vector3(0.0, 0.0, Tuning.NET_INTEREST_PLAYERS_METRES - 10.0))
|
||||
for _round: int in 30:
|
||||
_send()
|
||||
var seen: int = 0
|
||||
for round: int in 60:
|
||||
for other: SimEntity in busy:
|
||||
other.position.z += 0.25
|
||||
other.yaw = (other.yaw + 500) % 65536
|
||||
distant.position.x += 0.5
|
||||
var before: PackedInt32Array = receiver.entities.get(distant.id, PackedInt32Array())
|
||||
_send()
|
||||
if receiver.entities.get(distant.id, PackedInt32Array()) != before:
|
||||
seen += 1
|
||||
assert_true(sender.stats(HANDLE)["deferred"] > 0, "the snapshots really were over full")
|
||||
assert_true(seen >= 3, "the distant player was updated %d times in 60 snapshots" % seen)
|
||||
|
||||
|
||||
func test_a_new_match_starts_from_nothing() -> void:
|
||||
_other(Vector3(520.0, 0.0, 500.0))
|
||||
_send()
|
||||
_send()
|
||||
# A new simulation reuses entity IDs for different things.
|
||||
sender.reset()
|
||||
sim = Simulation.new(6, SimGround.flat())
|
||||
viewer = sim.entities.find(sim.add_player())
|
||||
viewer.position = Vector3(100.0, 0.0, 100.0)
|
||||
Inventory.spawn_pickup(sim, Vector3(101.0, 0.0, 100.0), 1, 1)
|
||||
var bytes: PackedByteArray = _send()
|
||||
assert_eq(NetSnapshot.decode_snapshot(bytes)["baseline"], 0)
|
||||
_assert_in_step("new match")
|
||||
assert_eq(receiver.malformed, 0)
|
||||
|
||||
|
||||
func test_no_viewer_means_an_empty_snapshot() -> void:
|
||||
_other(Vector3(520.0, 0.0, 500.0))
|
||||
tick += STEP
|
||||
var bytes: PackedByteArray = sender.build(HANDLE, sim, 0, tick, Match.LOBBY)
|
||||
assert_true(receiver.apply(bytes))
|
||||
assert_eq(receiver.entities.size(), 0)
|
||||
assert_eq(receiver.own_id, 0)
|
||||
assert_eq(receiver.match_phase, Match.LOBBY)
|
||||
Reference in New Issue
Block a user