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