1: package agent2d;
  2: import swarm.defobj.Zone;
  3: import swarm.gui.Raster;

  5: import swarm.random.NormalDist;
  6: import swarm.random.NormalDistImpl;
  7: import swarm.random.BernoulliDist;
  8: import swarm.random.BernoulliDistImpl;

 10: import swarm.Globals;

 12: import Organization;

 14: abstract class DirectedAgent2d extends Agent2d {
 15:   int xoffset, yoffset;
 16:   NormalDist resistProbabilityDistribution;
 17:   NormalDist energyDistribution;
 18:   double resistProbability = 0.0;
 19:   private BernoulliDist bernoulliDist =
 20:     new BernoulliDistImpl (getZone (), Globals.env.randomGenerator, .5);
 21:   boolean frobbed, resisting;
 22:   int direction, energy;

 24:   DirectedAgent2d (Zone aZone,
 25:                    Organization org,
 26:                    int x, int y,
 27:                    int scatter, int size,
 28:                    double resistProbabilityMean, double resistProbabilityDeviation,
 29:                    int energyMean, int energyDeviation) {
 30:     super (aZone, org, x, y, scatter, size);
 31:     this.resistProbabilityDistribution =
 32:       new NormalDistImpl (aZone,
 33:                           Globals.env.randomGenerator,
 34:                           resistProbabilityMean,
 35:                           resistProbabilityDeviation);
 36:     this.energyDistribution =
 37:       new NormalDistImpl (aZone,
 38:                           Globals.env.randomGenerator,
 39:                           energyMean,
 40:                           energyDeviation);
 41:   }

 43:   public void setOffsets () {
 44:     if (direction == 90) {
 45:       xoffset = 0;
 46:       yoffset = 1;
 47:     } else if (direction == 270) {
 48:       xoffset = 0;
 49:       yoffset = -1;
 50:     } else if (direction == 180) {
 51:       xoffset = -1;
 52:       yoffset = 0;
 53:     } else if (direction == 0) {
 54:       xoffset = 1;
 55:       yoffset = 0;
 56:     } else {
 57:       if (direction < 90) {
 58:         xoffset = 1;
 59:         yoffset = 1;
 60:       } else if (direction < 180) {
 61:         xoffset = -1;
 62:         yoffset = 1;
 63:       } else if (direction < 270) {
 64:         xoffset = -1;
 65:         yoffset = -1;
 66:       } else {
 67:         xoffset = 1;
 68:         yoffset = -1;
 69:       }
 70:     }
 71:   }

 73:   public void moveDirection () {
 74:     setOffsets ();
 75:     moveAgent (xoffset, yoffset);
 76:   }

 78:   public Object drawSelfOn (Raster r) {
 79:     double theta = Math.toRadians ((double) direction);
 80:     int xo = (int) Math.round (size * Math.cos (theta));
 81:     int yo = (int) Math.round (size * Math.sin (theta));

 83:     r.ellipseX0$Y0$X1$Y1$Width$Color (x - size, y - size,
 84:                                       x + size, y + size,
 85:                                       1, color);
 86:     r.lineX0$Y0$X1$Y1$Width$Color (x, y, x + xo, y + yo, 1, color);

 88:     return this;
 89:   }

 91:   public void sampleResistProbability () {
 92:     double prob;

 94:     prob = resistProbabilityDistribution.getDoubleSample ();
 95:     if (prob > 1.0)
 96:       prob = 1.0;
 97:     else if (prob < 0)
 98:       prob = 0.0;
 99:     resistProbability = prob;
100:   }

102:   public void sampleEnergy () {
103:     energy = Math.abs ((int) energyDistribution.getDoubleSample ());
104:   }

106:   public boolean frob (int direction) {
107:     frobbed = true;
108:     if (!bernoulliDist.getSampleWithProbability (resistProbability)
109:         || energy == 0) {
110:       resisting = false;
111:       this.direction = direction;
112:     } else {
113:       resisting = true;
114:       System.out.println (this + " expending energy (" + energy + ")");
115:       energy--;
116:     }
117:     return resisting;
118:   }

120:   public void clearFrobStatus () {
121:     resisting = false;
122:     frobbed = false;
123:   }
124: }