1: package agent2d;

  3: import swarm.objectbase.SwarmImpl;
  4: import swarm.objectbase.Swarm;
  5: import swarm.defobj.Zone;
  6: import swarm.space.Grid2d;
  7: import swarm.gui.Raster;

  9: import Organization;

 11: import swarm.Globals;

 13: public class Agent2d extends SwarmImpl {
 14:   int size;
 15:   int scatter;

 17:   int x, y; 
 18:   private Grid2d world;
 19:   byte color;

 21:   public Agent2d (Zone aZone, Organization org,
 22:                   int x, int y,
 23:                   int scatter, int size) {
 24:     super (aZone);
 25:     this.x = x;
 26:     this.y = y;
 27:     this.world = org.getWorld ();
 28:     this.scatter = scatter;
 29:     this.size = size;
 30:     world.putObject$atX$Y (this, x, y);
 31:   }
 32:   
 33:   private Agent2d getAgent (int xpos, int ypos) {
 34:     if (xpos < 0)
 35:       return null;
 36:     else if (ypos < 0)
 37:       return null;
 38:     else if (xpos >= world.getSizeX ())
 39:       return null;
 40:     else if (ypos >= world.getSizeY ())
 41:       return null;
 42:   
 43:     return (Agent2d) world.getObjectAtX$Y (xpos, ypos);
 44:   }

 46:   public Agent2d getNeighbor (int width) {
 47:     int agentCount = 0;
 48:     int xi, yi;

 50:     for (yi = -width; yi <= width; yi++)
 51:       if (yi != 0)
 52:         for (xi = -width; xi <= width; xi++)
 53:           if (xi != 0)
 54:             if (getAgent (x + xi, y + yi) != null)
 55:               agentCount++;
 56:     if (agentCount > 0) {
 57:       int selected =
 58:         Globals.env.uniformIntRand.getIntegerWithMin$withMax (0,
 59:                                                               agentCount - 1);
 60:       Agent2d agent;
 61:       
 62:       agentCount = 0;
 63:       for (yi = -width; yi <= width; yi++)
 64:         if (yi != 0)
 65:           for (xi = -width; xi <= width; xi++)
 66:             if (xi != 0) {
 67:               agent = getAgent (x + xi, y + yi);
 68:               if (agent != null) {
 69:                 if (agentCount == selected)
 70:                   return agent;
 71:                 else
 72:                   agentCount++;
 73:               }
 74:             }
 75:     }
 76:     return null;
 77:   }
 78:     
 79:   public void moveAgent (int xo, int yo) {
 80:     int newx, newy;
 81:     newx = x;
 82:     newy = y;
 83:     newx += xo;
 84:     newy += yo;
 85:     if (newx < 0)
 86:       newx = 0;
 87:     else if (newx >= world.getSizeX ())
 88:       newx = world.getSizeX () - 1;
 89:     if (newy < 0)
 90:       newy = 0;
 91:     else if (newy >= world.getSizeY ())
 92:       newy = world.getSizeY () - 1;
 93:     if (world.getObjectAtX$Y (newx, newy) == null) {
 94:       world.putObject$atX$Y (null, x, y);
 95:       x = newx;
 96:       y = newy;
 97:       world.putObject$atX$Y (this, x, y);
 98:     }
 99:   }

101:   public void moveAdjacentToNeighbor (Agent2d neighbor) {
102:     if (neighbor.x > x)
103:       moveAgent (neighbor.x - x - 1, neighbor.y - y);
104:     else if (neighbor.y > y)
105:       moveAgent (neighbor.x - x, neighbor.y - y - 1);
106:     else if (x > neighbor.x)
107:       moveAgent (neighbor.x - x + 1, neighbor.y - y);
108:     else if (y > neighbor.y)
109:       moveAgent (neighbor.x - x + 1, neighbor.y - y + 1);
110:   }

112:   public void randomWalk () {
113:     moveAgent (Globals.env.uniformIntRand.getIntegerWithMin$withMax (-scatter, scatter),
114:                Globals.env.uniformIntRand.getIntegerWithMin$withMax (-scatter, scatter));         
115:   }

117:   public Object drawSelfOn (Raster r) {
118:     r.drawPointX$Y$Color (x, y, color);
119:     return this;
120:   }

122:   public boolean frob (int direction) {
123:     return false;
124:   }
125: }