2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
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
10 * $Logfile: /Freespace2/code/Mission/MissionCampaign.cpp $
15 * source for dealing with campaigns
18 * Revision 1.8 2003/06/11 18:30:33 taylor
21 * Revision 1.7 2003/05/25 02:30:42 taylor
24 * Revision 1.6 2002/07/24 00:20:42 relnev
27 * Revision 1.5 2002/06/21 03:34:05 relnev
28 * implemented a stub and fixed a path
30 * Revision 1.4 2002/06/09 04:41:22 relnev
31 * added copyright header
33 * Revision 1.3 2002/06/09 03:16:04 relnev
36 * removed unneeded asm, old sdl 2d setup.
38 * fixed crash caused by opengl_get_region.
40 * Revision 1.2 2002/06/02 04:26:34 relnev
43 * Revision 1.1.1.1 2002/05/03 03:28:09 root
47 * 23 9/14/99 4:35a Dave
48 * Argh. Added all kinds of code to handle potential crashes in debriefing
51 * 22 9/09/99 11:40p Dave
52 * Handle an Assert() in beam code. Added supernova sounds. Play the right
53 * 2 end movies properly, based upon what the player did in the mission.
55 * 21 9/09/99 9:34a Jefff
56 * fixed a potential exit-loop bug
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.
63 * 19 9/07/99 2:19p Jefff
64 * clear skip mission player vars on mission skip
66 * 18 9/06/99 9:45p Jefff
67 * break out of loop and skip mission support
69 * 17 9/06/99 6:38p Dave
70 * Improved CD detection code.
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
78 * 15 8/27/99 12:04a Dave
79 * Campaign loop screen.
81 * 14 8/04/99 5:36p Andsager
82 * Show upsell screens at end of demo campaign before returning to main
85 * 13 2/05/99 3:50p Anoop
86 * Removed dumb campaign mission stats saving from multiplayer.
88 * 12 12/17/98 2:43p Andsager
89 * Modify fred campaign save file to include optional mission loops
91 * 11 12/12/98 3:17p Andsager
92 * Clean up mission eval, goal, event and mission scoring.
94 * 10 12/10/98 9:59a Andsager
95 * Fix some bugs with mission loops
97 * 9 12/09/98 1:56p Andsager
98 * Initial checkin of mission loop
100 * 8 11/05/98 5:55p Dave
101 * Big pass at reducing #includes
103 * 7 11/03/98 4:48p Johnson
104 * Fixed campaign file versioning bug left over from Silent Threat port.
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.
110 * 5 10/13/98 2:47p Andsager
111 * Remove reference to Tech_shivan_species_avail
113 * 4 10/13/98 9:28a Dave
114 * Started neatening up freespace.h. Many variables renamed and
115 * reorganized. Added AlphaColors.[h,cpp]
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
122 * 2 10/07/98 10:53a Dave
125 * 1 10/07/98 10:49a Dave
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.
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.
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.
143 * 92 6/17/98 9:30a Allender
144 * fixed red alert replay stats clearing problem
146 * 91 6/01/98 11:43a John
147 * JAS & MK: Classified all strings for localization.
149 * 90 5/25/98 1:29p Allender
150 * end mission sequencing
152 * 89 5/21/98 9:25p Allender
153 * endgame movie always viewable at end of campaign
155 * 88 5/13/98 5:14p Allender
156 * red alert support to go back to previous mission
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.
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.
166 * 85 5/05/98 12:19p Dave
167 * campaign description goes *after* num players
169 * 84 5/04/98 5:52p Comet
170 * Fixed bug with Galatea/Bastion selection when finishing missions.
172 * 83 5/01/98 2:46p Duncan
173 * fix a cfile problem with campaigns related to the new cfile stuff
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.
179 * 81 4/30/98 7:01p Duncan
180 * AL: don't allow deletion of campaign files in multiplayer
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.
194 #include <sys/types.h>
195 #include <sys/stat.h>
206 #include "missioncampaign.h"
207 #include "gamesequence.h"
210 #include "missionload.h"
211 #include "freespace.h"
215 #include "missiongoals.h"
218 #include "techmenu.h"
219 #include "eventmusic.h"
220 #include "alphacolors.h"
221 #include "localize.h"
222 #include "supernova.h"
224 // mission disk stuff
225 #define CAMPAIGN_SAVEFILE_MAX_SHIPS_OLD 75
226 #define CAMPAIGN_SAVEFILE_MAX_WEAPONS_OLD 44
228 #define CAMPAIGN_INITIAL_RELEASE_FILE_VERSION 6
230 // campaign wasn't ended
231 int Campaign_ended_in_mission = 0;
233 // stuff for selecting campaigns. We need to keep both arrays around since we display the
234 // list of campaigns by name, but must load campaigns by filename
235 char *Campaign_names[MAX_CAMPAIGNS];
236 char *Campaign_file_names[MAX_CAMPAIGNS];
239 const char *campaign_types[MAX_CAMPAIGN_TYPES] =
248 // modules local variables to deal with getting new ships/weapons available to the player
249 int Num_granted_ships, Num_granted_weapons; // per mission counts of new ships and weapons
250 int Granted_ships[MAX_SHIP_TYPES];
251 int Granted_weapons[MAX_WEAPON_TYPES];
253 // variables to control the UI stuff for loading campaigns
254 LOCAL UI_WINDOW Campaign_window;
255 LOCAL UI_LISTBOX Campaign_listbox;
256 LOCAL UI_BUTTON Campaign_okb, Campaign_cancelb;
261 // variables with deal with the campaign save file
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 #define CAMPAIGN_FILE_ID 0xbeefcafe
267 // variables with deal with the campaign stats save file
268 #define CAMPAIGN_STATS_FILE_VERSION 1
269 #define CAMPAIGN_STATS_FILE_COMPATIBLE_VERSION 1
270 #define CAMPAIGN_STATS_FILE_ID 0xabbadaad
272 // mission_campaign_get_name returns a string (which is malloced in this routine) of the name
273 // of the given freespace campaign file. In the type field, we return if the campaign is a single
274 // player or multiplayer campaign. The type field will only be valid if the name returned is non-NULL
275 int mission_campaign_get_info(const char *filename, char *name, int *type, int *max_players, char **desc)
278 char campaign_type[NAME_LENGTH], fname[MAX_FILENAME_LEN];
280 Assert( name != NULL );
281 Assert( type != NULL );
286 strcpy(fname, filename);
287 if ((strlen(fname) < 4) || stricmp(fname + strlen(fname) - 4, FS_CAMPAIGN_FILE_EXT)){
288 strcat(fname, FS_CAMPAIGN_FILE_EXT);
291 Assert(strlen(fname) < MAX_FILENAME_LEN);
293 if ((rval = setjmp(parse_abort)) != 0) {
295 // close localization
301 Error(LOCATION, "Error parsing '%s'\r\nError code = %i.\r\n", fname, rval);
304 read_file_text( fname );
306 required_string("$Name:");
308 stuff_string( name, F_NAME, NULL );
309 if ( name == NULL ) {
311 nprintf(("Warning", "No name found for campaign file %s\n", filename));
313 // close localization
319 required_string( "$Type:" );
320 stuff_string( campaign_type, F_NAME, NULL );
323 for (i=0; i<MAX_CAMPAIGN_TYPES; i++) {
324 if ( !stricmp(campaign_type, campaign_types[i]) ) {
331 if (optional_string("+Description:")) {
332 *desc = stuff_and_malloc_string(F_MULTITEXT, NULL, MISSION_DESC_LENGTH);
336 // if this is a multiplayer campaign, get the max players
338 skip_to_string("+Num Players:");
339 stuff_int(max_players);
342 // if we found a valid campaign type
344 // close localization
351 Int3(); // get Allender -- incorrect type found
353 // close localization
359 // parses campaign and returns a list of missions in it. Returns number of missions added to
360 // the 'list', and up to 'max' missions may be added to 'list'. Returns negative on error.
362 int mission_campaign_get_mission_list(const char *filename, char **list, int max)
364 int rval, i, num = 0;
365 char name[NAME_LENGTH];
367 filename = cf_add_ext(filename, FS_CAMPAIGN_FILE_EXT);
369 // read the mission file and get the list of mission filenames
370 if ((rval = setjmp(parse_abort)) != 0) {
371 // since we can't return count of allocated elements, free them instead
372 for (i=0; i<num; i++)
378 read_file_text(filename);
381 while (skip_to_string("$Mission:") > 0) {
382 stuff_string(name, F_NAME, NULL);
384 list[num++] = strdup(name);
391 void mission_campaign_maybe_add( const char *filename, int multiplayer )
393 char name[NAME_LENGTH];
394 int type,max_players;
396 if ( mission_campaign_get_info( filename, name, &type, &max_players) ) {
397 if ( !multiplayer && ( type == CAMPAIGN_TYPE_SINGLE) ) {
398 Campaign_names[Num_campaigns] = strdup(name);
399 Campaign_file_names[Num_campaigns] = strdup(filename);
405 // mission_campaign_build_list() builds up the list of campaigns that the user might
406 // be able to pick from. It uses the multiplayer flag to tell if we should display a list
407 // of single or multiplayer campaigns. This routine sets the Num_campaigns and Campaign_names
409 void mission_campaign_build_list( int multiplayer )
417 mission_campaign_maybe_add( BUILTIN_CAMPAIGN, multiplayer);
419 strcpy(wild_card, "Data/Missions");
421 dirp = opendir(wild_card);
423 while ((dir = readdir(dirp)) != NULL) {
424 if ( Num_campaigns >= MAX_CAMPAIGNS )
427 if (fnmatch("*"FS_CAMPAIGN_FILE_EXT, dir->d_name, 0) == 0) {
428 if (stricmp(dir->d_name, BUILTIN_CAMPAIGN) == 0)
432 snprintf(fn, MAX_PATH-1, "%s/%s", wild_card, dir->d_name);
436 if (stat(fn, &buf) == -1) {
440 if (!S_ISREG(buf.st_mode)) {
444 mission_campaign_maybe_add(dir->d_name, multiplayer);
455 mission_campaign_maybe_add( BUILTIN_CAMPAIGN, multiplayer);
457 memset(wild_card, 0, 256);
458 strcpy(wild_card, NOX("data\\missions\\*"));
459 strcat(wild_card, FS_CAMPAIGN_FILE_EXT);
460 find_handle = _findfirst( wild_card, &find );
461 if( find_handle != -1 ) {
462 if ( !(find.attrib & _A_SUBDIR) && stricmp(find.name, BUILTIN_CAMPAIGN) ){
463 mission_campaign_maybe_add( find.name, multiplayer);
466 while( !_findnext( find_handle, &find ) ) {
467 if ( !(find.attrib & _A_SUBDIR) && stricmp(find.name, BUILTIN_CAMPAIGN) ) {
468 if ( Num_campaigns >= MAX_CAMPAIGNS ){
469 //MessageBox( -2,-2, 1, "Only the first 300 files will be displayed.", "Ok" );
472 mission_campaign_maybe_add( find.name, multiplayer);
480 // gets optional ship/weapon information
481 void mission_campaign_get_sw_info()
483 int i, count, ship_list[MAX_SHIP_TYPES], weapon_list[MAX_WEAPON_TYPES];
485 // set allowable ships to the SIF_PLAYER_SHIPs
486 memset( Campaign.ships_allowed, 0, sizeof(Campaign.ships_allowed) );
487 for (i = 0; i < MAX_SHIP_TYPES; i++ ) {
488 if ( Ship_info[i].flags & SIF_PLAYER_SHIP )
489 Campaign.ships_allowed[i] = 1;
492 for (i = 0; i < MAX_WEAPON_TYPES; i++ )
493 Campaign.weapons_allowed[i] = 1;
495 if ( optional_string("+Starting Ships:") ) {
496 for (i = 0; i < MAX_SHIP_TYPES; i++ )
497 Campaign.ships_allowed[i] = 0;
499 count = stuff_int_list(ship_list, MAX_SHIP_TYPES, SHIP_INFO_TYPE);
501 // now set the array elements stating which ships we are allowed
502 for (i = 0; i < count; i++ ) {
503 if ( Ship_info[ship_list[i]].flags & SIF_PLAYER_SHIP )
504 Campaign.ships_allowed[ship_list[i]] = 1;
508 if ( optional_string("+Starting Weapons:") ) {
509 for (i = 0; i < MAX_WEAPON_TYPES; i++ )
510 Campaign.weapons_allowed[i] = 0;
512 count = stuff_int_list(weapon_list, MAX_WEAPON_TYPES, WEAPON_POOL_TYPE);
514 // now set the array elements stating which ships we are allowed
515 for (i = 0; i < count; i++ )
516 Campaign.weapons_allowed[weapon_list[i]] = 1;
520 // mission_campaign_load starts a new campaign. It reads in the mission information in the campaign file
521 // It also sets up all variables needed inside of the game to deal with starting mission numbers, etc
523 // Note: Due to difficulties in generalizing this function, parts of it are duplicated throughout
524 // this file. If you change the format of the campaign file, you should be sure these related
525 // functions work properly and update them if it breaks them.
526 int mission_campaign_load( const char *filename, int load_savefile )
529 char name[NAME_LENGTH], type[NAME_LENGTH];
531 filename = cf_add_ext(filename, FS_CAMPAIGN_FILE_EXT);
536 // read the mission file and get the list of mission filenames
537 if ((rval = setjmp(parse_abort)) != 0) {
538 mprintf(("Error parsing '%s'\r\nError code = %i.\r\n", filename, rval));
540 // close localization
543 return CAMPAIGN_ERROR_CORRUPT;
546 // be sure to remove all old malloced strings of Mission_names
547 // we must also free any goal stuff that was from a previous campaign
548 // this also frees sexpressions so the next call to init_sexp will be able to reclaim
549 // nodes previously used by another campaign.
550 mission_campaign_close();
552 strcpy( Campaign.filename, filename );
554 // only initialize the sexpression stuff when Fred isn't running. It'll screw things up major
556 if ( !Fred_running ){
557 init_sexp(); // must initialize the sexpression stuff
560 read_file_text( filename );
562 memset( &Campaign, 0, sizeof(Campaign) );
564 // copy filename to campaign structure minus the extension
565 len = strlen(filename) - 4;
566 Assert(len < MAX_FILENAME_LEN);
567 strncpy(Campaign.filename, filename, len);
568 Campaign.filename[len] = 0;
570 required_string("$Name:");
571 stuff_string( name, F_NAME, NULL );
573 //Store campaign name in the global struct
574 strcpy( Campaign.name, name );
576 required_string( "$Type:" );
577 stuff_string( type, F_NAME, NULL );
579 for (i = 0; i < MAX_CAMPAIGN_TYPES; i++ ) {
580 if ( !stricmp(type, campaign_types[i]) ) {
586 if ( i == MAX_CAMPAIGN_TYPES )
587 Error(LOCATION, "Unknown campaign type %s!", type);
589 Campaign.desc = NULL;
590 if (optional_string("+Description:"))
591 Campaign.desc = stuff_and_malloc_string(F_MULTITEXT, NULL, MISSION_DESC_LENGTH);
593 // if the type is multiplayer -- get the number of players
594 Campaign.num_players = 0;
595 if ( Campaign.type != CAMPAIGN_TYPE_SINGLE) {
596 required_string("+Num players:");
597 stuff_int( &(Campaign.num_players) );
601 // check if mission disk -
602 // doesn't do anything but prevent non fatal error messages
603 optional_string("+Missiondisk");
606 // parse the optional ship/weapon information
607 mission_campaign_get_sw_info();
609 // parse the mission file and actually read in the mission stuff
610 Campaign.num_missions = 0;
611 while ( required_string_either("#End", "$Mission:") ) {
614 required_string("$Mission:");
615 stuff_string(name, F_NAME, NULL);
616 cm = &Campaign.missions[Campaign.num_missions];
617 cm->name = strdup(name);
619 cm->briefing_cutscene[0] = 0;
620 if ( optional_string("+Briefing Cutscene:") )
621 stuff_string( cm->briefing_cutscene, F_NAME, NULL );
624 if (optional_string("+Flags:"))
625 stuff_int(&cm->flags);
628 if ( optional_string("+Formula:") ) {
629 cm->formula = get_sexp_main();
630 if ( !Fred_running ) {
631 Assert ( cm->formula != -1 );
632 sexp_mark_persistent( cm->formula );
635 if ( cm->formula == -1 ){
636 // close localization
639 return CAMPAIGN_ERROR_SEXP_EXHAUSTED;
644 // Do misison looping stuff
645 cm->has_mission_loop = 0;
646 if ( optional_string("+Mission Loop:") ) {
647 cm->has_mission_loop = 1;
650 cm->mission_loop_desc = NULL;
651 if ( optional_string("+Mission Loop Text:")) {
652 cm->mission_loop_desc = stuff_and_malloc_string(F_MULTITEXT, NULL, MISSION_DESC_LENGTH);
655 cm->mission_loop_brief_anim = NULL;
656 if ( optional_string("+Mission Loop Brief Anim:")) {
657 cm->mission_loop_brief_anim = stuff_and_malloc_string(F_MULTITEXT, NULL, MAX_FILENAME_LEN);
660 cm->mission_loop_brief_sound = NULL;
661 if ( optional_string("+Mission Loop Brief Sound:")) {
662 cm->mission_loop_brief_sound = stuff_and_malloc_string(F_MULTITEXT, NULL, MAX_FILENAME_LEN);
665 cm->mission_loop_formula = -1;
666 if ( optional_string("+Formula:") ) {
667 cm->mission_loop_formula = get_sexp_main();
668 if ( !Fred_running ) {
669 Assert ( cm->mission_loop_formula != -1 );
670 sexp_mark_persistent( cm->mission_loop_formula );
673 if ( cm->mission_loop_formula == -1 ){
674 // close localization
677 return CAMPAIGN_ERROR_SEXP_EXHAUSTED;
682 if (optional_string("+Level:")) {
683 stuff_int( &cm->level );
684 if ( cm->level == 0 ) // check if the top (root) of the whole tree
685 Campaign.next_mission = Campaign.num_missions;
688 Campaign.realign_required = 1;
690 if (optional_string("+Position:"))
691 stuff_int( &cm->pos );
693 Campaign.realign_required = 1;
707 Campaign.num_missions++;
711 // set up the other variables for the campaign stuff. After initializing, we must try and load
712 // the campaign save file for this player. Since all campaign loads go through this routine, I
713 // think this place should be the only necessary place to load the campaign save stuff. The campaign
714 // save file will get written when a mission has ended by player choice.
715 Campaign.next_mission = 0;
716 Campaign.prev_mission = -1;
717 Campaign.current_mission = -1;
719 // loading the campaign will get us to the current and next mission that the player must fly
720 // plus load all of the old goals that future missions might rely on.
721 if (!Fred_running && load_savefile && (Campaign.type == CAMPAIGN_TYPE_SINGLE)) {
722 mission_campaign_savefile_load(Campaign.filename);
725 // close localization
731 // mission_campaign_load_by_name() loads up a freespace campaign given the filename. This routine
732 // is used to load up campaigns when a pilot file is loaded. Generally, the
733 // filename will probably be the freespace campaign file, but not necessarily.
734 int mission_campaign_load_by_name( const char *filename )
736 char real_filename[MAX_FILENAME_LEN+1] = { 0 };
737 char name[NAME_LENGTH],test[5];
738 int type,max_players;
740 // make sure to tack on .fsc on the end if its not there already
741 if(strlen(filename) > 0){
742 strncpy(real_filename, filename, MAX_FILENAME_LEN);
744 if(strlen(real_filename) > 4){
745 strcpy(test,real_filename+(strlen(real_filename)-4));
746 if(strcmp(test, FS_CAMPAIGN_FILE_EXT)!=0){
747 strcat(real_filename, FS_CAMPAIGN_FILE_EXT);
750 strcat(real_filename, FS_CAMPAIGN_FILE_EXT);
753 Error(LOCATION,"Tried to load campaign file with illegal length/extension!");
756 if (!mission_campaign_get_info(real_filename, name, &type, &max_players)){
761 Campaign_file_names[Num_campaigns] = real_filename;
762 Campaign_names[Num_campaigns] = name;
764 mission_campaign_load(real_filename);
768 int mission_campaign_load_by_name_csfe( const char *filename, const char *callsign )
770 Game_mode |= GM_NORMAL;
771 strcpy(Player->callsign, callsign);
772 return mission_campaign_load_by_name( filename);
776 // mission_campaign_init initializes some variables then loads the default Freespace single player campaign.
777 void mission_campaign_init()
779 memset(&Campaign, 0, sizeof(Campaign) );
782 // Fill in the root of the campaign save filename
783 void mission_campaign_savefile_generate_root(char *filename)
785 char base[_MAX_FNAME];
787 Assert ( strlen(Campaign.filename) != 0 );
789 // build up the filename for the save file. There could be a problem with filename length,
790 // but this problem can get fixed in several ways -- ignore the problem for now though.
791 _splitpath( Campaign.filename, NULL, NULL, base, NULL );
792 Assert ( (strlen(base) + strlen(Player->callsign) + 1) < _MAX_FNAME );
794 sprintf( filename, NOX("%s.%s."), Player->callsign, base );
797 // mission_campaign_savefile_save saves the state of the campaign. This function will probably always be called
798 // then the player is done flying a mission in the campaign path. It will save the missions played, the
799 // state of the goals, etc.
801 int mission_campaign_savefile_save()
803 char filename[_MAX_FNAME];
805 int i,j, mission_count;
807 memset(filename, 0, _MAX_FNAME);
808 mission_campaign_savefile_generate_root(filename);
810 // name the file differently depending on whether we're in single player or multiplayer mode
811 // single player : *.csg
812 strcat( filename, NOX("csg"));
814 fp = cfopen(filename,"wb", CFILE_NORMAL, CF_TYPE_SINGLE_PLAYERS);
819 // Write out campaign file info
820 cfwrite_int( CAMPAIGN_FILE_ID,fp );
821 cfwrite_int( CAMPAIGN_FILE_VERSION,fp );
823 // put in the file signature (single or multiplayer campaign) - see MissionCampaign.h for the #defines
824 cfwrite_int( CAMPAIGN_SINGLE_PLAYER_SIG, fp );
826 // do we need to write out the filename of the campaign?
827 cfwrite_string_len( Campaign.filename, fp );
828 cfwrite_int( Campaign.prev_mission, fp );
829 cfwrite_int( Campaign.next_mission, fp );
830 cfwrite_int( Campaign.loop_reentry, fp );
831 cfwrite_int( Campaign.loop_enabled, fp );
833 // write out the information for ships/weapons which this player is allowed to use
834 cfwrite_int(Num_ship_types, fp);
835 cfwrite_int(Num_weapon_types, fp);
836 for ( i = 0; i < Num_ship_types; i++ ){
837 cfwrite_char( Campaign.ships_allowed[i], fp );
840 for ( i = 0; i < Num_weapon_types; i++ ){
841 cfwrite_char( Campaign.weapons_allowed[i], fp );
844 // write out the completed mission matrix. Used to tell which missions the player
845 // can replay in the simulator. Also, each completed mission contains a list of the goals
846 // that were in the mission along with the goal completion status.
847 cfwrite_int( Campaign.num_missions_completed, fp );
848 for (i = 0; i < MAX_CAMPAIGN_MISSIONS; i++ ) {
849 if ( Campaign.missions[i].completed ) {
850 cfwrite_int( i, fp );
851 cfwrite_int( Campaign.missions[i].num_goals, fp );
852 for ( j = 0; j < Campaign.missions[i].num_goals; j++ ) {
853 cfwrite_string_len( Campaign.missions[i].goals[j].name, fp );
854 cfwrite_char( Campaign.missions[i].goals[j].status, fp );
856 cfwrite_int( Campaign.missions[i].num_events, fp );
857 for ( j = 0; j < Campaign.missions[i].num_events; j++ ) {
858 cfwrite_string_len( Campaign.missions[i].events[j].name, fp );
859 cfwrite_char( Campaign.missions[i].events[j].status, fp );
863 cfwrite_int(Campaign.missions[i].flags, fp);
870 // ugh! due to horrible bug, the stats saved at the end of every level were not written
871 // out to disk. Write out a seperate file to do this. We will only read it in if we actually
873 memset(filename, 0, _MAX_FNAME);
874 mission_campaign_savefile_generate_root(filename);
876 // name the file differently depending on whether we're in single player or multiplayer mode
877 // single player : *.csg
878 strcat( filename, NOX("css"));
880 fp = cfopen(filename,"wb", CFILE_NORMAL, CF_TYPE_SINGLE_PLAYERS);
885 // Write out campaign file info
886 cfwrite_int( CAMPAIGN_STATS_FILE_ID,fp );
887 cfwrite_int( CAMPAIGN_STATS_FILE_VERSION,fp );
889 // determine how many missions we are saving -- I think that this method is safer than the method
890 // I used for release
892 for ( i = 0; i < Campaign.num_missions; i++ ) {
893 if ( Campaign.missions[i].completed ) {
898 // write out the stats information to disk.
899 cfwrite_int( mission_count, fp );
900 for (i = 0; i < Campaign.num_missions; i++ ) {
901 if ( Campaign.missions[i].completed ) {
902 cfwrite_int( i, fp );
903 cfwrite( &Campaign.missions[i].stats, sizeof(scoring_struct), 1, fp );
912 // The following function always only ever ever ever called by CSFE!!!!!
913 int campaign_savefile_save(const char *pname)
915 if (Campaign.type == CAMPAIGN_TYPE_SINGLE)
916 Game_mode &= ~GM_MULTIPLAYER;
918 Game_mode |= GM_MULTIPLAYER;
920 strcpy(Player->callsign, pname);
921 //memcpy(&Campaign, camp, sizeof(campaign));
922 return mission_campaign_savefile_save();
926 // the below two functions is internal to this module. It is here so that I can group the save/load
927 // functions together.
930 // mission_campaign_savefile_delete deletes any save file in the players directory for the given
932 void mission_campaign_savefile_delete( const char *cfilename, int is_multi )
934 char filename[_MAX_FNAME], base[_MAX_FNAME];
936 _splitpath( cfilename, NULL, NULL, base, NULL );
938 if ( Player->flags & PLAYER_FLAGS_IS_MULTI ) {
939 return; // no such thing as a multiplayer campaign savefile
942 sprintf( filename, NOX("%s.%s.csg"), Player->callsign, base );
944 cf_delete( filename, CF_TYPE_SINGLE_PLAYERS );
947 void campaign_delete_save( const char *cfn, const char *pname)
949 strcpy(Player->callsign, pname);
950 mission_campaign_savefile_delete(cfn);
953 // next function deletes all the save files for this particular pilot. Just call cfile function
954 // which will delete multiple files
955 // Player_select_mode tells us whether we are deleting single or multiplayer files
956 void mission_campaign_delete_all_savefiles( const char *pilot_name, int is_multi )
958 int dir_type, num_files, i;
959 char *names[MAX_CAMPAIGNS], spec[MAX_FILENAME_LEN + 2];
962 int (*filter_save)(const char *filename);
965 return; // can't have multiplayer campaign save files
969 dir_type = CF_TYPE_SINGLE_PLAYERS;
971 sprintf(spec, NOX("%s.*%s"), pilot_name, ext);
973 // HACK HACK HACK HACK!!!! cf_get_file_list is not reentrant. Pretty dumb because it should
974 // be. I have to save any file filters
975 filter_save = Get_file_list_filter;
976 Get_file_list_filter = NULL;
977 num_files = cf_get_file_list(MAX_CAMPAIGNS, names, dir_type, spec);
978 Get_file_list_filter = filter_save;
980 for (i=0; i<num_files; i++) {
981 strcpy(filename, names[i]);
982 strcat(filename, ext);
983 cf_delete(filename, dir_type);
988 // mission_campaign_savefile_load takes a filename of a campaign file as a parameter and loads all
989 // of the information stored in the campaign file.
990 void mission_campaign_savefile_load( const char *cfilename )
992 char filename[_MAX_FNAME], base[_MAX_FNAME];
993 int version, i, num, j, num_stats_blocks;
997 Assert ( strlen(cfilename) != 0 );
999 // probably only called from single player games anymore!!! should be anyway
1000 Assert( Game_mode & GM_NORMAL ); // get allender or DaveB. trying to save campaign in multiplayer
1002 // build up the filename for the save file. There could be a problem with filename length,
1003 // but this problem can get fixed in several ways -- ignore the problem for now though.
1004 _splitpath( cfilename, NULL, NULL, base, NULL );
1005 Assert ( (strlen(base) + strlen(Player->callsign) + 1) < _MAX_FNAME );
1007 if(Game_mode & GM_MULTIPLAYER)
1008 sprintf( filename, NOX("%s.%s.msg"), Player->callsign, base );
1010 sprintf( filename, NOX("%s.%s.csg"), Player->callsign, base );
1012 fp = cfopen(filename, "rb", CFILE_NORMAL, CF_TYPE_SINGLE_PLAYERS );
1016 id = cfread_int( fp );
1017 if ( id != CAMPAIGN_FILE_ID ) {
1018 Warning(LOCATION, "Campaign save file has invalid signature");
1023 version = cfread_int( fp );
1024 if ( version < CAMPAIGN_FILE_COMPATIBLE_VERSION ) {
1025 Warning(LOCATION, "Campaign save file too old -- not compatible. Deleting file.\nYou can continue from here without trouble\n\n");
1027 cf_delete( filename, CF_TYPE_SINGLE_PLAYERS );
1031 // verify that we are loading the correct type of campaign file for the mode that we are in.
1033 type_sig = cfread_int( fp );
1035 type_sig = CAMPAIGN_SINGLE_PLAYER_SIG;
1037 Assert( ((Game_mode & GM_MULTIPLAYER) && (type_sig==CAMPAIGN_MULTI_PLAYER_SIG)) || (!(Game_mode & GM_MULTIPLAYER) && (type_sig==CAMPAIGN_SINGLE_PLAYER_SIG)) );
1039 Campaign.type = type_sig == CAMPAIGN_SINGLE_PLAYER_SIG ? CAMPAIGN_TYPE_SINGLE : CAMPAIGN_TYPE_MULTI_COOP;
1041 // read in the filename of the campaign and compare the filenames to be sure that
1042 // we are reading data that really belongs to this campaign. I think that this check
1044 cfread_string_len( filename, _MAX_FNAME, fp );
1045 /*if ( stricmp( filename, cfilename) ) { // Used to be !stricmp. How did this ever work? --MK, 11/9/97
1046 Warning(LOCATION, "Campaign save file appears corrupt because of mismatching filenames.");
1051 Campaign.prev_mission = cfread_int( fp );
1052 Campaign.next_mission = cfread_int( fp );
1053 Campaign.loop_reentry = cfread_int( fp );
1054 Campaign.loop_enabled = cfread_int( fp );
1056 // load information about ships/weapons allowed
1057 int ship_count, weapon_count;
1059 // if earlier than mission disk version, use old MAX_SHIP_TYPES, otherwise read from the file
1060 if(version <= CAMPAIGN_INITIAL_RELEASE_FILE_VERSION){
1061 ship_count = CAMPAIGN_SAVEFILE_MAX_SHIPS_OLD;
1062 weapon_count = CAMPAIGN_SAVEFILE_MAX_WEAPONS_OLD;
1064 ship_count = cfread_int(fp);
1065 weapon_count = cfread_int(fp);
1068 for ( i = 0; i < ship_count; i++ ){
1069 Campaign.ships_allowed[i] = cfread_ubyte( fp );
1072 for ( i = 0; i < weapon_count; i++ ){
1073 Campaign.weapons_allowed[i] = cfread_ubyte( fp );
1076 // read in the completed mission matrix. Used to tell which missions the player
1077 // can replay in the simulator. Also, each completed mission contains a list of the goals
1078 // that were in the mission along with the goal completion status.
1079 Campaign.num_missions_completed = cfread_int( fp );
1080 for (i = 0; i < Campaign.num_missions_completed; i++ ) {
1081 num = cfread_int( fp );
1082 Campaign.missions[num].completed = 1;
1083 Campaign.missions[num].num_goals = cfread_int( fp );
1085 // be sure to malloc out space for the goals stuff, then zero the memory!!! Don't do malloc
1086 // if there are no goals
1087 Campaign.missions[num].goals = (mgoal *)malloc( Campaign.missions[num].num_goals * sizeof(mgoal) );
1088 if ( Campaign.missions[num].num_goals > 0 ) {
1089 memset( Campaign.missions[num].goals, 0, sizeof(mgoal) * Campaign.missions[num].num_goals );
1090 Assert( Campaign.missions[num].goals != NULL );
1093 // now read in the goal information for this mission
1094 for ( j = 0; j < Campaign.missions[num].num_goals; j++ ) {
1095 cfread_string_len( Campaign.missions[num].goals[j].name, NAME_LENGTH, fp );
1096 Campaign.missions[num].goals[j].status = cfread_char( fp );
1099 // get the events from the savefile
1100 Campaign.missions[num].num_events = cfread_int( fp );
1102 // be sure to malloc out space for the events stuff, then zero the memory!!! Don't do malloc
1103 // if there are no events
1104 // if (Campaign.missions[num].events < 0)
1105 // Campaign.missions[num].events = 0;
1106 Campaign.missions[num].events = (mevent *)malloc( Campaign.missions[num].num_events * sizeof(mevent) );
1107 if ( Campaign.missions[num].num_events > 0 ) {
1108 memset( Campaign.missions[num].events, 0, sizeof(mevent) * Campaign.missions[num].num_events );
1109 Assert( Campaign.missions[num].events != NULL );
1112 // now read in the event information for this mission
1113 for ( j = 0; j < Campaign.missions[num].num_events; j++ ) {
1114 cfread_string_len( Campaign.missions[num].events[j].name, NAME_LENGTH, fp );
1115 Campaign.missions[num].events[j].status = cfread_char( fp );
1119 Campaign.missions[num].flags = cfread_int(fp);
1125 // now, try and read in the campaign stats saved information. This code was added for the 1.03 patch
1126 // since the stats data was never written out to disk. We try and open the file, and if we cannot find
1127 // it, then simply return
1128 sprintf( filename, NOX("%s.%s.css"), Player->callsign, base );
1130 fp = cfopen(filename, "rb", CFILE_NORMAL, CF_TYPE_SINGLE_PLAYERS );
1134 id = cfread_int( fp );
1135 if ( id != CAMPAIGN_STATS_FILE_ID ) {
1136 Warning(LOCATION, "Campaign stats save file has invalid signature");
1141 version = cfread_int( fp );
1142 if ( version < CAMPAIGN_STATS_FILE_COMPATIBLE_VERSION ) {
1143 Warning(LOCATION, "Campaign save file too old -- not compatible. Deleting file.\nYou can continue from here without trouble\n\n");
1145 cf_delete( filename, CF_TYPE_SINGLE_PLAYERS );
1149 num_stats_blocks = cfread_int( fp );
1150 for (i = 0; i < num_stats_blocks; i++ ) {
1151 num = cfread_int( fp );
1152 cfread( &Campaign.missions[num].stats, sizeof(scoring_struct), 1, fp );
1158 // the following code only ever called by CSFE!!!!
1159 void campaign_savefile_load(const char *fname, const char *pname)
1161 if (Campaign.type==CAMPAIGN_TYPE_SINGLE) {
1162 Game_mode &= ~GM_MULTIPLAYER;
1163 Game_mode &= GM_NORMAL;
1166 Game_mode |= GM_MULTIPLAYER;
1167 strcpy(Player->callsign, pname);
1168 mission_campaign_savefile_load(fname);
1171 // mission_campaign_next_mission sets up the internal veriables of the campaign
1172 // structure so the player can play the next mission. If there are no more missions
1173 // available in the campaign, this function returns -1, else 0 if the mission was
1175 int mission_campaign_next_mission()
1177 if ( (Campaign.next_mission == -1) || (strlen(Campaign.name) == 0) ) // will be set to -1 when there is no next mission
1180 Campaign.current_mission = Campaign.next_mission;
1181 strncpy( Game_current_mission_filename, Campaign.missions[Campaign.current_mission].name, MAX_FILENAME_LEN );
1183 // check for end of loop.
1184 if (Campaign.current_mission == Campaign.loop_reentry) {
1185 Campaign.loop_enabled = 0;
1188 // reset the number of persistent ships and weapons for the next campaign mission
1189 Num_granted_ships = 0;
1190 Num_granted_weapons = 0;
1194 // mission_campaign_previous_mission() gets called to go to the previous mission in
1195 // the campaign. Used only for Red Alert missions
1196 int mission_campaign_previous_mission()
1198 if ( !(Game_mode & GM_CAMPAIGN_MODE) )
1201 if ( Campaign.prev_mission == -1 )
1204 Campaign.current_mission = Campaign.prev_mission;
1205 Campaign.next_mission = Campaign.current_mission;
1206 Campaign.num_missions_completed--;
1207 Campaign.missions[Campaign.next_mission].completed = 0;
1208 mission_campaign_savefile_save();
1210 // reset the player stats to be the stats from this level
1211 memcpy( &Player->stats, &Campaign.missions[Campaign.current_mission].stats, sizeof(Player->stats) );
1213 strncpy( Game_current_mission_filename, Campaign.missions[Campaign.current_mission].name, MAX_FILENAME_LEN );
1214 Num_granted_ships = 0;
1215 Num_granted_weapons = 0;
1221 // determine what the next mission is after the current one. Because this evaluates an sexp,
1222 // and that could check just about anything, the results are only going to be valid in
1224 // DA 12/09/98 -- To allow for mission loops, need to maintain call with store stats
1225 int mission_campaign_eval_next_mission( int store_stats )
1231 Campaign.next_mission = -1;
1232 cur = Campaign.current_mission;
1233 name = Campaign.missions[cur].name;
1235 mission = &Campaign.missions[cur];
1237 // first we must save the status of the current missions goals in the campaign mission structure.
1238 // After that, we can determine which mission is tagged as the next mission. Finally, we
1239 // can save the campaign save file
1240 // we might have goal and event status if the player replayed a mission
1241 if ( mission->num_goals > 0 ) {
1242 free( mission->goals );
1245 mission->num_goals = Num_goals;
1246 if ( mission->num_goals > 0 ) {
1247 mission->goals = (mgoal *)malloc( sizeof(mgoal) * Num_goals );
1248 Assert( mission->goals != NULL );
1251 // copy the needed info from the Mission_goal struct to our internal structure
1252 for (i = 0; i < Num_goals; i++ ) {
1253 if ( strlen(Mission_goals[i].name) == 0 ) {
1254 char name[NAME_LENGTH];
1256 sprintf(name, NOX("Goal #%d"), i);
1257 //Warning(LOCATION, "Mission goal in mission %s must have a +Name field! using %s for campaign save file\n", mission->name, name);
1258 strcpy( mission->goals[i].name, name);
1260 strcpy( mission->goals[i].name, Mission_goals[i].name );
1261 Assert ( Mission_goals[i].satisfied != GOAL_INCOMPLETE ); // should be true or false at this point!!!
1262 mission->goals[i].status = (char)Mission_goals[i].satisfied;
1265 // do the same thing for events as we did for goals
1266 // we might have goal and event status if the player replayed a mission
1267 if ( mission->num_events > 0 ) {
1268 free( mission->events );
1271 mission->num_events = Num_mission_events;
1272 if ( mission->num_events > 0 ) {
1273 mission->events = (mevent *)malloc( sizeof(mevent) * Num_mission_events );
1274 Assert( mission->events != NULL );
1277 // copy the needed info from the Mission_goal struct to our internal structure
1278 for (i = 0; i < Num_mission_events; i++ ) {
1279 if ( strlen(Mission_events[i].name) == 0 ) {
1280 char name[NAME_LENGTH];
1282 sprintf(name, NOX("Event #%d"), i);
1283 nprintf(("Warning", "Mission goal in mission %s must have a +Name field! using %s for campaign save file\n", mission->name, name));
1284 strcpy( mission->events[i].name, name);
1286 strcpy( mission->events[i].name, Mission_events[i].name );
1288 // getting status for the events is a little different. If the formula value for the event entry
1289 // is -1, then we know the value of the result field will never change. If the formula is
1290 // not -1 (i.e. still being evaluated at mission end time), we will write "incomplete" for the
1292 if ( Mission_events[i].formula == -1 ) {
1293 if ( Mission_events[i].result )
1294 mission->events[i].status = EVENT_SATISFIED;
1296 mission->events[i].status = EVENT_FAILED;
1301 // maybe store the alltime stats which would be current at the end of this mission
1302 if ( store_stats ) {
1303 memcpy( &mission->stats, &Player->stats, sizeof(Player->stats) );
1304 scoring_backout_accept( &mission->stats );
1307 if ( store_stats ) { // second (last) time through, so use choose loop_mission if chosen
1308 if ( Campaign.loop_enabled ) {
1309 Campaign.next_mission = Campaign.loop_mission;
1311 // evaluate next mission (straight path)
1312 if (Campaign.missions[cur].formula != -1) {
1313 flush_sexp_tree(Campaign.missions[cur].formula); // force formula to be re-evaluated
1314 eval_sexp(Campaign.missions[cur].formula); // this should reset Campaign.next_mission to proper value
1319 // evaluate next mission (straight path)
1320 if (Campaign.missions[cur].formula != -1) {
1321 flush_sexp_tree(Campaign.missions[cur].formula); // force formula to be re-evaluated
1322 eval_sexp(Campaign.missions[cur].formula); // this should reset Campaign.next_mission to proper value
1325 // evaluate mission loop mission (if any) so it can be used if chosen
1326 if ( Campaign.missions[cur].has_mission_loop ) {
1327 int copy_next_mission = Campaign.next_mission;
1328 // Set temporarily to -1 so we know if loop formula fails to assign
1329 Campaign.next_mission = -1; // Cannot exit campaign from loop
1330 if (Campaign.missions[cur].mission_loop_formula != -1) {
1331 flush_sexp_tree(Campaign.missions[cur].mission_loop_formula); // force formula to be re-evaluated
1332 eval_sexp(Campaign.missions[cur].mission_loop_formula); // this should reset Campaign.next_mission to proper value
1335 Campaign.loop_mission = Campaign.next_mission;
1336 Campaign.next_mission = copy_next_mission;
1340 if (Campaign.next_mission == -1)
1341 nprintf(("allender", "No next mission to proceed to.\n"));
1343 nprintf(("allender", "Next mission is number %d [%s]\n", Campaign.next_mission, Campaign.missions[Campaign.next_mission].name));
1345 return Campaign.next_mission;
1348 // Evaluate next campaign mission - set as Campaign.next_mission. Also set Campaign.loop_mission
1349 void mission_campaign_eval_next_mission()
1351 Campaign.next_mission = -1;
1352 int cur = Campaign.current_mission;
1354 // evaluate next mission (straight path)
1355 if (Campaign.missions[cur].formula != -1) {
1356 flush_sexp_tree(Campaign.missions[cur].formula); // force formula to be re-evaluated
1357 eval_sexp(Campaign.missions[cur].formula); // this should reset Campaign.next_mission to proper value
1360 // evaluate mission loop mission (if any) so it can be used if chosen
1361 if ( Campaign.missions[cur].has_mission_loop ) {
1362 int copy_next_mission = Campaign.next_mission;
1363 // Set temporarily to -1 so we know if loop formula fails to assign
1364 Campaign.next_mission = -1;
1365 if (Campaign.missions[cur].mission_loop_formula != -1) {
1366 flush_sexp_tree(Campaign.missions[cur].mission_loop_formula); // force formula to be re-evaluated
1367 eval_sexp(Campaign.missions[cur].mission_loop_formula); // this should reset Campaign.next_mission to proper value
1370 Campaign.loop_mission = Campaign.next_mission;
1371 Campaign.next_mission = copy_next_mission;
1374 if (Campaign.next_mission == -1) {
1375 nprintf(("allender", "No next mission to proceed to.\n"));
1377 nprintf(("allender", "Next mission is number %d [%s]\n", Campaign.next_mission, Campaign.missions[Campaign.next_mission].name));
1382 // Store mission's goals and events in Campaign struct
1383 void mission_campaign_store_goals_and_events()
1389 cur = Campaign.current_mission;
1390 name = Campaign.missions[cur].name;
1392 mission = &Campaign.missions[cur];
1394 // first we must save the status of the current missions goals in the campaign mission structure.
1395 // After that, we can determine which mission is tagged as the next mission. Finally, we
1396 // can save the campaign save file
1397 // we might have goal and event status if the player replayed a mission
1398 if ( mission->num_goals > 0 ) {
1399 free( mission->goals );
1402 mission->num_goals = Num_goals;
1403 if ( mission->num_goals > 0 ) {
1404 mission->goals = (mgoal *)malloc( sizeof(mgoal) * Num_goals );
1405 Assert( mission->goals != NULL );
1408 // copy the needed info from the Mission_goal struct to our internal structure
1409 for (i = 0; i < Num_goals; i++ ) {
1410 if ( strlen(Mission_goals[i].name) == 0 ) {
1411 char name[NAME_LENGTH];
1413 sprintf(name, NOX("Goal #%d"), i);
1414 //Warning(LOCATION, "Mission goal in mission %s must have a +Name field! using %s for campaign save file\n", mission->name, name);
1415 strcpy( mission->goals[i].name, name);
1417 strcpy( mission->goals[i].name, Mission_goals[i].name );
1418 Assert ( Mission_goals[i].satisfied != GOAL_INCOMPLETE ); // should be true or false at this point!!!
1419 mission->goals[i].status = (char)Mission_goals[i].satisfied;
1422 // do the same thing for events as we did for goals
1423 // we might have goal and event status if the player replayed a mission
1424 if ( mission->num_events > 0 ) {
1425 free( mission->events );
1428 mission->num_events = Num_mission_events;
1429 if ( mission->num_events > 0 ) {
1430 mission->events = (mevent *)malloc( sizeof(mevent) * Num_mission_events );
1431 Assert( mission->events != NULL );
1434 // copy the needed info from the Mission_goal struct to our internal structure
1435 for (i = 0; i < Num_mission_events; i++ ) {
1436 if ( strlen(Mission_events[i].name) == 0 ) {
1437 char name[NAME_LENGTH];
1439 sprintf(name, NOX("Event #%d"), i);
1440 nprintf(("Warning", "Mission goal in mission %s must have a +Name field! using %s for campaign save file\n", mission->name, name));
1441 strcpy( mission->events[i].name, name);
1443 strcpy( mission->events[i].name, Mission_events[i].name );
1445 // getting status for the events is a little different. If the formula value for the event entry
1446 // is -1, then we know the value of the result field will never change. If the formula is
1447 // not -1 (i.e. still being evaluated at mission end time), we will write "incomplete" for the
1449 if ( Mission_events[i].formula == -1 ) {
1450 if ( Mission_events[i].result )
1451 mission->events[i].status = EVENT_SATISFIED;
1453 mission->events[i].status = EVENT_FAILED;
1459 // this function is called when the player's mission is over. It updates the internal store of goals
1460 // and their status then saves the state of the campaign in the campaign file. This gets called
1461 // after player accepts mission results in debriefing.
1462 void mission_campaign_mission_over()
1467 // I don't think that we should have a record for these -- maybe we might?????? If we do,
1468 // then we should free them
1469 if ( !(Game_mode & GM_CAMPAIGN_MODE) ){
1473 mission_num = Campaign.current_mission;
1474 Assert( mission_num != -1 );
1475 mission = &Campaign.missions[mission_num];
1477 // determine if any ships/weapons were granted this mission
1478 for ( i=0; i<Num_granted_ships; i++ ){
1479 Campaign.ships_allowed[Granted_ships[i]] = 1;
1482 for ( i=0; i<Num_granted_weapons; i++ ){
1483 Campaign.weapons_allowed[Granted_weapons[i]] = 1;
1486 // DKA 12/11/98 - Unneeded already evaluated and stored
1487 // determine what new mission we are moving to.
1488 // mission_campaign_eval_next_mission(1);
1490 // update campaign.mission stats (used to allow backout inRedAlert)
1491 memcpy( &mission->stats, &Player->stats, sizeof(Player->stats) );
1492 if(!(Game_mode & GM_MULTIPLAYER)){
1493 scoring_backout_accept( &mission->stats );
1496 // if we are moving to a new mission, then change our data. If we are staying on the same mission,
1497 // then we don't want to do anything. Remove information about goals/events
1498 if ( Campaign.next_mission != mission_num ) {
1499 Campaign.prev_mission = mission_num;
1500 Campaign.current_mission = -1;
1501 Campaign.num_missions_completed++;
1502 Campaign.missions[mission_num].completed = 1;
1504 // save the scoring values from the previous mission at the start of this mission -- for red alert
1506 // save the state of the campaign in the campaign save file and move to the end_game state
1507 if (Campaign.type == CAMPAIGN_TYPE_SINGLE) {
1508 mission_campaign_savefile_save();
1512 // free up the goals and events which were just malloced. It's kind of like erasing any fact
1513 // that the player played this mission in the campaign.
1514 free( mission->goals );
1515 mission->num_goals = 0;
1517 free( mission->events );
1518 mission->num_events = 0;
1520 Sexp_nodes[mission->formula].value = SEXP_UNKNOWN;
1524 if (Campaign.missions[Campaign.next_mission].flags & CMISSION_FLAG_BASTION){
1525 Player->on_bastion = 1;
1527 Player->on_bastion = 0;
1530 mission_campaign_next_mission(); // sets up whatever needs to be set to actually play next mission
1533 // called when the game closes -- to get rid of memory errors for Bounds checker
1534 void mission_campaign_close()
1539 free(Campaign.desc);
1541 // be sure to remove all old malloced strings of Mission_names
1542 // we must also free any goal stuff that was from a previous campaign
1543 for ( i=0; i<Campaign.num_missions; i++ ) {
1544 if ( Campaign.missions[i].name ){
1545 free(Campaign.missions[i].name);
1548 if (Campaign.missions[i].notes){
1549 free(Campaign.missions[i].notes);
1552 if ( Campaign.missions[i].num_goals > 0 ){
1553 free ( Campaign.missions[i].goals );
1556 if ( Campaign.missions[i].num_events > 0 ){
1557 free ( Campaign.missions[i].events );
1560 // the next three are strdup'd return values from parselo.cpp
1561 if (Campaign.missions[i].mission_loop_desc) {
1562 free(Campaign.missions[i].mission_loop_desc);
1565 if (Campaign.missions[i].mission_loop_brief_anim) {
1566 free(Campaign.missions[i].mission_loop_brief_anim);
1569 if (Campaign.missions[i].mission_loop_brief_sound) {
1570 free(Campaign.missions[i].mission_loop_brief_sound);
1573 if ( !Fred_running ){
1574 sexp_unmark_persistent(Campaign.missions[i].formula); // free any sexpression nodes used by campaign.
1577 Campaign.missions[i].num_goals = 0;
1578 Campaign.missions[i].num_events = 0;
1582 // call from game_shutdown() ONLY!!!
1583 void mission_campaign_shutdown()
1587 for (i=0; i<MAX_CAMPAIGNS; i++) {
1588 if (Campaign_names[i] != NULL) {
1589 free(Campaign_names[i]);
1590 Campaign_names[i] = NULL;
1593 if (Campaign_file_names[i] != NULL) {
1594 free(Campaign_file_names[i]);
1595 Campaign_file_names[i] = NULL;
1600 // extract the mission filenames for a campaign.
1602 // filename => name of campaign file
1603 // dest => storage for the mission filename, must be already allocated
1604 // num => output parameter for the number of mission filenames in the campaign
1606 // note that dest should allocate at least dest[MAX_CAMPAIGN_MISSIONS][NAME_LENGTH]
1607 int mission_campaign_get_filenames(const char *filename, char dest[][NAME_LENGTH], int *num)
1611 // read the mission file and get the list of mission filenames
1612 if ((rval = setjmp(parse_abort)) != 0) {
1616 read_file_text(filename);
1617 Assert(strlen(filename) < MAX_FILENAME_LEN - 1); // make sure no overflow
1620 required_string("$Name:");
1621 advance_to_eoln(NULL);
1623 required_string( "$Type:" );
1624 advance_to_eoln(NULL);
1626 // parse the mission file and actually read in the mission stuff
1628 while ( skip_to_string("$Mission:") == 1 ) {
1629 stuff_string(dest[*num], F_NAME, NULL);
1637 // function to read the goals and events from a mission in a campaign file and store that information
1638 // in the campaign structure for use in the campaign editor, error checking, etc
1639 void read_mission_goal_list(int num)
1641 char *filename, notes[NOTES_LENGTH], goals[MAX_GOALS][NAME_LENGTH];
1642 char events[MAX_MISSION_EVENTS][NAME_LENGTH];
1643 int i, z, r, event_count, count = 0;
1645 filename = Campaign.missions[num].name;
1646 if ((r = setjmp(parse_abort))>0) {
1647 Warning(LOCATION, "Error reading \"%s\" (code = %d)", filename, r);
1651 // open localization
1654 read_file_text(filename);
1657 // first, read the mission notes for this mission. Used in campaign editor
1658 if (skip_to_string("#Mission Info")) {
1659 if (skip_to_string("$Notes:")) {
1660 stuff_string(notes, F_NOTES, NULL);
1661 if (Campaign.missions[num].notes){
1662 free(Campaign.missions[num].notes);
1665 Campaign.missions[num].notes = (char *) malloc(strlen(notes) + 1);
1666 strcpy(Campaign.missions[num].notes, notes);
1671 // skip to events section in the mission file. Events come before goals, so we process them first
1672 if ( skip_to_string("#Events") ) {
1674 if (skip_to_string("$Formula:", "#Goals") != 1){
1678 z = skip_to_string("+Name:", "$Formula:");
1684 stuff_string(events[event_count], F_NAME, NULL);
1686 sprintf(events[event_count], NOX("Event #%d"), event_count + 1);
1690 Assert(event_count < MAX_MISSION_EVENTS);
1695 if (skip_to_string("#Goals")) {
1697 if (skip_to_string("$Type:", "#End") != 1){
1701 z = skip_to_string("+Name:", "$Type:");
1707 stuff_string(goals[count], F_NAME, NULL);
1709 sprintf(goals[count], NOX("Goal #%d"), count + 1);
1713 Assert(count < MAX_GOALS);
1717 Campaign.missions[num].num_goals = count;
1719 Campaign.missions[num].goals = (mgoal *) malloc(count * sizeof(mgoal));
1720 Assert(Campaign.missions[num].goals); // make sure we got the memory
1721 memset(Campaign.missions[num].goals, 0, count * sizeof(mgoal));
1723 for (i=0; i<count; i++){
1724 strcpy(Campaign.missions[num].goals[i].name, goals[i]);
1728 Campaign.missions[num].num_events = event_count;
1730 Campaign.missions[num].events = (mevent *)malloc(event_count * sizeof(mevent));
1731 Assert ( Campaign.missions[num].events );
1732 memset(Campaign.missions[num].events, 0, event_count * sizeof(mevent));
1734 for (i = 0; i < event_count; i++ ){
1735 strcpy(Campaign.missions[num].events[i].name, events[i]);
1739 // close localization
1743 // function to return index into Campaign's list of missions of the mission with the given
1744 // filename. This function tried to be a little smart about filename looking for the .fsm
1745 // extension since filenames are stored with the extension in the campaign file. Returns
1746 // index of mission in campaign structure. -1 if mission name not found.
1747 int mission_campaign_find_mission( const char *name )
1750 char realname[_MAX_PATH];
1752 // look for an extension on the file. If no extension, add default ".fsm" onto the
1753 // end of the filename
1754 strcpy(realname, name );
1755 if ( strchr(name, '.') == NULL ){
1756 sprintf(realname, NOX("%s%s"), name, FS_MISSION_FILE_EXT );
1759 for (i = 0; i < Campaign.num_missions; i++ ) {
1760 if ( !stricmp(realname, Campaign.missions[i].name) ){
1768 void mission_campaign_maybe_play_movie(int type)
1773 // only support pre mission movies for now.
1774 Assert ( type == CAMPAIGN_MOVIE_PRE_MISSION );
1776 if ( !(Game_mode & GM_CAMPAIGN_MODE) )
1779 mission = Campaign.current_mission;
1780 Assert( mission != -1 );
1782 // get a possible filename for a movie to play.
1785 case CAMPAIGN_MOVIE_PRE_MISSION:
1786 if ( strlen(Campaign.missions[mission].briefing_cutscene) )
1787 filename = Campaign.missions[mission].briefing_cutscene;
1795 // no filename, no movie!
1799 movie_play( filename );
1802 // return nonzero if the passed filename is a multiplayer campaign, 0 otherwise
1803 int mission_campaign_parse_is_multi(const char *filename, char *name)
1808 read_file_text( filename );
1811 required_string("$Name:");
1812 stuff_string( temp, F_NAME, NULL );
1814 strcpy( name, temp );
1816 required_string( "$Type:" );
1817 stuff_string( temp, F_NAME, NULL );
1819 for (i = 0; i < MAX_CAMPAIGN_TYPES; i++ ) {
1820 if ( !stricmp(temp, campaign_types[i]) ) {
1825 Error(LOCATION, "Unknown campaign type %s", temp );
1829 // functions to save persistent information during a mission -- which then might or might not get
1830 // saved out when the mission is over depending on whether player replays mission or commits.
1831 void mission_campaign_save_persistent( int type, int sindex )
1833 // based on the type of information, save it off for possible saving into the campsign
1834 // savefile when the mission is over
1835 if ( type == CAMPAIGN_PERSISTENT_SHIP ) {
1836 Assert( Num_granted_ships < MAX_SHIP_TYPES );
1837 Granted_ships[Num_granted_ships] = sindex;
1838 Num_granted_ships++;
1839 } else if ( type == CAMPAIGN_PERSISTENT_WEAPON ) {
1840 Assert( Num_granted_weapons < MAX_WEAPON_TYPES );
1841 Granted_weapons[Num_granted_weapons] = sindex;
1842 Num_granted_weapons++;
1847 // returns 0: loaded, !0: error
1848 int mission_load_up_campaign()
1850 if (strlen(Player->current_campaign))
1851 return mission_campaign_load(Player->current_campaign);
1853 return mission_campaign_load(BUILTIN_CAMPAIGN);
1856 // for end of campaign in the single player game. Called when the end of campaign state is
1857 // entered, which is triggered when the end-campaign sexpression is hit
1859 void mission_campaign_end_init()
1861 // no need to do any initialization.
1864 void mission_campaign_end_do()
1867 event_music_level_close();
1868 mission_goal_fail_incomplete();
1869 scoring_level_close();
1870 mission_campaign_mission_over();
1873 movie_play("endgame.mve");
1875 // eventually we'll want to play one of two options (good ending or bad ending)
1876 // did the supernova blow?
1877 if(Supernova_status == SUPERNOVA_HIT){
1878 movie_play_two("endpart1.mve", "endprt2b.mve"); // good ending
1880 movie_play_two("endpart1.mve", "endprt2a.mve"); // good ending
1884 #if defined(FS2_DEMO) || defined(FS1_DEMO)
1885 gameseq_post_event( GS_EVENT_END_DEMO );
1886 #elif defined(MAKE_FS1)
1887 gameseq_post_event( GS_STATE_END_OF_CAMPAIGN );
1889 gameseq_post_event( GS_EVENT_MAIN_MENU );
1893 void mission_campaign_end_close()
1895 // nothing to do here.
1899 // skip to the next mission in the campaign
1900 // this also posts the state change by default. pass 0 to override that
1901 void mission_campaign_skip_to_next(int start_game)
1903 // mark all goals/events complete
1904 // these do not really matter, since is-previous-event-* and is-previous-goal-* sexps check
1905 // to see if the mission was skipped, and use defaults accordingly.
1906 mission_goal_mark_objectives_complete();
1907 mission_goal_mark_events_complete();
1909 // mark mission as skipped
1910 Campaign.missions[Campaign.current_mission].flags |= CMISSION_FLAG_SKIPPED;
1913 mission_campaign_store_goals_and_events();
1915 // now set the next mission
1916 mission_campaign_eval_next_mission();
1918 // clear out relevant player vars
1919 Player->failures_this_session = 0;
1920 Player->show_skip_popup = 1;
1923 // proceed to next mission or main hall
1924 if ((Campaign.missions[Campaign.current_mission].has_mission_loop) && (Campaign.loop_mission != -1)) {
1925 // go to loop solicitation
1926 gameseq_post_event(GS_EVENT_LOOP_BRIEF);
1928 // closes out mission stuff, sets up next one
1929 mission_campaign_mission_over();
1931 if ( Campaign.next_mission == -1 ) {
1932 // go to main hall, tha campaign is over!
1933 gameseq_post_event(GS_EVENT_MAIN_MENU);
1935 // go to next mission
1936 gameseq_post_event(GS_EVENT_START_GAME);
1943 // breaks your ass out of the loop
1944 // this also posts the state change
1945 void mission_campaign_exit_loop()
1947 // set campaign to loop reentry point
1948 Campaign.next_mission = Campaign.loop_reentry;
1949 Campaign.current_mission = -1;
1950 Campaign.loop_enabled = 0;
1952 // set things up for next mission
1953 mission_campaign_next_mission();
1954 gameseq_post_event(GS_EVENT_START_GAME);
1958 // used for jumping to a particular campaign mission
1959 // all pvs missions marked skipped
1960 // this relies on correct mission ordering in the campaign file
1961 void mission_campaign_jump_to_mission(const char *name)
1964 char dest_name[64] = { 0 };
1966 // load in the campaign junk
1967 mission_load_up_campaign();
1970 // tack the .fsm onto the input name
1971 strncpy(dest_name, cf_add_ext(name, ".fsm"), sizeof(dest_name)-1);
1973 // tack the .fs2 onto the input name
1974 strncpy(dest_name, cf_add_ext(name, ".fs2"), sizeof(dest_name)-1);
1977 // search for our mission
1978 for (i=0; i<Campaign.num_missions; i++) {
1979 if ((Campaign.missions[i].name != NULL) && !stricmp(Campaign.missions[i].name, dest_name) ) {
1980 Campaign.next_mission = i;
1981 Campaign.prev_mission = i-1;
1982 mission_campaign_next_mission();
1983 Game_mode |= GM_CAMPAIGN_MODE;
1984 gameseq_post_event(GS_EVENT_START_GAME);
1987 Campaign.missions[i].flags |= CMISSION_FLAG_SKIPPED;
1988 Campaign.num_missions_completed = i;
1992 // if we got here, no match was found
1993 // restart the campaign
1994 mission_campaign_savefile_delete(Campaign.filename);
1995 mission_campaign_load(Campaign.filename);