124 lines
3.5 KiB
GDScript
124 lines
3.5 KiB
GDScript
class_name ResourceStore
|
|
extends RefCounted
|
|
## The farm's construction materials (wood, scrap, straw): 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.
|
|
|
|
const MATERIALS: Array[String] = ["wood", "scrap", "straw"]
|
|
|
|
var shed := {"wood": 0, "scrap": 0, "straw": 0}
|
|
var pouch := {"wood": 0, "scrap": 0, "straw": 0}
|
|
var pouch_capacity: int = 15
|
|
|
|
|
|
func pouch_total() -> int:
|
|
var n := 0
|
|
for m in MATERIALS:
|
|
n += int(pouch[m])
|
|
return n
|
|
|
|
|
|
func shed_total() -> int:
|
|
var n := 0
|
|
for m in MATERIALS:
|
|
n += int(shed[m])
|
|
return n
|
|
|
|
|
|
## The material the shed holds most of (ties in MATERIALS order), or "" when it's empty.
|
|
func most_stocked() -> String:
|
|
var best := ""
|
|
var most := 0
|
|
for m in MATERIALS:
|
|
if int(shed[m]) > most:
|
|
most = int(shed[m])
|
|
best = m
|
|
return best
|
|
|
|
|
|
func pouch_room() -> int:
|
|
return maxi(pouch_capacity - pouch_total(), 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[material]) + added
|
|
return added
|
|
|
|
|
|
## Moves the whole pouch into the shed. Returns what moved (material -> amount, only non-zero).
|
|
func deposit() -> Dictionary:
|
|
var moved := {}
|
|
for m in MATERIALS:
|
|
var n := int(pouch[m])
|
|
if n > 0:
|
|
shed[m] = int(shed[m]) + n
|
|
pouch[m] = 0
|
|
moved[m] = n
|
|
return moved
|
|
|
|
|
|
func available(material: String) -> int:
|
|
return int(shed[material]) + int(pouch[material])
|
|
|
|
|
|
## What a cost still lacks (material -> amount short; empty when affordable).
|
|
func missing(cost: Dictionary) -> Dictionary:
|
|
var short := {}
|
|
for m in cost:
|
|
var need := int(cost[m]) - available(String(m))
|
|
if need > 0:
|
|
short[m] = need
|
|
return short
|
|
|
|
|
|
func can_afford(cost: Dictionary) -> bool:
|
|
return missing(cost).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):
|
|
return false
|
|
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)
|
|
return true
|
|
|
|
|
|
func to_dict() -> Dictionary:
|
|
return {"shed": shed.duplicate(), "pouch": pouch.duplicate(), "pouch_capacity": pouch_capacity}
|
|
|
|
|
|
## Returns null for unknown materials, missing entries, negative amounts, or an overfull pouch.
|
|
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")):
|
|
return null
|
|
var r := ResourceStore.new()
|
|
r.pouch_capacity = int(dd["pouch_capacity"])
|
|
for part in ["shed", "pouch"]:
|
|
var src: Dictionary = dd[part]
|
|
if src.size() != MATERIALS.size():
|
|
return null
|
|
for m in MATERIALS:
|
|
if not src.has(m) or int(src[m]) < 0:
|
|
return null
|
|
var into: Dictionary = r.shed if part == "shed" else r.pouch
|
|
into[m] = int(src[m])
|
|
return r if r.pouch_capacity >= 1 and r.pouch_total() <= r.pouch_capacity else null
|
|
|
|
|
|
## "6 wood, 2 straw" for a material -> amount dictionary (in MATERIALS order).
|
|
static func describe(amounts: Dictionary) -> String:
|
|
var parts: Array[String] = []
|
|
for m in MATERIALS:
|
|
if int(amounts.get(m, 0)) > 0:
|
|
parts.append("%d %s" % [int(amounts[m]), m])
|
|
return ", ".join(parts)
|