Optimized getClosestPlayer

This commit is contained in:
Garbage Mule
2011-06-01 16:26:13 +02:00
parent c9cc5cef8b
commit 5e4d9b5624
2 changed files with 7 additions and 12 deletions
BIN
View File
Binary file not shown.
@@ -50,10 +50,7 @@ public class MASpawnThread implements Runnable
} }
public void run() public void run()
{ {
// Make a new ArrayList with all the players for fast lookup
playerList = new ArrayList<Player>(ArenaManager.playerSet);
// Check if we need to grant more rewards with the recurrent waves. // Check if we need to grant more rewards with the recurrent waves.
for (Integer i : ArenaManager.everyWaveMap.keySet()) for (Integer i : ArenaManager.everyWaveMap.keySet())
{ {
@@ -215,9 +212,9 @@ public class MASpawnThread implements Runnable
public static Player getClosestPlayer(Entity e) public static Player getClosestPlayer(Entity e)
{ {
// Grab the coordinates. // Grab the coordinates.
double ex = e.getLocation().getX(); double x = e.getLocation().getX();
double ey = e.getLocation().getY(); double y = e.getLocation().getY();
double ez = e.getLocation().getZ(); double z = e.getLocation().getZ();
// Set up the comparison variable and the result. // Set up the comparison variable and the result.
double current = Double.POSITIVE_INFINITY; double current = Double.POSITIVE_INFINITY;
@@ -225,12 +222,10 @@ public class MASpawnThread implements Runnable
/* Iterate through the ArrayList, and update current and result every /* Iterate through the ArrayList, and update current and result every
* time a squared distance smaller than current is found. */ * time a squared distance smaller than current is found. */
for (int i = 0; i < playerList.size(); i++) for (Player p : ArenaManager.playerSet)
{ {
Player p = playerList.get(i); double dist = distance(p.getLocation(), x, y, z);
double dist = distance(p.getLocation(), ex, ey, ez); if (dist < current)
if (dist < current || current == -1.0D)
{ {
current = dist; current = dist;
result = p; result = p;