CMapEditManagerTest.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. }
  67. catch(const std::exception & e)
  68. {
  69. logGlobal-> errorStream() << e.what();
  70. }
  71. }
  72. BOOST_AUTO_TEST_CASE(CMapEditManager_DrawTerrain_View)
  73. {
  74. try
  75. {
  76. // Load maps and json config
  77. #if defined(__MINGW32__)
  78. const std::string TEST_DATA_DIR = "test/";
  79. #else
  80. const std::string TEST_DATA_DIR = ".";
  81. #endif // defined
  82. auto loader = new CFilesystemLoader("test/", TEST_DATA_DIR);
  83. dynamic_cast<CFilesystemList*>(CResourceHandler::get())->addLoader(loader, false);
  84. const auto originalMap = CMapService::loadMap("test/TerrainViewTest");
  85. auto map = CMapService::loadMap("test/TerrainViewTest");
  86. logGlobal->infoStream() << "Loaded test map successfully.";
  87. // Validate edit manager
  88. auto editManager = map->getEditManager();
  89. CRandomGenerator gen;
  90. const JsonNode viewNode(ResourceID("test/terrainViewMappings", EResType::TEXT));
  91. const auto & mappingsNode = viewNode["mappings"].Vector();
  92. for (const auto & node : mappingsNode)
  93. {
  94. // Get terrain group and id
  95. const auto & patternStr = node["pattern"].String();
  96. std::vector<std::string> patternParts;
  97. boost::split(patternParts, patternStr, boost::is_any_of("."));
  98. if(patternParts.size() != 2) throw std::runtime_error("A pattern should consist of two parts, the group and the id. Continue with next pattern.");
  99. const auto & groupStr = patternParts[0];
  100. const auto & id = patternParts[1];
  101. auto terGroup = VLC->terviewh->getTerrainGroup(groupStr);
  102. // Get mapping range
  103. const auto & pattern = VLC->terviewh->getTerrainViewPatternById(terGroup, id);
  104. const auto & mapping = (*pattern).mapping;
  105. const auto & positionsNode = node["pos"].Vector();
  106. for (const auto & posNode : positionsNode)
  107. {
  108. const auto & posVector = posNode.Vector();
  109. if(posVector.size() != 3) throw std::runtime_error("A position should consist of three values x,y,z. Continue with next position.");
  110. int3 pos(posVector[0].Float(), posVector[1].Float(), posVector[2].Float());
  111. logGlobal->infoStream() << boost::format("Test pattern '%s' on position x '%d', y '%d', z '%d'.") % patternStr % pos.x % pos.y % pos.z;
  112. const auto & originalTile = originalMap->getTile(pos);
  113. editManager->getTerrainSelection().selectRange(MapRect(pos, 1, 1));
  114. editManager->drawTerrain(originalTile.terType, &gen);
  115. const auto & tile = map->getTile(pos);
  116. bool isInRange = false;
  117. for(const auto & range : mapping)
  118. {
  119. if(tile.terView >= range.first && tile.terView <= range.second)
  120. {
  121. isInRange = true;
  122. break;
  123. }
  124. }
  125. BOOST_CHECK(isInRange);
  126. if(!isInRange) logGlobal->errorStream() << "No or invalid pattern found for current position.";
  127. }
  128. }
  129. }
  130. catch(const std::exception & e)
  131. {
  132. logGlobal-> errorStream() << e.what();
  133. }
  134. }