-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenemy.c
461 lines (378 loc) · 12.5 KB
/
enemy.c
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
/**********************************************************************
Module: enemy.c
Author: Junseok Lee
Represents an enemy caterpillar. Runs on a separate thread.
**********************************************************************/
#include "enemy.h"
//Enemy body diagram
char* ENEMY_BODY[ENEMY_BODY_ANIM_TILES][ENEMY_HEIGHT] =
{
{"|",
";"},
{"|",
","},
{"^",
";"},
{"@",
"="}
};
/********************support functions***************/
/* reset the enemy state to start */
void newEnemy(enemy *p)
{
p->row = p->startRow;
p->col = p->startCol;
p->tailRow = p->startRow + p->length;
p->tailCol = p->startRow - ENEMY_HEIGHT;
p->animTile = 3;
if(p->direction == LEFT)
p->state = LEFT_E;
else
p->state = RIGHT_E;
}
void _enemyRedrawMoved(enemy *p, int tailRow, int tailCol, bool lock)
{
if(!lock)return;
int i;
//ENEMY MOVING LEFT
if(p->direction == LEFT && p->state == LEFT_E)
{
wrappedMutexLock(&p->mutex);
//draw head
consoleDrawImage(p->row, p->col, ENEMY_BODY[ENEMY_ANIM_TILES], ENEMY_HEIGHT);
//draw enemy body
for(i=0; i< (p->length) ;i++){
char** tile = ENEMY_BODY[(p->animTile+i)%ENEMY_ANIM_TILES];
consoleDrawImage(p->row, p->col+i+1, tile, ENEMY_HEIGHT); //draw body (col+i+1 b/c head is drawn)
}
//clear enemy trail
consoleClearImage(p->row, p->col + (p->length), ENEMY_HEIGHT, 1);
wrappedMutexUnlock(&p->mutex);
}
//ENEMY WRAPPING AROUND LEFT SIDE
else if (p->direction == RIGHT && p->state == WRAPAROUND_L){
wrappedMutexLock(&p->mutex);
//ANIMATE REST OF PREVIOUS LINE
for(i=0; i< (p->length-1) ;i++){
char** tile = ENEMY_BODY[(p->animTile+i)%ENEMY_ANIM_TILES];
consoleDrawImage(p->row-2, p->col+i, tile, ENEMY_HEIGHT); //draw body
}
//clear enemy trail of previous line
consoleClearImage(p->row-2, p->col + (p->length), ENEMY_HEIGHT, 1); //put this outside for loop
//ANIMATE NEW LINE
consoleDrawImage(p->row, -1*p->col, ENEMY_BODY[ENEMY_ANIM_TILES], ENEMY_HEIGHT); //draw head
for(i=0; i< (p->length );i++){
char** tile = ENEMY_BODY[(p->animTile+i)%ENEMY_ANIM_TILES];
consoleDrawImage(p->row, -1*p->col-i-1, tile, ENEMY_HEIGHT); //draw body
}
wrappedMutexUnlock(&p->mutex);
}
//ENEMY WRAPPING AROUND RIGHT SIDE
else if (p->direction == LEFT && p->state == WRAPAROUND_R){
wrappedMutexLock(&p->mutex);
//ANIMATE REST OF PREVIOUS LINE
for(i=0; i< (p->length) ;i++){
char** tile = ENEMY_BODY[(p->animTile+i)%ENEMY_ANIM_TILES];
consoleDrawImage(p->row-2, p->col-i-1, tile, ENEMY_HEIGHT); //draw body
}
//clear enemy trail of previous line
consoleClearImage(p->row-2, p->col - p->length, ENEMY_HEIGHT, 1);//put this outside for loop
//ANIMATE NEW LINE
consoleDrawImage(p->row, p->col-(p->col - GAME_COLS)*2, ENEMY_BODY[ENEMY_ANIM_TILES], ENEMY_HEIGHT); //draw head
//draw enemy body
for(i=0; i< (p->length) ;i++){
char** tile = ENEMY_BODY[(p->animTile+i)%ENEMY_ANIM_TILES];
consoleDrawImage(p->row, p->col-(p->col - GAME_COLS)*2 + 1, tile, ENEMY_HEIGHT); //draw body
}
wrappedMutexUnlock(&p->mutex);
}
//ENEMY MOVING RIGHT
else if(p->direction == RIGHT && p->state == RIGHT_E ){
wrappedMutexLock(&p->mutex);
//draw head
consoleDrawImage(p->row, p->col, ENEMY_BODY[ENEMY_ANIM_TILES], ENEMY_HEIGHT);
//draw enemy body
for(i=0; i< (p->length );i++){
char** tile = ENEMY_BODY[(p->animTile+i)%ENEMY_ANIM_TILES];
consoleDrawImage(p->row, p->col-i-1, tile, ENEMY_HEIGHT); //draw body
}
//clear enemy trail of previous line
consoleClearImage(p->row, p->col - p->length*2 + 1, ENEMY_HEIGHT, p->length);
wrappedMutexUnlock(&p->mutex);
}
}
void enemyRedraw(enemy *p, bool lock)
{
_enemyRedrawMoved(p, p->tailRow, p->tailCol, lock);
}
/********************THREAD functions***************/
enemy* spawnEnemy(int len, int startRow, int startCol, int index, int speed, int direction)
{
if (len<5) return NULL;
enemy* p = (enemy*)(malloc(sizeof(enemy)));
if(p == NULL) return p; //check for malloc failure
/* Initialize enemy values*/
p->length = len;
p->startCol = startCol;
p->startRow = startRow;
p->index = index;
p->running = true;
p->speed = speed;
p->direction = direction;
//head enemy
if(index == LIST_TYPE_HEAD){
wrappedMutexLock(&caterpillarMutex);
head = p;
wrappedMutexUnlock(&caterpillarMutex);
}
//remaining enemies
else if (index == LIST_TYPE_BODY){
enemy * node = head;
//if first body
if(head->next == NULL){
wrappedMutexLock(&caterpillarMutex);
head->next = p;
wrappedMutexUnlock(&caterpillarMutex);
}
else{
wrappedMutexLock(&caterpillarMutex);
//traverse to end of list
while(node->next != NULL)
{
node = node->next;
}
node->next = p; //enemy is now @ end of list
wrappedMutexUnlock(&caterpillarMutex);
}
}
//Init mutex
wrappedMutexInit(&p->mutex, NULL);
//create and run thread
wrappedPthreadCreate(&(p->thread), NULL, runEnemyT, (void*)p);
return p;
}
void *runEnemyT(void *data)
{
enemy* p = (enemy*)data;
newEnemy(p); //initialize new enemy
while (p->running && gameRunning)
{
//kill enemies < 5
if(p->length<5){
wrappedMutexLock(&p->mutex);
p->state = DEAD_E;
wrappedMutexUnlock(&p->mutex);
break;
}
switch(p->state)
{
case LEFT_E:
enemyMove(p,p->row, p->col-1); //move left
break;
case RIGHT_E:
enemyMove(p,p->row, p->col+1); //Move right
break;
case WRAPAROUND_L:
enemyMove(p,p->row, p->col-1); //wraparound LHS
break;
case WRAPAROUND_R:
enemyMove(p,p->row, p->col+1); //wraparound RHS
break;
case DEAD_E:
wrappedMutexLock(&p->mutex);
p->running = false;
wrappedMutexUnlock(&p->mutex);
deleteEnemy(p);
return NULL;
break;
case SPLIT:
enemySplit(p);
break;
default:
break;
}
//head reach left end
if(p->col == LEFT_END-1)
{
//enemy is in the last row
if(p->row >= ENEMY_LAST_ROW){
wrappedMutexLock(&p->mutex);
p->state = WRAPAROUND_L;
p->direction = RIGHT;
wrappedMutexUnlock(&p->mutex);
//dont move further down
enemyMove(p, p->row, p->col);
wrappedMutexUnlock(&p->mutex);
}
//otherwise enemy now moving left
else{
wrappedMutexLock(&p->mutex);
p->state = WRAPAROUND_L;
p->direction = RIGHT;
wrappedMutexUnlock(&p->mutex);
enemyMove(p, p->row+ENEMY_HEIGHT, p->col);
}
}
//tail reach end of left end
if(p->col < (0 - p->length)){
wrappedMutexLock(&p->mutex);
p->state = RIGHT_E;
p->col += p->length*2;
wrappedMutexUnlock(&p->mutex);
enemyMove(p, p->row, p->col+ENEMY_HEIGHT);
}
//head reach right end
if(p->col == GAME_COLS+1 && p->direction == RIGHT)
{
//enemy is in the last row
if(p->row >= ENEMY_LAST_ROW){
wrappedMutexLock(&p->mutex);
p->state = WRAPAROUND_R;
p->direction = LEFT;
wrappedMutexUnlock(&p->mutex);
//dont move further down
enemyMove(p, p->row, p->col);
wrappedMutexUnlock(&p->mutex);
}
else{
wrappedMutexLock(&p->mutex);
p->direction = LEFT;
p->state = WRAPAROUND_R;
wrappedMutexUnlock(&p->mutex);
enemyMove(p, p->row+ENEMY_HEIGHT, p->col);
}
}
//tail reach end of right end:
if(p->col > (GAME_COLS + p->length)){
wrappedMutexLock(&p->mutex);
p->state = LEFT_E;
p->col -= p->length*2;
wrappedMutexUnlock(&p->mutex);
enemyMove(p, p->row, p->col-2);
}
//increment animation tile
wrappedMutexLock(&p->mutex);
p->animTile++;
p->animTile %= ENEMY_ANIM_TILES;
wrappedMutexUnlock(&p->mutex);
//redraw moved enemy, lock = true
enemyRedraw(p, true);
//fire bullet at random
if( p->col+1 < GAME_COLS && rand()%p->length == 0){
enemyBullet * eBtoAdd =spawnEnemyBullet(p->row + 1, p->col);
enemyBulletList* eBL = (enemyBulletList*)(malloc(sizeof(enemyBulletList)));
if(eBL == NULL) return NULL; //check for malloc failure
eBL->eB = eBtoAdd;
if(eBnodeHead == NULL)eBnodeHead = eBL;
else{
wrappedMutexLock(&enemyBulletMutex);
enemyBulletList * cur = eBnodeHead;
//traverse to end of list
while(cur->next != NULL)
{
cur = cur->next;
}
cur->next = eBL;
}
wrappedMutexUnlock(&enemyBulletMutex);
}
//sleep according to enemy speed
sleepTicks(p->speed);
}
return NULL;
}
void enemyMove(enemy *f, int dRow, int dCol)
{
wrappedMutexLock(&f->mutex);
//move enemy to specified locations
f->tailCol = dCol + f->length;
f->tailRow = f->row;
f->col = dCol;
f->row = dRow;
wrappedMutexUnlock(&f->mutex);
}
void enemySplit(enemy * p)
{
wrappedMutexLock(&p->mutex);
//switch back to stored previous state
p->state = p->prevState;
wrappedMutexUnlock(&p->mutex);
}
void deleteEnemy(enemy * p)
{
enemy *temp = p;
//LAST ENEMY IN LL
if(p->next == NULL){
free(p);
}
else if(p == head){
head = p-> next;
free(p);
}
else{
p->next = temp->next;
}
}
void cleanEnemy()
{
enemy *current = head;
enemy * prev;
if(current == NULL){
return;
}
//if head is dead
else if (current != NULL && current->state == DEAD_E ){
if(current->next) return;
wrappedMutexLock(&caterpillarMutex);
if(current->next !=NULL)
head = current->next;
free(current);
wrappedMutexUnlock(&caterpillarMutex);
return; //dont kill head, we lose pointer
}
//find not running node
while(current->state != DEAD_E && current != NULL && current->next != NULL){
prev = current;
current = current->next;
}
if(current == NULL || prev == NULL) return;
if(prev->next != NULL || current->next != NULL){
//remove current node
wrappedMutexLock(&caterpillarMutex);
prev->next = current->next;
wrappedMutexUnlock(&caterpillarMutex);
free(current);
}
}
void deleteEnemyList(){
enemy *current = head;
enemy * next = NULL;
if(current == NULL){
return;
}
wrappedMutexLock(&caterpillarMutex);
while (current != NULL)
{
next = current->next;
free(current);
current = next;
}
head = NULL;
wrappedMutexUnlock(&caterpillarMutex);
}
bool allEnemiesDead(){
enemy *current = head;
wrappedMutexLock(&caterpillarMutex);
//only move to next enemy node if current enemy is dead
while (current != NULL && (current->state == DEAD_E || current->running==false))
{
current = current->next;
}
wrappedMutexUnlock(&caterpillarMutex);
if(current == NULL ) return true;
else return false;
}
void joinEnemyBullets(enemyBullet * eB){
wrappedPthreadJoin(eB->thread,NULL);
free(eB);
}