RoadPlacer.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 "Functions.h"
  14. #include "CMapGenerator.h"
  15. #include "RmgMap.h"
  16. #include "../mapping/CMap.h"
  17. #include "../mapping/CMapEditManager.h"
  18. #include "RmgPath.h"
  19. void RoadPlacer::process()
  20. {
  21. connectRoads();
  22. }
  23. rmg::Area & RoadPlacer::areaForRoads()
  24. {
  25. return areaRoads;
  26. }
  27. rmg::Area & RoadPlacer::areaIsolated()
  28. {
  29. return isolated;
  30. }
  31. const rmg::Area & RoadPlacer::getRoads() const
  32. {
  33. return roads;
  34. }
  35. bool RoadPlacer::createRoad(const int3 & dst)
  36. {
  37. auto searchArea = zone.areaPossible() + areaRoads + zone.freePaths() - isolated + roads;
  38. rmg::Path path(searchArea);
  39. path.connect(roads);
  40. auto res = path.search(dst, true);
  41. if(!res.valid())
  42. {
  43. res = path.search(dst, false, [](const int3 & src, const int3 & dst)
  44. {
  45. float weight = dst.dist2dSQ(src);
  46. return weight * weight;
  47. });
  48. if(!res.valid())
  49. {
  50. logGlobal->warn("Failed to create road");
  51. return false;
  52. }
  53. }
  54. roads.unite(res.getPathArea());
  55. return true;
  56. }
  57. void RoadPlacer::drawRoads(bool secondary)
  58. {
  59. zone.areaPossible().subtract(roads);
  60. zone.freePaths().unite(roads);
  61. map.getEditManager()->getTerrainSelection().setSelection(roads.getTilesVector());
  62. std::string roadType = (secondary ? generator.getConfig().secondaryRoadType : generator.getConfig().defaultRoadType);
  63. map.getEditManager()->drawRoad(roadType, &generator.rand);
  64. }
  65. void RoadPlacer::addRoadNode(const int3& node)
  66. {
  67. roadNodes.insert(node);
  68. }
  69. void RoadPlacer::connectRoads()
  70. {
  71. bool noRoadNodes = false;
  72. //Assumes objects are already placed
  73. if (roadNodes.size() < 2)
  74. {
  75. //If there are no nodes, draw roads to mines
  76. noRoadNodes = true;
  77. if (auto* m = zone.getModificator<ObjectManager>())
  78. {
  79. for (auto object : m->getMines())
  80. {
  81. addRoadNode(object->visitablePos());
  82. }
  83. }
  84. }
  85. if(roadNodes.size() < 2)
  86. return;
  87. //take any tile from road nodes as destination zone for all other road nodes
  88. if(roads.empty())
  89. roads.add(*roadNodes.begin());
  90. for(auto & node : roadNodes)
  91. {
  92. createRoad(node);
  93. }
  94. //Draw dirt roads if there are only mines
  95. drawRoads(noRoadNodes);
  96. }
  97. char RoadPlacer::dump(const int3 & t)
  98. {
  99. if(roadNodes.count(t))
  100. return '@';
  101. if(roads.contains(t))
  102. return '+';
  103. if(isolated.contains(t))
  104. return 'i';
  105. return Modificator::dump(t);
  106. }