CCampaignHandler.h 6.3 KB

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