SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 28/03/2020, 09:06 PM   #1
Adamoneoone
Huge Clucker
 
Join Date: Sep 2014
Posts: 489
Reputation: 37
Default Locate the closest specific vehicle

Hello everybody, i use this piece of code to get the closest vehicle to a player:
pawn Code:
stock GetClosestVehicle(playerid, Float:Range = 100.0)
{
    new ClosestVehicle;
    new Float:ClosestDist = 100000.0;
    new Float:DistX;
    new Float:DistY;
    new Float:DistZ;
    new Float:px;
    new Float:py;
    new Float:pz;
    new Float:CheckDist;

    // Get the players position
    GetPlayerPos(playerid, px, py, pz);

    // Loop through vehicles to find cloest vehicle
    for(new i = 0; i < MAX_VEHICLES; i++)
    {
        // Vehicle doesn't exist
        if(!GetVehicleModel(i)) continue;

        // Get vehicle position
        GetVehiclePos(i, DistX, DistY, DistZ);

        // Get the distance
        CheckDist = floatsqroot(
                    floatpower(floatsub(DistX, px), 2.0) +
                    floatpower(floatsub(DistY, py), 2.0) +
                    floatpower(floatsub(DistZ, pz), 2.0));

        // Distance less than range?
        if(CheckDist <= Range)
        {
            // Distance less than cloest distance, new cloest distance!
            if(CheckDist < ClosestDist)
            {
                ClosestDist = CheckDist;
                ClosestVehicle = i;
            }
        }
    }
    // Cloest less than or equal to the range return the closest vehicle ID
    if(ClosestDist <= Range) return ClosestVehicle;
    // no vehichle? return INVALID_VEHICLE_ID
    return INVALID_VEHICLE_ID;
}
, however i've tried to modifiy it to look for a specific model, but i can't get it to work
I've tried adding another argument "model" and check if(GetVehicleModel(i) != GetVehicleModel(model)) continue;
but it didn't seem to work... the returned position is 0,0,0
Adamoneoone is offline   Reply With Quote
Old 28/03/2020, 09:11 PM   #2
Symon
Gangsta
 
Symon's Avatar
 
Join Date: Apr 2019
Location: Italy
Posts: 915
Reputation: 89
Default Re: Locate the closest specific vehicle

There you go:

pawn Code:
stock GetClosestVehicleModel(playerid, id)
{
    new closest = -1, Float: closestDist = 8000.00, Float: distance, Float: pX, Float: pY, Float: pZ;
   
    GetPlayerPos(playerid, pX, pY, pZ);

    for(new i = 0; i < MAX_VEHICLES; i++) if(GetVehicleModel(i) == id)
    {
        distance = GetVehicleDistanceFromPoint(i, pX, pY, pZ);
           
        if(closestDist > distance)
        {
            closestDist = distance;
            closest = i;
        }
    }
    return closest;
}

pawn Code:
GetClosestVehicleModel(playerid, 411); //Get closest Infernus

I haven't made this. Credits to whoever did.
Symon is offline   Reply With Quote
Old 28/03/2020, 09:15 PM   #3
Adamoneoone
Huge Clucker
 
Join Date: Sep 2014
Posts: 489
Reputation: 37
Default Re: Locate the closest specific vehicle

Thanks a lot mate, works like a charm!
Adamoneoone is offline   Reply With Quote
Old 28/03/2020, 09:16 PM   #4
Symon
Gangsta
 
Symon's Avatar
 
Join Date: Apr 2019
Location: Italy
Posts: 915
Reputation: 89
Default Re: Locate the closest specific vehicle

No problem.
Symon is offline   Reply With Quote
Old 29/03/2020, 08:26 AM   #5
Calisthenics
High-roller
 
Join Date: May 2018
Posts: 1,227
Reputation: 205
Default Re: Locate the closest specific vehicle

If you already use YSI, you can take advantage of new iterators:
pawn Code:
for(new i = 0; i < MAX_VEHICLES; i++)
{
    // Vehicle doesn't exist
    if(!GetVehicleModel(i)) continue;
to
pawn Code:
foreach(new i : StreamedVehicle[playerid])
or if you do not, at least use GetVehiclePoolSize function.
__________________
Calisthenics is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Help] Locate vehicle Reattor Scripting Help 2 03/11/2018 06:52 PM
closest vehicle n00el Scripting Help 1 06/06/2014 06:04 AM
If you want to enter a specific vehicle ID, you need a specific skin davelord Scripting Help 9 07/08/2012 01:09 AM
Closest Vehicle Jo0ker Scripting Help 1 12/06/2012 12:38 PM
How can i spawn a specific player in a specific vehicle ? AlbanianGuy Help Archive 8 16/01/2010 07:31 AM


All times are GMT. The time now is 12:27 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.