wip(0.S3): packages and examples migrated again from their pre-0.S sources in one run
ludic migrate state packages <every example program> packages/ludic.lab/example/plate.ludic 1804 vars into 126 states, 64 into lets; 23498 edits in 460 files Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
5ffe50ed02
commit
19fcf60599
459 changed files with 17862 additions and 17603 deletions
|
|
@ -12,31 +12,33 @@ export property ItemChanged {
|
|||
after: int = 0
|
||||
}
|
||||
|
||||
var iv_keys: []string = null
|
||||
var iv_counts: []int = null
|
||||
var iv_changes: Queue<ItemChanged> = null
|
||||
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(keys: []string) -> void {
|
||||
iv_keys = keys
|
||||
inv_clear()
|
||||
export function inv_config(inventory_st: mut InventoryState, keys: []string) -> void {
|
||||
inventory_st.iv_keys = keys
|
||||
inv_clear(inventory_st)
|
||||
}
|
||||
|
||||
export function inv_changes() -> Queue<ItemChanged> {
|
||||
if iv_changes == null { iv_changes = queue_new("inventory.changes") }
|
||||
return iv_changes
|
||||
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(it: int) -> bool { return iv_counts != null and it >= 0 and it < len(iv_counts) }
|
||||
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() -> int {
|
||||
if iv_keys == null { return 0 }
|
||||
return len(iv_keys)
|
||||
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(it: int) -> int {
|
||||
if not iv_valid(it) { return 0 }
|
||||
return iv_counts[it]
|
||||
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 {
|
||||
|
|
@ -44,16 +46,16 @@ export function inv_limit(it: int) -> int {
|
|||
}
|
||||
|
||||
# how many more fit
|
||||
export function inv_room(it: int) -> int {
|
||||
if not iv_valid(it) { return 0 }
|
||||
return Math.max(inv_limit(it) - iv_counts[it], 0)
|
||||
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(it: int, n: int) -> bool { return inv_count(it) >= n }
|
||||
export function inv_has(inventory_st: InventoryState, it: int, n: int) -> bool { return inv_count(inventory_st, it) >= n }
|
||||
|
||||
export function inv_total() -> int {
|
||||
export function inv_total(inventory_st: InventoryState) -> int {
|
||||
var t = 0
|
||||
if iv_counts == null { return 0 }
|
||||
for i in 0 .. len(iv_counts) { t += iv_counts[i] }
|
||||
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,14 @@
|
|||
# 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() -> void {
|
||||
inv_clear()
|
||||
q_clear(inv_changes())
|
||||
export function inv_reset(base_st: mut BaseState, inventory_st: mut InventoryState) -> void {
|
||||
inv_clear(inventory_st)
|
||||
q_clear(base_st, inv_changes(base_st, inventory_st))
|
||||
}
|
||||
|
||||
export function inv_save() -> Val {
|
||||
export function inv_save(inventory_st: InventoryState) -> Val {
|
||||
let counts = Value.object()
|
||||
for i in 0 .. inv_kinds() {
|
||||
if iv_counts[i] != 0 { sv_put_int(counts, iv_keys[i], iv_counts[i]) }
|
||||
for i in 0 .. inv_kinds(inventory_st) {
|
||||
if inventory_st.iv_counts[i] != 0 { sv_put_int(counts, inventory_st.iv_keys[i], inventory_st.iv_counts[i]) }
|
||||
}
|
||||
let v = Value.object()
|
||||
Value.put(v, "counts", counts)
|
||||
|
|
@ -16,11 +16,11 @@ export function inv_save() -> Val {
|
|||
}
|
||||
|
||||
# a name this build does not know is dropped
|
||||
export function inv_load(v: Val, version: int) -> void {
|
||||
inv_clear()
|
||||
export function inv_load(inventory_st: mut InventoryState, v: Val, version: int) -> void {
|
||||
inv_clear(inventory_st)
|
||||
if Value.has(v, "counts") == 0 { return }
|
||||
let counts = Value.get(v, "counts")
|
||||
for i in 0 .. inv_kinds() { iv_counts[i] = Math.max(sv_int(counts, iv_keys[i], 0), 0) }
|
||||
for i in 0 .. inv_kinds(inventory_st) { inventory_st.iv_counts[i] = Math.max(sv_int(counts, inventory_st.iv_keys[i], 0), 0) }
|
||||
}
|
||||
|
||||
export function inv_system() -> System {
|
||||
|
|
|
|||
|
|
@ -23,74 +23,74 @@ program InventoryTest {
|
|||
|
||||
bind PackRules { stack_limit: fn limit }
|
||||
|
||||
function fresh() -> void {
|
||||
inv_config(names("wood", "rod", "fish"))
|
||||
inv_reset()
|
||||
function fresh(base_st: mut BaseState, inventory_st: mut InventoryState) -> void {
|
||||
inv_config(inventory_st, names("wood", "rod", "fish"))
|
||||
inv_reset(base_st, inventory_st)
|
||||
}
|
||||
|
||||
function changes() -> []ItemChanged { return q_drain(inv_changes()) }
|
||||
function waiting() -> int { return q_len(inv_changes()) }
|
||||
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)) }
|
||||
|
||||
test "add puts in what fits and says how many" {
|
||||
fresh()
|
||||
expect_eq(inv_add(WOOD, 7), 7)
|
||||
expect_eq(inv_add(WOOD, 7), 3)
|
||||
expect_eq(inv_count(WOOD), 10)
|
||||
expect_eq(inv_room(WOOD), 0)
|
||||
expect_eq(inv_add(ROD, 2), 1)
|
||||
expect_eq(inv_total(), 11)
|
||||
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)
|
||||
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_total(inventory_st), 11)
|
||||
}
|
||||
|
||||
test "take is all or none" {
|
||||
fresh()
|
||||
inv_add(FISH, 3)
|
||||
expect(not inv_take(FISH, 4))
|
||||
expect_eq(inv_count(FISH), 3)
|
||||
expect(inv_take(FISH, 3))
|
||||
expect(not inv_has(FISH, 1))
|
||||
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))
|
||||
expect_eq(inv_count(inventory_st, FISH), 3)
|
||||
expect(inv_take(base_st, inventory_st, FISH, 3))
|
||||
expect(not inv_has(inventory_st, FISH, 1))
|
||||
}
|
||||
|
||||
test "set goes past the room, never below zero" {
|
||||
fresh()
|
||||
inv_set(ROD, 3)
|
||||
expect_eq(inv_count(ROD), 3)
|
||||
expect_eq(inv_room(ROD), 0)
|
||||
inv_set(ROD, -2)
|
||||
expect_eq(inv_count(ROD), 0)
|
||||
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)
|
||||
expect_eq(inv_count(inventory_st, ROD), 3)
|
||||
expect_eq(inv_room(inventory_st, ROD), 0)
|
||||
inv_set(base_st, inventory_st, ROD, -2)
|
||||
expect_eq(inv_count(inventory_st, ROD), 0)
|
||||
}
|
||||
|
||||
test "every change is a fact, and no change is not" {
|
||||
fresh()
|
||||
inv_add(WOOD, 2)
|
||||
inv_take(WOOD, 1)
|
||||
inv_set(WOOD, 1)
|
||||
let cs = changes()
|
||||
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)
|
||||
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(), 0)
|
||||
expect_eq(waiting(base_st, inventory_st), 0)
|
||||
}
|
||||
|
||||
test "an item out of range is nothing" {
|
||||
fresh()
|
||||
expect_eq(inv_add(9, 1), 0)
|
||||
expect_eq(inv_count(-1), 0)
|
||||
expect(not inv_take(9, 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)
|
||||
expect_eq(inv_count(inventory_st, -1), 0)
|
||||
expect(not inv_take(base_st, inventory_st, 9, 0))
|
||||
}
|
||||
|
||||
test "the save is by name, so a reordered table still reads it" {
|
||||
fresh()
|
||||
inv_add(WOOD, 4)
|
||||
inv_add(FISH, 2)
|
||||
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)
|
||||
let root = save_tree()
|
||||
save_section(root, "inventory", 1, inv_save())
|
||||
save_section(root, "inventory", 1, inv_save(inventory_st))
|
||||
let text = save_encode(root)
|
||||
inv_config(names("fish", "bait", "wood"))
|
||||
inv_config(inventory_st, names("fish", "bait", "wood"))
|
||||
let n = load_section(save_decode(text), "inventory")
|
||||
inv_load(n.data, n.version)
|
||||
expect_eq(inv_count(0), 2)
|
||||
expect_eq(inv_count(1), 0)
|
||||
expect_eq(inv_count(2), 4)
|
||||
inv_load(inventory_st, n.data, n.version)
|
||||
expect_eq(inv_count(inventory_st, 0), 2)
|
||||
expect_eq(inv_count(inventory_st, 1), 0)
|
||||
expect_eq(inv_count(inventory_st, 2), 4)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,38 +1,38 @@
|
|||
# verbs.ludic - the only way a count changes, and each change is said once
|
||||
function iv_change(it: int, after: int) -> void {
|
||||
let before = iv_counts[it]
|
||||
function iv_change(base_st: mut BaseState, inventory_st: mut InventoryState, it: int, after: int) -> void {
|
||||
let before = inventory_st.iv_counts[it]
|
||||
if before == after { return }
|
||||
iv_counts[it] = after
|
||||
inventory_st.iv_counts[it] = after
|
||||
let c = new ItemChanged
|
||||
c.item = it
|
||||
c.before = before
|
||||
c.after = after
|
||||
q_push(inv_changes(), c)
|
||||
q_push(base_st, inv_changes(base_st, inventory_st), c)
|
||||
}
|
||||
|
||||
# as many of n as there is room for; returns how many went in
|
||||
export function inv_add(it: int, n: int) -> int {
|
||||
if not iv_valid(it) or n <= 0 { return 0 }
|
||||
let k = Math.min(n, inv_room(it))
|
||||
if k > 0 { iv_change(it, iv_counts[it] + k) }
|
||||
export function inv_add(base_st: mut BaseState, 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) }
|
||||
return k
|
||||
}
|
||||
|
||||
# all n or none
|
||||
export function inv_take(it: int, n: int) -> bool {
|
||||
if not iv_valid(it) or iv_counts[it] < n { return false }
|
||||
if n > 0 { iv_change(it, iv_counts[it] - n) }
|
||||
export function inv_take(base_st: mut BaseState, 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) }
|
||||
return true
|
||||
}
|
||||
|
||||
# a count outright, room or not (a starting kit, a correction, the network's word); never below 0
|
||||
export function inv_set(it: int, n: int) -> void {
|
||||
if not iv_valid(it) { return }
|
||||
iv_change(it, Math.max(n, 0))
|
||||
export function inv_set(base_st: mut BaseState, 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))
|
||||
}
|
||||
|
||||
# everything gone, silently: a new trip, or before a load
|
||||
export function inv_clear() -> void {
|
||||
iv_counts = new []int
|
||||
for i in 0 .. inv_kinds() { push(iv_counts, 0) }
|
||||
export function inv_clear(inventory_st: mut InventoryState) -> void {
|
||||
inventory_st.iv_counts = new []int
|
||||
for i in 0 .. inv_kinds(inventory_st) { push(inventory_st.iv_counts, 0) }
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue