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.
15 * $Source: /cvs/cvsroot/d2x/main/mission.c,v $
18 * $Date: 2002-01-29 10:14:25 $
20 * Stuff for loading missions
22 * $Log: not supported by cvs2svn $
49 mle Mission_list[MAX_MISSIONS];
51 int Current_mission_num;
52 int N_secret_levels; // Made a global by MK for scoring purposes. August 1, 1995.
53 char *Current_mission_filename,*Current_mission_longname;
55 //this stuff should get defined elsewhere
57 char Level_names[MAX_LEVELS_PER_MISSION][FILENAME_LEN];
58 char Secret_level_names[MAX_SECRET_LEVELS_PER_MISSION][FILENAME_LEN];
60 //where the missions go
62 #define MISSION_DIR "missions/"
64 #define MISSION_DIR "./"
70 // Special versions of mission routines for shareware
73 #define SHAREWARE_MISSION_FILENAME "d2demo"
74 #define SHAREWARE_MISSION_NAME "Descent 2 Demo"
76 int build_mission_list(int anarchy_mode)
78 anarchy_mode++; //kill warning
80 strcpy(Mission_list[0].filename,SHAREWARE_MISSION_FILENAME);
81 strcpy(Mission_list[0].mission_name,SHAREWARE_MISSION_NAME);
82 Mission_list[0].anarchy_only_flag = 0;
84 return load_mission(0);
87 int load_mission(int mission_num)
89 Assert(mission_num == 0);
91 Current_mission_num = 0;
92 Current_mission_filename = Mission_list[0].filename;
93 Current_mission_longname = Mission_list[0].mission_name;
97 Assert(Last_level == 3);
99 #ifdef MACINTOSH // mac demo is using the regular hog and rl2 files
100 strcpy(Level_names[0],"d2leva-1.rl2");
101 strcpy(Level_names[1],"d2leva-2.rl2");
102 strcpy(Level_names[2],"d2leva-3.rl2");
104 strcpy(Level_names[0],"d2leva-1.sl2");
105 strcpy(Level_names[1],"d2leva-2.sl2");
106 strcpy(Level_names[2],"d2leva-3.sl2");
107 #endif // end of ifdef macintosh
112 int load_mission_by_name(char *mission_name)
114 if (strcmp(mission_name,SHAREWARE_MISSION_FILENAME))
115 return 0; //cannot load requested mission
117 return load_mission(0);
128 // Special versions of mission routines for Diamond/S3 version
131 #define OEM_MISSION_FILENAME "d2"
132 #define OEM_MISSION_NAME "D2 Destination:Quartzon"
134 int build_mission_list(int anarchy_mode)
136 anarchy_mode++; //kill warning
138 strcpy(Mission_list[0].filename,OEM_MISSION_FILENAME);
139 strcpy(Mission_list[0].mission_name,OEM_MISSION_NAME);
140 Mission_list[0].anarchy_only_flag = 0;
142 return load_mission(0);
145 int load_mission(int mission_num)
147 Assert(mission_num == 0);
149 Current_mission_num = 0;
150 Current_mission_filename = Mission_list[0].filename;
151 Current_mission_longname = Mission_list[0].mission_name;
155 Assert(Last_level == 8);
157 strcpy(Level_names[0],"d2leva-1.rl2");
158 strcpy(Level_names[1],"d2leva-2.rl2");
159 strcpy(Level_names[2],"d2leva-3.rl2");
160 strcpy(Level_names[3],"d2leva-4.rl2");
162 strcpy(Secret_level_names[0],"d2leva-s.rl2");
164 strcpy(Level_names[4],"d2levb-1.rl2");
165 strcpy(Level_names[5],"d2levb-2.rl2");
166 strcpy(Level_names[6],"d2levb-3.rl2");
167 strcpy(Level_names[7],"d2levb-4.rl2");
169 strcpy(Secret_level_names[1],"d2levb-s.rl2");
171 Secret_level_table[0] = 1;
172 Secret_level_table[1] = 5;
177 int load_mission_by_name(char *mission_name)
179 if (strcmp(mission_name,OEM_MISSION_FILENAME))
180 return 0; //cannot load requested mission
182 return load_mission(0);
188 //strips damn newline from end of line
189 char *mfgets(char *s,int n,CFILE *f)
194 if (r && s[strlen(s)-1] == '\n')
200 //compare a string for a token. returns true if match
201 int istok(char *buf,char *tok)
203 return strnicmp(buf,tok,strlen(tok)) == 0;
207 //adds a terminating 0 after a string at the first white space
208 void add_term(char *s)
210 while (*s && !isspace(*s)) s++;
215 //returns ptr to string after '=' & white space, or NULL if no '='
216 //adds 0 after parm at first white space
217 char *get_value(char *buf)
221 t = strchr(buf,'=')+1;
224 while (*t && isspace(*t)) t++;
230 return NULL; //error!
233 //reads a line, returns ptr to value of passed parm. returns NULL if none
234 char *get_parm_value(char *parm,CFILE *f)
238 if (!mfgets(buf,80,f))
242 return get_value(buf);
247 int ml_sort_func(mle *e0,mle *e1)
249 return stricmp(e0->mission_name,e1->mission_name);
253 extern char CDROM_dir[];
254 extern int HoardEquipped();
256 #define BUILTIN_MISSION "d2.mn2"
258 //returns 1 if file read ok, else 0
259 int read_mission_file(char *filename,int count,int location)
266 strcpy(filename2,MISSION_DIR);
270 songs_stop_redbook(); //so we can read from the CD
271 strcpy(filename2,CDROM_dir);
275 Int3(); //fall through
278 strcpy(filename2,"");
281 strcat(filename2,filename);
283 mfile = cfopen(filename2,"rb");
287 char temp[FILENAME_LEN],*t;
289 strcpy(temp,filename);
290 if ((t = strchr(temp,'.')) == NULL)
291 return 0; //missing extension
292 *t = 0; //kill extension
294 strncpy( Mission_list[count].filename, temp, 9 );
295 Mission_list[count].anarchy_only_flag = 0;
296 Mission_list[count].location = location;
298 p = get_parm_value("name",mfile);
300 if (!p) { //try enhanced mission
301 cfseek(mfile,0,SEEK_SET);
302 p = get_parm_value("xname",mfile);
308 if (!p) { //try super-enhanced mission!
309 cfseek(mfile,0,SEEK_SET);
310 p = get_parm_value("zname",mfile);
317 if ((t=strchr(p,';'))!=NULL)
320 while (isspace(*t)) t--;
321 strncpy(Mission_list[count].mission_name,p,MISSION_NAME_LEN);
328 p = get_parm_value("type",mfile);
332 Mission_list[count].anarchy_only_flag = istok(p,"anarchy");
343 //fills in the global list of missions. Returns the number of missions
344 //in the list. If anarchy_mode set, don't include non-anarchy levels.
345 //if there is only one mission, this function will call load_mission on it.
347 extern char CDROM_dir[];
348 extern char AltHogDir[];
349 extern char AltHogdir_initialized;
351 int build_mission_list(int anarchy_mode)
353 static int num_missions=-1;
354 int count=0,special_count=0;
356 char search_name[PATH_MAX + 5] = MISSION_DIR "*.mn2";
358 //now search for levels on disk
360 //@@Took out this code because after this routine was called once for
361 //@@a list of single-player missions, a subsequent call for a list of
362 //@@anarchy missions would not scan again, and thus would not find the
363 //@@anarchy-only missions. If we retain the minimum level of install,
364 //@@we may want to put the code back in, having it always scan for all
365 //@@missions, and have the code that uses it sort out the ones it wants.
366 //@@ if (num_missions != -1) {
367 //@@ if (Current_mission_num != 0)
368 //@@ load_mission(0); //set built-in mission as default
369 //@@ return num_missions;
372 if (!read_mission_file(BUILTIN_MISSION,0,ML_CURDIR)) //read built-in first
373 Error("Could not find required mission file <%s>",BUILTIN_MISSION);
375 special_count = count=1;
377 if( !FileFindFirst( search_name, &find ) ) {
379 if (stricmp(find.name,BUILTIN_MISSION)==0)
380 continue; //skip the built-in
382 if (read_mission_file(find.name,count,ML_MISSIONDIR)) {
384 if (anarchy_mode || !Mission_list[count].anarchy_only_flag)
388 } while( !FileFindNext( &find ) && count<MAX_MISSIONS);
392 if (AltHogdir_initialized) {
393 strcpy(search_name, AltHogDir);
394 strcat(search_name, "/" MISSION_DIR "*.mn2");
395 if( !FileFindFirst( search_name, &find ) ) {
397 if (stricmp(find.name,BUILTIN_MISSION)==0)
398 continue; //skip the built-in
400 if (read_mission_file(find.name,count,ML_MISSIONDIR)) {
402 if (anarchy_mode || !Mission_list[count].anarchy_only_flag)
406 } while( !FileFindNext( &find ) && count<MAX_MISSIONS);
411 //move vertigo to top of mission list
415 for (i=special_count;i<count;i++)
416 if (!stricmp(Mission_list[i].filename,"D2X")) { //swap!
419 temp = Mission_list[special_count];
420 Mission_list[special_count] = Mission_list[i];
421 Mission_list[i] = temp;
430 if (count>special_count)
431 qsort(&Mission_list[special_count],count-special_count,sizeof(*Mission_list),
432 (int (*)( const void *, const void * ))ml_sort_func);
434 load_mission(0); //set built-in mission as default
436 num_missions = count;
441 void init_extra_robot_movie(char *filename);
443 //values for built-in mission
445 //loads the specfied mission from the mission list. build_mission_list()
446 //must have been called. If build_mission_list() returns 0, this function
447 //does not need to be called. Returns true if mission loaded ok, else false.
448 int load_mission(int mission_num)
453 int enhanced_mission = 0;
455 Current_mission_num = mission_num;
457 mprintf(( 0, "Loading mission %d\n", mission_num ));
459 //read mission from file
461 switch (Mission_list[mission_num].location) {
462 case ML_MISSIONDIR: strcpy(buf,MISSION_DIR); break;
463 case ML_CDROM: strcpy(buf,CDROM_dir); break;
464 default: Int3(); //fall through
465 case ML_CURDIR: strcpy(buf,""); break;
467 strcat(buf,Mission_list[mission_num].filename);
470 mfile = cfopen(buf,"rb");
472 Current_mission_num = -1;
476 if (mission_num != 0) { //for non-builtin missions, load HOG
478 strcpy(buf+strlen(buf)-4,".hog"); //change extension
480 found_hogfile = cfile_use_alternate_hogfile(buf);
482 #ifdef RELEASE //for release, require mission to be in hogfile
483 if (! found_hogfile) {
485 Current_mission_num = -1;
493 Last_secret_level = 0;
495 while (mfgets(buf,80,mfile)) {
497 if (istok(buf,"name"))
498 continue; //already have name, go to next line
499 if (istok(buf,"xname")) {
500 enhanced_mission = 1;
501 continue; //already have name, go to next line
503 if (istok(buf,"zname")) {
504 enhanced_mission = 2;
505 continue; //already have name, go to next line
507 else if (istok(buf,"type"))
508 continue; //already have name, go to next line
509 else if (istok(buf,"hog")) {
512 while (*(bufp++) != '=')
516 while (*(++bufp) == ' ')
519 cfile_use_alternate_hogfile(bufp);
520 mprintf((0, "Hog file override = [%s]\n", bufp));
522 else if (istok(buf,"num_levels")) {
524 if ((v=get_value(buf))!=NULL) {
529 for (i=0;i<n_levels && mfgets(buf,80,mfile);i++) {
532 if (strlen(buf) <= 12) {
533 strcpy(Level_names[i],buf);
542 else if (istok(buf,"num_secrets")) {
543 if ((v=get_value(buf))!=NULL) {
546 N_secret_levels = atoi(v);
548 Assert(N_secret_levels <= MAX_SECRET_LEVELS_PER_MISSION);
550 for (i=0;i<N_secret_levels && mfgets(buf,80,mfile);i++) {
554 if ((t=strchr(buf,','))!=NULL) *t++=0;
559 if (strlen(buf) <= 12) {
560 strcpy(Secret_level_names[i],buf);
561 Secret_level_table[i] = atoi(t);
562 if (Secret_level_table[i]<1 || Secret_level_table[i]>Last_level)
577 if (Last_level <= 0) {
578 Current_mission_num = -1; //no valid mission loaded
582 Current_mission_filename = Mission_list[Current_mission_num].filename;
583 Current_mission_longname = Mission_list[Current_mission_num].mission_name;
585 if (enhanced_mission) {
587 extern void bm_read_extra_robots();
588 sprintf(t,"%s.ham",Current_mission_filename);
589 bm_read_extra_robots(t,enhanced_mission);
590 strncpy(t,Current_mission_filename,6);
592 init_extra_robot_movie(t);
598 //loads the named mission if exists.
599 //Returns true if mission loaded ok, else false.
600 int load_mission_by_name(char *mission_name)
604 n = build_mission_list(1);
607 if (!stricmp(mission_name,Mission_list[i].filename))
608 return load_mission(i);
610 return 0; //couldn't find mission