-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathstage5.c
More file actions
397 lines (334 loc) · 10.1 KB
/
stage5.c
File metadata and controls
397 lines (334 loc) · 10.1 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <termios.h>
#include <signal.h>
#include <string.h>
#define MAX_X 60
#define MAX_Y 26
typedef struct {
int key;
int pos_x;
int pos_y;
int gems_collected;
int dead;
int won;
char old_screen[MAX_Y][MAX_X];
char screen[MAX_Y][MAX_X];
} GameState;
static struct termios old_termios, new_termios;
void reset_terminal() {
printf("\e[m"); // reset color changes
printf("\e[?25h"); // show cursor
printf("\e[%d;%dH\n", MAX_Y + 3, MAX_X); // move cursor after game board
fflush(stdout);
tcsetattr(STDIN_FILENO, TCSANOW, &old_termios);
}
void configure_terminal() {
tcgetattr(STDIN_FILENO, &old_termios);
new_termios = old_termios; // save it to be able to reset on exit
new_termios.c_lflag &= ~(ICANON | ECHO); // turn off echo + non-canonical mode
new_termios.c_cc[VMIN] = 0;
new_termios.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSANOW, &new_termios);
printf("\e[?25l"); // hide cursor
atexit(reset_terminal);
}
static int exit_loop;
void signal_handler(__attribute__((unused)) int signum) {
exit_loop = 1;
}
int read_key(char* buf, int k) {
if (buf[k] == '\033' && buf[k + 1] == '[') {
switch (buf[k + 2]) {
case 'A': return 1; // UP
case 'B': return 2; // DOWN
case 'C': return 3; // RIGHT
case 'D': return 4; // LEFT
}
}
return 0;
}
void read_input(GameState* state) {
char buf[4096]; // maximum input buffer
int n = read(STDIN_FILENO, buf, sizeof(buf));
int final_key = 0;
// it's okay if we miss some keys
// we will correct it on next frame
for (int k = 0; k <= n - 3; k += 3) {
int key = read_key(buf, k);
if (key == 0) continue;
final_key = key;
}
state->key = final_key;
}
void handle_player(GameState* state) {
switch (state->key) {
case 1:
switch (state->screen[state->pos_y - 1][state->pos_x]) {
case '$':
state->gems_collected++; // fallthrough
case ' ':
case '.':
state->screen[state->pos_y][state->pos_x] = ' ';
state->screen[state->pos_y - 1][state->pos_x] = '@';
--state->pos_y;
break;
case 'E':
state->won = 1;
break;
case 'o':
case 'S':
state->dead = 1;
break;
default:
break;
}
break;
case 2:
switch (state->screen[state->pos_y + 1][state->pos_x]) {
case '$':
state->gems_collected++; // fallthrough
case ' ':
case '.':
state->screen[state->pos_y][state->pos_x] = ' ';
state->screen[state->pos_y + 1][state->pos_x] = '@';
++state->pos_y;
break;
case 'E':
state->won = 1;
break;
default:
break;
}
break;
case 3:
switch (state->screen[state->pos_y][state->pos_x + 1]) {
case '$':
state->gems_collected++; // fallthrough
case ' ':
case '.':
state->screen[state->pos_y][state->pos_x] = ' ';
state->screen[state->pos_y][state->pos_x + 1] = '@';
++state->pos_x;
break;
case 'E':
state->won = 1;
break;
case 'O':
if (state->screen[state->pos_y][state->pos_x + 2] == ' ') {
state->screen[state->pos_y][state->pos_x + 2] = 'O';
state->screen[state->pos_y][state->pos_x] = ' ';
state->screen[state->pos_y][state->pos_x + 1] = '@';
++state->pos_x;
}
break;
default:
break;
}
break;
case 4:
switch (state->screen[state->pos_y][state->pos_x - 1]) {
case '$':
state->gems_collected++; // fallthrough
case ' ':
case '.':
state->screen[state->pos_y][state->pos_x] = ' ';
state->screen[state->pos_y][state->pos_x - 1] = '@';
--state->pos_x;
break;
case 'E':
state->won = 1;
break;
case 'O':
if (state->screen[state->pos_y][state->pos_x - 2] == ' ') {
state->screen[state->pos_y][state->pos_x - 2] = 'O';
state->screen[state->pos_y][state->pos_x] = ' ';
state->screen[state->pos_y][state->pos_x - 1] = '@';
--state->pos_x;
}
break;
default:
break;
}
break;
default:
break;
}
}
void handle_rocks_gems(GameState* state, int x, int y) {
int gem = state->screen[y][x] == '$';
if (state->screen[y + 1][x] == ' ') { // start to fall
if (gem) {
state->screen[y][x] = 'S';
} else {
state->screen[y][x] = 'o';
}
return;
}
if (state->screen[y + 1][x] == 'O' || state->screen[y + 1][x] == '$') {
// check left
if (state->screen[y][x - 1] == ' ' && state->screen[y + 1][x - 1] == ' ') {
if (gem) {
state->screen[y][x] = 'S';
} else {
state->screen[y][x] = 'o';
}
}
// check right
if (state->screen[y][x + 1] == ' ' && state->screen[y + 1][x + 1] == ' ') {
if (gem) {
state->screen[y][x] = 'S';
} else {
state->screen[y][x] = 'o';
}
}
}
}
void handle_falling_rocks_gems(GameState* state, int x, int y) {
int gem = state->screen[y][x] == 'S';
if (state->screen[y + 1][x] == ' ') {
state->screen[y][x] = ' ';
if (gem) {
state->screen[y + 1][x] = 'S';
} else {
state->screen[y + 1][x] = 'o';
}
return;
}
if (state->screen[y + 1][x] == 'O' || state->screen[y + 1][x] == '$') {
// check left
if (state->screen[y][x - 1] == ' ' && state->screen[y + 1][x - 1] == ' ') {
state->screen[y][x] = ' ';
if (gem) {
state->screen[y][x - 1] = 'S';
} else {
state->screen[y][x - 1] = 'o';
}
return;
}
// check right
if (state->screen[y][x + 1] == ' ' && state->screen[y + 1][x + 1] == ' ') {
state->screen[y][x] = ' ';
if (gem) {
state->screen[y][x + 1] = 'S';
} else {
state->screen[y][x + 1] = 'o';
}
return;
}
}
if (state->screen[y + 1][x] == 'o' || state->screen[y + 1][x] == 'S') return;
if (state->screen[y + 1][x] == '@') {
state->dead = 1;
}
if (gem) {
state->screen[y][x] = '$';
} else {
state->screen[y][x] = 'O';
}
}
void update_all_elements(GameState* state) {
// We iterate over the screen from bottom to top from right to left
for (int j = MAX_Y - 1; j != 0; --j) {
for (int i = MAX_X - 2; i != 0; --i) {
switch (state->screen[j][i]) {
case 'O':
case '$':
handle_rocks_gems(state, i, j);
break;
case 'o':
case 'S':
handle_falling_rocks_gems(state, i, j);
break;
default:
break;
}
}
}
}
void update(GameState* state) {
memcpy(state->screen, state->old_screen, sizeof(state->screen));
handle_player(state);
update_all_elements(state);
}
void render(GameState* state) {
for (int j = 0; j < MAX_Y; ++j) {
for (int i = 0; i < MAX_X; ++i) {
if (state->old_screen[j][i] != state->screen[j][i]) {
printf("\e[%d;%dH", j + 1, i + 1); // move cursor
printf("%c", state->screen[j][i]);
}
}
}
fflush(stdout);
}
void find_player_position(GameState* state) {
for (int j = 0; j < MAX_Y; ++j) {
for (int i = 0; i < MAX_X; ++i) {
if (state->screen[j][i] == '@') {
state->pos_x = i;
state->pos_y = j;
return;
}
}
}
}
void load_level(GameState* state) {
FILE* f = fopen("./level_1.txt", "r");
if (!f) {
fprintf(stderr, "Failed to open level_1.txt");
exit(EXIT_FAILURE);
}
if (fread(state->screen, 1, MAX_X * MAX_Y, f) != MAX_X * MAX_Y) {
fprintf(stderr, "Failed to read level_1.txt");
fclose(f);
exit(EXIT_FAILURE);
}
render(state); // To display the level
find_player_position(state);
memcpy(state->old_screen, state->screen, sizeof(state->screen));
fclose(f);
}
void print_end_message(GameState* state) {
printf("\e[%d;%dH", MAX_Y + 2, 1); // move cursor
if (state->dead) {
printf("You died! Better luck next time!");
}
if (state->won) {
printf("You won! You collected %d gems!", state->gems_collected);
}
}
// Lower is faster
#define SPEED 0.1
int main() {
configure_terminal();
signal(SIGINT, signal_handler);
struct timespec req = {};
struct timespec rem = {};
GameState state = {
.pos_x = 5,
.pos_y = 5
};
printf("\e[2J");
load_level(&state);
clock_t start, end;
while (!exit_loop) {
start = clock();
read_input(&state);
update(&state);
if (state.won || state.dead) {
print_end_message(&state);
break;
}
render(&state);
memcpy(state.old_screen, state.screen, sizeof(state.screen));
end = clock();
double time_taken = ((double)(end - start)) / CLOCKS_PER_SEC;
if (time_taken > SPEED) continue;
req.tv_sec = 0;
req.tv_nsec = (SPEED - time_taken) * 1000000000; // 0.1 seconds
nanosleep(&req, &rem);
}
}