CampaignState.h 8.6 KB

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