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