2
0

CMapEditManager.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * CMapEditManager.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 "../CRandomGenerator.h"
  12. #include "CMap.h"
  13. class CGObjectInstance;
  14. class CTerrainViewPatternConfig;
  15. struct TerrainViewPattern;
  16. /// Represents a map rectangle.
  17. struct DLL_LINKAGE MapRect
  18. {
  19. MapRect();
  20. MapRect(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() { };
  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. };
  83. /// Selection class to select objects.
  84. class DLL_LINKAGE CObjectSelection: public CMapSelection<CGObjectInstance *>
  85. {
  86. public:
  87. explicit CObjectSelection(CMap * map);
  88. };
  89. /// The abstract base class CMapOperation defines an operation that can be executed, undone and redone.
  90. class DLL_LINKAGE CMapOperation : public boost::noncopyable
  91. {
  92. public:
  93. explicit CMapOperation(CMap * map);
  94. virtual ~CMapOperation() { };
  95. virtual void execute() = 0;
  96. virtual void undo() = 0;
  97. virtual void redo() = 0;
  98. virtual std::string getLabel() const = 0; /// Returns a display-able name of the operation.
  99. protected:
  100. CMap * map;
  101. };
  102. /// The CMapUndoManager provides the functionality to save operations and undo/redo them.
  103. class DLL_LINKAGE CMapUndoManager : boost::noncopyable
  104. {
  105. public:
  106. CMapUndoManager();
  107. void undo();
  108. void redo();
  109. void clearAll();
  110. /// The undo redo limit is a number which says how many undo/redo items can be saved. The default
  111. /// value is 10. If the value is 0, no undo/redo history will be maintained.
  112. int getUndoRedoLimit() const;
  113. void setUndoRedoLimit(int value);
  114. const CMapOperation * peekRedo() const;
  115. const CMapOperation * peekUndo() const;
  116. void addOperation(unique_ptr<CMapOperation> && operation); /// Client code does not need to call this method.
  117. private:
  118. typedef std::list<unique_ptr<CMapOperation> > TStack;
  119. void doOperation(TStack & fromStack, TStack & toStack, bool doUndo);
  120. const CMapOperation * peek(const TStack & stack) const;
  121. TStack undoStack;
  122. TStack redoStack;
  123. int undoRedoLimit;
  124. };
  125. /// The map edit manager provides functionality for drawing terrain and placing
  126. /// objects on the map.
  127. class DLL_LINKAGE CMapEditManager : boost::noncopyable
  128. {
  129. public:
  130. CMapEditManager(CMap * map);
  131. CMap * getMap();
  132. /// Clears the terrain. The free level is filled with water and the underground level with rock.
  133. void clearTerrain(CRandomGenerator * gen = nullptr);
  134. /// Draws terrain at the current terrain selection. The selection will be cleared automatically.
  135. void drawTerrain(ETerrainType terType, CRandomGenerator * gen = nullptr);
  136. void insertObject(CGObjectInstance * obj, const int3 & pos);
  137. CTerrainSelection & getTerrainSelection();
  138. CObjectSelection & getObjectSelection();
  139. CMapUndoManager & getUndoManager();
  140. private:
  141. void execute(unique_ptr<CMapOperation> && operation);
  142. CMap * map;
  143. CMapUndoManager undoManager;
  144. CRandomGenerator gen;
  145. CTerrainSelection terrainSel;
  146. CObjectSelection objectSel;
  147. };
  148. /* ---------------------------------------------------------------------------- */
  149. /* Implementation/Detail classes, Private API */
  150. /* ---------------------------------------------------------------------------- */
  151. /// The CComposedOperation is an operation which consists of several operations.
  152. class CComposedOperation : public CMapOperation
  153. {
  154. public:
  155. CComposedOperation(CMap * map);
  156. void execute() override;
  157. void undo() override;
  158. void redo() override;
  159. void addOperation(unique_ptr<CMapOperation> && operation);
  160. private:
  161. std::list<unique_ptr<CMapOperation> > operations;
  162. };
  163. namespace ETerrainGroup
  164. {
  165. enum ETerrainGroup
  166. {
  167. NORMAL,
  168. DIRT,
  169. SAND,
  170. WATER,
  171. ROCK
  172. };
  173. }
  174. /// The terrain view pattern describes a specific composition of terrain tiles
  175. /// in a 3x3 matrix and notes which terrain view frame numbers can be used.
  176. struct DLL_LINKAGE TerrainViewPattern
  177. {
  178. struct WeightedRule
  179. {
  180. WeightedRule();
  181. /// Gets true if this rule is a standard rule which means that it has a value of one of the RULE_* constants.
  182. bool isStandardRule() const;
  183. /// The name of the rule. Can be any value of the RULE_* constants or a ID of a another pattern.
  184. std::string name;
  185. /// Optional. A rule can have points. Patterns may have a minimum count of points to reach to be successful.
  186. int points;
  187. };
  188. static const int PATTERN_DATA_SIZE = 9;
  189. /// Constant for the flip mode different images. Pattern will be flipped and different images will be used(mapping area is divided into 4 parts)
  190. static const std::string FLIP_MODE_DIFF_IMAGES;
  191. /// Constant for the rule dirt, meaning a dirty border is required.
  192. static const std::string RULE_DIRT;
  193. /// Constant for the rule sand, meaning a sandy border is required.
  194. static const std::string RULE_SAND;
  195. /// Constant for the rule transition, meaning a dirty OR sandy border is required.
  196. static const std::string RULE_TRANSITION;
  197. /// Constant for the rule native, meaning a native border is required.
  198. static const std::string RULE_NATIVE;
  199. /// Constant for the rule native strong, meaning a native type is required.
  200. static const std::string RULE_NATIVE_STRONG;
  201. /// Constant for the rule any, meaning a native type, dirty OR sandy border is required.
  202. static const std::string RULE_ANY;
  203. TerrainViewPattern();
  204. /// The pattern data can be visualized as a 3x3 matrix:
  205. /// [ ][ ][ ]
  206. /// [ ][ ][ ]
  207. /// [ ][ ][ ]
  208. ///
  209. /// The box in the center belongs always to the native terrain type and
  210. /// is the point of origin. Depending on the terrain type different rules
  211. /// can be used. Their meaning differs also from type to type.
  212. ///
  213. /// std::vector -> several rules can be used in one cell
  214. std::array<std::vector<WeightedRule>, PATTERN_DATA_SIZE> data;
  215. /// The identifier of the pattern, if it's referenced from a another pattern.
  216. std::string id;
  217. /// This describes the mapping between this pattern and the corresponding range of frames
  218. /// which should be used for the ter view.
  219. ///
  220. /// std::vector -> size=1: typical, size=2: if this pattern should map to two different types of borders
  221. /// std::pair -> 1st value: lower range, 2nd value: upper range
  222. std::vector<std::pair<int, int> > mapping;
  223. /// If diffImages is true, different images/frames are used to place a rotated terrain view. If it's false
  224. /// the same frame will be used and rotated.
  225. bool diffImages;
  226. /// The rotationTypesCount is only used if diffImages is true and holds the number how many rotation types(horizontal, etc...)
  227. /// are supported.
  228. int rotationTypesCount;
  229. /// The minimum and maximum points to reach to validate the pattern successfully.
  230. int minPoints, maxPoints;
  231. };
  232. /// The terrain view pattern config loads pattern data from the filesystem.
  233. class DLL_LINKAGE CTerrainViewPatternConfig : public boost::noncopyable
  234. {
  235. public:
  236. static CTerrainViewPatternConfig & get();
  237. const std::vector<TerrainViewPattern> & getTerrainViewPatternsForGroup(ETerrainGroup::ETerrainGroup terGroup) const;
  238. boost::optional<const TerrainViewPattern &> getTerrainViewPatternById(ETerrainGroup::ETerrainGroup terGroup, const std::string & id) const;
  239. const TerrainViewPattern & getTerrainTypePatternById(const std::string & id) const;
  240. ETerrainGroup::ETerrainGroup getTerrainGroup(const std::string & terGroup) const;
  241. private:
  242. CTerrainViewPatternConfig();
  243. ~CTerrainViewPatternConfig();
  244. std::map<ETerrainGroup::ETerrainGroup, std::vector<TerrainViewPattern> > terrainViewPatterns;
  245. std::map<std::string, TerrainViewPattern> terrainTypePatterns;
  246. static boost::mutex smx;
  247. };
  248. /// The CDrawTerrainOperation class draws a terrain area on the map.
  249. class CDrawTerrainOperation : public CMapOperation
  250. {
  251. public:
  252. CDrawTerrainOperation(CMap * map, const CTerrainSelection & terrainSel, ETerrainType terType, CRandomGenerator * gen);
  253. void execute() override;
  254. void undo() override;
  255. void redo() override;
  256. std::string getLabel() const override;
  257. private:
  258. struct ValidationResult
  259. {
  260. ValidationResult(bool result, const std::string & transitionReplacement = "");
  261. bool result;
  262. /// The replacement of a T rule, either D or S.
  263. std::string transitionReplacement;
  264. int flip;
  265. };
  266. struct InvalidTiles
  267. {
  268. std::set<int3> foreignTiles, nativeTiles;
  269. };
  270. void updateTerrainTypes();
  271. void invalidateTerrainViews(const int3 & centerPos);
  272. InvalidTiles getInvalidTiles(const int3 & centerPos) const;
  273. MapRect extendTileAround(const int3 & centerPos) const;
  274. MapRect extendTileAroundSafely(const int3 & centerPos) const; /// doesn't exceed map size
  275. void updateTerrainViews();
  276. ETerrainGroup::ETerrainGroup getTerrainGroup(ETerrainType terType) const;
  277. /// Validates the terrain view of the given position and with the given pattern. The first method wraps the
  278. /// second method to validate the terrain view with the given pattern in all four flip directions(horizontal, vertical).
  279. ValidationResult validateTerrainView(const int3 & pos, const TerrainViewPattern & pattern, int recDepth = 0) const;
  280. ValidationResult validateTerrainViewInner(const int3 & pos, const TerrainViewPattern & pattern, int recDepth = 0) const;
  281. /// Tests whether the given terrain type is a sand type. Sand types are: Water, Sand and Rock
  282. bool isSandType(ETerrainType terType) const;
  283. TerrainViewPattern getFlippedPattern(const TerrainViewPattern & pattern, int flip) const;
  284. static const int FLIP_PATTERN_HORIZONTAL = 1;
  285. static const int FLIP_PATTERN_VERTICAL = 2;
  286. static const int FLIP_PATTERN_BOTH = 3;
  287. CTerrainSelection terrainSel;
  288. ETerrainType terType;
  289. CRandomGenerator * gen;
  290. std::set<int3> invalidatedTerViews;
  291. };
  292. /// The CClearTerrainOperation clears+initializes the terrain.
  293. class CClearTerrainOperation : public CComposedOperation
  294. {
  295. public:
  296. CClearTerrainOperation(CMap * map, CRandomGenerator * gen);
  297. std::string getLabel() const override;
  298. private:
  299. };
  300. /// The CInsertObjectOperation class inserts an object to the map.
  301. class CInsertObjectOperation : public CMapOperation
  302. {
  303. public:
  304. CInsertObjectOperation(CMap * map, CGObjectInstance * obj, const int3 & pos);
  305. void execute() override;
  306. void undo() override;
  307. void redo() override;
  308. std::string getLabel() const override;
  309. private:
  310. int3 pos;
  311. CGObjectInstance * obj;
  312. };