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) ## R3: `tidewall --connect=HOST` works without the "--" the engine wants before game ## options, as the player guide promises, and the engine's own options are left alone. func test_game_options_are_found_with_or_without_the_separator() -> void: var engine: PackedStringArray = PackedStringArray(["--headless", "--rendering-method", "gl_compatibility", "--connect=192.0.2.7:7000", "--name=Mara"]) var found: PackedStringArray = ClientConfig.game_args(PackedStringArray([]), engine) assert_eq(Array(found), ["--connect=192.0.2.7:7000", "--name=Mara"]) var join: ClientConfig = ClientConfig.parse(found) assert_true(join.is_valid(), str(join.errors)) assert_eq(join.host, "192.0.2.7") assert_eq(join.port, 7000) # Options after "--" are the only ones that count when there are any. var user: PackedStringArray = PackedStringArray(["--offline"]) assert_eq(Array(ClientConfig.game_args(user, engine)), ["--offline"]) # A bare start finds nothing, so a window opens the menu. assert_eq(ClientConfig.game_args(PackedStringArray([]), PackedStringArray(["--headless"])).size(), 0) 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()) static func _pad_button(index: JoyButton, pressed: bool) -> InputEventJoypadButton: var event: InputEventJoypadButton = InputEventJoypadButton.new() event.button_index = index event.pressed = pressed return event static func _pad_axis(which: JoyAxis, value: float) -> InputEventJoypadMotion: var event: InputEventJoypadMotion = InputEventJoypadMotion.new() event.axis = which event.axis_value = value return event func test_a_gamepad_plays_through_the_session() -> void: # A13: the script lands the player; then a simulated pad takes over. session.play_offline(4, 3) for frame: int in 60 * 150: session.pump(FRAME) if session.game.playing() and _own().drop_state == Drop.NONE and _own().on_ground: break assert_true(session.game.playing() and _own().drop_state == Drop.NONE, "landed") session.input.autoplay = false session.input.yaw = 0.0 var start: Vector3 = _own().position session.input.handle(_pad_axis(JOY_AXIS_LEFT_Y, -1.0), false) for frame: int in 60 * 2: session.pump(FRAME) var walked: float = Vector2(_own().position.x - start.x, _own().position.z - start.z).length() assert_true(walked > 3.0, "the left stick walked the player %.1f m" % walked) assert_false(session.input.targets.is_empty(), "aim assist sees the bots as opponents") session.input.handle(_pad_axis(JOY_AXIS_LEFT_Y, 0.0), false) session.input.handle(_pad_button(JOY_BUTTON_RIGHT_SHOULDER, true), false) session.input.handle(_pad_button(JOY_BUTTON_RIGHT_SHOULDER, false), false) for frame: int in 10: session.pump(FRAME) assert_eq(_own().selected, 1, "the right bumper picked the first slot") # Start asks for the pause menu, as Escape does. var asked: Array[bool] = [false] session.pause_requested.connect(func() -> void: asked[0] = true) var tree: SceneTree = Engine.get_main_loop() as SceneTree tree.root.add_child(session) session._unhandled_input(_pad_button(JOY_BUTTON_START, true)) assert_true(asked[0], "Start asked for the pause menu") tree.root.remove_child(session)