a_star_grid_2d.h 8.1 KB

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