Distance check
#1

So im just worrying because this thing will not work as it should. No matter if someone is around or not it will detect like there is someone near you
PHP код:
CMD:test(playerid,params[]) {
    new 
someplayer GetClosestPlayer(playerid);
    if(
GetDistanceBetweenPlayers(playerid,someplayer) < 5) {
        
SendClientMessage(playerid,COLOR_RED,"There are players near you.");
    }
    else if(
GetDistanceBetweenPlayers(playerid,someplayer) > 5) return SendClientMessage(playerid,COLOR_RED,"You are not near any player");
    return 
true;
}
forward Float:GetDistanceBetweenPlayers(p1,p2);
public 
Float:GetDistanceBetweenPlayers(p1,p2)
{
    new 
Float:x1,Float:y1,Float:z1,Float:x2,Float:y2,Float:z2;
    if(!
IsPlayerConnected(p1) || !IsPlayerConnected(p2))
    {
        return -
1.00;
    }
    
GetPlayerPos(p1,x1,y1,z1);
    
GetPlayerPos(p2,x2,y2,z2);
    return 
floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
}
forward GetClosestPlayer(p1);
public 
GetClosestPlayer(p1)
{
        new 
x,Float:dis,Float:dis2,player;
        
player = -1;
        
dis 5.00;
        for (
x=0;x<MAX_PLAYERS;x++) {
            if(
IsPlayerConnected(x)) {
                if(
!= p1) {
                    
dis2 GetDistanceBetweenPlayers(x,p1);
                    if(
dis2 dis && dis2 != -1.00) {
                        
dis dis2;
                        
player x;
                    }
                }
            }
        }
        return 
player;

Reply
#2

pawn Код:
Float:GetDistanceBetweenPlayers(p1,p2)
{
    if(!IsPlayerConnected(p1) || !IsPlayerConnected(p2))
    {
        return -1.0;
    }
    static Float:x,Float:y,Float:z;
    GetPlayerPos(p1,x,y,z);
    return GetPlayerDistanceFromPoint(p2,x,y,z);
}

GetClosestPlayer(playerid, Float:Range = 9999999.9)
{
    static Float:Distance, player;

    player = INVALID_PLAYER_ID;

    for(new i = GetPlayerPoolSize(); i > -1; i--)
        if(i != playerid) // && IsPlayerStreamedIn(i, playerid)
        {
            Distance = GetDistanceBetweenPlayers(playerid, i);
            if(Distance < Range && Distance > 0.0)
            {
                Range = Distance;
                player = i;
            }
        }

    return player;
}

CMD:test(playerid,params[])
{
    new someplayer = GetClosestPlayer(playerid); // you can set here any max distance GetClosestPlayer(playerid, 10.0);

    if(someplayer != INVALID_PLAYER_ID) SendClientMessage(playerid,COLOR_RED,"There are players near you.");
    else SendClientMessage(playerid,COLOR_RED,"You are not near any player");

    return 1;
}
Reply
#3

I wish I could rep you all day long, Id rep you now but cant anymore Already reped you.
Thanks mate! Ill rep ASAP
Reply
#4

Ah damn, sorry for double post, but ive tested it, it actually crashed my server
Reply
#5

And let me guess... You don't have crashdetect?

EVERYBODY SHOULD HAVE CRASHDETECT!

Get it! Use it so we know which specific line is causing the problem, I don't see anything wrong except the variables being declared with static.

@Jefff: Static variables inside functions retain their values for future use in that function. So you should be using 'new' for these types of things. Static variables outside of functions are a completely different thing, specific to their file.
Reply
#6

problem resolved.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)