feat(ecs): wildlife_near / wildlife_near_of into the caller's list, wildlife_alive_of

The living animals around a point from the grid (a rare species from its own list), into a list
the caller keeps in its own state, so a frame's proximity question walks the cells it covers
rather than every animal ever made. wildlife_alive_of is the species index's count.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-09-27 21:54:39 +03:00
parent 045bf22e10
commit 7ceefa8c50
3 changed files with 20 additions and 0 deletions

View file

@ -10,6 +10,7 @@ import "port.ludic"
import "facts.ludic"
import "table.ludic"
import "store.ludic"
import "near.ludic"
import "guest.ludic"
import "record.ludic"
import "habitat.ludic"

View file

@ -0,0 +1,15 @@
# near.ludic - the animals around a point, living ones only, into a list the CALLER keeps (in its own
# state, reused every frame): from the grid, or a species' own list when it is rare. Nothing is
# allocated or written here.
export function wildlife_near(wildlife_st: WildlifeState, x: float, z: float, r: float, out: []WildAnimal) -> int {
let w = wildlife_st.wl
return tb_within_recs(w.tab, w.grid, x, z, r, -1, 0, out)
}
export function wildlife_near_of(wildlife_st: WildlifeState, sp: int, x: float, z: float, r: float, out: []WildAnimal) -> int {
let w = wildlife_st.wl
return tb_within_of_recs(w.tab, w.grid, w.species, x, z, r, sp, out)
}
# how many of a species are alive, legends included, without a walk
export function wildlife_alive_of(wildlife_st: WildlifeState, sp: int) -> int { return ix_count(wildlife_st.wl.species, sp) }

View file

@ -243,6 +243,10 @@ program WildlifeTest {
expect_eq(wildlife_count(wildlife_st, 1), 0)
expect(wildlife_nearest(wildlife_st, -1, 0.0, 0.0, 0.0) == a)
expect(wildlife_nearest(wildlife_st, 1, 0.0, 0.0, 0.0) == c)
let near = new []WildAnimal
expect_eq(wildlife_near(wildlife_st, 0.0, 0.0, 40.0, near), 2)
expect_eq(wildlife_near_of(wildlife_st, 0, 0.0, 0.0, 200.0, near), 2)
expect_eq(wildlife_alive_of(wildlife_st, 1), 1)
wildlife_remove(a)
expect_eq(wildlife_count(wildlife_st, 0), 1)
expect(wildlife_nearest(wildlife_st, 0, 0.0, 0.0, 0.0) == b)