Skip to content

Commit 2a04b18

Browse files
authored
Merge pull request godotengine#70339 from Chaosus/astargrid2d_weight_scale
Restore weight scale for `AStarGrid2D` (partially)
2 parents f318d60 + 8c478dc commit 2a04b18

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

core/math/a_star_grid_2d.cpp

+20-1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,19 @@ bool AStarGrid2D::is_point_solid(const Vector2i &p_id) const {
155155
return points[p_id.y][p_id.x].solid;
156156
}
157157

158+
void AStarGrid2D::set_point_weight_scale(const Vector2i &p_id, real_t p_weight_scale) {
159+
ERR_FAIL_COND_MSG(dirty, "Grid is not initialized. Call the update method.");
160+
ERR_FAIL_COND_MSG(!is_in_boundsv(p_id), vformat("Can't set point's weight scale. Point out of bounds (%s/%s, %s/%s).", p_id.x, size.width, p_id.y, size.height));
161+
ERR_FAIL_COND_MSG(p_weight_scale < 0.0, vformat("Can't set point's weight scale less than 0.0: %f.", p_weight_scale));
162+
points[p_id.y][p_id.x].weight_scale = p_weight_scale;
163+
}
164+
165+
real_t AStarGrid2D::get_point_weight_scale(const Vector2i &p_id) const {
166+
ERR_FAIL_COND_V_MSG(dirty, 0, "Grid is not initialized. Call the update method.");
167+
ERR_FAIL_COND_V_MSG(!is_in_boundsv(p_id), 0, vformat("Can't get point's weight scale. Point out of bounds (%s/%s, %s/%s).", p_id.x, size.width, p_id.y, size.height));
168+
return points[p_id.y][p_id.x].weight_scale;
169+
}
170+
158171
AStarGrid2D::Point *AStarGrid2D::_jump(Point *p_from, Point *p_to) {
159172
if (!p_to || p_to->solid) {
160173
return nullptr;
@@ -388,7 +401,10 @@ bool AStarGrid2D::_solve(Point *p_begin_point, Point *p_end_point) {
388401
_get_nbors(p, nbors);
389402
for (List<Point *>::Element *E = nbors.front(); E; E = E->next()) {
390403
Point *e = E->get(); // The neighbour point.
404+
real_t weight_scale = 1.0;
405+
391406
if (jumping_enabled) {
407+
// TODO: Make it works with weight_scale.
392408
e = _jump(p, e);
393409
if (!e || e->closed_pass == pass) {
394410
continue;
@@ -397,9 +413,10 @@ bool AStarGrid2D::_solve(Point *p_begin_point, Point *p_end_point) {
397413
if (e->solid || e->closed_pass == pass) {
398414
continue;
399415
}
416+
weight_scale = e->weight_scale;
400417
}
401418

402-
real_t tentative_g_score = p->g_score + _compute_cost(p->id, e->id);
419+
real_t tentative_g_score = p->g_score + _compute_cost(p->id, e->id) * weight_scale;
403420
bool new_point = false;
404421

405422
if (e->open_pass != pass) { // The point wasn't inside the open list.
@@ -559,6 +576,8 @@ void AStarGrid2D::_bind_methods() {
559576
ClassDB::bind_method(D_METHOD("get_default_heuristic"), &AStarGrid2D::get_default_heuristic);
560577
ClassDB::bind_method(D_METHOD("set_point_solid", "id", "solid"), &AStarGrid2D::set_point_solid, DEFVAL(true));
561578
ClassDB::bind_method(D_METHOD("is_point_solid", "id"), &AStarGrid2D::is_point_solid);
579+
ClassDB::bind_method(D_METHOD("set_point_weight_scale", "id", "weight_scale"), &AStarGrid2D::set_point_weight_scale);
580+
ClassDB::bind_method(D_METHOD("get_point_weight_scale", "id"), &AStarGrid2D::get_point_weight_scale);
562581
ClassDB::bind_method(D_METHOD("clear"), &AStarGrid2D::clear);
563582

564583
ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id"), &AStarGrid2D::get_point_path);

core/math/a_star_grid_2d.h

+4
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class AStarGrid2D : public RefCounted {
7272

7373
bool solid = false;
7474
Vector2 pos;
75+
real_t weight_scale = 1.0;
7576

7677
// Used for pathfinding.
7778
Point *prev_point = nullptr;
@@ -166,6 +167,9 @@ class AStarGrid2D : public RefCounted {
166167
void set_point_solid(const Vector2i &p_id, bool p_solid = true);
167168
bool is_point_solid(const Vector2i &p_id) const;
168169

170+
void set_point_weight_scale(const Vector2i &p_id, real_t p_weight_scale);
171+
real_t get_point_weight_scale(const Vector2i &p_id) const;
172+
169173
void clear();
170174

171175
Vector<Vector2> get_point_path(const Vector2i &p_from, const Vector2i &p_to);

doc/classes/AStarGrid2D.xml

+17
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@
6969
[b]Note:[/b] This method is not thread-safe. If called from a [Thread], it will return an empty [PackedVector3Array] and will print an error message.
7070
</description>
7171
</method>
72+
<method name="get_point_weight_scale" qualifiers="const">
73+
<return type="float" />
74+
<param index="0" name="id" type="Vector2i" />
75+
<description>
76+
Returns the weight scale of the point associated with the given [param id].
77+
</description>
78+
</method>
7279
<method name="is_dirty" qualifiers="const">
7380
<return type="bool" />
7481
<description>
@@ -106,6 +113,15 @@
106113
[b]Note:[/b] Calling [method update] is not needed after the call of this function.
107114
</description>
108115
</method>
116+
<method name="set_point_weight_scale">
117+
<return type="void" />
118+
<param index="0" name="id" type="Vector2i" />
119+
<param index="1" name="weight_scale" type="float" />
120+
<description>
121+
Sets the [param weight_scale] for the point with the given [param id]. The [param weight_scale] is multiplied by the result of [method _compute_cost] when determining the overall cost of traveling across a segment from a neighboring point to this point.
122+
[b]Note:[/b] Calling [method update] is not needed after the call of this function.
123+
</description>
124+
</method>
109125
<method name="update">
110126
<return type="void" />
111127
<description>
@@ -125,6 +141,7 @@
125141
</member>
126142
<member name="jumping_enabled" type="bool" setter="set_jumping_enabled" getter="is_jumping_enabled" default="false">
127143
Enables or disables jumping to skip up the intermediate points and speeds up the searching algorithm.
144+
[b]Note:[/b] Currently, toggling it on disables the consideration of weight scaling in pathfinding.
128145
</member>
129146
<member name="offset" type="Vector2" setter="set_offset" getter="get_offset" default="Vector2(0, 0)">
130147
The offset of the grid which will be applied to calculate the resulting point position returned by [method get_point_path]. If changed, [method update] needs to be called before finding the next path.

0 commit comments

Comments
 (0)