ludic/packages/ludic.vehicles/table.ludic
Orkuncakilkaya 045bf22e10 feat(ecs): ludic.vehicles on a Table<Vehicle>
The vehicles are rows of a ludic.base Table with kind and owner as columns, every player's own by
an owner index and a nid map: vehicle_of walks only that player's rows, vehicle_by_nid is a map
lookup, and a player who leaves has their rows removed instead of the whole list rebuilt. ve_add
takes the owner, so the owner is filed once and never assigned around the table. Positions stay
on the record: a boat moves every frame and there are at most two a player. vehicles_test holds
the table (9 tests); the game builds against it and lab/unit passes (88 tests).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-27 21:50:23 +03:00

45 lines
1.4 KiB
Text

# table.ludic - the vehicles, rows of a ludic.base Table: kind and owner as columns, each player's
# own by index, and a nid to its handle. Where one is stays on its record: a boat moves every frame
# and there are never more than two a player to search.
export const VE_KIND: int = 0 # int columns
export const VE_OWNER: int = 1
export property VehicleTable {
tab: Table<Vehicle> = null
owners: IntIndex = null # every vehicle by the player it belongs to
nids: IntMap = null
}
function ve__new() -> VehicleTable {
let w = new VehicleTable
let tb: Table<Vehicle> = table_new(0, 2)
w.tab = tb
w.owners = tb_index(tb, VE_OWNER, -1)
w.nids = imap_new()
return w
}
# a new row for v, its kind and owner from its record
function ve_file(w: VehicleTable, v: Vehicle) -> void {
v.tab = w.tab
v.ent = tb_add(w.tab, v)
let r = tb_row(w.tab, v.ent)
w.tab.i[VE_KIND][r] = v.kind
w.tab.i[VE_OWNER][r] = v.owner
tb_refile(w.tab, r)
imap_put(w.nids, v.nid, v.ent)
}
function ve_unfile(w: VehicleTable, v: Vehicle) -> void {
imap_del(w.nids, v.nid)
tb_remove(w.tab, v.ent)
v.ent = -1
}
# the vehicle a message names, or null
export function vehicle_by_nid(vehicles_st: VehiclesState, nid: int) -> Vehicle {
let tb = vehicles_st.ve.tab
let r = tb_row(tb, imap_get(vehicles_st.ve.nids, nid, -1))
if r < 0 or tb.rec[r].nid != nid { return null }
return tb.rec[r]
}