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:
2026-11-12 08:26:02 -05:00
co-authored by Claude Opus 5.5
parent 881f449d10
commit e535c1f39c
26 changed files with 1695 additions and 24 deletions
+18 -2
View File
@@ -20,6 +20,11 @@ var match_seed: int = 0
## What happened during the last poll(): {"type": "joined", "handle", "name"},
## {"type": "left", "handle", "name", "reason"} and {"type": "refused", "reason"}.
var events: Array[Dictionary] = []
## Game packets from joined players during the last poll(), in arrival order:
## {"handle", "bytes"}. Whoever owns the server decodes them.
var packets: Array[Dictionary] = []
## The most game packets kept from one player in one poll(); the rest are dropped.
var max_packets_per_poll: int = 32
## Malformed or unexpected packets dropped so far.
var bad_packets: int = 0
@@ -33,6 +38,8 @@ var _players: Dictionary = {}
## ENetPacketPeer -> handle.
var _handle_of: Dictionary = {}
var _next_handle: int = 1
## Handle -> game packets kept from them during this poll().
var _packets_from: Dictionary = {}
## Starts listening on UDP `port` (0 picks a free one) on `address`. Returns OK or the
@@ -82,6 +89,8 @@ func name_of(handle: int) -> String:
## Call it every frame. `now_msec` is for tests; it defaults to the engine's clock.
func poll(now_msec: int = -1) -> void:
events.clear()
packets.clear()
_packets_from.clear()
if _host == null:
return
var now: int = now_msec if now_msec >= 0 else Time.get_ticks_msec()
@@ -147,8 +156,15 @@ func _received(peer: ENetPacketPeer, channel: int, bytes: PackedByteArray) -> vo
if _leaving.has(peer):
return
if _handle_of.has(peer):
# Game traffic arrives with N2. A second HELLO, or anything else, is dropped.
bad_packets += 1
# Game traffic goes to the owner. A second HELLO, anything on the control
# channel, and a flood are dropped.
var handle: int = _handle_of[peer]
var count: int = int(_packets_from.get(handle, 0))
if channel != NetProtocol.CHANNEL_STATE or bytes.is_empty() or count >= max_packets_per_poll:
bad_packets += 1
return
_packets_from[handle] = count + 1
packets.append({"handle": handle, "bytes": bytes})
return
if not _guests.has(peer):
return