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

@ -71,7 +71,7 @@ func _day(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter, hud: PhaseHu
Input.action_release("interact")
_step(sim, 1)
r.check(s.resources.pouch["wood"] == 2, "gathered 2 wood into the pouch")
s.resources.shed = {"wood": 20, "scrap": 20, "straw": 20}
s.resources.shed = {"wood": 20, "scrap": 20, "straw": 20, "alien_goop": 0}
_start_structures = sim.structures.count()
var fence := sim.structures.place("fence", 30.0, -18.5, 0)
sim.structures.place("automated_defense", 38.0, -17.0, 0)

View file

@ -4,7 +4,9 @@ extends RefCounted
## through JSON, and on the real farm (FarmSim stepped by hand): the gather points' layout,
## gathering by day only (charges, rooted, cancelled by letting go / a hit / fear, a full pouch,
## nightfall), the shed drop-off, overnight regrowth, retry restoring the morning's materials,
## patching's straw cost, and buying the Straw Stuffing upgrade from the workbench's menu.
## patching's straw cost, buying the Straw Stuffing upgrade from the workbench's menu, and Alien
## Goop (#4): dropped only by killed Aliens (thieves) at a config-tunable rate, credited like a
## gathered material, stealable from the shed once it's the stock on top, and surviving retry.
const P := CampaignController.Phase
const DT := 1.0 / 60.0
@ -13,6 +15,7 @@ const DT := 1.0 / 60.0
func run(r: GameTestRunner) -> void:
_store(r)
_state(r)
_goop_persistence(r)
var farm := (load("res://game/scenes/farm.tscn") as PackedScene).instantiate() as Node3D
r.root.add_child(farm)
await r.process_frame
@ -26,6 +29,7 @@ func run(r: GameTestRunner) -> void:
_deposit_and_night(r, sim, player)
_patch_cost(r, sim)
_stuffing(r, sim, player)
_goop_drops(r, sim)
farm.queue_free()
await r.process_frame
@ -66,18 +70,38 @@ func _store(r: GameTestRunner) -> void:
func _state(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.resources.shed == {"wood": cfg.start_wood, "scrap": cfg.start_scrap, "straw": cfg.start_straw}
and s.resources.pouch_capacity == cfg.pouch_capacity, "a new farm starts with the configured stock and pouch size")
r.check(s.resources.shed == {"wood": cfg.start_wood, "scrap": cfg.start_scrap, "straw": cfg.start_straw, "alien_goop": cfg.start_alien_goop}
and s.resources.pouch_capacity == cfg.pouch_capacity, "a new farm starts with the configured stock and pouch size (Alien Goop a 4th material, starting at 0)")
s.resources.add_to_pouch("scrap", 3)
s.resources.add_to_pouch("alien_goop", 2)
s.gather_charges = {"Snag": 1, "Wreck": 0}
s.upgrades = {"straw_stuffing": 1}
var d := s.to_dict()
var back := FarmState.from_dict(JSON.parse_string(JSON.stringify(d)))
r.check(back != null and back.to_dict() == d, "FarmState keeps materials, used gather points, and upgrades through JSON")
r.check(back != null and back.to_dict() == d, "FarmState keeps materials (including Alien Goop), used gather points, and upgrades through JSON")
d["gather_charges"] = {"Snag": -2}
r.check(FarmState.from_dict(d) == null, "FarmState rejects negative gather charges")
## Alien Goop round-trips like the other materials: a retry discards what was gained since the
## morning (same mechanic _deposit_and_night checks for wood/scrap below), and the morning
## snapshot itself (what SaveGame writes) survives a JSON round-trip.
func _goop_persistence(r: GameTestRunner) -> void:
var cfg := (load("res://game/resources/game_config.tres") as GameConfig).duplicate() as GameConfig
var c := r.make_campaign(cfg)
var morning := c.state.to_dict()
c.begin_day()
c.state.resources.add_to_pouch("alien_goop", 5)
c.state.resources.shed["alien_goop"] = 3
c.advance_phase() # DAY -> DUSK
c.advance_phase() # DUSK -> NIGHT
c.retry_night()
r.check(c.state.phase == P.MORNING and c.state.resources.to_dict() == morning["resources"],
"a retry discards Alien Goop gained since the morning, same as the other materials")
var back := FarmState.from_dict(JSON.parse_string(JSON.stringify(morning)))
r.check(back != null and back.resources.shed.get("alien_goop", -1) == 0, "the morning snapshot (what's saved) round-trips Alien Goop through JSON")
func _layout(r: GameTestRunner, sim: FarmSim) -> void:
var by_material := {}
var names := {}
@ -284,6 +308,58 @@ func _stuffing(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter) -> void
menu.close()
## Alien Goop (#4): dropped only by killed Aliens (thieves), at GameConfig's rate and quantity,
## credited like a gathered material (respecting the pouch's capacity), and stealable from the
## shed like any other material once it's the one on top.
func _goop_drops(r: GameTestRunner, sim: FarmSim) -> void:
var es := sim.enemies
var s := sim.campaign.state
var cfg := sim.config
var pouch_cap_before := s.resources.pouch_capacity
s.resources.pouch_capacity = 1000000 # room for every drop; isolates the chance roll from capacity
# Statistical: over many kills, the drop rate roughly matches the config chance (the drop
# roll uses a seeded RNG, so this is also reproducible run to run).
const N := 1000
var before := int(s.night_events.get("gathered_alien_goop", 0))
for i in N:
var h := es.spawn(EnemySystem.TYPE_THIEF, 12.0, -30.0, Vector2.UP)
es.apply_hit_handle(h, 99)
_step(sim, 1)
var gained := int(s.night_events.get("gathered_alien_goop", 0)) - before
var rate := float(gained) / float(N * cfg.alien_goop_drop_qty)
r.check(absf(rate - cfg.alien_goop_drop_chance) < 0.06,
"killing %d Aliens drops goop about %.0f%% of the time (got %.1f%%, %d units)" % [N, cfg.alien_goop_drop_chance * 100.0, rate * 100.0, gained])
# Type restriction: only thieves ("Alien") drop goop, not crows or fence breakers.
var before2 := int(s.night_events.get("gathered_alien_goop", 0))
for i in 50:
var hc := es.spawn(EnemySystem.TYPE_CROW, 12.0, -30.0, Vector2.UP)
es.apply_hit_handle(hc, 99)
_step(sim, 1)
var hb := es.spawn(EnemySystem.TYPE_BREAKER, 12.0, -30.0, Vector2.UP)
es.apply_hit_handle(hb, 99)
_step(sim, 1)
r.check(int(s.night_events.get("gathered_alien_goop", 0)) == before2, "only Aliens (thieves) drop goop, not crows or fence breakers")
# Deterministic: a guaranteed drop is credited like a gathered material, capped by pouch room.
cfg.alien_goop_drop_chance = 1.0
cfg.alien_goop_drop_qty = 3
s.resources.pouch_capacity = s.resources.pouch_total() + 2 # room for only 2 of the 3
var pouch_before := s.resources.pouch_total()
var h2 := es.spawn(EnemySystem.TYPE_THIEF, 12.0, -30.0, Vector2.UP)
es.apply_hit_handle(h2, 99)
_step(sim, 1)
r.check(s.resources.pouch_total() == pouch_before + 2, "a drop only adds what fits in the pouch (2 of 3, the pouch nearly full)")
cfg.alien_goop_drop_chance = 0.33
cfg.alien_goop_drop_qty = 1
s.resources.pouch_capacity = pouch_cap_before
# Stealable from the shed: once goop is the shed's most-stocked material, a supply claim
# takes it, and the shed's own pile wiring gives the thief somewhere real to stand.
s.resources.shed = {"wood": 0, "scrap": 0, "straw": 0, "alien_goop": 20}
var sack := sim.loot._claim_home(LootSystem.Category.SUPPLY)
r.check(sack > 0 and sim.loot.sack_material[sack] == "alien_goop" and sim.loot.home[sack] != Vector2.ZERO,
"a sack claimed at the shed takes goop when it's the most-stocked material, at a real spot")
s.resources.shed = {"wood": 0, "scrap": 0, "straw": 0, "alien_goop": 0}
func _node(it: Interactions, node_name: String) -> GatherNode:
for n in it.nodes:
if n.name == node_name: