]> icculus.org git repositories - btb/d2x.git/blob - main/inferno.c
Switched from Cygwin to mingw32 on MS boxes. Vastly improved compilability.
[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         con_init();  // Initialise the console
421         mem_init();
422
423         error_init(NULL, NULL);
424
425         args_init( argc,argv );
426
427         if ( FindArg( "-debug") )
428         {
429                 con_threshold.value = (float)2;
430
431         } else
432                 if ( FindArg( "-verbose" ) ) 
433                 {
434                         con_threshold.value = (float)1;
435                 }
436
437         arch_init_start();
438
439         arch_init();
440
441         //tell cfile about our counter
442         cfile_set_critical_error_counter_ptr(&descent_critical_error);
443
444         #ifdef SHAREWARE
445                 cfile_init("d2demo.hog");                       //specify name of hogfile
446         #else
447         #define HOGNAME "descent2.hog"
448         if (! cfile_init(HOGNAME)) {            //didn't find HOG.  Check on CD
449                 #ifdef RELEASE
450                         Error("Could not find required file <%s>",HOGNAME);
451                 #endif
452         }
453         #endif
454         
455         load_text();
456
457         //print out the banner title
458         con_printf(CON_NORMAL, "\nDESCENT 2 %s v%d.%d",VERSION_TYPE,Version_major,Version_minor);
459         con_printf(CON_NORMAL, "  %s %s\n", __DATE__,__TIME__);
460         con_printf(CON_NORMAL, "%s\n%s\n",TXT_COPYRIGHT,TXT_TRADEMARK); 
461         con_printf(CON_NORMAL, "This is a MODIFIED version of Descent 2. Copyright (c) 1999 Peter Hawkins\n");
462
463
464         if (FindArg( "-?" ) || FindArg( "-help" ) || FindArg( "?" ) ) {
465                 print_commandline_help();
466                 set_exit_message("");
467                 return(0);
468         }
469
470         con_printf(CON_NORMAL, "\n");
471         con_printf(CON_NORMAL, TXT_HELP, PROGNAME);             //help message has %s for program name
472         con_printf(CON_NORMAL, "\n");
473
474         con_printf(CON_VERBOSE, "\n%s...", "Checking for Descent 2 CD-ROM");
475
476         if ( FindArg( "-autodemo" ))
477                 Auto_demo = 1;
478
479         if ( FindArg( "-noscreens" ) )
480                 Skip_briefing_screens = 1;
481
482         Lighting_on = 1;
483
484 //      if (init_graphics()) return 1;
485
486         #ifdef EDITOR
487         if (!Inferno_is_800x600_available)      {
488                 con_printf(CON_NORMAL "The editor will not be available, press any key to start game...\n" );
489                 Function_mode = FMODE_MENU;
490         }
491         #endif
492
493         if (!WVIDEO_running)
494                 con_printf(CON_DEBUG,"WVIDEO_running = %d\n",WVIDEO_running);
495
496         con_printf (CON_VERBOSE, "%s", TXT_VERBOSE_1);
497         ReadConfigFile();
498
499 #ifdef NETWORK
500         do_network_init();
501 #endif
502
503 #if defined(POLY_ACC)
504     Current_display_mode = -1;
505     game_init_render_buffers(SM_640x480x15xPA, 640, 480, VR_NONE, VRF_COMPATIBLE_MENUS+VRF_ALLOW_COCKPIT );
506 #else
507
508         if (!VR_offscreen_buffer)       //if hasn't been initialied (by headset init)
509                 set_display_mode(0);            //..then set default display mode
510 #endif
511
512         i = FindArg( "-xcontrol" );
513         if ( i > 0 )    {
514                 kconfig_init_external_controls( strtol(Args[i+1], NULL, 0), strtol(Args[i+2], NULL, 0) );
515         }
516
517         con_printf(CON_VERBOSE, "\n%s\n\n", TXT_INITIALIZING_GRAPHICS);
518         if (FindArg("-nofade"))
519                 grd_fades_disabled=1;
520         
521         if ((t=gr_init())!=0)                           //doesn't do much
522                 Error(TXT_CANT_INIT_GFX,t);
523
524    #ifdef _3DFX
525    _3dfx_Init();
526    #endif
527
528         // Load the palette stuff. Returns non-zero if error.
529         con_printf(CON_DEBUG, "\nInitializing palette system..." );
530    gr_use_palette_table(DEFAULT_PALETTE );
531
532         con_printf(CON_DEBUG, "\nInitializing font system..." );
533         gamefont_init();        // must load after palette data loaded.
534
535         //determine whether we're using high-res menus & movies
536 #if !defined(POLY_ACC)
537         if (FindArg("-nohires") || FindArg("-nohighres") || (gr_check_mode(MENU_HIRES_MODE) != 0) || disable_high_res)
538                 MovieHires = MenuHires = MenuHiresAvailable = 0;
539         else
540 #endif
541                 //NOTE LINK TO ABOVE!
542                 MenuHires = MenuHiresAvailable = 1;
543
544         con_printf( CON_DEBUG, "\nInitializing movie libraries..." );
545         init_movies();          //init movie libraries
546
547         con_printf(CON_VERBOSE, "\nGoing into graphics mode...\n");
548 #if defined(POLY_ACC)
549         gr_set_mode(SM_640x480x15xPA);
550 #else
551         gr_set_mode(MovieHires?SM(640,480):SM(320,200));
552 #endif
553
554         #ifndef RELEASE
555         if ( FindArg( "-notitles" ) ) 
556                 songs_play_song( SONG_TITLE, 1);
557         else
558         #endif
559         {       //NOTE LINK TO ABOVE!
560                 int played=MOVIE_NOT_PLAYED;    //default is not played
561                 int song_playing = 0;
562
563                 #ifdef D2_OEM
564                 #define MOVIE_REQUIRED 0
565                 #else
566                 #define MOVIE_REQUIRED 1
567                 #endif
568
569 #ifdef D2_OEM   //$$POLY_ACC, jay.
570                 {       //show bundler screens
571                         FILE *tfile;
572                         char filename[FILENAME_LEN];
573
574                         played=MOVIE_NOT_PLAYED;        //default is not played
575
576             played = PlayMovie("pre_i.mve",0);
577
578                         if (!played) {
579                 strcpy(filename,MenuHires?"pre_i1b.pcx":"pre_i1.pcx");
580
581                                 while ((tfile=fopen(filename,"rb")) != NULL) {
582                                         fclose(tfile);
583                                         show_title_screen( filename, 1, 0 );
584                     filename[5]++;
585                                 }
586                         }
587                 }
588 #endif
589
590                 #ifndef SHAREWARE
591                         init_subtitles("intro.tex");
592                         played = PlayMovie("intro.mve",MOVIE_REQUIRED);
593                         close_subtitles();
594                 #endif
595
596                 #ifdef D2_OEM
597                 if (played != MOVIE_NOT_PLAYED)
598                         intro_played = 1;
599                 else {                                          //didn't get intro movie, try titles
600
601                         played = PlayMovie("titles.mve",MOVIE_REQUIRED);
602
603                         if (played == MOVIE_NOT_PLAYED) {
604 #if defined(POLY_ACC)
605             gr_set_mode(SM_640x480x15xPA);
606 #else
607                                 gr_set_mode(MenuHires?SM_640x480V:SM_320x200C);
608 #endif
609                                 con_printf( CON_DEBUG, "\nPlaying title song..." );
610                                 songs_play_song( SONG_TITLE, 1);
611                                 song_playing = 1;
612                                 con_printf( CON_DEBUG, "\nShowing logo screens..." );
613                                 show_title_screen( MenuHires?"iplogo1b.pcx":"iplogo1.pcx", 1, 1 );
614                                 show_title_screen( MenuHires?"logob.pcx":"logo.pcx", 1, 1 );
615                         }
616                 }
617
618                 {       //show bundler movie or screens
619
620                         FILE *tfile;
621                         char filename[FILENAME_LEN];
622                         int movie_handle;
623
624                         played=MOVIE_NOT_PLAYED;        //default is not played
625
626                         //check if OEM movie exists, so we don't stop the music if it doesn't
627                         movie_handle = open_movie_file("oem.mve",0);
628                         if (movie_handle != -1) {
629                                 close(movie_handle);
630                                 played = PlayMovie("oem.mve",0);
631                                 song_playing = 0;               //movie will kill sound
632                         }
633
634                         if (!played) {
635                                 strcpy(filename,MenuHires?"oem1b.pcx":"oem1.pcx");
636
637                                 while ((tfile=fopen(filename,"rb")) != NULL) {
638                                         fclose(tfile);
639                                         show_title_screen( filename, 1, 0 );
640                                         filename[3]++;
641                                 }
642                         }
643                 }
644                 #endif
645
646                 if (!song_playing)
647                         songs_play_song( SONG_TITLE, 1);
648                         
649         }
650
651    PA_DFX (pa_splash());
652
653         con_printf( CON_DEBUG, "\nShowing loading screen..." );
654         {
655                 //grs_bitmap title_bm;
656                 int pcx_error;
657                 char filename[14];
658
659                 #ifdef SHAREWARE
660                 strcpy(filename, "descentd.pcx");
661                 #else
662                 #ifdef D2_OEM
663                 strcpy(filename, MenuHires?"descntob.pcx":"descento.pcx");
664                 #else
665                 strcpy(filename, MenuHires?"descentb.pcx":"descent.pcx");
666                 #endif
667                 #endif
668
669 #if defined(POLY_ACC)
670                 gr_set_mode(SM_640x480x15xPA);
671 #else
672                 gr_set_mode(MenuHires?SM(640,480):SM(320,200));
673 #endif
674
675                 FontHires = MenuHires;
676
677                 if ((pcx_error=pcx_read_bitmap( filename, &grd_curcanv->cv_bitmap, grd_curcanv->cv_bitmap.bm_type, title_pal ))==PCX_ERROR_NONE)        {
678                         //vfx_set_palette_sub( title_pal );
679                         gr_palette_clear();
680                         gr_palette_fade_in( title_pal, 32, 0 );
681                 } else
682                         Error( "Couldn't load pcx file '%s', PCX load error: %s\n",filename, pcx_errormsg(pcx_error));
683         }
684
685         con_printf( CON_DEBUG , "\nDoing bm_init..." );
686         #ifdef EDITOR
687                 bm_init_use_tbl();
688         #else
689                 bm_init();
690         #endif
691
692         #ifdef EDITOR
693         if (FindArg("-hoarddata") != 0) {
694                 #define MAX_BITMAPS_PER_BRUSH 30
695                 grs_bitmap * bm[MAX_BITMAPS_PER_BRUSH];
696                 grs_bitmap icon;
697                 int nframes;
698                 short nframes_short;
699                 ubyte palette[256*3];
700                 FILE *ofile;
701                 int iff_error,i;
702                 char *sounds[] = {"selforb.raw","selforb.r22",          //SOUND_YOU_GOT_ORB                     
703                                                                 "teamorb.raw","teamorb.r22",            //SOUND_FRIEND_GOT_ORB                  
704                                                                 "enemyorb.raw","enemyorb.r22",  //SOUND_OPPONENT_GOT_ORB        
705                                                                 "OPSCORE1.raw","OPSCORE1.r22"}; //SOUND_OPPONENT_HAS_SCORED
706
707                 ofile = fopen("hoard.ham","wb");
708
709            iff_error = iff_read_animbrush("orb.abm",bm,MAX_BITMAPS_PER_BRUSH,&nframes,palette);
710                 Assert(iff_error == IFF_NO_ERROR);
711                 nframes_short = nframes;
712                 fwrite(&nframes_short,sizeof(nframes_short),1,ofile);
713                 fwrite(&bm[0]->bm_w,sizeof(short),1,ofile);
714                 fwrite(&bm[0]->bm_h,sizeof(short),1,ofile);
715                 fwrite(palette,3,256,ofile);
716                 for (i=0;i<nframes;i++)
717                         fwrite(bm[i]->bm_data,1,bm[i]->bm_w*bm[i]->bm_h,ofile);
718
719            iff_error = iff_read_animbrush("orbgoal.abm",bm,MAX_BITMAPS_PER_BRUSH,&nframes,palette);
720                 Assert(iff_error == IFF_NO_ERROR);
721                 Assert(bm[0]->bm_w == 64 && bm[0]->bm_h == 64);
722                 nframes_short = nframes;
723                 fwrite(&nframes_short,sizeof(nframes_short),1,ofile);
724                 fwrite(palette,3,256,ofile);
725                 for (i=0;i<nframes;i++)
726                         fwrite(bm[i]->bm_data,1,bm[i]->bm_w*bm[i]->bm_h,ofile);
727
728                 for (i=0;i<2;i++) {
729                         iff_error = iff_read_bitmap(i?"orbb.bbm":"orb.bbm",&icon,BM_LINEAR,palette);
730                         Assert(iff_error == IFF_NO_ERROR);
731                         fwrite(&icon.bm_w,sizeof(short),1,ofile);
732                         fwrite(&icon.bm_h,sizeof(short),1,ofile);
733                         fwrite(palette,3,256,ofile);
734                         fwrite(icon.bm_data,1,icon.bm_w*icon.bm_h,ofile);
735                 }
736
737                 for (i=0;i<sizeof(sounds)/sizeof(*sounds);i++) {
738                         FILE *ifile;
739                         int size;
740                         ubyte *buf;
741                         ifile = fopen(sounds[i],"rb");
742                         Assert(ifile != NULL);
743                         size = filelength(ifile->_handle);
744                         buf = d_malloc(size);
745                         fread(buf,1,size,ifile);
746                         fwrite(&size,sizeof(size),1,ofile);
747                         fwrite(buf,1,size,ofile);
748                         d_free(buf);
749                         fclose(ifile);
750                 }
751
752                 fclose(ofile);
753
754                 exit(1);
755         }
756         #endif
757
758         //the bitmap loading code changes gr_palette, so restore it
759         memcpy(gr_palette,title_pal,sizeof(gr_palette));
760
761         if ( FindArg( "-norun" ) )
762                 return(0);
763
764         con_printf( CON_DEBUG, "\nInitializing 3d system..." );
765         g3_init();
766
767         con_printf( CON_DEBUG, "\nInitializing texture caching system..." );
768         texmerge_init( 10 );            // 10 cache bitmaps
769
770         con_printf( CON_DEBUG, "\nRunning game...\n" );
771         set_screen_mode(SCREEN_MENU);
772
773         init_game();
774
775         //      If built with editor, option to auto-load a level and quit game
776         //      to write certain data.
777         #ifdef  EDITOR
778         {       int t;
779         if ( t = FindArg( "-autoload" ) ) {
780                 Auto_exit = 1;
781                 strcpy(Auto_file, Args[t+1]);
782         }
783                 
784         }
785
786         if (Auto_exit) {
787                 strcpy(Players[0].callsign, "dummy");
788         } else
789         #endif
790                 do_register_player(title_pal);
791
792         gr_palette_fade_out( title_pal, 32, 0 );
793
794         Game_mode = GM_GAME_OVER;
795
796         if (Auto_demo)  {
797                 newdemo_start_playback("descent.dem");          
798                 if (Newdemo_state == ND_STATE_PLAYBACK )
799                         Function_mode = FMODE_GAME;
800         }
801
802         //do this here because the demo code can do a longjmp when trying to
803         //autostart a demo from the main menu, never having gone into the game
804         setjmp(LeaveGame);
805
806         while (Function_mode != FMODE_EXIT)
807         {
808                 switch( Function_mode ) {
809                 case FMODE_MENU:
810                         set_screen_mode(SCREEN_MENU);
811                         if ( Auto_demo )        {
812                                 newdemo_start_playback(NULL);           // Randomly pick a file
813                                 if (Newdemo_state != ND_STATE_PLAYBACK) 
814                                         Error("No demo files were found for autodemo mode!");
815                         } else {
816                                 #ifdef EDITOR
817                                 if (Auto_exit) {
818                                         strcpy(&Level_names[0], Auto_file);
819                                         LoadLevel(1, 1);
820                                         Function_mode = FMODE_EXIT;
821                                         break;
822                                 }
823                                 #endif
824
825                                 check_joystick_calibration();
826                                 gr_palette_clear();             //I'm not sure why we need this, but we do
827                                 DoMenu();                                                                               
828                                 #ifdef EDITOR
829                                 if ( Function_mode == FMODE_EDITOR )    {
830                                         create_new_mine();
831                                         SetPlayerFromCurseg();
832                                         load_palette(NULL,1,0);
833                                 }
834                                 #endif
835                         }
836                         break;
837                 case FMODE_GAME:
838                         #ifdef EDITOR
839                                 keyd_editor_mode = 0;
840                         #endif
841
842 #ifdef SDL_INPUT
843                         /* keep the mouse from wandering in SDL/X11 */
844                         if (FindArg("-grabmouse"))
845                             SDL_WM_GrabInput(SDL_GRAB_ON);
846
847 #endif
848
849                         game();
850
851 #ifdef SDL_INPUT
852                         /* give control back to the WM */
853                         if (FindArg("-grabmouse"))
854                             SDL_WM_GrabInput(SDL_GRAB_OFF);
855 #endif
856
857                         if ( Function_mode == FMODE_MENU )
858                                 songs_play_song( SONG_TITLE, 1 );
859                         break;
860                 #ifdef EDITOR
861                 case FMODE_EDITOR:
862                         keyd_editor_mode = 1;
863                         editor();
864                         _harderr( (void*)descent_critical_error_handler );              // Reinstall game error handler
865                         if ( Function_mode == FMODE_GAME ) {
866                                 Game_mode = GM_EDITOR;
867                                 editor_reset_stuff_on_level();
868                                 N_players = 1;
869                         }
870                         break;
871                 #endif
872                 default:
873                         Error("Invalid function mode %d",Function_mode);
874                 }
875         }
876
877         WriteConfigFile();
878
879 #if 0 /* ????? */
880         #ifndef RELEASE
881         if (!FindArg( "-notitles" ))
882         #endif
883 #endif
884
885         #ifndef NDEBUG
886         if ( FindArg( "-showmeminfo" ) )
887                 show_mem_info = 1;              // Make memory statistics show
888         #endif
889
890         return(0);              //presumably successful exit
891 }
892
893
894 void check_joystick_calibration()       {
895         int x1, y1, x2, y2, c;
896         fix t1;
897
898         if ( (Config_control_type!=CONTROL_JOYSTICK) &&
899                   (Config_control_type!=CONTROL_FLIGHTSTICK_PRO) &&
900                   (Config_control_type!=CONTROL_THRUSTMASTER_FCS) &&
901                   (Config_control_type!=CONTROL_GRAVIS_GAMEPAD)
902                 ) return;
903
904         joy_get_pos( &x1, &y1 );
905
906         t1 = timer_get_fixed_seconds();
907         while( timer_get_fixed_seconds() < t1 + F1_0/100 )
908                 ;
909
910         joy_get_pos( &x2, &y2 );
911
912         // If joystick hasn't moved...
913         if ( (abs(x2-x1)<30) &&  (abs(y2-y1)<30) )      {
914                 if ( (abs(x1)>30) || (abs(x2)>30) ||  (abs(y1)>30) || (abs(y2)>30) )    {
915                         c = nm_messagebox( NULL, 2, TXT_CALIBRATE, TXT_SKIP, TXT_JOYSTICK_NOT_CEN );
916                         if ( c==0 )     {
917                                 joydefs_calibrate();
918                         }
919                 }
920         }
921
922 }