CampaignState.h 9.1 KB

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