ludic/packages/ludic.vehicles/call.ludic

77 lines
2.8 KiB
Text

# call.ludic - a player's boat or horse brought to a place: made there the first time, fetched there
# after; gone when its player leaves
function ve_add(vehicles_st: VehiclesState, kind: int, x: float, z: float, yaw: float) -> Vehicle {
if kind != VEHICLE_HORSE and kind != VEHICLE_BOAT { return null }
let v = new Vehicle
v.nid = VehicleWorld.next_id()
v.kind = kind
v.yaw = yaw
if kind == VEHICLE_HORSE {
v.seat_h = 1.35
v.fuel = 100.0
v.food = 40.0
} else {
v.seat_h = 0.30
}
ve_put(v, x, z)
push(ve_all(vehicles_st), v)
ve_settle(v)
VehicleWorld.made(v)
return v
}
# a boat's hull or a horse's legs put where the vehicle now is, at rest
function ve_settle(v: Vehicle) -> void {
if v.kind == VEHICLE_BOAT { VehicleHull.put(v.nid, v.x, v.y, v.z, v.yaw) } else { VehicleLegs.stand(v.nid, v.x, v.y, v.z) }
}
# gone from the world: its model and its hull or legs
function ve_gone(v: Vehicle) -> void {
if v.kind == VEHICLE_BOAT { VehicleHull.gone(v.nid) } else { VehicleLegs.gone(v.nid) }
VehicleWorld.gone(v)
}
# at rest at (x, z): a boat on the water, a horse on the ground and tied there
function ve_put(v: Vehicle, x: float, z: float) -> void {
v.x = x
v.z = z
v.speed = 0.0
if v.kind == VEHICLE_BOAT { v.y = VehicleWorld.water(x, z) + 0.02 } else { v.y = VehicleWorld.ground(x, z) }
v.home_x = x
v.home_z = z
}
# Bring player pid's boat or horse to place k. False when the place is somebody else's, or it is
# being ridden. A horse faces the trough: the bay lies on its near side.
export function vehicle_call_for(vehicles_st: VehiclesState, pid: int, kind: int, k: int) -> bool {
if not vehicles_st.ve_have_dock or k < 0 or k >= VEHICLE_PLACES { return false }
let here = vehicle_in_place(vehicles_st, kind, k)
let v = vehicle_of(vehicles_st, pid, kind)
if here != null { return v != null and here.nid == v.nid }
var yaw = vehicles_st.ve_dock_yaw
if kind == VEHICLE_HORSE { yaw = vehicles_st.ve_rail_yaw }
if v == null {
let made = ve_add(vehicles_st, kind, vehicle_place_x(vehicles_st, kind, k), vehicle_place_z(vehicles_st, kind, k), yaw)
if made == null { return false }
made.owner = pid
if pid == 0 { ve_apply_kept(vehicles_st, made) }
return true
}
if v.rider >= 0 { return false }
ve_put(v, vehicle_place_x(vehicles_st, kind, k), vehicle_place_z(vehicles_st, kind, k))
v.yaw = yaw
ve_settle(v)
VehicleWorld.placed(v)
return true
}
# a player left: their boat and horse go with them, and their places are free
export function vehicles_release(vehicles_st: mut VehiclesState, pid: int) -> void {
if pid == 0 { return }
let keep = new []Vehicle
let l = ve_all(vehicles_st)
for i in 0 .. len(l) {
if l[i].owner != pid { push(keep, l[i]) } else { ve_gone(l[i]) }
}
vehicles_st.ve_list = keep
}