CCampaignHandler.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. struct StartInfo;
  13. class CGHeroInstance;
  14. class CBinaryReader;
  15. class CMap;
  16. class CMapHeader;
  17. class CMapInfo;
  18. class JsonNode;
  19. namespace CampaignVersion
  20. {
  21. enum Version
  22. {
  23. RoE = 4,
  24. AB = 5,
  25. SoD = 6,
  26. WoG = 6
  27. };
  28. }
  29. class DLL_LINKAGE CCampaignHeader
  30. {
  31. public:
  32. si32 version; //4 - RoE, 5 - AB, 6 - SoD and WoG
  33. ui8 mapVersion; //CampText.txt's format
  34. std::string name, description;
  35. ui8 difficultyChoosenByPlayer;
  36. ui8 music; //CmpMusic.txt, start from 0
  37. std::string filename;
  38. ui8 loadFromLod; //if true, this campaign must be loaded fro, .lod file
  39. CCampaignHeader();
  40. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  41. {
  42. h & version;
  43. h & mapVersion;
  44. h & name;
  45. h & description;
  46. h & difficultyChoosenByPlayer;
  47. h & music;
  48. h & filename;
  49. h & loadFromLod;
  50. }
  51. };
  52. class DLL_LINKAGE CScenarioTravel
  53. {
  54. public:
  55. ui8 whatHeroKeeps; //bitfield [0] - experience, [1] - prim skills, [2] - sec skills, [3] - spells, [4] - artifacts
  56. std::array<ui8, 19> monstersKeptByHero;
  57. std::array<ui8, 18> artifsKeptByHero;
  58. ui8 startOptions; //1 - start bonus, 2 - traveling hero, 3 - hero options
  59. ui8 playerColor; //only for startOptions == 1
  60. struct DLL_LINKAGE STravelBonus
  61. {
  62. enum EBonusType {SPELL, MONSTER, BUILDING, ARTIFACT, SPELL_SCROLL, PRIMARY_SKILL, SECONDARY_SKILL, RESOURCE,
  63. HEROES_FROM_PREVIOUS_SCENARIO, HERO};
  64. EBonusType type; //uses EBonusType
  65. si32 info1, info2, info3; //purpose depends on type
  66. bool isBonusForHero() const;
  67. STravelBonus();
  68. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  69. {
  70. h & type;
  71. h & info1;
  72. h & info2;
  73. h & info3;
  74. }
  75. };
  76. std::vector<STravelBonus> bonusesToChoose;
  77. CScenarioTravel();
  78. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  79. {
  80. h & whatHeroKeeps;
  81. h & monstersKeptByHero;
  82. h & artifsKeptByHero;
  83. h & startOptions;
  84. h & playerColor;
  85. h & bonusesToChoose;
  86. }
  87. };
  88. class DLL_LINKAGE CCampaignScenario
  89. {
  90. public:
  91. struct DLL_LINKAGE SScenarioPrologEpilog
  92. {
  93. bool hasPrologEpilog;
  94. ui8 prologVideo; // from CmpMovie.txt
  95. ui8 prologMusic; // from CmpMusic.txt
  96. std::string prologText;
  97. SScenarioPrologEpilog();
  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; //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;
  111. ui8 difficulty;
  112. bool conquered;
  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(PlayerColor owner);
  123. std::vector<CGHeroInstance *> getLostCrossoverHeroes(); /// returns a list of crossover heroes which started the scenario, but didn't complete it
  124. std::vector<JsonNode> update787(std::vector<CGHeroInstance *> & heroes);
  125. CCampaignScenario();
  126. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  127. {
  128. h & mapName;
  129. h & scenarioName;
  130. h & packedMapSize;
  131. h & preconditionRegions;
  132. h & regionColor;
  133. h & difficulty;
  134. h & conquered;
  135. h & regionText;
  136. h & prolog;
  137. h & epilog;
  138. h & travelOptions;
  139. if(formatVersion < 787)
  140. {
  141. std::vector<CGHeroInstance *> crossoverHeroesOld, placedCrossoverHeroesOld;
  142. h & crossoverHeroesOld;
  143. h & placedCrossoverHeroesOld;
  144. crossoverHeroes = update787(crossoverHeroesOld);
  145. placedCrossoverHeroes = update787(placedCrossoverHeroesOld);
  146. }
  147. else
  148. {
  149. h & crossoverHeroes;
  150. h & placedCrossoverHeroes;
  151. }
  152. h & keepHeroes;
  153. }
  154. };
  155. class DLL_LINKAGE CCampaign
  156. {
  157. public:
  158. CCampaignHeader header;
  159. std::vector<CCampaignScenario> scenarios;
  160. std::map<int, std::string > mapPieces; //binary h3ms, scenario number -> map data
  161. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  162. {
  163. h & header;
  164. h & scenarios;
  165. h & mapPieces;
  166. }
  167. bool conquerable(int whichScenario) const;
  168. CCampaign();
  169. };
  170. class DLL_LINKAGE CCampaignState
  171. {
  172. public:
  173. std::unique_ptr<CCampaign> camp;
  174. std::string campaignName;
  175. std::vector<ui8> mapsConquered, mapsRemaining;
  176. boost::optional<si32> currentMap;
  177. std::map<ui8, ui8> chosenCampaignBonuses;
  178. void setCurrentMapAsConquered(const std::vector<CGHeroInstance*> & heroes);
  179. boost::optional<CScenarioTravel::STravelBonus> getBonusForCurrentMap() const;
  180. const CCampaignScenario & getCurrentScenario() const;
  181. CCampaignScenario & getCurrentScenario();
  182. ui8 currentBonusID() const;
  183. CMap * getMap(int scenarioId = -1) const;
  184. std::unique_ptr<CMapHeader> getHeader(int scenarioId = -1) const;
  185. std::shared_ptr<CMapInfo> getMapInfo(int scenarioId = -1) const;
  186. static JsonNode crossoverSerialize(CGHeroInstance * hero);
  187. static CGHeroInstance * crossoverDeserialize(JsonNode & node);
  188. CCampaignState();
  189. CCampaignState(std::unique_ptr<CCampaign> _camp);
  190. ~CCampaignState(){};
  191. template <typename Handler> void serialize(Handler &h, const int version)
  192. {
  193. h & camp;
  194. h & campaignName;
  195. h & mapsRemaining;
  196. h & mapsConquered;
  197. h & currentMap;
  198. h & chosenCampaignBonuses;
  199. }
  200. };
  201. class DLL_LINKAGE CCampaignHandler
  202. {
  203. static CCampaignHeader readHeaderFromMemory(CBinaryReader & reader);
  204. static CCampaignScenario readScenarioFromMemory(CBinaryReader & reader, int version, int mapVersion );
  205. static CScenarioTravel readScenarioTravelFromMemory(CBinaryReader & reader, int version);
  206. /// returns h3c split in parts. 0 = h3c header, 1-end - maps (binary h3m)
  207. /// headerOnly - only header will be decompressed, returned vector wont have any maps
  208. static std::vector< std::vector<ui8> > getFile(const std::string & name, bool headerOnly);
  209. public:
  210. static std::string prologVideoName(ui8 index);
  211. static std::string prologMusicName(ui8 index);
  212. static std::string prologVoiceName(ui8 index);
  213. static CCampaignHeader getHeader( const std::string & name); //name - name of appropriate file
  214. static std::unique_ptr<CCampaign> getCampaign(const std::string & name); //name - name of appropriate file
  215. };