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