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:
parent
26b735d3e6
commit
fa7f67fe04
23 changed files with 535 additions and 138 deletions
|
|
@ -1,24 +1,48 @@
|
|||
class_name ResourceStore
|
||||
extends RefCounted
|
||||
## 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.
|
||||
## shed, and what each scarecrow carries in its own pouch (pouch_capacity units across all
|
||||
## materials; one capacity shared by every pouch). A pouch empties into the shed when its
|
||||
## scarecrow walks in. Costs are paid all-or-nothing, from the payer's pouch first and then the
|
||||
## shed. Only shed stock can be stolen.
|
||||
## Pouches go by avatar slot (Avatar.slot): 0 is player 1 (the solo player, or the host in
|
||||
## co-op), 1 is player 2 (the partner). Every pouch method takes the slot and defaults to 0, so
|
||||
## solo callers never name one; `pouch` is slot 0's.
|
||||
## 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", "alien_goop"]
|
||||
## How many pouches there are (one per avatar slot), and their keys in to_dict(), by slot.
|
||||
const SLOTS := 2
|
||||
const POUCH_KEYS: Array[String] = ["pouch", "partner_pouch"]
|
||||
|
||||
var shed := {"wood": 0, "scrap": 0, "straw": 0, "alien_goop": 0}
|
||||
var pouch := {"wood": 0, "scrap": 0, "straw": 0, "alien_goop": 0}
|
||||
## Each scarecrow's pouch, by avatar slot.
|
||||
var pouches: Array[Dictionary] = [
|
||||
{"wood": 0, "scrap": 0, "straw": 0, "alien_goop": 0},
|
||||
{"wood": 0, "scrap": 0, "straw": 0, "alien_goop": 0},
|
||||
]
|
||||
## Player 1's pouch (slot 0): the solo player's, or the host's.
|
||||
var pouch: Dictionary:
|
||||
get:
|
||||
return pouches[0]
|
||||
set(value):
|
||||
pouches[0] = value
|
||||
## Shared by every pouch: one Bigger pouch upgrade grows them all.
|
||||
var pouch_capacity: int = 15
|
||||
|
||||
|
||||
func pouch_total() -> int:
|
||||
## The pouch of an avatar slot (an out-of-range slot reads as the nearest valid one).
|
||||
func pouch_of(slot: int = 0) -> Dictionary:
|
||||
return pouches[clampi(slot, 0, SLOTS - 1)]
|
||||
|
||||
|
||||
func pouch_total(slot: int = 0) -> int:
|
||||
var p := pouch_of(slot)
|
||||
var n := 0
|
||||
for m in MATERIALS:
|
||||
n += int(pouch.get(m, 0))
|
||||
n += int(p.get(m, 0))
|
||||
return n
|
||||
|
||||
|
||||
|
|
@ -40,61 +64,71 @@ func most_stocked() -> String:
|
|||
return best
|
||||
|
||||
|
||||
func pouch_room() -> int:
|
||||
return maxi(pouch_capacity - pouch_total(), 0)
|
||||
func pouch_room(slot: int = 0) -> int:
|
||||
return maxi(pouch_capacity - pouch_total(slot), 0)
|
||||
|
||||
|
||||
## 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.get(material, 0)) + added
|
||||
## Puts up to amount into a slot's pouch, as much as fits. Returns how much went in.
|
||||
func add_to_pouch(material: String, amount: int, slot: int = 0) -> int:
|
||||
var p := pouch_of(slot)
|
||||
var added := mini(maxi(amount, 0), pouch_room(slot))
|
||||
p[material] = int(p.get(material, 0)) + added
|
||||
return added
|
||||
|
||||
|
||||
## Moves the whole pouch into the shed. Returns what moved (material -> amount, only non-zero).
|
||||
func deposit() -> Dictionary:
|
||||
## Moves a slot's whole pouch into the shed. Returns what moved (material -> amount, only
|
||||
## non-zero).
|
||||
func deposit(slot: int = 0) -> Dictionary:
|
||||
var p := pouch_of(slot)
|
||||
var moved := {}
|
||||
for m in MATERIALS:
|
||||
var n := int(pouch.get(m, 0))
|
||||
var n := int(p.get(m, 0))
|
||||
if n > 0:
|
||||
shed[m] = int(shed.get(m, 0)) + n
|
||||
pouch[m] = 0
|
||||
p[m] = 0
|
||||
moved[m] = n
|
||||
return moved
|
||||
|
||||
|
||||
func available(material: String) -> int:
|
||||
return int(shed.get(material, 0)) + int(pouch.get(material, 0))
|
||||
## What a slot's scarecrow can spend of a material: the shed's stock plus its own pouch.
|
||||
func available(material: String, slot: int = 0) -> int:
|
||||
return int(shed.get(material, 0)) + int(pouch_of(slot).get(material, 0))
|
||||
|
||||
|
||||
## What a cost still lacks (material -> amount short; empty when affordable).
|
||||
func missing(cost: Dictionary) -> Dictionary:
|
||||
## What a cost still lacks for a slot's scarecrow (material -> amount short; empty when
|
||||
## affordable).
|
||||
func missing(cost: Dictionary, slot: int = 0) -> Dictionary:
|
||||
var short := {}
|
||||
for m in cost:
|
||||
var need := int(cost[m]) - available(String(m))
|
||||
var need := int(cost[m]) - available(String(m), slot)
|
||||
if need > 0:
|
||||
short[m] = need
|
||||
return short
|
||||
|
||||
|
||||
func can_afford(cost: Dictionary) -> bool:
|
||||
return missing(cost).is_empty()
|
||||
func can_afford(cost: Dictionary, slot: int = 0) -> bool:
|
||||
return missing(cost, slot).is_empty()
|
||||
|
||||
|
||||
## Pays a cost from the pouch first, then the shed. Pays nothing and returns false if short.
|
||||
func pay(cost: Dictionary) -> bool:
|
||||
if not can_afford(cost):
|
||||
## Pays a cost from a slot's pouch first, then the shed (never another scarecrow's pouch). Pays
|
||||
## nothing and returns false if short.
|
||||
func pay(cost: Dictionary, slot: int = 0) -> bool:
|
||||
if not can_afford(cost, slot):
|
||||
return false
|
||||
var p := pouch_of(slot)
|
||||
for m in cost:
|
||||
var need := int(cost[m])
|
||||
var from_pouch := mini(need, int(pouch[m]))
|
||||
pouch[m] = int(pouch[m]) - from_pouch
|
||||
shed[m] = int(shed[m]) - (need - from_pouch)
|
||||
var from_pouch := mini(need, int(p.get(m, 0)))
|
||||
p[m] = int(p.get(m, 0)) - from_pouch
|
||||
shed[m] = int(shed.get(m, 0)) - (need - from_pouch)
|
||||
return true
|
||||
|
||||
|
||||
func to_dict() -> Dictionary:
|
||||
return {"shed": _filled(shed), "pouch": _filled(pouch), "pouch_capacity": pouch_capacity}
|
||||
var d := {"shed": _filled(shed), "pouch_capacity": pouch_capacity}
|
||||
for slot in SLOTS:
|
||||
d[POUCH_KEYS[slot]] = _filled(pouches[slot])
|
||||
return d
|
||||
|
||||
|
||||
## A copy with every material present (one missing from shed/pouch, e.g. because it was replaced
|
||||
|
|
@ -108,23 +142,29 @@ static func _filled(d: Dictionary) -> Dictionary:
|
|||
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.
|
||||
## Returns null for a missing part (the shed, either pouch, the capacity), unknown materials,
|
||||
## negative amounts, or an overfull pouch. A material missing from the shed or a 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
|
||||
var dd := d as Dictionary
|
||||
if not (dd.get("shed") is Dictionary and dd.get("pouch") is Dictionary and dd.has("pouch_capacity")):
|
||||
if not (dd.get("shed") is Dictionary and dd.has("pouch_capacity")):
|
||||
return null
|
||||
for key in POUCH_KEYS:
|
||||
if not dd.get(key) is Dictionary:
|
||||
return null
|
||||
var r := ResourceStore.new()
|
||||
r.pouch_capacity = int(dd["pouch_capacity"])
|
||||
for part in ["shed", "pouch"]:
|
||||
var parts: Array[String] = ["shed"]
|
||||
parts.append_array(POUCH_KEYS)
|
||||
for part in parts:
|
||||
var src: Dictionary = dd[part]
|
||||
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
|
||||
var into: Dictionary = r.shed if part == "shed" else r.pouches[POUCH_KEYS.find(part)]
|
||||
for m in MATERIALS:
|
||||
if src.has(m):
|
||||
if int(src[m]) < 0:
|
||||
|
|
@ -132,7 +172,12 @@ static func from_dict(d: Variant) -> ResourceStore:
|
|||
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
|
||||
if r.pouch_capacity < 1:
|
||||
return null
|
||||
for slot in SLOTS:
|
||||
if r.pouch_total(slot) > r.pouch_capacity:
|
||||
return null
|
||||
return r
|
||||
|
||||
|
||||
## "6 wood, 2 straw" for a material -> amount dictionary (in MATERIALS order).
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue