RoadPlacer.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * RoadPlacer.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 "RoadPlacer.h"
  12. #include "ObjectManager.h"
  13. #include "Functions.h"
  14. #include "CMapGenerator.h"
  15. #include "RmgMap.h"
  16. #include "../mapping/CMap.h"
  17. #include "../mapping/CMapEditManager.h"
  18. #include "RmgPath.h"
  19. VCMI_LIB_NAMESPACE_BEGIN
  20. void RoadPlacer::process()
  21. {
  22. connectRoads();
  23. }
  24. rmg::Area & RoadPlacer::areaForRoads()
  25. {
  26. return areaRoads;
  27. }
  28. rmg::Area & RoadPlacer::areaIsolated()
  29. {
  30. return isolated;
  31. }
  32. const rmg::Area & RoadPlacer::getRoads() const
  33. {
  34. return roads;
  35. }
  36. bool RoadPlacer::createRoad(const int3 & dst)
  37. {
  38. auto searchArea = zone.areaPossible() + areaRoads + zone.freePaths() - isolated + roads;
  39. rmg::Path path(searchArea);
  40. path.connect(roads);
  41. auto res = path.search(dst, true);
  42. if(!res.valid())
  43. {
  44. res = path.search(dst, false, [](const int3 & src, const int3 & dst)
  45. {
  46. float weight = dst.dist2dSQ(src);
  47. return weight * weight;
  48. });
  49. if(!res.valid())
  50. {
  51. logGlobal->warn("Failed to create road");
  52. return false;
  53. }
  54. }
  55. roads.unite(res.getPathArea());
  56. return true;
  57. }
  58. void RoadPlacer::drawRoads(bool secondary)
  59. {
  60. zone.areaPossible().subtract(roads);
  61. zone.freePaths().unite(roads);
  62. map.getEditManager()->getTerrainSelection().setSelection(roads.getTilesVector());
  63. std::string roadCode = (secondary ? generator.getConfig().secondaryRoadType : generator.getConfig().defaultRoadType);
  64. RoadId roadType = VLC->terrainTypeHandler->getRoadByCode(roadCode)->id;
  65. map.getEditManager()->drawRoad(roadType, &generator.rand);
  66. }
  67. void RoadPlacer::addRoadNode(const int3& node)
  68. {
  69. roadNodes.insert(node);
  70. }
  71. void RoadPlacer::connectRoads()
  72. {
  73. bool noRoadNodes = false;
  74. //Assumes objects are already placed
  75. if (roadNodes.size() < 2)
  76. {
  77. //If there are no nodes, draw roads to mines
  78. noRoadNodes = true;
  79. if (auto* m = zone.getModificator<ObjectManager>())
  80. {
  81. for (auto object : m->getMines())
  82. {
  83. addRoadNode(object->visitablePos());
  84. }
  85. }
  86. }
  87. if(roadNodes.size() < 2)
  88. return;
  89. //take any tile from road nodes as destination zone for all other road nodes
  90. if(roads.empty())
  91. roads.add(*roadNodes.begin());
  92. for(auto & node : roadNodes)
  93. {
  94. createRoad(node);
  95. }
  96. //Draw dirt roads if there are only mines
  97. drawRoads(noRoadNodes);
  98. }
  99. char RoadPlacer::dump(const int3 & t)
  100. {
  101. if(roadNodes.count(t))
  102. return '@';
  103. if(roads.contains(t))
  104. return '+';
  105. if(isolated.contains(t))
  106. return 'i';
  107. return Modificator::dump(t);
  108. }
  109. VCMI_LIB_NAMESPACE_END