List of usage examples for com.badlogic.gdx.ai.pfa DefaultGraphPath DefaultGraphPath
public DefaultGraphPath(Array<N> nodes)
From source file:us.thirdmillenium.strategicassaultsimulator.environment.Environment.java
License:Apache License
/** * Generates a mapping of all tiles that are traversable by an Agent. * * Pixel (16,16) actually matched Cell (0,0) now. * * @return an IndexedGraph for an A* implementation *//*ww w .jav a2 s . c o m*/ public static GraphPath<TileNode> createGraphFromTileMap(ConcurrentHashMap<Integer, TileNode> traverseNodes, TiledMapTileLayer wallLayer) { GraphPath<TileNode> graphPath = null; try { Array<TileNode> myNodes = new Array<TileNode>(); TileNode temp; int tilePixel = Params.MapTileSize; int halfTilePixel = Params.MapTileSize / 2; int numCellY = Params.NumCellsY; int numCellX = Params.NumCellsX; // Step 1 : Collect all open tiles, create a TileNode for it, and add for (int cellX = 0; cellX < numCellX; cellX++) { // Calculate the pixel point for this X int x = halfTilePixel + (tilePixel * cellX); // Need to reverse Y to match (0,0) being bottom left corner in LibGDX for (int cellY = 37; cellY >= 0; cellY--) { // Calculate the pixel point for this Y int y = halfTilePixel + (tilePixel * cellY); if (!wallLayer.getCell(cellX, cellY).getTile().getProperties().containsKey("blocked")) { // Generate a unique (sequential start at 0) ID for key, then generate TileNode Integer key = new Integer((cellX * numCellY) + cellY); temp = new TileNode(x, y, cellX, cellY, key); // Add node to the Array<> and the HashMap myNodes.add(temp); traverseNodes.put(key, temp); } } } // Step 2 : Build TileNode Connections TileNode node; int idTag; for (int cellX = 0; cellX < numCellX; cellX++) { for (int cellY = 0; cellY < numCellY; cellY++) { // This cell idTag = (cellX * numCellY) + cellY; node = findIndex(idTag, traverseNodes); /* * Check eight surrounding cells */ if (node != null) { // Top Left int topLeftX = cellX - 1; int topLeftY = cellY + 1; int topLeftID = (topLeftX * numCellY) + topLeftY; if (topLeftX >= 0 && topLeftY < numCellY) { temp = findIndex(topLeftID, traverseNodes); // If it exists, add to connection list for the TileNode being considered. if (temp != null) { node.addConnection(temp); } } // Left int leftX = cellX - 1; int leftY = cellY; int leftID = (leftX * numCellY) + leftY; if (leftX >= 0) { temp = findIndex(leftID, traverseNodes); // If it exists, add to connection list for the TileNode being considered. if (temp != null) { node.addConnection(temp); } } // Bottom Left int botLeftX = cellX - 1; int botLeftY = cellY - 1; int botLeftID = (botLeftX * numCellY) + botLeftY; if (botLeftX >= 0 && botLeftY >= 0) { temp = findIndex(botLeftID, traverseNodes); // If it exists, add to connection list for the TileNode being considered. if (temp != null) { node.addConnection(temp); } } // Top int topX = cellX; int topY = cellY + 1; int topID = (topX * numCellY) + topY; if (topY < numCellY) { temp = findIndex(topID, traverseNodes); // If it exists, add to connection list for the TileNode being considered. if (temp != null) { node.addConnection(temp); } } // Bottom int botX = cellX; int botY = cellY - 1; int botID = (botX * numCellY) + botY; if (botY >= 0) { temp = findIndex(botID, traverseNodes); // If it exists, add to connection list for the TileNode being considered. if (temp != null) { node.addConnection(temp); } } // Top Right int topRightX = cellX + 1; int topRightY = cellY + 1; int topRightID = (topRightX * numCellY) + topRightY; if (topRightX < numCellX && topRightY < numCellY) { temp = findIndex(topRightID, traverseNodes); // If it exists, add to connection list for the TileNode being considered. if (temp != null) { node.addConnection(temp); } } // Right int rightX = cellX + 1; int rightY = cellY; int rightID = (rightX * numCellY) + rightY; if (rightX < numCellX) { temp = findIndex(rightID, traverseNodes); // If it exists, add to connection list for the TileNode being considered. if (temp != null) { node.addConnection(temp); } } // Bottom Right int botRightX = cellX + 1; int botRightY = cellY - 1; int botRightID = (botRightX * numCellY) + botRightY; if (botRightX < numCellX && botRightY >= 0) { temp = findIndex(botRightID, traverseNodes); // If it exists, add to connection list for the TileNode being considered. if (temp != null) { node.addConnection(temp); } } } } } graphPath = new DefaultGraphPath<TileNode>(myNodes); } catch (Exception ex) { System.out.println(ex.getMessage()); } finally { return graphPath; } }
From source file:us.thirdmillenium.strategicassaultsimulator.graphics.GraphicsHelpers.java
License:Apache License
/** * Builds the Preferred Path for the 5 testing maps. * * @param testID// w w w .j a v a 2s .c om * @param prefPathNodeTracker * @param traverseNodes * @return */ public static GraphPath<TileNode> getPrefPathTest(int testID, HashSet<TileNode> prefPathNodeTracker, ConcurrentHashMap<Integer, TileNode> traverseNodes) { TileAStarPathFinder pathFinder = new TileAStarPathFinder(null); TileHeuristic heuristic = new TileHeuristic(); Array<TileNode> prefPath = new Array<TileNode>(250); Array<TileNode> partialPath; TileNode start, end; switch (testID) { case 1: // Start Bottom Left int[] x1 = { 16, 336, 16, 50, 752, 752, 338, 174, 626 }; int[] y1 = { 1198, 1134, 910, 48, 46, 272, 270, 554, 554 }; for (int i = 0; i < x1.length - 1; i++) { start = GraphicsHelpers.findTileNodeByScreenTouch(x1[i], y1[i], traverseNodes); end = GraphicsHelpers.findTileNodeByScreenTouch(x1[i + 1], y1[i + 1], traverseNodes); partialPath = new Array<TileNode>(100); pathFinder.searchNodePath2(start, end, heuristic, partialPath); prefPath.addAll(partialPath); } break; case 2: // Start Bottom Right int[] x2 = { 754, 690, 48, 48, 368 }; int[] y2 = { 1170, 76, 82, 912, 526 }; for (int i = 0; i < x2.length - 1; i++) { start = GraphicsHelpers.findTileNodeByScreenTouch(x2[i], y2[i], traverseNodes); end = GraphicsHelpers.findTileNodeByScreenTouch(x2[i + 1], y2[i + 1], traverseNodes); partialPath = new Array<TileNode>(100); pathFinder.searchNodePath2(start, end, heuristic, partialPath); prefPath.addAll(partialPath); } break; case 3: // Start Top Right int[] x3 = { 784, 48, 52, 755, 755, 430, 430, 50, 50, 300 }; int[] y3 = { 20, 140, 490, 490, 1165, 1165, 875, 875, 1160, 1164 }; for (int i = 0; i < x3.length - 1; i++) { start = GraphicsHelpers.findTileNodeByScreenTouch(x3[i], y3[i], traverseNodes); end = GraphicsHelpers.findTileNodeByScreenTouch(x3[i + 1], y3[i + 1], traverseNodes); partialPath = new Array<TileNode>(100); pathFinder.searchNodePath2(start, end, heuristic, partialPath); prefPath.addAll(partialPath); } break; case 4: // Start Top Left int[] x4 = { 16, 80, 750, 720, 210, 400 }; int[] y4 = { 16, 1165, 1165, 48, 50, 850 }; for (int i = 0; i < x4.length - 1; i++) { start = GraphicsHelpers.findTileNodeByScreenTouch(x4[i], y4[i], traverseNodes); end = GraphicsHelpers.findTileNodeByScreenTouch(x4[i + 1], y4[i + 1], traverseNodes); partialPath = new Array<TileNode>(100); pathFinder.searchNodePath2(start, end, heuristic, partialPath); prefPath.addAll(partialPath); } break; case 5: // Start Bottom Left int[] x5 = { 16, 50, 530, 80 }; int[] y5 = { 1200, 400, 630, 270 }; for (int i = 0; i < x5.length - 1; i++) { start = GraphicsHelpers.findTileNodeByScreenTouch(x5[i], y5[i], traverseNodes); end = GraphicsHelpers.findTileNodeByScreenTouch(x5[i + 1], y5[i + 1], traverseNodes); partialPath = new Array<TileNode>(100); pathFinder.searchNodePath2(start, end, heuristic, partialPath); prefPath.addAll(partialPath); } break; default: break; } // Add all nodes to HashSet tracker for fast look-up later, deduping as we go Iterator<TileNode> itr = prefPath.iterator(); while (itr.hasNext()) { TileNode tile = itr.next(); if (prefPathNodeTracker.contains(tile)) { itr.remove(); } else { prefPathNodeTracker.add(tile); } } // Return our new Graph Path return new DefaultGraphPath<TileNode>(prefPath); }