CMapEditManager.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. namespace ETerrainGroup
  17. {
  18. enum ETerrainGroup
  19. {
  20. NORMAL,
  21. DIRT,
  22. SAND,
  23. WATER,
  24. ROCK
  25. };
  26. }
  27. /// Represents a map rectangle.
  28. struct DLL_LINKAGE MapRect
  29. {
  30. MapRect(int3 pos, si32 width, si32 height) : pos(pos), width(width), height(height) { };
  31. int3 pos;
  32. si32 width, height;
  33. };
  34. /// The abstract base class CMapOperation defines an operation that can be executed, undone and redone.
  35. class CMapOperation
  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 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. inline void doOperation(TStack & fromStack, TStack & toStack, bool doUndo);
  65. inline 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. /// The terrain view pattern describes a specific composition of terrain tiles
  92. /// in a 3x3 matrix and notes which terrain view frame numbers can be used.
  93. struct TerrainViewPattern
  94. {
  95. struct WeightedRule
  96. {
  97. WeightedRule();
  98. /// Gets true if this rule is a standard rule which means that it has a value of one of the RULE_* constants.
  99. bool isStandardRule() const;
  100. /// The name of the rule. Can be any value of the RULE_* constants or a ID of a another pattern.
  101. std::string name;
  102. /// Optional. A rule can have points. Patterns may have a minimum count of points to reach to be successful.
  103. int points;
  104. };
  105. /// Constant for the flip mode same image. Pattern will be flipped and the same image will be used(which is given in the mapping).
  106. static const std::string FLIP_MODE_SAME_IMAGE;
  107. /// Constant for the flip mode different images. Pattern will be flipped and different images will be used(mapping area is divided into 4 parts)
  108. static const std::string FLIP_MODE_DIFF_IMAGES;
  109. /// Constant for the rule dirt, meaning a dirty border is required.
  110. static const std::string RULE_DIRT;
  111. /// Constant for the rule sand, meaning a sandy border is required.
  112. static const std::string RULE_SAND;
  113. /// Constant for the rule transition, meaning a dirty OR sandy border is required.
  114. static const std::string RULE_TRANSITION;
  115. /// Constant for the rule native, meaning a native type is required.
  116. static const std::string RULE_NATIVE;
  117. /// Constant for the rule any, meaning a native type, dirty OR sandy border is required.
  118. static const std::string RULE_ANY;
  119. TerrainViewPattern();
  120. /// The pattern data can be visualized as a 3x3 matrix:
  121. /// [ ][ ][ ]
  122. /// [ ][ ][ ]
  123. /// [ ][ ][ ]
  124. ///
  125. /// The box in the center belongs always to the native terrain type and
  126. /// is the point of origin. Depending on the terrain type different rules
  127. /// can be used. Their meaning differs also from type to type.
  128. ///
  129. /// std::vector -> several rules can be used in one cell
  130. std::array<std::vector<WeightedRule>, 9> data;
  131. /// The identifier of the pattern, if it's referenced from a another pattern.
  132. std::string id;
  133. /// This describes the mapping between this pattern and the corresponding range of frames
  134. /// which should be used for the ter view.
  135. ///
  136. /// std::vector -> size=1: typical, size=2: if this pattern should map to two different types of borders
  137. /// std::pair -> 1st value: lower range, 2nd value: upper range
  138. std::vector<std::pair<int, int> > mapping;
  139. /// The minimum points to reach to to validate the pattern successfully.
  140. int minPoints;
  141. /// Describes if flipping is required and which mapping should be used.
  142. std::string flipMode;
  143. ETerrainGroup::ETerrainGroup terGroup;
  144. };
  145. /// The terrain view pattern config loads pattern data from the filesystem.
  146. class CTerrainViewPatternConfig : public boost::noncopyable
  147. {
  148. public:
  149. static CTerrainViewPatternConfig & get();
  150. const std::vector<TerrainViewPattern> & getPatternsForGroup(ETerrainGroup::ETerrainGroup terGroup) const;
  151. const TerrainViewPattern & getPatternById(ETerrainGroup::ETerrainGroup terGroup, const std::string & id) const;
  152. private:
  153. CTerrainViewPatternConfig();
  154. ~CTerrainViewPatternConfig();
  155. std::map<ETerrainGroup::ETerrainGroup, std::vector<TerrainViewPattern> > patterns;
  156. static boost::mutex smx;
  157. };
  158. /// The DrawTerrainOperation class draws a terrain area on the map.
  159. class DrawTerrainOperation : public CMapOperation
  160. {
  161. public:
  162. DrawTerrainOperation(CMap * map, const MapRect & rect, ETerrainType terType, CRandomGenerator * gen);
  163. void execute();
  164. void undo();
  165. void redo();
  166. std::string getLabel() const;
  167. private:
  168. struct ValidationResult
  169. {
  170. ValidationResult(bool result, const std::string & transitionReplacement = "");
  171. bool result;
  172. /// The replacement of a T rule, either D or S.
  173. std::string transitionReplacement;
  174. };
  175. void updateTerrainViews(const MapRect & rect);
  176. ETerrainGroup::ETerrainGroup getTerrainGroup(ETerrainType terType) const;
  177. /// Validates the terrain view of the given position and with the given pattern.
  178. ValidationResult validateTerrainView(const int3 & pos, const TerrainViewPattern & pattern, int recDepth = 0) const;
  179. /// Tests whether the given terrain type is a sand type. Sand types are: Water, Sand and Rock
  180. bool isSandType(ETerrainType terType) const;
  181. TerrainViewPattern getFlippedPattern(const TerrainViewPattern & pattern, int flip) const;
  182. static const int FLIP_PATTERN_HORIZONTAL = 1;
  183. static const int FLIP_PATTERN_VERTICAL = 2;
  184. static const int FLIP_PATTERN_BOTH = 3;
  185. MapRect rect;
  186. ETerrainType terType;
  187. CRandomGenerator * gen;
  188. };
  189. /// The InsertObjectOperation class inserts an object to the map.
  190. class InsertObjectOperation : public CMapOperation
  191. {
  192. public:
  193. InsertObjectOperation(CMap * map, const int3 & pos, CGObjectInstance * obj);
  194. void execute();
  195. void undo();
  196. void redo();
  197. std::string getLabel() const;
  198. private:
  199. int3 pos;
  200. CGObjectInstance * obj;
  201. };