SA-MP Forums Archive
Getclosest issue - 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: Getclosest issue (/showthread.php?tid=583993)



Getclosest issue - TwinkiDaBoss - 01.08.2015

Alright so the thing how it should work ,get closest player to you thats NOT part of your team
But preety much it doesnt work

pawn Код:
public GetClosestPlayerToPlayer(playerid)
{
    new Float:dist = 500.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;
        if(pInfo[i][Team] != pInfo[playerid][Team]) continue;
        if(GetVehicleModel(GetPlayerVehicleID(i) != 511) || GetVehicleModel(GetPlayerVehicleID(i) != 476)) 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;
}



AW: Getclosest issue - Macronix - 01.08.2015

Try:

PHP код:
if(pInfo[i][Team] != pInfo[playerid][Team]) continue; 
to change into:

PHP код:
if(pInfo[i][Team] == pInfo[playerid][Team]) continue; 



Re: Getclosest issue - Vince - 01.08.2015

Right, so you want to weed out anyone that IS in your team;
Код:
if(pInfo[i][Team] == pInfo[playerid][Team]) continue;
Sidenote: check out GetPlayerDistanceFromPoint or the example on VectorSize for a more efficient distance calculation method.


Re: Getclosest issue - TwinkiDaBoss - 01.08.2015

Hmm alright but heres another issue Im having with it, it keeps searching for that player I think.

Whats wrong with this code then

pawn Код:
public CheckPlayersInRangeOfAA(playerid)
{
    new
        near = GetClosestPlayerToPlayer(playerid),
        Float:NearPos[3],
        Float:x,Float:y,Float:z,
        string[128];
       
    if(pInfo[playerid][Team] != pInfo[near][Team]) /* If their teams dont match, aka not same team*/
    {
        GetPlayerPos(near,NearPos[0],NearPos[1],NearPos[2]); /* Getting that player position that we are going to shoot */
        if(IsPlayerInRangeOfPoint(playerid,200,x,y,z)) /* Checking if playerid is in of Near */
        {
            GetPlayerPos(playerid,x,y,z); /* Getting player positions to create object that we are going to move*/
            AAC = CreateObject(18647,x,y,z,0,0,0,0); /* Creating the object */

            SetObjectToFaceCords(AAC, NearPos[0],NearPos[1],NearPos[2]); /* Setting the object to face other player*/
            MoveObject(AAC,NearPos[0],NearPos[1],NearPos[2],100); /* moving the object */
           
            format(string,sizeof(string),"Send a rocket towards %s",GetName(near));
        }

    }
    return true;
}
It keeps making the infinite loop or something


Re: Getclosest issue - xVIP3Rx - 01.08.2015

Use the second one.


Re: Getclosest issue - TwinkiDaBoss - 01.08.2015

Yes thanks but I want to check if the player is ONLY in the beagle rustler

pawn Код:
if(i == playerid || !IsPlayerConnected(i) || pInfo[i][Team] == pInfo[playerid][Team] || vmodel == 511 || vmodel == 476) continue;



Re: Getclosest issue - xVIP3Rx - 01.08.2015

Quote:
Originally Posted by TwinkiDaBoss
Посмотреть сообщение
Yes thanks but I want to check if the player is ONLY in the beagle rustler
Okay use this

pawn Код:
public GetClosestPlayerToPlayer(playerid)
{
    new targetid = INVALID_PLAYER_ID;
    new Float:x,Float:y,Float:z;
    GetPlayerPos(playerid,x,y,z);

    for(new Float:closestdist = 999.0, Float:Dis, i; i<MAX_PLAYERS; i++) // You should use foreach or GetPlayerPoolSize
    {
        vmodel = GetVehicleModel(GetPlayerVehicleID(i));
        if(i == playerid || !IsPlayerConnected(i) || pInfo[i][Team] == pInfo[playerid][Team] || vmodel != 511 || vmodel != 476) continue;

        Dis = GetPlayerDistanceFromPoint(playerid, x, y, z);
        if(Dis < closestdist)
        {
            closestdist = Dis;
            targetid = i;
        }
    }
    return targetid;
}

public CheckPlayersInRangeOfAA(playerid)
{
    new id = GetClosestPlayerToPlayer(playerid),
        Float:x, Float:y, Float:z;
       
    GetPlayerPos(playerid, x, y, z);
    if(id != INVALID_PLAYER_ID && IsPlayerInRangeOfPoint(id, 200, x, y, z))
    {
        AAC = CreateObject(18647,x,y,z,0,0,0,0);

        GetPlayerPos(id, x, y, z);
        SetObjectToFaceCords(AAC, x, y, z);
        MoveObject(AAC,x, y, z, 100);

        new string[128];
        format(string,sizeof(string),"Send a rocket towards %s",GetName(id));
    }
    return true;
}
Or add the check inside the function..
pawn Код:
public CheckPlayersInRangeOfAA(playerid)
{
    new id = INVALID_PLAYER_ID,
        Float:x, Float:y, Float:z;
    GetPlayerPos(playerid, x, y, z);
   
    for(new Float:closestdist = 200.0, Float:Dis, vmodel, i; i<MAX_PLAYERS; i++) // You should use foreach or GetPlayerPoolSize
    {
        vmodel = GetVehicleModel(GetPlayerVehicleID(i));
        if(i == playerid || !IsPlayerConnected(i) || pInfo[i][Team] == pInfo[playerid][Team] || vmodel != 511 || vmodel != 476) continue;
        //If he's not in a Rustler/Beagle we're not checking him

        Dis = GetPlayerDistanceFromPoint(playerid, x, y, z);
        if(Dis < closestdist)
        {
            closestdist = Dis;
            id = i;
        }
    }

    if(id != INVALID_PLAYER_ID)
    {
        AAC = CreateObject(18647,x,y,z,0,0,0,0);

        GetPlayerPos(id, x, y, z);
        SetObjectToFaceCords(AAC, x, y, z);
        MoveObject(AAC,x, y, z, 100);

        new string[128];
        format(string,sizeof(string),"Send a rocket towards %s",GetName(id));
    }
    return true;
}



Re: Getclosest issue - TwinkiDaBoss - 01.08.2015

Nope doesnt work :/

Nothing really happends. It tries to get the nearest player but the rocket never takes off or anything


Re: Getclosest issue - xVIP3Rx - 01.08.2015

Both or the second one ?


Re: Getclosest issue - TwinkiDaBoss - 01.08.2015

Quote:
Originally Posted by xVIP3Rx
Посмотреть сообщение
What are you trying to do and why doesn't it work ?

This should check if somebody's on a rustler and closer then 200 GTA unit then create something and make it go toward the player
"and make it go toward the player" doesnt work. It doesnt detect the nearest player or such.

Basically
pawn Код:
format(string,sizeof(string),"Sent a rocket towards %s",GetName(id));
isnt being printed out and the object doesnt create and well doesnt move.


Well this part doesnt work..
pawn Код:
if(id != INVALID_PLAYER_ID)
    {
        AAC = CreateObject(18647,x,y,z,0,0,0,0);

        GetPlayerPos(id, x, y, z);
        SetObjectToFaceCords(AAC, x, y, z);
        MoveObject(AAC,x, y, z, 100);

        new string[128];
        format(string,sizeof(string),"Sent a rocket towards %s",GetName(id));
    }