Task #16: per-player pouch in co-op

Each scarecrow carries its own pouch (ResourceStore.pouches by avatar
slot; slot 0 is the solo player / host, `pouch` stays slot 0's), with one
shared pouch_capacity so a Bigger pouch grows both. The shed stays shared.

- Gathering, the shed drop-off, repair, upgrade-in-place, patching,
  building and buying all act on the acting avatar's slot. The host pays
  the partner's "place" and "buy" requests from P2's pouch and the shed.
- The partner's build mode, build hotbar, shop menus and materials HUD
  check affordability against P2's pouch (FarmSim.set_local).
- Alien Goop goes to the killer's pouch: FarmSim sets
  EnemySystem.attacker around each avatar's combat step and shots carry
  their shooter's slot; bomb/turret kills go to the nearest scarecrow.
  The replica no longer rolls goop locally.
- When the partner leaves, their pouch goes into the shed (and is named
  in the toast); a co-op save continued alone banks P2's pouch at morning.
- FarmState.VERSION 7 (resources.partner_pouch) with a v6 migration;
  CoopSession.PROTOCOL 4.
- Tests: per-slot store rules, v6 save migration, and test_coop cases for
  separate gathering, no cross-banking in the shed, own-pouch paying,
  shared capacity, goop credit and leaving. test_coop's crowd case now
  resets the loopback's drop counter so earlier traffic can't change
  which messages it loses.
- Docs: COOP.md and RULES.md no longer say the pouch is shared.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Forrest 2026-10-06 13:06:25 -04:00
parent 26b735d3e6
commit fa7f67fe04
23 changed files with 535 additions and 138 deletions

View file

@ -197,8 +197,9 @@ static func corners(fp: Dictionary, shrink_long: float = 0.0, shrink_wide: float
return pts
## Why a structure can't go here ("" if it can). Space first, then daytime, then the cost.
func validate(type: String, x: float, z: float, rot: int, scarecrow: Vector2) -> String:
## Why a structure can't go here ("" if it can). Space first, then daytime, then the cost (from
## the builder's pouch, its avatar slot, and the shed).
func validate(type: String, x: float, z: float, rot: int, scarecrow: Vector2, slot: int = 0) -> String:
var fp := footprint(type, x, z, rot)
var outline := corners(fp)
var inside := zones.playable.grow(-0.3)
@ -232,15 +233,16 @@ func validate(type: String, x: float, z: float, rot: int, scarecrow: Vector2) ->
if not campaign.is_daytime():
return "Building is for daytime"
var cost: Dictionary = config.costs.get(type, {})
if not campaign.state.resources.can_afford(cost):
return "Not enough: need %s more" % ResourceStore.describe(campaign.state.resources.missing(cost))
if not campaign.state.resources.can_afford(cost, slot):
return "Not enough: need %s more" % ResourceStore.describe(campaign.state.resources.missing(cost, slot))
return ""
## Builds after validate() passed: pays, records, shows, and updates navigation. Returns the id.
func place(type: String, x: float, z: float, rot: int) -> int:
## Builds after validate() passed: pays (from the builder's pouch, its avatar slot, and the shed),
## records, shows, and updates navigation. Returns the id.
func place(type: String, x: float, z: float, rot: int, slot: int = 0) -> int:
var s := campaign.state
if not s.pay(config.costs.get(type, {})):
if not s.pay(config.costs.get(type, {}), slot):
return -1
var id := s.next_structure_id
s.next_structure_id += 1
@ -365,10 +367,11 @@ func repair_cost(id: int) -> Dictionary:
return out
## Pays and repairs fully. Returns false if there was nothing to repair or it couldn't be paid.
func repair(id: int) -> bool:
## Pays (from the repairer's pouch, its avatar slot, and the shed) and repairs fully. Returns
## false if there was nothing to repair or it couldn't be paid.
func repair(id: int, slot: int = 0) -> bool:
var cost := repair_cost(id)
if cost.is_empty() or not campaign.state.pay(cost):
if cost.is_empty() or not campaign.state.pay(cost, slot):
return false
var e := campaign.state.structure(id)
e["hp"] = max_hp(e["type"])
@ -393,8 +396,9 @@ func upgrade_cost(id: int) -> Dictionary:
## Why id can't be upgraded right now ("" if it can): levelable, undamaged (repair takes
## priority), under MAX_LEVEL, a bigger tar patch footprint clear of neighbors and the world, and
## the cost affordable. Doesn't check the time of day (Interactions, like the build-mode rules).
func upgrade_blocker(id: int) -> String:
## the cost affordable (from the upgrader's pouch, its avatar slot, and the shed). Doesn't check
## the time of day (Interactions, like the build-mode rules).
func upgrade_blocker(id: int, slot: int = 0) -> String:
var e := campaign.state.structure(id)
if e.is_empty() or not is_levelable(e["type"]):
return "Can't be upgraded"
@ -415,17 +419,17 @@ func upgrade_blocker(id: int) -> String:
return "Not enough room to upgrade"
if _world_blocked(fp):
return "Not enough room to upgrade"
if not campaign.state.resources.can_afford(cost):
return "Not enough: need %s more" % ResourceStore.describe(campaign.state.resources.missing(cost))
if not campaign.state.resources.can_afford(cost, slot):
return "Not enough: need %s more" % ResourceStore.describe(campaign.state.resources.missing(cost, slot))
return ""
## Pays and levels a structure up by one (see upgrade_blocker). Returns false if it couldn't be.
func upgrade(id: int) -> bool:
if upgrade_blocker(id) != "":
func upgrade(id: int, slot: int = 0) -> bool:
if upgrade_blocker(id, slot) != "":
return false
var cost := upgrade_cost(id)
if not campaign.state.pay(cost):
if not campaign.state.pay(cost, slot):
return false
var e := campaign.state.structure(id)
e["level"] = int(e.get("level", 1)) + 1