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