extends RefCounted ## Co-op in one process: the host's farm in the main tree and the partner's in a SubViewport ## with its own world (so the two don't share physics), joined by a loopback CoopLink and stepped ## by hand, tick by tick. Covers the join (the welcome, both scarecrows on both farms, names, ## co-op health), requests from the partner (start the day, build, buy), the partner's scarecrow ## moving, gathering, and shooting on the host (and its pick of weapon), each scarecrow's own ## pouch (#16: gathering, the shed drop-off, paying, one shared capacity, goop to the killer, and ## the partner's pouch put in the shed when they leave), the partner's shops (#22: the prompt, the ## menus opening on the partner's farm and closing on interact, a press that beats the host's copy ## of P2 to the counter, a weapon bought into P2's hand), the weapons' stabs and ## pumpkin bombs (thrown from the off-hand, not with the right click leaving build mode) reaching the replica (drawn there, doing no damage of their own), enemies, loot, ## and shots reaching the replica, ## the co-op fall rule (one down is out until morning, both down is Game Over) and Retry, message ## sizes with a crowd, dropped unreliable messages, and either side leaving. And the traffic: the ## farm state isn't resent for the clock alone, a partner standing still sends little, enemies go ## by dead reckoning, and voice chat's codec and gate (CoopVoice). const P := CampaignController.Phase const DT := 1.0 / 60.0 const AWAY := Vector3(-11.0, 0.0, 13.0) ## Ticks for a change to the farm state to reach the partner (it goes out every STATE_EVERY). const SETTLE := CoopSession.STATE_EVERY + 3 var r: GameTestRunner var host_farm: Node3D var client_farm: Node3D var host: FarmSim var client: FarmSim var hs: CoopSession var cs: CoopSession var links: Array[CoopLink] var _viewport: SubViewport func run(p_r: GameTestRunner) -> void: r = p_r await _setup() _join() _requests() _quiet_wire() await _partner_moves_and_gathers() _own_pouches() _shops() _night() _partner_shoots() _goop_to_killer() _weapons_shown() _fall_rule() _crowd() _leaving() _voice() for l in links: l.close() host_farm.queue_free() _viewport.get_parent().queue_free() await r.process_frame func _setup() -> void: host_farm = (load("res://game/scenes/farm.tscn") as PackedScene).instantiate() as Node3D r.root.add_child(host_farm) _viewport = SubViewport.new() _viewport.own_world_3d = true _viewport.size = Vector2i(320, 180) var box := SubViewportContainer.new() r.root.add_child(box) box.add_child(_viewport) client_farm = (load("res://game/scenes/farm.tscn") as PackedScene).instantiate() as Node3D _viewport.add_child(client_farm) await r.process_frame await r.physics_frame host = host_farm.get_node("FarmSim") as FarmSim client = client_farm.get_node("FarmSim") as FarmSim for f: FarmSim in [host, client]: f.set_physics_process(false) f.coop.set_physics_process(false) f.waves.enabled = false host.player.global_position = AWAY host.waves.rng.seed = 1 # repeatable arrival points hs = host.coop cs = client.coop links = CoopLink.pair() ## One tick on both machines, in the order the frames would run them. func _pump(n: int = 1) -> void: for i in n: for body: PlayerCharacter in _remote_bodies(): body._follow_net(DT) host.step(DT) hs._physics_process(DT) client.step(DT) cs._physics_process(DT) func _remote_bodies() -> Array[PlayerCharacter]: var out: Array[PlayerCharacter] = [] for f: FarmSim in [host, client]: for a in f.avatars: if a.body.remote_driven: out.append(a.body) return out func _same_state() -> bool: var a := host.campaign.state.to_dict() var b := client.campaign.state.to_dict() for d: Dictionary in [a, b]: d.erase("phase_time_left") d.erase("night_events") return JSON.stringify(a, "", true, true) == JSON.stringify(b, "", true, true) func _join() -> void: hs.start_host(host, links[0], "Ana", "Ben") cs.start_client(client, links[1], "Ben") _pump(2) r.check(hs.is_host() and cs.is_client() and client.replica and not host.replica, "the host and the partner each take their role") r.check(host.avatars.size() == 2 and client.avatars.size() == 2 and host.partner.remote != null and not host.partner.is_local and client.local_avatar.slot == 1 and client.avatars[0].body.remote_driven and host.partner.body.remote_driven, "both farms have both scarecrows: the host plays P1, the partner P2") r.check(client.player == client.avatars[1].body and client.scarecrow.slot == 1 and client.hud.kit == client.scarecrow and (client.camera as FollowCamera).target == client.player, "the partner's HUD and camera follow P2") r.check(host.campaign.state.coop and client.campaign.state.coop and host.campaign.state.partner_hp == host.config.scarecrow_max_hp, "co-op is on: P2 has full health") r.check(client.campaign.state.phase == P.MORNING and _same_state(), "the partner's farm is the host's (the welcome)") r.check(host.avatars[0].body._tag.text == "P1 Ana" and host.avatars[1].body._tag.text == "P2 Ben" and client.avatars[0].body._tag.text == "P1 Ana" and client.avatars[1].body._tag.text == "P2 Ben", "name tags over both") r.check(client.campaign.save_path == "" and client.audio.remote_cues and host.audio.forward.is_valid(), "only the host saves; sounds come from the host") func _requests() -> void: client.review_panel._start.pressed.emit() _pump(2) r.check(host.campaign.state.phase == P.DAY and client.campaign.state.phase == P.DAY, "the partner can start the day (the host does it)") host.campaign.state.resources.shed = {"wood": 40, "scrap": 30, "straw": 30} # #13: the 3 m walkway between the two new crop fields (x -1..2, z 2..12) -- open ground, clear # of both fields' crop rows -- stands in for the once-empty spot this used to build in. client.player.global_position = Vector3(0.5, 0.0, 6.0) _pump(4) var before := host.structures.count() client.build.place_request.call("fence", 0.5, 8.0, 0) _pump(20) r.check(host.structures.count() == before + 1 and client.structures.count() == before + 1 and _same_state(), "the partner builds a fence: the host checks it, builds it, and both farms show it") client.build.place_request.call("fence", 30.0, -30.0, 0) _pump(20) r.check(host.structures.count() == before + 1, "a placement out of the partner's reach is refused by the host") r.check(not host.upgrade_panel._rows.has("corn_shooter") and host.shed_panel._rows.has("corn_shooter"), "the corn shooter is sold at the supply shed's menu, not the workbench's (also on the partner's replica)") client.shed_panel.buy_request.call("corn_shooter") _pump(20) r.check(Upgrades.owned(host.campaign.state, "corn_shooter") and Upgrades.owned(client.campaign.state, "corn_shooter"), "the partner buys a weapon at the supply shed (the host does it)") ## Standing still by day: the clock runs, nothing else changes. func _quiet_wire() -> void: _pump(30) # the requests' news settles var states := links[0].sent_count("state") var inputs := links[1].sent_count("input") var left := host.campaign.state.phase_time_left _pump(120) r.check(host.campaign.state.phase_time_left < left - 1.5 and links[0].sent_count("state") == states, "the day's clock runs on both farms, but the farm state isn't resent for it") var n := links[1].sent_count("input") - inputs r.check(n >= 7 and n <= 9, "the partner standing still sends its input %d times in 2 s (every %.2f s, not every frame)" % [n, CoopSession.INPUT_KEEPALIVE]) r.check(absf(client.campaign.state.phase_time_left - host.campaign.state.phase_time_left) < 0.2, "and the partner's clock follows the host's (from the snapshots)") func _partner_moves_and_gathers() -> void: var tree: GatherNode for n in host.interactions.nodes: if n.name == "DeadTree": tree = n var at := tree.global_position + Vector3(tree.reach * 0.7, 0.0, 0.0) client.player.global_position = at _pump(10) r.check(host.partner.body.global_position.distance_to(at) < 0.3, "the host's copy of P2 follows the partner's own moves") _empty_pouches() Input.action_press("interact") _pump(roundi(host.config.gather_time * 60.0) - 20) var gathering_there := client.interactions.gathering != null and String(client.interactions.gathering.name) == "DeadTree" and client.player.rooted _pump(40) Input.action_release("interact") _pump(20) r.check(gathering_there, "while P2 gathers, the partner sees it (the channel, rooted) from the host") var res := host.campaign.state.resources r.check(int(res.pouch_of(1).get("wood", 0)) >= 2 and int(client.campaign.state.resources.pouch_of(1).get("wood", 0)) >= 2 and int(host.campaign.state.night_events.get("gathered_wood", 0)) >= 2, "P2 gathered wood into P2's own pouch (%s)" % [res.pouch_of(1)]) r.check(res.pouch_total(0) == 0 and client.campaign.state.resources.pouch_total(0) == 0, "and only P2's: P1's pouch stays empty (%s)" % [res.pouch_of(0)]) r.check(not client.player.rooted, "and is free to move again") await r.process_frame ## The partner's copy of the materials (re-read each time: every farm state update replaces it). func _mine() -> ResourceStore: return client.campaign.state.resources ## Both scarecrows' pouches emptied (every material present, 0). func _empty_pouches() -> void: for slot in ResourceStore.SLOTS: for m in ResourceStore.MATERIALS: host.campaign.state.resources.pouch_of(slot)[m] = 0 ## #16: a pouch per scarecrow, one shared shed. P1 standing in the shed banks only P1's own pouch ## (not what P2 carries); P2 banks its own by walking in; each pays for what it builds or buys ## from its own pouch and the shed, never the other's pouch (the host pays P2's requests from ## P2's); and one Bigger pouch grows both pouches, as does its level 2, which P2's request buys ## once its day comes (#26). The partner's HUD, menus, and build mode go by P2's pouch. func _own_pouches() -> void: var res := host.campaign.state.resources r.check(client.resource_hud != null and client.resource_hud.slot == 1 and client.upgrade_panel.slot == 1 and client.shed_panel.slot == 1 and client.build.kit.slot == 1 and host.resource_hud.slot == 0 and host.upgrade_panel.slot == 0, "the partner's materials readout, menus, and build mode go by P2's pouch; the host's by P1's") var carried := int(res.pouch_of(1).get("wood", 0)) var shed_rect := host.zones.zone(FarmZone.Kind.SUPPLY_SHED).rect() var in_shed := Vector3(shed_rect.get_center().x, 0.0, shed_rect.get_center().y) var shed_wood := int(res.shed.get("wood", 0)) res.pouch_of(0)["straw"] = 1 # P1 carries something of its own var shed_straw := int(res.shed.get("straw", 0)) host.player.global_position = in_shed _pump(4) r.check(carried >= 2 and int(res.pouch_of(1).get("wood", 0)) == carried and int(res.shed.get("wood", 0)) == shed_wood, "P1 standing in the shed doesn't bank P2's pouch (P2 still carries %d wood)" % carried) r.check(res.pouch_total(0) == 0 and int(res.shed.get("straw", 0)) == shed_straw + 1, "it banks P1's own") client.player.global_position = in_shed + Vector3(0.8, 0.0, 0.0) _pump(SETTLE) r.check(res.pouch_total(1) == 0 and int(res.shed.get("wood", 0)) == shed_wood + carried and _mine().pouch_total(1) == 0, "P2 banks its own pouch by walking into the shed (on both farms)") host.player.global_position = AWAY # Building: P2's request is paid from P2's pouch and the shed, never P1's pouch. client.player.global_position = Vector3(0.5, 0.0, 6.0) _pump(12) var fence: Dictionary = host.config.costs["fence"] var wood := int(fence.get("wood", 0)) res.shed = {"wood": 0, "scrap": 0, "straw": 0, "alien_goop": 0} _empty_pouches() res.pouch_of(0)["wood"] = wood + 1 res.pouch_of(1)["wood"] = wood - 1 _pump(6) var before := host.structures.count() client.build.place_request.call("fence", 0.5, 4.0, 0) _pump(20) r.check(host.structures.count() == before and int(res.pouch_of(0)["wood"]) == wood + 1 and int(res.pouch_of(1)["wood"]) == wood - 1, "P2 can't build a fence with P1's wood: P2's pouch and the shed are short, so the host refuses") res.shed["wood"] = 1 _pump(SETTLE) r.check(host.structures.validate("fence", 0.5, 4.0, 0, host.partner.position(), 1) == "" and client.structures.validate("fence", 0.5, 4.0, 0, client.local_avatar.position(), 1) == "", "with 1 wood in the shed P2 can afford it (checked on both farms, P2's pouch)") client.build.place_request.call("fence", 0.5, 4.0, 0) _pump(20) r.check(host.structures.count() == before + 1 and int(res.pouch_of(1)["wood"]) == 0 and int(res.shed["wood"]) == 0 and int(res.pouch_of(0)["wood"]) == wood + 1 and client.structures.count() == before + 1, "P2 builds it, paying from P2's pouch first, then the shed; P1's wood is untouched") # P1 builds the way its build mode does (solo's calls: no slot, so player 1's pouch). host.player.global_position = Vector3(1.5, 0.0, 6.0) r.check(host.structures.validate("fence", 0.5, 2.5, 0, host.avatars[0].position()) == "" and host.structures.place("fence", 0.5, 2.5, 0) > 0 and int(res.pouch_of(0)["wood"]) == 1 and int(res.pouch_of(1)["wood"]) == 0, "and P1 building pays from P1's own pouch") host.player.global_position = AWAY # Buying: one Bigger pouch, bought by P2 at the work area, paid from P2's pouch and the shed; # it grows both pouches. host.structures.grant_workbench() _pump(4) var cost: Dictionary = host.config.costs["pouch_upgrade"] var room := res.pouch_capacity _empty_pouches() res.shed = {"wood": 0, "scrap": 0, "straw": 0, "alien_goop": 0} for m: String in cost: # P2 carries what fits; the shed has the rest var take := mini(int(cost[m]), res.pouch_room(1)) res.pouch_of(1)[m] = take res.shed[m] = int(cost[m]) - take res.pouch_of(0)["scrap"] = 3 # P1's, kept _pump(SETTLE) client.upgrade_panel.refresh() r.check(Upgrades.blocker(client.campaign, "pouch_upgrade", 1) == "" and Upgrades.blocker(client.campaign, "pouch_upgrade", 0) != "" and not (client.upgrade_panel._rows["pouch_upgrade"] as Button).disabled, "the partner's menu offers the Bigger pouch by P2's pouch and the shed (P1's couldn't pay)") client.upgrade_panel.buy_request.call("pouch_upgrade") _pump(SETTLE * 2) r.check(Upgrades.owned(host.campaign.state, "pouch_upgrade") and res.pouch_total(1) == 0 and res.shed_total() == 0 and int(res.pouch_of(0)["scrap"]) == 3, "P2 buys it: paid from P2's pouch and the shed, P1's pouch untouched") r.check(res.pouch_capacity > room and res.pouch_room(0) == res.pouch_capacity - 3 and res.pouch_room(1) == res.pouch_capacity and _mine().pouch_capacity == res.pouch_capacity and _mine().pouch_room(1) == res.pouch_capacity, "one Bigger pouch grows both pouches (%d -> %d units, on both farms)" % [room, res.pouch_capacity]) # Its level 2 (#26): the partner's request buys the next level, once that level's day comes; # the level reaches both farms, and the partner hears the jingle. var day := host.campaign.state.night var l2 := Upgrades.cost(host.config, "pouch_upgrade", 2) var grow := roundi(Upgrades.per_level(host.config.upgrades["pouch_upgrade"]["add"], 2)) room = res.pouch_capacity _empty_pouches() res.shed = {"wood": 0, "scrap": 0, "straw": 0, "alien_goop": 0} res.shed.merge(l2, true) _pump(SETTLE) client.upgrade_panel._buy("pouch_upgrade") _pump(SETTLE * 2) r.check(Upgrades.level(host.campaign.state, "pouch_upgrade") == 1 and res.shed == l2.merged({"wood": 0, "scrap": 0, "straw": 0, "alien_goop": 0}), "the partner can't buy level 2 before day 8 (the host refuses: '%s')" % Upgrades.blocker(host.campaign, "pouch_upgrade", 1)) host.campaign.state.night = 8 # (put back below, before any night) _pump(SETTLE) client.upgrade_panel.refresh() var row := client.upgrade_panel._rows["pouch_upgrade"] as Button r.check(not row.disabled and row.text.begins_with("Bigger pouch II —") and row.text.contains(ResourceStore.describe(l2)), "from day 8 the partner's menu offers Bigger pouch II for %s ('%s')" % [ResourceStore.describe(l2), row.text]) var jingles := int(client.audio.played.get("upgrade", 0)) client.upgrade_panel._buy("pouch_upgrade") _pump(SETTLE * 2) r.check(Upgrades.level(host.campaign.state, "pouch_upgrade") == 2 and Upgrades.level(client.campaign.state, "pouch_upgrade") == 2 and res.shed_total() == 0 and res.pouch_capacity == room + grow and _mine().pouch_capacity == res.pouch_capacity, "P2 buys level 2: level 2 on both farms, the pouches %d -> %d units" % [room, res.pouch_capacity]) r.check(int(client.audio.played.get("upgrade", 0)) == jingles + 1, "with the purchase's jingle on the partner's side") host.campaign.state.night = day _empty_pouches() _pump(6) ## #22: the partner's shops. P2 at the supply shed's counter by day sees its prompt, and interact ## opens the shed's menu on the partner's farm (the host asks for it), never on the host's; the ## host sees P2's menu up. Interact again closes it. A press that reaches the host while its copy ## of P2 is still walking up to the counter there isn't lost: it opens the menu once P2 is in reach ## (but a press long before doesn't open it later). The same at the work area's bench. A weapon ## P2 buys at the shed is paid from P2's pouch and the shed, and goes into P2's hand on both farms. func _shops() -> void: var res := host.campaign.state.resources var counter := client.interactions.shed_counter var bench := client.interactions.workbench r.check(host.campaign.is_daytime() and counter != null and bench != null and host.structures.has_workbench(), "(by day, with the shed's counter and the granted work area on both farms)") host.player.global_position = AWAY var at_counter := counter.global_position + Vector3(0.0, 0.0, 1.2) client.player.global_position = at_counter _pump(20) client.interactions._process(0.0) r.check(host.partner.interactions.focus is ShedCounter and client.interactions.focus == counter and counter._label.visible and counter._label.text.begins_with("[E / X] Shop"), "the partner at the shed's counter sees its prompt ('%s')" % counter._label.text.get_slice("\n", 0)) _tap_interact() r.check(client.shed_panel.is_open and not host.shed_panel.is_open and host.partner.remote.ui_open, "interact there opens the shed's menu on the partner's farm, not the host's (the host sees P2's menu up)") _tap_interact() r.check(not client.shed_panel.is_open and not host.partner.remote.ui_open and not host.shed_panel.is_open, "interact again closes it on the partner's farm (and the press doesn't open it again)") # Lag: the press reaches the host along with P2's arrival at the counter, while the host's copy # of P2 is still on its way there (out of reach for a few ticks). var off := counter.global_position + Vector3(0.0, 0.0, 4.5) client.player.global_position = off _pump(20) client.player.global_position = at_counter var taken: int = host.partner.remote._seen[3] # (interact) var taken_at := -1.0 # how far the host's copy of P2 was from the counter when it took the press Input.action_press("interact") for i in 8: _pump() # (the copy moves, then the host's hands take the press) if taken_at < 0.0 and host.partner.remote._seen[3] != taken: var hp := host.partner.body.global_position taken_at = Vector2(hp.x - counter.global_position.x, hp.z - counter.global_position.z).length() Input.action_release("interact") _pump(20) r.check(taken_at > counter.reach and client.shed_panel.is_open and host.partner.remote.ui_open, "a press the host takes while its copy of P2 is still %.1f m from the counter opens the menu once the copy is in reach" % taken_at) client.shed_panel.close() _pump(10) client.player.global_position = off _pump(20) _tap_interact() # well out of reach: nothing to open _pump(30) client.player.global_position = at_counter _pump(30) r.check(not client.shed_panel.is_open and not host.partner.remote.ui_open, "but a press long before P2 got there doesn't open it later") # The work area's bench. var at_bench := bench.global_position + (bench.global_position - host.zones.zone(FarmZone.Kind.WORK_AREA).global_position).normalized() * 1.2 client.player.global_position = at_bench _pump(20) client.interactions._process(0.0) r.check(host.partner.interactions.focus is Workbench and client.interactions.focus == bench and bench._label.text.begins_with("[E / X] Upgrades"), "the partner at the work area's bench sees its prompt ('%s')" % bench._label.text.get_slice("\n", 0)) _tap_interact() r.check(client.upgrade_panel.is_open and not host.upgrade_panel.is_open and host.partner.remote.ui_open, "interact there opens the work area's menu on the partner's farm, not the host's") _tap_interact() r.check(not client.upgrade_panel.is_open and not host.partner.remote.ui_open, "and interact again closes it") # Buying a weapon at the shed: P2's pouch, then the shed; into P2's hand. var id := "shurikens" var w := ScarecrowCombat.WEAPON_IDS.find(id) var cost: Dictionary = host.config.costs[id] var day := host.campaign.state.night host.campaign.state.night = Upgrades.from_day(host.config, id) # in stock (#26; put back below) client.player.global_position = at_counter _pump(20) _tap_interact() _empty_pouches() # (P2 banked its pouch walking into the shed: fill it again with the menu up) res.shed = {"wood": 0, "scrap": 0, "straw": 0, "alien_goop": 0} for m: String in cost: # P2 carries all but one of each; the shed has the rest res.pouch_of(1)[m] = int(cost[m]) - 1 res.shed[m] = 1 res.pouch_of(0)["scrap"] = 3 # P1's, kept _pump(SETTLE) var jingles := int(client.audio.played.get("upgrade", 0)) r.check(client.shed_panel.is_open and not (client.shed_panel._rows[id] as Button).disabled and not Upgrades.owned(host.campaign.state, id), "the partner's shed menu offers the %s (P2's pouch and the shed can pay)" % id) client.shed_panel._buy(id) _pump(SETTLE * 2) r.check(Upgrades.owned(host.campaign.state, id) and res.pouch_total(1) == 0 and res.shed_total() == 0 and int(res.pouch_of(0)["scrap"]) == 3, "P2 buys the %s: paid from P2's pouch and the shed, P1's pouch untouched (P1 %s)" % [id, res.pouch_of(0)]) r.check(client.combat.weapon == w and host.partner.combat.weapon == w and host.combat.weapon != w, "and it goes into P2's hand on both farms (P1's hand unchanged)") r.check(int(client.audio.played.get("upgrade", 0)) == jingles + 1, "with the purchase's jingle on the partner's side") client.shed_panel.close() host.campaign.state.upgrades.erase(id) host.campaign.state.night = day _empty_pouches() client.player.global_position = Vector3(0.5, 0.0, 6.0) _pump(SETTLE) r.check(client.combat.weapon == ScarecrowCombat.Weapon.PITCHFORK, "(back to the pitchfork)") ## The partner taps interact: held for a few ticks, then let go, and the news settles. func _tap_interact() -> void: Input.action_press("interact") _pump(4) Input.action_release("interact") _pump(20) func _night() -> void: host.campaign.advance_phase() host.campaign.advance_phase() _pump(2) r.check(host.campaign.state.phase == P.NIGHT and client.campaign.state.phase == P.NIGHT, "night falls on both farms") # Far from P1 (waiting at AWAY, on the farm's edge): where the north gaps were. host.spawn_enemy(EnemySystem.TYPE_CROW, LootSystem.ANY, GameTestRunner.SKY["north"]) host.spawn_enemy(EnemySystem.TYPE_THIEF, LootSystem.ANY, GameTestRunner.EDGE["north"]) host.spawn_enemy(EnemySystem.TYPE_BREAKER, LootSystem.ANY, GameTestRunner.EDGE["driveway"]) _pump(30) var types_host := _types(host.enemies) var types_client := _types(client.enemies) r.check(host.enemies.alive_count() == 3 and types_client == types_host, "the enemies reach the partner's farm, by type (%s)" % [types_client]) r.check(_enemies_match(0.6), "where the host has them (within 0.6 m)") r.check(client.characters.in_use() == 2, "the partner draws the ground enemies with characters") # A crow steals: the loot follows on the partner's farm. var stolen := false for i in 1800: _pump() if host.loot.count(LootSystem.Status.CARRIED) > 0: stolen = true break _pump(8) var carried := -1 for id in range(1, host.loot.item_count + 1): if host.loot.status[id] == LootSystem.Status.CARRIED: carried = id r.check(stolen and carried > 0 and client.loot.status[carried] == LootSystem.Status.CARRIED and client.enemies.resolve_handle(client.loot.holder[carried]) >= 0, "a carried crop is carried on both farms, by the same enemy") var killed := host.enemies.alive_count() for d in host.enemies.active_count: host.enemies.apply_hit(d, 99) _pump(6) r.check(killed > 0 and host.enemies.alive_count() == 0 and client.enemies.alive_count() == 0, "killed on the host, gone on the partner's farm") func _partner_shoots() -> void: var fired := host.partner.combat.shots_fired var heard := int(client.audio.played.get("shot", 0)) r.check(client.combat.equip(ScarecrowCombat.Weapon.CORN_SHOOTER), "the partner takes the corn shooter (bought for both)") client.player.global_position = Vector3(8.0, 0.0, -10.0) # open ground, nothing east of it client.combat.device = ScarecrowCombat.Device.GAMEPAD # keeps the aim it's given _pump(10) r.check(host.partner.combat.weapon == ScarecrowCombat.Weapon.CORN_SHOOTER and host.avatars[0].combat.weapon == ScarecrowCombat.Weapon.PITCHFORK, "the host sees P2 holding it, and P1 still has the pitchfork") client.combat.aim = Vector2(1.0, 0.0) Input.action_press("fire") _pump(30) r.check(client.projectiles.active_count > 0, "shots in flight show on the partner's farm (the host's, %d)" % client.projectiles.active_count) Input.action_release("fire") _pump(10) r.check(host.partner.combat.shots_fired > fired, "the partner holds fire: P2's gun fires on the host (%d shots)" % (host.partner.combat.shots_fired - fired)) r.check(int(client.audio.played.get("shot", 0)) > heard, "and the partner hears the shots") r.check(client.projectiles.fired == 0, "(the partner's own gun only aims: it fired nothing itself)") ## #16: an Alien's goop goes to its killer's pouch (here P2's corn shot, with P1 standing nearer), ## and a kill no scarecrow made itself (a pumpkin bomb's or a turret's) to the nearest scarecrow. func _goop_to_killer() -> void: var cfg := host.config var chance := cfg.alien_goop_drop_chance cfg.alien_goop_drop_chance = 1.0 var res := host.campaign.state.resources _empty_pouches() for i in 240: # P2's last shots fly out if host.projectiles.active_count == 0: break _pump() client.player.global_position = Vector3(8.0, 0.0, -10.0) client.combat.aim = Vector2(1.0, 0.0) _pump(6) Input.action_press("fire") var shot := PackedFloat32Array() for i in 30: _pump() shot = host.projectiles.replica_record(1) if shot.size() == 4: break Input.action_release("fire") var es := host.enemies var h := -1 var p1_nearer := false if shot.size() == 4: # An Alien just ahead of P2's shot, P1 beside it (nearer than P2, out of the shot's way). var ahead := Vector2(shot[0], shot[1]) + Vector2(shot[2], shot[3]) * 1.5 host.player.global_position = Vector3(ahead.x + 0.5, 0.0, ahead.y - 1.5) p1_nearer = host.avatars[0].position().distance_to(ahead) < host.partner.position().distance_to(ahead) h = es.spawn(EnemySystem.TYPE_THIEF, ahead.x, ahead.y, Vector2.UP) es.health[es.resolve_handle(h)] = 1 for i in 20: _pump() if es.resolve_handle(h) < 0: break r.check(h >= 0 and es.resolve_handle(h) < 0 and int(res.pouch_of(1).get("alien_goop", 0)) == cfg.alien_goop_drop_qty and int(res.pouch_of(0).get("alien_goop", 0)) == 0 and p1_nearer, "P2's shot kills an Alien: its goop goes into P2's pouch, not P1's (nearer) (P2 %s, P1 %s)" % [res.pouch_of(1), res.pouch_of(0)]) _pump(SETTLE) r.check(int(_mine().pouch_of(1).get("alien_goop", 0)) == cfg.alien_goop_drop_qty, "(on both farms)") _empty_pouches() for who in [0, 1]: var a := host.avatars[who] var at := a.position() + Vector2(0.6, 0.0) var hh := es.spawn(EnemySystem.TYPE_THIEF, at.x, at.y, Vector2.UP) es.apply_hit_handle(hh, 99) # as a pumpkin bomb's burst or a turret's shot: no scarecrow's own hit _pump() r.check(int(res.pouch_of(0).get("alien_goop", 0)) == cfg.alien_goop_drop_qty and int(res.pouch_of(1).get("alien_goop", 0)) == cfg.alien_goop_drop_qty, "a bomb's or a turret's kill goes to the scarecrow nearest to it (one each)") cfg.alien_goop_drop_chance = chance _empty_pouches() host.player.global_position = AWAY _pump(4) func _weapons_shown() -> void: var p1 := client.avatars[0].combat # the host's scarecrow, on the partner's farm p1.models._attack_t = -1.0 host.combat.equip(ScarecrowCombat.Weapon.PITCHFORK) host.combat.reset() host.combat.attack_toward(Vector2(host.player.global_position.x + 3.0, host.player.global_position.z)) _pump(3) r.check(p1.weapon == ScarecrowCombat.Weapon.PITCHFORK and p1.models._attack_t >= 0.0, "P1's pitchfork stab shows on the partner's farm") host.campaign.state.upgrades["pumpkin_bomb"] = 1 host.combat.equip(ScarecrowCombat.Weapon.PUMPKIN_BOMB) host.combat.ready_for_morning() var bursts := client.bombs.bursts host.combat.offhand_toward(Vector2(host.player.global_position.x + 6.0, host.player.global_position.z)) _pump(3) r.check(p1.weapon == ScarecrowCombat.Weapon.PITCHFORK and client.bombs.flying() == 1, "P1's pumpkin bomb flies on the partner's farm too (from the off-hand: the pitchfork stays in hand there)") _pump(90) r.check(client.bombs.bursts == bursts + 1 and client.bombs.enemies_hit == 0 and host.bombs.flying() == 0, "and bursts there, harmless (the host's burst does the damage)") r.check(client.combat.offhand == ScarecrowCombat.Weapon.PUMPKIN_BOMB and host.partner.combat.offhand == ScarecrowCombat.Weapon.PUMPKIN_BOMB, "the partner has bombs in its off-hand too (bought for both)") var thrown := host.partner.combat.bombs_thrown var attacks := host.partner.combat.attacks() Input.action_press("offhand") _pump(10) Input.action_release("offhand") _pump(4) r.check(host.partner.combat.bombs_thrown == thrown + 1 and host.partner.combat.attacks() == attacks + 1 and client.combat.bomb_left > 0.0, "the partner's right click throws on the host (and nothing else), and its recharge shows on the partner's HUD") _pump(roundi(host.config.bomb_cooldown * 60.0) + 10) client.build.set_active(true) _pump(4) thrown = host.partner.combat.bombs_thrown Input.action_press("cancel") # the right mouse button: both actions Input.action_press("offhand") _pump(10) var left := not client.build.active Input.action_release("cancel") Input.action_release("offhand") _pump(4) r.check(left and host.partner.combat.bombs_thrown == thrown, "the partner's right click that leaves build mode throws nothing on the host, even held") _pump(90) for c: ScarecrowCombat in [host.combat, client.combat]: c.equip(ScarecrowCombat.Weapon.PITCHFORK) host.campaign.state.upgrades.erase("pumpkin_bomb") _pump(4) func _fall_rule() -> void: host.partner.kit.invulnerable_left = 0.0 host.partner.kit.try_damage(99) _pump(6) r.check(not host.partner.kit.alive and host.campaign.state.phase == P.NIGHT and not host.campaign.is_over(), "in co-op one scarecrow down is out until morning, not Game Over") r.check(not client.scarecrow.alive and not client.player.alive, "the partner sees their own scarecrow fall") client._process(0.0) r.check((client.camera as FollowCamera).target == client.avatars[0].body, "and their camera watches P1 meanwhile") host.scarecrow.invulnerable_left = 0.0 host.scarecrow.try_damage(99) _pump(6) r.check(host.campaign.outcome == CampaignController.Outcome.GAME_OVER_SCARECROW and client.campaign.outcome == host.campaign.outcome and client.campaign.state.phase == P.GAME_OVER, "both down: Game Over on both farms") client.end_panel._process(EndPanel.DELAY + 0.1) r.check(client.end_panel.mode == "failure", "with the failure screen on the partner's too") client.end_panel._retry.pressed.emit() _pump(4) r.check(host.campaign.state.phase == P.MORNING and client.campaign.state.phase == P.MORNING and host.scarecrow.alive and host.partner.kit.alive and client.scarecrow.alive and host.campaign.state.coop, "Retry from the partner: both back at the morning") func _crowd() -> void: host.campaign.begin_day() host.campaign.advance_phase() host.campaign.advance_phase() for i in 90: host.spawn_enemy([EnemySystem.TYPE_CROW, EnemySystem.TYPE_THIEF, EnemySystem.TYPE_BREAKER][i % 3]) # The drop pattern starts here: the loopback counts every message sent so far, so without this # the earlier cases' traffic would decide which records are lost (a strictly periodic drop can # hit all three copies of an enemy's record). (links[0] as CoopLink.Loopback)._count = 0 links[0].drop_every = 3 # a third of the unreliable messages lost _pump(60) r.check(client.enemies.alive_count() == host.enemies.alive_count() and host.enemies.alive_count() > 40, "a crowd of %d enemies, with a third of the snapshots lost, all show" % host.enemies.alive_count()) r.check(_enemies_match(1.0), "close to where the host has them") r.check(links[0].oversized == 0 and links[1].oversized == 0 and links[0].largest_unreliable <= CoopLink.UNRELIABLE_LIMIT, "no unreliable message is too big for a datagram (largest %d bytes)" % links[0].largest_unreliable) r.info("co-op traffic: host sent %d messages, %.1f KB in %d ticks; partner %d messages" % [links[0].sent_messages, links[0].sent_bytes / 1024.0, hs.ticks, links[1].sent_messages]) links[0].drop_every = 0 # Dead reckoning: two seconds of the crowd, against every enemy in every snapshot. var records := hs.foe_records var bytes := links[0].sent_bytes var partner_bytes := links[1].sent_bytes var snaps := links[0].sent_count("snap") var crowd := host.enemies.alive_count() _pump(120) var every := crowd * (links[0].sent_count("snap") - snaps) r.check(hs.foe_records - records < every / 2, "the host sends %d enemy records in 2 s, not %d (only the ones the partner's guess drifted from)" % [hs.foe_records - records, every]) r.check(_enemies_match(1.0), "and the partner's crowd still matches the host's") r.info("crowd of %d: host %.1f KB/s, partner %.1f KB/s (before GD-Sync's wrapping)" % [crowd, (links[0].sent_bytes - bytes) / 2048.0, (links[1].sent_bytes - partner_bytes) / 2048.0]) func _leaving() -> void: var gone: Array[String] = [] cs.ended.connect(func(why: String) -> void: gone.append(why)) var res := host.campaign.state.resources _empty_pouches() res.pouch_of(1)["alien_goop"] = 2 # what P2 carries when they go (goop: no thief goes for it here) var goop := int(res.shed.get("alien_goop", 0)) links[1].close() # the partner quits _pump(2) r.check(not hs.is_active() and host.partner == null and host.avatars.size() == 1 and not host.campaign.state.coop and not is_instance_valid(host.get_parent().get_node_or_null("Player2")) or host.get_parent().get_node("Player2").is_queued_for_deletion(), "the partner leaves: the host plays on alone") r.check(res.pouch_total(1) == 0 and int(res.shed.get("alien_goop", 0)) == goop + 2, "what the partner carried is put in the shed (%s)" % [res.shed]) r.check(not cs.is_active() and gone.size() == 1, "and the partner's game is told the session is over ('%s')" % (gone[0] if not gone.is_empty() else "")) host.campaign.state.coop = true # as a co-op morning's save would have it host.campaign.retry_night() r.check(not host.campaign.state.coop, "a co-op morning played alone goes back to the solo rules") var snap := host.campaign.morning_snapshot() snap["coop"] = true snap["resources"]["partner_pouch"]["wood"] = 3 # the save of a morning P2 carried something var shed_wood := int(snap["resources"]["shed"]["wood"]) host.campaign._morning_snapshot = snap host.campaign.retry_night() res = host.campaign.state.resources r.check(res.pouch_total(1) == 0 and int(res.shed["wood"]) == shed_wood + 3, "and P2's pouch from that morning goes into the shed, so nothing is stranded") ## Voice chat's sending, without GD-Sync: the gate, the pieces, and the codec. func _voice() -> void: var v := CoopVoice.new() var mix := 48000.0 var hum := _frames(mix, 1.0, func(t: float) -> float: return 0.003 * sin(TAU * 50.0 * t)) r.check(v.feed(hum, mix).is_empty(), "voice: a quiet microphone (room hum) sends nothing") var click := _frames(mix, 0.03, func(t: float) -> float: return 0.6 if int(t * 4000.0) % 2 == 0 else -0.6) var sent := v.feed(click, mix) sent.append_array(v.feed(hum, mix)) r.check(sent.is_empty(), "a click alone doesn't open the gate") var words := v.feed(_frames(mix, 0.6, func(t: float) -> float: return 0.2 * sin(TAU * 220.0 * t)), mix) words.append_array(v.feed(hum, mix)) var size := 3 + CoopVoice.CHUNK / 2 var all_sized := true for piece in words: all_sized = all_sized and piece.size() == size r.check(words.size() >= 12 and words.size() <= 22 and all_sized, "speech opens it: %d pieces of %d bytes (0.6 s of voice, the piece before, and the hold)" % [words.size(), size]) r.check(v.feed(hum, mix).is_empty(), "and the gate shuts again after the hold") var kbs := size * float(CoopVoice.RATE) / CoopVoice.CHUNK / 1024.0 r.check(kbs < 7.0, "talking costs %.1f KB/s before GD-Sync's wrapping (GD-Sync's raw VoiceChat: about 47)" % kbs) v.free() var tone := PackedFloat32Array() for i in 2 * CoopVoice.CHUNK: tone.append(0.25 * sin(TAU * 300.0 * i / CoopVoice.RATE) + 0.1 * sin(TAU * 1100.0 * i / CoopVoice.RATE)) var state := [0, 0] var first := CoopVoice.encode(tone.slice(0, CoopVoice.CHUNK), state) var second := CoopVoice.encode(tone.slice(CoopVoice.CHUNK), state) var snr_first := _snr(tone.slice(0, CoopVoice.CHUNK), CoopVoice.decode(first)) var snr_second := _snr(tone.slice(CoopVoice.CHUNK), CoopVoice.decode(second)) r.check(snr_first > 15.0 and snr_second > 25.0, "ADPCM keeps the sound: %.0f dB signal to noise from a cold start (a few samples to adapt), %.0f dB running" % [snr_first, snr_second]) r.check(CoopVoice.decode(second).size() == CoopVoice.CHUNK, "and a piece decodes on its own (a lost one before it costs nothing more)") ## Stereo microphone frames: seconds of f(t) at mix Hz. static func _frames(mix: float, seconds: float, f: Callable) -> PackedVector2Array: var out := PackedVector2Array() out.resize(roundi(mix * seconds)) for i in out.size(): var s: float = f.call(i / mix) out[i] = Vector2(s, s) return out static func _snr(a: PackedFloat32Array, b: PackedFloat32Array) -> float: var sig := 0.0 var noise := 0.0 for i in mini(a.size(), b.size()): sig += a[i] * a[i] noise += (a[i] - b[i]) * (a[i] - b[i]) return 10.0 * log(sig / maxf(noise, 1e-12)) / log(10.0) func _types(es: EnemySystem) -> Dictionary: var out := {} for d in es.active_count: if es.alive[d] == 1: out[es.type[d]] = int(out.get(es.type[d], 0)) + 1 return out ## Every host enemy has a partner-side copy within tolerance meters. func _enemies_match(tolerance: float) -> bool: var es := host.enemies for d in es.active_count: if es.alive[d] == 0: continue var h := cs._local_handle(es.handle_of(d)) var c := client.enemies.resolve_handle(h) if c < 0: return false if Vector2(es.pos_x[d] - client.enemies.pos_x[c], es.pos_z[d] - client.enemies.pos_z[c]).length() > tolerance: return false return true