1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-28 08:48:48 +02:00
vcmi/lib/mapping/CCampaignHandler.cpp

877 lines
28 KiB
C++
Raw Normal View History

/*
* 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"
2010-02-08 16:39:19 +02:00
#include "CCampaignHandler.h"
#include "../filesystem/Filesystem.h"
2013-04-07 13:48:07 +03:00
#include "../filesystem/CCompressedStream.h"
#include "../filesystem/CMemoryStream.h"
#include "../filesystem/CBinaryReader.h"
#include "../VCMI_Lib.h"
#include "../vcmi_endian.h"
#include "../CGeneralTextHandler.h"
#include "../TextOperations.h"
#include "../StartInfo.h"
#include "../CModHandler.h"
#include "../CArtHandler.h" //for hero crossover
#include "../mapObjects/CGHeroInstance.h"//for hero crossover
#include "../CHeroHandler.h"
#include "../Languages.h"
2023-04-09 02:53:06 +02:00
#include "../StringConstants.h"
#include "CMapService.h"
#include "CMap.h"
#include "CMapInfo.h"
2010-02-08 16:39:19 +02:00
// For hero crossover
#include "serializer/CSerializer.h"
#include "serializer/JsonDeserializer.h"
#include "serializer/JsonSerializer.h"
2010-02-08 16:39:19 +02:00
VCMI_LIB_NAMESPACE_BEGIN
2023-04-17 02:26:35 +02:00
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(ResourceID("config/campaign_regions.json"));
for(const JsonNode & campaign : config["campaign_regions"].Vector())
campDescriptions.push_back(CampaignRegions::fromJson(campaign));
}
return campDescriptions.at(campId);
}
2016-11-25 16:21:27 +02:00
bool CScenarioTravel::STravelBonus::isBonusForHero() const
{
return type == SPELL || type == MONSTER || type == ARTIFACT || type == SPELL_SCROLL || type == PRIMARY_SKILL
|| type == SECONDARY_SKILL;
}
2023-04-17 02:26:35 +02:00
void CCampaignHeader::loadLegacyData(ui8 campId)
{
campaignRegions = CampaignRegions::getLegacy(campId);
numberOfScenarios = VLC->generaltexth->getCampaignLength(campId);
}
CCampaignHeader CCampaignHandler::getHeader( const std::string & name)
2010-02-08 16:39:19 +02:00
{
ResourceID resourceID(name, EResType::CAMPAIGN);
std::string modName = VLC->modh->findResourceOrigin(resourceID);
std::string language = VLC->modh->getModLanguage(modName);
std::string encoding = Languages::getLanguageOptions(language).encoding;
2023-04-09 01:24:40 +02:00
2023-04-20 17:22:27 +02:00
auto fileStream = CResourceHandler::get(modName)->load(resourceID);
std::vector<ui8> cmpgn = getFile(std::move(fileStream), true)[0];
JsonNode jsonCampaign((const char*)cmpgn.data(), cmpgn.size());
2023-04-09 01:24:40 +02:00
if(jsonCampaign.isNull())
{
2023-04-20 17:22:27 +02:00
//legacy OH3 campaign (*.h3c)
2023-04-09 01:24:40 +02:00
CMemoryStream stream(cmpgn.data(), cmpgn.size());
CBinaryReader reader(&stream);
2023-04-20 17:22:27 +02:00
return readHeaderFromMemory(reader, resourceID.getName(), modName, encoding);
2023-04-09 01:24:40 +02:00
}
2023-04-20 17:22:27 +02:00
//VCMI (*.vcmp)
return readHeaderFromJson(jsonCampaign, resourceID.getName(), modName, encoding);
2010-02-11 16:12:22 +02:00
}
std::unique_ptr<CCampaign> CCampaignHandler::getCampaign( const std::string & name )
2010-02-11 16:12:22 +02:00
{
ResourceID resourceID(name, EResType::CAMPAIGN);
std::string modName = VLC->modh->findResourceOrigin(resourceID);
std::string language = VLC->modh->getModLanguage(modName);
std::string encoding = Languages::getLanguageOptions(language).encoding;
2023-04-09 01:24:40 +02:00
2022-12-07 23:36:20 +02:00
auto ret = std::make_unique<CCampaign>();
2023-04-09 01:24:40 +02:00
2023-04-20 17:22:27 +02:00
auto fileStream = CResourceHandler::get(modName)->load(resourceID);
std::vector<std::vector<ui8>> files = getFile(std::move(fileStream), false);
2023-05-24 21:02:03 +02:00
if (files[0].front() < uint8_t(' ')) // binary format
2023-04-09 01:24:40 +02:00
{
2023-04-20 17:22:27 +02:00
CMemoryStream stream(files[0].data(), files[0].size());
2023-04-09 01:24:40 +02:00
CBinaryReader reader(&stream);
ret->header = readHeaderFromMemory(reader, resourceID.getName(), modName, encoding);
2023-04-20 17:22:27 +02:00
for(int g = 0; g < ret->header.numberOfScenarios; ++g)
ret->scenarios.emplace_back(readScenarioFromMemory(reader, ret->header));
2023-04-09 01:24:40 +02:00
}
2023-05-24 21:02:03 +02:00
else // text format (json)
{
2023-05-24 21:02:03 +02:00
JsonNode jsonCampaign((const char*)files[0].data(), files[0].size());
2023-04-09 01:24:40 +02:00
ret->header = readHeaderFromJson(jsonCampaign, resourceID.getName(), modName, encoding);
for(auto & scenario : jsonCampaign["scenarios"].Vector())
2023-04-20 17:22:27 +02:00
ret->scenarios.emplace_back(readScenarioFromJson(scenario));
}
//first entry is campaign header. start loop from 1
for(int scenarioID = 0, g = 1; g < files.size() && scenarioID < ret->header.numberOfScenarios; ++g)
{
while(!ret->scenarios[scenarioID].isNotVoid()) //skip void scenarios
scenarioID++;
std::string scenarioName = resourceID.getName();
boost::to_lower(scenarioName);
scenarioName += ':' + std::to_string(g - 1);
//set map piece appropriately, convert vector to string
ret->mapPieces[scenarioID].assign(reinterpret_cast<const char*>(files[g].data()), files[g].size());
CMapService mapService;
auto hdr = mapService.loadMapHeader(
reinterpret_cast<const ui8 *>(ret->mapPieces[scenarioID].c_str()),
static_cast<int>(ret->mapPieces[scenarioID].size()),
scenarioName,
modName,
encoding);
ret->scenarios[scenarioID].scenarioName = hdr->name;
scenarioID++;
}
2010-02-08 16:39:19 +02:00
// handle campaign specific discrepancies
2023-03-14 19:46:15 +02:00
if(resourceID.getName() == "DATA/AB")
{
2023-02-11 18:30:06 +02:00
ret->scenarios[6].keepHeroes.emplace_back(155); // keep hero Xeron for map 7 To Kill A Hero
}
2023-03-14 19:46:15 +02:00
else if(resourceID.getName() == "DATA/FINAL")
{
// keep following heroes for map 8 Final H
2023-02-11 18:30:06 +02:00
ret->scenarios[7].keepHeroes.emplace_back(148); // Gelu
ret->scenarios[7].keepHeroes.emplace_back(27); // Gem
ret->scenarios[7].keepHeroes.emplace_back(102); // Crag Hack
ret->scenarios[7].keepHeroes.emplace_back(96); // Yog
}
2010-02-11 16:12:22 +02:00
return ret;
}
2023-03-14 19:46:15 +02:00
static std::string convertMapName(std::string input)
{
2023-03-14 19:46:15 +02:00
boost::algorithm::to_lower(input);
boost::algorithm::trim(input);
size_t slashPos = input.find_last_of("/");
if (slashPos != std::string::npos)
return input.substr(slashPos + 1);
return input;
}
std::string CCampaignHandler::readLocalizedString(CBinaryReader & reader, std::string filename, std::string modName, std::string encoding, std::string identifier)
{
TextIdentifier stringID( "campaign", convertMapName(filename), identifier);
std::string input = TextOperations::toUnicode(reader.readBaseString(), encoding);
if (input.empty())
return "";
VLC->generaltexth->registerString(modName, stringID, input);
return VLC->generaltexth->translate(stringID.get());
}
2023-04-17 02:26:35 +02:00
CCampaignHeader CCampaignHandler::readHeaderFromJson(JsonNode & reader, std::string filename, std::string modName, std::string encoding)
2023-04-09 01:24:40 +02:00
{
CCampaignHeader ret;
ret.version = reader["version"].Integer();
if(ret.version < CampaignVersion::VCMI_MIN || ret.version > CampaignVersion::VCMI_MAX)
{
2023-04-09 11:29:07 +02:00
logGlobal->info("VCMP Loading: Unsupported campaign %s version %d", filename, ret.version);
2023-04-09 01:24:40 +02:00
return ret;
}
ret.version = CampaignVersion::VCMI;
2023-04-17 02:26:35 +02:00
ret.campaignRegions = CampaignRegions::fromJson(reader["regions"]);
ret.numberOfScenarios = reader["scenarios"].Vector().size();
2023-04-09 01:24:40 +02:00
ret.name = reader["name"].String();
ret.description = reader["description"].String();
ret.difficultyChoosenByPlayer = reader["allowDifficultySelection"].Bool();
//skip ret.music because it's unused in vcmi
ret.filename = filename;
ret.modName = modName;
ret.encoding = encoding;
ret.valid = true;
return ret;
}
2023-04-17 02:26:35 +02:00
CCampaignScenario CCampaignHandler::readScenarioFromJson(JsonNode & reader)
2023-04-09 01:24:40 +02:00
{
auto prologEpilogReader = [](JsonNode & identifier) -> CCampaignScenario::SScenarioPrologEpilog
{
CCampaignScenario::SScenarioPrologEpilog ret;
ret.hasPrologEpilog = !identifier.isNull();
if(ret.hasPrologEpilog)
{
ret.prologVideo = identifier["video"].String();
ret.prologMusic = identifier["music"].String();
2023-04-09 01:24:40 +02:00
ret.prologText = identifier["text"].String();
}
return ret;
};
CCampaignScenario ret;
ret.conquered = false;
ret.mapName = reader["map"].String();
2023-04-20 01:20:00 +02:00
for(auto & g : reader["preconditions"].Vector())
2023-04-09 01:24:40 +02:00
ret.preconditionRegions.insert(g.Integer());
ret.regionColor = reader["color"].Integer();
ret.difficulty = reader["difficulty"].Integer();
ret.regionText = reader["regionText"].String();
ret.prolog = prologEpilogReader(reader["prolog"]);
ret.epilog = prologEpilogReader(reader["epilog"]);
2023-04-17 02:26:35 +02:00
ret.travelOptions = readScenarioTravelFromJson(reader);
2023-04-09 01:24:40 +02:00
return ret;
}
2023-04-17 02:26:35 +02:00
CScenarioTravel CCampaignHandler::readScenarioTravelFromJson(JsonNode & reader)
2023-04-09 01:24:40 +02:00
{
CScenarioTravel ret;
std::map<std::string, ui8> startOptionsMap = {
{"none", 0},
{"bonus", 1},
{"crossover", 2},
{"hero", 3}
};
std::map<std::string, CScenarioTravel::STravelBonus::EBonusType> bonusTypeMap = {
{"spell", CScenarioTravel::STravelBonus::EBonusType::SPELL},
{"creature", CScenarioTravel::STravelBonus::EBonusType::MONSTER},
{"building", CScenarioTravel::STravelBonus::EBonusType::BUILDING},
{"artifact", CScenarioTravel::STravelBonus::EBonusType::ARTIFACT},
{"scroll", CScenarioTravel::STravelBonus::EBonusType::SPELL_SCROLL},
2023-04-09 02:26:04 +02:00
{"primarySkill", CScenarioTravel::STravelBonus::EBonusType::PRIMARY_SKILL},
{"secondarySkill", CScenarioTravel::STravelBonus::EBonusType::SECONDARY_SKILL},
2023-04-09 01:24:40 +02:00
{"resource", CScenarioTravel::STravelBonus::EBonusType::RESOURCE},
//{"prevHero", CScenarioTravel::STravelBonus::EBonusType::HEROES_FROM_PREVIOUS_SCENARIO},
//{"hero", CScenarioTravel::STravelBonus::EBonusType::HERO},
};
2023-04-09 02:26:04 +02:00
std::map<std::string, ui32> primarySkillsMap = {
{"attack", 0},
{"defence", 8},
{"spellpower", 16},
{"knowledge", 24},
};
2023-04-09 01:24:40 +02:00
std::map<std::string, ui16> heroSpecialMap = {
{"strongest", 0xFFFD},
{"generated", 0xFFFE},
{"random", 0xFFFF}
};
std::map<std::string, ui8> resourceTypeMap = {
//FD - wood+ore
//FE - mercury+sulfur+crystal+gem
{"wood", 0},
2023-04-09 02:26:04 +02:00
{"mercury", 1},
{"ore", 2},
2023-04-09 01:24:40 +02:00
{"sulfur", 3},
{"crystal", 4},
2023-04-09 02:26:04 +02:00
{"gems", 5},
2023-04-09 01:24:40 +02:00
{"gold", 6},
{"common", 0xFD},
{"rare", 0xFE}
};
for(auto & k : reader["heroKeeps"].Vector())
2023-04-17 21:19:56 +02:00
{
if(k.String() == "experience") ret.whatHeroKeeps.experience = true;
if(k.String() == "primarySkills") ret.whatHeroKeeps.primarySkills = true;
if(k.String() == "secondarySkills") ret.whatHeroKeeps.secondarySkills = true;
if(k.String() == "spells") ret.whatHeroKeeps.spells = true;
if(k.String() == "artifacts") ret.whatHeroKeeps.artifacts = true;
}
2023-04-09 01:24:40 +02:00
2023-04-09 03:02:34 +02:00
for(auto & k : reader["keepCreatures"].Vector())
{
2023-04-09 11:29:07 +02:00
if(auto identifier = VLC->modh->identifiers.getIdentifier(CModHandler::scopeMap(), "creature", k.String()))
2023-04-19 01:11:13 +02:00
ret.monstersKeptByHero.insert(CreatureID(identifier.value()));
2023-04-09 11:29:07 +02:00
else
logGlobal->warn("VCMP Loading: keepCreatures contains unresolved identifier %s", k.String());
2023-04-09 03:02:34 +02:00
}
for(auto & k : reader["keepArtifacts"].Vector())
{
2023-04-09 11:29:07 +02:00
if(auto identifier = VLC->modh->identifiers.getIdentifier(CModHandler::scopeMap(), "artifact", k.String()))
2023-04-19 01:11:13 +02:00
ret.artifactsKeptByHero.insert(ArtifactID(identifier.value()));
2023-04-09 11:29:07 +02:00
else
logGlobal->warn("VCMP Loading: keepArtifacts contains unresolved identifier %s", k.String());
2023-04-09 03:02:34 +02:00
}
2023-04-09 01:24:40 +02:00
ret.startOptions = startOptionsMap[reader["startOptions"].String()];
switch(ret.startOptions)
{
case 0:
//no bonuses. Seems to be OK
break;
case 1: //reading of bonuses player can choose
{
ret.playerColor = reader["playerColor"].Integer();
for(auto & bjson : reader["bonuses"].Vector())
{
CScenarioTravel::STravelBonus bonus;
bonus.type = bonusTypeMap[bjson["what"].String()];
2023-04-09 11:29:07 +02:00
switch (bonus.type)
2023-04-09 01:24:40 +02:00
{
2023-04-09 11:29:07 +02:00
case CScenarioTravel::STravelBonus::EBonusType::RESOURCE:
bonus.info1 = resourceTypeMap[bjson["type"].String()];
bonus.info2 = bjson["amount"].Integer();
break;
case CScenarioTravel::STravelBonus::EBonusType::BUILDING:
bonus.info1 = vstd::find_pos(EBuildingType::names, bjson["type"].String());
if(bonus.info1 == -1)
logGlobal->warn("VCMP Loading: unresolved building identifier %s", bjson["type"].String());
break;
default:
if(int heroId = heroSpecialMap[bjson["hero"].String()])
bonus.info1 = heroId;
else
if(auto identifier = VLC->modh->identifiers.getIdentifier(CModHandler::scopeMap(), "hero", bjson["hero"].String()))
2023-04-19 01:11:13 +02:00
bonus.info1 = identifier.value();
2023-04-09 11:29:07 +02:00
else
logGlobal->warn("VCMP Loading: unresolved hero identifier %s", bjson["hero"].String());
bonus.info3 = bjson["amount"].Integer();
switch(bonus.type)
{
case CScenarioTravel::STravelBonus::EBonusType::SPELL:
case CScenarioTravel::STravelBonus::EBonusType::MONSTER:
case CScenarioTravel::STravelBonus::EBonusType::SECONDARY_SKILL:
case CScenarioTravel::STravelBonus::EBonusType::ARTIFACT:
if(auto identifier = VLC->modh->identifiers.getIdentifier(CModHandler::scopeMap(), bjson["what"].String(), bjson["type"].String()))
2023-04-19 01:11:13 +02:00
bonus.info2 = identifier.value();
2023-04-09 11:29:07 +02:00
else
logGlobal->warn("VCMP Loading: unresolved %s identifier %s", bjson["what"].String(), bjson["type"].String());
break;
case CScenarioTravel::STravelBonus::EBonusType::SPELL_SCROLL:
if(auto Identifier = VLC->modh->identifiers.getIdentifier(CModHandler::scopeMap(), "spell", bjson["type"].String()))
2023-04-19 01:11:13 +02:00
bonus.info2 = Identifier.value();
2023-04-09 11:29:07 +02:00
else
logGlobal->warn("VCMP Loading: unresolved spell scroll identifier %s", bjson["type"].String());
break;
case CScenarioTravel::STravelBonus::EBonusType::PRIMARY_SKILL:
for(auto & ps : primarySkillsMap)
bonus.info2 |= bjson[ps.first].Integer() << ps.second;
break;
default:
bonus.info2 = bjson["type"].Integer();
}
break;
2023-04-09 01:24:40 +02:00
}
ret.bonusesToChoose.push_back(bonus);
}
break;
}
case 2: //reading of players (colors / scenarios ?) player can choose
{
for(auto & bjson : reader["bonuses"].Vector())
{
CScenarioTravel::STravelBonus bonus;
bonus.type = CScenarioTravel::STravelBonus::HEROES_FROM_PREVIOUS_SCENARIO;
bonus.info1 = bjson["playerColor"].Integer(); //player color
bonus.info2 = bjson["scenario"].Integer(); //from what scenario
ret.bonusesToChoose.push_back(bonus);
}
break;
}
case 3: //heroes player can choose between
{
for(auto & bjson : reader["bonuses"].Vector())
{
CScenarioTravel::STravelBonus bonus;
bonus.type = CScenarioTravel::STravelBonus::HERO;
bonus.info1 = bjson["playerColor"].Integer(); //player color
if(int heroId = heroSpecialMap[bjson["hero"].String()])
bonus.info2 = heroId;
else
2023-04-09 11:29:07 +02:00
if (auto identifier = VLC->modh->identifiers.getIdentifier(CModHandler::scopeMap(), "hero", bjson["hero"].String()))
2023-04-19 01:11:13 +02:00
bonus.info2 = identifier.value();
2023-04-09 11:29:07 +02:00
else
logGlobal->warn("VCMP Loading: unresolved hero identifier %s", bjson["hero"].String());
2023-04-09 01:24:40 +02:00
ret.bonusesToChoose.push_back(bonus);
}
break;
}
default:
{
2023-04-09 11:29:07 +02:00
logGlobal->warn("VCMP Loading: Unsupported start options value");
2023-04-09 01:24:40 +02:00
break;
}
}
return ret;
}
2023-02-25 17:44:15 +02:00
CCampaignHeader CCampaignHandler::readHeaderFromMemory( CBinaryReader & reader, std::string filename, std::string modName, std::string encoding )
2010-02-11 16:12:22 +02:00
{
CCampaignHeader ret;
ret.version = reader.readUInt32();
2023-04-17 02:26:35 +02:00
ui8 campId = reader.readUInt8() - 1;//change range of it from [1, 20] to [0, 19]
ret.loadLegacyData(campId);
2023-03-14 19:46:15 +02:00
ret.name = readLocalizedString(reader, filename, modName, encoding, "name");
ret.description = readLocalizedString(reader, filename, modName, encoding, "description");
if (ret.version > CampaignVersion::RoE)
ret.difficultyChoosenByPlayer = reader.readInt8();
else
2023-04-17 21:19:56 +02:00
ret.difficultyChoosenByPlayer = false;
reader.readInt8(); //music - skip as unused
ret.filename = filename;
2023-02-25 17:44:15 +02:00
ret.modName = modName;
ret.encoding = encoding;
2023-04-09 01:24:40 +02:00
ret.valid = true;
2010-02-11 16:12:22 +02:00
return ret;
}
2023-04-17 02:26:35 +02:00
CCampaignScenario CCampaignHandler::readScenarioFromMemory( CBinaryReader & reader, const CCampaignHeader & header)
2010-02-11 16:12:22 +02:00
{
2023-03-14 19:46:15 +02:00
auto prologEpilogReader = [&](const std::string & identifier) -> CCampaignScenario::SScenarioPrologEpilog
2010-02-11 16:12:22 +02:00
{
CCampaignScenario::SScenarioPrologEpilog ret;
ret.hasPrologEpilog = reader.readUInt8();
if(ret.hasPrologEpilog)
2010-02-11 16:12:22 +02:00
{
ret.prologVideo = CCampaignHandler::prologVideoName(reader.readUInt8());
ret.prologMusic = CCampaignHandler::prologMusicName(reader.readUInt8());
2023-04-17 02:26:35 +02:00
ret.prologText = readLocalizedString(reader, header.filename, header.modName, header.encoding, identifier);
2010-02-11 16:12:22 +02:00
}
return ret;
2010-02-11 16:12:22 +02:00
};
2010-02-11 16:12:22 +02:00
CCampaignScenario ret;
ret.conquered = false;
2023-03-14 19:46:15 +02:00
ret.mapName = reader.readBaseString();
2023-04-17 21:19:56 +02:00
reader.readUInt32(); //packedMapSize - not used
if(header.numberOfScenarios > 8) //unholy alliance
{
ret.loadPreconditionRegions(reader.readUInt16());
}
else
{
ret.loadPreconditionRegions(reader.readUInt8());
}
ret.regionColor = reader.readUInt8();
ret.difficulty = reader.readUInt8();
2023-04-17 02:26:35 +02:00
ret.regionText = readLocalizedString(reader, header.filename, header.modName, header.encoding, ret.mapName + ".region");
2023-03-14 19:46:15 +02:00
ret.prolog = prologEpilogReader(ret.mapName + ".prolog");
ret.epilog = prologEpilogReader(ret.mapName + ".epilog");
2010-02-11 16:12:22 +02:00
2023-04-17 02:26:35 +02:00
ret.travelOptions = readScenarioTravelFromMemory(reader, header.version);
2010-02-11 16:12:22 +02:00
return ret;
}
void CCampaignScenario::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(i);
}
}
CScenarioTravel CCampaignHandler::readScenarioTravelFromMemory(CBinaryReader & reader, int version )
2010-02-11 16:12:22 +02:00
{
CScenarioTravel ret;
2023-04-17 21:19:56 +02:00
ui8 whatHeroKeeps = reader.readUInt8();
ret.whatHeroKeeps.experience = whatHeroKeeps & 1;
ret.whatHeroKeeps.primarySkills = whatHeroKeeps & 2;
ret.whatHeroKeeps.secondarySkills = whatHeroKeeps & 4;
ret.whatHeroKeeps.spells = whatHeroKeeps & 8;
ret.whatHeroKeeps.artifacts = whatHeroKeeps & 16;
2023-04-19 01:11:13 +02:00
//TODO: replace with template lambda form C++20 and make typed containers
auto bitMaskToId = [&reader](std::set<int> & container, int size)
{
2023-04-17 21:19:56 +02:00
for(int iId = 0, byte = 0; iId < size * 8; ++iId)
{
if(iId % 8 == 0)
byte = reader.readUInt8();
if(byte & (1 << iId % 8))
2023-04-19 01:11:13 +02:00
container.insert(iId);
2023-04-17 21:19:56 +02:00
}
};
bitMaskToId(ret.monstersKeptByHero, 19);
2023-04-19 01:11:13 +02:00
bitMaskToId(ret.artifactsKeptByHero, version < CampaignVersion::SoD ? 17 : 18);
2010-02-11 16:12:22 +02:00
ret.startOptions = reader.readUInt8();
2010-02-11 16:12:22 +02:00
switch(ret.startOptions)
{
case 0:
//no bonuses. Seems to be OK
break;
2010-02-11 16:12:22 +02:00
case 1: //reading of bonuses player can choose
{
ret.playerColor = reader.readUInt8();
ui8 numOfBonuses = reader.readUInt8();
2010-02-11 16:12:22 +02:00
for (int g=0; g<numOfBonuses; ++g)
{
CScenarioTravel::STravelBonus bonus;
bonus.type = static_cast<CScenarioTravel::STravelBonus::EBonusType>(reader.readUInt8());
//hero: FFFD means 'most powerful' and FFFE means 'generated'
2010-02-11 16:12:22 +02:00
switch(bonus.type)
{
2012-09-22 18:16:45 +03:00
case CScenarioTravel::STravelBonus::SPELL:
2010-02-11 16:12:22 +02:00
{
bonus.info1 = reader.readUInt16(); //hero
bonus.info2 = reader.readUInt8(); //spell ID
2010-02-11 16:12:22 +02:00
break;
}
2012-09-22 18:16:45 +03:00
case CScenarioTravel::STravelBonus::MONSTER:
2010-02-11 16:12:22 +02:00
{
bonus.info1 = reader.readUInt16(); //hero
bonus.info2 = reader.readUInt16(); //monster type
bonus.info3 = reader.readUInt16(); //monster count
2010-02-11 16:12:22 +02:00
break;
}
2012-09-22 18:16:45 +03:00
case CScenarioTravel::STravelBonus::BUILDING:
2010-02-11 16:12:22 +02:00
{
bonus.info1 = reader.readUInt8(); //building ID (0 - town hall, 1 - city hall, 2 - capitol, etc)
2010-02-11 16:12:22 +02:00
break;
}
2012-09-22 18:16:45 +03:00
case CScenarioTravel::STravelBonus::ARTIFACT:
2010-02-11 16:12:22 +02:00
{
bonus.info1 = reader.readUInt16(); //hero
bonus.info2 = reader.readUInt16(); //artifact ID
2010-02-11 16:12:22 +02:00
break;
}
2012-09-22 18:16:45 +03:00
case CScenarioTravel::STravelBonus::SPELL_SCROLL:
2010-02-11 16:12:22 +02:00
{
bonus.info1 = reader.readUInt16(); //hero
bonus.info2 = reader.readUInt8(); //spell ID
2010-02-11 16:12:22 +02:00
break;
}
2012-09-22 18:16:45 +03:00
case CScenarioTravel::STravelBonus::PRIMARY_SKILL:
2010-02-11 16:12:22 +02:00
{
bonus.info1 = reader.readUInt16(); //hero
bonus.info2 = reader.readUInt32(); //bonuses (4 bytes for 4 skills)
2010-02-11 16:12:22 +02:00
break;
}
2012-09-22 18:16:45 +03:00
case CScenarioTravel::STravelBonus::SECONDARY_SKILL:
2010-02-11 16:12:22 +02:00
{
bonus.info1 = reader.readUInt16(); //hero
bonus.info2 = reader.readUInt8(); //skill ID
bonus.info3 = reader.readUInt8(); //skill level
2010-02-11 16:12:22 +02:00
break;
}
2012-09-22 18:16:45 +03:00
case CScenarioTravel::STravelBonus::RESOURCE:
2010-02-11 16:12:22 +02:00
{
bonus.info1 = reader.readUInt8(); //type
2010-02-11 16:12:22 +02:00
//FD - wood+ore
//FE - mercury+sulfur+crystal+gem
bonus.info2 = reader.readUInt32(); //count
2010-02-11 16:12:22 +02:00
break;
}
default:
2017-08-10 18:39:27 +02:00
logGlobal->warn("Corrupted h3c file");
break;
2010-02-11 16:12:22 +02:00
}
ret.bonusesToChoose.push_back(bonus);
}
break;
}
case 2: //reading of players (colors / scenarios ?) player can choose
{
ui8 numOfBonuses = reader.readUInt8();
2010-02-11 16:12:22 +02:00
for (int g=0; g<numOfBonuses; ++g)
{
CScenarioTravel::STravelBonus bonus;
bonus.type = CScenarioTravel::STravelBonus::HEROES_FROM_PREVIOUS_SCENARIO;
bonus.info1 = reader.readUInt8(); //player color
bonus.info2 = reader.readUInt8(); //from what scenario
2010-02-11 16:12:22 +02:00
ret.bonusesToChoose.push_back(bonus);
}
break;
}
case 3: //heroes player can choose between
{
ui8 numOfBonuses = reader.readUInt8();
2010-02-11 16:12:22 +02:00
for (int g=0; g<numOfBonuses; ++g)
{
CScenarioTravel::STravelBonus bonus;
2012-09-22 18:16:45 +03:00
bonus.type = CScenarioTravel::STravelBonus::HERO;
bonus.info1 = reader.readUInt8(); //player color
bonus.info2 = reader.readUInt16(); //hero, FF FF - random
2010-02-11 16:12:22 +02:00
ret.bonusesToChoose.push_back(bonus);
}
break;
}
default:
{
2017-08-10 18:39:27 +02:00
logGlobal->warn("Corrupted h3c file");
break;
2010-02-11 16:12:22 +02:00
}
}
2010-02-08 16:39:19 +02:00
return ret;
}
std::vector< std::vector<ui8> > CCampaignHandler::getFile(std::unique_ptr<CInputStream> file, bool headerOnly)
{
CCompressedStream stream(std::move(file), true);
std::vector< std::vector<ui8> > ret;
do
{
std::vector<ui8> block(stream.getSize());
stream.read(block.data(), block.size());
ret.push_back(block);
}
while (!headerOnly && stream.getNextBlock());
return ret;
}
bool CCampaign::conquerable( int whichScenario ) const
{
//check for void scenraio
if (!scenarios[whichScenario].isNotVoid())
{
return false;
}
if (scenarios[whichScenario].conquered)
{
return false;
}
//check preconditioned regions
for (int g=0; g<scenarios.size(); ++g)
{
if( vstd::contains(scenarios[whichScenario].preconditionRegions, g) && !scenarios[g].conquered)
return false; //prerequisite does not met
2016-11-25 15:40:11 +02:00
}
return true;
}
2010-05-08 21:56:38 +03:00
bool CCampaignScenario::isNotVoid() const
{
2023-02-11 18:30:06 +02:00
return !mapName.empty();
}
2023-02-11 18:30:06 +02:00
const CGHeroInstance * CCampaignScenario::strongestHero(const PlayerColor & owner)
{
std::function<bool(JsonNode & node)> isOwned = [owner](JsonNode & node)
{
2023-02-11 18:30:06 +02:00
auto * h = CCampaignState::crossoverDeserialize(node);
bool result = h->tempOwner == owner;
vstd::clear_pointer(h);
return result;
};
auto ownedHeroes = crossoverHeroes | boost::adaptors::filtered(isOwned);
auto i = vstd::maxElementByFun(ownedHeroes, [](JsonNode & node)
{
2023-02-11 18:30:06 +02:00
auto * h = CCampaignState::crossoverDeserialize(node);
double result = h->getHeroStrength();
vstd::clear_pointer(h);
return result;
});
return i == ownedHeroes.end() ? nullptr : CCampaignState::crossoverDeserialize(*i);
}
std::vector<CGHeroInstance *> CCampaignScenario::getLostCrossoverHeroes()
{
std::vector<CGHeroInstance *> lostCrossoverHeroes;
if(conquered)
{
for(auto node2 : placedCrossoverHeroes)
{
2023-02-11 18:30:06 +02:00
auto * hero = CCampaignState::crossoverDeserialize(node2);
auto it = range::find_if(crossoverHeroes, [hero](JsonNode node)
{
2023-02-11 18:30:06 +02:00
auto * h = CCampaignState::crossoverDeserialize(node);
bool result = hero->subID == h->subID;
vstd::clear_pointer(h);
return result;
});
if(it == crossoverHeroes.end())
{
lostCrossoverHeroes.push_back(hero);
}
}
}
2015-12-04 01:06:02 +02:00
return lostCrossoverHeroes;
}
void CCampaignState::setCurrentMapAsConquered(const std::vector<CGHeroInstance *> & heroes)
2010-08-20 16:34:39 +03:00
{
camp->scenarios[*currentMap].crossoverHeroes.clear();
for(CGHeroInstance * hero : heroes)
{
camp->scenarios[*currentMap].crossoverHeroes.push_back(crossoverSerialize(hero));
}
mapsConquered.push_back(*currentMap);
mapsRemaining -= *currentMap;
camp->scenarios[*currentMap].conquered = true;
}
2023-04-16 19:42:56 +02:00
std::optional<CScenarioTravel::STravelBonus> CCampaignState::getBonusForCurrentMap() const
{
auto bonuses = getCurrentScenario().travelOptions.bonusesToChoose;
assert(chosenCampaignBonuses.count(*currentMap) || bonuses.size() == 0);
2013-12-18 21:18:12 +03:00
if(bonuses.empty())
2023-04-16 19:42:56 +02:00
return std::optional<CScenarioTravel::STravelBonus>();
else
return bonuses[currentBonusID()];
}
const CCampaignScenario & CCampaignState::getCurrentScenario() const
{
return camp->scenarios[*currentMap];
}
CCampaignScenario & CCampaignState::getCurrentScenario()
{
return camp->scenarios[*currentMap];
}
ui8 CCampaignState::currentBonusID() const
{
return chosenCampaignBonuses.at(*currentMap);
}
CCampaignState::CCampaignState( std::unique_ptr<CCampaign> _camp ) : camp(std::move(_camp))
2012-09-22 18:16:45 +03:00
{
for(int i = 0; i < camp->scenarios.size(); i++)
{
if(vstd::contains(camp->mapPieces, i)) //not all maps must be present in a campaign
mapsRemaining.push_back(i);
2012-09-22 18:16:45 +03:00
}
}
CMap * CCampaignState::getMap(int scenarioId) const
{
// FIXME: there is certainly better way to handle maps inside campaigns
if(scenarioId == -1)
2023-04-16 19:42:56 +02:00
scenarioId = currentMap.value();
2023-04-09 01:24:40 +02:00
2023-04-20 17:46:27 +02:00
CMapService mapService;
std::string scenarioName = camp->header.filename.substr(0, camp->header.filename.find('.'));
boost::to_lower(scenarioName);
2023-02-11 18:30:06 +02:00
scenarioName += ':' + std::to_string(scenarioId);
std::string & mapContent = camp->mapPieces.find(scenarioId)->second;
2023-02-11 18:30:06 +02:00
const auto * buffer = reinterpret_cast<const ui8 *>(mapContent.data());
2023-02-25 17:44:15 +02:00
return mapService.loadMap(buffer, static_cast<int>(mapContent.size()), scenarioName, camp->header.modName, camp->header.encoding).release();
}
std::unique_ptr<CMapHeader> CCampaignState::getHeader(int scenarioId) const
{
if(scenarioId == -1)
2023-04-16 19:42:56 +02:00
scenarioId = currentMap.value();
2023-04-09 01:24:40 +02:00
CMapService mapService;
std::string scenarioName = camp->header.filename.substr(0, camp->header.filename.find('.'));
boost::to_lower(scenarioName);
2023-02-11 18:30:06 +02:00
scenarioName += ':' + std::to_string(scenarioId);
std::string & mapContent = camp->mapPieces.find(scenarioId)->second;
2023-02-11 18:30:06 +02:00
const auto * buffer = reinterpret_cast<const ui8 *>(mapContent.data());
2023-02-25 17:44:15 +02:00
return mapService.loadMapHeader(buffer, static_cast<int>(mapContent.size()), scenarioName, camp->header.modName, camp->header.encoding);
}
std::shared_ptr<CMapInfo> CCampaignState::getMapInfo(int scenarioId) const
{
if(scenarioId == -1)
2023-04-16 19:42:56 +02:00
scenarioId = currentMap.value();
auto mapInfo = std::make_shared<CMapInfo>();
mapInfo->fileURI = camp->header.filename;
mapInfo->mapHeader = getHeader(scenarioId);
mapInfo->countPlayers();
return mapInfo;
}
JsonNode CCampaignState::crossoverSerialize(CGHeroInstance * hero)
{
JsonNode node;
JsonSerializer handler(nullptr, node);
hero->serializeJsonOptions(handler);
return node;
}
CGHeroInstance * CCampaignState::crossoverDeserialize(JsonNode & node)
{
JsonDeserializer handler(nullptr, node);
2023-02-11 18:30:06 +02:00
auto * hero = new CGHeroInstance();
hero->ID = Obj::HERO;
hero->serializeJsonOptions(handler);
return hero;
}
std::string CCampaignHandler::prologVideoName(ui8 index)
{
JsonNode config(ResourceID(std::string("CONFIG/campaignMedia"), EResType::TEXT));
auto vids = config["videos"].Vector();
if(index < vids.size())
return vids[index].String();
2013-02-21 00:01:18 +03:00
return "";
}
std::string CCampaignHandler::prologMusicName(ui8 index)
{
std::vector<std::string> music;
2023-02-11 18:30:06 +02:00
return VLC->generaltexth->translate("core.cmpmusic." + std::to_string(static_cast<int>(index)));
}
std::string CCampaignHandler::prologVoiceName(ui8 index)
{
JsonNode config(ResourceID(std::string("CONFIG/campaignMedia"), EResType::TEXT));
auto audio = config["voice"].Vector();
if(index < audio.size())
return audio[index].String();
2013-02-21 00:01:18 +03:00
return "";
}
VCMI_LIB_NAMESPACE_END