]> icculus.org git repositories - btb/d2x.git/blob - main/mission.c
allow missions to be in AltHogDir
[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 #ifdef HAVE_CONFIG_H
15 #include <conf.h>
16 #endif
17
18 #ifdef RCS
19 static char rcsid[] = "$Id: mission.c,v 1.3 2002-01-29 10:11:31 bradleyb Exp $";
20 #endif
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include <limits.h>
27
28 #include "pstypes.h"
29 #include "cfile.h"
30
31 #include "strutil.h"
32 #include "inferno.h"
33 #include "mission.h"
34 #include "gameseq.h"
35 #include "titles.h"
36 #include "songs.h"
37 #include "mono.h"
38 #include "error.h"
39 #include "findfile.h"
40
41 mle Mission_list[MAX_MISSIONS];
42
43 int Current_mission_num;
44 int     N_secret_levels;                //      Made a global by MK for scoring purposes.  August 1, 1995.
45 char *Current_mission_filename,*Current_mission_longname;
46
47 //this stuff should get defined elsewhere
48
49 char Level_names[MAX_LEVELS_PER_MISSION][FILENAME_LEN];
50 char Secret_level_names[MAX_SECRET_LEVELS_PER_MISSION][FILENAME_LEN];
51
52 //where the missions go
53 #ifndef EDITOR
54 #define MISSION_DIR "missions/"
55 #else
56 #define MISSION_DIR "./"
57 #endif
58
59 #ifdef SHAREWARE
60
61 //
62 //  Special versions of mission routines for shareware
63 //
64
65 #define SHAREWARE_MISSION_FILENAME      "d2demo"
66 #define SHAREWARE_MISSION_NAME          "Descent 2 Demo"
67
68 int build_mission_list(int anarchy_mode)
69 {
70         anarchy_mode++;         //kill warning
71
72         strcpy(Mission_list[0].filename,SHAREWARE_MISSION_FILENAME);
73         strcpy(Mission_list[0].mission_name,SHAREWARE_MISSION_NAME);
74         Mission_list[0].anarchy_only_flag = 0;
75
76         return load_mission(0);
77 }
78
79 int load_mission(int mission_num)
80 {
81         Assert(mission_num == 0);
82
83         Current_mission_num = 0;
84         Current_mission_filename = Mission_list[0].filename;
85         Current_mission_longname = Mission_list[0].mission_name;
86
87         N_secret_levels = 0;
88
89         Assert(Last_level == 3);
90
91 #ifdef MACINTOSH        // mac demo is using the regular hog and rl2 files
92         strcpy(Level_names[0],"d2leva-1.rl2");
93         strcpy(Level_names[1],"d2leva-2.rl2");
94         strcpy(Level_names[2],"d2leva-3.rl2");
95 #else
96         strcpy(Level_names[0],"d2leva-1.sl2");
97         strcpy(Level_names[1],"d2leva-2.sl2");
98         strcpy(Level_names[2],"d2leva-3.sl2");
99 #endif  // end of ifdef macintosh
100
101         return 1;
102 }
103
104 int load_mission_by_name(char *mission_name)
105 {
106         if (strcmp(mission_name,SHAREWARE_MISSION_FILENAME))
107                 return 0;               //cannot load requested mission
108         else
109                 return load_mission(0);
110
111 }
112
113
114
115 #else
116
117 #if defined(D2_OEM)
118
119 //
120 //  Special versions of mission routines for Diamond/S3 version
121 //
122
123 #define OEM_MISSION_FILENAME    "d2"
124 #define OEM_MISSION_NAME                "D2 Destination:Quartzon"
125
126 int build_mission_list(int anarchy_mode)
127 {
128         anarchy_mode++;         //kill warning
129
130         strcpy(Mission_list[0].filename,OEM_MISSION_FILENAME);
131         strcpy(Mission_list[0].mission_name,OEM_MISSION_NAME);
132         Mission_list[0].anarchy_only_flag = 0;
133
134         return load_mission(0);
135 }
136
137 int load_mission(int mission_num)
138 {
139         Assert(mission_num == 0);
140
141         Current_mission_num = 0;
142         Current_mission_filename = Mission_list[0].filename;
143         Current_mission_longname = Mission_list[0].mission_name;
144
145         N_secret_levels = 2;
146
147         Assert(Last_level == 8);
148
149         strcpy(Level_names[0],"d2leva-1.rl2");
150         strcpy(Level_names[1],"d2leva-2.rl2");
151         strcpy(Level_names[2],"d2leva-3.rl2");
152         strcpy(Level_names[3],"d2leva-4.rl2");
153
154         strcpy(Secret_level_names[0],"d2leva-s.rl2");
155
156         strcpy(Level_names[4],"d2levb-1.rl2");
157         strcpy(Level_names[5],"d2levb-2.rl2");
158         strcpy(Level_names[6],"d2levb-3.rl2");
159         strcpy(Level_names[7],"d2levb-4.rl2");
160
161         strcpy(Secret_level_names[1],"d2levb-s.rl2");
162
163         Secret_level_table[0] = 1;
164         Secret_level_table[1] = 5;
165
166         return 1;
167 }
168
169 int load_mission_by_name(char *mission_name)
170 {
171         if (strcmp(mission_name,OEM_MISSION_FILENAME))
172                 return 0;               //cannot load requested mission
173         else
174                 return load_mission(0);
175
176 }
177
178 #else
179
180 //strips damn newline from end of line
181 char *mfgets(char *s,int n,CFILE *f)
182 {
183         char *r;
184
185         r = cfgets(s,n,f);
186         if (r && s[strlen(s)-1] == '\n')
187                 s[strlen(s)-1] = 0;
188
189         return r;
190 }
191
192 //compare a string for a token. returns true if match
193 int istok(char *buf,char *tok)
194 {
195         return strnicmp(buf,tok,strlen(tok)) == 0;
196
197 }
198
199 //adds a terminating 0 after a string at the first white space
200 void add_term(char *s)
201 {
202         while (*s && !isspace(*s)) s++;
203
204         *s = 0;         //terminate!
205 }
206
207 //returns ptr to string after '=' & white space, or NULL if no '='
208 //adds 0 after parm at first white space
209 char *get_value(char *buf)
210 {
211         char *t;
212
213         t = strchr(buf,'=')+1;
214
215         if (t) {
216                 while (*t && isspace(*t)) t++;
217
218                 if (*t)
219                         return t;
220         }
221
222         return NULL;            //error!
223 }
224
225 //reads a line, returns ptr to value of passed parm.  returns NULL if none
226 char *get_parm_value(char *parm,CFILE *f)
227 {
228         static char buf[80];
229         
230         if (!mfgets(buf,80,f))
231                 return NULL;
232
233         if (istok(buf,parm))
234                 return get_value(buf);
235         else
236                 return NULL;
237 }
238
239 int ml_sort_func(mle *e0,mle *e1)
240 {
241         return stricmp(e0->mission_name,e1->mission_name);
242
243 }
244
245 extern char CDROM_dir[];
246 extern int HoardEquipped();
247
248 #define BUILTIN_MISSION "d2.mn2"
249
250 //returns 1 if file read ok, else 0
251 int read_mission_file(char *filename,int count,int location)
252 {
253         char filename2[100];
254         CFILE *mfile;
255
256         switch (location) {
257                 case ML_MISSIONDIR:
258                         strcpy(filename2,MISSION_DIR);  
259                         break;
260
261                 case ML_CDROM:                  
262                         songs_stop_redbook();           //so we can read from the CD
263                         strcpy(filename2,CDROM_dir);            
264                         break;
265
266                 default:                                        
267                         Int3();         //fall through
268
269                 case ML_CURDIR:         
270                         strcpy(filename2,"");
271                         break;
272         }
273         strcat(filename2,filename);
274
275         mfile = cfopen(filename2,"rb");
276
277         if (mfile) {
278                 char *p;
279                 char temp[FILENAME_LEN],*t;
280
281                 strcpy(temp,filename);
282                 if ((t = strchr(temp,'.')) == NULL)
283                         return 0;       //missing extension
284                 *t = 0;                 //kill extension
285
286                 strncpy( Mission_list[count].filename, temp, 9 );
287                 Mission_list[count].anarchy_only_flag = 0;
288                 Mission_list[count].location = location;
289
290                 p = get_parm_value("name",mfile);
291
292                 if (!p) {               //try enhanced mission
293                         cfseek(mfile,0,SEEK_SET);
294                         p = get_parm_value("xname",mfile);
295                 }
296
297 #ifdef NETWORK
298                 if (HoardEquipped())
299                 {
300                         if (!p) {               //try super-enhanced mission!
301                                 cfseek(mfile,0,SEEK_SET);
302                                 p = get_parm_value("zname",mfile);
303                         }
304                 }
305 #endif
306
307                 if (p) {
308                         char *t;
309                         if ((t=strchr(p,';'))!=NULL)
310                                 *t=0;
311                         t = p + strlen(p)-1;
312                         while (isspace(*t)) t--;
313                         strncpy(Mission_list[count].mission_name,p,MISSION_NAME_LEN);
314                 }
315                 else {
316                         cfclose(mfile);
317                         return 0;
318                 }
319
320                 p = get_parm_value("type",mfile);
321
322                 //get mission type 
323                 if (p)
324                         Mission_list[count].anarchy_only_flag = istok(p,"anarchy");
325
326                 cfclose(mfile);
327
328                 return 1;
329         }
330
331         return 0;
332 }
333
334
335 //fills in the global list of missions.  Returns the number of missions
336 //in the list.  If anarchy_mode set, don't include non-anarchy levels.
337 //if there is only one mission, this function will call load_mission on it.
338
339 extern char CDROM_dir[];
340 extern char AltHogDir[];
341 extern char AltHogdir_initialized;
342
343 int build_mission_list(int anarchy_mode)
344 {
345         static int num_missions=-1;
346         int count=0,special_count=0;
347         FILEFINDSTRUCT find;
348         char search_name[PATH_MAX + 5] = MISSION_DIR "*.mn2";
349
350         //now search for levels on disk
351
352 //@@Took out this code because after this routine was called once for
353 //@@a list of single-player missions, a subsequent call for a list of
354 //@@anarchy missions would not scan again, and thus would not find the
355 //@@anarchy-only missions.  If we retain the minimum level of install,
356 //@@we may want to put the code back in, having it always scan for all
357 //@@missions, and have the code that uses it sort out the ones it wants.
358 //@@    if (num_missions != -1) {
359 //@@            if (Current_mission_num != 0)
360 //@@                    load_mission(0);                                //set built-in mission as default
361 //@@            return num_missions;
362 //@@    }
363
364         if (!read_mission_file(BUILTIN_MISSION,0,ML_CURDIR))            //read built-in first
365                 Error("Could not find required mission file <%s>",BUILTIN_MISSION);
366
367         special_count = count=1; 
368
369         if( !FileFindFirst( search_name, &find ) ) {
370                 do      {
371                         if (stricmp(find.name,BUILTIN_MISSION)==0)
372                                 continue;               //skip the built-in
373
374                         if (read_mission_file(find.name,count,ML_MISSIONDIR)) {
375
376                                 if (anarchy_mode || !Mission_list[count].anarchy_only_flag)
377                                         count++;
378                         }
379
380                 } while( !FileFindNext( &find ) && count<MAX_MISSIONS);
381                 FileFindClose();
382         }
383
384         if (AltHogdir_initialized) {
385                 strcpy(search_name, AltHogDir);
386                 strcat(search_name, "/" MISSION_DIR "*.mn2");
387                 if( !FileFindFirst( search_name, &find ) ) {
388                         do      {
389                                 if (stricmp(find.name,BUILTIN_MISSION)==0)
390                                         continue;               //skip the built-in
391
392                                 if (read_mission_file(find.name,count,ML_MISSIONDIR)) {
393
394                                         if (anarchy_mode || !Mission_list[count].anarchy_only_flag)
395                                                 count++;
396                                 }
397
398                         } while( !FileFindNext( &find ) && count<MAX_MISSIONS);
399                         FileFindClose();
400                 }
401         }
402
403         //move vertigo to top of mission list
404         {
405                 int i;
406
407                 for (i=special_count;i<count;i++)
408                         if (!stricmp(Mission_list[i].filename,"D2X")) {         //swap!
409                                 mle temp;
410
411                                 temp = Mission_list[special_count];
412                                 Mission_list[special_count] = Mission_list[i];
413                                 Mission_list[i] = temp;
414
415                                 special_count++;
416
417                                 break;
418                         }
419         }
420
421
422         if (count>special_count)
423                 qsort(&Mission_list[special_count],count-special_count,sizeof(*Mission_list),
424                                 (int (*)( const void *, const void * ))ml_sort_func);
425
426         load_mission(0);                        //set built-in mission as default
427
428         num_missions = count;
429
430         return count;
431 }
432
433 void init_extra_robot_movie(char *filename);
434
435 //values for built-in mission
436
437 //loads the specfied mission from the mission list.  build_mission_list()
438 //must have been called.  If build_mission_list() returns 0, this function
439 //does not need to be called.  Returns true if mission loaded ok, else false.
440 int load_mission(int mission_num)
441 {
442         CFILE *mfile;
443         char buf[80], *v;
444         int found_hogfile;
445         int enhanced_mission = 0;
446
447         Current_mission_num = mission_num;
448
449         mprintf(( 0, "Loading mission %d\n", mission_num ));
450
451         //read mission from file 
452
453         switch (Mission_list[mission_num].location) {
454                 case ML_MISSIONDIR:     strcpy(buf,MISSION_DIR);        break;
455                 case ML_CDROM:                  strcpy(buf,CDROM_dir);          break;
456                 default:                                        Int3();                                                 //fall through
457                 case ML_CURDIR:         strcpy(buf,"");                         break;
458         }
459         strcat(buf,Mission_list[mission_num].filename);
460         strcat(buf,".mn2");
461
462         mfile = cfopen(buf,"rb");
463         if (mfile == NULL) {
464                 Current_mission_num = -1;
465                 return 0;               //error!
466         }
467
468         if (mission_num != 0) {         //for non-builtin missions, load HOG
469
470                 strcpy(buf+strlen(buf)-4,".hog");               //change extension
471
472                 found_hogfile = cfile_use_alternate_hogfile(buf);
473
474                 #ifdef RELEASE                          //for release, require mission to be in hogfile
475                 if (! found_hogfile) {
476                         cfclose(mfile);
477                         Current_mission_num = -1;
478                         return 0;
479                 }
480                 #endif
481         }
482
483         //init vars
484         Last_level =            0;
485         Last_secret_level = 0;
486
487         while (mfgets(buf,80,mfile)) {
488
489                 if (istok(buf,"name"))
490                         continue;                                               //already have name, go to next line
491                 if (istok(buf,"xname")) {
492                         enhanced_mission = 1;
493                         continue;                                               //already have name, go to next line
494                 }
495                 if (istok(buf,"zname")) {
496                         enhanced_mission = 2;
497                         continue;                                               //already have name, go to next line
498                 }
499                 else if (istok(buf,"type"))
500                         continue;                                               //already have name, go to next line                            
501                 else if (istok(buf,"hog")) {
502                         char    *bufp = buf;
503
504                         while (*(bufp++) != '=')
505                                 ;
506
507                         if (*bufp == ' ')
508                                 while (*(++bufp) == ' ')
509                                         ;
510
511                         cfile_use_alternate_hogfile(bufp);
512                         mprintf((0, "Hog file override = [%s]\n", bufp));
513                 }
514                 else if (istok(buf,"num_levels")) {
515
516                         if ((v=get_value(buf))!=NULL) {
517                                 int n_levels,i;
518
519                                 n_levels = atoi(v);
520
521                                 for (i=0;i<n_levels && mfgets(buf,80,mfile);i++) {
522
523                                         add_term(buf);
524                                         if (strlen(buf) <= 12) {
525                                                 strcpy(Level_names[i],buf);
526                                                 Last_level++;
527                                         }
528                                         else
529                                                 break;
530                                 }
531
532                         }
533                 }
534                 else if (istok(buf,"num_secrets")) {
535                         if ((v=get_value(buf))!=NULL) {
536                                 int i;
537
538                                 N_secret_levels = atoi(v);
539
540                                 Assert(N_secret_levels <= MAX_SECRET_LEVELS_PER_MISSION);
541
542                                 for (i=0;i<N_secret_levels && mfgets(buf,80,mfile);i++) {
543                                         char *t;
544
545                                         
546                                         if ((t=strchr(buf,','))!=NULL) *t++=0;
547                                         else
548                                                 break;
549
550                                         add_term(buf);
551                                         if (strlen(buf) <= 12) {
552                                                 strcpy(Secret_level_names[i],buf);
553                                                 Secret_level_table[i] = atoi(t);
554                                                 if (Secret_level_table[i]<1 || Secret_level_table[i]>Last_level)
555                                                         break;
556                                                 Last_secret_level--;
557                                         }
558                                         else
559                                                 break;
560                                 }
561
562                         }
563                 }
564
565         }
566
567         cfclose(mfile);
568
569         if (Last_level <= 0) {
570                 Current_mission_num = -1;               //no valid mission loaded
571                 return 0;
572         }
573
574         Current_mission_filename = Mission_list[Current_mission_num].filename;
575         Current_mission_longname = Mission_list[Current_mission_num].mission_name;
576
577         if (enhanced_mission) {
578                 char t[50];
579                 extern void bm_read_extra_robots();
580                 sprintf(t,"%s.ham",Current_mission_filename);
581                 bm_read_extra_robots(t,enhanced_mission);
582                 strncpy(t,Current_mission_filename,6);
583                 strcat(t,"-l.mvl");
584                 init_extra_robot_movie(t);
585         }
586
587         return 1;
588 }
589
590 //loads the named mission if exists.
591 //Returns true if mission loaded ok, else false.
592 int load_mission_by_name(char *mission_name)
593 {
594         int n,i;
595
596         n = build_mission_list(1);
597
598         for (i=0;i<n;i++)
599                 if (!stricmp(mission_name,Mission_list[i].filename))
600                         return load_mission(i);
601
602         return 0;               //couldn't find mission
603 }
604
605 #endif
606 #endif
607