Guest User

pathfinder

a guest
Jul 18th, 2014
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. package com.streak324.pathfinding;
  2.  
  3. import java.util.Comparator;
  4. import java.util.HashSet;
  5. import java.util.PriorityQueue;
  6.  
  7. import com.badlogic.gdx.utils.Array;
  8.  
  9. public class PathFinder {
  10.  
  11. private boolean foundTarget;
  12. private int width, height;
  13. private Array<PathNode> path;
  14. private PathNode[][] nodes;
  15. private PriorityQueue<PathNode> open;
  16. private HashSet<PathNode> closed;
  17. private PathNode start, target, current;
  18. private int range = 1;
  19.  
  20. public PathFinder(int width, int height, boolean map[][]) {
  21. this.width = width;
  22. this.height = height;
  23. nodes = new PathNode[width][height];
  24.  
  25. for(int i=0; i<width; i++) {
  26. for(int j=0; j<height; j++) {
  27. nodes[i][j] = new PathNode(i, j);
  28. if(map[i][j] != true)
  29. nodes[i][j].passable = false;
  30. }
  31. }
  32. open = new PriorityQueue<PathNode>(new CostComparator());
  33. closed = new HashSet<PathNode>();
  34. }
  35.  
  36. public Array<PathNode> getPath(int sx, int sy ,int tx, int ty) {
  37. path = new Array<PathNode>();
  38. open.clear();
  39. closed.clear();
  40.  
  41. start = nodes[sx][sy];
  42. start.movementCost = 0;
  43. target = nodes[tx][ty];
  44. addToOpenList(start);
  45.  
  46. while(foundTarget != true) {
  47. if(open.size() == 0) { return null; }
  48.  
  49. current = open.poll();
  50. addToClosedList(current);
  51.  
  52. checkNeighbors(current);
  53. }
  54.  
  55. traceBack();
  56.  
  57. return path;
  58. }
  59.  
  60. private void traceBack() {
  61. while(current != start) {
  62. path.add(current);
  63. current = current.parent;
  64. }
  65. }
  66.  
  67. private void checkNeighbors(PathNode node) {
  68. for(int i = node.x - range; i <= (node.x + range); i++) {
  69. if(i >= width || i < 0) { continue; }
  70. for(int j = node.y - range; j <= (node.y + range); j++) {
  71. if( j >= height || j < 0) { continue; }
  72.  
  73. if((i == node.x && j == node.y) ) { continue; }
  74.  
  75. PathNode neighbor = nodes[i][j];
  76.  
  77. identifyNode(neighbor);
  78.  
  79. }
  80. }
  81. }
  82.  
  83. private void identifyNode(PathNode node) {
  84. if(!node.passable || closed.contains(node) ) return;
  85.  
  86. if(node == target) {
  87. foundTarget = true;
  88. System.out.println("Target Found: " + node.x + ", " + node.y);
  89. return;
  90. }
  91. else if(!open.contains(node)) {
  92. addToOpenList(node);
  93. calcHeuristic(node);
  94. updateNode(node, current);
  95. }
  96. else {
  97. checkForReparenting(node);
  98. }
  99. }
  100.  
  101. private void checkForReparenting(PathNode node) {
  102. float cost = node.movementCost;
  103. float reCost = calcMovementCost(node, current);
  104.  
  105. if(reCost < cost) {
  106. System.out.println("Reparenting");
  107. updateNode(node, current);
  108. }
  109. }
  110.  
  111. private void updateNode(PathNode child, PathNode parent) {
  112. child.parent = parent;
  113. child.movementCost = calcMovementCost(child, parent);
  114. child.totalCost = child.movementCost + child.heuristic;
  115. }
  116.  
  117. private float calcMovementCost(PathNode n1, PathNode n2) {
  118. float dx = n1.x - n2.x;
  119. float dy = n1.y - n2.y;
  120. return (float) Math.sqrt( (dx*dx + dy*dy) ) + n2.movementCost;
  121. }
  122.  
  123. private float calcHeuristic(PathNode node) {
  124. float dx = node.x - target.x;
  125. float dy = node.y - target.y;
  126. return (float) Math.sqrt( (dx*dx + dy*dy) );
  127. }
  128.  
  129. private void addToOpenList(PathNode node) {
  130. if(!open.contains(node) && !closed.contains(node)) {
  131. open.add(node);
  132. }
  133. }
  134.  
  135. private void addToClosedList(PathNode node) {
  136. if(!closed.contains(node)) {
  137. closed.add(node);
  138. }
  139. }
  140.  
  141. public class PathNode {
  142. public int x, y;
  143. public PathNode parent;
  144. public float movementCost, heuristic, totalCost;
  145. public boolean passable;
  146.  
  147. public PathNode(int x, int y) {
  148. this.x = x;
  149. this.y = y;
  150. passable = true;
  151. }
  152.  
  153. }
  154.  
  155. private class CostComparator implements Comparator<PathNode> {
  156.  
  157. @Override
  158. public int compare(PathNode a, PathNode b) {
  159.  
  160. if(a.totalCost < b.totalCost) return 1;
  161. else return -1;
  162. }
  163.  
  164. }
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment