Example #1
0
  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);
    }
  }
Example #2
0
 /*
  * (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);
 }