ludic/packages/ludic.vehicles/state.ludic
Orkuncakilkaya fffedf71d0 ludic.physics, ludic.character, ludic.vehicles: fact records reused, and phys_sink allocates nothing
The per-frame sweep over physics, character, vehicles and nav found three fact makers that made a record per fact: contacts and splashes, the character's and the vehicles'. Each keeps a pool now, as wildlife's and npc's do (q_unheld). phys_sink, asked on every removal, built two new lists each time; it filters into a kept spare pair and swaps. ludic.nav's paths and crowds already allocate nothing, and none of the four builds a string per call. What remains at load, reset or a world swap is not per frame. vehicle_feed takes its state mut. vehicles_test: 1000 fed facts, drained turn by turn, use at most 4 records. All packages 421.

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

78 lines
2.9 KiB
Text

# state.ludic - every vehicle, the one this machine's player is on, where the places are measured
# from, and the facts
const ve_pi: float = 3.14159265
export function vehicle_facts(vehicles_st: VehiclesState) -> Queue<VehicleFact> {
return vehicles_st.ve_fact_q
}
# a fact record no drain holds any more, made fresh (and kept) only when every one is held: a
# fact can come every frame, and a record a frame is never given back
function ve_fact_record(vehicles_st: mut VehiclesState) -> VehicleFact {
if vehicles_st.ve_fnext >= len(vehicles_st.ve_ffree) {
q_unheld(vehicle_facts(vehicles_st), vehicles_st.ve_fpool, vehicles_st.ve_ffree)
vehicles_st.ve_fnext = 0
}
if vehicles_st.ve_fnext < len(vehicles_st.ve_ffree) {
let f = vehicles_st.ve_ffree[vehicles_st.ve_fnext]
vehicles_st.ve_fnext += 1
return f
}
let f = new VehicleFact
push(vehicles_st.ve_fpool, f)
return f
}
# how many fact records have ever been made: the peak held at once, not the facts pushed
export function vehicle_fact_records(vehicles_st: VehiclesState) -> int { return len(vehicles_st.ve_fpool) }
function ve_say(vehicles_st: mut VehiclesState, what: int, v: Vehicle, n: float) -> void {
let f = ve_fact_record(vehicles_st)
f.what = what
f.nid = v.nid
f.kind = v.kind
f.owner = v.owner
f.x = v.x
f.z = v.z
f.v = n
q_push(vehicle_facts(vehicles_st), f)
}
function ve_all(vehicles_st: VehiclesState) -> []Vehicle { return vehicles_st.ve.tab.rec }
# every vehicle in the valley, to draw and to aim at (change one only through the verbs)
export function vehicles_all(vehicles_st: VehiclesState) -> []Vehicle { return ve_all(vehicles_st) }
# the kind this machine's player is riding, 0 none
export function vehicle_ride_kind(vehicles_st: VehiclesState) -> int {
if vehicles_st.ve_cur == null { return 0 }
return vehicles_st.ve_cur.kind
}
export function vehicle_current(vehicles_st: VehiclesState) -> Vehicle { return vehicles_st.ve_cur }
export function vehicle_of(vehicles_st: VehiclesState, pid: int, kind: int) -> Vehicle {
let rows = ix_rows(vehicles_st.ve.owners, pid)
let l = ve_all(vehicles_st)
for k in 0 .. len(rows) { if l[rows[k]].kind == kind { return l[rows[k]] } }
return null
}
# this player's own boat or horse
export function vehicle_find(vehicles_st: VehiclesState, kind: int) -> Vehicle { return vehicle_of(vehicles_st, 0, kind) }
# the nearest vehicle nobody is on within `reach` of (x, z); nothing while riding
export function vehicle_nearest(vehicles_st: VehiclesState, x: float, z: float, reach: float) -> Vehicle {
if vehicles_st.ve_cur != null { return null }
var best: Vehicle = null
var bd = reach
let l = ve_all(vehicles_st)
for i in 0 .. len(l) {
let v = l[i]
if v.rider >= 0 { continue }
let d = Math.sqrt((v.x - x) * (v.x - x) + (v.z - z) * (v.z - z))
if d < bd {
bd = d
best = v
}
}
return best
}