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
|
|
@ -39,8 +39,8 @@ import "ludic.base"
|
|||
| --- | --- |
|
||||
| `Tick { dt, frame, hours }`, `tick_new(dt, frame, hours)` | what a system's tick receives |
|
||||
| `PH_INPUT`, `PH_SENSE`, `PH_SIMULATE`, `PH_RESOLVE`, `PH_COMMIT`, `PH_PRESENT`, `PH_COUNT` | the phases, in the order they run |
|
||||
| `Queue<T>`, `queue_new<T>(name)`, `q_push(q, v)`, `q_drain(q) -> []T`, `q_len(q)`, `q_clear(q)` | facts, first in first out (`queue_new`, not `q_new`: that name is render3d's quaternion) |
|
||||
| `core_undrained() -> []string` | the names of queues still holding facts; ask it at the end of a frame |
|
||||
| `Queue<T>`, `queue_new<T>(name)`, `q_push(q, v)`, `q_drain(q) -> []T`, `q_len(q)`, `q_clear(q)` | facts, first in first out (`queue_new`, not `q_new`: that name is render3d's quaternion). A queue keeps its own count, so its verbs take only the queue: a mechanic's verbs take only the mechanic's own state |
|
||||
| `q_tag(q) -> QueueTag`, `core_undrained(tags) -> []string` | the names of the given queues still holding facts; ask it at the end of a frame |
|
||||
| `Rng`, `rng_new(seed)`, `rng_seed(r, seed)`, `rng_next(r)`, `rng_float(r)`, `rng_between(r, lo, hi)`, `rng_span(r, lo, hi)` | a xorshift32 stream of its own (`rng_between` is inclusive; `rng_range` is taken by `Random.range`) |
|
||||
| `SaveNode { found, version, data }`, `save_tree()`, `save_section(root, key, version, v)`, `load_section(root, key) -> SaveNode`, `save_encode`, `save_decode` | the save tree: `{"fishing": {"v": 3, ...}}`; `v` is reserved in a section, and a non-object value rides under `data` |
|
||||
| `sv_int`, `sv_float`, `sv_bool`, `sv_str` (key, fallback), `sv_ints` | typed reads with a fallback; a float is kept as thousandths |
|
||||
|
|
@ -68,17 +68,17 @@ export function fishing_cast(toy_fishing_st: mut ToyFishingState, x: float, z: f
|
|||
toy_fishing_st.casts += 1
|
||||
return true
|
||||
}
|
||||
function fishing_reset(base_st: mut BaseState, toy_fishing_st: mut ToyFishingState) -> void {
|
||||
function fishing_reset(toy_fishing_st: mut ToyFishingState) -> void {
|
||||
toy_fishing_st.casts = 0
|
||||
toy_fishing_st.dice = rng_new(7)
|
||||
toy_fishing_st.caught = queue_new(base_st, "fishing.caught")
|
||||
toy_fishing_st.caught = queue_new("fishing.caught")
|
||||
}
|
||||
function fishing_tick(base_st: mut BaseState, toy_fishing_st: mut ToyFishingState, t: Tick) -> void {
|
||||
function fishing_tick(toy_fishing_st: mut ToyFishingState, t: Tick) -> void {
|
||||
while toy_fishing_st.casts > 0 {
|
||||
let c = new Caught
|
||||
c.species = rng_between(toy_fishing_st.dice, 0, 2)
|
||||
c.weight = 0.5 + rng_float(toy_fishing_st.dice)
|
||||
q_push(base_st, toy_fishing_st.caught, c)
|
||||
q_push(toy_fishing_st.caught, c)
|
||||
toy_fishing_st.casts -= 1
|
||||
}
|
||||
}
|
||||
|
|
@ -106,8 +106,8 @@ program Game {
|
|||
bind FishingWorld { is_water: fn lake }
|
||||
|
||||
# the route: a landed fish goes into the pack
|
||||
function route_fishing_pack(base_st: mut BaseState, fishing: ToyFishingState, pack: mut ToyPackState, t: Tick) -> void {
|
||||
let fish = q_drain(base_st, fishing.caught)
|
||||
function route_fishing_pack(fishing: ToyFishingState, pack: mut ToyPackState, t: Tick) -> void {
|
||||
let fish = q_drain(fishing.caught)
|
||||
for i in 0 .. len(fish) { pack_add(pack, fish[i].species, 1) }
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,60 +2,58 @@
|
|||
# game drains it in a later phase. First in, first out; nothing here rolls dice or reorders.
|
||||
export property Queue<T> {
|
||||
items: []T = null
|
||||
id: int = -1
|
||||
tag: QueueTag = null
|
||||
}
|
||||
# what a queue says about itself, whatever it holds: its name and how many facts wait in it
|
||||
export property QueueTag {
|
||||
name: string = ""
|
||||
pending: int = 0
|
||||
}
|
||||
|
||||
# every named queue's pending count, so a frame's end can say which facts nobody read
|
||||
# BaseState is the system runner's (system.ludic). A queue keeps its own count, so pushing and
|
||||
# draining one takes only the state that owns the queue.
|
||||
export state BaseState {
|
||||
qb_names: []string = null
|
||||
qb_pending: []int = null
|
||||
cs_list: []System = null
|
||||
}
|
||||
|
||||
function qb_init(base_st: mut BaseState) -> void {
|
||||
if base_st.qb_names != null { return }
|
||||
base_st.qb_names = new []string
|
||||
base_st.qb_pending = new []int
|
||||
}
|
||||
|
||||
# a queue; the name is only for core_undrained
|
||||
export function queue_new<T>(base_st: mut BaseState, name: string) -> Queue<T> {
|
||||
qb_init(base_st)
|
||||
export function queue_new<T>(name: string) -> Queue<T> {
|
||||
let q = new Queue<T>
|
||||
q.items = new []T
|
||||
q.id = len(base_st.qb_names)
|
||||
push(base_st.qb_names, name)
|
||||
push(base_st.qb_pending, 0)
|
||||
q.tag = new QueueTag
|
||||
q.tag.name = name
|
||||
return q
|
||||
}
|
||||
|
||||
export function q_push<T>(base_st: mut BaseState, q: Queue<T>, v: T) -> void {
|
||||
export function q_push<T>(q: Queue<T>, v: T) -> void {
|
||||
push(q.items, v)
|
||||
base_st.qb_pending[q.id] = len(q.items)
|
||||
q.tag.pending = len(q.items)
|
||||
}
|
||||
|
||||
# the facts in the order they were pushed; the queue is empty afterwards
|
||||
export function q_drain<T>(base_st: mut BaseState, q: Queue<T>) -> []T {
|
||||
export function q_drain<T>(q: Queue<T>) -> []T {
|
||||
let out = q.items
|
||||
q.items = new []T
|
||||
base_st.qb_pending[q.id] = 0
|
||||
q.tag.pending = 0
|
||||
return out
|
||||
}
|
||||
|
||||
export function q_len<T>(q: Queue<T>) -> int { return len(q.items) }
|
||||
|
||||
export function q_clear<T>(base_st: mut BaseState, q: Queue<T>) -> void {
|
||||
export function q_clear<T>(q: Queue<T>) -> void {
|
||||
q.items = new []T
|
||||
base_st.qb_pending[q.id] = 0
|
||||
q.tag.pending = 0
|
||||
}
|
||||
|
||||
# the names of the queues still holding facts; call it at the end of a frame, where a fact
|
||||
# a queue's tag, for core_undrained: queues of different facts in one list
|
||||
export function q_tag<T>(q: Queue<T>) -> QueueTag { return q.tag }
|
||||
|
||||
# the names of the given queues still holding facts; ask it at the end of a frame, where a fact
|
||||
# left behind is a route nobody wrote
|
||||
export function core_undrained(base_st: mut BaseState) -> []string {
|
||||
qb_init(base_st)
|
||||
export function core_undrained(tags: []QueueTag) -> []string {
|
||||
let out = new []string
|
||||
for i in 0 .. len(base_st.qb_names) {
|
||||
if base_st.qb_pending[i] > 0 { push(out, base_st.qb_names[i]) }
|
||||
for i in 0 .. len(tags) {
|
||||
if tags[i] != null and tags[i].pending > 0 { push(out, tags[i].name) }
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,52 +12,64 @@ program QueueTest {
|
|||
return c
|
||||
}
|
||||
|
||||
function fifo_case(base_st: mut BaseState) -> void {
|
||||
let q: Queue<int> = queue_new(base_st, "ints")
|
||||
q_push(base_st, q, 3)
|
||||
q_push(base_st, q, 1)
|
||||
q_push(base_st, q, 2)
|
||||
function fifo_case() -> void {
|
||||
let q: Queue<int> = queue_new("ints")
|
||||
q_push(q, 3)
|
||||
q_push(q, 1)
|
||||
q_push(q, 2)
|
||||
expect_eq(q_len(q), 3)
|
||||
let xs = q_drain(base_st, q)
|
||||
let xs = q_drain(q)
|
||||
expect_eq(len(xs), 3)
|
||||
expect_eq(xs[0], 3)
|
||||
expect_eq(xs[1], 1)
|
||||
expect_eq(xs[2], 2)
|
||||
expect_eq(q_len(q), 0)
|
||||
expect_eq(len(q_drain(base_st, q)), 0)
|
||||
expect_eq(len(q_drain(q)), 0)
|
||||
}
|
||||
|
||||
function record_case(base_st: mut BaseState) -> void {
|
||||
let q: Queue<Caught> = queue_new(base_st, "caught")
|
||||
q_push(base_st, q, caught(2, 1.5))
|
||||
q_push(base_st, q, caught(4, 0.25))
|
||||
let cs = q_drain(base_st, q)
|
||||
function record_case() -> void {
|
||||
let q: Queue<Caught> = queue_new("caught")
|
||||
q_push(q, caught(2, 1.5))
|
||||
q_push(q, caught(4, 0.25))
|
||||
let cs = q_drain(q)
|
||||
expect_eq(cs[1].species, 4)
|
||||
expect(cs[0].weight == 1.5)
|
||||
}
|
||||
|
||||
function clear_case(base_st: mut BaseState) -> void {
|
||||
let q: Queue<string> = queue_new(base_st, "words")
|
||||
q_push(base_st, q, "a")
|
||||
q_clear(base_st, q)
|
||||
function clear_case() -> void {
|
||||
let q: Queue<string> = queue_new("words")
|
||||
q_push(q, "a")
|
||||
q_clear(q)
|
||||
expect_eq(q_len(q), 0)
|
||||
}
|
||||
|
||||
function undrained_case(base_st: mut BaseState) -> void {
|
||||
let a: Queue<int> = queue_new(base_st, "left_behind")
|
||||
let b: Queue<int> = queue_new(base_st, "read")
|
||||
q_push(base_st, a, 1)
|
||||
q_push(base_st, b, 2)
|
||||
q_drain(base_st, b)
|
||||
let names = core_undrained(base_st)
|
||||
expect_eq(len(names), 1)
|
||||
expect(names[0] == "left_behind")
|
||||
q_drain(base_st, a)
|
||||
expect_eq(len(core_undrained(base_st)), 0)
|
||||
# nothing but the queue says what T is: a drain and a clear still know it
|
||||
function infer_case() -> void {
|
||||
let q: Queue<Caught> = queue_new("told")
|
||||
q_push(q, caught(1, 2.0))
|
||||
expect_eq(q_drain(q)[0].species, 1)
|
||||
q_push(q, caught(3, 1.0))
|
||||
q_clear(q)
|
||||
expect_eq(q_len(q), 0)
|
||||
}
|
||||
|
||||
test "a queue drains in the order it was pushed" (base_st: mut BaseState) { fifo_case(base_st) }
|
||||
test "a queue holds records" (base_st: mut BaseState) { record_case(base_st) }
|
||||
test "clear drops what is waiting" (base_st: mut BaseState) { clear_case(base_st) }
|
||||
test "the undrained queues are named, and a drain clears the report" (base_st: mut BaseState) { undrained_case(base_st) }
|
||||
function undrained_case() -> void {
|
||||
let a: Queue<int> = queue_new("left_behind")
|
||||
let b: Queue<int> = queue_new("read")
|
||||
q_push(a, 1)
|
||||
q_push(b, 2)
|
||||
q_drain(b)
|
||||
let tags = [q_tag(a), q_tag(b)]
|
||||
let names = core_undrained(tags)
|
||||
expect_eq(len(names), 1)
|
||||
expect(names[0] == "left_behind")
|
||||
q_drain(a)
|
||||
expect_eq(len(core_undrained(tags)), 0)
|
||||
}
|
||||
|
||||
test "a queue drains in the order it was pushed" () { fifo_case() }
|
||||
test "a queue holds records" () { record_case() }
|
||||
test "clear drops what is waiting" () { clear_case() }
|
||||
test "a queue's element type is told by the queue alone" () { infer_case() }
|
||||
test "the undrained queues are named, and a drain clears the report" () { undrained_case() }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,8 +10,8 @@ program RouteTest {
|
|||
bind FishingWorld { is_water: fn water_everywhere_but_land }
|
||||
|
||||
# the route: a landed fish goes into the pack
|
||||
function route_fishing_pack(base_st: mut BaseState, toy_fishing_st: ToyFishingState, toy_pack_st: mut ToyPackState, t: Tick) -> void {
|
||||
let fish = q_drain(base_st, toy_fishing_st.caught)
|
||||
function route_fishing_pack(toy_fishing_st: ToyFishingState, toy_pack_st: mut ToyPackState, t: Tick) -> void {
|
||||
let fish = q_drain(toy_fishing_st.caught)
|
||||
for i in 0 .. len(fish) { pack_add(toy_pack_st, fish[i].species, 1) }
|
||||
}
|
||||
|
||||
|
|
@ -35,7 +35,7 @@ program RouteTest {
|
|||
expect(not fishing_cast(toy_fishing_st, 500.0, 2.0))
|
||||
core_tick_all(base_st, tick_new(1.0 / 60.0, 0, 0.0))
|
||||
expect_eq(pack_total(toy_pack_st), 3)
|
||||
expect_eq(len(core_undrained(base_st)), 0)
|
||||
expect_eq(len(core_undrained([q_tag(toy_fishing_st.caught)])), 0)
|
||||
}
|
||||
|
||||
test "the pack saves its own section and the fishing, which saves nothing, is reset" (base_st: mut BaseState, toy_fishing_st: mut ToyFishingState, toy_pack_st: ToyPackState) {
|
||||
|
|
|
|||
|
|
@ -14,17 +14,17 @@ export function fishing_cast(toy_fishing_st: mut ToyFishingState, x: float, z: f
|
|||
toy_fishing_st.casts += 1
|
||||
return true
|
||||
}
|
||||
function fishing_reset(base_st: mut BaseState, toy_fishing_st: mut ToyFishingState) -> void {
|
||||
function fishing_reset(toy_fishing_st: mut ToyFishingState) -> void {
|
||||
toy_fishing_st.casts = 0
|
||||
toy_fishing_st.dice = rng_new(7)
|
||||
toy_fishing_st.caught = queue_new(base_st, "fishing.caught")
|
||||
toy_fishing_st.caught = queue_new("fishing.caught")
|
||||
}
|
||||
function fishing_tick(base_st: mut BaseState, toy_fishing_st: mut ToyFishingState, t: Tick) -> void {
|
||||
function fishing_tick(toy_fishing_st: mut ToyFishingState, t: Tick) -> void {
|
||||
while toy_fishing_st.casts > 0 {
|
||||
let c = new Caught
|
||||
c.species = rng_between(toy_fishing_st.dice, 0, 2)
|
||||
c.weight = 0.5 + rng_float(toy_fishing_st.dice)
|
||||
q_push(base_st, toy_fishing_st.caught, c)
|
||||
q_push(toy_fishing_st.caught, c)
|
||||
toy_fishing_st.casts -= 1
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue