MapEditUtils.h 7.1 KB

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