source: lost-perception/astar/AStarSearch.java

Last change on this file was ebd3538, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 6 years ago

Initial commit. This codebase for the Lost Perception game was created by decompiling code from a jar file.

  • Property mode set to 100644
File size: 3.4 KB
Line 
1package astar;
2
3import java.util.ListIterator;
4import java.util.Iterator;
5import java.awt.geom.Point2D;
6import java.util.LinkedList;
7import java.awt.Point;
8
9public class AStarSearch
10{
11 private static int base;
12
13 static {
14 AStarSearch.base = 0;
15 }
16
17 public static LinkedList<Point> findPath(final Point source, final Point dest, final AStarMap map) {
18 final BinaryHeap openList = new BinaryHeap();
19 LinkedList<Point> newPath = null;
20 final AStarNode sourceNode = map.getNode(source.x, source.y);
21 final AStarNode destNode = map.getNode(dest.x, dest.y);
22 if (!destNode.passable) {
23 return newPath;
24 }
25 sourceNode.prev = null;
26 sourceNode.G = 0;
27 sourceNode.H = (int)sourceNode.getMidpoint().distance(destNode.getMidpoint());
28 openList.add(sourceNode);
29 sourceNode.curList = AStarSearch.base + 1;
30 while (destNode.curList != AStarSearch.base + 2 && !openList.isEmpty()) {
31 final AStarNode smallest = openList.removeSmallest();
32 smallest.curList = AStarSearch.base + 2;
33 for (final AStarNode curChild : smallest.neighbors) {
34 if (curChild.curList != AStarSearch.base + 2) {
35 if (!curChild.passable) {
36 continue;
37 }
38 if (curChild.curList == AStarSearch.base + 1) {
39 final int newG = smallest.G + (int)smallest.getMidpoint().distance(curChild.getMidpoint());
40 if (newG >= curChild.G) {
41 continue;
42 }
43 curChild.G = newG;
44 curChild.prev = smallest;
45 openList.update(curChild);
46 }
47 else {
48 curChild.prev = smallest;
49 curChild.G = curChild.prev.G + (int)curChild.prev.getMidpoint().distance(curChild.getMidpoint());
50 curChild.H = (int)curChild.getMidpoint().distance(destNode.getMidpoint());
51 curChild.curList = AStarSearch.base + 1;
52 openList.add(curChild);
53 }
54 }
55 }
56 }
57 if (destNode.curList == AStarSearch.base + 2) {
58 AStarNode cur = destNode;
59 newPath = new LinkedList<Point>();
60 newPath.addFirst(dest);
61 while (cur != null) {
62 newPath.addFirst(cur.getMidpoint());
63 cur = cur.prev;
64 }
65 newPath.addFirst(source);
66 flattenPath(newPath, map);
67 }
68 AStarSearch.base += 2;
69 return newPath;
70 }
71
72 private static void flattenPath(final LinkedList<Point> path, final AStarMap map) {
73 final int size = path.size();
74 if (size < 3) {
75 return;
76 }
77 final ListIterator<Point> iter = path.listIterator();
78 Point prev = iter.next();
79 iter.next();
80 while (iter.hasNext()) {
81 final Point next = iter.next();
82 if (map.validPath(prev, next)) {
83 iter.previous();
84 iter.previous();
85 iter.remove();
86 iter.next();
87 }
88 if (iter.hasNext()) {
89 prev = next;
90 iter.next();
91 }
92 }
93 if (path.size() == size) {
94 return;
95 }
96 flattenPath(path, map);
97 }
98}
Note: See TracBrowser for help on using the repository browser.