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