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,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)
|
||||
Reference in New Issue
Block a user