SA-MP Forums Archive
Respawning vehicles within a certain range - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Respawning vehicles within a certain range (/showthread.php?tid=603235)



Respawning vehicles within a certain range - FunnyBear - 20.03.2016

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


Re: Respawning vehicles within a certain range - Abagail - 20.03.2016

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