RoadPlacer.cpp 5.2 KB

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