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