SA-MP Forums Archive
About GetClosestPlayer - 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: About GetClosestPlayer (/showthread.php?tid=450620)



About GetClosestPlayer - xeon_inside - 13.07.2013

I want that the Npc check if there's a player of X meters and if the player if near of X meters the NPC will follow him

Here's the code

Код:
forward FollowPlayer(playerid, npcindex);
public FollowPlayer(playerid,npcindex)
{
        for(new i; i < MAX_NPC;i++)
        {
         
		new Float:x, Float:y, Float:z;
        	new Float:nx, Float:ny, Float:nz;
        	GetPlayerPos(playerid, x, y, z);
        	FCNPC_GetPosition(NPCID[i], nx, ny, nz);
        	if(IsPlayerInRangeOfPoint(playerid, 2.0, nx, ny, nz))
        	{
                FCNPC_Stop(NPCID[i]);
                FCNPC_AimAt(NPCID[i], x, y, z, 1);
        	}
        else FCNPC_GoTo(NPCID[i], x, y, z, MOVE_TYPE_RUN, 0.0, 3);
		}
		return 1;
}
And Here's the GetClosestPlayer and GetDistanceBetweenPoints

Код:
function Float:GetDistanceBetweenPoints(Float:rx1,Float:ry1,Float:rz1,Float:rx2,Float:ry2,Float:rz2)
{
    return floatadd(floatadd(floatsqroot(floatpower(floatsub(rx1,rx2),2)),floatsqroot(floatpower(floatsub(ry1,ry2),2))),floatsqroot(floatpower(floatsub(rz1,rz2),2)));
}
Код:
stock GetClosestPlayer(playerid,Float:limit)
{
    new Float:x1, Float:y1, Float:z1, Float:x2, Float:y2, Float:z2;
    GetPlayerPos(playerid,x1,y1,z1);
    new Float:Range = 999.9;
    new id = -1;
    for(new i; i < MAX_PLAYERS;i++)
    {
        if(!IsPlayerConnected(i)) continue;
        if(playerid != i)
        {
            GetPlayerPos(i,x2,y2,z2);
            new Float:Dist = GetDistanceBetweenPoints(x1,y1,z1,x2,y2,z2);
            if(floatcmp(Range,Dist) == 1 && floatcmp(limit,Range) == 1)
            {
                Range = Dist;
                id = i;
            }
        }
    }
    return id;
}