]> icculus.org git repositories - btb/d2x.git/blob - main/inferno.c
fix bugfix of 2004-05-15
[btb/d2x.git] / main / inferno.c
1 /* $Id: inferno.c,v 1.89 2004-08-29 17:57:23 schaffner Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 /*
16  *
17  * inferno.c: Entry point of program (main procedure)
18  *
19  * After main initializes everything, most of the time is spent in the loop
20  * while (Function_mode != FMODE_EXIT)
21  * In this loop, the main menu is brought up first.
22  *
23  * main() for Inferno
24  *
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include <conf.h>
29 #endif
30
31 char copyright[] = "DESCENT II  COPYRIGHT (C) 1994-1996 PARALLAX SOFTWARE CORPORATION";
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <limits.h>
37
38 #ifdef __unix__
39 #include <unistd.h>
40 #include <sys/stat.h>
41 #include <sys/types.h>
42 #endif
43
44 #include "pstypes.h"
45 #include "strutil.h"
46 #include "console.h"
47 #include "pa_enabl.h"       //$$POLY_ACC
48 #include "gr.h"
49 #include "fix.h"
50 #include "vecmat.h"
51 #include "mono.h"
52 #include "key.h"
53 #include "timer.h"
54 #include "3d.h"
55 #include "bm.h"
56 #include "inferno.h"
57 #include "error.h"
58 #include "game.h"
59 #include "segment.h"            //for Side_to_verts
60 #include "u_mem.h"
61 #include "segpoint.h"
62 #include "screens.h"
63 #include "texmap.h"
64 #include "texmerge.h"
65 #include "menu.h"
66 #include "wall.h"
67 #include "polyobj.h"
68 #include "effects.h"
69 #include "digi.h"
70 #include "iff.h"
71 #include "pcx.h"
72 #include "palette.h"
73 #include "args.h"
74 #include "sounds.h"
75 #include "titles.h"
76 #include "player.h"
77 #include "text.h"
78 #include "newdemo.h"
79 #ifdef NETWORK
80 #include "network.h"
81 #include "modem.h"
82 #endif
83 #include "gamefont.h"
84 #include "kconfig.h"
85 #include "mouse.h"
86 #include "joy.h"
87 #include "newmenu.h"
88 #include "desc_id.h"
89 #include "config.h"
90 #include "joydefs.h"
91 #include "multi.h"
92 #include "songs.h"
93 #include "cfile.h"
94 #include "gameseq.h"
95 #include "gamepal.h"
96 #include "mission.h"
97 #include "movie.h"
98 #include "compbit.h"
99 #include "d_io.h"
100
101 // #  include "3dfx_des.h"
102
103 //added on 9/30/98 by Matt Mueller for selectable automap modes
104 #include "automap.h"
105 //end addition -MM
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 #endif
119
120 #ifndef __MSDOS__
121 #include <SDL.h>
122 #endif
123
124 #include "vers_id.h"
125
126 void mem_init(void);
127 void arch_init(void);
128 void arch_init_start(void);
129
130 //Current version number
131
132 ubyte Version_major = 1;                //FULL VERSION
133 ubyte Version_minor = 2;
134
135 //static const char desc_id_checksum_str[] = DESC_ID_CHKSUM_TAG "0000"; // 4-byte checksum
136 char desc_id_exit_num = 0;
137
138 int Function_mode=FMODE_MENU;           //game or editor?
139 int Screen_mode=-1;                                     //game screen or editor screen?
140
141 //--unused-- grs_bitmap Inferno_bitmap_title;
142
143 int WVIDEO_running=0;           //debugger can set to 1 if running
144
145 #ifdef EDITOR
146 int Inferno_is_800x600_available = 0;
147 #endif
148
149 //--unused-- int Cyberman_installed=0;                  // SWIFT device present
150 ubyte CybermouseActive=0;
151
152 int __far descent_critical_error_handler( unsigned deverr, unsigned errcode, unsigned __far * devhdr );
153
154 void check_joystick_calibration(void);
155
156 void show_order_form(void);
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
177 #define LINE_LEN        100
178
179 //read help from a file & print to screen
180 void print_commandline_help()
181 {
182         CFILE *ifile;
183         int have_binary=0;
184         char line[LINE_LEN];
185
186         ifile = cfopen("help.tex","rb");
187         if (!ifile) {
188                 ifile = cfopen("help.txb","rb");
189                 if (!ifile)
190                         Warning("Cannot load help text file.");
191                 have_binary = 1;
192         }
193
194         if (ifile)
195         {
196         char *end;
197         
198                 while (end = cfgets(line,LINE_LEN,ifile)) {
199
200             // This is the only use of cfgets that needs the CR
201             *end++ = '\n';
202                         if (have_binary) {
203                                 int i;
204                                 for (i = 0; i < strlen(line) - 1; i++) {
205                                         encode_rotate_left(&(line[i]));
206                                         line[i] = line[i] ^ BITMAP_TBL_XOR;
207                                         encode_rotate_left(&(line[i]));
208                                 }
209                         }
210
211                         if (line[0] == ';')
212                                 continue;               //don't show comments
213
214                         printf("%s",line);
215
216                 }
217
218                 cfclose(ifile);
219
220         }
221
222 //      printf( " Diagnostic:\n\n");
223 //      printf( "  -emul           %s\n", "Certain video cards need this option in order to run game");
224 //      printf( "  -ddemul         %s\n", "If -emul doesn't work, use this option");
225 //      printf( "\n");
226 #ifdef EDITOR
227         printf( " Editor Options:\n\n");
228         printf( "  -autoload <file>%s\n", "Autoload a level in the editor");
229         printf( "  -hoarddata      %s\n","FIXME: Undocumented");
230 //      printf( "  -nobm           %s\n","FIXME: Undocumented");
231         printf( "\n");
232 #endif
233         printf( " D2X Options:\n\n");
234         printf( "  -noredundancy   %s\n", "Do not send messages when picking up redundant items in multi");
235         printf( "  -shortpackets   %s\n", "Set shortpackets to default as on");
236 #ifdef OGL // currently only does anything on ogl build, so don't advertise othewise.
237         printf("  -renderstats    %s\n", "Enable renderstats info by default");
238 #endif
239         printf( "  -maxfps <n>     %s\n", "Set maximum framerate (1-100)");
240         printf( "  -notitles       %s\n", "Do not show titlescreens on startup");
241         printf( "  -hogdir <dir>   %s\n", "set shared data directory to <dir>");
242 #ifdef __unix__
243         printf( "  -nohogdir       %s\n", "don't try to use shared data directory");
244         printf( "  -userdir <dir>  %s\n", "set user dir to <dir> instead of $HOME/.d2x");
245 #endif
246         printf( "  -ini <file>     %s\n", "option file (alternate to command line), defaults to d2x.ini");
247         printf( "  -autodemo       %s\n", "Start in demo mode");
248         printf( "  -bigpig         %s\n","FIXME: Undocumented");
249         printf( "  -bspgen         %s\n","FIXME: Undocumented");
250 //      printf( "  -cdproxy        %s\n","FIXME: Undocumented");
251 #ifndef NDEBUG
252         printf( "  -checktime      %s\n","FIXME: Undocumented");
253         printf( "  -showmeminfo    %s\n","FIXME: Undocumented");
254 #endif
255 //      printf( "  -codereadonly   %s\n","FIXME: Undocumented");
256 //      printf( "  -cyberimpact    %s\n","FIXME: Undocumented");
257         printf( "  -debug          %s\n","Enable very verbose output");
258 //      printf( "  -debugmode      %s\n","FIXME: Undocumented");
259 //      printf( "  -disallowgfx    %s\n","FIXME: Undocumented");
260 //      printf( "  -disallowreboot %s\n","FIXME: Undocumented");
261 //      printf( "  -dynamicsockets %s\n","FIXME: Undocumented");
262 //      printf( "  -forcegfx       %s\n","FIXME: Undocumented");
263 #ifdef SDL_INPUT
264         printf( "  -grabmouse      %s\n","Keeps the mouse from wandering out of the window");
265 #endif
266 //      printf( "  -hw_3dacc       %s\n","FIXME: Undocumented");
267 #ifndef RELEASE
268         printf( "  -invulnerability %s\n","Make yourself invulnerable");
269 #endif
270         printf( "  -ipxnetwork <num> %s\n","Use IPX network number <num>");
271         printf( "  -jasen          %s\n","FIXME: Undocumented");
272         printf( "  -joyslow        %s\n","FIXME: Undocumented");
273 #ifdef NETWORK
274         printf( "  -kali           %s\n","use Kali for networking");
275 #endif
276 //      printf( "  -logfile        %s\n","FIXME: Undocumented");
277         printf( "  -lowresmovies   %s\n","Play low resolution movies if available (for slow machines)");
278 #if defined(EDITOR) || !defined(MACDATA)
279         printf( "  -macdata        %s\n","Read (and, for editor, write) mac data files (swap colors)");
280 #endif
281 //      printf( "  -memdbg         %s\n","FIXME: Undocumented");
282 //      printf( "  -monodebug      %s\n","FIXME: Undocumented");
283         printf( "  -nocdrom        %s\n","FIXME: Undocumented");
284 #ifdef __DJGPP__
285         printf( "  -nocyberman     %s\n","FIXME: Undocumented");
286 #endif
287 #ifndef NDEBUG
288         printf( "  -nofade         %s\n","Disable fades");
289 #endif
290 #ifdef NETWORK
291         printf( "  -nomatrixcheat  %s\n","FIXME: Undocumented");
292         printf( "  -norankings     %s\n","Disable multiplayer ranking system");
293         printf( "  -packets <num>  %s\n","Specifies the number of packets per second\n");
294 //      printf( "  -showaddress    %s\n","FIXME: Undocumented");
295         printf( "  -socket         %s\n","FIXME: Undocumented");
296 #endif
297 #if !defined(MACINTOSH) && !defined(WINDOWS)
298         printf( "  -nomixer        %s\n","Don't crank music volume");
299 //      printf( "  -superhires     %s\n","Allow higher-resolution modes");
300 #endif
301 //      printf( "  -nomodex        %s\n","FIXME: Undocumented");
302 #ifndef RELEASE
303         printf( "  -nomovies       %s\n","Don't play movies");
304         printf( "  -noscreens      %s\n","Skip briefing screens");
305 #endif
306 #if !defined(SHAREWARE) || ( defined(SHAREWARE) && defined(APPLE_DEMO) )
307         printf( "  -noredbook      %s\n","Disable redbook audio");
308 #endif
309         printf( "  -norun          %s\n","Bail out after initialization");
310 //      printf( "  -ordinaljoy     %s\n","FIXME: Undocumented");
311 //      printf( "  -rtscts         %s\n","Same as -ctsrts");
312 //      printf( "  -semiwin        %s\n","Use non-fullscreen mode");
313 //      printf( "  -specialdevice  %s\n","FIXME: Undocumented");
314 #ifdef TACTILE
315         printf( "  -stickmag       %s\n","FIXME: Undocumented");
316 #endif
317 //      printf( "  -stopwatch      %s\n","FIXME: Undocumented");
318         printf( "  -subtitles      %s\n","Turn on movie subtitles (English-only)");
319 //      printf( "  -sysram         %s\n","FIXME: Undocumented");
320         printf( "  -text <file>    %s\n","Specify alternate .tex file");
321 //      printf( "  -tsengdebug1    %s\n","FIXME: Undocumented");
322 //      printf( "  -tsengdebug2    %s\n","FIXME: Undocumented");
323 //      printf( "  -tsengdebug3    %s\n","FIXME: Undocumented");
324 //      printf( "  -vidram         %s\n","FIXME: Undocumented");
325         printf( "  -xcontrol       %s\n","FIXME: Undocumented");
326         printf( "  -xname          %s\n","FIXME: Undocumented");
327         printf( "  -xver           %s\n","FIXME: Undocumented");
328         printf( "  -tmap <t>       %s\n","select texmapper to use (c,fp,i386,pent,ppro)");
329 #ifdef __MSDOS__
330         printf( "  -<X>x<Y>        %s\n", "Change screen resolution. Options:");
331         printf( "                     320x100;320x200;320x240;320x400;640x400;640x480;800x600;1024x768\n");
332 #else
333         printf( "  -<X>x<Y>        %s\n", "Change screen resolution to <X> by <Y>");
334 #endif
335         printf("  -niceautomap    %s\n", "Free cpu while doing automap");
336         printf( "  -automap<X>x<Y> %s\n","Set automap resolution to <X> by <Y>");
337         printf( "  -automap_gameres %s\n","Set automap to use the same resolution as in game");
338 //      printf( "  -menu<X>x<Y>    %s\n","Set menu resolution to <X> by <Y>");
339 //      printf( "  -menu_gameres   %s\n","Set menus to use the same resolution as in game");
340         printf("  -rearviewtime t %s\n", "time holding rearview key to use toggle mode (default 0.0625 seconds)");
341         printf( "\n");
342
343         printf( "D2X System Options:\n\n");
344 #ifdef __MSDOS__
345         printf("  -ihaveabrokenmouse %s\n", "try to make mouse work if it is not currently");
346         printf( "  -joy209         %s\n", "Use alternate port 209 for joystick");
347 #endif
348 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
349         printf( "  -fullscreen     %s\n", "Use fullscreen mode if available");
350 #endif
351 #ifdef OGL
352         printf( "  -gl_texmagfilt <f> %s\n","set GL_TEXTURE_MAG_FILTER (see readme.d1x)");
353         printf( "  -gl_texminfilt <f> %s\n","set GL_TEXTURE_MIN_FILTER (see readme.d1x)");
354         printf("  -gl_mipmap      %s\n", "set gl texture filters to \"standard\" (bilinear) mipmapping");
355         printf("  -gl_trilinear   %s\n", "set gl texture filters to trilinear mipmapping");
356         printf( "  -gl_simple      %s\n","set gl texture filters to gl_nearest for \"original\" look. (default)");
357         printf("  -gl_anisotropy <f> %s\n", "set maximum degree of anisotropy to <f>");
358         printf( "  -gl_alttexmerge %s\n","use new texmerge, usually uses less ram (default)");
359         printf( "  -gl_stdtexmerge %s\n","use old texmerge, uses more ram, but _might_ be a bit faster");
360 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
361         printf( "  -gl_voodoo      %s\n","force fullscreen mode only");
362 #endif
363         printf( "  -gl_16bittextures %s\n","attempt to use 16bit textures");
364         printf("  -gl_16bpp       %s\n", "attempt to use 16bit screen mode");
365         printf( "  -gl_reticle <r> %s\n","use OGL reticle 0=never 1=above 320x* 2=always");
366         printf( "  -gl_intensity4_ok %s\n","FIXME: Undocumented");
367         printf( "  -gl_luminance4_alpha4_ok %s\n","FIXME: Undocumented");
368         printf( "  -gl_readpixels_ok %s\n","FIXME: Undocumented");
369         printf( "  -gl_rgba2_ok    %s\n","FIXME: Undocumented");
370 //      printf( "  -gl_test1       %s\n","FIXME: Undocumented");
371         printf( "  -gl_test2       %s\n","FIXME: Undocumented");
372         printf( "  -gl_vidmem      %s\n","FIXME: Undocumented");
373 #ifdef OGL_RUNTIME_LOAD
374         printf( "  -gl_library <l> %s\n","use alternate opengl library");
375 #endif
376 #ifdef WGL_VIDEO
377         printf("  -gl_refresh <r> %s\n", "set refresh rate (in fullscreen mode)");
378 #endif
379 #endif
380 #ifdef SDL_VIDEO
381         printf( "  -nosdlvidmodecheck %s\n", "Some X servers don't like checking vidmode first, so just switch");
382         printf( "  -hwsurface      %s\n","FIXME: Undocumented");
383 #endif
384 #ifdef NETWORK
385         printf("  -udp            %s\n", "Specify options for udp/ip:");
386         printf("    @<shift>      %s\n", "  shift udp port base offset");
387         printf("    =<HOST_LIST>  %s\n", "  broadcast both local and to HOST_LIST");
388         printf("    +<HOST_LIST>  %s\n", "  broadcast only to HOST_LIST");
389         printf("                  %s\n", "   HOSTS can be any IP or hostname")
390                 ;
391         printf("                  %s\n", "   HOSTS can also be in the form of <address>:<shift>");
392         printf("                  %s\n", "   separate multiple HOSTS with a ,");
393 #endif
394 #ifdef __unix__
395         printf( "  -serialdevice <s> %s\n", "Set serial/modem device to <s>");
396         printf( "  -serialread <r> %s\n", "Set serial/modem to read from <r>");
397 #endif
398         printf( "\n Help:\n\n");
399         printf( "  -help, -h, -?, ? %s\n", "View this help screen");
400         printf( "\n");
401 }
402
403 void do_joystick_init()
404 {
405
406         if (!FindArg( "-nojoystick" ))  {
407                 con_printf(CON_VERBOSE, "\n%s", TXT_VERBOSE_6);
408                 joy_init();
409                 if ( FindArg( "-joyslow" ))     {
410                         con_printf(CON_VERBOSE, "\n%s", TXT_VERBOSE_7);
411                         joy_set_slow_reading(JOY_SLOW_READINGS);
412                 }
413                 if ( FindArg( "-joypolled" ))   {
414                         con_printf(CON_VERBOSE, "\n%s", TXT_VERBOSE_8);
415                         joy_set_slow_reading(JOY_POLLED_READINGS);
416                 }
417                 if ( FindArg( "-joybios" ))     {
418                         con_printf(CON_VERBOSE, "\n%s", TXT_VERBOSE_9);
419                         joy_set_slow_reading(JOY_BIOS_READINGS);
420                 }
421
422         //      Added from Descent v1.5 by John.  Adapted by Samir.
423         } else {
424                 con_printf(CON_VERBOSE, "\n%s", TXT_VERBOSE_10);
425         }
426 }
427
428 //set this to force game to run in low res
429 int disable_high_res=0;
430
431 void do_register_player(ubyte *title_pal)
432 {
433         Players[Player_num].callsign[0] = '\0';
434
435         if (!Auto_demo)         {
436
437                 key_flush();
438
439                 //now, before we bring up the register player menu, we need to
440                 //do some stuff to make sure the palette is ok.  First, we need to
441                 //get our current palette into the 2d's array, so the remapping will
442                 //work.  Second, we need to remap the fonts.  Third, we need to fill
443                 //in part of the fade tables so the darkening of the menu edges works
444
445                 memcpy(gr_palette,title_pal,sizeof(gr_palette));
446                 remap_fonts_and_menus(1);
447                 RegisterPlayer();               //get player's name
448         }
449
450 }
451
452 #define PROGNAME argv[0]
453
454 extern char Language[];
455
456 //can we do highres menus?
457 extern int MenuHiresAvailable;
458
459 int intro_played = 0;
460
461 int Inferno_verbose = 0;
462
463 //added on 11/18/98 by Victor Rachels to add -mission and -startgame
464 int start_net_immediately = 0;
465 //int start_with_mission = 0;
466 //char *start_with_mission_name;
467 //end this section addition
468
469 int open_movie_file(char *filename,int must_have);
470
471 #if defined(POLY_ACC)
472 #define MENU_HIRES_MODE SM_640x480x15xPA
473 #else
474 #define MENU_HIRES_MODE SM(640,480)
475 #endif
476
477 //      DESCENT II by Parallax Software
478 //              Descent Main
479
480 //extern ubyte gr_current_pal[];
481
482 #ifdef  EDITOR
483 int     Auto_exit = 0;
484 char    Auto_file[128] = "";
485 #endif
486
487 int main(int argc, char *argv[])
488 {
489         int i, t;
490         ubyte title_pal[768];
491
492         con_init();  // Initialise the console
493         mem_init();
494
495         error_init(NULL, NULL);
496
497         InitArgs( argc,argv );
498
499 #ifdef __unix__
500         {
501                 char *home = getenv("HOME");
502
503                 if ((t = FindArg("-userdir")))
504                         chdir(Args[t+1]);
505
506                 else if (home) {
507                         char buf[PATH_MAX + 5];
508
509                         strcpy(buf, home);
510                         strcat(buf, "/.d2x");
511                         if (chdir(buf)) {
512                                 mkdir(buf, 0755);
513                                 if (chdir(buf))
514                                         fprintf(stderr, "Cannot change to $HOME/.d2x\n");
515                         }
516                 }
517         }
518 #endif
519
520         if (FindArg("-debug"))
521                 con_threshold.value = (float)2;
522         else if (FindArg("-verbose"))
523                 con_threshold.value = (float)1;
524
525         //tell cfile where hogdir is
526         if ((t=FindArg("-hogdir")))
527                 cfile_use_alternate_hogdir(Args[t+1]);
528 #ifdef __unix__
529         else if (!FindArg("-nohogdir"))
530                 cfile_use_alternate_hogdir(SHAREPATH);
531 #endif
532
533         //tell cfile about our counter
534         cfile_set_critical_error_counter_ptr(&descent_critical_error);
535
536         if (! cfile_init("descent2.hog"))
537                 if (! cfile_init("d2demo.hog"))
538                         Warning("Could not find a valid hog file (descent2.hog or d2demo.hog)\nPossible locations are:\n"
539 #ifdef __unix__
540                               "\t$HOME/.d2x\n"
541                               "\t" SHAREPATH "\n"
542 #else
543                                   "\tCurrent directory\n"
544 #endif
545                                   "Or use the -hogdir option to specify an alternate location.");
546         load_text();
547
548         //print out the banner title
549         con_printf(CON_NORMAL, "\nDESCENT 2 %s v%d.%d",VERSION_TYPE,Version_major,Version_minor);
550         #ifdef VERSION_NAME
551         con_printf(CON_NORMAL, "  %s", VERSION_NAME);
552         #endif
553         if (cfexist(MISSION_DIR "d2x.hog"))
554                 con_printf(CON_NORMAL, "  Vertigo Enhanced");
555
556         con_printf(CON_NORMAL, "  %s %s\n", __DATE__,__TIME__);
557         con_printf(CON_NORMAL, "%s\n%s\n",TXT_COPYRIGHT,TXT_TRADEMARK);
558         con_printf(CON_NORMAL, "This is a MODIFIED version of Descent 2. Copyright (c) 1999 Peter Hawkins\n");
559         con_printf(CON_NORMAL, "                                         Copyright (c) 2002 Bradley Bell\n");
560
561
562         if (FindArg( "-?" ) || FindArg( "-help" ) || FindArg( "?" ) || FindArg( "-h" ) ) {
563                 print_commandline_help();
564                 set_exit_message("");
565 #ifdef __MINGW32__
566                 exit(0);  /* mingw hangs on this return.  dunno why */
567 #endif
568                 return(0);
569         }
570
571         con_printf(CON_NORMAL, "\n");
572         con_printf(CON_NORMAL, TXT_HELP, PROGNAME);             //help message has %s for program name
573         con_printf(CON_NORMAL, "\n");
574
575         //(re)added Mar 30, 2003 Micah Lieske - Allow use of 22K sound samples again.
576         if(FindArg("-sound22k"))
577         {
578                 digi_sample_rate = SAMPLE_RATE_22K;
579         }
580
581         if(FindArg("-sound11k"))
582         {
583                 digi_sample_rate = SAMPLE_RATE_11K;
584         }
585
586         arch_init_start();
587
588         arch_init();
589
590         //con_printf(CON_VERBOSE, "\n%s...", "Checking for Descent 2 CD-ROM");
591
592         if ((t = FindArg("-rearviewtime")))
593         {
594                 float f = atof(Args[t + 1]);
595                 Rear_view_leave_time = f * f1_0;
596         }
597         con_printf(CON_VERBOSE, "Rear_view_leave_time=0x%x (%f sec)\n", Rear_view_leave_time, Rear_view_leave_time / (float)f1_0);
598
599         //added/edited 8/18/98 by Victor Rachels to set maximum fps <= 100
600         if ((t = FindArg( "-maxfps" ))) {
601                 t=atoi(Args[t+1]);
602                 if (t>0&&t<=80)
603                         maxfps=t;
604         }
605         //end addition - Victor Rachels
606
607 #ifdef SUPPORTS_NICEFPS
608         if (FindArg("-nicefps"))
609                 use_nice_fps = 1;
610         if (FindArg("-niceautomap"))
611                 nice_automap = 1;
612 #endif
613
614         if (FindArg("-renderstats"))
615                 gr_renderstats = 1;
616
617         if ( FindArg( "-autodemo" ))
618                 Auto_demo = 1;
619
620 #ifndef RELEASE
621         if ( FindArg( "-noscreens" ) )
622                 Skip_briefing_screens = 1;
623 #endif
624
625         if ((t=FindArg("-tmap"))){
626                 select_tmap(Args[t+1]);
627         }else
628                 select_tmap(NULL);
629
630         Lighting_on = 1;
631
632 //      if (init_graphics()) return 1;
633
634         #ifdef EDITOR
635         if (!Inferno_is_800x600_available)      {
636                 con_printf(CON_NORMAL, "The editor will not be available, press any key to start game...\n" );
637                 Function_mode = FMODE_MENU;
638         }
639         #endif
640
641         if (!WVIDEO_running)
642                 con_printf(CON_DEBUG,"WVIDEO_running = %d\n",WVIDEO_running);
643
644         con_printf (CON_VERBOSE, "%s", TXT_VERBOSE_1);
645         ReadConfigFile();
646
647         do_joystick_init();
648
649 #if defined(POLY_ACC)
650     Current_display_mode = -1;
651     game_init_render_buffers(SM_640x480x15xPA, 640, 480, VR_NONE, VRF_COMPATIBLE_MENUS+VRF_ALLOW_COCKPIT );
652 #else
653
654         if (!VR_offscreen_buffer)       //if hasn't been initialied (by headset init)
655                 set_display_mode(0);            //..then set default display mode
656 #endif
657
658         {
659                 int screen_width = 640;
660                 int screen_height = 480;
661                 int vr_mode = VR_NONE;
662                 int screen_flags = VRF_USE_PAGING;
663
664                 if (FindResArg("", &screen_width, &screen_height))
665                 {
666                         /* stuff below mirrors values from display_mode_info in
667                          * menu.c which is used by set_display_mode. In fact,
668                          * set_display_mode should probably be rewritten to allow
669                          * arbitrary resolutions, and then we get rid of this
670                          * stuff here.
671                          */
672                         switch (SM(screen_width, screen_height))
673                         {
674                         case SM(320, 200):
675                         case SM(640, 480):
676                                 screen_flags = VRF_ALLOW_COCKPIT + VRF_COMPATIBLE_MENUS;
677                                 break;
678                         case SM(320, 400):
679                                 screen_flags = VRF_USE_PAGING;
680                                 break;
681                         case SM(640, 400):
682                         case SM(800, 600):
683                         case SM(1024, 768):
684                         case SM(1280, 1024):
685                                 screen_flags = VRF_COMPATIBLE_MENUS;
686                                 break;
687                         }
688
689                         con_printf(CON_VERBOSE, "Using %ix%i ...\n", screen_width, screen_height);
690                 }
691
692 // added ifdef on 9/30/98 by Matt Mueller to fix high res in linux
693 #ifdef __MSDOS__
694                 if (FindArg("-nodoublebuffer"))
695 #endif
696 // end addition -MM
697                 {
698                         con_printf(CON_VERBOSE, "Double-buffering disabled...\n");
699                         screen_flags &= ~VRF_USE_PAGING;
700                 }
701
702                 // added 3/24/99 by Owen Evans for screen res changing
703                 Game_screen_mode = SM(screen_width, screen_height);
704                 // end added -OE
705                 game_init_render_buffers(Game_screen_mode, screen_width, screen_height, vr_mode, screen_flags);
706
707         }
708         {
709 // added/edited on 12/14/98 by Matt Mueller - override res in d1x.ini with command line args
710                 int i, argnum = INT_MAX, w, h;
711 // added on 9/30/98 by Matt Mueller for selectable automap modes - edited 11/21/99 whee, more fun with defines. - edited 03/31/02 to use new FindResArg.
712 #define SMODE(V,VV,VG) if ((i=FindResArg(#V, &w, &h)) && (i < argnum)) { argnum = i; VV = SM(w, h); VG = 0; }
713 #define SMODE_GR(V,VG) if ((i=FindArg("-" #V "_gameres"))){if (i<argnum) VG=1;}
714 #define SMODE_PRINT(V,VV,VG) if (VG) con_printf(CON_VERBOSE, #V " using game resolution ...\n"); else con_printf(CON_VERBOSE, #V " using %ix%i ...\n",SM_W(VV),SM_H(VV) );
715 // aren't #defines great? :)
716 // end addition/edit -MM
717 #define S_MODE(V,VV,VG) argnum = INT_MAX; SMODE(V, VV, VG); SMODE_GR(V, VG); SMODE_PRINT(V, VV, VG);
718
719                 S_MODE(automap,automap_mode,automap_use_game_res);
720 //              S_MODE(menu,menu_screen_mode,menu_use_game_res);
721          }
722 //end addition -MM
723
724         i = FindArg( "-xcontrol" );
725         if ( i > 0 )    {
726                 kconfig_init_external_controls( strtol(Args[i+1], NULL, 0), strtol(Args[i+2], NULL, 0) );
727         }
728
729         con_printf(CON_VERBOSE, "\n%s\n\n", TXT_INITIALIZING_GRAPHICS);
730         if (FindArg("-nofade"))
731                 grd_fades_disabled=1;
732
733         //determine whether we're using high-res menus & movies
734 #if !defined(POLY_ACC)
735         if (FindArg("-nohires") || FindArg("-nohighres") || (gr_check_mode(MENU_HIRES_MODE) != 0) || disable_high_res)
736                 MovieHires = MenuHires = MenuHiresAvailable = 0;
737         else
738 #endif
739                 //NOTE LINK TO ABOVE!
740                 MenuHires = MenuHiresAvailable = 1;
741
742         if (FindArg( "-lowresmovies" ))
743                 MovieHires = 0;
744
745         if ((t=gr_init())!=0)                           //doesn't do much
746                 Error(TXT_CANT_INIT_GFX,t);
747
748    #ifdef _3DFX
749    _3dfx_Init();
750    #endif
751
752         // Load the palette stuff. Returns non-zero if error.
753         con_printf(CON_DEBUG, "Initializing palette system...\n" );
754         gr_use_palette_table(DEFAULT_PALETTE );
755
756         con_printf(CON_DEBUG, "Initializing font system...\n" );
757         gamefont_init();        // must load after palette data loaded.
758
759         con_printf( CON_DEBUG, "Initializing movie libraries...\n" );
760         init_movies();          //init movie libraries
761
762 #if 0
763         con_printf(CON_VERBOSE, "Going into graphics mode...\n");
764 #if defined(POLY_ACC)
765         gr_set_mode(SM_640x480x15xPA);
766 #else
767         gr_set_mode(MovieHires?SM(640,480):SM(320,200));
768 #endif
769 #endif
770
771         #ifndef RELEASE
772         if ( FindArg( "-notitles" ) )
773                 songs_play_song( SONG_TITLE, 1);
774         else
775         #endif
776         {       //NOTE LINK TO ABOVE!
777 #ifndef SHAREWARE
778                 int played=MOVIE_NOT_PLAYED;    //default is not played
779 #endif
780                 int song_playing = 0;
781
782                 #ifdef D2_OEM
783                 #define MOVIE_REQUIRED 0
784                 #else
785                 #define MOVIE_REQUIRED 1
786                 #endif
787
788 #ifdef D2_OEM   //$$POLY_ACC, jay.
789                 {       //show bundler screens
790                         char filename[FILENAME_LEN];
791
792                         played=MOVIE_NOT_PLAYED;        //default is not played
793
794             played = PlayMovie("pre_i.mve",0);
795
796                         if (!played) {
797                 strcpy(filename,MenuHires?"pre_i1b.pcx":"pre_i1.pcx");
798
799                                 while (cfexist(filename))
800                                 {
801                                         show_title_screen( filename, 1, 0 );
802                     filename[5]++;
803                                 }
804                         }
805                 }
806 #endif
807
808                 #ifndef SHAREWARE
809                         init_subtitles("intro.tex");
810                         played = PlayMovie("intro.mve",MOVIE_REQUIRED);
811                         close_subtitles();
812                 #endif
813
814                 #ifdef D2_OEM
815                 if (played != MOVIE_NOT_PLAYED)
816                         intro_played = 1;
817                 else {                                          //didn't get intro movie, try titles
818
819                         played = PlayMovie("titles.mve",MOVIE_REQUIRED);
820
821                         if (played == MOVIE_NOT_PLAYED) {
822 #if defined(POLY_ACC)
823                                 gr_set_mode(SM_640x480x15xPA);
824 #else
825                                 gr_set_mode(MenuHires?SM_640x480V:SM_320x200C);
826 #endif
827                                 con_printf( CON_DEBUG, "\nPlaying title song..." );
828                                 songs_play_song( SONG_TITLE, 1);
829                                 song_playing = 1;
830                                 con_printf( CON_DEBUG, "\nShowing logo screens..." );
831                                 show_title_screen( MenuHires?"iplogo1b.pcx":"iplogo1.pcx", 1, 1 );
832                                 show_title_screen( MenuHires?"logob.pcx":"logo.pcx", 1, 1 );
833                         }
834                 }
835
836                 {       //show bundler movie or screens
837
838                         char filename[FILENAME_LEN];
839                         int movie_handle;
840
841                         played=MOVIE_NOT_PLAYED;        //default is not played
842
843                         //check if OEM movie exists, so we don't stop the music if it doesn't
844                         movie_handle = open_movie_file("oem.mve",0);
845                         if (movie_handle != -1) {
846                                 close(movie_handle);
847                                 played = PlayMovie("oem.mve",0);
848                                 song_playing = 0;               //movie will kill sound
849                         }
850
851                         if (!played) {
852                                 strcpy(filename,MenuHires?"oem1b.pcx":"oem1.pcx");
853
854                                 while (cfexist(filename))
855                                 {
856                                         show_title_screen( filename, 1, 0 );
857                                         filename[3]++;
858                                 }
859                         }
860                 }
861                 #endif
862
863                 if (!song_playing)
864                         songs_play_song( SONG_TITLE, 1);
865                         
866         }
867
868         PA_DFX (pa_splash());
869
870         con_printf( CON_DEBUG, "\nShowing loading screen..." );
871         {
872                 //grs_bitmap title_bm;
873                 int pcx_error;
874                 char filename[14];
875
876                 strcpy(filename, MenuHires?"descentb.pcx":"descent.pcx");
877                 if (! cfexist(filename))
878                         strcpy(filename, MenuHires?"descntob.pcx":"descento.pcx"); // OEM
879                 if (! cfexist(filename))
880                         strcpy(filename, "descentd.pcx"); // SHAREWARE
881                 if (! cfexist(filename))
882                         strcpy(filename, "descentb.pcx"); // MAC SHAREWARE
883
884 #if defined(POLY_ACC)
885                 gr_set_mode(SM_640x480x15xPA);
886 #else
887                 gr_set_mode(MenuHires?SM(640,480):SM(320,200));
888 #endif
889 #ifdef OGL
890                 set_screen_mode(SCREEN_MENU);
891 #endif
892
893                 FontHires = FontHiresAvailable && MenuHires;
894
895                 if ((pcx_error=pcx_read_fullscr( filename, title_pal ))==PCX_ERROR_NONE)        {
896                         //vfx_set_palette_sub( title_pal );
897                         gr_palette_clear();
898                         gr_palette_fade_in( title_pal, 32, 0 );
899                 } else
900                         Error( "Couldn't load pcx file '%s', PCX load error: %s\n",filename, pcx_errormsg(pcx_error));
901         }
902
903         con_printf( CON_DEBUG , "\nDoing bm_init..." );
904         #ifdef EDITOR
905                 bm_init_use_tbl();
906         #else
907                 bm_init();
908         #endif
909
910         #ifdef EDITOR
911         if (FindArg("-hoarddata") != 0) {
912                 #define MAX_BITMAPS_PER_BRUSH 30
913                 grs_bitmap * bm[MAX_BITMAPS_PER_BRUSH];
914                 grs_bitmap icon;
915                 int nframes;
916                 short nframes_short;
917                 ubyte palette[256*3];
918                 FILE *ofile;
919                 int iff_error,i;
920                 char *sounds[] = {"selforb.raw","selforb.r22",          //SOUND_YOU_GOT_ORB                     
921                                                                 "teamorb.raw","teamorb.r22",            //SOUND_FRIEND_GOT_ORB                  
922                                                                 "enemyorb.raw","enemyorb.r22",  //SOUND_OPPONENT_GOT_ORB        
923                                                                 "OPSCORE1.raw","OPSCORE1.r22"}; //SOUND_OPPONENT_HAS_SCORED
924
925                 ofile = fopen("hoard.ham","wb");
926
927            iff_error = iff_read_animbrush("orb.abm",bm,MAX_BITMAPS_PER_BRUSH,&nframes,palette);
928                 Assert(iff_error == IFF_NO_ERROR);
929                 nframes_short = nframes;
930                 fwrite(&nframes_short,sizeof(nframes_short),1,ofile);
931                 fwrite(&bm[0]->bm_w,sizeof(short),1,ofile);
932                 fwrite(&bm[0]->bm_h,sizeof(short),1,ofile);
933                 fwrite(palette,3,256,ofile);
934                 for (i=0;i<nframes;i++)
935                         fwrite(bm[i]->bm_data,1,bm[i]->bm_w*bm[i]->bm_h,ofile);
936
937                 iff_error = iff_read_animbrush("orbgoal.abm",bm,MAX_BITMAPS_PER_BRUSH,&nframes,palette);
938                 Assert(iff_error == IFF_NO_ERROR);
939                 Assert(bm[0]->bm_w == 64 && bm[0]->bm_h == 64);
940                 nframes_short = nframes;
941                 fwrite(&nframes_short,sizeof(nframes_short),1,ofile);
942                 fwrite(palette,3,256,ofile);
943                 for (i=0;i<nframes;i++)
944                         fwrite(bm[i]->bm_data,1,bm[i]->bm_w*bm[i]->bm_h,ofile);
945
946                 for (i=0;i<2;i++) {
947                         iff_error = iff_read_bitmap(i?"orbb.bbm":"orb.bbm",&icon,BM_LINEAR,palette);
948                         Assert(iff_error == IFF_NO_ERROR);
949                         fwrite(&icon.bm_w,sizeof(short),1,ofile);
950                         fwrite(&icon.bm_h,sizeof(short),1,ofile);
951                         fwrite(palette,3,256,ofile);
952                         fwrite(icon.bm_data,1,icon.bm_w*icon.bm_h,ofile);
953                 }
954
955                 for (i=0;i<sizeof(sounds)/sizeof(*sounds);i++) {
956                         FILE *ifile;
957                         int size;
958                         ubyte *buf;
959                         ifile = fopen(sounds[i],"rb");
960                         Assert(ifile != NULL);
961                         size = ffilelength(ifile);
962                         buf = d_malloc(size);
963                         fread(buf,1,size,ifile);
964                         fwrite(&size,sizeof(size),1,ofile);
965                         fwrite(buf,1,size,ofile);
966                         d_free(buf);
967                         fclose(ifile);
968                 }
969
970                 fclose(ofile);
971
972                 exit(1);
973         }
974         #endif
975
976         //the bitmap loading code changes gr_palette, so restore it
977         memcpy(gr_palette,title_pal,sizeof(gr_palette));
978
979         if ( FindArg( "-norun" ) )
980                 return(0);
981
982         con_printf( CON_DEBUG, "\nInitializing 3d system..." );
983         g3_init();
984
985         con_printf( CON_DEBUG, "\nInitializing texture caching system..." );
986         texmerge_init( 10 );            // 10 cache bitmaps
987
988         con_printf( CON_DEBUG, "\nRunning game...\n" );
989         set_screen_mode(SCREEN_MENU);
990
991         init_game();
992
993         //      If built with editor, option to auto-load a level and quit game
994         //      to write certain data.
995         #ifdef  EDITOR
996         {       int t;
997         if ( (t = FindArg( "-autoload" )) ) {
998                 Auto_exit = 1;
999                 strcpy(Auto_file, Args[t+1]);
1000         }
1001                 
1002         }
1003
1004         if (Auto_exit) {
1005                 strcpy(Players[0].callsign, "dummy");
1006         } else
1007         #endif
1008                 do_register_player(title_pal);
1009
1010         gr_palette_fade_out( title_pal, 32, 0 );
1011
1012         Game_mode = GM_GAME_OVER;
1013
1014         if (Auto_demo)  {
1015                 newdemo_start_playback("descent.dem");          
1016                 if (Newdemo_state == ND_STATE_PLAYBACK )
1017                         Function_mode = FMODE_GAME;
1018         }
1019
1020         //do this here because the demo code can do a longjmp when trying to
1021         //autostart a demo from the main menu, never having gone into the game
1022         setjmp(LeaveGame);
1023
1024         while (Function_mode != FMODE_EXIT)
1025         {
1026                 switch( Function_mode ) {
1027                 case FMODE_MENU:
1028                         set_screen_mode(SCREEN_MENU);
1029                         if ( Auto_demo )        {
1030                                 newdemo_start_playback(NULL);           // Randomly pick a file
1031                                 if (Newdemo_state != ND_STATE_PLAYBACK) 
1032                                         Error("No demo files were found for autodemo mode!");
1033                         } else {
1034                                 #ifdef EDITOR
1035                                 if (Auto_exit) {
1036                                         strcpy((char *)&Level_names[0], Auto_file);
1037                                         LoadLevel(1, 1);
1038                                         Function_mode = FMODE_EXIT;
1039                                         break;
1040                                 }
1041                                 #endif
1042
1043                                 check_joystick_calibration();
1044                                 gr_palette_clear();             //I'm not sure why we need this, but we do
1045                                 DoMenu();                                                                               
1046                                 #ifdef EDITOR
1047                                 if ( Function_mode == FMODE_EDITOR )    {
1048                                         create_new_mine();
1049                                         SetPlayerFromCurseg();
1050                                         load_palette(NULL,1,0);
1051                                 }
1052                                 #endif
1053                         }
1054                         break;
1055                 case FMODE_GAME:
1056                         #ifdef EDITOR
1057                                 keyd_editor_mode = 0;
1058                         #endif
1059
1060 #ifdef SDL_INPUT
1061                         /* keep the mouse from wandering in SDL */
1062                         if (FindArg("-grabmouse"))
1063                             SDL_WM_GrabInput(SDL_GRAB_ON);
1064 #endif
1065
1066                         game();
1067
1068 #ifdef SDL_INPUT
1069                         /* give control back to the WM */
1070                         if (FindArg("-grabmouse"))
1071                             SDL_WM_GrabInput(SDL_GRAB_OFF);
1072 #endif
1073
1074                         if ( Function_mode == FMODE_MENU )
1075                                 songs_play_song( SONG_TITLE, 1 );
1076                         break;
1077                 #ifdef EDITOR
1078                 case FMODE_EDITOR:
1079                         keyd_editor_mode = 1;
1080                         editor();
1081 #ifdef __WATCOMC__
1082                         _harderr( (void*)descent_critical_error_handler );              // Reinstall game error handler
1083 #endif
1084                         if ( Function_mode == FMODE_GAME ) {
1085                                 Game_mode = GM_EDITOR;
1086                                 editor_reset_stuff_on_level();
1087                                 N_players = 1;
1088                         }
1089                         break;
1090                 #endif
1091                 default:
1092                         Error("Invalid function mode %d",Function_mode);
1093                 }
1094         }
1095
1096         WriteConfigFile();
1097
1098         #ifndef RELEASE
1099         if (!FindArg( "-notitles" ))
1100         #endif
1101                 show_order_form();
1102
1103         #ifndef NDEBUG
1104         if ( FindArg( "-showmeminfo" ) )
1105                 show_mem_info = 1;              // Make memory statistics show
1106         #endif
1107
1108         return(0);              //presumably successful exit
1109 }
1110
1111
1112 void check_joystick_calibration()       {
1113         int x1, y1, x2, y2, c;
1114         fix t1;
1115
1116         if ( (Config_control_type!=CONTROL_JOYSTICK) &&
1117                   (Config_control_type!=CONTROL_FLIGHTSTICK_PRO) &&
1118                   (Config_control_type!=CONTROL_THRUSTMASTER_FCS) &&
1119                   (Config_control_type!=CONTROL_GRAVIS_GAMEPAD)
1120                 ) return;
1121
1122         joy_get_pos( &x1, &y1 );
1123
1124         t1 = timer_get_fixed_seconds();
1125         while( timer_get_fixed_seconds() < t1 + F1_0/100 )
1126                 ;
1127
1128         joy_get_pos( &x2, &y2 );
1129
1130         // If joystick hasn't moved...
1131         if ( (abs(x2-x1)<30) &&  (abs(y2-y1)<30) )      {
1132                 if ( (abs(x1)>30) || (abs(x2)>30) ||  (abs(y1)>30) || (abs(y2)>30) )    {
1133                         c = nm_messagebox( NULL, 2, TXT_CALIBRATE, TXT_SKIP, TXT_JOYSTICK_NOT_CEN );
1134                         if ( c==0 )     {
1135                                 joydefs_calibrate();
1136                         }
1137                 }
1138         }
1139
1140 }
1141
1142 void show_order_form()
1143 {
1144 #ifndef EDITOR
1145
1146         int pcx_error;
1147         unsigned char title_pal[768];
1148         char    exit_screen[16];
1149
1150         gr_set_current_canvas( NULL );
1151         gr_palette_clear();
1152
1153         key_flush();
1154
1155         strcpy(exit_screen, MenuHires?"ordrd2ob.pcx":"ordrd2o.pcx"); // OEM
1156         if (! cfexist(exit_screen))
1157                 strcpy(exit_screen, MenuHires?"orderd2b.pcx":"orderd2.pcx"); // SHAREWARE, prefer mac if hires
1158         if (! cfexist(exit_screen))
1159                 strcpy(exit_screen, MenuHires?"orderd2.pcx":"orderd2b.pcx"); // SHAREWARE, have to rescale
1160         if (! cfexist(exit_screen))
1161                 strcpy(exit_screen, MenuHires?"warningb.pcx":"warning.pcx"); // D1
1162         if (! cfexist(exit_screen))
1163                 return; // D2 registered
1164
1165         if ((pcx_error=pcx_read_fullscr( exit_screen, title_pal ))==PCX_ERROR_NONE) {
1166                 //vfx_set_palette_sub( title_pal );
1167                 gr_palette_fade_in( title_pal, 32, 0 );
1168                 gr_update();
1169                 while (!key_inkey() && !mouse_button_state(0)) {} //key_getch();
1170                 gr_palette_fade_out( title_pal, 32, 0 );
1171         }
1172         else
1173                 Int3();         //can't load order screen
1174
1175         key_flush();
1176
1177 #endif
1178 }
1179
1180 void quit_request()
1181 {
1182 #ifdef NETWORK
1183 //      void network_abort_game();
1184 //      if(Network_status)
1185 //              network_abort_game();
1186 #endif
1187         exit(0);
1188 }