extends SceneTree ## Co-op over real GD-Sync (LAN mode, peer to peer on this machine): run two of these at once, ## a host and a partner. Each loads the game (main.tscn, with its own save and settings files), ## the host starts a vacation and opens a lobby, the partner finds it on the network and joins, ## and both play a day and a night with enemies. Each prints what it saw and exits 0 when it ## all checked out: ## godot --headless --path . --script res://game/tools/coop_smoke.gd -- --role=host & ## godot --headless --path . --script res://game/tools/coop_smoke.gd -- --role=join ## Options: --seconds=N how long to play once joined (default 20); --discover makes the partner ## find the host by GD-Sync's LAN announcements (broadcast) instead of joining by address ## (127.0.0.1), for networks where broadcasts reach this machine. --online plays over GD-Sync's ## servers instead (needs the API keys): the partner finds the host's lobby in the online list. ## --host-name=Ana names the host (and so its lobby, "Ana's farm"); give both sides the same one. ## --address=192.168.1.20 joins a host at that address (LAN) instead of 127.0.0.1. var role := "host" var seconds := 20.0 var discover := false var online := false var host_name := "Ana" var address := "127.0.0.1" ## --shot=path: a screenshot at night with the enemies in (needs a window, not --headless). var shot := "" var main: Main var failures: Array[String] = [] func _initialize() -> void: for a in OS.get_cmdline_user_args(): if String(a).begins_with("--role="): role = String(a).trim_prefix("--role=") elif String(a).begins_with("--shot="): shot = String(a).trim_prefix("--shot=") elif String(a) == "--discover": discover = true elif String(a) == "--online": online = true elif String(a).begins_with("--host-name="): host_name = String(a).trim_prefix("--host-name=") elif String(a).begins_with("--address="): address = String(a).trim_prefix("--address=") elif String(a).begins_with("--seconds="): seconds = float(String(a).trim_prefix("--seconds=")) _run.call_deferred() func _say(text: String) -> void: print("[%s %6.2f] %s" % [role, Time.get_ticks_msec() / 1000.0, text]) func _check(ok: bool, what: String) -> void: _say(("OK " if ok else "FAIL ") + what) if not ok: failures.append(what) func _run() -> void: main = (load("res://game/scenes/main.tscn") as PackedScene).instantiate() as Main main.save_path = "user://coop_smoke_%s_save.json" % role main.settings_path = "user://coop_smoke_%s_settings.cfg" % role root.add_child(main) current_scene = main # as in the game: GD-Sync looks at the current scene await process_frame await process_frame var lobby := main.lobby var sim := main.sim lobby.failed.connect(func(t: String) -> void: _say("lobby failed: " + t)) lobby.status_changed.connect(func(t: String) -> void: if t != "": _say("lobby: " + t)) if role == "host": main.new_vacation() lobby.host(CoopLobby.Mode.ONLINE if online else CoopLobby.Mode.LAN, host_name) if not await _wait(func() -> bool: return sim.coop.is_host(), 60.0): _check(false, "a partner joined within 60 s") await _finish() return _check(true, "a partner joined: %s" % sim.coop.partner_name) await _wait(func() -> bool: return false, 3.0) sim.campaign.begin_day() await _wait(func() -> bool: return false, 2.0) sim.campaign.advance_phase() sim.campaign.advance_phase() for i in 24: sim.spawn_enemy([EnemySystem.TYPE_CROW, EnemySystem.TYPE_THIEF, EnemySystem.TYPE_BREAKER][i % 3]) await _wait(func() -> bool: return false, 8.0) await _screenshot() await _wait(func() -> bool: return false, seconds - 8.0) _check(sim.partner != null and sim.partner.body.global_position.distance_to(Vector3(-4.0, 0.0, -2.0)) < 1.0, "P2 walked where the partner moved it, twice (%s)" % [sim.partner.body.global_position if sim.partner != null else "gone"]) _check(sim.partner != null and sim.partner.combat.attacks() > 0, "P2's weapon attacked on the host (%d attacks)" % (sim.partner.combat.attacks() if sim.partner != null else 0)) _check(sim.campaign.state.coop, "co-op is on in the farm state") _say("waiting for the partner to finish") await _wait(func() -> bool: return not sim.coop.is_active(), 30.0) _check(not sim.coop.is_active() and sim.partner == null, "the partner left; the host plays on alone") else: if discover or online: var want := "%s's farm" % host_name lobby.search(CoopLobby.Mode.ONLINE if online else CoopLobby.Mode.LAN, "Ben") var found: Array = [] lobby.lobbies_found.connect(func(list: Array) -> void: found.assign(list.filter(func(l: Dictionary) -> bool: return String(l["name"]) == want))) if not await _wait(func() -> bool: return not found.is_empty(), 40.0): _check(false, "found the host's game \"%s\" %s within 40 s" % [want, "online" if online else "on the network"]) await _finish() return _check(true, "found %s %s" % [found[0]["name"], "online" if online else "on the network"]) lobby.join(String(found[0]["name"])) else: lobby.join_address(address, "Ben") if not await _wait(func() -> bool: return sim.coop.is_client() and sim.coop._is_welcomed, 40.0): _check(false, "joined and got the farm within 40 s") await _finish() return _check(main.screen == Main.Screen.PLAYING and sim.replica and sim.local_avatar.slot == 1, "joined: playing P2 on a replica of the host's farm") sim.player.global_position = Vector3(4.0, 0.0, -8.0) await _wait(func() -> bool: return sim.campaign.state.phase == CampaignController.Phase.NIGHT, 30.0) _check(sim.campaign.state.phase == CampaignController.Phase.NIGHT, "the host's nightfall reached the partner") Input.action_press("fire") await _wait(func() -> bool: return false, 4.0) Input.action_release("fire") await _wait(func() -> bool: return sim.enemies.alive_count() >= 10, 10.0) _check(sim.enemies.alive_count() >= 10, "the host's enemies show on the partner's farm (%d)" % sim.enemies.alive_count()) _check(sim.coop.voices.size() == 2, "voice chat is set up (a VoiceChat per player)") sim.player.global_position = Vector3(-4.0, 0.0, -2.0) # off the host's screen: the arrow await _wait(func() -> bool: return false, 2.0) await _screenshot() await _wait(func() -> bool: return false, seconds - 4.0) _say("leaving") lobby.leave() await _wait(func() -> bool: return false, 2.0) _finish() func _screenshot() -> void: if shot == "": return await RenderingServer.frame_post_draw root.get_texture().get_image().save_png(shot) _say("screenshot " + shot) ## Waits up to timeout seconds for done() (real time); returns whether it happened. func _wait(done: Callable, timeout: float) -> bool: var t0 := Time.get_ticks_msec() while Time.get_ticks_msec() - t0 < timeout * 1000.0: if done.call(): return true await process_frame return bool(done.call()) func _finish() -> void: _say("%s: %d failed" % ["PASS" if failures.is_empty() else "FAILURES", failures.size()]) for p in ["save.json", "settings.cfg"]: var path := "user://coop_smoke_%s_%s" % [role, p] if FileAccess.file_exists(path): DirAccess.remove_absolute(ProjectSettings.globalize_path(path)) if is_instance_valid(main): main.lobby.leave() main.queue_free() await process_frame await create_timer(0.3).timeout quit(failures.size())