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 <noreply@anthropic.com>
This commit is contained in:
Forrest 2026-10-05 15:13:20 -04:00
parent 186c9c44cb
commit 13efe6603f
7 changed files with 252 additions and 41 deletions

View file

@ -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: