1 /* $Id: mission.c,v 1.41 2005-02-25 12:07:08 chris 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 *filename; // filename without extension
57 int builtin_hogsize; // if it's the built-in mission, used for determining the version
58 char mission_name[MISSION_NAME_LEN+1];
59 ubyte descent_version; // descent 1 or descent 2?
60 ubyte anarchy_only_flag; // if true, mission is anarchy only
61 char *path; // relative file path
62 int location; // see defines below
65 //values that describe where a mission is located
67 #define ML_MISSIONDIR 1
70 int num_missions = -1;
72 Mission *Current_mission = NULL; // currently loaded mission
75 // Special versions of mission routines for d1 builtins
78 int load_mission_d1(void)
82 switch (cfile_size("descent.hog")) {
83 case D1_SHAREWARE_MISSION_HOGSIZE:
84 case D1_SHAREWARE_10_MISSION_HOGSIZE:
88 Last_secret_level = 0;
91 for (i=0;i<Last_level;i++)
92 sprintf(Level_names[i], "level%02d.sdl", i+1);
95 case D1_MAC_SHARE_MISSION_HOGSIZE:
99 Last_secret_level = 0;
102 for (i=0;i<Last_level;i++)
103 sprintf(Level_names[i], "level%02d.sdl", i+1);
106 case D1_OEM_MISSION_HOGSIZE:
107 case D1_OEM_10_MISSION_HOGSIZE:
111 Last_secret_level = -1;
114 for (i=0; i < Last_level - 1; i++)
115 sprintf(Level_names[i], "level%02d.rdl", i+1);
116 sprintf(Level_names[i], "saturn%02d.rdl", i+1);
117 for (i=0; i < -Last_secret_level; i++)
118 sprintf(Secret_level_names[i], "levels%1d.rdl", i+1);
120 Secret_level_table[0] = 10;
124 Int3(); // fall through
125 case D1_MISSION_HOGSIZE:
126 case D1_10_MISSION_HOGSIZE:
127 case D1_MAC_MISSION_HOGSIZE:
130 Last_level = BIM_LAST_LEVEL;
131 Last_secret_level = BIM_LAST_SECRET_LEVEL;
134 for (i=0;i<Last_level;i++)
135 sprintf(Level_names[i], "level%02d.rdl", i+1);
136 for (i=0;i<-Last_secret_level;i++)
137 sprintf(Secret_level_names[i], "levels%1d.rdl", i+1);
139 Secret_level_table[0] = 10;
140 Secret_level_table[1] = 21;
141 Secret_level_table[2] = 24;
145 strcpy(Briefing_text_filename,BIM_BRIEFING_FILE);
146 strcpy(Ending_text_filename,BIM_ENDING_FILE);
153 // Special versions of mission routines for shareware
156 int load_mission_shareware(void)
158 strcpy(Current_mission->mission_name, SHAREWARE_MISSION_NAME);
159 Current_mission->descent_version = 2;
160 Current_mission->anarchy_only_flag = 0;
162 switch (Current_mission->builtin_hogsize) {
163 case MAC_SHARE_MISSION_HOGSIZE:
167 Last_secret_level = -1;
169 // mac demo is using the regular hog and rl2 files
170 strcpy(Level_names[0],"d2leva-1.rl2");
171 strcpy(Level_names[1],"d2leva-2.rl2");
172 strcpy(Level_names[2],"d2leva-3.rl2");
173 strcpy(Level_names[3],"d2leva-4.rl2");
174 strcpy(Secret_level_names[0],"d2leva-s.rl2");
177 Int3(); // fall through
178 case SHAREWARE_MISSION_HOGSIZE:
182 Last_secret_level = 0;
184 strcpy(Level_names[0],"d2leva-1.sl2");
185 strcpy(Level_names[1],"d2leva-2.sl2");
186 strcpy(Level_names[2],"d2leva-3.sl2");
194 // Special versions of mission routines for Diamond/S3 version
197 int load_mission_oem(void)
199 strcpy(Current_mission->mission_name, OEM_MISSION_NAME);
200 Current_mission->descent_version = 2;
201 Current_mission->anarchy_only_flag = 0;
206 Last_secret_level = -2;
208 strcpy(Level_names[0],"d2leva-1.rl2");
209 strcpy(Level_names[1],"d2leva-2.rl2");
210 strcpy(Level_names[2],"d2leva-3.rl2");
211 strcpy(Level_names[3],"d2leva-4.rl2");
213 strcpy(Secret_level_names[0],"d2leva-s.rl2");
215 strcpy(Level_names[4],"d2levb-1.rl2");
216 strcpy(Level_names[5],"d2levb-2.rl2");
217 strcpy(Level_names[6],"d2levb-3.rl2");
218 strcpy(Level_names[7],"d2levb-4.rl2");
220 strcpy(Secret_level_names[1],"d2levb-s.rl2");
222 Secret_level_table[0] = 1;
223 Secret_level_table[1] = 5;
229 //compare a string for a token. returns true if match
230 int istok(char *buf,char *tok)
232 return strnicmp(buf,tok,strlen(tok)) == 0;
236 //adds a terminating 0 after a string at the first white space
237 void add_term(char *s)
239 while (*s && !isspace(*s)) s++;
244 //returns ptr to string after '=' & white space, or NULL if no '='
245 //adds 0 after parm at first white space
246 char *get_value(char *buf)
250 t = strchr(buf,'=')+1;
253 while (*t && isspace(*t)) t++;
259 return NULL; //error!
262 //reads a line, returns ptr to value of passed parm. returns NULL if none
263 char *get_parm_value(char *parm,CFILE *f)
267 if (!cfgets(buf,80,f))
271 return get_value(buf);
276 int ml_sort_func(mle *e0,mle *e1)
278 return stricmp(e0->mission_name,e1->mission_name);
282 extern char CDROM_dir[];
284 //returns 1 if file read ok, else 0
285 int read_mission_file(mle *mission, char *filename, int location)
290 //printf("reading: %s\n", filename);
294 strcpy(filename2,MISSION_DIR);
298 songs_stop_redbook(); //so we can read from the CD
299 strcpy(filename2,CDROM_dir);
303 Int3(); //fall through
306 strcpy(filename2,"");
309 strcat(filename2,filename);
311 mfile = cfopen(filename2,"rb");
315 char temp[PATH_MAX], *ext;
317 strcpy(temp,filename);
318 p = strrchr(temp, '/'); // get the filename at the end of the path
323 if ((ext = strchr(p, '.')) == NULL)
324 return 0; //missing extension
325 // look if it's .mn2 or .msn
326 mission->descent_version = (ext[3] == '2') ? 2 : 1;
327 *ext = 0; //kill extension
329 mission->path = d_strdup(temp);
330 mission->anarchy_only_flag = 0;
331 mission->filename = mission->path + (p - temp);
332 mission->location = location;
334 p = get_parm_value("name",mfile);
336 if (!p) { //try enhanced mission
337 cfseek(mfile,0,SEEK_SET);
338 p = get_parm_value("xname",mfile);
341 if (!p) { //try super-enhanced mission!
342 cfseek(mfile,0,SEEK_SET);
343 p = get_parm_value("zname",mfile);
348 if ((t=strchr(p,';'))!=NULL)
352 *t-- = 0; // remove trailing whitespace
353 if (strlen(p) > MISSION_NAME_LEN)
354 p[MISSION_NAME_LEN] = 0;
355 strncpy(mission->mission_name, p, MISSION_NAME_LEN + 1);
359 d_free(mission->path);
363 p = get_parm_value("type",mfile);
367 mission->anarchy_only_flag = istok(p,"anarchy");
377 void add_d1_builtin_mission_to_list(mle *mission)
381 if (!cfexist("descent.hog"))
384 size = cfile_size("descent.hog");
387 case D1_SHAREWARE_MISSION_HOGSIZE:
388 case D1_SHAREWARE_10_MISSION_HOGSIZE:
389 case D1_MAC_SHARE_MISSION_HOGSIZE:
390 mission->filename = d_strdup(D1_MISSION_FILENAME);
391 strcpy(mission->mission_name, D1_SHAREWARE_MISSION_NAME);
392 mission->anarchy_only_flag = 0;
394 case D1_OEM_MISSION_HOGSIZE:
395 case D1_OEM_10_MISSION_HOGSIZE:
396 mission->filename = d_strdup(D1_MISSION_FILENAME);
397 strcpy(mission->mission_name, D1_OEM_MISSION_NAME);
398 mission->anarchy_only_flag = 0;
401 Warning("Unknown D1 hogsize %d\n", size);
404 case D1_MISSION_HOGSIZE:
405 case D1_10_MISSION_HOGSIZE:
406 case D1_MAC_MISSION_HOGSIZE:
407 mission->filename = d_strdup(D1_MISSION_FILENAME);
408 strcpy(mission->mission_name, D1_MISSION_NAME);
409 mission->anarchy_only_flag = 0;
413 mission->descent_version = 1;
414 mission->anarchy_only_flag = 0;
415 mission->builtin_hogsize = 0;
416 mission->path = mission->filename;
421 void add_builtin_mission_to_list(mle *mission, char *name)
423 int size = cfile_size("descent2.hog");
426 size = cfile_size("d2demo.hog");
429 case SHAREWARE_MISSION_HOGSIZE:
430 case MAC_SHARE_MISSION_HOGSIZE:
431 mission->filename = d_strdup(SHAREWARE_MISSION_FILENAME);
432 strcpy(mission->mission_name,SHAREWARE_MISSION_NAME);
433 mission->anarchy_only_flag = 0;
435 case OEM_MISSION_HOGSIZE:
436 mission->filename = d_strdup(OEM_MISSION_FILENAME);
437 strcpy(mission->mission_name,OEM_MISSION_NAME);
438 mission->anarchy_only_flag = 0;
441 Warning("Unknown hogsize %d, trying %s\n", size, FULL_MISSION_FILENAME ".mn2");
442 Int3(); //fall through
443 case FULL_MISSION_HOGSIZE:
444 case FULL_10_MISSION_HOGSIZE:
445 case MAC_FULL_MISSION_HOGSIZE:
446 if (!read_mission_file(mission, FULL_MISSION_FILENAME ".mn2", ML_CURDIR))
447 Error("Could not find required mission file <%s>", FULL_MISSION_FILENAME ".mn2");
450 strcpy(name, mission->filename);
451 mission->builtin_hogsize = size;
452 mission->descent_version = 2;
453 mission->anarchy_only_flag = 0;
458 void add_missions_to_list(mle *mission, int anarchy_mode)
460 char **find, **i, *ext;
462 find = PHYSFS_enumerateFiles(MISSION_DIR);
464 for (i = find; *i != NULL; i++)
466 ext = strrchr(*i, '.');
467 if (ext && (!strnicmp(ext, ".msn", 4) || !strnicmp(ext, ".mn2", 4)))
468 if (read_mission_file(mission, *i, ML_MISSIONDIR))
470 if (anarchy_mode || !mission->anarchy_only_flag) {
471 mission->builtin_hogsize = 0;
476 d_free(mission->filename);
478 if (num_missions >= MAX_MISSIONS)
480 mprintf((0, "Warning: more missions than d2x can handle\n"));
485 PHYSFS_freeList(find);
488 /* move <mission_name> to <place> on mission list, increment <place> */
489 void promote (mle *mission_list, char * mission_name, int * top_place)
492 char name[FILENAME_LEN], * t;
493 strcpy(name, mission_name);
494 if ((t = strchr(name,'.')) != NULL)
495 *t = 0; //kill extension
496 //printf("promoting: %s\n", name);
497 for (i = *top_place; i < num_missions; i++)
498 if (!stricmp(mission_list[i].filename, name)) {
499 //swap mission positions
502 temp = mission_list[*top_place];
503 mission_list[*top_place] = mission_list[i];
504 mission_list[i] = temp;
510 void free_mission(void)
512 // May become more complex with the editor
513 if (Current_mission) {
514 d_free(Current_mission->filename);
515 d_free(Current_mission);
521 //fills in the global list of missions. Returns the number of missions
522 //in the list. If anarchy_mode is set, then also add anarchy-only missions.
524 extern char CDROM_dir[];
526 extern char AltHogDir[];
527 extern char AltHogdir_initialized;
530 mle *build_mission_list(int anarchy_mode)
534 char builtin_mission_filename[FILENAME_LEN];
536 //now search for levels on disk
538 //@@Took out this code because after this routine was called once for
539 //@@a list of single-player missions, a subsequent call for a list of
540 //@@anarchy missions would not scan again, and thus would not find the
541 //@@anarchy-only missions. If we retain the minimum level of install,
542 //@@we may want to put the code back in, having it always scan for all
543 //@@missions, and have the code that uses it sort out the ones it wants.
544 //@@ if (num_missions != -1) {
545 //@@ if (Current_mission_num != 0)
546 //@@ load_mission(0); //set built-in mission as default
547 //@@ return num_missions;
550 MALLOC(mission_list, mle, MAX_MISSIONS);
553 add_builtin_mission_to_list(mission_list + num_missions, builtin_mission_filename); //read built-in first
554 add_d1_builtin_mission_to_list(mission_list + num_missions);
555 add_missions_to_list(mission_list + num_missions, anarchy_mode);
557 // move original missions (in story-chronological order)
558 // to top of mission list
560 promote(mission_list, "descent", &top_place); // original descent 1 mission
561 promote(mission_list, builtin_mission_filename, &top_place); // d2 or d2demo
562 promote(mission_list, "d2x", &top_place); // vertigo
564 if (num_missions > top_place)
565 qsort(&mission_list[top_place],
566 num_missions - top_place,
567 sizeof(*mission_list),
568 (int (*)( const void *, const void * ))ml_sort_func);
571 if (num_missions > top_place)
572 qsort(&mission_list[top_place],
573 num_missions - top_place,
574 sizeof(*mission_list),
575 (int (*)( const void *, const void * ))ml_sort_func);
577 //load_mission(0); //set built-in mission as default
579 atexit(free_mission);
584 void free_mission_list(mle *mission_list)
588 for (i = 0; i < num_missions; i++)
589 d_free(mission_list[i].path);
591 d_free(mission_list);
595 void init_extra_robot_movie(char *filename);
597 //values for built-in mission
599 //loads the specfied mission from the mission list.
600 //build_mission_list() must have been called.
601 //Returns true if mission loaded ok, else false.
602 int load_mission(mle *mission)
605 char buf[PATH_MAX], *v;
610 Current_mission = d_malloc(sizeof(Mission));
611 if (!Current_mission) return 0;
612 *(mle *) Current_mission = *mission;
613 Current_mission->filename = d_strdup(mission->filename); // don't want to lose it
615 // for Descent 1 missions, load descent.hog
617 if (!cfile_init("descent.hog"))
618 Warning("descent.hog not available, this mission may be missing some files required for briefings and exit sequence\n");
619 if (!stricmp(Current_mission_filename, D1_MISSION_FILENAME))
620 return load_mission_d1();
623 if (PLAYING_BUILTIN_MISSION) {
624 switch (Current_mission->builtin_hogsize) {
625 case SHAREWARE_MISSION_HOGSIZE:
626 case MAC_SHARE_MISSION_HOGSIZE:
627 return load_mission_shareware();
629 case OEM_MISSION_HOGSIZE:
630 return load_mission_oem();
633 Int3(); // fall through
634 case FULL_MISSION_HOGSIZE:
635 case FULL_10_MISSION_HOGSIZE:
636 case MAC_FULL_MISSION_HOGSIZE:
637 // continue on... (use d2.mn2 from hogfile)
642 mprintf(( 0, "Loading mission %s\n", Current_mission_filename ));
644 //read mission from file
646 switch (mission->location) {
648 strcpy(buf,MISSION_DIR);
651 strcpy(buf,CDROM_dir);
654 Int3(); //fall through
659 strcat(buf, mission->path);
660 if (mission->descent_version == 2)
665 PHYSFSEXT_locateCorrectCase(buf);
667 mfile = cfopen(buf,"rb");
673 //for non-builtin missions, load HOG
674 if (!PLAYING_BUILTIN_MISSION) {
676 strcpy(buf+strlen(buf)-4,".hog"); //change extension
678 PHYSFSEXT_locateCorrectCase(buf);
680 found_hogfile = cfile_init(buf);
682 #ifdef RELEASE //for release, require mission to be in hogfile
683 if (! found_hogfile) {
693 Last_secret_level = 0;
694 Briefing_text_filename[0] = 0;
695 Ending_text_filename[0] = 0;
697 while (cfgets(buf,80,mfile)) {
699 if (istok(buf,"name")) {
700 Current_mission->enhanced = 0;
701 continue; //already have name, go to next line
703 if (istok(buf,"xname")) {
704 Current_mission->enhanced = 1;
705 continue; //already have name, go to next line
707 if (istok(buf,"zname")) {
708 Current_mission->enhanced = 2;
709 continue; //already have name, go to next line
711 else if (istok(buf,"type"))
712 continue; //already have name, go to next line
713 else if (istok(buf,"hog")) {
716 while (*(bufp++) != '=')
720 while (*(++bufp) == ' ')
724 mprintf((0, "Hog file override = [%s]\n", bufp));
726 else if (istok(buf,"briefing")) {
727 if ((v = get_value(buf)) != NULL) {
730 strcpy(Briefing_text_filename,v);
733 else if (istok(buf,"ending")) {
734 if ((v = get_value(buf)) != NULL) {
737 strcpy(Ending_text_filename,v);
740 else if (istok(buf,"num_levels")) {
742 if ((v=get_value(buf))!=NULL) {
747 for (i=0;i<n_levels && cfgets(buf,80,mfile);i++) {
750 if (strlen(buf) <= 12) {
751 strcpy(Level_names[i],buf);
760 else if (istok(buf,"num_secrets")) {
761 if ((v=get_value(buf))!=NULL) {
764 N_secret_levels = atoi(v);
766 Assert(N_secret_levels <= MAX_SECRET_LEVELS_PER_MISSION);
768 for (i=0;i<N_secret_levels && cfgets(buf,80,mfile);i++) {
772 if ((t=strchr(buf,','))!=NULL) *t++=0;
777 if (strlen(buf) <= 12) {
778 strcpy(Secret_level_names[i],buf);
779 Secret_level_table[i] = atoi(t);
780 if (Secret_level_table[i]<1 || Secret_level_table[i]>Last_level)
795 if (Last_level <= 0) {
796 free_mission(); //no valid mission loaded
800 if (Current_mission->enhanced) {
802 extern void bm_read_extra_robots();
803 sprintf(t,"%s.ham",Current_mission_filename);
804 bm_read_extra_robots(t, Current_mission->enhanced);
805 strncpy(t,Current_mission_filename,6);
806 init_extra_robot_movie(t);
812 //loads the named mission if exists.
813 //Returns true if mission loaded ok, else false.
814 int load_mission_by_name(char *mission_name)
817 mle *mission_list = build_mission_list(1);
820 for (i = 0; i < num_missions; i++)
821 if (!stricmp(mission_name, mission_list[i].filename))
822 found = load_mission(mission_list + i);
824 free_mission_list(mission_list);
828 int select_mission(int anarchy_mode, char *message)
830 mle *mission_list = build_mission_list(anarchy_mode);
833 if (num_missions <= 1) {
834 new_mission_num = load_mission(mission_list) ? 0 : -1;
836 int i, default_mission;
837 char * m[MAX_MISSIONS];
840 for (i = 0; i < num_missions; i++) {
841 m[i] = mission_list[i].mission_name;
842 if ( !stricmp( m[i], config_last_mission ) )
846 new_mission_num = newmenu_listbox1( message, num_missions, m, 1, default_mission, NULL );
848 if (new_mission_num >= 0) {
850 strcpy(config_last_mission, m[new_mission_num] );
852 if (!load_mission(mission_list + new_mission_num)) {
853 nm_messagebox( NULL, 1, TXT_OK, TXT_MISSION_ERROR);
854 new_mission_num = -1;
859 free_mission_list(mission_list);
860 return (new_mission_num >= 0);