ludic/packages/ludic.inventory/state.ludic
Orkuncakilkaya 71735b10a2 feat(base): 0.R4 - a queue keeps its own count, so every package verb takes only its own state; ludic migrate state --prune
ludic.base's Queue<T> carries a QueueTag (its name and pending count): queue_new(name), q_push(q, v),
q_drain(q), q_clear(q) take no BaseState, and core_undrained(tags) names the given queues still holding
facts. A reducer on a package's state can now call that package's verbs (wallet_earn(wallet_st, n)).

ludic migrate state --prune (ludicc --migrate-prune) takes out each state parameter a function no
longer uses, nor anything it calls, and the argument that fills it - including an argument for a
parameter the callee has dropped, which is taken out before the call is checked, so a generic's T is
told by the argument that says it. A reducer keeps its state. --dry-run now counts the edits it would
make. Every package was moved with it: 608 base_st parameters and their arguments, 1889 edits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 04:19:56 +03:00

61 lines
2 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(inventory_st: mut InventoryState) -> Queue<ItemChanged> {
if inventory_st.iv_changes == null { inventory_st.iv_changes = queue_new("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
}