Guest User

Pathfinding in game (Android)

a guest
Jan 20th, 2014
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.21 KB | None | 0 0
  1.     private void calcGoing() {
  2.         listClosedGo.clear();// откуда можно атаковать
  3.         listClosedGoShot.clear();// откуда можно атаковать
  4.         ArrayList<Node> listOpen = new ArrayList<Node>();// стек
  5.         int haveP = currentUnit.getPassability();
  6.         int currP;
  7.         int maxShot = currentUnit.getShotMaxPassability();
  8.         Node startNode = new Node(null, currentUnit.getX(), currentUnit.getY(), 0);
  9.         listOpen.add(startNode);
  10.         int x, y;
  11.         while (!listOpen.isEmpty()) {
  12.             Node n = listOpen.get(0);
  13.             listOpen.remove(n);
  14.             x = n.getX();
  15.             y = n.getY();
  16.             currP = n.getPassability();
  17.             if (currP > haveP) {
  18.                 continue;// нить окончена
  19.             }
  20.             if (maxShot >= currP) {
  21.                 if (listClosedGoShot.contains(n)) {
  22.                     Node exist = listClosedGoShot.get(listClosedGoShot.indexOf(n));
  23.                     if (exist.getPassability() > n.getPassability()) {
  24.                         listClosedGoShot.remove(exist);
  25.                         listClosedGoShot.add(n);
  26.                     } else {
  27.                         continue;
  28.                     }
  29.                 } else if (listClosedGo.contains(n)) {
  30.                     listClosedGo.remove(n);
  31.                     listClosedGoShot.add(n);
  32.                 } else {
  33.                     Unit u = getUnit(x, y);// получаем юнита на указанной клетке или null, если нет никого
  34.                     if (u != null && u != currentUnit) {
  35.                         continue;
  36.                     } else {
  37.                         listClosedGoShot.add(n);
  38.                     }
  39.                 }
  40.             } else {
  41.                 if (listClosedGoShot.contains(n)) {
  42.                     continue;
  43.                 }
  44.                 if (listClosedGo.contains(n)) {
  45.                     Node exist = listClosedGo.get(listClosedGo.indexOf(n));
  46.                     if (exist.getPassability() > n.getPassability()) {
  47.                         listClosedGo.remove(exist);
  48.                         listClosedGo.add(n);
  49.                     } else {
  50.                         continue;
  51.                     }
  52.                 } else {
  53.                     Unit u = getUnit(x, y);
  54.                     if (u != null && u != currentUnit) {
  55.                         continue;
  56.                     } else {
  57.                         listClosedGo.add(n);
  58.                     }
  59.                 }
  60.             }
  61.             int[] newx = {x, x, x - 1, x + 1};
  62.             int[] newy = {y - 1, y + 1, y, y};
  63.             for (int i = 0; i < 4; i++) {
  64.                 int tmpx = newx[i];
  65.                 int tmpy = newy[i];
  66.                 if (tmpx < 0 || tmpx > wTiles - 1 || tmpy < 0 || tmpy > hTiles - 1) {
  67.                     continue;
  68.                 } else {
  69.                     int addP = currentUnit.calcPassability(gameMap.getTileCode(tmpx, tmpy));// считаем проходимость клетки-кандидата на исследование
  70.                     Node addNode = new Node(n, tmpx, tmpy, currP + addP);
  71.                     listOpen.add(addNode);
  72.                 }
  73.             }
  74.         }
  75.         invalidate();// рисуем, можно все в Path добавить и рисовать
  76.     }
  77.  
  78. /**
  79.  *А вот так ходить на выбранную клетку по самому оптимальному пути:
  80.  */
  81.  
  82. [spoiler][code=java]
  83.     private void goTo(int x, int y) {
  84.         ArrayList<Node> path = new ArrayList<Node>();
  85.         ArrayList<Node> listAll = new ArrayList<Node>(listClosedGoShot);
  86.         listAll.addAll(listClosedGo);
  87.         Node curr = listAll.get(listAll.indexOf(new Node(null, x, y, 0)));
  88. //      currentUnit.changePassability(curr.getPassability());
  89.         path.add(curr);
  90.         Node tmp;
  91.         while ((tmp = curr.getParent()) != null) {
  92.             path.add(tmp);
  93.             curr = tmp;
  94.         }
  95.         path.remove(path.size() - 1);// юнит и так уже находится в начале пути
  96.         Collections.reverse(path);
  97.         currentUnit.setMove(path);// идем
  98.     }
Advertisement
Add Comment
Please, Sign In to add comment