CRmgTemplate.h 5.7 KB

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