RoadPlacer.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 "../Functions.h"
  15. #include "../CMapGenerator.h"
  16. #include "../threadpool/MapProxy.h"
  17. #include "../../CModHandler.h"
  18. #include "../../mapping/CMapEditManager.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. //RecursiveLock lock(externalAccessMutex);
  66. {
  67. //FIXME: double lock - unsafe
  68. Zone::Lock lock(zone.areaMutex);
  69. zone.areaPossible().subtract(roads);
  70. zone.freePaths().unite(roads);
  71. }
  72. auto tiles = roads.getTilesVector();
  73. std::string roadName = (secondary ? generator.getConfig().secondaryRoadType : generator.getConfig().defaultRoadType);
  74. RoadId roadType(*VLC->modh->identifiers.getIdentifier(CModHandler::scopeGame(), "road", roadName));
  75. mapProxy->drawRoads(zone.getRand(), tiles, roadType);
  76. }
  77. void RoadPlacer::addRoadNode(const int3& node)
  78. {
  79. RecursiveLock lock(externalAccessMutex);
  80. roadNodes.insert(node);
  81. }
  82. void RoadPlacer::connectRoads()
  83. {
  84. bool noRoadNodes = false;
  85. //Assumes objects are already placed
  86. if (roadNodes.size() < 2)
  87. {
  88. //If there are no nodes, draw roads to mines
  89. noRoadNodes = true;
  90. if (auto* m = zone.getModificator<ObjectManager>())
  91. {
  92. for(auto * object : m->getMines())
  93. {
  94. addRoadNode(object->visitablePos());
  95. }
  96. }
  97. }
  98. if(roadNodes.size() < 2)
  99. return;
  100. //take any tile from road nodes as destination zone for all other road nodes
  101. RecursiveLock lock(externalAccessMutex);
  102. if(roads.empty())
  103. roads.add(*roadNodes.begin());
  104. for(const auto & node : roadNodes)
  105. {
  106. createRoad(node);
  107. }
  108. //Draw dirt roads if there are only mines
  109. drawRoads(noRoadNodes);
  110. }
  111. char RoadPlacer::dump(const int3 & t)
  112. {
  113. if(roadNodes.count(t))
  114. return '@';
  115. if(roads.contains(t))
  116. return '+';
  117. if(isolated.contains(t))
  118. return 'i';
  119. return Modificator::dump(t);
  120. }
  121. VCMI_LIB_NAMESPACE_END