RoadPlacer.cpp 4.8 KB

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