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