harvest/game/tests/test_scarecrow.gd
Forrest 962c7b0b93 #46: sprint becomes a permanent speed upgrade
Owning the Sprint upgrade now multiplies the scarecrow's speed by
sprint_speed_mult (1.25) all the time, walking and running, on every
platform; the run clip speeds up to match. The hold-to-sprint burst is
gone: sprint_time, sprint_cooldown, the stamina state in PlayerCharacter
(can_sprint is now has_sprint), and the HUD's stamina bar. The upgrade
keeps its id, cost, and name; its text is "Run 25% faster, always".
The sprint input action stays in project.godot, unused.

Tests: test_scarecrow's sprint checks cover 1x without it, x1.25 with it
(run and walk, clip speed, network strength), 30 s with no time limit,
no cooldown, the next morning, and no platform dependence; test_nights
checks it after the retry and a loaded save; test_economy checks the
text matches the multiplier, the menu row, and that an old save's
{"sprint": 1} still owns it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 14:32:39 -04:00

335 lines
16 KiB
GDScript

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 (#46: the upgrade makes it sprint_speed_mult times as fast for
## good, walking and running, with nothing to hold, no time limit, no cooldown, and no stamina
## bar), 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
## A light stick: it walks (under run_threshold even with the Sprint upgrade).
const WALK_STICK := 0.4
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"]:
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)
## Sprint (#46): owning the upgrade makes the scarecrow sprint_speed_mult times as fast for good,
## walking and running alike, on desktop and phones. Nothing is held (the old sprint key does
## nothing), and there's no time limit, no cooldown, and no stamina bar.
func _sprint(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter, hud: ScarecrowHud) -> void:
var cfg := sim.config
var mult := cfg.sprint_speed_mult
var s := sim.campaign.state
var start := Vector3(0.0, 0.0, 6.0) # the open ground south-west of the pen
s.upgrades.erase("sprint")
r.check(not player.has_sprint() and player.speed_factor() == 1.0 and not hud.has_method(&"stamina"),
"no Sprint before the upgrade: 1x speed (and the HUD has no stamina bar)")
# On the real body, over physics frames: a full stick runs, a light one walks.
var old_key := InputMap.has_action("sprint")
if old_key:
Input.action_press("sprint") # the old hold-to-sprint key
var run0: float = await _speed_after(r, player, start, 1.0)
var walk0: float = await _speed_after(r, player, start, WALK_STICK)
if old_key:
Input.action_release("sprint")
r.check(absf(run0 - player.run_speed) < 0.05, "without it, it runs at %.2f m/s, even holding the old sprint key" % run0)
s.upgrades["sprint"] = 1
var run1: float = await _speed_after(r, player, start, 1.0)
r.check(absf(run1 - player.run_speed * mult) < 0.05,
"with the Sprint upgrade it runs x%.2f with nothing held: %.2f m/s (%.2f without)" % [mult, run1, run0])
r.check(player.animation_player.current_animation == player.run_animation and absf(player.animation_player.speed_scale - mult) < 0.02,
"the run clip plays x%.2f to match" % player.animation_player.speed_scale)
r.check(absf(player.move_strength() - mult) < 0.02, "the partner's machine is told it's faster (strength %.2f)" % player.move_strength())
var walk1: float = await _speed_after(r, player, start, WALK_STICK)
r.check(walk0 > 0.5 and absf(walk1 - walk0 * mult) < 0.05 and player.animation_player.current_animation == player.walk_animation,
"walking (a light stick) is x%.2f too: %.2f m/s (%.2f without), on the walk clip" % [walk1 / maxf(walk0, 0.001), walk1, walk0])
# No time limit: 30 s of moving without a stop, the body stepped tick by tick inside one physics
# frame and put back on the open ground each second.
Input.action_press("move_right")
await r.physics_frame
var span := _hand_step(player, start, 30.0)
var last := Vector2(player.velocity.x, player.velocity.z).length()
r.check(absf(span.x - run1) < 0.05 and absf(span.y - run1) < 0.05 and absf(last - run1) < 0.05,
"no time limit: after 30 s of moving it still runs %.2f m/s (%.2f..%.2f m/s throughout)" % [last, span.x, span.y])
# No cooldown: stop, then go again.
Input.action_release("move_right")
var stopped := _hand_step(player, start, 1.0)
Input.action_press("move_right")
var again := _hand_step(player, start, 0.5)
r.check(stopped.y < 0.05 and absf(again.x - run1) < 0.05 and absf(again.y - run1) < 0.05,
"no cooldown: stopped, then off again at once at %.2f m/s" % again.y)
Input.action_release("move_right")
sim.scarecrow.reset_for_morning()
r.check(is_equal_approx(player.speed_factor(), mult), "a new morning keeps it (x%.2f)" % player.speed_factor())
# Phones: the speed path never asks the platform, so MobilePort.active() makes no difference.
var asks := false
for script: GDScript in [player.get_script(), load("res://game/scripts/upgrades.gd")]:
asks = asks or script.source_code.contains("MobilePort")
r.check(not asks, "the speed doesn't depend on the platform (PlayerCharacter and Upgrades never ask MobilePort; phone mode here: %s)" % MobilePort.active())
s.upgrades.erase("sprint")
await _frames(r, 30)
player.velocity = Vector3.ZERO
player.global_position = Vector3(SPOT.x, 0.0, SPOT.y)
## Puts the scarecrow at a spot at rest, holds move_right at a stick strength for 30 physics
## frames, and returns its speed (m/s). move_right stays held.
func _speed_after(r: GameTestRunner, player: PlayerCharacter, at: Vector3, strength: float) -> float:
player.global_position = at
player.velocity = Vector3.ZERO
Input.action_press("move_right", strength)
await _frames(r, 30)
return Vector2(player.velocity.x, player.velocity.z).length()
## Steps the body by hand for that many seconds of 60 Hz ticks (call it right after awaiting
## physics_frame, so move_and_slide moves by DT), putting it back at the spot every second so it
## stays on open ground. Returns the lowest and highest speed (m/s) over the ticks after the
## first quarter second (the speed-up).
func _hand_step(player: PlayerCharacter, at: Vector3, seconds: float) -> Vector2:
var span := Vector2(INF, 0.0)
var ticks := roundi(seconds / DT)
for i in ticks:
if i % 60 == 0:
player.global_position = at
player._physics_process(DT)
if i >= 15:
var v := Vector2(player.velocity.x, player.velocity.z).length()
span = Vector2(minf(span.x, v), maxf(span.y, v))
return span
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)