ludic/packages/ludic.session/near.ludic
Orkuncakilkaya 19fcf60599 wip(0.S3): packages and examples migrated again from their pre-0.S sources in one run
ludic migrate state packages <every example program> packages/ludic.lab/example/plate.ludic
  1804 vars into 126 states, 64 into lets; 23498 edits in 460 files

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 15:31:34 +03:00

30 lines
1.1 KiB
Text

# near.ludic - the world asks who is nearest, never "the player": an animal notices whoever is
# closest, a camper keeps clear of everyone
export function ply_near_d2(session_st: mut SessionState, x: float, z: float) -> float {
session_st.session__near_i = 0
var best = 0.0
var first = true
for p in 0 .. PLY_MAX {
if not ply_on(session_st, p) { continue }
let dx = x - ply_x(session_st, p)
let dz = z - ply_z(session_st, p)
let d2 = dx * dx + dz * dz
if first or d2 < best {
best = d2
session_st.session__near_i = p
first = false
}
}
return best
}
# the distance to the nearest player, in metres; ply_near_i() says which
export function ply_near(session_st: mut SessionState, x: float, z: float) -> float { return Math.sqrt(ply_near_d2(session_st, x, z)) }
export function ply_near_i(session_st: SessionState) -> int { return session_st.session__near_i }
export function ply_dist(session_st: SessionState, p: int, x: float, z: float) -> float {
let dx = x - ply_x(session_st, p)
let dz = z - ply_z(session_st, p)
return Math.sqrt(dx * dx + dz * dz)
}