ludic/packages/ludic.things/kinds.ludic
Orkuncakilkaya f802bdd3ec 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>
2026-09-27 15:52:51 +03:00

65 lines
2.9 KiB
Text

# kinds.ludic - a Thing, and what a kind of Thing is: plain facts about the kind, and its
# behaviour as function values the dispatcher calls (a null one is a question the kind ignores)
export property Thing {
uid: int = 0 # unique for the run, never reused
kind: int = 0 # its place in ThingKinds
id: int = 0 # what the kind says it means (which sign, which item, which site)
look: string = "" # how the game draws it; "" is not drawn
x: float = 0.0
y: float = 0.0
z: float = 0.0
yaw: float = 0.0
owner: int = -1 # whoever put it down; -1 the world's own
active: bool = true # false: taken, hidden, used up for now
reach: float = 0.0 # how close it is used from; 0 is its kind's
timer: float = 0.0 # a clock the kind decides the meaning of
used: int = 0 # the kind's own state (split for today, which sign, ...)
n: int = 0 # a counter the kind decides the meaning of
sx: float = 0.0 # where using it puts the user, and which way that faces
sz: float = 0.0
syaw: float = 0.0
shared: bool = false # the game copies it to others (a party)
ent: int = -1 # its handle in the table, -1 once removed
tab: Table<Thing> = null # the table holding it, so a verb on the Thing alone keeps the indexes
}
export property ThingKind {
key: string = "" # the entry's own name: what the save calls the kind
name: string = "" # what it is called when nothing about the one in front of you changes it
reach: float = 2.2 # how close it is used from, when the Thing does not say
personal: bool = false # one belongs to whoever put it down
saved: bool = false # the save's section keeps a placed one (the port has the last word)
put_down: bool = false # set down by someone rather than grown by the world
keeps: bool = false # keeps its `used` through a restock
name_of: fn(Thing) -> string = null # "" falls back to `name`
prompt: fn(Thing) -> string = null # what using it would do, "" nothing
use: fn(Thing) -> void = null
tick: fn(Thing, float) -> void = null # hours of the world's clock
reach_of: fn(Thing) -> float = null # a reach that depends on the one in front of you
}
# the game fills it: `def ThingKinds tent { name: "The tent", use: fn tent_use }`
export open registry ThingKinds of ThingKind as TH
export const THING_PLACED: int = 0
export const THING_REMOVED: int = 1
export const THING_USED: int = 2
# something happened to a Thing: placed, removed for good, or used
export property ThingFact {
what: int = 0
uid: int = 0
kind: int = 0
id: int = 0
owner: int = -1
x: float = 0.0
z: float = 0.0
}
export function thing_kind_ok(k: int) -> bool { return k >= 0 and k < TH_COUNT }
# the kind the save calls `key`, or -1
export function thing_kind_by_key(key: string) -> int {
for k in 0 .. TH_COUNT { if ThingKinds[k].key == key { return k } }
return -1
}