PDA

View Full Version : Get Nearest Vehicle


HondaCBR
31/12/2011, 11:53 AM
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.


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:


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;
}

BigETI
31/12/2011, 12:21 PM
Go and learn the basics how to use loops and detect any vehicle position

WooTFTW
31/12/2011, 12:35 PM
Go and learn the basics how to use loops and detect any vehicle position
Very hepful -.-

Use my function

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;
}



new vehicleid;
vehicleid = GetClosestVehicle(playerid, 20);


This will find the nearest vehicle to you in radius of 20.

HondaCBR
31/12/2011, 12:40 PM
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?

HondaCBR
31/12/2011, 01:01 PM
Right using your "stock" I made /lock command

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?

WooTFTW
31/12/2011, 01:10 PM
Try checking if any vehicle is in the range.

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.

BigETI
31/12/2011, 01:19 PM
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.

WooTFTW
31/12/2011, 01:28 PM
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.
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.

BigETI
31/12/2011, 01:38 PM
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.

HondaCBR
31/12/2011, 01:47 PM
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.

Incognation
31/12/2011, 02:02 PM
Dude, be normal and let him ask questions. Please do not act like you can everything.

HondaCBR
31/12/2011, 02:29 PM
Right I did what I could, it comes up with "There is no vehicle near you" When Im close and far away.


COMMAND:lock(playerid,params[])
{
new pName[MAX_PLAYER_NAME];GetPlayerName(playerid,pName,sizeof pName);
new vehicleid;
vehicleid = GetClosestVehicle(playerid, 20);
new veh = GetPlayerVehicleID(vehicleid);
if(vehicleid != 0)
{
SendClientMessage(playerid,COLOR_WHITE,"There is no vehicle near you!");
return 0;
}
if(strcmp(pName,PrivateVehicles[ConvertVID(veh)][_owner]))
{
SendClientMessage(playerid,COLOR_WHITE,"Closest vehicle doesn't belong to you!");
return 0;
}
veh = ConvertVID(veh);
PrivateVehicles[veh][_locked] = 0;
SendClientMessage(playerid,COLOR_RED,"Car Locked!");
SetVehicleParamsEx(veh,engine,lights,alarm,0,bonne t,boot,objective);
SaveVehicle2(PrivateVehicles[veh][_vehicleid]);
return 1;
}

What hava I missed out/did wrong?

ev0lution
31/12/2011, 02:34 PM
I was busy watching manchester unitedThere's your problem.

WooTFTW
31/12/2011, 02:59 PM
COMMAND:lock(playerid,params[])
{
new pName[MAX_PLAYER_NAME];GetPlayerName(playerid,pName,sizeof pName);
new vehicleid;
vehicleid = GetClosestVehicle(playerid, 20);
if(vehicleid == 0)
{
SendClientMessage(playerid,COLOR_WHITE,"There is no vehicle near you!");
return 0;
}
if(strcmp(pName,PrivateVehicles[ConvertVID(vehicleid)][_owner]))
{
SendClientMessage(playerid,COLOR_WHITE,"Closest vehicle doesn't belong to you!");
return 0;
}
vehicleid = ConvertVID(vehicleid);
PrivateVehicles[vehicleid][_locked] = 0;
SendClientMessage(playerid,COLOR_RED,"Car Locked!");
SetVehicleParamsEx(vehicleid,engine,lights,alarm,0 ,bonnet,boot,objective);
SaveVehicle2(PrivateVehicles[vehicleid][_vehicleid]);
return 1;
}

HondaCBR
31/12/2011, 02:59 PM
Ill try it now.

HondaCBR
31/12/2011, 03:36 PM
It doesnt work for some reason, I get a click sound just like I would with an unknown command.

Does are the only 3 commands I have in this script, all work apart from /lock:

COMMAND:lock(playerid,params[])
{
new pName[MAX_PLAYER_NAME];GetPlayerName(playerid,pName,sizeof pName);
new vehicleid;
vehicleid = GetClosestVehicle(playerid, 20);
if(vehicleid == 0)
{
SendClientMessage(playerid,COLOR_WHITE,"There is no vehicle near you!");
return 1;
}
if(strcmp(pName,PrivateVehicles[ConvertVID(vehicleid)][_owner]))
{
SendClientMessage(playerid,COLOR_WHITE,"Closest vehicle doesn't belong to you!");
return 1;
}
vehicleid = ConvertVID(vehicleid);
PrivateVehicles[vehicleid][_locked] = 0;
SendClientMessage(playerid,COLOR_RED,"Car Locked!");
SetVehicleParamsEx(vehicleid,engine,lights,alarm,0 ,bonnet,boot,objective);
SaveVehicle2(PrivateVehicles[vehicleid][_vehicleid]);
return 1;
}

COMMAND:park(playerid,params[])
{
if(!IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playerid,COLOR_RED,"You are not in a vehicle!");
new veh = GetPlayerVehicleID(playerid),Float:x,Float:y,Float :z,Float:rot;
if(!IsVehiclePrivate(veh)) return SendClientMessage(playerid,COLOR_RED,"You are not in a private vehicle!");
new pName[MAX_PLAYER_NAME];GetPlayerName(playerid,pName,sizeof pName);
GetVehiclePos(veh,x,y,z);GetVehicleZAngle(veh,rot) ;

if(strcmp(pName,PrivateVehicles[ConvertVID(veh)][_owner])) return SendClientMessage(playerid,COLOR_RED,"You are not the owner of this vehicle!");


veh = ConvertVID(veh);

GetVehicleParamsEx(veh,engine,lights,alarm,doors,b onnet,boot,objective);
new Float:health2;
GetVehicleHealth(veh, health2);

PrivateVehicles[veh][_x] = x;
PrivateVehicles[veh][_y] = y;
PrivateVehicles[veh][_z] = z;
PrivateVehicles[veh][_rot] = rot;
PrivateVehicles[veh][_spawned] = 1;
PrivateVehicles[veh][_locked] = doors;
PrivateVehicles[veh][_health] = health2;

SaveVehicle(PrivateVehicles[veh][_vehicleid]);
SendClientMessage(playerid,COLOR_YELLOW,"You've succesfully parked your vehicle.");
return 1;
}

COMMAND:sellvehicle(playerid,params)
{
if(!IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playerid,COLOR_RED,"You are not in a vehicle!");
new veh = GetPlayerVehicleID(playerid);
if(!IsVehiclePrivate(veh)) return SendClientMessage(playerid,COLOR_RED,"You are not in a private vehicle!");
new pName[MAX_PLAYER_NAME];GetPlayerName(playerid,pName,sizeof pName);if(strcmp(pName,PrivateVehicles[ConvertVID(veh)][_owner])) return SendClientMessage(playerid,COLOR_RED,"You are not the owner of this vehicle!");

new str[93];format(str,sizeof str,"{FFFF00}Are you sure you want to sell your vehicle ? You will receive {FFFFFF}$%i",(PrivateVehicles[ConvertVID(veh)][_price] / SELL_DIVIDE));
ShowPlayerDialog(playerid,DIALOG_SELL,0,"Selling your vehicle",str,"Yes","no");
return 1;
}

HondaCBR
31/12/2011, 04:04 PM
Just for a test I tried:


COMMAND:lock(playerid,params[])
{
new pName[MAX_PLAYER_NAME];GetPlayerName(playerid,pName,sizeof pName);
new vehicleid;
vehicleid = GetClosestVehicle(playerid, 20);
if(vehicleid == 0)
{
SendClientMessage(playerid,COLOR_WHITE,"There is no vehicle near you!");
return 1;
}
}


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;
}


This bit doesnt work, I dont get that message when Im close or far away from any cars.