ludic/packages/ludic.npc/act.ludic
Orkuncakilkaya fe2d2bacd7 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>
2026-09-28 01:58:19 +03:00

47 lines
1.7 KiB
Text

# act.ludic - doing the act: turned toward what it is about - or toward whoever came over to talk,
# within the kind's notice - the game's own tick for the act, and a new plan when its time is up
export function npc_act(npc_st: mut NpcState, p: NpcPerson, dt: float) -> void {
p.act_t = p.act_t - dt
var wx = p.fx
var wz = p.fz
if p.fixed {
wx = p.x - Math.sin(p.stance) * 10.0
wz = p.z - Math.cos(p.stance) * 10.0
}
if not p.stalk and np_turns(p.act) and npc_near(npc_st, p.x, p.z) < NpcKinds[p.kind].notice {
let i = npc_near_i(npc_st)
wx = NpcWorld.player_x(i)
wz = NpcWorld.player_z(i)
}
var r = 2.0 * dt
if p.fixed { r = 2.2 * dt }
p.yaw = p.yaw + Math.clamp(npc_wrap(Math.atan2(-(wx - p.x), -(wz - p.z)) - p.yaw), -r, r)
if p.fixed { return }
if p.act >= 0 and p.act < NPCA_COUNT and NpcActs[p.act].tick != null {
let tick = NpcActs[p.act].tick
if tick(p, dt) { return }
}
if p.act_t < 0.0 { npc_plan(npc_st, p) }
}
function np_turns(act: int) -> bool {
if act < 0 or act >= NPCA_COUNT { return true }
return NpcActs[act].turns
}
# a player who came within notice since the last look is greeted - once, until they walk off
export function npc_notice(npc_st: mut NpcState, p: NpcPerson) -> void {
let reach = NpcKinds[p.kind].notice
for i in 0 .. NpcWorld.players() {
if i >= 30 { break }
let bit = 1 << i
var d = 1000000.0
if NpcWorld.player_on(i) { d = npc_d(p.x, p.z, NpcWorld.player_x(i), NpcWorld.player_z(i)) }
if d < reach and (p.near & bit) == 0 {
p.near = p.near | bit
np_fact(npc_st, NPC_F_GREETED, p, i)
}
if d > reach * 1.5 and (p.near & bit) != 0 { p.near = p.near - bit }
}
}