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