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
|
/* Mission.cpp
Copyright (c) 2014 by Michael Zahniser
Endless Sky is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
Endless Sky is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "Mission.h"
#include "DataNode.h"
#include "DataWriter.h"
#include "Dialog.h"
#include "DistanceMap.h"
#include "text/Format.h"
#include "GameData.h"
#include "Government.h"
#include "Logger.h"
#include "Messages.h"
#include "Phrase.h"
#include "Planet.h"
#include "PlayerInfo.h"
#include "Random.h"
#include "Ship.h"
#include "ShipEvent.h"
#include "System.h"
#include "UI.h"
#include <cmath>
#include <limits>
#include <sstream>
using namespace std;
namespace {
// Pick a random commodity that would make sense to be exported from the
// first system to the second.
const Trade::Commodity *PickCommodity(const System &from, const System &to)
{
vector<int> weight;
int total = 0;
for(const Trade::Commodity &commodity : GameData::Commodities())
{
// For every 100 credits in profit you can make, double the chance
// of this commodity being chosen.
double profit = to.Trade(commodity.name) - from.Trade(commodity.name);
int w = max<int>(1, 100. * pow(2., profit * .01));
weight.push_back(w);
total += w;
}
total += !total;
// Pick a random commodity based on those weights.
int r = Random::Int(total);
for(unsigned i = 0; i < weight.size(); ++i)
{
r -= weight[i];
if(r < 0)
return &GameData::Commodities()[i];
}
// Control will never reach here, but to satisfy the compiler:
return nullptr;
}
// If a source, destination, waypoint, or stopover supplies more than one explicit choice
// or a mixture of explicit choice and location filter, print a warning.
void ParseMixedSpecificity(const DataNode &node, string &&kind, int expected)
{
if(node.Size() >= expected + 1)
node.PrintTrace("Warning: use a location filter to choose from multiple " + kind + "s:");
if(node.HasChildren())
node.PrintTrace("Warning: location filter ignored due to use of explicit " + kind + ":");
}
string TriggerToText(Mission::Trigger trigger)
{
switch(trigger)
{
case Mission::Trigger::ABORT:
return "on abort";
case Mission::Trigger::ACCEPT:
return "on accept";
case Mission::Trigger::COMPLETE:
return "on complete";
case Mission::Trigger::DECLINE:
return "on decline";
case Mission::Trigger::DEFER:
return "on defer";
case Mission::Trigger::FAIL:
return "on fail";
case Mission::Trigger::OFFER:
return "on offer";
case Mission::Trigger::STOPOVER:
return "on stopover";
case Mission::Trigger::VISIT:
return "on visit";
case Mission::Trigger::WAYPOINT:
return "on waypoint";
case Mission::Trigger::DAILY:
return "on daily";
case Mission::Trigger::DISABLED:
return "on disabled";
default:
return "unknown trigger";
}
}
}
// Construct and Load() at the same time.
Mission::Mission(const DataNode &node, const ConditionsStore *playerConditions,
const set<const System *> *visitedSystems, const set<const Planet *> *visitedPlanets)
{
Load(node, playerConditions, visitedSystems, visitedPlanets);
}
// Load a mission, either from the game data or from a saved game.
void Mission::Load(const DataNode &node, const ConditionsStore *playerConditions,
const set<const System *> *visitedSystems, const set<const Planet *> *visitedPlanets)
{
// All missions need a name.
if(node.Size() < 2)
{
node.PrintTrace("Error: No name specified for mission:");
return;
}
// If a mission object is "loaded" twice, that is most likely an error (e.g.
// due to a plugin containing a mission with the same name as the base game
// or another plugin). This class is not designed to allow merging or
// overriding of mission data from two different definitions.
if(!trueName.empty())
{
node.PrintTrace("Error: Duplicate definition of mission:");
return;
}
trueName = node.Token(1);
if(!DataNode::IsConditionName(trueName))
node.PrintTrace("Error: Invalid mission name");
for(const DataNode &child : node)
{
const string &key = child.Token(0);
bool hasValue = child.Size() >= 2;
if(key == "name" && hasValue)
displayName = child.Token(1);
else if(key == "uuid" && hasValue)
uuid = EsUuid::FromString(child.Token(1));
else if(key == "description" && hasValue)
description = child.Token(1);
else if(key == "blocked" && hasValue)
blocked = child.Token(1);
else if(key == "deadline" && child.Size() >= 4)
deadline = Date(child.Value(1), child.Value(2), child.Value(3));
else if(key == "deadline")
{
if(child.Size() == 1)
deadlineMultiplier += 2;
if(child.Size() >= 2)
deadlineBase += child.Value(1);
if(child.Size() >= 3)
deadlineMultiplier += child.Value(2);
}
else if(key == "distance calculation settings" && child.HasChildren())
{
distanceCalcSettings.Load(child);
}
else if(key == "cargo" && child.Size() >= 3)
{
cargo = child.Token(1);
cargoSize = child.Value(2);
if(child.Size() >= 4)
cargoLimit = child.Value(3);
if(child.Size() >= 5)
cargoProb = child.Value(4);
for(const DataNode &grand : child)
{
if(!ParseContraband(grand))
grand.PrintTrace("Skipping unrecognized attribute:");
else
grand.PrintTrace("Warning: Deprecated use of \"stealth\" and \"illegal\" as a child of \"cargo\"."
" They are now mission-level properties:");
}
}
else if(key == "passengers" && hasValue)
{
passengers = child.Value(1);
if(child.Size() >= 3)
passengerLimit = child.Value(2);
if(child.Size() >= 4)
passengerProb = child.Value(3);
}
else if(key == "apparent payment" && hasValue)
paymentApparent = child.Value(1);
else if(ParseContraband(child))
{
// This was an "illegal" or "stealth" entry. It has already been
// parsed, so nothing more needs to be done here.
}
else if(key == "invisible")
isVisible = false;
else if(key == "priority")
hasPriority = true;
else if(key == "non-blocking")
isNonBlocking = true;
else if(key == "minor")
isMinor = true;
else if(key == "offer precedence" && hasValue)
offerPrecedence = child.Value(1);
else if(key == "autosave")
autosave = true;
else if(key == "job")
location = JOB;
else if(key == "landing")
location = LANDING;
else if(key == "assisting")
location = ASSISTING;
else if(key == "boarding")
{
location = BOARDING;
for(const DataNode &grand : child)
if(grand.Token(0) == "override capture")
overridesCapture = true;
else
grand.PrintTrace("Skipping unrecognized attribute:");
}
else if(key == "shipyard")
location = SHIPYARD;
else if(key == "outfitter")
location = OUTFITTER;
else if(key == "job board")
location = JOB_BOARD;
else if(key == "entering")
location = ENTERING;
else if(key == "repeat")
repeat = (child.Size() == 1 ? 0 : static_cast<int>(child.Value(1)));
else if(key == "clearance")
{
clearance = (child.Size() == 1 ? "auto" : child.Token(1));
clearanceFilter.Load(child, visitedSystems, visitedPlanets);
}
else if(child.Size() == 2 && key == "ignore" && child.Token(1) == "clearance")
ignoreClearance = true;
else if(key == "infiltrating")
hasFullClearance = false;
else if(key == "failed")
hasFailed = true;
else if(key == "to" && hasValue)
{
if(child.Token(1) == "offer")
toOffer.Load(child, playerConditions);
else if(child.Token(1) == "complete")
toComplete.Load(child, playerConditions);
else if(child.Token(1) == "fail")
toFail.Load(child, playerConditions);
else if(child.Token(1) == "accept")
toAccept.Load(child, playerConditions);
else
child.PrintTrace("Skipping unrecognized attribute:");
}
else if(key == "source" && hasValue)
{
source = GameData::Planets().Get(child.Token(1));
ParseMixedSpecificity(child, "planet", 2);
}
else if(key == "source")
sourceFilter.Load(child, visitedSystems, visitedPlanets);
else if(key == "destination" && child.Size() == 2)
{
destination = GameData::Planets().Get(child.Token(1));
ParseMixedSpecificity(child, "planet", 2);
}
else if(key == "destination")
destinationFilter.Load(child, visitedSystems, visitedPlanets);
else if(key == "complete")
{
if(!hasValue)
child.PrintTrace("Incomplete \"complete\" option. Follow \"complete\" with \"at\".");
else if(child.Token(1) == "at")
{
LocationFilter loaded(child, visitedSystems, visitedPlanets);
if(loaded.IsEmpty())
child.PrintTrace("Error: The \"complete at\" filter must not be empty. Ignoring this filter.");
else
completionFilter = loaded;
}
else
child.PrintTrace("Unknown \"complete\" option \"" + child.Token(1) + '"');
}
else if(key == "waypoint" && hasValue)
{
bool visited = child.Size() >= 3 && child.Token(2) == "visited";
set<const System *> &set = visited ? visitedWaypoints : waypoints;
set.insert(GameData::Systems().Get(child.Token(1)));
ParseMixedSpecificity(child, "system", 2 + visited);
}
else if(key == "waypoint" && child.HasChildren())
waypointFilters.emplace_back(child, visitedSystems, visitedPlanets);
else if(key == "stopover" && hasValue)
{
bool visited = child.Size() >= 3 && child.Token(2) == "visited";
set<const Planet *> &set = visited ? visitedStopovers : stopovers;
set.insert(GameData::Planets().Get(child.Token(1)));
ParseMixedSpecificity(child, "planet", 2 + visited);
}
else if(key == "stopover" && child.HasChildren())
stopoverFilters.emplace_back(child, visitedSystems, visitedPlanets);
else if(key == "mark" && hasValue)
{
bool unmarked = child.Size() >= 3 && child.Token(2) == "unmarked";
set<const System *> &set = unmarked ? unmarkedSystems : markedSystems;
set.insert(GameData::Systems().Get(child.Token(1)));
}
else if(key == "substitutions" && child.HasChildren())
substitutions.Load(child, playerConditions);
else if(key == "npc")
npcs.emplace_back(child, playerConditions, visitedSystems, visitedPlanets);
else if(key == "on" && hasValue && child.Token(1) == "enter")
{
// "on enter" nodes may either name a specific system or use a LocationFilter
// to control the triggering system.
if(child.Size() >= 3)
{
MissionAction &action = onEnter[GameData::Systems().Get(child.Token(2))];
action.Load(child, playerConditions, visitedSystems, visitedPlanets);
}
else
genericOnEnter.emplace_back(child, playerConditions, visitedSystems, visitedPlanets);
}
else if(key == "on" && hasValue)
{
static const map<string, Trigger> trigger = {
{"complete", COMPLETE},
{"offer", OFFER},
{"accept", ACCEPT},
{"decline", DECLINE},
{"fail", FAIL},
{"abort", ABORT},
{"defer", DEFER},
{"visit", VISIT},
{"stopover", STOPOVER},
{"waypoint", WAYPOINT},
{"daily", DAILY},
{"disabled", DISABLED},
};
auto it = trigger.find(child.Token(1));
if(it != trigger.end())
actions[it->second].Load(child, playerConditions, visitedSystems, visitedPlanets);
else
child.PrintTrace("Skipping unrecognized attribute:");
}
else if(key == "color" && child.Size() >= 3)
{
auto setColor = [&child](ExclusiveItem<Color> &color) noexcept -> void {
if(child.Size() >= 5)
color = ExclusiveItem<Color>(Color(child.Value(2), child.Value(3), child.Value(4)));
else
color = ExclusiveItem<Color>(GameData::Colors().Get(child.Token(2)));
};
const string &value = child.Token(1);
if(value == "unavailable")
setColor(unavailable);
else if(value == "unselected")
setColor(unselected);
else if(value == "selected")
setColor(selected);
else
child.PrintTrace("Skipping unrecognized attribute:");
}
else
child.PrintTrace("Skipping unrecognized attribute:");
}
if(displayName.empty())
displayName = trueName;
}
// Save a mission. It is safe to assume that any mission that is being saved
// is already "instantiated," so only a subset of the data must be saved.
void Mission::Save(DataWriter &out, const string &tag) const
{
out.Write(tag, trueName);
out.BeginChild();
{
out.Write("name", displayName);
out.Write("uuid", uuid.ToString());
if(!description.empty())
out.Write("description", description);
if(!blocked.empty())
out.Write("blocked", blocked);
if(deadline)
out.Write("deadline", deadline.Day(), deadline.Month(), deadline.Year());
if(cargoSize)
out.Write("cargo", cargo, cargoSize);
if(passengers)
out.Write("passengers", passengers);
if(paymentApparent)
out.Write("apparent payment", paymentApparent);
if(fine)
out.Write("illegal", fine, fineMessage);
if(failIfDiscovered)
out.Write("stealth");
if(!isVisible)
out.Write("invisible");
auto saveColor = [&out](const ExclusiveItem<Color> &color, string tokenName) noexcept -> void {
if(!color->IsLoaded())
return;
if(!color->TrueName().empty())
out.Write("color", tokenName, color->TrueName());
else
{
const float *rgba = color->Get();
out.Write("color", tokenName, rgba[0], rgba[1], rgba[2]);
}
};
saveColor(unavailable, "unavailable");
saveColor(unselected, "unselected");
saveColor(selected, "selected");
if(hasPriority)
out.Write("priority");
if(isNonBlocking)
out.Write("non-blocking");
if(isMinor)
out.Write("minor");
if(offerPrecedence)
out.Write("offer precedence", offerPrecedence);
if(autosave)
out.Write("autosave");
if(location == LANDING)
out.Write("landing");
else if(location == SHIPYARD)
out.Write("shipyard");
else if(location == OUTFITTER)
out.Write("outfitter");
else if(location == JOB_BOARD)
out.Write("job board");
else if(location == ASSISTING)
out.Write("assisting");
else if(location == BOARDING)
{
out.Write("boarding");
if(overridesCapture)
{
out.BeginChild();
{
out.Write("override capture");
}
out.EndChild();
}
}
else if(location == JOB)
out.Write("job");
else if(location == ENTERING)
out.Write("entering");
if(!clearance.empty())
{
out.Write("clearance", clearance);
clearanceFilter.Save(out);
}
if(ignoreClearance)
out.Write("ignore", "clearance");
if(!hasFullClearance)
out.Write("infiltrating");
if(hasFailed)
out.Write("failed");
if(repeat != 1)
out.Write("repeat", repeat);
if(!toOffer.IsEmpty())
{
out.Write("to", "offer");
out.BeginChild();
{
toOffer.Save(out);
}
out.EndChild();
}
if(!toAccept.IsEmpty())
{
out.Write("to", "accept");
out.BeginChild();
{
toAccept.Save(out);
}
out.EndChild();
}
if(!toComplete.IsEmpty())
{
out.Write("to", "complete");
out.BeginChild();
{
toComplete.Save(out);
}
out.EndChild();
}
if(!toFail.IsEmpty())
{
out.Write("to", "fail");
out.BeginChild();
{
toFail.Save(out);
}
out.EndChild();
}
if(destination)
out.Write("destination", destination->TrueName());
if(!completionFilter.IsEmpty() && completionFilter.IsValid())
{
out.Write("complete", "at");
completionFilter.Save(out);
}
for(const System *system : waypoints)
out.Write("waypoint", system->TrueName());
for(const System *system : visitedWaypoints)
out.Write("waypoint", system->TrueName(), "visited");
for(const Planet *planet : stopovers)
out.Write("stopover", planet->TrueName());
for(const Planet *planet : visitedStopovers)
out.Write("stopover", planet->TrueName(), "visited");
for(const System *system : markedSystems)
out.Write("mark", system->TrueName());
for(const System *system : unmarkedSystems)
out.Write("mark", system->TrueName(), "unmarked");
for(const NPC &npc : npcs)
npc.Save(out);
// Save all the actions, because this might be an "available mission" that
// has not been received yet but must still be included in the saved game.
for(const auto &it : actions)
it.second.Save(out);
// Save any "on enter" actions that have not been performed.
for(const auto &it : onEnter)
if(!didEnter.contains(&it.second))
it.second.Save(out);
for(const MissionAction &action : genericOnEnter)
if(!didEnter.contains(&action))
action.Save(out);
}
out.EndChild();
}
void Mission::NeverOffer()
{
toOffer.MakeNever();
}
// Basic mission information.
const EsUuid &Mission::UUID() const noexcept
{
return uuid;
}
// Get the internal name used for this mission. This name is unique and is
// never modified by string substitution, so it can be used in condition
// variables, etc.
const string &Mission::TrueName() const
{
return trueName;
}
const string &Mission::DisplayName() const
{
return displayName;
}
const string &Mission::Description() const
{
return description;
}
// Check if this mission should be shown in your mission list. If not, the
// player will not know this mission exists (which is sometimes useful).
bool Mission::IsVisible() const
{
return isVisible;
}
// The colors that should be used to display the mission name if it is shown
// in your mission list.
const Color &Mission::Unavailable() const
{
return *unavailable;
}
const Color &Mission::Unselected() const
{
return *unselected;
}
const Color &Mission::Selected() const
{
return *selected;
}
// Check if this instantiated mission uses any systems, planets, or ships that are
// not fully defined. If everything is fully defined, this is a valid mission.
bool Mission::IsValid() const
{
// Planets must be defined and in a system. However, a source system does not necessarily exist.
if(source && !source->IsValid())
return false;
// Every mission is required to have a destination.
if(!destination || !destination->IsValid())
return false;
// Missions with completion filters must have valid filters.
if(!completionFilter.IsEmpty() && !completionFilter.IsValid())
return false;
// All stopovers must be valid.
for(auto &&planet : Stopovers())
if(!planet->IsValid())
return false;
for(auto &&planet : VisitedStopovers())
if(!planet->IsValid())
return false;
// Systems must have a defined position.
for(auto &&system : Waypoints())
if(!system->IsValid())
return false;
for(auto &&system : VisitedWaypoints())
if(!system->IsValid())
return false;
for(auto &&system : MarkedSystems())
if(!system->IsValid())
return false;
for(auto &&system : UnmarkedSystems())
if(!system->IsValid())
return false;
// Actions triggered when entering a system should reference valid systems.
for(auto &&it : onEnter)
if(!it.first->IsValid() || !it.second.Validate().empty())
return false;
for(auto &&it : actions)
if(!it.second.Validate().empty())
return false;
// Generic "on enter" may use a LocationFilter that exclusively references invalid content.
for(auto &&action : genericOnEnter)
if(!action.Validate().empty())
return false;
if(!clearanceFilter.IsValid())
return false;
// The instantiated NPCs should also be valid.
for(auto &&npc : NPCs())
if(!npc.Validate().empty())
return false;
return true;
}
// Check if this mission has high priority. If any priority missions
// are available, only other priority missions and non-blocking ones can offer alongside it.
bool Mission::HasPriority() const
{
return hasPriority;
}
// Check if this mission is a "non-blocking" mission.
// Such missions will not prevent minor missions from being offered alongside them,
// and will not be prevented from offering by priority missions.
bool Mission::IsNonBlocking() const
{
return isNonBlocking;
}
// Check if this mission is a "minor" mission. Minor missions will only be
// offered if no other missions (minor or otherwise) are being offered.
bool Mission::IsMinor() const
{
return isMinor;
}
int Mission::OfferPrecedence() const
{
return offerPrecedence;
}
bool Mission::IsAtLocation(Location location) const
{
return (this->location == location);
}
// Information about what you are doing.
const Ship *Mission::SourceShip() const
{
return sourceShip;
}
const Planet *Mission::Destination() const
{
return destination;
}
const set<const System *> &Mission::Waypoints() const
{
return waypoints;
}
const set<const System *> &Mission::VisitedWaypoints() const
{
return visitedWaypoints;
}
const set<const Planet *> &Mission::Stopovers() const
{
return stopovers;
}
const set<const Planet *> &Mission::VisitedStopovers() const
{
return visitedStopovers;
}
const set<const System *> &Mission::MarkedSystems() const
{
return markedSystems;
}
const set<const System *> &Mission::UnmarkedSystems() const
{
return unmarkedSystems;
}
void Mission::Mark(const System *system) const
{
markedSystems.insert(system);
unmarkedSystems.erase(system);
}
void Mission::Unmark(const System *system) const
{
if(markedSystems.erase(system))
unmarkedSystems.insert(system);
}
const string &Mission::Cargo() const
{
return cargo;
}
int Mission::CargoSize() const
{
return cargoSize;
}
int Mission::Fine() const
{
return fine;
}
string Mission::FineMessage() const
{
return fineMessage;
}
bool Mission::FailIfDiscovered() const
{
return failIfDiscovered;
}
int Mission::Passengers() const
{
return passengers;
}
int64_t Mission::DisplayedPayment() const
{
return paymentApparent ? paymentApparent : GetAction(Mission::Trigger::COMPLETE).Payment();
}
const int Mission::ExpectedJumps() const
{
return expectedJumps;
}
// The mission must be completed by this deadline (if there is a deadline).
const Date &Mission::Deadline() const
{
return deadline;
}
// If this mission's deadline was before the given date and it has not been
// marked as failing already, mark it and return true.
bool Mission::CheckDeadline(const Date &today)
{
if(!hasFailed && deadline && deadline < today)
{
hasFailed = true;
return true;
}
return false;
}
// Check if you have special clearance to land on your destination.
bool Mission::HasClearance(const Planet *planet) const
{
if(clearance.empty())
return false;
if(planet == destination || stopovers.contains(planet) || visitedStopovers.contains(planet))
return true;
return (!clearanceFilter.IsEmpty() && clearanceFilter.Matches(planet));
}
// Get the string to be shown in the destination planet's hailing dialog. If
// this is "auto", you don't have to hail them to get landing permission.
const string &Mission::ClearanceMessage() const
{
return clearance;
}
// Check whether we have full clearance to land and use the planet's
// services, or whether we are landing in secret ("infiltrating").
bool Mission::HasFullClearance() const
{
return hasFullClearance;
}
// Check if it's possible to offer or complete this mission right now.
bool Mission::CanOffer(const PlayerInfo &player, const shared_ptr<Ship> &boardingShip) const
{
if(location == BOARDING || location == ASSISTING)
{
if(!boardingShip)
return false;
if(!sourceFilter.Matches(*boardingShip))
return false;
}
else if(location == ENTERING)
{
if(!sourceFilter.Matches(player.GetSystem()))
return false;
}
else
{
if(source && source != player.GetPlanet())
return false;
if(!sourceFilter.Matches(player.GetPlanet()))
return false;
}
if(!toOffer.Test())
return false;
if(!toFail.IsEmpty() && toFail.Test())
return false;
if(repeat && player.Conditions().Get(trueName + ": offered") >= repeat)
return false;
bool isFailed = IsFailed();
auto it = actions.find(OFFER);
if(it != actions.end() && !it->second.CanBeDone(player, isFailed, boardingShip))
return false;
it = actions.find(ACCEPT);
if(it != actions.end() && !it->second.CanBeDone(player, isFailed, boardingShip))
return false;
it = actions.find(DECLINE);
if(it != actions.end() && !it->second.CanBeDone(player, isFailed, boardingShip))
return false;
it = actions.find(DEFER);
if(it != actions.end() && !it->second.CanBeDone(player, isFailed, boardingShip))
return false;
return true;
}
bool Mission::CanAccept(const PlayerInfo &player) const
{
if(!toAccept.Test())
return false;
bool isFailed = IsFailed();
auto it = actions.find(OFFER);
if(it != actions.end() && !it->second.CanBeDone(player, isFailed))
return false;
it = actions.find(ACCEPT);
if(it != actions.end() && !it->second.CanBeDone(player, isFailed))
return false;
return HasSpace(player);
}
bool Mission::HasSpace(const PlayerInfo &player) const
{
int extraCrew = 0;
if(player.Flagship())
extraCrew = player.Flagship()->Crew() - player.Flagship()->RequiredCrew();
return (cargoSize <= player.Cargo().Free() + player.Cargo().CommoditiesSize()
&& passengers <= player.Cargo().BunksFree() + extraCrew);
}
// Check if this mission's cargo can fit entirely on the referenced ship.
bool Mission::HasSpace(const Ship &ship) const
{
return (cargoSize <= ship.Cargo().Free() && passengers <= ship.Cargo().BunksFree());
}
bool Mission::CanComplete(const PlayerInfo &player) const
{
if(completionFilter.IsEmpty() && player.GetPlanet() != destination)
return false;
else if(!completionFilter.IsEmpty() && !completionFilter.Matches(player.GetPlanet(), player.GetSystem()))
return false;
return IsSatisfied(player);
}
// This function dictates whether missions on the player's map are shown in
// bright or dim text colors, and may be called while in-flight or landed.
bool Mission::IsSatisfied(const PlayerInfo &player) const
{
if(!waypoints.empty() || !stopovers.empty())
return false;
// Test the completion conditions for this mission.
if(!toComplete.Test())
return false;
// Determine if any fines or outfits that must be transferred, can.
auto it = actions.find(COMPLETE);
if(it != actions.end() && !it->second.CanBeDone(player, IsFailed()))
return false;
// NPCs which must be accompanied or evaded must be present (or not),
// and any needed scans, boarding, or assisting must also be completed.
for(const NPC &npc : npcs)
if(!npc.HasSucceeded(player.GetSystem()))
return false;
// If any of the cargo for this mission is being carried by a ship that is
// not in this system, the mission cannot be completed right now.
for(const auto &ship : player.Ships())
{
// Skip in-system ships, and carried ships whose parent is in-system.
if(ship->GetSystem() == player.GetSystem() || (!ship->GetSystem() && ship->CanBeCarried()
&& ship->GetParent() && ship->GetParent()->GetSystem() == player.GetSystem()))
continue;
if(ship->Cargo().GetPassengers(this))
return false;
// Check for all mission cargo, including that which has 0 mass.
auto &cargo = ship->Cargo().MissionCargo();
if(cargo.find(this) != cargo.end())
return false;
}
return true;
}
bool Mission::IsFailed() const
{
if(!toFail.IsEmpty() && toFail.Test())
return true;
for(const NPC &npc : npcs)
if(npc.HasFailed())
return true;
return hasFailed;
}
bool Mission::OverridesCapture() const
{
return overridesCapture;
}
// Mark a mission failed (e.g. due to a "fail" action in another mission).
void Mission::Fail()
{
hasFailed = true;
}
// Get a string to show if this mission is "blocked" from being offered
// because it requires you to have more passenger or cargo space free. After
// calling this function, any future calls to it will return an empty string
// so that you do not display the same message multiple times.
string Mission::BlockedMessage(const PlayerInfo &player)
{
if(blocked.empty())
return "";
int extraCrew = 0;
const Ship *flagship = player.Flagship();
// You cannot fire crew in space.
if(flagship && player.GetPlanet())
extraCrew = flagship->Crew() - flagship->RequiredCrew();
int cargoNeeded = cargoSize;
int bunksNeeded = passengers;
if(player.GetPlanet())
{
cargoNeeded -= (player.Cargo().Free() + player.Cargo().CommoditiesSize());
bunksNeeded -= (player.Cargo().BunksFree() + extraCrew);
}
else
{
// Boarding a ship, so only use the flagship's space.
cargoNeeded -= flagship->Cargo().Free();
bunksNeeded -= flagship->Cargo().BunksFree();
}
map<string, string> subs;
GameData::GetTextReplacements().Substitutions(subs);
substitutions.Substitutions(subs);
player.AddPlayerSubstitutions(subs);
subs["<conditions>"] = toAccept.Test() ? "meet" : "do not meet";
ostringstream out;
if(bunksNeeded > 0)
out << (bunksNeeded == 1 ? "another bunk" : to_string(bunksNeeded) + " more bunks");
if(bunksNeeded > 0 && cargoNeeded > 0)
out << " and ";
if(cargoNeeded > 0)
out << (cargoNeeded == 1 ? "another ton" : to_string(cargoNeeded) + " more tons") << " of cargo space";
if(bunksNeeded <= 0 && cargoNeeded <= 0)
out << "no additional space";
subs["<capacity>"] = out.str();
for(const auto &keyValue : subs)
subs[keyValue.first] = Phrase::ExpandPhrases(keyValue.second);
Format::Expand(subs);
string message = Format::Replace(blocked, subs);
blocked.clear();
return message;
}
// Check if this mission recommends that the game be autosaved when it is
// accepted. This should be set for main story line missions that have a
// high chance of failing, such as escort missions.
bool Mission::RecommendsAutosave() const
{
return autosave;
}
// Check if this mission is unique, i.e. not something that will be offered
// over and over again in different variants.
bool Mission::IsUnique() const
{
return (repeat == 1);
}
// When the state of this mission changes, it may make changes to the player
// information or show new UI panels. PlayerInfo::MissionCallback() will be
// used as the callback for any UI panel that returns a value.
bool Mission::Do(Trigger trigger, PlayerInfo &player, UI *ui, const shared_ptr<Ship> &boardingShip)
{
if(trigger == STOPOVER)
{
// If this is not one of this mission's stopover planets, or if it is
// not the very last one that must be visited, do nothing.
auto it = stopovers.find(player.GetPlanet());
if(it == stopovers.end())
return false;
for(const NPC &npc : npcs)
if(npc.IsLeftBehind(player.GetSystem()))
{
ui->Push(new Dialog("This is a stop for one of your missions, but you have left a ship behind."));
return false;
}
visitedStopovers.insert(*it);
stopovers.erase(it);
if(!stopovers.empty())
return false;
}
if(trigger == ABORT && IsFailed())
return false;
if(trigger == WAYPOINT && !waypoints.empty())
return false;
auto it = actions.find(trigger);
// If this mission was aborted but no ABORT action exists, look for a FAIL
// action instead. This is done for backwards compatibility purposes from
// when aborting a mission activated the FAIL trigger.
if(trigger == ABORT && it == actions.end())
it = actions.find(FAIL);
// Fail and abort conditions get updated regardless of whether the action
// can be done, as a fail or abort action not being able to be done does
// not prevent a mission from being failed or aborted.
if(trigger == FAIL)
{
--player.Conditions()[trueName + ": active"];
++player.Conditions()[trueName + ": failed"];
}
else if(trigger == ABORT)
{
--player.Conditions()[trueName + ": active"];
++player.Conditions()[trueName + ": aborted"];
// Set the failed mission condition here as well for
// backwards compatibility.
++player.Conditions()[trueName + ": failed"];
}
// Don't update any further conditions if this action exists and can't be completed.
if(it != actions.end() && !it->second.CanBeDone(player, IsFailed(), boardingShip))
return false;
if(trigger == ACCEPT)
{
++player.Conditions()[trueName + ": offered"];
++player.Conditions()[trueName + ": active"];
// Any potential on offer conversation has been finished, so update
// the active NPCs for the first time.
UpdateNPCs(player);
if(deadline)
{
DistanceMap here(player);
player.CalculateRemainingDeadline(*this, here);
}
}
else if(trigger == DECLINE)
{
++player.Conditions()[trueName + ": offered"];
++player.Conditions()[trueName + ": declined"];
}
else if(trigger == COMPLETE)
{
--player.Conditions()[trueName + ": active"];
++player.Conditions()[trueName + ": done"];
}
// "Jobs" should never show dialogs when offered, nor should they call the
// player's mission callback.
if(trigger == OFFER && location == JOB)
ui = nullptr;
// If this trigger has actions tied to it, perform them. Otherwise, check
// if this is a non-job mission that just got offered and if so,
// automatically accept it.
// Actions that are performed only receive the mission destination
// system if the mission is visible. This is because the purpose of
// a MissionAction being given the destination system is for drawing
// a special marker at the destination if the map is opened during any
// mission dialog or conversation. Invisible missions don't show this
// marker.
if(it != actions.end())
it->second.Do(player, ui, this, (destination && isVisible) ? destination->GetSystem() : nullptr,
boardingShip, IsUnique());
else if(trigger == OFFER && location != JOB)
player.MissionCallback(Conversation::ACCEPT);
return true;
}
bool Mission::RequiresGiftedShip(const string &shipId) const
{
// Check if any uncompleted actions required for the mission needs this ship.
set<Trigger> requiredActions;
{
requiredActions.insert(Trigger::COMPLETE);
if(!stopovers.empty())
requiredActions.insert(Trigger::STOPOVER);
if(!waypoints.empty())
requiredActions.insert(Trigger::WAYPOINT);
}
for(const auto &it : actions)
if(requiredActions.contains(it.first) && it.second.RequiresGiftedShip(shipId))
return true;
return false;
}
// Get a list of NPCs associated with this mission. Every time the player
// takes off from a planet, they should be added to the active ships.
const list<NPC> &Mission::NPCs() const
{
return npcs;
}
// Update which NPCs are active based on their spawn and despawn conditions.
void Mission::UpdateNPCs(const PlayerInfo &player)
{
for(auto &npc : npcs)
npc.UpdateSpawning(player);
}
// Checks if the given ship belongs to one of the mission's NPCs.
bool Mission::HasShip(const shared_ptr<Ship> &ship) const
{
for(const auto &npc : npcs)
for(const auto &npcShip : npc.Ships())
if(npcShip == ship)
return true;
return false;
}
// If any event occurs between two ships, check to see if this mission cares
// about it. This may affect the mission status or display a message.
void Mission::Do(const ShipEvent &event, PlayerInfo &player, UI *ui)
{
if(event.TargetGovernment()->IsPlayer() && !IsFailed())
{
bool failed = false;
string message;
if(event.Type() & ShipEvent::DESTROY)
{
// Destroyed ships carrying mission cargo result in failed missions.
// Mission cargo may have a quantity of zero (i.e. 0 mass).
for(const auto &it : event.Target()->Cargo().MissionCargo())
failed |= (it.first == this);
// If any mission passengers were present, this mission is failed.
for(const auto &it : event.Target()->Cargo().PassengerList())
failed |= (it.first == this && it.second);
}
else if(event.Type() & ShipEvent::BOARD)
{
// Fail missions whose cargo is stolen by a boarding vessel.
for(const auto &it : event.Actor()->Cargo().MissionCargo())
failed |= (it.first == this);
if(failed)
message = "Your " + event.Target()->DisplayModelName() +
" \"" + event.Target()->GivenName() + "\" has been plundered. ";
}
if(failed)
{
hasFailed = true;
if(isVisible)
Messages::Add(message + "Mission failed: \"" + displayName + "\".", Messages::Importance::Highest);
}
}
if((event.Type() & ShipEvent::DISABLE) && event.Target() == player.FlagshipPtr())
Do(DISABLED, player, ui);
// Jump events are only created for the player's flagship.
if((event.Type() & ShipEvent::JUMP) && event.Actor())
{
const System *system = event.Actor()->GetSystem();
// If this was a waypoint, clear it.
if(waypoints.erase(system))
{
visitedWaypoints.insert(system);
Do(WAYPOINT, player, ui);
}
// Perform an "on enter" action for this system, if possible, and if
// any was performed, update this mission's NPC spawn states.
if(Enter(system, player, ui))
UpdateNPCs(player);
}
for(NPC &npc : npcs)
npc.Do(event, player, ui, this, isVisible);
}
// Get a specific mission action from this mission.
// If a mission action is not found for the given trigger, returns an empty
// mission action.
const MissionAction &Mission::GetAction(Trigger trigger) const
{
auto ait = actions.find(trigger);
static const MissionAction EMPTY{};
return ait != actions.end() ? ait->second : EMPTY;
}
// "Instantiate" a mission by replacing randomly selected values and places
// with a single choice, and then replacing any wildcard text as well.
Mission Mission::Instantiate(const PlayerInfo &player, const shared_ptr<Ship> &boardingShip) const
{
Mission result;
// If anything goes wrong below, this mission should not be offered.
result.hasFailed = true;
result.isVisible = isVisible;
result.unavailable = unavailable;
result.unselected = unselected;
result.selected = selected;
result.hasPriority = hasPriority;
result.isNonBlocking = isNonBlocking;
result.isMinor = isMinor;
result.offerPrecedence = offerPrecedence;
result.autosave = autosave;
result.location = location;
result.overridesCapture = overridesCapture;
result.sourceShip = boardingShip.get();
result.repeat = repeat;
result.trueName = trueName;
result.waypoints = waypoints;
result.completionFilter = completionFilter;
result.markedSystems = markedSystems;
// Handle waypoint systems that are chosen randomly.
const System *const sourceSystem = player.GetSystem();
for(const LocationFilter &filter : waypointFilters)
{
const System *system = filter.PickSystem(sourceSystem);
if(!system)
return result;
result.waypoints.insert(system);
}
// If one of the waypoints is the current system, it is already visited.
if(result.waypoints.erase(sourceSystem))
result.visitedWaypoints.insert(sourceSystem);
// Copy the template's stopovers, and add planets that match the template's filters.
result.stopovers = stopovers;
// Make sure they all exist in a valid system.
for(auto it = result.stopovers.begin(); it != result.stopovers.end(); )
{
if((*it)->IsValid())
++it;
else
it = result.stopovers.erase(it);
}
for(const LocationFilter &filter : stopoverFilters)
{
// Unlike destinations, we can allow stopovers on planets that don't have a spaceport.
const Planet *planet = filter.PickPlanet(sourceSystem, ignoreClearance || !clearance.empty(), false);
if(!planet)
return result;
result.stopovers.insert(planet);
}
// First, pick values for all the variables.
// If a specific destination is not specified in the mission, pick a random
// one out of all the destinations that satisfy the mission requirements.
result.destination = destination;
if(!result.destination && !destinationFilter.IsEmpty())
{
result.destination = destinationFilter.PickPlanet(sourceSystem, ignoreClearance || !clearance.empty());
if(!result.destination)
return result;
}
// If no destination is specified, it is the same as the source planet. Also
// use the source planet if the given destination is not a valid planet.
if(!result.destination || !result.destination->IsValid())
{
if(player.GetPlanet())
result.destination = player.GetPlanet();
else
return result;
}
// If cargo is being carried, see if we are supposed to replace a generic
// cargo name with something more specific.
if(!cargo.empty())
{
const string expandedCargo = Phrase::ExpandPhrases(cargo);
const Trade::Commodity *commodity = nullptr;
if(expandedCargo == "random")
commodity = PickCommodity(*sourceSystem, *result.destination->GetSystem());
else
{
for(const Trade::Commodity &option : GameData::Commodities())
if(option.name == expandedCargo)
{
commodity = &option;
break;
}
for(const Trade::Commodity &option : GameData::SpecialCommodities())
if(option.name == expandedCargo)
{
commodity = &option;
break;
}
}
if(commodity)
result.cargo = commodity->items[Random::Int(commodity->items.size())];
else
result.cargo = expandedCargo;
}
// Pick a random cargo amount, if requested.
if(cargoSize || cargoLimit)
{
if(cargoProb)
result.cargoSize = Random::Polya(cargoLimit, cargoProb) + cargoSize;
else if(cargoLimit > cargoSize)
result.cargoSize = cargoSize + Random::Int(cargoLimit - cargoSize + 1);
else
result.cargoSize = cargoSize;
}
// Pick a random passenger count, if requested.
if(passengers || passengerLimit)
{
if(passengerProb)
result.passengers = Random::Polya(passengerLimit, passengerProb) + passengers;
else if(passengerLimit > passengers)
result.passengers = passengers + Random::Int(passengerLimit - passengers + 1);
else
result.passengers = passengers;
}
result.paymentApparent = paymentApparent;
result.fine = fine;
result.fineMessage = Phrase::ExpandPhrases(fineMessage);
result.failIfDiscovered = failIfDiscovered;
result.distanceCalcSettings = distanceCalcSettings;
int jumps = result.CalculateJumps(sourceSystem);
int64_t payload = static_cast<int64_t>(result.cargoSize) + 10 * static_cast<int64_t>(result.passengers);
// Set the deadline, if requested.
if(deadlineBase || deadlineMultiplier)
result.deadline = player.GetDate() + deadlineBase + deadlineMultiplier * jumps;
// Copy the conditions. The offer conditions must be copied too, because they
// may depend on a condition that other mission offers might change.
result.toOffer = toOffer;
result.toAccept = toAccept;
result.toComplete = toComplete;
result.toFail = toFail;
// Generate the substitutions map.
map<string, string> subs;
GameData::GetTextReplacements().Substitutions(subs);
substitutions.Substitutions(subs);
subs["<commodity>"] = result.cargo;
subs["<tons>"] = Format::MassString(result.cargoSize);
subs["<cargo>"] = Format::CargoString(result.cargoSize, subs["<commodity>"]);
subs["<bunks>"] = to_string(result.passengers);
subs["<passengers>"] = (result.passengers == 1) ? "passenger" : "passengers";
subs["<fare>"] = (result.passengers == 1) ? "a passenger" : (subs["<bunks>"] + " passengers");
if(player.GetPlanet())
subs["<origin>"] = player.GetPlanet()->DisplayName();
else if(boardingShip)
subs["<origin>"] = boardingShip->GivenName();
subs["<planet>"] = result.destination ? result.destination->DisplayName() : "";
subs["<system>"] = result.destination ? result.destination->GetSystem()->DisplayName() : "";
subs["<destination>"] = subs["<planet>"] + " in the " + subs["<system>"] + " system";
subs["<date>"] = result.deadline.ToString();
subs["<day>"] = result.deadline.LongString();
if(result.paymentApparent)
subs["<payment>"] = Format::CreditString(abs(result.paymentApparent));
// Stopovers: "<name> in the <system name> system" with "," and "and".
auto getDisplayName = [](const auto *const &item)
{
return item->DisplayName();
};
if(!result.stopovers.empty())
{
subs["<stopovers>"] = Format::List<set, const Planet *>(result.stopovers,
[](const Planet *const &planet)
{
return planet->DisplayName() + " in the " + planet->GetSystem()->DisplayName() + " system";
});
subs["<planet stopovers>"] = Format::List<set, const Planet *>(result.stopovers, getDisplayName);
}
// Waypoints and marks: "<system name>" with "," and "and".
if(!result.waypoints.empty())
subs["<waypoints>"] = Format::List<set, const System *>(result.waypoints, getDisplayName);
if(!result.markedSystems.empty())
subs["<marks>"] = Format::List<set, const System *>(result.markedSystems, getDisplayName);
// Done making subs, so expand the phrases and recursively substitute.
for(const auto &keyValue : subs)
subs[keyValue.first] = Phrase::ExpandPhrases(keyValue.second);
Format::Expand(subs);
// Instantiate the NPCs. This also fills in the "<npc>" substitution.
string reason;
for(auto &&n : npcs)
reason = n.Validate(true);
if(!reason.empty())
{
Logger::LogError("Instantiation Error: NPC template in mission \""
+ TrueName() + "\" uses invalid " + std::move(reason));
return result;
}
for(const NPC &npc : npcs)
result.npcs.push_back(npc.Instantiate(player, subs, sourceSystem, result.destination->GetSystem(), jumps, payload));
// Instantiate the actions. The "complete" action is always first so that
// the "<payment>" substitution can be filled in.
auto ait = actions.begin();
for( ; ait != actions.end(); ++ait)
{
reason = ait->second.Validate();
if(!reason.empty())
break;
}
if(ait != actions.end())
{
Logger::LogError("Instantiation Error: Action \"" + TriggerToText(ait->first) + "\" in mission \""
+ TrueName() + "\" uses invalid " + std::move(reason));
return result;
}
for(const auto &it : actions)
result.actions[it.first] = it.second.Instantiate(subs, sourceSystem, jumps, payload);
auto oit = onEnter.begin();
for( ; oit != onEnter.end(); ++oit)
{
reason = oit->first->IsValid() ? oit->second.Validate() : "trigger system";
if(!reason.empty())
break;
}
if(oit != onEnter.end())
{
Logger::LogError("Instantiation Error: Action \"on enter '" + oit->first->TrueName() + "'\" in mission \""
+ TrueName() + "\" uses invalid " + std::move(reason));
return result;
}
for(const auto &it : onEnter)
result.onEnter[it.first] = it.second.Instantiate(subs, sourceSystem, jumps, payload);
auto eit = genericOnEnter.begin();
for( ; eit != genericOnEnter.end(); ++eit)
{
reason = eit->Validate();
if(!reason.empty())
break;
}
if(eit != genericOnEnter.end())
{
Logger::LogError("Instantiation Error: Generic \"on enter\" action in mission \""
+ TrueName() + "\" uses invalid " + std::move(reason));
return result;
}
for(const MissionAction &action : genericOnEnter)
result.genericOnEnter.emplace_back(action.Instantiate(subs, sourceSystem, jumps, payload));
// Perform substitution in the name and description.
result.displayName = Format::Replace(Phrase::ExpandPhrases(displayName), subs);
result.description = Format::Replace(Phrase::ExpandPhrases(description), subs);
result.clearance = Format::Replace(Phrase::ExpandPhrases(clearance), subs);
result.blocked = Format::Replace(Phrase::ExpandPhrases(blocked), subs);
result.clearanceFilter = clearanceFilter;
result.hasFullClearance = hasFullClearance;
result.hasFailed = false;
return result;
}
int Mission::CalculateJumps(const System *sourceSystem)
{
expectedJumps = 0;
// Estimate how far the player will have to travel to visit all the waypoints
// and stopovers and then to land on the destination planet. Rather than a
// full traveling salesman path, just calculate a greedy approximation.
list<const System *> destinations;
for(const System *system : waypoints)
destinations.push_back(system);
for(const Planet *planet : stopovers)
destinations.push_back(planet->GetSystem());
while(!destinations.empty())
{
// Find the closest destination to this location.
DistanceMap distance(sourceSystem,
distanceCalcSettings.WormholeStrat(),
distanceCalcSettings.AssumesJumpDrive());
auto it = destinations.begin();
auto bestIt = it;
int bestDays = distance.Days(**bestIt);
if(bestDays < 0)
bestDays = numeric_limits<int>::max();
for(++it; it != destinations.end(); ++it)
{
int days = distance.Days(**it);
if(days >= 0 && days < bestDays)
{
bestIt = it;
bestDays = days;
}
}
sourceSystem = *bestIt;
// If currently unreachable, this system adds -1 to the deadline, to match previous behavior.
expectedJumps += bestDays == numeric_limits<int>::max() ? -1 : bestDays;
destinations.erase(bestIt);
}
DistanceMap distance(sourceSystem,
distanceCalcSettings.WormholeStrat(),
distanceCalcSettings.AssumesJumpDrive());
// If currently unreachable, this system adds -1 to the deadline, to match previous behavior.
expectedJumps += distance.Days(*destination->GetSystem());
return expectedJumps;
}
// Perform an "on enter" MissionAction associated with the current system.
// Returns true if an action was performed.
bool Mission::Enter(const System *system, PlayerInfo &player, UI *ui)
{
const auto eit = onEnter.find(system);
const auto originalSize = didEnter.size();
if(eit != onEnter.end() && !didEnter.contains(&eit->second) && eit->second.CanBeDone(player, IsFailed()))
{
eit->second.Do(player, ui, this);
didEnter.insert(&eit->second);
}
// If no specific `on enter` was performed, try matching to a generic "on enter,"
// which may use a LocationFilter to govern which systems it can be performed in.
else
for(MissionAction &action : genericOnEnter)
if(!didEnter.contains(&action) && action.CanBeDone(player, IsFailed()))
{
action.Do(player, ui, this);
didEnter.insert(&action);
break;
}
return didEnter.size() > originalSize;
}
// For legacy code, contraband definitions can be placed in two different
// locations, so move that parsing out to a helper function.
bool Mission::ParseContraband(const DataNode &node)
{
const string &key = node.Token(0);
if(key == "illegal" && node.Size() == 2)
fine = node.Value(1);
else if(key == "illegal" && node.Size() == 3)
{
fine = node.Value(1);
fineMessage = node.Token(2);
}
else if(key == "stealth")
failIfDiscovered = true;
else
return false;
return true;
}
|