a_star.h 8.2 KB

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