1 /* $Id: mission.c,v 1.31 2004-11-27 05:10:33 btb Exp $ */
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
17 * Code to handle multiple missions
48 //values for d1 built-in mission
49 #define BIM_LAST_LEVEL 27
50 #define BIM_LAST_SECRET_LEVEL -3
51 #define BIM_BRIEFING_FILE "briefing.tex"
52 #define BIM_ENDING_FILE "endreg.tex"
56 char mission_name[MISSION_NAME_LEN+1];
57 char filename[9]; // filename without extension
58 ubyte descent_version; // descent 1 or descent 2?
59 bool anarchy_only_flag; // if true, mission is anarchy only
60 int builtin_hogsize; // This is used to determine the version. Only for the built in mission.
61 ubyte location; // see defines below
64 //values that describe where a mission is located
66 #define ML_MISSIONDIR 1
69 mle Mission_list[MAX_MISSIONS];
71 int num_missions = -1;
73 Mission *Current_mission = NULL; // currently loaded mission
76 // Special versions of mission routines for d1 builtins
79 int load_mission_d1(void)
83 switch (cfile_size("descent.hog")) {
84 case D1_SHAREWARE_MISSION_HOGSIZE:
85 case D1_SHAREWARE_10_MISSION_HOGSIZE:
89 Last_secret_level = 0;
92 for (i=0;i<Last_level;i++)
93 sprintf(Level_names[i], "level%02d.sdl", i+1);
96 case D1_MAC_SHARE_MISSION_HOGSIZE:
100 Last_secret_level = 0;
103 for (i=0;i<Last_level;i++)
104 sprintf(Level_names[i], "level%02d.sdl", i+1);
107 case D1_OEM_MISSION_HOGSIZE:
108 case D1_OEM_10_MISSION_HOGSIZE:
112 Last_secret_level = -1;
115 for (i=0; i < Last_level - 1; i++)
116 sprintf(Level_names[i], "level%02d.rdl", i+1);
117 sprintf(Level_names[i], "saturn%02d.rdl", i+1);
118 for (i=0; i < -Last_secret_level; i++)
119 sprintf(Secret_level_names[i], "levels%1d.rdl", i+1);
121 Secret_level_table[0] = 10;
125 Int3(); // fall through
126 case D1_MISSION_HOGSIZE:
127 case D1_10_MISSION_HOGSIZE:
128 case D1_MAC_MISSION_HOGSIZE:
131 Last_level = BIM_LAST_LEVEL;
132 Last_secret_level = BIM_LAST_SECRET_LEVEL;
135 for (i=0;i<Last_level;i++)
136 sprintf(Level_names[i], "level%02d.rdl", i+1);
137 for (i=0;i<-Last_secret_level;i++)
138 sprintf(Secret_level_names[i], "levels%1d.rdl", i+1);
140 Secret_level_table[0] = 10;
141 Secret_level_table[1] = 21;
142 Secret_level_table[2] = 24;
146 strcpy(Briefing_text_filename,BIM_BRIEFING_FILE);
147 strcpy(Ending_text_filename,BIM_ENDING_FILE);
154 // Special versions of mission routines for shareware
157 int load_mission_shareware(void)
159 strcpy(Current_mission->mission_name, SHAREWARE_MISSION_NAME);
160 Current_mission->descent_version = 2;
161 Current_mission->anarchy_only_flag = 0;
163 switch (Current_mission->builtin_hogsize) {
164 case MAC_SHARE_MISSION_HOGSIZE:
168 Last_secret_level = -1;
170 // mac demo is using the regular hog and rl2 files
171 strcpy(Level_names[0],"d2leva-1.rl2");
172 strcpy(Level_names[1],"d2leva-2.rl2");
173 strcpy(Level_names[2],"d2leva-3.rl2");
174 strcpy(Level_names[3],"d2leva-4.rl2");
175 strcpy(Secret_level_names[0],"d2leva-s.rl2");
178 Int3(); // fall through
179 case SHAREWARE_MISSION_HOGSIZE:
183 Last_secret_level = 0;
185 strcpy(Level_names[0],"d2leva-1.sl2");
186 strcpy(Level_names[1],"d2leva-2.sl2");
187 strcpy(Level_names[2],"d2leva-3.sl2");
195 // Special versions of mission routines for Diamond/S3 version
198 int load_mission_oem(void)
200 strcpy(Current_mission->mission_name, OEM_MISSION_NAME);
201 Current_mission->descent_version = 2;
202 Current_mission->anarchy_only_flag = 0;
207 Last_secret_level = -2;
209 strcpy(Level_names[0],"d2leva-1.rl2");
210 strcpy(Level_names[1],"d2leva-2.rl2");
211 strcpy(Level_names[2],"d2leva-3.rl2");
212 strcpy(Level_names[3],"d2leva-4.rl2");
214 strcpy(Secret_level_names[0],"d2leva-s.rl2");
216 strcpy(Level_names[4],"d2levb-1.rl2");
217 strcpy(Level_names[5],"d2levb-2.rl2");
218 strcpy(Level_names[6],"d2levb-3.rl2");
219 strcpy(Level_names[7],"d2levb-4.rl2");
221 strcpy(Secret_level_names[1],"d2levb-s.rl2");
223 Secret_level_table[0] = 1;
224 Secret_level_table[1] = 5;
230 //compare a string for a token. returns true if match
231 int istok(char *buf,char *tok)
233 return strnicmp(buf,tok,strlen(tok)) == 0;
237 //adds a terminating 0 after a string at the first white space
238 void add_term(char *s)
240 while (*s && !isspace(*s)) s++;
245 //returns ptr to string after '=' & white space, or NULL if no '='
246 //adds 0 after parm at first white space
247 char *get_value(char *buf)
251 t = strchr(buf,'=')+1;
254 while (*t && isspace(*t)) t++;
260 return NULL; //error!
263 //reads a line, returns ptr to value of passed parm. returns NULL if none
264 char *get_parm_value(char *parm,CFILE *f)
268 if (!cfgets(buf,80,f))
272 return get_value(buf);
277 int ml_sort_func(mle *e0,mle *e1)
279 return stricmp(e0->mission_name,e1->mission_name);
283 extern char CDROM_dir[];
285 //returns 1 if file read ok, else 0
286 int read_mission_file(char *filename,int count,int location)
291 //printf("reading: %s\n", filename);
295 strcpy(filename2,MISSION_DIR);
299 songs_stop_redbook(); //so we can read from the CD
300 strcpy(filename2,CDROM_dir);
304 Int3(); //fall through
307 strcpy(filename2,"");
310 strcat(filename2,filename);
312 mfile = cfopen(filename2,"rb");
316 char temp[FILENAME_LEN],*t;
318 strcpy(temp,filename);
319 if ((t = strchr(temp,'.')) == NULL)
320 return 0; //missing extension
321 // look if it's .mn2 or .msn
322 Mission_list[count].descent_version = (t[3] == '2') ? 2 : 1;
323 *t = 0; //kill extension
325 strncpy( Mission_list[count].filename, temp, 9 );
326 Mission_list[count].anarchy_only_flag = 0;
327 Mission_list[count].location = location;
329 p = get_parm_value("name",mfile);
331 if (!p) { //try enhanced mission
332 cfseek(mfile,0,SEEK_SET);
333 p = get_parm_value("xname",mfile);
336 if (!p) { //try super-enhanced mission!
337 cfseek(mfile,0,SEEK_SET);
338 p = get_parm_value("zname",mfile);
343 if ((t=strchr(p,';'))!=NULL)
346 while (isspace(*t)) t--;
347 strncpy(Mission_list[count].mission_name,p,MISSION_NAME_LEN);
354 p = get_parm_value("type",mfile);
358 Mission_list[count].anarchy_only_flag = istok(p,"anarchy");
368 void add_d1_builtin_mission_to_list(int *count)
372 if (!cfexist("descent.hog"))
375 size = cfile_size("descent.hog");
378 case D1_SHAREWARE_MISSION_HOGSIZE:
379 case D1_SHAREWARE_10_MISSION_HOGSIZE:
380 case D1_MAC_SHARE_MISSION_HOGSIZE:
381 strcpy(Mission_list[*count].filename, D1_MISSION_FILENAME);
382 strcpy(Mission_list[*count].mission_name, D1_SHAREWARE_MISSION_NAME);
383 Mission_list[*count].anarchy_only_flag = 0;
385 case D1_OEM_MISSION_HOGSIZE:
386 case D1_OEM_10_MISSION_HOGSIZE:
387 strcpy(Mission_list[*count].filename, D1_MISSION_FILENAME);
388 strcpy(Mission_list[*count].mission_name, D1_OEM_MISSION_NAME);
389 Mission_list[*count].anarchy_only_flag = 0;
392 Warning("Unknown D1 hogsize %d\n", size);
395 case D1_MISSION_HOGSIZE:
396 case D1_10_MISSION_HOGSIZE:
397 case D1_MAC_MISSION_HOGSIZE:
398 strcpy(Mission_list[*count].filename, D1_MISSION_FILENAME);
399 strcpy(Mission_list[*count].mission_name, D1_MISSION_NAME);
400 Mission_list[*count].anarchy_only_flag = 0;
404 Mission_list[*count].builtin_hogsize = 0;
405 Mission_list[*count].descent_version = 1;
406 Mission_list[*count].anarchy_only_flag = 0;
411 void add_builtin_mission_to_list(int *count, char *name)
413 int size = cfile_size("descent2.hog");
416 size = cfile_size("d2demo.hog");
419 case SHAREWARE_MISSION_HOGSIZE:
420 case MAC_SHARE_MISSION_HOGSIZE:
421 strcpy(Mission_list[*count].filename,SHAREWARE_MISSION_FILENAME);
422 strcpy(Mission_list[*count].mission_name,SHAREWARE_MISSION_NAME);
423 Mission_list[*count].anarchy_only_flag = 0;
425 case OEM_MISSION_HOGSIZE:
426 strcpy(Mission_list[*count].filename,OEM_MISSION_FILENAME);
427 strcpy(Mission_list[*count].mission_name,OEM_MISSION_NAME);
428 Mission_list[*count].anarchy_only_flag = 0;
431 Warning("Unknown hogsize %d, trying %s\n", size, FULL_MISSION_FILENAME ".mn2");
432 Int3(); //fall through
433 case FULL_MISSION_HOGSIZE:
434 case FULL_10_MISSION_HOGSIZE:
435 case MAC_FULL_MISSION_HOGSIZE:
436 if (!read_mission_file(FULL_MISSION_FILENAME ".mn2", 0, ML_CURDIR))
437 Error("Could not find required mission file <%s>", FULL_MISSION_FILENAME ".mn2");
440 strcpy(name, Mission_list[*count].filename);
441 Mission_list[*count].builtin_hogsize = size;
442 Mission_list[*count].descent_version = 2;
443 Mission_list[*count].anarchy_only_flag = 0;
448 void add_missions_to_list(char *search_name, int *count, int anarchy_mode)
451 if( !FileFindFirst( search_name, &find ) ) {
453 if (read_mission_file( find.name, *count, ML_MISSIONDIR )) {
455 if (anarchy_mode || !Mission_list[*count].anarchy_only_flag)
459 } while( !FileFindNext( &find ) && *count < MAX_MISSIONS);
461 if (*count >= MAX_MISSIONS)
462 mprintf((0, "Warning: more missions than d2x can handle\n"));
466 /* move <mission_name> to <place> on mission list, increment <place> */
467 void promote (char * mission_name, int * top_place, int num_missions)
470 char name[FILENAME_LEN], * t;
471 strcpy(name, mission_name);
472 if ((t = strchr(name,'.')) != NULL)
473 *t = 0; //kill extension
474 //printf("promoting: %s\n", name);
475 for (i = *top_place; i < num_missions; i++)
476 if (!stricmp(Mission_list[i].filename, name)) {
477 //swap mission positions
480 temp = Mission_list[*top_place];
481 Mission_list[*top_place] = Mission_list[i];
482 Mission_list[i] = temp;
488 void free_mission(void)
490 // May become more complex with the editor
491 if (Current_mission) {
492 d_free(Current_mission);
498 //fills in the global list of missions. Returns the number of missions
499 //in the list. If anarchy_mode is set, then also add anarchy-only missions.
501 extern char CDROM_dir[];
502 extern char AltHogDir[];
503 extern char AltHogdir_initialized;
505 int build_mission_list(int anarchy_mode)
507 static int num_missions=-1;
510 char builtin_mission_filename[FILENAME_LEN];
512 //now search for levels on disk
514 //@@Took out this code because after this routine was called once for
515 //@@a list of single-player missions, a subsequent call for a list of
516 //@@anarchy missions would not scan again, and thus would not find the
517 //@@anarchy-only missions. If we retain the minimum level of install,
518 //@@we may want to put the code back in, having it always scan for all
519 //@@missions, and have the code that uses it sort out the ones it wants.
520 //@@ if (num_missions != -1) {
521 //@@ if (Current_mission_num != 0)
522 //@@ load_mission(0); //set built-in mission as default
523 //@@ return num_missions;
526 add_builtin_mission_to_list(&count, builtin_mission_filename); //read built-in first
527 add_d1_builtin_mission_to_list(&count);
528 add_missions_to_list(MISSION_DIR "*.mn2", &count, anarchy_mode);
529 add_missions_to_list(MISSION_DIR "*.msn", &count, anarchy_mode);
531 if (AltHogdir_initialized) {
532 char search_name[PATH_MAX + 5];
533 strcpy(search_name, AltHogDir);
534 strcat(search_name, "/" MISSION_DIR "*.mn2");
535 add_missions_to_list(search_name, &count, anarchy_mode);
536 strcpy(search_name, AltHogDir);
537 strcat(search_name, "/" MISSION_DIR "*.msn");
538 add_missions_to_list(search_name, &count, anarchy_mode);
541 // move original missions (in story-chronological order)
542 // to top of mission list
544 promote("descent", &top_place, count); // original descent 1 mission
545 promote(builtin_mission_filename, &top_place, count); // d2 or d2demo
546 promote("d2x", &top_place, count); // vertigo
548 if (count > top_place)
549 qsort(&Mission_list[top_place],
551 sizeof(*Mission_list),
552 (int (*)( const void *, const void * ))ml_sort_func);
555 if (count > top_place)
556 qsort(&Mission_list[top_place],
558 sizeof(*Mission_list),
559 (int (*)( const void *, const void * ))ml_sort_func);
561 //load_mission(0); //set built-in mission as default
563 num_missions = count;
565 atexit(free_mission);
570 void init_extra_robot_movie(char *filename);
572 //values for built-in mission
574 //loads the specfied mission from the mission list.
575 //build_mission_list() must have been called.
576 //Returns true if mission loaded ok, else false.
577 int load_mission(int mission_num)
585 Current_mission = d_malloc(sizeof(Mission));
586 if (!Current_mission) return 0;
587 *(mle *) Current_mission = Mission_list[mission_num];
589 // for Descent 1 missions, load descent.hog
591 if (!cfile_use_descent1_hogfile("descent.hog"))
592 Warning("descent.hog not available, this mission may be missing some files required for briefings and exit sequence\n");
593 if (!stricmp(Current_mission_filename, D1_MISSION_FILENAME))
594 return load_mission_d1();
597 if (PLAYING_BUILTIN_MISSION) {
598 switch (Current_mission->builtin_hogsize) {
599 case SHAREWARE_MISSION_HOGSIZE:
600 case MAC_SHARE_MISSION_HOGSIZE:
601 return load_mission_shareware();
603 case OEM_MISSION_HOGSIZE:
604 return load_mission_oem();
607 Int3(); // fall through
608 case FULL_MISSION_HOGSIZE:
609 case FULL_10_MISSION_HOGSIZE:
610 case MAC_FULL_MISSION_HOGSIZE:
611 // continue on... (use d2.mn2 from hogfile)
616 mprintf(( 0, "Loading mission %s\n", Current_mission_filename ));
618 //read mission from file
620 switch (Mission_list[mission_num].location) {
622 strcpy(buf,MISSION_DIR);
625 strcpy(buf,CDROM_dir);
628 Int3(); //fall through
633 strcat(buf,Mission_list[mission_num].filename);
634 if (Mission_list[mission_num].descent_version == 2)
639 mfile = cfopen(buf,"rb");
645 //for non-builtin missions, load HOG
646 if (!PLAYING_BUILTIN_MISSION) {
648 strcpy(buf+strlen(buf)-4,".hog"); //change extension
650 found_hogfile = cfile_use_alternate_hogfile(buf);
652 #ifdef RELEASE //for release, require mission to be in hogfile
653 if (! found_hogfile) {
663 Last_secret_level = 0;
664 Briefing_text_filename[0] = 0;
665 Ending_text_filename[0] = 0;
667 while (cfgets(buf,80,mfile)) {
669 if (istok(buf,"name")) {
670 Current_mission->enhanced = 0;
671 continue; //already have name, go to next line
673 if (istok(buf,"xname")) {
674 Current_mission->enhanced = 1;
675 continue; //already have name, go to next line
677 if (istok(buf,"zname")) {
678 Current_mission->enhanced = 2;
679 continue; //already have name, go to next line
681 else if (istok(buf,"type"))
682 continue; //already have name, go to next line
683 else if (istok(buf,"hog")) {
686 while (*(bufp++) != '=')
690 while (*(++bufp) == ' ')
693 cfile_use_alternate_hogfile(bufp);
694 mprintf((0, "Hog file override = [%s]\n", bufp));
696 else if (istok(buf,"briefing")) {
697 if ((v = get_value(buf)) != NULL) {
700 strcpy(Briefing_text_filename,v);
703 else if (istok(buf,"ending")) {
704 if ((v = get_value(buf)) != NULL) {
707 strcpy(Ending_text_filename,v);
710 else if (istok(buf,"num_levels")) {
712 if ((v=get_value(buf))!=NULL) {
717 for (i=0;i<n_levels && cfgets(buf,80,mfile);i++) {
720 if (strlen(buf) <= 12) {
721 strcpy(Level_names[i],buf);
730 else if (istok(buf,"num_secrets")) {
731 if ((v=get_value(buf))!=NULL) {
734 N_secret_levels = atoi(v);
736 Assert(N_secret_levels <= MAX_SECRET_LEVELS_PER_MISSION);
738 for (i=0;i<N_secret_levels && cfgets(buf,80,mfile);i++) {
742 if ((t=strchr(buf,','))!=NULL) *t++=0;
747 if (strlen(buf) <= 12) {
748 strcpy(Secret_level_names[i],buf);
749 Secret_level_table[i] = atoi(t);
750 if (Secret_level_table[i]<1 || Secret_level_table[i]>Last_level)
765 if (Last_level <= 0) {
766 free_mission(); //no valid mission loaded
770 if (Current_mission->enhanced) {
772 extern void bm_read_extra_robots();
773 sprintf(t,"%s.ham",Current_mission_filename);
774 bm_read_extra_robots(t, Current_mission->enhanced);
775 strncpy(t,Current_mission_filename,6);
777 init_extra_robot_movie(t);
783 //loads the named mission if exists.
784 //Returns true if mission loaded ok, else false.
785 int load_mission_by_name(char *mission_name)
789 n = build_mission_list(1);
792 if (!stricmp(mission_name,Mission_list[i].filename))
793 return load_mission(i);
795 return 0; //couldn't find mission
798 int select_mission(int anarchy_mode, char *message)
802 n_missions = build_mission_list(anarchy_mode);
804 if (n_missions <= 1) {
807 int new_mission_num,i, default_mission;
808 char * m[MAX_MISSIONS];
811 for (i=0;i<n_missions;i++) {
812 m[i] = Mission_list[i].mission_name;
813 if ( !stricmp( m[i], config_last_mission ) )
817 new_mission_num = newmenu_listbox1( message, n_missions, m, 1, default_mission, NULL );
819 if (new_mission_num == -1)
822 strcpy(config_last_mission, m[new_mission_num] );
824 if (!load_mission(new_mission_num)) {
825 nm_messagebox( NULL, 1, TXT_OK, TXT_MISSION_ERROR);