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.
16 * Code to handle multiple missions
36 #include "ignorecase.h"
39 //values for d1 built-in mission
40 #define BIM_LAST_LEVEL 27
41 #define BIM_LAST_SECRET_LEVEL -3
42 #define BIM_BRIEFING_FILE "briefing.tex"
43 #define BIM_ENDING_FILE "endreg.tex"
45 #define OEM_LAST_LEVEL 15
46 #define OEM_LAST_SECRET_LEVEL -1
47 #define OEM_BRIEFING_FILE "briefsat.tex"
48 #define OEM_ENDING_FILE "endsat.tex"
50 #define SHAREWARE_LAST_LEVEL 7
51 #define SHAREWARE_ENDING_FILE "ending.tex"
53 #define MAC_SHARE_LAST_LEVEL 3
58 char *filename; // filename without extension
59 int builtin_hogsize; // if it's the built-in mission, used for determining the version
60 char mission_name[MISSION_NAME_LEN+1];
61 ubyte descent_version; // descent 1 or descent 2?
62 ubyte anarchy_only_flag; // if true, mission is anarchy only
63 char *path; // relative file path
64 int location; // see defines below
67 //values that describe where a mission is located
69 #define ML_MISSIONDIR 1
71 int num_missions = -1;
73 Mission *Current_mission = NULL; // currently loaded mission
76 // Special versions of mission routines for d1 builtins
79 int load_mission_d1(void)
83 switch (cfile_size("descent.hog")) {
84 case D1_SHAREWARE_MISSION_HOGSIZE:
85 case D1_SHAREWARE_10_MISSION_HOGSIZE:
88 Last_level = SHAREWARE_LAST_LEVEL;
89 Last_secret_level = 0;
90 strcpy(Briefing_text_filename, BIM_BRIEFING_FILE);
91 strcpy(Ending_text_filename, SHAREWARE_ENDING_FILE);
94 for (i=0;i<Last_level;i++)
95 sprintf(Level_names[i], "level%02d.sdl", i+1);
98 case D1_MAC_SHARE_MISSION_HOGSIZE:
101 Last_level = MAC_SHARE_LAST_LEVEL;
102 Last_secret_level = 0;
103 strcpy(Briefing_text_filename, BIM_BRIEFING_FILE);
104 strcpy(Ending_text_filename, SHAREWARE_ENDING_FILE);
107 for (i=0;i<Last_level;i++)
108 sprintf(Level_names[i], "level%02d.sdl", i+1);
111 case D1_OEM_MISSION_HOGSIZE:
112 case D1_OEM_10_MISSION_HOGSIZE:
115 Last_level = OEM_LAST_LEVEL;
116 Last_secret_level = OEM_LAST_SECRET_LEVEL;
117 strcpy(Briefing_text_filename, OEM_BRIEFING_FILE);
118 strcpy(Ending_text_filename, OEM_ENDING_FILE);
121 for (i=0; i < Last_level - 1; i++)
122 sprintf(Level_names[i], "level%02d.rdl", i+1);
123 sprintf(Level_names[i], "saturn%02d.rdl", i+1);
124 for (i=0; i < -Last_secret_level; i++)
125 sprintf(Secret_level_names[i], "levels%1d.rdl", i+1);
127 Secret_level_table[0] = 10;
131 Int3(); // fall through
132 case D1_MISSION_HOGSIZE:
133 case D1_MISSION_HOGSIZE2:
134 case D1_10_MISSION_HOGSIZE:
135 case D1_MAC_MISSION_HOGSIZE:
138 Last_level = BIM_LAST_LEVEL;
139 Last_secret_level = BIM_LAST_SECRET_LEVEL;
140 strcpy(Briefing_text_filename, BIM_BRIEFING_FILE);
141 strcpy(Ending_text_filename, BIM_ENDING_FILE);
144 for (i=0;i<Last_level;i++)
145 sprintf(Level_names[i], "level%02d.rdl", i+1);
146 for (i=0;i<-Last_secret_level;i++)
147 sprintf(Secret_level_names[i], "levels%1d.rdl", i+1);
149 Secret_level_table[0] = 10;
150 Secret_level_table[1] = 21;
151 Secret_level_table[2] = 24;
161 // Special versions of mission routines for shareware
164 int load_mission_shareware(void)
166 strcpy(Current_mission->mission_name, SHAREWARE_MISSION_NAME);
167 Current_mission->descent_version = 2;
168 Current_mission->anarchy_only_flag = 0;
170 switch (Current_mission->builtin_hogsize) {
171 case MAC_SHARE_MISSION_HOGSIZE:
175 Last_secret_level = -1;
177 // mac demo is using the regular hog and rl2 files
178 strcpy(Level_names[0],"d2leva-1.rl2");
179 strcpy(Level_names[1],"d2leva-2.rl2");
180 strcpy(Level_names[2],"d2leva-3.rl2");
181 strcpy(Level_names[3],"d2leva-4.rl2");
182 strcpy(Secret_level_names[0],"d2leva-s.rl2");
185 Int3(); // fall through
186 case SHAREWARE_MISSION_HOGSIZE:
190 Last_secret_level = 0;
192 strcpy(Level_names[0],"d2leva-1.sl2");
193 strcpy(Level_names[1],"d2leva-2.sl2");
194 strcpy(Level_names[2],"d2leva-3.sl2");
202 // Special versions of mission routines for Diamond/S3 version
205 int load_mission_oem(void)
207 strcpy(Current_mission->mission_name, OEM_MISSION_NAME);
208 Current_mission->descent_version = 2;
209 Current_mission->anarchy_only_flag = 0;
214 Last_secret_level = -2;
216 strcpy(Level_names[0],"d2leva-1.rl2");
217 strcpy(Level_names[1],"d2leva-2.rl2");
218 strcpy(Level_names[2],"d2leva-3.rl2");
219 strcpy(Level_names[3],"d2leva-4.rl2");
221 strcpy(Secret_level_names[0],"d2leva-s.rl2");
223 strcpy(Level_names[4],"d2levb-1.rl2");
224 strcpy(Level_names[5],"d2levb-2.rl2");
225 strcpy(Level_names[6],"d2levb-3.rl2");
226 strcpy(Level_names[7],"d2levb-4.rl2");
228 strcpy(Secret_level_names[1],"d2levb-s.rl2");
230 Secret_level_table[0] = 1;
231 Secret_level_table[1] = 5;
237 //compare a string for a token. returns true if match
238 int istok(char *buf,char *tok)
240 return strnicmp(buf,tok,strlen(tok)) == 0;
244 //adds a terminating 0 after a string at the first white space
245 void add_term(char *s)
247 while (*s && !isspace(*s)) s++;
252 //returns ptr to string after '=' & white space, or NULL if no '='
253 //adds 0 after parm at first white space
254 char *get_value(char *buf)
258 t = strchr(buf,'=')+1;
261 while (*t && isspace(*t)) t++;
267 return NULL; //error!
270 //reads a line, returns ptr to value of passed parm. returns NULL if none
271 char *get_parm_value(char *parm,CFILE *f)
275 if (!cfgets(buf,80,f))
279 return get_value(buf);
284 int ml_sort_func(mle *e0,mle *e1)
286 return stricmp(e0->mission_name,e1->mission_name);
291 //returns 1 if file read ok, else 0
292 int read_mission_file(mle *mission, char *filename, int location)
297 //printf("reading: %s\n", filename);
301 strcpy(filename2,MISSION_DIR);
305 Int3(); //fall through
308 strcpy(filename2,"");
311 strcat(filename2,filename);
313 mfile = cfopen(filename2,"rb");
317 char temp[PATH_MAX], *ext;
319 strcpy(temp,filename);
320 p = strrchr(temp, '/'); // get the filename at the end of the path
325 if ((ext = strchr(p, '.')) == NULL)
326 return 0; //missing extension
327 // look if it's .mn2 or .msn
328 mission->descent_version = (ext[3] == '2') ? 2 : 1;
329 *ext = 0; //kill extension
331 mission->path = d_strdup(temp);
332 mission->anarchy_only_flag = 0;
333 mission->filename = mission->path + (p - temp);
334 mission->location = location;
336 p = get_parm_value("name",mfile);
338 if (!p) { //try enhanced mission
339 cfseek(mfile,0,SEEK_SET);
340 p = get_parm_value("xname",mfile);
343 if (!p) { //try super-enhanced mission!
344 cfseek(mfile,0,SEEK_SET);
345 p = get_parm_value("zname",mfile);
350 if ((t=strchr(p,';'))!=NULL)
354 *t-- = 0; // remove trailing whitespace
355 if (strlen(p) > MISSION_NAME_LEN)
356 p[MISSION_NAME_LEN] = 0;
357 strncpy(mission->mission_name, p, MISSION_NAME_LEN + 1);
361 d_free(mission->path);
365 p = get_parm_value("type",mfile);
369 mission->anarchy_only_flag = istok(p,"anarchy");
379 void add_d1_builtin_mission_to_list(mle *mission)
383 if (!cfexist("descent.hog"))
386 size = cfile_size("descent.hog");
389 case D1_SHAREWARE_MISSION_HOGSIZE:
390 case D1_SHAREWARE_10_MISSION_HOGSIZE:
391 case D1_MAC_SHARE_MISSION_HOGSIZE:
392 mission->filename = d_strdup(D1_MISSION_FILENAME);
393 strcpy(mission->mission_name, D1_SHAREWARE_MISSION_NAME);
394 mission->anarchy_only_flag = 0;
396 case D1_OEM_MISSION_HOGSIZE:
397 case D1_OEM_10_MISSION_HOGSIZE:
398 mission->filename = d_strdup(D1_MISSION_FILENAME);
399 strcpy(mission->mission_name, D1_OEM_MISSION_NAME);
400 mission->anarchy_only_flag = 0;
403 Warning("Unknown D1 hogsize %d\n", size);
406 case D1_MISSION_HOGSIZE:
407 case D1_MISSION_HOGSIZE2:
408 case D1_10_MISSION_HOGSIZE:
409 case D1_MAC_MISSION_HOGSIZE:
410 mission->filename = d_strdup(D1_MISSION_FILENAME);
411 strcpy(mission->mission_name, D1_MISSION_NAME);
412 mission->anarchy_only_flag = 0;
416 mission->descent_version = 1;
417 mission->anarchy_only_flag = 0;
418 mission->builtin_hogsize = 0;
419 mission->path = mission->filename;
424 void add_builtin_mission_to_list(mle *mission, char *name)
426 int size = cfile_size("descent2.hog");
429 size = cfile_size("d2demo.hog");
432 case SHAREWARE_MISSION_HOGSIZE:
433 case MAC_SHARE_MISSION_HOGSIZE:
434 mission->filename = d_strdup(SHAREWARE_MISSION_FILENAME);
435 strcpy(mission->mission_name,SHAREWARE_MISSION_NAME);
436 mission->anarchy_only_flag = 0;
438 case OEM_MISSION_HOGSIZE:
439 mission->filename = d_strdup(OEM_MISSION_FILENAME);
440 strcpy(mission->mission_name,OEM_MISSION_NAME);
441 mission->anarchy_only_flag = 0;
444 Warning("Unknown hogsize %d, trying %s\n", size, FULL_MISSION_FILENAME ".mn2");
445 Int3(); //fall through
446 case FULL_MISSION_HOGSIZE:
447 case FULL_10_MISSION_HOGSIZE:
448 case MAC_FULL_MISSION_HOGSIZE:
449 if (!read_mission_file(mission, FULL_MISSION_FILENAME ".mn2", ML_CURDIR))
450 Error("Could not find required mission file <%s>", FULL_MISSION_FILENAME ".mn2");
453 mission->path = mission->filename;
454 strcpy(name, mission->filename);
455 mission->builtin_hogsize = size;
456 mission->descent_version = 2;
457 mission->anarchy_only_flag = 0;
462 void add_missions_to_list(mle *mission_list, char *path, char *rel_path, int anarchy_mode)
464 char **find, **i, *ext;
466 find = PHYSFS_enumerateFiles(path);
468 for (i = find; *i != NULL; i++)
470 if (strlen(path) + strlen(*i) + 1 >= PATH_MAX)
471 continue; // path is too long
473 strcat(rel_path, *i);
474 if (PHYSFS_isDirectory(path))
476 strcat(rel_path, "/");
477 add_missions_to_list(mission_list, path, rel_path, anarchy_mode);
478 *(strrchr(path, '/')) = 0;
480 else if ((ext = strrchr(*i, '.')) && (!strnicmp(ext, ".msn", 4) || !strnicmp(ext, ".mn2", 4)))
481 if (read_mission_file(&mission_list[num_missions], rel_path, ML_MISSIONDIR))
483 if (anarchy_mode || !mission_list[num_missions].anarchy_only_flag)
485 mission_list[num_missions].builtin_hogsize = 0;
489 d_free(mission_list[num_missions].path);
492 if (num_missions >= MAX_MISSIONS)
494 mprintf((0, "Warning: more missions than d2x can handle\n"));
498 (strrchr(path, '/'))[1] = 0; // chop off the entry
501 PHYSFS_freeList(find);
504 /* move <mission_name> to <place> on mission list, increment <place> */
505 void promote (mle *mission_list, char * mission_name, int * top_place)
508 char name[FILENAME_LEN], * t;
509 strcpy(name, mission_name);
510 if ((t = strchr(name,'.')) != NULL)
511 *t = 0; //kill extension
512 //printf("promoting: %s\n", name);
513 for (i = *top_place; i < num_missions; i++)
514 if (!stricmp(mission_list[i].filename, name)) {
515 //swap mission positions
518 temp = mission_list[*top_place];
519 mission_list[*top_place] = mission_list[i];
520 mission_list[i] = temp;
526 void free_mission(void)
528 // May become more complex with the editor
529 if (Current_mission) {
530 d_free(Current_mission->filename);
531 d_free(Current_mission);
537 //fills in the global list of missions. Returns the number of missions
538 //in the list. If anarchy_mode is set, then also add anarchy-only missions.
541 extern char AltHogDir[];
542 extern char AltHogdir_initialized;
545 mle *build_mission_list(int anarchy_mode)
549 char builtin_mission_filename[FILENAME_LEN];
550 char search_str[PATH_MAX] = MISSION_DIR;
552 //now search for levels on disk
554 //@@Took out this code because after this routine was called once for
555 //@@a list of single-player missions, a subsequent call for a list of
556 //@@anarchy missions would not scan again, and thus would not find the
557 //@@anarchy-only missions. If we retain the minimum level of install,
558 //@@we may want to put the code back in, having it always scan for all
559 //@@missions, and have the code that uses it sort out the ones it wants.
560 //@@ if (num_missions != -1) {
561 //@@ if (Current_mission_num != 0)
562 //@@ load_mission(0); //set built-in mission as default
563 //@@ return num_missions;
566 MALLOC(mission_list, mle, MAX_MISSIONS);
569 add_builtin_mission_to_list(mission_list + num_missions, builtin_mission_filename); //read built-in first
570 add_d1_builtin_mission_to_list(mission_list + num_missions);
571 add_missions_to_list(mission_list, search_str, search_str + strlen(search_str), anarchy_mode);
573 // move original missions (in story-chronological order)
574 // to top of mission list
576 promote(mission_list, "descent", &top_place); // original descent 1 mission
577 promote(mission_list, builtin_mission_filename, &top_place); // d2 or d2demo
578 promote(mission_list, "d2x", &top_place); // vertigo
580 if (num_missions > top_place)
581 qsort(&mission_list[top_place],
582 num_missions - top_place,
583 sizeof(*mission_list),
584 (int (*)( const void *, const void * ))ml_sort_func);
587 if (num_missions > top_place)
588 qsort(&mission_list[top_place],
589 num_missions - top_place,
590 sizeof(*mission_list),
591 (int (*)( const void *, const void * ))ml_sort_func);
593 //load_mission(0); //set built-in mission as default
595 atexit(free_mission);
600 void free_mission_list(mle *mission_list)
604 for (i = 0; i < num_missions; i++)
605 d_free(mission_list[i].path);
607 d_free(mission_list);
611 void init_extra_robot_movie(char *filename);
613 //values for built-in mission
615 //loads the specfied mission from the mission list.
616 //build_mission_list() must have been called.
617 //Returns true if mission loaded ok, else false.
618 int load_mission(mle *mission)
621 char buf[PATH_MAX], *v;
626 Current_mission = d_malloc(sizeof(Mission));
627 if (!Current_mission) return 0;
628 *(mle *) Current_mission = *mission;
629 Current_mission->filename = d_strdup(mission->filename); // don't want to lose it
633 // for Descent 1 missions, load descent.hog
635 if (!cfile_init("descent.hog"))
636 Warning("descent.hog not available, this mission may be missing some files required for briefings and exit sequence\n");
637 if (!stricmp(Current_mission_filename, D1_MISSION_FILENAME))
638 return load_mission_d1();
640 cfile_close("descent.hog");
642 if (PLAYING_BUILTIN_MISSION) {
643 switch (Current_mission->builtin_hogsize) {
644 case SHAREWARE_MISSION_HOGSIZE:
645 case MAC_SHARE_MISSION_HOGSIZE:
646 return load_mission_shareware();
648 case OEM_MISSION_HOGSIZE:
649 return load_mission_oem();
652 Int3(); // fall through
653 case FULL_MISSION_HOGSIZE:
654 case FULL_10_MISSION_HOGSIZE:
655 case MAC_FULL_MISSION_HOGSIZE:
656 // continue on... (use d2.mn2 from hogfile)
661 mprintf(( 0, "Loading mission %s\n", Current_mission_filename ));
663 //read mission from file
665 switch (mission->location) {
667 strcpy(buf,MISSION_DIR);
670 Int3(); //fall through
675 strcat(buf, mission->path);
676 if (mission->descent_version == 2)
681 PHYSFSEXT_locateCorrectCase(buf);
683 mfile = cfopen(buf,"rb");
689 //for non-builtin missions, load HOG
690 if (!PLAYING_BUILTIN_MISSION) {
692 strcpy(buf+strlen(buf)-4,".hog"); //change extension
694 PHYSFSEXT_locateCorrectCase(buf);
696 found_hogfile = cfile_init(buf);
698 #ifdef RELEASE //for release, require mission to be in hogfile
699 if (! found_hogfile) {
709 Last_secret_level = 0;
710 Briefing_text_filename[0] = 0;
711 Ending_text_filename[0] = 0;
713 while (cfgets(buf,80,mfile)) {
715 if (istok(buf,"name")) {
716 Current_mission->enhanced = 0;
717 continue; //already have name, go to next line
719 if (istok(buf,"xname")) {
720 Current_mission->enhanced = 1;
721 continue; //already have name, go to next line
723 if (istok(buf,"zname")) {
724 Current_mission->enhanced = 2;
725 continue; //already have name, go to next line
727 else if (istok(buf,"type"))
728 continue; //already have name, go to next line
729 else if (istok(buf,"hog")) {
732 while (*(bufp++) != '=')
736 while (*(++bufp) == ' ')
740 mprintf((0, "Hog file override = [%s]\n", bufp));
742 else if (istok(buf,"briefing")) {
743 if ((v = get_value(buf)) != NULL) {
746 strcpy(Briefing_text_filename,v);
749 else if (istok(buf,"ending")) {
750 if ((v = get_value(buf)) != NULL) {
753 strcpy(Ending_text_filename,v);
756 else if (istok(buf,"num_levels")) {
758 if ((v=get_value(buf))!=NULL) {
763 for (i=0;i<n_levels && cfgets(buf,80,mfile);i++) {
766 if (strlen(buf) <= 12) {
767 strcpy(Level_names[i],buf);
776 else if (istok(buf,"num_secrets")) {
777 if ((v=get_value(buf))!=NULL) {
780 N_secret_levels = atoi(v);
782 Assert(N_secret_levels <= MAX_SECRET_LEVELS_PER_MISSION);
784 for (i=0;i<N_secret_levels && cfgets(buf,80,mfile);i++) {
788 if ((t=strchr(buf,','))!=NULL) *t++=0;
793 if (strlen(buf) <= 12) {
794 strcpy(Secret_level_names[i],buf);
795 Secret_level_table[i] = atoi(t);
796 if (Secret_level_table[i]<1 || Secret_level_table[i]>Last_level)
811 if (Last_level <= 0) {
812 free_mission(); //no valid mission loaded
816 if (Current_mission->enhanced) {
818 extern void bm_read_extra_robots();
819 sprintf(t,"%s.ham",Current_mission_filename);
820 bm_read_extra_robots(t, Current_mission->enhanced);
821 strncpy(t,Current_mission_filename,6);
822 init_extra_robot_movie(t);
828 //loads the named mission if exists.
829 //Returns true if mission loaded ok, else false.
830 int load_mission_by_name(char *mission_name)
833 mle *mission_list = build_mission_list(1);
836 for (i = 0; i < num_missions; i++)
837 if (!stricmp(mission_name, mission_list[i].filename))
838 found = load_mission(mission_list + i);
840 free_mission_list(mission_list);
844 int select_mission(int anarchy_mode, char *message)
846 mle *mission_list = build_mission_list(anarchy_mode);
849 if (num_missions <= 1) {
850 new_mission_num = load_mission(mission_list) ? 0 : -1;
852 int i, default_mission;
853 char * m[MAX_MISSIONS];
856 for (i = 0; i < num_missions; i++) {
857 m[i] = mission_list[i].mission_name;
858 if ( !stricmp( m[i], config_last_mission.string ) )
862 new_mission_num = newmenu_listbox1( message, num_missions, m, 1, default_mission, NULL );
864 if (new_mission_num >= 0) {
866 cvar_set_cvar( &config_last_mission, m[new_mission_num] );
868 if (!load_mission(mission_list + new_mission_num)) {
869 nm_messagebox( NULL, 1, TXT_OK, TXT_MISSION_ERROR);
870 new_mission_num = -1;
875 free_mission_list(mission_list);
876 return (new_mission_num >= 0);
881 void create_new_mission(void)
885 Current_mission = d_malloc(sizeof(Mission));
886 Current_mission->filename = d_strdup("new_mission");
887 strcpy(Level_names[0], "GAMESAVE.LVL");
892 //set a new highest level for player for this mission
893 int mission_write_config(void)
895 char filename[FILENAME_LEN+15];
898 PHYSFS_mkdir(MISSION_DIR);
900 sprintf(filename, MISSION_DIR "%s.cfg", Current_mission_filename);
901 file = PHYSFSX_openWriteBuffered(filename);
905 nm_messagebox(NULL, 1, TXT_OK, "Cannot open mission config file");
909 PHYSFSX_printf(file, "%s=%d\n", Player_highest_level.name, Current_level_num);
917 //gets the player's highest level from the file for this mission
918 int mission_read_config(void)
920 cvar_setint(&Player_highest_level, 0);
922 cmd_appendf("exec " MISSION_DIR "%s.cfg", Current_mission_filename);