CampaignState.h 8.3 KB

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