CCampaignHandler.h 6.8 KB

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