From 1c437e7528964a1407e92bf4e822c45e6c4df018 Mon Sep 17 00:00:00 2001 From: allison Date: Mon, 21 Oct 2024 21:16:40 -0700 Subject: [PATCH] add method to get last path cost to AStar2D, AStar3D, and AStarGrid2D * A function to return the last total path cost added to AStar2D, AStar3D and AStarGrid2D. --- core/math/a_star.cpp | 14 ++++++++++++++ core/math/a_star.h | 2 ++ core/math/a_star_grid_2d.cpp | 9 +++++++++ core/math/a_star_grid_2d.h | 1 + doc/classes/AStar2D.xml | 6 ++++++ doc/classes/AStar3D.xml | 6 ++++++ doc/classes/AStarGrid2D.xml | 6 ++++++ 7 files changed, 44 insertions(+) diff --git a/core/math/a_star.cpp b/core/math/a_star.cpp index c85201a9739..ad731a6249d 100644 --- a/core/math/a_star.cpp +++ b/core/math/a_star.cpp @@ -549,6 +549,14 @@ bool AStar3D::is_point_disabled(int64_t p_id) const { return !p->enabled; } +real_t AStar3D::get_last_cost() const { + if (last_closest_point == nullptr) { + return -1; + } + + return last_closest_point->g_score; +} + void AStar3D::_bind_methods() { ClassDB::bind_method(D_METHOD("get_available_point_id"), &AStar3D::get_available_point_id); ClassDB::bind_method(D_METHOD("add_point", "id", "position", "weight_scale"), &AStar3D::add_point, DEFVAL(1.0)); @@ -560,6 +568,7 @@ void AStar3D::_bind_methods() { ClassDB::bind_method(D_METHOD("has_point", "id"), &AStar3D::has_point); ClassDB::bind_method(D_METHOD("get_point_connections", "id"), &AStar3D::get_point_connections); ClassDB::bind_method(D_METHOD("get_point_ids"), &AStar3D::get_point_ids); + ClassDB::bind_method(D_METHOD("get_last_cost"), &AStar3D::get_last_cost); ClassDB::bind_method(D_METHOD("set_point_disabled", "id", "disabled"), &AStar3D::set_point_disabled, DEFVAL(true)); ClassDB::bind_method(D_METHOD("is_point_disabled", "id"), &AStar3D::is_point_disabled); @@ -888,6 +897,10 @@ bool AStar2D::_solve(AStar3D::Point *begin_point, AStar3D::Point *end_point, boo return found_route; } +real_t AStar2D::get_last_cost() const { + return astar.get_last_cost(); +} + void AStar2D::_bind_methods() { ClassDB::bind_method(D_METHOD("get_available_point_id"), &AStar2D::get_available_point_id); ClassDB::bind_method(D_METHOD("add_point", "id", "position", "weight_scale"), &AStar2D::add_point, DEFVAL(1.0)); @@ -899,6 +912,7 @@ void AStar2D::_bind_methods() { ClassDB::bind_method(D_METHOD("has_point", "id"), &AStar2D::has_point); ClassDB::bind_method(D_METHOD("get_point_connections", "id"), &AStar2D::get_point_connections); ClassDB::bind_method(D_METHOD("get_point_ids"), &AStar2D::get_point_ids); + ClassDB::bind_method(D_METHOD("get_last_cost"), &AStar2D::get_last_cost); ClassDB::bind_method(D_METHOD("set_point_disabled", "id", "disabled"), &AStar2D::set_point_disabled, DEFVAL(true)); ClassDB::bind_method(D_METHOD("is_point_disabled", "id"), &AStar2D::is_point_disabled); diff --git a/core/math/a_star.h b/core/math/a_star.h index cbaafc10183..267d87c1b76 100644 --- a/core/math/a_star.h +++ b/core/math/a_star.h @@ -144,6 +144,7 @@ public: bool has_point(int64_t p_id) const; Vector get_point_connections(int64_t p_id); PackedInt64Array get_point_ids(); + real_t get_last_cost() const; void set_point_disabled(int64_t p_id, bool p_disabled = true); bool is_point_disabled(int64_t p_id) const; @@ -200,6 +201,7 @@ public: bool has_point(int64_t p_id) const; Vector get_point_connections(int64_t p_id); PackedInt64Array get_point_ids(); + real_t get_last_cost() const; void set_point_disabled(int64_t p_id, bool p_disabled = true); bool is_point_disabled(int64_t p_id) const; diff --git a/core/math/a_star_grid_2d.cpp b/core/math/a_star_grid_2d.cpp index 7e0e982c624..65370b55464 100644 --- a/core/math/a_star_grid_2d.cpp +++ b/core/math/a_star_grid_2d.cpp @@ -734,6 +734,14 @@ TypedArray AStarGrid2D::get_id_path(const Vector2i &p_from_id, const V return path; } +real_t AStarGrid2D::get_last_cost() const { + if (last_closest_point == nullptr) { + return -1; + } + + return last_closest_point->g_score; +} + void AStarGrid2D::_bind_methods() { ClassDB::bind_method(D_METHOD("set_region", "region"), &AStarGrid2D::set_region); ClassDB::bind_method(D_METHOD("get_region"), &AStarGrid2D::get_region); @@ -769,6 +777,7 @@ void AStarGrid2D::_bind_methods() { ClassDB::bind_method(D_METHOD("get_point_data_in_region", "region"), &AStarGrid2D::get_point_data_in_region); ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id", "allow_partial_path"), &AStarGrid2D::get_point_path, DEFVAL(false)); ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id", "allow_partial_path"), &AStarGrid2D::get_id_path, DEFVAL(false)); + ClassDB::bind_method(D_METHOD("get_last_cost"), &AStarGrid2D::get_last_cost); GDVIRTUAL_BIND(_estimate_cost, "from_id", "end_id") GDVIRTUAL_BIND(_compute_cost, "from_id", "to_id") diff --git a/core/math/a_star_grid_2d.h b/core/math/a_star_grid_2d.h index 0536b8109b7..19c84e6b9da 100644 --- a/core/math/a_star_grid_2d.h +++ b/core/math/a_star_grid_2d.h @@ -226,6 +226,7 @@ public: TypedArray get_point_data_in_region(const Rect2i &p_region) const; Vector get_point_path(const Vector2i &p_from, const Vector2i &p_to, bool p_allow_partial_path = false); TypedArray get_id_path(const Vector2i &p_from, const Vector2i &p_to, bool p_allow_partial_path = false); + real_t get_last_cost() const; }; VARIANT_ENUM_CAST(AStarGrid2D::DiagonalMode); diff --git a/doc/classes/AStar2D.xml b/doc/classes/AStar2D.xml index a41da4c3184..0afae9f4131 100644 --- a/doc/classes/AStar2D.xml +++ b/doc/classes/AStar2D.xml @@ -176,6 +176,12 @@ If you change the 2nd point's weight to 3, then the result will be [code][1, 4, 3][/code] instead, because now even though the distance is longer, it's "easier" to get through point 4 than through point 2. + + + + Returns the total cost of the last path found through [method get_id_path] or [method get_point_path]. Returns -1 if there was no last path calculated. + + diff --git a/doc/classes/AStar3D.xml b/doc/classes/AStar3D.xml index 2e8ae37a20b..31705c79fcb 100644 --- a/doc/classes/AStar3D.xml +++ b/doc/classes/AStar3D.xml @@ -204,6 +204,12 @@ If you change the 2nd point's weight to 3, then the result will be [code][1, 4, 3][/code] instead, because now even though the distance is longer, it's "easier" to get through point 4 than through point 2. + + + + Returns the total cost of the last path found through [method get_id_path] or [method get_point_path]. Returns -1 if there was no last path calculated. + + diff --git a/doc/classes/AStarGrid2D.xml b/doc/classes/AStarGrid2D.xml index 8e1972af116..4e8cd490809 100644 --- a/doc/classes/AStarGrid2D.xml +++ b/doc/classes/AStarGrid2D.xml @@ -82,6 +82,12 @@ [b]Note:[/b] When [param allow_partial_path] is [code]true[/code] and [param to_id] is solid the search may take an unusually long time to finish. + + + + Returns the total cost of the last path found through [method get_id_path] or [method get_point_path]. Returns -1 if there was no last path calculated. + +