Пример #1
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);
      }
    }
  }
Пример #2
0
  public MapLocation getNextJumpingLocTowardDes(int tolerance) {
    MapLocation currentLoc = controllers.myRC.getLocation();
    MapLocation jumpLoc = controllers.myRC.getLocation();
    MapLocation nextLoc = jumpLoc;
    Direction nextDir;

    do {
      jumpLoc = nextLoc;
      if (jumpLoc.distanceSquaredTo(destination) < tolerance && isTraversable(jumpLoc)) {
        //				controllers.myRC.setIndicatorString(2, "here: " + jumpLoc + ", des" + destination);
        return jumpLoc;
      }
      nextDir = jumpLoc.directionTo(destination);
      nextLoc = nextLoc.add(nextDir);

    } while (currentLoc.distanceSquaredTo(nextLoc) <= 16);

    //		controllers.myRC.setIndicatorString(2, "optimal jump to: " + jumpLoc);

    // Find alternative jumping location
    if (!isTraversable(jumpLoc)) {
      while (!jumpLoc.isAdjacentTo(currentLoc)) {
        MapLocation temp = jumpLoc;
        MapLocation best = null;
        int distance = currentLoc.distanceSquaredTo(destination);
        for (int i = 0; i < 8; i++) {
          temp = jumpLoc.add(Util.dirs[i]);
          if (currentLoc.distanceSquaredTo(temp) <= 16
              && isTraversable(temp)
              && temp.distanceSquaredTo(destination) < distance) {
            best = temp;
            distance = temp.distanceSquaredTo(destination);
          }
        }

        if (best != null) {
          return best;
        } else {
          jumpLoc = jumpLoc.subtract(currentLoc.directionTo(jumpLoc));
        }
      }
    }
    if (jumpLoc.isAdjacentTo(currentLoc) || jumpLoc.equals(currentLoc)) return null;

    return jumpLoc;
  }