a_star.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*************************************************************************/
  2. /* a_star.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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 <[email protected]>
  37. */
  38. class AStar : public Reference {
  39. GDCLASS(AStar, Reference);
  40. friend class AStar2D;
  41. struct Point {
  42. Point() {}
  43. int id;
  44. Vector3 pos;
  45. real_t weight_scale;
  46. bool enabled;
  47. OAHashMap<int, Point *> neighbours = 4u;
  48. OAHashMap<int, Point *> unlinked_neighbours = 4u;
  49. // Used for pathfinding.
  50. Point *prev_point;
  51. real_t g_score;
  52. real_t f_score;
  53. uint64_t open_pass;
  54. uint64_t closed_pass;
  55. };
  56. struct SortPoints {
  57. _FORCE_INLINE_ bool operator()(const Point *A, const Point *B) const { // Returns true when the Point A is worse than Point B.
  58. if (A->f_score > B->f_score) {
  59. return true;
  60. } else if (A->f_score < B->f_score) {
  61. return false;
  62. } else {
  63. 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.
  64. }
  65. }
  66. };
  67. struct Segment {
  68. union {
  69. struct {
  70. int32_t u;
  71. int32_t v;
  72. };
  73. uint64_t key = 0;
  74. };
  75. enum {
  76. NONE = 0,
  77. FORWARD = 1,
  78. BACKWARD = 2,
  79. BIDIRECTIONAL = FORWARD | BACKWARD
  80. };
  81. unsigned char direction = NONE;
  82. bool operator<(const Segment &p_s) const { return key < p_s.key; }
  83. Segment() {}
  84. Segment(int p_from, int p_to) {
  85. if (p_from < p_to) {
  86. u = p_from;
  87. v = p_to;
  88. direction = FORWARD;
  89. } else {
  90. u = p_to;
  91. v = p_from;
  92. direction = BACKWARD;
  93. }
  94. }
  95. };
  96. int last_free_id = 0;
  97. uint64_t pass = 1;
  98. OAHashMap<int, Point *> points;
  99. Set<Segment> segments;
  100. bool _solve(Point *begin_point, Point *end_point);
  101. protected:
  102. static void _bind_methods();
  103. virtual real_t _estimate_cost(int p_from_id, int p_to_id);
  104. virtual real_t _compute_cost(int p_from_id, int p_to_id);
  105. public:
  106. int get_available_point_id() const;
  107. void add_point(int p_id, const Vector3 &p_pos, real_t p_weight_scale = 1);
  108. Vector3 get_point_position(int p_id) const;
  109. void set_point_position(int p_id, const Vector3 &p_pos);
  110. real_t get_point_weight_scale(int p_id) const;
  111. void set_point_weight_scale(int p_id, real_t p_weight_scale);
  112. void remove_point(int p_id);
  113. bool has_point(int p_id) const;
  114. Vector<int> get_point_connections(int p_id);
  115. Array get_points();
  116. void set_point_disabled(int p_id, bool p_disabled = true);
  117. bool is_point_disabled(int p_id) const;
  118. void connect_points(int p_id, int p_with_id, bool bidirectional = true);
  119. void disconnect_points(int p_id, int p_with_id, bool bidirectional = true);
  120. bool are_points_connected(int p_id, int p_with_id, bool bidirectional = true) const;
  121. int get_point_count() const;
  122. int get_point_capacity() const;
  123. void reserve_space(int p_num_nodes);
  124. void clear();
  125. int get_closest_point(const Vector3 &p_point, bool p_include_disabled = false) const;
  126. Vector3 get_closest_position_in_segment(const Vector3 &p_point) const;
  127. Vector<Vector3> get_point_path(int p_from_id, int p_to_id);
  128. Vector<int> get_id_path(int p_from_id, int p_to_id);
  129. AStar() {}
  130. ~AStar();
  131. };
  132. class AStar2D : public Reference {
  133. GDCLASS(AStar2D, Reference);
  134. AStar astar;
  135. bool _solve(AStar::Point *begin_point, AStar::Point *end_point);
  136. protected:
  137. static void _bind_methods();
  138. virtual real_t _estimate_cost(int p_from_id, int p_to_id);
  139. virtual real_t _compute_cost(int p_from_id, int p_to_id);
  140. public:
  141. int get_available_point_id() const;
  142. void add_point(int p_id, const Vector2 &p_pos, real_t p_weight_scale = 1);
  143. Vector2 get_point_position(int p_id) const;
  144. void set_point_position(int p_id, const Vector2 &p_pos);
  145. real_t get_point_weight_scale(int p_id) const;
  146. void set_point_weight_scale(int p_id, real_t p_weight_scale);
  147. void remove_point(int p_id);
  148. bool has_point(int p_id) const;
  149. Vector<int> get_point_connections(int p_id);
  150. Array get_points();
  151. void set_point_disabled(int p_id, bool p_disabled = true);
  152. bool is_point_disabled(int p_id) const;
  153. void connect_points(int p_id, int p_with_id, bool p_bidirectional = true);
  154. void disconnect_points(int p_id, int p_with_id);
  155. bool are_points_connected(int p_id, int p_with_id) const;
  156. int get_point_count() const;
  157. int get_point_capacity() const;
  158. void reserve_space(int p_num_nodes);
  159. void clear();
  160. int get_closest_point(const Vector2 &p_point, bool p_include_disabled = false) const;
  161. Vector2 get_closest_position_in_segment(const Vector2 &p_point) const;
  162. Vector<Vector2> get_point_path(int p_from_id, int p_to_id);
  163. Vector<int> get_id_path(int p_from_id, int p_to_id);
  164. AStar2D() {}
  165. ~AStar2D() {}
  166. };
  167. #endif // A_STAR_H