feat(ludic.inventory, ludic.wallet): the pack and the purse as mechanic packages

ludic.inventory: counts per item kind, a PackRules port (stack_limit), add
what fits / take all or none / set outright, an ItemChanged queue, and a save
section keyed by item NAME so a game can reorder its items with no migration.
ludic.wallet: earn, spend only what is there, lose down to zero, set, a
MoneyChanged queue and its own save section. Six and five test blocks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-09-25 04:29:17 +03:00
parent 6cfaaf0bf9
commit 8075892e0e
12 changed files with 445 additions and 0 deletions

View file

@ -0,0 +1,62 @@
# wallet.ludic - the purse, its verbs, its facts and its save section
export property MoneyChanged {
before: int = 0
after: int = 0
}
var wl_money: int = 0
var wl_changes: Queue<MoneyChanged> = null
export function wallet_changes() -> Queue<MoneyChanged> {
if wl_changes == null { wl_changes = queue_new("wallet.changes") }
return wl_changes
}
export function wallet_money() -> int { return wl_money }
export function wallet_can(n: int) -> bool { return wl_money >= n }
# the purse holds exactly n (a starting purse, a correction, the network's word)
export function wallet_set(n: int) -> void {
if n == wl_money { return }
let c = new MoneyChanged
c.before = wl_money
c.after = n
wl_money = n
q_push(wallet_changes(), c)
}
export function wallet_earn(n: int) -> void { wallet_set(wl_money + n) }
# only what is there: false, and nothing spent, when it is not
export function wallet_spend(n: int) -> bool {
if n > wl_money { return false }
wallet_set(wl_money - n)
return true
}
# a cost that happens whether or not it can be paid (a rescue, a fine); never below zero
export function wallet_lose(n: int) -> void { wallet_set(Math.max(wl_money - n, 0)) }
export function wallet_reset() -> void {
wl_money = 0
q_clear(wallet_changes())
}
export function wallet_save() -> Val {
let v = Value.object()
sv_put_int(v, "money", wl_money)
return v
}
export function wallet_load(v: Val, version: int) -> void {
wl_money = sv_int(v, "money", 0)
q_clear(wallet_changes())
}
export function wallet_system() -> System {
let s = system_new("wallet", PH_COMMIT)
s.reset = fn wallet_reset
s.save = fn wallet_save
s.load = fn wallet_load
return s
}