mirror of https://github.com/godotengine/godot
Merge pull request #29488 from Daw11/astar-remove-node
Fix the performance of remove_point of AStar
This commit is contained in:
commit
bd937ea397
|
|
@ -99,14 +99,22 @@ void AStar::remove_point(int p_id) {
|
||||||
|
|
||||||
Point *p = points[p_id];
|
Point *p = points[p_id];
|
||||||
|
|
||||||
Map<int, Point *>::Element *PE = points.front();
|
for (Set<Point *>::Element *E = p->neighbours.front(); E; E = E->next()) {
|
||||||
while (PE) {
|
|
||||||
for (Set<Point *>::Element *E = PE->get()->neighbours.front(); E; E = E->next()) {
|
Segment s(p_id, E->get()->id);
|
||||||
Segment s(p_id, E->get()->id);
|
segments.erase(s);
|
||||||
segments.erase(s);
|
|
||||||
E->get()->neighbours.erase(p);
|
E->get()->neighbours.erase(p);
|
||||||
}
|
E->get()->unlinked_neighbours.erase(p);
|
||||||
PE = PE->next();
|
}
|
||||||
|
|
||||||
|
for (Set<Point *>::Element *E = p->unlinked_neighbours.front(); E; E = E->next()) {
|
||||||
|
|
||||||
|
Segment s(p_id, E->get()->id);
|
||||||
|
segments.erase(s);
|
||||||
|
|
||||||
|
E->get()->neighbours.erase(p);
|
||||||
|
E->get()->unlinked_neighbours.erase(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
memdelete(p);
|
memdelete(p);
|
||||||
|
|
@ -125,6 +133,8 @@ void AStar::connect_points(int p_id, int p_with_id, bool bidirectional) {
|
||||||
|
|
||||||
if (bidirectional)
|
if (bidirectional)
|
||||||
b->neighbours.insert(a);
|
b->neighbours.insert(a);
|
||||||
|
else
|
||||||
|
b->unlinked_neighbours.insert(a);
|
||||||
|
|
||||||
Segment s(p_id, p_with_id);
|
Segment s(p_id, p_with_id);
|
||||||
if (s.from == p_id) {
|
if (s.from == p_id) {
|
||||||
|
|
@ -147,7 +157,9 @@ void AStar::disconnect_points(int p_id, int p_with_id) {
|
||||||
Point *a = points[p_id];
|
Point *a = points[p_id];
|
||||||
Point *b = points[p_with_id];
|
Point *b = points[p_with_id];
|
||||||
a->neighbours.erase(b);
|
a->neighbours.erase(b);
|
||||||
|
a->unlinked_neighbours.erase(b);
|
||||||
b->neighbours.erase(a);
|
b->neighbours.erase(a);
|
||||||
|
b->unlinked_neighbours.erase(a);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AStar::has_point(int p_id) const {
|
bool AStar::has_point(int p_id) const {
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,7 @@ class AStar : public Reference {
|
||||||
bool enabled;
|
bool enabled;
|
||||||
|
|
||||||
Set<Point *> neighbours;
|
Set<Point *> neighbours;
|
||||||
|
Set<Point *> unlinked_neighbours;
|
||||||
|
|
||||||
// Used for pathfinding
|
// Used for pathfinding
|
||||||
Point *prev_point;
|
Point *prev_point;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue