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