示例#1
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();
   }
 }
示例#2
0
  public Robot(RobotController rc) {
    this.rc = rc;
    rand = new Random(rc.getID());
    team = rc.getTeam();
    enemy = team.opponent();
    currentLocation = rc.getLocation();

    RobotType type = rc.getType();
    senseRadius = type.sensorRadiusSquared;
    attackRadius = type.attackRadiusSquared;
  }
示例#3
0
 public RobotRunner(RobotController rcin) {
   this.rc = rcin;
   randall = new Random(rc.getID());
   myTeam = rc.getTeam();
   enemyTeam = myTeam.opponent();
   eden = rc.getInitialArchonLocations(myTeam)[0];
   memory = new Information(); // Everybody has a brain
   robotSenseRadius = (int) Math.sqrt(rc.getType().sensorRadiusSquared);
   marco = new Move();
   enigma = new MessageHash();
 }
示例#4
0
  // Beavers
  private static void runBeaver() {
    strategy = new BuildStrategy(rc);
    rand = new Random(rc.getID());

    while (true) {
      threats.update();
      myLoc = rc.getLocation();

      if (rc.isCoreReady()) {
        RobotType build = strategy.getBuildOrder();
        if (build != null) tryBuild(rc.getLocation().directionTo(threats.enemyHQ), build);
      }

      // Attack if there is an enemy in sight
      if (rc.isWeaponReady()) attackWeakest();

      double ore = rc.senseOre(rc.getLocation());

      // Move if we can and want to
      if (rc.isCoreReady()) {
        boolean ignoreThreat = overwhelms();

        if (!ignoreThreat && shouldRetreat()) {
          doRetreatMove(); // Pull back if in range of the enemy guns
        } else {
          doMinerMove();
          if (ore == 0 && rc.isCoreReady()) { // We didn't find ore nearby
            doSearchMove();
          }
        }
      }

      // Mine if possible
      if (rc.isCoreReady() && ore > 0) {
        try {
          rc.mine();
        } catch (GameActionException e) {
          System.out.println("Mining Exception");
          // e.printStackTrace();
        }
      }

      doTransfer();

      rc.yield();
    }
  }
示例#5
0
 /** @param rc */
 public static void run(RobotController rc) {
   rand = new Random(rc.getID());
   myTeam = rc.getTeam();
   while (true) {
     if (rc.getType() == RobotType.ARCHON) {
       archon(rc);
     } else if (rc.getType() == RobotType.SCOUT) {
       scout(rc);
     } else if (rc.getType().equals(RobotType.TURRET)) {
       turret(rc);
     } else if (rc.getType().equals(RobotType.TTM)) {
       ttm(rc);
     } else if (rc.getType() == RobotType.SOLDIER) {
       soldier(rc);
     } else if (rc.getType() == RobotType.GUARD) {
       guard(rc);
     } else if (rc.getType() == RobotType.VIPER) {
       viper(rc);
     }
   }
 }
示例#6
0
  /*
   * Beavers and miners without ore do this
   */
  private static void doSearchMove() {
    // We keep moving in the direction we were going
    // When blocked we turn left or right depending on our unique ID

    rc.setIndicatorString(2, "Mining: No ore - searching");
    if (lastMove == null || rand.nextInt(10) == 1)
      lastMove = directions[rand.nextInt(directions.length)];

    Direction startDir = lastMove;
    while (!rc.canMove(lastMove) || threats.isThreatened(myLoc.add(lastMove))) {
      if (rc.getID() % 2 == 0) lastMove = lastMove.rotateLeft();
      else lastMove = lastMove.rotateRight();
      if (lastMove == startDir) // We are trapped
      return;
    }
    try {
      rc.move(lastMove);
    } catch (GameActionException e) {
      System.out.println("Move exception");
      // e.printStackTrace();
    }
  }
  public void run(final RobotController robotController) throws GameActionException {

    final MapInfoModule mapInfoModule = new MapInfoModule();

    final CombatModule combatModule = new CombatModule();
    final CommunicationModule communicationModule = new CommunicationModule(mapInfoModule);
    final DirectionModule directionModule = new DirectionModule(robotController.getID());
    final MovementModule movementModule = new MovementModule();
    final RubbleModule rubbleModule = new RubbleModule();

    final Team currentTeam = robotController.getTeam();
    int turnsStuck = 0;

    while (true) {

      RobotType type = robotController.getType();
      MapLocation currentLocation = robotController.getLocation();

      // update communication

      communicationModule.processIncomingSignals(robotController);

      // let's verify existing information

      communicationModule.verifyCommunicationsInformation(robotController, null, false);

      if (type == RobotType.TTM) {

        // MOVE

        // let's get the best assignment

        CommunicationModuleSignal objectiveSignal = null;
        int closestObjectiveLocationDistance = Integer.MAX_VALUE;

        final Enumeration<CommunicationModuleSignal> zombieDenCommunicationModuleSignals =
            communicationModule.zombieDens.elements();
        while (zombieDenCommunicationModuleSignals.hasMoreElements()) {

          final CommunicationModuleSignal signal =
              zombieDenCommunicationModuleSignals.nextElement();
          final int distance = signal.location.distanceSquaredTo(currentLocation);
          if (distance < closestObjectiveLocationDistance) {

            objectiveSignal = signal;
            closestObjectiveLocationDistance = distance;
          }
        }

        final Enumeration<CommunicationModuleSignal> enemyArchonCommunicationModuleSignals =
            communicationModule.enemyArchons.elements();
        while (enemyArchonCommunicationModuleSignals.hasMoreElements()) {

          final CommunicationModuleSignal signal =
              enemyArchonCommunicationModuleSignals.nextElement();
          final int distance =
              signal.location.distanceSquaredTo(currentLocation)
                  * 6; // multiplying by 6 to prioritize the dens
          if (distance < closestObjectiveLocationDistance) {

            objectiveSignal = signal;
            closestObjectiveLocationDistance = distance;
          }
        }

        final Enumeration<CommunicationModuleSignal> enemyTurretCommunicationModuleSignals =
            communicationModule.enemyTurrets.elements();
        while (enemyTurretCommunicationModuleSignals.hasMoreElements()) {

          final CommunicationModuleSignal signal =
              enemyTurretCommunicationModuleSignals.nextElement();
          final int distance = signal.location.distanceSquaredTo(currentLocation) * 20;
          if (distance < closestObjectiveLocationDistance) {

            objectiveSignal = signal;
            closestObjectiveLocationDistance = distance;
          }
        }

        boolean shouldMove = true;
        Direction desiredMovementDirection = null;
        Direction targetRubbleClearanceDirection = null;

        // check to make sure we are safe

        final RobotInfo[] enemies =
            robotController.senseHostileRobots(
                currentLocation, robotController.getType().sensorRadiusSquared);

        if (robotController.isCoreReady() && enemies.length > 0) {

          final Direction fleeDirection =
              directionModule.averageDirectionTowardDangerousRobotsAndOuterBounds(
                  robotController, enemies);
          if (fleeDirection != null) {

            final Direction fleeMovementDirection =
                directionModule.recommendedMovementDirectionForDirection(
                    fleeDirection.opposite(), robotController, false);
            if (fleeMovementDirection != null) {

              robotController.move(fleeMovementDirection);
              currentLocation = robotController.getLocation();
              robotController.setIndicatorString(
                  1, fleeDirection.name() + " " + fleeMovementDirection.name());
            }
          }
        }

        // check if there are nearby signals

        if (desiredMovementDirection == null) {

          int closestSignalDistance = Integer.MAX_VALUE;
          MapLocation closestSignalLocation = null;

          final ArrayList<Signal> notifications = communicationModule.notifications;
          for (int i = 0; i < notifications.size(); i++) {

            final Signal signal = notifications.get(i);
            final int distance = currentLocation.distanceSquaredTo(signal.getLocation());
            if (distance < closestSignalDistance) {

              closestSignalDistance = distance;
              closestSignalLocation = signal.getLocation();
            }
          }
          if (closestSignalLocation != null) {

            desiredMovementDirection = currentLocation.directionTo(closestSignalLocation);
          }
        }

        // now let's try move toward an assignment

        if (robotController.isCoreReady()
            && communicationModule.initialInformationReceived
            && shouldMove) {

          // check if we have an objective

          if (desiredMovementDirection == null) {

            if (objectiveSignal != null) {

              final MapLocation objectiveLocation = objectiveSignal.location;
              if (objectiveLocation.distanceSquaredTo(currentLocation) >= 8) {

                desiredMovementDirection = currentLocation.directionTo(objectiveLocation);
              }
            }
          }

          // try move towards archon starting positions

          if (desiredMovementDirection == null) {

            int closestArchonDistance = Integer.MAX_VALUE;
            MapLocation closestArchonLocation = null;

            final MapLocation[] archonLocations =
                robotController.getInitialArchonLocations(robotController.getTeam().opponent());
            for (int i = 0; i < archonLocations.length; i++) {

              final MapLocation location = archonLocations[i];
              final int distance = currentLocation.distanceSquaredTo(location);
              if (distance < closestArchonDistance) {

                closestArchonDistance = distance;
                closestArchonLocation = location;
              }
            }
            if (closestArchonLocation != null) {

              desiredMovementDirection = currentLocation.directionTo(closestArchonLocation);
            }
          }

          // process movement

          if (desiredMovementDirection != null) {

            final Direction recommendedMovementDirection =
                directionModule.recommendedMovementDirectionForDirection(
                    desiredMovementDirection, robotController, false);
            final MapLocation recommendedMovementLocation =
                recommendedMovementDirection != null
                    ? currentLocation.add(recommendedMovementDirection)
                    : null;
            if (recommendedMovementDirection != null
                && !movementModule.isMovementLocationRepetitive(
                    recommendedMovementLocation, robotController)) {

              robotController.move(recommendedMovementDirection);
              movementModule.addMovementLocation(recommendedMovementLocation, robotController);
              currentLocation = robotController.getLocation();
              turnsStuck = 0;
            }
          }
        }

        // unpack if we're safe

        RobotInfo[] nearbyTeammates = robotController.senseNearbyRobots(8, currentTeam);
        RobotInfo[] nearbySoldiers =
            combatModule.robotsOfTypesFromRobots(
                nearbyTeammates, new RobotType[] {RobotType.SOLDIER});

        if (nearbySoldiers.length > 2) {

          robotController.unpack();
        }

      } else {

        // ATTACK

        final RobotInfo[] enemies =
            robotController.senseHostileRobots(
                currentLocation, robotController.getType().attackRadiusSquared);

        RobotInfo bestEnemy = this.getBestEnemyToAttackFromEnemies(robotController, enemies);

        // handle attacking

        if (bestEnemy != null) {

          if (robotController.isWeaponReady()) {

            // we can attack the enemy

            robotController.attackLocation(bestEnemy.location);
            if (bestEnemy.type != RobotType.ZOMBIEDEN) {

              communicationModule.broadcastSignal(
                  robotController,
                  CommunicationModule.maximumFreeBroadcastRangeForRobotType(
                      robotController.getType()));
            }
          }
        }

        // pack if we aren't near soldiers

        RobotInfo[] nearbyTeammates =
            robotController.senseNearbyRobots(type.sensorRadiusSquared, currentTeam);
        RobotInfo[] nearbySoldiers =
            combatModule.robotsOfTypesFromRobots(
                nearbyTeammates, new RobotType[] {RobotType.SOLDIER});

        if (nearbySoldiers.length < 3) {

          robotController.pack();
        }
      }

      Clock.yield();
    }
  }
示例#8
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();
      }
    }
  }
示例#9
0
  public static void run() throws GameActionException {
    rc = RobotPlayer.rc;
    rand = new Random(rc.getID());

    // build scouts right away
    buildRobot(RobotType.SCOUT);

    while (true) {
      /*
       * INPUT
       */
      if (rc.getLocation().equals(goal)) {
        goal = null; // you made it to the goal
        past10Locations =
            new ArrayList<MapLocation>(); // delete the slug trail after you reach your goal
      }

      // sense locations around you
      nearbyMapLocations =
          MapLocation.getAllMapLocationsWithinRadiusSq(
              rc.getLocation(), rc.getType().sensorRadiusSquared);

      // parts locations
      nearbyPartsLocations = rc.sensePartLocations(RobotType.ARCHON.sensorRadiusSquared);

      // find the nearest mapLocation with the most parts
      double maxParts = 0;
      MapLocation nearbyLocationWithMostParts = null;
      for (MapLocation loc : nearbyPartsLocations) {
        // add to locationsWithParts arraylist
        if (locationsWithParts.contains(loc) == false) {
          locationsWithParts.add(loc);
        }

        // find the location with the most parts
        double partsAtLoc = rc.senseParts(loc);
        if (partsAtLoc > maxParts) {
          maxParts = partsAtLoc;
          nearbyLocationWithMostParts = loc;
        }
      }

      // read signals
      Signal[] signals = rc.emptySignalQueue();
      for (Signal signal : signals) {
        // check if the signal has parts at the location
        int[] message = signal.getMessage();
        if (message != null && message[0] == Utility.PARTS_CODE) {
          // add that location to the locationsWithParts arraylist
          locationsWithParts.add(signal.getLocation());
        }
      }

      // sense robots
      MapLocation myLoc = rc.getLocation();
      robots = rc.senseNearbyRobots();
      foes = new ArrayList<RobotInfo>();
      foesWithinAttackRange = new ArrayList<RobotInfo>();
      for (RobotInfo robot : robots) {
        if (robot.team == Team.ZOMBIE
            || robot.team == rc.getTeam().opponent()) // if the robot is a foe
        {
          foes.add(robot);

          if (myLoc.distanceSquaredTo(robot.location) < robot.type.attackRadiusSquared) {
            foesWithinAttackRange.add(robot);
          }
        }
      }
      int nearbyFoes = foes.size();
      int nearbyFoesInAttackRange = foesWithinAttackRange.size();

      /*//check stats
      double health = rc.getHealth();
      int infectedTurns = rc.getInfectedTurns();
      int robotsAlive = rc.getRobotCount();
      */

      /*
       * OUPUT
       */

      // what to do
      if (nearbyFoes == 0) // if there are no foes in sight
      {
        if (rc.getTeamParts() >= RobotType.TURRET.partCost) // build if you can
        {
          buildRobots();
        } else {
          if (maxParts > 0 && goal == null) // if there are parts nearby
          {
            // make that the goal
            goal = nearbyLocationWithMostParts;
          } else if (goal == null) // if there aren't and there is no goal
          {
            // build something or find new parts
            // 80% build, 20% new parts
            if (locationsWithParts.size() > 0 && rand.nextFloat() > .8) {
              goal = locationsWithParts.get(0);
              locationsWithParts.remove(0);
              goalIsASafeLocation = false;
            }
            // calculate the next goal - maybe a new parts location you got via signal
          } else if (goal != null) // if there is a goal, move there
          {
            moveToLocation(goal);
          }
        }
      } else // there are foes nearby
      {
        // message for help!
        if (Math.random() < probSignal) {
          rc.broadcastSignal(archonInTroubleSignalRadiusSquared);
        }

        if (nearbyFoesInAttackRange > 0) {
          goal = findSaferLocation();
          rc.setIndicatorString(0, "" + goal.x + " " + goal.y);
          goalIsASafeLocation = true;
          moveToLocation(goal);
        }
      }

      Clock.yield();
    }
  }
示例#10
0
  public static void run(RobotController rc) {
    int myAttackRange = 0;
    Random rand = new Random(rc.getID());
    Team myTeam = rc.getTeam();
    Team enemyTeam = myTeam.opponent();

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

    while (true) {
      // This is a loop to prevent the run() method from returning. Because of the Clock.yield()
      // at the end of it, the loop will iterate once per game round.
      try {
        MapLocation myLoc = rc.getLocation();
        // If this robot type can attack, check for enemies within range and attack one
        // guards only attack ZOMBIES
        RobotInfo[] enemiesWithinRange =
            rc.senseNearbyRobots(rc.getType().sensorRadiusSquared, Team.ZOMBIE);
        RobotInfo[] friendliesWithinRange =
            rc.senseNearbyRobots(rc.getType().sensorRadiusSquared, myTeam);
        int closestArchonDist = 60;
        MapLocation closestArchonLoc = null;
        for (RobotInfo f : friendliesWithinRange) {
          if (f.type == RobotType.ARCHON) {
            MapLocation curArchonLoc = f.location;
            if (myLoc.distanceSquaredTo(curArchonLoc) < closestArchonDist) {
              closestArchonDist = myLoc.distanceSquaredTo(curArchonLoc);
              closestArchonLoc = curArchonLoc;
            }
          }
        }

        if (closestArchonLoc != null) {
          if (enemiesWithinRange.length > 0) {
            RobotInfo nearestEnemyToArchon = enemiesWithinRange[0];
            int nearestEnemyToArchonDist =
                nearestEnemyToArchon.location.distanceSquaredTo(closestArchonLoc);
            for (RobotInfo e : enemiesWithinRange) {
              if (e.location.distanceSquaredTo(closestArchonLoc) < nearestEnemyToArchonDist) {
                nearestEnemyToArchonDist = e.location.distanceSquaredTo(closestArchonLoc);
                nearestEnemyToArchon = e;
              }
            }
            // only attack/move towards if it's within 24 range of the archon
            if (nearestEnemyToArchonDist <= 24) {
              // Check if weapon is ready
              if (rc.isWeaponReady() && rc.canAttackLocation(nearestEnemyToArchon.location)) {
                rc.attackLocation(nearestEnemyToArchon.location);
              } else { // otherwise try to move towards the enemy
                if (rc.isCoreReady()) {
                  // try to move towards enemy
                  Direction dirToMove = myLoc.directionTo(nearestEnemyToArchon.location);
                  if (rc.canMove(dirToMove)) {
                    rc.move(dirToMove);
                  } else if (rc.canMove(dirToMove.rotateLeft())) {
                    rc.move(dirToMove.rotateLeft());
                  } else if (rc.canMove(dirToMove.rotateRight())) {
                    rc.move(dirToMove.rotateRight());
                  }
                }
              }
            }
          } else { // if no enemies, it should try to circle nearest archon
            if (rc.isCoreReady()) {
              Direction dirToMove = myLoc.directionTo(closestArchonLoc);
              if (rc.canMove(dirToMove)) {
                rc.move(dirToMove);
              } else if (rc.canMove(dirToMove.rotateLeft())) {
                rc.move(dirToMove.rotateLeft());
              } else if (rc.canMove(dirToMove.rotateRight())) {
                rc.move(dirToMove.rotateRight());
              }
            }
          }
        } else {
          rc.setIndicatorString(1, "no archon");
          // if no archons nearby, move randomly and attack
          if (enemiesWithinRange.length > 0) {
            RobotInfo nearestEnemy = enemiesWithinRange[0];
            int nearestEnemyDist = nearestEnemy.location.distanceSquaredTo(myLoc);
            for (RobotInfo e : enemiesWithinRange) {
              if (e.location.distanceSquaredTo(myLoc) < nearestEnemyDist) {
                nearestEnemyDist = e.location.distanceSquaredTo(myLoc);
                nearestEnemy = e;
              }
            }
            if (rc.isWeaponReady() && rc.canAttackLocation(nearestEnemy.location)) {
              rc.attackLocation(nearestEnemy.location);
            } else { // otherwise try to move towards the enemy
              if (rc.isCoreReady()) {
                // try to move towards enemy
                Direction dirToMove = myLoc.directionTo(nearestEnemy.location);
                if (rc.canMove(dirToMove)) {
                  rc.move(dirToMove);
                } else if (rc.canMove(dirToMove.rotateLeft())) {
                  rc.move(dirToMove.rotateLeft());
                } else if (rc.canMove(dirToMove.rotateRight())) {
                  rc.move(dirToMove.rotateRight());
                }
              }
            }
          } else if (rc.isCoreReady()) {
            Direction dirToMove = RobotPlayer.directions[(rand.nextInt(8))];
            for (int i = 0; i < 8; i++) {
              if (rc.canMove(dirToMove)) {
                rc.move(dirToMove);
                break;
              } else {
                dirToMove = dirToMove.rotateLeft();
              }
            }
          }
        }
        Clock.yield();
      } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
      }
    }
  }
示例#11
0
  public static void run(RobotController rc) {
    int myAttackRange = 0;
    Random rand = new Random(rc.getID());
    Team myTeam = rc.getTeam();
    Team enemyTeam = myTeam.opponent();

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

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

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

        boolean shouldAttack = false;

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

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

        Clock.yield();
      } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
      }
    }
  }
示例#12
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();
        }
      }
    }
  }