From 0c6235e55b23afd553f9de3b90e0f1dc201c4a86 Mon Sep 17 00:00:00 2001 From: Orkuncakilkaya Date: Sun, 27 Sep 2026 22:25:34 +0300 Subject: [PATCH] feat(ecs): thing_nearest_within - the nearest of a kind strictly within a radius, from the grid or the kind's list Co-Authored-By: Claude Opus 5.5 --- packages/ludic.things/near.ludic | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/ludic.things/near.ludic b/packages/ludic.things/near.ludic index 62b811b2..27a4063a 100644 --- a/packages/ludic.things/near.ludic +++ b/packages/ludic.things/near.ludic @@ -24,3 +24,12 @@ export function thing_of_kind_all(things_st: ThingsState, kind: int, i: int) -> if i < 0 or i >= len(rows) { return null } return things_st.th.tab.rec[rows[i]] } + +# the nearest active one of a kind (below 0: any) strictly within maxr of (x, z), or null +export function thing_nearest_within(things_st: ThingsState, kind: int, x: float, z: float, maxr: float) -> Thing { + let tb = things_st.th.tab + var r = -1 + if kind < 0 { r = tb_nearest(tb, things_st.th.grid, x, z, maxr, -1, 0) } else { r = tb_nearest_of(tb, things_st.th.grid, things_st.th.kinds, x, z, maxr, kind) } + if r < 0 { return null } + return tb.rec[r] +}