]> icculus.org git repositories - btb/d2x.git/blob - main/inferno.c
This commit was generated by cvs2svn to compensate for changes in r2,
[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 char copyright[] = "DESCENT II  COPYRIGHT (C) 1994-1996 PARALLAX SOFTWARE CORPORATION";
15
16 #include <conf.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include "pstypes.h"
22 #include "strutil.h"
23 #include "console.h"
24 #include "pa_enabl.h"       //$$POLY_ACC
25 #include "gr.h"
26 #include "fix.h"
27 #include "vecmat.h"
28 #include "mono.h"
29 #include "key.h"
30 #include "timer.h"
31 #include "3d.h"
32 #include "bm.h"
33 #include "inferno.h"
34 #include "error.h"
35 #include "game.h"
36 #include "segment.h"            //for Side_to_verts
37 #include "u_mem.h"
38 #include "segpoint.h"
39 #include "screens.h"
40 #include "texmap.h"
41 #include "texmerge.h"
42 #include "menu.h"
43 #include "wall.h"
44 #include "polyobj.h"
45 #include "effects.h"
46 #include "digi.h"
47 #include "iff.h"
48 #include "pcx.h"
49 #include "palette.h"
50 #include "args.h"
51 #include "sounds.h"
52 #include "titles.h"
53 #include "player.h"
54 #include "text.h"
55 #include "ipx.h"
56 #include "newdemo.h"
57 #include "network.h"
58 #include "modem.h"
59 #include "gamefont.h"
60 #include "kconfig.h"
61 #include "mouse.h"
62 #include "joy.h"
63 #include "newmenu.h"
64 #include "desc_id.h"
65 #include "config.h"
66 #include "joydefs.h"
67 #include "multi.h"
68 #include "songs.h"
69 #include "cfile.h"
70 #include "gameseq.h"
71 #include "gamepal.h"
72 #include "mission.h"
73 #include "movie.h"
74 #include "compbit.h"
75
76 // #  include "3dfx_des.h"
77
78 #if defined(POLY_ACC)
79 #include "poly_acc.h"
80 extern int Current_display_mode;        //$$ there's got to be a better way than hacking this.
81 #endif
82
83 #ifdef EDITOR
84 #include "editor\editor.h"
85 #include "editor\kdefs.h"
86 #include "ui.h"
87 #endif
88
89 #include "vers_id.h"
90
91 void mem_init(void);
92 void arch_init(void);
93 void arch_init_start(void);
94
95 //Current version number
96
97 ubyte Version_major = 1;                //FULL VERSION
98 ubyte Version_minor = 2;
99
100 static const char desc_id_checksum_str[] = DESC_ID_CHKSUM_TAG "0000";   //4-byte checksum
101 char desc_id_exit_num = 0;
102
103 int Function_mode=FMODE_MENU;           //game or editor?
104 int Screen_mode=-1;                                     //game screen or editor screen?
105
106 //--unused-- grs_bitmap Inferno_bitmap_title;
107
108 int WVIDEO_running=0;           //debugger can set to 1 if running
109
110 #ifdef EDITOR
111 int Inferno_is_800x600_available = 0;
112 #endif
113
114 //--unused-- int Cyberman_installed=0;                  // SWIFT device present
115 ubyte CybermouseActive=0;
116
117 int __far descent_critical_error_handler( unsigned deverr, unsigned errcode, unsigned __far * devhdr );
118
119 void check_joystick_calibration(void);
120
121
122 //--------------------------------------------------------------------------
123
124 extern int piggy_low_memory;
125
126 void mem_int_to_string( int number, char *dest )
127 {
128         int i,l,c;
129         char buffer[20],*p;
130
131         sprintf( buffer, "%d", number );
132
133         l = strlen(buffer);
134         if (l<=3) {
135                 // Don't bother with less than 3 digits
136                 sprintf( dest, "%d", number );
137                 return;
138         }
139
140         c = 0;
141         p=dest;
142         for (i=l-1; i>=0; i-- ) {
143                 if (c==3) {
144                         *p++=',';
145                         c = 0;
146                 }
147                 c++;
148                 *p++ = buffer[i];
149         }
150         *p++ = '\0';
151         strrev(dest);
152 }
153
154
155 int descent_critical_error = 0;
156 unsigned descent_critical_deverror = 0;
157 unsigned descent_critical_errcode = 0;
158
159 extern int Network_allow_socket_changes;
160
161 extern void vfx_set_palette_sub(ubyte *);
162
163 extern int VR_low_res;
164
165 extern int Config_vr_type;
166 extern int Config_vr_resolution;
167 extern int Config_vr_tracking;
168 int grd_fades_disabled=1;
169
170 #define LINE_LEN        100
171
172 void do_joystick_init()
173 {
174  
175
176         if (!args_find( "-nojoystick" ))        {
177                 con_printf(CON_VERBOSE, "\n%s", TXT_VERBOSE_6);
178                 joy_init();
179                 if ( args_find( "-joyslow" ))   {
180                         con_printf(CON_VERBOSE, "\n%s", TXT_VERBOSE_7);
181                         joy_set_slow_reading(JOY_SLOW_READINGS);
182                 }
183                 if ( args_find( "-joypolled" )) {
184                         con_printf(CON_VERBOSE, "\n%s", TXT_VERBOSE_8);
185                         joy_set_slow_reading(JOY_POLLED_READINGS);
186                 }
187                 if ( args_find( "-joybios" ))   {
188                         con_printf(CON_VERBOSE, "\n%s", TXT_VERBOSE_9);
189                         joy_set_slow_reading(JOY_BIOS_READINGS);
190                 }
191
192         //      Added from Descent v1.5 by John.  Adapted by Samir.
193         } else {
194                 con_printf(CON_VERBOSE, "\n%s", TXT_VERBOSE_10);
195         }
196 }
197
198 //set this to force game to run in low res
199 int disable_high_res=0;
200
201 void do_register_player(ubyte *title_pal)
202 {
203         Players[Player_num].callsign[0] = '\0';
204
205         if (!Auto_demo)         {
206
207                 key_flush();
208
209                 //now, before we bring up the register player menu, we need to 
210                 //do some stuff to make sure the palette is ok.  First, we need to
211                 //get our current palette into the 2d's array, so the remapping will
212                 //work.  Second, we need to remap the fonts.  Third, we need to fill 
213                 //in part of the fade tables so the darkening of the menu edges works
214
215                 memcpy(gr_palette,title_pal,sizeof(gr_palette));
216                 remap_fonts_and_menus(1);
217                 RegisterPlayer();               //get player's name
218         }
219
220 }
221
222 #ifdef NETWORK
223 void do_network_init()
224 {
225         if (!args_find( "-nonetwork" )) {
226                 int socket=0, showaddress=0, t;
227                 int ipx_error;
228
229                 con_printf(CON_VERBOSE, "\n%s ", TXT_INITIALIZING_NETWORK);
230                 if ((t=args_find("-socket")))
231                         socket = atoi( Args[t+1] );
232                 //@@if ( args_find("-showaddress") ) showaddress=1;
233                 if ((ipx_error=ipx_init(IPX_DEFAULT_SOCKET+socket,showaddress))==0)     {
234                         con_printf(CON_VERBOSE, "%s %d.\n", TXT_IPX_CHANNEL, socket );
235                         Network_active = 1;
236                 } else {
237                         switch( ipx_error )     {
238                         case 3:         con_printf(CON_VERBOSE, "%s\n", TXT_NO_NETWORK); break;
239                         case -2: con_printf(CON_VERBOSE, "%s 0x%x.\n", TXT_SOCKET_ERROR, IPX_DEFAULT_SOCKET+socket); break;
240                         case -4: con_printf(CON_VERBOSE, "%s\n", TXT_MEMORY_IPX ); break;
241                         default:
242                                 con_printf(CON_VERBOSE, "%s %d", TXT_ERROR_IPX, ipx_error );
243                         }
244                         con_printf(CON_VERBOSE, "%s\n",TXT_NETWORK_DISABLED);
245                         Network_active = 0;             // Assume no network
246                 }
247                 ipx_read_user_file( "descent.usr" );
248                 ipx_read_network_file( "descent.net" );
249                 //@@if ( args_find( "-dynamicsockets" ))
250                 //@@    Network_allow_socket_changes = 1;
251                 //@@else
252                 //@@    Network_allow_socket_changes = 0;
253         } else {
254                 con_printf(CON_VERBOSE, "%s\n", TXT_NETWORK_DISABLED);
255                 Network_active = 0;             // Assume no network
256         }
257 }
258 #endif
259
260 #ifdef SHAREARE
261 #define PROGNAME "d2demo"
262 #else
263 #define PROGNAME "d2"
264 #endif
265
266 extern char Language[];
267
268 //can we do highres menus?
269 extern int MenuHiresAvailable;
270
271 #ifdef D2_OEM
272 int intro_played = 0;
273 #endif
274
275 int open_movie_file(char *filename,int must_have);
276
277 #if defined(POLY_ACC)
278 #define MENU_HIRES_MODE SM_640x480x15xPA
279 #else
280 #define MENU_HIRES_MODE SM(640,480)
281 #endif
282
283 //      DESCENT II by Parallax Software
284 //              Descent Main
285
286 //extern ubyte gr_current_pal[];
287
288 #ifdef  EDITOR
289 int     Auto_exit = 0;
290 char    Auto_file[128] = "";
291 #endif
292
293 int main(int argc,char **argv)
294 {
295         int i,t;
296         ubyte title_pal[768];
297
298         
299         con_init();  // Initialise the console
300         mem_init();
301
302         error_init(NULL, NULL);
303
304         args_init( argc,argv );
305
306         if ( args_find( "-debug") )
307         {
308                 con_threshold.value = (float)2;
309
310         } else
311                 if ( args_find( "-verbose" ) ) 
312                 {
313                         con_threshold.value = (float)1;
314                 }
315
316         arch_init_start();
317
318         arch_init();
319
320         //tell cfile about our counter
321         cfile_set_critical_error_counter_ptr(&descent_critical_error);
322
323         #ifdef SHAREWARE
324                 cfile_init("d2demo.hog");                       //specify name of hogfile
325         #else
326         #define HOGNAME "descent2.hog"
327         if (! cfile_init(HOGNAME)) {            //didn't find HOG.  Check on CD
328                 #ifdef RELEASE
329                         Error("Could not find required file <%s>",HOGNAME);
330                 #endif
331         }
332         #endif
333         
334         load_text();
335
336         //print out the banner title
337         con_printf(CON_NORMAL, "\nDESCENT 2 %s v%d.%d",VERSION_TYPE,Version_major,Version_minor);
338         con_printf(CON_NORMAL, "  %s %s\n", __DATE__,__TIME__);
339         con_printf(CON_NORMAL, "%s\n%s\n",TXT_COPYRIGHT,TXT_TRADEMARK); 
340         con_printf(CON_NORMAL, "This is a MODIFIED version of Descent 2. Copyright (c) 1999 Peter Hawkins\n");
341
342
343         if (args_find( "-?" ) || args_find( "-help" ) || args_find( "?" ) ) {
344                 //print_commandline_help();
345                 set_exit_message("");
346                 return(0);
347         }
348
349         con_printf(CON_NORMAL, "\n");
350         con_printf(CON_NORMAL, TXT_HELP, PROGNAME);             //help message has %s for program name
351         con_printf(CON_NORMAL, "\n");
352
353         con_printf(CON_VERBOSE, "\n%s...", "Checking for Descent 2 CD-ROM");
354
355         if ( args_find( "-autodemo" ))
356                 Auto_demo = 1;
357
358         if ( args_find( "-noscreens" ) )
359                 Skip_briefing_screens = 1;
360
361         Lighting_on = 1;
362
363 //      if (init_graphics()) return 1;
364
365         #ifdef EDITOR
366         if (!Inferno_is_800x600_available)      {
367                 con_printf(CON_NORMAL "The editor will not be available, press any key to start game...\n" );
368                 Function_mode = FMODE_MENU;
369         }
370         #endif
371
372         if (!WVIDEO_running)
373                 con_printf(CON_DEBUG,"WVIDEO_running = %d\n",WVIDEO_running);
374
375         con_printf (CON_VERBOSE, "%s", TXT_VERBOSE_1);
376         ReadConfigFile();
377
378 #ifdef NETWORK
379         do_network_init();
380 #endif
381
382 #if defined(POLY_ACC)
383     Current_display_mode = -1;
384     game_init_render_buffers(SM_640x480x15xPA, 640, 480, VR_NONE, VRF_COMPATIBLE_MENUS+VRF_ALLOW_COCKPIT );
385 #else
386
387         if (!VR_offscreen_buffer)       //if hasn't been initialied (by headset init)
388                 set_display_mode(0);            //..then set default display mode
389 #endif
390
391         i = args_find( "-xcontrol" );
392         if ( i > 0 )    {
393                 kconfig_init_external_controls( strtol(Args[i+1], NULL, 0), strtol(Args[i+2], NULL, 0) );
394         }
395
396         con_printf(CON_VERBOSE, "\n%s\n\n", TXT_INITIALIZING_GRAPHICS);
397         if (args_find("-nofade"))
398                 grd_fades_disabled=1;
399         
400         if ((t=gr_init())!=0)                           //doesn't do much
401                 Error(TXT_CANT_INIT_GFX,t);
402
403    #ifdef _3DFX
404    _3dfx_Init();
405    #endif
406
407         // Load the palette stuff. Returns non-zero if error.
408         con_printf(CON_DEBUG, "\nInitializing palette system..." );
409    gr_use_palette_table(DEFAULT_PALETTE );
410
411         con_printf(CON_DEBUG, "\nInitializing font system..." );
412         gamefont_init();        // must load after palette data loaded.
413
414         //determine whether we're using high-res menus & movies
415 #if !defined(POLY_ACC)
416         if (args_find("-nohires") || args_find("-nohighres") || (gr_check_mode(MENU_HIRES_MODE) != 0) || disable_high_res)
417                 MovieHires = MenuHires = MenuHiresAvailable = 0;
418         else
419 #endif
420                 //NOTE LINK TO ABOVE!
421                 MenuHires = MenuHiresAvailable = 1;
422
423         con_printf( CON_DEBUG, "\nInitializing movie libraries..." );
424         init_movies();          //init movie libraries
425
426         con_printf(CON_VERBOSE, "\nGoing into graphics mode...\n");
427 #if defined(POLY_ACC)
428         gr_set_mode(SM_640x480x15xPA);
429 #else
430         gr_set_mode(MovieHires?SM(640,480):SM(320,200));
431 #endif
432
433         #ifndef RELEASE
434         if ( args_find( "-notitles" ) ) 
435                 songs_play_song( SONG_TITLE, 1);
436         else
437         #endif
438         {       //NOTE LINK TO ABOVE!
439                 int played=MOVIE_NOT_PLAYED;    //default is not played
440                 int song_playing = 0;
441
442                 #ifdef D2_OEM
443                 #define MOVIE_REQUIRED 0
444                 #else
445                 #define MOVIE_REQUIRED 1
446                 #endif
447
448 #ifdef D2_OEM   //$$POLY_ACC, jay.
449                 {       //show bundler screens
450                         FILE *tfile;
451                         char filename[FILENAME_LEN];
452
453                         played=MOVIE_NOT_PLAYED;        //default is not played
454
455             played = PlayMovie("pre_i.mve",0);
456
457                         if (!played) {
458                 strcpy(filename,MenuHires?"pre_i1b.pcx":"pre_i1.pcx");
459
460                                 while ((tfile=fopen(filename,"rb")) != NULL) {
461                                         fclose(tfile);
462                                         show_title_screen( filename, 1, 0 );
463                     filename[5]++;
464                                 }
465                         }
466                 }
467 #endif
468
469                 #ifndef SHAREWARE
470                         init_subtitles("intro.tex");
471                         played = PlayMovie("intro.mve",MOVIE_REQUIRED);
472                         close_subtitles();
473                 #endif
474
475                 #ifdef D2_OEM
476                 if (played != MOVIE_NOT_PLAYED)
477                         intro_played = 1;
478                 else {                                          //didn't get intro movie, try titles
479
480                         played = PlayMovie("titles.mve",MOVIE_REQUIRED);
481
482                         if (played == MOVIE_NOT_PLAYED) {
483 #if defined(POLY_ACC)
484             gr_set_mode(SM_640x480x15xPA);
485 #else
486                                 gr_set_mode(MenuHires?SM_640x480V:SM_320x200C);
487 #endif
488                                 con_printf( CON_DEBUG, "\nPlaying title song..." );
489                                 songs_play_song( SONG_TITLE, 1);
490                                 song_playing = 1;
491                                 con_printf( CON_DEBUG, "\nShowing logo screens..." );
492                                 show_title_screen( MenuHires?"iplogo1b.pcx":"iplogo1.pcx", 1, 1 );
493                                 show_title_screen( MenuHires?"logob.pcx":"logo.pcx", 1, 1 );
494                         }
495                 }
496
497                 {       //show bundler movie or screens
498
499                         FILE *tfile;
500                         char filename[FILENAME_LEN];
501                         int movie_handle;
502
503                         played=MOVIE_NOT_PLAYED;        //default is not played
504
505                         //check if OEM movie exists, so we don't stop the music if it doesn't
506                         movie_handle = open_movie_file("oem.mve",0);
507                         if (movie_handle != -1) {
508                                 close(movie_handle);
509                                 played = PlayMovie("oem.mve",0);
510                                 song_playing = 0;               //movie will kill sound
511                         }
512
513                         if (!played) {
514                                 strcpy(filename,MenuHires?"oem1b.pcx":"oem1.pcx");
515
516                                 while ((tfile=fopen(filename,"rb")) != NULL) {
517                                         fclose(tfile);
518                                         show_title_screen( filename, 1, 0 );
519                                         filename[3]++;
520                                 }
521                         }
522                 }
523                 #endif
524
525                 if (!song_playing)
526                         songs_play_song( SONG_TITLE, 1);
527                         
528         }
529
530    PA_DFX (pa_splash());
531
532         con_printf( CON_DEBUG, "\nShowing loading screen..." );
533         {
534                 //grs_bitmap title_bm;
535                 int pcx_error;
536                 char filename[14];
537
538                 #ifdef SHAREWARE
539                 strcpy(filename, "descentd.pcx");
540                 #else
541                 #ifdef D2_OEM
542                 strcpy(filename, MenuHires?"descntob.pcx":"descento.pcx");
543                 #else
544                 strcpy(filename, MenuHires?"descentb.pcx":"descent.pcx");
545                 #endif
546                 #endif
547
548 #if defined(POLY_ACC)
549                 gr_set_mode(SM_640x480x15xPA);
550 #else
551                 gr_set_mode(MenuHires?SM(640,480):SM(320,200));
552 #endif
553
554                 FontHires = MenuHires;
555
556                 if ((pcx_error=pcx_read_bitmap( filename, &grd_curcanv->cv_bitmap, grd_curcanv->cv_bitmap.bm_type, title_pal ))==PCX_ERROR_NONE)        {
557                         //vfx_set_palette_sub( title_pal );
558                         gr_palette_clear();
559                         gr_palette_fade_in( title_pal, 32, 0 );
560                 } else
561                         Error( "Couldn't load pcx file '%s', PCX load error: %s\n",filename, pcx_errormsg(pcx_error));
562         }
563
564         con_printf( CON_DEBUG , "\nDoing bm_init..." );
565         #ifdef EDITOR
566                 bm_init_use_tbl();
567         #else
568                 bm_init();
569         #endif
570
571         #ifdef EDITOR
572         if (args_find("-hoarddata") != 0) {
573                 #define MAX_BITMAPS_PER_BRUSH 30
574                 grs_bitmap * bm[MAX_BITMAPS_PER_BRUSH];
575                 grs_bitmap icon;
576                 int nframes;
577                 short nframes_short;
578                 ubyte palette[256*3];
579                 FILE *ofile;
580                 int iff_error,i;
581                 char *sounds[] = {"selforb.raw","selforb.r22",          //SOUND_YOU_GOT_ORB                     
582                                                                 "teamorb.raw","teamorb.r22",            //SOUND_FRIEND_GOT_ORB                  
583                                                                 "enemyorb.raw","enemyorb.r22",  //SOUND_OPPONENT_GOT_ORB        
584                                                                 "OPSCORE1.raw","OPSCORE1.r22"}; //SOUND_OPPONENT_HAS_SCORED
585
586                 ofile = fopen("hoard.ham","wb");
587
588            iff_error = iff_read_animbrush("orb.abm",bm,MAX_BITMAPS_PER_BRUSH,&nframes,palette);
589                 Assert(iff_error == IFF_NO_ERROR);
590                 nframes_short = nframes;
591                 fwrite(&nframes_short,sizeof(nframes_short),1,ofile);
592                 fwrite(&bm[0]->bm_w,sizeof(short),1,ofile);
593                 fwrite(&bm[0]->bm_h,sizeof(short),1,ofile);
594                 fwrite(palette,3,256,ofile);
595                 for (i=0;i<nframes;i++)
596                         fwrite(bm[i]->bm_data,1,bm[i]->bm_w*bm[i]->bm_h,ofile);
597
598            iff_error = iff_read_animbrush("orbgoal.abm",bm,MAX_BITMAPS_PER_BRUSH,&nframes,palette);
599                 Assert(iff_error == IFF_NO_ERROR);
600                 Assert(bm[0]->bm_w == 64 && bm[0]->bm_h == 64);
601                 nframes_short = nframes;
602                 fwrite(&nframes_short,sizeof(nframes_short),1,ofile);
603                 fwrite(palette,3,256,ofile);
604                 for (i=0;i<nframes;i++)
605                         fwrite(bm[i]->bm_data,1,bm[i]->bm_w*bm[i]->bm_h,ofile);
606
607                 for (i=0;i<2;i++) {
608                         iff_error = iff_read_bitmap(i?"orbb.bbm":"orb.bbm",&icon,BM_LINEAR,palette);
609                         Assert(iff_error == IFF_NO_ERROR);
610                         fwrite(&icon.bm_w,sizeof(short),1,ofile);
611                         fwrite(&icon.bm_h,sizeof(short),1,ofile);
612                         fwrite(palette,3,256,ofile);
613                         fwrite(icon.bm_data,1,icon.bm_w*icon.bm_h,ofile);
614                 }
615
616                 for (i=0;i<sizeof(sounds)/sizeof(*sounds);i++) {
617                         FILE *ifile;
618                         int size;
619                         ubyte *buf;
620                         ifile = fopen(sounds[i],"rb");
621                         Assert(ifile != NULL);
622                         size = filelength(ifile->_handle);
623                         buf = d_malloc(size);
624                         fread(buf,1,size,ifile);
625                         fwrite(&size,sizeof(size),1,ofile);
626                         fwrite(buf,1,size,ofile);
627                         d_free(buf);
628                         fclose(ifile);
629                 }
630
631                 fclose(ofile);
632
633                 exit(1);
634         }
635         #endif
636
637         //the bitmap loading code changes gr_palette, so restore it
638         memcpy(gr_palette,title_pal,sizeof(gr_palette));
639
640         if ( args_find( "-norun" ) )
641                 return(0);
642
643         con_printf( CON_DEBUG, "\nInitializing 3d system..." );
644         g3_init();
645
646         con_printf( CON_DEBUG, "\nInitializing texture caching system..." );
647         texmerge_init( 10 );            // 10 cache bitmaps
648
649         con_printf( CON_DEBUG, "\nRunning game...\n" );
650         set_screen_mode(SCREEN_MENU);
651
652         init_game();
653
654         //      If built with editor, option to auto-load a level and quit game
655         //      to write certain data.
656         #ifdef  EDITOR
657         {       int t;
658         if ( t = args_find( "-autoload" ) ) {
659                 Auto_exit = 1;
660                 strcpy(Auto_file, Args[t+1]);
661         }
662                 
663         }
664
665         if (Auto_exit) {
666                 strcpy(Players[0].callsign, "dummy");
667         } else
668         #endif
669                 do_register_player(title_pal);
670
671         gr_palette_fade_out( title_pal, 32, 0 );
672
673         Game_mode = GM_GAME_OVER;
674
675         if (Auto_demo)  {
676                 newdemo_start_playback("descent.dem");          
677                 if (Newdemo_state == ND_STATE_PLAYBACK )
678                         Function_mode = FMODE_GAME;
679         }
680
681         //do this here because the demo code can do a longjmp when trying to
682         //autostart a demo from the main menu, never having gone into the game
683         setjmp(LeaveGame);
684
685         while (Function_mode != FMODE_EXIT)
686         {
687                 switch( Function_mode ) {
688                 case FMODE_MENU:
689                         set_screen_mode(SCREEN_MENU);
690                         if ( Auto_demo )        {
691                                 newdemo_start_playback(NULL);           // Randomly pick a file
692                                 if (Newdemo_state != ND_STATE_PLAYBACK) 
693                                         Error("No demo files were found for autodemo mode!");
694                         } else {
695                                 #ifdef EDITOR
696                                 if (Auto_exit) {
697                                         strcpy(&Level_names[0], Auto_file);
698                                         LoadLevel(1, 1);
699                                         Function_mode = FMODE_EXIT;
700                                         break;
701                                 }
702                                 #endif
703
704                                 check_joystick_calibration();
705                                 gr_palette_clear();             //I'm not sure why we need this, but we do
706                                 DoMenu();                                                                               
707                                 #ifdef EDITOR
708                                 if ( Function_mode == FMODE_EDITOR )    {
709                                         create_new_mine();
710                                         SetPlayerFromCurseg();
711                                         load_palette(NULL,1,0);
712                                 }
713                                 #endif
714                         }
715                         break;
716                 case FMODE_GAME:
717                         #ifdef EDITOR
718                                 keyd_editor_mode = 0;
719                         #endif
720                         game();
721                         if ( Function_mode == FMODE_MENU )
722                                 songs_play_song( SONG_TITLE, 1 );
723                         break;
724                 #ifdef EDITOR
725                 case FMODE_EDITOR:
726                         keyd_editor_mode = 1;
727                         editor();
728                         _harderr( (void*)descent_critical_error_handler );              // Reinstall game error handler
729                         if ( Function_mode == FMODE_GAME ) {
730                                 Game_mode = GM_EDITOR;
731                                 editor_reset_stuff_on_level();
732                                 N_players = 1;
733                         }
734                         break;
735                 #endif
736                 default:
737                         Error("Invalid function mode %d",Function_mode);
738                 }
739         }
740
741         WriteConfigFile();
742
743         #ifndef RELEASE
744         if (!args_find( "-notitles" ))
745         #endif
746
747         #ifndef NDEBUG
748         if ( args_find( "-showmeminfo" ) )
749                 show_mem_info = 1;              // Make memory statistics show
750         #endif
751
752         return(0);              //presumably successful exit
753 }
754
755
756 void check_joystick_calibration()       {
757         int x1, y1, x2, y2, c;
758         fix t1;
759
760         if ( (Config_control_type!=CONTROL_JOYSTICK) &&
761                   (Config_control_type!=CONTROL_FLIGHTSTICK_PRO) &&
762                   (Config_control_type!=CONTROL_THRUSTMASTER_FCS) &&
763                   (Config_control_type!=CONTROL_GRAVIS_GAMEPAD)
764                 ) return;
765
766         joy_get_pos( &x1, &y1 );
767
768         t1 = timer_get_fixed_seconds();
769         while( timer_get_fixed_seconds() < t1 + F1_0/100 )
770                 ;
771
772         joy_get_pos( &x2, &y2 );
773
774         // If joystick hasn't moved...
775         if ( (abs(x2-x1)<30) &&  (abs(y2-y1)<30) )      {
776                 if ( (abs(x1)>30) || (abs(x2)>30) ||  (abs(y1)>30) || (abs(y2)>30) )    {
777                         c = nm_messagebox( NULL, 2, TXT_CALIBRATE, TXT_SKIP, TXT_JOYSTICK_NOT_CEN );
778                         if ( c==0 )     {
779                                 joydefs_calibrate();
780                         }
781                 }
782         }
783
784 }