From 6b21e9d4f19bd91c736226362258b846d997a96b Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 12 Nov 2026 14:27:58 -0500 Subject: [PATCH] N5: Replicate building, world damage, tide and loot Built pieces, props' health, opened chests and the tide's schedule now reach clients as WORLD messages on the reliable channel, and each player is told what they carry as OWN messages. - NetWorld: the WORLD and OWN wire formats, and world_hash(), a digest of everything replicated this way. - WorldSender: one message after each tick in which something changed, the same for everyone, and the whole state for someone joining or a new match. StructureSet and PropSet keep the change lists it reads. - ClientWorld: applies them to the client's world, so pieces and broken props are solid for prediction. A piece's health follows from its building and damage, so building costs nothing on the wire. - Placing ahead of the server: a provisional piece goes up with the command and is replaced by the server's, or taken back once a snapshot shows the server had its chance. Snapshots carry the newest WORLD number for that. - Protocol version 3. A test plays a whole scripted match on a real island with a ClientGame attached and compares the two worlds' hashes throughout. Closes #36 Co-Authored-By: Claude Opus 5.5 --- docs/ARCHITECTURE.md | 60 ++++- src/net/client_game.gd | 56 +++- src/net/client_prediction.gd | 4 +- src/net/client_world.gd | 214 +++++++++++++++ src/net/client_world.gd.uid | 1 + src/net/net_protocol.gd | 2 +- src/net/net_snapshot.gd | 8 +- src/net/net_world.gd | 310 ++++++++++++++++++++++ src/net/net_world.gd.uid | 1 + src/net/snapshot_receiver.gd | 3 + src/net/snapshot_sender.gd | 6 +- src/net/world_sender.gd | 140 ++++++++++ src/net/world_sender.gd.uid | 1 + src/server/net_game.gd | 61 ++++- src/sim/building.gd | 39 ++- src/sim/prop_set.gd | 4 + src/sim/structure_set.gd | 42 ++- test/net/test_net_world.gd | 128 +++++++++ test/net/test_net_world.gd.uid | 1 + test/net/test_world_sync.gd | 296 +++++++++++++++++++++ test/net/test_world_sync.gd.uid | 1 + test/server/test_client_game.gd | 11 + test/server/test_world_replication.gd | 199 ++++++++++++++ test/server/test_world_replication.gd.uid | 1 + 24 files changed, 1547 insertions(+), 42 deletions(-) create mode 100644 src/net/client_world.gd create mode 100644 src/net/client_world.gd.uid create mode 100644 src/net/net_world.gd create mode 100644 src/net/net_world.gd.uid create mode 100644 src/net/world_sender.gd create mode 100644 src/net/world_sender.gd.uid create mode 100644 test/net/test_net_world.gd create mode 100644 test/net/test_net_world.gd.uid create mode 100644 test/net/test_world_sync.gd create mode 100644 test/net/test_world_sync.gd.uid create mode 100644 test/server/test_world_replication.gd create mode 100644 test/server/test_world_replication.gd.uid diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index b00cbce..0f78bf2 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -155,7 +155,7 @@ A snapshot is a binary, little-endian packet on channel 1, unreliable: | Part | Contents | |---|---| -| Header | Message type (u8), server tick (u32), baseline tick (u32, 0 = builds on nothing), simulation tick (u32), the newest command tick received from this client plus one (u32, 0 = none yet), the entity the client is watching (varint) | +| Header | Message type (u8), server tick (u32), baseline tick (u32, 0 = builds on nothing), simulation tick (u32), the newest command tick received from this client plus one (u32, 0 = none yet), the number of the newest world message made (u32), the entity the client is watching (varint) | | Match and tide | Match phase (u8), tide phase (u8), sea level in centimetres (s16) | | Own block | A flag (u8), then, while the client's own player is alive and is who it watches, 37 bytes nobody else is sent: position and velocity as f32, the water level around the player as f64, which of the nine flood cells around the player that water reaches (u16), swimming stamina (u16) and state flags (u8) | | Removed | Count (u8), then IDs (varint) | @@ -196,8 +196,55 @@ the newest tick it has received in every snapshot, taken or not. **Match notice.** On joining and at every change of phase the server sends, reliably on channel 0, the match phase, index and seed, so the client can generate the island. -Building changes will also travel on a reliable ordered event stream (N5), since a -dropped wall is a gameplay lie. +## World messages + +Built pieces, props, chests and the tide's schedule aren't entities and don't go in +snapshots. A wall the client never heard about is a lie it would keep walking into, +so they travel on channel 0, reliably and in order, as **WORLD** messages (`NetWorld`, +`src/net/`). + +| Part | Contents | +|---|---| +| Header | Message type (u8), flags (u8: whole state, loot placed, tide follows), sequence number (u32), simulation tick (u32) | +| Tide | Only when it starts: the tick it started (u32) and each phase's level (f32) | +| New pieces | ID, kind, material and facing in one byte, slot (s16, s8, s16, u8), owner, tick placed (u32), ticks of building done, damage taken | +| Damaged pieces | ID and damage taken so far | +| Removed pieces | IDs | +| Props | Prop and health left, for each one damaged | +| Chests | Indices of chests opened | + +Counts and IDs are varints. `WorldSender` makes one message after each tick in which +something changed and everyone is sent the same one; a quiet tick makes none. The +simulation keeps the lists it reads (`StructureSet.changed` and `.removed`, +`PropSet.changed`), so nothing is scanned. Someone joining is sent the **whole state** +in one message after the MATCH notice, and so is everyone when a new match's +simulation appears. + +What isn't sent: props, chests' places and their contents, which the client rolls +itself from the seed, as it does the island; and a piece's health, which follows from +how far its building had got, the ticks since, and the damage it has taken. So a +piece being built costs nothing on the wire. A scripted match of 12,198 ticks with 24 +players made 121 messages, 2.3 KB in all. Bots don't build yet, so a match of people +building will make far more: a new piece is about 16 bytes to each client. + +`ClientWorld` applies them to the client's world. `NetWorld.world_hash()` covers +everything replicated this way, and a client that has applied every message up to a +tick agrees with the server's simulation at that tick, which a test checks through a +whole match. + +Each player is also told what they carry, as an **OWN** message on the same channel +whenever it changes: held selection, materials, the five slots and ammunition. + +**Placing ahead of the server.** When the player's command places a piece and the +client's own run of `Building.check` passes, `ClientWorld` puts a provisional piece +in its world at once, under an ID the server never uses, and holds its cost back from +the materials shown. The server's piece arriving in that slot replaces it. Every +snapshot carries the number of the newest WORLD message made, so the client knows +when it has heard everything that snapshot's world had in it: once a snapshot from +after the placing tick has come, and the world messages up to its number have too, +and no piece came, the server turned it down (or never got the command) and the +provisional piece is taken away. Losing a race for a slot ends the same way, with +the winner's piece standing. ## Lag compensation @@ -260,9 +307,10 @@ which is the normal case. If not, the copy is put where the server says, every c since is run again, and the distance the player's present position moved is the correction. A correction is never shown at once: the drawn position keeps an offset that fades at `NET_CORRECTION_SMOOTH_RATE`, unless it is over 3 m, which is shown as -the teleport it is. What the client can't predict yet: built pieces and broken props -(not replicated until N5), a basin still filling after a breach, and anything another -player does to it. +the teleport it is. Built pieces and broken props are solid on the client as soon as +their WORLD message arrives, a moment after they are on the server, so a client +running into a wall that new is corrected. What the client can't predict: a basin +still filling after a breach, and anything another player does to it. **Interpolation.** `SnapshotInterpolator` draws every other entity `Tuning.NET_INTERP_DELAY_TICKS` (4 ticks, two snapshots) in the past, on a straight diff --git a/src/net/client_game.gd b/src/net/client_game.gd index 2f51f9c..8ebaa40 100644 --- a/src/net/client_game.gd +++ b/src/net/client_game.gd @@ -8,7 +8,10 @@ ## ## The client generates the match's island itself from the seed in the MATCH notice ## (`world`, a Simulation that is never stepped) and plans the same barge route, so it -## can run the same movement code as the server on the same ground. +## can run the same movement code as the server on the same ground. WORLD messages keep +## that world's built pieces, props, chests and tide in line with the server's, and OWN +## messages say what the player carries (`replica`, a ClientWorld). Pieces the player +## places appear at once and are taken back if the server turns them down. class_name ClientGame extends RefCounted @@ -24,6 +27,8 @@ var remote: SnapshotInterpolator = SnapshotInterpolator.new() ## The match's island, points of interest and props, generated here. Null until the ## first MATCH notice. var world: Simulation = null +## Keeps `world` in line with the server, and holds what the player carries. +var replica: ClientWorld = ClientWorld.new() var world_seed: int = 0 ## From the last MATCH notice; the phase is also kept up to date by snapshots. var match_phase: int = Match.LOBBY @@ -63,11 +68,27 @@ func poll(now_msec: int = -1) -> void: ## Takes one packet from the server. func receive(channel: int, bytes: PackedByteArray) -> void: if channel == NetProtocol.CHANNEL_CONTROL: - var notice: Dictionary = NetSnapshot.decode_match(bytes) - if notice.is_empty(): - bad_packets += 1 - return - _take_notice(notice) + var type: int = bytes[0] if not bytes.is_empty() else 0 + if type == NetWorld.MSG_WORLD: + var message: Dictionary = NetWorld.decode_world(bytes) + if message.is_empty() or world == null: + bad_packets += 1 + return + _take_world(message) + elif type == NetWorld.MSG_OWN: + var carried: Dictionary = NetWorld.decode_own(bytes) + if carried.is_empty(): + bad_packets += 1 + return + replica.own = carried + NetWorld.apply_own(prediction.player, carried) + prediction.player.materials = replica.available_materials() + else: + var notice: Dictionary = NetSnapshot.decode_match(bytes) + if notice.is_empty(): + bad_packets += 1 + return + _take_notice(notice) elif receiver.apply(bytes): _take_snapshot() @@ -85,6 +106,8 @@ func advance(delta: float) -> void: while prediction.next_tick <= target: var command: PlayerCommand = _input_for(prediction.next_tick) prediction.predict(command) + # A piece placed by this command goes up now, not when the server answers. + replica.try_place(prediction.player, command, command.tick) _recent.append(command) while _recent.size() > Tuning.NET_INPUT_REDUNDANCY: _recent.pop_front() @@ -113,14 +136,28 @@ func _take_notice(notice: Dictionary) -> void: var new_match: bool = int(notice["index"]) != match_index match_phase = notice["phase"] match_index = notice["index"] - if world == null or int(notice["seed"]) != world_seed: + if world == null or int(notice["seed"]) != world_seed or (new_match and replica.dirty): world_seed = notice["seed"] - world = make_world.call(world_seed) + _make_world() new_match = true if new_match: _reset_match() +func _make_world() -> void: + world = make_world.call(world_seed) + replica.begin(world) + + +func _take_world(message: Dictionary) -> void: + if message["full"] and replica.dirty: + # The whole state has to go on a world nothing was done to. + _make_world() + _reset_match() + replica.take(message) + prediction.player.materials = replica.available_materials() + + func _reset_match() -> void: prediction.stop() clock.stop() @@ -140,6 +177,9 @@ func _take_snapshot() -> void: remote.reset() else: remote.push(receiver.sim_tick, receiver.entities) + replica.sync(receiver.sim_tick) + if replica.settle(receiver.sim_tick, receiver.world_seq) > 0: + prediction.player.materials = replica.available_materials() if receiver.own.is_empty() or world == null: if prediction.active: prediction.stop() diff --git a/src/net/client_prediction.gd b/src/net/client_prediction.gd index c679ccb..3d665b4 100644 --- a/src/net/client_prediction.gd +++ b/src/net/client_prediction.gd @@ -9,8 +9,8 @@ ## correction. A correction isn't shown at once: the difference is carried as an offset ## on the drawn position and fades out. ## -## What isn't predicted: built pieces and broken props, which aren't replicated yet, -## and the flood beyond the cells around the player. Near those the server corrects. +## What isn't predicted: the flood beyond the cells around the player, and a piece or +## broken prop whose WORLD message hasn't arrived yet. Near those the server corrects. class_name ClientPrediction extends RefCounted diff --git a/src/net/client_world.gd b/src/net/client_world.gd new file mode 100644 index 0000000..2b03685 --- /dev/null +++ b/src/net/client_world.gd @@ -0,0 +1,214 @@ +## The client's side of WORLD and OWN messages (see NetWorld): keeps the client's copy +## of the match's world, a Simulation that is never stepped, in line with the server's, +## and places the client's own pieces before the server has answered. +## +## What is kept in line: built pieces (with their health and building), props' health, +## opened chests, unspilled crates and the tide's schedule. NetWorld.world_hash() of +## the client's world equals the server's once every message up to a tick has been +## applied and sync() has been called for that tick. +## +## Placing ahead of the server: when the player places a piece and the client's own +## check passes, a provisional piece goes into the world at once, under an ID the +## server never uses, and its cost is held back from the materials shown. The server's +## piece arriving in the same slot replaces it. If a snapshot from after the placing +## tick has arrived, the world messages it speaks of have all arrived too, and no +## piece came, the server turned it down and the provisional piece is taken away. +class_name ClientWorld +extends RefCounted + +## Provisional pieces get IDs from here up. +const PROVISIONAL_FIRST_ID: int = 1 << 30 + +var world: Simulation = null +## The newest WORLD applied: its number (-1 before the first full one) and tick. +var seq: int = -1 +var tick: int = 0 +## True once anything has been applied that a new match would have to undo. +var dirty: bool = false +## The last OWN (see NetWorld.decode_own), or empty. +var own: Dictionary = {} +## Counts, for tests and the network graph. +var applied: int = 0 +var gaps: int = 0 +var confirmed: int = 0 +var rolled_back: int = 0 + +## Piece ID -> damage taken, for every piece the server has sent. +var _damage: Dictionary = {} +## Piece ID -> the simulation tick its building began, worked back from how far it had +## got when the server sent it. +var _build_began: Dictionary = {} +## Provisional pieces: {"id", "slot", "kind", "material", "tick", "owner"}. +var _pending: Array[Dictionary] = [] +var _next_provisional: int = PROVISIONAL_FIRST_ID +var _build_ready_tick: int = 0 +var _loot_planned: bool = false + + +## Starts again on a freshly made world. +func begin(fresh: Simulation) -> void: + world = fresh + seq = -1 + tick = 0 + dirty = false + own = {} + _damage.clear() + _build_began.clear() + _pending.clear() + _build_ready_tick = 0 + _loot_planned = false + + +## Applies one decoded WORLD. A full one must be given a world nothing has been applied +## to: see `dirty`. +func take(message: Dictionary) -> void: + if world == null: + return + var structures: StructureSet = world.structures + if message["full"]: + _damage.clear() + _build_began.clear() + elif int(message["seq"]) != seq + 1: + # The channel is reliable and ordered, so this shouldn't happen. Carry on: the + # changes are still true. + gaps += 1 + seq = message["seq"] + tick = int(message["tick"]) if message["full"] else maxi(tick, int(message["tick"])) + applied += 1 + if message["loot"] and not _loot_planned: + _loot_planned = true + if world.island != null: + # The same rolls the server made, from the same stream and the same props. + world.loot = LootSet.plan(world.rng.stream("loot"), world.pois.loot_markers(), Simulation.crate_spill_points(world.props)) + dirty = true + var tide: Dictionary = message["tide"] + if not tide.is_empty(): + world.tide = Tide.new(tide["started"], tide["levels"]) + dirty = true + for entry: Array in message["pieces"]: + var piece_id: int = entry[NetWorld.P_ID] + dirty = true + if structures.find(piece_id) == null: + _replace_provisional(entry) + structures.add(entry[NetWorld.P_KIND], entry[NetWorld.P_MATERIAL], entry[NetWorld.P_SLOT], entry[NetWorld.P_FACING], entry[NetWorld.P_OWNER], entry[NetWorld.P_PLACED], world.ground, piece_id) + _damage[piece_id] = entry[NetWorld.P_DAMAGE] + _build_began[piece_id] = int(message["tick"]) - int(entry[NetWorld.P_ELAPSED]) + _set_progress(piece_id) + var damaged: PackedInt32Array = message["damaged"] + for index: int in range(0, damaged.size(), 2): + if structures.find(damaged[index]) != null: + _damage[damaged[index]] = damaged[index + 1] + _set_progress(damaged[index]) + for piece_id: int in message["removed"]: + if structures.remove(piece_id, world.ground): + _damage.erase(piece_id) + _build_began.erase(piece_id) + var props: PackedInt32Array = message["props"] + for index: int in range(0, props.size(), 2): + var prop: int = props[index] + if prop >= world.props.count(): + continue + dirty = true + if world.props.damage(prop, world.props.health[prop] - props[index + 1]): + # A broken crate has spilled what it held. + world.loot.crate_contents.erase(prop) + for chest: int in message["chests"]: + if chest < world.loot.chest_opened.size(): + world.loot.chest_opened[chest] = 1 + dirty = true + + +## Brings every piece still being built up to simulation tick `sim_tick`. +func sync(sim_tick: int) -> void: + if world == null: + return + tick = maxi(tick, sim_tick) + for piece_id: int in world.structures.building().duplicate(): + _set_progress(piece_id) + + +## The materials the player has to spend: what the server last said, less what the +## provisional pieces will cost. +func available_materials() -> PackedInt32Array: + var materials: PackedInt32Array = PackedInt32Array() + materials.resize(Materials.NAMES.size()) + if not own.is_empty(): + materials = (own["materials"] as PackedInt32Array).duplicate() + for pending: Dictionary in _pending: + var material: int = pending["material"] + materials[material] = maxi(materials[material] - StructureSet.cost_of(pending["kind"]), 0) + return materials + + +## Provisional pieces waiting for the server's answer. +func pending_count() -> int: + return _pending.size() + + +## The player's command for `command_tick` has just been predicted on `player`: if it +## places a piece and the checks the server will make pass here, puts a provisional +## piece in the world. Returns the Building.Result, or -1 if the command places nothing +## or the last piece was too recent. +func try_place(player: SimEntity, command: PlayerCommand, command_tick: int) -> int: + if world == null or command == null or command_tick < _build_ready_tick: + return -1 + if not command.is_pressed(PlayerCommand.BUTTON_BUILD) or not command.is_pressed(PlayerCommand.BUTTON_FIRE): + return -1 + player.materials = available_materials() + var at: Building.Target = Building.target(player, command.selection) + var result: Building.Result = Building.check(world, player, at, command.material) + if result == Building.Result.OK and Building.would_trap(player, Building.boxes_for_target(at, world.ground)): + result = Building.Result.BLOCKED_BY_PLAYER + if result != Building.Result.OK: + return result + var piece: StructureSet.Piece = world.structures.add(at.kind, command.material, at.slot, at.facing, player.id, command_tick, world.ground, _next_provisional) + _next_provisional += 1 + _pending.append({"id": piece.id, "slot": at.slot, "kind": at.kind, "material": command.material, "tick": command_tick, "owner": player.id}) + dirty = true + _build_ready_tick = command_tick + Tuning.BUILD_INTERVAL_TICKS + Building.lift(player, piece.boxes) + player.materials = available_materials() + return result + + +## Called with each snapshot: `sim_tick` is the snapshot's simulation tick and +## `world_seq` the newest WORLD the server had made by then. Takes away every +## provisional piece the server has had its chance to place and didn't. Returns how +## many went. +func settle(sim_tick: int, world_seq: int) -> int: + if _pending.is_empty() or seq < world_seq: + return 0 + var waiting: Array[Dictionary] = [] + var gone: int = 0 + for pending: Dictionary in _pending: + if int(pending["tick"]) >= sim_tick: + waiting.append(pending) + continue + world.structures.remove(pending["id"], world.ground) + gone += 1 + _pending = waiting + rolled_back += gone + return gone + + +## The server's piece `entry` is about to be added: a provisional piece in its slot +## makes way. +func _replace_provisional(entry: Array) -> void: + for index: int in _pending.size(): + var pending: Dictionary = _pending[index] + if pending["slot"] != entry[NetWorld.P_SLOT]: + continue + world.structures.remove(pending["id"], world.ground) + _pending.remove_at(index) + if pending["kind"] == entry[NetWorld.P_KIND] and pending["owner"] == entry[NetWorld.P_OWNER]: + confirmed += 1 + else: + rolled_back += 1 + return + + +func _set_progress(piece_id: int) -> void: + var piece: StructureSet.Piece = world.structures.find(piece_id) + if piece != null: + # A provisional piece hasn't begun, as far as the server has said. + world.structures.set_progress(piece_id, tick - int(_build_began.get(piece_id, tick)), int(_damage.get(piece_id, 0))) diff --git a/src/net/client_world.gd.uid b/src/net/client_world.gd.uid new file mode 100644 index 0000000..63336b4 --- /dev/null +++ b/src/net/client_world.gd.uid @@ -0,0 +1 @@ +uid://cyjsoswoc1opy diff --git a/src/net/net_protocol.gd b/src/net/net_protocol.gd index cd6f5bc..6bb28a5 100644 --- a/src/net/net_protocol.gd +++ b/src/net/net_protocol.gd @@ -16,7 +16,7 @@ class_name NetProtocol extends RefCounted ## Raised whenever the wire format changes in a way old clients can't read. -const VERSION: int = 2 +const VERSION: int = 3 const MAGIC: String = "TDWL" const CHANNEL_CONTROL: int = 0 ## Unreliable game traffic: commands up, snapshots down. diff --git a/src/net/net_snapshot.gd b/src/net/net_snapshot.gd index 67eaf75..5f3e9b2 100644 --- a/src/net/net_snapshot.gd +++ b/src/net/net_snapshot.gd @@ -9,6 +9,7 @@ ## server -> SNAPSHOT (channel 1, unreliable) ## u8 type, u32 server tick, u32 baseline tick (0 = none), u32 simulation tick, ## u32 newest command tick received from this client plus one (0 = none yet), +## u32 the sequence number of the newest WORLD message made (see NetWorld), ## varint the entity the client is watching (its own player, or the one it ## spectates; 0 = none), u8 match phase, u8 tide phase, s16 sea level in centimetres, ## u8 1 if the client's own block follows, then that block (see SELF_BYTES), @@ -31,7 +32,7 @@ const MSG_INPUT: int = 17 ## The server tick counts every tick the server has run and never goes back, so it ## orders snapshots across matches. The simulation tick is the one commands name. -const HEADER_BYTES: int = 17 +const HEADER_BYTES: int = 21 const MATCH_BYTES: int = 14 ## The client's own block: what it needs to predict its own player and nobody else is ## told. Exact, not quantized, so a prediction made from it matches the server's to the @@ -241,6 +242,7 @@ static func encode_snapshot(header: Dictionary, removed: PackedInt32Array, recor out.put_u32(int(header.get("baseline", 0)) & 0xFFFFFFFF) out.put_u32(int(header.get("sim_tick", 0)) & 0xFFFFFFFF) out.put_u32((int(header.get("command_tick", -1)) + 1) & 0xFFFFFFFF) + out.put_u32(int(header.get("world_seq", 0)) & 0xFFFFFFFF) _put_varint(out, int(header.get("own_id", 0))) out.put_u8(int(header.get("match_phase", 0))) out.put_u8(int(header.get("tide_phase", 0))) @@ -285,7 +287,7 @@ static func encode_snapshot(header: Dictionary, removed: PackedInt32Array, recor ## Reads a SNAPSHOT's header and lists, without applying them: {"tick", "baseline", -## "sim_tick", "command_tick" (-1 = none yet), "own_id", "match_phase", "tide_phase", +## "sim_tick", "command_tick" (-1 = none yet), "world_seq", "own_id", "match_phase", "tide_phase", ## "sea_cm", "self": the client's own block as self_of() made it, or empty, "removed": ## PackedInt32Array, "records": [[entity ID, archetype, mask, values]]}, where `values` ## holds the fields present, in record order. Empty if malformed. @@ -299,7 +301,7 @@ static func decode_snapshot(bytes: PackedByteArray) -> Dictionary: var at: int = cursor[0] var result: Dictionary = { "tick": bytes.decode_u32(1), "baseline": bytes.decode_u32(5), "sim_tick": bytes.decode_u32(9), - "command_tick": bytes.decode_u32(13) - 1, + "command_tick": bytes.decode_u32(13) - 1, "world_seq": bytes.decode_u32(17), "own_id": own_id, "match_phase": bytes[at], "tide_phase": bytes[at + 1], "sea_cm": bytes.decode_s16(at + 2), } var own: Dictionary = {} diff --git a/src/net/net_world.gd b/src/net/net_world.gd new file mode 100644 index 0000000..121df1d --- /dev/null +++ b/src/net/net_world.gd @@ -0,0 +1,310 @@ +## The parts of a match that aren't entities, on the wire: built pieces, damaged and +## broken props, opened chests and the tide's schedule (WORLD), and what a client's own +## player carries (OWN). Both go on channel 0, reliably and in order, because a wall +## the client never heard about is a lie it would keep walking into. +## +## server -> WORLD +## u8 type, u8 flags (FLAG_ bits), u32 sequence number, u32 simulation tick, +## if FLAG_TIDE: u32 the tick the tide started, u8 level count, then f32 levels, +## varint count, then new pieces: varint ID, u8 (kind | material << 3 | facing << 5), +## s16 slot x, s8 slot level, s16 slot z, u8 slot type, varint owner entity, +## u32 the tick it was placed, varint ticks of building done, varint damage taken, +## varint count, then damaged pieces: varint ID, varint damage taken so far, +## varint count, then removed pieces: varint ID, +## varint count, then props: varint prop, varint health left, +## varint count, then opened chests: varint index +## server -> OWN +## u8 type, u32 simulation tick, u8 held selection, +## u8 count, then u16 of each material, +## u8 count, then slots: u8 item code plus one, u16 count, u16 rounds, u8 tier, +## u8 count, then u16 of each ammunition +## +## A WORLD with FLAG_FULL is the whole state: the client forgets what it had first. The +## others are changes, numbered one after another from the last full one. A piece's +## health isn't sent: it follows from how far its building had got at the message's +## tick, the ticks since, and the damage it has taken (StructureSet.set_progress), so a +## piece being built costs nothing to keep up to date. +## +## Decoders return an empty Dictionary for anything malformed and never raise an error. +class_name NetWorld +extends RefCounted + +const MSG_WORLD: int = 5 +const MSG_OWN: int = 6 + +## The whole state, not changes: forget everything first. +const FLAG_FULL: int = 1 << 0 +## The match's loot has been rolled and placed, so its chests exist. +const FLAG_LOOT: int = 1 << 1 +## The tide's schedule follows. +const FLAG_TIDE: int = 1 << 2 + +const WORLD_HEADER_BYTES: int = 10 +## Indices into a decoded piece. +const P_ID: int = 0 +const P_KIND: int = 1 +const P_MATERIAL: int = 2 +const P_FACING: int = 3 +const P_SLOT: int = 4 +const P_OWNER: int = 5 +const P_PLACED: int = 6 +const P_DAMAGE: int = 7 +## Ticks of building done when the message was made. +const P_ELAPSED: int = 8 + + +## A piece as it travels: see the P_ indices. +static func piece_entry(piece: StructureSet.Piece) -> Array: + return [ + piece.id, piece.kind, piece.material, piece.facing, piece.slot, piece.owner, piece.placed_tick, + StructureSet.damage_taken(piece), piece.build_ticks - piece.build_ticks_left, + ] + + +## Builds a WORLD. `message` has "flags", "seq", "tick", and optionally "tide" +## ({"started", "levels"}), "pieces" (piece_entry() arrays), "damaged" and "props" +## (PackedInt32Arrays of pairs), "removed" and "chests" (PackedInt32Arrays). +static func encode_world(message: Dictionary) -> PackedByteArray: + var out: StreamPeerBuffer = StreamPeerBuffer.new() + out.big_endian = false + var tide: Dictionary = message.get("tide", {}) + var flags: int = int(message.get("flags", 0)) & ~FLAG_TIDE + if not tide.is_empty(): + flags |= FLAG_TIDE + out.put_u8(MSG_WORLD) + out.put_u8(flags) + out.put_u32(int(message.get("seq", 0)) & 0xFFFFFFFF) + out.put_u32(int(message.get("tick", 0)) & 0xFFFFFFFF) + if not tide.is_empty(): + var levels: PackedFloat32Array = tide["levels"] + out.put_u32(int(tide["started"]) & 0xFFFFFFFF) + out.put_u8(levels.size()) + for level: float in levels: + out.put_float(level) + var pieces: Array = message.get("pieces", []) + _put_varint(out, pieces.size()) + for entry: Array in pieces: + var slot: Vector4i = entry[P_SLOT] + _put_varint(out, int(entry[P_ID])) + out.put_u8(int(entry[P_KIND]) | int(entry[P_MATERIAL]) << 3 | int(entry[P_FACING]) << 5) + out.put_16(slot.x) + out.put_8(slot.y) + out.put_16(slot.z) + out.put_u8(slot.w) + _put_varint(out, int(entry[P_OWNER])) + out.put_u32(int(entry[P_PLACED]) & 0xFFFFFFFF) + _put_varint(out, maxi(int(entry[P_ELAPSED]), 0)) + _put_varint(out, maxi(int(entry[P_DAMAGE]), 0)) + for key: String in ["damaged", "removed", "props", "chests"]: + var values: PackedInt32Array = message.get(key, PackedInt32Array()) + var width: int = 2 if key == "damaged" or key == "props" else 1 + _put_varint(out, values.size() / width) + for value: int in values: + _put_varint(out, maxi(value, 0)) + return out.data_array + + +## {"full": bool, "loot": bool, "seq", "tick", "tide": {} or {"started", "levels"}, +## "pieces", "damaged", "removed", "props", "chests"} as encode_world() takes them, or +## empty if malformed. +static func decode_world(bytes: PackedByteArray) -> Dictionary: + if bytes.size() < WORLD_HEADER_BYTES + 5 or bytes[0] != MSG_WORLD or bytes[1] > (FLAG_FULL | FLAG_LOOT | FLAG_TIDE): + return {} + var flags: int = bytes[1] + var result: Dictionary = { + "full": flags & FLAG_FULL != 0, "loot": flags & FLAG_LOOT != 0, + "seq": bytes.decode_u32(2), "tick": bytes.decode_u32(6), "tide": {}, + } + var cursor: Array[int] = [WORLD_HEADER_BYTES] + if flags & FLAG_TIDE != 0: + if bytes.size() < cursor[0] + 5: + return {} + var count: int = bytes[cursor[0] + 4] + if count != Tide.phase_count() or bytes.size() < cursor[0] + 5 + count * 4: + return {} + var levels: PackedFloat32Array = PackedFloat32Array() + for index: int in count: + var level: float = bytes.decode_float(cursor[0] + 5 + index * 4) + if not is_finite(level): + return {} + levels.append(level) + result["tide"] = {"started": bytes.decode_u32(cursor[0]), "levels": levels} + cursor[0] += 5 + count * 4 + var piece_count: int = _read_varint(bytes, cursor) + if piece_count < 0 or piece_count > bytes.size(): + return {} + var pieces: Array = [] + for _index: int in piece_count: + var piece_id: int = _read_varint(bytes, cursor) + var at: int = cursor[0] + if piece_id <= 0 or at + 7 > bytes.size(): + return {} + var packed: int = bytes[at] + var kind: int = packed & 7 + var material: int = (packed >> 3) & 3 + var slot: Vector4i = Vector4i(bytes.decode_s16(at + 1), bytes.decode_s8(at + 3), bytes.decode_s16(at + 4), bytes[at + 6]) + if packed > 0x7F or kind >= StructureSet.KINDS.size() or not Materials.is_valid(material): + return {} + var slot_fits: bool = slot.w == StructureSet.slot_type_of(kind) + if StructureSet.is_wall_kind(kind): + slot_fits = slot.w == StructureSet.SLOT_WALL_X or slot.w == StructureSet.SLOT_WALL_Z + if not slot_fits: + return {} + cursor[0] = at + 7 + var owner: int = _read_varint(bytes, cursor) + if owner < 0 or cursor[0] + 4 > bytes.size(): + return {} + var placed: int = bytes.decode_u32(cursor[0]) + cursor[0] += 4 + var elapsed: int = _read_varint(bytes, cursor) + var damage: int = _read_varint(bytes, cursor) + if elapsed < 0 or damage < 0: + return {} + pieces.append([piece_id, kind, material, packed >> 5, slot, owner, placed, damage, elapsed]) + result["pieces"] = pieces + for key: String in ["damaged", "removed", "props", "chests"]: + var width: int = 2 if key == "damaged" or key == "props" else 1 + var count: int = _read_varint(bytes, cursor) + if count < 0 or count * width > bytes.size(): + return {} + var values: PackedInt32Array = PackedInt32Array() + for _index: int in count * width: + var value: int = _read_varint(bytes, cursor) + if value < 0 or value > 0x7FFFFFFF: + return {} + values.append(value) + result[key] = values + if cursor[0] != bytes.size(): + return {} + return result + + +## What a player carries, as OWN sends it. +static func encode_own(player: SimEntity, sim_tick: int) -> PackedByteArray: + var out: StreamPeerBuffer = StreamPeerBuffer.new() + out.big_endian = false + out.put_u8(MSG_OWN) + out.put_u32(sim_tick & 0xFFFFFFFF) + out.put_u8(clampi(player.selected, 0, 255)) + out.put_u8(player.materials.size()) + for amount: int in player.materials: + out.put_u16(clampi(amount, 0, 65535)) + out.put_u8(player.slot_items.size()) + for slot: int in player.slot_items.size(): + out.put_u8(clampi(player.slot_items[slot] + 1, 0, 255)) + out.put_u16(clampi(player.slot_counts[slot], 0, 65535)) + out.put_u16(clampi(player.slot_rounds[slot], 0, 65535)) + out.put_u8(clampi(player.slot_tiers[slot], 0, 255)) + out.put_u8(player.ammo.size()) + for amount: int in player.ammo: + out.put_u16(clampi(amount, 0, 65535)) + return out.data_array + + +## {"tick", "selected", "materials", "slot_items", "slot_counts", "slot_rounds", +## "slot_tiers", "ammo"}, the lists as PackedInt32Arrays, or empty if malformed or not +## the shape this build's players have. +static func decode_own(bytes: PackedByteArray) -> Dictionary: + var materials: int = Materials.NAMES.size() + var slots: int = Inventory.slot_count() + var ammo: int = Tuning.AMMO.size() + if bytes.size() != 9 + materials * 2 + slots * 6 + ammo * 2 or bytes[0] != MSG_OWN: + return {} + if bytes[6] != materials or bytes[7 + materials * 2] != slots or bytes[8 + materials * 2 + slots * 6] != ammo: + return {} + var result: Dictionary = { + "tick": bytes.decode_u32(1), "selected": bytes[5], + "materials": PackedInt32Array(), "slot_items": PackedInt32Array(), "slot_counts": PackedInt32Array(), + "slot_rounds": PackedInt32Array(), "slot_tiers": PackedInt32Array(), "ammo": PackedInt32Array(), + } + var at: int = 7 + for _index: int in materials: + result["materials"].append(bytes.decode_u16(at)) + at += 2 + at += 1 + for _index: int in slots: + var item: int = bytes[at] - 1 + if item >= ItemCatalog.count(): + return {} + result["slot_items"].append(item) + result["slot_counts"].append(bytes.decode_u16(at + 1)) + result["slot_rounds"].append(bytes.decode_u16(at + 3)) + result["slot_tiers"].append(bytes[at + 5]) + at += 6 + at += 1 + for _index: int in ammo: + result["ammo"].append(bytes.decode_u16(at)) + at += 2 + if int(result["selected"]) > slots: + return {} + return result + + +## Puts a decoded OWN into `player`. +static func apply_own(player: SimEntity, own: Dictionary) -> void: + player.selected = own["selected"] + player.materials = (own["materials"] as PackedInt32Array).duplicate() + player.slot_items = (own["slot_items"] as PackedInt32Array).duplicate() + player.slot_counts = (own["slot_counts"] as PackedInt32Array).duplicate() + player.slot_rounds = (own["slot_rounds"] as PackedInt32Array).duplicate() + player.slot_tiers = (own["slot_tiers"] as PackedInt32Array).duplicate() + player.ammo = (own["ammo"] as PackedInt32Array).duplicate() + + +## SHA-256 hex digest of everything WORLD replicates, as `sim` holds it: the tide's +## schedule, every prop's health, every built piece with its health and building, the +## opened chests and the crates not yet spilled. A client that has applied every WORLD +## up to some tick, and brought its pieces' building up to that tick, agrees with the +## server's simulation at that tick. +static func world_hash(sim: Simulation) -> String: + var buffer: StreamPeerBuffer = StreamPeerBuffer.new() + buffer.big_endian = false + buffer.put_64(sim.tide.started_tick if sim.tide != null else -1) + if sim.tide != null: + buffer.put_data(sim.tide.levels.to_byte_array()) + buffer.put_64(sim.props.count()) + buffer.put_data(sim.props.health.to_byte_array()) + var piece_ids: Array = sim.structures.pieces.keys() + piece_ids.sort() + buffer.put_64(piece_ids.size()) + for piece_id: int in piece_ids: + var piece: StructureSet.Piece = sim.structures.pieces[piece_id] + buffer.put_64(piece.id) + buffer.put_u8(piece.kind) + buffer.put_u8(piece.material) + buffer.put_u8(piece.facing) + for value: int in [piece.slot.x, piece.slot.y, piece.slot.z, piece.slot.w]: + buffer.put_32(value) + buffer.put_64(piece.owner) + buffer.put_32(piece.health) + buffer.put_32(piece.build_ticks_left) + buffer.put_u8(1 if piece.grounded else 0) + buffer.put_64(piece.placed_tick) + sim.loot.write_state(buffer) + var hasher: HashingContext = HashingContext.new() + hasher.start(HashingContext.HASH_SHA256) + hasher.update(buffer.data_array) + return hasher.finish().hex_encode() + + +static func _put_varint(out: StreamPeerBuffer, value: int) -> void: + value &= 0xFFFFFFFF + while value >= 0x80: + out.put_u8(value & 0x7F | 0x80) + value >>= 7 + out.put_u8(value) + + +## Reads a varint of at most five bytes at cursor[0] and moves the cursor past it. +## Returns -1 if the buffer ends first or the number is too long. +static func _read_varint(bytes: PackedByteArray, cursor: Array[int]) -> int: + var value: int = 0 + for shift: int in range(0, 35, 7): + if cursor[0] >= bytes.size(): + return -1 + var byte: int = bytes[cursor[0]] + cursor[0] += 1 + value |= (byte & 0x7F) << shift + if byte & 0x80 == 0: + return value & 0xFFFFFFFF + return -1 diff --git a/src/net/net_world.gd.uid b/src/net/net_world.gd.uid new file mode 100644 index 0000000..581d5ad --- /dev/null +++ b/src/net/net_world.gd.uid @@ -0,0 +1 @@ +uid://ded1axgwbh60m diff --git a/src/net/snapshot_receiver.gd b/src/net/snapshot_receiver.gd index 3ad1c8c..b6f7d3e 100644 --- a/src/net/snapshot_receiver.gd +++ b/src/net/snapshot_receiver.gd @@ -21,6 +21,8 @@ var sim_tick: int = 0 var own_id: int = 0 ## The newest of this client's command ticks the server had received, or -1. var command_tick: int = -1 +## The newest WORLD message the server had made when it built that picture. +var world_seq: int = 0 ## This client's own block from that picture (see NetSnapshot.self_of), or empty when ## it isn't playing: in the lobby, eliminated or watching someone else. var own: Dictionary = {} @@ -88,6 +90,7 @@ func apply(bytes: PackedByteArray) -> bool: sim_tick = snapshot["sim_tick"] own_id = snapshot["own_id"] command_tick = snapshot["command_tick"] + world_seq = snapshot["world_seq"] own = snapshot["self"] match_phase = snapshot["match_phase"] tide_phase = snapshot["tide_phase"] diff --git a/src/net/snapshot_sender.gd b/src/net/snapshot_sender.gd index c9707d1..0d550fa 100644 --- a/src/net/snapshot_sender.gd +++ b/src/net/snapshot_sender.gd @@ -109,7 +109,9 @@ static func interest(sim: Simulation, viewer: SimEntity, among: Array[SimEntity] ## outside a match. `tick` is the server's own tick count, which must grow with every ## call for the same client. `own_id` is the client's own player, or 0: while that is ## who it watches and they are still in the match, the snapshot carries their own block. -func build(handle: int, sim: Simulation, viewer_id: int, tick: int, match_phase: int, own_id: int = 0) -> PackedByteArray: +## `world_seq` is the newest WORLD message made (see WorldSender), so the client can +## tell when it has heard everything this snapshot's world had in it. +func build(handle: int, sim: Simulation, viewer_id: int, tick: int, match_phase: int, own_id: int = 0, world_seq: int = 0) -> PackedByteArray: if not _clients.has(handle): return PackedByteArray() var client: Dictionary = _clients[handle] @@ -128,7 +130,7 @@ func build(handle: int, sim: Simulation, viewer_id: int, tick: int, match_phase: "tick": tick, "baseline": baseline_tick, "sim_tick": sim.tick, "command_tick": client["command_tick"], "own_id": viewer.id if viewer != null else 0, "match_phase": match_phase, "tide_phase": sim.tide.phase_at(sim.tick) if sim.tide != null else 0, - "sea_cm": roundi(sim.sea_level * 100.0), + "sea_cm": roundi(sim.sea_level * 100.0), "world_seq": world_seq, } var own_bytes: int = 0 if viewer != null and viewer.id == own_id and not viewer.eliminated: diff --git a/src/net/world_sender.gd b/src/net/world_sender.gd new file mode 100644 index 0000000..920b99b --- /dev/null +++ b/src/net/world_sender.gd @@ -0,0 +1,140 @@ +## The server's side of WORLD messages (see NetWorld): works out, after each tick, what +## changed in the parts of the match that aren't entities, and makes the whole state +## for someone who has just arrived. +## +## Everyone is sent the same changes, in order and reliably, so one message is made +## per tick and nothing is remembered per client. A tick in which nothing changed +## makes no message. Changes are numbered from the last begin(); every snapshot says +## the newest number, so a client knows when it has heard everything a snapshot's +## world had in it. +## +## The simulation keeps the lists this reads: StructureSet.changed and .removed, and +## PropSet.changed. This is the one reader that clears them. +class_name WorldSender +extends RefCounted + +## The newest change message's number: 0 until the first. +var seq: int = 0 +## Messages and bytes made so far, full ones not counted. +var messages: int = 0 +var bytes_made: int = 0 + +var _sim: Simulation = null +var _removed_seen: int = 0 +var _chests: PackedByteArray = PackedByteArray() +var _tide_sent: bool = false +var _loot_sent: bool = false +## Pieces with an ID below this have been sent as new. +var _next_new: int = 1 + + +## True if begin() was last called for `sim`. +func is_for(sim: Simulation) -> bool: + return _sim == sim + + +## Starts again for a new simulation. Whatever it already holds goes in full(), not +## in changes. +func begin(sim: Simulation) -> void: + _sim = sim + seq = 0 + sim.structures.changed.clear() + sim.props.changed.clear() + _removed_seen = sim.structures.removed.size() + _chests = sim.loot.chest_opened.duplicate() + _tide_sent = sim.tide != null + _loot_sent = _loot_placed(sim) + _next_new = sim.structures.next_id() + + +## The whole state as it stands, for a client that knows nothing. It carries the +## newest change's number, and the changes after it apply on top; one already included +## here does no harm applied twice. +func full() -> PackedByteArray: + var sim: Simulation = _sim + var message: Dictionary = { + "flags": NetWorld.FLAG_FULL | (NetWorld.FLAG_LOOT if _loot_sent or _loot_placed(sim) else 0), + "seq": seq, "tick": sim.tick, + } + if sim.tide != null: + message["tide"] = {"started": sim.tide.started_tick, "levels": sim.tide.levels} + var pieces: Array = [] + var piece_ids: Array = sim.structures.pieces.keys() + piece_ids.sort() + for piece_id: int in piece_ids: + pieces.append(NetWorld.piece_entry(sim.structures.pieces[piece_id])) + message["pieces"] = pieces + var props: PackedInt32Array = PackedInt32Array() + for prop: int in sim.props.count(): + if sim.props.health[prop] != int(Tuning.PROP_KINDS[sim.props.kind_name(prop)]["health"]): + props.append(prop) + props.append(sim.props.health[prop]) + message["props"] = props + var chests: PackedInt32Array = PackedInt32Array() + for index: int in sim.loot.chest_opened.size(): + if sim.loot.chest_opened[index] != 0: + chests.append(index) + message["chests"] = chests + return NetWorld.encode_world(message) + + +## What changed since the last call, as one WORLD message, or nothing if nothing did. +## Call after every tick. +func changes() -> PackedByteArray: + var sim: Simulation = _sim + var structures: StructureSet = sim.structures + var tide_new: bool = sim.tide != null and not _tide_sent + var loot_now: bool = _loot_sent or _loot_placed(sim) + var chests_changed: bool = sim.loot.chest_opened != _chests + if structures.changed.is_empty() and sim.props.changed.is_empty() and structures.removed.size() == _removed_seen and not tide_new and loot_now == _loot_sent and not chests_changed: + return PackedByteArray() + var message: Dictionary = {"flags": NetWorld.FLAG_LOOT if loot_now else 0, "tick": sim.tick} + if tide_new: + message["tide"] = {"started": sim.tide.started_tick, "levels": sim.tide.levels} + var pieces: Array = [] + var damaged: PackedInt32Array = PackedInt32Array() + var piece_ids: Array = structures.changed.keys() + piece_ids.sort() + for piece_id: int in piece_ids: + var piece: StructureSet.Piece = structures.find(piece_id) + if piece == null: + continue + if piece_id >= _next_new: + pieces.append(NetWorld.piece_entry(piece)) + else: + damaged.append(piece_id) + damaged.append(StructureSet.damage_taken(piece)) + message["pieces"] = pieces + message["damaged"] = damaged + message["removed"] = structures.removed.slice(_removed_seen) + var props: PackedInt32Array = PackedInt32Array() + var prop_ids: Array = sim.props.changed.keys() + prop_ids.sort() + for prop: int in prop_ids: + props.append(prop) + props.append(sim.props.health[prop]) + message["props"] = props + var chests: PackedInt32Array = PackedInt32Array() + if chests_changed: + for index: int in sim.loot.chest_opened.size(): + if sim.loot.chest_opened[index] != 0 and (index >= _chests.size() or _chests[index] == 0): + chests.append(index) + message["chests"] = chests + + structures.changed.clear() + sim.props.changed.clear() + _removed_seen = structures.removed.size() + _chests = sim.loot.chest_opened.duplicate() + _tide_sent = sim.tide != null + _loot_sent = loot_now + _next_new = structures.next_id() + seq += 1 + message["seq"] = seq + var bytes: PackedByteArray = NetWorld.encode_world(message) + messages += 1 + bytes_made += bytes.size() + return bytes + + +static func _loot_placed(sim: Simulation) -> bool: + return sim.loot.floor_spots.size() + sim.loot.chest_positions.size() + sim.loot.crate_contents.size() > 0 diff --git a/src/net/world_sender.gd.uid b/src/net/world_sender.gd.uid new file mode 100644 index 0000000..a641306 --- /dev/null +++ b/src/net/world_sender.gd.uid @@ -0,0 +1 @@ +uid://d27rnyrb1srcn diff --git a/src/server/net_game.gd b/src/server/net_game.gd index e1de101..d5c0f13 100644 --- a/src/server/net_game.gd +++ b/src/server/net_game.gd @@ -1,6 +1,8 @@ ## 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, 15 times a second. +## snapshots of what they may see, 15 times a second. Built pieces, props, chests and +## the tide's schedule go to everyone reliably as they change (WorldSender), and each +## player is told what they carry whenever it changes. ## ## Call poll() once a frame before running ticks, and after_tick() after each tick. class_name NetGame @@ -9,14 +11,22 @@ extends RefCounted var loop: ServerLoop var net: NetServer var sender: SnapshotSender = SnapshotSender.new() +var world: WorldSender = WorldSender.new() ## What happened during the last poll(): {"type": "joined" or "left", "handle", "name", ## "reason"}, for the server's log. var events: Array[Dictionary] = [] ## Input packets that couldn't be read, so far. var bad_inputs: int = 0 +## The WORLD message the last after_tick() sent everyone, or empty, and the OWN +## messages it sent, by handle. For owners that carry the packets themselves. +var world_changes: PackedByteArray = PackedByteArray() +var own_changes: Dictionary = {} + ## Handle -> the newest command tick received from them this match. var _last_command: Dictionary = {} +## Handle -> what they were last told they carry, as _own_notice() lists it. +var _own_sent: Dictionary = {} func _init(server_loop: ServerLoop, server_net: NetServer) -> void: @@ -24,6 +34,7 @@ func _init(server_loop: ServerLoop, server_net: NetServer) -> void: net = server_net net.max_players = loop.match_flow.rules.max_players net.match_seed = loop.match_flow.current_seed() + world.begin(loop.sim) ## Services the network: joins and leaves, then every input packet that arrived. @@ -37,6 +48,7 @@ func poll() -> void: if loop.match_flow.join(handle): adopt(handle) _send_match(handle) + net.send(handle, NetProtocol.CHANNEL_CONTROL, world_notice(), true) events.append(event) else: net.kick(handle, NetProtocol.REASON_FULL) @@ -44,6 +56,7 @@ func poll() -> void: loop.match_flow.leave(handle) sender.remove_client(handle) _last_command.erase(handle) + _own_sent.erase(handle) events.append(event) for packet: Dictionary in net.packets: take_input(packet["handle"], packet["bytes"]) @@ -78,10 +91,13 @@ func take_input(handle: int, bytes: PackedByteArray) -> void: loop.submit(handle, command) -## Tells everyone about a change of phase, and sends snapshots when one is due. +## Tells everyone about a change of phase and whatever changed in the world, and sends +## snapshots when one is due. func after_tick() -> void: var flow: Match = loop.match_flow var changed: bool = false + world_changes = PackedByteArray() + own_changes.clear() for event: Dictionary in flow.events: if event["type"] == "phase": changed = true @@ -94,6 +110,24 @@ func after_tick() -> void: if changed: for handle: int in net.handles(): _send_match(handle) + # The world goes before the snapshots, which say how much of it there is to know. + if not world.is_for(loop.sim): + # A new match's simulation: everyone starts again from the whole of it. + world.begin(loop.sim) + _own_sent.clear() + world_changes = world.full() + else: + world_changes = world.changes() + if not world_changes.is_empty(): + for handle: int in net.handles(): + if sender.has_client(handle): + net.send(handle, NetProtocol.CHANNEL_CONTROL, world_changes, true) + if flow.in_match(): + for handle: int in _last_command: + var bytes: PackedByteArray = _own_notice(handle) + if not bytes.is_empty(): + own_changes[handle] = bytes + net.send(handle, NetProtocol.CHANNEL_CONTROL, bytes, true) if not snapshot_due(): return for handle: int in net.handles(): @@ -111,7 +145,13 @@ func snapshot_due() -> bool: 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) + return sender.build(handle, loop.sim, viewer_of(handle), loop.ticks, flow.phase, own_id, world.seq) + + +## The whole world as it stands, as one WORLD message: what someone who has just +## joined is sent after the MATCH notice. +func world_notice() -> PackedByteArray: + return world.full() ## The MATCH notice as things stand: the phase, and the match's index and seed. @@ -134,5 +174,20 @@ func viewer_of(handle: int) -> int: return int(flow.entity_of[handle]) +## An OWN message for a participant if what they carry has changed since they were +## last told, or nothing. +func _own_notice(handle: int) -> PackedByteArray: + var flow: Match = loop.match_flow + var player: SimEntity = loop.sim.entities.find(int(flow.entity_of.get(handle, 0))) + if player == null or player.eliminated: + return PackedByteArray() + var carried: Array = [player.selected, player.materials, player.slot_items, player.slot_counts, player.slot_rounds, player.slot_tiers, player.ammo] + if _own_sent.get(handle, []) == carried: + return PackedByteArray() + # Copies: the player's own arrays go on changing. + _own_sent[handle] = carried.duplicate(true) + return NetWorld.encode_own(player, loop.sim.tick) + + func _send_match(handle: int) -> void: net.send(handle, NetProtocol.CHANNEL_CONTROL, match_notice(), true) diff --git a/src/sim/building.gd b/src/sim/building.gd index bfc428a..a66ae30 100644 --- a/src/sim/building.gd +++ b/src/sim/building.gd @@ -165,25 +165,36 @@ static func _traps_a_player(sim: Simulation, boxes: Array[AABB]) -> bool: for entity: SimEntity in sim.entities.entities(): if entity.archetype != SimEntity.ARCHETYPE_PLAYER or entity.health <= 0: continue - var feet: Vector3 = entity.position - var height: float = Movement.body_height(entity) - for box: AABB in boxes: - if not _body_overlaps(box, feet, height): - continue - if box.end.y - feet.y > Tuning.STEP_HEIGHT: - return true + if would_trap(entity, boxes): + return true + return false + + +## True if `player`'s body overlaps the boxes by more than a step. +static func would_trap(player: SimEntity, boxes: Array[AABB]) -> bool: + var feet: Vector3 = player.position + var height: float = Movement.body_height(player) + for box: AABB in boxes: + if not _body_overlaps(box, feet, height): + continue + if box.end.y - feet.y > Tuning.STEP_HEIGHT: + return true return false static func _lift_players(sim: Simulation, boxes: Array[AABB]) -> void: for entity: SimEntity in sim.entities.entities(): - if entity.archetype != SimEntity.ARCHETYPE_PLAYER: - continue - var feet: Vector3 = entity.position - var height: float = Movement.body_height(entity) - for box: AABB in boxes: - if _body_overlaps(box, feet, height) and box.end.y > entity.position.y: - entity.position.y = box.end.y + if entity.archetype == SimEntity.ARCHETYPE_PLAYER: + lift(entity, boxes) + + +## Lifts `player` onto any low step of the boxes it is standing in. +static func lift(player: SimEntity, boxes: Array[AABB]) -> void: + var feet: Vector3 = player.position + var height: float = Movement.body_height(player) + for box: AABB in boxes: + if _body_overlaps(box, feet, height) and box.end.y > player.position.y: + player.position.y = box.end.y static func _body_overlaps(box: AABB, feet: Vector3, height: float) -> bool: diff --git a/src/sim/prop_set.gd b/src/sim/prop_set.gd index 3282c32..eb94bd0 100644 --- a/src/sim/prop_set.gd +++ b/src/sim/prop_set.gd @@ -45,6 +45,9 @@ var health: PackedInt32Array = PackedInt32Array() var solid_ids: PackedInt32Array = PackedInt32Array() ## Prop IDs in the order they were destroyed, for views to catch up on. var destroyed: PackedInt32Array = PackedInt32Array() +## Props damaged since whoever sends them to clients last cleared this. A set: the +## values mean nothing. Not match state. +var changed: Dictionary = {} var _ground: SimGround ## SimGround solid ID -> prop ID, for props whose box is in `_ground`. @@ -105,6 +108,7 @@ func damage(prop: int, amount: int) -> bool: if prop < 0 or prop >= count() or not is_standing(prop) or amount <= 0: return false health[prop] = maxi(health[prop] - amount, 0) + changed[prop] = true if health[prop] > 0: return false destroyed.append(prop) diff --git a/src/sim/structure_set.gd b/src/sim/structure_set.gd index 9bcd686..5634cca 100644 --- a/src/sim/structure_set.gd +++ b/src/sim/structure_set.gd @@ -79,6 +79,9 @@ var pieces: Dictionary = {} var neighbours: Dictionary = {} ## Removed piece IDs, in the order they went, for views to catch up on. var removed: PackedInt32Array = PackedInt32Array() +## IDs of pieces placed or damaged since whoever sends them to clients last cleared +## this. A set: the values mean nothing. Not match state. +var changed: Dictionary = {} ## Props, whose boxes don't count as ground. Set by Simulation. var props: PropSet = null @@ -256,10 +259,12 @@ func touching(boxes: Array[AABB], ground: SimGround) -> PackedInt32Array: ## Adds a piece, its solids and its neighbour links. The caller has validated it. -func add(kind: int, material: int, slot: Vector4i, facing: int, owner: int, tick: int, ground: SimGround) -> Piece: +## A network client, which is told each piece's ID by the server, passes it as +## `piece_id`; the server leaves it out and the next free ID is used. +func add(kind: int, material: int, slot: Vector4i, facing: int, owner: int, tick: int, ground: SimGround, piece_id: int = 0) -> Piece: var piece: Piece = Piece.new() - piece.id = _next_id - _next_id += 1 + piece.id = piece_id if piece_id > 0 else _next_id + _next_id = maxi(_next_id, piece.id + 1) piece.kind = kind piece.material = material piece.slot = slot @@ -283,6 +288,7 @@ func add(kind: int, material: int, slot: Vector4i, facing: int, owner: int, tick piece.solid_ids.append(solid_id) _by_solid[solid_id] = piece.id pieces[piece.id] = piece + changed[piece.id] = true _building.append(piece.id) _by_slot[slot] = piece.id neighbours[piece.id] = links @@ -311,6 +317,7 @@ func remove(piece_id: int, ground: SimGround) -> bool: _building.remove_at(building_at) _by_slot.erase(piece.slot) pieces.erase(piece_id) + changed.erase(piece_id) removed.append(piece_id) return true @@ -338,6 +345,34 @@ static func built_gain(piece: Piece, elapsed: int) -> int: return (piece.max_health - piece.start_health) * elapsed / piece.build_ticks +## The damage a piece has taken so far: what building has given it, less what it has. +static func damage_taken(piece: Piece) -> int: + return piece.start_health + built_gain(piece, piece.build_ticks - piece.build_ticks_left) - piece.health + + +## Puts a piece's building and health where another copy of the match says they are: +## `elapsed` ticks into building, with `damage` taken. For a network client, which is +## told these rather than stepping the pieces itself. +func set_progress(piece_id: int, elapsed: int, damage_so_far: int) -> void: + var piece: Piece = pieces.get(piece_id) + if piece == null: + return + elapsed = clampi(elapsed, 0, piece.build_ticks) + piece.build_ticks_left = piece.build_ticks - elapsed + piece.health = maxi(piece.start_health + built_gain(piece, elapsed) - damage_so_far, 0) + var at: int = _building.find(piece_id) + if piece.build_ticks_left > 0 and at < 0: + _building.append(piece_id) + _building.sort() + elif piece.build_ticks_left == 0 and at >= 0: + _building.remove_at(at) + + +## IDs of pieces still building, ascending. +func building() -> PackedInt32Array: + return _building + + ## Advances building by one tick: each unfinished piece gains its share of health. func advance_building() -> void: if _building.is_empty(): @@ -367,6 +402,7 @@ func damage(piece_id: int, amount: int, cause: String, ground: SimGround, events return false piece.health = maxi(piece.health - amount, 0) if piece.health > 0: + changed[piece_id] = true return false destroy(piece_id, cause, ground, events) return true diff --git a/test/net/test_net_world.gd b/test/net/test_net_world.gd new file mode 100644 index 0000000..dae5fdb --- /dev/null +++ b/test/net/test_net_world.gd @@ -0,0 +1,128 @@ +extends TestCase + +## Issue N5's wire formats: WORLD and OWN messages there and back, and what the +## decoders make of rubbish. + + +func _message() -> Dictionary: + return { + "flags": NetWorld.FLAG_LOOT, "seq": 70000, "tick": 123456, + "pieces": [ + [1, StructureSet.WALL, Materials.DRIFTWOOD, 3, Vector4i(120, -8, 7, StructureSet.SLOT_WALL_X), 4, 900, 0, 0], + [300, StructureSet.RAMP, Materials.SALVAGE_IRON, 2, Vector4i(255, 63, 0, StructureSet.SLOT_VOLUME), 200, 70000, 513, 150], + [20000, StructureSet.BREAKWATER, Materials.STONE, 0, Vector4i(-1, 0, 255, StructureSet.SLOT_WALL_Z), 1, 0, 7, 3], + ], + "damaged": PackedInt32Array([1, 40, 300, 1000]), + "removed": PackedInt32Array([2, 129, 16384]), + "props": PackedInt32Array([0, 0, 5000, 35]), + "chests": PackedInt32Array([0, 17]), + } + + +func _levels() -> PackedFloat32Array: + var levels: PackedFloat32Array = PackedFloat32Array() + for index: int in Tide.phase_count(): + levels.append(1.5 + index * 2.75) + return levels + + +func test_a_world_message_survives_the_wire() -> void: + var message: Dictionary = _message() + message["tide"] = {"started": 31, "levels": _levels()} + var decoded: Dictionary = NetWorld.decode_world(NetWorld.encode_world(message)) + assert_false(decoded.is_empty()) + assert_false(decoded["full"]) + assert_true(decoded["loot"]) + assert_eq(decoded["seq"], 70000) + assert_eq(decoded["tick"], 123456) + assert_eq(decoded["tide"]["started"], 31) + assert_eq(decoded["tide"]["levels"], _levels()) + assert_eq(decoded["pieces"], message["pieces"]) + for key: String in ["damaged", "removed", "props", "chests"]: + assert_eq(decoded[key], message[key], key) + + +func test_an_empty_full_message_is_fifteen_bytes() -> void: + var bytes: PackedByteArray = NetWorld.encode_world({"flags": NetWorld.FLAG_FULL, "seq": 0, "tick": 0}) + assert_eq(bytes.size(), NetWorld.WORLD_HEADER_BYTES + 5) + var decoded: Dictionary = NetWorld.decode_world(bytes) + assert_true(decoded["full"]) + assert_false(decoded["loot"]) + assert_true((decoded["tide"] as Dictionary).is_empty()) + assert_eq((decoded["pieces"] as Array).size(), 0) + + +func test_a_cut_off_or_padded_world_message_is_refused() -> void: + var message: Dictionary = _message() + message["tide"] = {"started": 31, "levels": _levels()} + var bytes: PackedByteArray = NetWorld.encode_world(message) + for size: int in bytes.size(): + assert_true(NetWorld.decode_world(bytes.slice(0, size)).is_empty(), "%d of %d bytes" % [size, bytes.size()]) + var padded: PackedByteArray = bytes.duplicate() + padded.append(0) + assert_true(NetWorld.decode_world(padded).is_empty()) + assert_true(NetWorld.decode_world(PackedByteArray()).is_empty()) + + +func test_pieces_that_cannot_exist_are_refused() -> void: + var good: Array = [9, StructureSet.FLOOR, Materials.STONE, 1, Vector4i(3, 2, 4, StructureSet.SLOT_FLOOR), 6, 50, 0, 0] + assert_false(NetWorld.decode_world(NetWorld.encode_world({"pieces": [good]})).is_empty()) + var cases: Dictionary = { + "an unknown kind": [NetWorld.P_KIND, 6], + "an unknown material": [NetWorld.P_MATERIAL, 3], + "a floor in a wall's slot": [NetWorld.P_SLOT, Vector4i(3, 2, 4, StructureSet.SLOT_WALL_X)], + "ID zero": [NetWorld.P_ID, 0], + } + for what: String in cases: + var bad: Array = good.duplicate() + bad[cases[what][0]] = cases[what][1] + assert_true(NetWorld.decode_world(NetWorld.encode_world({"pieces": [bad]})).is_empty(), what) + var wall: Array = [9, StructureSet.WALL, Materials.STONE, 1, Vector4i(3, 2, 4, StructureSet.SLOT_VOLUME), 6, 50, 0, 0] + assert_true(NetWorld.decode_world(NetWorld.encode_world({"pieces": [wall]})).is_empty(), "a wall in a ramp's slot") + + +func test_random_bytes_never_raise_an_error() -> void: + var rng: RandomNumberGenerator = RandomNumberGenerator.new() + rng.seed = 77 + var accepted: int = 0 + for _round: int in 3000: + var bytes: PackedByteArray = PackedByteArray() + bytes.resize(rng.randi_range(0, 80)) + for index: int in bytes.size(): + bytes[index] = rng.randi_range(0, 255) + if not bytes.is_empty(): + bytes[0] = NetWorld.MSG_WORLD if rng.randf() < 0.5 else NetWorld.MSG_OWN + if bytes.size() > 1 and rng.randf() < 0.7: + bytes[1] = rng.randi_range(0, 7) + if not NetWorld.decode_world(bytes).is_empty() or not NetWorld.decode_own(bytes).is_empty(): + accepted += 1 + assert_true(accepted < 3000) + + +func test_what_a_player_carries_survives_the_wire() -> void: + var sim: Simulation = Simulation.new(4, SimGround.flat()) + var player: SimEntity = sim.entities.find(sim.add_player()) + player.materials = PackedInt32Array([500, 0, 37]) + var weapon: int = ItemCatalog.code_of(LootSet.loot_weapons().keys()[0]) + Inventory.give(player, weapon, 1, 3, 2) + Inventory.give(player, Inventory.ammo_code(1), 250) + player.selected = 1 + var bytes: PackedByteArray = NetWorld.encode_own(player, 4321) + var own: Dictionary = NetWorld.decode_own(bytes) + assert_false(own.is_empty()) + assert_eq(own["tick"], 4321) + var copy: SimEntity = SimEntity.new(9, SimEntity.ARCHETYPE_PLAYER) + NetWorld.apply_own(copy, own) + assert_eq(copy.selected, 1) + assert_eq(copy.materials, player.materials) + assert_eq(copy.slot_items, player.slot_items) + assert_eq(copy.slot_counts, player.slot_counts) + assert_eq(copy.slot_rounds, player.slot_rounds) + assert_eq(copy.slot_tiers, player.slot_tiers) + assert_eq(copy.ammo, player.ammo) + assert_eq(copy.slot_items[1], Inventory.EMPTY, "an empty slot stays empty") + for size: int in bytes.size(): + assert_true(NetWorld.decode_own(bytes.slice(0, size)).is_empty()) + var unknown: PackedByteArray = bytes.duplicate() + unknown[8 + Materials.NAMES.size() * 2] = 255 + assert_true(NetWorld.decode_own(unknown).is_empty(), "an item this build doesn't have") diff --git a/test/net/test_net_world.gd.uid b/test/net/test_net_world.gd.uid new file mode 100644 index 0000000..607f91d --- /dev/null +++ b/test/net/test_net_world.gd.uid @@ -0,0 +1 @@ +uid://da5efv7pnka0p diff --git a/test/net/test_world_sync.gd b/test/net/test_world_sync.gd new file mode 100644 index 0000000..515e44b --- /dev/null +++ b/test/net/test_world_sync.gd @@ -0,0 +1,296 @@ +extends TestCase + +## Issue N5 without a network: a server Simulation with a WorldSender, and a client's +## never-stepped copy with a ClientWorld. Messages are carried by hand, so a test can +## hold one back. + +const W: int = StructureSet.WALL +const F: int = StructureSet.FLOOR +const BUILD: int = PlayerCommand.BUTTON_BUILD | PlayerCommand.BUTTON_FIRE + +var server: Simulation +var sender: WorldSender +var builder: SimEntity +var world: Simulation +var replica: ClientWorld +## The client's copy of its own player, as ClientPrediction keeps one. +var mine: SimEntity +## WORLD messages made and not yet delivered. +var in_flight: Array[PackedByteArray] = [] + + +func before_each() -> void: + server = Simulation.new(5, SimGround.flat()) + builder = server.entities.find(server.add_player()) + builder.position = Vector3(102.0, 0.0, 102.0) + builder.materials = PackedInt32Array([200, 200, 200]) + sender = WorldSender.new() + sender.begin(server) + world = Simulation.new(5, SimGround.flat()) + replica = ClientWorld.new() + replica.begin(world) + in_flight.clear() + replica.take(NetWorld.decode_world(sender.full())) + mine = SimEntity.new(builder.id, SimEntity.ARCHETYPE_PLAYER) + mine.health = builder.health + mine.on_ground = true + _tell_own() + + +func _tell_own() -> void: + replica.own = NetWorld.decode_own(NetWorld.encode_own(builder, server.tick)) + + +func _command(kind: int, yaw_degrees: float = 0.0, pressed: int = BUILD) -> PlayerCommand: + return PlayerCommand.from_input(server.tick, Vector2.ZERO, deg_to_rad(yaw_degrees), 0.0, pressed, kind, Materials.DRIFTWOOD) + + +## One server tick with `command` for the builder. The WORLD message it makes, if +## any, waits in `in_flight`. +func _tick(command: PlayerCommand = null) -> void: + if command != null: + assert_true(server.submit_command(builder.id, command)) + server.step() + var bytes: PackedByteArray = sender.changes() + if not bytes.is_empty(): + in_flight.append(bytes) + + +func _deliver() -> void: + for bytes: PackedByteArray in in_flight: + replica.take(NetWorld.decode_world(bytes)) + in_flight.clear() + + +## The client predicts `command` on its copy of the player, as ClientGame does. +func _predict(command: PlayerCommand) -> int: + mine.position = builder.position + mine.yaw = command.yaw + mine.pitch = command.pitch + return replica.try_place(mine, command, command.tick) + + +## Solids in a ground that haven't been removed. +func _solids(ground: SimGround) -> int: + var count: int = 0 + for box: AABB in ground.solids: + if box != SimGround.REMOVED: + count += 1 + return count + + +func _agrees(message: String = "") -> void: + replica.sync(server.tick) + assert_eq(NetWorld.world_hash(world), NetWorld.world_hash(server), message) + + +func test_nothing_changing_makes_no_message() -> void: + for _index: int in 40: + _tick() + assert_eq(in_flight.size(), 0) + assert_eq(sender.seq, 0) + _agrees() + + +func test_pieces_follow_the_server_while_they_build_and_break() -> void: + _tick(_command(W)) + assert_eq(server.structures.count(), 1) + assert_eq(in_flight.size(), 1) + _deliver() + assert_eq(world.structures.count(), 1) + var piece: StructureSet.Piece = world.structures.find(1) + assert_eq(piece.slot, server.structures.find(1).slot) + assert_eq(piece.owner, builder.id) + assert_eq(_solids(world.ground), 1, "the piece is solid on the client too") + _agrees("just placed") + # Building: health rises every tick and no message says so. + var build_ticks: int = server.structures.find(1).build_ticks + for _index: int in build_ticks / 2: + _tick() + _agrees("while building") + assert_eq(in_flight.size(), 0, "building costs nothing on the wire") + assert_true(piece.health > piece.start_health and piece.health < piece.max_health) + # Damage part way through building, then the rest of it. + server.damage_piece(1, 7) + _tick() + assert_eq(in_flight.size(), 1) + _deliver() + _agrees("damaged while building") + for _index: int in build_ticks: + _tick() + _agrees("built") + assert_eq(piece.health, piece.max_health - 7) + assert_eq(piece.build_ticks_left, 0) + # A second piece leaning on the first, then the first destroyed: both go. + builder.pitch = 0 + var floor_at: Building.Target = Building.Target.new() + floor_at.kind = F + floor_at.slot = Vector4i(piece.slot.x, 2, piece.slot.z, StructureSet.SLOT_FLOOR) + assert_eq(Building.place(server, builder, floor_at, Materials.STONE), Building.Result.OK) + _tick() + _deliver() + assert_eq(world.structures.count(), 2) + _agrees("two pieces") + server.damage_piece(1, 100000) + assert_eq(server.structures.count(), 0, "the floor collapsed with the wall") + _tick() + _deliver() + assert_eq(world.structures.count(), 0) + assert_eq(_solids(world.ground), 0) + _agrees("all gone") + assert_eq(replica.gaps, 0) + + +func test_a_piece_placed_and_destroyed_in_one_tick_never_appears() -> void: + _tick(_command(W)) + server.damage_piece(1, 100000) + in_flight.clear() + in_flight.append(sender.changes()) + _tick(_command(W, 90.0)) + _deliver() + assert_eq(world.structures.count(), server.structures.count()) + _agrees() + + +func test_someone_joining_late_is_sent_the_whole_state() -> void: + server.start_tide() + _tick(_command(W)) + for _index: int in 5: + _tick() + _tick(_command(F, 90.0)) + server.damage_piece(1, 9) + for _index: int in 5: + _tick() + _tick(_command(W, 180.0)) + # Changes made since the last message are in the full state, and come again. + var late_world: Simulation = Simulation.new(5, SimGround.flat()) + var late: ClientWorld = ClientWorld.new() + late.begin(late_world) + server.damage_piece(2, 4) + late.take(NetWorld.decode_world(sender.full())) + late.sync(server.tick) + assert_eq(NetWorld.world_hash(late_world), NetWorld.world_hash(server), "from the full state alone") + assert_eq(late_world.structures.count(), 3) + assert_not_null(late_world.tide) + assert_eq(late_world.tide.levels, server.tide.levels) + var heard: int = in_flight.size() + for _index: int in 3: + _tick() + _tick(_command(F, 270.0)) + assert_eq(server.structures.count(), 4) + assert_true(in_flight.size() >= heard + 2, "the damage, then the new floor") + for index: int in range(heard, in_flight.size()): + late.take(NetWorld.decode_world(in_flight[index])) + late.sync(server.tick) + assert_eq(NetWorld.world_hash(late_world), NetWorld.world_hash(server), "and the changes after it") + assert_eq(late.gaps, 0) + _deliver() + _agrees("the client that was there all along") + + +func test_a_placed_piece_appears_at_once_and_the_servers_replaces_it() -> void: + var command: PlayerCommand = _command(W) + assert_eq(_predict(command), Building.Result.OK) + assert_eq(world.structures.count(), 1, "up before the server has heard") + assert_eq(replica.pending_count(), 1) + var cost: int = StructureSet.cost_of(W) + assert_eq(replica.available_materials()[Materials.DRIFTWOOD], 200 - cost, "its cost is held back") + assert_eq(_predict(_command(W)), -1, "not again inside the build interval") + _tick(command) + # Snapshots from before the answer arrives change nothing. + assert_eq(replica.settle(server.tick, sender.seq), 0, "the world message is still on its way") + assert_eq(replica.pending_count(), 1) + _deliver() + _tell_own() + assert_eq(replica.pending_count(), 0) + assert_eq(replica.confirmed, 1) + assert_eq(replica.rolled_back, 0) + assert_eq(world.structures.count(), 1) + assert_not_null(world.structures.find(1), "under the server's ID") + assert_eq(_solids(world.ground), 1) + assert_eq(replica.available_materials()[Materials.DRIFTWOOD], 200 - cost) + assert_eq(replica.settle(server.tick, sender.seq), 0) + _agrees() + + +func test_a_piece_the_server_turns_down_is_taken_back() -> void: + # The client thinks it can pay; the server knows it can't. + builder.materials = PackedInt32Array([0, 0, 0]) + var before: String = NetWorld.world_hash(world) + var command: PlayerCommand = _command(W) + assert_eq(_predict(command), Building.Result.OK) + assert_eq(_solids(world.ground), 1) + assert_eq(replica.settle(server.tick, sender.seq), 0, "the server hasn't run that tick yet") + _tick(command) + assert_eq(server.structures.count(), 0) + assert_eq(replica.settle(server.tick, sender.seq), 1) + assert_eq(replica.rolled_back, 1) + assert_eq(replica.pending_count(), 0) + assert_eq(world.structures.count(), 0) + assert_eq(_solids(world.ground), 0, "nothing left to bump into") + assert_eq(NetWorld.world_hash(world), before) + assert_eq(replica.available_materials()[Materials.DRIFTWOOD], 200, "and nothing spent, as far as the client knows") + _agrees() + # The next one can go in the same slot. + builder.materials = PackedInt32Array([200, 0, 0]) + _tell_own() + for _index: int in Tuning.BUILD_INTERVAL_TICKS: + _tick() + var again: PlayerCommand = _command(W) + assert_eq(_predict(again), Building.Result.OK) + _tick(again) + _deliver() + assert_eq(replica.confirmed, 1) + _agrees() + + +func test_a_command_that_never_arrives_is_taken_back_too() -> void: + var command: PlayerCommand = _command(W) + assert_eq(_predict(command), Building.Result.OK) + _tick() + _tick() + assert_eq(replica.settle(server.tick, sender.seq), 1) + assert_eq(world.structures.count(), 0) + _agrees() + + +func test_losing_a_race_for_a_slot_leaves_the_winners_piece() -> void: + # Someone else's wall goes up in the slot a tick before, and the news is late. + var rival: SimEntity = server.entities.find(server.add_player()) + rival.position = builder.position + Vector3(0.0, 0.0, 3.0) + rival.materials = PackedInt32Array([200, 200, 200]) + var slot: Vector4i = Building.target(builder, W).slot + var at: Building.Target = Building.Target.new() + at.kind = W + at.slot = slot + assert_eq(Building.place(server, rival, at, Materials.STONE), Building.Result.OK) + _tick() + var command: PlayerCommand = _command(W) + assert_eq(_predict(command), Building.Result.OK, "the client doesn't know yet") + _tick(command) + assert_eq(server.structures.count(), 1, "the server refused the second wall") + _deliver() + assert_eq(replica.pending_count(), 0) + assert_eq(replica.confirmed, 0) + assert_eq(replica.rolled_back, 1) + assert_eq(world.structures.count(), 1) + assert_eq(world.structures.find(1).owner, rival.id) + assert_eq(world.structures.find(1).material, Materials.STONE) + assert_eq(replica.available_materials()[Materials.DRIFTWOOD], 200) + _agrees() + + +func test_the_client_refuses_what_the_server_would() -> void: + replica.own["materials"] = PackedInt32Array([0, 0, 0]) + assert_eq(_predict(_command(W)), Building.Result.UNAFFORDABLE) + _tell_own() + assert_eq(_predict(_command(W, 0.0, PlayerCommand.BUTTON_FIRE)), -1, "not in build mode") + assert_eq(_predict(_command(99)), Building.Result.BAD_REQUEST) + mine.swimming = true + assert_eq(_predict(_command(W)), Building.Result.NOT_ALLOWED) + mine.swimming = false + assert_eq(world.structures.count(), 0) + assert_eq(replica.pending_count(), 0) + assert_false(replica.dirty, "a world nothing was done to can be used again") + assert_eq(_predict(_command(W)), Building.Result.OK) + assert_true(replica.dirty) diff --git a/test/net/test_world_sync.gd.uid b/test/net/test_world_sync.gd.uid new file mode 100644 index 0000000..5c0c317 --- /dev/null +++ b/test/net/test_world_sync.gd.uid @@ -0,0 +1 @@ +uid://crtbrc8hqi2gj diff --git a/test/server/test_client_game.gd b/test/server/test_client_game.gd index 0565607..b142554 100644 --- a/test/server/test_client_game.gd +++ b/test/server/test_client_game.gd @@ -52,6 +52,17 @@ func test_a_client_joins_over_udp_generates_the_island_and_plays() -> void: if client.playing(): played += 1 OS.delay_msec(1) + # Let the last reliable messages arrive, then compare the two worlds. + for _wait: int in 20: + game.poll() + client.poll() + OS.delay_msec(1) + client.replica.sync(loop.sim.tick) + assert_true(client.replica.applied >= 2, "the whole state on joining, then the match starting: %d WORLD messages" % client.replica.applied) + assert_eq(client.replica.seq, game.world.seq) + assert_eq(NetWorld.world_hash(client.world), NetWorld.world_hash(loop.sim), "the world arrived on the reliable channel") + assert_not_null(client.world.tide) + assert_false(client.replica.own.is_empty(), "and what the player carries") link.leave() assert_eq(played, 150, "the client got into the match and played") var flow: Match = loop.match_flow diff --git a/test/server/test_world_replication.gd b/test/server/test_world_replication.gd new file mode 100644 index 0000000..cf3fa91 --- /dev/null +++ b/test/server/test_world_replication.gd @@ -0,0 +1,199 @@ +extends TestCase + +## Issue N5 end to end: a ClientGame plays a whole scripted match against a real server +## on a real island, and its copy of the world has to agree with the server's all the +## way through. The match is seed 2 with two people and 22 bots. One person is the +## scripted player, who lands, loots, harvests and builds. The other is the network +## client, who leaps over land, puts up two walls ahead of the server's answer, and +## then a floor the server turns down. + +const DT: float = 1.0 / Tuning.TICK_RATE_HZ +const CLIENT: int = 2 +const BUILD: int = PlayerCommand.BUTTON_BUILD | PlayerCommand.BUTTON_FIRE +## Ticks between the client's attempts to build. +const ATTEMPT_EVERY: int = 20 +const LIMIT_TICKS: int = 25 * 60 * Tuning.TICK_RATE_HZ + +var loop: ServerLoop +var game: NetGame +var client: ClientGame +var leap_tick: int = 0 +## Build attempts the client has made, and the tick of the last. +var attempts: int = 0 +var last_attempt: int = -1000 +var wanted_attempts: int = 0 + + +func _input(tick: int) -> PlayerCommand: + var me: SimEntity = client.prediction.player + var centre: Vector3 = Vector3(Tuning.WORLD_SIZE_METRES * 0.5, 0.0, Tuning.WORLD_SIZE_METRES * 0.5) + var yaw: float = atan2(centre.x - me.position.x, centre.z - me.position.z) + if me.drop_state == Drop.ABOARD: + return PlayerCommand.from_input(tick, Vector2.ZERO, yaw, 0.0, PlayerCommand.BUTTON_JUMP if tick >= leap_tick else 0) + if me.drop_state != Drop.NONE: + return PlayerCommand.from_input(tick, Vector2(0.0, 1.0), yaw, 0.0) + if attempts < wanted_attempts and tick - last_attempt >= ATTEMPT_EVERY and me.on_ground and Building.is_allowed(me): + last_attempt = tick + attempts += 1 + # Two walls on sides of the cell it stands in, then a floor beside it. + var kind: int = StructureSet.WALL if attempts <= 2 else StructureSet.FLOOR + return PlayerCommand.from_input(tick, Vector2.ZERO, mini(attempts, 2) * PI * 0.5, 0.0, BUILD, kind, Materials.DRIFTWOOD) + return PlayerCommand.from_input(tick, Vector2.ZERO, yaw, 0.0) + + +## One round: the client's packets go up, the server runs a tick, and everything it +## sends comes down, reliable messages first as they were sent. +func _round() -> void: + for bytes: PackedByteArray in client.outbox: + game.take_input(CLIENT, bytes) + 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 not game.world_changes.is_empty(): + client.receive(NetProtocol.CHANNEL_CONTROL, game.world_changes) + if game.own_changes.has(CLIENT): + client.receive(NetProtocol.CHANNEL_CONTROL, game.own_changes[CLIENT]) + if game.snapshot_due(): + client.receive(NetProtocol.CHANNEL_STATE, game.snapshot_for(CLIENT)) + client.advance(DT) + + +func _agrees(message: String) -> bool: + client.replica.sync(loop.sim.tick) + var same: bool = NetWorld.world_hash(client.world) == NetWorld.world_hash(loop.sim) + assert_true(same, "%s: the client's world differs at tick %d" % [message, loop.sim.tick]) + return same + + +func test_a_clients_world_agrees_with_the_servers_through_a_scripted_match() -> void: + loop = ServerLoop.new(2, 2, 22) + loop.play_humans_by_script() + var flow: Match = loop.match_flow + var sim: Simulation = loop.sim + var mine: SimEntity = sim.entities.find(flow.entity_of[CLIENT]) + # Seat 2 is the network client's, not a script's. + for index: int in range(loop.scripted.size() - 1, -1, -1): + if loop.scripted[index].player_id == mine.id: + loop.scripted.remove_at(index) + assert_eq(loop.scripted.size(), 1) + game = NetGame.new(loop, NetServer.new()) + game.adopt(CLIENT) + leap_tick = sim.drop.duration_ticks / 2 + for tick: int in range(30, sim.drop.duration_ticks): + var under: Vector3 = sim.drop.barge_position(tick) + if sim.ground.terrain_height(under.x, under.z) > Tuning.ISLAND_BEACH_TOP + 2.0: + leap_tick = tick + break + client = ClientGame.new() + client.sample_input = _input + client.receive(NetProtocol.CHANNEL_CONTROL, game.match_notice()) + client.receive(NetProtocol.CHANNEL_CONTROL, game.world_notice()) + assert_eq(client.world.props.layout_hash(), sim.props.layout_hash(), "the same props on both ends") + _agrees("at the start") + assert_eq(client.world.loot.chest_positions, sim.loot.chest_positions, "the client rolled the same chests") + assert_true(client.world.loot.chest_positions.size() > 0) + assert_eq(client.world.tide.levels, sim.tide.levels) + + var wall_cost: int = StructureSet.cost_of(StructureSet.WALL) + var landed: bool = false + var refused: bool = false + var differed: int = 0 + var carried_checks: int = 0 + var most_pending: int = 0 + var most_pieces: int = 0 + while flow.phase != Match.RESULTS and loop.ticks < LIMIT_TICKS and differed == 0: + _round() + most_pending = maxi(most_pending, client.replica.pending_count()) + most_pieces = maxi(most_pieces, sim.structures.count()) + if not landed and mine.drop_state == Drop.NONE and mine.on_ground and not mine.eliminated: + # On the ground: hand it driftwood for two walls and a floor, and let it + # try the walls. + landed = true + mine.materials[Materials.DRIFTWOOD] = wall_cost * 2 + StructureSet.cost_of(StructureSet.FLOOR) + wanted_attempts = 2 + elif landed and not refused and client.replica.confirmed == 2 and wanted_attempts == 2: + wanted_attempts = 3 + elif wanted_attempts == 3 and not refused and client.replica.pending_count() == 1: + # The floor is down on the client and its command is on the way. The + # server finds the driftwood gone before it runs that tick. + refused = true + mine.materials[Materials.DRIFTWOOD] = 0 + if loop.ticks % 150 == 0: + if not _agrees("during the match"): + differed += 1 + if client.playing() and client.replica.pending_count() == 0 and not client.replica.own.is_empty(): + carried_checks += 1 + assert_eq(client.replica.own["materials"], mine.materials, "materials at tick %d" % sim.tick) + assert_eq(client.replica.own["slot_items"], mine.slot_items) + assert_eq(client.replica.own["ammo"], mine.ammo) + assert_eq(flow.phase, Match.RESULTS, "the match ended, after %d ticks" % loop.ticks) + _agrees("after the match") + + assert_true(landed, "the client's player reached the ground") + assert_eq(client.replica.confirmed, 2, "two walls placed ahead of the server and kept") + assert_true(refused) + assert_eq(client.replica.rolled_back, 1, "the floor taken back") + assert_eq(client.replica.pending_count(), 0) + assert_true(most_pending >= 1) + assert_true(carried_checks > 0, "what the client carries was compared %d times" % carried_checks) + assert_eq(client.replica.gaps, 0) + assert_eq(client.bad_packets, 0) + assert_eq(game.bad_inputs, 0) + # The match had something of everything to replicate. + var destroyed_props: int = sim.props.destroyed.size() + var opened: int = sim.loot.chest_opened.count(1) + assert_true(most_pieces >= 3, "%d pieces stood at once" % most_pieces) + assert_true(sim.structures.removed.size() + sim.structures.count() >= 4, "the scripted player built too") + assert_true(destroyed_props > 0, "%d props destroyed" % destroyed_props) + assert_eq(client.world.props.destroyed.size(), destroyed_props) + assert_true(opened > 0, "%d chests opened" % opened) + assert_eq(client.world.loot.chest_opened, sim.loot.chest_opened) + assert_eq(client.world.loot.crate_contents.size(), sim.loot.crate_contents.size()) + print("N5 match: %d ticks, %d WORLD messages, %d bytes (%.0f B/s), %d pieces built, %d removed, %d props destroyed, %d chests opened, largest correction %.2f m" % [ + sim.tick, game.world.messages, game.world.bytes_made, game.world.bytes_made / (sim.tick * DT), + sim.structures.next_id() - 1, sim.structures.removed.size(), destroyed_props, opened, client.prediction.max_correction, + ]) + + +func test_a_new_match_or_a_whole_state_starts_on_a_clean_world() -> void: + var worlds: Array[Simulation] = [] + client = ClientGame.new() + client.make_world = func(match_seed: int) -> Simulation: + worlds.append(Simulation.new(match_seed, SimGround.flat())) + return worlds[worlds.size() - 1] + var piece: Array = [3, StructureSet.FLOOR, Materials.STONE, 0, Vector4i(5, 0, 5, StructureSet.SLOT_FLOOR), 1, 10, 0, 4] + var full: PackedByteArray = NetWorld.encode_world({"flags": NetWorld.FLAG_FULL, "seq": 4, "tick": 14, "pieces": [piece]}) + # A WORLD before any MATCH has no world to go on. + client.receive(NetProtocol.CHANNEL_CONTROL, full) + assert_eq(client.bad_packets, 1) + client.receive(NetProtocol.CHANNEL_CONTROL, NetSnapshot.encode_match(Match.PLAY, 0, 11)) + client.receive(NetProtocol.CHANNEL_CONTROL, full) + assert_eq(worlds.size(), 1) + assert_eq(client.world.structures.count(), 1) + assert_eq(client.replica.seq, 4) + assert_true(client.replica.dirty) + # Changes go on the same world. + client.receive(NetProtocol.CHANNEL_CONTROL, NetWorld.encode_world({"seq": 5, "tick": 20, "removed": PackedInt32Array([3])})) + assert_eq(worlds.size(), 1) + assert_eq(client.world.structures.count(), 0) + # The whole state again: a fresh world, not the old one patched. + client.receive(NetProtocol.CHANNEL_CONTROL, full) + assert_eq(worlds.size(), 2) + assert_eq(client.world, worlds[1]) + assert_eq(client.world.structures.count(), 1) + # The next match, on the same seed as it happens: a fresh world again. + client.receive(NetProtocol.CHANNEL_CONTROL, NetSnapshot.encode_match(Match.LOBBY, 1, 11)) + assert_eq(worlds.size(), 3) + assert_eq(client.world.structures.count(), 0) + assert_false(client.replica.dirty) + assert_eq(client.replica.seq, -1) + # A phase change within a match leaves the world alone. + client.receive(NetProtocol.CHANNEL_CONTROL, NetSnapshot.encode_match(Match.COUNTDOWN, 1, 11)) + assert_eq(worlds.size(), 3) + # Rubbish is counted and changes nothing. + client.receive(NetProtocol.CHANNEL_CONTROL, PackedByteArray([NetWorld.MSG_WORLD, 0, 1])) + client.receive(NetProtocol.CHANNEL_CONTROL, PackedByteArray([NetWorld.MSG_OWN, 0, 1])) + assert_eq(client.bad_packets, 3) diff --git a/test/server/test_world_replication.gd.uid b/test/server/test_world_replication.gd.uid new file mode 100644 index 0000000..d127ee0 --- /dev/null +++ b/test/server/test_world_replication.gd.uid @@ -0,0 +1 @@ +uid://dcc2ed5gasyev