Files
tidewall/test/client/test_client_session.gd
T
claudeandClaude Opus 5.5 38812120df H3: Balance pass from 200 bot matches (make balance)
make balance (tools/balance.sh, tools/balance.gd, BalanceReport) plays 200
bot-only matches in four processes under nice and reports match length,
players standing over time, causes of death, eliminations by weapon and by
difficulty, judged against the new BALANCE_* targets in Tuning and the
GDD's Balance section.

Before: median match 5.0 min, 3.4 of 24 standing at 2 min, the boathook
41% of eliminations. After, over seeds 1001-1200: median 13.1 min (p10 9.2,
p90 14.6), top weapon the rivet repeater at 31.7%. Passes.

Changes, each traced from single-match runs (docs/BALANCE.md):
- Fix: bots leapt from the barge on a timer and landed hundreds of metres
  short of their spot; they now leap when it's in gliding reach or the
  barge starts drawing away from it.
- Bots spread their landings, only brawl whoever hit them, pick out new
  opponents within 30 m, roam locally and hold ground, run from a gun when
  unarmed, and (normal and hard) break off when badly hurt.
- Aim error widened; rivet repeater damage 17 to 15.
- Tide: lower early levels, longer holds; every match ends within 17.75 min.

Tests: BalanceReport, the new bot behaviours, the tide length. Tests that
leaned on old bot luck now pin or set up what they check. make match moves
to seed 29, where the scripted player still builds.

Closes #56

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 06:28:15 -05:00

255 lines
12 KiB
GDScript

extends TestCase
## Issue N10: the client program's session. Offline it plays against a server in the
## same process; on the network, against a real server over loopback UDP. Both go
## through the same ClientGame, and the same MatchView draws the client's own copy of
## the world.
const HOST: String = "127.0.0.1"
const DT: float = 1.0 / Tuning.TICK_RATE_HZ
const FRAME: float = 1.0 / 60.0
var session: ClientSession
var net: NetServer
func before_each() -> void:
session = ClientSession.new()
session.input.autoplay = true
net = null
func after_each() -> void:
session.leave()
session.free()
if net != null:
net.close()
func _own() -> SimEntity:
return session.game.prediction.player
func _agrees(sim: Simulation, message: String) -> void:
session.game.replica.sync(sim.tick)
assert_eq(NetWorld.world_hash(session.game.world), NetWorld.world_hash(sim), message)
func test_offline_play_goes_through_the_same_client_against_a_local_server() -> void:
session.play_offline(2, 3)
assert_null(session.link)
assert_null(session.game.net, "no socket: the packets are handed across")
var server: ServerLoop = session.local.loop
var seen_barge: bool = false
var most_figures: int = 0
var landed_frame: int = -1
for frame: int in 60 * 150:
session.pump(FRAME)
if session.view != null:
most_figures = maxi(most_figures, session.view.entities.figure_count())
if session.view.entities.barge().visible and session.mirror.own_id != 0:
if not seen_barge:
seen_barge = true
var figure: Node3D = session.view.entities.figure_of(session.mirror.own_id)
assert_not_null(figure, "the client's own player is drawn")
assert_true(figure.position.distance_to(session.view.entities.barge().position) < 6.0, "on the barge's deck")
assert_true(session.view.camera.position.distance_to(figure.position) < 9.0, "the camera is on its shoulder")
if landed_frame < 0 and session.game.playing() and _own().drop_state == Drop.NONE:
landed_frame = frame
if landed_frame >= 0 and frame > landed_frame + 60 * 8:
break
assert_true(seen_barge)
assert_true(landed_frame > 0, "the scripted player leapt and came down")
assert_eq(session.phases[0], Match.DROP)
assert_eq(most_figures, 4, "all four players were drawn at once")
assert_true(session.frames_playing > 60 * 20)
var flow: Match = server.match_flow
var mine: SimEntity = server.sim.entities.find(flow.entity_of[LocalServer.HANDLE])
assert_false(mine.eliminated)
assert_eq(session.mirror.own_id, mine.id)
assert_eq(session.status_text(), "", "nothing to say while playing")
var figure: Node3D = session.view.entities.figure_of(mine.id)
assert_true(figure.position.distance_to(mine.position) < 1.5, "drawn %.2f m from where the server has it" % figure.position.distance_to(mine.position))
var came_down: Vector3 = mine.position
assert_true(session.view.camera.position.distance_to(figure.position) < 9.0)
assert_eq(session.view.sim, session.game.world, "the view draws the client's own copy of the world")
assert_ne(session.game.world, server.sim)
assert_eq(session.view.entities.get_node("Chests").multimesh.instance_count, server.sim.loot.chest_positions.size())
assert_near(session.game.world.sea_level, server.sim.sea_level, 0.05)
_agrees(server.sim, "on the ground")
assert_eq(session.game.bad_packets, 0)
assert_eq(session.local.game.bad_inputs, 0)
assert_true(session.game.prediction.max_correction < 1.0, "largest correction %.2f m" % session.game.prediction.max_correction)
# A bot brings the scripted player down. (It used to be left to the bots, but since
# H3 they hold their ground and how soon one turns up is luck.) From then on the
# client watches whoever it is given to watch.
for frame: int in 60 * 20:
session.pump(FRAME)
if mine.eliminated or came_down.distance_to(mine.position) > 3.0:
break
var walked_to: Vector3 = mine.position
Weapons.damage_player(server.sim, mine, Tuning.PLAYER_MAX_HEALTH + Tuning.PLAYER_MAX_ARMOR, server.bots[0].player_id)
for frame: int in 60 * 10:
session.pump(FRAME)
if mine.eliminated and session.mirror.watched_id != 0 and session.mirror.watched_id != mine.id:
break
assert_true(came_down.distance_to(walked_to) > 3.0, "it walked: the server took its commands")
assert_true(mine.eliminated, "the scripted player fell")
assert_false(session.game.playing())
assert_eq(session.mirror.own_id, 0)
assert_eq(session.status_text(), "Watching")
var watched: Node3D = session.view.entities.figure_of(session.mirror.watched_id)
assert_not_null(watched)
assert_true(session.view.camera.position.distance_to(watched.position) < 9.0, "the camera moved to the player being watched")
_agrees(server.sim, "while watching")
func test_a_scripted_client_plays_a_whole_match_on_a_real_server() -> void:
var rules: MatchRules = MatchRules.new()
rules.max_players = 4
rules.min_players = 1
rules.lobby_ticks = 10
rules.countdown_ticks = 10
rules.results_ticks = 90
var loop: ServerLoop = ServerLoop.new(41, 0, 3, rules)
net = NetServer.new()
assert_eq(net.listen(0, HOST), OK)
var game: NetGame = NetGame.new(loop, net)
assert_eq(session.connect_to(HOST, net.port(), "Mara"), OK)
assert_true(session.status_text().length() > 0, "it says it is connecting")
var flow: Match = loop.match_flow
var started: int = Time.get_ticks_msec()
var spectated: int = 0
var checked: int = 0
# A server tick and a client frame of the same length each round, so the clocks
# agree however fast this machine is.
while Time.get_ticks_msec() - started < 240000 and not session.phases.has(Match.RESULTS):
game.poll()
if session.game.building_world():
# The island is made in the background. A real lobby lasts 20 s, far
# longer than that takes; this one is 10 ticks, so the server waits.
session.pump(DT)
OS.delay_msec(5)
continue
loop.run_tick()
game.after_tick()
session.pump(DT)
if flow.in_match() and not session.game.playing() and session.mirror.watched_id != 0:
spectated += 1
if flow.in_match() and loop.ticks % 300 == 0 and session.game.replica.seq == game.world.seq:
checked += 1
_agrees(loop.sim, "at tick %d" % loop.sim.tick)
assert_eq(session.phases, [Match.LOBBY, Match.COUNTDOWN, Match.DROP, Match.PLAY, Match.RESULTS] as Array[int], "lobby to results")
assert_eq(flow.phase, Match.RESULTS)
assert_eq(session.link.state, NetClient.JOINED)
assert_eq(session.game.match_index, 0)
assert_true(session.frames_playing > 300, "it played for %d ticks" % session.frames_playing)
assert_true(checked > 0, "the worlds were compared %d times" % checked)
assert_not_null(session.view)
assert_eq(session.view.sim.island.heightmap_hash(), loop.sim.island.heightmap_hash())
# Results: the camera is on whoever is left, or the client itself if it won.
assert_true(session.mirror.watched_id != 0, "someone to watch at the end")
assert_not_null(session.view.entities.figure_of(session.mirror.watched_id))
var summary: Array[Dictionary] = flow.summary()
assert_eq(summary.size(), 4)
assert_eq(session.game.bad_packets, 0)
assert_eq(game.bad_inputs, 0)
assert_eq(session.game.replica.gaps, 0)
print("N10 match over UDP: %d server ticks, client played %d and watched %d, %d world checks, %d corrections (largest %.2f m), %d WORLD messages" % [
loop.ticks, session.frames_playing, spectated, checked, session.game.prediction.mispredictions,
session.game.prediction.max_correction, session.game.replica.applied,
])
func test_keys_and_mouse_become_commands() -> void:
var input: ClientInput = ClientInput.new()
input.move = Vector2(1.0, -1.0)
input.buttons = PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_SPRINT
input.press(KEY_3)
var command: PlayerCommand = input.command(77)
assert_eq(command.tick, 77)
assert_eq(command.move_x, PlayerCommand.AXIS_MAX)
assert_eq(command.move_y, -PlayerCommand.AXIS_MAX)
assert_eq(command.selection, 2, "3 is the second slot: 1 is the boathook")
assert_true(command.is_pressed(PlayerCommand.BUTTON_FIRE) and command.is_pressed(PlayerCommand.BUTTON_SPRINT))
assert_false(command.is_pressed(PlayerCommand.BUTTON_BUILD))
input.press(KEY_1)
assert_eq(input.command(78).selection, Simulation.BOATHOOK_SLOT)
input.press(KEY_9)
assert_eq(input.slot, 0, "there is no eighth slot")
# Build mode: the number row picks the piece, Tab the material, fire places.
input.press(KEY_TAB)
assert_eq(input.material, Materials.DRIFTWOOD, "Tab does nothing outside build mode")
input.press(KEY_B)
input.press(KEY_5)
input.press(KEY_TAB)
command = input.command(79)
assert_true(command.is_pressed(PlayerCommand.BUTTON_BUILD))
assert_eq(command.selection, StructureSet.BREAKWATER)
assert_eq(command.material, Materials.STONE)
input.press(KEY_7)
assert_eq(input.build_kind, StructureSet.BREAKWATER, "there is no seventh piece")
input.press(KEY_B)
assert_eq(input.command(80).selection, 0, "back to what was held")
# The mouse turns the look: right is clockwise seen from above, up looks up.
input.turn(Vector2(100.0, -40.0))
assert_true(input.yaw > PI, "turned right from north: %.2f" % input.yaw)
assert_true(input.pitch > 0.0)
command = input.command(81)
assert_eq(command.yaw, PlayerCommand.from_input(0, Vector2.ZERO, input.yaw, input.pitch).yaw)
# Aiming slows the look, and build mode doesn't aim.
input.buttons = PlayerCommand.BUTTON_AIM
assert_true(input.aiming())
input.press(KEY_B)
assert_false(input.aiming())
func test_the_command_line_picks_the_mode() -> void:
var offline: ClientConfig = ClientConfig.parse(PackedStringArray([]))
assert_true(offline.is_valid())
assert_false(offline.asked)
assert_eq(offline.host, "")
assert_eq(offline.bots, Tuning.SOLO_PLAYERS - 1)
var join: ClientConfig = ClientConfig.parse(PackedStringArray(["--connect=play.example:7000", "--name=Mara", "--autoplay", "--client-seconds=2.5"]))
assert_true(join.is_valid(), str(join.errors))
assert_true(join.asked)
assert_eq(join.host, "play.example")
assert_eq(join.port, 7000)
assert_eq(join.player_name, "Mara")
assert_true(join.autoplay)
assert_near(join.seconds, 2.5, 0.001)
assert_eq(ClientConfig.parse(PackedStringArray(["--connect=192.0.2.7"])).port, ServerConfig.DEFAULT_PORT)
var seeded: ClientConfig = ClientConfig.parse(PackedStringArray(["--offline", "--seed=9", "--bots=5"]))
assert_true(seeded.asked and seeded.has_seed)
assert_eq(seeded.match_seed, 9)
assert_eq(seeded.bots, 5)
for bad: String in ["--connect=", "--connect=host:0", "--connect=host:port", "--seed=x", "--bots=99", "--client-seconds=0", "--nonsense"]:
assert_false(ClientConfig.parse(PackedStringArray([bad])).is_valid(), bad)
func test_restored_entities_keep_their_ids_and_their_order() -> void:
var registry: EntityRegistry = EntityRegistry.new()
registry.restore(7, SimEntity.ARCHETYPE_PLAYER)
registry.restore(3, SimEntity.ARCHETYPE_PICKUP)
registry.restore(12, SimEntity.ARCHETYPE_PLAYER)
registry.restore(5, SimEntity.ARCHETYPE_PROJECTILE)
var ids: Array[int] = []
for entity: SimEntity in registry.entities():
ids.append(entity.id)
assert_eq(ids, [3, 5, 7, 12] as Array[int])
assert_eq(registry.find(3).archetype, SimEntity.ARCHETYPE_PICKUP)
assert_eq(registry.create(SimEntity.ARCHETYPE_PLAYER).id, 13, "new IDs carry on past the highest")
func test_browse_options() -> void:
var browse: ClientConfig = ClientConfig.parse(PackedStringArray(["--browse", "--list=192.0.2.4", "--lan-target=127.0.0.1", "--lan-port=30000", "--client-seconds=1"]))
assert_true(browse.is_valid(), str(browse.errors))
assert_true(browse.browse)
assert_true(browse.asked)
assert_eq(browse.lists, [{"host": "192.0.2.4", "port": NetDiscovery.DEFAULT_LIST_PORT}] as Array[Dictionary])
assert_eq(browse.lan_targets, ["127.0.0.1"] as Array[String])
assert_eq(browse.lan_port, 30000)
assert_eq(ClientConfig.parse(PackedStringArray(["--browse"])).lan_port, ServerConfig.DEFAULT_PORT)
assert_false(ClientConfig.parse(PackedStringArray(["--browse", "--lan-target=somewhere"])).is_valid())
assert_false(ClientConfig.parse(PackedStringArray(["--browse", "--connect=192.0.2.7"])).is_valid())