MapEditUtils.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * MapEditUtils.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. VCMI_LIB_NAMESPACE_BEGIN
  14. class CGObjectInstance;
  15. class CMap;
  16. /// Represents a map rectangle.
  17. struct DLL_LINKAGE MapRect
  18. {
  19. MapRect();
  20. MapRect(const int3 & pos, si32 width, si32 height);
  21. si32 x;
  22. si32 y;
  23. si32 z;
  24. si32 width;
  25. si32 height;
  26. si32 left() const;
  27. si32 right() const;
  28. si32 top() const;
  29. si32 bottom() const;
  30. int3 topLeft() const; /// Top left corner of this rect.
  31. int3 topRight() const; /// Top right corner of this rect.
  32. int3 bottomLeft() const; /// Bottom left corner of this rect.
  33. int3 bottomRight() const; /// Bottom right corner of this rect.
  34. /// Returns a MapRect of the intersection of this rectangle and the given one.
  35. MapRect operator&(const MapRect& rect) const;
  36. template<typename Func>
  37. void forEach(Func f) const
  38. {
  39. for(int j = y; j < bottom(); ++j)
  40. {
  41. for(int i = x; i < right(); ++i)
  42. {
  43. f(int3(i, j, z));
  44. }
  45. }
  46. }
  47. };
  48. /// Generic selection class to select any type
  49. template<typename T>
  50. class DLL_LINKAGE CMapSelection
  51. {
  52. public:
  53. explicit CMapSelection(CMap* map) : map(map) { }
  54. virtual ~CMapSelection() = default;
  55. void select(const T & item)
  56. {
  57. selectedItems.insert(item);
  58. }
  59. void deselect(const T & item)
  60. {
  61. selectedItems.erase(item);
  62. }
  63. std::set<T> getSelectedItems()
  64. {
  65. return selectedItems;
  66. }
  67. CMap* getMap() { return map; }
  68. virtual void selectRange(const MapRect & rect) { }
  69. virtual void deselectRange(const MapRect & rect) { }
  70. virtual void selectAll() { }
  71. virtual void clearSelection() { }
  72. private:
  73. std::set<T> selectedItems;
  74. CMap* map;
  75. };
  76. /// Selection class to select terrain.
  77. class DLL_LINKAGE CTerrainSelection : public CMapSelection<int3>
  78. {
  79. public:
  80. explicit CTerrainSelection(CMap * map);
  81. void selectRange(const MapRect & rect) override;
  82. void deselectRange(const MapRect & rect) override;
  83. void selectAll() override;
  84. void clearSelection() override;
  85. void setSelection(const std::vector<int3> & vec);
  86. };
  87. /// Selection class to select objects.
  88. class DLL_LINKAGE CObjectSelection : public CMapSelection<CGObjectInstance *>
  89. {
  90. public:
  91. explicit CObjectSelection(CMap * map);
  92. };
  93. /// The terrain view pattern describes a specific composition of terrain tiles
  94. /// in a 3x3 matrix and notes which terrain view frame numbers can be used.
  95. struct DLL_LINKAGE TerrainViewPattern
  96. {
  97. struct WeightedRule
  98. {
  99. WeightedRule(std::string& Name);
  100. /// Gets true if this rule is a standard rule which means that it has a value of one of the RULE_* constants.
  101. inline bool isStandardRule() const
  102. {
  103. return standardRule;
  104. }
  105. inline bool isAnyRule() const
  106. {
  107. return anyRule;
  108. }
  109. inline bool isDirtRule() const
  110. {
  111. return dirtRule;
  112. }
  113. inline bool isSandRule() const
  114. {
  115. return sandRule;
  116. }
  117. inline bool isTransition() const
  118. {
  119. return transitionRule;
  120. }
  121. inline bool isNativeStrong() const
  122. {
  123. return nativeStrongRule;
  124. }
  125. inline bool isNativeRule() const
  126. {
  127. return nativeRule;
  128. }
  129. void setNative();
  130. /// The name of the rule. Can be any value of the RULE_* constants or a ID of a another pattern.
  131. //FIXME: remove string variable altogether, use only in constructor
  132. std::string name;
  133. /// Optional. A rule can have points. Patterns may have a minimum count of points to reach to be successful.
  134. int points;
  135. private:
  136. bool standardRule;
  137. bool anyRule;
  138. bool dirtRule;
  139. bool sandRule;
  140. bool transitionRule;
  141. bool nativeStrongRule;
  142. bool nativeRule;
  143. WeightedRule(); //only allow string constructor
  144. };
  145. static const int PATTERN_DATA_SIZE = 9;
  146. /// Constant for the flip mode different images. Pattern will be flipped and different images will be used(mapping area is divided into 4 parts)
  147. static const std::string FLIP_MODE_DIFF_IMAGES;
  148. /// Constant for the rule dirt, meaning a dirty border is required.
  149. static const std::string RULE_DIRT;
  150. /// Constant for the rule sand, meaning a sandy border is required.
  151. static const std::string RULE_SAND;
  152. /// Constant for the rule transition, meaning a dirty OR sandy border is required.
  153. static const std::string RULE_TRANSITION;
  154. /// Constant for the rule native, meaning a native border is required.
  155. static const std::string RULE_NATIVE;
  156. /// Constant for the rule native strong, meaning a native type is required.
  157. static const std::string RULE_NATIVE_STRONG;
  158. /// Constant for the rule any, meaning a native type, dirty OR sandy border is required.
  159. static const std::string RULE_ANY;
  160. TerrainViewPattern();
  161. /// The pattern data can be visualized as a 3x3 matrix:
  162. /// [ ][ ][ ]
  163. /// [ ][ ][ ]
  164. /// [ ][ ][ ]
  165. ///
  166. /// The box in the center belongs always to the native terrain type and
  167. /// is the point of origin. Depending on the terrain type different rules
  168. /// can be used. Their meaning differs also from type to type.
  169. ///
  170. /// std::vector -> several rules can be used in one cell
  171. std::array<std::vector<WeightedRule>, PATTERN_DATA_SIZE> data;
  172. /// The identifier of the pattern, if it's referenced from a another pattern.
  173. std::string id;
  174. /// This describes the mapping between this pattern and the corresponding range of frames
  175. /// which should be used for the ter view.
  176. ///
  177. /// std::vector -> size=1: typical, size=2: if this pattern should map to two different types of borders
  178. /// std::pair -> 1st value: lower range, 2nd value: upper range
  179. std::vector<std::pair<int, int> > mapping;
  180. /// If diffImages is true, different images/frames are used to place a rotated terrain view. If it's false
  181. /// the same frame will be used and rotated.
  182. bool diffImages;
  183. /// If true, then this pattern describes decoration tiles and should be used with specified probability
  184. bool decoration;
  185. /// The rotationTypesCount is only used if diffImages is true and holds the number how many rotation types(horizontal, etc...)
  186. /// are supported.
  187. int rotationTypesCount;
  188. /// The minimum and maximum points to reach to validate the pattern successfully.
  189. int minPoints;
  190. int maxPoints;
  191. };
  192. /// The terrain view pattern config loads pattern data from the filesystem.
  193. class DLL_LINKAGE CTerrainViewPatternConfig : public boost::noncopyable
  194. {
  195. public:
  196. using TVPVector = std::vector<TerrainViewPattern>;
  197. CTerrainViewPatternConfig();
  198. const std::vector<TVPVector> & getTerrainViewPatterns(TerrainId terrain) const;
  199. std::optional<const std::reference_wrapper<const TerrainViewPattern>> getTerrainViewPatternById(const std::string & patternId, const std::string & id) const;
  200. std::optional<const std::reference_wrapper<const CTerrainViewPatternConfig::TVPVector>> getTerrainViewPatternsById(TerrainId terrain, const std::string & id) const;
  201. const TVPVector * getTerrainTypePatternById(const std::string & id) const;
  202. void flipPattern(TerrainViewPattern & pattern, int flip) const;
  203. private:
  204. std::map<std::string, std::vector<TVPVector> > terrainViewPatterns;
  205. std::map<std::string, TVPVector> terrainTypePatterns;
  206. };
  207. class DLL_LINKAGE CTerrainViewPatternUtils
  208. {
  209. public:
  210. static void printDebuggingInfoAboutTile(const CMap * map, const int3 & pos);
  211. };
  212. VCMI_LIB_NAMESPACE_END