SHARE
TWEET

Untitled

a guest Oct 24th, 2015 106 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. final class Pathfinder {
  2.         private static ArrayList<ArrayList<Node>> nodes;
  3.         private static final PriorityQueue<Node> openList = new PriorityQueue<Node>(new Comparator<Node>() {
  4.                 @Override
  5.                 public int compare(Node o1, Node o2) {
  6.                         if (o1.getF() < o2.getF()) {
  7.                                 return 1;
  8.                         } else if (o1.getF() > o2.getF()) {
  9.                                 return 2;
  10.                         }
  11.                         return 0;
  12.                 }
  13.         });
  14.         private static final ArrayList<Node> closedList = new ArrayList<Node>();
  15.         private Pathfinder() {
  16.         }
  17.         public static Stack<Vector2> getPath(final float startX, final float startY, final float endX, final float endY) {
  18.                 nodes = new ArrayList<ArrayList<Node>>();
  19.                 openList.clear();
  20.                 closedList.clear();
  21.                 convertTilesToNodes(Level.physicsToTileX(endX), Level.physicsToTileY(endY));
  22.                 final Node startNode = nodes.get(Level.physicsToTileX(startX)).get(Level.physicsToTileY(startY));
  23.                 final Node endNode = nodes.get(Level.physicsToTileX(endX)).get(Level.physicsToTileY(endY));
  24.                 openList.add(startNode);
  25.                 while (!openList.isEmpty()) {
  26.                         final Node currentNode = openList.poll();
  27.                         if (currentNode == endNode) {
  28.                                 final Stack<Vector2> path = new Stack<Vector2>();
  29.                                 Node pathNode = currentNode;
  30.                                 while (pathNode != null) {
  31.                                         path.push(Level.tileToPhysicsPosition(pathNode.x, pathNode.y));
  32.                                         pathNode = pathNode.parent;
  33.                                 }
  34.                                 return path;
  35.                         }
  36.                         checkAdjacentNodes(currentNode);
  37.                 }
  38.                 throw new NullPointerException();
  39.         }
  40.         private static void convertTilesToNodes(final int endX, final int endY) {
  41.                 final int[][] tiles = Level.getTiles();
  42.                 for (int i = 0; i < Level.getWidth(); i++) {
  43.                         final ArrayList<Node> verticalStrip = new ArrayList<Node>();
  44.                         for (int j = 0; j < Level.getHeight(); j++) {
  45.                                 if (tiles[i][j] == 0) {
  46.                                         verticalStrip.add(null);
  47.                                 } else {
  48.                                         verticalStrip.add(new Node(i, j, calculateCost(i, j, endX, endY)));
  49.                                 }
  50.                         }
  51.                         nodes.add(verticalStrip);
  52.                 }
  53.         }
  54.         private static void checkAdjacentNodes(final Node parentNode) {
  55.                 closedList.add(parentNode);
  56.                 for (int i = Math.max(parentNode.x - 1, 0); i <= Math.min(parentNode.x + 1, Level.getWidth() - 1); i++) {
  57.                         for (int j = Math.max(parentNode.y - 1, 0); j <= Math.min(parentNode.y + 1, Level.getHeight() - 1); j++) {
  58.                                 final Node currentNode = nodes.get(i).get(j);
  59.                                 if (currentNode != null && !closedList.contains(currentNode)) {
  60.                                         if (openList.contains(currentNode)) {
  61.                                                 if (parentNode.g + calculateCost(parentNode.x, parentNode.y, currentNode.x, currentNode.y) < currentNode.g) {
  62.                                                         currentNode.g = parentNode.g + calculateCost(parentNode.x, parentNode.y, currentNode.x, currentNode.y);
  63.                                                         currentNode.parent = parentNode;
  64.                                                 }
  65.                                         } else {
  66.                                                 currentNode.g = parentNode.g + calculateCost(parentNode.x, parentNode.y, currentNode.x, currentNode.y);
  67.                                                 currentNode.parent = parentNode;
  68.                                                 openList.add(currentNode);
  69.                                         }
  70.                                 }
  71.                         }
  72.                 }
  73.         }
  74.         private static double calculateCost(final int nodeOneX, final int nodeOneY, final int nodeTwoX, final int nodeTwoY) {
  75.                 final int nonDiagonalCost = 10;
  76.                 final int diagonalCost = 14;
  77.                 final int dX = Math.abs(nodeTwoX - nodeOneX);
  78.                 final int dY = Math.abs(nodeTwoY - nodeOneY);
  79.                 return nonDiagonalCost * (dX + dY) + (diagonalCost - 2 * nonDiagonalCost) * Math.min(dX, dY);
  80.         }
  81.         private static final class Node {
  82.                 private final int x;
  83.                 private final int y;
  84.                 private double g;
  85.                 private final double h;
  86.                 private Node parent;
  87.                 private Node(final int x, final int y, final double h) {
  88.                         this.x = x;
  89.                         this.y = y;
  90.                         this.h = h;
  91.                 }
  92.                 private double getF() {
  93.                         return g + h;
  94.                 }
  95.         }
  96. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top