CRmgTemplate.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. #include "ObjectInfo.h"
  15. #include "ObjectConfig.h"
  16. #include "../mapObjectConstructors/CObjectClassesHandler.h"
  17. VCMI_LIB_NAMESPACE_BEGIN
  18. class JsonSerializeFormat;
  19. struct CompoundMapObjectID;
  20. enum class ETemplateZoneType
  21. {
  22. PLAYER_START,
  23. CPU_START,
  24. TREASURE,
  25. JUNCTION,
  26. WATER
  27. };
  28. namespace EWaterContent // Not enum class, because it's used in method RandomMapTab::setMapGenOptions
  29. { // defined in client\lobby\RandomMapTab.cpp and probably in other similar places
  30. enum EWaterContent // as an argument of CToggleGroup::setSelected(int id) from \client\widgets\Buttons.cpp
  31. {
  32. RANDOM = -1,
  33. NONE,
  34. NORMAL,
  35. ISLANDS
  36. };
  37. }
  38. namespace EMonsterStrength // used as int in monster generation procedure and in map description for the generated random map
  39. {
  40. enum EMonsterStrength
  41. {
  42. ZONE_NONE = -3,
  43. RANDOM = -2,
  44. ZONE_WEAK = -1,
  45. ZONE_NORMAL = 0,
  46. ZONE_STRONG = 1,
  47. GLOBAL_WEAK = 2,
  48. GLOBAL_NORMAL = 3,
  49. GLOBAL_STRONG = 4
  50. };
  51. }
  52. class DLL_LINKAGE CTreasureInfo
  53. {
  54. public:
  55. ui32 min;
  56. ui32 max;
  57. ui16 density;
  58. CTreasureInfo();
  59. CTreasureInfo(ui32 min, ui32 max, ui16 density);
  60. bool operator ==(const CTreasureInfo & other) const;
  61. void serializeJson(JsonSerializeFormat & handler);
  62. };
  63. namespace rmg
  64. {
  65. enum class EConnectionType
  66. {
  67. GUARDED = 0, //default
  68. FICTIVE,
  69. REPULSIVE,
  70. WIDE
  71. };
  72. enum class ERoadOption
  73. {
  74. ROAD_TRUE,
  75. ROAD_FALSE,
  76. ROAD_RANDOM
  77. };
  78. class DLL_LINKAGE ZoneConnection
  79. {
  80. public:
  81. ZoneConnection();
  82. TRmgTemplateZoneId getZoneA() const;
  83. TRmgTemplateZoneId getZoneB() const;
  84. TRmgTemplateZoneId getOtherZoneId(TRmgTemplateZoneId id) const;
  85. int getGuardStrength() const;
  86. rmg::EConnectionType getConnectionType() const;
  87. rmg::ERoadOption getRoadOption() const;
  88. void serializeJson(JsonSerializeFormat & handler);
  89. friend bool operator==(const ZoneConnection &, const ZoneConnection &);
  90. private:
  91. TRmgTemplateZoneId zoneA;
  92. TRmgTemplateZoneId zoneB;
  93. int guardStrength;
  94. rmg::EConnectionType connectionType;
  95. rmg::ERoadOption hasRoad;
  96. };
  97. class DLL_LINKAGE ZoneOptions
  98. {
  99. public:
  100. static const TRmgTemplateZoneId NO_ZONE;
  101. class DLL_LINKAGE CTownInfo
  102. {
  103. public:
  104. CTownInfo();
  105. int getTownCount() const;
  106. int getCastleCount() const;
  107. int getTownDensity() const;
  108. int getCastleDensity() const;
  109. void serializeJson(JsonSerializeFormat & handler);
  110. private:
  111. int townCount;
  112. int castleCount;
  113. int townDensity;
  114. int castleDensity;
  115. // TODO: Copy from another zone once its randomized
  116. TRmgTemplateZoneId sourceZone = NO_ZONE;
  117. };
  118. ZoneOptions();
  119. TRmgTemplateZoneId getId() const;
  120. void setId(TRmgTemplateZoneId value);
  121. ETemplateZoneType getType() const;
  122. void setType(ETemplateZoneType value);
  123. int getSize() const;
  124. void setSize(int value);
  125. std::optional<int> getOwner() const;
  126. std::set<TerrainId> getTerrainTypes() const;
  127. void setTerrainTypes(const std::set<TerrainId> & value);
  128. std::set<TerrainId> getDefaultTerrainTypes() const;
  129. const CTownInfo & getPlayerTowns() const;
  130. const CTownInfo & getNeutralTowns() const;
  131. std::set<FactionID> getDefaultTownTypes() const;
  132. std::set<FactionID> getTownTypes() const;
  133. std::set<FactionID> getMonsterTypes() const;
  134. void setTownTypes(const std::set<FactionID> & value);
  135. void setMonsterTypes(const std::set<FactionID> & value);
  136. void setMinesInfo(const std::map<TResource, ui16> & value);
  137. std::map<TResource, ui16> getMinesInfo() const;
  138. void setTreasureInfo(const std::vector<CTreasureInfo> & value);
  139. void addTreasureInfo(const CTreasureInfo & value);
  140. std::vector<CTreasureInfo> getTreasureInfo() const;
  141. ui32 getMaxTreasureValue() const;
  142. void recalculateMaxTreasureValue();
  143. TRmgTemplateZoneId getMinesLikeZone() const;
  144. TRmgTemplateZoneId getTerrainTypeLikeZone() const;
  145. TRmgTemplateZoneId getTreasureLikeZone() const;
  146. void addConnection(const ZoneConnection & connection);
  147. std::vector<ZoneConnection> getConnections() const;
  148. std::vector<TRmgTemplateZoneId> getConnectedZoneIds() const;
  149. void serializeJson(JsonSerializeFormat & handler);
  150. EMonsterStrength::EMonsterStrength monsterStrength;
  151. bool areTownsSameType() const;
  152. bool isMatchTerrainToTown() const;
  153. // Get a group of configured objects
  154. const std::vector<CompoundMapObjectID> & getBannedObjects() const;
  155. const std::vector<ObjectConfig::EObjectCategory> & getBannedObjectCategories() const;
  156. const std::vector<ObjectInfo> & getConfiguredObjects() const;
  157. // Copy whole custom object config from another zone
  158. ObjectConfig getCustomObjects() const;
  159. void setCustomObjects(const ObjectConfig & value);
  160. TRmgTemplateZoneId getCustomObjectsLikeZone() const;
  161. protected:
  162. TRmgTemplateZoneId id;
  163. ETemplateZoneType type;
  164. int size;
  165. ui32 maxTreasureValue;
  166. std::optional<int> owner;
  167. ObjectConfig objectConfig;
  168. CTownInfo playerTowns;
  169. CTownInfo neutralTowns;
  170. bool matchTerrainToTown;
  171. std::set<TerrainId> terrainTypes;
  172. std::set<TerrainId> bannedTerrains;
  173. bool townsAreSameType;
  174. std::set<FactionID> townTypes;
  175. std::set<FactionID> bannedTownTypes;
  176. std::set<FactionID> monsterTypes;
  177. std::set<FactionID> bannedMonsters;
  178. std::map<TResource, ui16> mines; //obligatory mines to spawn in this zone
  179. std::vector<CTreasureInfo> treasureInfo;
  180. std::vector<TRmgTemplateZoneId> connectedZoneIds; //list of adjacent zone ids
  181. std::vector<ZoneConnection> connectionDetails; //list of connections linked to that zone
  182. TRmgTemplateZoneId minesLikeZone;
  183. TRmgTemplateZoneId terrainTypeLikeZone;
  184. TRmgTemplateZoneId treasureLikeZone;
  185. TRmgTemplateZoneId customObjectsLikeZone;
  186. };
  187. }
  188. /// The CRmgTemplate describes a random map template.
  189. class DLL_LINKAGE CRmgTemplate
  190. {
  191. public:
  192. using Zones = std::map<TRmgTemplateZoneId, std::shared_ptr<rmg::ZoneOptions>>;
  193. class DLL_LINKAGE CPlayerCountRange
  194. {
  195. public:
  196. void addRange(int lower, int upper);
  197. void addNumber(int value);
  198. bool isInRange(int count) const;
  199. std::set<int> getNumbers() const;
  200. std::string toString() const;
  201. void fromString(const std::string & value);
  202. int maxValue() const;
  203. int minValue() const;
  204. private:
  205. std::vector<std::pair<int, int> > range;
  206. };
  207. CRmgTemplate();
  208. bool matchesSize(const int3 & value) const;
  209. bool isWaterContentAllowed(EWaterContent::EWaterContent waterContent) const;
  210. const std::set<EWaterContent::EWaterContent> & getWaterContentAllowed() const;
  211. void setId(const std::string & value);
  212. void setName(const std::string & value);
  213. const std::string & getId() const;
  214. const std::string & getName() const;
  215. const std::string & getDescription() const;
  216. const CPlayerCountRange & getPlayers() const;
  217. const CPlayerCountRange & getHumanPlayers() const;
  218. std::pair<int3, int3> getMapSizes() const;
  219. const Zones & getZones() const;
  220. const std::vector<rmg::ZoneConnection> & getConnectedZoneIds() const;
  221. void validate() const; /// Tests template on validity and throws exception on failure
  222. void serializeJson(JsonSerializeFormat & handler);
  223. void afterLoad();
  224. private:
  225. std::string id;
  226. std::string name;
  227. std::string description;
  228. int3 minSize;
  229. int3 maxSize;
  230. CPlayerCountRange players;
  231. CPlayerCountRange humanPlayers;
  232. Zones zones;
  233. std::vector<rmg::ZoneConnection> connectedZoneIds;
  234. std::set<EWaterContent::EWaterContent> allowedWaterContent;
  235. std::set<TerrainId> inheritTerrainType(std::shared_ptr<rmg::ZoneOptions> zone, uint32_t iteration = 0);
  236. std::map<TResource, ui16> inheritMineTypes(std::shared_ptr<rmg::ZoneOptions> zone, uint32_t iteration = 0);
  237. std::vector<CTreasureInfo> inheritTreasureInfo(std::shared_ptr<rmg::ZoneOptions> zone, uint32_t iteration = 0);
  238. // TODO: Copy custom object settings
  239. // TODO: Copy town type after source town is actually randomized
  240. void serializeSize(JsonSerializeFormat & handler, int3 & value, const std::string & fieldName);
  241. void serializePlayers(JsonSerializeFormat & handler, CPlayerCountRange & value, const std::string & fieldName);
  242. template<typename T>
  243. T inheritZoneProperty(std::shared_ptr<rmg::ZoneOptions> zone,
  244. T (rmg::ZoneOptions::*getter)() const,
  245. void (rmg::ZoneOptions::*setter)(const T&),
  246. TRmgTemplateZoneId (rmg::ZoneOptions::*inheritFrom)() const,
  247. const std::string& propertyString,
  248. uint32_t iteration = 0);
  249. };
  250. VCMI_LIB_NAMESPACE_END