DEADSOFTWARE

save: move save code to separated file
[flatwaifu.git] / src / menu.c
1 /*
2 Copyright (C) Prikol Software 1996-1997
3 Copyright (C) Aleksey Volynskov 1996-1997
4 Copyright (C) <ARembo@gmail.com> 2011
6 This file is part of the Doom2D:Rembo project.
8 Doom2D:Rembo is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as
10 published by the Free Software Foundation.
12 Doom2D:Rembo is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/> or
19 write to the Free Software Foundation, Inc.,
20 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
23 #include "glob.h"
24 #include "files.h"
25 #include "memory.h"
26 #include "error.h"
27 #include "sound.h"
28 #include "view.h"
29 #include "player.h"
30 #include "switch.h"
31 #include "menu.h"
32 #include "misc.h"
33 #include "render.h"
34 #include "config.h"
35 #include "game.h"
36 #include "player.h"
37 #include "sound.h"
38 #include "music.h"
39 #include "input.h"
40 #include "system.h"
42 #include "save.h"
44 #include <stdio.h>
45 #include <string.h>
46 #include <stdlib.h>
47 #include <assert.h>
49 #define PCOLORN 10
50 static byte pcolortab[PCOLORN] = {
51 0x18, 0x20, 0x40, 0x58, 0x60, 0x70, 0x80, 0xB0, 0xC0, 0xD0
52 };
53 static int p1color = 5;
54 static int p2color = 4;
56 byte _warp;
58 #define MAX_STACK 8
59 static struct {
60 const menu_t *m;
61 } stack[MAX_STACK];
62 static int stack_p = -1;
64 #define GM_MAX_INPUT 24
65 char ibuf[GM_MAX_INPUT];
66 byte input;
67 int icur;
68 int imax;
69 static byte cbuf[32];
70 short lastkey;
72 #define QSND_NUM 14
73 static int qsnd[QSND_NUM];
74 static snd_t *csnd1, *csnd2, *msnd1, *msnd2, *msnd3, *msnd4, *msnd5, *msnd6;
75 static snd_t *voc;
76 static int voc_ch;
78 static void GM_stop (void) {
79 if (voc != NULL) {
80 if (voc_ch) {
81 S_stop(voc_ch);
82 voc_ch = 0;
83 }
84 S_free(voc);
85 voc = NULL;
86 }
87 }
89 static int GM_say (const char nm[8]) {
90 snd_t *snd = S_load(nm);
91 if (snd) {
92 GM_stop();
93 voc = S_load(nm);
94 voc_ch = S_play(voc, 0, 255);
95 }
96 return 1;
97 }
99 int GM_init_int0 (menu_msg_t *msg, int i, int a, int b, int s) {
100 assert(msg != NULL);
101 msg->integer.i = i;
102 msg->integer.a = a;
103 msg->integer.b = b;
104 msg->integer.s = s;
105 return 1;
108 int GM_init_int (menu_msg_t *msg, int i, int a, int b, int s) {
109 assert(msg != NULL);
110 assert(a <= b);
111 assert(s >= 0);
112 return GM_init_int0(msg, min(max(i, a), b), a, b, s);
115 int GM_init_str (menu_msg_t *msg, char *str, int maxlen) {
116 assert(msg != NULL);
117 assert(str != NULL);
118 assert(maxlen >= 0);
119 msg->string.s = str;
120 msg->string.maxlen = maxlen;
121 return 1;
124 int basic_menu_handler (menu_msg_t *msg, byte type, char *title, char *say, int n, int *cur) {
125 assert(msg != NULL);
126 assert(type == GM_BIG || type == GM_SMALL);
127 assert(title != NULL);
128 assert(n >= 0);
129 assert(cur != NULL);
130 switch (msg->type) {
131 case GM_QUERY: return GM_init_int0(msg, *cur, n, n, type);
132 case GM_GETTITLE: return GM_init_str(msg, title, strlen(title));
133 case GM_ENTER: return say ? GM_say(say) : 1;
134 case GM_UP: *cur = GM_CYCLE(*cur - 1, 0, n - 1); return 1;
135 case GM_DOWN: *cur = GM_CYCLE(*cur + 1, 0, n - 1); return 1;
137 return 0;
140 int simple_menu_handler (menu_msg_t *msg, int i, int n, const simple_menu_t *m, int *cur) {
141 assert(msg != NULL);
142 assert(n >= 0);
143 assert(i >= 0 && i < n);
144 assert(m != NULL);
145 assert(cur != NULL);
146 switch (msg->type) {
147 case GM_GETENTRY: return GM_init_int0(msg, m->type == GM_SMALL ? GM_SMALL_BUTTON : GM_BUTTON, 0, 0, 0);
148 case GM_GETCAPTION: return GM_init_str(msg, m->entries[i].caption, strlen(m->entries[i].caption));
149 case GM_SELECT: return m->entries[i].submenu ? GM_push(m->entries[i].submenu) : 1;
151 return basic_menu_handler(msg, m->type, m->title, m->say, n, cur);
154 static int start_game (int twoplayers, int dm, int level) {
155 _2pl = twoplayers;
156 g_dm = dm;
157 g_map = level ? level : 1;
158 PL_reset();
159 pl1.color = pcolortab[p1color];
160 pl2.color = pcolortab[p2color];
161 G_start();
162 return GM_popall();
165 static int new_game_menu_handler (menu_msg_t *msg, const menu_t *m, void *data, int i) {
166 static int cur;
167 enum { ONEPLAYER, TWOPLAYERS, DEATHMATCH, __NUM__ };
168 static const simple_menu_t sm = {
169 GM_BIG, "New Game", "_NEWGAME",
171 { "One Player", NULL },
172 { "Two Players", NULL },
173 { "Deathmatch", NULL },
175 };
176 if (msg->type == GM_SELECT) {
177 switch (i) {
178 case ONEPLAYER: GM_say("_1PLAYER"); return start_game(0, 0, _warp);
179 case TWOPLAYERS: GM_say("_2PLAYER"); return start_game(1, 0, _warp);
180 case DEATHMATCH: GM_say("_DM"); return start_game(1, 1, _warp);
181 // GM_say("_COOP");
184 return simple_menu_handler(msg, i, __NUM__, &sm, &cur);
187 static const menu_t new_game_menu = {
188 NULL, &new_game_menu_handler
189 };
191 static int load_game_menu_handler (menu_msg_t *msg, const menu_t *m, void *data, int i) {
192 static int cur;
193 const int max_slots = 7;
194 assert(i >= 0 && i < max_slots);
195 switch (msg->type) {
196 case GM_ENTER: F_getsavnames(); break;
197 case GM_GETENTRY: return GM_init_int0(msg, GM_TEXTFIELD_BUTTON, 0, 0, 0);
198 case GM_GETSTR: return GM_init_str(msg, (char*)savname[i], 24);
199 case GM_SELECT:
200 if (savok[i]) {
201 load_game(i);
202 GM_popall();
204 return 1;
206 return basic_menu_handler(msg, GM_BIG, "Load game", "_OLDGAME", max_slots, &cur);
209 static const menu_t load_game_menu = {
210 NULL, &load_game_menu_handler
211 };
213 static int save_game_menu_handler (menu_msg_t *msg, const menu_t *m, void *data, int i) {
214 static int cur;
215 const int max_slots = 7;
216 assert(i >= 0 && i < max_slots);
217 switch (msg->type) {
218 case GM_ENTER:
219 if (g_st == GS_GAME) {
220 F_getsavnames();
221 break;
222 } else {
223 return GM_pop();
225 case GM_GETENTRY: return GM_init_int0(msg, GM_TEXTFIELD, 0, 0, 0);
226 case GM_GETSTR: return GM_init_str(msg, (char*)savname[i], 24);
227 case GM_END:
228 if (g_st == GS_GAME) {
229 assert(msg->string.maxlen >= 24);
230 F_savegame(i, msg->string.s);
232 return GM_popall();
234 return basic_menu_handler(msg, GM_BIG, "Save game", "_SAVGAME", max_slots, &cur);
237 static const menu_t save_game_menu = {
238 NULL, &save_game_menu_handler
239 };
241 static int sound_menu_handler (menu_msg_t *msg, const menu_t *m, void *data, int i) {
242 static int cur;
243 enum { VOLUME, __NUM__ };
244 static const simple_menu_t sm = {
245 GM_BIG, "Sound", NULL,
247 { "Volume", NULL },
249 };
250 if (i == VOLUME) {
251 switch (msg->type) {
252 case GM_GETENTRY: return GM_init_int0(msg, GM_SCROLLER, 0, 0, 0);
253 case GM_GETINT: return GM_init_int(msg, snd_vol, 0, 128, 8);
254 case GM_SETINT: S_volume(msg->integer.i); return 1;
257 return simple_menu_handler(msg, i, __NUM__, &sm, &cur);
260 static const menu_t sound_menu = {
261 NULL, &sound_menu_handler
262 };
264 static int music_menu_handler (menu_msg_t *msg, const menu_t *m, void *data, int i) {
265 static int cur;
266 enum { VOLUME, MUSIC, __NUM__ };
267 static const simple_menu_t sm = {
268 GM_BIG, "Music", NULL,
270 { "Volume", NULL },
271 { "Music: ", NULL },
273 };
274 if (i == VOLUME) {
275 switch (msg->type) {
276 case GM_GETENTRY: return GM_init_int0(msg, GM_SCROLLER, 0, 0, 0);
277 case GM_GETINT: return GM_init_int(msg, mus_vol, 0, 128, 8);
278 case GM_SETINT: S_volumemusic(msg->integer.i); return 1;
280 } else if (i == MUSIC) {
281 switch (msg->type) {
282 case GM_GETSTR: return GM_init_str(msg, g_music, strlen(g_music));
283 case GM_SELECT:
284 F_freemus();
285 F_nextmus(g_music);
286 F_loadmus(g_music);
287 S_startmusic(music_time * 2); // ???
288 return 1;
291 return simple_menu_handler(msg, i, __NUM__, &sm, &cur);
294 static const menu_t music_menu = {
295 NULL, &music_menu_handler
296 };
298 static int options_menu_handler (menu_msg_t *msg, const menu_t *m, void *data, int i) {
299 static int cur;
300 enum { VIDEO, SOUND, MUSIC, __NUM__ };
301 static const simple_menu_t sm = {
302 GM_BIG, "Options", NULL,
304 { "Video", NULL },
305 { "Sound", &sound_menu },
306 { "Music", &music_menu },
308 };
309 if (msg->type == GM_SELECT) {
310 if (i == VIDEO) {
311 const menu_t *mm = R_menu();
312 return mm ? GM_push(mm) : 1;
315 return simple_menu_handler(msg, i, __NUM__, &sm, &cur);
318 static const menu_t options_menu = {
319 NULL, &options_menu_handler
320 };
322 static int exit_menu_handler (menu_msg_t *msg, const menu_t *m, void *data, int i) {
323 static int cur;
324 enum { YES, NO, __NUM__ };
325 static const simple_menu_t sm = {
326 GM_SMALL, "You are sure?", NULL,
328 { "Yes", NULL },
329 { "No", NULL },
331 };
332 if (msg->type == GM_ENTER) {
333 return GM_say(rand() & 1 ? "_EXIT1" : "_EXIT2");
334 } else if (msg->type == GM_SELECT) {
335 switch (i) {
336 case YES:
337 F_freemus();
338 GM_stop();
339 Z_sound(S_get(qsnd[myrand(QSND_NUM)]), 255);
340 S_wait();
341 ERR_quit();
342 return 1;
343 case NO:
344 return GM_pop();
347 return simple_menu_handler(msg, i, __NUM__, &sm, &cur);
350 static const menu_t exit_menu = {
351 NULL, &exit_menu_handler
352 };
354 static int main_menu_handler (menu_msg_t *msg, const menu_t *m, void *data, int i) {
355 static int cur;
356 enum { NEWGAME, OLDGAME, SAVEGAME, OPTIONS, EXIT, __NUM__ };
357 static const simple_menu_t sm = {
358 GM_BIG, "Menu", NULL,
360 { "New Game", &new_game_menu },
361 { "Load Game", &load_game_menu },
362 { "Save Game", &save_game_menu },
363 { "Options", &options_menu },
364 { "Exit", &exit_menu },
366 };
367 return simple_menu_handler(msg, i, __NUM__, &sm, &cur);
370 static const menu_t main_menu = {
371 NULL, &main_menu_handler
372 };
374 int GM_push (const menu_t *m) {
375 assert(m != NULL);
376 assert(stack_p >= -1);
377 assert(stack_p < MAX_STACK - 1);
378 menu_msg_t msg;
379 stack_p += 1;
380 stack[stack_p].m = m;
381 msg.type = GM_ENTER;
382 GM_send_this(m, &msg);
383 return 1;
386 int GM_pop (void) {
387 assert(stack_p >= 0);
388 menu_msg_t msg;
389 stack_p -= 1;
390 msg.type = GM_LEAVE;
391 GM_send_this(stack[stack_p + 1].m, &msg);
392 return 1;
395 int GM_popall (void) {
396 int i;
397 for (i = 0; i >= -1; i--) {
398 GM_pop();
400 return 1;
403 const menu_t *GM_get (void) {
404 if (stack_p >= 0) {
405 return stack[stack_p].m;
406 } else {
407 return NULL;
411 static void GM_normalize_message (menu_msg_t *msg) {
412 switch (msg->type) {
413 case GM_SETINT:
414 msg->integer.i = min(max(msg->integer.i, msg->integer.a), msg->integer.b);
415 break;
416 case GM_SETSTR:
417 assert(msg->string.maxlen >= 0);
418 break;
422 int GM_send_this (const menu_t *m, menu_msg_t *msg) {
423 assert(m != NULL);
424 assert(msg != NULL);
425 if (m->handler != NULL) {
426 GM_normalize_message(msg);
427 return m->handler(msg, m, m->data, 0);
429 return 0;
432 int GM_send (const menu_t *m, int i, menu_msg_t *msg) {
433 assert(m != NULL);
434 assert(i >= 0);
435 assert(msg != NULL);
436 if (m->handler != NULL) {
437 GM_normalize_message(msg);
438 return m->handler(msg, m, m->data, i);
440 return 0;
443 void G_code (void) {
444 void *s;
445 s=csnd2;
446 if(memcmp(cbuf+32-5,"IDDQD",5)==0) {
447 PL_hit(&pl1,400,0,HIT_SOME);
448 if(_2pl) PL_hit(&pl2,400,0,HIT_SOME);
449 s=csnd1;
450 }else if(memcmp(cbuf+32-4,"TANK",4)==0) {
451 pl1.life=pl1.armor=200;pl1.drawst|=PL_DRAWARMOR|PL_DRAWLIFE;
452 if(_2pl) {pl2.life=pl2.armor=200;pl2.drawst|=PL_DRAWARMOR|PL_DRAWLIFE;}
453 }else if(memcmp(cbuf+32-8,"BULLFROG",8)==0) {
454 PL_JUMP=(PL_JUMP==10)?20:10;
455 }else if(memcmp(cbuf+32-8,"FORMULA1",8)==0) {
456 PL_RUN=(PL_RUN==8)?24:8;
457 }else if(memcmp(cbuf+32-5,"RAMBO",5)==0) {
458 pl1.ammo=pl1.shel=pl1.rock=pl1.cell=pl1.fuel=30000;
459 pl1.wpns=0x7FF;pl1.drawst|=PL_DRAWWPN|PL_DRAWKEYS;
460 pl1.keys=0x70;
461 if(_2pl) {
462 pl2.ammo=pl2.shel=pl2.rock=pl2.cell=pl1.fuel=30000;
463 pl2.wpns=0x7FF;pl2.drawst|=PL_DRAWWPN|PL_DRAWKEYS;
464 pl2.keys=0x70;
466 }else if(memcmp(cbuf+32-5,"UJHTW",5)==0) {
467 p_immortal=!p_immortal;
468 }else if(memcmp(cbuf+32-9,",TKSQJHTK",9)==0) {
469 p_fly=!p_fly;
470 }else if(memcmp(cbuf+32-6,"CBVCBV",6)==0) {
471 SW_cheat_open();
472 }else if(memcmp(cbuf+32-7,"GOODBYE",7)==0) {
473 g_exit=1;
474 }else if(memcmp(cbuf+32-9,"GJITKYF",7)==0) {
475 if(cbuf[30]>='0' && cbuf[30]<='9' && cbuf[31]>='0' && cbuf[31]<='9') {
476 g_map=(cbuf[30]=='0')?0:(cbuf[30]-'0')*10;
477 g_map+=(cbuf[31]=='0')?0:(cbuf[31]-'0');
478 G_start();
480 }else return;
481 memset(cbuf,0,32);
482 Z_sound(s,128);
485 static int strnlen (const char *s, int len) {
486 int i = 0;
487 while (i < len && s[i] != 0) {
488 i++;
490 return i;
493 static int state_for_anykey (int x) {
494 return x == GS_TITLE || x == GS_ENDSCR;
497 int GM_act (void) {
498 menu_msg_t msg;
499 int n, cur, type;
500 const menu_t *m = GM_get ();
501 if (m == NULL) {
502 if (lastkey == KEY_ESCAPE || (state_for_anykey(g_st) && lastkey != KEY_UNKNOWN)) {
503 GM_push(&main_menu);
504 Z_sound(msnd3, 128);
506 } else {
507 msg.type = GM_QUERY;
508 assert(GM_send_this(m, &msg));
509 cur = msg.integer.i;
510 n = msg.integer.a;
511 msg.type = GM_GETENTRY;
512 assert(GM_send(m, cur, &msg));
513 type = msg.integer.i;
514 switch (lastkey) {
515 case KEY_ESCAPE:
516 if (type == GM_TEXTFIELD && input) {
517 input = 0;
518 Y_disable_text_input();
519 msg.type = GM_CANCEL;
520 GM_send(m, cur, &msg);
521 } else {
522 GM_pop();
523 Z_sound(msnd4, 128);
525 break;
526 case KEY_UP:
527 case KEY_DOWN:
528 msg.type = lastkey == KEY_UP ? GM_UP : GM_DOWN;
529 if (GM_send(m, cur, &msg)) {
530 Z_sound(msnd1, 128);
532 break;
533 case KEY_LEFT:
534 case KEY_RIGHT:
535 if (type == GM_SCROLLER) {
536 msg.integer.type = GM_GETINT;
537 if (GM_send(m, cur, &msg)) {
538 msg.integer.type = GM_SETINT;
539 msg.integer.i += lastkey == KEY_LEFT ? -msg.integer.s : msg.integer.s;
540 msg.integer.i = min(max(msg.integer.i, msg.integer.a), msg.integer.b);
541 if (GM_send(m, cur, &msg)) {
542 Z_sound(lastkey == KEY_LEFT ? msnd5 : msnd6, 255);
545 } else if (type == GM_TEXTFIELD) {
546 //if (input) {
547 // icur += lastkey == KEY_LEFT ? -1 : +1;
548 // icur = min(max(icur, 0), strnlen(ibuf, imax));
549 //}
551 break;
552 case KEY_BACKSPACE:
553 if (type == GM_TEXTFIELD) {
554 if (input && icur > 0) {
555 // FIXIT buffers in strncpy must not overlap
556 strncpy(&ibuf[icur - 1], &ibuf[icur], imax - icur);
557 ibuf[imax - 1] = 0;
558 icur -= 1;
561 break;
562 case KEY_RETURN:
563 if (type == GM_TEXTFIELD) {
564 if (input) {
565 input = 0;
566 Y_disable_text_input();
567 msg.type = GM_END;
568 msg.string.s = ibuf;
569 msg.string.maxlen = imax;
570 GM_send(m, cur, &msg);
571 } else {
572 msg.type = GM_GETSTR;
573 if (GM_send(m, cur, &msg)) {
574 imax = min(msg.string.maxlen, GM_MAX_INPUT);
575 strncpy(ibuf, msg.string.s, imax);
576 icur = strnlen(ibuf, imax);
577 } else {
578 memset(ibuf, 0, GM_MAX_INPUT);
579 imax = GM_MAX_INPUT;
580 icur = 0;
582 input = 1;
583 Y_enable_text_input();
584 msg.type = GM_BEGIN;
585 GM_send(m, cur, &msg);
587 Z_sound(msnd2, 128);
588 } else {
589 msg.type = GM_SELECT;
590 if (GM_send(m, cur, &msg)) {
591 Z_sound(msnd2, 128);
594 break;
597 lastkey = KEY_UNKNOWN;
598 return m != NULL;
601 void GM_input (int ch) {
602 if (ch != 0 && input) {
603 if (icur < imax) {
604 ibuf[icur] = ch;
605 icur += 1;
606 if (icur < imax) {
607 ibuf[icur] = 0;
613 void GM_key (int key, int down) {
614 int i;
615 if (down) {
616 lastkey = key;
617 if (!_2pl || cheat) {
618 for (i = 0; i < 31; i++) {
619 cbuf[i] = cbuf[i + 1];
621 if (key >= KEY_0 && key <= KEY_9) {
622 cbuf[31] = key - KEY_0 + '0';
623 } else if (key >= KEY_A && key <= KEY_Z) {
624 cbuf[31] = key - KEY_A + 'A';
625 } else {
626 cbuf[31] = 0;
632 void GM_init (void) {
633 int i;
634 char s[8];
635 static const char nm[QSND_NUM][6] = {
636 "CYBSIT", "KNTDTH", "MNPAIN", "PEPAIN", "SLOP", "MANSIT", "BOSPN", "VILACT",
637 "PLFALL", "BGACT", "BGDTH2", "POPAIN", "SGTATK", "VILDTH"
638 };
639 s[0] = 'D';
640 s[1] = 'S';
641 for (i = 0; i < QSND_NUM; ++i) {
642 memcpy(s + 2, nm[i], 6);
643 qsnd[i] = F_getresid(s);
645 csnd1 = Z_getsnd("HAHA1");
646 csnd2 = Z_getsnd("RADIO");
647 msnd1 = Z_getsnd("PSTOP");
648 msnd2 = Z_getsnd("PISTOL");
649 msnd3 = Z_getsnd("SWTCHN");
650 msnd4 = Z_getsnd("SWTCHX");
651 msnd5 = Z_getsnd("SUDI");
652 msnd6 = Z_getsnd("TUDI");
653 F_loadmus("MENU");
654 S_startmusic(0);