]> icculus.org git repositories - btb/d2x.git/blob - main/inferno.c
added TODO file
[btb/d2x.git] / main / inferno.c
1 /* $Id: inferno.c,v 1.42 2002-08-30 00:57:32 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 #if 0
697         con_printf(CON_VERBOSE, "Going into graphics mode...\n");
698 #if defined(POLY_ACC)
699         gr_set_mode(SM_640x480x15xPA);
700 #else
701         gr_set_mode(MovieHires?SM(640,480):SM(320,200));
702 #endif
703 #endif
704
705         #ifndef RELEASE
706         if ( FindArg( "-notitles" ) ) 
707                 songs_play_song( SONG_TITLE, 1);
708         else
709         #endif
710         {       //NOTE LINK TO ABOVE!
711 #ifndef SHAREWARE
712                 int played=MOVIE_NOT_PLAYED;    //default is not played
713 #endif
714                 int song_playing = 0;
715
716                 #ifdef D2_OEM
717                 #define MOVIE_REQUIRED 0
718                 #else
719                 #define MOVIE_REQUIRED 1
720                 #endif
721
722 #ifdef D2_OEM   //$$POLY_ACC, jay.
723                 {       //show bundler screens
724                         FILE *tfile;
725                         char filename[FILENAME_LEN];
726
727                         played=MOVIE_NOT_PLAYED;        //default is not played
728
729             played = PlayMovie("pre_i.mve",0);
730
731                         if (!played) {
732                 strcpy(filename,MenuHires?"pre_i1b.pcx":"pre_i1.pcx");
733
734                                 while ((tfile=fopen(filename,"rb")) != NULL) {
735                                         fclose(tfile);
736                                         show_title_screen( filename, 1, 0 );
737                     filename[5]++;
738                                 }
739                         }
740                 }
741 #endif
742
743                 #ifndef SHAREWARE
744                         init_subtitles("intro.tex");
745                         played = PlayMovie("intro.mve",MOVIE_REQUIRED);
746                         close_subtitles();
747                 #endif
748
749                 #ifdef D2_OEM
750                 if (played != MOVIE_NOT_PLAYED)
751                         intro_played = 1;
752                 else {                                          //didn't get intro movie, try titles
753
754                         played = PlayMovie("titles.mve",MOVIE_REQUIRED);
755
756                         if (played == MOVIE_NOT_PLAYED) {
757 #if defined(POLY_ACC)
758                                 gr_set_mode(SM_640x480x15xPA);
759 #else
760                                 gr_set_mode(MenuHires?SM_640x480V:SM_320x200C);
761 #endif
762                                 con_printf( CON_DEBUG, "\nPlaying title song..." );
763                                 songs_play_song( SONG_TITLE, 1);
764                                 song_playing = 1;
765                                 con_printf( CON_DEBUG, "\nShowing logo screens..." );
766                                 show_title_screen( MenuHires?"iplogo1b.pcx":"iplogo1.pcx", 1, 1 );
767                                 show_title_screen( MenuHires?"logob.pcx":"logo.pcx", 1, 1 );
768                         }
769                 }
770
771                 {       //show bundler movie or screens
772
773                         FILE *tfile;
774                         char filename[FILENAME_LEN];
775                         int movie_handle;
776
777                         played=MOVIE_NOT_PLAYED;        //default is not played
778
779                         //check if OEM movie exists, so we don't stop the music if it doesn't
780                         movie_handle = open_movie_file("oem.mve",0);
781                         if (movie_handle != -1) {
782                                 close(movie_handle);
783                                 played = PlayMovie("oem.mve",0);
784                                 song_playing = 0;               //movie will kill sound
785                         }
786
787                         if (!played) {
788                                 strcpy(filename,MenuHires?"oem1b.pcx":"oem1.pcx");
789
790                                 while ((tfile=fopen(filename,"rb")) != NULL) {
791                                         fclose(tfile);
792                                         show_title_screen( filename, 1, 0 );
793                                         filename[3]++;
794                                 }
795                         }
796                 }
797                 #endif
798
799                 if (!song_playing)
800                         songs_play_song( SONG_TITLE, 1);
801                         
802         }
803
804         PA_DFX (pa_splash());
805
806         con_printf( CON_DEBUG, "\nShowing loading screen..." );
807         {
808                 //grs_bitmap title_bm;
809                 int pcx_error;
810                 char filename[14];
811
812                 strcpy(filename, MenuHires?"descentb.pcx":"descent.pcx");
813                 if (! cfexist(filename))
814                         strcpy(filename, MenuHires?"descntob.pcx":"descento.pcx"); // OEM
815                 if (! cfexist(filename))
816                         strcpy(filename, "descentd.pcx"); // SHAREWARE
817
818 #if defined(POLY_ACC)
819                 gr_set_mode(SM_640x480x15xPA);
820 #else
821                 gr_set_mode(MenuHires?SM(640,480):SM(320,200));
822 #endif
823
824                 FontHires = FontHiresAvailable && MenuHires;
825
826                 if ((pcx_error=pcx_read_fullscr( filename, title_pal ))==PCX_ERROR_NONE)        {
827                         //vfx_set_palette_sub( title_pal );
828                         gr_palette_clear();
829                         gr_palette_fade_in( title_pal, 32, 0 );
830                 } else
831                         Error( "Couldn't load pcx file '%s', PCX load error: %s\n",filename, pcx_errormsg(pcx_error));
832         }
833
834         con_printf( CON_DEBUG , "\nDoing bm_init..." );
835         #ifdef EDITOR
836                 bm_init_use_tbl();
837         #else
838                 bm_init();
839         #endif
840
841         #ifdef EDITOR
842         if (FindArg("-hoarddata") != 0) {
843                 #define MAX_BITMAPS_PER_BRUSH 30
844                 grs_bitmap * bm[MAX_BITMAPS_PER_BRUSH];
845                 grs_bitmap icon;
846                 int nframes;
847                 short nframes_short;
848                 ubyte palette[256*3];
849                 FILE *ofile;
850                 int iff_error,i;
851                 char *sounds[] = {"selforb.raw","selforb.r22",          //SOUND_YOU_GOT_ORB                     
852                                                                 "teamorb.raw","teamorb.r22",            //SOUND_FRIEND_GOT_ORB                  
853                                                                 "enemyorb.raw","enemyorb.r22",  //SOUND_OPPONENT_GOT_ORB        
854                                                                 "OPSCORE1.raw","OPSCORE1.r22"}; //SOUND_OPPONENT_HAS_SCORED
855
856                 ofile = fopen("hoard.ham","wb");
857
858            iff_error = iff_read_animbrush("orb.abm",bm,MAX_BITMAPS_PER_BRUSH,&nframes,palette);
859                 Assert(iff_error == IFF_NO_ERROR);
860                 nframes_short = nframes;
861                 fwrite(&nframes_short,sizeof(nframes_short),1,ofile);
862                 fwrite(&bm[0]->bm_w,sizeof(short),1,ofile);
863                 fwrite(&bm[0]->bm_h,sizeof(short),1,ofile);
864                 fwrite(palette,3,256,ofile);
865                 for (i=0;i<nframes;i++)
866                         fwrite(bm[i]->bm_data,1,bm[i]->bm_w*bm[i]->bm_h,ofile);
867
868                 iff_error = iff_read_animbrush("orbgoal.abm",bm,MAX_BITMAPS_PER_BRUSH,&nframes,palette);
869                 Assert(iff_error == IFF_NO_ERROR);
870                 Assert(bm[0]->bm_w == 64 && bm[0]->bm_h == 64);
871                 nframes_short = nframes;
872                 fwrite(&nframes_short,sizeof(nframes_short),1,ofile);
873                 fwrite(palette,3,256,ofile);
874                 for (i=0;i<nframes;i++)
875                         fwrite(bm[i]->bm_data,1,bm[i]->bm_w*bm[i]->bm_h,ofile);
876
877                 for (i=0;i<2;i++) {
878                         iff_error = iff_read_bitmap(i?"orbb.bbm":"orb.bbm",&icon,BM_LINEAR,palette);
879                         Assert(iff_error == IFF_NO_ERROR);
880                         fwrite(&icon.bm_w,sizeof(short),1,ofile);
881                         fwrite(&icon.bm_h,sizeof(short),1,ofile);
882                         fwrite(palette,3,256,ofile);
883                         fwrite(icon.bm_data,1,icon.bm_w*icon.bm_h,ofile);
884                 }
885
886                 for (i=0;i<sizeof(sounds)/sizeof(*sounds);i++) {
887                         FILE *ifile;
888                         int size;
889                         ubyte *buf;
890                         ifile = fopen(sounds[i],"rb");
891                         Assert(ifile != NULL);
892                         size = ffilelength(ifile);
893                         buf = d_malloc(size);
894                         fread(buf,1,size,ifile);
895                         fwrite(&size,sizeof(size),1,ofile);
896                         fwrite(buf,1,size,ofile);
897                         d_free(buf);
898                         fclose(ifile);
899                 }
900
901                 fclose(ofile);
902
903                 exit(1);
904         }
905         #endif
906
907         //the bitmap loading code changes gr_palette, so restore it
908         memcpy(gr_palette,title_pal,sizeof(gr_palette));
909
910         if ( FindArg( "-norun" ) )
911                 return(0);
912
913         con_printf( CON_DEBUG, "\nInitializing 3d system..." );
914         g3_init();
915
916         con_printf( CON_DEBUG, "\nInitializing texture caching system..." );
917         texmerge_init( 10 );            // 10 cache bitmaps
918
919         con_printf( CON_DEBUG, "\nRunning game...\n" );
920         set_screen_mode(SCREEN_MENU);
921
922         init_game();
923
924         //      If built with editor, option to auto-load a level and quit game
925         //      to write certain data.
926         #ifdef  EDITOR
927         {       int t;
928         if ( (t = FindArg( "-autoload" )) ) {
929                 Auto_exit = 1;
930                 strcpy(Auto_file, Args[t+1]);
931         }
932                 
933         }
934
935         if (Auto_exit) {
936                 strcpy(Players[0].callsign, "dummy");
937         } else
938         #endif
939                 do_register_player(title_pal);
940
941         gr_palette_fade_out( title_pal, 32, 0 );
942
943         Game_mode = GM_GAME_OVER;
944
945         if (Auto_demo)  {
946                 newdemo_start_playback("descent.dem");          
947                 if (Newdemo_state == ND_STATE_PLAYBACK )
948                         Function_mode = FMODE_GAME;
949         }
950
951         //do this here because the demo code can do a longjmp when trying to
952         //autostart a demo from the main menu, never having gone into the game
953         setjmp(LeaveGame);
954
955         while (Function_mode != FMODE_EXIT)
956         {
957                 switch( Function_mode ) {
958                 case FMODE_MENU:
959                         set_screen_mode(SCREEN_MENU);
960                         if ( Auto_demo )        {
961                                 newdemo_start_playback(NULL);           // Randomly pick a file
962                                 if (Newdemo_state != ND_STATE_PLAYBACK) 
963                                         Error("No demo files were found for autodemo mode!");
964                         } else {
965                                 #ifdef EDITOR
966                                 if (Auto_exit) {
967                                         strcpy((char *)&Level_names[0], Auto_file);
968                                         LoadLevel(1, 1);
969                                         Function_mode = FMODE_EXIT;
970                                         break;
971                                 }
972                                 #endif
973
974                                 check_joystick_calibration();
975                                 gr_palette_clear();             //I'm not sure why we need this, but we do
976                                 DoMenu();                                                                               
977                                 #ifdef EDITOR
978                                 if ( Function_mode == FMODE_EDITOR )    {
979                                         create_new_mine();
980                                         SetPlayerFromCurseg();
981                                         load_palette(NULL,1,0);
982                                 }
983                                 #endif
984                         }
985                         break;
986                 case FMODE_GAME:
987                         #ifdef EDITOR
988                                 keyd_editor_mode = 0;
989                         #endif
990
991 #ifdef SDL_INPUT
992                         /* keep the mouse from wandering in SDL */
993                         if (FindArg("-grabmouse"))
994                             SDL_WM_GrabInput(SDL_GRAB_ON);
995 #endif
996
997                         game();
998
999 #ifdef SDL_INPUT
1000                         /* give control back to the WM */
1001                         if (FindArg("-grabmouse"))
1002                             SDL_WM_GrabInput(SDL_GRAB_OFF);
1003 #endif
1004
1005                         if ( Function_mode == FMODE_MENU )
1006                                 songs_play_song( SONG_TITLE, 1 );
1007                         break;
1008                 #ifdef EDITOR
1009                 case FMODE_EDITOR:
1010                         keyd_editor_mode = 1;
1011                         editor();
1012 #ifdef __WATCOMC__
1013                         _harderr( (void*)descent_critical_error_handler );              // Reinstall game error handler
1014 #endif
1015                         if ( Function_mode == FMODE_GAME ) {
1016                                 Game_mode = GM_EDITOR;
1017                                 editor_reset_stuff_on_level();
1018                                 N_players = 1;
1019                         }
1020                         break;
1021                 #endif
1022                 default:
1023                         Error("Invalid function mode %d",Function_mode);
1024                 }
1025         }
1026
1027         WriteConfigFile();
1028
1029 #if 0 /* ????? */
1030         #ifndef RELEASE
1031         if (!FindArg( "-notitles" ))
1032         #endif
1033 #endif
1034
1035         #ifndef NDEBUG
1036         if ( FindArg( "-showmeminfo" ) )
1037                 show_mem_info = 1;              // Make memory statistics show
1038         #endif
1039
1040         return(0);              //presumably successful exit
1041 }
1042
1043
1044 void check_joystick_calibration()       {
1045         int x1, y1, x2, y2, c;
1046         fix t1;
1047
1048         if ( (Config_control_type!=CONTROL_JOYSTICK) &&
1049                   (Config_control_type!=CONTROL_FLIGHTSTICK_PRO) &&
1050                   (Config_control_type!=CONTROL_THRUSTMASTER_FCS) &&
1051                   (Config_control_type!=CONTROL_GRAVIS_GAMEPAD)
1052                 ) return;
1053
1054         joy_get_pos( &x1, &y1 );
1055
1056         t1 = timer_get_fixed_seconds();
1057         while( timer_get_fixed_seconds() < t1 + F1_0/100 )
1058                 ;
1059
1060         joy_get_pos( &x2, &y2 );
1061
1062         // If joystick hasn't moved...
1063         if ( (abs(x2-x1)<30) &&  (abs(y2-y1)<30) )      {
1064                 if ( (abs(x1)>30) || (abs(x2)>30) ||  (abs(y1)>30) || (abs(y2)>30) )    {
1065                         c = nm_messagebox( NULL, 2, TXT_CALIBRATE, TXT_SKIP, TXT_JOYSTICK_NOT_CEN );
1066                         if ( c==0 )     {
1067                                 joydefs_calibrate();
1068                         }
1069                 }
1070         }
1071
1072 }
1073
1074 void show_order_form()
1075 {
1076 #if !defined(EDITOR) && (defined(SHAREWARE) || defined(D2_OEM))
1077
1078         int pcx_error;
1079         char title_pal[768];
1080         char    exit_screen[16];
1081
1082         gr_set_current_canvas( NULL );
1083         gr_palette_clear();
1084
1085         key_flush();
1086
1087         #ifdef D2_OEM
1088                 strcpy(exit_screen, MenuHires?"ordrd2ob.pcx":"ordrd2o.pcx");
1089         #else
1090         #if defined(SHAREWARE)
1091                 strcpy(exit_screen, "orderd2.pcx");
1092         #else
1093                 strcpy(exit_screen, MenuHires?"warningb.pcx":"warning.pcx");
1094         #endif
1095         #endif
1096
1097         if ((pcx_error=pcx_read_fullscr( exit_screen, title_pal ))==PCX_ERROR_NONE) {
1098                 //vfx_set_palette_sub( title_pal );
1099                 gr_palette_fade_in( title_pal, 32, 0 );
1100                 key_getch();
1101                 gr_palette_fade_out( title_pal, 32, 0 );
1102         }
1103         else
1104                 Int3();         //can't load order screen
1105
1106         key_flush();
1107
1108 #endif
1109 }
1110
1111 void quit_request()
1112 {
1113 #ifdef NETWORK
1114 //      void network_abort_game();
1115 //      if(Network_status)
1116 //              network_abort_game();
1117 #endif
1118         exit(0);
1119 }