CMapEditManager.h 12 KB

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