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