RoadPlacer.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "ObstaclePlacer.h"
  14. #include "RockFiller.h"
  15. #include "../Functions.h"
  16. #include "../CMapGenerator.h"
  17. #include "../threadpool/MapProxy.h"
  18. #include "../../CModHandler.h"
  19. #include "../../mapping/CMapEditManager.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. void RoadPlacer::init()
  28. {
  29. if (zone.isUnderground())
  30. {
  31. DEPENDENCY_ALL(RockFiller);
  32. }
  33. }
  34. rmg::Area & RoadPlacer::areaForRoads()
  35. {
  36. return areaRoads;
  37. }
  38. rmg::Area & RoadPlacer::areaIsolated()
  39. {
  40. return isolated;
  41. }
  42. const rmg::Area & RoadPlacer::getRoads() const
  43. {
  44. return roads;
  45. }
  46. bool RoadPlacer::createRoad(const int3 & dst)
  47. {
  48. auto searchArea = zone.areaPossible() + areaRoads + zone.freePaths() - isolated + roads;
  49. rmg::Path path(searchArea);
  50. path.connect(roads);
  51. auto res = path.search(dst, true);
  52. if(!res.valid())
  53. {
  54. res = path.search(dst, false, [](const int3 & src, const int3 & dst)
  55. {
  56. float weight = dst.dist2dSQ(src);
  57. return weight * weight;
  58. });
  59. if(!res.valid())
  60. {
  61. logGlobal->warn("Failed to create road");
  62. return false;
  63. }
  64. }
  65. roads.unite(res.getPathArea());
  66. return true;
  67. }
  68. void RoadPlacer::drawRoads(bool secondary)
  69. {
  70. {
  71. //Clean space under roads even if they won't be eventually generated
  72. Zone::Lock lock(zone.areaMutex);
  73. zone.areaPossible().subtract(roads);
  74. zone.freePaths().unite(roads);
  75. }
  76. if (!generator.getMapGenOptions().isRoadEnabled())
  77. {
  78. return;
  79. }
  80. if((secondary && generator.getConfig().secondaryRoadType.empty())
  81. || (!secondary && generator.getConfig().defaultRoadType.empty()))
  82. return;
  83. //TODO: Allow custom road type for object
  84. //TODO: Remove these default types
  85. auto tiles = roads.getTilesVector();
  86. std::string roadName = (secondary ? generator.getConfig().secondaryRoadType : generator.getConfig().defaultRoadType);
  87. RoadId roadType(*VLC->modh->identifiers.getIdentifier(CModHandler::scopeGame(), "road", roadName));
  88. //If our road type is not enabled, choose highest below it
  89. for (int8_t bestRoad = roadType.getNum(); bestRoad > RoadId(Road::NO_ROAD).getNum(); bestRoad--)
  90. {
  91. if (generator.getMapGenOptions().isRoadEnabled(RoadId(bestRoad)))
  92. {
  93. mapProxy->drawRoads(zone.getRand(), tiles, RoadId(bestRoad));
  94. return;
  95. }
  96. }
  97. }
  98. void RoadPlacer::addRoadNode(const int3& node)
  99. {
  100. RecursiveLock lock(externalAccessMutex);
  101. roadNodes.insert(node);
  102. }
  103. void RoadPlacer::connectRoads()
  104. {
  105. bool noRoadNodes = false;
  106. //Assumes objects are already placed
  107. if (roadNodes.size() < 2)
  108. {
  109. //If there are no nodes, draw roads to mines
  110. noRoadNodes = true;
  111. if (auto* m = zone.getModificator<ObjectManager>())
  112. {
  113. for(auto * object : m->getMines())
  114. {
  115. addRoadNode(object->visitablePos());
  116. }
  117. }
  118. }
  119. if(roadNodes.size() < 2)
  120. return;
  121. //take any tile from road nodes as destination zone for all other road nodes
  122. RecursiveLock lock(externalAccessMutex);
  123. if(roads.empty())
  124. roads.add(*roadNodes.begin());
  125. for(const auto & node : roadNodes)
  126. {
  127. createRoad(node);
  128. }
  129. //Draw dirt roads if there are only mines
  130. drawRoads(noRoadNodes);
  131. }
  132. char RoadPlacer::dump(const int3 & t)
  133. {
  134. if(roadNodes.count(t))
  135. return '@';
  136. if(roads.contains(t))
  137. return '+';
  138. if(isolated.contains(t))
  139. return 'i';
  140. return Modificator::dump(t);
  141. }
  142. VCMI_LIB_NAMESPACE_END