示例#1
0
  // HQ or pastr calls this function to spend spare bytecodes computing paths for soldiers
  public static void work(MapLocation dest, int priority, int bytecodeLimit)
      throws GameActionException {
    int page = findFreePage(dest, priority);
    Debug.indicate("path", 1, "BFS Pathing to " + dest.toString() + "; using page " + page);
    if (page == -1) return; // We can't do any work, or don't have to

    if (!dest.equals(previousDest)) {
      Debug.indicate("path", 0, "BFS initingQueue");
      initQueue(dest);
    } else {
      Debug.indicate("path", 0, "BFS queue already inited");
    }

    previousDest = dest;
    previousRoundWorked = Clock.getRoundNum();
    previousPage = page;

    int mapWidth = rc.getMapWidth();
    int mapHeight = rc.getMapHeight();
    MapLocation enemyHQ = rc.senseEnemyHQLocation();
    boolean destInSpawn = dest.distanceSquaredTo(enemyHQ) <= 25;

    while (locQueueHead != locQueueTail && Clock.getBytecodeNum() < bytecodeLimit) {
      // pop a location from the queue
      MapLocation loc = locQueue[locQueueHead];
      locQueueHead++;

      int locX = loc.x;
      int locY = loc.y;
      for (int i = 8; i-- > 0; ) {
        int x = locX + dirsX[i];
        int y = locY + dirsY[i];
        if (x >= 0 && y >= 0 && x < mapWidth && y < mapHeight && !wasQueued[x][y]) {
          MapLocation newLoc = new MapLocation(x, y);
          if (rc.senseTerrainTile(newLoc) != TerrainTile.VOID
              && (destInSpawn || !Bot.isInTheirHQAttackRange(newLoc))) {
            publishResult(page, newLoc, dest, dirs[i]);

            // push newLoc onto queue
            locQueue[locQueueTail] = newLoc;
            locQueueTail++;
            wasQueued[x][y] = true;
          }
        }
      }
    }

    boolean finished = locQueueHead == locQueueTail;
    Debug.indicate("path", 2, "BFS finished = " + finished + "; locQueueHead = " + locQueueHead);
    writePageMetadata(page, Clock.getRoundNum(), dest, priority, finished);
  }