Respawning vehicles within a certain range
#1

Hello there,

I made a command which its aim is to respawn all vehicles within a certain range. The range can be chosen by the administrator in the command itself. The problem is, it respawns every vehicle in the server and not just the ones within the chosen range.

Код:
CMD:range(playerid, params[])
{
	if(pInfo[playerid][pAdmin] < 3)
		return 0;
		
	new range, Float:x, Float:y, Float:z;
   	GetPlayerPos(playerid, x, y, z);
	
	if( sscanf(params, "f", range))
		return SendUsageError( playerid, "/range [Range Distance]" );

	for(new i = 1, j = GetVehiclePoolSize(); i <= j; i ++)
	{
	    if(!IsPlayerInRangeOfPoint(i, range, x, y, z))
	    {
			if(!IsValidVehicle(i))
			{
				continue;
			}

			if(spawnedcar[i])
			{
				DestroyVehicle(i);

				spawnedcar[i] = 0;
			}
			else
			{
				SetVehicleToRespawn(i);
			}
		}
	}
}
Help would be appreciated.

Thanks
Reply
#2

You are checking if the player is in range, but obviously you want to check for vehicles in range. SA-MP instead offers GetVehicleDistanceFromPoint to check if a vehicle is within range of a point.

pawn Код:
CMD:range(playerid, params[])
{
    if(pInfo[playerid][pAdmin] < 3)
        return 0;
   
    new Float: range;
    if( sscanf(params, "f", range) || range < 0.0)
        return SendUsageError( playerid, "/range [Range Distance]" );
   
    new Float:x, Float:y, Float:z;
    GetPlayerPos(playerid, x, y, z);

    for(new i = 1, j = GetVehiclePoolSize(); i <= j; i ++)
    {
        if(GetVehicleDistanceFromPoint(i, x, y, z) <= range)
        {
            if(!IsValidVehicle(i))
            {
                continue;
            }

            if(spawnedcar[i])
            {
                DestroyVehicle(i);

                spawnedcar[i] = 0;
            }
            else
            {
                SetVehicleToRespawn(i);
            }
        }
    }
}
Likewise, you can still do something like this if you prefer the old method:
pawn Код:
IsVehicleInRangeOfPoint(vehicleid, Float: radius, Float: x, Float: y, Float: z) {
    if(GetVehicleDistanceFromPoint(vehicleid, x, y, z) <= radius)
        return true;

    return false;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)