CRmgTemplate.h 11 KB

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