2
0

CMapEditManager.h 12 KB

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