Ejemplo n.º 1
0
  /** Returns a (random) coordinate that is between two adjacent MapNodes */
  @Override
  public Coord getInitialLocation(boolean translate) {
    List<MapNode> nodes = map.getNodes();
    MapNode n, n2;
    Coord n2Location, nLocation, placement;
    double dx, dy;
    double rnd = rng.nextDouble();

    // choose a random node (from OK types if such are defined)
    do {
      n = nodes.get(rng.nextInt(nodes.size()));
    } while (okMapNodeTypes != null && !n.isType(okMapNodeTypes));

    // choose a random neighbor of the selected node
    n2 = n.getNeighbors().get(rng.nextInt(n.getNeighbors().size()));

    nLocation = n.getLocation();
    n2Location = n2.getLocation();

    placement = n.getLocation().clone();

    dx = rnd * (n2Location.getX() - nLocation.getX());
    dy = rnd * (n2Location.getY() - nLocation.getY());
    if (translate == false) {
      dx = 0;
      dy = 0;
    }
    placement.translate(dx, dy); // move coord from n towards n2

    this.lastMapNode = n;
    return placement;
  }
Ejemplo n.º 2
0
  @Override
  public Path getPath() {
    Path p = new Path(generateSpeed());
    MapNode curNode = lastMapNode;
    MapNode prevNode = lastMapNode;
    MapNode nextNode = null;
    List<MapNode> neighbors;
    Coord nextCoord;

    assert lastMapNode != null : "Tried to get a path before placement";

    // start paths from current node
    p.addWaypoint(curNode.getLocation());

    int pathLength = rng.nextInt(maxPathLength - minPathLength) + minPathLength;

    for (int i = 0; i < pathLength; i++) {
      neighbors = curNode.getNeighbors();
      Vector<MapNode> n2 = new Vector<MapNode>(neighbors);
      if (!this.backAllowed) {
        n2.remove(prevNode); // to prevent going back
      }

      if (okMapNodeTypes != null) { // remove neighbor nodes that aren't ok
        for (int j = 0; j < n2.size(); ) {
          if (!n2.get(j).isType(okMapNodeTypes)) {
            n2.remove(j);
          } else {
            j++;
          }
        }
      }

      if (n2.size() == 0) { // only option is to go back
        nextNode = prevNode;
      } else { // choose a random node from remaining neighbors
        nextNode = n2.get(rng.nextInt(n2.size()));
      }

      prevNode = curNode;

      nextCoord = nextNode.getLocation();
      curNode = nextNode;

      p.addWaypoint(nextCoord);
    }

    lastMapNode = curNode;

    return p;
  }
Ejemplo n.º 3
0
 public Coord getLastLocation() {
   if (lastMapNode != null) {
     return lastMapNode.getLocation();
   } else {
     return null;
   }
 }
Ejemplo n.º 4
0
 /**
  * Checks that all coordinates of map nodes are within the min&max limits of the movement model
  *
  * @param nodes The list of nodes to check
  * @throws SettingsError if some map node is out of bounds
  */
 private void checkCoordValidity(List<MapNode> nodes) {
   // Check that all map nodes are within world limits
   for (MapNode n : nodes) {
     double x = n.getLocation().getX();
     double y = n.getLocation().getY();
     if (x < 0 || x > getMaxX() || y < 0 || y > getMaxY()) {
       throw new SettingsError(
           "Map node "
               + n.getLocation()
               + " is out of world  bounds "
               + "(x: 0..."
               + getMaxX()
               + " y: 0..."
               + getMaxY()
               + ")");
     }
   }
 }
Ejemplo n.º 5
0
 public void setLocation(Coord lastWaypoint) {
   // TODO: This should be optimized
   MapNode nearest = null;
   double minDistance = Double.MAX_VALUE;
   Iterator<MapNode> iterator = getMap().getNodes().iterator();
   while (iterator.hasNext()) {
     MapNode temp = iterator.next();
     double distance = temp.getLocation().distance(lastWaypoint);
     if (distance < minDistance) {
       minDistance = distance;
       nearest = temp;
     }
   }
   lastMapNode = nearest;
 }
  @Override
  public Path getPath() {
    if (mode == WALKING_HOME_MODE) {
      // Try to find home
      SimMap map = super.getMap();
      if (map == null) {
        return null;
      }
      MapNode thisNode = map.getNodeByCoord(lastWaypoint);
      MapNode destinationNode = map.getNodeByCoord(homeLocation);
      List<MapNode> nodes = pathFinder.getShortestPath(thisNode, destinationNode);
      Path path = new Path(generateSpeed());
      for (MapNode node : nodes) {
        path.addWaypoint(node.getLocation());
      }
      lastWaypoint = homeLocation.clone();
      mode = AT_HOME_MODE;

      double newX = lastWaypoint.getX() + (rng.nextDouble() - 0.5) * distance;
      if (newX > getMaxX()) {
        newX = getMaxX();
      } else if (newX < 0) {
        newX = 0;
      }
      double newY = lastWaypoint.getY() + (rng.nextDouble() - 0.5) * distance;
      if (newY > getMaxY()) {
        newY = getMaxY();
      } else if (newY < 0) {
        newY = 0;
      }
      Coord c = new Coord(newX, newY);
      path.addWaypoint(c);
      return path;
    } else {
      Path path = new Path(1);
      path.addWaypoint(lastWaypoint.clone());
      mode = READY_MODE;
      return path;
    }
  }