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