-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisting.txt
2543 lines (1985 loc) · 66.1 KB
/
listing.txt
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************
Module: bullet.h
Author: Junseok Lee
Purpose: manages the player bullet thread. Moves the bullets and the
linked list that controls the player bullet.
**********************************************************************/
#ifndef BULLET_H
#define BULLET_H
#include "console.h"
#include "globals.h"
#include "enemy.h"
#include "llist.h"
#include "threadwrappers.h"
#include <pthread.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/select.h>
#include <curses.h>
#include <string.h>
/* Enum, Represents bullet state */
typedef enum bulletState_enum
{
TRANSIT, //bullet is in transit
COLLISION, //bullet colldies with player
DESTROY //bullet done, to be cleaned
} bulletState;
/* Represents player bullet */
typedef struct bullet_struct
{
int startCol; //bullet starting column at player position
int startRow; //bullet starting row at player position
bulletState state; //current state of bullet
bool running; //bullet in motion when true
int row; //bullet's current position (row)
int col; //bullet's current position (column)
int prevRow; //bullet's row in the previous tick
int prevCol; //bullet's column in the previous tick
pthread_t thread; //thread for each bullet
pthread_mutex_t mutex; //mutex for each bullet
struct bullet_struct * next; //next bullet linked list
} bullet;
/* Creates new player bullet. Changes state to TRANSIT,
rows to startRow, col to startCol */
void newPlayerBullet(bullet *b);
/* Moves bullet one tick. Checks if enemy has been hit
(using the enemyHit(enemy * e, bullet * b) method),
and checks if bullet is out of boundaries. */
void playerBulletMove(bullet *b);
/* Spawns player bullet in given row and column,
returns spawned bullet pointer. Initializes enemy bullet
mutex and connects list*/
bullet* spawnBullet(int startRow, int startCol); // player bulllet = 0, enemy bullet = 1
/* Runs bullet thread. Manages bullet state and
bullet movement. */
void *runBulletT(void *data);
/* Redraws the moved bullet. */
void bulletRedraw(bullet *b, bool lock);
/* Checks if enemy caterpillar is hit at
player bullet's current position. */
int enemyHit(enemy * e, bullet * b);
/* Clears bullet from the screen */
void killBullets();
/* Frees bullets from allocation */
void freeBullets(bullet * head);
struct bullet_struct *bulletHead; //Head of player bullet list
pthread_mutex_t bulletMutex; //Player bullet mutex
#endif/**********************************************************************
Module: bulletList.h
Author: Junseok Lee
Contains linked list for enemy bullets, and the enemy bullet head
**********************************************************************/
#ifndef BULLET_LIST_H
#define BULLET_LIST_H
#include "bullet.h"
//represents bullet list
typedef struct bullet_node
{
bullet * b; //a single bullet
struct bullet_node * next; //next bullet
} bulletList;
//enemy bullet head
struct bullet_node *bNodeHead;
#endif/**********************************************************************
Module: centipede.h
Author: Junseok Lee
Purpose: The management of the game engine. Includes a screen
refresh thread, a keyboard thread, an enemy spawn thread and a
main thread.
**********************************************************************/
#ifndef CENTIPEDE_H
#define CENTIPEDE_H
#include "console.h"
#include "centipede.h"
#include "player.h"
#include "enemy.h"
#include "bullet.h"
#include "bulletList.h"
#include "upKeep.h"
#include "threadwrappers.h"
/* Runs the game engine (main thread). Handles
thread initialization, joins, finalization, etc.
Blocks when all threads are initialized and run,
and when signalled, cleans up and ends game */
void centipedeRun();
/* Screen refresh thread run*/
void * screenRunT(void * mutex);
/* Thread run to get keyboard input,
uses the select command */
void * getKeyT(void * data);
/* Thread to run and handle the
enemy spawns. Joins all enemies
when this thread ends. */
void * spawnRunT(void * data);
/* Joins and frees all bullets,
and enemy bullets. */
void joinExternalThreads();
/* Cleans up remaining lists */
void cleanUp();
#endif
/**********************************************************************
Module: console.h
Author: Daniel Rea
Purpose: Draw 2d images on the screen. Also, draw main game image.
**********************************************************************/
#ifndef CONSOLE_H
#define CONSOLE_H
#define _GNU_SOURCE
#include <stdbool.h>
/**************** DRAWING **************************/
/* directions in terms of deltas in x / y dimension */
#define LEFT -1
#define RIGHT 1
#define UP -1
#define DOWN 1
#define SCR_LEFT 0
#define SCR_TOP 0
/* Initialize curses, draw initial gamescreen. Refreshes console to terminal.
Also stores the requested dimensions of the consoe and tests the terminal for the
given dimensions.*/
extern bool consoleInit(int reqHeight, int reqWidth, char *image[]);
/* Draws 2d `image' of `height' rows, at curses coordinates `(row, col)'.
Note: parts of the `image' falling on negative rows are not drawn; each
row drawn is clipped on the left and right side of the game console (note
that `col' may be negative, indicating `image' starts to the left of the
screen and will thus only be partially drawn. Useful for objects that are
half off the screen */
extern void consoleDrawImage(int row, int col, char *image[], int height);
/* Clears a 2d `width'x`height' rectangle with spaces. Upper left hand
corner is curses coordinate `(row,col)'. */
extern void consoleClearImage(int row, int col, int height, int width);
/* Moves cursor to bottom right corner and refreshes. If this is not done,
the curses internal buffer (that you have been drawing to) is not dumped
to screen. */
extern void consoleRefresh(void);
/* turns off all updates. Can be used to prevent the screen refresh from working, e.g., at game end while threads are all catching up.*/
extern void disableConsole(int disabled);
/* Terminates curses cleanly. */
extern void consoleFinish(void);
/* Puts the given banner in the center of the screen */
void putBanner(const char *);
/* Draws the given string at the given location */
void putString(char *, int row, int col, int maxlen);
/* Sleeps the given number of 20ms ticks */
void sleepTicks(int ticks);
/* clears the input buffer and then waits for one more key */
void finalKeypress();
/* gets a timespec that represents the time of one tick */
struct timespec getTimeout(int ticks);
#endif /* CONSOLE_H */
/**********************************************************************
Module: enemyBullet.h
Author: Junseok Lee
Purpose: Manages the enemy bullet on its own thread. Also detects
Collision with the player. Returns a pointer to an enemy bullet.
**********************************************************************/
#ifndef ENEMYBULLET_H
#define ENEMYBULLET_H
#include "console.h"
#include "globals.h"
#include "player.h"
#include "threadwrappers.h"
#include <pthread.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/select.h>
#include <string.h>
/* Enum, Represents an enemy bullet state */
typedef enum enemyBulletState_enum
{
TRANSIT_E, //Enemy bullet in transit
PLAYERHIT, //Enemy bullet hits player
CLEAN //Enemy bullet finishes job
} enemyBulletState;
/* Represents an enemy bullet */
typedef struct enemy_bullet_struct
{
int startCol; //bullet starting column at enemy position
int startRow; //bullet starting column at enemy position
enemyBulletState state; //state of bullet stored in enum
bool running; //bullet is in motion when true
int row; //bullet's position (row)
int col; //bullet's position (columns)
int prevRow; //bullet's previous Row
pthread_t thread; //thread for each enemy bullet
pthread_mutex_t mutex; //mutex for each enemy bullet
struct enemy_bullet_struct * next; //next bullet linked list
} enemyBullet;
/* Creates new enemy bullet. Changes state to TRANSIT_E,
rows to startRow, col to startCol */
void newEnemyBullet(enemyBullet *b);
/* Moves enemy bullet one tick. Checks if player has been hit
(using the shipHit method), and checks if bullet is out
of boundaries. */
void eBulletMove(enemyBullet * b, player * p);
/* Spawns enemy bullet in given row and column,
returns spawned enemy bullet pointer. Initializes enemy
bullet mutex and connects list*/
enemyBullet* spawnEnemyBullet(int startRow, int startCol);
/* Runs Enemy bullet thread. Manages bullet state and
bullet movement. */
void *runEBulletT(void *data);
/* Redraws the moved bullet. */
void enemyBulletRedraw(enemyBullet *b, bool lock);
/* Checks if player ship is hit at enemy bullet's
current position. */
int shipHit(player * p, enemyBullet * b);
/* Redraws the moved bullet. */
void freeEnemyBullets(enemyBullet * head);
struct enemy_bullet_struct *enemyBulletHead; //Head of enemy bullet list
pthread_mutex_t enemyBulletMutex; //Enemy bullet mutex
#endif/**********************************************************************
Module: enemyBulletList.h
Author: Junseok Lee
Contains linked list for enemy bullets, and the enemy bullet head
**********************************************************************/
#ifndef ENEMY_BULLET_LIST_H
#define ENEMY_BULLET_LIST_H
#include "enemyBullet.h"
//represents enmey bullet list
typedef struct enemy_bullet_node
{
enemyBullet * eB; //a single enemy bullet
struct enemy_bullet_node * next; //next enemy bullet
} enemyBulletList;
//enemy bullet head
struct enemy_bullet_node *eBnodeHead;
#endif/**********************************************************************
Module: enemy.h
Author: Junseok Lee
Purpose: manages the enemy caterpillar thread. Enemy bullets are
used (called) by this thread. Handles enemy positioning and enemy
caterpillar draws.
**********************************************************************/
#ifndef ENEMY_H
#define ENEMY_H
#include "console.h"
#include "globals.h"
#include "enemyBullet.h"
#include "threadwrappers.h"
#include "enemyBulletList.h"
#include <pthread.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/select.h>
#include <curses.h>
#include <string.h>
/* Enum, Represents an enemy state */
typedef enum enemyState_enum
{
START_E, //enemy start
LEFT_E, //enemy moving left
RIGHT_E, //enemy moving right
DEAD_E, //enemy dead
WRAPAROUND_L, //enemy wrapping around LHS
WRAPAROUND_R, //enemy wrapping around RHS
SPLIT //enemy splitting
} enemyState;
/* Represents an enemy */
typedef struct enemy_struct
{
int length; //enemy length
int startCol; //enemy starting column
int startRow; //enemy starting row
int index; //list index, 0 = head, 1 = body
int speed; //enemy speed in ticks
int direction; //enemy movement direction LEFT = -1, RIGHT = 1
enemyState state; //state of enemy
enemyState prevState; //enemy's previous state
bool running; //enemy is in motion when true
int row; //enemy's position in row
int col; //enemy's position in column
int tailRow; //the last row of enemy (enemy tail row)
int tailCol; //the last column enemy (enemy tail column)
int animTile; //animation tile
pthread_t thread; //thread for each enemy
pthread_mutex_t mutex; //mutex for each enemy
struct enemy_struct * next; //next enemy in list
} enemy;
struct enemy_struct *head; //head enemy list
/* The thread runs as the enemy */
void *runEnemyT(void *data);
/* Spawns an enemy for the first time, called externally,
returns the newly spawned enemy*/
enemy* spawnEnemy(int length, int startRow, int startCol, int index, int speed, int direction);
/* Moves enemy to the destination row & column*/
void enemyMove(enemy *f, int dRow, int dCol);
/* Draws the redrawn enemy & animates the enemy using its tiles*/
void _enemyRedrawMoved(enemy *p, int tailRow, int tailCol, bool lock);
/* Run when enemy is split */
void enemySplit(enemy * p);
/* Deletes an enemy */
void deleteEnemy(enemy * p);
/* cleans and frees an enemy */
void cleanEnemy();
/* Deletes the enemy list */
void deleteEnemyList();
/* Checks if all enemies are dead */
bool allEnemiesDead();
/* Mutex for caterpillar list */
pthread_mutex_t caterpillarMutex;
#endif/**********************************************************************
Module: globals.h
Author: Junseok Lee
Purpose: Defines and stores globally accessed values and information.
**********************************************************************/
#ifndef GLOBALS_H
#define GLOBALS_H
/**** GAME BOUNDARIES */
#define GAME_ROWS 24
#define GAME_COLS 80
#define TOP_WALL 3
#define FIRST_ROW 0
#define ENEMY_LAST_ROW 14
/**** ENEMY DIMENSIONS */
#define ENEMY_START 75
#define INITIAL_ENEMY_Y 2
#define ENEMY_HEIGHT 2
#define ENEMY_BODY_ANIM_TILES 4
#define ENEMY_DEFAULT_LENGTH 25
#define ENEMY_ANIM_TILES 3
#define ENEMY_ANIM_TICKS 50
#define ENEMY_SPEED_SLOW 20
#define ENEMY_SPEED_FAST 10
#define LIST_TYPE_HEAD 0
#define LIST_TYPE_BODY 2
#define ENEMY_SPAWN_RATE 1000 //enemy spawn speed in 20ms ticks
#define ENEMY_SINGLE_TICK 1
/**** PLAYER DIMENSIONS */
#define PLAYER_START_ROW 20
#define PLAYER_START_COL 42
#define PLAYER_START_LIVES 3
#define PRINT_LIVES_SIZE 2
#define BOTTOM_END 17
#define LEFT_END 0
#define RIGHT_END 77
#define TOP_END 21
#define PLAYER_ANIM_TILES 3
#define PLAYER_ANIM_TICKS 4
#define PLAYER_WIDTH 3
#define PLAYER_HEIGHT 3
/**** BULLET CONFIG */
#define BULLET_SIZE 1
#define PLAYER_BULLET_SPEED 4
#define ENEMY_BULLET_SPEED 3
/**** DIRECTION KEYS */
#define LEFT_KEY 'a'
#define RIGHT_KEY 'd'
#define DOWN_KEY 's'
#define UP_KEY 'w'
#define SPACE_KEY ' '
#define QUIT_KEY 'q'
/**** SELECT TIMEOUT VALUES */
#define TIMEOUT_SEC 3
#define TIMEOUT_USEC 0
#define WRITE_FD 1
/**** MESSAGES */
#define QUIT_MSG "quitter"
#define WIN_MSG "YOU DID IT!"
#define LOSE_MSG "GAME OVER..."
char dir; //keyboard input
int gameRunning; //1 if running, 0 if not running
#endif/**********************************************************************
Module: llist.h
Author: Junseok Lee
Contains linked list for the enemy. Contains the node head.
**********************************************************************/
#ifndef LLIST_H
#define LLIST_H
#include "enemy.h"
/*Represents a node containing an enemy*/
typedef struct Node
{
enemy * e; //single enemy
struct Node * next; //next node in ll
} node;
//linked list node head
struct Node *nodeHead;
#endif/**********************************************************************
Module: player.h
Author: Junseok Lee
Purpose: Manages the player's ship for invaders
**********************************************************************/
#ifndef PLAYER_H
#define PLAYER_H
#include "console.h"
#include "globals.h"
#include "threadwrappers.h"
#include <pthread.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/select.h>
#include <curses.h>
#include <string.h>
/* Enum of player's state */
typedef enum playerState_enum
{
GAME, //game running
DEAD, //player dead
GAMEOVER, //game over
WIN //player win, game end
} playerState;
/* Represents a player */
typedef struct player_struct
{
int startCol; //starting column of player
int startRow; //starting row of player
playerState state; //player's current state: GAME,DEAD,GAMEOVER,WIN
bool running; //current player is running
int lives; //lives the player has
int row; //the player's current row
int col; //the player's current column
int prevRow; //the player's previous row
int prevCol; //the player's previous column
int animTile; //the player animation tile
pthread_t thread; //the player thread
pthread_mutex_t mutex; //the player mutex
} player;
/* The thread runs as the player */
void *runPlayerT(void *data);
/* Spawns the player for the first time, called externally
returns the newly spawned player*/
player* spawnPlayer(int startRow, int startCol, int lives);
/* Moves player to the destination row & column*/
void playerMove(player *f, int dRow, int dCol);
/* removes a life, ends the game if all lives gone */
void killPlayer(player* p);
/* specifies left,right,up,down to playerMove*/
void keyMove(player * p, char direction);
/* Points to the player object*/
player * playerPtr;
#endif/**********************************************************************
Module: threadwrappers.h
Author: Daniel Rea and Junseok Lee
Purpose: error-checking wrappers for pthread functions
For distribution to students. Not all functions implemented.
This is just from my solution with parts removed.
Treat it as a guide. Feel free to implement,
change, remove, etc, in your own solution.
**********************************************************************/
#ifndef THREADWRAPPERS_H
#define THREADWRAPPERS_H
#include <pthread.h>
/* locks mutex, returns status*/
int wrappedMutexLock(pthread_mutex_t *mutex);
/* unlocks mutex, returns status*/
int wrappedMutexUnlock(pthread_mutex_t *mutex);
/* initializes mutex, returns status*/
int wrappedMutexInit(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr);
/* creates thread, returns status*/
int wrappedPthreadCreate(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
/* Join thread by waiting for thread to finish, returns status*/
int wrappedPthreadJoin(pthread_t __th, void **__thread_return);
/* Signals waiting thread with condition*/
int wrappedCondSignal(pthread_cond_t *__cond);
/* Waits until signalled by another thread*/
int wrappedCondWait(pthread_cond_t *__restrict__ __cond, pthread_mutex_t *__restrict__ __mutex);
/* condition used to signal end of wait*/
pthread_cond_t gameCondition;
/* mutex used as to manage the game*/
pthread_mutex_t gameMutex;
#endif /* THREADWRAPPERS_H*/
/**********************************************************************
Module: upKeep.h
Author: Junseok Lee
Purpose: Updates player lives to screen, checks
if there are no enemies left, and does regular cleanup.
**********************************************************************/
#ifndef UPKEEP_H
#define UPKEEP_H
#include "console.h"
#include "player.h"
#include "enemy.h"
#include "bullet.h"
#include "globals.h"
#include "threadwrappers.h"
/* Creates/initializes upkeep thread, returns
created thread */
pthread_t createUpkeepT(pthread_mutex_t * mutex);
/* Runs upkeep thread, check if enemies dead */
void *runUpkeepT(void *data) ;
/* Prints the player's current life on the screen*/
void printLives(pthread_mutex_t * mutex);
#endif
/**********************************************************************
Module: bullet.c
Author: Junseok Lee
Represents a player bullet
**********************************************************************/
#include "bullet.h"
void newPlayerBullet(bullet *b)
{
b->row = b->startRow;
b->col = b->startCol;
b->state = TRANSIT;
}
void playerBulletMove(bullet *b)
{
enemy * e = head; //head enemy
//collide when bullets hits ceiling
if(b->row < TOP_WALL){
wrappedMutexLock(&b->mutex);
b->state = COLLISION;
wrappedMutexUnlock(&b->mutex);
}
//Traverse through enemy list to check if the bullet hit
while(e != NULL)
{
//when player bullet hits a caterpillar
if(enemyHit(e,b))
{
enemy* newEnemy;
node * nodeToAdd = (node*)(malloc(sizeof(node)));
if(nodeToAdd == NULL) return; //check for malloc failure
//set bulllet state to collide
wrappedMutexLock(&b->mutex);
b->state = COLLISION;
wrappedMutexUnlock(&b->mutex);
//save enemy state set it to split
wrappedMutexLock(&e->mutex);
e->prevState = e->state;
e->state = SPLIT;
wrappedMutexUnlock(&e->mutex);
int prevLength = e->length; //enemy previous length
int prevRow = e->row; //enemy previous row
int prevCol = e->col; //enemy previous column
//SPLIT ENEMY MOVING LEFT
if(e->direction == LEFT){
//cut enemy length
wrappedMutexLock(&e->mutex);
e->length = e->length - (b->col - e->col)-1;
wrappedMutexUnlock(&e->mutex);
//move cut enemy back
enemyMove(e,e->row, b->col);
//spawn new enemy in front of the moved old enemy
newEnemy = spawnEnemy(prevLength-(e->length+1), prevRow, prevCol,LIST_TYPE_BODY,ENEMY_SPEED_FAST,LEFT);
}
//SPLIT ENEMY MOVING RIGHT
else if (e->direction == RIGHT){
//cut enemy length
wrappedMutexLock(&e->mutex);
e->length = e->length-(e->col-b->col)-1;
wrappedMutexUnlock(&e->mutex);
enemyMove(e,e->row, b->col); //move cut enemy back
//spawn new enemy in front of the moved old enemy
newEnemy = spawnEnemy(prevLength-(e->length+1), prevRow, prevCol,LIST_TYPE_BODY,ENEMY_SPEED_FAST,RIGHT);
}
nodeToAdd->e = newEnemy;
//ADD NEW ENEMY TO END OF LIST
wrappedMutexLock(&caterpillarMutex);
node * cur = nodeHead;
//traverse to end of list
while(cur->next != NULL)
{
cur = cur->next;
}
cur->next = nodeToAdd; //enemy is now @ end of list
wrappedMutexUnlock(&caterpillarMutex);
break;
}
//move to next enemy on llist
e = e->next;
}
//keep moving up if bullet hasn't collided
if(b->state != COLLISION)
{
wrappedMutexLock(&b->mutex);
b->prevRow = b->row;
b->row--;
wrappedMutexUnlock(&b->mutex);
}
}
int enemyHit(enemy * e, bullet * b){
int bulletCol = b->col;
int success = false;
//enemy going left & bullet hit a part of enemy
bool leftHit = (e->direction == LEFT) && ((bulletCol-(e->length) < (e->col)) && (bulletCol-(e->length) >= (e->col - e->length)) && (b->row == e->row+ENEMY_HEIGHT));
//enemy going right & bullet hit body)
bool rightHit = (e->direction == RIGHT) && (((bulletCol + e->length) > (e->col)) && ((bulletCol+e->length) <= (e->col + e->length)) && (b->row == e->row+ENEMY_HEIGHT));
bool moving = (e->state != DEAD_E && e->state != SPLIT);
if((leftHit || rightHit) && moving)
{
success = true;
}
return success;
}
bullet* spawnBullet(int startRow, int startCol)
{
bullet* b = (bullet*)(malloc(sizeof(bullet)));
b->startCol = startCol;
b->startRow = startRow;
b->running = true;
//set as head if bullet list is undefined
if(bulletHead == NULL){
wrappedMutexLock(&bulletMutex);
bulletHead = b;
wrappedMutexUnlock(&bulletMutex);
}
//add to bullet list
else{
bullet * node = bulletHead;
//traverse to end of list
while(node->next != NULL)
{
node = node->next;
}
//set as last element of list
wrappedMutexLock(&bulletMutex);
node->next = b;
wrappedMutexUnlock(&bulletMutex);
}
//Init mutex
pthread_mutex_init(&b->mutex, NULL);
//create & run bullet thread
wrappedPthreadCreate(&(b->thread), NULL, runBulletT, (void*)b);
return b;
}
void *runBulletT(void *data)
{
bullet* b = (bullet*)data;
newPlayerBullet(b);
while (b->running && gameRunning)
{
switch(b->state)
{
case TRANSIT:
playerBulletMove(b);
bulletRedraw(b, true); //redraw moved bullet, lock = true
break;
case COLLISION:
wrappedMutexLock(&b->mutex);
b->running = false;
wrappedMutexUnlock(&b->mutex);
break;
case DESTROY:
b->running = false;
break;
default:
break;
}
//sleep for next bullet tick
sleepTicks(PLAYER_BULLET_SPEED);
}
return NULL;
}
char * BULLET_BODY[BULLET_SIZE] = {"@"};
void bulletRedraw(bullet *b, bool lock)
{
if(lock)
{
wrappedMutexLock(&b->mutex);
consoleClearImage(b->prevRow, b->col, BULLET_SIZE, BULLET_SIZE);
consoleDrawImage(b->row, b->col, BULLET_BODY, BULLET_SIZE);
wrappedMutexUnlock(&b->mutex);
}
}
void killBullets(){
if(bulletHead != NULL){
bullet * b = bulletHead;
//traverse through bullet list
while (b != NULL){
wrappedMutexLock(&b->mutex);
consoleClearImage(b->row,b->col,1,1);
wrappedMutexUnlock(&b->mutex);
b = b->next;
//free(temp);
}
}
}
void freeBullets(bullet * headB){
bullet * current = headB;
bullet * temp;
//return if head
if( current == NULL || current->next == NULL) return;
wrappedMutexLock(&bulletMutex);
while (current != NULL){
//wrappedPthreadJoin(current->thread,NULL);
temp = current;
current = current->next;
free(temp);
}
bulletHead = NULL; //set bullet head to null
wrappedMutexUnlock(&bulletMutex);
}
/**********************************************************************
Module: centipede.c
Author: Junseok Lee
Purpose: Game engine management.
**********************************************************************/
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>
#include "centipede.h"
/**** Game Board Dimensions */
char *GAME_BOARD[] = {
" Score: Lives:",
"=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-centipiede!=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"",
"",
"",
"",
"",
"",
"",
"" };
/****GLOBALS*/
//Mutex to control screen locking
pthread_mutex_t screenMutex;
pthread_mutex_t keyMutex; //keyboard mutex
/*************GLOBALS from other files*************************/
//player *playerPtr; //defined in player.h
//pthread_mutex_t * caterpillarMutex; //defined in enemy.h
//enemy * head; //defined in enemy.h
//enemy * last; //defined in enemy.h
//pthread_cond_t gameCondition; //defined in threadwrappers.h
//pthread_mutex_t * gameMutex; //defined in threadwrappers.h
//char dir; //defined in globals.h