CRmgTemplate.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. FORCE_PORTAL
  72. };
  73. enum class ERoadOption
  74. {
  75. ROAD_TRUE,
  76. ROAD_FALSE,
  77. ROAD_RANDOM
  78. };
  79. class DLL_LINKAGE ZoneConnection
  80. {
  81. public:
  82. ZoneConnection();
  83. TRmgTemplateZoneId getZoneA() const;
  84. TRmgTemplateZoneId getZoneB() const;
  85. TRmgTemplateZoneId getOtherZoneId(TRmgTemplateZoneId id) const;
  86. int getGuardStrength() const;
  87. rmg::EConnectionType getConnectionType() const;
  88. rmg::ERoadOption getRoadOption() const;
  89. void serializeJson(JsonSerializeFormat & handler);
  90. friend bool operator==(const ZoneConnection &, const ZoneConnection &);
  91. private:
  92. TRmgTemplateZoneId zoneA;
  93. TRmgTemplateZoneId zoneB;
  94. int guardStrength;
  95. rmg::EConnectionType connectionType;
  96. rmg::ERoadOption hasRoad;
  97. };
  98. class DLL_LINKAGE ZoneOptions
  99. {
  100. public:
  101. static const TRmgTemplateZoneId NO_ZONE;
  102. class DLL_LINKAGE CTownInfo
  103. {
  104. public:
  105. CTownInfo();
  106. int getTownCount() const;
  107. int getCastleCount() const;
  108. int getTownDensity() const;
  109. int getCastleDensity() const;
  110. void serializeJson(JsonSerializeFormat & handler);
  111. private:
  112. int townCount;
  113. int castleCount;
  114. int townDensity;
  115. int castleDensity;
  116. // TODO: Copy from another zone once its randomized
  117. TRmgTemplateZoneId sourceZone = NO_ZONE;
  118. };
  119. ZoneOptions();
  120. TRmgTemplateZoneId getId() const;
  121. void setId(TRmgTemplateZoneId value);
  122. ETemplateZoneType getType() const;
  123. void setType(ETemplateZoneType value);
  124. int getSize() const;
  125. void setSize(int value);
  126. std::optional<int> getOwner() const;
  127. std::set<TerrainId> getTerrainTypes() const;
  128. void setTerrainTypes(const std::set<TerrainId> & value);
  129. std::set<TerrainId> getDefaultTerrainTypes() const;
  130. const CTownInfo & getPlayerTowns() const;
  131. const CTownInfo & getNeutralTowns() const;
  132. std::set<FactionID> getDefaultTownTypes() const;
  133. std::set<FactionID> getTownTypes() const;
  134. std::set<FactionID> getMonsterTypes() const;
  135. void setTownTypes(const std::set<FactionID> & value);
  136. void setMonsterTypes(const std::set<FactionID> & value);
  137. void setMinesInfo(const std::map<TResource, ui16> & value);
  138. std::map<TResource, ui16> getMinesInfo() const;
  139. void setTreasureInfo(const std::vector<CTreasureInfo> & value);
  140. void addTreasureInfo(const CTreasureInfo & value);
  141. std::vector<CTreasureInfo> getTreasureInfo() const;
  142. ui32 getMaxTreasureValue() const;
  143. void recalculateMaxTreasureValue();
  144. TRmgTemplateZoneId getMinesLikeZone() const;
  145. TRmgTemplateZoneId getTerrainTypeLikeZone() const;
  146. TRmgTemplateZoneId getTreasureLikeZone() const;
  147. void addConnection(const ZoneConnection & connection);
  148. std::vector<ZoneConnection> getConnections() const;
  149. std::vector<TRmgTemplateZoneId> getConnectedZoneIds() const;
  150. void serializeJson(JsonSerializeFormat & handler);
  151. EMonsterStrength::EMonsterStrength monsterStrength;
  152. bool areTownsSameType() const;
  153. bool isMatchTerrainToTown() const;
  154. // Get a group of configured objects
  155. const std::vector<CompoundMapObjectID> & getBannedObjects() const;
  156. const std::vector<ObjectConfig::EObjectCategory> & getBannedObjectCategories() const;
  157. const std::vector<ObjectInfo> & getConfiguredObjects() const;
  158. // Copy whole custom object config from another zone
  159. ObjectConfig getCustomObjects() const;
  160. void setCustomObjects(const ObjectConfig & value);
  161. TRmgTemplateZoneId getCustomObjectsLikeZone() const;
  162. protected:
  163. TRmgTemplateZoneId id;
  164. ETemplateZoneType type;
  165. int size;
  166. ui32 maxTreasureValue;
  167. std::optional<int> owner;
  168. ObjectConfig objectConfig;
  169. CTownInfo playerTowns;
  170. CTownInfo neutralTowns;
  171. bool matchTerrainToTown;
  172. std::set<TerrainId> terrainTypes;
  173. std::set<TerrainId> bannedTerrains;
  174. bool townsAreSameType;
  175. std::set<FactionID> townTypes;
  176. std::set<FactionID> bannedTownTypes;
  177. std::set<FactionID> monsterTypes;
  178. std::set<FactionID> bannedMonsters;
  179. std::map<TResource, ui16> mines; //obligatory mines to spawn in this zone
  180. std::vector<CTreasureInfo> treasureInfo;
  181. std::vector<TRmgTemplateZoneId> connectedZoneIds; //list of adjacent zone ids
  182. std::vector<ZoneConnection> connectionDetails; //list of connections linked to that zone
  183. TRmgTemplateZoneId minesLikeZone;
  184. TRmgTemplateZoneId terrainTypeLikeZone;
  185. TRmgTemplateZoneId treasureLikeZone;
  186. TRmgTemplateZoneId customObjectsLikeZone;
  187. };
  188. }
  189. /// The CRmgTemplate describes a random map template.
  190. class DLL_LINKAGE CRmgTemplate : boost::noncopyable
  191. {
  192. public:
  193. using Zones = std::map<TRmgTemplateZoneId, std::shared_ptr<rmg::ZoneOptions>>;
  194. class DLL_LINKAGE CPlayerCountRange
  195. {
  196. public:
  197. void addRange(int lower, int upper);
  198. void addNumber(int value);
  199. bool isInRange(int count) const;
  200. std::set<int> getNumbers() const;
  201. std::string toString() const;
  202. void fromString(const std::string & value);
  203. int maxValue() const;
  204. int minValue() const;
  205. private:
  206. std::vector<std::pair<int, int> > range;
  207. };
  208. CRmgTemplate();
  209. ~CRmgTemplate();
  210. bool matchesSize(const int3 & value) const;
  211. bool isWaterContentAllowed(EWaterContent::EWaterContent waterContent) const;
  212. const std::set<EWaterContent::EWaterContent> & getWaterContentAllowed() const;
  213. void setId(const std::string & value);
  214. void setName(const std::string & value);
  215. const std::string & getId() const;
  216. const std::string & getName() const;
  217. const std::string & getDescription() const;
  218. const CPlayerCountRange & getPlayers() const;
  219. const CPlayerCountRange & getHumanPlayers() const;
  220. std::pair<int3, int3> getMapSizes() const;
  221. const Zones & getZones() const;
  222. const JsonNode & getMapSettings() const;
  223. const std::vector<rmg::ZoneConnection> & getConnectedZoneIds() const;
  224. void validate() const; /// Tests template on validity and throws exception on failure
  225. void serializeJson(JsonSerializeFormat & handler);
  226. void afterLoad();
  227. private:
  228. std::string id;
  229. std::string name;
  230. std::string description;
  231. int3 minSize;
  232. int3 maxSize;
  233. CPlayerCountRange players;
  234. CPlayerCountRange humanPlayers;
  235. Zones zones;
  236. std::vector<rmg::ZoneConnection> connectedZoneIds;
  237. std::set<EWaterContent::EWaterContent> allowedWaterContent;
  238. std::unique_ptr<JsonNode> mapSettings;
  239. std::set<TerrainId> inheritTerrainType(std::shared_ptr<rmg::ZoneOptions> zone, uint32_t iteration = 0);
  240. std::map<TResource, ui16> inheritMineTypes(std::shared_ptr<rmg::ZoneOptions> zone, uint32_t iteration = 0);
  241. std::vector<CTreasureInfo> inheritTreasureInfo(std::shared_ptr<rmg::ZoneOptions> zone, uint32_t iteration = 0);
  242. // TODO: Copy custom object settings
  243. // TODO: Copy town type after source town is actually randomized
  244. void serializeSize(JsonSerializeFormat & handler, int3 & value, const std::string & fieldName);
  245. void serializePlayers(JsonSerializeFormat & handler, CPlayerCountRange & value, const std::string & fieldName);
  246. template<typename T>
  247. T inheritZoneProperty(std::shared_ptr<rmg::ZoneOptions> zone,
  248. T (rmg::ZoneOptions::*getter)() const,
  249. void (rmg::ZoneOptions::*setter)(const T&),
  250. TRmgTemplateZoneId (rmg::ZoneOptions::*inheritFrom)() const,
  251. const std::string& propertyString,
  252. uint32_t iteration = 0);
  253. };
  254. VCMI_LIB_NAMESPACE_END