]> git.itanic.dy.fi Git - sdl-planets/commitdiff
move_planet: Optimize the tree topology change detection
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Sun, 18 Apr 2010 10:24:56 +0000 (13:24 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Sun, 18 Apr 2010 10:27:52 +0000 (13:27 +0300)
This change avoids repositioning unnecessary the nodes within the
tree.

Signed-off-by: Timo Kokkonen <kaapeli@itanic.dy.fi>
planet.c

index 330f82af7b91dae0099330f4e3dfc9110d7632d6..2482dd9e86e6387e3e995e403451ea41aa2a61e0 100644 (file)
--- a/planet.c
+++ b/planet.c
@@ -302,6 +302,9 @@ void planet_move_iterator(struct quadtree *node, struct quadtree_iterator *it)
 {
        struct quadtree *parent;
 
 {
        struct quadtree *parent;
 
+       if (node == it->head)
+               return;
+
        parent = quadtree_del(node, &planet_ops);
        quadtree_add(parent, node, &planet_ops);
 }
        parent = quadtree_del(node, &planet_ops);
        quadtree_add(parent, node, &planet_ops);
 }
@@ -342,17 +345,18 @@ struct planet *move_planet(struct planet *p, const double time)
                return tree_to_planet(tree_parent);
        }
 
                return tree_to_planet(tree_parent);
        }
 
-       /*
-        * Now, search the subtree for any crossed children and move
-        * them into correct place within the tree.
-        */
-       it.qt_iterator.head = &p->tree;
-       it.limit[0] = p->pos;
-       it.limit[1] = new_pos;
-       it.qt_iterator.direction = planet_search_when_moving;
-       it.qt_iterator.callback = planet_move_iterator;
-       walk_quadtree(&it.qt_iterator);
-
+       if(p->tree.children) {
+               /*
+                * Now, search the subtree for any crossed children and move
+                * them into correct place within the tree.
+                */
+               it.qt_iterator.head = &p->tree;
+               it.limit[0] = p->pos;
+               it.limit[1] = new_pos;
+               it.qt_iterator.direction = planet_search_when_moving;
+               it.qt_iterator.callback = planet_move_iterator;
+               walk_quadtree(&it.qt_iterator);
+       }
        p->pos = new_pos;
 
        return tree_to_planet(quadtree_find_parent(&p->tree));
        p->pos = new_pos;
 
        return tree_to_planet(quadtree_find_parent(&p->tree));