RoadPlacer.cpp 2.9 KB

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