feat(ecs): ludic.base Table<T> - dense rows of hot columns, generational handles, a spatial grid, kind indexes and an id map kept current by the setters; ludic.things on it
A mechanic that keeps many of something keeps them as rows of a Table<T> rather than a list it scans. Removal swaps the last row in; a handle (22-bit slot, 9-bit generation) goes stale when its entity is removed. tb_set_f / tb_set_xz / tb_set_i stamp a change tick and refile the row in every index over that column in O(1): tb_grid (a doubly linked spatial hash, rings outward for nearest, rehashing as it grows), tb_index (a cached query: the rows of each value of a kind column, gated by an active column), tb_nearest_of / tb_within_of (a rare kind from its own list), tb_nearest_where (a predicate on the record), tb_within_recs (into the caller's list), tb_changed_since / tb_added_since, IntMap. No question allocates or writes: Ludic frees nothing, and the old lists' per-call copies leaked every frame. ludic.things keeps its Things as a Table<Thing> with x, z, kind and active as columns; every verb writes the record and the row together (a Thing carries its handle and table, so thing_hide(t) still needs no state), thing_set_on / thing_set_xz / thing_place_at are the silent forms the game used to do by assignment, and things_verify holds the columns against the records. things_near(_of) fill a caller's list, thing_of_kind walks a kind, things_count_of counts one, and things_tick visits only the kinds that tick. thing_find answers the first-placed by uid. Against a []Record scanned (M4 Pro): nearest 0.37 / 1.5 / 7.2 us at 10k / 100k / 1M (list 30 / 307 / 3075), by id 0.09 us at 100k (list 17), a move refiled in 11-44 ns. ecs_fuzz_test holds the grid, the kind index and the record queries against a scan through 9000 random changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
e3813b1ea4
commit
f802bdd3ec
23 changed files with 1367 additions and 125 deletions
115
packages/ludic.things/verbs.ludic
Normal file
115
packages/ludic.things/verbs.ludic
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
# verbs.ludic - the only ways a Thing is made, removed, moved, shown or hidden: each writes the
|
||||
# record and the table's columns together, so the grid and the kind indexes never fall behind
|
||||
export function thing_spawn(things_st: mut ThingsState, kind: int, look: string, x: float, y: float, z: float, yaw: float, id: int) -> Thing {
|
||||
return th_place(things_st, new Thing, kind, look, x, y, z, yaw, id)
|
||||
}
|
||||
|
||||
function th_place(things_st: mut ThingsState, t: Thing, kind: int, look: string, x: float, y: float, z: float, yaw: float, id: int) -> Thing {
|
||||
t.kind = kind
|
||||
t.look = look
|
||||
t.x = x
|
||||
t.y = y
|
||||
t.z = z
|
||||
t.yaw = yaw
|
||||
t.id = id
|
||||
things_st.th_uid += 1
|
||||
t.uid = things_st.th_uid
|
||||
let tb = things_st.th.tab
|
||||
t.tab = tb
|
||||
t.ent = tb_add(tb, t)
|
||||
th_columns(t)
|
||||
imap_put(things_st.th_uids, t.uid, t.ent)
|
||||
ThingsWorld.placed(t)
|
||||
th_fact(things_st, THING_PLACED, t)
|
||||
return t
|
||||
}
|
||||
|
||||
# the record's x, z, kind and active into its row, refiled once
|
||||
function th_columns(t: Thing) -> void {
|
||||
let r = th_row(t)
|
||||
if r < 0 { return }
|
||||
let tb = t.tab
|
||||
tb.f[TH_X][r] = t.x
|
||||
tb.f[TH_Z][r] = t.z
|
||||
tb.i[TH_KIND][r] = t.kind
|
||||
var on = 0
|
||||
if t.active { on = 1 }
|
||||
tb.i[TH_ON][r] = on
|
||||
tb_refile(tb, r)
|
||||
}
|
||||
|
||||
# a Thing on the ground (the port's), set into it by `sink`, used from `reach`
|
||||
export function thing_put(things_st: mut ThingsState, kind: int, look: string, x: float, z: float, yaw: float, sink: float, reach: float, id: int) -> Thing {
|
||||
let t = new Thing
|
||||
t.reach = reach
|
||||
return th_place(things_st, t, kind, look, x, ThingsWorld.ground(x, z) - sink, z, yaw, id)
|
||||
}
|
||||
|
||||
# gone for good
|
||||
export function thing_remove(things_st: mut ThingsState, t: Thing) -> void {
|
||||
if th_row(t) < 0 { return }
|
||||
ThingsWorld.removed(t)
|
||||
th_fact(things_st, THING_REMOVED, t)
|
||||
imap_del(things_st.th_uids, t.uid)
|
||||
tb_remove(t.tab, t.ent)
|
||||
t.ent = -1
|
||||
}
|
||||
|
||||
# every Thing `gone` says so is removed for good; from the last row down, so the row a removal
|
||||
# moves into place has already been asked
|
||||
export function things_drop(things_st: mut ThingsState, gone: fn(Thing) -> bool) -> void {
|
||||
let tb = things_st.th.tab
|
||||
var r = tb_len(tb) - 1
|
||||
while r >= 0 {
|
||||
let t = tb.rec[r]
|
||||
if gone(t) { thing_remove(things_st, t) }
|
||||
r -= 1
|
||||
}
|
||||
}
|
||||
|
||||
export function thing_move(t: Thing, x: float, z: float, yaw: float) -> void {
|
||||
thing_move_to(t, x, ThingsWorld.ground(x, z), z, yaw)
|
||||
}
|
||||
|
||||
export function thing_move_to(t: Thing, x: float, y: float, z: float, yaw: float) -> void {
|
||||
thing_place_at(t, x, y, z, yaw)
|
||||
ThingsWorld.moved(t)
|
||||
}
|
||||
|
||||
# moved without telling the port: a stand-in the game draws itself, or nothing draws
|
||||
export function thing_place_at(t: Thing, x: float, y: float, z: float, yaw: float) -> void {
|
||||
t.x = x
|
||||
t.y = y
|
||||
t.z = z
|
||||
t.yaw = yaw
|
||||
let r = th_row(t)
|
||||
if r >= 0 { tb_set_xz(t.tab, TH_X, TH_Z, r, x, z) }
|
||||
}
|
||||
|
||||
export function thing_set_active(t: Thing, on: bool) -> void {
|
||||
if t == null { return }
|
||||
thing_set_on(t, on)
|
||||
ThingsWorld.shown(t)
|
||||
}
|
||||
export function thing_hide(t: Thing) -> void { thing_set_active(t, false) }
|
||||
export function thing_show(t: Thing) -> void { thing_set_active(t, true) }
|
||||
|
||||
# active or not without telling the port
|
||||
export function thing_set_on(t: Thing, on: bool) -> void {
|
||||
if t == null { return }
|
||||
t.active = on
|
||||
let r = th_row(t)
|
||||
if r < 0 { return }
|
||||
var v = 0
|
||||
if on { v = 1 }
|
||||
tb_set_i(t.tab, TH_ON, r, v)
|
||||
}
|
||||
|
||||
# where it stands on the ground plane, without telling the port (its height is the caller's)
|
||||
export function thing_set_xz(t: Thing, x: float, z: float) -> void {
|
||||
if t == null { return }
|
||||
t.x = x
|
||||
t.z = z
|
||||
let r = th_row(t)
|
||||
if r >= 0 { tb_set_xz(t.tab, TH_X, TH_Z, r, x, z) }
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue