Ejemplo n.º 1
0
  private static void goToLocationSwarm(MapLocation location, boolean defuseMines)
      throws GameActionException {
    // Swarming
    double dxToLocation = location.x - rc.getLocation().x;
    double dyToLocation = location.y - rc.getLocation().y;
    double distanceToLocation = Math.sqrt(rc.getLocation().distanceSquaredTo(location));

    Robot[] nearbyAlliedRobots = rc.senseNearbyGameObjects(Robot.class, 14, rc.getTeam());
    int totalDx = 0;
    int totalDy = 0;
    int totalNearbyerDx = 0;
    int totalNearbyerDy = 0;
    for (int i = nearbyAlliedRobots.length; --i >= 0; ) {
      RobotInfo robotInfo = rc.senseRobotInfo(nearbyAlliedRobots[i]);
      if (robotInfo.type == RobotType.SOLDIER) {
        MapLocation iterLocation = robotInfo.location;
        totalDx += (iterLocation.x - rc.getLocation().x);
        totalDy += (iterLocation.y - rc.getLocation().y);
        if (rc.getLocation().distanceSquaredTo(iterLocation) <= 5) {
          // for repelling
          totalNearbyerDx += (iterLocation.x - rc.getLocation().x);
          totalNearbyerDy += (iterLocation.y - rc.getLocation().y);
        }
      }
    }

    double denom = Math.sqrt(totalDx * totalDx + totalDy * totalDy);
    double nearbyerDenom =
        Math.sqrt(totalNearbyerDx * totalNearbyerDx + totalNearbyerDy * totalNearbyerDy);
    double addX, addY;
    double addXNearbyer, addYNearbyer;

    if (Math.abs(totalDx) < 0.01) {
      addX = 0;
    } else {
      addX = swarmC * totalDx / denom;
    }
    if (Math.abs(totalDy) < 0.01) {
      addY = 0;
    } else {
      addY = swarmC * totalDy / denom;
    }

    if (Math.abs(totalNearbyerDx) < 0.01) {
      addXNearbyer = 0;
    } else {
      addXNearbyer = swarmD * totalNearbyerDx / nearbyerDenom;
    }
    if (Math.abs(totalNearbyerDy) < 0.01) {
      addYNearbyer = 0;
    } else {
      addYNearbyer = swarmD * totalNearbyerDy / nearbyerDenom;
    }
    double finalDx = dxToLocation / distanceToLocation + addX - addXNearbyer;
    double finalDy = dyToLocation / distanceToLocation + addY - addYNearbyer;

    //		rc.setIndicatorString(0, "totalDx: " + Integer.toString(totalDx) + ", finalDx: " +
    // Double.toString(finalDx));
    //		rc.setIndicatorString(1, "totalDy: " + Integer.toString(totalDy) + ", finalDy: " +
    // Double.toString(finalDy));

    int dirOffset;
    double ratioCutoff = 2.5;

    double ratio = Math.abs(finalDx / finalDy);

    if (ratio > ratioCutoff) {
      // go along x-axis
      if (finalDx > 0) {
        dirOffset = 2;
      } else {
        dirOffset = 6;
      }
    } else if (ratio < 1 / ratioCutoff) {
      // go along y-axis
      if (finalDy > 0) {
        dirOffset = 4;
      } else {
        dirOffset = 0;
      }
    } else {
      if (finalDx > 0) {
        if (finalDy >= 0) {
          dirOffset = 3;
        } else {
          dirOffset = 1;
        }
      } else {
        if (finalDy > 0) {
          dirOffset = 5;
        } else {
          dirOffset = 7;
        }
      }
    }

    Direction dirToMoveIn = Direction.values()[dirOffset];
    if (defuseMines) {
      NavSystem.goDirectionAndDefuse(dirToMoveIn);
    } else {
      NavSystem.goDirectionAvoidMines(dirToMoveIn);
    }
  }