DEADSOFTWARE

common: move endianness converters to common
[flatwaifu.git] / src / menu.c
1 /* Copyright (C) 2020 SovietPony
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 */
16 #include "glob.h"
17 #include "files.h"
18 #include "memory.h"
19 #include "error.h"
20 #include "sound.h"
21 #include "view.h"
22 #include "player.h"
23 #include "switch.h"
24 #include "menu.h"
25 #include "misc.h"
26 #include "render.h"
27 #include "config.h"
28 #include "game.h"
29 #include "player.h"
30 #include "sound.h"
31 #include "music.h"
32 #include "input.h"
33 #include "system.h"
35 #include "save.h"
37 #include <stdio.h>
38 #include <string.h>
39 #include <stdlib.h>
40 #include <assert.h>
42 #define PCOLORN 10
43 static byte pcolortab[PCOLORN] = {
44 0x18, 0x20, 0x40, 0x58, 0x60, 0x70, 0x80, 0xB0, 0xC0, 0xD0
45 };
46 static int p1color = 5;
47 static int p2color = 4;
49 byte _warp;
51 #define MAX_STACK 8
52 static struct {
53 const menu_t *m;
54 } stack[MAX_STACK];
55 static int stack_p = -1;
57 #define GM_MAX_INPUT 24
58 char ibuf[GM_MAX_INPUT];
59 byte input;
60 int icur;
61 int imax;
62 static byte cbuf[32];
63 short lastkey;
65 #define QSND_NUM 14
66 static int qsnd[QSND_NUM];
67 static snd_t *csnd1, *csnd2, *msnd1, *msnd2, *msnd3, *msnd4, *msnd5, *msnd6;
68 static snd_t *voc;
69 static int voc_ch;
71 static void GM_stop (void) {
72 if (voc != NULL) {
73 if (voc_ch) {
74 S_stop(voc_ch);
75 voc_ch = 0;
76 }
77 S_free(voc);
78 voc = NULL;
79 }
80 }
82 static int GM_say (const char nm[8]) {
83 snd_t *snd = S_load(nm);
84 if (snd) {
85 GM_stop();
86 voc = S_load(nm);
87 voc_ch = S_play(voc, 0, 255);
88 }
89 return 1;
90 }
92 int GM_init_int0 (menu_msg_t *msg, int i, int a, int b, int s) {
93 assert(msg != NULL);
94 msg->integer.i = i;
95 msg->integer.a = a;
96 msg->integer.b = b;
97 msg->integer.s = s;
98 return 1;
99 }
101 int GM_init_int (menu_msg_t *msg, int i, int a, int b, int s) {
102 assert(msg != NULL);
103 assert(a <= b);
104 assert(s >= 0);
105 return GM_init_int0(msg, min(max(i, a), b), a, b, s);
108 int GM_init_str (menu_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 int basic_menu_handler (menu_msg_t *msg, byte type, char *title, char *say, int n, int *cur) {
118 assert(msg != NULL);
119 assert(type == GM_BIG || type == GM_SMALL);
120 assert(title != NULL);
121 assert(n >= 0);
122 assert(cur != NULL);
123 switch (msg->type) {
124 case GM_QUERY: return GM_init_int0(msg, *cur, n, n, type);
125 case GM_GETTITLE: return GM_init_str(msg, title, strlen(title));
126 case GM_ENTER: return say ? GM_say(say) : 1;
127 case GM_UP: *cur = GM_CYCLE(*cur - 1, 0, n - 1); return 1;
128 case GM_DOWN: *cur = GM_CYCLE(*cur + 1, 0, n - 1); return 1;
130 return 0;
133 int simple_menu_handler (menu_msg_t *msg, int i, int n, const simple_menu_t *m, int *cur) {
134 assert(msg != NULL);
135 assert(n >= 0);
136 assert(i >= 0 && i < n);
137 assert(m != NULL);
138 assert(cur != NULL);
139 switch (msg->type) {
140 case GM_GETENTRY: return GM_init_int0(msg, m->type == GM_SMALL ? GM_SMALL_BUTTON : GM_BUTTON, 0, 0, 0);
141 case GM_GETCAPTION: return GM_init_str(msg, m->entries[i].caption, strlen(m->entries[i].caption));
142 case GM_SELECT: return m->entries[i].submenu ? GM_push(m->entries[i].submenu) : 1;
144 return basic_menu_handler(msg, m->type, m->title, m->say, n, cur);
147 static int start_game (int twoplayers, int dm, int level) {
148 _2pl = twoplayers;
149 g_dm = dm;
150 g_map = level ? level : 1;
151 PL_reset();
152 pl1.color = pcolortab[p1color];
153 pl2.color = pcolortab[p2color];
154 G_start();
155 return GM_popall();
158 static int new_game_menu_handler (menu_msg_t *msg, const menu_t *m, int i) {
159 static int cur;
160 enum { ONEPLAYER, TWOPLAYERS, DEATHMATCH, __NUM__ };
161 static const simple_menu_t sm = {
162 GM_BIG, "New Game", "_NEWGAME",
164 { "One Player", NULL },
165 { "Two Players", NULL },
166 { "Deathmatch", NULL },
168 };
169 if (msg->type == GM_SELECT) {
170 switch (i) {
171 case ONEPLAYER: GM_say("_1PLAYER"); return start_game(0, 0, _warp);
172 case TWOPLAYERS: GM_say("_2PLAYER"); return start_game(1, 0, _warp);
173 case DEATHMATCH: GM_say("_DM"); return start_game(1, 1, _warp);
174 // GM_say("_COOP");
177 return simple_menu_handler(msg, i, __NUM__, &sm, &cur);
180 static int load_game_menu_handler (menu_msg_t *msg, const menu_t *m, int i) {
181 static int cur;
182 const int max_slots = 7;
183 assert(i >= 0 && i < max_slots);
184 switch (msg->type) {
185 case GM_ENTER: F_getsavnames(); break;
186 case GM_GETENTRY: return GM_init_int0(msg, GM_TEXTFIELD_BUTTON, 0, 0, 0);
187 case GM_GETSTR: return GM_init_str(msg, (char*)savname[i], 24);
188 case GM_SELECT:
189 if (savok[i]) {
190 load_game(i);
191 GM_popall();
193 return 1;
195 return basic_menu_handler(msg, GM_BIG, "Load game", "_OLDGAME", max_slots, &cur);
198 static int save_game_menu_handler (menu_msg_t *msg, const menu_t *m, int i) {
199 static int cur;
200 const int max_slots = 7;
201 assert(i >= 0 && i < max_slots);
202 switch (msg->type) {
203 case GM_ENTER:
204 if (g_st == GS_GAME) {
205 F_getsavnames();
206 break;
207 } else {
208 return GM_pop();
210 case GM_GETENTRY: return GM_init_int0(msg, GM_TEXTFIELD, 0, 0, 0);
211 case GM_GETSTR: return GM_init_str(msg, (char*)savname[i], 24);
212 case GM_END:
213 if (g_st == GS_GAME) {
214 assert(msg->string.maxlen >= 24);
215 F_savegame(i, msg->string.s);
217 return GM_popall();
219 return basic_menu_handler(msg, GM_BIG, "Save game", "_SAVGAME", max_slots, &cur);
222 typedef struct controls_menu_t {
223 menu_t base;
224 int *pl_keys;
225 } controls_menu_t;
227 static int controls_menu_handler (menu_msg_t *msg, const menu_t *m, int i) {
228 static int cur = 0;
229 static int state = 0;
230 const int max_controls = 9;
231 const char *str = NULL;
232 const controls_menu_t *mm = (controls_menu_t*)m;
233 const char *captions[] = {
234 "Up: ", "Down: ", "Left: ", "Right: ", "Fire: ", "Jump: ", "Prev: ", "Next: ", "Press: "
235 };
236 assert(i >= 0 && i < max_controls);
237 switch (msg->type) {
238 case GM_ENTER:
239 state = 0;
240 break; /* default behavior */
241 case GM_LEAVE:
242 if (state == 0) {
243 break; /* default behavior */
244 } else {
245 state = 0;
246 return 1;
248 case GM_UP:
249 case GM_DOWN:
250 if (state == 0) {
251 break; /* default behavior */
252 } else {
253 return 1; /* ignore */
255 case GM_KEY:
256 if (state != 0 && msg->integer.i != KEY_UNKNOWN) {
257 mm->pl_keys[cur] = msg->integer.i;
258 state = 0;
260 return 1;
261 case GM_GETENTRY:
262 return GM_init_int0(msg, GM_SMALL_BUTTON, 0, 0, 0);
263 case GM_GETCAPTION:
264 return GM_init_str(msg, (char*)captions[i], strlen(captions[i]));
265 case GM_GETSTR:
266 str = state == 0 || i != cur ? I_key_to_string(mm->pl_keys[i]) : "...";
267 return GM_init_str(msg, (char*)str, strlen(str));
268 case GM_SELECT:
269 state = state == 0 ? 1 : 0;
270 return 1;
272 return basic_menu_handler(msg, GM_BIG, "Player 1 controls", NULL, max_controls, &cur);
275 static int options_menu_handler (menu_msg_t *msg, const menu_t *m, int i) {
276 static int cur;
277 const menu_t *mm;
278 enum { VIDEO, SOUND, MUSIC, CONTROLS_1, CONTROLS_2, __NUM__ };
279 static const controls_menu_t c1 = {
280 { controls_menu_handler },
281 &pl1.ku
282 };
283 static const controls_menu_t c2 = {
284 { controls_menu_handler },
285 &pl2.ku
286 };
287 static const simple_menu_t sm = {
288 GM_BIG, "Options", NULL,
290 { "Video", NULL },
291 { "Sound", NULL },
292 { "Music", NULL },
293 { "Controls 1", &c1.base },
294 { "Controls 2", &c2.base },
296 };
297 if (msg->type == GM_SELECT) {
298 switch (i) {
299 case VIDEO: mm = R_menu(); break;
300 case SOUND: mm = S_menu(); break;
301 case MUSIC: mm = MUS_menu(); break;
302 default: mm = NULL;
304 if (mm != NULL) {
305 return GM_push(mm);
308 return simple_menu_handler(msg, i, __NUM__, &sm, &cur);
311 static int exit_menu_handler (menu_msg_t *msg, const menu_t *m, int i) {
312 static int cur;
313 enum { YES, NO, __NUM__ };
314 static const simple_menu_t sm = {
315 GM_SMALL, "You are sure?", NULL,
317 { "Yes", NULL },
318 { "No", NULL },
320 };
321 if (msg->type == GM_ENTER) {
322 return GM_say(rand() & 1 ? "_EXIT1" : "_EXIT2");
323 } else if (msg->type == GM_SELECT) {
324 switch (i) {
325 case YES:
326 MUS_free();
327 GM_stop();
328 Z_sound(S_get(qsnd[myrand(QSND_NUM)]), 255);
329 S_wait();
330 ERR_quit();
331 return 1;
332 case NO:
333 return GM_pop();
336 return simple_menu_handler(msg, i, __NUM__, &sm, &cur);
339 static int main_menu_handler (menu_msg_t *msg, const menu_t *m, int i) {
340 enum { NEWGAME, OLDGAME, SAVEGAME, OPTIONS, EXIT, __NUM__ };
341 assert(i >= 0 && i < __NUM__);
342 static int cur;
343 static const menu_t hm[__NUM__] = {
344 { new_game_menu_handler },
345 { load_game_menu_handler },
346 { save_game_menu_handler },
347 { options_menu_handler},
348 { exit_menu_handler }
349 };
350 static const simple_menu_t sm = {
351 GM_BIG, "Menu", NULL,
353 { "New Game", &hm[NEWGAME] },
354 { "Load Game", &hm[OLDGAME] },
355 { "Save Game", &hm[SAVEGAME] },
356 { "Options", &hm[OPTIONS] },
357 { "Exit", &hm[EXIT] }
359 };
360 return simple_menu_handler(msg, i, __NUM__, &sm, &cur);
363 static const menu_t main_menu = { &main_menu_handler };
365 int GM_push (const menu_t *m) {
366 assert(m != NULL);
367 assert(stack_p >= -1);
368 assert(stack_p < MAX_STACK - 1);
369 menu_msg_t msg;
370 stack_p += 1;
371 stack[stack_p].m = m;
372 msg.type = GM_ENTER;
373 GM_send_this(m, &msg);
374 return 1;
377 int GM_pop (void) {
378 assert(stack_p >= 0);
379 menu_msg_t msg;
380 stack_p -= 1;
381 msg.type = GM_LEAVE;
382 GM_send_this(stack[stack_p + 1].m, &msg);
383 return 1;
386 int GM_popall (void) {
387 int i;
388 for (i = 0; i >= -1; i--) {
389 GM_pop();
391 return 1;
394 const menu_t *GM_get (void) {
395 if (stack_p >= 0) {
396 return stack[stack_p].m;
397 } else {
398 return NULL;
402 static void GM_normalize_message (menu_msg_t *msg) {
403 switch (msg->type) {
404 case GM_SETINT:
405 msg->integer.i = min(max(msg->integer.i, msg->integer.a), msg->integer.b);
406 break;
407 case GM_SETSTR:
408 assert(msg->string.maxlen >= 0);
409 break;
413 int GM_send_this (const menu_t *m, menu_msg_t *msg) {
414 assert(m != NULL);
415 assert(msg != NULL);
416 if (m->handler != NULL) {
417 GM_normalize_message(msg);
418 return m->handler(msg, m, 0);
420 return 0;
423 int GM_send (const menu_t *m, int i, menu_msg_t *msg) {
424 assert(m != NULL);
425 assert(i >= 0);
426 assert(msg != NULL);
427 if (m->handler != NULL) {
428 GM_normalize_message(msg);
429 return m->handler(msg, m, i);
431 return 0;
434 void G_code (void) {
435 void *s;
436 s=csnd2;
437 if(memcmp(cbuf+32-5,"IDDQD",5)==0) {
438 PL_hit(&pl1,400,0,HIT_SOME);
439 if(_2pl) PL_hit(&pl2,400,0,HIT_SOME);
440 s=csnd1;
441 }else if(memcmp(cbuf+32-4,"TANK",4)==0) {
442 pl1.life=pl1.armor=200;pl1.drawst|=PL_DRAWARMOR|PL_DRAWLIFE;
443 if(_2pl) {pl2.life=pl2.armor=200;pl2.drawst|=PL_DRAWARMOR|PL_DRAWLIFE;}
444 }else if(memcmp(cbuf+32-8,"BULLFROG",8)==0) {
445 PL_JUMP=(PL_JUMP==10)?20:10;
446 }else if(memcmp(cbuf+32-8,"FORMULA1",8)==0) {
447 PL_RUN=(PL_RUN==8)?24:8;
448 }else if(memcmp(cbuf+32-5,"RAMBO",5)==0) {
449 pl1.ammo=pl1.shel=pl1.rock=pl1.cell=pl1.fuel=30000;
450 pl1.wpns=0x7FF;pl1.drawst|=PL_DRAWWPN|PL_DRAWKEYS;
451 pl1.keys=0x70;
452 if(_2pl) {
453 pl2.ammo=pl2.shel=pl2.rock=pl2.cell=pl1.fuel=30000;
454 pl2.wpns=0x7FF;pl2.drawst|=PL_DRAWWPN|PL_DRAWKEYS;
455 pl2.keys=0x70;
457 }else if(memcmp(cbuf+32-5,"UJHTW",5)==0) {
458 p_immortal=!p_immortal;
459 }else if(memcmp(cbuf+32-9,",TKSQJHTK",9)==0) {
460 p_fly=!p_fly;
461 }else if(memcmp(cbuf+32-6,"CBVCBV",6)==0) {
462 SW_cheat_open();
463 }else if(memcmp(cbuf+32-7,"GOODBYE",7)==0) {
464 g_exit=1;
465 }else if(memcmp(cbuf+32-9,"GJITKYF",7)==0) {
466 if(cbuf[30]>='0' && cbuf[30]<='9' && cbuf[31]>='0' && cbuf[31]<='9') {
467 g_map=(cbuf[30]=='0')?0:(cbuf[30]-'0')*10;
468 g_map+=(cbuf[31]=='0')?0:(cbuf[31]-'0');
469 G_start();
471 }else return;
472 memset(cbuf,0,32);
473 Z_sound(s,128);
476 static int x_strnlen (const char *s, int len) {
477 int i = 0;
478 while (i < len && s[i] != 0) {
479 i++;
481 return i;
484 static int state_for_anykey (int x) {
485 return x == GS_TITLE || x == GS_ENDSCR;
488 int GM_act (void) {
489 menu_msg_t msg;
490 int n, cur, type;
491 const menu_t *m = GM_get();
492 if (m == NULL) {
493 if (lastkey == KEY_ESCAPE || (state_for_anykey(g_st) && lastkey != KEY_UNKNOWN)) {
494 GM_push(&main_menu);
495 Z_sound(msnd3, 128);
497 } else {
498 /* 1. send key */
499 msg.type = GM_KEY;
500 GM_init_int0(&msg, lastkey, 0, 0, 0);
501 GM_send_this(m, &msg);
502 /* 2. send query */
503 msg.type = GM_QUERY;
504 if (GM_send_this(m, &msg)) {
505 /* 3. send getentry */
506 cur = msg.integer.i;
507 n = msg.integer.a;
508 msg.type = GM_GETENTRY;
509 if (GM_send(m, cur, &msg)) {
510 /* 4. send actions */
511 type = msg.integer.i;
512 switch (lastkey) {
513 case KEY_ESCAPE:
514 if (type == GM_TEXTFIELD && input) {
515 input = 0;
516 Y_disable_text_input();
517 msg.type = GM_CANCEL;
518 GM_send(m, cur, &msg);
519 } else {
520 GM_pop();
521 Z_sound(msnd4, 128);
523 break;
524 case KEY_UP:
525 case KEY_DOWN:
526 if (input == 0) {
527 msg.type = lastkey == KEY_UP ? GM_UP : GM_DOWN;
528 if (GM_send(m, cur, &msg)) {
529 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), x_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 = x_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;
599 lastkey = KEY_UNKNOWN;
600 return m != NULL;
603 void GM_input (int ch) {
604 if (ch != 0 && input) {
605 if (icur < imax) {
606 ibuf[icur] = ch;
607 icur += 1;
608 if (icur < imax) {
609 ibuf[icur] = 0;
615 void GM_key (int key, int down) {
616 int i;
617 if (down) {
618 lastkey = key;
619 if (!_2pl || cheat) {
620 for (i = 0; i < 31; i++) {
621 cbuf[i] = cbuf[i + 1];
623 if (key >= KEY_0 && key <= KEY_9) {
624 cbuf[31] = key - KEY_0 + '0';
625 } else if (key >= KEY_A && key <= KEY_Z) {
626 cbuf[31] = key - KEY_A + 'A';
627 } else {
628 cbuf[31] = 0;
634 void GM_init (void) {
635 int i;
636 char s[8];
637 static const char nm[QSND_NUM][6] = {
638 "CYBSIT", "KNTDTH", "MNPAIN", "PEPAIN", "SLOP", "MANSIT", "BOSPN", "VILACT",
639 "PLFALL", "BGACT", "BGDTH2", "POPAIN", "SGTATK", "VILDTH"
640 };
641 s[0] = 'D';
642 s[1] = 'S';
643 for (i = 0; i < QSND_NUM; ++i) {
644 memcpy(s + 2, nm[i], 6);
645 qsnd[i] = F_getresid(s);
647 csnd1 = Z_getsnd("HAHA1");
648 csnd2 = Z_getsnd("RADIO");
649 msnd1 = Z_getsnd("PSTOP");
650 msnd2 = Z_getsnd("PISTOL");
651 msnd3 = Z_getsnd("SWTCHN");
652 msnd4 = Z_getsnd("SWTCHX");
653 msnd5 = Z_getsnd("SUDI");
654 msnd6 = Z_getsnd("TUDI");
655 MUS_load("MENU");
656 MUS_start(0);