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>
124 lines
6.5 KiB
GDScript
124 lines
6.5 KiB
GDScript
extends RefCounted
|
|
## FarmState: creation from config (all three asset categories, and no starting structures --
|
|
## the pen is fully player-built), exact to_dict/from_dict round trips (also through JSON),
|
|
## rejection of invalid data, snapshot independence, and night loss records for every category.
|
|
|
|
|
|
func run(r: GameTestRunner) -> void:
|
|
var cfg := (load("res://game/resources/game_config.tres") as GameConfig).duplicate() as GameConfig
|
|
var s := FarmState.create(cfg)
|
|
r.check(s.night == 1 and s.nights_survived == 0, "a new farm starts before night 1")
|
|
r.check(s.phase == CampaignController.Phase.MORNING, "a new farm starts in the morning")
|
|
r.check(s.scarecrow_hp == cfg.scarecrow_max_hp and s.scarecrow_max_hp == cfg.scarecrow_max_hp, "scarecrow starts at full HP")
|
|
r.check(s.crops == cfg.start_crops and s.crops_total == cfg.start_crops, "crops start at the configured count")
|
|
r.check(s.animals == cfg.start_animals and s.animals_total == cfg.start_animals, "animals start at the configured count")
|
|
var stock := cfg.start_wood + cfg.start_scrap + cfg.start_straw
|
|
r.check(s.supplies == stock and s.supplies_total == stock and not s.shed_raided, "supplies are the shed's starting stock (%d)" % stock)
|
|
r.check(s.structures.size() == cfg.start_structures.size() and s.structures.is_empty() and s.next_structure_id == 1,
|
|
"no structures stand at the start: the pen is fully player-built")
|
|
r.check(s.is_valid(), "a new farm is valid")
|
|
|
|
# A state with every field away from its default.
|
|
s.night = 3
|
|
s.nights_survived = 2
|
|
s.phase = CampaignController.Phase.DUSK
|
|
s.phase_time_left = 12.5
|
|
s.scarecrow_hp = 6
|
|
s.crops_total = 40
|
|
for id in [3, 7, 8, 12, 19, 20, 21, 33, 40]:
|
|
s.lose_crop(id)
|
|
s.night_events = {"crops_stolen": 2, "crops_recovered": 1, "animals_strayed": 1, "supplies_lost": 5}
|
|
for id in [2, 5, 7]:
|
|
s.lose_animal(id)
|
|
s.supplies_total = 120
|
|
s.shed_raided = true
|
|
s.night_log.append(_entry(1, 4, 0, 0))
|
|
s.night_log.append(_entry(2, 5, 3, 43))
|
|
var d := s.to_dict()
|
|
|
|
var copy := FarmState.from_dict(d)
|
|
r.check(copy != null and copy.to_dict() == d, "to_dict -> from_dict round trip is exact")
|
|
var parsed: Variant = JSON.parse_string(JSON.stringify(d))
|
|
var from_json := FarmState.from_dict(parsed as Dictionary)
|
|
r.check(from_json != null and from_json.to_dict() == d, "round trip through JSON is exact (numbers coerced back to ints)")
|
|
r.check(from_json != null and typeof(from_json.night_log[1]["supplies_lost"]) == TYPE_INT, "night log values are ints after JSON")
|
|
|
|
d["night_log"][0]["crops_lost"] = 99
|
|
r.check(s.night_log[0]["crops_lost"] == 4, "a snapshot is independent of the live state")
|
|
|
|
_rejects(r, s.to_dict())
|
|
_night_records(r, cfg)
|
|
|
|
|
|
func _entry(night: int, crops: int, animals: int, supplies: int) -> Dictionary:
|
|
var e := {"night": night}
|
|
for key in FarmState.LOG_KEYS:
|
|
if key != "night":
|
|
e[key] = 0
|
|
e["crops_lost"] = crops
|
|
e["animals_lost"] = animals
|
|
e["supplies_lost"] = supplies
|
|
e["crops_stolen"] = crops + 1
|
|
e["animals_stolen"] = animals
|
|
e["supplies_stolen"] = supplies + 5
|
|
return e
|
|
|
|
|
|
func _rejects(r: GameTestRunner, good: Dictionary) -> void:
|
|
var cases := {
|
|
"another version": func(d: Dictionary) -> void: d["version"] = FarmState.VERSION + 1,
|
|
"a missing key": func(d: Dictionary) -> void: d.erase("lost_crops"),
|
|
"an unknown phase": func(d: Dictionary) -> void: d["phase"] = 42,
|
|
"a lost crop id beyond the total": func(d: Dictionary) -> void: d["lost_crops"] = [d["crops_total"] + 1],
|
|
"a crop lost twice": func(d: Dictionary) -> void: d["lost_crops"] = [3, 3],
|
|
"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,
|
|
"more nights survived than played": func(d: Dictionary) -> void: d["nights_survived"] = d["night"] + 1,
|
|
"a malformed night log": func(d: Dictionary) -> void: d["night_log"] = [{"night": 1, "crops_lost": 0, "animals_lost": 0, "supplies_lost": 0}],
|
|
}
|
|
for name: String in cases:
|
|
var d := good.duplicate(true)
|
|
(cases[name] as Callable).call(d)
|
|
r.check(FarmState.from_dict(d) == null, "from_dict rejects %s" % name)
|
|
|
|
|
|
func _night_records(r: GameTestRunner, cfg: GameConfig) -> void:
|
|
var s := FarmState.create(cfg)
|
|
var start := s.asset_counts()
|
|
for id in range(1, 8):
|
|
s.lose_crop(id)
|
|
s.lose_animal(3)
|
|
s.record_event("crops_stolen")
|
|
s.record_event("crops_stolen")
|
|
s.record_event("crops_recovered")
|
|
s.record_event("supplies_stolen", 5)
|
|
s.record_event("supplies_lost", 5)
|
|
s.resources.shed["wood"] = 99 # stored and spent materials don't count as losses
|
|
s.log_night(start)
|
|
var e: Dictionary = s.night_log[0]
|
|
r.check(s.night_log.size() == 1 and e["night"] == 1 and e["crops_lost"] == 7 and e["animals_lost"] == 1,
|
|
"log_night records the crops and animals lost since the night started")
|
|
r.check(e["supplies_lost"] == 5 and e["supplies_stolen"] == 5, "supplies lost are the units carried off (%d)" % e["supplies_lost"])
|
|
r.check(e["crops_stolen"] == 2 and e["crops_recovered"] == 1 and s.night_events.is_empty(),
|
|
"log_night copies the night's theft events and clears them")
|
|
|
|
s = FarmState.create(cfg)
|
|
r.check(s.lose_crop(5) and not s.lose_crop(5) and not s.lose_crop(0) and not s.lose_crop(s.crops_total + 1),
|
|
"lose_crop accepts each known unit once")
|
|
r.check(s.crops == s.crops_total - 1 and s.is_crop_lost(5) and not s.is_crop_lost(6), "crops is derived from the lost units")
|
|
r.check(s.lose_animal(1) and not s.lose_animal(1) and not s.lose_animal(0) and s.animals == s.animals_total - 1,
|
|
"lose_animal accepts each known animal once; animals is derived")
|
|
s.supplies_in_transit = 4
|
|
r.check(s.supplies == s.resources.shed_total() + 4, "supplies count the shed's stock and the sacks still on the farm")
|
|
s.supplies_in_transit = 0
|
|
s.animals_total = 0
|
|
s.supplies_total = 0
|
|
s.lose_all_crops()
|
|
r.check(s.crops == 0 and s.is_stripped(), "losing every crop unit strips a crops-only farm")
|