MapEditUtils.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * MapEditUtils.cpp, 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. #include "StdInc.h"
  11. #include "MapEditUtils.h"
  12. #include "../filesystem/Filesystem.h"
  13. #include "../JsonNode.h"
  14. #include "CMap.h"
  15. #include "CMapOperation.h"
  16. MapRect::MapRect() : x(0), y(0), z(0), width(0), height(0)
  17. {
  18. }
  19. MapRect::MapRect(int3 pos, si32 width, si32 height) : x(pos.x), y(pos.y), z(pos.z), width(width), height(height)
  20. {
  21. }
  22. MapRect MapRect::operator&(const MapRect& rect) const
  23. {
  24. bool intersect = right() > rect.left() && rect.right() > left()
  25. && bottom() > rect.top() && rect.bottom() > top()
  26. && z == rect.z;
  27. if (intersect)
  28. {
  29. MapRect ret;
  30. ret.x = std::max(left(), rect.left());
  31. ret.y = std::max(top(), rect.top());
  32. ret.z = rect.z;
  33. ret.width = std::min(right(), rect.right()) - ret.x;
  34. ret.height = std::min(bottom(), rect.bottom()) - ret.y;
  35. return ret;
  36. }
  37. else
  38. {
  39. return MapRect();
  40. }
  41. }
  42. si32 MapRect::left() const
  43. {
  44. return x;
  45. }
  46. si32 MapRect::right() const
  47. {
  48. return x + width;
  49. }
  50. si32 MapRect::top() const
  51. {
  52. return y;
  53. }
  54. si32 MapRect::bottom() const
  55. {
  56. return y + height;
  57. }
  58. int3 MapRect::topLeft() const
  59. {
  60. return int3(x, y, z);
  61. }
  62. int3 MapRect::topRight() const
  63. {
  64. return int3(right(), y, z);
  65. }
  66. int3 MapRect::bottomLeft() const
  67. {
  68. return int3(x, bottom(), z);
  69. }
  70. int3 MapRect::bottomRight() const
  71. {
  72. return int3(right(), bottom(), z);
  73. }
  74. CTerrainSelection::CTerrainSelection(CMap * map) : CMapSelection(map)
  75. {
  76. }
  77. void CTerrainSelection::selectRange(const MapRect& rect)
  78. {
  79. rect.forEach([this](const int3 pos)
  80. {
  81. this->select(pos);
  82. });
  83. }
  84. void CTerrainSelection::deselectRange(const MapRect& rect)
  85. {
  86. rect.forEach([this](const int3 pos)
  87. {
  88. this->deselect(pos);
  89. });
  90. }
  91. void CTerrainSelection::setSelection(const std::vector<int3> & vec)
  92. {
  93. for (const auto & pos : vec)
  94. this->select(pos);
  95. }
  96. void CTerrainSelection::selectAll()
  97. {
  98. selectRange(MapRect(int3(0, 0, 0), getMap()->width, getMap()->height));
  99. selectRange(MapRect(int3(0, 0, 1), getMap()->width, getMap()->height));
  100. }
  101. void CTerrainSelection::clearSelection()
  102. {
  103. deselectRange(MapRect(int3(0, 0, 0), getMap()->width, getMap()->height));
  104. deselectRange(MapRect(int3(0, 0, 1), getMap()->width, getMap()->height));
  105. }
  106. CObjectSelection::CObjectSelection(CMap * map) : CMapSelection(map)
  107. {
  108. }
  109. const std::string TerrainViewPattern::FLIP_MODE_DIFF_IMAGES = "D";
  110. const std::string TerrainViewPattern::RULE_DIRT = "D";
  111. const std::string TerrainViewPattern::RULE_SAND = "S";
  112. const std::string TerrainViewPattern::RULE_TRANSITION = "T";
  113. const std::string TerrainViewPattern::RULE_NATIVE = "N";
  114. const std::string TerrainViewPattern::RULE_NATIVE_STRONG = "N!";
  115. const std::string TerrainViewPattern::RULE_ANY = "?";
  116. TerrainViewPattern::TerrainViewPattern() : diffImages(false), rotationTypesCount(0), minPoints(0)
  117. {
  118. maxPoints = std::numeric_limits<int>::max();
  119. }
  120. TerrainViewPattern::WeightedRule::WeightedRule(std::string& Name) : points(0), name(Name)
  121. {
  122. standardRule = (TerrainViewPattern::RULE_ANY == Name || TerrainViewPattern::RULE_DIRT == Name
  123. || TerrainViewPattern::RULE_NATIVE == Name || TerrainViewPattern::RULE_SAND == Name
  124. || TerrainViewPattern::RULE_TRANSITION == Name || TerrainViewPattern::RULE_NATIVE_STRONG == Name);
  125. anyRule = (Name == TerrainViewPattern::RULE_ANY);
  126. dirtRule = (Name == TerrainViewPattern::RULE_DIRT);
  127. sandRule = (Name == TerrainViewPattern::RULE_SAND);
  128. transitionRule = (Name == TerrainViewPattern::RULE_TRANSITION);
  129. nativeStrongRule = (Name == TerrainViewPattern::RULE_NATIVE_STRONG);
  130. nativeRule = (Name == TerrainViewPattern::RULE_NATIVE);
  131. }
  132. void TerrainViewPattern::WeightedRule::setNative()
  133. {
  134. nativeRule = true;
  135. standardRule = true;
  136. //TODO: would look better as a bitfield
  137. dirtRule = sandRule = transitionRule = nativeStrongRule = anyRule = false; //no idea what they mean, but look mutually exclusive
  138. }
  139. CTerrainViewPatternConfig::CTerrainViewPatternConfig()
  140. {
  141. const JsonNode config(ResourceID("config/terrainViewPatterns.json"));
  142. static const std::string patternTypes[] = { "terrainView", "terrainType" };
  143. for (int i = 0; i < ARRAY_COUNT(patternTypes); ++i)
  144. {
  145. const auto& patternsVec = config[patternTypes[i]].Vector();
  146. for (const auto& ptrnNode : patternsVec)
  147. {
  148. TerrainViewPattern pattern;
  149. // Read pattern data
  150. const JsonVector& data = ptrnNode["data"].Vector();
  151. assert(data.size() == 9);
  152. for (int j = 0; j < data.size(); ++j)
  153. {
  154. std::string cell = data[j].String();
  155. boost::algorithm::erase_all(cell, " ");
  156. std::vector<std::string> rules;
  157. boost::split(rules, cell, boost::is_any_of(","));
  158. for (const std::string & ruleStr : rules)
  159. {
  160. std::vector<std::string> ruleParts;
  161. boost::split(ruleParts, ruleStr, boost::is_any_of("-"));
  162. TerrainViewPattern::WeightedRule rule(ruleParts[0]);
  163. assert(!rule.name.empty());
  164. if (ruleParts.size() > 1)
  165. {
  166. rule.points = boost::lexical_cast<int>(ruleParts[1]);
  167. }
  168. pattern.data[j].push_back(rule);
  169. }
  170. }
  171. // Read various properties
  172. pattern.id = ptrnNode["id"].String();
  173. assert(!pattern.id.empty());
  174. pattern.minPoints = static_cast<int>(ptrnNode["minPoints"].Float());
  175. pattern.maxPoints = static_cast<int>(ptrnNode["maxPoints"].Float());
  176. if (pattern.maxPoints == 0)
  177. pattern.maxPoints = std::numeric_limits<int>::max();
  178. // Read mapping
  179. if (i == 0)
  180. {
  181. const auto & mappingStruct = ptrnNode["mapping"].Struct();
  182. for (const auto & mappingPair : mappingStruct)
  183. {
  184. TerrainViewPattern terGroupPattern = pattern;
  185. auto mappingStr = mappingPair.second.String();
  186. boost::algorithm::erase_all(mappingStr, " ");
  187. auto colonIndex = mappingStr.find_first_of(":");
  188. const auto & flipMode = mappingStr.substr(0, colonIndex);
  189. terGroupPattern.diffImages = TerrainViewPattern::FLIP_MODE_DIFF_IMAGES == &(flipMode[flipMode.length() - 1]);
  190. if (terGroupPattern.diffImages)
  191. {
  192. terGroupPattern.rotationTypesCount = boost::lexical_cast<int>(flipMode.substr(0, flipMode.length() - 1));
  193. assert(terGroupPattern.rotationTypesCount == 2 || terGroupPattern.rotationTypesCount == 4);
  194. }
  195. mappingStr = mappingStr.substr(colonIndex + 1);
  196. std::vector<std::string> mappings;
  197. boost::split(mappings, mappingStr, boost::is_any_of(","));
  198. for (const std::string & mapping : mappings)
  199. {
  200. std::vector<std::string> range;
  201. boost::split(range, mapping, boost::is_any_of("-"));
  202. terGroupPattern.mapping.push_back(std::make_pair(boost::lexical_cast<int>(range[0]),
  203. boost::lexical_cast<int>(range.size() > 1 ? range[1] : range[0])));
  204. }
  205. // Add pattern to the patterns map
  206. std::vector<TerrainViewPattern> terrainViewPatternFlips{terGroupPattern};
  207. for (int i = 1; i < 4; ++i)
  208. {
  209. //auto p = terGroupPattern;
  210. flipPattern(terGroupPattern, i); //FIXME: we flip in place - doesn't make much sense now, but used to work
  211. terrainViewPatternFlips.push_back(terGroupPattern);
  212. }
  213. terrainViewPatterns[mappingPair.first].push_back(terrainViewPatternFlips);
  214. }
  215. }
  216. else if (i == 1)
  217. {
  218. terrainTypePatterns[pattern.id].push_back(pattern);
  219. for (int i = 1; i < 4; ++i)
  220. {
  221. //auto p = pattern;
  222. flipPattern(pattern, i); ///FIXME: we flip in place - doesn't make much sense now
  223. terrainTypePatterns[pattern.id].push_back(pattern);
  224. }
  225. }
  226. }
  227. }
  228. }
  229. CTerrainViewPatternConfig::~CTerrainViewPatternConfig()
  230. {
  231. }
  232. const std::vector<CTerrainViewPatternConfig::TVPVector> & CTerrainViewPatternConfig::getTerrainViewPatterns(TTerrainId terrain) const
  233. {
  234. auto iter = terrainViewPatterns.find(VLC->terrainTypeHandler->terrains()[terrain]->terrainViewPatterns);
  235. if (iter == terrainViewPatterns.end())
  236. return terrainViewPatterns.at("normal");
  237. return iter->second;
  238. }
  239. boost::optional<const TerrainViewPattern &> CTerrainViewPatternConfig::getTerrainViewPatternById(std::string patternId, const std::string & id) const
  240. {
  241. auto iter = terrainViewPatterns.find(patternId);
  242. const std::vector<TVPVector> & groupPatterns = (iter == terrainViewPatterns.end()) ? terrainViewPatterns.at("normal") : iter->second;
  243. for (const TVPVector & patternFlips : groupPatterns)
  244. {
  245. const TerrainViewPattern & pattern = patternFlips.front();
  246. if (id == pattern.id)
  247. {
  248. return boost::optional<const TerrainViewPattern&>(pattern);
  249. }
  250. }
  251. return boost::optional<const TerrainViewPattern&>();
  252. }
  253. boost::optional<const CTerrainViewPatternConfig::TVPVector &> CTerrainViewPatternConfig::getTerrainViewPatternsById(TTerrainId terrain, const std::string & id) const
  254. {
  255. const std::vector<TVPVector> & groupPatterns = getTerrainViewPatterns(terrain);
  256. for (const TVPVector & patternFlips : groupPatterns)
  257. {
  258. const TerrainViewPattern & pattern = patternFlips.front();
  259. if (id == pattern.id)
  260. {
  261. return boost::optional<const TVPVector&>(patternFlips);
  262. }
  263. }
  264. return boost::optional<const TVPVector&>();
  265. }
  266. const CTerrainViewPatternConfig::TVPVector* CTerrainViewPatternConfig::getTerrainTypePatternById(const std::string& id) const
  267. {
  268. auto it = terrainTypePatterns.find(id);
  269. assert(it != terrainTypePatterns.end());
  270. return &(it->second);
  271. }
  272. void CTerrainViewPatternConfig::flipPattern(TerrainViewPattern & pattern, int flip) const
  273. {
  274. //flip in place to avoid expensive constructor. Seriously.
  275. if (flip == 0)
  276. {
  277. return;
  278. }
  279. //always flip horizontal
  280. for (int i = 0; i < 3; ++i)
  281. {
  282. int y = i * 3;
  283. std::swap(pattern.data[y], pattern.data[y + 2]);
  284. }
  285. //flip vertical only at 2nd step
  286. if (flip == CMapOperation::FLIP_PATTERN_VERTICAL)
  287. {
  288. for (int i = 0; i < 3; ++i)
  289. {
  290. std::swap(pattern.data[i], pattern.data[6 + i]);
  291. }
  292. }
  293. }
  294. void CTerrainViewPatternUtils::printDebuggingInfoAboutTile(const CMap * map, int3 pos)
  295. {
  296. logGlobal->debug("Printing detailed info about nearby map tiles of pos '%s'", pos.toString());
  297. for (int y = pos.y - 2; y <= pos.y + 2; ++y)
  298. {
  299. std::string line;
  300. const int PADDED_LENGTH = 10;
  301. for (int x = pos.x - 2; x <= pos.x + 2; ++x)
  302. {
  303. auto debugPos = int3(x, y, pos.z);
  304. if (map->isInTheMap(debugPos))
  305. {
  306. auto debugTile = map->getTile(debugPos);
  307. std::string terType = debugTile.terType->name.substr(0, 6);
  308. line += terType;
  309. line.insert(line.end(), PADDED_LENGTH - terType.size(), ' ');
  310. }
  311. else
  312. {
  313. line += "X";
  314. line.insert(line.end(), PADDED_LENGTH - 1, ' ');
  315. }
  316. }
  317. logGlobal->debug(line);
  318. }
  319. }