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