CampaignState.h 8.4 KB

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