DFS target finding will now prioritize user given end node.
Meaning if the general area of the target is known, DFS can use it.
This commit is contained in:
parent
690abbdbe0
commit
c8715e3dbb
@ -1,7 +1,6 @@
|
||||
package ca.recrown.islandsurvivalcraft.pathfinding;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Objects;
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.Queue;
|
||||
@ -17,6 +16,7 @@ public class DepthFirstSearch {
|
||||
|
||||
public DepthFirstSearch() {
|
||||
checkedNodes = new HashSet<>();
|
||||
queue = new PriorityQueue<>();
|
||||
}
|
||||
|
||||
public DepthFirstSearch(CoordinateValidatable validatable) {
|
||||
@ -53,7 +53,7 @@ public class DepthFirstSearch {
|
||||
if (validatable == null) throw new IllegalStateException("Need to set a validator.");
|
||||
if (startNode == null) throw new IllegalStateException("Need to set the starting position.");
|
||||
|
||||
queue = new PriorityQueue<>();
|
||||
queue.clear();
|
||||
checkedNodes.clear();
|
||||
Node begin = startNode;
|
||||
queue.add(begin);
|
||||
@ -82,6 +82,7 @@ public class DepthFirstSearch {
|
||||
|
||||
/**
|
||||
* Finds the end node.
|
||||
* If an end node is set, it will try going in that direction first.
|
||||
* Will not set end node if one wasn't found,
|
||||
* therefore, previous end node data is kept.
|
||||
* @param targetValidator
|
||||
@ -92,7 +93,7 @@ public class DepthFirstSearch {
|
||||
if (targetValidator == null) throw new NullArgumentException("targetValidator");
|
||||
if (startNode == null) throw new IllegalStateException("Need to set the starting position.");
|
||||
|
||||
queue = new LinkedList<>();
|
||||
queue.clear();
|
||||
checkedNodes.clear();
|
||||
Node begin = startNode;
|
||||
queue.add(begin);
|
||||
@ -134,6 +135,7 @@ public class DepthFirstSearch {
|
||||
|
||||
@Override
|
||||
public int compareTo(Node o) {
|
||||
if (endNode == null) return 0;
|
||||
return Math.round(distanceToGoal(endNode) - o.distanceToGoal(endNode));
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user