make function more robust
authorMatthias Braun <matze@braunis.de>
Tue, 23 Nov 2004 16:28:03 +0000 (16:28 +0000)
committerMatthias Braun <matze@braunis.de>
Tue, 23 Nov 2004 16:28:03 +0000 (16:28 +0000)
SVN-Revision: 2152

src/level.cpp

index fd954a7..f317b22 100644 (file)
@@ -252,10 +252,22 @@ int
 Level::get_total_coins()
 {
   int total_coins = 0;
-  for(Sectors::iterator it = sectors.begin(); it != sectors.end(); ++it)
-    for(int x = 0; static_cast<unsigned int>(x) < it->second->solids->get_width(); x++)
-      for(int y = 0; static_cast<unsigned int>(y) < it->second->solids->get_height(); y++)
-        if(it->second->solids->get_tile(x,y)->attributes & Tile::COIN)
+  for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i) {
+    TileMap* solids = i->second->solids;
+    if(!solids) {
+      std::cerr << "Sector '" << i->first << "' contains no solids!?!\n";
+      continue;
+    }
+    for(size_t x = 0; x < solids->get_width(); ++x)
+      for(size_t y = 0; y < solids->get_height(); ++y) {
+        const Tile* tile = solids->get_tile(x, y);
+        if(tile == 0) {
+          std::cerr << "Invalid tile in sector '" << i->first << "'.\n";
+          continue;
+        }
+        if(tile->attributes & Tile::COIN)
           total_coins++;
+      }
+  }
   return total_coins;
 }