forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_menu.cpp
426 lines (390 loc) · 16.3 KB
/
main_menu.cpp
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
#include "game.h"
#include "input.h"
#include "keypress.h"
#include "debug.h"
#include "mapbuffer.h"
#include "cursesdef.h"
#include <sys/stat.h>
#include <dirent.h>
#define dbg(x) dout((DebugLevel)(x),D_GAME) << __FILE__ << ":" << __LINE__ << ": "
void game::print_menu(WINDOW* w_open, int iSel, const int iMenuOffsetX, int iMenuOffsetY, bool bShowDDA)
{
//Clear Lines
werase(w_open);
for (int i = 1; i < 79; i++)
mvwputch(w_open, 23, i, c_white, LINE_OXOX);
mvwprintz(w_open, 24, 5, c_red, "Please report bugs to TheDarklingWolf@gmail.com or post on the forums.");
int iLine = 0;
const int iOffsetX1 = 3;
const int iOffsetX2 = 4;
const int iOffsetX3 = 18;
const nc_color cColor1 = c_ltcyan;
const nc_color cColor2 = c_ltblue;
const nc_color cColor3 = c_ltblue;
mvwprintz(w_open, iLine++, iOffsetX1, cColor1, "_________ __ .__ ");
mvwprintz(w_open, iLine++, iOffsetX1, cColor1, "\\_ ___ \\ _____ _/ |_ _____ ____ | | ___.__ ______ _____ ");
mvwprintz(w_open, iLine++, iOffsetX1, cColor1, "/ \\ \\/ \\__ \\ \\ __\\\\__ \\ _/ ___\\ | | < | | / ___/ / \\ ");
mvwprintz(w_open, iLine++, iOffsetX1, cColor1, "\\ \\____ / __ \\_ | | / __ \\_\\ \\___ | |__ \\___ | \\___ \\ | Y Y \\");
mvwprintz(w_open, iLine++, iOffsetX1, cColor1, " \\______ /(____ / |__| (____ / \\___ >|____/ / ____|/____ >|__|_| /");
mvwprintz(w_open, iLine++, iOffsetX1, cColor1, " \\/ \\/ \\/ \\/ \\/ \\/ \\/ ");
if (bShowDDA) {
iLine++;
mvwprintz(w_open, iLine++, iOffsetX2, cColor2, "________ .__ ________ ");
mvwprintz(w_open, iLine++, iOffsetX2, cColor2, "\\______ \\ _____ _______ | | __ \\______ \\ _____ ___.__ ______");
mvwprintz(w_open, iLine++, iOffsetX2, cColor2, " | | \\ \\__ \\ \\_ __ \\| |/ / | | \\ \\__ \\ < | | / ___/");
mvwprintz(w_open, iLine++, iOffsetX2, cColor2, " | ` \\ / __ \\_ | | \\/| < | ` \\ / __ \\_ \\___ | \\___ \\ ");
mvwprintz(w_open, iLine++, iOffsetX2, cColor2, "/_______ /(____ / |__| |__|_ \\ /_______ /(____ / / ____|/____ >");
mvwprintz(w_open, iLine++, iOffsetX2, cColor2, " \\/ \\/ \\/ \\/ \\/ \\/ \\/ ");
iLine++;
mvwprintz(w_open, iLine++, iOffsetX3, cColor3, " _____ .__ .___");
mvwprintz(w_open, iLine++, iOffsetX3, cColor3, " / _ \\ | |__ ____ _____ __| _/");
mvwprintz(w_open, iLine++, iOffsetX3, cColor3, " / /_\\ \\ | | \\ _/ __ \\ \\__ \\ / __ | ");
mvwprintz(w_open, iLine++, iOffsetX3, cColor3, "/ | \\| Y \\\\ ___/ / __ \\_/ /_/ | ");
mvwprintz(w_open, iLine++, iOffsetX3, cColor3, "\\____|__ /|___| / \\___ >(____ /\\____ | ");
mvwprintz(w_open, iLine++, iOffsetX3, cColor3, " \\/ \\/ \\/ \\/ \\/ ");
}
std::vector<std::string> vMenuItems;
vMenuItems.push_back("MOTD");
vMenuItems.push_back("New Game");
vMenuItems.push_back("Load");
vMenuItems.push_back("Reset");
vMenuItems.push_back("Special");
vMenuItems.push_back("Options");
vMenuItems.push_back("Help");
vMenuItems.push_back("Credits");
vMenuItems.push_back("Quit");
print_menu_items(w_open, vMenuItems, iSel, iMenuOffsetY, iMenuOffsetX);
refresh();
wrefresh(w_open);
refresh();
}
void game::print_menu_items(WINDOW* w_in, std::vector<std::string> vItems, int iSel, int iOffsetY, int iOffsetX)
{
mvwprintz(w_in, iOffsetY, iOffsetX, c_black, "");
for (int i=0; i < vItems.size(); i++) {
wprintz(w_in, c_ltgray, "[");
if (iSel == i) {
wprintz(w_in, h_white, vItems[i].c_str());
} else {
wprintz(w_in, c_white, (vItems[i].substr(0, 1)).c_str());
wprintz(w_in, c_ltgray, (vItems[i].substr(1)).c_str());
}
wprintz(w_in, c_ltgray, "] ");
}
}
bool game::opening_screen()
{
WINDOW* w_background = newwin(TERMY, TERMX, 0, 0);
werase(w_background);
wrefresh(w_background);
WINDOW* w_open = newwin(25, 80, (TERMY > 25) ? (TERMY-25)/2 : 0, (TERMX > 80) ? (TERMX-80)/2 : 0);
const int iMenuOffsetX = 2;
int iMenuOffsetY = 22;
std::vector<std::string> vSubItems;
vSubItems.push_back("Custom Character");
vSubItems.push_back("Preset Character");
vSubItems.push_back("Random Character");
print_menu(w_open, 0, iMenuOffsetX, iMenuOffsetY);
std::vector<std::string> savegames, templates;
std::string tmp;
dirent *dp;
DIR *dir = opendir("save");
if (!dir) {
#if (defined _WIN32 || defined __WIN32__)
mkdir("save");
#else
mkdir("save", 0777);
#endif
dir = opendir("save");
}
if (!dir) {
dbg(D_ERROR) << "game:opening_screen: Unable to make save directory.";
debugmsg("Could not make './save' directory");
endwin();
exit(1);
}
while ((dp = readdir(dir))) {
tmp = dp->d_name;
if (tmp.find(".sav") != std::string::npos)
savegames.push_back(tmp.substr(0, tmp.find(".sav")));
}
closedir(dir);
dir = opendir("data");
while ((dp = readdir(dir))) {
tmp = dp->d_name;
if (tmp.find(".template") != std::string::npos)
templates.push_back(tmp.substr(0, tmp.find(".template")));
}
int sel1 = 1, sel2 = 1, layer = 1;
InputEvent input;
int chInput;
bool start = false;
// Load MOTD and store it in a string
std::vector<std::string> motd;
std::ifstream motd_file;
motd_file.open("data/motd");
if (!motd_file.is_open())
motd.push_back("No message today.");
else {
while (!motd_file.eof()) {
std::string tmp;
getline(motd_file, tmp);
if (tmp[0] != '#')
motd.push_back(tmp);
}
}
// Load Credits and store it in a string
std::vector<std::string> credits;
std::ifstream credits_file;
credits_file.open("data/credits");
if (!credits_file.is_open())
credits.push_back("No message today.");
else {
while (!credits_file.eof()) {
std::string tmp;
getline(credits_file, tmp);
if (tmp[0] != '#')
credits.push_back(tmp);
}
}
while(!start) {
if (layer == 1) {
print_menu(w_open, sel1, iMenuOffsetX, iMenuOffsetY, (sel1 == 0 || sel1 == 7) ? false : true);
if (sel1 == 0) { // Print the MOTD.
for (int i = 0; i < motd.size() && i < 16; i++)
mvwprintz(w_open, i + 7, 8, c_ltred, motd[i].c_str());
wrefresh(w_open);
refresh();
} else if (sel1 == 7) { // Print the Credits.
for (int i = 0; i < credits.size() && i < 16; i++)
mvwprintz(w_open, i + 7, 8, c_ltred, credits[i].c_str());
wrefresh(w_open);
refresh();
}
chInput = getch();
if (chInput == 'm' || chInput == 'M') {
sel1 = 0;
chInput = '\n';
} else if (chInput == 'n' || chInput == 'N') {
sel1 = 1;
chInput = '\n';
} else if (chInput == 'L') {
sel1 = 2;
chInput = '\n';
} else if (chInput == 'r' || chInput == 'R') {
sel1 = 3;
chInput = '\n';
} else if (chInput == 's' || chInput == 'S') {
sel1 = 4;
chInput = '\n';
} else if (chInput == 'o' || chInput == 'O') {
sel1 = 5;
chInput = '\n';
} else if (chInput == 'H') {
sel1 = 6;
chInput = '\n';
} else if (chInput == 'c' || chInput == 'C') {
sel1 = 7;
chInput = '\n';
} else if (chInput == 'q' || chInput == 'Q' || chInput == KEY_ESCAPE) {
sel1 = 8;
chInput = '\n';
}
if (chInput == KEY_LEFT || chInput == 'h') {
if (sel1 > 0)
sel1--;
else
sel1 = 8;
} else if (chInput == KEY_RIGHT || chInput == 'l') {
if (sel1 < 8)
sel1++;
else
sel1 = 0;
} else if ((chInput == KEY_UP || chInput == 'k' || chInput == '\n') && sel1 > 0 && sel1 != 7) {
if (sel1 == 5) {
show_options();
} else if (sel1 == 6) {
help();
} else if (sel1 == 8) {
uquit = QUIT_MENU;
return false;
} else {
sel2 = 0;
layer = 2;
print_menu(w_open, sel1, iMenuOffsetX, iMenuOffsetY, (sel1 == 0 || sel1 == 7) ? false : true);
}
}
} else if (layer == 2) {
if (sel1 == 1) { // New Character
print_menu_items(w_open, vSubItems, sel2, iMenuOffsetY-2, iMenuOffsetX+7);
wrefresh(w_open);
refresh();
chInput = getch();
if (chInput == 'c' || chInput == 'C') {
sel2 = 0;
chInput = '\n' ;
} else if (chInput == 'p' || chInput == 'P') {
sel2 = 1;
chInput = '\n';
} else if (chInput == 'r' || chInput == 'R') {
sel2 = 2;
chInput = '\n';
}
if (chInput == KEY_LEFT || chInput == 'h') {
if (sel2 > 0)
sel2--;
else
sel2 = 2;
} if (chInput == KEY_RIGHT || chInput == 'l') {
if (sel2 < 2)
sel2++;
else
sel2 = 0;
} else if (chInput == KEY_DOWN || chInput == 'j' || chInput == KEY_ESCAPE) {
layer = 1;
sel1 = 1;
}
if (chInput == KEY_UP || chInput == 'k' || chInput == '\n') {
if (sel2 == 0 || sel2 == 2) {
if (!u.create(this, (sel2 == 0) ? PLTYPE_CUSTOM : PLTYPE_RANDOM)) {
u = player();
delwin(w_open);
return (opening_screen());
}
werase(w_background);
wrefresh(w_background);
start_game();
start = true;
} else if (sel2 == 1) {
layer = 3;
sel1 = 0;
print_menu_items(w_open, vSubItems, sel2, iMenuOffsetY-2, iMenuOffsetX+7);
}
}
} else if (sel1 == 2) { // Load Character
if (savegames.size() == 0)
mvwprintz(w_open, iMenuOffsetY - 2, 19 + iMenuOffsetX, c_red, "No save games found!");
else {
for (int i = 0; i < savegames.size(); i++) {
int line = iMenuOffsetY - 2 - i;
mvwprintz(w_open, line, 19 + iMenuOffsetX, (sel2 == i ? h_white : c_white), savegames[i].c_str());
}
}
wrefresh(w_open);
refresh();
input = get_input();
if (savegames.size() == 0 && (input == DirectionS || input == Confirm)) {
layer = 1;
} else if (input == DirectionS) {
if (sel2 > 0)
sel2--;
else
sel2 = savegames.size() - 1;
} else if (input == DirectionN) {
if (sel2 < savegames.size() - 1)
sel2++;
else
sel2 = 0;
} else if (input == DirectionW || input == Cancel) {
layer = 1;
}
if (input == DirectionE || input == Confirm) {
if (sel2 >= 0 && sel2 < savegames.size()) {
werase(w_background);
wrefresh(w_background);
load(savegames[sel2]);
start = true;
}
}
} else if (sel1 == 3) { // Delete world
if (query_yn("Delete the world and all saves?")) {
delete_save();
savegames.clear();
MAPBUFFER.reset();
MAPBUFFER.make_volatile();
}
layer = 1;
} else if (sel1 == 4) { // Special game
for (int i = 1; i < NUM_SPECIAL_GAMES; i++) {
mvwprintz(w_open, iMenuOffsetY-i-1, 34 + iMenuOffsetX, (sel2 == i-1 ? h_white : c_white),
special_game_name( special_game_id(i) ).c_str());
}
wrefresh(w_open);
refresh();
input = get_input();
if (input == DirectionS) {
if (sel2 > 0)
sel2--;
else
sel2 = NUM_SPECIAL_GAMES - 2;
} else if (input == DirectionN) {
if (sel2 < NUM_SPECIAL_GAMES - 2)
sel2++;
else
sel2 = 0;
} else if (input == DirectionW || input == Cancel) {
layer = 1;
}
if (input == DirectionE || input == Confirm) {
if (sel2 >= 0 && sel2 < NUM_SPECIAL_GAMES - 1) {
delete gamemode;
gamemode = get_special_game( special_game_id(sel2+1) );
if (!gamemode->init(this)) {
delete gamemode;
gamemode = new special_game;
u = player();
delwin(w_open);
return (opening_screen());
}
start = true;
}
}
}
} else if (layer == 3) { // Character Templates
if (templates.size() == 0)
mvwprintz(w_open, iMenuOffsetY-4, iMenuOffsetX+27, c_red, "No templates found!");
else {
for (int i = 0; i < templates.size(); i++) {
int line = iMenuOffsetY - 4 - i;
mvwprintz(w_open, line, 27 + iMenuOffsetX, (sel1 == i ? h_white : c_white), templates[i].c_str());
}
}
wrefresh(w_open);
refresh();
input = get_input();
if (input == DirectionS) {
if (sel1 > 0)
sel1--;
else
sel1 = templates.size() - 1;
} else if (templates.size() == 0 && (input == DirectionN || input == Confirm)) {
sel1 = 1;
layer = 2;
print_menu(w_open, sel1, iMenuOffsetX, iMenuOffsetY);
} else if (input == DirectionN) {
if (sel1 < templates.size() - 1)
sel1++;
else
sel1 = 0;
} else if (input == DirectionW || input == Cancel || templates.size() == 0) {
sel1 = 1;
layer = 2;
print_menu(w_open, sel1, iMenuOffsetX, iMenuOffsetY);
} else if (input == DirectionE || input == Confirm) {
if (!u.create(this, PLTYPE_TEMPLATE, templates[sel1])) {
u = player();
delwin(w_open);
return (opening_screen());
}
werase(w_background);
wrefresh(w_background);
start_game();
start = true;
}
}
}
delwin(w_open);
if (start == false)
uquit = QUIT_MENU;
return start;
}