SA-MP Forums Archive
2 stock help - 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: 2 stock help (/showthread.php?tid=357413)



2 stock help - Chris1337 - 07.07.2012

Everytime i use this stock (i made a test debug command) appears always ID 0

how to make it not return id 0

pawn Код:
stock nearestwanted(playerid)
{
    new Float:x, Float:y, Float:z;
    new ii;
    GetPlayerPos(playerid, x, y, z);
    for( new i = 0; i < MAX_PLAYERS; i++ )
    {
        if (IsPlayerInRangeOfPoint(playerid, 5.0, x, y, z) && GetPlayerWantedLevel(i) >= 3)
        {
            printf("Player %d is The Nearest Wanted Level", i);
            ii = i;
            return ii;
        }
        else
        {
            print("No Wanted Level Near");
            ii = 0;
            return ii;
        }
    }
    return -1;
}

stock nearestrobbable(playerid)
{
    new Float:x, Float:y, Float:z;
    new ii;
    GetPlayerPos(playerid, x, y, z);
    for( new i = 0; i < MAX_PLAYERS; i++ )
    {
        if (IsPlayerInRangeOfPoint(playerid, 5.0, x, y, z))
        {
            printf("Player %d is The Nearest Robbable Player", i);
            if(i = playerid) return 1;
            else
            {
                ii = i;
                return ii;
            }
        }
        else
        {
            print("No Robbable player near");
            ii = 0;
            return ii;
        }
    }
    return -1;
}



Re: 2 stock help - [MM]RoXoR[FS] - 07.07.2012

It wont show you nearest player as you return from loop before the loop is completed

Use this UNTESTED
pawn Код:
stock Float:GetDistance(Float:x1,Float:y1,Float:z1,Float:x2,Float:y2,Float:z2) //By Gabriel "Larcius" Cordes
{
    return floatadd(floatadd(floatsqroot(floatpower(floatsub(x1,x2),2)),floatsqroot(floatpower(floatsub(y1,y2),2))),floatsqroot(floatpower(floatsub(z1,z2),2)));
}

stock nearestwanted(playerid)
{
    new Float:x, Float:y, Float:z;
    new Float:x1, Float:y1, Float:z1;//To store position of player i (in loop)
    new ii;
    GetPlayerPos(playerid, x, y, z);
    new Float:d=9999;//Assuming d is distance b/w playerid and i and it is 9999 (much)
    for( new i = 0; i < MAX_PLAYERS; i++ )
    {
        if(!IsPlayerConnected(i) || i == playerid) continue;//If player is not connected or playerid is equal to  i
        GetPlayerPos(i, x1, y1, z1);//Store Player Pos (i)  in x1,y1,z1
        if (GetDistance(x, y, z,x1,y1,z1)<d && GetPlayerWantedLevel(i) >= 3)//If distance is lesser than smallest distance
//We assumed smallest distance to be 9999.
        {
            //printf("Player %d is The Nearest Wanted Level", i); We dont need this.
            ii = i;//Store value of i in ii
            d=GetDistance(x, y, z,x1,y1,z1);//Store the new smallest distance in d.
            //return ii;We wont return until loop has gone through all players
        }
    }
    if(d<=5) return ii;//If smallest distance loop could find was less than or equal to 5 then return nearest playerid.
    print("No Wanted Level Near");//Else no player was i range of 5
    return -1;
}
//Similarly for this.
stock nearestrobbable(playerid)
{
    new Float:x, Float:y, Float:z;
    new Float:x1, Float:y1, Float:z1;
    new ii;
    new Float:d = 9999;
    GetPlayerPos(playerid, x, y, z);
    for( new i = 0; i < MAX_PLAYERS; i++ )
    {
        if(!IsPlayerConnected(i) || i == playerid) continue;
        GetPlayerPos(i, x1, y1, z1);
        if (GetDistance(x, y, z,x1,y1,z1)<d)
        {
            //printf("Player %d is The Nearest Robbable Player", i);
            ii = i;
            d=GetDistance(x, y, z,x1,y1,z1);
        }
    }
    id( d<=5)   return ii;
    print("No Robbable player near");
    return -1;
}