CampaignState.h 8.8 KB

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