harvest/game/tests/test_targets.gd
Forrest ffeec30873 Task #12 follow-up: theft alert for a claimed loose animal
AlertManager.collect() only raised the THEFT alert for a claimed
Status.HOME item; a thief that claimed a loose (Status.LOOSE) animal
raised no alert at all. The acceptance criteria call for loose animals
to behave per the existing theft rules, and the alert is part of that
UX, so treat LOOSE like HOME there too. Added a check to test_targets.gd
's _loose_start confirming a claimed loose animal raises the same THEFT
alert as a penned one (same AlertManager pattern test_roster.gd's
_alerts uses).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-05 15:20:58 -04:00

566 lines
28 KiB
GDScript

extends RefCounted
## All three farm targets on the real farm, with FarmSim stepped by hand at 60 Hz: task #12's
## loose start (animals scattered outside the pen, grazing in place, claimable by thieves, and led
## in with the follow-trail mechanic); then, with every animal penned up front the old way, the
## animals in the pen (wandering, kept in by the pen's gate), an animal thief breaking in, catching
## and carrying one off, a dropped animal running for the farm's edge, leading animals back (at most
## three, through a gate), a pen breach letting animals wander out at night (and not by day), supply
## thieves taking bundles from the shed's piles (never more than there is), dropping, recovering,
## and losing them, the piles growing and shrinking with the stock, thieves falling back to another
## target, the asset HUD, the end of the dawn grace, retry, and a farm stripped of all three.
const P := CampaignController.Phase
const S := LootSystem.Status
const C := LootSystem.Category
const T := ThiefBehavior.State
const DT := 1.0 / 60.0
const AWAY := Vector3(-11.0, 0.0, 13.0)
var _events: Array = [] # [kind, id]
func run(r: GameTestRunner) -> void:
await _loose_start(r) # task #12: animals start loose, on their own fresh farm instance
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 # every thief here is sent in by hand
var player := farm.get_node("Player") as PlayerCharacter
player.global_position = AWAY
sim.loot.loot_event.connect(func(kind: StringName, id: int, _p: Vector3) -> void: _events.append([kind, id]))
sim.campaign.begin_day()
PenRing.build_full(sim) # the pen is player-built now: close it the way the old starting gate did
_settle(sim)
# This suite is about pen mechanics (containment, straying, leading in), not task #12's loose
# start (covered by _loose_start above): pen every animal up front like the old always-penned
# behavior.
sim.campaign.state.pen_all_animals()
sim.loot.sync_from_state()
sim.animals.sync_from_state()
_layout(r, sim)
_pen_by_day(r, sim)
_animal_theft(r, sim, player)
_leading(r, sim, player)
_breach(r, sim, player)
_supplies(r, sim, player)
_bundle_limits(r, sim)
_piles(r, sim)
_fallback(r, sim)
_hud(r, farm, sim)
_stripped(r, sim)
farm.queue_free()
await r.process_frame
## Task #12: animals start loose, scattered outside the pen, grazing in place; a thief can still
## claim and steal one (raising the same theft alert as for a penned one, and same rule as any
## theft once it's dropped); the player leads one in with the follow-trail mechanic, entering the
## pen makes it home, and FarmState records it as penned so a morning resync keeps it home (and the
## rest loose) instead of resetting the herd into the pen.
func _loose_start(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 = AWAY
var loot := sim.loot
var animals := sim.animals
sim.campaign.begin_day()
var pen := sim.zones.zone(FarmZone.Kind.ANIMAL_PEN).rect()
var all_loose := true
var all_outside := true
for id in range(1, animals.count + 1):
all_loose = all_loose and loot.status[loot.animal_item(id)] == S.LOOSE
all_outside = all_outside and not pen.has_point(animals.position(id))
r.check(all_loose and all_outside and sim.campaign.state.penned_animals.is_empty(),
"%d animals start loose, scattered outside the (unbuilt) pen" % animals.count)
# They graze in place near their own spot, not down the escape field toward the edge.
var start := animals.pos.duplicate()
_step(sim, 600)
var moved := 0
var strayed := false
for id in range(1, animals.count + 1):
var d := animals.position(id).distance_to(start[id])
if d > 0.2:
moved += 1
if d > 4.0:
strayed = true
r.check(moved > 0 and not strayed, "loose animals graze in place (%d of %d moved a little, none wandered off)" % [moved, animals.count])
# A thief can still claim and steal a loose animal; killed, it drops the animal, which still
# runs for the edge (unchanged DROPPED rule).
var h := sim.spawn_thief(C.ANIMAL, animals.position(1) + Vector2(1.5, 0.0))
_step(sim, 1)
var item1 := sim.enemies.target[sim.enemies.resolve_handle(h)]
r.check(item1 > 0 and loot.category[item1] == C.ANIMAL and loot.status[item1] == S.LOOSE,
"a thief claims a loose animal, not just a penned one")
var am := farm.get_node("Hud/Alerts") as AlertManager
var theft_alerts := am.collect().filter(func(a: Dictionary) -> bool: return a["kind"] == AlertManager.Kind.THEFT and int(a["category"]) == C.ANIMAL)
r.check(not theft_alerts.is_empty(), "claiming a loose animal raises the same theft alert as a penned one")
var caught := _until(sim, func() -> bool: return loot.status[item1] == S.CARRIED, 60.0)
r.check(caught < 60.0, "and catches it (%.1f s)" % caught)
var dh := sim.enemies.resolve_handle(h)
sim.enemies.apply_hit(dh, 99)
_step(sim, 1)
r.check(loot.status[item1] == S.DROPPED, "killed, it drops the animal loose again (same rule as any theft)")
var stolen_index := loot.animal_index(item1)
# Build the pen and lead a different (still-loose) animal in.
PenRing.build_full(sim)
_settle(sim)
var lead_id := 1
while lead_id == stolen_index:
lead_id += 1
var item := loot.animal_item(lead_id)
var p := animals.position(lead_id)
player.global_position = Vector3(p.x + 0.8, 0.0, p.y)
_step(sim, 1)
r.check(loot.status[item] == S.FOLLOWING, "walking up to a loose animal starts it following, same as a dropped one")
var path: Array[Vector2] = [Vector2(p.x, -2.0), Vector2(7.5, -2.0), Vector2(7.5, -6.0), Vector2(0.0, -6.0), Vector2(-3.0, -6.0)]
var from := Vector2(player.global_position.x, player.global_position.z)
for target in path:
while from.distance_to(target) > 0.05:
from = from.move_toward(target, 4.5 * DT)
player.global_position = Vector3(from.x, 0.0, from.y)
sim.step(DT)
_step(sim, 60)
r.check(loot.status[item] == S.HOME and sim.campaign.state.is_animal_penned(lead_id),
"led through the gate into the pen: it's home, and FarmState records it as penned")
player.global_position = AWAY
_step(sim, 600)
r.check(pen.has_point(animals.position(lead_id)), "once penned, it stays in the pen by day like any other")
# A new morning keeps the penned one home and the rest loose, not reset into the pen.
loot.sync_from_state()
animals.sync_from_state()
var other_id := 1
while other_id == stolen_index or other_id == lead_id:
other_id += 1
r.check(loot.status[item] == S.HOME and sim.campaign.state.is_animal_penned(lead_id), "a new morning keeps the penned one home")
r.check(loot.status[loot.animal_item(other_id)] == S.LOOSE, "and a never-led animal loose, not reset into the pen")
farm.queue_free()
await r.process_frame
func _layout(r: GameTestRunner, sim: FarmSim) -> void:
var loot := sim.loot
var cfg := sim.config
r.check(sim.animals.count == cfg.start_animals and loot.animal_count == cfg.start_animals and sim.campaign.state.animals == cfg.start_animals,
"%d animals in the pen, matching GameConfig.start_animals" % sim.animals.count)
r.check(loot.item_count == loot.unit_count + loot.animal_count + LootSystem.SACKS and loot.category[loot.unit_count] == C.CROP
and loot.category[loot.animal_item(1)] == C.ANIMAL and loot.category[loot.first_sack()] == C.SUPPLY,
"one id space: crops, then animals, then sacks")
var kinds := {}
var pen := sim.zones.zone(FarmZone.Kind.ANIMAL_PEN)
var inside := true
for id in range(1, sim.animals.count + 1):
kinds[sim.animals.kind(id)] = true
inside = inside and pen.rect().has_point(sim.animals.position(id))
r.check(inside and kinds.keys().size() == 3, "chickens, pigs, and sheep (%s), all inside the pen" % [kinds.keys()])
r.check(not sim.animals.pen_open and sim.campaign.state.structure(1).get("type", "") == "gate", "the pen's gate shuts the pen")
var gap := _edge_cell(sim, "south-west")
r.check(sim.animal_field[gap] < FarmNavigation.UNREACHABLE, "thieves have a route into the pen (through its gate)")
for m in ResourceStore.MATERIALS:
var spot := sim.shed_stock.spot(m)
var c := sim.nav.cell_of(spot.x, spot.y)
r.check(sim.nav.is_walkable(c) and sim.supply_fields[m][_edge_cell(sim, "north")] < FarmNavigation.UNREACHABLE
and sim.escape_field[c] < FarmNavigation.UNREACHABLE, "the %s pile has a spot thieves can reach and leave" % m)
## By day the animals wander but stay in the pen: the gate keeps them in.
func _pen_by_day(r: GameTestRunner, sim: FarmSim) -> void:
var start := sim.animals.pos.duplicate()
var pen := sim.zones.zone(FarmZone.Kind.ANIMAL_PEN).rect()
var outside := 0
for i in 1800:
sim.step(DT)
for id in range(1, sim.animals.count + 1):
if not pen.has_point(sim.animals.position(id)):
outside += 1
var moved := 0
for id in range(1, sim.animals.count + 1):
if sim.animals.position(id).distance_to(start[id]) > 1.0:
moved += 1
r.check(moved >= sim.animals.count / 2.0 and outside == 0, "30 s by day: %d of %d animals wandered, none left the pen" % [moved, sim.animals.count])
## An animal thief breaks the pen's gate, catches an animal, and carries it; killed, it drops the
## animal, which runs for the edge of the farm; touched, the animal follows the scarecrow.
func _animal_theft(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter) -> void:
var es := sim.enemies
var loot := sim.loot
var s := sim.campaign.state
var gate := 1
# Task #11: the pen's boundary is a full ring of equally-breakable player-built fences now, not
# a single gap in an otherwise solid wall, so a thief's nearest crossing depends on where it
# comes from. Spawn just outside the gate (not a shared GameTestRunner.EDGE point) so it's
# still the nearest barrier, keeping this test's "breaks the gate" narrative.
var h := sim.spawn_thief(C.ANIMAL, Vector2(7.0, -6.0))
_step(sim, 1)
var item := es.target[es.resolve_handle(h)]
r.check(item > 0 and loot.category[item] == C.ANIMAL, "an animal thief claims an animal")
loot.update_visuals()
r.check(loot.threat[item] == LootSystem.Threat.TARGETED, "the animal shows as targeted")
var at_gate := _until(sim, func() -> bool: return es.state[es.resolve_handle(h)] == T.BREAK, 40.0)
r.check(at_gate < 40.0 and es.barrier[es.resolve_handle(h)] == gate, "the thief comes to the pen's gate and breaks it (%.1f s)" % at_gate)
var caught := _until(sim, func() -> bool: return es.state[es.resolve_handle(h)] == T.CARRY, 50.0)
var d := es.resolve_handle(h)
r.check(caught < 50.0 and s.structure(gate).is_empty() and loot.status[item] == S.CARRIED and es.carry[d] == item,
"it gets in through the broken gate and catches the animal (%.1f s more)" % caught)
r.check(int(s.night_events.get("animals_stolen", 0)) == 1, "the theft counts toward the night's record")
var pen := sim.zones.zone(FarmZone.Kind.ANIMAL_PEN).rect().grow(1.5)
var out := _until(sim, func() -> bool: return not pen.has_point(Vector2(es.pos_x[es.resolve_handle(h)], es.pos_z[es.resolve_handle(h)])), 20.0)
d = es.resolve_handle(h)
var index := loot.animal_index(item)
r.check(out < 20.0 and sim.animals.position(index).distance_to(Vector2(es.pos_x[d], es.pos_z[d])) < 0.1,
"the animal rides with its thief out of the pen (%.1f s)" % out)
var at := Vector2(es.pos_x[d], es.pos_z[d])
es.apply_hit(d, 99)
_step(sim, 1)
r.check(loot.status[item] == S.DROPPED and sim.animals.position(index).distance_to(at) < 0.2, "killing the carrier drops the animal where it fell")
var near_edge := _edge_distance(sim, sim.animals.position(index))
_step(sim, 300)
var later := _edge_distance(sim, sim.animals.position(index))
r.check(loot.status[item] == S.DROPPED and later < near_edge - 2.0, "the loose animal makes for the edge of the farm (%.1f m -> %.1f m from it)" % [near_edge, later])
var p := sim.animals.position(index)
player.global_position = Vector3(p.x + 0.8, 0.0, p.y)
_step(sim, 1)
r.check(loot.status[item] == S.FOLLOWING and _has(&"following", item), "walking up to it: it follows the scarecrow")
loot.status[item] = S.DROPPED # loose again for the next part
sim.animals.place(index, p)
player.global_position = AWAY
_step(sim, 1)
## Lead loose animals back: at most three follow at once; they pass through a gate behind the
## scarecrow, and inside the pen they are home.
func _leading(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter) -> void:
var loot := sim.loot
var st := sim.structures
var s := sim.campaign.state
s.resources.shed = {"wood": 50, "scrap": 50, "straw": 50}
r.check(st.validate("gate", 4.0, -6.0, 2, Vector2(6.0, -6.0)) == "" and st.place("gate", 4.0, -6.0, 2) > 0, "a new gate goes in the pen opening")
_settle(sim)
r.check(not sim.animals.pen_open, "the pen is shut again")
var loose: Array[int] = []
for id in range(1, sim.animals.count + 1):
if loot.status[loot.animal_item(id)] == S.DROPPED:
loose.append(id)
for id in range(1, sim.animals.count + 1):
if loose.size() < 4 and loot.status[loot.animal_item(id)] == S.HOME:
loose.append(id)
for k in loose.size():
_make_loose(sim, loose[k], Vector2(9.0 + k * 3.0, -2.0))
var recovered_before := int(s.night_events.get("animals_recovered", 0))
for k in loose.size():
player.global_position = Vector3(9.0 + k * 3.0 + 0.6, 0.0, -2.0)
_step(sim, 1)
r.check(loot.followers() == sim.config.max_following_animals and loot.status[loot.animal_item(loose[3])] == S.DROPPED
and _has(&"follow_full", loot.animal_item(loose[3])), "three animals follow; the fourth waits (and the scarecrow is told why)")
# Walk the line in through the gate: east of the pen, then west through the opening.
var path: Array[Vector2] = [Vector2(15.0, -2.0), Vector2(7.5, -2.0), Vector2(7.5, -6.0), Vector2(0.0, -6.0), Vector2(-3.0, -6.0)]
var from := Vector2(player.global_position.x, player.global_position.z)
for target in path:
while from.distance_to(target) > 0.05:
from = from.move_toward(target, 4.5 * DT)
player.global_position = Vector3(from.x, 0.0, from.y)
sim.step(DT)
_step(sim, 120)
var home := 0
for k in 3:
if loot.status[loot.animal_item(loose[k])] == S.HOME:
home += 1
r.check(home == 3 and not st.validate("gate", 4.0, -6.0, 2, Vector2(6.0, -6.0)).is_empty(), "the three follow the scarecrow through the gate into the pen")
r.check(int(s.night_events.get("animals_recovered", 0)) == recovered_before + 3, "each counts as led back")
_make_loose(sim, loose[3], Vector2(-1.0, -2.5)) # the fourth wanders back in by itself
_step(sim, 1)
r.check(loot.status[loot.animal_item(loose[3])] == S.HOME, "a loose animal inside the pen is home again")
player.global_position = AWAY
_step(sim, 1)
## With the gate down at night, animals wander out of the pen one at a time; a loose one that
## reaches the farm's edge is lost. By day an open pen keeps its animals. The end of the dawn grace brings
## loose ones back; retry restores the morning.
func _breach(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter) -> void:
var c := sim.campaign
var loot := sim.loot
var st := sim.structures
var gate := sim.nav.structure_at[sim.nav.cell_of(4.0, -6.0)]
st.damage(gate, 999)
_settle(sim)
r.check(sim.animals.pen_open, "a broken gate opens the pen")
_events.clear()
_step(sim, roundi(sim.config.animal_wander_interval * 60.0))
r.check(not _has(&"strayed", -1) and loot.count(S.HOME, C.ANIMAL) == loot.animal_count - loot.count(S.LOST, C.ANIMAL),
"by day the animals stay in an open pen")
_to_night(sim)
var morning_lost := c.state.lost_animals.duplicate()
var first := _until(sim, func() -> bool: return _has(&"strayed", -1), sim.config.animal_wander_interval + 1.0)
r.check(first <= sim.config.animal_wander_interval * 0.3 + 0.1, "at night an animal soon wanders out of the open pen (%.1f s)" % first)
var strays := _until(sim, func() -> bool: return _count(&"strayed") >= 2, sim.config.animal_wander_interval + 1.0)
r.check(strays <= sim.config.animal_wander_interval + 0.1, "then another every %.0f s" % sim.config.animal_wander_interval)
var lost := _until(sim, func() -> bool: return c.state.lost_animals.size() > morning_lost.size(), 90.0)
r.check(lost < 90.0, "a wandering animal that reaches the farm's edge is lost (%.1f s later)" % lost)
var loose_at_dawn := loot.count(S.DROPPED, C.ANIMAL)
r.check(loose_at_dawn > 0, "some are still loose on the farm (%d)" % loose_at_dawn)
var still_lost := c.state.lost_animals.duplicate()
c.advance_phase()
c.advance_phase()
r.check(c.state.phase == P.MORNING and loot.count(S.DROPPED, C.ANIMAL) == 0 and c.state.lost_animals == still_lost
and loot.count(S.HOME, C.ANIMAL) == loot.animal_count - still_lost.size(), "the end of the dawn grace brings the loose ones back to the pen")
var pen := sim.zones.zone(FarmZone.Kind.ANIMAL_PEN).rect()
var inside := true
for id in range(1, sim.animals.count + 1):
if loot.status[loot.animal_item(id)] == S.HOME:
inside = inside and pen.has_point(sim.animals.position(id))
r.check(inside and sim.animals.pen_open, "every animal left is in the pen (still open: the gate is gone)")
r.check(sim.review_panel._body.text.contains("The pen is open"), "the morning review says the pen is open")
var e: Dictionary = c.state.night_log[c.state.night_log.size() - 1]
r.check(e["animals_lost"] == still_lost.size() - morning_lost.size() and e["animals_strayed"] >= 2, "the night's record: %s" % [e])
# Retry: lose another one tonight, fall, and the morning comes back.
var morning := c.state.to_dict()
c.begin_day()
c.state.resources.shed = {"wood": 50, "scrap": 50, "straw": 50}
st.place("gate", 4.0, -6.0, 2)
_to_night(sim)
var h := sim.spawn_thief(C.ANIMAL, GameTestRunner.EDGE["west"])
_until(sim, func() -> bool: return sim.enemies.resolve_handle(h) == -1, 90.0)
r.check(c.state.lost_animals.size() >= still_lost.size() + 1, "an unopposed animal thief carries one off the farm")
c.state.scarecrow_hp = 0
_step(sim, 1)
c.retry_night()
_step(sim, 1)
r.check(c.state.to_dict() == morning and loot.count(S.LOST, C.ANIMAL) == still_lost.size() and sim.animals.pen_open,
"retry restores the morning (that animal is back, the new gate gone)")
player.global_position = AWAY
c.begin_day()
c.state.resources.shed = {"wood": 50, "scrap": 50, "straw": 50}
st.place("gate", 4.0, -6.0, 2)
_settle(sim)
## A supply thief takes a bundle of the material the shed holds most of; killed, it drops the
## sack, which the scarecrow takes back to the shed; unopposed, it carries it off.
func _supplies(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter) -> void:
var es := sim.enemies
var loot := sim.loot
var s := sim.campaign.state
var r0 := s.resources
r0.shed = {"wood": 3, "scrap": 9, "straw": 4}
var total := r0.shed_total()
var stolen0 := int(s.night_events.get("supplies_stolen", 0))
var h := sim.spawn_thief(C.SUPPLY, GameTestRunner.EDGE["north"])
_step(sim, 1)
var sack := es.target[es.resolve_handle(h)]
r.check(sack >= loot.first_sack() and loot.sack_material[sack] == "scrap" and loot.home[sack] == sim.shed_stock.spot("scrap"),
"a supply thief heads for the pile the shed holds most of (scrap)")
var took := _until(sim, func() -> bool: return es.state[es.resolve_handle(h)] == T.CARRY, 40.0)
r.check(took < 40.0 and int(r0.shed["scrap"]) == 9 - sim.config.supply_bundle and loot.sack_amount[sack] == sim.config.supply_bundle
and loot.status[sack] == S.CARRIED, "it fills a sack with %d scrap (%.1f s)" % [sim.config.supply_bundle, took])
r.check(s.supplies == total and s.supplies_in_transit == sim.config.supply_bundle
and int(s.night_events.get("supplies_stolen", 0)) == stolen0 + sim.config.supply_bundle, "a carried sack still counts as on the farm")
_step(sim, 60)
var d := es.resolve_handle(h)
var at := Vector2(es.pos_x[d], es.pos_z[d])
es.apply_hit(d, 99)
_step(sim, 1)
loot.update_visuals()
r.check(loot.status[sack] == S.DROPPED and loot.ground[sack].distance_to(at) < 0.2 and loot._tokens[sack].visible, "killing the carrier drops the sack")
player.global_position = Vector3(at.x + 0.8, 0.0, at.y)
_step(sim, 1)
r.check(loot.status[sack] == S.HOME and int(r0.shed["scrap"]) == 9 and s.supplies_in_transit == 0 and loot.sack_amount[sack] == 0,
"walking up to the sack puts the scrap back in the shed")
player.global_position = AWAY
# Unopposed, it gets away with the sack.
var lost0 := int(s.night_events.get("supplies_lost", 0))
h = sim.spawn_thief(C.SUPPLY, GameTestRunner.EDGE["north"])
_until(sim, func() -> bool: return es.resolve_handle(h) == -1, 70.0)
r.check(int(r0.shed["scrap"]) == 9 - sim.config.supply_bundle and s.supplies == total - sim.config.supply_bundle
and int(s.night_events.get("supplies_lost", 0)) == lost0 + sim.config.supply_bundle, "an unopposed thief carries %d scrap off the farm" % sim.config.supply_bundle)
r.check(loot.count(S.HOME, C.SUPPLY) == LootSystem.SACKS and s.supplies_in_transit == 0, "its sack is free for the next theft")
## A bundle never holds more than the shed has; an emptied shed leaves the thief nothing to take.
func _bundle_limits(r: GameTestRunner, sim: FarmSim) -> void:
var es := sim.enemies
var loot := sim.loot
var s := sim.campaign.state
s.resources.shed = {"wood": 2, "scrap": 1, "straw": 0}
var h := sim.spawn_thief(C.SUPPLY, GameTestRunner.EDGE["north"])
_until(sim, func() -> bool: return es.state[es.resolve_handle(h)] == T.CARRY, 40.0)
var d := es.resolve_handle(h)
var sack := es.carry[d]
r.check(sack > 0 and loot.sack_amount[sack] == 2 and int(s.resources.shed["wood"]) == 0, "with 2 wood in the shed, the bundle is 2 wood")
es.apply_hit(d, 99)
_step(sim, 1)
loot.return_dropped()
r.check(int(s.resources.shed["wood"]) == 2, "(the dropped sack goes back)")
h = sim.spawn_thief(C.SUPPLY, GameTestRunner.EDGE["north"])
_until(sim, func() -> bool: return es.state[es.resolve_handle(h)] == T.STEAL, 40.0)
d = es.resolve_handle(h)
sack = es.target[d]
for m in ResourceStore.MATERIALS:
s.resources.shed[m] = 0 # spent while the thief fills its sack
_until(sim, func() -> bool: return es.resolve_handle(h) < 0 or es.state[es.resolve_handle(h)] != T.STEAL, 10.0)
d = es.resolve_handle(h)
r.check(d >= 0 and es.carry[d] == -1 and loot.status[sack] == S.HOME and s.supplies_in_transit == 0 and s.resources.shed_total() == 0,
"the shed emptied before it finished: it takes nothing")
r.check(not s.shed_raided, "spending the last supplies isn't a raid")
_step(sim, 1)
d = es.resolve_handle(h)
r.check(d >= 0 and (es.target[d] < 0 or loot.category[es.target[d]] != C.SUPPLY), "and goes for something else")
es.apply_hit(d, 99)
_step(sim, 1)
r.check(not loot.available(C.SUPPLY), "an empty shed has nothing to claim")
s.resources.shed = {"wood": 3, "scrap": 0, "straw": 0}
h = sim.spawn_thief(C.SUPPLY, GameTestRunner.EDGE["north"])
_until(sim, func() -> bool: return es.state[es.resolve_handle(h)] == T.CARRY, 40.0)
r.check(s.resources.shed_total() == 0 and s.shed_raided, "a thief taking the last of the stock raids the shed")
es.apply_hit(es.resolve_handle(h), 99)
_step(sim, 1)
loot.return_dropped()
_step(sim, 1)
r.check(int(s.resources.shed["wood"]) == 3 and not s.shed_raided, "stock back in the shed: no longer raided")
## The shed's piles show one prop per few units, and hidden props don't collide.
func _piles(r: GameTestRunner, sim: FarmSim) -> void:
var ss := sim.shed_stock
var s := sim.campaign.state
var wood: Array = ss.slots["wood"]
var per: int = ShedStock.PILES["wood"]["per"]
for amount in [0, per * 2 + 1, 999]:
s.resources.shed["wood"] = amount
ss._process(0.0)
var want := mini(ceili(amount / float(per)), wood.size())
var shown := 0
var colliding := 0
for n: Node3D in wood:
if n.visible:
shown += 1
for b: CollisionObject3D in n.find_children("*", "CollisionObject3D", true, false):
if b.collision_layer != 0:
colliding += 1
r.check(shown == want and colliding == want, "%d wood: %d of %d log piles shown (and colliding)" % [amount, shown, wood.size()])
s.resources.shed["wood"] = 10
ss._process(0.0)
## A thief whose target is gone goes for another; one with no set target takes anything.
func _fallback(r: GameTestRunner, sim: FarmSim) -> void:
var es := sim.enemies
var loot := sim.loot
var s := sim.campaign.state
var keep := s.lost_animals.duplicate()
s.lose_all_animals()
loot.sync_from_state()
var h := sim.spawn_thief(C.ANIMAL, GameTestRunner.EDGE["south-west"])
_step(sim, 1)
var item := es.target[es.resolve_handle(h)]
r.check(item > 0 and loot.category[item] != C.ANIMAL, "with no animals left, an animal thief goes for %s" % LootSystem.CATEGORY_KEYS[loot.category[item]])
es.apply_hit_handle(h, 99)
var any := sim.spawn_thief(LootSystem.ANY, GameTestRunner.EDGE["east"])
_step(sim, 1)
r.check(es.target[es.resolve_handle(any)] > 0, "a thief with no set target claims something")
es.apply_hit_handle(any, 99)
_step(sim, 1)
s.lost_animals = keep
loot.sync_from_state()
sim.animals.sync_from_state()
func _hud(r: GameTestRunner, farm: Node3D, sim: FarmSim) -> void:
var hud := farm.get_node("Hud/AssetHud") as AssetHud
var s := sim.campaign.state
hud._process(0.0)
var before := hud.describe()
s.lose_animal(s.animals_total if not s.is_animal_lost(s.animals_total) else 1)
hud._process(0.05)
r.check(hud.describe() == "Crops %d/%d · Animals %d/%d · Supplies %d" % [s.crops, s.crops_total, s.animals, s.animals_total, s.supplies]
and hud.describe() != before and hud._flash[1] > 0.0 and hud._flash_color[1] == AssetHud.LOSS, "the asset HUD updates and flashes red on a loss: '%s'" % hud.describe())
## The farm is stripped when the last supplies are carried off with every crop and animal gone.
func _stripped(r: GameTestRunner, sim: FarmSim) -> void:
var c := sim.campaign
var s := c.state
s.lose_all_crops()
s.lose_all_animals()
sim.loot.sync_from_state()
s.resources.shed = {"wood": 0, "scrap": 4, "straw": 0}
_step(sim, 1)
r.check(c.state.phase != P.GAME_OVER, "crops and animals gone, supplies left: the vacation goes on")
var h := sim.spawn_thief(C.SUPPLY, GameTestRunner.EDGE["north"])
var stolen := _until(sim, func() -> bool: return sim.enemies.resolve_handle(h) >= 0 and sim.enemies.carry[sim.enemies.resolve_handle(h)] > 0, 40.0)
r.check(stolen < 40.0 and c.state.phase != P.GAME_OVER, "the last supplies in a thief's sack still count")
_until(sim, func() -> bool: return c.is_over(), 70.0)
r.check(c.state.phase == P.GAME_OVER and c.outcome == CampaignController.Outcome.GAME_OVER_STRIPPED,
"carried off with nothing else left: the farm is stripped (D2)")
## Puts an animal loose at p (as if dropped there).
func _make_loose(sim: FarmSim, id: int, p: Vector2) -> void:
var item := sim.loot.animal_item(id)
sim.loot.status[item] = S.DROPPED
sim.loot.holder[item] = -1
sim.loot.ground[item] = p
sim.animals.place(id, p)
func _to_night(sim: FarmSim) -> void:
var c := sim.campaign
while c.state.phase != P.NIGHT:
if c.state.phase == P.MORNING:
c.begin_day()
else:
c.advance_phase()
func _has(kind: StringName, id: int) -> bool:
for e in _events:
if e[0] == kind and (id < 0 or e[1] == id):
return true
return false
func _count(kind: StringName) -> int:
var n := 0
for e in _events:
if e[0] == kind:
n += 1
return n
func _edge_distance(sim: FarmSim, p: Vector2) -> float:
return sim.nav.meters(sim.escape_field[sim.nav.cell_of(p.x, p.y)])
func _edge_cell(sim: FarmSim, side: String) -> int:
var p: Vector2 = GameTestRunner.EDGE[side]
return sim.nav.nearest_free(sim.nav.cell_of(p.x, p.y))
func _settle(sim: FarmSim) -> void:
var guard := 0
while sim.rebuilding and guard < 600:
sim.step(DT)
guard += 1
func _step(sim: FarmSim, n: int) -> void:
for i in n:
sim.step(DT)
## Steps until cond() holds; returns the seconds it took (or max_seconds).
func _until(sim: FarmSim, cond: Callable, max_seconds: float) -> float:
var t := 0.0
while t < max_seconds and not cond.call():
sim.step(DT)
t += DT
return t