From 13efe6603f839bb1b0da50d7a72692d4b7965940 Mon Sep 17 00:00:00 2001 From: Forrest Date: Mon, 5 Oct 2026 15:13:20 -0400 Subject: [PATCH 1/2] Task #12: animals start loose; herd into the player-built pen Adds LootSystem.Status.LOOSE: animals now start scattered at GameConfig. animal_loose_spots outside the pen rect and graze in place (AnimalSystem, anchored to their own spot instead of the pen) instead of starting HOME or escaping down the field. A thief can still claim and carry a loose animal off (same DROPPED/escape rules as before); the scarecrow's existing follow-trail recovery now also picks up a loose animal directly, and entering the pen makes it HOME as before. FarmState gains penned_animals (which ids are currently penned), set whenever an animal becomes HOME (led in, or auto-returned at the dawn grace) and cleared when it's lost. LootSystem.sync_from_state() uses it each morning/retry/load to restore penned animals HOME and leave the rest LOOSE, instead of resetting the whole herd into the pen. Bumped FarmState. VERSION to 5 with a migration step that pens every surviving animal for older saves (which always kept them in the pen). test_targets.gd's existing pen-mechanics suite (containment, straying, leading in) now pens every animal up front to keep covering that behavior; a new _loose_start check covers the loose start itself: animals spawn loose and scattered, graze in place, a thief can still claim and steal a loose one, leading one in with the follow-trail mechanic pens it, and a morning resync keeps the penned one home and the rest loose. Co-Authored-By: Claude Sonnet 5 --- game/scripts/animal_system.gd | 74 ++++++++++++++++-------- game/scripts/farm_state.gd | 53 ++++++++++++++++-- game/scripts/game_config.gd | 8 +++ game/scripts/loot_system.gd | 40 +++++++++---- game/scripts/save_game.gd | 13 +++++ game/tests/test_farm_state.gd | 3 + game/tests/test_targets.gd | 102 +++++++++++++++++++++++++++++++++- 7 files changed, 252 insertions(+), 41 deletions(-) diff --git a/game/scripts/animal_system.gd b/game/scripts/animal_system.gd index 1ec2af1..d91add3 100644 --- a/game/scripts/animal_system.gd +++ b/game/scripts/animal_system.gd @@ -2,18 +2,24 @@ class_name AnimalSystem extends Node3D ## The pen's animals as moving entities. Their theft state lives in LootSystem; this moves and ## shows them: -## in the pen (HOME) wander between random spots in the pen, pausing to graze; one a thief is -## catching stands still. At night (night and dawn), while the pen is open (a -## way out that crosses no structure: its gate broken or taken down), one calm -## animal wanders out every animal_wander_interval and is loose. -## loose (DROPPED) dropped by its thief, or wandered out: walks toward the farm's nearest edge -## along the escape field at stray_speed, stopping to graze now and then, and -## can't pass structures. At the edge it is lost; inside the pen it is home -## again. -## following walks the scarecrow's trail (so it goes wherever the scarecrow went, gates -## included), in line behind it. Inside the pen it is home (returned) and calm -## for animal_calm_time. -## carried held under its thief, wriggling. +## in the pen (HOME) wander between random spots in the pen, pausing to graze; one a thief is +## catching stands still. At night (night and dawn), while the pen is open (a +## way out that crosses no structure: its gate broken or taken down), one calm +## animal wanders out every animal_wander_interval and is loose (DROPPED). +## grazing (LOOSE) #12: where every animal starts, scattered at its own GameConfig. +## animal_loose_spots spot outside the pen. Same calm wander as a penned +## animal, but anchored to that spot instead of the pen, and never walks the +## escape field on its own. A thief may still claim and carry it off (then +## it's DROPPED, below); the scarecrow walking up to it starts it following, +## same as a dropped one. +## loose (DROPPED) dropped by its thief, or wandered out of an open pen: walks toward the +## farm's nearest edge along the escape field at stray_speed, stopping to +## graze now and then, and can't pass structures. At the edge it is lost; +## inside the pen it is home again. +## following walks the scarecrow's trail (so it goes wherever the scarecrow went, gates +## included), in line behind it. Inside the pen it is home (returned) and calm +## for animal_calm_time. +## carried held under its thief, wriggling. ## The farm pack's animals are static meshes, so walking is procedural: chickens hop, pigs and ## sheep waddle. Each scene animal (a farm-pack prefab under the pen zone) is reparented under a ## pivot at the middle of its body. @@ -25,6 +31,8 @@ const FOLLOW_GAP := 1.0 const FOLLOW_FIRST := 1.5 ## A follower whose scarecrow got this far away (a retry, a fall) is loose again. const FOLLOW_LEASH := 20.0 +## A grazing (LOOSE) animal's wander goals stay within this many meters of its loose spot. +const LOOSE_WANDER_RADIUS := 0.8 ## The pen opened (its gate broken or taken down) or was shut again. signal pen_changed(open: bool) @@ -59,6 +67,9 @@ var kinds: Array[String] = [] var _start := PackedVector2Array() var _start_yaw := PackedFloat32Array() +## #12: each animal's grazing spot outside the pen, where it starts (GameConfig.animal_loose_spots, +## cycled if there are more animals than spots) and wanders around while LOOSE. +var _loose_spot := PackedVector2Array() var _pivots: Array[Node3D] = [] var _meshes: Array = [] # per animal: Array[MeshInstance3D] ## Co-op: the scarecrows animals can follow (player 1's, player 2's), each with its trail, and @@ -95,7 +106,7 @@ func setup(p_campaign: CampaignController, pen_zone: FarmZone, p_nav: FarmNaviga var found := find_animals(pen_zone) count = found.size() var n := count + 1 - for a: PackedVector2Array in [pos, prev, goal, _start]: + for a: PackedVector2Array in [pos, prev, goal, _start, _loose_spot]: a.resize(n) for a: PackedFloat32Array in [yaw, speed, timer, calm, phase, _start_yaw]: a.resize(n) @@ -125,22 +136,26 @@ func setup(p_campaign: CampaignController, pen_zone: FarmZone, p_nav: FarmNaviga _meshes[id] = meshes _start[id] = Vector2(world.x, world.z) _start_yaw[id] = atan2(forward.x, forward.z) + _loose_spot[id] = config.animal_loose_spots[(id - 1) % config.animal_loose_spots.size()] \ + if not config.animal_loose_spots.is_empty() else _start[id] phase[id] = _rng.randf() * TAU sync_from_state() -## Every animal back at its spot in the pen (or hidden if lost), nobody following (new morning, -## retry). +## Every animal back at its spot -- in the pen if penned, or its loose spot outside it otherwise +## (or hidden if lost) -- nobody following (new morning, retry). func sync_from_state() -> void: for k in _trails.size(): _trails[k] = PackedVector2Array() _wander_wait = config.animal_wander_interval * 0.3 for id in range(1, count + 1): - pos[id] = _start[id] - prev[id] = _start[id] + var item := loot.animal_item(id) + var home_spot := _start[id] if loot.status[item] == LootSystem.Status.HOME else _loose_spot[id] + pos[id] = home_spot + prev[id] = home_spot yaw[id] = _start_yaw[id] speed[id] = 0.0 - goal[id] = _start[id] + goal[id] = home_spot timer[id] = _rng.randf_range(0.5, 3.0) calm[id] = 0.0 slot[id] = -1 @@ -251,6 +266,9 @@ func step(dt: float) -> void: LootSystem.Status.HOME: slot[id] = -1 _wander(id, item, dt) + LootSystem.Status.LOOSE: + slot[id] = -1 + _wander(id, item, dt) LootSystem.Status.DROPPED: slot[id] = -1 if not in_pen(pos[id]): @@ -290,7 +308,7 @@ func _wander(id: int, item: int, dt: float) -> void: timer[id] -= dt if speed[id] == 0.0: if timer[id] <= 0.0: - goal[id] = _wander_goal() + goal[id] = _wander_goal(id, item) speed[id] = float(WANDER_SPEED.get(kinds[id], 0.7)) timer[id] = 8.0 # give up on a goal it can't reach return @@ -366,13 +384,23 @@ func _rest(id: int) -> void: timer[id] = _rng.randf_range(1.5, 4.0) -func _wander_goal() -> Vector2: - var area := pen.grow(-1.2) +## A HOME animal wanders inside the pen; a LOOSE one grazes within LOOSE_WANDER_RADIUS of its own +## loose spot instead. +func _wander_goal(id: int, item: int) -> Vector2: + if loot.status[item] == LootSystem.Status.HOME: + var area := pen.grow(-1.2) + for attempt in 12: + var p := Vector2(_rng.randf_range(area.position.x, area.end.x), _rng.randf_range(area.position.y, area.end.y)) + if nav.is_walkable(nav.cell_of(p.x, p.y)): + return p + return area.get_center() + var center := _loose_spot[id] for attempt in 12: - var p := Vector2(_rng.randf_range(area.position.x, area.end.x), _rng.randf_range(area.position.y, area.end.y)) + var p := center + Vector2(_rng.randf_range(-LOOSE_WANDER_RADIUS, LOOSE_WANDER_RADIUS), + _rng.randf_range(-LOOSE_WANDER_RADIUS, LOOSE_WANDER_RADIUS)) if nav.is_walkable(nav.cell_of(p.x, p.y)): return p - return area.get_center() + return center ## At night, while the pen is open, a calm animal walks out now and then. diff --git a/game/scripts/farm_state.gd b/game/scripts/farm_state.gd index 0b3022b..555f001 100644 --- a/game/scripts/farm_state.gd +++ b/game/scripts/farm_state.gd @@ -8,12 +8,12 @@ extends RefCounted ## pouch live in resources). Gather points below full, bought upgrades, and the built structures ## are kept too. -const VERSION := 4 +const VERSION := 5 const KEYS: Array[String] = [ "night", "nights_survived", "phase", "phase_time_left", "scarecrow_max_hp", "scarecrow_hp", - "crops_total", "lost_crops", "animals_total", "lost_animals", "supplies_total", "shed_raided", - "resources", "gather_charges", "upgrades", "structures", "next_structure_id", "night_events", - "night_log", "coop", "partner_max_hp", "partner_hp", + "crops_total", "lost_crops", "animals_total", "lost_animals", "penned_animals", "supplies_total", + "shed_raided", "resources", "gather_charges", "upgrades", "structures", "next_structure_id", + "night_events", "night_log", "coop", "partner_max_hp", "partner_hp", ] const STRUCTURE_KEYS: Array[String] = ["id", "type", "x", "z", "rot", "hp"] ## Events counted from the morning to the end of the dawn grace, copied into the night's log @@ -65,6 +65,10 @@ var lost_crops := PackedInt32Array() var animals_total: int = 0 ## Ids of the animals that are gone for good (carried or wandered off the farm), sorted. var lost_animals := PackedInt32Array() +## Ids of the animals currently penned (#12): led into a built pen (or auto-returned there after +## being dropped), so sync_from_state() keeps them LootSystem.Status.HOME each morning; an id not +## here (and not lost) is loose, grazing outside the pen until led in. Never overlaps lost_animals. +var penned_animals := PackedInt32Array() ## The shed's stock when the vacation started (0 = supplies not in play). var supplies_total: int = 0 ## The shed was last emptied by thieves (not by spending). Only then do empty supplies count @@ -160,6 +164,7 @@ func lose_animal(id: int) -> bool: return false lost_animals.append(id) lost_animals.sort() + unpen_animal(id) return true @@ -168,6 +173,35 @@ func lose_all_animals() -> void: lose_animal(id) +func is_animal_penned(id: int) -> bool: + return penned_animals.has(id) + + +## Marks an animal as penned (led into the pen, or auto-returned there). Returns false for an +## unknown, already-penned, or lost animal. +func pen_animal(id: int) -> bool: + if id < 1 or id > animals_total or penned_animals.has(id) or lost_animals.has(id): + return false + penned_animals.append(id) + penned_animals.sort() + return true + + +## Marks an animal as loose again. Returns false if it wasn't penned. +func unpen_animal(id: int) -> bool: + var i := penned_animals.find(id) + if i < 0: + return false + penned_animals.remove_at(i) + return true + + +## Debug and tests: every surviving animal penned at once (the old always-in-the-pen behavior). +func pen_all_animals() -> void: + for id in range(1, animals_total + 1): + pen_animal(id) + + ## Debug and tests: every asset gone, the shed emptied by thieves. func strip() -> void: lose_all_crops() @@ -263,6 +297,9 @@ func to_dict() -> Dictionary: var lost_a: Array = [] for id in lost_animals: lost_a.append(id) + var penned: Array = [] + for id in penned_animals: + penned.append(id) return { "version": VERSION, "night": night, @@ -275,6 +312,7 @@ func to_dict() -> Dictionary: "lost_crops": lost, "animals_total": animals_total, "lost_animals": lost_a, + "penned_animals": penned, "supplies_total": supplies_total, "shed_raided": shed_raided, "resources": resources.to_dict(), @@ -301,7 +339,8 @@ static func from_dict(d: Dictionary) -> FarmState: var phase_value := int(d["phase"]) if not CampaignController.Phase.values().has(phase_value): return null - if not (d["night_log"] is Array and d["lost_crops"] is Array and d["lost_animals"] is Array and d["night_events"] is Dictionary + if not (d["night_log"] is Array and d["lost_crops"] is Array and d["lost_animals"] is Array and d["penned_animals"] is Array + and d["night_events"] is Dictionary and d["gather_charges"] is Dictionary and d["upgrades"] is Dictionary and d["structures"] is Array): return null var store := ResourceStore.from_dict(d["resources"]) @@ -325,6 +364,9 @@ static func from_dict(d: Dictionary) -> FarmState: for id in d["lost_animals"]: if not s.lose_animal(int(id)): return null + for id in d["penned_animals"]: + if not s.pen_animal(int(id)): + return null # unknown, duplicate, or already-lost animal id s.supplies_total = int(d["supplies_total"]) s.shed_raided = bool(d["shed_raided"]) s.resources = store @@ -394,4 +436,5 @@ func is_valid() -> bool: and partner_max_hp >= 1 and partner_hp >= 0 and partner_hp <= partner_max_hp \ and crops_total >= 0 and lost_crops.size() <= crops_total \ and animals_total >= 0 and lost_animals.size() <= animals_total \ + and penned_animals.size() <= animals_total - lost_animals.size() \ and supplies_total >= 0 and supplies_in_transit >= 0 diff --git a/game/scripts/game_config.gd b/game/scripts/game_config.gd index 47414ea..b32fac7 100644 --- a/game/scripts/game_config.gd +++ b/game/scripts/game_config.gd @@ -85,6 +85,14 @@ extends Resource @export var animal_calm_time: float = 30.0 ## Animals following the scarecrow walk its trail at up to this speed. @export var follow_speed: float = 6.0 +## Where loose animals start (#12): scattered open-farmyard spots outside FarmZone.Kind.ANIMAL_PEN +## (x -10..4, z -11..-1), walkable, and clear of the future crop fields (x -11..-1 z 2..12 and +## x 2..12 z 2..12 -- see LAYOUT.md). Each is grazed around (AnimalSystem.LOOSE_WANDER_RADIUS) +## until the player leads that animal into a built pen. Cycles if there are more animals than spots. +@export var animal_loose_spots: Array[Vector2] = [ + Vector2(-9.0, 0.5), Vector2(-6.0, 0.5), Vector2(-3.0, 0.5), Vector2(0.0, 0.5), Vector2(3.0, 0.5), + Vector2(-12.0, -3.0), Vector2(-12.0, -6.0), Vector2(-12.0, -9.0), +] ## What a thief with no set target goes for ("any" in a night plan), and where a thief whose target ## is gone goes next: relative weights per category. @export var target_weights: Dictionary = {"crops": 2.0, "animals": 1.0, "supplies": 1.0} diff --git a/game/scripts/loot_system.gd b/game/scripts/loot_system.gd index a0b853e..50cdafa 100644 --- a/game/scripts/loot_system.gd +++ b/game/scripts/loot_system.gd @@ -17,6 +17,12 @@ extends Node3D ## recover it (a loose animal then follows it, at most max_following_animals at once), ## and another thief may claim it. ## FOLLOWING animals only: following the scarecrow; back inside the pen it is home. +## LOOSE animals only (#12): starts here, scattered outside the pen, grazing calmly in place +## (AnimalSystem) instead of making for the edge. A thief may still claim and carry one +## off (then it's DROPPED, same as any theft: it runs for the edge). The scarecrow +## walking up to it starts it following, same as a dropped animal; entering the pen +## makes it HOME, and FarmState records it as penned so sync_from_state() keeps it +## home each morning instead of resetting it loose. ## LOST carried (or, for an animal, wandered) off the farm. Crops and animals are recorded ## in FarmState and gone for the rest of the run; a sack's contents are gone and the ## sack is free for the next theft. @@ -26,7 +32,7 @@ extends Node3D ## recoveries, strays, and supply losses count toward the night's record. enum Category { CROP, ANIMAL, SUPPLY } -enum Status { HOME, CARRIED, DROPPED, LOST, FOLLOWING } +enum Status { HOME, CARRIED, DROPPED, LOST, FOLLOWING, LOOSE } enum Threat { NONE, TARGETED, CARRIED, DROPPED } ## EnemySystem.want for a thief with no set target (it goes by GameConfig.target_weights). @@ -174,7 +180,13 @@ func sync_from_state() -> void: status[id] = Status.LOST if s.is_crop_lost(id) else Status.HOME _units[id].visible = status[id] == Status.HOME Category.ANIMAL: - status[id] = Status.LOST if s.is_animal_lost(animal_index(id)) else Status.HOME + var aid := animal_index(id) + if s.is_animal_lost(aid): + status[id] = Status.LOST + elif s.is_animal_penned(aid): + status[id] = Status.HOME + else: + status[id] = Status.LOOSE Category.SUPPLY: status[id] = Status.HOME sack_amount[id] = 0 @@ -238,7 +250,7 @@ func first_sack() -> int: func is_claimed_by(id: int, handle: int) -> bool: return is_valid_unit(id) and handle >= 0 and holder[id] == handle \ - and (status[id] == Status.HOME or status[id] == Status.DROPPED) + and (status[id] == Status.HOME or status[id] == Status.DROPPED or status[id] == Status.LOOSE) ## Where a thief has to go for this item: its plot, the animal itself, the shed pile, or where it @@ -290,14 +302,14 @@ func claim(handle: int, from: Vector2, nav: FarmNavigation, view_range: float, w return best -## Whether a thief could claim anything at home in this category right now. +## Whether a thief could claim anything at home (or, for an animal, loose) in this category now. func available(c: Category) -> bool: match c: Category.SUPPLY: return campaign.state.resources.shed_total() > 0 and _free_sack() > 0 _: for id in _ids(c): - if status[id] == Status.HOME and holder[id] == -1: + if (status[id] == Status.HOME or status[id] == Status.LOOSE) and holder[id] == -1: return true return false @@ -368,10 +380,14 @@ func escape(id: int, handle: int) -> void: _lose(id) -## The scarecrow got a dropped item back: a crop returns to its plot, a sack to the shed, and a -## loose animal starts following (if the scarecrow has room for another). +## The scarecrow got a dropped (or loose, #12) item back: a crop returns to its plot, a sack to +## the shed, and an animal starts following (if the scarecrow has room for another) -- a dropped +## one recovered from a thief exactly as before, and a loose one led in for the first time. func recover(id: int) -> bool: - if not is_valid_unit(id) or status[id] != Status.DROPPED: + if not is_valid_unit(id): + return false + var loose_animal := category[id] == Category.ANIMAL and status[id] == Status.LOOSE + if status[id] != Status.DROPPED and not loose_animal: return false var s := campaign.state match category[id]: @@ -406,6 +422,7 @@ func animal_home(id: int) -> void: status[id] = Status.HOME holder[id] = -1 progress[id] = 0.0 + campaign.state.pen_animal(animal_index(id)) if led: campaign.state.record_event("animals_recovered") loot_event.emit(&"recovered" if led else &"returned", id, _world(position_of(id))) @@ -465,7 +482,8 @@ func step(scarecrow: Vector2, who: int = 0) -> void: recoverer = who var r2 := config.pickup_radius * config.pickup_radius for id in range(1, item_count + 1): - if status[id] != Status.DROPPED: + var recoverable := status[id] == Status.DROPPED or (category[id] == Category.ANIMAL and status[id] == Status.LOOSE) + if not recoverable: _warned[id] = 0 continue var near := position_of(id).distance_squared_to(scarecrow) <= r2 @@ -629,7 +647,7 @@ func _claim_home(c: int) -> int: return sack var free: Array[int] = [] for id in _ids(c): - if status[id] == Status.HOME and holder[id] == -1: + if (status[id] == Status.HOME or status[id] == Status.LOOSE) and holder[id] == -1: free.append(id) if free.is_empty(): return -1 @@ -681,6 +699,8 @@ func _return_home(id: int) -> void: progress[id] = 0.0 if category[id] == Category.CROP: _units[id].visible = true + elif category[id] == Category.ANIMAL: + campaign.state.pen_animal(animal_index(id)) func _world(p: Vector2) -> Vector3: diff --git a/game/scripts/save_game.gd b/game/scripts/save_game.gd index 3b34ae1..626756a 100644 --- a/game/scripts/save_game.gd +++ b/game/scripts/save_game.gd @@ -88,6 +88,19 @@ static func migrate(d: Dictionary) -> Dictionary: out["partner_max_hp"] = int(out.get("scarecrow_max_hp", 10)) out["partner_hp"] = int(out.get("scarecrow_max_hp", 10)) out["version"] = 4 + 4: + # Version 5 adds the Animal Pen's loose/penned state (#12). Older saves always kept + # every surviving animal in the pen, so migrate them in as already penned. + var total := int(out.get("animals_total", 0)) + var lost := {} # id (int) -> true; Array.has() won't match a float to an int + for raw: Variant in out.get("lost_animals", []): + lost[int(raw)] = true + var penned: Array = [] + for id in range(1, total + 1): + if not lost.has(id): + penned.append(id) + out["penned_animals"] = penned + out["version"] = 5 _: return {} return out diff --git a/game/tests/test_farm_state.gd b/game/tests/test_farm_state.gd index bf9032c..6f7f568 100644 --- a/game/tests/test_farm_state.gd +++ b/game/tests/test_farm_state.gd @@ -74,6 +74,9 @@ func _rejects(r: GameTestRunner, good: Dictionary) -> void: "an unknown night event": func(d: Dictionary) -> void: d["night_events"] = {"cows_tipped": 1}, "a lost animal id beyond the total": func(d: Dictionary) -> void: d["lost_animals"] = [d["animals_total"] + 1], "an animal lost twice": func(d: Dictionary) -> void: d["lost_animals"] = [2, 2], + "a penned animal id beyond the total": func(d: Dictionary) -> void: d["penned_animals"] = [d["animals_total"] + 1], + "an animal penned twice": func(d: Dictionary) -> void: d["penned_animals"] = [1, 1], + "a penned animal that's also lost": func(d: Dictionary) -> void: d["penned_animals"] = [2], # 2 is already lost in `good` "negative supplies in play": func(d: Dictionary) -> void: d["supplies_total"] = -1, "HP above max": func(d: Dictionary) -> void: d["scarecrow_hp"] = d["scarecrow_max_hp"] + 1, "night 0": func(d: Dictionary) -> void: d["night"] = 0, diff --git a/game/tests/test_targets.gd b/game/tests/test_targets.gd index 4a9d975..bd07f51 100644 --- a/game/tests/test_targets.gd +++ b/game/tests/test_targets.gd @@ -1,7 +1,9 @@ extends RefCounted -## All three farm targets on the real farm, with FarmSim stepped by hand at 60 Hz: 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 +## 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 @@ -18,6 +20,7 @@ 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 @@ -31,6 +34,12 @@ func run(r: GameTestRunner) -> void: 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) @@ -46,6 +55,93 @@ func run(r: GameTestRunner) -> void: await r.process_frame +## Task #12: animals start loose, scattered outside the pen, grazing in place; a thief can still +## claim and steal one (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 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 From ffeec3087365a44a36582e72c97d7e154fb0939d Mon Sep 17 00:00:00 2001 From: Forrest Date: Mon, 5 Oct 2026 15:20:58 -0400 Subject: [PATCH 2/2] 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 --- game/scripts/alert_manager.gd | 2 +- game/tests/test_targets.gd | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/game/scripts/alert_manager.gd b/game/scripts/alert_manager.gd index f9daccb..e1a9db2 100644 --- a/game/scripts/alert_manager.gd +++ b/game/scripts/alert_manager.gd @@ -85,7 +85,7 @@ func collect() -> Array[Dictionary]: continue var p := Vector2(enemies.pos_x[d], enemies.pos_z[d]) raw.append(_alert(Kind.ESCAPE, enemies.type[d], loot.category[id], p, _escalation(d, p))) - LootSystem.Status.HOME: + LootSystem.Status.HOME, LootSystem.Status.LOOSE: var d := enemies.resolve_handle(loot.holder[id]) if loot.holder[id] >= 0 else -1 if d >= 0: raw.append(_alert(Kind.THEFT, enemies.type[d], loot.category[id], loot.position_of(id), 0.0)) diff --git a/game/tests/test_targets.gd b/game/tests/test_targets.gd index bd07f51..ab564af 100644 --- a/game/tests/test_targets.gd +++ b/game/tests/test_targets.gd @@ -56,9 +56,10 @@ func run(r: GameTestRunner) -> void: ## Task #12: animals start loose, scattered outside the pen, grazing in place; a thief can still -## claim and steal one (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. +## 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) @@ -99,6 +100,9 @@ func _loose_start(r: GameTestRunner) -> void: 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)