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 <noreply@anthropic.com>
129 lines
5.5 KiB
GDScript
129 lines
5.5 KiB
GDScript
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")
|