CampaignState.h 8.4 KB

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