a_star.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**************************************************************************/
  2. /* a_star.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef A_STAR_H
  31. #define A_STAR_H
  32. #include "core/oa_hash_map.h"
  33. #include "core/reference.h"
  34. /**
  35. A* pathfinding algorithm
  36. @author Juan Linietsky <reduzio@gmail.com>
  37. */
  38. class AStar : public Reference {
  39. GDCLASS(AStar, Reference);
  40. friend class AStar2D;
  41. struct Point {
  42. Point() :
  43. neighbours(4u),
  44. unlinked_neighbours(4u) {}
  45. int id;
  46. Vector3 pos;
  47. real_t weight_scale;
  48. bool enabled;
  49. OAHashMap<int, Point *> neighbours;
  50. OAHashMap<int, Point *> unlinked_neighbours;
  51. // Used for pathfinding.
  52. Point *prev_point;
  53. real_t g_score;
  54. real_t f_score;
  55. uint64_t open_pass;
  56. uint64_t closed_pass;
  57. };
  58. struct SortPoints {
  59. _FORCE_INLINE_ bool operator()(const Point *A, const Point *B) const { // Returns true when the Point A is worse than Point B.
  60. if (A->f_score > B->f_score) {
  61. return true;
  62. } else if (A->f_score < B->f_score) {
  63. return false;
  64. } else {
  65. return A->g_score < B->g_score; // If the f_costs are the same then prioritize the points that are further away from the start.
  66. }
  67. }
  68. };
  69. struct Segment {
  70. union {
  71. struct {
  72. int32_t u;
  73. int32_t v;
  74. };
  75. uint64_t key;
  76. };
  77. enum {
  78. NONE = 0,
  79. FORWARD = 1,
  80. BACKWARD = 2,
  81. BIDIRECTIONAL = FORWARD | BACKWARD
  82. };
  83. unsigned char direction;
  84. bool operator<(const Segment &p_s) const { return key < p_s.key; }
  85. Segment() {
  86. key = 0;
  87. direction = NONE;
  88. }
  89. Segment(int p_from, int p_to) {
  90. if (p_from < p_to) {
  91. u = p_from;
  92. v = p_to;
  93. direction = FORWARD;
  94. } else {
  95. u = p_to;
  96. v = p_from;
  97. direction = BACKWARD;
  98. }
  99. }
  100. };
  101. int last_free_id;
  102. uint64_t pass;
  103. OAHashMap<int, Point *> points;
  104. Set<Segment> segments;
  105. bool _solve(Point *begin_point, Point *end_point);
  106. protected:
  107. static void _bind_methods();
  108. virtual real_t _estimate_cost(int p_from_id, int p_to_id);
  109. virtual real_t _compute_cost(int p_from_id, int p_to_id);
  110. public:
  111. int get_available_point_id() const;
  112. void add_point(int p_id, const Vector3 &p_pos, real_t p_weight_scale = 1);
  113. Vector3 get_point_position(int p_id) const;
  114. void set_point_position(int p_id, const Vector3 &p_pos);
  115. real_t get_point_weight_scale(int p_id) const;
  116. void set_point_weight_scale(int p_id, real_t p_weight_scale);
  117. void remove_point(int p_id);
  118. bool has_point(int p_id) const;
  119. PoolVector<int> get_point_connections(int p_id);
  120. Array get_points();
  121. void set_point_disabled(int p_id, bool p_disabled = true);
  122. bool is_point_disabled(int p_id) const;
  123. void connect_points(int p_id, int p_with_id, bool bidirectional = true);
  124. void disconnect_points(int p_id, int p_with_id, bool bidirectional = true);
  125. bool are_points_connected(int p_id, int p_with_id, bool bidirectional = true) const;
  126. int get_point_count() const;
  127. int get_point_capacity() const;
  128. void reserve_space(int p_num_nodes);
  129. void clear();
  130. int get_closest_point(const Vector3 &p_point, bool p_include_disabled = false) const;
  131. Vector3 get_closest_position_in_segment(const Vector3 &p_point) const;
  132. PoolVector<Vector3> get_point_path(int p_from_id, int p_to_id);
  133. PoolVector<int> get_id_path(int p_from_id, int p_to_id);
  134. AStar();
  135. ~AStar();
  136. };
  137. class AStar2D : public Reference {
  138. GDCLASS(AStar2D, Reference);
  139. AStar astar;
  140. bool _solve(AStar::Point *begin_point, AStar::Point *end_point);
  141. protected:
  142. static void _bind_methods();
  143. virtual real_t _estimate_cost(int p_from_id, int p_to_id);
  144. virtual real_t _compute_cost(int p_from_id, int p_to_id);
  145. public:
  146. int get_available_point_id() const;
  147. void add_point(int p_id, const Vector2 &p_pos, real_t p_weight_scale = 1);
  148. Vector2 get_point_position(int p_id) const;
  149. void set_point_position(int p_id, const Vector2 &p_pos);
  150. real_t get_point_weight_scale(int p_id) const;
  151. void set_point_weight_scale(int p_id, real_t p_weight_scale);
  152. void remove_point(int p_id);
  153. bool has_point(int p_id) const;
  154. PoolVector<int> get_point_connections(int p_id);
  155. Array get_points();
  156. void set_point_disabled(int p_id, bool p_disabled = true);
  157. bool is_point_disabled(int p_id) const;
  158. void connect_points(int p_id, int p_with_id, bool p_bidirectional = true);
  159. void disconnect_points(int p_id, int p_with_id, bool p_bidirectional = true);
  160. bool are_points_connected(int p_id, int p_with_id, bool p_bidirectional = true) const;
  161. int get_point_count() const;
  162. int get_point_capacity() const;
  163. void reserve_space(int p_num_nodes);
  164. void clear();
  165. int get_closest_point(const Vector2 &p_point, bool p_include_disabled = false) const;
  166. Vector2 get_closest_position_in_segment(const Vector2 &p_point) const;
  167. PoolVector<Vector2> get_point_path(int p_from_id, int p_to_id);
  168. PoolVector<int> get_id_path(int p_from_id, int p_to_id);
  169. AStar2D();
  170. ~AStar2D();
  171. };
  172. #endif // A_STAR_H