Beispiel #1
0
 private LinkedList<Vector2f> astar(Field2D field, boolean flag) {
   for (; pathes.size() > 0; ) {
     ScoredPath spath = pathes.remove(0);
     Vector2f current = spath.path.get(spath.path.size() - 1);
     if (current.equals(goal)) {
       return spath.path;
     }
     ArrayList<Vector2f> list = field.neighbors(current, flag);
     int size = list.size();
     for (int i = 0; i < size; i++) {
       Vector2f next = list.get(i);
       if (visitedCache.contains(next)) {
         continue;
       }
       visitedCache.add(next);
       if (!field.isHit(next) && !flying) {
         continue;
       }
       LinkedList<Vector2f> path = new LinkedList<Vector2f>(spath.path);
       path.add(next);
       int score = spath.score + field.score(goal, next);
       insert(score, path);
     }
   }
   return null;
 }
Beispiel #2
0
 public LinkedList<Vector2f> findPath() {
   if (start == null) {
     start = new Vector2f(startX, startY);
   } else {
     start.set(startX, startY);
   }
   if (over == null) {
     over = new Vector2f(endX, endY);
   } else {
     over.set(endX, endY);
   }
   return calc(field, start, over, flag);
 }
Beispiel #3
0
 public static LinkedList<Vector2f> find(
     int[][] maps, int x1, int y1, int x2, int y2, boolean flag) {
   if (start == null) {
     start = new Vector2f(x1, y1);
   } else {
     start.set(x1, y1);
   }
   if (over == null) {
     over = new Vector2f(x2, y2);
   } else {
     over.set(x2, y2);
   }
   return find(maps, start, over, flag);
 }
Beispiel #4
0
 private LinkedList<Vector2f> calc(Field2D field, Vector2f start, Vector2f goal, boolean flag) {
   if (start.equals(goal)) {
     LinkedList<Vector2f> v = new LinkedList<Vector2f>();
     v.add(start);
     return v;
   }
   this.goal = goal;
   if (visitedCache == null) {
     visitedCache = new HashSet<Vector2f>();
   } else {
     visitedCache.clear();
   }
   if (pathes == null) {
     pathes = new LinkedList<ScoredPath>();
   } else {
     pathes.clear();
   }
   visitedCache.add(start);
   if (path == null) {
     path = new LinkedList<Vector2f>();
   } else {
     path.clear();
   }
   path.add(start);
   if (spath == null) {
     spath = new ScoredPath(0, path);
   } else {
     spath.score = 0;
     spath.path = path;
   }
   pathes.add(spath);
   return astar(field, flag);
 }
Beispiel #5
0
 public static LinkedList<Vector2f> find(
     Field2D maps, int x1, int y1, int x2, int y2, boolean flag) {
   if (astar == null) {
     astar = new AStarFinder();
   }
   if (start == null) {
     start = new Vector2f(x1, y1);
   } else {
     start.set(x1, y1);
   }
   if (over == null) {
     over = new Vector2f(x2, y2);
   } else {
     over.set(x2, y2);
   }
   return astar.calc(maps, start, over, flag);
 }