-
Notifications
You must be signed in to change notification settings - Fork 0
/
ant.pde
267 lines (234 loc) · 7.95 KB
/
ant.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
class Ant
{
AntHill antHill;
QVector2D position;
QVector2D direction;
QVector2D idealDirection;
QVector2D finalDestination = new QVector2D(0, 0);
boolean hasFood = false;
int speed = 2;
int size = 4;
int appetite = 0;
int maxAppetite = width*2;
boolean exploring = true;
int whiteness = 0;
float rangeOfSight = 10;
QVector2D nearestFood = null;
float rangeOfSmell = 10;
QVector2D nearestSmell = null;
int timeWithoutFood = 0;
QVector2D tempVector1 = new QVector2D(0, 0);
QVector2D tempVector2 = new QVector2D(0, 0);
Ant (AntHill _antHill)
{
this.antHill = _antHill;
this.position = this.antHill.getPosition();
}
float getAngle ()
{
return (float) Math.toDegrees(this.direction.heading2D());
}
float getIdealAngle ()
{
return (float) Math.toDegrees(this.idealDirection.heading2D());
}
float getDeviation ()
{
return this.getAngle() - this.getIdealAngle();
}
void leaveAntHill ()
{
this.newFinalDestination();
this.direction = this.finalDestination.get();
this.direction.sub(this.position);
this.exploring = true;
this.hasFood = false;
}
void draw ()
{
if (this.isHome() && this.hasFood) {
this.antHill.addFood(foodValue);
this.hasFood = false;
} else if (this.appetite > 0 && this.isHome()) {
if (this.antHill.takeFood(2) != 2) {
this.timeWithoutFood++;
} else {
this.appetite -= 2;
}
} else {
if (this.appetite <= 0) this.leaveAntHill();
this.go();
}
}
boolean isHome ()
{
QVector2D temp = this.antHill.getPosition();
temp.sub(this.position);
return (temp.mag() < 2);
}
void go ()
{
if (!this.hasFood && this.appetite < this.maxAppetite) {
this.updateNearestFood();
if (this.nearestFood != null) {
this.finalDestination.set(this.nearestFood.get());
} else {
this.smell();
if (this.nearestSmell != null) {
this.finalDestination.set(this.nearestSmell.get());
}
}
}
this.updateIdealDirection();
if (this.idealDirection.mag() < this.speed) {
if (this.nearestFood != null) {
// yay, we have the food!
this.hasFood = true;
this.exploring = false;
this.finalDestination.set(this.antHill.getPosition());
food[(int) this.nearestFood.x][(int) this.nearestFood.y] = false;
} else {
// reached our random destination, now go look somewhere else
this.newFinalDestination();
}
this.updateIdealDirection();
} else if (this.appetite > this.maxAppetite) {
// go home if we are too hungry
this.exploring = false;
this.finalDestination.set(this.antHill.getPosition());
}
// stray off course if we’re looking for stuff
int randomness = this.exploring ? 20 : 5;
// figure out actual direction
this.direction.rotate(random(randomness*-1, randomness));
if (abs(this.getDeviation()) > (randomness * 2)) {
this.direction.set(this.idealDirection.get());
this.direction.rotate(random(randomness*-1/2, randomness/2));
}
// set distance
this.direction.normalize();
float multiplier = this.idealDirection.mag() > this.speed ? this.speed : this.idealDirection.mag();
this.direction.mult(this.speed);
// update position
this.position.add(this.direction);
// draw the ant
if (drawObjects) {
if (this.hasFood) {
fill(128, 255, 128);
} else {
// set colour based on appetite
this.whiteness = (int) map(this.appetite, 0, this.maxAppetite, 0, 128);
fill(255, whiteness, whiteness);
}
noStroke();
ellipse(this.position.x, this.position.y, this.size, this.size);
}
// all this walking around is making us hungry
this.appetite++;
// leave some pheromones if we have food
if (this.hasFood) {
int constrainedX = (int) constrain(this.position.x, 0, width-1);
int constrainedY = (int) constrain(this.position.y, 0, height-1);
scent[constrainedX][constrainedY] += 10;
scent[constrainedX - 1][constrainedY] += 10;
scent[constrainedX + 1][constrainedY] += 10;
scent[constrainedX][constrainedY - 1] += 10;
scent[constrainedX][constrainedY + 1] += 10;
} else {
/*
if (scent[(int) constrain(this.position.x, 0, width-1)][(int) constrain(this.position.y, 0, height-1)] > 0.5) {
scent[(int) constrain(this.position.x, 0, width-1)][(int) constrain(this.position.y, 0, height-1)] *= 5;
}
*/
paths[(int) constrain(this.position.x, 0, width-1)][(int) constrain(this.position.y, 0, height-1)] += 10;
}
}
void updateNearestFood ()
{
int xLow = (int) (this.position.x - this.rangeOfSight);
int xHigh = (int) (this.position.x + this.rangeOfSight);
int yLow = (int) (this.position.y - this.rangeOfSight);
int yHigh = (int) (this.position.y + this.rangeOfSight);
if (xLow < 0) xLow = 0;
if (yLow < 0) yLow = 0;
if (xHigh > width) xHigh = width;
if (yHigh > height) yHigh = height;
this.tempVector2 = null;
float lowestDistance = this.rangeOfSight;
float distance = this.rangeOfSight + 1;
for (int i = xLow; i < xHigh; i++) {
for (int j = yLow; j < yHigh; j++) {
if (food[i][j]) {
this.tempVector1.set(i, j);
this.tempVector1.sub(this.position);
distance = this.tempVector1.mag();
if (distance < this.rangeOfSight && distance < lowestDistance) {
lowestDistance = distance;
this.tempVector2 = new QVector2D(i, j);
}
}
}
}
this.nearestFood = this.tempVector2 == null ? null : this.tempVector2.get();
}
void smell ()
{
float angle = this.getAngle();
int xLow = 0;
int xHigh = 0;
int yLow = 0;
int yHigh = 0;
// this is where paying attention in math classes would have really helped…
if (angle > -45 && angle < 45) {
xLow = (int) (this.position.x + 1);
xHigh = (int) (this.position.x + this.rangeOfSmell);
yLow = (int) (this.position.y - this.rangeOfSmell);
yHigh = (int) (this.position.y + this.rangeOfSmell);
} else if (angle > 45 && angle < 135) {
xLow = (int) (this.position.x - this.rangeOfSmell);
xHigh = (int) (this.position.x + this.rangeOfSmell);
yLow = (int) (this.position.y + 1);
yHigh = (int) (this.position.y + this.rangeOfSmell);
} else if (angle < -45 && angle > -135) {
xLow = (int) (this.position.x - this.rangeOfSmell);
xHigh = (int) (this.position.x + this.rangeOfSmell);
yLow = (int) (this.position.y - this.rangeOfSmell);
yHigh = (int) (this.position.y - 1);
} else {
xLow = (int) (this.position.x - this.rangeOfSmell);
xHigh = (int) (this.position.x - 1);
yLow = (int) (this.position.y - this.rangeOfSmell);
yHigh = (int) (this.position.y + this.rangeOfSmell);
}
if (xLow < 0) xLow = 0;
if (yLow < 0) yLow = 0;
if (xHigh > width) xHigh = width;
if (yHigh > height) yHigh = height;
this.tempVector2 = null;
float lowestDistance = this.rangeOfSmell;
float distance = this.rangeOfSmell + 1;
for (int i = xLow; i < xHigh; i++) {
for (int j = yLow; j < yHigh; j++) {
if (scent[i][j] > 0.5) {
this.tempVector1.set(i, j);
this.tempVector1.sub(this.position);
distance = this.tempVector1.mag();
if (distance < this.rangeOfSmell && distance < lowestDistance) {
lowestDistance = distance;
this.tempVector2 = new QVector2D(i, j);
}
}
}
}
this.nearestSmell = this.tempVector2 == null ? null : this.tempVector2.get();
}
void updateIdealDirection ()
{
this.idealDirection = this.finalDestination.get();
this.idealDirection.sub(this.position);
}
void newFinalDestination ()
{
this.finalDestination.set(random(0, height), random(0, width));
}
}