/**
  * Returns true if a grid point of the map is free (with no car)
  *
  * @param p
  * @return
  */
 private boolean isFree(GridPoint p) {
   int count = 0;
   Iterable<TrafficElement> elements = map.getObjectsAt(p.getX(), p.getY());
   for (TrafficElement elem : elements) {
     count++;
   }
   return count == 0;
 }
Exemplo n.º 2
0
  public void moveTowards(GridPoint pt) {
    // only move if we are not already in this grid location
    if (!pt.equals(grid.getLocation(this))) {
      NdPoint myPoint = space.getLocation(this);
      NdPoint otherPoint = new NdPoint(pt.getX(), pt.getY());
      double angle = SpatialMath.calcAngleFor2DMovement(space, myPoint, otherPoint);
      space.moveByVector(this, 1, angle, 0);
      myPoint = space.getLocation(this);
      grid.moveTo(this, (int) myPoint.getX(), (int) myPoint.getY());

      moved = true;
    }
  }
  /** Executes step method for each car and adds them to their new position */
  private void moveCars() {
    this.carsToRemove.clear();
    this.positionsToCheck.clear();

    // Cars compute their new position
    for (Car car : travelingCars) car.move();

    // Move cars in the map
    for (Car car : travelingCars) {
      int x = car.getX();
      int y = car.getY();

      // Move car
      if (!this.isPositionOutOfBounds(x, y)) {
        map.moveTo(car, x, y);
        this.addPositionToCheck(car.getPosition().getGridPoint());
      }
      // Car moved out of the map. Add for removal
      else {
        this.addCarToRemove(car);
      }
    }

    // Remove cars out of bounds
    for (Car car : this.carsToRemove) {
      remove(car);
    }

    // Manage collisions
    for (String posId : this.positionsToCheck.keySet()) {
      GridPoint pos = this.positionsToCheck.get(posId);
      int x = pos.getX();
      int y = pos.getY();

      if (this.getNumElements(x, y) > 1) {
        Collision col = new Collision(x, y, map);
        Iterable<TrafficElement> elements = map.getObjectsAt(x, y);
        remove(elements);

        context.add(col);
        map.moveTo(col, x, y);

        this.collisions.add(col);
      }
    }
  }
Exemplo n.º 4
0
  public float[] getLocation(Object obj) {
    GridPoint gpoint = grid.getLocation(obj);
    int[] origin = grid.getDimensions().originToIntArray(null);
    float xOffset = (float) origin[0];
    float yOffset = (float) origin[1];

    if (gpoint == null) {
      point[0] = Float.POSITIVE_INFINITY;
      point[1] = Float.POSITIVE_INFINITY;
      return point;
    }

    float x = (float) (gpoint.getX() + xOffset) * cellSize;
    float y = (float) (gpoint.getY() + yOffset) * cellSize;
    point[0] = x;
    point[1] = y;
    return point;
  }
Exemplo n.º 5
0
  public void infect() {
    GridPoint pt = grid.getLocation(this);
    List<Object> humans = new ArrayList<Object>();
    for (Object obj : grid.getObjectsAt(pt.getX(), pt.getY())) {
      if (obj instanceof Human) {
        humans.add(obj);
      }
    }

    if (humans.size() > 0) {
      int index = RandomHelper.nextIntFromTo(0, humans.size() - 1);
      Object obj = humans.get(index);
      NdPoint spacePt = space.getLocation(obj);
      Context<Object> context = ContextUtils.getContext(obj);
      context.remove(obj);
      Zombie zombie = new Zombie(space, grid);
      context.add(zombie);
      space.moveTo(zombie, spacePt.getX(), spacePt.getY());
      grid.moveTo(zombie, pt.getX(), pt.getY());

      Network<Object> net = (Network<Object>) context.getProjection("infection network");
      net.addEdge(this, zombie);
    }
  }
Exemplo n.º 6
0
  /**
   * Rain clouds appear with a certain chance, influenced by the weather For every rain cloud in the
   * grid the velocity of every rain object is updated Rain clouds are removed if they have passed a
   * certain time
   */
  @ScheduledMethod(start = 1, interval = 1, priority = 0)
  public void rain() {
    // Let new raingroups appear with a certain chance
    double chance = SimulationParameters.rainProb;
    // The probability of rain appearing decreases if there is already rain in the grid
    if (noRainGroups == 1) chance = (chance / (noRainGroups)) * 0.5;
    if (noRainGroups == 2) chance = (chance / (noRainGroups)) * 0.1;
    if (noRainGroups > 2) chance = (chance / (noRainGroups)) * 0.01;
    double f = urng.nextDouble();
    if (f < chance) {
      // Let rain appear
      int x = rand.nextInt((SimulationParameters.gridSize - 0) + 1);
      int y = rand.nextInt((SimulationParameters.gridSize - 0) + 1);
      int[] newLoc = {x, y};
      // Let new raingroup appear in random location
      RainGroup rg = new RainGroup(ContextUtils.getContext(this), grid, newLoc);
      noRainGroups++;
      rainGroups.add(rg);
    }

    ArrayList<RainGroup> toRemove = new ArrayList<RainGroup>();
    for (RainGroup rg : rainGroups) {
      // Get velocity vector of the rain
      float x = Wind.getWindVelocity().x;
      float y = Wind.getWindVelocity().y;
      Vector2 velRain = new Vector2(x, y);
      velRain.setLength(
          Wind.getWindVelocity().len() * 0.9f); // Rain speed is a bit lower than that of the wind

      List<Rain> toRemove1 = new ArrayList<Rain>();
      // Let rain be carried by the wind
      if (urng.nextDouble() < velRain.len()) {
        for (Rain rain : rg.getRainObjects()) {
          Directions dir = Directions.fromVectorToDir(velRain);
          GridPoint pt = grid.getLocation(rain);
          int cX = pt.getX() + dir.xDiff;
          int cY = pt.getY() + dir.yDiff;

          // If new rain-location is out of borders, delete this rain object
          // In this way the cloud "travels" out of the grid
          if (cX < 0
              || cX >= SimulationParameters.gridSize
              || cY < 0
              || cY >= SimulationParameters.gridSize) {
            toRemove1.add(rain);
          } else grid.moveTo(rain, cX, cY);
        }
      }

      for (Rain r : toRemove1) {
        rg.removeRain(r);
        TreeBuilder.performance.decreaseRainCount();
      }
    }

    // Remove the raingroups from our list which were removed from the context
    for (RainGroup rg : toRemove) {
      rainGroups.remove(rg);
      noRainGroups--;
    }
  }
Exemplo n.º 7
0
  /** Initialisation of Capo agent */
  public void linkin() {

    System.out.println("Objects assigned to Capo #" + index + ", " + this.toString() + ": ");

    GridPoint pt = grid.getLocation(this);

    // finde Farbe des Territoriums auf dem der Capo sich befindet und setze
    // eigene Familienfarbe entsprechend
    for (Object obj : grid.getObjectsAt(pt.getX(), pt.getY())) {
      if (obj instanceof TerritoryMarker) {
        familyColor = ((TerritoryMarker) obj).getTerritoryColor();
        break;
      }
    }

    // Ordne alle Soldaten und Shops auf dem Territorium dem Capo zu
    Iterable allObjects = grid.getObjects();
    for (Object object : allObjects) {

      // finde Farbe des Territoriums auf dem sich das aktuelle Objekt
      // befindet
      Color c = Color.BLACK;
      GridPoint objLocation = grid.getLocation(object);
      for (Object obj : grid.getObjectsAt(objLocation.getX(), objLocation.getY())) {
        if (obj instanceof TerritoryMarker) {
          c = ((TerritoryMarker) obj).getTerritoryColor();
          break;
        }
      }

      // Wenn Object auf Territorium der Familie (entspr. gleiche Farbe),
      // dann...
      if (familyColor.equals(c)) {

        // System.out.print("   Soldiers: ");
        // Soldat zuordnen
        if (object instanceof Soldiers) {
          Soldiers soldier = (Soldiers) object;
          soldiers.add(soldier);
          soldier.setMycapo(this);
          System.out.print(soldier.toString() + " ");
        }

        // System.out.print("\n   Shops: ");
        // Shop zuordnen
        if (object instanceof Shop) {
          Shop shop = (Shop) object;
          shop.setCapo(this);
          shops.add(shop);
          System.out.print(shop.toString() + " ");
        }
      }
    }

    // for (int x=pt.getX(); x<pt.getX()+25; x++)
    // for(int y=pt.getY(); y<pt.getY()+25;y++){
    // Object obj=grid.getObjectsAt(x,y);
    // // System.out.print(obj.toString()+" ");
    // if (obj instanceof UnremoveableIterator){
    // UnremoveableIterator iter = (UnremoveableIterator)obj;
    // while (iter.hasNext()){
    // Object element =iter.next();
    // if (element instanceof Soldiers) {
    // Soldiers s = (Soldiers) element;
    // soldiers.add(s);
    // s.setMycapo(this);
    // }
    // System.out.print(element.toString()+" ");
    // }
    // //
    // // System.out.print(obj.toString()+" ");
    // }
    // }
    System.out.println();
    // for (Object obj:grid.getObjectsAt(pt.getX(), pt.getY())){
    // if (obj instanceof Soldiers){
    // soldiers.add(obj);
    // }
    // }
    // if (soldiers.size()>0){
    // int index = RandomHelper.nextIntFromTo(0, 5);
    // Object obj = soldiers.get(index);
    // ArrayList<Soldiers> soldiers1 ;
    // NdPoint spacePt = space.getLocation(obj);
    // Context<Object> context = ContextUtils.getContext(obj);
    // space.moveTo(soldiers, spacePt.getX(), spacePt.getY());
    // grid.moveTo(soldiers, pt.getX(), pt.getY());
    //
    //
    //
    // Network<Object> net
    // =(Network<Object>)context.getProjection("network");
    // net.addEdge(this, soldiers);
    //
    // }

  }
 /** @param pos */
 private void addPositionToCheck(GridPoint pos) {
   String s = pos.getX() + "-" + pos.getY();
   this.positionsToCheck.put(s, pos);
 }