From 55627129756a5a2dcd3cd2038cb8faa765019ce2 Mon Sep 17 00:00:00 2001 From: ElPumpo Date: Tue, 14 Apr 2020 13:08:10 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9A=A0=EF=B8=8F=E2=9A=A0=EF=B8=8FBREAKING=20?= =?UTF-8?q?COMMIT=E2=9A=A0=EF=B8=8F=E2=9A=A0=EF=B8=8F:=20Merged=20dupe=20c?= =?UTF-8?q?lient=20functions=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This changes arguments on the following functions: - ESX.Game.GetClosestObject() - ESX.Game.GetPeds(), no more arg for ignoreList - ESX.Game.GetClosestPed(), no longer a ignore list, instead a whitelist --- client/entityiter.lua | 6 +- client/functions.lua | 201 +++++------------------------------------- 2 files changed, 26 insertions(+), 181 deletions(-) diff --git a/client/entityiter.lua b/client/entityiter.lua index 20dd2018..c2e87406 100644 --- a/client/entityiter.lua +++ b/client/entityiter.lua @@ -45,11 +45,11 @@ local function EnumerateEntities(initFunc, moveFunc, disposeFunc) local enum = {handle = iter, destructor = disposeFunc} setmetatable(enum, entityEnumerator) - local next = true + repeat - coroutine.yield(id) - next, id = moveFunc(iter) + coroutine.yield(id) + next, id = moveFunc(iter) until not next enum.destructor, enum.handle = nil, nil diff --git a/client/functions.lua b/client/functions.lua index 177bf7f5..814a8c39 100644 --- a/client/functions.lua +++ b/client/functions.lua @@ -427,63 +427,9 @@ ESX.Game.IsVehicleEmpty = function(vehicle) return passengers == 0 and driverSeatFree end -ESX.Game.GetObjects = function() - local objects = {} - - for object in EnumerateObjects() do - table.insert(objects, object) - end - - return objects -end - -ESX.Game.GetClosestObject = function(filter, coords) - local objects = ESX.Game.GetObjects() - local closestDistance, closestObject = -1, -1 - local filter, coords = filter, coords - - if type(filter) == 'string' then - if filter ~= '' then - filter = {filter} - end - end - - if coords then - coords = vector3(coords.x, coords.y, coords.z) - else - local playerPed = PlayerPedId() - coords = GetEntityCoords(playerPed) - end - - for i=1, #objects, 1 do - local foundObject = false - - if filter == nil or (type(filter) == 'table' and #filter == 0) then - foundObject = true - else - local objectModel = GetEntityModel(objects[i]) - - for j=1, #filter, 1 do - if objectModel == GetHashKey(filter[j]) then - foundObject = true - break - end - end - end - - if foundObject then - local objectCoords = GetEntityCoords(objects[i]) - local distance = #(objectCoords - coords) - - if closestDistance == -1 or closestDistance > distance then - closestObject = objects[i] - closestDistance = distance - end - end - end - - return closestObject, closestDistance -end +ESX.Game.GetObjects = function() return EnumerateObjects() end +ESX.Game.GetPeds = function() return EnumeratePeds() end +ESX.Game.GetVehicles = function() return EnumerateVehicles() end ESX.Game.GetPlayers = function() local players = {} @@ -499,64 +445,16 @@ ESX.Game.GetPlayers = function() return players end -ESX.Game.GetClosestPlayer = function(coords) - local players, closestDistance, closestPlayer = ESX.Game.GetPlayers(), -1, -1 - local coords, usePlayerPed = coords, false - local playerPed, playerId = PlayerPedId(), PlayerId() +ESX.Game.GetClosestObject = function(coords, modelFilter) return ESX.Game.GetClosestEntity(EnumerateObjects(), coords, modelFilter) end +ESX.Game.GetClosestPed = function(coords, modelFilter) return ESX.Game.GetClosestEntity(EnumeratePeds(), coords, modelFilter) end +ESX.Game.GetClosestPlayer = function(coords, modelFilter) return ESX.Game.GetClosestEntity(ESX.Game.GetPlayers(), coords, modelFilter) end +ESX.Game.GetClosestVehicle = function(coords, modelFilter) return ESX.Game.GetClosestEntity(EnumerateVehicles(), coords, modelFilter) end +ESX.Game.GetPlayersInArea = function(coords, area) return enumerateInArea(ESX.Game.GetPlayers(), coords, area) end +ESX.Game.GetVehiclesInArea = function(coords, area) return enumerateInArea(ESX.Game.GetVehicles(), coords, area) end +ESX.Game.IsSpawnPointClear = function(coords, radius) return #ESX.Game.GetVehiclesInArea(coords, radius) == 0 end - if coords then - coords = vector3(coords.x, coords.y, coords.z) - else - usePlayerPed = true - coords = GetEntityCoords(playerPed) - end - - for i=1, #players, 1 do - local target = GetPlayerPed(players[i]) - - if not usePlayerPed or (usePlayerPed and players[i] ~= playerId) then - local targetCoords = GetEntityCoords(target) - local distance = #(coords - targetCoords) - - if closestDistance == -1 or closestDistance > distance then - closestPlayer = players[i] - closestDistance = distance - end - end - end - - return closestPlayer, closestDistance -end - -ESX.Game.GetPlayersInArea = function(coords, area) - local players, playersInArea = ESX.Game.GetPlayers(), {} - coords = vector3(coords.x, coords.y, coords.z) - - for i=1, #players, 1 do - local target = GetPlayerPed(players[i]) - local targetCoords = GetEntityCoords(target) - - if #(coords - targetCoords) <= area then - table.insert(playersInArea, players[i]) - end - end - - return playersInArea -end - -ESX.Game.GetVehicles = function() - local vehicles = {} - - for vehicle in EnumerateVehicles() do - table.insert(vehicles, vehicle) - end - - return vehicles -end - -ESX.Game.GetClosestVehicle = function(coords) - local vehicles = ESX.Game.GetVehicles() - local closestDistance, closestVehicle, coords = -1, -1, coords +ESX.Game.GetClosestEntity = function(entities, coords, modelFilter) + local closestEntity, closestEntityDistance, filteredEntities = -1, -1, nil if coords then coords = vector3(coords.x, coords.y, coords.z) @@ -565,32 +463,25 @@ ESX.Game.GetClosestVehicle = function(coords) coords = GetEntityCoords(playerPed) end - for i=1, #vehicles, 1 do - local vehicleCoords = GetEntityCoords(vehicles[i]) - local distance = #(coords - vehicleCoords) + if modelFilter then + filteredEntities = {} - if closestDistance == -1 or closestDistance > distance then - closestVehicle, closestDistance = vehicles[i], distance + for k,entity in ipairs(entities) do + if modelFilter[GetEntityModel(entity)] then + table.insert(filteredEntities, entity) + end end end - return closestVehicle, closestDistance -end + for k,entity in ipairs(filteredEntities or entities) do + local distance = #(coords - GetEntityCoords(entity)) -ESX.Game.GetVehiclesInArea = function(coords, area) - local vehicles = ESX.Game.GetVehicles() - local vehiclesInArea = {} - - for i=1, #vehicles, 1 do - local vehicleCoords = GetEntityCoords(vehicles[i]) - local distance = GetDistanceBetweenCoords(vehicleCoords, coords.x, coords.y, coords.z, true) - - if distance <= area then - table.insert(vehiclesInArea, vehicles[i]) + if closestEntityDistance == -1 or distance < closestEntityDistance then + closestEntity, closestEntityDistance = entity, distance end end - return vehiclesInArea + return closestEntity, closestEntityDistance end ESX.Game.GetVehicleInDirection = function() @@ -607,52 +498,6 @@ ESX.Game.GetVehicleInDirection = function() return nil end -ESX.Game.IsSpawnPointClear = function(coords, radius) - local vehicles = ESX.Game.GetVehiclesInArea(coords, radius) - - return #vehicles == 0 -end - -ESX.Game.GetPeds = function(ignoreList) - local ignoreList = ignoreList or {} - local peds = {} - - for ped in EnumeratePeds() do - local found = false - - for j=1, #ignoreList, 1 do - if ignoreList[j] == ped then - found = true - end - end - - if not found then - table.insert(peds, ped) - end - end - - return peds -end - -ESX.Game.GetClosestPed = function(coords, ignoreList) - local ignoreList = ignoreList or {} - local peds = ESX.Game.GetPeds(ignoreList) - local closestDistance = -1 - local closestPed = -1 - - for i=1, #peds, 1 do - local pedCoords = GetEntityCoords(peds[i]) - local distance = GetDistanceBetweenCoords(pedCoords, coords.x, coords.y, coords.z, true) - - if closestDistance == -1 or closestDistance > distance then - closestPed = peds[i] - closestDistance = distance - end - end - - return closestPed, closestDistance -end - ESX.Game.GetVehicleProperties = function(vehicle) if DoesEntityExist(vehicle) then local colorPrimary, colorSecondary = GetVehicleColours(vehicle)