RoadPlacer.cpp 5.3 KB

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