2
0

MapEditUtils.cpp 10 KB

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