Пример #1
0
  // In this function the HQ spawns a soldier ideally toward the enemy base but in any direction
  // otherwise
  public static void SpawnSoldiers(RobotController rc) {
    try {
      if (rc.isActive() && rc.getType() == RobotType.HQ) {
        Direction toEnemy = rc.getLocation().directionTo(rc.senseEnemyHQLocation());
        if (rc.senseObjectAtLocation(rc.getLocation().add(toEnemy)) == null) {
        } else {
          for (int i = 0; i < 7; i++) {
            toEnemy = toEnemy.rotateLeft();

            if (rc.senseObjectAtLocation(rc.getLocation().add(toEnemy)) == null) {
              i = 47;
            } else if (i == 6) {
              toEnemy = Direction.NONE;
            }
          }
        }

        if (toEnemy != Direction.NONE) {
          if (rc.isActive()) {
            if (rc.getType() == RobotType.HQ) {
              rc.spawn(toEnemy);
            }
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
      System.out.println("Utility Exception");
    }
  }
Пример #2
0
  public static void run(RobotController myRC) {
    BaseRobot br = null;
    try {
      switch (myRC.getType()) {
        case HQ:
          br = new HQRobot(myRC);
          break;
        case SOLDIER:
          br = new SoldierRobot(myRC);
          break;
        case ARTILLERY:
          br = new Artillery(myRC);
          break;
        default:
          br = new PassiveEncampment(myRC);
      }

      while (true) {
        br.curRound = Clock.getRoundNum();
        br.run();

        br.rc.yield();
      }

    } catch (Exception e) {
      // DEBUG
      System.out.println("Shit happened!");
      e.printStackTrace();
      br.rc.addMatchObservation(e.toString());
    }
  }
Пример #3
0
  public static void fire(RobotController rc) {
    int radius;

    try {
      if (rc.getType() == RobotType.HQ) {
        radius = 15;
        Robot[] enemies = rc.senseNearbyGameObjects(Robot.class, radius, rc.getTeam().opponent());
        Direction[] dirs = Direction.values();
        Robot target = null;
        int maxValue = 0;

        for (int k = 0; k < enemies.length; k++) {
          MapLocation loc = rc.senseRobotInfo(enemies[k]).location;
          int value = 2;
          for (int a = 0; a < 8; a++) {
            try {
              if (rc.senseObjectAtLocation(loc.add(dirs[a])).getTeam() == rc.getTeam().opponent()) {
                value++;
              } else if (rc.senseObjectAtLocation(loc.add(dirs[a])).getTeam() == rc.getTeam()) {
                value--;
              }
            } catch (Exception e) {
              e.printStackTrace();
            }
          }

          rc.setIndicatorString(0, "" + value);

          if (value > maxValue) {
            maxValue = value;
            target = enemies[k];
          }
        }

        if (target != null) {
          rc.attackSquare(rc.senseRobotInfo(target).location);
        }

      } else {
        radius = 10;
        Robot[] enemies = rc.senseNearbyGameObjects(Robot.class, radius, rc.getTeam().opponent());
        Robot target = null;

        for (int k = 0; k < enemies.length; k++) {
          if (target == null) {
            target = enemies[k];
          } else if (rc.senseRobotInfo(enemies[k]).health < rc.senseRobotInfo(target).health) {
            target = enemies[k];
          }
        }

        if (target != null) {
          rc.attackSquare(rc.senseRobotInfo(target).location);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Пример #4
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();
   }
 }
Пример #5
0
  public static MapLocation[] BestPastureSpots(RobotController rc) {
    MapLocation[] empty = new MapLocation[1];
    try {
      // Check if a robot is spawnable and spawn one if it is
      double growthRates[][] = rc.senseCowGrowth();
      int countofBestSpots = 0;
      double best = 0.0001;
      for (int i = 0; i < rc.getMapWidth(); i++) {
        for (int k = 0; k < rc.getMapHeight(); k++) {
          if (growthRates[i][k] > best) {
            best = growthRates[i][k];
            countofBestSpots = 1;
          } else if (growthRates[i][k] == best) {
            countofBestSpots++;
          }

          SpawnSoldiers(rc);
        }
      }

      MapLocation[] bestSpots = new MapLocation[countofBestSpots];
      int index = 0;
      for (int i = 0; i < rc.getMapWidth(); i++) {
        for (int k = 0; k < rc.getMapHeight(); k++) {
          if (growthRates[i][k] == best) {
            bestSpots[index] = new MapLocation(i, k);
            index++;
          }

          SpawnSoldiers(rc);
        }
      }

      for (int i = 1; i < countofBestSpots; i++) {
        for (int j = i; j < countofBestSpots; j++) {
          int dist1 = rc.getLocation().distanceSquaredTo(bestSpots[j]);
          int dist2 = rc.getLocation().distanceSquaredTo(bestSpots[j]);

          if (dist1 < dist2) {
            MapLocation temp = bestSpots[j];
            bestSpots[j] = bestSpots[j - 1];
            bestSpots[j - 1] = temp;
          }

          SpawnSoldiers(rc);
        }
      }
      return bestSpots;
    } catch (Exception e) {
      e.printStackTrace();
      System.out.println("Utility Exception");
    }
    return empty;
  }
Пример #6
0
 public static void loop(RobotController theRC) throws Exception {
   init(theRC);
   while (true) {
     try {
       turn();
     } catch (Exception e) {
       e.printStackTrace();
     }
     rc.yield();
   }
 }
Пример #7
0
 public static void loop(RobotController theRC) throws GameActionException {
   Clock.yield();
   Bot.init(theRC);
   init();
   while (true) {
     try {
       turn();
     } catch (Exception e) {
       e.printStackTrace();
     }
     Clock.yield();
   }
 }
Пример #8
0
  @Override
  public void run() {
    while (true) {
      try {

      } catch (Exception e) {
        System.out.println(
            "Robot "
                + myRC.getRobot().getID()
                + " during round number "
                + Clock.getRoundNum()
                + " caught exception:");
        e.printStackTrace();
      }
    }
  }
Пример #9
0
  public static void soldierCode(RobotController myRC) throws GameActionException {
    rc = myRC;
    rallyPoint = findRallyPoint();
    while (true) {
      try {
        // returns all enemy robots within our sight range (aka that we can actually fight right now
        // or very soon)
        Robot[] closeEnemyRobots =
            rc.senseNearbyGameObjects(Robot.class, rc.getLocation(), 14, rc.getTeam().opponent());

        // returns all enemy robots reasonably close to us (aka that we should move toward and
        // potentially assist our allies in fighting)
        Robot[] enemyRobots = rc.senseNearbyGameObjects(Robot.class, 63, rc.getTeam().opponent());

        if (closeEnemyRobots.length == 0 && enemyRobots.length == 0) // no enemies are nearby
        {
          int command = HQCommand();
          switch (command) {
            case Constants.commandExpand:
              expand();
              break;
            case Constants.commandRally:
              rally();
              break;
            case Constants.commandEnemyNukeHalfDone:
              rally();
            case Constants.commandBuildIndividual: // TODO- implement this
              break;
          }
        } else {

          // enemy spotted
          localscan = false;
          MapLocation closestEnemy = findClosestRobot(enemyRobots);
          goToLocation(closestEnemy);
        }
      } catch (Exception e) {
        System.out.println("Soldier Exception");
        e.printStackTrace();
      }
      rc.yield();
    }
  }
Пример #10
0
  public static void run(RobotController myRC) {
    rc = myRC;
    barracks = findBarracks();
    while (true) {
      try {
        if (rc.getType() == RobotType.SOLDIER) {
          Robot[] enemyRobots =
              rc.senseNearbyGameObjects(
                  Robot.class, 50000, rc.getTeam().opponent()); // list of enemy robots
          if (enemyRobots.length == 0) {
            Clock.getRoundNum(); // gets the round number
            if (Clock.getRoundNum() < 250) {
              goToLocation(
                  barracks); // if you are before round 250 travel to the "barracks" or meeting
                             // place.
            } else {
              goToLocation(rc.senseEnemyHQLocation());
            }
          } else { // else attack the closest enemy
            int closestDist = 50000;
            MapLocation closestEnemy = null;
            for (int i = 0; i < enemyRobots.length; i++) {
              Robot enemyBot = enemyRobots[i];
              RobotInfo arobotInfo = rc.senseRobotInfo(enemyBot);
              int dist = arobotInfo.location.distanceSquaredTo(rc.getLocation());
              if (dist < closestDist) {
                closestDist = dist;
                closestEnemy = arobotInfo.location;
              }
            }
            goToLocation(closestEnemy);
          }
        } else {
          HqCommand();
        }

      } catch (Exception e) {
        e.printStackTrace();
      }
      rc.yield(); // end turn
    }
  }
Пример #11
0
  /** Calculates the order in which the archons were spawned. */
  public void senseArchonNumber() {
    Message[] messages = controller.getAllMessages();
    int min = 1;
    for (Message m : messages) {
      if (m.ints[0] >= min) {
        min = m.ints[0] + 1;
      }
    }

    archonNumber = min;

    Message m = new Message();
    m.ints = new int[] {min};
    try {
      controller.broadcast(m);
    } catch (Exception e) {
      System.out.println("----Caught Exception in senseArchonNumber.  Exception: " + e.toString());
    }
    System.out.println("Number: " + min);
  }
Пример #12
0
  public void placeTower() {
    idealTowerSpawnLocations = null;
    turnsWaitedForTowerSpawnLocationMessage = 0;
    towerSpawnFromLocation = null;
    towerSpawnLocation = null;
    turnsWaitedForMove = 0;

    ArrayList<MapLocation> towers; // //sensing.senseAlliedTeleporters();
    int towerID = BroadcastMessage.everyone;
    MapLocation location;
    Robot robot;

    towers = sensing.senseAlliedTowerLocations();
    if (towers.size() > 0) {
      // no teles in range, but there are other towers.  they should be talking to the tele and
      // should know the status of where to build
      try {
        location = navigation.findClosest(towers);
        if (controller.canSenseSquare(location) && location != null) {
          robot = controller.senseGroundRobotAtLocation(location);
          if (robot != null) towerID = robot.getID();
          else pa("cannot sense robot at " + location);
        }
        messaging.sendTowerBuildLocationRequest(towerID);
        setGoal(Goal.askingForTowerLocation);
        return;
      } catch (Exception e) {
        pa("----Caught exception in place tower. " + e.toString());
      }
    }

    // no towers in range, lets just ask everyone
    messaging.sendTowerBuildLocationRequest(BroadcastMessage.everyone);
    setGoal(Goal.askingForTowerLocation);
    return;
  }
Пример #13
0
  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();
      }
    }
  }
Пример #14
0
  // this method will advance one square towards a target and try to avoid enemies as much as
  // possible
  public static void avoidEnemiesMove(RobotController rc, MapLocation target) {
    try {
      // first we will find all enemy bots near us
      GameObject[] nearByBots = rc.senseNearbyGameObjects(Robot.class, 15, rc.getTeam().opponent());
      Direction direction = rc.getLocation().directionTo(target);

      // if we don't see anything then lets head towards target
      if (nearByBots.length == 0) {
        // rc.setIndicatorString(2, "No enemies detected");
        // rc.setIndicatorString(1, "x: "+target.x + " y: " + target.y);
        direction = rc.getLocation().directionTo(target);
        if (rc.canMove(direction)) // &&
        // !rc.senseTerrainTile(rc.getLocation().add(direction).add(direction)).equals(TerrainTile.VOID))
        {
          if (rc.isActive()) {
            rc.move(direction);
          }
        } else {
          MapLocation target2 = rc.getLocation().add(direction);
          if (rc.senseTerrainTile(target2).equals(TerrainTile.VOID)) {
            int j = 0;
            while (rc.senseTerrainTile(target2).equals(TerrainTile.VOID)) {
              rc.setIndicatorString(0, "" + j);
              j++;

              target2 = target2.add(direction);
            }
            Utilities.MoveMapLocation(rc, target2, false);
          }
          /*
          int distanceRight = 0;
          int distanceLeft = 0;
             direction = direction.rotateRight();
             while (!rc.canMove(direction) && rc.senseTerrainTile(rc.getLocation().add(direction)).equals(TerrainTile.VOID))
             {
                 direction = direction.rotateRight();
             }
             if (rc.isActive())
             {
                 if (rc.canMove(direction))
                 {
                     rc.move(direction);
                 }
             }
             */
        }
      }
      // otherwise we need to avoid them
      else {
        rc.setIndicatorString(2, "Avoiding enemies");
        rc.setIndicatorString(1, "Numb of Enemies: " + nearByBots.length);
        // now we will calculate the distance form all 5 spots towards are target and the distance
        // from that spot to all enemies we can see
        // we will pick the one with the greatest distance
        int[] distancesToLocations = new int[5];

        for (int k = 0; k < distancesToLocations.length; k++) {
          distancesToLocations[k] = 0;
        }
        MapLocation spot;
        Direction newDir;

        // first we look 90 to our right
        newDir = direction.rotateRight().rotateRight();
        for (int j = 0; j < 5; j++) {
          if (rc.canMove(newDir)) {
            spot = rc.getLocation().add(newDir);
            for (int i = 0; i < nearByBots.length; i++) {
              // System.out.println("entering for loop");
              distancesToLocations[j] += spot.distanceSquaredTo(rc.senseLocationOf(nearByBots[i]));
            }
          } else {
            distancesToLocations[j] = -123;
          }
          // every time through the loop we look one further to the left
          newDir.rotateLeft();
        }

        int indexOfLargest = 0;
        int largest = distancesToLocations[0];
        for (int j = 1; j < distancesToLocations.length; j++) {
          if (largest < distancesToLocations[j]) {
            indexOfLargest = j;
            largest = distancesToLocations[j];
          }
        }

        // now we orientate newDir to the right spot
        newDir = direction.rotateRight().rotateRight();
        for (int i = 0; i <= indexOfLargest; i++) {
          newDir = newDir.rotateLeft();
        }

        while (!rc.isActive()) {
          rc.yield();
        }

        // now we can finally move
        if (rc.isActive()) {
          if (rc.canMove(newDir)) {
            rc.move(newDir);
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Пример #15
0
  public static void MoveMapLocation(RobotController rc, MapLocation target, boolean sneak) {
    MapLocation[] pastLocations = new MapLocation[10];
    int side = 45;
    Direction dir;
    Direction newDir;
    rand = new Random();
    // we initialize pastLocations to hold our current location 5 times
    for (int i = 0; i < pastLocations.length; i++) {
      pastLocations[i] = rc.getLocation();
    }

    // this method will run until we get to our target location
    while (!rc.getLocation().equals(target)) {
      // we put the try block inside of the while loop so an exception won't terminate the method
      try {
        dir = rc.getLocation().directionTo(target);
        newDir = Direction.NONE;

        // simple shoot at an enemy if we see one will need to be improved later
        Robot[] nearbyEnemies = rc.senseNearbyGameObjects(Robot.class, 10, rc.getTeam().opponent());
        if (nearbyEnemies.length > 0 && rc.senseRobotInfo((Robot) nearbyEnemies[0]).health < 201) {
          fire(rc);
        }
        // if we can move towards target and we haven't been on the square recently then lets move
        else if (rc.canMove(dir)
            && !MapLocationInArray(rc, rc.getLocation().add(dir), pastLocations)) {
          newDir = dir;
          // if we found a direction to move then we go to it
          if (newDir != Direction.NONE) {
            // now we decide if we are going to sneak or run
            if (sneak) {
              // another check to make sure we don't throw any exceptions
              if (rc.isActive() && rc.canMove(newDir)) {
                // System.out.println(newDir);
                rc.sneak(newDir);
              }
            } else {
              // another check to make sure we don't throw any exceptions
              if (rc.isActive() && rc.canMove(newDir)) {
                rc.move(newDir);
              }
            }
          }
          side = 45;
        } else {
          // if their is a robot blocking our way then we just move in a random direction
          if (rc.senseObjectAtLocation(rc.getLocation().add(dir)) != null) {
            // newDir = directions[rand.nextInt(8)];
            MoveDirection(rc, dir, sneak);
          } else {
            // rc.setIndicatorString(2, "Looking elswhere");
            Direction dir2 = dir;
            MapLocation right;
            MapLocation left;
            dir2 = (dir.rotateRight());
            while (!rc.canMove(dir2)) {
              dir2 = dir2.rotateRight();
            }
            right = rc.getLocation().add(dir2);

            dir2 = dir.rotateLeft();
            while (!rc.canMove(dir2)) {
              dir2 = dir2.rotateLeft();
            }

            left = rc.getLocation().add(dir2);

            // left seems better so lets go that way
            if (left.distanceSquaredTo(target) < right.distanceSquaredTo(target)) {
              side = 1;
            }
            // right seems better so lets try that way
            else {
              side = 0;
            }

            // we will go hugging one side of obstacle until we get back on our original line
            while (!dir2.equals(dir) && !rc.getLocation().equals(target)) // && rc.canMove(dir2))
            {
              try {
                if (rc.isActive()) {
                  // rc.setIndicatorString(1, "Trying to Avoid");
                  // rc.setIndicatorString(2, ""+side);
                  dir2 = rc.getLocation().directionTo(target);
                  if (rc.canMove(dir2)
                      && !MapLocationInArray(rc, rc.getLocation().add(dir2), pastLocations)) //  &&
                  // !rc.senseTerrainTile(rc.getLocation().add(dir2).add(dir2)).equals(TerrainTile.VOID))
                  {
                    // rc.setIndicatorString(0, "Going straight");
                  } else {
                    for (int i = 0; i < 4; i++) {
                      if (side == 1) {
                        dir2 = dir2.rotateLeft();
                      } else {
                        dir2 = dir2.rotateRight();
                      }
                      if (rc.senseTerrainTile(rc.getLocation().add(dir2))
                          .equals(TerrainTile.OFF_MAP)) {
                        dir2 = Direction.NONE;
                        i = 48;
                      } else if ((rc.canMove(dir2)
                          || (rc.senseObjectAtLocation(rc.getLocation().add(dir2))
                              != null))) // && !MapLocationInArray(rc, rc.getLocation().add(dir2),
                      // pastLocations))// &&
                      // !rc.senseTerrainTile(rc.getLocation().add(dir2).add(dir2)).equals(TerrainTile.VOID))
                      {
                        i = 48;
                      } else if (i == 3) {
                        dir2 = Direction.NONE;
                        // rc.setIndicatorString(1, "We failed to find a spot");
                      }
                    }
                  }

                  // if we can move
                  if (dir2 != Direction.NONE) {
                    if (rc.isActive()) {
                      if (rc.canMove(dir2)) {
                        if (sneak) {
                          rc.sneak(dir2);
                        } else {
                          rc.move(dir2);
                        }
                      } else {
                        MoveDirection(rc, dir2, sneak);
                      }
                    }
                  } else {
                    if (side == 1) {
                      side = 0;
                    } else {
                      side = 1;
                    }
                  }
                }
                // rc.setIndicatorString(0, "Dir: "+ dir +" Dir2: " + dir2);
              } catch (Exception e) {
                // tell the console we through an exception in utility object for debug purposes
                // System.out.println("Utility Exception");
                // System.out.println(e.toString());
                e.printStackTrace();
                rc.yield();
              }
              if (!rc.getLocation().equals(pastLocations[(pastLocations.length - 1)])) {
                for (int j = 0; j < (pastLocations.length - 1); j++) {
                  pastLocations[j] = pastLocations[j + 1];
                  // System.out.println(pastLocations[j]);
                }
                // stick current local into array
                pastLocations[(pastLocations.length - 1)] = rc.getLocation();
              }

              rc.yield();
            }
            // rc.setIndicatorString(1, "Not trying to Avoid");
          }
        }

        // now we  shift everything up one in pastLocations
        if (rc.getLocation() != pastLocations[(pastLocations.length - 1)]) {
          for (int j = 0; j < (pastLocations.length - 1); j++) {
            pastLocations[j] = pastLocations[j + 1];
            // System.out.println(pastLocations[j]);
          }
          // stick current local into array
          pastLocations[(pastLocations.length - 1)] = rc.getLocation();
        }

        rc.yield();
      } catch (Exception e) {
        // tell the console we through an exception in utility object for debug purposes
        System.out.println("Utility Exception");
        e.printStackTrace();
        // System.out.println(e.toString());
        rc.yield();
      }
    }
  }
Пример #16
0
  public static void MoveDirection(RobotController rc, Direction dir, boolean sneak) {
    Direction newDir = Direction.NONE;
    int counter = 1;
    try {
      // here we do some checks to make sure that we don't throw any exceptions
      if (rc.isActive()) {

        if (dir != Direction.NONE && dir != Direction.OMNI) {
          if (!rc.canMove(dir)) {
            // now we loop through the other 7 directions to find one that works
            for (int i = 0; i < 7; i++) {
              newDir = dir;
              // first look right
              if (i % 2 == 0) {
                // now we rotate 45 right a certain numb of times
                for (int j = 0; j < counter; j++) {
                  newDir = newDir.rotateRight();
                }
              }
              // the look left
              else {
                // now we rotate 45 left a certain numb of times
                for (int j = 0; j < counter; j++) {
                  newDir = newDir.rotateLeft();
                }
                // now after we have looked both ways we update counter
                counter++;
              }
              // at end of for loop we check to see if we can move or if we need to keep looking
              if (rc.canMove(newDir)) {
                i = 48;
              }
              // if we have gone through all our options and can't move then we will wait
              else if (i == 5 && !rc.canMove(newDir)) {
                newDir = Direction.NONE;
              }
            }
          }
          // we are going to move in the direction of newDir and as we can move in direction dir we
          // assign newDir to it
          else {
            newDir = dir;
          }

          if (newDir != Direction.NONE) {

            // now we decide if we are going to sneak or run
            if (sneak) {
              // another check to make sure we don't throw any exceptions
              if (rc.isActive() && rc.canMove(newDir)) {
                rc.sneak(newDir);
              }
            } else {
              // another check to make sure we don't throw any exceptions
              if (rc.isActive() && rc.canMove(newDir)) {
                rc.move(newDir);
              }
            }
          }
        }
      }
    } catch (Exception e) {
      // tell the console we through an exception in utility object for debug purposes
      System.out.println("Utility Exception");
      e.printStackTrace();
      // System.out.println(newDir);
    }
  }
Пример #17
0
  @Override
  public void doAction() {
    try {
      if (rc.isActive()) {
        /*				MapLocation myLocation = rc.getLocation();
         */ MapLocation enemyHQ = rc.senseEnemyHQLocation();
        MapLocation alliedHQ = rc.senseHQLocation();
        Robot[] enemyRobots =
            rc.senseNearbyGameObjects(Robot.class, 100000, rc.getTeam().opponent());
        Robot[] alliedRobots = rc.senseNearbyGameObjects(Robot.class, 100000, rc.getTeam());
        Robot[] nearbyEnemyRobots =
            rc.senseNearbyGameObjects(Robot.class, 14, rc.getTeam().opponent());
        Robot[] nearbyAlliedRobots = rc.senseNearbyGameObjects(Robot.class, 14, rc.getTeam());

        MapLocation[] myEncamp = rc.senseAlliedEncampmentSquares();
        if (enemyRobots.length > 0) {
          int closestDist = 10000000;
          MapLocation closestEnemy = null;
          MapLocation closestAlly = null;
          for (Robot r : enemyRobots) {
            RobotInfo aRobotInfo = rc.senseRobotInfo(r);
            int dist = aRobotInfo.location.distanceSquaredTo(rc.getLocation());
            if (dist < closestDist) {
              closestDist = dist;
              ;
              closestEnemy = aRobotInfo.location;
            }
          }
          closestDist = 10000000;
          for (Robot r : alliedRobots) {
            RobotInfo aRobotInfo = rc.senseRobotInfo(r);
            int dist = aRobotInfo.location.distanceSquaredTo(rc.getLocation());
            if (dist < closestDist) {
              closestDist = dist;
              ;
              closestAlly = aRobotInfo.location;
            }
          }
          if (nearbyAlliedRobots.length >= nearbyEnemyRobots.length) {
            goToLocation(closestEnemy);
          } else if (rc.getLocation().distanceSquaredTo(alliedHQ)
              >= rc.getLocation().distanceSquaredTo(enemyHQ)) {
            goToLocation(enemyHQ);
          } else {
            if (myEncamp.length > 0) {
              int closestDistance = 10000000;
              MapLocation closestEncampment = null;
              for (MapLocation ml : myEncamp) {
                int dist = ml.distanceSquaredTo(rc.getLocation());
                if (dist < closestDistance) {
                  closestDistance = dist;
                  closestEncampment = ml;
                }
              }
              retreat(closestEnemy, closestEncampment);
            }
          }
        } else {
          goToLocation(enemyHQ);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Пример #18
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();
        }
      }
    }
  }