SA-MP Forums Archive
Distance check - 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: Distance check (/showthread.php?tid=595796)



Distance check - TwinkiDaBoss - 07.12.2015

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;




Re: Distance check - Jefff - 08.12.2015

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



Re: Distance check - TwinkiDaBoss - 08.12.2015

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


Re: Distance check - TwinkiDaBoss - 08.12.2015

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


Re: Distance check - Crayder - 08.12.2015

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.


Re: Distance check - TwinkiDaBoss - 08.12.2015

problem resolved.