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>
77 lines
2.8 KiB
Text
77 lines
2.8 KiB
Text
# dispatch.ludic - asking a Thing its kind's questions: what it is called, what using it would do,
|
|
# doing it, how close it is used from, and a tick for the kinds that have one
|
|
export function thing_kind(t: Thing) -> ThingKind { return ThingKinds[t.kind] }
|
|
|
|
# its own name, else its kind's, else ""
|
|
export function thing_name(t: Thing) -> string {
|
|
if t == null or not thing_kind_ok(t.kind) { return "" }
|
|
let k = ThingKinds[t.kind]
|
|
if k.name_of != null {
|
|
let s = k.name_of(t)
|
|
if s != "" { return s }
|
|
}
|
|
return k.name
|
|
}
|
|
|
|
export function thing_prompt(t: Thing) -> string {
|
|
if t == null or not thing_kind_ok(t.kind) { return "" }
|
|
let k = ThingKinds[t.kind]
|
|
if k.prompt == null { return "" }
|
|
return k.prompt(t)
|
|
}
|
|
|
|
# the kind does what using it does; the use is a fact whatever the kind made of it
|
|
export function thing_use(things_st: ThingsState, t: Thing) -> void {
|
|
if t == null or not thing_kind_ok(t.kind) { return }
|
|
let k = ThingKinds[t.kind]
|
|
th_fact(things_st, THING_USED, t)
|
|
if k.use != null { k.use(t) }
|
|
}
|
|
|
|
export function thing_can_use(t: Thing) -> bool {
|
|
return t != null and thing_kind_ok(t.kind) and ThingKinds[t.kind].use != null
|
|
}
|
|
|
|
# the kind's own reach question, else the Thing's reach, else the kind's
|
|
export function thing_reach(t: Thing) -> float {
|
|
if t == null or not thing_kind_ok(t.kind) { return 0.0 }
|
|
let k = ThingKinds[t.kind]
|
|
if k.reach_of != null { return k.reach_of(t) }
|
|
if t.reach > 0.0 { return t.reach }
|
|
return k.reach
|
|
}
|
|
|
|
export function thing_is_personal(t: Thing) -> bool { return thing_kind_ok(t.kind) and ThingKinds[t.kind].personal }
|
|
# set down by someone rather than grown by the world: the kind says, and the port has the last word
|
|
export function thing_is_put_down(t: Thing) -> bool { return ThingsWorld.put_down(t) }
|
|
|
|
# every active Thing whose kind ticks, `hours` of the world's clock on: only the kinds that tick
|
|
# are visited, through the kind index, from a list of handles taken first - a tick may hide one
|
|
export function things_tick(things_st: mut ThingsState, hours: float) -> void {
|
|
let hs = things_st.th_hs
|
|
List.clear(hs)
|
|
let tb = things_st.th.tab
|
|
for k in 0 .. TH_COUNT {
|
|
if ThingKinds[k].tick == null { continue }
|
|
let rows = ix_rows(things_st.th.kinds, k)
|
|
for i in 0 .. len(rows) { push(hs, tb.ent[rows[i]]) }
|
|
}
|
|
for i in 0 .. len(hs) {
|
|
let r = tb_row(tb, hs[i])
|
|
if r < 0 { continue }
|
|
let t = tb.rec[r]
|
|
if t.active { ThingKinds[t.kind].tick(t, hours) }
|
|
}
|
|
}
|
|
|
|
# a restock: every Thing the world grew is back, and loses its `used` unless its kind keeps it;
|
|
# what was put down is left as it is
|
|
export function things_restock(things_st: ThingsState) -> void {
|
|
let all = things_all(things_st)
|
|
for i in 0 .. len(all) {
|
|
let t = all[i]
|
|
if thing_is_put_down(t) { continue }
|
|
if not ThingKinds[t.kind].keeps { t.used = 0 }
|
|
if not t.active { thing_show(t) }
|
|
}
|
|
}
|