CMapEditManagerTest.cpp 6.8 KB

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