RoadPlacer.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 roadType = (secondary ? generator.getConfig().secondaryRoadType : generator.getConfig().defaultRoadType);
  64. map.getEditManager()->drawRoad(roadType, &generator.rand);
  65. }
  66. void RoadPlacer::addRoadNode(const int3& node)
  67. {
  68. roadNodes.insert(node);
  69. }
  70. void RoadPlacer::connectRoads()
  71. {
  72. bool noRoadNodes = false;
  73. //Assumes objects are already placed
  74. if (roadNodes.size() < 2)
  75. {
  76. //If there are no nodes, draw roads to mines
  77. noRoadNodes = true;
  78. if (auto* m = zone.getModificator<ObjectManager>())
  79. {
  80. for (auto object : m->getMines())
  81. {
  82. addRoadNode(object->visitablePos());
  83. }
  84. }
  85. }
  86. if(roadNodes.size() < 2)
  87. return;
  88. //take any tile from road nodes as destination zone for all other road nodes
  89. if(roads.empty())
  90. roads.add(*roadNodes.begin());
  91. for(auto & node : roadNodes)
  92. {
  93. createRoad(node);
  94. }
  95. //Draw dirt roads if there are only mines
  96. drawRoads(noRoadNodes);
  97. }
  98. char RoadPlacer::dump(const int3 & t)
  99. {
  100. if(roadNodes.count(t))
  101. return '@';
  102. if(roads.contains(t))
  103. return '+';
  104. if(isolated.contains(t))
  105. return 'i';
  106. return Modificator::dump(t);
  107. }
  108. VCMI_LIB_NAMESPACE_END