Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.1 kB
0
Indexable
Never
protected void findShortestWay() {
        int[][] dis = new int[spritesMap.getHeight()][spritesMap.getWidth()];
        Pair[][] last = new Pair[spritesMap.getHeight()][spritesMap.getWidth()];
        for (int i = 0; i < spritesMap.getHeight(); i++) {
            for (int j = 0; j < spritesMap.getWidth(); j++) {
                dis[i][j] = Integer.MAX_VALUE;
            }
        }

        Deque<Pair> way = new ArrayDeque<>();
        way.addLast(new Pair(enemy.getXInMap(), enemy.getYInMap()));
        dis[enemy.getYInMap()][enemy.getXInMap()] = 0;

        while (!way.isEmpty()) {
            Pair cur = way.pop();
            if (spritesMap.getMap()[cur.getY()][cur.getX()].getTypeSprite(PLAYER)) {
                while (!cur.equals(new Pair(enemy.getXInMap(), enemy.getYInMap()))) {
                    nextStep = cur;
                    cur = last[cur.getY()][cur.getX()];
                }
                break;
            }

            for (Direction direct : Direction.values()) {
                int X = cur.getX() + Map.dx[direct.ordinal()];
                int Y = cur.getY() + Map.dy[direct.ordinal()];
                if (spritesMap.checkSquareInMap(X, Y) && dis[Y][X] == Integer.MAX_VALUE) {
                    dis[Y][X] = dis[cur.getY()][Map.getX()] + 1;
                    if (dis[Y][X] <= 1) {
                        if ((!spritesMap.checkDanger(X, Y) && spritesMap.getMap()[Y][X].checkNotExist(new TypeSprite[]{EXPLODE, WALL, BOX}))) {
                            way.addLast(new Pair(X, Y));
                            last[Y][X] = new Pair(cur.getX(), cur.getY());
                        }
                    } else {
                        if (!spritesMap.getMap()[Y][X].getTypeSprite(WALL)) {
                            way.addLast(new Pair(X, Y));
                            last[Y][X] = new Pair(cur.getX(), cur.getY());
                        }
                    }
                }
            }
        }
    }

    // find way to avoid bomb if this enemy is threatened
    protected boolean avoidBomb(int safe) {
        boolean[][] visited = new boolean[spritesMap.getHeight()][spritesMap.getWidth()];
        Pair[][] last = new Pair[spritesMap.getHeight()][spritesMap.getWidth()];
        Deque<Pair> way = new ArrayDeque<>();
        way.addLast(new Pair(enemy.getXInMap(), enemy.getYInMap()));

        while (!way.isEmpty()) {
            Pair cur = way.pop();
            boolean existWay = true;
            if (safe >= 1) {
                if ((cur.getX() == enemy.getXInMap() && Math.abs(cur.getY() - enemy.getYInMap()) <= enemy.powerBombProperty().getValue())
                        || (cur.getY() == enemy.getYInMap() && Math.abs(cur.getX() - enemy.getXInMap()) <= enemy.powerBombProperty().getValue())) {
                    existWay = false;
                }
            }

            if (spritesMap.checkDanger(cur.getX(), cur.getY())) {
                existWay = false;
            }

            if (existWay) {
                while (!cur.equals(new Pair(enemy.getXInMap(), enemy.getYInMap()))) {
                    nextStep = cur;
                    cur = last[cur.getY()][cur.getX()];
                }
                return true;
            }

            visited[cur.getY()][cur.getX()] = true;
            for (int i = 0; i < Direction.values().length; ++i) {
                int X = cur.getX() + Map.dx[i];
                int Y = cur.getY() + Map.dy[i];
                if (spritesMap.checkSquareInMap(X, Y) && !visited[Y][X]
                        && spritesMap.getMap()[Y][X].checkNotExist(new TypeSprite[]{BOMB, BOX, EXPLODE, WALL})) {
                    if ((safe >= 1 && spritesMap.checkDanger(X, Y)) ||
                            (safe >= 2 && spritesMap.getMap()[Y][X].getTypeSprite(PLAYER))) {
                        continue;
                    }

                    way.addLast(new Pair(X, Y));
                    last[Y][X] = new Pair(cur.getX(), cur.getY());
                }
            }
        }
        return false;
    }