PDA

View Full Version : Comparing which is closer


JaKe Elite
31/12/2015, 07:51 AM
I have recently posted a topic about the Phone Signal.
Well this topic is similar to that except the issue this time is that the GetClosestTower always pickups the tower which has the lowest ID. I have created a tower at San Fierro Medical Center, and there is a tower I have placed on Doherty's Skyscrapers. The phone signal on San Fierro Medical Center is two (indicating that the signal range still works at San Fierro Medical Center, however I wanted to extend the signal on the ocean so I have added another tower). I have noticed that even I am near towerID 3 (San Fierro's Medical Center Phone Tower) the phone signal remains at two bars, It suppose to be five bars, the towerID over Doherty's is two. So i am guessing that my GetClosestTower always returns the lowest towerID nearby the player, I wanted to change the way how it works instead of returning the lowest towerID nearby the player (if there are two or more towers nearby the player) how can I compare which tower is closer to the player?

GetClosestTower code


stock GetClosestTower(playerid)
{
new i=0;
new Float:_pos[3];
GetPlayerPos(playerid, _pos[0], _pos[1], _pos[2]);
while(i != MAX_TOWERS)
{
if(IsPlayerInRangeOfPoint(playerid, tInfo[i][tower_Signal], tInfo[i][tower_Pos][0], tInfo[i][tower_Pos][1], tInfo[i][tower_Pos][2]) && GetPlayerInterior(playerid) == tInfo[i][tower_Interior] && GetPlayerVirtualWorld(playerid) == tInfo[i][tower_World])
{
if(tInfo[i][tower_Pos][2]+800.0 >= _pos[2])
{ // Ignore this part, This code extends the signal range to the skies.
return i;
}
else
{
return -1;
}
}
i++;
}
return -1;
}

PrO.GameR
31/12/2015, 08:31 AM
stock GetClosestTower(playerid,Float:searchrange=5.0)
{
new i=0,closest=-1;
new Float:_pos[3];
GetPlayerPos(playerid, _pos[0], _pos[1], _pos[2]);
while(i != MAX_TOWERS)
{
new towerdist=GetPlayerDistanceFromPoint(playerid, tInfo[i][tower_Signal], tInfo[i][tower_Pos][0], tInfo[i][tower_Pos][1], tInfo[i][tower_Pos][2]);
if(towerdist<searchrange && GetPlayerInterior(playerid) == tInfo[i][tower_Interior] && GetPlayerVirtualWorld(playerid) == tInfo[i][tower_World])
{
searchrange=towerdist;
closest=i;
}
i++;
}
return closest;
}
This is pretty much how it's done, change it to fit your needs.

JaKe Elite
31/12/2015, 08:44 AM
It didn't changed anything the signals are the same.. I would try doing my best to found a proper code as well while I am waiting for you guys to help me out ;D

jlalt
31/12/2015, 09:13 AM
Well, is sginal being five when you're near other towers?

JaKe Elite
31/12/2015, 10:24 AM
It is being five when there are only one towers in players sight (1000 meters + signal range for each tower), otherwise if there are more towers on player's sight (if they caught up on the 1000 meters signal range) then yeah everything began to jammed up.

jlalt
31/12/2015, 11:21 AM
How much towers you have .-.? this should work on three xd >>

stock GetClosestTower(playerid)
{
new i=0;
new Float:_pos[3],Float:distance[MAX_TOWERS];
GetPlayerPos(playerid, _pos[0], _pos[1], _pos[2]);
while(i != MAX_TOWERS)
{
if(IsPlayerInRangeOfPoint(playerid, tInfo[i][tower_Signal], tInfo[i][tower_Pos][0], tInfo[i][tower_Pos][1], tInfo[i][tower_Pos][2]) && GetPlayerInterior(playerid) == tInfo[i][tower_Interior] && GetPlayerVirtualWorld(playerid) == tInfo[i][tower_World])
{
if(tInfo[i][tower_Pos][2]+800.0 >= _pos[2])
{ // Ignore this part, This code extends the signal range to the skies.
//return i;
distance[i] = GetPlayerDistanceFromPoint(playerid, tInfo[i][tower_Pos][0], tInfo[i][tower_Pos][1], tInfo[i][tower_Pos][2]);
if(MAX_TOWERS - 1 == i) {
for(new distance1 = i - 1; distance1 != 0; distance1++) {
if(distance[distance1] > distance[i] && distance[distance1] > distance[distance1-1])return distance1;
if(distance[distance1] < distance[i] && distance[i] > distance[distance1-1]) return i;
if(distance[distance1-1] > distance[i] && distance[i] < distance[distance1-1]) return distance1-1;
}
}
}
else
{
return -1;
}
}
i++;
}
return -1;
}

did not find better way to get most nearby tower

Nero_3D
31/12/2015, 11:56 AM
stock GetClosestTower(playerid) {
new
i = 0,
Float: X,
Float: Y,
Float: Z,
Float: tmp,
closest = -1,
Float: dist = Float: 0x7F800000
;
if(GetPlayerPos(playerid, X, Y, Z)) {
do {
if(IsPlayerInRangeOfPoint(playerid, tInfo[i][tower_Signal], tInfo[i][tower_Pos][0], tInfo[i][tower_Pos][1], tInfo[i][tower_Pos][2]) && GetPlayerInterior(playerid) == tInfo[i][tower_Interior] && GetPlayerVirtualWorld(playerid) == tInfo[i][tower_World]) {
if(tInfo[i][tower_Pos][2]+800.0 >= _pos[2]) { // Ignore this part, This code extends the signal range to the skies.
tmp = VectorSize(X - tInfo[i][tower_Pos][0], Y - tInfo[i][tower_Pos][1], Z - tInfo[i][tower_Pos][2]);

if(tmp < dist) {
closest = i;
dist = tmp;
}
}
}
} while(++i < MAX_TOWERS);
}
return closest;
}

JaKe Elite
31/12/2015, 12:16 PM
Thank you so much Nero_3D and jlait, Nero your code works pretty well, SWEET. Your help is appreciated same goes for you mah bruh, jlait who is always there on my topics.