Guest User

Untitled

a guest
Oct 22nd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. public class BinaryPathFinder extends PathFinder {
  2. private final PriorityBuffer<PathNode> paths;
  3. private final HashMap<Point, Integer> mindists = new HashMap<Point, Integer>();
  4. private final ComparableComparator<PathNode> comparator = new ComparableComparator<PathNode>();
  5. private Path lastPath;
  6. private Point start, end;
  7. private final byte SIZE_INCREMENT = 20;
  8.  
  9. public BinaryPathFinder(PathHeuristic heuristic,
  10. Pathplayer player, PathWorld pathWorld) {
  11. super(heuristic, player, pathWorld);
  12. this.world = pathWorld.getWorld();
  13. paths = new PriorityBuffer<PathNode>(8000, comparator);
  14. }
  15.  
  16. @Override
  17. public boolean find() {
  18. try {
  19. PathNode root = new PathNode();
  20. root.point = start;
  21. calculateTotalCost(root, start, start, false);
  22. expand(root);
  23. while (true) {
  24. PathNode p = paths.remove();
  25. if (p == null)
  26. return false;
  27. Point last = p.point;
  28. if (isGoal(last)) {
  29. calculatePath(p); // Iterate back.
  30. this.mindists.clear();
  31. this.paths.clear();
  32. return true;
  33. }
  34. expand(p);
  35. }
  36. } catch (Exception e) {
  37. e.printStackTrace();
  38. }
  39. this.mindists.clear();
  40. this.paths.clear();
  41. return false;
  42. }
  43.  
  44. @Override
  45. public Path path() {
  46. return this.lastPath;
  47. }
  48.  
  49. @Override
  50. public void recalculate(Point start, Point end) {
  51. this.start = start;
  52. this.end = end;
  53. this.lastPath = null;
  54. }
  55.  
  56. private void expand(PathNode path) {
  57. Point p = path.point;
  58. Integer min = mindists.get(p);
  59. if (min == null || min > path.totalCost)
  60. mindists.put(p, path.totalCost);
  61. else
  62. return;
  63. Point[] successors = generateSuccessors(p);
  64. for (Point t : successors) {
  65. if (t == null)
  66. continue;
  67. PathNode newPath = new PathNode(path);
  68. newPath.point = t;
  69. calculateTotalCost(newPath, p, t, false);
  70. paths.add(newPath);
  71. }
  72. }
  73.  
  74. private void calculatePath(PathNode p) {
  75. Point[] retPath = new Point[20];
  76. Point[] copy = null;
  77. short added = 0;
  78. for (PathNode i = p; i != null; i = i.parent) {
  79. if (added >= retPath.length) {
  80. copy = new Point[retPath.length + SIZE_INCREMENT];
  81. System.arraycopy(retPath, 0, copy, 0, retPath.length);
  82. retPath = copy;
  83. }
  84. retPath[added++] = i.point;
  85. }
  86. this.lastPath = new Path(retPath);
  87. }
  88.  
  89. private int calculateHeuristic(Point start, Point end, boolean endPoint) {
  90. return this.heuristic.calculate(start, end, this.pathWorld,
  91. this.player, endPoint);
  92. }
  93.  
  94. private int calculateTotalCost(PathNode p, Point from, Point to,
  95. boolean endPoint) {
  96. int g = (calculateHeuristic(from, to, endPoint) + ((p.parent != null) ? p.parent.cost
  97. : 0));
  98. int h = calculateHeuristic(from, to, endPoint);
  99. p.cost = g;
  100. p.totalCost = (g + h);
  101. return p.totalCost;
  102. }
  103.  
  104. private Point[] generateSuccessors(Point point) {
  105. Point[] points = new Point[27];
  106. Point temp = null;
  107. byte counter = -1;
  108. for (int x = point.x - 1; x <= point.x + 1; ++x) {
  109. for (int y = point.y + 1; y >= point.y - 1; --y) {
  110. for (int z = point.z - 1; z <= point.z + 1; ++z) {
  111. ++counter;
  112. if (x == 0 && y == 0 && z == 0)
  113. continue;
  114. temp = new Point(x, y, z);
  115. if (valid(temp))
  116. points[counter] = temp;
  117. }
  118. }
  119. }
  120. return points;
  121. }
  122.  
  123. private boolean isGoal(Point last) {
  124. return last.equals(this.end);
  125. }
  126. }
Add Comment
Please, Sign In to add comment