From 99cab6d0725f27ed2868e438b6d7af777c6296f6 Mon Sep 17 00:00:00 2001 From: Jag <77469789+jag3dagster@users.noreply.github.com> Date: Tue, 16 Aug 2022 00:34:49 -0700 Subject: [PATCH] feat(imports): getClosestVehicle and getNearbyVehicles (#71) --- imports/getClosestVehicle/client.lua | 27 ++++++++++++++++++++++++++ imports/getNearbyVehicles/client.lua | 29 ++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 imports/getClosestVehicle/client.lua create mode 100644 imports/getNearbyVehicles/client.lua diff --git a/imports/getClosestVehicle/client.lua b/imports/getClosestVehicle/client.lua new file mode 100644 index 0000000..4083b87 --- /dev/null +++ b/imports/getClosestVehicle/client.lua @@ -0,0 +1,27 @@ +---@param coords vector3 The coords to check from. +---@param maxDistance number The max distance to check. +---@param includePlayerVehicle boolean Whether or not to include the player's current vehicle. +---@return number? vehicle +---@return vector3? vehicleCoords +return function(coords, maxDistance, includePlayerVehicle) + local vehicles = GetGamePool('CVehicle') + local closestVehicle, closestCoords + maxDistance = maxDistance or 2.0 + + for i = 1, #vehicles do + local vehicle = vehicles[i] + + if not cache.vehicle or vehicle ~= cache.vehicle or includePlayerVehicle then + local vehicleCoords = GetEntityCoords(vehicle) + local distance = #(coords - vehicleCoords) + + if distance < maxDistance then + maxDistance = distance + closestVehicle = vehicle + closestCoords = vehicleCoords + end + end + end + + return closestVehicle, closestCoords +end \ No newline at end of file diff --git a/imports/getNearbyVehicles/client.lua b/imports/getNearbyVehicles/client.lua new file mode 100644 index 0000000..e1836f1 --- /dev/null +++ b/imports/getNearbyVehicles/client.lua @@ -0,0 +1,29 @@ +---@param coords vector3 The coords to check from. +---@param maxDistance number The max distance to check. +---@param includePlayerVehicle boolean Whether or not to include the player's current vehicle. +---@return number[] +return function(coords, maxDistance, includePlayerVehicle) + local vehicles = GetGamePool('CVehicle') + local nearby = {} + local count = 0 + maxDistance = maxDistance or 2.0 + + for i = 1, #vehicles do + local vehicle = vehicles[i] + + if not cache.vehicle or vehicle ~= cache.vehicle or includePlayerVehicle then + local vehicleCoords = GetEntityCoords(vehicle) + local distance = #(coords - vehicleCoords) + + if distance < maxDistance then + count += 1 + nearby[count] = { + vehicle = vehicle, + coords = coords + } + end + end + end + + return nearby +end \ No newline at end of file