Fixed the astar algoritm and added some comments
This commit is contained in:
parent
c356ddf327
commit
e280861f2d
14 changed files with 776 additions and 1365 deletions
|
|
@ -10,6 +10,7 @@ import ei.game.algo.AStarNeighbour.Location;
|
|||
/**
|
||||
*
|
||||
* @author Árni Arent
|
||||
* @author Ziver Koc
|
||||
*
|
||||
*/
|
||||
public class AStarPathfinder{
|
||||
|
|
@ -17,6 +18,7 @@ public class AStarPathfinder{
|
|||
protected LinkedList<AStarNode> closed;
|
||||
private AStarNode start;
|
||||
private AStarNode goal;
|
||||
private AStarNode closest;
|
||||
|
||||
/**
|
||||
* Constructs a Pathfinder.
|
||||
|
|
@ -55,6 +57,10 @@ public class AStarPathfinder{
|
|||
node = (AStarNode) open.removeFirst();
|
||||
node.setVisited(true);
|
||||
node.setVisitOrder(order++);
|
||||
// calculates the nearest way to the destination
|
||||
if(closest == null || closest.estimatedCostToGoal > node.estimatedCostToGoal){
|
||||
closest = node;
|
||||
}
|
||||
|
||||
// Check if we found the goal.
|
||||
if (node == goal) {
|
||||
|
|
@ -78,12 +84,12 @@ public class AStarPathfinder{
|
|||
float costFromStart = node.costFromStart + node.getCost(neighbourNode, location);
|
||||
boolean inClosed = closed.contains(neighbourNode);
|
||||
boolean inOpen = open.contains(neighbourNode);
|
||||
|
||||
|
||||
if ((!inOpen && !inClosed) || costFromStart < neighbourNode.costFromStart) {
|
||||
neighbourNode.setParent(node);
|
||||
neighbourNode.costFromStart = costFromStart;
|
||||
neighbourNode.estimatedCostToGoal = neighbourNode.getEstimatedCostTo(goal,start);
|
||||
|
||||
|
||||
if (inClosed) {
|
||||
closed.remove(neighbourNode);
|
||||
}
|
||||
|
|
@ -96,9 +102,9 @@ public class AStarPathfinder{
|
|||
closed.add(node);
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("Path Not found!!");
|
||||
//return null;
|
||||
return constructPath(node);
|
||||
return constructPath(closest);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue