]> icculus.org git repositories - btb/d2x.git/blob - main/mission.c
set builtin_hogsize for non-built-in missions
[btb/d2x.git] / main / mission.c
1 /* $Id: mission.c,v 1.33 2004-12-02 16:41:54 schaffner 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                                         Mission_list[*count].builtin_hogsize = 0;
462                                 }
463                 if (*count >= MAX_MISSIONS)
464                 {
465                         mprintf((0, "Warning: more missions than d2x can handle\n"));
466                         break;
467                 }
468         }
469
470         PHYSFS_freeList(find);
471 }
472
473 /* move <mission_name> to <place> on mission list, increment <place> */
474 void promote (char * mission_name, int * top_place, int num_missions)
475 {
476         int i;
477         char name[FILENAME_LEN], * t;
478         strcpy(name, mission_name);
479         if ((t = strchr(name,'.')) != NULL)
480                 *t = 0; //kill extension
481         //printf("promoting: %s\n", name);
482         for (i = *top_place; i < num_missions; i++)
483                 if (!stricmp(Mission_list[i].filename, name)) {
484                         //swap mission positions
485                         mle temp;
486
487                         temp = Mission_list[*top_place];
488                         Mission_list[*top_place] = Mission_list[i];
489                         Mission_list[i] = temp;
490                         ++(*top_place);
491                         break;
492                 }
493 }
494
495 void free_mission(void)
496 {
497     // May become more complex with the editor
498     if (Current_mission) {
499         d_free(Current_mission);
500     }
501 }
502
503
504
505 //fills in the global list of missions.  Returns the number of missions
506 //in the list.  If anarchy_mode is set, then also add anarchy-only missions.
507
508 extern char CDROM_dir[];
509 #if 0
510 extern char AltHogDir[];
511 extern char AltHogdir_initialized;
512 #endif
513
514 int build_mission_list(int anarchy_mode)
515 {
516         static int num_missions=-1;
517         int count = 0;
518         int top_place;
519     char        builtin_mission_filename[FILENAME_LEN];
520
521         //now search for levels on disk
522
523 //@@Took out this code because after this routine was called once for
524 //@@a list of single-player missions, a subsequent call for a list of
525 //@@anarchy missions would not scan again, and thus would not find the
526 //@@anarchy-only missions.  If we retain the minimum level of install,
527 //@@we may want to put the code back in, having it always scan for all
528 //@@missions, and have the code that uses it sort out the ones it wants.
529 //@@    if (num_missions != -1) {
530 //@@            if (Current_mission_num != 0)
531 //@@                    load_mission(0);                                //set built-in mission as default
532 //@@            return num_missions;
533 //@@    }
534
535         add_builtin_mission_to_list(&count, builtin_mission_filename);  //read built-in first
536         add_d1_builtin_mission_to_list(&count);
537         add_missions_to_list(&count, anarchy_mode);
538         
539         // move original missions (in story-chronological order)
540         // to top of mission list
541         top_place = 0;
542         promote("descent", &top_place, count); // original descent 1 mission
543         promote(builtin_mission_filename, &top_place, count); // d2 or d2demo
544         promote("d2x", &top_place, count); // vertigo
545
546         if (count > top_place)
547                 qsort(&Mission_list[top_place],
548                       count - top_place,
549                       sizeof(*Mission_list),
550                                 (int (*)( const void *, const void * ))ml_sort_func);
551
552
553         if (count > top_place)
554                 qsort(&Mission_list[top_place],
555                       count - top_place,
556                       sizeof(*Mission_list),
557                       (int (*)( const void *, const void * ))ml_sort_func);
558
559         //load_mission(0);   //set built-in mission as default
560
561         num_missions = count;
562
563     atexit(free_mission);
564
565         return count;
566 }
567
568 void init_extra_robot_movie(char *filename);
569
570 //values for built-in mission
571
572 //loads the specfied mission from the mission list.
573 //build_mission_list() must have been called.
574 //Returns true if mission loaded ok, else false.
575 int load_mission(int mission_num)
576 {
577         CFILE *mfile;
578         char buf[80], *v;
579     int found_hogfile;
580
581     if (Current_mission)
582         free_mission();
583     Current_mission = d_malloc(sizeof(Mission));
584     if (!Current_mission) return 0;
585     *(mle *) Current_mission = Mission_list[mission_num];
586
587     // for Descent 1 missions, load descent.hog
588     if (EMULATING_D1) {
589         if (!cfile_init("descent.hog"))
590             Warning("descent.hog not available, this mission may be missing some files required for briefings and exit sequence\n");
591         if (!stricmp(Current_mission_filename, D1_MISSION_FILENAME))
592             return load_mission_d1();
593     }
594
595     if (PLAYING_BUILTIN_MISSION) {
596                 switch (Current_mission->builtin_hogsize) {
597                 case SHAREWARE_MISSION_HOGSIZE:
598                 case MAC_SHARE_MISSION_HOGSIZE:
599                         return load_mission_shareware();
600                         break;
601                 case OEM_MISSION_HOGSIZE:
602                         return load_mission_oem();
603                         break;
604                 default:
605                         Int3(); // fall through
606                 case FULL_MISSION_HOGSIZE:
607                 case FULL_10_MISSION_HOGSIZE:
608                 case MAC_FULL_MISSION_HOGSIZE:
609                         // continue on... (use d2.mn2 from hogfile)
610                         break;
611                 }
612     }
613
614         mprintf(( 0, "Loading mission %s\n", Current_mission_filename ));
615
616         //read mission from file
617
618         switch (Mission_list[mission_num].location) {
619         case ML_MISSIONDIR:
620                 strcpy(buf,MISSION_DIR);
621                 break;
622         case ML_CDROM:
623                 strcpy(buf,CDROM_dir);
624                 break;
625         default:
626                 Int3();                                                 //fall through
627         case ML_CURDIR:
628                 strcpy(buf,"");
629                 break;
630         }
631         strcat(buf,Mission_list[mission_num].filename);
632         if (Mission_list[mission_num].descent_version == 2)
633                 strcat(buf,".mn2");
634         else
635                 strcat(buf,".msn");
636
637         PHYSFSEXT_locateCorrectCase(buf);
638
639         mfile = cfopen(buf,"rb");
640         if (mfile == NULL) {
641         free_mission();
642                 return 0;               //error!
643         }
644
645     //for non-builtin missions, load HOG
646     if (!PLAYING_BUILTIN_MISSION) {
647
648         strcpy(buf+strlen(buf)-4,".hog");               //change extension
649
650                 PHYSFSEXT_locateCorrectCase(buf);
651
652                 found_hogfile = cfile_init(buf);
653
654 #ifdef RELEASE                          //for release, require mission to be in hogfile
655         if (! found_hogfile) {
656             cfclose(mfile);
657             free_mission();
658             return 0;
659         }
660 #endif
661     }
662
663     //init vars
664         Last_level = 0;
665         Last_secret_level = 0;
666         Briefing_text_filename[0] = 0;
667         Ending_text_filename[0] = 0;
668
669         while (cfgets(buf,80,mfile)) {
670
671                 if (istok(buf,"name")) {
672                         Current_mission->enhanced = 0;
673                         continue;                                               //already have name, go to next line
674                 }
675                 if (istok(buf,"xname")) {
676                         Current_mission->enhanced = 1;
677                         continue;                                               //already have name, go to next line
678                 }
679                 if (istok(buf,"zname")) {
680                         Current_mission->enhanced = 2;
681                         continue;                                               //already have name, go to next line
682                 }
683                 else if (istok(buf,"type"))
684                         continue;                                               //already have name, go to next line
685                 else if (istok(buf,"hog")) {
686                         char    *bufp = buf;
687
688                         while (*(bufp++) != '=')
689                                 ;
690
691                         if (*bufp == ' ')
692                                 while (*(++bufp) == ' ')
693                                         ;
694
695                         cfile_init(bufp);
696                         mprintf((0, "Hog file override = [%s]\n", bufp));
697                 }
698                 else if (istok(buf,"briefing")) {
699                         if ((v = get_value(buf)) != NULL) {
700                                 add_term(v);
701                                 if (strlen(v) < 13)
702                                         strcpy(Briefing_text_filename,v);
703                         }
704                 }
705                 else if (istok(buf,"ending")) {
706                         if ((v = get_value(buf)) != NULL) {
707                                 add_term(v);
708                                 if (strlen(v) < 13)
709                                         strcpy(Ending_text_filename,v);
710                         }
711                 }
712                 else if (istok(buf,"num_levels")) {
713
714                         if ((v=get_value(buf))!=NULL) {
715                                 int n_levels,i;
716
717                                 n_levels = atoi(v);
718
719                                 for (i=0;i<n_levels && cfgets(buf,80,mfile);i++) {
720
721                                         add_term(buf);
722                                         if (strlen(buf) <= 12) {
723                                                 strcpy(Level_names[i],buf);
724                                                 Last_level++;
725                                         }
726                                         else
727                                                 break;
728                                 }
729
730                         }
731                 }
732                 else if (istok(buf,"num_secrets")) {
733                         if ((v=get_value(buf))!=NULL) {
734                                 int i;
735
736                                 N_secret_levels = atoi(v);
737
738                                 Assert(N_secret_levels <= MAX_SECRET_LEVELS_PER_MISSION);
739
740                                 for (i=0;i<N_secret_levels && cfgets(buf,80,mfile);i++) {
741                                         char *t;
742
743                                         
744                                         if ((t=strchr(buf,','))!=NULL) *t++=0;
745                                         else
746                                                 break;
747
748                                         add_term(buf);
749                                         if (strlen(buf) <= 12) {
750                                                 strcpy(Secret_level_names[i],buf);
751                                                 Secret_level_table[i] = atoi(t);
752                                                 if (Secret_level_table[i]<1 || Secret_level_table[i]>Last_level)
753                                                         break;
754                                                 Last_secret_level--;
755                                         }
756                                         else
757                                                 break;
758                                 }
759
760                         }
761                 }
762
763         }
764
765         cfclose(mfile);
766
767         if (Last_level <= 0) {
768                 free_mission();         //no valid mission loaded
769                 return 0;
770         }
771
772         if (Current_mission->enhanced) {
773                 char t[50];
774                 extern void bm_read_extra_robots();
775                 sprintf(t,"%s.ham",Current_mission_filename);
776                 bm_read_extra_robots(t, Current_mission->enhanced);
777                 strncpy(t,Current_mission_filename,6);
778                 init_extra_robot_movie(t);
779         }
780
781         return 1;
782 }
783
784 //loads the named mission if exists.
785 //Returns true if mission loaded ok, else false.
786 int load_mission_by_name(char *mission_name)
787 {
788         int n,i;
789
790         n = build_mission_list(1);
791
792         for (i=0;i<n;i++)
793                 if (!stricmp(mission_name,Mission_list[i].filename))
794                         return load_mission(i);
795
796         return 0;               //couldn't find mission
797 }
798
799 int select_mission(int anarchy_mode, char *message)
800 {
801     int n_missions;
802     
803     n_missions = build_mission_list(anarchy_mode);
804
805     if (n_missions <= 1) {
806         load_mission(0);
807     } else {
808         int new_mission_num,i, default_mission;
809         char * m[MAX_MISSIONS];
810
811         default_mission = 0;
812         for (i=0;i<n_missions;i++) {
813             m[i] = Mission_list[i].mission_name;
814             if ( !stricmp( m[i], config_last_mission ) )
815                 default_mission = i;
816         }
817
818         new_mission_num = newmenu_listbox1( message, n_missions, m, 1, default_mission, NULL );
819
820         if (new_mission_num == -1)
821             return 0;         //abort!
822
823         strcpy(config_last_mission, m[new_mission_num]  );
824
825         if (!load_mission(new_mission_num)) {
826             nm_messagebox( NULL, 1, TXT_OK, TXT_MISSION_ERROR);
827             return 0;
828         }
829     }
830
831     return 1;
832 }