1 /* $Id: mission.c,v 1.35 2004-12-02 16:50:20 schaffner 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
46 #include "ignorecase.h"
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 int num_missions = -1;
71 Mission *Current_mission = NULL; // currently loaded mission
74 // Special versions of mission routines for d1 builtins
77 int load_mission_d1(void)
81 switch (cfile_size("descent.hog")) {
82 case D1_SHAREWARE_MISSION_HOGSIZE:
83 case D1_SHAREWARE_10_MISSION_HOGSIZE:
87 Last_secret_level = 0;
90 for (i=0;i<Last_level;i++)
91 sprintf(Level_names[i], "level%02d.sdl", i+1);
94 case D1_MAC_SHARE_MISSION_HOGSIZE:
98 Last_secret_level = 0;
101 for (i=0;i<Last_level;i++)
102 sprintf(Level_names[i], "level%02d.sdl", i+1);
105 case D1_OEM_MISSION_HOGSIZE:
106 case D1_OEM_10_MISSION_HOGSIZE:
110 Last_secret_level = -1;
113 for (i=0; i < Last_level - 1; i++)
114 sprintf(Level_names[i], "level%02d.rdl", i+1);
115 sprintf(Level_names[i], "saturn%02d.rdl", i+1);
116 for (i=0; i < -Last_secret_level; i++)
117 sprintf(Secret_level_names[i], "levels%1d.rdl", i+1);
119 Secret_level_table[0] = 10;
123 Int3(); // fall through
124 case D1_MISSION_HOGSIZE:
125 case D1_10_MISSION_HOGSIZE:
126 case D1_MAC_MISSION_HOGSIZE:
129 Last_level = BIM_LAST_LEVEL;
130 Last_secret_level = BIM_LAST_SECRET_LEVEL;
133 for (i=0;i<Last_level;i++)
134 sprintf(Level_names[i], "level%02d.rdl", i+1);
135 for (i=0;i<-Last_secret_level;i++)
136 sprintf(Secret_level_names[i], "levels%1d.rdl", i+1);
138 Secret_level_table[0] = 10;
139 Secret_level_table[1] = 21;
140 Secret_level_table[2] = 24;
144 strcpy(Briefing_text_filename,BIM_BRIEFING_FILE);
145 strcpy(Ending_text_filename,BIM_ENDING_FILE);
152 // Special versions of mission routines for shareware
155 int load_mission_shareware(void)
157 strcpy(Current_mission->mission_name, SHAREWARE_MISSION_NAME);
158 Current_mission->descent_version = 2;
159 Current_mission->anarchy_only_flag = 0;
161 switch (Current_mission->builtin_hogsize) {
162 case MAC_SHARE_MISSION_HOGSIZE:
166 Last_secret_level = -1;
168 // mac demo is using the regular hog and rl2 files
169 strcpy(Level_names[0],"d2leva-1.rl2");
170 strcpy(Level_names[1],"d2leva-2.rl2");
171 strcpy(Level_names[2],"d2leva-3.rl2");
172 strcpy(Level_names[3],"d2leva-4.rl2");
173 strcpy(Secret_level_names[0],"d2leva-s.rl2");
176 Int3(); // fall through
177 case SHAREWARE_MISSION_HOGSIZE:
181 Last_secret_level = 0;
183 strcpy(Level_names[0],"d2leva-1.sl2");
184 strcpy(Level_names[1],"d2leva-2.sl2");
185 strcpy(Level_names[2],"d2leva-3.sl2");
193 // Special versions of mission routines for Diamond/S3 version
196 int load_mission_oem(void)
198 strcpy(Current_mission->mission_name, OEM_MISSION_NAME);
199 Current_mission->descent_version = 2;
200 Current_mission->anarchy_only_flag = 0;
205 Last_secret_level = -2;
207 strcpy(Level_names[0],"d2leva-1.rl2");
208 strcpy(Level_names[1],"d2leva-2.rl2");
209 strcpy(Level_names[2],"d2leva-3.rl2");
210 strcpy(Level_names[3],"d2leva-4.rl2");
212 strcpy(Secret_level_names[0],"d2leva-s.rl2");
214 strcpy(Level_names[4],"d2levb-1.rl2");
215 strcpy(Level_names[5],"d2levb-2.rl2");
216 strcpy(Level_names[6],"d2levb-3.rl2");
217 strcpy(Level_names[7],"d2levb-4.rl2");
219 strcpy(Secret_level_names[1],"d2levb-s.rl2");
221 Secret_level_table[0] = 1;
222 Secret_level_table[1] = 5;
228 //compare a string for a token. returns true if match
229 int istok(char *buf,char *tok)
231 return strnicmp(buf,tok,strlen(tok)) == 0;
235 //adds a terminating 0 after a string at the first white space
236 void add_term(char *s)
238 while (*s && !isspace(*s)) s++;
243 //returns ptr to string after '=' & white space, or NULL if no '='
244 //adds 0 after parm at first white space
245 char *get_value(char *buf)
249 t = strchr(buf,'=')+1;
252 while (*t && isspace(*t)) t++;
258 return NULL; //error!
261 //reads a line, returns ptr to value of passed parm. returns NULL if none
262 char *get_parm_value(char *parm,CFILE *f)
266 if (!cfgets(buf,80,f))
270 return get_value(buf);
275 int ml_sort_func(mle *e0,mle *e1)
277 return stricmp(e0->mission_name,e1->mission_name);
281 extern char CDROM_dir[];
283 //returns 1 if file read ok, else 0
284 int read_mission_file(mle *mission, char *filename, int location)
289 //printf("reading: %s\n", filename);
293 strcpy(filename2,MISSION_DIR);
297 songs_stop_redbook(); //so we can read from the CD
298 strcpy(filename2,CDROM_dir);
302 Int3(); //fall through
305 strcpy(filename2,"");
308 strcat(filename2,filename);
310 mfile = cfopen(filename2,"rb");
314 char temp[FILENAME_LEN],*t;
316 strcpy(temp,filename);
317 if ((t = strchr(temp,'.')) == NULL)
318 return 0; //missing extension
319 // look if it's .mn2 or .msn
320 mission->descent_version = (t[3] == '2') ? 2 : 1;
321 *t = 0; //kill extension
323 strncpy( mission->filename, temp, 9 );
324 mission->anarchy_only_flag = 0;
325 mission->location = location;
327 p = get_parm_value("name",mfile);
329 if (!p) { //try enhanced mission
330 cfseek(mfile,0,SEEK_SET);
331 p = get_parm_value("xname",mfile);
334 if (!p) { //try super-enhanced mission!
335 cfseek(mfile,0,SEEK_SET);
336 p = get_parm_value("zname",mfile);
341 if ((t=strchr(p,';'))!=NULL)
344 while (isspace(*t)) t--;
345 strncpy(mission->mission_name,p,MISSION_NAME_LEN);
352 p = get_parm_value("type",mfile);
356 mission->anarchy_only_flag = istok(p,"anarchy");
366 void add_d1_builtin_mission_to_list(mle *mission)
370 if (!cfexist("descent.hog"))
373 size = cfile_size("descent.hog");
376 case D1_SHAREWARE_MISSION_HOGSIZE:
377 case D1_SHAREWARE_10_MISSION_HOGSIZE:
378 case D1_MAC_SHARE_MISSION_HOGSIZE:
379 strcpy(mission->filename, D1_MISSION_FILENAME);
380 strcpy(mission->mission_name, D1_SHAREWARE_MISSION_NAME);
381 mission->anarchy_only_flag = 0;
383 case D1_OEM_MISSION_HOGSIZE:
384 case D1_OEM_10_MISSION_HOGSIZE:
385 strcpy(mission->filename, D1_MISSION_FILENAME);
386 strcpy(mission->mission_name, D1_OEM_MISSION_NAME);
387 mission->anarchy_only_flag = 0;
390 Warning("Unknown D1 hogsize %d\n", size);
393 case D1_MISSION_HOGSIZE:
394 case D1_10_MISSION_HOGSIZE:
395 case D1_MAC_MISSION_HOGSIZE:
396 strcpy(mission->filename, D1_MISSION_FILENAME);
397 strcpy(mission->mission_name, D1_MISSION_NAME);
398 mission->anarchy_only_flag = 0;
402 mission->descent_version = 1;
403 mission->anarchy_only_flag = 0;
408 void add_builtin_mission_to_list(mle *mission, char *name)
410 int size = cfile_size("descent2.hog");
413 size = cfile_size("d2demo.hog");
416 case SHAREWARE_MISSION_HOGSIZE:
417 case MAC_SHARE_MISSION_HOGSIZE:
418 strcpy(mission->filename,SHAREWARE_MISSION_FILENAME);
419 strcpy(mission->mission_name,SHAREWARE_MISSION_NAME);
420 mission->anarchy_only_flag = 0;
422 case OEM_MISSION_HOGSIZE:
423 strcpy(mission->filename,OEM_MISSION_FILENAME);
424 strcpy(mission->mission_name,OEM_MISSION_NAME);
425 mission->anarchy_only_flag = 0;
428 Warning("Unknown hogsize %d, trying %s\n", size, FULL_MISSION_FILENAME ".mn2");
429 Int3(); //fall through
430 case FULL_MISSION_HOGSIZE:
431 case FULL_10_MISSION_HOGSIZE:
432 case MAC_FULL_MISSION_HOGSIZE:
433 if (!read_mission_file(mission, FULL_MISSION_FILENAME ".mn2", ML_CURDIR))
434 Error("Could not find required mission file <%s>", FULL_MISSION_FILENAME ".mn2");
437 strcpy(name, mission->filename);
438 mission->builtin_hogsize = size;
439 mission->descent_version = 2;
440 mission->anarchy_only_flag = 0;
445 void add_missions_to_list(mle *mission, int anarchy_mode)
447 char **find, **i, *ext;
449 find = PHYSFS_enumerateFiles(MISSION_DIR);
451 for (i = find; *i != NULL; i++)
453 ext = strrchr(*i, '.');
454 if (ext && (!strnicmp(ext, ".msn", 4) || !strnicmp(ext, ".mn2", 4)))
455 if (read_mission_file(mission, *i, ML_MISSIONDIR))
456 if (anarchy_mode || !mission->anarchy_only_flag) {
459 mission->builtin_hogsize = 0;
461 if (num_missions >= MAX_MISSIONS)
463 mprintf((0, "Warning: more missions than d2x can handle\n"));
468 PHYSFS_freeList(find);
471 /* move <mission_name> to <place> on mission list, increment <place> */
472 void promote (mle *mission_list, char * mission_name, int * top_place)
475 char name[FILENAME_LEN], * t;
476 strcpy(name, mission_name);
477 if ((t = strchr(name,'.')) != NULL)
478 *t = 0; //kill extension
479 //printf("promoting: %s\n", name);
480 for (i = *top_place; i < num_missions; i++)
481 if (!stricmp(mission_list[i].filename, name)) {
482 //swap mission positions
485 temp = mission_list[*top_place];
486 mission_list[*top_place] = mission_list[i];
487 mission_list[i] = temp;
493 void free_mission(void)
495 // May become more complex with the editor
496 if (Current_mission) {
497 d_free(Current_mission);
503 //fills in the global list of missions. Returns the number of missions
504 //in the list. If anarchy_mode is set, then also add anarchy-only missions.
506 extern char CDROM_dir[];
508 extern char AltHogDir[];
509 extern char AltHogdir_initialized;
512 mle *build_mission_list(int anarchy_mode)
516 char builtin_mission_filename[FILENAME_LEN];
518 //now search for levels on disk
520 //@@Took out this code because after this routine was called once for
521 //@@a list of single-player missions, a subsequent call for a list of
522 //@@anarchy missions would not scan again, and thus would not find the
523 //@@anarchy-only missions. If we retain the minimum level of install,
524 //@@we may want to put the code back in, having it always scan for all
525 //@@missions, and have the code that uses it sort out the ones it wants.
526 //@@ if (num_missions != -1) {
527 //@@ if (Current_mission_num != 0)
528 //@@ load_mission(0); //set built-in mission as default
529 //@@ return num_missions;
532 MALLOC(mission_list, mle, MAX_MISSIONS);
535 add_builtin_mission_to_list(mission_list + num_missions, builtin_mission_filename); //read built-in first
536 add_d1_builtin_mission_to_list(mission_list + num_missions);
537 add_missions_to_list(mission_list + num_missions, anarchy_mode);
539 // move original missions (in story-chronological order)
540 // to top of mission list
542 promote(mission_list, "descent", &top_place); // original descent 1 mission
543 promote(mission_list, builtin_mission_filename, &top_place); // d2 or d2demo
544 promote(mission_list, "d2x", &top_place); // vertigo
546 if (num_missions > top_place)
547 qsort(&mission_list[top_place],
548 num_missions - top_place,
549 sizeof(*mission_list),
550 (int (*)( const void *, const void * ))ml_sort_func);
553 if (num_missions > top_place)
554 qsort(&mission_list[top_place],
555 num_missions - top_place,
556 sizeof(*mission_list),
557 (int (*)( const void *, const void * ))ml_sort_func);
559 //load_mission(0); //set built-in mission as default
561 atexit(free_mission);
566 void free_mission_list(mle *mission_list)
568 d_free(mission_list);
572 void init_extra_robot_movie(char *filename);
574 //values for built-in mission
576 //loads the specfied mission from the mission list.
577 //build_mission_list() must have been called.
578 //Returns true if mission loaded ok, else false.
579 int load_mission(mle *mission)
587 Current_mission = d_malloc(sizeof(Mission));
588 if (!Current_mission) return 0;
589 *(mle *) Current_mission = *mission;
591 // for Descent 1 missions, load descent.hog
593 if (!cfile_init("descent.hog"))
594 Warning("descent.hog not available, this mission may be missing some files required for briefings and exit sequence\n");
595 if (!stricmp(Current_mission_filename, D1_MISSION_FILENAME))
596 return load_mission_d1();
599 if (PLAYING_BUILTIN_MISSION) {
600 switch (Current_mission->builtin_hogsize) {
601 case SHAREWARE_MISSION_HOGSIZE:
602 case MAC_SHARE_MISSION_HOGSIZE:
603 return load_mission_shareware();
605 case OEM_MISSION_HOGSIZE:
606 return load_mission_oem();
609 Int3(); // fall through
610 case FULL_MISSION_HOGSIZE:
611 case FULL_10_MISSION_HOGSIZE:
612 case MAC_FULL_MISSION_HOGSIZE:
613 // continue on... (use d2.mn2 from hogfile)
618 mprintf(( 0, "Loading mission %s\n", Current_mission_filename ));
620 //read mission from file
622 switch (mission->location) {
624 strcpy(buf,MISSION_DIR);
627 strcpy(buf,CDROM_dir);
630 Int3(); //fall through
635 strcat(buf, mission->filename);
636 if (mission->descent_version == 2)
641 PHYSFSEXT_locateCorrectCase(buf);
643 mfile = cfopen(buf,"rb");
649 //for non-builtin missions, load HOG
650 if (!PLAYING_BUILTIN_MISSION) {
652 strcpy(buf+strlen(buf)-4,".hog"); //change extension
654 PHYSFSEXT_locateCorrectCase(buf);
656 found_hogfile = cfile_init(buf);
658 #ifdef RELEASE //for release, require mission to be in hogfile
659 if (! found_hogfile) {
669 Last_secret_level = 0;
670 Briefing_text_filename[0] = 0;
671 Ending_text_filename[0] = 0;
673 while (cfgets(buf,80,mfile)) {
675 if (istok(buf,"name")) {
676 Current_mission->enhanced = 0;
677 continue; //already have name, go to next line
679 if (istok(buf,"xname")) {
680 Current_mission->enhanced = 1;
681 continue; //already have name, go to next line
683 if (istok(buf,"zname")) {
684 Current_mission->enhanced = 2;
685 continue; //already have name, go to next line
687 else if (istok(buf,"type"))
688 continue; //already have name, go to next line
689 else if (istok(buf,"hog")) {
692 while (*(bufp++) != '=')
696 while (*(++bufp) == ' ')
700 mprintf((0, "Hog file override = [%s]\n", bufp));
702 else if (istok(buf,"briefing")) {
703 if ((v = get_value(buf)) != NULL) {
706 strcpy(Briefing_text_filename,v);
709 else if (istok(buf,"ending")) {
710 if ((v = get_value(buf)) != NULL) {
713 strcpy(Ending_text_filename,v);
716 else if (istok(buf,"num_levels")) {
718 if ((v=get_value(buf))!=NULL) {
723 for (i=0;i<n_levels && cfgets(buf,80,mfile);i++) {
726 if (strlen(buf) <= 12) {
727 strcpy(Level_names[i],buf);
736 else if (istok(buf,"num_secrets")) {
737 if ((v=get_value(buf))!=NULL) {
740 N_secret_levels = atoi(v);
742 Assert(N_secret_levels <= MAX_SECRET_LEVELS_PER_MISSION);
744 for (i=0;i<N_secret_levels && cfgets(buf,80,mfile);i++) {
748 if ((t=strchr(buf,','))!=NULL) *t++=0;
753 if (strlen(buf) <= 12) {
754 strcpy(Secret_level_names[i],buf);
755 Secret_level_table[i] = atoi(t);
756 if (Secret_level_table[i]<1 || Secret_level_table[i]>Last_level)
771 if (Last_level <= 0) {
772 free_mission(); //no valid mission loaded
776 if (Current_mission->enhanced) {
778 extern void bm_read_extra_robots();
779 sprintf(t,"%s.ham",Current_mission_filename);
780 bm_read_extra_robots(t, Current_mission->enhanced);
781 strncpy(t,Current_mission_filename,6);
782 init_extra_robot_movie(t);
788 //loads the named mission if exists.
789 //Returns true if mission loaded ok, else false.
790 int load_mission_by_name(char *mission_name)
793 mle *mission_list = build_mission_list(1);
796 for (i = 0; i < num_missions; i++)
797 if (!stricmp(mission_name, mission_list[i].filename))
798 found = load_mission(mission_list + i);
800 free_mission_list(mission_list);
804 int select_mission(int anarchy_mode, char *message)
806 mle *mission_list = build_mission_list(anarchy_mode);
809 if (num_missions <= 1) {
810 new_mission_num = load_mission(mission_list) ? 0 : -1;
812 int i, default_mission;
813 char * m[MAX_MISSIONS];
816 for (i = 0; i < num_missions; i++) {
817 m[i] = mission_list[i].mission_name;
818 if ( !stricmp( m[i], config_last_mission ) )
822 new_mission_num = newmenu_listbox1( message, num_missions, m, 1, default_mission, NULL );
824 if (new_mission_num >= 0) {
826 strcpy(config_last_mission, m[new_mission_num] );
828 if (!load_mission(mission_list + new_mission_num)) {
829 nm_messagebox( NULL, 1, TXT_OK, TXT_MISSION_ERROR);
830 new_mission_num = -1;
835 free_mission_list(mission_list);
836 return (new_mission_num >= 0);