2
0

CampaignState.h 8.4 KB

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