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

    if (rc.isWeaponReady()) {
      if (rc.canAttackLocation(enemyArchon.location)) {
        rc.attackLocation(enemyArchon.location);
      }
    }
  }
Ejemplo n.º 2
0
  private MapLocation traceNext(MapLocation currentLoc, Direction faceDir, boolean cw) {

    // Put isTraversable first so that the second while loop will rotate the
    // direction to walkable position
    // Try code reuse in the future

    int step = 0;

    if (cw) {
      while (isTraversable(currentLoc.add(faceDir)) && step < 8) {
        faceDir = faceDir.rotateRight();
        step++;
      }
      step = 0;
      while (!isTraversable(currentLoc.add(faceDir)) && step < 8) {
        faceDir = faceDir.rotateLeft();
        step++;
      }
    } else {
      while (isTraversable(currentLoc.add(faceDir)) && step < 8) {
        faceDir = faceDir.rotateLeft();
        step++;
      }
      step = 0;
      while (!isTraversable(currentLoc.add(faceDir)) && step < 8) {
        faceDir = faceDir.rotateRight();
        step++;
      }
    }

    return currentLoc.add(faceDir);
  }
Ejemplo n.º 3
0
 private static void shootRandomly() throws GameActionException {
   if (rc.getRoundNum() > 500) {
     Direction trueAway = alpha.directionTo(here);
     Direction away = trueAway;
     MapLocation enemyLocation = here;
     int count = 0;
     while (count < 4 || rc.canAttackLocation(enemyLocation.add(away))) {
       enemyLocation = enemyLocation.add(away);
       away =
           (new Direction[] {
                 trueAway,
                 trueAway.rotateLeft(),
                 trueAway.rotateRight(),
                 trueAway.rotateLeft().rotateLeft(),
                 trueAway.rotateRight().rotateRight()
               })
               [rand.nextInt(5)];
       count++;
       if (rc.canSenseLocation(enemyLocation) && !rc.onTheMap(enemyLocation)) {
         return;
       }
     }
     if (rc.canAttackLocation(enemyLocation)) {
       rc.attackLocation(enemyLocation);
       return;
     }
   }
 }
Ejemplo n.º 4
0
  public static void enemyMicro(RobotController rc, RobotInfo bestEnemy)
      throws GameActionException {
    // Prioritize movement
    Direction d = myLoc.directionTo(bestEnemy.location);
    if (rc.isCoreReady()) {
      if (rc.getHealth() > (numEnemySoldiers + 1) * RobotType.SOLDIER.attackPower) {
        // If the enemy can be killed but we're not in range, move forward
        if (!rc.canAttackLocation(bestEnemy.location)
            && bestEnemy.health <= RobotType.SOLDIER.attackPower) {
          if (rc.canMove(d)) {
            rc.move(d);
          } else if (rc.canMove(d.rotateLeft())) {
            rc.move(d.rotateLeft());
          } else if (rc.canMove(d.rotateRight())) {
            rc.move(d.rotateRight());
          }
          // If not in range, see if we should move in by comparing soldier health
        } else {
          double totalOurSoldierHealth = 0;
          RobotInfo[] allies = rc.senseNearbyRobots(bestEnemy.location, 18, rc.getTeam());
          for (RobotInfo ally : allies) {
            if (ally.type == RobotType.SOLDIER) {
              if (ally.health > numEnemySoldiers * RobotType.SOLDIER.attackPower) {
                totalOurSoldierHealth += ally.health;
              }
            }
          }
          // If we feel that we are strong enough, rush in.
          if (totalOurSoldierHealth > totalEnemySoldierHealth) {
            if (!rc.canAttackLocation(bestEnemy.location)) {
              if (rc.canMove(d)) {
                rc.move(d);
              } else if (rc.canMove(d.rotateLeft())) {
                rc.move(d.rotateLeft());
              } else if (rc.canMove(d.rotateRight())) {
                rc.move(d.rotateRight());
              }
            }
          } else if (4 * totalOurSoldierHealth < 3 * totalEnemySoldierHealth) {
            if (rc.canMove(d.opposite())) {
              rc.move(d.opposite());
            } else if (rc.canMove(d.opposite().rotateLeft())) {
              rc.move(d.opposite().rotateLeft());
            } else if (rc.canMove(d.opposite().rotateRight())) {
              rc.move(d.opposite().rotateRight());
            }
          }
        }
      }
    }

    // Attack whenever you can
    if (rc.isWeaponReady()) {
      if (rc.canAttackLocation(bestEnemy.location)) {
        broadcastingAttack(rc, bestEnemy);
      }
    }
  }
Ejemplo n.º 5
0
  public static void goToGoal(
      Direction movingDirection, boolean greaterAttackForce, int numAllAllies)
      throws GameActionException {

    // forwardish(randomDirection());
    if (RobotPlayer.soldierLeader) {
      RobotPlayer.rc.setIndicatorString(2, "I am a leader about to move");
      if (numAllAllies > 7 && greaterAttackForce) {
        RobotPlayer.rc.setIndicatorString(2, "I am a leader with a strong force");
        forwardish(movingDirection);
      } else if (greaterAttackForce) {
        // TODO what to do with small groups?
        RobotPlayer.rc.setIndicatorString(2, "I am a leader with a  larger force");
        if (RobotPlayer.rc.getRoundNum() % 8 == 0) forwardish(movingDirection);
      } else {
        RobotPlayer.rc.setIndicatorString(2, "greater attack force? " + greaterAttackForce);
        if (RobotPlayer.rc.getRoundNum() % 5 == 0) {
          RobotPlayer.movingDirection = movingDirection.rotateRight();
          forwardish(RobotPlayer.movingDirection);
        }
      }

    } else if (RobotPlayer.rc.getType() == RobotType.SOLDIER) {
      if (greaterAttackForce || RobotPlayer.haveLeader) {
        forwardish(movingDirection); // keep going where we are going
      } else {
        forwardish(
            movingDirection
                .rotateRight()); // wiat for leader, turn (should i go in random direction?)
      }
    } else if (RobotPlayer.rc.getType() == RobotType.GUARD) {
      if (greaterAttackForce || RobotPlayer.haveLeader) {
        forwardish(movingDirection); // keep going where we are going
      } else {
        forwardish(
            movingDirection
                .rotateRight()); // wiat for leader, turn (should i go in random direction?)
      }
    }
    // else {
    //
    //			if (greaterAttackForce){
    //				forwardish(movingDirection);//keep going where we are going
    //			}
    //
    //			forwardish(randomDirection());//wiat for leader, go in random direction
    ////			forwardish(movingDirection);
    //		}
    //		//			else if(RobotPlayer.haveLeader){
    //		//
    //		//		}else{
    //
    //
    //
    //		//}

  }
Ejemplo n.º 6
0
  // move to a maplocation
  public static void moveToLocation(MapLocation m) throws GameActionException {
    MapLocation currentLoc = rc.getLocation();
    Direction directionToM = currentLoc.directionTo(m);
    Direction actualDirectionToMove = directionToM;

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

    MapLocation locationToMoveTo = currentLoc.add(directionToM);

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

        while (right.equals(actualDirectionToMove) == false) {
          if (canMoveThere(right, rightLoc)) {
            moveInDirection(right);
            right = actualDirectionToMove;
          } else {
            right = right.rotateRight();
            rightLoc = currentLoc.add(right);
          }
        }
      }
    }
  }
Ejemplo n.º 7
0
 public static Direction[] bestDir(Direction dir) {
   Direction[] bestDir = {
     dir,
     dir.rotateLeft(),
     dir.rotateRight(),
     dir.rotateLeft().rotateLeft(),
     dir.rotateRight().rotateRight()
   };
   return bestDir;
 }
Ejemplo n.º 8
0
  private static void runMissile() {
    int lastTurn = Clock.getRoundNum() + GameConstants.MISSILE_LIFESPAN;
    int[] damageRange = {0, 8, 15, 24, 35, 48};
    MapLocation target = null;
    boolean targetMoves = true;

    while (true) {
      myLoc = rc.getLocation();
      int turns = lastTurn - Clock.getRoundNum();

      if (targetMoves) { // Re-acquire the target's location
        RobotInfo[] inRange = rc.senseNearbyRobots(damageRange[turns], enemyTeam);
        if (inRange.length > 0) { // No units to target
          target = inRange[0].location;
          targetMoves = inRange[0].type.canMove();
          rc.setIndicatorString(0, "Missile targetting " + inRange[0].type + "@" + target);
        } else {
          targetMoves = false; // Pick a tower or the HQ
          MapLocation[] enemyTowers = rc.senseEnemyTowerLocations();
          for (MapLocation t : enemyTowers) {
            if (myLoc.distanceSquaredTo(t) <= damageRange[turns]) {
              target = t;
              rc.setIndicatorString(0, "Missile targetting Tower @" + target);
              break;
            }
          }
          if (target == null) {
            target = rc.senseEnemyHQLocation();
            rc.setIndicatorString(0, "Missile targetting HQ @" + target);
          }
        }
      }

      try {
        if (target != null) {
          if (myLoc.distanceSquaredTo(target) <= GameConstants.MISSILE_RADIUS_SQUARED) rc.explode();
          else {
            Direction d = myLoc.directionTo(target);
            if (rc.canMove(d)) rc.move(d);
            else if (rc.canMove(d.rotateLeft())) rc.move(d.rotateLeft());
            else if (rc.canMove(d.rotateRight())) rc.move(d.rotateRight());
          }
        }
      } catch (GameActionException e) {
        System.out.println("Missile exception");
        // e.printStackTrace();
      }
      rc.yield();
    }
  }
Ejemplo n.º 9
0
  private boolean moveToPath() throws GameActionException {
    if (dstar == null) {
      dstar = new DStar(outPath, distances, currentLocation);
    }

    if (!dstar.arrived(currentLocation)) {
      dstar.compute(7000);
    }

    if (!RC.isActive()) return true;

    Direction dir = Direction.NORTH, best = null;
    int min = Integer.MAX_VALUE;
    for (int i = 0; i < 8; i++) {

      int d = RC.canMove(dir) ? dstar.getDistance(currentLocation.add(dir)) : Integer.MAX_VALUE;
      if (d < min) {
        min = d;
        best = dir;
      }
      dir = dir.rotateRight();
    }

    if (best != null && move(best)) {
      RC.setIndicatorString(1, "Moving to outPath");
      return true;
    } else {
      return false;
    }
  }
Ejemplo n.º 10
0
  /**
   * Attempts to build a robot of the given type in a random direction (attempts all directions).
   * Only attempts building if rc has no core delay and has more parts than the threshold.
   *
   * @param rc RobotController from which to build
   * @param buildType type of robot to build
   * @return true if buildType is built else false
   * @throws GameActionException
   */
  private static boolean tryToBuild(RobotController rc, RobotType buildType)
      throws GameActionException {
    if (rc.isCoreReady()) {
      Direction buildDir = getRandomDirection();
      for (int i = 0; i < 8; i++) {
        if (rc.canBuild(buildDir, buildType)) {
          rc.build(buildDir, buildType);
          rc.broadcastMessageSignal(SENDING_MODE, currentMode, ONE_SQUARE_RADIUS);

          if (!turtleCorner.equals(LOCATION_NONE)) {
            rc.broadcastMessageSignal(SENDING_TURTLE_X, turtleCorner.x, ONE_SQUARE_RADIUS);
            rc.broadcastMessageSignal(SENDING_TURTLE_Y, turtleCorner.y, ONE_SQUARE_RADIUS);
          }

          if (buildType.equals(RobotType.SCOUT)) {
            for (MapLocation scoutKitingLoc : SCOUT_KITING_LOCATIONS) {
              rc.broadcastMessageSignal(
                  SENDING_SCOUT_KITING_LOCATION_X, scoutKitingLoc.x, ONE_SQUARE_RADIUS);
              rc.broadcastMessageSignal(
                  SENDING_SCOUT_KITING_LOCATION_Y, scoutKitingLoc.y, ONE_SQUARE_RADIUS);
            }
          }
          return true;
        }
        buildDir = buildDir.rotateRight(); // try all directions clockwise
      }
    }
    return false;
  }
Ejemplo n.º 11
0
  private void cautiouslyApproachVisibleEnemySoldier(MapLocation enemySoldier, int maxEnemyExposure)
      throws GameActionException {
    int[] numEnemiesAttackingDirs = countNumEnemiesAttackingMoveDirs();

    Direction toEnemy = here.directionTo(enemySoldier);
    Direction[] tryDirs = new Direction[] {toEnemy, toEnemy.rotateLeft(), toEnemy.rotateRight()};
    for (int i = 0; i < tryDirs.length; i++) {
      Direction tryDir = tryDirs[i];
      if (!rc.canMove(tryDir)) continue;
      if (numEnemiesAttackingDirs[tryDir.ordinal()] > maxEnemyExposure) continue;
      if (Util.inHQAttackRange(here.add(tryDir), theirHQ)) continue;
      Debug.indicate(
          "micro",
          1,
          String.format(
              "cautiously approaching enemy soldier; direction %d; attackers = %d %d %d %d %d %d %d %d",
              tryDir.ordinal(),
              numEnemiesAttackingDirs[0],
              numEnemiesAttackingDirs[1],
              numEnemiesAttackingDirs[2],
              numEnemiesAttackingDirs[3],
              numEnemiesAttackingDirs[4],
              numEnemiesAttackingDirs[5],
              numEnemiesAttackingDirs[6],
              numEnemiesAttackingDirs[7]));
      rc.move(tryDir);
      return;
    }
    Debug.indicate("micro", 1, "can't safely approach enemy soldier");
  }
Ejemplo n.º 12
0
 private static void doMinerMove() {
   // If there is an available adjacent tile with twice as much ore we are better off moving
   Direction startDir = directions[rand.nextInt(directions.length)];
   Direction d = startDir;
   Direction best = Direction.NONE;
   double mostOre = rc.senseOre(myLoc) * 2; // Only move if there is twice as much ore
   boolean done = false;
   while (!done) {
     if (rc.canMove(d)) {
       MapLocation adj = rc.getLocation().add(d);
       double ore = rc.senseOre(adj);
       if ((ore > mostOre
               || (ore == mostOre
                   && ore > 0
                   && adj.distanceSquaredTo(myHQ) > myLoc.distanceSquaredTo(myHQ)))
           && !threats.isThreatened(adj)) {
         mostOre = rc.senseOre(adj);
         best = d;
       }
     }
     d = d.rotateRight();
     done = (d == startDir);
   }
   if (best != Direction.NONE) {
     rc.setIndicatorString(2, "Mining - better ore " + d);
     try {
       rc.move(best);
     } catch (GameActionException e) {
       System.out.println("Miner move exception");
       // e.printStackTrace();
     }
   }
 }
Ejemplo n.º 13
0
  private boolean spawn() throws GameActionException {

    Direction dir = rc.getLocation().directionTo(rc.senseEnemyHQLocation());

    // Need to do this first so the termination check works
    if (rc.canMove(dir)) {
      rc.spawn(dir);
      return true;
    }
    // Spawn as close to the desired direction as possible
    Direction dirLeft = dir;
    Direction dirRight = dir;

    do {
      dirLeft = dirLeft.rotateLeft();
      if (rc.canMove(dirLeft)) {
        rc.spawn(dirLeft);
        return true;
      }

      dirRight = dirRight.rotateRight();
      if (rc.canMove(dirRight)) {
        rc.spawn(dirRight);
        return true;
      }

    } while (dirRight != dirLeft);

    return false;
  }
Ejemplo n.º 14
0
  private boolean isOpen(
      MapLocation currentLoc,
      Direction currentDir,
      Direction nextDir,
      Direction desDir,
      boolean cw) {
    if (desDir == Direction.OMNI) {
      return true;
    }

    if (!isTraversable(currentLoc.add(currentDir))) return false;

    if (cw) {
      while (currentDir != nextDir) {
        if (currentDir == desDir) return true;
        currentDir = currentDir.rotateRight();
      }
    } else {
      while (currentDir != nextDir) {
        if (currentDir == desDir) return true;
        currentDir = currentDir.rotateLeft();
      }
    }
    return (nextDir == desDir);
  }
Ejemplo n.º 15
0
  /*
   * Head towards the nearest tile that we haven't sensed before
   * If there is a tie, pick the one nearest to the hq
   */
  private static void doPatrol() {
    if (!droneCentred) {
      // start point for the spiral is 2/5 of the way from our HQ to their HQ
      MapLocation centre =
          new MapLocation(
              (3 * myHQ.x + 2 * threats.enemyHQ.x) / 5, (3 * myHQ.y + 2 * threats.enemyHQ.y) / 5);
      if (threats.isThreatened(centre) || myLoc.distanceSquaredTo(centre) <= 2) droneCentred = true;
      else {
        moveDir = myLoc.directionTo(centre);
      }
    }

    if (droneCentred && --droneMoveCurrent <= 0) {
      if (patrolClockwise) moveDir = moveDir.rotateRight();
      else moveDir = moveDir.rotateLeft();
      if (!moveDir.isDiagonal()) droneMoveMax++;
      droneMoveCurrent = droneMoveMax;
    }

    while (true) {
      try {
        if (rc.canMove(moveDir) && !threats.isThreatened(myLoc.add(moveDir))) {
          rc.move(moveDir);
          break;
        } else if (rc.canMove(moveDir.rotateLeft())
            && !threats.isThreatened(myLoc.add(moveDir.rotateLeft()))) {
          rc.move(moveDir.rotateLeft());
          break;
        } else if (rc.canMove(moveDir.rotateRight())
            && !threats.isThreatened(myLoc.add(moveDir.rotateRight()))) {
          rc.move(moveDir.rotateRight());
          break;
        } else if (droneCentred) {
          moveDir = moveDir.opposite();
          patrolClockwise = !patrolClockwise;
          if (!moveDir.isDiagonal()) droneMoveMax++;
          droneMoveCurrent = droneMoveMax;
        } else {
          break;
        }
      } catch (GameActionException e) {
        System.out.println("Drone patrol exception");
        // e.printStackTrace();
      }
    }
  }
Ejemplo n.º 16
0
  public Direction Bug(MapLocation s, MapLocation t, int tolerance) {

    // arrive the destination
    if (s.distanceSquaredTo(t) <= tolerance) {
      reset();
      return Direction.OMNI;
    }

    Direction nextDir;
    Direction faceDir = controllers.myRC.getDirection();

    // if target is not traversable, back-tracing the destination
    while (!isTraversable(t)) {
      nextDir = s.directionTo(t);
      t = t.subtract(nextDir);

      // beside the source
      if (s.distanceSquaredTo(t) <= tolerance) {
        reset();
        return Direction.OMNI;
      }
    }

    modifiedDes = t;

    Direction desDir = s.directionTo(t);
    MapLocation nextLoc;

    if (isTracing) {

      Direction startTracingDir =
          isCW ? faceDir.rotateRight().rotateRight() : faceDir.rotateLeft().rotateLeft();
      nextLoc = traceNext(s, startTracingDir, isCW);
      nextDir = s.directionTo(nextLoc);

      // The way is open
      if (isOpen(s, faceDir, nextDir, desDir, isCW) && isTraversable(s.add(desDir))) {
        isTracing = false;
        return desDir;
      }

      return nextDir;

    } else {
      if (isTraversable(s.add(desDir))) {
        return desDir;
      } else {
        isTracing = true;
        isCW = betterTracingWay(s, t);
        nextLoc = traceNext(s, desDir, isCW);

        return s.directionTo(nextLoc);
      }
    }
  }
Ejemplo n.º 17
0
  protected boolean trySafeMove(
      Direction direction, RobotInfo[] nearbyEnemies, RobotInfo[] nearbyZombies)
      throws GameActionException {
    MapLocation currentLocation = rc.getLocation();
    MapLocation next = currentLocation.add(direction);
    if (canMoveSafely(direction, next, nearbyEnemies, nearbyZombies)) {
      rc.move(direction);
      return true;
    }

    Direction left = direction.rotateLeft();
    next = currentLocation.add(left);
    if (canMoveSafely(left, next, nearbyEnemies, nearbyZombies)) {
      rc.move(left);
      return true;
    }

    Direction right = direction.rotateRight();
    next = currentLocation.add(right);
    if (canMoveSafely(right, next, nearbyEnemies, nearbyZombies)) {
      rc.move(right);
      return true;
    }

    for (int i = 0; i < 2; i++) {
      left = left.rotateLeft();
      next = currentLocation.add(left);
      if (canMoveSafely(left, next, nearbyEnemies, nearbyZombies)) {
        rc.move(left);
        return true;
      }

      right = right.rotateRight();
      next = currentLocation.add(right);
      if (canMoveSafely(right, next, nearbyEnemies, nearbyZombies)) {
        rc.move(right);
        return true;
      }
    }

    return false;
  }
Ejemplo n.º 18
0
  private Direction detour(Direction faceDir, boolean cw) {

    if (cw) {
      while (controllers.motor.canMove(faceDir)) {
        faceDir = faceDir.rotateRight();
      }
      while (!controllers.motor.canMove(faceDir)) {
        faceDir = faceDir.rotateLeft();
      }
    } else {
      while (controllers.motor.canMove(faceDir)) {
        faceDir = faceDir.rotateLeft();
      }
      while (!controllers.motor.canMove(faceDir)) {
        faceDir = faceDir.rotateRight();
      }
    }

    return faceDir;
  }
Ejemplo n.º 19
0
  /**
   * Try to move.
   *
   * @param sneak:
   * @return
   */
  public void execute(int sneak) {
    // int bc = Clock.getBytecodesLeft();
    // RC.setIndicatorString(1, "my x = " + Integer.toString(RC.getLocation().x) + ", my y = " +
    // Integer.toString(RC.getLocation().y)
    // + "x = " + Integer.toString(dest.x) + ", y = " + Integer.toString(dest.y));
    // RC.setIndicatorString(2, Clock.getRoundNum() + " | dest = " + dest + ", navtype = " +
    // navType);

    if (arrived()) return;

    if (RC.isActive()) {
      Direction d;
      d = navAlg.getNextDir();
      if (d != null && d != Direction.NONE && d != Direction.OMNI) {
        if (RC.canMove(d)) {
          try {
            switch (sneak) {
              case SNEAK:
                // RC.setIndicatorString(2, dest.x + ", " + dest.y + ": sneak");
                RC.sneak(d);
                break;
              case RUN:
                // RC.setIndicatorString(2, dest.x + ", " + dest.y + ": run");
                RC.move(d);
                break;
              case PUSH_HOME:
                // RC.setIndicatorString(2, dest.x + ", " + dest.y + ": push_home");
                Direction awayFromHome = currentLocation.directionTo(ALLY_HQ).opposite();
                if (d == awayFromHome
                    || d == awayFromHome.rotateLeft()
                    || d == awayFromHome.rotateRight()) {
                  RC.sneak(d);
                } else {
                  RC.move(d);
                }
                break;
              default:
                break;
            }
          } catch (GameActionException e) {
            e.printStackTrace();
          }
        } else if (currentLocation.distanceSquaredTo(dest) <= 2) {
          setTarget(currentLocation);
        }
      }
    }
    // System.out.println("Bytecodes used by Mover.execute() = " +
    // Integer.toString(bc-Clock.getBytecodesLeft()));
  }
Ejemplo n.º 20
0
 /////////////////////////////////////////////////////////////
 // moveAway()
 //
 private static boolean moveAway(MapLocation center) throws GameActionException {
   while (!rc.isCoreReady()) {
     Clock.yield();
   }
   Direction dir = center.directionTo(rc.getLocation());
   int c = 0;
   while (!rc.canMove(dir)) {
     dir = dir.rotateRight();
     c++;
     if (c >= 4) return false;
   }
   rc.move(dir);
   return true;
 }
Ejemplo n.º 21
0
  /**
   * Checks if rc can move in direction dir (runs isCoreReady and canMove). If so, moves. If not,
   * moves rc in any of the four directions nearest dir, if possible.
   *
   * @param rc
   * @param dir
   * @return true if rc moves else false
   * @throws GameActionException
   */
  private static void moveTowards(RobotController rc, Direction dir) throws GameActionException {
    if (rc.isCoreReady() && !dir.equals(Direction.OMNI) && !dir.equals(Direction.NONE)) {
      Direction[] moveRight = {
        dir,
        dir.rotateRight(),
        dir.rotateLeft(),
        dir.rotateRight().rotateRight(),
        dir.rotateLeft().rotateLeft()
      };
      Direction[] moveLeft = {
        dir,
        dir.rotateLeft(),
        dir.rotateRight(),
        dir.rotateLeft().rotateLeft(),
        dir.rotateRight().rotateRight()
      };
      Direction[] nearDirections =
          Math.random() >= .5
              ? moveRight
              : moveLeft; // 50% chance robot tries to move to right first

      for (Direction nearDir : nearDirections) {
        if (rc.canMove(nearDir) && rc.isCoreReady()) {
          rc.move(nearDir);
        }
      }
      if (rc.getType() != RobotType.TTM
          && rc.getType() != RobotType.TTM) // these types can't clear rubble
      {
        if (rc.isCoreReady()
            && rc.onTheMap(rc.getLocation().add(dir))
            && rc.senseRubble(rc.getLocation().add(dir)) > RUBBLE_LOWER_CLEAR_THRESHOLD) {
          clearRubble(rc);
        }
      }
    }
  }
Ejemplo n.º 22
0
 private static boolean isBlockingSomeone(RobotController rc, MapLocation target)
     throws GameActionException {
   Direction dir = myLoc.directionTo(target);
   MapLocation behind = myLoc.add(dir.opposite());
   MapLocation left = behind.add(dir.rotateLeft());
   MapLocation right = behind.add(dir.rotateRight());
   // There is someone behind us
   if (rc.senseRobotAtLocation(behind) != null) {
     // If there is stuff blocking on both sides, then blocking
     if (!rc.canMove(myLoc.directionTo(left)) && !rc.canMove(myLoc.directionTo(right))) {
       return true;
     }
   }
   return false;
 }
Ejemplo n.º 23
0
 /**
  * @param desiredDir
  * @param otherDir
  * @return Integer.MAX_VALUE if otherDir can't be rotated to desiredDir or... TODO
  */
 private static double distanceFromDirection(Direction desiredDir, Direction otherDir) {
   double[] weights = {0, 3, 8, 15, 25};
   //		double[] weights = {0.75, 0.8, .85, .9, .95};
   //		double[] weights = {1, 1.0001, 1.0002, 1.0003, 1.0004};
   Direction leftRotation = otherDir;
   Direction rightRotation = otherDir;
   for (int i = 0; i <= 4; i++) {
     if (desiredDir.equals(leftRotation) || desiredDir.equals(rightRotation)) {
       return weights[i];
     }
     leftRotation = leftRotation.rotateLeft();
     rightRotation = rightRotation.rotateRight();
   }
   return Integer.MAX_VALUE;
 }
Ejemplo n.º 24
0
  protected void tryMove(Direction direction) throws GameActionException {
    if (rc.canMove(direction)) {
      rc.move(direction);
      return;
    }

    Direction left = direction.rotateLeft();
    if (rc.canMove(left)) {
      rc.move(left);
      return;
    }

    Direction right = direction.rotateRight();
    if (rc.canMove(right)) {
      rc.move(right);
      return;
    }

    for (int i = 0; i < 2; i++) {
      left = left.rotateLeft();
      if (rc.canMove(left)) {
        rc.move(left);
        return;
      }

      right = right.rotateRight();
      if (rc.canMove(right)) {
        rc.move(right);
        return;
      }
    }

    if (rc.getType() != RobotType.TTM) {
      tryClearRubble(direction);
    }
  }
Ejemplo n.º 25
0
  protected void goTo(Direction direction) throws GameActionException {
    Direction destinationDirection = direction;
    Direction currentDirection = destinationDirection;

    while (!myRC.canMove(currentDirection)
        && !currentDirection.equals(destinationDirection.rotateLeft())) {
      currentDirection = currentDirection.rotateRight();
    }

    if (myRC.canMove(currentDirection)) {
      if (!myRC.getDirection().equals(currentDirection)) {
        myRC.setDirection(currentDirection);
      } else {
        myRC.moveForward();
      }
    }
  }
Ejemplo n.º 26
0
 /**
  * @param rc
  * @param dirToCorner
  * @return the location of the corner in the given direction or LOCATION_NONE if it is not a
  *     corner
  * @throws GameActionException
  */
 private static MapLocation checkForCorner(RobotController rc, Direction dirToCorner)
     throws GameActionException {
   int senseRadiusMinusOneSquared =
       (int) Math.pow(Math.sqrt(rc.getType().sensorRadiusSquared) - 1, 2);
   // so that when you add one below to check for a corner, you can still sense the +1 location
   MapLocation[] nearby =
       MapLocation.getAllMapLocationsWithinRadiusSq(rc.getLocation(), senseRadiusMinusOneSquared);
   boolean isCorner = true;
   MapLocation corner = getFurthestInDirection(rc, nearby, dirToCorner);
   Direction[] nearDirections = {dirToCorner, dirToCorner.rotateLeft(), dirToCorner.rotateRight()};
   for (Direction dir : nearDirections) {
     if (rc.onTheMap(corner.add(dir))) {
       isCorner = false;
     }
   }
   return isCorner ? corner : LOCATION_NONE;
 }
Ejemplo n.º 27
0
  private static void herdTowardPastrSmart() throws GameActionException {
    if (smartLoc == null || smartLoc.distanceSquaredTo(pastr) <= GameConstants.PASTR_RANGE) {
      smartLoc =
          pastr.add(
              nextStartDir,
              nextStartDir.isDiagonal() ? maxDiagonalRadius - 3 : maxOrthogonalRadius - 3);
      nextStartDir = nextStartDir.rotateRight();
      edgeTrimIndex = edgesX.length - 1;
      // Debug.indicate("herd", 0, "starting new spoke");
    }

    while (edgeTrimIndex >= 0) {
      MapLocation edge =
          new MapLocation(pastr.x + edgesX[edgeTrimIndex], pastr.y + edgesY[edgeTrimIndex]);
      edgeTrimIndex--;
      if (isOnMap(edge) && rc.canSenseSquare(edge) && rc.senseCowsAtLocation(edge) > 1000) {
        MapLocation targetSquare = edge.add(pastr.directionTo(edge));
        if (rc.canAttackSquare(targetSquare)) {
          rc.attackSquareLight(targetSquare);
          return;
        }
      }
    }

    while (!rc.canAttackSquare(smartLoc) || !isOnMap(smartLoc)) {
      Direction moveDir = smartLoc.directionTo(pastr);
      Direction computedMoveDir = HerdPattern.readHerdDir(smartLoc, rc);
      if (computedMoveDir != null) moveDir = computedMoveDir;
      smartLoc = smartLoc.add(moveDir);
      if (here.distanceSquaredTo(smartLoc) > RobotType.NOISETOWER.attackRadiusMaxSquared) {
        smartLoc = null;
        return;
      }
      if (smartLoc.equals(pastr))
        return; // otherwise in some situations we could get an infinite loop
    }

    Direction herdDir = smartLoc.directionTo(pastr);
    Direction computedHerdDir = HerdPattern.readHerdDir(smartLoc, rc);
    if (computedHerdDir != null) herdDir = computedHerdDir;

    MapLocation targetSquare = smartLoc.add(Util.opposite(herdDir), 3);
    // Debug.indicate("herd", 2, "want to attack " + targetSquare.toString());
    if (rc.canAttackSquare(targetSquare)) rc.attackSquare(targetSquare);
    smartLoc = smartLoc.add(herdDir);
  }
Ejemplo n.º 28
0
 /**
  * If robotType is null, compute where to move. Else compute where to build.
  *
  * @param rc
  * @param dir
  * @param robotType
  * @return Direction to move/build in
  */
 static Direction findPathDirection(RobotController rc, Direction dir, RobotType robotType) {
   final int maxRotations = 8;
   int diff = rand.nextInt(2);
   for (int i = 0; i <= maxRotations; ++i) {
     if (i == maxRotations) return Direction.NONE;
     if ((i + diff) % 2 == 0) {
       for (int j = 0; j < i; ++j) dir = dir.rotateLeft();
     } else {
       for (int j = 0; j < i; ++j) dir = dir.rotateRight();
     }
     if (robotType == null) {
       if (rc.canMove(dir)) break;
     } else {
       if (rc.canBuild(dir, robotType)) break;
     }
   }
   return dir;
 }
Ejemplo n.º 29
0
 private static MapLocation getFurthestInDirection(
     RobotController rc, MapLocation[] locs, Direction dir) throws GameActionException {
   final MapLocation myLocation = rc.getLocation();
   MapLocation furthest = myLocation.add(dir);
   int furthestDist = myLocation.distanceSquaredTo(furthest);
   List<Direction> directionsTowards = Arrays.asList(dir, dir.rotateLeft(), dir.rotateRight());
   for (MapLocation loc : locs) {
     if (directionsTowards.contains(myLocation.directionTo(loc))) {
       if (rc.onTheMap(loc)) {
         int dist = myLocation.distanceSquaredTo(loc);
         if (dist > furthestDist) {
           furthestDist = dist;
           furthest = loc;
         }
       }
     }
   }
   return furthest;
 }
Ejemplo n.º 30
0
  public Direction getNextDir(int tolerance) {

    if (controllers.myRC.getLocation().equals(previousRobLoc)) {
      if (controllers.motor.canMove(previousDir)) {
        //				controllers.myRC.setIndicatorString(1, "precomputed");
        return previousDir;
      }
      //			else if (!controllers.motor.isActive() &&
      // controllers.motor.canMove(previousDir.rotateRight())) {
      //				isTracing = false;
      //				previousDir = previousDir.rotateRight();
      //				controllers.myRC.setIndicatorString(1, "yield");
      //				return previousDir;
      //			}
      else {
        //				controllers.myRC.setIndicatorString(1, "detour " + isCW);
        if (!isTracing) {
          previousDir = detour(controllers.myRC.getDirection(), isCW);
        } else {
          Direction faceDir = controllers.myRC.getDirection();
          Direction startTracingDir =
              isCW ? faceDir.rotateRight().rotateRight() : faceDir.rotateLeft().rotateLeft();
          previousDir = detour(startTracingDir, isCW);
        }
      }
      return previousDir;

    } else {
      //			controllers.myRC.setIndicatorString(1, "bugging");
      previousRobLoc = controllers.myRC.getLocation();

      if (destination == null) {
        reset();
        previousDir = Direction.OMNI;
        return Direction.OMNI;
      } else {
        previousDir = Bug(controllers.myRC.getLocation(), modifiedDes, tolerance);
        return previousDir;
      }
    }
  }