1 /* $Id: mission.c,v 1.6 2002-08-07 07:34:09 btb 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 * Stuff for loading missions
45 mle Mission_list[MAX_MISSIONS];
47 int Current_mission_num;
48 int N_secret_levels; // Made a global by MK for scoring purposes. August 1, 1995.
49 char *Current_mission_filename,*Current_mission_longname;
51 //this stuff should get defined elsewhere
53 char Level_names[MAX_LEVELS_PER_MISSION][FILENAME_LEN];
54 char Secret_level_names[MAX_SECRET_LEVELS_PER_MISSION][FILENAME_LEN];
56 //where the missions go
58 #define MISSION_DIR "missions/"
60 #define MISSION_DIR "./"
66 // Special versions of mission routines for shareware
69 #define SHAREWARE_MISSION_FILENAME "d2demo"
70 #define SHAREWARE_MISSION_NAME "Descent 2 Demo"
72 int build_mission_list(int anarchy_mode)
74 anarchy_mode++; //kill warning
76 strcpy(Mission_list[0].filename,SHAREWARE_MISSION_FILENAME);
77 strcpy(Mission_list[0].mission_name,SHAREWARE_MISSION_NAME);
78 Mission_list[0].anarchy_only_flag = 0;
80 return load_mission(0);
83 int load_mission(int mission_num)
85 Assert(mission_num == 0);
87 Current_mission_num = 0;
88 Current_mission_filename = Mission_list[0].filename;
89 Current_mission_longname = Mission_list[0].mission_name;
93 Assert(Last_level == 3);
95 #ifdef MACINTOSH // mac demo is using the regular hog and rl2 files
96 strcpy(Level_names[0],"d2leva-1.rl2");
97 strcpy(Level_names[1],"d2leva-2.rl2");
98 strcpy(Level_names[2],"d2leva-3.rl2");
100 strcpy(Level_names[0],"d2leva-1.sl2");
101 strcpy(Level_names[1],"d2leva-2.sl2");
102 strcpy(Level_names[2],"d2leva-3.sl2");
103 #endif // end of ifdef macintosh
108 int load_mission_by_name(char *mission_name)
110 if (strcmp(mission_name,SHAREWARE_MISSION_FILENAME))
111 return 0; //cannot load requested mission
113 return load_mission(0);
124 // Special versions of mission routines for Diamond/S3 version
127 #define OEM_MISSION_FILENAME "d2"
128 #define OEM_MISSION_NAME "D2 Destination:Quartzon"
130 int build_mission_list(int anarchy_mode)
132 anarchy_mode++; //kill warning
134 strcpy(Mission_list[0].filename,OEM_MISSION_FILENAME);
135 strcpy(Mission_list[0].mission_name,OEM_MISSION_NAME);
136 Mission_list[0].anarchy_only_flag = 0;
138 return load_mission(0);
141 int load_mission(int mission_num)
143 Assert(mission_num == 0);
145 Current_mission_num = 0;
146 Current_mission_filename = Mission_list[0].filename;
147 Current_mission_longname = Mission_list[0].mission_name;
151 Assert(Last_level == 8);
153 strcpy(Level_names[0],"d2leva-1.rl2");
154 strcpy(Level_names[1],"d2leva-2.rl2");
155 strcpy(Level_names[2],"d2leva-3.rl2");
156 strcpy(Level_names[3],"d2leva-4.rl2");
158 strcpy(Secret_level_names[0],"d2leva-s.rl2");
160 strcpy(Level_names[4],"d2levb-1.rl2");
161 strcpy(Level_names[5],"d2levb-2.rl2");
162 strcpy(Level_names[6],"d2levb-3.rl2");
163 strcpy(Level_names[7],"d2levb-4.rl2");
165 strcpy(Secret_level_names[1],"d2levb-s.rl2");
167 Secret_level_table[0] = 1;
168 Secret_level_table[1] = 5;
173 int load_mission_by_name(char *mission_name)
175 if (strcmp(mission_name,OEM_MISSION_FILENAME))
176 return 0; //cannot load requested mission
178 return load_mission(0);
184 //strips damn newline from end of line
185 char *mfgets(char *s,int n,CFILE *f)
190 if (r && s[strlen(s)-1] == '\n')
196 //compare a string for a token. returns true if match
197 int istok(char *buf,char *tok)
199 return strnicmp(buf,tok,strlen(tok)) == 0;
203 //adds a terminating 0 after a string at the first white space
204 void add_term(char *s)
206 while (*s && !isspace(*s)) s++;
211 //returns ptr to string after '=' & white space, or NULL if no '='
212 //adds 0 after parm at first white space
213 char *get_value(char *buf)
217 t = strchr(buf,'=')+1;
220 while (*t && isspace(*t)) t++;
226 return NULL; //error!
229 //reads a line, returns ptr to value of passed parm. returns NULL if none
230 char *get_parm_value(char *parm,CFILE *f)
234 if (!mfgets(buf,80,f))
238 return get_value(buf);
243 int ml_sort_func(mle *e0,mle *e1)
245 return stricmp(e0->mission_name,e1->mission_name);
249 extern char CDROM_dir[];
250 extern int HoardEquipped();
252 #define BUILTIN_MISSION (cfexist("d2.mn2")?"d2.mn2":"d2demo.mn2")
254 //returns 1 if file read ok, else 0
255 int read_mission_file(char *filename,int count,int location)
262 strcpy(filename2,MISSION_DIR);
266 songs_stop_redbook(); //so we can read from the CD
267 strcpy(filename2,CDROM_dir);
271 Int3(); //fall through
274 strcpy(filename2,"");
277 strcat(filename2,filename);
279 mfile = cfopen(filename2,"rb");
283 char temp[FILENAME_LEN],*t;
285 strcpy(temp,filename);
286 if ((t = strchr(temp,'.')) == NULL)
287 return 0; //missing extension
288 // look if it's .mn2 or .msn
289 Mission_list[count].descent_version = (t[3] == '2') ? 2 : 1;
290 *t = 0; //kill extension
292 strncpy( Mission_list[count].filename, temp, 9 );
293 Mission_list[count].anarchy_only_flag = 0;
294 Mission_list[count].location = location;
296 p = get_parm_value("name",mfile);
298 if (!p) { //try enhanced mission
299 cfseek(mfile,0,SEEK_SET);
300 p = get_parm_value("xname",mfile);
306 if (!p) { //try super-enhanced mission!
307 cfseek(mfile,0,SEEK_SET);
308 p = get_parm_value("zname",mfile);
315 if ((t=strchr(p,';'))!=NULL)
318 while (isspace(*t)) t--;
319 strncpy(Mission_list[count].mission_name,p,MISSION_NAME_LEN);
326 p = get_parm_value("type",mfile);
330 Mission_list[count].anarchy_only_flag = istok(p,"anarchy");
341 void add_missions_to_list(char * search_name, int * count, int anarchy_mode) {
343 if( !FileFindFirst( search_name, &find ) ) {
345 if (read_mission_file( find.name, *count, ML_MISSIONDIR )) {
347 if (anarchy_mode || !Mission_list[*count].anarchy_only_flag)
351 } while( !FileFindNext( &find ) && *count < MAX_MISSIONS);
353 if (*count >= MAX_MISSIONS)
354 mprintf((0, "Warning: more missions than d2x can handle\n"));
359 //fills in the global list of missions. Returns the number of missions
360 //in the list. If anarchy_mode set, don't include non-anarchy levels.
361 //if there is only one mission, this function will call load_mission on it.
363 extern char CDROM_dir[];
364 extern char AltHogDir[];
365 extern char AltHogdir_initialized;
367 int build_mission_list(int anarchy_mode)
369 static int num_missions=-1;
370 int count=0,special_count=0;
372 //now search for levels on disk
374 //@@Took out this code because after this routine was called once for
375 //@@a list of single-player missions, a subsequent call for a list of
376 //@@anarchy missions would not scan again, and thus would not find the
377 //@@anarchy-only missions. If we retain the minimum level of install,
378 //@@we may want to put the code back in, having it always scan for all
379 //@@missions, and have the code that uses it sort out the ones it wants.
380 //@@ if (num_missions != -1) {
381 //@@ if (Current_mission_num != 0)
382 //@@ load_mission(0); //set built-in mission as default
383 //@@ return num_missions;
386 if (!read_mission_file(BUILTIN_MISSION,0,ML_CURDIR)) //read built-in first
387 Error("Could not find required mission file <%s>",BUILTIN_MISSION);
389 special_count = count=1;
391 add_missions_to_list(MISSION_DIR "*.mn2", &count, anarchy_mode);
392 add_missions_to_list(MISSION_DIR "*.msn", &count, anarchy_mode);
394 if (AltHogdir_initialized) {
395 char search_name[PATH_MAX + 5];
396 strcpy(search_name, AltHogDir);
397 strcat(search_name, "/" MISSION_DIR "*.mn2");
398 add_missions_to_list(search_name, &count, anarchy_mode);
399 strcpy(search_name, AltHogDir);
400 strcat(search_name, "/" MISSION_DIR "*.msn");
401 add_missions_to_list(search_name, &count, anarchy_mode);
404 //move vertigo to top of mission list
408 for (i=special_count;i<count;i++)
409 if (!stricmp(Mission_list[i].filename,"D2X")) { //swap!
412 temp = Mission_list[special_count];
413 Mission_list[special_count] = Mission_list[i];
414 Mission_list[i] = temp;
423 if (count>special_count)
424 qsort(&Mission_list[special_count],count-special_count,sizeof(*Mission_list),
425 (int (*)( const void *, const void * ))ml_sort_func);
427 load_mission(0); //set built-in mission as default
429 num_missions = count;
434 void init_extra_robot_movie(char *filename);
436 //values for built-in mission
438 //loads the specfied mission from the mission list. build_mission_list()
439 //must have been called. If build_mission_list() returns 0, this function
440 //does not need to be called. Returns true if mission loaded ok, else false.
441 int load_mission(int mission_num)
446 int enhanced_mission = 0;
448 Current_mission_num = mission_num;
450 mprintf(( 0, "Loading mission %d\n", mission_num ));
452 //read mission from file
454 switch (Mission_list[mission_num].location) {
456 strcpy(buf,MISSION_DIR);
459 strcpy(buf,CDROM_dir);
462 Int3(); //fall through
467 strcat(buf,Mission_list[mission_num].filename);
468 if (Mission_list[mission_num].descent_version == 2)
473 mfile = cfopen(buf,"rb");
475 Current_mission_num = -1;
479 if (mission_num != 0) { //for non-builtin missions, load HOG
481 strcpy(buf+strlen(buf)-4,".hog"); //change extension
483 found_hogfile = cfile_use_alternate_hogfile(buf);
485 #ifdef RELEASE //for release, require mission to be in hogfile
486 if (! found_hogfile) {
488 Current_mission_num = -1;
496 Last_secret_level = 0;
498 while (mfgets(buf,80,mfile)) {
500 if (istok(buf,"name"))
501 continue; //already have name, go to next line
502 if (istok(buf,"xname")) {
503 enhanced_mission = 1;
504 continue; //already have name, go to next line
506 if (istok(buf,"zname")) {
507 enhanced_mission = 2;
508 continue; //already have name, go to next line
510 else if (istok(buf,"type"))
511 continue; //already have name, go to next line
512 else if (istok(buf,"hog")) {
515 while (*(bufp++) != '=')
519 while (*(++bufp) == ' ')
522 cfile_use_alternate_hogfile(bufp);
523 mprintf((0, "Hog file override = [%s]\n", bufp));
525 else if (istok(buf,"num_levels")) {
527 if ((v=get_value(buf))!=NULL) {
532 for (i=0;i<n_levels && mfgets(buf,80,mfile);i++) {
535 if (strlen(buf) <= 12) {
536 strcpy(Level_names[i],buf);
545 else if (istok(buf,"num_secrets")) {
546 if ((v=get_value(buf))!=NULL) {
549 N_secret_levels = atoi(v);
551 Assert(N_secret_levels <= MAX_SECRET_LEVELS_PER_MISSION);
553 for (i=0;i<N_secret_levels && mfgets(buf,80,mfile);i++) {
557 if ((t=strchr(buf,','))!=NULL) *t++=0;
562 if (strlen(buf) <= 12) {
563 strcpy(Secret_level_names[i],buf);
564 Secret_level_table[i] = atoi(t);
565 if (Secret_level_table[i]<1 || Secret_level_table[i]>Last_level)
580 if (Last_level <= 0) {
581 Current_mission_num = -1; //no valid mission loaded
585 Current_mission_filename = Mission_list[Current_mission_num].filename;
586 Current_mission_longname = Mission_list[Current_mission_num].mission_name;
588 if (enhanced_mission) {
590 extern void bm_read_extra_robots();
591 sprintf(t,"%s.ham",Current_mission_filename);
592 bm_read_extra_robots(t,enhanced_mission);
593 strncpy(t,Current_mission_filename,6);
595 init_extra_robot_movie(t);
601 //loads the named mission if exists.
602 //Returns true if mission loaded ok, else false.
603 int load_mission_by_name(char *mission_name)
607 n = build_mission_list(1);
610 if (!stricmp(mission_name,Mission_list[i].filename))
611 return load_mission(i);
613 return 0; //couldn't find mission