-
Notifications
You must be signed in to change notification settings - Fork 0
/
ants_0_1.pde
154 lines (127 loc) · 3.15 KB
/
ants_0_1.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
PrintWriter output;
int counter;
final color ANT_COLOR = #000000;
final color HOME_COLOR = #775c0a;
final color FOOD_COLOR = #2eb217;
final color SIGNAL_POS_COLOR = #7251cc;
final color SIGNAL_NEG_COLOR = #f70e35;
ArrayList<Signal>[][] signals;
ArrayList<Signal>[][] signalsTBR;
ArrayList<Food> foods = new ArrayList<Food>();
ArrayList<Food> foodsTBR = new ArrayList<Food>();
ArrayList<Ant> ants = new ArrayList<Ant>();
ArrayList<Ant> antsTBR = new ArrayList<Ant>();
Home home;
int NUMBER_ANTS = 500;
boolean paused = true;
boolean show_signals = false;
boolean show_all = true;
void setup() {
//size(1280, 720);
size(1600, 900);
frameRate(1000);
ellipseMode(RADIUS);
fill(0);
textAlign(LEFT, TOP);
textSize(30);
output = createWriter("ants.csv");
signals = new ArrayList[int(width/Ant.VIEW_CONE_RADIUS)+1][int(height/Ant.VIEW_CONE_RADIUS)+1];
signalsTBR = new ArrayList[signals.length][signals[0].length];
for (int i = 0; i < signals.length; i++) {
for (int j = 0; j < signals[0].length; j++) {
signals[i][j] = new ArrayList<Signal>();
signalsTBR[i][j] = new ArrayList<Signal>();
}
}
home = new Home(new PVector(width/2, height/2), 50);
for (int i = 0; i < NUMBER_ANTS; i++) {
home.newAnt();
}
}
void draw() {
background(0xcc);
if (!paused) {
updateAll();
if (foods.size() < 8) {
foods.add(new Food(new PVector(random(0, width), random(0, height)), int(random(50, 100))));
}
counter ++;
if (counter >= 100) {
counter = 0;
output.println(ants.size());
// output.flush();
}
if (ants.size() <= 0) {
output.flush();
output.close();
exit();
}
}
if (show_all) {
showAll();
}
text(ants.size(), 0, 0);
text(frameRate, 0, 25);
}
void updateAll() {
for (int i = 0; i < signals.length; i++) {
for (int j = 0; j < signals[0].length; j++) {
for (Signal s : signals[i][j]) {
s.update();
}
signals[i][j].removeAll(signalsTBR[i][j]);
}
}
for (Ant a : ants) {
a.update();
}
ants.removeAll(antsTBR);
foods.removeAll(foodsTBR);
home.update();
}
void showAll() {
if (show_signals) {
for (int i = 0; i < signals.length; i++) {
for (int j = 0; j < signals[0].length; j++) {
for (Signal s : signals[i][j]) {
s.show();
}
}
}
}
for (Food f : foods) {
f.show();
}
for (Ant a : ants) {
a.show();
}
home.show();
}
void mousePressed() {
if (mouseButton == LEFT) {
addNewSignal(new Signal(new PVector(mouseX, mouseY), 100));
} else if (mouseButton == RIGHT) {
addNewSignal(new Signal(new PVector(mouseX, mouseY), -100));
} else if (mouseButton == CENTER) {
foods.add(new Food(new PVector(mouseX, mouseY), 200));
}
}
void keyPressed() {
if (key == ' ') {
paused = !paused;
} else if (key == 's') {
show_signals = !show_signals;
} else if (key == ESC) {
output.flush();
output.close();
exit();
} else if ( key == 'a') {
show_all = !show_all;
}
}
void addNewSignal(Signal s) {
signals[signalsIndex(s.pos.x)][signalsIndex(s.pos.y)].add(s);
}
int signalsIndex(float p) {
return int(p/Ant.VIEW_CONE_RADIUS);
}