示例#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;
 }
示例#2
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);
 }