Files
tidewall/test/server/test_client_game.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

154 lines
6.1 KiB
GDScript

extends TestCase
## Issue N3: ClientGame against a real server. Over loopback UDP with a NetClient, and
## by hand, packet by packet.
const HOST: String = "127.0.0.1"
const DT: float = 1.0 / Tuning.TICK_RATE_HZ
var loop: ServerLoop
var net: NetServer
var game: NetGame
func _rules() -> MatchRules:
var rules: MatchRules = MatchRules.new()
rules.max_players = 3
rules.min_players = 1
rules.lobby_ticks = 6
rules.countdown_ticks = 6
rules.results_ticks = 6
return rules
func before_each() -> void:
loop = ServerLoop.new(31, 0, 2, _rules())
net = NetServer.new()
game = NetGame.new(loop, net)
func after_each() -> void:
net.close()
func test_a_client_joins_over_udp_generates_the_island_and_plays() -> void:
assert_eq(net.listen(0, HOST), OK)
var link: NetClient = NetClient.new()
var client: ClientGame = ClientGame.new()
client.attach(link)
client.sample_input = func(tick: int) -> PlayerCommand:
return PlayerCommand.from_input(tick, Vector2(0.0, 1.0), 2.0, 0.1)
assert_eq(link.connect_to(HOST, net.port(), "Mara"), OK)
var started: int = Time.get_ticks_msec()
var played: int = 0
# A server tick and a client frame of the same length each round, so both clocks
# agree however fast the test machine is.
while Time.get_ticks_msec() - started < 20000 and played < 150:
game.poll()
loop.run_tick()
game.after_tick()
client.poll()
client.advance(DT)
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
assert_true(flow.in_match())
assert_eq(client.world_seed, flow.current_seed())
assert_eq(client.world.island.heightmap_hash(), loop.sim.island.heightmap_hash(), "the same island on both ends")
assert_eq(client.match_phase, flow.phase)
var mine: SimEntity = loop.sim.entities.find(flow.entity_of[link.handle])
assert_eq(client.prediction.player.id, mine.id)
assert_eq(mine.yaw, PlayerCommand.from_input(0, Vector2.ZERO, 2.0, 0.0).yaw, "the server is applying this client's commands")
assert_eq(client.prediction.player.drop_state, mine.drop_state)
assert_true(client.prediction.reconciles > 50, "%d snapshots compared" % client.prediction.reconciles)
assert_true(client.prediction.max_correction < 2.0, "largest correction %.3f m" % client.prediction.max_correction)
assert_true(client.own_position().distance_to(mine.position) < 10.0, "drawn %.1f m from where the server has it" % client.own_position().distance_to(mine.position))
# The two bots ride the same barge.
assert_eq(flow.entity_of.size(), 3)
for handle: int in flow.entity_of:
assert_true(client.remote.has(flow.entity_of[handle]), "player %d is drawn" % handle)
assert_eq(client.bad_packets, 0)
assert_eq(game.bad_inputs, 0)
func test_in_the_lobby_it_only_says_which_snapshot_it_holds() -> void:
var worlds: Array[int] = []
var client: ClientGame = ClientGame.new()
client.make_world = func(match_seed: int) -> Simulation:
worlds.append(match_seed)
return Simulation.new(match_seed, SimGround.flat())
loop.match_flow.rules.min_players = 5
assert_true(loop.match_flow.join(7))
game.adopt(7)
client.receive(NetProtocol.CHANNEL_CONTROL, game.match_notice())
assert_eq(worlds, [loop.match_flow.current_seed()], "the island is made as soon as the seed is known")
for _round: int in 4:
loop.run_tick()
game.after_tick()
if game.snapshot_due():
client.receive(NetProtocol.CHANNEL_STATE, game.snapshot_for(7))
client.advance(DT)
assert_false(client.playing())
assert_eq(client.match_phase, Match.LOBBY)
assert_eq(client.outbox.size(), 2, "one answer per snapshot")
var answer: Dictionary = NetSnapshot.decode_input(client.outbox[1])
assert_eq(answer["ack"], client.receiver.tick)
assert_eq((answer["commands"] as Array).size(), 0)
# Junk is counted and changes nothing.
client.receive(NetProtocol.CHANNEL_CONTROL, PackedByteArray([NetSnapshot.MSG_MATCH, 1, 2]))
client.receive(NetProtocol.CHANNEL_STATE, PackedByteArray([NetSnapshot.MSG_SNAPSHOT, 9, 9]))
assert_eq(client.bad_packets, 1)
assert_eq(client.receiver.malformed, 1)
assert_eq(worlds.size(), 1)
func test_a_new_match_starts_everything_again() -> void:
var worlds: Array[int] = []
var client: ClientGame = ClientGame.new()
client.make_world = func(match_seed: int) -> Simulation:
worlds.append(match_seed)
return Simulation.new(match_seed, SimGround.flat())
assert_true(loop.match_flow.join(7))
game.adopt(7)
client.receive(NetProtocol.CHANNEL_CONTROL, game.match_notice())
var first_seed: int = loop.match_flow.current_seed()
var sent: int = 0
for _round: int in 60:
for bytes: PackedByteArray in client.outbox:
game.take_input(7, bytes)
sent += 1
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 game.snapshot_due():
client.receive(NetProtocol.CHANNEL_STATE, game.snapshot_for(7))
client.advance(DT)
assert_true(client.playing(), "phase %s" % loop.match_flow.phase_name())
assert_true(sent > 30)
assert_true(client.receiver.command_tick >= 0, "the server echoes the commands it gets")
assert_eq(worlds, [first_seed], "one world for the whole match")
# The server moves on to another match: a notice with the next index and seed.
client.receive(NetProtocol.CHANNEL_CONTROL, NetSnapshot.encode_match(Match.LOBBY, 1, first_seed + 1))
assert_false(client.playing())
assert_false(client.clock.running)
assert_false(client.remote.started)
assert_eq(worlds, [first_seed, first_seed + 1])
assert_eq(client.match_index, 1)