]> git.itanic.dy.fi Git - sdl-planets/commitdiff
planet.c: Always remove the smallest planet
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Wed, 17 Nov 2010 16:45:55 +0000 (18:45 +0200)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Wed, 17 Nov 2010 17:26:40 +0000 (19:26 +0200)
As we are not allowed to just move nodes in the quadtree (that might
lead crossing of branch boundaries), we will try to only remove the
smaller planet and add everything else to the larger planet.

planet.c

index 1880dc6baa40b4be1d1320042654c4a7ac77c797..1bedc6e6f2900036beb6515fa07b81488a3cd24e 100644 (file)
--- a/planet.c
+++ b/planet.c
@@ -263,12 +263,14 @@ int gravitize_planet_with_tree(struct planet *a, struct planet *b,
 }
 
 /*
- * Merge planets a and b into planet a
+ * Merge planets a and b together
  *
- * It is left for the caller to deal with the scrap planet b
+ * The smaller planet will be deleted, matter will be transferred to
+ * the bigger planet.
  */
-static void _merge_planets(struct planet *a, struct planet *b)
+static struct planet *_merge_planets(struct planet *a, struct planet *b)
 {
+       struct planet *rem, *nr;
        struct vector pa, pb, p;
        float mass;
 
@@ -277,34 +279,47 @@ static void _merge_planets(struct planet *a, struct planet *b)
        vector_add(&pa, &pb, &p);
        mass = a->mass + b->mass;
 
-       //if (a->mass < b->mass)
-       //a->tree.pos = b->tree.pos;
+       if (a->mass < b->mass) {
+               rem = a;
+               nr = a;
+       } else {
+               rem = b;
+               nr = a;
+       }
+
+       nr->r = (a->r * a->mass + b->r * b->mass) / mass;
+       nr->g = (a->g * a->mass + b->g * b->mass) / mass;
+       nr->b = (a->b * a->mass + b->b * b->mass) / mass;
 
-       a->r = (a->r * a->mass + b->r * b->mass) / mass;
-       a->g = (a->g * a->mass + b->g * b->mass) / mass;
-       a->b = (a->b * a->mass + b->b * b->mass) / mass;
+       nr->mass = mass;
+       reshape_planet(nr);
+       vector_div(&p, nr->mass, &nr->speed);
 
-       a->mass = mass;
-       reshape_planet(a);
-       vector_div(&p, a->mass, &a->speed);
+       return rem;
 }
 
 /*
- * Merge planets a and b into a the new planet a, which pointer is
- * returned to the caller. Planet b is removed from the linked list
- * and it's memory is freed. The merged planet will retain in the
- * list.
+ * Merge planets a and b into a the new planet, which pointer is
+ * returned to the caller. The other planet is removed from the linked
+ * list and it's memory is freed. The merged planet will be kept in
+ * the list.
  */
 struct planet *merge_planets(struct planet *a, struct planet *b)
 {
-       _merge_planets(a, b);
+       struct planet *r;
+
+       r = _merge_planets(a, b);
 
        //list_del(&b->list);
        //quadtree_del(&b->tree, &planet_ops);
 
        //free(b);
-       b->to_be_deleted = 1;
-       return a;
+       if (r == a)
+               a->to_be_deleted = 1;
+       else
+               b->to_be_deleted = 1;
+
+       return r;
 }
 
 static void gravitize_planet(struct planet *p, struct planet *pt, double time)
@@ -391,7 +406,6 @@ static int planet_search_when_moving(struct quadtree *node,
 
 void planet_move_iterator(struct quadtree *node, struct quadtree_iterator *it)
 {
-       struct quadtree *parent;
        struct planet_search_iterator *itr = qt_itr_to_planet_itr(it);
        if (node == it->head)
                return;