CCampaignHandler.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * CCampaignHandler.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 "../../lib/GameConstants.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. struct StartInfo;
  14. class CGHeroInstance;
  15. class CBinaryReader;
  16. class CInputStream;
  17. class CMap;
  18. class CMapHeader;
  19. class CMapInfo;
  20. class JsonNode;
  21. namespace CampaignVersion
  22. {
  23. enum Version
  24. {
  25. RoE = 4,
  26. AB = 5,
  27. SoD = 6,
  28. WoG = 6,
  29. // Chr = 7, // Heroes Chronicles, likely identical to SoD, untested
  30. VCMI = 1
  31. };
  32. const int VCMI_MIN = 1;
  33. const int VCMI_MAX = 1;
  34. }
  35. struct DLL_LINKAGE CampaignRegions
  36. {
  37. std::string campPrefix;
  38. int colorSuffixLength;
  39. struct DLL_LINKAGE RegionDescription
  40. {
  41. std::string infix;
  42. int xpos, ypos;
  43. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  44. {
  45. h & infix;
  46. h & xpos;
  47. h & ypos;
  48. }
  49. static CampaignRegions::RegionDescription fromJson(const JsonNode & node);
  50. };
  51. std::vector<RegionDescription> regions;
  52. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  53. {
  54. h & campPrefix;
  55. h & colorSuffixLength;
  56. h & regions;
  57. }
  58. static CampaignRegions fromJson(const JsonNode & node);
  59. static CampaignRegions getLegacy(int campId);
  60. };
  61. class DLL_LINKAGE CCampaignHeader
  62. {
  63. public:
  64. si32 version = 0; //4 - RoE, 5 - AB, 6 - SoD, WoG and HotA
  65. CampaignRegions campaignRegions;
  66. int numberOfScenarios = 0;
  67. std::string name, description;
  68. bool difficultyChoosenByPlayer = false;
  69. bool valid = false;
  70. std::string filename;
  71. std::string modName;
  72. std::string encoding;
  73. void loadLegacyData(ui8 campId);
  74. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  75. {
  76. h & version;
  77. h & campaignRegions;
  78. h & numberOfScenarios;
  79. h & name;
  80. h & description;
  81. h & difficultyChoosenByPlayer;
  82. h & filename;
  83. h & modName;
  84. h & encoding;
  85. h & valid;
  86. }
  87. };
  88. class DLL_LINKAGE CScenarioTravel
  89. {
  90. public:
  91. struct DLL_LINKAGE WhatHeroKeeps
  92. {
  93. bool experience = false;
  94. bool primarySkills = false;
  95. bool secondarySkills = false;
  96. bool spells = false;
  97. bool artifacts = false;
  98. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  99. {
  100. h & experience;
  101. h & primarySkills;
  102. h & secondarySkills;
  103. h & spells;
  104. h & artifacts;
  105. }
  106. };
  107. WhatHeroKeeps whatHeroKeeps;
  108. //TODO: use typed containers
  109. std::set<int> monstersKeptByHero;
  110. std::set<int> artifactsKeptByHero;
  111. ui8 startOptions = 0; //1 - start bonus, 2 - traveling hero, 3 - hero options
  112. ui8 playerColor = 0; //only for startOptions == 1
  113. struct DLL_LINKAGE STravelBonus
  114. {
  115. enum EBonusType {SPELL, MONSTER, BUILDING, ARTIFACT, SPELL_SCROLL, PRIMARY_SKILL, SECONDARY_SKILL, RESOURCE,
  116. HEROES_FROM_PREVIOUS_SCENARIO, HERO};
  117. EBonusType type = EBonusType::SPELL; //uses EBonusType
  118. si32 info1 = 0, info2 = 0, info3 = 0; //purpose depends on type
  119. bool isBonusForHero() const;
  120. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  121. {
  122. h & type;
  123. h & info1;
  124. h & info2;
  125. h & info3;
  126. }
  127. };
  128. std::vector<STravelBonus> bonusesToChoose;
  129. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  130. {
  131. h & whatHeroKeeps;
  132. h & monstersKeptByHero;
  133. h & artifactsKeptByHero;
  134. h & startOptions;
  135. h & playerColor;
  136. h & bonusesToChoose;
  137. }
  138. };
  139. class DLL_LINKAGE CCampaignScenario
  140. {
  141. public:
  142. struct DLL_LINKAGE SScenarioPrologEpilog
  143. {
  144. bool hasPrologEpilog = false;
  145. std::string prologVideo; // from CmpMovie.txt
  146. std::string prologMusic; // from CmpMusic.txt
  147. std::string prologText;
  148. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  149. {
  150. h & hasPrologEpilog;
  151. h & prologVideo;
  152. h & prologMusic;
  153. h & prologText;
  154. }
  155. };
  156. std::string mapName; //*.h3m
  157. std::string scenarioName; //from header. human-readble
  158. std::set<ui8> preconditionRegions; //what we need to conquer to conquer this one (stored as bitfield in h3c)
  159. ui8 regionColor = 0;
  160. ui8 difficulty = 0;
  161. bool conquered = false;
  162. std::string regionText;
  163. SScenarioPrologEpilog prolog, epilog;
  164. CScenarioTravel travelOptions;
  165. std::vector<HeroTypeID> keepHeroes; // contains list of heroes which should be kept for next scenario (doesn't matter if they lost)
  166. std::vector<JsonNode> crossoverHeroes; // contains all heroes with the same state when the campaign scenario was finished
  167. std::vector<JsonNode> placedCrossoverHeroes; // contains all placed crossover heroes defined by hero placeholders when the scenario was started
  168. void loadPreconditionRegions(ui32 regions);
  169. bool isNotVoid() const;
  170. // FIXME: due to usage of JsonNode I can't make these methods const
  171. const CGHeroInstance * strongestHero(const PlayerColor & owner);
  172. std::vector<CGHeroInstance *> getLostCrossoverHeroes(); /// returns a list of crossover heroes which started the scenario, but didn't complete it
  173. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  174. {
  175. h & mapName;
  176. h & scenarioName;
  177. h & preconditionRegions;
  178. h & regionColor;
  179. h & difficulty;
  180. h & conquered;
  181. h & regionText;
  182. h & prolog;
  183. h & epilog;
  184. h & travelOptions;
  185. h & crossoverHeroes;
  186. h & placedCrossoverHeroes;
  187. h & keepHeroes;
  188. }
  189. };
  190. class DLL_LINKAGE CCampaign
  191. {
  192. public:
  193. CCampaignHeader header;
  194. std::vector<CCampaignScenario> scenarios;
  195. std::map<int, std::string > mapPieces; //binary h3ms, scenario number -> map data
  196. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  197. {
  198. h & header;
  199. h & scenarios;
  200. h & mapPieces;
  201. }
  202. bool conquerable(int whichScenario) const;
  203. };
  204. class DLL_LINKAGE CCampaignState
  205. {
  206. public:
  207. std::unique_ptr<CCampaign> camp;
  208. std::string fileEncoding;
  209. std::vector<ui8> mapsConquered, mapsRemaining;
  210. std::optional<si32> currentMap;
  211. std::map<ui8, ui8> chosenCampaignBonuses;
  212. void setCurrentMapAsConquered(const std::vector<CGHeroInstance*> & heroes);
  213. std::optional<CScenarioTravel::STravelBonus> getBonusForCurrentMap() const;
  214. const CCampaignScenario & getCurrentScenario() const;
  215. CCampaignScenario & getCurrentScenario();
  216. ui8 currentBonusID() const;
  217. CMap * getMap(int scenarioId = -1) const;
  218. std::unique_ptr<CMapHeader> getHeader(int scenarioId = -1) const;
  219. std::shared_ptr<CMapInfo> getMapInfo(int scenarioId = -1) const;
  220. static JsonNode crossoverSerialize(CGHeroInstance * hero);
  221. static CGHeroInstance * crossoverDeserialize(JsonNode & node);
  222. CCampaignState() = default;
  223. CCampaignState(std::unique_ptr<CCampaign> _camp);
  224. template <typename Handler> void serialize(Handler &h, const int version)
  225. {
  226. h & camp;
  227. h & mapsRemaining;
  228. h & mapsConquered;
  229. h & currentMap;
  230. h & chosenCampaignBonuses;
  231. }
  232. };
  233. class DLL_LINKAGE CCampaignHandler
  234. {
  235. static std::string readLocalizedString(CBinaryReader & reader, std::string filename, std::string modName, std::string encoding, std::string identifier);
  236. //parsers for VCMI campaigns (*.vcmp)
  237. static CCampaignHeader readHeaderFromJson(JsonNode & reader, std::string filename, std::string modName, std::string encoding);
  238. static CCampaignScenario readScenarioFromJson(JsonNode & reader);
  239. static CScenarioTravel readScenarioTravelFromJson(JsonNode & reader);
  240. //parsers for original H3C campaigns
  241. static CCampaignHeader readHeaderFromMemory(CBinaryReader & reader, std::string filename, std::string modName, std::string encoding);
  242. static CCampaignScenario readScenarioFromMemory(CBinaryReader & reader, const CCampaignHeader & header);
  243. static CScenarioTravel readScenarioTravelFromMemory(CBinaryReader & reader, int version);
  244. /// returns h3c split in parts. 0 = h3c header, 1-end - maps (binary h3m)
  245. /// headerOnly - only header will be decompressed, returned vector wont have any maps
  246. static std::vector<std::vector<ui8>> getFile(std::unique_ptr<CInputStream> file, bool headerOnly);
  247. static std::string prologVideoName(ui8 index);
  248. static std::string prologMusicName(ui8 index);
  249. static std::string prologVoiceName(ui8 index);
  250. public:
  251. static CCampaignHeader getHeader( const std::string & name); //name - name of appropriate file
  252. static std::unique_ptr<CCampaign> getCampaign(const std::string & name); //name - name of appropriate file
  253. };
  254. VCMI_LIB_NAMESPACE_END