ludic/packages/ludic.wildlife/steer.ludic
Orkuncakilkaya b1fff7c921 ludic.wildlife: a climb is judged over half a metre ahead, not one frame's step
17.10 found almost every animal turn-back was for a climb, where the navmesh (40-45 degrees) is stricter than the step's 1.2 per metre. So the frame-sized test was refusing a few centimetres of steep ground: a pebble a walker steps across. It also refused sooner the faster the frame rate. wl_climb takes the ground half a metre ahead along the way the step actually goes. climb_test holds it at 60 and 240 Hz: a pebble is crossed and a wall is not. Both tests fail under the old rule. wildlife 24/24.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-27 23:52:49 +03:00

99 lines
3.8 KiB
Text

# steer.ludic - turning toward a point at a rate, a step along the facing that turns away from water
# and a climb; a walker leaves a print every 2.8 m (the way there and a target about home: way.ludic)
function wl_wrap(a: float) -> float {
var r = a
while r > 3.1415927 { r = r - WL_TAU }
while r < -3.1415927 { r = r + WL_TAU }
return r
}
function wl_face(a: WildAnimal, tx: float, tz: float, rate: float, dt: float) -> void {
let want = Math.atan2(-(tx - a.x), -(tz - a.z))
let r = rate * dt
a.yaw = a.yaw + Math.clamp(wl_wrap(want - a.yaw), -r, r)
}
# true when it moved; a walker is slid round a still thing (a trunk, a tent), a bird is not
# how many steps were refused and turned from since the start: how often the way was not enough
export function wildlife_turned(wildlife_st: WildlifeState) -> int { return wildlife_st.wl_turned }
# of those, how many for water ahead and how many for a climb (the rest: pushed back by a still thing)
export function wildlife_turned_wet(wildlife_st: WildlifeState) -> int { return wildlife_st.wl_turned_wet }
export function wildlife_turned_steep(wildlife_st: WildlifeState) -> int { return wildlife_st.wl_turned_steep }
# a climb is judged over half a metre ahead, not one frame's step: a few centimetres of the drawn
# ground rise steeply over a pebble a walker steps across, and a frame's step shrinks with the rate
const WL_LOOK: float = 0.5
function wl_climb(a: WildAnimal, nx: float, nz: float) -> float {
let dx = nx - a.x
let dz = nz - a.z
let l = Math.sqrt(dx * dx + dz * dz)
return WildlifeWorld.ground(a.x + dx / l * WL_LOOK, a.z + dz / l * WL_LOOK) - a.y
}
function wl_step(wildlife_st: mut WildlifeState, a: WildAnimal, dt: float) -> bool {
let d = a.speed * dt
if d < 0.0001 { return false }
var nx = a.x - Math.sin(a.yaw) * d
var nz = a.z - Math.cos(a.yaw) * d
let s = wl_sp(wildlife_st, a.sp)
if not s.flies and wl_push(wildlife_st, a, s, nx, nz) {
nx = wildlife_st.wl_out_x
nz = wildlife_st.wl_out_z
}
let nh = WildlifeWorld.ground(nx, nz)
let moved = Math.abs(nx - a.x) + Math.abs(nz - a.z)
let wet = nh < WildlifeWorld.water(nx, nz) + 0.6
let steep = moved > 0.0001 and wl_climb(a, nx, nz) > WL_LOOK * 1.2
if wet or steep or moved < d * 0.2 {
if wet { wildlife_st.wl_turned_wet += 1 } else if steep { wildlife_st.wl_turned_steep += 1 }
a.yaw = a.yaw + Math.deg_to_rad(75.0)
a.timer = 0.0
wildlife_st.wl_turned += 1
return false
}
a.x = nx
a.z = nz
a.y = nh
a.track_d = a.track_d + d
if s.tracks and a.track_d > 2.8 {
a.track_d = 0.0
wl_fact(wildlife_st, WILD_PRINT, a)
}
return true
}
# (x, z) pushed clear of every still thing for a body its size; wl_out_x / _z say where
function wl_push(wildlife_st: mut WildlifeState, a: WildAnimal, s: WildSpecies, x: float, z: float) -> bool {
wildlife_st.wl_ask_x = x
wildlife_st.wl_ask_z = z
let k = Math.clamp(float(s.size), 0.0, 2.0)
let r = (0.2 + 0.2 * k) * s.scale * a.scale
let hit = WildlifeWorld.push(x, z, r, a.y + 0.1, a.y + (0.5 + 0.7 * k) * s.scale * a.scale)
wildlife_st.wl_out_x = x
wildlife_st.wl_out_z = z
if hit {
wildlife_st.wl_out_x = WildlifeWorld.pushed_x()
wildlife_st.wl_out_z = WildlifeWorld.pushed_z()
}
return hit
}
function wl_at_target(a: WildAnimal, r: float) -> bool {
let dx = a.tx - a.x
let dz = a.tz - a.z
return dx * dx + dz * dz < r * r
}
# back to wandering about home after `t` seconds of whatever it was doing
function wl_wander(wildlife_st: WildlifeState, a: WildAnimal, t: float) -> void {
a.state = WILD_WANDER
a.timer = t
wl_pick_target(wildlife_st, a, wl_sp(wildlife_st, a.sp).home)
}
# home where it stands now, kept inside its range
function wl_rehome(wildlife_st: WildlifeState, a: WildAnimal) -> void {
a.home_x = a.x
a.home_z = a.z
wl_clamp(wildlife_st, a)
}