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