RoadPlacer.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 "../../mapping/CMapEditManager.h"
  19. #include "../../modding/IdentifierStorage.h"
  20. #include "../../modding/ModScope.h"
  21. #include "../../TerrainHandler.h"
  22. VCMI_LIB_NAMESPACE_BEGIN
  23. class TerrainType;
  24. void RoadPlacer::process()
  25. {
  26. if(generator.getConfig().defaultRoadType.empty() && generator.getConfig().secondaryRoadType.empty())
  27. return; //do not generate roads at all
  28. connectRoads();
  29. }
  30. void RoadPlacer::init()
  31. {
  32. if(zone.isUnderground())
  33. {
  34. DEPENDENCY_ALL(RockFiller);
  35. }
  36. }
  37. rmg::Area & RoadPlacer::areaForRoads()
  38. {
  39. return areaRoads;
  40. }
  41. rmg::Area & RoadPlacer::areaIsolated()
  42. {
  43. return isolated;
  44. }
  45. const rmg::Area & RoadPlacer::getRoads() const
  46. {
  47. return roads;
  48. }
  49. bool RoadPlacer::createRoad(const int3 & dst)
  50. {
  51. auto searchArea = zone.areaPossible() + zone.freePaths() + areaRoads + roads;
  52. rmg::Path path(searchArea);
  53. path.connect(roads);
  54. auto simpleRoutig = [this](const int3& src, const int3& dst)
  55. {
  56. if(areaIsolated().contains(dst))
  57. {
  58. return 1000.0f; //Do not route road behind objects that are not visitable from top
  59. }
  60. else
  61. {
  62. return 1.0f;
  63. }
  64. };
  65. auto res = path.search(dst, true, simpleRoutig);
  66. if(!res.valid())
  67. {
  68. auto desperateRoutig = [this](const int3& src, const int3& dst) -> float
  69. {
  70. //Do not allow connections straight up through object not visitable from top
  71. if(std::abs((src - dst).y) == 1)
  72. {
  73. if(areaIsolated().contains(dst) || areaIsolated().contains(src))
  74. {
  75. return 1e12;
  76. }
  77. }
  78. else
  79. {
  80. if(areaIsolated().contains(dst))
  81. {
  82. return 1e6;
  83. }
  84. }
  85. float weight = dst.dist2dSQ(src);
  86. return weight * weight;
  87. };
  88. res = path.search(dst, false, desperateRoutig);
  89. if(!res.valid())
  90. {
  91. logGlobal->warn("Failed to create road to node %s", dst.toString());
  92. return false;
  93. }
  94. }
  95. roads.unite(res.getPathArea());
  96. return true;
  97. }
  98. void RoadPlacer::drawRoads(bool secondary)
  99. {
  100. {
  101. //Clean space under roads even if they won't be eventually generated
  102. Zone::Lock lock(zone.areaMutex);
  103. //Do not draw roads on underground rock or water
  104. roads.erase_if([this](const int3& pos) -> bool
  105. {
  106. const auto* terrain = map.getTile(pos).terType;
  107. return !terrain->isPassable() || !terrain->isLand();
  108. });
  109. zone.areaPossible().subtract(roads);
  110. zone.freePaths().unite(roads);
  111. }
  112. if(!generator.getMapGenOptions().isRoadEnabled())
  113. {
  114. return;
  115. }
  116. if((secondary && generator.getConfig().secondaryRoadType.empty())
  117. || (!secondary && generator.getConfig().defaultRoadType.empty()))
  118. return;
  119. //TODO: Allow custom road type for object
  120. //TODO: Remove these default types
  121. auto tiles = roads.getTilesVector();
  122. std::string roadName = (secondary ? generator.getConfig().secondaryRoadType : generator.getConfig().defaultRoadType);
  123. RoadId roadType(*VLC->identifiers()->getIdentifier(ModScope::scopeGame(), "road", roadName));
  124. //If our road type is not enabled, choose highest below it
  125. for (int8_t bestRoad = roadType.getNum(); bestRoad > RoadId(Road::NO_ROAD).getNum(); bestRoad--)
  126. {
  127. if(generator.getMapGenOptions().isRoadEnabled(RoadId(bestRoad)))
  128. {
  129. mapProxy->drawRoads(zone.getRand(), tiles, RoadId(bestRoad));
  130. return;
  131. }
  132. }
  133. }
  134. void RoadPlacer::addRoadNode(const int3& node)
  135. {
  136. RecursiveLock lock(externalAccessMutex);
  137. roadNodes.insert(node);
  138. }
  139. void RoadPlacer::connectRoads()
  140. {
  141. bool noRoadNodes = false;
  142. //Assumes objects are already placed
  143. if(roadNodes.size() < 2)
  144. {
  145. //If there are no nodes, draw roads to mines
  146. noRoadNodes = true;
  147. if(auto* m = zone.getModificator<ObjectManager>())
  148. {
  149. for(auto * object : m->getMines())
  150. {
  151. addRoadNode(object->visitablePos());
  152. }
  153. }
  154. }
  155. if(roadNodes.size() < 2)
  156. return;
  157. //take any tile from road nodes as destination zone for all other road nodes
  158. RecursiveLock lock(externalAccessMutex);
  159. if(roads.empty())
  160. roads.add(*roadNodes.begin());
  161. for(const auto & node : roadNodes)
  162. {
  163. try
  164. {
  165. createRoad(node);
  166. }
  167. catch (const rmgException& e)
  168. {
  169. logGlobal->warn("Handled exception while drawing road to node %s: %s", node.toString(), e.what());
  170. }
  171. catch (const std::exception & e)
  172. {
  173. logGlobal->error("Unhandled exception while drawing road to node %s: %s", node.toString(), e.what());
  174. throw;
  175. }
  176. }
  177. //Draw dirt roads if there are only mines
  178. drawRoads(noRoadNodes);
  179. }
  180. char RoadPlacer::dump(const int3 & t)
  181. {
  182. if(roadNodes.count(t))
  183. return '@';
  184. if(roads.contains(t))
  185. return '+';
  186. if(isolated.contains(t))
  187. return 'i';
  188. return Modificator::dump(t);
  189. }
  190. VCMI_LIB_NAMESPACE_END