Change /Rob <ID> into /Rob (GetClosestPlayer)
#1

Hi,

I currently have a system where you do /command and the ID, but I am wanting to change it, especially the /rob command, so it will find the nearest player, I have ******d it, but still a little hazy on how to follow it out.

If someone could shed some light on how to do this, would be great help
Reply
#2

When you type a command "/rob" just start a for loop which gonna count your server slots or max players (default is: 500) and then get your position to local float variables and check with function:

pawn Код:
IsPlayerInRangeOfPoint
Is any player is in your range value from the function above, check IsPlayerInRangeOfPoint parameters in here:
https://sampwiki.blast.hk/wiki/IsPlayerInRangeOfPoint

And then just display player names/id's in dialog, chat box or anything else.

@down, it's gonna display one player.
Reply
#3

pawn Код:
forward GetClosestPlayerToPlayer(playerid);
public GetClosestPlayerToPlayer(playerid)
{
    new Float:dist = 1000.0;
    new targetid = INVALID_PLAYER_ID;
    new Float:x1,Float:y1,Float:z1;
    new Float:x2,Float:y2,Float:z2;
    new Float:tmpdis;
    GetPlayerPos(playerid,x1,y1,z1);
    for(new i=0;i<MAX_PLAYERS;i++)
    {
        if(i == playerid) continue;
        GetPlayerPos(i,x2,y2,z2);
        tmpdis = floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
        if(tmpdis < dist)
        {
            dist = tmpdis;
            targetid = i;
        }
    }
    return targetid;
}
Reply
#4

Well, this was just my shot at it :S
If you're using a team based gamemode, I would recommend you do team checks to make sure a player isn't robbing someone of the same team (excluding civilians for example)...

pawn Код:
stock GetClosestPlayer(playerid)
{
    new closestplayer = INVALID_PLAYER_ID;
    new Float:closex, Float:closey, Float:closez;
    for(new i = 0; i < MAX_PLAYERS; i++) //Foreach is better
    {
        if(IsPlayerConnected(i))
        {
            if(i == playerid) continue;
            //if(Team[i] == Team[playerid]) continue; //Recommended if you're going to use this for a /rob command
            new Float:X, Float:Y, Float:Z;
            GetPlayerPos(i, X, Y, Z);
            if(closestplayer == INVALID_PLAYER_ID)
            {
                closestplayer = i;
                closex = X;
                closey = Y;
                closez = Z;
            }
            else
            {
                if(GetPlayerDistanceFromPoint(playerid, X, Y, Z) < GetPlayerDistanceFromPoint(playerid, closex, closey, closez))
                {
                    closestplayer = i;
                    closex = X;
                    closey = Y;
                    closez = Z;
                }
            }
        }
    }
    return closestplayer;
}
Should honestly work...
Reply
#5

Quote:
Originally Posted by FunnyBear
Посмотреть сообщение
pawn Код:
forward GetClosestPlayerToPlayer(playerid);
public GetClosestPlayerToPlayer(playerid)
{
    new Float:dist = 1000.0;
    new targetid = INVALID_PLAYER_ID;
    new Float:x1,Float:y1,Float:z1;
    new Float:x2,Float:y2,Float:z2;
    new Float:tmpdis;
    GetPlayerPos(playerid,x1,y1,z1);
    for(new i=0;i<MAX_PLAYERS;i++)
    {
        if(i == playerid) continue;
        GetPlayerPos(i,x2,y2,z2);
        tmpdis = floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
        if(tmpdis < dist)
        {
            dist = tmpdis;
            targetid = i;
        }
    }
    return targetid;
}
F:\SAMP SERVER\gamemodes\ICNR.pwn(539) : error 017: undefined symbol "x1"
F:\SAMP SERVER\gamemodes\ICNR.pwn(543) : error 017: undefined symbol "x2"
F:\SAMP SERVER\gamemodes\ICNR.pwn(544) : error 017: undefined symbol "x2"
F:\SAMP SERVER\gamemodes\ICNR.pwn(544) : error 017: undefined symbol "er"
F:\SAMP SERVER\gamemodes\ICNR.pwn(544) : error 029: invalid expression, assumed zero
F:\SAMP SERVER\gamemodes\ICNR.pwn(544) : fatal error 107: too many error messages on one line

:S
Reply
#6

pawn Код:
Float:distanceP2P(playerid, toplayerid)
{
    new Float:p1pos[3], Float: p2pos[3];
    GetPlayerPos(playerid, p1pos[0], p1pos[1], p1pos[2]);
    GetPlayerPos(toplayerid, p2pos[0], p2pos[1], p2pos[2]);
   
    return float(floatround(floatsqroot( ((p1pos[0] - p2pos[0]) ^2) + ((p1pos[1] - p2pos[1]) ^ 2) + ((p1pos[2] - p2pos[2]) ^ 2))));
}

GetClosestPlayer(toplayerid)
{
    new cur = INVALID_PLAYER_ID, Float: distancetmp = 99999.99;
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if( (distanceP2P(toplayerid, i) < distancetmp) && (i != toplayerid) && (i != INVALID_PLAYER_ID) ) cur = i;
    }
    return cur;
}

public OnPlayerCommandText(playerid, cmdtext[])
{
    if(!strcmp(cmdtext, "/rob"))
    {
        new amount = 1500 + random(500); // A random amount of robbed money between 1500 and 2000$
        new success = random(2);
        GivePlayerMoney(GetClosestPlayer(playerid), (success == 1) ? -amount : 0);
        GivePlayerMoney(playerid, (success == 1) ? amount : 0);
        return 1;
    }
    return 0;
}
Reply
#7

ProxDetexctorS(5.0, playerid, giveplayerid);
Reply
#8

Quote:
Originally Posted by [XST]O_x
Посмотреть сообщение
pawn Код:
Float:distanceP2P(playerid, toplayerid)
{
    new Float:p1pos[3], Float: p2pos[3];
    GetPlayerPos(playerid, p1pos[0], p1pos[1], p1pos[2]);
    GetPlayerPos(playerid, p2pos[0], p2pos[1], p2pos[2]);
   
    return float(floatround(floatsqroot( ((p1pos[0] - p2pos[0]) ^2) + ((p1pos[1] - p2pos[1]) ^ 2) + ((p1pos[2] - p2pos[2]) ^ 2))));
}

GetClosestPlayer(toplayerid)
{
    new cur = INVALID_PLAYER_ID, Float: distance = 99999.99;
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if( (distanceP2P(toplayerid, i) < distancetmp) && (i != toplayerid) && (i != INVALID_PLAYER_ID) ) cur = i;
    }
    return cur;
}

public OnPlayerCommandText(playerid, cmdtext[])
{
    if(!strcmp(cmdtext, "/rob"))
    {
        new amount = 1500 + random(500); // A random amount of robbed money between 1500 and 2000$
        new success = random(2);
        GivePlayerMoney(GetClosestPlayer(playerid), (success == 1) ? -amount : 0);
        GivePlayerMoney(playerid, (success == 1) ? amount : 0);
        return 1;
    }
    return 0;
}
Where exactly do I place this?

Float:distanceP2P(playerid, toplayerid)
{
new Float1pos[3], Float: p2pos[3];
GetPlayerPos(playerid, p1pos[0], p1pos[1], p1pos[2]);
GetPlayerPos(playerid, p2pos[0], p2pos[1], p2pos[2]);

return float(floatround(floatsqroot( ((p1pos[0] - p2pos[0]) ^2) + ((p1pos[1] - p2pos[1]) ^ 2) + ((p1pos[2] - p2pos[2]) ^ 2))));
}

GetClosestPlayer(toplayerid)
{
new cur = INVALID_PLAYER_ID, Float: distance = 99999.99;
for(new i = 0; i < MAX_PLAYERS; i++)
{
if( (distanceP2P(toplayerid, i) < distancetmp) && (i != toplayerid) && (i != INVALID_PLAYER_ID) ) cur = i;
}
return cur;
}
Reply
#9

Quote:
Originally Posted by 15outland
Посмотреть сообщение
Where exactly do I place this?

Float:distanceP2P(playerid, toplayerid)
{
new Float1pos[3], Float: p2pos[3];
GetPlayerPos(playerid, p1pos[0], p1pos[1], p1pos[2]);
GetPlayerPos(playerid, p2pos[0], p2pos[1], p2pos[2]);

return float(floatround(floatsqroot( ((p1pos[0] - p2pos[0]) ^2) + ((p1pos[1] - p2pos[1]) ^ 2) + ((p1pos[2] - p2pos[2]) ^ 2))));
}

GetClosestPlayer(toplayerid)
{
new cur = INVALID_PLAYER_ID, Float: distance = 99999.99;
for(new i = 0; i < MAX_PLAYERS; i++)
{
if( (distanceP2P(toplayerid, i) < distancetmp) && (i != toplayerid) && (i != INVALID_PLAYER_ID) ) cur = i;
}
return cur;
}
I've fixed the code now, I've written it blindly on the forum and not in Pawno, so it was kinda difficult to find mistakes.
Place these two functions anywhere on your script that is OUTSIDE of any other callback.
Reply
#10

Quote:
Originally Posted by BenzoAMG
Посмотреть сообщение
Well, this was just my shot at it :S
If you're using a team based gamemode, I would recommend you do team checks to make sure a player isn't robbing someone of the same team (excluding civilians for example)...

pawn Код:
stock GetClosestPlayer(playerid)
{
    new closestplayer = INVALID_PLAYER_ID;
    new Float:closex, Float:closey, Float:closez;
    for(new i = 0; i < MAX_PLAYERS; i++) //Foreach is better
    {
        if(IsPlayerConnected(i))
        {
            if(i == playerid) continue;
            //if(Team[i] == Team[playerid]) continue; //Recommended if you're going to use this for a /rob command
            new Float:X, Float:Y, Float:Z;
            GetPlayerPos(i, X, Y, Z);
            if(closestplayer == INVALID_PLAYER_ID)
            {
                closestplayer = i;
                closex = X;
                closey = Y;
                closez = Z;
            }
            else
            {
                if(GetPlayerDistanceFromPoint(playerid, X, Y, Z) < GetPlayerDistanceFromPoint(playerid, closex, closey, closez))
                {
                    closestplayer = i;
                    closex = X;
                    closey = Y;
                    closez = Z;
                }
            }
        }
    }
    return closestplayer;
}
Should honestly work...
I did give you the stock for this...
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)