2
0

a_star.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*************************************************************************/
  2. /* a_star.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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/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. int id = 0;
  45. Vector3 pos;
  46. real_t weight_scale = 0;
  47. bool enabled = false;
  48. OAHashMap<int, Point *> neighbours = 4u;
  49. OAHashMap<int, 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. union {
  70. struct {
  71. int32_t u;
  72. int32_t v;
  73. };
  74. uint64_t key = 0;
  75. };
  76. enum {
  77. NONE = 0,
  78. FORWARD = 1,
  79. BACKWARD = 2,
  80. BIDIRECTIONAL = FORWARD | BACKWARD
  81. };
  82. unsigned char direction = NONE;
  83. bool operator<(const Segment &p_s) const { return key < p_s.key; }
  84. Segment() {}
  85. Segment(int p_from, int p_to) {
  86. if (p_from < p_to) {
  87. u = p_from;
  88. v = p_to;
  89. direction = FORWARD;
  90. } else {
  91. u = p_to;
  92. v = p_from;
  93. direction = BACKWARD;
  94. }
  95. }
  96. };
  97. int last_free_id = 0;
  98. uint64_t pass = 1;
  99. OAHashMap<int, Point *> points;
  100. RBSet<Segment> segments;
  101. bool _solve(Point *begin_point, Point *end_point);
  102. protected:
  103. static void _bind_methods();
  104. virtual real_t _estimate_cost(int p_from_id, int p_to_id);
  105. virtual real_t _compute_cost(int p_from_id, int p_to_id);
  106. GDVIRTUAL2RC(real_t, _estimate_cost, int64_t, int64_t)
  107. GDVIRTUAL2RC(real_t, _compute_cost, int64_t, int64_t)
  108. public:
  109. int get_available_point_id() const;
  110. void add_point(int p_id, const Vector3 &p_pos, real_t p_weight_scale = 1);
  111. Vector3 get_point_position(int p_id) const;
  112. void set_point_position(int p_id, const Vector3 &p_pos);
  113. real_t get_point_weight_scale(int p_id) const;
  114. void set_point_weight_scale(int p_id, real_t p_weight_scale);
  115. void remove_point(int p_id);
  116. bool has_point(int p_id) const;
  117. Vector<int> get_point_connections(int p_id);
  118. Array get_point_ids();
  119. void set_point_disabled(int p_id, bool p_disabled = true);
  120. bool is_point_disabled(int p_id) const;
  121. void connect_points(int p_id, int p_with_id, bool bidirectional = true);
  122. void disconnect_points(int p_id, int p_with_id, bool bidirectional = true);
  123. bool are_points_connected(int p_id, int p_with_id, bool bidirectional = true) const;
  124. int get_point_count() const;
  125. int get_point_capacity() const;
  126. void reserve_space(int p_num_nodes);
  127. void clear();
  128. int get_closest_point(const Vector3 &p_point, bool p_include_disabled = false) const;
  129. Vector3 get_closest_position_in_segment(const Vector3 &p_point) const;
  130. Vector<Vector3> get_point_path(int p_from_id, int p_to_id);
  131. Vector<int> get_id_path(int p_from_id, int p_to_id);
  132. AStar3D() {}
  133. ~AStar3D();
  134. };
  135. class AStar2D : public RefCounted {
  136. GDCLASS(AStar2D, RefCounted);
  137. AStar3D astar;
  138. bool _solve(AStar3D::Point *begin_point, AStar3D::Point *end_point);
  139. protected:
  140. static void _bind_methods();
  141. virtual real_t _estimate_cost(int p_from_id, int p_to_id);
  142. virtual real_t _compute_cost(int p_from_id, int p_to_id);
  143. GDVIRTUAL2RC(real_t, _estimate_cost, int64_t, int64_t)
  144. GDVIRTUAL2RC(real_t, _compute_cost, int64_t, int64_t)
  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. Vector<int> get_point_connections(int p_id);
  155. Array get_point_ids();
  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);
  160. bool are_points_connected(int p_id, int p_with_id) 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. Vector<Vector2> get_point_path(int p_from_id, int p_to_id);
  168. Vector<int> get_id_path(int p_from_id, int p_to_id);
  169. AStar2D() {}
  170. ~AStar2D() {}
  171. };
  172. #endif // A_STAR_H