]> icculus.org git repositories - btb/d2x.git/blob - main/inferno.c
remove warning created by the fix for the cfgets issue of 2004-08-29
[btb/d2x.git] / main / inferno.c
1 /* $Id: inferno.c,v 1.91 2004-10-23 20:53:08 schaffner 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 "pstypes.h"
45 #include "strutil.h"
46 #include "console.h"
47 #include "pa_enabl.h"       //$$POLY_ACC
48 #include "gr.h"
49 #include "fix.h"
50 #include "vecmat.h"
51 #include "mono.h"
52 #include "key.h"
53 #include "timer.h"
54 #include "3d.h"
55 #include "bm.h"
56 #include "inferno.h"
57 #include "error.h"
58 #include "game.h"
59 #include "segment.h"            //for Side_to_verts
60 #include "u_mem.h"
61 #include "segpoint.h"
62 #include "screens.h"
63 #include "texmap.h"
64 #include "texmerge.h"
65 #include "menu.h"
66 #include "wall.h"
67 #include "polyobj.h"
68 #include "effects.h"
69 #include "digi.h"
70 #include "iff.h"
71 #include "pcx.h"
72 #include "palette.h"
73 #include "args.h"
74 #include "sounds.h"
75 #include "titles.h"
76 #include "player.h"
77 #include "text.h"
78 #include "newdemo.h"
79 #ifdef NETWORK
80 #include "network.h"
81 #include "modem.h"
82 #endif
83 #include "gamefont.h"
84 #include "kconfig.h"
85 #include "mouse.h"
86 #include "joy.h"
87 #include "newmenu.h"
88 #include "desc_id.h"
89 #include "config.h"
90 #include "joydefs.h"
91 #include "multi.h"
92 #include "songs.h"
93 #include "cfile.h"
94 #include "gameseq.h"
95 #include "gamepal.h"
96 #include "mission.h"
97 #include "movie.h"
98 #include "d_io.h"
99
100 // #  include "3dfx_des.h"
101
102 //added on 9/30/98 by Matt Mueller for selectable automap modes
103 #include "automap.h"
104 //end addition -MM
105
106 #include "../texmap/scanline.h" //for select_tmap -MM
107
108 #if defined(POLY_ACC)
109 #include "poly_acc.h"
110 extern int Current_display_mode;        //$$ there's got to be a better way than hacking this.
111 #endif
112
113 #ifdef EDITOR
114 #include "editor/editor.h"
115 #include "editor/kdefs.h"
116 #include "ui.h"
117 #endif
118
119 #ifndef __MSDOS__
120 #include <SDL.h>
121 #endif
122
123 #include "vers_id.h"
124
125 void mem_init(void);
126 void arch_init(void);
127 void arch_init_start(void);
128
129 //Current version number
130
131 ubyte Version_major = 1;                //FULL VERSION
132 ubyte Version_minor = 2;
133
134 //static const char desc_id_checksum_str[] = DESC_ID_CHKSUM_TAG "0000"; // 4-byte checksum
135 char desc_id_exit_num = 0;
136
137 int Function_mode=FMODE_MENU;           //game or editor?
138 int Screen_mode=-1;                                     //game screen or editor screen?
139
140 //--unused-- grs_bitmap Inferno_bitmap_title;
141
142 int WVIDEO_running=0;           //debugger can set to 1 if running
143
144 #ifdef EDITOR
145 int Inferno_is_800x600_available = 0;
146 #endif
147
148 //--unused-- int Cyberman_installed=0;                  // SWIFT device present
149 ubyte CybermouseActive=0;
150
151 int __far descent_critical_error_handler( unsigned deverr, unsigned errcode, unsigned __far * devhdr );
152
153 void check_joystick_calibration(void);
154
155 void show_order_form(void);
156
157 //--------------------------------------------------------------------------
158
159 extern int piggy_low_memory;
160
161
162 int descent_critical_error = 0;
163 unsigned descent_critical_deverror = 0;
164 unsigned descent_critical_errcode = 0;
165
166 extern int Network_allow_socket_changes;
167
168 extern void vfx_set_palette_sub(ubyte *);
169
170 extern int VR_low_res;
171
172 extern int Config_vr_type;
173 extern int Config_vr_resolution;
174 extern int Config_vr_tracking;
175
176 #define LINE_LEN        100
177
178 //read help from a file & print to screen
179 void print_commandline_help()
180 {
181         CFILE *ifile;
182         int have_binary=0;
183         char line[LINE_LEN];
184
185         ifile = cfopen("help.tex","rb");
186         if (!ifile) {
187                 ifile = cfopen("help.txb","rb");
188                 if (!ifile)
189                         Warning("Cannot load help text file.");
190                 have_binary = 1;
191         }
192
193         if (ifile)
194         {
195         char *end;
196         
197                 while ((end = cfgets(line,LINE_LEN,ifile))) {
198
199             // This is the only use of cfgets that needs the CR
200             *end++ = '\n';
201                         if (have_binary)
202                                 decode_text_line (line);
203
204                         if (line[0] == ';')
205                                 continue;               //don't show comments
206
207                         printf("%s",line);
208
209                 }
210
211                 cfclose(ifile);
212
213         }
214
215 //      printf( " Diagnostic:\n\n");
216 //      printf( "  -emul           %s\n", "Certain video cards need this option in order to run game");
217 //      printf( "  -ddemul         %s\n", "If -emul doesn't work, use this option");
218 //      printf( "\n");
219 #ifdef EDITOR
220         printf( " Editor Options:\n\n");
221         printf( "  -autoload <file>%s\n", "Autoload a level in the editor");
222         printf( "  -hoarddata      %s\n","FIXME: Undocumented");
223 //      printf( "  -nobm           %s\n","FIXME: Undocumented");
224         printf( "\n");
225 #endif
226         printf( " D2X Options:\n\n");
227         printf( "  -noredundancy   %s\n", "Do not send messages when picking up redundant items in multi");
228         printf( "  -shortpackets   %s\n", "Set shortpackets to default as on");
229 #ifdef OGL // currently only does anything on ogl build, so don't advertise othewise.
230         printf("  -renderstats    %s\n", "Enable renderstats info by default");
231 #endif
232         printf( "  -maxfps <n>     %s\n", "Set maximum framerate (1-100)");
233         printf( "  -notitles       %s\n", "Do not show titlescreens on startup");
234         printf( "  -hogdir <dir>   %s\n", "set shared data directory to <dir>");
235 #ifdef __unix__
236         printf( "  -nohogdir       %s\n", "don't try to use shared data directory");
237         printf( "  -userdir <dir>  %s\n", "set user dir to <dir> instead of $HOME/.d2x");
238 #endif
239         printf( "  -ini <file>     %s\n", "option file (alternate to command line), defaults to d2x.ini");
240         printf( "  -autodemo       %s\n", "Start in demo mode");
241         printf( "  -bigpig         %s\n","FIXME: Undocumented");
242         printf( "  -bspgen         %s\n","FIXME: Undocumented");
243 //      printf( "  -cdproxy        %s\n","FIXME: Undocumented");
244 #ifndef NDEBUG
245         printf( "  -checktime      %s\n","FIXME: Undocumented");
246         printf( "  -showmeminfo    %s\n","FIXME: Undocumented");
247 #endif
248 //      printf( "  -codereadonly   %s\n","FIXME: Undocumented");
249 //      printf( "  -cyberimpact    %s\n","FIXME: Undocumented");
250         printf( "  -debug          %s\n","Enable very verbose output");
251 //      printf( "  -debugmode      %s\n","FIXME: Undocumented");
252 //      printf( "  -disallowgfx    %s\n","FIXME: Undocumented");
253 //      printf( "  -disallowreboot %s\n","FIXME: Undocumented");
254 //      printf( "  -dynamicsockets %s\n","FIXME: Undocumented");
255 //      printf( "  -forcegfx       %s\n","FIXME: Undocumented");
256 #ifdef SDL_INPUT
257         printf( "  -grabmouse      %s\n","Keeps the mouse from wandering out of the window");
258 #endif
259 //      printf( "  -hw_3dacc       %s\n","FIXME: Undocumented");
260 #ifndef RELEASE
261         printf( "  -invulnerability %s\n","Make yourself invulnerable");
262 #endif
263         printf( "  -ipxnetwork <num> %s\n","Use IPX network number <num>");
264         printf( "  -jasen          %s\n","FIXME: Undocumented");
265         printf( "  -joyslow        %s\n","FIXME: Undocumented");
266 #ifdef NETWORK
267         printf( "  -kali           %s\n","use Kali for networking");
268 #endif
269 //      printf( "  -logfile        %s\n","FIXME: Undocumented");
270         printf( "  -lowresmovies   %s\n","Play low resolution movies if available (for slow machines)");
271 #if defined(EDITOR) || !defined(MACDATA)
272         printf( "  -macdata        %s\n","Read (and, for editor, write) mac data files (swap colors)");
273 #endif
274 //      printf( "  -memdbg         %s\n","FIXME: Undocumented");
275 //      printf( "  -monodebug      %s\n","FIXME: Undocumented");
276         printf( "  -nocdrom        %s\n","FIXME: Undocumented");
277 #ifdef __DJGPP__
278         printf( "  -nocyberman     %s\n","FIXME: Undocumented");
279 #endif
280 #ifndef NDEBUG
281         printf( "  -nofade         %s\n","Disable fades");
282 #endif
283 #ifdef NETWORK
284         printf( "  -nomatrixcheat  %s\n","FIXME: Undocumented");
285         printf( "  -norankings     %s\n","Disable multiplayer ranking system");
286         printf( "  -packets <num>  %s\n","Specifies the number of packets per second\n");
287 //      printf( "  -showaddress    %s\n","FIXME: Undocumented");
288         printf( "  -socket         %s\n","FIXME: Undocumented");
289 #endif
290 #if !defined(MACINTOSH) && !defined(WINDOWS)
291         printf( "  -nomixer        %s\n","Don't crank music volume");
292 //      printf( "  -superhires     %s\n","Allow higher-resolution modes");
293 #endif
294 //      printf( "  -nomodex        %s\n","FIXME: Undocumented");
295 #ifndef RELEASE
296         printf( "  -nomovies       %s\n","Don't play movies");
297         printf( "  -noscreens      %s\n","Skip briefing screens");
298 #endif
299 #if !defined(SHAREWARE) || ( defined(SHAREWARE) && defined(APPLE_DEMO) )
300         printf( "  -noredbook      %s\n","Disable redbook audio");
301 #endif
302         printf( "  -norun          %s\n","Bail out after initialization");
303 //      printf( "  -ordinaljoy     %s\n","FIXME: Undocumented");
304 //      printf( "  -rtscts         %s\n","Same as -ctsrts");
305 //      printf( "  -semiwin        %s\n","Use non-fullscreen mode");
306 //      printf( "  -specialdevice  %s\n","FIXME: Undocumented");
307 #ifdef TACTILE
308         printf( "  -stickmag       %s\n","FIXME: Undocumented");
309 #endif
310 //      printf( "  -stopwatch      %s\n","FIXME: Undocumented");
311         printf( "  -subtitles      %s\n","Turn on movie subtitles (English-only)");
312 //      printf( "  -sysram         %s\n","FIXME: Undocumented");
313         printf( "  -text <file>    %s\n","Specify alternate .tex file");
314 //      printf( "  -tsengdebug1    %s\n","FIXME: Undocumented");
315 //      printf( "  -tsengdebug2    %s\n","FIXME: Undocumented");
316 //      printf( "  -tsengdebug3    %s\n","FIXME: Undocumented");
317 //      printf( "  -vidram         %s\n","FIXME: Undocumented");
318         printf( "  -xcontrol       %s\n","FIXME: Undocumented");
319         printf( "  -xname          %s\n","FIXME: Undocumented");
320         printf( "  -xver           %s\n","FIXME: Undocumented");
321         printf( "  -tmap <t>       %s\n","select texmapper to use (c,fp,i386,pent,ppro)");
322 #ifdef __MSDOS__
323         printf( "  -<X>x<Y>        %s\n", "Change screen resolution. Options:");
324         printf( "                     320x100;320x200;320x240;320x400;640x400;640x480;800x600;1024x768\n");
325 #else
326         printf( "  -<X>x<Y>        %s\n", "Change screen resolution to <X> by <Y>");
327 #endif
328         printf("  -niceautomap    %s\n", "Free cpu while doing automap");
329         printf( "  -automap<X>x<Y> %s\n","Set automap resolution to <X> by <Y>");
330         printf( "  -automap_gameres %s\n","Set automap to use the same resolution as in game");
331 //      printf( "  -menu<X>x<Y>    %s\n","Set menu resolution to <X> by <Y>");
332 //      printf( "  -menu_gameres   %s\n","Set menus to use the same resolution as in game");
333         printf("  -rearviewtime t %s\n", "time holding rearview key to use toggle mode (default 0.0625 seconds)");
334         printf( "\n");
335
336         printf( "D2X System Options:\n\n");
337 #ifdef __MSDOS__
338         printf("  -ihaveabrokenmouse %s\n", "try to make mouse work if it is not currently");
339         printf( "  -joy209         %s\n", "Use alternate port 209 for joystick");
340 #endif
341 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
342         printf( "  -fullscreen     %s\n", "Use fullscreen mode if available");
343 #endif
344 #ifdef OGL
345         printf( "  -gl_texmagfilt <f> %s\n","set GL_TEXTURE_MAG_FILTER (see readme.d1x)");
346         printf( "  -gl_texminfilt <f> %s\n","set GL_TEXTURE_MIN_FILTER (see readme.d1x)");
347         printf("  -gl_mipmap      %s\n", "set gl texture filters to \"standard\" (bilinear) mipmapping");
348         printf("  -gl_trilinear   %s\n", "set gl texture filters to trilinear mipmapping");
349         printf( "  -gl_simple      %s\n","set gl texture filters to gl_nearest for \"original\" look. (default)");
350         printf("  -gl_anisotropy <f> %s\n", "set maximum degree of anisotropy to <f>");
351         printf( "  -gl_alttexmerge %s\n","use new texmerge, usually uses less ram (default)");
352         printf( "  -gl_stdtexmerge %s\n","use old texmerge, uses more ram, but _might_ be a bit faster");
353 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
354         printf( "  -gl_voodoo      %s\n","force fullscreen mode only");
355 #endif
356         printf( "  -gl_16bittextures %s\n","attempt to use 16bit textures");
357         printf("  -gl_16bpp       %s\n", "attempt to use 16bit screen mode");
358         printf( "  -gl_reticle <r> %s\n","use OGL reticle 0=never 1=above 320x* 2=always");
359         printf( "  -gl_intensity4_ok %s\n","FIXME: Undocumented");
360         printf( "  -gl_luminance4_alpha4_ok %s\n","FIXME: Undocumented");
361         printf( "  -gl_readpixels_ok %s\n","FIXME: Undocumented");
362         printf( "  -gl_rgba2_ok    %s\n","FIXME: Undocumented");
363 //      printf( "  -gl_test1       %s\n","FIXME: Undocumented");
364         printf( "  -gl_test2       %s\n","FIXME: Undocumented");
365         printf( "  -gl_vidmem      %s\n","FIXME: Undocumented");
366 #ifdef OGL_RUNTIME_LOAD
367         printf( "  -gl_library <l> %s\n","use alternate opengl library");
368 #endif
369 #ifdef WGL_VIDEO
370         printf("  -gl_refresh <r> %s\n", "set refresh rate (in fullscreen mode)");
371 #endif
372 #endif
373 #ifdef SDL_VIDEO
374         printf( "  -nosdlvidmodecheck %s\n", "Some X servers don't like checking vidmode first, so just switch");
375         printf( "  -hwsurface      %s\n","FIXME: Undocumented");
376 #endif
377 #ifdef NETWORK
378         printf("  -udp            %s\n", "Specify options for udp/ip:");
379         printf("    @<shift>      %s\n", "  shift udp port base offset");
380         printf("    =<HOST_LIST>  %s\n", "  broadcast both local and to HOST_LIST");
381         printf("    +<HOST_LIST>  %s\n", "  broadcast only to HOST_LIST");
382         printf("                  %s\n", "   HOSTS can be any IP or hostname")
383                 ;
384         printf("                  %s\n", "   HOSTS can also be in the form of <address>:<shift>");
385         printf("                  %s\n", "   separate multiple HOSTS with a ,");
386 #endif
387 #ifdef __unix__
388         printf( "  -serialdevice <s> %s\n", "Set serial/modem device to <s>");
389         printf( "  -serialread <r> %s\n", "Set serial/modem to read from <r>");
390 #endif
391         printf( "\n Help:\n\n");
392         printf( "  -help, -h, -?, ? %s\n", "View this help screen");
393         printf( "\n");
394 }
395
396 void do_joystick_init()
397 {
398
399         if (!FindArg( "-nojoystick" ))  {
400                 con_printf(CON_VERBOSE, "\n%s", TXT_VERBOSE_6);
401                 joy_init();
402                 if ( FindArg( "-joyslow" ))     {
403                         con_printf(CON_VERBOSE, "\n%s", TXT_VERBOSE_7);
404                         joy_set_slow_reading(JOY_SLOW_READINGS);
405                 }
406                 if ( FindArg( "-joypolled" ))   {
407                         con_printf(CON_VERBOSE, "\n%s", TXT_VERBOSE_8);
408                         joy_set_slow_reading(JOY_POLLED_READINGS);
409                 }
410                 if ( FindArg( "-joybios" ))     {
411                         con_printf(CON_VERBOSE, "\n%s", TXT_VERBOSE_9);
412                         joy_set_slow_reading(JOY_BIOS_READINGS);
413                 }
414
415         //      Added from Descent v1.5 by John.  Adapted by Samir.
416         } else {
417                 con_printf(CON_VERBOSE, "\n%s", TXT_VERBOSE_10);
418         }
419 }
420
421 //set this to force game to run in low res
422 int disable_high_res=0;
423
424 void do_register_player(ubyte *title_pal)
425 {
426         Players[Player_num].callsign[0] = '\0';
427
428         if (!Auto_demo)         {
429
430                 key_flush();
431
432                 //now, before we bring up the register player menu, we need to
433                 //do some stuff to make sure the palette is ok.  First, we need to
434                 //get our current palette into the 2d's array, so the remapping will
435                 //work.  Second, we need to remap the fonts.  Third, we need to fill
436                 //in part of the fade tables so the darkening of the menu edges works
437
438                 memcpy(gr_palette,title_pal,sizeof(gr_palette));
439                 remap_fonts_and_menus(1);
440                 RegisterPlayer();               //get player's name
441         }
442
443 }
444
445 #define PROGNAME argv[0]
446
447 extern char Language[];
448
449 //can we do highres menus?
450 extern int MenuHiresAvailable;
451
452 int intro_played = 0;
453
454 int Inferno_verbose = 0;
455
456 //added on 11/18/98 by Victor Rachels to add -mission and -startgame
457 int start_net_immediately = 0;
458 //int start_with_mission = 0;
459 //char *start_with_mission_name;
460 //end this section addition
461
462 int open_movie_file(char *filename,int must_have);
463
464 #if defined(POLY_ACC)
465 #define MENU_HIRES_MODE SM_640x480x15xPA
466 #else
467 #define MENU_HIRES_MODE SM(640,480)
468 #endif
469
470 //      DESCENT II by Parallax Software
471 //              Descent Main
472
473 //extern ubyte gr_current_pal[];
474
475 #ifdef  EDITOR
476 int     Auto_exit = 0;
477 char    Auto_file[128] = "";
478 #endif
479
480 int main(int argc, char *argv[])
481 {
482         int i, t;
483         ubyte title_pal[768];
484
485         con_init();  // Initialise the console
486         mem_init();
487
488         error_init(NULL, NULL);
489
490         InitArgs( argc,argv );
491
492 #ifdef __unix__
493         {
494                 char *home = getenv("HOME");
495
496                 if ((t = FindArg("-userdir")))
497                         chdir(Args[t+1]);
498
499                 else if (home) {
500                         char buf[PATH_MAX + 5];
501
502                         strcpy(buf, home);
503                         strcat(buf, "/.d2x");
504                         if (chdir(buf)) {
505                                 mkdir(buf, 0755);
506                                 if (chdir(buf))
507                                         fprintf(stderr, "Cannot change to $HOME/.d2x\n");
508                         }
509                 }
510         }
511 #endif
512
513         if (FindArg("-debug"))
514                 con_threshold.value = (float)2;
515         else if (FindArg("-verbose"))
516                 con_threshold.value = (float)1;
517
518         //tell cfile where hogdir is
519         if ((t=FindArg("-hogdir")))
520                 cfile_use_alternate_hogdir(Args[t+1]);
521 #ifdef __unix__
522         else if (!FindArg("-nohogdir"))
523                 cfile_use_alternate_hogdir(SHAREPATH);
524 #endif
525
526         //tell cfile about our counter
527         cfile_set_critical_error_counter_ptr(&descent_critical_error);
528
529         if (! cfile_init("descent2.hog"))
530                 if (! cfile_init("d2demo.hog"))
531                         Warning("Could not find a valid hog file (descent2.hog or d2demo.hog)\nPossible locations are:\n"
532 #ifdef __unix__
533                               "\t$HOME/.d2x\n"
534                               "\t" SHAREPATH "\n"
535 #else
536                                   "\tCurrent directory\n"
537 #endif
538                                   "Or use the -hogdir option to specify an alternate location.");
539         load_text();
540
541         //print out the banner title
542         con_printf(CON_NORMAL, "\nDESCENT 2 %s v%d.%d",VERSION_TYPE,Version_major,Version_minor);
543         #ifdef VERSION_NAME
544         con_printf(CON_NORMAL, "  %s", VERSION_NAME);
545         #endif
546         if (cfexist(MISSION_DIR "d2x.hog"))
547                 con_printf(CON_NORMAL, "  Vertigo Enhanced");
548
549         con_printf(CON_NORMAL, "  %s %s\n", __DATE__,__TIME__);
550         con_printf(CON_NORMAL, "%s\n%s\n",TXT_COPYRIGHT,TXT_TRADEMARK);
551         con_printf(CON_NORMAL, "This is a MODIFIED version of Descent 2. Copyright (c) 1999 Peter Hawkins\n");
552         con_printf(CON_NORMAL, "                                         Copyright (c) 2002 Bradley Bell\n");
553
554
555         if (FindArg( "-?" ) || FindArg( "-help" ) || FindArg( "?" ) || FindArg( "-h" ) ) {
556                 print_commandline_help();
557                 set_exit_message("");
558 #ifdef __MINGW32__
559                 exit(0);  /* mingw hangs on this return.  dunno why */
560 #endif
561                 return(0);
562         }
563
564         con_printf(CON_NORMAL, "\n");
565         con_printf(CON_NORMAL, TXT_HELP, PROGNAME);             //help message has %s for program name
566         con_printf(CON_NORMAL, "\n");
567
568         //(re)added Mar 30, 2003 Micah Lieske - Allow use of 22K sound samples again.
569         if(FindArg("-sound22k"))
570         {
571                 digi_sample_rate = SAMPLE_RATE_22K;
572         }
573
574         if(FindArg("-sound11k"))
575         {
576                 digi_sample_rate = SAMPLE_RATE_11K;
577         }
578
579         arch_init_start();
580
581         arch_init();
582
583         //con_printf(CON_VERBOSE, "\n%s...", "Checking for Descent 2 CD-ROM");
584
585         if ((t = FindArg("-rearviewtime")))
586         {
587                 float f = atof(Args[t + 1]);
588                 Rear_view_leave_time = f * f1_0;
589         }
590         con_printf(CON_VERBOSE, "Rear_view_leave_time=0x%x (%f sec)\n", Rear_view_leave_time, Rear_view_leave_time / (float)f1_0);
591
592         //added/edited 8/18/98 by Victor Rachels to set maximum fps <= 100
593         if ((t = FindArg( "-maxfps" ))) {
594                 t=atoi(Args[t+1]);
595                 if (t>0&&t<=80)
596                         maxfps=t;
597         }
598         //end addition - Victor Rachels
599
600 #ifdef SUPPORTS_NICEFPS
601         if (FindArg("-nicefps"))
602                 use_nice_fps = 1;
603         if (FindArg("-niceautomap"))
604                 nice_automap = 1;
605 #endif
606
607         if (FindArg("-renderstats"))
608                 gr_renderstats = 1;
609
610         if ( FindArg( "-autodemo" ))
611                 Auto_demo = 1;
612
613 #ifndef RELEASE
614         if ( FindArg( "-noscreens" ) )
615                 Skip_briefing_screens = 1;
616 #endif
617
618         if ((t=FindArg("-tmap"))){
619                 select_tmap(Args[t+1]);
620         }else
621                 select_tmap(NULL);
622
623         Lighting_on = 1;
624
625 //      if (init_graphics()) return 1;
626
627         #ifdef EDITOR
628         if (!Inferno_is_800x600_available)      {
629                 con_printf(CON_NORMAL, "The editor will not be available, press any key to start game...\n" );
630                 Function_mode = FMODE_MENU;
631         }
632         #endif
633
634         if (!WVIDEO_running)
635                 con_printf(CON_DEBUG,"WVIDEO_running = %d\n",WVIDEO_running);
636
637         con_printf (CON_VERBOSE, "%s", TXT_VERBOSE_1);
638         ReadConfigFile();
639
640         do_joystick_init();
641
642 #if defined(POLY_ACC)
643     Current_display_mode = -1;
644     game_init_render_buffers(SM_640x480x15xPA, 640, 480, VR_NONE, VRF_COMPATIBLE_MENUS+VRF_ALLOW_COCKPIT );
645 #else
646
647         if (!VR_offscreen_buffer)       //if hasn't been initialied (by headset init)
648                 set_display_mode(0);            //..then set default display mode
649 #endif
650
651         {
652                 int screen_width = 640;
653                 int screen_height = 480;
654                 int vr_mode = VR_NONE;
655                 int screen_flags = VRF_USE_PAGING;
656
657                 if (FindResArg("", &screen_width, &screen_height))
658                 {
659                         /* stuff below mirrors values from display_mode_info in
660                          * menu.c which is used by set_display_mode. In fact,
661                          * set_display_mode should probably be rewritten to allow
662                          * arbitrary resolutions, and then we get rid of this
663                          * stuff here.
664                          */
665                         switch (SM(screen_width, screen_height))
666                         {
667                         case SM(320, 200):
668                         case SM(640, 480):
669                                 screen_flags = VRF_ALLOW_COCKPIT + VRF_COMPATIBLE_MENUS;
670                                 break;
671                         case SM(320, 400):
672                                 screen_flags = VRF_USE_PAGING;
673                                 break;
674                         case SM(640, 400):
675                         case SM(800, 600):
676                         case SM(1024, 768):
677                         case SM(1280, 1024):
678                                 screen_flags = VRF_COMPATIBLE_MENUS;
679                                 break;
680                         }
681
682                         con_printf(CON_VERBOSE, "Using %ix%i ...\n", screen_width, screen_height);
683                 }
684
685 // added ifdef on 9/30/98 by Matt Mueller to fix high res in linux
686 #ifdef __MSDOS__
687                 if (FindArg("-nodoublebuffer"))
688 #endif
689 // end addition -MM
690                 {
691                         con_printf(CON_VERBOSE, "Double-buffering disabled...\n");
692                         screen_flags &= ~VRF_USE_PAGING;
693                 }
694
695                 // added 3/24/99 by Owen Evans for screen res changing
696                 Game_screen_mode = SM(screen_width, screen_height);
697                 // end added -OE
698                 game_init_render_buffers(Game_screen_mode, screen_width, screen_height, vr_mode, screen_flags);
699
700         }
701         {
702 // added/edited on 12/14/98 by Matt Mueller - override res in d1x.ini with command line args
703                 int i, argnum = INT_MAX, w, h;
704 // 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.
705 #define SMODE(V,VV,VG) if ((i=FindResArg(#V, &w, &h)) && (i < argnum)) { argnum = i; VV = SM(w, h); VG = 0; }
706 #define SMODE_GR(V,VG) if ((i=FindArg("-" #V "_gameres"))){if (i<argnum) VG=1;}
707 #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) );
708 // aren't #defines great? :)
709 // end addition/edit -MM
710 #define S_MODE(V,VV,VG) argnum = INT_MAX; SMODE(V, VV, VG); SMODE_GR(V, VG); SMODE_PRINT(V, VV, VG);
711
712                 S_MODE(automap,automap_mode,automap_use_game_res);
713 //              S_MODE(menu,menu_screen_mode,menu_use_game_res);
714          }
715 //end addition -MM
716
717         i = FindArg( "-xcontrol" );
718         if ( i > 0 )    {
719                 kconfig_init_external_controls( strtol(Args[i+1], NULL, 0), strtol(Args[i+2], NULL, 0) );
720         }
721
722         con_printf(CON_VERBOSE, "\n%s\n\n", TXT_INITIALIZING_GRAPHICS);
723         if (FindArg("-nofade"))
724                 grd_fades_disabled=1;
725
726         //determine whether we're using high-res menus & movies
727 #if !defined(POLY_ACC)
728         if (FindArg("-nohires") || FindArg("-nohighres") || (gr_check_mode(MENU_HIRES_MODE) != 0) || disable_high_res)
729                 MovieHires = MenuHires = MenuHiresAvailable = 0;
730         else
731 #endif
732                 //NOTE LINK TO ABOVE!
733                 MenuHires = MenuHiresAvailable = 1;
734
735         if (FindArg( "-lowresmovies" ))
736                 MovieHires = 0;
737
738         if ((t=gr_init())!=0)                           //doesn't do much
739                 Error(TXT_CANT_INIT_GFX,t);
740
741    #ifdef _3DFX
742    _3dfx_Init();
743    #endif
744
745         // Load the palette stuff. Returns non-zero if error.
746         con_printf(CON_DEBUG, "Initializing palette system...\n" );
747         gr_use_palette_table(DEFAULT_PALETTE );
748
749         con_printf(CON_DEBUG, "Initializing font system...\n" );
750         gamefont_init();        // must load after palette data loaded.
751
752         con_printf( CON_DEBUG, "Initializing movie libraries...\n" );
753         init_movies();          //init movie libraries
754
755 #if 0
756         con_printf(CON_VERBOSE, "Going into graphics mode...\n");
757 #if defined(POLY_ACC)
758         gr_set_mode(SM_640x480x15xPA);
759 #else
760         gr_set_mode(MovieHires?SM(640,480):SM(320,200));
761 #endif
762 #endif
763
764         #ifndef RELEASE
765         if ( FindArg( "-notitles" ) )
766                 songs_play_song( SONG_TITLE, 1);
767         else
768         #endif
769         {       //NOTE LINK TO ABOVE!
770 #ifndef SHAREWARE
771                 int played=MOVIE_NOT_PLAYED;    //default is not played
772 #endif
773                 int song_playing = 0;
774
775                 #ifdef D2_OEM
776                 #define MOVIE_REQUIRED 0
777                 #else
778                 #define MOVIE_REQUIRED 1
779                 #endif
780
781 #ifdef D2_OEM   //$$POLY_ACC, jay.
782                 {       //show bundler screens
783                         char filename[FILENAME_LEN];
784
785                         played=MOVIE_NOT_PLAYED;        //default is not played
786
787             played = PlayMovie("pre_i.mve",0);
788
789                         if (!played) {
790                 strcpy(filename,MenuHires?"pre_i1b.pcx":"pre_i1.pcx");
791
792                                 while (cfexist(filename))
793                                 {
794                                         show_title_screen( filename, 1, 0 );
795                     filename[5]++;
796                                 }
797                         }
798                 }
799 #endif
800
801                 #ifndef SHAREWARE
802                         init_subtitles("intro.tex");
803                         played = PlayMovie("intro.mve",MOVIE_REQUIRED);
804                         close_subtitles();
805                 #endif
806
807                 #ifdef D2_OEM
808                 if (played != MOVIE_NOT_PLAYED)
809                         intro_played = 1;
810                 else {                                          //didn't get intro movie, try titles
811
812                         played = PlayMovie("titles.mve",MOVIE_REQUIRED);
813
814                         if (played == MOVIE_NOT_PLAYED) {
815 #if defined(POLY_ACC)
816                                 gr_set_mode(SM_640x480x15xPA);
817 #else
818                                 gr_set_mode(MenuHires?SM_640x480V:SM_320x200C);
819 #endif
820                                 con_printf( CON_DEBUG, "\nPlaying title song..." );
821                                 songs_play_song( SONG_TITLE, 1);
822                                 song_playing = 1;
823                                 con_printf( CON_DEBUG, "\nShowing logo screens..." );
824                                 show_title_screen( MenuHires?"iplogo1b.pcx":"iplogo1.pcx", 1, 1 );
825                                 show_title_screen( MenuHires?"logob.pcx":"logo.pcx", 1, 1 );
826                         }
827                 }
828
829                 {       //show bundler movie or screens
830
831                         char filename[FILENAME_LEN];
832                         int movie_handle;
833
834                         played=MOVIE_NOT_PLAYED;        //default is not played
835
836                         //check if OEM movie exists, so we don't stop the music if it doesn't
837                         movie_handle = open_movie_file("oem.mve",0);
838                         if (movie_handle != -1) {
839                                 close(movie_handle);
840                                 played = PlayMovie("oem.mve",0);
841                                 song_playing = 0;               //movie will kill sound
842                         }
843
844                         if (!played) {
845                                 strcpy(filename,MenuHires?"oem1b.pcx":"oem1.pcx");
846
847                                 while (cfexist(filename))
848                                 {
849                                         show_title_screen( filename, 1, 0 );
850                                         filename[3]++;
851                                 }
852                         }
853                 }
854                 #endif
855
856                 if (!song_playing)
857                         songs_play_song( SONG_TITLE, 1);
858                         
859         }
860
861         PA_DFX (pa_splash());
862
863         con_printf( CON_DEBUG, "\nShowing loading screen..." );
864         {
865                 //grs_bitmap title_bm;
866                 int pcx_error;
867                 char filename[14];
868
869                 strcpy(filename, MenuHires?"descentb.pcx":"descent.pcx");
870                 if (! cfexist(filename))
871                         strcpy(filename, MenuHires?"descntob.pcx":"descento.pcx"); // OEM
872                 if (! cfexist(filename))
873                         strcpy(filename, "descentd.pcx"); // SHAREWARE
874                 if (! cfexist(filename))
875                         strcpy(filename, "descentb.pcx"); // MAC SHAREWARE
876
877 #if defined(POLY_ACC)
878                 gr_set_mode(SM_640x480x15xPA);
879 #else
880                 gr_set_mode(MenuHires?SM(640,480):SM(320,200));
881 #endif
882 #ifdef OGL
883                 set_screen_mode(SCREEN_MENU);
884 #endif
885
886                 FontHires = FontHiresAvailable && MenuHires;
887
888                 if ((pcx_error=pcx_read_fullscr( filename, title_pal ))==PCX_ERROR_NONE)        {
889                         //vfx_set_palette_sub( title_pal );
890                         gr_palette_clear();
891                         gr_palette_fade_in( title_pal, 32, 0 );
892                 } else
893                         Error( "Couldn't load pcx file '%s', PCX load error: %s\n",filename, pcx_errormsg(pcx_error));
894         }
895
896         con_printf( CON_DEBUG , "\nDoing bm_init..." );
897         #ifdef EDITOR
898                 bm_init_use_tbl();
899         #else
900                 bm_init();
901         #endif
902
903         #ifdef EDITOR
904         if (FindArg("-hoarddata") != 0) {
905                 #define MAX_BITMAPS_PER_BRUSH 30
906                 grs_bitmap * bm[MAX_BITMAPS_PER_BRUSH];
907                 grs_bitmap icon;
908                 int nframes;
909                 short nframes_short;
910                 ubyte palette[256*3];
911                 FILE *ofile;
912                 int iff_error,i;
913                 char *sounds[] = {"selforb.raw","selforb.r22",          //SOUND_YOU_GOT_ORB                     
914                                                                 "teamorb.raw","teamorb.r22",            //SOUND_FRIEND_GOT_ORB                  
915                                                                 "enemyorb.raw","enemyorb.r22",  //SOUND_OPPONENT_GOT_ORB        
916                                                                 "OPSCORE1.raw","OPSCORE1.r22"}; //SOUND_OPPONENT_HAS_SCORED
917
918                 ofile = fopen("hoard.ham","wb");
919
920            iff_error = iff_read_animbrush("orb.abm",bm,MAX_BITMAPS_PER_BRUSH,&nframes,palette);
921                 Assert(iff_error == IFF_NO_ERROR);
922                 nframes_short = nframes;
923                 fwrite(&nframes_short,sizeof(nframes_short),1,ofile);
924                 fwrite(&bm[0]->bm_w,sizeof(short),1,ofile);
925                 fwrite(&bm[0]->bm_h,sizeof(short),1,ofile);
926                 fwrite(palette,3,256,ofile);
927                 for (i=0;i<nframes;i++)
928                         fwrite(bm[i]->bm_data,1,bm[i]->bm_w*bm[i]->bm_h,ofile);
929
930                 iff_error = iff_read_animbrush("orbgoal.abm",bm,MAX_BITMAPS_PER_BRUSH,&nframes,palette);
931                 Assert(iff_error == IFF_NO_ERROR);
932                 Assert(bm[0]->bm_w == 64 && bm[0]->bm_h == 64);
933                 nframes_short = nframes;
934                 fwrite(&nframes_short,sizeof(nframes_short),1,ofile);
935                 fwrite(palette,3,256,ofile);
936                 for (i=0;i<nframes;i++)
937                         fwrite(bm[i]->bm_data,1,bm[i]->bm_w*bm[i]->bm_h,ofile);
938
939                 for (i=0;i<2;i++) {
940                         iff_error = iff_read_bitmap(i?"orbb.bbm":"orb.bbm",&icon,BM_LINEAR,palette);
941                         Assert(iff_error == IFF_NO_ERROR);
942                         fwrite(&icon.bm_w,sizeof(short),1,ofile);
943                         fwrite(&icon.bm_h,sizeof(short),1,ofile);
944                         fwrite(palette,3,256,ofile);
945                         fwrite(icon.bm_data,1,icon.bm_w*icon.bm_h,ofile);
946                 }
947
948                 for (i=0;i<sizeof(sounds)/sizeof(*sounds);i++) {
949                         FILE *ifile;
950                         int size;
951                         ubyte *buf;
952                         ifile = fopen(sounds[i],"rb");
953                         Assert(ifile != NULL);
954                         size = ffilelength(ifile);
955                         buf = d_malloc(size);
956                         fread(buf,1,size,ifile);
957                         fwrite(&size,sizeof(size),1,ofile);
958                         fwrite(buf,1,size,ofile);
959                         d_free(buf);
960                         fclose(ifile);
961                 }
962
963                 fclose(ofile);
964
965                 exit(1);
966         }
967         #endif
968
969         //the bitmap loading code changes gr_palette, so restore it
970         memcpy(gr_palette,title_pal,sizeof(gr_palette));
971
972         if ( FindArg( "-norun" ) )
973                 return(0);
974
975         con_printf( CON_DEBUG, "\nInitializing 3d system..." );
976         g3_init();
977
978         con_printf( CON_DEBUG, "\nInitializing texture caching system..." );
979         texmerge_init( 10 );            // 10 cache bitmaps
980
981         con_printf( CON_DEBUG, "\nRunning game...\n" );
982         set_screen_mode(SCREEN_MENU);
983
984         init_game();
985
986         //      If built with editor, option to auto-load a level and quit game
987         //      to write certain data.
988         #ifdef  EDITOR
989         {       int t;
990         if ( (t = FindArg( "-autoload" )) ) {
991                 Auto_exit = 1;
992                 strcpy(Auto_file, Args[t+1]);
993         }
994                 
995         }
996
997         if (Auto_exit) {
998                 strcpy(Players[0].callsign, "dummy");
999         } else
1000         #endif
1001                 do_register_player(title_pal);
1002
1003         gr_palette_fade_out( title_pal, 32, 0 );
1004
1005         Game_mode = GM_GAME_OVER;
1006
1007         if (Auto_demo)  {
1008                 newdemo_start_playback("descent.dem");          
1009                 if (Newdemo_state == ND_STATE_PLAYBACK )
1010                         Function_mode = FMODE_GAME;
1011         }
1012
1013         //do this here because the demo code can do a longjmp when trying to
1014         //autostart a demo from the main menu, never having gone into the game
1015         setjmp(LeaveGame);
1016
1017         while (Function_mode != FMODE_EXIT)
1018         {
1019                 switch( Function_mode ) {
1020                 case FMODE_MENU:
1021                         set_screen_mode(SCREEN_MENU);
1022                         if ( Auto_demo )        {
1023                                 newdemo_start_playback(NULL);           // Randomly pick a file
1024                                 if (Newdemo_state != ND_STATE_PLAYBACK) 
1025                                         Error("No demo files were found for autodemo mode!");
1026                         } else {
1027                                 #ifdef EDITOR
1028                                 if (Auto_exit) {
1029                                         strcpy((char *)&Level_names[0], Auto_file);
1030                                         LoadLevel(1, 1);
1031                                         Function_mode = FMODE_EXIT;
1032                                         break;
1033                                 }
1034                                 #endif
1035
1036                                 check_joystick_calibration();
1037                                 gr_palette_clear();             //I'm not sure why we need this, but we do
1038                                 DoMenu();                                                                               
1039                                 #ifdef EDITOR
1040                                 if ( Function_mode == FMODE_EDITOR )    {
1041                                         create_new_mine();
1042                                         SetPlayerFromCurseg();
1043                                         load_palette(NULL,1,0);
1044                                 }
1045                                 #endif
1046                         }
1047                         break;
1048                 case FMODE_GAME:
1049                         #ifdef EDITOR
1050                                 keyd_editor_mode = 0;
1051                         #endif
1052
1053 #ifdef SDL_INPUT
1054                         /* keep the mouse from wandering in SDL */
1055                         if (FindArg("-grabmouse"))
1056                             SDL_WM_GrabInput(SDL_GRAB_ON);
1057 #endif
1058
1059                         game();
1060
1061 #ifdef SDL_INPUT
1062                         /* give control back to the WM */
1063                         if (FindArg("-grabmouse"))
1064                             SDL_WM_GrabInput(SDL_GRAB_OFF);
1065 #endif
1066
1067                         if ( Function_mode == FMODE_MENU )
1068                                 songs_play_song( SONG_TITLE, 1 );
1069                         break;
1070                 #ifdef EDITOR
1071                 case FMODE_EDITOR:
1072                         keyd_editor_mode = 1;
1073                         editor();
1074 #ifdef __WATCOMC__
1075                         _harderr( (void*)descent_critical_error_handler );              // Reinstall game error handler
1076 #endif
1077                         if ( Function_mode == FMODE_GAME ) {
1078                                 Game_mode = GM_EDITOR;
1079                                 editor_reset_stuff_on_level();
1080                                 N_players = 1;
1081                         }
1082                         break;
1083                 #endif
1084                 default:
1085                         Error("Invalid function mode %d",Function_mode);
1086                 }
1087         }
1088
1089         WriteConfigFile();
1090
1091         #ifndef RELEASE
1092         if (!FindArg( "-notitles" ))
1093         #endif
1094                 show_order_form();
1095
1096         #ifndef NDEBUG
1097         if ( FindArg( "-showmeminfo" ) )
1098                 show_mem_info = 1;              // Make memory statistics show
1099         #endif
1100
1101         return(0);              //presumably successful exit
1102 }
1103
1104
1105 void check_joystick_calibration()       {
1106         int x1, y1, x2, y2, c;
1107         fix t1;
1108
1109         if ( (Config_control_type!=CONTROL_JOYSTICK) &&
1110                   (Config_control_type!=CONTROL_FLIGHTSTICK_PRO) &&
1111                   (Config_control_type!=CONTROL_THRUSTMASTER_FCS) &&
1112                   (Config_control_type!=CONTROL_GRAVIS_GAMEPAD)
1113                 ) return;
1114
1115         joy_get_pos( &x1, &y1 );
1116
1117         t1 = timer_get_fixed_seconds();
1118         while( timer_get_fixed_seconds() < t1 + F1_0/100 )
1119                 ;
1120
1121         joy_get_pos( &x2, &y2 );
1122
1123         // If joystick hasn't moved...
1124         if ( (abs(x2-x1)<30) &&  (abs(y2-y1)<30) )      {
1125                 if ( (abs(x1)>30) || (abs(x2)>30) ||  (abs(y1)>30) || (abs(y2)>30) )    {
1126                         c = nm_messagebox( NULL, 2, TXT_CALIBRATE, TXT_SKIP, TXT_JOYSTICK_NOT_CEN );
1127                         if ( c==0 )     {
1128                                 joydefs_calibrate();
1129                         }
1130                 }
1131         }
1132
1133 }
1134
1135 void show_order_form()
1136 {
1137 #ifndef EDITOR
1138
1139         int pcx_error;
1140         unsigned char title_pal[768];
1141         char    exit_screen[16];
1142
1143         gr_set_current_canvas( NULL );
1144         gr_palette_clear();
1145
1146         key_flush();
1147
1148         strcpy(exit_screen, MenuHires?"ordrd2ob.pcx":"ordrd2o.pcx"); // OEM
1149         if (! cfexist(exit_screen))
1150                 strcpy(exit_screen, MenuHires?"orderd2b.pcx":"orderd2.pcx"); // SHAREWARE, prefer mac if hires
1151         if (! cfexist(exit_screen))
1152                 strcpy(exit_screen, MenuHires?"orderd2.pcx":"orderd2b.pcx"); // SHAREWARE, have to rescale
1153         if (! cfexist(exit_screen))
1154                 strcpy(exit_screen, MenuHires?"warningb.pcx":"warning.pcx"); // D1
1155         if (! cfexist(exit_screen))
1156                 return; // D2 registered
1157
1158         if ((pcx_error=pcx_read_fullscr( exit_screen, title_pal ))==PCX_ERROR_NONE) {
1159                 //vfx_set_palette_sub( title_pal );
1160                 gr_palette_fade_in( title_pal, 32, 0 );
1161                 gr_update();
1162                 while (!key_inkey() && !mouse_button_state(0)) {} //key_getch();
1163                 gr_palette_fade_out( title_pal, 32, 0 );
1164         }
1165         else
1166                 Int3();         //can't load order screen
1167
1168         key_flush();
1169
1170 #endif
1171 }
1172
1173 void quit_request()
1174 {
1175 #ifdef NETWORK
1176 //      void network_abort_game();
1177 //      if(Network_status)
1178 //              network_abort_game();
1179 #endif
1180         exit(0);
1181 }