- Build ghost: ClientSession shows a BuildGhost in build mode while the client plays, asking Building.check() of its own world and predicted player with the mouse's newest look, so it agrees with the server. - Flooded basins: ClientMirror.follow_flood() spreads the client's flood as the server's tide tick does, from the tide and the pieces it has. WaterView passes a mask of flooded cells (Flood.pours says when it changed) to the sea shader, which leaves dry cells undrawn. - Shots, impacts and swings: a new FX message (NetFx) goes unreliably on the state channel next to each snapshot, with what happened within NET_FX_RADIUS_METRES of what the client watches or involving that player. ClientGame keeps them in `fx` for views and sound. They change no state, so a lost one is a missed flash; sending them reliably would add to the traffic a bad link can least afford (N8). The fuzzer covers the new parser. - Captures: client_build and client_build_refused show the ghost in its allowed and refused tints; client_basin rings shore ground with breakwaters and plays until the tide stands over it, and the server, the client's flood and the drawn sea all keep the basin dry. - Tests: NetFx round trips, limits and malformed input; a landed player's shots and impacts reach a client, each matching a server event; the client keeps a ringed basin dry through the first rise, floods it with no ring, catches up after a long frame; the water mask follows pours. The held item is still not drawn; that needs the models (A4, #44). Closes #78 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
294 lines
12 KiB
GDScript
294 lines
12 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)
|
|
|
|
|
|
## N8: with build_in_background the island is made on a worker thread, and the packets
|
|
## that arrive meanwhile are held, then taken in order once it's made.
|
|
func test_a_background_island_holds_packets_and_takes_them_in_order() -> void:
|
|
var worlds: Array[int] = []
|
|
var client: ClientGame = ClientGame.new()
|
|
client.build_in_background = true
|
|
client.make_world = func(match_seed: int) -> Simulation:
|
|
# Slow enough that the packets below certainly arrive while it runs.
|
|
OS.delay_msec(300)
|
|
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_true(client.building_world(), "the notice starts the island in the background")
|
|
assert_null(client.world, "and nothing waits for it")
|
|
client.receive(NetProtocol.CHANNEL_CONTROL, game.world_notice())
|
|
loop.run_tick()
|
|
game.after_tick()
|
|
client.receive(NetProtocol.CHANNEL_STATE, game.snapshot_for(7))
|
|
assert_eq(client.held_count(), 3, "the notice, the world and the snapshot are held")
|
|
assert_eq(client.receiver.malformed, 0)
|
|
var started: int = Time.get_ticks_msec()
|
|
while client.building_world() and Time.get_ticks_msec() - started < 5000:
|
|
client.advance(DT)
|
|
OS.delay_msec(5)
|
|
assert_false(client.building_world())
|
|
assert_eq(client.held_count(), 0, "everything held was taken in")
|
|
assert_eq(worlds, [loop.match_flow.current_seed()], "one island, for the notice's seed")
|
|
assert_not_null(client.world)
|
|
assert_eq(client.world_seed, loop.match_flow.current_seed())
|
|
assert_eq(client.match_index, 0)
|
|
assert_true(client.replica.applied >= 1, "the WORLD message went on the new island")
|
|
assert_eq(client.outbox.size(), 1, "and the snapshot was answered")
|
|
assert_eq(client.bad_packets, 0)
|
|
# The same seed again doesn't make another island.
|
|
client.receive(NetProtocol.CHANNEL_CONTROL, game.match_notice())
|
|
assert_false(client.building_world())
|
|
assert_eq(worlds.size(), 1)
|
|
|
|
|
|
func test_a_client_freed_while_its_island_is_made_waits_for_it() -> void:
|
|
var client: ClientGame = ClientGame.new()
|
|
client.build_in_background = true
|
|
client.make_world = func(match_seed: int) -> Simulation:
|
|
OS.delay_msec(100)
|
|
return Simulation.new(match_seed, SimGround.flat())
|
|
client.receive(NetProtocol.CHANNEL_CONTROL, game.match_notice())
|
|
assert_true(client.building_world())
|
|
client = null
|
|
assert_true(true, "freeing it with a task running raises no error")
|
|
|
|
|
|
## N11: shots, impacts and swings near what the client watches reach it as FX
|
|
## records, each one an event the server's simulation really had. The client's own
|
|
## player, once landed, is given a hitscan weapon and fires at the ground.
|
|
func test_shots_and_hits_reach_the_client() -> void:
|
|
var client: ClientGame = ClientGame.new()
|
|
assert_true(loop.match_flow.join(7))
|
|
game.adopt(7)
|
|
client.receive(NetProtocol.CHANNEL_CONTROL, game.match_notice())
|
|
client.receive(NetProtocol.CHANNEL_CONTROL, game.world_notice())
|
|
var weapon: ItemDef = null
|
|
for def: ItemDef in ItemCatalog.all():
|
|
if def.kind == ItemDef.WEAPON and def.projectile_speed <= 0.0 and def.ammo_code >= 0 and def.damage > 0:
|
|
weapon = def
|
|
break
|
|
assert_not_null(weapon, "a hitscan weapon to fire")
|
|
var happened: Dictionary = {}
|
|
var shots: int = 0
|
|
var impacts: int = 0
|
|
var armed: bool = false
|
|
for round_index: int in 4000:
|
|
var flow: Match = loop.match_flow
|
|
var mine: SimEntity = loop.sim.entities.find(int(flow.entity_of.get(7, 0))) if flow.in_match() else null
|
|
if mine != null and mine.drop_state == Drop.NONE and not mine.eliminated:
|
|
if not armed:
|
|
armed = true
|
|
# Wherever it came down, stand it on dry ground well above the sea.
|
|
mine.position = _dry_spot(loop.sim)
|
|
mine.velocity = Vector3.ZERO
|
|
mine.swimming = false
|
|
mine.on_ground = true
|
|
Inventory.give(mine, weapon.code, 1)
|
|
Inventory.give(mine, weapon.ammo_code, 60)
|
|
# Press and release, so any weapon fires again.
|
|
var buttons: int = PlayerCommand.BUTTON_FIRE if round_index % 8 == 0 else 0
|
|
loop.submit(7, PlayerCommand.from_input(loop.sim.tick, Vector2.ZERO, 0.0, -0.7, buttons, 1))
|
|
loop.run_tick()
|
|
game.after_tick()
|
|
if loop.match_flow.in_match():
|
|
for event: Dictionary in loop.sim.events:
|
|
var record: Dictionary = NetFx.from_event(event, loop.sim.tick)
|
|
if not record.is_empty():
|
|
# A scattering weapon lands several pellets on one tick.
|
|
var key: String = "%d %d %d" % [record["tick"], record["kind"], record["player"]]
|
|
if not happened.has(key):
|
|
happened[key] = []
|
|
(happened[key] as Array).append(record["position"])
|
|
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.snapshot_due():
|
|
client.receive(NetProtocol.CHANNEL_STATE, game.snapshot_for(7))
|
|
var fx: PackedByteArray = game.fx_for(7)
|
|
if not fx.is_empty():
|
|
client.receive(NetProtocol.CHANNEL_STATE, fx)
|
|
for record: Dictionary in client.fx:
|
|
var key: String = "%d %d %d" % [record["tick"], record["kind"], record["player"]]
|
|
assert_true(happened.has(key), "FX %s happened on the server" % key)
|
|
if happened.has(key) and record["kind"] != NetFx.HARVEST:
|
|
var near: bool = false
|
|
for position: Vector3 in happened[key]:
|
|
near = near or (record["position"] as Vector3).distance_to(position) < 0.1
|
|
assert_true(near, "FX %s at the same place" % key)
|
|
shots += 1 if record["kind"] == NetFx.SHOT else 0
|
|
impacts += 1 if record["kind"] == NetFx.IMPACT else 0
|
|
client.fx.clear()
|
|
if shots >= 3 and impacts >= 3:
|
|
break
|
|
assert_true(armed, "the player landed and was armed")
|
|
assert_true(shots >= 3, "shots arrived: %d" % shots)
|
|
assert_true(impacts >= 3, "and where they landed: %d" % impacts)
|
|
assert_eq(client.bad_packets, 0)
|
|
assert_true(game.fx_for(7).is_empty(), "nothing is sent twice")
|
|
|
|
|
|
func _dry_spot(sim: Simulation) -> Vector3:
|
|
for z: int in range(64, 960, 16):
|
|
for x: int in range(64, 960, 16):
|
|
var y: float = sim.ground.terrain_height(x, z)
|
|
var feet: Vector3 = Vector3(x, y, z)
|
|
if y > 15.0 and not sim.ground.body_blocked(feet, 2.0, Tuning.PLAYER_HEIGHT, 0.0):
|
|
return feet
|
|
return Vector3(512.0, sim.ground.terrain_height(512.0, 512.0), 512.0)
|