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 <noreply@anthropic.com>
This commit is contained in:
@@ -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")
|
||||
@@ -0,0 +1 @@
|
||||
uid://da5efv7pnka0p
|
||||
@@ -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)
|
||||
@@ -0,0 +1 @@
|
||||
uid://crtbrc8hqi2gj
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
@@ -0,0 +1 @@
|
||||
uid://dcc2ed5gasyev
|
||||
Reference in New Issue
Block a user