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. By day the partner walks up to the supply shed's ## counter, and E (a key event, as a keyboard sends it) opens the shed's menu there and E again ## closes it (#22). 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() for visit: String in ["standing at the counter", "on arrival"]: # the partner's _shop _check(await _wait(func() -> bool: return sim.partner != null and sim.partner.remote.ui_open, 20.0), "P2 opened the shed's menu by day, %s (the host saw P2's menu up)" % visit) _check(await _wait(func() -> bool: return sim.partner == null or not sim.partner.remote.ui_open, 10.0), "and closed it") await _wait(func() -> bool: return false, 1.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") await _wait(func() -> bool: return sim.campaign.is_daytime(), 30.0) await _shop(sim) 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() ## The partner's shop (#22): P2 walks up to the supply shed's counter by day, and E (a key event, ## as a player's keyboard sends it) opens the shed's menu; E again closes it. Then E pressed on ## arrival, still walking in at run speed just inside the counter's reach: the host's copy of P2 ## isn't in reach yet when the press gets there, and it opens all the same. func _shop(sim: FarmSim) -> void: var counter := sim.interactions.shed_counter sim.player.global_position = counter.global_position + Vector3(0.0, 0.0, 1.2) _check(await _wait(func() -> bool: return sim.interactions.focus == counter and counter._label.text.begins_with("[E / X] Shop"), 3.0), "P2 at the shed's counter: its prompt shows ('%s')" % counter._label.text.get_slice("\n", 0)) await _key(KEY_E) _check(await _wait(func() -> bool: return sim.shed_panel.is_open, 4.0), "E there opens the shed's menu") await _wait(func() -> bool: return false, 0.5) await _key(KEY_E) _check(await _wait(func() -> bool: return not sim.shed_panel.is_open, 4.0), "E again closes it") var way := Vector3(1.0, 0.0, 1.0).normalized() var d := 6.0 sim.player.global_position = counter.global_position + way * d await _wait(func() -> bool: return false, 1.0) var pressed := false while d > 1.2: # 5 m/s, as the scarecrow runs; E at 2.1 m (the reach is 2.2) await physics_frame d = maxf(d - 5.0 / 60.0, 1.2) sim.player.global_position = counter.global_position + way * d if not pressed and d <= 2.1: pressed = true Input.parse_input_event(_key_event(KEY_E, true)) await _wait(func() -> bool: return false, 0.05) Input.parse_input_event(_key_event(KEY_E, false)) _check(await _wait(func() -> bool: return sim.shed_panel.is_open, 4.0), "E on arrival, still walking in at run speed, opens it too") await _wait(func() -> bool: return false, 0.5) await _key(KEY_E) _check(await _wait(func() -> bool: return not sim.shed_panel.is_open, 4.0), "(and E closes it)") await _wait(func() -> bool: return false, 1.0) ## A key pressed and let go (input events, through the GUI and every _unhandled_input, as a ## player's keyboard sends them). func _key(physical: Key) -> void: for down in [true, false]: Input.parse_input_event(_key_event(physical, down)) await _wait(func() -> bool: return false, 0.12) static func _key_event(physical: Key, down: bool) -> InputEventKey: var e := InputEventKey.new() e.physical_keycode = physical e.pressed = down return e 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())