diff --git a/core/math/a_star.cpp b/core/math/a_star.cpp index 6c3b84d49a..e1388ad2ac 100644 --- a/core/math/a_star.cpp +++ b/core/math/a_star.cpp @@ -55,6 +55,7 @@ void AStar::add_point(int p_id, const Vector3 &p_pos, real_t p_weight_scale) { pt->weight_scale = p_weight_scale; pt->prev_point = NULL; pt->last_pass = 0; + pt->enabled = true; points[p_id] = pt; } else { points[p_id]->pos = p_pos; @@ -242,6 +243,9 @@ bool AStar::_solve(Point *begin_point, Point *end_point) { pass++; + if (!end_point->enabled) + return false; + SelfList::List open_list; bool found_route = false; @@ -249,6 +253,10 @@ bool AStar::_solve(Point *begin_point, Point *end_point) { for (Set::Element *E = begin_point->neighbours.front(); E; E = E->next()) { Point *n = E->get(); + + if (!n->enabled) + continue; + n->prev_point = begin_point; n->distance = _compute_cost(begin_point->id, n->id) * n->weight_scale; n->last_pass = pass; @@ -290,6 +298,9 @@ bool AStar::_solve(Point *begin_point, Point *end_point) { Point *e = E->get(); + if (!e->enabled) + continue; + real_t distance = _compute_cost(p->id, e->id) * e->weight_scale + p->distance; if (e->last_pass == pass) { @@ -438,6 +449,14 @@ PoolVector AStar::get_id_path(int p_from_id, int p_to_id) { return path; } +void AStar::set_point_disabled(int p_id, bool p_disabled) { + points[p_id]->enabled = !p_disabled; +} + +bool AStar::is_point_disabled(int p_id) const { + return !points[p_id]->enabled; +} + void AStar::_bind_methods() { ClassDB::bind_method(D_METHOD("get_available_point_id"), &AStar::get_available_point_id); @@ -450,6 +469,9 @@ void AStar::_bind_methods() { ClassDB::bind_method(D_METHOD("has_point", "id"), &AStar::has_point); ClassDB::bind_method(D_METHOD("get_points"), &AStar::get_points); + ClassDB::bind_method(D_METHOD("set_point_disabled", "id", "disabled"), &AStar::set_point_disabled, DEFVAL(true)); + ClassDB::bind_method(D_METHOD("is_point_disabled", "id"), &AStar::is_point_disabled); + ClassDB::bind_method(D_METHOD("get_point_connections", "id"), &AStar::get_point_connections); ClassDB::bind_method(D_METHOD("connect_points", "id", "to_id", "bidirectional"), &AStar::connect_points, DEFVAL(true)); diff --git a/core/math/a_star.h b/core/math/a_star.h index d094bc4863..c63e1aa4dc 100644 --- a/core/math/a_star.h +++ b/core/math/a_star.h @@ -54,6 +54,7 @@ class AStar : public Reference { Vector3 pos; real_t weight_scale; uint64_t last_pass; + bool enabled; Set neighbours; @@ -114,6 +115,9 @@ public: PoolVector get_point_connections(int p_id); Array get_points(); + void set_point_disabled(int p_id, bool p_disabled = true); + bool is_point_disabled(int p_id) const; + void connect_points(int p_id, int p_with_id, bool bidirectional = true); void disconnect_points(int p_id, int p_with_id); bool are_points_connected(int p_id, int p_with_id) const; diff --git a/doc/classes/AStar.xml b/doc/classes/AStar.xml index 50beac9074..4fdb7af4ed 100644 --- a/doc/classes/AStar.xml +++ b/doc/classes/AStar.xml @@ -224,6 +224,15 @@ Returns whether a point associated with the given id exists. + + + + + + + Returns whether a point is disabled or not for pathfinding. By default, all points are enabled. + + @@ -233,6 +242,17 @@ Removes the point associated with the given id from the points pool. + + + + + + + + + Disables or enables the specified point for pathfinding. Useful for making a temporary obstacle. + +