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