CCampaignHandler.h 8.3 KB

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