private static void moveSoldier(RobotController rc) throws GameActionException {
   // if we have a real current destination
   rc.setIndicatorString(1, "moving somewhere " + currentDestination + rc.getRoundNum());
   if (currentDestination != null) {
     // if bugging is never initialized or we are switching destinations, reinitialize bugging
     if (!currentDestination.equals(storedDestination) || bugging == null) {
       bugging = new Bugging(rc, currentDestination);
       storedDestination = currentDestination;
     }
     // if we are trying to move towards a turret, stay out of range
     if (rc.isCoreReady()) {
       if (currentDestination.equals(nearestTurretLocation)) {
         bugging.turretAvoidMove(turretLocations);
       } else
       // if core is ready, then try to move towards destination
       if (nearestTurretLocation != null) {
         bugging.turretAvoidMove(turretLocations);
       } else {
         bugging.move();
       }
     }
   } else if (nearestArchonLocation
       != null) { // we don't actually have a destination, so we want to try to move towards the
                  // closest archon
     rc.setIndicatorString(
         0, "moving to nearest archon " + nearestArchonLocation + rc.getRoundNum());
     if (!nearestArchonLocation.equals(storedDestination)) {
       bugging = new Bugging(rc, nearestArchonLocation);
       storedDestination = nearestArchonLocation;
     }
     // if core is ready, try to move
     if (rc.isCoreReady() && bugging != null) {
       if (nearestTurretLocation != null) {
         bugging.turretAvoidMove(turretLocations);
       } else if (nearestArchonLocation.equals(bugging.destination)
           && myLoc.distanceSquaredTo(nearestArchonLocation)
               > 13) { // if soldier is far, move towards archon
         bugging.move();
       } else if (nearestArchonLocation.equals(bugging.destination)
           && myLoc.distanceSquaredTo(nearestArchonLocation)
               < 13) { // if soldier is too close, move towards archon
         // try to move away from nearest archon
         if (rc.canMove(nearestArchonLocation.directionTo(myLoc))) {
           rc.move(nearestArchonLocation.directionTo(myLoc));
         }
       }
     }
   } else { // if we literally have nowhere to go
     rc.setIndicatorString(1, "bugging around friendly " + rc.getRoundNum());
     bugAroundFriendly(rc);
   }
 }
 public static void setRetreatingStatus(RobotController rc, RobotInfo[] hostiles)
     throws GameActionException {
   // Retreating is when your first hit less than a third health or when you were retreating
   // already and is not max health yet.
   // But you should not be retreating if you are infected. That's not a good idea!
   healing =
       (3 * rc.getHealth() < RobotType.SOLDIER.maxHealth
               || (wasHealing && rc.getHealth() < RobotType.SOLDIER.maxHealth))
           && (rc.getHealth() > 2 * rc.getViperInfectedTurns());
   if (!healing) {
     if (wasHealing) bugging = null;
     wasHealing = false;
   }
   if (healing) {
     RobotInfo[] nearbyRobots = rc.senseNearbyRobots(sightRadius, myTeam);
     for (RobotInfo r : nearbyRobots) {
       if (r.type == RobotType.ARCHON) nearestArchonLocation = r.location;
     }
     rc.setIndicatorString(0, "should be retreating " + nearestArchonLocation + rc.getRoundNum());
     if (!wasHealing || !bugging.destination.equals(nearestArchonLocation)) {
       if (nearestArchonLocation == null) {
         bugging = new Bugging(rc, rc.getLocation().add(Direction.EAST));
       } else {
         bugging = new Bugging(rc, nearestArchonLocation);
       }
     }
     wasHealing = true;
   }
 }
Example #3
0
 /**
  * 1) process signals, try to find turtleCorner 2) if enemies are within sensing radius, unpack 3)
  * if too close to archons, move away 4) if too close to wall, move away 5) if too close to
  * corner, move away 6) if it did nothing, unpack
  *
  * @param rc
  */
 private static void ttm(RobotController rc) {
   turtleCorner = turtleCorner.equals(null) ? LOCATION_NONE : turtleCorner;
   try {
     RobotInfo[] friendlyRobots =
         rc.senseNearbyRobots(rc.getType().sensorRadiusSquared, rc.getTeam());
     if (rc.getHealth() <= 3 && rc.isInfected() && friendlyRobots.length > 0) {
       rc.disintegrate();
     }
     // 1)
     if (isNearbyEnemies(rc) && rc.isCoreReady()) {
       rc.unpack();
     }
     // 2)
     processFighterSignals(rc);
     // 3)
     moveAwayFromArchons(rc);
     // 4)
     moveAwayFromWalls(rc);
     // 5)
     if (turtleCorner != LOCATION_NONE) moveFromCorner(rc);
     // 6)
     if (rc.isCoreReady() && rc.getType() == RobotType.TTM && rc.getRoundNum() % 25 == 0)
       rc.unpack();
     rc.setIndicatorString(0, "Turtle x: " + turtleCorner.x + "Turtle y: " + turtleCorner.y);
     rc.setIndicatorString(1, "I am a TTM!");
     Clock.yield();
   } catch (GameActionException e) {
     e.printStackTrace();
   }
 }
  public static void yield() {
    if (my_type == RobotType.ARCHON) rc.setIndicatorString(0, "Beta 1.5");

    if (rc.getRoundNum() % 25 == 0) Scanner.reset_health();

    Clock.yield();
  }
 // check whether or not you should lure zombies to the enemy
 private static boolean shouldLure(
     RobotController rc, RobotInfo[] nearbyEnemies, RobotInfo[] nearbyAllies) {
   int zombieCount = 0, enemyCount = 0;
   for (RobotInfo r : nearbyEnemies) {
     if (r.team.equals(Team.ZOMBIE)) zombieCount++;
     else if (r.team.equals(enemyTeam)) enemyCount++;
   }
   return rc.getRoundNum() > 1500 && nearbyAllies.length < 3 && zombieCount > enemyCount;
 }
Example #6
0
 static void init(RobotController rc) {
   int roundLimit = rc.getRoundLimit();
   Common.rc = rc;
   rand = new Random(rc.getID());
   id = rc.getID();
   myTeam = rc.getTeam();
   enemyTeam = myTeam.opponent();
   history = new MapLocation[roundLimit];
   robotType = rc.getType();
   enrollment = rc.getRoundNum();
   if (robotType != RobotType.ARCHON) birthday = enrollment - robotType.buildTurns - BUILD_LAG;
   hometown = rc.getLocation();
   sightRadius = robotType.sensorRadiusSquared;
   straightSight = (int) Math.sqrt(sightRadius);
   canMessageSignal = robotType.canMessageSignal();
   Signals.buildTarget = new MapLocation[roundLimit];
   Signals.buildStrategy = new SignalStrategy[roundLimit];
   try {
     addInfo(rc.senseRobot(id));
     myArchonHometowns = rc.getInitialArchonLocations(myTeam);
     enemyArchonHometowns = rc.getInitialArchonLocations(enemyTeam);
     int coordinates[] = new int[MAP_MAX];
     int x = 0;
     int y = 0;
     for (int i = enemyArchonHometowns.length - 1; i >= 0; --i) {
       MapLocation loc = enemyArchonHometowns[i];
       twiceCenterX += loc.x;
       twiceCenterY += loc.y;
       coordinates[loc.y] *= MAP_MAX;
       coordinates[loc.y] += loc.x + 1;
     }
     for (int i = 0; i < myArchonHometowns.length; ++i) {
       MapLocation loc = myArchonHometowns[i];
       twiceCenterX += loc.x;
       twiceCenterY += loc.y;
       x += loc.x;
       y += loc.y;
     }
     twiceCenterX /= myArchonHometowns.length;
     twiceCenterY /= myArchonHometowns.length;
     x /= myArchonHometowns.length;
     y /= myArchonHometowns.length;
     for (int i = 0; i < myArchonHometowns.length; ++i) {
       MapLocation loc = myArchonHometowns[i];
       int xCoord = coordinates[loc.y] - 1;
       coordinates[loc.y] /= MAP_MAX;
       if (loc.x != twiceCenterX - xCoord) rotation = true;
     }
     Archon.center = new MapLocation(x, y);
     myBase = new MapLocation(twiceCenterX / 2, twiceCenterY / 2).directionTo(Archon.center);
     enemyBase = myBase.opposite();
   } catch (Exception e) {
     System.out.println(e.getMessage());
     e.printStackTrace();
   }
 }
Example #7
0
 @SuppressWarnings("fallthrough")
 static void addInfo(int id, Team team, RobotType robotType, MapLocation loc)
     throws GameActionException {
   boolean newLoc = false;
   // use knownTypes because team, time, and location can come from intercepting signals
   boolean newRobot = knownTypes[id] == null;
   knownTeams[id] = team;
   knownTypes[id] = robotType;
   if (loc != null && loc.x != MAP_NONE) { // loc.y assumed
     if (knownLocations[id] == null) newLoc = true;
     knownTimes[id] = rc.getRoundNum();
     knownLocations[id] = loc;
   }
   if (rc.getType().canMessageSignal()
       && (newRobot || newLoc)
       && rc.getRoundNum() - enrollment > 10) {
     if (team == Team.NEUTRAL) {
       SignalUnit s = new SignalUnit(id, team, robotType, loc);
       s.add();
       typeSignals[typeSignalsSize++] = s.toInt();
       neutralIds[neutralIdsSize++] = id;
       neutralTypes[neutralTypesSize++] = robotType;
       neutralLocations[neutralLocationsSize++] = loc;
     } else {
       switch (robotType) {
         case VIPER:
           if (team != myTeam) break;
         case TURRET:
         case TTM:
           if (robotType == RobotType.VIPER || team != enemyTeam) break;
         case ARCHON:
         case ZOMBIEDEN:
         case BIGZOMBIE:
           new SignalUnit(id, team, robotType, loc).add();
           if (newRobot)
             typeSignals[typeSignalsSize++] = new SignalUnit(id, team, robotType).toInt();
           break;
         default:
           break;
       }
     }
   }
 }
Example #8
0
 @Override
 public boolean runInner(RobotController rc) throws GameActionException {
   if (rc.getRoundNum() == Common.enrollment) {
     target = new Target(Common.enemyBase);
     target.weights.put(Target.TargetType.ZOMBIE_ATTACK, Target.TargetType.Level.INACTIVE);
     Common.models.addFirst(target);
   }
   move(rc);
   attack(rc, rc.senseNearbyRobots(Common.sightRadius, Common.enemyTeam));
   return false;
 }
Example #9
0
 /**
  * Expensive method. Should call upon initialization only.
  *
  * @param rc
  * @throws GameActionException
  */
 static void senseRubble(RobotController rc) throws GameActionException {
   int roundNum = rc.getRoundNum();
   for (int x = -straightSight; x <= straightSight; ++x) {
     for (int y = -straightSight; y <= straightSight; ++y) {
       MapLocation loc = rc.getLocation().add(x, y);
       if (rc.canSense(loc)) {
         rubbleTimes[loc.x % MAP_MOD][loc.y % MAP_MOD] = roundNum;
         mapRubble[loc.x % MAP_MOD][loc.y % MAP_MOD] = rc.senseRubble(loc);
       }
     }
   }
 }
Example #10
0
 public void run() {
   while (true) {
     try {
       currentLocation = rc.getLocation();
       roundNumber = rc.getRoundNum();
       doTurn();
       Clock.yield();
     } catch (GameActionException e) {
       System.out.println(e.getMessage());
       e.printStackTrace();
     }
   }
 }
Example #11
0
 static void build(
     RobotController rc,
     Direction dir,
     RobotType robotType,
     LowStrategy lowStrategy,
     Target.TargetType targetType,
     MapLocation targetLocation)
     throws GameActionException {
   buildCommon(rc, dir, robotType);
   int round = rc.getRoundNum() + robotType.buildTurns - 1;
   Signals.buildStrategy[round] =
       new SignalStrategy(Common.highStrategy, lowStrategy, targetType, Common.archonIds);
   Signals.buildTarget[round] = targetLocation;
 }
Example #12
0
 /**
  * If rc finds a zombie den, it signals out to surrounding robots If rc has no weapon delay,
  * attacks in the following priority: 1) adjacent robots (if robot is a bigzombie or standard
  * zombie move away every other turn to kite it) 2) big zombies 3) nearest enemy
  *
  * @param rc RobotController which will attack
  * @param RobotController
  * @throws GameActionException
  * @return true if this robot attacked else false
  */
 private static void attackFirst(RobotController rc) throws GameActionException {
   boolean equalHealth = true;
   int lowestHealthIndex = -1;
   int lowestDistanceIndex = -1;
   int attackIndex = -1;
   RobotInfo[] enemies = rc.senseHostileRobots(rc.getLocation(), rc.getType().attackRadiusSquared);
   if (rc.isWeaponReady() && enemies.length > 0) {
     for (int i = 0; i < enemies.length; i++) {
       if (enemies[i].type == RobotType.ZOMBIEDEN) {
         rc.broadcastSignal(rc.getType().sensorRadiusSquared * 2);
       }
       if (attackIndex < 0 && (rc.getLocation()).isAdjacentTo(enemies[i].location)) {
         attackIndex = i;
         // TODO test this part - work on kiting
         if ((enemies[i].type == RobotType.BIGZOMBIE
                 || enemies[i].type == RobotType.STANDARDZOMBIE)
             && rc.getRoundNum() % 2 == 0
             && rc.isCoreReady()) {
           moveAwayFromEnemy(rc, rc.getLocation().directionTo(enemies[i].location));
         }
         if (rc.isWeaponReady()) {
           rc.attackLocation(enemies[i].location);
         }
       }
       if (rc.isWeaponReady() && enemies[i].type == RobotType.BIGZOMBIE) {
         attackIndex = i;
         rc.attackLocation(enemies[i].location);
       }
       if (attackIndex < 0) {
         lowestHealthIndex = lowestHealthIndex < 0 ? 0 : lowestHealthIndex;
         lowestDistanceIndex = lowestDistanceIndex < 0 ? 0 : lowestDistanceIndex;
         equalHealth = equalHealth && enemies[i].health == enemies[lowestHealthIndex].health;
         lowestDistanceIndex =
             rc.getLocation().distanceSquaredTo(enemies[i].location)
                     < rc.getLocation().distanceSquaredTo(enemies[lowestDistanceIndex].location)
                 ? i
                 : lowestDistanceIndex;
         lowestHealthIndex =
             enemies[i].health < enemies[lowestHealthIndex].health ? i : lowestHealthIndex;
       }
     }
     if (attackIndex < 0 && enemies.length > 0) {
       attackIndex = equalHealth ? lowestDistanceIndex : lowestHealthIndex;
     }
     if (attackIndex >= 0 && rc.isWeaponReady()) {
       rc.attackLocation(enemies[attackIndex].location);
     }
   }
 }
Example #13
0
 static void activate(
     RobotController rc,
     MapLocation loc,
     RobotType robotType,
     LowStrategy lowStrategy,
     Target.TargetType targetType,
     MapLocation targetLocation)
     throws GameActionException {
   activateCommon(rc, loc);
   // abusing build pipeline
   int round = rc.getRoundNum() + 1;
   Signals.buildStrategy[round] =
       new SignalStrategy(Common.highStrategy, lowStrategy, targetType, Common.archonIds);
   Signals.buildTarget[round] = targetLocation;
 }
Example #14
0
 static void senseParts(RobotController rc) throws GameActionException {
   int roundNum = rc.getRoundNum();
   for (MapLocation loc : rc.sensePartLocations(sightRadius)) {
     if (mapParts[loc.x % MAP_MOD][loc.y % MAP_MOD] == 0) {
       double parts = rc.senseParts(loc);
       if (parts >= PART_KEEP_THRESH) partLocations[partLocationsSize++] = loc;
       mapParts[loc.x % MAP_MOD][loc.y % MAP_MOD] = parts;
     }
     partsTimes[loc.x % MAP_MOD][loc.y % MAP_MOD] = roundNum;
   }
   // if(robotType == RobotType.ARCHON && rand.nextInt(1) == 0) {
   // // cull list
   // ListIterator<MapLocation> it = partLocations.listIterator();
   // while(it.hasNext()) {
   // MapLocation loc = it.next();
   // if(rc.canSense(loc) && rc.senseParts(loc) == 0) it.remove();
   // }
   // }
 }
Example #15
0
  /**
   * Code to run every turn.
   *
   * @param rc
   */
  static void runBefore(RobotController rc) throws GameActionException {
    // -2 for build signals
    Signals.maxMessages = GameConstants.MESSAGE_SIGNALS_PER_TURN - 2;
    read = Signals.readSignals(rc);
    sent = 0;
    int turn = rc.getRoundNum();

    switch (turn - enrollment) {
      case 0:
        if (targetType != null && Signals.targetsSize > 0) {
          models.addFirst(new Target(targetType, Signals.targets[0]));
        }
        break;
      case 1:
        for (int i = 0; i < sqrt.length; ++i) sqrt[i] = Math.sqrt(i);
        break;
      case 2:
        // Sense rubble a little after construction
        // TODO: change to 3(?) to avoid overlapping with action
        senseRubble(rc);
        break;
      default:
        break;
    }

    if (canMessageSignal) {
      sendRadius = 2 * sightRadius;
      sendBoundariesLow = false;
      sendBoundariesHigh = false;
      senseParts(rc);
    }

    updateMap(rc);
    if (turn > 5) {
      robotInfos = rc.senseNearbyRobots();
      for (RobotInfo info : robotInfos) {
        addInfo(info);
      }
    }
  }
Example #16
0
  /**
   * @Nathan - Should we adjust these priorities (specifically where should building troops be?)
   * TODO for archons - 1) repair nearest injured unit 2) signal turtle location 3) pick up adjacent
   * parts and activate neutral units 4) move to turtle corner 5) build troops 6) run away from
   * enemies
   *
   * @param rc
   * @throws GameActionException
   */
  private static void archon(RobotController rc) {
    numArchons = rc.getInitialArchonLocations(myTeam).length;
    turtleCorner = LOCATION_NONE;
    while (true) {
      try {
        // 1) repair nearest injured unit
        repairFirst(rc);
        // 2) signal turtle location - every 25 turns
        int broadcastRadius = rc.getType().sensorRadiusSquared * (2 + (rc.getRobotCount() / 50));
        if (rc.getRoundNum() > 100 && rc.getRoundNum() % 30 == 0 && turtleCorner != LOCATION_NONE) {
          rc.broadcastMessageSignal(turtleCorner.x, turtleCorner.y, broadcastRadius);
        }

        // @Nathan - Intro mode is all you -- I just pasted all the text into a separate method for
        // clarity
        if (turtleCorner != LOCATION_NONE && currentMode != TURTLE_MODE) {
          currentMode = TRANSITION_MODE;
        }
        if (currentMode == INTRO_MODE) {
          archonIntroMode(rc);
          // if (turtleCorner.equals(LOCATION_NONE)) tryToLocateCorner(rc);
        }
        // 4) Move to turtle corner
        if (currentMode == TRANSITION_MODE) {
          if (rc.getLocation().distanceSquaredTo(turtleCorner) > 4) {
            moveTowards(rc, rc.getLocation().directionTo(turtleCorner));
          } else {
            // TODO get in proper position
            currentMode = TURTLE_MODE;
            rc.broadcastMessageSignal(SENDING_MODE, TURTLE_MODE, rc.getType().sensorRadiusSquared);
          }
        }

        // 5) Build Troops !
        // @Nathan do we need to build more scouts in turtle mode so we can do more kiting?
        boolean hasNearbyEnemies = isNearbyEnemies(rc);
        if (!hasNearbyEnemies && rc.getRobotCount() - numArchons < NUM_INTRO_SCOUTS) {
          tryToBuild(rc, RobotType.SCOUT);
          Direction directionToTargetCorner = calculateDirectionToClosestCorner(rc);
          rc.broadcastMessageSignal(
              SENDING_TARGET_DIRECTION, DIRECTION_TO_SIGNAL.get(directionToTargetCorner), 4);
        }
        if ((rc.getRobotCount() <= 30 || hasNearbyEnemies)) {
          tryToBuild(rc, RobotType.SOLDIER);
        }
        if (rc.getRobotCount() > 30 && !hasNearbyEnemies) {
          tryToBuild(rc, RobotType.TURRET);
        }
        // 6) if senses enemy troop that is not a scout, run away from it
        if (hasNearbyEnemies) moveAwayFromEnemies(rc);
        // 3) Pick up parts on current square, check for adjacent parts and try to collect them
        collectParts(rc);
        // 3a) activate any nearby units
        activateUnits(rc);
        rc.setIndicatorString(0, "Turtle x: " + turtleCorner.x + "Turtle y: " + turtleCorner.y);
        rc.setIndicatorString(1, "Current Mode" + currentMode);
        Clock.yield(); // end turn
      } catch (GameActionException e) {
        e.printStackTrace();
      }
    }
  }
  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);
        }
      }
    }
  }
  // special micro if a unit is infected by a viper
  private static void viperInfectedMicro(RobotController rc) throws GameActionException {
    // If there are enemies, consider moving closer to them then to us.
    RobotInfo closestEnemy = null;
    int enemyDist = 10000;
    RobotInfo[] enemies = rc.senseNearbyRobots(sightRadius, enemyTeam);
    for (RobotInfo enemy : enemies) {
      int dist = myLoc.distanceSquaredTo(enemy.location);
      if (dist < enemyDist) {
        closestEnemy = enemy;
        enemyDist = dist;
      }
    }

    RobotInfo closestAlly = null;
    int allyDist = 10000;
    for (RobotInfo ally : nearbyAllies) {
      int dist = myLoc.distanceSquaredTo(ally.location);
      if (dist < allyDist) {
        closestAlly = ally;
        allyDist = dist;
      }
    }

    if (rc.getRoundNum() > 2100) {
      // Move toward s enemy
      if (closestEnemy != null) {
        if (rc.isCoreReady()) {
          Direction dir =
              Movement.getBestMoveableDirection(myLoc.directionTo(closestEnemy.location), rc, 2);
          if (dir != Direction.NONE) {
            rc.move(dir);
          }
        }
      }
      // Move away from allies.
      if (closestAlly != null) {
        if (rc.isCoreReady()) {
          Direction dir =
              Movement.getBestMoveableDirection(closestAlly.location.directionTo(myLoc), rc, 2);
          if (dir != Direction.NONE) {
            rc.move(dir);
          }
        }
      }
    } else if (rc.getHealth() < 2 * rc.getViperInfectedTurns()) {
      if (closestEnemy != null && closestAlly != null) {
        if (rc.isCoreReady()) {
          // When enemy is further than (or same dist) as ally, move closer to enemy.
          if (enemyDist >= allyDist) {
            Direction dir =
                Movement.getBestMoveableDirection(myLoc.directionTo(closestEnemy.location), rc, 1);
            // Move closer to enemy obviously
            if (dir != Direction.NONE) {
              rc.move(dir);
            }
            // If you could not move, see if you can attack the enemy and attack him.
            else {
              if (rc.isWeaponReady()) {
                if (rc.canAttackLocation(closestEnemy.location)) {
                  broadcastingAttack(rc, closestEnemy);
                }
              }
            }
          }
          // If closer to the enemy, then just attack them if possible. Otherwise move closer.
          else {
            if (rc.isCoreReady()) {
              if (!rc.canAttackLocation(closestEnemy.location)) {
                Direction dir =
                    Movement.getBestMoveableDirection(
                        myLoc.directionTo(closestEnemy.location), rc, 2);
                if (dir != Direction.NONE) {
                  rc.move(dir);
                }
              }
            }
            if (rc.isWeaponReady()) {
              if (rc.canAttackLocation(closestEnemy.location)) {
                broadcastingAttack(rc, closestEnemy);
              }
            }
          }
        }
      } else if (closestEnemy != null) {
        // Move closer if can't hit closest. Otherwise attack closest.
        if (rc.isCoreReady()) {
          if (!rc.canAttackLocation(closestEnemy.location)) {
            Direction dir =
                Movement.getBestMoveableDirection(myLoc.directionTo(closestEnemy.location), rc, 2);
            if (dir != Direction.NONE) {
              rc.move(dir);
            }
          }
        }
        if (rc.isWeaponReady()) {
          if (rc.canAttackLocation(closestEnemy.location)) {
            broadcastingAttack(rc, closestEnemy);
          }
        }
      }
      // Get the hell away from ally!
      else if (closestAlly != null) {
        if (rc.isCoreReady()) {
          Direction dir =
              Movement.getBestMoveableDirection(closestAlly.location.directionTo(myLoc), rc, 2);
          if (dir != Direction.NONE) {
            rc.move(dir);
          }
        }
      }
    }
  }
  public static void run(RobotController rc) {
    sightRadius = RobotType.SOLDIER.sensorRadiusSquared;
    attackRadius = RobotType.SOLDIER.attackRadiusSquared;
    myTeam = rc.getTeam();
    enemyTeam = myTeam.opponent();
    while (true) {
      try {
        numEnemySoldiers = 0;
        totalEnemySoldierHealth = 0;
        myLoc = rc.getLocation();
        nearbyAllies = rc.senseNearbyRobots(sightRadius, myTeam);
        nearbyEnemies = rc.senseHostileRobots(myLoc, sightRadius);
        newArchonLoc = null;

        // clear bad locations
        resetLocations(rc);
        // read messages and get destination
        readMessages(rc);
        // heal if need (and set the archon destination to go to)
        setRetreatingStatus(rc, nearbyEnemies);

        // Remove turret locations that you can see are not there.
        // Does NOT remove turret locations due to broadcasts. Already done in read messages.
        removeTurretLocations(rc);

        if (newArchonLoc != null) {
          nearestArchonLocation = newArchonLoc;
        }

        rc.setIndicatorString(2, "Round: " + rc.getRoundNum() + ", rushing: " + rush);
        // Reset rushing if turns since rush is > 20 and see no more enemies.
        if (rush && myLoc.distanceSquaredTo(rushLocation) <= 100) turnsSinceRush++;
        if (turnsSinceRush > 20) {
          if (rc.senseNearbyRobots(sightRadius, enemyTeam).length == 0) {
            turnsSinceRush = 0;
            rush = false;
          }
        }

        // When rushing, be mad aggressive.
        if (rush) {
          rushMicro(rc, nearbyEnemies);
        }
        // When retreating, retreat
        else if (healing) {
          if (rc.isCoreReady()) {
            if (nearestArchonLocation != null) {
              if (myLoc.distanceSquaredTo(nearestArchonLocation) > 13) {
                bugging.enemyAvoidMove(nearbyEnemies);
                // Get away from archons that are not too close together.
              } else if (myLoc.distanceSquaredTo(nearestArchonLocation) <= 2) {
                Direction radialDir = nearestArchonLocation.directionTo(myLoc);
                Direction awayDir = Movement.getBestMoveableDirection(radialDir, rc, 2);
                if (awayDir != Direction.NONE) {
                  rc.move(awayDir);
                }
              }
            }
          }
          // Make sure to attack people even when retreating.
          // Prioritize the closest enemy. Then the closest zombie.
          if (rc.isWeaponReady()) {
            // Attack the closest enemy. If there is not one, then attack the closest zombie
            int closestDist = 10000;
            RobotInfo target = null;
            for (RobotInfo hostile : nearbyEnemies) {
              int dist = myLoc.distanceSquaredTo(hostile.location);
              if (rc.canAttackLocation(hostile.location)) {
                // There is already is a target
                if (target != null) {
                  if (target.team == enemyTeam) {
                    // Target is already enemy, so prioritize the closest
                    if (hostile.team == enemyTeam) {
                      if (dist < closestDist) {
                        target = hostile;
                        closestDist = dist;
                      }
                    } // If hostile is not an enemy, not worth considering.
                  } else {
                    // Target is not on enemy team, so hostile is best choice!
                    if (hostile.team == enemyTeam) {
                      target = hostile;
                      closestDist = dist;
                      // Both are zombies, so just pick the closest.
                    } else {
                      if (dist < closestDist) {
                        target = hostile;
                        closestDist = dist;
                      }
                    }
                  }
                  // Set a target when there is not one.
                } else {
                  target = hostile;
                  closestDist = dist;
                }
              }
            }
            // We know that if there is a target, we can attack it.
            if (target != null) {
              rc.attackLocation(target.location);
            }
          }
        }

        // When viper infected and will die from the infection, do special micro
        else if (isViperInfected(rc)
            && (rc.getHealth() < rc.getViperInfectedTurns() * 2 || rc.getRoundNum() > 2100)) {
          viperInfectedMicro(rc);
        }

        // if there are enemies in range, we should focus on attack and micro
        else if (nearbyEnemies.length > 0) {
          if (shouldLure(rc, nearbyEnemies, nearbyAllies)) luringMicro(rc);
          else micro(rc);
        } else { // otherwise, we should always be moving somewhere
          moveSoldier(rc);
        }

        Clock.yield();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
Example #20
0
 /**
  * Move and update history
  *
  * @param rc
  * @param dir
  */
 static void move(RobotController rc, Direction dir) throws GameActionException {
   rc.move(dir);
   MapLocation loc = rc.getLocation();
   history[historySize++] = loc;
   int roundNum = rc.getRoundNum();
   switch (dir) {
     case NORTH_EAST:
     case EAST:
     case SOUTH_EAST:
       for (int y = -straightSight; y <= straightSight; ++y) {
         MapLocation senseLocation = loc.add(straightSight, y);
         if (rc.canSense(senseLocation)) {
           rubbleTimes[senseLocation.x % MAP_MOD][senseLocation.y % MAP_MOD] = roundNum;
           mapRubble[senseLocation.x % MAP_MOD][senseLocation.y % MAP_MOD] =
               rc.senseRubble(senseLocation);
         }
       }
       break;
     case SOUTH_WEST:
     case WEST:
     case NORTH_WEST:
       for (int y = -straightSight; y <= straightSight; ++y) {
         MapLocation senseLocation = loc.add(-straightSight, y);
         if (rc.canSense(senseLocation)) {
           rubbleTimes[senseLocation.x % MAP_MOD][senseLocation.y % MAP_MOD] = roundNum;
           mapRubble[senseLocation.x % MAP_MOD][senseLocation.y % MAP_MOD] =
               rc.senseRubble(senseLocation);
         }
       }
       break;
     default:
       break;
   }
   switch (dir) {
     case NORTH_WEST:
     case NORTH:
     case NORTH_EAST:
       for (int x = -straightSight; x <= straightSight; ++x) {
         MapLocation senseLocation = loc.add(x, straightSight);
         if (rc.canSense(senseLocation)) {
           rubbleTimes[senseLocation.x % MAP_MOD][senseLocation.y % MAP_MOD] = roundNum;
           mapRubble[senseLocation.x % MAP_MOD][senseLocation.y % MAP_MOD] =
               rc.senseRubble(senseLocation);
         }
       }
       break;
     case SOUTH_EAST:
     case SOUTH:
     case SOUTH_WEST:
       for (int x = -straightSight; x <= straightSight; ++x) {
         MapLocation senseLocation = loc.add(x, -straightSight);
         if (rc.canSense(senseLocation)) {
           rubbleTimes[senseLocation.x % MAP_MOD][senseLocation.y % MAP_MOD] = roundNum;
           mapRubble[senseLocation.x % MAP_MOD][senseLocation.y % MAP_MOD] =
               rc.senseRubble(senseLocation);
         }
       }
       break;
     default:
       break;
   }
   if (robotType == RobotType.ARCHON) mapParts[loc.x % MAP_MOD][loc.y % MAP_MOD] = 0;
 }
Example #21
0
 static void addInfo(RobotInfo info) throws GameActionException {
   seenRobots[info.ID] = info;
   seenTimes[info.ID] = rc.getRoundNum();
   addInfo(info.ID, info.team, info.type, info.location);
 }
Example #22
0
 static void addInfo(int id, Team team, MapLocation loc) {
   // not regular update since RobotType unknown
   knownTeams[id] = team;
   knownTimes[id] = rc.getRoundNum();
   knownLocations[id] = loc;
 }
Example #23
0
 protected void setIndicatorString(int i, String s) {
   if (Config.DEBUG) {
     int roundNum = rc.getRoundNum();
     rc.setIndicatorString(i, String.format("%d: %s", roundNum, s));
   }
 }
Example #24
0
  private static void scout(RobotController rc) {
    // do one time things here
    Direction targetDirection = Direction.NONE;
    while (true) {
      try {
        // TODO - I've made it so the scouts will go in pairs now; not sure if we want this later or
        // not, but they
        // aren't avoiding enemies well enough
        if (currentMode == INTRO_MODE && rc.getRoundNum() >= 40) {
          // TODO - scouts don't pick their directions well enough - try running diffusion.
          // one often starts already in/near a corner; if we find a corner very close, we should
          // probably just move to it
          // and turtle right away (even if we don't know about the others yet)
          if (targetDirection.equals(Direction.NONE)) {
            targetDirection = getScoutTargetDirection(rc);
          }

          //					RobotInfo[] zombies = rc.senseNearbyRobots(-1, Team.ZOMBIE);
          // TODO: bring this back in, but only if there aren't bots that will attack the scout
          // (otherwise, the delay makes them too vulnerable; finding corners matters more first)
          //					for (RobotInfo zombie : zombies) {
          //						if (zombie.type == RobotType.ZOMBIEDEN) {
          //							if (!ZOMBIE_DEN_LOCATIONS.contains(zombie.location)) {
          //								ZOMBIE_DEN_LOCATIONS.add(zombie.location);
          //								rc.broadcastMessageSignal(SENDING_DEN_X, zombie.location.x, fullMapRadius);
          //								rc.broadcastMessageSignal(SENDING_DEN_Y, zombie.location.y, fullMapRadius);
          //							}
          //						}
          //					}
          //					int start = Clock.getBytecodeNum();
          MapLocation corner = checkForCorner(rc, targetDirection);
          //					System.out.println("CheckforCorner: " + (Clock.getBytecodeNum() - start));
          if (!corner.equals(LOCATION_NONE)) {
            rc.broadcastMessageSignal(SENDING_CORNER_X, corner.x, fullMapRadius);
            rc.broadcastMessageSignal(SENDING_CORNER_Y, corner.y, fullMapRadius);
            // head to opposite in case other scout didn't make it
            // TODO or scout around for other zombie dens, kite, etc.
            targetDirection = targetDirection.opposite();
          }
          //					start = Clock.getBytecodeNum();
          moveCautiously(rc, targetDirection);
          //					System.out.println("moveCautiously: " + (Clock.getBytecodeNum() - start));
        } else { // not intro mode; do kiting
          RobotInfo[] zombies = rc.senseNearbyRobots(-1, Team.ZOMBIE);
          if (zombies.length > 0) {
            turnsToCheckForZombies = NUM_TURNS_TO_CHECK_FOR_ZOMBIES;
          }
          if (turnsToCheckForZombies > 0) {
            turnsToCheckForZombies--;
            leadZombiesToEnemy(rc);
          } else {

          }
          //				rc.sensePartLocations(-1); //TODO
        }
        Clock.yield();
      } catch (GameActionException e) {
        e.printStackTrace();
      }
    }
  }
Example #25
0
  public static void run(RobotController rc) {
    Random rand = new Random(rc.getID());
    Team myTeam = rc.getTeam();
    Team enemyTeam = myTeam.opponent();
    int numFriendly = 0;
    RobotInfo[] adjNeutralRobots = rc.senseNearbyRobots(2, Team.NEUTRAL);
    // if useTurrets = true, then use turret strategy
    boolean useTurrets = false;

    try {
      // Any code here gets executed exactly once at the beginning of the game.
      RobotInfo[] nearbyArchons =
          rc.senseNearbyRobots(RobotType.ARCHON.sensorRadiusSquared, rc.getTeam());
      if (nearbyArchons.length >= 2) {
        useTurrets = true;
      }
      // rc.setIndicatorString(1, "newString");
    } 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 {
        if (useTurrets) {
          boolean escape = false;
          //					if (rc.isCoreReady()) {
          //						int numAdjTurrets = 0;
          //						for (RobotInfo f : friendlyAdjacent) {
          //							if (f.type == RobotType.TURRET) {
          //								numAdjTurrets++;
          //							}
          //						}
          //						if (numAdjTurrets < 3) {
          //							escape = Movement.moveAwayFromEnemy(rc);
          //						}
          //					}
          //					if (rc.isCoreReady()) {
          //						escape = Movement.moveAwayFromEnemy(rc);
          //					}
          if (!escape) {
            if (adjNeutralRobots.length > 0) {
              // if there is a neutral robot adjacent, activate it or wait until there's no core
              // delay
              if (rc.isCoreReady()) {
                rc.activate(adjNeutralRobots[0].location);
              }
            }
            // careful- moving to parts might get into enemy turret range
            if (Movement.getToAdjParts(rc)) {
            } else {
              boolean toheal = false;
              // repair a nearby friendly robot
              if (rc.isWeaponReady()) {
                RobotInfo[] friendlyWithinRange = rc.senseNearbyRobots(24, myTeam);
                numFriendly = friendlyWithinRange.length;
                if (friendlyWithinRange.length > 0) {
                  RobotInfo toRepair = friendlyWithinRange[0];
                  for (RobotInfo r : friendlyWithinRange) {
                    if ((r.health < toRepair.health)
                        && (r.type != RobotType.ARCHON)
                        && (r.maxHealth - r.health > 1)) {
                      toRepair = r;
                    }
                  }
                  if ((toRepair.maxHealth - toRepair.health > 1)
                      && (toRepair.type != RobotType.ARCHON)) {
                    toheal = true;
                    rc.repair(toRepair.location);
                  }
                }
              }
              if (toheal == false && rc.isCoreReady()) {
                // did not heal any robots

                // sense all the hostile robots within the archon's radius
                MapLocation myLoc = rc.getLocation();
                RobotInfo[] hostileWithinRange =
                    rc.senseHostileRobots(myLoc, RobotType.ARCHON.sensorRadiusSquared);
                RobotInfo closestRobot = null;
                int closestDistance = 0;
                // get the furthest robot from the scout
                for (RobotInfo r : hostileWithinRange) {
                  if (r.location.distanceSquaredTo(myLoc) > closestDistance) {
                    closestRobot = r;
                    closestDistance = r.location.distanceSquaredTo(myLoc);
                  }
                }
                // if there is such an enemy, signal it to range 8
                if (closestRobot != null) {
                  try {
                    // this signaling is only effective against non turret enemies
                    rc.broadcastMessageSignal(closestRobot.location.x, closestRobot.location.y, 8);
                  } catch (GameActionException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                  }
                }

                int turnNum = rc.getRoundNum();
                int numVeryCloseScouts = 0;
                int numNearbyTurrets = 0;
                RobotInfo[] friendlyNearby =
                    rc.senseNearbyRobots(RobotType.ARCHON.sensorRadiusSquared, myTeam);
                for (RobotInfo f : friendlyNearby) {
                  if (f.type == RobotType.TURRET) {
                    numNearbyTurrets++;
                  }
                }
                // for sensing if there are guards within range 24
                RobotInfo[] friendlyClose = rc.senseNearbyRobots(24, myTeam);
                int numNearbyGuards = 0;
                for (RobotInfo f : friendlyClose) {
                  if (f.type == RobotType.GUARD) {
                    numNearbyGuards++;
                  }
                }
                // check for scouts; how close should they be????
                RobotInfo[] friendlyVeryClose = rc.senseNearbyRobots(15, myTeam);
                for (RobotInfo f : friendlyClose) {
                  if (f.type == RobotType.SCOUT) {
                    numVeryCloseScouts++;
                  }
                }
                if (rc.hasBuildRequirements(RobotType.GUARD)
                    && rc.isCoreReady()
                    && numNearbyGuards < 1) {
                  Direction dirToBuild = RobotPlayer.directions[rand.nextInt(8)];
                  for (int i = 0; i < 8; i++) {
                    // If possible, build in this direction
                    if (rc.canBuild(dirToBuild, RobotType.GUARD)) {
                      rc.build(dirToBuild, RobotType.GUARD);
                      break;
                    } else {
                      // Rotate the direction to try
                      dirToBuild = dirToBuild.rotateLeft();
                    }
                  }
                }
                // if there are <1 turrets next to archon, build asap
                if (rc.hasBuildRequirements(RobotType.TURRET)
                    && rc.isCoreReady()
                    && numNearbyTurrets < 1) {
                  Direction dirToBuild = RobotPlayer.directions[rand.nextInt(4) * 2];
                  for (int i = 0; i < 4; i++) {
                    // If possible, build in this direction
                    if (rc.canBuild(dirToBuild, RobotType.TURRET)) {
                      rc.build(dirToBuild, RobotType.TURRET);
                      turretCount++;
                      break;
                    } else {
                      // Rotate the direction to try
                      dirToBuild = dirToBuild.rotateLeft();
                      dirToBuild = dirToBuild.rotateLeft();
                    }
                  }
                }
                // if there are <1 scout next to archon and 1 turret, build scout asap
                if (rc.hasBuildRequirements(RobotType.SCOUT)
                    && rc.isCoreReady()
                    && numNearbyTurrets > 0
                    && numVeryCloseScouts == 0
                    && turnNum < 400) {
                  Direction dirToBuild = RobotPlayer.directions[rand.nextInt(8)];
                  for (int i = 0; i < 8; i++) {
                    // If possible, build in this direction
                    if (rc.canBuild(dirToBuild, RobotType.SCOUT)) {
                      rc.build(dirToBuild, RobotType.SCOUT);
                      scoutCount++;
                      break;
                    } else {
                      // Rotate the direction to try
                      dirToBuild = dirToBuild.rotateLeft();
                    }
                  }
                }
                // build turret every 100 turns until turn 400
                //								if (turnNum > 1 && turnNum < 400) {
                //									if (turnNum % 100 == 85 && rc.isCoreReady()) {
                //										Direction dirToBuild = RobotPlayer.directions[rand.nextInt(4)*2];
                //										for (int i = 0; i < 4; i++) {
                //											// If possible, build in this direction
                //											if (rc.canBuild(dirToBuild, RobotType.TURRET)) {
                //												rc.build(dirToBuild, RobotType.TURRET);
                //												turretCount++;
                //												break;
                //											} else {
                //												// Rotate the direction to try
                //												dirToBuild = dirToBuild.rotateLeft();
                //												dirToBuild = dirToBuild.rotateLeft();
                //											}
                //										}
                //									}
                //								}
                else {
                  // Check if this ARCHON's core is ready
                  if (rc.isCoreReady()) {
                    boolean built = false;
                    RobotType typeToBuild = RobotType.TURRET;
                    if (scoutCount < turretCount / 5) {
                      typeToBuild = RobotType.SCOUT;
                    }
                    // never build scouts after a certain turn
                    if (turnNum < 1500) {
                      typeToBuild = RobotType.TURRET;
                    }
                    // Check for sufficient parts
                    if (rc.hasBuildRequirements(typeToBuild)) {
                      // Choose a random direction to try to build in; NESW for turrets; all 8 for
                      // scouts
                      if (typeToBuild.equals(RobotType.TURRET)) {
                        Direction dirToBuild = RobotPlayer.directions[rand.nextInt(4) * 2];
                        for (int i = 0; i < 4; i++) {
                          // If possible, build in this direction
                          if (rc.canBuild(dirToBuild, RobotType.TURRET)) {
                            rc.build(dirToBuild, RobotType.TURRET);
                            turretCount++;
                            break;
                          } else {
                            // Rotate the direction to try
                            dirToBuild = dirToBuild.rotateLeft();
                            dirToBuild = dirToBuild.rotateLeft();
                          }
                        }
                      } else {
                        Direction dirToBuild = RobotPlayer.directions[rand.nextInt(8)];
                        for (int i = 0; i < 8; i++) {
                          // If possible, build in this direction
                          if (rc.canBuild(dirToBuild, RobotType.SCOUT)) {
                            rc.build(dirToBuild, RobotType.SCOUT);
                            scoutCount++;
                            break;
                          } else {
                            // Rotate the direction to try
                            dirToBuild = dirToBuild.rotateLeft();
                          }
                        }
                      }
                    }
                    // only move around if there are resources
                    if ((!built)
                        && rc.hasBuildRequirements(RobotType.TURRET)
                        && (rc.isCoreReady())) {
                      // don't move into enemy turret range if scout sends signal about it
                      Set<Direction> dangerousDirs = new HashSet<>();
                      Signal currentSignal = rc.readSignal();
                      while (currentSignal != null) {
                        int messageX = currentSignal.getMessage()[0];
                        int messageY = currentSignal.getMessage()[1];
                        // if signal message > 80000, then the message is signaling a turret
                        // location
                        if (messageX > 80000) {
                          messageX = messageX - 100000;
                          messageY = messageY - 100000;
                          MapLocation enemyTurretLoc = new MapLocation(messageX, messageY);
                          Direction dirToEnemyTurret = myLoc.directionTo(enemyTurretLoc);
                          Direction dirToEnemyTurretL =
                              myLoc.directionTo(enemyTurretLoc).rotateLeft();
                          Direction dirToEnemyTurretR =
                              myLoc.directionTo(enemyTurretLoc).rotateRight();
                          if (myLoc.add(dirToEnemyTurret).distanceSquaredTo(enemyTurretLoc) <= 48) {
                            dangerousDirs.add(dirToEnemyTurret);
                          }
                          if (myLoc.add(dirToEnemyTurretL).distanceSquaredTo(enemyTurretLoc)
                              <= 48) {
                            dangerousDirs.add(dirToEnemyTurretL);
                          }
                          if (myLoc.add(dirToEnemyTurretR).distanceSquaredTo(enemyTurretLoc)
                              <= 48) {
                            dangerousDirs.add(dirToEnemyTurretR);
                          }
                        }
                        currentSignal = rc.readSignal();
                      }

                      Direction dirToMove = RobotPlayer.directions[(rand.nextInt(4) * 2) + 1];
                      for (int i = 0; i < 4; i++) {
                        if (rc.canMove(dirToMove) && !dangerousDirs.contains(dirToMove)) {
                          rc.move(dirToMove);
                          break;
                        } else {
                          dirToMove = dirToMove.rotateLeft();
                          dirToMove = dirToMove.rotateLeft();
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        } else { // use soldiers
          boolean escape = false;
          if (rc.isCoreReady()) {
            escape = Movement.moveAwayFromEnemy(rc);
          }
          if (!escape) {
            if (adjNeutralRobots.length > 0) {
              // if there is a neutral robot adjacent, activate it or wait until there's no core
              // delay
              if (rc.isCoreReady()) {
                rc.activate(adjNeutralRobots[0].location);
              }
            }
            if (Movement.getToAdjParts(rc)) {
            } else {
              boolean toheal = false;
              // repair a nearby friendly robot
              if (rc.isWeaponReady()) {
                RobotInfo[] friendlyWithinRange = rc.senseNearbyRobots(24, myTeam);
                numFriendly = friendlyWithinRange.length;
                if (friendlyWithinRange.length > 0) {
                  RobotInfo toRepair = friendlyWithinRange[0];
                  for (RobotInfo r : friendlyWithinRange) {
                    if ((r.health < toRepair.health)
                        && (r.type != RobotType.ARCHON)
                        && (r.maxHealth - r.health > 1)) {
                      toRepair = r;
                    }
                  }
                  if ((toRepair.maxHealth - toRepair.health > 1)
                      && (toRepair.type != RobotType.ARCHON)) {
                    toheal = true;
                    rc.repair(toRepair.location);
                  }
                }
              }
              if (toheal == false) {
                // for sensing if there are guards within range 24
                RobotInfo[] friendlyClose = rc.senseNearbyRobots(24, myTeam);
                int numNearbyGuards = 0;
                for (RobotInfo f : friendlyClose) {
                  if (f.type == RobotType.GUARD) {
                    numNearbyGuards++;
                  }
                }

                boolean built = false;
                int turnNum = rc.getRoundNum();
                if (rc.hasBuildRequirements(RobotType.SCOUT)
                    && rc.isCoreReady()
                    && turnNum > 1
                    && turnNum % 150 >= 0
                    && turnNum % 150 <= 19
                    && turnNum < 900) {
                  Direction dirToBuild = RobotPlayer.directions[rand.nextInt(8)];
                  for (int i = 0; i < 8; i++) {
                    // If possible, build in this direction
                    if (rc.canBuild(dirToBuild, RobotType.SCOUT)) {
                      rc.build(dirToBuild, RobotType.SCOUT);
                      built = true;
                      break;
                    } else {
                      // Rotate the direction to try
                      dirToBuild = dirToBuild.rotateLeft();
                    }
                  }
                }
                if (rc.hasBuildRequirements(RobotType.GUARD)
                    && rc.isCoreReady()
                    && !built
                    && numNearbyGuards < 1) {
                  Direction dirToBuild = RobotPlayer.directions[rand.nextInt(8)];
                  for (int i = 0; i < 8; i++) {
                    // If possible, build in this direction
                    if (rc.canBuild(dirToBuild, RobotType.GUARD)) {
                      rc.build(dirToBuild, RobotType.GUARD);
                      built = true;
                      break;
                    } else {
                      // Rotate the direction to try
                      dirToBuild = dirToBuild.rotateLeft();
                    }
                  }
                }
                if (rc.hasBuildRequirements(RobotType.SOLDIER) && rc.isCoreReady() && !built) {
                  Direction dirToBuild = RobotPlayer.directions[rand.nextInt(8)];
                  for (int i = 0; i < 8; i++) {
                    // If possible, build in this direction
                    if (rc.canBuild(dirToBuild, RobotType.SOLDIER)) {
                      rc.build(dirToBuild, RobotType.SOLDIER);
                      built = true;
                      break;
                    } else {
                      // Rotate the direction to try
                      dirToBuild = dirToBuild.rotateLeft();
                    }
                  }
                }
                // if archon has nothing to do, tell soldiers to come to it's location
                /*if (rc.getRoundNum() > 500 && rc.isCoreReady() && rc.isWeaponReady()) {
                	rc.broadcastMessageSignal(-100, 0, 70 * 70);
                }*/
              }
            }
          }
        }
        Clock.yield();
      } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
      }
    }
  }
Example #26
0
  public static void run(RobotController rc) {
    team = rc.getTeam();
    while (true) {
      try {
        myLoc = rc.getLocation();
        int numOurTurrets = 0;

        // Move opposite of ally scout. Also keep track of our number of turrets.
        // Try to pair with ally turret.
        RobotInfo[] allies = rc.senseNearbyRobots(myLoc, sightRange, team);
        isPaired = false;
        int followedTurretDist = 10000;
        for (RobotInfo ally : allies) {
          if (ally.type == RobotType.SCOUT) {
            int randInt = rand.nextInt(3);
            if (randInt == 0) {
              mainDir = ally.location.directionTo(myLoc);
            } else if (randInt == 1) {
              mainDir = ally.location.directionTo(myLoc).rotateLeft();
            } else {
              mainDir = ally.location.directionTo(myLoc).rotateRight();
            }
          } else if (ally.type == RobotType.TURRET || ally.type == RobotType.TTM) {
            numOurTurrets++;
            int dist = myLoc.distanceSquaredTo(ally.location);
            if (dist < followedTurretDist) {
              // Try to pair with this turret.
              // Confirm that no other scout allies are nearby.
              RobotInfo[] otherAllies = rc.senseNearbyRobots(ally.location, dist, team);
              boolean canPairWith = true;
              for (RobotInfo otherAlly : otherAllies) {
                if (otherAlly.type == RobotType.SCOUT) {
                  int otherDist = ally.location.distanceSquaredTo(otherAlly.location);
                  if (otherDist < dist) {
                    canPairWith = false;
                    break;
                  }
                }
              }
              if (canPairWith) {
                // This is turret we can pair with.
                isPaired = true;
                followedTurretDist = dist;
                pairedTurret = ally.location;
              }
            }
          }
        }
        rc.setIndicatorString(0, "Round: " + rc.getRoundNum() + ", Is paired: " + isPaired);

        int numEnemyTurrets = 0;
        boolean inEnemyAttackRangeAndPaired = false;
        Direction dodgeEnemyDir = Direction.NONE;
        boolean inDanger = false;

        RobotInfo[] hostiles = rc.senseHostileRobots(myLoc, sightRange);
        if (isPaired) {
          if (hostiles.length > 0) {
            closestTurretLoc = null;
            int closestDist = 10000;
            RobotInfo closestEnemy = hostiles[0];
            RobotInfo bestEnemy = hostiles[0];
            // Find the best enemy.
            // In the meantime, also find the closest enemy that can hit me and get away.
            MapLocation enemyTurretLoc = null;
            MapLocation enemyScoutLoc = null;
            for (RobotInfo hostile : hostiles) {
              int dist = myLoc.distanceSquaredTo(hostile.location);
              if (hostile.type == RobotType.SCOUT) {
                enemyScoutLoc = hostile.location;
              } else if (hostile.type == RobotType.TURRET) {
                enemyTurretLoc = hostile.location;
              } else if (hostile.type == RobotType.ZOMBIEDEN) {
                // if (rc.isCoreReady()) {
                if (!hostile.location.equals(previouslyBroadcastedDen)) {
                  if (myLoc.distanceSquaredTo(pairedTurret) <= 2) {
                    previouslyBroadcastedDen = hostile.location;
                    Message.sendMessageGivenDelay(rc, hostile.location, Message.ZOMBIEDEN, 10);
                  }
                }
                // }
              }

              // First handle finding the best enemy.
              // make sure hostile range is > 5
              if (hostile.location.distanceSquaredTo(pairedTurret)
                      <= RobotType.TURRET.attackRadiusSquared
                  && hostile.location.distanceSquaredTo(pairedTurret) > 5) {
                if (bestEnemy.type == RobotType.ARCHON) {
                  if (hostile.type == RobotType.ARCHON) {
                    if (hostile.health < bestEnemy.health) {
                      bestEnemy = hostile;
                    }
                  }
                } else {
                  if (hostile.type == RobotType.ARCHON) {
                    bestEnemy = hostile;
                  } else {
                    if (hostile.health < bestEnemy.health) {
                      bestEnemy = hostile;
                    }
                  }
                }
              }
              // Then find the closest turret
              if (closestTurretDist > dist
                  && hostile.type == RobotType.TURRET
                  && hostile.location.distanceSquaredTo(pairedTurret) > 5) {
                closestTurretDist = dist;
                closestTurretLoc = hostile.location;
              }

              // Find the closest enemy
              if (closestDist > dist
                  && hostile.type != RobotType.ARCHON
                  && hostile.location.distanceSquaredTo(pairedTurret) > 5) {
                closestDist = dist;
                closestEnemy = hostile;
              }

              // If my closest enemy can hit me, get away.
              if (closestEnemy.location.distanceSquaredTo(myLoc)
                  <= closestEnemy.type.attackRadiusSquared) {
                inEnemyAttackRangeAndPaired = true;
                // Find a direction closest to paired turret that is not in attack range.
                int closestPairedDist = 10000;
                for (Direction dir : RobotPlayer.directions) {
                  if (rc.canMove(dir)) {
                    MapLocation dirLoc = myLoc.add(dir);
                    int pairedDist = dirLoc.distanceSquaredTo(pairedTurret);
                    if (dirLoc.distanceSquaredTo(closestEnemy.location)
                        > closestEnemy.type.attackRadiusSquared) {
                      if (closestPairedDist > pairedDist) {
                        closestPairedDist = pairedDist;
                        dodgeEnemyDir = dir;
                      }
                    }
                  }
                }
              }
            }
            // If there is a best enemy, send a message.
            if (bestEnemy != null
                && bestEnemy.location.distanceSquaredTo(pairedTurret) > 5
                && rc.isCoreReady()) {
              Message.sendMessageGivenRange(rc, bestEnemy.location, Message.PAIREDATTACK, 15);
            }

            // If there is a closest turret, send a message.
            if (closestTurretLoc != null && rc.isCoreReady()) {
              Message.sendMessageGivenDelay(rc, closestTurretLoc, Message.TURRET, 2.25);
              previouslyBroadcastedClosestTurretLoc = closestTurretLoc;
            }

            if (previouslyBroadcastedClosestTurretLoc != null
                && closestTurretLoc == null
                && rc.isCoreReady()) {
              Message.sendMessageGivenDelay(
                  rc, previouslyBroadcastedClosestTurretLoc, Message.TURRETKILLED, 2.25);
            }

            // if it sees enemy turret with a scout, signal that
            if (enemyScoutLoc != null && enemyTurretLoc != null && rc.isCoreReady()) {
              Message.sendMessageGivenRange(rc, enemyTurretLoc, Message.ENEMYTURRETSCOUT, 8);
            }
          }

        } else {
          // If sees an enemy, get away and record the two closest enemies. Then broadcast the
          // location while running away.
          // If Scout sees Den, then just broadcast immediately.
          // If Scout sees other enemies, then wait until far enough to broadcast.
          closestRecordedEnemy = null; // does not include the Den!
          secondClosestRecordedEnemy = null; // does not include the Den!
          int closestRecordedEnemyDist = 10000;
          int secondClosestRecordedEnemyDist = 20000;
          if (hostiles.length > 0) {
            MapLocation realLoc = myLoc.add(mainDir);
            int closestAttackingEnemyDist = 10000;
            for (RobotInfo hostile : hostiles) {
              if (hostile.type == RobotType.ZOMBIEDEN) {
                if (!hostile.location.equals(previouslyBroadcastedDen)) {
                  previouslyBroadcastedDen = hostile.location;
                  // if (rc.isCoreReady()) {
                  Message.sendMessageGivenDelay(rc, hostile.location, Message.ZOMBIEDEN, 10);
                  Direction dir = hostile.location.directionTo(myLoc);
                  if (rc.canMove(dir)) {
                    mainDir = dir;
                  } else if (rc.canMove(dir.rotateLeft())) {
                    mainDir = dir.rotateLeft();
                  } else if (rc.canMove(dir.rotateRight())) {
                    mainDir = dir.rotateRight();
                  }
                  // }
                }
              } else {
                if (hostile.type == RobotType.TURRET) {
                  numEnemyTurrets++;
                }
                // In danger only if someone can attack me.
                if (hostile.type != RobotType.ARCHON) {
                  int dist = realLoc.distanceSquaredTo(hostile.location);
                  if (hostile.type == RobotType.ZOMBIEDEN) {
                    if (dist <= 5) {
                      inDanger = true;
                    }
                  } else if (hostile.type == RobotType.TURRET) {
                    if (dist <= hostile.type.attackRadiusSquared) inDanger = true;
                  } else {
                    if (dist <= hostile.type.sensorRadiusSquared) inDanger = true;
                  }
                }

                int dist = myLoc.distanceSquaredTo(hostile.location);
                if (closestRecordedEnemy == null) {
                  closestRecordedEnemy = hostile;
                } else if (dist
                    < closestRecordedEnemyDist) { // update the two closest stored locations.
                  secondClosestRecordedEnemyDist = closestRecordedEnemyDist;
                  secondClosestRecordedEnemy = closestRecordedEnemy;
                  closestRecordedEnemyDist = dist;
                  closestRecordedEnemy = hostile;
                } else if (dist
                    < secondClosestRecordedEnemyDist) { // update the second closest stored location
                                                        // only.
                  secondClosestRecordedEnemyDist = dist;
                  secondClosestRecordedEnemy = hostile;
                }
              }
            }
            if (rc.isCoreReady()) {
              if (closestRecordedEnemy != null) {
                if (closestRecordedEnemyDist <= closestRecordedEnemy.type.attackRadiusSquared) {
                  // Send a message of the closest enemy
                  broadcastRecordedEnemy(rc, closestRecordedEnemy);
                  if (secondClosestRecordedEnemy != null) {
                    // Send a message of the second closest enemy.
                    broadcastRecordedEnemy(rc, secondClosestRecordedEnemy);
                  }
                }
              }
            }
          }
        }

        // Broadcast collectibles
        if (rc.isCoreReady()) {
          if (numTurnsStationary < 10) {
            if (isPaired) {
              if (myLoc.distanceSquaredTo(pairedTurret) <= 2) {
                broadcastCollectibles(rc, hostiles.length > 0);
              }
            } else {
              broadcastCollectibles(rc, hostiles.length > 0);
            }
          }
        }

        // Every 50 turns, broadcast whether or not I am paired
        if (rc.getRoundNum() % 50 == 0) {
          int messageType = isPaired ? Message.PAIRED : Message.UNPAIRED;
          if (isPaired) {
            if (myLoc.distanceSquaredTo(pairedTurret) <= 2) {
              if (hostiles.length > 0) {
                Message.sendMessageGivenDelay(rc, myLoc, messageType, 8);
              } else {
                Message.sendMessageGivenDelay(rc, myLoc, messageType, 0.3);
              }
            }
          } else {
            if (hostiles.length > 0) {
              Message.sendMessageGivenDelay(rc, myLoc, messageType, 8);
            } else {
              Message.sendMessageGivenDelay(rc, myLoc, messageType, 0.3);
            }
          }
        }

        // When we have more turrets, broadcast that.
        if (numOurTurrets > numEnemyTurrets && isPaired && rc.isCoreReady()) {
          if (myLoc.distanceSquaredTo(pairedTurret) <= 2) {
            if (closestTurretLoc != null) {
              Message.sendMessageGivenRange(rc, closestTurretLoc, Message.RUSH, 2 * sightRange);
            } else {
              Message.sendMessageGivenRange(
                  rc, new MapLocation(0, 0), Message.RUSHNOTURRET, 2 * sightRange);
            }
          }
        }

        // When paired, move along with the turret
        // Otherwise move in your main direction, and change it accordingly if you cannot move.
        if (isPaired) {
          if (rc.isCoreReady()) {
            if (inEnemyAttackRangeAndPaired) {
              // mainDir already computed above.
              if (dodgeEnemyDir != Direction.NONE) {
                mainDir = dodgeEnemyDir;
                rc.move(mainDir);
                numTurnsStationary = 0;
              }
            } else {
              // When not in enemy attack range, cling to paired turret (and make sure not to get
              // hit!)
              Direction dirToTurret = myLoc.directionTo(pairedTurret);
              // If right next to turret, then circle around turret
              if (myLoc.add(dirToTurret).equals(pairedTurret)) {
                Direction left = dirToTurret.rotateLeft();
                if (rc.canMove(left) && !inEnemyAttackRange(myLoc.add(left), hostiles)) {
                  mainDir = left;
                  rc.move(mainDir);
                  numTurnsStationary = 0;
                } else {
                  Direction right = dirToTurret.rotateRight();
                  if (rc.canMove(right) && !inEnemyAttackRange(myLoc.add(right), hostiles)) {
                    mainDir = right;
                    rc.move(mainDir);
                    numTurnsStationary = 0;
                  }
                }
              }
              // Otherwise, move closer to the turret.
              else {
                Direction closerDir = Movement.getBestMoveableDirection(dirToTurret, rc, 2);
                if (closerDir != Direction.NONE
                    && !inEnemyAttackRange(myLoc.add(closerDir), hostiles)) {
                  mainDir = closerDir;
                  rc.move(mainDir);
                  numTurnsStationary = 0;
                }
              }
            }
          }
        } else {
          rc.setIndicatorString(1, "Round: " + rc.getRoundNum() + ", In Danger: " + inDanger);
          if (rc.isCoreReady()) {
            if (inDanger) {
              // Go in direction maximizing the minimum distance
              int maxMinDist = 0;
              for (Direction dir : RobotPlayer.directions) {
                if (rc.canMove(dir)) {
                  MapLocation dirLoc = myLoc.add(dir);
                  int minDist = 10000;
                  for (RobotInfo hostile : hostiles) {
                    int dist = dirLoc.distanceSquaredTo(hostile.location);
                    minDist = Math.min(dist, minDist);
                  }
                  if (maxMinDist < minDist) {
                    maxMinDist = minDist;
                    mainDir = dir;
                  }
                }
              }
              rc.setIndicatorString(
                  2,
                  "Round: "
                      + rc.getRoundNum()
                      + ", Max min dist: "
                      + maxMinDist
                      + ", Dir: "
                      + mainDir);
              if (rc.canMove(mainDir)) {
                rc.move(mainDir);
                numTurnsStationary = 0;
              }
            } else {
              if (!rc.canMove(mainDir)) {
                int[] disps = {1, -1, 3, -3};
                for (int disp : disps) {
                  Direction dir = RobotPlayer.directions[((mainDir.ordinal() + disp) % 8 + 8) % 8];
                  if (rc.canMove(dir)) {
                    mainDir = dir;
                    break;
                  }
                }
              }
              if (rc.canMove(mainDir)) {
                rc.move(mainDir);
                numTurnsStationary = 0;
              }
            }
          }
        }
        numTurnsStationary++;
        Clock.yield();
      } catch (Exception e) {
        e.printStackTrace();
        Clock.yield();
      }
    }
  }