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