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