2
0

CampaignState.h 10 KB

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