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