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