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

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