CMapEditManagerTest.cpp 5.2 KB

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