CampaignState.h 9.9 KB

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