-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcentipede.c
372 lines (293 loc) · 11.8 KB
/
centipede.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
/**********************************************************************
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
//int gameRunning; //defined in globals.h
/**************************************************************/
/* 1 if initial spawn thread run, 0 otherwise*/
int initialRun = true;
void centipedeRun()
{
if (consoleInit(GAME_ROWS, GAME_COLS, GAME_BOARD))
{
/************* MAIN THREAD *************************/
pthread_t screenThread; //Thread to re-draw the screen / perform a refresh using the command in console.c
pthread_t keyThread; //Thread to get input from the keyboard
pthread_t spawnThread; //Thread to handle enemy spawns
gameRunning = true; //set game as running
//initialize cateripllar mutex
wrappedMutexInit(&caterpillarMutex, NULL);
//initialize keyboard mutex
wrappedMutexInit(&keyMutex, NULL);
//create player
playerPtr = spawnPlayer(PLAYER_START_ROW, PLAYER_START_COL, PLAYER_START_LIVES);
//create enemy
enemy * firstEnemy = spawnEnemy(ENEMY_DEFAULT_LENGTH,INITIAL_ENEMY_Y,GAME_COLS,LIST_TYPE_HEAD,ENEMY_SPEED_SLOW,LEFT);
nodeHead =(node*)(malloc(sizeof(node)));
if(nodeHead == NULL) return; //check for malloc failure
nodeHead->e = firstEnemy;
//create upkeep thread
pthread_t upkeepThread = createUpkeepT(&screenMutex);
//create screen thread
wrappedPthreadCreate(&screenThread, NULL, screenRunT, (void*)&screenMutex);
//create keyboard thread
wrappedPthreadCreate(&keyThread,NULL, getKeyT, (void*)playerPtr);
//run spawn thread
wrappedPthreadCreate(&spawnThread, NULL, spawnRunT, (void*)&screenMutex);
/* now started all threads, sleep using condition variable */
//initialize game mutex
wrappedMutexInit(&gameMutex, NULL);
//block the main thread until signal
wrappedMutexLock(&gameMutex);
wrappedCondWait(&gameCondition, &gameMutex);
//when game end is signalled, switch gameRunning to false
gameRunning = false;
wrappedMutexUnlock(&gameMutex);
/* Join all threads */
//disable console while we wait for threads to join
disableConsole(true);
//wait for player thread to end
wrappedPthreadJoin(playerPtr->thread, NULL);
//wait for screen thread to end
wrappedPthreadJoin(screenThread, NULL);
//wait for key thread to end
wrappedPthreadJoin(keyThread,NULL);
//wait for spawn thread to end
//joins enemy threads here
wrappedPthreadJoin(spawnThread,NULL);
//wait for upkeep thread to end
wrappedPthreadJoin(upkeepThread,NULL);
//join enemy bullet and bullet threads
joinExternalThreads();
//free pointers
free(playerPtr);
/*all other memory allocations are freed elsewhere
e.g. joinExternalThreads(), spawnRunT, etc. */
}
//wait for final key press and finish console
finalKeypress();
consoleFinish();
}
void * screenRunT(void * data)
{
pthread_mutex_t * mutex= (pthread_mutex_t *)(data);
while(gameRunning)
{
wrappedMutexLock(mutex);
consoleRefresh(); //draw everything to screen.
wrappedMutexUnlock(mutex);
sleepTicks(PLAYER_ANIM_TICKS); //sleep for given time
}
return NULL;
}
void * getKeyT(void * data)
{
player * p = (player*)(data);
int fd=0; //file descriptor
int selStatus; //status of select
fd_set rdfds; //read file descriptor
struct timeval timeout; //time out value struct
while(gameRunning)
{
//stdin check for input
FD_ZERO(&rdfds);
FD_SET(fd, &rdfds);
//wait for specified TIMEOUT_SEC
timeout.tv_sec = TIMEOUT_SEC;
timeout.tv_usec = TIMEOUT_USEC;
selStatus = select(WRITE_FD, &rdfds, NULL, NULL, &timeout);
if(selStatus != FALSE)
{
//get character input
char c = getchar();
//directional specifications
if(c == UP_KEY ||
c == DOWN_KEY ||
c == LEFT_KEY ||
c == RIGHT_KEY)
{
wrappedMutexLock(&keyMutex);
dir = c;
wrappedMutexUnlock(&keyMutex);
}
else if(c == SPACE_KEY)
{
//Fire player bullet
bullet * bToAdd = spawnBullet(p->row, p->col+1);
bulletList* bL = (bulletList*)(malloc(sizeof(bulletList)));
if(bL == NULL) return NULL; //check for malloc failure
bL->b = bToAdd;
if(bNodeHead == NULL)bNodeHead = bL;
else{
wrappedMutexLock(&bulletMutex);
bulletList * cur = bNodeHead;
//traverse to end of list
while(cur->next != NULL)
{
cur = cur->next;
}
cur->next = bL;
}
wrappedMutexUnlock(&bulletMutex);
}
else if(c == QUIT_KEY)
{
putBanner(QUIT_MSG); //post quit banner
/*SIGNAL (pthread_cond_signal) THE START OF
CLEANUP THREAD (JOINS ALL THREADS, ETC)*/
wrappedMutexLock(&gameMutex);
wrappedCondSignal(&gameCondition); //signal main thread
wrappedMutexUnlock(&gameMutex);
return NULL;
}
}
else //no data for 3 seconds
{
if(!gameRunning)
{
//stop thread
break;
}
}
}
return NULL;
}
void * spawnRunT(void * data)
{
pthread_mutex_t * scrMutex= (pthread_mutex_t *)(data);
int i=0; //amount of ticks requred to match enemy tick rate
enemy * e;
//Spawn thread when game is running
while(gameRunning)
{ //Sleep one tick to check if game is running every tick
sleepTicks(ENEMY_SINGLE_TICK);
//Run if slept for exactly 'ENEMY_SPAWN_RATE' ticks
if(i>ENEMY_SPAWN_RATE){
//INITIAL RUN
if(initialRun){
node * nodeToAdd = (node*)(malloc(sizeof(node)));
wrappedMutexLock(scrMutex);
//spawns enemy
e= spawnEnemy(ENEMY_DEFAULT_LENGTH,INITIAL_ENEMY_Y,GAME_COLS,LIST_TYPE_BODY,ENEMY_SPEED_SLOW,LEFT); //spawn enemy
initialRun=0;
wrappedMutexUnlock(scrMutex);
nodeToAdd->e = e;
//new node is next to head
wrappedMutexLock(&caterpillarMutex);
nodeHead->next = nodeToAdd;
wrappedMutexUnlock(&caterpillarMutex);
i=0;
}
//EVERY OTHER RUNS
else{
node * nodeToAdd = (node*)(malloc(sizeof(node)));
wrappedMutexLock(scrMutex);
e = spawnEnemy(ENEMY_DEFAULT_LENGTH,INITIAL_ENEMY_Y,GAME_COLS,LIST_TYPE_BODY,ENEMY_SPEED_SLOW,LEFT); //spawn enemy
wrappedMutexUnlock(scrMutex);
nodeToAdd->e = e;
node * cur = nodeHead;
wrappedMutexLock(&caterpillarMutex);
//traverse to end of list
while(cur->next != NULL)
{
cur = cur->next;
}
cur->next = nodeToAdd; //enemy is now @ end of list
wrappedMutexUnlock(&caterpillarMutex);
i=0;
}
}
i++;
}
//join and free all enemy threads
node * next = NULL;
node * current = nodeHead;
while (current != NULL)
{
if(current->e != NULL){
wrappedPthreadJoin(current->e->thread, NULL);
free(current->e);
}
next = current->next;
free(current); //free malloc
current = next;
}
return NULL;
}
void joinExternalThreads(){
//join and free all bullet threads
bulletList * nextBL = NULL;
bulletList * currentBL = bNodeHead;
while (currentBL != NULL)
{
if(currentBL->b != NULL){
wrappedPthreadJoin(currentBL->b->thread, NULL);
free(currentBL->b);
}
nextBL = currentBL->next;
free(currentBL);
currentBL = nextBL;
}
//join and free all enemy bullet threads
enemyBulletList * nextEBL = NULL;
enemyBulletList * currentEBL = eBnodeHead;
while (currentEBL != NULL)
{
if(currentEBL->eB != NULL){
wrappedPthreadJoin(currentEBL->eB->thread, NULL);
free(currentEBL->eB);
}
nextEBL = currentEBL->next;
free(currentEBL);
currentEBL = nextEBL;
}
}
void cleanUp(){
deleteEnemyList();
if(bulletHead != NULL) freeBullets(bulletHead);
freeEnemyBullets(enemyBulletHead);
}