MapEditUtils.cpp 10 KB

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