|
@@ -24,6 +24,7 @@
|
|
|
#include "../mapObjects/CGHeroInstance.h"//for hero crossover
|
|
|
#include "../CHeroHandler.h"
|
|
|
#include "../Languages.h"
|
|
|
+#include "../StringConstants.h"
|
|
|
#include "CMapService.h"
|
|
|
#include "CMap.h"
|
|
|
#include "CMapInfo.h"
|
|
@@ -35,27 +36,73 @@
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
|
+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);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
bool CScenarioTravel::STravelBonus::isBonusForHero() const
|
|
|
{
|
|
|
return type == SPELL || type == MONSTER || type == ARTIFACT || type == SPELL_SCROLL || type == PRIMARY_SKILL
|
|
|
|| type == SECONDARY_SKILL;
|
|
|
}
|
|
|
|
|
|
+void CCampaignHeader::loadLegacyData(ui8 campId)
|
|
|
+{
|
|
|
+ campaignRegions = CampaignRegions::getLegacy(campId);
|
|
|
+ numberOfScenarios = VLC->generaltexth->getCampaignLength(campId);
|
|
|
+}
|
|
|
+
|
|
|
CCampaignHeader CCampaignHandler::getHeader( const std::string & name)
|
|
|
{
|
|
|
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;
|
|
|
+
|
|
|
auto fileStream = CResourceHandler::get(modName)->load(resourceID);
|
|
|
-
|
|
|
std::vector<ui8> cmpgn = getFile(std::move(fileStream), true)[0];
|
|
|
-
|
|
|
- CMemoryStream stream(cmpgn.data(), cmpgn.size());
|
|
|
- CBinaryReader reader(&stream);
|
|
|
- CCampaignHeader ret = readHeaderFromMemory(reader, resourceID.getName(), modName, encoding);
|
|
|
-
|
|
|
- return ret;
|
|
|
+ JsonNode jsonCampaign((const char*)cmpgn.data(), cmpgn.size());
|
|
|
+ if(jsonCampaign.isNull())
|
|
|
+ {
|
|
|
+ //legacy OH3 campaign (*.h3c)
|
|
|
+ CMemoryStream stream(cmpgn.data(), cmpgn.size());
|
|
|
+ CBinaryReader reader(&stream);
|
|
|
+ return readHeaderFromMemory(reader, resourceID.getName(), modName, encoding);
|
|
|
+ }
|
|
|
+
|
|
|
+ //VCMI (*.vcmp)
|
|
|
+ return readHeaderFromJson(jsonCampaign, resourceID.getName(), modName, encoding);
|
|
|
}
|
|
|
|
|
|
std::unique_ptr<CCampaign> CCampaignHandler::getCampaign( const std::string & name )
|
|
@@ -64,39 +111,42 @@ std::unique_ptr<CCampaign> CCampaignHandler::getCampaign( const std::string & na
|
|
|
std::string modName = VLC->modh->findResourceOrigin(resourceID);
|
|
|
std::string language = VLC->modh->getModLanguage(modName);
|
|
|
std::string encoding = Languages::getLanguageOptions(language).encoding;
|
|
|
- auto fileStream = CResourceHandler::get(modName)->load(resourceID);
|
|
|
-
|
|
|
+
|
|
|
auto ret = std::make_unique<CCampaign>();
|
|
|
+
|
|
|
+ auto fileStream = CResourceHandler::get(modName)->load(resourceID);
|
|
|
|
|
|
- std::vector<std::vector<ui8>> file = getFile(std::move(fileStream), false);
|
|
|
-
|
|
|
- CMemoryStream stream(file[0].data(), file[0].size());
|
|
|
- CBinaryReader reader(&stream);
|
|
|
- ret->header = readHeaderFromMemory(reader, resourceID.getName(), modName, encoding);
|
|
|
-
|
|
|
- int howManyScenarios = static_cast<int>(VLC->generaltexth->getCampaignLength(ret->header.mapVersion));
|
|
|
- for(int g=0; g<howManyScenarios; ++g)
|
|
|
+ std::vector<std::vector<ui8>> files = getFile(std::move(fileStream), false);
|
|
|
+
|
|
|
+ JsonNode jsonCampaign((const char*)files[0].data(), files[0].size());
|
|
|
+ if(jsonCampaign.isNull())
|
|
|
{
|
|
|
- CCampaignScenario sc = readScenarioFromMemory(reader, resourceID.getName(), modName, encoding, ret->header.version, ret->header.mapVersion);
|
|
|
- ret->scenarios.push_back(sc);
|
|
|
+ CMemoryStream stream(files[0].data(), files[0].size());
|
|
|
+ CBinaryReader reader(&stream);
|
|
|
+ ret->header = readHeaderFromMemory(reader, resourceID.getName(), modName, encoding);
|
|
|
+
|
|
|
+ for(int g = 0; g < ret->header.numberOfScenarios; ++g)
|
|
|
+ ret->scenarios.emplace_back(readScenarioFromMemory(reader, ret->header));
|
|
|
}
|
|
|
-
|
|
|
- int scenarioID = 0;
|
|
|
-
|
|
|
+ else
|
|
|
+ {
|
|
|
+ ret->header = readHeaderFromJson(jsonCampaign, resourceID.getName(), modName, encoding);
|
|
|
+ for(auto & scenario : jsonCampaign["scenarios"].Vector())
|
|
|
+ ret->scenarios.emplace_back(readScenarioFromJson(scenario));
|
|
|
+ }
|
|
|
+
|
|
|
//first entry is campaign header. start loop from 1
|
|
|
- for (int g=1; g<file.size() && scenarioID<howManyScenarios; ++g)
|
|
|
+ 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* >(file[g].data()), file[g].size());
|
|
|
+ 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()),
|
|
@@ -151,26 +201,273 @@ std::string CCampaignHandler::readLocalizedString(CBinaryReader & reader, std::s
|
|
|
return VLC->generaltexth->translate(stringID.get());
|
|
|
}
|
|
|
|
|
|
+CCampaignHeader CCampaignHandler::readHeaderFromJson(JsonNode & reader, std::string filename, std::string modName, std::string encoding)
|
|
|
+{
|
|
|
+ CCampaignHeader ret;
|
|
|
+
|
|
|
+ ret.version = reader["version"].Integer();
|
|
|
+ if(ret.version < CampaignVersion::VCMI_MIN || ret.version > CampaignVersion::VCMI_MAX)
|
|
|
+ {
|
|
|
+ logGlobal->info("VCMP Loading: Unsupported campaign %s version %d", filename, ret.version);
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ ret.version = CampaignVersion::VCMI;
|
|
|
+ ret.campaignRegions = CampaignRegions::fromJson(reader["regions"]);
|
|
|
+ ret.numberOfScenarios = reader["scenarios"].Vector().size();
|
|
|
+ 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;
|
|
|
+}
|
|
|
+
|
|
|
+CCampaignScenario CCampaignHandler::readScenarioFromJson(JsonNode & reader)
|
|
|
+{
|
|
|
+ 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();
|
|
|
+ ret.prologText = identifier["text"].String();
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+ };
|
|
|
+
|
|
|
+ CCampaignScenario ret;
|
|
|
+ ret.conquered = false;
|
|
|
+ ret.mapName = reader["map"].String();
|
|
|
+ for(auto & g : reader["preconditions"].Vector())
|
|
|
+ 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"]);
|
|
|
+
|
|
|
+ ret.travelOptions = readScenarioTravelFromJson(reader);
|
|
|
+
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
+CScenarioTravel CCampaignHandler::readScenarioTravelFromJson(JsonNode & reader)
|
|
|
+{
|
|
|
+ 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},
|
|
|
+ {"primarySkill", CScenarioTravel::STravelBonus::EBonusType::PRIMARY_SKILL},
|
|
|
+ {"secondarySkill", CScenarioTravel::STravelBonus::EBonusType::SECONDARY_SKILL},
|
|
|
+ {"resource", CScenarioTravel::STravelBonus::EBonusType::RESOURCE},
|
|
|
+ //{"prevHero", CScenarioTravel::STravelBonus::EBonusType::HEROES_FROM_PREVIOUS_SCENARIO},
|
|
|
+ //{"hero", CScenarioTravel::STravelBonus::EBonusType::HERO},
|
|
|
+ };
|
|
|
+
|
|
|
+ std::map<std::string, ui32> primarySkillsMap = {
|
|
|
+ {"attack", 0},
|
|
|
+ {"defence", 8},
|
|
|
+ {"spellpower", 16},
|
|
|
+ {"knowledge", 24},
|
|
|
+ };
|
|
|
+
|
|
|
+ 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},
|
|
|
+ {"mercury", 1},
|
|
|
+ {"ore", 2},
|
|
|
+ {"sulfur", 3},
|
|
|
+ {"crystal", 4},
|
|
|
+ {"gems", 5},
|
|
|
+ {"gold", 6},
|
|
|
+ {"common", 0xFD},
|
|
|
+ {"rare", 0xFE}
|
|
|
+ };
|
|
|
+
|
|
|
+ for(auto & k : reader["heroKeeps"].Vector())
|
|
|
+ {
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ for(auto & k : reader["keepCreatures"].Vector())
|
|
|
+ {
|
|
|
+ if(auto identifier = VLC->modh->identifiers.getIdentifier(CModHandler::scopeMap(), "creature", k.String()))
|
|
|
+ ret.monstersKeptByHero.insert(CreatureID(identifier.value()));
|
|
|
+ else
|
|
|
+ logGlobal->warn("VCMP Loading: keepCreatures contains unresolved identifier %s", k.String());
|
|
|
+ }
|
|
|
+ for(auto & k : reader["keepArtifacts"].Vector())
|
|
|
+ {
|
|
|
+ if(auto identifier = VLC->modh->identifiers.getIdentifier(CModHandler::scopeMap(), "artifact", k.String()))
|
|
|
+ ret.artifactsKeptByHero.insert(ArtifactID(identifier.value()));
|
|
|
+ else
|
|
|
+ logGlobal->warn("VCMP Loading: keepArtifacts contains unresolved identifier %s", k.String());
|
|
|
+ }
|
|
|
+
|
|
|
+ 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()];
|
|
|
+ switch (bonus.type)
|
|
|
+ {
|
|
|
+ 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()))
|
|
|
+ bonus.info1 = identifier.value();
|
|
|
+ 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()))
|
|
|
+ bonus.info2 = identifier.value();
|
|
|
+ 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()))
|
|
|
+ bonus.info2 = Identifier.value();
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ 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
|
|
|
+ if (auto identifier = VLC->modh->identifiers.getIdentifier(CModHandler::scopeMap(), "hero", bjson["hero"].String()))
|
|
|
+ bonus.info2 = identifier.value();
|
|
|
+ else
|
|
|
+ logGlobal->warn("VCMP Loading: unresolved hero identifier %s", bjson["hero"].String());
|
|
|
+
|
|
|
+ ret.bonusesToChoose.push_back(bonus);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ default:
|
|
|
+ {
|
|
|
+ logGlobal->warn("VCMP Loading: Unsupported start options value");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
CCampaignHeader CCampaignHandler::readHeaderFromMemory( CBinaryReader & reader, std::string filename, std::string modName, std::string encoding )
|
|
|
{
|
|
|
CCampaignHeader ret;
|
|
|
|
|
|
ret.version = reader.readUInt32();
|
|
|
- ret.mapVersion = reader.readUInt8() - 1;//change range of it from [1, 20] to [0, 19]
|
|
|
+ ui8 campId = reader.readUInt8() - 1;//change range of it from [1, 20] to [0, 19]
|
|
|
+ ret.loadLegacyData(campId);
|
|
|
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
|
|
|
- ret.difficultyChoosenByPlayer = 0;
|
|
|
- ret.music = reader.readInt8();
|
|
|
+ ret.difficultyChoosenByPlayer = false;
|
|
|
+ reader.readInt8(); //music - skip as unused
|
|
|
ret.filename = filename;
|
|
|
ret.modName = modName;
|
|
|
ret.encoding = encoding;
|
|
|
+ ret.valid = true;
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
-CCampaignScenario CCampaignHandler::readScenarioFromMemory( CBinaryReader & reader, std::string filename, std::string modName, std::string encoding, int version, int mapVersion )
|
|
|
+CCampaignScenario CCampaignHandler::readScenarioFromMemory( CBinaryReader & reader, const CCampaignHeader & header)
|
|
|
{
|
|
|
auto prologEpilogReader = [&](const std::string & identifier) -> CCampaignScenario::SScenarioPrologEpilog
|
|
|
{
|
|
@@ -178,9 +475,9 @@ CCampaignScenario CCampaignHandler::readScenarioFromMemory( CBinaryReader & read
|
|
|
ret.hasPrologEpilog = reader.readUInt8();
|
|
|
if(ret.hasPrologEpilog)
|
|
|
{
|
|
|
- ret.prologVideo = reader.readUInt8();
|
|
|
- ret.prologMusic = reader.readUInt8();
|
|
|
- ret.prologText = readLocalizedString(reader, filename, modName, encoding, identifier);
|
|
|
+ ret.prologVideo = CCampaignHandler::prologVideoName(reader.readUInt8());
|
|
|
+ ret.prologMusic = CCampaignHandler::prologMusicName(reader.readUInt8());
|
|
|
+ ret.prologText = readLocalizedString(reader, header.filename, header.modName, header.encoding, identifier);
|
|
|
}
|
|
|
return ret;
|
|
|
};
|
|
@@ -188,8 +485,8 @@ CCampaignScenario CCampaignHandler::readScenarioFromMemory( CBinaryReader & read
|
|
|
CCampaignScenario ret;
|
|
|
ret.conquered = false;
|
|
|
ret.mapName = reader.readBaseString();
|
|
|
- ret.packedMapSize = reader.readUInt32();
|
|
|
- if(mapVersion == 18)//unholy alliance
|
|
|
+ reader.readUInt32(); //packedMapSize - not used
|
|
|
+ if(header.numberOfScenarios > 8) //unholy alliance
|
|
|
{
|
|
|
ret.loadPreconditionRegions(reader.readUInt16());
|
|
|
}
|
|
@@ -199,11 +496,11 @@ CCampaignScenario CCampaignHandler::readScenarioFromMemory( CBinaryReader & read
|
|
|
}
|
|
|
ret.regionColor = reader.readUInt8();
|
|
|
ret.difficulty = reader.readUInt8();
|
|
|
- ret.regionText = readLocalizedString(reader, filename, modName, encoding, ret.mapName + ".region");
|
|
|
+ ret.regionText = readLocalizedString(reader, header.filename, header.modName, header.encoding, ret.mapName + ".region");
|
|
|
ret.prolog = prologEpilogReader(ret.mapName + ".prolog");
|
|
|
ret.epilog = prologEpilogReader(ret.mapName + ".epilog");
|
|
|
|
|
|
- ret.travelOptions = readScenarioTravelFromMemory(reader, version);
|
|
|
+ ret.travelOptions = readScenarioTravelFromMemory(reader, header.version);
|
|
|
|
|
|
return ret;
|
|
|
}
|
|
@@ -221,18 +518,27 @@ CScenarioTravel CCampaignHandler::readScenarioTravelFromMemory(CBinaryReader & r
|
|
|
{
|
|
|
CScenarioTravel ret;
|
|
|
|
|
|
- ret.whatHeroKeeps = reader.readUInt8();
|
|
|
- reader.getStream()->read(ret.monstersKeptByHero.data(), ret.monstersKeptByHero.size());
|
|
|
-
|
|
|
- if (version < CampaignVersion::SoD)
|
|
|
+ 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;
|
|
|
+
|
|
|
+ //TODO: replace with template lambda form C++20 and make typed containers
|
|
|
+ auto bitMaskToId = [&reader](std::set<int> & container, int size)
|
|
|
{
|
|
|
- ret.artifsKeptByHero.fill(0);
|
|
|
- reader.getStream()->read(ret.artifsKeptByHero.data(), ret.artifsKeptByHero.size() - 1);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- reader.getStream()->read(ret.artifsKeptByHero.data(), ret.artifsKeptByHero.size());
|
|
|
- }
|
|
|
+ for(int iId = 0, byte = 0; iId < size * 8; ++iId)
|
|
|
+ {
|
|
|
+ if(iId % 8 == 0)
|
|
|
+ byte = reader.readUInt8();
|
|
|
+ if(byte & (1 << iId % 8))
|
|
|
+ container.insert(iId);
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ bitMaskToId(ret.monstersKeptByHero, 19);
|
|
|
+ bitMaskToId(ret.artifactsKeptByHero, version < CampaignVersion::SoD ? 17 : 18);
|
|
|
|
|
|
ret.startOptions = reader.readUInt8();
|
|
|
|
|
@@ -490,12 +796,13 @@ CMap * CCampaignState::getMap(int scenarioId) const
|
|
|
// FIXME: there is certainly better way to handle maps inside campaigns
|
|
|
if(scenarioId == -1)
|
|
|
scenarioId = currentMap.value();
|
|
|
+
|
|
|
+ CMapService mapService;
|
|
|
std::string scenarioName = camp->header.filename.substr(0, camp->header.filename.find('.'));
|
|
|
boost::to_lower(scenarioName);
|
|
|
scenarioName += ':' + std::to_string(scenarioId);
|
|
|
std::string & mapContent = camp->mapPieces.find(scenarioId)->second;
|
|
|
const auto * buffer = reinterpret_cast<const ui8 *>(mapContent.data());
|
|
|
- CMapService mapService;
|
|
|
return mapService.loadMap(buffer, static_cast<int>(mapContent.size()), scenarioName, camp->header.modName, camp->header.encoding).release();
|
|
|
}
|
|
|
|
|
@@ -503,13 +810,13 @@ std::unique_ptr<CMapHeader> CCampaignState::getHeader(int scenarioId) const
|
|
|
{
|
|
|
if(scenarioId == -1)
|
|
|
scenarioId = currentMap.value();
|
|
|
-
|
|
|
+
|
|
|
+ CMapService mapService;
|
|
|
std::string scenarioName = camp->header.filename.substr(0, camp->header.filename.find('.'));
|
|
|
boost::to_lower(scenarioName);
|
|
|
scenarioName += ':' + std::to_string(scenarioId);
|
|
|
std::string & mapContent = camp->mapPieces.find(scenarioId)->second;
|
|
|
const auto * buffer = reinterpret_cast<const ui8 *>(mapContent.data());
|
|
|
- CMapService mapService;
|
|
|
return mapService.loadMapHeader(buffer, static_cast<int>(mapContent.size()), scenarioName, camp->header.modName, camp->header.encoding);
|
|
|
}
|
|
|
|