a_star_grid_2d.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**************************************************************************/
  2. /* a_star_grid_2d.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_GRID_2D_H
  31. #define A_STAR_GRID_2D_H
  32. #include "core/object/gdvirtual.gen.inc"
  33. #include "core/object/ref_counted.h"
  34. #include "core/templates/list.h"
  35. #include "core/templates/local_vector.h"
  36. class AStarGrid2D : public RefCounted {
  37. GDCLASS(AStarGrid2D, RefCounted);
  38. public:
  39. enum DiagonalMode {
  40. DIAGONAL_MODE_ALWAYS,
  41. DIAGONAL_MODE_NEVER,
  42. DIAGONAL_MODE_AT_LEAST_ONE_WALKABLE,
  43. DIAGONAL_MODE_ONLY_IF_NO_OBSTACLES,
  44. DIAGONAL_MODE_MAX,
  45. };
  46. enum Heuristic {
  47. HEURISTIC_EUCLIDEAN,
  48. HEURISTIC_MANHATTAN,
  49. HEURISTIC_OCTILE,
  50. HEURISTIC_CHEBYSHEV,
  51. HEURISTIC_MAX,
  52. };
  53. enum CellShape {
  54. CELL_SHAPE_SQUARE,
  55. CELL_SHAPE_ISOMETRIC_RIGHT,
  56. CELL_SHAPE_ISOMETRIC_DOWN,
  57. CELL_SHAPE_MAX,
  58. };
  59. private:
  60. Rect2i region;
  61. Vector2 offset;
  62. Size2 cell_size = Size2(1, 1);
  63. bool dirty = false;
  64. CellShape cell_shape = CELL_SHAPE_SQUARE;
  65. bool jumping_enabled = false;
  66. DiagonalMode diagonal_mode = DIAGONAL_MODE_ALWAYS;
  67. Heuristic default_compute_heuristic = HEURISTIC_EUCLIDEAN;
  68. Heuristic default_estimate_heuristic = HEURISTIC_EUCLIDEAN;
  69. struct Point {
  70. Vector2i id;
  71. bool solid = false;
  72. Vector2 pos;
  73. real_t weight_scale = 1.0;
  74. // Used for pathfinding.
  75. Point *prev_point = nullptr;
  76. real_t g_score = 0;
  77. real_t f_score = 0;
  78. uint64_t open_pass = 0;
  79. uint64_t closed_pass = 0;
  80. // Used for getting last_closest_point.
  81. real_t abs_g_score = 0;
  82. real_t abs_f_score = 0;
  83. Point() {}
  84. Point(const Vector2i &p_id, const Vector2 &p_pos) :
  85. id(p_id), pos(p_pos) {}
  86. };
  87. struct SortPoints {
  88. _FORCE_INLINE_ bool operator()(const Point *A, const Point *B) const { // Returns true when the Point A is worse than Point B.
  89. if (A->f_score > B->f_score) {
  90. return true;
  91. } else if (A->f_score < B->f_score) {
  92. return false;
  93. } else {
  94. 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.
  95. }
  96. }
  97. };
  98. LocalVector<LocalVector<Point>> points;
  99. Point *end = nullptr;
  100. Point *last_closest_point = nullptr;
  101. uint64_t pass = 1;
  102. private: // Internal routines.
  103. _FORCE_INLINE_ bool _is_walkable(int32_t p_x, int32_t p_y) const {
  104. if (region.has_point(Vector2i(p_x, p_y))) {
  105. return !points[p_y - region.position.y][p_x - region.position.x].solid;
  106. }
  107. return false;
  108. }
  109. _FORCE_INLINE_ Point *_get_point(int32_t p_x, int32_t p_y) {
  110. if (region.has_point(Vector2i(p_x, p_y))) {
  111. return &points[p_y - region.position.y][p_x - region.position.x];
  112. }
  113. return nullptr;
  114. }
  115. _FORCE_INLINE_ Point *_get_point_unchecked(int32_t p_x, int32_t p_y) {
  116. return &points[p_y - region.position.y][p_x - region.position.x];
  117. }
  118. _FORCE_INLINE_ Point *_get_point_unchecked(const Vector2i &p_id) {
  119. return &points[p_id.y - region.position.y][p_id.x - region.position.x];
  120. }
  121. _FORCE_INLINE_ const Point *_get_point_unchecked(const Vector2i &p_id) const {
  122. return &points[p_id.y - region.position.y][p_id.x - region.position.x];
  123. }
  124. void _get_nbors(Point *p_point, LocalVector<Point *> &r_nbors);
  125. Point *_jump(Point *p_from, Point *p_to);
  126. bool _solve(Point *p_begin_point, Point *p_end_point);
  127. protected:
  128. static void _bind_methods();
  129. virtual real_t _estimate_cost(const Vector2i &p_from_id, const Vector2i &p_to_id);
  130. virtual real_t _compute_cost(const Vector2i &p_from_id, const Vector2i &p_to_id);
  131. GDVIRTUAL2RC(real_t, _estimate_cost, Vector2i, Vector2i)
  132. GDVIRTUAL2RC(real_t, _compute_cost, Vector2i, Vector2i)
  133. #ifndef DISABLE_DEPRECATED
  134. TypedArray<Vector2i> _get_id_path_bind_compat_88047(const Vector2i &p_from, const Vector2i &p_to);
  135. Vector<Vector2> _get_point_path_bind_compat_88047(const Vector2i &p_from, const Vector2i &p_to);
  136. static void _bind_compatibility_methods();
  137. #endif
  138. public:
  139. void set_region(const Rect2i &p_region);
  140. Rect2i get_region() const;
  141. void set_size(const Size2i &p_size);
  142. Size2i get_size() const;
  143. void set_offset(const Vector2 &p_offset);
  144. Vector2 get_offset() const;
  145. void set_cell_size(const Size2 &p_cell_size);
  146. Size2 get_cell_size() const;
  147. void set_cell_shape(CellShape p_cell_shape);
  148. CellShape get_cell_shape() const;
  149. void update();
  150. bool is_in_bounds(int32_t p_x, int32_t p_y) const;
  151. bool is_in_boundsv(const Vector2i &p_id) const;
  152. bool is_dirty() const;
  153. void set_jumping_enabled(bool p_enabled);
  154. bool is_jumping_enabled() const;
  155. void set_diagonal_mode(DiagonalMode p_diagonal_mode);
  156. DiagonalMode get_diagonal_mode() const;
  157. void set_default_compute_heuristic(Heuristic p_heuristic);
  158. Heuristic get_default_compute_heuristic() const;
  159. void set_default_estimate_heuristic(Heuristic p_heuristic);
  160. Heuristic get_default_estimate_heuristic() const;
  161. void set_point_solid(const Vector2i &p_id, bool p_solid = true);
  162. bool is_point_solid(const Vector2i &p_id) const;
  163. void set_point_weight_scale(const Vector2i &p_id, real_t p_weight_scale);
  164. real_t get_point_weight_scale(const Vector2i &p_id) const;
  165. void fill_solid_region(const Rect2i &p_region, bool p_solid = true);
  166. void fill_weight_scale_region(const Rect2i &p_region, real_t p_weight_scale);
  167. void clear();
  168. Vector2 get_point_position(const Vector2i &p_id) const;
  169. Vector<Vector2> get_point_path(const Vector2i &p_from, const Vector2i &p_to, bool p_allow_partial_path = false);
  170. TypedArray<Vector2i> get_id_path(const Vector2i &p_from, const Vector2i &p_to, bool p_allow_partial_path = false);
  171. };
  172. VARIANT_ENUM_CAST(AStarGrid2D::DiagonalMode);
  173. VARIANT_ENUM_CAST(AStarGrid2D::Heuristic);
  174. VARIANT_ENUM_CAST(AStarGrid2D::CellShape)
  175. #endif // A_STAR_GRID_2D_H