]> icculus.org git repositories - btb/d2x.git/blob - main/mission.c
get rid of CDRom_dir, other kludges
[btb/d2x.git] / main / mission.c
1 /*
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.
12 */
13
14 /*
15  *
16  * Code to handle multiple missions
17  *
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <conf.h>
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <limits.h>
29
30 #include "pstypes.h"
31 #include "cfile.h"
32
33 #include "strutil.h"
34 #include "inferno.h"
35 #include "mission.h"
36 #include "gameseq.h"
37 #include "titles.h"
38 #include "songs.h"
39 #include "mono.h"
40 #include "error.h"
41 #include "config.h"
42 #include "newmenu.h"
43 #include "text.h"
44 #include "u_mem.h"
45 #include "ignorecase.h"
46
47 //values for d1 built-in mission
48 #define BIM_LAST_LEVEL          27
49 #define BIM_LAST_SECRET_LEVEL   -3
50 #define BIM_BRIEFING_FILE       "briefing.tex"
51 #define BIM_ENDING_FILE         "endreg.tex"
52
53 //mission list entry
54 typedef struct mle {
55         char    *filename;          // filename without extension
56         int     builtin_hogsize;    // if it's the built-in mission, used for determining the version
57         char    mission_name[MISSION_NAME_LEN+1];
58         ubyte   descent_version;    // descent 1 or descent 2?
59         ubyte   anarchy_only_flag;  // if true, mission is anarchy only
60         char    *path;                          // relative file path
61         int             location;           // see defines below
62 } mle;
63
64 //values that describe where a mission is located
65 #define ML_CURDIR       0
66 #define ML_MISSIONDIR   1
67
68 int num_missions = -1;
69
70 Mission *Current_mission = NULL; // currently loaded mission
71
72 //
73 //  Special versions of mission routines for d1 builtins
74 //
75
76 int load_mission_d1(void)
77 {
78         int i;
79
80         switch (cfile_size("descent.hog")) {
81         case D1_SHAREWARE_MISSION_HOGSIZE:
82         case D1_SHAREWARE_10_MISSION_HOGSIZE:
83                 N_secret_levels = 0;
84
85                 Last_level = 7;
86                 Last_secret_level = 0;
87
88                 //build level names
89                 for (i=0;i<Last_level;i++)
90                         sprintf(Level_names[i], "level%02d.sdl", i+1);
91
92                 break;
93         case D1_MAC_SHARE_MISSION_HOGSIZE:
94                 N_secret_levels = 0;
95
96                 Last_level = 3;
97                 Last_secret_level = 0;
98
99                 //build level names
100                 for (i=0;i<Last_level;i++)
101                         sprintf(Level_names[i], "level%02d.sdl", i+1);
102
103                 break;
104         case D1_OEM_MISSION_HOGSIZE:
105         case D1_OEM_10_MISSION_HOGSIZE:
106                 N_secret_levels = 1;
107
108                 Last_level = 15;
109                 Last_secret_level = -1;
110
111                 //build level names
112                 for (i=0; i < Last_level - 1; i++)
113                         sprintf(Level_names[i], "level%02d.rdl", i+1);
114                 sprintf(Level_names[i], "saturn%02d.rdl", i+1);
115                 for (i=0; i < -Last_secret_level; i++)
116                         sprintf(Secret_level_names[i], "levels%1d.rdl", i+1);
117
118                 Secret_level_table[0] = 10;
119
120                 break;
121         default:
122                 Int3(); // fall through
123         case D1_MISSION_HOGSIZE:
124         case D1_10_MISSION_HOGSIZE:
125         case D1_MAC_MISSION_HOGSIZE:
126                 N_secret_levels = 3;
127
128                 Last_level = BIM_LAST_LEVEL;
129                 Last_secret_level = BIM_LAST_SECRET_LEVEL;
130
131                 //build level names
132                 for (i=0;i<Last_level;i++)
133                         sprintf(Level_names[i], "level%02d.rdl", i+1);
134                 for (i=0;i<-Last_secret_level;i++)
135                         sprintf(Secret_level_names[i], "levels%1d.rdl", i+1);
136
137                 Secret_level_table[0] = 10;
138                 Secret_level_table[1] = 21;
139                 Secret_level_table[2] = 24;
140
141                 break;
142         }
143         strcpy(Briefing_text_filename,BIM_BRIEFING_FILE);
144         strcpy(Ending_text_filename,BIM_ENDING_FILE);
145
146         return 1;
147 }
148
149
150 //
151 //  Special versions of mission routines for shareware
152 //
153
154 int load_mission_shareware(void)
155 {
156     strcpy(Current_mission->mission_name, SHAREWARE_MISSION_NAME);
157     Current_mission->descent_version = 2;
158     Current_mission->anarchy_only_flag = 0;
159     
160     switch (Current_mission->builtin_hogsize) {
161         case MAC_SHARE_MISSION_HOGSIZE:
162                 N_secret_levels = 1;
163
164                 Last_level = 4;
165                 Last_secret_level = -1;
166
167                 // mac demo is using the regular hog and rl2 files
168                 strcpy(Level_names[0],"d2leva-1.rl2");
169                 strcpy(Level_names[1],"d2leva-2.rl2");
170                 strcpy(Level_names[2],"d2leva-3.rl2");
171                 strcpy(Level_names[3],"d2leva-4.rl2");
172                 strcpy(Secret_level_names[0],"d2leva-s.rl2");
173                 break;
174         default:
175                 Int3(); // fall through
176         case SHAREWARE_MISSION_HOGSIZE:
177                 N_secret_levels = 0;
178
179                 Last_level = 3;
180                 Last_secret_level = 0;
181
182                 strcpy(Level_names[0],"d2leva-1.sl2");
183                 strcpy(Level_names[1],"d2leva-2.sl2");
184                 strcpy(Level_names[2],"d2leva-3.sl2");
185         }
186
187         return 1;
188 }
189
190
191 //
192 //  Special versions of mission routines for Diamond/S3 version
193 //
194
195 int load_mission_oem(void)
196 {
197     strcpy(Current_mission->mission_name, OEM_MISSION_NAME);
198     Current_mission->descent_version = 2;
199     Current_mission->anarchy_only_flag = 0;
200     
201         N_secret_levels = 2;
202
203         Last_level = 8;
204         Last_secret_level = -2;
205
206         strcpy(Level_names[0],"d2leva-1.rl2");
207         strcpy(Level_names[1],"d2leva-2.rl2");
208         strcpy(Level_names[2],"d2leva-3.rl2");
209         strcpy(Level_names[3],"d2leva-4.rl2");
210
211         strcpy(Secret_level_names[0],"d2leva-s.rl2");
212
213         strcpy(Level_names[4],"d2levb-1.rl2");
214         strcpy(Level_names[5],"d2levb-2.rl2");
215         strcpy(Level_names[6],"d2levb-3.rl2");
216         strcpy(Level_names[7],"d2levb-4.rl2");
217
218         strcpy(Secret_level_names[1],"d2levb-s.rl2");
219
220         Secret_level_table[0] = 1;
221         Secret_level_table[1] = 5;
222
223         return 1;
224 }
225
226
227 //compare a string for a token. returns true if match
228 int istok(char *buf,char *tok)
229 {
230         return strnicmp(buf,tok,strlen(tok)) == 0;
231
232 }
233
234 //adds a terminating 0 after a string at the first white space
235 void add_term(char *s)
236 {
237         while (*s && !isspace(*s)) s++;
238
239         *s = 0;         //terminate!
240 }
241
242 //returns ptr to string after '=' & white space, or NULL if no '='
243 //adds 0 after parm at first white space
244 char *get_value(char *buf)
245 {
246         char *t;
247
248         t = strchr(buf,'=')+1;
249
250         if (t) {
251                 while (*t && isspace(*t)) t++;
252
253                 if (*t)
254                         return t;
255         }
256
257         return NULL;            //error!
258 }
259
260 //reads a line, returns ptr to value of passed parm.  returns NULL if none
261 char *get_parm_value(char *parm,CFILE *f)
262 {
263         static char buf[80];
264
265         if (!cfgets(buf,80,f))
266                 return NULL;
267
268         if (istok(buf,parm))
269                 return get_value(buf);
270         else
271                 return NULL;
272 }
273
274 int ml_sort_func(mle *e0,mle *e1)
275 {
276         return stricmp(e0->mission_name,e1->mission_name);
277
278 }
279
280
281 //returns 1 if file read ok, else 0
282 int read_mission_file(mle *mission, char *filename, int location)
283 {
284         char filename2[100];
285         CFILE *mfile;
286
287         //printf("reading: %s\n", filename);
288
289         switch (location) {
290                 case ML_MISSIONDIR:
291                         strcpy(filename2,MISSION_DIR);
292                         break;
293
294                 default:
295                         Int3();         //fall through
296
297                 case ML_CURDIR:
298                         strcpy(filename2,"");
299                         break;
300         }
301         strcat(filename2,filename);
302
303         mfile = cfopen(filename2,"rb");
304
305         if (mfile) {
306                 char *p;
307                 char temp[PATH_MAX], *ext;
308
309                 strcpy(temp,filename);
310                 p = strrchr(temp, '/'); // get the filename at the end of the path
311                 if (!p)
312                         p = temp;
313                 else p++;
314                 
315                 if ((ext = strchr(p, '.')) == NULL)
316                         return 0;       //missing extension
317                 // look if it's .mn2 or .msn
318                 mission->descent_version = (ext[3] == '2') ? 2 : 1;
319                 *ext = 0;                       //kill extension
320
321                 mission->path = d_strdup(temp);
322                 mission->anarchy_only_flag = 0;
323                 mission->filename = mission->path + (p - temp);
324                 mission->location = location;
325
326                 p = get_parm_value("name",mfile);
327
328                 if (!p) {               //try enhanced mission
329                         cfseek(mfile,0,SEEK_SET);
330                         p = get_parm_value("xname",mfile);
331                 }
332
333                 if (!p) {       //try super-enhanced mission!
334                         cfseek(mfile,0,SEEK_SET);
335                         p = get_parm_value("zname",mfile);
336                 }
337
338                 if (p) {
339                         char *t;
340                         if ((t=strchr(p,';'))!=NULL)
341                                 *t=0;
342                         t = p + strlen(p)-1;
343                         while (isspace(*t))
344                                 *t-- = 0; // remove trailing whitespace
345                         if (strlen(p) > MISSION_NAME_LEN)
346                                 p[MISSION_NAME_LEN] = 0;
347                         strncpy(mission->mission_name, p, MISSION_NAME_LEN + 1);
348                 }
349                 else {
350                         cfclose(mfile);
351                         d_free(mission->path);
352                         return 0;
353                 }
354
355                 p = get_parm_value("type",mfile);
356
357                 //get mission type
358                 if (p)
359                         mission->anarchy_only_flag = istok(p,"anarchy");
360
361                 cfclose(mfile);
362
363                 return 1;
364         }
365
366         return 0;
367 }
368
369 void add_d1_builtin_mission_to_list(mle *mission)
370 {
371     int size;
372     
373         if (!cfexist("descent.hog"))
374                 return;
375
376         size = cfile_size("descent.hog");
377
378         switch (size) {
379         case D1_SHAREWARE_MISSION_HOGSIZE:
380         case D1_SHAREWARE_10_MISSION_HOGSIZE:
381         case D1_MAC_SHARE_MISSION_HOGSIZE:
382                 mission->filename = d_strdup(D1_MISSION_FILENAME);
383                 strcpy(mission->mission_name, D1_SHAREWARE_MISSION_NAME);
384                 mission->anarchy_only_flag = 0;
385                 break;
386         case D1_OEM_MISSION_HOGSIZE:
387         case D1_OEM_10_MISSION_HOGSIZE:
388                 mission->filename = d_strdup(D1_MISSION_FILENAME);
389                 strcpy(mission->mission_name, D1_OEM_MISSION_NAME);
390                 mission->anarchy_only_flag = 0;
391                 break;
392         default:
393                 Warning("Unknown D1 hogsize %d\n", size);
394                 Int3();
395                 // fall through
396         case D1_MISSION_HOGSIZE:
397         case D1_10_MISSION_HOGSIZE:
398         case D1_MAC_MISSION_HOGSIZE:
399                 mission->filename = d_strdup(D1_MISSION_FILENAME);
400                 strcpy(mission->mission_name, D1_MISSION_NAME);
401                 mission->anarchy_only_flag = 0;
402                 break;
403         }
404
405         mission->descent_version = 1;
406         mission->anarchy_only_flag = 0;
407         mission->builtin_hogsize = 0;
408         mission->path = mission->filename;
409         num_missions++;
410 }
411
412
413 void add_builtin_mission_to_list(mle *mission, char *name)
414 {
415     int size = cfile_size("descent2.hog");
416     
417         if (size == -1)
418                 size = cfile_size("d2demo.hog");
419
420         switch (size) {
421         case SHAREWARE_MISSION_HOGSIZE:
422         case MAC_SHARE_MISSION_HOGSIZE:
423                 mission->filename = d_strdup(SHAREWARE_MISSION_FILENAME);
424                 strcpy(mission->mission_name,SHAREWARE_MISSION_NAME);
425                 mission->anarchy_only_flag = 0;
426                 break;
427         case OEM_MISSION_HOGSIZE:
428                 mission->filename = d_strdup(OEM_MISSION_FILENAME);
429                 strcpy(mission->mission_name,OEM_MISSION_NAME);
430                 mission->anarchy_only_flag = 0;
431                 break;
432         default:
433                 Warning("Unknown hogsize %d, trying %s\n", size, FULL_MISSION_FILENAME ".mn2");
434                 Int3(); //fall through
435         case FULL_MISSION_HOGSIZE:
436         case FULL_10_MISSION_HOGSIZE:
437         case MAC_FULL_MISSION_HOGSIZE:
438                 if (!read_mission_file(mission, FULL_MISSION_FILENAME ".mn2", ML_CURDIR))
439                         Error("Could not find required mission file <%s>", FULL_MISSION_FILENAME ".mn2");
440         }
441
442         mission->path = mission->filename;
443         strcpy(name, mission->filename);
444     mission->builtin_hogsize = size;
445         mission->descent_version = 2;
446         mission->anarchy_only_flag = 0;
447         num_missions++;
448 }
449
450
451 void add_missions_to_list(mle *mission_list, char *path, char *rel_path, int anarchy_mode)
452 {
453         char **find, **i, *ext;
454
455         find = PHYSFS_enumerateFiles(path);
456
457         for (i = find; *i != NULL; i++)
458         {
459                 if (strlen(path) + strlen(*i) + 1 >= PATH_MAX)
460                         continue;       // path is too long
461
462                 strcat(rel_path, *i);
463                 if (PHYSFS_isDirectory(path))
464                 {
465                         strcat(rel_path, "/");
466                         add_missions_to_list(mission_list, path, rel_path, anarchy_mode);
467                         *(strrchr(path, '/')) = 0;
468                 }
469                 else if ((ext = strrchr(*i, '.')) && (!strnicmp(ext, ".msn", 4) || !strnicmp(ext, ".mn2", 4)))
470                         if (read_mission_file(&mission_list[num_missions], rel_path, ML_MISSIONDIR))
471                         {
472                                 if (anarchy_mode || !mission_list[num_missions].anarchy_only_flag)
473                                 {
474                                         mission_list[num_missions].builtin_hogsize = 0;
475                                         num_missions++;
476                                 }
477                                 else
478                                         d_free(mission_list[num_missions].path);
479                         }
480                 
481                 if (num_missions >= MAX_MISSIONS)
482                 {
483                         mprintf((0, "Warning: more missions than d2x can handle\n"));
484                         break;
485                 }
486
487                 (strrchr(path, '/'))[1] = 0;    // chop off the entry
488         }
489
490         PHYSFS_freeList(find);
491 }
492
493 /* move <mission_name> to <place> on mission list, increment <place> */
494 void promote (mle *mission_list, char * mission_name, int * top_place)
495 {
496         int i;
497         char name[FILENAME_LEN], * t;
498         strcpy(name, mission_name);
499         if ((t = strchr(name,'.')) != NULL)
500                 *t = 0; //kill extension
501         //printf("promoting: %s\n", name);
502         for (i = *top_place; i < num_missions; i++)
503                 if (!stricmp(mission_list[i].filename, name)) {
504                         //swap mission positions
505                         mle temp;
506
507                         temp = mission_list[*top_place];
508                         mission_list[*top_place] = mission_list[i];
509                         mission_list[i] = temp;
510                         ++(*top_place);
511                         break;
512                 }
513 }
514
515 void free_mission(void)
516 {
517     // May become more complex with the editor
518     if (Current_mission) {
519                 d_free(Current_mission->filename);
520         d_free(Current_mission);
521     }
522 }
523
524
525
526 //fills in the global list of missions.  Returns the number of missions
527 //in the list.  If anarchy_mode is set, then also add anarchy-only missions.
528
529 #if 0
530 extern char AltHogDir[];
531 extern char AltHogdir_initialized;
532 #endif
533
534 mle *build_mission_list(int anarchy_mode)
535 {
536         mle *mission_list;
537         int top_place;
538     char        builtin_mission_filename[FILENAME_LEN];
539         char    search_str[PATH_MAX] = MISSION_DIR;
540
541         //now search for levels on disk
542
543 //@@Took out this code because after this routine was called once for
544 //@@a list of single-player missions, a subsequent call for a list of
545 //@@anarchy missions would not scan again, and thus would not find the
546 //@@anarchy-only missions.  If we retain the minimum level of install,
547 //@@we may want to put the code back in, having it always scan for all
548 //@@missions, and have the code that uses it sort out the ones it wants.
549 //@@    if (num_missions != -1) {
550 //@@            if (Current_mission_num != 0)
551 //@@                    load_mission(0);                                //set built-in mission as default
552 //@@            return num_missions;
553 //@@    }
554
555         MALLOC(mission_list, mle, MAX_MISSIONS);
556         num_missions = 0;
557         
558         add_builtin_mission_to_list(mission_list + num_missions, builtin_mission_filename);  //read built-in first
559         add_d1_builtin_mission_to_list(mission_list + num_missions);
560         add_missions_to_list(mission_list, search_str, search_str + strlen(search_str), anarchy_mode);
561         
562         // move original missions (in story-chronological order)
563         // to top of mission list
564         top_place = 0;
565         promote(mission_list, "descent", &top_place); // original descent 1 mission
566         promote(mission_list, builtin_mission_filename, &top_place); // d2 or d2demo
567         promote(mission_list, "d2x", &top_place); // vertigo
568
569         if (num_missions > top_place)
570                 qsort(&mission_list[top_place],
571                       num_missions - top_place,
572                       sizeof(*mission_list),
573                                 (int (*)( const void *, const void * ))ml_sort_func);
574
575
576         if (num_missions > top_place)
577                 qsort(&mission_list[top_place],
578                       num_missions - top_place,
579                       sizeof(*mission_list),
580                       (int (*)( const void *, const void * ))ml_sort_func);
581
582         //load_mission(0);   //set built-in mission as default
583
584     atexit(free_mission);
585
586         return mission_list;
587 }
588
589 void free_mission_list(mle *mission_list)
590 {
591         int i;
592
593         for (i = 0; i < num_missions; i++)
594                 d_free(mission_list[i].path);
595         
596         d_free(mission_list);
597         num_missions = 0;
598 }
599
600 void init_extra_robot_movie(char *filename);
601
602 //values for built-in mission
603
604 //loads the specfied mission from the mission list.
605 //build_mission_list() must have been called.
606 //Returns true if mission loaded ok, else false.
607 int load_mission(mle *mission)
608 {
609         CFILE *mfile;
610         char buf[PATH_MAX], *v;
611     int found_hogfile;
612
613     if (Current_mission)
614         free_mission();
615     Current_mission = d_malloc(sizeof(Mission));
616     if (!Current_mission) return 0;
617     *(mle *) Current_mission = *mission;
618         Current_mission->filename = d_strdup(mission->filename); // don't want to lose it
619
620     // for Descent 1 missions, load descent.hog
621     if (EMULATING_D1) {
622         if (!cfile_init("descent.hog"))
623             Warning("descent.hog not available, this mission may be missing some files required for briefings and exit sequence\n");
624         if (!stricmp(Current_mission_filename, D1_MISSION_FILENAME))
625             return load_mission_d1();
626     }
627
628     if (PLAYING_BUILTIN_MISSION) {
629                 switch (Current_mission->builtin_hogsize) {
630                 case SHAREWARE_MISSION_HOGSIZE:
631                 case MAC_SHARE_MISSION_HOGSIZE:
632                         return load_mission_shareware();
633                         break;
634                 case OEM_MISSION_HOGSIZE:
635                         return load_mission_oem();
636                         break;
637                 default:
638                         Int3(); // fall through
639                 case FULL_MISSION_HOGSIZE:
640                 case FULL_10_MISSION_HOGSIZE:
641                 case MAC_FULL_MISSION_HOGSIZE:
642                         // continue on... (use d2.mn2 from hogfile)
643                         break;
644                 }
645     }
646
647         mprintf(( 0, "Loading mission %s\n", Current_mission_filename ));
648
649         //read mission from file
650
651         switch (mission->location) {
652         case ML_MISSIONDIR:
653                 strcpy(buf,MISSION_DIR);
654                 break;
655         default:
656                 Int3();                                                 //fall through
657         case ML_CURDIR:
658                 strcpy(buf,"");
659                 break;
660         }
661         strcat(buf, mission->path);
662         if (mission->descent_version == 2)
663                 strcat(buf,".mn2");
664         else
665                 strcat(buf,".msn");
666
667         PHYSFSEXT_locateCorrectCase(buf);
668
669         mfile = cfopen(buf,"rb");
670         if (mfile == NULL) {
671         free_mission();
672                 return 0;               //error!
673         }
674
675     //for non-builtin missions, load HOG
676     if (!PLAYING_BUILTIN_MISSION) {
677
678         strcpy(buf+strlen(buf)-4,".hog");               //change extension
679
680                 PHYSFSEXT_locateCorrectCase(buf);
681
682                 found_hogfile = cfile_init(buf);
683
684 #ifdef RELEASE                          //for release, require mission to be in hogfile
685         if (! found_hogfile) {
686             cfclose(mfile);
687             free_mission();
688             return 0;
689         }
690 #endif
691     }
692
693     //init vars
694         Last_level = 0;
695         Last_secret_level = 0;
696         Briefing_text_filename[0] = 0;
697         Ending_text_filename[0] = 0;
698
699         while (cfgets(buf,80,mfile)) {
700
701                 if (istok(buf,"name")) {
702                         Current_mission->enhanced = 0;
703                         continue;                                               //already have name, go to next line
704                 }
705                 if (istok(buf,"xname")) {
706                         Current_mission->enhanced = 1;
707                         continue;                                               //already have name, go to next line
708                 }
709                 if (istok(buf,"zname")) {
710                         Current_mission->enhanced = 2;
711                         continue;                                               //already have name, go to next line
712                 }
713                 else if (istok(buf,"type"))
714                         continue;                                               //already have name, go to next line
715                 else if (istok(buf,"hog")) {
716                         char    *bufp = buf;
717
718                         while (*(bufp++) != '=')
719                                 ;
720
721                         if (*bufp == ' ')
722                                 while (*(++bufp) == ' ')
723                                         ;
724
725                         cfile_init(bufp);
726                         mprintf((0, "Hog file override = [%s]\n", bufp));
727                 }
728                 else if (istok(buf,"briefing")) {
729                         if ((v = get_value(buf)) != NULL) {
730                                 add_term(v);
731                                 if (strlen(v) < 13)
732                                         strcpy(Briefing_text_filename,v);
733                         }
734                 }
735                 else if (istok(buf,"ending")) {
736                         if ((v = get_value(buf)) != NULL) {
737                                 add_term(v);
738                                 if (strlen(v) < 13)
739                                         strcpy(Ending_text_filename,v);
740                         }
741                 }
742                 else if (istok(buf,"num_levels")) {
743
744                         if ((v=get_value(buf))!=NULL) {
745                                 int n_levels,i;
746
747                                 n_levels = atoi(v);
748
749                                 for (i=0;i<n_levels && cfgets(buf,80,mfile);i++) {
750
751                                         add_term(buf);
752                                         if (strlen(buf) <= 12) {
753                                                 strcpy(Level_names[i],buf);
754                                                 Last_level++;
755                                         }
756                                         else
757                                                 break;
758                                 }
759
760                         }
761                 }
762                 else if (istok(buf,"num_secrets")) {
763                         if ((v=get_value(buf))!=NULL) {
764                                 int i;
765
766                                 N_secret_levels = atoi(v);
767
768                                 Assert(N_secret_levels <= MAX_SECRET_LEVELS_PER_MISSION);
769
770                                 for (i=0;i<N_secret_levels && cfgets(buf,80,mfile);i++) {
771                                         char *t;
772
773                                         
774                                         if ((t=strchr(buf,','))!=NULL) *t++=0;
775                                         else
776                                                 break;
777
778                                         add_term(buf);
779                                         if (strlen(buf) <= 12) {
780                                                 strcpy(Secret_level_names[i],buf);
781                                                 Secret_level_table[i] = atoi(t);
782                                                 if (Secret_level_table[i]<1 || Secret_level_table[i]>Last_level)
783                                                         break;
784                                                 Last_secret_level--;
785                                         }
786                                         else
787                                                 break;
788                                 }
789
790                         }
791                 }
792
793         }
794
795         cfclose(mfile);
796
797         if (Last_level <= 0) {
798                 free_mission();         //no valid mission loaded
799                 return 0;
800         }
801
802         if (Current_mission->enhanced) {
803                 char t[50];
804                 extern void bm_read_extra_robots();
805                 sprintf(t,"%s.ham",Current_mission_filename);
806                 bm_read_extra_robots(t, Current_mission->enhanced);
807                 strncpy(t,Current_mission_filename,6);
808                 init_extra_robot_movie(t);
809         }
810
811         return 1;
812 }
813
814 //loads the named mission if exists.
815 //Returns true if mission loaded ok, else false.
816 int load_mission_by_name(char *mission_name)
817 {
818         int i;
819         mle *mission_list = build_mission_list(1);
820         bool found = 0;
821
822         for (i = 0; i < num_missions; i++)
823                 if (!stricmp(mission_name, mission_list[i].filename))
824                         found = load_mission(mission_list + i);
825
826         free_mission_list(mission_list);
827         return found;
828 }
829
830 int select_mission(int anarchy_mode, char *message)
831 {
832     mle *mission_list = build_mission_list(anarchy_mode);
833         int new_mission_num;
834
835     if (num_missions <= 1) {
836         new_mission_num = load_mission(mission_list) ? 0 : -1;
837     } else {
838         int i, default_mission;
839         char * m[MAX_MISSIONS];
840
841         default_mission = 0;
842         for (i = 0; i < num_missions; i++) {
843             m[i] = mission_list[i].mission_name;
844             if ( !stricmp( m[i], config_last_mission.string ) )
845                 default_mission = i;
846         }
847
848         new_mission_num = newmenu_listbox1( message, num_missions, m, 1, default_mission, NULL );
849
850         if (new_mission_num >= 0) {
851                         // Chose a mission
852                         cvar_set_cvar( &config_last_mission, m[new_mission_num] );
853         
854                         if (!load_mission(mission_list + new_mission_num)) {
855                                 nm_messagebox( NULL, 1, TXT_OK, TXT_MISSION_ERROR);
856                                 new_mission_num = -1;
857                         }
858                 }
859     }
860
861         free_mission_list(mission_list);
862     return (new_mission_num >= 0);
863 }
864
865
866 //set a new highest level for player for this mission
867 int mission_write_config(void)
868 {
869         char filename[FILENAME_LEN+15];
870         PHYSFS_file *file;
871
872         PHYSFS_mkdir(MISSION_DIR);
873
874         sprintf(filename, MISSION_DIR "%s.cfg", Current_mission_filename);
875         file = PHYSFSX_openWriteBuffered(filename);
876
877         if (!file)
878         {
879                 nm_messagebox(NULL, 1, TXT_OK, "Cannot open mission config file");
880                 return -1;
881         }
882
883         PHYSFSX_printf(file, "%s=%d\n", Player_highest_level.name, Current_level_num);
884
885         PHYSFS_close(file);
886
887         return 0;
888 }
889
890
891 //gets the player's highest level from the file for this mission
892 int mission_read_config(void)
893 {
894         cvar_setint(&Player_highest_level, 0);
895
896         cmd_appendf("exec " MISSION_DIR "%s.cfg", Current_mission_filename);
897         cmd_queue_process();
898
899         return 0;
900 }