CCampaignHandler.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #pragma once
  2. #include "../../lib/GameConstants.h"
  3. /*
  4. * CCampaignHandler.h, part of VCMI engine
  5. *
  6. * Authors: listed in file AUTHORS in main folder
  7. *
  8. * License: GNU General Public License v2.0 or later
  9. * Full text of license available in license.txt file, in main folder
  10. *
  11. */
  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 & mapVersion & name & description & difficultyChoosenByPlayer & music & filename & loadFromLod;
  39. }
  40. };
  41. class DLL_LINKAGE CScenarioTravel
  42. {
  43. public:
  44. ui8 whatHeroKeeps; //bitfield [0] - experience, [1] - prim skills, [2] - sec skills, [3] - spells, [4] - artifacts
  45. std::array<ui8, 19> monstersKeptByHero;
  46. std::array<ui8, 18> artifsKeptByHero;
  47. ui8 startOptions; //1 - start bonus, 2 - traveling hero, 3 - hero options
  48. ui8 playerColor; //only for startOptions == 1
  49. struct DLL_LINKAGE STravelBonus
  50. {
  51. enum EBonusType {SPELL, MONSTER, BUILDING, ARTIFACT, SPELL_SCROLL, PRIMARY_SKILL, SECONDARY_SKILL, RESOURCE,
  52. HEROES_FROM_PREVIOUS_SCENARIO, HERO};
  53. EBonusType type; //uses EBonusType
  54. si32 info1, info2, info3; //purpose depends on type
  55. bool isBonusForHero() const;
  56. STravelBonus();
  57. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  58. {
  59. h & type & info1 & info2 & info3;
  60. }
  61. };
  62. std::vector<STravelBonus> bonusesToChoose;
  63. CScenarioTravel();
  64. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  65. {
  66. h & whatHeroKeeps & monstersKeptByHero & artifsKeptByHero & startOptions & playerColor & bonusesToChoose;
  67. }
  68. };
  69. class DLL_LINKAGE CCampaignScenario
  70. {
  71. public:
  72. struct DLL_LINKAGE SScenarioPrologEpilog
  73. {
  74. bool hasPrologEpilog;
  75. ui8 prologVideo; // from CmpMovie.txt
  76. ui8 prologMusic; // from CmpMusic.txt
  77. std::string prologText;
  78. SScenarioPrologEpilog();
  79. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  80. {
  81. h & hasPrologEpilog & prologVideo & prologMusic & prologText;
  82. }
  83. };
  84. std::string mapName; //*.h3m
  85. std::string scenarioName; //from header. human-readble
  86. ui32 packedMapSize; //generally not used
  87. std::set<ui8> preconditionRegions; //what we need to conquer to conquer this one (stored as bitfield in h3c)
  88. ui8 regionColor;
  89. ui8 difficulty;
  90. bool conquered;
  91. std::string regionText;
  92. SScenarioPrologEpilog prolog, epilog;
  93. CScenarioTravel travelOptions;
  94. std::vector<HeroTypeID> keepHeroes; // contains list of heroes which should be kept for next scenario (doesn't matter if they lost)
  95. std::vector<CGHeroInstance *> crossoverHeroes; // contains all heroes with the same state when the campaign scenario was finished
  96. std::vector<CGHeroInstance *> placedCrossoverHeroes; // contains all placed crossover heroes defined by hero placeholders when the scenario was started
  97. const CGHeroInstance * strongestHero(PlayerColor owner) const;
  98. void loadPreconditionRegions(ui32 regions);
  99. bool isNotVoid() const;
  100. std::vector<CGHeroInstance *> getLostCrossoverHeroes() const; /// returns a list of crossover heroes which started the scenario, but didn't complete it
  101. CCampaignScenario();
  102. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  103. {
  104. h & mapName & scenarioName & packedMapSize & preconditionRegions & regionColor & difficulty & conquered & regionText &
  105. prolog & epilog & travelOptions & crossoverHeroes & placedCrossoverHeroes & keepHeroes;
  106. }
  107. };
  108. class DLL_LINKAGE CCampaign
  109. {
  110. public:
  111. CCampaignHeader header;
  112. std::vector<CCampaignScenario> scenarios;
  113. std::map<int, std::string > mapPieces; //binary h3ms, scenario number -> map data
  114. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  115. {
  116. h & header & scenarios & mapPieces;
  117. }
  118. bool conquerable(int whichScenario) const;
  119. CCampaign();
  120. };
  121. class DLL_LINKAGE CCampaignState
  122. {
  123. public:
  124. std::unique_ptr<CCampaign> camp;
  125. std::string campaignName;
  126. std::vector<ui8> mapsConquered, mapsRemaining;
  127. boost::optional<si32> currentMap;
  128. std::map<ui8, ui8> chosenCampaignBonuses;
  129. //void initNewCampaign(const StartInfo &si);
  130. void setCurrentMapAsConquered(const std::vector<CGHeroInstance*> & heroes);
  131. boost::optional<CScenarioTravel::STravelBonus> getBonusForCurrentMap() const;
  132. const CCampaignScenario & getCurrentScenario() const;
  133. CCampaignScenario & getCurrentScenario();
  134. ui8 currentBonusID() const;
  135. CCampaignState();
  136. CCampaignState(std::unique_ptr<CCampaign> _camp);
  137. ~CCampaignState(){};
  138. template <typename Handler> void serialize(Handler &h, const int version)
  139. {
  140. h & camp & campaignName & mapsRemaining & mapsConquered & currentMap;
  141. h & chosenCampaignBonuses;
  142. }
  143. };
  144. class DLL_LINKAGE CCampaignHandler
  145. {
  146. static CCampaignHeader readHeaderFromMemory(CBinaryReader & reader);
  147. static CCampaignScenario readScenarioFromMemory(CBinaryReader & reader, int version, int mapVersion );
  148. static CScenarioTravel readScenarioTravelFromMemory(CBinaryReader & reader, int version);
  149. /// returns h3c split in parts. 0 = h3c header, 1-end - maps (binary h3m)
  150. /// headerOnly - only header will be decompressed, returned vector wont have any maps
  151. static std::vector< std::vector<ui8> > getFile(const std::string & name, bool headerOnly);
  152. public:
  153. static std::string prologVideoName(ui8 index);
  154. static std::string prologMusicName(ui8 index);
  155. static std::string prologVoiceName(ui8 index);
  156. static CCampaignHeader getHeader( const std::string & name); //name - name of appropriate file
  157. static std::unique_ptr<CCampaign> getCampaign(const std::string & name); //name - name of appropriate file
  158. };