Files
tidewall/test/server/test_world_replication.gd
T
claudeandClaude Opus 5.5 6b21e9d4f1 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>
2026-11-12 14:27:58 -05:00

200 lines
9.7 KiB
GDScript

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)