CampaignState.h 9.4 KB

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