CampaignState.h 10 KB

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