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