-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreads.c
More file actions
587 lines (555 loc) · 15 KB
/
threads.c
File metadata and controls
587 lines (555 loc) · 15 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
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
/**
* file: threads.c
*
* Utilize C curses library and C POSIX library to create a war game between
* an attacker (computer-controlled) and defender (user-controlled). User
* controls shield to block missiles raining down on city.
*
*/
#define _DEFAULT_SOURCE
#define MAX_DELAY_MS 300
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ncurses.h>
#include <pthread.h>
#include <limits.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
struct Game *game;
int height, width;
int row = 2, col;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
/**
* Represents entire game. Contains data of defender, attacker, and city.
*/
struct Game {
struct Defender *defender;
struct Attacker *attacker;
int *layout;
int size;
int cap;
int tallest;
_Bool gameOver;
};
/**
* Represents defender
*/
struct Defender {
_Bool *gameOver;
char *name;
const char *shield;
int shieldY;
int shieldX;
};
/**
* Represents attacker
*/
struct Attacker {
_Bool *gameOver;
char *name;
int totalMissiles;
};
/**
* Represents a missile
*/
struct Missile {
int x;
int y;
char c;
};
/**
* Free mem alloc'd for defender
*
* @param defender pointer to Defender to free
*/
void destroyDefender(struct Defender *defender) {
if (defender) {
if (defender->name) free(defender->name);
free(defender);
}
}
/**
* Free mem alloc'd for attacker
*
* @param attacker pointer to Attacker to free
*/
void destroyAttacker(struct Attacker *attacker) {
if (attacker) {
if (attacker->name) free(attacker->name);
free(attacker);
}
}
/**
* Free mem alloc'd for game
*
* @param game pointer to Game to free
*/
void destroyGame(struct Game *game) {
if (game) {
if (game->layout) free(game->layout);
if (game->defender) destroyDefender(game->defender);
if (game->attacker) destroyAttacker(game->attacker);
free(game);
}
}
/**
* Convert string to integer
*
* @param str string to convert
* @param val pointer to int storing result of function
* @return 0 unsuccessful conversion
* 1 successful conversion
*/
int strInt(char *str, int *val) {
int sum = 0;
for (size_t i = 0; i < strlen(str); i++) {
if (i == 0 && str[i] == '-') continue;
int digit = str[i] - '0';
if (digit < 0 || digit > 9) return 0;
if (sum != 0 && (INT_MAX - digit) / sum < 10) return 0;
sum = sum * 10 + digit;
}
if (str[0] == '\0' || (str[0] == '-' && str[1] == '\0')) return 0;
*val = (str[0] == '-') ? -sum: sum;
return 1;
}
/**
* Process config file and store data in Game struct
*
* @param filename the config file
* @return created Game struct
*/
struct Game *createGame(char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
perror(filename);
exit(EXIT_FAILURE);
}
struct Game *game = malloc(sizeof *game);
if (game == NULL) {
perror("createGame");
exit(EXIT_FAILURE);
}
struct Defender *defender = malloc(sizeof *defender);
if (defender == NULL) {
perror("createGame");
exit(EXIT_FAILURE);
}
struct Attacker *attacker = malloc(sizeof *attacker);
if (attacker == NULL) {
perror("createGame");
exit(EXIT_FAILURE);
}
game->gameOver = 0;
game->defender = defender;
game->attacker = attacker;
game->cap = 10;
game->size = 0;
game->layout = malloc(sizeof *game->layout * game->cap);
if (game->layout == NULL) {
perror("createGame");
exit(EXIT_FAILURE);
}
game->tallest = 0;
defender->gameOver = &game->gameOver;
defender->name = NULL;
defender->shield = "#####";
attacker->gameOver = &game->gameOver;
attacker->name = NULL;
int line = 0;
char *str = NULL;
size_t len = 0;
ssize_t char_read;
while ((char_read = getline(&str, &len, fp)) > 0) {
if (str[0] == '#') continue;
str[strlen(str) - 1] = '\0'; // strip '\n'
if (line == 0) defender->name = strndup(str, 80);
else if (line == 1) attacker->name = strndup(str, 80);
else if (line == 2) {
int retval = strInt(str, &attacker->totalMissiles);
if (retval == 0 || attacker->totalMissiles < 0) {
if (retval == 0) fprintf(stderr,
"Error: missing missile specification.\n");
else fprintf(stderr, "Error: missile specification < 0.\n");
free(str);
fclose(fp);
destroyGame(game);
exit(EXIT_FAILURE);
}
if (attacker->totalMissiles == 0) attacker->totalMissiles = -1;
} else {
char *token = strtok(str, " ");
while (token != NULL) {
if (game->size >= game->cap - 1) {
game->cap *= 2;
game->layout = realloc(game->layout,
sizeof *game->layout * game->cap);
if (game->layout == NULL) {
perror("createGame");
exit(EXIT_FAILURE);
}
}
if (strInt(token, game->layout + game->size)) {
if (game->layout[game->size] > game->tallest) {
game->tallest = game->layout[game->size];
}
game->size++;
}
token = strtok(NULL, " ");
}
}
line++;
}
if (str != NULL) free(str);
fclose(fp);
if (char_read == -1 && (errno == EINVAL || errno == ENOMEM)) {
perror("createGame");
exit(EXIT_FAILURE);
}
if (line < 4) {
if (line == 0) fprintf(stderr, "Error: missing defender name.\n");
else if (line == 1) fprintf(stderr, "Error: missing attacker name.\n");
else if (line == 2) fprintf(stderr,
"Error: missing missile specification.\n");
else fprintf(stderr, "Error: missing city layout.\n");
destroyGame(game);
exit(EXIT_FAILURE);
}
return game;
}
/**
* Initialize display for city and defender (shield)
*
* @param game container includes information about city layout
*/
void initDisplay(struct Game *game) {
int prev = 2;
for (int i = 0; i < width; i++) {
int curr = (i > game->size - 1) ? 2: game->layout[i];
if (curr > 2) {
if (curr == prev) {
mvinsch(height - curr, i, '_');
refresh();
} else if (curr < prev) {
if (mvinch(height - prev, i - 1) == '_') {
mvdelch(height - prev, i - 1);
refresh();
for (int j = height - prev + 1; j <= height - 2; j++) {
mvinsch(j, i - 1, '|');
refresh();
}
}
mvinsch(height - curr, i, '_');
refresh();
} else if (curr > prev) {
for (int j = height - curr + 1; j <= height - 2; j++) {
mvinsch(j, i, '|');
refresh();
}
}
} else if (curr == 1 || curr == 2) {
if (prev > 2 && mvinch(height - prev, i - 1) == '_') {
mvdelch(height - prev, i - 1);
refresh();
for (int j = height - prev + 1; j <= height - 2; j++) {
mvinsch(j, i - 1, '|');
refresh();
}
}
mvinsch(height - curr, i, '_');
refresh();
}
prev = curr;
}
game->defender->shieldY = height -
((game->tallest < 2) ? 2: game->tallest) - 2;
game->defender->shieldX = width / 2 - 2; // width >= 4
for (size_t i = 0; i < strlen(game->defender->shield); i++) {
mvinsch(game->defender->shieldY, game->defender->shieldX, '#');
refresh();
}
}
/**
* Display message on curses window
*
* @param str message to display
* @param r row of curses window to start message
* @param c col of curses window to start message
* @param b whether or not to skip a line
* @return 0 if unable to write message
* 1 if successful
*/
int displayMessage(char *str, int *r, int *c, _Bool b) {
int oldr = *r;
int oldc = *c;
if (*r >= height || *r < 0) return 0;
while (*c == 0 &&
mvinch(*r, *c) != ' ' && mvinch(*r, *c) != '|') {
if (*r == 0 || *r == 1) break;
if (++(*r) >= height) {
*r = oldr;
return 0;
}
}
if (*r != 0 && *r != 1 && b && *r > 0 && mvinch(*r - 1, 0) != ' ') {
if (++(*r) >= height) {
*r = oldr;
return 0;
}
}
for (size_t i = 0; i < strlen(str); i++) {
if (*c / width + *r >= height) {
*r = oldr;
*c = oldc;
return 0;
}
mvdelch(*c / width + *r, *c % width);
mvinsch(*c / width + *r, *c % width, str[i]);
*c += 1;
refresh();
}
*r += *c / width;
return 1;
}
/**
* Function for defense thread, controls defender.
*
* @param defender defender to control
* @return NULL
*/
void *startDef(void *defender) {
struct Defender *ndefender = defender;
_Bool b = 1;
flushinp();
while (!*ndefender->gameOver || b) {
int c = getch();
if (c == 'q') {
pthread_mutex_lock(&mutex);
*ndefender->gameOver = 1;
b = 0;
pthread_mutex_unlock(&mutex);
} else if (c == KEY_LEFT) {
pthread_mutex_lock(&mutex);
if (ndefender->shieldX > 0) {
mvdelch(ndefender->shieldY, ndefender->shieldX + 4);
mvinsch(ndefender->shieldY, ndefender->shieldX + 4, ' ');
refresh();
ndefender->shieldX--;
for (int i = 0; i < 5; i++) {
mvdelch(ndefender->shieldY, ndefender->shieldX + i);
mvinsch(ndefender->shieldY, ndefender->shieldX + i, '#');
refresh();
}
}
pthread_mutex_unlock(&mutex);
} else if (c == KEY_RIGHT) {
pthread_mutex_lock(&mutex);
if (ndefender->shieldX < width - 5) {
mvdelch(ndefender->shieldY, ndefender->shieldX);
mvinsch(ndefender->shieldY, ndefender->shieldX, ' ');
refresh();
ndefender->shieldX++;
for (int i = 0; i < 5; i++) {
mvdelch(ndefender->shieldY, ndefender->shieldX + i);
mvinsch(ndefender->shieldY, ndefender->shieldX + i, '#');
refresh();
}
}
pthread_mutex_unlock(&mutex);
}
}
pthread_mutex_lock(&mutex);
if (!displayMessage("The ", &row, &col, 1)) {
int row_temp = 1;
displayMessage("The ", &row_temp, &col, 0);
if (row_temp > row) row = row_temp;
}
if (!displayMessage(ndefender->name, &row, &col, 0)) {
int row_temp = 1;
displayMessage(ndefender->name, &row_temp, &col, 0);
if (row_temp > row) row = row_temp;
}
if (!displayMessage(" defense has ended.", &row, &col, 0)) {
int row_temp = 1;
displayMessage(" defense has ended.", &row_temp, &col, 0);
if (row_temp > row) row = row_temp;
}
col = 0;
pthread_mutex_unlock(&mutex);
return NULL;
}
/**
* Function for a missile thread, controls a missile.
*
* @param missile missile to control
* @return NULL
*/
void *launchMissile(void *missile) {
struct Missile *nmissile = missile;
pthread_mutex_lock(&mutex);
char prevc = (mvinch(nmissile->y, nmissile->x) == '|') ?
' ': mvinch(nmissile->y, nmissile->x);
mvdelch(nmissile->y, nmissile->x);
mvinsch(nmissile->y, nmissile->x, nmissile->c);
pthread_mutex_unlock(&mutex);
while (nmissile->c != '*') {
usleep(rand() % MAX_DELAY_MS * 1000);
pthread_mutex_lock(&mutex);
if (nmissile->y < height) mvdelch(nmissile->y, nmissile->x);
if (nmissile->y < height) mvinsch(nmissile->y, nmissile->x, prevc);
refresh();
nmissile->y++;
char c = (nmissile->y >= height) ?
' ': mvinch(nmissile->y, nmissile->x);
if (c == '#' ||
(c == '*' && nmissile->y < height - game->tallest)) {
nmissile->c = '*';
} else if (nmissile->y == height - ((nmissile->x > game->size - 1) ?
2: game->layout[nmissile->x]) + 1) {
nmissile->c = '*';
if (nmissile->x < game->size &&
game->layout[nmissile->x] != 2) {
game->layout[nmissile->x]--;
}
} else if (c == '|' ||
c == '_' ||
c == '?' ||
c == '*') {
prevc = ' ';
} else {
prevc = c;
}
if (nmissile->y < height) {
mvdelch(nmissile->y, nmissile->x);
}
if (nmissile->y < height) {
mvinsch(nmissile->y, nmissile->x, nmissile->c);
}
if (nmissile->c == '*' && nmissile->y <= height) {
mvdelch(nmissile->y - 1, nmissile->x);
mvinsch(nmissile->y - 1, nmissile->x, '?');
}
refresh();
pthread_mutex_unlock(&mutex);
if (nmissile->y > height) break;
}
return NULL;
}
/**
* Function for attack thread, controls attacker.
*
* @param attacker attacker to control
* @return NULL
*/
void *startAtk(void *attacker) {
struct Attacker *nattacker = attacker;
int packetSize = (width > 32) ? 8: ((width / 4 == 0) ? width: width / 4);
struct Missile *missiles = calloc(packetSize, sizeof *missiles);
if (missiles == NULL) {
int row_tmp = 1;
pthread_mutex_lock(&mutex);
displayMessage("startAtk: ", &row_tmp, &col, 0);
displayMessage(strerror(errno), &row_tmp, &col, 0);
if (row_tmp > row) row = row_tmp;
col = 0;
*nattacker->gameOver = 1;
pthread_mutex_unlock(&mutex);
}
pthread_t tids[packetSize];
while (!*nattacker->gameOver) {
int i = 0;
while (i < packetSize) {
usleep(rand() % (MAX_DELAY_MS * 3) * 1000);
missiles[i].y = 2;
missiles[i].x = rand() % width;
missiles[i].c = '|';
pthread_create(tids + i, NULL, launchMissile, missiles + i);
i++;
if (*nattacker->gameOver ||
((nattacker->totalMissiles > 0) ?
(--nattacker->totalMissiles == 0): 0)) {
pthread_mutex_lock(&mutex);
*nattacker->gameOver = 1;
pthread_mutex_unlock(&mutex);
break;
}
}
for (int n = 0; n < i; n++) {
pthread_join(tids[n], NULL);
}
}
pthread_mutex_lock(&mutex);
if (!displayMessage("The ", &row, &col, 1)) {
int row_temp = 1;
displayMessage("The ", &row_temp, &col, 0);
if (row_temp > row) row = row_temp;
}
if (!displayMessage(nattacker->name, &row, &col, 0)) {
int row_temp = 1;
displayMessage(nattacker->name, &row_temp, &col, 0);
if (row_temp > row) row = row_temp;
}
if (!displayMessage(" attack has ended.", &row, &col, 0)) {
int row_temp = 1;
displayMessage(" attack has ended.", &row_temp, &col, 0);
if (row_temp > row) row = row_temp;
}
col = 0;
pthread_mutex_unlock(&mutex);
free(missiles);
return NULL;
}
/**
* Entry function for program. Creates game, runs game,
* and handle game termination
*
* @param argc number of commandline arguments
* @param argv commandline arguments
* @return 0 on successful execution
*/
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: threads config-file\n");
exit(EXIT_FAILURE);
}
game = createGame(argv[1]);
initscr();
cbreak();
noecho();
keypad(stdscr, 1);
getmaxyx(stdscr, height, width);
if (height - ((game->tallest < 2) ? 2: game->tallest) - 2 - 1 - 2 < 0) {
endwin();
fprintf(stderr,
"Error: runtime terminal height (%d) shorter than layout.\n",
height);
destroyGame(game);
exit(EXIT_FAILURE);
}
srand(time(NULL));
initDisplay(game);
pthread_mutex_lock(&mutex);
int row_temp = 0;
displayMessage("Enter 'q' to quit at end of attack, or control-C",
&row_temp, &col, 0);
col = 0;
pthread_mutex_unlock(&mutex);
pthread_t defTID, atkTID;
pthread_create(&defTID, NULL, startDef, game->defender);
pthread_create(&atkTID, NULL, startAtk, game->attacker);
pthread_join(defTID, NULL);
pthread_join(atkTID, NULL);
if (!displayMessage("hit enter to close...", &row, &col, 0)) {
row_temp = 1;
displayMessage("hit enter to close...", &row_temp, &col, 0);
if (row_temp > row) row = row_temp;
}
col = 0;
flushinp();
while (getch() != '\n');
endwin();
destroyGame(game);
return 0;
}