ludic.base, ludic.wildlife, ludic.npc: fact records reused, not made per fact
wl_fact made a new WildFact per fact, about one a frame, and Ludic never gives one back. ludic.base's q_unheld(q, pool, out) lists the records a queue no longer holds: neither waiting nor handed out by its last drain, which is good until the next drain. ludic.wildlife and ludic.npc now keep a pool, hand out a free record, and make a new one only when every record is held. Taking a record writes the state, so wl_fact / np_fact and the few callers holding their state read-only take it mut. Tests: q_unheld's rules (an empty drain holds on, the safe side); 3000 prints drained frame by frame use at most 4 records; 500 arrivals drained turn by turn use at most 4. base 37, wildlife 27, npc 17; all packages 420. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
3e3c6088fb
commit
fe2d2bacd7
13 changed files with 129 additions and 10 deletions
|
|
@ -32,8 +32,31 @@ export function wildlife_facts(wildlife_st: WildlifeState) -> Queue<WildFact> {
|
|||
return wildlife_st.wl_facts
|
||||
}
|
||||
|
||||
function wl_fact(wildlife_st: WildlifeState, what: int, a: WildAnimal) -> WildFact {
|
||||
# a fact record: one no drain holds any more, made fresh (and kept) only when every one is held - a
|
||||
# fact is pushed about once a frame, and a record a frame is never given back
|
||||
function wl_fact_record(wildlife_st: mut WildlifeState) -> WildFact {
|
||||
if wildlife_st.wl_fnext[0] >= len(wildlife_st.wl_ffree) {
|
||||
q_unheld(wildlife_st.wl_facts, wildlife_st.wl_fpool, wildlife_st.wl_ffree)
|
||||
wildlife_st.wl_fnext[0] = 0
|
||||
}
|
||||
if wildlife_st.wl_fnext[0] < len(wildlife_st.wl_ffree) {
|
||||
let f = wildlife_st.wl_ffree[wildlife_st.wl_fnext[0]]
|
||||
wildlife_st.wl_fnext[0] += 1
|
||||
f.player = 0
|
||||
f.lure = 0
|
||||
f.owner = -1
|
||||
f.diet = 0
|
||||
return f
|
||||
}
|
||||
let f = new WildFact
|
||||
push(wildlife_st.wl_fpool, f)
|
||||
return f
|
||||
}
|
||||
# how many fact records have ever been made: the peak held at once, not the facts pushed
|
||||
export function wildlife_fact_records(wildlife_st: WildlifeState) -> int { return len(wildlife_st.wl_fpool) }
|
||||
|
||||
function wl_fact(wildlife_st: mut WildlifeState, what: int, a: WildAnimal) -> WildFact {
|
||||
let f = wl_fact_record(wildlife_st)
|
||||
f.what = what
|
||||
f.key = a.key
|
||||
f.nid = a.nid
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue