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