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:
Orkun ÇAKILKAYA 2026-09-26 04:19:56 +03:00
parent 8cf4b3fed6
commit 71735b10a2
151 changed files with 58668 additions and 54719 deletions

View file

@ -5,29 +5,29 @@
export function ses_sleeping(session_st: SessionState) -> bool { return session_st.session__me_sleeping }
# this player turns in (or gets up): false alone, where a bed simply sleeps
export function ses_sleep(base_st: mut BaseState, session_st: mut SessionState, on: bool) -> bool {
export function ses_sleep(session_st: mut SessionState, on: bool) -> bool {
if not ses_live(session_st) { return false }
if on == session_st.session__me_sleeping { return on }
session_st.session__me_sleeping = on
let f = session__fact(base_st, session_st, SES_SLEEP, 0, 0)
let f = session__fact(session_st, SES_SLEEP, 0, 0)
f.yes = on
if on { ses_night_check(base_st, session_st) }
if on { ses_night_check(session_st) }
return true
}
# the host: a guest said it turned in, or got up
export function ses_sleep_ready(base_st: mut BaseState, session_st: mut SessionState, p: int, on: bool) -> void {
export function ses_sleep_ready(session_st: mut SessionState, p: int, on: bool) -> void {
if p <= 0 or not ply_on(session_st, p) { return }
session_st.session__slot[p].ready = on
ses_night_check(base_st, session_st)
ses_night_check(session_st)
}
# the host: is everyone in? Then the night is due, and everyone's readiness starts again
export function ses_night_check(base_st: mut BaseState, session_st: mut SessionState) -> void {
export function ses_night_check(session_st: mut SessionState) -> void {
if session_st.session__role != SES_HOST or not session_st.session__me_sleeping { return }
for p in 1 .. PLY_MAX { if ply_on(session_st, p) and not session_st.session__slot[p].ready { return } }
for p in 1 .. PLY_MAX { session_st.session__slot[p].ready = false }
session__fact(base_st, session_st, SES_NIGHT, -1, 0)
session__fact(session_st, SES_NIGHT, -1, 0)
}
# the night went by on this machine: awake again

View file

@ -2,10 +2,10 @@
# guest owns only its own player. The transport says what this machine became.
export function ses_role(session_st: SessionState) -> int { return session_st.session__role }
export function ses_set_role(base_st: mut BaseState, session_st: mut SessionState, r: int) -> void {
export function ses_set_role(session_st: mut SessionState, r: int) -> void {
if r == session_st.session__role { return }
session_st.session__role = r
session__fact(base_st, session_st, SES_ROLE, -1, 0)
session__fact(session_st, SES_ROLE, -1, 0)
}
# this machine runs the world: the clock, the weather, the animals, the people
@ -37,9 +37,9 @@ export function ses_open(session_st: SessionState) -> bool { return session_st.s
export function ses_room_for_one(session_st: SessionState) -> bool { return session_st.session__open and ply_count(session_st) < session_st.session__max }
# the session is over: everyone else gone, alone again, nothing asked and nobody asleep
export function ses_reset(base_st: mut BaseState, session_st: mut SessionState) -> void {
ply_leave_all(base_st, session_st)
ses_set_role(base_st, session_st, SES_SOLO)
export function ses_reset(session_st: mut SessionState) -> void {
ply_leave_all(session_st)
ses_set_role(session_st, SES_SOLO)
session_st.session__me_sleeping = false
session_st.session__vote_t = 0.0
session_st.session__vote_mine = -1

View file

@ -25,7 +25,7 @@ export function ply_count(session_st: SessionState) -> int {
# a remote player comes in, standing where this one stands until they say: the first free
# slot, or -1 when the party is full
export function ply_join(base_st: mut BaseState, session_st: mut SessionState, pid: int) -> int {
export function ply_join(session_st: mut SessionState, pid: int) -> int {
session__init(session_st)
if pid <= 0 { return -1 } # 0 is this machine's player
for p in 1 .. PLY_MAX {
@ -37,21 +37,21 @@ export function ply_join(base_st: mut BaseState, session_st: mut SessionState, p
r.y = SessionLocal.y()
r.z = SessionLocal.z()
session_st.session__slot[p] = r
session__fact(base_st, session_st, SES_JOINED, p, pid)
session__fact(session_st, SES_JOINED, p, pid)
return p
}
return -1
}
export function ply_leave(base_st: mut BaseState, session_st: mut SessionState, p: int) -> void {
export function ply_leave(session_st: mut SessionState, p: int) -> void {
if p <= 0 or not ply_on(session_st, p) { return }
let r = session_st.session__slot[p]
r.on = false
r.ready = false
session__fact(base_st, session_st, SES_LEFT, p, r.pid)
session__fact(session_st, SES_LEFT, p, r.pid)
}
export function ply_leave_all(base_st: mut BaseState, session_st: mut SessionState) -> void { for p in 1 .. PLY_MAX { ply_leave(base_st, session_st, p) } }
export function ply_leave_all(session_st: mut SessionState) -> void { for p in 1 .. PLY_MAX { ply_leave(session_st, p) } }
# what a remote player's machine reported this tick
export function ply_report(session_st: mut SessionState, p: int, x: float, y: float, z: float, yaw: float, speed: float) -> void {

View file

@ -78,18 +78,18 @@ export state SessionState {
session__vote_passed: int = -1 # the last vote: -1 none, 0 failed, 1 passed
}
export function ses_facts(base_st: mut BaseState, session_st: mut SessionState) -> Queue<SessionFact> {
if session_st.session__facts == null { session_st.session__facts = queue_new(base_st, "session.facts") }
export function ses_facts(session_st: mut SessionState) -> Queue<SessionFact> {
if session_st.session__facts == null { session_st.session__facts = queue_new("session.facts") }
return session_st.session__facts
}
function session__fact(base_st: mut BaseState, session_st: mut SessionState, what: int, slot: int, pid: int) -> SessionFact {
function session__fact(session_st: mut SessionState, what: int, slot: int, pid: int) -> SessionFact {
let f = new SessionFact
f.what = what
f.slot = slot
f.pid = pid
f.role = session_st.session__role
q_push(base_st, ses_facts(base_st, session_st), f)
q_push(ses_facts(session_st), f)
return f
}

View file

@ -14,164 +14,164 @@ program SessionTest {
function fname() -> string { return "me" }
bind SessionLocal { x: fn fx, y: fn fy, z: fn fz, name: fn fname }
function whats(base_st: mut BaseState, session_st: mut SessionState) -> []int {
let fs = q_drain(base_st, ses_facts(base_st, session_st))
function whats(session_st: mut SessionState) -> []int {
let fs = q_drain(ses_facts(session_st))
let out = new []int
for i in 0 .. len(fs) { push(out, fs[i].what) }
return out
}
function last_of(base_st: mut BaseState, session_st: mut SessionState, what: int) -> SessionFact {
let fs = q_drain(base_st, ses_facts(base_st, session_st))
function last_of(session_st: mut SessionState, what: int) -> SessionFact {
let fs = q_drain(ses_facts(session_st))
var f: SessionFact = null
for i in 0 .. len(fs) { if fs[i].what == what { f = fs[i] } }
return f
}
# a host with n guests, pids 2.., facts drained
function party(base_st: mut BaseState, session_st: mut SessionState, n: int) -> void {
function party(session_st: mut SessionState, n: int) -> void {
ply_init(session_st)
ses_set_role(base_st, session_st, SES_HOST)
for i in 0 .. n { ply_join(base_st, session_st, 2 + i) }
q_clear(base_st, ses_facts(base_st, session_st))
ses_set_role(session_st, SES_HOST)
for i in 0 .. n { ply_join(session_st, 2 + i) }
q_clear(ses_facts(session_st))
}
test "slot 0 is the local player, read live; others join at its feet and leave" (base_st: mut BaseState, session_st: mut SessionState, session_test_st: mut SessionTestState) {
test "slot 0 is the local player, read live; others join at its feet and leave" (session_st: mut SessionState, session_test_st: mut SessionTestState) {
ply_init(session_st)
session_test_st.me_x = 5.0
expect(ply_on(session_st, 0))
expect_near(ply_x(session_st, 0), 5.0, 0.001)
expect(ply_name(session_st, 0) == "me")
let p = ply_join(base_st, session_st, 7)
let p = ply_join(session_st, 7)
expect_eq(p, 1)
expect_near(ply_x(session_st, p), 5.0, 0.001)
expect_eq(ply_slot_of(session_st, 7), 1)
expect_eq(ply_count(session_st), 2)
expect_eq(ply_join(base_st, session_st, 0), -1)
ply_leave(base_st, session_st, p)
expect_eq(ply_join(session_st, 0), -1)
ply_leave(session_st, p)
expect_eq(ply_slot_of(session_st, 7), -1)
expect_eq(ply_count(session_st), 1)
let w = whats(base_st, session_st)
let w = whats(session_st)
expect_eq(len(w), 2)
expect_eq(w[0], SES_JOINED)
expect_eq(w[1], SES_LEFT)
}
test "the roster fills at twelve" (base_st: mut BaseState, session_st: mut SessionState) {
test "the roster fills at twelve" (session_st: mut SessionState) {
ply_init(session_st)
for i in 0 .. PLY_MAX - 1 { expect(ply_join(base_st, session_st, 10 + i) > 0) }
expect_eq(ply_join(base_st, session_st, 99), -1)
for i in 0 .. PLY_MAX - 1 { expect(ply_join(session_st, 10 + i) > 0) }
expect_eq(ply_join(session_st, 99), -1)
expect_eq(ply_count(session_st), PLY_MAX)
}
test "the nearest player, whoever it is" (base_st: mut BaseState, session_st: mut SessionState, session_test_st: mut SessionTestState) {
test "the nearest player, whoever it is" (session_st: mut SessionState, session_test_st: mut SessionTestState) {
ply_init(session_st)
session_test_st.me_x = 100.0
let p = ply_join(base_st, session_st, 2)
let p = ply_join(session_st, 2)
ply_report(session_st, p, 3.0, 0.0, 4.0, 0.0, 1.0)
expect_near(ply_near(session_st, 0.0, 0.0), 5.0, 0.001)
expect_eq(ply_near_i(session_st), p)
expect_near(ply_dist(session_st, 0, 100.0, 0.0), 0.0, 0.001)
}
test "roles: alone owns the world, a guest does not, and a change is a fact" (base_st: mut BaseState, session_st: mut SessionState) {
test "roles: alone owns the world, a guest does not, and a change is a fact" (session_st: mut SessionState) {
ply_init(session_st)
expect(ses_owns_world(session_st))
expect(not ses_live(session_st))
expect(ses_night_passes(session_st))
ses_set_role(base_st, session_st, SES_GUEST)
ses_set_role(session_st, SES_GUEST)
expect(not ses_owns_world(session_st))
expect(ses_live(session_st))
expect(not ses_night_passes(session_st))
let f = last_of(base_st, session_st, SES_ROLE)
let f = last_of(session_st, SES_ROLE)
expect_eq(f.role, SES_GUEST)
ses_set_role(base_st, session_st, SES_HOST)
ses_set_role(session_st, SES_HOST)
expect(ses_owns_world(session_st))
expect(not ses_night_passes(session_st))
}
test "the party's size and door" (base_st: mut BaseState, session_st: mut SessionState) {
test "the party's size and door" (session_st: mut SessionState) {
ses_set_party(session_st, 20, true)
expect_eq(ses_max(session_st), PLY_MAX)
ses_set_party(session_st, 3, true)
party(base_st, session_st, 1)
party(session_st, 1)
expect(ses_room_for_one(session_st))
ply_join(base_st, session_st, 9)
ply_join(session_st, 9)
expect(not ses_room_for_one(session_st))
ses_set_party(session_st, 12, false)
expect(not ses_room_for_one(session_st))
}
test "the night passes when the host and every guest have turned in" (base_st: mut BaseState, session_st: mut SessionState) {
party(base_st, session_st, 2)
expect(ses_sleep(base_st, session_st, true))
test "the night passes when the host and every guest have turned in" (session_st: mut SessionState) {
party(session_st, 2)
expect(ses_sleep(session_st, true))
expect_eq(ses_sleep_waiting(session_st), 2)
ses_sleep_ready(base_st, session_st, 1, true)
expect(last_of(base_st, session_st, SES_NIGHT) == null)
ses_sleep_ready(base_st, session_st, 2, true)
expect(last_of(base_st, session_st, SES_NIGHT) != null)
ses_sleep_ready(session_st, 1, true)
expect(last_of(session_st, SES_NIGHT) == null)
ses_sleep_ready(session_st, 2, true)
expect(last_of(session_st, SES_NIGHT) != null)
ses_night_passed(session_st)
expect(not ses_sleeping(session_st))
}
test "a guest's turning in is a fact for the host to hear; alone a bed just sleeps" (base_st: mut BaseState, session_st: mut SessionState) {
test "a guest's turning in is a fact for the host to hear; alone a bed just sleeps" (session_st: mut SessionState) {
ply_init(session_st)
expect(not ses_sleep(base_st, session_st, true))
ses_set_role(base_st, session_st, SES_GUEST)
q_clear(base_st, ses_facts(base_st, session_st))
expect(ses_sleep(base_st, session_st, true))
let f = last_of(base_st, session_st, SES_SLEEP)
expect(not ses_sleep(session_st, true))
ses_set_role(session_st, SES_GUEST)
q_clear(ses_facts(session_st))
expect(ses_sleep(session_st, true))
let f = last_of(session_st, SES_SLEEP)
expect(f.yes)
ses_sleep(base_st, session_st, false)
ses_sleep(session_st, false)
expect(not ses_sleeping(session_st))
}
test "a vote passes on a majority, the host's yes counted" (base_st: mut BaseState, session_st: mut SessionState) {
party(base_st, session_st, 2)
expect_eq(ses_may(base_st, session_st, 4, 9, "set out"), SES_MAY_ASKED)
test "a vote passes on a majority, the host's yes counted" (session_st: mut SessionState) {
party(session_st, 2)
expect_eq(ses_may(session_st, 4, 9, "set out"), SES_MAY_ASKED)
expect(ses_vote_live(session_st))
expect_eq(ses_may(base_st, session_st, 4, 9, "set out"), SES_MAY_BUSY)
ses_vote_heard(base_st, session_st, ses_vote_id(session_st), true)
let f = last_of(base_st, session_st, SES_VOTE_RESOLVED)
expect_eq(ses_may(session_st, 4, 9, "set out"), SES_MAY_BUSY)
ses_vote_heard(session_st, ses_vote_id(session_st), true)
let f = last_of(session_st, SES_VOTE_RESOLVED)
expect(f.yes)
expect_eq(f.param, 9)
expect(f.text == "set out")
expect(not ses_vote_live(session_st))
}
test "a vote fails on half saying no, or when the time runs out" (base_st: mut BaseState, session_st: mut SessionState) {
party(base_st, session_st, 1)
ses_vote_open(base_st, session_st, 1, 0, "a")
ses_vote_heard(base_st, session_st, ses_vote_id(session_st), false)
expect(not last_of(base_st, session_st, SES_VOTE_RESOLVED).yes)
party(base_st, session_st, 3)
ses_vote_open(base_st, session_st, 1, 0, "b")
ses_vote_tick(base_st, session_st, 31.0)
let f = last_of(base_st, session_st, SES_VOTE_RESOLVED)
test "a vote fails on half saying no, or when the time runs out" (session_st: mut SessionState) {
party(session_st, 1)
ses_vote_open(session_st, 1, 0, "a")
ses_vote_heard(session_st, ses_vote_id(session_st), false)
expect(not last_of(session_st, SES_VOTE_RESOLVED).yes)
party(session_st, 3)
ses_vote_open(session_st, 1, 0, "b")
ses_vote_tick(session_st, 31.0)
let f = last_of(session_st, SES_VOTE_RESOLVED)
expect(f != null)
expect(not f.yes)
}
test "alone it may go; a guest may not, and its vote goes to the host" (base_st: mut BaseState, session_st: mut SessionState) {
test "alone it may go; a guest may not, and its vote goes to the host" (session_st: mut SessionState) {
ply_init(session_st)
expect_eq(ses_may(base_st, session_st, 1, 0, "x"), SES_MAY_GO)
ses_set_role(base_st, session_st, SES_GUEST)
expect_eq(ses_may(base_st, session_st, 1, 0, "x"), SES_MAY_HOSTS)
expect_eq(ses_may(session_st, 1, 0, "x"), SES_MAY_GO)
ses_set_role(session_st, SES_GUEST)
expect_eq(ses_may(session_st, 1, 0, "x"), SES_MAY_HOSTS)
ses_vote_told(session_st, 5, 1, "x")
expect(ses_vote_live(session_st))
q_clear(base_st, ses_facts(base_st, session_st))
ses_vote_cast(base_st, session_st, true)
let f = last_of(base_st, session_st, SES_VOTE_CAST)
q_clear(ses_facts(session_st))
ses_vote_cast(session_st, true)
let f = last_of(session_st, SES_VOTE_CAST)
expect_eq(f.id, 5)
expect(f.yes)
ses_vote_ended(session_st, 5, true)
expect_eq(ses_vote_passed(session_st), 1)
}
test "a reset leaves everyone and stands alone" (base_st: mut BaseState, session_st: mut SessionState) {
party(base_st, session_st, 2)
ses_sleep(base_st, session_st, true)
ses_reset(base_st, session_st)
test "a reset leaves everyone and stands alone" (session_st: mut SessionState) {
party(session_st, 2)
ses_sleep(session_st, true)
ses_reset(session_st)
expect_eq(ply_count(session_st), 1)
expect_eq(ses_role(session_st), SES_SOLO)
expect(not ses_sleeping(session_st))

View file

@ -10,16 +10,16 @@ const SESSION_VOTE_SECS: float = 30.0
# a world-changing act: may it go ahead now, or does the party have to be asked first?
export function ses_may(base_st: mut BaseState, session_st: mut SessionState, kind: int, param: int, text: string) -> int {
export function ses_may(session_st: mut SessionState, kind: int, param: int, text: string) -> int {
if not ses_live(session_st) { return SES_MAY_GO }
if session_st.session__role == SES_GUEST { return SES_MAY_HOSTS }
if ply_count(session_st) <= 1 { return SES_MAY_GO }
if not ses_vote_open(base_st, session_st, kind, param, text) { return SES_MAY_BUSY }
if not ses_vote_open(session_st, kind, param, text) { return SES_MAY_BUSY }
return SES_MAY_ASKED
}
# the host puts it to the party, voting yes itself
export function ses_vote_open(base_st: mut BaseState, session_st: mut SessionState, kind: int, param: int, text: string) -> bool {
export function ses_vote_open(session_st: mut SessionState, kind: int, param: int, text: string) -> bool {
if session_st.session__role != SES_HOST or ses_vote_live(session_st) { return false }
session_st.session__vote_id += 1
session_st.session__vote_kind = kind
@ -30,29 +30,29 @@ export function ses_vote_open(base_st: mut BaseState, session_st: mut SessionSta
session_st.session__vote_no = 0
session_st.session__vote_mine = 1
session_st.session__vote_passed = -1
let f = session__vote_fact(base_st, session_st, SES_VOTE_OPENED)
let f = session__vote_fact(session_st, SES_VOTE_OPENED)
f.yes = true
session__vote_check(base_st, session_st)
session__vote_check(session_st)
return true
}
# this player's answer; a guest's goes to the host as SES_VOTE_CAST
export function ses_vote_cast(base_st: mut BaseState, session_st: mut SessionState, yes: bool) -> void {
export function ses_vote_cast(session_st: mut SessionState, yes: bool) -> void {
if not ses_vote_live(session_st) or session_st.session__vote_mine >= 0 { return }
if yes { session_st.session__vote_mine = 1 } else { session_st.session__vote_mine = 0 }
if session_st.session__role == SES_GUEST {
let f = session__vote_fact(base_st, session_st, SES_VOTE_CAST)
let f = session__vote_fact(session_st, SES_VOTE_CAST)
f.yes = yes
return
}
ses_vote_heard(base_st, session_st, session_st.session__vote_id, yes)
ses_vote_heard(session_st, session_st.session__vote_id, yes)
}
# the host: a player's answer to vote `id`
export function ses_vote_heard(base_st: mut BaseState, session_st: mut SessionState, id: int, yes: bool) -> void {
export function ses_vote_heard(session_st: mut SessionState, id: int, yes: bool) -> void {
if session_st.session__role != SES_HOST or id != session_st.session__vote_id or not ses_vote_live(session_st) { return }
if yes { session_st.session__vote_yes += 1 } else { session_st.session__vote_no += 1 }
session__vote_check(base_st, session_st)
session__vote_check(session_st)
}
# a guest: the host opened vote `id`, or ended it
@ -71,29 +71,29 @@ export function ses_vote_ended(session_st: mut SessionState, id: int, passed: bo
}
# the vote's thirty seconds; on the host, running out decides it
export function ses_vote_tick(base_st: mut BaseState, session_st: mut SessionState, dt: float) -> void {
export function ses_vote_tick(session_st: mut SessionState, dt: float) -> void {
if not ses_vote_live(session_st) { return }
session_st.session__vote_t = session_st.session__vote_t - dt
if session_st.session__role == SES_HOST and not ses_vote_live(session_st) { session__vote_finish(base_st, session_st) }
if session_st.session__role == SES_HOST and not ses_vote_live(session_st) { session__vote_finish(session_st) }
}
function session__vote_check(base_st: mut BaseState, session_st: mut SessionState) -> void {
function session__vote_check(session_st: mut SessionState) -> void {
let n = ply_count(session_st)
let yes = session_st.session__vote_yes
let no = session_st.session__vote_no
if yes * 2 > n or no * 2 >= n or yes + no >= n { session__vote_finish(base_st, session_st) }
if yes * 2 > n or no * 2 >= n or yes + no >= n { session__vote_finish(session_st) }
}
function session__vote_finish(base_st: mut BaseState, session_st: mut SessionState) -> void {
function session__vote_finish(session_st: mut SessionState) -> void {
let passed = session_st.session__vote_yes * 2 > ply_count(session_st)
session_st.session__vote_t = 0.0
if passed { session_st.session__vote_passed = 1 } else { session_st.session__vote_passed = 0 }
let f = session__vote_fact(base_st, session_st, SES_VOTE_RESOLVED)
let f = session__vote_fact(session_st, SES_VOTE_RESOLVED)
f.yes = passed
}
function session__vote_fact(base_st: mut BaseState, session_st: mut SessionState, what: int) -> SessionFact {
let f = session__fact(base_st, session_st, what, -1, 0)
function session__vote_fact(session_st: mut SessionState, what: int) -> SessionFact {
let f = session__fact(session_st, what, -1, 0)
f.id = session_st.session__vote_id
f.kind = session_st.session__vote_kind
f.param = session_st.session__vote_param