1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872
|
/*
* Copyright (C) Volition, Inc. 1999. All rights reserved.
*
* All source code herein is the property of Volition, Inc. You may not sell
* or otherwise commercially exploit the source or things you created based on the
* source.
*
*/
#include <cstdio>
#include <cstring>
#include <csetjmp>
#include <cerrno>
#ifdef _WIN32
#include <direct.h>
#include <io.h>
#endif
#ifdef SCP_UNIX
#include <sys/stat.h>
#include <glob.h>
#endif
#include "freespace.h"
#include "missioncampaign.h"
#include "cfile/cfile.h"
#include "cutscene/cutscenes.h"
#include "cutscene/movie.h"
#include "gamesequence/gamesequence.h"
#include "gamesnd/eventmusic.h"
#include "localization/localize.h"
#include "menuui/mainhallmenu.h"
#include "menuui/techmenu.h"
#include "mission/missioncampaign.h"
#include "mission/missiongoals.h"
#include "missionui/missionscreencommon.h"
#include "missionui/redalert.h"
#include "parse/parselo.h"
#include "parse/sexp.h"
#include "parse/sexp_container.h"
#include "pilotfile/pilotfile.h"
#include "playerman/player.h"
#include "popup/popup.h"
#include "scripting/global_hooks.h"
#include "scripting/scripting.h"
#include "ship/ship.h"
#include "starfield/supernova.h"
#include "ui/ui.h"
#include "weapon/weapon.h"
// campaign wasn't ended
int Campaign_ending_via_supernova = 0;
// stuff for selecting campaigns. We need to keep both arrays around since we display the
// list of campaigns by name, but must load campaigns by filename
char *Campaign_names[MAX_CAMPAIGNS] = { NULL };
char *Campaign_file_names[MAX_CAMPAIGNS] = { NULL };
char *Campaign_descs[MAX_CAMPAIGNS] = { NULL };
int Num_campaigns;
bool Campaign_file_missing = false;
int Campaign_load_failure = 0;
int Campaign_names_inited = 0;
SCP_vector<SCP_string> Ignored_campaigns;
char Default_campaign_file_name[MAX_FILENAME_LEN - 4] = { 0 };
// stuff used for campaign list building
static bool MC_desc = false;
static bool MC_multiplayer = false;
const char *campaign_types[MAX_CAMPAIGN_TYPES] =
{
//XSTR:OFF
"single",
"multi coop",
"multi teams"
//XSTR:ON
};
// modules local variables to deal with getting new ships/weapons available to the player
int Num_granted_ships, Num_granted_weapons; // per mission counts of new ships and weapons
int Granted_ships[MAX_SHIP_CLASSES];
int Granted_weapons[MAX_WEAPON_TYPES];
// variables to control the UI stuff for loading campaigns
LOCAL UI_WINDOW Campaign_window;
LOCAL UI_LISTBOX Campaign_listbox;
LOCAL UI_BUTTON Campaign_okb, Campaign_cancelb;
// the campaign!!!!!
campaign Campaign;
/**
* Returns a string (which is malloced in this routine) of the name of the given freespace campaign file.
* In the type field, we return if the campaign is a single player or multiplayer campaign.
* The type field will only be valid if the name returned is non-NULL
*/
int mission_campaign_get_info(const char *filename, char *name, int *type, int *max_players, char **desc, char **first_mission)
{
int i, success = 0;
char campaign_type[NAME_LENGTH], fname[MAX_FILENAME_LEN];
Assert( name != NULL );
Assert( type != NULL );
strncpy(fname, filename, MAX_FILENAME_LEN - 1);
auto fname_len = strlen(fname);
if ((fname_len < 4) || stricmp(fname + fname_len - 4, FS_CAMPAIGN_FILE_EXT) != 0){
strcat_s(fname, FS_CAMPAIGN_FILE_EXT);
fname_len += 4;
}
Assert(fname_len < MAX_FILENAME_LEN);
*type = -1;
do {
try
{
read_file_text(fname);
reset_parse();
required_string("$Name:");
stuff_string(name, F_NAME, NAME_LENGTH);
if (name == NULL) {
nprintf(("Warning", "No name found for campaign file %s\n", filename));
break;
}
required_string("$Type:");
stuff_string(campaign_type, F_NAME, NAME_LENGTH);
for (i = 0; i < MAX_CAMPAIGN_TYPES; i++) {
if (!stricmp(campaign_type, campaign_types[i])) {
*type = i;
}
}
if (name == NULL) {
Warning(LOCATION, "Invalid campaign type \"%s\"\n", campaign_type);
break;
}
if (desc) {
if (optional_string("+Description:")) {
*desc = stuff_and_malloc_string(F_MULTITEXT, NULL);
}
else {
*desc = NULL;
}
}
// if this is a multiplayer campaign, get the max players
if ((*type) != CAMPAIGN_TYPE_SINGLE) {
skip_to_string("+Num Players:");
stuff_int(max_players);
// Cyborg17 - and the first mission name if we want it, too
if (first_mission) {
skip_to_string("$Mission:");
*first_mission = stuff_and_malloc_string(F_NAME, nullptr);
}
}
// if we found a valid campaign type
if ((*type) >= 0) {
success = 1;
}
}
catch (const parse::ParseException& e)
{
mprintf(("MISSIONCAMPAIGN: Unable to parse '%s'! Error message = %s.\n", fname, e.what()));
break;
}
} while (0);
Assert(success);
return success;
}
/**
* Parses campaign and returns a list of missions in it.
* @return Number of missions added to the 'list', and up to 'max' missions may be added to 'list'.
* @return Negative on error.
*/
int mission_campaign_get_mission_list(const char *filename, char **list, int max)
{
int i, num = 0;
char name[MAX_FILENAME_LEN];
filename = cf_add_ext(filename, FS_CAMPAIGN_FILE_EXT);
// read the campaign file and get the list of mission filenames
try
{
read_file_text(filename);
reset_parse();
while (skip_to_string("$Mission:") > 0) {
stuff_string(name, F_NAME, MAX_FILENAME_LEN);
if (num < max)
list[num++] = vm_strdup(name);
else
Warning(LOCATION, "Maximum number of missions exceeded (%d)!", max);
}
}
catch (const parse::ParseException& e)
{
mprintf(("MISSIONCAMPAIGN: Unable to parse '%s'! Error message = %s.\n", filename, e.what()));
// since we can't return count of allocated elements, free them instead
for (i = 0; i<num; i++)
vm_free(list[i]);
num = -1;
}
return num;
}
void mission_campaign_free_list()
{
int i;
if ( !Campaign_names_inited )
return;
for (i = 0; i < Num_campaigns; i++) {
if (Campaign_names[i] != NULL) {
vm_free(Campaign_names[i]);
Campaign_names[i] = NULL;
}
if (Campaign_file_names[i] != NULL) {
vm_free(Campaign_file_names[i]);
Campaign_file_names[i] = NULL;
}
if (Campaign_descs[i] != NULL) {
vm_free(Campaign_descs[i]);
Campaign_descs[i] = NULL;
}
}
Num_campaigns = 0;
Campaign_names_inited = 0;
}
int mission_campaign_maybe_add(const char *filename)
{
char name[NAME_LENGTH];
char *desc = NULL;
int type, max_players;
// don't add ignored campaigns
if (campaign_is_ignored(filename)) {
return 0;
}
if ( mission_campaign_get_info( filename, name, &type, &max_players, &desc) ) {
if ( !MC_multiplayer && (type == CAMPAIGN_TYPE_SINGLE) ) {
Campaign_names[Num_campaigns] = vm_strdup(name);
if (MC_desc)
Campaign_descs[Num_campaigns] = desc;
Num_campaigns++;
// Note that we're not freeing desc here because the pointer is getting copied to Campaign_descs which is freed later.
return 1;
}
}
if (desc != NULL)
vm_free(desc);
return 0;
}
/**
* Builds up the list of campaigns that the user might be able to pick from.
* It uses the multiplayer flag to tell if we should display a list of single or multiplayer campaigns.
* This routine sets the Num_campaigns and Campaign_names global variables
*/
void mission_campaign_build_list(bool desc, bool sort, bool multiplayer)
{
char wild_card[10];
int i, j, incr = 0;
char *t = NULL;
int rc = 0;
if (Campaign_names_inited)
return;
MC_desc = desc;
MC_multiplayer = multiplayer;
memset(wild_card, 0, sizeof(wild_card));
strcpy_s(wild_card, NOX("*"));
strcat_s(wild_card, FS_CAMPAIGN_FILE_EXT);
// if we have already been loaded then free everything and reload
if (Num_campaigns != 0)
mission_campaign_free_list();
// set filter for cf_get_file_list() if there isn't one set already (the simroom has a special one)
if (Get_file_list_filter == NULL)
Get_file_list_filter = mission_campaign_maybe_add;
// now get the list of all mission names
// NOTE: we don't do sorting here, but we assume CF_SORT_NAME, and do it manually below
rc = cf_get_file_list(MAX_CAMPAIGNS, Campaign_file_names, CF_TYPE_MISSIONS, wild_card, CF_SORT_NONE);
Assert( rc == Num_campaigns );
// now sort everything, if we are supposed to
if (sort) {
incr = Num_campaigns / 2;
while (incr > 0) {
for (i = incr; i < Num_campaigns; i++) {
j = i - incr;
while (j >= 0) {
char *name1 = Campaign_names[j];
char *name2 = Campaign_names[j + incr];
// if we hit this then a coder probably did something dumb (like not needing to sort)
if ( (name1 == NULL) || (name2 == NULL) ) {
Int3();
break;
}
if ( !strnicmp(name1, "the ", 4) )
name1 += 4;
if ( !strnicmp(name2, "the ", 4) )
name2 += 4;
if (stricmp(name1, name2) > 0) {
// first, do filenames
t = Campaign_file_names[j];
Campaign_file_names[j] = Campaign_file_names[j + incr];
Campaign_file_names[j + incr] = t;
// next, actual names
t = Campaign_names[j];
Campaign_names[j] = Campaign_names[j + incr];
Campaign_names[j + incr] = t;
// finally, do descriptions
if (desc) {
t = Campaign_descs[j];
Campaign_descs[j] = Campaign_descs[j + incr];
Campaign_descs[j + incr] = t;
}
j -= incr;
} else {
break;
}
}
}
incr /= 2;
}
}
// Done!
Campaign_names_inited = 1;
}
/**
* Gets optional ship/weapon information
*/
void mission_campaign_get_sw_info()
{
int i, count, ship_list[MAX_SHIP_CLASSES], weapon_list[MAX_WEAPON_TYPES];
if (optional_string("+Starting Ships:")) {
count = (int)stuff_int_list(ship_list, MAX_SHIP_CLASSES, SHIP_INFO_TYPE);
// now set the array elements stating which ships we are allowed
for (i = 0; i < count; i++) {
if (Ship_info[ship_list[i]].flags[Ship::Info_Flags::Player_ship])
Campaign.ships_allowed[ship_list[i]] = 1;
}
}
else {
// set allowable ships to the SIF_PLAYER_SHIPs
for (auto it = Ship_info.cbegin(); it != Ship_info.cend(); ++it) {
if (it->flags[Ship::Info_Flags::Player_ship])
Campaign.ships_allowed[std::distance(Ship_info.cbegin(), it)] = 1;
}
}
if (optional_string("+Starting Weapons:")) {
count = (int)stuff_int_list(weapon_list, MAX_WEAPON_TYPES, WEAPON_POOL_TYPE);
// now set the array elements stating which ships we are allowed
for (i = 0; i < count; i++) {
if (Weapon_info[weapon_list[i]].wi_flags[Weapon::Info_Flags::Player_allowed])
Campaign.weapons_allowed[weapon_list[i]] = 1;
}
}
else {
// set allowable weapons to the player-allowed ones
for (auto it = Weapon_info.cbegin(); it != Weapon_info.cend(); ++it) {
if (it->wi_flags[Weapon::Info_Flags::Player_allowed])
Campaign.weapons_allowed[std::distance(Weapon_info.cbegin(), it)] = 1;
}
}
}
/**
* Starts a new campaign. It reads in the mission information in the campaign file
* It also sets up all variables needed inside of the game to deal with starting mission numbers, etc
*
* Note: Due to difficulties in generalizing this function, parts of it are duplicated throughout
* this file. If you change the format of the campaign file, you should be sure these related
* functions work properly and update them if it breaks them.
*/
int mission_campaign_load(const char* filename, const char* full_path, player* pl, int load_savefile)
{
int i;
char name[NAME_LENGTH], type[NAME_LENGTH], temp[NAME_LENGTH];
if (campaign_is_ignored(filename)) {
Campaign_file_missing = true;
Campaign_load_failure = CAMPAIGN_ERROR_IGNORED;
return CAMPAIGN_ERROR_IGNORED;
}
filename = cf_add_ext(filename, FS_CAMPAIGN_FILE_EXT);
if ( pl == NULL )
pl = Player;
if ( !Fred_running && load_savefile && (pl == NULL) ) {
Int3();
load_savefile = 0;
}
// be sure to remove all old malloced strings of Mission_names
// we must also free any goal stuff that was from a previous campaign
// this also frees sexpressions so the next call to init_sexp will be able to reclaim
// nodes previously used by another campaign.
mission_campaign_clear();
// read the mission file and get the list of mission filenames
try
{
strcpy_s( Campaign.filename, filename );
// only initialize the sexpression stuff when Fred isn't running. It'll screw things up major
// if it does
if ( !Fred_running ){
init_sexp(); // must initialize the sexpression stuff
}
read_file_text( full_path ? full_path : filename );
reset_parse();
// copy filename to campaign structure minus the extension
auto len = strlen(filename) - 4;
Assert(len + 1 < CF_MAX_PATHNAME_LENGTH);
strncpy(Campaign.filename, filename, len);
Campaign.filename[len] = '\0';
required_string("$Name:");
stuff_string( name, F_NAME, NAME_LENGTH );
//Store campaign name in the global struct
strcpy_s( Campaign.name, name );
required_string( "$Type:" );
stuff_string( type, F_NAME, NAME_LENGTH );
for (i = 0; i < MAX_CAMPAIGN_TYPES; i++ ) {
if ( !stricmp(type, campaign_types[i]) ) {
Campaign.type = i;
break;
}
}
if ( i == MAX_CAMPAIGN_TYPES )
Error(LOCATION, "Unknown campaign type %s!", type);
if (optional_string("+Description:"))
Campaign.desc = stuff_and_malloc_string(F_MULTITEXT, NULL);
// if the type is multiplayer -- get the number of players
if ( Campaign.type != CAMPAIGN_TYPE_SINGLE) {
required_string("+Num players:");
stuff_int( &(Campaign.num_players) );
}
// parse flags - Goober5000
if (optional_string("$Flags:"))
{
stuff_int( &(Campaign.flags) );
}
// parse the optional ship/weapon information
mission_campaign_get_sw_info();
// parse the mission file and actually read in the mission stuff
while ( required_string_either("#End", "$Mission:") ) {
cmission *cm;
required_string("$Mission:");
stuff_string(name, F_NAME, NAME_LENGTH);
cm = &Campaign.missions[Campaign.num_missions];
cm->name = vm_strdup(name);
cm->notes = NULL;
cm->briefing_cutscene[0] = 0;
if ( optional_string("+Briefing Cutscene:") )
stuff_string( cm->briefing_cutscene, F_NAME, NAME_LENGTH );
cm->flags = 0;
if (optional_string("+Flags:"))
stuff_int(&cm->flags);
cm->main_hall = "0";
// deal with previous campaign versions
if (cm->flags & CMISSION_FLAG_BASTION) {
cm->main_hall = "1";
}
// Goober5000 - new main hall stuff!
// Updated by CommanderDJ
if (optional_string("+Main Hall:")) {
stuff_string(temp, F_RAW, 32);
cm->main_hall = temp;
}
// Goober5000 - substitute main hall (like substitute music)
if (optional_string("+Substitute Main Hall:")) {
stuff_string(temp, F_RAW, 32);
// see if this main hall exists
main_hall_defines *mhd = main_hall_get_pointer(temp);
if (mhd != NULL) {
cm->main_hall = temp;
} else {
mprintf(("Substitute main hall '%s' not found\n", temp));
}
}
// Goober5000 - new debriefing persona stuff!
cm->debrief_persona_index = 0;
if (optional_string("+Debriefing Persona Index:"))
stuff_ubyte(&cm->debrief_persona_index);
cm->formula = -1;
if ( optional_string("+Formula:") ) {
cm->formula = get_sexp_main();
if ( !Fred_running ) {
Assert ( cm->formula != -1 );
sexp_mark_persistent( cm->formula );
} else {
if ( cm->formula == -1 ){
Campaign_load_failure = CAMPAIGN_ERROR_SEXP_EXHAUSTED;
return CAMPAIGN_ERROR_SEXP_EXHAUSTED;
}
}
}
// Do mission branching stuff
if ( optional_string("+Mission Loop:") ) {
cm->flags |= CMISSION_FLAG_HAS_LOOP;
} else if ( optional_string("+Mission Fork:") ) {
cm->flags |= CMISSION_FLAG_HAS_FORK;
}
cm->mission_branch_desc = NULL;
if ( optional_string("+Mission Loop Text:") || optional_string("+Mission Fork Text:") ) {
cm->mission_branch_desc = stuff_and_malloc_string(F_MULTITEXT, NULL);
}
cm->mission_branch_brief_anim = NULL;
if ( optional_string("+Mission Loop Brief Anim:") || optional_string("+Mission Fork Brief Anim:") ) {
cm->mission_branch_brief_anim = stuff_and_malloc_string(F_MULTITEXT, NULL);
}
cm->mission_branch_brief_sound = NULL;
if ( optional_string("+Mission Loop Brief Sound:") || optional_string("+Mission Fork Brief Sound:") ) {
cm->mission_branch_brief_sound = stuff_and_malloc_string(F_MULTITEXT, NULL);
}
cm->mission_loop_formula = -1;
if ( optional_string("+Formula:") ) {
cm->mission_loop_formula = get_sexp_main();
if ( !Fred_running ) {
Assert ( cm->mission_loop_formula != -1 );
sexp_mark_persistent( cm->mission_loop_formula );
} else {
if ( cm->mission_loop_formula == -1 ){
Campaign_load_failure = CAMPAIGN_ERROR_SEXP_EXHAUSTED;
return CAMPAIGN_ERROR_SEXP_EXHAUSTED;
}
}
}
cm->level = 0;
if (optional_string("+Level:")) {
stuff_int( &cm->level );
if ( cm->level == 0 ) // check if the top (root) of the whole tree
Campaign.next_mission = Campaign.num_missions;
} else
Campaign.realign_required = 1;
cm->pos = 0;
if (optional_string("+Position:"))
stuff_int( &cm->pos );
else
Campaign.realign_required = 1;
cm->goals.clear();
cm->events.clear();
cm->variables.clear();
cm->flags |= CMISSION_FLAG_FRED_LOAD_PENDING;
Campaign.num_missions++;
}
if ((Game_mode & GM_MULTIPLAYER) && Campaign.num_missions > UINT8_MAX)
throw parse::ParseException("Number of campaign missions is too high and breaks multi!");
}
catch (const parse::FileOpenException& foe)
{
mprintf(("Error opening '%s'\r\nError message = %s.\r\n", filename, foe.what()));
Campaign.filename[0] = 0;
Campaign.num_missions = 0;
Campaign_file_missing = true;
Campaign_load_failure = CAMPAIGN_ERROR_MISSING;
return CAMPAIGN_ERROR_MISSING;
}
catch (const parse::ParseException& pe)
{
mprintf(("Error parsing '%s'\r\nError message = %s.\r\n", filename, pe.what()));
Campaign.filename[0] = 0;
Campaign.num_missions = 0;
Campaign_file_missing = true;
Campaign_load_failure = CAMPAIGN_ERROR_CORRUPT;
return CAMPAIGN_ERROR_CORRUPT;
}
// set up the other variables for the campaign stuff. After initializing, we must try and load
// the campaign save file for this player. Since all campaign loads go through this routine, I
// think this place should be the only necessary place to load the campaign save stuff. The campaign
// save file will get written when a mission has ended by player choice.
// loading the campaign will get us to the current and next mission that the player must fly
// plus load all of the old goals that future missions might rely on.
if (!Fred_running && load_savefile && (Campaign.type == CAMPAIGN_TYPE_SINGLE)) {
// savefile can fail to load for numerous otherwise non-fatal reasons
// if it doesn't load in that case then it will be (re)created at save
if ( !Pilot.load_savefile(pl, Campaign.filename) ) {
// but if the data is invalid for the savefile then it is fatal
if ( Pilot.is_invalid() ) {
Campaign.filename[0] = 0;
Campaign.num_missions = 0;
Campaign_load_failure = CAMPAIGN_ERROR_SAVEFILE;
return CAMPAIGN_ERROR_SAVEFILE;
}
// start with fresh new campaign data
else {
Pilot.clear_savefile(false); // don't reset ships and weapons because they are currently the campaign's starting ones
Campaign.next_mission = 0;
Pilot.save_savefile();
}
}
}
// all is good here, move along
Campaign_file_missing = false;
return 0;
}
/*
* initialise Player_loadout with default values
*/
void player_loadout_init()
{
int i = 0, j = 0;
memset(Player_loadout.filename, 0, sizeof(Player_loadout.filename));
memset(Player_loadout.last_modified, 0, sizeof(Player_loadout.last_modified));
for ( i = 0; i < MAX_SHIP_CLASSES; i++ ) {
Player_loadout.ship_pool[i] = 0;
}
for ( i = 0; i < MAX_WEAPON_TYPES; i++ ) {
Player_loadout.weapon_pool[i] = 0;
}
for ( i = 0; i < MAX_WSS_SLOTS; i++ ) {
Player_loadout.unit_data[i].ship_class = -1;
for ( j = 0; j < MAX_SHIP_WEAPONS; j++ ) {
Player_loadout.unit_data[i].wep[j] = 0;
Player_loadout.unit_data[i].wep_count[j] = 0;
}
}
}
/**
* Initializes some variables then loads the default FreeSpace single player campaign.
*/
void mission_campaign_init()
{
mission_campaign_clear();
Campaign_file_missing = false;
player_loadout_init();
}
// the below two functions is internal to this module. It is here so that I can group the save/load
// functions together.
//
/**
* Deletes any save file in the players directory for the given
* campaign filename
*/
void mission_campaign_savefile_delete(const char* cfilename)
{
char filename[_MAX_FNAME], base[_MAX_FNAME];
_splitpath( cfilename, NULL, NULL, base, NULL );
if ( Player->flags & PLAYER_FLAGS_IS_MULTI ) {
return; // no such thing as a multiplayer campaign savefile
}
// only support the new filename here - taylor
sprintf_safe( filename, NOX("%s.%s.csg"), Player->callsign, base );
cf_delete(filename, CF_TYPE_PLAYERS, CF_LOCATION_ROOT_USER | CF_LOCATION_ROOT_GAME | CF_LOCATION_TYPE_ROOT);
}
/**
* Deletes all the save files for this particular pilot.
* Just call cfile function which will delete multiple files
*
* @param pilot_name Name of pilot
*/
void mission_campaign_delete_all_savefiles(char *pilot_name)
{
int dir_type, num_files, i;
char file_spec[MAX_FILENAME_LEN + 2];
char filename[1024];
int(*filter_save)(const char *filename);
SCP_vector<SCP_string> names;
auto ext = NOX(".csg");
dir_type = CF_TYPE_PLAYERS;
sprintf(file_spec, NOX("%s.*%s"), pilot_name, ext);
// HACK HACK HACK HACK!!!! cf_get_file_list is not reentrant. Pretty dumb because it should
// be. I have to save any file filters
filter_save = Get_file_list_filter;
Get_file_list_filter = NULL;
num_files = cf_get_file_list(names, dir_type, file_spec, CF_SORT_NONE, nullptr,
CF_LOCATION_ROOT_USER | CF_LOCATION_ROOT_GAME | CF_LOCATION_TYPE_ROOT);
Get_file_list_filter = filter_save;
for (i = 0; i < num_files; i++) {
strcpy_s(filename, names[i].c_str());
strcat_s(filename, ext);
cf_delete(filename, dir_type, CF_LOCATION_ROOT_USER | CF_LOCATION_ROOT_GAME | CF_LOCATION_TYPE_ROOT);
}
}
/**
* Sets up the internal veriables of the campaign structure so the player can play the next mission.
* If there are no more missions available in the campaign, this function returns -1, else 0 if the mission was
* set successfully
*/
int mission_campaign_next_mission()
{
if ((Campaign.next_mission == -1) || (Campaign.name[0] == '\0')) // will be set to -1 when there is no next mission
return -1;
if (Campaign.num_missions < 1)
return -2;
Campaign.current_mission = Campaign.next_mission;
strcpy_s(Game_current_mission_filename, Campaign.missions[Campaign.current_mission].name);
// check for end of loop.
if (Campaign.current_mission == Campaign.loop_reentry) {
Campaign.loop_enabled = 0;
}
// reset the number of persistent ships and weapons for the next campaign mission
Num_granted_ships = 0;
Num_granted_weapons = 0;
return 0;
}
/**
* Called to go to the previous mission in the campaign. Used only for Red Alert missions
*/
int mission_campaign_previous_mission()
{
if (!(Game_mode & GM_CAMPAIGN_MODE))
return 0;
if (Campaign.prev_mission == -1)
return 0;
Campaign.current_mission = Campaign.prev_mission;
Campaign.prev_mission = -1;
Campaign.next_mission = Campaign.current_mission;
Campaign.num_missions_completed--;
Campaign.missions[Campaign.next_mission].completed = 0;
// copy backed up variables over
for (auto& ra_variable : Campaign.red_alert_variables) {
Campaign.persistent_variables.push_back(ra_variable);
}
// copy backed up containers over
for (const auto& ra_container : Campaign.red_alert_containers) {
Campaign.persistent_containers.emplace_back(ra_container);
}
Pilot.save_savefile();
// reset the player stats to be the stats from this level
Player->stats.assign( Campaign.missions[Campaign.current_mission].stats );
strcpy_s( Game_current_mission_filename, Campaign.missions[Campaign.current_mission].name );
Num_granted_ships = 0;
Num_granted_weapons = 0;
return 1;
}
/**
* Evaluate next campaign mission - set as Campaign.next_mission. Also set Campaign.loop_mission
*/
void mission_campaign_eval_next_mission()
{
Campaign.next_mission = -1;
int cur = Campaign.current_mission;
// evaluate next mission (straight path)
if (Campaign.missions[cur].formula != -1) {
flush_sexp_tree(Campaign.missions[cur].formula); // force formula to be re-evaluated
eval_sexp(Campaign.missions[cur].formula); // this should reset Campaign.next_mission to proper value
}
// evaluate mission loop mission (if any) so it can be used if chosen
if ( Campaign.missions[cur].flags & CMISSION_FLAG_HAS_LOOP ) {
int saved_next_mission = Campaign.next_mission;
// Set temporarily to -1 so we know if loop formula fails to assign
Campaign.next_mission = -1;
if (Campaign.missions[cur].mission_loop_formula != -1) {
flush_sexp_tree(Campaign.missions[cur].mission_loop_formula); // force formula to be re-evaluated
eval_sexp(Campaign.missions[cur].mission_loop_formula); // this should set Campaign.next_mission to the loop mission
}
Campaign.loop_mission = Campaign.next_mission;
Campaign.next_mission = saved_next_mission;
// If the loop mission and the next mission are the same, then don't do the loop. This could be the case if the campaign
// only allows us to proceed to the loop mission if certain conditions are met.
if (Campaign.loop_mission == Campaign.next_mission) {
Campaign.loop_mission = -1;
}
}
if (Campaign.next_mission == -1) {
nprintf(("allender", "No next mission to proceed to.\n"));
} else {
nprintf(("allender", "Next mission is number %d [%s]\n", Campaign.next_mission, Campaign.missions[Campaign.next_mission].name));
}
}
/**
* Store mission's goals and events in Campaign struct
*/
void mission_campaign_store_goals_and_events()
{
int cur;
cmission *mission_obj;
if (!(Game_mode & GM_CAMPAIGN_MODE) || (Campaign.current_mission < 0))
return;
cur = Campaign.current_mission;
mission_obj = &Campaign.missions[cur];
// first we must save the status of the current missions goals in the campaign mission structure.
// After that, we can determine which mission is tagged as the next mission. Finally, we
// can save the campaign save file
// we might have goal and event status if the player replayed a mission
mission_obj->goals.clear();
// copy the needed info from the Mission_goal struct to our internal structure
for (const auto& goal: Mission_goals) {
mission_obj->goals.emplace_back();
auto& stored_goal = mission_obj->goals.back();
if (goal.name.empty())
sprintf(stored_goal.name, NOX("Goal #" SIZE_T_ARG), &goal - &Mission_goals[0] + 1);
else
strncpy_s(stored_goal.name, goal.name.c_str(), NAME_LENGTH - 1);
Assert ( goal.satisfied != GOAL_INCOMPLETE ); // should be true or false at this point!!!
stored_goal.status = (char)goal.satisfied;
}
// do the same thing for events as we did for goals
// we might have goal and event status if the player replayed a mission
mission_obj->events.clear();
// copy the needed info from the Mission_goal struct to our internal structure
for (const auto& event: Mission_events) {
mission_obj->events.emplace_back();
auto& stored_event = mission_obj->events.back();
if (event.name.empty()) {
sprintf(stored_event.name, NOX("Event #" SIZE_T_ARG), &event - &Mission_events[0] + 1);
nprintf(("Warning", "Mission event in mission %s must have a +Name field! using %s for campaign save file\n", mission_obj->name, stored_event.name));
} else
strncpy_s(stored_event.name, event.name.c_str(), NAME_LENGTH - 1);
// Old method:
// getting status for the events is a little different. If the formula value for the event entry
// is -1, then we know the value of the result field will never change. If the formula is
// not -1 (i.e. still being evaluated at mission end time), we will write "incomplete" for the
// event evaluation
// New method: check a flag. Also, even with the old method, events are always
// forced satisfied or failed at the end of a mission
if (event.flags & MEF_EVENT_IS_DONE) {
if ( event.result )
stored_event.status = static_cast<int>(EventStatus::SATISFIED);
else
stored_event.status = static_cast<int>(EventStatus::FAILED);
} else
UNREACHABLE("Mission event formula should be marked MEF_EVENT_IS_DONE at end-of-mission");
}
}
void mission_campaign_store_variables(int persistence_type, bool store_red_alert)
{
int cur, i, j;
cmission *mission_obj;
if (!(Game_mode & GM_CAMPAIGN_MODE) || (Campaign.current_mission < 0))
return;
cur = Campaign.current_mission;
mission_obj = &Campaign.missions[cur];
// handle variables that are saved on mission victory -------------------------------------
mission_obj->variables.clear();
int num_mission_variables = sexp_campaign_file_variable_count();
if (num_mission_variables > 0) {
if (store_red_alert) {
for (auto& current_rav : Campaign.red_alert_variables) {
Campaign.persistent_variables.push_back(current_rav);
}
}
for (i = 0; i < sexp_variable_count(); i++) {
if (!(Sexp_variables[i].type & SEXP_VARIABLE_SAVE_TO_PLAYER_FILE)) {
if (Sexp_variables[i].type & persistence_type) {
bool add_it = true;
// see if we already have a variable with this name
for (j = 0; j < (int)Campaign.persistent_variables.size(); j++) {
if (!(stricmp(Sexp_variables[i].variable_name, Campaign.persistent_variables[j].variable_name))) {
add_it = false;
Campaign.persistent_variables[j].type = Sexp_variables[i].type;
strcpy_s(Campaign.persistent_variables[j].text, Sexp_variables[i].text);
break;
}
}
// new variable
if (add_it) {
Campaign.persistent_variables.push_back(Sexp_variables[i]);
}
}
}
// we might need to save some eternal variables
else if ((persistence_type & SEXP_VARIABLE_SAVE_ON_MISSION_PROGRESS) && (Sexp_variables[i].type & persistence_type) && (Sexp_variables[i].type & SEXP_VARIABLE_SAVE_TO_PLAYER_FILE)) {
bool add_it = true;
for (j = 0; j < (int)Player->variables.size(); j++) {
if (!(stricmp(Sexp_variables[i].variable_name, Player->variables[j].variable_name))) {
Player->variables[j] = Sexp_variables[i];
add_it = false;
break;
}
}
// if not found then add new entry
if (add_it) {
Player->variables.push_back(Sexp_variables[i]);
}
}
}
}
}
// jg18 - adapted from mission_campaign_store_variables()
void mission_campaign_store_containers(ContainerType persistence_type, bool store_red_alert)
{
if (!(Game_mode & GM_CAMPAIGN_MODE) || (Campaign.current_mission < 0))
return;
if (!sexp_container_has_persistent_non_eternal_containers()) {
// nothing to do
return;
}
if (store_red_alert) {
for (const auto& current_con : Campaign.red_alert_containers) {
Campaign.persistent_containers.emplace_back(current_con);
}
}
for (const auto &container : get_all_sexp_containers()) {
if (!container.is_eternal()) {
if (any(container.type & persistence_type)) {
// see if we already have a container with this name
auto cpc_it = std::find_if(Campaign.persistent_containers.begin(),
Campaign.persistent_containers.end(),
[container](const sexp_container &cpc) {
return cpc.name_matches(container);
});
if (cpc_it != Campaign.persistent_containers.end()) {
*cpc_it = container;
} else {
// new container
Campaign.persistent_containers.emplace_back(container);
}
}
} else if (any(persistence_type & ContainerType::SAVE_ON_MISSION_PROGRESS) &&
any(container.type & persistence_type) && container.is_eternal()) {
// we might need to save some eternal player-persistent containers
auto ppc_it = std::find_if(Player->containers.begin(),
Player->containers.end(),
[container](const sexp_container &ppc) {
return ppc.name_matches(container);
});
if (ppc_it != Player->containers.end()) {
*ppc_it = container;
} else {
// new player-persistent container
Player->containers.emplace_back(container);
}
}
}
}
void mission_campaign_store_goals_and_events_and_variables()
{
mission_campaign_store_goals_and_events();
mission_campaign_store_variables(SEXP_VARIABLE_SAVE_ON_MISSION_PROGRESS);
mission_campaign_store_containers(ContainerType::SAVE_ON_MISSION_PROGRESS);
}
/**
* Called when the player's mission is over. It updates the internal store of goals
* and their status then saves the state of the campaign in the campaign file.
* This gets called after player accepts mission results in debriefing.
*/
void mission_campaign_mission_over(bool do_next_mission)
{
int mission_num, i;
cmission *mission_obj;
// I don't think that we should have a record for these -- maybe we might?????? If we do,
// then we should free them
if ( !(Game_mode & GM_CAMPAIGN_MODE) ){
return;
}
mission_num = Campaign.current_mission;
Assert( mission_num != -1 );
mission_obj = &Campaign.missions[mission_num];
// determine if any ships/weapons were granted this mission
for ( i=0; i<Num_granted_ships; i++ ){
Campaign.ships_allowed[Granted_ships[i]] = 1;
}
for ( i=0; i<Num_granted_weapons; i++ ){
Campaign.weapons_allowed[Granted_weapons[i]] = 1;
}
// Goober5000 - player-persistent variables are handled when the mission is
// over, not necessarily when the mission is accepted
// update campaign.mission stats (used to allow backout inRedAlert)
// .. but we don't do this if we are inside of the prev/current loop hack
if ( Campaign.prev_mission != Campaign.current_mission ) {
mission_obj->stats.assign( Player->stats );
if(!(Game_mode & GM_MULTIPLAYER)){
scoring_backout_accept( &mission_obj->stats );
}
}
// if we are moving to a new mission, then change our data. If we are staying on the same mission,
// then we don't want to do anything. Remove information about goals/events
if ( Campaign.next_mission != mission_num ) {
Campaign.prev_mission = mission_num;
Campaign.current_mission = -1;
// very minor support for non-linear campaigns - taylor
if (Campaign.missions[mission_num].completed != 1) {
Campaign.num_missions_completed++;
Campaign.missions[mission_num].completed = 1;
}
// save the scoring values from the previous mission at the start of this mission -- for red alert
// save the state of the campaign in the campaign save file and move to the end_game state
if (Campaign.type == CAMPAIGN_TYPE_SINGLE) {
Pilot.save_savefile();
}
// runs the new scripting conditional hook, "On Campaign Mission Accept" --wookieejedi
scripting::hooks::OnCampaignMissionAccept->run();
} else {
// free up the goals and events which were just malloced. It's kind of like erasing any fact
// that the player played this mission in the campaign.
mission_obj->goals.clear();
mission_obj->events.clear();
mission_obj->variables.clear();
Sexp_nodes[mission_obj->formula].value = SEXP_UNKNOWN;
}
if (do_next_mission)
mission_campaign_next_mission(); // sets up whatever needs to be set to actually play next mission
}
/**
* Called when the game closes -- to get rid of memory errors for Bounds checker
* also called at campaign init and campaign load
*/
void mission_campaign_clear()
{
int i;
if (Campaign.desc != NULL) {
vm_free(Campaign.desc);
Campaign.desc = NULL;
}
// be sure to remove all old malloced strings of Mission_names
// we must also free any goal stuff that was from a previous campaign
for ( i=0; i<Campaign.num_missions; i++ ) {
if ( Campaign.missions[i].name != NULL ) {
vm_free(Campaign.missions[i].name);
Campaign.missions[i].name = NULL;
}
if (Campaign.missions[i].notes != NULL) {
vm_free(Campaign.missions[i].notes);
Campaign.missions[i].notes = NULL;
}
Campaign.missions[i].goals.clear();
Campaign.missions[i].events.clear();
Campaign.missions[i].variables.clear();
// the next three are strdup'd return values from parselo.cpp - taylor
if (Campaign.missions[i].mission_branch_desc != NULL) {
vm_free(Campaign.missions[i].mission_branch_desc);
Campaign.missions[i].mission_branch_desc = NULL;
}
if (Campaign.missions[i].mission_branch_brief_anim != NULL) {
vm_free(Campaign.missions[i].mission_branch_brief_anim);
Campaign.missions[i].mission_branch_brief_anim = NULL;
}
if (Campaign.missions[i].mission_branch_brief_sound != NULL) {
vm_free(Campaign.missions[i].mission_branch_brief_sound);
Campaign.missions[i].mission_branch_brief_sound = NULL;
}
if ( !Fred_running ){
sexp_unmark_persistent(Campaign.missions[i].formula); // free any sexpression nodes used by campaign.
}
memset(Campaign.missions[i].briefing_cutscene, 0, NAME_LENGTH);
Campaign.missions[i].formula = 0;
Campaign.missions[i].completed = 0;
Campaign.missions[i].mission_loop_formula = 0;
Campaign.missions[i].level = 0;
Campaign.missions[i].pos = 0;
Campaign.missions[i].flags = 0;
Campaign.missions[i].main_hall = "";
Campaign.missions[i].debrief_persona_index = 0;
Campaign.missions[i].stats.init();
}
memset(Campaign.name, 0, NAME_LENGTH);
memset(Campaign.filename, 0, MAX_FILENAME_LEN);
Campaign.type = 0;
Campaign.flags = CF_DEFAULT_VALUE;
Campaign.num_missions = 0;
Campaign.num_missions_completed = 0;
Campaign.current_mission = -1;
Campaign.next_mission = 0;
Campaign.prev_mission = -1;
Campaign.loop_enabled = 0;
Campaign.loop_mission = CAMPAIGN_LOOP_MISSION_UNINITIALIZED;
Campaign.loop_reentry = 0;
Campaign.realign_required = 0;
Campaign.num_players = 0;
memset( Campaign.ships_allowed, 0, sizeof(Campaign.ships_allowed) );
memset( Campaign.weapons_allowed, 0, sizeof(Campaign.weapons_allowed) );
Campaign.persistent_variables.clear();
Campaign.red_alert_variables.clear();
Campaign.persistent_containers.clear();
Campaign.red_alert_containers.clear();
}
/**
* Extract the mission filenames for a campaign.
*
* @param filename Name of campaign file
* @param dest Storage for the mission filename, must be already allocated
* @param num Output parameter for the number of mission filenames in the campaign
*
* note that dest should allocate at least dest[MAX_CAMPAIGN_MISSIONS][NAME_LENGTH]
*/
int mission_campaign_get_filenames(char *filename, char dest[][NAME_LENGTH], int *num)
{
// read the mission file and get the list of mission filenames
try
{
Assert( strlen(filename) < MAX_FILENAME_LEN ); // make sure no overflow
read_file_text(filename);
reset_parse();
required_string("$Name:");
advance_to_eoln(NULL);
required_string( "$Type:" );
advance_to_eoln(NULL);
// parse the mission file and actually read in the mission stuff
*num = 0;
while ( skip_to_string("$Mission:") == 1 ) {
stuff_string(dest[*num], F_NAME, NAME_LENGTH);
(*num)++;
}
}
catch (const parse::ParseException& e)
{
mprintf(("MISSIONCAMPAIGN: Unable to parse '%s'! Error message = %s.\n", filename, e.what()));
return 1;
}
return 0;
}
SCP_string mission_campaign_get_name(const char* filename)
{
// read the mission file and only read the name entry
SCP_string filename_str = filename;
filename_str += FS_CAMPAIGN_FILE_EXT;
try {
Assertion(filename_str.size() < MAX_FILENAME_LEN,
"Filename (%s) is too long. Is " SIZE_T_ARG " bytes long but maximum is %d.", filename_str.c_str(),
filename_str.size(), MAX_FILENAME_LEN); // make sure no overflow
read_file_text(filename_str.c_str());
reset_parse();
required_string("$Name:");
SCP_string res;
stuff_string(res, F_NAME);
return res;
} catch (const parse::ParseException& e) {
mprintf(("MISSIONCAMPAIGN: Unable to parse '%s'! Error message = %s.\n", filename_str.c_str(), e.what()));
return SCP_string();
}
}
/**
* Read the goals and events from a mission in a campaign file and store that information
* in the campaign structure for use in the campaign editor, error checking, etc
*/
void read_mission_goal_list(int num)
{
char *filename, notes[NOTES_LENGTH];
int z;
Assertion(num >= 0 && num < Campaign.num_missions, "mission number out of range!");
filename = Campaign.missions[num].name;
if (Campaign.missions[num].notes) {
vm_free(Campaign.missions[num].notes);
Campaign.missions[num].notes = nullptr;
}
Campaign.missions[num].events.clear();
Campaign.missions[num].goals.clear();
Campaign.missions[num].variables.clear();
try
{
read_file_text(filename);
reset_parse();
init_sexp();
// first, read the mission notes for this mission. Used in campaign editor
if (skip_to_string("#Mission Info")) {
if (skip_to_string("$Notes:")) {
stuff_string(notes, F_NOTES, NOTES_LENGTH);
Campaign.missions[num].notes = (char *)vm_malloc(strlen(notes) + 1);
strcpy(Campaign.missions[num].notes, notes);
}
}
// skip to events section in the mission file. Events come before goals, so we process them first
if (skip_to_string("#Events")) {
while (true) {
if (skip_to_string("$Formula:", "#Goals") != 1){
break;
}
z = skip_to_string("+Name:", "$Formula:");
if (!z){
break;
}
Campaign.missions[num].events.emplace_back();
auto& stored_event = Campaign.missions[num].events.back();
if (z == 1)
stuff_string(stored_event.name, F_NAME, NAME_LENGTH);
else
sprintf(stored_event.name, NOX("Event #" SIZE_T_ARG), &stored_event - &Campaign.missions[num].events[0] + 1);
}
}
if (skip_to_string("#Goals")) {
while (true) {
if (skip_to_string("$Type:", "#End") != 1){
break;
}
z = skip_to_string("+Name:", "$Type:");
if (!z){
break;
}
Campaign.missions[num].goals.emplace_back();
auto& stored_goal = Campaign.missions[num].goals.back();
if (z == 1)
stuff_string(stored_goal.name, F_NAME, NAME_LENGTH);
else
sprintf(stored_goal.name, NOX("Goal #" SIZE_T_ARG), &stored_goal - &Campaign.missions[num].goals[0] + 1);
}
}
// Goober5000 - variables do not need to be read here
}
catch (const parse::ParseException& e)
{
mprintf(("MISSIONCAMPAIGN: Unable to parse '%s'! Error message = %s.\n", filename, e.what()));
return;
}
}
/**
* Return index into Campaign's list of missions of the mission with the given
* filename.
*
* This function tried to be a little smart about filename looking for the .fsm
* extension since filenames are stored with the extension in the campaign file.
*
* @return index of mission in campaign structure. -1 if mission name not found.
*/
int mission_campaign_find_mission( const char *name )
{
int i;
char realname[_MAX_PATH];
if (name == NULL)
return -1;
// look for an extension on the file. If no extension, add default ".fsm" onto the
// end of the filename
strcpy_s(realname, name );
if ( strchr(name, '.') == NULL ){
sprintf(realname, NOX("%s%s"), name, FS_MISSION_FILE_EXT );
}
for (i = 0; i < Campaign.num_missions; i++ ) {
if ( !stricmp(realname, Campaign.missions[i].name) ){
return i;
}
}
return -1;
}
void mission_campaign_maybe_play_movie(int type)
{
int mission_idx;
char *filename;
// only support pre mission movies for now.
Assert ( type == CAMPAIGN_MOVIE_PRE_MISSION );
if ( !(Game_mode & GM_CAMPAIGN_MODE) )
return;
mission_idx = Campaign.current_mission;
Assert( mission_idx != -1 );
// get a possible filename for a movie to play.
filename = NULL;
switch( type ) {
case CAMPAIGN_MOVIE_PRE_MISSION:
if ( strlen(Campaign.missions[mission_idx].briefing_cutscene) )
filename = Campaign.missions[mission_idx].briefing_cutscene;
break;
default:
Int3();
break;
}
// no filename, no movie!
if ( !filename )
return;
movie::play(filename); //Play the movie!
cutscene_mark_viewable( filename );
}
/**
* Return the type of campaign of the passed filename
*/
int mission_campaign_parse_is_multi(const char *filename, char *name)
{
int i;
char temp[NAME_LENGTH];
try
{
read_file_text(filename);
reset_parse();
required_string("$Name:");
stuff_string(temp, F_NAME, NAME_LENGTH);
if (name)
strcpy(name, temp);
required_string("$Type:");
stuff_string(temp, F_NAME, NAME_LENGTH);
for (i = 0; i < MAX_CAMPAIGN_TYPES; i++) {
if (!stricmp(temp, campaign_types[i])) {
return i;
}
}
Error(LOCATION, "Unknown campaign type %s", temp);
return -1;
}
catch (const parse::ParseException& e)
{
mprintf(("MISSIONCAMPAIGN: Unable to parse '%s'! Error message = %s.\n", filename, e.what()));
return -1;
}
}
/**
* Save persistent information during a mission -- which then might or might not get
* saved out when the mission is over depending on whether player replays mission or commits.
*/
void mission_campaign_save_persistent( int type, int sindex )
{
// based on the type of information, save it off for possible saving into the campsign
// savefile when the mission is over
if ( type == CAMPAIGN_PERSISTENT_SHIP ) {
Assert( Num_granted_ships < MAX_SHIP_CLASSES );
Granted_ships[Num_granted_ships] = sindex;
Num_granted_ships++;
} else if ( type == CAMPAIGN_PERSISTENT_WEAPON ) {
Assert( Num_granted_weapons < MAX_WEAPON_TYPES );
Granted_weapons[Num_granted_weapons] = sindex;
Num_granted_weapons++;
} else
Int3();
}
bool campaign_is_ignored(const char *filename)
{
SCP_string filename_no_ext = filename;
drop_extension(filename_no_ext);
SCP_tolower(filename_no_ext);
for (auto &ii: Ignored_campaigns) {
if (ii == filename_no_ext) {
return true;
}
}
return false;
}
static bool campaign_reportable_error(int val)
{
return val != 0 && val != CAMPAIGN_ERROR_MISSING && val != CAMPAIGN_ERROR_IGNORED;
}
// returns 0: loaded, !0: error
int mission_load_up_campaign(bool fall_back_from_current)
{
int rc = -1, idx;
auto pl = Player;
// find best campaign to use:
// 1) last used
// 2) builtin
// 3) anything else
// Note that in each step we only fall back when the error is benign, e.g. ignored or missing;
// if there's some other real error with the campaign file, we report it.
// Also note that if we *have* a current campaign, we shouldn't fall back *at all*, lest we repeatedly
// reset what the current campaign is, *unless* we are starting a brand new session or loading a new pilot.
// last used...
if ( strlen(pl->current_campaign) ) {
rc = mission_campaign_load(pl->current_campaign, nullptr, pl);
if (rc == 0 || !fall_back_from_current || campaign_reportable_error(rc)) {
return rc;
}
}
// builtin...
rc = mission_campaign_load(Default_campaign_file_name, nullptr, pl);
// everything else...
if (rc < 0 && !campaign_reportable_error(rc)) {
// no descriptions, no sorting
mission_campaign_build_list(false, false);
for (idx = 0; (idx < Num_campaigns) && (rc < 0); idx++) {
if ( (Campaign_file_names[idx] == NULL) || !strlen(Campaign_file_names[idx]) ) {
continue;
}
// skip current and builtin since they already didn't work
if ( !stricmp(Campaign_file_names[idx], pl->current_campaign) ) {
continue;
}
if ( !stricmp(Campaign_file_names[idx], BUILTIN_CAMPAIGN) ) {
continue;
}
// try to load it, whatever "it" is
rc = mission_campaign_load(Campaign_file_names[idx], nullptr, pl);
}
mission_campaign_free_list();
}
// update pilot with the new current campaign
if (rc == 0) {
strcpy_s(pl->current_campaign, Campaign.filename);
}
return rc;
}
/**
* For end of campaign in the single player game. Called when the end of campaign state is
* entered, which is triggered when the end-campaign sexpression is hit
*/
void mission_campaign_end_init()
{
// no need to do any initialization.
}
void mission_campaign_end_do()
{
// close out the mission
event_music_level_close();
mission_goal_fail_incomplete();
scoring_level_close();
mission_campaign_mission_over();
// play the movies
// eventually we'll want to play one of two options (good ending or bad ending)
// this is specific to the FreeSpace 2 single-player campaign
if (!stricmp(Campaign.filename, "freespace2")) {
// did the supernova blow?
if (supernova_stage() >= SUPERNOVA_STAGE::HIT) {
movie::play_two("endpart1.mve", "endprt2b.mve"); // bad ending
} else {
movie::play_two("endpart1.mve", "endprt2a.mve"); // good ending
}
} else {
common_maybe_play_cutscene(MOVIE_END_CAMPAIGN);
}
gameseq_post_event( GS_EVENT_MAIN_MENU );
}
void mission_campaign_end_close()
{
// no need to do any initialization.
}
/**
* Skip to the next mission in the campaign
* this also posts the state change
*/
void mission_campaign_skip_to_next()
{
// mark mission as skipped
Campaign.missions[Campaign.current_mission].flags |= CMISSION_FLAG_SKIPPED;
// mark all goals/events complete
// these do not really matter, since is-previous-event-* and is-previous-goal-* sexps check
// to see if the mission was skipped, and use defaults accordingly.
mission_goal_mark_objectives_complete();
mission_goal_mark_events_complete();
// store
mission_campaign_store_goals_and_events_and_variables();
// now set the next mission
mission_campaign_eval_next_mission();
// clear out relevant player vars
Player->failures_this_session = 0;
Player->show_skip_popup = 1;
// proceed to next mission or main hall
if ((Campaign.missions[Campaign.current_mission].flags & CMISSION_FLAG_HAS_LOOP) && (Campaign.loop_mission != -1)) {
// go to loop solicitation
gameseq_post_event(GS_EVENT_LOOP_BRIEF);
} else {
// closes out mission stuff, sets up next one
mission_campaign_mission_over();
if ( Campaign.next_mission == -1 || (The_mission.flags[Mission::Mission_Flags::End_to_mainhall]) ) {
// go to main hall, either the campaign is over or the FREDer requested it.
gameseq_post_event(GS_EVENT_MAIN_MENU);
} else {
// go to next mission
gameseq_post_event(GS_EVENT_START_GAME);
}
}
}
/**
* Breaks your ass out of the loop
* this also posts the state change
*/
void mission_campaign_exit_loop()
{
// set campaign to loop reentry point
Campaign.next_mission = Campaign.loop_reentry;
Campaign.current_mission = -1;
Campaign.loop_enabled = 0;
// set things up for next mission
mission_campaign_next_mission();
if ( Campaign.next_mission == -1 || (The_mission.flags[Mission::Mission_Flags::End_to_mainhall]) ) {
gameseq_post_event( GS_EVENT_MAIN_MENU );
} else {
gameseq_post_event( GS_EVENT_START_GAME );
}
}
/**
* Used for jumping to a particular campaign mission
* all previous missions marked skipped
* this relies on correct mission ordering in the campaign file
*/
void mission_campaign_jump_to_mission(const char* name, bool no_skip)
{
int i = 0, mission_num = -1;
char dest_name[64], *p;
// load in the campaign junk
mission_load_up_campaign();
// tack the .fs2 onto the input name
strcpy_s(dest_name, name);
p = strchr(dest_name, '.');
if (p != NULL)
*p = '\0';
strcat_s(dest_name, ".fs2");
// search for our mission
for (i = 0; i < Campaign.num_missions; i++) {
if ((Campaign.missions[i].name != NULL) && !stricmp(Campaign.missions[i].name, dest_name)) {
mission_num = i;
break;
} else if (!no_skip) {
Campaign.missions[i].flags |= CMISSION_FLAG_SKIPPED;
Campaign.missions[i].completed = 1;
Campaign.num_missions_completed = i + 1;
}
}
if (mission_num < 0) {
// if we got here, no match was found
// restart the campaign
mission_campaign_savefile_delete(Campaign.filename);
mission_campaign_load(Campaign.filename);
} else {
for (SCP_vector<ship_info>::iterator it = Ship_info.begin(); it != Ship_info.end(); it++) {
i = static_cast<int>(std::distance(Ship_info.begin(), it));
Campaign.ships_allowed[i] = 1;
}
for (i = 0; i < weapon_info_size(); i++) {
Campaign.weapons_allowed[i] = 1;
}
Campaign.next_mission = mission_num;
Campaign.prev_mission = mission_num - 1;
mission_campaign_next_mission();
Game_mode |= GM_CAMPAIGN_MODE;
gameseq_post_event(GS_EVENT_START_GAME);
}
}
// Goober5000
void mission_campaign_save_on_close_variables()
{
int i;
// make sure we are actually playing a single-player campaign
if (!(Game_mode & GM_CAMPAIGN_MODE) || (Campaign.type != CAMPAIGN_TYPE_SINGLE) || (Campaign.current_mission < 0))
return;
// now save variables
for (i = 0; i < sexp_variable_count(); i++) {
// we only want the on mission close type. On campaign progress type are dealt with elsewhere
if ( !(Sexp_variables[i].type & SEXP_VARIABLE_SAVE_ON_MISSION_CLOSE) ) {
continue;
}
bool found = false;
// deal with eternals
if ((Sexp_variables[i].type & SEXP_VARIABLE_SAVE_TO_PLAYER_FILE)) {
// check if variable already exists and updated it
for (auto& current_variable : Player->variables) {
if (!(stricmp(Sexp_variables[i].variable_name, current_variable.variable_name))) {
current_variable = Sexp_variables[i];
found = true;
break;
}
}
// if not found then add new entry
if (!found) {
Player->variables.push_back(Sexp_variables[i]);
}
}
}
// store any non-eternal on mission close variables
mission_campaign_store_variables(SEXP_VARIABLE_SAVE_ON_MISSION_CLOSE, false);
}
// jg18 - adapted from mission_campaign_save_on_close_variables()
void mission_campaign_save_on_close_containers()
{
// make sure we are actually playing a single-player campaign
if (!(Game_mode & GM_CAMPAIGN_MODE) || (Campaign.type != CAMPAIGN_TYPE_SINGLE) || (Campaign.current_mission < 0))
return;
// now save containers
for (const auto &container : get_all_sexp_containers()) {
// we only want the on mission close type. On campaign progress type are dealt with elsewhere
if (none(container.type & ContainerType::SAVE_ON_MISSION_CLOSE)) {
continue;
}
// deal with eternals
if (container.is_eternal()) {
// check if container already exists and update it
auto ppc_it = std::find_if(Player->containers.begin(),
Player->containers.end(),
[container](const sexp_container &ppc) {
return ppc.name_matches(container);
});
if (ppc_it != Player->containers.end()) {
*ppc_it = container;
} else {
// if not found then add new entry
Player->containers.emplace_back(container);
}
}
}
// store any non-eternal on mission close containers
mission_campaign_store_containers(ContainerType::SAVE_ON_MISSION_CLOSE, false);
}
void mission_campaign_load_failure_popup()
{
if (Campaign_load_failure == 0) {
return;
}
if (Campaign_load_failure == CAMPAIGN_ERROR_CORRUPT) {
popup(PF_TITLE_BIG | PF_TITLE_RED | PF_USE_AFFIRMATIVE_ICON | PF_NO_NETWORKING, 1, POPUP_OK, XSTR("Error!\n\nRequested campaign is corrupt and cannot be loaded.\n\n"
"Please select a different campaign in the Campaign Room.", 1614));
} else if (Campaign_load_failure == CAMPAIGN_ERROR_SEXP_EXHAUSTED) {
popup(PF_TITLE_BIG | PF_TITLE_RED | PF_USE_AFFIRMATIVE_ICON | PF_NO_NETWORKING, 1, POPUP_OK, XSTR("Error!\n\nRequested campaign requires too many SEXPs and cannot be loaded.\n\n"
"Please select a different campaign in the Campaign Room.", 1615));
} else if (Campaign_load_failure == CAMPAIGN_ERROR_MISSING) {
// if it's just the campaign missing, there's another popup to deal with that
;
} else if (Campaign_load_failure == CAMPAIGN_ERROR_SAVEFILE) {
popup(PF_TITLE_BIG | PF_TITLE_RED | PF_USE_AFFIRMATIVE_ICON | PF_NO_NETWORKING, 1, POPUP_OK, XSTR("Error!\n\nThe pilot savefile "
"for this campaign is invalid for the current mod.\n\nPlease select another campaign or switch to the correct "
"mod in order to use this campaign.", 1617));
}
Campaign_load_failure = 0;
}
|