public void visitNeighbour(AStarNode origin, int x, int y, int z) { int costFromStart = origin.getCostFromStart() + goal.getNodeCost(origin, x, y, z); int costToGoal = goal.distanceToGoal(x, y, z); int totalCost = costFromStart + costToGoal; // Check if on open/closed list // If yes, check if new path is more efficient (lower f value) // and update path if so. AStarNode foundNode = openList.findOpenNode(x, y, z); boolean foundCheaperPath = false; if (foundNode != null) { if (totalCost < foundNode.getTotalCost()) { foundCheaperPath = true; openList.removeNodeFromOpenList(foundNode); } } else { foundNode = closedList.findClosedNode(x, y, z); if (foundNode != null) { if (totalCost < foundNode.getTotalCost()) { foundCheaperPath = true; closedList.removeNodeFromClosedList(foundNode); } } else { // Node not found in open and closed list, thus put a // new one on the open list. foundNode = map.nodeAt(x, y, z); foundCheaperPath = true; } } if (foundCheaperPath) { foundNode.initialize(origin, costFromStart, costToGoal, totalCost, x, y, z); openList.addNodeToOpenList(foundNode); } }
/* * (non-Javadoc) * * @see machine.AStarMachine#setUpSearchSpace(algorithm.AStarMapBase, * algorithm.AStarGoalBase) */ public void setUpSearchSpace(AStarMapBase<AStarNode> newMap, AStarGoalBase<AStarNode> newGoal) { if (newMap == null) throw new IllegalArgumentException("The map to search may not be null."); if (newGoal == null) throw new IllegalArgumentException("The goal object may not be null."); map = newMap; openList.setMap(map); closedList.setMap(map); goal = newGoal; goal.setNodeStorage(openList); }
/* * (non-Javadoc) * * @see machine.AStarMachine#findPath() */ public List<AStarNode> findPath( int startX, int startY, int startZ, int endX, int endY, int endZ) { if (map == null) throw new IllegalStateException( "The searched map must be specified before path search can be done."); if (this.goal == null) throw new IllegalStateException( "The goal object must be specified before path search can be done."); AStarNode goalNode = map.nodeAt(endX, endY, endZ); int goalDistance = this.goal.getNodeCost(goalNode, startX, startY, startZ); goalNode.initialize(null, goalDistance, 0, goalDistance, endX, endY, endZ); this.goal.setGoalNode(goalNode); AStarNode startNode = map.nodeAt(startX, startY, startZ); startNode.initialize(null, 0, goalDistance, goalDistance, startX, startY, startZ); openList.addNodeToOpenList(startNode); return AStar.findPath(openList, closedList, map, this.goal, nodePool, this); }