| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/object/gdvirtual.gen.inc" |
| 34 |
#include "core/object/ref_counted.h" |
| 35 |
#include "core/templates/local_vector.h" |
| 36 |
|
| 37 |
class AStarGrid2D : public RefCounted { |
| 38 |
GDCLASS(AStarGrid2D, RefCounted); |
| 39 |
|
| 40 |
public: |
| 41 |
enum DiagonalMode { |
| 42 |
DIAGONAL_MODE_ALWAYS, |
| 43 |
DIAGONAL_MODE_NEVER, |
| 44 |
DIAGONAL_MODE_AT_LEAST_ONE_WALKABLE, |
| 45 |
DIAGONAL_MODE_ONLY_IF_NO_OBSTACLES, |
| 46 |
DIAGONAL_MODE_MAX, |
| 47 |
}; |
| 48 |
|
| 49 |
enum Heuristic { |
| 50 |
HEURISTIC_EUCLIDEAN, |
| 51 |
HEURISTIC_MANHATTAN, |
| 52 |
HEURISTIC_OCTILE, |
| 53 |
HEURISTIC_CHEBYSHEV, |
| 54 |
HEURISTIC_MAX, |
| 55 |
}; |
| 56 |
|
| 57 |
enum CellShape { |
| 58 |
CELL_SHAPE_SQUARE, |
| 59 |
CELL_SHAPE_ISOMETRIC_RIGHT, |
| 60 |
CELL_SHAPE_ISOMETRIC_DOWN, |
| 61 |
CELL_SHAPE_MAX, |
| 62 |
}; |
| 63 |
|
| 64 |
private: |
| 65 |
Rect2i region; |
| 66 |
Vector2 offset; |
| 67 |
Size2 cell_size = Size2(1, 1); |
| 68 |
bool dirty = false; |
| 69 |
CellShape cell_shape = CELL_SHAPE_SQUARE; |
| 70 |
|
| 71 |
bool jumping_enabled = false; |
| 72 |
DiagonalMode diagonal_mode = DIAGONAL_MODE_ALWAYS; |
| 73 |
Heuristic default_compute_heuristic = HEURISTIC_EUCLIDEAN; |
| 74 |
Heuristic default_estimate_heuristic = HEURISTIC_EUCLIDEAN; |
| 75 |
|
| 76 |
struct Point { |
| 77 |
Vector2i id; |
| 78 |
|
| 79 |
Vector2 pos; |
| 80 |
real_t weight_scale = 1.0; |
| 81 |
|
| 82 |
|
| 83 |
Point *prev_point = nullptr; |
| 84 |
real_t g_score = 0; |
| 85 |
real_t f_score = 0; |
| 86 |
uint64_t open_pass = 0; |
| 87 |
uint64_t closed_pass = 0; |
| 88 |
|
| 89 |
|
| 90 |
real_t abs_g_score = 0; |
| 91 |
real_t abs_f_score = 0; |
| 92 |
|
| 93 |
Point() {} |
| 94 |
|
| 95 |
Point(const Vector2i &p_id, const Vector2 &p_pos) : |
| 96 |
id(p_id), pos(p_pos) {} |
| 97 |
}; |
| 98 |
|
| 99 |
struct SortPoints { |
| 100 |
_FORCE_INLINE_ bool operator()(const Point *A, const Point *B) const { |
| 101 |
if (A->f_score > B->f_score) { |
| 102 |
return true; |
| 103 |
} else if (A->f_score < B->f_score) { |
| 104 |
return false; |
| 105 |
} else { |
| 106 |
return A->g_score < B->g_score; |
| 107 |
} |
| 108 |
} |
| 109 |
}; |
| 110 |
|
| 111 |
LocalVector<bool> solid_mask; |
| 112 |
LocalVector<LocalVector<Point>> points; |
| 113 |
Point *end = nullptr; |
| 114 |
Point *last_closest_point = nullptr; |
| 115 |
|
| 116 |
uint64_t pass = 1; |
| 117 |
|
| 118 |
private: |
| 119 |
_FORCE_INLINE_ size_t _to_mask_index(int32_t p_x, int32_t p_y) const { |
| 120 |
return ((p_y - region.position.y + 1) * (region.size.x + 2)) + p_x - region.position.x + 1; |
| 121 |
} |
| 122 |
|
| 123 |
_FORCE_INLINE_ bool _is_walkable(int32_t p_x, int32_t p_y) const { |
| 124 |
return !solid_mask[_to_mask_index(p_x, p_y)]; |
| 125 |
} |
| 126 |
|
| 127 |
_FORCE_INLINE_ Point *_get_point(int32_t p_x, int32_t p_y) { |
| 128 |
if (region.has_point(Vector2i(p_x, p_y))) { |
| 129 |
return &points[p_y - region.position.y][p_x - region.position.x]; |
| 130 |
} |
| 131 |
return nullptr; |
| 132 |
} |
| 133 |
|
| 134 |
_FORCE_INLINE_ void _set_solid_unchecked(int32_t p_x, int32_t p_y, bool p_solid) { |
| 135 |
solid_mask[_to_mask_index(p_x, p_y)] = p_solid; |
| 136 |
} |
| 137 |
|
| 138 |
_FORCE_INLINE_ void _set_solid_unchecked(const Vector2i &p_id, bool p_solid) { |
| 139 |
solid_mask[_to_mask_index(p_id.x, p_id.y)] = p_solid; |
| 140 |
} |
| 141 |
|
| 142 |
_FORCE_INLINE_ bool _get_solid_unchecked(const Vector2i &p_id) const { |
| 143 |
return solid_mask[_to_mask_index(p_id.x, p_id.y)]; |
| 144 |
} |
| 145 |
|
| 146 |
_FORCE_INLINE_ Point *_get_point_unchecked(int32_t p_x, int32_t p_y) { |
| 147 |
return &points[p_y - region.position.y][p_x - region.position.x]; |
| 148 |
} |
| 149 |
|
| 150 |
_FORCE_INLINE_ Point *_get_point_unchecked(const Vector2i &p_id) { |
| 151 |
return &points[p_id.y - region.position.y][p_id.x - region.position.x]; |
| 152 |
} |
| 153 |
|
| 154 |
_FORCE_INLINE_ const Point *_get_point_unchecked(const Vector2i &p_id) const { |
| 155 |
return &points[p_id.y - region.position.y][p_id.x - region.position.x]; |
| 156 |
} |
| 157 |
|
| 158 |
void _get_nbors(Point *p_point, LocalVector<Point *> &r_nbors); |
| 159 |
Point *_jump(Point *p_from, Point *p_to); |
| 160 |
bool _solve(Point *p_begin_point, Point *p_end_point, bool p_allow_partial_path); |
| 161 |
Point *_forced_successor(int32_t p_x, int32_t p_y, int32_t p_dx, int32_t p_dy, bool p_inclusive = false); |
| 162 |
|
| 163 |
protected: |
| 164 |
static void _bind_methods(); |
| 165 |
|
| 166 |
virtual real_t _estimate_cost(const Vector2i &p_from_id, const Vector2i &p_end_id); |
| 167 |
virtual real_t _compute_cost(const Vector2i &p_from_id, const Vector2i &p_to_id); |
| 168 |
|
| 169 |
GDVIRTUAL2RC(real_t, _estimate_cost, Vector2i, Vector2i) |
| 170 |
GDVIRTUAL2RC(real_t, _compute_cost, Vector2i, Vector2i) |
| 171 |
|
| 172 |
#ifndef DISABLE_DEPRECATED |
| 173 |
TypedArray<Vector2i> _get_id_path_bind_compat_88047(const Vector2i &p_from, const Vector2i &p_to); |
| 174 |
Vector<Vector2> _get_point_path_bind_compat_88047(const Vector2i &p_from, const Vector2i &p_to); |
| 175 |
static void _bind_compatibility_methods(); |
| 176 |
#endif |
| 177 |
|
| 178 |
public: |
| 179 |
void set_region(const Rect2i &p_region); |
| 180 |
Rect2i get_region() const; |
| 181 |
|
| 182 |
void set_size(const Size2i &p_size); |
| 183 |
Size2i get_size() const; |
| 184 |
|
| 185 |
void set_offset(const Vector2 &p_offset); |
| 186 |
Vector2 get_offset() const; |
| 187 |
|
| 188 |
void set_cell_size(const Size2 &p_cell_size); |
| 189 |
Size2 get_cell_size() const; |
| 190 |
|
| 191 |
void set_cell_shape(CellShape p_cell_shape); |
| 192 |
CellShape get_cell_shape() const; |
| 193 |
|
| 194 |
void update(); |
| 195 |
|
| 196 |
bool is_in_bounds(int32_t p_x, int32_t p_y) const; |
| 197 |
bool is_in_boundsv(const Vector2i &p_id) const; |
| 198 |
bool is_dirty() const; |
| 199 |
|
| 200 |
void set_jumping_enabled(bool p_enabled); |
| 201 |
bool is_jumping_enabled() const; |
| 202 |
|
| 203 |
void set_diagonal_mode(DiagonalMode p_diagonal_mode); |
| 204 |
DiagonalMode get_diagonal_mode() const; |
| 205 |
|
| 206 |
void set_default_compute_heuristic(Heuristic p_heuristic); |
| 207 |
Heuristic get_default_compute_heuristic() const; |
| 208 |
|
| 209 |
void set_default_estimate_heuristic(Heuristic p_heuristic); |
| 210 |
Heuristic get_default_estimate_heuristic() const; |
| 211 |
|
| 212 |
void set_point_solid(const Vector2i &p_id, bool p_solid = true); |
| 213 |
bool is_point_solid(const Vector2i &p_id) const; |
| 214 |
|
| 215 |
void set_point_weight_scale(const Vector2i &p_id, real_t p_weight_scale); |
| 216 |
real_t get_point_weight_scale(const Vector2i &p_id) const; |
| 217 |
|
| 218 |
void fill_solid_region(const Rect2i &p_region, bool p_solid = true); |
| 219 |
void fill_weight_scale_region(const Rect2i &p_region, real_t p_weight_scale); |
| 220 |
|
| 221 |
void clear(); |
| 222 |
|
| 223 |
Vector2 get_point_position(const Vector2i &p_id) const; |
| 224 |
TypedArray<Dictionary> get_point_data_in_region(const Rect2i &p_region) const; |
| 225 |
Vector<Vector2> get_point_path(const Vector2i &p_from, const Vector2i &p_to, bool p_allow_partial_path = false); |
| 226 |
TypedArray<Vector2i> get_id_path(const Vector2i &p_from, const Vector2i &p_to, bool p_allow_partial_path = false); |
| 227 |
}; |
| 228 |
|
| 229 |
VARIANT_ENUM_CAST(AStarGrid2D::DiagonalMode); |
| 230 |
VARIANT_ENUM_CAST(AStarGrid2D::Heuristic); |
| 231 |
VARIANT_ENUM_CAST(AStarGrid2D::CellShape) |