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