CMapEditManager.h 12 KB

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