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