Merge branch 'pathfinding'.

This commit is contained in:
Ryley Kimmel
2015-02-26 17:15:54 -05:00
5 changed files with 36 additions and 25 deletions
@@ -120,6 +120,8 @@ public final class ObjectDefinitionDecoder {
data.get(); data.get();
} else if (opcode >= 70 && opcode <= 72) { } else if (opcode >= 70 && opcode <= 72) {
data.getShort(); data.getShort();
} else if (opcode == 73) {
definition.setObstructive(true);
} else if (opcode == 75) { } else if (opcode == 75) {
data.get(); data.get();
} else { } else {
@@ -82,6 +82,11 @@ public final class ObjectDefinition {
*/ */
private boolean interactive; private boolean interactive;
/**
* Denotes whether or not this object obstructs the ground.
*/
private boolean obstructive;
/** /**
* This object's length. * This object's length.
*/ */
@@ -173,7 +178,8 @@ public final class ObjectDefinition {
/** /**
* Indicates the impenetrability of this object. * Indicates the impenetrability of this object.
* *
* @return {@code true} if this object is impenetrable, otherwise {@code false}. * @return {@code true} if this object is impenetrable, otherwise
* {@code false}.
*/ */
public boolean isImpenetrable() { public boolean isImpenetrable() {
return impenetrable; return impenetrable;
@@ -182,12 +188,23 @@ public final class ObjectDefinition {
/** /**
* Indicates the interactivity of this object. * Indicates the interactivity of this object.
* *
* @return {@code true} if the object is interactive, otherwise {@code false}. * @return {@code true} if the object is interactive, otherwise
* {@code false}.
*/ */
public boolean isInteractive() { public boolean isInteractive() {
return interactive; return interactive;
} }
/**
* Indicates whether or not this object obstructs the ground.
*
* @return {@code true} if the object obstructs the ground otherwise
* {@code false}.
*/
public boolean isObstructive() {
return obstructive;
}
/** /**
* Indicates the solidity of this object. * Indicates the solidity of this object.
* *
@@ -269,4 +286,13 @@ public final class ObjectDefinition {
this.width = width; this.width = width;
} }
/**
* Sets whether or not this object is obstructive to the ground.
*
* @param obstructive Whether or not this object obstructs the ground.
*/
public void setObstructive(boolean obstructive) {
this.obstructive = obstructive;
}
} }
@@ -71,7 +71,7 @@ final class AStarPathfindingAlgorithm extends PathfindingAlgorithm {
Position adjacent = new Position(nextX, nextY); Position adjacent = new Position(nextX, nextY);
if (traversable(adjacent)) { if (traversable(adjacent)) {
Node node = createIfAbsent(adjacent, nodes); Node node = nodes.computeIfAbsent(adjacent, Node::new);
compare(active, node, open, sorted, heuristic); compare(active, node, open, sorted, heuristic);
} }
} }
@@ -118,24 +118,6 @@ final class AStarPathfindingAlgorithm extends PathfindingAlgorithm {
} }
} }
/**
* Creates a {@link Node} and inserts it into the specified {@link Map} if one does not already exist, then returns
* that node.
*
* @param position The {@link Position}.
* @param nodes The map of positions to nodes.
* @return The node.
*/
private Node createIfAbsent(Position position, Map<Position, Node> nodes) {
Node existing = nodes.get(position);
if (existing == null) {
existing = new Node(position);
nodes.put(position, existing);
}
return existing;
}
/** /**
* Gets the cheapest open {@link Node} from the {@link Queue}. * Gets the cheapest open {@link Node} from the {@link Queue}.
* *
@@ -5,6 +5,8 @@ import java.util.Optional;
import org.apollo.game.model.Position; 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}.
* *
@@ -100,7 +102,7 @@ final class Node {
@Override @Override
public int hashCode() { public int hashCode() {
return position.getX() * 31 + position.getY(); return position.hashCode();
} }
/** /**
@@ -141,8 +143,7 @@ final class Node {
@Override @Override
public String toString() { public String toString() {
return Node.class.getSimpleName() + " [x=" + position.getX() + ", y=" + position.getY() + ", open=" + open + ", cost=" return MoreObjects.toStringHelper(this).add("position", position).add("open", open).add("cost", cost).toString();
+ cost + "]";
} }
} }
@@ -8,7 +8,7 @@ import org.apollo.game.model.Position;
/** /**
* A very simple pathfinding algorithm that simply walks in the direction of the target until it either reaches it or is * A very simple pathfinding algorithm that simply walks in the direction of the target until it either reaches it or is
* blocked. TODO diagonal movement support. * blocked.
* *
* @author Major * @author Major
*/ */