extends RefCounted ## All three farm targets on the real farm, with FarmSim stepped by hand at 60 Hz: tasks #12 and ## #21's loose animals (scattered outside the pen at the start, roaming the whole farm day and ## night without ever leaving it, wandering into the pen, or getting lost on their own, back at ## their spots each morning, claimable by thieves, loose again where a killed carrier drops them, ## 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 loose where it fell, leading animals back (at ## most three, through a gate), a pen breach letting animals out at night as loose animals (and ## not by day; none lost), an unopposed thief carrying one off the farm, 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 ## Where the scarecrow waits when it isn't needed: off the playable map, so loose animals (#21: they ## roam the whole map) never come within reach and start following it. const AWAY := Vector3(-20.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 ## Tasks #12 and #21: animals start loose, scattered outside the pen. A thief can still claim and ## steal a loose animal (raising the same theft alert as for a penned one); killed, it drops the ## animal loose where it fell, not lost. Over a long day and night (no thieves) loose animals roam ## the whole farm, well away from where they started, but never off the playable map, never into ## the (unbuilt) pen, and never lost on their own; each morning they're back at their spots. 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. Last, an unopposed thief carries one off the farm: the only way ## to lose an animal. 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 var c := sim.campaign var s := c.state c.begin_day() var pen := sim.zones.zone(FarmZone.Kind.ANIMAL_PEN).rect() var playable := sim.zones.playable 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 s.penned_animals.is_empty(), "%d animals start loose, scattered outside the (unbuilt) pen" % animals.count) # A thief can still claim and steal a loose animal; killed, it drops the animal loose where it # fell (#21: not DROPPED, and it doesn't run for the edge). 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) var fell := Vector2(sim.enemies.pos_x[dh], sim.enemies.pos_z[dh]) sim.enemies.apply_hit(dh, 99) _step(sim, 1) var stolen_index := loot.animal_index(item1) r.check(loot.status[item1] == S.LOOSE and animals.position(stolen_index).distance_to(fell) < 0.2, "killed, it drops the animal loose where it fell (not lost)") # A long day and night with no thieves: they roam the whole farm, and stay on it. var farthest := {"day": PackedFloat32Array(), "night": PackedFloat32Array()} var off_map := 0 var into_pen := 0 for part: String in ["day", "night"]: if part == "night": _to_night(sim) var start := animals.pos.duplicate() var far := PackedFloat32Array() far.resize(animals.count + 1) for i in 90 * 60: sim.step(DT) if i % 10 != 0: continue for id in range(1, animals.count + 1): var at := animals.position(id) far[id] = maxf(far[id], at.distance_to(start[id])) if not playable.has_point(at): off_map += 1 if pen.has_point(at): into_pen += 1 farthest[part] = far for part: String in ["day", "night"]: var far: PackedFloat32Array = farthest[part] var roamed := 0 var least := INF var most := 0.0 for id in range(1, animals.count + 1): if far[id] > 4.0: roamed += 1 least = minf(least, far[id]) most = maxf(most, far[id]) r.info("loose animals over 90 s of %s: %.1f..%.1f m from where they started" % [part, least, most]) r.check(roamed == animals.count, "over 90 s of %s, all %d loose animals roam more than 4 m (the least %.1f m, the most %.1f m)" % [part, animals.count, least, most]) r.check(off_map == 0 and into_pen == 0 and s.phase == P.NIGHT, "and they stay on the farm: never off the playable map, never into the pen") r.check(loot.count(S.LOOSE, C.ANIMAL) == animals.count and s.lost_animals.is_empty(), "none of them is lost without a thief") # The next morning puts every loose animal back at its spot. c.advance_phase() c.advance_phase() r.check(s.phase == P.MORNING and loot.count(S.LOOSE, C.ANIMAL) == animals.count and _at_loose_spots(sim), "the next morning every loose animal is back at its spot") c.begin_day() # Build the pen and lead a different (still-loose) animal in through its gate. The others are # put back at their spots, south of the pen, so the walk (from east of the gate, straight in # along its line) passes no other animal. PenRing.build_full(sim) _settle(sim) animals.sync_from_state() var lead_id := animals.count while lead_id == stolen_index: lead_id -= 1 var item := loot.animal_item(lead_id) var p := Vector2(PenRing.EAST_X + 10.0, PenRing.GATE_Z) animals.place(lead_id, p) 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(0.0, PenRing.GATE_Z), Vector2(-3.0, PenRing.GATE_Z)] 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 s.is_animal_penned(lead_id) and loot.followers() == 0, "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 (at their spots), not reset into # the pen. loot.sync_from_state() animals.sync_from_state() r.check(loot.status[item] == S.HOME and s.is_animal_penned(lead_id) and s.penned_animals.size() == 1, "a new morning keeps the penned one home") r.check(loot.count(S.LOOSE, C.ANIMAL) == animals.count - 1 and _at_loose_spots(sim), "and every never-led animal loose at its spot, not reset into the pen") # The one way to lose an animal: a thief carrying it off the farm. (Sent in amid the loose # spots, so every loose animal is in plain view.) var h2 := sim.spawn_thief(C.ANIMAL, Vector2(1.5, 0.5)) var gone := _until(sim, func() -> bool: return sim.enemies.resolve_handle(h2) == -1, 90.0) r.check(gone < 90.0 and s.lost_animals.size() == 1 and loot.count(S.LOST, C.ANIMAL) == 1, "an unopposed thief carries an animal off the farm: that one, and only that one, is lost (%.1f s)" % gone) 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 loose where it fell (#21: no longer penned, but it stays on the farm instead of running ## for the edge); 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.LOOSE and sim.animals.position(index).distance_to(at) < 0.2 and not s.is_animal_penned(index), "killing the carrier drops the animal where it fell: loose (no longer penned), not lost") _step(sim, 1200) r.check(loot.status[item] == S.LOOSE and not s.is_animal_lost(index) and sim.zones.playable.has_point(sim.animals.position(index)), "20 s later it's still loose on the farm (it doesn't run for the edge)") 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") _make_loose(sim, index, p) # loose again for the next part 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. An animal dropped inside the pen (its carrier ## stopped there) is home again. 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.LOOSE: 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.LOOSE 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") # #21: loose animals never wander into the pen by themselves, but one dropped inside it (its # carrier stopped there) is home again, and penned. _events.clear() _make_loose(sim, loose[3], Vector2(-1.0, -2.5)) _step(sim, 1) r.check(loot.status[loot.animal_item(loose[3])] == S.HOME and s.is_animal_penned(loose[3]) and _has(&"returned", loot.animal_item(loose[3])), "an animal dropped inside the pen is home again") player.global_position = AWAY _step(sim, 1) ## With the gate down at night, penned animals get out of the pen one at a time: each is loose at ## once (#21: no longer penned), walks out through the opening, and roams the farm; none is lost ## without a thief. By day an open pen keeps its animals. The next morning the ones that got out ## are back at their loose spots (not in the pen) and the rest in the pen. An unopposed thief still ## carries one off the farm; 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 s := c.state var pen := sim.zones.zone(FarmZone.Kind.ANIMAL_PEN).rect() 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 := s.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 gets out of the open pen (%.1f s)" % first) var out_item: int = _events.filter(func(e: Array) -> bool: return e[0] == &"strayed")[0][1] var out_id := loot.animal_index(out_item) r.check(loot.status[out_item] == S.LOOSE and not s.is_animal_penned(out_id), "it's loose at once (no longer penned), not dropped or lost") 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 walked := _until(sim, func() -> bool: return not pen.grow(0.75).has_point(sim.animals.position(out_id)), 30.0) r.check(walked < 30.0 and loot.status[out_item] == S.LOOSE, "the first walks out of the pen through the opening") _step(sim, 80 * 60) var roaming := 0 var on_farm := true for id in range(1, sim.animals.count + 1): if loot.status[loot.animal_item(id)] == S.LOOSE: roaming += 1 var at := sim.animals.position(id) on_farm = on_farm and sim.zones.playable.has_point(at) and not pen.has_point(at) r.check(s.phase == P.NIGHT and roaming >= 2 and on_farm and s.lost_animals == morning_lost and loot.count(S.LOST, C.ANIMAL) == morning_lost.size(), "late in the night %d animals that got out roam the farm outside the pen, and none is lost" % roaming) c.advance_phase() c.advance_phase() var penned := s.penned_animals.size() r.check(s.phase == P.MORNING and s.lost_animals == morning_lost and loot.count(S.HOME, C.ANIMAL) == penned and loot.count(S.LOOSE, C.ANIMAL) == loot.animal_count - morning_lost.size() - penned and penned < loot.animal_count - morning_lost.size(), "the next morning the ones that got out are still loose (not put back in the pen), the rest penned") 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 _at_loose_spots(sim) and sim.animals.pen_open, "every penned animal is in the pen and every loose one at its spot (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 = s.night_log[s.night_log.size() - 1] r.check(e["animals_lost"] == 0 and e["animals_strayed"] >= 2, "the night's record: %s" % [e]) # Retry: lose one to a thief tonight (the only way to lose an animal), fall, and the morning # comes back. The thief is sent in amid the loose spots: it takes a loose animal in plain view, # or breaks into the pen (right behind it) for a penned one. (A thief after a loose animal it # can't see heads for the pen meanwhile and gives up on it after a while.) var still_lost := s.lost_animals.duplicate() var morning := s.to_dict() c.begin_day() s.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, Vector2(1.5, 0.5)) _until(sim, func() -> bool: return sim.enemies.resolve_handle(h) == -1, 90.0) r.check(s.lost_animals.size() >= still_lost.size() + 1, "an unopposed animal thief carries one off the farm") s.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 its carrier was stopped there (#21: LOOSE and no longer penned; ## inside the pen it's home again on the next tick). func _make_loose(sim: FarmSim, id: int, p: Vector2) -> void: sim.loot.animal_loose(sim.loot.animal_item(id), p) sim.animals.place(id, p) ## Every loose animal stands at its own GameConfig.animal_loose_spots spot. func _at_loose_spots(sim: FarmSim) -> bool: var spots := sim.config.animal_loose_spots for id in range(1, sim.animals.count + 1): if sim.loot.status[sim.loot.animal_item(id)] == S.LOOSE \ and sim.animals.position(id).distance_to(spots[(id - 1) % spots.size()]) > 0.01: return false return true 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_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