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