a_star.h 7.4 KB

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