1: package agent2d;
3: import swarm.objectbase.SwarmImpl;
4: import swarm.objectbase.Swarm;
5: import swarm.activity.Schedule;
6: import swarm.activity.ScheduleImpl;
7: import swarm.activity.Activity;
8: import swarm.activity.Action;
9: import swarm.defobj.Zone;
10: import swarm.gui.Raster;
11: import swarm.Selector;
12: import swarm.defobj.FArgumentsC;
13: import swarm.defobj.FArguments;
14: import swarm.defobj.FArgumentsImpl;
15: import swarm.defobj.FArgumentsCImpl;
16: import swarm.defobj.FCallImpl;
18: import swarm.Globals;
20: import ObserverSwarm;
21: import Organization;
23: public class Marcus2d extends DirectedAgent2d {
24: private final static int incubationTime = 50;
25: Schedule schedule;
26: Selector incubateSelector;
27: Selector checkWorkSelector;
28: Selector xmoveSelector, ymoveSelector;
29: Action xmoveNext, ymoveNext;
30: int xfreq, yfreq;
31: int incubationRemaining;
32: boolean working;
34: public Marcus2d (Zone aZone, Organization org, int x, int y) {
35: super (aZone, org, x, y, 5, 4, .25, .75, 100, 10);
36:
37: schedule = new ScheduleImpl (aZone, true);
39: try {
40: Selector sel =
41: new Selector (getClass (), "startIncubation", false);
42: FArgumentsC fac =
43: new FArgumentsCImpl (new FArgumentsImpl ());
44: fac.createBegin (getZone ());
45: fac.setSelector (sel);
46: fac.addInt (0);
47: FArguments fa = (FArguments) fac.createEnd ();
49: schedule.at$createFAction
50: (0, new FCallImpl (getZone (), this, sel, fa));
51: incubateSelector =
52: new Selector (getClass (), "incubate", false);
53: checkWorkSelector =
54: new Selector (getClass (), "checkWork", false);
55: xmoveSelector =
56: new Selector (getClass (), "xmove", false);
57: ymoveSelector =
58: new Selector (getClass (), "ymove", false);
59: } catch (Exception e) {
60: e.printStackTrace (System.err);
61: System.exit (1);
62: }
63: }
64:
65: public void startIncubation (int t) {
66: color = ObserverSwarm.MarcusIncubateColor;
67: working = false;
68: incubationRemaining = incubationTime;
69: sampleResistProbability ();
70: sampleEnergy ();
71: schedule.at$createActionTo$message (t, this, incubateSelector);
72: }
74: public void startWork (int t) {
75: working = true;
76: if (frobbed)
77: color = ObserverSwarm.MarcusListenColor;
78: else {
79: color = ObserverSwarm.MarcusNativeColor;
80: direction =
81: Globals.env.uniformIntRand.getIntegerWithMin$withMax (0, 359);
82: }
83:
84: setOffsets ();
85: if (direction == 90) {
86: yfreq = 1;
87: xfreq = 0;
88: } else if (direction == 270) {
89: xfreq = 0;
90: yfreq = 1;
91: } else if (direction == 180) {
92: xfreq = 1;
93: yfreq = 0;
94: } else if (direction == 0) {
95: xfreq = 1;
96: yfreq = 0;
97: } else {
98: double radians = Math.toRadians ((double) direction);
99: double slope = Math.tan (radians);
101: if (Math.abs (slope) >= 1.0) {
102: yfreq = 1;
103: xfreq = (int) Math.abs (slope);
104: }
105: else {
106: xfreq = 1;
107: yfreq = (int) Math.abs (1.0 / slope);
108: }
109: }
110: schedule.at$createActionTo$message (t, this, checkWorkSelector);
111: if (xfreq > 0)
112: schedule.at$createActionTo$message (t, this, xmoveSelector);
113: if (yfreq > 0)
114: schedule.at$createActionTo$message (t, this, ymoveSelector);
115: }
117: public void incubate () {
118: if (incubationRemaining == 0 || (frobbed && !resisting)) {
119: if (energy > 0)
120: startWork (Globals.env.getCurrentTime () + 1);
121: else
122: startIncubation (Globals.env.getCurrentTime () + 1);
123: }
124: else {
125: color = resisting ? ObserverSwarm.MarcusResistColor : ObserverSwarm.MarcusIncubateColor;
126: randomWalk ();
127: incubationRemaining--;
128: schedule.at$createActionTo$message (Globals.env.getCurrentTime () + 1,
129: this,
130: incubateSelector);
131: }
132: clearFrobStatus ();
133: }
135: public void checkWork () {
136: if (energy == 0) {
137: if (xmoveNext != null) {
138: schedule.remove (xmoveNext);
139: xmoveNext = null;
140: }
141: if (ymoveNext != null) {
142: schedule.remove (ymoveNext);
143: ymoveNext = null;
144: }
145: startIncubation (Globals.env.getCurrentTime () + 1);
146: }
147: else {
148: energy--;
149: schedule.at$createActionTo$message (Globals.env.getCurrentTime () + 1,
150: this,
151: checkWorkSelector);
152: }
153: }
154:
155: public void xmove () {
156: moveAgent (xoffset, 0);
157: xmoveNext = schedule.at$createActionTo$message (Globals.env.getCurrentTime () + xfreq,
158: this,
159: xmoveSelector);
160: }
161: public void ymove () {
162: moveAgent (0, yoffset);
163: ymoveNext = schedule.at$createActionTo$message (Globals.env.getCurrentTime () + yfreq,
164: this,
165: ymoveSelector);
166: }
168: public Activity activateIn (Swarm context) {
169: super.activateIn (context);
171: schedule.activateIn (this);
172: return getActivity ();
173: }
176: public boolean frob (int direction) {
177: if (!working)
178: return super.frob (direction);
179: else
180: return false;
181: }
182: }