a_star.h 7.4 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) 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/object/gdvirtual.gen.inc"
  33. #include "core/object/ref_counted.h"
  34. #include "core/templates/oa_hash_map.h"
  35. /**
  36. A* pathfinding algorithm.
  37. */
  38. class AStar3D : public RefCounted {
  39. GDCLASS(AStar3D, RefCounted);
  40. friend class AStar2D;
  41. struct Point {
  42. Point() {}
  43. int64_t id = 0;
  44. Vector3 pos;
  45. real_t weight_scale = 0;
  46. bool enabled = false;
  47. OAHashMap<int64_t, Point *> neighbors = 4u;
  48. OAHashMap<int64_t, Point *> unlinked_neighbours = 4u;
  49. // Used for pathfinding.
  50. Point *prev_point = nullptr;
  51. real_t g_score = 0;
  52. real_t f_score = 0;
  53. uint64_t open_pass = 0;
  54. uint64_t closed_pass = 0;
  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. Pair<int64_t, int64_t> key;
  69. enum {
  70. NONE = 0,
  71. FORWARD = 1,
  72. BACKWARD = 2,
  73. BIDIRECTIONAL = FORWARD | BACKWARD
  74. };
  75. unsigned char direction = NONE;
  76. static uint32_t hash(const Segment &p_seg) {
  77. return PairHash<int64_t, int64_t>().hash(p_seg.key);
  78. }
  79. bool operator==(const Segment &p_s) const { return key == p_s.key; }
  80. Segment() {}
  81. Segment(int64_t p_from, int64_t p_to) {
  82. if (p_from < p_to) {
  83. key.first = p_from;
  84. key.second = p_to;
  85. direction = FORWARD;
  86. } else {
  87. key.first = p_to;
  88. key.second = p_from;
  89. direction = BACKWARD;
  90. }
  91. }
  92. };
  93. int64_t last_free_id = 0;
  94. uint64_t pass = 1;
  95. OAHashMap<int64_t, Point *> points;
  96. HashSet<Segment, Segment> segments;
  97. bool _solve(Point *begin_point, Point *end_point);
  98. protected:
  99. static void _bind_methods();
  100. virtual real_t _estimate_cost(int64_t p_from_id, int64_t p_to_id);
  101. virtual real_t _compute_cost(int64_t p_from_id, int64_t p_to_id);
  102. GDVIRTUAL2RC(real_t, _estimate_cost, int64_t, int64_t)
  103. GDVIRTUAL2RC(real_t, _compute_cost, int64_t, int64_t)
  104. public:
  105. int64_t get_available_point_id() const;
  106. void add_point(int64_t p_id, const Vector3 &p_pos, real_t p_weight_scale = 1);
  107. Vector3 get_point_position(int64_t p_id) const;
  108. void set_point_position(int64_t p_id, const Vector3 &p_pos);
  109. real_t get_point_weight_scale(int64_t p_id) const;
  110. void set_point_weight_scale(int64_t p_id, real_t p_weight_scale);
  111. void remove_point(int64_t p_id);
  112. bool has_point(int64_t p_id) const;
  113. Vector<int64_t> get_point_connections(int64_t p_id);
  114. PackedInt64Array get_point_ids();
  115. void set_point_disabled(int64_t p_id, bool p_disabled = true);
  116. bool is_point_disabled(int64_t p_id) const;
  117. void connect_points(int64_t p_id, int64_t p_with_id, bool bidirectional = true);
  118. void disconnect_points(int64_t p_id, int64_t p_with_id, bool bidirectional = true);
  119. bool are_points_connected(int64_t p_id, int64_t p_with_id, bool bidirectional = true) const;
  120. int64_t get_point_count() const;
  121. int64_t get_point_capacity() const;
  122. void reserve_space(int64_t p_num_nodes);
  123. void clear();
  124. int64_t get_closest_point(const Vector3 &p_point, bool p_include_disabled = false) const;
  125. Vector3 get_closest_position_in_segment(const Vector3 &p_point) const;
  126. Vector<Vector3> get_point_path(int64_t p_from_id, int64_t p_to_id);
  127. Vector<int64_t> get_id_path(int64_t p_from_id, int64_t p_to_id);
  128. AStar3D() {}
  129. ~AStar3D();
  130. };
  131. class AStar2D : public RefCounted {
  132. GDCLASS(AStar2D, RefCounted);
  133. AStar3D astar;
  134. bool _solve(AStar3D::Point *begin_point, AStar3D::Point *end_point);
  135. protected:
  136. static void _bind_methods();
  137. virtual real_t _estimate_cost(int64_t p_from_id, int64_t p_to_id);
  138. virtual real_t _compute_cost(int64_t p_from_id, int64_t p_to_id);
  139. GDVIRTUAL2RC(real_t, _estimate_cost, int64_t, int64_t)
  140. GDVIRTUAL2RC(real_t, _compute_cost, int64_t, int64_t)
  141. public:
  142. int64_t get_available_point_id() const;
  143. void add_point(int64_t p_id, const Vector2 &p_pos, real_t p_weight_scale = 1);
  144. Vector2 get_point_position(int64_t p_id) const;
  145. void set_point_position(int64_t p_id, const Vector2 &p_pos);
  146. real_t get_point_weight_scale(int64_t p_id) const;
  147. void set_point_weight_scale(int64_t p_id, real_t p_weight_scale);
  148. void remove_point(int64_t p_id);
  149. bool has_point(int64_t p_id) const;
  150. Vector<int64_t> get_point_connections(int64_t p_id);
  151. PackedInt64Array get_point_ids();
  152. void set_point_disabled(int64_t p_id, bool p_disabled = true);
  153. bool is_point_disabled(int64_t p_id) const;
  154. void connect_points(int64_t p_id, int64_t p_with_id, bool p_bidirectional = true);
  155. void disconnect_points(int64_t p_id, int64_t p_with_id, bool p_bidirectional = true);
  156. bool are_points_connected(int64_t p_id, int64_t p_with_id, bool p_bidirectional = true) const;
  157. int64_t get_point_count() const;
  158. int64_t get_point_capacity() const;
  159. void reserve_space(int64_t p_num_nodes);
  160. void clear();
  161. int64_t get_closest_point(const Vector2 &p_point, bool p_include_disabled = false) const;
  162. Vector2 get_closest_position_in_segment(const Vector2 &p_point) const;
  163. Vector<Vector2> get_point_path(int64_t p_from_id, int64_t p_to_id);
  164. Vector<int64_t> get_id_path(int64_t p_from_id, int64_t p_to_id);
  165. AStar2D() {}
  166. ~AStar2D() {}
  167. };
  168. #endif // A_STAR_H