extends RefCounted ## The scarecrow's three weapons on the real farm (FarmSim stepped by hand at 60 Hz): only the ## pitchfork to start; its stab (in reach and in the arc of the aim only, the nearest few, its ## damage, once per interval, again and again while fire is held, not through a wall); the corn ## shooter and pumpkin bombs bought at the workbench (rapid fire and heavy seeds need the corn ## shooter first; a weapon bought goes into the hand); switching (Tab / 1-3, owned ones only, not ## in build mode; one no longer owned falls back to the pitchfork); a bomb's throw (at the aim, ## its range clamped), flight, and burst (everything in its radius, crows too, nothing beyond, ## not the scarecrow), its recharge and the morning refill; the models on the scarecrow; and the ## HUD's weapon panel. const DT := 1.0 / 60.0 const AT := Vector2(0.0, 6.0) # open ground south-west of the pen const W := ScarecrowCombat.Weapon const THIEF := EnemySystem.TYPE_THIEF func run(r: GameTestRunner) -> void: var farm := (load("res://game/scenes/farm.tscn") as PackedScene).instantiate() as Node3D r.root.add_child(farm) await r.process_frame await r.physics_frame var sim := farm.get_node("FarmSim") as FarmSim sim.set_physics_process(false) sim.waves.enabled = false var player := farm.get_node("Player") as PlayerCharacter player.global_position = Vector3(AT.x, 0.0, AT.y) var keep := sim.enemies.behavior sim.enemies.behavior = null # enemies stand still _start(r, sim, player) _pitchfork(r, sim) await _wall(r, sim, farm) _unlocks(r, farm, sim) _switching(r, sim) _bombs(r, sim) _hud(r, farm, sim) sim.enemies.behavior = keep farm.queue_free() await r.process_frame func _start(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter) -> void: var c := sim.combat r.check(c.weapon == W.PITCHFORK and c.owns(W.PITCHFORK) and not c.owns(W.CORN_SHOOTER) and not c.owns(W.PUMPKIN_BOMB), "a new vacation: the pitchfork in hand, and nothing else") var pivots := c.models._pivots r.check(pivots.size() == 3 and pivots[0].visible and not pivots[1].visible and not pivots[2].visible, "only the pitchfork shows on the scarecrow") var near := true for p in pivots: near = near and p.global_position.distance_to(player.global_position) < 3.0 r.check(near, "the models sit on the scarecrow (their exports were 1.8 km off the origin)") func _pitchfork(r: GameTestRunner, sim: FarmSim) -> void: var c := sim.combat var cfg := sim.config var es := sim.enemies var front := es.spawn(THIEF, AT.x + 1.2, AT.y, Vector2.LEFT) var front2 := es.spawn(THIEF, AT.x + 1.7, AT.y + 0.7, Vector2.LEFT) var behind := es.spawn(THIEF, AT.x - 1.2, AT.y, Vector2.RIGHT) var far := es.spawn(THIEF, AT.x + cfg.pitchfork_reach + 1.5, AT.y, Vector2.LEFT) _step(sim, 1) var full := es.t_hp[THIEF] r.check(c.attack_toward(AT + Vector2(5.0, 0.0)) and c.stabs == 1, "a stab to the east") r.check(_hp(es, front) == full - cfg.pitchfork_damage and _hp(es, front2) == full - cfg.pitchfork_damage, "both thieves in front, in reach, take %d" % cfg.pitchfork_damage) r.check(_hp(es, behind) == full and _hp(es, far) == full, "the one behind and the one out of reach don't") r.check(not c.attack_toward(AT + Vector2(5.0, 0.0)), "one stab per %.1f s: not again at once" % cfg.pitchfork_interval) _step(sim, c.stab_ticks()) r.check(c.attack_toward(AT + Vector2(5.0, 0.0)), "and again after it") _step(sim, 1) r.check(es.resolve_handle(front) < 0 and es.resolve_handle(front2) < 0, "two stabs stop a thief") _clear(sim) # The nearest few only, crows too. var crowd: Array[int] = [] for k in 5: crowd.append(es.spawn(THIEF, AT.x + 0.9 + k * 0.28, AT.y + (0.5 if k % 2 == 0 else -0.5), Vector2.LEFT)) _step(sim, 1) _step(sim, c.stab_ticks()) c.attack_toward(AT + Vector2(5.0, 0.0)) var hit := crowd.filter(func(h: int) -> bool: return _hp(es, h) < full).size() r.check(hit == cfg.pitchfork_targets, "a stab hits the nearest %d of a crowd (%d)" % [cfg.pitchfork_targets, hit]) _clear(sim) var crow := es.spawn(EnemySystem.TYPE_CROW, AT.x + 1.5, AT.y, Vector2.LEFT) _step(sim, c.stab_ticks()) c.attack_toward(AT + Vector2(5.0, 0.0)) _step(sim, 1) r.check(es.resolve_handle(crow) < 0, "a crow in reach is stabbed out of the air") # Held fire stabs again and again. c.reset() _step(sim, 1) var before := c.stabs Input.action_press("fire") _step(sim, c.stab_ticks() * 2) Input.action_release("fire") _step(sim, 1) r.check(c.stabs - before == 2, "holding fire stabs every %.1f s (%d in %.1f s)" % [cfg.pitchfork_interval, c.stabs - before, cfg.pitchfork_interval * 2.0]) func _wall(r: GameTestRunner, sim: FarmSim, farm: Node3D) -> void: var c := sim.combat var es := sim.enemies var h := es.spawn(THIEF, AT.x + 1.5, AT.y, Vector2.LEFT) var wall := StaticBody3D.new() var shape := CollisionShape3D.new() var box := BoxShape3D.new() box.size = Vector3(0.2, 3.0, 2.0) shape.shape = box wall.add_child(shape) farm.add_child(wall) wall.global_position = Vector3(AT.x + 0.75, 1.5, AT.y) await r.physics_frame # it joins the physics space on the next physics frame _step(sim, c.stab_ticks()) c.attack_toward(AT + Vector2(5.0, 0.0)) r.check(_hp(es, h) == es.t_hp[THIEF], "no stab through a wall") wall.queue_free() await r.physics_frame _clear(sim) func _unlocks(r: GameTestRunner, farm: Node3D, sim: FarmSim) -> void: var c := sim.campaign var s := c.state var combat := sim.combat var st := sim.structures c.begin_day() s.resources.shed = {"wood": 99, "scrap": 99, "straw": 99} r.check(not combat.equip(W.CORN_SHOOTER) and combat.weapon == W.PITCHFORK, "the corn shooter can't be taken up before it's bought") r.check(Upgrades.blocker(c, "rapid_fire") == "Needs the corn shooter first" and Upgrades.buy(c, "heavy_seeds", st).contains("needs the corn shooter") and not Upgrades.owned(s, "heavy_seeds"), "rapid fire and heavy seeds need the corn shooter first") r.check(Upgrades.buy(c, "corn_shooter", st).contains("done:") and combat.owns(W.CORN_SHOOTER) and Upgrades.blocker(c, "rapid_fire") == "", "the corn shooter is bought at the workbench; then rapid fire can be") var panel := farm.get_node("Hud/UpgradePanel") as UpgradePanel panel._buy("pumpkin_bomb") r.check(combat.owns(W.PUMPKIN_BOMB) and combat.weapon == W.PUMPKIN_BOMB, "a weapon bought from the menu goes into the hand") r.check(combat.models._pivots[2].visible and not combat.models._pivots[0].visible, "and shows on the scarecrow") func _switching(r: GameTestRunner, sim: FarmSim) -> void: var c := sim.combat var s := sim.campaign.state c.equip(W.PITCHFORK) var seen: Array[int] = [] for k in 3: _tap(sim, "weapon_next") seen.append(c.weapon) r.check(seen == [W.CORN_SHOOTER, W.PUMPKIN_BOMB, W.PITCHFORK], "Tab / d-pad down cycles through them: %s" % [seen]) _tap(sim, "weapon_3") var third := c.weapon _tap(sim, "weapon_1") r.check(third == W.PUMPKIN_BOMB and c.weapon == W.PITCHFORK, "1-3 pick one") var east := AT + Vector2(5.0, 0.0) _step(sim, c.stab_ticks()) c.attack_toward(east) c.equip(W.CORN_SHOOTER) var shot := c.attack_toward(east) c.equip(W.PITCHFORK) r.check(shot and not c.attack_toward(east), "each weapon keeps its own wait: swapping doesn't skip the pitchfork's") sim.build.set_active(true) _tap(sim, "weapon_2") _tap(sim, "weapon_next") var building := c.weapon sim.build.set_active(false) r.check(building == W.PITCHFORK, "not in build mode (those keys pick structures there)") s.upgrades.erase("pumpkin_bomb") c.equip(W.CORN_SHOOTER) _tap(sim, "weapon_next") var skipped := c.weapon _tap(sim, "weapon_3") r.check(skipped == W.PITCHFORK and c.weapon == W.PITCHFORK, "only weapons owned: no pumpkin bombs without the upgrade") c.equip(W.CORN_SHOOTER) s.upgrades.erase("corn_shooter") _step(sim, 1) r.check(c.weapon == W.PITCHFORK, "one no longer owned (a retry from before it was bought) goes back to the pitchfork") s.upgrades["corn_shooter"] = 1 s.upgrades["pumpkin_bomb"] = 1 func _bombs(r: GameTestRunner, sim: FarmSim) -> void: var c := sim.combat var cfg := sim.config var es := sim.enemies var kit := sim.scarecrow c.equip(W.PUMPKIN_BOMB) c.ready_for_morning() var target := AT + Vector2(6.0, 0.0) var on := es.spawn(THIEF, target.x, target.y, Vector2.LEFT) var near := es.spawn(THIEF, target.x + cfg.bomb_radius - 0.5, target.y, Vector2.LEFT) var crow := es.spawn(EnemySystem.TYPE_CROW, target.x, target.y + 1.5, Vector2.LEFT) var out := es.spawn(THIEF, target.x + cfg.bomb_radius + 2.0, target.y, Vector2.LEFT) _step(sim, 1) var hp := kit.hp() r.check(c.attack_toward(target) and sim.bombs.flying() == 1 and is_equal_approx(c.bomb_left, cfg.bomb_cooldown), "a pumpkin bomb is thrown at the aim point") r.check(not c.attack_toward(target), "only one every %.0f s" % cfg.bomb_cooldown) r.check(c.models._pivots[2].scale.x < 0.05, "it's gone from the hand") var ticks := 0 while sim.bombs.flying() > 0 and ticks < 200: _step(sim, 1) ticks += 1 _step(sim, 1) r.check(ticks * DT >= PumpkinBombs.MIN_FLIGHT - DT and ticks * DT <= PumpkinBombs.MAX_FLIGHT + DT, "it flies %.2f s" % (ticks * DT)) r.check(es.resolve_handle(on) < 0 and es.resolve_handle(near) < 0 and es.resolve_handle(crow) < 0, "its burst stops everything within %.0f m (%d damage), crows too" % [cfg.bomb_radius, cfg.bomb_damage]) r.check(_hp(es, out) == es.t_hp[THIEF] and kit.hp() == hp and sim.bombs.bursts == 1, "nothing beyond, and not the scarecrow") _clear(sim) c.device = ScarecrowCombat.Device.MOUSE var me := Vector2(sim.player.global_position.x, sim.player.global_position.z) c.ground_point = Vector3(me.x + 30.0, 0.0, me.y) var long := Vector2(c.throw_target().x, c.throw_target().z).distance_to(me) c.ground_point = Vector3(me.x + 0.5, 0.0, me.y) var short := Vector2(c.throw_target().x, c.throw_target().z).distance_to(me) r.check(is_equal_approx(long, cfg.bomb_range) and is_equal_approx(short, cfg.bomb_min_range), "it lands %.0f .. %.0f m away (%.1f, %.1f)" % [cfg.bomb_min_range, cfg.bomb_range, short, long]) _step(sim, roundi(cfg.bomb_cooldown * 60.0) + 2) r.check(c.bomb_left == 0.0 and c.models._pivots[2].scale.x > 0.99, "after %.0f s another one is in hand" % cfg.bomb_cooldown) c.attack_toward(target) c.ready_for_morning() r.check(c.bomb_left == 0.0, "a new morning puts one in hand") sim.bombs.clear() func _hud(r: GameTestRunner, farm: Node3D, sim: FarmSim) -> void: var hud := farm.get_node("Hud/ScarecrowHud") as ScarecrowHud var c := sim.combat var s := sim.campaign.state s.upgrades.erase("corn_shooter") c.equip(W.PUMPKIN_BOMB) c.attack_toward(AT + Vector2(6.0, 0.0)) hud._process(0.0) var rows := hud._weapon_rows.map(func(l: Label) -> String: return l.text) r.check(rows[0] == "1 Pitchfork" and rows[1] == "2 Corn shooter (workbench)" and rows[2] == "3 Pumpkin bombs 4 s", "the HUD lists the weapons: %s" % [rows]) r.check(hud._weapon_title.visible and hud._weapon_hint.text.contains("Tab"), "with how to switch") s.upgrades.erase("pumpkin_bomb") _step(sim, 1) sim.bombs.clear() _step(sim, 1) func _tap(sim: FarmSim, action: String) -> void: Input.action_press(action) _step(sim, 1) Input.action_release(action) _step(sim, 1) func _hp(es: EnemySystem, handle: int) -> int: var d := es.resolve_handle(handle) return es.health[d] if d >= 0 else 0 func _clear(sim: FarmSim) -> void: for d in sim.enemies.active_count: sim.enemies.apply_hit(d, 99) _step(sim, 1) func _step(sim: FarmSim, n: int) -> void: for i in n: sim.step(DT)