]> git.itanic.dy.fi Git - sdl-planets/commitdiff
Does not appear to crash anymore
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Tue, 16 Nov 2010 20:52:48 +0000 (22:52 +0200)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Tue, 16 Nov 2010 20:52:48 +0000 (22:52 +0200)
quadtree.c

index 4b0a0e3354eb3d8cd67d5a106934f0f31af68c8b..2c8df4cf3d7a2bf7a590bbe01d1d2e30bc2cb276 100644 (file)
@@ -360,7 +360,7 @@ static struct quadtree *quadtree_find_nearest(struct quadtree *tree,
                                        struct vector *corner)
 {
        struct vector tmp;
-       struct quadtree *nearest = tree, *node;
+       struct quadtree *nearest, *node;
        double distance, dist;
        int ret, i, directions;
 
@@ -376,8 +376,13 @@ static struct quadtree *quadtree_find_nearest(struct quadtree *tree,
                return NULL;
        }
 
-       vector_sub(pos, &tree->pos, &tmp);
-       distance = vector_abs(&tmp);
+       if (is_within(&tree->pos, corner)) {
+               vector_sub(pos, &tree->pos, &tmp);
+               distance = vector_abs(&tmp);
+               nearest = tree;
+       } else {
+               nearest = NULL;
+       }
 
        directions = quadrants_to_search(tree, corner);
 
@@ -403,7 +408,7 @@ static struct quadtree *quadtree_find_nearest(struct quadtree *tree,
                printf("%p: (%f %f)dist: %f, distance: %f\n",
                        node, node->pos.x, node->pos.y,
                        dist, distance);
-               if (dist < distance) {
+               if (!nearest || dist < distance) {
                        nearest = node;
                        distance = dist;
 
@@ -411,7 +416,7 @@ static struct quadtree *quadtree_find_nearest(struct quadtree *tree,
                }
        }
 
-       if (!is_within(&nearest->pos, corner)) {
+       if (nearest && !is_within(&nearest->pos, corner)) {
                if (debug)
                        printf("Node %p (%f %f) is not within "
                                "search window (%f %f) (%f %f)\n",
@@ -420,6 +425,7 @@ static struct quadtree *quadtree_find_nearest(struct quadtree *tree,
                                corner[1].x, corner[1].y);
                return NULL;
        }
+       printf("%d: Returning %p\n",__LINE__, nearest);
 
        return nearest;
 }
@@ -437,7 +443,7 @@ static struct quadtree *quadtree_find_nearest_noparent(struct quadtree *tree,
        nearest = quadtree_find_nearest(tree, pos, corner);
 
        if (nearest != tree)
-               return nearest;
+               goto out;
 
        if (debug)
                printf("Avoiding parent or NULL node %p\n", nearest);
@@ -479,6 +485,8 @@ static struct quadtree *quadtree_find_nearest_noparent(struct quadtree *tree,
                printf("Re-setting nearest from %d\n", i);
        }
 
+out:
+       printf("%d: Returning %p\n", __LINE__, nearest);
        return nearest;
 }