mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-07 08:39:17 +00:00
Fix AStarPathfindingAlgorithm.
This commit is contained in:
@@ -27,15 +27,15 @@ import org.apollo.game.model.area.RegionRepository;
|
|||||||
public final class AStarPathfindingAlgorithm extends PathfindingAlgorithm {
|
public final class AStarPathfindingAlgorithm extends PathfindingAlgorithm {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The heuristic.
|
* The Heuristic used by this PathfindingAlgorithm.
|
||||||
*/
|
*/
|
||||||
private final Heuristic heuristic;
|
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 repository The {@link RegionRepository}.
|
||||||
* @param heuristic The heuristic.
|
* @param heuristic The Heuristic.
|
||||||
*/
|
*/
|
||||||
public AStarPathfindingAlgorithm(RegionRepository repository, Heuristic heuristic) {
|
public AStarPathfindingAlgorithm(RegionRepository repository, Heuristic heuristic) {
|
||||||
super(repository);
|
super(repository);
|
||||||
@@ -66,8 +66,8 @@ public final class AStarPathfindingAlgorithm extends PathfindingAlgorithm {
|
|||||||
active.close();
|
active.close();
|
||||||
|
|
||||||
int x = position.getX(), y = position.getY();
|
int x = position.getX(), y = position.getY();
|
||||||
for (int nextX = x - 1; x <= x + 1; nextX++) {
|
for (int nextX = x - 1; nextX <= x + 1; nextX++) {
|
||||||
for (int nextY = y - 1; y <= y + 1; nextY++) {
|
for (int nextY = y - 1; nextY <= y + 1; nextY++) {
|
||||||
if (nextX == x && nextY == y) {
|
if (nextX == x && nextY == y) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,46 +8,46 @@ import org.apollo.game.model.Position;
|
|||||||
import com.google.common.base.MoreObjects;
|
import com.google.common.base.MoreObjects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A node representing a weighted {@link Position}.
|
* A Node representing a weighted {@link Position}.
|
||||||
*
|
*
|
||||||
* @author Major
|
* @author Major
|
||||||
*/
|
*/
|
||||||
final class Node {
|
final class Node implements Comparable<Node> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The cost of this node.
|
* The cost of this Node.
|
||||||
*/
|
*/
|
||||||
private int cost;
|
private int cost;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Whether or not this node is open.
|
* Whether or not this Node is open.
|
||||||
*/
|
*/
|
||||||
private boolean open = true;
|
private boolean open = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The parent node of this node.
|
* The parent Node of this Node.
|
||||||
*/
|
*/
|
||||||
private Optional<Node> parent = Optional.empty();
|
private Optional<Node> parent = Optional.empty();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The point this node represents.
|
* The Position of this Node.
|
||||||
*/
|
*/
|
||||||
private final Position position;
|
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) {
|
public Node(Position position) {
|
||||||
this(position, 0);
|
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 position The Position.
|
||||||
* @param cost The cost of the node.
|
* @param cost The cost of the Node.
|
||||||
*/
|
*/
|
||||||
public Node(Position position, int cost) {
|
public Node(Position position, int cost) {
|
||||||
this.position = position;
|
this.position = position;
|
||||||
@@ -55,12 +55,17 @@ final class Node {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Closes this node.
|
* Closes this Node.
|
||||||
*/
|
*/
|
||||||
public void close() {
|
public void close() {
|
||||||
open = false;
|
open = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(Node other) {
|
||||||
|
return Integer.compare(cost, other.cost);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (obj instanceof Node) {
|
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.
|
* @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.
|
* @return The parent Node.
|
||||||
* @throws NoSuchElementException If this node does not have a parent.
|
* @throws NoSuchElementException If this Node does not have a parent.
|
||||||
*/
|
*/
|
||||||
public Node getParent() {
|
public Node getParent() {
|
||||||
return parent.get();
|
return parent.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the {@link Position} this node represents.
|
* Gets the {@link Position} this Node represents.
|
||||||
*
|
*
|
||||||
* @return The position.
|
* @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() {
|
public boolean hasParent() {
|
||||||
return parent.isPresent();
|
return parent.isPresent();
|
||||||
@@ -117,14 +122,14 @@ final class Node {
|
|||||||
/**
|
/**
|
||||||
* Returns whether or not this {@link Node} is open.
|
* 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() {
|
public boolean isOpen() {
|
||||||
return open;
|
return open;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the cost of this node.
|
* Sets the cost of this Node.
|
||||||
*
|
*
|
||||||
* @param cost The cost.
|
* @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) {
|
public void setParent(Node parent) {
|
||||||
this.parent = Optional.ofNullable(parent);
|
this.parent = Optional.ofNullable(parent);
|
||||||
@@ -143,7 +148,8 @@ final class Node {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
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);
|
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);
|
return addHorizontal(last, target, positions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user