RoadPlacer.cpp 2.9 KB

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