-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.h
More file actions
632 lines (566 loc) · 19.5 KB
/
program.h
File metadata and controls
632 lines (566 loc) · 19.5 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
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
#define BLANK ' '
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curses.h>
#include <time.h>
#include <unistd.h>
#include <stdbool.h>
#include <math.h>
//need to fix collision of enemy with next level hitbox
//hitboxes
char player[3][5] = {{
'<',
'-',
'-',
'-',
'>',
},
{
'<',
'-',
'-',
'-',
'>',
},
{
'<',
'-',
'-',
'-',
'>',
}
};
//if player hits V
bool colision_check(const int rows, char field[][rows], char undiscovered[][rows])
{
const int cols = 24;
for (int y = 0; y < cols; y++)
{
for (int x = 0; x < rows; x++)
{
if (field[y][x] == '<' || field[y][x] == '>' || field[y][x] == '-') //next level
{
if (undiscovered[y][x] == 'V')
{
return true;
}
}
}
}
return false;
}
int colision_x(const int rows, char field[][rows], char undiscovered[][rows])
{
const int cols = 24;
int counter = 20;
for (int y = 0; y < cols; y++)
{
for (int x = 0; x < rows; x++)
{
if (undiscovered[y][x] == '.') //if hit by X
{
counter --;
}
}
}
return counter;
}
//user input
void arrow_up(const int rows, char field[][rows], char undiscovered[][rows], int y_axis, int x_axis)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 5; j++)
{
if (field[21 - y_axis + i][(45 / 2) + x_axis - 2 + j] == 'X') //if player hits X
{
if (undiscovered[21 - y_axis + i][(45 / 2) + x_axis - 2 + j] == 'X')
{
undiscovered[21 - y_axis + i][(45 / 2) + x_axis - 2 + j] = '.';
}
}
field[21 - y_axis + i][(45 / 2) + x_axis - 2 + j] = player[i][j]; //set new position
do // !!!!!!!!!!!!!!!!!!!!!!!!!!!!! vykonat len raz
{
field[21 - (y_axis - 1) + i][(45 / 2) + x_axis - 2 + j] = ' '; //remove previous position
} while (y_axis > 999);
refresh();
}
}
}
void arrow_down(const int rows, char field[][rows], char undiscovered[][rows], int y_axis, int x_axis)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 5; j++)
{
if (field[21 - y_axis + i][(45 / 2) + x_axis - 2 + j] == 'X') //if player hits X
{
if (undiscovered[21 - y_axis + i][(45 / 2) + x_axis - 2 + j] == 'X')
{
undiscovered[21 - y_axis + i][(45 / 2) + x_axis - 2 + j] = '.';
}
}
field[21 - y_axis + i][(45 / 2) + x_axis - 2 + j] = player[i][j]; //set new position
do // !!!!!!!!!!!!!!!!!!!!!!!!!!!!! vykonat len raz
{
field[21 - (y_axis + 1)][(45 / 2) + x_axis - 2] = ' '; //remove previous position
} while (y_axis > 999);
refresh();
}
}
}
void arrow_left(const int rows, char field[][rows], char undiscovered[][rows], int y_axis, int x_axis)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 5; j++)
{
if (field[21 - y_axis + i][(45 / 2) + x_axis - 2 + j] == 'X') //if player hits X
{
if (undiscovered[21 - y_axis + i][(45 / 2) + x_axis - 2 + j] == 'X')
{
undiscovered[21 - y_axis + i][(45 / 2) + x_axis - 2 + j] = '.';
}
}
field[21 - y_axis + i][(45 / 2) - 2 + x_axis + j] = player[i][j]; //set new position
do // !!!!!!!!!!!!!!!!!!!!!!!!!!!!! vykonat len raz
{
field[21 - (y_axis) + i][(45 / 2) + x_axis + 1 - 2 + j] = ' '; //remove previous position
} while (x_axis > 999);
refresh();
}
}
}
void arrow_right(const int rows, char field[][rows], char undiscovered[][rows], int y_axis, int x_axis)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 5; j++)
{
if (field[21 - y_axis + i][(45 / 2) + x_axis - 2 + j] == 'X') //if player hits X
{
if (undiscovered[21 - y_axis + i][(45 / 2) + x_axis - 2 + j] == 'X')
{
undiscovered[21 - y_axis + i][(45 / 2) + x_axis - 2 + j] = '.';
}
}
field[21 - y_axis + i][(45 / 2) - 2 + x_axis + j] = player[i][j]; //set new position
do // !!!!!!!!!!!!!!!!!!!!!!!!!!!!! vykonat len raz
{
field[21 - (y_axis) + i][(45 / 2) + x_axis - 1 - 2] = ' '; //remove previous position
} while (x_axis > 999);
refresh();
}
}
}
void behind_fog(const int rows, char undiscovered[][rows], char field[][rows])
{
const int cols = 24;
for (int y = 0; y < cols; y++)
{
for (int x = 0; x < rows; x++)
{
if (field[y][x] == BLANK)
{
field[y][x] = undiscovered[y][x];
}
refresh();
}
}
}
//field of view
void field_of_view(const int rows, char field[][rows], int y_axis, int x_axis)
{
for (int i = 1; i < 4; i++) //left-right
{
for (int j = 1; j < 4; j++)
{
long int up = 0;
up = (45 / 2) + x_axis - 2 - j;
if (up < 0 || up >= 37) //to correct movement outsice of field
{
if (up < 0)
{
field[21 - y_axis - 1 + i][(45 / 2) + x_axis + 6 - j] = BLANK; //Fov Right
}
else
{
field[21 - y_axis - 1 + i][(45 / 2) + x_axis - 2 - j] = BLANK; //FoV left
}
}
else if (up >= 0 && up < 37) //basic main movement
{
field[21 - y_axis - 1 + i][(45 / 2) + x_axis - 2 - j] = BLANK; //FoV left
field[21 - y_axis - 1 + i][(45 / 2) + x_axis + 6 - j] = BLANK; //Fov Right
}
refresh();
}
}
for (int i = 1; i < 3; i++) //up-down
{
for (int j = 1; j < 6; j++)
{
field[21 - y_axis - 3 + i][(45 / 2) + x_axis + 3 - j] = BLANK; //FoV top
field[21 - y_axis + 2 + i][(45 / 2) + x_axis + 3 - j] = BLANK; //FoV down
refresh();
}
}
}
//draw enemy + enemy functions
void discover_enemy(const int rows, char field[][rows], char undiscovered[][rows])
{
const int cols = 24;
int counter = 0;
while (counter < 5)
{
if (colision_check(rows, field, undiscovered) == true)
{
return;
}
for (int y = 0; y < cols; y++) //from down-left
{
for (int x = 0; x < rows; x++)
{
if (field[y][x] == 'V')
{
if (undiscovered[y][x + 1] == 'V')
{
field[y][x + 1] = undiscovered[y][x + 1];
if (undiscovered[y - 1][x] == 'V')
{
field[y - 1][x] = undiscovered[y - 1][x];
}
else if (undiscovered[y + 1][x] == 'V')
{
field[y + 1][x] = undiscovered[y + 1][x];
}
}
if (undiscovered[y][x - 1] == 'V')
{
field[y][x - 1] = undiscovered[y][x - 1];
if (undiscovered[y - 1][x] == 'V')
{
field[y - 1][x] = undiscovered[y - 1][x];
}
else if (undiscovered[y + 1][x] == 'V')
{
field[y + 1][x] = undiscovered[y + 1][x];
}
}
if (undiscovered[y - 1][x] == 'V')
{
field[y - 1][x] = undiscovered[y - 1][x];
if (undiscovered[y - 1][x] == 'V')
{
field[y - 1][x] = undiscovered[y - 1][x];
}
else if (undiscovered[y + 1][x] == 'V')
{
field[y + 1][x] = undiscovered[y + 1][x];
}
}
if (undiscovered[y + 1][x] == 'V')
{
field[y + 1][x] = undiscovered[y + 1][x];
if (undiscovered[y - 1][x] == 'V')
{
field[y - 1][x] = undiscovered[y - 1][x];
}
else if (undiscovered[y + 1][x] == 'V')
{
field[y + 1][x] = undiscovered[y + 1][x];
}
}
}
if (field[y][x] == 'V')
{
if (undiscovered[y][x + 1] == BLANK || undiscovered[y][x + 1] == '*') //block right path
{
if (field[y][x + 1] != BLANK)
{
for (int i = 1; i < 6; i++)
{
if (x + i >= 0 && x + i <= 44)
{
if (undiscovered[y][x + i] != 'V' && field[y][x + i] == '?')
{
undiscovered[y][x + i] = 'X';
}
if (field[y][x + i] == '?')
{
field[y][x + i] = undiscovered[y][x + i];
}
}
}
}
}
if (undiscovered[y][x - 1] == BLANK || undiscovered[y][x + 1] == '*') //block left path
{
if (field[y][x - 1] != BLANK)
{
for (int i = 1; i < 6; i++)
{
if (x - i >= 0 && x + i <= 44)
{
if (undiscovered[y][x - i] != 'V' && field[y][x - i] == '?')
{
undiscovered[y][x - i] = 'X';
}
if (field[y][x - i] == '?')
{
field[y][x - i] = undiscovered[y][x - i];
}
}
}
}
}
if (undiscovered[y + 1][x] == BLANK || undiscovered[y][x + 1] == '*') //block up path
{
if (field[y + 1][x] != BLANK)
{
for (int i = 1; i < 4; i++)
{
if (undiscovered[y + i][x] != 'V' && field[y + i][x] == '?')
{
undiscovered[y + i][x] = 'X';
}
if (field[y + i][x] == '?')
{
field[y + i][x] = undiscovered[y + i][x];
}
}
}
}
if (undiscovered[y - 1][x] == BLANK || undiscovered[y][x + 1] == '*') //block down path
{
if (field[y - 1][x] != BLANK)
{
for (int i = 1; i < 4; i++)
{
if (undiscovered[y - i][x] != 'V' && field[y - i][x] == '?')
{
undiscovered[y - i][x] = 'X';
}
if (field[y - i][x] == '?')
{
field[y - i][x] = undiscovered[y - i][x];
}
}
}
}
}
}
}
behind_fog(rows, undiscovered, field);
counter++;
}
}
//draw map
void draw(const int rows, char field[][rows], char undiscovered[][rows])
{
if (colision_check(rows, field, undiscovered) == true)
{
return;
}
discover_enemy(rows, field, undiscovered);
behind_fog(rows, undiscovered, field);
const int cols = 24;
for (int y = 0; y < cols; y++)
{
for (int x = 0; x < rows; x++)
{
mvprintw(y + LINES / 2 - 12, x + COLS / 2 - 22, "%c", field[y][x]);
if (field[y][x] == 'X')
{
attron(COLOR_PAIR(2));
mvprintw(y + LINES / 2 - 12, x + COLS / 2 - 22, "%c", field[y][x]);
attroff(COLOR_PAIR(2));
}
if (field[y][x] == '.')
{
attron(COLOR_PAIR(2));
mvprintw(y + LINES / 2 - 12, x + COLS / 2 - 22, "%c", field[y][x]);
attroff(COLOR_PAIR(2));
}
if (field[y][x] == 'V')
{
attron(COLOR_PAIR(1));
mvprintw(y + LINES / 2 - 12, x + COLS / 2 - 22, "%c", field[y][x]);
attroff(COLOR_PAIR(1));
}
if (field[y][x] == '?')
{
attron(COLOR_PAIR(4));
mvprintw(y + LINES / 2 - 12, x + COLS / 2 - 22, "%c", field[y][x]);
attroff(COLOR_PAIR(4));
}
refresh();
}
}
}
//if player touches * = next level
bool level_reach(const int rows, char field[][rows], char undiscovered[][rows])
{
const int cols = 24;
for (int y = 0; y < cols; y++)
{
for (int x = 0; x < rows; x++)
{
if (undiscovered[y][x] == '*') //next level
{
if (field[y][x] == '<' || field[y][x] == '>' || field[y][x] == '-')
{
return true;
}
}
}
}
return false;
}
//player movement
void movement(const int rows, char field[][rows], int y_axis, int x_axis, int c, char undiscovered[][rows])
{
do
{
mvprintw(45,80,"You have %d lives left!", colision_x(rows,field,undiscovered));
refresh();
if (colision_x(rows,field,undiscovered) <=0)
{
return;
}
if ((colision_check(rows, field, undiscovered) == true))
{
return;
}
c = getch();
switch (c)
{
case KEY_LEFT:
if (x_axis < 21 && x_axis > -21)
{
if (x_axis == -20)
{
x_axis = -19;
}
x_axis -= 1;
arrow_left(rows, field, undiscovered, y_axis, x_axis);
field_of_view(rows, field, y_axis, x_axis);
draw(rows, field, undiscovered);
break;
}
case KEY_RIGHT:
if (x_axis < 21 && x_axis > -21)
{
if (x_axis == 20)
{
x_axis -= 1;
}
x_axis += 1;
arrow_right(rows, field, undiscovered, y_axis, x_axis);
field_of_view(rows, field, y_axis, x_axis);
draw(rows, field, undiscovered);
break;
}
case KEY_UP:
if (y_axis < 22 && y_axis >= 0)
{
if (y_axis == 21)
{
y_axis -= 1;
}
y_axis += 1;
arrow_up(rows, field, undiscovered, y_axis, x_axis);
field_of_view(rows, field, y_axis, x_axis);
draw(rows, field, undiscovered);
break;
}
case KEY_DOWN:
if (y_axis < 22 && y_axis > 0)
{
y_axis -= 1;
arrow_down(rows, field, undiscovered, y_axis, x_axis);
field_of_view(rows, field, y_axis, x_axis);
draw(rows, field, undiscovered);
break;
}
default:
continue;
break;
}
refresh();
} while (level_reach(rows, field, undiscovered) == false);
}
void map(const int rows, char undiscovered[][rows], char field[][rows], int level)
{
const int cols = 24;
//generate BLANK field
for (int y = 0; y < cols; y++)
{
for (int x = 0; x < rows; x++)
{
undiscovered[y][x] = field[y][x];
}
}
//randomise placement of enemies
int enemies = 0;
while (enemies < 5+level)
{
int random_x = (rand() % rows);
int random_y = (rand() % cols);
///////////////////////////////////////////////
if (undiscovered[random_y][random_x] == '?' && (undiscovered[random_y + 3][random_x + 5] == '?') && (undiscovered[random_y + 3][random_x] == '?') && (undiscovered[random_y][random_x + 5] == '?') && random_x < 41 && random_y < 21)
{
for (int y = random_y; y < random_y + 3; y++)
{
for (int x = random_x; x < random_x + 5; x++)
{
undiscovered[y][x] = 'V';
}
}
enemies++;
}
}
//generate next level
int treasure = 0;
while (treasure < 1)
{
int random_x = (rand() % rows);
int random_y = (rand() % cols);
///////////////////////////////////////////////
if (undiscovered[random_y][random_x] == '?' && (undiscovered[random_y + 3][random_x + 5] == '?') && (undiscovered[random_y + 3][random_x] == '?') && (undiscovered[random_y][random_x + 5] == '?') && random_x < 41 && random_y < 21)
{
for (int y = random_y; y < random_y + 3; y++)
{
for (int x = random_x; x < random_x + 5; x++)
{
undiscovered[y][x] = '*';
}
}
treasure++;
}
}
//remove fog and player from map
for (int y = 0; y < cols; y++)
{
for (int x = 0; x < rows; x++)
{
if (undiscovered[y][x] == '?' || undiscovered[y][x] == '-' || undiscovered[y][x] == '<' || undiscovered[y][x] == '>')
{
undiscovered[y][x] = BLANK;
}
}
}
}
//player spawn
void spawn_player(const int rows, char field[][rows], char undiscovered[][rows])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 5; j++)
{
field[21 + i][(44 / 2) - 2 + j] = player[i][j];
}
}
}