CRmgTemplate.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * CRmgTemplate.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 "../int3.h"
  12. #include "../GameConstants.h"
  13. #include "../ResourceSet.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. class JsonSerializeFormat;
  16. enum class ETemplateZoneType
  17. {
  18. PLAYER_START,
  19. CPU_START,
  20. TREASURE,
  21. JUNCTION,
  22. WATER
  23. };
  24. namespace EWaterContent // Not enum class, because it's used in method RandomMapTab::setMapGenOptions
  25. { // defined in client\lobby\RandomMapTab.cpp and probably in other similar places
  26. enum EWaterContent // as an argument of CToggleGroup::setSelected(int id) from \client\widgets\Buttons.cpp
  27. {
  28. RANDOM = -1,
  29. NONE,
  30. NORMAL,
  31. ISLANDS
  32. };
  33. }
  34. namespace EMonsterStrength // used as int in monster generation procedure and in map description for the generated random map
  35. {
  36. enum EMonsterStrength
  37. {
  38. ZONE_NONE = -3,
  39. RANDOM = -2,
  40. ZONE_WEAK = -1,
  41. ZONE_NORMAL = 0,
  42. ZONE_STRONG = 1,
  43. GLOBAL_WEAK = 2,
  44. GLOBAL_NORMAL = 3,
  45. GLOBAL_STRONG = 4
  46. };
  47. }
  48. class DLL_LINKAGE CTreasureInfo
  49. {
  50. public:
  51. ui32 min;
  52. ui32 max;
  53. ui16 density;
  54. CTreasureInfo();
  55. CTreasureInfo(ui32 min, ui32 max, ui16 density);
  56. bool operator ==(const CTreasureInfo & other) const;
  57. void serializeJson(JsonSerializeFormat & handler);
  58. };
  59. namespace rmg
  60. {
  61. class DLL_LINKAGE ZoneConnection
  62. {
  63. public:
  64. ZoneConnection();
  65. TRmgTemplateZoneId getZoneA() const;
  66. TRmgTemplateZoneId getZoneB() const;
  67. int getGuardStrength() const;
  68. void serializeJson(JsonSerializeFormat & handler);
  69. friend bool operator==(const ZoneConnection &, const ZoneConnection &);
  70. private:
  71. TRmgTemplateZoneId zoneA;
  72. TRmgTemplateZoneId zoneB;
  73. int guardStrength;
  74. };
  75. class DLL_LINKAGE ZoneOptions
  76. {
  77. public:
  78. static const TRmgTemplateZoneId NO_ZONE;
  79. class DLL_LINKAGE CTownInfo
  80. {
  81. public:
  82. CTownInfo();
  83. int getTownCount() const;
  84. int getCastleCount() const;
  85. int getTownDensity() const;
  86. int getCastleDensity() const;
  87. void serializeJson(JsonSerializeFormat & handler);
  88. private:
  89. int townCount;
  90. int castleCount;
  91. int townDensity;
  92. int castleDensity;
  93. };
  94. ZoneOptions();
  95. TRmgTemplateZoneId getId() const;
  96. void setId(TRmgTemplateZoneId value);
  97. ETemplateZoneType getType() const;
  98. void setType(ETemplateZoneType value);
  99. int getSize() const;
  100. void setSize(int value);
  101. std::optional<int> getOwner() const;
  102. const std::set<TerrainId> & getTerrainTypes() const;
  103. void setTerrainTypes(const std::set<TerrainId> & value);
  104. const CTownInfo & getPlayerTowns() const;
  105. const CTownInfo & getNeutralTowns() const;
  106. std::set<FactionID> getDefaultTownTypes() const;
  107. const std::set<FactionID> & getTownTypes() const;
  108. const std::set<FactionID> & getMonsterTypes() const;
  109. void setTownTypes(const std::set<FactionID> & value);
  110. void setMonsterTypes(const std::set<FactionID> & value);
  111. void setMinesInfo(const std::map<TResource, ui16> & value);
  112. std::map<TResource, ui16> getMinesInfo() const;
  113. void setTreasureInfo(const std::vector<CTreasureInfo> & value);
  114. void addTreasureInfo(const CTreasureInfo & value);
  115. const std::vector<CTreasureInfo> & getTreasureInfo() const;
  116. ui32 getMaxTreasureValue() const;
  117. void recalculateMaxTreasureValue();
  118. TRmgTemplateZoneId getMinesLikeZone() const;
  119. TRmgTemplateZoneId getTerrainTypeLikeZone() const;
  120. TRmgTemplateZoneId getTreasureLikeZone() const;
  121. void addConnection(TRmgTemplateZoneId otherZone);
  122. std::vector<TRmgTemplateZoneId> getConnections() const;
  123. void serializeJson(JsonSerializeFormat & handler);
  124. EMonsterStrength::EMonsterStrength monsterStrength;
  125. bool areTownsSameType() const;
  126. bool isMatchTerrainToTown() const;
  127. protected:
  128. TRmgTemplateZoneId id;
  129. ETemplateZoneType type;
  130. int size;
  131. ui32 maxTreasureValue;
  132. std::optional<int> owner;
  133. CTownInfo playerTowns;
  134. CTownInfo neutralTowns;
  135. bool matchTerrainToTown;
  136. std::set<TerrainId> terrainTypes;
  137. bool townsAreSameType;
  138. std::set<FactionID> townTypes;
  139. std::set<FactionID> monsterTypes;
  140. std::map<TResource, ui16> mines; //obligatory mines to spawn in this zone
  141. std::vector<CTreasureInfo> treasureInfo;
  142. std::vector<TRmgTemplateZoneId> connections; //list of adjacent zones
  143. TRmgTemplateZoneId minesLikeZone;
  144. TRmgTemplateZoneId terrainTypeLikeZone;
  145. TRmgTemplateZoneId treasureLikeZone;
  146. };
  147. }
  148. /// The CRmgTemplate describes a random map template.
  149. class DLL_LINKAGE CRmgTemplate
  150. {
  151. public:
  152. using Zones = std::map<TRmgTemplateZoneId, std::shared_ptr<rmg::ZoneOptions>>;
  153. class DLL_LINKAGE CPlayerCountRange
  154. {
  155. public:
  156. void addRange(int lower, int upper);
  157. void addNumber(int value);
  158. bool isInRange(int count) const;
  159. std::set<int> getNumbers() const;
  160. std::string toString() const;
  161. void fromString(const std::string & value);
  162. private:
  163. std::vector<std::pair<int, int> > range;
  164. };
  165. CRmgTemplate();
  166. bool matchesSize(const int3 & value) const;
  167. bool isWaterContentAllowed(EWaterContent::EWaterContent waterContent) const;
  168. const std::set<EWaterContent::EWaterContent> & getWaterContentAllowed() const;
  169. void setId(const std::string & value);
  170. void setName(const std::string & value);
  171. const std::string & getId() const;
  172. const std::string & getName() const;
  173. const CPlayerCountRange & getPlayers() const;
  174. const CPlayerCountRange & getCpuPlayers() const;
  175. std::pair<int3, int3> getMapSizes() const;
  176. const Zones & getZones() const;
  177. const std::vector<rmg::ZoneConnection> & getConnections() const;
  178. void validate() const; /// Tests template on validity and throws exception on failure
  179. void serializeJson(JsonSerializeFormat & handler);
  180. private:
  181. std::string id;
  182. std::string name;
  183. int3 minSize, maxSize;
  184. CPlayerCountRange players, cpuPlayers;
  185. Zones zones;
  186. std::vector<rmg::ZoneConnection> connections;
  187. std::set<EWaterContent::EWaterContent> allowedWaterContent;
  188. void afterLoad();
  189. std::set<TerrainId> inheritTerrainType(std::shared_ptr<rmg::ZoneOptions> zone, uint32_t iteration = 0);
  190. std::map<TResource, ui16> inheritMineTypes(std::shared_ptr<rmg::ZoneOptions> zone, uint32_t iteration = 0);
  191. std::vector<CTreasureInfo> inheritTreasureInfo(std::shared_ptr<rmg::ZoneOptions> zone, uint32_t iteration = 0);
  192. void serializeSize(JsonSerializeFormat & handler, int3 & value, const std::string & fieldName);
  193. void serializePlayers(JsonSerializeFormat & handler, CPlayerCountRange & value, const std::string & fieldName);
  194. };
  195. VCMI_LIB_NAMESPACE_END