diff --git a/packages/ludic.npc/names.ludic b/packages/ludic.npc/names.ludic index 48582954..81d6699c 100644 --- a/packages/ludic.npc/names.ludic +++ b/packages/ludic.npc/names.ludic @@ -7,30 +7,47 @@ export function npc_name_used(npc_st: NpcState, n: string) -> bool { for i in 0 .. len(all) { if all[i].active and all[i].name == n { return true } } let posts = npc_posts(npc_st) for i in 0 .. len(posts) { if posts[i].active and posts[i].name == n { return true } } - if npc_st.np_recent != null { for i in 0 .. len(npc_st.np_recent) { if npc_st.np_recent[i] == n { return true } } } + for i in 0 .. len(npc_st.np_recent) { if npc_st.np_recent[i] != "" and npc_st.np_recent[i] == n { return true } } return false } # a free name from the body's pool, from a place in it the dice choose ("" when the pool is empty) export function npc_pick_name(npc_st: NpcState, body: int) -> string { - let pool = new []string - for i in 0 .. NPCN_COUNT { if NpcNames[i].body == body { push(pool, NpcNames[i].name) } } - let n = len(pool) + var n = 0 + for i in 0 .. NPCN_COUNT { if NpcNames[i].body == body { n += 1 } } if n == 0 { return "" } let start = npc_between(npc_st, 0, n - 1) for k in 0 .. n { - let name = pool[(start + k) % n] + let name = np_name_nth(body, (start + k) % n) if not npc_name_used(npc_st, name) { return name } } - return pool[start] + return np_name_nth(body, start) +} +# the body's j-th name in the table, counted in place rather than gathered into a list +function np_name_nth(body: int, j: int) -> string { + var seen = 0 + for i in 0 .. NPCN_COUNT { + if NpcNames[i].body == body { + if seen == j { return NpcNames[i].name } + seen += 1 + } + } + return "" } +# the last eight who left, in a ring of slots made with the state function np_left(npc_st: mut NpcState, name: string) -> void { - push(npc_st.np_recent, name) - if len(npc_st.np_recent) <= 8 { return } - let keep = new []string - for i in len(npc_st.np_recent) - 8 .. len(npc_st.np_recent) { push(keep, npc_st.np_recent[i]) } - npc_st.np_recent = keep + npc_st.np_recent[npc_st.np_rnext] = name + npc_st.np_rnext = (npc_st.np_rnext + 1) % len(npc_st.np_recent) +} +function np_recent_new() -> []string { + let l = new []string + for i in 0 .. 8 { push(l, "") } + return l +} +function np_recent_clear(npc_st: mut NpcState) -> void { + for i in 0 .. len(npc_st.np_recent) { npc_st.np_recent[i] = "" } + npc_st.np_rnext = 0 } # how many pairs share a name, the place's own names included diff --git a/packages/ludic.npc/person.ludic b/packages/ludic.npc/person.ludic index f6f90360..7615671c 100644 --- a/packages/ludic.npc/person.ludic +++ b/packages/ludic.npc/person.ludic @@ -6,7 +6,8 @@ export state NpcState { np_wx: float = 0.0 np_wz: float = 0.0 np_clock: float = 0.0 - np_recent: []string = new []string # the last few names that left + np_recent: []string = np_recent_new() # the last few names that left, a ring + np_rnext: int = 0 np_walkers: []NpcPerson = new []NpcPerson np_posts: []NpcPerson = new []NpcPerson np_max: int = 10 # walkers at most diff --git a/packages/ludic.npc/tick.ludic b/packages/ludic.npc/tick.ludic index 1e9da316..ae7c8a48 100644 --- a/packages/ludic.npc/tick.ludic +++ b/packages/ludic.npc/tick.ludic @@ -85,7 +85,7 @@ export function npc_reset(npc_st: mut NpcState) -> void { npc_st.np_n = 0 npc_st.np_next_h = 0.0 npc_st.np_hold = false - npc_st.np_recent = new []string + np_recent_clear(npc_st) q_clear(npc_facts(npc_st)) }