Fix AStarPathfindingAlgorithm.

This commit is contained in:
Major-
2015-08-01 14:10:07 +01:00
parent 4665d7617d
commit 02fb07b871
8 changed files with 604 additions and 597 deletions
@@ -27,15 +27,15 @@ import org.apollo.game.model.area.RegionRepository;
public final class AStarPathfindingAlgorithm extends PathfindingAlgorithm {
/**
* The heuristic.
* The Heuristic used by this PathfindingAlgorithm.
*/
private final Heuristic heuristic;
/**
* Creates the A* pathfinding algorithm with the specified heuristic.
* Creates the A* pathfinding algorithm with the specified {@link Heuristic}.
*
* @param repository The {@link RegionRepository}.
* @param heuristic The heuristic.
* @param heuristic The Heuristic.
*/
public AStarPathfindingAlgorithm(RegionRepository repository, Heuristic heuristic) {
super(repository);
@@ -66,8 +66,8 @@ public final class AStarPathfindingAlgorithm extends PathfindingAlgorithm {
active.close();
int x = position.getX(), y = position.getY();
for (int nextX = x - 1; x <= x + 1; nextX++) {
for (int nextY = y - 1; y <= y + 1; nextY++) {
for (int nextX = x - 1; nextX <= x + 1; nextX++) {
for (int nextY = y - 1; nextY <= y + 1; nextY++) {
if (nextX == x && nextY == y) {
continue;
}
@@ -8,46 +8,46 @@ import org.apollo.game.model.Position;
import com.google.common.base.MoreObjects;
/**
* A node representing a weighted {@link Position}.
* A Node representing a weighted {@link Position}.
*
* @author Major
*/
final class Node {
final class Node implements Comparable<Node> {
/**
* The cost of this node.
* The cost of this Node.
*/
private int cost;
/**
* Whether or not this node is open.
* Whether or not this Node is open.
*/
private boolean open = true;
/**
* The parent node of this node.
* The parent Node of this Node.
*/
private Optional<Node> parent = Optional.empty();
/**
* The point this node represents.
* The Position of this Node.
*/
private final Position position;
/**
* Creates the node with the specified {@link Position} and cost.
* Creates the Node with the specified {@link Position} and cost.
*
* @param position The position.
* @param position The Position.
*/
public Node(Position position) {
this(position, 0);
}
/**
* Creates the node with the specified {@link Position} and cost.
* Creates the Node with the specified {@link Position} and cost.
*
* @param position The position.
* @param cost The cost of the node.
* @param position The Position.
* @param cost The cost of the Node.
*/
public Node(Position position, int cost) {
this.position = position;
@@ -55,12 +55,17 @@ final class Node {
}
/**
* Closes this node.
* Closes this Node.
*/
public void close() {
open = false;
}
@Override
public int compareTo(Node other) {
return Integer.compare(cost, other.cost);
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Node) {
@@ -73,7 +78,7 @@ final class Node {
}
/**
* Gets the cost of this node.
* Gets the cost of this Node.
*
* @return The cost.
*/
@@ -82,17 +87,17 @@ final class Node {
}
/**
* Gets the parent node of this node.
* Gets the parent Node of this Node.
*
* @return The parent node.
* @throws NoSuchElementException If this node does not have a parent.
* @return The parent Node.
* @throws NoSuchElementException If this Node does not have a parent.
*/
public Node getParent() {
return parent.get();
}
/**
* Gets the {@link Position} this node represents.
* Gets the {@link Position} this Node represents.
*
* @return The position.
*/
@@ -106,9 +111,9 @@ final class Node {
}
/**
* Returns whether or not this node has a parent node.
* Returns whether or not this Node has a parent Node.
*
* @return {@code true} if this node has a parent node, otherwise {@code false}.
* @return {@code true} if this Node has a parent Node, otherwise {@code false}.
*/
public boolean hasParent() {
return parent.isPresent();
@@ -117,14 +122,14 @@ final class Node {
/**
* Returns whether or not this {@link Node} is open.
*
* @return {@code true} if this node is open, otherwise {@code false}.
* @return {@code true} if this Node is open, otherwise {@code false}.
*/
public boolean isOpen() {
return open;
}
/**
* Sets the cost of this node.
* Sets the cost of this Node.
*
* @param cost The cost.
*/
@@ -133,9 +138,9 @@ final class Node {
}
/**
* Sets the parent node of this node.
* Sets the parent Node of this Node.
*
* @param parent The parent node. May be {@code null}.
* @param parent The parent Node. May be {@code null}.
*/
public void setParent(Node parent) {
this.parent = Optional.ofNullable(parent);
@@ -143,7 +148,8 @@ final class Node {
@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("position", position).add("open", open).add("cost", cost).toString();
return MoreObjects.toStringHelper(this).add("position", position).add("open", open).add("cost", cost)
.toString();
}
}
@@ -132,7 +132,8 @@ public final class SimplePathfindingAlgorithm extends PathfindingAlgorithm {
}
Position last = new Position(x, y, height);
if (!last.equals(target) && dx != 0 && traversable(last, boundaries, dx > 0 ? Direction.WEST : Direction.EAST)) {
if (!last.equals(target) && dx != 0
&& traversable(last, boundaries, dx > 0 ? Direction.WEST : Direction.EAST)) {
return addHorizontal(last, target, positions);
}