1 /* Copyright (C) 2020 SovietPony
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.
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.
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/>.
43 static byte pcolortab
[PCOLORN
] = {
44 0x18, 0x20, 0x40, 0x58, 0x60, 0x70, 0x80, 0xB0, 0xC0, 0xD0
46 static int p1color
= 5;
47 static int p2color
= 4;
55 static int stack_p
= -1;
57 #define GM_MAX_INPUT 24
58 char ibuf
[GM_MAX_INPUT
];
66 static int qsnd
[QSND_NUM
];
67 static snd_t
*csnd1
, *csnd2
, *msnd1
, *msnd2
, *msnd3
, *msnd4
, *msnd5
, *msnd6
;
71 static void GM_stop (void) {
82 static int GM_say (const char nm
[8]) {
83 snd_t
*snd
= S_load(nm
);
87 voc_ch
= S_play(voc
, 0, 255);
92 int GM_init_int0 (menu_msg_t
*msg
, int i
, int a
, int b
, int s
) {
101 int GM_init_int (menu_msg_t
*msg
, int i
, int a
, int b
, int s
) {
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
) {
113 msg
->string
.maxlen
= maxlen
;
117 int basic_menu_handler (menu_msg_t
*msg
, byte type
, char *title
, char *say
, int n
, int *cur
) {
119 assert(type
== GM_BIG
|| type
== GM_SMALL
);
120 assert(title
!= NULL
);
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;
133 int simple_menu_handler (menu_msg_t
*msg
, int i
, int n
, const simple_menu_t
*m
, int *cur
) {
136 assert(i
>= 0 && i
< n
);
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
) {
150 g_map
= level
? level
: 1;
152 pl1
.color
= pcolortab
[p1color
];
153 pl2
.color
= pcolortab
[p2color
];
158 static int new_game_menu_handler (menu_msg_t
*msg
, const menu_t
*m
, int i
) {
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
},
169 if (msg
->type
== GM_SELECT
) {
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
);
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
) {
182 const int max_slots
= 7;
183 assert(i
>= 0 && i
< max_slots
);
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);
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
) {
200 const int max_slots
= 7;
201 assert(i
>= 0 && i
< max_slots
);
204 if (g_st
== GS_GAME
) {
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);
213 if (g_st
== GS_GAME
) {
214 assert(msg
->string
.maxlen
>= 24);
215 F_savegame(i
, msg
->string
.s
);
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
) {
225 enum { VIDEO
, SOUND
, MUSIC
, __NUM__
};
226 static const simple_menu_t sm
= {
227 GM_BIG
, "Options", NULL
,
234 if (msg
->type
== GM_SELECT
) {
236 case VIDEO
: mm
= R_menu(); break;
237 case SOUND
: mm
= S_menu(); break;
238 case MUSIC
: mm
= MUS_menu(); break;
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
) {
250 enum { YES
, NO
, __NUM__
};
251 static const simple_menu_t sm
= {
252 GM_SMALL
, "You are sure?", NULL
,
258 if (msg
->type
== GM_ENTER
) {
259 return GM_say(rand() & 1 ? "_EXIT1" : "_EXIT2");
260 } else if (msg
->type
== GM_SELECT
) {
265 Z_sound(S_get(qsnd
[myrand(QSND_NUM
)]), 255);
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__
);
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
}
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
] }
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
) {
304 assert(stack_p
>= -1);
305 assert(stack_p
< MAX_STACK
- 1);
308 stack
[stack_p
].m
= m
;
310 GM_send_this(m
, &msg
);
315 assert(stack_p
>= 0);
319 GM_send_this(stack
[stack_p
+ 1].m
, &msg
);
323 int GM_popall (void) {
325 for (i
= 0; i
>= -1; i
--) {
331 const menu_t
*GM_get (void) {
333 return stack
[stack_p
].m
;
339 static void GM_normalize_message (menu_msg_t
*msg
) {
342 msg
->integer
.i
= min(max(msg
->integer
.i
, msg
->integer
.a
), msg
->integer
.b
);
345 assert(msg
->string
.maxlen
>= 0);
350 int GM_send_this (const menu_t
*m
, menu_msg_t
*msg
) {
353 if (m
->handler
!= NULL
) {
354 GM_normalize_message(msg
);
355 return m
->handler(msg
, m
, 0);
360 int GM_send (const menu_t
*m
, int i
, menu_msg_t
*msg
) {
364 if (m
->handler
!= NULL
) {
365 GM_normalize_message(msg
);
366 return m
->handler(msg
, m
, i
);
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
);
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
;
390 pl2
.ammo
=pl2
.shel
=pl2
.rock
=pl2
.cell
=pl1
.fuel
=30000;
391 pl2
.wpns
=0x7FF;pl2
.drawst
|=PL_DRAWWPN
|PL_DRAWKEYS
;
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) {
398 }else if(memcmp(cbuf
+32-6,"CBVCBV",6)==0) {
400 }else if(memcmp(cbuf
+32-7,"GOODBYE",7)==0) {
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');
413 static int x_strnlen (const char *s
, int len
) {
415 while (i
< len
&& s
[i
] != 0) {
421 static int state_for_anykey (int x
) {
422 return x
== GS_TITLE
|| x
== GS_ENDSCR
;
428 const menu_t
*m
= GM_get();
430 if (lastkey
== KEY_ESCAPE
|| (state_for_anykey(g_st
) && lastkey
!= KEY_UNKNOWN
)) {
436 if (GM_send_this(m
, &msg
)) {
439 msg
.type
= GM_GETENTRY
;
440 if (GM_send(m
, cur
, &msg
)) {
441 type
= msg
.integer
.i
;
444 if (type
== GM_TEXTFIELD
&& input
) {
446 Y_disable_text_input();
447 msg
.type
= GM_CANCEL
;
448 GM_send(m
, cur
, &msg
);
457 msg
.type
= lastkey
== KEY_UP
? GM_UP
: GM_DOWN
;
458 if (GM_send(m
, cur
, &msg
)) {
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
) {
477 // icur += lastkey == KEY_LEFT ? -1 : +1;
478 // icur = min(max(icur, 0), x_strnlen(ibuf, imax));
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
);
493 if (type
== GM_TEXTFIELD
) {
496 Y_disable_text_input();
499 msg
.string
.maxlen
= imax
;
500 GM_send(m
, cur
, &msg
);
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
= x_strnlen(ibuf
, imax
);
508 memset(ibuf
, 0, GM_MAX_INPUT
);
513 Y_enable_text_input();
515 GM_send(m
, cur
, &msg
);
519 msg
.type
= GM_SELECT
;
520 if (GM_send(m
, cur
, &msg
)) {
529 lastkey
= KEY_UNKNOWN
;
533 void GM_input (int ch
) {
534 if (ch
!= 0 && input
) {
545 void GM_key (int key
, int down
) {
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';
564 void GM_init (void) {
567 static const char nm
[QSND_NUM
][6] = {
568 "CYBSIT", "KNTDTH", "MNPAIN", "PEPAIN", "SLOP", "MANSIT", "BOSPN", "VILACT",
569 "PLFALL", "BGACT", "BGDTH2", "POPAIN", "SGTATK", "VILDTH"
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");