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