Added functions to AStar for disable/enable points
(cherry picked from commit cc71fb2308
)
This commit is contained in:
parent
bb8f015595
commit
9535a6079e
|
@ -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->weight_scale = p_weight_scale;
|
||||||
pt->prev_point = NULL;
|
pt->prev_point = NULL;
|
||||||
pt->last_pass = 0;
|
pt->last_pass = 0;
|
||||||
|
pt->enabled = true;
|
||||||
points[p_id] = pt;
|
points[p_id] = pt;
|
||||||
} else {
|
} else {
|
||||||
points[p_id]->pos = p_pos;
|
points[p_id]->pos = p_pos;
|
||||||
|
@ -242,6 +243,9 @@ bool AStar::_solve(Point *begin_point, Point *end_point) {
|
||||||
|
|
||||||
pass++;
|
pass++;
|
||||||
|
|
||||||
|
if (!end_point->enabled)
|
||||||
|
return false;
|
||||||
|
|
||||||
SelfList<Point>::List open_list;
|
SelfList<Point>::List open_list;
|
||||||
|
|
||||||
bool found_route = false;
|
bool found_route = false;
|
||||||
|
@ -249,6 +253,10 @@ bool AStar::_solve(Point *begin_point, Point *end_point) {
|
||||||
for (Set<Point *>::Element *E = begin_point->neighbours.front(); E; E = E->next()) {
|
for (Set<Point *>::Element *E = begin_point->neighbours.front(); E; E = E->next()) {
|
||||||
|
|
||||||
Point *n = E->get();
|
Point *n = E->get();
|
||||||
|
|
||||||
|
if (!n->enabled)
|
||||||
|
continue;
|
||||||
|
|
||||||
n->prev_point = begin_point;
|
n->prev_point = begin_point;
|
||||||
n->distance = _compute_cost(begin_point->id, n->id) * n->weight_scale;
|
n->distance = _compute_cost(begin_point->id, n->id) * n->weight_scale;
|
||||||
n->last_pass = pass;
|
n->last_pass = pass;
|
||||||
|
@ -290,6 +298,9 @@ bool AStar::_solve(Point *begin_point, Point *end_point) {
|
||||||
|
|
||||||
Point *e = E->get();
|
Point *e = E->get();
|
||||||
|
|
||||||
|
if (!e->enabled)
|
||||||
|
continue;
|
||||||
|
|
||||||
real_t distance = _compute_cost(p->id, e->id) * e->weight_scale + p->distance;
|
real_t distance = _compute_cost(p->id, e->id) * e->weight_scale + p->distance;
|
||||||
|
|
||||||
if (e->last_pass == pass) {
|
if (e->last_pass == pass) {
|
||||||
|
@ -438,6 +449,14 @@ PoolVector<int> AStar::get_id_path(int p_from_id, int p_to_id) {
|
||||||
return path;
|
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() {
|
void AStar::_bind_methods() {
|
||||||
|
|
||||||
ClassDB::bind_method(D_METHOD("get_available_point_id"), &AStar::get_available_point_id);
|
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("has_point", "id"), &AStar::has_point);
|
||||||
ClassDB::bind_method(D_METHOD("get_points"), &AStar::get_points);
|
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("get_point_connections", "id"), &AStar::get_point_connections);
|
||||||
|
|
||||||
ClassDB::bind_method(D_METHOD("connect_points", "id", "to_id", "bidirectional"), &AStar::connect_points, DEFVAL(true));
|
ClassDB::bind_method(D_METHOD("connect_points", "id", "to_id", "bidirectional"), &AStar::connect_points, DEFVAL(true));
|
||||||
|
|
|
@ -54,6 +54,7 @@ class AStar : public Reference {
|
||||||
Vector3 pos;
|
Vector3 pos;
|
||||||
real_t weight_scale;
|
real_t weight_scale;
|
||||||
uint64_t last_pass;
|
uint64_t last_pass;
|
||||||
|
bool enabled;
|
||||||
|
|
||||||
Set<Point *> neighbours;
|
Set<Point *> neighbours;
|
||||||
|
|
||||||
|
@ -114,6 +115,9 @@ public:
|
||||||
PoolVector<int> get_point_connections(int p_id);
|
PoolVector<int> get_point_connections(int p_id);
|
||||||
Array get_points();
|
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 connect_points(int p_id, int p_with_id, bool bidirectional = true);
|
||||||
void disconnect_points(int p_id, int p_with_id);
|
void disconnect_points(int p_id, int p_with_id);
|
||||||
bool are_points_connected(int p_id, int p_with_id) const;
|
bool are_points_connected(int p_id, int p_with_id) const;
|
||||||
|
|
|
@ -224,6 +224,15 @@
|
||||||
Returns whether a point associated with the given id exists.
|
Returns whether a point associated with the given id exists.
|
||||||
</description>
|
</description>
|
||||||
</method>
|
</method>
|
||||||
|
<method name="is_point_disabled" qualifiers="const">
|
||||||
|
<return type="bool">
|
||||||
|
</return>
|
||||||
|
<argument index="0" name="id" type="int">
|
||||||
|
</argument>
|
||||||
|
<description>
|
||||||
|
Returns whether a point is disabled or not for pathfinding. By default, all points are enabled.
|
||||||
|
</description>
|
||||||
|
</method>
|
||||||
<method name="remove_point">
|
<method name="remove_point">
|
||||||
<return type="void">
|
<return type="void">
|
||||||
</return>
|
</return>
|
||||||
|
@ -233,6 +242,17 @@
|
||||||
Removes the point associated with the given id from the points pool.
|
Removes the point associated with the given id from the points pool.
|
||||||
</description>
|
</description>
|
||||||
</method>
|
</method>
|
||||||
|
<method name="set_point_disabled">
|
||||||
|
<return type="void">
|
||||||
|
</return>
|
||||||
|
<argument index="0" name="id" type="int">
|
||||||
|
</argument>
|
||||||
|
<argument index="1" name="disabled" type="bool" default="true">
|
||||||
|
</argument>
|
||||||
|
<description>
|
||||||
|
Disables or enables the specified point for pathfinding. Useful for making a temporary obstacle.
|
||||||
|
</description>
|
||||||
|
</method>
|
||||||
<method name="set_point_position">
|
<method name="set_point_position">
|
||||||
<return type="void">
|
<return type="void">
|
||||||
</return>
|
</return>
|
||||||
|
|
Loading…
Reference in New Issue