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