CRmgTemplate.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. namespace ETemplateZoneType
  17. {
  18. enum ETemplateZoneType
  19. {
  20. PLAYER_START,
  21. CPU_START,
  22. TREASURE,
  23. JUNCTION,
  24. WATER
  25. };
  26. }
  27. namespace EWaterContent
  28. {
  29. enum EWaterContent
  30. {
  31. RANDOM = -1,
  32. NONE,
  33. NORMAL,
  34. ISLANDS
  35. };
  36. }
  37. namespace EMonsterStrength
  38. {
  39. enum EMonsterStrength
  40. {
  41. RANDOM = -2,
  42. ZONE_WEAK = -1,
  43. ZONE_NORMAL = 0,
  44. ZONE_STRONG = 1,
  45. GLOBAL_WEAK = 2,
  46. GLOBAL_NORMAL = 3,
  47. GLOBAL_STRONG = 4
  48. };
  49. }
  50. class DLL_LINKAGE CTreasureInfo
  51. {
  52. public:
  53. ui32 min;
  54. ui32 max;
  55. ui16 density;
  56. CTreasureInfo();
  57. CTreasureInfo(ui32 min, ui32 max, ui16 density);
  58. bool operator ==(const CTreasureInfo & other) const;
  59. void serializeJson(JsonSerializeFormat & handler);
  60. };
  61. namespace rmg
  62. {
  63. class DLL_LINKAGE ZoneConnection
  64. {
  65. public:
  66. ZoneConnection();
  67. TRmgTemplateZoneId getZoneA() const;
  68. TRmgTemplateZoneId getZoneB() const;
  69. int getGuardStrength() const;
  70. void serializeJson(JsonSerializeFormat & handler);
  71. friend bool operator==(const ZoneConnection &, const ZoneConnection &);
  72. private:
  73. TRmgTemplateZoneId zoneA;
  74. TRmgTemplateZoneId zoneB;
  75. int guardStrength;
  76. };
  77. class DLL_LINKAGE ZoneOptions
  78. {
  79. public:
  80. static const TRmgTemplateZoneId NO_ZONE;
  81. class DLL_LINKAGE CTownInfo
  82. {
  83. public:
  84. CTownInfo();
  85. int getTownCount() const;
  86. int getCastleCount() const;
  87. int getTownDensity() const;
  88. int getCastleDensity() const;
  89. void serializeJson(JsonSerializeFormat & handler);
  90. private:
  91. int townCount;
  92. int castleCount;
  93. int townDensity;
  94. int castleDensity;
  95. };
  96. ZoneOptions();
  97. TRmgTemplateZoneId getId() const;
  98. void setId(TRmgTemplateZoneId value);
  99. ETemplateZoneType::ETemplateZoneType getType() const;
  100. void setType(ETemplateZoneType::ETemplateZoneType value);
  101. int getSize() const;
  102. void setSize(int value);
  103. boost::optional<int> getOwner() const;
  104. const std::set<TerrainId> & getTerrainTypes() const;
  105. void setTerrainTypes(const std::set<TerrainId> & value);
  106. const CTownInfo & getPlayerTowns() const;
  107. const CTownInfo & getNeutralTowns() const;
  108. std::set<TFaction> getDefaultTownTypes() const;
  109. const std::set<TFaction> & getTownTypes() const;
  110. const std::set<TFaction> & getMonsterTypes() const;
  111. void setTownTypes(const std::set<TFaction> & value);
  112. void setMonsterTypes(const std::set<TFaction> & value);
  113. void setMinesInfo(const std::map<TResource, ui16> & value);
  114. std::map<TResource, ui16> getMinesInfo() const;
  115. void setTreasureInfo(const std::vector<CTreasureInfo> & value);
  116. void addTreasureInfo(const CTreasureInfo & value);
  117. const std::vector<CTreasureInfo> & getTreasureInfo() const;
  118. TRmgTemplateZoneId getMinesLikeZone() const;
  119. TRmgTemplateZoneId getTerrainTypeLikeZone() const;
  120. TRmgTemplateZoneId getTreasureLikeZone() const;
  121. void addConnection(TRmgTemplateZoneId otherZone);
  122. std::vector<TRmgTemplateZoneId> getConnections() const;
  123. void serializeJson(JsonSerializeFormat & handler);
  124. EMonsterStrength::EMonsterStrength zoneMonsterStrength;
  125. bool areTownsSameType() const;
  126. bool isMatchTerrainToTown() const;
  127. protected:
  128. TRmgTemplateZoneId id;
  129. ETemplateZoneType::ETemplateZoneType type;
  130. int size;
  131. boost::optional<int> owner;
  132. CTownInfo playerTowns;
  133. CTownInfo neutralTowns;
  134. bool matchTerrainToTown;
  135. std::set<TerrainId> terrainTypes;
  136. bool townsAreSameType;
  137. std::set<TFaction> townTypes;
  138. std::set<TFaction> monsterTypes;
  139. std::map<TResource, ui16> mines; //obligatory mines to spawn in this zone
  140. std::vector<CTreasureInfo> treasureInfo;
  141. std::vector<TRmgTemplateZoneId> connections; //list of adjacent zones
  142. TRmgTemplateZoneId minesLikeZone;
  143. TRmgTemplateZoneId terrainTypeLikeZone;
  144. TRmgTemplateZoneId treasureLikeZone;
  145. };
  146. }
  147. /// The CRmgTemplate describes a random map template.
  148. class DLL_LINKAGE CRmgTemplate
  149. {
  150. public:
  151. using Zones = std::map<TRmgTemplateZoneId, std::shared_ptr<rmg::ZoneOptions>>;
  152. class DLL_LINKAGE CPlayerCountRange
  153. {
  154. public:
  155. void addRange(int lower, int upper);
  156. void addNumber(int value);
  157. bool isInRange(int count) const;
  158. std::set<int> getNumbers() const;
  159. std::string toString() const;
  160. void fromString(const std::string & value);
  161. private:
  162. std::vector<std::pair<int, int> > range;
  163. };
  164. CRmgTemplate();
  165. bool matchesSize(const int3 & value) const;
  166. bool isWaterContentAllowed(EWaterContent::EWaterContent waterContent) const;
  167. const std::set<EWaterContent::EWaterContent> & getWaterContentAllowed() const;
  168. void setId(const std::string & value);
  169. void setName(const std::string & value);
  170. const std::string & getId() const;
  171. const std::string & getName() const;
  172. const CPlayerCountRange & getPlayers() const;
  173. const CPlayerCountRange & getCpuPlayers() const;
  174. std::pair<int3, int3> getMapSizes() const;
  175. const Zones & getZones() const;
  176. const std::vector<rmg::ZoneConnection> & getConnections() const;
  177. void validate() const; /// Tests template on validity and throws exception on failure
  178. void serializeJson(JsonSerializeFormat & handler);
  179. private:
  180. std::string id;
  181. std::string name;
  182. int3 minSize, maxSize;
  183. CPlayerCountRange players, cpuPlayers;
  184. Zones zones;
  185. std::vector<rmg::ZoneConnection> connections;
  186. std::set<EWaterContent::EWaterContent> allowedWaterContent;
  187. void afterLoad();
  188. std::set<TerrainId> inheritTerrainType(std::shared_ptr<rmg::ZoneOptions> zone, uint32_t iteration = 0);
  189. std::map<TResource, ui16> inheritMineTypes(std::shared_ptr<rmg::ZoneOptions> zone, uint32_t iteration = 0);
  190. std::vector<CTreasureInfo> inheritTreasureInfo(std::shared_ptr<rmg::ZoneOptions> zone, uint32_t iteration = 0);
  191. void serializeSize(JsonSerializeFormat & handler, int3 & value, const std::string & fieldName);
  192. void serializePlayers(JsonSerializeFormat & handler, CPlayerCountRange & value, const std::string & fieldName);
  193. };
  194. VCMI_LIB_NAMESPACE_END