Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.29 KB | None | 0 0
  1. -- Here's a dump of my utility scripts
  2.  
  3. local entityEnumerator = {
  4. __gc = function(enum)
  5. if enum.destructor and enum.handle then
  6. enum.destructor(enum.handle)
  7. end
  8. enum.destructor = nil
  9. enum.handle = nil
  10. end
  11. }
  12.  
  13. local function EnumerateEntities(initFunc, moveFunc, disposeFunc)
  14. return coroutine.wrap(function()
  15. local iter, id = initFunc()
  16. if not id or id == 0 then
  17. disposeFunc(iter)
  18. return
  19. end
  20.  
  21. local enum = {handle = iter, destructor = disposeFunc}
  22. setmetatable(enum, entityEnumerator)
  23.  
  24. local next = true
  25. repeat
  26. coroutine.yield(id)
  27. next, id = moveFunc(iter)
  28. until not next
  29.  
  30. enum.destructor, enum.handle = nil, nil
  31. disposeFunc(iter)
  32. end)
  33. end
  34.  
  35. function EnumerateObjects()
  36. return EnumerateEntities(FindFirstObject, FindNextObject, EndFindObject)
  37. end
  38.  
  39. function EnumeratePeds()
  40. return EnumerateEntities(FindFirstPed, FindNextPed, EndFindPed)
  41. end
  42.  
  43. function EnumerateVehicles()
  44. return EnumerateEntities(FindFirstVehicle, FindNextVehicle, EndFindVehicle)
  45. end
  46.  
  47. function EnumeratePickups()
  48. return EnumerateEntities(FindFirstPickup, FindNextPickup, EndFindPickup)
  49. end
  50.  
  51.  
  52. GetObjects = function()
  53.  
  54. local objects = {}
  55.  
  56. for object in EnumerateObjects() do
  57. table.insert(objects, object)
  58. end
  59.  
  60. return objects
  61.  
  62. end
  63.  
  64. GetClosestObject = function(filter, coords)
  65.  
  66. local objects = GetObjects()
  67. local closestDistance = -1
  68. local closestObject = -1
  69. local filter = filter
  70. local coords = coords
  71.  
  72. if type(filter) == 'string' then
  73. if filter ~= '' then
  74. filter = {filter}
  75. end
  76. end
  77.  
  78. if coords == nil then
  79. local playerPed = GetPlayerPed(-1)
  80. coords = GetEntityCoords(playerPed)
  81. end
  82.  
  83. for i=1, #objects, 1 do
  84.  
  85. local foundObject = false
  86.  
  87. if filter == nil or (type(filter) == 'table' and #filter == 0) then
  88. foundObject = true
  89. else
  90.  
  91. local objectModel = GetEntityModel(objects[i])
  92.  
  93. for j=1, #filter, 1 do
  94. if objectModel == GetHashKey(filter[j]) then
  95. foundObject = true
  96. end
  97. end
  98.  
  99. end
  100.  
  101. if foundObject then
  102.  
  103. local objectCoords = GetEntityCoords(objects[i])
  104. local distance = GetDistanceBetweenCoords(objectCoords.x, objectCoords.y, objectCoords.z, coords.x, coords.y, coords.z, true)
  105.  
  106. if closestDistance == -1 or closestDistance > distance then
  107. closestObject = objects[i]
  108. closestDistance = distance
  109. end
  110.  
  111. end
  112.  
  113. end
  114.  
  115. return closestObject, closestDistance
  116.  
  117. end
  118.  
  119. GetPlayers = function()
  120.  
  121. local maxPlayers = Config.MaxPlayers
  122. local players = {}
  123.  
  124. for i=0, maxPlayers, 1 do
  125.  
  126. local ped = GetPlayerPed(i)
  127.  
  128. if DoesEntityExist(ped) then
  129. table.insert(players, i)
  130. end
  131. end
  132.  
  133. return players
  134.  
  135. end
  136.  
  137. GetClosestPlayer = function(coords)
  138.  
  139. local players = GetPlayers()
  140. local closestDistance = -1
  141. local closestPlayer = -1
  142. local coords = coords
  143. local usePlayerPed = false
  144. local playerPed = GetPlayerPed(-1)
  145. local playerId = PlayerId()
  146.  
  147. if coords == nil then
  148. usePlayerPed = true
  149. coords = GetEntityCoords(playerPed)
  150. end
  151.  
  152. for i=1, #players, 1 do
  153.  
  154. local target = GetPlayerPed(players[i])
  155.  
  156. if not usePlayerPed or (usePlayerPed and players[i] ~= playerId) then
  157.  
  158. local targetCoords = GetEntityCoords(target)
  159. local distance = GetDistanceBetweenCoords(targetCoords.x, targetCoords.y, targetCoords.z, coords.x, coords.y, coords.z, true)
  160.  
  161. if closestDistance == -1 or closestDistance > distance then
  162. closestPlayer = players[i]
  163. closestDistance = distance
  164. end
  165.  
  166. end
  167.  
  168. end
  169.  
  170. return closestPlayer, closestDistance
  171. end
  172.  
  173. GetPlayersInArea = function(coords, area)
  174.  
  175. local players = GetPlayers()
  176. local playersInArea = {}
  177.  
  178. for i=1, #players, 1 do
  179.  
  180. local target = GetPlayerPed(players[i])
  181. local targetCoords = GetEntityCoords(target)
  182. local distance = GetDistanceBetweenCoords(targetCoords.x, targetCoords.y, targetCoords.z, coords.x, coords.y, coords.z, true)
  183.  
  184. if distance <= area then
  185. table.insert(playersInArea, players[i])
  186. end
  187.  
  188. end
  189.  
  190. return playersInArea
  191. end
  192.  
  193. GetVehicles = function()
  194.  
  195. local vehicles = {}
  196.  
  197. for vehicle in EnumerateVehicles() do
  198. table.insert(vehicles, vehicle)
  199. end
  200.  
  201. return vehicles
  202.  
  203. end
  204.  
  205. GetClosestVehicle = function(coords)
  206.  
  207. local vehicles = GetVehicles()
  208. local closestDistance = -1
  209. local closestVehicle = -1
  210. local coords = coords
  211.  
  212. if coords == nil then
  213. local playerPed = GetPlayerPed(-1)
  214. coords = GetEntityCoords(playerPed)
  215. end
  216.  
  217. for i=1, #vehicles, 1 do
  218.  
  219. local vehicleCoords = GetEntityCoords(vehicles[i])
  220. local distance = GetDistanceBetweenCoords(vehicleCoords.x, vehicleCoords.y, vehicleCoords.z, coords.x, coords.y, coords.z, true)
  221.  
  222. if closestDistance == -1 or closestDistance > distance then
  223. closestVehicle = vehicles[i]
  224. closestDistance = distance
  225. end
  226.  
  227. end
  228.  
  229. return closestVehicle, closestDistance
  230.  
  231. end
  232.  
  233. GetVehiclesInArea = function(coords, area)
  234.  
  235. local vehicles = GetVehicles()
  236. local vehiclesInArea = {}
  237.  
  238. for i=1, #vehicles, 1 do
  239.  
  240. local vehicleCoords = GetEntityCoords(vehicles[i])
  241. local distance = GetDistanceBetweenCoords(vehicleCoords.x, vehicleCoords.y, vehicleCoords.z, coords.x, coords.y, coords.z, true)
  242.  
  243. if distance <= area then
  244. table.insert(vehiclesInArea, vehicles[i])
  245. end
  246.  
  247. end
  248.  
  249. return vehiclesInArea
  250. end
  251.  
  252. GetPeds = function(ignoreList)
  253.  
  254. local ignoreList = ignoreList or {}
  255. local peds = {}
  256.  
  257. for ped in EnumeratePeds() do
  258.  
  259. local found = false
  260.  
  261. for j=1, #ignoreList, 1 do
  262. if ignoreList[j] == ped then
  263. found = true
  264. end
  265. end
  266.  
  267. if not found then
  268. table.insert(peds, ped)
  269. end
  270.  
  271. end
  272.  
  273. return peds
  274.  
  275. end
  276.  
  277. GetClosestPed = function(coords, ignoreList)
  278.  
  279. local ignoreList = ignoreList or {}
  280. local peds = GetPeds(ignoreList)
  281. local closestDistance = -1
  282. local closestPed = -1
  283.  
  284. for i=1, #peds, 1 do
  285.  
  286. local pedCoords = GetEntityCoords(peds[i])
  287. local distance = GetDistanceBetweenCoords(pedCoords.x, pedCoords.y, pedCoords.z, coords.x, coords.y, coords.z, true)
  288.  
  289. if closestDistance == -1 or closestDistance > distance then
  290. closestPed = peds[i]
  291. closestDistance = distance
  292. end
  293.  
  294. end
  295.  
  296. return closestPed, closestDistance
  297.  
  298. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement