ludic/packages/ludic.inventory/state.ludic
2026-09-25 14:02:21 +03:00

61 lines
2.1 KiB
Text

# state.ludic - the pack's port, its facts, and its counts
# what the pack asks the game: how many of a kind fit (unbound: no limit)
export port PackRules {
stack_limit: fn(int) -> int = fn iv_no_limit
}
function iv_no_limit(it: int) -> int { return 2147483647 }
# a count changed, however: added, taken, or set outright
export property ItemChanged {
item: int = 0
before: int = 0
after: int = 0
}
export state InventoryState {
iv_keys: []string = null
iv_counts: []int = null
iv_changes: Queue<ItemChanged> = null
}
# the kinds, by the name each is saved under; the pack starts empty
export function inv_config(inventory_st: mut InventoryState, keys: []string) -> void {
inventory_st.iv_keys = keys
inv_clear(inventory_st)
}
export function inv_changes(base_st: mut BaseState, inventory_st: mut InventoryState) -> Queue<ItemChanged> {
if inventory_st.iv_changes == null { inventory_st.iv_changes = queue_new(base_st, "inventory.changes") }
return inventory_st.iv_changes
}
function iv_valid(inventory_st: InventoryState, it: int) -> bool { return inventory_st.iv_counts != null and it >= 0 and it < len(inventory_st.iv_counts) }
export function inv_kinds(inventory_st: InventoryState) -> int {
if inventory_st.iv_keys == null { return 0 }
return len(inventory_st.iv_keys)
}
export function inv_count(inventory_st: InventoryState, it: int) -> int {
if not iv_valid(inventory_st, it) { return 0 }
return inventory_st.iv_counts[it]
}
export function inv_limit(it: int) -> int {
return PackRules.stack_limit(it)
}
# how many more fit
export function inv_room(inventory_st: InventoryState, it: int) -> int {
if not iv_valid(inventory_st, it) { return 0 }
return Math.max(inv_limit(it) - inventory_st.iv_counts[it], 0)
}
export function inv_has(inventory_st: InventoryState, it: int, n: int) -> bool { return inv_count(inventory_st, it) >= n }
export function inv_total(inventory_st: InventoryState) -> int {
var t = 0
if inventory_st.iv_counts == null { return 0 }
for i in 0 .. len(inventory_st.iv_counts) { t += inventory_st.iv_counts[i] }
return t
}