CMapEditManager.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. namespace ETerrainGroup
  15. {
  16. /**
  17. * This enumeration lists terrain groups which differ in the terrain view frames alignment.
  18. */
  19. enum ETerrainGroup
  20. {
  21. NORMAL,
  22. DIRT,
  23. SAND,
  24. WATER,
  25. ROCK
  26. };
  27. }
  28. /**
  29. * The terrain view pattern describes a specific composition of terrain tiles
  30. * in a 3x3 matrix and notes which terrain view frame numbers can be used.
  31. */
  32. struct TerrainViewPattern
  33. {
  34. /**
  35. * A weighted rule struct is a combination of the rule name and optionally points.
  36. */
  37. struct WeightedRule
  38. {
  39. /** The name of the rule. Can be any value of the RULE_* constants or a ID of a another pattern. */
  40. std::string name;
  41. /** Optional. A rule can have points. Patterns may have a minimum count of points to reach to be successful. */
  42. int points;
  43. /**
  44. * Constructor.
  45. */
  46. WeightedRule();
  47. /**
  48. * Gets true if this rule is a standard rule which means that it has a value of one of the RULE_* constants.
  49. *
  50. * @return true for a standard rule
  51. */
  52. bool isStandardRule() const;
  53. };
  54. /** Constant for the flip mode same image. Pattern will be flipped and the same image will be used(which is given in the mapping). */
  55. static const std::string FLIP_MODE_SAME_IMAGE;
  56. /** Constant for the flip mode different images. Pattern will be flipped and different images will be used(mapping area is divided into 4 parts) */
  57. static const std::string FLIP_MODE_DIFF_IMAGES;
  58. /** Constant for the rule dirt, meaning a dirty border is required. */
  59. static const std::string RULE_DIRT;
  60. /** Constant for the rule sand, meaning a sandy border is required. */
  61. static const std::string RULE_SAND;
  62. /** Constant for the rule transition, meaning a dirty OR sandy border is required. */
  63. static const std::string RULE_TRANSITION;
  64. /** Constant for the rule native, meaning a native type is required. */
  65. static const std::string RULE_NATIVE;
  66. /** Constant for the rule any, meaning a native type, dirty OR sandy border is required. */
  67. static const std::string RULE_ANY;
  68. /**
  69. * Default constructor.
  70. */
  71. TerrainViewPattern();
  72. /**
  73. * The pattern data.
  74. *
  75. * It can be visualized as a 3x3 matrix:
  76. * [ ][ ][ ]
  77. * [ ][ ][ ]
  78. * [ ][ ][ ]
  79. *
  80. * The box in the center belongs always to the native terrain type and
  81. * is the point of origin. Depending on the terrain type different rules
  82. * can be used. Their meaning differs also from type to type.
  83. *
  84. * std::vector -> several rules can be used in one cell
  85. */
  86. std::array<std::vector<WeightedRule>, 9> data;
  87. /** The identifier of the pattern, if it's referenced from a another pattern. */
  88. std::string id;
  89. /**
  90. * This describes the mapping between this pattern and the corresponding range of frames
  91. * which should be used for the ter view.
  92. *
  93. * std::vector -> size=1: typical, size=2: if this pattern should map to two different types of borders
  94. * std::pair -> 1st value: lower range, 2nd value: upper range
  95. */
  96. std::vector<std::pair<int, int> > mapping;
  97. /** The minimum points to reach to to validate the pattern successfully. */
  98. int minPoints;
  99. /** Describes if flipping is required and which mapping should be used. */
  100. std::string flipMode;
  101. /** The terrain group to which the pattern belongs to. */
  102. ETerrainGroup::ETerrainGroup terGroup;
  103. };
  104. /**
  105. * The terrain view pattern config loads pattern data from the filesystem.
  106. */
  107. class CTerrainViewPatternConfig
  108. {
  109. public:
  110. /**
  111. * Constructor. Initializes the patterns data.
  112. */
  113. CTerrainViewPatternConfig();
  114. /**
  115. * Gets the patterns for a specific group of terrain.
  116. *
  117. * @param terGroup the terrain group e.g. normal for grass, lava,... OR dirt OR sand,...
  118. * @return a vector containing patterns
  119. */
  120. const std::vector<TerrainViewPattern> & getPatternsForGroup(ETerrainGroup::ETerrainGroup terGroup) const;
  121. /**
  122. * Gets a pattern by ID. Throws if pattern isn't available(config error).
  123. *
  124. * @param terGroup the terrain group e.g. normal for grass, lava,... OR dirt OR sand,...
  125. * @param id the id of the pattern
  126. * @return the pattern which matches the ID
  127. */
  128. const TerrainViewPattern & getPatternById(ETerrainGroup::ETerrainGroup terGroup, const std::string & id) const;
  129. private:
  130. /** The patterns data. */
  131. std::map<ETerrainGroup::ETerrainGroup, std::vector<TerrainViewPattern> > patterns;
  132. };
  133. /**
  134. * The map edit manager provides functionality for drawing terrain and placing
  135. * objects on the map.
  136. *
  137. * TODO add undo / selection functionality for the map editor
  138. */
  139. class CMapEditManager
  140. {
  141. public:
  142. /**
  143. * Constructor. The map object / terrain data has to be initialized.
  144. *
  145. * @param terViewPatternConfig the terrain view pattern config
  146. * @param map the map object which should be edited
  147. * @param randomSeed optional. the seed which is used for generating randomly terrain views
  148. */
  149. CMapEditManager(const CTerrainViewPatternConfig * terViewPatternConfig, CMap * map, int randomSeed = std::time(nullptr));
  150. /**
  151. * Clears the terrain. The free level is filled with water and the
  152. * underground level with rock.
  153. */
  154. void clearTerrain();
  155. /**
  156. * Draws terrain.
  157. *
  158. * @param terType the type of the terrain to draw
  159. * @param posx the x coordinate
  160. * @param posy the y coordinate
  161. * @param width the height of the terrain to draw
  162. * @param height the width of the terrain to draw
  163. * @param underground true if you want to draw at the underground, false if open
  164. */
  165. void drawTerrain(ETerrainType terType, int posx, int posy, int width, int height, bool underground);
  166. /**
  167. * Inserts an object.
  168. *
  169. * @param obj the object to insert
  170. * @param posx the x coordinate
  171. * @param posy the y coordinate
  172. * @param underground true if you want to draw at the underground, false if open
  173. */
  174. void insertObject(CGObjectInstance * obj, int posx, int posy, bool underground);
  175. private:
  176. /**
  177. * The validation result struct represents the result of a pattern validation.
  178. */
  179. struct ValidationResult
  180. {
  181. /**
  182. * Constructor.
  183. *
  184. * @param result the result of the validation either true or false
  185. * @param transitionReplacement optional. the replacement of a T rule, either D or S
  186. */
  187. ValidationResult(bool result, const std::string & transitionReplacement = "");
  188. /** The result of the validation. */
  189. bool result;
  190. /** The replacement of a T rule, either D or S. */
  191. std::string transitionReplacement;
  192. };
  193. /**
  194. * Updates the terrain view ids in the specified area.
  195. *
  196. * @param posx the x coordinate
  197. * @param posy the y coordinate
  198. * @param width the height of the terrain to update
  199. * @param height the width of the terrain to update
  200. * @param mapLevel the map level, 0 for open and 1 for underground
  201. */
  202. void updateTerrainViews(int posx, int posy, int width, int height, int mapLevel);
  203. /**
  204. * Gets the terrain group by the terrain type number.
  205. *
  206. * @param terType the terrain type
  207. * @return the terrain group
  208. */
  209. ETerrainGroup::ETerrainGroup getTerrainGroup(ETerrainType terType) const;
  210. /**
  211. * Validates the terrain view of the given position and with the given pattern.
  212. *
  213. * @param posx the x position
  214. * @param posy the y position
  215. * @param mapLevel the map level, 0 for open and 1 for underground
  216. * @param pattern the pattern to validate the terrain view with
  217. * @param recDepth the depth of the recursion, 0 for no recursion - 1 for recursion
  218. * @return a validation result struct
  219. */
  220. ValidationResult validateTerrainView(int posx, int posy, int mapLevel, const TerrainViewPattern & pattern, int recDepth = 0) const;
  221. /**
  222. * Tests whether the given terrain type is a sand type. Sand types are: Water, Sand and Rock
  223. *
  224. * @param terType the terrain type to test
  225. * @return true if the terrain type is a sand type, otherwise false
  226. */
  227. bool isSandType(ETerrainType terType) const;
  228. /**
  229. * Gets a flipped pattern.
  230. *
  231. * @param pattern the original pattern to flip
  232. * @param flip the flip mode value, see FLIP_PATTERN_* constants for details
  233. * @return the flipped pattern
  234. */
  235. TerrainViewPattern getFlippedPattern(const TerrainViewPattern & pattern, int flip) const;
  236. /** Constant for flipping a pattern horizontally. */
  237. static const int FLIP_PATTERN_HORIZONTAL = 1;
  238. /** Constant for flipping a pattern vertically. */
  239. static const int FLIP_PATTERN_VERTICAL = 2;
  240. /** Constant for flipping a pattern horizontally and vertically. */
  241. static const int FLIP_PATTERN_BOTH = 3;
  242. /** The map object to edit. */
  243. CMap * map;
  244. /** The random number generator. */
  245. CRandomGenerator gen;
  246. /** The terrain view pattern config. */
  247. const CTerrainViewPatternConfig * terViewPatternConfig;
  248. };