public Direction moveToward(MapLocation location, Boolean allowGreaterDistance, int[] offsets)
      throws GameActionException {

    if (location == null) return null;

    MapLocation currentLocation = this.robot.locationController.currentLocation();
    double currentDistance = currentLocation.distanceSquaredTo(location);

    Direction direction = currentLocation.directionTo(location);
    int directionInteger = directionToInt(direction);

    if (this.lastPosition != null && currentLocation.distanceSquaredTo(this.lastPosition) <= 1)
      this.turnsStuck++;

    if (this.turnsStuck > 1) return this.moveAroundObstacleToward(location);

    if (moveTo(direction)) {

      this.lastPosition = currentLocation;
      return direction;

    } else {

      for (int offset : offsets) {

        Direction directionOne = MovementController.directionFromInt(directionInteger + offset);
        Direction directionTwo = MovementController.directionFromInt(directionInteger - offset);
        direction =
            this.moveTowardDirections(
                currentLocation,
                location,
                directionOne,
                directionTwo,
                allowGreaterDistance,
                currentDistance);
        if (direction != null) {

          this.lastPosition = currentLocation;
          return direction;
        }
      }
    }

    this.turnsStuck++;
    return null;
  }
  // moves away from a location using the moveTo method (and its implications)
  public Direction moveAway(MapLocation location) throws GameActionException {

    if (location == null) return null;

    MapLocation currentLocation = this.robot.locationController.currentLocation();
    Direction direction = currentLocation.directionTo(location);

    return this.moveToward(
        currentLocation.add(MovementController.directionWithOffset(direction, -4)));
  }
  public Direction moveAway(RobotInfo[] enemies) throws GameActionException {

    MapLocation currentLocation = this.robot.locationController.currentLocation();
    Direction opposite = null;

    for (RobotInfo enemy : enemies) {

      opposite =
          MovementController.directionWithOffset(currentLocation.directionTo(enemy.location), -4);
      break;
    }
    return this.moveToward(currentLocation.add(opposite, 40));
  }
 public static Direction directionWithOffset(Direction direction, int offset) {
   return MovementController.directionFromInt(
       MovementController.directionToInt(direction) + offset);
 }
  // follows a wall until it can move towards it's initial target, switching directions if it gets
  // stuck
  public Direction moveAroundObstacleToward(MapLocation location) throws GameActionException {

    if (this.traversalDirection == 0) { // start with a random traversal direction

      if (this.robot.random.nextBoolean()) this.traversalDirection = -1;
      else this.traversalDirection = 1;
    }

    MapLocation robotLocation = this.robot.locationController.currentLocation();
    Direction directionToTarget = robotLocation.directionTo(location);
    Direction directionToLastPosition =
        this.lastPosition != null ? robotLocation.directionTo(this.lastPosition) : null;
    int switchedDirection = 0;

    this.robot.robotController.setIndicatorString(
        2,
        "Traversal Direction "
            + this.traversalDirection
            + " Dir to target "
            + directionToTarget
            + " Dir to LP "
            + directionToLastPosition);

    // see if it can move toward it's target
    if (directionToTarget == directionToLastPosition || !this.moveTo(directionToTarget)) {

      if (this.lastLastPosition != null
          && robotLocation.distanceSquaredTo(this.lastLastPosition)
              <= 1) { // if just moved in a triangle

        this.traversalDirection = -this.traversalDirection;
      }

      this.robot.robotController.setIndicatorString(2, "I CAN'T move towards target");
      // traverse along obstacle, switching directions if it hits a dead end or outer wall
      for (int i = 1; i < 8 && switchedDirection <= 1; i++) {

        Direction direction =
            MovementController.directionWithOffset(directionToTarget, i * this.traversalDirection);
        MapLocation nextLocation = robotLocation.add(direction);
        this.robot.robotController.setIndicatorDot(nextLocation, 255, 255, 255);

        if (!direction.equals(directionToLastPosition)) {

          if (this.robot.movementController.moveTo(direction)) {

            this.lastLastPosition = this.lastPosition;
            this.lastPosition = robotLocation;

            return direction;

          } else {

            if (this.robot
                .robotController
                .senseTerrainTile(nextLocation)
                .equals(TerrainTile.OFF_MAP)) {

              this.traversalDirection =
                  -this
                      .traversalDirection; // Might need to do additional checks if this switches
                                           // too often
              i = 0;
              switchedDirection++;
            }
          }
        }
      }

    } else {

      this.robot.robotController.setIndicatorString(2, "I can move towards target");
      this.robot.robotController.setIndicatorLine(robotLocation, location, 255, 255, 255);
      this.turnsStuck = 0;
      return directionToTarget;
    }

    // System.out.println("Didn't find move location.");
    return null;
  }