MapEditUtils.cpp 10 KB

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