2
0

CMapEditManager.h 7.9 KB

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