CRmgTemplate.h 5.3 KB

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