CRmgTemplate.h 9.3 KB

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