CampaignState.h 8.2 KB

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