RmgPath.cpp 4.5 KB

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