CRmgTemplate.h 5.0 KB

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