ludic/packages/ludic.vehicles/horse.ludic
Orkuncakilkaya 231715bf98 fix(leaks): what play allocated every frame and never freed - a shadow uniform's name, the Tick, a ridden vehicle's moves
Found with malloc_history over 1200 frames of play without the interface (the first Ludic function
on each allocating stack, live bytes at two marks): sh_loc built `name + "[0]"` per program per pass
per frame - 24.6 MB of 33 in the window; its callers pass both names as literals now. tick_set fills
the frame loop's one Tick instead of tick_new making one a frame. A ridden boat or horse said
VEHICLE_MOVED as a new fact every frame; the metres are added up in VehiclesState and taken once
(vehicle_moved_take / _kind / _x / _z). Play's growth without the interface: 92 -> 14.7 MB a minute
(maroon-lake tests/leakcheck.sh); what is left is phys_push (Physics' fix on lang/nav) and the HUD's
strings. Packages 407, ludic-dev test 305 pass.

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

50 lines
2.3 KiB
Text

# horse.ludic - a horse under the stick: a walk, a canter on stamina, hay for every metre; it will
# not step into water, and its legs (VehicleLegs) meet the ground and whatever stands in its way
const VE_HORSE_WALK: float = 4.5
const VE_HORSE_GALLOP: float = 11.0
const VE_HORSE_ACC: float = 4.0
const VE_HORSE_TURN: float = 1.6
function ve_ride_horse(vehicles_st: mut VehiclesState, v: Vehicle, ix: float, iz: float, run: bool, dt: float) -> void {
var top = VE_HORSE_WALK
if run and v.fuel > 5.0 { top = VE_HORSE_GALLOP }
v.speed = v.speed + Math.clamp(iz * top - v.speed, -VE_HORSE_ACC * dt, VE_HORSE_ACC * dt)
# Steering follows SIGNED speed: backing up with the stick left swings the nose right
if Math.abs(v.speed) > 0.3 {
var rate = Math.clamp(Math.abs(v.speed) / 3.0, 0.4, 1.0)
if v.speed < 0.0 { rate = -rate }
v.yaw = v.yaw - ix * VE_HORSE_TURN * (dt * rate)
}
let fx = -Math.sin(v.yaw)
let fz = -Math.cos(v.yaw)
let d = v.speed * dt
let nh = VehicleWorld.ground(v.x + fx * d, v.z + fz * d)
if nh < VehicleWorld.water(v.x + fx * d, v.z + fz * d) + 0.4 {
v.speed = 0.0
return
}
ve_legs_walk(vehicles_st, v, fx * v.speed, fz * v.speed, dt)
ve_stamina(v, d, dt)
}
# the legs carry it from where it is; what they covered along its heading is its speed now, never
# more than was asked, so a trunk or a rise it cannot take brings it to a stand
function ve_legs_walk(vehicles_st: mut VehiclesState, v: Vehicle, vx: float, vz: float, dt: float) -> void {
if not VehicleLegs.walk(v.nid, v.x, v.y, v.z, vx, vz, dt) { return }
let dx = VehicleLegs.x() - v.x
let dz = VehicleLegs.z() - v.z
v.x = VehicleLegs.x()
v.y = VehicleLegs.y()
v.z = VehicleLegs.z()
let got = (dx * -Math.sin(v.yaw) + dz * -Math.cos(v.yaw)) / dt
if v.speed > 0.0 { v.speed = Math.clamp(got, 0.0, v.speed) } else { v.speed = Math.clamp(got, v.speed, 0.0) }
let moved = Math.sqrt(dx * dx + dz * dz)
if moved > 0.0 { ve_moved_add(vehicles_st, v, moved) }
}
# a gallop spends stamina and a walk gives it back; every metre costs hay
function ve_stamina(v: Vehicle, d: float, dt: float) -> void {
if Math.abs(v.speed) > 6.0 { v.fuel = Math.max(v.fuel - 6.0 * dt, 0.0) } else { v.fuel = Math.min(v.fuel + 3.0 * dt, 100.0) }
v.food = Math.max(v.food - Math.abs(d) * 0.02, 0.0)
}