Fixed compiler warnings due to new loglevel aware log macro
[supertux.git] / src / badguy / dispenser.cpp
index 732e732..071e17d 100644 (file)
@@ -183,7 +183,7 @@ Dispenser::launch_badguy()
 
     if (badguys.size() > 1) {
       if (random) {
-        next_badguy = systemRandom.rand(badguys.size());
+        next_badguy = gameRandom.rand(badguys.size());
       }
       else {
         next_badguy++;
@@ -199,23 +199,48 @@ Dispenser::launch_badguy()
       log_warning << "random is outdated; use a list of badguys to select from." << std::endl;
       return;
     }
-
-    GameObject* badguy_object = NULL;
+    if(badguy == "goldbomb") {
+      log_warning << "goldbomb is not allowed to be dispensed" << std::endl;
+      return;
+    }
 
     try {
+      GameObject *game_object;
+      BadGuy *bad_guy;
       Vector spawnpoint;
+      Rectf object_bbox;
+
+      /* Need to allocate the badguy first to figure out its bounding box. */
+      game_object = ObjectFactory::instance().create(badguy, get_pos(), launchdir);
+      if (game_object == NULL)
+        throw std::runtime_error("Creating " + badguy + " object failed.");
+
+      bad_guy = dynamic_cast<BadGuy *> (game_object);
+      if (bad_guy == NULL)
+        throw std::runtime_error(badguy + " is not a badguy.");
+
+      object_bbox = bad_guy->get_bbox ();
+
+      if (type == "dropper") {
+        spawnpoint = get_anchor_pos (get_bbox (), ANCHOR_BOTTOM);
+        spawnpoint.x -= 0.5 * object_bbox.get_width ();
+      }
+      else if ((type == "cannon") || (type == "rocketlauncher")) {
+        spawnpoint = get_pos (); /* top-left corner of the cannon */
+        if (launchdir == LEFT)
+          spawnpoint.x -= object_bbox.get_width () + 1;
+        else
+          spawnpoint.x += get_bbox ().get_width () + 1;
+      }
 
-      if (type == "dropper")
-        spawnpoint = Vector(get_pos().x, get_pos().y+32);
-      else if (type == "cannon")
-        spawnpoint = Vector(get_pos().x + (launchdir == LEFT ? -32 : 32), get_pos().y);
-      else if (type == "rocketlauncher")
-        spawnpoint = Vector(get_pos().x + (launchdir == LEFT ? -32 : 32), get_pos().y);
+      /* Now we set the real spawn position */
+      bad_guy->set_pos (spawnpoint);
 
-      badguy_object = ObjectFactory::instance().create(badguy, spawnpoint, launchdir);
+      /* We don't want to count dispensed badguys in level stats */
+      if(bad_guy->countMe)
+        bad_guy->countMe = false;
 
-      if (badguy_object)
-        Sector::current()->add_object(badguy_object);
+      Sector::current()->add_object(bad_guy);
     } catch(std::exception& e) {
       log_warning << "Error dispensing badguy: " << e.what() << std::endl;
       return;