R3QQ

getClosestVehicleFromPedPos GTAV

Mar 18th, 2016
12,913
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. bool doesEntityExistsAndIsNotNull(Entity entity){
  2.     return (entity != NULL && ENTITY::DOES_ENTITY_EXIST(entity));
  3. }
  4.  
  5. float getDistanceBetweenEntities(Entity entity1, Entity entity2){
  6.     Vector3 entity1Coords = ENTITY::GET_ENTITY_COORDS(entity1, true);
  7.     Vector3 entity2Coords = ENTITY::GET_ENTITY_COORDS(entity2, true);
  8.     return GAMEPLAY::GET_DISTANCE_BETWEEN_COORDS(entity1Coords.x, entity1Coords.y, entity1Coords.z, entity2Coords.x, entity2Coords.y, entity2Coords.z, 1);
  9. }
  10.  
  11. float getDistanceToGround(Entity entity){
  12.     Vector3 entityCoords = ENTITY::GET_ENTITY_COORDS(entity, true);
  13.     float groundZ = 0;
  14.     GAMEPLAY::GET_GROUND_Z_FOR_3D_COORD(entityCoords.x, entityCoords.y, entityCoords.z, &groundZ, 0);
  15.     return GAMEPLAY::GET_DISTANCE_BETWEEN_COORDS(entityCoords.x, entityCoords.y, entityCoords.z, entityCoords.x, entityCoords.y, groundZ, 1);
  16. }
  17.  
  18. Hash getModelHash(Vehicle veh){
  19.     return ENTITY::GET_ENTITY_MODEL(veh);
  20. }
  21.  
  22. bool isVehicleDrivable(Vehicle veh){
  23.     if (VEHICLE::IS_VEHICLE_DRIVEABLE(veh, false) &&
  24.         (VEHICLE::IS_THIS_MODEL_A_CAR(getModelHash(veh)) ||
  25.         VEHICLE::IS_THIS_MODEL_A_BIKE(getModelHash(veh)) ||
  26.         VEHICLE::IS_THIS_MODEL_A_QUADBIKE(getModelHash(veh)) ||
  27.         //VEHICLE::IS_THIS_MODEL_A_TRAIN(getModel(veh)) || // use if you like to
  28.         VEHICLE::IS_THIS_MODEL_A_HELI(getModelHash(veh)) ||
  29.         //VEHICLE::_IS_THIS_MODEL_A_SUBMERSIBLE(getModel(veh)) || // doesn't seem to work
  30.         VEHICLE::IS_THIS_MODEL_A_PLANE(getModelHash(veh)) ||
  31.         VEHICLE::IS_THIS_MODEL_A_BOAT(getModelHash(veh)) ||
  32.         VEHICLE::IS_THIS_MODEL_A_BICYCLE(getModelHash(veh)))){
  33.         return true;
  34.     }
  35.  
  36.     return false;
  37. }
  38.  
  39. Vehicle getClosestVehicleFromPedPos(Ped ped, int maxDistance, int maxHeight, bool canReturnVehicleInside){
  40.     Vehicle veh = NULL;
  41.     float smallestDistance = (float)maxDistance;
  42.     const int ARR_SIZE = 1024;
  43.     Vehicle vehs[ARR_SIZE];
  44.     int count = worldGetAllVehicles(vehs, ARR_SIZE);
  45.  
  46.     if (vehs != NULL)
  47.     {
  48.         for (int i = 0; i < count; i++)
  49.         {
  50.             if (doesEntityExistsAndIsNotNull(vehs[i]) && (canReturnVehicleInside || PED::IS_PED_IN_VEHICLE(ped, vehs[i], false) == false))
  51.             {
  52.                 float distance = getDistanceBetweenEntities(ped, vehs[i]);
  53.                 float height = getDistanceToGround(vehs[i]);
  54.                 if (distance <= smallestDistance && height <= maxHeight && height >= 0 && isVehicleDrivable(vehs[i]))
  55.                 {
  56.                     smallestDistance = distance;
  57.                     veh = vehs[i];
  58.                 }
  59.             }
  60.         }
  61.     }
  62.  
  63.     return veh;
  64. }
Add Comment
Please, Sign In to add comment