CMapEditManager.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. };
  34. /// The abstract base class CMapOperation defines an operation that can be executed, undone and redone.
  35. class DLL_LINKAGE CMapOperation : public boost::noncopyable
  36. {
  37. public:
  38. CMapOperation(CMap * map);
  39. virtual ~CMapOperation() { };
  40. virtual void execute() = 0;
  41. virtual void undo() = 0;
  42. virtual void redo() = 0;
  43. virtual std::string getLabel() const; /// Returns a display-able name of the operation.
  44. protected:
  45. CMap * map;
  46. };
  47. /// The CMapUndoManager provides the functionality to save operations and undo/redo them.
  48. class DLL_LINKAGE CMapUndoManager : boost::noncopyable
  49. {
  50. public:
  51. CMapUndoManager();
  52. void undo();
  53. void redo();
  54. void clearAll();
  55. /// The undo redo limit is a number which says how many undo/redo items can be saved. The default
  56. /// value is 10. If the value is 0, no undo/redo history will be maintained.
  57. int getUndoRedoLimit() const;
  58. void setUndoRedoLimit(int value);
  59. const CMapOperation * peekRedo() const;
  60. const CMapOperation * peekUndo() const;
  61. void addOperation(unique_ptr<CMapOperation> && operation); /// Client code does not need to call this method.
  62. private:
  63. typedef std::list<unique_ptr<CMapOperation> > TStack;
  64. void doOperation(TStack & fromStack, TStack & toStack, bool doUndo);
  65. const CMapOperation * peek(const TStack & stack) const;
  66. TStack undoStack;
  67. TStack redoStack;
  68. int undoRedoLimit;
  69. };
  70. /// The map edit manager provides functionality for drawing terrain and placing
  71. /// objects on the map.
  72. class DLL_LINKAGE CMapEditManager : boost::noncopyable
  73. {
  74. public:
  75. CMapEditManager(CMap * map);
  76. /// Clears the terrain. The free level is filled with water and the underground level with rock.
  77. void clearTerrain(CRandomGenerator * gen);
  78. void drawTerrain(const MapRect & rect, ETerrainType terType, CRandomGenerator * gen);
  79. void insertObject(const int3 & pos, CGObjectInstance * obj);
  80. CMapUndoManager & getUndoManager();
  81. void undo();
  82. void redo();
  83. private:
  84. void execute(unique_ptr<CMapOperation> && operation);
  85. CMap * map;
  86. CMapUndoManager undoManager;
  87. };
  88. /* ---------------------------------------------------------------------------- */
  89. /* Implementation/Detail classes, Private API */
  90. /* ---------------------------------------------------------------------------- */
  91. namespace ETerrainGroup
  92. {
  93. enum ETerrainGroup
  94. {
  95. NORMAL,
  96. DIRT,
  97. SAND,
  98. WATER,
  99. ROCK
  100. };
  101. }
  102. /// The terrain view pattern describes a specific composition of terrain tiles
  103. /// in a 3x3 matrix and notes which terrain view frame numbers can be used.
  104. struct DLL_LINKAGE TerrainViewPattern
  105. {
  106. struct WeightedRule
  107. {
  108. WeightedRule();
  109. /// Gets true if this rule is a standard rule which means that it has a value of one of the RULE_* constants.
  110. bool isStandardRule() const;
  111. /// The name of the rule. Can be any value of the RULE_* constants or a ID of a another pattern.
  112. std::string name;
  113. /// Optional. A rule can have points. Patterns may have a minimum count of points to reach to be successful.
  114. int points;
  115. };
  116. /// Constant for the flip mode same image. Pattern will be flipped and the same image will be used(which is given in the mapping).
  117. static const std::string FLIP_MODE_SAME_IMAGE;
  118. /// Constant for the flip mode different images. Pattern will be flipped and different images will be used(mapping area is divided into 4 parts)
  119. static const std::string FLIP_MODE_DIFF_IMAGES;
  120. /// Constant for the rule dirt, meaning a dirty border is required.
  121. static const std::string RULE_DIRT;
  122. /// Constant for the rule sand, meaning a sandy border is required.
  123. static const std::string RULE_SAND;
  124. /// Constant for the rule transition, meaning a dirty OR sandy border is required.
  125. static const std::string RULE_TRANSITION;
  126. /// Constant for the rule native, meaning a native type is required.
  127. static const std::string RULE_NATIVE;
  128. /// Constant for the rule any, meaning a native type, dirty OR sandy border is required.
  129. static const std::string RULE_ANY;
  130. TerrainViewPattern();
  131. /// The pattern data can be visualized as a 3x3 matrix:
  132. /// [ ][ ][ ]
  133. /// [ ][ ][ ]
  134. /// [ ][ ][ ]
  135. ///
  136. /// The box in the center belongs always to the native terrain type and
  137. /// is the point of origin. Depending on the terrain type different rules
  138. /// can be used. Their meaning differs also from type to type.
  139. ///
  140. /// std::vector -> several rules can be used in one cell
  141. std::array<std::vector<WeightedRule>, 9> data;
  142. /// The identifier of the pattern, if it's referenced from a another pattern.
  143. std::string id;
  144. /// This describes the mapping between this pattern and the corresponding range of frames
  145. /// which should be used for the ter view.
  146. ///
  147. /// std::vector -> size=1: typical, size=2: if this pattern should map to two different types of borders
  148. /// std::pair -> 1st value: lower range, 2nd value: upper range
  149. std::vector<std::pair<int, int> > mapping;
  150. /// The minimum points to reach to to validate the pattern successfully.
  151. int minPoints;
  152. /// Describes if flipping is required and which mapping should be used.
  153. std::string flipMode;
  154. ETerrainGroup::ETerrainGroup terGroup;
  155. };
  156. /// The terrain view pattern config loads pattern data from the filesystem.
  157. class DLL_LINKAGE CTerrainViewPatternConfig : public boost::noncopyable
  158. {
  159. public:
  160. static CTerrainViewPatternConfig & get();
  161. const std::vector<TerrainViewPattern> & getPatternsForGroup(ETerrainGroup::ETerrainGroup terGroup) const;
  162. const TerrainViewPattern & getPatternById(ETerrainGroup::ETerrainGroup terGroup, const std::string & id) const;
  163. ETerrainGroup::ETerrainGroup getTerrainGroup(const std::string & terGroup) const;
  164. private:
  165. CTerrainViewPatternConfig();
  166. ~CTerrainViewPatternConfig();
  167. std::map<ETerrainGroup::ETerrainGroup, std::vector<TerrainViewPattern> > patterns;
  168. static boost::mutex smx;
  169. };
  170. /// The DrawTerrainOperation class draws a terrain area on the map.
  171. class DrawTerrainOperation : public CMapOperation
  172. {
  173. public:
  174. DrawTerrainOperation(CMap * map, const MapRect & rect, ETerrainType terType, CRandomGenerator * gen);
  175. void execute() override;
  176. void undo() override;
  177. void redo() override;
  178. std::string getLabel() const override;
  179. private:
  180. struct ValidationResult
  181. {
  182. ValidationResult(bool result, const std::string & transitionReplacement = "");
  183. bool result;
  184. /// The replacement of a T rule, either D or S.
  185. std::string transitionReplacement;
  186. };
  187. void updateTerrainViews(const MapRect & rect);
  188. ETerrainGroup::ETerrainGroup getTerrainGroup(ETerrainType terType) const;
  189. /// Validates the terrain view of the given position and with the given pattern.
  190. ValidationResult validateTerrainView(const int3 & pos, const TerrainViewPattern & pattern, int recDepth = 0) const;
  191. /// Tests whether the given terrain type is a sand type. Sand types are: Water, Sand and Rock
  192. bool isSandType(ETerrainType terType) const;
  193. TerrainViewPattern getFlippedPattern(const TerrainViewPattern & pattern, int flip) const;
  194. static const int FLIP_PATTERN_HORIZONTAL = 1;
  195. static const int FLIP_PATTERN_VERTICAL = 2;
  196. static const int FLIP_PATTERN_BOTH = 3;
  197. MapRect rect;
  198. ETerrainType terType;
  199. CRandomGenerator * gen;
  200. };
  201. /// The InsertObjectOperation class inserts an object to the map.
  202. class InsertObjectOperation : public CMapOperation
  203. {
  204. public:
  205. InsertObjectOperation(CMap * map, const int3 & pos, CGObjectInstance * obj);
  206. void execute() override;
  207. void undo() override;
  208. void redo() override;
  209. std::string getLabel() const override;
  210. private:
  211. int3 pos;
  212. CGObjectInstance * obj;
  213. };