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.
16 static char rcsid[] = "$Id: mission.c,v 1.1.1.1 2001-01-19 03:30:05 bradleyb Exp $";
39 mle Mission_list[MAX_MISSIONS];
41 int Current_mission_num;
42 int N_secret_levels; // Made a global by MK for scoring purposes. August 1, 1995.
43 char *Current_mission_filename,*Current_mission_longname;
45 //this stuff should get defined elsewhere
47 char Level_names[MAX_LEVELS_PER_MISSION][FILENAME_LEN];
48 char Secret_level_names[MAX_SECRET_LEVELS_PER_MISSION][FILENAME_LEN];
50 //where the missions go
52 #define MISSION_DIR "missions/"
54 #define MISSION_DIR "./"
60 // Special versions of mission routines for shareware
63 #define SHAREWARE_MISSION_FILENAME "d2demo"
64 #define SHAREWARE_MISSION_NAME "Descent 2 Demo"
66 int build_mission_list(int anarchy_mode)
68 anarchy_mode++; //kill warning
70 strcpy(Mission_list[0].filename,SHAREWARE_MISSION_FILENAME);
71 strcpy(Mission_list[0].mission_name,SHAREWARE_MISSION_NAME);
72 Mission_list[0].anarchy_only_flag = 0;
74 return load_mission(0);
77 int load_mission(int mission_num)
79 Assert(mission_num == 0);
81 Current_mission_num = 0;
82 Current_mission_filename = Mission_list[0].filename;
83 Current_mission_longname = Mission_list[0].mission_name;
87 Assert(Last_level == 3);
89 #ifdef MACINTOSH // mac demo is using the regular hog and rl2 files
90 strcpy(Level_names[0],"d2leva-1.rl2");
91 strcpy(Level_names[1],"d2leva-2.rl2");
92 strcpy(Level_names[2],"d2leva-3.rl2");
94 strcpy(Level_names[0],"d2leva-1.sl2");
95 strcpy(Level_names[1],"d2leva-2.sl2");
96 strcpy(Level_names[2],"d2leva-3.sl2");
97 #endif // end of ifdef macintosh
102 int load_mission_by_name(char *mission_name)
104 if (strcmp(mission_name,SHAREWARE_MISSION_FILENAME))
105 return 0; //cannot load requested mission
107 return load_mission(0);
118 // Special versions of mission routines for Diamond/S3 version
121 #define OEM_MISSION_FILENAME "d2"
122 #define OEM_MISSION_NAME "D2 Destination:Quartzon"
124 int build_mission_list(int anarchy_mode)
126 anarchy_mode++; //kill warning
128 strcpy(Mission_list[0].filename,OEM_MISSION_FILENAME);
129 strcpy(Mission_list[0].mission_name,OEM_MISSION_NAME);
130 Mission_list[0].anarchy_only_flag = 0;
132 return load_mission(0);
135 int load_mission(int mission_num)
137 Assert(mission_num == 0);
139 Current_mission_num = 0;
140 Current_mission_filename = Mission_list[0].filename;
141 Current_mission_longname = Mission_list[0].mission_name;
145 Assert(Last_level == 8);
147 strcpy(Level_names[0],"d2leva-1.rl2");
148 strcpy(Level_names[1],"d2leva-2.rl2");
149 strcpy(Level_names[2],"d2leva-3.rl2");
150 strcpy(Level_names[3],"d2leva-4.rl2");
152 strcpy(Secret_level_names[0],"d2leva-s.rl2");
154 strcpy(Level_names[4],"d2levb-1.rl2");
155 strcpy(Level_names[5],"d2levb-2.rl2");
156 strcpy(Level_names[6],"d2levb-3.rl2");
157 strcpy(Level_names[7],"d2levb-4.rl2");
159 strcpy(Secret_level_names[1],"d2levb-s.rl2");
161 Secret_level_table[0] = 1;
162 Secret_level_table[1] = 5;
167 int load_mission_by_name(char *mission_name)
169 if (strcmp(mission_name,OEM_MISSION_FILENAME))
170 return 0; //cannot load requested mission
172 return load_mission(0);
178 //strips damn newline from end of line
179 char *mfgets(char *s,int n,CFILE *f)
184 if (r && s[strlen(s)-1] == '\n')
190 //compare a string for a token. returns true if match
191 int istok(char *buf,char *tok)
193 return strnicmp(buf,tok,strlen(tok)) == 0;
197 //adds a terminating 0 after a string at the first white space
198 void add_term(char *s)
200 while (*s && !isspace(*s)) s++;
205 //returns ptr to string after '=' & white space, or NULL if no '='
206 //adds 0 after parm at first white space
207 char *get_value(char *buf)
211 t = strchr(buf,'=')+1;
214 while (*t && isspace(*t)) t++;
220 return NULL; //error!
223 //reads a line, returns ptr to value of passed parm. returns NULL if none
224 char *get_parm_value(char *parm,CFILE *f)
228 if (!mfgets(buf,80,f))
232 return get_value(buf);
237 int ml_sort_func(mle *e0,mle *e1)
239 return stricmp(e0->mission_name,e1->mission_name);
243 extern char CDROM_dir[];
244 extern int HoardEquipped();
246 #define BUILTIN_MISSION "d2.mn2"
248 //returns 1 if file read ok, else 0
249 int read_mission_file(char *filename,int count,int location)
256 strcpy(filename2,MISSION_DIR);
260 songs_stop_redbook(); //so we can read from the CD
261 strcpy(filename2,CDROM_dir);
265 Int3(); //fall through
268 strcpy(filename2,"");
271 strcat(filename2,filename);
273 mfile = cfopen(filename2,"rb");
277 char temp[FILENAME_LEN],*t;
279 strcpy(temp,filename);
280 if ((t = strchr(temp,'.')) == NULL)
281 return 0; //missing extension
282 *t = 0; //kill extension
284 strncpy( Mission_list[count].filename, temp, 9 );
285 Mission_list[count].anarchy_only_flag = 0;
286 Mission_list[count].location = location;
288 p = get_parm_value("name",mfile);
290 if (!p) { //try enhanced mission
291 cfseek(mfile,0,SEEK_SET);
292 p = get_parm_value("xname",mfile);
298 if (!p) { //try super-enhanced mission!
299 cfseek(mfile,0,SEEK_SET);
300 p = get_parm_value("zname",mfile);
307 if ((t=strchr(p,';'))!=NULL)
310 while (isspace(*t)) t--;
311 strncpy(Mission_list[count].mission_name,p,MISSION_NAME_LEN);
318 p = get_parm_value("type",mfile);
322 Mission_list[count].anarchy_only_flag = istok(p,"anarchy");
333 //fills in the global list of missions. Returns the number of missions
334 //in the list. If anarchy_mode set, don't include non-anarchy levels.
335 //if there is only one mission, this function will call load_mission on it.
337 extern char CDROM_dir[];
339 int build_mission_list(int anarchy_mode)
341 static int num_missions=-1;
342 int count=0,special_count=0;
344 char search_name[100] = MISSION_DIR "*.mn2";
346 //now search for levels on disk
348 //@@Took out this code because after this routine was called once for
349 //@@a list of single-player missions, a subsequent call for a list of
350 //@@anarchy missions would not scan again, and thus would not find the
351 //@@anarchy-only missions. If we retain the minimum level of install,
352 //@@we may want to put the code back in, having it always scan for all
353 //@@missions, and have the code that uses it sort out the ones it wants.
354 //@@ if (num_missions != -1) {
355 //@@ if (Current_mission_num != 0)
356 //@@ load_mission(0); //set built-in mission as default
357 //@@ return num_missions;
360 if (!read_mission_file(BUILTIN_MISSION,0,ML_CURDIR)) //read built-in first
361 Error("Could not find required mission file <%s>",BUILTIN_MISSION);
363 special_count = count=1;
365 if( !FileFindFirst( search_name, &find ) ) {
367 if (stricmp(find.name,BUILTIN_MISSION)==0)
368 continue; //skip the built-in
370 if (read_mission_file(find.name,count,ML_MISSIONDIR)) {
372 if (anarchy_mode || !Mission_list[count].anarchy_only_flag)
376 } while( !FileFindNext( &find ) && count<MAX_MISSIONS);
380 //move vertigo to top of mission list
384 for (i=special_count;i<count;i++)
385 if (!stricmp(Mission_list[i].filename,"D2X")) { //swap!
388 temp = Mission_list[special_count];
389 Mission_list[special_count] = Mission_list[i];
390 Mission_list[i] = temp;
399 if (count>special_count)
400 qsort(&Mission_list[special_count],count-special_count,sizeof(*Mission_list),
401 (int (*)( const void *, const void * ))ml_sort_func);
403 load_mission(0); //set built-in mission as default
405 num_missions = count;
410 void init_extra_robot_movie(char *filename);
412 //values for built-in mission
414 //loads the specfied mission from the mission list. build_mission_list()
415 //must have been called. If build_mission_list() returns 0, this function
416 //does not need to be called. Returns true if mission loaded ok, else false.
417 int load_mission(int mission_num)
422 int enhanced_mission = 0;
424 Current_mission_num = mission_num;
426 mprintf(( 0, "Loading mission %d\n", mission_num ));
428 //read mission from file
430 switch (Mission_list[mission_num].location) {
431 case ML_MISSIONDIR: strcpy(buf,MISSION_DIR); break;
432 case ML_CDROM: strcpy(buf,CDROM_dir); break;
433 default: Int3(); //fall through
434 case ML_CURDIR: strcpy(buf,""); break;
436 strcat(buf,Mission_list[mission_num].filename);
439 mfile = cfopen(buf,"rb");
441 Current_mission_num = -1;
445 if (mission_num != 0) { //for non-builtin missions, load HOG
447 strcpy(buf+strlen(buf)-4,".hog"); //change extension
449 found_hogfile = cfile_use_alternate_hogfile(buf);
451 #ifdef RELEASE //for release, require mission to be in hogfile
452 if (! found_hogfile) {
454 Current_mission_num = -1;
462 Last_secret_level = 0;
464 while (mfgets(buf,80,mfile)) {
466 if (istok(buf,"name"))
467 continue; //already have name, go to next line
468 if (istok(buf,"xname")) {
469 enhanced_mission = 1;
470 continue; //already have name, go to next line
472 if (istok(buf,"zname")) {
473 enhanced_mission = 2;
474 continue; //already have name, go to next line
476 else if (istok(buf,"type"))
477 continue; //already have name, go to next line
478 else if (istok(buf,"hog")) {
481 while (*(bufp++) != '=')
485 while (*(++bufp) == ' ')
488 cfile_use_alternate_hogfile(bufp);
489 mprintf((0, "Hog file override = [%s]\n", bufp));
491 else if (istok(buf,"num_levels")) {
493 if ((v=get_value(buf))!=NULL) {
498 for (i=0;i<n_levels && mfgets(buf,80,mfile);i++) {
501 if (strlen(buf) <= 12) {
502 strcpy(Level_names[i],buf);
511 else if (istok(buf,"num_secrets")) {
512 if ((v=get_value(buf))!=NULL) {
515 N_secret_levels = atoi(v);
517 Assert(N_secret_levels <= MAX_SECRET_LEVELS_PER_MISSION);
519 for (i=0;i<N_secret_levels && mfgets(buf,80,mfile);i++) {
523 if ((t=strchr(buf,','))!=NULL) *t++=0;
528 if (strlen(buf) <= 12) {
529 strcpy(Secret_level_names[i],buf);
530 Secret_level_table[i] = atoi(t);
531 if (Secret_level_table[i]<1 || Secret_level_table[i]>Last_level)
546 if (Last_level <= 0) {
547 Current_mission_num = -1; //no valid mission loaded
551 Current_mission_filename = Mission_list[Current_mission_num].filename;
552 Current_mission_longname = Mission_list[Current_mission_num].mission_name;
554 if (enhanced_mission) {
556 extern void bm_read_extra_robots();
557 sprintf(t,"%s.ham",Current_mission_filename);
558 bm_read_extra_robots(t,enhanced_mission);
559 strncpy(t,Current_mission_filename,6);
561 init_extra_robot_movie(t);
567 //loads the named mission if exists.
568 //Returns true if mission loaded ok, else false.
569 int load_mission_by_name(char *mission_name)
573 n = build_mission_list(1);
576 if (!stricmp(mission_name,Mission_list[i].filename))
577 return load_mission(i);
579 return 0; //couldn't find mission