DEADSOFTWARE

ab0e5735fb24006d2465f7f94ba53ad83752c23a
[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 <stdio.h>
43 #include <string.h>
44 #include <stdlib.h>
45 #include <assert.h>
47 #define PCOLORN 10
48 static byte pcolortab[PCOLORN] = {
49 0x18, 0x20, 0x40, 0x58, 0x60, 0x70, 0x80, 0xB0, 0xC0, 0xD0
50 };
51 static int p1color = 5;
52 static int p2color = 4;
54 byte _warp;
56 #define MAX_STACK 8
57 static struct {
58 int n;
59 const new_menu_t *m;
60 } stack[MAX_STACK];
61 static int stack_p = -1;
63 #define GM_MAX_INPUT 24
64 char ibuf[GM_MAX_INPUT];
65 byte input;
66 int icur;
67 static int imax;
68 static byte cbuf[32];
69 short lastkey;
71 #define QSND_NUM 14
72 static int qsnd[QSND_NUM];
73 static snd_t *csnd1, *csnd2, *msnd1, *msnd2, *msnd3, *msnd4, *msnd5, *msnd6;
74 static snd_t *voc;
75 static int voc_ch;
77 static void GM_stop (void) {
78 if (voc != NULL) {
79 if (voc_ch) {
80 S_stop(voc_ch);
81 voc_ch = 0;
82 }
83 S_free(voc);
84 voc = NULL;
85 }
86 }
88 static void GM_say (const char nm[8]) {
89 snd_t *snd = S_load(nm);
90 if (snd) {
91 GM_stop();
92 voc = S_load(nm);
93 voc_ch = S_play(voc, 0, 255);
94 }
95 }
97 static int GM_init_int (new_msg_t *msg, int i, int a, int b, int s) {
98 assert(msg != NULL);
99 assert(a <= b);
100 assert(s >= 0);
101 msg->integer.i = min(max(i, a), b);
102 msg->integer.a = a;
103 msg->integer.b = b;
104 msg->integer.s = s;
105 return 1;
108 static int GM_init_str (new_msg_t *msg, char *str, int maxlen) {
109 assert(msg != NULL);
110 assert(str != NULL);
111 assert(maxlen >= 0);
112 msg->string.s = str;
113 msg->string.maxlen = maxlen;
114 return 1;
117 static int GM_newgame_handler (new_msg_t *msg, const new_menu_t *m, void *data) {
118 assert(msg != NULL);
119 intptr_t i = (intptr_t)data;
120 switch (msg->type) {
121 case GM_ENTER:
122 GM_say("_NEWGAME");
123 return 1;
124 case GM_SELECT:
125 _2pl = 0;
126 g_dm = 0;
127 switch (i) {
128 case 0: GM_say("_1PLAYER"); break;
129 case 1: GM_say("_2PLAYER"); break;
130 case 2: GM_say("_DM"); break;
131 // GM_say("_COOP");
133 switch (i) {
134 case 2: // DEATHMATCH
135 g_dm = 1;
136 case 1: // COOPERATIVE
137 _2pl = 1;
138 case 0: // SINGLEPLAYER
139 g_map = _warp ? _warp : 1;
140 PL_reset();
141 pl1.color = pcolortab[p1color];
142 pl2.color = pcolortab[p2color];
143 G_start();
144 GM_popall();
145 return 1;
147 break;
149 return 0;
152 static int GM_var_handler (new_msg_t *msg, const new_menu_t *m, void *data) {
153 assert(msg != NULL);
154 if (data == &snd_vol) {
155 switch (msg->type) {
156 case GM_GETINT: return GM_init_int(msg, snd_vol, 0, 128, 8);
157 case GM_SETINT: S_volume(msg->integer.i); return 1;
159 } else if (data == &mus_vol) {
160 switch (msg->type) {
161 case GM_GETINT: return GM_init_int(msg, mus_vol, 0, 128, 8);
162 case GM_SETINT: S_volumemusic(msg->integer.i); return 1;
164 } else if (data == g_music) {
165 switch (msg->type) {
166 case GM_GETSTR:
167 return GM_init_str(msg, g_music, 8);
168 case GM_SELECT:
169 F_freemus();
170 F_nextmus(g_music);
171 F_loadmus(g_music);
172 S_startmusic(music_time * 2); // ???
173 return 1;
176 return 0;
179 static int GM_load_handler (new_msg_t *msg, const new_menu_t *m, void *data) {
180 assert(msg != NULL);
181 intptr_t i = (intptr_t)data;
182 switch (msg->type) {
183 case GM_ENTER:
184 F_getsavnames();
185 return 1;
186 case GM_GETSTR:
187 return GM_init_str(msg, (char*)savname[i], 24);
188 case GM_SELECT:
189 if (savok[i]) {
190 load_game(i);
191 GM_popall();
192 return 1;
194 break;
196 return 0;
199 static int GM_save_handler (new_msg_t *msg, const new_menu_t *m, void *data) {
200 assert(msg != NULL);
201 intptr_t i = (intptr_t)data;
202 switch (msg->type) {
203 case GM_ENTER:
204 F_getsavnames();
205 return 1;
206 case GM_GETSTR:
207 F_getsavnames();
208 return GM_init_str(msg, (char*)savname[i], 24);
209 case GM_END:
210 if (g_st == GS_GAME) {
211 F_savegame(i, msg->string.s); // TODO check size
212 GM_popall();
213 return 1;
215 break;
217 return 0;
220 static int GM_exit_handler (new_msg_t *msg, const new_menu_t *m, void *data) {
221 switch (msg->type) {
222 case GM_ENTER:
223 GM_say(rand() & 1 ? "_EXIT1" : "_EXIT2");
224 return 1;
225 case GM_SELECT:
226 if (data != NULL) {
227 F_freemus();
228 GM_stop();
229 Z_sound(S_get(qsnd[myrand(QSND_NUM)]), 255);
230 S_wait();
231 ERR_quit();
232 } else {
233 GM_pop();
235 return 1;
237 return 0;
240 static const new_menu_t newgame_menu = {
241 GM_BIG, "New game", NULL, NULL,
243 { GM_BUTTON, "One player", (void*)0, &GM_newgame_handler, NULL },
244 { GM_BUTTON, "Two players", (void*)1, &GM_newgame_handler, NULL },
245 { GM_BUTTON, "Deathmatch", (void*)2, &GM_newgame_handler, NULL },
246 { 0, NULL, NULL, NULL, NULL } // end
248 }, loadgame_menu = {
249 GM_BIG, "Load game", NULL, &GM_load_handler,
251 { GM_TEXTFIELD_BUTTON, "", (void*)0, &GM_load_handler, NULL },
252 { GM_TEXTFIELD_BUTTON, "", (void*)1, &GM_load_handler, NULL },
253 { GM_TEXTFIELD_BUTTON, "", (void*)2, &GM_load_handler, NULL },
254 { GM_TEXTFIELD_BUTTON, "", (void*)3, &GM_load_handler, NULL },
255 { GM_TEXTFIELD_BUTTON, "", (void*)4, &GM_load_handler, NULL },
256 { GM_TEXTFIELD_BUTTON, "", (void*)5, &GM_load_handler, NULL },
257 { GM_TEXTFIELD_BUTTON, "", (void*)6, &GM_load_handler, NULL },
258 { 0, NULL, NULL, NULL, NULL } // end
260 }, savegame_menu = {
261 GM_BIG, "Save game", NULL, &GM_save_handler,
263 { GM_TEXTFIELD, "", (void*)0, &GM_save_handler, NULL },
264 { GM_TEXTFIELD, "", (void*)1, &GM_save_handler, NULL },
265 { GM_TEXTFIELD, "", (void*)2, &GM_save_handler, NULL },
266 { GM_TEXTFIELD, "", (void*)3, &GM_save_handler, NULL },
267 { GM_TEXTFIELD, "", (void*)4, &GM_save_handler, NULL },
268 { GM_TEXTFIELD, "", (void*)5, &GM_save_handler, NULL },
269 { GM_TEXTFIELD, "", (void*)6, &GM_save_handler, NULL },
270 { 0, NULL, NULL, NULL, NULL } // end
272 }, sound_menu = {
273 GM_BIG, "Sound", NULL, NULL,
275 { GM_SCROLLER, "Volume", &snd_vol, &GM_var_handler, NULL },
276 { 0, NULL, NULL, NULL, NULL } // end
278 }, music_menu = {
279 GM_BIG, "Music", NULL, NULL,
281 { GM_SCROLLER, "Volume", &mus_vol, &GM_var_handler, NULL },
282 { GM_BUTTON, "Music:", g_music, &GM_var_handler, NULL },
283 { 0, NULL, NULL, NULL, NULL } // end
285 }, options_menu = {
286 GM_BIG, "Options", NULL, NULL,
288 //{ GM_BUTTON, "Video", NULL, NULL, NULL },
289 { GM_BUTTON, "Sound", NULL, NULL, &sound_menu },
290 { GM_BUTTON, "Music", NULL, NULL, &music_menu },
291 { 0, NULL, NULL, NULL, NULL } // end
293 }, exit_menu = {
294 GM_SMALL, "You are sure?", NULL, &GM_exit_handler,
296 { GM_SMALL_BUTTON, "Yes", (void*)1, &GM_exit_handler, NULL },
297 { GM_SMALL_BUTTON, "No", (void*)0, &GM_exit_handler, NULL },
298 { 0, NULL, NULL, NULL, NULL } // end
300 }, main_menu = {
301 GM_BIG, "Menu", NULL, NULL,
303 { GM_BUTTON, "New game", NULL, NULL, &newgame_menu },
304 { GM_BUTTON, "Load game", NULL, NULL, &loadgame_menu },
305 { GM_BUTTON, "Save game", NULL, NULL, &savegame_menu },
306 { GM_BUTTON, "Options", NULL, NULL, &options_menu },
307 { GM_BUTTON, "Exit", NULL, NULL, &exit_menu },
308 { 0, NULL, NULL, NULL, NULL } // end
310 };
312 void GM_push (const new_menu_t *m) {
313 assert(m != NULL);
314 assert(stack_p >= -1);
315 assert(stack_p < MAX_STACK - 1);
316 new_msg_t msg;
317 stack_p += 1;
318 if (stack[stack_p].m != m) {
319 stack[stack_p].n = 0;
320 stack[stack_p].m = m;
322 msg.type = GM_ENTER;
323 GM_send_this(m, &msg);
326 void GM_pop (void) {
327 assert(stack_p >= 0);
328 new_msg_t msg;
329 stack_p -= 1;
330 msg.type = GM_LEAVE;
331 GM_send_this(stack[stack_p + 1].m, &msg);
334 void GM_popall (void) {
335 int i;
336 for (i = 0; i >= -1; i--) {
337 GM_pop();
341 const new_menu_t *GM_get (void) {
342 if (stack_p >= 0) {
343 return stack[stack_p].m;
344 } else {
345 return NULL;
349 int GM_geti (void) {
350 if (stack_p >= 0) {
351 return stack[stack_p].n;
352 } else {
353 return 0;
357 static void GM_normalize_message (new_msg_t *msg) {
358 switch (msg->type) {
359 case GM_SETINT:
360 msg->integer.i = min(max(msg->integer.i, msg->integer.a), msg->integer.b);
361 break;
362 case GM_SETSTR:
363 assert(msg->string.maxlen >= 0);
364 break;
368 int GM_send_this (const new_menu_t *m, new_msg_t *msg) {
369 assert(m != NULL);
370 assert(msg != NULL);
371 if (m->handler != NULL) {
372 GM_normalize_message(msg);
373 return m->handler(msg, m, m->data);
375 return 0;
378 int GM_send (const new_menu_t *m, int i, new_msg_t *msg) {
379 assert(m != NULL);
380 assert(i >= 0);
381 assert(msg != NULL);
382 const new_var_t *v = &m->entries[i];
383 if (v->handler != NULL) {
384 GM_normalize_message(msg);
385 return v->handler(msg, m, v->data);
387 return 0;
390 void G_code (void) {
391 void *s;
392 s=csnd2;
393 if(memcmp(cbuf+32-5,"IDDQD",5)==0) {
394 PL_hit(&pl1,400,0,HIT_SOME);
395 if(_2pl) PL_hit(&pl2,400,0,HIT_SOME);
396 s=csnd1;
397 }else if(memcmp(cbuf+32-4,"TANK",4)==0) {
398 pl1.life=pl1.armor=200;pl1.drawst|=PL_DRAWARMOR|PL_DRAWLIFE;
399 if(_2pl) {pl2.life=pl2.armor=200;pl2.drawst|=PL_DRAWARMOR|PL_DRAWLIFE;}
400 }else if(memcmp(cbuf+32-8,"BULLFROG",8)==0) {
401 PL_JUMP=(PL_JUMP==10)?20:10;
402 }else if(memcmp(cbuf+32-8,"FORMULA1",8)==0) {
403 PL_RUN=(PL_RUN==8)?24:8;
404 }else if(memcmp(cbuf+32-5,"RAMBO",5)==0) {
405 pl1.ammo=pl1.shel=pl1.rock=pl1.cell=pl1.fuel=30000;
406 pl1.wpns=0x7FF;pl1.drawst|=PL_DRAWWPN|PL_DRAWKEYS;
407 pl1.keys=0x70;
408 if(_2pl) {
409 pl2.ammo=pl2.shel=pl2.rock=pl2.cell=pl1.fuel=30000;
410 pl2.wpns=0x7FF;pl2.drawst|=PL_DRAWWPN|PL_DRAWKEYS;
411 pl2.keys=0x70;
413 }else if(memcmp(cbuf+32-5,"UJHTW",5)==0) {
414 p_immortal=!p_immortal;
415 }else if(memcmp(cbuf+32-9,",TKSQJHTK",9)==0) {
416 p_fly=!p_fly;
417 }else if(memcmp(cbuf+32-6,"CBVCBV",6)==0) {
418 SW_cheat_open();
419 }else if(memcmp(cbuf+32-7,"GOODBYE",7)==0) {
420 g_exit=1;
421 }else if(memcmp(cbuf+32-9,"GJITKYF",7)==0) {
422 if(cbuf[30]>='0' && cbuf[30]<='9' && cbuf[31]>='0' && cbuf[31]<='9') {
423 g_map=(cbuf[30]=='0')?0:(cbuf[30]-'0')*10;
424 g_map+=(cbuf[31]=='0')?0:(cbuf[31]-'0');
425 G_start();
427 }else return;
428 memset(cbuf,0,32);
429 Z_sound(s,128);
432 static int count_menu_entries (const new_menu_t *m) {
433 assert(m != NULL);
434 int i = 0;
435 while (m->entries[i].type != 0) {
436 i += 1;
438 return i;
441 static int strnlen (const char *s, int len) {
442 int i = 0;
443 while (i < len && s[i] != 0) {
444 i++;
446 return i;
449 static int state_for_anykey (int x) {
450 return x == GS_TITLE || x == GS_ENDSCR;
453 int GM_act (void) {
454 int n, cur;
455 new_msg_t msg;
456 const new_var_t *v;
457 const new_menu_t *m = GM_get ();
458 if (m == NULL) {
459 if (lastkey == KEY_ESCAPE || (state_for_anykey(g_st) && lastkey != KEY_UNKNOWN)) {
460 GM_push(&main_menu);
461 Z_sound(msnd3, 128);
463 } else {
464 n = count_menu_entries(m);
465 cur = stack[stack_p].n;
466 v = &m->entries[cur];
467 switch (lastkey) {
468 case KEY_ESCAPE:
469 if (v->type == GM_TEXTFIELD && input) {
470 input = 0;
471 Y_disable_text_input();
472 msg.type = GM_CANCEL;
473 GM_send(m, cur, &msg);
474 } else {
475 GM_pop();
476 Z_sound(msnd4, 128);
478 break;
479 case KEY_UP:
480 stack[stack_p].n = stack[stack_p].n - 1 < 0 ? n - 1 : stack[stack_p].n - 1;
481 Z_sound(msnd1, 128);
482 break;
483 case KEY_DOWN:
484 stack[stack_p].n = stack[stack_p].n + 1 >= n ? 0 : stack[stack_p].n + 1;
485 Z_sound(msnd1, 128);
486 break;
487 case KEY_LEFT:
488 case KEY_RIGHT:
489 if (v->type == GM_SCROLLER) {
490 msg.integer.type = GM_GETINT;
491 if (GM_send(m, cur, &msg)) {
492 msg.integer.type = GM_SETINT;
493 msg.integer.i += lastkey == KEY_LEFT ? -msg.integer.s : msg.integer.s;
494 msg.integer.i = min(max(msg.integer.i, msg.integer.a), msg.integer.b);
495 if (GM_send(m, cur, &msg)) {
496 Z_sound(lastkey == KEY_LEFT ? msnd5 : msnd6, 255);
499 } else if (v->type == GM_TEXTFIELD && input) {
500 //icur += lastkey == KEY_LEFT ? -1 : +1;
501 //icur = min(max(icur, 0), strnlen(ibuf, imax));
503 break;
504 case KEY_BACKSPACE:
505 if (v->type == GM_TEXTFIELD && input) {
506 if (icur > 0) {
507 // FIXIT buffers in strncpy must not overlap
508 strncpy(&ibuf[icur - 1], &ibuf[icur], imax - icur);
509 ibuf[imax - 1] = 0;
510 icur -= 1;
513 break;
514 case KEY_RETURN:
515 if (v->submenu != NULL) {
516 GM_push(v->submenu);
517 Z_sound(msnd2, 128);
518 } else if (v->type == GM_TEXTFIELD) {
519 if (input) {
520 input = 0;
521 Y_disable_text_input();
522 msg.type = GM_END;
523 msg.string.s = ibuf;
524 msg.string.maxlen = imax;
525 GM_send(m, cur, &msg);
526 } else {
527 msg.type = GM_GETSTR;
528 if (GM_send(m, cur, &msg)) {
529 imax = min(msg.string.maxlen, GM_MAX_INPUT);
530 strncpy(ibuf, msg.string.s, imax);
531 icur = strnlen(ibuf, imax);
533 input = 1;
534 Y_enable_text_input();
535 msg.type = GM_BEGIN;
536 GM_send(m, cur, &msg);
538 } else {
539 msg.type = GM_SELECT;
540 GM_send(m, cur, &msg);
542 break;
545 lastkey = KEY_UNKNOWN;
546 return m != NULL;
549 void GM_input (int ch) {
550 if (ch != 0 && input) {
551 if (icur < imax) {
552 ibuf[icur] = ch;
553 icur += 1;
554 if (icur < imax) {
555 ibuf[icur] = 0;
561 void GM_key (int key, int down) {
562 int i;
563 if (down) {
564 lastkey = key;
565 if (!_2pl || cheat) {
566 for (i = 0; i < 31; ++i) {
567 cbuf[i] = cbuf[i + 1];
569 //cbuf[31] = get_keychar(key);
574 void GM_init (void) {
575 int i;
576 char s[8];
577 static const char nm[QSND_NUM][6] = {
578 "CYBSIT", "KNTDTH", "MNPAIN", "PEPAIN", "SLOP", "MANSIT", "BOSPN", "VILACT",
579 "PLFALL", "BGACT", "BGDTH2", "POPAIN", "SGTATK", "VILDTH"
580 };
581 s[0] = 'D';
582 s[1] = 'S';
583 for (i = 0; i < QSND_NUM; ++i) {
584 memcpy(s + 2, nm[i], 6);
585 qsnd[i] = F_getresid(s);
587 csnd1 = Z_getsnd("HAHA1");
588 csnd2 = Z_getsnd("RADIO");
589 msnd1 = Z_getsnd("PSTOP");
590 msnd2 = Z_getsnd("PISTOL");
591 msnd3 = Z_getsnd("SWTCHN");
592 msnd4 = Z_getsnd("SWTCHX");
593 msnd5 = Z_getsnd("SUDI");
594 msnd6 = Z_getsnd("TUDI");
595 F_loadmus("MENU");
596 S_startmusic(0);