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