]> icculus.org git repositories - btb/d2x.git/blob - main/mission.c
move all mission stuff to mission.{c,h}, make mission structre memory allocated
[btb/d2x.git] / main / mission.c
1 /* $Id: mission.c,v 1.29 2004-10-24 12:46:49 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 "findfile.h"
43 #include "config.h"
44 #include "newmenu.h"
45 #include "text.h"
46 #include "u_mem.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].descent_version = 1;
405         Mission_list[*count].anarchy_only_flag = 0;
406         ++(*count);
407 }
408
409
410 void add_builtin_mission_to_list(int *count, char *name)
411 {
412     int size = cfile_size("descent2.hog");
413     
414         if (size == -1)
415                 size = cfile_size("d2demo.hog");
416
417         switch (size) {
418         case SHAREWARE_MISSION_HOGSIZE:
419         case MAC_SHARE_MISSION_HOGSIZE:
420                 strcpy(Mission_list[*count].filename,SHAREWARE_MISSION_FILENAME);
421                 strcpy(Mission_list[*count].mission_name,SHAREWARE_MISSION_NAME);
422                 Mission_list[*count].anarchy_only_flag = 0;
423                 break;
424         case OEM_MISSION_HOGSIZE:
425                 strcpy(Mission_list[*count].filename,OEM_MISSION_FILENAME);
426                 strcpy(Mission_list[*count].mission_name,OEM_MISSION_NAME);
427                 Mission_list[*count].anarchy_only_flag = 0;
428                 break;
429         default:
430                 Warning("Unknown hogsize %d, trying %s\n", size, FULL_MISSION_FILENAME ".mn2");
431                 Int3(); //fall through
432         case FULL_MISSION_HOGSIZE:
433         case FULL_10_MISSION_HOGSIZE:
434         case MAC_FULL_MISSION_HOGSIZE:
435                 if (!read_mission_file(FULL_MISSION_FILENAME ".mn2", 0, ML_CURDIR))
436                         Error("Could not find required mission file <%s>", FULL_MISSION_FILENAME ".mn2");
437         }
438
439         strcpy(name, Mission_list[*count].filename);
440     Mission_list[*count].builtin_hogsize = size;
441         Mission_list[*count].descent_version = 2;
442         Mission_list[*count].anarchy_only_flag = 0;
443         ++(*count);
444 }
445
446
447 void add_missions_to_list(char *search_name, int *count, int anarchy_mode)
448 {
449         FILEFINDSTRUCT find;
450         if( !FileFindFirst( search_name, &find ) ) {
451                 do      {
452                         if (read_mission_file( find.name, *count, ML_MISSIONDIR )) {
453
454                                 if (anarchy_mode || !Mission_list[*count].anarchy_only_flag)
455                                         ++(*count);
456                         }
457
458                 } while( !FileFindNext( &find ) && *count < MAX_MISSIONS);
459                 FileFindClose();
460                 if (*count >= MAX_MISSIONS)
461                         mprintf((0, "Warning: more missions than d2x can handle\n"));
462         }
463 }
464
465 /* move <mission_name> to <place> on mission list, increment <place> */
466 void promote (char * mission_name, int * top_place, int num_missions)
467 {
468         int i;
469         char name[FILENAME_LEN], * t;
470         strcpy(name, mission_name);
471         if ((t = strchr(name,'.')) != NULL)
472                 *t = 0; //kill extension
473         //printf("promoting: %s\n", name);
474         for (i = *top_place; i < num_missions; i++)
475                 if (!stricmp(Mission_list[i].filename, name)) {
476                         //swap mission positions
477                         mle temp;
478
479                         temp = Mission_list[*top_place];
480                         Mission_list[*top_place] = Mission_list[i];
481                         Mission_list[i] = temp;
482                         ++(*top_place);
483                         break;
484                 }
485 }
486
487 void free_mission(void)
488 {
489     // May become more complex with the editor
490     if (Current_mission) {
491         d_free(Current_mission);
492     }
493 }
494
495
496
497 //fills in the global list of missions.  Returns the number of missions
498 //in the list.  If anarchy_mode is set, then also add anarchy-only missions.
499
500 extern char CDROM_dir[];
501 extern char AltHogDir[];
502 extern char AltHogdir_initialized;
503
504 int build_mission_list(int anarchy_mode)
505 {
506         static int num_missions=-1;
507         int count = 0;
508         int top_place;
509     char        builtin_mission_filename[FILENAME_LEN];
510
511         //now search for levels on disk
512
513 //@@Took out this code because after this routine was called once for
514 //@@a list of single-player missions, a subsequent call for a list of
515 //@@anarchy missions would not scan again, and thus would not find the
516 //@@anarchy-only missions.  If we retain the minimum level of install,
517 //@@we may want to put the code back in, having it always scan for all
518 //@@missions, and have the code that uses it sort out the ones it wants.
519 //@@    if (num_missions != -1) {
520 //@@            if (Current_mission_num != 0)
521 //@@                    load_mission(0);                                //set built-in mission as default
522 //@@            return num_missions;
523 //@@    }
524
525         add_builtin_mission_to_list(&count, builtin_mission_filename);  //read built-in first
526         add_d1_builtin_mission_to_list(&count);
527         add_missions_to_list(MISSION_DIR "*.mn2", &count, anarchy_mode);
528         add_missions_to_list(MISSION_DIR "*.msn", &count, anarchy_mode);
529
530         if (AltHogdir_initialized) {
531                 char search_name[PATH_MAX + 5];
532                 strcpy(search_name, AltHogDir);
533                 strcat(search_name, "/" MISSION_DIR "*.mn2");
534                 add_missions_to_list(search_name, &count, anarchy_mode);
535                 strcpy(search_name, AltHogDir);
536                 strcat(search_name, "/" MISSION_DIR "*.msn");
537                 add_missions_to_list(search_name, &count, anarchy_mode);
538         }
539
540         // move original missions (in story-chronological order)
541         // to top of mission list
542         top_place = 0;
543         promote("descent", &top_place, count); // original descent 1 mission
544         promote(builtin_mission_filename, &top_place, count); // d2 or d2demo
545         promote("d2x", &top_place, count); // vertigo
546
547         if (count > top_place)
548                 qsort(&Mission_list[top_place],
549                       count - top_place,
550                       sizeof(*Mission_list),
551                                 (int (*)( const void *, const void * ))ml_sort_func);
552
553
554         if (count > top_place)
555                 qsort(&Mission_list[top_place],
556                       count - top_place,
557                       sizeof(*Mission_list),
558                       (int (*)( const void *, const void * ))ml_sort_func);
559
560         //load_mission(0);   //set built-in mission as default
561
562         num_missions = count;
563
564     atexit(free_mission);
565
566         return count;
567 }
568
569 void init_extra_robot_movie(char *filename);
570
571 //values for built-in mission
572
573 //loads the specfied mission from the mission list.
574 //build_mission_list() must have been called.
575 //Returns true if mission loaded ok, else false.
576 int load_mission(int mission_num)
577 {
578         CFILE *mfile;
579         char buf[80], *v;
580     int found_hogfile;
581         int enhanced_mission = 0;
582
583     if (Current_mission)
584         free_mission();
585     Current_mission = d_malloc(sizeof(Mission));
586     if (!Current_mission) return 0;
587     *(mle *) Current_mission = Mission_list[mission_num];
588
589     // for Descent 1 missions, load descent.hog
590     if (EMULATING_D1) {
591         if (!cfile_use_descent1_hogfile("descent.hog"))
592             Warning("descent.hog not available, this mission may be missing some files required for briefings and exit sequence\n");
593         if (!stricmp(Current_mission_filename, D1_MISSION_FILENAME))
594             return load_mission_d1();
595     }
596
597     if (PLAYING_BUILTIN_MISSION) {
598                 switch (Current_mission->builtin_hogsize) {
599                 case SHAREWARE_MISSION_HOGSIZE:
600                 case MAC_SHARE_MISSION_HOGSIZE:
601                         return load_mission_shareware();
602                         break;
603                 case OEM_MISSION_HOGSIZE:
604                         return load_mission_oem();
605                         break;
606                 default:
607                         Int3(); // fall through
608                 case FULL_MISSION_HOGSIZE:
609                 case FULL_10_MISSION_HOGSIZE:
610                 case MAC_FULL_MISSION_HOGSIZE:
611                         // continue on... (use d2.mn2 from hogfile)
612                         break;
613                 }
614     }
615
616         mprintf(( 0, "Loading mission %s\n", Current_mission_filename ));
617
618         //read mission from file
619
620         switch (Mission_list[mission_num].location) {
621         case ML_MISSIONDIR:
622                 strcpy(buf,MISSION_DIR);
623                 break;
624         case ML_CDROM:
625                 strcpy(buf,CDROM_dir);
626                 break;
627         default:
628                 Int3();                                                 //fall through
629         case ML_CURDIR:
630                 strcpy(buf,"");
631                 break;
632         }
633         strcat(buf,Mission_list[mission_num].filename);
634         if (Mission_list[mission_num].descent_version == 2)
635                 strcat(buf,".mn2");
636         else
637                 strcat(buf,".msn");
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         found_hogfile = cfile_use_alternate_hogfile(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                         continue;                                               //already have name, go to next line
671                 if (istok(buf,"xname")) {
672                         enhanced_mission = 1;
673                         continue;                                               //already have name, go to next line
674                 }
675                 if (istok(buf,"zname")) {
676                         enhanced_mission = 2;
677                         continue;                                               //already have name, go to next line
678                 }
679                 else if (istok(buf,"type"))
680                         continue;                                               //already have name, go to next line
681                 else if (istok(buf,"hog")) {
682                         char    *bufp = buf;
683
684                         while (*(bufp++) != '=')
685                                 ;
686
687                         if (*bufp == ' ')
688                                 while (*(++bufp) == ' ')
689                                         ;
690
691                         cfile_use_alternate_hogfile(bufp);
692                         mprintf((0, "Hog file override = [%s]\n", bufp));
693                 }
694                 else if (istok(buf,"briefing")) {
695                         if ((v = get_value(buf)) != NULL) {
696                                 add_term(v);
697                                 if (strlen(v) < 13)
698                                         strcpy(Briefing_text_filename,v);
699                         }
700                 }
701                 else if (istok(buf,"ending")) {
702                         if ((v = get_value(buf)) != NULL) {
703                                 add_term(v);
704                                 if (strlen(v) < 13)
705                                         strcpy(Ending_text_filename,v);
706                         }
707                 }
708                 else if (istok(buf,"num_levels")) {
709
710                         if ((v=get_value(buf))!=NULL) {
711                                 int n_levels,i;
712
713                                 n_levels = atoi(v);
714
715                                 for (i=0;i<n_levels && cfgets(buf,80,mfile);i++) {
716
717                                         add_term(buf);
718                                         if (strlen(buf) <= 12) {
719                                                 strcpy(Level_names[i],buf);
720                                                 Last_level++;
721                                         }
722                                         else
723                                                 break;
724                                 }
725
726                         }
727                 }
728                 else if (istok(buf,"num_secrets")) {
729                         if ((v=get_value(buf))!=NULL) {
730                                 int i;
731
732                                 N_secret_levels = atoi(v);
733
734                                 Assert(N_secret_levels <= MAX_SECRET_LEVELS_PER_MISSION);
735
736                                 for (i=0;i<N_secret_levels && cfgets(buf,80,mfile);i++) {
737                                         char *t;
738
739                                         
740                                         if ((t=strchr(buf,','))!=NULL) *t++=0;
741                                         else
742                                                 break;
743
744                                         add_term(buf);
745                                         if (strlen(buf) <= 12) {
746                                                 strcpy(Secret_level_names[i],buf);
747                                                 Secret_level_table[i] = atoi(t);
748                                                 if (Secret_level_table[i]<1 || Secret_level_table[i]>Last_level)
749                                                         break;
750                                                 Last_secret_level--;
751                                         }
752                                         else
753                                                 break;
754                                 }
755
756                         }
757                 }
758
759         }
760
761         cfclose(mfile);
762
763         if (Last_level <= 0) {
764                 free_mission();         //no valid mission loaded
765                 return 0;
766         }
767
768         if (enhanced_mission) {
769                 char t[50];
770                 extern void bm_read_extra_robots();
771                 sprintf(t,"%s.ham",Current_mission_filename);
772                 bm_read_extra_robots(t,enhanced_mission);
773                 strncpy(t,Current_mission_filename,6);
774                 strcat(t,"-l.mvl");
775                 init_extra_robot_movie(t);
776         }
777
778         return 1;
779 }
780
781 //loads the named mission if exists.
782 //Returns true if mission loaded ok, else false.
783 int load_mission_by_name(char *mission_name)
784 {
785         int n,i;
786
787         n = build_mission_list(1);
788
789         for (i=0;i<n;i++)
790                 if (!stricmp(mission_name,Mission_list[i].filename))
791                         return load_mission(i);
792
793         return 0;               //couldn't find mission
794 }
795
796 int select_mission(int anarchy_mode, char *message)
797 {
798     int n_missions;
799     
800     n_missions = build_mission_list(anarchy_mode);
801
802     if (n_missions <= 1) {
803         load_mission(0);
804     } else {
805         int new_mission_num,i, default_mission;
806         char * m[MAX_MISSIONS];
807
808         default_mission = 0;
809         for (i=0;i<n_missions;i++) {
810             m[i] = Mission_list[i].mission_name;
811             if ( !stricmp( m[i], config_last_mission ) )
812                 default_mission = i;
813         }
814
815         new_mission_num = newmenu_listbox1( message, n_missions, m, 1, default_mission, NULL );
816
817         if (new_mission_num == -1)
818             return 0;         //abort!
819
820         strcpy(config_last_mission, m[new_mission_num]  );
821
822         if (!load_mission(new_mission_num)) {
823             nm_messagebox( NULL, 1, TXT_OK, TXT_MISSION_ERROR);
824             return 0;
825         }
826     }
827
828     return 1;
829 }