CampaignState.h 8.6 KB

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