CCampaignHandler.h 6.0 KB

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