CampaignState.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * CCampaignHandler.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CampaignState.h"
  12. #include "../Point.h"
  13. #include "../filesystem/ResourcePath.h"
  14. #include "../GameLibrary.h"
  15. #include "../texts/CGeneralTextHandler.h"
  16. #include "../mapping/CMapService.h"
  17. #include "../mapping/CMapInfo.h"
  18. #include "../mapping/CMap.h"
  19. #include "../mapping/MapFormatSettings.h"
  20. #include "../mapObjects/CGHeroInstance.h"
  21. #include "../serializer/JsonDeserializer.h"
  22. #include "../serializer/JsonSerializer.h"
  23. #include "../json/JsonUtils.h"
  24. VCMI_LIB_NAMESPACE_BEGIN
  25. void CampaignScenario::loadPreconditionRegions(ui32 regions)
  26. {
  27. for (int i=0; i<32; i++) //for each bit in region. h3c however can only hold up to 16
  28. {
  29. if ( (1 << i) & regions)
  30. preconditionRegions.insert(static_cast<CampaignScenarioID>(i));
  31. }
  32. }
  33. bool CampaignHeader::playerSelectedDifficulty() const
  34. {
  35. return difficultyChosenByPlayer;
  36. }
  37. CampaignVersion CampaignHeader::getFormat() const
  38. {
  39. return version;
  40. }
  41. std::string CampaignHeader::getDescriptionTranslated() const
  42. {
  43. return description.toString();
  44. }
  45. std::string CampaignHeader::getNameTranslated() const
  46. {
  47. return name.toString();
  48. }
  49. std::string CampaignHeader::getAuthor() const
  50. {
  51. return authorContact.toString();
  52. }
  53. std::string CampaignHeader::getAuthorContact() const
  54. {
  55. return authorContact.toString();
  56. }
  57. std::string CampaignHeader::getCampaignVersion() const
  58. {
  59. return campaignVersion.toString();
  60. }
  61. time_t CampaignHeader::getCreationDateTime() const
  62. {
  63. return creationDateTime;
  64. }
  65. std::string CampaignHeader::getFilename() const
  66. {
  67. return filename;
  68. }
  69. std::string CampaignHeader::getModName() const
  70. {
  71. return modName;
  72. }
  73. std::string CampaignHeader::getEncoding() const
  74. {
  75. return encoding;
  76. }
  77. AudioPath CampaignHeader::getMusic() const
  78. {
  79. return music;
  80. }
  81. ImagePath CampaignHeader::getLoadingBackground() const
  82. {
  83. return loadingBackground;
  84. }
  85. ImagePath CampaignHeader::getVideoRim() const
  86. {
  87. return videoRim;
  88. }
  89. VideoPath CampaignHeader::getIntroVideo() const
  90. {
  91. return introVideo;
  92. }
  93. VideoPath CampaignHeader::getOutroVideo() const
  94. {
  95. return outroVideo;
  96. }
  97. const CampaignRegions & CampaignHeader::getRegions() const
  98. {
  99. return campaignRegions;
  100. }
  101. TextContainerRegistrable & CampaignHeader::getTexts()
  102. {
  103. return textContainer;
  104. }
  105. bool CampaignState::isConquered(CampaignScenarioID whichScenario) const
  106. {
  107. return vstd::contains(mapsConquered, whichScenario);
  108. }
  109. bool CampaignState::isAvailable(CampaignScenarioID whichScenario) const
  110. {
  111. //check for void scenraio
  112. if (!scenario(whichScenario).isNotVoid())
  113. {
  114. return false;
  115. }
  116. if (vstd::contains(mapsConquered, whichScenario))
  117. {
  118. return false;
  119. }
  120. //check preconditioned regions
  121. for (auto const & it : scenario(whichScenario).preconditionRegions)
  122. {
  123. if (!vstd::contains(mapsConquered, it))
  124. return false;
  125. }
  126. return true;
  127. }
  128. bool CampaignScenario::isNotVoid() const
  129. {
  130. return !mapName.empty();
  131. }
  132. std::set<HeroTypeID> CampaignState::getReservedHeroes() const
  133. {
  134. std::set<HeroTypeID> result;
  135. for (auto const & scenarioID : allScenarios())
  136. {
  137. if (isConquered(scenarioID))
  138. continue;
  139. auto header = getMapHeader(scenarioID);
  140. result.insert(header->reservedCampaignHeroes.begin(), header->reservedCampaignHeroes.end());
  141. }
  142. return result;
  143. }
  144. std::shared_ptr<CGHeroInstance> CampaignState::strongestHero(CampaignScenarioID scenarioId, const PlayerColor & owner) const
  145. {
  146. std::function<bool(const JsonNode & node)> isOwned = [&](const JsonNode & node)
  147. {
  148. auto h = CampaignState::crossoverDeserialize(node, nullptr);
  149. bool result = h->tempOwner == owner;
  150. return result;
  151. };
  152. auto ownedHeroes = scenarioHeroPool.at(scenarioId) | boost::adaptors::filtered(isOwned);
  153. if (ownedHeroes.empty())
  154. return nullptr;
  155. return CampaignState::crossoverDeserialize(ownedHeroes.front(), nullptr);
  156. }
  157. /// Returns heroes that can be instantiated as hero placeholders by power
  158. const std::vector<JsonNode> & CampaignState::getHeroesByPower(CampaignScenarioID scenarioId) const
  159. {
  160. static const std::vector<JsonNode> emptyVector;
  161. if (scenarioHeroPool.count(scenarioId))
  162. return scenarioHeroPool.at(scenarioId);
  163. return emptyVector;
  164. }
  165. /// Returns hero for instantiation as placeholder by type
  166. /// May return empty JsonNode if such hero was not found
  167. const JsonNode & CampaignState::getHeroByType(HeroTypeID heroID) const
  168. {
  169. static const JsonNode emptyNode;
  170. if (!getReservedHeroes().count(heroID))
  171. return emptyNode;
  172. if (!globalHeroPool.count(heroID))
  173. return emptyNode;
  174. return globalHeroPool.at(heroID);
  175. }
  176. void CampaignState::setCurrentMapAsConquered(std::vector<CGHeroInstance *> heroes)
  177. {
  178. boost::range::sort(heroes, [](const CGHeroInstance * a, const CGHeroInstance * b)
  179. {
  180. return CGHeroInstance::compareCampaignValue(a, b);
  181. });
  182. logGlobal->info("Scenario %d of campaign %s (%s) has been completed", currentMap->getNum(), getFilename(), getNameTranslated());
  183. mapsConquered.push_back(*currentMap);
  184. auto reservedHeroes = getReservedHeroes();
  185. for (auto * hero : heroes)
  186. {
  187. JsonNode node = CampaignState::crossoverSerialize(hero);
  188. if (reservedHeroes.count(hero->getHeroTypeID()))
  189. {
  190. logGlobal->info("Hero crossover: %d (%s) exported to global pool", hero->getHeroTypeID(), hero->getNameTranslated());
  191. globalHeroPool[hero->getHeroTypeID()] = node;
  192. }
  193. else
  194. {
  195. logGlobal->info("Hero crossover: %d (%s) exported to scenario pool", hero->getHeroTypeID(), hero->getNameTranslated());
  196. scenarioHeroPool[*currentMap].push_back(node);
  197. }
  198. }
  199. }
  200. std::optional<CampaignBonus> CampaignState::getBonus(CampaignScenarioID which) const
  201. {
  202. auto bonuses = scenario(which).travelOptions.bonusesToChoose;
  203. assert(chosenCampaignBonuses.count(*currentMap) || bonuses.empty());
  204. if(bonuses.empty())
  205. return std::optional<CampaignBonus>();
  206. if (!getBonusID(which))
  207. return std::optional<CampaignBonus>();
  208. return bonuses[getBonusID(which).value()];
  209. }
  210. std::optional<ui8> CampaignState::getBonusID(CampaignScenarioID which) const
  211. {
  212. if (!chosenCampaignBonuses.count(which))
  213. return std::nullopt;
  214. return chosenCampaignBonuses.at(which);
  215. }
  216. std::unique_ptr<CMap> CampaignState::getMap(CampaignScenarioID scenarioId, IGameInfoCallback * cb)
  217. {
  218. // FIXME: there is certainly better way to handle maps inside campaigns
  219. if(scenarioId == CampaignScenarioID::NONE)
  220. scenarioId = currentMap.value();
  221. CMapService mapService;
  222. std::string scenarioName = getFilename().substr(0, getFilename().find('.'));
  223. boost::to_lower(scenarioName);
  224. scenarioName += ':' + std::to_string(scenarioId.getNum());
  225. if(!mapPieces.count(scenarioId))
  226. return nullptr;
  227. const auto & mapContent = mapPieces.find(scenarioId)->second;
  228. auto result = mapService.loadMap(mapContent.data(), mapContent.size(), scenarioName, getModName(), getEncoding(), cb);
  229. mapTranslations[scenarioId] = result->texts;
  230. return result;
  231. }
  232. std::unique_ptr<CMapHeader> CampaignState::getMapHeader(CampaignScenarioID scenarioId) const
  233. {
  234. if(scenarioId == CampaignScenarioID::NONE)
  235. scenarioId = currentMap.value();
  236. CMapService mapService;
  237. std::string scenarioName = getFilename().substr(0, getFilename().find('.'));
  238. boost::to_lower(scenarioName);
  239. scenarioName += ':' + std::to_string(scenarioId.getNum());
  240. const auto & mapContent = mapPieces.find(scenarioId)->second;
  241. return mapService.loadMapHeader(mapContent.data(), mapContent.size(), scenarioName, getModName(), getEncoding());
  242. }
  243. std::shared_ptr<CMapInfo> CampaignState::getMapInfo(CampaignScenarioID scenarioId) const
  244. {
  245. if(scenarioId == CampaignScenarioID::NONE)
  246. scenarioId = currentMap.value();
  247. auto mapInfo = std::make_shared<CMapInfo>();
  248. mapInfo->fileURI = getFilename();
  249. mapInfo->mapHeader = getMapHeader(scenarioId);
  250. mapInfo->countPlayers();
  251. return mapInfo;
  252. }
  253. JsonNode CampaignState::crossoverSerialize(CGHeroInstance * hero) const
  254. {
  255. JsonNode node;
  256. JsonSerializer handler(nullptr, node);
  257. hero->serializeJsonOptions(handler);
  258. node.setModScope(ModScope::scopeGame());
  259. logGlobal->info(node.toString());
  260. return node;
  261. }
  262. std::shared_ptr<CGHeroInstance> CampaignState::crossoverDeserialize(const JsonNode & node, CMap * map) const
  263. {
  264. JsonDeserializer handler(nullptr, const_cast<JsonNode&>(node));
  265. auto hero = std::make_shared<CGHeroInstance>(map ? map->cb : nullptr);
  266. hero->ID = Obj::HERO;
  267. hero->serializeJsonOptions(handler);
  268. if (map)
  269. {
  270. hero->serializeJsonArtifacts(handler, "artifacts", map);
  271. }
  272. return hero;
  273. }
  274. void CampaignState::setCurrentMap(CampaignScenarioID which)
  275. {
  276. assert(scenario(which).isNotVoid());
  277. currentMap = which;
  278. }
  279. void CampaignState::setCurrentMapBonus(ui8 which)
  280. {
  281. chosenCampaignBonuses[*currentMap] = which;
  282. }
  283. std::optional<CampaignScenarioID> CampaignState::currentScenario() const
  284. {
  285. return currentMap;
  286. }
  287. std::optional<CampaignScenarioID> CampaignState::lastScenario() const
  288. {
  289. if (mapsConquered.empty())
  290. return std::nullopt;
  291. return mapsConquered.back();
  292. }
  293. std::set<CampaignScenarioID> CampaignState::conqueredScenarios() const
  294. {
  295. std::set<CampaignScenarioID> result;
  296. result.insert(mapsConquered.begin(), mapsConquered.end());
  297. return result;
  298. }
  299. std::set<CampaignScenarioID> Campaign::allScenarios() const
  300. {
  301. std::set<CampaignScenarioID> result;
  302. for (auto const & entry : scenarios)
  303. {
  304. if (entry.second.isNotVoid())
  305. result.insert(entry.first);
  306. }
  307. return result;
  308. }
  309. void Campaign::overrideCampaign()
  310. {
  311. const JsonNode & overrides = LIBRARY->mapFormat->campaignOverrides(filename);
  312. if(!overrides["regions"].isNull())
  313. campaignRegions = CampaignRegions(overrides["regions"]);
  314. if (!overrides["scenarioCount"].isNull())
  315. numberOfScenarios = overrides["scenarioCount"].Integer();
  316. if(!overrides["loadingBackground"].isNull())
  317. loadingBackground = ImagePath::builtin(overrides["loadingBackground"].String());
  318. if(!overrides["videoRim"].isNull())
  319. videoRim = ImagePath::builtin(overrides["videoRim"].String());
  320. if(!overrides["introVideo"].isNull())
  321. introVideo = VideoPath::builtin(overrides["introVideo"].String());
  322. if(!overrides["outroVideo"].isNull())
  323. outroVideo = VideoPath::builtin(overrides["outroVideo"].String());
  324. if(!overrides["heroGemSorceress"].isNull())
  325. gemSorceressID = HeroTypeID(*LIBRARY->identifiersHandler->getIdentifier("hero", overrides["heroGemSorceress"]));
  326. if(!overrides["heroYogWizard"].isNull())
  327. yogWizardID = HeroTypeID(*LIBRARY->identifiersHandler->getIdentifier("hero", overrides["heroYogWizard"]));
  328. restrictGarrisonsAI = overrides["restrictedGarrisonsForAI"].Bool();
  329. }
  330. void Campaign::overrideCampaignScenarios()
  331. {
  332. const JsonNode & overrides = LIBRARY->mapFormat->campaignOverrides(filename);
  333. if(!overrides["scenarios"].isNull())
  334. {
  335. auto sc = overrides["scenarios"].Vector();
  336. for(int i = 0; i < sc.size(); i++)
  337. {
  338. auto it = scenarios.begin();
  339. std::advance(it, i);
  340. if(!sc.at(i)["voiceProlog"].isNull())
  341. it->second.prolog.prologVoice = AudioPath::builtin(sc.at(i)["voiceProlog"].String());
  342. if(!sc.at(i)["voiceEpilog"].isNull())
  343. it->second.epilog.prologVoice = AudioPath::builtin(sc.at(i)["voiceEpilog"].String());
  344. }
  345. }
  346. }
  347. int Campaign::scenariosCount() const
  348. {
  349. return allScenarios().size();
  350. }
  351. const CampaignScenario & Campaign::scenario(CampaignScenarioID which) const
  352. {
  353. assert(scenarios.count(which));
  354. assert(scenarios.at(which).isNotVoid());
  355. return scenarios.at(which);
  356. }
  357. bool CampaignState::isCampaignFinished() const
  358. {
  359. return conqueredScenarios() == allScenarios();
  360. }
  361. HeroTypeID CampaignHeader::getYogWizardID() const
  362. {
  363. return yogWizardID;
  364. }
  365. HeroTypeID CampaignHeader::getGemSorceressID() const
  366. {
  367. return gemSorceressID;
  368. }
  369. bool CampaignHeader::restrictedGarrisonsForAI() const
  370. {
  371. return restrictGarrisonsAI;
  372. }
  373. VCMI_LIB_NAMESPACE_END