-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMeshGraphics.cpp
More file actions
549 lines (487 loc) · 12.6 KB
/
MeshGraphics.cpp
File metadata and controls
549 lines (487 loc) · 12.6 KB
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#include <iostream>
#include <stdlib.h>
#include <omp.h>
#include <unistd.h>
#include "Occupant.cpp"
#include "SDL/SDL.h"
#include <math.h>
using namespace std;
#define STEPS 50
#define SIZE 486
#define DEATHPC 0.0002175
#define BIRTHPC 0.0000358
#define ZOMBIEAGE 28
#define ZOMBIEATTACK 0.7
SDL_Surface *screen = SDL_SetVideoMode(SIZE, SIZE, 0, SDL_DOUBLEBUF);
void setpixel(SDL_Surface *screen, int x, int y, Uint8 r, Uint8 g, Uint8 b)
{
Uint32 *pixmem32;
Uint32 colour;
colour = SDL_MapRGB( screen->format, r, g, b );
pixmem32 = (Uint32*) screen->pixels + y *SIZE + x;
*pixmem32 = colour;
}
bool isEmpty(Occupant o) {
if (o.type == 'O') {
return true;
}
return false;
}
bool isReproducible(Occupant o) {
if (o.type == 'H' && ((o.gender == 'F' && o.age < 16060 && o.age > 5475) || (o.gender == 'M' && o.age > 5475))) {
return true;
}
return false;
}
bool isExposed(Occupant o) {
if (o.type == 'E') {
return true;
}
return false;
}
bool isHuman(Occupant o) {
if (o.type == 'H') {
return true;
}
return false;
}
bool isZombie(Occupant o) {
if (o.type == 'Z') {
return true;
}
return false;
}
bool isOppositeGender(Occupant h, char gender) {
if(h.gender == gender) {
return false;
}
return true;
}
void zombify(Occupant **MeshA){
#if defined(_OPENMP)
#pragma omp parallel for default(none) shared(MeshA)
#endif
for (int i = 1; i <= SIZE; i++) {
for (int j = 1; j <= SIZE; j++) {
if (isZombie(MeshA[i][j])){
if (isHuman(MeshA[i-1][j]) ) {
if (drand48() < ZOMBIEATTACK) {
MeshA[i-1][j].type = 'E';
MeshA[i-1][j].exposedPeriod = 0;
}
}
if (isHuman(MeshA[i+1][j]) ) {
if (drand48() < ZOMBIEATTACK) {
MeshA[i+1][j].type = 'E';
MeshA[i+1][j].exposedPeriod = 0;
}
}
if (isHuman(MeshA[i][j-1]) ) {
if (drand48() < ZOMBIEATTACK) {
MeshA[i][j-1].type = 'E';
MeshA[i][j-1].exposedPeriod = 0;
}
}
if (isHuman(MeshA[i][j+1]) ) {
if (drand48() < ZOMBIEATTACK) {
MeshA[i][j+1].type = 'E';
MeshA[i][j+1].exposedPeriod = 0;
}
}
} else if(isExposed(MeshA[i][j])) {
if(MeshA[i][j].exposedPeriod > 2) { // Exposed to Zombie takes 3 days
Occupant z('Z');
MeshA[i][j] = z;
} else {
MeshA[i][j].exposedPeriod++;
}
}
}
}
}
int getHumanPop(Occupant **Mesh){
int humanPop = 0;
for (int i = 1; i <= SIZE; i++) {
for (int j = 1; j <= SIZE; j++) {
if (isHuman(Mesh[i][j]) ) {
humanPop++;
}
}
}
return humanPop;
}
int getZombiePop(Occupant **Mesh){
int zombiePop = 0;
for (int i = 1; i <= SIZE; i++) {
for (int j = 1; j <= SIZE; j++) {
if (isZombie(Mesh[i][j]) ) {
zombiePop++;
}
}
}
return zombiePop;
}
double getBirthRate(Occupant ** Mesh){
double delta = getHumanPop(Mesh)/(SIZE*SIZE - getZombiePop(Mesh));
double birthRate = BIRTHPC/(1-pow(1-delta,4));
return birthRate;
}
void placeBaby(int i1,int j1,int i2, int j2,Occupant ** Mesh, Occupant ** MeshB){
double g = drand48();
char gender;
if (g < 1.0*0.5) {
gender = 'M';
} else {
gender = 'F';
}
Occupant baby('H');
baby.gender = gender;
if (i1 == i2){
if (isEmpty(MeshB[i1-1][j1]) && isEmpty(Mesh[i1-1][j1])){
MeshB[i1-1][j1] = baby;
}
else if (isEmpty(MeshB[i1-1][j2]) && isEmpty(Mesh[i1-1][j2])){
MeshB[i1-1][j2] = baby;
}
else if (isEmpty(MeshB[i1+1][j1]) && isEmpty(Mesh[i1+1][j1])){
MeshB[i1+1][j1] = baby;
}
else if (isEmpty(MeshB[i1+1][j2]) && isEmpty(Mesh[i1+1][j2])){
MeshB[i1+1][j2] = baby;
}
else if (isEmpty(MeshB[i1][min(j1,j2)-1]) && isEmpty(Mesh[i1][min(j1,j2)-1])){
MeshB[i1][min(j1,j2)-1] = baby;
}
else if (isEmpty(MeshB[i1][max(j1,j2)+1]) && isEmpty(Mesh[i1][max(j1,j2)+1])){
MeshB[i1][max(j1,j2)+1]= baby;
}
}
else if (j1==j2){
if (isEmpty(MeshB[i1][j1-1]) && isEmpty(Mesh[i1][j1-1])){
MeshB[i1][j1-1] = baby;
}
else if (isEmpty(MeshB[i2][j1-1]) && isEmpty(Mesh[i2][j1-1])){
MeshB[i2][j1-1] = baby;
}
else if (isEmpty(MeshB[i1][j1+1]) && isEmpty(Mesh[i1][j1+1])){
MeshB[i1][j1+1] = baby;
}
else if (isEmpty(MeshB[i2][j1+1]) && isEmpty(Mesh[i2][j1+1])){
MeshB[i2][j1+1] = baby;
}
else if (isEmpty(MeshB[min(i1,i2)-1][j1]) && isEmpty(Mesh[min(i1,i2)-1][j1])){
MeshB[min(i1,i2)-1][j1]= baby;
}
else if (isEmpty(MeshB[max(i1,i2)+1][j1]) && isEmpty(Mesh[max(i1,i2)+1][j1])){
MeshB[max(i1,i2)+1][j1]= baby;
}
}
}
#if defined(_OPENMP)
void lock(int i, bool *locks, int lockRows) {
for (bool locked = false; locked == false; /*NOP*/) {
#pragma omp critical (LockRegion)
{
bool temp = true;
for(int j = 0; j <= lockRows; j++) {
locked = temp && !locks[i - j] && !locks[i + j];
temp = locked;
}
if (locked) {
for(int j = 0; j <= lockRows; j++) {
locks[i-j] = true;
}
}
}
}
}
void unlock(int i, bool *locks, int lockRows) {
#pragma omp critical (LockRegion)
{
for(int j = 0; j <= lockRows; j++) {
locks[i - j] = false;
}
}
}
#endif
Occupant **CreateMesh(int I, int J) {
Occupant **Mesh = new Occupant *[I];
for (int i = 0; i < I; i++) {
Mesh[i] = new Occupant[J];
for (int j = 0; j < J; j++) {
//Mesh[i][j] = NULL;
}
}
return(Mesh);
}
/*
* Boundary condition check using combination of No Flux & Periodic Approach
*/
Occupant **checkBoundary(Occupant **Mesh) {
Occupant temp;
for (int i = 1; i <= SIZE; i++) {
if(!isEmpty(Mesh[i][0])) {
if (isEmpty(Mesh[i][1])) {
Mesh[i][1] = Mesh[i][0];
}
Mesh[i][0] = temp;
}
if(!isEmpty(Mesh[i][SIZE + 1])) {
if (isEmpty(Mesh[i][SIZE])) {
Mesh[i][SIZE] = Mesh[i][SIZE + 1];
}
Mesh[i][SIZE + 1] = temp;
}
if(!isEmpty(Mesh[0][i])) {
if (isEmpty(Mesh[SIZE][i])) {
Mesh[SIZE][i] = Mesh[0][i];
}
Mesh[0][i] = temp;
}
if(!isEmpty(Mesh[SIZE + 1][i])) {
if (isEmpty(Mesh[1][i])) {
Mesh[1][i] = Mesh[SIZE + 1][i];
}
Mesh[SIZE + 1][i] = temp;
}
}
return(Mesh);
}
void print(Occupant **Mesh, int t) {
int humanPop = 0;
int zombiePop = 0;
int exposedPop = 0;
for (int i = 1; i <= SIZE; i++) {
for (int j = 1; j <= SIZE; j++) {
SDL_LockSurface(screen);
if (isHuman(Mesh[i][j])) {
// int ytimesw = (j-1) *(screen->pitch/BPP);
setpixel(screen , i-1 , j , 255, 0 , 0 );
humanPop++;
} else if (isZombie(Mesh[i][j])){
setpixel(screen , i-1 , j-1 , 0, 255 , 0 );
zombiePop++;
} else if (isExposed(Mesh[i][j])) {
setpixel(screen , i-1 , j-1 , 0, 0 , 255 );
exposedPop++;
}
else
{
setpixel(screen , i-1 , j-1 , 0, 0 , 0 );
}
SDL_UnlockSurface(screen);
}
}
cout<<t<<"\t"<<humanPop<<"\t"<<zombiePop<<"\t"<<exposedPop<<std::endl;
}
int main(int argc, char **argv) {
int i = 0;
atexit(SDL_Quit);
SDL_WM_SetCaption( "Zombies!!!", NULL );
if (screen == NULL)
return 2;
srand48(getpid());
int random;
bool *locks = new bool[SIZE + 2];
for (int i = 0; i < SIZE + 2; i++) locks[i] = false;
Occupant **MeshA = CreateMesh(SIZE + 2, SIZE + 2);
Occupant **MeshB = CreateMesh (SIZE + 2, SIZE + 2);
for (int i = 1; i <= SIZE; i++) {
for (int j = 1; j <= SIZE; j++) {
//#zombies should be reduced
double randNum = drand48();
double ageDistribution = drand48();
if (randNum < 0.01) {
// Modification required to incorporate Male, Female & Zombie differentiation
if(getZombiePop(MeshA) < 2) {
Occupant z('Z');
MeshA[i][j] = z;
}
} else {
if (ageDistribution < 0.182) {
random = rand()%(5475 - 0 + 1) + 0;
}
else if (ageDistribution < 0.61) {
random = rand()%(16060 - 5475 + 1) + 5475;
} else {
random = rand()%(36500 - 16060 + 1) + 16060;
}
if(randNum < 0.05){
char gender = 'M';
Occupant h('H');
h.gender = gender;
h.age = random;
MeshA[i][j] = h;
} else if (randNum < 0.10) {
char gender = 'F';
Occupant h('H');
h.gender = gender;
h.age = random;
MeshA[i][j] = h;
}
}
}
}
std::cout<<"Time"<<"\t"<<"humanPop"<<"\t"<<"zombiePop"<<std::endl;
print(MeshA, 0);
for (int n = 0; n < STEPS; n++) {
#if defined(_OPENMP)
#pragma omp parallel for default(none) shared(MeshA, MeshB, locks, n)
#endif
for (int i = 1; i <= SIZE; i++) {
lock(i, locks, 1);
for (int j = 1; j <= SIZE; j++) {
if (!isEmpty(MeshA[i][j])) {
double move = drand48();
MeshA[i][j].age++ ;
if (move < 1.0*MeshA[i][j].probabilityOfMovement && isEmpty(MeshB[i-1][j]) && isEmpty(MeshA[i-1][j])) {
MeshB[i-1][j] = MeshA[i][j];
} else if (move < 2.0*MeshA[i][j].probabilityOfMovement && isEmpty(MeshB[i+1][j]) && isEmpty(MeshA[i+1][j])) {
MeshB[i+1][j] = MeshA[i][j];
} else if (move < 3.0*MeshA[i][j].probabilityOfMovement && isEmpty(MeshB[i][j-1]) && isEmpty(MeshA[i][j-1])) {
MeshB[i][j-1] = MeshA[i][j];
} else if (move < 4.0*MeshA[i][j].probabilityOfMovement && isEmpty(MeshB[i][j+1]) && isEmpty(MeshA[i][j+1])) {
MeshB[i][j+1] = MeshA[i][j];
} else {
MeshB[i][j] = MeshA[i][j];
}
Occupant temp;
MeshA[i][j] = temp;
}
}
unlock(i, locks, 1);
}
MeshB = checkBoundary(MeshB);
swap(MeshA, MeshB);
print(MeshA, n+1);
/*
cout<< "\n\nAFTER Moving - Mesh A \n";
for(int p = 1; p <= SIZE; p++) {
for(int q = 1; q <= SIZE; q++) {
if(isHuman(MeshA[p][q])) {
cout<<MeshA[p][q].age<<" H | ";
} else if (isZombie(MeshA[p][q])) {
cout<<MeshA[p][q].age<<" Z | ";
}else{
cout<<" | ";
}
}
cout<<"\n";
}
cout<<"\n\n\n\n\n\n\n\n\n";
*/
//Birth
#if defined(_OPENMP)
#pragma omp parallel for default(none) shared(MeshA, MeshB, locks, n)
#endif
for (int i = 1; i <= SIZE; i++) {
lock(i, locks, 2);
for (int j = 1; j <= SIZE; j++) {
if (isReproducible(MeshA[i][j])){
if (isReproducible(MeshA[i][j-1]) && isOppositeGender(MeshA[i][j-1], MeshA[i][j].gender)){
double birth = drand48();
if (birth <= 1.0*getBirthRate(MeshA)){
placeBaby(i,j,i,j-1,MeshA, MeshB);
MeshB[i][j]= MeshA[i][j];
Occupant temp;
MeshA[i][j] = temp;
continue;
}
}
else if (isReproducible(MeshA[i][j+1]) && isOppositeGender(MeshA[i][j+1], MeshA[i][j].gender)){
double birth = drand48();
if (birth <= 1.0*getBirthRate(MeshA)){
placeBaby(i,j,i,j+1,MeshA, MeshB);
MeshB[i][j]= MeshA[i][j];
Occupant temp;
MeshA[i][j] = temp;
continue;
}
}
else if (isReproducible(MeshA[i-1][j]) && isOppositeGender(MeshA[i - 1][j], MeshA[i][j].gender)){
double birth = drand48();
if (birth <= 1.0*getBirthRate(MeshA)){
placeBaby(i,j,i-1,j,MeshA, MeshB);
MeshB[i][j]= MeshA[i][j];
Occupant temp;
MeshA[i][j] = temp;
continue;
}
}
else if (isReproducible(MeshA[i+1][j]) && isOppositeGender(MeshA[i + 1][j], MeshA[i][j].gender)){
double birth = drand48();
if (birth <= 1.0*getBirthRate(MeshA)){
placeBaby(i,j,i+1,j,MeshA, MeshB);
MeshB[i][j]= MeshA[i][j];
Occupant temp;
MeshA[i][j] = temp;
continue;
}
} else {
MeshB[i][j]= MeshA[i][j];
Occupant temp;
MeshA[i][j] = temp;
}
} else {
MeshB[i][j]= MeshA[i][j];
Occupant temp;
MeshA[i][j] = temp;
}
}
unlock(i, locks, 2);
}
MeshB = checkBoundary(MeshB);
swap(MeshA, MeshB);
// Death
for (int i = 1; i <= SIZE; i++) {
for (int j = 1; j <= SIZE; j++) {
double die = drand48();
if (isHuman(MeshA[i][j]) && die < 1.0*DEATHPC) {
Occupant temp;
MeshA[i][j] = temp;
} else if (isZombie(MeshA[i][j]) && MeshA[i][j].age == ZOMBIEAGE ) {
Occupant temp;
MeshA[i][j] = temp;
}
}
}
// Zombification
zombify(MeshA);
SDL_Flip(screen);
/*
cout<< "\n\nMesh A \n";
for(int p = 1; p <= SIZE; p++) {
for(int q = 1; q <= SIZE; q++) {
if(isHuman(MeshA[p][q])) {
cout<<MeshA[p][q].gender<<" H | ";
} else if (isZombie(MeshA[p][q])) {
cout<<MeshA[p][q].age<<" Z | ";
}else if (isExposed(MeshA[p][q])) {
cout<<MeshA[p][q].age<<" E | ";
} else {
cout<<" | ";
}
}
cout<<"\n";
}
cout<<"\n\n\n\n\n\n\n\n\n";
cout<< "\n\nMesh B \n";
for(int p = 1; p <= SIZE; p++) {
for(int q = 1; q <= SIZE; q++) {
if(isHuman(MeshB[p][q])) {
cout<<MeshB[p][q].gender<<" H | ";
} else if (isZombie(MeshB[p][q])) {
cout<<MeshB[p][q].age<<" Z | ";
}else if (isExposed(MeshB[p][q])) {
cout<<MeshB[p][q].age<<" E | ";
} else {
cout<<" | ";
}
}
cout<<"\n";
}
cout<<"\n\n\n\n\n\n\n\n\n";
*/
}
SDL_Quit();
}