2023-06-25 21:28:24 +02:00
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
|
2024-01-09 16:43:36 +02:00
|
|
|
#include "../Point.h"
|
2023-08-23 14:07:50 +02:00
|
|
|
#include "../filesystem/ResourcePath.h"
|
2023-06-25 21:28:24 +02:00
|
|
|
#include "../VCMI_Lib.h"
|
2024-07-20 14:55:17 +02:00
|
|
|
#include "../texts/CGeneralTextHandler.h"
|
2023-06-25 21:28:24 +02:00
|
|
|
#include "../mapping/CMapService.h"
|
|
|
|
#include "../mapping/CMapInfo.h"
|
|
|
|
#include "../mapping/CMap.h"
|
|
|
|
#include "../mapObjects/CGHeroInstance.h"
|
|
|
|
#include "../serializer/JsonDeserializer.h"
|
|
|
|
#include "../serializer/JsonSerializer.h"
|
2024-08-31 13:15:07 +02:00
|
|
|
#include "../json/JsonUtils.h"
|
2023-06-25 21:28:24 +02:00
|
|
|
|
|
|
|
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();
|
2024-09-27 18:14:48 +02:00
|
|
|
rd.pos = Point(static_cast<int>(node["x"].Float()), static_cast<int>(node["y"].Float()));
|
2024-09-24 11:23:10 +02:00
|
|
|
if(!node["labelPos"].isNull())
|
2024-09-27 18:14:48 +02:00
|
|
|
rd.labelPos = Point(static_cast<int>(node["labelPos"]["x"].Float()), static_cast<int>(node["labelPos"]["y"].Float()));
|
2024-09-24 11:23:10 +02:00
|
|
|
else
|
2024-09-27 18:14:48 +02:00
|
|
|
rd.labelPos = std::nullopt;
|
2023-06-25 21:28:24 +02:00
|
|
|
return rd;
|
|
|
|
}
|
|
|
|
|
|
|
|
CampaignRegions CampaignRegions::fromJson(const JsonNode & node)
|
|
|
|
{
|
|
|
|
CampaignRegions cr;
|
|
|
|
cr.campPrefix = node["prefix"].String();
|
2024-08-12 17:57:34 +02:00
|
|
|
cr.colorSuffixLength = static_cast<int>(node["colorSuffixLength"].Float());
|
2024-08-10 15:27:22 +02:00
|
|
|
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();
|
2023-06-25 21:28:24 +02:00
|
|
|
|
|
|
|
for(const JsonNode & desc : node["desc"].Vector())
|
|
|
|
cr.regions.push_back(CampaignRegions::RegionDescription::fromJson(desc));
|
|
|
|
|
|
|
|
return cr;
|
|
|
|
}
|
|
|
|
|
|
|
|
CampaignRegions CampaignRegions::getLegacy(int campId)
|
|
|
|
{
|
|
|
|
static std::vector<CampaignRegions> campDescriptions;
|
|
|
|
if(campDescriptions.empty()) //read once
|
|
|
|
{
|
2023-09-01 23:26:14 +02:00
|
|
|
const JsonNode config(JsonPath::builtin("config/campaign_regions.json"));
|
2023-06-25 21:28:24 +02:00
|
|
|
for(const JsonNode & campaign : config["campaign_regions"].Vector())
|
|
|
|
campDescriptions.push_back(CampaignRegions::fromJson(campaign));
|
|
|
|
}
|
|
|
|
|
|
|
|
return campDescriptions.at(campId);
|
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
ImagePath CampaignRegions::getBackgroundName() const
|
2023-06-26 00:42:53 +02:00
|
|
|
{
|
2024-08-10 15:27:22 +02:00
|
|
|
if(campBackground.empty())
|
|
|
|
return ImagePath::builtin(campPrefix + "_BG.BMP");
|
|
|
|
else
|
|
|
|
return ImagePath::builtin(campBackground);
|
2023-06-26 00:42:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Point CampaignRegions::getPosition(CampaignScenarioID which) const
|
|
|
|
{
|
2023-11-02 16:56:02 +02:00
|
|
|
auto const & region = regions[which.getNum()];
|
2024-09-27 18:14:48 +02:00
|
|
|
return region.pos;
|
2023-06-26 00:42:53 +02:00
|
|
|
}
|
|
|
|
|
2024-09-24 11:23:10 +02:00
|
|
|
std::optional<Point> CampaignRegions::getLabelPosition(CampaignScenarioID which) const
|
|
|
|
{
|
|
|
|
auto const & region = regions[which.getNum()];
|
2024-09-27 18:14:48 +02:00
|
|
|
return region.labelPos;
|
2023-06-26 00:42:53 +02:00
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
ImagePath CampaignRegions::getNameFor(CampaignScenarioID which, int colorIndex, std::string type) const
|
2023-06-26 00:42:53 +02:00
|
|
|
{
|
2023-11-02 16:56:02 +02:00
|
|
|
auto const & region = regions[which.getNum()];
|
2023-06-26 00:42:53 +02:00
|
|
|
|
2024-08-12 01:16:06 +02:00
|
|
|
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" }
|
|
|
|
}};
|
2023-06-26 00:42:53 +02:00
|
|
|
|
2024-08-10 15:27:22 +02:00
|
|
|
std::string color = colors[colorSuffixLength][colorIndex];
|
2023-06-26 00:42:53 +02:00
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
return ImagePath::builtin(campPrefix + region.infix + "_" + type + color + ".BMP");
|
2023-06-26 00:42:53 +02:00
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
ImagePath CampaignRegions::getAvailableName(CampaignScenarioID which, int color) const
|
2023-06-26 00:42:53 +02:00
|
|
|
{
|
2024-08-12 01:16:06 +02:00
|
|
|
if(campSuffix.empty())
|
2024-08-10 15:27:22 +02:00
|
|
|
return getNameFor(which, color, "En");
|
|
|
|
else
|
|
|
|
return getNameFor(which, color, campSuffix[0]);
|
2023-06-26 00:42:53 +02:00
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
ImagePath CampaignRegions::getSelectedName(CampaignScenarioID which, int color) const
|
2023-06-26 00:42:53 +02:00
|
|
|
{
|
2024-08-12 01:16:06 +02:00
|
|
|
if(campSuffix.empty())
|
2024-08-10 15:27:22 +02:00
|
|
|
return getNameFor(which, color, "Se");
|
|
|
|
else
|
|
|
|
return getNameFor(which, color, campSuffix[1]);
|
2023-06-26 00:42:53 +02:00
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
ImagePath CampaignRegions::getConqueredName(CampaignScenarioID which, int color) const
|
2023-06-26 00:42:53 +02:00
|
|
|
{
|
2024-08-12 01:16:06 +02:00
|
|
|
if(campSuffix.empty())
|
2024-08-10 15:27:22 +02:00
|
|
|
return getNameFor(which, color, "Co");
|
|
|
|
else
|
|
|
|
return getNameFor(which, color, campSuffix[2]);
|
2023-06-26 00:42:53 +02:00
|
|
|
}
|
|
|
|
|
2023-06-25 21:28:24 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-08-31 13:15:07 +02:00
|
|
|
void CampaignHeader::loadLegacyData(CampaignRegions regions, int numOfScenario)
|
|
|
|
{
|
|
|
|
campaignRegions = regions;
|
|
|
|
numberOfScenarios = numOfScenario;
|
|
|
|
}
|
|
|
|
|
2023-06-26 00:07:55 +02:00
|
|
|
bool CampaignHeader::playerSelectedDifficulty() const
|
|
|
|
{
|
2024-06-24 03:23:26 +02:00
|
|
|
return difficultyChosenByPlayer;
|
2023-06-26 00:07:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CampaignHeader::formatVCMI() const
|
|
|
|
{
|
|
|
|
return version == CampaignVersion::VCMI;
|
|
|
|
}
|
|
|
|
|
2023-09-27 22:53:13 +02:00
|
|
|
std::string CampaignHeader::getDescriptionTranslated() const
|
2023-06-26 00:07:55 +02:00
|
|
|
{
|
2023-09-27 22:53:13 +02:00
|
|
|
return description.toString();
|
2023-06-26 00:07:55 +02:00
|
|
|
}
|
|
|
|
|
2023-09-27 22:53:13 +02:00
|
|
|
std::string CampaignHeader::getNameTranslated() const
|
2023-06-26 00:07:55 +02:00
|
|
|
{
|
2023-09-27 22:53:13 +02:00
|
|
|
return name.toString();
|
2023-06-26 00:07:55 +02:00
|
|
|
}
|
|
|
|
|
2024-08-10 14:59:22 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-06-26 00:07:55 +02:00
|
|
|
std::string CampaignHeader::getFilename() const
|
|
|
|
{
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2023-06-27 19:09:11 +02:00
|
|
|
std::string CampaignHeader::getModName() const
|
|
|
|
{
|
|
|
|
return modName;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CampaignHeader::getEncoding() const
|
|
|
|
{
|
|
|
|
return encoding;
|
|
|
|
}
|
|
|
|
|
2023-09-04 12:03:15 +02:00
|
|
|
AudioPath CampaignHeader::getMusic() const
|
2023-06-27 19:09:11 +02:00
|
|
|
{
|
|
|
|
return music;
|
|
|
|
}
|
|
|
|
|
2024-08-31 16:03:42 +02:00
|
|
|
ImagePath CampaignHeader::getLoadingBackground() const
|
|
|
|
{
|
|
|
|
return loadingBackground;
|
|
|
|
}
|
|
|
|
|
2024-09-05 21:31:17 +02:00
|
|
|
ImagePath CampaignHeader::getVideoRim() const
|
2024-08-31 17:57:27 +02:00
|
|
|
{
|
2024-09-05 21:31:17 +02:00
|
|
|
return videoRim;
|
2024-08-31 17:57:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
VideoPath CampaignHeader::getIntroVideo() const
|
|
|
|
{
|
|
|
|
return introVideo;
|
|
|
|
}
|
|
|
|
|
2024-09-05 21:31:17 +02:00
|
|
|
VideoPath CampaignHeader::getOutroVideo() const
|
|
|
|
{
|
|
|
|
return outroVideo;
|
|
|
|
}
|
|
|
|
|
2023-06-26 00:07:55 +02:00
|
|
|
const CampaignRegions & CampaignHeader::getRegions() const
|
|
|
|
{
|
|
|
|
return campaignRegions;
|
|
|
|
}
|
|
|
|
|
2024-01-15 13:10:25 +02:00
|
|
|
TextContainerRegistrable & CampaignHeader::getTexts()
|
|
|
|
{
|
|
|
|
return textContainer;
|
|
|
|
}
|
|
|
|
|
2023-06-25 23:01:25 +02:00
|
|
|
bool CampaignState::isConquered(CampaignScenarioID whichScenario) const
|
|
|
|
{
|
|
|
|
return vstd::contains(mapsConquered, whichScenario);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CampaignState::isAvailable(CampaignScenarioID whichScenario) const
|
2023-06-25 21:28:24 +02:00
|
|
|
{
|
|
|
|
//check for void scenraio
|
2023-06-26 00:07:55 +02:00
|
|
|
if (!scenario(whichScenario).isNotVoid())
|
2023-06-25 21:28:24 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-06-25 23:01:25 +02:00
|
|
|
if (vstd::contains(mapsConquered, whichScenario))
|
2023-06-25 21:28:24 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
//check preconditioned regions
|
2023-06-26 00:07:55 +02:00
|
|
|
for (auto const & it : scenario(whichScenario).preconditionRegions)
|
2023-06-25 21:28:24 +02:00
|
|
|
{
|
2023-06-25 23:01:25 +02:00
|
|
|
if (!vstd::contains(mapsConquered, it))
|
2023-06-25 21:28:24 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CampaignScenario::isNotVoid() const
|
|
|
|
{
|
|
|
|
return !mapName.empty();
|
|
|
|
}
|
|
|
|
|
2023-06-26 15:25:34 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-06-25 23:01:25 +02:00
|
|
|
const CGHeroInstance * CampaignState::strongestHero(CampaignScenarioID scenarioId, const PlayerColor & owner) const
|
2023-06-25 21:28:24 +02:00
|
|
|
{
|
2024-01-01 16:37:48 +02:00
|
|
|
std::function<bool(const JsonNode & node)> isOwned = [&](const JsonNode & node)
|
2023-06-25 21:28:24 +02:00
|
|
|
{
|
2023-06-26 15:25:34 +02:00
|
|
|
auto * h = CampaignState::crossoverDeserialize(node, nullptr);
|
2023-06-25 21:28:24 +02:00
|
|
|
bool result = h->tempOwner == owner;
|
|
|
|
vstd::clear_pointer(h);
|
|
|
|
return result;
|
|
|
|
};
|
2023-06-26 15:25:34 +02:00
|
|
|
auto ownedHeroes = scenarioHeroPool.at(scenarioId) | boost::adaptors::filtered(isOwned);
|
2023-06-25 21:28:24 +02:00
|
|
|
|
2023-06-26 15:25:34 +02:00
|
|
|
if (ownedHeroes.empty())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return CampaignState::crossoverDeserialize(ownedHeroes.front(), nullptr);
|
2023-06-25 21:28:24 +02:00
|
|
|
}
|
|
|
|
|
2023-06-26 15:25:34 +02:00
|
|
|
/// Returns heroes that can be instantiated as hero placeholders by power
|
|
|
|
const std::vector<JsonNode> & CampaignState::getHeroesByPower(CampaignScenarioID scenarioId) const
|
2023-06-25 21:28:24 +02:00
|
|
|
{
|
2023-06-26 15:25:34 +02:00
|
|
|
static const std::vector<JsonNode> emptyVector;
|
2023-06-25 23:01:25 +02:00
|
|
|
|
2023-06-26 15:25:34 +02:00
|
|
|
if (scenarioHeroPool.count(scenarioId))
|
|
|
|
return scenarioHeroPool.at(scenarioId);
|
2023-06-25 23:01:25 +02:00
|
|
|
|
2023-06-26 15:25:34 +02:00
|
|
|
return emptyVector;
|
2023-06-25 21:28:24 +02:00
|
|
|
}
|
|
|
|
|
2023-06-26 15:25:34 +02:00
|
|
|
/// 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
|
2023-06-25 23:01:25 +02:00
|
|
|
{
|
2023-06-26 15:25:34 +02:00
|
|
|
static const JsonNode emptyNode;
|
|
|
|
|
|
|
|
if (!getReservedHeroes().count(heroID))
|
|
|
|
return emptyNode;
|
|
|
|
|
|
|
|
if (!globalHeroPool.count(heroID))
|
|
|
|
return emptyNode;
|
|
|
|
|
|
|
|
return globalHeroPool.at(heroID);
|
2023-06-25 23:01:25 +02:00
|
|
|
}
|
|
|
|
|
2023-06-26 15:25:34 +02:00
|
|
|
void CampaignState::setCurrentMapAsConquered(std::vector<CGHeroInstance *> heroes)
|
2023-06-25 21:28:24 +02:00
|
|
|
{
|
2023-06-26 15:25:34 +02:00
|
|
|
range::sort(heroes, [](const CGHeroInstance * a, const CGHeroInstance * b)
|
2023-06-25 21:28:24 +02:00
|
|
|
{
|
2024-09-05 17:38:27 +02:00
|
|
|
return a->getHeroStrengthForCampaign() > b->getHeroStrengthForCampaign();
|
2023-06-26 15:25:34 +02:00
|
|
|
});
|
|
|
|
|
2023-11-02 16:56:02 +02:00
|
|
|
logGlobal->info("Scenario %d of campaign %s (%s) has been completed", currentMap->getNum(), getFilename(), getNameTranslated());
|
2023-06-25 21:28:24 +02:00
|
|
|
|
|
|
|
mapsConquered.push_back(*currentMap);
|
2023-06-26 15:25:34 +02:00
|
|
|
auto reservedHeroes = getReservedHeroes();
|
|
|
|
|
|
|
|
for (auto * hero : heroes)
|
|
|
|
{
|
|
|
|
JsonNode node = CampaignState::crossoverSerialize(hero);
|
|
|
|
|
2024-10-05 21:37:52 +02:00
|
|
|
if (reservedHeroes.count(hero->getHeroTypeID()))
|
2023-06-26 15:25:34 +02:00
|
|
|
{
|
2024-10-05 21:37:52 +02:00
|
|
|
logGlobal->info("Hero crossover: %d (%s) exported to global pool", hero->getHeroTypeID(), hero->getNameTranslated());
|
|
|
|
globalHeroPool[hero->getHeroTypeID()] = node;
|
2023-06-26 15:25:34 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-10-05 21:37:52 +02:00
|
|
|
logGlobal->info("Hero crossover: %d (%s) exported to scenario pool", hero->getHeroTypeID(), hero->getNameTranslated());
|
2023-06-26 15:25:34 +02:00
|
|
|
scenarioHeroPool[*currentMap].push_back(node);
|
|
|
|
}
|
|
|
|
}
|
2023-06-25 21:28:24 +02:00
|
|
|
}
|
|
|
|
|
2023-06-26 00:07:55 +02:00
|
|
|
std::optional<CampaignBonus> CampaignState::getBonus(CampaignScenarioID which) const
|
2023-06-25 21:28:24 +02:00
|
|
|
{
|
2023-06-26 00:07:55 +02:00
|
|
|
auto bonuses = scenario(which).travelOptions.bonusesToChoose;
|
2023-06-26 15:25:34 +02:00
|
|
|
assert(chosenCampaignBonuses.count(*currentMap) || bonuses.empty());
|
2023-06-25 21:28:24 +02:00
|
|
|
|
|
|
|
if(bonuses.empty())
|
|
|
|
return std::optional<CampaignBonus>();
|
|
|
|
|
2023-06-26 00:07:55 +02:00
|
|
|
if (!getBonusID(which))
|
|
|
|
return std::optional<CampaignBonus>();
|
|
|
|
|
|
|
|
return bonuses[getBonusID(which).value()];
|
2023-06-25 21:28:24 +02:00
|
|
|
}
|
|
|
|
|
2023-06-26 00:07:55 +02:00
|
|
|
std::optional<ui8> CampaignState::getBonusID(CampaignScenarioID which) const
|
2023-06-25 21:28:24 +02:00
|
|
|
{
|
2023-06-25 23:01:25 +02:00
|
|
|
if (!chosenCampaignBonuses.count(which))
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
return chosenCampaignBonuses.at(which);
|
2023-06-25 21:28:24 +02:00
|
|
|
}
|
|
|
|
|
2024-01-31 20:01:24 +02:00
|
|
|
std::unique_ptr<CMap> CampaignState::getMap(CampaignScenarioID scenarioId, IGameCallback * cb)
|
2023-06-25 21:28:24 +02:00
|
|
|
{
|
|
|
|
// FIXME: there is certainly better way to handle maps inside campaigns
|
|
|
|
if(scenarioId == CampaignScenarioID::NONE)
|
|
|
|
scenarioId = currentMap.value();
|
|
|
|
|
|
|
|
CMapService mapService;
|
2023-06-27 19:09:11 +02:00
|
|
|
std::string scenarioName = getFilename().substr(0, getFilename().find('.'));
|
2023-06-25 21:28:24 +02:00
|
|
|
boost::to_lower(scenarioName);
|
2023-11-02 16:56:02 +02:00
|
|
|
scenarioName += ':' + std::to_string(scenarioId.getNum());
|
2023-06-27 15:27:35 +02:00
|
|
|
const auto & mapContent = mapPieces.find(scenarioId)->second;
|
2024-01-31 20:01:24 +02:00
|
|
|
auto result = mapService.loadMap(mapContent.data(), mapContent.size(), scenarioName, getModName(), getEncoding(), cb);
|
|
|
|
|
|
|
|
mapTranslations[scenarioId] = result->texts;
|
|
|
|
return result;
|
2023-06-25 21:28:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<CMapHeader> CampaignState::getMapHeader(CampaignScenarioID scenarioId) const
|
|
|
|
{
|
|
|
|
if(scenarioId == CampaignScenarioID::NONE)
|
|
|
|
scenarioId = currentMap.value();
|
|
|
|
|
|
|
|
CMapService mapService;
|
2023-06-27 19:09:11 +02:00
|
|
|
std::string scenarioName = getFilename().substr(0, getFilename().find('.'));
|
2023-06-25 21:28:24 +02:00
|
|
|
boost::to_lower(scenarioName);
|
2023-11-02 16:56:02 +02:00
|
|
|
scenarioName += ':' + std::to_string(scenarioId.getNum());
|
2023-06-27 15:27:35 +02:00
|
|
|
const auto & mapContent = mapPieces.find(scenarioId)->second;
|
2023-06-27 19:09:11 +02:00
|
|
|
return mapService.loadMapHeader(mapContent.data(), mapContent.size(), scenarioName, getModName(), getEncoding());
|
2023-06-25 21:28:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<CMapInfo> CampaignState::getMapInfo(CampaignScenarioID scenarioId) const
|
|
|
|
{
|
|
|
|
if(scenarioId == CampaignScenarioID::NONE)
|
|
|
|
scenarioId = currentMap.value();
|
|
|
|
|
|
|
|
auto mapInfo = std::make_shared<CMapInfo>();
|
2023-06-27 19:09:11 +02:00
|
|
|
mapInfo->fileURI = getFilename();
|
2023-06-25 21:28:24 +02:00
|
|
|
mapInfo->mapHeader = getMapHeader(scenarioId);
|
|
|
|
mapInfo->countPlayers();
|
|
|
|
return mapInfo;
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:37:48 +02:00
|
|
|
JsonNode CampaignState::crossoverSerialize(CGHeroInstance * hero) const
|
2023-06-25 21:28:24 +02:00
|
|
|
{
|
|
|
|
JsonNode node;
|
|
|
|
JsonSerializer handler(nullptr, node);
|
|
|
|
hero->serializeJsonOptions(handler);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:37:48 +02:00
|
|
|
CGHeroInstance * CampaignState::crossoverDeserialize(const JsonNode & node, CMap * map) const
|
2023-06-25 21:28:24 +02:00
|
|
|
{
|
2023-06-25 23:01:25 +02:00
|
|
|
JsonDeserializer handler(nullptr, const_cast<JsonNode&>(node));
|
2024-04-06 13:49:36 +02:00
|
|
|
auto * hero = new CGHeroInstance(map ? map->cb : nullptr);
|
2023-06-25 21:28:24 +02:00
|
|
|
hero->ID = Obj::HERO;
|
|
|
|
hero->serializeJsonOptions(handler);
|
2023-06-26 15:25:34 +02:00
|
|
|
if (map)
|
2024-09-04 19:39:13 +02:00
|
|
|
{
|
2024-09-04 13:32:47 +02:00
|
|
|
hero->serializeJsonArtifacts(handler, "artifacts");
|
2024-09-04 19:39:13 +02:00
|
|
|
map->addNewArtifactInstance(*hero);
|
|
|
|
}
|
2023-06-25 21:28:24 +02:00
|
|
|
return hero;
|
|
|
|
}
|
2023-06-25 23:01:25 +02:00
|
|
|
|
|
|
|
void CampaignState::setCurrentMap(CampaignScenarioID which)
|
|
|
|
{
|
2023-06-26 00:07:55 +02:00
|
|
|
assert(scenario(which).isNotVoid());
|
2023-06-25 23:01:25 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-06-26 00:07:55 +02:00
|
|
|
std::set<CampaignScenarioID> Campaign::allScenarios() const
|
2023-06-25 23:01:25 +02:00
|
|
|
{
|
|
|
|
std::set<CampaignScenarioID> result;
|
|
|
|
|
|
|
|
for (auto const & entry : scenarios)
|
|
|
|
{
|
|
|
|
if (entry.second.isNotVoid())
|
|
|
|
result.insert(entry.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-09-01 12:23:10 +02:00
|
|
|
void Campaign::overrideCampaign()
|
2024-08-31 14:17:25 +02:00
|
|
|
{
|
2024-09-01 12:23:10 +02:00
|
|
|
const JsonNode node = JsonUtils::assembleFromFiles("config/campaignOverrides.json");
|
2024-08-31 14:17:25 +02:00
|
|
|
for (auto & entry : node.Struct())
|
2024-09-01 02:16:03 +02:00
|
|
|
if(filename == entry.first)
|
2024-08-31 14:17:25 +02:00
|
|
|
{
|
2024-09-01 12:23:10 +02:00
|
|
|
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());
|
2024-09-05 21:31:17 +02:00
|
|
|
if(!entry.second["videoRim"].isNull())
|
|
|
|
videoRim = ImagePath::builtin(entry.second["videoRim"].String());
|
2024-09-01 12:23:10 +02:00
|
|
|
if(!entry.second["introVideo"].isNull())
|
|
|
|
introVideo = VideoPath::builtin(entry.second["introVideo"].String());
|
2024-09-05 21:31:17 +02:00
|
|
|
if(!entry.second["outroVideo"].isNull())
|
|
|
|
outroVideo = VideoPath::builtin(entry.second["outroVideo"].String());
|
2024-09-01 12:23:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
2024-09-01 02:16:03 +02:00
|
|
|
{
|
2024-09-01 12:23:10 +02:00
|
|
|
auto sc = entry.second["scenarios"].Vector();
|
|
|
|
for(int i = 0; i < sc.size(); i++)
|
2024-08-31 14:17:25 +02:00
|
|
|
{
|
2024-09-01 12:23:10 +02:00
|
|
|
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());
|
2024-08-31 14:17:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-26 00:07:55 +02:00
|
|
|
int Campaign::scenariosCount() const
|
|
|
|
{
|
|
|
|
return allScenarios().size();
|
|
|
|
}
|
|
|
|
|
|
|
|
const CampaignScenario & Campaign::scenario(CampaignScenarioID which) const
|
2023-06-25 23:01:25 +02:00
|
|
|
{
|
|
|
|
assert(scenarios.count(which));
|
|
|
|
assert(scenarios.at(which).isNotVoid());
|
|
|
|
|
|
|
|
return scenarios.at(which);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CampaignState::isCampaignFinished() const
|
|
|
|
{
|
|
|
|
return conqueredScenarios() == allScenarios();
|
|
|
|
}
|
2023-06-26 16:25:29 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|