CMapEditManagerTest.cpp 6.0 KB

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