RmgPath.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * RmgPath.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 "RmgPath.h"
  12. #include <boost/heap/priority_queue.hpp> //A*
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. using namespace rmg;
  15. const std::function<float(const int3 &, const int3 &)> Path::DEFAULT_MOVEMENT_FUNCTION =
  16. [](const int3 & src, const int3 & dst)
  17. {
  18. return 1.f;
  19. };
  20. //A* priority queue
  21. typedef std::pair<int3, float> TDistance;
  22. struct NodeComparer
  23. {
  24. bool operator()(const TDistance & lhs, const TDistance & rhs) const
  25. {
  26. return (rhs.second < lhs.second);
  27. }
  28. };
  29. boost::heap::priority_queue<TDistance, boost::heap::compare<NodeComparer>> createPriorityQueue()
  30. {
  31. return boost::heap::priority_queue<TDistance, boost::heap::compare<NodeComparer>>();
  32. }
  33. Path::Path(const Area & area): dArea(&area)
  34. {
  35. }
  36. Path::Path(const Area & area, const int3 & src): dArea(&area)
  37. {
  38. dPath.add(src);
  39. }
  40. Path::Path(const Path & path): dArea(path.dArea), dPath(path.dPath)
  41. {
  42. }
  43. Path & Path::operator= (const Path & path)
  44. {
  45. //do not modify area
  46. dPath = path.dPath;
  47. return *this;
  48. }
  49. bool Path::valid() const
  50. {
  51. return !dPath.empty();
  52. }
  53. Path Path::invalid()
  54. {
  55. return Path({});
  56. }
  57. Path Path::search(const Tileset & dst, bool straight, std::function<float(const int3 &, const int3 &)> moveCostFunction) const
  58. {
  59. //A* algorithm taken from Wiki http://en.wikipedia.org/wiki/A*_search_algorithm
  60. if(!dArea)
  61. return Path::invalid();
  62. auto resultArea = *dArea + dst;
  63. Path result(resultArea);
  64. if(dst.empty())
  65. return result;
  66. int3 src = rmg::Area(dst).nearest(dPath);
  67. result.connect(src);
  68. Tileset closed; // The set of nodes already evaluated.
  69. auto open = createPriorityQueue(); // The set of tentative nodes to be evaluated, initially containing the start node
  70. std::map<int3, int3> cameFrom; // The map of navigated nodes.
  71. std::map<int3, float> distances;
  72. cameFrom[src] = int3(-1, -1, -1); //first node points to finish condition
  73. distances[src] = 0;
  74. open.push(std::make_pair(src, 0.f));
  75. // Cost from start along best known path.
  76. while(!open.empty())
  77. {
  78. auto node = open.top();
  79. open.pop();
  80. int3 currentNode = node.first;
  81. closed.insert(currentNode);
  82. if(dPath.contains(currentNode)) //we reached connection, stop
  83. {
  84. // Trace the path using the saved parent information and return path
  85. int3 backTracking = currentNode;
  86. while (cameFrom[backTracking].valid())
  87. {
  88. result.dPath.add(backTracking);
  89. backTracking = cameFrom[backTracking];
  90. }
  91. return result;
  92. }
  93. else
  94. {
  95. auto computeTileScore = [&open, &closed, &cameFrom, &currentNode, &distances, &moveCostFunction, &result](const int3& pos) -> void
  96. {
  97. if(closed.count(pos))
  98. return;
  99. if(!result.dArea->contains(pos))
  100. return;
  101. float movementCost = moveCostFunction(currentNode, pos) + currentNode.dist2d(pos);
  102. float distance = distances[currentNode] + movementCost; //we prefer to use already free paths
  103. int bestDistanceSoFar = std::numeric_limits<int>::max();
  104. auto it = distances.find(pos);
  105. if(it != distances.end())
  106. bestDistanceSoFar = static_cast<int>(it->second);
  107. if(distance < bestDistanceSoFar)
  108. {
  109. cameFrom[pos] = currentNode;
  110. open.push(std::make_pair(pos, distance));
  111. distances[pos] = distance;
  112. }
  113. };
  114. auto dirs = int3::getDirs();
  115. std::vector<int3> neighbors(dirs.begin(), dirs.end());
  116. if(straight)
  117. neighbors.assign(rmg::dirs4.begin(), rmg::dirs4.end());
  118. for(auto & i : neighbors)
  119. {
  120. computeTileScore(currentNode + i);
  121. }
  122. }
  123. }
  124. result.dPath.clear();
  125. return result;
  126. }
  127. Path Path::search(const int3 & dst, bool straight, std::function<float(const int3 &, const int3 &)> moveCostFunction) const
  128. {
  129. return search(Tileset{dst}, straight, moveCostFunction);
  130. }
  131. Path Path::search(const Area & dst, bool straight, std::function<float(const int3 &, const int3 &)> moveCostFunction) const
  132. {
  133. return search(dst.getTiles(), straight, moveCostFunction);
  134. }
  135. Path Path::search(const Path & dst, bool straight, std::function<float(const int3 &, const int3 &)> moveCostFunction) const
  136. {
  137. assert(dst.dArea == dArea);
  138. return search(dst.dPath, straight, moveCostFunction);
  139. }
  140. void Path::connect(const int3 & path)
  141. {
  142. dPath.add(path);
  143. }
  144. void Path::connect(const Tileset & path)
  145. {
  146. Area a(path);
  147. dPath.unite(a);
  148. }
  149. void Path::connect(const Area & path)
  150. {
  151. dPath.unite(path);
  152. }
  153. void Path::connect(const Path & path)
  154. {
  155. dPath.unite(path.dPath);
  156. }
  157. const Area & Path::getPathArea() const
  158. {
  159. return dPath;
  160. }
  161. VCMI_LIB_NAMESPACE_END