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