private static void archonMicro(RobotController rc, RobotInfo enemyArchon)
      throws GameActionException {
    if (rc.isCoreReady()) {
      int dist = myLoc.distanceSquaredTo(enemyArchon.location);
      // When not adjacent to the archon, walk/mine through to him.
      if (dist > 2) {
        Direction desired = myLoc.directionTo(enemyArchon.location);
        Direction dir = Movement.getBestMoveableDirection(desired, rc, 2);
        if (dir != Direction.NONE) {
          rc.move(dir);
        } else if (shouldMine(rc, desired)) {
          rc.clearRubble(desired);
        } else if (shouldMine(rc, desired.rotateLeft())) {
          rc.clearRubble(desired.rotateLeft());
        } else if (shouldMine(rc, desired.rotateRight())) {
          rc.clearRubble(desired.rotateRight());
        }
      }
      // When adjacent to archon, rotate to the left/right when possible.
      else {
        Direction dir = myLoc.directionTo(enemyArchon.location);
        if (rc.canMove(dir.rotateLeft())) {
          rc.move(dir.rotateLeft());
        } else if (rc.canMove(dir.rotateRight())) {
          rc.move(dir.rotateRight());
        }
      }
    }

    if (rc.isWeaponReady()) {
      if (rc.canAttackLocation(enemyArchon.location)) {
        rc.attackLocation(enemyArchon.location);
      }
    }
  }
Example #2
0
 private void tryClearRubble(Direction direction) throws GameActionException {
   MapLocation nextLocation = rc.getLocation().add(direction);
   double rubble = rc.senseRubble(nextLocation);
   if (rubble > 100) {
     rc.clearRubble(direction);
   }
 }
Example #3
0
  // move to a maplocation
  public static void moveToLocation(MapLocation m) throws GameActionException {
    MapLocation currentLoc = rc.getLocation();
    Direction directionToM = currentLoc.directionTo(m);
    Direction actualDirectionToMove = directionToM;

    // deal with the slug trail - trim it to size
    if (past10Locations.size() > 10) {
      while (past10Locations.size() > 10) {
        past10Locations.remove(past10Locations.size() - 1);
      }
    }
    past10Locations.add(currentLoc);

    MapLocation locationToMoveTo = currentLoc.add(directionToM);

    if (canMoveThere(
        actualDirectionToMove,
        locationToMoveTo)) // make sure it's not part of the slug trail and it's not blocked by
                           // rubble and you can move there
    {
      moveInDirection(actualDirectionToMove);
    } else {
      // first, check if you should remove rubble. only if the surrounding squares are empty
      // boolean shouldRemoveRubble = false;
      int directionsWithRubble = 0;
      for (Direction d : Direction.values()) {
        MapLocation added = goal.add(d);
        boolean isFullOfRubble = rc.senseRubble(added) > 50;
        if (isFullOfRubble) {
          directionsWithRubble++;
        }
      }
      if (directionsWithRubble > 2) // if it's surrounded then dig
      {
        if (rc.isCoreReady()) {
          if (actualDirectionToMove.equals(Direction.OMNI) == false) {
            rc.clearRubble(actualDirectionToMove);
          }
        }
      } else // if not, path around it
      {
        Direction right = actualDirectionToMove.rotateRight();
        MapLocation rightLoc = currentLoc.add(right);

        while (right.equals(actualDirectionToMove) == false) {
          if (canMoveThere(right, rightLoc)) {
            moveInDirection(right);
            right = actualDirectionToMove;
          } else {
            right = right.rotateRight();
            rightLoc = currentLoc.add(right);
          }
        }
      }
    }
  }
  public static void clear_rubble() throws GameActionException {
    if (!rc.isCoreReady()) return;
    if (Scanner.can_see_targets()) return;
    if (Scanner.there_is_hidden_enemy()) return;

    Direction direction_to_clear = current_location.directionTo(destination);

    if (direction_to_clear == Direction.NONE || direction_to_clear == Direction.OMNI) return;

    if (rc.senseRubble(current_location.add(direction_to_clear)) < 1) return;

    rc.clearRubble(direction_to_clear);
    rc.setIndicatorString(1, "Clearing: " + direction_to_clear.toString());
  }
Example #5
0
  protected void tryMoveToward(MapLocation location) throws GameActionException {
    MapLocation currentLocation = rc.getLocation();
    Direction moveDirection = currentLocation.directionTo(location);

    if (location.isAdjacentTo(currentLocation)) {
      double rubble = rc.senseRubble(location);
      if (rubble >= 100) {
        rc.clearRubble(moveDirection);
        return;
      }
    }

    tryMove(moveDirection);
  }
Example #6
0
  // move in a given direction
  public static void moveInDirection(Direction d) throws GameActionException {
    MapLocation m = rc.getLocation().add(d);

    if (rc.senseRubble(m) < tooMuchRubble
        || Math.random() < probClearRubbleAnyways) // if it's less than 20, just move there
    {
      if (rc.isCoreReady() && rc.canMove(d)) {
        rc.move(d);
      }
    } else // clear it
    {
      if (rc.isCoreReady()) {
        rc.clearRubble(d);
      }
    }
  }
Example #7
0
 /**
  * Finds adjacent with lowest rubble (greater than 0) and clears it.
  *
  * @param rc
  * @return
  * @throws GameActionException
  */
 private static void clearRubble(RobotController rc) throws GameActionException {
   MapLocation myLocation = rc.getLocation();
   MapLocation[] adjacentLocations =
       MapLocation.getAllMapLocationsWithinRadiusSq(myLocation, ONE_SQUARE_RADIUS);
   int lowestRubbleIndex = -1;
   for (int i = 0; i < adjacentLocations.length; i++) {
     double rubble = rc.senseRubble(adjacentLocations[i]);
     if (lowestRubbleIndex == -1) {
       lowestRubbleIndex = rubble > 0 ? i : lowestRubbleIndex;
     } else {
       lowestRubbleIndex =
           rubble > 0 && rubble < rc.senseRubble(adjacentLocations[lowestRubbleIndex])
               ? i
               : lowestRubbleIndex;
     }
   }
   if (lowestRubbleIndex >= 0) {
     Direction dir = myLocation.directionTo(adjacentLocations[lowestRubbleIndex]);
     if (rc.isCoreReady() && !dir.equals(Direction.OMNI) && !dir.equals(Direction.NONE)) {
       rc.clearRubble(dir);
     }
   }
 }
Example #8
0
  public static void forwardishIfProtection(Direction movingDirection2) throws GameActionException {

    boolean stationArchon = RobotPlayer.stationArchon;
    // boolean tempRoamingArchon = RobotPlayer.tempRoamingArchon;
    if (stationArchon) return; // or do other stuff
    int id = RobotPlayer.id;
    // System.out.println(Integer.toString(id));

    // int waitTurns = 1;

    RobotController rc = RobotPlayer.rc;

    int waitTurns = rc.getType() == RobotType.ARCHON ? 1 : 1;
    // if(rc.getRoundNum()%waitTurns ==0){// leader let the soldiers catch up
    if (true) {
      for (int i : possibleDirections) {
        Direction candidateDirection = Direction.values()[(movingDirection2.ordinal() + i + 8) % 8];
        MapLocation candidateLocation =
            rc.getLocation().add(candidateDirection.dx, candidateDirection.dy);
        if (patient > 0) {
          //					if (rc.getType() == RobotType.ARCHON)
          //						System.out.println("should be  an archon moving now");

          MapLocation oldLoc = rc.getLocation();
          if (rc.canMove(candidateDirection)
              && !pastLocations.contains(candidateLocation)
              && enoughProtectionAhead(candidateLocation)) {

            if (!pastLocations.contains(oldLoc)) {

              pastLocations.add(rc.getLocation());
            }
            if (pastLocations.size() > 5) pastLocations.remove(0);

            rc.move(candidateDirection);
            if (rc.getType() == RobotType.ARCHON && !RobotPlayer.stationArchon) {
              // System.out.println("Im getting this far");
              // System.out.println("I should have moved in direction: "+
              // candidateDirection.toString());
            }

            // if (rc.getType() == RobotTypse.ARCHON)
            //	System.out.println("should have moved an archon new location " +
            // rc.getLocation().toString()+ "old : " + oldLoc) ;

            patient = Math.min(patient + 1, 30);
            return;
          }
        } else {

          if (rc.canMove(candidateDirection) && enoughProtectionAhead(candidateLocation)) {
            rc.move(candidateDirection);
            patient = Math.min(patient + 1, 30);
            return;
          } else { // dig !
            if (rc.senseRubble(candidateLocation) > GameConstants.RUBBLE_OBSTRUCTION_THRESH) {
              rc.clearRubble(candidateDirection);
              return;
            }
          }
        }
      }
      patient = patient - 5;
    }
  }
  private static void rushMicro(RobotController rc, RobotInfo[] hostiles)
      throws GameActionException {
    // Prioritizes attacking turrets.
    RobotInfo bestEnemy = null;
    boolean canAttackBestEnemy = false;
    int bestEnemyDist = 10000; // only care if can't hit
    for (RobotInfo hostile : hostiles) {
      // Can attack this enemy.
      int dist = myLoc.distanceSquaredTo(hostile.location);
      // Summary:
      // Prioritizes enemies over zombies.
      // Prioritizes turret enemies over other enemies.
      // Prioritizes lowest health enemy last
      if (dist <= attackRadius) {
        if (bestEnemy != null) {
          if (bestEnemy.team == enemyTeam) { // best is already enemy
            if (hostile.team == enemyTeam) { // found an enemy
              if (countsAsTurret(bestEnemy.type)) {
                if (countsAsTurret(hostile.type)) {
                  // Take lowest health
                  if (bestEnemy.health > hostile.health) bestEnemy = hostile;
                }
              } else {
                if (countsAsTurret(hostile.type)) {
                  bestEnemy = hostile;
                } else {
                  // Take lowest health
                  if (bestEnemy.health > hostile.health) bestEnemy = hostile;
                }
              }
            }
          } else { // best is not an enemy!
            if (hostile.team == enemyTeam) { // found an enemy
              bestEnemy = hostile;
            } else {
              // Take lowest health
              if (bestEnemy.health > hostile.health) bestEnemy = hostile;
            }
          }
        } else {
          bestEnemy = hostile;
        }
        canAttackBestEnemy = true;
      } else {
        // Only update best enemy if you can't attack best enemy
        if (!canAttackBestEnemy) {
          if (bestEnemy != null) {
            // Pick the closest one
            if (bestEnemyDist > dist) {
              bestEnemyDist = dist;
              bestEnemy = hostile;
            }
          } else {
            bestEnemyDist = dist;
            bestEnemy = hostile;
          }
        }
      }
    }
    rc.setIndicatorString(0, "Round: " + rc.getRoundNum() + ", Best enemy: " + bestEnemy);
    if (rc.isCoreReady()) {
      // If there is a best enemy, attack him.
      if (bestEnemy != null) {
        // Move closer only if blocking someone.
        if (rc.canAttackLocation(bestEnemy.location)) {
          if (isBlockingSomeone(rc, bestEnemy.location)) {
            Direction desired = myLoc.directionTo(bestEnemy.location);
            Direction dir = Movement.getBestMoveableDirection(desired, rc, 2);
            if (dir != Direction.NONE) {
              rc.move(dir);
            } else if (shouldMine(rc, desired)) {
              rc.clearRubble(desired);
            } else if (shouldMine(rc, desired.rotateLeft())) {
              rc.clearRubble(desired.rotateLeft());
            } else if (shouldMine(rc, desired.rotateRight())) {
              rc.clearRubble(desired.rotateRight());
            }
          }
        }
        // If can't attack it, move closer!
        else {
          Direction desired = myLoc.directionTo(bestEnemy.location);
          Direction dir = Movement.getBestMoveableDirection(desired, rc, 2);
          if (dir != Direction.NONE) {
            rc.move(dir);
          } else if (shouldMine(rc, desired)) {
            rc.clearRubble(desired);
          } else if (shouldMine(rc, desired.rotateLeft())) {
            rc.clearRubble(dir.rotateLeft());
          } else if (shouldMine(rc, desired.rotateRight())) {
            rc.clearRubble(desired.rotateRight());
          }
        }
      }
      // Otherwise move closer to destination
      else {
        if (currentDestination != null) {
          RobotInfo info = null;
          if (rc.canSenseLocation(currentDestination)) {
            info = rc.senseRobotAtLocation(currentDestination);
          }
          if (info != null) {
            // If can attack it, just only move closer if blocking someone behind.
            if (rc.canAttackLocation(info.location)) {
              if (isBlockingSomeone(rc, currentDestination)) {
                Direction desired = myLoc.directionTo(currentDestination);
                Direction dir = Movement.getBestMoveableDirection(desired, rc, 2);
                if (dir != Direction.NONE) {
                  rc.move(dir);
                } else if (shouldMine(rc, desired)) {
                  rc.clearRubble(desired);
                } else if (shouldMine(rc, desired.rotateLeft())) {
                  rc.clearRubble(desired.rotateLeft());
                } else if (shouldMine(rc, desired.rotateRight())) {
                  rc.clearRubble(desired.rotateRight());
                }
              }
            }
            // If can't attack it, move closer!
            else {
              Direction desired = myLoc.directionTo(currentDestination);
              Direction dir = Movement.getBestMoveableDirection(desired, rc, 2);
              if (dir != Direction.NONE) {
                rc.move(dir);
              } else if (shouldMine(rc, desired)) {
                rc.clearRubble(desired);
              } else if (shouldMine(rc, desired.rotateLeft())) {
                rc.clearRubble(desired.rotateLeft());
              } else if (shouldMine(rc, desired.rotateRight())) {
                rc.clearRubble(desired.rotateRight());
              }
            }
          }
          // If not there, just move closer.
          else {
            Direction desired = myLoc.directionTo(currentDestination);
            Direction dir = Movement.getBestMoveableDirection(desired, rc, 2);
            if (dir != Direction.NONE) {
              rc.move(dir);
            } else if (shouldMine(rc, desired)) {
              rc.clearRubble(desired);
            } else if (shouldMine(rc, desired.rotateLeft())) {
              rc.clearRubble(desired.rotateLeft());
            } else if (shouldMine(rc, desired.rotateRight())) {
              rc.clearRubble(desired.rotateRight());
            }
          }
        }
      }
    }

    // Attack whenever you can.
    if (bestEnemy != null) {
      if (rc.isWeaponReady()) {
        if (rc.canAttackLocation(bestEnemy.location)) {
          broadcastingAttack(rc, bestEnemy);
        }
      }
    }
  }
  public static void zombieMicro(RobotController rc) throws GameActionException {
    boolean thereIsNonKitableZombie = false;
    RobotInfo closestEnemy = null;
    int closestDist = 10000;
    for (RobotInfo hostile : nearbyEnemies) {
      if (hostile.type == RobotType.FASTZOMBIE || hostile.type == RobotType.RANGEDZOMBIE) {
        thereIsNonKitableZombie = true;
      }
      int dist = myLoc.distanceSquaredTo(hostile.location);
      if (dist < closestDist) {
        closestDist = dist;
        closestEnemy = hostile;
      }
    }

    Direction d = myLoc.directionTo(closestEnemy.location);
    // if we're too close, move further away
    if (myLoc.distanceSquaredTo(closestEnemy.location) < 5 && rc.isCoreReady()) {
      Direction desired = d.opposite();
      Direction dir = Movement.getBestMoveableDirection(desired, rc, 1);
      if (dir != Direction.NONE) {
        rc.move(dir);
      } else if (shouldMine(rc, desired)) {
        rc.clearRubble(desired);
      } else if (shouldMine(rc, desired.rotateLeft())) {
        rc.clearRubble(desired.rotateLeft());
      } else if (shouldMine(rc, desired.rotateRight())) {
        rc.clearRubble(desired.rotateRight());
      }
    }
    if (!thereIsNonKitableZombie) {
      // Only move in closer if there is no non-kitable zombie
      if (myLoc.distanceSquaredTo(closestEnemy.location) > attackRadius
          && rc.isCoreReady()) { // if we are too far, we want to move closer
        // Desired direction is d.
        Direction dir = Movement.getBestMoveableDirection(d, rc, 1);
        if (dir != Direction.NONE) {
          rc.move(dir);
        } else if (shouldMine(rc, d)) {
          rc.clearRubble(d);
        } else if (shouldMine(rc, d.rotateLeft())) {
          rc.clearRubble(d.rotateLeft());
        } else if (shouldMine(rc, d.rotateRight())) {
          rc.clearRubble(d.rotateRight());
        } else { // probably meaning you are blocked by allies
          if (closestEnemy.type == RobotType.ZOMBIEDEN) {
            // It is likely that we wanted to go to that den, but possibly coincidence
            // If not a coincidence, bug there.
            if (bugging != null) {
              if (bugging.destination.equals(closestEnemy.location)) {
                bugging.turretAvoidMove(turretLocations);
                // If coincidence, set new bugging.
              } else {
                bugging = new Bugging(rc, closestEnemy.location);
                bugging.turretAvoidMove(turretLocations);
              }
            } else {
              bugging = new Bugging(rc, closestEnemy.location);
              bugging.turretAvoidMove(turretLocations);
            }
          }
        }
      }
    }
    if (rc.isWeaponReady() && rc.canAttackLocation(closestEnemy.location)) {
      broadcastingAttack(rc, closestEnemy);
    }
  }
  private static void luringMicro(RobotController rc) throws GameActionException {
    boolean thereIsNonKitableZombie = false;
    RobotInfo closestEnemy = null;
    MapLocation closestOpponent = null;
    int closestDist = 10000;
    for (RobotInfo hostile : nearbyEnemies) {
      if (hostile.type == RobotType.FASTZOMBIE || hostile.type == RobotType.RANGEDZOMBIE) {
        thereIsNonKitableZombie = true;
      }
      int dist = myLoc.distanceSquaredTo(hostile.location);
      if (dist < closestDist) {
        closestDist = dist;
        closestEnemy = hostile;
      }
    }

    // try to get the closest place to lure zombie
    for (MapLocation loc : turretLocations) {
      if (closestOpponent == null) closestOpponent = loc;
      else if (myLoc.distanceSquaredTo(loc) < myLoc.distanceSquaredTo(closestOpponent))
        closestOpponent = null;
    }
    if (closestOpponent == null) closestOpponent = nearestEnemyLocation;
    Direction d = null;
    if (closestOpponent != null) d = myLoc.directionTo(closestOpponent);
    else d = myLoc.directionTo(rc.getInitialArchonLocations(enemyTeam)[0]);

    // if we are moving directly into the zombie, try to move to the side
    Direction temp = myLoc.directionTo(closestEnemy.location);
    if (d.equals(temp)) d = d.rotateLeft().rotateLeft();
    else if (d.equals(temp.rotateLeft())) d = d.rotateLeft();
    else if (d.equals(temp.rotateRight())) d = d.rotateRight();

    // if we're too close, move further away towards the closest turret location or the closest
    // enemy
    if (myLoc.distanceSquaredTo(closestEnemy.location) < 10 && rc.isCoreReady()) {
      Direction desired = d;
      Direction dir = Movement.getBestMoveableDirection(desired, rc, 1);
      if (dir != Direction.NONE) {
        rc.move(dir);
      } else if (shouldMine(rc, desired)) {
        rc.clearRubble(desired);
      } else if (shouldMine(rc, desired.rotateLeft())) {
        rc.clearRubble(desired.rotateLeft());
      } else if (shouldMine(rc, desired.rotateRight())) {
        rc.clearRubble(desired.rotateRight());
      }
    }
    if (!thereIsNonKitableZombie) {
      // Only move in closer if there is no non-kitable zombie
      if (myLoc.distanceSquaredTo(closestEnemy.location) > attackRadius
          && rc.isCoreReady()) { // if we are too far, we want to move closer
        // Desired direction is d.
        Direction dir = Movement.getBestMoveableDirection(d, rc, 1);
        if (dir != Direction.NONE) {
          rc.move(dir);
        } else if (shouldMine(rc, d)) {
          rc.clearRubble(d);
        } else if (shouldMine(rc, d.rotateLeft())) {
          rc.clearRubble(d.rotateLeft());
        } else if (shouldMine(rc, d.rotateRight())) {
          rc.clearRubble(d.rotateRight());
        } else { // probably meaning you are blocked by allies
          if (closestEnemy.type == RobotType.ZOMBIEDEN) {
            // It is likely that we wanted to go to that den, but possibly coincidence
            // If not a coincidence, bug there.
            if (bugging != null) {
              if (bugging.destination.equals(closestEnemy.location)) {
                bugging.turretAvoidMove(turretLocations);
                // If coincidence, set new bugging.
              } else {
                bugging = new Bugging(rc, closestOpponent);
                bugging.turretAvoidMove(turretLocations);
              }
            } else {
              bugging = new Bugging(rc, closestOpponent);
              bugging.turretAvoidMove(turretLocations);
            }
          }
        }
      }
    }
    if (rc.isWeaponReady() && rc.canAttackLocation(closestEnemy.location)) {
      broadcastingAttack(rc, closestEnemy);
    }
  }
Example #12
0
  public int bugMove(RobotController rc, MapLocation end) {
    try {
      MapLocation curLoc = rc.getLocation();
      int dist = curLoc.distanceSquaredTo(end);
      Direction dir = curLoc.directionTo(end);

      for (int i = 0; i < prevDistanceN; i++) {
        if (dist == prevDistanceN) {
          if (rc.isCoreReady()) {
            if (rc.senseRobotAtLocation(curLoc.add(dir)) == null) {
              rc.clearRubble(dir);
              for (int j = prevDistanceN - 1; j > 0; j--) {
                prevDistance[j] = prevDistance[j - 1];
              }
            }
          }
          return dist;
        }
      }
      for (int j = prevDistanceN - 1; j > 0; j--) {
        prevDistance[j] = prevDistance[j - 1];
      }
      // Direction dir = start.directionTo(end);

      int c = 0;
      while (!rc.canMove(dir)) {
        dir = dir.rotateRight();
        if (c == 3) {
          dir = dir.rotateRight();
          c++;
        }
        if (c > 6) {
          break;
        }
        c++;
      }
      if (c < 7) {
        if (rc.isCoreReady()) {
          rc.move(dir);
          return dist;
        }
      } else {
        if (rc.isCoreReady()) {
          MapLocation nextLoc = curLoc.add(dir);
          if (rc.onTheMap(nextLoc)) {
            if (rc.senseRobotAtLocation(nextLoc) == null) {
              rc.clearRubble(dir);
              for (int j = prevDistanceN - 1; j > 0; j--) {
                prevDistance[j] = prevDistance[j - 1];
              }
            }
          }
        }
        return dist;
      }
    } catch (Exception e) {
      System.out.println(e.getMessage());
      e.printStackTrace();
    }
    return 99999;
  }
Example #13
0
  public static void run(RobotController rc) {
    int myAttackRange = 0;
    Random rand = new Random(rc.getID());
    Team myTeam = rc.getTeam();
    Team enemyTeam = myTeam.opponent();

    try {
      // Any code here gets executed exactly once at the beginning of the game.
      myAttackRange = rc.getType().attackRadiusSquared;
    } catch (Exception e) {
      // Throwing an uncaught exception makes the robot die, so we need to catch exceptions.
      // Caught exceptions will result in a bytecode penalty.
      System.out.println(e.getMessage());
      e.printStackTrace();
    }

    while (true) {
      // This is a loop to prevent the run() method from returning. Because of the Clock.yield()
      // at the end of it, the loop will iterate once per game round.
      try {
        int fate = rand.nextInt(1000);

        if (fate % 5 == 3) {
          // Send a normal signal
          rc.broadcastSignal(80);
        }

        boolean shouldAttack = false;

        // If this robot type can attack, check for enemies within range and attack one
        if (myAttackRange > 0) {
          RobotInfo[] enemiesWithinRange = rc.senseNearbyRobots(myAttackRange, enemyTeam);
          RobotInfo[] zombiesWithinRange = rc.senseNearbyRobots(myAttackRange, Team.ZOMBIE);
          if (enemiesWithinRange.length > 0) {
            shouldAttack = true;
            // Check if weapon is ready
            if (rc.isWeaponReady()) {
              rc.attackLocation(
                  enemiesWithinRange[rand.nextInt(enemiesWithinRange.length)].location);
            }
          } else if (zombiesWithinRange.length > 0) {
            shouldAttack = true;
            // Check if weapon is ready
            if (rc.isWeaponReady()) {
              rc.attackLocation(
                  zombiesWithinRange[rand.nextInt(zombiesWithinRange.length)].location);
            }
          }
        }

        if (!shouldAttack) {
          if (rc.isCoreReady()) {
            if (fate < 600) {
              // Choose a random direction to try to move in
              Direction dirToMove = RobotPlayer.directions[fate % 8];
              // Check the rubble in that direction
              if (rc.senseRubble(rc.getLocation().add(dirToMove))
                  >= GameConstants.RUBBLE_OBSTRUCTION_THRESH) {
                // Too much rubble, so I should clear it
                rc.clearRubble(dirToMove);
                // Check if I can move in this direction
              } else if (rc.canMove(dirToMove)) {
                // Move
                rc.move(dirToMove);
              }
            }
          }
        }

        Clock.yield();
      } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
      }
    }
  }
Example #14
0
  /**
   * run() is the method that is called when a robot is instantiated in the Battlecode world. If
   * this method returns, the robot dies!
   */
  @SuppressWarnings("unused")
  public static void run(RobotController rc) {
    // You can instantiate variables here.
    Direction[] directions = {
      Direction.NORTH,
      Direction.NORTH_EAST,
      Direction.EAST,
      Direction.SOUTH_EAST,
      Direction.SOUTH,
      Direction.SOUTH_WEST,
      Direction.WEST,
      Direction.NORTH_WEST
    };
    RobotType[] robotTypes = {
      RobotType.SCOUT,
      RobotType.SOLDIER,
      RobotType.SOLDIER,
      RobotType.SOLDIER,
      RobotType.GUARD,
      RobotType.GUARD,
      RobotType.VIPER,
      RobotType.TURRET
    };
    Random rand = new Random(rc.getID());
    int myAttackRange = 0;
    Team myTeam = rc.getTeam();
    Team enemyTeam = myTeam.opponent();

    if (rc.getType() == RobotType.ARCHON) {
      try {
        // Any code here gets executed exactly once at the beginning of the game.
      } catch (Exception e) {
        // Throwing an uncaught exception makes the robot die, so we need to catch exceptions.
        // Caught exceptions will result in a bytecode penalty.
        System.out.println(e.getMessage());
        e.printStackTrace();
      }

      while (true) {
        /*
            // This is a loop to prevent the run() method from returning. Because of the Clock.yield()
            // at the end of it, the loop will iterate once per game round.
            try {
                int fate = rand.nextInt(1000);
                // Check if this ARCHON's core is ready
                if (fate % 10 == 2) {
                    // Send a message signal containing the data (6370, 6147)
                    rc.broadcastMessageSignal(6370, 6147, 80);
                }
                Signal[] signals = rc.emptySignalQueue();
                if (signals.length > 0) {
                    // Set an indicator string that can be viewed in the client
                    rc.setIndicatorString(0, "I received a signal this turn!");
                } else {
                    rc.setIndicatorString(0, "I don't any signal buddies");
                }
                if (rc.isCoreReady()) {
                    if (fate < 800) {
                        // Choose a random direction to try to move in
                        Direction dirToMove = directions[fate % 8];
                        // Check the rubble in that direction
                        if (rc.senseRubble(rc.getLocation().add(dirToMove)) >= GameConstants.RUBBLE_OBSTRUCTION_THRESH) {
                            // Too much rubble, so I should clear it
                            rc.clearRubble(dirToMove);
                            // Check if I can move in this direction
                        } else if (rc.canMove(dirToMove)) {
                            // Move
                            rc.move(dirToMove);
                        }
                    } else {
                        // Choose a random unit to build
                        RobotType typeToBuild = robotTypes[fate % 8];
                        // Check for sufficient parts
                        if (rc.hasBuildRequirements(typeToBuild)) {
                            // Choose a random direction to try to build in
                            Direction dirToBuild = directions[rand.nextInt(8)];
                            for (int i = 0; i < 8; i++) {
                                // If possible, build in this direction
                                if (rc.canBuild(dirToBuild, typeToBuild)) {
                                    rc.build(dirToBuild, typeToBuild);
                                    break;
                                } else {
                                    // Rotate the direction to try
                                    dirToBuild = dirToBuild.rotateLeft();
                                }
                            }
                        }
                    }
                }

                Clock.yield();
            } catch (Exception e) {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        */
      }
    } else if (rc.getType() != RobotType.TURRET) {
      try {
        // Any code here gets executed exactly once at the beginning of the game.
        myAttackRange = rc.getType().attackRadiusSquared;
      } catch (Exception e) {
        // Throwing an uncaught exception makes the robot die, so we need to catch exceptions.
        // Caught exceptions will result in a bytecode penalty.
        System.out.println(e.getMessage());
        e.printStackTrace();
      }

      while (true) {
        // This is a loop to prevent the run() method from returning. Because of the Clock.yield()
        // at the end of it, the loop will iterate once per game round.
        try {
          int fate = rand.nextInt(1000);

          if (fate % 5 == 3) {
            // Send a normal signal
            rc.broadcastSignal(80);
          }

          boolean shouldAttack = false;

          // If this robot type can attack, check for enemies within range and attack one
          if (myAttackRange > 0) {
            RobotInfo[] enemiesWithinRange = rc.senseNearbyRobots(myAttackRange, enemyTeam);
            RobotInfo[] zombiesWithinRange = rc.senseNearbyRobots(myAttackRange, Team.ZOMBIE);
            if (enemiesWithinRange.length > 0) {
              shouldAttack = true;
              // Check if weapon is ready
              if (rc.isWeaponReady()) {
                rc.attackLocation(
                    enemiesWithinRange[rand.nextInt(enemiesWithinRange.length)].location);
              }
            } else if (zombiesWithinRange.length > 0) {
              shouldAttack = true;
              // Check if weapon is ready
              if (rc.isWeaponReady()) {
                rc.attackLocation(
                    zombiesWithinRange[rand.nextInt(zombiesWithinRange.length)].location);
              }
            }
          }

          if (!shouldAttack) {
            if (rc.isCoreReady()) {
              if (fate < 600) {
                // Choose a random direction to try to move in
                Direction dirToMove = directions[fate % 8];
                // Check the rubble in that direction
                if (rc.senseRubble(rc.getLocation().add(dirToMove))
                    >= GameConstants.RUBBLE_OBSTRUCTION_THRESH) {
                  // Too much rubble, so I should clear it
                  rc.clearRubble(dirToMove);
                  // Check if I can move in this direction
                } else if (rc.canMove(dirToMove)) {
                  // Move
                  rc.move(dirToMove);
                }
              }
            }
          }

          Clock.yield();
        } catch (Exception e) {
          System.out.println(e.getMessage());
          e.printStackTrace();
        }
      }
    } else if (rc.getType() == RobotType.TURRET) {
      try {
        myAttackRange = rc.getType().attackRadiusSquared;
      } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
      }

      while (true) {
        // This is a loop to prevent the run() method from returning. Because of the Clock.yield()
        // at the end of it, the loop will iterate once per game round.
        try {
          // If this robot type can attack, check for enemies within range and attack one
          if (rc.isWeaponReady()) {
            RobotInfo[] enemiesWithinRange = rc.senseNearbyRobots(myAttackRange, enemyTeam);
            RobotInfo[] zombiesWithinRange = rc.senseNearbyRobots(myAttackRange, Team.ZOMBIE);
            if (enemiesWithinRange.length > 0) {
              for (RobotInfo enemy : enemiesWithinRange) {
                // Check whether the enemy is in a valid attack range (turrets have a minimum range)
                if (rc.canAttackLocation(enemy.location)) {
                  rc.attackLocation(enemy.location);
                  break;
                }
              }
            } else if (zombiesWithinRange.length > 0) {
              for (RobotInfo zombie : zombiesWithinRange) {
                if (rc.canAttackLocation(zombie.location)) {
                  rc.attackLocation(zombie.location);
                  break;
                }
              }
            }
          }

          Clock.yield();
        } catch (Exception e) {
          System.out.println(e.getMessage());
          e.printStackTrace();
        }
      }
    }
  }