]> icculus.org git repositories - btb/d2x.git/blob - main/inferno.c
command queueing
[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         mem_init();
470         CON_Init();  // Initialise the console
471
472         error_init(NULL, NULL);
473         PHYSFSX_init(argc, argv);
474
475         if ((t = FindArg("-autoexec")))
476                 cmd_parsef("exec %s", Args[t+1]);
477         else {
478                 cmd_append("exec autoexec.cfg");
479                 cmd_parse("exec autoexec.cfg");
480         }
481
482         if (FindArg("-debug"))
483                 con_threshold.value = (float)CON_DEBUG;
484         else if (FindArg("-verbose"))
485                 con_threshold.value = (float)CON_VERBOSE;
486
487         if (! cfile_init("descent2.hog"))
488                 if (! cfile_init("d2demo.hog"))
489                         Warning("Could not find a valid hog file (descent2.hog or d2demo.hog)\nPossible locations are:\n"
490 #ifdef __unix__
491                               "\t$HOME/.d2x\n"
492                               "\t" SHAREPATH "\n"
493 #else
494                                   "\tCurrent directory\n"
495 #endif
496                                   "Or use the -hogdir option to specify an alternate location.");
497         load_text();
498
499         //print out the banner title
500         con_printf(CON_NORMAL, "\nDESCENT 2 %s v%d.%d",VERSION_TYPE,Version_major,Version_minor);
501         #ifdef VERSION_NAME
502         con_printf(CON_NORMAL, "  %s", VERSION_NAME);
503         #endif
504         if (cfexist(MISSION_DIR "d2x.hog"))
505                 con_printf(CON_NORMAL, "  Vertigo Enhanced");
506
507         con_printf(CON_NORMAL, "  %s %s\n", __DATE__,__TIME__);
508         con_printf(CON_NORMAL, "%s\n%s\n",TXT_COPYRIGHT,TXT_TRADEMARK);
509         con_printf(CON_NORMAL, "This is a MODIFIED version of Descent 2. Copyright (c) 1999 Peter Hawkins\n");
510         con_printf(CON_NORMAL, "                                         Copyright (c) 2002 Bradley Bell\n");
511
512
513         if (FindArg( "-?" ) || FindArg( "-help" ) || FindArg( "?" ) || FindArg( "-h" ) ) {
514                 print_commandline_help();
515                 set_exit_message("");
516 #ifdef __MINGW32__
517                 exit(0);  /* mingw hangs on this return.  dunno why */
518 #endif
519                 return(0);
520         }
521
522         con_printf(CON_NORMAL, "\n");
523         con_printf(CON_NORMAL, TXT_HELP, PROGNAME);             //help message has %s for program name
524         con_printf(CON_NORMAL, "\n");
525
526         {
527                 char **i, **list;
528
529                 for (i = PHYSFS_getSearchPath(); *i != NULL; i++)
530                         con_printf(CON_VERBOSE, "PHYSFS: [%s] is in the search path.\n", *i);
531
532                 list = PHYSFS_getCdRomDirs();
533                 for (i = list; *i != NULL; i++)
534                         con_printf(CON_VERBOSE, "PHYSFS: cdrom dir [%s] is available.\n", *i);
535                 PHYSFS_freeList(list);
536
537                 list = PHYSFS_enumerateFiles("");
538                 for (i = list; *i != NULL; i++)
539                         con_printf(CON_DEBUG, "PHYSFS: * We've got [%s].\n", *i);
540                 PHYSFS_freeList(list);
541         }
542
543         //(re)added Mar 30, 2003 Micah Lieske - Allow use of 22K sound samples again.
544         if(FindArg("-sound22k"))
545         {
546                 digi_sample_rate = SAMPLE_RATE_22K;
547         }
548
549         if(FindArg("-sound11k"))
550         {
551                 digi_sample_rate = SAMPLE_RATE_11K;
552         }
553
554         arch_init_start();
555
556         arch_init();
557
558         //con_printf(CON_VERBOSE, "\n%s...", "Checking for Descent 2 CD-ROM");
559
560         if ((t = FindArg("-rearviewtime")))
561         {
562                 float f = atof(Args[t + 1]);
563                 Rear_view_leave_time = f * f1_0;
564         }
565         con_printf(CON_VERBOSE, "Rear_view_leave_time=0x%x (%f sec)\n", Rear_view_leave_time, Rear_view_leave_time / (float)f1_0);
566
567         //added/edited 8/18/98 by Victor Rachels to set maximum fps <= 100
568         if ((t = FindArg( "-maxfps" ))) {
569                 t=atoi(Args[t+1]);
570                 if (t > 0 && t <= MAX_FPS)
571                         maxfps=t;
572         }
573         //end addition - Victor Rachels
574
575 #ifdef SUPPORTS_NICEFPS
576         if (FindArg("-nicefps"))
577                 use_nice_fps = 1;
578         if (FindArg("-niceautomap"))
579                 nice_automap = 1;
580 #endif
581
582         if (FindArg("-renderstats"))
583                 gr_renderstats = 1;
584
585         if ( FindArg( "-autodemo" ))
586                 Auto_demo = 1;
587
588 #ifndef RELEASE
589         if ( FindArg( "-noscreens" ) )
590                 Skip_briefing_screens = 1;
591 #endif
592
593         if ((t=FindArg("-tmap"))){
594                 select_tmap(Args[t+1]);
595         }else
596                 select_tmap(NULL);
597
598         Lighting_on = 1;
599
600 //      if (init_graphics()) return 1;
601
602         #ifdef EDITOR
603         if (gr_check_mode(SM(800, 600)) != 0)
604         {
605                 con_printf(CON_NORMAL, "The editor will not be available, press any key to start game...\n" );
606                 Function_mode = FMODE_MENU;
607         }
608         #endif
609
610         if (!WVIDEO_running)
611                 con_printf(CON_DEBUG,"WVIDEO_running = %d\n",WVIDEO_running);
612
613         con_printf (CON_VERBOSE, "%s", TXT_VERBOSE_1);
614         ReadConfigFile();
615
616         do_joystick_init();
617
618         if (!VR_offscreen_buffer)       //if hasn't been initialied (by headset init)
619                 set_display_mode(0);            //..then set default display mode
620
621         {
622                 int screen_width = 640;
623                 int screen_height = 480;
624                 int vr_mode = VR_NONE;
625                 int screen_flags = VRF_USE_PAGING;
626
627                 if (FindResArg("", &screen_width, &screen_height))
628                 {
629                         /* stuff below mirrors values from display_mode_info in
630                          * menu.c which is used by set_display_mode. In fact,
631                          * set_display_mode should probably be rewritten to allow
632                          * arbitrary resolutions, and then we get rid of this
633                          * stuff here.
634                          */
635                         switch (SM(screen_width, screen_height))
636                         {
637                         case SM(320, 200):
638                         case SM(640, 480):
639                                 screen_flags = VRF_ALLOW_COCKPIT + VRF_COMPATIBLE_MENUS;
640                                 break;
641                         case SM(320, 400):
642                                 screen_flags = VRF_USE_PAGING;
643                                 break;
644                         case SM(640, 400):
645                         case SM(800, 600):
646                         case SM(1024, 768):
647                         case SM(1280, 1024):
648                                 screen_flags = VRF_COMPATIBLE_MENUS;
649                                 break;
650                         }
651
652                         con_printf(CON_VERBOSE, "Using %ix%i ...\n", screen_width, screen_height);
653                 }
654
655 // added ifdef on 9/30/98 by Matt Mueller to fix high res in linux
656 #ifdef __MSDOS__
657                 if (FindArg("-nodoublebuffer"))
658 #endif
659 // end addition -MM
660                 {
661                         con_printf(CON_VERBOSE, "Double-buffering disabled...\n");
662                         screen_flags &= ~VRF_USE_PAGING;
663                 }
664
665                 // added 3/24/99 by Owen Evans for screen res changing
666                 Game_screen_mode = SM(screen_width, screen_height);
667                 // end added -OE
668                 game_init_render_buffers(Game_screen_mode, screen_width, screen_height, vr_mode, screen_flags);
669
670         }
671         {
672 // added/edited on 12/14/98 by Matt Mueller - override res in d1x.ini with command line args
673                 int i, argnum = INT_MAX, w, h;
674 // 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.
675 #define SMODE(V,VV,VG) if ((i=FindResArg(#V, &w, &h)) && (i < argnum)) { argnum = i; VV = SM(w, h); VG = 0; }
676 #define SMODE_GR(V,VG) if ((i=FindArg("-" #V "_gameres"))){if (i<argnum) VG=1;}
677 #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) );
678 // aren't #defines great? :)
679 // end addition/edit -MM
680 #define S_MODE(V,VV,VG) argnum = INT_MAX; SMODE(V, VV, VG); SMODE_GR(V, VG); SMODE_PRINT(V, VV, VG);
681
682                 S_MODE(automap,automap_mode,automap_use_game_res);
683 //              S_MODE(menu,menu_screen_mode,menu_use_game_res);
684          }
685 //end addition -MM
686
687         i = FindArg( "-xcontrol" );
688         if ( i > 0 )    {
689                 kconfig_init_external_controls( strtol(Args[i+1], NULL, 0), strtol(Args[i+2], NULL, 0) );
690         }
691
692         con_printf(CON_VERBOSE, "\n%s\n\n", TXT_INITIALIZING_GRAPHICS);
693         if (FindArg("-nofade"))
694                 grd_fades_disabled=1;
695
696         //determine whether we're using high-res menus & movies
697         if (FindArg("-nohires") || FindArg("-nohighres") || (gr_check_mode(MENU_HIRES_MODE) != 0) || disable_high_res)
698                 MovieHires = MenuHires = MenuHiresAvailable = 0;
699         else
700                 //NOTE LINK TO ABOVE!
701                 MenuHires = MenuHiresAvailable = 1;
702
703         if (FindArg( "-lowresmovies" ))
704                 MovieHires = 0;
705
706         if ((t=gr_init())!=0)                           //doesn't do much
707                 Error(TXT_CANT_INIT_GFX,t);
708
709         // Load the palette stuff. Returns non-zero if error.
710         con_printf(CON_DEBUG, "Initializing palette system...\n" );
711         gr_use_palette_table(DEFAULT_PALETTE );
712
713         con_printf(CON_DEBUG, "Initializing font system...\n" );
714         gamefont_init();        // must load after palette data loaded.
715
716         con_printf( CON_DEBUG, "Initializing movie libraries...\n" );
717         init_movies();          //init movie libraries
718
719 #if 0
720         con_printf(CON_VERBOSE, "Going into graphics mode...\n");
721         gr_set_mode(MovieHires?SM(640,480):SM(320,200));
722 #endif
723
724         if ( FindArg( "-notitles" ) )
725                 songs_play_song( SONG_TITLE, 1);
726         else
727                 show_titles();
728
729         con_printf( CON_DEBUG, "\nShowing loading screen..." );
730         show_loading_screen(title_pal); // title_pal is needed (see below)
731
732         CON_InitGFX(SWIDTH, SHEIGHT / 2);
733
734         con_printf( CON_DEBUG , "\nDoing bm_init..." );
735         #ifdef EDITOR
736         if (!bm_init_use_tbl())
737         #endif
738                 bm_init();
739
740         #ifdef EDITOR
741         if (FindArg("-hoarddata") != 0)
742         {
743                 save_hoard_data();
744                 exit(1);
745         }
746         #endif
747
748         //the bitmap loading code changes gr_palette, so restore it
749         memcpy(gr_palette,title_pal,sizeof(gr_palette));
750
751         if ( FindArg( "-norun" ) )
752                 return(0);
753
754         con_printf( CON_DEBUG, "\nInitializing 3d system..." );
755         g3_init();
756
757         con_printf( CON_DEBUG, "\nInitializing texture caching system..." );
758         texmerge_init( 10 );            // 10 cache bitmaps
759
760         con_printf( CON_DEBUG, "\nRunning game...\n" );
761         set_screen_mode(SCREEN_MENU);
762
763         init_game();
764
765         //      If built with editor, option to auto-load a level and quit game
766         //      to write certain data.
767         #ifdef  EDITOR
768         {       int t;
769         if ( (t = FindArg( "-autoload" )) ) {
770                 Auto_exit = 1;
771                 strcpy(Auto_file, Args[t+1]);
772         }
773                 
774         }
775
776         if (Auto_exit) {
777                 strcpy(Players[0].callsign, "dummy");
778         } else
779         #endif
780                 do_register_player(title_pal);
781
782         gr_palette_fade_out( title_pal, 32, 0 );
783
784         Game_mode = GM_GAME_OVER;
785
786         if (Auto_demo)  {
787                 newdemo_start_playback("descent.dem");          
788                 if (Newdemo_state == ND_STATE_PLAYBACK )
789                         Function_mode = FMODE_GAME;
790         }
791
792         //do this here because the demo code can do a longjmp when trying to
793         //autostart a demo from the main menu, never having gone into the game
794         setjmp(LeaveGame);
795
796         while (Function_mode != FMODE_EXIT)
797         {
798                 switch( Function_mode ) {
799                 case FMODE_MENU:
800                         set_screen_mode(SCREEN_MENU);
801                         if ( Auto_demo )        {
802                                 newdemo_start_playback(NULL);           // Randomly pick a file
803                                 if (Newdemo_state != ND_STATE_PLAYBACK) 
804                                         Error("No demo files were found for autodemo mode!");
805                         } else {
806                                 #ifdef EDITOR
807                                 if (Auto_exit) {
808                                         strcpy((char *)&Level_names[0], Auto_file);
809                                         LoadLevel(1, 1);
810                                         Function_mode = FMODE_EXIT;
811                                         break;
812                                 }
813                                 #endif
814
815                                 check_joystick_calibration();
816                                 gr_palette_clear();             //I'm not sure why we need this, but we do
817                                 DoMenu();                                                                               
818                                 #ifdef EDITOR
819                                 if ( Function_mode == FMODE_EDITOR )    {
820                                         create_new_mine();
821                                         SetPlayerFromCurseg();
822                                         load_palette(NULL,1,0);
823                                 }
824                                 #endif
825                         }
826                         break;
827                 case FMODE_GAME:
828                         #ifdef EDITOR
829                                 keyd_editor_mode = 0;
830                         #endif
831
832 #ifdef SDL_INPUT
833                         /* keep the mouse from wandering in SDL */
834                         if (FindArg("-grabmouse"))
835                             SDL_WM_GrabInput(SDL_GRAB_ON);
836 #endif
837
838                         game();
839
840 #ifdef SDL_INPUT
841                         /* give control back to the WM */
842                         if (FindArg("-grabmouse"))
843                             SDL_WM_GrabInput(SDL_GRAB_OFF);
844 #endif
845
846                         if ( Function_mode == FMODE_MENU )
847                                 songs_play_song( SONG_TITLE, 1 );
848                         break;
849                 #ifdef EDITOR
850                 case FMODE_EDITOR:
851                         keyd_editor_mode = 1;
852                         editor();
853 #ifdef __WATCOMC__
854                         _harderr( (void*)descent_critical_error_handler );              // Reinstall game error handler
855 #endif
856                         if ( Function_mode == FMODE_GAME ) {
857                                 Game_mode = GM_EDITOR;
858                                 editor_reset_stuff_on_level();
859                                 N_players = 1;
860                         }
861                         break;
862                 #endif
863                 default:
864                         Error("Invalid function mode %d",Function_mode);
865                 }
866         }
867
868         WriteConfigFile();
869
870         if (!FindArg( "-notitles" ))
871                 show_order_form();
872
873         #ifndef NDEBUG
874         if ( FindArg( "-showmeminfo" ) )
875                 show_mem_info = 1;              // Make memory statistics show
876         #endif
877
878         return(0);              //presumably successful exit
879 }
880
881
882 void check_joystick_calibration()       {
883         int x1, y1, x2, y2, c;
884         fix t1;
885
886         if ( (Config_control_type!=CONTROL_JOYSTICK) &&
887                   (Config_control_type!=CONTROL_FLIGHTSTICK_PRO) &&
888                   (Config_control_type!=CONTROL_THRUSTMASTER_FCS) &&
889                   (Config_control_type!=CONTROL_GRAVIS_GAMEPAD)
890                 ) return;
891
892         joy_get_pos( &x1, &y1 );
893
894         t1 = timer_get_fixed_seconds();
895         while( timer_get_fixed_seconds() < t1 + F1_0/100 )
896                 ;
897
898         joy_get_pos( &x2, &y2 );
899
900         // If joystick hasn't moved...
901         if ( (abs(x2-x1)<30) &&  (abs(y2-y1)<30) )      {
902                 if ( (abs(x1)>30) || (abs(x2)>30) ||  (abs(y1)>30) || (abs(y2)>30) )    {
903                         c = nm_messagebox( NULL, 2, TXT_CALIBRATE, TXT_SKIP, TXT_JOYSTICK_NOT_CEN );
904                         if ( c==0 )     {
905                                 joydefs_calibrate();
906                         }
907                 }
908         }
909
910 }
911
912 void quit_request()
913 {
914 #ifdef NETWORK
915 //      void network_abort_game();
916 //      if(Network_status)
917 //              network_abort_game();
918 #endif
919         exit(0);
920 }