]> icculus.org git repositories - taylor/freespace2.git/blob - src/mission/missioncampaign.cpp
switch to SDL_min/SDL_max (fixes compiler errors)
[taylor/freespace2.git] / src / mission / missioncampaign.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/Mission/MissionCampaign.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * source for dealing with campaigns
16  *
17  * $Log$
18  * Revision 1.8  2003/06/11 18:30:33  taylor
19  * plug memory leaks
20  *
21  * Revision 1.7  2003/05/25 02:30:42  taylor
22  * Freespace 1 support
23  *
24  * Revision 1.6  2002/07/24 00:20:42  relnev
25  * nothing interesting
26  *
27  * Revision 1.5  2002/06/21 03:34:05  relnev
28  * implemented a stub and fixed a path
29  *
30  * Revision 1.4  2002/06/09 04:41:22  relnev
31  * added copyright header
32  *
33  * Revision 1.3  2002/06/09 03:16:04  relnev
34  * added _splitpath.
35  *
36  * removed unneeded asm, old sdl 2d setup.
37  *
38  * fixed crash caused by opengl_get_region.
39  *
40  * Revision 1.2  2002/06/02 04:26:34  relnev
41  * warning cleanup
42  *
43  * Revision 1.1.1.1  2002/05/03 03:28:09  root
44  * Initial import.
45  *
46  * 
47  * 23    9/14/99 4:35a Dave
48  * Argh. Added all kinds of code to handle potential crashes in debriefing
49  * code.
50  * 
51  * 22    9/09/99 11:40p Dave
52  * Handle an SDL_assert() in beam code. Added supernova sounds. Play the right
53  * 2 end movies properly, based upon what the player did in the mission.
54  * 
55  * 21    9/09/99 9:34a Jefff
56  * fixed a potential exit-loop bug
57  * 
58  * 20    9/07/99 6:55p Jefff
59  * functionality to break out of a loop.  hacked functionality to jump to
60  * a specific mission in a campaign -- doesnt grant ships/weapons from
61  * skipped missions tho.
62  * 
63  * 19    9/07/99 2:19p Jefff
64  * clear skip mission player vars on mission skip
65  * 
66  * 18    9/06/99 9:45p Jefff
67  * break out of loop and skip mission support
68  * 
69  * 17    9/06/99 6:38p Dave
70  * Improved CD detection code.
71  * 
72  * 16    9/03/99 1:32a Dave
73  * CD checking by act. Added support to play 2 cutscenes in a row
74  * seamlessly. Fixed super low level cfile bug related to files in the
75  * root directory of a CD. Added cheat code to set campaign mission # in
76  * main hall.
77  * 
78  * 15    8/27/99 12:04a Dave
79  * Campaign loop screen.
80  * 
81  * 14    8/04/99 5:36p Andsager
82  * Show upsell screens at end of demo campaign before returning to main
83  * hall.
84  * 
85  * 13    2/05/99 3:50p Anoop
86  * Removed dumb campaign mission stats saving from multiplayer.
87  * 
88  * 12    12/17/98 2:43p Andsager
89  * Modify fred campaign save file to include optional mission loops
90  * 
91  * 11    12/12/98 3:17p Andsager
92  * Clean up mission eval, goal, event and mission scoring.
93  * 
94  * 10    12/10/98 9:59a Andsager
95  * Fix some bugs with mission loops
96  * 
97  * 9     12/09/98 1:56p Andsager
98  * Initial checkin of mission loop
99  * 
100  * 8     11/05/98 5:55p Dave
101  * Big pass at reducing #includes
102  * 
103  * 7     11/03/98 4:48p Johnson
104  * Fixed campaign file versioning bug left over from Silent Threat port.
105  * 
106  * 6     10/23/98 3:51p Dave
107  * Full support for tstrings.tbl and foreign languages. All that remains
108  * is to make it active in Fred.
109  * 
110  * 5     10/13/98 2:47p Andsager
111  * Remove reference to Tech_shivan_species_avail
112  * 
113  * 4     10/13/98 9:28a Dave
114  * Started neatening up freespace.h. Many variables renamed and
115  * reorganized. Added AlphaColors.[h,cpp]
116  * 
117  * 3     10/07/98 6:27p Dave
118  * Globalized mission and campaign file extensions. Removed Silent Threat
119  * special code. Moved \cache \players and \multidata into the \data
120  * directory.
121  * 
122  * 2     10/07/98 10:53a Dave
123  * Initial checkin.
124  * 
125  * 1     10/07/98 10:49a Dave
126  * 
127  * 95    9/10/98 1:17p Dave
128  * Put in code to flag missions and campaigns as being MD or not in Fred
129  * and Freespace. Put in multiplayer support for filtering out MD
130  * missions. Put in multiplayer popups for warning of non-valid missions.
131  * 
132  * 94    9/01/98 4:25p Dave
133  * Put in total (I think) backwards compatibility between mission disk
134  * freespace and non mission disk freespace, including pilot files and
135  * campaign savefiles.
136  * 
137  * 93    7/06/98 4:10p Hoffoss
138  * Fixed some bugs that presented themselves when trying to use a pilot
139  * that has a no longer existent campaign active.  Also expanded the
140  * campaign load code to actually return a proper error code, instead of
141  * always trapping errors internally and crashing, and always returning 0.
142  * 
143  * 92    6/17/98 9:30a Allender
144  * fixed red alert replay stats clearing problem
145  * 
146  * 91    6/01/98 11:43a John
147  * JAS & MK:  Classified all strings for localization.
148  * 
149  * 90    5/25/98 1:29p Allender
150  * end mission sequencing
151  * 
152  * 89    5/21/98 9:25p Allender
153  * endgame movie always viewable at end of campaign
154  * 
155  * 88    5/13/98 5:14p Allender
156  * red alert support to go back to previous mission
157  * 
158  * 87    5/12/98 4:16p Hoffoss
159  * Fixed bug where not all missions in all campaigns were being filtered
160  * out of stand alone mission listing in simulator room.
161  * 
162  * 86    5/05/98 3:29p Hoffoss
163  * Changed code so description is BEFORE num players in campaign file.
164  * Other code is relying on this ordering.
165  * 
166  * 85    5/05/98 12:19p Dave
167  * campaign description goes *after* num players
168  * 
169  * 84    5/04/98 5:52p Comet
170  * Fixed bug with Galatea/Bastion selection when finishing missions.
171  * 
172  * 83    5/01/98 2:46p Duncan
173  * fix a cfile problem with campaigns related to the new cfile stuff
174  * 
175  * 82    5/01/98 12:34p John
176  * Added code to force FreeSpace to run in the same dir as exe and made
177  * all the parse error messages a little nicer.
178  * 
179  * 81    4/30/98 7:01p Duncan
180  * AL: don't allow deletion of campaign files in multiplayer
181  * 
182  * 80    4/30/98 4:53p John
183  * Restructured and cleaned up cfile code.  Added capability to read off
184  * of CD-ROM drive and out of multiple pack files.
185  *
186  * $NoKeywords: $
187  */
188
189 #include <stdio.h>
190 #ifndef PLAT_UNIX
191 #include <direct.h>
192 #include <io.h>
193 #else
194 #include <sys/types.h>
195 #include <sys/stat.h>
196 #include <unistd.h>
197 #include <dirent.h>
198 #include <fnmatch.h>
199 #endif
200 #include <string.h>
201 #include <errno.h>
202
203 #include "key.h"
204 #include "ui.h"
205 #include "missioncampaign.h"
206 #include "gamesequence.h"
207 #include "2d.h"
208 #include "parselo.h"
209 #include "missionload.h"
210 #include "freespace.h"
211 #include "sexp.h"
212 #include "cfile.h"
213 #include "player.h"
214 #include "missiongoals.h"
215 #include "movie.h"
216 #include "multi.h"
217 #include "techmenu.h"
218 #include "eventmusic.h"
219 #include "alphacolors.h"
220 #include "localize.h"
221 #include "supernova.h"
222
223 // mission disk stuff
224 #define CAMPAIGN_SAVEFILE_MAX_SHIPS_OLD                                         75
225 #define CAMPAIGN_SAVEFILE_MAX_WEAPONS_OLD                                               44
226
227 #define CAMPAIGN_INITIAL_RELEASE_FILE_VERSION                           6
228
229 // campaign wasn't ended
230 int Campaign_ended_in_mission = 0;
231
232 // stuff for selecting campaigns.  We need to keep both arrays around since we display the
233 // list of campaigns by name, but must load campaigns by filename
234 char *Campaign_names[MAX_CAMPAIGNS];
235 char *Campaign_file_names[MAX_CAMPAIGNS];
236 int     Num_campaigns;
237
238 const char *campaign_types[MAX_CAMPAIGN_TYPES] = 
239 {
240 //XSTR:OFF
241         "single",
242         "multi coop",
243         "multi teams"
244 //XSTR:ON
245 };
246
247 // modules local variables to deal with getting new ships/weapons available to the player
248 int Num_granted_ships, Num_granted_weapons;             // per mission counts of new ships and weapons
249 int Granted_ships[MAX_SHIP_TYPES];
250 int Granted_weapons[MAX_WEAPON_TYPES];
251
252 // variables to control the UI stuff for loading campaigns
253 static UI_WINDOW Campaign_window;
254 static UI_LISTBOX Campaign_listbox;
255 static UI_BUTTON Campaign_okb, Campaign_cancelb;
256
257 // the campaign!!!!!
258 campaign Campaign;
259
260 // variables with deal with the campaign save file
261 #ifndef MAKE_FS1
262 #define CAMPAIGN_FILE_VERSION                                                   12
263 //#define CAMPAIGN_FILE_COMPATIBLE_VERSION              CAMPAIGN_INITIAL_RELEASE_FILE_VERSION
264 #define CAMPAIGN_FILE_COMPATIBLE_VERSION                        CAMPAIGN_FILE_VERSION
265 #else
266 #define CAMPAIGN_FILE_VERSION                                   7
267 #define CAMPAIGN_FILE_COMPATIBLE_VERSION                CAMPAIGN_INITIAL_RELEASE_FILE_VERSION
268 #endif
269 #define CAMPAIGN_FILE_ID                                                                0xbeefcafe
270
271 // variables with deal with the campaign stats save file
272 #define CAMPAIGN_STATS_FILE_VERSION                                     1
273 #define CAMPAIGN_STATS_FILE_COMPATIBLE_VERSION  1
274 #define CAMPAIGN_STATS_FILE_ID                                          0xabbadaad
275
276 // mission_campaign_get_name returns a string (which is malloced in this routine) of the name
277 // of the given freespace campaign file.  In the type field, we return if the campaign is a single
278 // player or multiplayer campaign.  The type field will only be valid if the name returned is non-NULL
279 int mission_campaign_get_info(const char *filename, char *name, int *type, int *max_players, char **desc)
280 {
281         int i;
282         char campaign_type[NAME_LENGTH], fname[MAX_FILENAME_LEN];
283
284         SDL_assert( name != NULL );
285         SDL_assert( type != NULL );
286
287         // open localization
288         lcl_ext_open();
289
290         SDL_strlcpy(fname, filename, SDL_arraysize(fname));
291         if ((strlen(fname) < 4) || SDL_strcasecmp(fname + strlen(fname) - 4, FS_CAMPAIGN_FILE_EXT)){
292                 SDL_strlcat(fname, FS_CAMPAIGN_FILE_EXT, SDL_arraysize(fname));
293         }
294
295         SDL_assert(strlen(fname) < MAX_FILENAME_LEN);
296
297         try {
298                 read_file_text( fname );
299                 reset_parse();
300                 required_string("$Name:");
301
302                 stuff_string( name, F_NAME, NULL );
303                 if ( name == NULL ) {
304                         Int3();
305                         nprintf(("Warning", "No name found for campaign file %s\n", filename));
306
307                         // close localization
308                         lcl_ext_close();
309
310                         return 0;
311                 }
312
313                 required_string( "$Type:" );
314                 stuff_string( campaign_type, F_NAME, NULL );
315
316                 *type = -1;
317                 for (i=0; i<MAX_CAMPAIGN_TYPES; i++) {
318                         if ( !SDL_strcasecmp(campaign_type, campaign_types[i]) ) {
319                                 *type = i;
320                         }
321                 }
322
323                 if (desc) {
324                         *desc = NULL;
325                         if (optional_string("+Description:")) {
326                                 *desc = stuff_and_malloc_string(F_MULTITEXT, NULL, MISSION_DESC_LENGTH);
327                         }
328                 }
329
330                 // if this is a multiplayer campaign, get the max players
331                 if ((*type) > 0) {
332                         skip_to_string("+Num Players:");
333                         stuff_int(max_players);
334                 }               
335
336                 // if we found a valid campaign type
337                 if ((*type) >= 0) {
338                         // close localization
339                         lcl_ext_close();
340
341                         return 1;
342                 }
343         } catch (parse_error_t rval) {
344                 if (rval == PARSE_ERROR_FILE_NOT_FOUND) {
345                         // close localization
346                         lcl_ext_close();
347
348                         return 0;
349                 }
350
351                 Error(LOCATION, "Error parsing '%s'\r\nError code = %i.\r\n", fname, (int)rval);
352         }
353
354         Int3();         // get Allender -- incorrect type found
355
356         // close localization
357         lcl_ext_close();
358
359         return 0;
360 }
361
362 // parses campaign and returns a list of missions in it.  Returns number of missions added to
363 // the 'list', and up to 'max' missions may be added to 'list'.  Returns negative on error.
364 //
365 int mission_campaign_get_mission_list(const char *filename, char **list, int max)
366 {
367         int i, num = 0;
368         char name[NAME_LENGTH];
369
370         filename = cf_add_ext(filename, FS_CAMPAIGN_FILE_EXT);
371
372         // read the mission file and get the list of mission filenames
373         try {
374                 read_file_text(filename);
375                 reset_parse();
376
377                 while (skip_to_string("$Mission:") > 0) {
378                         stuff_string(name, F_NAME, NULL);
379                         if (num < max)
380                                 list[num++] = strdup(name);
381                 }
382         } catch (parse_error_t) {
383                 // since we can't return count of allocated elements, free them instead
384                 for (i=0; i<num; i++)
385                         free(list[i]);
386
387                 return -1;
388         }
389
390         return num;
391 }
392
393 // gets optional ship/weapon information
394 void mission_campaign_get_sw_info()
395 {
396         int i, count, ship_list[MAX_SHIP_TYPES], weapon_list[MAX_WEAPON_TYPES];
397
398         // set allowable ships to the SIF_PLAYER_SHIPs
399         memset( Campaign.ships_allowed, 0, sizeof(Campaign.ships_allowed) );
400         for (i = 0; i < MAX_SHIP_TYPES; i++ ) {
401                 if ( Ship_info[i].flags & SIF_PLAYER_SHIP )
402                         Campaign.ships_allowed[i] = 1;
403         }
404
405         for (i = 0; i < MAX_WEAPON_TYPES; i++ )
406                 Campaign.weapons_allowed[i] = 1;
407
408         if ( optional_string("+Starting Ships:") ) {
409                 for (i = 0; i < MAX_SHIP_TYPES; i++ )
410                         Campaign.ships_allowed[i] = 0;
411
412                 count = stuff_int_list(ship_list, MAX_SHIP_TYPES, SHIP_INFO_TYPE);
413
414                 // now set the array elements stating which ships we are allowed
415                 for (i = 0; i < count; i++ ) {
416                         if ( Ship_info[ship_list[i]].flags & SIF_PLAYER_SHIP )
417                                 Campaign.ships_allowed[ship_list[i]] = 1;
418                 }
419         }
420
421         if ( optional_string("+Starting Weapons:") ) {
422                 for (i = 0; i < MAX_WEAPON_TYPES; i++ )
423                         Campaign.weapons_allowed[i] = 0;
424
425                 count = stuff_int_list(weapon_list, MAX_WEAPON_TYPES, WEAPON_POOL_TYPE);
426
427                 // now set the array elements stating which ships we are allowed
428                 for (i = 0; i < count; i++ )
429                         Campaign.weapons_allowed[weapon_list[i]] = 1;
430         }
431 }
432
433 // mission_campaign_load starts a new campaign.  It reads in the mission information in the campaign file
434 // It also sets up all variables needed inside of the game to deal with starting mission numbers, etc
435 //
436 // Note: Due to difficulties in generalizing this function, parts of it are duplicated throughout
437 // this file.  If you change the format of the campaign file, you should be sure these related
438 // functions work properly and update them if it breaks them.
439 int mission_campaign_load( const char *filename, int load_savefile )
440 {
441         int len, i;
442         char name[NAME_LENGTH], type[NAME_LENGTH];
443
444         filename = cf_add_ext(filename, FS_CAMPAIGN_FILE_EXT);
445
446         // open localization
447         lcl_ext_open(); 
448
449         // read the mission file and get the list of mission filenames
450         try {
451                 // be sure to remove all old malloced strings of Mission_names
452                 // we must also free any goal stuff that was from a previous campaign
453                 // this also frees sexpressions so the next call to init_sexp will be able to reclaim
454                 // nodes previously used by another campaign.
455                 mission_campaign_close();
456
457                 SDL_strlcpy( Campaign.filename, filename, SDL_arraysize(Campaign.filename) );
458
459                 // only initialize the sexpression stuff when Fred isn't running.  It'll screw things up major
460                 // if it does
461                 if ( !Fred_running ){
462                         init_sexp();            // must initialize the sexpression stuff
463                 }
464
465                 read_file_text( filename );
466                 reset_parse();
467                 memset( &Campaign, 0, sizeof(Campaign) );
468
469                 // copy filename to campaign structure minus the extension
470                 len = SDL_min(strlen(filename) - 4 + 1, SDL_arraysize(Campaign.filename));
471                 SDL_strlcpy(Campaign.filename, filename, len);
472
473                 required_string("$Name:");
474                 stuff_string( name, F_NAME, NULL );
475                 
476                 //Store campaign name in the global struct
477                 SDL_strlcpy( Campaign.name, name, SDL_arraysize(Campaign.name) );
478
479                 required_string( "$Type:" );
480                 stuff_string( type, F_NAME, NULL );
481
482                 for (i = 0; i < MAX_CAMPAIGN_TYPES; i++ ) {
483                         if ( !SDL_strcasecmp(type, campaign_types[i]) ) {
484                                 Campaign.type = i;
485                                 break;
486                         }
487                 }
488
489                 if ( i == MAX_CAMPAIGN_TYPES )
490                         Error(LOCATION, "Unknown campaign type %s!", type);
491
492                 Campaign.desc = NULL;
493                 if (optional_string("+Description:"))
494                         Campaign.desc = stuff_and_malloc_string(F_MULTITEXT, NULL, MISSION_DESC_LENGTH);
495
496                 // if the type is multiplayer -- get the number of players
497                 Campaign.num_players = 0;
498                 if ( Campaign.type != CAMPAIGN_TYPE_SINGLE) {
499                         required_string("+Num players:");
500                         stuff_int( &(Campaign.num_players) );
501                 }               
502
503 #ifdef MAKE_FS1
504                 // check if mission disk -
505                 // doesn't do anything but prevent non fatal error messages
506                 optional_string("+Missiondisk");
507 #endif
508                 
509                 // parse the optional ship/weapon information
510                 mission_campaign_get_sw_info();
511
512                 // parse the mission file and actually read in the mission stuff
513                 Campaign.num_missions = 0;
514                 while ( required_string_either("#End", "$Mission:") ) {
515                         cmission *cm;
516
517                         required_string("$Mission:");
518                         stuff_string(name, F_NAME, NULL);
519                         cm = &Campaign.missions[Campaign.num_missions];
520                         cm->name = strdup(name);
521
522                         cm->briefing_cutscene[0] = 0;
523                         if ( optional_string("+Briefing Cutscene:") )
524                                 stuff_string( cm->briefing_cutscene, F_NAME, NULL );
525
526                         cm->flags = 0;
527                         if (optional_string("+Flags:"))
528                                 stuff_int(&cm->flags);
529
530                         cm->formula = -1;
531                         if ( optional_string("+Formula:") ) {
532                                 cm->formula = get_sexp_main();
533                                 if ( !Fred_running ) {
534                                         SDL_assert ( cm->formula != -1 );
535                                         sexp_mark_persistent( cm->formula );
536
537                                 } else {
538                                         if ( cm->formula == -1 ){
539                                                 // close localization
540                                                 lcl_ext_close();
541
542                                                 return CAMPAIGN_ERROR_SEXP_EXHAUSTED;
543                                         }
544                                 }
545                         }
546
547                         // Do misison looping stuff
548                         cm->has_mission_loop = 0;
549                         if ( optional_string("+Mission Loop:") ) {
550                                 cm->has_mission_loop = 1;
551                         }
552
553                         cm->mission_loop_desc = NULL;
554                         if ( optional_string("+Mission Loop Text:")) {
555                                 cm->mission_loop_desc = stuff_and_malloc_string(F_MULTITEXT, NULL, MISSION_DESC_LENGTH);
556                         }
557
558                         cm->mission_loop_brief_anim = NULL;
559                         if ( optional_string("+Mission Loop Brief Anim:")) {
560                                 cm->mission_loop_brief_anim = stuff_and_malloc_string(F_MULTITEXT, NULL, MAX_FILENAME_LEN);
561                         }
562
563                         cm->mission_loop_brief_sound = NULL;
564                         if ( optional_string("+Mission Loop Brief Sound:")) {
565                                 cm->mission_loop_brief_sound = stuff_and_malloc_string(F_MULTITEXT, NULL, MAX_FILENAME_LEN);
566                         }
567
568                         cm->mission_loop_formula = -1;
569                         if ( optional_string("+Formula:") ) {
570                                 cm->mission_loop_formula = get_sexp_main();
571                                 if ( !Fred_running ) {
572                                         SDL_assert ( cm->mission_loop_formula != -1 );
573                                         sexp_mark_persistent( cm->mission_loop_formula );
574
575                                 } else {
576                                         if ( cm->mission_loop_formula == -1 ){
577                                                 // close localization
578                                                 lcl_ext_close();
579
580                                                 return CAMPAIGN_ERROR_SEXP_EXHAUSTED;
581                                         }
582                                 }
583                         }
584
585                         if (optional_string("+Level:")) {
586                                 stuff_int( &cm->level );
587                                 if ( cm->level == 0 )  // check if the top (root) of the whole tree
588                                         Campaign.next_mission = Campaign.num_missions;
589
590                         } else
591                                 Campaign.realign_required = 1;
592
593                         if (optional_string("+Position:"))
594                                 stuff_int( &cm->pos );
595                         else
596                                 Campaign.realign_required = 1;
597
598                         if (Fred_running) {
599                                 cm->num_goals = -1;
600                                 cm->num_events = -1;
601                                 cm->notes = NULL;
602
603                         } else {
604                                 cm->num_goals = 0;
605                                 cm->num_events = 0;
606                         }
607
608                         cm->goals = NULL;
609                         cm->events = NULL;
610                         Campaign.num_missions++;
611                 }
612         } catch (parse_error_t rval) {
613                 (void)rval;     // suppress unused warning in release build
614                 mprintf(("Error parsing '%s'\r\nError code = %i.\r\n", filename, (int)rval));
615
616                 // close localization
617                 lcl_ext_close();
618
619                 return CAMPAIGN_ERROR_CORRUPT;
620         }
621
622         // set up the other variables for the campaign stuff.  After initializing, we must try and load
623         // the campaign save file for this player.  Since all campaign loads go through this routine, I
624         // think this place should be the only necessary place to load the campaign save stuff.  The campaign
625         // save file will get written when a mission has ended by player choice.
626         Campaign.next_mission = 0;
627         Campaign.prev_mission = -1;
628         Campaign.current_mission = -1;  
629
630         // loading the campaign will get us to the current and next mission that the player must fly
631         // plus load all of the old goals that future missions might rely on.
632         if (!Fred_running && load_savefile && (Campaign.type == CAMPAIGN_TYPE_SINGLE)) {
633                 mission_campaign_savefile_load(Campaign.filename);
634         }
635
636         // close localization
637         lcl_ext_close();
638
639         return 0;
640 }
641
642 // mission_campaign_load_by_name() loads up a freespace campaign given the filename.  This routine
643 // is used to load up campaigns when a pilot file is loaded.  Generally, the
644 // filename will probably be the freespace campaign file, but not necessarily.
645 int mission_campaign_load_by_name( const char *filename )
646 {
647         char real_filename[MAX_FILENAME_LEN+1] = { 0 };
648         char name[NAME_LENGTH],test[5];
649         int type,max_players;
650
651         // make sure to tack on .fsc on the end if its not there already
652         if(strlen(filename) > 0){
653                 SDL_strlcpy(real_filename, filename, MAX_FILENAME_LEN);
654
655                 if(strlen(real_filename) > 4){
656                         SDL_strlcpy(test, real_filename+(strlen(real_filename)-4), SDL_arraysize(test));
657                         if(strcmp(test, FS_CAMPAIGN_FILE_EXT)!=0){
658                                 SDL_strlcat(real_filename, FS_CAMPAIGN_FILE_EXT, SDL_arraysize(real_filename));
659                         }
660                 } else {
661                         SDL_strlcat(real_filename, FS_CAMPAIGN_FILE_EXT, SDL_arraysize(real_filename));
662                 }
663         } else {
664                 Error(LOCATION,"Tried to load campaign file with illegal length/extension!");
665         }
666
667         if (!mission_campaign_get_info(real_filename, name, &type, &max_players)){
668                 return -1;      
669         }
670
671         Num_campaigns = 0;
672         Campaign_file_names[Num_campaigns] = real_filename;
673         Campaign_names[Num_campaigns] = name;
674         Num_campaigns++;
675         mission_campaign_load(real_filename);           
676         return 0;
677 }
678
679 int mission_campaign_load_by_name_csfe( const char *filename, const char *callsign )
680 {
681         Game_mode |= GM_NORMAL;
682         SDL_strlcpy(Player->callsign, callsign, SDL_arraysize(Player->callsign));
683         return mission_campaign_load_by_name( filename);
684 }
685
686
687 // mission_campaign_init initializes some variables then loads the default Freespace single player campaign.
688 void mission_campaign_init()
689 {
690         memset(&Campaign, 0, sizeof(Campaign) );
691 }
692
693 // Fill in the root of the campaign save filename
694 void mission_campaign_savefile_generate_root(char *filename, const int max_len)
695 {
696         char base[MAX_FILENAME_LEN];
697
698         SDL_assert ( strlen(Campaign.filename) != 0 );
699
700         // build up the filename for the save file.  There could be a problem with filename length,
701         // but this problem can get fixed in several ways -- ignore the problem for now though.
702         base_filename(Campaign.filename, base, SDL_arraysize(base));
703         SDL_assert ( (int)(strlen(base) + strlen(Player->callsign)) < max_len );
704
705         SDL_snprintf( filename, max_len, NOX("%s.%s."), Player->callsign, base );
706 }
707
708 // mission_campaign_savefile_save saves the state of the campaign.  This function will probably always be called
709 // then the player is done flying a mission in the campaign path.  It will save the missions played, the
710 // state of the goals, etc.
711
712 int mission_campaign_savefile_save()
713 {
714         char filename[MAX_PATH_LEN];
715         CFILE *fp;
716         int i,j, mission_count;
717
718         memset(filename, 0, sizeof(filename));
719         mission_campaign_savefile_generate_root(filename, SDL_arraysize(filename));
720
721         // name the file differently depending on whether we're in single player or multiplayer mode
722         // single player : *.csg
723         SDL_strlcat( filename, NOX("csg"), SDL_arraysize(filename) );
724
725         fp = cfopen(filename,"wb", CFILE_NORMAL, CF_TYPE_SINGLE_PLAYERS);
726
727         if (!fp)
728                 return errno;
729
730         // Write out campaign file info
731         cfwrite_int( CAMPAIGN_FILE_ID,fp );
732         cfwrite_int( CAMPAIGN_FILE_VERSION,fp );
733
734         // put in the file signature (single or multiplayer campaign) - see MissionCampaign.h for the #defines
735         cfwrite_int( CAMPAIGN_SINGLE_PLAYER_SIG, fp );
736
737         // do we need to write out the filename of the campaign?
738         cfwrite_string_len( Campaign.filename, fp );
739         cfwrite_int( Campaign.prev_mission, fp );
740         cfwrite_int( Campaign.next_mission, fp );
741 #ifndef MAKE_FS1
742         cfwrite_int( Campaign.loop_reentry, fp );
743         cfwrite_int( Campaign.loop_enabled, fp );
744 #endif
745
746         // write out the information for ships/weapons which this player is allowed to use
747         cfwrite_int(Num_ship_types, fp);
748         cfwrite_int(Num_weapon_types, fp);
749         for ( i = 0; i < Num_ship_types; i++ ){
750                 cfwrite_char( Campaign.ships_allowed[i], fp );
751         }
752
753         for ( i = 0; i < Num_weapon_types; i++ ){
754                 cfwrite_char( Campaign.weapons_allowed[i], fp );
755         }
756
757         // write out the completed mission matrix.  Used to tell which missions the player
758         // can replay in the simulator.  Also, each completed mission contains a list of the goals
759         // that were in the mission along with the goal completion status.
760         cfwrite_int( Campaign.num_missions_completed, fp );
761         for (i = 0; i < MAX_CAMPAIGN_MISSIONS; i++ ) {
762                 if ( Campaign.missions[i].completed ) {
763                         cfwrite_int( i, fp );
764                         cfwrite_int( Campaign.missions[i].num_goals, fp );
765                         for ( j = 0; j < Campaign.missions[i].num_goals; j++ ) {
766                                 cfwrite_string_len( Campaign.missions[i].goals[j].name, fp );
767                                 cfwrite_char( Campaign.missions[i].goals[j].status, fp );
768                         }
769                         cfwrite_int( Campaign.missions[i].num_events, fp );
770                         for ( j = 0; j < Campaign.missions[i].num_events; j++ ) {
771                                 cfwrite_string_len( Campaign.missions[i].events[j].name, fp );
772                                 cfwrite_char( Campaign.missions[i].events[j].status, fp );
773                         }
774 #ifndef MAKE_FS1
775                         // write flags
776                         cfwrite_int(Campaign.missions[i].flags, fp);
777 #endif
778                 }
779         }
780
781         cfclose( fp );
782
783         // 6/17/98
784         // ugh!  due to horrible bug, the stats saved at the end of every level were not written
785         // out to disk.  Write out a seperate file to do this.  We will only read it in if we actually
786         // find the file.
787         memset(filename, 0, sizeof(filename));
788         mission_campaign_savefile_generate_root(filename, SDL_arraysize(filename));
789
790         // name the file differently depending on whether we're in single player or multiplayer mode
791         // single player : *.csg
792         SDL_strlcat( filename, NOX("css"), SDL_arraysize(filename) );
793
794         fp = cfopen(filename,"wb", CFILE_NORMAL, CF_TYPE_SINGLE_PLAYERS);
795
796         if (!fp)
797                 return errno;
798
799         // Write out campaign file info
800         cfwrite_int( CAMPAIGN_STATS_FILE_ID,fp );
801         cfwrite_int( CAMPAIGN_STATS_FILE_VERSION,fp );
802
803         // determine how many missions we are saving -- I think that this method is safer than the method
804         // I used for release
805         mission_count = 0;
806         for ( i = 0; i < Campaign.num_missions; i++ ) {
807                 if ( Campaign.missions[i].completed ) {
808                         mission_count++;
809                 }
810         }
811
812         // write out the stats information to disk.     
813         cfwrite_int( mission_count, fp );
814         for (i = 0; i < Campaign.num_missions; i++ ) {
815                 if ( Campaign.missions[i].completed ) {
816                         cfwrite_int( i, fp );
817                         cfwrite( &Campaign.missions[i].stats, sizeof(scoring_struct), 1, fp );
818                 }
819         }
820
821         cfclose( fp );
822         
823         return 0;
824 }
825
826 // The following function always only ever ever ever called by CSFE!!!!!
827 int campaign_savefile_save(const char *pname)
828 {
829         if (Campaign.type == CAMPAIGN_TYPE_SINGLE)
830                 Game_mode &= ~GM_MULTIPLAYER;
831         else
832                 Game_mode |= GM_MULTIPLAYER;
833
834         SDL_strlcpy(Player->callsign, pname, SDL_arraysize(Player->callsign));
835         //memcpy(&Campaign, camp, sizeof(campaign));
836         return mission_campaign_savefile_save();
837 }
838
839
840 // the below two functions is internal to this module.  It is here so that I can group the save/load
841 // functions together.
842 //
843
844 // mission_campaign_savefile_delete deletes any save file in the players directory for the given
845 // campaign filename
846 void mission_campaign_savefile_delete( const char *cfilename, int is_multi )
847 {
848         char filename[MAX_PATH_LEN], base[MAX_FILENAME_LEN];
849
850         base_filename(cfilename, base, SDL_arraysize(base));
851
852         if ( Player->flags & PLAYER_FLAGS_IS_MULTI ) {
853                 return; // no such thing as a multiplayer campaign savefile
854         }
855
856         SDL_snprintf( filename, SDL_arraysize(filename), NOX("%s.%s.csg"), Player->callsign, base );
857
858         cf_delete( filename, CF_TYPE_SINGLE_PLAYERS );
859 }
860
861 void campaign_delete_save( const char *cfn, const char *pname)
862 {
863         SDL_strlcpy(Player->callsign, pname, SDL_arraysize(Player->callsign));
864         mission_campaign_savefile_delete(cfn);
865 }
866
867 // next function deletes all the save files for this particular pilot.  Just call cfile function
868 // which will delete multiple files
869 // Player_select_mode tells us whether we are deleting single or multiplayer files
870 void mission_campaign_delete_all_savefiles( const char *pilot_name, int is_multi )
871 {
872         int dir_type, num_files, i;
873         char *names[MAX_CAMPAIGNS], spec[MAX_FILENAME_LEN + 2];
874         const char *ext;
875         char filename[1024];
876         int (*filter_save)(const char *filename);
877
878         if ( is_multi ) {
879                 return;                         // can't have multiplayer campaign save files
880         }
881
882         ext = NOX(".csg");
883         dir_type = CF_TYPE_SINGLE_PLAYERS;
884
885         SDL_snprintf(spec, SDL_arraysize(spec), NOX("%s.*%s"), pilot_name, ext);
886
887         // HACK HACK HACK HACK!!!!  cf_get_file_list is not reentrant.  Pretty dumb because it should
888         // be.  I have to save any file filters
889         filter_save = Get_file_list_filter;
890         Get_file_list_filter = NULL;
891         num_files = cf_get_file_list(MAX_CAMPAIGNS, names, dir_type, spec);
892         Get_file_list_filter = filter_save;
893
894         for (i=0; i<num_files; i++) {
895                 SDL_strlcpy(filename, names[i], SDL_arraysize(filename));
896                 SDL_strlcat(filename, ext, SDL_arraysize(filename));
897                 cf_delete(filename, dir_type);
898                 free(names[i]);
899         }
900 }
901
902 // mission_campaign_savefile_load takes a filename of a campaign file as a parameter and loads all
903 // of the information stored in the campaign file.
904 void mission_campaign_savefile_load( const char *cfilename )
905 {
906         char filename[MAX_PATH_LEN], base[MAX_FILENAME_LEN];
907         int version, i, num, j, num_stats_blocks;
908         uint id, type_sig;
909         CFILE *fp;
910
911         SDL_assert ( strlen(cfilename) != 0 );
912
913         // probably only called from single player games anymore!!! should be anyway
914         SDL_assert( Game_mode & GM_NORMAL );            // get allender or DaveB.  trying to save campaign in multiplayer
915
916         // build up the filename for the save file.  There could be a problem with filename length,
917         // but this problem can get fixed in several ways -- ignore the problem for now though.
918         base_filename(cfilename, base, SDL_arraysize(base));
919         SDL_assert ( (strlen(base) + strlen(Player->callsign)) < SDL_arraysize(filename) );
920
921         if(Game_mode & GM_MULTIPLAYER)
922                 SDL_snprintf( filename, SDL_arraysize(filename), NOX("%s.%s.msg"), Player->callsign, base );
923         else
924                 SDL_snprintf( filename, SDL_arraysize(filename), NOX("%s.%s.csg"), Player->callsign, base );
925
926         fp = cfopen(filename, "rb", CFILE_NORMAL, CF_TYPE_SINGLE_PLAYERS );
927         if ( !fp )
928                 return;
929
930         id = cfread_int( fp );
931         if ( id != CAMPAIGN_FILE_ID ) {
932                 Warning(LOCATION, "Campaign save file has invalid signature");
933                 cfclose( fp );
934                 return;
935         }
936
937         version = cfread_int( fp );
938         if ( version < CAMPAIGN_FILE_COMPATIBLE_VERSION ) {
939                 Warning(LOCATION, "Campaign save file too old -- not compatible.  Deleting file.\nYou can continue from here without trouble\n\n");
940                 cfclose( fp );
941                 cf_delete( filename, CF_TYPE_SINGLE_PLAYERS );
942                 return;
943         }
944
945         // verify that we are loading the correct type of campaign file for the mode that we are in.
946         if(version >= 3)
947                 type_sig = cfread_int( fp );
948         else
949                 type_sig = CAMPAIGN_SINGLE_PLAYER_SIG;
950         // the actual check
951         SDL_assert( ((Game_mode & GM_MULTIPLAYER) && (type_sig==CAMPAIGN_MULTI_PLAYER_SIG)) || (!(Game_mode & GM_MULTIPLAYER) && (type_sig==CAMPAIGN_SINGLE_PLAYER_SIG)) );
952
953         Campaign.type = type_sig == CAMPAIGN_SINGLE_PLAYER_SIG ? CAMPAIGN_TYPE_SINGLE : CAMPAIGN_TYPE_MULTI_COOP;
954
955         // read in the filename of the campaign and compare the filenames to be sure that
956         // we are reading data that really belongs to this campaign.  I think that this check
957         // is redundant.
958         cfread_string_len( filename, SDL_arraysize(filename), fp );
959         /*if ( SDL_strcasecmp( filename, cfilename) ) { //      Used to be !SDL_strcasecmp.  How did this ever work? --MK, 11/9/97
960                 Warning(LOCATION, "Campaign save file appears corrupt because of mismatching filenames.");
961                 cfclose(fp);
962                 return;
963         }*/
964
965         Campaign.prev_mission = cfread_int( fp );
966         Campaign.next_mission = cfread_int( fp );
967 #ifndef MAKE_FS1
968         Campaign.loop_reentry = cfread_int( fp );
969         Campaign.loop_enabled = cfread_int( fp );
970 #endif
971
972         //  load information about ships/weapons allowed        
973         int ship_count, weapon_count;
974
975         // if earlier than mission disk version, use old MAX_SHIP_TYPES, otherwise read from the file           
976         if(version <= CAMPAIGN_INITIAL_RELEASE_FILE_VERSION){
977                 ship_count = CAMPAIGN_SAVEFILE_MAX_SHIPS_OLD;
978                 weapon_count = CAMPAIGN_SAVEFILE_MAX_WEAPONS_OLD;
979         } else {
980                 ship_count = cfread_int(fp);
981                 weapon_count = cfread_int(fp);
982         }
983
984         for ( i = 0; i < ship_count; i++ ){
985                 Campaign.ships_allowed[i] = cfread_ubyte( fp );
986         }
987
988         for ( i = 0; i < weapon_count; i++ ){
989                 Campaign.weapons_allowed[i] = cfread_ubyte( fp );
990         }       
991
992         // read in the completed mission matrix.  Used to tell which missions the player
993         // can replay in the simulator.  Also, each completed mission contains a list of the goals
994         // that were in the mission along with the goal completion status.
995         Campaign.num_missions_completed = cfread_int( fp );
996         for (i = 0; i < Campaign.num_missions_completed; i++ ) {
997                 num = cfread_int( fp );
998                 Campaign.missions[num].completed = 1;
999                 Campaign.missions[num].num_goals = cfread_int( fp );
1000                 
1001                 // be sure to malloc out space for the goals stuff, then zero the memory!!!  Don't do malloc
1002                 // if there are no goals
1003                 Campaign.missions[num].goals = (mgoal *)malloc( Campaign.missions[num].num_goals * sizeof(mgoal) );
1004                 if ( Campaign.missions[num].num_goals > 0 ) {
1005                         memset( Campaign.missions[num].goals, 0, sizeof(mgoal) * Campaign.missions[num].num_goals );
1006                         SDL_assert( Campaign.missions[num].goals != NULL );
1007                 }
1008
1009                 // now read in the goal information for this mission
1010                 for ( j = 0; j < Campaign.missions[num].num_goals; j++ ) {
1011                         cfread_string_len( Campaign.missions[num].goals[j].name, NAME_LENGTH, fp );
1012                         Campaign.missions[num].goals[j].status = cfread_char( fp );
1013                 }
1014
1015                 // get the events from the savefile
1016                 Campaign.missions[num].num_events = cfread_int( fp );
1017                 
1018                 // be sure to malloc out space for the events stuff, then zero the memory!!!  Don't do malloc
1019                 // if there are no events
1020 //              if (Campaign.missions[num].events < 0)
1021 //                      Campaign.missions[num].events = 0;
1022                 Campaign.missions[num].events = (mevent *)malloc( Campaign.missions[num].num_events * sizeof(mevent) );
1023                 if ( Campaign.missions[num].num_events > 0 ) {
1024                         memset( Campaign.missions[num].events, 0, sizeof(mevent) * Campaign.missions[num].num_events );
1025                         SDL_assert( Campaign.missions[num].events != NULL );
1026                 }
1027                 
1028                 // now read in the event information for this mission
1029                 for ( j = 0; j < Campaign.missions[num].num_events; j++ ) {
1030                         cfread_string_len( Campaign.missions[num].events[j].name, NAME_LENGTH, fp );
1031                         Campaign.missions[num].events[j].status = cfread_char( fp );
1032                 }
1033
1034 #ifndef MAKE_FS1
1035                 // now read flags
1036                 Campaign.missions[num].flags = cfread_int(fp);
1037 #endif
1038         }       
1039
1040         cfclose( fp );
1041
1042         // 4/17/98
1043         // now, try and read in the campaign stats saved information.  This code was added for the 1.03 patch
1044         // since the stats data was never written out to disk.  We try and open the file, and if we cannot find
1045         // it, then simply return
1046         SDL_snprintf( filename, SDL_arraysize(filename), NOX("%s.%s.css"), Player->callsign, base );
1047
1048         fp = cfopen(filename, "rb", CFILE_NORMAL, CF_TYPE_SINGLE_PLAYERS );
1049         if ( !fp )
1050                 return;
1051
1052         id = cfread_int( fp );
1053         if ( id != CAMPAIGN_STATS_FILE_ID ) {
1054                 Warning(LOCATION, "Campaign stats save file has invalid signature");
1055                 cfclose( fp );
1056                 return;
1057         }
1058
1059         version = cfread_int( fp );
1060         if ( version < CAMPAIGN_STATS_FILE_COMPATIBLE_VERSION ) {
1061                 Warning(LOCATION, "Campaign save file too old -- not compatible.  Deleting file.\nYou can continue from here without trouble\n\n");
1062                 cfclose( fp );
1063                 cf_delete( filename, CF_TYPE_SINGLE_PLAYERS );
1064                 return;
1065         }
1066
1067         num_stats_blocks = cfread_int( fp );
1068         for (i = 0; i < num_stats_blocks; i++ ) {
1069                 num = cfread_int( fp );
1070                 cfread( &Campaign.missions[num].stats, sizeof(scoring_struct), 1, fp );
1071         }
1072
1073         cfclose(fp);
1074 }
1075
1076 // the following code only ever called by CSFE!!!!
1077 void campaign_savefile_load(const char *fname, const char *pname)
1078 {
1079         if (Campaign.type==CAMPAIGN_TYPE_SINGLE) {
1080                 Game_mode &= ~GM_MULTIPLAYER;
1081                 Game_mode &= GM_NORMAL;
1082         }
1083         else
1084                 Game_mode |= GM_MULTIPLAYER;
1085         SDL_strlcpy(Player->callsign, pname, SDL_arraysize(Player->callsign));
1086         mission_campaign_savefile_load(fname);
1087 }
1088
1089 // mission_campaign_next_mission sets up the internal veriables of the campaign
1090 // structure so the player can play the next mission.  If there are no more missions
1091 // available in the campaign, this function returns -1, else 0 if the mission was
1092 // set successfully
1093 int mission_campaign_next_mission()
1094 {
1095         if ( (Campaign.next_mission == -1) || (strlen(Campaign.name) == 0) ) // will be set to -1 when there is no next mission
1096                 return -1;
1097
1098         Campaign.current_mission = Campaign.next_mission;       
1099         SDL_strlcpy( Game_current_mission_filename, Campaign.missions[Campaign.current_mission].name, MAX_FILENAME_LEN );
1100
1101         // check for end of loop.
1102         if (Campaign.current_mission == Campaign.loop_reentry) {
1103                 Campaign.loop_enabled = 0;
1104         }
1105
1106         // reset the number of persistent ships and weapons for the next campaign mission
1107         Num_granted_ships = 0;
1108         Num_granted_weapons = 0;
1109         return 0;
1110 }
1111
1112 // mission_campaign_previous_mission() gets called to go to the previous mission in
1113 // the campaign.  Used only for Red Alert missions
1114 int mission_campaign_previous_mission()
1115 {
1116         if ( !(Game_mode & GM_CAMPAIGN_MODE) )
1117                 return 0;
1118
1119         if ( Campaign.prev_mission == -1 )
1120                 return 0;
1121
1122         Campaign.current_mission = Campaign.prev_mission;
1123         Campaign.next_mission = Campaign.current_mission;
1124         Campaign.num_missions_completed--;
1125         Campaign.missions[Campaign.next_mission].completed = 0;
1126         mission_campaign_savefile_save();
1127
1128         // reset the player stats to be the stats from this level
1129         memcpy( &Player->stats, &Campaign.missions[Campaign.current_mission].stats, sizeof(Player->stats) );
1130
1131         SDL_strlcpy( Game_current_mission_filename, Campaign.missions[Campaign.current_mission].name, MAX_FILENAME_LEN );
1132         Num_granted_ships = 0;
1133         Num_granted_weapons = 0;
1134
1135         return 1;
1136 }
1137
1138 /*
1139 // determine what the next mission is after the current one.  Because this evaluates an sexp,
1140 // and that could check just about anything, the results are only going to be valid in
1141 // certain places.
1142 // DA 12/09/98 -- To allow for mission loops, need to maintain call with store stats
1143 int mission_campaign_eval_next_mission( int store_stats )
1144 {
1145         char *name;
1146         int cur, i;
1147         cmission *mission;
1148
1149         Campaign.next_mission = -1;
1150         cur = Campaign.current_mission;
1151         name = Campaign.missions[cur].name;
1152
1153         mission = &Campaign.missions[cur];
1154
1155         // first we must save the status of the current missions goals in the campaign mission structure.
1156         // After that, we can determine which mission is tagged as the next mission.  Finally, we
1157         // can save the campaign save file
1158         // we might have goal and event status if the player replayed a mission
1159         if ( mission->num_goals > 0 ) {
1160                 free( mission->goals );
1161         }
1162
1163         mission->num_goals = Num_goals;
1164         if ( mission->num_goals > 0 ) {
1165                 mission->goals = (mgoal *)malloc( sizeof(mgoal) * Num_goals );
1166                 SDL_assert( mission->goals != NULL );
1167         }
1168
1169         // copy the needed info from the Mission_goal struct to our internal structure
1170         for (i = 0; i < Num_goals; i++ ) {
1171                 if ( strlen(Mission_goals[i].name) == 0 ) {
1172                         char name[NAME_LENGTH];
1173
1174                         sprintf(name, NOX("Goal #%d"), i);
1175                         //Warning(LOCATION, "Mission goal in mission %s must have a +Name field! using %s for campaign save file\n", mission->name, name);
1176                         strcpy( mission->goals[i].name, name);
1177                 } else
1178                         strcpy( mission->goals[i].name, Mission_goals[i].name );
1179                 SDL_assert ( Mission_goals[i].satisfied != GOAL_INCOMPLETE );           // should be true or false at this point!!!
1180                 mission->goals[i].status = (char)Mission_goals[i].satisfied;
1181         }
1182
1183         // do the same thing for events as we did for goals
1184         // we might have goal and event status if the player replayed a mission
1185         if ( mission->num_events > 0 ) {
1186                 free( mission->events );
1187         }
1188
1189         mission->num_events = Num_mission_events;
1190         if ( mission->num_events > 0 ) {
1191                 mission->events = (mevent *)malloc( sizeof(mevent) * Num_mission_events );
1192                 SDL_assert( mission->events != NULL );
1193         }
1194
1195         // copy the needed info from the Mission_goal struct to our internal structure
1196         for (i = 0; i < Num_mission_events; i++ ) {
1197                 if ( strlen(Mission_events[i].name) == 0 ) {
1198                         char name[NAME_LENGTH];
1199
1200                         sprintf(name, NOX("Event #%d"), i);
1201                         nprintf(("Warning", "Mission goal in mission %s must have a +Name field! using %s for campaign save file\n", mission->name, name));
1202                         strcpy( mission->events[i].name, name);
1203                 } else
1204                         strcpy( mission->events[i].name, Mission_events[i].name );
1205
1206                 // getting status for the events is a little different.  If the formula value for the event entry
1207                 // is -1, then we know the value of the result field will never change.  If the formula is
1208                 // not -1 (i.e. still being evaluated at mission end time), we will write "incomplete" for the
1209                 // event evaluation
1210                 if ( Mission_events[i].formula == -1 ) {
1211                         if ( Mission_events[i].result )
1212                                 mission->events[i].status = EVENT_SATISFIED;
1213                         else
1214                                 mission->events[i].status = EVENT_FAILED;
1215                 } else
1216                         Int3();
1217         }
1218
1219         // maybe store the alltime stats which would be current at the end of this mission
1220         if ( store_stats ) {
1221                 memcpy( &mission->stats, &Player->stats, sizeof(Player->stats) );
1222                 scoring_backout_accept( &mission->stats );
1223         }
1224
1225         if ( store_stats ) {    // second (last) time through, so use choose loop_mission if chosen
1226                 if ( Campaign.loop_enabled ) {
1227                         Campaign.next_mission = Campaign.loop_mission;
1228                 } else {
1229                         // evaluate next mission (straight path)
1230                         if (Campaign.missions[cur].formula != -1) {
1231                                 flush_sexp_tree(Campaign.missions[cur].formula);  // force formula to be re-evaluated
1232                                 eval_sexp(Campaign.missions[cur].formula);  // this should reset Campaign.next_mission to proper value
1233                         }
1234                 }
1235         } else {
1236
1237                 // evaluate next mission (straight path)
1238                 if (Campaign.missions[cur].formula != -1) {
1239                         flush_sexp_tree(Campaign.missions[cur].formula);  // force formula to be re-evaluated
1240                         eval_sexp(Campaign.missions[cur].formula);  // this should reset Campaign.next_mission to proper value
1241                 }
1242
1243                 // evaluate mission loop mission (if any) so it can be used if chosen
1244                 if ( Campaign.missions[cur].has_mission_loop ) {
1245                         int copy_next_mission = Campaign.next_mission;
1246                         // Set temporarily to -1 so we know if loop formula fails to assign
1247                         Campaign.next_mission = -1;  // Cannot exit campaign from loop
1248                         if (Campaign.missions[cur].mission_loop_formula != -1) {
1249                                 flush_sexp_tree(Campaign.missions[cur].mission_loop_formula);  // force formula to be re-evaluated
1250                                 eval_sexp(Campaign.missions[cur].mission_loop_formula);  // this should reset Campaign.next_mission to proper value
1251                         }
1252
1253                         Campaign.loop_mission = Campaign.next_mission;
1254                         Campaign.next_mission = copy_next_mission;
1255                 }
1256         }
1257
1258         if (Campaign.next_mission == -1)
1259                 nprintf(("allender", "No next mission to proceed to.\n"));
1260         else
1261                 nprintf(("allender", "Next mission is number %d [%s]\n", Campaign.next_mission, Campaign.missions[Campaign.next_mission].name));
1262
1263         return Campaign.next_mission;
1264 } */
1265
1266 // Evaluate next campaign mission - set as Campaign.next_mission.  Also set Campaign.loop_mission
1267 void mission_campaign_eval_next_mission()
1268 {
1269         Campaign.next_mission = -1;
1270         int cur = Campaign.current_mission;
1271
1272         // evaluate next mission (straight path)
1273         if (Campaign.missions[cur].formula != -1) {
1274                 flush_sexp_tree(Campaign.missions[cur].formula);  // force formula to be re-evaluated
1275                 eval_sexp(Campaign.missions[cur].formula);  // this should reset Campaign.next_mission to proper value
1276         }
1277
1278         // evaluate mission loop mission (if any) so it can be used if chosen
1279         if ( Campaign.missions[cur].has_mission_loop ) {
1280                 int copy_next_mission = Campaign.next_mission;
1281                 // Set temporarily to -1 so we know if loop formula fails to assign
1282                 Campaign.next_mission = -1;
1283                 if (Campaign.missions[cur].mission_loop_formula != -1) {
1284                         flush_sexp_tree(Campaign.missions[cur].mission_loop_formula);  // force formula to be re-evaluated
1285                         eval_sexp(Campaign.missions[cur].mission_loop_formula);  // this should reset Campaign.next_mission to proper value
1286                 }
1287
1288                 Campaign.loop_mission = Campaign.next_mission;
1289                 Campaign.next_mission = copy_next_mission;
1290         }
1291
1292         if (Campaign.next_mission == -1) {
1293                 nprintf(("allender", "No next mission to proceed to.\n"));
1294         } else {
1295                 nprintf(("allender", "Next mission is number %d [%s]\n", Campaign.next_mission, Campaign.missions[Campaign.next_mission].name));
1296         }
1297
1298 }
1299
1300 // Store mission's goals and events in Campaign struct
1301 void mission_campaign_store_goals_and_events()
1302 {
1303         int cur, i;
1304         cmission *mission;
1305
1306         cur = Campaign.current_mission;
1307
1308         mission = &Campaign.missions[cur];
1309
1310         // first we must save the status of the current missions goals in the campaign mission structure.
1311         // After that, we can determine which mission is tagged as the next mission.  Finally, we
1312         // can save the campaign save file
1313         // we might have goal and event status if the player replayed a mission
1314         if ( mission->num_goals > 0 ) {
1315                 free( mission->goals );
1316         }
1317
1318         mission->num_goals = Num_goals;
1319         if ( mission->num_goals > 0 ) {
1320                 mission->goals = (mgoal *)malloc( sizeof(mgoal) * Num_goals );
1321                 SDL_assert( mission->goals != NULL );
1322         }
1323
1324         // copy the needed info from the Mission_goal struct to our internal structure
1325         for (i = 0; i < Num_goals; i++ ) {
1326                 if ( strlen(Mission_goals[i].name) == 0 ) {
1327                         char name[NAME_LENGTH];
1328
1329                         SDL_snprintf(name, SDL_arraysize(name), NOX("Goal #%d"), i);
1330                         //Warning(LOCATION, "Mission goal in mission %s must have a +Name field! using %s for campaign save file\n", mission->name, name);
1331                         SDL_strlcpy( mission->goals[i].name, name, SDL_arraysize(mission->goals[0].name));
1332                 } else
1333                         SDL_strlcpy( mission->goals[i].name, Mission_goals[i].name, SDL_arraysize(mission->goals[0].name) );
1334                 SDL_assert ( Mission_goals[i].satisfied != GOAL_INCOMPLETE );           // should be true or false at this point!!!
1335                 mission->goals[i].status = (char)Mission_goals[i].satisfied;
1336         }
1337
1338         // do the same thing for events as we did for goals
1339         // we might have goal and event status if the player replayed a mission
1340         if ( mission->num_events > 0 ) {
1341                 free( mission->events );
1342         }
1343
1344         mission->num_events = Num_mission_events;
1345         if ( mission->num_events > 0 ) {
1346                 mission->events = (mevent *)malloc( sizeof(mevent) * Num_mission_events );
1347                 SDL_assert( mission->events != NULL );
1348         }
1349
1350         // copy the needed info from the Mission_goal struct to our internal structure
1351         for (i = 0; i < Num_mission_events; i++ ) {
1352                 if ( strlen(Mission_events[i].name) == 0 ) {
1353                         char name[NAME_LENGTH];
1354
1355                         SDL_snprintf(name, SDL_arraysize(name), NOX("Event #%d"), i);
1356                         nprintf(("Warning", "Mission goal in mission %s must have a +Name field! using %s for campaign save file\n", mission->name, name));
1357                         SDL_strlcpy( mission->events[i].name, name, SDL_arraysize(mission->events[0].name));
1358                 } else
1359                         SDL_strlcpy( mission->events[i].name, Mission_events[i].name, SDL_arraysize(mission->events[0].name) );
1360
1361                 // getting status for the events is a little different.  If the formula value for the event entry
1362                 // is -1, then we know the value of the result field will never change.  If the formula is
1363                 // not -1 (i.e. still being evaluated at mission end time), we will write "incomplete" for the
1364                 // event evaluation
1365                 if ( Mission_events[i].formula == -1 ) {
1366                         if ( Mission_events[i].result )
1367                                 mission->events[i].status = EVENT_SATISFIED;
1368                         else
1369                                 mission->events[i].status = EVENT_FAILED;
1370                 } else
1371                         Int3();
1372         }
1373 }
1374
1375 // this function is called when the player's mission is over.  It updates the internal store of goals
1376 // and their status then saves the state of the campaign in the campaign file.  This gets called
1377 // after player accepts mission results in debriefing.
1378 void mission_campaign_mission_over()
1379 {
1380         int mission_num, i;
1381         cmission *mission;
1382
1383         // I don't think that we should have a record for these -- maybe we might??????  If we do,
1384         // then we should free them
1385         if ( !(Game_mode & GM_CAMPAIGN_MODE) ){
1386                 return;
1387         }
1388
1389         mission_num = Campaign.current_mission;
1390         SDL_assert( mission_num != -1 );
1391         mission = &Campaign.missions[mission_num];
1392
1393         // determine if any ships/weapons were granted this mission
1394         for ( i=0; i<Num_granted_ships; i++ ){
1395                 Campaign.ships_allowed[Granted_ships[i]] = 1;
1396         }
1397
1398         for ( i=0; i<Num_granted_weapons; i++ ){
1399                 Campaign.weapons_allowed[Granted_weapons[i]] = 1;       
1400         }
1401
1402         // DKA 12/11/98 - Unneeded already evaluated and stored
1403         // determine what new mission we are moving to.
1404         //      mission_campaign_eval_next_mission(1);
1405
1406         // update campaign.mission stats (used to allow backout inRedAlert)
1407         memcpy( &mission->stats, &Player->stats, sizeof(Player->stats) );
1408         if(!(Game_mode & GM_MULTIPLAYER)){
1409                 scoring_backout_accept( &mission->stats );
1410         }
1411
1412         // if we are moving to a new mission, then change our data.  If we are staying on the same mission,
1413         // then we don't want to do anything.  Remove information about goals/events
1414         if ( Campaign.next_mission != mission_num ) {
1415                 Campaign.prev_mission = mission_num;
1416                 Campaign.current_mission = -1;
1417                 Campaign.num_missions_completed++;
1418                 Campaign.missions[mission_num].completed = 1;
1419
1420                 // save the scoring values from the previous mission at the start of this mission -- for red alert
1421
1422                 // save the state of the campaign in the campaign save file and move to the end_game state
1423                 if (Campaign.type == CAMPAIGN_TYPE_SINGLE) {
1424                         mission_campaign_savefile_save();
1425                 }
1426
1427         } else {
1428                 // free up the goals and events which were just malloced.  It's kind of like erasing any fact
1429                 // that the player played this mission in the campaign.
1430                 free( mission->goals );
1431                 mission->num_goals = 0;
1432
1433                 free( mission->events );
1434                 mission->num_events = 0;
1435
1436                 Sexp_nodes[mission->formula].value = SEXP_UNKNOWN;
1437         }
1438
1439         SDL_assert(Player);
1440         if (Campaign.missions[Campaign.next_mission].flags & CMISSION_FLAG_BASTION){
1441                 Player->on_bastion = 1;
1442         } else {
1443                 Player->on_bastion = 0;
1444         }
1445
1446         mission_campaign_next_mission();                        // sets up whatever needs to be set to actually play next mission
1447 }
1448
1449 // called when the game closes -- to get rid of memory errors for Bounds checker
1450 void mission_campaign_close()
1451 {
1452         int i;
1453
1454         if (Campaign.desc)
1455                 free(Campaign.desc);
1456
1457         // be sure to remove all old malloced strings of Mission_names
1458         // we must also free any goal stuff that was from a previous campaign
1459         for ( i=0; i<Campaign.num_missions; i++ ) {
1460                 if ( Campaign.missions[i].name ){
1461                         free(Campaign.missions[i].name);
1462                 }
1463
1464                 if (Campaign.missions[i].notes){
1465                         free(Campaign.missions[i].notes);
1466                 }
1467
1468                 if ( Campaign.missions[i].num_goals > 0 ){
1469                         free ( Campaign.missions[i].goals );
1470                 }
1471
1472                 if ( Campaign.missions[i].num_events > 0 ){
1473                         free ( Campaign.missions[i].events );
1474                 }
1475
1476                 // the next three are strdup'd return values from parselo.cpp
1477                 if (Campaign.missions[i].mission_loop_desc) {
1478                         free(Campaign.missions[i].mission_loop_desc);
1479                 }
1480
1481                 if (Campaign.missions[i].mission_loop_brief_anim) {
1482                         free(Campaign.missions[i].mission_loop_brief_anim);
1483                 }
1484
1485                 if (Campaign.missions[i].mission_loop_brief_sound) {
1486                         free(Campaign.missions[i].mission_loop_brief_sound);
1487                 }
1488
1489                 if ( !Fred_running ){
1490                         sexp_unmark_persistent(Campaign.missions[i].formula);           // free any sexpression nodes used by campaign.
1491                 }
1492
1493                 Campaign.missions[i].num_goals = 0;
1494                 Campaign.missions[i].num_events = 0;
1495         }
1496 }
1497
1498 // call from game_shutdown() ONLY!!!
1499 void mission_campaign_shutdown()
1500 {
1501         int i;
1502         
1503         for (i=0; i<MAX_CAMPAIGNS; i++) {
1504                 if (Campaign_names[i] != NULL) {
1505                         free(Campaign_names[i]);
1506                         Campaign_names[i] = NULL;
1507                 }
1508
1509                 if (Campaign_file_names[i] != NULL) {
1510                         free(Campaign_file_names[i]);
1511                         Campaign_file_names[i] = NULL;
1512                 }
1513         }
1514 }
1515
1516 // extract the mission filenames for a campaign.  
1517 //
1518 // filename     =>      name of campaign file
1519 //      dest            => storage for the mission filename, must be already allocated
1520 // num          => output parameter for the number of mission filenames in the campaign
1521 //
1522 // note that dest should allocate at least dest[MAX_CAMPAIGN_MISSIONS][NAME_LENGTH]
1523 int mission_campaign_get_filenames(const char *filename, char dest[][NAME_LENGTH], int *num)
1524 {
1525         // read the mission file and get the list of mission filenames
1526         try {
1527                 read_file_text(filename);
1528                 SDL_assert(strlen(filename) < MAX_FILENAME_LEN - 1);  // make sure no overflow
1529
1530                 reset_parse();
1531                 required_string("$Name:");
1532                 advance_to_eoln(NULL);
1533
1534                 required_string( "$Type:" );
1535                 advance_to_eoln(NULL);
1536
1537                 // parse the mission file and actually read in the mission stuff
1538                 *num = 0;
1539                 while ( skip_to_string("$Mission:") == 1 ) {
1540                         stuff_string(dest[*num], F_NAME, NULL);
1541                         (*num)++;
1542                 }
1543         } catch (parse_error_t rval) {
1544                 return (int)rval;
1545         }
1546
1547         return 0;
1548 }
1549
1550 // function to read the goals and events from a mission in a campaign file and store that information
1551 // in the campaign structure for use in the campaign editor, error checking, etc
1552 void read_mission_goal_list(int num)
1553 {
1554         char *filename, notes[NOTES_LENGTH], goals[MAX_GOALS][NAME_LENGTH];
1555         char events[MAX_MISSION_EVENTS][NAME_LENGTH];
1556         int i, z, event_count, count = 0;
1557
1558         filename = Campaign.missions[num].name;
1559
1560         // open localization
1561         lcl_ext_open(); 
1562
1563         // default counts to zero
1564         event_count = 0;
1565         count = 0;
1566
1567         try {
1568                 read_file_text(filename);
1569                 init_parse();
1570
1571                 // first, read the mission notes for this mission.  Used in campaign editor
1572                 if (skip_to_string("#Mission Info")) {
1573                         if (skip_to_string("$Notes:")) {
1574                                 stuff_string(notes, F_NOTES, NULL);
1575                                 if (Campaign.missions[num].notes){
1576                                         free(Campaign.missions[num].notes);
1577                                 }
1578
1579                                 Campaign.missions[num].notes = (char *) malloc(strlen(notes) + 1);
1580                                 SDL_strlcpy(Campaign.missions[num].notes, notes, strlen(notes) + 1);
1581                         }
1582                 }
1583
1584                 // skip to events section in the mission file.  Events come before goals, so we process them first
1585                 if ( skip_to_string("#Events") ) {
1586                         while (1) {
1587                                 if (skip_to_string("$Formula:", "#Goals") != 1){
1588                                         break;
1589                                 }
1590
1591                                 z = skip_to_string("+Name:", "$Formula:");
1592                                 if (!z){
1593                                         break;
1594                                 }
1595
1596                                 if (z == 1){
1597                                         stuff_string(events[event_count], F_NAME, NULL);
1598                                 } else {
1599                                         SDL_snprintf(events[event_count], NAME_LENGTH, NOX("Event #%d"), event_count + 1);
1600                                 }
1601
1602                                 event_count++;
1603                                 SDL_assert(event_count < MAX_MISSION_EVENTS);
1604                         }
1605                 }
1606
1607                 if (skip_to_string("#Goals")) {
1608                         while (1) {
1609                                 if (skip_to_string("$Type:", "#End") != 1){
1610                                         break;
1611                                 }
1612
1613                                 z = skip_to_string("+Name:", "$Type:");
1614                                 if (!z){
1615                                         break;
1616                                 }
1617
1618                                 if (z == 1){
1619                                         stuff_string(goals[count], F_NAME, NULL);
1620                                 } else {
1621                                         SDL_snprintf(goals[count], NAME_LENGTH, NOX("Goal #%d"), count + 1);
1622                                 }
1623
1624                                 count++;
1625                                 SDL_assert(count < MAX_GOALS);
1626                         }
1627                 }
1628         } catch (parse_error_t rval) {
1629                 Warning(LOCATION, "Error reading \"%s\" (code = %d)", filename, (int)rval);
1630         }
1631
1632         Campaign.missions[num].num_goals = count;
1633         if (count) {
1634                 Campaign.missions[num].goals = (mgoal *) malloc(count * sizeof(mgoal));
1635                 SDL_assert(Campaign.missions[num].goals);  // make sure we got the memory
1636                 memset(Campaign.missions[num].goals, 0, count * sizeof(mgoal));
1637
1638                 for (i=0; i<count; i++){
1639                         SDL_strlcpy(Campaign.missions[num].goals[i].name, goals[i], SDL_arraysize(Campaign.missions[num].goals[i].name));
1640                 }
1641         }
1642                 // copy the events
1643         Campaign.missions[num].num_events = event_count;
1644         if (event_count) {
1645                 Campaign.missions[num].events = (mevent *)malloc(event_count * sizeof(mevent));
1646                 SDL_assert ( Campaign.missions[num].events );
1647                 memset(Campaign.missions[num].events, 0, event_count * sizeof(mevent));
1648
1649                 for (i = 0; i < event_count; i++ ){
1650                         SDL_strlcpy(Campaign.missions[num].events[i].name, events[i], SDL_arraysize(Campaign.missions[num].events[i].name));
1651                 }
1652         }
1653
1654         // close localization
1655         lcl_ext_close();
1656 }
1657
1658 // function to return index into Campaign's list of missions of the mission with the given
1659 // filename.  This function tried to be a little smart about filename looking for the .fsm
1660 // extension since filenames are stored with the extension in the campaign file.  Returns
1661 // index of mission in campaign structure.  -1 if mission name not found.
1662 int mission_campaign_find_mission( const char *name )
1663 {
1664         int i;
1665         char realname[MAX_FILENAME_LEN];
1666
1667         // look for an extension on the file.  If no extension, add default ".fsm" onto the
1668         // end of the filename
1669         if ( SDL_strchr(name, '.') == NULL ){
1670                 SDL_snprintf(realname, SDL_arraysize(realname), NOX("%s%s"), name, FS_MISSION_FILE_EXT );
1671         } else {
1672                 SDL_strlcpy(realname, name, SDL_arraysize(realname));
1673         }
1674
1675         for (i = 0; i < Campaign.num_missions; i++ ) {
1676                 if ( !SDL_strcasecmp(realname, Campaign.missions[i].name) ){
1677                         return i;
1678                 }
1679         }
1680
1681         return -1;
1682 }
1683
1684 void mission_campaign_maybe_play_movie(int type)
1685 {
1686         int mission;
1687         char *filename;
1688
1689         // only support pre mission movies for now.
1690         SDL_assert ( type == CAMPAIGN_MOVIE_PRE_MISSION );
1691
1692         if ( !(Game_mode & GM_CAMPAIGN_MODE) )
1693                 return;
1694
1695         mission = Campaign.current_mission;
1696         SDL_assert( mission != -1 );
1697
1698         // get a possible filename for a movie to play.
1699         filename = NULL;
1700         switch( type ) {
1701         case CAMPAIGN_MOVIE_PRE_MISSION:
1702                 if ( strlen(Campaign.missions[mission].briefing_cutscene) )
1703                         filename = Campaign.missions[mission].briefing_cutscene;
1704                 break;
1705
1706         default:
1707                 Int3();
1708                 break;
1709         }
1710
1711         // no filename, no movie!
1712         if ( !filename )
1713                 return;
1714
1715         movie_play( filename );
1716 }
1717
1718 // return nonzero if the passed filename is a multiplayer campaign, 0 otherwise
1719 int mission_campaign_parse_is_multi(const char *filename, char *name, const int max_len)
1720 {       
1721         int i;
1722         char temp[50];
1723
1724         try {
1725                 read_file_text( filename );
1726                 reset_parse();
1727
1728                 required_string("$Name:");
1729                 stuff_string( temp, F_NAME, NULL );
1730                 if ( name )
1731                         SDL_strlcpy( name, temp, max_len );
1732
1733                 required_string( "$Type:" );
1734                 stuff_string( temp, F_NAME, NULL );
1735
1736                 for (i = 0; i < MAX_CAMPAIGN_TYPES; i++ ) {
1737                         if ( !SDL_strcasecmp(temp, campaign_types[i]) ) {
1738                                 return i;
1739                         }
1740                 }
1741         } catch (parse_error_t rval) {
1742                 Error(LOCATION, "Unable to parse %s!  Code = %i.\n", filename, (int)rval);
1743         }
1744
1745         Error(LOCATION, "Unknown campaign type %s", temp );
1746         return -1;
1747 }
1748
1749 // functions to save persistent information during a mission -- which then might or might not get
1750 // saved out when the mission is over depending on whether player replays mission or commits.
1751 void mission_campaign_save_persistent( int type, int sindex )
1752 {
1753         // based on the type of information, save it off for possible saving into the campsign
1754         // savefile when the mission is over
1755         if ( type == CAMPAIGN_PERSISTENT_SHIP ) {
1756                 SDL_assert( Num_granted_ships < MAX_SHIP_TYPES );
1757                 Granted_ships[Num_granted_ships] = sindex;
1758                 Num_granted_ships++;
1759         } else if ( type == CAMPAIGN_PERSISTENT_WEAPON ) {
1760                 SDL_assert( Num_granted_weapons < MAX_WEAPON_TYPES );
1761                 Granted_weapons[Num_granted_weapons] = sindex;
1762                 Num_granted_weapons++;
1763         } else
1764                 Int3();
1765 }
1766
1767 // returns 0: loaded, !0: error
1768 int mission_load_up_campaign()
1769 {
1770         if (strlen(Player->current_campaign))
1771                 return mission_campaign_load(Player->current_campaign);
1772         else
1773                 return mission_campaign_load(BUILTIN_CAMPAIGN);
1774 }
1775
1776 // for end of campaign in the single player game.  Called when the end of campaign state is
1777 // entered, which is triggered when the end-campaign sexpression is hit
1778
1779 void mission_campaign_end_init()
1780 {
1781         // no need to do any initialization.
1782 }
1783
1784 void mission_campaign_end_do()
1785 {
1786         // play the movies
1787         event_music_level_close();
1788         mission_goal_fail_incomplete();
1789         scoring_level_close();
1790         mission_campaign_mission_over();
1791
1792 #ifdef MAKE_FS1
1793         movie_play("endgame.mve");
1794 #else
1795         // eventually we'll want to play one of two options (good ending or bad ending)
1796         // did the supernova blow?
1797         if(Supernova_status == SUPERNOVA_HIT){
1798                 movie_play_two("endpart1.mve", "endprt2b.mve");                 // good ending
1799         } else {
1800                 movie_play_two("endpart1.mve", "endprt2a.mve");                 // good ending
1801         }
1802 #endif
1803
1804 #if defined(FS2_DEMO) || defined(FS1_DEMO)
1805         gameseq_post_event( GS_EVENT_END_DEMO );
1806 #elif defined(MAKE_FS1)
1807         gameseq_post_event( GS_STATE_END_OF_CAMPAIGN );
1808 #else
1809         gameseq_post_event( GS_EVENT_MAIN_MENU );
1810 #endif
1811 }
1812
1813 void mission_campaign_end_close()
1814 {
1815         // nothing to do here.
1816 }
1817
1818
1819 // skip to the next mission in the campaign
1820 // this also posts the state change by default.  pass 0 to override that
1821 void mission_campaign_skip_to_next(int start_game)
1822 {
1823         // mark all goals/events complete
1824         // these do not really matter, since is-previous-event-* and is-previous-goal-* sexps check
1825         // to see if the mission was skipped, and use defaults accordingly.
1826         mission_goal_mark_objectives_complete();
1827         mission_goal_mark_events_complete();
1828
1829         // mark mission as skipped
1830         Campaign.missions[Campaign.current_mission].flags |= CMISSION_FLAG_SKIPPED;
1831
1832         // store
1833         mission_campaign_store_goals_and_events();
1834
1835         // now set the next mission
1836         mission_campaign_eval_next_mission();
1837
1838         // clear out relevant player vars
1839         Player->failures_this_session = 0;
1840         Player->show_skip_popup = 1;
1841
1842         if (start_game) {
1843                 // proceed to next mission or main hall
1844                 if ((Campaign.missions[Campaign.current_mission].has_mission_loop) && (Campaign.loop_mission != -1)) {
1845                         // go to loop solicitation
1846                         gameseq_post_event(GS_EVENT_LOOP_BRIEF);
1847                 } else {
1848                         // closes out mission stuff, sets up next one
1849                         mission_campaign_mission_over();
1850
1851                         if ( Campaign.next_mission == -1 ) {
1852                                 // go to main hall, tha campaign is over!
1853                                 gameseq_post_event(GS_EVENT_MAIN_MENU);
1854                         } else {
1855                                 // go to next mission
1856                                 gameseq_post_event(GS_EVENT_START_GAME);
1857                         }
1858                 }
1859         }
1860 }
1861
1862
1863 // breaks your ass out of the loop
1864 // this also posts the state change
1865 void mission_campaign_exit_loop()
1866 {
1867         // set campaign to loop reentry point
1868         Campaign.next_mission = Campaign.loop_reentry;
1869         Campaign.current_mission = -1;
1870         Campaign.loop_enabled = 0;
1871
1872         // set things up for next mission
1873         mission_campaign_next_mission();
1874         gameseq_post_event(GS_EVENT_START_GAME);
1875 }
1876
1877
1878 // used for jumping to a particular campaign mission
1879 // all pvs missions marked skipped
1880 // this relies on correct mission ordering in the campaign file
1881 void mission_campaign_jump_to_mission(const char *name)
1882 {
1883         int i = 0;
1884         char dest_name[64] = { 0 };
1885
1886         // load in the campaign junk
1887         mission_load_up_campaign();
1888
1889         // tack the .fs2 onto the input name
1890         SDL_strlcpy(dest_name, cf_add_ext(name, FS_MISSION_FILE_EXT), SDL_arraysize(dest_name));
1891
1892         // search for our mission
1893         for (i=0; i<Campaign.num_missions; i++) {
1894                 if ((Campaign.missions[i].name != NULL) && !SDL_strcasecmp(Campaign.missions[i].name, dest_name) ) {
1895                         Campaign.next_mission = i;
1896                         Campaign.prev_mission = i-1;
1897                         mission_campaign_next_mission();
1898                         Game_mode |= GM_CAMPAIGN_MODE;
1899                         gameseq_post_event(GS_EVENT_START_GAME);
1900                         return;
1901                 } else {
1902                         Campaign.missions[i].flags |= CMISSION_FLAG_SKIPPED;
1903                         Campaign.num_missions_completed = i;
1904                 }
1905         }
1906
1907         // if we got here, no match was found
1908         // restart the campaign
1909         mission_campaign_savefile_delete(Campaign.filename);
1910         mission_campaign_load(Campaign.filename);
1911 }
1912