-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
461 lines (398 loc) · 9.33 KB
/
main.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
460
461
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <assert.h>
#include <time.h>
/*
#define DEBUG 1
*/
#define LEVEL_ADD_BADDIES 2
#define LEVEL_ADD_TRAPS 2
#define TYPE_PLAYER 1
#define TYPE_BADDIE 2
#define TYPE_TRAP 3
typedef struct {
int x;
int y;
} POSITION;
typedef struct {
char view;
unsigned char type;
POSITION pos;
unsigned char dead;
} PLAYER;
char **board;
PLAYER *baddies;
PLAYER *traps;
int MAX_X;
int MAX_Y;
int MAX_BADDIES;
int MAX_TRAPS;
PLAYER player;
int num_baddies;
int num_traps;
int num_teleports;
int num_zaps;
int level;
void allocate_arrays(int x, int y) {
int i = 0;
MAX_X = x;
MAX_Y = y;
MAX_BADDIES = (MAX_X * MAX_Y / 2);
MAX_TRAPS = (MAX_X * MAX_Y / 2);
board = malloc(MAX_X * sizeof(*board));
for (i = 0; i < MAX_X; i++) {
board[i] = malloc(MAX_Y * sizeof(board[0]));
}
baddies = malloc(MAX_BADDIES * sizeof(PLAYER));
traps = malloc(MAX_TRAPS * sizeof(PLAYER));
}
void deallocate_arrays(void) {
int i = 0;
for (i = 0; i < MAX_X; i++) {
free(board[i]);
}
free(board);
free(baddies);
free(traps);
}
int getch(void) {
int c=0;
struct termios org_opts, new_opts;
int res=0;
//----- store old settings -----------
res=tcgetattr(STDIN_FILENO, &org_opts);
assert(res==0);
//---- set new terminal parms --------
memcpy(&new_opts, &org_opts, sizeof(new_opts));
new_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL);
tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);
c=getchar();
//------ restore old settings ---------
res=tcsetattr(STDIN_FILENO, TCSANOW, &org_opts);
assert(res==0);
return(c);
}
void board_clear(void) {
for (int y = 0; y < MAX_Y; y++) {
for (int x = 0; x < MAX_X; x++) {
board[x][y] = '.';
}
printf("\n");
}
}
unsigned char victory(void) {
int b = 0;
for (b = 0; b < num_baddies; b++) {
if (!baddies[b].dead) {
// still at least one baddie left
return 0;
}
}
return 1;
}
int baddies_count(void) {
int result = 0;
int i = 0;
for (i = 0; i < num_baddies; i++) {
if (!baddies[i].dead) {
result++;
}
}
return result;
}
void board_draw(void) {
int x, y;
// clear screen and home cursor
#ifndef DEBUG
printf("\033[2J"); // clear
printf("\033[H"); // home
#endif
// print top border
printf("+-");
for (x = 0; x < MAX_X; x++) {
printf("--");
}
printf("+\n");
// print board
for (y = 0; y < MAX_Y; y++) {
printf("| ");
for (x = 0; x < MAX_X; x++) {
// if (board[x][y] == player.view) printf("\033[7m");
printf("%c", board[x][y]);
// if (board[x][y] == player.view) printf("\033[0m");
printf(" ");
}
printf("|\n");
}
// print bottom border
printf("+-");
for (x = 0; x < MAX_X; x++) {
printf("--");
}
printf("+\n");
printf("u k i level: %d baddies: %d\n", level, baddies_count());
printf("h l q=quit\n");
printf("n j m t=teleport [%d] z=zap [%d]", num_teleports, num_zaps);
if (player.dead) printf("***** DEAD *****\n");
if (victory()) printf("***** YOU WON *****\n");
}
void board_update(void) {
int i = 0;
board_clear();
// player
board[player.pos.x][player.pos.y] = player.view;
// traps
for (i = 0; i < num_traps; i++) {
board[traps[i].pos.x][traps[i].pos.y] = traps[i].view;
}
// baddies
for (i = 0; i < num_baddies; i++) {
if (!baddies[i].dead) {
board[baddies[i].pos.x][baddies[i].pos.y] = baddies[i].view;
}
}
}
unsigned char occupied(int x, int y) {
unsigned char result = 0;
int i = 0;
if ((player.pos.x == x) && (player.pos.y == y)) {
//occupied by player
return 1;
}
for (i = 0; i < num_baddies; i++) {
if ((baddies[i].pos.x == x) && (baddies[i].pos.y == y)) {
//occupied by a baddie
return 1;
}
}
for (i = 0; i < num_traps; i++) {
if ((traps[i].pos.x == x) && (traps[i].pos.y == y)) {
//occupied by a traps
return 1;
}
}
return result;
}
void players_init(int n_baddies, int n_traps, int n_teleports, int n_zaps) {
int randx, randy;
// player
player.view = '@';
player.pos.x = (int)(MAX_X / 2);
player.pos.y = (int)(MAX_Y / 2);
player.type = TYPE_PLAYER;
// baddies
num_baddies = 0;
while (num_baddies < n_baddies) {
baddies[num_baddies].dead = 0;
baddies[num_baddies].view = 'B';
baddies[num_baddies].type = TYPE_BADDIE;
randx = rand() % MAX_X; randy = rand() % MAX_Y;
while (occupied(randx, randy)) {
printf("generating random player positions x=%d, y=%d\r", randx, randy);
randx = rand() % MAX_X; randy = rand() % MAX_Y;
}
baddies[num_baddies].pos.x = randx;
baddies[num_baddies].pos.y = randy;
num_baddies++;
}
//traps
num_traps = 0;
while(num_traps < n_traps) {
traps[num_traps].view = '%';
traps[num_traps].type = TYPE_TRAP;
randx = rand() % MAX_X; randy = rand() % MAX_Y;
while (occupied(randx, randy)) {
printf("generating random player positions x=%d, y=%d\r", randx, randy);
randx = rand() % MAX_X; randy = rand() % MAX_Y;
}
traps[num_traps].pos.x = randx;
traps[num_traps].pos.y = randy;
num_traps++;
}
//teleports
num_teleports = n_teleports;
//zaps
num_zaps = n_zaps;
}
void trap_new(int x, int y) {
num_traps++;
traps[num_traps - 1].view = '%';
traps[num_traps - 1].type = TYPE_TRAP;
traps[num_traps - 1].pos.x = x;
traps[num_traps - 1].pos.y = y;
}
void player_teleport(void) {
int randx, randy;
randx = rand() % MAX_X;
randy = rand() % MAX_Y;
while (occupied(randx, randy)) {
randx = rand() % MAX_X;
randy = rand() % MAX_Y;
}
player.pos.x = randx;
player.pos.y = randy;
}
void player_zap(void) {
int i;
for (i = 0; i < num_baddies; i++) {
if (!baddies[i].dead) {
if (abs(baddies[i].pos.x - player.pos.x) <= 1) {
if (abs(baddies[i].pos.y - player.pos.y) <= 1) {
baddies[i].dead = 1;
}
}
}
}
}
unsigned char player_move(char key) {
unsigned char handled = 1;
switch (key) {
case ' ': break; // valid move == stand still
case 'h': if (player.pos.x > 0) player.pos.x--; break;
case 'j': if (player.pos.y < MAX_Y - 1) player.pos.y++; break;
case 'k': if (player.pos.y > 0) player.pos.y--; break;
case 'l': if (player.pos.x < MAX_X - 1) player.pos.x++; break;
case 'u': handled = player_move('h'); handled = player_move('k'); break;
case 'i': handled = player_move('l'); handled = player_move('k'); break;
case 'n': handled = player_move('h'); handled = player_move('j'); break;
case 'm': handled = player_move('l'); handled = player_move('j'); break;
case 't':
if (num_teleports > 0) {
player_teleport();
num_teleports--;
}
else {
handled = 0;
}
break;
case 'z':
if (num_zaps > 0) {
player_zap();
num_zaps--;
}
else {
handled = 0;
}
break;
default: handled = 0; break;
}
return(handled);
}
void baddies_move(void) {
int b = num_baddies;
for (b = 0; b < num_baddies; b++) {
if (!baddies[b].dead) {
if (baddies[b].pos.x > player.pos.x) baddies[b].pos.x--;
if (baddies[b].pos.x < player.pos.x) baddies[b].pos.x++;
if (baddies[b].pos.y > player.pos.y) baddies[b].pos.y--;
if (baddies[b].pos.y < player.pos.y) baddies[b].pos.y++;
}
}
}
void collision_detect(void) {
int b = 0;
int b2 = 0;
// check for player collisions with baddies
for (b = 0; b < num_baddies; b++) {
if (!baddies[b].dead) {
if (player.pos.x == baddies[b].pos.x) {
if (player.pos.y == baddies[b].pos.y) {
// collision - player dead
player.dead = 1;
return;
}
}
}
}
// check for player collisions with traps
for (b = 0; b < num_traps; b++) {
if (player.pos.x == traps[b].pos.x) {
if (player.pos.y == traps[b].pos.y) {
// collision - player dead
player.dead = 1;
return;
}
}
}
// check for baddies collisions
for (b = 0; b < num_baddies; b++) {
if (!baddies[b].dead) {
// check other baddies
for (b2 = b+1; b2 < num_baddies; b2++) {
if (!baddies[b2].dead) {
if (baddies[b].pos.x == baddies[b2].pos.x) {
if (baddies[b].pos.y == baddies[b2].pos.y) {
// collision - baddies dead, trap created
baddies[b].dead = 1;
baddies[b2].dead = 1;
// this should be more elegant: don't create a trap if one already exists here
trap_new(baddies[b].pos.x, baddies[b].pos.y);
break;
}
}
}
}
if (!baddies[b].dead) {
// check traps
for (b2 = 0; b2 < num_traps; b2++) {
if (baddies[b].pos.x == traps[b2].pos.x) {
if (baddies[b].pos.y == traps[b2].pos.y) {
// collision with trap
baddies[b].dead = 1;
break;
}
}
}
}
}
}
}
int main(int argc, char**argv) {
char input_key = '\0';
unsigned char move_baddies = 0;
int cx = 25;
int cy = 25;
if (argc == 3) { // get board dimensions from command line
cx = atoi(argv[1]);
cy = atoi(argv[2]);
} else if (argc == 2) {
printf("usage: baddies [x-size y-size]\n");
return(1);
}
// seed the random number generator
srand(time(NULL));
allocate_arrays(cx, cy);
level = 1;
board_clear();
board_draw();
players_init(3, 4, 2, 2);
board_update();
board_draw();
while (1) {
input_key = (char)getch();
if (input_key == 'q') {
deallocate_arrays();
return(1);
}
move_baddies = player_move(input_key);
if (move_baddies) {
baddies_move();
collision_detect();
}
board_update();
board_draw();
if (victory()) {
// level up
level++;
board_clear();
board_draw();
players_init(num_baddies + LEVEL_ADD_BADDIES, num_traps + LEVEL_ADD_TRAPS, level, 2);
board_update();
board_draw();
}
}
return(0);
}