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