merge lang/physics-senses: animals and people slide round still things, the aim's, photo's and animals' sight lines are one Jolt ray

Conflicts with 0.R5's state defaults in ludic.wildlife resolved; wl_step writes the push's answer, so
its callers take WildlifeState mut again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-09-27 01:23:17 +03:00
commit ac815bf638
28 changed files with 236 additions and 158 deletions

View file

@ -78,6 +78,8 @@ a `fill` line's words are `NpcTalk.say(p, line)`, and "" means it has nothing tr
export port NpcWorld { # every member has a default: a flat dry field at noon, one player, nobody wanted
ground, water, slope: fn(float, float) -> float
allowed: fn(float, float) -> bool # may a person stop here
push: fn(x, z, r, feet, head) -> bool, pushed_x, pushed_z # a walker slid clear of still things (r 0.35)
clear: fn(ax, ay, az, bx, by, bz) -> bool # no still thing between: npc_line_ok asks it at 1 m
hour: fn() -> float, day: fn() -> int # the clock
players: fn() -> int, player_on: fn(int) -> bool, player_x / player_z: fn(int) -> float
want: fn() -> int, extra: fn() -> int # the census: how many, and a kind brought out regardless (-1)

View file

@ -1,5 +1,5 @@
# ground.ludic - where a person can stand and walk: dry, gentle, allowed; a straight walk with no
# water and no scramble on it; a spot in a ring, a spot toward something, and the nearest shore.
# water, scramble or still thing on it; a spot in a ring, a spot toward something, the nearest shore.
# A finder that succeeds leaves its answer in npc_px / npc_pz (and the water in npc_wx / npc_wz).
export function npc_px(npc_st: NpcState) -> float { return npc_st.np_px }
@ -18,6 +18,7 @@ export function npc_line_ok(ax: float, az: float, bx: float, bz: float) -> bool
var n = int(d / 1.5)
if n < 1 { n = 1 }
var prev = NpcWorld.ground(ax, az)
if not NpcWorld.clear(ax, prev + 1.0, az, bx, NpcWorld.ground(bx, bz) + 1.0, bz) { return false } # a trunk, a tent
for i in 1 .. n + 1 {
let t = float(i) / float(n)
let x = ax + (bx - ax) * t

View file

@ -19,6 +19,8 @@ export state NpcState {
nw_ids: int = 0
np_near_i: int = -1
np_said: string = ""
np_ask_x: float = 0.0 # the point handed to NpcWorld.push, then where it came out
np_ask_z: float = 0.0
}
const NP_PI: float = 3.1415927
const NP_TAU: float = 6.2831853

View file

@ -5,6 +5,11 @@ export port NpcWorld {
water: fn(float, float) -> float = fn nw_dry # the water's surface there
slope: fn(float, float) -> float = fn nw_zero2
allowed: fn(float, float) -> bool = fn nw_yes2 # a person may stop here (not the player's camp)
# (x, z, r, feet, head): true when a still thing takes a body of radius r there; pushed_x / _z say where
push: fn(float, float, float, float, float) -> bool = fn nw_no_push
pushed_x: fn() -> float = fn nw_asked_x
pushed_z: fn() -> float = fn nw_asked_z
clear: fn(float, float, float, float, float, float) -> bool = fn nw_clear # nothing between two points
hour: fn() -> float = fn nw_noon
day: fn() -> int = fn nw_day_one
players: fn() -> int = fn nw_one # player slots
@ -34,6 +39,10 @@ function nw_zero1(p: int) -> float { return 0.0 }
function nw_zero2(x: float, z: float) -> float { return 0.0 }
function nw_dry(x: float, z: float) -> float { return -1000.0 }
function nw_yes2(x: float, z: float) -> bool { return true }
function nw_no_push(x: float, z: float, r: float, y0: float, y1: float) -> bool { return false }
function nw_asked_x(npc_st: NpcState) -> float { return npc_st.np_ask_x }
function nw_asked_z(npc_st: NpcState) -> float { return npc_st.np_ask_z }
function nw_clear(ax: float, ay: float, az: float, bx: float, by: float, bz: float) -> bool { return true }
function nw_noon() -> float { return 12.0 }
function nw_day_one() -> int { return 1 }
function nw_one() -> int { return 1 }

View file

@ -11,6 +11,9 @@ export state NpcTestsFakeState {
fk_on: []bool = null
fk_born: int = 0
fk_gone: int = 0
fk_trunk: bool = false # a trunk 1 m round at (10, 0.3)
fk_out_x: float = 0.0
fk_out_z: float = 0.0
}
function fk_setup(npc_st: mut NpcState, npc_tests_fake_st: mut NpcTestsFakeState) -> void {
@ -23,6 +26,7 @@ function fk_setup(npc_st: mut NpcState, npc_tests_fake_st: mut NpcTestsFakeState
npc_tests_fake_st.fk_on = [true, false]
npc_tests_fake_st.fk_born = 0
npc_tests_fake_st.fk_gone = 0
npc_tests_fake_st.fk_trunk = false
npc_clear(npc_st)
npc_reset(npc_st)
npc_seed(npc_st, 7)
@ -61,3 +65,22 @@ function fk_count(npc_st: NpcState, what: int) -> int {
for i in 0 .. len(fs) { if fs[i].what == what { n += 1 } }
return n
}
# the trunk, when it stands: a body inside it is pushed straight out from its axis, and a line
# passing within its radius is not clear
function fk_push(npc_tests_fake_st: mut NpcTestsFakeState, x: float, z: float, r: float, y0: float, y1: float) -> bool {
let d = npc_d(x, z, 10.0, 0.3)
if not npc_tests_fake_st.fk_trunk or d > 1.0 + r or d < 0.0001 { return false }
npc_tests_fake_st.fk_out_x = 10.0 + (x - 10.0) / d * (1.0 + r)
npc_tests_fake_st.fk_out_z = 0.3 + (z - 0.3) / d * (1.0 + r)
return true
}
function fk_pushed_x(npc_tests_fake_st: NpcTestsFakeState) -> float { return npc_tests_fake_st.fk_out_x }
function fk_pushed_z(npc_tests_fake_st: NpcTestsFakeState) -> float { return npc_tests_fake_st.fk_out_z }
function fk_clear(npc_tests_fake_st: NpcTestsFakeState, ax: float, ay: float, az: float, bx: float, by: float, bz: float) -> bool {
if not npc_tests_fake_st.fk_trunk { return true }
let lx = bx - ax
let lz = bz - az
let t = Math.clamp(((10.0 - ax) * lx + (0.3 - az) * lz) / Math.max(lx * lx + lz * lz, 0.0001), 0.0, 1.0)
return npc_d(ax + lx * t, az + lz * t, 10.0, 0.3) > 1.0
}

View file

@ -10,6 +10,10 @@ program NpcTest {
ground: fn fk_ground
water: fn fk_water
allowed: fn fk_allowed
push: fn fk_push
pushed_x: fn fk_pushed_x
pushed_z: fn fk_pushed_z
clear: fn fk_clear
hour: fn fk_hour_now
day: fn fk_day_now
players: fn fk_players
@ -95,6 +99,21 @@ program NpcTest {
expect_near(p.fx, 30.0, 0.01)
}
test "a trunk in the way is walked round, and a line through it is not a clear walk" (npc_st: mut NpcState, npc_tests_fake_st: mut NpcTestsFakeState) {
let p = one(npc_st, npc_tests_fake_st, NPCK_HIKER)
npc_tests_fake_st.fk_trunk = true
expect(not npc_line_ok(0.0, 0.0, 20.0, 0.0))
expect(npc_line_ok(0.0, 5.0, 20.0, 5.0))
npc_go(npc_st, p, 20.0, 0.0, NPCA_SIT, 30.0)
var closest = 100.0
for k in 0 .. 600 {
npc_tick(npc_st, 0.05)
closest = Math.min(closest, npc_d(p.x, p.z, 10.0, 0.3))
}
expect(closest > 1.3)
expect_eq(p.act, NPCA_SIT)
}
test "nobody walks into the lake" (npc_st: mut NpcState, npc_tests_fake_st: mut NpcTestsFakeState) {
let p = one(npc_st, npc_tests_fake_st, NPCK_HIKER)
npc_go(npc_st, p, -80.0, 0.0, NPCA_SIT, 30.0)

View file

@ -1,5 +1,8 @@
# walk.ludic - walking to the target: turn toward it, step, never into the water and never up a
# scramble - step round it instead - and give up on a target that gets no closer for twenty seconds
# walk.ludic - walking to the target: turn toward it, step, slid round a trunk or a tent, never into
# the water and never up a scramble - step round it instead - and give up on a target that gets no
# closer for twenty seconds
const NP_BODY_R: float = 0.35 # a person's width, for the still things
export function npc_walk(npc_st: mut NpcState, p: NpcPerson, dt: float) -> void {
var gx = p.tx
var gz = p.tz
@ -29,10 +32,12 @@ export function npc_walk(npc_st: mut NpcState, p: NpcPerson, dt: float) -> void
let r = 3.0 * dt
p.yaw = p.yaw + Math.clamp(npc_wrap(want - p.yaw), -r, r)
let step = sp * dt
let nx = p.x - Math.sin(p.yaw) * step
let nz = p.z - Math.cos(p.yaw) * step
np_slide(npc_st, p, p.x - Math.sin(p.yaw) * step, p.z - Math.cos(p.yaw) * step)
let nx = npc_st.np_ask_x
let nz = npc_st.np_ask_z
let nh = NpcWorld.ground(nx, nz)
if nh < NpcWorld.water(nx, nz) + 0.4 or nh - p.y > step * 1.4 {
let slid = Math.abs(nx - p.x) + Math.abs(nz - p.z)
if nh < NpcWorld.water(nx, nz) + 0.4 or nh - p.y > step * 1.4 or slid < step * 0.2 {
p.stall = p.stall + dt
if p.stall > p.stall_max { p.stall_max = p.stall }
if p.detours < 10 and np_detour(p, want) {
@ -49,6 +54,17 @@ export function npc_walk(npc_st: mut NpcState, p: NpcPerson, dt: float) -> void
p.phase = p.phase + step * 2.6
}
# (x, z) slid clear of every still thing for a person; np_ask_x / _z hold where it came out
function np_slide(npc_st: mut NpcState, p: NpcPerson, x: float, z: float) -> void {
npc_st.np_ask_x = x
npc_st.np_ask_z = z
if not NpcWorld.push(x, z, NP_BODY_R, p.y + 0.1, p.y + 1.8) { return }
let ox = NpcWorld.pushed_x()
let oz = NpcWorld.pushed_z()
npc_st.np_ask_x = ox
npc_st.np_ask_z = oz
}
# there: a step round something ends, or the errand begins, facing what it came for
function np_arrive(npc_st: mut NpcState, p: NpcPerson) -> void {
if p.via_on {