return a;
}
-static int planet_search_when_moving(struct quadtree *node,
- struct quadtree_iterator *itr)
-{
- struct planet *p = tree_to_planet(node);
- struct planet_search_iterator *it = qt_itr_to_planet_itr(itr);
- int direction = 0, i;
- int up = 0, left = 0, right = 0, down = 0;
-
- for (i = 0; i < 2; i++) {
- if (it->limit[i].x < p->tree.pos.x)
- left = 1;
- else
- right = 1;
- if (it->limit[i].y < p->tree.pos.y)
- up = 1;
- else
- down = 1;
- }
-
- if (left || up)
- direction |= QUADTREE_UPLEFT;
- if (right || up)
- direction |= QUADTREE_UPRIGHT;
- if (left || down)
- direction |= QUADTREE_DOWNLEFT;
- if (right || down)
- direction |= QUADTREE_DOWNRIGHT;
- if ((left && right) || (up && down))
- direction |= QUADTREE_SELF;
-
- return direction;
-}
-
-void planet_move_iterator(struct quadtree *node, struct quadtree_iterator *it)
-{
- struct quadtree *parent;
-
- if (node == it->head)
- return;
-
- parent = quadtree_del(node, &planet_ops);
- quadtree_add(parent, node, &planet_ops);
-}
-
struct planet *move_planet(struct planet *p, const double time)
{
struct vector tmp, new_pos;
- struct quadtree *parent, *tree_parent;
- struct planet *pa;
- struct planet_search_iterator it;
-
- int modify = 0;
vector_scale(&p->speed, time, &tmp);
vector_add(&p->tree.pos, &tmp, &new_pos);
- /* Check if we have crossed any of the parents */
- parent = p->tree.parent;
- while (parent) {
- pa = tree_to_planet(parent);
- if (p->tree.pos.x < pa->tree.pos.x && new_pos.x > pa->tree.pos.x)
- modify = 1;
- if (p->tree.pos.x > pa->tree.pos.x && new_pos.x < pa->tree.pos.x)
- modify = 1;
- if (p->tree.pos.y < pa->tree.pos.y && new_pos.y > pa->tree.pos.y)
- modify = 1;
- if (p->tree.pos.y > pa->tree.pos.y && new_pos.y < pa->tree.pos.y)
- modify = 1;
-
- if (!modify) {
- parent = parent->parent;
- continue;
- }
-
- tree_parent = quadtree_del(&p->tree, &planet_ops);
- p->tree.pos = new_pos;
- quadtree_add(tree_parent, &p->tree, &planet_ops);
- return tree_to_planet(tree_parent);
- }
-
- 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->tree.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->tree.pos = new_pos;
-
- return tree_to_planet(quadtree_find_parent(&p->tree));
+ return tree_to_planet(quadtree_move(&p->tree, new_pos, &planet_ops));
}
void print_planet(const struct planet *p)