Skip to content

Commit

Permalink
Increase A* pathfinder accuracy by switching back to distance squared
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Jul 5, 2013
1 parent 3109fee commit 9c169b0
Showing 1 changed file with 6 additions and 8 deletions.
Expand Up @@ -6,20 +6,17 @@
import net.citizensnpcs.api.astar.Plan;

import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.util.Vector;

import com.google.common.collect.Lists;

public class VectorNode extends AStarNode implements PathPoint {
private float blockCost = -1;
final BlockSource blockSource;
private final BlockSource blockSource;
List<PathCallback> callbacks;
private final BlockExaminer[] examiners;
final Vector location;

boolean played = false;

public VectorNode(Location location, BlockSource source, BlockExaminer... examiners) {
this(location.toVector(), source, examiners);
}
Expand All @@ -32,8 +29,9 @@ public VectorNode(Vector location, BlockSource source, BlockExaminer... examiner

@Override
public void addCallback(PathCallback callback) {
if (callbacks == null)
if (callbacks == null) {
callbacks = Lists.newArrayList();
}
callbacks.add(callback);
}

Expand All @@ -59,7 +57,6 @@ private float getBlockCost() {

@Override
public Iterable<AStarNode> getNeighbours() {
World world = blockSource.getWorld();
List<AStarNode> nodes = Lists.newArrayList();
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
Expand Down Expand Up @@ -89,14 +86,15 @@ public Vector getVector() {
}

public float heuristicDistance(Vector goal) {
return (float) (location.distance(goal) + getBlockCost()) * TIEBREAKER;
return (float) (location.distanceSquared(goal) + getBlockCost()) * TIEBREAKER;
}

private boolean isPassable(PathPoint mod) {
for (BlockExaminer examiner : examiners) {
boolean passable = examiner.isPassable(blockSource, mod);
if (!passable)
if (!passable) {
return false;
}
}
return true;
}
Expand Down

0 comments on commit 9c169b0

Please sign in to comment.