harvest/game/tests/test_weapons.gd
Forrest a0aee34e3c Task #18: Alien Ray fires a beam
The Alien Ray is now a held beam instead of a copy of the potato shot.
While fire is held it ticks every alien_ray_tick_interval (0.15 s). Each
tick traces a path from the scarecrow's center through the muzzle and on
alien_ray_range (20 m) past it, cut short at the first wall
(AlienBeam.trace on the world layer). It damages every enemy within
alien_ray_width / 2 (0.25 m) plus its hit radius of that path, flyers
included as with shots, nearest first, up to alien_ray_pierce (4), for
alien_ray_damage (1). That is about 6.7 damage a second per enemy on the
beam. Hits stay inside combat.step, so enemies.attacker credits kills.

- EnemySystem.path_hits: every enemy along a polyline, ordered along it,
  with flat ends (nothing behind the scarecrow or past the wall).
  sweep_hit is unchanged.
- AlienBeam: a WeaponModels held effect (hold 1.5 ticks, no fade). It is
  an unshaded HDR green core and glow, a muzzle light, and a flare where a
  wall stops it. Each frame it lays itself out from the muzzle along the
  model's -Z, with the same trace, so the partner's machine recomputes the
  wall clip locally. No protocol change.
- Counters: a beam_ticks counter joins attacks(), and ray ticks no
  longer count as shots_fired.
- Config: alien_ray_speed, alien_ray_lifetime and alien_ray_cooldown are
  removed. alien_ray_range, _width, _tick_interval and _pierce are added.
  The shop text says what it does now. Cost and hidden/reveal are
  unchanged.
- Sound: a zapped signal and a "zap" cue (synth alien_beam.wav, its own
  seed). The other synth files regenerate byte-identical.

Tests (test_weapons) replace the projectile-travel ray check and keep
buy/equip:
- a far target is hit on the same step;
- nothing beside, behind or past the end is hit;
- two in line and a crow are all hit;
- a line is capped at pierce;
- a wall stops it, including a barrel poking through one;
- held ticks repeat and stop on release;
- the beam is lit only while held, ends at the wall clip, follows the
  model's yaw, and the replica and attack-event paths work.

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

735 lines
36 KiB
GDScript

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 supply shed, not the workbench (rapid fire and heavy
## seeds, bought at the workbench, need the corn shooter first; a weapon bought goes into its
## hand, bombs into the off-hand); switching the main
## weapon (Tab / 1-2, owned ones only, not in build mode, never the bombs; one no longer owned
## falls back to the pitchfork); the off-hand on its own button (right mouse / LT: with the main
## weapon in hand, both at once, held, not with the right click that leaves build mode); 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; the HUD's weapon panel; the shed's other weapons; the Propane Torch's band of
## fire and its flame (#17); and the Alien Ray's beam (#18).
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)
_offhand(r, sim)
_bombs(r, sim)
_hud(r, farm, sim)
_shed_weapons(r, farm, sim)
await _torch(r, farm, sim)
await _alien_ray(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)
and c.offhand == -1, "a new vacation: the pitchfork in hand, and nothing else (the off-hand empty)")
var pivots := c.models._pivots
var only_pitchfork := pivots[0].visible
for i in range(1, pivots.size()):
only_pitchfork = only_pitchfork and not pivots[i].visible
r.check(pivots.size() == ScarecrowCombat.WEAPON_IDS.size() and only_pitchfork, "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 supply shed; then rapid fire (at the workbench) can be")
var bench_panel := farm.get_node("Hud/UpgradePanel") as UpgradePanel
var panel := farm.get_node("Hud/ShedPanel") as UpgradePanel
r.check(not bench_panel._rows.has("corn_shooter") and not bench_panel._rows.has("pumpkin_bomb")
and panel._rows.has("corn_shooter") and panel._rows.has("pumpkin_bomb"),
"the weapons are sold at the supply shed's menu, not the workbench's")
panel._buy("pumpkin_bomb")
r.check(combat.owns(W.PUMPKIN_BOMB) and combat.offhand == W.PUMPKIN_BOMB and combat.weapon == W.PITCHFORK,
"pumpkin bombs bought from the shed's menu go into the off-hand, the pitchfork staying in hand")
var pivots := combat.models._pivots
r.check(pivots[2].visible and pivots[0].visible and not pivots[1].visible, "both show on the scarecrow")
r.check(pivots[0].position.x > 0.0 and pivots[2].position.x < 0.0, "the pitchfork in the right hand, the pumpkin in the left")
s.upgrades.erase("corn_shooter")
panel._buy("corn_shooter")
r.check(combat.weapon == W.CORN_SHOOTER and combat.offhand == W.PUMPKIN_BOMB, "the corn shooter bought from the shed's menu goes into the hand")
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.PITCHFORK, W.CORN_SHOOTER] and c.offhand == W.PUMPKIN_BOMB,
"Tab / d-pad down cycles through the main weapons, the bombs staying in the off-hand: %s" % [seen])
_tap(sim, "weapon_1")
var first := c.weapon
_tap(sim, "weapon_2")
r.check(first == W.PITCHFORK and c.weapon == W.CORN_SHOOTER, "1-2 pick one")
r.check(c.equip(W.PUMPKIN_BOMB) and c.weapon == W.CORN_SHOOTER and not InputMap.has_action("weapon_3"),
"the bombs never take the main hand (and there's no third weapon key)")
c.equip(W.PITCHFORK)
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)")
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")
_tap(sim, "weapon_next")
var skipped := c.weapon
_tap(sim, "weapon_2")
r.check(skipped == W.PITCHFORK and c.weapon == W.PITCHFORK, "only weapons owned: no corn shooter without the upgrade")
s.upgrades.erase("pumpkin_bomb")
_step(sim, 1)
r.check(c.offhand == -1 and not c.models._pivots[2].visible and not c.use_offhand(), "bombs no longer owned leave the off-hand")
s.upgrades["corn_shooter"] = 1
s.upgrades["pumpkin_bomb"] = 1
_step(sim, 1)
r.check(c.offhand == W.PUMPKIN_BOMB, "and come back to it once owned")
## The off-hand's own button, with the corn shooter in hand.
func _offhand(r: GameTestRunner, sim: FarmSim) -> void:
var c := sim.combat
var cfg := sim.config
var recharge := roundi(cfg.bomb_cooldown * 60.0) + 2
c.equip(W.CORN_SHOOTER)
c.device = ScarecrowCombat.Device.GAMEPAD # keeps the aim it's given, and build mode's cursor in the open
c.aim = Vector2(1.0, 0.0)
c.ready_for_morning()
_step(sim, 1)
var thrown := c.bombs_thrown
var shots := c.shots_fired
_tap(sim, "offhand")
r.check(c.bombs_thrown == thrown + 1 and c.shots_fired == shots and c.weapon == W.CORN_SHOOTER,
"right mouse / LT throws a pumpkin bomb from the off-hand, the corn shooter staying in hand")
_step(sim, recharge)
Input.action_press("fire")
Input.action_press("offhand")
_step(sim, 1)
Input.action_release("fire")
Input.action_release("offhand")
_step(sim, 1)
r.check(c.bombs_thrown == thrown + 2 and c.shots_fired == shots + 1, "both at once: a shot and a throw")
_step(sim, recharge)
thrown = c.bombs_thrown
Input.action_press("offhand")
_step(sim, recharge + 2)
Input.action_release("offhand")
_step(sim, 1)
r.check(c.bombs_thrown == thrown + 2, "holding it throws the next one as soon as it's back in hand (%d)" % (c.bombs_thrown - thrown))
_step(sim, recharge)
# The right mouse button also leaves build mode (cancel): that click throws nothing.
sim.build.set_active(true)
_step(sim, 2)
thrown = c.bombs_thrown
Input.action_press("cancel")
Input.action_press("offhand")
_step(sim, 1)
var left := not sim.build.active
_step(sim, 30)
Input.action_release("cancel")
Input.action_release("offhand")
_step(sim, 1)
r.check(left and c.bombs_thrown == thrown, "the right click that leaves build mode throws no bomb, even held")
_tap(sim, "offhand")
r.check(c.bombs_thrown == thrown + 1, "the next click does")
c.ready_for_morning()
thrown = c.bombs_thrown
Input.action_press("offhand")
_step(sim, 3)
Input.action_release("offhand")
_step(sim, 1)
r.check(c.bombs_thrown == thrown, "a new morning needs a fresh press (held from the night before: nothing)")
sim.bombs.clear()
c.equip(W.PITCHFORK)
c.device = ScarecrowCombat.Device.MOUSE
func _bombs(r: GameTestRunner, sim: FarmSim) -> void:
var c := sim.combat
var cfg := sim.config
var es := sim.enemies
var kit := sim.scarecrow
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()
var bursts := sim.bombs.bursts
r.check(c.offhand_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.offhand_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 == 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.offhand_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 cfg := sim.config
var s := sim.campaign.state
s.upgrades.erase("corn_shooter")
c.ready_for_morning()
_step(sim, 1)
hud._process(0.0)
var charged: Array = hud._offhand_rows.map(func(l: Label) -> String: return l.text)
c.offhand_toward(AT + Vector2(6.0, 0.0))
hud._process(0.0)
var rows: Array = hud._weapon_rows.map(func(l: Label) -> String: return l.text)
var off: Array = hud._offhand_rows.map(func(l: Label) -> String: return l.text)
r.check(rows == ["1 Pitchfork", "2 Corn shooter (supply shed)", "Potato cannon (supply shed)",
"Shurikens (supply shed)", "Propane torch (supply shed)", "Alien ray (supply shed)"],
"the HUD lists the main weapons, the first two by number (no more number keys): %s" % [rows])
r.check(charged == ["Pumpkin bombs"] and off == ["Pumpkin bombs %.0f s" % ceilf(cfg.bomb_cooldown)],
"and the off-hand's bombs, with their recharge: %s, %s" % [charged, off])
r.check(hud._weapon_title.visible and hud._weapon_hint.text.contains("Tab") and hud._offhand_title.text.contains("right mouse"),
"with how to switch, and the off-hand's button")
s.upgrades.erase("pumpkin_bomb")
_step(sim, 1)
sim.bombs.clear()
_step(sim, 1)
## Task #7's four shed weapons (Potato Cannon, Shurikens, Propane Torch, Alien Ray): each
## purchasable at the supply shed, equippable, and deals damage. The Alien Ray stays hidden until
## enough Alien Goop is collected (Upgrades.revealed), then buys like the others (_alien_ray: its
## beam, #18; _torch: the torch's band, #17).
func _shed_weapons(r: GameTestRunner, farm: Node3D, sim: FarmSim) -> void:
var c := sim.combat
var cfg := sim.config
var s := sim.campaign.state
var panel := farm.get_node("Hud/ShedPanel") as UpgradePanel
s.resources.shed = {"wood": 999, "scrap": 999, "straw": 999, "alien_goop": 999}
c.equip(W.PITCHFORK)
c.ready_for_morning()
_step(sim, 1)
# The Alien Ray is hidden until enough Alien Goop is collected (Decision 11 / #5's pattern).
s.night_events.erase("gathered_alien_goop")
panel.refresh()
r.check(not Upgrades.revealed(s, cfg, "alien_ray") and (panel._rows["alien_ray"] as Button).text == "?",
"the Alien Ray stays a \"?\" in the shop until enough Alien Goop is collected")
r.check(not Upgrades.buy(sim.campaign, "alien_ray").contains("done:") and not Upgrades.owned(s, "alien_ray"),
"and can't be bought yet")
s.night_events["gathered_alien_goop"] = cfg.hidden_reveal_goop
r.check(Upgrades.revealed(s, cfg, "alien_ray"), "revealed once hidden_reveal_goop is collected")
_buy_equip_hit(r, farm, sim, "potato_cannon", W.POTATO_CANNON, cfg.potato_cannon_speed)
_buy_equip_hit(r, farm, sim, "shurikens", W.SHURIKENS, cfg.shuriken_speed)
c.equip(W.PITCHFORK)
## The Propane Torch (#17): bought at the supply shed, a band of fire along the gun's line
## (torch_width wide, from the scarecrow out to torch_range past the muzzle). An enemy in the band
## burns; one beside, behind, or beyond it doesn't. Held, it ticks every torch_tick_interval and
## stops on release. Its flame, sized to the band and facing the model's way, is lit only while
## fire is held: on its own machine, for the partner's replica, and on the partner's machine (lit
## by attack events, which nothing steps there) where it goes out by itself.
func _torch(r: GameTestRunner, farm: Node3D, sim: FarmSim) -> void:
var c := sim.combat
var cfg := sim.config
var s := sim.campaign.state
var es := sim.enemies
var panel := farm.get_node("Hud/ShedPanel") as UpgradePanel
var at := Vector2(sim.player.global_position.x, sim.player.global_position.z) # (AT, give or take)
s.resources.shed = {"wood": 999, "scrap": 999, "straw": 999}
r.check(not c.owns(W.PROPANE_TORCH), "the propane torch isn't owned yet")
panel._buy("propane_torch")
r.check(Upgrades.owned(s, "propane_torch") and c.owns(W.PROPANE_TORCH), "it's purchasable at the supply shed")
r.check(c.equip(W.PROPANE_TORCH) and c.weapon == W.PROPANE_TORCH, "and equippable")
var flame := c.models.held_effect(W.PROPANE_TORCH) as TorchFlame
r.check(flame != null and not flame.visible and not c.models.lit(W.PROPANE_TORCH), "its flame is out until it fires")
r.check(is_equal_approx(flame.length, cfg.torch_range) and is_equal_approx(flame.width, cfg.torch_width),
"the flame is sized to the band (%.1f m long, %.1f m wide)" % [flame.length, flame.width])
# One tick aimed east (spawned and burned on the same step, so nobody gets a swing in first).
# The gun's line runs SIDE_OFFSET south of the scarecrow's, bent onto the aim point.
var reach := cfg.muzzle_distance + cfg.torch_range # from the scarecrow's center
var far_in := es.spawn(THIEF, at.x + reach - 0.2, at.y, Vector2.LEFT)
var side_in := es.spawn(THIEF, at.x + 2.5, at.y + 0.95, Vector2.LEFT)
var beside := es.spawn(THIEF, at.x + 2.5, at.y + 2.3, Vector2.LEFT)
var beside2 := es.spawn(THIEF, at.x + 3.5, at.y - 1.9, Vector2.LEFT)
var behind := es.spawn(THIEF, at.x - 1.5, at.y, Vector2.RIGHT)
var beyond := es.spawn(THIEF, at.x + reach + 1.0, at.y, Vector2.LEFT)
var full := es.t_hp[THIEF]
c.reset()
var ticks := c.torch_ticks
var played := sim.audio.played if sim.audio != null else {}
var roars := int(played.get("burn", 0))
var stabs := int(played.get("stab", 0))
r.check(c.attack_toward(at + Vector2(8.0, 0.0)) and c.torch_ticks == ticks + 1, "it fires its first tick")
r.check(sim.audio == null or (int(played.get("burn", 0)) == roars + 1 and int(played.get("stab", 0)) == stabs),
"with a roar (its own sound, not the pitchfork's whoosh)")
r.check(_hp(es, far_in) == full - cfg.torch_damage and _hp(es, side_in) == full - cfg.torch_damage,
"an enemy in the band burns for %d: %.1f m past the muzzle, and off its center line" % [cfg.torch_damage, cfg.torch_range - 0.2])
r.check(_hp(es, beside) == full and _hp(es, beside2) == full, "one beside the band, either side, doesn't")
r.check(_hp(es, behind) == full and _hp(es, beyond) == full, "nor one behind the scarecrow, nor one beyond the band's end")
r.check(c.models._attack_t < 0.0 and c.models.lit(W.PROPANE_TORCH) and flame.visible,
"a tick lights the flame instead of replaying the gun's kick")
_clear(sim)
var hugger := es.spawn(THIEF, at.x + 0.7, at.y, Vector2.LEFT)
c.reset()
c.attack_toward(at + Vector2(0.7, 0.0))
r.check(_hp(es, hugger) == full - cfg.torch_damage,
"a thief pressed against the scarecrow, aimed at, burns too (it stands inside the muzzle)")
_clear(sim)
# Held: a tick every torch_tick_interval, the flame lit throughout; let go, both stop.
var tough := es.spawn(THIEF, at.x + 3.5, at.y, Vector2.LEFT)
es.health[es.resolve_handle(tough)] = 99
c.device = ScarecrowCombat.Device.GAMEPAD # keeps the aim it's given
c.aim = Vector2(1.0, 0.0)
c.reset()
_step(sim, 1) # fire not held: the reset's release is seen
r.check(not c.models.lit(W.PROPANE_TORCH), "(the flame went out: fire isn't held)")
ticks = c.torch_ticks
var burns: Array[int] = []
var on_burn := func(n: int) -> void: burns.append(n)
c.burned.connect(on_burn)
var intervals := 5
var lit_throughout := true
Input.action_press("fire")
for i in c.torch_interval_ticks() * intervals:
_step(sim, 1)
lit_throughout = lit_throughout and c.models.lit(W.PROPANE_TORCH) and flame.lit and flame.visible
var held_ticks := c.torch_ticks - ticks
var burned := 99 - _hp(es, tough)
Input.action_release("fire")
_step(sim, 1)
var out_at_once := not c.models.lit(W.PROPANE_TORCH) and not flame.lit and flame.visible
var after := c.torch_ticks
_step(sim, c.torch_interval_ticks() * 3)
r.check(held_ticks == intervals and burned == intervals * cfg.torch_damage and burns.size() == intervals,
"holding fire burns every %.1f s: %d ticks in %.1f s, %d damage" % [cfg.torch_tick_interval, held_ticks, intervals * cfg.torch_tick_interval, burned])
r.check(c.torch_ticks == after and 99 - _hp(es, tough) == burned, "letting go stops it")
r.check(lit_throughout, "the flame stays lit the whole time fire is held, between ticks too")
r.check(out_at_once and not flame.visible, "and goes out when fire is let go (the fire in the air fades, then it's hidden)")
c.burned.disconnect(on_burn)
# The flame comes from the gun and faces the model's way (all the partner's machine knows of
# the other scarecrow's aim).
var model := sim.player.model
var yaw := model.rotation.y
model.rotation.y = 1.0
var dir := flame.direction()
var facing := Vector2(-sin(1.0), -cos(1.0))
r.check(Vector2(dir.x, dir.z).normalized().dot(facing) > 0.999 and dir.y < 0.0,
"the flame points the way the model faces (dipping toward the band)")
model.rotation.y = yaw
# The partner's own scarecrow on its machine (a replica: the host burns for it) shows the
# flame while fire is held there.
c.replica = true
Input.action_press("fire")
_step(sim, c.torch_interval_ticks() * 2)
var replica_lit := c.models.lit(W.PROPANE_TORCH) and flame.visible and c.torch_ticks == after
Input.action_release("fire")
_step(sim, 1)
c.replica = false
r.check(replica_lit and not c.models.lit(W.PROPANE_TORCH), "a replica's flame is lit while fire is held there, and burns nothing")
# The other scarecrow on the partner's machine: lit by its attack events (CoopSession: show the
# weapon, then models.attack), one per tick, and stepped by nothing there, so its flame times
# out on the physics frames.
_step(sim, ceili(TorchFlame.LIFETIME * 60.0) + 2)
var events_lit := true
for k in 3:
c.show_weapon(W.PROPANE_TORCH)
c.models.attack(W.PROPANE_TORCH)
for i in c.torch_interval_ticks():
await r.physics_frame
events_lit = events_lit and c.models.lit(W.PROPANE_TORCH) and flame.visible
var hold := cfg.torch_tick_interval * ScarecrowCombat.HOLD_TICKS
for i in ceili((hold + TorchFlame.LIFETIME) * Engine.physics_ticks_per_second) + 6:
await r.physics_frame
r.check(events_lit and not c.models.lit(W.PROPANE_TORCH) and not flame.visible,
"on the partner's machine, attack events keep the flame lit, and it goes out by itself once they stop")
c.models.attack(W.PROPANE_TORCH)
c.equip(W.PITCHFORK)
r.check(not flame.visible and not c.models.lit(W.PROPANE_TORCH), "switching weapons puts it out at once")
c.device = ScarecrowCombat.Device.MOUSE
_clear(sim)
## The Alien Ray (#18), revealed in _shed_weapons: bought at the supply shed, a beam from the
## scarecrow through the muzzle and on alien_ray_range past it. A tick hits at once, however far
## along the beam, every enemy on it (crows too, the nearest alien_ray_pierce); nothing beside,
## behind, or beyond it; nothing past a wall, which cuts it short. Held, it ticks every
## alien_ray_tick_interval and stops on release. Its beam shows only while fire is held, follows
## the model's yaw, and ends where the damage does: its own wall test, as on the partner's
## machine, where attack events light it and it goes out by itself.
func _alien_ray(r: GameTestRunner, farm: Node3D, sim: FarmSim) -> void:
var c := sim.combat
var cfg := sim.config
var s := sim.campaign.state
var es := sim.enemies
var panel := farm.get_node("Hud/ShedPanel") as UpgradePanel
var model := sim.player.model
var at := Vector2(sim.player.global_position.x, sim.player.global_position.z) # (AT, give or take; east is open)
s.resources.shed = {"wood": 999, "scrap": 999, "straw": 999, "alien_goop": 999}
r.check(Upgrades.revealed(s, cfg, "alien_ray") and not c.owns(W.ALIEN_RAY), "the alien ray (revealed) isn't owned yet")
panel._buy("alien_ray")
r.check(Upgrades.owned(s, "alien_ray") and c.owns(W.ALIEN_RAY), "it's purchasable at the supply shed")
r.check(c.equip(W.ALIEN_RAY) and c.weapon == W.ALIEN_RAY, "and equippable")
var beam := c.models.held_effect(W.ALIEN_RAY) as AlienBeam
r.check(beam != null and not beam.visible and not c.models.lit(W.ALIEN_RAY), "its beam is off until it fires")
r.check(is_equal_approx(beam.length, cfg.alien_ray_range) and is_equal_approx(beam.width, cfg.alien_ray_width),
"the beam is sized to the ray (%.0f m long, %.1f m wide)" % [beam.length, beam.width])
var full := es.t_hp[THIEF]
var reach := cfg.muzzle_distance + cfg.alien_ray_range # from the scarecrow's center
var east := at + Vector2(16.0, 0.0)
# One tick aimed east, spawned and zapped on the same step: no shot to wait for. The gun's line
# runs SIDE_OFFSET south of the scarecrow's, bent onto the aim point.
var far := es.spawn(THIEF, at.x + reach - 1.0, at.y, Vector2.LEFT)
var beside := es.spawn(THIEF, at.x + 7.0, at.y + 1.4, Vector2.LEFT)
var beside2 := es.spawn(THIEF, at.x + 11.0, at.y - 1.3, Vector2.LEFT)
var behind := es.spawn(THIEF, at.x - 1.5, at.y, Vector2.RIGHT)
var beyond := es.spawn(THIEF, at.x + reach + 1.5, at.y, Vector2.LEFT)
c.reset()
var ticks := c.beam_ticks
var shots := c.shots_fired
var attacks := c.attacks()
var played := sim.audio.played if sim.audio != null else {}
var zaps := int(played.get("zap", 0))
var pops := int(played.get("shot", 0))
r.check(c.attack_toward(east) and c.beam_ticks == ticks + 1 and c.shots_fired == shots and c.attacks() == attacks + 1,
"it fires a beam tick (counted as one, not as a shot)")
r.check(sim.audio == null or (int(played.get("zap", 0)) == zaps + 1 and int(played.get("shot", 0)) == pops),
"with a hum (its own sound, not the gun's pop)")
r.check(_hp(es, far) == full - cfg.alien_ray_damage,
"a thief %.0f m off, near the beam's end, is hit for %d on the same step" % [reach - 1.0, cfg.alien_ray_damage])
r.check(_hp(es, beside) == full and _hp(es, beside2) == full, "one beside the beam, either side, isn't")
r.check(_hp(es, behind) == full and _hp(es, beyond) == full, "nor one behind the scarecrow, nor one past the beam's end")
r.check(c.models._attack_t < 0.0 and c.models.lit(W.ALIEN_RAY) and beam.visible,
"a tick lights the beam instead of replaying the gun's kick")
# The beam drawn, on the model turned to the aim (all the partner's machine knows of it), runs
# from the muzzle to the end of the damage's path.
var yaw := model.rotation.y
model.rotation.y = atan2(-c.aim.x, -c.aim.y)
var path := c.beam_path()
var drawn := beam.rebuild(beam.global_transform)
r.check(path.size() == 3 and _flat(beam.start).distance_to(_flat(path[1])) < 0.02 and _flat(beam.end).distance_to(_flat(path[2])) < 0.02
and absf(drawn - cfg.alien_ray_range) < 0.02 and not beam.blocked and beam._core.visible,
"the beam runs from the muzzle the full %.0f m, where the damage does (drawn %.2f m)" % [cfg.alien_ray_range, drawn])
_clear(sim)
# Two in line (and a crow over the same line) are all hit: it pierces.
var first := es.spawn(THIEF, at.x + 4.0, at.y, Vector2.LEFT)
var second := es.spawn(THIEF, at.x + 8.0, at.y, Vector2.LEFT)
var crow := es.spawn(EnemySystem.TYPE_CROW, at.x + 12.0, at.y, Vector2.LEFT)
c.reset()
c.attack_toward(east)
r.check(_hp(es, first) == full - cfg.alien_ray_damage and _hp(es, second) == full - cfg.alien_ray_damage,
"two thieves in line are both hit (the beam pierces)")
r.check(_hp(es, crow) < es.t_hp[EnemySystem.TYPE_CROW], "a crow over the beam is hit too")
_clear(sim)
# A whole line: the nearest alien_ray_pierce only.
var line: Array[int] = []
for k in cfg.alien_ray_pierce + 2:
line.append(es.spawn(THIEF, at.x + 3.0 + k * 1.5, at.y, Vector2.LEFT))
c.reset()
c.attack_toward(east)
var hit := line.map(func(h: int) -> bool: return _hp(es, h) < full)
var nearest := true
for k in line.size():
nearest = nearest and hit[k] == (k < cfg.alien_ray_pierce)
r.check(nearest, "a line of %d: the nearest %d are hit (%s)" % [line.size(), cfg.alien_ray_pierce, hit])
_clear(sim)
# A wall cuts it short: what's in front is hit, what's behind isn't, and the beam drawn ends there.
var wall := StaticBody3D.new()
var shape := CollisionShape3D.new()
var box := BoxShape3D.new()
box.size = Vector3(0.2, 3.0, 4.0)
shape.shape = box
wall.add_child(shape)
farm.add_child(wall)
wall.global_position = Vector3(at.x + 6.0, 1.5, at.y)
await r.physics_frame # it joins the physics space on the next physics frame
var front := es.spawn(THIEF, at.x + 4.0, at.y, Vector2.LEFT)
var walled := es.spawn(THIEF, at.x + 8.0, at.y, Vector2.LEFT)
var walled_far := es.spawn(THIEF, at.x + 15.0, at.y, Vector2.LEFT)
c.reset()
c.attack_toward(east)
r.check(_hp(es, front) == full - cfg.alien_ray_damage and _hp(es, walled) == full and _hp(es, walled_far) == full,
"a wall stops it: the thief in front is hit, the ones behind the wall aren't")
path = c.beam_path()
model.rotation.y = atan2(-c.aim.x, -c.aim.y)
drawn = beam.rebuild(beam.global_transform)
var face := at.x + 6.0 - 0.1
r.check(path.size() == 3 and absf(path[2].x - face) < 0.05, "the beam's path ends at the wall's face (x %.2f, wall at %.2f)" % [path[2].x, face])
r.check(beam.blocked and _flat(beam.end).distance_to(_flat(path[2])) < 0.05 and absf(drawn - _flat(path[1]).distance_to(_flat(path[2]))) < 0.05
and beam._flare.visible, "the beam drawn ends there too (%.2f m from the muzzle), with a flare" % drawn)
_clear(sim)
# A barrel poking through a wall (the scarecrow pressed against it) fires nothing past it.
wall.global_position = Vector3(at.x + 0.75, 1.5, at.y)
await r.physics_frame
var past := es.spawn(THIEF, at.x + 3.0, at.y, Vector2.LEFT)
c.reset()
c.attack_toward(east)
path = c.beam_path()
drawn = beam.rebuild(beam.global_transform)
r.check(_hp(es, past) == full and path.size() == 2 and drawn == 0.0 and not beam._core.visible,
"a barrel poking through a wall hits nothing past it (and no beam shows beyond)")
wall.queue_free()
await r.physics_frame
_clear(sim)
model.rotation.y = yaw
# Held: a tick every alien_ray_tick_interval, the beam lit throughout; let go, both stop.
var tough := es.spawn(THIEF, at.x + 10.0, at.y, Vector2.LEFT)
es.health[es.resolve_handle(tough)] = 99
c.device = ScarecrowCombat.Device.GAMEPAD # keeps the aim it's given
c.aim = Vector2(1.0, 0.0)
c.reset()
_step(sim, 1) # fire not held: the reset's release is seen
r.check(not c.models.lit(W.ALIEN_RAY) and not beam.visible, "(the beam went out: fire isn't held)")
ticks = c.beam_ticks
shots = c.shots_fired
var zapped: Array[int] = []
var on_zap := func(n: int) -> void: zapped.append(n)
c.zapped.connect(on_zap)
var intervals := 5
var lit_throughout := true
Input.action_press("fire")
for i in c.alien_ray_ticks() * intervals:
_step(sim, 1)
lit_throughout = lit_throughout and c.models.lit(W.ALIEN_RAY) and beam.lit and beam.visible
var held_ticks := c.beam_ticks - ticks
var damage := 99 - _hp(es, tough)
Input.action_release("fire")
_step(sim, 1)
var out_at_once := not c.models.lit(W.ALIEN_RAY) and not beam.lit and not beam.visible
var after := c.beam_ticks
_step(sim, c.alien_ray_ticks() * 3)
r.check(held_ticks == intervals and damage == intervals * cfg.alien_ray_damage and zapped.size() == intervals and c.shots_fired == shots,
"holding fire zaps every %.2f s: %d ticks in %.2f s, %d damage" % [cfg.alien_ray_tick_interval, held_ticks, intervals * cfg.alien_ray_tick_interval, damage])
r.check(c.beam_ticks == after and 99 - _hp(es, tough) == damage, "letting go stops it")
r.check(lit_throughout, "the beam stays lit the whole time fire is held, between ticks too")
r.check(out_at_once, "and goes out the moment fire is let go")
c.zapped.disconnect(on_zap)
# The beam follows the model's yaw.
model.rotation.y = 1.0
c.models.attack(W.ALIEN_RAY)
beam.rebuild(beam.global_transform)
var dir := _flat(beam.end - beam.start).normalized()
r.check(dir.dot(Vector2(-sin(1.0), -cos(1.0))) > 0.999, "the beam points the way the model faces")
model.rotation.y = yaw
# The partner's own scarecrow on its machine (a replica: the host zaps for it) shows the beam
# while fire is held there.
c.replica = true
Input.action_press("fire")
_step(sim, c.alien_ray_ticks() * 2)
var replica_lit := c.models.lit(W.ALIEN_RAY) and beam.visible and c.beam_ticks == after
Input.action_release("fire")
_step(sim, 1)
c.replica = false
r.check(replica_lit and not c.models.lit(W.ALIEN_RAY), "a replica's beam is lit while fire is held there, and zaps nothing")
# The other scarecrow on the partner's machine: lit by its attack events (one per tick) and
# stepped by nothing there, so its beam times out on the physics frames (and lays itself out
# every frame meanwhile).
var events_lit := true
for k in 3:
c.show_weapon(W.ALIEN_RAY)
c.models.attack(W.ALIEN_RAY)
for i in c.alien_ray_ticks():
await r.physics_frame
events_lit = events_lit and c.models.lit(W.ALIEN_RAY) and beam.visible
var hold := cfg.alien_ray_tick_interval * ScarecrowCombat.HOLD_TICKS
for i in ceili(hold * Engine.physics_ticks_per_second) + 6:
await r.physics_frame
r.check(events_lit and not c.models.lit(W.ALIEN_RAY) and not beam.visible,
"on the partner's machine, attack events keep the beam lit, and it goes out by itself once they stop")
c.models.attack(W.ALIEN_RAY)
c.equip(W.PITCHFORK)
r.check(not beam.visible and not c.models.lit(W.ALIEN_RAY), "switching weapons puts it out at once")
c.device = ScarecrowCombat.Device.MOUSE
_clear(sim)
func _flat(v: Vector3) -> Vector2:
return Vector2(v.x, v.z)
## Buys id at the supply shed, equips w, and checks that a shot fired at a thief some meters off
## damages it once it arrives (speed m/s: how long to wait).
func _buy_equip_hit(r: GameTestRunner, farm: Node3D, sim: FarmSim, id: String, w: int, speed: float) -> void:
var c := sim.combat
var s := sim.campaign.state
var es := sim.enemies
var panel := farm.get_node("Hud/ShedPanel") as UpgradePanel
r.check(not c.owns(w), "%s isn't owned yet" % id)
panel._buy(id)
r.check(Upgrades.owned(s, id) and c.owns(w), "%s is purchasable at the supply shed" % id)
r.check(c.equip(w) and c.weapon == w, "and equippable")
var dist := 4.0
var target := es.spawn(THIEF, AT.x + dist, AT.y, Vector2.LEFT)
_step(sim, 1)
var hp := _hp(es, target)
r.check(c.attack_toward(AT + Vector2(dist + 2.0, 0.0)), "%s fires" % id)
var ticks := ceili(dist / speed * Engine.physics_ticks_per_second) + 15
var dropped := false
for i in ticks:
_step(sim, 1)
var d := es.resolve_handle(target)
if d < 0 or es.health[d] < hp:
dropped = true
break
r.check(dropped, "%s deals damage once its shot arrives" % id)
_clear(sim)
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)