a_star.h 7.0 KB

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