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.actualLastPosition != null
        && currentLocation.distanceSquaredTo(this.actualLastPosition) <= 1) this.turnsStuck++;

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

    if (moveTo(direction)) {

      this.actualLastPosition = 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.actualLastPosition = 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();

    RobotInfo closestEnemy = null;
    double closestDistance = Double.MAX_VALUE;

    for (RobotInfo enemy : enemies) {

      double distance = currentLocation.distanceSquaredTo(enemy.location);
      if (distance < closestDistance) {

        closestEnemy = enemy;
        closestDistance = distance;
      }
    }

    Direction opposite =
        MovementController.directionWithOffset(
            currentLocation.directionTo(closestEnemy.location), -4);
    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();
    this.actualLastPosition = robotLocation;
    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
          && this.turnsStuckWhilePathfinding == 0) { // 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 loop around 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) || this.turnsStuckWhilePathfinding > 1) {

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

            this.lastLastPosition = this.lastPosition;
            this.lastPosition = robotLocation;
            if (this.turnsStuckWhilePathfinding > 0) this.turnsStuckWhilePathfinding -= 1;

            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++;
            }
          }
        }
      }

      // check if didn't move (dead end)
      if (robotLocation.equals(this.actualLastPosition)) {

        this.turnsStuckWhilePathfinding++;
      }

    } 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;
  }