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.
19 static char rcsid[] = "$Id: mission.c,v 1.3 2002-01-29 10:11:31 bradleyb Exp $";
41 mle Mission_list[MAX_MISSIONS];
43 int Current_mission_num;
44 int N_secret_levels; // Made a global by MK for scoring purposes. August 1, 1995.
45 char *Current_mission_filename,*Current_mission_longname;
47 //this stuff should get defined elsewhere
49 char Level_names[MAX_LEVELS_PER_MISSION][FILENAME_LEN];
50 char Secret_level_names[MAX_SECRET_LEVELS_PER_MISSION][FILENAME_LEN];
52 //where the missions go
54 #define MISSION_DIR "missions/"
56 #define MISSION_DIR "./"
62 // Special versions of mission routines for shareware
65 #define SHAREWARE_MISSION_FILENAME "d2demo"
66 #define SHAREWARE_MISSION_NAME "Descent 2 Demo"
68 int build_mission_list(int anarchy_mode)
70 anarchy_mode++; //kill warning
72 strcpy(Mission_list[0].filename,SHAREWARE_MISSION_FILENAME);
73 strcpy(Mission_list[0].mission_name,SHAREWARE_MISSION_NAME);
74 Mission_list[0].anarchy_only_flag = 0;
76 return load_mission(0);
79 int load_mission(int mission_num)
81 Assert(mission_num == 0);
83 Current_mission_num = 0;
84 Current_mission_filename = Mission_list[0].filename;
85 Current_mission_longname = Mission_list[0].mission_name;
89 Assert(Last_level == 3);
91 #ifdef MACINTOSH // mac demo is using the regular hog and rl2 files
92 strcpy(Level_names[0],"d2leva-1.rl2");
93 strcpy(Level_names[1],"d2leva-2.rl2");
94 strcpy(Level_names[2],"d2leva-3.rl2");
96 strcpy(Level_names[0],"d2leva-1.sl2");
97 strcpy(Level_names[1],"d2leva-2.sl2");
98 strcpy(Level_names[2],"d2leva-3.sl2");
99 #endif // end of ifdef macintosh
104 int load_mission_by_name(char *mission_name)
106 if (strcmp(mission_name,SHAREWARE_MISSION_FILENAME))
107 return 0; //cannot load requested mission
109 return load_mission(0);
120 // Special versions of mission routines for Diamond/S3 version
123 #define OEM_MISSION_FILENAME "d2"
124 #define OEM_MISSION_NAME "D2 Destination:Quartzon"
126 int build_mission_list(int anarchy_mode)
128 anarchy_mode++; //kill warning
130 strcpy(Mission_list[0].filename,OEM_MISSION_FILENAME);
131 strcpy(Mission_list[0].mission_name,OEM_MISSION_NAME);
132 Mission_list[0].anarchy_only_flag = 0;
134 return load_mission(0);
137 int load_mission(int mission_num)
139 Assert(mission_num == 0);
141 Current_mission_num = 0;
142 Current_mission_filename = Mission_list[0].filename;
143 Current_mission_longname = Mission_list[0].mission_name;
147 Assert(Last_level == 8);
149 strcpy(Level_names[0],"d2leva-1.rl2");
150 strcpy(Level_names[1],"d2leva-2.rl2");
151 strcpy(Level_names[2],"d2leva-3.rl2");
152 strcpy(Level_names[3],"d2leva-4.rl2");
154 strcpy(Secret_level_names[0],"d2leva-s.rl2");
156 strcpy(Level_names[4],"d2levb-1.rl2");
157 strcpy(Level_names[5],"d2levb-2.rl2");
158 strcpy(Level_names[6],"d2levb-3.rl2");
159 strcpy(Level_names[7],"d2levb-4.rl2");
161 strcpy(Secret_level_names[1],"d2levb-s.rl2");
163 Secret_level_table[0] = 1;
164 Secret_level_table[1] = 5;
169 int load_mission_by_name(char *mission_name)
171 if (strcmp(mission_name,OEM_MISSION_FILENAME))
172 return 0; //cannot load requested mission
174 return load_mission(0);
180 //strips damn newline from end of line
181 char *mfgets(char *s,int n,CFILE *f)
186 if (r && s[strlen(s)-1] == '\n')
192 //compare a string for a token. returns true if match
193 int istok(char *buf,char *tok)
195 return strnicmp(buf,tok,strlen(tok)) == 0;
199 //adds a terminating 0 after a string at the first white space
200 void add_term(char *s)
202 while (*s && !isspace(*s)) s++;
207 //returns ptr to string after '=' & white space, or NULL if no '='
208 //adds 0 after parm at first white space
209 char *get_value(char *buf)
213 t = strchr(buf,'=')+1;
216 while (*t && isspace(*t)) t++;
222 return NULL; //error!
225 //reads a line, returns ptr to value of passed parm. returns NULL if none
226 char *get_parm_value(char *parm,CFILE *f)
230 if (!mfgets(buf,80,f))
234 return get_value(buf);
239 int ml_sort_func(mle *e0,mle *e1)
241 return stricmp(e0->mission_name,e1->mission_name);
245 extern char CDROM_dir[];
246 extern int HoardEquipped();
248 #define BUILTIN_MISSION "d2.mn2"
250 //returns 1 if file read ok, else 0
251 int read_mission_file(char *filename,int count,int location)
258 strcpy(filename2,MISSION_DIR);
262 songs_stop_redbook(); //so we can read from the CD
263 strcpy(filename2,CDROM_dir);
267 Int3(); //fall through
270 strcpy(filename2,"");
273 strcat(filename2,filename);
275 mfile = cfopen(filename2,"rb");
279 char temp[FILENAME_LEN],*t;
281 strcpy(temp,filename);
282 if ((t = strchr(temp,'.')) == NULL)
283 return 0; //missing extension
284 *t = 0; //kill extension
286 strncpy( Mission_list[count].filename, temp, 9 );
287 Mission_list[count].anarchy_only_flag = 0;
288 Mission_list[count].location = location;
290 p = get_parm_value("name",mfile);
292 if (!p) { //try enhanced mission
293 cfseek(mfile,0,SEEK_SET);
294 p = get_parm_value("xname",mfile);
300 if (!p) { //try super-enhanced mission!
301 cfseek(mfile,0,SEEK_SET);
302 p = get_parm_value("zname",mfile);
309 if ((t=strchr(p,';'))!=NULL)
312 while (isspace(*t)) t--;
313 strncpy(Mission_list[count].mission_name,p,MISSION_NAME_LEN);
320 p = get_parm_value("type",mfile);
324 Mission_list[count].anarchy_only_flag = istok(p,"anarchy");
335 //fills in the global list of missions. Returns the number of missions
336 //in the list. If anarchy_mode set, don't include non-anarchy levels.
337 //if there is only one mission, this function will call load_mission on it.
339 extern char CDROM_dir[];
340 extern char AltHogDir[];
341 extern char AltHogdir_initialized;
343 int build_mission_list(int anarchy_mode)
345 static int num_missions=-1;
346 int count=0,special_count=0;
348 char search_name[PATH_MAX + 5] = MISSION_DIR "*.mn2";
350 //now search for levels on disk
352 //@@Took out this code because after this routine was called once for
353 //@@a list of single-player missions, a subsequent call for a list of
354 //@@anarchy missions would not scan again, and thus would not find the
355 //@@anarchy-only missions. If we retain the minimum level of install,
356 //@@we may want to put the code back in, having it always scan for all
357 //@@missions, and have the code that uses it sort out the ones it wants.
358 //@@ if (num_missions != -1) {
359 //@@ if (Current_mission_num != 0)
360 //@@ load_mission(0); //set built-in mission as default
361 //@@ return num_missions;
364 if (!read_mission_file(BUILTIN_MISSION,0,ML_CURDIR)) //read built-in first
365 Error("Could not find required mission file <%s>",BUILTIN_MISSION);
367 special_count = count=1;
369 if( !FileFindFirst( search_name, &find ) ) {
371 if (stricmp(find.name,BUILTIN_MISSION)==0)
372 continue; //skip the built-in
374 if (read_mission_file(find.name,count,ML_MISSIONDIR)) {
376 if (anarchy_mode || !Mission_list[count].anarchy_only_flag)
380 } while( !FileFindNext( &find ) && count<MAX_MISSIONS);
384 if (AltHogdir_initialized) {
385 strcpy(search_name, AltHogDir);
386 strcat(search_name, "/" MISSION_DIR "*.mn2");
387 if( !FileFindFirst( search_name, &find ) ) {
389 if (stricmp(find.name,BUILTIN_MISSION)==0)
390 continue; //skip the built-in
392 if (read_mission_file(find.name,count,ML_MISSIONDIR)) {
394 if (anarchy_mode || !Mission_list[count].anarchy_only_flag)
398 } while( !FileFindNext( &find ) && count<MAX_MISSIONS);
403 //move vertigo to top of mission list
407 for (i=special_count;i<count;i++)
408 if (!stricmp(Mission_list[i].filename,"D2X")) { //swap!
411 temp = Mission_list[special_count];
412 Mission_list[special_count] = Mission_list[i];
413 Mission_list[i] = temp;
422 if (count>special_count)
423 qsort(&Mission_list[special_count],count-special_count,sizeof(*Mission_list),
424 (int (*)( const void *, const void * ))ml_sort_func);
426 load_mission(0); //set built-in mission as default
428 num_missions = count;
433 void init_extra_robot_movie(char *filename);
435 //values for built-in mission
437 //loads the specfied mission from the mission list. build_mission_list()
438 //must have been called. If build_mission_list() returns 0, this function
439 //does not need to be called. Returns true if mission loaded ok, else false.
440 int load_mission(int mission_num)
445 int enhanced_mission = 0;
447 Current_mission_num = mission_num;
449 mprintf(( 0, "Loading mission %d\n", mission_num ));
451 //read mission from file
453 switch (Mission_list[mission_num].location) {
454 case ML_MISSIONDIR: strcpy(buf,MISSION_DIR); break;
455 case ML_CDROM: strcpy(buf,CDROM_dir); break;
456 default: Int3(); //fall through
457 case ML_CURDIR: strcpy(buf,""); break;
459 strcat(buf,Mission_list[mission_num].filename);
462 mfile = cfopen(buf,"rb");
464 Current_mission_num = -1;
468 if (mission_num != 0) { //for non-builtin missions, load HOG
470 strcpy(buf+strlen(buf)-4,".hog"); //change extension
472 found_hogfile = cfile_use_alternate_hogfile(buf);
474 #ifdef RELEASE //for release, require mission to be in hogfile
475 if (! found_hogfile) {
477 Current_mission_num = -1;
485 Last_secret_level = 0;
487 while (mfgets(buf,80,mfile)) {
489 if (istok(buf,"name"))
490 continue; //already have name, go to next line
491 if (istok(buf,"xname")) {
492 enhanced_mission = 1;
493 continue; //already have name, go to next line
495 if (istok(buf,"zname")) {
496 enhanced_mission = 2;
497 continue; //already have name, go to next line
499 else if (istok(buf,"type"))
500 continue; //already have name, go to next line
501 else if (istok(buf,"hog")) {
504 while (*(bufp++) != '=')
508 while (*(++bufp) == ' ')
511 cfile_use_alternate_hogfile(bufp);
512 mprintf((0, "Hog file override = [%s]\n", bufp));
514 else if (istok(buf,"num_levels")) {
516 if ((v=get_value(buf))!=NULL) {
521 for (i=0;i<n_levels && mfgets(buf,80,mfile);i++) {
524 if (strlen(buf) <= 12) {
525 strcpy(Level_names[i],buf);
534 else if (istok(buf,"num_secrets")) {
535 if ((v=get_value(buf))!=NULL) {
538 N_secret_levels = atoi(v);
540 Assert(N_secret_levels <= MAX_SECRET_LEVELS_PER_MISSION);
542 for (i=0;i<N_secret_levels && mfgets(buf,80,mfile);i++) {
546 if ((t=strchr(buf,','))!=NULL) *t++=0;
551 if (strlen(buf) <= 12) {
552 strcpy(Secret_level_names[i],buf);
553 Secret_level_table[i] = atoi(t);
554 if (Secret_level_table[i]<1 || Secret_level_table[i]>Last_level)
569 if (Last_level <= 0) {
570 Current_mission_num = -1; //no valid mission loaded
574 Current_mission_filename = Mission_list[Current_mission_num].filename;
575 Current_mission_longname = Mission_list[Current_mission_num].mission_name;
577 if (enhanced_mission) {
579 extern void bm_read_extra_robots();
580 sprintf(t,"%s.ham",Current_mission_filename);
581 bm_read_extra_robots(t,enhanced_mission);
582 strncpy(t,Current_mission_filename,6);
584 init_extra_robot_movie(t);
590 //loads the named mission if exists.
591 //Returns true if mission loaded ok, else false.
592 int load_mission_by_name(char *mission_name)
596 n = build_mission_list(1);
599 if (!stricmp(mission_name,Mission_list[i].filename))
600 return load_mission(i);
602 return 0; //couldn't find mission