Added command-line help.
[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 char copyright[] = "DESCENT II  COPYRIGHT (C) 1994-1996 PARALLAX SOFTWARE CORPORATION";
15
16 #include <conf.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include "pstypes.h"
22 #include "strutil.h"
23 #include "console.h"
24 #include "pa_enabl.h"       //$$POLY_ACC
25 #include "gr.h"
26 #include "fix.h"
27 #include "vecmat.h"
28 #include "mono.h"
29 #include "key.h"
30 #include "timer.h"
31 #include "3d.h"
32 #include "bm.h"
33 #include "inferno.h"
34 #include "error.h"
35 #include "game.h"
36 #include "segment.h"            //for Side_to_verts
37 #include "u_mem.h"
38 #include "segpoint.h"
39 #include "screens.h"
40 #include "texmap.h"
41 #include "texmerge.h"
42 #include "menu.h"
43 #include "wall.h"
44 #include "polyobj.h"
45 #include "effects.h"
46 #include "digi.h"
47 #include "iff.h"
48 #include "pcx.h"
49 #include "palette.h"
50 #include "args.h"
51 #include "sounds.h"
52 #include "titles.h"
53 #include "player.h"
54 #include "text.h"
55 #include "ipx.h"
56 #include "newdemo.h"
57 #include "network.h"
58 #include "modem.h"
59 #include "gamefont.h"
60 #include "kconfig.h"
61 #include "mouse.h"
62 #include "joy.h"
63 #include "newmenu.h"
64 #include "desc_id.h"
65 #include "config.h"
66 #include "joydefs.h"
67 #include "multi.h"
68 #include "songs.h"
69 #include "cfile.h"
70 #include "gameseq.h"
71 #include "gamepal.h"
72 #include "mission.h"
73 #include "movie.h"
74 #include "compbit.h"
75
76 // #  include "3dfx_des.h"
77
78 #if defined(POLY_ACC)
79 #include "poly_acc.h"
80 extern int Current_display_mode;        //$$ there's got to be a better way than hacking this.
81 #endif
82
83 #ifdef EDITOR
84 #include "editor\editor.h"
85 #include "editor\kdefs.h"
86 #include "ui.h"
87 #endif
88
89 #ifdef SDL_INPUT
90 #include <SDL/SDL.h>
91 #endif
92
93 #include "vers_id.h"
94
95 void mem_init(void);
96 void arch_init(void);
97 void arch_init_start(void);
98
99 //Current version number
100
101 ubyte Version_major = 1;                //FULL VERSION
102 ubyte Version_minor = 2;
103
104 static const char desc_id_checksum_str[] = DESC_ID_CHKSUM_TAG "0000";   //4-byte checksum
105 char desc_id_exit_num = 0;
106
107 int Function_mode=FMODE_MENU;           //game or editor?
108 int Screen_mode=-1;                                     //game screen or editor screen?
109
110 //--unused-- grs_bitmap Inferno_bitmap_title;
111
112 int WVIDEO_running=0;           //debugger can set to 1 if running
113
114 #ifdef EDITOR
115 int Inferno_is_800x600_available = 0;
116 #endif
117
118 //--unused-- int Cyberman_installed=0;                  // SWIFT device present
119 ubyte CybermouseActive=0;
120
121 int __far descent_critical_error_handler( unsigned deverr, unsigned errcode, unsigned __far * devhdr );
122
123 void check_joystick_calibration(void);
124
125
126 //--------------------------------------------------------------------------
127
128 extern int piggy_low_memory;
129
130
131 int descent_critical_error = 0;
132 unsigned descent_critical_deverror = 0;
133 unsigned descent_critical_errcode = 0;
134
135 extern int Network_allow_socket_changes;
136
137 extern void vfx_set_palette_sub(ubyte *);
138
139 extern int VR_low_res;
140
141 extern int Config_vr_type;
142 extern int Config_vr_resolution;
143 extern int Config_vr_tracking;
144 int grd_fades_disabled=1;
145
146 #define LINE_LEN        100
147
148 //read help from a file & print to screen
149 void print_commandline_help()
150 {
151         CFILE *ifile;
152         int have_binary=0;
153         char line[LINE_LEN];
154
155         ifile = cfopen("help.tex","rb");
156         if (!ifile) {
157                 ifile = cfopen("help.txb","rb");
158                 if (!ifile)
159                         Error("Cannot load help text file.");
160                 have_binary = 1;
161         }
162
163         while (cfgets(line,LINE_LEN,ifile)) {
164
165                 if (have_binary) {
166                         int i;
167                         for (i = 0; i < strlen(line) - 1; i++) {
168                                 encode_rotate_left(&(line[i]));
169                                 line[i] = line[i] ^ BITMAP_TBL_XOR;
170                                 encode_rotate_left(&(line[i]));
171                         }
172                 }
173
174 //              if (line[0] == ';')
175 //                      continue;               //don't show comments
176
177                 printf("%s",line);
178
179         }
180
181         cfclose(ifile);
182
183 // D2X-specific options:
184         printf( " D2X Options:\n\n");
185         printf( "  -noredundancy   %s\n", "Do not send messages when picking up redundant items in multi");
186         printf( "  -shortpackets   %s\n", "Set shortpackets to default as on");
187         printf( "  -notitles       %s\n", "Do not show titlescreens on startup");
188         printf( "  -ini <file>     %s\n", "option file (alternate to command line)");
189         printf( "  -autodemo       %s\n","FIXME: Undocumented");
190         printf( "  -autoload       %s\n","FIXME: Undocumented");
191         printf( "  -bigpig         %s\n","FIXME: Undocumented");
192         printf( "  -bspgen         %s\n","FIXME: Undocumented");
193         printf( "  -cdproxy        %s\n","FIXME: Undocumented");
194         printf( "  -checktime      %s\n","FIXME: Undocumented");
195         printf( "  -codereadonly   %s\n","FIXME: Undocumented");
196         printf( "  -cyberimpact    %s\n","FIXME: Undocumented");
197         printf( "  -ddemul         %s\n","FIXME: Undocumented");
198         printf( "  -debug          %s\n","FIXME: Undocumented");
199         printf( "  -debugmode      %s\n","FIXME: Undocumented");
200         printf( "  -disallowgfx    %s\n","FIXME: Undocumented");
201         printf( "  -disallowreboot %s\n","FIXME: Undocumented");
202         printf( "  -dynamicsockets %s\n","FIXME: Undocumented");
203         printf( "  -emul           %s\n","FIXME: Undocumented");
204         printf( "  -forcegfx       %s\n","FIXME: Undocumented");
205         printf( "  -grabmouse      %s\n","FIXME: Undocumented");
206         printf( "  -hoarddata      %s\n","FIXME: Undocumented");
207         printf( "  -hw_3dacc       %s\n","FIXME: Undocumented");
208         printf( "  -hwsurface      %s\n","FIXME: Undocumented");
209         printf( "  -invulnerability %s\n","FIXME: Undocumented");
210         printf( "  -ipxnetwork     %s\n","FIXME: Undocumented");
211         printf( "  -jasen          %s\n","FIXME: Undocumented");
212         printf( "  -joyslow        %s\n","FIXME: Undocumented");
213         printf( "  -kali           %s\n","FIXME: Undocumented");
214         printf( "  -logfile        %s\n","FIXME: Undocumented");
215         printf( "  -lowresmovies   %s\n","FIXME: Undocumented");
216         printf( "  -macdata        %s\n","FIXME: Undocumented");
217         printf( "  -memdbg         %s\n","FIXME: Undocumented");
218         printf( "  -monodebug      %s\n","FIXME: Undocumented");
219         printf( "  -nobm           %s\n","FIXME: Undocumented");
220         printf( "  -nocdrom        %s\n","FIXME: Undocumented");
221         printf( "  -nocyberman     %s\n","FIXME: Undocumented");
222         printf( "  -nofade         %s\n","FIXME: Undocumented");
223         printf( "  -nomatrixcheat  %s\n","FIXME: Undocumented");
224         printf( "  -nomixer        %s\n","FIXME: Undocumented");
225         printf( "  -nomodex        %s\n","FIXME: Undocumented");
226         printf( "  -nomovies       %s\n","FIXME: Undocumented");
227         printf( "  -norankings     %s\n","FIXME: Undocumented");
228         printf( "  -noredbook      %s\n","FIXME: Undocumented");
229         printf( "  -norun          %s\n","FIXME: Undocumented");
230         printf( "  -noscreens      %s\n","FIXME: Undocumented");
231         printf( "  -ordinaljoy     %s\n","FIXME: Undocumented");
232         printf( "  -packets        %s\n","FIXME: Undocumented");
233         printf( "  -rtscts         %s\n","FIXME: Undocumented");
234         printf( "  -semiwin        %s\n","FIXME: Undocumented");
235         printf( "  -showaddress    %s\n","FIXME: Undocumented");
236         printf( "  -showmeminfo    %s\n","FIXME: Undocumented");
237         printf( "  -socket         %s\n","FIXME: Undocumented");
238         printf( "  -specialdevice  %s\n","FIXME: Undocumented");
239         printf( "  -stickmag       %s\n","FIXME: Undocumented");
240         printf( "  -stopwatch      %s\n","FIXME: Undocumented");
241         printf( "  -superhires     %s\n","FIXME: Undocumented");
242         printf( "  -sysram         %s\n","FIXME: Undocumented");
243         printf( "  -text           %s\n","FIXME: Undocumented");
244         printf( "  -tsengdebug1    %s\n","FIXME: Undocumented");
245         printf( "  -tsengdebug2    %s\n","FIXME: Undocumented");
246         printf( "  -tsengdebug3    %s\n","FIXME: Undocumented");
247         printf( "  -udp            %s\n","FIXME: Undocumented");
248         printf( "  -vidram         %s\n","FIXME: Undocumented");
249         printf( "  -xcontrol       %s\n","FIXME: Undocumented");
250         printf( "  -xname          %s\n","FIXME: Undocumented");
251         printf( "  -xver           %s\n","FIXME: Undocumented");
252
253         printf( "\n D2X System Options:\n\n");
254
255 #ifdef __MSDOS__
256         printf( "  -joy209         %s\n", "Use alternate port 209 for joystick");
257 #endif
258 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE 
259         printf( "  -fullscreen     %s\n", "Use fullscreen mode if available");
260 #endif
261 #ifdef OGL
262         printf( "  -gl_texmagfilt <f> %s\n","set GL_TEXTURE_MAG_FILTER (see readme.d1x)");
263         printf( "  -gl_texminfilt <f> %s\n","set GL_TEXTURE_MIN_FILTER (see readme.d1x)");
264         printf( "  -gl_mipmap      %s\n","set gl texture filters to \"standard\" options for mipmapping");
265         printf( "  -gl_simple      %s\n","set gl texture filters to gl_nearest for \"original\" look. (default)");
266         printf( "  -gl_alttexmerge %s\n","use new texmerge, usually uses less ram (default)");
267         printf( "  -gl_stdtexmerge %s\n","use old texmerge, uses more ram, but _might_ be a bit faster");
268         printf( "  -gl_voodoo      %s\n","force fullscreen mode only");
269         printf( "  -gl_16bittextures %s\n","attempt to use 16bit textures");
270         printf( "  -gl_reticle <r> %s\n","use OGL reticle 0=never 1=above 320x* 2=always");
271         printf( "  -gl_intensity4_ok %s\n","FIXME: Undocumented");
272         printf( "  -gl_luminance4_alpha4_ok %s\n","FIXME: Undocumented");
273         printf( "  -gl_readpixels_ok %s\n","FIXME: Undocumented");
274         printf( "  -gl_rgba2_ok    %s\n","FIXME: Undocumented");
275         printf( "  -gl_test1       %s\n","FIXME: Undocumented");
276         printf( "  -gl_test2       %s\n","FIXME: Undocumented");
277         printf( "  -gl_vidmem      %s\n","FIXME: Undocumented");
278 #ifdef OGL_RUNTIME_LOAD
279         printf( "  -gl_library <l> %s\n","use alternate opengl library");
280 #endif
281 #endif
282 #ifdef SDL_VIDEO
283         printf( "  -nosdlvidmodecheck %s\n", "Some X servers don't like checking vidmode first, so just switch");
284 #endif
285 #ifdef __ENV_LINUX__
286         printf( "  -serialdevice <s> %s\n", "Set serial/modem device to <s>");
287         printf( "  -serialread <r> %s\n", "Set serial/modem to read from <r>");
288 #endif
289         printf( "Help:\n");
290         printf( "  -help, -?, ?    %s\n", "View this help screen");
291         printf( "\n");
292 }
293
294 void do_joystick_init()
295 {
296  
297
298         if (!FindArg( "-nojoystick" ))  {
299                 con_printf(CON_VERBOSE, "\n%s", TXT_VERBOSE_6);
300                 joy_init();
301                 if ( FindArg( "-joyslow" ))     {
302                         con_printf(CON_VERBOSE, "\n%s", TXT_VERBOSE_7);
303                         joy_set_slow_reading(JOY_SLOW_READINGS);
304                 }
305                 if ( FindArg( "-joypolled" ))   {
306                         con_printf(CON_VERBOSE, "\n%s", TXT_VERBOSE_8);
307                         joy_set_slow_reading(JOY_POLLED_READINGS);
308                 }
309                 if ( FindArg( "-joybios" ))     {
310                         con_printf(CON_VERBOSE, "\n%s", TXT_VERBOSE_9);
311                         joy_set_slow_reading(JOY_BIOS_READINGS);
312                 }
313
314         //      Added from Descent v1.5 by John.  Adapted by Samir.
315         } else {
316                 con_printf(CON_VERBOSE, "\n%s", TXT_VERBOSE_10);
317         }
318 }
319
320 //set this to force game to run in low res
321 int disable_high_res=0;
322
323 void do_register_player(ubyte *title_pal)
324 {
325         Players[Player_num].callsign[0] = '\0';
326
327         if (!Auto_demo)         {
328
329                 key_flush();
330
331                 //now, before we bring up the register player menu, we need to 
332                 //do some stuff to make sure the palette is ok.  First, we need to
333                 //get our current palette into the 2d's array, so the remapping will
334                 //work.  Second, we need to remap the fonts.  Third, we need to fill 
335                 //in part of the fade tables so the darkening of the menu edges works
336
337                 memcpy(gr_palette,title_pal,sizeof(gr_palette));
338                 remap_fonts_and_menus(1);
339                 RegisterPlayer();               //get player's name
340         }
341
342 }
343
344 #ifdef NETWORK
345 void do_network_init()
346 {
347         if (!FindArg( "-nonetwork" ))   {
348                 int socket=0, showaddress=0, t;
349                 int ipx_error;
350
351                 con_printf(CON_VERBOSE, "\n%s ", TXT_INITIALIZING_NETWORK);
352                 if ((t=FindArg("-socket")))
353                         socket = atoi( Args[t+1] );
354                 //@@if ( FindArg("-showaddress") ) showaddress=1;
355                 if ((ipx_error=ipx_init(IPX_DEFAULT_SOCKET+socket,showaddress))==0)     {
356                         con_printf(CON_VERBOSE, "%s %d.\n", TXT_IPX_CHANNEL, socket );
357                         Network_active = 1;
358                 } else {
359                         switch( ipx_error )     {
360                         case 3:         con_printf(CON_VERBOSE, "%s\n", TXT_NO_NETWORK); break;
361                         case -2: con_printf(CON_VERBOSE, "%s 0x%x.\n", TXT_SOCKET_ERROR, IPX_DEFAULT_SOCKET+socket); break;
362                         case -4: con_printf(CON_VERBOSE, "%s\n", TXT_MEMORY_IPX ); break;
363                         default:
364                                 con_printf(CON_VERBOSE, "%s %d", TXT_ERROR_IPX, ipx_error );
365                         }
366                         con_printf(CON_VERBOSE, "%s\n",TXT_NETWORK_DISABLED);
367                         Network_active = 0;             // Assume no network
368                 }
369                 ipx_read_user_file( "descent.usr" );
370                 ipx_read_network_file( "descent.net" );
371                 //@@if ( FindArg( "-dynamicsockets" ))
372                 //@@    Network_allow_socket_changes = 1;
373                 //@@else
374                 //@@    Network_allow_socket_changes = 0;
375         } else {
376                 con_printf(CON_VERBOSE, "%s\n", TXT_NETWORK_DISABLED);
377                 Network_active = 0;             // Assume no network
378         }
379 }
380 #endif
381
382 #ifdef SHAREARE
383 #define PROGNAME "d2demo"
384 #else
385 #define PROGNAME "d2"
386 #endif
387
388 extern char Language[];
389
390 //can we do highres menus?
391 extern int MenuHiresAvailable;
392
393 #ifdef D2_OEM
394 int intro_played = 0;
395 #endif
396
397 int open_movie_file(char *filename,int must_have);
398
399 #if defined(POLY_ACC)
400 #define MENU_HIRES_MODE SM_640x480x15xPA
401 #else
402 #define MENU_HIRES_MODE SM(640,480)
403 #endif
404
405 //      DESCENT II by Parallax Software
406 //              Descent Main
407
408 //extern ubyte gr_current_pal[];
409
410 #ifdef  EDITOR
411 int     Auto_exit = 0;
412 char    Auto_file[128] = "";
413 #endif
414
415 int main(int argc,char **argv)
416 {
417         int i,t;
418         ubyte title_pal[768];
419
420         
421         con_init();  // Initialise the console
422         mem_init();
423
424         error_init(NULL, NULL);
425
426         args_init( argc,argv );
427
428         if ( FindArg( "-debug") )
429         {
430                 con_threshold.value = (float)2;
431
432         } else
433                 if ( FindArg( "-verbose" ) ) 
434                 {
435                         con_threshold.value = (float)1;
436                 }
437
438         arch_init_start();
439
440         arch_init();
441
442         //tell cfile about our counter
443         cfile_set_critical_error_counter_ptr(&descent_critical_error);
444
445         #ifdef SHAREWARE
446                 cfile_init("d2demo.hog");                       //specify name of hogfile
447         #else
448         #define HOGNAME "descent2.hog"
449         if (! cfile_init(HOGNAME)) {            //didn't find HOG.  Check on CD
450                 #ifdef RELEASE
451                         Error("Could not find required file <%s>",HOGNAME);
452                 #endif
453         }
454         #endif
455         
456         load_text();
457
458         //print out the banner title
459         con_printf(CON_NORMAL, "\nDESCENT 2 %s v%d.%d",VERSION_TYPE,Version_major,Version_minor);
460         con_printf(CON_NORMAL, "  %s %s\n", __DATE__,__TIME__);
461         con_printf(CON_NORMAL, "%s\n%s\n",TXT_COPYRIGHT,TXT_TRADEMARK); 
462         con_printf(CON_NORMAL, "This is a MODIFIED version of Descent 2. Copyright (c) 1999 Peter Hawkins\n");
463
464
465         if (FindArg( "-?" ) || FindArg( "-help" ) || FindArg( "?" ) ) {
466                 print_commandline_help();
467                 set_exit_message("");
468                 return(0);
469         }
470
471         con_printf(CON_NORMAL, "\n");
472         con_printf(CON_NORMAL, TXT_HELP, PROGNAME);             //help message has %s for program name
473         con_printf(CON_NORMAL, "\n");
474
475         con_printf(CON_VERBOSE, "\n%s...", "Checking for Descent 2 CD-ROM");
476
477         if ( FindArg( "-autodemo" ))
478                 Auto_demo = 1;
479
480         if ( FindArg( "-noscreens" ) )
481                 Skip_briefing_screens = 1;
482
483         Lighting_on = 1;
484
485 //      if (init_graphics()) return 1;
486
487         #ifdef EDITOR
488         if (!Inferno_is_800x600_available)      {
489                 con_printf(CON_NORMAL "The editor will not be available, press any key to start game...\n" );
490                 Function_mode = FMODE_MENU;
491         }
492         #endif
493
494         if (!WVIDEO_running)
495                 con_printf(CON_DEBUG,"WVIDEO_running = %d\n",WVIDEO_running);
496
497         con_printf (CON_VERBOSE, "%s", TXT_VERBOSE_1);
498         ReadConfigFile();
499
500 #ifdef NETWORK
501         do_network_init();
502 #endif
503
504 #if defined(POLY_ACC)
505     Current_display_mode = -1;
506     game_init_render_buffers(SM_640x480x15xPA, 640, 480, VR_NONE, VRF_COMPATIBLE_MENUS+VRF_ALLOW_COCKPIT );
507 #else
508
509         if (!VR_offscreen_buffer)       //if hasn't been initialied (by headset init)
510                 set_display_mode(0);            //..then set default display mode
511 #endif
512
513         i = FindArg( "-xcontrol" );
514         if ( i > 0 )    {
515                 kconfig_init_external_controls( strtol(Args[i+1], NULL, 0), strtol(Args[i+2], NULL, 0) );
516         }
517
518         con_printf(CON_VERBOSE, "\n%s\n\n", TXT_INITIALIZING_GRAPHICS);
519         if (FindArg("-nofade"))
520                 grd_fades_disabled=1;
521         
522         if ((t=gr_init())!=0)                           //doesn't do much
523                 Error(TXT_CANT_INIT_GFX,t);
524
525    #ifdef _3DFX
526    _3dfx_Init();
527    #endif
528
529         // Load the palette stuff. Returns non-zero if error.
530         con_printf(CON_DEBUG, "\nInitializing palette system..." );
531    gr_use_palette_table(DEFAULT_PALETTE );
532
533         con_printf(CON_DEBUG, "\nInitializing font system..." );
534         gamefont_init();        // must load after palette data loaded.
535
536         //determine whether we're using high-res menus & movies
537 #if !defined(POLY_ACC)
538         if (FindArg("-nohires") || FindArg("-nohighres") || (gr_check_mode(MENU_HIRES_MODE) != 0) || disable_high_res)
539                 MovieHires = MenuHires = MenuHiresAvailable = 0;
540         else
541 #endif
542                 //NOTE LINK TO ABOVE!
543                 MenuHires = MenuHiresAvailable = 1;
544
545         con_printf( CON_DEBUG, "\nInitializing movie libraries..." );
546         init_movies();          //init movie libraries
547
548         con_printf(CON_VERBOSE, "\nGoing into graphics mode...\n");
549 #if defined(POLY_ACC)
550         gr_set_mode(SM_640x480x15xPA);
551 #else
552         gr_set_mode(MovieHires?SM(640,480):SM(320,200));
553 #endif
554
555         #ifndef RELEASE
556         if ( FindArg( "-notitles" ) ) 
557                 songs_play_song( SONG_TITLE, 1);
558         else
559         #endif
560         {       //NOTE LINK TO ABOVE!
561                 int played=MOVIE_NOT_PLAYED;    //default is not played
562                 int song_playing = 0;
563
564                 #ifdef D2_OEM
565                 #define MOVIE_REQUIRED 0
566                 #else
567                 #define MOVIE_REQUIRED 1
568                 #endif
569
570 #ifdef D2_OEM   //$$POLY_ACC, jay.
571                 {       //show bundler screens
572                         FILE *tfile;
573                         char filename[FILENAME_LEN];
574
575                         played=MOVIE_NOT_PLAYED;        //default is not played
576
577             played = PlayMovie("pre_i.mve",0);
578
579                         if (!played) {
580                 strcpy(filename,MenuHires?"pre_i1b.pcx":"pre_i1.pcx");
581
582                                 while ((tfile=fopen(filename,"rb")) != NULL) {
583                                         fclose(tfile);
584                                         show_title_screen( filename, 1, 0 );
585                     filename[5]++;
586                                 }
587                         }
588                 }
589 #endif
590
591                 #ifndef SHAREWARE
592                         init_subtitles("intro.tex");
593                         played = PlayMovie("intro.mve",MOVIE_REQUIRED);
594                         close_subtitles();
595                 #endif
596
597                 #ifdef D2_OEM
598                 if (played != MOVIE_NOT_PLAYED)
599                         intro_played = 1;
600                 else {                                          //didn't get intro movie, try titles
601
602                         played = PlayMovie("titles.mve",MOVIE_REQUIRED);
603
604                         if (played == MOVIE_NOT_PLAYED) {
605 #if defined(POLY_ACC)
606             gr_set_mode(SM_640x480x15xPA);
607 #else
608                                 gr_set_mode(MenuHires?SM_640x480V:SM_320x200C);
609 #endif
610                                 con_printf( CON_DEBUG, "\nPlaying title song..." );
611                                 songs_play_song( SONG_TITLE, 1);
612                                 song_playing = 1;
613                                 con_printf( CON_DEBUG, "\nShowing logo screens..." );
614                                 show_title_screen( MenuHires?"iplogo1b.pcx":"iplogo1.pcx", 1, 1 );
615                                 show_title_screen( MenuHires?"logob.pcx":"logo.pcx", 1, 1 );
616                         }
617                 }
618
619                 {       //show bundler movie or screens
620
621                         FILE *tfile;
622                         char filename[FILENAME_LEN];
623                         int movie_handle;
624
625                         played=MOVIE_NOT_PLAYED;        //default is not played
626
627                         //check if OEM movie exists, so we don't stop the music if it doesn't
628                         movie_handle = open_movie_file("oem.mve",0);
629                         if (movie_handle != -1) {
630                                 close(movie_handle);
631                                 played = PlayMovie("oem.mve",0);
632                                 song_playing = 0;               //movie will kill sound
633                         }
634
635                         if (!played) {
636                                 strcpy(filename,MenuHires?"oem1b.pcx":"oem1.pcx");
637
638                                 while ((tfile=fopen(filename,"rb")) != NULL) {
639                                         fclose(tfile);
640                                         show_title_screen( filename, 1, 0 );
641                                         filename[3]++;
642                                 }
643                         }
644                 }
645                 #endif
646
647                 if (!song_playing)
648                         songs_play_song( SONG_TITLE, 1);
649                         
650         }
651
652    PA_DFX (pa_splash());
653
654         con_printf( CON_DEBUG, "\nShowing loading screen..." );
655         {
656                 //grs_bitmap title_bm;
657                 int pcx_error;
658                 char filename[14];
659
660                 #ifdef SHAREWARE
661                 strcpy(filename, "descentd.pcx");
662                 #else
663                 #ifdef D2_OEM
664                 strcpy(filename, MenuHires?"descntob.pcx":"descento.pcx");
665                 #else
666                 strcpy(filename, MenuHires?"descentb.pcx":"descent.pcx");
667                 #endif
668                 #endif
669
670 #if defined(POLY_ACC)
671                 gr_set_mode(SM_640x480x15xPA);
672 #else
673                 gr_set_mode(MenuHires?SM(640,480):SM(320,200));
674 #endif
675
676                 FontHires = MenuHires;
677
678                 if ((pcx_error=pcx_read_bitmap( filename, &grd_curcanv->cv_bitmap, grd_curcanv->cv_bitmap.bm_type, title_pal ))==PCX_ERROR_NONE)        {
679                         //vfx_set_palette_sub( title_pal );
680                         gr_palette_clear();
681                         gr_palette_fade_in( title_pal, 32, 0 );
682                 } else
683                         Error( "Couldn't load pcx file '%s', PCX load error: %s\n",filename, pcx_errormsg(pcx_error));
684         }
685
686         con_printf( CON_DEBUG , "\nDoing bm_init..." );
687         #ifdef EDITOR
688                 bm_init_use_tbl();
689         #else
690                 bm_init();
691         #endif
692
693         #ifdef EDITOR
694         if (FindArg("-hoarddata") != 0) {
695                 #define MAX_BITMAPS_PER_BRUSH 30
696                 grs_bitmap * bm[MAX_BITMAPS_PER_BRUSH];
697                 grs_bitmap icon;
698                 int nframes;
699                 short nframes_short;
700                 ubyte palette[256*3];
701                 FILE *ofile;
702                 int iff_error,i;
703                 char *sounds[] = {"selforb.raw","selforb.r22",          //SOUND_YOU_GOT_ORB                     
704                                                                 "teamorb.raw","teamorb.r22",            //SOUND_FRIEND_GOT_ORB                  
705                                                                 "enemyorb.raw","enemyorb.r22",  //SOUND_OPPONENT_GOT_ORB        
706                                                                 "OPSCORE1.raw","OPSCORE1.r22"}; //SOUND_OPPONENT_HAS_SCORED
707
708                 ofile = fopen("hoard.ham","wb");
709
710            iff_error = iff_read_animbrush("orb.abm",bm,MAX_BITMAPS_PER_BRUSH,&nframes,palette);
711                 Assert(iff_error == IFF_NO_ERROR);
712                 nframes_short = nframes;
713                 fwrite(&nframes_short,sizeof(nframes_short),1,ofile);
714                 fwrite(&bm[0]->bm_w,sizeof(short),1,ofile);
715                 fwrite(&bm[0]->bm_h,sizeof(short),1,ofile);
716                 fwrite(palette,3,256,ofile);
717                 for (i=0;i<nframes;i++)
718                         fwrite(bm[i]->bm_data,1,bm[i]->bm_w*bm[i]->bm_h,ofile);
719
720            iff_error = iff_read_animbrush("orbgoal.abm",bm,MAX_BITMAPS_PER_BRUSH,&nframes,palette);
721                 Assert(iff_error == IFF_NO_ERROR);
722                 Assert(bm[0]->bm_w == 64 && bm[0]->bm_h == 64);
723                 nframes_short = nframes;
724                 fwrite(&nframes_short,sizeof(nframes_short),1,ofile);
725                 fwrite(palette,3,256,ofile);
726                 for (i=0;i<nframes;i++)
727                         fwrite(bm[i]->bm_data,1,bm[i]->bm_w*bm[i]->bm_h,ofile);
728
729                 for (i=0;i<2;i++) {
730                         iff_error = iff_read_bitmap(i?"orbb.bbm":"orb.bbm",&icon,BM_LINEAR,palette);
731                         Assert(iff_error == IFF_NO_ERROR);
732                         fwrite(&icon.bm_w,sizeof(short),1,ofile);
733                         fwrite(&icon.bm_h,sizeof(short),1,ofile);
734                         fwrite(palette,3,256,ofile);
735                         fwrite(icon.bm_data,1,icon.bm_w*icon.bm_h,ofile);
736                 }
737
738                 for (i=0;i<sizeof(sounds)/sizeof(*sounds);i++) {
739                         FILE *ifile;
740                         int size;
741                         ubyte *buf;
742                         ifile = fopen(sounds[i],"rb");
743                         Assert(ifile != NULL);
744                         size = filelength(ifile->_handle);
745                         buf = d_malloc(size);
746                         fread(buf,1,size,ifile);
747                         fwrite(&size,sizeof(size),1,ofile);
748                         fwrite(buf,1,size,ofile);
749                         d_free(buf);
750                         fclose(ifile);
751                 }
752
753                 fclose(ofile);
754
755                 exit(1);
756         }
757         #endif
758
759         //the bitmap loading code changes gr_palette, so restore it
760         memcpy(gr_palette,title_pal,sizeof(gr_palette));
761
762         if ( FindArg( "-norun" ) )
763                 return(0);
764
765         con_printf( CON_DEBUG, "\nInitializing 3d system..." );
766         g3_init();
767
768         con_printf( CON_DEBUG, "\nInitializing texture caching system..." );
769         texmerge_init( 10 );            // 10 cache bitmaps
770
771         con_printf( CON_DEBUG, "\nRunning game...\n" );
772         set_screen_mode(SCREEN_MENU);
773
774         init_game();
775
776         //      If built with editor, option to auto-load a level and quit game
777         //      to write certain data.
778         #ifdef  EDITOR
779         {       int t;
780         if ( t = FindArg( "-autoload" ) ) {
781                 Auto_exit = 1;
782                 strcpy(Auto_file, Args[t+1]);
783         }
784                 
785         }
786
787         if (Auto_exit) {
788                 strcpy(Players[0].callsign, "dummy");
789         } else
790         #endif
791                 do_register_player(title_pal);
792
793         gr_palette_fade_out( title_pal, 32, 0 );
794
795         Game_mode = GM_GAME_OVER;
796
797         if (Auto_demo)  {
798                 newdemo_start_playback("descent.dem");          
799                 if (Newdemo_state == ND_STATE_PLAYBACK )
800                         Function_mode = FMODE_GAME;
801         }
802
803         //do this here because the demo code can do a longjmp when trying to
804         //autostart a demo from the main menu, never having gone into the game
805         setjmp(LeaveGame);
806
807         while (Function_mode != FMODE_EXIT)
808         {
809                 switch( Function_mode ) {
810                 case FMODE_MENU:
811                         set_screen_mode(SCREEN_MENU);
812                         if ( Auto_demo )        {
813                                 newdemo_start_playback(NULL);           // Randomly pick a file
814                                 if (Newdemo_state != ND_STATE_PLAYBACK) 
815                                         Error("No demo files were found for autodemo mode!");
816                         } else {
817                                 #ifdef EDITOR
818                                 if (Auto_exit) {
819                                         strcpy(&Level_names[0], Auto_file);
820                                         LoadLevel(1, 1);
821                                         Function_mode = FMODE_EXIT;
822                                         break;
823                                 }
824                                 #endif
825
826                                 check_joystick_calibration();
827                                 gr_palette_clear();             //I'm not sure why we need this, but we do
828                                 DoMenu();                                                                               
829                                 #ifdef EDITOR
830                                 if ( Function_mode == FMODE_EDITOR )    {
831                                         create_new_mine();
832                                         SetPlayerFromCurseg();
833                                         load_palette(NULL,1,0);
834                                 }
835                                 #endif
836                         }
837                         break;
838                 case FMODE_GAME:
839                         #ifdef EDITOR
840                                 keyd_editor_mode = 0;
841                         #endif
842
843 #ifdef SDL_INPUT
844                         /* keep the mouse from wandering in SDL/X11 */
845                         if (FindArg("-grabmouse"))
846                             SDL_WM_GrabInput(SDL_GRAB_ON);
847
848 #endif
849
850                         game();
851
852 #ifdef SDL_INPUT
853                         /* give control back to the WM */
854                         if (FindArg("-grabmouse"))
855                             SDL_WM_GrabInput(SDL_GRAB_OFF);
856 #endif
857
858                         if ( Function_mode == FMODE_MENU )
859                                 songs_play_song( SONG_TITLE, 1 );
860                         break;
861                 #ifdef EDITOR
862                 case FMODE_EDITOR:
863                         keyd_editor_mode = 1;
864                         editor();
865                         _harderr( (void*)descent_critical_error_handler );              // Reinstall game error handler
866                         if ( Function_mode == FMODE_GAME ) {
867                                 Game_mode = GM_EDITOR;
868                                 editor_reset_stuff_on_level();
869                                 N_players = 1;
870                         }
871                         break;
872                 #endif
873                 default:
874                         Error("Invalid function mode %d",Function_mode);
875                 }
876         }
877
878         WriteConfigFile();
879
880 #if 0 /* ????? */
881         #ifndef RELEASE
882         if (!FindArg( "-notitles" ))
883         #endif
884 #endif
885
886         #ifndef NDEBUG
887         if ( FindArg( "-showmeminfo" ) )
888                 show_mem_info = 1;              // Make memory statistics show
889         #endif
890
891         return(0);              //presumably successful exit
892 }
893
894
895 void check_joystick_calibration()       {
896         int x1, y1, x2, y2, c;
897         fix t1;
898
899         if ( (Config_control_type!=CONTROL_JOYSTICK) &&
900                   (Config_control_type!=CONTROL_FLIGHTSTICK_PRO) &&
901                   (Config_control_type!=CONTROL_THRUSTMASTER_FCS) &&
902                   (Config_control_type!=CONTROL_GRAVIS_GAMEPAD)
903                 ) return;
904
905         joy_get_pos( &x1, &y1 );
906
907         t1 = timer_get_fixed_seconds();
908         while( timer_get_fixed_seconds() < t1 + F1_0/100 )
909                 ;
910
911         joy_get_pos( &x2, &y2 );
912
913         // If joystick hasn't moved...
914         if ( (abs(x2-x1)<30) &&  (abs(y2-y1)<30) )      {
915                 if ( (abs(x1)>30) || (abs(x2)>30) ||  (abs(y1)>30) || (abs(y2)>30) )    {
916                         c = nm_messagebox( NULL, 2, TXT_CALIBRATE, TXT_SKIP, TXT_JOYSTICK_NOT_CEN );
917                         if ( c==0 )     {
918                                 joydefs_calibrate();
919                         }
920                 }
921         }
922
923 }