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