DEADSOFTWARE

update copyrights
[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 static int options_menu_handler (menu_msg_t *msg, const menu_t *m, int i) {
223 static int cur;
224 const menu_t *mm;
225 enum { VIDEO, SOUND, MUSIC, __NUM__ };
226 static const simple_menu_t sm = {
227 GM_BIG, "Options", NULL,
229 { "Video", NULL },
230 { "Sound", NULL },
231 { "Music", NULL },
233 };
234 if (msg->type == GM_SELECT) {
235 switch (i) {
236 case VIDEO: mm = R_menu(); break;
237 case SOUND: mm = S_menu(); break;
238 case MUSIC: mm = MUS_menu(); break;
239 default: mm = NULL;
241 if (mm != NULL) {
242 return GM_push(mm);
245 return simple_menu_handler(msg, i, __NUM__, &sm, &cur);
248 static int exit_menu_handler (menu_msg_t *msg, const menu_t *m, int i) {
249 static int cur;
250 enum { YES, NO, __NUM__ };
251 static const simple_menu_t sm = {
252 GM_SMALL, "You are sure?", NULL,
254 { "Yes", NULL },
255 { "No", NULL },
257 };
258 if (msg->type == GM_ENTER) {
259 return GM_say(rand() & 1 ? "_EXIT1" : "_EXIT2");
260 } else if (msg->type == GM_SELECT) {
261 switch (i) {
262 case YES:
263 MUS_free();
264 GM_stop();
265 Z_sound(S_get(qsnd[myrand(QSND_NUM)]), 255);
266 S_wait();
267 ERR_quit();
268 return 1;
269 case NO:
270 return GM_pop();
273 return simple_menu_handler(msg, i, __NUM__, &sm, &cur);
276 static int main_menu_handler (menu_msg_t *msg, const menu_t *m, int i) {
277 enum { NEWGAME, OLDGAME, SAVEGAME, OPTIONS, EXIT, __NUM__ };
278 assert(i >= 0 && i < __NUM__);
279 static int cur;
280 static const menu_t hm[__NUM__] = {
281 { new_game_menu_handler },
282 { load_game_menu_handler },
283 { save_game_menu_handler },
284 { options_menu_handler},
285 { exit_menu_handler }
286 };
287 static const simple_menu_t sm = {
288 GM_BIG, "Menu", NULL,
290 { "New Game", &hm[NEWGAME] },
291 { "Load Game", &hm[OLDGAME] },
292 { "Save Game", &hm[SAVEGAME] },
293 { "Options", &hm[OPTIONS] },
294 { "Exit", &hm[EXIT] }
296 };
297 return simple_menu_handler(msg, i, __NUM__, &sm, &cur);
300 static const menu_t main_menu = { &main_menu_handler };
302 int GM_push (const menu_t *m) {
303 assert(m != NULL);
304 assert(stack_p >= -1);
305 assert(stack_p < MAX_STACK - 1);
306 menu_msg_t msg;
307 stack_p += 1;
308 stack[stack_p].m = m;
309 msg.type = GM_ENTER;
310 GM_send_this(m, &msg);
311 return 1;
314 int GM_pop (void) {
315 assert(stack_p >= 0);
316 menu_msg_t msg;
317 stack_p -= 1;
318 msg.type = GM_LEAVE;
319 GM_send_this(stack[stack_p + 1].m, &msg);
320 return 1;
323 int GM_popall (void) {
324 int i;
325 for (i = 0; i >= -1; i--) {
326 GM_pop();
328 return 1;
331 const menu_t *GM_get (void) {
332 if (stack_p >= 0) {
333 return stack[stack_p].m;
334 } else {
335 return NULL;
339 static void GM_normalize_message (menu_msg_t *msg) {
340 switch (msg->type) {
341 case GM_SETINT:
342 msg->integer.i = min(max(msg->integer.i, msg->integer.a), msg->integer.b);
343 break;
344 case GM_SETSTR:
345 assert(msg->string.maxlen >= 0);
346 break;
350 int GM_send_this (const menu_t *m, menu_msg_t *msg) {
351 assert(m != NULL);
352 assert(msg != NULL);
353 if (m->handler != NULL) {
354 GM_normalize_message(msg);
355 return m->handler(msg, m, 0);
357 return 0;
360 int GM_send (const menu_t *m, int i, menu_msg_t *msg) {
361 assert(m != NULL);
362 assert(i >= 0);
363 assert(msg != NULL);
364 if (m->handler != NULL) {
365 GM_normalize_message(msg);
366 return m->handler(msg, m, i);
368 return 0;
371 void G_code (void) {
372 void *s;
373 s=csnd2;
374 if(memcmp(cbuf+32-5,"IDDQD",5)==0) {
375 PL_hit(&pl1,400,0,HIT_SOME);
376 if(_2pl) PL_hit(&pl2,400,0,HIT_SOME);
377 s=csnd1;
378 }else if(memcmp(cbuf+32-4,"TANK",4)==0) {
379 pl1.life=pl1.armor=200;pl1.drawst|=PL_DRAWARMOR|PL_DRAWLIFE;
380 if(_2pl) {pl2.life=pl2.armor=200;pl2.drawst|=PL_DRAWARMOR|PL_DRAWLIFE;}
381 }else if(memcmp(cbuf+32-8,"BULLFROG",8)==0) {
382 PL_JUMP=(PL_JUMP==10)?20:10;
383 }else if(memcmp(cbuf+32-8,"FORMULA1",8)==0) {
384 PL_RUN=(PL_RUN==8)?24:8;
385 }else if(memcmp(cbuf+32-5,"RAMBO",5)==0) {
386 pl1.ammo=pl1.shel=pl1.rock=pl1.cell=pl1.fuel=30000;
387 pl1.wpns=0x7FF;pl1.drawst|=PL_DRAWWPN|PL_DRAWKEYS;
388 pl1.keys=0x70;
389 if(_2pl) {
390 pl2.ammo=pl2.shel=pl2.rock=pl2.cell=pl1.fuel=30000;
391 pl2.wpns=0x7FF;pl2.drawst|=PL_DRAWWPN|PL_DRAWKEYS;
392 pl2.keys=0x70;
394 }else if(memcmp(cbuf+32-5,"UJHTW",5)==0) {
395 p_immortal=!p_immortal;
396 }else if(memcmp(cbuf+32-9,",TKSQJHTK",9)==0) {
397 p_fly=!p_fly;
398 }else if(memcmp(cbuf+32-6,"CBVCBV",6)==0) {
399 SW_cheat_open();
400 }else if(memcmp(cbuf+32-7,"GOODBYE",7)==0) {
401 g_exit=1;
402 }else if(memcmp(cbuf+32-9,"GJITKYF",7)==0) {
403 if(cbuf[30]>='0' && cbuf[30]<='9' && cbuf[31]>='0' && cbuf[31]<='9') {
404 g_map=(cbuf[30]=='0')?0:(cbuf[30]-'0')*10;
405 g_map+=(cbuf[31]=='0')?0:(cbuf[31]-'0');
406 G_start();
408 }else return;
409 memset(cbuf,0,32);
410 Z_sound(s,128);
413 static int strnlen (const char *s, int len) {
414 int i = 0;
415 while (i < len && s[i] != 0) {
416 i++;
418 return i;
421 static int state_for_anykey (int x) {
422 return x == GS_TITLE || x == GS_ENDSCR;
425 int GM_act (void) {
426 menu_msg_t msg;
427 int n, cur, type;
428 const menu_t *m = GM_get();
429 if (m == NULL) {
430 if (lastkey == KEY_ESCAPE || (state_for_anykey(g_st) && lastkey != KEY_UNKNOWN)) {
431 GM_push(&main_menu);
432 Z_sound(msnd3, 128);
434 } else {
435 msg.type = GM_QUERY;
436 if (GM_send_this(m, &msg)) {
437 cur = msg.integer.i;
438 n = msg.integer.a;
439 msg.type = GM_GETENTRY;
440 if (GM_send(m, cur, &msg)) {
441 type = msg.integer.i;
442 switch (lastkey) {
443 case KEY_ESCAPE:
444 if (type == GM_TEXTFIELD && input) {
445 input = 0;
446 Y_disable_text_input();
447 msg.type = GM_CANCEL;
448 GM_send(m, cur, &msg);
449 } else {
450 GM_pop();
451 Z_sound(msnd4, 128);
453 break;
454 case KEY_UP:
455 case KEY_DOWN:
456 if (input == 0) {
457 msg.type = lastkey == KEY_UP ? GM_UP : GM_DOWN;
458 if (GM_send(m, cur, &msg)) {
459 Z_sound(msnd1, 128);
462 break;
463 case KEY_LEFT:
464 case KEY_RIGHT:
465 if (type == GM_SCROLLER) {
466 msg.integer.type = GM_GETINT;
467 if (GM_send(m, cur, &msg)) {
468 msg.integer.type = GM_SETINT;
469 msg.integer.i += lastkey == KEY_LEFT ? -msg.integer.s : msg.integer.s;
470 msg.integer.i = min(max(msg.integer.i, msg.integer.a), msg.integer.b);
471 if (GM_send(m, cur, &msg)) {
472 Z_sound(lastkey == KEY_LEFT ? msnd5 : msnd6, 255);
475 } else if (type == GM_TEXTFIELD) {
476 //if (input) {
477 // icur += lastkey == KEY_LEFT ? -1 : +1;
478 // icur = min(max(icur, 0), strnlen(ibuf, imax));
479 //}
481 break;
482 case KEY_BACKSPACE:
483 if (type == GM_TEXTFIELD) {
484 if (input && icur > 0) {
485 // FIXIT buffers in strncpy must not overlap
486 strncpy(&ibuf[icur - 1], &ibuf[icur], imax - icur);
487 ibuf[imax - 1] = 0;
488 icur -= 1;
491 break;
492 case KEY_RETURN:
493 if (type == GM_TEXTFIELD) {
494 if (input) {
495 input = 0;
496 Y_disable_text_input();
497 msg.type = GM_END;
498 msg.string.s = ibuf;
499 msg.string.maxlen = imax;
500 GM_send(m, cur, &msg);
501 } else {
502 msg.type = GM_GETSTR;
503 if (GM_send(m, cur, &msg)) {
504 imax = min(msg.string.maxlen, GM_MAX_INPUT);
505 strncpy(ibuf, msg.string.s, imax);
506 icur = strnlen(ibuf, imax);
507 } else {
508 memset(ibuf, 0, GM_MAX_INPUT);
509 imax = GM_MAX_INPUT;
510 icur = 0;
512 input = 1;
513 Y_enable_text_input();
514 msg.type = GM_BEGIN;
515 GM_send(m, cur, &msg);
517 Z_sound(msnd2, 128);
518 } else {
519 msg.type = GM_SELECT;
520 if (GM_send(m, cur, &msg)) {
521 Z_sound(msnd2, 128);
524 break;
529 lastkey = KEY_UNKNOWN;
530 return m != NULL;
533 void GM_input (int ch) {
534 if (ch != 0 && input) {
535 if (icur < imax) {
536 ibuf[icur] = ch;
537 icur += 1;
538 if (icur < imax) {
539 ibuf[icur] = 0;
545 void GM_key (int key, int down) {
546 int i;
547 if (down) {
548 lastkey = key;
549 if (!_2pl || cheat) {
550 for (i = 0; i < 31; i++) {
551 cbuf[i] = cbuf[i + 1];
553 if (key >= KEY_0 && key <= KEY_9) {
554 cbuf[31] = key - KEY_0 + '0';
555 } else if (key >= KEY_A && key <= KEY_Z) {
556 cbuf[31] = key - KEY_A + 'A';
557 } else {
558 cbuf[31] = 0;
564 void GM_init (void) {
565 int i;
566 char s[8];
567 static const char nm[QSND_NUM][6] = {
568 "CYBSIT", "KNTDTH", "MNPAIN", "PEPAIN", "SLOP", "MANSIT", "BOSPN", "VILACT",
569 "PLFALL", "BGACT", "BGDTH2", "POPAIN", "SGTATK", "VILDTH"
570 };
571 s[0] = 'D';
572 s[1] = 'S';
573 for (i = 0; i < QSND_NUM; ++i) {
574 memcpy(s + 2, nm[i], 6);
575 qsnd[i] = F_getresid(s);
577 csnd1 = Z_getsnd("HAHA1");
578 csnd2 = Z_getsnd("RADIO");
579 msnd1 = Z_getsnd("PSTOP");
580 msnd2 = Z_getsnd("PISTOL");
581 msnd3 = Z_getsnd("SWTCHN");
582 msnd4 = Z_getsnd("SWTCHX");
583 msnd5 = Z_getsnd("SUDI");
584 msnd6 = Z_getsnd("TUDI");
585 MUS_load("MENU");
586 MUS_start(0);