Beispiel #1
0
  public static void createTerrainMap() {

    // Get cow density field and map dimensions
    double cowDensMap[][] = hq.senseCowGrowth();
    int mapY = cowDensMap.length, mapX = cowDensMap[0].length;

    // Initialize terrain map array
    terrainMap = new int[mapY][mapX];

    // Scan over map to identify types of terrain at each location
    for (int i = 0; i < mapY; i++) {
      for (int j = 0; j < mapX; j++) {
        TerrainTile t = hq.senseTerrainTile(new MapLocation(j, i));
        if (t == TerrainTile.valueOf("NORMAL")) terrainMap[i][j] = NORMAL;
        else if (t == TerrainTile.valueOf("ROAD")) terrainMap[i][j] = ROAD;
        else if (t == TerrainTile.valueOf("VOID")) terrainMap[i][j] = WALL;
        else terrainMap[i][j] = OFFMAP;
      }
    }
  }
Beispiel #2
0
  public BFSNoiseTower() throws GameActionException {
    System.out.println("start " + Clock.getBytecodeNum());

    queue[0] = currentLocation;
    dir[17][17] = Direction.OMNI;

    for (int i = 7; i >= 0; i--) {
      Direction d = directions[i];
      MapLocation loc = currentLocation.add(d);
      TerrainTile there = RC.senseTerrainTile(loc);
      if (!there.isTraversableAtHeight(RobotLevel.ON_GROUND)) continue;
      int x = loc.x - currentLocation.x;
      int y = loc.y - currentLocation.y;
      dir[x + 17][y + 17] = d.opposite();
      queue[at] = loc;
      at++;
    }

    for (int s = 1; s < at; s++) {
      if (s % 30 == 0 && RC.isActive()) {
        if (target == null
            || target.distanceSquaredTo(ALLY_PASTR_COUNT > 0 ? ALLY_PASTR_LOCS[0] : currentLocation)
                <= 5) {
          earlyNearbyCows();
        }
        MapLocation realTarget = target.add(currentLocation.directionTo(target), 3);

        if (RC.canAttackSquare(realTarget)) RC.attackSquare(realTarget);

        target = target.add(target.directionTo(currentLocation));
      }

      Direction initD =
          dir[queue[s].x - currentLocation.x + 17][queue[s].y - currentLocation.y + 17];
      Direction aD;
      Direction bD;
      if (initD.isDiagonal()) {
        aD = initD.rotateLeft().rotateLeft();
        bD = initD.rotateRight();
      } else {
        aD = initD.rotateLeft().rotateLeft().rotateLeft();
        bD = initD.rotateRight().rotateRight();
      }
      for (Direction d = aD; d != bD; d = d.rotateLeft()) {
        MapLocation loc = queue[s].add(d);
        TerrainTile there = RC.senseTerrainTile(loc);
        if (!there.isTraversableAtHeight(RobotLevel.ON_GROUND)) continue;
        int x = loc.x - currentLocation.x;
        int y = loc.y - currentLocation.y;
        if (x * x + y * y > 200) continue;

        if (dir[x + 17][y + 17] == null) {
          dir[x + 17][y + 17] = d.opposite();
          queue[at] = loc;
          at++;
        }
      }
    }

    System.out.println("end " + Clock.getBytecodeNum());
    System.out.println("at " + at);
  }
Beispiel #3
0
  private boolean doWork(MapLocation dest, int priority, int stopWhen, int page)
      throws GameActionException {
    if (!dest.equals(previousDest)) {
      initQueue(dest);
      System.out.print(
          "Cleanser BFS to "
              + dest
              + ", page "
              + page
              + ", start round "
              + Clock.getRoundNum()
              + "\n");
    }

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

    int emptyCount = 0;
    while (emptyCount < NUM_QUEUES
        && qHeads[currentQ] == qTails[currentQ]) { // Skip over empty queues
      emptyCount++;
      currentQ = (currentQ + 1) % NUM_QUEUES;
    }
    if (emptyCount == NUM_QUEUES) return true; // Finished

    while (Clock.getBytecodesLeft() > stopWhen) {
      // pop a location from the queue
      int data = locQueues[qHeads[currentQ]];
      int locX = data >> 24;
      int locY = (data >> 16) & 0xff;
      double delay = (data & 0xffff) / 10;
      if (++qHeads[currentQ] % MAX_QUEUE_SIZE == 0) qHeads[currentQ] -= MAX_QUEUE_SIZE;

      for (int i = 8; i-- > 0; ) {
        int x = cropX(locX + dirsX[i]);
        int y = cropY(locY + dirsY[i]);
        boolean isDiagonal = (i <= 3);

        if (!processed[x][y]) {
          processed[x][y] = true;
          TerrainTile t = map.tile(x, y);

          if (t.isTraversable()) {
            MapLocation newLoc = new MapLocation(x, y);
            // push newLoc onto queue - pick queue according to how long the move takes
            double newDelay;
            if (isDiagonal) newDelay = diagonalDelay;
            else newDelay = moveDelay;
            int newQ = (currentQ + (int) (newDelay + (delay % 1))) % NUM_QUEUES;
            newDelay += delay;
            locQueues[qTails[newQ]] = (x << 24) | (y << 16) | (int) (newDelay * 10);
            if (++qTails[newQ] % MAX_QUEUE_SIZE == 0) qTails[newQ] -= MAX_QUEUE_SIZE;
            // System.out.print("Adding " + x + ", " + y + " to queue " + newQ + " element " +
            // qTails[newQ] + "\n");
            publishResult(page, newLoc, dest, dirs[i], (int) newDelay);
          } else if (t == TerrainTile.UNKNOWN) containsUnknowns = true;
        }
      }
      emptyCount = 0;
      while (emptyCount < NUM_QUEUES
          && qHeads[currentQ] == qTails[currentQ]) { // Skip over empty queues
        emptyCount++;
        currentQ = (currentQ + 1) % NUM_QUEUES;
      }
      if (emptyCount == NUM_QUEUES) {
        // map.dump();
        break;
      }
    }

    writePageMetadata(page, previousRoundWorked, dest, priority, (emptyCount == NUM_QUEUES));
    return (emptyCount == NUM_QUEUES && containsUnknowns == false);
  }