CampaignState.h 8.7 KB

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