- just doing some C++ifying
[supertux.git] / src / sector.cpp
index 434582b..b815ba2 100644 (file)
@@ -274,28 +274,7 @@ Sector::write(LispWriter& writer)
 void
 Sector::add_object(GameObject* object)
 {
-  // XXX a bit hackish, at least try to keep the number of these things down...
-  BadGuy* badguy = dynamic_cast<BadGuy*> (object);
-  if(badguy)
-    badguys.push_back(badguy);
-  Bullet* bullet = dynamic_cast<Bullet*> (object);
-  if(bullet)
-    bullets.push_back(bullet);
-  Upgrade* upgrade = dynamic_cast<Upgrade*> (object);
-  if(upgrade)
-    upgrades.push_back(upgrade);
-  Trampoline* trampoline = dynamic_cast<Trampoline*> (object);
-  if(trampoline)
-    trampolines.push_back(trampoline);
-  FlyingPlatform* flying_platform = dynamic_cast<FlyingPlatform*> (object);
-  if(flying_platform)
-    flying_platforms.push_back(flying_platform);
-  InteractiveObject* interactive_object 
-      = dynamic_cast<InteractiveObject*> (object);
-  if(interactive_object)
-    interactive_objects.push_back(interactive_object);
-
-  gameobjects.push_back(object);
+  gameobjects_new.push_back(object);
 }
 
 void
@@ -397,6 +376,35 @@ Sector::action(float elapsed_time)
       ++i;
     }
   }
+
+  /* add newly created objects */
+  for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
+      i != gameobjects_new.end(); ++i)
+  {
+          BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
+          if(badguy)
+            badguys.push_back(badguy);
+          Bullet* bullet = dynamic_cast<Bullet*> (*i);
+          if(bullet)
+            bullets.push_back(bullet);
+          Upgrade* upgrade = dynamic_cast<Upgrade*> (*i);
+          if(upgrade)
+            upgrades.push_back(upgrade);
+          Trampoline* trampoline = dynamic_cast<Trampoline*> (*i);
+          if(trampoline)
+            trampolines.push_back(trampoline);
+          FlyingPlatform* flying_platform = dynamic_cast<FlyingPlatform*> (*i);
+          if(flying_platform)
+            flying_platforms.push_back(flying_platform);
+          InteractiveObject* interactive_object 
+              = dynamic_cast<InteractiveObject*> (*i);
+          if(interactive_object)
+            interactive_objects.push_back(interactive_object);
+
+          gameobjects.push_back(*i);
+  }
+  gameobjects_new.clear();
+
 }
 
 void
@@ -618,6 +626,13 @@ bool
 Sector::trybreakbrick(const Vector& pos, bool small)
 {
   Tile* tile = solids->get_tile_at(pos);
+  if (!tile)
+  {
+    char errmsg[64];
+    sprintf(errmsg, "Invalid tile at %i,%i", (int)((pos.x+1)/32*32), (int)((pos.y+1)/32*32));
+    throw SuperTuxException(errmsg, __FILE__, __LINE__);
+  }
+
   if (tile->attributes & Tile::BRICK)
     {
       if (tile->data > 0)
@@ -674,6 +689,14 @@ void
 Sector::tryemptybox(const Vector& pos, Direction col_side)
 {
   Tile* tile = solids->get_tile_at(pos);
+  if (!tile)
+  {
+    char errmsg[64];
+    sprintf(errmsg, "Invalid tile at %i,%i", (int)((pos.x+1)/32*32), (int)((pos.y+1)/32*32));
+    throw SuperTuxException(errmsg, __FILE__, __LINE__);
+  }
+
+
   if (!(tile->attributes & Tile::FULLBOX))
     return;
                                                                                 
@@ -730,6 +753,19 @@ void
 Sector::trygrabdistro(const Vector& pos, int bounciness)
 {
   Tile* tile = solids->get_tile_at(pos);
+  if (!tile)
+  {
+    /*char errmsg[64];
+    sprintf(errmsg, "Invalid tile at %i,%i", (int)((pos.x+1)/32*32), (int)((pos.y+1)/32*32));
+    throw SuperTuxException(errmsg, __FILE__, __LINE__); */
+    
+    //Bad tiles (i.e. tiles that are not defined in supertux.stgt but appear in the map) are changed to ID 0 (blank tile)
+    std::cout << "Warning: Undefined tile at " <<(int)pos.x/32 << "/" << (int)pos.y/32 << " (ID: " << (int)solids->get_tile_id_at(pos) << ")" << std::endl;
+    solids->change_at(pos,0);
+    tile = solids->get_tile_at(pos);
+  }
+
+
   if (!(tile->attributes & Tile::COIN))
     return;
 
@@ -744,6 +780,7 @@ Sector::trygrabdistro(const Vector& pos, int bounciness)
                                                                             
   player_status.score = player_status.score + SCORE_DISTRO;
   player_status.distros++;
+
 }
                                                                                 
 /* Try to bump a bad guy from below: */