Get Nearest Vehicle
#1

Im using vDealer car system, and it saves car pos in file 0.cfg, 1.cfg etc.

How can I perform check for closest vehicle to my position, for example you type /info, it checks closest vehicle to you, and it sends you a message with that vehicle name.

pawn Код:
name = my-car-custom-name
_z = position-z-here
_x = position-x-here
_y = position-y-here
this is what is used to save/load car:

pawn Код:
stock SaveVehicle(vehicleid)
{
    new veh = ConvertVID(vehicleid);
    new file[64];
    format(file,sizeof file,VEHICLE_PATH,veh);
    new INI:handler = INI_Open(file);
    INI_WriteString(handler,"_owner",PrivateVehicles[veh][_owner]);
    INI_WriteInt(handler,"_modelid",PrivateVehicles[veh][_modelid]);
    INI_WriteFloat(handler,"_x",PrivateVehicles[veh][_x]);
    INI_WriteFloat(handler,"_y",PrivateVehicles[veh][_y]);
    INI_WriteFloat(handler,"_z",PrivateVehicles[veh][_z]);
    INI_WriteFloat(handler,"_rot",PrivateVehicles[veh][_rot]);
    INI_WriteInt(handler,"_c1",PrivateVehicles[veh][_c1]);
    INI_WriteInt(handler,"_c2",PrivateVehicles[veh][_c2]);
    INI_WriteInt(handler,"_price",PrivateVehicles[veh][_price]);
    INI_WriteInt(handler,"_spawned",PrivateVehicles[veh][_spawned]);
    INI_WriteInt(handler,"_locked",PrivateVehicles[veh][_locked]);
    new Float:health2;
    PrivateVehicles[veh][_health] = health2;
    INI_WriteFloat(handler,"_health",PrivateVehicles[veh][_health]);
    INI_Close(handler);
    return 1;
}

stock LoadVehicles()
{
    new counter = 0;
    for(new i = 0;i<M_VEHICLES;i++)
    {
        new file[64];format(file,sizeof file,VEHICLE_PATH,i);
        if(fexist(file))
        {
            new INI:handler = INI_Open(file);
            INI_ParseFile(file,"LoadVehicle",.bExtra = true, .extra = i);
            PrivateVehicles[i][_vehicleid] = AddStaticVehicle(PrivateVehicles[i][_modelid],PrivateVehicles[i][_x],PrivateVehicles[i][_y],PrivateVehicles[i][_z],PrivateVehicles[i][_rot],PrivateVehicles[i][_c1],PrivateVehicles[i][_c2]);
            SetVehicleNumberPlate(PrivateVehicles[i][_vehicleid],PrivateVehicles[i][_owner]);
            SetVehicleToRespawn(PrivateVehicles[i][_vehicleid]);
            SetVehicleHealth(PrivateVehicles[i][_vehicleid],PrivateVehicles[i][_health]);
            SetVehicleParamsEx(PrivateVehicles[i][_vehicleid],engine,lights,alarm,PrivateVehicles[i][_locked],bonnet,boot,objective);
            INI_Close(handler);
            counter++;
        }
    }
    printf("vDealer: %i vehicle(s) loaded.",counter);
}

forward LoadVehicle(i,name[],value[]);
public LoadVehicle(i,name[],value[])
{
    if(!strcmp(name,"_owner")){format(PrivateVehicles[i][_owner],MAX_PLAYER_NAME,"%s",value);}
    INI_Int("_modelid",PrivateVehicles[i][_modelid]);
    INI_Float("_x",PrivateVehicles[i][_x]);
    INI_Float("_y",PrivateVehicles[i][_y]);
    INI_Float("_z",PrivateVehicles[i][_z]);
    INI_Float("_rot",PrivateVehicles[i][_rot]);
    INI_Int("_c1",PrivateVehicles[i][_c1]);
    INI_Int("_c2",PrivateVehicles[i][_c2]);
    INI_Int("_price",PrivateVehicles[i][_price]);
    INI_Int("_spawned",PrivateVehicles[i][_spawned]);
    INI_Int("_locked",PrivateVehicles[i][_locked]);
    INI_Float("_health",PrivateVehicles[i][_health]);
    return 1;
}

stock CreateVehicleDealer(dealername[],Float:x,Float:y,Float:z)
{
    new id = GenerateID(1);
    format(Dealers[id][_dealername],64,"%s",dealername);
    Dealers[id][_x] = x;
    Dealers[id][_y] = y;
    Dealers[id][_z] = z;
    Dealers[id][_active] = true;
    #if USE_CP == true
        Dealers[id][_pickupid] = CreateDynamicCP(x,y,z,CP_SIZE);
    #else
        Dealers[id][_pickupid] = CreateDynamicPickup(PICKUP_MODEL,23,x,y,z);
    #endif
    Dealers[id][_3dtextlabel] = CreateDynamic3DTextLabel(dealername,COLOR_YELLOW,x,y,z +1,STREAMER_DISTANCE);
    Dealers[id][_mapicon] = CreateDynamicMapIcon(x,y,z,MAP_ICON,0);
    return id;
}
Reply
#2

Go and learn the basics how to use loops and detect any vehicle position
Reply
#3

Quote:
Originally Posted by BigETI
Посмотреть сообщение
Go and learn the basics how to use loops and detect any vehicle position
Very hepful -.-

Use my function
pawn Код:
stock GetClosestVehicle(playerid, Float:range)
{
    new     Float:p_X;
    new     Float:p_Y;
    new     Float:p_Z;

    new     Float:Distance;
    new     Float:PretendentDistance = range +1;
    new     Pretendent;

    GetPlayerPos(playerid, p_X, p_Y, p_Z);

    for(new vehicleid=1; vehicleid < MAX_VEHICLES; vehicleid++)
    {
        Distance = GetVehicleDistanceFromPoint(vehicleid, p_X, p_Y, p_Z);

        if(Distance <= range && Distance <= PretendentDistance)
        {
            Pretendent = vehicleid;
            PretendentDistance = Distance;
        }
    }

    return Pretendent;
}
pawn Код:
new vehicleid;
vehicleid = GetClosestVehicle(playerid, 20);
This will find the nearest vehicle to you in radius of 20.
Reply
#4

Thx skorch, this is helpful when you get a code and can study it, just like a year a go when every scripter gave a help like this.

To: BigETI, If the thing you want to say isn't helpful and doesn't make sense, why bother?
Reply
#5

Right using your "stock" I made /lock command
pawn Код:
COMMAND:lock(playerid,params[])
{
    new pName[MAX_PLAYER_NAME];GetPlayerName(playerid,pName,sizeof pName);
    new vehicleid;
    vehicleid = GetClosestVehicle(playerid, 5);
    new veh = GetPlayerVehicleID(playerid);
    if(IsVehiclePrivate(veh))
    {
        if(strcmp(pName,PrivateVehicles[ConvertVID(veh)][_owner]))
        {
            SendClientMessage(playerid,COLOR_WHITE,"Closest vehicle doesn't belong to you!");
            return 1;
        }
        else
        {
            veh = ConvertVID(veh);
            PrivateVehicles[veh][_locked] = 0;
            SendClientMessage(playerid,COLOR_RED,"Car Locked!");
            SetVehicleParamsEx(vehicleid,engine,lights,alarm,0,bonnet,boot,objective);
        }
    }
    return 1;
}
But it doesn't work, It keeps coming up with Car Locked when you type it, It doesn' matter how far away from the car you are and if closest car is your car, it just comes up "Car Locked", any ideas?
Reply
#6

Try checking if any vehicle is in the range.
pawn Код:
if(vehicleid != 0) // if any vehicle is in range, returns 0 if theres no vehicle in range
and you use variable 'veh' in the most of the cmd.
Reply
#7

Quote:

To: BigETI, If the thing you want to say isn't helpful and doesn't make sense, why bother?

Looks like that you don't bother to learn anything since I see hundreds of threads by you with scripting questions.

If you think that you don't know anything at scripting and don't want to bother to learn it so go and hire a scripter instead of asking all the time on this forum.
Reply
#8

Quote:
Originally Posted by BigETI
Посмотреть сообщение
If you think that you don't know anything at scripting and don't want to bother to learn it so go and hire a scripter instead of asking all the time on this forum.
This subforum is meant to ask for help.
Quote:
Originally Posted by BigETI
Посмотреть сообщение
Looks like that you don't bother to learn anything since I see hundreds of threads by you with scripting questions.
That doesn't mean that he doesn't bother to learn anything. How can you be so sure?

Your first reply on this topic was completely useless, you can go to all topics where people ask some help in scripting questions, and answer: "Go learn basics.". So you should stop posting replies like that.
Reply
#9

Quote:
Originally Posted by Skorch
Посмотреть сообщение
This subforum is meant to ask for help.

That doesn't mean that he doesn't bother to learn anything. How can you be so sure?

Your first reply on this topic was completely useless, you can go to all topics where people ask some help in scripting questions, and answer: "Go learn basics.". So you should stop posting replies like that.
Stop acting like a smartass please.
I know that this sub forum is made for people with scripting problems but first of all I saw (go search on his/her profile) hundreds I MEAN hundreds of threads with questions related to PAWN scripts where you don't see any posts where he/she did learned from.

Seriosly in the world wide web there are several documents how to learn this scripting language and also SA:MP wiki does explain everything related to SA:MP.
Reply
#10

Thx skorch, I was busy watching manchester united, I did the command quickly, didnt work I didnt check it I posted it here. And during the match I was thinking that no way that command will work, Im gonna fix it now.

And BigETI, WHY? WHY bother? If you ain't going to help, stop making things more difficult! You want to get more posts in your account or something? Becuse they are useless and you are only making everything more difficult then it already is.

You dont want to help? Fine dont! But at least dont make it harder for does who do!

BigETI - Dont even reply to this, just read this post and go help somewhere else.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)