ludic/packages/ludic.wildlife/react.ludic
Orkuncakilkaya 71735b10a2 feat(base): 0.R4 - a queue keeps its own count, so every package verb takes only its own state; ludic migrate state --prune
ludic.base's Queue<T> carries a QueueTag (its name and pending count): queue_new(name), q_push(q, v),
q_drain(q), q_clear(q) take no BaseState, and core_undrained(tags) names the given queues still holding
facts. A reducer on a package's state can now call that package's verbs (wallet_earn(wallet_st, n)).

ludic migrate state --prune (ludicc --migrate-prune) takes out each state parameter a function no
longer uses, nor anything it calls, and the argument that fills it - including an argument for a
parameter the callee has dropped, which is taken out before the call is checked, so a generic's T is
told by the argument that says it. A reducer keeps its state. --dry-run now counts the edits it would
make. Every package was moved with it: 608 base_st parameters and their arguments, 1889 edits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 04:19:56 +03:00

79 lines
2.7 KiB
Text

# react.ludic - alert (it stops and looks: the window the camera wants), wary (it puts ground between
# you without running) and fleeing; true when it is doing one of them, which is all it does this tick
function wl_react(wildlife_st: mut WildlifeState, a: WildAnimal, np: int, dt: float) -> bool {
let s = wl_sp(wildlife_st, a.sp)
if a.state == WILD_ALERT {
a.speed = 0.0
wl_face(a, a.sus_x, a.sus_z, 2.2, dt)
if a.timer < 0.0 { wl_wander(wildlife_st, a, 3.0 + wl_rnd(wildlife_st) * 4.0) }
return true
}
if a.state == WILD_WARY {
wl_face(a, a.x + (a.x - a.sus_x), a.z + (a.z - a.sus_z), 2.6, dt)
a.speed = s.walk * 0.8
wl_step(wildlife_st, a, dt)
if a.timer < 0.0 { wl_wander(wildlife_st, a, 3.0 + wl_rnd(wildlife_st) * 4.0) }
return true
}
if a.state == WILD_FLEE {
wl_face(a, a.x + (a.x - WildlifeWorld.player_x(np)), a.z + (a.z - WildlifeWorld.player_z(np)), 5.0, dt)
a.speed = s.run
wl_step(wildlife_st, a, dt)
if a.timer < 0.0 {
wl_rehome(wildlife_st, a)
wl_wander(wildlife_st, a, 5.0)
}
return true
}
return false
}
# walking to (or reached) something set out; true while it is
function wl_approach(wildlife_st: mut WildlifeState, a: WildAnimal, dt: float) -> bool {
if (a.state == WILD_IDLE or a.state == WILD_WANDER) and wl_attraction(wildlife_st, a) { wl_take_lure(wildlife_st, a) }
if a.state != WILD_APPROACH { return false }
if a.lure == 0 or not WildlifeWorld.present(a.lure) {
a.lure = 0
a.state = WILD_IDLE
a.timer = 2.0
return true
}
wl_face(a, a.lure_x, a.lure_z, 3.0, dt)
a.speed = wl_sp(wildlife_st, a.sp).walk
let dx = a.lure_x - a.x
let dz = a.lure_z - a.z
if Math.sqrt(dx * dx + dz * dz) < 1.3 {
a.speed = 0.0
wl_arrive(wildlife_st, a)
return true
}
wl_step(wildlife_st, a, dt)
return true
}
# at a snare it is held; at food it eats for 14 s; at a lure it stands curious for 9
function wl_arrive(wildlife_st: mut WildlifeState, a: WildAnimal) -> void {
a.state = WILD_EAT
a.timer = 9.0
if a.lure_kind == WILD_FEED { a.timer = 14.0 }
if a.lure_kind != WILD_SNARE { return }
a.state = WILD_TRAPPED
a.x = a.lure_x
a.z = a.lure_z
a.y = WildlifeWorld.ground(a.x, a.z)
let f = wl_fact(wildlife_st, WILD_CAUGHT, a)
f.lure = a.lure
f.owner = a.lure_owner
}
# eating what it came to; when it is done, food is credited and it wanders off
function wl_eat(wildlife_st: mut WildlifeState, a: WildAnimal) -> bool {
if a.state != WILD_EAT { return false }
a.speed = 0.0
if a.timer < 0.0 {
if a.lure != 0 and a.lure_kind == WILD_FEED and WildlifeWorld.present(a.lure) { wl_ate(wildlife_st, a, a.lure, a.lure_owner, wl_sp(wildlife_st, a.sp).diet) }
a.lure = 0
wl_wander(wildlife_st, a, 4.0)
}
return true
}