-
Notifications
You must be signed in to change notification settings - Fork 0
/
ants.pde
198 lines (171 loc) · 4.39 KB
/
ants.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
boolean[][] food;
float[][] scent;
float[][] paths;
float[][] scentAllTime;
float[][] pathsAllTime;
ArrayList antHills;
int foodValue = 300;
boolean drawObjects = true;
boolean drawPaths = false;
boolean drawScents = false;
boolean drawAllTime = false;
boolean unlimitedFood = false;
int lasttime = 0;
boolean drawP = false;
boolean drawS = false;
float r;
float g;
float b;
float maxScent;
float maxPath;
void setup ()
{
frameRate(200);
size(400, 400, P2D);
//smooth();
background(255);
fill(0);
food = new boolean[height][width];
scent = new float[height][width];
paths = new float[height][width];
scentAllTime = new float[height][width];
pathsAllTime = new float[height][width];
antHills = new ArrayList();
antHills.add(new AntHill(new QVector2D(random(40, height-40), random(40, width-40))));
maxScent = 0;
maxPath = 0;
growFood();
for (int i = 0; i < scent.length; i++) {
for (int j = 0; j < scent[i].length; j++) {
scent[i][j] = 0;
paths[i][j] = 0;
scentAllTime[i][j] = 0;
pathsAllTime[i][j] = 0;
}
}
lasttime = millis();
}
void draw ()
{
println(millis() - lasttime);
lasttime = millis();
if (mousePressed && mouseButton == RIGHT) food[mouseX][mouseY] = true;
background(0);
for (int i = 0; i < scent.length; i++) {
for (int j = 0; j < scent[i].length; j++) {
if (paths[i][j] > 0.25) {
paths[i][j] *= 0.995;
drawP = true;
} else {
drawP = false;
paths[i][j] = 0;
}
if (scent[i][j] > 0.25) {
scent[i][j] *= 0.995;
drawS = true;
} else {
drawS = false;
scent[i][j] = 0;
}
scentAllTime[i][j] += scent[i][j];
pathsAllTime[i][j] += paths[i][j];
if (drawAllTime) {
if (scentAllTime[i][j] > maxScent) maxScent = scentAllTime[i][j];
if (pathsAllTime[i][j] > maxPath) maxPath = pathsAllTime[i][j];
r = 0;
g = 0;
b = 0;
if (pathsAllTime[i][j] > 1) {
r = map(pathsAllTime[i][j], 0, maxPath/3, 0, 255);
}
if (scentAllTime[i][j] > 1) {
g = map(scentAllTime[i][j], 0, maxScent/3, 0, 255);
}
if (r + g + b > 3) {
stroke(r, g, b);
point(i, j);
}
} else if ((drawPaths || drawScents) && (drawP || drawS)) {
r = 0;
g = 0;
b = 0;
if (drawPaths && drawP) {
r = map(paths[i][j], 0, 30, 0, 255);
}
if (drawScents && drawS) {
g = map(scent[i][j], 0, 50, 0, 255);
}
stroke(r, g, b);
point(i, j);
}
}
}
if (drawObjects) {
stroke(0, 255, 0);
for (int i = 0; i < food.length; i++) {
for (int j = 0; j < food[i].length; j++) {
if (food[i][j]) point(i, j);
}
}
}
AntHill antHill;
for (int i = 0; i < antHills.size(); i++) {
antHill = (AntHill) antHills.get(i);
if (antHill.dead) {
antHills.remove(i);
} else {
antHill.draw();
}
}
if (antHills.size() < 1) setup();
}
void mouseReleased ()
{
if (mouseButton == LEFT) antHills.add(new AntHill(new QVector2D(mouseX, mouseY)));
}
void keyReleased ()
{
if (key == ' ') {
AntHill antHill;
for (int i = 0; i < antHills.size(); i++) {
antHill = (AntHill) antHills.get(i);
antHill.addAnt();
}
} else if (key == 'p') {
drawPaths = drawPaths ? false : true;
} else if (key == 's') {
drawScents = drawScents ? false : true;
} else if (key == 'o') {
drawObjects = drawObjects ? false : true;
} else if (key == 'c') {
setup();
} else if (key == 'a') {
drawAllTime = drawAllTime ? false : true;
} else if (key == 'f') {
growFood();
} else if (key == 'r') {
unlimitedFood = unlimitedFood ? false : true;
}
}
void growFood ()
{
int xBlob = 0;
int yBlob = 0;
int tall = 0;
int wide = 0;
int numberOfFood = 0;
int xFood = 0;
int yFood = 0;
for (int i = 0; i < 10; i++) {
xBlob = (int) random(1, width);
yBlob = (int) random(1, height);
tall = (int) random(30, 70);
wide = (int) random(30, 70);
numberOfFood = (int) random(40, 100);
for (int j = 0; j < numberOfFood; j++) {
xFood = (int) random(xBlob, (xBlob + wide));
yFood = (int) random(yBlob, (yBlob + tall));
if (xFood < (width - 1) && yFood < (height - 1)) food[xFood][yFood] = true;
}
}
}