CMapEditManager.h 12 KB

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