Task #4: Alien Goop resource and drop-on-kill from Aliens

Add alien_goop as a 4th ResourceStore material alongside wood/scrap/straw,
dropped only by killed Aliens (TYPE_THIEF) at a config-tunable rate/quantity
(GameConfig.alien_goop_drop_chance default 0.33, alien_goop_drop_qty default
1), credited straight into the pouch like a gathered material via a new
enemy_removed hook in FarmSim (seeded RNG for reproducible trials). It's
stealable from the shed through the existing most-stocked-material sack
mechanic once it outranks the others (ShedStock gets a placeholder front/
label spot for it, reusing the wood pile's proven-walkable spot; no new art).

ResourceStore.from_dict() now treats a shed/pouch entry missing a material
as 0 instead of rejecting it (old saves predate Alien Goop), and to_dict()
always fills in every material so round-trips are consistent regardless of
how the live dict was built; every method summing across MATERIALS reads
with get(m, 0) so a hand-built 3-key dict elsewhere doesn't error out.

New checks in test_economy.gd: _goop_persistence (retry discards goop
gained since the morning; the morning snapshot round-trips through JSON)
and _goop_drops (1000-kill statistical drop-rate check against config,
type restriction to Aliens only, pouch-capacity-respecting credit, and
shed-theft eligibility). Updated _state()'s and test_cycle.gd's hardcoded
3-key shed dicts for the new 4th material.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Forrest 2026-10-05 14:23:22 -04:00
parent aa50e97311
commit 1ab666e5e4
9 changed files with 178 additions and 37 deletions

View file

@ -1,28 +1,31 @@
class_name ResourceStore
extends RefCounted
## The farm's construction materials (wood, scrap, straw): stock stored in the supply shed, and
## what the scarecrow carries in its pouch (pouch_capacity units across all materials). The
## pouch empties into the shed when the scarecrow walks in. Costs are paid all-or-nothing, from
## the pouch first and then the shed. Only shed stock can be stolen.
## The farm's construction materials (wood, scrap, straw, alien goop): stock stored in the supply
## shed, and what the scarecrow carries in its pouch (pouch_capacity units across all materials).
## The pouch empties into the shed when the scarecrow walks in. Costs are paid all-or-nothing,
## from the pouch first and then the shed. Only shed stock can be stolen.
## Methods that sum across MATERIALS read with get(m, 0): a shed/pouch dict built or replaced
## elsewhere (tests, old saves) without every material present is treated as 0 for the rest,
## rather than erroring.
const MATERIALS: Array[String] = ["wood", "scrap", "straw"]
const MATERIALS: Array[String] = ["wood", "scrap", "straw", "alien_goop"]
var shed := {"wood": 0, "scrap": 0, "straw": 0}
var pouch := {"wood": 0, "scrap": 0, "straw": 0}
var shed := {"wood": 0, "scrap": 0, "straw": 0, "alien_goop": 0}
var pouch := {"wood": 0, "scrap": 0, "straw": 0, "alien_goop": 0}
var pouch_capacity: int = 15
func pouch_total() -> int:
var n := 0
for m in MATERIALS:
n += int(pouch[m])
n += int(pouch.get(m, 0))
return n
func shed_total() -> int:
var n := 0
for m in MATERIALS:
n += int(shed[m])
n += int(shed.get(m, 0))
return n
@ -31,8 +34,8 @@ func most_stocked() -> String:
var best := ""
var most := 0
for m in MATERIALS:
if int(shed[m]) > most:
most = int(shed[m])
if int(shed.get(m, 0)) > most:
most = int(shed.get(m, 0))
best = m
return best
@ -44,7 +47,7 @@ func pouch_room() -> int:
## Puts up to amount into the pouch, as much as fits. Returns how much went in.
func add_to_pouch(material: String, amount: int) -> int:
var added := mini(maxi(amount, 0), pouch_room())
pouch[material] = int(pouch[material]) + added
pouch[material] = int(pouch.get(material, 0)) + added
return added
@ -52,16 +55,16 @@ func add_to_pouch(material: String, amount: int) -> int:
func deposit() -> Dictionary:
var moved := {}
for m in MATERIALS:
var n := int(pouch[m])
var n := int(pouch.get(m, 0))
if n > 0:
shed[m] = int(shed[m]) + n
shed[m] = int(shed.get(m, 0)) + n
pouch[m] = 0
moved[m] = n
return moved
func available(material: String) -> int:
return int(shed[material]) + int(pouch[material])
return int(shed.get(material, 0)) + int(pouch.get(material, 0))
## What a cost still lacks (material -> amount short; empty when affordable).
@ -91,10 +94,23 @@ func pay(cost: Dictionary) -> bool:
func to_dict() -> Dictionary:
return {"shed": shed.duplicate(), "pouch": pouch.duplicate(), "pouch_capacity": pouch_capacity}
return {"shed": _filled(shed), "pouch": _filled(pouch), "pouch_capacity": pouch_capacity}
## Returns null for unknown materials, missing entries, negative amounts, or an overfull pouch.
## A copy with every material present (one missing from shed/pouch, e.g. because it was replaced
## elsewhere without every material present, defaults to 0): to_dict() always reads the same way
## whether or not the live dict happens to have every key, so it matches what from_dict() (and an
## old save that predates a new material) produces.
static func _filled(d: Dictionary) -> Dictionary:
var out := {}
for m in MATERIALS:
out[m] = int(d.get(m, 0))
return out
## Returns null for unknown materials, negative amounts, or an overfull pouch. A material missing
## from shed/pouch (an older save from before it existed, e.g. Alien Goop) is treated as 0, not
## rejected, so new materials stay load-compatible with old saves.
static func from_dict(d: Variant) -> ResourceStore:
if not d is Dictionary:
return null
@ -105,13 +121,17 @@ static func from_dict(d: Variant) -> ResourceStore:
r.pouch_capacity = int(dd["pouch_capacity"])
for part in ["shed", "pouch"]:
var src: Dictionary = dd[part]
if src.size() != MATERIALS.size():
return null
for key in src:
if not MATERIALS.has(String(key)):
return null # unknown material
var into: Dictionary = r.shed if part == "shed" else r.pouch
for m in MATERIALS:
if not src.has(m) or int(src[m]) < 0:
return null
var into: Dictionary = r.shed if part == "shed" else r.pouch
into[m] = int(src[m])
if src.has(m):
if int(src[m]) < 0:
return null
into[m] = int(src[m])
else:
into[m] = 0
return r if r.pouch_capacity >= 1 and r.pouch_total() <= r.pouch_capacity else null