/
birds.cpp
370 lines (260 loc) · 9.53 KB
/
birds.cpp
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
extern "C" {
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
}
#include <sstream>
#include <iostream>
#include "pointLineSegmentDistance.h"
#include "options.h"
#include "utils.h"
#include "bird.h"
#include "stats.h"
#include "birdslib.h"
#include "birdsmouse.h"
#include "birdskeys.h"
#include "birdsdisplay.h"
#include "birdsmenu.h"
using namespace std;
// compilare con: g++ -framework GLUT -framework OpenGL birds.c -o birds
// eseguire con: ./birds [n] (con n = num. di uccelli)
/* cose da fare:
* v- OSTACOLI: Segmenti che gli uccelli devono evitare (tramite un'apposito calcolo) per non incontrare la morte.
* v- STEPS PER TUTTI: Inserire nella classe bird un vettore che mantenga lo storico delle posizioni (quindi un percorso).
* Con possibilità di visualizzare tutti i percorsi insieme.
* v- ANALISI PERCORSI PREFERITI: Mappare la presenza degli uccelli durante il tempo suddividendo l'area in tanti quadrati
* e per ogni quadrato tenere un contatore. Incrementare il contatore aggiungendo il numero
* di uccelli interni al quadrato relativo ogni istante di tempo (o anche più raramente se
* i quadrati sono abbastanza grandi).
* - MIGLIORIE GRAFICHE: Spostare le statistiche dal terminale a un'area della finestra.
* Inserire pulsanti nella toolbar che emulino le più importanti voci del menu popup.
* - RIPRODUZIONE: Permettere l'evoluzione dei caratteri degli uccelli tramite la riproduzione e la "selezione naturale".
* - PREDATORI: Altre creature che inseguono e mangiano gli uccelli (l'evoluzione cambia?).
* - MIGLIORIE ALLE PRESTAZIONI: Ottimizzare i calcoli per avere maggiore fluidità e poter aumentare il numero massimo
* di uccelli. Inserire una modalità "velocità massima" (niente ritardi, magari anche senza
* calcoli grafici).
* - MIGLIORIE ALLA PASSMAP: Possibilità di scegliere la definizione, salvataggi automatici (immagini o dati) ogni tot.
* - DIRMAP: Simile alla passMap, ma registra la somma delle velocità di tutti gli uccelli passati (e il numero) in modo
* da mostrare una serie di vettori (uno per settore, che parte dal suo centro) proporzionali alla media.
*/
static void timerCallback (int value) {
if((cicli%7500 == 0 || liveBirdNum<50) && obstacleTest) { //only for test if a bird can pass trough an obstacle
numberOfObstaclesTests++;
cout << "BEGINNING OBSTACLE TEST N. " << numberOfObstaclesTests << " LETHAL DISTANCE = "<<LETHAL_DISTANCE<<endl << endl;
if(numberOfObstaclesTests%100 == 0)
LETHAL_DISTANCE -= 0.00005;
cicli = 0;
for(int i=0; i<MAXBIRDS; i++)
{
birds[i] = new bird(i);
}
liveBirdNum = MAXBIRDS;
}
if(!paused) {
for (int i=0; i<birdNum; i++) {
birds[i]->doStep();
// aggiorna la passMap
if(birds[i]->live)
passMapUpdate(birds[i]->posX, birds[i]->posY);
}
cicli++;
}
if(liveBirdNum > 600) pointSize = lineWidth = 2.0;
else if(liveBirdNum > 200) pointSize = lineWidth = 3.0;
else pointSize = 4.0;
glutPostRedisplay();
/* call back again after elapsedUSecs have passed */
if(!stop) glutTimerFunc (millsec_per_frame, timerCallback, value);
}
int main(int argc, char *argv[]) //modificata il 13.9.10
{
if (argc < 2 || argc > 6 || atoi(argv[1]) > MAXBIRDS) {
errorStart = true;
} else {
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-d") == 0) {
DEBUG = 1;
} else if (strcmp(argv[i], "-ot") == 0) { //obstacle test
if (DEBUG) {
printf("\nDEBUG main0.0. argv[%d]=%s\n", i, argv[i]);
fflush(stdout);
}
obstacleTest = true;
obstacles[0][0] = -1.0;
obstacles[0][1] = -1.0;
obstacles[0][2] = 1.0;
obstacles[0][3] = 1.0;
obstNum++;
millsec_per_frame = 10 / FRAMES_PER_SEC;
startbirds = 800;
} else if (strcmp(argv[i], "-ot2") == 0) { //obstacle test 2
obstacleTest2 = true;
axes = true;
useCampo=false;
randomObstacles();
} else if (strcmp(argv[i], "-ot2c") == 0) { //obstacle test 2 campo
obstacleTest2 = true;
axes = true;
useCampo=true;
randomObstacles();
} else if(strcmp(argv[i], "-wb") == 0) {
whiteBackground=true;
} else if (atoi(argv[i]) == 0) errorStart = true;
}
}
if (errorStart) {
printf("Usage: ./birds n [-d] [-ot] [-ot2] [-wb] \n");
printf(" ( where \'n\' is the starting number of birds and must be smaller than %d \n", MAXBIRDS);
printf(" -d (optional) starts birds in debug mode\n");
printf(" -ot (optional) tests if birds can pass through obstacles\n");
printf(" -ot2 (optional) tests the vector resultant from an obstacle\n");
printf(" -ot2c (optional) tests the vector resultant from an obstacle in a campo (field) mode\n");
printf(" -wb (optional) sets a white background (better bird-look) )\n");
return 0;
}
srand(time(NULL));
if (DEBUG) {
printf("\nDEBUG main.\n");
fflush(stdout);
}
glutInit(&argc, argv);
if (!obstacleTest) startbirds = atoi(argv[1]);
if (DEBUG) {printf("\nDEBUG2 main.\n"); fflush(stdout); }
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(700, 740);
glutCreateWindow("BIRDS");
if (DEBUG) { printf("\nDEBUG3 main.\n"); fflush(stdout); }
int killSubMenu = glutCreateMenu(menuecho);
int selectSubMenu = glutCreateMenu(menuecho);
birds[0] = new bird(0);
birds[0]->hawk = true;
birdNum++;
liveBirdNum++;
glutSetMenu(killSubMenu);
glutAddMenuEntry("ID 0", 0 - startbirds);
glutSetMenu(selectSubMenu);
glutAddMenuEntry("ID 0", 0);
birds[1] = new bird(0);
birds[1]->hawk = true;
birdNum++;
liveBirdNum++;
glutSetMenu(killSubMenu);
glutAddMenuEntry("ID 0", 0 - startbirds);
glutSetMenu(selectSubMenu);
glutAddMenuEntry("ID 0", 0);
if(!obstacleTest2)
for (int i = birdNum; i < startbirds; i++) {
birds[i] = new bird(i);
birdNum++;
liveBirdNum++;
std::ostringstream os;
os << "ID " << i;
glutSetMenu(killSubMenu);
glutAddMenuEntry(os.str().c_str(), -i - startbirds);
glutSetMenu(selectSubMenu);
glutAddMenuEntry(os.str().c_str(), -i);
//printf("costruito uccello %d di %d\n", i+1, startbirds);
}
printf("costruiti %d uccelli (%d vivi)\n", birdNum, liveBirdNum);
if (whiteBackground) {
RedRGB = 0.0;
GreenRGB = 0.0;
BlueRGB = 0.0;
showSpeed = false;
nativeColor = false;
colorSpeed = false;
relativeColorSpeed = false;
dinamicColor = false;
}
int pointSubMenu = glutCreateMenu(menuecho);
glutAddMenuEntry("1", 30);
glutAddMenuEntry("2", 31);
glutAddMenuEntry("3", 32);
glutAddMenuEntry("4", 33);
glutAddMenuEntry("5", 34);
glutAddMenuEntry("6", 35);
glutAddMenuEntry("7", 36);
glutAddMenuEntry("8", 37);
glutAddMenuEntry("9", 38);
int colorSubMenu = glutCreateMenu(menuecho);
glutAddMenuEntry("Red", 60);
glutAddMenuEntry("Blue", 61);
glutAddMenuEntry("Yellow", 62);
glutAddMenuEntry("Pink", 63);
glutAddMenuEntry("Green", 64);
glutAddMenuEntry("Orange", 65);
glutAddMenuEntry("Black", 66);
glutAddMenuEntry("Natural-Static", 68);
glutAddMenuEntry("Natural-Dinamic", 69);
glutAddMenuEntry("Speed-Absolute", 71);
glutAddMenuEntry("Speed-Relative", 72);
int birdSubMenu = glutCreateMenu(menuecho);
glutAddSubMenu("Select", selectSubMenu);
glutAddSubMenu("Kill", killSubMenu);
glutAddSubMenu("Dimension", pointSubMenu);
glutAddSubMenu("Color", colorSubMenu);
int animationSubMenu = glutCreateMenu(menuecho);
glutAddMenuEntry("0.01x", 120);
glutAddMenuEntry("0.1x", 121);
glutAddMenuEntry("0.5x", 122);
glutAddMenuEntry("1x", 123);
glutAddMenuEntry("2x", 124);
glutAddMenuEntry("10x", 125);
glutAddMenuEntry("100x", 126);
showHideSubMenu = glutCreateMenu(menuecho);
glutAddMenuEntry("Show Axes", 70);
if (showSpeed) glutAddMenuEntry("Hide Speed", 80);
else glutAddMenuEntry("Show Speed", 80);
glutAddMenuEntry("Show SightCircle", 90);
glutAddMenuEntry("Show ObstCircle", 91);
glutAddMenuEntry("Show DistCircles", 92);
glutAddMenuEntry("Show Steps", 93);
glutAddMenuEntry("Show NearBirdsAverage", 94);
glutAddMenuEntry("Show IDs", 95);
glutAddMenuEntry("Show All", 99);
glutAddMenuEntry("Show PassMap",81);
glutAddMenuEntry("Hide Birds",82);
attribMenu = glutCreateMenu(menuecho);
glutAddSubMenu("Bird", birdSubMenu);
glutAddSubMenu("Animation", animationSubMenu);
glutAddSubMenu("Show/Hide", showHideSubMenu);
glutAddMenuEntry("Go Towards/As friends", 100);
if(!obstacleTest2)
randomObstacles();
/*
obstacles[0][0] = 0.0;
obstacles[0][1] = -0.8;
obstacles[0][2] = -0.5;
obstacles[0][3] = -0.5;
obstNum++;
obstacles[1][0] = 0.0;
obstacles[1][1] = 0.8;
obstacles[1][2] = 0.8;
obstacles[1][3] = 0.5;
obstNum++;
obstacles[2][0] = 0.0;
obstacles[2][1] = 0.8;
obstacles[2][2] = 0.8;
obstacles[2][3] = 0.5;
obstNum++;
*/
if (!obstacleTest2) glutTimerFunc(millsec_per_frame, timerCallback, 0);
//if (!obstacleTest2) glutTimerFunc(millsec_per_frame * 4, stepCallback, 0);
if (!obstacleTest2) glutTimerFunc(10, statsUpdate, 0);
passMapInit();
glutDisplayFunc(display);
glutMotionFunc(motion);
glutPassiveMotionFunc(motion);
glutKeyboardFunc(myKeyboard);
glutMouseFunc(myMouse);
glutMenuStatusFunc(menustatus);
if (DEBUG) {
printf("\nDEBUG main.\n");
fflush(stdout);
}
glutMainLoop();
/* Quest'ultima istruzione non sara' mai eseguita. */
return 1;
}