CRmgTemplate.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 EConnectionType
  60. {
  61. enum class EConnectionType
  62. {
  63. GUARDED = 0, //default
  64. FICTIVE,
  65. REPULSIVE,
  66. WIDE
  67. };
  68. }
  69. namespace rmg
  70. {
  71. class DLL_LINKAGE ZoneConnection
  72. {
  73. public:
  74. ZoneConnection();
  75. TRmgTemplateZoneId getZoneA() const;
  76. TRmgTemplateZoneId getZoneB() const;
  77. TRmgTemplateZoneId getOtherZoneId(TRmgTemplateZoneId id) const;
  78. int getGuardStrength() const;
  79. EConnectionType::EConnectionType getConnectionType() const;
  80. void serializeJson(JsonSerializeFormat & handler);
  81. friend bool operator==(const ZoneConnection &, const ZoneConnection &);
  82. private:
  83. TRmgTemplateZoneId zoneA;
  84. TRmgTemplateZoneId zoneB;
  85. int guardStrength;
  86. EConnectionType::EConnectionType connectionType;
  87. };
  88. class DLL_LINKAGE ZoneOptions
  89. {
  90. public:
  91. static const TRmgTemplateZoneId NO_ZONE;
  92. class DLL_LINKAGE CTownInfo
  93. {
  94. public:
  95. CTownInfo();
  96. int getTownCount() const;
  97. int getCastleCount() const;
  98. int getTownDensity() const;
  99. int getCastleDensity() const;
  100. void serializeJson(JsonSerializeFormat & handler);
  101. private:
  102. int townCount;
  103. int castleCount;
  104. int townDensity;
  105. int castleDensity;
  106. };
  107. ZoneOptions();
  108. TRmgTemplateZoneId getId() const;
  109. void setId(TRmgTemplateZoneId value);
  110. ETemplateZoneType getType() const;
  111. void setType(ETemplateZoneType value);
  112. int getSize() const;
  113. void setSize(int value);
  114. std::optional<int> getOwner() const;
  115. const std::set<TerrainId> & getTerrainTypes() const;
  116. void setTerrainTypes(const std::set<TerrainId> & value);
  117. const CTownInfo & getPlayerTowns() const;
  118. const CTownInfo & getNeutralTowns() const;
  119. std::set<FactionID> getDefaultTownTypes() const;
  120. const std::set<FactionID> & getTownTypes() const;
  121. const std::set<FactionID> & getMonsterTypes() const;
  122. void setTownTypes(const std::set<FactionID> & value);
  123. void setMonsterTypes(const std::set<FactionID> & value);
  124. void setMinesInfo(const std::map<TResource, ui16> & value);
  125. std::map<TResource, ui16> getMinesInfo() const;
  126. void setTreasureInfo(const std::vector<CTreasureInfo> & value);
  127. void addTreasureInfo(const CTreasureInfo & value);
  128. const std::vector<CTreasureInfo> & getTreasureInfo() const;
  129. ui32 getMaxTreasureValue() const;
  130. void recalculateMaxTreasureValue();
  131. TRmgTemplateZoneId getMinesLikeZone() const;
  132. TRmgTemplateZoneId getTerrainTypeLikeZone() const;
  133. TRmgTemplateZoneId getTreasureLikeZone() const;
  134. void addConnection(const ZoneConnection & connection);
  135. std::vector<ZoneConnection> getConnections() const;
  136. std::vector<TRmgTemplateZoneId> getConnectedZoneIds() const;
  137. void serializeJson(JsonSerializeFormat & handler);
  138. EMonsterStrength::EMonsterStrength monsterStrength;
  139. bool areTownsSameType() const;
  140. bool isMatchTerrainToTown() const;
  141. protected:
  142. TRmgTemplateZoneId id;
  143. ETemplateZoneType type;
  144. int size;
  145. ui32 maxTreasureValue;
  146. std::optional<int> owner;
  147. CTownInfo playerTowns;
  148. CTownInfo neutralTowns;
  149. bool matchTerrainToTown;
  150. std::set<TerrainId> terrainTypes;
  151. bool townsAreSameType;
  152. std::set<FactionID> townTypes;
  153. std::set<FactionID> monsterTypes;
  154. std::map<TResource, ui16> mines; //obligatory mines to spawn in this zone
  155. std::vector<CTreasureInfo> treasureInfo;
  156. std::vector<TRmgTemplateZoneId> connectedZoneIds; //list of adjacent zone ids
  157. std::vector<ZoneConnection> connectionDetails; //list of connections linked to that zone
  158. TRmgTemplateZoneId minesLikeZone;
  159. TRmgTemplateZoneId terrainTypeLikeZone;
  160. TRmgTemplateZoneId treasureLikeZone;
  161. };
  162. }
  163. /// The CRmgTemplate describes a random map template.
  164. class DLL_LINKAGE CRmgTemplate
  165. {
  166. public:
  167. using Zones = std::map<TRmgTemplateZoneId, std::shared_ptr<rmg::ZoneOptions>>;
  168. class DLL_LINKAGE CPlayerCountRange
  169. {
  170. public:
  171. void addRange(int lower, int upper);
  172. void addNumber(int value);
  173. bool isInRange(int count) const;
  174. std::set<int> getNumbers() const;
  175. std::string toString() const;
  176. void fromString(const std::string & value);
  177. private:
  178. std::vector<std::pair<int, int> > range;
  179. };
  180. CRmgTemplate();
  181. bool matchesSize(const int3 & value) const;
  182. bool isWaterContentAllowed(EWaterContent::EWaterContent waterContent) const;
  183. const std::set<EWaterContent::EWaterContent> & getWaterContentAllowed() const;
  184. void setId(const std::string & value);
  185. void setName(const std::string & value);
  186. const std::string & getId() const;
  187. const std::string & getName() const;
  188. const CPlayerCountRange & getPlayers() const;
  189. const CPlayerCountRange & getCpuPlayers() const;
  190. std::pair<int3, int3> getMapSizes() const;
  191. const Zones & getZones() const;
  192. const std::vector<rmg::ZoneConnection> & getConnectedZoneIds() const;
  193. void validate() const; /// Tests template on validity and throws exception on failure
  194. void serializeJson(JsonSerializeFormat & handler);
  195. void afterLoad();
  196. private:
  197. std::string id;
  198. std::string name;
  199. int3 minSize, maxSize;
  200. CPlayerCountRange players, cpuPlayers;
  201. Zones zones;
  202. std::vector<rmg::ZoneConnection> connectedZoneIds;
  203. std::set<EWaterContent::EWaterContent> allowedWaterContent;
  204. std::set<TerrainId> inheritTerrainType(std::shared_ptr<rmg::ZoneOptions> zone, uint32_t iteration = 0);
  205. std::map<TResource, ui16> inheritMineTypes(std::shared_ptr<rmg::ZoneOptions> zone, uint32_t iteration = 0);
  206. std::vector<CTreasureInfo> inheritTreasureInfo(std::shared_ptr<rmg::ZoneOptions> zone, uint32_t iteration = 0);
  207. void serializeSize(JsonSerializeFormat & handler, int3 & value, const std::string & fieldName);
  208. void serializePlayers(JsonSerializeFormat & handler, CPlayerCountRange & value, const std::string & fieldName);
  209. };
  210. VCMI_LIB_NAMESPACE_END