CMapEditManagerTest.cpp 7.0 KB

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