RoadPlacer.cpp 4.9 KB

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