CCampaignHandler.h 6.9 KB

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