List of usage examples for com.badlogic.gdx.utils BinaryHeap pop
public T pop()
From source file:com.bladecoder.engine.pathfinder.AStarPathFinder.java
License:Apache License
@Override public boolean findPath(Object mover, N startNode, N targetNode, NavPath<N> out) { this.mover = mover; distance = 0;/*from w w w.j a v a 2s . c o m*/ if (isBlocked(targetNode, targetNode)) return false; checkedID++; if (checkedID < 0) checkedID = 1; BinaryHeap<AStarAlgoData> openList = this.openList; AStarHeuristicCalculator<N> heuristicCalculator = this.heuristicCalculator; int maxSearchDistance = this.maxSearchDistance; openList.clear(); addToOpenList(getAlgoData(startNode)); getAlgoData(targetNode); AStarAlgoData currentData = null; int maxDepth = 0; while (maxDepth < maxSearchDistance && openList.size != 0) { AStarAlgoData lastData = currentData; currentData = openList.pop(); currentData.open = false; distance = currentData.depth; currentData.closed = true; if (currentData.node == targetNode && lastData != null && !isBlocked(lastData.node, targetNode)) break; float currentCost = currentData.cost; for (N neighborNode : currentData.node.neighbors) { AStarAlgoData neighborData = getAlgoData(neighborNode); if (!isBlocked(currentData.node, neighborNode)) { sourceNodeInContext = startNode; float nextStepCost = currentCost + graph.getCost(this, neighborNode); if (nextStepCost < neighborData.cost) { if (neighborData.open) { openList.remove(neighborData); neighborData.open = false; } neighborData.closed = false; } if (!neighborData.open && !neighborData.closed) { neighborData.cost = nextStepCost; neighborData.heuristic = heuristicCalculator.getCost(this, mover, neighborNode, targetNode); neighborData.depth = currentData.depth + 1; neighborNode.parent = currentData.node; maxDepth = Math.max(maxDepth, neighborData.depth); addToOpenList(neighborData); } } } } boolean pathFound = targetNode.parent != null; if (pathFound) out.fill(startNode, targetNode); return pathFound; }
From source file:com.vlaaad.dice.game.world.util.AStar.java
License:Open Source License
public static Array<Coordinate> search(World grid, WorldObject startObject, WorldObject endObject, boolean near) { Node start = Pools.obtain(Node.class).set(startObject.getX(), startObject.getY()); Node end = Pools.obtain(Node.class).set(endObject.getX(), endObject.getY()); nodes.put(start.x, start.y, start);/* ww w. j a v a2 s . c o m*/ nodes.put(end.x, end.y, end); BinaryHeap<Node> openHeap = new BinaryHeap<Node>(); openHeap.add(start); List<Node> neighbours = new LinkedList<Node>(); Node nearNode = null; boolean searchNear = true; while (openHeap.size > 0) { Node current = openHeap.pop(); int cx = current.x - end.x; int cy = current.y - end.y; if (cx == 0 && cy == 0) { Array<Coordinate> result = new Array<Coordinate>(); while (current.parent != null) { result.add(Coordinate.obtain(current.x, current.y)); current = current.parent; } result.reverse(); cleanUp(); return result; } if (near && searchNear && !isValid(grid, end.x, end.y)) { nearNode = current; if (Math.abs(cx) <= 1 && Math.abs(cy) <= 1) { searchNear = false; } } current.closed = true; neighbours.clear(); int gScore = current.g + 1; addNeighbours(grid, neighbours, current); for (Node neighbour : neighbours) { if (neighbour.closed) continue; if (!neighbour.visited || gScore < neighbour.g) { neighbour.parent = current; int dx = neighbour.x - end.x; int dy = neighbour.y - end.y; neighbour.h = (float) (Math.sqrt(dx * dx + dy * dy)); neighbour.g = gScore; neighbour.f = neighbour.g + neighbour.h; if (!neighbour.visited) { openHeap.add(neighbour); } openHeap.setValue(neighbour, neighbour.f); neighbour.visited = true; } } } if (nearNode != null && !searchNear) { Array<Coordinate> result = new Array<Coordinate>(); while (nearNode.parent != null) { result.add(Coordinate.obtain(nearNode.x, nearNode.y)); nearNode = nearNode.parent; } result.reverse(); cleanUp(); return result; } cleanUp(); return null; }
From source file:org.bladecoder.bladeengine.pathfinder.AStarPathFinder.java
License:Apache License
@Override public boolean findPath(Object mover, NavNode startNode, NavNode targetNode, NavPath out) { this.mover = mover; distance = 0;//from ww w . j a v a2 s . co m if (isBlocked(targetNode, targetNode)) return false; checkedID++; if (checkedID < 0) checkedID = 1; BinaryHeap<AStarAlgoData> openList = this.openList; AStarHeuristicCalculator heuristicCalculator = this.heuristicCalculator; int maxSearchDistance = this.maxSearchDistance; openList.clear(); addToOpenList(getAlgoData(startNode)); getAlgoData(targetNode); AStarAlgoData currentData = null; int maxDepth = 0; while (maxDepth < maxSearchDistance && openList.size != 0) { AStarAlgoData lastData = currentData; currentData = openList.pop(); currentData.open = false; distance = currentData.depth; currentData.closed = true; if (currentData.node == targetNode && lastData != null && !isBlocked(lastData.node, targetNode)) break; float currentCost = currentData.cost; for (NavNode neighborNode : currentData.node.neighbors) { AStarAlgoData neighborData = getAlgoData(neighborNode); if (!isBlocked(currentData.node, neighborNode)) { sourceNodeInContext = startNode; float nextStepCost = currentCost + graph.getCost(this, neighborNode); if (nextStepCost < neighborData.cost) { if (neighborData.open) { openList.remove(neighborData); neighborData.open = false; } neighborData.closed = false; } if (!neighborData.open && !neighborData.closed) { neighborData.cost = nextStepCost; neighborData.heuristic = heuristicCalculator.getCost(this, mover, neighborNode, targetNode); neighborData.depth = currentData.depth + 1; neighborNode.parent = currentData.node; maxDepth = Math.max(maxDepth, neighborData.depth); addToOpenList(neighborData); } } } } boolean pathFound = targetNode.parent != null; if (pathFound) out.fill(startNode, targetNode); return pathFound; }