CCampaignHandler.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #pragma once
  2. /*
  3. * CCampaignHandler.h, part of VCMI engine
  4. *
  5. * Authors: listed in file AUTHORS in main folder
  6. *
  7. * License: GNU General Public License v2.0 or later
  8. * Full text of license available in license.txt file, in main folder
  9. *
  10. */
  11. struct StartInfo;
  12. class CGHeroInstance;
  13. namespace CampaignVersion
  14. {
  15. enum Version
  16. {
  17. RoE = 4,
  18. AB = 5,
  19. SoD = 6,
  20. WoG = 6
  21. };
  22. }
  23. class DLL_LINKAGE CCampaignHeader
  24. {
  25. public:
  26. si32 version; //4 - RoE, 5 - AB, 6 - SoD and WoG
  27. ui8 mapVersion; //CampText.txt's format
  28. std::string name, description;
  29. ui8 difficultyChoosenByPlayer;
  30. ui8 music; //CmpMusic.txt, start from 0
  31. std::string filename;
  32. ui8 loadFromLod; //if true, this campaign must be loaded fro, .lod file
  33. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  34. {
  35. h & version & mapVersion & name & description & difficultyChoosenByPlayer & music & filename & loadFromLod;
  36. }
  37. };
  38. class DLL_LINKAGE CScenarioTravel
  39. {
  40. public:
  41. ui8 whatHeroKeeps; //bitfield [0] - experience, [1] - prim skills, [2] - sec skills, [3] - spells, [4] - artifacts
  42. ui8 monstersKeptByHero[19];
  43. ui8 artifsKeptByHero[18];
  44. ui8 startOptions; //1 - start bonus, 2 - traveling hero, 3 - hero options
  45. ui8 playerColor; //only for startOptions == 1
  46. struct DLL_LINKAGE STravelBonus
  47. {
  48. enum EBonusType {SPELL, MONSTER, BUILDING, ARTIFACT, SPELL_SCROLL, PRIMARY_SKILL, SECONDARY_SKILL, RESOURCE,
  49. PLAYER_PREV_SCENARIO, HERO};
  50. ui8 type; //uses EBonusType
  51. si32 info1, info2, info3; //purpose depends on type
  52. bool isBonusForHero() const;
  53. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  54. {
  55. h & type & info1 & info2 & info3;
  56. }
  57. };
  58. std::vector<STravelBonus> bonusesToChoose;
  59. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  60. {
  61. h & whatHeroKeeps & monstersKeptByHero & artifsKeptByHero & startOptions & playerColor & bonusesToChoose;
  62. }
  63. };
  64. class DLL_LINKAGE CCampaignScenario
  65. {
  66. public:
  67. std::string mapName;
  68. ui32 packedMapSize; //generally not used
  69. std::set<ui8> preconditionRegions; //what we need to conquer to conquer this one (stored as bitfield in h3c)
  70. ui8 regionColor;
  71. ui8 difficulty;
  72. ui8 conquered;
  73. std::string regionText;
  74. struct DLL_LINKAGE SScenarioPrologEpilog
  75. {
  76. ui8 hasPrologEpilog;
  77. ui8 prologVideo; // from CmpMovie.txt
  78. ui8 prologMusic; // from CmpMusic.txt
  79. std::string prologText;
  80. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  81. {
  82. h & hasPrologEpilog & prologVideo & prologMusic & prologText;
  83. }
  84. };
  85. SScenarioPrologEpilog prolog, epilog;
  86. CScenarioTravel travelOptions;
  87. std::vector<CGHeroInstance*> crossoverHeroes;
  88. void loadPreconditionRegions(ui32 regions);
  89. void prepareCrossoverHeroes(std::vector<CGHeroInstance *> heroes);
  90. bool isNotVoid() const;
  91. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  92. {
  93. h & mapName & packedMapSize & preconditionRegions & regionColor & difficulty & conquered & regionText &
  94. prolog & epilog & travelOptions & crossoverHeroes;
  95. }
  96. };
  97. class DLL_LINKAGE CCampaign
  98. {
  99. public:
  100. CCampaignHeader header;
  101. std::vector<CCampaignScenario> scenarios;
  102. std::map<int, std::string > mapPieces; //binary h3ms, scenario number -> map data
  103. template <typename Handler> void serialize(Handler &h, const int formatVersion)
  104. {
  105. h & header & scenarios & mapPieces;
  106. }
  107. bool conquerable(int whichScenario) const;
  108. CCampaign();
  109. };
  110. class DLL_LINKAGE CCampaignState
  111. {
  112. public:
  113. unique_ptr<CCampaign> camp;
  114. std::string campaignName;
  115. std::vector<ui8> mapsConquered, mapsRemaining;
  116. ui8 currentMap;
  117. bmap<ui8, ui8> chosenCampaignBonuses; //used only for mode CAMPAIGN
  118. //void initNewCampaign(const StartInfo &si);
  119. void mapConquered(const std::vector<CGHeroInstance*> & heroes);
  120. boost::optional<CScenarioTravel::STravelBonus> getBonusForCurrentMap() const;
  121. const CCampaignScenario &getCurrentScenario() const;
  122. ui8 currentBonusID() const;
  123. CCampaignState();
  124. CCampaignState(unique_ptr<CCampaign> _camp);
  125. template <typename Handler> void serialize(Handler &h, const int version)
  126. {
  127. h & camp & campaignName & mapsRemaining & mapsConquered & currentMap;
  128. h & chosenCampaignBonuses;
  129. }
  130. };
  131. class DLL_LINKAGE CCampaignHandler
  132. {
  133. static CCampaignHeader readHeaderFromMemory( const ui8 *buffer, int & outIt );
  134. static CCampaignScenario readScenarioFromMemory( const ui8 *buffer, int & outIt, int version, int mapVersion );
  135. static CScenarioTravel readScenarioTravelFromMemory( const ui8 * buffer, int & outIt , int version);
  136. /// returns h3c split in parts. 0 = h3c header, 1-end - maps (binary h3m)
  137. /// headerOnly - only header will be decompressed, returned vector wont have any maps
  138. static std::vector< std::vector<ui8> > getFile(const std::string & name, bool headerOnly);
  139. public:
  140. static CCampaignHeader getHeader( const std::string & name); //name - name of appropriate file
  141. static unique_ptr<CCampaign> getCampaign(const std::string & name); //name - name of appropriate file
  142. };