a_star_grid_2d.h 6.4 KB

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