| 1 | /**************************************************************************/ | 
|---|
| 2 | /*  a_star.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 |  | 
|---|
| 31 | #ifndef A_STAR_H | 
|---|
| 32 | #define A_STAR_H | 
|---|
| 33 |  | 
|---|
| 34 | #include "core/object/gdvirtual.gen.inc" | 
|---|
| 35 | #include "core/object/ref_counted.h" | 
|---|
| 36 | #include "core/templates/oa_hash_map.h" | 
|---|
| 37 |  | 
|---|
| 38 | /** | 
|---|
| 39 | A* pathfinding algorithm. | 
|---|
| 40 | */ | 
|---|
| 41 |  | 
|---|
| 42 | class AStar3D : public RefCounted { | 
|---|
| 43 | GDCLASS(AStar3D, RefCounted); | 
|---|
| 44 | friend class AStar2D; | 
|---|
| 45 |  | 
|---|
| 46 | struct Point { | 
|---|
| 47 | Point() {} | 
|---|
| 48 |  | 
|---|
| 49 | int64_t id = 0; | 
|---|
| 50 | Vector3 pos; | 
|---|
| 51 | real_t weight_scale = 0; | 
|---|
| 52 | bool enabled = false; | 
|---|
| 53 |  | 
|---|
| 54 | OAHashMap<int64_t, Point *> neighbors = 4u; | 
|---|
| 55 | OAHashMap<int64_t, Point *> unlinked_neighbours = 4u; | 
|---|
| 56 |  | 
|---|
| 57 | // Used for pathfinding. | 
|---|
| 58 | Point *prev_point = nullptr; | 
|---|
| 59 | real_t g_score = 0; | 
|---|
| 60 | real_t f_score = 0; | 
|---|
| 61 | uint64_t open_pass = 0; | 
|---|
| 62 | uint64_t closed_pass = 0; | 
|---|
| 63 | }; | 
|---|
| 64 |  | 
|---|
| 65 | struct SortPoints { | 
|---|
| 66 | _FORCE_INLINE_ bool operator()(const Point *A, const Point *B) const { // Returns true when the Point A is worse than Point B. | 
|---|
| 67 | if (A->f_score > B->f_score) { | 
|---|
| 68 | return true; | 
|---|
| 69 | } else if (A->f_score < B->f_score) { | 
|---|
| 70 | return false; | 
|---|
| 71 | } else { | 
|---|
| 72 | 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. | 
|---|
| 73 | } | 
|---|
| 74 | } | 
|---|
| 75 | }; | 
|---|
| 76 |  | 
|---|
| 77 | struct Segment { | 
|---|
| 78 | Pair<int64_t, int64_t> key; | 
|---|
| 79 |  | 
|---|
| 80 | enum { | 
|---|
| 81 | NONE = 0, | 
|---|
| 82 | FORWARD = 1, | 
|---|
| 83 | BACKWARD = 2, | 
|---|
| 84 | BIDIRECTIONAL = FORWARD | BACKWARD | 
|---|
| 85 | }; | 
|---|
| 86 | unsigned char direction = NONE; | 
|---|
| 87 |  | 
|---|
| 88 | static uint32_t hash(const Segment &p_seg) { | 
|---|
| 89 | return PairHash<int64_t, int64_t>().hash(p_seg.key); | 
|---|
| 90 | } | 
|---|
| 91 | bool operator==(const Segment &p_s) const { return key == p_s.key; } | 
|---|
| 92 |  | 
|---|
| 93 | Segment() {} | 
|---|
| 94 | Segment(int64_t p_from, int64_t p_to) { | 
|---|
| 95 | if (p_from < p_to) { | 
|---|
| 96 | key.first = p_from; | 
|---|
| 97 | key.second = p_to; | 
|---|
| 98 | direction = FORWARD; | 
|---|
| 99 | } else { | 
|---|
| 100 | key.first = p_to; | 
|---|
| 101 | key.second = p_from; | 
|---|
| 102 | direction = BACKWARD; | 
|---|
| 103 | } | 
|---|
| 104 | } | 
|---|
| 105 | }; | 
|---|
| 106 |  | 
|---|
| 107 | int64_t last_free_id = 0; | 
|---|
| 108 | uint64_t pass = 1; | 
|---|
| 109 |  | 
|---|
| 110 | OAHashMap<int64_t, Point *> points; | 
|---|
| 111 | HashSet<Segment, Segment> segments; | 
|---|
| 112 |  | 
|---|
| 113 | bool _solve(Point *begin_point, Point *end_point); | 
|---|
| 114 |  | 
|---|
| 115 | protected: | 
|---|
| 116 | static void _bind_methods(); | 
|---|
| 117 |  | 
|---|
| 118 | virtual real_t _estimate_cost(int64_t p_from_id, int64_t p_to_id); | 
|---|
| 119 | virtual real_t _compute_cost(int64_t p_from_id, int64_t p_to_id); | 
|---|
| 120 |  | 
|---|
| 121 | GDVIRTUAL2RC(real_t, _estimate_cost, int64_t, int64_t) | 
|---|
| 122 | GDVIRTUAL2RC(real_t, _compute_cost, int64_t, int64_t) | 
|---|
| 123 |  | 
|---|
| 124 | public: | 
|---|
| 125 | int64_t get_available_point_id() const; | 
|---|
| 126 |  | 
|---|
| 127 | void add_point(int64_t p_id, const Vector3 &p_pos, real_t p_weight_scale = 1); | 
|---|
| 128 | Vector3 get_point_position(int64_t p_id) const; | 
|---|
| 129 | void set_point_position(int64_t p_id, const Vector3 &p_pos); | 
|---|
| 130 | real_t get_point_weight_scale(int64_t p_id) const; | 
|---|
| 131 | void set_point_weight_scale(int64_t p_id, real_t p_weight_scale); | 
|---|
| 132 | void remove_point(int64_t p_id); | 
|---|
| 133 | bool has_point(int64_t p_id) const; | 
|---|
| 134 | Vector<int64_t> get_point_connections(int64_t p_id); | 
|---|
| 135 | PackedInt64Array get_point_ids(); | 
|---|
| 136 |  | 
|---|
| 137 | void set_point_disabled(int64_t p_id, bool p_disabled = true); | 
|---|
| 138 | bool is_point_disabled(int64_t p_id) const; | 
|---|
| 139 |  | 
|---|
| 140 | void connect_points(int64_t p_id, int64_t p_with_id, bool bidirectional = true); | 
|---|
| 141 | void disconnect_points(int64_t p_id, int64_t p_with_id, bool bidirectional = true); | 
|---|
| 142 | bool are_points_connected(int64_t p_id, int64_t p_with_id, bool bidirectional = true) const; | 
|---|
| 143 |  | 
|---|
| 144 | int64_t get_point_count() const; | 
|---|
| 145 | int64_t get_point_capacity() const; | 
|---|
| 146 | void reserve_space(int64_t p_num_nodes); | 
|---|
| 147 | void clear(); | 
|---|
| 148 |  | 
|---|
| 149 | int64_t get_closest_point(const Vector3 &p_point, bool p_include_disabled = false) const; | 
|---|
| 150 | Vector3 get_closest_position_in_segment(const Vector3 &p_point) const; | 
|---|
| 151 |  | 
|---|
| 152 | Vector<Vector3> get_point_path(int64_t p_from_id, int64_t p_to_id); | 
|---|
| 153 | Vector<int64_t> get_id_path(int64_t p_from_id, int64_t p_to_id); | 
|---|
| 154 |  | 
|---|
| 155 | AStar3D() {} | 
|---|
| 156 | ~AStar3D(); | 
|---|
| 157 | }; | 
|---|
| 158 |  | 
|---|
| 159 | class AStar2D : public RefCounted { | 
|---|
| 160 | GDCLASS(AStar2D, RefCounted); | 
|---|
| 161 | AStar3D astar; | 
|---|
| 162 |  | 
|---|
| 163 | bool _solve(AStar3D::Point *begin_point, AStar3D::Point *end_point); | 
|---|
| 164 |  | 
|---|
| 165 | protected: | 
|---|
| 166 | static void _bind_methods(); | 
|---|
| 167 |  | 
|---|
| 168 | virtual real_t _estimate_cost(int64_t p_from_id, int64_t p_to_id); | 
|---|
| 169 | virtual real_t _compute_cost(int64_t p_from_id, int64_t p_to_id); | 
|---|
| 170 |  | 
|---|
| 171 | GDVIRTUAL2RC(real_t, _estimate_cost, int64_t, int64_t) | 
|---|
| 172 | GDVIRTUAL2RC(real_t, _compute_cost, int64_t, int64_t) | 
|---|
| 173 |  | 
|---|
| 174 | public: | 
|---|
| 175 | int64_t get_available_point_id() const; | 
|---|
| 176 |  | 
|---|
| 177 | void add_point(int64_t p_id, const Vector2 &p_pos, real_t p_weight_scale = 1); | 
|---|
| 178 | Vector2 get_point_position(int64_t p_id) const; | 
|---|
| 179 | void set_point_position(int64_t p_id, const Vector2 &p_pos); | 
|---|
| 180 | real_t get_point_weight_scale(int64_t p_id) const; | 
|---|
| 181 | void set_point_weight_scale(int64_t p_id, real_t p_weight_scale); | 
|---|
| 182 | void remove_point(int64_t p_id); | 
|---|
| 183 | bool has_point(int64_t p_id) const; | 
|---|
| 184 | Vector<int64_t> get_point_connections(int64_t p_id); | 
|---|
| 185 | PackedInt64Array get_point_ids(); | 
|---|
| 186 |  | 
|---|
| 187 | void set_point_disabled(int64_t p_id, bool p_disabled = true); | 
|---|
| 188 | bool is_point_disabled(int64_t p_id) const; | 
|---|
| 189 |  | 
|---|
| 190 | void connect_points(int64_t p_id, int64_t p_with_id, bool p_bidirectional = true); | 
|---|
| 191 | void disconnect_points(int64_t p_id, int64_t p_with_id, bool p_bidirectional = true); | 
|---|
| 192 | bool are_points_connected(int64_t p_id, int64_t p_with_id, bool p_bidirectional = true) const; | 
|---|
| 193 |  | 
|---|
| 194 | int64_t get_point_count() const; | 
|---|
| 195 | int64_t get_point_capacity() const; | 
|---|
| 196 | void reserve_space(int64_t p_num_nodes); | 
|---|
| 197 | void clear(); | 
|---|
| 198 |  | 
|---|
| 199 | int64_t get_closest_point(const Vector2 &p_point, bool p_include_disabled = false) const; | 
|---|
| 200 | Vector2 get_closest_position_in_segment(const Vector2 &p_point) const; | 
|---|
| 201 |  | 
|---|
| 202 | Vector<Vector2> get_point_path(int64_t p_from_id, int64_t p_to_id); | 
|---|
| 203 | Vector<int64_t> get_id_path(int64_t p_from_id, int64_t p_to_id); | 
|---|
| 204 |  | 
|---|
| 205 | AStar2D() {} | 
|---|
| 206 | ~AStar2D() {} | 
|---|
| 207 | }; | 
|---|
| 208 |  | 
|---|
| 209 | #endif // A_STAR_H | 
|---|
| 210 |  | 
|---|