-
Notifications
You must be signed in to change notification settings - Fork 1
/
zad1.c
545 lines (459 loc) · 19.5 KB
/
zad1.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
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include "conio.h"
#define mapX 120
#define mapY 63
#define L_UP 1
#define L_LEFT 2
#define L_RIGHT 4
#define L_DOWN 3
#define DRAW_DEBUG 0
#define E_HORIZONTAL_TUNNEL 0
#define E_VERTICAL_TUNNEL 1
#define E_LEFT_DOWN 2
#define E_RIGHT_DOWN 3
#define E_DOWN_LEFT 4
#define E_DOWN_RIGHT 5
#define E_FILLED 6
#define E_ZEROS 7
#define A_SAVE 0
#define A_LOAD 1
#define G_CONTINUE 0
#define G_SAVE 2
#define G_QUIT 3
#define G_RESET 4
#define G_LOAD 5
void title_screen() {
printf("\x1b[2J\x1b[0;0f");
printf("\n");
printf(" ███ ███ ███ ██\n");
printf(" ███ ███ ███ ██████\n");
printf(" ███ ███ ████ ██ ██ ██ ██ █████ ██\n");
printf(" ███ ██████ ███████ ███ ██ ██ ████ ██ ██ ████ ██ ██\n");
printf(" ███ ██ ██ ███ ███ ███ █████ ██ ██ ███ ██ ██\n");
printf(" ███ ██ ███ ██ ██ ███ ███ ██ ██ ██ ██ ██ ██\n");
printf(" ██████████ ███████ ██ ██████████ ███ ███ █████ ██ ██ ██████\n");
printf(" ███ ██\n");
printf(" ███ ██ ███\n");
printf(" █████████████████████████████████████████████████████████████\n");
printf(" ██ ██\n");
printf(" /██ ██ ████ ██\n");
printf(" ███████████████████████████████████████████\n");
printf(" ██/// ██ ██████ ██ ██ ████ ██ ███\n");
printf(" ██// Nacisnij dowolny klawisz ██ ██ ████ ██ ██ ██ ██\n");
printf(" ██/ ██ ██ ██ ██ ██ ███████\n");
printf(" ██ ruch - klawiatura numeryczna ██ ██████ ██ ████ ██ ██ ██\n");
printf(" ██ r - restart | q - koniec ██\n");
printf(" ██ s - zapis | l - wczytanie /██\n");
printf(" ██ ------- //██\n");
printf(" ██ ///██\n");
printf(" ███████████████████████████████████████████\n");
printf(" ██ ██/\n");
}
void finished_screen(int width, int height) {
int start_X = width/2 - 10;
int start_Y = height/2 - 4;
int cur_Y = start_Y;
printf("\x1b[%d;%df", cur_Y++, start_X);
printf("*------------------*");printf("\x1b[%d;%df", cur_Y++, start_X);
printf("| *fanfary* |");printf("\x1b[%d;%df", cur_Y++, start_X);
printf("| ZWYCIESTWO! |");printf("\x1b[%d;%df", cur_Y++, start_X);
printf("| ---- |");printf("\x1b[%d;%df", cur_Y++, start_X);
printf("| nacisnij dowolny |");printf("\x1b[%d;%df", cur_Y++, start_X);
printf("| klawisz_ |");printf("\x1b[%d;%df", cur_Y++, start_X);
printf("*------------------*");printf("\x1b[%d;%df", cur_Y++, start_X);
int h;
h = getch();
}
void draw_map(int (*map)[mapX][mapY], int width, int height, int x, int y, int endX, int endY) {
// if map is smaller than screen size, use map height as screen height
if (mapY-height < 0)
height = mapY;
// if map is smaller than screen size, use map width as screen width
if (mapX - (width/2) < 0)
width = mapX;
int tmpX = 0;
if (x > (width/4)) {
tmpX = x - (width/4);
}
int tmpY = y-(height/2);
if (tmpY < 0)
tmpY = 0;
if (tmpY > mapY-height)
tmpY = mapY-height;
int i, j;
for (j = tmpY; j<tmpY+height; j++) { // height
printf("\x1b[%d;0f", j+1-tmpY);
for (i = tmpX; i<=(tmpX+width/2)-2; i++) {
if (j==y && i==x) printf("\\/");
else if (j==endY && i == endX) printf("[]");
else {
if ((*map)[i][j] != 0)
printf("██", (*map)[i][j]);
else printf(" "); }
}
}
}
int is_taken(int x, int y, int (*map)[mapX][mapY]) {
// calculate x range
int start_x = x*3;
int end_x = start_x+2;
// calculate y range
int start_y = y*3;
int end_y = start_y+2;
// iterate through all blocks
int i,j;
for (j = start_y; j <= end_y; j++)
for (i = start_x; i <= end_x; i++)
// if any block taken, return 1
if ((*map)[i][j] != 0) return 1;
return 0;
}
void set_block(int x, int y, int old_direction, int direction, int (*map)[mapX][mapY]) {
// calculate x range
int start_x = x*3;
int mid_x = start_x+1;
int end_x = start_x+2;
// calculate y range
int start_y = y*3;
int mid_y = start_y+1;
int end_y = start_y+2;
int element = 8;
switch (direction) {
case 0:
if (old_direction == L_LEFT || old_direction == L_RIGHT) element = E_HORIZONTAL_TUNNEL;
if (old_direction == L_UP || old_direction == L_DOWN) element = E_VERTICAL_TUNNEL;
element = E_FILLED;
break;
case L_UP:
element = E_VERTICAL_TUNNEL;
if (old_direction == L_LEFT) element = E_DOWN_LEFT;
if (old_direction == L_RIGHT) element = E_DOWN_RIGHT;
break;
case L_LEFT:
element = E_HORIZONTAL_TUNNEL;
if (old_direction == L_DOWN) element = E_DOWN_RIGHT;
if (old_direction == L_UP) element = E_RIGHT_DOWN;
break;
case L_RIGHT:
element = E_HORIZONTAL_TUNNEL;
if (old_direction == L_DOWN) element = E_DOWN_LEFT;
if (old_direction == L_UP) element = E_LEFT_DOWN;
break;
case L_DOWN:
element = E_VERTICAL_TUNNEL;
if (old_direction == L_LEFT) element = E_LEFT_DOWN;
if (old_direction == L_RIGHT) element = E_RIGHT_DOWN;
break;
case E_ZEROS:
element = E_ZEROS;
break;
}
switch (element) {
case E_HORIZONTAL_TUNNEL:
(*map)[start_x][start_y] = (*map)[mid_x][start_y] = (*map)[end_x][start_y] = 1; // ###
//
(*map)[start_x][end_y] = (*map)[mid_x][end_y] = (*map)[end_x][end_y] = 1; // ###
break;
case E_VERTICAL_TUNNEL:
(*map)[start_x][start_y] = (*map)[end_x][start_y] = 1; // # #
(*map)[start_x][mid_y] = (*map)[end_x][mid_y] = 1; // # #
(*map)[start_x][end_y] = (*map)[end_x][end_y] = 1; // # #
break;
case E_LEFT_DOWN:
(*map)[start_x][start_y] = (*map)[mid_x][start_y] = (*map)[end_x][start_y] = 1; // ###
(*map)[start_x][mid_y] = 1; // #
(*map)[start_x][end_y] = (*map)[end_x][end_y] = 1; // # #
break;
case E_RIGHT_DOWN:
(*map)[start_x][start_y] = (*map)[mid_x][start_y] = (*map)[end_x][start_y] = 1; // ###
(*map)[end_x][mid_y] = 1; // #
(*map)[start_x][end_y] = (*map)[end_x][end_y] = 1; // # #
break;
case E_DOWN_LEFT:
(*map)[start_x][start_y] = (*map)[end_x][start_y] = 1; // # #
(*map)[start_x][mid_y] = 1; // #
(*map)[start_x][end_y] = (*map)[mid_x][end_y] = (*map)[end_x][end_y] = 1; // ###
break;
case E_DOWN_RIGHT:
(*map)[start_x][start_y] = (*map)[end_x][start_y] = 1; // # #
(*map)[end_x][mid_y] = 1; // #
(*map)[start_x][end_y] = (*map)[mid_x][end_y] = (*map)[end_x][end_y] = 1; // ###
break;
case E_FILLED:
(*map)[start_x][start_y] = (*map)[mid_x][start_y] = (*map)[end_x][start_y] = 1;
(*map)[start_x][mid_y] = (*map)[mid_x][mid_y] = (*map)[end_x][mid_y] = 1;
(*map)[start_x][end_y] = (*map)[mid_x][end_y] = (*map)[end_x][end_y] = 1;
break;
case E_ZEROS:
(*map)[start_x][start_y] = (*map)[mid_x][start_y] = (*map)[end_x][start_y] = 0;
(*map)[start_x][mid_y] = (*map)[mid_x][mid_y] = (*map)[end_x][mid_y] = 0;
(*map)[start_x][end_y] = (*map)[mid_x][end_y] = (*map)[end_x][end_y] = 0;
break;
}
if (DRAW_DEBUG == 1) {
draw_map(map, 62, 24, (x*3)+1, (y*3)+1, -1, -1);
//usleep(5000);
}
}
int test_gen_direction(int (*map)[mapX][mapY], int direction, int x, int y) {
int newX = x; int newY = y;
switch (direction) {
case 0: return 0;
case L_UP : newY -= 1; break;
case L_LEFT : newX -= 1; break;
case L_DOWN : newY += 1; break;
case L_RIGHT: newX += 1; break;
}
// check if is in range
if (newX < 0 || newX*3 >= mapX) return 0;
if (newY < 0 || newY*3 >= mapY) return 0;
// avoid overwriting blocks
if (is_taken(newX, newY, map)) return 0;
return 1;
}
void mapgen(int (*map)[mapX][mapY], int startX, int startY, int (*taken_map)[mapX][mapY], int *endX, int *endY) {
// calculate length
int length = (120*63)/100;
// initialize array for storing path
int path[length];
int x = startX; int y = startY;
int z=0;
while (z<length) {
// starting with 0
int old_direction = 0;
// if not first element, get old one
if (z > 0)
old_direction = path[z-1];
// copy position to new variables
int newX = x; int newY = y;
int legal_moves[5] = {0, 0, 0, 0, 0}; // count, 1, 2, 3, 4
if (test_gen_direction(taken_map, L_UP, x, y) == 1 && old_direction != L_DOWN) {
legal_moves[0] += 1;
legal_moves[legal_moves[0]] = L_UP;
}
if (test_gen_direction(taken_map, L_DOWN, x, y) == 1 && old_direction != L_UP) {
legal_moves[0] += 1;
legal_moves[legal_moves[0]] = L_DOWN;
}
if (test_gen_direction(taken_map, L_LEFT, x, y) == 1 && old_direction != L_RIGHT) {
legal_moves[0] += 1;
legal_moves[legal_moves[0]] = L_LEFT;
}
if (test_gen_direction(taken_map, L_RIGHT, x, y) == 1 && old_direction != L_LEFT) {
legal_moves[0] += 1;
legal_moves[legal_moves[0]] = L_RIGHT;
}
int direction = 0;
if (legal_moves[0] > 1) {
// randomize direction
int random = (rand()/(float)RAND_MAX)*legal_moves[0];
random++;
direction = legal_moves[random];
} else if (legal_moves[0] == 1) {
direction = legal_moves[1];
} else if (legal_moves[0] == 0) {
set_block(x, y, old_direction, E_ZEROS, map);
set_block(x, y, old_direction, direction, map);
break;
}
switch (direction) {
case 0: break;
case L_UP : newY -= 1; break;
case L_LEFT : newX -= 1; break;
case L_DOWN : newY += 1; break;
case L_RIGHT: newX += 1; break;
}
// exit if no direction
if (direction == 0) continue;
// yaay, it worked!
path[z] = direction;
z++;
// reset element
set_block(x, y, 0, E_ZEROS, map);
// set element
set_block(x, y, old_direction, direction, map);
if (direction != 0) {
(*endX) = (x*3)+1;
(*endY) = (y*3)+1;
}
// make sure blocks are registered on both maps
if (map != taken_map) {
set_block(x, y, 0, E_ZEROS, taken_map);
set_block(x, y, old_direction, direction, taken_map);
}
// set old direction to current
old_direction = direction;
x = newX; y = newY;
}
}
void join_paths(int (*map)[mapX][mapY]) {
int x = 0; int y = 0;
int przesuniecie = 0;
for (y = 0; y<(mapY/3)-1; y++) {
for (x = 0; x<mapX/3; x++) {
int sx = x*3;
int sy = y*3;
for (przesuniecie = 0; przesuniecie <= 2; przesuniecie++) {
if ((*map)[sx][sy+1+przesuniecie] && (*map)[sx+1][sy+1+przesuniecie] && (*map)[sx+2][sy+1+przesuniecie]) {
if ((*map)[sx+1][sy+przesuniecie] == 0 && (*map)[sx+1][sy+2+przesuniecie] == 0) {
(*map)[sx+1][sy+1+przesuniecie] = 0;
}
}
}
}
}
for (y = 0; y<mapY/3; y++) {
for (x = 0; x<(mapX/3)-1; x++) {
int sx = x*3;
int sy = y*3;
for (przesuniecie = 0; przesuniecie <= 2; przesuniecie++) {
if ((*map)[sx+1+przesuniecie][sy] && (*map)[sx+1+przesuniecie][sy+1] && (*map)[sx+1+przesuniecie][sy+2]) { // jest pionowa liinia
if ((*map)[sx+przesuniecie][sy+1] == 0 && (*map)[sx+2+przesuniecie][sy+1] == 0) {
(*map)[sx+1+przesuniecie][sy+1] = 0;
}
}
}
}
}
}
void hardcore_mapgen(int (*map)[mapX][mapY], int *endX, int *endY) {
int x, y;
// reset map
for (y=0; y<mapY; y++)
for (x=0; x<mapX; x++)
(*map)[x][y] = 0;
// generate path starting from every empty block
for (y = 0; y<mapY/3; y++)
for (x = 0; x<mapX/3; x++)
if (is_taken(x, y, map) == 0) // check if block is empty
mapgen(map,x,y,map, endX, endY); // generate path
// generate final route
int final_map[mapX][mapY] = {0};
mapgen(map, 0, 0, &final_map, endX, endY);
// remove walls between paths to merge them
join_paths(map);
}
int move(int (*map)[mapX][mapY], int *x, int *y) {
// get direction from user
int btn = 0;
btn = getch();
switch (btn) {
case 56:
if ((*map)[(*x)][(*y)-1] == 0)
(*y) -= 1;
break;
case 50:
if ((*map)[(*x)][(*y)+1] == 0)
(*y) += 1;
break;
case 52:
if ((*map)[(*x)-1][(*y)] == 0)
(*x) -= 1;
break;
case 54:
if ((*map)[(*x)+1][(*y)] == 0)
(*x) += 1;
break;
case 115:
return G_SAVE;
break;
case 113:
return G_QUIT;
break;
case 108:
return G_LOAD;
break;
case 114:
return G_RESET;
break;
}
return 0;
}
void save_load(int (*map)[mapX][mapY], int *x, int *y, int *endX, int *endY, int action) {
FILE *fp;
int x1, y1;
switch (action) {
case A_SAVE:
fp = fopen("zapis.txt", "w");
fprintf(fp, "%d %d %d %d \n", *x, *y, *endX, *endY);
// save map
for (y1 = 0; y1<mapY; y1++) {
for (x1 = 0; x1<mapX; x1++)
fprintf(fp, "%d ", (*map)[x1][y1]);
fprintf(fp, "\n");
}
fclose(fp);
break;
case A_LOAD:
printf("Wczytywanie stanu gry...\n");
fp = fopen("zapis.txt", "r");
fscanf(fp, "%d", x);
fscanf(fp, "%d", y);
fscanf(fp, "%d", endX);
fscanf(fp, "%d", endY);
// load map
for (y1 = 0; y1<mapY; y1++)
for (x1 = 0; x1<mapX; x1++)
fscanf(fp, "%d", &(*map)[x1][y1]);
fclose(fp);
break;
}
}
int main() {
// randomize
srand(time(NULL));
// make sure game resets at the beginning
int restart = 1;
// initialize values
int x, y, endX, endY;
int map[mapX][mapY] = {0};
// display title screen
title_screen();
// wait for user to press the key
int h; h = getch();
// get console data
struct winsize max;
ioctl(0, TIOCGWINSZ , &max);
// if key is l, load game state
if (h==108) {
save_load(&map,&x,&y,&endX,&endY, A_LOAD); // load previous state
restart = 0; // make sure game won't restart
usleep(600000); // make sure user got time to read
}
int is_running = 1;
// game loop
while (is_running) {
// check if player got to the end
if (x == endX && y == endY) {
finished_screen(max.ws_col-1, max.ws_row-1);
restart = 1; // make sure game restarts
}
// if restart is set to 1, reset all values
if (restart == 1) {
x = 1; y = 1;
hardcore_mapgen(&map, &endX, &endY);
restart = 0;
}
// draw map on screen
draw_map(&map,max.ws_col, max.ws_row, x, y, endX, endY);
h = move(&map, &x, &y);
switch (h) {
case G_SAVE:
save_load(&map,&x,&y,&endX,&endY, A_SAVE);
break;
case G_QUIT:
is_running = 0;
break;
case G_RESET:
restart = 1;
break;
case G_LOAD:
save_load(&map,&x,&y,&endX,&endY, A_LOAD);
break;
}
}
return 0;
}