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