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 "../JsonNode.h"
  14. #include "../TerrainHandler.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(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() : diffImages(false), rotationTypesCount(0), minPoints(0)
  119. {
  120. maxPoints = std::numeric_limits<int>::max();
  121. }
  122. TerrainViewPattern::WeightedRule::WeightedRule(std::string& Name) : points(0), name(Name)
  123. {
  124. standardRule = (TerrainViewPattern::RULE_ANY == Name || TerrainViewPattern::RULE_DIRT == Name
  125. || TerrainViewPattern::RULE_NATIVE == Name || TerrainViewPattern::RULE_SAND == Name
  126. || TerrainViewPattern::RULE_TRANSITION == Name || TerrainViewPattern::RULE_NATIVE_STRONG == Name);
  127. anyRule = (Name == TerrainViewPattern::RULE_ANY);
  128. dirtRule = (Name == TerrainViewPattern::RULE_DIRT);
  129. sandRule = (Name == TerrainViewPattern::RULE_SAND);
  130. transitionRule = (Name == TerrainViewPattern::RULE_TRANSITION);
  131. nativeStrongRule = (Name == TerrainViewPattern::RULE_NATIVE_STRONG);
  132. nativeRule = (Name == TerrainViewPattern::RULE_NATIVE);
  133. }
  134. void TerrainViewPattern::WeightedRule::setNative()
  135. {
  136. nativeRule = true;
  137. standardRule = true;
  138. //TODO: would look better as a bitfield
  139. dirtRule = sandRule = transitionRule = nativeStrongRule = anyRule = false; //no idea what they mean, but look mutually exclusive
  140. }
  141. CTerrainViewPatternConfig::CTerrainViewPatternConfig()
  142. {
  143. const JsonNode config(ResourceID("config/terrainViewPatterns.json"));
  144. static const std::string patternTypes[] = { "terrainView", "terrainType" };
  145. for (int i = 0; i < ARRAY_COUNT(patternTypes); ++i)
  146. {
  147. const auto& patternsVec = config[patternTypes[i]].Vector();
  148. for (const auto& ptrnNode : patternsVec)
  149. {
  150. TerrainViewPattern pattern;
  151. // Read pattern data
  152. const JsonVector& data = ptrnNode["data"].Vector();
  153. assert(data.size() == 9);
  154. for (int j = 0; j < data.size(); ++j)
  155. {
  156. std::string cell = data[j].String();
  157. boost::algorithm::erase_all(cell, " ");
  158. std::vector<std::string> rules;
  159. boost::split(rules, cell, boost::is_any_of(","));
  160. for (const std::string & ruleStr : rules)
  161. {
  162. std::vector<std::string> ruleParts;
  163. boost::split(ruleParts, ruleStr, boost::is_any_of("-"));
  164. TerrainViewPattern::WeightedRule rule(ruleParts[0]);
  165. assert(!rule.name.empty());
  166. if (ruleParts.size() > 1)
  167. {
  168. rule.points = boost::lexical_cast<int>(ruleParts[1]);
  169. }
  170. pattern.data[j].push_back(rule);
  171. }
  172. }
  173. // Read various properties
  174. pattern.id = ptrnNode["id"].String();
  175. assert(!pattern.id.empty());
  176. pattern.minPoints = static_cast<int>(ptrnNode["minPoints"].Float());
  177. pattern.maxPoints = static_cast<int>(ptrnNode["maxPoints"].Float());
  178. if (pattern.maxPoints == 0)
  179. pattern.maxPoints = std::numeric_limits<int>::max();
  180. // Read mapping
  181. if (i == 0)
  182. {
  183. const auto & mappingStruct = ptrnNode["mapping"].Struct();
  184. for (const auto & mappingPair : mappingStruct)
  185. {
  186. TerrainViewPattern terGroupPattern = pattern;
  187. auto mappingStr = mappingPair.second.String();
  188. boost::algorithm::erase_all(mappingStr, " ");
  189. auto colonIndex = mappingStr.find_first_of(":");
  190. const auto & flipMode = mappingStr.substr(0, colonIndex);
  191. terGroupPattern.diffImages = TerrainViewPattern::FLIP_MODE_DIFF_IMAGES == &(flipMode[flipMode.length() - 1]);
  192. if (terGroupPattern.diffImages)
  193. {
  194. terGroupPattern.rotationTypesCount = boost::lexical_cast<int>(flipMode.substr(0, flipMode.length() - 1));
  195. assert(terGroupPattern.rotationTypesCount == 2 || terGroupPattern.rotationTypesCount == 4);
  196. }
  197. mappingStr = mappingStr.substr(colonIndex + 1);
  198. std::vector<std::string> mappings;
  199. boost::split(mappings, mappingStr, boost::is_any_of(","));
  200. for (const std::string & mapping : mappings)
  201. {
  202. std::vector<std::string> range;
  203. boost::split(range, mapping, boost::is_any_of("-"));
  204. terGroupPattern.mapping.push_back(std::make_pair(boost::lexical_cast<int>(range[0]),
  205. boost::lexical_cast<int>(range.size() > 1 ? range[1] : range[0])));
  206. }
  207. // Add pattern to the patterns map
  208. std::vector<TerrainViewPattern> terrainViewPatternFlips{terGroupPattern};
  209. for (int i = 1; i < 4; ++i)
  210. {
  211. //auto p = terGroupPattern;
  212. flipPattern(terGroupPattern, i); //FIXME: we flip in place - doesn't make much sense now, but used to work
  213. terrainViewPatternFlips.push_back(terGroupPattern);
  214. }
  215. terrainViewPatterns[mappingPair.first].push_back(terrainViewPatternFlips);
  216. }
  217. }
  218. else if (i == 1)
  219. {
  220. terrainTypePatterns[pattern.id].push_back(pattern);
  221. for (int i = 1; i < 4; ++i)
  222. {
  223. //auto p = pattern;
  224. flipPattern(pattern, i); ///FIXME: we flip in place - doesn't make much sense now
  225. terrainTypePatterns[pattern.id].push_back(pattern);
  226. }
  227. }
  228. }
  229. }
  230. }
  231. CTerrainViewPatternConfig::~CTerrainViewPatternConfig()
  232. {
  233. }
  234. const std::vector<CTerrainViewPatternConfig::TVPVector> & CTerrainViewPatternConfig::getTerrainViewPatterns(TerrainId terrain) const
  235. {
  236. auto iter = terrainViewPatterns.find(VLC->terrainTypeHandler->getById(terrain)->terrainViewPatterns);
  237. if (iter == terrainViewPatterns.end())
  238. return terrainViewPatterns.at("normal");
  239. return iter->second;
  240. }
  241. boost::optional<const TerrainViewPattern &> CTerrainViewPatternConfig::getTerrainViewPatternById(std::string patternId, const std::string & id) const
  242. {
  243. auto iter = terrainViewPatterns.find(patternId);
  244. const std::vector<TVPVector> & groupPatterns = (iter == terrainViewPatterns.end()) ? terrainViewPatterns.at("normal") : iter->second;
  245. for (const TVPVector & patternFlips : groupPatterns)
  246. {
  247. const TerrainViewPattern & pattern = patternFlips.front();
  248. if (id == pattern.id)
  249. {
  250. return boost::optional<const TerrainViewPattern&>(pattern);
  251. }
  252. }
  253. return boost::optional<const TerrainViewPattern&>();
  254. }
  255. boost::optional<const CTerrainViewPatternConfig::TVPVector &> CTerrainViewPatternConfig::getTerrainViewPatternsById(TerrainId terrain, const std::string & id) const
  256. {
  257. const std::vector<TVPVector> & groupPatterns = getTerrainViewPatterns(terrain);
  258. for (const TVPVector & patternFlips : groupPatterns)
  259. {
  260. const TerrainViewPattern & pattern = patternFlips.front();
  261. if (id == pattern.id)
  262. {
  263. return boost::optional<const TVPVector&>(patternFlips);
  264. }
  265. }
  266. return boost::optional<const TVPVector&>();
  267. }
  268. const CTerrainViewPatternConfig::TVPVector* CTerrainViewPatternConfig::getTerrainTypePatternById(const std::string& id) const
  269. {
  270. auto it = terrainTypePatterns.find(id);
  271. assert(it != terrainTypePatterns.end());
  272. return &(it->second);
  273. }
  274. void CTerrainViewPatternConfig::flipPattern(TerrainViewPattern & pattern, int flip) const
  275. {
  276. //flip in place to avoid expensive constructor. Seriously.
  277. if (flip == 0)
  278. {
  279. return;
  280. }
  281. //always flip horizontal
  282. for (int i = 0; i < 3; ++i)
  283. {
  284. int y = i * 3;
  285. std::swap(pattern.data[y], pattern.data[y + 2]);
  286. }
  287. //flip vertical only at 2nd step
  288. if (flip == CMapOperation::FLIP_PATTERN_VERTICAL)
  289. {
  290. for (int i = 0; i < 3; ++i)
  291. {
  292. std::swap(pattern.data[i], pattern.data[6 + i]);
  293. }
  294. }
  295. }
  296. void CTerrainViewPatternUtils::printDebuggingInfoAboutTile(const CMap * map, int3 pos)
  297. {
  298. logGlobal->debug("Printing detailed info about nearby map tiles of pos '%s'", pos.toString());
  299. for (int y = pos.y - 2; y <= pos.y + 2; ++y)
  300. {
  301. std::string line;
  302. const int PADDED_LENGTH = 10;
  303. for (int x = pos.x - 2; x <= pos.x + 2; ++x)
  304. {
  305. auto debugPos = int3(x, y, pos.z);
  306. if (map->isInTheMap(debugPos))
  307. {
  308. auto debugTile = map->getTile(debugPos);
  309. std::string terType = debugTile.terType->shortIdentifier;
  310. line += terType;
  311. line.insert(line.end(), PADDED_LENGTH - terType.size(), ' ');
  312. }
  313. else
  314. {
  315. line += "X";
  316. line.insert(line.end(), PADDED_LENGTH - 1, ' ');
  317. }
  318. }
  319. logGlobal->debug(line);
  320. }
  321. }
  322. VCMI_LIB_NAMESPACE_END