RoadPlacer.cpp 2.0 KB

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