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