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

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

View file

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