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

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