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