1 /* $Id: mission.c,v 1.18 2003-01-11 02:58:33 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 * Code to handle multiple missions
20 * Revision 1.4 1995/10/31 10:21:40 allender
21 * no mission support in shareware
23 * Revision 1.3 1995/10/21 22:53:04 allender
24 * moved missions to data folder
26 * Revision 1.2 1995/09/13 08:47:29 allender
27 * made to work with Chris' direct stuff
29 * Revision 1.1 1995/05/16 15:27:48 allender
32 * Revision 2.9 1995/05/26 16:16:32 john
33 * Split SATURN into define's for requiring cd, using cd, etc.
34 * Also started adding all the Rockwell stuff.
36 * Revision 2.8 1995/03/20 15:49:31 mike
37 * Remove eof char from comment which confused make depend, causing
38 * no mission.obj: line in makefile. Pretty stupid tool, huh?
40 * Revision 2.7 1995/03/20 12:12:11 john
43 * Revision 2.6 1995/03/15 14:32:49 john
44 * Added code to force the Descent CD-rom in the drive.
46 * Revision 2.5 1995/03/15 11:41:15 john
47 * Better Saturn CD-ROM support.
49 * Revision 2.4 1995/03/14 18:24:14 john
50 * Force Destination Saturn to use CD-ROM drive.
52 * Revision 2.3 1995/03/07 14:19:41 mike
53 * More destination saturn stuff.
55 * Revision 2.2 1995/03/06 23:09:03 mike
56 * more saturn stuff: make briefings work for saturn mission.
58 * Revision 2.1 1995/03/06 16:47:48 mike
61 * Revision 2.0 1995/02/27 11:27:49 john
62 * New version 2.0, which has no anonymous unions, builds with
63 * Watcom 10.0, and doesn't require parsing BITMAPS.TBL.
65 * Revision 1.14 1995/02/15 11:30:37 john
66 * Fixed bug with potential mem overwrite on line 160.
68 * Revision 1.13 1995/02/10 17:53:20 matt
69 * Changed mission name again
71 * Revision 1.12 1995/02/10 17:35:38 matt
72 * Changed name of built-in mission
74 * Revision 1.11 1995/02/07 17:13:51 allender
75 * removed return statement in load_mission so that built in mission will
76 * actually set the Current_mission_name and _filename
78 * Revision 1.10 1995/01/31 01:19:45 matt
79 * Made build_mission_list() sort missions by name
81 * Revision 1.9 1995/01/30 13:49:58 allender
82 * changed build_mission_list in load_mission_by_name to include
85 * Revision 1.8 1995/01/30 13:03:51 matt
88 * Revision 1.7 1995/01/30 12:55:22 matt
89 * Added vars to point to mission names
91 * Revision 1.6 1995/01/22 18:57:28 matt
92 * Made player highest level work with missions
94 * Revision 1.5 1995/01/22 14:13:08 matt
95 * Added flag in mission list for anarchy-only missions
97 * Revision 1.4 1995/01/21 23:13:12 matt
98 * Made high scores with (not work, really) with loaded missions
99 * Don't give player high score when quit game
101 * Revision 1.3 1995/01/21 16:27:12 matt
102 * Made endlevel briefing work with missions
104 * Revision 1.2 1995/01/20 22:47:50 matt
105 * Mission system implemented, though imcompletely
107 * Revision 1.1 1995/01/20 13:42:19 matt
134 #include "findfile.h"
137 mle Mission_list[MAX_MISSIONS];
139 int Current_mission_num;
140 int N_secret_levels; // Made a global by MK for scoring purposes. August 1, 1995.
141 char *Current_mission_filename,*Current_mission_longname;
143 int Builtin_mission_num;
144 char Builtin_mission_filename[9];
145 int Builtin_mission_hogsize;
147 int D1_Builtin_mission_num;
148 char D1_Builtin_mission_filename[9];
149 int D1_Builtin_mission_hogsize;
151 //this stuff should get defined elsewhere
153 char Level_names[MAX_LEVELS_PER_MISSION][FILENAME_LEN];
154 char Secret_level_names[MAX_SECRET_LEVELS_PER_MISSION][FILENAME_LEN];
156 //where the missions go
158 #define MISSION_DIR "missions/"
160 #define MISSION_DIR "./"
163 //values for d1 built-in mission
164 #define BIM_LAST_LEVEL 27
165 #define BIM_LAST_SECRET_LEVEL -3
166 #define BIM_BRIEFING_FILE "briefing.tex"
167 #define BIM_ENDING_FILE "endreg.tex"
170 // Special versions of mission routines for d1 builtins
173 int load_mission_d1(int mission_num)
177 cfile_use_descent1_hogfile("descent.hog");
179 Current_mission_num = mission_num;
180 Current_mission_filename = Mission_list[mission_num].filename;
181 Current_mission_longname = Mission_list[mission_num].mission_name;
183 switch (D1_Builtin_mission_hogsize) {
184 case D1_SHAREWARE_MISSION_HOGSIZE:
188 Last_secret_level = 0;
191 for (i=0;i<Last_level;i++)
192 sprintf(Level_names[i], "level%02d.sdl", i+1);
195 case D1_MAC_SHARE_MISSION_HOGSIZE:
199 Last_secret_level = 0;
202 for (i=0;i<Last_level;i++)
203 sprintf(Level_names[i], "level%02d.sdl", i+1);
207 Int3(); // fall through
208 case D1_MISSION_HOGSIZE:
209 case D1_MAC_MISSION_HOGSIZE:
212 Last_level = BIM_LAST_LEVEL;
213 Last_secret_level = BIM_LAST_SECRET_LEVEL;
216 for (i=0;i<Last_level;i++)
217 sprintf(Level_names[i], "level%02d.rdl", i+1);
218 for (i=0;i<-Last_secret_level;i++)
219 sprintf(Secret_level_names[i], "levels%1d.rdl", i+1);
221 Secret_level_table[0] = 10;
222 Secret_level_table[1] = 21;
223 Secret_level_table[2] = 24;
227 strcpy(Briefing_text_filename,BIM_BRIEFING_FILE);
228 strcpy(Ending_text_filename,BIM_ENDING_FILE);
235 // Special versions of mission routines for shareware
238 int load_mission_shareware(int mission_num)
240 Current_mission_num = mission_num;
241 Current_mission_filename = Mission_list[mission_num].filename;
242 Current_mission_longname = Mission_list[mission_num].mission_name;
247 Last_secret_level = 0;
249 switch (Builtin_mission_hogsize) {
250 case MAC_SHARE_MISSION_HOGSIZE:
251 // mac demo is using the regular hog and rl2 files
252 strcpy(Level_names[0],"d2leva-1.rl2");
253 strcpy(Level_names[1],"d2leva-2.rl2");
254 strcpy(Level_names[2],"d2leva-3.rl2");
257 Int3(); // fall through
258 case SHAREWARE_MISSION_HOGSIZE:
259 strcpy(Level_names[0],"d2leva-1.sl2");
260 strcpy(Level_names[1],"d2leva-2.sl2");
261 strcpy(Level_names[2],"d2leva-3.sl2");
269 // Special versions of mission routines for Diamond/S3 version
272 int load_mission_oem(int mission_num)
274 Current_mission_num = mission_num;
275 Current_mission_filename = Mission_list[mission_num].filename;
276 Current_mission_longname = Mission_list[mission_num].mission_name;
281 Last_secret_level = -2;
283 strcpy(Level_names[0],"d2leva-1.rl2");
284 strcpy(Level_names[1],"d2leva-2.rl2");
285 strcpy(Level_names[2],"d2leva-3.rl2");
286 strcpy(Level_names[3],"d2leva-4.rl2");
288 strcpy(Secret_level_names[0],"d2leva-s.rl2");
290 strcpy(Level_names[4],"d2levb-1.rl2");
291 strcpy(Level_names[5],"d2levb-2.rl2");
292 strcpy(Level_names[6],"d2levb-3.rl2");
293 strcpy(Level_names[7],"d2levb-4.rl2");
295 strcpy(Secret_level_names[1],"d2levb-s.rl2");
297 Secret_level_table[0] = 1;
298 Secret_level_table[1] = 5;
304 //strips damn newline from end of line
305 char *mfgets(char *s,int n,CFILE *f)
310 if (r && s[strlen(s)-1] == '\n')
316 //compare a string for a token. returns true if match
317 int istok(char *buf,char *tok)
319 return strnicmp(buf,tok,strlen(tok)) == 0;
323 //adds a terminating 0 after a string at the first white space
324 void add_term(char *s)
326 while (*s && !isspace(*s)) s++;
331 //returns ptr to string after '=' & white space, or NULL if no '='
332 //adds 0 after parm at first white space
333 char *get_value(char *buf)
337 t = strchr(buf,'=')+1;
340 while (*t && isspace(*t)) t++;
346 return NULL; //error!
349 //reads a line, returns ptr to value of passed parm. returns NULL if none
350 char *get_parm_value(char *parm,CFILE *f)
354 if (!mfgets(buf,80,f))
358 return get_value(buf);
363 int ml_sort_func(mle *e0,mle *e1)
365 return stricmp(e0->mission_name,e1->mission_name);
369 extern char CDROM_dir[];
371 //returns 1 if file read ok, else 0
372 int read_mission_file(char *filename,int count,int location)
377 //printf("reading: %s\n", filename);
381 strcpy(filename2,MISSION_DIR);
385 songs_stop_redbook(); //so we can read from the CD
386 strcpy(filename2,CDROM_dir);
390 Int3(); //fall through
393 strcpy(filename2,"");
396 strcat(filename2,filename);
398 mfile = cfopen(filename2,"rb");
402 char temp[FILENAME_LEN],*t;
404 strcpy(temp,filename);
405 if ((t = strchr(temp,'.')) == NULL)
406 return 0; //missing extension
407 // look if it's .mn2 or .msn
408 Mission_list[count].descent_version = (t[3] == '2') ? 2 : 1;
409 *t = 0; //kill extension
411 strncpy( Mission_list[count].filename, temp, 9 );
412 Mission_list[count].anarchy_only_flag = 0;
413 Mission_list[count].location = location;
415 p = get_parm_value("name",mfile);
417 if (!p) { //try enhanced mission
418 cfseek(mfile,0,SEEK_SET);
419 p = get_parm_value("xname",mfile);
424 if (!p) { //try super-enhanced mission!
425 cfseek(mfile,0,SEEK_SET);
426 p = get_parm_value("zname",mfile);
432 if ((t=strchr(p,';'))!=NULL)
435 while (isspace(*t)) t--;
436 strncpy(Mission_list[count].mission_name,p,MISSION_NAME_LEN);
443 p = get_parm_value("type",mfile);
447 Mission_list[count].anarchy_only_flag = istok(p,"anarchy");
457 void add_d1_builtin_mission_to_list(int *count)
459 if (!cfexist("descent.hog"))
462 D1_Builtin_mission_hogsize = cfile_size("descent.hog");
464 switch (D1_Builtin_mission_hogsize) {
465 case D1_SHAREWARE_MISSION_HOGSIZE:
466 case D1_MAC_SHARE_MISSION_HOGSIZE:
467 strcpy(Mission_list[*count].filename, D1_MISSION_FILENAME);
468 strcpy(Mission_list[*count].mission_name, D1_SHAREWARE_MISSION_NAME);
469 Mission_list[*count].anarchy_only_flag = 0;
472 Warning("Unknown D1 hogsize %d\n", D1_Builtin_mission_hogsize);
475 case D1_MISSION_HOGSIZE:
476 case D1_MAC_MISSION_HOGSIZE:
477 strcpy(Mission_list[*count].filename, D1_MISSION_FILENAME);
478 strcpy(Mission_list[*count].mission_name, D1_MISSION_NAME);
479 Mission_list[*count].anarchy_only_flag = 0;
483 strcpy(D1_Builtin_mission_filename, Mission_list[*count].filename);
484 Mission_list[*count].descent_version = 1;
485 Mission_list[*count].anarchy_only_flag = 0;
490 void add_builtin_mission_to_list(int *count)
492 Builtin_mission_hogsize = cfile_size("descent2.hog");
493 if (Builtin_mission_hogsize == -1)
494 Builtin_mission_hogsize = cfile_size("d2demo.hog");
496 switch (Builtin_mission_hogsize) {
497 case SHAREWARE_MISSION_HOGSIZE:
498 case MAC_SHARE_MISSION_HOGSIZE:
499 strcpy(Mission_list[*count].filename,SHAREWARE_MISSION_FILENAME);
500 strcpy(Mission_list[*count].mission_name,SHAREWARE_MISSION_NAME);
501 Mission_list[*count].anarchy_only_flag = 0;
503 case OEM_MISSION_HOGSIZE:
504 strcpy(Mission_list[*count].filename,OEM_MISSION_FILENAME);
505 strcpy(Mission_list[*count].mission_name,OEM_MISSION_NAME);
506 Mission_list[*count].anarchy_only_flag = 0;
509 Warning("Unknown hogsize %d, trying %s\n", Builtin_mission_hogsize, FULL_MISSION_FILENAME ".mn2");
510 Int3(); //fall through
511 case FULL_MISSION_HOGSIZE:
512 if (!read_mission_file(FULL_MISSION_FILENAME ".mn2", 0, ML_CURDIR))
513 Error("Could not find required mission file <%s>", FULL_MISSION_FILENAME ".mn2");
516 strcpy(Builtin_mission_filename, Mission_list[*count].filename);
517 Mission_list[*count].descent_version = 2;
518 Mission_list[*count].anarchy_only_flag = 0;
523 void add_missions_to_list(char *search_name, int *count, int anarchy_mode)
526 if( !FileFindFirst( search_name, &find ) ) {
528 if (read_mission_file( find.name, *count, ML_MISSIONDIR )) {
530 if (anarchy_mode || !Mission_list[*count].anarchy_only_flag)
534 } while( !FileFindNext( &find ) && *count < MAX_MISSIONS);
536 if (*count >= MAX_MISSIONS)
537 mprintf((0, "Warning: more missions than d2x can handle\n"));
541 /* move <mission_name> to <place> on mission list, increment <place> */
542 void promote (char * mission_name, int * top_place, int num_missions)
545 char name[FILENAME_LEN], * t;
546 strcpy(name, mission_name);
547 if ((t = strchr(name,'.')) != NULL)
548 *t = 0; //kill extension
549 //printf("promoting: %s\n", name);
550 for (i = *top_place; i < num_missions; i++)
551 if (!stricmp(Mission_list[i].filename, name)) {
552 //swap mission positions
555 temp = Mission_list[*top_place];
556 Mission_list[*top_place] = Mission_list[i];
557 Mission_list[i] = temp;
565 //fills in the global list of missions. Returns the number of missions
566 //in the list. If anarchy_mode set, don't include non-anarchy levels.
568 extern char CDROM_dir[];
569 extern char AltHogDir[];
570 extern char AltHogdir_initialized;
572 int build_mission_list(int anarchy_mode)
574 static int num_missions=-1;
578 //now search for levels on disk
580 //@@Took out this code because after this routine was called once for
581 //@@a list of single-player missions, a subsequent call for a list of
582 //@@anarchy missions would not scan again, and thus would not find the
583 //@@anarchy-only missions. If we retain the minimum level of install,
584 //@@we may want to put the code back in, having it always scan for all
585 //@@missions, and have the code that uses it sort out the ones it wants.
586 //@@ if (num_missions != -1) {
587 //@@ if (Current_mission_num != 0)
588 //@@ load_mission(0); //set built-in mission as default
589 //@@ return num_missions;
592 add_builtin_mission_to_list(&count); //read built-in first
593 add_d1_builtin_mission_to_list(&count);
594 add_missions_to_list(MISSION_DIR "*.mn2", &count, anarchy_mode);
595 add_missions_to_list(MISSION_DIR "*.msn", &count, anarchy_mode);
597 if (AltHogdir_initialized) {
598 char search_name[PATH_MAX + 5];
599 strcpy(search_name, AltHogDir);
600 strcat(search_name, "/" MISSION_DIR "*.mn2");
601 add_missions_to_list(search_name, &count, anarchy_mode);
602 strcpy(search_name, AltHogDir);
603 strcat(search_name, "/" MISSION_DIR "*.msn");
604 add_missions_to_list(search_name, &count, anarchy_mode);
607 // move original missions (in story-chronological order)
608 // to top of mission list
610 promote("descent", &top_place, count); // original descent 1 mission
611 D1_Builtin_mission_num = top_place - 1;
612 promote(Builtin_mission_filename, &top_place, count); // d2 or d2demo
613 Builtin_mission_num = top_place - 1;
614 promote("d2x", &top_place, count); // vertigo
616 if (count > top_place)
617 qsort(&Mission_list[top_place],
619 sizeof(*Mission_list),
620 (int (*)( const void *, const void * ))ml_sort_func);
623 if (count > top_place)
624 qsort(&Mission_list[top_place],
626 sizeof(*Mission_list),
627 (int (*)( const void *, const void * ))ml_sort_func);
629 //load_mission(0); //set built-in mission as default
631 num_missions = count;
636 void init_extra_robot_movie(char *filename);
638 //values for built-in mission
640 //loads the specfied mission from the mission list.
641 //build_mission_list() must have been called.
642 //Returns true if mission loaded ok, else false.
643 int load_mission(int mission_num)
648 int enhanced_mission = 0;
650 if (mission_num == D1_Builtin_mission_num) {
651 cfile_use_descent1_hogfile("descent.hog");
652 switch (D1_Builtin_mission_hogsize) {
654 Int3(); // fall through
655 case D1_MISSION_HOGSIZE:
656 case D1_MAC_MISSION_HOGSIZE:
657 case D1_SHAREWARE_MISSION_HOGSIZE:
658 case D1_MAC_SHARE_MISSION_HOGSIZE:
659 return load_mission_d1(mission_num);
664 if (mission_num == Builtin_mission_num) {
665 switch (Builtin_mission_hogsize) {
666 case SHAREWARE_MISSION_HOGSIZE:
667 case MAC_SHARE_MISSION_HOGSIZE:
668 return load_mission_shareware(mission_num);
670 case OEM_MISSION_HOGSIZE:
671 return load_mission_oem(mission_num);
674 Int3(); // fall through
675 case FULL_MISSION_HOGSIZE:
676 // continue on... (use d2.mn2 from hogfile)
681 Current_mission_num = mission_num;
683 mprintf(( 0, "Loading mission %d\n", mission_num ));
685 //read mission from file
687 switch (Mission_list[mission_num].location) {
689 strcpy(buf,MISSION_DIR);
692 strcpy(buf,CDROM_dir);
695 Int3(); //fall through
700 strcat(buf,Mission_list[mission_num].filename);
701 if (Mission_list[mission_num].descent_version == 2)
706 mfile = cfopen(buf,"rb");
708 Current_mission_num = -1;
712 //for non-builtin missions, load HOG
713 if (strcmp(Mission_list[mission_num].filename, Builtin_mission_filename)) {
715 strcpy(buf+strlen(buf)-4,".hog"); //change extension
717 found_hogfile = cfile_use_alternate_hogfile(buf);
719 #ifdef RELEASE //for release, require mission to be in hogfile
720 if (! found_hogfile) {
722 Current_mission_num = -1;
727 // for Descent 1 missions, load descent.hog
728 if (Mission_list[mission_num].descent_version == 1 && strcmp(buf, "descent.hog"))
729 if (!cfile_use_descent1_hogfile("descent.hog"))
730 Warning("descent.hog not available, this mission may be missing some files required for briefings\n");
735 Last_secret_level = 0;
736 Briefing_text_filename[0] = 0;
737 Ending_text_filename[0] = 0;
739 while (mfgets(buf,80,mfile)) {
741 if (istok(buf,"name"))
742 continue; //already have name, go to next line
743 if (istok(buf,"xname")) {
744 enhanced_mission = 1;
745 continue; //already have name, go to next line
747 if (istok(buf,"zname")) {
748 enhanced_mission = 2;
749 continue; //already have name, go to next line
751 else if (istok(buf,"type"))
752 continue; //already have name, go to next line
753 else if (istok(buf,"hog")) {
756 while (*(bufp++) != '=')
760 while (*(++bufp) == ' ')
763 cfile_use_alternate_hogfile(bufp);
764 mprintf((0, "Hog file override = [%s]\n", bufp));
766 else if (istok(buf,"briefing")) {
767 if ((v = get_value(buf)) != NULL) {
770 strcpy(Briefing_text_filename,v);
773 else if (istok(buf,"ending")) {
774 if ((v = get_value(buf)) != NULL) {
777 strcpy(Ending_text_filename,v);
780 else if (istok(buf,"num_levels")) {
782 if ((v=get_value(buf))!=NULL) {
787 for (i=0;i<n_levels && mfgets(buf,80,mfile);i++) {
790 if (strlen(buf) <= 12) {
791 strcpy(Level_names[i],buf);
800 else if (istok(buf,"num_secrets")) {
801 if ((v=get_value(buf))!=NULL) {
804 N_secret_levels = atoi(v);
806 Assert(N_secret_levels <= MAX_SECRET_LEVELS_PER_MISSION);
808 for (i=0;i<N_secret_levels && mfgets(buf,80,mfile);i++) {
812 if ((t=strchr(buf,','))!=NULL) *t++=0;
817 if (strlen(buf) <= 12) {
818 strcpy(Secret_level_names[i],buf);
819 Secret_level_table[i] = atoi(t);
820 if (Secret_level_table[i]<1 || Secret_level_table[i]>Last_level)
835 if (Last_level <= 0) {
836 Current_mission_num = -1; //no valid mission loaded
840 Current_mission_filename = Mission_list[Current_mission_num].filename;
841 Current_mission_longname = Mission_list[Current_mission_num].mission_name;
843 if (enhanced_mission) {
845 extern void bm_read_extra_robots();
846 sprintf(t,"%s.ham",Current_mission_filename);
847 bm_read_extra_robots(t,enhanced_mission);
848 strncpy(t,Current_mission_filename,6);
850 init_extra_robot_movie(t);
856 //loads the named mission if exists.
857 //Returns true if mission loaded ok, else false.
858 int load_mission_by_name(char *mission_name)
862 n = build_mission_list(1);
865 if (!stricmp(mission_name,Mission_list[i].filename))
866 return load_mission(i);
868 return 0; //couldn't find mission