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