CMapEditManagerTest.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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)
  21. {
  22. try
  23. {
  24. // Load maps and json config
  25. auto loader = make_shared<CFilesystemLoader>(".");
  26. CResourceHandler::get()->addLoader("test/", loader, false);
  27. const auto originalMap = CMapService::loadMap("test/TerrainViewTest");
  28. auto map = CMapService::loadMap("test/TerrainViewTest");
  29. logGlobal->infoStream() << "Loaded test map successfully.";
  30. // Validate edit manager
  31. auto editManager = map->getEditManager();
  32. CRandomGenerator gen;
  33. const JsonNode viewNode(ResourceID("test/terrainViewMappings", EResType::TEXT));
  34. const auto & mappingsNode = viewNode["mappings"].Vector();
  35. BOOST_FOREACH(const auto & node, mappingsNode)
  36. {
  37. // Get terrain group and id
  38. const auto & patternStr = node["pattern"].String();
  39. std::vector<std::string> patternParts;
  40. boost::split(patternParts, patternStr, boost::is_any_of("."));
  41. if(patternParts.size() != 2) throw std::runtime_error("A pattern should consist of two parts, the group and the id. Continue with next pattern.");
  42. const auto & groupStr = patternParts[0];
  43. const auto & id = patternParts[1];
  44. auto terGroup = CTerrainViewPatternConfig::get().getTerrainGroup(groupStr);
  45. // Get mapping range
  46. const auto & pattern = CTerrainViewPatternConfig::get().getPatternById(terGroup, id);
  47. const auto & mapping = pattern.mapping;
  48. const auto & positionsNode = node["pos"].Vector();
  49. BOOST_FOREACH(const auto & posNode, positionsNode)
  50. {
  51. const auto & posVector = posNode.Vector();
  52. if(posVector.size() != 3) throw std::runtime_error("A position should consist of three values x,y,z. Continue with next position.");
  53. int3 pos(posVector[0].Float(), posVector[1].Float(), posVector[2].Float());
  54. logGlobal->infoStream() << boost::format("Test pattern '%s' on position x '%d', y '%d', z '%d'.") % patternStr % pos.x % pos.y % pos.z;
  55. const auto & originalTile = originalMap->getTile(pos);
  56. editManager->drawTerrain(MapRect(pos, 1, 1), originalTile.terType, &gen);
  57. const auto & tile = map->getTile(pos);
  58. bool isInRange = false;
  59. BOOST_FOREACH(const auto & range, mapping)
  60. {
  61. if(tile.terView >= range.first && tile.terView <= range.second)
  62. {
  63. isInRange = true;
  64. break;
  65. }
  66. }
  67. BOOST_CHECK(isInRange);
  68. if(!isInRange) logGlobal->errorStream() << "No or invalid pattern found for current position.";
  69. }
  70. }
  71. }
  72. catch(const std::exception & e)
  73. {
  74. logGlobal-> errorStream() << e.what();
  75. }
  76. }