CampaignState.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * CampaignState.h, 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. #pragma once
  11. #include "CampaignBonus.h"
  12. #include "CampaignRegions.h"
  13. #include "CampaignScenarioPrologEpilog.h"
  14. #include "../filesystem/ResourcePath.h"
  15. #include "../gameState/HighScore.h"
  16. #include "../serializer/Serializeable.h"
  17. #include "../texts/TextLocalizationContainer.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. struct StartInfo;
  20. class CGHeroInstance;
  21. class CBinaryReader;
  22. class CInputStream;
  23. class CMap;
  24. class CMapHeader;
  25. class CMapInfo;
  26. class JsonNode;
  27. class IGameInfoCallback;
  28. class DLL_LINKAGE CampaignHeader : public boost::noncopyable
  29. {
  30. friend class CampaignHandler;
  31. friend class Campaign;
  32. // Campaign editor
  33. friend class CampaignEditor;
  34. friend class CampaignProperties;
  35. friend class ScenarioProperties;
  36. CampaignVersion version = CampaignVersion::NONE;
  37. CampaignRegions campaignRegions;
  38. MetaString name;
  39. MetaString description;
  40. MetaString author;
  41. MetaString authorContact;
  42. MetaString campaignVersion;
  43. std::time_t creationDateTime;
  44. AudioPath music;
  45. std::string filename;
  46. std::string modName;
  47. std::string encoding;
  48. ImagePath loadingBackground;
  49. ImagePath videoRim;
  50. VideoPath introVideo;
  51. VideoPath outroVideo;
  52. HeroTypeID yogWizardID;
  53. HeroTypeID gemSorceressID;
  54. int numberOfScenarios = 0;
  55. bool difficultyChosenByPlayer = false;
  56. bool restrictGarrisonsAI = false;
  57. TextContainerRegistrable textContainer;
  58. public:
  59. bool playerSelectedDifficulty() const;
  60. CampaignVersion getFormat() const;
  61. std::string getDescriptionTranslated() const;
  62. std::string getNameTranslated() const;
  63. std::string getAuthor() const;
  64. std::string getAuthorContact() const;
  65. std::string getCampaignVersion() const;
  66. time_t getCreationDateTime() const;
  67. std::string getFilename() const;
  68. std::string getModName() const;
  69. std::string getEncoding() const;
  70. AudioPath getMusic() const;
  71. ImagePath getLoadingBackground() const;
  72. ImagePath getVideoRim() const;
  73. VideoPath getIntroVideo() const;
  74. VideoPath getOutroVideo() const;
  75. HeroTypeID getYogWizardID() const;
  76. HeroTypeID getGemSorceressID() const;
  77. bool restrictedGarrisonsForAI() const;
  78. const CampaignRegions & getRegions() const;
  79. TextContainerRegistrable & getTexts();
  80. template <typename Handler> void serialize(Handler &h)
  81. {
  82. h & version;
  83. h & campaignRegions;
  84. h & numberOfScenarios;
  85. h & name;
  86. h & description;
  87. h & author;
  88. h & authorContact;
  89. h & campaignVersion;
  90. h & creationDateTime;
  91. h & difficultyChosenByPlayer;
  92. if (h.hasFeature(Handler::Version::CAMPAIGN_BONUSES))
  93. h & restrictGarrisonsAI;
  94. h & filename;
  95. h & modName;
  96. h & music;
  97. h & encoding;
  98. h & textContainer;
  99. h & loadingBackground;
  100. h & videoRim;
  101. h & introVideo;
  102. h & outroVideo;
  103. if (h.hasFeature(Handler::Version::CAMPAIGN_BONUSES))
  104. {
  105. h & yogWizardID;
  106. h & gemSorceressID;
  107. }
  108. }
  109. };
  110. struct DLL_LINKAGE CampaignTravel
  111. {
  112. struct DLL_LINKAGE WhatHeroKeeps
  113. {
  114. bool experience = false;
  115. bool primarySkills = false;
  116. bool secondarySkills = false;
  117. bool spells = false;
  118. bool artifacts = false;
  119. template <typename Handler> void serialize(Handler &h)
  120. {
  121. h & experience;
  122. h & primarySkills;
  123. h & secondarySkills;
  124. h & spells;
  125. h & artifacts;
  126. }
  127. };
  128. std::set<CreatureID> monstersKeptByHero;
  129. std::set<ArtifactID> artifactsKeptByHero;
  130. std::vector<CampaignBonus> bonusesToChoose;
  131. WhatHeroKeeps whatHeroKeeps;
  132. CampaignStartOptions startOptions = CampaignStartOptions::NONE; //1 - start bonus, 2 - traveling hero, 3 - hero options
  133. PlayerColor playerColor = PlayerColor::NEUTRAL; //only for startOptions == 1
  134. template <typename Handler> void serialize(Handler &h)
  135. {
  136. h & whatHeroKeeps;
  137. h & monstersKeptByHero;
  138. h & artifactsKeptByHero;
  139. h & startOptions;
  140. h & playerColor;
  141. if (h.hasFeature(Handler::Version::CAMPAIGN_BONUSES))
  142. {
  143. h & bonusesToChoose;
  144. }
  145. else
  146. {
  147. struct OldBonus{
  148. CampaignBonusType type = {};
  149. int32_t info1 = 0;
  150. int32_t info2 = 0;
  151. int32_t info3 = 0;
  152. void serialize(Handler &h)
  153. {
  154. h & type;
  155. h & info1;
  156. h & info2;
  157. h & info3;
  158. }
  159. };
  160. std::vector<OldBonus> oldBonuses;
  161. h & oldBonuses;
  162. }
  163. }
  164. };
  165. struct DLL_LINKAGE CampaignScenario
  166. {
  167. std::string mapName; //*.h3m
  168. MetaString scenarioName; //from header
  169. std::set<CampaignScenarioID> preconditionRegions; //what we need to conquer to conquer this one (stored as bitfield in h3c)
  170. ui8 regionColor = 0;
  171. ui8 difficulty = 0;
  172. MetaString regionText;
  173. CampaignScenarioPrologEpilog prolog;
  174. CampaignScenarioPrologEpilog epilog;
  175. CampaignTravel travelOptions;
  176. void loadPreconditionRegions(ui32 regions);
  177. bool isNotVoid() const;
  178. template <typename Handler> void serialize(Handler &h)
  179. {
  180. h & mapName;
  181. h & scenarioName;
  182. h & preconditionRegions;
  183. h & regionColor;
  184. h & difficulty;
  185. h & regionText;
  186. h & prolog;
  187. h & epilog;
  188. h & travelOptions;
  189. }
  190. };
  191. /// Class that represents loaded campaign information
  192. class DLL_LINKAGE Campaign : public CampaignHeader, public Serializeable
  193. {
  194. friend class CampaignHandler;
  195. // Campaign editor
  196. friend class CampaignEditor;
  197. friend class CampaignProperties;
  198. friend class ScenarioProperties;
  199. std::map<CampaignScenarioID, CampaignScenario> scenarios;
  200. public:
  201. const CampaignScenario & scenario(CampaignScenarioID which) const;
  202. std::set<CampaignScenarioID> allScenarios() const;
  203. int scenariosCount() const;
  204. void overrideCampaign();
  205. void overrideCampaignScenarios();
  206. template <typename Handler> void serialize(Handler &h)
  207. {
  208. h & static_cast<CampaignHeader&>(*this);
  209. h & scenarios;
  210. }
  211. };
  212. /// Class that represent campaign that is being played at
  213. /// Contains campaign itself as well as current state of the campaign
  214. class DLL_LINKAGE CampaignState : public Campaign
  215. {
  216. friend class CampaignHandler;
  217. // Campaign editor
  218. friend class CampaignEditor;
  219. friend class CampaignProperties;
  220. friend class ScenarioProperties;
  221. using ScenarioPoolType = std::vector<JsonNode>;
  222. using CampaignPoolType = std::map<CampaignScenarioID, ScenarioPoolType>;
  223. using GlobalPoolType = std::map<HeroTypeID, JsonNode>;
  224. /// List of all maps completed by player, in order of their completion
  225. std::vector<CampaignScenarioID> mapsConquered;
  226. /// List of previously loaded campaign maps, to prevent translation of transferred hero names getting lost after their original map has been completed
  227. std::map<CampaignScenarioID, TextContainerRegistrable> mapTranslations;
  228. std::map<CampaignScenarioID, std::vector<uint8_t> > mapPieces; //binary h3ms, scenario number -> map data
  229. std::map<CampaignScenarioID, ui8> chosenCampaignBonuses;
  230. std::optional<CampaignScenarioID> currentMap;
  231. /// Heroes from specific scenario, ordered by descending strength
  232. CampaignPoolType scenarioHeroPool;
  233. /// Pool of heroes currently reserved for usage in campaign
  234. GlobalPoolType globalHeroPool;
  235. public:
  236. CampaignState() = default;
  237. /// Returns last completed scenario, if any
  238. std::optional<CampaignScenarioID> lastScenario() const;
  239. std::optional<CampaignScenarioID> currentScenario() const;
  240. std::set<CampaignScenarioID> conqueredScenarios() const;
  241. /// Returns bonus selected for specific scenario
  242. std::optional<CampaignBonus> getBonus(CampaignScenarioID which) const;
  243. /// Returns index of selected bonus for specified scenario
  244. std::optional<ui8> getBonusID(CampaignScenarioID which) const;
  245. /// Returns true if selected scenario can be selected and started by player
  246. bool isAvailable(CampaignScenarioID whichScenario) const;
  247. /// Returns true if selected scenario has been already completed by player
  248. bool isConquered(CampaignScenarioID whichScenario) const;
  249. /// Returns true if all available scenarios have been completed and campaign is finished
  250. bool isCampaignFinished() const;
  251. std::unique_ptr<CMap> getMap(CampaignScenarioID scenarioId, IGameInfoCallback * cb);
  252. std::unique_ptr<CMapHeader> getMapHeader(CampaignScenarioID scenarioId) const;
  253. std::shared_ptr<CMapInfo> getMapInfo(CampaignScenarioID scenarioId) const;
  254. void setCurrentMap(CampaignScenarioID which);
  255. void setCurrentMapBonus(ui8 which);
  256. void setCurrentMapAsConquered(std::vector<CGHeroInstance*> heroes);
  257. /// Returns list of heroes that must be reserved for campaign and can only be used for hero placeholders
  258. std::set<HeroTypeID> getReservedHeroes() const;
  259. /// Returns strongest hero from specified scenario, or null if none found
  260. std::shared_ptr<CGHeroInstance> strongestHero(CampaignScenarioID scenarioId, const PlayerColor & owner) const;
  261. /// Returns heroes that can be instantiated as hero placeholders by power
  262. const std::vector<JsonNode> & getHeroesByPower(CampaignScenarioID scenarioId) const;
  263. /// Returns hero for instantiation as placeholder by type
  264. /// May return empty JsonNode if such hero was not found
  265. const JsonNode & getHeroByType(HeroTypeID heroID) const;
  266. JsonNode crossoverSerialize(CGHeroInstance * hero) const;
  267. std::shared_ptr<CGHeroInstance> crossoverDeserialize(const JsonNode & node, CMap * map) const;
  268. std::string campaignSet;
  269. std::vector<HighScoreParameter> highscoreParameters;
  270. template <typename Handler> void serialize(Handler &h)
  271. {
  272. h & static_cast<Campaign&>(*this);
  273. h & scenarioHeroPool;
  274. h & globalHeroPool;
  275. h & mapPieces;
  276. h & mapsConquered;
  277. h & currentMap;
  278. h & chosenCampaignBonuses;
  279. h & campaignSet;
  280. h & mapTranslations;
  281. h & highscoreParameters;
  282. }
  283. };
  284. VCMI_LIB_NAMESPACE_END