CMapEditManagerTest.cpp 6.2 KB

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