CMapEditManagerTest.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. try
  25. {
  26. auto map = make_unique<CMap>();
  27. map->width = 100;
  28. map->height = 100;
  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. // 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(ETerrainType::SUBTERRANEAN);
  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(ETerrainType::ROCK);
  87. }
  88. catch(const std::exception & e)
  89. {
  90. logGlobal-> errorStream() << e.what();
  91. }
  92. }
  93. BOOST_AUTO_TEST_CASE(CMapEditManager_DrawTerrain_View)
  94. {
  95. try
  96. {
  97. // Load maps and json config
  98. // #if defined(__MINGW32__)
  99. const std::string TEST_DATA_DIR = "test/";
  100. // #else
  101. // const std::string TEST_DATA_DIR = ".";
  102. // #endif // defined
  103. auto loader = new CFilesystemLoader("test/", TEST_DATA_DIR);
  104. dynamic_cast<CFilesystemList*>(CResourceHandler::get())->addLoader(loader, false);
  105. const auto originalMap = CMapService::loadMap("test/TerrainViewTest");
  106. auto map = CMapService::loadMap("test/TerrainViewTest");
  107. logGlobal->infoStream() << "Loaded test map successfully.";
  108. // Validate edit manager
  109. auto editManager = map->getEditManager();
  110. CRandomGenerator gen;
  111. const JsonNode viewNode(ResourceID("test/terrainViewMappings", EResType::TEXT));
  112. const auto & mappingsNode = viewNode["mappings"].Vector();
  113. for (const auto & node : mappingsNode)
  114. {
  115. // Get terrain group and id
  116. const auto & patternStr = node["pattern"].String();
  117. std::vector<std::string> patternParts;
  118. boost::split(patternParts, patternStr, boost::is_any_of("."));
  119. if(patternParts.size() != 2) throw std::runtime_error("A pattern should consist of two parts, the group and the id. Continue with next pattern.");
  120. const auto & groupStr = patternParts[0];
  121. const auto & id = patternParts[1];
  122. auto terGroup = VLC->terviewh->getTerrainGroup(groupStr);
  123. // Get mapping range
  124. const auto & pattern = VLC->terviewh->getTerrainViewPatternById(terGroup, id);
  125. const auto & mapping = (*pattern).mapping;
  126. const auto & positionsNode = node["pos"].Vector();
  127. for (const auto & posNode : positionsNode)
  128. {
  129. const auto & posVector = posNode.Vector();
  130. if(posVector.size() != 3) throw std::runtime_error("A position should consist of three values x,y,z. Continue with next position.");
  131. int3 pos(posVector[0].Float(), posVector[1].Float(), posVector[2].Float());
  132. logGlobal->infoStream() << boost::format("Test pattern '%s' on position x '%d', y '%d', z '%d'.") % patternStr % pos.x % pos.y % pos.z;
  133. CTerrainViewPatternUtils::printDebuggingInfoAboutTile(map.get(), pos);
  134. const auto & originalTile = originalMap->getTile(pos);
  135. editManager->getTerrainSelection().selectRange(MapRect(pos, 1, 1));
  136. editManager->drawTerrain(originalTile.terType, &gen);
  137. const auto & tile = map->getTile(pos);
  138. bool isInRange = false;
  139. for(const auto & range : mapping)
  140. {
  141. if(tile.terView >= range.first && tile.terView <= range.second)
  142. {
  143. isInRange = true;
  144. break;
  145. }
  146. }
  147. BOOST_CHECK(isInRange);
  148. if(!isInRange) logGlobal->errorStream() << "No or invalid pattern found for current position.";
  149. }
  150. }
  151. }
  152. catch(const std::exception & e)
  153. {
  154. logGlobal-> errorStream() << e.what();
  155. }
  156. }