CCampaignHandler.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #pragma once
  2. #include "../../Global.h"
  3. #include "../../lib/GameConstants.h"
  4. /*
  5. * CCampaignHandler.h, part of VCMI engine
  6. *
  7. * Authors: listed in file AUTHORS in main folder
  8. *
  9. * License: GNU General Public License v2.0 or later
  10. * Full text of license available in license.txt file, in main folder
  11. *
  12. */
  13. struct StartInfo;
  14. class CGHeroInstance;
  15. class CBinaryReader;
  16. namespace CampaignVersion
  17. {
  18. enum Version
  19. {
  20. RoE = 4,
  21. AB = 5,
  22. SoD = 6,
  23. WoG = 6
  24. };
  25. }
  26. class DLL_LINKAGE CCampaignHeader
  27. {
  28. public:
  29. si32 version; //4 - RoE, 5 - AB, 6 - SoD and WoG
  30. ui8 mapVersion; //CampText.txt's format
  31. std::string name, description;
  32. ui8 difficultyChoosenByPlayer;
  33. ui8 music; //CmpMusic.txt, start from 0
  34. std::string filename;
  35. ui8 loadFromLod; //if true, this campaign must be loaded fro, .lod file
  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. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  57. {
  58. h & type & info1 & info2 & info3;
  59. }
  60. };
  61. std::vector<STravelBonus> bonusesToChoose;
  62. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  63. {
  64. h & whatHeroKeeps & monstersKeptByHero & artifsKeptByHero & startOptions & playerColor & bonusesToChoose;
  65. }
  66. };
  67. class DLL_LINKAGE CCampaignScenario
  68. {
  69. public:
  70. struct DLL_LINKAGE SScenarioPrologEpilog
  71. {
  72. bool hasPrologEpilog;
  73. ui8 prologVideo; // from CmpMovie.txt
  74. ui8 prologMusic; // from CmpMusic.txt
  75. std::string prologText;
  76. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  77. {
  78. h & hasPrologEpilog & prologVideo & prologMusic & prologText;
  79. }
  80. };
  81. std::string mapName; //*.h3m
  82. std::string scenarioName; //from header. human-readble
  83. ui32 packedMapSize; //generally not used
  84. std::set<ui8> preconditionRegions; //what we need to conquer to conquer this one (stored as bitfield in h3c)
  85. ui8 regionColor;
  86. ui8 difficulty;
  87. bool conquered;
  88. std::string regionText;
  89. SScenarioPrologEpilog prolog, epilog;
  90. CScenarioTravel travelOptions;
  91. std::vector<HeroTypeID> keepHeroes; // contains list of heroes which should be kept for next scenario (doesn't matter if they lost)
  92. std::vector<CGHeroInstance *> crossoverHeroes; // contains all heroes with the same state when the campaign scenario was finished
  93. std::vector<CGHeroInstance *> placedCrossoverHeroes; // contains all placed crossover heroes defined by hero placeholders when the scenario was started
  94. const CGHeroInstance * strongestHero(PlayerColor owner) const;
  95. void loadPreconditionRegions(ui32 regions);
  96. bool isNotVoid() const;
  97. std::vector<CGHeroInstance *> getLostCrossoverHeroes() const; /// returns a list of crossover heroes which started the scenario, but didn't complete it
  98. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  99. {
  100. h & mapName & scenarioName & packedMapSize & preconditionRegions & regionColor & difficulty & conquered & regionText &
  101. prolog & epilog & travelOptions & crossoverHeroes & placedCrossoverHeroes & keepHeroes;
  102. }
  103. };
  104. class DLL_LINKAGE CCampaign
  105. {
  106. public:
  107. CCampaignHeader header;
  108. std::vector<CCampaignScenario> scenarios;
  109. std::map<int, std::string > mapPieces; //binary h3ms, scenario number -> map data
  110. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  111. {
  112. h & header & scenarios & mapPieces;
  113. }
  114. bool conquerable(int whichScenario) const;
  115. CCampaign();
  116. };
  117. class DLL_LINKAGE CCampaignState
  118. {
  119. public:
  120. unique_ptr<CCampaign> camp;
  121. std::string campaignName;
  122. std::vector<ui8> mapsConquered, mapsRemaining;
  123. boost::optional<si32> currentMap;
  124. std::map<ui8, ui8> chosenCampaignBonuses;
  125. //void initNewCampaign(const StartInfo &si);
  126. void setCurrentMapAsConquered(const std::vector<CGHeroInstance*> & heroes);
  127. boost::optional<CScenarioTravel::STravelBonus> getBonusForCurrentMap() const;
  128. const CCampaignScenario & getCurrentScenario() const;
  129. CCampaignScenario & getCurrentScenario();
  130. ui8 currentBonusID() const;
  131. CCampaignState();
  132. CCampaignState(unique_ptr<CCampaign> _camp);
  133. ~CCampaignState(){};
  134. template <typename Handler> void serialize(Handler &h, const int version)
  135. {
  136. h & camp & campaignName & mapsRemaining & mapsConquered & currentMap;
  137. h & chosenCampaignBonuses;
  138. }
  139. };
  140. class DLL_LINKAGE CCampaignHandler
  141. {
  142. static CCampaignHeader readHeaderFromMemory(CBinaryReader & reader);
  143. static CCampaignScenario readScenarioFromMemory(CBinaryReader & reader, int version, int mapVersion );
  144. static CScenarioTravel readScenarioTravelFromMemory(CBinaryReader & reader, int version);
  145. /// returns h3c split in parts. 0 = h3c header, 1-end - maps (binary h3m)
  146. /// headerOnly - only header will be decompressed, returned vector wont have any maps
  147. static std::vector< std::vector<ui8> > getFile(const std::string & name, bool headerOnly);
  148. public:
  149. static std::string prologVideoName(ui8 index);
  150. static std::string prologMusicName(ui8 index);
  151. static std::string prologVoiceName(ui8 index);
  152. static CCampaignHeader getHeader( const std::string & name); //name - name of appropriate file
  153. static unique_ptr<CCampaign> getCampaign(const std::string & name); //name - name of appropriate file
  154. };