-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocessing.c
329 lines (292 loc) · 7.61 KB
/
processing.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
// Copyright 2022 <Maros Varchola - mvarchdev>
#include "processing.h"
#include <ctype.h>
#include <ncurses.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "common_tools.h"
#include "rendering.h"
#define SAVES_FILE "./assets/saves.conf"
#define HALLOFFAME_FILE "./assets/hall_of_fame.conf"
char act_nick[64] = {0};
int get_last_level(char *nickname);
int set_last_level(char *nickname, int level);
int get_hall_of_fame(char nickname[], int level);
int set_hall_of_fame(char nickname[], int score, int level);
int process_run_level(level *inplvl);
int select_level_dialog();
void request_nickname();
int show_about_page();
int show_hof();
int process_option(int option) {
switch (option) {
case 0: {
int last_level = get_last_level(act_nick);
if (last_level > 0) {
level loadedlvl = load_level_file(last_level);
if (!loadedlvl.loaded) {
render_header_string(
"The level that is saved, does not exist! Setting to level 1.", 0,
true, true);
refresh();
msleep(1500);
flushinp();
set_last_level(act_nick, 1);
} else
process_run_level(&loadedlvl);
} else {
render_header_string(
"You are playing this game for the first time, running level 1", 0,
true, true);
refresh();
msleep(1500);
flushinp();
level loadedlvl = load_level_file(1);
process_run_level(&loadedlvl);
}
} break;
case 1: {
int selectedlvl = select_level_dialog();
if (selectedlvl > 0) {
level loadedlvl = load_level_file(selectedlvl);
process_run_level(&loadedlvl);
}
} break;
case 2:
request_nickname();
break;
case 3:
show_hof();
break;
case 4:
show_about_page();
break;
default:
break;
}
return 0;
}
int select_level_dialog() {
int actoption = 0, actlvl = 1, scroll = 0;
keypad(stdscr, true);
int degrees = 0;
while (true) {
bool refrall = false;
int maxscroll = 0;
print_level_options(actoption);
level tmplvl = load_level_file(actlvl);
maxscroll = print_level_info(&tmplvl, scroll);
refresh();
flushinp();
timeout(0);
while (true) {
degrees = render_bird_floating(&tmplvl, degrees);
switch (getch()) {
case KEY_RIGHT:
if (actoption < 5) actoption++;
refrall = true;
break;
case KEY_LEFT:
if (actoption > 0) actoption--;
refrall = true;
break;
case '\n':
refrall = true;
switch (actoption) {
case 0:
return actlvl;
case 1:
if (scroll < maxscroll) scroll++;
break;
case 2:
if (scroll > 0) scroll--;
break;
case 3:
if (load_level_file(actlvl + 1).loaded) actlvl++;
break;
case 4:
if (actlvl - 1 > 0 && load_level_file(actlvl - 1).loaded)
actlvl--;
break;
case 5:
keypad(stdscr, false);
return 0;
default:
break;
}
break;
default:
break;
}
if (refrall) break;
}
}
}
int show_about_page() {
int scroll = 0;
timeout(-1);
keypad(stdscr, true);
render_header_string("This is \"about this game\" page, to exit, press 'b'",
1, true, true);
while (true) {
int maxscroll = render_about_page(scroll);
refresh();
flushinp();
int ch = getch();
switch (tolower(ch)) {
case KEY_UP:
if (scroll > 0) scroll--;
break;
case KEY_DOWN:
if (scroll < maxscroll) scroll++;
break;
case 'b':
timeout(0);
keypad(stdscr, false);
return 0;
default:
break;
}
}
}
int process_run_level(level *inplvl) {
if (!inplvl) return -1;
if (!inplvl->loaded) {
char outputstr[60] = {0};
sprintf(outputstr, "The level %d does not exist! Exiting...",
inplvl->levelnumber);
render_header_string(outputstr, 0, true, true);
refresh();
msleep(1500);
flushinp();
return -1;
}
int status = 0;
set_last_level(act_nick, inplvl->levelnumber);
int score = run_level(inplvl, &status);
int hof = get_hall_of_fame(act_nick, inplvl->levelnumber);
char outstring[255] = {0};
if (score > hof && score > 0) {
sprintf(outstring,
"GAME OVER! NEW HIGH SCORE FOR THIS LEVEL! %s - YOUR SCORE: %d "
"[SAVING IT TO HALL OF FAME]",
act_nick, score);
set_hall_of_fame(act_nick, score, inplvl->levelnumber);
} else
sprintf(outstring, "GAME OVER! %s - YOUR SCORE: %d", act_nick, score);
render_header_string(outstring, -1, true, true);
refresh();
msleep(1500);
flushinp();
return score;
}
int run_menu() {
int actoption = 0;
while (true) {
keypad(stdscr, true);
render_menu(actoption, act_nick);
refresh();
flushinp();
timeout(-1);
int ch = getch();
switch (ch) {
case KEY_RIGHT:
if (actoption < menu_inem_n - 1) actoption++;
break;
case KEY_LEFT:
if (actoption > 0) actoption--;
break;
case '\n':
if (actoption == 5) {
keypad(stdscr, false);
return 1;
}
process_option(actoption);
break;
default:
break;
}
}
}
void request_nickname() {
while (true) {
render_header_string("Please write here your nickname (max 63 chars): ", 1,
true, true);
refresh();
echo();
curs_set(1);
flushinp();
timeout(-1);
turn_on_header_color(true);
getnstr(act_nick, sizeof(act_nick) - 1);
turn_off_header_color(true);
curs_set(0);
noecho();
if (strcmp("", act_nick) == 0) {
render_header_string("Your nick cannot be empty!", 1, true, true);
refresh();
msleep(1500);
} else
break;
}
char outmessage[120] = {0};
sprintf(outmessage, "Your nickname is now: %s", act_nick);
render_header_string(outmessage, 1, true, true);
refresh();
msleep(1500);
}
int get_last_level(char nickname[]) {
return get_int_key_value(SAVES_FILE, nickname);
}
int set_last_level(char nickname[], int inlevel) {
return set_int_key_value("./lvltmpfile", SAVES_FILE, nickname, inlevel);
}
int get_hall_of_fame(char nickname[], int level) {
char findkey[120] = {0};
sprintf(findkey, "%s#lvl_%d#", nickname, level);
return get_int_key_value(HALLOFFAME_FILE, findkey);
}
int set_hall_of_fame(char nickname[], int score, int level) {
char setkey[120] = {0};
sprintf(setkey, "%s#lvl_%d#", nickname, level);
return set_int_key_value("./hoftmpfile", HALLOFFAME_FILE, setkey, score);
}
int show_hof() {
int scroll = 0;
timeout(-1);
keypad(stdscr, true);
render_header_string("This is \"HALL OF FAME\" page, to exit, press 'b'", 1,
true, true);
config_option_t hof = read_config_file(HALLOFFAME_FILE);
while (true) {
bool scrollalowed = render_hof(hof, scroll, false, act_nick);
refresh();
flushinp();
int ch = getch();
switch (tolower(ch)) {
case KEY_UP:
if (scroll > 0) scroll--;
break;
case KEY_DOWN:
if (scrollalowed) scroll++;
break;
case 'b':
timeout(0);
keypad(stdscr, false);
while (hof) {
config_option_t prev = hof->prev;
free(hof);
hof = prev;
}
return 0;
default:
break;
}
}
}
int run_game() {
request_nickname();
run_menu();
return 0;
}