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