Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Here's a dump of my utility scripts
- local entityEnumerator = {
- __gc = function(enum)
- if enum.destructor and enum.handle then
- enum.destructor(enum.handle)
- end
- enum.destructor = nil
- enum.handle = nil
- end
- }
- local function EnumerateEntities(initFunc, moveFunc, disposeFunc)
- return coroutine.wrap(function()
- local iter, id = initFunc()
- if not id or id == 0 then
- disposeFunc(iter)
- return
- end
- local enum = {handle = iter, destructor = disposeFunc}
- setmetatable(enum, entityEnumerator)
- local next = true
- repeat
- coroutine.yield(id)
- next, id = moveFunc(iter)
- until not next
- enum.destructor, enum.handle = nil, nil
- disposeFunc(iter)
- end)
- end
- function EnumerateObjects()
- return EnumerateEntities(FindFirstObject, FindNextObject, EndFindObject)
- end
- function EnumeratePeds()
- return EnumerateEntities(FindFirstPed, FindNextPed, EndFindPed)
- end
- function EnumerateVehicles()
- return EnumerateEntities(FindFirstVehicle, FindNextVehicle, EndFindVehicle)
- end
- function EnumeratePickups()
- return EnumerateEntities(FindFirstPickup, FindNextPickup, EndFindPickup)
- end
- GetObjects = function()
- local objects = {}
- for object in EnumerateObjects() do
- table.insert(objects, object)
- end
- return objects
- end
- GetClosestObject = function(filter, coords)
- local objects = GetObjects()
- local closestDistance = -1
- local closestObject = -1
- local filter = filter
- local coords = coords
- if type(filter) == 'string' then
- if filter ~= '' then
- filter = {filter}
- end
- end
- if coords == nil then
- local playerPed = GetPlayerPed(-1)
- 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
- end
- end
- end
- if foundObject then
- local objectCoords = GetEntityCoords(objects[i])
- local distance = GetDistanceBetweenCoords(objectCoords.x, objectCoords.y, objectCoords.z, coords.x, coords.y, coords.z, true)
- if closestDistance == -1 or closestDistance > distance then
- closestObject = objects[i]
- closestDistance = distance
- end
- end
- end
- return closestObject, closestDistance
- end
- GetPlayers = function()
- local maxPlayers = Config.MaxPlayers
- local players = {}
- for i=0, maxPlayers, 1 do
- local ped = GetPlayerPed(i)
- if DoesEntityExist(ped) then
- table.insert(players, i)
- end
- end
- return players
- end
- GetClosestPlayer = function(coords)
- local players = GetPlayers()
- local closestDistance = -1
- local closestPlayer = -1
- local coords = coords
- local usePlayerPed = false
- local playerPed = GetPlayerPed(-1)
- local playerId = PlayerId()
- if coords == nil then
- 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 = GetDistanceBetweenCoords(targetCoords.x, targetCoords.y, targetCoords.z, coords.x, coords.y, coords.z, true)
- if closestDistance == -1 or closestDistance > distance then
- closestPlayer = players[i]
- closestDistance = distance
- end
- end
- end
- return closestPlayer, closestDistance
- end
- GetPlayersInArea = function(coords, area)
- local players = GetPlayers()
- local playersInArea = {}
- for i=1, #players, 1 do
- local target = GetPlayerPed(players[i])
- local targetCoords = GetEntityCoords(target)
- local distance = GetDistanceBetweenCoords(targetCoords.x, targetCoords.y, targetCoords.z, coords.x, coords.y, coords.z, true)
- if distance <= area then
- table.insert(playersInArea, players[i])
- end
- end
- return playersInArea
- end
- GetVehicles = function()
- local vehicles = {}
- for vehicle in EnumerateVehicles() do
- table.insert(vehicles, vehicle)
- end
- return vehicles
- end
- GetClosestVehicle = function(coords)
- local vehicles = GetVehicles()
- local closestDistance = -1
- local closestVehicle = -1
- local coords = coords
- if coords == nil then
- local playerPed = GetPlayerPed(-1)
- coords = GetEntityCoords(playerPed)
- end
- for i=1, #vehicles, 1 do
- local vehicleCoords = GetEntityCoords(vehicles[i])
- local distance = GetDistanceBetweenCoords(vehicleCoords.x, vehicleCoords.y, vehicleCoords.z, coords.x, coords.y, coords.z, true)
- if closestDistance == -1 or closestDistance > distance then
- closestVehicle = vehicles[i]
- closestDistance = distance
- end
- end
- return closestVehicle, closestDistance
- end
- GetVehiclesInArea = function(coords, area)
- local vehicles = GetVehicles()
- local vehiclesInArea = {}
- for i=1, #vehicles, 1 do
- local vehicleCoords = GetEntityCoords(vehicles[i])
- local distance = GetDistanceBetweenCoords(vehicleCoords.x, vehicleCoords.y, vehicleCoords.z, coords.x, coords.y, coords.z, true)
- if distance <= area then
- table.insert(vehiclesInArea, vehicles[i])
- end
- end
- return vehiclesInArea
- end
- 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
- GetClosestPed = function(coords, ignoreList)
- local ignoreList = ignoreList or {}
- local peds = GetPeds(ignoreList)
- local closestDistance = -1
- local closestPed = -1
- for i=1, #peds, 1 do
- local pedCoords = GetEntityCoords(peds[i])
- local distance = GetDistanceBetweenCoords(pedCoords.x, pedCoords.y, pedCoords.z, 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement