]> git.xonotic.org Git - xonotic/darkplaces.git/blob - menu.c
physics: fix and refactor unsticking
[xonotic/darkplaces.git] / menu.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 #include "quakedef.h"
21 #include "cdaudio.h"
22 #include "image.h"
23 #include "progsvm.h"
24
25 #include "mprogdefs.h"
26
27 #define TYPE_DEMO 1
28 #define TYPE_GAME 2
29 #define TYPE_BOTH 3
30
31 static cvar_t forceqmenu = {CF_CLIENT, "forceqmenu", "0", "enables the quake menu instead of the quakec menu.dat (if present)"};
32 static cvar_t menu_progs = {CF_CLIENT, "menu_progs", "menu.dat", "name of quakec menu.dat file"};
33
34 static int NehGameType;
35
36 enum m_state_e m_state;
37
38 void M_Menu_Main_f(cmd_state_t *cmd);
39         void M_Menu_SinglePlayer_f(cmd_state_t *cmd);
40                 void M_Menu_Transfusion_Episode_f(cmd_state_t *cmd);
41                         void M_Menu_Transfusion_Skill_f(cmd_state_t *cmd);
42                 void M_Menu_Load_f(cmd_state_t *cmd);
43                 void M_Menu_Save_f(cmd_state_t *cmd);
44         void M_Menu_MultiPlayer_f(cmd_state_t *cmd);
45                 void M_Menu_Setup_f(cmd_state_t *cmd);
46         void M_Menu_Options_f(cmd_state_t *cmd);
47         void M_Menu_Options_Effects_f(cmd_state_t *cmd);
48         void M_Menu_Options_Graphics_f(cmd_state_t *cmd);
49         void M_Menu_Options_ColorControl_f(cmd_state_t *cmd);
50                 void M_Menu_Keys_f(cmd_state_t *cmd);
51                 void M_Menu_Reset_f(cmd_state_t *cmd);
52                 void M_Menu_Video_f(cmd_state_t *cmd);
53         void M_Menu_Help_f(cmd_state_t *cmd);
54         void M_Menu_Credits_f(cmd_state_t *cmd);
55         void M_Menu_Quit_f(cmd_state_t *cmd);
56 void M_Menu_LanConfig_f(cmd_state_t *cmd);
57 void M_Menu_GameOptions_f(cmd_state_t *cmd);
58 void M_Menu_ServerList_f(cmd_state_t *cmd);
59 void M_Menu_ModList_f(cmd_state_t *cmd);
60
61 static void M_Main_Draw (void);
62         static void M_SinglePlayer_Draw (void);
63                 static void M_Transfusion_Episode_Draw (void);
64                         static void M_Transfusion_Skill_Draw (void);
65                 static void M_Load_Draw (void);
66                 static void M_Save_Draw (void);
67         static void M_MultiPlayer_Draw (void);
68                 static void M_Setup_Draw (void);
69         static void M_Options_Draw (void);
70         static void M_Options_Effects_Draw (void);
71         static void M_Options_Graphics_Draw (void);
72         static void M_Options_ColorControl_Draw (void);
73                 static void M_Keys_Draw (void);
74                 static void M_Reset_Draw (void);
75                 static void M_Video_Draw (void);
76         static void M_Help_Draw (void);
77         static void M_Credits_Draw (void);
78         static void M_Quit_Draw (void);
79 static void M_LanConfig_Draw (void);
80 static void M_GameOptions_Draw (void);
81 static void M_ServerList_Draw (void);
82 static void M_ModList_Draw (void);
83
84
85 static void M_Main_Key(cmd_state_t *cmd, int key, int ascii);
86         static void M_SinglePlayer_Key(cmd_state_t *cmd, int key, int ascii);
87                 static void M_Transfusion_Episode_Key(cmd_state_t *cmd, int key, int ascii);
88                         static void M_Transfusion_Skill_Key(cmd_state_t *cmd, int key, int ascii);
89                 static void M_Load_Key(cmd_state_t *cmd, int key, int ascii);
90                 static void M_Save_Key(cmd_state_t *cmd, int key, int ascii);
91         static void M_MultiPlayer_Key(cmd_state_t *cmd, int key, int ascii);
92                 static void M_Setup_Key(cmd_state_t *cmd, int key, int ascii);
93         static void M_Options_Key(cmd_state_t *cmd, int key, int ascii);
94         static void M_Options_Effects_Key(cmd_state_t *cmd, int key, int ascii);
95         static void M_Options_Graphics_Key(cmd_state_t *cmd, int key, int ascii);
96         static void M_Options_ColorControl_Key(cmd_state_t *cmd, int key, int ascii);
97                 static void M_Keys_Key(cmd_state_t *cmd, int key, int ascii);
98                 static void M_Reset_Key(cmd_state_t *cmd, int key, int ascii);
99                 static void M_Video_Key(cmd_state_t *cmd, int key, int ascii);
100         static void M_Help_Key(cmd_state_t *cmd, int key, int ascii);
101         static void M_Credits_Key(cmd_state_t *cmd, int key, int ascii);
102         static void M_Quit_Key(cmd_state_t *cmd, int key, int ascii);
103 static void M_LanConfig_Key(cmd_state_t *cmd, int key, int ascii);
104 static void M_GameOptions_Key(cmd_state_t *cmd, int key, int ascii);
105 static void M_ServerList_Key(cmd_state_t *cmd, int key, int ascii);
106 static void M_ModList_Key(cmd_state_t *cmd, int key, int ascii);
107
108 static qbool    m_entersound;           ///< play after drawing a frame, so caching won't disrupt the sound
109
110 #define StartingGame    (m_multiplayer_cursor == 1)
111 #define JoiningGame             (m_multiplayer_cursor == 0)
112
113 // Nehahra
114 #define NumberOfNehahraDemos 34
115 typedef struct nehahrademonames_s
116 {
117         const char *name;
118         const char *desc;
119 } nehahrademonames_t;
120
121 static nehahrademonames_t NehahraDemos[NumberOfNehahraDemos] =
122 {
123         {"intro", "Prologue"},
124         {"genf", "The Beginning"},
125         {"genlab", "A Doomed Project"},
126         {"nehcre", "The New Recruits"},
127         {"maxneh", "Breakthrough"},
128         {"maxchar", "Renewal and Duty"},
129         {"crisis", "Worlds Collide"},
130         {"postcris", "Darkening Skies"},
131         {"hearing", "The Hearing"},
132         {"getjack", "On a Mexican Radio"},
133         {"prelude", "Honor and Justice"},
134         {"abase", "A Message Sent"},
135         {"effect", "The Other Side"},
136         {"uhoh", "Missing in Action"},
137         {"prepare", "The Response"},
138         {"vision", "Farsighted Eyes"},
139         {"maxturns", "Enter the Immortal"},
140         {"backlot", "Separate Ways"},
141         {"maxside", "The Ancient Runes"},
142         {"counter", "The New Initiative"},
143         {"warprep", "Ghosts to the World"},
144         {"counter1", "A Fate Worse Than Death"},
145         {"counter2", "Friendly Fire"},
146         {"counter3", "Minor Setback"},
147         {"madmax", "Scores to Settle"},
148         {"quake", "One Man"},
149         {"cthmm", "Shattered Masks"},
150         {"shades", "Deal with the Dead"},
151         {"gophil", "An Unlikely Hero"},
152         {"cstrike", "War in Hell"},
153         {"shubset", "The Conspiracy"},
154         {"shubdie", "Even Death May Die"},
155         {"newranks", "An Empty Throne"},
156         {"seal", "The Seal is Broken"}
157 };
158
159 static float menu_x, menu_y, menu_width, menu_height;
160
161 static void M_Background(int width, int height)
162 {
163         menu_width = bound(1.0f, (float)width, vid_conwidth.value);
164         menu_height = bound(1.0f, (float)height, vid_conheight.value);
165         menu_x = (vid_conwidth.integer - menu_width) * 0.5;
166         menu_y = (vid_conheight.integer - menu_height) * 0.5;
167         //DrawQ_Fill(menu_x, menu_y, menu_width, menu_height, 0, 0, 0, 0.5, 0);
168         DrawQ_Fill(0, 0, vid_conwidth.integer, vid_conheight.integer, 0, 0, 0, 0.5, 0);
169 }
170
171 /*
172 ================
173 M_DrawCharacter
174
175 Draws one solid graphics character
176 ================
177 */
178 static void M_DrawCharacter (float cx, float cy, int num)
179 {
180         char temp[2];
181         temp[0] = num;
182         temp[1] = 0;
183         DrawQ_String(menu_x + cx, menu_y + cy, temp, 1, 8, 8, 1, 1, 1, 1, 0, NULL, true, FONT_MENU);
184 }
185
186 static void M_PrintColored(float cx, float cy, const char *str)
187 {
188         DrawQ_String(menu_x + cx, menu_y + cy, str, 0, 8, 8, 1, 1, 1, 1, 0, NULL, false, FONT_MENU);
189 }
190
191 static void M_Print(float cx, float cy, const char *str)
192 {
193         DrawQ_String(menu_x + cx, menu_y + cy, str, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true, FONT_MENU);
194 }
195
196 static void M_PrintRed(float cx, float cy, const char *str)
197 {
198         DrawQ_String(menu_x + cx, menu_y + cy, str, 0, 8, 8, 1, 0, 0, 1, 0, NULL, true, FONT_MENU);
199 }
200
201 static void M_ItemPrint(float cx, float cy, const char *str, int unghosted)
202 {
203         if (unghosted)
204                 DrawQ_String(menu_x + cx, menu_y + cy, str, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true, FONT_MENU);
205         else
206                 DrawQ_String(menu_x + cx, menu_y + cy, str, 0, 8, 8, 0.4, 0.4, 0.4, 1, 0, NULL, true, FONT_MENU);
207 }
208
209 static void M_DrawPic(float cx, float cy, const char *picname)
210 {
211         DrawQ_Pic(menu_x + cx, menu_y + cy, Draw_CachePic (picname), 0, 0, 1, 1, 1, 1, 0);
212 }
213
214 static void M_DrawTextBox(float x, float y, float width, float height)
215 {
216         int n;
217         float cx, cy;
218
219         // draw left side
220         cx = x;
221         cy = y;
222         M_DrawPic (cx, cy, "gfx/box_tl");
223         for (n = 0; n < height; n++)
224         {
225                 cy += 8;
226                 M_DrawPic (cx, cy, "gfx/box_ml");
227         }
228         M_DrawPic (cx, cy+8, "gfx/box_bl");
229
230         // draw middle
231         cx += 8;
232         while (width > 0)
233         {
234                 cy = y;
235                 M_DrawPic (cx, cy, "gfx/box_tm");
236                 for (n = 0; n < height; n++)
237                 {
238                         cy += 8;
239                         if (n >= 1)
240                                 M_DrawPic (cx, cy, "gfx/box_mm2");
241                         else
242                                 M_DrawPic (cx, cy, "gfx/box_mm");
243                 }
244                 M_DrawPic (cx, cy+8, "gfx/box_bm");
245                 width -= 2;
246                 cx += 16;
247         }
248
249         // draw right side
250         cy = y;
251         M_DrawPic (cx, cy, "gfx/box_tr");
252         for (n = 0; n < height; n++)
253         {
254                 cy += 8;
255                 M_DrawPic (cx, cy, "gfx/box_mr");
256         }
257         M_DrawPic (cx, cy+8, "gfx/box_br");
258 }
259
260 //=============================================================================
261
262 //int m_save_demonum;
263
264 /*
265 ================
266 M_ToggleMenu
267 ================
268 */
269 static void M_ToggleMenu(int mode)
270 {
271         m_entersound = true;
272
273         if ((key_dest != key_menu && key_dest != key_menu_grabbed) || m_state != m_main)
274         {
275                 if(mode == 0)
276                         return; // the menu is off, and we want it off
277                 M_Menu_Main_f (cmd_local);
278         }
279         else
280         {
281                 if(mode == 1)
282                         return; // the menu is on, and we want it on
283                 key_dest = key_game;
284                 m_state = m_none;
285         }
286 }
287
288
289 static int demo_cursor;
290 static void M_Demo_Draw (void)
291 {
292         int i;
293
294         M_Background(320, 200);
295
296         for (i = 0;i < NumberOfNehahraDemos;i++)
297                 M_Print(16, 16 + 8*i, NehahraDemos[i].desc);
298
299         // line cursor
300         M_DrawCharacter (8, 16 + demo_cursor*8, 12+((int)(host.realtime*4)&1));
301 }
302
303
304 static void M_Menu_Demos_f(cmd_state_t *cmd)
305 {
306         key_dest = key_menu;
307         m_state = m_demo;
308         m_entersound = true;
309 }
310
311
312 static void M_Demo_Key (cmd_state_t *cmd, int k, int ascii)
313 {
314         char vabuf[1024];
315         switch (k)
316         {
317         case K_ESCAPE:
318                 M_Menu_Main_f (cmd);
319                 break;
320
321         case K_ENTER:
322                 S_LocalSound ("sound/misc/menu2.wav");
323                 m_state = m_none;
324                 key_dest = key_game;
325                 Cbuf_AddText (cmd, va(vabuf, sizeof(vabuf), "playdemo %s\n", NehahraDemos[demo_cursor].name));
326                 return;
327
328         case K_UPARROW:
329         case K_LEFTARROW:
330                 S_LocalSound ("sound/misc/menu1.wav");
331                 demo_cursor--;
332                 if (demo_cursor < 0)
333                         demo_cursor = NumberOfNehahraDemos-1;
334                 break;
335
336         case K_DOWNARROW:
337         case K_RIGHTARROW:
338                 S_LocalSound ("sound/misc/menu1.wav");
339                 demo_cursor++;
340                 if (demo_cursor >= NumberOfNehahraDemos)
341                         demo_cursor = 0;
342                 break;
343         }
344 }
345
346 //=============================================================================
347 /* MAIN MENU */
348
349 static int      m_main_cursor;
350 static qbool m_missingdata = false;
351
352 static int MAIN_ITEMS = 4; // Nehahra: Menu Disable
353
354
355 void M_Menu_Main_f(cmd_state_t *cmd)
356 {
357         const char *s;
358         s = "gfx/mainmenu";
359
360         if (gamemode == GAME_NEHAHRA)
361         {
362                 if (FS_FileExists("maps/neh1m4.bsp"))
363                 {
364                         if (FS_FileExists("hearing.dem"))
365                         {
366                                 Con_DPrint("Main menu: Nehahra movie and game detected.\n");
367                                 NehGameType = TYPE_BOTH;
368                         }
369                         else
370                         {
371                                 Con_DPrint("Nehahra game detected.\n");
372                                 NehGameType = TYPE_GAME;
373                         }
374                 }
375                 else
376                 {
377                         if (FS_FileExists("hearing.dem"))
378                         {
379                                 Con_DPrint("Nehahra movie detected.\n");
380                                 NehGameType = TYPE_DEMO;
381                         }
382                         else
383                         {
384                                 Con_DPrint("Nehahra not found.\n");
385                                 NehGameType = TYPE_GAME; // could just complain, but...
386                         }
387                 }
388                 if (NehGameType == TYPE_DEMO)
389                         MAIN_ITEMS = 4;
390                 else if (NehGameType == TYPE_GAME)
391                         MAIN_ITEMS = 5;
392                 else
393                         MAIN_ITEMS = 6;
394         }
395         else if (gamemode == GAME_TRANSFUSION)
396         {
397                 s = "gfx/menu/mainmenu1";
398                 if (sv.active && !cl.intermission && cl.islocalgame)
399                         MAIN_ITEMS = 8;
400                 else
401                         MAIN_ITEMS = 7;
402         }
403         else
404                 MAIN_ITEMS = 5;
405
406         // check if the game data is missing and use a different main menu if so
407         m_missingdata = !forceqmenu.integer && !Draw_IsPicLoaded(Draw_CachePic_Flags(s, CACHEPICFLAG_FAILONMISSING));
408         if (m_missingdata)
409                 MAIN_ITEMS = 2;
410
411         /*
412         if (key_dest != key_menu)
413         {
414                 m_save_demonum = cls.demonum;
415                 cls.demonum = -1;
416         }
417         */
418         key_dest = key_menu;
419         m_state = m_main;
420         m_entersound = true;
421 }
422
423
424 static void M_Main_Draw (void)
425 {
426         int             f;
427         cachepic_t      *p;
428         char vabuf[1024];
429
430         if (m_missingdata)
431         {
432                 float y;
433                 const char *s;
434                 M_Background(640, 480); //fall back is always to 640x480, this makes it most readable at that.
435                 y = 480/3-16;
436                 s = "You have reached this menu due to missing or unlocatable content/data";M_PrintRed ((640-strlen(s)*8)*0.5, (480/3)-16, s);y+=8;
437                 y+=8;
438                 s = "You may consider adding";M_Print ((640-strlen(s)*8)*0.5, y, s);y+=8;
439                 s = "-basedir /path/to/game";M_Print ((640-strlen(s)*8)*0.5, y, s);y+=8;
440                 s = "to your launch commandline";M_Print ((640-strlen(s)*8)*0.5, y, s);y+=8;
441                 M_Print (640/2 - 48, 480/2, "Open Console"); //The console usually better shows errors (failures)
442                 M_Print (640/2 - 48, 480/2 + 8, "Quit");
443                 M_DrawCharacter(640/2 - 56, 480/2 + (8 * m_main_cursor), 12+((int)(host.realtime*4)&1));
444                 return;
445         }
446
447         if (gamemode == GAME_TRANSFUSION) {
448                 int y1, y2, y3;
449                 M_Background(640, 480);
450                 p = Draw_CachePic ("gfx/menu/tb-transfusion");
451                 M_DrawPic (640/2 - Draw_GetPicWidth(p)/2, 40, "gfx/menu/tb-transfusion");
452                 y2 = 120;
453                 // 8 rather than MAIN_ITEMS to skip a number and not miss the last option
454                 for (y1 = 1; y1 <= 8; y1++)
455                 {
456                         if (MAIN_ITEMS == 7 && y1 == 4)
457                                 y1++;
458                         M_DrawPic (0, y2, va(vabuf, sizeof(vabuf), "gfx/menu/mainmenu%i", y1));
459                         y2 += 40;
460                 }
461                 if (MAIN_ITEMS == 7 && m_main_cursor > 2)
462                         y3 = m_main_cursor + 2;
463                 else
464                         y3 = m_main_cursor + 1;
465                 M_DrawPic (0, 120 + m_main_cursor * 40, va(vabuf, sizeof(vabuf), "gfx/menu/mainmenu%iselected", y3));
466                 return;
467         }
468
469         M_Background(320, 200);
470         M_DrawPic (16, 4, "gfx/qplaque");
471         p = Draw_CachePic ("gfx/ttl_main");
472         M_DrawPic ( (320-Draw_GetPicWidth(p))/2, 4, "gfx/ttl_main");
473 // Nehahra
474         if (gamemode == GAME_NEHAHRA)
475         {
476                 if (NehGameType == TYPE_BOTH)
477                         M_DrawPic (72, 32, "gfx/mainmenu");
478                 else if (NehGameType == TYPE_GAME)
479                         M_DrawPic (72, 32, "gfx/gamemenu");
480                 else
481                         M_DrawPic (72, 32, "gfx/demomenu");
482         }
483         else
484                 M_DrawPic (72, 32, "gfx/mainmenu");
485
486         f = (int)(host.realtime * 10)%6;
487
488         M_DrawPic (54, 32 + m_main_cursor * 20, va(vabuf, sizeof(vabuf), "gfx/menudot%i", f+1));
489 }
490
491
492 static void M_Main_Key(cmd_state_t *cmd, int key, int ascii)
493 {
494         switch (key)
495         {
496         case K_ESCAPE:
497                 key_dest = key_game;
498                 m_state = m_none;
499                 //cls.demonum = m_save_demonum;
500                 //if (cls.demonum != -1 && !cls.demoplayback && cls.state != ca_connected)
501                 //      CL_NextDemo ();
502                 break;
503
504         case K_DOWNARROW:
505                 S_LocalSound ("sound/misc/menu1.wav");
506                 if (++m_main_cursor >= MAIN_ITEMS)
507                         m_main_cursor = 0;
508                 break;
509
510         case K_UPARROW:
511                 S_LocalSound ("sound/misc/menu1.wav");
512                 if (--m_main_cursor < 0)
513                         m_main_cursor = MAIN_ITEMS - 1;
514                 break;
515
516         case K_ENTER:
517                 m_entersound = true;
518
519                 if (m_missingdata)
520                 {
521                         switch (m_main_cursor)
522                         {
523                         case 0:
524                                 if (cls.state == ca_connected)
525                                 {
526                                         m_state = m_none;
527                                         key_dest = key_game;
528                                 }
529                                 Con_ToggleConsole_f(cmd);
530                                 break;
531                         case 1:
532                                 M_Menu_Quit_f(cmd);
533                                 break;
534                         }
535                 }
536                 else if (gamemode == GAME_NEHAHRA)
537                 {
538                         switch (NehGameType)
539                         {
540                         case TYPE_BOTH:
541                                 switch (m_main_cursor)
542                                 {
543                                 case 0:
544                                         M_Menu_SinglePlayer_f(cmd);
545                                         break;
546
547                                 case 1:
548                                         M_Menu_Demos_f(cmd);
549                                         break;
550
551                                 case 2:
552                                         M_Menu_MultiPlayer_f(cmd);
553                                         break;
554
555                                 case 3:
556                                         M_Menu_Options_f(cmd);
557                                         break;
558
559                                 case 4:
560                                         key_dest = key_game;
561                                         if (sv.active)
562                                                 Cbuf_AddText (cmd, "disconnect\n");
563                                         Cbuf_AddText (cmd, "playdemo endcred\n");
564                                         break;
565
566                                 case 5:
567                                         M_Menu_Quit_f(cmd);
568                                         break;
569                                 }
570                                 break;
571                         case TYPE_GAME:
572                                 switch (m_main_cursor)
573                                 {
574                                 case 0:
575                                         M_Menu_SinglePlayer_f(cmd);
576                                         break;
577
578                                 case 1:
579                                         M_Menu_MultiPlayer_f(cmd);
580                                         break;
581
582                                 case 2:
583                                         M_Menu_Options_f(cmd);
584                                         break;
585
586                                 case 3:
587                                         key_dest = key_game;
588                                         if (sv.active)
589                                                 Cbuf_AddText (cmd, "disconnect\n");
590                                         Cbuf_AddText (cmd, "playdemo endcred\n");
591                                         break;
592
593                                 case 4:
594                                         M_Menu_Quit_f(cmd);
595                                         break;
596                                 }
597                                 break;
598                         case TYPE_DEMO:
599                                 switch (m_main_cursor)
600                                 {
601                                 case 0:
602                                         M_Menu_Demos_f(cmd);
603                                         break;
604
605                                 case 1:
606                                         key_dest = key_game;
607                                         if (sv.active)
608                                                 Cbuf_AddText (cmd, "disconnect\n");
609                                         Cbuf_AddText (cmd, "playdemo endcred\n");
610                                         break;
611
612                                 case 2:
613                                         M_Menu_Options_f(cmd);
614                                         break;
615
616                                 case 3:
617                                         M_Menu_Quit_f(cmd);
618                                         break;
619                                 }
620                                 break;
621                         }
622                 }
623                 else if (gamemode == GAME_TRANSFUSION) {
624                         if (MAIN_ITEMS == 7)
625                         {
626                                 switch (m_main_cursor)
627                                 {
628                                 case 0:
629                                         M_Menu_Transfusion_Episode_f(cmd);
630                                         break;
631
632                                 case 1:
633                                         M_Menu_MultiPlayer_f(cmd);
634                                         break;
635
636                                 case 2:
637                                         M_Menu_Options_f(cmd);
638                                         break;
639
640                                 case 3:
641                                         M_Menu_Load_f(cmd);
642                                         break;
643
644                                 case 4:
645                                         M_Menu_Help_f(cmd);
646                                         break;
647
648                                 case 5:
649                                         M_Menu_Credits_f(cmd);
650                                         break;
651
652                                 case 6:
653                                         M_Menu_Quit_f(cmd);
654                                         break;
655                                 }
656                         }
657                         else
658                         {
659                                 switch (m_main_cursor)
660                                 {
661                                 case 0:
662                                         M_Menu_Transfusion_Episode_f(cmd);
663                                         break;
664
665                                 case 1:
666                                         M_Menu_MultiPlayer_f(cmd);
667                                         break;
668
669                                 case 2:
670                                         M_Menu_Options_f(cmd);
671                                         break;
672
673                                 case 3:
674                                         M_Menu_Save_f(cmd);
675                                         break;
676
677                                 case 4:
678                                         M_Menu_Load_f(cmd);
679                                         break;
680
681                                 case 5:
682                                         M_Menu_Help_f(cmd);
683                                         break;
684
685                                 case 6:
686                                         M_Menu_Credits_f(cmd);
687                                         break;
688
689                                 case 7:
690                                         M_Menu_Quit_f(cmd);
691                                         break;
692                                 }
693                         }
694                 }
695                 else
696                 {
697                         switch (m_main_cursor)
698                         {
699                         case 0:
700                                 M_Menu_SinglePlayer_f(cmd);
701                                 break;
702
703                         case 1:
704                                 M_Menu_MultiPlayer_f(cmd);
705                                 break;
706
707                         case 2:
708                                 M_Menu_Options_f(cmd);
709                                 break;
710
711                         case 3:
712                                 M_Menu_Help_f(cmd);
713                                 break;
714
715                         case 4:
716                                 M_Menu_Quit_f(cmd);
717                                 break;
718                         }
719                 }
720         }
721 }
722
723 //=============================================================================
724 /* SINGLE PLAYER MENU */
725
726 static int      m_singleplayer_cursor;
727 #define SINGLEPLAYER_ITEMS      3
728
729
730 void M_Menu_SinglePlayer_f(cmd_state_t *cmd)
731 {
732         key_dest = key_menu;
733         m_state = m_singleplayer;
734         m_entersound = true;
735 }
736
737
738 static void M_SinglePlayer_Draw (void)
739 {
740         cachepic_t      *p;
741         char vabuf[1024];
742
743         M_Background(320, 200);
744
745         M_DrawPic (16, 4, "gfx/qplaque");
746         p = Draw_CachePic ("gfx/ttl_sgl");
747
748         // Some mods don't have a single player mode
749         if (gamemode == GAME_GOODVSBAD2 || gamemode == GAME_BATTLEMECH)
750         {
751                 M_DrawPic ((320 - Draw_GetPicWidth(p)) / 2, 4, "gfx/ttl_sgl");
752
753                 M_DrawTextBox (60, 8 * 8, 23, 4);
754                 if (gamemode == GAME_GOODVSBAD2)
755                         M_Print(95, 10 * 8, "Good Vs Bad 2 is for");
756                 else  // if (gamemode == GAME_BATTLEMECH)
757                         M_Print(95, 10 * 8, "Battlemech is for");
758                 M_Print(83, 11 * 8, "multiplayer play only");
759         }
760         else
761         {
762                 int             f;
763
764                 M_DrawPic ( (320-Draw_GetPicWidth(p))/2, 4, "gfx/ttl_sgl");
765                 M_DrawPic (72, 32, "gfx/sp_menu");
766
767                 f = (int)(host.realtime * 10)%6;
768
769                 M_DrawPic (54, 32 + m_singleplayer_cursor * 20, va(vabuf, sizeof(vabuf), "gfx/menudot%i", f+1));
770         }
771 }
772
773
774 static void M_SinglePlayer_Key(cmd_state_t *cmd, int key, int ascii)
775 {
776         if (gamemode == GAME_GOODVSBAD2 || gamemode == GAME_BATTLEMECH)
777         {
778                 if (key == K_ESCAPE || key == K_ENTER)
779                         m_state = m_main;
780                 return;
781         }
782
783         switch (key)
784         {
785         case K_ESCAPE:
786                 M_Menu_Main_f(cmd);
787                 break;
788
789         case K_DOWNARROW:
790                 S_LocalSound ("sound/misc/menu1.wav");
791                 if (++m_singleplayer_cursor >= SINGLEPLAYER_ITEMS)
792                         m_singleplayer_cursor = 0;
793                 break;
794
795         case K_UPARROW:
796                 S_LocalSound ("sound/misc/menu1.wav");
797                 if (--m_singleplayer_cursor < 0)
798                         m_singleplayer_cursor = SINGLEPLAYER_ITEMS - 1;
799                 break;
800
801         case K_ENTER:
802                 m_entersound = true;
803
804                 switch (m_singleplayer_cursor)
805                 {
806                 case 0:
807                         key_dest = key_game;
808                         if (sv.active)
809                                 Cbuf_AddText(cmd, "disconnect\n");
810                         Cbuf_AddText(cmd, "maxplayers 1\n");
811                         Cbuf_AddText(cmd, "deathmatch 0\n");
812                         Cbuf_AddText(cmd, "coop 0\n");
813                         if (gamemode == GAME_TRANSFUSION)
814                         {
815                                 key_dest = key_menu;
816                                 M_Menu_Transfusion_Episode_f(cmd);
817                                 break;
818                         }
819                         Cbuf_AddText(cmd, "startmap_sp\n");
820                         break;
821
822                 case 1:
823                         M_Menu_Load_f(cmd);
824                         break;
825
826                 case 2:
827                         M_Menu_Save_f(cmd);
828                         break;
829                 }
830         }
831 }
832
833 //=============================================================================
834 /* LOAD/SAVE MENU */
835
836 static int              load_cursor;            ///< 0 < load_cursor < MAX_SAVEGAMES
837
838 static char     m_filenames[MAX_SAVEGAMES][SAVEGAME_COMMENT_LENGTH+1];
839 static int              loadable[MAX_SAVEGAMES];
840
841 static void M_ScanSaves (void)
842 {
843         int             i, j;
844         size_t  len;
845         char    name[MAX_OSPATH];
846         char    buf[SAVEGAME_COMMENT_LENGTH + 256];
847         const char *t;
848         qfile_t *f;
849 //      int             version;
850
851         for (i=0 ; i<MAX_SAVEGAMES ; i++)
852         {
853                 dp_strlcpy (m_filenames[i], "--- UNUSED SLOT ---", sizeof(m_filenames[i]));
854                 loadable[i] = false;
855                 dpsnprintf (name, sizeof(name), "s%i.sav", (int)i);
856                 f = FS_OpenRealFile (name, "rb", false);
857                 if (!f)
858                         continue;
859                 // read enough to get the comment
860                 len = FS_Read(f, buf, sizeof(buf) - 1);
861                 len = min(len, sizeof(buf)-1);
862                 buf[len] = 0;
863                 t = buf;
864                 // version
865                 COM_ParseToken_Simple(&t, false, false, true);
866                 //version = atoi(com_token);
867                 // description
868                 COM_ParseToken_Simple(&t, false, false, true);
869                 dp_strlcpy (m_filenames[i], com_token, sizeof (m_filenames[i]));
870
871         // change _ back to space
872                 for (j=0 ; j<SAVEGAME_COMMENT_LENGTH ; j++)
873                         if (m_filenames[i][j] == '_')
874                                 m_filenames[i][j] = ' ';
875                 loadable[i] = true;
876                 FS_Close (f);
877         }
878 }
879
880 void M_Menu_Load_f(cmd_state_t *cmd)
881 {
882         m_entersound = true;
883         m_state = m_load;
884         key_dest = key_menu;
885         M_ScanSaves ();
886 }
887
888
889 void M_Menu_Save_f(cmd_state_t *cmd)
890 {
891         if (!sv.active)
892                 return;
893 #if 1
894         // LadyHavoc: allow saving multiplayer games
895         if (cl.islocalgame && cl.intermission)
896                 return;
897 #else
898         if (cl.intermission)
899                 return;
900         if (!cl.islocalgame)
901                 return;
902 #endif
903         m_entersound = true;
904         m_state = m_save;
905         key_dest = key_menu;
906         M_ScanSaves ();
907 }
908
909
910 static void M_Load_Draw (void)
911 {
912         int             i;
913         cachepic_t      *p;
914
915         M_Background(320, 200);
916
917         p = Draw_CachePic ("gfx/p_load");
918         M_DrawPic ( (320-Draw_GetPicWidth(p))/2, 4, "gfx/p_load" );
919
920         for (i=0 ; i< MAX_SAVEGAMES; i++)
921                 M_Print(16, 32 + 8*i, m_filenames[i]);
922
923 // line cursor
924         M_DrawCharacter (8, 32 + load_cursor*8, 12+((int)(host.realtime*4)&1));
925 }
926
927
928 static void M_Save_Draw (void)
929 {
930         int             i;
931         cachepic_t      *p;
932
933         M_Background(320, 200);
934
935         p = Draw_CachePic ("gfx/p_save");
936         M_DrawPic ( (320-Draw_GetPicWidth(p))/2, 4, "gfx/p_save");
937
938         for (i=0 ; i<MAX_SAVEGAMES ; i++)
939                 M_Print(16, 32 + 8*i, m_filenames[i]);
940
941 // line cursor
942         M_DrawCharacter (8, 32 + load_cursor*8, 12+((int)(host.realtime*4)&1));
943 }
944
945
946 static void M_Load_Key(cmd_state_t *cmd, int k, int ascii)
947 {
948         char vabuf[1024];
949         switch (k)
950         {
951         case K_ESCAPE:
952                 if (gamemode == GAME_TRANSFUSION)
953                         M_Menu_Main_f(cmd);
954                 else
955                         M_Menu_SinglePlayer_f(cmd);
956                 break;
957
958         case K_ENTER:
959                 S_LocalSound ("sound/misc/menu2.wav");
960                 if (!loadable[load_cursor])
961                         return;
962                 m_state = m_none;
963                 key_dest = key_game;
964
965                 // issue the load command
966                 Cbuf_AddText (cmd, va(vabuf, sizeof(vabuf), "load s%i\n", load_cursor) );
967                 return;
968
969         case K_UPARROW:
970         case K_LEFTARROW:
971                 S_LocalSound ("sound/misc/menu1.wav");
972                 load_cursor--;
973                 if (load_cursor < 0)
974                         load_cursor = MAX_SAVEGAMES-1;
975                 break;
976
977         case K_DOWNARROW:
978         case K_RIGHTARROW:
979                 S_LocalSound ("sound/misc/menu1.wav");
980                 load_cursor++;
981                 if (load_cursor >= MAX_SAVEGAMES)
982                         load_cursor = 0;
983                 break;
984         }
985 }
986
987
988 static void M_Save_Key(cmd_state_t *cmd, int k, int ascii)
989 {
990         char vabuf[1024];
991         switch (k)
992         {
993         case K_ESCAPE:
994                 if (gamemode == GAME_TRANSFUSION)
995                         M_Menu_Main_f(cmd);
996                 else
997                         M_Menu_SinglePlayer_f(cmd);
998                 break;
999
1000         case K_ENTER:
1001                 m_state = m_none;
1002                 key_dest = key_game;
1003                 Cbuf_AddText(cmd, va(vabuf, sizeof(vabuf), "save s%i\n", load_cursor));
1004                 return;
1005
1006         case K_UPARROW:
1007         case K_LEFTARROW:
1008                 S_LocalSound ("sound/misc/menu1.wav");
1009                 load_cursor--;
1010                 if (load_cursor < 0)
1011                         load_cursor = MAX_SAVEGAMES-1;
1012                 break;
1013
1014         case K_DOWNARROW:
1015         case K_RIGHTARROW:
1016                 S_LocalSound ("sound/misc/menu1.wav");
1017                 load_cursor++;
1018                 if (load_cursor >= MAX_SAVEGAMES)
1019                         load_cursor = 0;
1020                 break;
1021         }
1022 }
1023
1024 //=============================================================================
1025 /* Transfusion Single Player Episode Menu */
1026
1027 static int      m_episode_cursor;
1028 #define EPISODE_ITEMS   6
1029
1030 void M_Menu_Transfusion_Episode_f(cmd_state_t *cmd)
1031 {
1032         m_entersound = true;
1033         m_state = m_transfusion_episode;
1034         key_dest = key_menu;
1035 }
1036
1037 static void M_Transfusion_Episode_Draw (void)
1038 {
1039         int y;
1040         cachepic_t *p;
1041         char vabuf[1024];
1042         M_Background(640, 480);
1043
1044         p = Draw_CachePic ("gfx/menu/tb-episodes");
1045         M_DrawPic (640/2 - Draw_GetPicWidth(p)/2, 40, "gfx/menu/tb-episodes");
1046         for (y = 0; y < EPISODE_ITEMS; y++){
1047                 M_DrawPic (0, 160 + y * 40, va(vabuf, sizeof(vabuf), "gfx/menu/episode%i", y+1));
1048         }
1049
1050         M_DrawPic (0, 120 + (m_episode_cursor + 1) * 40, va(vabuf, sizeof(vabuf), "gfx/menu/episode%iselected", m_episode_cursor + 1));
1051 }
1052
1053 static void M_Transfusion_Episode_Key(cmd_state_t *cmd, int key, int ascii)
1054 {
1055         switch (key)
1056         {
1057         case K_ESCAPE:
1058                 M_Menu_Main_f(cmd);
1059                 break;
1060
1061         case K_DOWNARROW:
1062                 S_LocalSound ("sound/misc/menu1.wav");
1063                 m_episode_cursor++;
1064                 if (m_episode_cursor >= EPISODE_ITEMS)
1065                         m_episode_cursor = 0;
1066                 break;
1067
1068         case K_UPARROW:
1069                 S_LocalSound ("sound/misc/menu1.wav");
1070                 m_episode_cursor--;
1071                 if (m_episode_cursor < 0)
1072                         m_episode_cursor = EPISODE_ITEMS - 1;
1073                 break;
1074
1075         case K_ENTER:
1076                 Cbuf_AddText(cmd, "deathmatch 0\n");
1077                 m_entersound = true;
1078                 M_Menu_Transfusion_Skill_f(cmd);
1079         }
1080 }
1081
1082 //=============================================================================
1083 /* Transfusion Single Player Skill Menu */
1084
1085 static int      m_skill_cursor = 2;
1086 #define SKILL_ITEMS     5
1087
1088 void M_Menu_Transfusion_Skill_f(cmd_state_t *cmd)
1089 {
1090         m_entersound = true;
1091         m_state = m_transfusion_skill;
1092         key_dest = key_menu;
1093 }
1094
1095 static void M_Transfusion_Skill_Draw (void)
1096 {
1097         int y;
1098         cachepic_t      *p;
1099         char vabuf[1024];
1100         M_Background(640, 480);
1101
1102         p = Draw_CachePic ("gfx/menu/tb-difficulty");
1103         M_DrawPic(640/2 - Draw_GetPicWidth(p)/2, 40, "gfx/menu/tb-difficulty");
1104
1105         for (y = 0; y < SKILL_ITEMS; y++)
1106         {
1107                 M_DrawPic (0, 180 + y * 40, va(vabuf, sizeof(vabuf), "gfx/menu/difficulty%i", y+1));
1108         }
1109         M_DrawPic (0, 140 + (m_skill_cursor + 1) *40, va(vabuf, sizeof(vabuf), "gfx/menu/difficulty%iselected", m_skill_cursor + 1));
1110 }
1111
1112 static void M_Transfusion_Skill_Key(cmd_state_t *cmd, int key, int ascii)
1113 {
1114         switch (key)
1115         {
1116         case K_ESCAPE:
1117                 M_Menu_Transfusion_Episode_f(cmd);
1118                 break;
1119
1120         case K_DOWNARROW:
1121                 S_LocalSound ("sound/misc/menu1.wav");
1122                 m_skill_cursor++;
1123                 if (m_skill_cursor >= SKILL_ITEMS)
1124                         m_skill_cursor = 0;
1125                 break;
1126
1127         case K_UPARROW:
1128                 S_LocalSound ("sound/misc/menu1.wav");
1129                 m_skill_cursor--;
1130                 if (m_skill_cursor < 0)
1131                         m_skill_cursor = SKILL_ITEMS - 1;
1132                 break;
1133
1134         case K_ENTER:
1135                 m_entersound = true;
1136                 switch (m_skill_cursor)
1137                 {
1138                 case 0:
1139                         Cbuf_AddText(cmd, "skill 1\n");
1140                         break;
1141                 case 1:
1142                         Cbuf_AddText(cmd, "skill 2\n");
1143                         break;
1144                 case 2:
1145                         Cbuf_AddText(cmd, "skill 3\n");
1146                         break;
1147                 case 3:
1148                         Cbuf_AddText(cmd, "skill 4\n");
1149                         break;
1150                 case 4:
1151                         Cbuf_AddText(cmd, "skill 5\n");
1152                         break;
1153                 }
1154                 key_dest = key_game;
1155                 if (sv.active)
1156                         Cbuf_AddText(cmd, "disconnect\n");
1157                 Cbuf_AddText(cmd, "maxplayers 1\n");
1158                 Cbuf_AddText(cmd, "deathmatch 0\n");
1159                 Cbuf_AddText(cmd, "coop 0\n");
1160                 switch (m_episode_cursor)
1161                 {
1162                 case 0:
1163                         Cbuf_AddText(cmd, "map e1m1\n");
1164                         break;
1165                 case 1:
1166                         Cbuf_AddText(cmd, "map e2m1\n");
1167                         break;
1168                 case 2:
1169                         Cbuf_AddText(cmd, "map e3m1\n");
1170                         break;
1171                 case 3:
1172                         Cbuf_AddText(cmd, "map e4m1\n");
1173                         break;
1174                 case 4:
1175                         Cbuf_AddText(cmd, "map e6m1\n");
1176                         break;
1177                 case 5:
1178                         Cbuf_AddText(cmd, "map cp01\n");
1179                         break;
1180                 }
1181         }
1182 }
1183 //=============================================================================
1184 /* MULTIPLAYER MENU */
1185
1186 static int      m_multiplayer_cursor;
1187 #define MULTIPLAYER_ITEMS       3
1188
1189
1190 void M_Menu_MultiPlayer_f(cmd_state_t *cmd)
1191 {
1192         key_dest = key_menu;
1193         m_state = m_multiplayer;
1194         m_entersound = true;
1195 }
1196
1197
1198 static void M_MultiPlayer_Draw (void)
1199 {
1200         int             f;
1201         cachepic_t      *p;
1202         char vabuf[1024];
1203
1204         if (gamemode == GAME_TRANSFUSION)
1205         {
1206                 M_Background(640, 480);
1207                 p = Draw_CachePic ("gfx/menu/tb-online");
1208                 M_DrawPic (640/2 - Draw_GetPicWidth(p)/2, 140, "gfx/menu/tb-online");
1209                 for (f = 1; f <= MULTIPLAYER_ITEMS; f++)
1210                         M_DrawPic (0, 180 + f*40, va(vabuf, sizeof(vabuf), "gfx/menu/online%i", f));
1211                 M_DrawPic (0, 220 + m_multiplayer_cursor * 40, va(vabuf, sizeof(vabuf), "gfx/menu/online%iselected", m_multiplayer_cursor + 1));
1212                 return;
1213         }
1214         M_Background(320, 200);
1215
1216         M_DrawPic (16, 4, "gfx/qplaque");
1217         p = Draw_CachePic ("gfx/p_multi");
1218         M_DrawPic ( (320-Draw_GetPicWidth(p))/2, 4, "gfx/p_multi");
1219         M_DrawPic (72, 32, "gfx/mp_menu");
1220
1221         f = (int)(host.realtime * 10)%6;
1222
1223         M_DrawPic (54, 32 + m_multiplayer_cursor * 20, va(vabuf, sizeof(vabuf), "gfx/menudot%i", f+1));
1224 }
1225
1226
1227 static void M_MultiPlayer_Key(cmd_state_t *cmd, int key, int ascii)
1228 {
1229         switch (key)
1230         {
1231         case K_ESCAPE:
1232                 M_Menu_Main_f(cmd);
1233                 break;
1234
1235         case K_DOWNARROW:
1236                 S_LocalSound ("sound/misc/menu1.wav");
1237                 if (++m_multiplayer_cursor >= MULTIPLAYER_ITEMS)
1238                         m_multiplayer_cursor = 0;
1239                 break;
1240
1241         case K_UPARROW:
1242                 S_LocalSound ("sound/misc/menu1.wav");
1243                 if (--m_multiplayer_cursor < 0)
1244                         m_multiplayer_cursor = MULTIPLAYER_ITEMS - 1;
1245                 break;
1246
1247         case K_ENTER:
1248                 m_entersound = true;
1249                 switch (m_multiplayer_cursor)
1250                 {
1251                 case 0:
1252                 case 1:
1253                         M_Menu_LanConfig_f(cmd);
1254                         break;
1255
1256                 case 2:
1257                         M_Menu_Setup_f(cmd);
1258                         break;
1259                 }
1260         }
1261 }
1262
1263 //=============================================================================
1264 /* SETUP MENU */
1265
1266 static int              setup_cursor = 4;
1267 static int              setup_cursor_table[] = {40, 64, 88, 124, 140};
1268
1269 static char     setup_myname[MAX_SCOREBOARDNAME];
1270 static int              setup_oldtop;
1271 static int              setup_oldbottom;
1272 static int              setup_top;
1273 static int              setup_bottom;
1274 static int              setup_rate;
1275 static int              setup_oldrate;
1276
1277 #define NUM_SETUP_CMDS  5
1278
1279 extern cvar_t cl_topcolor;
1280 extern cvar_t cl_bottomcolor;
1281
1282 void M_Menu_Setup_f(cmd_state_t *cmd)
1283 {
1284         key_dest = key_menu;
1285         m_state = m_setup;
1286         m_entersound = true;
1287         dp_strlcpy(setup_myname, cl_name.string, sizeof(setup_myname));
1288         setup_top = setup_oldtop = cl_topcolor.integer;
1289         setup_bottom = setup_oldbottom = cl_bottomcolor.integer;
1290         setup_rate = cl_rate.integer;
1291 }
1292
1293 static int menuplyr_width, menuplyr_height, menuplyr_top, menuplyr_bottom, menuplyr_load;
1294 static unsigned char *menuplyr_pixels;
1295 static unsigned int *menuplyr_translated;
1296
1297 typedef struct ratetable_s
1298 {
1299         int rate;
1300         const char *name;
1301 }
1302 ratetable_t;
1303
1304 #define RATES ((int)(sizeof(setup_ratetable)/sizeof(setup_ratetable[0])))
1305 static ratetable_t setup_ratetable[] =
1306 {
1307         {1000, "28.8 bad"},
1308         {1500, "28.8 mediocre"},
1309         {2000, "28.8 good"},
1310         {2500, "33.6 mediocre"},
1311         {3000, "33.6 good"},
1312         {3500, "56k bad"},
1313         {4000, "56k mediocre"},
1314         {4500, "56k adequate"},
1315         {5000, "56k good"},
1316         {7000, "64k ISDN"},
1317         {15000, "128k ISDN"},
1318         {25000, "broadband"}
1319 };
1320
1321 static int setup_rateindex(int rate)
1322 {
1323         int i;
1324         for (i = 0;i < RATES;i++)
1325                 if (setup_ratetable[i].rate > setup_rate)
1326                         break;
1327         return bound(1, i, RATES) - 1;
1328 }
1329
1330 static void M_Setup_Draw (void)
1331 {
1332         int i, j;
1333         cachepic_t      *p;
1334         char vabuf[1024];
1335
1336         M_Background(320, 200);
1337
1338         M_DrawPic (16, 4, "gfx/qplaque");
1339         p = Draw_CachePic ("gfx/p_multi");
1340         M_DrawPic ( (320-Draw_GetPicWidth(p))/2, 4, "gfx/p_multi");
1341
1342         M_Print(64, 40, "Your name");
1343         M_DrawTextBox (160, 32, 16, 1);
1344         M_PrintColored(168, 40, setup_myname);
1345
1346         if (gamemode != GAME_GOODVSBAD2)
1347         {
1348                 M_Print(64, 64, "Shirt color");
1349                 M_Print(64, 88, "Pants color");
1350         }
1351
1352         M_Print(64, 124-8, "Network speed limit");
1353         M_Print(168, 124, va(vabuf, sizeof(vabuf), "%i (%s)", setup_rate, setup_ratetable[setup_rateindex(setup_rate)].name));
1354
1355         M_DrawTextBox (64, 140-8, 14, 1);
1356         M_Print(72, 140, "Accept Changes");
1357
1358         // LadyHavoc: rewrote this code greatly
1359         if (menuplyr_load)
1360         {
1361                 unsigned char *f;
1362                 fs_offset_t filesize;
1363                 menuplyr_load = false;
1364                 menuplyr_top = -1;
1365                 menuplyr_bottom = -1;
1366                 f = FS_LoadFile("gfx/menuplyr.lmp", tempmempool, true, &filesize);
1367                 if (f && filesize >= 9)
1368                 {
1369                         int width, height;
1370                         width = f[0] + f[1] * 256 + f[2] * 65536 + f[3] * 16777216;
1371                         height = f[4] + f[5] * 256 + f[6] * 65536 + f[7] * 16777216;
1372                         if (filesize >= 8 + width * height)
1373                         {
1374                                 menuplyr_width = width;
1375                                 menuplyr_height = height;
1376                                 menuplyr_pixels = (unsigned char *)Mem_Alloc(cls.permanentmempool, width * height);
1377                                 menuplyr_translated = (unsigned int *)Mem_Alloc(cls.permanentmempool, width * height * 4);
1378                                 memcpy(menuplyr_pixels, f + 8, width * height);
1379                         }
1380                 }
1381                 if (f)
1382                         Mem_Free(f);
1383         }
1384
1385         if (menuplyr_pixels)
1386         {
1387                 if (menuplyr_top != setup_top || menuplyr_bottom != setup_bottom)
1388                 {
1389                         menuplyr_top = setup_top;
1390                         menuplyr_bottom = setup_bottom;
1391
1392                         for (i = 0;i < menuplyr_width * menuplyr_height;i++)
1393                         {
1394                                 j = menuplyr_pixels[i];
1395                                 if (j >= TOP_RANGE && j < TOP_RANGE + 16)
1396                                 {
1397                                         if (menuplyr_top < 8 || menuplyr_top == 14)
1398                                                 j = menuplyr_top * 16 + (j - TOP_RANGE);
1399                                         else
1400                                                 j = menuplyr_top * 16 + 15-(j - TOP_RANGE);
1401                                 }
1402                                 else if (j >= BOTTOM_RANGE && j < BOTTOM_RANGE + 16)
1403                                 {
1404                                         if (menuplyr_bottom < 8 || menuplyr_bottom == 14)
1405                                                 j = menuplyr_bottom * 16 + (j - BOTTOM_RANGE);
1406                                         else
1407                                                 j = menuplyr_bottom * 16 + 15-(j - BOTTOM_RANGE);
1408                                 }
1409                                 menuplyr_translated[i] = palette_bgra_transparent[j];
1410                         }
1411                         Draw_NewPic("gfx/menuplyr", menuplyr_width, menuplyr_height, (unsigned char *)menuplyr_translated, TEXTYPE_BGRA, TEXF_CLAMP);
1412                 }
1413                 M_DrawPic(160, 48, "gfx/bigbox");
1414                 M_DrawPic(172, 56, "gfx/menuplyr");
1415         }
1416
1417         if (setup_cursor == 0)
1418                 M_DrawCharacter (168 + 8*strlen(setup_myname), setup_cursor_table [setup_cursor], 10+((int)(host.realtime*4)&1));
1419         else
1420                 M_DrawCharacter (56, setup_cursor_table [setup_cursor], 12+((int)(host.realtime*4)&1));
1421 }
1422
1423
1424 static void M_Setup_Key(cmd_state_t *cmd, int k, int ascii)
1425 {
1426         int                     l;
1427         char vabuf[1024];
1428
1429         switch (k)
1430         {
1431         case K_ESCAPE:
1432                 M_Menu_MultiPlayer_f(cmd);
1433                 break;
1434
1435         case K_UPARROW:
1436                 S_LocalSound ("sound/misc/menu1.wav");
1437                 setup_cursor--;
1438                 if (setup_cursor < 0)
1439                         setup_cursor = NUM_SETUP_CMDS-1;
1440                 break;
1441
1442         case K_DOWNARROW:
1443                 S_LocalSound ("sound/misc/menu1.wav");
1444                 setup_cursor++;
1445                 if (setup_cursor >= NUM_SETUP_CMDS)
1446                         setup_cursor = 0;
1447                 break;
1448
1449         case K_LEFTARROW:
1450                 if (setup_cursor < 1)
1451                         return;
1452                 S_LocalSound ("sound/misc/menu3.wav");
1453                 if (setup_cursor == 1)
1454                         setup_top = setup_top - 1;
1455                 if (setup_cursor == 2)
1456                         setup_bottom = setup_bottom - 1;
1457                 if (setup_cursor == 3)
1458                 {
1459                         l = setup_rateindex(setup_rate) - 1;
1460                         if (l < 0)
1461                                 l = RATES - 1;
1462                         setup_rate = setup_ratetable[l].rate;
1463                 }
1464                 break;
1465         case K_RIGHTARROW:
1466                 if (setup_cursor < 1)
1467                         return;
1468 forward:
1469                 S_LocalSound ("sound/misc/menu3.wav");
1470                 if (setup_cursor == 1)
1471                         setup_top = setup_top + 1;
1472                 if (setup_cursor == 2)
1473                         setup_bottom = setup_bottom + 1;
1474                 if (setup_cursor == 3)
1475                 {
1476                         l = setup_rateindex(setup_rate) + 1;
1477                         if (l >= RATES)
1478                                 l = 0;
1479                         setup_rate = setup_ratetable[l].rate;
1480                 }
1481                 break;
1482
1483         case K_ENTER:
1484                 if (setup_cursor == 0)
1485                         return;
1486
1487                 if (setup_cursor == 1 || setup_cursor == 2 || setup_cursor == 3)
1488                         goto forward;
1489
1490                 // setup_cursor == 4 (Accept changes)
1491                 if (strcmp(cl_name.string, setup_myname) != 0)
1492                         Cbuf_AddText(cmd, va(vabuf, sizeof(vabuf), "name \"%s\"\n", setup_myname) );
1493                 if (setup_top != setup_oldtop || setup_bottom != setup_oldbottom)
1494                         Cbuf_AddText(cmd, va(vabuf, sizeof(vabuf), "color %i %i\n", setup_top, setup_bottom) );
1495                 if (setup_rate != setup_oldrate)
1496                         Cbuf_AddText(cmd, va(vabuf, sizeof(vabuf), "rate %i\n", setup_rate));
1497
1498                 m_entersound = true;
1499                 M_Menu_MultiPlayer_f(cmd);
1500                 break;
1501
1502         case K_BACKSPACE:
1503                 if (setup_cursor == 0)
1504                 {
1505                         if (strlen(setup_myname))
1506                                 setup_myname[strlen(setup_myname)-1] = 0;
1507                 }
1508                 break;
1509
1510         default:
1511                 if (ascii < 32)
1512                         break;
1513                 if (setup_cursor == 0)
1514                 {
1515                         l = (int)strlen(setup_myname);
1516                         if (l < 15)
1517                         {
1518                                 setup_myname[l+1] = 0;
1519                                 setup_myname[l] = ascii;
1520                         }
1521                 }
1522         }
1523
1524         if (setup_top > 15)
1525                 setup_top = 0;
1526         if (setup_top < 0)
1527                 setup_top = 15;
1528         if (setup_bottom > 15)
1529                 setup_bottom = 0;
1530         if (setup_bottom < 0)
1531                 setup_bottom = 15;
1532 }
1533
1534 //=============================================================================
1535 /* OPTIONS MENU */
1536
1537 #define SLIDER_RANGE    10
1538
1539 static void M_DrawSlider (int x, int y, float num, float rangemin, float rangemax)
1540 {
1541         char text[16];
1542         int i;
1543         float range;
1544         range = bound(0, (num - rangemin) / (rangemax - rangemin), 1);
1545         M_DrawCharacter (x-8, y, 128);
1546         for (i = 0;i < SLIDER_RANGE;i++)
1547                 M_DrawCharacter (x + i*8, y, 129);
1548         M_DrawCharacter (x+i*8, y, 130);
1549         M_DrawCharacter (x + (SLIDER_RANGE-1)*8 * range, y, 131);
1550         if (fabs((int)num - num) < 0.01)
1551                 dpsnprintf(text, sizeof(text), "%i", (int)num);
1552         else
1553                 dpsnprintf(text, sizeof(text), "%.3f", num);
1554         M_Print(x + (SLIDER_RANGE+2) * 8, y, text);
1555 }
1556
1557 static void M_DrawCheckbox (int x, int y, int on)
1558 {
1559         if (on)
1560                 M_Print(x, y, "on");
1561         else
1562                 M_Print(x, y, "off");
1563 }
1564
1565
1566 //#define OPTIONS_ITEMS 25 aule was here
1567 #define OPTIONS_ITEMS 27
1568
1569
1570 static int options_cursor;
1571
1572 void M_Menu_Options_f(cmd_state_t *cmd)
1573 {
1574         key_dest = key_menu;
1575         m_state = m_options;
1576         m_entersound = true;
1577 }
1578
1579 extern cvar_t host_timescale;
1580 extern dllhandle_t jpeg_dll;
1581 extern cvar_t gl_texture_anisotropy;
1582 extern cvar_t r_textshadow;
1583 extern cvar_t r_hdr_scenebrightness;
1584
1585 static void M_Menu_Options_AdjustSliders (int dir)
1586 {
1587         int optnum;
1588         double f;
1589         S_LocalSound ("sound/misc/menu3.wav");
1590
1591         optnum = 0;
1592              if (options_cursor == optnum++) ;
1593         else if (options_cursor == optnum++) ;
1594         else if (options_cursor == optnum++) ;
1595         else if (options_cursor == optnum++) ;
1596         else if (options_cursor == optnum++) Cvar_SetValueQuick(&crosshair, bound(0, crosshair.integer + dir, 7));
1597         else if (options_cursor == optnum++) Cvar_SetValueQuick(&sensitivity, bound(1, sensitivity.value + dir * 0.5, 50));
1598         else if (options_cursor == optnum++) Cvar_SetValueQuick(&m_pitch, -m_pitch.value);
1599         else if (options_cursor == optnum++) Cvar_SetValueQuick(&scr_fov, bound(1, scr_fov.integer + dir * 1, 170));
1600         else if (options_cursor == optnum++)
1601         {
1602                 if (cl_forwardspeed.value > 200)
1603                 {
1604                         Cvar_SetValueQuick (&cl_forwardspeed, 200);
1605                         Cvar_SetValueQuick (&cl_backspeed, 200);
1606                 }
1607                 else
1608                 {
1609                         Cvar_SetValueQuick (&cl_forwardspeed, 400);
1610                         Cvar_SetValueQuick (&cl_backspeed, 400);
1611                 }
1612         }
1613         else if (options_cursor == optnum++) Cvar_SetValueQuick(&cl_showfps, !cl_showfps.integer);
1614         else if (options_cursor == optnum++) {f = !(cl_showdate.integer && cl_showtime.integer);Cvar_SetValueQuick(&cl_showdate, f);Cvar_SetValueQuick(&cl_showtime, f);}
1615         else if (options_cursor == optnum++) ;
1616         else if (options_cursor == optnum++) Cvar_SetValueQuick(&r_hdr_scenebrightness, bound(1, r_hdr_scenebrightness.value + dir * 0.0625, 4));
1617         else if (options_cursor == optnum++) Cvar_SetValueQuick(&v_contrast, bound(1, v_contrast.value + dir * 0.0625, 4));
1618         else if (options_cursor == optnum++) Cvar_SetValueQuick(&v_gamma, bound(0.5, v_gamma.value + dir * 0.0625, 3));
1619         else if (options_cursor == optnum++) Cvar_SetValueQuick(&volume, bound(0, volume.value + dir * 0.0625, 1));
1620         else if (options_cursor == optnum++) Cvar_SetValueQuick(&bgmvolume, bound(0, bgmvolume.value + dir * 0.0625, 1));
1621 }
1622
1623 static int m_optnum;
1624 static int m_opty;
1625 static int m_optcursor;
1626
1627 static void M_Options_PrintCommand(const char *s, int enabled)
1628 {
1629         if (m_opty >= 32)
1630         {
1631                 if (m_optnum == m_optcursor)
1632                         DrawQ_Fill(menu_x + 48, menu_y + m_opty, 320, 8, m_optnum == m_optcursor ? (0.5 + 0.2 * sin(host.realtime * M_PI)) : 0, 0, 0, 0.5, 0);
1633                 M_ItemPrint(0 + 48, m_opty, s, enabled);
1634         }
1635         m_opty += 8;
1636         m_optnum++;
1637 }
1638
1639 static void M_Options_PrintCheckbox(const char *s, int enabled, int yes)
1640 {
1641         if (m_opty >= 32)
1642         {
1643                 if (m_optnum == m_optcursor)
1644                         DrawQ_Fill(menu_x + 48, menu_y + m_opty, 320, 8, m_optnum == m_optcursor ? (0.5 + 0.2 * sin(host.realtime * M_PI)) : 0, 0, 0, 0.5, 0);
1645                 M_ItemPrint(0 + 48, m_opty, s, enabled);
1646                 M_DrawCheckbox(0 + 48 + (int)strlen(s) * 8 + 8, m_opty, yes);
1647         }
1648         m_opty += 8;
1649         m_optnum++;
1650 }
1651
1652 static void M_Options_PrintSlider(const char *s, int enabled, float value, float minvalue, float maxvalue)
1653 {
1654         if (m_opty >= 32)
1655         {
1656                 if (m_optnum == m_optcursor)
1657                         DrawQ_Fill(menu_x + 48, menu_y + m_opty, 320, 8, m_optnum == m_optcursor ? (0.5 + 0.2 * sin(host.realtime * M_PI)) : 0, 0, 0, 0.5, 0);
1658                 M_ItemPrint(0 + 48, m_opty, s, enabled);
1659                 M_DrawSlider(0 + 48 + (int)strlen(s) * 8 + 8, m_opty, value, minvalue, maxvalue);
1660         }
1661         m_opty += 8;
1662         m_optnum++;
1663 }
1664
1665 static void M_Options_Draw (void)
1666 {
1667         int visible;
1668         cachepic_t      *p;
1669
1670         M_Background(320, bound(200, 32 + OPTIONS_ITEMS * 8, vid_conheight.integer));
1671
1672         M_DrawPic(16, 4, "gfx/qplaque");
1673         p = Draw_CachePic ("gfx/p_option");
1674         M_DrawPic((320-Draw_GetPicWidth(p))/2, 4, "gfx/p_option");
1675
1676         m_optnum = 0;
1677         m_optcursor = options_cursor;
1678         visible = (int)((menu_height - 32) / 8);
1679         m_opty = 32 - bound(0, m_optcursor - (visible >> 1), max(0, OPTIONS_ITEMS - visible)) * 8;
1680
1681         M_Options_PrintCommand( "    Customize controls", true);
1682         M_Options_PrintCommand( "         Go to console", true);
1683         M_Options_PrintCommand( "     Reset to defaults", true);
1684         M_Options_PrintCommand( "     Change Video Mode", true);
1685         M_Options_PrintSlider(  "             Crosshair", true, crosshair.value, 0, 7);
1686         M_Options_PrintSlider(  "           Mouse Speed", true, sensitivity.value, 1, 50);
1687         M_Options_PrintCheckbox("          Invert Mouse", true, m_pitch.value < 0);
1688         M_Options_PrintSlider(  "         Field of View", true, scr_fov.integer, 1, 170);
1689         M_Options_PrintCheckbox("            Always Run", true, cl_forwardspeed.value > 200);
1690         M_Options_PrintCheckbox("        Show Framerate", true, cl_showfps.integer);
1691         M_Options_PrintCheckbox("    Show Date and Time", true, cl_showdate.integer && cl_showtime.integer);
1692         M_Options_PrintCommand( "     Custom Brightness", true);
1693         M_Options_PrintSlider(  "       Game Brightness", true, r_hdr_scenebrightness.value, 1, 4);
1694         M_Options_PrintSlider(  "            Brightness", true, v_contrast.value, 1, 2);
1695         M_Options_PrintSlider(  "                 Gamma", true, v_gamma.value, 0.5, 3);
1696         M_Options_PrintSlider(  "          Sound Volume", snd_initialized.integer, volume.value, 0, 1);
1697         M_Options_PrintSlider(  "          Music Volume", cdaudioinitialized.integer, bgmvolume.value, 0, 1);
1698         M_Options_PrintCommand( "     Customize Effects", true);
1699         M_Options_PrintCommand( "       Effects:  Quake", true);
1700         M_Options_PrintCommand( "       Effects: Normal", true);
1701         M_Options_PrintCommand( "       Effects:   High", true);
1702         M_Options_PrintCommand( "    Customize Lighting", true);
1703         M_Options_PrintCommand( "      Lighting: Flares", true);
1704         M_Options_PrintCommand( "      Lighting: Normal", true);
1705         M_Options_PrintCommand( "      Lighting:   High", true);
1706         M_Options_PrintCommand( "      Lighting:   Full", true);
1707         M_Options_PrintCommand( "           Browse Mods", true);
1708 }
1709
1710
1711 static void M_Options_Key(cmd_state_t *cmd, int k, int ascii)
1712 {
1713         switch (k)
1714         {
1715         case K_ESCAPE:
1716                 M_Menu_Main_f(cmd);
1717                 break;
1718
1719         case K_ENTER:
1720                 m_entersound = true;
1721                 switch (options_cursor)
1722                 {
1723                 case 0:
1724                         M_Menu_Keys_f(cmd);
1725                         break;
1726                 case 1:
1727                         m_state = m_none;
1728                         key_dest = key_game;
1729                         Con_ToggleConsole_f(cmd);
1730                         break;
1731                 case 2:
1732                         M_Menu_Reset_f(cmd);
1733                         break;
1734                 case 3:
1735                         M_Menu_Video_f(cmd);
1736                         break;
1737                 case 11:
1738                         M_Menu_Options_ColorControl_f(cmd);
1739                         break;
1740                 case 17: // Customize Effects
1741                         M_Menu_Options_Effects_f(cmd);
1742                         break;
1743                 case 18: // Effects: Quake
1744                         Cbuf_AddText(cmd, "cl_particles 1;cl_particles_quake 1;cl_particles_quality 1;cl_particles_explosions_shell 0;r_explosionclip 1;cl_stainmaps 0;cl_stainmaps_clearonload 1;cl_decals 0;cl_particles_bulletimpacts 1;cl_particles_smoke 1;cl_particles_sparks 1;cl_particles_bubbles 1;cl_particles_blood 1;cl_particles_blood_alpha 1;cl_particles_blood_bloodhack 0;cl_beams_polygons 0;cl_beams_instantaimhack 0;cl_beams_quakepositionhack 1;cl_beams_lightatend 0;r_lerpmodels 1;r_lerpsprites 1;r_lerplightstyles 0;gl_polyblend 1;r_skyscroll1 1;r_skyscroll2 2;r_waterwarp 1;r_wateralpha 1;r_waterscroll 1\n");
1745                         break;
1746                 case 19: // Effects: Normal
1747                         Cbuf_AddText(cmd, "cl_particles 1;cl_particles_quake 0;cl_particles_quality 1;cl_particles_explosions_shell 0;r_explosionclip 1;cl_stainmaps 0;cl_stainmaps_clearonload 1;cl_decals 1;cl_particles_bulletimpacts 1;cl_particles_smoke 1;cl_particles_sparks 1;cl_particles_bubbles 1;cl_particles_blood 1;cl_particles_blood_alpha 1;cl_particles_blood_bloodhack 1;cl_beams_polygons 1;cl_beams_instantaimhack 0;cl_beams_quakepositionhack 1;cl_beams_lightatend 0;r_lerpmodels 1;r_lerpsprites 1;r_lerplightstyles 0;gl_polyblend 1;r_skyscroll1 1;r_skyscroll2 2;r_waterwarp 1;r_wateralpha 1;r_waterscroll 1\n");
1748                         break;
1749                 case 20: // Effects: High
1750                         Cbuf_AddText(cmd, "cl_particles 1;cl_particles_quake 0;cl_particles_quality 2;cl_particles_explosions_shell 0;r_explosionclip 1;cl_stainmaps 1;cl_stainmaps_clearonload 1;cl_decals 1;cl_particles_bulletimpacts 1;cl_particles_smoke 1;cl_particles_sparks 1;cl_particles_bubbles 1;cl_particles_blood 1;cl_particles_blood_alpha 1;cl_particles_blood_bloodhack 1;cl_beams_polygons 1;cl_beams_instantaimhack 0;cl_beams_quakepositionhack 1;cl_beams_lightatend 0;r_lerpmodels 1;r_lerpsprites 1;r_lerplightstyles 0;gl_polyblend 1;r_skyscroll1 1;r_skyscroll2 2;r_waterwarp 1;r_wateralpha 1;r_waterscroll 1\n");
1751                         break;
1752                 case 21:
1753                         M_Menu_Options_Graphics_f(cmd);
1754                         break;
1755                 case 22: // Lighting: Flares
1756                         Cbuf_AddText(cmd, "r_coronas 1;gl_flashblend 1;r_shadow_gloss 0;r_shadow_realtime_dlight 0;r_shadow_realtime_dlight_shadows 0;r_shadow_realtime_world 0;r_shadow_realtime_world_lightmaps 0;r_shadow_realtime_world_shadows 1;r_bloom 0");
1757                         break;
1758                 case 23: // Lighting: Normal
1759                         Cbuf_AddText(cmd, "r_coronas 1;gl_flashblend 0;r_shadow_gloss 1;r_shadow_realtime_dlight 1;r_shadow_realtime_dlight_shadows 0;r_shadow_realtime_world 0;r_shadow_realtime_world_lightmaps 0;r_shadow_realtime_world_shadows 1;r_bloom 0");
1760                         break;
1761                 case 24: // Lighting: High
1762                         Cbuf_AddText(cmd, "r_coronas 1;gl_flashblend 0;r_shadow_gloss 1;r_shadow_realtime_dlight 1;r_shadow_realtime_dlight_shadows 1;r_shadow_realtime_world 0;r_shadow_realtime_world_lightmaps 0;r_shadow_realtime_world_shadows 1;r_bloom 1");
1763                         break;
1764                 case 25: // Lighting: Full
1765                         Cbuf_AddText(cmd, "r_coronas 1;gl_flashblend 0;r_shadow_gloss 1;r_shadow_realtime_dlight 1;r_shadow_realtime_dlight_shadows 1;r_shadow_realtime_world 1;r_shadow_realtime_world_lightmaps 0;r_shadow_realtime_world_shadows 1;r_bloom 1");
1766                         break;
1767                 case 26:
1768                         M_Menu_ModList_f(cmd);
1769                         break;
1770                 default:
1771                         M_Menu_Options_AdjustSliders (1);
1772                         break;
1773                 }
1774                 return;
1775
1776         case K_UPARROW:
1777                 S_LocalSound ("sound/misc/menu1.wav");
1778                 options_cursor--;
1779                 if (options_cursor < 0)
1780                         options_cursor = OPTIONS_ITEMS-1;
1781                 break;
1782
1783         case K_DOWNARROW:
1784                 S_LocalSound ("sound/misc/menu1.wav");
1785                 options_cursor++;
1786                 if (options_cursor >= OPTIONS_ITEMS)
1787                         options_cursor = 0;
1788                 break;
1789
1790         case K_LEFTARROW:
1791                 M_Menu_Options_AdjustSliders (-1);
1792                 break;
1793
1794         case K_RIGHTARROW:
1795                 M_Menu_Options_AdjustSliders (1);
1796                 break;
1797         }
1798 }
1799
1800 #define OPTIONS_EFFECTS_ITEMS   35
1801
1802 static int options_effects_cursor;
1803
1804 void M_Menu_Options_Effects_f(cmd_state_t *cmd)
1805 {
1806         key_dest = key_menu;
1807         m_state = m_options_effects;
1808         m_entersound = true;
1809 }
1810
1811
1812 extern cvar_t cl_stainmaps;
1813 extern cvar_t cl_stainmaps_clearonload;
1814 extern cvar_t r_explosionclip;
1815 extern cvar_t r_coronas;
1816 extern cvar_t gl_flashblend;
1817 extern cvar_t cl_beams_polygons;
1818 extern cvar_t cl_beams_quakepositionhack;
1819 extern cvar_t cl_beams_instantaimhack;
1820 extern cvar_t cl_beams_lightatend;
1821 extern cvar_t r_lightningbeam_thickness;
1822 extern cvar_t r_lightningbeam_scroll;
1823 extern cvar_t r_lightningbeam_repeatdistance;
1824 extern cvar_t r_lightningbeam_color_red;
1825 extern cvar_t r_lightningbeam_color_green;
1826 extern cvar_t r_lightningbeam_color_blue;
1827 extern cvar_t r_lightningbeam_qmbtexture;
1828
1829 static void M_Menu_Options_Effects_AdjustSliders (int dir)
1830 {
1831         int optnum;
1832         S_LocalSound ("sound/misc/menu3.wav");
1833
1834         optnum = 0;
1835              if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&cl_particles, !cl_particles.integer);
1836         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&cl_particles_quake, !cl_particles_quake.integer);
1837         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&cl_particles_quality, bound(1, cl_particles_quality.value + dir * 0.5, 4));
1838         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&cl_particles_explosions_shell, !cl_particles_explosions_shell.integer);
1839         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&r_explosionclip, !r_explosionclip.integer);
1840         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&cl_stainmaps, !cl_stainmaps.integer);
1841         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&cl_stainmaps_clearonload, !cl_stainmaps_clearonload.integer);
1842         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&cl_decals, !cl_decals.integer);
1843         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&cl_particles_bulletimpacts, !cl_particles_bulletimpacts.integer);
1844         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&cl_particles_smoke, !cl_particles_smoke.integer);
1845         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&cl_particles_sparks, !cl_particles_sparks.integer);
1846         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&cl_particles_bubbles, !cl_particles_bubbles.integer);
1847         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&cl_particles_blood, !cl_particles_blood.integer);
1848         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&cl_particles_blood_alpha, bound(0.2, cl_particles_blood_alpha.value + dir * 0.1, 1));
1849         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&cl_particles_blood_bloodhack, !cl_particles_blood_bloodhack.integer);
1850         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&cl_beams_polygons, !cl_beams_polygons.integer);
1851         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&cl_beams_instantaimhack, !cl_beams_instantaimhack.integer);
1852         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&cl_beams_quakepositionhack, !cl_beams_quakepositionhack.integer);
1853         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&cl_beams_lightatend, !cl_beams_lightatend.integer);
1854         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&r_lightningbeam_thickness, bound(1, r_lightningbeam_thickness.integer + dir, 10));
1855         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&r_lightningbeam_scroll, bound(0, r_lightningbeam_scroll.integer + dir, 10));
1856         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&r_lightningbeam_repeatdistance, bound(64, r_lightningbeam_repeatdistance.integer + dir * 64, 1024));
1857         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&r_lightningbeam_color_red, bound(0, r_lightningbeam_color_red.value + dir * 0.1, 1));
1858         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&r_lightningbeam_color_green, bound(0, r_lightningbeam_color_green.value + dir * 0.1, 1));
1859         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&r_lightningbeam_color_blue, bound(0, r_lightningbeam_color_blue.value + dir * 0.1, 1));
1860         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&r_lightningbeam_qmbtexture, !r_lightningbeam_qmbtexture.integer);
1861         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&r_lerpmodels, !r_lerpmodels.integer);
1862         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&r_lerpsprites, !r_lerpsprites.integer);
1863         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&r_lerplightstyles, !r_lerplightstyles.integer);
1864         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&gl_polyblend, bound(0, gl_polyblend.value + dir * 0.1, 1));
1865         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&r_skyscroll1, bound(-8, r_skyscroll1.value + dir * 0.1, 8));
1866         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&r_skyscroll2, bound(-8, r_skyscroll2.value + dir * 0.1, 8));
1867         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&r_waterwarp, bound(0, r_waterwarp.value + dir * 0.1, 1));
1868         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&r_wateralpha, bound(0, r_wateralpha.value + dir * 0.1, 1));
1869         else if (options_effects_cursor == optnum++) Cvar_SetValueQuick (&r_waterscroll, bound(0, r_waterscroll.value + dir * 0.5, 10));
1870 }
1871
1872 static void M_Options_Effects_Draw (void)
1873 {
1874         int visible;
1875         cachepic_t      *p;
1876
1877         M_Background(320, bound(200, 32 + OPTIONS_EFFECTS_ITEMS * 8, vid_conheight.integer));
1878
1879         M_DrawPic(16, 4, "gfx/qplaque");
1880         p = Draw_CachePic ("gfx/p_option");
1881         M_DrawPic((320-Draw_GetPicWidth(p))/2, 4, "gfx/p_option");
1882
1883         m_optcursor = options_effects_cursor;
1884         m_optnum = 0;
1885         visible = (int)((menu_height - 32) / 8);
1886         m_opty = 32 - bound(0, m_optcursor - (visible >> 1), max(0, OPTIONS_EFFECTS_ITEMS - visible)) * 8;
1887
1888         M_Options_PrintCheckbox("             Particles", true, cl_particles.integer);
1889         M_Options_PrintCheckbox(" Quake-style Particles", true, cl_particles_quake.integer);
1890         M_Options_PrintSlider(  "     Particles Quality", true, cl_particles_quality.value, 1, 4);
1891         M_Options_PrintCheckbox("       Explosion Shell", true, cl_particles_explosions_shell.integer);
1892         M_Options_PrintCheckbox("  Explosion Shell Clip", true, r_explosionclip.integer);
1893         M_Options_PrintCheckbox("             Stainmaps", true, cl_stainmaps.integer);
1894         M_Options_PrintCheckbox("Onload Clear Stainmaps", true, cl_stainmaps_clearonload.integer);
1895         M_Options_PrintCheckbox("                Decals", true, cl_decals.integer);
1896         M_Options_PrintCheckbox("        Bullet Impacts", true, cl_particles_bulletimpacts.integer);
1897         M_Options_PrintCheckbox("                 Smoke", true, cl_particles_smoke.integer);
1898         M_Options_PrintCheckbox("                Sparks", true, cl_particles_sparks.integer);
1899         M_Options_PrintCheckbox("               Bubbles", true, cl_particles_bubbles.integer);
1900         M_Options_PrintCheckbox("                 Blood", true, cl_particles_blood.integer);
1901         M_Options_PrintSlider(  "         Blood Opacity", true, cl_particles_blood_alpha.value, 0.2, 1);
1902         M_Options_PrintCheckbox("Force New Blood Effect", true, cl_particles_blood_bloodhack.integer);
1903         M_Options_PrintCheckbox("     Polygon Lightning", true, cl_beams_polygons.integer);
1904         M_Options_PrintCheckbox("Smooth Sweep Lightning", true, cl_beams_instantaimhack.integer);
1905         M_Options_PrintCheckbox(" Waist-level Lightning", true, cl_beams_quakepositionhack.integer);
1906         M_Options_PrintCheckbox("   Lightning End Light", true, cl_beams_lightatend.integer);
1907         M_Options_PrintSlider(  "   Lightning Thickness", cl_beams_polygons.integer, r_lightningbeam_thickness.integer, 1, 10);
1908         M_Options_PrintSlider(  "      Lightning Scroll", cl_beams_polygons.integer, r_lightningbeam_scroll.integer, 0, 10);
1909         M_Options_PrintSlider(  " Lightning Repeat Dist", cl_beams_polygons.integer, r_lightningbeam_repeatdistance.integer, 64, 1024);
1910         M_Options_PrintSlider(  "   Lightning Color Red", cl_beams_polygons.integer, r_lightningbeam_color_red.value, 0, 1);
1911         M_Options_PrintSlider(  " Lightning Color Green", cl_beams_polygons.integer, r_lightningbeam_color_green.value, 0, 1);
1912         M_Options_PrintSlider(  "  Lightning Color Blue", cl_beams_polygons.integer, r_lightningbeam_color_blue.value, 0, 1);
1913         M_Options_PrintCheckbox(" Lightning QMB Texture", cl_beams_polygons.integer, r_lightningbeam_qmbtexture.integer);
1914         M_Options_PrintCheckbox("   Model Interpolation", true, r_lerpmodels.integer);
1915         M_Options_PrintCheckbox("  Sprite Interpolation", true, r_lerpsprites.integer);
1916         M_Options_PrintCheckbox(" Flicker Interpolation", true, r_lerplightstyles.integer);
1917         M_Options_PrintSlider(  "            View Blend", true, gl_polyblend.value, 0, 1);
1918         M_Options_PrintSlider(  "Upper Sky Scroll Speed", true, r_skyscroll1.value, -8, 8);
1919         M_Options_PrintSlider(  "Lower Sky Scroll Speed", true, r_skyscroll2.value, -8, 8);
1920         M_Options_PrintSlider(  "  Underwater View Warp", true, r_waterwarp.value, 0, 1);
1921         M_Options_PrintSlider(  " Water Alpha (opacity)", true, r_wateralpha.value, 0, 1);
1922         M_Options_PrintSlider(  "        Water Movement", true, r_waterscroll.value, 0, 10);
1923 }
1924
1925
1926 static void M_Options_Effects_Key(cmd_state_t *cmd, int k, int ascii)
1927 {
1928         switch (k)
1929         {
1930         case K_ESCAPE:
1931                 M_Menu_Options_f(cmd);
1932                 break;
1933
1934         case K_ENTER:
1935                 M_Menu_Options_Effects_AdjustSliders (1);
1936                 break;
1937
1938         case K_UPARROW:
1939                 S_LocalSound ("sound/misc/menu1.wav");
1940                 options_effects_cursor--;
1941                 if (options_effects_cursor < 0)
1942                         options_effects_cursor = OPTIONS_EFFECTS_ITEMS-1;
1943                 break;
1944
1945         case K_DOWNARROW:
1946                 S_LocalSound ("sound/misc/menu1.wav");
1947                 options_effects_cursor++;
1948                 if (options_effects_cursor >= OPTIONS_EFFECTS_ITEMS)
1949                         options_effects_cursor = 0;
1950                 break;
1951
1952         case K_LEFTARROW:
1953                 M_Menu_Options_Effects_AdjustSliders (-1);
1954                 break;
1955
1956         case K_RIGHTARROW:
1957                 M_Menu_Options_Effects_AdjustSliders (1);
1958                 break;
1959         }
1960 }
1961
1962
1963 #define OPTIONS_GRAPHICS_ITEMS  20
1964
1965 static int options_graphics_cursor;
1966
1967 void M_Menu_Options_Graphics_f(cmd_state_t *cmd)
1968 {
1969         key_dest = key_menu;
1970         m_state = m_options_graphics;
1971         m_entersound = true;
1972 }
1973
1974 extern cvar_t r_shadow_gloss;
1975 extern cvar_t r_shadow_realtime_dlight;
1976 extern cvar_t r_shadow_realtime_dlight_shadows;
1977 extern cvar_t r_shadow_realtime_world;
1978 extern cvar_t r_shadow_realtime_world_lightmaps;
1979 extern cvar_t r_shadow_realtime_world_shadows;
1980 extern cvar_t r_bloom;
1981 extern cvar_t r_bloom_colorscale;
1982 extern cvar_t r_bloom_colorsubtract;
1983 extern cvar_t r_bloom_colorexponent;
1984 extern cvar_t r_bloom_blur;
1985 extern cvar_t r_bloom_brighten;
1986 extern cvar_t r_bloom_resolution;
1987 extern cvar_t r_hdr_scenebrightness;
1988 extern cvar_t r_hdr_glowintensity;
1989 extern cvar_t gl_picmip;
1990
1991 static void M_Menu_Options_Graphics_AdjustSliders (cmd_state_t *cmd, int dir)
1992 {
1993         int optnum;
1994         S_LocalSound ("sound/misc/menu3.wav");
1995
1996         optnum = 0;
1997
1998              if (options_graphics_cursor == optnum++) Cvar_SetValueQuick (&r_coronas, bound(0, r_coronas.value + dir * 0.125, 4));
1999         else if (options_graphics_cursor == optnum++) Cvar_SetValueQuick (&gl_flashblend, !gl_flashblend.integer);
2000         else if (options_graphics_cursor == optnum++) Cvar_SetValueQuick (&r_shadow_gloss,                                                      bound(0, r_shadow_gloss.integer + dir, 2));
2001         else if (options_graphics_cursor == optnum++) Cvar_SetValueQuick (&r_shadow_realtime_dlight,                            !r_shadow_realtime_dlight.integer);
2002         else if (options_graphics_cursor == optnum++) Cvar_SetValueQuick (&r_shadow_realtime_dlight_shadows,            !r_shadow_realtime_dlight_shadows.integer);
2003         else if (options_graphics_cursor == optnum++) Cvar_SetValueQuick (&r_shadow_realtime_world,                                     !r_shadow_realtime_world.integer);
2004         else if (options_graphics_cursor == optnum++) Cvar_SetValueQuick (&r_shadow_realtime_world_lightmaps,           bound(0, r_shadow_realtime_world_lightmaps.value + dir * 0.1, 1));
2005         else if (options_graphics_cursor == optnum++) Cvar_SetValueQuick (&r_shadow_realtime_world_shadows,                     !r_shadow_realtime_world_shadows.integer);
2006         else if (options_graphics_cursor == optnum++) Cvar_SetValueQuick (&r_bloom,                                 !r_bloom.integer);
2007         else if (options_graphics_cursor == optnum++) Cvar_SetValueQuick (&r_hdr_scenebrightness,                   bound(0.25, r_hdr_scenebrightness.value + dir * 0.125, 4));
2008         else if (options_graphics_cursor == optnum++) Cvar_SetValueQuick (&r_hdr_glowintensity,                     bound(0, r_hdr_glowintensity.value + dir * 0.25, 4));
2009         else if (options_graphics_cursor == optnum++) Cvar_SetValueQuick (&r_bloom_colorscale,                      bound(0.0625, r_bloom_colorscale.value + dir * 0.0625, 1));
2010         else if (options_graphics_cursor == optnum++) Cvar_SetValueQuick (&r_bloom_colorsubtract,                   bound(0, r_bloom_colorsubtract.value + dir * 0.0625, 1-0.0625));
2011         else if (options_graphics_cursor == optnum++) Cvar_SetValueQuick (&r_bloom_colorexponent,                   bound(1, r_bloom_colorexponent.value * (dir > 0 ? 2.0 : 0.5), 8));
2012         else if (options_graphics_cursor == optnum++) Cvar_SetValueQuick (&r_bloom_brighten,                        bound(1, r_bloom_brighten.value + dir * 0.0625, 4));
2013         else if (options_graphics_cursor == optnum++) Cvar_SetValueQuick (&r_bloom_blur,                            bound(1, r_bloom_blur.value + dir * 1, 16));
2014         else if (options_graphics_cursor == optnum++) Cvar_SetValueQuick (&r_bloom_resolution,                      bound(64, r_bloom_resolution.value + dir * 64, 2048));
2015         else if (options_graphics_cursor == optnum++) Cbuf_AddText(cmd, "r_restart\n");
2016 }
2017
2018
2019 static void M_Options_Graphics_Draw (void)
2020 {
2021         int visible;
2022         cachepic_t      *p;
2023
2024         M_Background(320, bound(200, 32 + OPTIONS_GRAPHICS_ITEMS * 8, vid_conheight.integer));
2025
2026         M_DrawPic(16, 4, "gfx/qplaque");
2027         p = Draw_CachePic ("gfx/p_option");
2028         M_DrawPic((320-Draw_GetPicWidth(p))/2, 4, "gfx/p_option");
2029
2030         m_optcursor = options_graphics_cursor;
2031         m_optnum = 0;
2032         visible = (int)((menu_height - 32) / 8);
2033         m_opty = 32 - bound(0, m_optcursor - (visible >> 1), max(0, OPTIONS_GRAPHICS_ITEMS - visible)) * 8;
2034
2035         M_Options_PrintSlider(  "      Corona Intensity", true, r_coronas.value, 0, 4);
2036         M_Options_PrintCheckbox("      Use Only Coronas", true, gl_flashblend.integer);
2037         M_Options_PrintSlider(  "            Gloss Mode", true, r_shadow_gloss.integer, 0, 2);
2038         M_Options_PrintCheckbox("            RT DLights", !gl_flashblend.integer, r_shadow_realtime_dlight.integer);
2039         M_Options_PrintCheckbox("     RT DLight Shadows", !gl_flashblend.integer, r_shadow_realtime_dlight_shadows.integer);
2040         M_Options_PrintCheckbox("              RT World", true, r_shadow_realtime_world.integer);
2041         M_Options_PrintSlider(  "    RT World Lightmaps", true, r_shadow_realtime_world_lightmaps.value, 0, 1);
2042         M_Options_PrintCheckbox("       RT World Shadow", true, r_shadow_realtime_world_shadows.integer);
2043         M_Options_PrintCheckbox("          Bloom Effect", true, r_bloom.integer);
2044         M_Options_PrintSlider(  "      Scene Brightness", true, r_hdr_scenebrightness.value, 0.25, 4);
2045         M_Options_PrintSlider(  "       Glow Brightness", true, r_hdr_glowintensity.value, 0, 4);
2046         M_Options_PrintSlider(  "     Bloom Color Scale", r_bloom.integer, r_bloom_colorscale.value, 0.0625, 1);
2047         M_Options_PrintSlider(  "  Bloom Color Subtract", r_bloom.integer, r_bloom_colorsubtract.value, 0, 1-0.0625);
2048         M_Options_PrintSlider(  "  Bloom Color Exponent", r_bloom.integer, r_bloom_colorexponent.value, 1, 8);
2049         M_Options_PrintSlider(  "       Bloom Intensity", r_bloom.integer, r_bloom_brighten.value, 1, 4);
2050         M_Options_PrintSlider(  "            Bloom Blur", r_bloom.integer, r_bloom_blur.value, 1, 16);
2051         M_Options_PrintSlider(  "      Bloom Resolution", r_bloom.integer, r_bloom_resolution.value, 64, 2048);
2052         M_Options_PrintCommand( "      Restart Renderer", true);
2053 }
2054
2055
2056 static void M_Options_Graphics_Key (cmd_state_t *cmd, int k, int ascii)
2057 {
2058         switch (k)
2059         {
2060         case K_ESCAPE:
2061                 M_Menu_Options_f(cmd);
2062                 break;
2063
2064         case K_ENTER:
2065                 M_Menu_Options_Graphics_AdjustSliders (cmd, 1);
2066                 break;
2067
2068         case K_UPARROW:
2069                 S_LocalSound ("sound/misc/menu1.wav");
2070                 options_graphics_cursor--;
2071                 if (options_graphics_cursor < 0)
2072                         options_graphics_cursor = OPTIONS_GRAPHICS_ITEMS-1;
2073                 break;
2074
2075         case K_DOWNARROW:
2076                 S_LocalSound ("sound/misc/menu1.wav");
2077                 options_graphics_cursor++;
2078                 if (options_graphics_cursor >= OPTIONS_GRAPHICS_ITEMS)
2079                         options_graphics_cursor = 0;
2080                 break;
2081
2082         case K_LEFTARROW:
2083                 M_Menu_Options_Graphics_AdjustSliders(cmd, -1);
2084                 break;
2085
2086         case K_RIGHTARROW:
2087                 M_Menu_Options_Graphics_AdjustSliders(cmd, 1);
2088                 break;
2089         }
2090 }
2091
2092
2093 #define OPTIONS_COLORCONTROL_ITEMS      18
2094
2095 static int              options_colorcontrol_cursor;
2096
2097 // intensity value to match up to 50% dither to 'correct' quake
2098 static cvar_t menu_options_colorcontrol_correctionvalue = {CF_CLIENT, "menu_options_colorcontrol_correctionvalue", "0.5", "intensity value that matches up to white/black dither pattern, should be 0.5 for linear color"};
2099
2100 void M_Menu_Options_ColorControl_f(cmd_state_t *cmd)
2101 {
2102         key_dest = key_menu;
2103         m_state = m_options_colorcontrol;
2104         m_entersound = true;
2105 }
2106
2107
2108 static void M_Menu_Options_ColorControl_AdjustSliders (int dir)
2109 {
2110         int optnum;
2111         float f;
2112         S_LocalSound ("sound/misc/menu3.wav");
2113
2114         optnum = 1;
2115         if (options_colorcontrol_cursor == optnum++)
2116         {
2117                 Cvar_SetValueQuick (&v_color_enable, 0);
2118                 Cvar_SetValueQuick (&v_gamma, bound(1, v_gamma.value + dir * 0.125, 5));
2119         }
2120         else if (options_colorcontrol_cursor == optnum++)
2121         {
2122                 Cvar_SetValueQuick (&v_color_enable, 0);
2123                 Cvar_SetValueQuick (&v_contrast, bound(1, v_contrast.value + dir * 0.125, 5));
2124         }
2125         else if (options_colorcontrol_cursor == optnum++)
2126         {
2127                 Cvar_SetValueQuick (&v_color_enable, 0);
2128                 Cvar_SetValueQuick (&v_brightness, bound(0, v_brightness.value + dir * 0.05, 0.8));
2129         }
2130         else if (options_colorcontrol_cursor == optnum++)
2131         {
2132                 Cvar_SetValueQuick (&v_color_enable, !v_color_enable.integer);
2133         }
2134         else if (options_colorcontrol_cursor == optnum++)
2135         {
2136                 Cvar_SetValueQuick (&v_color_enable, 1);
2137                 Cvar_SetValueQuick (&v_color_black_r, bound(0, v_color_black_r.value + dir * 0.0125, 0.8));
2138         }
2139         else if (options_colorcontrol_cursor == optnum++)
2140         {
2141                 Cvar_SetValueQuick (&v_color_enable, 1);
2142                 Cvar_SetValueQuick (&v_color_black_g, bound(0, v_color_black_g.value + dir * 0.0125, 0.8));
2143         }
2144         else if (options_colorcontrol_cursor == optnum++)
2145         {
2146                 Cvar_SetValueQuick (&v_color_enable, 1);
2147                 Cvar_SetValueQuick (&v_color_black_b, bound(0, v_color_black_b.value + dir * 0.0125, 0.8));
2148         }
2149         else if (options_colorcontrol_cursor == optnum++)
2150         {
2151                 Cvar_SetValueQuick (&v_color_enable, 1);
2152                 f = bound(0, (v_color_black_r.value + v_color_black_g.value + v_color_black_b.value) / 3 + dir * 0.0125, 0.8);
2153                 Cvar_SetValueQuick (&v_color_black_r, f);
2154                 Cvar_SetValueQuick (&v_color_black_g, f);
2155                 Cvar_SetValueQuick (&v_color_black_b, f);
2156         }
2157         else if (options_colorcontrol_cursor == optnum++)
2158         {
2159                 Cvar_SetValueQuick (&v_color_enable, 1);
2160                 Cvar_SetValueQuick (&v_color_grey_r, bound(0, v_color_grey_r.value + dir * 0.0125, 0.95));
2161         }
2162         else if (options_colorcontrol_cursor == optnum++)
2163         {
2164                 Cvar_SetValueQuick (&v_color_enable, 1);
2165                 Cvar_SetValueQuick (&v_color_grey_g, bound(0, v_color_grey_g.value + dir * 0.0125, 0.95));
2166         }
2167         else if (options_colorcontrol_cursor == optnum++)
2168         {
2169                 Cvar_SetValueQuick (&v_color_enable, 1);
2170                 Cvar_SetValueQuick (&v_color_grey_b, bound(0, v_color_grey_b.value + dir * 0.0125, 0.95));
2171         }
2172         else if (options_colorcontrol_cursor == optnum++)
2173         {
2174                 Cvar_SetValueQuick (&v_color_enable, 1);
2175                 f = bound(0, (v_color_grey_r.value + v_color_grey_g.value + v_color_grey_b.value) / 3 + dir * 0.0125, 0.95);
2176                 Cvar_SetValueQuick (&v_color_grey_r, f);
2177                 Cvar_SetValueQuick (&v_color_grey_g, f);
2178                 Cvar_SetValueQuick (&v_color_grey_b, f);
2179         }
2180         else if (options_colorcontrol_cursor == optnum++)
2181         {
2182                 Cvar_SetValueQuick (&v_color_enable, 1);
2183                 Cvar_SetValueQuick (&v_color_white_r, bound(1, v_color_white_r.value + dir * 0.125, 5));
2184         }
2185         else if (options_colorcontrol_cursor == optnum++)
2186         {
2187                 Cvar_SetValueQuick (&v_color_enable, 1);
2188                 Cvar_SetValueQuick (&v_color_white_g, bound(1, v_color_white_g.value + dir * 0.125, 5));
2189         }
2190         else if (options_colorcontrol_cursor == optnum++)
2191         {
2192                 Cvar_SetValueQuick (&v_color_enable, 1);
2193                 Cvar_SetValueQuick (&v_color_white_b, bound(1, v_color_white_b.value + dir * 0.125, 5));
2194         }
2195         else if (options_colorcontrol_cursor == optnum++)
2196         {
2197                 Cvar_SetValueQuick (&v_color_enable, 1);
2198                 f = bound(1, (v_color_white_r.value + v_color_white_g.value + v_color_white_b.value) / 3 + dir * 0.125, 5);
2199                 Cvar_SetValueQuick (&v_color_white_r, f);
2200                 Cvar_SetValueQuick (&v_color_white_g, f);
2201                 Cvar_SetValueQuick (&v_color_white_b, f);
2202         }
2203 }
2204
2205 static void M_Options_ColorControl_Draw (void)
2206 {
2207         int visible;
2208         float x, s, t, u, v;
2209         float c[3];
2210         cachepic_t      *p, *dither;
2211
2212         dither = Draw_CachePic_Flags ("gfx/colorcontrol/ditherpattern", CACHEPICFLAG_NOCLAMP);
2213
2214         M_Background(320, 256);
2215
2216         M_DrawPic(16, 4, "gfx/qplaque");
2217         p = Draw_CachePic ("gfx/p_option");
2218         M_DrawPic((320-Draw_GetPicWidth(p))/2, 4, "gfx/p_option");
2219
2220         m_optcursor = options_colorcontrol_cursor;
2221         m_optnum = 0;
2222         visible = (int)((menu_height - 32) / 8);
2223         m_opty = 32 - bound(0, m_optcursor - (visible >> 1), max(0, OPTIONS_COLORCONTROL_ITEMS - visible)) * 8;
2224
2225         M_Options_PrintCommand( "     Reset to defaults", true);
2226         M_Options_PrintSlider(  "                 Gamma", !v_color_enable.integer, v_gamma.value, 1, 5);
2227         M_Options_PrintSlider(  "              Contrast", !v_color_enable.integer, v_contrast.value, 1, 5);
2228         M_Options_PrintSlider(  "            Brightness", !v_color_enable.integer, v_brightness.value, 0, 0.8);
2229         M_Options_PrintCheckbox("  Color Level Controls", true, v_color_enable.integer);
2230         M_Options_PrintSlider(  "          Black: Red  ", v_color_enable.integer, v_color_black_r.value, 0, 0.8);
2231         M_Options_PrintSlider(  "          Black: Green", v_color_enable.integer, v_color_black_g.value, 0, 0.8);
2232         M_Options_PrintSlider(  "          Black: Blue ", v_color_enable.integer, v_color_black_b.value, 0, 0.8);
2233         M_Options_PrintSlider(  "          Black: Grey ", v_color_enable.integer, (v_color_black_r.value + v_color_black_g.value + v_color_black_b.value) / 3, 0, 0.8);
2234         M_Options_PrintSlider(  "           Grey: Red  ", v_color_enable.integer, v_color_grey_r.value, 0, 0.95);
2235         M_Options_PrintSlider(  "           Grey: Green", v_color_enable.integer, v_color_grey_g.value, 0, 0.95);
2236         M_Options_PrintSlider(  "           Grey: Blue ", v_color_enable.integer, v_color_grey_b.value, 0, 0.95);
2237         M_Options_PrintSlider(  "           Grey: Grey ", v_color_enable.integer, (v_color_grey_r.value + v_color_grey_g.value + v_color_grey_b.value) / 3, 0, 0.95);
2238         M_Options_PrintSlider(  "          White: Red  ", v_color_enable.integer, v_color_white_r.value, 1, 5);
2239         M_Options_PrintSlider(  "          White: Green", v_color_enable.integer, v_color_white_g.value, 1, 5);
2240         M_Options_PrintSlider(  "          White: Blue ", v_color_enable.integer, v_color_white_b.value, 1, 5);
2241         M_Options_PrintSlider(  "          White: Grey ", v_color_enable.integer, (v_color_white_r.value + v_color_white_g.value + v_color_white_b.value) / 3, 1, 5);
2242
2243         m_opty += 4;
2244         DrawQ_Fill(menu_x, menu_y + m_opty, 320, 4 + 64 + 8 + 64 + 4, 0, 0, 0, 1, 0);m_opty += 4;
2245         s = (float) 312 / 2 * vid.mode.width / vid_conwidth.integer;
2246         t = (float) 4 / 2 * vid.mode.height / vid_conheight.integer;
2247         DrawQ_SuperPic(menu_x + 4, menu_y + m_opty, dither, 312, 4, 0,0, 1,0,0,1, s,0, 1,0,0,1, 0,t, 1,0,0,1, s,t, 1,0,0,1, 0);m_opty += 4;
2248         DrawQ_SuperPic(menu_x + 4, menu_y + m_opty, NULL  , 312, 4, 0,0, 0,0,0,1, 1,0, 1,0,0,1, 0,1, 0,0,0,1, 1,1, 1,0,0,1, 0);m_opty += 4;
2249         DrawQ_SuperPic(menu_x + 4, menu_y + m_opty, dither, 312, 4, 0,0, 0,1,0,1, s,0, 0,1,0,1, 0,t, 0,1,0,1, s,t, 0,1,0,1, 0);m_opty += 4;
2250         DrawQ_SuperPic(menu_x + 4, menu_y + m_opty, NULL  , 312, 4, 0,0, 0,0,0,1, 1,0, 0,1,0,1, 0,1, 0,0,0,1, 1,1, 0,1,0,1, 0);m_opty += 4;
2251         DrawQ_SuperPic(menu_x + 4, menu_y + m_opty, dither, 312, 4, 0,0, 0,0,1,1, s,0, 0,0,1,1, 0,t, 0,0,1,1, s,t, 0,0,1,1, 0);m_opty += 4;
2252         DrawQ_SuperPic(menu_x + 4, menu_y + m_opty, NULL  , 312, 4, 0,0, 0,0,0,1, 1,0, 0,0,1,1, 0,1, 0,0,0,1, 1,1, 0,0,1,1, 0);m_opty += 4;
2253         DrawQ_SuperPic(menu_x + 4, menu_y + m_opty, dither, 312, 4, 0,0, 1,1,1,1, s,0, 1,1,1,1, 0,t, 1,1,1,1, s,t, 1,1,1,1, 0);m_opty += 4;
2254         DrawQ_SuperPic(menu_x + 4, menu_y + m_opty, NULL  , 312, 4, 0,0, 0,0,0,1, 1,0, 1,1,1,1, 0,1, 0,0,0,1, 1,1, 1,1,1,1, 0);m_opty += 4;
2255
2256         c[0] = menu_options_colorcontrol_correctionvalue.value; // intensity value that should be matched up to a 50% dither to 'correct' quake
2257         c[1] = c[0];
2258         c[2] = c[1];
2259         VID_ApplyGammaToColor(c, c);
2260         s = (float) 48 / 2 * vid.mode.width / vid_conwidth.integer;
2261         t = (float) 48 / 2 * vid.mode.height / vid_conheight.integer;
2262         u = s * 0.5;
2263         v = t * 0.5;
2264         m_opty += 8;
2265         x = 4;
2266         DrawQ_Fill(menu_x + x, menu_y + m_opty, 64, 48, c[0], 0, 0, 1, 0);
2267         DrawQ_SuperPic(menu_x + x + 16, menu_y + m_opty + 16, dither, 16, 16, 0,0, 1,0,0,1, s,0, 1,0,0,1, 0,t, 1,0,0,1, s,t, 1,0,0,1, 0);
2268         DrawQ_SuperPic(menu_x + x + 32, menu_y + m_opty + 16, dither, 16, 16, 0,0, 1,0,0,1, u,0, 1,0,0,1, 0,v, 1,0,0,1, u,v, 1,0,0,1, 0);
2269         x += 80;
2270         DrawQ_Fill(menu_x + x, menu_y + m_opty, 64, 48, 0, c[1], 0, 1, 0);
2271         DrawQ_SuperPic(menu_x + x + 16, menu_y + m_opty + 16, dither, 16, 16, 0,0, 0,1,0,1, s,0, 0,1,0,1, 0,t, 0,1,0,1, s,t, 0,1,0,1, 0);
2272         DrawQ_SuperPic(menu_x + x + 32, menu_y + m_opty + 16, dither, 16, 16, 0,0, 0,1,0,1, u,0, 0,1,0,1, 0,v, 0,1,0,1, u,v, 0,1,0,1, 0);
2273         x += 80;
2274         DrawQ_Fill(menu_x + x, menu_y + m_opty, 64, 48, 0, 0, c[2], 1, 0);
2275         DrawQ_SuperPic(menu_x + x + 16, menu_y + m_opty + 16, dither, 16, 16, 0,0, 0,0,1,1, s,0, 0,0,1,1, 0,t, 0,0,1,1, s,t, 0,0,1,1, 0);
2276         DrawQ_SuperPic(menu_x + x + 32, menu_y + m_opty + 16, dither, 16, 16, 0,0, 0,0,1,1, u,0, 0,0,1,1, 0,v, 0,0,1,1, u,v, 0,0,1,1, 0);
2277         x += 80;
2278         DrawQ_Fill(menu_x + x, menu_y + m_opty, 64, 48, c[0], c[1], c[2], 1, 0);
2279         DrawQ_SuperPic(menu_x + x + 16, menu_y + m_opty + 16, dither, 16, 16, 0,0, 1,1,1,1, s,0, 1,1,1,1, 0,t, 1,1,1,1, s,t, 1,1,1,1, 0);
2280         DrawQ_SuperPic(menu_x + x + 32, menu_y + m_opty + 16, dither, 16, 16, 0,0, 1,1,1,1, u,0, 1,1,1,1, 0,v, 1,1,1,1, u,v, 1,1,1,1, 0);
2281 }
2282
2283
2284 static void M_Options_ColorControl_Key(cmd_state_t *cmd, int k, int ascii)
2285 {
2286         switch (k)
2287         {
2288         case K_ESCAPE:
2289                 M_Menu_Options_f(cmd);
2290                 break;
2291
2292         case K_ENTER:
2293                 m_entersound = true;
2294                 switch (options_colorcontrol_cursor)
2295                 {
2296                 case 0:
2297                         Cvar_SetValueQuick(&v_gamma, 1);
2298                         Cvar_SetValueQuick(&v_contrast, 1);
2299                         Cvar_SetValueQuick(&v_brightness, 0);
2300                         Cvar_SetValueQuick(&v_color_enable, 0);
2301                         Cvar_SetValueQuick(&v_color_black_r, 0);
2302                         Cvar_SetValueQuick(&v_color_black_g, 0);
2303                         Cvar_SetValueQuick(&v_color_black_b, 0);
2304                         Cvar_SetValueQuick(&v_color_grey_r, 0);
2305                         Cvar_SetValueQuick(&v_color_grey_g, 0);
2306                         Cvar_SetValueQuick(&v_color_grey_b, 0);
2307                         Cvar_SetValueQuick(&v_color_white_r, 1);
2308                         Cvar_SetValueQuick(&v_color_white_g, 1);
2309                         Cvar_SetValueQuick(&v_color_white_b, 1);
2310                         break;
2311                 default:
2312                         M_Menu_Options_ColorControl_AdjustSliders (1);
2313                         break;
2314                 }
2315                 return;
2316
2317         case K_UPARROW:
2318                 S_LocalSound ("sound/misc/menu1.wav");
2319                 options_colorcontrol_cursor--;
2320                 if (options_colorcontrol_cursor < 0)
2321                         options_colorcontrol_cursor = OPTIONS_COLORCONTROL_ITEMS-1;
2322                 break;
2323
2324         case K_DOWNARROW:
2325                 S_LocalSound ("sound/misc/menu1.wav");
2326                 options_colorcontrol_cursor++;
2327                 if (options_colorcontrol_cursor >= OPTIONS_COLORCONTROL_ITEMS)
2328                         options_colorcontrol_cursor = 0;
2329                 break;
2330
2331         case K_LEFTARROW:
2332                 M_Menu_Options_ColorControl_AdjustSliders (-1);
2333                 break;
2334
2335         case K_RIGHTARROW:
2336                 M_Menu_Options_ColorControl_AdjustSliders (1);
2337                 break;
2338         }
2339 }
2340
2341
2342 //=============================================================================
2343 /* KEYS MENU */
2344
2345 static const char *quakebindnames[][2] =
2346 {
2347 {"+attack",             "attack"},
2348 {"impulse 10",          "next weapon"},
2349 {"impulse 12",          "previous weapon"},
2350 {"+jump",                       "jump / swim up"},
2351 {"+forward",            "walk forward"},
2352 {"+back",                       "backpedal"},
2353 {"+left",                       "turn left"},
2354 {"+right",                      "turn right"},
2355 {"+speed",                      "run"},
2356 {"+moveleft",           "step left"},
2357 {"+moveright",          "step right"},
2358 {"+strafe",             "sidestep"},
2359 {"+lookup",             "look up"},
2360 {"+lookdown",           "look down"},
2361 {"centerview",          "center view"},
2362 {"+mlook",                      "mouse look"},
2363 {"+klook",                      "keyboard look"},
2364 {"+moveup",                     "swim up"},
2365 {"+movedown",           "swim down"}
2366 };
2367
2368 static const char *transfusionbindnames[][2] =
2369 {
2370 {"",                            "Movement"},            // Movement commands
2371 {"+forward",            "walk forward"},
2372 {"+back",                       "backpedal"},
2373 {"+left",                       "turn left"},
2374 {"+right",                      "turn right"},
2375 {"+moveleft",           "step left"},
2376 {"+moveright",          "step right"},
2377 {"+jump",                       "jump / swim up"},
2378 {"+movedown",           "swim down"},
2379 {"",                            "Combat"},                      // Combat commands
2380 {"impulse 1",           "Pitch Fork"},
2381 {"impulse 2",           "Flare Gun"},
2382 {"impulse 3",           "Shotgun"},
2383 {"impulse 4",           "Machine Gun"},
2384 {"impulse 5",           "Incinerator"},
2385 {"impulse 6",           "Bombs (TNT)"},
2386 {"impulse 35",          "Proximity Bomb"},
2387 {"impulse 36",          "Remote Detonator"},
2388 {"impulse 7",           "Aerosol Can"},
2389 {"impulse 8",           "Tesla Cannon"},
2390 {"impulse 9",           "Life Leech"},
2391 {"impulse 10",          "Voodoo Doll"},
2392 {"impulse 21",          "next weapon"},
2393 {"impulse 22",          "previous weapon"},
2394 {"+attack",             "attack"},
2395 {"+button3",            "altfire"},
2396 {"",                            "Inventory"},           // Inventory commands
2397 {"impulse 40",          "Dr.'s Bag"},
2398 {"impulse 41",          "Crystal Ball"},
2399 {"impulse 42",          "Beast Vision"},
2400 {"impulse 43",          "Jump Boots"},
2401 {"impulse 23",          "next item"},
2402 {"impulse 24",          "previous item"},
2403 {"impulse 25",          "use item"},
2404 {"",                            "Misc"},                        // Misc commands
2405 {"+button4",            "use"},
2406 {"impulse 50",          "add bot (red)"},
2407 {"impulse 51",          "add bot (blue)"},
2408 {"impulse 52",          "kick a bot"},
2409 {"impulse 26",          "next armor type"},
2410 {"impulse 27",          "identify player"},
2411 {"impulse 55",          "voting menu"},
2412 {"impulse 56",          "observer mode"},
2413 {"",                            "Taunts"},            // Taunts
2414 {"impulse 70",          "taunt 0"},
2415 {"impulse 71",          "taunt 1"},
2416 {"impulse 72",          "taunt 2"},
2417 {"impulse 73",          "taunt 3"},
2418 {"impulse 74",          "taunt 4"},
2419 {"impulse 75",          "taunt 5"},
2420 {"impulse 76",          "taunt 6"},
2421 {"impulse 77",          "taunt 7"},
2422 {"impulse 78",          "taunt 8"},
2423 {"impulse 79",          "taunt 9"}
2424 };
2425
2426 static const char *goodvsbad2bindnames[][2] =
2427 {
2428 {"impulse 69",          "Power 1"},
2429 {"impulse 70",          "Power 2"},
2430 {"impulse 71",          "Power 3"},
2431 {"+jump",                       "jump / swim up"},
2432 {"+forward",            "walk forward"},
2433 {"+back",                       "backpedal"},
2434 {"+left",                       "turn left"},
2435 {"+right",                      "turn right"},
2436 {"+speed",                      "run"},
2437 {"+moveleft",           "step left"},
2438 {"+moveright",          "step right"},
2439 {"+strafe",             "sidestep"},
2440 {"+lookup",             "look up"},
2441 {"+lookdown",           "look down"},
2442 {"centerview",          "center view"},
2443 {"+mlook",                      "mouse look"},
2444 {"kill",                        "kill yourself"},
2445 {"+moveup",                     "swim up"},
2446 {"+movedown",           "swim down"}
2447 };
2448
2449 static int numcommands;
2450 static const char *(*bindnames)[2];
2451
2452 /*
2453 typedef struct binditem_s
2454 {
2455         char *command, *description;
2456         struct binditem_s *next;
2457 }
2458 binditem_t;
2459
2460 typedef struct bindcategory_s
2461 {
2462         char *name;
2463         binditem_t *binds;
2464         struct bindcategory_s *next;
2465 }
2466 bindcategory_t;
2467
2468 static bindcategory_t *bindcategories = NULL;
2469
2470 static void M_ClearBinds (void)
2471 {
2472         for (c = bindcategories;c;c = cnext)
2473         {
2474                 cnext = c->next;
2475                 for (b = c->binds;b;b = bnext)
2476                 {
2477                         bnext = b->next;
2478                         Z_Free(b);
2479                 }
2480                 Z_Free(c);
2481         }
2482         bindcategories = NULL;
2483 }
2484
2485 static void M_AddBindToCategory(bindcategory_t *c, char *command, char *description)
2486 {
2487         for (b = &c->binds;*b;*b = &(*b)->next);
2488         *b = Z_Alloc(sizeof(binditem_t) + strlen(command) + 1 + strlen(description) + 1);
2489         *b->command = (char *)((*b) + 1);
2490         *b->description = *b->command + strlen(command) + 1;
2491         strlcpy(*b->command, command, strlen(command) + 1);
2492         strlcpy(*b->description, description, strlen(description) + 1);
2493 }
2494
2495 static void M_AddBind (char *category, char *command, char *description)
2496 {
2497         for (c = &bindcategories;*c;c = &(*c)->next)
2498         {
2499                 if (!strcmp(category, (*c)->name))
2500                 {
2501                         M_AddBindToCategory(*c, command, description);
2502                         return;
2503                 }
2504         }
2505         *c = Z_Alloc(sizeof(bindcategory_t));
2506         M_AddBindToCategory(*c, command, description);
2507 }
2508
2509 static void M_DefaultBinds (void)
2510 {
2511         M_ClearBinds();
2512         M_AddBind("movement", "+jump", "jump / swim up");
2513         M_AddBind("movement", "+forward", "walk forward");
2514         M_AddBind("movement", "+back", "backpedal");
2515         M_AddBind("movement", "+left", "turn left");
2516         M_AddBind("movement", "+right", "turn right");
2517         M_AddBind("movement", "+speed", "run");
2518         M_AddBind("movement", "+moveleft", "step left");
2519         M_AddBind("movement", "+moveright", "step right");
2520         M_AddBind("movement", "+strafe", "sidestep");
2521         M_AddBind("movement", "+lookup", "look up");
2522         M_AddBind("movement", "+lookdown", "look down");
2523         M_AddBind("movement", "centerview", "center view");
2524         M_AddBind("movement", "+mlook", "mouse look");
2525         M_AddBind("movement", "+klook", "keyboard look");
2526         M_AddBind("movement", "+moveup", "swim up");
2527         M_AddBind("movement", "+movedown", "swim down");
2528         M_AddBind("weapons", "+attack", "attack");
2529         M_AddBind("weapons", "impulse 10", "next weapon");
2530         M_AddBind("weapons", "impulse 12", "previous weapon");
2531         M_AddBind("weapons", "impulse 1", "select weapon 1 (axe)");
2532         M_AddBind("weapons", "impulse 2", "select weapon 2 (shotgun)");
2533         M_AddBind("weapons", "impulse 3", "select weapon 3 (super )");
2534         M_AddBind("weapons", "impulse 4", "select weapon 4 (nailgun)");
2535         M_AddBind("weapons", "impulse 5", "select weapon 5 (super nailgun)");
2536         M_AddBind("weapons", "impulse 6", "select weapon 6 (grenade launcher)");
2537         M_AddBind("weapons", "impulse 7", "select weapon 7 (rocket launcher)");
2538         M_AddBind("weapons", "impulse 8", "select weapon 8 (lightning gun)");
2539 }
2540 */
2541
2542
2543 static int              keys_cursor;
2544 static int              bind_grab;
2545
2546 void M_Menu_Keys_f(cmd_state_t *cmd)
2547 {
2548         key_dest = key_menu_grabbed;
2549         m_state = m_keys;
2550         m_entersound = true;
2551
2552         if (gamemode == GAME_TRANSFUSION)
2553         {
2554                 numcommands = sizeof(transfusionbindnames) / sizeof(transfusionbindnames[0]);
2555                 bindnames = transfusionbindnames;
2556         }
2557         else if (gamemode == GAME_GOODVSBAD2)
2558         {
2559                 numcommands = sizeof(goodvsbad2bindnames) / sizeof(goodvsbad2bindnames[0]);
2560                 bindnames = goodvsbad2bindnames;
2561         }
2562         else
2563         {
2564                 numcommands = sizeof(quakebindnames) / sizeof(quakebindnames[0]);
2565                 bindnames = quakebindnames;
2566         }
2567
2568         // Make sure "keys_cursor" doesn't start on a section in the binding list
2569         keys_cursor = 0;
2570         while (bindnames[keys_cursor][0][0] == '\0')
2571         {
2572                 keys_cursor++;
2573
2574                 // Only sections? There may be a problem somewhere...
2575                 if (keys_cursor >= numcommands)
2576                         Sys_Error ("M_Init: The key binding list only contains sections");
2577         }
2578 }
2579
2580 #define NUMKEYS 5
2581
2582 static void M_UnbindCommand (const char *command)
2583 {
2584         int             j;
2585         const char      *b;
2586
2587         for (j = 0; j < (int)sizeof (keybindings[0]) / (int)sizeof (keybindings[0][0]); j++)
2588         {
2589                 b = keybindings[0][j];
2590                 if (!b)
2591                         continue;
2592                 if (!strcmp (b, command))
2593                         Key_SetBinding (j, 0, "");
2594         }
2595 }
2596
2597
2598 static void M_Keys_Draw (void)
2599 {
2600         int             i, j;
2601         int             keys[NUMKEYS];
2602         int             y;
2603         cachepic_t      *p;
2604         char    keystring[MAX_INPUTLINE];
2605
2606         M_Background(320, 48 + 8 * numcommands);
2607
2608         p = Draw_CachePic ("gfx/ttl_cstm");
2609         M_DrawPic ( (320-Draw_GetPicWidth(p))/2, 4, "gfx/ttl_cstm");
2610
2611         if (bind_grab)
2612                 M_Print(12, 32, "Press a key or button for this action");
2613         else
2614                 M_Print(18, 32, "Enter to change, backspace to clear");
2615
2616 // search for known bindings
2617         for (i=0 ; i<numcommands ; i++)
2618         {
2619                 y = 48 + 8*i;
2620
2621                 // If there's no command, it's just a section
2622                 if (bindnames[i][0][0] == '\0')
2623                 {
2624                         M_PrintRed (4, y, "\x0D");  // #13 is the little arrow pointing to the right
2625                         M_PrintRed (16, y, bindnames[i][1]);
2626                         continue;
2627                 }
2628                 else
2629                         M_Print(16, y, bindnames[i][1]);
2630
2631                 Key_FindKeysForCommand (bindnames[i][0], keys, NUMKEYS, 0);
2632
2633                 // LadyHavoc: redesigned to print more than 2 keys, inspired by Tomaz's MiniRacer
2634                 if (keys[0] == -1)
2635                         dp_strlcpy(keystring, "???", sizeof(keystring));
2636                 else
2637                 {
2638                         char tinystr[TINYSTR_LEN];
2639                         keystring[0] = 0;
2640                         for (j = 0;j < NUMKEYS;j++)
2641                         {
2642                                 if (keys[j] != -1)
2643                                 {
2644                                         if (j > 0)
2645                                                 dp_strlcat(keystring, " or ", sizeof(keystring));
2646                                         dp_strlcat(keystring, Key_KeynumToString (keys[j], tinystr, TINYSTR_LEN), sizeof(keystring));
2647                                 }
2648                         }
2649                 }
2650                 M_Print(150, y, keystring);
2651         }
2652
2653         if (bind_grab)
2654                 M_DrawCharacter (140, 48 + keys_cursor*8, '=');
2655         else
2656                 M_DrawCharacter (140, 48 + keys_cursor*8, 12+((int)(host.realtime*4)&1));
2657 }
2658
2659
2660 static void M_Keys_Key(cmd_state_t *cmd, int k, int ascii)
2661 {
2662         char    line[80];
2663         int             keys[NUMKEYS];
2664         char    tinystr[TINYSTR_LEN];
2665
2666         if (bind_grab)
2667         {       // defining a key
2668                 S_LocalSound ("sound/misc/menu1.wav");
2669                 if (k == K_ESCAPE)
2670                 {
2671                         bind_grab = false;
2672                 }
2673                 else //if (k != '`')
2674                 {
2675                         dpsnprintf(line, sizeof(line), "bind \"%s\" \"%s\"\n", Key_KeynumToString(k, tinystr, TINYSTR_LEN), bindnames[keys_cursor][0]);
2676                         Cbuf_InsertText (cmd, line);
2677                 }
2678
2679                 bind_grab = false;
2680                 return;
2681         }
2682
2683         switch (k)
2684         {
2685         case K_ESCAPE:
2686                 M_Menu_Options_f(cmd);
2687                 break;
2688
2689         case K_LEFTARROW:
2690         case K_UPARROW:
2691                 S_LocalSound ("sound/misc/menu1.wav");
2692                 do
2693                 {
2694                         keys_cursor--;
2695                         if (keys_cursor < 0)
2696                                 keys_cursor = numcommands-1;
2697                 }
2698                 while (bindnames[keys_cursor][0][0] == '\0');  // skip sections
2699                 break;
2700
2701         case K_DOWNARROW:
2702         case K_RIGHTARROW:
2703                 S_LocalSound ("sound/misc/menu1.wav");
2704                 do
2705                 {
2706                         keys_cursor++;
2707                         if (keys_cursor >= numcommands)
2708                                 keys_cursor = 0;
2709                 }
2710                 while (bindnames[keys_cursor][0][0] == '\0');  // skip sections
2711                 break;
2712
2713         case K_ENTER:           // go into bind mode
2714                 Key_FindKeysForCommand (bindnames[keys_cursor][0], keys, NUMKEYS, 0);
2715                 S_LocalSound ("sound/misc/menu2.wav");
2716                 if (keys[NUMKEYS - 1] != -1)
2717                         M_UnbindCommand (bindnames[keys_cursor][0]);
2718                 bind_grab = true;
2719                 break;
2720
2721         case K_BACKSPACE:               // delete bindings
2722         case K_DEL:                             // delete bindings
2723                 S_LocalSound ("sound/misc/menu2.wav");
2724                 M_UnbindCommand (bindnames[keys_cursor][0]);
2725                 break;
2726         }
2727 }
2728
2729 void M_Menu_Reset_f(cmd_state_t *cmd)
2730 {
2731         key_dest = key_menu;
2732         m_state = m_reset;
2733         m_entersound = true;
2734 }
2735
2736
2737 static void M_Reset_Key(cmd_state_t *cmd, int key, int ascii)
2738 {
2739         switch (key)
2740         {
2741         case 'Y':
2742         case 'y':
2743                 Cbuf_AddText(cmd, "cvar_resettodefaults_all;exec default.cfg\n");
2744                 // no break here since we also exit the menu
2745
2746         case K_ESCAPE:
2747         case 'n':
2748         case 'N':
2749                 m_state = m_options;
2750                 m_entersound = true;
2751                 break;
2752
2753         default:
2754                 break;
2755         }
2756 }
2757
2758 static void M_Reset_Draw (void)
2759 {
2760         int lines = 2, linelength = 20;
2761         M_Background(linelength * 8 + 16, lines * 8 + 16);
2762         M_DrawTextBox(0, 0, linelength, lines);
2763         M_Print(8 + 4 * (linelength - 19),  8, "Really wanna reset?");
2764         M_Print(8 + 4 * (linelength - 11), 16, "Press y / n");
2765 }
2766
2767 //=============================================================================
2768 /* VIDEO MENU */
2769
2770 video_resolution_t video_resolutions_hardcoded[] =
2771 {
2772 {"Standard 4x3"              ,  320, 240, 320, 240, 1     },
2773 {"Standard 4x3"              ,  400, 300, 400, 300, 1     },
2774 {"Standard 4x3"              ,  512, 384, 512, 384, 1     },
2775 {"Standard 4x3"              ,  640, 480, 640, 480, 1     },
2776 {"Standard 4x3"              ,  800, 600, 640, 480, 1     },
2777 {"Standard 4x3"              , 1024, 768, 640, 480, 1     },
2778 {"Standard 4x3"              , 1152, 864, 640, 480, 1     },
2779 {"Standard 4x3"              , 1280, 960, 640, 480, 1     },
2780 {"Standard 4x3"              , 1400,1050, 640, 480, 1     },
2781 {"Standard 4x3"              , 1600,1200, 640, 480, 1     },
2782 {"Standard 4x3"              , 1792,1344, 640, 480, 1     },
2783 {"Standard 4x3"              , 1856,1392, 640, 480, 1     },
2784 {"Standard 4x3"              , 1920,1440, 640, 480, 1     },
2785 {"Standard 4x3"              , 2048,1536, 640, 480, 1     },
2786 {"Short Pixel (CRT) 5x4"     ,  320, 256, 320, 256, 0.9375},
2787 {"Short Pixel (CRT) 5x4"     ,  640, 512, 640, 512, 0.9375},
2788 {"Short Pixel (CRT) 5x4"     , 1280,1024, 640, 512, 0.9375},
2789 {"Tall Pixel (CRT) 8x5"      ,  320, 200, 320, 200, 1.2   },
2790 {"Tall Pixel (CRT) 8x5"      ,  640, 400, 640, 400, 1.2   },
2791 {"Tall Pixel (CRT) 8x5"      ,  840, 525, 640, 400, 1.2   },
2792 {"Tall Pixel (CRT) 8x5"      ,  960, 600, 640, 400, 1.2   },
2793 {"Tall Pixel (CRT) 8x5"      , 1680,1050, 640, 400, 1.2   },
2794 {"Tall Pixel (CRT) 8x5"      , 1920,1200, 640, 400, 1.2   },
2795 {"Square Pixel (LCD) 5x4"    ,  320, 256, 320, 256, 1     },
2796 {"Square Pixel (LCD) 5x4"    ,  640, 512, 640, 512, 1     },
2797 {"Square Pixel (LCD) 5x4"    , 1280,1024, 640, 512, 1     },
2798 {"WideScreen 5x3"            ,  640, 384, 640, 384, 1     },
2799 {"WideScreen 5x3"            , 1280, 768, 640, 384, 1     },
2800 {"WideScreen 8x5"            ,  320, 200, 320, 200, 1     },
2801 {"WideScreen 8x5"            ,  640, 400, 640, 400, 1     },
2802 {"WideScreen 8x5"            ,  720, 450, 720, 450, 1     },
2803 {"WideScreen 8x5"            ,  840, 525, 640, 400, 1     },
2804 {"WideScreen 8x5"            ,  960, 600, 640, 400, 1     },
2805 {"WideScreen 8x5"            , 1280, 800, 640, 400, 1     },
2806 {"WideScreen 8x5"            , 1440, 900, 720, 450, 1     },
2807 {"WideScreen 8x5"            , 1680,1050, 640, 400, 1     },
2808 {"WideScreen 8x5"            , 1920,1200, 640, 400, 1     },
2809 {"WideScreen 8x5"            , 2560,1600, 640, 400, 1     },
2810 {"WideScreen 8x5"            , 3840,2400, 640, 400, 1     },
2811 {"WideScreen 14x9"           ,  840, 540, 640, 400, 1     },
2812 {"WideScreen 14x9"           , 1680,1080, 640, 400, 1     },
2813 {"WideScreen 16x9"           ,  640, 360, 640, 360, 1     },
2814 {"WideScreen 16x9"           ,  683, 384, 683, 384, 1     },
2815 {"WideScreen 16x9"           ,  960, 540, 640, 360, 1     },
2816 {"WideScreen 16x9"           , 1280, 720, 640, 360, 1     },
2817 {"WideScreen 16x9"           , 1360, 768, 680, 384, 1     },
2818 {"WideScreen 16x9"           , 1366, 768, 683, 384, 1     },
2819 {"WideScreen 16x9"           , 1600, 900, 640, 360, 1     },
2820 {"WideScreen 16x9"           , 1920,1080, 640, 360, 1     },
2821 {"WideScreen 16x9"           , 2560,1440, 640, 360, 1     },
2822 {"WideScreen 16x9"           , 3840,2160, 640, 360, 1     },
2823 {"NTSC 3x2"                  ,  360, 240, 360, 240, 1.125 },
2824 {"NTSC 3x2"                  ,  720, 480, 720, 480, 1.125 },
2825 {"PAL 14x11"                 ,  360, 283, 360, 283, 0.9545},
2826 {"PAL 14x11"                 ,  720, 566, 720, 566, 0.9545},
2827 {"NES 8x7"                   ,  256, 224, 256, 224, 1.1667},
2828 {"SNES 8x7"                  ,  512, 448, 512, 448, 1.1667},
2829 {NULL, 0, 0, 0, 0, 0}
2830 };
2831 // this is the number of the default mode (640x480) in the list above
2832 int video_resolutions_hardcoded_count = sizeof(video_resolutions_hardcoded) / sizeof(*video_resolutions_hardcoded) - 1;
2833
2834 #define VIDEO_ITEMS 11
2835 static int video_cursor = 0;
2836 static int video_cursor_table[VIDEO_ITEMS] = {68, 88, 96, 104, 112, 120, 128, 136, 144, 152, 160};
2837 static int menu_video_resolution;
2838
2839 video_resolution_t *video_resolutions;
2840 int video_resolutions_count;
2841
2842 static video_resolution_t *menu_video_resolutions;
2843 static int menu_video_resolutions_count;
2844 static qbool menu_video_resolutions_forfullscreen;
2845
2846 static void M_Menu_Video_FindResolution(int w, int h, float a)
2847 {
2848         int i;
2849
2850         if(menu_video_resolutions_forfullscreen)
2851         {
2852                 menu_video_resolutions = video_resolutions;
2853                 menu_video_resolutions_count = video_resolutions_count;
2854         }
2855         else
2856         {
2857                 menu_video_resolutions = video_resolutions_hardcoded;
2858                 menu_video_resolutions_count = video_resolutions_hardcoded_count;
2859         }
2860
2861         // Look for the closest match to the current resolution
2862         menu_video_resolution = 0;
2863         for (i = 1;i < menu_video_resolutions_count;i++)
2864         {
2865                 // if the new mode would be a worse match in width, skip it
2866                 if (abs(menu_video_resolutions[i].width - w) > abs(menu_video_resolutions[menu_video_resolution].width - w))
2867                         continue;
2868                 // if it is equal in width, check height
2869                 if (menu_video_resolutions[i].width == w && menu_video_resolutions[menu_video_resolution].width == w)
2870                 {
2871                         // if the new mode would be a worse match in height, skip it
2872                         if (abs(menu_video_resolutions[i].height - h) > abs(menu_video_resolutions[menu_video_resolution].height - h))
2873                                 continue;
2874                         // if it is equal in width and height, check pixel aspect
2875                         if (menu_video_resolutions[i].height == h && menu_video_resolutions[menu_video_resolution].height == h)
2876                         {
2877                                 // if the new mode would be a worse match in pixel aspect, skip it
2878                                 if (fabs(menu_video_resolutions[i].pixelheight - a) > fabs(menu_video_resolutions[menu_video_resolution].pixelheight - a))
2879                                         continue;
2880                                 // if it is equal in everything, skip it (prefer earlier modes)
2881                                 if (menu_video_resolutions[i].pixelheight == a && menu_video_resolutions[menu_video_resolution].pixelheight == a)
2882                                         continue;
2883                                 // better match for width, height, and pixel aspect
2884                                 menu_video_resolution = i;
2885                         }
2886                         else // better match for width and height
2887                                 menu_video_resolution = i;
2888                 }
2889                 else // better match for width
2890                         menu_video_resolution = i;
2891         }
2892 }
2893
2894 void M_Menu_Video_f(cmd_state_t *cmd)
2895 {
2896         key_dest = key_menu;
2897         m_state = m_video;
2898         m_entersound = true;
2899
2900         M_Menu_Video_FindResolution(vid.mode.width, vid.mode.height, vid_pixelheight.value);
2901 }
2902
2903
2904 static void M_Video_Draw (void)
2905 {
2906         int t;
2907         cachepic_t      *p;
2908         char vabuf[1024];
2909
2910         if(!!vid_fullscreen.integer != menu_video_resolutions_forfullscreen)
2911         {
2912                 video_resolution_t *res = &menu_video_resolutions[menu_video_resolution];
2913                 menu_video_resolutions_forfullscreen = !!vid_fullscreen.integer;
2914                 M_Menu_Video_FindResolution(res->width, res->height, res->pixelheight);
2915         }
2916
2917         M_Background(320, 200);
2918
2919         M_DrawPic(16, 4, "gfx/qplaque");
2920         p = Draw_CachePic ("gfx/vidmodes");
2921         M_DrawPic((320-Draw_GetPicWidth(p))/2, 4, "gfx/vidmodes");
2922
2923         t = 0;
2924
2925         // Current and Proposed Resolution
2926         M_Print(16, video_cursor_table[t] - 12, "    Current Resolution");
2927         if (vid.mode.refreshrate && vid.mode.fullscreen && !vid.mode.desktopfullscreen)
2928                 M_Print(220, video_cursor_table[t] - 12, va(vabuf, sizeof(vabuf), "%dx%d %.2fhz", vid.mode.width, vid.mode.height, vid.mode.refreshrate));
2929         else
2930                 M_Print(220, video_cursor_table[t] - 12, va(vabuf, sizeof(vabuf), "%dx%d", vid.mode.width, vid.mode.height));
2931         M_Print(16, video_cursor_table[t], "        New Resolution");
2932         M_Print(220, video_cursor_table[t], va(vabuf, sizeof(vabuf), "%dx%d", menu_video_resolutions[menu_video_resolution].width, menu_video_resolutions[menu_video_resolution].height));
2933         M_Print(96, video_cursor_table[t] + 8, va(vabuf, sizeof(vabuf), "Type: %s", menu_video_resolutions[menu_video_resolution].type));
2934         t++;
2935
2936         // Antialiasing
2937         M_Print(16, video_cursor_table[t], "          Antialiasing");
2938         M_DrawSlider(220, video_cursor_table[t], vid_samples.value, 1, 32);
2939         t++;
2940
2941         // Refresh Rate
2942         M_ItemPrint(16, video_cursor_table[t], "          Refresh Rate", vid_fullscreen.integer && !vid_desktopfullscreen.integer);
2943         M_DrawSlider(220, video_cursor_table[t], vid_refreshrate.value, 50, 480);
2944         t++;
2945
2946         // Fullscreen
2947         M_Print(16, video_cursor_table[t], "            Fullscreen");
2948         M_DrawCheckbox(220, video_cursor_table[t], vid_fullscreen.integer);
2949         t++;
2950
2951         // Desktop Fullscreen
2952         M_ItemPrint(16, video_cursor_table[t], "    Desktop Fullscreen", vid_fullscreen.integer);
2953         M_DrawCheckbox(220, video_cursor_table[t], vid_desktopfullscreen.integer);
2954         t++;
2955
2956         // Display selection (multi-monitor)
2957         M_ItemPrint(16, video_cursor_table[t], "       Display/Monitor", vid_info_displaycount.integer > 1);
2958         M_DrawSlider(220, video_cursor_table[t], vid_display.integer, 0, vid_info_displaycount.integer - 1);
2959         t++;
2960
2961         // Vertical Sync
2962         M_ItemPrint(16, video_cursor_table[t], "         Vertical Sync", true);
2963         M_DrawSlider(220, video_cursor_table[t], vid_vsync.integer, -1, 1);
2964         t++;
2965
2966         M_ItemPrint(16, video_cursor_table[t], "    Anisotropic Filter", vid.support.ext_texture_filter_anisotropic);
2967         M_DrawSlider(220, video_cursor_table[t], gl_texture_anisotropy.integer, 1, vid.max_anisotropy);
2968         t++;
2969
2970         M_ItemPrint(16, video_cursor_table[t], "       Texture Quality", true);
2971         M_DrawSlider(220, video_cursor_table[t], gl_picmip.value, 3, 0);
2972         t++;
2973
2974         M_ItemPrint(16, video_cursor_table[t], "   Texture Compression", true);
2975         M_DrawCheckbox(220, video_cursor_table[t], gl_texturecompression.integer);
2976         t++;
2977
2978         // "Apply" button
2979         M_Print(220, video_cursor_table[t], "Apply");
2980         t++;
2981
2982         // Cursor
2983         M_DrawCharacter(200, video_cursor_table[video_cursor], 12+((int)(host.realtime*4)&1));
2984 }
2985
2986
2987 static void M_Menu_Video_AdjustSliders (int dir)
2988 {
2989         int t;
2990
2991         S_LocalSound ("sound/misc/menu3.wav");
2992
2993         t = 0;
2994         if (video_cursor == t++)
2995         {
2996                 // Resolution
2997                 int r;
2998                 for(r = 0;r < menu_video_resolutions_count;r++)
2999                 {
3000                         menu_video_resolution += dir;
3001                         if (menu_video_resolution >= menu_video_resolutions_count)
3002                                 menu_video_resolution = 0;
3003                         if (menu_video_resolution < 0)
3004                                 menu_video_resolution = menu_video_resolutions_count - 1;
3005                         if (menu_video_resolutions[menu_video_resolution].width >= vid_minwidth.integer && menu_video_resolutions[menu_video_resolution].height >= vid_minheight.integer)
3006                                 break;
3007                 }
3008         }
3009         else if (video_cursor == t++)
3010                 Cvar_SetValueQuick (&vid_samples, bound(1, vid_samples.value * (dir > 0 ? 2 : 0.5), 32));
3011         else if (video_cursor == t++) // allow jumping from the minimum refreshrate to 0 (auto)
3012                 Cvar_SetValueQuick (&vid_refreshrate, vid_refreshrate.value <= 50 && dir == -1 ? 0 : bound(50, vid_refreshrate.value + dir, 480));
3013         else if (video_cursor == t++)
3014                 Cvar_SetValueQuick (&vid_fullscreen, !vid_fullscreen.integer);
3015         else if (video_cursor == t++)
3016                 Cvar_SetValueQuick (&vid_desktopfullscreen, !vid_desktopfullscreen.integer);
3017         else if (video_cursor == t++)
3018                 Cvar_SetValueQuick (&vid_display, bound(0, vid_display.integer + dir, vid_info_displaycount.integer - 1));
3019         else if (video_cursor == t++)
3020                 Cvar_SetValueQuick (&vid_vsync, bound(-1, vid_vsync.integer + dir, 1));
3021         else if (video_cursor == t++)
3022                 Cvar_SetValueQuick (&gl_texture_anisotropy, bound(1, gl_texture_anisotropy.value * (dir < 0 ? 0.5 : 2.0), vid.max_anisotropy));
3023         else if (video_cursor == t++)
3024                 Cvar_SetValueQuick (&gl_picmip, bound(0, gl_picmip.value - dir, 3));
3025         else if (video_cursor == t++)
3026                 Cvar_SetValueQuick (&gl_texturecompression, !gl_texturecompression.integer);
3027 }
3028
3029
3030 static void M_Video_Key(cmd_state_t *cmd, int key, int ascii)
3031 {
3032         switch (key)
3033         {
3034                 case K_ESCAPE:
3035                         // vid_shared.c has a copy of the current video config. We restore it
3036                         Cvar_SetValueQuick(&vid_display, vid.mode.display);
3037                         Cvar_SetValueQuick(&vid_fullscreen, vid.mode.fullscreen);
3038                         Cvar_SetValueQuick(&vid_desktopfullscreen, vid.mode.desktopfullscreen);
3039                         Cvar_SetValueQuick(&vid_bitsperpixel, vid.mode.bitsperpixel);
3040                         Cvar_SetValueQuick(&vid_samples, vid.mode.samples);
3041                         Cvar_SetValueQuick(&vid_refreshrate, vid.mode.refreshrate);
3042
3043                         S_LocalSound ("sound/misc/menu1.wav");
3044                         M_Menu_Options_f(cmd);
3045                         break;
3046
3047                 case K_ENTER:
3048                         m_entersound = true;
3049                         switch (video_cursor)
3050                         {
3051                                 case (VIDEO_ITEMS - 1):
3052                                         Cvar_SetValueQuick (&vid_width, menu_video_resolutions[menu_video_resolution].width);
3053                                         Cvar_SetValueQuick (&vid_height, menu_video_resolutions[menu_video_resolution].height);
3054                                         Cvar_SetValueQuick (&vid_conwidth, menu_video_resolutions[menu_video_resolution].conwidth);
3055                                         Cvar_SetValueQuick (&vid_conheight, menu_video_resolutions[menu_video_resolution].conheight);
3056                                         Cvar_SetValueQuick (&vid_pixelheight, menu_video_resolutions[menu_video_resolution].pixelheight);
3057                                         Cbuf_AddText(cmd, "vid_restart\n");
3058                                         M_Menu_Options_f(cmd);
3059                                         break;
3060                                 default:
3061                                         M_Menu_Video_AdjustSliders (1);
3062                         }
3063                         break;
3064
3065                 case K_UPARROW:
3066                         S_LocalSound ("sound/misc/menu1.wav");
3067                         video_cursor--;
3068                         if (video_cursor < 0)
3069                                 video_cursor = VIDEO_ITEMS-1;
3070                         break;
3071
3072                 case K_DOWNARROW:
3073                         S_LocalSound ("sound/misc/menu1.wav");
3074                         video_cursor++;
3075                         if (video_cursor >= VIDEO_ITEMS)
3076                                 video_cursor = 0;
3077                         break;
3078
3079                 case K_LEFTARROW:
3080                         M_Menu_Video_AdjustSliders (-1);
3081                         break;
3082
3083                 case K_RIGHTARROW:
3084                         M_Menu_Video_AdjustSliders (1);
3085                         break;
3086         }
3087 }
3088
3089 //=============================================================================
3090 /* HELP MENU */
3091
3092 static int              help_page;
3093 #define NUM_HELP_PAGES  6
3094
3095
3096 void M_Menu_Help_f(cmd_state_t *cmd)
3097 {
3098         key_dest = key_menu;
3099         m_state = m_help;
3100         m_entersound = true;
3101         help_page = 0;
3102 }
3103
3104
3105
3106 static void M_Help_Draw (void)
3107 {
3108         char vabuf[1024];
3109         M_Background(320, 200);
3110         M_DrawPic (0, 0, va(vabuf, sizeof(vabuf), "gfx/help%i", help_page));
3111 }
3112
3113
3114 static void M_Help_Key(cmd_state_t *cmd, int key, int ascii)
3115 {
3116         switch (key)
3117         {
3118         case K_ESCAPE:
3119                 M_Menu_Main_f(cmd);
3120                 break;
3121
3122         case K_UPARROW:
3123         case K_RIGHTARROW:
3124                 m_entersound = true;
3125                 if (++help_page >= NUM_HELP_PAGES)
3126                         help_page = 0;
3127                 break;
3128
3129         case K_DOWNARROW:
3130         case K_LEFTARROW:
3131                 m_entersound = true;
3132                 if (--help_page < 0)
3133                         help_page = NUM_HELP_PAGES-1;
3134                 break;
3135         }
3136
3137 }
3138
3139 //=============================================================================
3140 /* CEDITS MENU */
3141
3142 void M_Menu_Credits_f(cmd_state_t *cmd)
3143 {
3144         key_dest = key_menu;
3145         m_state = m_credits;
3146         m_entersound = true;
3147 }
3148
3149
3150
3151 static void M_Credits_Draw (void)
3152 {
3153         M_Background(640, 480);
3154         M_DrawPic (0, 0, "gfx/creditsmiddle");
3155         M_Print (640/2 - 14/2*8, 236, "Coming soon...");
3156         M_DrawPic (0, 0, "gfx/creditstop");
3157         M_DrawPic (0, 433, "gfx/creditsbottom");
3158 }
3159
3160
3161 static void M_Credits_Key(cmd_state_t *cmd, int key, int ascii)
3162 {
3163                 M_Menu_Main_f(cmd);
3164 }
3165
3166 //=============================================================================
3167 /* QUIT MENU */
3168
3169 static const char *m_quit_message[9];
3170 static int              m_quit_prevstate;
3171 static qbool    wasInMenus;
3172
3173
3174 static int M_QuitMessage(const char *line1, const char *line2, const char *line3, const char *line4, const char *line5, const char *line6, const char *line7, const char *line8)
3175 {
3176         m_quit_message[0] = line1;
3177         m_quit_message[1] = line2;
3178         m_quit_message[2] = line3;
3179         m_quit_message[3] = line4;
3180         m_quit_message[4] = line5;
3181         m_quit_message[5] = line6;
3182         m_quit_message[6] = line7;
3183         m_quit_message[7] = line8;
3184         m_quit_message[8] = NULL;
3185         return 1;
3186 }
3187
3188 static int M_ChooseQuitMessage(int request)
3189 {
3190         if (m_missingdata)
3191         {
3192                 // frag related quit messages are pointless for a fallback menu, so use something generic
3193                 if (request-- == 0) return M_QuitMessage("Are you sure you want to quit?","Press Y to quit, N to stay",NULL,NULL,NULL,NULL,NULL,NULL);
3194                 return 0;
3195         }
3196         switch (gamemode)
3197         {
3198         case GAME_NORMAL:
3199         case GAME_HIPNOTIC:
3200         case GAME_ROGUE:
3201         case GAME_QUOTH:
3202         case GAME_NEHAHRA:
3203         case GAME_DEFEATINDETAIL2:
3204                 if (request-- == 0) return M_QuitMessage("Are you gonna quit","this game just like","everything else?",NULL,NULL,NULL,NULL,NULL);
3205                 if (request-- == 0) return M_QuitMessage("Milord, methinks that","thou art a lowly","quitter. Is this true?",NULL,NULL,NULL,NULL,NULL);
3206                 if (request-- == 0) return M_QuitMessage("Do I need to bust your","face open for trying","to quit?",NULL,NULL,NULL,NULL,NULL);
3207                 if (request-- == 0) return M_QuitMessage("Man, I oughta smack you","for trying to quit!","Press Y to get","smacked out.",NULL,NULL,NULL,NULL);
3208                 if (request-- == 0) return M_QuitMessage("Press Y to quit like a","big loser in life.","Press N to stay proud","and successful!",NULL,NULL,NULL,NULL);
3209                 if (request-- == 0) return M_QuitMessage("If you press Y to","quit, I will summon","Satan all over your","hard drive!",NULL,NULL,NULL,NULL);
3210                 if (request-- == 0) return M_QuitMessage("Um, Asmodeus dislikes","his children trying to","quit. Press Y to return","to your Tinkertoys.",NULL,NULL,NULL,NULL);
3211                 if (request-- == 0) return M_QuitMessage("If you quit now, I'll","throw a blanket-party","for you next time!",NULL,NULL,NULL,NULL,NULL);
3212                 break;
3213         case GAME_GOODVSBAD2:
3214                 if (request-- == 0) return M_QuitMessage("Press Yes To Quit","...","Yes",NULL,NULL,NULL,NULL,NULL);
3215                 if (request-- == 0) return M_QuitMessage("Do you really want to","Quit?","Play Good vs bad 3!",NULL,NULL,NULL,NULL,NULL);
3216                 if (request-- == 0) return M_QuitMessage("All your quit are","belong to long duck","dong",NULL,NULL,NULL,NULL,NULL);
3217                 if (request-- == 0) return M_QuitMessage("Press Y to quit","","But are you too legit?",NULL,NULL,NULL,NULL,NULL);
3218                 if (request-- == 0) return M_QuitMessage("This game was made by","e@chip-web.com","It is by far the best","game ever made.",NULL,NULL,NULL,NULL);
3219                 if (request-- == 0) return M_QuitMessage("Even I really dont","know of a game better","Press Y to quit","like rougue chedder",NULL,NULL,NULL,NULL);
3220                 if (request-- == 0) return M_QuitMessage("After you stop playing","tell the guys who made","counterstrike to just","kill themselves now",NULL,NULL,NULL,NULL);
3221                 if (request-- == 0) return M_QuitMessage("Press Y to exit to DOS","","SSH login as user Y","to exit to Linux",NULL,NULL,NULL,NULL);
3222                 if (request-- == 0) return M_QuitMessage("Press Y like you","were waanderers","from Ys'",NULL,NULL,NULL,NULL,NULL);
3223                 if (request-- == 0) return M_QuitMessage("This game was made in","Nippon like the SS","announcer's saying ipon",NULL,NULL,NULL,NULL,NULL);
3224                 if (request-- == 0) return M_QuitMessage("you","want to quit?",NULL,NULL,NULL,NULL,NULL,NULL);
3225                 if (request-- == 0) return M_QuitMessage("Please stop playing","this stupid game",NULL,NULL,NULL,NULL,NULL,NULL);
3226                 break;
3227         case GAME_BATTLEMECH:
3228                 if (request-- == 0) return M_QuitMessage("? WHY ?","Press Y to quit, N to keep fraggin'",NULL,NULL,NULL,NULL,NULL,NULL);
3229                 if (request-- == 0) return M_QuitMessage("Leave now and your mech is scrap!","Press Y to quit, N to keep fraggin'",NULL,NULL,NULL,NULL,NULL,NULL);
3230                 if (request-- == 0) return M_QuitMessage("Accept Defeat?","Press Y to quit, N to keep fraggin'",NULL,NULL,NULL,NULL,NULL,NULL);
3231                 if (request-- == 0) return M_QuitMessage("Wait! There are more mechs to destroy!","Press Y to quit, N to keep fraggin'",NULL,NULL,NULL,NULL,NULL,NULL);
3232                 if (request-- == 0) return M_QuitMessage("Where's your bloodlust?","Press Y to quit, N to keep fraggin'",NULL,NULL,NULL,NULL,NULL,NULL);
3233                 if (request-- == 0) return M_QuitMessage("Your mech here is way more impressive","than your car out there...","Press Y to quit, N to keep fraggin'",NULL,NULL,NULL,NULL,NULL);
3234                 if (request-- == 0) return M_QuitMessage("Quitting won't reduce your debt","Press Y to quit, N to keep fraggin'",NULL,NULL,NULL,NULL,NULL,NULL);
3235                 break;
3236         case GAME_OPENQUARTZ:
3237                 if (request-- == 0) return M_QuitMessage("There is nothing like free beer!","Press Y to quit, N to stay",NULL,NULL,NULL,NULL,NULL,NULL);
3238                 if (request-- == 0) return M_QuitMessage("GNU is not Unix!","Press Y to quit, N to stay",NULL,NULL,NULL,NULL,NULL,NULL);
3239                 if (request-- == 0) return M_QuitMessage("You prefer free beer over free speech?","Press Y to quit, N to stay",NULL,NULL,NULL,NULL,NULL,NULL);
3240                 if (request-- == 0) return M_QuitMessage("Is OpenQuartz Propaganda?","Press Y to quit, N to stay",NULL,NULL,NULL,NULL,NULL,NULL);
3241                 break;
3242         default:
3243                 if (request-- == 0) return M_QuitMessage("Tired of fragging already?",NULL,NULL,NULL,NULL,NULL,NULL,NULL);
3244                 if (request-- == 0) return M_QuitMessage("Quit now and forfeit your bodycount?",NULL,NULL,NULL,NULL,NULL,NULL,NULL);
3245                 if (request-- == 0) return M_QuitMessage("Are you sure you want to quit?",NULL,NULL,NULL,NULL,NULL,NULL,NULL);
3246                 if (request-- == 0) return M_QuitMessage("Off to do something constructive?",NULL,NULL,NULL,NULL,NULL,NULL,NULL);
3247                 break;
3248         }
3249         return 0;
3250 }
3251
3252 void M_Menu_Quit_f(cmd_state_t *cmd)
3253 {
3254         int n;
3255         if (m_state == m_quit)
3256                 return;
3257         wasInMenus = (key_dest == key_menu || key_dest == key_menu_grabbed);
3258         key_dest = key_menu;
3259         m_quit_prevstate = m_state;
3260         m_state = m_quit;
3261         m_entersound = true;
3262         // count how many there are
3263         for (n = 1;M_ChooseQuitMessage(n);n++);
3264         // choose one
3265         M_ChooseQuitMessage(rand() % n);
3266 }
3267
3268
3269 static void M_Quit_Key(cmd_state_t *cmd, int key, int ascii)
3270 {
3271         switch (key)
3272         {
3273         case K_ESCAPE:
3274         case 'n':
3275         case 'N':
3276                 if (wasInMenus)
3277                 {
3278                         m_state = (enum m_state_e)m_quit_prevstate;
3279                         m_entersound = true;
3280                 }
3281                 else
3282                 {
3283                         key_dest = key_game;
3284                         m_state = m_none;
3285                 }
3286                 break;
3287
3288         case 'Y':
3289         case 'y':
3290                 host.state = host_shutdown;
3291                 break;
3292
3293         default:
3294                 break;
3295         }
3296 }
3297
3298 static void M_Quit_Draw (void)
3299 {
3300         int i, l, linelength, firstline, lastline, lines;
3301         for (i = 0, linelength = 0, firstline = 9999, lastline = -1;m_quit_message[i];i++)
3302         {
3303                 if ((l = (int)strlen(m_quit_message[i])))
3304                 {
3305                         if (firstline > i)
3306                                 firstline = i;
3307                         if (lastline < i)
3308                                 lastline = i;
3309                         if (linelength < l)
3310                                 linelength = l;
3311                 }
3312         }
3313         lines = (lastline - firstline) + 1;
3314         M_Background(linelength * 8 + 16, lines * 8 + 16);
3315         if (!m_missingdata) //since this is a fallback menu for missing data, it is very hard to read with the box
3316                 M_DrawTextBox(0, 0, linelength, lines); //this is less obtrusive than hacking up the M_DrawTextBox function
3317         for (i = 0, l = firstline;i < lines;i++, l++)
3318                 M_Print(8 + 4 * (linelength - strlen(m_quit_message[l])), 8 + 8 * i, m_quit_message[l]);
3319 }
3320
3321 //=============================================================================
3322 /* LAN CONFIG MENU */
3323
3324 static int              lanConfig_cursor = -1;
3325 static int              lanConfig_cursor_table [] = {56, 76, 84, 120};
3326 #define NUM_LANCONFIG_CMDS      4
3327
3328 static int      lanConfig_port;
3329 static char     lanConfig_portname[6];
3330 static char     lanConfig_joinname[40];
3331
3332 void M_Menu_LanConfig_f(cmd_state_t *cmd)
3333 {
3334         key_dest = key_menu;
3335         m_state = m_lanconfig;
3336         m_entersound = true;
3337         if (lanConfig_cursor == -1)
3338         {
3339                 if (JoiningGame)
3340                         lanConfig_cursor = 1;
3341         }
3342         if (StartingGame)
3343                 lanConfig_cursor = 1;
3344         lanConfig_port = 26000;
3345         dpsnprintf(lanConfig_portname, sizeof(lanConfig_portname), "%u", (unsigned int) lanConfig_port);
3346
3347         cl_connect_status[0] = '\0';
3348 }
3349
3350
3351 static void M_LanConfig_Draw (void)
3352 {
3353         cachepic_t      *p;
3354         int             basex;
3355         const char      *startJoin;
3356         const char      *protocol;
3357         char vabuf[1024];
3358
3359         M_Background(320, 200);
3360
3361         M_DrawPic (16, 4, "gfx/qplaque");
3362         p = Draw_CachePic ("gfx/p_multi");
3363         basex = (320-Draw_GetPicWidth(p))/2;
3364         M_DrawPic (basex, 4, "gfx/p_multi");
3365
3366         if (StartingGame)
3367                 startJoin = "New Game";
3368         else
3369                 startJoin = "Join Game";
3370         protocol = "TCP/IP";
3371         M_Print(basex, 32, va(vabuf, sizeof(vabuf), "%s - %s", startJoin, protocol));
3372         basex += 8;
3373
3374         M_Print(basex, lanConfig_cursor_table[0], "Port");
3375         M_DrawTextBox (basex+8*8, lanConfig_cursor_table[0]-8, sizeof(lanConfig_portname), 1);
3376         M_Print(basex+9*8, lanConfig_cursor_table[0], lanConfig_portname);
3377
3378         if (JoiningGame)
3379         {
3380                 M_Print(basex, lanConfig_cursor_table[1], "Search for DarkPlaces games...");
3381                 M_Print(basex, lanConfig_cursor_table[2], "Search for QuakeWorld games...");
3382                 M_Print(basex, lanConfig_cursor_table[3]-16, "Join game at:");
3383                 M_DrawTextBox (basex+8, lanConfig_cursor_table[3]-8, sizeof(lanConfig_joinname), 1);
3384                 M_Print(basex+16, lanConfig_cursor_table[3], lanConfig_joinname);
3385         }
3386         else
3387         {
3388                 M_DrawTextBox (basex, lanConfig_cursor_table[1]-8, 2, 1);
3389                 M_Print(basex+8, lanConfig_cursor_table[1], "OK");
3390         }
3391
3392         M_DrawCharacter (basex-8, lanConfig_cursor_table [lanConfig_cursor], 12+((int)(host.realtime*4)&1));
3393
3394         if (lanConfig_cursor == 0)
3395                 M_DrawCharacter (basex+9*8 + 8*strlen(lanConfig_portname), lanConfig_cursor_table [lanConfig_cursor], 10+((int)(host.realtime*4)&1));
3396
3397         if (lanConfig_cursor == 3)
3398                 M_DrawCharacter (basex+16 + 8*strlen(lanConfig_joinname), lanConfig_cursor_table [lanConfig_cursor], 10+((int)(host.realtime*4)&1));
3399
3400         if (*cl_connect_status)
3401                 M_Print(basex, 168, cl_connect_status);
3402 }
3403
3404
3405 static void M_LanConfig_Key(cmd_state_t *cmd, int key, int ascii)
3406 {
3407         int             l;
3408         char vabuf[1024];
3409
3410         switch (key)
3411         {
3412         case K_ESCAPE:
3413                 M_Menu_MultiPlayer_f(cmd);
3414                 break;
3415
3416         case K_UPARROW:
3417                 S_LocalSound ("sound/misc/menu1.wav");
3418                 lanConfig_cursor--;
3419                 if (lanConfig_cursor < 0)
3420                         lanConfig_cursor = NUM_LANCONFIG_CMDS-1;
3421                 // when in start game menu, skip the unused search qw servers item
3422                 if (StartingGame && lanConfig_cursor == 2)
3423                         lanConfig_cursor = 1;
3424                 break;
3425
3426         case K_DOWNARROW:
3427                 S_LocalSound ("sound/misc/menu1.wav");
3428                 lanConfig_cursor++;
3429                 if (lanConfig_cursor >= NUM_LANCONFIG_CMDS)
3430                         lanConfig_cursor = 0;
3431                 // when in start game menu, skip the unused search qw servers item
3432                 if (StartingGame && lanConfig_cursor == 1)
3433                         lanConfig_cursor = 2;
3434                 break;
3435
3436         case K_ENTER:
3437                 if (lanConfig_cursor == 0)
3438                         break;
3439
3440                 m_entersound = true;
3441
3442                 Cbuf_AddText(cmd, "stopdemo\n");
3443
3444                 Cvar_SetValue(&cvars_all, "port", lanConfig_port);
3445
3446                 if (lanConfig_cursor == 1 || lanConfig_cursor == 2)
3447                 {
3448                         if (StartingGame)
3449                         {
3450                                 M_Menu_GameOptions_f(cmd);
3451                                 break;
3452                         }
3453                         M_Menu_ServerList_f(cmd);
3454                         break;
3455                 }
3456
3457                 if (lanConfig_cursor == 3)
3458                         Cbuf_AddText(cmd, va(vabuf, sizeof(vabuf), "connect \"%s\"\n", lanConfig_joinname) );
3459                 break;
3460
3461         case K_BACKSPACE:
3462                 if (lanConfig_cursor == 0)
3463                 {
3464                         if (strlen(lanConfig_portname))
3465                                 lanConfig_portname[strlen(lanConfig_portname)-1] = 0;
3466                 }
3467
3468                 if (lanConfig_cursor == 3)
3469                 {
3470                         if (strlen(lanConfig_joinname))
3471                                 lanConfig_joinname[strlen(lanConfig_joinname)-1] = 0;
3472                 }
3473                 break;
3474
3475         default:
3476                 if (ascii < 32)
3477                         break;
3478
3479                 if (lanConfig_cursor == 3)
3480                 {
3481                         l = (int)strlen(lanConfig_joinname);
3482                         if (l < (int)sizeof(lanConfig_joinname) - 1)
3483                         {
3484                                 lanConfig_joinname[l+1] = 0;
3485                                 lanConfig_joinname[l] = ascii;
3486                         }
3487                 }
3488
3489                 if (ascii < '0' || ascii > '9')
3490                         break;
3491                 if (lanConfig_cursor == 0)
3492                 {
3493                         l = (int)strlen(lanConfig_portname);
3494                         if (l < (int)sizeof(lanConfig_portname) - 1)
3495                         {
3496                                 lanConfig_portname[l+1] = 0;
3497                                 lanConfig_portname[l] = ascii;
3498                         }
3499                 }
3500         }
3501
3502         if (StartingGame && lanConfig_cursor == 3)
3503         {
3504                 if (key == K_UPARROW)
3505                         lanConfig_cursor = 1;
3506                 else
3507                         lanConfig_cursor = 0;
3508         }
3509
3510         l =  atoi(lanConfig_portname);
3511         if (l <= 65535)
3512                 lanConfig_port = l;
3513         dpsnprintf(lanConfig_portname, sizeof(lanConfig_portname), "%u", (unsigned int) lanConfig_port);
3514 }
3515
3516 //=============================================================================
3517 /* GAME OPTIONS MENU */
3518
3519 typedef struct level_s
3520 {
3521         const char      *name;
3522         const char      *description;
3523 } level_t;
3524
3525 typedef struct episode_s
3526 {
3527         const char      *description;
3528         int             firstLevel;
3529         int             levels;
3530 } episode_t;
3531
3532 typedef struct gamelevels_s
3533 {
3534         const char *gamename;
3535         level_t *levels;
3536         episode_t *episodes;
3537         int numepisodes;
3538 }
3539 gamelevels_t;
3540
3541 static level_t quakelevels[] =
3542 {
3543         {"start", "Entrance"},  // 0
3544
3545         {"e1m1", "Slipgate Complex"},                           // 1
3546         {"e1m2", "Castle of the Damned"},
3547         {"e1m3", "The Necropolis"},
3548         {"e1m4", "The Grisly Grotto"},
3549         {"e1m5", "Gloom Keep"},
3550         {"e1m6", "The Door To Chthon"},
3551         {"e1m7", "The House of Chthon"},
3552         {"e1m8", "Ziggurat Vertigo"},
3553
3554         {"e2m1", "The Installation"},                           // 9
3555         {"e2m2", "Ogre Citadel"},
3556         {"e2m3", "Crypt of Decay"},
3557         {"e2m4", "The Ebon Fortress"},
3558         {"e2m5", "The Wizard's Manse"},
3559         {"e2m6", "The Dismal Oubliette"},
3560         {"e2m7", "Underearth"},
3561
3562         {"e3m1", "Termination Central"},                        // 16
3563         {"e3m2", "The Vaults of Zin"},
3564         {"e3m3", "The Tomb of Terror"},
3565         {"e3m4", "Satan's Dark Delight"},
3566         {"e3m5", "Wind Tunnels"},
3567         {"e3m6", "Chambers of Torment"},
3568         {"e3m7", "The Haunted Halls"},
3569
3570         {"e4m1", "The Sewage System"},                          // 23
3571         {"e4m2", "The Tower of Despair"},
3572         {"e4m3", "The Elder God Shrine"},
3573         {"e4m4", "The Palace of Hate"},
3574         {"e4m5", "Hell's Atrium"},
3575         {"e4m6", "The Pain Maze"},
3576         {"e4m7", "Azure Agony"},
3577         {"e4m8", "The Nameless City"},
3578
3579         {"end", "Shub-Niggurath's Pit"},                        // 31
3580
3581         {"dm1", "Place of Two Deaths"},                         // 32
3582         {"dm2", "Claustrophobopolis"},
3583         {"dm3", "The Abandoned Base"},
3584         {"dm4", "The Bad Place"},
3585         {"dm5", "The Cistern"},
3586         {"dm6", "The Dark Zone"}
3587 };
3588
3589 static episode_t quakeepisodes[] =
3590 {
3591         {"Welcome to Quake", 0, 1},
3592         {"Doomed Dimension", 1, 8},
3593         {"Realm of Black Magic", 9, 7},
3594         {"Netherworld", 16, 7},
3595         {"The Elder World", 23, 8},
3596         {"Final Level", 31, 1},
3597         {"Deathmatch Arena", 32, 6}
3598 };
3599
3600  //MED 01/06/97 added hipnotic levels
3601 static level_t     hipnoticlevels[] =
3602 {
3603    {"start", "Command HQ"},  // 0
3604
3605    {"hip1m1", "The Pumping Station"},          // 1
3606    {"hip1m2", "Storage Facility"},
3607    {"hip1m3", "The Lost Mine"},
3608    {"hip1m4", "Research Facility"},
3609    {"hip1m5", "Military Complex"},
3610
3611    {"hip2m1", "Ancient Realms"},          // 6
3612    {"hip2m2", "The Black Cathedral"},
3613    {"hip2m3", "The Catacombs"},
3614    {"hip2m4", "The Crypt"},
3615    {"hip2m5", "Mortum's Keep"},
3616    {"hip2m6", "The Gremlin's Domain"},
3617
3618    {"hip3m1", "Tur Torment"},       // 12
3619    {"hip3m2", "Pandemonium"},
3620    {"hip3m3", "Limbo"},
3621    {"hip3m4", "The Gauntlet"},
3622
3623    {"hipend", "Armagon's Lair"},       // 16
3624
3625    {"hipdm1", "The Edge of Oblivion"}           // 17
3626 };
3627
3628 //MED 01/06/97  added hipnotic episodes
3629 static episode_t   hipnoticepisodes[] =
3630 {
3631    {"Scourge of Armagon", 0, 1},
3632    {"Fortress of the Dead", 1, 5},
3633    {"Dominion of Darkness", 6, 6},
3634    {"The Rift", 12, 4},
3635    {"Final Level", 16, 1},
3636    {"Deathmatch Arena", 17, 1}
3637 };
3638
3639 //PGM 01/07/97 added rogue levels
3640 //PGM 03/02/97 added dmatch level
3641 static level_t          roguelevels[] =
3642 {
3643         {"start",       "Split Decision"},
3644         {"r1m1",        "Deviant's Domain"},
3645         {"r1m2",        "Dread Portal"},
3646         {"r1m3",        "Judgement Call"},
3647         {"r1m4",        "Cave of Death"},
3648         {"r1m5",        "Towers of Wrath"},
3649         {"r1m6",        "Temple of Pain"},
3650         {"r1m7",        "Tomb of the Overlord"},
3651         {"r2m1",        "Tempus Fugit"},
3652         {"r2m2",        "Elemental Fury I"},
3653         {"r2m3",        "Elemental Fury II"},
3654         {"r2m4",        "Curse of Osiris"},
3655         {"r2m5",        "Wizard's Keep"},
3656         {"r2m6",        "Blood Sacrifice"},
3657         {"r2m7",        "Last Bastion"},
3658         {"r2m8",        "Source of Evil"},
3659         {"ctf1",    "Division of Change"}
3660 };
3661
3662 //PGM 01/07/97 added rogue episodes
3663 //PGM 03/02/97 added dmatch episode
3664 static episode_t        rogueepisodes[] =
3665 {
3666         {"Introduction", 0, 1},
3667         {"Hell's Fortress", 1, 7},
3668         {"Corridors of Time", 8, 8},
3669         {"Deathmatch Arena", 16, 1}
3670 };
3671
3672 static level_t          nehahralevels[] =
3673 {
3674         {"nehstart",    "Welcome to Nehahra"},
3675         {"neh1m1",      "Forge City1: Slipgates"},
3676         {"neh1m2",      "Forge City2: Boiler"},
3677         {"neh1m3",      "Forge City3: Escape"},
3678         {"neh1m4",      "Grind Core"},
3679         {"neh1m5",      "Industrial Silence"},
3680         {"neh1m6",      "Locked-Up Anger"},
3681         {"neh1m7",      "Wanderer of the Wastes"},
3682         {"neh1m8",      "Artemis System Net"},
3683         {"neh1m9",      "To the Death"},
3684         {"neh2m1",      "The Gates of Ghoro"},
3685         {"neh2m2",      "Sacred Trinity"},
3686         {"neh2m3",      "Realm of the Ancients"},
3687         {"neh2m4",      "Temple of the Ancients"},
3688         {"neh2m5",      "Dreams Made Flesh"},
3689         {"neh2m6",      "Your Last Cup of Sorrow"},
3690         {"nehsec",      "Ogre's Bane"},
3691         {"nehahra",     "Nehahra's Den"},
3692         {"nehend",      "Quintessence"}
3693 };
3694
3695 static episode_t        nehahraepisodes[] =
3696 {
3697         {"Welcome to Nehahra", 0, 1},
3698         {"The Fall of Forge", 1, 9},
3699         {"The Outlands", 10, 7},
3700         {"Dimension of the Lost", 17, 2}
3701 };
3702
3703 // Map list for Transfusion
3704 static level_t          transfusionlevels[] =
3705 {
3706         {"e1m1",                "Cradle to Grave"},
3707         {"e1m2",                "Wrong Side of the Tracks"},
3708         {"e1m3",                "Phantom Express"},
3709         {"e1m4",                "Dark Carnival"},
3710         {"e1m5",                "Hallowed Grounds"},
3711         {"e1m6",                "The Great Temple"},
3712         {"e1m7",                "Altar of Stone"},
3713         {"e1m8",                "House of Horrors"},
3714
3715         {"e2m1",                "Shipwrecked"},
3716         {"e2m2",                "The Lumber Mill"},
3717         {"e2m3",                "Rest for the Wicked"},
3718         {"e2m4",                "The Overlooked Hotel"},
3719         {"e2m5",                "The Haunting"},
3720         {"e2m6",                "The Cold Rush"},
3721         {"e2m7",                "Bowels of the Earth"},
3722         {"e2m8",                "The Lair of Shial"},
3723         {"e2m9",                "Thin Ice"},
3724
3725         {"e3m1",                "Ghost Town"},
3726         {"e3m2",                "The Siege"},
3727         {"e3m3",                "Raw Sewage"},
3728         {"e3m4",                "The Sick Ward"},
3729         {"e3m5",                "Spare Parts"},
3730         {"e3m6",                "Monster Bait"},
3731         {"e3m7",                "The Pit of Cerberus"},
3732         {"e3m8",                "Catacombs"},
3733
3734         {"e4m1",                "Butchery Loves Company"},
3735         {"e4m2",                "Breeding Grounds"},
3736         {"e4m3",                "Charnel House"},
3737         {"e4m4",                "Crystal Lake"},
3738         {"e4m5",                "Fire and Brimstone"},
3739         {"e4m6",                "The Ganglion Depths"},
3740         {"e4m7",                "In the Flesh"},
3741         {"e4m8",                "The Hall of the Epiphany"},
3742         {"e4m9",                "Mall of the Dead"},
3743
3744         {"bb1",                 "The Stronghold"},
3745         {"bb2",                 "Winter Wonderland"},
3746         {"bb3",                 "Bodies"},
3747         {"bb4",                 "The Tower"},
3748         {"bb5",                 "Click!"},
3749         {"bb6",                 "Twin Fortress"},
3750         {"bb7",                 "Midgard"},
3751         {"bb8",                 "Fun With Heads"},
3752         {"dm1",                 "Monolith Building 11"},
3753         {"dm2",                 "Power!"},
3754         {"dm3",                 "Area 15"},
3755
3756         {"e6m1",                "Welcome to Your Life"},
3757         {"e6m2",                "They Are Here"},
3758         {"e6m3",                "Public Storage"},
3759         {"e6m4",                "Aqueducts"},
3760         {"e6m5",                "The Ruined Temple"},
3761         {"e6m6",                "Forbidden Rituals"},
3762         {"e6m7",                "The Dungeon"},
3763         {"e6m8",                "Beauty and the Beast"},
3764         {"e6m9",                "Forgotten Catacombs"},
3765
3766         {"cp01",                "Boat Docks"},
3767         {"cp02",                "Old Opera House"},
3768         {"cp03",                "Gothic Library"},
3769         {"cp04",                "Lost Monastery"},
3770         {"cp05",                "Steamboat"},
3771         {"cp06",                "Graveyard"},
3772         {"cp07",                "Mountain Pass"},
3773         {"cp08",                "Abysmal Mine"},
3774         {"cp09",                "Castle"},
3775         {"cps1",                "Boggy Creek"},
3776
3777         {"cpbb01",              "Crypt of Despair"},
3778         {"cpbb02",              "Pits of Blood"},
3779         {"cpbb03",              "Unholy Cathedral"},
3780         {"cpbb04",              "Deadly Inspirations"},
3781
3782         {"b2a15",               "Area 15 (B2)"},
3783         {"b2bodies",    "BB_Bodies (B2)"},
3784         {"b2cabana",    "BB_Cabana"},
3785         {"b2power",             "BB_Power"},
3786         {"barena",              "Blood Arena"},
3787         {"bkeep",               "Blood Keep"},
3788         {"bstar",               "Brown Star"},
3789         {"crypt",               "The Crypt"},
3790
3791         {"bb3_2k1",             "Bodies Infusion"},
3792         {"captasao",    "Captasao"},
3793         {"curandero",   "Curandero"},
3794         {"dcamp",               "DeathCamp"},
3795         {"highnoon",    "HighNoon"},
3796         {"qbb1",                "The Confluence"},
3797         {"qbb2",                "KathartiK"},
3798         {"qbb3",                "Caleb's Woodland Retreat"},
3799         {"zoo",                 "Zoo"},
3800
3801         {"dranzbb6",    "Black Coffee"},
3802         {"fragm",               "Frag'M"},
3803         {"maim",                "Maim"},
3804         {"qe1m7",               "The House of Chthon"},
3805         {"qdm1",                "Place of Two Deaths"},
3806         {"qdm4",                "The Bad Place"},
3807         {"qdm5",                "The Cistern"},
3808         {"qmorbias",    "DM-Morbias"},
3809         {"simple",              "Dead Simple"}
3810 };
3811
3812 static episode_t        transfusionepisodes[] =
3813 {
3814         {"The Way of All Flesh", 0, 8},
3815         {"Even Death May Die", 8, 9},
3816         {"Farewell to Arms", 17, 8},
3817         {"Dead Reckoning", 25, 9},
3818         {"BloodBath", 34, 11},
3819         {"Post Mortem", 45, 9},
3820         {"Cryptic Passage", 54, 10},
3821         {"Cryptic BloodBath", 64, 4},
3822         {"Blood 2", 68, 8},
3823         {"Transfusion", 76, 9},
3824         {"Conversions", 85, 9}
3825 };
3826
3827 static level_t goodvsbad2levels[] =
3828 {
3829         {"rts", "Many Paths"},  // 0
3830         {"chess", "Chess, Scott Hess"},                         // 1
3831         {"dot", "Big Wall"},
3832         {"city2", "The Big City"},
3833         {"bwall", "0 G like Psychic TV"},
3834         {"snow", "Wireframed"},
3835         {"telep", "Infinite Falling"},
3836         {"faces", "Facing Bases"},
3837         {"island", "Adventure Islands"},
3838 };
3839
3840 static episode_t goodvsbad2episodes[] =
3841 {
3842         {"Levels? Bevels!", 0, 8},
3843 };
3844
3845 static level_t battlemechlevels[] =
3846 {
3847         {"start", "Parking Level"},
3848         {"dm1", "Hot Dump"},                        // 1
3849         {"dm2", "The Pits"},
3850         {"dm3", "Dimber Died"},
3851         {"dm4", "Fire in the Hole"},
3852         {"dm5", "Clubhouses"},
3853         {"dm6", "Army go Underground"},
3854 };
3855
3856 static episode_t battlemechepisodes[] =
3857 {
3858         {"Time for Battle", 0, 7},
3859 };
3860
3861 static level_t openquartzlevels[] =
3862 {
3863         {"start", "Welcome to Openquartz"},
3864
3865         {"void1", "The center of nowhere"},                        // 1
3866         {"void2", "The place with no name"},
3867         {"void3", "The lost supply base"},
3868         {"void4", "Past the outer limits"},
3869         {"void5", "Into the nonexistance"},
3870         {"void6", "Void walk"},
3871
3872         {"vtest", "Warp Central"},
3873         {"box", "The deathmatch box"},
3874         {"bunkers", "Void command"},
3875         {"house", "House of chaos"},
3876         {"office", "Overnight office kill"},
3877         {"am1", "The nameless chambers"},
3878 };
3879
3880 static episode_t openquartzepisodes[] =
3881 {
3882         {"Single Player", 0, 1},
3883         {"Void Deathmatch", 1, 6},
3884         {"Contrib", 7, 6},
3885 };
3886
3887 static level_t defeatindetail2levels[] =
3888 {
3889         {"atac3",       "River Crossing"},
3890         {"atac4",       "Canyon Chaos"},
3891         {"atac7",       "Desert Stormer"},
3892 };
3893
3894 static episode_t defeatindetail2episodes[] =
3895 {
3896         {"ATAC Campaign", 0, 3},
3897 };
3898
3899 static level_t prydonlevels[] =
3900 {
3901         {"curig2", "Capel Curig"},      // 0
3902
3903         {"tdastart", "Gateway"},                                // 1
3904 };
3905
3906 static episode_t prydonepisodes[] =
3907 {
3908         {"Prydon Gate", 0, 1},
3909         {"The Dark Age", 1, 1}
3910 };
3911
3912 static gamelevels_t sharewarequakegame = {"Shareware Quake", quakelevels, quakeepisodes, 2};
3913 static gamelevels_t registeredquakegame = {"Quake", quakelevels, quakeepisodes, 7};
3914 static gamelevels_t hipnoticgame = {"Scourge of Armagon", hipnoticlevels, hipnoticepisodes, 6};
3915 static gamelevels_t roguegame = {"Dissolution of Eternity", roguelevels, rogueepisodes, 4};
3916 static gamelevels_t nehahragame = {"Nehahra", nehahralevels, nehahraepisodes, 4};
3917 static gamelevels_t transfusiongame = {"Transfusion", transfusionlevels, transfusionepisodes, 11};
3918 static gamelevels_t goodvsbad2game = {"Good Vs. Bad 2", goodvsbad2levels, goodvsbad2episodes, 1};
3919 static gamelevels_t battlemechgame = {"Battlemech", battlemechlevels, battlemechepisodes, 1};
3920 static gamelevels_t openquartzgame = {"OpenQuartz", openquartzlevels, openquartzepisodes, 3};
3921 static gamelevels_t defeatindetail2game = {"Defeat In Detail 2", defeatindetail2levels, defeatindetail2episodes, 1};
3922 static gamelevels_t prydongame = {"Prydon Gate", prydonlevels, prydonepisodes, 2};
3923
3924 typedef struct gameinfo_s
3925 {
3926         gamemode_t gameid;
3927         gamelevels_t *notregistered;
3928         gamelevels_t *registered;
3929 }
3930 gameinfo_t;
3931
3932 static gameinfo_t gamelist[] =
3933 {
3934         {GAME_NORMAL, &sharewarequakegame, &registeredquakegame},
3935         {GAME_HIPNOTIC, &hipnoticgame, &hipnoticgame},
3936         {GAME_ROGUE, &roguegame, &roguegame},
3937         {GAME_QUOTH, &sharewarequakegame, &registeredquakegame},
3938         {GAME_NEHAHRA, &nehahragame, &nehahragame},
3939         {GAME_TRANSFUSION, &transfusiongame, &transfusiongame},
3940         {GAME_GOODVSBAD2, &goodvsbad2game, &goodvsbad2game},
3941         {GAME_BATTLEMECH, &battlemechgame, &battlemechgame},
3942         {GAME_OPENQUARTZ, &openquartzgame, &openquartzgame},
3943         {GAME_DEFEATINDETAIL2, &defeatindetail2game, &defeatindetail2game},
3944         {GAME_PRYDON, &prydongame, &prydongame},
3945 };
3946
3947 static gamelevels_t *gameoptions_levels  = NULL;
3948
3949 static int      startepisode;
3950 static int      startlevel;
3951 static int maxplayers;
3952 static qbool m_serverInfoMessage = false;
3953 static double m_serverInfoMessageTime;
3954
3955 void M_Menu_GameOptions_f(cmd_state_t *cmd)
3956 {
3957         int i;
3958         key_dest = key_menu;
3959         m_state = m_gameoptions;
3960         m_entersound = true;
3961         if (maxplayers == 0)
3962                 maxplayers = svs.maxclients;
3963         if (maxplayers < 2)
3964                 maxplayers = min(8, MAX_SCOREBOARD);
3965         // pick game level list based on gamemode (use GAME_NORMAL if no matches)
3966         gameoptions_levels = registered.integer ? gamelist[0].registered : gamelist[0].notregistered;
3967         for (i = 0;i < (int)(sizeof(gamelist)/sizeof(gamelist[0]));i++)
3968                 if (gamelist[i].gameid == gamemode)
3969                         gameoptions_levels = registered.integer ? gamelist[i].registered : gamelist[i].notregistered;
3970 }
3971
3972
3973 static int gameoptions_cursor_table[] = {40, 56, 64, 72, 80, 88, 96, 104, 112, 140, 160, 168};
3974 #define NUM_GAMEOPTIONS 12
3975 static int              gameoptions_cursor;
3976
3977 void M_GameOptions_Draw (void)
3978 {
3979         cachepic_t      *p;
3980         int             x;
3981         char vabuf[1024];
3982
3983         M_Background(320, 200);
3984
3985         M_DrawPic (16, 4, "gfx/qplaque");
3986         p = Draw_CachePic ("gfx/p_multi");
3987         M_DrawPic ( (320-Draw_GetPicWidth(p))/2, 4, "gfx/p_multi");
3988
3989         M_DrawTextBox (152, 32, 10, 1);
3990         M_Print(160, 40, "begin game");
3991
3992         M_Print(0, 56, "      Max players");
3993         M_Print(160, 56, va(vabuf, sizeof(vabuf), "%i", maxplayers) );
3994
3995         if (gamemode != GAME_GOODVSBAD2)
3996         {
3997                 M_Print(0, 64, "        Game Type");
3998                 if (gamemode == GAME_TRANSFUSION)
3999                 {
4000                         if (!coop.integer && !deathmatch.integer)
4001                                 Cvar_SetValue(&cvars_all, "deathmatch", 1);
4002                         if (deathmatch.integer == 0)
4003                                 M_Print(160, 64, "Cooperative");
4004                         else if (deathmatch.integer == 2)
4005                                 M_Print(160, 64, "Capture the Flag");
4006                         else
4007                                 M_Print(160, 64, "Blood Bath");
4008                 }
4009                 else if (gamemode == GAME_BATTLEMECH)
4010                 {
4011                         if (!deathmatch.integer)
4012                                 Cvar_SetValue(&cvars_all, "deathmatch", 1);
4013                         if (deathmatch.integer == 2)
4014                                 M_Print(160, 64, "Rambo Match");
4015                         else
4016                                 M_Print(160, 64, "Deathmatch");
4017                 }
4018                 else
4019                 {
4020                         if (!coop.integer && !deathmatch.integer)
4021                                 Cvar_SetValue(&cvars_all, "deathmatch", 1);
4022                         if (coop.integer)
4023                                 M_Print(160, 64, "Cooperative");
4024                         else
4025                                 M_Print(160, 64, "Deathmatch");
4026                 }
4027
4028                 M_Print(0, 72, "        Teamplay");
4029                 if (gamemode == GAME_ROGUE)
4030                 {
4031                         const char *msg;
4032
4033                         switch((int)teamplay.integer)
4034                         {
4035                                 case 1: msg = "No Friendly Fire"; break;
4036                                 case 2: msg = "Friendly Fire"; break;
4037                                 case 3: msg = "Tag"; break;
4038                                 case 4: msg = "Capture the Flag"; break;
4039                                 case 5: msg = "One Flag CTF"; break;
4040                                 case 6: msg = "Three Team CTF"; break;
4041                                 default: msg = "Off"; break;
4042                         }
4043                         M_Print(160, 72, msg);
4044                 }
4045                 else
4046                 {
4047                         const char *msg;
4048
4049                         switch (teamplay.integer)
4050                         {
4051                                 case 0: msg = "Off"; break;
4052                                 case 2: msg = "Friendly Fire"; break;
4053                                 default: msg = "No Friendly Fire"; break;
4054                         }
4055                         M_Print(160, 72, msg);
4056                 }
4057                 M_Print(0, 80, "            Skill");
4058                 if (gamemode == GAME_TRANSFUSION)
4059                 {
4060                         if (skill.integer == 1)
4061                                 M_Print(160, 80, "Still Kicking");
4062                         else if (skill.integer == 2)
4063                                 M_Print(160, 80, "Pink On The Inside");
4064                         else if (skill.integer == 3)
4065                                 M_Print(160, 80, "Lightly Broiled");
4066                         else if (skill.integer == 4)
4067                                 M_Print(160, 80, "Well Done");
4068                         else
4069                                 M_Print(160, 80, "Extra Crispy");
4070                 }
4071                 else
4072                 {
4073                         if (skill.integer == 0)
4074                                 M_Print(160, 80, "Easy difficulty");
4075                         else if (skill.integer == 1)
4076                                 M_Print(160, 80, "Normal difficulty");
4077                         else if (skill.integer == 2)
4078                                 M_Print(160, 80, "Hard difficulty");
4079                         else
4080                                 M_Print(160, 80, "Nightmare difficulty");
4081                 }
4082                 M_Print(0, 88, "       Frag Limit");
4083                 if (fraglimit.integer == 0)
4084                         M_Print(160, 88, "none");
4085                 else
4086                         M_Print(160, 88, va(vabuf, sizeof(vabuf), "%i frags", fraglimit.integer));
4087
4088                 M_Print(0, 96, "       Time Limit");
4089                 if (timelimit.integer == 0)
4090                         M_Print(160, 96, "none");
4091                 else
4092                         M_Print(160, 96, va(vabuf, sizeof(vabuf), "%i minutes", timelimit.integer));
4093         }
4094
4095         M_Print(0, 104, "    Public server");
4096         M_Print(160, 104, (sv_public.integer == 0) ? "no" : "yes");
4097
4098         M_Print(0, 112, "   Server maxrate");
4099         M_Print(160, 112, va(vabuf, sizeof(vabuf), "%i", sv_maxrate.integer));
4100
4101         M_Print(0, 128, "      Server name");
4102         M_DrawTextBox (0, 132, 38, 1);
4103         M_Print(8, 140, hostname.string);
4104
4105         if (gamemode != GAME_GOODVSBAD2)
4106         {
4107                 M_Print(0, 160, "         Episode");
4108                 M_Print(160, 160, gameoptions_levels->episodes[startepisode].description);
4109         }
4110
4111         M_Print(0, 168, "           Level");
4112         M_Print(160, 168, gameoptions_levels->levels[gameoptions_levels->episodes[startepisode].firstLevel + startlevel].description);
4113         M_Print(160, 176, gameoptions_levels->levels[gameoptions_levels->episodes[startepisode].firstLevel + startlevel].name);
4114
4115 // line cursor
4116         if (gameoptions_cursor == 9)
4117                 M_DrawCharacter (8 + 8 * strlen(hostname.string), gameoptions_cursor_table[gameoptions_cursor], 10+((int)(host.realtime*4)&1));
4118         else
4119                 M_DrawCharacter (144, gameoptions_cursor_table[gameoptions_cursor], 12+((int)(host.realtime*4)&1));
4120
4121         if (m_serverInfoMessage)
4122         {
4123                 if ((host.realtime - m_serverInfoMessageTime) < 5.0)
4124                 {
4125                         x = (320-26*8)/2;
4126                         M_DrawTextBox (x, 138, 24, 4);
4127                         x += 8;
4128                         M_Print(x, 146, " More than 255 players??");
4129                         M_Print(x, 154, "  First, question your  ");
4130                         M_Print(x, 162, "   sanity, then email   ");
4131                         M_Print(x, 170, "darkplacesengine@gmail.com");
4132                 }
4133                 else
4134                         m_serverInfoMessage = false;
4135         }
4136 }
4137
4138
4139 static void M_NetStart_Change (int dir)
4140 {
4141         int count;
4142
4143         switch (gameoptions_cursor)
4144         {
4145         case 1:
4146                 maxplayers += dir;
4147                 if (maxplayers > MAX_SCOREBOARD)
4148                 {
4149                         maxplayers = MAX_SCOREBOARD;
4150                         m_serverInfoMessage = true;
4151                         m_serverInfoMessageTime = host.realtime;
4152                 }
4153                 if (maxplayers < 2)
4154                         maxplayers = 2;
4155                 break;
4156
4157         case 2:
4158                 if (gamemode == GAME_GOODVSBAD2)
4159                         break;
4160                 if (gamemode == GAME_TRANSFUSION)
4161                 {
4162                         switch (deathmatch.integer)
4163                         {
4164                                 // From Cooperative to BloodBath
4165                                 case 0:
4166                                         Cvar_SetValueQuick (&coop, 0);
4167                                         Cvar_SetValueQuick (&deathmatch, 1);
4168                                         break;
4169
4170                                 // From BloodBath to CTF
4171                                 case 1:
4172                                         Cvar_SetValueQuick (&coop, 0);
4173                                         Cvar_SetValueQuick (&deathmatch, 2);
4174                                         break;
4175
4176                                 // From CTF to Cooperative
4177                                 //case 2:
4178                                 default:
4179                                         Cvar_SetValueQuick (&coop, 1);
4180                                         Cvar_SetValueQuick (&deathmatch, 0);
4181                         }
4182                 }
4183                 else if (gamemode == GAME_BATTLEMECH)
4184                 {
4185                         if (deathmatch.integer == 2) // changing from Rambo to Deathmatch
4186                                 Cvar_SetValueQuick (&deathmatch, 0);
4187                         else // changing from Deathmatch to Rambo
4188                                 Cvar_SetValueQuick (&deathmatch, 2);
4189                 }
4190                 else
4191                 {
4192                         if (deathmatch.integer) // changing from deathmatch to coop
4193                         {
4194                                 Cvar_SetValueQuick (&coop, 1);
4195                                 Cvar_SetValueQuick (&deathmatch, 0);
4196                         }
4197                         else // changing from coop to deathmatch
4198                         {
4199                                 Cvar_SetValueQuick (&coop, 0);
4200                                 Cvar_SetValueQuick (&deathmatch, 1);
4201                         }
4202                 }
4203                 break;
4204
4205         case 3:
4206                 if (gamemode == GAME_GOODVSBAD2)
4207                         break;
4208                 if (gamemode == GAME_ROGUE)
4209                         count = 6;
4210                 else
4211                         count = 2;
4212
4213                 Cvar_SetValueQuick (&teamplay, teamplay.integer + dir);
4214                 if (teamplay.integer > count)
4215                         Cvar_SetValueQuick (&teamplay, 0);
4216                 else if (teamplay.integer < 0)
4217                         Cvar_SetValueQuick (&teamplay, count);
4218                 break;
4219
4220         case 4:
4221                 if (gamemode == GAME_GOODVSBAD2)
4222                         break;
4223                 Cvar_SetValueQuick (&skill, skill.integer + dir);
4224                 if (gamemode == GAME_TRANSFUSION)
4225                 {
4226                         if (skill.integer > 5)
4227                                 Cvar_SetValueQuick (&skill, 1);
4228                         if (skill.integer < 1)
4229                                 Cvar_SetValueQuick (&skill, 5);
4230                 }
4231                 else
4232                 {
4233                         if (skill.integer > 3)
4234                                 Cvar_SetValueQuick (&skill, 0);
4235                         if (skill.integer < 0)
4236                                 Cvar_SetValueQuick (&skill, 3);
4237                 }
4238                 break;
4239
4240         case 5:
4241                 if (gamemode == GAME_GOODVSBAD2)
4242                         break;
4243                 Cvar_SetValueQuick (&fraglimit, fraglimit.integer + dir*10);
4244                 if (fraglimit.integer > 100)
4245                         Cvar_SetValueQuick (&fraglimit, 0);
4246                 if (fraglimit.integer < 0)
4247                         Cvar_SetValueQuick (&fraglimit, 100);
4248                 break;
4249
4250         case 6:
4251                 if (gamemode == GAME_GOODVSBAD2)
4252                         break;
4253                 Cvar_SetValueQuick (&timelimit, timelimit.value + dir*5);
4254                 if (timelimit.value > 60)
4255                         Cvar_SetValueQuick (&timelimit, 0);
4256                 if (timelimit.value < 0)
4257                         Cvar_SetValueQuick (&timelimit, 60);
4258                 break;
4259
4260         case 7:
4261                 Cvar_SetValueQuick (&sv_public, !sv_public.integer);
4262                 break;
4263
4264         case 8:
4265                 Cvar_SetValueQuick (&sv_maxrate, sv_maxrate.integer + dir*500);
4266                 if (sv_maxrate.integer < NET_MINRATE)
4267                         Cvar_SetValueQuick (&sv_maxrate, NET_MINRATE);
4268                 break;
4269
4270         case 9:
4271                 break;
4272
4273         case 10:
4274                 if (gamemode == GAME_GOODVSBAD2)
4275                         break;
4276                 startepisode += dir;
4277
4278                 if (startepisode < 0)
4279                         startepisode = gameoptions_levels->numepisodes - 1;
4280
4281                 if (startepisode >= gameoptions_levels->numepisodes)
4282                         startepisode = 0;
4283
4284                 startlevel = 0;
4285                 break;
4286
4287         case 11:
4288                 startlevel += dir;
4289
4290                 if (startlevel < 0)
4291                         startlevel = gameoptions_levels->episodes[startepisode].levels - 1;
4292
4293                 if (startlevel >= gameoptions_levels->episodes[startepisode].levels)
4294                         startlevel = 0;
4295                 break;
4296         }
4297 }
4298
4299 static void M_GameOptions_Key(cmd_state_t *cmd, int key, int ascii)
4300 {
4301         int l;
4302         char hostnamebuf[128];
4303         char vabuf[1024];
4304
4305         switch (key)
4306         {
4307         case K_ESCAPE:
4308                 M_Menu_MultiPlayer_f(cmd);
4309                 break;
4310
4311         case K_UPARROW:
4312                 S_LocalSound ("sound/misc/menu1.wav");
4313                 gameoptions_cursor--;
4314                 if (gameoptions_cursor < 0)
4315                         gameoptions_cursor = NUM_GAMEOPTIONS-1;
4316                 break;
4317
4318         case K_DOWNARROW:
4319                 S_LocalSound ("sound/misc/menu1.wav");
4320                 gameoptions_cursor++;
4321                 if (gameoptions_cursor >= NUM_GAMEOPTIONS)
4322                         gameoptions_cursor = 0;
4323                 break;
4324
4325         case K_LEFTARROW:
4326                 if (gameoptions_cursor == 0)
4327                         break;
4328                 S_LocalSound ("sound/misc/menu3.wav");
4329                 M_NetStart_Change (-1);
4330                 break;
4331
4332         case K_RIGHTARROW:
4333                 if (gameoptions_cursor == 0)
4334                         break;
4335                 S_LocalSound ("sound/misc/menu3.wav");
4336                 M_NetStart_Change (1);
4337                 break;
4338
4339         case K_ENTER:
4340                 S_LocalSound ("sound/misc/menu2.wav");
4341                 if (gameoptions_cursor == 0)
4342                 {
4343                         if (sv.active)
4344                                 Cbuf_AddText(cmd, "disconnect\n");
4345                         Cbuf_AddText(cmd, va(vabuf, sizeof(vabuf), "maxplayers %u\n", maxplayers) );
4346
4347                         Cbuf_AddText(cmd, va(vabuf, sizeof(vabuf), "map %s\n", gameoptions_levels->levels[gameoptions_levels->episodes[startepisode].firstLevel + startlevel].name) );
4348                         return;
4349                 }
4350
4351                 M_NetStart_Change (1);
4352                 break;
4353
4354         case K_BACKSPACE:
4355                 if (gameoptions_cursor == 9)
4356                 {
4357                         l = (int)strlen(hostname.string);
4358                         if (l)
4359                         {
4360                                 l = min(l - 1, 37);
4361                                 memcpy(hostnamebuf, hostname.string, l);
4362                                 hostnamebuf[l] = 0;
4363                                 Cvar_Set(&cvars_all, "hostname", hostnamebuf);
4364                         }
4365                 }
4366                 break;
4367
4368         default:
4369                 if (ascii < 32)
4370                         break;
4371                 if (gameoptions_cursor == 9)
4372                 {
4373                         l = (int)strlen(hostname.string);
4374                         if (l < 37)
4375                         {
4376                                 memcpy(hostnamebuf, hostname.string, l);
4377                                 hostnamebuf[l] = ascii;
4378                                 hostnamebuf[l+1] = 0;
4379                                 Cvar_Set(&cvars_all, "hostname", hostnamebuf);
4380                         }
4381                 }
4382         }
4383 }
4384
4385 //=============================================================================
4386 /* SLIST MENU */
4387
4388 static unsigned slist_cursor;
4389 static unsigned slist_visible;
4390
4391 void M_Menu_ServerList_f(cmd_state_t *cmd)
4392 {
4393         key_dest = key_menu;
4394         m_state = m_slist;
4395         m_entersound = true;
4396         slist_cursor = 0;
4397         cl_connect_status[0] = '\0';
4398         if (lanConfig_cursor == 2)
4399                 Net_SlistQW_f(cmd);
4400         else
4401                 Net_Slist_f(cmd);
4402 }
4403
4404
4405 static void M_ServerList_Draw (void)
4406 {
4407         unsigned n, y, start, end, statnumplayers, statmaxplayers;
4408         cachepic_t *p;
4409         const char *s;
4410         char vabuf[1024];
4411
4412         // use as much vertical space as available
4413         if (gamemode == GAME_TRANSFUSION)
4414                 M_Background(640, vid_conheight.integer - 80);
4415         else
4416                 M_Background(640, vid_conheight.integer);
4417         // scroll the list as the cursor moves
4418         ServerList_GetPlayerStatistics(&statnumplayers, &statmaxplayers);
4419         s = va(vabuf, sizeof(vabuf), "%u/%u masters %u/%u servers %u/%u players", masterreplycount, masterquerycount, serverreplycount, serverquerycount, statnumplayers, statmaxplayers);
4420         M_PrintRed((640 - strlen(s) * 8) / 2, 32, s);
4421         if (*cl_connect_status)
4422                 M_Print(16, menu_height - 8, cl_connect_status);
4423         y = 48;
4424         slist_visible = (menu_height - 16 - y) / 8 / 2;
4425         start = min(slist_cursor - min(slist_cursor, slist_visible >> 1), serverlist_viewcount - min(serverlist_viewcount, slist_visible));
4426         end = min(start + slist_visible, serverlist_viewcount);
4427
4428         p = Draw_CachePic ("gfx/p_multi");
4429         M_DrawPic((640 - Draw_GetPicWidth(p)) / 2, 4, "gfx/p_multi");
4430         if (end > start)
4431         {
4432                 for (n = start;n < end;n++)
4433                 {
4434                         serverlist_entry_t *entry = ServerList_GetViewEntry(n);
4435                         DrawQ_Fill(menu_x, menu_y + y, 640, 16, n == slist_cursor ? (0.5 + 0.2 * sin(host.realtime * M_PI)) : 0, 0, 0, 0.5, 0);
4436                         M_PrintColored(0, y, entry->line1);y += 8;
4437                         M_PrintColored(0, y, entry->line2);y += 8;
4438                 }
4439         }
4440         else if (host.realtime - masterquerytime > 10)
4441         {
4442                 if (masterquerycount)
4443                         M_Print(0, y, "No servers found");
4444                 else
4445                         M_Print(0, y, "No master servers found (network problem?)");
4446         }
4447         else
4448         {
4449                 if (serverquerycount)
4450                         M_Print(0, y, "Querying servers");
4451                 else
4452                         M_Print(0, y, "Querying master servers");
4453         }
4454 }
4455
4456
4457 static void M_ServerList_Key(cmd_state_t *cmd, int k, int ascii)
4458 {
4459         char vabuf[1024];
4460         switch (k)
4461         {
4462         case K_ESCAPE:
4463                 M_Menu_LanConfig_f(cmd);
4464                 break;
4465
4466         case K_SPACE:
4467                 if (lanConfig_cursor == 2)
4468                         Net_SlistQW_f(cmd);
4469                 else
4470                         Net_Slist_f(cmd);
4471                 break;
4472
4473         case K_PGUP:
4474                 slist_cursor -= slist_visible - 2;
4475         case K_UPARROW:
4476         case K_LEFTARROW:
4477                 S_LocalSound ("sound/misc/menu1.wav");
4478                 slist_cursor--;
4479                 if (slist_cursor >= serverlist_viewcount)
4480                         slist_cursor = serverlist_viewcount - 1;
4481                 break;
4482
4483         case K_PGDN:
4484                 slist_cursor += slist_visible - 2;
4485         case K_DOWNARROW:
4486         case K_RIGHTARROW:
4487                 S_LocalSound ("sound/misc/menu1.wav");
4488                 slist_cursor++;
4489                 if (slist_cursor >= serverlist_viewcount)
4490                         slist_cursor = 0;
4491                 break;
4492
4493         case K_HOME:
4494                 S_LocalSound ("sound/misc/menu1.wav");
4495                 slist_cursor = 0;
4496                 break;
4497
4498         case K_END:
4499                 S_LocalSound ("sound/misc/menu1.wav");
4500                 slist_cursor = serverlist_viewcount - 1;
4501                 break;
4502
4503         case K_ENTER:
4504                 S_LocalSound ("sound/misc/menu2.wav");
4505                 if (serverlist_viewcount)
4506                         Cbuf_AddText(cmd, va(vabuf, sizeof(vabuf), "connect \"%s\"\n", ServerList_GetViewEntry(slist_cursor)->info.cname));
4507                 break;
4508
4509         default:
4510                 break;
4511         }
4512
4513 }
4514
4515 //=============================================================================
4516 /* MODLIST MENU */
4517 // same limit of mod dirs as in fs.c
4518 #define MODLIST_MAXDIRS 16
4519 static int modlist_enabled [MODLIST_MAXDIRS];   //array of indexs to modlist
4520 static int modlist_numenabled;                  //number of enabled (or in process to be..) mods
4521
4522 typedef struct modlist_entry_s
4523 {
4524         qbool loaded;   // used to determine whether this entry is loaded and running
4525         int enabled;            // index to array of modlist_enabled
4526
4527         // name of the modification, this is displayed on the menu entry
4528         char name[128];
4529         // directory where we will find it
4530         char dir[MAX_QPATH];
4531 } modlist_entry_t;
4532
4533 static int modlist_cursor;
4534 //static int modlist_viewcount;
4535
4536 static int modlist_count = 0;
4537 static modlist_entry_t modlist[MODLIST_TOTALSIZE];
4538
4539 static void ModList_RebuildList(void)
4540 {
4541         int i,j;
4542         stringlist_t list;
4543         const char *description;
4544
4545         stringlistinit(&list);
4546         listdirectory(&list, fs_basedir, "");
4547         stringlistsort(&list, true);
4548         modlist_count = 0;
4549         modlist_numenabled = fs_numgamedirs;
4550         for (i = 0;i < list.numstrings && modlist_count < MODLIST_TOTALSIZE;i++)
4551         {
4552                 int desc_len;
4553
4554                 // reject any dirs that are part of the base game
4555                 if (gamedirname1 && !strcasecmp(gamedirname1, list.strings[i])) continue;
4556                 //if (gamedirname2 && !strcasecmp(gamedirname2, list.strings[i])) continue;
4557
4558                 // check if we can get a description of the gamedir (from modinfo.txt),
4559                 // or if the directory is valid but has no description (fs_checkgamedir_missing)
4560                 // otherwise this isn't a valid gamedir
4561                 description = FS_CheckGameDir(list.strings[i]);
4562                 if (description == NULL || description == fs_checkgamedir_missing) continue;
4563
4564                 desc_len = min(strlen(description), sizeof(modlist[modlist_count].name));
4565                 for (j = 0; j < desc_len; ++j)
4566                         if (!ISWHITESPACE(description[j]))
4567                         {
4568                                 dp_strlcpy(modlist[modlist_count].name, description, sizeof(modlist[modlist_count].name));
4569                                 break;
4570                         }
4571
4572                 dp_strlcpy (modlist[modlist_count].dir, list.strings[i], sizeof(modlist[modlist_count].dir));
4573                 //check currently loaded mods
4574                 modlist[modlist_count].loaded = false;
4575                 if (fs_numgamedirs)
4576                         for (j = 0; j < fs_numgamedirs; j++)
4577                                 if (!strcasecmp(fs_gamedirs[j], modlist[modlist_count].dir))
4578                                 {
4579                                         modlist[modlist_count].loaded = true;
4580                                         modlist[modlist_count].enabled = j;
4581                                         modlist_enabled[j] = modlist_count;
4582                                         break;
4583                                 }
4584                 modlist_count ++;
4585         }
4586         stringlistfreecontents(&list);
4587 }
4588
4589 static void ModList_Enable (void)
4590 {
4591         int i;
4592         int numgamedirs;
4593         char gamedirs[MODLIST_MAXDIRS][MAX_QPATH];
4594
4595         // copy our mod list into an array for FS_ChangeGameDirs
4596         numgamedirs = modlist_numenabled;
4597         for (i = 0; i < modlist_numenabled; i++)
4598                 dp_strlcpy (gamedirs[i], modlist[modlist_enabled[i]].dir,sizeof (gamedirs[i]));
4599
4600         // this code snippet is from FS_ChangeGameDirs
4601         if (fs_numgamedirs == numgamedirs)
4602         {
4603                 for (i = 0;i < numgamedirs;i++)
4604                         if (strcasecmp(fs_gamedirs[i], gamedirs[i]))
4605                                 break;
4606                 if (i == numgamedirs)
4607                         return; // already using this set of gamedirs, do nothing
4608         }
4609
4610         // this part is basically the same as the FS_GameDir_f function
4611         if ((cls.state == ca_connected && !cls.demoplayback) || sv.active)
4612         {
4613                 // actually, changing during game would work fine, but would be stupid
4614                 Con_Printf("Can not change gamedir while client is connected or server is running!\n");
4615                 return;
4616         }
4617
4618         FS_ChangeGameDirs (modlist_numenabled, gamedirs, true, true);
4619 }
4620
4621 void M_Menu_ModList_f(cmd_state_t *cmd)
4622 {
4623         key_dest = key_menu;
4624         m_state = m_modlist;
4625         m_entersound = true;
4626         modlist_cursor = 0;
4627         cl_connect_status[0] = '\0';
4628         ModList_RebuildList();
4629 }
4630
4631 static void M_Menu_ModList_AdjustSliders (int dir)
4632 {
4633         int i;
4634         S_LocalSound ("sound/misc/menu3.wav");
4635
4636         // stop adding mods, we reach the limit
4637         if (!modlist[modlist_cursor].loaded && (modlist_numenabled == MODLIST_MAXDIRS)) return;
4638         modlist[modlist_cursor].loaded = !modlist[modlist_cursor].loaded;
4639         if (modlist[modlist_cursor].loaded)
4640         {
4641                 modlist[modlist_cursor].enabled = modlist_numenabled;
4642                 //push the value on the enabled list
4643                 modlist_enabled[modlist_numenabled++] = modlist_cursor;
4644         }
4645         else
4646         {
4647                 //eliminate the value from the enabled list
4648                 for (i = modlist[modlist_cursor].enabled; i < modlist_numenabled; i++)
4649                 {
4650                         modlist_enabled[i] = modlist_enabled[i+1];
4651                         modlist[modlist_enabled[i]].enabled--;
4652                 }
4653                 modlist_numenabled--;
4654         }
4655 }
4656
4657 static void M_ModList_Draw (void)
4658 {
4659         int n, y, visible, start, end;
4660         cachepic_t *p;
4661         const char *s_available = "Available Mods";
4662         const char *s_enabled = "Enabled Mods";
4663
4664         // use as much vertical space as available
4665         if (gamemode == GAME_TRANSFUSION)
4666                 M_Background(640, vid_conheight.integer - 80);
4667         else
4668                 M_Background(640, vid_conheight.integer);
4669
4670         M_PrintRed(48 + 32, 32, s_available);
4671         M_PrintRed(432, 32, s_enabled);
4672         // Draw a list box with all enabled mods
4673         DrawQ_Pic(menu_x + 432, menu_y + 48, NULL, 172, 8 * modlist_numenabled, 0, 0, 0, 0.5, 0);
4674         for (y = 0; y < modlist_numenabled; y++)
4675                 M_PrintRed(432, 48 + y * 8, modlist[modlist_enabled[y]].dir);
4676
4677         if (*cl_connect_status)
4678                 M_Print(16, menu_height - 8, cl_connect_status);
4679         // scroll the list as the cursor moves
4680         y = 48;
4681         visible = (int)((menu_height - 16 - y) / 8 / 2);
4682         start = bound(0, modlist_cursor - (visible >> 1), modlist_count - visible);
4683         end = min(start + visible, modlist_count);
4684
4685         p = Draw_CachePic ("gfx/p_option");
4686         M_DrawPic((640 - Draw_GetPicWidth(p)) / 2, 4, "gfx/p_option");
4687         if (end > start)
4688         {
4689                 for (n = start;n < end;n++)
4690                 {
4691                         const char *item_label = (modlist[n].name[0] != '\0') ? modlist[n].name : modlist[n].dir;
4692
4693                         DrawQ_Pic(menu_x + 40, menu_y + y, NULL, 360, 8, n == modlist_cursor ? (0.5 + 0.2 * sin(host.realtime * M_PI)) : 0, 0, 0, 0.5, 0);
4694                         M_ItemPrint(80, y, item_label, true);
4695                         M_DrawCheckbox(48, y, modlist[n].loaded);
4696                         y +=8;
4697                 }
4698         }
4699         else
4700         {
4701                 M_Print(80, y, "No Mods found");
4702         }
4703 }
4704
4705 static void M_ModList_Key(cmd_state_t *cmd, int k, int ascii)
4706 {
4707         switch (k)
4708         {
4709         case K_ESCAPE:
4710                 ModList_Enable ();
4711                 M_Menu_Options_f(cmd);
4712                 break;
4713
4714         case K_SPACE:
4715                 S_LocalSound ("sound/misc/menu2.wav");
4716                 ModList_RebuildList();
4717                 break;
4718
4719         case K_UPARROW:
4720                 S_LocalSound ("sound/misc/menu1.wav");
4721                 modlist_cursor--;
4722                 if (modlist_cursor < 0)
4723                         modlist_cursor = modlist_count - 1;
4724                 break;
4725
4726         case K_LEFTARROW:
4727                 M_Menu_ModList_AdjustSliders (-1);
4728                 break;
4729
4730         case K_DOWNARROW:
4731                 S_LocalSound ("sound/misc/menu1.wav");
4732                 modlist_cursor++;
4733                 if (modlist_cursor >= modlist_count)
4734                         modlist_cursor = 0;
4735                 break;
4736
4737         case K_RIGHTARROW:
4738                 M_Menu_ModList_AdjustSliders (1);
4739                 break;
4740
4741         case K_ENTER:
4742                 S_LocalSound ("sound/misc/menu2.wav");
4743                 ModList_Enable ();
4744                 break;
4745
4746         default:
4747                 break;
4748         }
4749
4750 }
4751
4752 //=============================================================================
4753 /* Menu Subsystem */
4754
4755 static void M_KeyEvent(int key, int ascii, qbool downevent);
4756 static void M_Draw(void);
4757 void M_ToggleMenu(int mode);
4758 static void M_Shutdown(void);
4759
4760 static void M_Init (void)
4761 {
4762         menuplyr_load = true;
4763         menuplyr_pixels = NULL;
4764
4765         Cmd_AddCommand(CF_CLIENT, "menu_main", M_Menu_Main_f, "open the main menu");
4766         Cmd_AddCommand(CF_CLIENT, "menu_singleplayer", M_Menu_SinglePlayer_f, "open the singleplayer menu");
4767         Cmd_AddCommand(CF_CLIENT, "menu_load", M_Menu_Load_f, "open the loadgame menu");
4768         Cmd_AddCommand(CF_CLIENT, "menu_save", M_Menu_Save_f, "open the savegame menu");
4769         Cmd_AddCommand(CF_CLIENT, "menu_multiplayer", M_Menu_MultiPlayer_f, "open the multiplayer menu");
4770         Cmd_AddCommand(CF_CLIENT, "menu_setup", M_Menu_Setup_f, "open the player setup menu");
4771         Cmd_AddCommand(CF_CLIENT, "menu_options", M_Menu_Options_f, "open the options menu");
4772         Cmd_AddCommand(CF_CLIENT, "menu_options_effects", M_Menu_Options_Effects_f, "open the effects options menu");
4773         Cmd_AddCommand(CF_CLIENT, "menu_options_graphics", M_Menu_Options_Graphics_f, "open the graphics options menu");
4774         Cmd_AddCommand(CF_CLIENT, "menu_options_colorcontrol", M_Menu_Options_ColorControl_f, "open the color control menu");
4775         Cmd_AddCommand(CF_CLIENT, "menu_keys", M_Menu_Keys_f, "open the key binding menu");
4776         Cmd_AddCommand(CF_CLIENT, "menu_video", M_Menu_Video_f, "open the video options menu");
4777         Cmd_AddCommand(CF_CLIENT, "menu_reset", M_Menu_Reset_f, "open the reset to defaults menu");
4778         Cmd_AddCommand(CF_CLIENT, "menu_mods", M_Menu_ModList_f, "open the mods browser menu");
4779         Cmd_AddCommand(CF_CLIENT, "help", M_Menu_Help_f, "open the help menu");
4780         Cmd_AddCommand(CF_CLIENT, "menu_quit", M_Menu_Quit_f, "open the quit menu");
4781         Cmd_AddCommand(CF_CLIENT, "menu_transfusion_episode", M_Menu_Transfusion_Episode_f, "open the transfusion episode select menu");
4782         Cmd_AddCommand(CF_CLIENT, "menu_transfusion_skill", M_Menu_Transfusion_Skill_f, "open the transfusion skill select menu");
4783         Cmd_AddCommand(CF_CLIENT, "menu_credits", M_Menu_Credits_f, "open the credits menu");
4784 }
4785
4786 void M_Draw (void)
4787 {
4788         char vabuf[1024];
4789         if (key_dest != key_menu && key_dest != key_menu_grabbed)
4790                 m_state = m_none;
4791
4792         if (m_state == m_none)
4793                 return;
4794
4795         switch (m_state)
4796         {
4797         case m_none:
4798                 break;
4799
4800         case m_main:
4801                 M_Main_Draw ();
4802                 break;
4803
4804         case m_demo:
4805                 M_Demo_Draw ();
4806                 break;
4807
4808         case m_singleplayer:
4809                 M_SinglePlayer_Draw ();
4810                 break;
4811
4812         case m_transfusion_episode:
4813                 M_Transfusion_Episode_Draw ();
4814                 break;
4815
4816         case m_transfusion_skill:
4817                 M_Transfusion_Skill_Draw ();
4818                 break;
4819
4820         case m_load:
4821                 M_Load_Draw ();
4822                 break;
4823
4824         case m_save:
4825                 M_Save_Draw ();
4826                 break;
4827
4828         case m_multiplayer:
4829                 M_MultiPlayer_Draw ();
4830                 break;
4831
4832         case m_setup:
4833                 M_Setup_Draw ();
4834                 break;
4835
4836         case m_options:
4837                 M_Options_Draw ();
4838                 break;
4839
4840         case m_options_effects:
4841                 M_Options_Effects_Draw ();
4842                 break;
4843
4844         case m_options_graphics:
4845                 M_Options_Graphics_Draw ();
4846                 break;
4847
4848         case m_options_colorcontrol:
4849                 M_Options_ColorControl_Draw ();
4850                 break;
4851
4852         case m_keys:
4853                 M_Keys_Draw ();
4854                 break;
4855
4856         case m_reset:
4857                 M_Reset_Draw ();
4858                 break;
4859
4860         case m_video:
4861                 M_Video_Draw ();
4862                 break;
4863
4864         case m_help:
4865                 M_Help_Draw ();
4866                 break;
4867
4868         case m_credits:
4869                 M_Credits_Draw ();
4870                 break;
4871
4872         case m_quit:
4873                 M_Quit_Draw ();
4874                 break;
4875
4876         case m_lanconfig:
4877                 M_LanConfig_Draw ();
4878                 break;
4879
4880         case m_gameoptions:
4881                 M_GameOptions_Draw ();
4882                 break;
4883
4884         case m_slist:
4885                 M_ServerList_Draw ();
4886                 break;
4887
4888         case m_modlist:
4889                 M_ModList_Draw ();
4890                 break;
4891         }
4892
4893         if (gamemode == GAME_TRANSFUSION && !m_missingdata) {
4894                 if (m_state != m_credits) {
4895                         cachepic_t      *p, *drop1, *drop2, *drop3;
4896                         int g, scale_x, scale_y, scale_y_repeat, top_offset;
4897                         float scale_y_rate;
4898                         scale_y_repeat = vid_conheight.integer * 2;
4899                         g = (int)(host.realtime * 64)%96;
4900                         scale_y_rate = (float)(g+1) / 96;
4901                         top_offset = (g+12)/12;
4902                         p = Draw_CachePic (va(vabuf, sizeof(vabuf), "gfx/menu/blooddrip%i", top_offset));
4903                         drop1 = Draw_CachePic ("gfx/menu/blooddrop1");
4904                         drop2 = Draw_CachePic ("gfx/menu/blooddrop2");
4905                         drop3 = Draw_CachePic ("gfx/menu/blooddrop3");
4906                         for (scale_x = 0; scale_x <= vid_conwidth.integer; scale_x += Draw_GetPicWidth(p)) {
4907                                 for (scale_y = -scale_y_repeat; scale_y <= vid_conheight.integer; scale_y += scale_y_repeat) {
4908                                         DrawQ_Pic (scale_x + 21, scale_y_repeat * .5 + scale_y + scale_y_rate * scale_y_repeat, drop3, 0, 0, 1, 1, 1, 1, 0);
4909                                         DrawQ_Pic (scale_x +  116, scale_y_repeat + scale_y + scale_y_rate * scale_y_repeat, drop1, 0, 0, 1, 1, 1, 1, 0);
4910                                         DrawQ_Pic (scale_x + 180, scale_y_repeat * .275 + scale_y + scale_y_rate * scale_y_repeat, drop3, 0, 0, 1, 1, 1, 1, 0);
4911                                         DrawQ_Pic (scale_x + 242, scale_y_repeat * .75 + scale_y + scale_y_rate * scale_y_repeat, drop3, 0, 0, 1, 1, 1, 1, 0);
4912                                         DrawQ_Pic (scale_x + 304, scale_y_repeat * .25 + scale_y + scale_y_rate * scale_y_repeat, drop3, 0, 0, 1, 1, 1, 1, 0);
4913                                         DrawQ_Pic (scale_x + 362, scale_y_repeat * .46125 + scale_y + scale_y_rate * scale_y_repeat, drop3, 0, 0, 1, 1, 1, 1, 0);
4914                                         DrawQ_Pic (scale_x + 402, scale_y_repeat * .1725 + scale_y + scale_y_rate * scale_y_repeat, drop3, 0, 0, 1, 1, 1, 1, 0);
4915                                         DrawQ_Pic (scale_x + 438, scale_y_repeat * .9 + scale_y + scale_y_rate * scale_y_repeat, drop1, 0, 0, 1, 1, 1, 1, 0);
4916                                         DrawQ_Pic (scale_x + 484, scale_y_repeat * .5 + scale_y + scale_y_rate * scale_y_repeat, drop3, 0, 0, 1, 1, 1, 1, 0);
4917                                         DrawQ_Pic (scale_x + 557, scale_y_repeat * .9425 + scale_y + scale_y_rate * scale_y_repeat, drop1, 0, 0, 1, 1, 1, 1, 0);
4918                                         DrawQ_Pic (scale_x + 606, scale_y_repeat * .5 + scale_y + scale_y_rate * scale_y_repeat, drop2, 0, 0, 1, 1, 1, 1, 0);
4919                                 }
4920                                 DrawQ_Pic (scale_x, -1, Draw_CachePic (va(vabuf, sizeof(vabuf), "gfx/menu/blooddrip%i", top_offset)), 0, 0, 1, 1, 1, 1, 0);
4921                         }
4922                 }
4923         }
4924
4925         if (m_entersound)
4926         {
4927                 S_LocalSound ("sound/misc/menu2.wav");
4928                 m_entersound = false;
4929         }
4930
4931         S_ExtraUpdate ();
4932 }
4933
4934
4935 void M_KeyEvent (int key, int ascii, qbool downevent)
4936 {
4937         cmd_state_t *cmd = cmd_local;
4938         if (!downevent)
4939                 return;
4940         switch (m_state)
4941         {
4942         case m_none:
4943                 return;
4944
4945         case m_main:
4946                 M_Main_Key(cmd, key, ascii);
4947                 return;
4948
4949         case m_demo:
4950                 M_Demo_Key(cmd, key, ascii);
4951                 return;
4952
4953         case m_singleplayer:
4954                 M_SinglePlayer_Key(cmd, key, ascii);
4955                 return;
4956
4957         case m_transfusion_episode:
4958                 M_Transfusion_Episode_Key(cmd, key, ascii);
4959                 return;
4960
4961         case m_transfusion_skill:
4962                 M_Transfusion_Skill_Key(cmd, key, ascii);
4963                 return;
4964
4965         case m_load:
4966                 M_Load_Key(cmd, key, ascii);
4967                 return;
4968
4969         case m_save:
4970                 M_Save_Key(cmd, key, ascii);
4971                 return;
4972
4973         case m_multiplayer:
4974                 M_MultiPlayer_Key(cmd, key, ascii);
4975                 return;
4976
4977         case m_setup:
4978                 M_Setup_Key(cmd, key, ascii);
4979                 return;
4980
4981         case m_options:
4982                 M_Options_Key(cmd, key, ascii);
4983                 return;
4984
4985         case m_options_effects:
4986                 M_Options_Effects_Key(cmd, key, ascii);
4987                 return;
4988
4989         case m_options_graphics:
4990                 M_Options_Graphics_Key(cmd, key, ascii);
4991                 return;
4992
4993         case m_options_colorcontrol:
4994                 M_Options_ColorControl_Key(cmd, key, ascii);
4995                 return;
4996
4997         case m_keys:
4998                 M_Keys_Key(cmd, key, ascii);
4999                 return;
5000
5001         case m_reset:
5002                 M_Reset_Key(cmd, key, ascii);
5003                 return;
5004
5005         case m_video:
5006                 M_Video_Key(cmd, key, ascii);
5007                 return;
5008
5009         case m_help:
5010                 M_Help_Key(cmd, key, ascii);
5011                 return;
5012
5013         case m_credits:
5014                 M_Credits_Key(cmd, key, ascii);
5015                 return;
5016
5017         case m_quit:
5018                 M_Quit_Key(cmd, key, ascii);
5019                 return;
5020
5021         case m_lanconfig:
5022                 M_LanConfig_Key(cmd, key, ascii);
5023                 return;
5024
5025         case m_gameoptions:
5026                 M_GameOptions_Key(cmd, key, ascii);
5027                 return;
5028
5029         case m_slist:
5030                 M_ServerList_Key(cmd, key, ascii);
5031                 return;
5032
5033         case m_modlist:
5034                 M_ModList_Key(cmd, key, ascii);
5035                 return;
5036         }
5037
5038 }
5039
5040 static void M_NewMap(void)
5041 {
5042 }
5043
5044 static int M_GetServerListEntryCategory(const serverlist_entry_t *entry)
5045 {
5046         return 0;
5047 }
5048
5049 void M_Shutdown(void)
5050 {
5051         // reset key_dest
5052         key_dest = key_game;
5053 }
5054
5055 //============================================================================
5056 // Menu prog handling
5057
5058 static void MP_CheckRequiredFuncs(prvm_prog_t *prog, const char *filename)
5059 {
5060         int i;
5061         const char *m_required_func[] = {
5062                 "m_init",
5063                 "m_keydown",
5064                 "m_draw",
5065                 "m_toggle",
5066                 "m_shutdown",
5067         };
5068         int m_numrequiredfunc = sizeof(m_required_func) / sizeof(char*);
5069
5070         for(i = 0; i < m_numrequiredfunc; ++i)
5071                 if(PRVM_ED_FindFunction(prog, m_required_func[i]) == 0)
5072                         prog->error_cmd("%s: %s not found in %s",prog->name, m_required_func[i], filename);
5073 }
5074
5075 static prvm_required_field_t m_required_fields[] =
5076 {
5077 #define PRVM_DECLARE_serverglobalfloat(x)
5078 #define PRVM_DECLARE_serverglobalvector(x)
5079 #define PRVM_DECLARE_serverglobalstring(x)
5080 #define PRVM_DECLARE_serverglobaledict(x)
5081 #define PRVM_DECLARE_serverglobalfunction(x)
5082 #define PRVM_DECLARE_clientglobalfloat(x)
5083 #define PRVM_DECLARE_clientglobalvector(x)
5084 #define PRVM_DECLARE_clientglobalstring(x)
5085 #define PRVM_DECLARE_clientglobaledict(x)
5086 #define PRVM_DECLARE_clientglobalfunction(x)
5087 #define PRVM_DECLARE_menuglobalfloat(x)
5088 #define PRVM_DECLARE_menuglobalvector(x)
5089 #define PRVM_DECLARE_menuglobalstring(x)
5090 #define PRVM_DECLARE_menuglobaledict(x)
5091 #define PRVM_DECLARE_menuglobalfunction(x)
5092 #define PRVM_DECLARE_serverfieldfloat(x)
5093 #define PRVM_DECLARE_serverfieldvector(x)
5094 #define PRVM_DECLARE_serverfieldstring(x)
5095 #define PRVM_DECLARE_serverfieldedict(x)
5096 #define PRVM_DECLARE_serverfieldfunction(x)
5097 #define PRVM_DECLARE_clientfieldfloat(x)
5098 #define PRVM_DECLARE_clientfieldvector(x)
5099 #define PRVM_DECLARE_clientfieldstring(x)
5100 #define PRVM_DECLARE_clientfieldedict(x)
5101 #define PRVM_DECLARE_clientfieldfunction(x)
5102 #define PRVM_DECLARE_menufieldfloat(x) {ev_float, #x},
5103 #define PRVM_DECLARE_menufieldvector(x) {ev_vector, #x},
5104 #define PRVM_DECLARE_menufieldstring(x) {ev_string, #x},
5105 #define PRVM_DECLARE_menufieldedict(x) {ev_entity, #x},
5106 #define PRVM_DECLARE_menufieldfunction(x) {ev_function, #x},
5107 #define PRVM_DECLARE_serverfunction(x)
5108 #define PRVM_DECLARE_clientfunction(x)
5109 #define PRVM_DECLARE_menufunction(x)
5110 #define PRVM_DECLARE_field(x)
5111 #define PRVM_DECLARE_global(x)
5112 #define PRVM_DECLARE_function(x)
5113 #include "prvm_offsets.h"
5114 #undef PRVM_DECLARE_serverglobalfloat
5115 #undef PRVM_DECLARE_serverglobalvector
5116 #undef PRVM_DECLARE_serverglobalstring
5117 #undef PRVM_DECLARE_serverglobaledict
5118 #undef PRVM_DECLARE_serverglobalfunction
5119 #undef PRVM_DECLARE_clientglobalfloat
5120 #undef PRVM_DECLARE_clientglobalvector
5121 #undef PRVM_DECLARE_clientglobalstring
5122 #undef PRVM_DECLARE_clientglobaledict
5123 #undef PRVM_DECLARE_clientglobalfunction
5124 #undef PRVM_DECLARE_menuglobalfloat
5125 #undef PRVM_DECLARE_menuglobalvector
5126 #undef PRVM_DECLARE_menuglobalstring
5127 #undef PRVM_DECLARE_menuglobaledict
5128 #undef PRVM_DECLARE_menuglobalfunction
5129 #undef PRVM_DECLARE_serverfieldfloat
5130 #undef PRVM_DECLARE_serverfieldvector
5131 #undef PRVM_DECLARE_serverfieldstring
5132 #undef PRVM_DECLARE_serverfieldedict
5133 #undef PRVM_DECLARE_serverfieldfunction
5134 #undef PRVM_DECLARE_clientfieldfloat
5135 #undef PRVM_DECLARE_clientfieldvector
5136 #undef PRVM_DECLARE_clientfieldstring
5137 #undef PRVM_DECLARE_clientfieldedict
5138 #undef PRVM_DECLARE_clientfieldfunction
5139 #undef PRVM_DECLARE_menufieldfloat
5140 #undef PRVM_DECLARE_menufieldvector
5141 #undef PRVM_DECLARE_menufieldstring
5142 #undef PRVM_DECLARE_menufieldedict
5143 #undef PRVM_DECLARE_menufieldfunction
5144 #undef PRVM_DECLARE_serverfunction
5145 #undef PRVM_DECLARE_clientfunction
5146 #undef PRVM_DECLARE_menufunction
5147 #undef PRVM_DECLARE_field
5148 #undef PRVM_DECLARE_global
5149 #undef PRVM_DECLARE_function
5150 };
5151
5152 static int m_numrequiredfields = sizeof(m_required_fields) / sizeof(m_required_fields[0]);
5153
5154 static prvm_required_field_t m_required_globals[] =
5155 {
5156 #define PRVM_DECLARE_serverglobalfloat(x)
5157 #define PRVM_DECLARE_serverglobalvector(x)
5158 #define PRVM_DECLARE_serverglobalstring(x)
5159 #define PRVM_DECLARE_serverglobaledict(x)
5160 #define PRVM_DECLARE_serverglobalfunction(x)
5161 #define PRVM_DECLARE_clientglobalfloat(x)
5162 #define PRVM_DECLARE_clientglobalvector(x)
5163 #define PRVM_DECLARE_clientglobalstring(x)
5164 #define PRVM_DECLARE_clientglobaledict(x)
5165 #define PRVM_DECLARE_clientglobalfunction(x)
5166 #define PRVM_DECLARE_menuglobalfloat(x) {ev_float, #x},
5167 #define PRVM_DECLARE_menuglobalvector(x) {ev_vector, #x},
5168 #define PRVM_DECLARE_menuglobalstring(x) {ev_string, #x},
5169 #define PRVM_DECLARE_menuglobaledict(x) {ev_entity, #x},
5170 #define PRVM_DECLARE_menuglobalfunction(x) {ev_function, #x},
5171 #define PRVM_DECLARE_serverfieldfloat(x)
5172 #define PRVM_DECLARE_serverfieldvector(x)
5173 #define PRVM_DECLARE_serverfieldstring(x)
5174 #define PRVM_DECLARE_serverfieldedict(x)
5175 #define PRVM_DECLARE_serverfieldfunction(x)
5176 #define PRVM_DECLARE_clientfieldfloat(x)
5177 #define PRVM_DECLARE_clientfieldvector(x)
5178 #define PRVM_DECLARE_clientfieldstring(x)
5179 #define PRVM_DECLARE_clientfieldedict(x)
5180 #define PRVM_DECLARE_clientfieldfunction(x)
5181 #define PRVM_DECLARE_menufieldfloat(x)
5182 #define PRVM_DECLARE_menufieldvector(x)
5183 #define PRVM_DECLARE_menufieldstring(x)
5184 #define PRVM_DECLARE_menufieldedict(x)
5185 #define PRVM_DECLARE_menufieldfunction(x)
5186 #define PRVM_DECLARE_serverfunction(x)
5187 #define PRVM_DECLARE_clientfunction(x)
5188 #define PRVM_DECLARE_menufunction(x)
5189 #define PRVM_DECLARE_field(x)
5190 #define PRVM_DECLARE_global(x)
5191 #define PRVM_DECLARE_function(x)
5192 #include "prvm_offsets.h"
5193 #undef PRVM_DECLARE_serverglobalfloat
5194 #undef PRVM_DECLARE_serverglobalvector
5195 #undef PRVM_DECLARE_serverglobalstring
5196 #undef PRVM_DECLARE_serverglobaledict
5197 #undef PRVM_DECLARE_serverglobalfunction
5198 #undef PRVM_DECLARE_clientglobalfloat
5199 #undef PRVM_DECLARE_clientglobalvector
5200 #undef PRVM_DECLARE_clientglobalstring
5201 #undef PRVM_DECLARE_clientglobaledict
5202 #undef PRVM_DECLARE_clientglobalfunction
5203 #undef PRVM_DECLARE_menuglobalfloat
5204 #undef PRVM_DECLARE_menuglobalvector
5205 #undef PRVM_DECLARE_menuglobalstring
5206 #undef PRVM_DECLARE_menuglobaledict
5207 #undef PRVM_DECLARE_menuglobalfunction
5208 #undef PRVM_DECLARE_serverfieldfloat
5209 #undef PRVM_DECLARE_serverfieldvector
5210 #undef PRVM_DECLARE_serverfieldstring
5211 #undef PRVM_DECLARE_serverfieldedict
5212 #undef PRVM_DECLARE_serverfieldfunction
5213 #undef PRVM_DECLARE_clientfieldfloat
5214 #undef PRVM_DECLARE_clientfieldvector
5215 #undef PRVM_DECLARE_clientfieldstring
5216 #undef PRVM_DECLARE_clientfieldedict
5217 #undef PRVM_DECLARE_clientfieldfunction
5218 #undef PRVM_DECLARE_menufieldfloat
5219 #undef PRVM_DECLARE_menufieldvector
5220 #undef PRVM_DECLARE_menufieldstring
5221 #undef PRVM_DECLARE_menufieldedict
5222 #undef PRVM_DECLARE_menufieldfunction
5223 #undef PRVM_DECLARE_serverfunction
5224 #undef PRVM_DECLARE_clientfunction
5225 #undef PRVM_DECLARE_menufunction
5226 #undef PRVM_DECLARE_field
5227 #undef PRVM_DECLARE_global
5228 #undef PRVM_DECLARE_function
5229 };
5230
5231 static int m_numrequiredglobals = sizeof(m_required_globals) / sizeof(m_required_globals[0]);
5232
5233 void MR_SetRouting (qbool forceold);
5234
5235 void MVM_error_cmd(const char *format, ...) DP_FUNC_PRINTF(1);
5236 void MVM_error_cmd(const char *format, ...)
5237 {
5238         static qbool processingError = false;
5239         char errorstring[MAX_INPUTLINE];
5240         va_list argptr;
5241         int outfd = sys.outfd;
5242
5243         // set output to stderr
5244         sys.outfd = fileno(stderr);
5245
5246         va_start (argptr, format);
5247         dpvsnprintf (errorstring, sizeof(errorstring), format, argptr);
5248         va_end (argptr);
5249
5250         if (host.framecount < 3)
5251                 Sys_Error("Menu_Error: %s", errorstring);
5252
5253         Con_Printf(CON_ERROR "Menu_Error: %s\n", errorstring);
5254
5255         if(!processingError)
5256         {
5257                 processingError = true;
5258                 PRVM_Crash();
5259                 processingError = false;
5260         }
5261         else
5262                 Sys_Error("Menu_Error: Recursive call to MVM_error_cmd (from PRVM_Crash)!");
5263
5264         Con_Print("Falling back to engine menu\n");
5265         key_dest = key_game;
5266         MR_SetRouting (true);
5267
5268         // reset the active scene, too (to be on the safe side ;))
5269         R_SelectScene( RST_CLIENT );
5270
5271         // prevent an endless loop if the error was triggered by a command
5272         Cbuf_Clear(cmd_local->cbuf);
5273
5274         // restore configured outfd
5275         sys.outfd = outfd;
5276
5277         // Let video start at least
5278         Host_AbortCurrentFrame();
5279 }
5280
5281 static void MVM_begin_increase_edicts(prvm_prog_t *prog)
5282 {
5283 }
5284
5285 static void MVM_end_increase_edicts(prvm_prog_t *prog)
5286 {
5287 }
5288
5289 static void MVM_init_edict(prvm_prog_t *prog, prvm_edict_t *edict)
5290 {
5291 }
5292
5293 static void MVM_free_edict(prvm_prog_t *prog, prvm_edict_t *ed)
5294 {
5295 }
5296
5297 static void MVM_count_edicts(prvm_prog_t *prog)
5298 {
5299         int i;
5300         prvm_edict_t *ent;
5301         int active;
5302
5303         active = 0;
5304         for (i=0 ; i<prog->num_edicts ; i++)
5305         {
5306                 ent = PRVM_EDICT_NUM(i);
5307                 if (ent->free)
5308                         continue;
5309                 active++;
5310         }
5311
5312         Con_Printf("num_edicts:%3i\n", prog->num_edicts);
5313         Con_Printf("active    :%3i\n", active);
5314 }
5315
5316 static qbool MVM_load_edict(prvm_prog_t *prog, prvm_edict_t *ent)
5317 {
5318         return true;
5319 }
5320
5321 static void MP_KeyEvent (int key, int ascii, qbool downevent)
5322 {
5323         prvm_prog_t *prog = MVM_prog;
5324
5325         // pass key
5326         prog->globals.fp[OFS_PARM0] = (prvm_vec_t) key;
5327         prog->globals.fp[OFS_PARM1] = (prvm_vec_t) ascii;
5328         if (downevent)
5329                 prog->ExecuteProgram(prog, PRVM_menufunction(m_keydown),"m_keydown(float key, float ascii) required");
5330         else if (PRVM_menufunction(m_keyup))
5331                 prog->ExecuteProgram(prog, PRVM_menufunction(m_keyup),"m_keyup(float key, float ascii) required");
5332 }
5333
5334 static void MP_Draw (void)
5335 {
5336         prvm_prog_t *prog = MVM_prog;
5337         float oldquality;
5338
5339         // don't crash if we draw a frame between prog shutdown and restart, see Host_LoadConfig_f
5340         if (!prog->loaded)
5341                 return;
5342
5343         R_SelectScene( RST_MENU );
5344
5345         // reset the temp entities each frame
5346         r_refdef.scene.numtempentities = 0;
5347
5348         // menu scenes do not use reduced rendering quality
5349         oldquality = r_refdef.view.quality;
5350         r_refdef.view.quality = 1;
5351         // TODO: this needs to be exposed to R_SetView (or something similar) ASAP [2/5/2008 Andreas]
5352         r_refdef.scene.time = host.realtime;
5353
5354         // free memory for resources that are no longer referenced
5355         PRVM_GarbageCollection(prog);
5356
5357         // FIXME: this really shouldnt error out lest we have a very broken refdef state...?
5358         // or does it kill the server too?
5359         PRVM_G_FLOAT(OFS_PARM0) = vid.mode.width;
5360         PRVM_G_FLOAT(OFS_PARM1) = vid.mode.height;
5361         prog->ExecuteProgram(prog, PRVM_menufunction(m_draw),"m_draw() required");
5362
5363         // TODO: imo this should be moved into scene, too [1/27/2008 Andreas]
5364         r_refdef.view.quality = oldquality;
5365
5366         R_SelectScene( RST_CLIENT );
5367 }
5368
5369 static void MP_ToggleMenu(int mode)
5370 {
5371         prvm_prog_t *prog = MVM_prog;
5372
5373         prog->globals.fp[OFS_PARM0] = (prvm_vec_t) mode;
5374         prog->ExecuteProgram(prog, PRVM_menufunction(m_toggle),"m_toggle(float mode) required");
5375 }
5376
5377 static void MP_NewMap(void)
5378 {
5379         prvm_prog_t *prog = MVM_prog;
5380         if (PRVM_menufunction(m_newmap))
5381                 prog->ExecuteProgram(prog, PRVM_menufunction(m_newmap),"m_newmap() required");
5382 }
5383
5384 const serverlist_entry_t *serverlist_callbackentry = NULL;
5385 static int MP_GetServerListEntryCategory(const serverlist_entry_t *entry)
5386 {
5387         prvm_prog_t *prog = MVM_prog;
5388         serverlist_callbackentry = entry;
5389         if (PRVM_menufunction(m_gethostcachecategory))
5390         {
5391                 prog->globals.fp[OFS_PARM0] = (prvm_vec_t) -1;
5392                 prog->ExecuteProgram(prog, PRVM_menufunction(m_gethostcachecategory),"m_gethostcachecategory(float entry) required");
5393                 serverlist_callbackentry = NULL;
5394                 return prog->globals.fp[OFS_RETURN];
5395         }
5396         else
5397         {
5398                 return 0;
5399         }
5400 }
5401
5402 static void MP_Shutdown (void)
5403 {
5404         prvm_prog_t *prog = MVM_prog;
5405         if (prog->loaded)
5406                 prog->ExecuteProgram(prog, PRVM_menufunction(m_shutdown),"m_shutdown() required");
5407
5408         // reset key_dest
5409         key_dest = key_game;
5410
5411         // AK not using this cause Im not sure whether this is useful at all instead :
5412         PRVM_Prog_Reset(prog);
5413 }
5414
5415 static void MP_Init (void)
5416 {
5417         prvm_prog_t *prog = MVM_prog;
5418         PRVM_Prog_Init(prog, cmd_local);
5419
5420         prog->edictprivate_size = 0; // no private struct used
5421         prog->name = "menu";
5422         prog->num_edicts = 1;
5423         prog->limit_edicts = M_MAX_EDICTS;
5424         prog->extensionstring = vm_m_extensions;
5425         prog->builtins = vm_m_builtins;
5426         prog->numbuiltins = vm_m_numbuiltins;
5427
5428         // all callbacks must be defined (pointers are not checked before calling)
5429         prog->begin_increase_edicts = MVM_begin_increase_edicts;
5430         prog->end_increase_edicts   = MVM_end_increase_edicts;
5431         prog->init_edict            = MVM_init_edict;
5432         prog->free_edict            = MVM_free_edict;
5433         prog->count_edicts          = MVM_count_edicts;
5434         prog->load_edict            = MVM_load_edict;
5435         prog->init_cmd              = MVM_init_cmd;
5436         prog->reset_cmd             = MVM_reset_cmd;
5437         prog->error_cmd             = MVM_error_cmd;
5438         prog->ExecuteProgram        = MVM_ExecuteProgram;
5439
5440         // allocate the mempools
5441         prog->progs_mempool = Mem_AllocPool(menu_progs.string, 0, NULL);
5442
5443         PRVM_Prog_Load(prog, menu_progs.string, NULL, 0, MP_CheckRequiredFuncs, m_numrequiredfields, m_required_fields, m_numrequiredglobals, m_required_globals);
5444
5445         // note: OP_STATE is not supported by menu qc, we don't even try to detect
5446         // it here
5447
5448         in_client_mouse = true;
5449
5450         // call the prog init
5451         prog->ExecuteProgram(prog, PRVM_menufunction(m_init),"m_init() required");
5452
5453         // Once m_init was called, we consider menuqc code fully initialized.
5454         prog->inittime = host.realtime;
5455 }
5456
5457 //============================================================================
5458 // Menu router
5459
5460 void (*MR_KeyEvent) (int key, int ascii, qbool downevent);
5461 void (*MR_Draw) (void);
5462 void (*MR_ToggleMenu) (int mode);
5463 void (*MR_Shutdown) (void);
5464 void (*MR_NewMap) (void);
5465 int (*MR_GetServerListEntryCategory) (const serverlist_entry_t *entry);
5466
5467 void MR_SetRouting(qbool forceold)
5468 {
5469         // if the menu prog isnt available or forceqmenu ist set, use the old menu
5470         if(!FS_FileExists(menu_progs.string) || forceqmenu.integer || forceold)
5471         {
5472                 // set menu router function pointers
5473                 MR_KeyEvent = M_KeyEvent;
5474                 MR_Draw = M_Draw;
5475                 MR_ToggleMenu = M_ToggleMenu;
5476                 MR_Shutdown = M_Shutdown;
5477                 MR_NewMap = M_NewMap;
5478                 MR_GetServerListEntryCategory = M_GetServerListEntryCategory;
5479                 M_Init();
5480         }
5481         else
5482         {
5483                 // set menu router function pointers
5484                 MR_KeyEvent = MP_KeyEvent;
5485                 MR_Draw = MP_Draw;
5486                 MR_ToggleMenu = MP_ToggleMenu;
5487                 MR_Shutdown = MP_Shutdown;
5488                 MR_NewMap = MP_NewMap;
5489                 MR_GetServerListEntryCategory = MP_GetServerListEntryCategory;
5490                 MP_Init();
5491         }
5492 }
5493
5494 void MR_Restart(void)
5495 {
5496         if(MR_Shutdown)
5497                 MR_Shutdown ();
5498         MR_SetRouting (false);
5499 }
5500
5501 static void MR_Restart_f(cmd_state_t *cmd)
5502 {
5503         MR_Restart();
5504 }
5505
5506 static void Call_MR_ToggleMenu_f(cmd_state_t *cmd)
5507 {
5508         int m;
5509         m = ((Cmd_Argc(cmd) < 2) ? -1 : atoi(Cmd_Argv(cmd, 1)));
5510         CL_StartVideo();
5511         if(MR_ToggleMenu)
5512                 MR_ToggleMenu(m);
5513 }
5514
5515 void MR_Init_Commands(void)
5516 {
5517         // set router console commands
5518         Cvar_RegisterVariable (&forceqmenu);
5519         Cvar_RegisterVariable (&menu_options_colorcontrol_correctionvalue);
5520         Cvar_RegisterVariable (&menu_progs);
5521         Cmd_AddCommand(CF_CLIENT, "menu_restart", MR_Restart_f, "restart menu system (reloads menu.dat)");
5522         Cmd_AddCommand(CF_CLIENT, "togglemenu", Call_MR_ToggleMenu_f, "opens or closes menu");
5523 }
5524
5525 void MR_Init(void)
5526 {
5527         vid_mode_t res[1024];
5528         size_t res_count, i;
5529
5530         res_count = VID_ListModes(res, sizeof(res) / sizeof(*res));
5531         res_count = VID_SortModes(res, res_count, false, false, true);
5532         if(res_count)
5533         {
5534                 video_resolutions_count = (int)res_count;
5535                 video_resolutions = (video_resolution_t *) Mem_Alloc(cls.permanentmempool, sizeof(*video_resolutions) * (video_resolutions_count + 1));
5536                 memset(&video_resolutions[video_resolutions_count], 0, sizeof(video_resolutions[video_resolutions_count]));
5537                 for(i = 0; i < res_count; ++i)
5538                 {
5539                         int n, d, t;
5540                         video_resolutions[i].type = "Detected mode"; // FIXME make this more dynamic
5541                         video_resolutions[i].width = res[i].width;
5542                         video_resolutions[i].height = res[i].height;
5543                         video_resolutions[i].pixelheight = res[i].pixelheight_num / (double) res[i].pixelheight_denom;
5544                         n = res[i].pixelheight_denom * video_resolutions[i].width;
5545                         d = res[i].pixelheight_num * video_resolutions[i].height;
5546                         while(d)
5547                         {
5548                                 t = n;
5549                                 n = d;
5550                                 d = t % d;
5551                         }
5552                         d = (res[i].pixelheight_num * video_resolutions[i].height) / n;
5553                         n = (res[i].pixelheight_denom * video_resolutions[i].width) / n;
5554                         switch(n * 0x10000 | d)
5555                         {
5556                                 case 0x00040003:
5557                                         video_resolutions[i].conwidth = 640;
5558                                         video_resolutions[i].conheight = 480;
5559                                         video_resolutions[i].type = "Standard 4x3";
5560                                         break;
5561                                 case 0x00050004:
5562                                         video_resolutions[i].conwidth = 640;
5563                                         video_resolutions[i].conheight = 512;
5564                                         if(res[i].pixelheight_denom == res[i].pixelheight_num)
5565                                                 video_resolutions[i].type = "Square Pixel (LCD) 5x4";
5566                                         else
5567                                                 video_resolutions[i].type = "Short Pixel (CRT) 5x4";
5568                                         break;
5569                                 case 0x00080005:
5570                                         video_resolutions[i].conwidth = 640;
5571                                         video_resolutions[i].conheight = 400;
5572                                         if(res[i].pixelheight_denom == res[i].pixelheight_num)
5573                                                 video_resolutions[i].type = "Widescreen 8x5";
5574                                         else
5575                                                 video_resolutions[i].type = "Tall Pixel (CRT) 8x5";
5576
5577                                         break;
5578                                 case 0x00050003:
5579                                         video_resolutions[i].conwidth = 640;
5580                                         video_resolutions[i].conheight = 384;
5581                                         video_resolutions[i].type = "Widescreen 5x3";
5582                                         break;
5583                                 case 0x000D0009:
5584                                         video_resolutions[i].conwidth = 640;
5585                                         video_resolutions[i].conheight = 400;
5586                                         video_resolutions[i].type = "Widescreen 14x9";
5587                                         break;
5588                                 case 0x00100009:
5589                                         video_resolutions[i].conwidth = 640;
5590                                         video_resolutions[i].conheight = 480;
5591                                         video_resolutions[i].type = "Widescreen 16x9";
5592                                         break;
5593                                 case 0x00030002:
5594                                         video_resolutions[i].conwidth = 720;
5595                                         video_resolutions[i].conheight = 480;
5596                                         video_resolutions[i].type = "NTSC 3x2";
5597                                         break;
5598                                 case 0x000D000B:
5599                                         video_resolutions[i].conwidth = 720;
5600                                         video_resolutions[i].conheight = 566;
5601                                         video_resolutions[i].type = "PAL 14x11";
5602                                         break;
5603                                 case 0x00080007:
5604                                         if(video_resolutions[i].width >= 512)
5605                                         {
5606                                                 video_resolutions[i].conwidth = 512;
5607                                                 video_resolutions[i].conheight = 448;
5608                                                 video_resolutions[i].type = "SNES 8x7";
5609                                         }
5610                                         else
5611                                         {
5612                                                 video_resolutions[i].conwidth = 256;
5613                                                 video_resolutions[i].conheight = 224;
5614                                                 video_resolutions[i].type = "NES 8x7";
5615                                         }
5616                                         break;
5617                                 default:
5618                                         video_resolutions[i].conwidth = 640;
5619                                         video_resolutions[i].conheight = 640 * d / n;
5620                                         video_resolutions[i].type = "Detected mode";
5621                                         break;
5622                         }
5623                         if(video_resolutions[i].conwidth > video_resolutions[i].width || video_resolutions[i].conheight > video_resolutions[i].height)
5624                         {
5625                                 int f1, f2;
5626                                 f1 = video_resolutions[i].conwidth > video_resolutions[i].width;
5627                                 f2 = video_resolutions[i].conheight > video_resolutions[i].height;
5628                                 if(f1 > f2)
5629                                 {
5630                                         video_resolutions[i].conwidth = video_resolutions[i].width;
5631                                         video_resolutions[i].conheight = video_resolutions[i].conheight / f1;
5632                                 }
5633                                 else
5634                                 {
5635                                         video_resolutions[i].conwidth = video_resolutions[i].conwidth / f2;
5636                                         video_resolutions[i].conheight = video_resolutions[i].height;
5637                                 }
5638                         }
5639                 }
5640         }
5641         else
5642         {
5643                 video_resolutions = video_resolutions_hardcoded;
5644                 video_resolutions_count = sizeof(video_resolutions_hardcoded) / sizeof(*video_resolutions_hardcoded) - 1;
5645         }
5646
5647         menu_video_resolutions_forfullscreen = !!vid_fullscreen.integer;
5648         M_Menu_Video_FindResolution(vid.mode.width, vid.mode.height, vid_pixelheight.value);
5649
5650         // use -forceqmenu to use always the normal quake menu (it sets forceqmenu to 1)
5651 // COMMANDLINEOPTION: Client: -forceqmenu disables menu.dat (same as +forceqmenu 1)
5652         if(Sys_CheckParm("-forceqmenu"))
5653                 Cvar_SetValueQuick(&forceqmenu,1);
5654         // use -useqmenu for debugging proposes, cause it starts
5655         // the normal quake menu only the first time
5656 // COMMANDLINEOPTION: Client: -useqmenu causes the first time you open the menu to use the quake menu, then reverts to menu.dat (if forceqmenu is 0)
5657         if(Sys_CheckParm("-useqmenu"))
5658                 MR_SetRouting (true);
5659         else
5660                 MR_SetRouting (false);
5661 }