Skip to content

Commit

Permalink
Add a way to filter neighbor points to AStar2D/3D
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaosus committed Jan 22, 2025
1 parent a7146ef commit 2c64233
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
16 changes: 16 additions & 0 deletions core/math/a_star.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,13 @@ bool AStar3D::_solve(Point *begin_point, Point *end_point, bool p_allow_partial_
continue;
}

{
bool filtered;
if (GDVIRTUAL_CALL(_filter_neighbor, p->id, e->id, filtered) && filtered) {
continue;
}
}

real_t tentative_g_score = p->g_score + _compute_cost(p->id, e->id) * e->weight_scale;

bool new_point = false;
Expand Down Expand Up @@ -578,6 +585,7 @@ void AStar3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id", "allow_partial_path"), &AStar3D::get_point_path, DEFVAL(false));
ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id", "allow_partial_path"), &AStar3D::get_id_path, DEFVAL(false));

GDVIRTUAL_BIND(_filter_neighbor, "from_id", "neighbor_id")
GDVIRTUAL_BIND(_estimate_cost, "from_id", "end_id")
GDVIRTUAL_BIND(_compute_cost, "from_id", "to_id")
}
Expand Down Expand Up @@ -858,6 +866,13 @@ bool AStar2D::_solve(AStar3D::Point *begin_point, AStar3D::Point *end_point, boo
continue;
}

{
bool filtered;
if (GDVIRTUAL_CALL(_filter_neighbor, p->id, e->id, filtered) && filtered) {
continue;
}
}

real_t tentative_g_score = p->g_score + _compute_cost(p->id, e->id) * e->weight_scale;

bool new_point = false;
Expand Down Expand Up @@ -917,6 +932,7 @@ void AStar2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id", "allow_partial_path"), &AStar2D::get_point_path, DEFVAL(false));
ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id", "allow_partial_path"), &AStar2D::get_id_path, DEFVAL(false));

GDVIRTUAL_BIND(_filter_neighbor, "from_id", "neighbor_id")
GDVIRTUAL_BIND(_estimate_cost, "from_id", "end_id")
GDVIRTUAL_BIND(_compute_cost, "from_id", "to_id")
}
2 changes: 2 additions & 0 deletions core/math/a_star.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class AStar3D : public RefCounted {
virtual real_t _estimate_cost(int64_t p_from_id, int64_t p_end_id);
virtual real_t _compute_cost(int64_t p_from_id, int64_t p_to_id);

GDVIRTUAL2RC(bool, _filter_neighbor, int64_t, int64_t)
GDVIRTUAL2RC(real_t, _estimate_cost, int64_t, int64_t)
GDVIRTUAL2RC(real_t, _compute_cost, int64_t, int64_t)

Expand Down Expand Up @@ -179,6 +180,7 @@ class AStar2D : public RefCounted {
virtual real_t _estimate_cost(int64_t p_from_id, int64_t p_end_id);
virtual real_t _compute_cost(int64_t p_from_id, int64_t p_to_id);

GDVIRTUAL2RC(bool, _filter_neighbor, int64_t, int64_t)
GDVIRTUAL2RC(real_t, _estimate_cost, int64_t, int64_t)
GDVIRTUAL2RC(real_t, _compute_cost, int64_t, int64_t)

Expand Down
9 changes: 9 additions & 0 deletions doc/classes/AStar2D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@
Note that this function is hidden in the default [AStar2D] class.
</description>
</method>
<method name="_filter_neighbor" qualifiers="virtual const">
<return type="bool" />
<param index="0" name="from_id" type="int" />
<param index="1" name="neighbor_id" type="int" />
<description>
Called when neighboring point comes in to the processing. If returning a [code]true[/code] the point will not being processed.
Note that this function is hidden in the default [AStar2D] class.
</description>
</method>
<method name="add_point">
<return type="void" />
<param index="0" name="id" type="int" />
Expand Down
9 changes: 9 additions & 0 deletions doc/classes/AStar3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@
Note that this function is hidden in the default [AStar3D] class.
</description>
</method>
<method name="_filter_neighbor" qualifiers="virtual const">
<return type="bool" />
<param index="0" name="from_id" type="int" />
<param index="1" name="neighbor_id" type="int" />
<description>
Called when neighboring point comes in to the processing. If returning a [code]true[/code] the point will not being processed.
Note that this function is hidden in the default [AStar3D] class.
</description>
</method>
<method name="add_point">
<return type="void" />
<param index="0" name="id" type="int" />
Expand Down

0 comments on commit 2c64233

Please sign in to comment.