CRmgTemplate.h 5.7 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. ZoneOptions & operator=(const ZoneOptions & other);
  98. TRmgTemplateZoneId getId() const;
  99. void setId(TRmgTemplateZoneId value);
  100. ETemplateZoneType::ETemplateZoneType getType() const;
  101. void setType(ETemplateZoneType::ETemplateZoneType value);
  102. int getSize() const;
  103. void setSize(int value);
  104. boost::optional<int> getOwner() const;
  105. const std::set<TerrainId> & getTerrainTypes() const;
  106. void setTerrainTypes(const std::set<TerrainId> & value);
  107. const CTownInfo & getPlayerTowns() const;
  108. const CTownInfo & getNeutralTowns() const;
  109. std::set<TFaction> getDefaultTownTypes() const;
  110. const std::set<TFaction> & getTownTypes() const;
  111. const std::set<TFaction> & getMonsterTypes() const;
  112. void setTownTypes(const std::set<TFaction> & value);
  113. void setMonsterTypes(const std::set<TFaction> & value);
  114. void setMinesInfo(const std::map<TResource, ui16> & value);
  115. std::map<TResource, ui16> getMinesInfo() const;
  116. void setTreasureInfo(const std::vector<CTreasureInfo> & value);
  117. void addTreasureInfo(const CTreasureInfo & value);
  118. const std::vector<CTreasureInfo> & getTreasureInfo() const;
  119. TRmgTemplateZoneId getMinesLikeZone() const;
  120. TRmgTemplateZoneId getTerrainTypeLikeZone() const;
  121. TRmgTemplateZoneId getTreasureLikeZone() const;
  122. void addConnection(TRmgTemplateZoneId otherZone);
  123. std::vector<TRmgTemplateZoneId> getConnections() const;
  124. void serializeJson(JsonSerializeFormat & handler);
  125. EMonsterStrength::EMonsterStrength zoneMonsterStrength;
  126. bool areTownsSameType() const;
  127. bool isMatchTerrainToTown() const;
  128. protected:
  129. TRmgTemplateZoneId id;
  130. ETemplateZoneType::ETemplateZoneType type;
  131. int size;
  132. boost::optional<int> owner;
  133. CTownInfo playerTowns;
  134. CTownInfo neutralTowns;
  135. bool matchTerrainToTown;
  136. std::set<TerrainId> terrainTypes;
  137. bool townsAreSameType;
  138. std::set<TFaction> townTypes;
  139. std::set<TFaction> monsterTypes;
  140. std::map<TResource, ui16> mines; //obligatory mines to spawn in this zone
  141. std::vector<CTreasureInfo> treasureInfo;
  142. std::vector<TRmgTemplateZoneId> connections; //list of adjacent zones
  143. TRmgTemplateZoneId minesLikeZone;
  144. TRmgTemplateZoneId terrainTypeLikeZone;
  145. TRmgTemplateZoneId treasureLikeZone;
  146. };
  147. }
  148. /// The CRmgTemplate describes a random map template.
  149. class DLL_LINKAGE CRmgTemplate
  150. {
  151. public:
  152. using Zones = std::map<TRmgTemplateZoneId, std::shared_ptr<rmg::ZoneOptions>>;
  153. class DLL_LINKAGE CPlayerCountRange
  154. {
  155. public:
  156. void addRange(int lower, int upper);
  157. void addNumber(int value);
  158. bool isInRange(int count) const;
  159. std::set<int> getNumbers() const;
  160. std::string toString() const;
  161. void fromString(const std::string & value);
  162. private:
  163. std::vector<std::pair<int, int> > range;
  164. };
  165. CRmgTemplate();
  166. ~CRmgTemplate();
  167. bool matchesSize(const int3 & value) const;
  168. bool isWaterContentAllowed(EWaterContent::EWaterContent waterContent) const;
  169. const std::set<EWaterContent::EWaterContent> & getWaterContentAllowed() const;
  170. void setId(const std::string & value);
  171. void setName(const std::string & value);
  172. const std::string & getId() const;
  173. const std::string & getName() const;
  174. const CPlayerCountRange & getPlayers() const;
  175. const CPlayerCountRange & getCpuPlayers() const;
  176. std::pair<int3, int3> getMapSizes() const;
  177. const Zones & getZones() const;
  178. const std::vector<rmg::ZoneConnection> & getConnections() const;
  179. void validate() const; /// Tests template on validity and throws exception on failure
  180. void serializeJson(JsonSerializeFormat & handler);
  181. private:
  182. std::string id;
  183. std::string name;
  184. int3 minSize, maxSize;
  185. CPlayerCountRange players, cpuPlayers;
  186. Zones zones;
  187. std::vector<rmg::ZoneConnection> connections;
  188. std::set<EWaterContent::EWaterContent> allowedWaterContent;
  189. void afterLoad();
  190. void serializeSize(JsonSerializeFormat & handler, int3 & value, const std::string & fieldName);
  191. void serializePlayers(JsonSerializeFormat & handler, CPlayerCountRange & value, const std::string & fieldName);
  192. };
  193. VCMI_LIB_NAMESPACE_END