| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 | 
							- /*
 
-  * 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"
 
- 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.xpos = static_cast<int>(node["x"].Float());
 
- 	rd.ypos = static_cast<int>(node["y"].Float());
 
- 	return rd;
 
- }
 
- CampaignRegions CampaignRegions::fromJson(const JsonNode & node)
 
- {
 
- 	CampaignRegions cr;
 
- 	cr.campPrefix = node["prefix"].String();
 
- 	cr.colorSuffixLength = static_cast<int>(node["color_suffix_length"].Float());
 
- 	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
 
- 	{
 
- 		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
 
- {
 
- 	return ImagePath::builtin(campPrefix + "_BG.BMP");
 
- }
 
- Point CampaignRegions::getPosition(CampaignScenarioID which) const
 
- {
 
- 	auto const & region = regions[which.getNum()];
 
- 	return Point(region.xpos, region.ypos);
 
- }
 
- ImagePath CampaignRegions::getNameFor(CampaignScenarioID which, int colorIndex, std::string type) const
 
- {
 
- 	auto const & region = regions[which.getNum()];
 
- 	static const std::string colors[2][8] =
 
- 	{
 
- 		{"R", "B", "N", "G", "O", "V", "T", "P"},
 
- 		{"Re", "Bl", "Br", "Gr", "Or", "Vi", "Te", "Pi"}
 
- 	};
 
- 	std::string color = colors[colorSuffixLength - 1][colorIndex];
 
- 	return ImagePath::builtin(campPrefix + region.infix + "_" + type + color + ".BMP");
 
- }
 
- ImagePath CampaignRegions::getAvailableName(CampaignScenarioID which, int color) const
 
- {
 
- 	return getNameFor(which, color, "En");
 
- }
 
- ImagePath CampaignRegions::getSelectedName(CampaignScenarioID which, int color) const
 
- {
 
- 	return getNameFor(which, color, "Se");
 
- }
 
- ImagePath CampaignRegions::getConqueredName(CampaignScenarioID which, int color) const
 
- {
 
- 	return getNameFor(which, color, "Co");
 
- }
 
- 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);
 
- }
 
- 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;
 
- }
 
- 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->getHeroStrength() > b->getHeroStrength();
 
- 	});
 
- 	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->getHeroType()))
 
- 		{
 
- 			logGlobal->info("Hero crossover: %d (%s) exported to global pool", hero->getHeroType(), hero->getNameTranslated());
 
- 			globalHeroPool[hero->getHeroType()] = node;
 
- 		}
 
- 		else
 
- 		{
 
- 			logGlobal->info("Hero crossover: %d (%s) exported to scenario pool", hero->getHeroType(), 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);
 
- 	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;
 
- }
 
- 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
 
 
  |