]> icculus.org git repositories - btb/d2x.git/blob - main/inferno.c
add gamma cvar
[btb/d2x.git] / main / inferno.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 /*
15  *
16  * inferno.c: Entry point of program (main procedure)
17  *
18  * After main initializes everything, most of the time is spent in the loop
19  * while (Function_mode != FMODE_EXIT)
20  * In this loop, the main menu is brought up first.
21  *
22  * main() for Inferno
23  *
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include <conf.h>
28 #endif
29
30 char copyright[] = "DESCENT II  COPYRIGHT (C) 1994-1996 PARALLAX SOFTWARE CORPORATION";
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <limits.h>
36
37 #ifdef __unix__
38 #include <unistd.h>
39 #include <sys/stat.h>
40 #include <sys/types.h>
41 #endif
42
43 #include <physfs.h>
44
45 #include "pstypes.h"
46 #include "strutil.h"
47 #include "console.h"
48 #include "gr.h"
49 #include "fix.h"
50 #include "vecmat.h"
51 #include "mono.h"
52 #include "key.h"
53 #include "timer.h"
54 #include "3d.h"
55 #include "inferno.h"
56 #include "inferno.h"
57 #include "error.h"
58 #include "game.h"
59 #include "segment.h"            //for Side_to_verts
60 #include "u_mem.h"
61 #include "segpoint.h"
62 #include "screens.h"
63 #include "texmap.h"
64 #include "texmerge.h"
65 #include "menu.h"
66 #include "wall.h"
67 #include "polyobj.h"
68 #include "effects.h"
69 #include "digi.h"
70 #include "palette.h"
71 #include "args.h"
72 #include "sounds.h"
73 #include "titles.h"
74 #include "player.h"
75 #include "text.h"
76 #include "newdemo.h"
77 #ifdef NETWORK
78 #include "network.h"
79 #include "modem.h"
80 #endif
81 #include "gamefont.h"
82 #include "kconfig.h"
83 #include "mouse.h"
84 #include "joy.h"
85 #include "newmenu.h"
86 #include "desc_id.h"
87 #include "config.h"
88 #include "joydefs.h"
89 #include "multi.h"
90 #include "songs.h"
91 #include "cfile.h"
92 #include "gameseq.h"
93 #include "gamepal.h"
94 #include "mission.h"
95 #include "movie.h"
96
97 // #  include "3dfx_des.h"
98
99 //added on 9/30/98 by Matt Mueller for selectable automap modes
100 #include "automap.h"
101 //end addition -MM
102
103 #include "../texmap/scanline.h" //for select_tmap -MM
104
105 #ifdef EDITOR
106 #include "editor/editor.h"
107 #include "editor/kdefs.h"
108 #include "ui.h"
109 #endif
110
111 #ifndef __MSDOS__
112 #include <SDL.h>
113 #endif
114
115 #include "vers_id.h"
116
117 void mem_init(void);
118 void arch_init(void);
119 void arch_init_start(void);
120
121 //Current version number
122
123 ubyte Version_major = 1;                //FULL VERSION
124 ubyte Version_minor = 2;
125
126 //static const char desc_id_checksum_str[] = DESC_ID_CHKSUM_TAG "0000"; // 4-byte checksum
127 char desc_id_exit_num = 0;
128
129 int Function_mode=FMODE_MENU;           //game or editor?
130 int Screen_mode=-1;                                     //game screen or editor screen?
131
132 //--unused-- grs_bitmap Inferno_bitmap_title;
133
134 int WVIDEO_running=0;           //debugger can set to 1 if running
135
136 #ifdef __WATCOMC__
137 int __far descent_critical_error_handler( unsigned deverr, unsigned errcode, unsigned __far * devhdr );
138 #endif
139
140 void check_joystick_calibration(void);
141
142 void show_order_form(void);
143
144 //--------------------------------------------------------------------------
145
146 extern int piggy_low_memory;
147
148
149 int descent_critical_error = 0;
150 unsigned descent_critical_deverror = 0;
151 unsigned descent_critical_errcode = 0;
152
153 extern int Network_allow_socket_changes;
154
155 extern void vfx_set_palette_sub(ubyte *);
156
157 extern int VR_low_res;
158
159 #define LINE_LEN        100
160
161 //read help from a file & print to screen
162 void print_commandline_help()
163 {
164         CFILE *ifile;
165         int have_binary=0;
166         char line[LINE_LEN];
167
168         ifile = cfopen("help.tex","rb");
169         if (!ifile) {
170                 ifile = cfopen("help.txb","rb");
171                 if (!ifile)
172                         Warning("Cannot load help text file.");
173                 have_binary = 1;
174         }
175
176         if (ifile)
177         {
178         char *end;
179         
180                 while ((end = cfgets(line,LINE_LEN,ifile))) {
181
182                         if (have_binary)
183                                 decode_text_line (line);
184
185             // This is the only use of cfgets that needs the CR
186                         strcat(end, "\n");
187
188                         if (line[0] == ';')
189                                 continue;               //don't show comments
190
191                         printf("%s",line);
192
193                 }
194
195                 cfclose(ifile);
196
197         }
198
199 //      printf( " Diagnostic:\n\n");
200 //      printf( "  -emul           %s\n", "Certain video cards need this option in order to run game");
201 //      printf( "  -ddemul         %s\n", "If -emul doesn't work, use this option");
202 //      printf( "\n");
203 #ifdef EDITOR
204         printf( " Editor Options:\n\n");
205         printf( "  -autoload <file>%s\n", "Autoload a level in the editor");
206         printf( "  -hoarddata      %s\n","Make the hoard ham file from some files, then exit");
207 //      printf( "  -nobm           %s\n","FIXME: Undocumented");
208         printf( "\n");
209 #endif
210         printf( " D2X Options:\n\n");
211         printf( "  -noredundancy   %s\n", "Do not send messages when picking up redundant items in multi");
212         printf( "  -shortpackets   %s\n", "Set shortpackets to default as on");
213 #ifdef OGL // currently only does anything on ogl build, so don't advertise othewise.
214         printf("  -renderstats    %s\n", "Enable renderstats info by default");
215 #endif
216         printf( "  -maxfps <n>     %s\n", "Set maximum framerate (1-100)");
217         printf( "  -notitles       %s\n", "Do not show titlescreens on startup");
218         printf( "  -hogdir <dir>   %s\n", "set shared data directory to <dir>");
219 #ifdef __unix__
220         printf( "  -nohogdir       %s\n", "don't try to use shared data directory");
221         printf( "  -userdir <dir>  %s\n", "set user dir to <dir> instead of $HOME/.d2x");
222 #endif
223         printf( "  -autoexec <file> %s\n", "autoexec file (execute console commands), defaults to autoexec.cfg");
224         printf( "  -ini <file>     %s\n", "option file (alternate to command line), defaults to d2x.ini");
225         printf( "  -autodemo       %s\n", "Start in demo mode");
226         printf( "  -bigpig         %s\n","FIXME: Undocumented");
227         printf( "  -bspgen         %s\n","FIXME: Undocumented");
228 //      printf( "  -cdproxy        %s\n","FIXME: Undocumented");
229 #ifndef NDEBUG
230         printf( "  -checktime      %s\n","FIXME: Undocumented");
231         printf( "  -showmeminfo    %s\n","FIXME: Undocumented");
232 #endif
233 //      printf( "  -codereadonly   %s\n","FIXME: Undocumented");
234 //      printf( "  -cyberimpact    %s\n","FIXME: Undocumented");
235         printf( "  -debug          %s\n","Enable very verbose output");
236 //      printf( "  -debugmode      %s\n","FIXME: Undocumented");
237 //      printf( "  -disallowgfx    %s\n","FIXME: Undocumented");
238 //      printf( "  -disallowreboot %s\n","FIXME: Undocumented");
239 //      printf( "  -dynamicsockets %s\n","FIXME: Undocumented");
240 //      printf( "  -forcegfx       %s\n","FIXME: Undocumented");
241 //      printf( "  -hw_3dacc       %s\n","FIXME: Undocumented");
242 #ifndef RELEASE
243         printf( "  -invulnerability %s\n","Make yourself invulnerable");
244 #endif
245         printf( "  -ipxnetwork <num> %s\n","Use IPX network number <num>");
246         printf( "  -jasen          %s\n","FIXME: Undocumented");
247         printf( "  -joyslow        %s\n","FIXME: Undocumented");
248 #ifdef NETWORK
249         printf( "  -kali           %s\n","use Kali for networking");
250 #endif
251 //      printf( "  -logfile        %s\n","FIXME: Undocumented");
252         printf( "  -lowresmovies   %s\n","Play low resolution movies if available (for slow machines)");
253 #if defined(EDITOR) || !defined(MACDATA)
254         printf( "  -macdata        %s\n","Read (and, for editor, write) mac data files (swap colors)");
255 #endif
256 //      printf( "  -memdbg         %s\n","FIXME: Undocumented");
257 //      printf( "  -monodebug      %s\n","FIXME: Undocumented");
258         printf( "  -nocdrom        %s\n","FIXME: Undocumented");
259 #ifndef NDEBUG
260         printf( "  -nofade         %s\n","Disable fades");
261 #endif
262 #ifdef NETWORK
263         printf( "  -nomatrixcheat  %s\n","FIXME: Undocumented");
264         printf( "  -norankings     %s\n","Disable multiplayer ranking system");
265         printf( "  -packets <num>  %s\n","Specifies the number of packets per second\n");
266 //      printf( "  -showaddress    %s\n","FIXME: Undocumented");
267         printf( "  -socket         %s\n","FIXME: Undocumented");
268 #endif
269 #if !defined(MACINTOSH) && !defined(WINDOWS)
270         printf( "  -nomixer        %s\n","Don't crank music volume");
271 #endif
272 //      printf( "  -nomodex        %s\n","FIXME: Undocumented");
273 #ifndef RELEASE
274         printf( "  -nomovies       %s\n","Don't play movies");
275         printf( "  -noscreens      %s\n","Skip briefing screens");
276 #endif
277         printf( "  -noredbook      %s\n","Disable redbook audio");
278         printf( "  -norun          %s\n","Bail out after initialization");
279 //      printf( "  -ordinaljoy     %s\n","FIXME: Undocumented");
280 //      printf( "  -rtscts         %s\n","Same as -ctsrts");
281 //      printf( "  -semiwin        %s\n","Use non-fullscreen mode");
282 //      printf( "  -specialdevice  %s\n","FIXME: Undocumented");
283 #ifdef TACTILE
284         printf( "  -stickmag       %s\n","FIXME: Undocumented");
285 #endif
286 //      printf( "  -stopwatch      %s\n","FIXME: Undocumented");
287         printf( "  -subtitles      %s\n","Turn on movie subtitles (English-only)");
288 //      printf( "  -sysram         %s\n","FIXME: Undocumented");
289         printf( "  -text <file>    %s\n","Specify alternate .tex file");
290 //      printf( "  -tsengdebug1    %s\n","FIXME: Undocumented");
291 //      printf( "  -tsengdebug2    %s\n","FIXME: Undocumented");
292 //      printf( "  -tsengdebug3    %s\n","FIXME: Undocumented");
293 //      printf( "  -vidram         %s\n","FIXME: Undocumented");
294         printf( "  -tmap <t>       %s\n","select texmapper to use (c,fp,i386,pent,ppro)");
295 #ifdef __MSDOS__
296         printf( "  -<X>x<Y>        %s\n", "Change screen resolution. Options:");
297         printf( "                     320x100;320x200;320x240;320x400;640x400;640x480;800x600;1024x768\n");
298 #else
299         printf( "  -<X>x<Y>        %s\n", "Change screen resolution to <X> by <Y>");
300 #endif
301         printf("  -niceautomap    %s\n", "Free cpu while doing automap");
302         printf( "  -automap<X>x<Y> %s\n","Set automap resolution to <X> by <Y>");
303         printf( "  -automap_gameres %s\n","Set automap to use the same resolution as in game");
304 //      printf( "  -menu<X>x<Y>    %s\n","Set menu resolution to <X> by <Y>");
305 //      printf( "  -menu_gameres   %s\n","Set menus to use the same resolution as in game");
306         printf("  -rearviewtime t %s\n", "time holding rearview key to use toggle mode (default 0.0625 seconds)");
307         printf( "\n");
308
309         printf( "D2X System Options:\n\n");
310 #ifdef __MSDOS__
311         printf("  -ihaveabrokenmouse %s\n", "try to make mouse work if it is not currently");
312         printf( "  -joy209         %s\n", "Use alternate port 209 for joystick");
313 #endif
314 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
315         printf( "  -fullscreen     %s\n", "Use fullscreen mode if available");
316 #endif
317 #ifdef OGL
318         printf( "  -gl_texmagfilt <f> %s\n","set GL_TEXTURE_MAG_FILTER (see readme.d1x)");
319         printf( "  -gl_texminfilt <f> %s\n","set GL_TEXTURE_MIN_FILTER (see readme.d1x)");
320         printf("  -gl_mipmap      %s\n", "set gl texture filters to \"standard\" (bilinear) mipmapping");
321         printf("  -gl_trilinear   %s\n", "set gl texture filters to trilinear mipmapping");
322         printf( "  -gl_simple      %s\n","set gl texture filters to gl_nearest for \"original\" look. (default)");
323         printf("  -gl_anisotropy <f> %s\n", "set maximum degree of anisotropy to <f>");
324         printf( "  -gl_alttexmerge %s\n","use new texmerge, usually uses less ram (default)");
325         printf( "  -gl_stdtexmerge %s\n","use old texmerge, uses more ram, but _might_ be a bit faster");
326 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
327         printf( "  -gl_voodoo      %s\n","force fullscreen mode only");
328 #endif
329         printf( "  -gl_16bittextures %s\n","attempt to use 16bit textures");
330         printf("  -gl_16bpp       %s\n", "attempt to use 16bit screen mode");
331         printf( "  -gl_reticle <r> %s\n","use OGL reticle 0=never 1=above 320x* 2=always");
332         printf( "  -gl_intensity4_ok %s\n","FIXME: Undocumented");
333         printf( "  -gl_luminance4_alpha4_ok %s\n","FIXME: Undocumented");
334         printf( "  -gl_readpixels_ok %s\n","FIXME: Undocumented");
335         printf( "  -gl_rgba2_ok    %s\n","FIXME: Undocumented");
336 //      printf( "  -gl_test1       %s\n","FIXME: Undocumented");
337         printf( "  -gl_test2       %s\n","FIXME: Undocumented");
338         printf( "  -gl_vidmem      %s\n","FIXME: Undocumented");
339 #ifdef OGL_RUNTIME_LOAD
340         printf( "  -gl_library <l> %s\n","use alternate opengl library");
341 #endif
342 #ifdef WGL_VIDEO
343         printf("  -gl_refresh <r> %s\n", "set refresh rate (in fullscreen mode)");
344 #endif
345 #endif
346 #ifdef SDL_VIDEO
347         printf( "  -nosdlvidmodecheck %s\n", "Some X servers don't like checking vidmode first, so just switch");
348         printf( "  -hwsurface      %s\n","FIXME: Undocumented");
349 #endif
350 #ifdef NETWORK
351         printf("  -udp            %s\n", "Specify options for udp/ip:");
352         printf("    @<shift>      %s\n", "  shift udp port base offset");
353         printf("    =<HOST_LIST>  %s\n", "  broadcast both local and to HOST_LIST");
354         printf("    +<HOST_LIST>  %s\n", "  broadcast only to HOST_LIST");
355         printf("                  %s\n", "   HOSTS can be any IP or hostname")
356                 ;
357         printf("                  %s\n", "   HOSTS can also be in the form of <address>:<shift>");
358         printf("                  %s\n", "   separate multiple HOSTS with a ,");
359 #endif
360 #ifdef __unix__
361         printf( "  -serialdevice <s> %s\n", "Set serial/modem device to <s>");
362         printf( "  -serialread <r> %s\n", "Set serial/modem to read from <r>");
363 #endif
364         printf( "\n Help:\n\n");
365         printf( "  -help, -h, -?, ? %s\n", "View this help screen");
366         printf( "\n");
367 }
368
369 void do_joystick_init()
370 {
371
372         if (!FindArg( "-nojoystick" ))  {
373                 con_printf(CON_VERBOSE, "\n%s", TXT_VERBOSE_6);
374                 joy_init();
375                 if ( FindArg( "-joyslow" ))     {
376                         con_printf(CON_VERBOSE, "\n%s", TXT_VERBOSE_7);
377                         joy_set_slow_reading(JOY_SLOW_READINGS);
378                 }
379                 if ( FindArg( "-joypolled" ))   {
380                         con_printf(CON_VERBOSE, "\n%s", TXT_VERBOSE_8);
381                         joy_set_slow_reading(JOY_POLLED_READINGS);
382                 }
383                 if ( FindArg( "-joybios" ))     {
384                         con_printf(CON_VERBOSE, "\n%s", TXT_VERBOSE_9);
385                         joy_set_slow_reading(JOY_BIOS_READINGS);
386                 }
387
388         //      Added from Descent v1.5 by John.  Adapted by Samir.
389         } else {
390                 con_printf(CON_VERBOSE, "\n%s", TXT_VERBOSE_10);
391         }
392 }
393
394 //set this to force game to run in low res
395 int disable_high_res=0;
396
397 void do_register_player(ubyte *title_pal)
398 {
399         strncpy( Players[Player_num].callsign, config_last_player.string, CALLSIGN_LEN );
400         if (strlen(Players[Player_num].callsign))
401                 return;
402
403         if (!Auto_demo)         {
404
405                 key_flush();
406
407                 //now, before we bring up the register player menu, we need to
408                 //do some stuff to make sure the palette is ok.  First, we need to
409                 //get our current palette into the 2d's array, so the remapping will
410                 //work.  Second, we need to remap the fonts.  Third, we need to fill
411                 //in part of the fade tables so the darkening of the menu edges works
412
413 #if 0
414                 memcpy(gr_palette,title_pal,sizeof(gr_palette));
415 #endif
416                 remap_fonts_and_menus(1);
417                 RegisterPlayer();               //get player's name
418         }
419
420 }
421
422 #define PROGNAME argv[0]
423
424 extern char Language[];
425
426 //can we do highres menus?
427 extern int MenuHiresAvailable;
428
429 int Inferno_verbose = 0;
430
431 //added on 11/18/98 by Victor Rachels to add -mission and -startgame
432 int start_net_immediately = 0;
433 //int start_with_mission = 0;
434 //char *start_with_mission_name;
435 //end this section addition
436
437 #define MENU_HIRES_MODE SM(640,480)
438
439 //      DESCENT II by Parallax Software
440 //              Descent Main
441
442 //extern ubyte gr_current_pal[];
443
444 #ifdef  EDITOR
445 int     Auto_exit = 0;
446 char    Auto_file[128] = "";
447 #endif
448
449 int main(int argc, char *argv[])
450 {
451         int t;
452         ubyte title_pal[768];
453
454         mem_init();
455         CON_Init();  // Initialise the console
456
457         error_init(NULL, NULL);
458         PHYSFSX_init(argc, argv);
459
460         if (FindArg("-debug"))
461                 cvar_setint( &con_threshold, CON_DEBUG );
462         else if (FindArg("-verbose"))
463                 cvar_setint( &con_threshold, CON_VERBOSE );
464
465         if (! cfile_init("descent2.hog"))
466                 if (! cfile_init("d2demo.hog"))
467                         Warning("Could not find a valid hog file (descent2.hog or d2demo.hog)\nPossible locations are:\n"
468 #ifdef __unix__
469                               "\t$HOME/.d2x\n"
470                               "\t" SHAREPATH "\n"
471 #else
472                                   "\tCurrent directory\n"
473 #endif
474                                   "Or use the -hogdir option to specify an alternate location.");
475         load_text();
476
477         //print out the banner title
478         con_printf(CON_NORMAL, "\nDESCENT 2 %s v%d.%d",VERSION_TYPE,Version_major,Version_minor);
479         #ifdef VERSION_NAME
480         con_printf(CON_NORMAL, "  %s", VERSION_NAME);
481         #endif
482         if (cfexist(MISSION_DIR "d2x.hog"))
483                 con_printf(CON_NORMAL, "  Vertigo Enhanced");
484
485         con_printf(CON_NORMAL, "  %s %s\n", __DATE__,__TIME__);
486         con_printf(CON_NORMAL, "%s\n%s\n",TXT_COPYRIGHT,TXT_TRADEMARK);
487         con_printf(CON_NORMAL, "This is a MODIFIED version of Descent 2. Copyright (c) 1999 Peter Hawkins\n");
488         con_printf(CON_NORMAL, "                                         Copyright (c) 2002 Bradley Bell\n");
489
490
491         if (FindArg( "-?" ) || FindArg( "-help" ) || FindArg( "?" ) || FindArg( "-h" ) ) {
492                 print_commandline_help();
493                 set_exit_message("");
494 #ifdef __MINGW32__
495                 exit(0);  /* mingw hangs on this return.  dunno why */
496 #endif
497                 return(0);
498         }
499
500         con_printf(CON_NORMAL, "\n");
501         con_printf(CON_NORMAL, TXT_HELP, PROGNAME);             //help message has %s for program name
502         con_printf(CON_NORMAL, "\n");
503
504         {
505                 char **i, **list;
506
507                 for (i = PHYSFS_getSearchPath(); *i != NULL; i++)
508                         con_printf(CON_VERBOSE, "PHYSFS: [%s] is in the search path.\n", *i);
509
510                 list = PHYSFS_getCdRomDirs();
511                 for (i = list; *i != NULL; i++)
512                         con_printf(CON_VERBOSE, "PHYSFS: cdrom dir [%s] is available.\n", *i);
513                 PHYSFS_freeList(list);
514
515                 list = PHYSFS_enumerateFiles("");
516                 for (i = list; *i != NULL; i++)
517                         con_printf(CON_DEBUG, "PHYSFS: * We've got [%s].\n", *i);
518                 PHYSFS_freeList(list);
519         }
520
521         //(re)added Mar 30, 2003 Micah Lieske - Allow use of 22K sound samples again.
522         if(FindArg("-sound22k"))
523         {
524                 digi_sample_rate = SAMPLE_RATE_22K;
525         }
526
527         if(FindArg("-sound11k"))
528         {
529                 digi_sample_rate = SAMPLE_RATE_11K;
530         }
531
532         arch_init_start();
533
534         arch_init();
535
536         //con_printf(CON_VERBOSE, "\n%s...", "Checking for Descent 2 CD-ROM");
537
538         if ((t = FindArg("-rearviewtime")))
539         {
540                 float f = atof(Args[t + 1]);
541                 Rear_view_leave_time = f * f1_0;
542         }
543         con_printf(CON_VERBOSE, "Rear_view_leave_time=0x%x (%f sec)\n", Rear_view_leave_time, Rear_view_leave_time / (float)f1_0);
544
545         //added/edited 8/18/98 by Victor Rachels to set maximum fps <= 100
546         if ((t = FindArg( "-maxfps" ))) {
547                 t=atoi(Args[t+1]);
548                 if (t > 0 && t <= MAX_FPS)
549                         maxfps=t;
550         }
551         //end addition - Victor Rachels
552
553 #ifdef SUPPORTS_NICEFPS
554         if (FindArg("-nicefps"))
555                 use_nice_fps = 1;
556         if (FindArg("-niceautomap"))
557                 nice_automap = 1;
558 #endif
559
560         if (FindArg("-renderstats"))
561                 gr_renderstats = 1;
562
563         if ( FindArg( "-autodemo" ))
564                 Auto_demo = 1;
565
566 #ifndef RELEASE
567         if ( FindArg( "-noscreens" ) )
568                 Skip_briefing_screens = 1;
569 #endif
570
571         if ((t=FindArg("-tmap"))){
572                 select_tmap(Args[t+1]);
573         }else
574                 select_tmap(NULL);
575
576         Lighting_on = 1;
577
578 //      if (init_graphics()) return 1;
579
580         #ifdef EDITOR
581         if (gr_check_mode(SM(800, 600)) != 0)
582         {
583                 con_printf(CON_NORMAL, "The editor will not be available, press any key to start game...\n" );
584                 Function_mode = FMODE_MENU;
585         }
586         #endif
587
588         if (!WVIDEO_running)
589                 con_printf(CON_DEBUG,"WVIDEO_running = %d\n",WVIDEO_running);
590
591         do_joystick_init();
592
593         if ((t = gr_init()) != 0) // doesn't do much
594                 Error(TXT_CANT_INIT_GFX, t);
595
596         con_printf (CON_VERBOSE, "%s", TXT_VERBOSE_1);
597         ReadConfigFile();
598
599         if (!VR_offscreen_buffer)       //if hasn't been initialied (by headset init)
600                 set_display_mode(Default_display_mode); //..then set default display mode
601
602         {
603                 int screen_width = 640;
604                 int screen_height = 480;
605
606                 FindResArg("", &screen_width, &screen_height);
607
608                 // added 3/24/99 by Owen Evans for screen res changing
609                 Game_screen_mode = SM(screen_width, screen_height);
610                 // end added -OE
611                 set_display_mode(Game_screen_mode);
612
613         }
614         {
615 // added/edited on 12/14/98 by Matt Mueller - override res in d1x.ini with command line args
616                 int i, argnum = INT_MAX, w, h;
617 // added on 9/30/98 by Matt Mueller for selectable automap modes - edited 11/21/99 whee, more fun with defines. - edited 03/31/02 to use new FindResArg.
618 #define SMODE(V,VV,VG) if ((i=FindResArg(#V, &w, &h)) && (i < argnum)) { argnum = i; VV = SM(w, h); VG = 0; }
619 #define SMODE_GR(V,VG) if ((i=FindArg("-" #V "_gameres"))){if (i<argnum) VG=1;}
620 #define SMODE_PRINT(V,VV,VG) if (VG) con_printf(CON_VERBOSE, #V " using game resolution ...\n"); else con_printf(CON_VERBOSE, #V " using %ix%i ...\n",SM_W(VV),SM_H(VV) );
621 // aren't #defines great? :)
622 // end addition/edit -MM
623 #define S_MODE(V,VV,VG) argnum = INT_MAX; SMODE(V, VV, VG); SMODE_GR(V, VG); SMODE_PRINT(V, VV, VG);
624
625                 S_MODE(automap,automap_mode,automap_use_game_res);
626 //              S_MODE(menu,menu_screen_mode,menu_use_game_res);
627          }
628 //end addition -MM
629
630         controls_init();
631
632         con_printf(CON_VERBOSE, "\n%s\n\n", TXT_INITIALIZING_GRAPHICS);
633         if (FindArg("-nofade"))
634                 grd_fades_disabled=1;
635
636         //determine whether we're using high-res menus & movies
637         if (FindArg("-nohires") || FindArg("-nohighres") || (gr_check_mode(MENU_HIRES_MODE) != 0) || disable_high_res)
638                 cvar_setint( &MovieHires, MenuHires = MenuHiresAvailable = 0 );
639         else
640                 //NOTE LINK TO ABOVE!
641                 MenuHires = MenuHiresAvailable = 1;
642
643         if (FindArg( "-lowresmovies" ))
644                 cvar_setint( &MovieHires, 0 );
645
646         con_printf(CON_VERBOSE, "Going into graphics mode...\n");
647         gr_set_mode(MovieHires.intval?SM(640,480):SM(320,200));
648
649         // Load the palette stuff. Returns non-zero if error.
650         con_printf(CON_DEBUG, "Initializing palette system...\n" );
651         gr_use_palette_table(DEFAULT_PALETTE );
652
653         con_printf(CON_DEBUG, "Initializing font system...\n" );
654         gamefont_init();        // must load after palette data loaded.
655
656         con_printf( CON_DEBUG, "Initializing movie libraries...\n" );
657         init_movies();          //init movie libraries
658
659         if ( FindArg( "-notitles" ) )
660                 songs_play_song( SONG_TITLE, 1);
661         else
662                 show_titles();
663
664         con_printf( CON_DEBUG, "\nShowing loading screen..." );
665         show_loading_screen(title_pal); // title_pal is needed (see below)
666
667         CON_InitGFX(SWIDTH, SHEIGHT / 2);
668
669         con_printf( CON_DEBUG , "\nDoing bm_init..." );
670         #ifdef EDITOR
671         if (!bm_init_use_tbl())
672         #endif
673                 bm_init();
674
675         #ifdef EDITOR
676         if (FindArg("-hoarddata") != 0)
677         {
678                 save_hoard_data();
679                 exit(1);
680         }
681         #endif
682
683         //the bitmap loading code changes gr_palette, so restore it
684         memcpy(gr_palette,title_pal,sizeof(gr_palette));
685
686         if ( FindArg( "-norun" ) )
687                 return(0);
688
689         con_printf( CON_DEBUG, "\nInitializing 3d system..." );
690         g3_init();
691
692         con_printf( CON_DEBUG, "\nInitializing texture caching system..." );
693         texmerge_init( 10 );            // 10 cache bitmaps
694
695         con_printf( CON_DEBUG, "\nRunning game...\n" );
696         set_screen_mode(SCREEN_MENU);
697
698         init_game();
699
700         remap_fonts_and_menus(1);
701
702         if ((t = FindArg("-autoexec")))
703                 cmd_appendf("exec %s", Args[t+1]);
704         else if (cfexist("autoexec.cfg"))
705                 cmd_append("exec autoexec.cfg");
706
707         cmd_queue_process();
708
709         //      If built with editor, option to auto-load a level and quit game
710         //      to write certain data.
711         #ifdef  EDITOR
712         {       int t;
713         if ( (t = FindArg( "-autoload" )) ) {
714                 Auto_exit = 1;
715                 strcpy(Auto_file, Args[t+1]);
716         }
717                 
718         }
719
720         if (Auto_exit) {
721                 strcpy(Players[0].callsign, "dummy");
722         } else
723         #endif
724                 if (!strlen(Players[0].callsign))
725                         do_register_player(title_pal);
726
727         Game_mode = GM_GAME_OVER;
728
729         if (Auto_demo)  {
730                 newdemo_start_playback("descent.dem");
731                 if (Newdemo_state == ND_STATE_PLAYBACK )
732                         Function_mode = FMODE_GAME;
733         }
734
735         //do this here because the demo code can do a longjmp when trying to
736         //autostart a demo from the main menu, never having gone into the game
737         setjmp(LeaveGame);
738
739         remap_fonts_and_menus(1);
740
741         while (Function_mode != FMODE_EXIT)
742         {
743                 cmd_queue_process();
744
745                 switch( Function_mode ) {
746                 case FMODE_MENU:
747                         set_screen_mode(SCREEN_MENU);
748                         if ( Auto_demo )        {
749                                 newdemo_start_playback(NULL);           // Randomly pick a file
750                                 if (Newdemo_state != ND_STATE_PLAYBACK) 
751                                         Error("No demo files were found for autodemo mode!");
752                         } else {
753                                 #ifdef EDITOR
754                                 if (Auto_exit) {
755                                         strcpy((char *)&Level_names[0], Auto_file);
756                                         LoadLevel(1, 1);
757                                         Function_mode = FMODE_EXIT;
758                                         break;
759                                 }
760                                 #endif
761
762                                 check_joystick_calibration();
763                                 gr_palette_clear();             //I'm not sure why we need this, but we do
764                                 DoMenu();                                                                               
765                                 #ifdef EDITOR
766                                 if ( Function_mode == FMODE_EDITOR )    {
767                                         create_new_mine();
768                                         SetPlayerFromCurseg();
769                                         load_palette(NULL,1,0);
770                                 }
771                                 #endif
772                         }
773                         break;
774                 case FMODE_GAME:
775                         #ifdef EDITOR
776                                 keyd_editor_mode = 0;
777                         #endif
778
779                         game();
780
781                         if ( Function_mode == FMODE_MENU )
782                                 songs_play_song( SONG_TITLE, 1 );
783                         break;
784                 #ifdef EDITOR
785                 case FMODE_EDITOR:
786                         keyd_editor_mode = 1;
787                         editor();
788 #ifdef __WATCOMC__
789                         _harderr( (void*)descent_critical_error_handler );              // Reinstall game error handler
790 #endif
791                         if ( Function_mode == FMODE_GAME ) {
792                                 Game_mode = GM_EDITOR;
793                                 editor_reset_stuff_on_level();
794                                 N_players = 1;
795                         }
796                         break;
797                 #endif
798                 default:
799                         Error("Invalid function mode %d",Function_mode);
800                 }
801         }
802
803         WriteConfigFile();
804
805         if (!FindArg( "-notitles" ))
806                 show_order_form();
807
808         #ifndef NDEBUG
809         if ( FindArg( "-showmeminfo" ) )
810                 show_mem_info = 1;              // Make memory statistics show
811         #endif
812
813         return(0);              //presumably successful exit
814 }
815
816
817 void check_joystick_calibration()       {
818         int x1, y1, x2, y2, c;
819         fix t1;
820
821         if ( !Config_control_joystick.intval )
822                 return;
823
824         joy_get_pos( &x1, &y1 );
825
826         t1 = timer_get_fixed_seconds();
827         while( timer_get_fixed_seconds() < t1 + F1_0/100 )
828                 ;
829
830         joy_get_pos( &x2, &y2 );
831
832         // If joystick hasn't moved...
833         if ( (abs(x2-x1)<30) &&  (abs(y2-y1)<30) )      {
834                 if ( (abs(x1)>30) || (abs(x2)>30) ||  (abs(y1)>30) || (abs(y2)>30) )    {
835                         c = nm_messagebox( NULL, 2, TXT_CALIBRATE, TXT_SKIP, TXT_JOYSTICK_NOT_CEN );
836                         if ( c==0 )     {
837                                 joydefs_calibrate();
838                         }
839                 }
840         }
841
842 }
843
844 void quit_request()
845 {
846 #ifdef NETWORK
847 //      void network_abort_game();
848 //      if(Network_status)
849 //              network_abort_game();
850 #endif
851         exit(0);
852 }