CMapEditManager.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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(const 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. /// Draws rivers at the current terrain selection. The selection will be cleared automatically.
  148. void drawRiver(const std::string & riverType, CRandomGenerator * gen = nullptr);
  149. void insertObject(CGObjectInstance * obj);
  150. CTerrainSelection & getTerrainSelection();
  151. CObjectSelection & getObjectSelection();
  152. CMapUndoManager & getUndoManager();
  153. private:
  154. void execute(std::unique_ptr<CMapOperation> && operation);
  155. CMap * map;
  156. CMapUndoManager undoManager;
  157. CRandomGenerator gen;
  158. CTerrainSelection terrainSel;
  159. CObjectSelection objectSel;
  160. };
  161. /* ---------------------------------------------------------------------------- */
  162. /* Implementation/Detail classes, Private API */
  163. /* ---------------------------------------------------------------------------- */
  164. /// The CComposedOperation is an operation which consists of several operations.
  165. class CComposedOperation : public CMapOperation
  166. {
  167. public:
  168. CComposedOperation(CMap * map);
  169. void execute() override;
  170. void undo() override;
  171. void redo() override;
  172. void addOperation(std::unique_ptr<CMapOperation> && operation);
  173. private:
  174. std::list<std::unique_ptr<CMapOperation> > operations;
  175. };
  176. /// The terrain view pattern describes a specific composition of terrain tiles
  177. /// in a 3x3 matrix and notes which terrain view frame numbers can be used.
  178. struct DLL_LINKAGE TerrainViewPattern
  179. {
  180. struct WeightedRule
  181. {
  182. WeightedRule(std::string &Name);
  183. /// Gets true if this rule is a standard rule which means that it has a value of one of the RULE_* constants.
  184. inline bool isStandardRule() const
  185. {
  186. return standardRule;
  187. }
  188. inline bool isAnyRule() const
  189. {
  190. return anyRule;
  191. }
  192. inline bool isDirtRule() const
  193. {
  194. return dirtRule;
  195. }
  196. inline bool isSandRule() const
  197. {
  198. return sandRule;
  199. }
  200. inline bool isTransition() const
  201. {
  202. return transitionRule;
  203. }
  204. inline bool isNativeStrong() const
  205. {
  206. return nativeStrongRule;
  207. }
  208. inline bool isNativeRule() const
  209. {
  210. return nativeRule;
  211. }
  212. void setNative();
  213. /// The name of the rule. Can be any value of the RULE_* constants or a ID of a another pattern.
  214. //FIXME: remove string variable altogether, use only in constructor
  215. std::string name;
  216. /// Optional. A rule can have points. Patterns may have a minimum count of points to reach to be successful.
  217. int points;
  218. private:
  219. bool standardRule;
  220. bool anyRule;
  221. bool dirtRule;
  222. bool sandRule;
  223. bool transitionRule;
  224. bool nativeStrongRule;
  225. bool nativeRule;
  226. WeightedRule(); //only allow string constructor
  227. };
  228. static const int PATTERN_DATA_SIZE = 9;
  229. /// Constant for the flip mode different images. Pattern will be flipped and different images will be used(mapping area is divided into 4 parts)
  230. static const std::string FLIP_MODE_DIFF_IMAGES;
  231. /// Constant for the rule dirt, meaning a dirty border is required.
  232. static const std::string RULE_DIRT;
  233. /// Constant for the rule sand, meaning a sandy border is required.
  234. static const std::string RULE_SAND;
  235. /// Constant for the rule transition, meaning a dirty OR sandy border is required.
  236. static const std::string RULE_TRANSITION;
  237. /// Constant for the rule native, meaning a native border is required.
  238. static const std::string RULE_NATIVE;
  239. /// Constant for the rule native strong, meaning a native type is required.
  240. static const std::string RULE_NATIVE_STRONG;
  241. /// Constant for the rule any, meaning a native type, dirty OR sandy border is required.
  242. static const std::string RULE_ANY;
  243. TerrainViewPattern();
  244. /// The pattern data can be visualized as a 3x3 matrix:
  245. /// [ ][ ][ ]
  246. /// [ ][ ][ ]
  247. /// [ ][ ][ ]
  248. ///
  249. /// The box in the center belongs always to the native terrain type and
  250. /// is the point of origin. Depending on the terrain type different rules
  251. /// can be used. Their meaning differs also from type to type.
  252. ///
  253. /// std::vector -> several rules can be used in one cell
  254. std::array<std::vector<WeightedRule>, PATTERN_DATA_SIZE> data;
  255. /// The identifier of the pattern, if it's referenced from a another pattern.
  256. std::string id;
  257. /// This describes the mapping between this pattern and the corresponding range of frames
  258. /// which should be used for the ter view.
  259. ///
  260. /// std::vector -> size=1: typical, size=2: if this pattern should map to two different types of borders
  261. /// std::pair -> 1st value: lower range, 2nd value: upper range
  262. std::vector<std::pair<int, int> > mapping;
  263. /// If diffImages is true, different images/frames are used to place a rotated terrain view. If it's false
  264. /// the same frame will be used and rotated.
  265. bool diffImages;
  266. /// The rotationTypesCount is only used if diffImages is true and holds the number how many rotation types(horizontal, etc...)
  267. /// are supported.
  268. int rotationTypesCount;
  269. /// The minimum and maximum points to reach to validate the pattern successfully.
  270. int minPoints, maxPoints;
  271. };
  272. /// The terrain view pattern config loads pattern data from the filesystem.
  273. class DLL_LINKAGE CTerrainViewPatternConfig : public boost::noncopyable
  274. {
  275. public:
  276. typedef std::vector<TerrainViewPattern> TVPVector;
  277. CTerrainViewPatternConfig();
  278. ~CTerrainViewPatternConfig();
  279. const std::vector<TVPVector> & getTerrainViewPatterns(const Terrain & terrain) const;
  280. boost::optional<const TerrainViewPattern &> getTerrainViewPatternById(std::string patternId, const std::string & id) const;
  281. boost::optional<const TVPVector &> getTerrainViewPatternsById(const Terrain & terrain, const std::string & id) const;
  282. const TVPVector * getTerrainTypePatternById(const std::string & id) const;
  283. void flipPattern(TerrainViewPattern & pattern, int flip) const;
  284. private:
  285. std::map<std::string, std::vector<TVPVector> > terrainViewPatterns;
  286. std::map<std::string, TVPVector> terrainTypePatterns;
  287. };
  288. /// The CDrawTerrainOperation class draws a terrain area on the map.
  289. class CDrawTerrainOperation : public CMapOperation
  290. {
  291. public:
  292. CDrawTerrainOperation(CMap * map, const CTerrainSelection & terrainSel, Terrain terType, CRandomGenerator * gen);
  293. void execute() override;
  294. void undo() override;
  295. void redo() override;
  296. std::string getLabel() const override;
  297. private:
  298. struct ValidationResult
  299. {
  300. ValidationResult(bool result, const std::string & transitionReplacement = "");
  301. bool result;
  302. /// The replacement of a T rule, either D or S.
  303. std::string transitionReplacement;
  304. int flip;
  305. };
  306. struct InvalidTiles
  307. {
  308. std::set<int3> foreignTiles, nativeTiles;
  309. bool centerPosValid;
  310. InvalidTiles() : centerPosValid(false) { }
  311. };
  312. void updateTerrainTypes();
  313. void invalidateTerrainViews(const int3 & centerPos);
  314. InvalidTiles getInvalidTiles(const int3 & centerPos) const;
  315. void updateTerrainViews();
  316. /// Validates the terrain view of the given position and with the given pattern. The first method wraps the
  317. /// second method to validate the terrain view with the given pattern in all four flip directions(horizontal, vertical).
  318. ValidationResult validateTerrainView(const int3 & pos, const std::vector<TerrainViewPattern> * pattern, int recDepth = 0) const;
  319. ValidationResult validateTerrainViewInner(const int3 & pos, const TerrainViewPattern & pattern, int recDepth = 0) const;
  320. CTerrainSelection terrainSel;
  321. Terrain terType;
  322. CRandomGenerator * gen;
  323. std::set<int3> invalidatedTerViews;
  324. };
  325. class DLL_LINKAGE CTerrainViewPatternUtils
  326. {
  327. public:
  328. static void printDebuggingInfoAboutTile(const CMap * map, int3 pos);
  329. };
  330. /// The CClearTerrainOperation clears+initializes the terrain.
  331. class CClearTerrainOperation : public CComposedOperation
  332. {
  333. public:
  334. CClearTerrainOperation(CMap * map, CRandomGenerator * gen);
  335. std::string getLabel() const override;
  336. private:
  337. };
  338. /// The CInsertObjectOperation class inserts an object to the map.
  339. class CInsertObjectOperation : public CMapOperation
  340. {
  341. public:
  342. CInsertObjectOperation(CMap * map, CGObjectInstance * obj);
  343. void execute() override;
  344. void undo() override;
  345. void redo() override;
  346. std::string getLabel() const override;
  347. private:
  348. CGObjectInstance * obj;
  349. };