Example #1
0
  @Override
  public void receiveMessage(Message m) {
    if (m instanceof MapUpdate) {
      // int del = -1;
      for (int i = 0; i < teamPosition.size(); ++i) {
        Bag b = (Bag) teamPosition.get(i);
        if (((MapUpdate) m).sender.equals(((MapUpdate) b.get(0)).sender)) {
          // del = i;
          System.out.printf(
              "Newer map received from sender. Already collected %d positions.\n",
              ((Bag) teamPosition.get(i)).size() - 1);
          teamPosition.remove(i);
          break;
        }
      }
      // if (del != -1) {
      // }

      MapUpdate mu = (MapUpdate) m;
      // for position some more steps are needed -> wait for pings from sender while "traveling
      // around"
      Bag b = new Bag();
      b.add(mu);
      b.add(new Int2D(super.pos.x, super.pos.y));
      teamPosition.add(b);
    } else if (m instanceof Ping) {
      Ping p = (Ping) m;
      // Look for all teams that want to update the map
      for (int i = 0; i < teamPosition.size(); ++i) {
        Bag b = (Bag) teamPosition.get(i);
        if (p.sender.equals(((MapUpdate) b.get(0)).sender)) {
          boolean newPos = true;
          for (int j = 1; j < b.size(); ++j) {
            Int2D pos = (Int2D) b.get(j);
            if (pos.x == super.pos.x && pos.y == super.pos.y) {
              newPos = false;
            }
          }
          if (newPos) {
            if (b.size() < 1 + neededPositions - 1) {
              b.add(new Int2D(super.pos.x, super.pos.y));
              // System.out.printf("New position collected.\n");
            } else {
              // all needed positions collected
              MapUpdate msg = (MapUpdate) b.get(0);
              System.out.printf("Collected all needed positions!\n");
              MapUpdate answer = new MapUpdate(this, msg.sender, msg.number, null, null, true);
              sendMessage(answer);
              // System.out.printf("Sender position:
              // %d,%d.\n",msg.sender.getPos().x,msg.sender.getPos().y);
              updateBaseMap(
                  msg.map,
                  msg.agentpos,
                  posToLocalMapPos(msg.sender.getPos(), msg.sender.getLocalMapScale()));
            }
          }
        }
      }
    }
  }
  /** Used to find the global attributes that another inspector has set so I can share it. */
  ChartGenerator chartToUse(final String sName, Frame parent, final GUIState simulation) {
    Bag charts = new Bag();
    if (simulation.guiObjects != null)
      for (int i = 0; i < simulation.guiObjects.numObjs; i++)
        if (simulation.guiObjects.objs[i] instanceof ChartGenerator
            && validChartGenerator((ChartGenerator) (simulation.guiObjects.objs[i])))
          charts.add(simulation.guiObjects.objs[i]);
    if (charts.numObjs == 0) return createNewChart(simulation);

    // init the dialog panel
    JPanel p = new JPanel();
    p.setLayout(new BorderLayout());

    String[] chartNames = new String[charts.numObjs + 1];

    chartNames[0] = "[Create a New Chart]";
    for (int i = 0; i < charts.numObjs; i++)
      chartNames[i + 1] = ((ChartGenerator) (charts.objs[i])).getTitle();

    // add widgets
    JPanel panel2 = new JPanel();
    panel2.setLayout(new BorderLayout());
    panel2.setBorder(new javax.swing.border.TitledBorder("Plot on Chart..."));
    JComboBox encoding = new JComboBox(chartNames);
    panel2.add(encoding, BorderLayout.CENTER);
    p.add(panel2, BorderLayout.SOUTH);

    // ask
    if (JOptionPane.showConfirmDialog(
            parent, p, "Create a New Chart...", JOptionPane.OK_CANCEL_OPTION)
        != JOptionPane.OK_OPTION) return null;

    if (encoding.getSelectedIndex() == 0) return createNewChart(simulation);
    else return (ChartGenerator) (charts.objs[encoding.getSelectedIndex() - 1]);
  }
Example #3
0
  public void vision(SimState state, SparseGrid2D grid) {
    // System.out.println("Direction: " + direction);
    // Visual Processor

    Int2D cord = grid.getObjectLocation(this);
    assert (vP != null && cord != null);
    assert (direction != 0);
    // System.out.println(this + "was at location: " + cord);
    if (cord != null) {
      seen = vP.sight(cord.x, cord.y, state, direction);
      Bag locations = new Bag();

      // Testing Print Statements
      for (int s = 0; s < seen.size(); s++) {
        Object j = seen.get(s);
        // System.out.print(s + "saw " + seen.get(s));
        Int2D obLoc = grid.getObjectLocation(j);
        locations.add(obLoc);
        // System.out.println(" at location:" + obLoc);
        // if(j.equals(Prey.class))
        // System.out.println("****" + seen.get(s));
      } // end of for loop

      this.behaviorProb(locations, seen);

      // Move every timestep
      super.move(grid, state);
    }
  } // end of vision
Example #4
0
 public Caller(int teamNr, IntGrid2D baseMapGrid) {
   this.teamNr = teamNr;
   this.baseMapGrid = baseMapGrid;
   communicationRadius = 100;
   zeroPos = new Int2D(0, 0);
   callerNr = callers.size() + 1;
   callers.add(this);
 }
  private UpperclassHousingSelection() {

    dorms = new Bag();

    // actually mixed-year but for simplicity I'm keeping it
    // upperclassman only for now
    Dorm Arrington = new Dorm("Arrington", false, false, 74);

    Dorm Ball = new Dorm("Ball", true, false, 53);
    Dorm Custis = new Dorm("Custis", false, false, 21);
    Dorm EagleLanding = new Dorm("Eagle Landing", false, false, 312);
    Dorm Framar = new Dorm("Framar", false, false, 11);
    Dorm Madison = new Dorm("Madison", false, false, 21);
    Dorm Marshall = new Dorm("Marshall", false, false, 74);
    Dorm Mason = new Dorm("Mason", false, false, 93);
    Dorm Westmoreland = new Dorm("Westmoreland", false, false, 56);

    // Willard houses students in single rooms which is not accounted for yet
    Dorm Willard = new Dorm("Willard", false, false, 91);

    // 100 of the rooms in the aparments are singles which is not
    // accounted for yet
    Dorm Apartments = new Dorm("Apartments", false, false, 381);

    // for extraneous students who commute or live off campus
    Dorm Offcampus = new Dorm("Offcampus", false, false, 3500);

    dorms.add(Ball);
    dorms.add(Arrington);
    dorms.add(Custis);
    dorms.add(Framar);
    dorms.add(Madison);
    dorms.add(Mason);
    dorms.add(Westmoreland);
    dorms.add(Willard);
    dorms.add(Apartments);
    dorms.add(EagleLanding);
    dorms.add(Offcampus);
  }
Example #6
0
 protected void createGrids() {
   emptySpaces.clear();
   neighbors = new IntGrid2D(gridWidth, gridHeight, 0);
   int[][] g = neighbors.field;
   for (int x = 0; x < gridWidth; x++)
     for (int y = 0; y < gridHeight; y++) {
       double d = random.nextDouble();
       if (d < redProbability) g[x][y] = RED;
       else if (d < redProbability + blueProbability) g[x][y] = BLUE;
       else if (d < redProbability + blueProbability + emptyProbability) {
         g[x][y] = EMPTY;
         emptySpaces.add(new Int2D(x, y));
       } else g[x][y] = UNAVAILABLE;
     }
 }
  /**
   * For each <xPos,yPos,zPos> location, puts all such objects into the result bag. Returns the
   * result bag. If the provided result bag is null, one will be created and returned.
   */
  public Bag getObjectsAtLocations(
      final IntBag xPos, final IntBag yPos, final IntBag zPos, Bag result) {
    if (result == null) result = new Bag();
    else result.clear();

    final int len = xPos.numObjs;
    final int[] xs = xPos.objs;
    final int[] ys = yPos.objs;
    final int[] zs = zPos.objs;
    for (int i = 0; i < len; i++) {
      // a little efficiency: add if we're 1, addAll if we're > 1,
      // do nothing if we're 0
      Bag temp = getObjectsAtLocation(xs[i], ys[i], zs[i]);
      if (temp != null) {
        int n = temp.numObjs;
        if (n == 1) result.add(temp.objs[0]);
        else if (n > 1) result.addAll(temp);
      }
    }
    return result;
  }
Example #8
0
 private static void updateTargets(IntGrid2D baseMapGrid, int scale, Int2D zeroPos) {
   if (targets == null) {
     targets = new Bag();
   } else {
     targets.clear();
   }
   java.util.HashMap<Integer, java.util.HashSet<Integer>> starget =
       new java.util.HashMap<Integer, java.util.HashSet<Integer>>();
   for (int i = 0; i < baseMapGrid.getWidth(); ++i) {
     for (int j = 0; j < baseMapGrid.getHeight(); ++j) {
       int val = baseMapGrid.get(i, j);
       if (val == 0) {
         Int2D locPos = posToLocalMapPos(new Int2D(i, j), scale, zeroPos);
         if (!starget.containsKey(locPos.x)) {
           starget.put(locPos.x, new java.util.HashSet<Integer>());
         }
         boolean ok = starget.get(locPos.x).add(locPos.y);
         if (ok) {
           targets.add(locPos);
         }
       }
     }
   }
 }
  protected void hitOrDraw(Graphics2D graphics, DrawInfo2D info, Bag putInHere) {
    final Grid2D field = (Grid2D) (this.field);
    if (field == null) return;

    // first question: determine the range in which we need to draw.
    final int maxX = field.getWidth();
    final int maxY = field.getHeight();
    if (maxX == 0 || maxY == 0) return;

    final double divideByX =
        ((maxX % 2 == 0) ? (3.0 * maxX / 2.0 + 0.5) : (3.0 * maxX / 2.0 + 2.0));
    final double divideByY = (1.0 + 2.0 * maxY);

    final double xScale = info.draw.width / divideByX;
    final double yScale = info.draw.height / divideByY;
    int startx = (int) (((info.clip.x - info.draw.x) / xScale - 0.5) / 1.5) - 2;
    int starty = (int) ((info.clip.y - info.draw.y) / (yScale * 2.0)) - 2;
    int endx = /*startx +*/
        (int) (((info.clip.x - info.draw.x + info.clip.width) / xScale - 0.5) / 1.5)
            + 4; // with rounding, width be as much as 1 off
    int endy = /*starty +*/
        (int) ((info.clip.y - info.draw.y + info.clip.height) / (yScale * 2.0))
            + 4; // with rounding, height be as much as 1 off

    //
    //
    // CAUTION!
    //
    // At some point we should triple check the math for rounding such
    // that the margins are drawn properly
    //
    //

    // next we determine if this is a DoubleGrid2D or an IntGrid2D

    //        final Rectangle clip = (graphics==null ? null : graphics.getClipBounds());

    final boolean isDoubleGrid2D = (field instanceof DoubleGrid2D);
    final double[][] doubleField = (isDoubleGrid2D ? ((DoubleGrid2D) field).field : null);
    final int[][] intField = (isDoubleGrid2D ? null : ((IntGrid2D) field).field);

    double xyC_x, xyC_y, xyC_ulx, xyC_uly, xyC_upx, xyC_upy, xyC_urx, xyC_ury, x0, y0, tx, ty;

    if (startx < 0) startx = 0;
    if (starty < 0) starty = 0;
    if (endx > maxX) endx = maxX;
    if (endy > maxY) endy = maxY;

    for (int y = starty; y < endy; y++)
      for (int x = startx; x < endx; x++) {
        // getxyC( x, y, xScale, yScale, info.draw.x, info.draw.y, xyC );
        // getxyC( field.ulx(x,y), field.uly(x,y), xScale, yScale, info.draw.x, info.draw.y, xyC_ul
        // );
        // getxyC( field.upx(x,y), field.upy(x,y), xScale, yScale, info.draw.x, info.draw.y, xyC_up
        // );
        // getxyC( field.urx(x,y), field.ury(x,y), xScale, yScale, info.draw.x, info.draw.y, xyC_ur
        // );

        x0 = x;
        y0 = y;
        tx = info.draw.x;
        ty = info.draw.y;
        xyC_x = tx + xScale * (1.5 * x0 + 1);
        xyC_y = ty + yScale * (1.0 + 2.0 * y0 + (x0 < 0 ? (-x0) % 2 : x0 % 2));

        x0 = field.ulx(x, y);
        y0 = field.uly(x, y);
        tx = info.draw.x;
        ty = info.draw.y;
        xyC_ulx = tx + xScale * (1.5 * x0 + 1);
        xyC_uly = ty + yScale * (1.0 + 2.0 * y0 + (x0 < 0 ? (-x0) % 2 : x0 % 2));

        x0 = field.upx(x, y);
        y0 = field.upy(x, y);
        tx = info.draw.x;
        ty = info.draw.y;
        xyC_upx = tx + xScale * (1.5 * x0 + 1);
        xyC_upy = ty + yScale * (1.0 + 2.0 * y0 + (x0 < 0 ? (-x0) % 2 : x0 % 2));

        x0 = field.urx(x, y);
        y0 = field.ury(x, y);
        tx = info.draw.x;
        ty = info.draw.y;
        xyC_urx = tx + xScale * (1.5 * x0 + 1);
        xyC_ury = ty + yScale * (1.0 + 2.0 * y0 + (x0 < 0 ? (-x0) % 2 : x0 % 2));

        xPoints[0] = (int) (xyC_urx - 0.5 * xScale);
        yPoints[0] = (int) (xyC_ury + yScale);
        xPoints[1] = (int) (xyC_upx + 0.5 * xScale);
        yPoints[1] = (int) (xyC_upy + yScale);
        xPoints[2] = (int) (xyC_upx - 0.5 * xScale);
        yPoints[2] = (int) (xyC_upy + yScale);
        xPoints[3] = (int) (xyC_ulx + 0.5 * xScale);
        yPoints[3] = (int) (xyC_uly + yScale);
        xPoints[4] = (int) (xyC_x - 0.5 * xScale);
        yPoints[4] = (int) (xyC_y + yScale);
        xPoints[5] = (int) (xyC_x + 0.5 * xScale);
        yPoints[5] = (int) (xyC_y + yScale);

        if (graphics == null) {
          generalPath.reset();
          generalPath.moveTo(xPoints[0], yPoints[0]);
          for (int i = 1; i < 6; i++) generalPath.lineTo(xPoints[i], yPoints[i]);
          generalPath.closePath();
          Area area = new Area(generalPath);
          if (area.intersects(info.clip.x, info.clip.y, info.clip.width, info.clip.height)) {
            valueToPass.val = isDoubleGrid2D ? doubleField[x][y] : intField[x][y];
            putInHere.add(getWrapper(valueToPass.val, x, y));
          }
        } else {
          Color c = map.getColor(isDoubleGrid2D ? doubleField[x][y] : intField[x][y]);
          if (c.getAlpha() == 0) continue;
          graphics.setColor(c);

          // MacOS X 10.3 Panther has a bug which resets the clip, YUCK
          //                    graphics.setClip(clip);
          graphics.fillPolygon(xPoints, yPoints, 6);
        }
      }
  }
Example #10
0
 public void addTeamMember(AbstractAgent a) {
   team.add(a);
 }