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