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