Пример #1
0
 public void InitAttackClan() {
   _attackClanList.clear();
 }
Пример #2
0
  /**
   * Run the thread. This function keeps running as long as the path finder thread is working. It
   * will sleep as long as the the path finder is idle. To quit this function use the {@link
   * #saveShutdown()} function.
   */
  @SuppressWarnings("null")
  @Override
  public void run() {
    Location searchStartLoc = null;
    Location searchEndLoc = null;
    boolean searching = false;
    PathReceiver searchReceiver = null;
    int maxDepth = 0;
    boolean outputPath = false;
    while (running) {
      if (!searching && !restart) {
        synchronized (this) {
          try {
            this.wait();
          } catch (final InterruptedException e) {
            // nothing
          }
        }
        continue;
      }

      if (restart) {
        if (searchStartLoc == null) {
          searchStartLoc = Location.getInstance();
        }
        if (searchEndLoc == null) {
          searchEndLoc = Location.getInstance();
        }
        if (open == null) {
          open = FastTable.newInstance();
          open.setValueComparator(FastComparator.IDENTITY);
        }
        if (closed == null) {
          closed = FastTable.newInstance();
        }
        restart = false;
        searchStartLoc.set(startLoc);
        searchEndLoc.set(endLoc);
        searchReceiver = receiver;
        if (searchReceiver == null) {
          continue;
        }
        searching = true;

        PathNode.clearCache();
        closed.clear();
        open.clear();
        maxDepth = 0;
        outputPath = false;

        if (PathNode.getNode(searchEndLoc).isBlocked()) {
          searching = false;
          continue;
        }

        addToOpen(PathNode.getNode(searchStartLoc));
      }

      if (outputPath) {
        outputPath = false;
        searching = false;
        if (PathNode.getNode(searchEndLoc).getParent() == null) {
          continue;
        }

        final Path resultPath = new Path();
        PathNode currentNode = PathNode.getNode(searchEndLoc);
        final PathNode searchNode = PathNode.getNode(searchStartLoc);
        while (!currentNode.equals(searchNode)) {
          resultPath.prependStep(currentNode);
          currentNode = currentNode.getParent();
        }
        resultPath.prependStep(searchNode);
        searchReceiver.handlePath(resultPath);
        searchStartLoc.recycle();
        searchStartLoc = null;
        searchEndLoc.recycle();
        searchEndLoc = null;
        FastTable.recycle(open);
        open = null;
        FastTable.recycle(closed);
        closed = null;
        continue;
      }

      if ((maxDepth > MAX_LENGTH) || open.isEmpty()) {
        searching = false;
        continue;
      }

      final PathNode currentNode = open.get(0);
      maxDepth = currentNode.getDepth();
      final Location currentLoc = currentNode.getLocation();

      if (currentNode.getLocation().equals(searchEndLoc)) {
        outputPath = true;
        continue;
      }

      removeFromOpen(currentNode);
      addToClosed(currentNode);

      final Location searchLoc = Location.getInstance();
      for (int dir = 0; dir < Location.DIR_MOVE8; ++dir) {
        searchLoc.set(currentLoc);
        searchLoc.moveSC8(dir);
        final PathNode searchNode = PathNode.getNode(searchLoc);

        if (!searchNode.isBlocked()) {
          int newMoveCost = currentNode.getCost() + searchNode.getValue();

          if ((dir == Location.DIR_NORTHEAST)
              || (dir == Location.DIR_NORTHWEST)
              || (dir == Location.DIR_SOUTHEAST)
              || (dir == Location.DIR_SOUTHWEST)) {
            newMoveCost *= 1.1f;
          }

          if (newMoveCost < searchNode.getCost()) {
            if (searchNode.getOpen()) {
              removeFromOpen(searchNode);
            }
            if (searchNode.getClosed()) {
              removeFromClosed(searchNode);
            }
          }

          if (!searchNode.getOpen() && !searchNode.getClosed()) {
            searchNode.setCost(newMoveCost);
            searchNode.setParent(currentNode);

            maxDepth = Math.max(maxDepth, searchNode.getDepth());

            searchNode.setHeuristic(MIN_MOVE_COST * searchLoc.getDistance(searchEndLoc));

            addToOpen(searchNode);
          }
        }
      }
      searchLoc.recycle();
      open.sort();
    }
  }