ludic/packages/ludic.things/store.ludic
Orkuncakilkaya 1af62d0879 ludic.things: a removed Thing's record placed again, oldest first past a lag of 32; things_reserve sizes the grid once
thing_put and thing_spawn made a new record for every Thing placed, and the world places and removes
them all day (an animal's sign and bed, a drop, a fish), so play grew by a record per placement.
Removed records wait in a queue compacted in place; once 32 wait, the oldest is set back as new
with a new uid. ludic.base's grid_reserve makes the buckets for n rows now, since a rehash in play
leaves the old bucket array behind. Tests: a record comes back only past the lag, as new, with a
new uid, the indexes agree, and the spare queue stays bounded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 11:52:23 +03:00

94 lines
3.6 KiB
Text

# store.ludic - the Things, rows of a ludic.base Table: where each stands and whether it is active
# are columns a grid and two kind indexes are kept over, and only the verbs (verbs.ludic) make,
# move, show, hide and remove one; each placing and removing is a fact the port hears at once
export const TH_X: int = 0 # float columns
export const TH_Z: int = 1
export const TH_KIND: int = 0 # int columns
export const TH_ON: int = 1 # 1 while active
export const TH_CHUNK: int = 2 # the chunk of the world it stands in (chunk_of, TH_CHUNK_M)
export const TH_CHUNK_M: float = 256.0
const TH_CELL: float = 16.0
# the table and the indexes kept over it, made together
export property ThingsTable {
tab: Table<Thing> = null
grid: Grid = null
kinds: IntIndex = null # active Things by kind
every: IntIndex = null # every Thing by kind, active or not
chunks: IntIndex = null # every Thing by the chunk it stands in: loaded and let go together
}
export state ThingsState {
th: ThingsTable = th__new()
th_uids: IntMap = imap_new()
th_uid: int = 0
th_facts: Queue<ThingFact> = th_facts__new()
th_hs: words = words(0) # things_tick's handles, taken before any tick runs
th_spare: []Thing = new []Thing # removed records, made again oldest first (spare.ludic)
th_spare_at: int = 0
}
function th_facts__new() -> Queue<ThingFact> { return queue_new("things.facts") }
function th__new() -> ThingsTable {
let th = new ThingsTable
let tb: Table<Thing> = table_new(2, 3)
th.tab = tb
th.grid = tb_grid(tb, TH_X, TH_Z, TH_ON, TH_CELL)
th.kinds = tb_index(tb, TH_KIND, TH_ON)
th.every = tb_index(tb, TH_KIND, -1)
th.chunks = tb_index(tb, TH_CHUNK, -1)
return th
}
export function things_facts(things_st: ThingsState) -> Queue<ThingFact> {
return things_st.th_facts
}
function th_fact(things_st: ThingsState, what: int, t: Thing) -> void {
let f = new ThingFact
f.what = what
f.uid = t.uid
f.kind = t.kind
f.id = t.id
f.owner = t.owner
f.x = t.x
f.z = t.z
q_push(things_facts(things_st), f)
}
# whether a world has been set out (things_clear) since the program began
export function things_ready(things_st: ThingsState) -> bool { return things_st.th.tab != null }
# every Thing, one per row - the table's own list: read it, never push to it. A removal moves the
# last Thing into the removed one's place, so the order is the table's, not the order placed.
export function things_all(things_st: ThingsState) -> []Thing { return things_st.th.tab.rec }
export function things_count(things_st: ThingsState) -> int { return tb_len(things_st.th.tab) }
# a new world: every Thing forgotten, quietly (the game has already dropped its drawings)
export function things_clear(things_st: mut ThingsState) -> void {
let tb = things_st.th.tab
for r in 0 .. tb_len(tb) { tb.rec[r].ent = -1 }
tb_clear(tb)
imap_clear(things_st.th_uids)
}
# the row a Thing stands in, or -1 when it is not in the table any more
function th_row(t: Thing) -> int {
if t == null or t.tab == null { return -1 }
return tb_row(t.tab, t.ent)
}
# the columns say what the record says: a Thing's fields written around the verbs are caught here
export function things_verify(things_st: ThingsState) -> int {
let tb = things_st.th.tab
var bad = 0
for r in 0 .. tb_len(tb) {
let t = tb.rec[r]
var on = 0
if t.active { on = 1 }
if tb.f[TH_X][r] != t.x or tb.f[TH_Z][r] != t.z or tb.i[TH_KIND][r] != t.kind or tb.i[TH_ON][r] != on or tb.i[TH_CHUNK][r] != chunk_of(t.x, t.z, TH_CHUNK_M) or t.ent != tb.ent[r] { bad += 1 }
}
return bad
}