diff --git a/gdx-ai/src/com/badlogic/gdx/ai/pfa/indexed/IndexedAStarPathFinder.java b/gdx-ai/src/com/badlogic/gdx/ai/pfa/indexed/IndexedAStarPathFinder.java
index 54777d39..275c2c0b 100644
--- a/gdx-ai/src/com/badlogic/gdx/ai/pfa/indexed/IndexedAStarPathFinder.java
+++ b/gdx-ai/src/com/badlogic/gdx/ai/pfa/indexed/IndexedAStarPathFinder.java
@@ -43,6 +43,12 @@
* to the information we need). Unfortunately, we can't get rid of the open list because we still need to be able to retrieve the
* element with the lowest cost. However, we use a {@link BinaryHeap} for the open list in order to keep performance as high as
* possible.
+ *
+ * This class defaults to comparing {@code N} nodes by reference equality, but you can customize this behavior by
+ * passing a {@link StopCondition} to the constructor, such as a {@link EqualsMethodStopCondition} to use the equals()
+ * method on each node for comparisons. You can also set {@link #stopCondition} after construction, though typically
+ * before finding a path. More elaborate implementations are possible that can stop pathfinding under specific
+ * conditions, like a path moving off-screen.
*
* @param Type of node
*
@@ -52,6 +58,12 @@ public class IndexedAStarPathFinder implements PathFinder {
NodeRecord[] nodeRecords;
BinaryHeap> openList;
NodeRecord current;
+ /**
+ * A {@link StopCondition} with the same node type as this IndexedAStarPathFinder;
+ * defaults to a {@link EqualsByReferenceStopCondition} if unspecified. This is used
+ * to determine whether two N nodes are equal or some other condition means the search
+ * must stop at that point.
+ */
public StopCondition stopCondition;
public Metrics metrics;
@@ -68,10 +80,15 @@ public IndexedAStarPathFinder (IndexedGraph graph) {
@SuppressWarnings("unchecked")
public IndexedAStarPathFinder (IndexedGraph graph, boolean calculateMetrics) {
+ this(graph, calculateMetrics, new EqualsByReferenceStopCondition());
+ }
+
+ @SuppressWarnings("unchecked")
+ public IndexedAStarPathFinder (IndexedGraph graph, boolean calculateMetrics, StopCondition stopCondition) {
this.graph = graph;
this.nodeRecords = (NodeRecord[])new NodeRecord[graph.getNodeCount()];
this.openList = new BinaryHeap<>();
- this.stopCondition = new EqualsByReferenceStopCondition<>();
+ this.stopCondition = stopCondition;
if (calculateMetrics) this.metrics = new Metrics();
}
@@ -343,6 +360,11 @@ public void reset () {
}
/** This interface is used to define criteria to interrupt the search.
+ * Normally, equality is fine as a criterion, but some situations may use
+ * additional or different criteria, such as a maximum distance from a point
+ * being reached, or the path moving off-screen.
+ *
+ * If you use Java 8 or higher, you can use a lambda as a StopCondition.
*
* @param Type of node
*
@@ -352,14 +374,29 @@ public interface StopCondition {
}
/** Default implementation of {@link StopCondition}, which compares two given nodes by reference.
- *
+ * User code typically doesn't need to create one of these StopConditions, because
+ * {@link IndexedAStarPathFinder#IndexedAStarPathFinder(IndexedGraph, boolean)} creates one by default.
+ *
* @param Type of node
*
* @author niemandkun */
- private static class EqualsByReferenceStopCondition implements StopCondition {
+ public static class EqualsByReferenceStopCondition implements StopCondition {
@Override
public boolean shouldStopSearch(N currentNode, N endNode) {
return currentNode == endNode;
}
}
+ /** A {@link StopCondition} which compares two given nodes by calling {@code currentNode.equals(endNode)}
+ * if currentNode is not null. If currentNode is null, then this always returns false (meaning a null
+ * node is not equal to anything, including a null endNode).
+ *
+ * @param Type of node
+ *
+ * @author tommyettinger */
+ public static class EqualsMethodStopCondition implements StopCondition {
+ @Override
+ public boolean shouldStopSearch(N currentNode, N endNode) {
+ return currentNode != null && currentNode.equals(endNode);
+ }
+ }
}
diff --git a/gdx-ai/tests/com/badlogic/gdx/ai/pfa/indexed/IndexedAStarPathFinderTest.java b/gdx-ai/tests/com/badlogic/gdx/ai/pfa/indexed/IndexedAStarPathFinderTest.java
index eb03ac26..b87df595 100644
--- a/gdx-ai/tests/com/badlogic/gdx/ai/pfa/indexed/IndexedAStarPathFinderTest.java
+++ b/gdx-ai/tests/com/badlogic/gdx/ai/pfa/indexed/IndexedAStarPathFinderTest.java
@@ -220,13 +220,8 @@ public MyNode getNewInstance(MyNode old) {
}
);
- final IndexedAStarPathFinder pathfinder = new IndexedAStarPathFinder<>(dynamicGraph);
- pathfinder.stopCondition = new IndexedAStarPathFinder.StopCondition() {
- @Override
- public boolean shouldStopSearch(MyNode currentNode, MyNode endNode) {
- return currentNode.equals(endNode);
- }
- };
+ final IndexedAStarPathFinder pathfinder = new IndexedAStarPathFinder<>(dynamicGraph, false,
+ new IndexedAStarPathFinder.EqualsMethodStopCondition());
final GraphPath outPath = new DefaultGraphPath<>();