Task #21: animals roam the whole farm, never lost on their own

Loose animals now roam the whole playable map, day and night. Each walk
goes to a goal 4-8 m away that is walkable, in a clear straight line,
at least 2 m inside FarmZones.playable (passed into AnimalSystem.setup),
and neither inside the pen nor across it. Every morning a loose animal
is back at its loose spot (no save change).

Animals no longer escape on their own:
- _stray and LootSystem.animal_escaped are gone.
- A new LootSystem.animal_loose makes an animal LOOSE where it stands
  and unpens it. It covers a carrier killed or scared (drop), a broken
  follow leash, and an animal getting out of an open pen (animal_strays,
  which still records animals_strayed).
- An animal dropped inside the pen is home again.
- An animal let out of an open pen walks out along the escape field,
  then roams. If the pen shuts first, it stays home.
- return_dropped still pens followers at dawn's end, but leaves loose
  animals loose.
- The only loss paths left are a carrier leaving the farm
  (thief escape, or removal with reason LEFT).

Loose spots at x = -12 move inward to (6|9|12, 0.5). The toast,
review-panel, dropped-animal toast, RULES.md and header text no longer
promise animals wander off or run for the edge.

test_targets: _loose_start has a 90 s day + 90 s night roam run (every
animal >4 m, never off the map or into the pen, none lost), a morning
reset to the spots, a killed carrier leaving the animal loose, and an
unopposed thief carrying one off. _animal_theft, _leading, _breach and
_make_loose follow the new rules. AWAY moves off the map so roaming
animals can't start following the parked scarecrow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Forrest 2026-10-06 13:07:13 -04:00
parent 26b735d3e6
commit aaf5a81b4e
8 changed files with 379 additions and 226 deletions

View file

@ -1,25 +1,30 @@
class_name AnimalSystem
extends Node3D
## The pen's animals as moving entities. Their theft state lives in LootSystem; this moves and
## The farm's animals as moving entities. Their theft state lives in LootSystem; this moves and
## shows them:
## loose (LOOSE) #12, #21: where every animal starts, at its own GameConfig.animal_loose_spots
## spot outside the pen, and where a loose one is back every morning. It roams
## the whole farm, day and night: each walk goes to a spot ROAM_MIN..ROAM_MAX
## meters away that it can reach in a straight line, inside the playable map
## (ROAM_MARGIN in from its edge) and outside the pen, and then it stops to
## graze. Structures stop it. It never leaves the farm on its own and never
## walks into the pen by itself. A thief may claim and carry it off; a stopped
## carrier drops it loose where it fell (inside the pen, it's home again). The
## scarecrow walking up to it starts it following.
## 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.
## way out that crosses no structure: nothing built yet, or its gate broken or
## taken down), one calm animal gets out every animal_wander_interval: it's
## loose at once (and no longer penned), walks out of the pen along the escape
## field at stray_speed, and then roams like any loose animal. If the pen shuts
## before it's out, it stays home.
## 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.
## for animal_calm_time. A follower whose scarecrow got FOLLOW_LEASH away is
## loose where it stands.
## carried held under its thief, wriggling.
## An animal is never left DROPPED (that's for crops and sacks), and the only way to lose one is a
## thief carrying it off the farm (LootSystem.escape).
## 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.
@ -31,8 +36,15 @@ 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
## #21: a loose (LOOSE) animal's next wander goal is this many meters from where it stands...
const ROAM_MIN := 4.0
const ROAM_MAX := 8.0
## ...and at least this far inside the playable map's edge.
const ROAM_MARGIN := 2.0
## Loose animals keep this far out of the pen's rectangle (goals), and never cut across it.
const PEN_KEEP_OUT := 1.0
## Seconds an animal walking out of an open pen keeps at it before it settles back in.
const LEAVE_TIME := 30.0
## The pen opened (its gate broken or taken down) or was shut again.
signal pen_changed(open: bool)
@ -44,7 +56,10 @@ var loot: LootSystem
var enemies: EnemySystem
var player: Node3D
var pen: Rect2
## Set by FarmSim: the field loose animals walk along, toward the farm's nearest edge.
## The playable map (FarmZones.playable): loose animals roam inside it.
var farm_rect := Rect2()
## Set by FarmSim: the field toward the farm's nearest edge. An animal getting out of an open pen
## walks down it until it's out of the pen (and no further).
var escape_field := PackedInt32Array()
var count: int = 0
## There's a way out of the pen that crosses no structure.
@ -61,14 +76,14 @@ var calm := PackedFloat32Array()
var phase := PackedFloat32Array()
## Order in the line behind the scarecrow (0 = first), -1 when not following.
var slot := PackedInt32Array()
## Wandering out of the open pen and not out yet (so being inside doesn't count as home).
## Loose and walking out of the open pen, not out yet (so being inside doesn't make it home).
var leaving := PackedByteArray()
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.
## #12: each animal's spot outside the pen (GameConfig.animal_loose_spots, cycled if there are
## more animals than spots), where it starts and where it's back every morning while LOOSE.
var _loose_spot := PackedVector2Array()
var _pivots: Array[Node3D] = []
var _meshes: Array = [] # per animal: Array[MeshInstance3D]
@ -92,8 +107,10 @@ static func find_animals(pen_zone: Node3D) -> Array[Node3D]:
return out
## p_farm_rect: the playable map (FarmZones.playable), where loose animals roam; empty means the
## whole navigation grid.
func setup(p_campaign: CampaignController, pen_zone: FarmZone, p_nav: FarmNavigation, p_loot: LootSystem,
p_enemies: EnemySystem, p_player: Node3D) -> void:
p_enemies: EnemySystem, p_player: Node3D, p_farm_rect: Rect2 = Rect2()) -> void:
campaign = p_campaign
config = p_campaign.config
nav = p_nav
@ -102,6 +119,8 @@ func setup(p_campaign: CampaignController, pen_zone: FarmZone, p_nav: FarmNaviga
player = p_player
leaders = [p_player]
pen = pen_zone.rect()
farm_rect = p_farm_rect if p_farm_rect.has_area() \
else Rect2(nav.origin_x, nav.origin_z, nav.width * nav.cell_size, nav.height * nav.cell_size).grow(-nav.cell_size)
_rng.seed = 8061
var found := find_animals(pen_zone)
count = found.size()
@ -142,8 +161,8 @@ func setup(p_campaign: CampaignController, pen_zone: FarmZone, p_nav: FarmNaviga
sync_from_state()
## 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).
## Every animal back at its spot -- in the pen if penned, or its loose spot outside it otherwise,
## wherever it roamed (or hidden if lost) -- nobody following (new morning, retry).
func sync_from_state() -> void:
for k in _trails.size():
_trails[k] = PackedVector2Array()
@ -200,7 +219,8 @@ func place(id: int, p: Vector2) -> void:
pos[id] = p
prev[id] = p
speed[id] = 0.0
timer[id] = _rng.randf_range(0.6, 1.2) # a moment's shock before it bolts
leaving[id] = 0
timer[id] = _rng.randf_range(0.6, 1.2) # a moment's shock before it wanders off
func start_following(id: int) -> void:
@ -266,18 +286,17 @@ func step(dt: float) -> void:
LootSystem.Status.HOME:
slot[id] = -1
_wander(id, item, dt)
LootSystem.Status.LOOSE:
LootSystem.Status.LOOSE, LootSystem.Status.DROPPED:
slot[id] = -1
_wander(id, item, dt)
LootSystem.Status.DROPPED:
slot[id] = -1
if not in_pen(pos[id]):
leaving[id] = 0
if in_pen(pos[id]) and leaving[id] == 0:
loot.animal_home(item)
if loot.status[item] == LootSystem.Status.DROPPED:
loot.animal_loose(item, pos[id]) # #21: an animal is never left lying dropped
if leaving[id] == 1:
_leave_pen(id, item, dt)
elif pen.has_point(pos[id]):
loot.animal_home(item) # dropped inside the pen: it's home again
_rest(id)
else:
_stray(id, item, dt)
_wander(id, item, dt)
LootSystem.Status.FOLLOWING:
var k := clampi(leader[id], 0, at.size() - 1)
_follow(id, item, at[k], dt)
@ -311,6 +330,8 @@ func _wander(id: int, item: int, dt: float) -> void:
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
if loot.status[item] == LootSystem.Status.LOOSE:
timer[id] = maxf(timer[id], pos[id].distance_to(goal[id]) / speed[id] + 2.0) # a longer walk
return
var to := goal[id] - pos[id]
if to.length() < 0.2 or timer[id] <= 0.0:
@ -319,21 +340,21 @@ func _wander(id: int, item: int, dt: float) -> void:
_move(id, to.normalized(), speed[id], dt)
func _stray(id: int, item: int, dt: float) -> void:
if escape_field.size() > 0 and escape_field[nav.cell_of(pos[id].x, pos[id].y)] == 0:
loot.animal_escaped(item)
speed[id] = 0.0
return
## A loose animal that got out of the open pen walks down the escape field until it's out of the
## pen (then it roams like any loose animal). If the pen shuts first, or it can't get out in
## LEAVE_TIME, it settles: back home if it's still inside the pen.
func _leave_pen(id: int, item: int, dt: float) -> void:
var out := not pen.grow(0.75).has_point(pos[id])
timer[id] -= dt
if timer[id] <= 0.0:
if speed[id] > 0.0:
speed[id] = 0.0 # graze a moment
timer[id] = _rng.randf_range(0.8, 1.8)
else:
speed[id] = config.stray_speed
timer[id] = _rng.randf_range(3.0, 5.0)
if speed[id] > 0.0 and escape_field.size() > 0 and nav.flow_direction(escape_field, pos[id].x, pos[id].y, _dir):
if not out and pen_open and timer[id] > 0.0 and escape_field.size() > 0 \
and nav.flow_direction(escape_field, pos[id].x, pos[id].y, _dir):
speed[id] = config.stray_speed
_move(id, Vector2(_dir[0], _dir[1]), speed[id], dt)
return
leaving[id] = 0
if pen.has_point(pos[id]):
loot.animal_home(item)
_rest(id)
func _follow(id: int, item: int, sc: Vector2, dt: float) -> void:
@ -344,8 +365,7 @@ func _follow(id: int, item: int, sc: Vector2, dt: float) -> void:
_rest(id)
return
if pos[id].distance_to(sc) > FOLLOW_LEASH:
loot.status[item] = LootSystem.Status.DROPPED
loot.ground[item] = pos[id]
loot.animal_loose(item, pos[id])
slot[id] = -1
_rest(id)
return
@ -384,8 +404,10 @@ func _rest(id: int) -> void:
timer[id] = _rng.randf_range(1.5, 4.0)
## A HOME animal wanders inside the pen; a LOOSE one grazes within LOOSE_WANDER_RADIUS of its own
## loose spot instead.
## A HOME animal wanders inside the pen. A LOOSE one (#21) roams the farm: its next goal is
## ROAM_MIN..ROAM_MAX meters away, inside the playable map (ROAM_MARGIN in from the edge),
## walkable, in a clear straight line (no obstacle or structure in the way), and neither inside the
## pen (PEN_KEEP_OUT around it) nor across it. With no such goal it stays put a while.
func _wander_goal(id: int, item: int) -> Vector2:
if loot.status[item] == LootSystem.Status.HOME:
var area := pen.grow(-1.2)
@ -394,16 +416,35 @@ func _wander_goal(id: int, item: int) -> Vector2:
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 := 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 center
var from := pos[id]
var area := farm_rect.grow(-ROAM_MARGIN)
var keep_out := pen.grow(PEN_KEEP_OUT)
# Dropped off the map by a carrier: reach far enough to get back on.
var reach := ROAM_MAX + from.distance_to(Vector2(clampf(from.x, area.position.x, area.end.x),
clampf(from.y, area.position.y, area.end.y)))
for attempt in 16:
var p := from + Vector2.from_angle(_rng.randf() * TAU) * _rng.randf_range(ROAM_MIN, reach)
if not area.has_point(p) or keep_out.has_point(p) or not nav.is_walkable(nav.cell_of(p.x, p.y)):
continue
if _crosses(pen, from, p) or not nav.line_clear(from.x, from.y, p.x, p.y):
continue
return p
return from
## At night, while the pen is open, a calm animal walks out now and then.
## Whether the segment a..b touches the rectangle.
static func _crosses(r: Rect2, a: Vector2, b: Vector2) -> bool:
if r.has_point(a) or r.has_point(b):
return true
var corners := [r.position, Vector2(r.end.x, r.position.y), r.end, Vector2(r.position.x, r.end.y)]
for k in 4:
if Geometry2D.segment_intersects_segment(a, b, corners[k], corners[(k + 1) % 4]) != null:
return true
return false
## At night, while the pen is open, a calm animal gets out now and then: it's loose (LootSystem.
## animal_strays) and walks out of the pen (_leave_pen).
func _wander_out(dt: float) -> void:
var ph := campaign.state.phase
if not pen_open or (ph != CampaignController.Phase.NIGHT and ph != CampaignController.Phase.DAWN):
@ -422,7 +463,7 @@ func _wander_out(dt: float) -> void:
loot.animal_strays(loot.animal_item(id))
leaving[id] = 1
speed[id] = config.stray_speed
timer[id] = 4.0
timer[id] = LEAVE_TIME
func _record_trail(k: int, sc: Vector2) -> void: