ludic/runtime/native/query.ludic
Orkuncakilkaya bf36bc8a8f refactor(runtime,packages,examples): named constants, package enums, idiom sweep
- runtime: HEADLESS_FRAME_PATH, STICK_DEADZONE / STICK_LEFT_X/Y, key and
  byte codes as char literals throughout (`k == 'w'`, `fill(rt_map, ' ', …)`)
- ludic.gameplay/stats: drop the duplicate `stat_field` (it answered "atk"
  for every build stat); Stats.base uses stats_field_name
- ludic.shooter: compare aim modes and fire patterns with AimMode.* and
  WeaponPattern.* instead of raw ints; STICK_RIGHT_X/Y
- ludic.npcai: DecisionMade / brain_set_state use AiState.*
- examples/games/menu.ludic uses Font.load / Ui.* with FONT_PATH and
  BACKDROP named; strings.ludic header says what it prints
- whole tree: `x = x + 1` → `x += 1` (single-term right-hand sides only),
  `0 - x` → `-x`, ASCII codes → char literals; every .ludic and every
  ```ludic fence reformatted with the fixed formatter (whitespace only)

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-05 01:12:26 +03:00

76 lines
3.1 KiB
Text

# ============================================================================
# query.ludic — ECS spatial queries over the reflection ABI, in Ludic.
#
# The Query.* namespace (see emit_call.ludic) answers questions about the live
# entities that carry a given property: how many, the first, the nearest to a
# point, and every one inside a radius. `prop` is a property id from
# World.prop_id("Name"); the spatial forms read two int fields of that property
# (field ids from World.field_id) as an (x, y) position. Everything is integer
# and deterministic — same world + same query reproduce the same answer, entity
# order included, every run.
#
# ludicc splices this file into a game when it sees Query.* (parse.ludic), and
# force-emits the reflection ABI it stands on (emit_decl.ludic) so a game that
# uses Query needs no @events of its own. The scan is linear over the entity
# table — ample for the entity counts Ludic targets, exactly like the grid
# pathfinder's open set — and allocates nothing except the one result slice
# `within` returns. world_query_next / world_get are the bare reflection
# builtins (they lower to the generated @ludic_query_next / @ludic_get).
# ============================================================================
# how many live entities carry `prop`.
function query_count(prop: int) -> int {
var n = 0
var e = world_query_next(prop, 0)
while e >= 0 {
n += 1
e = world_query_next(prop, e + 1)
}
return n
}
# the first (lowest-id) live entity carrying `prop`, or -1 if none.
function query_first(prop: int) -> int {
return world_query_next(prop, 0)
}
# the entity carrying `prop` whose position is closest to (px, py) by squared
# distance, or -1 if none. Ties go to the lower entity id. The position is read
# from `pos` — the property that holds the coordinates — at its (x_field,
# y_field); pass the same id for `prop` and `pos` to query by the position
# component itself, or a separate tag for `prop` (e.g. "nearest Enemy").
function query_nearest(prop: int, pos: int, x_field: int, y_field: int, px: int, py: int) -> int {
var best = -1
var bestd = 0
var e = world_query_next(prop, 0)
while e >= 0 {
let dx = world_get(e, pos, x_field) - px
let dy = world_get(e, pos, y_field) - py
let d = dx * dx + dy * dy
if best < 0 or d < bestd {
best = e
bestd = d
}
e = world_query_next(prop, e + 1)
}
return best
}
# every live entity carrying `prop` whose position is within `radius` of
# (px, py), in ascending entity-id order. Returns a []int of entity ids — index
# it with len / [i]. The boundary is inclusive (distance <= radius). Position is
# read from `pos` at (x_field, y_field), as in query_nearest.
function query_within(prop: int, pos: int, px: int, py: int, radius: int, x_field: int, y_field: int) -> []int {
let out = new []int
let r2 = radius * radius
var e = world_query_next(prop, 0)
while e >= 0 {
let dx = world_get(e, pos, x_field) - px
let dy = world_get(e, pos, y_field) - py
if dx * dx + dy * dy <= r2 {
push(out, e)
}
e = world_query_next(prop, e + 1)
}
return out
}