PDA

View Full Version : Getclosest issue


TwinkiDaBoss
01/08/2015, 03:27 PM
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

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)+floatpow er(floatabs(floatsub(z2,z1)),2));
if(tmpdis < dist)
{
dist = tmpdis;
targetid = i;
}
}
return targetid;
}

Macronix
01/08/2015, 03:45 PM
Try:

if(pInfo[i][Team] != pInfo[playerid][Team]) continue;

to change into:

if(pInfo[i][Team] == pInfo[playerid][Team]) continue;

Vince
01/08/2015, 03:46 PM
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 (http://wiki.sa-mp.com/wiki/GetPlayerDistanceFromPoint) or the example on VectorSize (http://wiki.sa-mp.com/wiki/VectorSize) for a more efficient distance calculation method.

TwinkiDaBoss
01/08/2015, 04:15 PM
Hmm alright but heres another issue Im having with it, it keeps searching for that player I think.

Whats wrong with this code then

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

xVIP3Rx
01/08/2015, 04:31 PM
Use the second one.

TwinkiDaBoss
01/08/2015, 04:36 PM
Yes thanks but I want to check if the player is ONLY in the beagle rustler

if(i == playerid || !IsPlayerConnected(i) || pInfo[i][Team] == pInfo[playerid][Team] || vmodel == 511 || vmodel == 476) continue;

xVIP3Rx
01/08/2015, 04:40 PM
Yes thanks but I want to check if the player is ONLY in the beagle rustler

Okay use this


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..

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

TwinkiDaBoss
01/08/2015, 04:55 PM
Nope doesnt work :/

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

xVIP3Rx
01/08/2015, 04:57 PM
Both or the second one ?

TwinkiDaBoss
01/08/2015, 04:59 PM
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
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..
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));
}

xVIP3Rx
01/08/2015, 05:03 PM
Is that a timer ?
Do you want it to check if there's anybody around somewhere and shoot him if he is ?

Check there conditions and make sure you're not making any of them true;
(i == playerid || !IsPlayerConnected(i) || pInfo[i][Team] == pInfo[playerid][Team] || vmodel != 511 || vmodel != 476)

Also try to increase the closestDistance, cause I think 200 is not enough for a plane in the air.

TwinkiDaBoss
01/08/2015, 05:24 PM
Yes preety much its under a timer
CMD:radaron(playerid,params[])
{
if(GetVehicleModel(GetPlayerVehicleID(playerid)) != 554)
return SendClientMessage(playerid,COLOR_RED,"<!> You are not inside AA Vehicle!");

if(pInfo[playerid][Class] != CLASS_SUPPORT)
return SendClientMessage(playerid,COLOR_RED,"You are not a support class!");

SetTimerEx("CheckPlayersInRangeOfAA",2000,false,"i",playerid);
SendClientMessage(playerid,COLOR_GREEN,"You have turned on your radar, please wait while we track closest target");
return true;
}


EDIT: And yes i've changed the range but still same issue. The stuff after ivalid player ID aint being called.

xVIP3Rx
01/08/2015, 05:48 PM
public CheckPlayersInRangeOfAA(playerid)
{
new id = INVALID_PLAYER_ID,
Float:x, Float:y, Float:z;
GetPlayerPos(playerid, x, y, z);

for(new Float:closestdist = 500.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;

Dis = GetPlayerDistanceFromPoint(playerid, x, y, z);

new String[128];
format(String, 128, "ID %d is not on your team and is in vehicle id 511/476 and he is %f ft away from you", i, Dis);
SendClientMessage(playerid, -1, String);
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;
}

If nothing show up when you do it, then one of the conditions are wrong

Are you testing it alone or with somebody ? stupid question but I have to ask it.

TwinkiDaBoss
01/08/2015, 10:55 PM
Preety much the remaning issue is

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

for(new Float:closestdist = 500.0, Float:Dis, vmodel, i; i<MAX_PLAYERS; i++)
{
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;
}

it will shoot only at players that are NOT inside beagle and rustler.

I need of it to shoot players that are inside those vehicles

xVIP3Rx
01/08/2015, 11:07 PM
This should do it.


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

for(new Float:closestdist = 500.0, Float:Dis, vmodel, i; i<MAX_PLAYERS; i++)
{
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;
}

TwinkiDaBoss
01/08/2015, 11:10 PM
Nope, that doesnt work at all actually.
Ive tried with != but then it doesnt shoot the flare, rocket at all.

TwinkiDaBoss
02/08/2015, 02:15 AM
Issue resolved.