21.06.2015, 19:09
I would do it like this:
pawn Код:
// [ DEVELOPMENT GAMEMODE ]
// INCLUDES:
#include <a_samp>
#include <zcmd>
// NATIVES:
native IsValidVehicle(vehicleid);
// MAIN:
main()
{
print("Development Mode: closest_vehicle_to_player.amx");
}
// CALLBACKS:
public OnGameModeInit()
{
return 1;
}
public OnGameModeExit()
{
return 1;
}
// COMMANDS:
CMD:closestvehicle(playerid, params[])
{
new string[144], closest_vehicle = GetClosestVehicleToPlayer(playerid);
if(closest_vehicle == -1)
{
strcat(string, "No vehicles have been found.");
SendClientMessage(playerid, -1, string);
}
else
{
new Float:x, Float:y, Float:z, Float:distance;
GetVehiclePos(closest_vehicle, x, y, z);
distance = GetPlayerDistanceFromPoint(playerid, x, y, z);
format(string, sizeof(string), "Vehicle with the ID %d is the closest vehicle to you (%0.2f meters).", closest_vehicle, distance);
SendClientMessage(playerid, -1, string);
}
return 1;
}
// FUNCTIONS:
stock GetClosestVehicleToPlayer(playerid)
{
new Float:x, Float:y, Float:z, closest_vehicle = -1, Float:distance, Float:temp;
new pool = GetVehiclePoolSize();
for(new i = 1; i <= pool; i ++)
{
if(!IsValidVehicle(i)) continue;
GetVehiclePos(i, x, y, z);
if(closest_vehicle != -1)
{
temp = GetPlayerDistanceFromPoint(playerid, x, y, z);
if(temp < distance)
{
closest_vehicle = i;
distance = temp;
}
}
else
{
closest_vehicle = i;
distance = GetPlayerDistanceFromPoint(playerid, x, y, z);
}
}
return closest_vehicle;
}