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>
This commit is contained in:
parent
fe2d2bacd7
commit
fffedf71d0
10 changed files with 92 additions and 11 deletions
|
|
@ -104,6 +104,9 @@ export state CharacterState {
|
||||||
chr_safe_x: float = 0.0
|
chr_safe_x: float = 0.0
|
||||||
chr_safe_z: float = 0.0
|
chr_safe_z: float = 0.0
|
||||||
chr_fact_q: Queue<CharacterFact> = chr_fact_q__new()
|
chr_fact_q: Queue<CharacterFact> = chr_fact_q__new()
|
||||||
|
chr_fpool: []CharacterFact = new []CharacterFact # every fact record made, reused once no drain holds it
|
||||||
|
chr_ffree: []CharacterFact = new []CharacterFact
|
||||||
|
chr_fnext: int = 0
|
||||||
}
|
}
|
||||||
function chr__top_plain(x: float, z: float, r: float, from_y: float, step: float, base: float) -> float { return base }
|
function chr__top_plain(x: float, z: float, r: float, from_y: float, step: float, base: float) -> float { return base }
|
||||||
function chr__bed_plain(x: float, z: float) -> float { return CharacterGround.height(x, z) }
|
function chr__bed_plain(x: float, z: float) -> float { return CharacterGround.height(x, z) }
|
||||||
|
|
|
||||||
|
|
@ -13,8 +13,24 @@ export function char_facts(character_st: CharacterState) -> Queue<CharacterFact>
|
||||||
return character_st.chr_fact_q
|
return character_st.chr_fact_q
|
||||||
}
|
}
|
||||||
|
|
||||||
function chr_say(character_st: CharacterState, what: int, v: float) -> void {
|
# 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 chr_fact_record(character_st: mut CharacterState) -> CharacterFact {
|
||||||
|
if character_st.chr_fnext >= len(character_st.chr_ffree) {
|
||||||
|
q_unheld(char_facts(character_st), character_st.chr_fpool, character_st.chr_ffree)
|
||||||
|
character_st.chr_fnext = 0
|
||||||
|
}
|
||||||
|
if character_st.chr_fnext < len(character_st.chr_ffree) {
|
||||||
|
let f = character_st.chr_ffree[character_st.chr_fnext]
|
||||||
|
character_st.chr_fnext += 1
|
||||||
|
return f
|
||||||
|
}
|
||||||
let f = new CharacterFact
|
let f = new CharacterFact
|
||||||
|
push(character_st.chr_fpool, f)
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
function chr_say(character_st: mut CharacterState, what: int, v: float) -> void {
|
||||||
|
let f = chr_fact_record(character_st)
|
||||||
f.what = what
|
f.what = what
|
||||||
f.x = character_st.chr_x
|
f.x = character_st.chr_x
|
||||||
f.y = character_st.chr_y
|
f.y = character_st.chr_y
|
||||||
|
|
|
||||||
|
|
@ -32,8 +32,8 @@ export function phys_close(physics_st: mut PhysicsState) -> void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
physics_st.ph_shapes = new []pointer
|
physics_st.ph_shapes = new []pointer
|
||||||
physics_st.ph_floats = new []int
|
List.clear(physics_st.ph_floats)
|
||||||
physics_st.ph_float_k = new []float
|
List.clear(physics_st.ph_float_k)
|
||||||
if physics_st.ph_facts != null { q_clear(physics_st.ph_facts) }
|
if physics_st.ph_facts != null { q_clear(physics_st.ph_facts) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -46,12 +46,17 @@ export state PhysicsState {
|
||||||
ph_hard: float = 2.0 # metres a second: a contact under this is not a fact
|
ph_hard: float = 2.0 # metres a second: a contact under this is not a fact
|
||||||
ph_floats: []int = new []int # buoyant bodies' ids
|
ph_floats: []int = new []int # buoyant bodies' ids
|
||||||
ph_float_k: []float = new []float # and theirs: buoyancy, linear drag, angular drag, wet
|
ph_float_k: []float = new []float # and theirs: buoyancy, linear drag, angular drag, wet
|
||||||
|
ph_floats2: []int = new []int # the spare pair phys_sink filters into
|
||||||
|
ph_float_k2: []float = new []float
|
||||||
ph_rx: float = 0.0 # where the last phys_resolve put a body
|
ph_rx: float = 0.0 # where the last phys_resolve put a body
|
||||||
ph_rz: float = 0.0
|
ph_rz: float = 0.0
|
||||||
ph_walkers: []pointer = new []pointer # a walker id is its place here; null once removed
|
ph_walkers: []pointer = new []pointer # a walker id is its place here; null once removed
|
||||||
ph_ids: []int = words(PH_CAP * 2)
|
ph_ids: []int = words(PH_CAP * 2)
|
||||||
ph_vals: []float = floats(PH_CAP * 4)
|
ph_vals: []float = floats(PH_CAP * 4)
|
||||||
ph_facts: Queue<PhysFact> = ph_facts__new()
|
ph_facts: Queue<PhysFact> = ph_facts__new()
|
||||||
|
ph_fpool: []PhysFact = new []PhysFact # every fact record made, reused once no drain holds it
|
||||||
|
ph_ffree: []PhysFact = new []PhysFact
|
||||||
|
ph_fnext: int = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
# the facts, oldest first
|
# the facts, oldest first
|
||||||
|
|
|
||||||
|
|
@ -33,12 +33,28 @@ export function phys_steps(physics_st: PhysicsState) -> int { return physics_st.
|
||||||
# m/s2 down is negative
|
# m/s2 down is negative
|
||||||
export function phys_gravity(physics_st: mut PhysicsState, y: float) -> void { if physics_st.ph_world != null { jph_world_gravity(physics_st.ph_world, y) } }
|
export function phys_gravity(physics_st: mut PhysicsState, y: float) -> void { if physics_st.ph_world != null { jph_world_gravity(physics_st.ph_world, y) } }
|
||||||
|
|
||||||
|
# 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 ph_fact_record(physics_st: mut PhysicsState) -> PhysFact {
|
||||||
|
if physics_st.ph_fnext >= len(physics_st.ph_ffree) {
|
||||||
|
q_unheld(phys_facts(physics_st), physics_st.ph_fpool, physics_st.ph_ffree)
|
||||||
|
physics_st.ph_fnext = 0
|
||||||
|
}
|
||||||
|
if physics_st.ph_fnext < len(physics_st.ph_ffree) {
|
||||||
|
let f = physics_st.ph_ffree[physics_st.ph_fnext]
|
||||||
|
physics_st.ph_fnext += 1
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
let f = new PhysFact
|
||||||
|
push(physics_st.ph_fpool, f)
|
||||||
|
return f
|
||||||
|
}
|
||||||
function ph_contacts(physics_st: mut PhysicsState) -> void {
|
function ph_contacts(physics_st: mut PhysicsState) -> void {
|
||||||
let n = jph_contacts_drain(physics_st.ph_world, physics_st.ph_ids, physics_st.ph_vals, PH_CAP)
|
let n = jph_contacts_drain(physics_st.ph_world, physics_st.ph_ids, physics_st.ph_vals, PH_CAP)
|
||||||
for i in 0 .. n {
|
for i in 0 .. n {
|
||||||
let speed = physics_st.ph_vals[i * 4 + 3]
|
let speed = physics_st.ph_vals[i * 4 + 3]
|
||||||
if speed >= physics_st.ph_hard {
|
if speed >= physics_st.ph_hard {
|
||||||
let f = new PhysFact
|
let f = ph_fact_record(physics_st)
|
||||||
f.what = PHYS_HIT
|
f.what = PHYS_HIT
|
||||||
f.a = physics_st.ph_ids[i * 2]
|
f.a = physics_st.ph_ids[i * 2]
|
||||||
f.b = physics_st.ph_ids[i * 2 + 1]
|
f.b = physics_st.ph_ids[i * 2 + 1]
|
||||||
|
|
|
||||||
|
|
@ -13,17 +13,22 @@ export function phys_float(physics_st: mut PhysicsState, id: int, buoyancy: floa
|
||||||
push(physics_st.ph_float_k, 0.0)
|
push(physics_st.ph_float_k, 0.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
# no longer floats (it is still a body)
|
# no longer floats (it is still a body); every removal asks, so the lists are filtered into a kept
|
||||||
|
# spare pair and swapped, never made anew
|
||||||
export function phys_sink(physics_st: mut PhysicsState, id: int) -> void {
|
export function phys_sink(physics_st: mut PhysicsState, id: int) -> void {
|
||||||
if physics_st.ph_floats == null { return }
|
if physics_st.ph_floats == null { return }
|
||||||
let ids = new []int
|
let ids = physics_st.ph_floats2
|
||||||
let ks = new []float
|
let ks = physics_st.ph_float_k2
|
||||||
|
List.clear(ids)
|
||||||
|
List.clear(ks)
|
||||||
for i in 0 .. len(physics_st.ph_floats) {
|
for i in 0 .. len(physics_st.ph_floats) {
|
||||||
if physics_st.ph_floats[i] != id {
|
if physics_st.ph_floats[i] != id {
|
||||||
push(ids, physics_st.ph_floats[i])
|
push(ids, physics_st.ph_floats[i])
|
||||||
for k in 0 .. PH_K { push(ks, physics_st.ph_float_k[i * PH_K + k]) }
|
for k in 0 .. PH_K { push(ks, physics_st.ph_float_k[i * PH_K + k]) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
physics_st.ph_floats2 = physics_st.ph_floats
|
||||||
|
physics_st.ph_float_k2 = physics_st.ph_float_k
|
||||||
physics_st.ph_floats = ids
|
physics_st.ph_floats = ids
|
||||||
physics_st.ph_float_k = ks
|
physics_st.ph_float_k = ks
|
||||||
}
|
}
|
||||||
|
|
@ -56,10 +61,12 @@ function ph_buoy_one(physics_st: mut PhysicsState, i: int, id: int, x: float, z:
|
||||||
physics_st.ph_float_k[k + 3] = float(wet)
|
physics_st.ph_float_k[k + 3] = float(wet)
|
||||||
}
|
}
|
||||||
|
|
||||||
function ph_splash(physics_st: PhysicsState, id: int, x: float, y: float, z: float) -> void {
|
function ph_splash(physics_st: mut PhysicsState, id: int, x: float, y: float, z: float) -> void {
|
||||||
let f = new PhysFact
|
let f = ph_fact_record(physics_st)
|
||||||
f.what = PHYS_SPLASH
|
f.what = PHYS_SPLASH
|
||||||
f.a = id
|
f.a = id
|
||||||
|
f.b = -1
|
||||||
|
f.speed = 0.0
|
||||||
f.x = x; f.y = y; f.z = z
|
f.x = x; f.y = y; f.z = z
|
||||||
q_push(phys_facts(physics_st), f)
|
q_push(phys_facts(physics_st), f)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
export function vehicle_own(v: Vehicle) -> void { if v != null { v.owned = true } }
|
export function vehicle_own(v: Vehicle) -> void { if v != null { v.owned = true } }
|
||||||
|
|
||||||
# hay for a horse, up to a full belly
|
# hay for a horse, up to a full belly
|
||||||
export function vehicle_feed(vehicles_st: VehiclesState, v: Vehicle, amount: float) -> void {
|
export function vehicle_feed(vehicles_st: mut VehiclesState, v: Vehicle, amount: float) -> void {
|
||||||
if v == null { return }
|
if v == null { return }
|
||||||
v.food = Math.min(v.food + amount, 100.0)
|
v.food = Math.min(v.food + amount, 100.0)
|
||||||
ve_say(vehicles_st, VEHICLE_FED, v, amount)
|
ve_say(vehicles_st, VEHICLE_FED, v, amount)
|
||||||
|
|
|
||||||
|
|
@ -63,6 +63,9 @@ export state VehiclesState {
|
||||||
ve_ly: float = 0.0
|
ve_ly: float = 0.0
|
||||||
ve_lz: float = 0.0
|
ve_lz: float = 0.0
|
||||||
ve_fact_q: Queue<VehicleFact> = ve_fact_q__new()
|
ve_fact_q: Queue<VehicleFact> = ve_fact_q__new()
|
||||||
|
ve_fpool: []VehicleFact = new []VehicleFact # every fact record made, reused once no drain holds it
|
||||||
|
ve_ffree: []VehicleFact = new []VehicleFact
|
||||||
|
ve_fnext: int = 0
|
||||||
}
|
}
|
||||||
function ve__dry(x: float, z: float) -> float { return -10000.0 }
|
function ve__dry(x: float, z: float) -> float { return -10000.0 }
|
||||||
function ve__calm(x: float, z: float, fx: float, fz: float) -> float { return 0.0 }
|
function ve__calm(x: float, z: float, fx: float, fz: float) -> float { return 0.0 }
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,26 @@ export function vehicle_facts(vehicles_st: VehiclesState) -> Queue<VehicleFact>
|
||||||
return vehicles_st.ve_fact_q
|
return vehicles_st.ve_fact_q
|
||||||
}
|
}
|
||||||
|
|
||||||
function ve_say(vehicles_st: VehiclesState, what: int, v: Vehicle, n: float) -> void {
|
# 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
|
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.what = what
|
||||||
f.nid = v.nid
|
f.nid = v.nid
|
||||||
f.kind = v.kind
|
f.kind = v.kind
|
||||||
|
|
|
||||||
|
|
@ -375,4 +375,17 @@ program VehiclesTest {
|
||||||
expect_eq(len(vehicles_all(vehicles_st)), 0)
|
expect_eq(len(vehicles_all(vehicles_st)), 0)
|
||||||
expect(vehicle_by_nid(vehicles_st, b.nid) == null)
|
expect(vehicle_by_nid(vehicles_st, b.nid) == null)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "a fed fact a turn, drained each turn, makes a few records and then reuses them" (vehicles_st: mut VehiclesState, vehicles_test_st: mut VehiclesTestState) {
|
||||||
|
fresh(vehicles_st, vehicles_test_st)
|
||||||
|
vehicle_call_for(vehicles_st, 0, VEHICLE_HORSE, 0)
|
||||||
|
let h = vehicle_find(vehicles_st, VEHICLE_HORSE)
|
||||||
|
var fed = 0
|
||||||
|
for k in 0 .. 1000 {
|
||||||
|
vehicle_feed(vehicles_st, h, 1.0)
|
||||||
|
fed += count(facts(vehicles_st), VEHICLE_FED)
|
||||||
|
}
|
||||||
|
expect_eq(fed, 1000)
|
||||||
|
expect(vehicle_fact_records(vehicles_st) <= 4)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue