extends RefCounted ## The scarecrow's kit on the real farm, with FarmSim stepped by hand at 60 Hz: health and ## invulnerability (several hits on one tick cost one), thief melee (touching, on a cooldown, ## not through cover, not while fleeing), fear (only thieves in range flee, carriers drop their ## crop, thieves mid-steal lose their progress, the cooldown, thieves come back afterwards), ## patching up (heals after the full channel while rooted; release, a hit, or fear cancel it; ## not at full health), sprint (only with the Sprint upgrade: faster while held and moving, 3 s at ## most, then a 10 s recharge; letting go or stopping ends it early; the HUD line), defeat (Game ## Over ahead of a stripped farm, the fall, the end panel and its Retry), the morning revive, and ## the new input actions. const S := LootSystem.Status const T := ThiefBehavior.State const DT := 1.0 / 60.0 const AWAY := Vector3(-11.0, 0.0, 13.0) const SPOT := Vector2(8.0, -10.0) # open ground between the pen, the shed, and the field func run(r: GameTestRunner) -> void: _input_actions(r) 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(SPOT.x, 0.0, SPOT.y) r.check(sim.scarecrow != null and sim.scarecrow.hp() == sim.config.scarecrow_max_hp and sim.scarecrow.fear_left == 0.0, "the scarecrow starts at full health with fear ready") _damage(r, sim) _melee(r, sim) _fear(r, sim) _patch(r, sim, player) await _sprint(r, sim, player, farm.get_node("Hud/ScarecrowHud") as ScarecrowHud) _defeat(r, sim, player, farm.get_node("Hud/EndPanel") as EndPanel) farm.queue_free() await r.process_frame func _input_actions(r: GameTestRunner) -> void: for action in ["fear", "patch", "interact", "build_mode", "rotate_left", "rotate_right", "cancel", "pause", "sprint"]: var keyboard := false var pad := false if InputMap.has_action(action): for e in InputMap.action_get_events(action): keyboard = keyboard or e is InputEventKey or e is InputEventMouseButton pad = pad or e is InputEventJoypadButton r.check(keyboard and pad, "input action '%s' has a keyboard/mouse and a gamepad binding" % action) func _damage(r: GameTestRunner, sim: FarmSim) -> void: var kit := sim.scarecrow var s := sim.campaign.state var hits: Array[int] = [] # lambdas capture ints by value, so count in an array kit.damaged.connect(func(hp: int) -> void: hits.append(hp)) r.check(kit.try_damage(1) and s.scarecrow_hp == 9 and is_equal_approx(kit.invulnerable_left, sim.config.invulnerability_time), "an accepted hit costs 1 HP and starts the invulnerability") r.check(not kit.try_damage(1) and s.scarecrow_hp == 9 and kit.rejected_hits == 1, "hits during invulnerability are rejected") _step(sim, roundi(sim.config.invulnerability_time * 60.0) + 1) var accepted := 0 for i in 3: if kit.try_damage(1): accepted += 1 r.check(accepted == 1 and s.scarecrow_hp == 8 and hits == [9, 8], "three hits on one tick cost one hit") _heal(sim) func _melee(r: GameTestRunner, sim: FarmSim) -> void: var es := sim.enemies var kit := sim.scarecrow var s := sim.campaign.state var h := es.spawn(EnemySystem.TYPE_THIEF, SPOT.x + 0.6, SPOT.y, Vector2.LEFT) _step(sim, 1) r.check(s.scarecrow_hp == s.scarecrow_max_hp - 1 and es.attacks_attempted == 1, "a thief touching the scarecrow hits it") var d := es.resolve_handle(h) r.check(es.attack_cd[d] > 1.0, "the thief's attack cooldown started") _place(es, h, Vector2(SPOT.x + 0.6, SPOT.y)) _step(sim, 30) r.check(s.scarecrow_hp == s.scarecrow_max_hp - 1 and es.attacks_attempted == 1, "no second attack while the cooldown runs") # Fleeing thieves don't fight. _heal(sim) _place(es, h, Vector2(SPOT.x + 0.6, SPOT.y)) d = es.resolve_handle(h) es.attack_cd[d] = 0.0 es.state[d] = T.FLEE es.timer[d] = 5.0 es.grid.rebuild(es.pos_x, es.pos_z, es.active_count) es.evaluate_attacks(SPOT, FarmSim.SCARECROW_RADIUS, kit) r.check(s.scarecrow_hp == s.scarecrow_max_hp, "a fleeing thief doesn't attack") # Cover: a thief on the far side of the south fence, within (an enlarged) reach. es.state[d] = T.SEEK _place(es, h, Vector2(20.25, 15.75)) es.grid.rebuild(es.pos_x, es.pos_z, es.active_count) var blocked := es.attacks_blocked_by_cover es.evaluate_attacks(Vector2(20.25, 14.6), 0.8, kit) r.check(es.attacks_blocked_by_cover == blocked + 1 and s.scarecrow_hp == s.scarecrow_max_hp, "a thief can't hit through the fence") _kill_all(sim) func _fear(r: GameTestRunner, sim: FarmSim) -> void: var es := sim.enemies var loot := sim.loot var kit := sim.scarecrow var near := es.spawn(EnemySystem.TYPE_THIEF, SPOT.x + 3.0, SPOT.y, Vector2.LEFT) var far := es.spawn(EnemySystem.TYPE_THIEF, SPOT.x + 9.0, SPOT.y, Vector2.LEFT) var carrier := es.spawn(EnemySystem.TYPE_THIEF, SPOT.x, SPOT.y + 4.0, Vector2.UP) var stealer := es.spawn(EnemySystem.TYPE_THIEF, SPOT.x - 3.0, SPOT.y - 3.5, Vector2.UP) var cu := _home_unit(loot, -1) loot.holder[cu] = carrier loot.pick_up(cu, carrier) es.carry[es.resolve_handle(carrier)] = cu es.state[es.resolve_handle(carrier)] = T.CARRY var su := _home_unit(loot, cu) loot.holder[su] = stealer loot.set_progress(su, stealer, 0.6) es.target[es.resolve_handle(stealer)] = su es.state[es.resolve_handle(stealer)] = T.STEAL var used: Array[int] = [] kit.fear_used.connect(func(_p: Vector3, n: int) -> void: used.append(n)) Input.action_press("fear") _step(sim, 1) Input.action_release("fear") r.check(used == [3], "the fear action scares the 3 thieves within %.0f m (got %s)" % [sim.config.fear_radius, used]) for h in [near, carrier, stealer]: r.check(es.state[es.resolve_handle(h)] == T.FLEE, "a thief in range flees") r.check(es.state[es.resolve_handle(far)] != T.FLEE, "the thief 9 m away is unaffected") r.check(loot.status[cu] == S.DROPPED and es.carry[es.resolve_handle(carrier)] == -1, "the scared carrier drops its crop") r.check(loot.holder[su] == -1 and loot.progress[su] == 0.0, "the scared thief loses its claim and progress") var d0 := Vector2(es.pos_x[es.resolve_handle(near)], es.pos_z[es.resolve_handle(near)]).distance_to(SPOT) _step(sim, 30) var d1 := Vector2(es.pos_x[es.resolve_handle(near)], es.pos_z[es.resolve_handle(near)]).distance_to(SPOT) r.check(d1 > d0 + 1.0, "fleeing thieves run away from the scarecrow (%.1f -> %.1f m)" % [d0, d1]) r.check(kit.use_fear() == -1 and kit.fear_left > 10.0, "fear can't be used again during its cooldown") _step(sim, roundi(sim.config.fear_duration * 60.0)) r.check(es.state[es.resolve_handle(near)] == T.SEEK and es.state[es.resolve_handle(carrier)] == T.SEEK, "after %.0f s the thieves get back to stealing" % sim.config.fear_duration) _kill_all(sim) var readied: Array[int] = [] kit.fear_ready.connect(func() -> void: readied.append(1)) _step(sim, roundi(sim.config.fear_cooldown * 60.0)) r.check(kit.fear_left == 0.0 and readied.size() == 1 and kit.use_fear() == 0, "fear is ready again after its cooldown") kit.fear_left = 0.0 _step(sim, 1) _return_crops(sim) func _patch(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter) -> void: var kit := sim.scarecrow var s := sim.campaign.state s.resources.shed["straw"] = 40 # patching costs straw (Step 5) kit.step_patch(DT, true) r.check(not kit.patching, "no patching at full health") s.scarecrow_hp = 5 Input.action_press("patch") _step(sim, 30) r.check(kit.patching and player.rooted and not kit.can_shoot(), "patching roots the scarecrow and stops it shooting") _step(sim, roundi(sim.config.patch_time * 60.0) - 30 + 2) # +2: float sums land a tick late r.check(s.scarecrow_hp == 5 + sim.config.patch_heal, "a full %.0f s patch restores %d HP (now %d)" % [sim.config.patch_time, sim.config.patch_heal, s.scarecrow_hp]) r.check(kit.patching, "still holding (and not at full health): the next patch starts") Input.action_release("patch") _step(sim, 1) r.check(not kit.patching and not player.rooted, "letting go frees the scarecrow") s.scarecrow_hp = 5 Input.action_press("patch") _step(sim, 60) Input.action_release("patch") _step(sim, 60) r.check(s.scarecrow_hp == 5 and not kit.patching, "releasing early cancels the patch") Input.action_press("patch") _step(sim, 60) kit.invulnerable_left = 0.0 kit.try_damage(1) r.check(not kit.patching and s.scarecrow_hp == 4, "a hit cancels the patch") _step(sim, 60) r.check(s.scarecrow_hp == 4 and kit.patching, "holding on starts the patch over from the beginning") kit.use_fear() r.check(not kit.patching, "using fear cancels the patch") Input.action_release("patch") _step(sim, 1) kit.fear_left = 0.0 _heal(sim) func _sprint(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter, hud: ScarecrowHud) -> void: var cfg := sim.config var s := sim.campaign.state s.upgrades.erase("sprint") hud._process(0.0) r.check(not player.can_sprint() and not hud.stamina_shown(), "no sprint before the upgrade (and no stamina bar on the HUD)") # On the real body, over physics frames: the open ground south-west of the pen. player.global_position = Vector3(0.0, 0.0, 6.0) player.velocity = Vector3.ZERO Input.action_press("move_right") Input.action_press("sprint") await _frames(r, 30) var speed := Vector2(player.velocity.x, player.velocity.z).length() r.check(not player.sprinting and speed <= player.run_speed + 0.05, "without it, holding sprint just runs (%.1f m/s)" % speed) s.upgrades["sprint"] = 1 await _frames(r, 30) speed = Vector2(player.velocity.x, player.velocity.z).length() r.check(player.sprinting and absf(speed - player.run_speed * cfg.sprint_speed_mult) < 0.1, "with the Sprint upgrade, holding sprint while moving sprints: %.1f m/s (runs at %.1f)" % [speed, player.run_speed]) r.check(player.animation_player.current_animation == player.run_animation and player.animation_player.speed_scale > 1.5, "the run clip plays faster (x%.2f)" % player.animation_player.speed_scale) r.check(player.move_strength() > 1.5, "the partner's machine is told it's sprinting (strength %.2f)" % player.move_strength()) r.check(hud.stamina_shown() and hud.stamina() < 1.0 and is_equal_approx(hud.stamina(), player.sprint_left / cfg.sprint_time), "the HUD's stamina bar appears, and drains while sprinting (%.2f)" % hud.stamina()) Input.action_release("move_right") await _frames(r, 2) r.check(not player.sprinting and player.sprint_recharge > cfg.sprint_cooldown - 0.1, "stopping ends it, and the recharge starts") r.check(hud.stamina() < 0.1, "and starts filling back as it recharges (%.2f)" % hud.stamina()) Input.action_release("sprint") await _frames(r, 30) player.velocity = Vector3.ZERO player.global_position = Vector3(SPOT.x, 0.0, SPOT.y) # The timing, tick by tick. player.reset_sprint() var ticks := 0 player.step_sprint(DT, true, true) while player.sprinting and ticks < 1200: player.step_sprint(DT, true, true) ticks += 1 r.check(absf(ticks * DT - cfg.sprint_time) < DT * 2.0, "held, a sprint lasts %.1f s" % (ticks * DT)) ticks = 0 while not player.sprinting and ticks < 1200: player.step_sprint(DT, true, true) ticks += 1 r.check(absf(ticks * DT - cfg.sprint_cooldown) < DT * 2.0, "then %.1f s to recharge, and still held it starts again" % (ticks * DT)) player.step_sprint(1.0, true, true) player.step_sprint(DT, false, true) r.check(not player.sprinting and is_equal_approx(player.sprint_recharge, cfg.sprint_cooldown), "letting go early ends it, and the full recharge starts") player.step_sprint(DT, true, false) r.check(not player.sprinting, "and it doesn't start standing still") sim.scarecrow.reset_for_morning() r.check(player.sprint_recharge == 0.0 and hud.stamina() == 1.0, "a new morning has it ready (a full bar)") s.upgrades.erase("sprint") func _frames(r: GameTestRunner, n: int) -> void: for i in n: await r.physics_frame func _defeat(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter, panel: EndPanel) -> void: var kit := sim.scarecrow var c := sim.campaign var s := c.state s.scarecrow_hp = 1 s.strip() # the farm is stripped on the same tick the scarecrow falls r.check(kit.try_damage(1) and not kit.alive and not player.alive, "the last HP lost: the scarecrow falls") r.check(player.animation_player.current_animation == player.death_animation, "the death clip plays") sim.step(DT) r.check(c.state.phase == CampaignController.Phase.GAME_OVER and c.outcome == CampaignController.Outcome.GAME_OVER_SCARECROW, "Game Over, reported as the scarecrow's fall ahead of the stripped farm") r.check(not kit.try_damage(1) and kit.use_fear() == -1 and not kit.can_shoot(), "a fallen scarecrow takes no hits and can't act") r.check(not panel.visible, "the end panel waits a moment so the fall can play") panel._process(EndPanel.DELAY + 0.1) r.check(panel.visible and panel._retry.visible and panel._retry.has_focus(), "the end panel shows with Retry focused") panel._retry.pressed.emit() sim.step(DT) r.check(c.state.phase == CampaignController.Phase.MORNING and not panel.visible, "Retry goes back to the morning") r.check(kit.alive and player.alive and kit.fear_left == 0.0 and c.state.scarecrow_hp == c.state.scarecrow_max_hp and c.state.crops == c.state.crops_total, "the scarecrow is back on its feet and the farm as it was that morning") func _heal(sim: FarmSim) -> void: sim.campaign.state.scarecrow_hp = sim.campaign.state.scarecrow_max_hp sim.scarecrow.invulnerable_left = 0.0 func _kill_all(sim: FarmSim) -> void: for d in sim.enemies.active_count: sim.enemies.apply_hit(d, 99) _step(sim, 1) _heal(sim) func _return_crops(sim: FarmSim) -> void: sim.loot.return_dropped() func _place(es: EnemySystem, handle: int, p: Vector2) -> void: var d := es.resolve_handle(handle) es.pos_x[d] = p.x es.pos_z[d] = p.y es.prev_x[d] = p.x es.prev_z[d] = p.y func _home_unit(loot: LootSystem, other: int) -> int: for id in range(1, loot.unit_count + 1): if id != other and loot.status[id] == S.HOME and loot.holder[id] == -1: return id return -1 func _step(sim: FarmSim, n: int) -> void: for i in n: sim.step(DT)