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
|
/*
* CCampaignHandler.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "CampaignState.h"
#include "../Point.h"
#include "../filesystem/ResourcePath.h"
#include "../VCMI_Lib.h"
#include "../texts/CGeneralTextHandler.h"
#include "../mapping/CMapService.h"
#include "../mapping/CMapInfo.h"
#include "../mapping/CMap.h"
#include "../mapObjects/CGHeroInstance.h"
#include "../serializer/JsonDeserializer.h"
#include "../serializer/JsonSerializer.h"
#include "../json/JsonUtils.h"
VCMI_LIB_NAMESPACE_BEGIN
void CampaignScenario::loadPreconditionRegions(ui32 regions)
{
for (int i=0; i<32; i++) //for each bit in region. h3c however can only hold up to 16
{
if ( (1 << i) & regions)
preconditionRegions.insert(static_cast<CampaignScenarioID>(i));
}
}
CampaignRegions::RegionDescription CampaignRegions::RegionDescription::fromJson(const JsonNode & node)
{
CampaignRegions::RegionDescription rd;
rd.infix = node["infix"].String();
rd.pos = Point(static_cast<int>(node["x"].Float()), static_cast<int>(node["y"].Float()));
if(!node["labelPos"].isNull())
rd.labelPos = Point(static_cast<int>(node["labelPos"]["x"].Float()), static_cast<int>(node["labelPos"]["y"].Float()));
else
rd.labelPos = std::nullopt;
return rd;
}
JsonNode CampaignRegions::RegionDescription::toJson(CampaignRegions::RegionDescription & rd)
{
JsonNode node;
node["infix"].String() = rd.infix;
node["x"].Float() = rd.pos.x;
node["y"].Float() = rd.pos.y;
if(rd.labelPos != std::nullopt)
{
node["labelPos"]["x"].Float() = (*rd.labelPos).x;
node["labelPos"]["y"].Float() = (*rd.labelPos).y;
}
else
node["labelPos"].clear();
return node;
}
CampaignRegions CampaignRegions::fromJson(const JsonNode & node)
{
CampaignRegions cr;
cr.campPrefix = node["prefix"].String();
cr.colorSuffixLength = static_cast<int>(node["colorSuffixLength"].Float());
cr.campSuffix = node["suffix"].isNull() ? std::vector<std::string>() : std::vector<std::string>{node["suffix"].Vector()[0].String(), node["suffix"].Vector()[1].String(), node["suffix"].Vector()[2].String()};
cr.campBackground = node["background"].isNull() ? "" : node["background"].String();
for(const JsonNode & desc : node["desc"].Vector())
cr.regions.push_back(CampaignRegions::RegionDescription::fromJson(desc));
return cr;
}
JsonNode CampaignRegions::toJson(CampaignRegions cr)
{
JsonNode node;
node["prefix"].String() = cr.campPrefix;
node["colorSuffixLength"].Float() = cr.colorSuffixLength;
if(!cr.campSuffix.size())
node["suffix"].clear();
else
node["suffix"].Vector() = JsonVector{ JsonNode(cr.campSuffix[0]), JsonNode(cr.campSuffix[1]), JsonNode(cr.campSuffix[2]) };
if(cr.campBackground.empty())
node["background"].clear();
else
node["background"].String() = cr.campBackground;
node["desc"].Vector() = JsonVector();
for(auto & region : cr.regions)
node["desc"].Vector().push_back(CampaignRegions::RegionDescription::toJson(region));
return node;
}
CampaignRegions CampaignRegions::getLegacy(int campId)
{
static std::vector<CampaignRegions> campDescriptions;
if(campDescriptions.empty()) //read once
{
const JsonNode config(JsonPath::builtin("config/campaign_regions.json"));
for(const JsonNode & campaign : config["campaign_regions"].Vector())
campDescriptions.push_back(CampaignRegions::fromJson(campaign));
}
return campDescriptions.at(campId);
}
ImagePath CampaignRegions::getBackgroundName() const
{
if(campBackground.empty())
return ImagePath::builtin(campPrefix + "_BG.BMP");
else
return ImagePath::builtin(campBackground);
}
Point CampaignRegions::getPosition(CampaignScenarioID which) const
{
auto const & region = regions[which.getNum()];
return region.pos;
}
std::optional<Point> CampaignRegions::getLabelPosition(CampaignScenarioID which) const
{
auto const & region = regions[which.getNum()];
return region.labelPos;
}
ImagePath CampaignRegions::getNameFor(CampaignScenarioID which, int colorIndex, std::string type) const
{
auto const & region = regions[which.getNum()];
static const std::array<std::array<std::string, 8>, 3> colors = {{
{ "", "", "", "", "", "", "", "" },
{ "R", "B", "N", "G", "O", "V", "T", "P" },
{ "Re", "Bl", "Br", "Gr", "Or", "Vi", "Te", "Pi" }
}};
std::string color = colors[colorSuffixLength][colorIndex];
return ImagePath::builtin(campPrefix + region.infix + "_" + type + color + ".BMP");
}
ImagePath CampaignRegions::getAvailableName(CampaignScenarioID which, int color) const
{
if(campSuffix.empty())
return getNameFor(which, color, "En");
else
return getNameFor(which, color, campSuffix[0]);
}
ImagePath CampaignRegions::getSelectedName(CampaignScenarioID which, int color) const
{
if(campSuffix.empty())
return getNameFor(which, color, "Se");
else
return getNameFor(which, color, campSuffix[1]);
}
ImagePath CampaignRegions::getConqueredName(CampaignScenarioID which, int color) const
{
if(campSuffix.empty())
return getNameFor(which, color, "Co");
else
return getNameFor(which, color, campSuffix[2]);
}
bool CampaignBonus::isBonusForHero() const
{
return type == CampaignBonusType::SPELL ||
type == CampaignBonusType::MONSTER ||
type == CampaignBonusType::ARTIFACT ||
type == CampaignBonusType::SPELL_SCROLL ||
type == CampaignBonusType::PRIMARY_SKILL ||
type == CampaignBonusType::SECONDARY_SKILL;
}
void CampaignHeader::loadLegacyData(ui8 campId)
{
campaignRegions = CampaignRegions::getLegacy(campId);
numberOfScenarios = VLC->generaltexth->getCampaignLength(campId);
}
void CampaignHeader::loadLegacyData(CampaignRegions regions, int numOfScenario)
{
campaignRegions = regions;
numberOfScenarios = numOfScenario;
}
bool CampaignHeader::playerSelectedDifficulty() const
{
return difficultyChosenByPlayer;
}
bool CampaignHeader::formatVCMI() const
{
return version == CampaignVersion::VCMI;
}
std::string CampaignHeader::getDescriptionTranslated() const
{
return description.toString();
}
std::string CampaignHeader::getNameTranslated() const
{
return name.toString();
}
std::string CampaignHeader::getAuthor() const
{
return authorContact.toString();
}
std::string CampaignHeader::getAuthorContact() const
{
return authorContact.toString();
}
std::string CampaignHeader::getCampaignVersion() const
{
return campaignVersion.toString();
}
time_t CampaignHeader::getCreationDateTime() const
{
return creationDateTime;
}
std::string CampaignHeader::getFilename() const
{
return filename;
}
std::string CampaignHeader::getModName() const
{
return modName;
}
std::string CampaignHeader::getEncoding() const
{
return encoding;
}
AudioPath CampaignHeader::getMusic() const
{
return music;
}
ImagePath CampaignHeader::getLoadingBackground() const
{
return loadingBackground;
}
ImagePath CampaignHeader::getVideoRim() const
{
return videoRim;
}
VideoPath CampaignHeader::getIntroVideo() const
{
return introVideo;
}
VideoPath CampaignHeader::getOutroVideo() const
{
return outroVideo;
}
const CampaignRegions & CampaignHeader::getRegions() const
{
return campaignRegions;
}
TextContainerRegistrable & CampaignHeader::getTexts()
{
return textContainer;
}
bool CampaignState::isConquered(CampaignScenarioID whichScenario) const
{
return vstd::contains(mapsConquered, whichScenario);
}
bool CampaignState::isAvailable(CampaignScenarioID whichScenario) const
{
//check for void scenraio
if (!scenario(whichScenario).isNotVoid())
{
return false;
}
if (vstd::contains(mapsConquered, whichScenario))
{
return false;
}
//check preconditioned regions
for (auto const & it : scenario(whichScenario).preconditionRegions)
{
if (!vstd::contains(mapsConquered, it))
return false;
}
return true;
}
bool CampaignScenario::isNotVoid() const
{
return !mapName.empty();
}
std::set<HeroTypeID> CampaignState::getReservedHeroes() const
{
std::set<HeroTypeID> result;
for (auto const & scenarioID : allScenarios())
{
if (isConquered(scenarioID))
continue;
auto header = getMapHeader(scenarioID);
result.insert(header->reservedCampaignHeroes.begin(), header->reservedCampaignHeroes.end());
}
return result;
}
const CGHeroInstance * CampaignState::strongestHero(CampaignScenarioID scenarioId, const PlayerColor & owner) const
{
std::function<bool(const JsonNode & node)> isOwned = [&](const JsonNode & node)
{
auto * h = CampaignState::crossoverDeserialize(node, nullptr);
bool result = h->tempOwner == owner;
vstd::clear_pointer(h);
return result;
};
auto ownedHeroes = scenarioHeroPool.at(scenarioId) | boost::adaptors::filtered(isOwned);
if (ownedHeroes.empty())
return nullptr;
return CampaignState::crossoverDeserialize(ownedHeroes.front(), nullptr);
}
/// Returns heroes that can be instantiated as hero placeholders by power
const std::vector<JsonNode> & CampaignState::getHeroesByPower(CampaignScenarioID scenarioId) const
{
static const std::vector<JsonNode> emptyVector;
if (scenarioHeroPool.count(scenarioId))
return scenarioHeroPool.at(scenarioId);
return emptyVector;
}
/// Returns hero for instantiation as placeholder by type
/// May return empty JsonNode if such hero was not found
const JsonNode & CampaignState::getHeroByType(HeroTypeID heroID) const
{
static const JsonNode emptyNode;
if (!getReservedHeroes().count(heroID))
return emptyNode;
if (!globalHeroPool.count(heroID))
return emptyNode;
return globalHeroPool.at(heroID);
}
void CampaignState::setCurrentMapAsConquered(std::vector<CGHeroInstance *> heroes)
{
range::sort(heroes, [](const CGHeroInstance * a, const CGHeroInstance * b)
{
return a->getValueForCampaign() > b->getValueForCampaign();
});
logGlobal->info("Scenario %d of campaign %s (%s) has been completed", currentMap->getNum(), getFilename(), getNameTranslated());
mapsConquered.push_back(*currentMap);
auto reservedHeroes = getReservedHeroes();
for (auto * hero : heroes)
{
JsonNode node = CampaignState::crossoverSerialize(hero);
if (reservedHeroes.count(hero->getHeroTypeID()))
{
logGlobal->info("Hero crossover: %d (%s) exported to global pool", hero->getHeroTypeID(), hero->getNameTranslated());
globalHeroPool[hero->getHeroTypeID()] = node;
}
else
{
logGlobal->info("Hero crossover: %d (%s) exported to scenario pool", hero->getHeroTypeID(), hero->getNameTranslated());
scenarioHeroPool[*currentMap].push_back(node);
}
}
}
std::optional<CampaignBonus> CampaignState::getBonus(CampaignScenarioID which) const
{
auto bonuses = scenario(which).travelOptions.bonusesToChoose;
assert(chosenCampaignBonuses.count(*currentMap) || bonuses.empty());
if(bonuses.empty())
return std::optional<CampaignBonus>();
if (!getBonusID(which))
return std::optional<CampaignBonus>();
return bonuses[getBonusID(which).value()];
}
std::optional<ui8> CampaignState::getBonusID(CampaignScenarioID which) const
{
if (!chosenCampaignBonuses.count(which))
return std::nullopt;
return chosenCampaignBonuses.at(which);
}
std::unique_ptr<CMap> CampaignState::getMap(CampaignScenarioID scenarioId, IGameCallback * cb)
{
// FIXME: there is certainly better way to handle maps inside campaigns
if(scenarioId == CampaignScenarioID::NONE)
scenarioId = currentMap.value();
CMapService mapService;
std::string scenarioName = getFilename().substr(0, getFilename().find('.'));
boost::to_lower(scenarioName);
scenarioName += ':' + std::to_string(scenarioId.getNum());
const auto & mapContent = mapPieces.find(scenarioId)->second;
auto result = mapService.loadMap(mapContent.data(), mapContent.size(), scenarioName, getModName(), getEncoding(), cb);
mapTranslations[scenarioId] = result->texts;
return result;
}
std::unique_ptr<CMapHeader> CampaignState::getMapHeader(CampaignScenarioID scenarioId) const
{
if(scenarioId == CampaignScenarioID::NONE)
scenarioId = currentMap.value();
CMapService mapService;
std::string scenarioName = getFilename().substr(0, getFilename().find('.'));
boost::to_lower(scenarioName);
scenarioName += ':' + std::to_string(scenarioId.getNum());
const auto & mapContent = mapPieces.find(scenarioId)->second;
return mapService.loadMapHeader(mapContent.data(), mapContent.size(), scenarioName, getModName(), getEncoding());
}
std::shared_ptr<CMapInfo> CampaignState::getMapInfo(CampaignScenarioID scenarioId) const
{
if(scenarioId == CampaignScenarioID::NONE)
scenarioId = currentMap.value();
auto mapInfo = std::make_shared<CMapInfo>();
mapInfo->fileURI = getFilename();
mapInfo->mapHeader = getMapHeader(scenarioId);
mapInfo->countPlayers();
return mapInfo;
}
JsonNode CampaignState::crossoverSerialize(CGHeroInstance * hero) const
{
JsonNode node;
JsonSerializer handler(nullptr, node);
hero->serializeJsonOptions(handler);
return node;
}
CGHeroInstance * CampaignState::crossoverDeserialize(const JsonNode & node, CMap * map) const
{
JsonDeserializer handler(nullptr, const_cast<JsonNode&>(node));
auto * hero = new CGHeroInstance(map ? map->cb : nullptr);
hero->ID = Obj::HERO;
hero->serializeJsonOptions(handler);
if (map)
{
hero->serializeJsonArtifacts(handler, "artifacts");
map->addNewArtifactInstance(*hero);
}
return hero;
}
void CampaignState::setCurrentMap(CampaignScenarioID which)
{
assert(scenario(which).isNotVoid());
currentMap = which;
}
void CampaignState::setCurrentMapBonus(ui8 which)
{
chosenCampaignBonuses[*currentMap] = which;
}
std::optional<CampaignScenarioID> CampaignState::currentScenario() const
{
return currentMap;
}
std::optional<CampaignScenarioID> CampaignState::lastScenario() const
{
if (mapsConquered.empty())
return std::nullopt;
return mapsConquered.back();
}
std::set<CampaignScenarioID> CampaignState::conqueredScenarios() const
{
std::set<CampaignScenarioID> result;
result.insert(mapsConquered.begin(), mapsConquered.end());
return result;
}
std::set<CampaignScenarioID> Campaign::allScenarios() const
{
std::set<CampaignScenarioID> result;
for (auto const & entry : scenarios)
{
if (entry.second.isNotVoid())
result.insert(entry.first);
}
return result;
}
void Campaign::overrideCampaign()
{
const JsonNode node = JsonUtils::assembleFromFiles("config/campaignOverrides.json");
for (auto & entry : node.Struct())
if(filename == entry.first)
{
if(!entry.second["regions"].isNull() && !entry.second["scenarioCount"].isNull())
loadLegacyData(CampaignRegions::fromJson(entry.second["regions"]), entry.second["scenarioCount"].Integer());
if(!entry.second["loadingBackground"].isNull())
loadingBackground = ImagePath::builtin(entry.second["loadingBackground"].String());
if(!entry.second["videoRim"].isNull())
videoRim = ImagePath::builtin(entry.second["videoRim"].String());
if(!entry.second["introVideo"].isNull())
introVideo = VideoPath::builtin(entry.second["introVideo"].String());
if(!entry.second["outroVideo"].isNull())
outroVideo = VideoPath::builtin(entry.second["outroVideo"].String());
}
}
void Campaign::overrideCampaignScenarios()
{
const JsonNode node = JsonUtils::assembleFromFiles("config/campaignOverrides.json");
for (auto & entry : node.Struct())
if(filename == entry.first)
{
if(!entry.second["scenarios"].isNull())
{
auto sc = entry.second["scenarios"].Vector();
for(int i = 0; i < sc.size(); i++)
{
auto it = scenarios.begin();
std::advance(it, i);
if(!sc.at(i)["voiceProlog"].isNull())
it->second.prolog.prologVoice = AudioPath::builtin(sc.at(i)["voiceProlog"].String());
if(!sc.at(i)["voiceEpilog"].isNull())
it->second.epilog.prologVoice = AudioPath::builtin(sc.at(i)["voiceEpilog"].String());
}
}
}
}
int Campaign::scenariosCount() const
{
return allScenarios().size();
}
const CampaignScenario & Campaign::scenario(CampaignScenarioID which) const
{
assert(scenarios.count(which));
assert(scenarios.at(which).isNotVoid());
return scenarios.at(which);
}
bool CampaignState::isCampaignFinished() const
{
return conqueredScenarios() == allScenarios();
}
VCMI_LIB_NAMESPACE_END
|