CCampaignHandler.h 7.6 KB

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