Пример #1
0
  public DStar(LocSet sources, int[] distances, MapLocation dest) {
    this.sources = sources;
    this.dest = dest;

    for (int i = sources.size; --i >= 0; ) {
      MapLocation source = sources.get(i);
      int e = distances[i] + heuristic(source, dest);
      queue.insert(e, source);
      distance[source.x][source.y] = distances[i];
      // estimate[source.x][source.y] = e;
      // leave as null to cause exceptions if we accidentally try to use it?
      from[source.x][source.y] = Direction.NONE;
    }
  }
Пример #2
0
  public boolean compute(int bytecodes) {
    // cache variables
    int d, w, e, x, y;
    int[] weight;
    MapLocation next, nbr;
    Direction dir;
    final BucketQueue<MapLocation> queue = this.queue;
    final int[][] distance = this.distance;
    final Direction[][] from = this.from;

    // int iters = 0;
    // int bc = Clock.getBytecodeNum();

    while (queue.size > 0) {
      // iters++;
      if (Clock.getBytecodeNum() >= bytecodes - 600) {
        break;
      }

      // RC.setIndicatorString(0, Integer.toString(min));
      // ALERT: queue.min is valid only after a call to deleteMin()!
      next = queue.deleteMin();

      x = next.x;
      y = next.y;
      d = distance[x][y];

      // check if we have already visited this node
      if (!visited[x][y]) {
        visited[x][y] = true;
        /*
         * if (broadcast) {
         * try {
         * messagingSystem.writePathingDirection(next, from[x][y]);
         * } catch (GameActionException ex) {
         * ex.printStackTrace();
         * }
         * }
         */

        weight = WEIGHT[RC.senseTerrainTile(next).ordinal()];

        dir = from[x][y];
        int i;
        if (dir == Direction.NONE) {
          dir = Direction.NORTH;
          i = 8;
        } else if (dir.isDiagonal()) {
          dir = dir.rotateLeft().rotateLeft();
          i = 5;
        } else {
          dir = dir.rotateLeft();
          i = 3;
        }

        for (; --i >= 0; dir = dir.rotateRight()) {
          nbr = next.add(dir);
          if (RC.senseTerrainTile(nbr).isTraversableAtHeight(RobotLevel.ON_GROUND)) {
            w = d + weight[dir.ordinal()];
            e = w + heuristic(next, dest);

            x = nbr.x;
            y = nbr.y;

            if (from[x][y] == null) {
              queue.insert(e, nbr);
              // if (RC.getRobot().getID() == 118)
              // System.out.println("inserted " + nbr + ": " + w + " " + e);
              distance[x][y] = w;
              // estimate[x][y] = e;
              from[x][y] = dir;
            } else {
              if (w < distance[x][y]) {
                queue.insert(e, nbr);
                distance[x][y] = w;
                // estimate[x][y] = e;
                from[x][y] = dir;
                visited[x][y] = false;
              }
            }
          }
        }
      }
    }

    // bc = Clock.getBytecodeNum() - bc;
    // RC.setIndicatorString(2, "average DStar bytecodes: " + (iters > 0 ? bc / iters : bc));

    return arrived(dest);
  }