RoadPlacer.cpp 5.4 KB

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