CMapEditManagerTest.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * CMapEditManagerTest.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 "../lib/filesystem/ResourceID.h"
  12. #include "../lib/mapping/CMapService.h"
  13. #include "../lib/mapping/CMap.h"
  14. #include "../lib/JsonNode.h"
  15. #include "../lib/mapping/CMapEditManager.h"
  16. #include "../lib/int3.h"
  17. #include "../lib/CRandomGenerator.h"
  18. #include "../lib/VCMI_Lib.h"
  19. TEST(MapManager, DrawTerrain_Type)
  20. {
  21. try
  22. {
  23. auto map = make_unique<CMap>();
  24. map->width = 52;
  25. map->height = 52;
  26. map->initTerrain();
  27. auto editManager = map->getEditManager();
  28. editManager->clearTerrain();
  29. // 1x1 Blow up
  30. editManager->getTerrainSelection().select(int3(5, 5, 0));
  31. editManager->drawTerrain(ETerrainType::GRASS);
  32. static const int3 squareCheck[] = { int3(5,5,0), int3(5,4,0), int3(4,4,0), int3(4,5,0) };
  33. for(int i = 0; i < ARRAY_COUNT(squareCheck); ++i)
  34. {
  35. EXPECT_EQ(map->getTile(squareCheck[i]).terType, ETerrainType::GRASS);
  36. }
  37. // Concat to square
  38. editManager->getTerrainSelection().select(int3(6, 5, 0));
  39. editManager->drawTerrain(ETerrainType::GRASS);
  40. EXPECT_EQ(map->getTile(int3(6, 4, 0)).terType, ETerrainType::GRASS);
  41. editManager->getTerrainSelection().select(int3(6, 5, 0));
  42. editManager->drawTerrain(ETerrainType::LAVA);
  43. EXPECT_EQ(map->getTile(int3(4, 4, 0)).terType, ETerrainType::GRASS);
  44. EXPECT_EQ(map->getTile(int3(7, 4, 0)).terType, ETerrainType::LAVA);
  45. // Special case water,rock
  46. editManager->getTerrainSelection().selectRange(MapRect(int3(10, 10, 0), 10, 5));
  47. editManager->drawTerrain(ETerrainType::GRASS);
  48. editManager->getTerrainSelection().selectRange(MapRect(int3(15, 17, 0), 10, 5));
  49. editManager->drawTerrain(ETerrainType::GRASS);
  50. editManager->getTerrainSelection().select(int3(21, 16, 0));
  51. editManager->drawTerrain(ETerrainType::GRASS);
  52. EXPECT_EQ(map->getTile(int3(20, 15, 0)).terType, ETerrainType::GRASS);
  53. // Special case non water,rock
  54. static const int3 diagonalCheck[] = { int3(31,42,0), int3(32,42,0), int3(32,43,0), int3(33,43,0), int3(33,44,0),
  55. int3(34,44,0), int3(34,45,0), int3(35,45,0), int3(35,46,0), int3(36,46,0),
  56. int3(36,47,0), int3(37,47,0)};
  57. for(int i = 0; i < ARRAY_COUNT(diagonalCheck); ++i)
  58. {
  59. editManager->getTerrainSelection().select(diagonalCheck[i]);
  60. }
  61. editManager->drawTerrain(ETerrainType::GRASS);
  62. EXPECT_EQ(map->getTile(int3(35, 44, 0)).terType, ETerrainType::WATER);
  63. // Rock case
  64. editManager->getTerrainSelection().selectRange(MapRect(int3(1, 1, 1), 15, 15));
  65. editManager->drawTerrain(ETerrainType::SUBTERRANEAN);
  66. std::vector<int3> vec({ int3(6, 6, 1), int3(7, 6, 1), int3(8, 6, 1), int3(5, 7, 1), int3(6, 7, 1), int3(7, 7, 1),
  67. int3(8, 7, 1), int3(4, 8, 1), int3(5, 8, 1), int3(6, 8, 1)});
  68. editManager->getTerrainSelection().setSelection(vec);
  69. editManager->drawTerrain(ETerrainType::ROCK);
  70. EXPECT_TRUE(map->getTile(int3(5, 6, 1)).terType == ETerrainType::ROCK || map->getTile(int3(7, 8, 1)).terType == ETerrainType::ROCK);
  71. //todo: add checks here and enable, also use smaller size
  72. #if 0
  73. // next check
  74. auto map2 = make_unique<CMap>();
  75. map2->width = 128;
  76. map2->height = 128;
  77. map2->initTerrain();
  78. auto editManager2 = map2->getEditManager();
  79. editManager2->getTerrainSelection().selectRange(MapRect(int3(0, 0, 1), 128, 128));
  80. editManager2->drawTerrain(ETerrainType::SUBTERRANEAN);
  81. std::vector<int3> selection({ int3(95, 43, 1), int3(95, 44, 1), int3(94, 45, 1), int3(95, 45, 1), int3(96, 45, 1),
  82. int3(93, 46, 1), int3(94, 46, 1), int3(95, 46, 1), int3(96, 46, 1), int3(97, 46, 1),
  83. int3(98, 46, 1), int3(99, 46, 1)});
  84. editManager2->getTerrainSelection().setSelection(selection);
  85. editManager2->drawTerrain(ETerrainType::ROCK);
  86. #endif // 0
  87. }
  88. catch(const std::exception & e)
  89. {
  90. FAIL()<<e.what();
  91. throw;
  92. }
  93. }
  94. TEST(MapManager, DrawTerrain_View)
  95. {
  96. try
  97. {
  98. const ResourceID testMap("test/TerrainViewTest", EResType::MAP);
  99. // Load maps and json config
  100. const auto originalMap = CMapService::loadMap(testMap);
  101. auto map = CMapService::loadMap(testMap);
  102. // Validate edit manager
  103. auto editManager = map->getEditManager();
  104. CRandomGenerator gen;
  105. const JsonNode viewNode(ResourceID("test/terrainViewMappings", EResType::TEXT));
  106. const auto & mappingsNode = viewNode["mappings"].Vector();
  107. for (const auto & node : mappingsNode)
  108. {
  109. // Get terrain group and id
  110. const auto & patternStr = node["pattern"].String();
  111. std::vector<std::string> patternParts;
  112. boost::split(patternParts, patternStr, boost::is_any_of("."));
  113. if(patternParts.size() != 2) throw std::runtime_error("A pattern should consist of two parts, the group and the id. Continue with next pattern.");
  114. const auto & groupStr = patternParts[0];
  115. const auto & id = patternParts[1];
  116. auto terGroup = VLC->terviewh->getTerrainGroup(groupStr);
  117. // Get mapping range
  118. const auto & pattern = VLC->terviewh->getTerrainViewPatternById(terGroup, id);
  119. const auto & mapping = (*pattern).mapping;
  120. const auto & positionsNode = node["pos"].Vector();
  121. for (const auto & posNode : positionsNode)
  122. {
  123. const auto & posVector = posNode.Vector();
  124. if(posVector.size() != 3) throw std::runtime_error("A position should consist of three values x,y,z. Continue with next position.");
  125. int3 pos(posVector[0].Float(), posVector[1].Float(), posVector[2].Float());
  126. const auto & originalTile = originalMap->getTile(pos);
  127. editManager->getTerrainSelection().selectRange(MapRect(pos, 1, 1));
  128. editManager->drawTerrain(originalTile.terType, &gen);
  129. const auto & tile = map->getTile(pos);
  130. bool isInRange = false;
  131. for(const auto & range : mapping)
  132. {
  133. if(tile.terView >= range.first && tile.terView <= range.second)
  134. {
  135. isInRange = true;
  136. break;
  137. }
  138. }
  139. EXPECT_TRUE(isInRange);
  140. if(!isInRange)
  141. FAIL()<<("No or invalid pattern found for current position.");
  142. }
  143. }
  144. }
  145. catch(const std::exception & e)
  146. {
  147. FAIL()<<e.what();
  148. throw;
  149. }
  150. }