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