2017-07-13 10:26:03 +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
|
|
|
|
*
|
|
|
|
*/
|
2011-12-14 00:23:17 +03:00
|
|
|
#include "StdInc.h"
|
2010-02-08 16:39:19 +02:00
|
|
|
#include "CCampaignHandler.h"
|
2011-12-14 00:23:17 +03:00
|
|
|
|
2013-07-28 17:49:50 +03:00
|
|
|
#include "../filesystem/Filesystem.h"
|
2013-04-07 13:48:07 +03:00
|
|
|
#include "../filesystem/CCompressedStream.h"
|
2014-02-15 01:46:06 +03:00
|
|
|
#include "../filesystem/CMemoryStream.h"
|
|
|
|
#include "../filesystem/CBinaryReader.h"
|
2012-11-03 16:30:47 +03:00
|
|
|
#include "../VCMI_Lib.h"
|
|
|
|
#include "../vcmi_endian.h"
|
|
|
|
#include "../CGeneralTextHandler.h"
|
2023-02-24 22:38:12 +02:00
|
|
|
#include "../TextOperations.h"
|
2012-11-03 16:30:47 +03:00
|
|
|
#include "../StartInfo.h"
|
2023-02-24 22:38:12 +02:00
|
|
|
#include "../CModHandler.h"
|
2012-11-03 16:30:47 +03:00
|
|
|
#include "../CArtHandler.h" //for hero crossover
|
2014-06-05 19:52:14 +03:00
|
|
|
#include "../mapObjects/CGHeroInstance.h"//for hero crossover
|
2012-11-03 16:30:47 +03:00
|
|
|
#include "../CHeroHandler.h"
|
2023-02-24 22:38:12 +02:00
|
|
|
#include "../Languages.h"
|
2013-02-09 22:19:14 +03:00
|
|
|
#include "CMapService.h"
|
|
|
|
#include "CMap.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "CMapInfo.h"
|
2010-02-08 16:39:19 +02:00
|
|
|
|
2018-01-05 19:21:07 +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
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-08-01 15:02:54 +03:00
|
|
|
CCampaignHeader CCampaignHandler::getHeader( const std::string & name)
|
2010-02-08 16:39:19 +02:00
|
|
|
{
|
2023-02-24 22:38:12 +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
|
|
|
|
|
|
|
JsonNode jsonCampaign(resourceID);
|
|
|
|
if(jsonCampaign.isNull())
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCampaignHeader ret = readHeaderFromJson(jsonCampaign, resourceID.getName(), modName, encoding);
|
|
|
|
return ret;
|
|
|
|
}
|
2010-02-11 16:12:22 +02:00
|
|
|
}
|
|
|
|
|
2015-12-29 04:43:33 +02:00
|
|
|
std::unique_ptr<CCampaign> CCampaignHandler::getCampaign( const std::string & name )
|
2010-02-11 16:12:22 +02:00
|
|
|
{
|
2023-02-24 22:38:12 +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
|
|
|
|
|
|
|
JsonNode jsonCampaign(resourceID);
|
|
|
|
if(jsonCampaign.isNull())
|
|
|
|
{
|
|
|
|
auto fileStream = CResourceHandler::get(modName)->load(resourceID);
|
2010-02-11 16:12:22 +02:00
|
|
|
|
2023-04-09 01:24:40 +02:00
|
|
|
std::vector<std::vector<ui8>> file = getFile(std::move(fileStream), false);
|
2010-02-11 16:12:22 +02:00
|
|
|
|
2023-04-09 01:24:40 +02:00
|
|
|
CMemoryStream stream(file[0].data(), file[0].size());
|
|
|
|
CBinaryReader reader(&stream);
|
|
|
|
ret->header = readHeaderFromMemory(reader, resourceID.getName(), modName, encoding);
|
2010-02-12 17:04:01 +02:00
|
|
|
|
2023-04-09 01:24:40 +02:00
|
|
|
int howManyScenarios = static_cast<int>(VLC->generaltexth->getCampaignLength(ret->header.mapVersion));
|
|
|
|
for(int g=0; g<howManyScenarios; ++g)
|
|
|
|
{
|
|
|
|
CCampaignScenario sc = readScenarioFromMemory(reader, resourceID.getName(), modName, encoding, ret->header.version, ret->header.mapVersion);
|
|
|
|
ret->scenarios.push_back(sc);
|
|
|
|
}
|
|
|
|
|
|
|
|
int scenarioID = 0;
|
|
|
|
|
|
|
|
//first entry is campaign header. start loop from 1
|
|
|
|
for (int g=1; g<file.size() && scenarioID<howManyScenarios; ++g)
|
|
|
|
{
|
|
|
|
while(!ret->scenarios[scenarioID].isNotVoid()) //skip void scenarios
|
|
|
|
{
|
|
|
|
scenarioID++;
|
|
|
|
}
|
2010-02-16 19:28:56 +02:00
|
|
|
|
2023-04-09 01:24:40 +02:00
|
|
|
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());
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2010-02-12 17:04:01 +02:00
|
|
|
{
|
2023-04-09 01:24:40 +02:00
|
|
|
ret->header = readHeaderFromJson(jsonCampaign, resourceID.getName(), modName, encoding);
|
|
|
|
|
|
|
|
for(auto & scenario : jsonCampaign["scenarios"].Vector())
|
2010-07-30 14:29:42 +03:00
|
|
|
{
|
2023-04-09 01:24:40 +02:00
|
|
|
CCampaignScenario sc = readScenarioFromJson(scenario, resourceID.getName(), modName, encoding, ret->header.version, ret->header.mapVersion);
|
|
|
|
if(sc.isNotVoid())
|
|
|
|
{
|
|
|
|
CMapService mapService;
|
|
|
|
auto hdr = mapService.loadMapHeader(ResourceID(sc.mapName, EResType::MAP));
|
|
|
|
sc.scenarioName = hdr->name;
|
|
|
|
}
|
|
|
|
ret->scenarios.push_back(sc);
|
2010-07-30 14:29:42 +03:00
|
|
|
}
|
2010-02-12 17:04:01 +02:00
|
|
|
}
|
2010-02-08 16:39:19 +02:00
|
|
|
|
2014-02-23 16:52:06 +03:00
|
|
|
// handle campaign specific discrepancies
|
2023-03-14 19:46:15 +02:00
|
|
|
if(resourceID.getName() == "DATA/AB")
|
2014-02-23 16:52:06 +03:00
|
|
|
{
|
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
|
2014-02-23 16:52:06 +03:00
|
|
|
}
|
2023-03-14 19:46:15 +02:00
|
|
|
else if(resourceID.getName() == "DATA/FINAL")
|
2014-02-23 16:52:06 +03:00
|
|
|
{
|
|
|
|
// 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
|
2014-02-23 16:52:06 +03:00
|
|
|
}
|
|
|
|
|
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-02-24 16:15:45 +02:00
|
|
|
{
|
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-02-24 16:15:45 +02:00
|
|
|
}
|
|
|
|
|
2023-04-09 01:24:40 +02:00
|
|
|
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("Unsupported campaign %s version %d", filename, ret.version);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.version = CampaignVersion::VCMI;
|
|
|
|
ret.mapVersion = reader["campaignId"].Integer();
|
|
|
|
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, std::string filename, std::string modName, std::string encoding, int version, int mapVersion)
|
|
|
|
{
|
|
|
|
auto prologEpilogReader = [](JsonNode & identifier) -> CCampaignScenario::SScenarioPrologEpilog
|
|
|
|
{
|
|
|
|
CCampaignScenario::SScenarioPrologEpilog ret;
|
|
|
|
ret.hasPrologEpilog = !identifier.isNull();
|
|
|
|
if(ret.hasPrologEpilog)
|
|
|
|
{
|
|
|
|
ret.prologVideo = identifier["video"].Integer();
|
|
|
|
ret.prologMusic = identifier["music"].Integer();
|
|
|
|
ret.prologText = identifier["text"].String();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
CCampaignScenario ret;
|
|
|
|
ret.conquered = false;
|
|
|
|
ret.mapName = reader["map"].String();
|
|
|
|
for(auto & g : reader["precoditions"].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, version);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
CScenarioTravel CCampaignHandler::readScenarioTravelFromJson(JsonNode & reader, int version )
|
|
|
|
{
|
|
|
|
CScenarioTravel ret;
|
|
|
|
|
|
|
|
std::map<std::string, ui8> heroKeepsMap = {
|
|
|
|
{"experience", 1},
|
|
|
|
{"primary", 2},
|
|
|
|
{"skills", 4},
|
|
|
|
{"spells", 8},
|
|
|
|
{"artifacts", 16}
|
|
|
|
};
|
|
|
|
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},
|
|
|
|
{"primary", CScenarioTravel::STravelBonus::EBonusType::PRIMARY_SKILL},
|
|
|
|
{"skill", 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, ui16> heroSpecialMap = {
|
|
|
|
{"strongest", 0xFFFD},
|
|
|
|
{"generated", 0xFFFE},
|
|
|
|
{"random", 0xFFFF}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::map<std::string, ui8> resourceTypeMap = {
|
|
|
|
//FD - wood+ore
|
|
|
|
//FE - mercury+sulfur+crystal+gem
|
|
|
|
{"wood", 0},
|
|
|
|
{"ore", 1},
|
|
|
|
{"mercury", 2},
|
|
|
|
{"sulfur", 3},
|
|
|
|
{"crystal", 4},
|
|
|
|
{"gem", 5},
|
|
|
|
{"gold", 6},
|
|
|
|
{"common", 0xFD},
|
|
|
|
{"rare", 0xFE}
|
|
|
|
};
|
|
|
|
|
|
|
|
for(auto & k : reader["heroKeeps"].Vector())
|
|
|
|
ret.whatHeroKeeps |= heroKeepsMap[k.String()];
|
|
|
|
|
|
|
|
//reader.getStream()->read(ret.monstersKeptByHero.data(), ret.monstersKeptByHero.size());
|
|
|
|
//reader.getStream()->read(ret.artifsKeptByHero.data(), ret.artifsKeptByHero.size());
|
|
|
|
|
|
|
|
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()];
|
|
|
|
if(bonus.type == CScenarioTravel::STravelBonus::EBonusType::RESOURCE)
|
|
|
|
{
|
|
|
|
bonus.info1 = resourceTypeMap[bjson["type"].String()];
|
|
|
|
bonus.info2 = bjson["amount"].Integer();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(int heroId = heroSpecialMap[bjson["hero"].String()])
|
|
|
|
bonus.info1 = heroId;
|
|
|
|
else
|
|
|
|
bonus.info1 = VLC->modh->identifiers.getIdentifier(CModHandler::scopeMap(), "hero", bjson["hero"].String()).get();
|
|
|
|
|
|
|
|
if(bonus.type == CScenarioTravel::STravelBonus::EBonusType::SPELL
|
|
|
|
|| bonus.type == CScenarioTravel::STravelBonus::EBonusType::MONSTER
|
|
|
|
|| bonus.type == CScenarioTravel::STravelBonus::EBonusType::ARTIFACT)
|
|
|
|
bonus.info2 = VLC->modh->identifiers.getIdentifier(CModHandler::scopeMap(), bjson["what"].String(), bjson["type"].String()).get();
|
|
|
|
else
|
|
|
|
bonus.info2 = bjson["type"].Integer();
|
|
|
|
|
|
|
|
bonus.info3 = bjson["amount"].Integer();
|
|
|
|
}
|
|
|
|
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
|
|
|
|
bonus.info2 = VLC->modh->identifiers.getIdentifier(CModHandler::scopeMap(), "hero", bjson["hero"].String()).get();
|
|
|
|
|
|
|
|
ret.bonusesToChoose.push_back(bonus);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
logGlobal->warn("Corrupted h3c file");
|
|
|
|
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;
|
2014-02-15 01:46:06 +03:00
|
|
|
|
|
|
|
ret.version = reader.readUInt32();
|
|
|
|
ret.mapVersion = reader.readUInt8() - 1;//change range of it from [1, 20] to [0, 19]
|
2023-03-14 19:46:15 +02:00
|
|
|
ret.name = readLocalizedString(reader, filename, modName, encoding, "name");
|
|
|
|
ret.description = readLocalizedString(reader, filename, modName, encoding, "description");
|
2011-11-27 16:14:20 +03:00
|
|
|
if (ret.version > CampaignVersion::RoE)
|
2014-02-15 01:46:06 +03:00
|
|
|
ret.difficultyChoosenByPlayer = reader.readInt8();
|
2011-11-27 16:14:20 +03:00
|
|
|
else
|
|
|
|
ret.difficultyChoosenByPlayer = 0;
|
2014-02-15 01:46:06 +03:00
|
|
|
ret.music = reader.readInt8();
|
2023-02-24 22:38:12 +02:00
|
|
|
ret.filename = filename;
|
2023-02-25 17:44:15 +02:00
|
|
|
ret.modName = modName;
|
2023-02-24 22:38:12 +02:00
|
|
|
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-03-14 19:46:15 +02:00
|
|
|
CCampaignScenario CCampaignHandler::readScenarioFromMemory( CBinaryReader & reader, std::string filename, std::string modName, std::string encoding, int version, int mapVersion )
|
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
|
|
|
{
|
2014-02-15 01:46:06 +03:00
|
|
|
CCampaignScenario::SScenarioPrologEpilog ret;
|
|
|
|
ret.hasPrologEpilog = reader.readUInt8();
|
|
|
|
if(ret.hasPrologEpilog)
|
2010-02-11 16:12:22 +02:00
|
|
|
{
|
2014-02-15 01:46:06 +03:00
|
|
|
ret.prologVideo = reader.readUInt8();
|
|
|
|
ret.prologMusic = reader.readUInt8();
|
2023-03-14 19:46:15 +02:00
|
|
|
ret.prologText = readLocalizedString(reader, filename, modName, encoding, identifier);
|
2010-02-11 16:12:22 +02:00
|
|
|
}
|
2014-02-15 01:46:06 +03:00
|
|
|
return ret;
|
2010-02-11 16:12:22 +02:00
|
|
|
};
|
2014-02-15 01:46:06 +03:00
|
|
|
|
2010-02-11 16:12:22 +02:00
|
|
|
CCampaignScenario ret;
|
2010-02-16 19:28:56 +02:00
|
|
|
ret.conquered = false;
|
2023-03-14 19:46:15 +02:00
|
|
|
ret.mapName = reader.readBaseString();
|
2014-02-15 01:46:06 +03:00
|
|
|
ret.packedMapSize = reader.readUInt32();
|
2010-02-15 15:28:33 +02:00
|
|
|
if(mapVersion == 18)//unholy alliance
|
2010-02-16 19:28:56 +02:00
|
|
|
{
|
2014-02-15 01:46:06 +03:00
|
|
|
ret.loadPreconditionRegions(reader.readUInt16());
|
2010-02-16 19:28:56 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-15 01:46:06 +03:00
|
|
|
ret.loadPreconditionRegions(reader.readUInt8());
|
2010-02-16 19:28:56 +02:00
|
|
|
}
|
2014-02-15 01:46:06 +03:00
|
|
|
ret.regionColor = reader.readUInt8();
|
|
|
|
ret.difficulty = reader.readUInt8();
|
2023-03-14 19:46:15 +02:00
|
|
|
ret.regionText = readLocalizedString(reader, filename, modName, encoding, ret.mapName + ".region");
|
|
|
|
ret.prolog = prologEpilogReader(ret.mapName + ".prolog");
|
|
|
|
ret.epilog = prologEpilogReader(ret.mapName + ".epilog");
|
2010-02-11 16:12:22 +02:00
|
|
|
|
2014-02-15 01:46:06 +03:00
|
|
|
ret.travelOptions = readScenarioTravelFromMemory(reader, version);
|
2010-02-11 16:12:22 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-01-19 17:33:22 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-15 01:46:06 +03:00
|
|
|
CScenarioTravel CCampaignHandler::readScenarioTravelFromMemory(CBinaryReader & reader, int version )
|
2010-02-11 16:12:22 +02:00
|
|
|
{
|
|
|
|
CScenarioTravel ret;
|
|
|
|
|
2014-02-15 01:46:06 +03:00
|
|
|
ret.whatHeroKeeps = reader.readUInt8();
|
|
|
|
reader.getStream()->read(ret.monstersKeptByHero.data(), ret.monstersKeptByHero.size());
|
|
|
|
|
2011-11-27 16:14:20 +03:00
|
|
|
if (version < CampaignVersion::SoD)
|
2010-02-15 14:12:21 +02:00
|
|
|
{
|
2014-02-15 01:46:06 +03:00
|
|
|
ret.artifsKeptByHero.fill(0);
|
|
|
|
reader.getStream()->read(ret.artifsKeptByHero.data(), ret.artifsKeptByHero.size() - 1);
|
2016-11-25 15:40:11 +02:00
|
|
|
}
|
2011-11-27 16:14:20 +03:00
|
|
|
else
|
2010-02-15 14:12:21 +02:00
|
|
|
{
|
2014-02-15 01:46:06 +03:00
|
|
|
reader.getStream()->read(ret.artifsKeptByHero.data(), ret.artifsKeptByHero.size());
|
2010-02-15 14:12:21 +02:00
|
|
|
}
|
2010-02-11 16:12:22 +02:00
|
|
|
|
2014-02-15 01:46:06 +03:00
|
|
|
ret.startOptions = reader.readUInt8();
|
|
|
|
|
2010-02-11 16:12:22 +02:00
|
|
|
switch(ret.startOptions)
|
|
|
|
{
|
2010-02-16 19:28:56 +02:00
|
|
|
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
|
|
|
|
{
|
2014-02-15 01:46:06 +03:00
|
|
|
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;
|
2014-02-15 01:46:06 +03:00
|
|
|
bonus.type = static_cast<CScenarioTravel::STravelBonus::EBonusType>(reader.readUInt8());
|
2010-07-31 16:55:05 +03:00
|
|
|
//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
|
|
|
{
|
2014-02-15 01:46:06 +03: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
|
|
|
{
|
2014-02-15 01:46:06 +03: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
|
|
|
{
|
2014-02-15 01:46:06 +03: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
|
|
|
{
|
2014-02-15 01:46:06 +03: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
|
|
|
{
|
2014-02-15 01:46:06 +03: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
|
|
|
{
|
2014-02-15 01:46:06 +03: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
|
|
|
{
|
2014-02-15 01:46:06 +03: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
|
|
|
{
|
2014-02-15 01:46:06 +03:00
|
|
|
bonus.info1 = reader.readUInt8(); //type
|
2010-02-11 16:12:22 +02:00
|
|
|
//FD - wood+ore
|
|
|
|
//FE - mercury+sulfur+crystal+gem
|
2014-02-15 01:46:06 +03:00
|
|
|
bonus.info2 = reader.readUInt32(); //count
|
2010-02-11 16:12:22 +02:00
|
|
|
break;
|
|
|
|
}
|
2012-09-22 20:23:28 +03:00
|
|
|
default:
|
2017-08-10 18:39:27 +02:00
|
|
|
logGlobal->warn("Corrupted h3c file");
|
2012-09-22 20:23:28 +03:00
|
|
|
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
|
|
|
|
{
|
2014-02-15 01:46:06 +03:00
|
|
|
ui8 numOfBonuses = reader.readUInt8();
|
2010-02-11 16:12:22 +02:00
|
|
|
for (int g=0; g<numOfBonuses; ++g)
|
|
|
|
{
|
|
|
|
CScenarioTravel::STravelBonus bonus;
|
2013-12-20 16:07:58 +03:00
|
|
|
bonus.type = CScenarioTravel::STravelBonus::HEROES_FROM_PREVIOUS_SCENARIO;
|
2014-02-15 01:46:06 +03:00
|
|
|
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
|
|
|
|
{
|
2014-02-15 01:46:06 +03:00
|
|
|
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;
|
2014-02-15 01:46:06 +03:00
|
|
|
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");
|
2016-03-12 03:41:27 +02:00
|
|
|
break;
|
2010-02-11 16:12:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-08 16:39:19 +02:00
|
|
|
return ret;
|
2010-02-12 17:04:01 +02:00
|
|
|
}
|
|
|
|
|
2023-02-24 22:38:12 +02:00
|
|
|
std::vector< std::vector<ui8> > CCampaignHandler::getFile(std::unique_ptr<CInputStream> file, bool headerOnly)
|
2010-02-12 17:04:01 +02:00
|
|
|
{
|
2023-02-24 22:38:12 +02:00
|
|
|
CCompressedStream stream(std::move(file), true);
|
2010-02-12 17:04:01 +02:00
|
|
|
|
2012-08-02 14:03:26 +03:00
|
|
|
std::vector< std::vector<ui8> > ret;
|
|
|
|
do
|
2010-02-12 17:04:01 +02:00
|
|
|
{
|
2012-08-02 14:03:26 +03:00
|
|
|
std::vector<ui8> block(stream.getSize());
|
|
|
|
stream.read(block.data(), block.size());
|
|
|
|
ret.push_back(block);
|
2010-02-12 17:04:01 +02:00
|
|
|
}
|
2012-08-02 14:03:26 +03:00
|
|
|
while (!headerOnly && stream.getNextBlock());
|
2010-02-16 19:28:56 +02:00
|
|
|
|
2012-08-02 14:03:26 +03:00
|
|
|
return ret;
|
2010-02-18 16:09:16 +02:00
|
|
|
}
|
|
|
|
|
2010-02-16 19:28:56 +02:00
|
|
|
bool CCampaign::conquerable( int whichScenario ) const
|
|
|
|
{
|
2010-07-30 14:29:42 +03:00
|
|
|
//check for void scenraio
|
|
|
|
if (!scenarios[whichScenario].isNotVoid())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-02-16 19:28:56 +02:00
|
|
|
if (scenarios[whichScenario].conquered)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
//check preconditioned regions
|
|
|
|
for (int g=0; g<scenarios.size(); ++g)
|
|
|
|
{
|
2012-01-19 17:33:22 +03:00
|
|
|
if( vstd::contains(scenarios[whichScenario].preconditionRegions, g) && !scenarios[g].conquered)
|
2010-02-16 19:28:56 +02:00
|
|
|
return false; //prerequisite does not met
|
2016-11-25 15:40:11 +02:00
|
|
|
|
2010-02-16 19:28:56 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2010-05-08 21:56:38 +03:00
|
|
|
|
2010-07-30 14:29:42 +03:00
|
|
|
bool CCampaignScenario::isNotVoid() const
|
|
|
|
{
|
2023-02-11 18:30:06 +02:00
|
|
|
return !mapName.empty();
|
2010-07-30 14:29:42 +03:00
|
|
|
}
|
2010-07-31 16:55:05 +03:00
|
|
|
|
2023-02-11 18:30:06 +02:00
|
|
|
const CGHeroInstance * CCampaignScenario::strongestHero(const PlayerColor & owner)
|
2013-02-09 22:19:14 +03:00
|
|
|
{
|
2018-01-05 19:21:07 +02:00
|
|
|
std::function<bool(JsonNode & node)> isOwned = [owner](JsonNode & node)
|
|
|
|
{
|
2023-02-11 18:30:06 +02:00
|
|
|
auto * h = CCampaignState::crossoverDeserialize(node);
|
2018-01-05 19:21:07 +02:00
|
|
|
bool result = h->tempOwner == owner;
|
|
|
|
vstd::clear_pointer(h);
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
auto ownedHeroes = crossoverHeroes | boost::adaptors::filtered(isOwned);
|
2013-02-09 22:19:14 +03:00
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
auto i = vstd::maxElementByFun(ownedHeroes, [](JsonNode & node)
|
|
|
|
{
|
2023-02-11 18:30:06 +02:00
|
|
|
auto * h = CCampaignState::crossoverDeserialize(node);
|
2018-01-05 19:21:07 +02:00
|
|
|
double result = h->getHeroStrength();
|
|
|
|
vstd::clear_pointer(h);
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
return i == ownedHeroes.end() ? nullptr : CCampaignState::crossoverDeserialize(*i);
|
2013-02-09 22:19:14 +03:00
|
|
|
}
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
std::vector<CGHeroInstance *> CCampaignScenario::getLostCrossoverHeroes()
|
2014-01-30 21:56:31 +03:00
|
|
|
{
|
|
|
|
std::vector<CGHeroInstance *> lostCrossoverHeroes;
|
|
|
|
if(conquered)
|
|
|
|
{
|
2018-01-05 19:21:07 +02:00
|
|
|
for(auto node2 : placedCrossoverHeroes)
|
2014-01-30 21:56:31 +03:00
|
|
|
{
|
2023-02-11 18:30:06 +02:00
|
|
|
auto * hero = CCampaignState::crossoverDeserialize(node2);
|
2018-01-05 19:21:07 +02:00
|
|
|
auto it = range::find_if(crossoverHeroes, [hero](JsonNode node)
|
|
|
|
{
|
2023-02-11 18:30:06 +02:00
|
|
|
auto * h = CCampaignState::crossoverDeserialize(node);
|
2018-01-05 19:21:07 +02:00
|
|
|
bool result = hero->subID == h->subID;
|
|
|
|
vstd::clear_pointer(h);
|
|
|
|
return result;
|
|
|
|
});
|
2014-01-30 21:56:31 +03:00
|
|
|
if(it == crossoverHeroes.end())
|
|
|
|
{
|
|
|
|
lostCrossoverHeroes.push_back(hero);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-04 01:06:02 +02:00
|
|
|
return lostCrossoverHeroes;
|
2014-01-30 21:56:31 +03:00
|
|
|
}
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
void CCampaignState::setCurrentMapAsConquered(const std::vector<CGHeroInstance *> & heroes)
|
2010-08-20 16:34:39 +03:00
|
|
|
{
|
2018-01-05 19:21:07 +02:00
|
|
|
camp->scenarios[*currentMap].crossoverHeroes.clear();
|
|
|
|
for(CGHeroInstance * hero : heroes)
|
|
|
|
{
|
|
|
|
camp->scenarios[*currentMap].crossoverHeroes.push_back(crossoverSerialize(hero));
|
|
|
|
}
|
|
|
|
|
2013-12-16 21:39:56 +03:00
|
|
|
mapsConquered.push_back(*currentMap);
|
|
|
|
mapsRemaining -= *currentMap;
|
|
|
|
camp->scenarios[*currentMap].conquered = true;
|
2011-10-08 04:23:46 +03:00
|
|
|
}
|
2012-09-21 20:59:54 +03:00
|
|
|
|
2012-09-24 22:23:11 +03:00
|
|
|
boost::optional<CScenarioTravel::STravelBonus> CCampaignState::getBonusForCurrentMap() const
|
2012-09-21 20:59:54 +03:00
|
|
|
{
|
2012-09-24 22:23:11 +03:00
|
|
|
auto bonuses = getCurrentScenario().travelOptions.bonusesToChoose;
|
2013-12-16 21:39:56 +03:00
|
|
|
assert(chosenCampaignBonuses.count(*currentMap) || bonuses.size() == 0);
|
2013-12-18 21:18:12 +03:00
|
|
|
|
2014-02-09 00:05:24 +03:00
|
|
|
if(bonuses.empty())
|
|
|
|
return boost::optional<CScenarioTravel::STravelBonus>();
|
|
|
|
else
|
|
|
|
return bonuses[currentBonusID()];
|
2012-09-21 20:59:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const CCampaignScenario & CCampaignState::getCurrentScenario() const
|
|
|
|
{
|
2013-12-16 21:39:56 +03:00
|
|
|
return camp->scenarios[*currentMap];
|
|
|
|
}
|
|
|
|
|
2014-01-30 21:56:31 +03:00
|
|
|
CCampaignScenario & CCampaignState::getCurrentScenario()
|
|
|
|
{
|
|
|
|
return camp->scenarios[*currentMap];
|
|
|
|
}
|
|
|
|
|
2013-12-16 21:39:56 +03:00
|
|
|
ui8 CCampaignState::currentBonusID() const
|
|
|
|
{
|
|
|
|
return chosenCampaignBonuses.at(*currentMap);
|
|
|
|
}
|
|
|
|
|
2015-12-29 04:43:33 +02:00
|
|
|
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++)
|
|
|
|
{
|
2013-02-23 22:16:14 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2013-02-19 23:39:09 +03:00
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
CMap * CCampaignState::getMap(int scenarioId) const
|
|
|
|
{
|
|
|
|
// FIXME: there is certainly better way to handle maps inside campaigns
|
|
|
|
if(scenarioId == -1)
|
|
|
|
scenarioId = currentMap.get();
|
2023-04-09 01:24:40 +02:00
|
|
|
|
|
|
|
CMapService mapService;
|
|
|
|
if(camp->header.version == CampaignVersion::Version::VCMI)
|
|
|
|
return mapService.loadMap(ResourceID(camp->scenarios.at(scenarioId).mapName, EResType::MAP)).release();
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
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);
|
2018-01-05 19:21:07 +02:00
|
|
|
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();
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<CMapHeader> CCampaignState::getHeader(int scenarioId) const
|
|
|
|
{
|
|
|
|
if(scenarioId == -1)
|
|
|
|
scenarioId = currentMap.get();
|
2023-04-09 01:24:40 +02:00
|
|
|
|
|
|
|
CMapService mapService;
|
|
|
|
if(camp->header.version == CampaignVersion::Version::VCMI)
|
|
|
|
return mapService.loadMapHeader(ResourceID(camp->scenarios.at(scenarioId).mapName, EResType::MAP));
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
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);
|
2018-01-05 19:21:07 +02:00
|
|
|
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);
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<CMapInfo> CCampaignState::getMapInfo(int scenarioId) const
|
|
|
|
{
|
|
|
|
if(scenarioId == -1)
|
|
|
|
scenarioId = currentMap.get();
|
|
|
|
|
|
|
|
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();
|
2018-01-05 19:21:07 +02:00
|
|
|
hero->ID = Obj::HERO;
|
|
|
|
hero->serializeJsonOptions(handler);
|
|
|
|
return hero;
|
|
|
|
}
|
|
|
|
|
2013-02-19 23:39:09 +03:00
|
|
|
std::string CCampaignHandler::prologVideoName(ui8 index)
|
|
|
|
{
|
2013-02-23 00:03:56 +03:00
|
|
|
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 "";
|
2013-02-19 23:39:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string CCampaignHandler::prologMusicName(ui8 index)
|
|
|
|
{
|
2013-08-29 19:41:14 +03:00
|
|
|
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)));
|
2013-08-29 19:41:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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 "";
|
2013-02-19 23:39:09 +03:00
|
|
|
}
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_END
|