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

@ -368,7 +368,9 @@ func step(dt: float) -> void:
for a in avatars:
var building := build.active if a.is_local else (a.remote != null and a.remote.build_active)
if a.kit.can_shoot() and a.interactions.can_shoot() and not building:
enemies.attacker = a.slot # its kills (and its shots') are its own (Alien Goop)
a.combat.step(dt)
enemies.attacker = -1
enemies.step(dt, p, SCARECROW_RADIUS)
for a in avatars:
if a.kit.alive:
@ -497,10 +499,14 @@ func add_partner(local: bool, name_text: String = "") -> Avatar:
## Co-op: player 2 left. Their scarecrow goes; if player 1's is down, that's the end of the night.
func remove_partner() -> void:
## What they carried goes into the shed (on the host). Returns what moved (material -> amount).
func remove_partner() -> Dictionary:
if partner == null:
return
return {}
var a := partner
var moved := {} if replica else campaign.state.resources.deposit(a.slot)
if not moved.is_empty():
shed_stock.refresh()
partner = null
avatars.erase(a)
if camera is FollowCamera and (camera as FollowCamera).target == a.body:
@ -515,10 +521,12 @@ func remove_partner() -> void:
campaign.state.coop = false
behavior.scarecrow2 = Vector2(INF, INF)
enemies.scarecrow2 = Vector2(INF, INF)
return moved
## Makes another scarecrow this machine's own (the partner's machine plays player 2): the HUD,
## the camera, build mode, the upgrade menu, sounds, and tips follow it.
## the camera, build mode, the upgrade menu, the materials readout (its own pouch), sounds, and
## tips follow it.
func set_local(a: Avatar) -> void:
for other in avatars:
other.is_local = other == a
@ -538,8 +546,12 @@ func set_local(a: Avatar) -> void:
build.kit = a.kit
if upgrade_panel != null:
upgrade_panel.player = a.body
upgrade_panel.slot = a.slot
if shed_panel != null:
shed_panel.player = a.body
shed_panel.slot = a.slot
if resource_hud != null:
resource_hud.slot = a.slot
if camera is FollowCamera:
(camera as FollowCamera).target = a.body
(camera as FollowCamera).snap_to_target()
@ -625,19 +637,37 @@ func _count_removal(handle: int, reason: EnemySystem.Removal, _x: float, _z: flo
## Alien Goop (Decision 11): a killed Alien (the thief type, display-named "Alien") has a
## config-tunable chance to drop goop, credited like a gathered material straight into the
## pouch (respecting its capacity; a full pouch simply loses the drop, same as any overflow).
func _drop_alien_goop(handle: int, reason: EnemySystem.Removal, _x: float, _z: float, _carry: int, _target: int) -> void:
if reason != EnemySystem.Removal.KILLED:
## Co-op: into the killer's pouch (EnemySystem.killer_of); a turret's or a bomb's kill goes to the
## scarecrow nearest to it (goop_slot). Only the host rolls it: the partner's farm state follows.
func _drop_alien_goop(handle: int, reason: EnemySystem.Removal, x: float, z: float, _carry: int, _target: int) -> void:
if reason != EnemySystem.Removal.KILLED or replica:
return
var d := enemies.resolve_handle(handle)
if d < 0 or enemies.type[d] != EnemySystem.TYPE_THIEF:
return
if _goop_rng.randf() >= config.alien_goop_drop_chance:
return
var added := campaign.state.resources.add_to_pouch("alien_goop", config.alien_goop_drop_qty)
var slot := goop_slot(enemies.killer_of(handle), Vector2(x, z))
var added := campaign.state.resources.add_to_pouch("alien_goop", config.alien_goop_drop_qty, slot)
if added > 0:
campaign.state.record_event("gathered_alien_goop", added)
## Whose pouch a kill's goop goes to: the killer's (an avatar slot still on the farm), else the
## scarecrow nearest to where it fell, a standing one before a downed one (solo: always slot 0).
func goop_slot(killer: int, at: Vector2) -> int:
var best := -1
var best_d := INF
for a in avatars:
if a.slot == killer:
return killer
var d := a.position().distance_squared_to(at) + (0.0 if a.kit.alive else 1e12)
if d < best_d:
best_d = d
best = a.slot
return maxi(best, 0)
## Keeps the field toward the scarecrow fresh while anything is fighting it.
func _update_hunt(dt: float) -> void:
_hunt_wait = maxf(_hunt_wait - dt, 0.0)
@ -745,6 +775,7 @@ func _on_phase_changed(phase: CampaignController.Phase, _previous: CampaignContr
CampaignController.Phase.MORNING:
if partner == null and not replica:
campaign.state.coop = false # a co-op save continued alone: solo rules
campaign.state.resources.deposit(1) # and player 2's pouch, saved that morning, is in the shed
enemies.clear()
projectiles.clear()
bombs.clear()