]> icculus.org git repositories - btb/d2x.git/blob - main/mission.c
remove rcs tags
[btb/d2x.git] / main / mission.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 /*
15  *
16  * Code to handle multiple missions
17  *
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <conf.h>
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <limits.h>
29
30 #include "pstypes.h"
31 #include "cfile.h"
32
33 #include "strutil.h"
34 #include "inferno.h"
35 #include "mission.h"
36 #include "gameseq.h"
37 #include "titles.h"
38 #include "songs.h"
39 #include "mono.h"
40 #include "error.h"
41 #include "config.h"
42 #include "newmenu.h"
43 #include "text.h"
44 #include "u_mem.h"
45 #include "ignorecase.h"
46
47 //values for d1 built-in mission
48 #define BIM_LAST_LEVEL          27
49 #define BIM_LAST_SECRET_LEVEL   -3
50 #define BIM_BRIEFING_FILE       "briefing.tex"
51 #define BIM_ENDING_FILE         "endreg.tex"
52
53 //mission list entry
54 typedef struct mle {
55         char    *filename;          // filename without extension
56         int     builtin_hogsize;    // if it's the built-in mission, used for determining the version
57         char    mission_name[MISSION_NAME_LEN+1];
58         ubyte   descent_version;    // descent 1 or descent 2?
59         ubyte   anarchy_only_flag;  // if true, mission is anarchy only
60         char    *path;                          // relative file path
61         int             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[PATH_MAX], *ext;
315
316                 strcpy(temp,filename);
317                 p = strrchr(temp, '/'); // get the filename at the end of the path
318                 if (!p)
319                         p = temp;
320                 else p++;
321                 
322                 if ((ext = strchr(p, '.')) == NULL)
323                         return 0;       //missing extension
324                 // look if it's .mn2 or .msn
325                 mission->descent_version = (ext[3] == '2') ? 2 : 1;
326                 *ext = 0;                       //kill extension
327
328                 mission->path = d_strdup(temp);
329                 mission->anarchy_only_flag = 0;
330                 mission->filename = mission->path + (p - temp);
331                 mission->location = location;
332
333                 p = get_parm_value("name",mfile);
334
335                 if (!p) {               //try enhanced mission
336                         cfseek(mfile,0,SEEK_SET);
337                         p = get_parm_value("xname",mfile);
338                 }
339
340                 if (!p) {       //try super-enhanced mission!
341                         cfseek(mfile,0,SEEK_SET);
342                         p = get_parm_value("zname",mfile);
343                 }
344
345                 if (p) {
346                         char *t;
347                         if ((t=strchr(p,';'))!=NULL)
348                                 *t=0;
349                         t = p + strlen(p)-1;
350                         while (isspace(*t))
351                                 *t-- = 0; // remove trailing whitespace
352                         if (strlen(p) > MISSION_NAME_LEN)
353                                 p[MISSION_NAME_LEN] = 0;
354                         strncpy(mission->mission_name, p, MISSION_NAME_LEN + 1);
355                 }
356                 else {
357                         cfclose(mfile);
358                         d_free(mission->path);
359                         return 0;
360                 }
361
362                 p = get_parm_value("type",mfile);
363
364                 //get mission type
365                 if (p)
366                         mission->anarchy_only_flag = istok(p,"anarchy");
367
368                 cfclose(mfile);
369
370                 return 1;
371         }
372
373         return 0;
374 }
375
376 void add_d1_builtin_mission_to_list(mle *mission)
377 {
378     int size;
379     
380         if (!cfexist("descent.hog"))
381                 return;
382
383         size = cfile_size("descent.hog");
384
385         switch (size) {
386         case D1_SHAREWARE_MISSION_HOGSIZE:
387         case D1_SHAREWARE_10_MISSION_HOGSIZE:
388         case D1_MAC_SHARE_MISSION_HOGSIZE:
389                 mission->filename = d_strdup(D1_MISSION_FILENAME);
390                 strcpy(mission->mission_name, D1_SHAREWARE_MISSION_NAME);
391                 mission->anarchy_only_flag = 0;
392                 break;
393         case D1_OEM_MISSION_HOGSIZE:
394         case D1_OEM_10_MISSION_HOGSIZE:
395                 mission->filename = d_strdup(D1_MISSION_FILENAME);
396                 strcpy(mission->mission_name, D1_OEM_MISSION_NAME);
397                 mission->anarchy_only_flag = 0;
398                 break;
399         default:
400                 Warning("Unknown D1 hogsize %d\n", size);
401                 Int3();
402                 // fall through
403         case D1_MISSION_HOGSIZE:
404         case D1_10_MISSION_HOGSIZE:
405         case D1_MAC_MISSION_HOGSIZE:
406                 mission->filename = d_strdup(D1_MISSION_FILENAME);
407                 strcpy(mission->mission_name, D1_MISSION_NAME);
408                 mission->anarchy_only_flag = 0;
409                 break;
410         }
411
412         mission->descent_version = 1;
413         mission->anarchy_only_flag = 0;
414         mission->builtin_hogsize = 0;
415         mission->path = mission->filename;
416         num_missions++;
417 }
418
419
420 void add_builtin_mission_to_list(mle *mission, char *name)
421 {
422     int size = cfile_size("descent2.hog");
423     
424         if (size == -1)
425                 size = cfile_size("d2demo.hog");
426
427         switch (size) {
428         case SHAREWARE_MISSION_HOGSIZE:
429         case MAC_SHARE_MISSION_HOGSIZE:
430                 mission->filename = d_strdup(SHAREWARE_MISSION_FILENAME);
431                 strcpy(mission->mission_name,SHAREWARE_MISSION_NAME);
432                 mission->anarchy_only_flag = 0;
433                 break;
434         case OEM_MISSION_HOGSIZE:
435                 mission->filename = d_strdup(OEM_MISSION_FILENAME);
436                 strcpy(mission->mission_name,OEM_MISSION_NAME);
437                 mission->anarchy_only_flag = 0;
438                 break;
439         default:
440                 Warning("Unknown hogsize %d, trying %s\n", size, FULL_MISSION_FILENAME ".mn2");
441                 Int3(); //fall through
442         case FULL_MISSION_HOGSIZE:
443         case FULL_10_MISSION_HOGSIZE:
444         case MAC_FULL_MISSION_HOGSIZE:
445                 if (!read_mission_file(mission, FULL_MISSION_FILENAME ".mn2", ML_CURDIR))
446                         Error("Could not find required mission file <%s>", FULL_MISSION_FILENAME ".mn2");
447         }
448
449         mission->path = mission->filename;
450         strcpy(name, mission->filename);
451     mission->builtin_hogsize = size;
452         mission->descent_version = 2;
453         mission->anarchy_only_flag = 0;
454         num_missions++;
455 }
456
457
458 void add_missions_to_list(mle *mission_list, char *path, char *rel_path, int anarchy_mode)
459 {
460         char **find, **i, *ext;
461
462         find = PHYSFS_enumerateFiles(path);
463
464         for (i = find; *i != NULL; i++)
465         {
466                 if (strlen(path) + strlen(*i) + 1 >= PATH_MAX)
467                         continue;       // path is too long
468
469                 strcat(rel_path, *i);
470                 if (PHYSFS_isDirectory(path))
471                 {
472                         strcat(rel_path, "/");
473                         add_missions_to_list(mission_list, path, rel_path, anarchy_mode);
474                         *(strrchr(path, '/')) = 0;
475                 }
476                 else if ((ext = strrchr(*i, '.')) && (!strnicmp(ext, ".msn", 4) || !strnicmp(ext, ".mn2", 4)))
477                         if (read_mission_file(&mission_list[num_missions], rel_path, ML_MISSIONDIR))
478                         {
479                                 if (anarchy_mode || !mission_list[num_missions].anarchy_only_flag)
480                                 {
481                                         mission_list[num_missions].builtin_hogsize = 0;
482                                         num_missions++;
483                                 }
484                                 else
485                                         d_free(mission_list[num_missions].path);
486                         }
487                 
488                 if (num_missions >= MAX_MISSIONS)
489                 {
490                         mprintf((0, "Warning: more missions than d2x can handle\n"));
491                         break;
492                 }
493
494                 (strrchr(path, '/'))[1] = 0;    // chop off the entry
495         }
496
497         PHYSFS_freeList(find);
498 }
499
500 /* move <mission_name> to <place> on mission list, increment <place> */
501 void promote (mle *mission_list, char * mission_name, int * top_place)
502 {
503         int i;
504         char name[FILENAME_LEN], * t;
505         strcpy(name, mission_name);
506         if ((t = strchr(name,'.')) != NULL)
507                 *t = 0; //kill extension
508         //printf("promoting: %s\n", name);
509         for (i = *top_place; i < num_missions; i++)
510                 if (!stricmp(mission_list[i].filename, name)) {
511                         //swap mission positions
512                         mle temp;
513
514                         temp = mission_list[*top_place];
515                         mission_list[*top_place] = mission_list[i];
516                         mission_list[i] = temp;
517                         ++(*top_place);
518                         break;
519                 }
520 }
521
522 void free_mission(void)
523 {
524     // May become more complex with the editor
525     if (Current_mission) {
526                 d_free(Current_mission->filename);
527         d_free(Current_mission);
528     }
529 }
530
531
532
533 //fills in the global list of missions.  Returns the number of missions
534 //in the list.  If anarchy_mode is set, then also add anarchy-only missions.
535
536 extern char CDROM_dir[];
537 #if 0
538 extern char AltHogDir[];
539 extern char AltHogdir_initialized;
540 #endif
541
542 mle *build_mission_list(int anarchy_mode)
543 {
544         mle *mission_list;
545         int top_place;
546     char        builtin_mission_filename[FILENAME_LEN];
547         char    search_str[PATH_MAX] = MISSION_DIR;
548
549         //now search for levels on disk
550
551 //@@Took out this code because after this routine was called once for
552 //@@a list of single-player missions, a subsequent call for a list of
553 //@@anarchy missions would not scan again, and thus would not find the
554 //@@anarchy-only missions.  If we retain the minimum level of install,
555 //@@we may want to put the code back in, having it always scan for all
556 //@@missions, and have the code that uses it sort out the ones it wants.
557 //@@    if (num_missions != -1) {
558 //@@            if (Current_mission_num != 0)
559 //@@                    load_mission(0);                                //set built-in mission as default
560 //@@            return num_missions;
561 //@@    }
562
563         MALLOC(mission_list, mle, MAX_MISSIONS);
564         num_missions = 0;
565         
566         add_builtin_mission_to_list(mission_list + num_missions, builtin_mission_filename);  //read built-in first
567         add_d1_builtin_mission_to_list(mission_list + num_missions);
568         add_missions_to_list(mission_list, search_str, search_str + strlen(search_str), anarchy_mode);
569         
570         // move original missions (in story-chronological order)
571         // to top of mission list
572         top_place = 0;
573         promote(mission_list, "descent", &top_place); // original descent 1 mission
574         promote(mission_list, builtin_mission_filename, &top_place); // d2 or d2demo
575         promote(mission_list, "d2x", &top_place); // vertigo
576
577         if (num_missions > top_place)
578                 qsort(&mission_list[top_place],
579                       num_missions - top_place,
580                       sizeof(*mission_list),
581                                 (int (*)( const void *, const void * ))ml_sort_func);
582
583
584         if (num_missions > top_place)
585                 qsort(&mission_list[top_place],
586                       num_missions - top_place,
587                       sizeof(*mission_list),
588                       (int (*)( const void *, const void * ))ml_sort_func);
589
590         //load_mission(0);   //set built-in mission as default
591
592     atexit(free_mission);
593
594         return mission_list;
595 }
596
597 void free_mission_list(mle *mission_list)
598 {
599         int i;
600
601         for (i = 0; i < num_missions; i++)
602                 d_free(mission_list[i].path);
603         
604         d_free(mission_list);
605         num_missions = 0;
606 }
607
608 void init_extra_robot_movie(char *filename);
609
610 //values for built-in mission
611
612 //loads the specfied mission from the mission list.
613 //build_mission_list() must have been called.
614 //Returns true if mission loaded ok, else false.
615 int load_mission(mle *mission)
616 {
617         CFILE *mfile;
618         char buf[PATH_MAX], *v;
619     int found_hogfile;
620
621     if (Current_mission)
622         free_mission();
623     Current_mission = d_malloc(sizeof(Mission));
624     if (!Current_mission) return 0;
625     *(mle *) Current_mission = *mission;
626         Current_mission->filename = d_strdup(mission->filename); // don't want to lose it
627
628     // for Descent 1 missions, load descent.hog
629     if (EMULATING_D1) {
630         if (!cfile_init("descent.hog"))
631             Warning("descent.hog not available, this mission may be missing some files required for briefings and exit sequence\n");
632         if (!stricmp(Current_mission_filename, D1_MISSION_FILENAME))
633             return load_mission_d1();
634     }
635
636     if (PLAYING_BUILTIN_MISSION) {
637                 switch (Current_mission->builtin_hogsize) {
638                 case SHAREWARE_MISSION_HOGSIZE:
639                 case MAC_SHARE_MISSION_HOGSIZE:
640                         return load_mission_shareware();
641                         break;
642                 case OEM_MISSION_HOGSIZE:
643                         return load_mission_oem();
644                         break;
645                 default:
646                         Int3(); // fall through
647                 case FULL_MISSION_HOGSIZE:
648                 case FULL_10_MISSION_HOGSIZE:
649                 case MAC_FULL_MISSION_HOGSIZE:
650                         // continue on... (use d2.mn2 from hogfile)
651                         break;
652                 }
653     }
654
655         mprintf(( 0, "Loading mission %s\n", Current_mission_filename ));
656
657         //read mission from file
658
659         switch (mission->location) {
660         case ML_MISSIONDIR:
661                 strcpy(buf,MISSION_DIR);
662                 break;
663         case ML_CDROM:
664                 strcpy(buf,CDROM_dir);
665                 break;
666         default:
667                 Int3();                                                 //fall through
668         case ML_CURDIR:
669                 strcpy(buf,"");
670                 break;
671         }
672         strcat(buf, mission->path);
673         if (mission->descent_version == 2)
674                 strcat(buf,".mn2");
675         else
676                 strcat(buf,".msn");
677
678         PHYSFSEXT_locateCorrectCase(buf);
679
680         mfile = cfopen(buf,"rb");
681         if (mfile == NULL) {
682         free_mission();
683                 return 0;               //error!
684         }
685
686     //for non-builtin missions, load HOG
687     if (!PLAYING_BUILTIN_MISSION) {
688
689         strcpy(buf+strlen(buf)-4,".hog");               //change extension
690
691                 PHYSFSEXT_locateCorrectCase(buf);
692
693                 found_hogfile = cfile_init(buf);
694
695 #ifdef RELEASE                          //for release, require mission to be in hogfile
696         if (! found_hogfile) {
697             cfclose(mfile);
698             free_mission();
699             return 0;
700         }
701 #endif
702     }
703
704     //init vars
705         Last_level = 0;
706         Last_secret_level = 0;
707         Briefing_text_filename[0] = 0;
708         Ending_text_filename[0] = 0;
709
710         while (cfgets(buf,80,mfile)) {
711
712                 if (istok(buf,"name")) {
713                         Current_mission->enhanced = 0;
714                         continue;                                               //already have name, go to next line
715                 }
716                 if (istok(buf,"xname")) {
717                         Current_mission->enhanced = 1;
718                         continue;                                               //already have name, go to next line
719                 }
720                 if (istok(buf,"zname")) {
721                         Current_mission->enhanced = 2;
722                         continue;                                               //already have name, go to next line
723                 }
724                 else if (istok(buf,"type"))
725                         continue;                                               //already have name, go to next line
726                 else if (istok(buf,"hog")) {
727                         char    *bufp = buf;
728
729                         while (*(bufp++) != '=')
730                                 ;
731
732                         if (*bufp == ' ')
733                                 while (*(++bufp) == ' ')
734                                         ;
735
736                         cfile_init(bufp);
737                         mprintf((0, "Hog file override = [%s]\n", bufp));
738                 }
739                 else if (istok(buf,"briefing")) {
740                         if ((v = get_value(buf)) != NULL) {
741                                 add_term(v);
742                                 if (strlen(v) < 13)
743                                         strcpy(Briefing_text_filename,v);
744                         }
745                 }
746                 else if (istok(buf,"ending")) {
747                         if ((v = get_value(buf)) != NULL) {
748                                 add_term(v);
749                                 if (strlen(v) < 13)
750                                         strcpy(Ending_text_filename,v);
751                         }
752                 }
753                 else if (istok(buf,"num_levels")) {
754
755                         if ((v=get_value(buf))!=NULL) {
756                                 int n_levels,i;
757
758                                 n_levels = atoi(v);
759
760                                 for (i=0;i<n_levels && cfgets(buf,80,mfile);i++) {
761
762                                         add_term(buf);
763                                         if (strlen(buf) <= 12) {
764                                                 strcpy(Level_names[i],buf);
765                                                 Last_level++;
766                                         }
767                                         else
768                                                 break;
769                                 }
770
771                         }
772                 }
773                 else if (istok(buf,"num_secrets")) {
774                         if ((v=get_value(buf))!=NULL) {
775                                 int i;
776
777                                 N_secret_levels = atoi(v);
778
779                                 Assert(N_secret_levels <= MAX_SECRET_LEVELS_PER_MISSION);
780
781                                 for (i=0;i<N_secret_levels && cfgets(buf,80,mfile);i++) {
782                                         char *t;
783
784                                         
785                                         if ((t=strchr(buf,','))!=NULL) *t++=0;
786                                         else
787                                                 break;
788
789                                         add_term(buf);
790                                         if (strlen(buf) <= 12) {
791                                                 strcpy(Secret_level_names[i],buf);
792                                                 Secret_level_table[i] = atoi(t);
793                                                 if (Secret_level_table[i]<1 || Secret_level_table[i]>Last_level)
794                                                         break;
795                                                 Last_secret_level--;
796                                         }
797                                         else
798                                                 break;
799                                 }
800
801                         }
802                 }
803
804         }
805
806         cfclose(mfile);
807
808         if (Last_level <= 0) {
809                 free_mission();         //no valid mission loaded
810                 return 0;
811         }
812
813         if (Current_mission->enhanced) {
814                 char t[50];
815                 extern void bm_read_extra_robots();
816                 sprintf(t,"%s.ham",Current_mission_filename);
817                 bm_read_extra_robots(t, Current_mission->enhanced);
818                 strncpy(t,Current_mission_filename,6);
819                 init_extra_robot_movie(t);
820         }
821
822         return 1;
823 }
824
825 //loads the named mission if exists.
826 //Returns true if mission loaded ok, else false.
827 int load_mission_by_name(char *mission_name)
828 {
829         int i;
830         mle *mission_list = build_mission_list(1);
831         bool found = 0;
832
833         for (i = 0; i < num_missions; i++)
834                 if (!stricmp(mission_name, mission_list[i].filename))
835                         found = load_mission(mission_list + i);
836
837         free_mission_list(mission_list);
838         return found;
839 }
840
841 int select_mission(int anarchy_mode, char *message)
842 {
843     mle *mission_list = build_mission_list(anarchy_mode);
844         int new_mission_num;
845
846     if (num_missions <= 1) {
847         new_mission_num = load_mission(mission_list) ? 0 : -1;
848     } else {
849         int i, default_mission;
850         char * m[MAX_MISSIONS];
851
852         default_mission = 0;
853         for (i = 0; i < num_missions; i++) {
854             m[i] = mission_list[i].mission_name;
855             if ( !stricmp( m[i], config_last_mission.string ) )
856                 default_mission = i;
857         }
858
859         new_mission_num = newmenu_listbox1( message, num_missions, m, 1, default_mission, NULL );
860
861         if (new_mission_num >= 0) {
862                         // Chose a mission
863                         cvar_set_cvar( &config_last_mission, m[new_mission_num] );
864         
865                         if (!load_mission(mission_list + new_mission_num)) {
866                                 nm_messagebox( NULL, 1, TXT_OK, TXT_MISSION_ERROR);
867                                 new_mission_num = -1;
868                         }
869                 }
870     }
871
872         free_mission_list(mission_list);
873     return (new_mission_num >= 0);
874 }