CRmgTemplate.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. std::set<TerrainId> getDefaultTerrainTypes() const;
  118. const CTownInfo & getPlayerTowns() const;
  119. const CTownInfo & getNeutralTowns() const;
  120. std::set<FactionID> getDefaultTownTypes() const;
  121. const std::set<FactionID> getTownTypes() const;
  122. const std::set<FactionID> getMonsterTypes() const;
  123. void setTownTypes(const std::set<FactionID> & value);
  124. void setMonsterTypes(const std::set<FactionID> & value);
  125. void setMinesInfo(const std::map<TResource, ui16> & value);
  126. std::map<TResource, ui16> getMinesInfo() const;
  127. void setTreasureInfo(const std::vector<CTreasureInfo> & value);
  128. void addTreasureInfo(const CTreasureInfo & value);
  129. const std::vector<CTreasureInfo> & getTreasureInfo() const;
  130. ui32 getMaxTreasureValue() const;
  131. void recalculateMaxTreasureValue();
  132. TRmgTemplateZoneId getMinesLikeZone() const;
  133. TRmgTemplateZoneId getTerrainTypeLikeZone() const;
  134. TRmgTemplateZoneId getTreasureLikeZone() const;
  135. void addConnection(const ZoneConnection & connection);
  136. std::vector<ZoneConnection> getConnections() const;
  137. std::vector<TRmgTemplateZoneId> getConnectedZoneIds() const;
  138. void serializeJson(JsonSerializeFormat & handler);
  139. EMonsterStrength::EMonsterStrength monsterStrength;
  140. bool areTownsSameType() const;
  141. bool isMatchTerrainToTown() const;
  142. protected:
  143. TRmgTemplateZoneId id;
  144. ETemplateZoneType type;
  145. int size;
  146. ui32 maxTreasureValue;
  147. std::optional<int> owner;
  148. CTownInfo playerTowns;
  149. CTownInfo neutralTowns;
  150. bool matchTerrainToTown;
  151. std::set<TerrainId> terrainTypes;
  152. std::set<TerrainId> bannedTerrains;
  153. bool townsAreSameType;
  154. std::set<FactionID> townTypes;
  155. std::set<FactionID> bannedTownTypes;
  156. std::set<FactionID> monsterTypes;
  157. std::set<FactionID> bannedMonsters;
  158. std::map<TResource, ui16> mines; //obligatory mines to spawn in this zone
  159. std::vector<CTreasureInfo> treasureInfo;
  160. std::vector<TRmgTemplateZoneId> connectedZoneIds; //list of adjacent zone ids
  161. std::vector<ZoneConnection> connectionDetails; //list of connections linked to that zone
  162. TRmgTemplateZoneId minesLikeZone;
  163. TRmgTemplateZoneId terrainTypeLikeZone;
  164. TRmgTemplateZoneId treasureLikeZone;
  165. };
  166. }
  167. /// The CRmgTemplate describes a random map template.
  168. class DLL_LINKAGE CRmgTemplate
  169. {
  170. public:
  171. using Zones = std::map<TRmgTemplateZoneId, std::shared_ptr<rmg::ZoneOptions>>;
  172. class DLL_LINKAGE CPlayerCountRange
  173. {
  174. public:
  175. void addRange(int lower, int upper);
  176. void addNumber(int value);
  177. bool isInRange(int count) const;
  178. std::set<int> getNumbers() const;
  179. std::string toString() const;
  180. void fromString(const std::string & value);
  181. private:
  182. std::vector<std::pair<int, int> > range;
  183. };
  184. CRmgTemplate();
  185. bool matchesSize(const int3 & value) const;
  186. bool isWaterContentAllowed(EWaterContent::EWaterContent waterContent) const;
  187. const std::set<EWaterContent::EWaterContent> & getWaterContentAllowed() const;
  188. void setId(const std::string & value);
  189. void setName(const std::string & value);
  190. const std::string & getId() const;
  191. const std::string & getName() const;
  192. const CPlayerCountRange & getPlayers() const;
  193. const CPlayerCountRange & getCpuPlayers() const;
  194. std::pair<int3, int3> getMapSizes() const;
  195. const Zones & getZones() const;
  196. const std::vector<rmg::ZoneConnection> & getConnectedZoneIds() const;
  197. void validate() const; /// Tests template on validity and throws exception on failure
  198. void serializeJson(JsonSerializeFormat & handler);
  199. void afterLoad();
  200. private:
  201. std::string id;
  202. std::string name;
  203. int3 minSize, maxSize;
  204. CPlayerCountRange players, cpuPlayers;
  205. Zones zones;
  206. std::vector<rmg::ZoneConnection> connectedZoneIds;
  207. std::set<EWaterContent::EWaterContent> allowedWaterContent;
  208. std::set<TerrainId> inheritTerrainType(std::shared_ptr<rmg::ZoneOptions> zone, uint32_t iteration = 0);
  209. std::map<TResource, ui16> inheritMineTypes(std::shared_ptr<rmg::ZoneOptions> zone, uint32_t iteration = 0);
  210. std::vector<CTreasureInfo> inheritTreasureInfo(std::shared_ptr<rmg::ZoneOptions> zone, uint32_t iteration = 0);
  211. void serializeSize(JsonSerializeFormat & handler, int3 & value, const std::string & fieldName);
  212. void serializePlayers(JsonSerializeFormat & handler, CPlayerCountRange & value, const std::string & fieldName);
  213. };
  214. VCMI_LIB_NAMESPACE_END