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

@ -112,6 +112,11 @@ var _free_ids := PackedInt32Array()
var _free_top: int = 0
var _pending := PackedInt32Array()
var _pending_count: int = 0
## Co-op goop credit: the avatar slot whose attack is being resolved right now (FarmSim sets it
## around each scarecrow's combat step; -1 otherwise: turrets, bombs going off, tests), and the
## slot that landed each enemy's killing hit, by stable id (read through killer_of()).
var attacker: int = -1
var _killer := PackedInt32Array()
# Diagnostics.
var spawn_skipped: int = 0
@ -134,7 +139,7 @@ func setup(cfg: GameConfig, p_nav: FarmNavigation, p_behavior: ThiefBehavior, me
pace, head_x, head_z, flap_phase, hurt, timer, attack_cd, slow_left, alt]:
a.resize(capacity)
a.fill(0.0)
for a: PackedInt32Array in [health, target, carry, barrier, dense_id, id_dense, id_generation, _free_ids, _pending]:
for a: PackedInt32Array in [health, target, carry, barrier, dense_id, id_dense, id_generation, _free_ids, _pending, _killer]:
a.resize(capacity)
a.fill(0)
for a: PackedByteArray in [type, state, alive, removal, resume, want]:
@ -222,6 +227,7 @@ func spawn(enemy_type: int, x: float, z: float, heading: Vector2, p_want: int =
spawn_skipped += 1
return -1
_free_top -= 1
_killer[id] = -1
var d := active_count
active_count += 1
var h := heading.normalized() if heading.length_squared() > 1e-6 else Vector2(0.0, -1.0)
@ -287,8 +293,9 @@ func visual_position(d: int) -> Vector2:
return Vector2(lerpf(prev_x[d], pos_x[d], f), lerpf(prev_z[d], pos_z[d], f))
## Applies one hit. Returns false for dead or pending slots (no double kills).
func apply_hit(d: int, damage: int = 1) -> bool:
## Applies one hit. Returns false for dead or pending slots (no double kills). by: the avatar
## slot that landed it (a shot's shooter), or -1 for whoever is attacking now (attacker).
func apply_hit(d: int, damage: int = 1, by: int = -1) -> bool:
if not is_alive_slot(d):
return false
health[d] -= damage
@ -297,14 +304,22 @@ func apply_hit(d: int, damage: int = 1) -> bool:
characters.hit(dense_id[d])
if health[d] <= 0:
kills += 1
_killer[dense_id[d]] = by if by >= 0 else attacker
queue_removal(d, Removal.KILLED)
elif behavior != null:
behavior.on_hit(self, d)
return true
func apply_hit_handle(handle: int, damage: int = 1) -> bool:
return apply_hit(resolve_handle(handle), damage)
func apply_hit_handle(handle: int, damage: int = 1, by: int = -1) -> bool:
return apply_hit(resolve_handle(handle), damage, by)
## The avatar slot whose hit killed this enemy (while enemy_removed is emitted for it), or -1:
## killed by a turret or a bomb, not killed, or a stale handle.
func killer_of(handle: int) -> int:
var d := resolve_handle(handle)
return _killer[dense_id[d]] if d >= 0 else -1
## Takes a living enemy out of the simulation now; its slot is released (and enemy_removed