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.2 2001-01-31 15:17:54 bradleyb Exp $";
40 mle Mission_list[MAX_MISSIONS];
42 int Current_mission_num;
43 int N_secret_levels; // Made a global by MK for scoring purposes. August 1, 1995.
44 char *Current_mission_filename,*Current_mission_longname;
46 //this stuff should get defined elsewhere
48 char Level_names[MAX_LEVELS_PER_MISSION][FILENAME_LEN];
49 char Secret_level_names[MAX_SECRET_LEVELS_PER_MISSION][FILENAME_LEN];
51 //where the missions go
53 #define MISSION_DIR "missions/"
55 #define MISSION_DIR "./"
61 // Special versions of mission routines for shareware
64 #define SHAREWARE_MISSION_FILENAME "d2demo"
65 #define SHAREWARE_MISSION_NAME "Descent 2 Demo"
67 int build_mission_list(int anarchy_mode)
69 anarchy_mode++; //kill warning
71 strcpy(Mission_list[0].filename,SHAREWARE_MISSION_FILENAME);
72 strcpy(Mission_list[0].mission_name,SHAREWARE_MISSION_NAME);
73 Mission_list[0].anarchy_only_flag = 0;
75 return load_mission(0);
78 int load_mission(int mission_num)
80 Assert(mission_num == 0);
82 Current_mission_num = 0;
83 Current_mission_filename = Mission_list[0].filename;
84 Current_mission_longname = Mission_list[0].mission_name;
88 Assert(Last_level == 3);
90 #ifdef MACINTOSH // mac demo is using the regular hog and rl2 files
91 strcpy(Level_names[0],"d2leva-1.rl2");
92 strcpy(Level_names[1],"d2leva-2.rl2");
93 strcpy(Level_names[2],"d2leva-3.rl2");
95 strcpy(Level_names[0],"d2leva-1.sl2");
96 strcpy(Level_names[1],"d2leva-2.sl2");
97 strcpy(Level_names[2],"d2leva-3.sl2");
98 #endif // end of ifdef macintosh
103 int load_mission_by_name(char *mission_name)
105 if (strcmp(mission_name,SHAREWARE_MISSION_FILENAME))
106 return 0; //cannot load requested mission
108 return load_mission(0);
119 // Special versions of mission routines for Diamond/S3 version
122 #define OEM_MISSION_FILENAME "d2"
123 #define OEM_MISSION_NAME "D2 Destination:Quartzon"
125 int build_mission_list(int anarchy_mode)
127 anarchy_mode++; //kill warning
129 strcpy(Mission_list[0].filename,OEM_MISSION_FILENAME);
130 strcpy(Mission_list[0].mission_name,OEM_MISSION_NAME);
131 Mission_list[0].anarchy_only_flag = 0;
133 return load_mission(0);
136 int load_mission(int mission_num)
138 Assert(mission_num == 0);
140 Current_mission_num = 0;
141 Current_mission_filename = Mission_list[0].filename;
142 Current_mission_longname = Mission_list[0].mission_name;
146 Assert(Last_level == 8);
148 strcpy(Level_names[0],"d2leva-1.rl2");
149 strcpy(Level_names[1],"d2leva-2.rl2");
150 strcpy(Level_names[2],"d2leva-3.rl2");
151 strcpy(Level_names[3],"d2leva-4.rl2");
153 strcpy(Secret_level_names[0],"d2leva-s.rl2");
155 strcpy(Level_names[4],"d2levb-1.rl2");
156 strcpy(Level_names[5],"d2levb-2.rl2");
157 strcpy(Level_names[6],"d2levb-3.rl2");
158 strcpy(Level_names[7],"d2levb-4.rl2");
160 strcpy(Secret_level_names[1],"d2levb-s.rl2");
162 Secret_level_table[0] = 1;
163 Secret_level_table[1] = 5;
168 int load_mission_by_name(char *mission_name)
170 if (strcmp(mission_name,OEM_MISSION_FILENAME))
171 return 0; //cannot load requested mission
173 return load_mission(0);
179 //strips damn newline from end of line
180 char *mfgets(char *s,int n,CFILE *f)
185 if (r && s[strlen(s)-1] == '\n')
191 //compare a string for a token. returns true if match
192 int istok(char *buf,char *tok)
194 return strnicmp(buf,tok,strlen(tok)) == 0;
198 //adds a terminating 0 after a string at the first white space
199 void add_term(char *s)
201 while (*s && !isspace(*s)) s++;
206 //returns ptr to string after '=' & white space, or NULL if no '='
207 //adds 0 after parm at first white space
208 char *get_value(char *buf)
212 t = strchr(buf,'=')+1;
215 while (*t && isspace(*t)) t++;
221 return NULL; //error!
224 //reads a line, returns ptr to value of passed parm. returns NULL if none
225 char *get_parm_value(char *parm,CFILE *f)
229 if (!mfgets(buf,80,f))
233 return get_value(buf);
238 int ml_sort_func(mle *e0,mle *e1)
240 return stricmp(e0->mission_name,e1->mission_name);
244 extern char CDROM_dir[];
245 extern int HoardEquipped();
247 #define BUILTIN_MISSION "d2.mn2"
249 //returns 1 if file read ok, else 0
250 int read_mission_file(char *filename,int count,int location)
257 strcpy(filename2,MISSION_DIR);
261 songs_stop_redbook(); //so we can read from the CD
262 strcpy(filename2,CDROM_dir);
266 Int3(); //fall through
269 strcpy(filename2,"");
272 strcat(filename2,filename);
274 mfile = cfopen(filename2,"rb");
278 char temp[FILENAME_LEN],*t;
280 strcpy(temp,filename);
281 if ((t = strchr(temp,'.')) == NULL)
282 return 0; //missing extension
283 *t = 0; //kill extension
285 strncpy( Mission_list[count].filename, temp, 9 );
286 Mission_list[count].anarchy_only_flag = 0;
287 Mission_list[count].location = location;
289 p = get_parm_value("name",mfile);
291 if (!p) { //try enhanced mission
292 cfseek(mfile,0,SEEK_SET);
293 p = get_parm_value("xname",mfile);
299 if (!p) { //try super-enhanced mission!
300 cfseek(mfile,0,SEEK_SET);
301 p = get_parm_value("zname",mfile);
308 if ((t=strchr(p,';'))!=NULL)
311 while (isspace(*t)) t--;
312 strncpy(Mission_list[count].mission_name,p,MISSION_NAME_LEN);
319 p = get_parm_value("type",mfile);
323 Mission_list[count].anarchy_only_flag = istok(p,"anarchy");
334 //fills in the global list of missions. Returns the number of missions
335 //in the list. If anarchy_mode set, don't include non-anarchy levels.
336 //if there is only one mission, this function will call load_mission on it.
338 extern char CDROM_dir[];
340 int build_mission_list(int anarchy_mode)
342 static int num_missions=-1;
343 int count=0,special_count=0;
345 char search_name[100] = MISSION_DIR "*.mn2";
347 //now search for levels on disk
349 //@@Took out this code because after this routine was called once for
350 //@@a list of single-player missions, a subsequent call for a list of
351 //@@anarchy missions would not scan again, and thus would not find the
352 //@@anarchy-only missions. If we retain the minimum level of install,
353 //@@we may want to put the code back in, having it always scan for all
354 //@@missions, and have the code that uses it sort out the ones it wants.
355 //@@ if (num_missions != -1) {
356 //@@ if (Current_mission_num != 0)
357 //@@ load_mission(0); //set built-in mission as default
358 //@@ return num_missions;
361 if (!read_mission_file(BUILTIN_MISSION,0,ML_CURDIR)) //read built-in first
362 Error("Could not find required mission file <%s>",BUILTIN_MISSION);
364 special_count = count=1;
366 if( !FileFindFirst( search_name, &find ) ) {
368 if (stricmp(find.name,BUILTIN_MISSION)==0)
369 continue; //skip the built-in
371 if (read_mission_file(find.name,count,ML_MISSIONDIR)) {
373 if (anarchy_mode || !Mission_list[count].anarchy_only_flag)
377 } while( !FileFindNext( &find ) && count<MAX_MISSIONS);
381 //move vertigo to top of mission list
385 for (i=special_count;i<count;i++)
386 if (!stricmp(Mission_list[i].filename,"D2X")) { //swap!
389 temp = Mission_list[special_count];
390 Mission_list[special_count] = Mission_list[i];
391 Mission_list[i] = temp;
400 if (count>special_count)
401 qsort(&Mission_list[special_count],count-special_count,sizeof(*Mission_list),
402 (int (*)( const void *, const void * ))ml_sort_func);
404 load_mission(0); //set built-in mission as default
406 num_missions = count;
411 void init_extra_robot_movie(char *filename);
413 //values for built-in mission
415 //loads the specfied mission from the mission list. build_mission_list()
416 //must have been called. If build_mission_list() returns 0, this function
417 //does not need to be called. Returns true if mission loaded ok, else false.
418 int load_mission(int mission_num)
423 int enhanced_mission = 0;
425 Current_mission_num = mission_num;
427 mprintf(( 0, "Loading mission %d\n", mission_num ));
429 //read mission from file
431 switch (Mission_list[mission_num].location) {
432 case ML_MISSIONDIR: strcpy(buf,MISSION_DIR); break;
433 case ML_CDROM: strcpy(buf,CDROM_dir); break;
434 default: Int3(); //fall through
435 case ML_CURDIR: strcpy(buf,""); break;
437 strcat(buf,Mission_list[mission_num].filename);
440 mfile = cfopen(buf,"rb");
442 Current_mission_num = -1;
446 if (mission_num != 0) { //for non-builtin missions, load HOG
448 strcpy(buf+strlen(buf)-4,".hog"); //change extension
450 found_hogfile = cfile_use_alternate_hogfile(buf);
452 #ifdef RELEASE //for release, require mission to be in hogfile
453 if (! found_hogfile) {
455 Current_mission_num = -1;
463 Last_secret_level = 0;
465 while (mfgets(buf,80,mfile)) {
467 if (istok(buf,"name"))
468 continue; //already have name, go to next line
469 if (istok(buf,"xname")) {
470 enhanced_mission = 1;
471 continue; //already have name, go to next line
473 if (istok(buf,"zname")) {
474 enhanced_mission = 2;
475 continue; //already have name, go to next line
477 else if (istok(buf,"type"))
478 continue; //already have name, go to next line
479 else if (istok(buf,"hog")) {
482 while (*(bufp++) != '=')
486 while (*(++bufp) == ' ')
489 cfile_use_alternate_hogfile(bufp);
490 mprintf((0, "Hog file override = [%s]\n", bufp));
492 else if (istok(buf,"num_levels")) {
494 if ((v=get_value(buf))!=NULL) {
499 for (i=0;i<n_levels && mfgets(buf,80,mfile);i++) {
502 if (strlen(buf) <= 12) {
503 strcpy(Level_names[i],buf);
512 else if (istok(buf,"num_secrets")) {
513 if ((v=get_value(buf))!=NULL) {
516 N_secret_levels = atoi(v);
518 Assert(N_secret_levels <= MAX_SECRET_LEVELS_PER_MISSION);
520 for (i=0;i<N_secret_levels && mfgets(buf,80,mfile);i++) {
524 if ((t=strchr(buf,','))!=NULL) *t++=0;
529 if (strlen(buf) <= 12) {
530 strcpy(Secret_level_names[i],buf);
531 Secret_level_table[i] = atoi(t);
532 if (Secret_level_table[i]<1 || Secret_level_table[i]>Last_level)
547 if (Last_level <= 0) {
548 Current_mission_num = -1; //no valid mission loaded
552 Current_mission_filename = Mission_list[Current_mission_num].filename;
553 Current_mission_longname = Mission_list[Current_mission_num].mission_name;
555 if (enhanced_mission) {
557 extern void bm_read_extra_robots();
558 sprintf(t,"%s.ham",Current_mission_filename);
559 bm_read_extra_robots(t,enhanced_mission);
560 strncpy(t,Current_mission_filename,6);
562 init_extra_robot_movie(t);
568 //loads the named mission if exists.
569 //Returns true if mission loaded ok, else false.
570 int load_mission_by_name(char *mission_name)
574 n = build_mission_list(1);
577 if (!stricmp(mission_name,Mission_list[i].filename))
578 return load_mission(i);
580 return 0; //couldn't find mission