summaryrefslogtreecommitdiff
path: root/core/math
diff options
context:
space:
mode:
authorRobin Hübner <profan@prfn.se>2019-08-25 21:30:52 +0200
committerRobin Hübner <profan@prfn.se>2019-08-27 00:38:35 +0200
commit1031833fb04784908b7a28579af055f7264a2ce1 (patch)
treea25902f0987810b66d47c4510140d7675f789bb1 /core/math
parent6d6d4371467a94c01418e9d475e994fe61b7b4d0 (diff)
allow to reserve space in OAHashMap explicitly and also in AStar.
* also handle overflow occurring in _get_probe_length
Diffstat (limited to 'core/math')
-rw-r--r--core/math/a_star.cpp32
-rw-r--r--core/math/a_star.h10
2 files changed, 42 insertions, 0 deletions
diff --git a/core/math/a_star.cpp b/core/math/a_star.cpp
index aea42a1edf..60b7326c29 100644
--- a/core/math/a_star.cpp
+++ b/core/math/a_star.cpp
@@ -243,6 +243,20 @@ void AStar::clear() {
points.clear();
}
+int AStar::get_point_count() const {
+ return points.get_num_elements();
+}
+
+int AStar::get_point_capacity() const {
+ return points.get_capacity();
+}
+
+void AStar::reserve_space(int p_num_nodes) {
+ ERR_FAIL_COND_MSG(p_num_nodes <= 0, "New capacity must be greater than 0, was: " + itos(p_num_nodes) + ".");
+ ERR_FAIL_COND_MSG((uint32_t)p_num_nodes < points.get_capacity(), "New capacity must be greater than current capacity: " + itos(points.get_capacity()) + ", new was: " + itos(p_num_nodes) + ".");
+ points.reserve(p_num_nodes);
+}
+
int AStar::get_closest_point(const Vector3 &p_point) const {
int closest_id = -1;
@@ -521,6 +535,9 @@ void AStar::_bind_methods() {
ClassDB::bind_method(D_METHOD("disconnect_points", "id", "to_id"), &AStar::disconnect_points);
ClassDB::bind_method(D_METHOD("are_points_connected", "id", "to_id"), &AStar::are_points_connected);
+ ClassDB::bind_method(D_METHOD("get_point_count"), &AStar::get_point_count);
+ ClassDB::bind_method(D_METHOD("get_point_capacity"), &AStar::get_point_capacity);
+ ClassDB::bind_method(D_METHOD("reserve_space", "num_nodes"), &AStar::reserve_space);
ClassDB::bind_method(D_METHOD("clear"), &AStar::clear);
ClassDB::bind_method(D_METHOD("get_closest_point", "to_position"), &AStar::get_closest_point);
@@ -605,10 +622,22 @@ bool AStar2D::are_points_connected(int p_id, int p_with_id) const {
return astar.are_points_connected(p_id, p_with_id);
}
+int AStar2D::get_point_count() const {
+ return astar.get_point_count();
+}
+
+int AStar2D::get_point_capacity() const {
+ return astar.get_point_capacity();
+}
+
void AStar2D::clear() {
astar.clear();
}
+void AStar2D::reserve_space(int p_num_nodes) {
+ astar.reserve_space(p_num_nodes);
+}
+
int AStar2D::get_closest_point(const Vector2 &p_point) const {
return astar.get_closest_point(Vector3(p_point.x, p_point.y, 0));
}
@@ -659,6 +688,9 @@ void AStar2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("disconnect_points", "id", "to_id"), &AStar2D::disconnect_points);
ClassDB::bind_method(D_METHOD("are_points_connected", "id", "to_id"), &AStar2D::are_points_connected);
+ ClassDB::bind_method(D_METHOD("get_point_count"), &AStar2D::get_point_count);
+ ClassDB::bind_method(D_METHOD("get_point_capacity"), &AStar2D::get_point_capacity);
+ ClassDB::bind_method(D_METHOD("reserve_space", "num_nodes"), &AStar2D::reserve_space);
ClassDB::bind_method(D_METHOD("clear"), &AStar2D::clear);
ClassDB::bind_method(D_METHOD("get_closest_point", "to_position"), &AStar2D::get_closest_point);
diff --git a/core/math/a_star.h b/core/math/a_star.h
index 53aaaa1f6c..ec2a06f07f 100644
--- a/core/math/a_star.h
+++ b/core/math/a_star.h
@@ -46,6 +46,10 @@ class AStar : public Reference {
struct Point {
+ Point() :
+ neighbours(4u),
+ unlinked_neighbours(4u) {}
+
int id;
Vector3 pos;
real_t weight_scale;
@@ -132,6 +136,9 @@ public:
void disconnect_points(int p_id, int p_with_id);
bool are_points_connected(int p_id, int p_with_id) const;
+ int get_point_count() const;
+ int get_point_capacity() const;
+ void reserve_space(int p_num_nodes);
void clear();
int get_closest_point(const Vector3 &p_point) const;
@@ -171,6 +178,9 @@ public:
void disconnect_points(int p_id, int p_with_id);
bool are_points_connected(int p_id, int p_with_id) const;
+ int get_point_count() const;
+ int get_point_capacity() const;
+ void reserve_space(int p_num_nodes);
void clear();
int get_closest_point(const Vector2 &p_point) const;