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>
This commit is contained in:
parent
8cf4b3fed6
commit
71735b10a2
151 changed files with 58668 additions and 54719 deletions
|
|
@ -24,8 +24,8 @@ export function inv_config(inventory_st: mut InventoryState, keys: []string) ->
|
|||
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") }
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
# system.ludic - the pack as a system: reset and its own save section, each count under its
|
||||
# kind's name, so kinds can be added or reordered without a migration
|
||||
export function inv_reset(base_st: mut BaseState, inventory_st: mut InventoryState) -> void {
|
||||
export function inv_reset(inventory_st: mut InventoryState) -> void {
|
||||
inv_clear(inventory_st)
|
||||
q_clear(base_st, inv_changes(base_st, inventory_st))
|
||||
q_clear(inv_changes(inventory_st))
|
||||
}
|
||||
|
||||
export function inv_save(inventory_st: InventoryState) -> Val {
|
||||
|
|
|
|||
|
|
@ -23,66 +23,66 @@ program InventoryTest {
|
|||
|
||||
bind PackRules { stack_limit: fn limit }
|
||||
|
||||
function fresh(base_st: mut BaseState, inventory_st: mut InventoryState) -> void {
|
||||
function fresh(inventory_st: mut InventoryState) -> void {
|
||||
inv_config(inventory_st, names("wood", "rod", "fish"))
|
||||
inv_reset(base_st, inventory_st)
|
||||
inv_reset(inventory_st)
|
||||
}
|
||||
|
||||
function changes(base_st: mut BaseState, inventory_st: mut InventoryState) -> []ItemChanged { return q_drain(base_st, inv_changes(base_st, inventory_st)) }
|
||||
function waiting(base_st: mut BaseState, inventory_st: mut InventoryState) -> int { return q_len(inv_changes(base_st, inventory_st)) }
|
||||
function changes(inventory_st: mut InventoryState) -> []ItemChanged { return q_drain(inv_changes(inventory_st)) }
|
||||
function waiting(inventory_st: mut InventoryState) -> int { return q_len(inv_changes(inventory_st)) }
|
||||
|
||||
test "add puts in what fits and says how many" (base_st: mut BaseState, inventory_st: mut InventoryState) {
|
||||
fresh(base_st, inventory_st)
|
||||
expect_eq(inv_add(base_st, inventory_st, WOOD, 7), 7)
|
||||
expect_eq(inv_add(base_st, inventory_st, WOOD, 7), 3)
|
||||
test "add puts in what fits and says how many" (inventory_st: mut InventoryState) {
|
||||
fresh(inventory_st)
|
||||
expect_eq(inv_add(inventory_st, WOOD, 7), 7)
|
||||
expect_eq(inv_add(inventory_st, WOOD, 7), 3)
|
||||
expect_eq(inv_count(inventory_st, WOOD), 10)
|
||||
expect_eq(inv_room(inventory_st, WOOD), 0)
|
||||
expect_eq(inv_add(base_st, inventory_st, ROD, 2), 1)
|
||||
expect_eq(inv_add(inventory_st, ROD, 2), 1)
|
||||
expect_eq(inv_total(inventory_st), 11)
|
||||
}
|
||||
|
||||
test "take is all or none" (base_st: mut BaseState, inventory_st: mut InventoryState) {
|
||||
fresh(base_st, inventory_st)
|
||||
inv_add(base_st, inventory_st, FISH, 3)
|
||||
expect(not inv_take(base_st, inventory_st, FISH, 4))
|
||||
test "take is all or none" (inventory_st: mut InventoryState) {
|
||||
fresh(inventory_st)
|
||||
inv_add(inventory_st, FISH, 3)
|
||||
expect(not inv_take(inventory_st, FISH, 4))
|
||||
expect_eq(inv_count(inventory_st, FISH), 3)
|
||||
expect(inv_take(base_st, inventory_st, FISH, 3))
|
||||
expect(inv_take(inventory_st, FISH, 3))
|
||||
expect(not inv_has(inventory_st, FISH, 1))
|
||||
}
|
||||
|
||||
test "set goes past the room, never below zero" (base_st: mut BaseState, inventory_st: mut InventoryState) {
|
||||
fresh(base_st, inventory_st)
|
||||
inv_set(base_st, inventory_st, ROD, 3)
|
||||
test "set goes past the room, never below zero" (inventory_st: mut InventoryState) {
|
||||
fresh(inventory_st)
|
||||
inv_set(inventory_st, ROD, 3)
|
||||
expect_eq(inv_count(inventory_st, ROD), 3)
|
||||
expect_eq(inv_room(inventory_st, ROD), 0)
|
||||
inv_set(base_st, inventory_st, ROD, -2)
|
||||
inv_set(inventory_st, ROD, -2)
|
||||
expect_eq(inv_count(inventory_st, ROD), 0)
|
||||
}
|
||||
|
||||
test "every change is a fact, and no change is not" (base_st: mut BaseState, inventory_st: mut InventoryState) {
|
||||
fresh(base_st, inventory_st)
|
||||
inv_add(base_st, inventory_st, WOOD, 2)
|
||||
inv_take(base_st, inventory_st, WOOD, 1)
|
||||
inv_set(base_st, inventory_st, WOOD, 1)
|
||||
let cs = changes(base_st, inventory_st)
|
||||
test "every change is a fact, and no change is not" (inventory_st: mut InventoryState) {
|
||||
fresh(inventory_st)
|
||||
inv_add(inventory_st, WOOD, 2)
|
||||
inv_take(inventory_st, WOOD, 1)
|
||||
inv_set(inventory_st, WOOD, 1)
|
||||
let cs = changes(inventory_st)
|
||||
expect_eq(len(cs), 2)
|
||||
expect_eq(cs[0].after, 2)
|
||||
expect_eq(cs[1].before, 2)
|
||||
expect_eq(cs[1].after, 1)
|
||||
expect_eq(waiting(base_st, inventory_st), 0)
|
||||
expect_eq(waiting(inventory_st), 0)
|
||||
}
|
||||
|
||||
test "an item out of range is nothing" (base_st: mut BaseState, inventory_st: mut InventoryState) {
|
||||
fresh(base_st, inventory_st)
|
||||
expect_eq(inv_add(base_st, inventory_st, 9, 1), 0)
|
||||
test "an item out of range is nothing" (inventory_st: mut InventoryState) {
|
||||
fresh(inventory_st)
|
||||
expect_eq(inv_add(inventory_st, 9, 1), 0)
|
||||
expect_eq(inv_count(inventory_st, -1), 0)
|
||||
expect(not inv_take(base_st, inventory_st, 9, 0))
|
||||
expect(not inv_take(inventory_st, 9, 0))
|
||||
}
|
||||
|
||||
test "the save is by name, so a reordered table still reads it" (base_st: mut BaseState, inventory_st: mut InventoryState) {
|
||||
fresh(base_st, inventory_st)
|
||||
inv_add(base_st, inventory_st, WOOD, 4)
|
||||
inv_add(base_st, inventory_st, FISH, 2)
|
||||
test "the save is by name, so a reordered table still reads it" (inventory_st: mut InventoryState) {
|
||||
fresh(inventory_st)
|
||||
inv_add(inventory_st, WOOD, 4)
|
||||
inv_add(inventory_st, FISH, 2)
|
||||
let root = save_tree()
|
||||
save_section(root, "inventory", 1, inv_save(inventory_st))
|
||||
let text = save_encode(root)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
# verbs.ludic - the only way a count changes, and each change is said once
|
||||
function iv_change(base_st: mut BaseState, inventory_st: mut InventoryState, it: int, after: int) -> void {
|
||||
function iv_change(inventory_st: mut InventoryState, it: int, after: int) -> void {
|
||||
let before = inventory_st.iv_counts[it]
|
||||
if before == after { return }
|
||||
inventory_st.iv_counts[it] = after
|
||||
|
|
@ -7,28 +7,28 @@ function iv_change(base_st: mut BaseState, inventory_st: mut InventoryState, it:
|
|||
c.item = it
|
||||
c.before = before
|
||||
c.after = after
|
||||
q_push(base_st, inv_changes(base_st, inventory_st), c)
|
||||
q_push(inv_changes(inventory_st), c)
|
||||
}
|
||||
|
||||
# as many of n as there is room for; returns how many went in
|
||||
export function inv_add(base_st: mut BaseState, inventory_st: mut InventoryState, it: int, n: int) -> int {
|
||||
export function inv_add(inventory_st: mut InventoryState, it: int, n: int) -> int {
|
||||
if not iv_valid(inventory_st, it) or n <= 0 { return 0 }
|
||||
let k = Math.min(n, inv_room(inventory_st, it))
|
||||
if k > 0 { iv_change(base_st, inventory_st, it, inventory_st.iv_counts[it] + k) }
|
||||
if k > 0 { iv_change(inventory_st, it, inventory_st.iv_counts[it] + k) }
|
||||
return k
|
||||
}
|
||||
|
||||
# all n or none
|
||||
export function inv_take(base_st: mut BaseState, inventory_st: mut InventoryState, it: int, n: int) -> bool {
|
||||
export function inv_take(inventory_st: mut InventoryState, it: int, n: int) -> bool {
|
||||
if not iv_valid(inventory_st, it) or inventory_st.iv_counts[it] < n { return false }
|
||||
if n > 0 { iv_change(base_st, inventory_st, it, inventory_st.iv_counts[it] - n) }
|
||||
if n > 0 { iv_change(inventory_st, it, inventory_st.iv_counts[it] - n) }
|
||||
return true
|
||||
}
|
||||
|
||||
# a count outright, room or not (a starting kit, a correction, the network's word); never below 0
|
||||
export function inv_set(base_st: mut BaseState, inventory_st: mut InventoryState, it: int, n: int) -> void {
|
||||
export function inv_set(inventory_st: mut InventoryState, it: int, n: int) -> void {
|
||||
if not iv_valid(inventory_st, it) { return }
|
||||
iv_change(base_st, inventory_st, it, Math.max(n, 0))
|
||||
iv_change(inventory_st, it, Math.max(n, 0))
|
||||
}
|
||||
|
||||
# everything gone, silently: a new trip, or before a load
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue