CMapEditManager.h 13 KB

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