RoadPlacer.cpp 5.7 KB

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