Example usage for com.badlogic.gdx.ai.pfa Heuristic estimate

List of usage examples for com.badlogic.gdx.ai.pfa Heuristic estimate

Introduction

In this page you can find the example usage for com.badlogic.gdx.ai.pfa Heuristic estimate.

Prototype

public float estimate(N node, N endNode);

Source Link

Document

Calculates an estimated cost to reach the goal node from the given node.

Usage

From source file:us.thirdmillenium.strategicassaultsimulator.ai.tile.TileAStarPathFinder.java

License:Apache License

@Override
public boolean searchNodePath(TileNode startNode, TileNode endNode, Heuristic<TileNode> heuristic,
        GraphPath<TileNode> outPath) {
    PriorityQueue<AStarTileNode> openList = new PriorityQueue<AStarTileNode>(200,
            new AStarTileNodeComparator());
    HashSet<TileNode> closeList = new HashSet<TileNode>(250);
    HashSet<TileNode> openListTracker = new HashSet<TileNode>(250);
    AStarTileNode current, end, temp;//from ww  w.  j  av  a  2  s . co  m
    TileNode neighbor;

    // Setup A* with StartNode
    current = new AStarTileNode(startNode, 0, heuristic.estimate(startNode, endNode));
    end = new AStarTileNode(endNode, 0, 0);

    openList.add(current);

    // Begin Search
    while (openList.size() > 0) {
        // Collect closest item to the Goal
        current = openList.peek();

        // Path has been found
        if (current.equals(end)) {
            return returnNodePath(current, outPath);
        }

        // Remove current from openList, add to closeList
        current = openList.poll();
        closeList.add(current.getTileNode());

        // Loop through neighbors, adding to openList
        Array<Connection<TileNode>> adjacentCells = current.getTileNode().getConnections();

        for (int i = 0; i < adjacentCells.size; i++) {
            neighbor = adjacentCells.get(i).getToNode();

            if (!closeList.contains(neighbor) && !openListTracker.contains(neighbor)) {
                temp = new AStarTileNode(neighbor, current.getDistanceFromStart() + 1,
                        heuristic.estimate(neighbor, endNode));
                temp.setCameFrom(current);
                openList.add(temp);
                openListTracker.add(neighbor);
            }
        }
    }

    return false;
}

From source file:us.thirdmillenium.strategicassaultsimulator.ai.tile.TileAStarPathFinder.java

License:Apache License

public boolean searchNodePath2(TileNode startNode, TileNode endNode, Heuristic<TileNode> heuristic,
        Array<TileNode> outPath) {
    PriorityQueue<AStarTileNode> openList = new PriorityQueue<AStarTileNode>(200,
            new AStarTileNodeComparator());
    HashSet<TileNode> closeList = new HashSet<TileNode>(250);
    HashSet<TileNode> openListTracker = new HashSet<TileNode>(250);
    AStarTileNode current, end, temp;/*from  ww  w .jav  a 2 s  .  com*/
    TileNode neighbor;

    // Setup A* with StartNode
    current = new AStarTileNode(startNode, 0, heuristic.estimate(startNode, endNode));
    end = new AStarTileNode(endNode, 0, 0);

    openList.add(current);

    // Begin Search
    while (openList.size() > 0) {
        // Collect closest item to the Goal
        current = openList.peek();

        // Path has been found
        if (current.equals(end)) {
            return returnNodePath2(current, outPath);
        }

        // Remove current from openList, add to closeList
        current = openList.poll();
        closeList.add(current.getTileNode());

        // Loop through neighbors, adding to openList
        Array<Connection<TileNode>> adjacentCells = current.getTileNode().getConnections();

        for (int i = 0; i < adjacentCells.size; i++) {
            neighbor = adjacentCells.get(i).getToNode();

            if (!closeList.contains(neighbor) && !openListTracker.contains(neighbor)) {
                temp = new AStarTileNode(neighbor, current.getDistanceFromStart() + 1,
                        heuristic.estimate(neighbor, endNode));
                temp.setCameFrom(current);
                openList.add(temp);
                openListTracker.add(neighbor);
            }
        }
    }

    return false;
}