Change music fade-in time
[supertux.git] / src / supertux / sector.cpp
index 3553d59..ba097a0 100644 (file)
@@ -58,6 +58,7 @@
 #include "supertux/savegame.hpp"
 #include "supertux/spawn_point.hpp"
 #include "supertux/tile.hpp"
+#include "trigger/secretarea_trigger.hpp"
 #include "trigger/sequence_trigger.hpp"
 #include "util/file_system.hpp"
 
@@ -76,6 +77,7 @@ Sector::Sector(Level* parent) :
   sector_table(),
   scripts(),
   ambient_light( 1.0f, 1.0f, 1.0f, 1.0f ),
+  foremost_layer(),
   gameobjects(),
   moving_objects(),
   spawnpoints(),
@@ -113,11 +115,17 @@ Sector::Sector(Level* parent) :
 Sector::~Sector()
 {
   using namespace scripting;
+  try
+  {
+    deactivate();
+  }
+  catch(const std::exception& err)
+  {
+    log_warning << err.what() << std::endl;
+  }
 
-  deactivate();
 
-  for(ScriptList::iterator i = scripts.begin();
-      i != scripts.end(); ++i) {
+  for(auto i = scripts.begin(); i != scripts.end(); ++i) {
     HSQOBJECT& object = *i;
     sq_release(global_vm, &object);
   }
@@ -127,8 +135,7 @@ Sector::~Sector()
   update_game_objects();
   assert(gameobjects_new.size() == 0);
 
-  for(GameObjects::iterator i = gameobjects.begin();
-      i != gameobjects.end(); ++i) {
+  for(auto i = gameobjects.begin(); i != gameobjects.end(); ++i) {
     GameObjectPtr object = *i;
     before_object_remove(object);
   }
@@ -226,7 +233,7 @@ Sector::parse(const Reader& sector)
 
   update_game_objects();
 
-  if(solid_tilemaps.size() < 1) { log_warning << "sector '" << name << "' does not contain a solid tile layer." << std::endl; }
+  if(solid_tilemaps.empty()) { log_warning << "sector '" << name << "' does not contain a solid tile layer." << std::endl; }
 
   fix_old_tiles();
   if(!camera) {
@@ -236,6 +243,7 @@ Sector::parse(const Reader& sector)
   }
 
   update_game_objects();
+  foremost_layer = calculate_foremost_layer();
 }
 
 void
@@ -391,7 +399,7 @@ Sector::parse_old_format(const Reader& reader)
 
   update_game_objects();
 
-  if(solid_tilemaps.size() < 1) { log_warning << "sector '" << name << "' does not contain a solid tile layer." << std::endl; }
+  if(solid_tilemaps.empty()) { log_warning << "sector '" << name << "' does not contain a solid tile layer." << std::endl; }
 
   fix_old_tiles();
   update_game_objects();
@@ -400,7 +408,7 @@ Sector::parse_old_format(const Reader& reader)
 void
 Sector::fix_old_tiles()
 {
-  for(std::list<TileMap*>::iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
+  for(auto i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
     TileMap* solids = *i;
     for(size_t x=0; x < solids->get_width(); ++x) {
       for(size_t y=0; y < solids->get_height(); ++y) {
@@ -418,7 +426,9 @@ Sector::fix_old_tiles()
           add_object(std::make_shared<BonusBlock>(pos, tile->getData()));
           solids->change(x, y, 0);
         } else if(tile->getAttributes() & Tile::BRICK) {
-          if( ( id == 78 ) || ( id == 105 ) ){
+          if( ( id == 3159 ) || ( id == 3160 ) ){
+            add_object( std::make_shared<Brick>(pos, tile->getData(), "images/objects/bonus_block/brickWeb.sprite") );
+          } else if( ( id == 78 ) || ( id == 105 ) ){
             add_object( std::make_shared<Brick>(pos, tile->getData(), "images/objects/bonus_block/brickIce.sprite") );
           } else if( ( id == 77 ) || ( id == 104 ) ){
             add_object( std::make_shared<Brick>(pos, tile->getData(), "images/objects/bonus_block/brick.sprite") );
@@ -437,7 +447,7 @@ Sector::fix_old_tiles()
   }
 
   // add lights for special tiles
-  for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); i++) {
+  for(auto i = gameobjects.begin(); i != gameobjects.end(); i++) {
     TileMap* tm = dynamic_cast<TileMap*>(i->get());
     if (!tm) continue;
     for(size_t x=0; x < tm->get_width(); ++x) {
@@ -474,8 +484,7 @@ Sector::run_script(std::istream& in, const std::string& sourcename)
   using namespace scripting;
 
   // garbage collect thread list
-  for(ScriptList::iterator i = scripts.begin();
-      i != scripts.end(); ) {
+  for(auto i = scripts.begin(); i != scripts.end(); ) {
     HSQOBJECT& object = *i;
     HSQUIRRELVM vm = object_to_vm(object);
 
@@ -511,12 +520,10 @@ Sector::add_object(GameObjectPtr object)
 {
   // make sure the object isn't already in the list
 #ifndef NDEBUG
-  for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end();
-      ++i) {
+  for(auto i = gameobjects.begin(); i != gameobjects.end(); ++i) {
     assert(*i != object);
   }
-  for(GameObjects::iterator i = gameobjects_new.begin();
-      i != gameobjects_new.end(); ++i) {
+  for(auto i = gameobjects_new.begin(); i != gameobjects_new.end(); ++i) {
     assert(*i != object);
   }
 #endif
@@ -528,8 +535,7 @@ void
 Sector::activate(const std::string& spawnpoint)
 {
   std::shared_ptr<SpawnPoint> sp;
-  for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
-      ++i) {
+  for(auto i = spawnpoints.begin(); i != spawnpoints.end(); ++i) {
     if((*i)->name == spawnpoint) {
       sp = *i;
       break;
@@ -588,7 +594,8 @@ Sector::activate(const Vector& player_pos)
 
     // spawning tux in the ground would kill him
     if(!is_free_of_tiles(p->get_bbox())) {
-      log_warning << "Tried spawning Tux in solid matter. Compensating." << std::endl;
+      std::string current_level = "[" + Sector::current()->get_level()->filename + "] ";
+      log_warning << current_level << "Tried spawning Tux in solid matter. Compensating." << std::endl;
       Vector npos = p->get_bbox().p1;
       npos.y-=32;
       p->move(npos);
@@ -656,6 +663,36 @@ Sector::get_active_region()
     camera->get_translation() + Vector(1600, 1200) + Vector(SCREEN_WIDTH,SCREEN_HEIGHT));
 }
 
+int
+Sector::calculate_foremost_layer()
+{
+  int layer = LAYER_BACKGROUND0;
+  for(auto i = gameobjects.begin(); i != gameobjects.end(); ++i)
+  {
+    TileMap* tm = dynamic_cast<TileMap*>(i->get());
+    if (!tm) continue;
+    if(tm->get_layer() > layer)
+    {
+      if( (tm->get_alpha() < 1.0) )
+      {
+        layer = tm->get_layer() - 1;
+      }
+      else
+      {
+        layer = tm->get_layer() + 1;
+      }
+    }
+  }
+  log_debug << "Calculated baduy falling layer was: " << layer << std::endl;
+  return layer;
+}
+
+int
+Sector::get_foremost_layer()
+{
+  return foremost_layer;
+}
+
 void
 Sector::update(float elapsed_time)
 {
@@ -874,8 +911,7 @@ Sector::draw(DrawingContext& context)
 
   if(show_collrects) {
     Color color(1.0f, 0.0f, 0.0f, 0.75f);
-    for(MovingObjects::iterator i = moving_objects.begin();
-        i != moving_objects.end(); ++i) {
+    for(auto i = moving_objects.begin(); i != moving_objects.end(); ++i) {
       MovingObject* object = *i;
       const Rectf& rect = object->get_bbox();
 
@@ -974,7 +1010,7 @@ Sector::collision_tilemap(collision::Constraints* constraints,
   float y1 = dest.get_top();
   float y2 = dest.get_bottom();
 
-  for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
+  for(auto i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
     TileMap* solids = *i;
 
     // test with all tiles in this rectangle
@@ -1029,7 +1065,7 @@ Sector::collision_tile_attributes(const Rectf& dest) const
   float y2 = dest.p2.y;
 
   uint32_t result = 0;
-  for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
+  for(auto i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
     TileMap* solids = *i;
 
     // test with all tiles in this rectangle
@@ -1135,8 +1171,7 @@ Sector::collision_static(collision::Constraints* constraints,
   collision_tilemap(constraints, movement, dest, object);
 
   // collision with other (static) objects
-  for(MovingObjects::iterator i = moving_objects.begin();
-      i != moving_objects.end(); ++i) {
+  for(auto i = moving_objects.begin(); i != moving_objects.end(); ++i) {
     MovingObject* moving_object = *i;
     if(moving_object->get_group() != COLGROUP_STATIC
        && moving_object->get_group() != COLGROUP_MOVING_STATIC)
@@ -1259,8 +1294,7 @@ Sector::handle_collisions()
   using namespace collision;
 
   // calculate destination positions of the objects
-  for(MovingObjects::iterator i = moving_objects.begin();
-      i != moving_objects.end(); ++i) {
+  for(auto i = moving_objects.begin(); i != moving_objects.end(); ++i) {
     MovingObject* moving_object = *i;
     Vector mov = moving_object->get_movement();
 
@@ -1275,8 +1309,7 @@ Sector::handle_collisions()
   }
 
   // part1: COLGROUP_MOVING vs COLGROUP_STATIC and tilemap
-  for(MovingObjects::iterator i = moving_objects.begin();
-      i != moving_objects.end(); ++i) {
+  for(auto i = moving_objects.begin(); i != moving_objects.end(); ++i) {
     MovingObject* moving_object = *i;
     if((moving_object->get_group() != COLGROUP_MOVING
         && moving_object->get_group() != COLGROUP_MOVING_STATIC
@@ -1288,8 +1321,7 @@ Sector::handle_collisions()
   }
 
   // part2: COLGROUP_MOVING vs tile attributes
-  for(MovingObjects::iterator i = moving_objects.begin();
-      i != moving_objects.end(); ++i) {
+  for(auto i = moving_objects.begin(); i != moving_objects.end(); ++i) {
     MovingObject* moving_object = *i;
     if((moving_object->get_group() != COLGROUP_MOVING
         && moving_object->get_group() != COLGROUP_MOVING_STATIC
@@ -1304,16 +1336,14 @@ Sector::handle_collisions()
   }
 
   // part2.5: COLGROUP_MOVING vs COLGROUP_TOUCHABLE
-  for(MovingObjects::iterator i = moving_objects.begin();
-      i != moving_objects.end(); ++i) {
+  for(auto i = moving_objects.begin(); i != moving_objects.end(); ++i) {
     MovingObject* moving_object = *i;
     if((moving_object->get_group() != COLGROUP_MOVING
         && moving_object->get_group() != COLGROUP_MOVING_STATIC)
        || !moving_object->is_valid())
       continue;
 
-    for(MovingObjects::iterator i2 = moving_objects.begin();
-        i2 != moving_objects.end(); ++i2) {
+    for(auto i2 = moving_objects.begin(); i2 != moving_objects.end(); ++i2) {
       MovingObject* moving_object_2 = *i2;
       if(moving_object_2->get_group() != COLGROUP_TOUCHABLE
          || !moving_object_2->is_valid())
@@ -1336,8 +1366,7 @@ Sector::handle_collisions()
   }
 
   // part3: COLGROUP_MOVING vs COLGROUP_MOVING
-  for(MovingObjects::iterator i = moving_objects.begin();
-      i != moving_objects.end(); ++i) {
+  for(auto i = moving_objects.begin(); i != moving_objects.end(); ++i) {
     MovingObject* moving_object = *i;
 
     if((moving_object->get_group() != COLGROUP_MOVING
@@ -1345,8 +1374,7 @@ Sector::handle_collisions()
        || !moving_object->is_valid())
       continue;
 
-    for(MovingObjects::iterator i2 = i+1;
-        i2 != moving_objects.end(); ++i2) {
+    for(auto i2 = i+1; i2 != moving_objects.end(); ++i2) {
       MovingObject* moving_object_2 = *i2;
       if((moving_object_2->get_group() != COLGROUP_MOVING
           && moving_object_2->get_group() != COLGROUP_MOVING_STATIC)
@@ -1358,8 +1386,7 @@ Sector::handle_collisions()
   }
 
   // apply object movement
-  for(MovingObjects::iterator i = moving_objects.begin();
-      i != moving_objects.end(); ++i) {
+  for(auto i = moving_objects.begin(); i != moving_objects.end(); ++i) {
     MovingObject* moving_object = *i;
 
     moving_object->bbox = moving_object->dest;
@@ -1372,7 +1399,7 @@ Sector::is_free_of_tiles(const Rectf& rect, const bool ignoreUnisolid) const
 {
   using namespace collision;
 
-  for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
+  for(auto i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
     TileMap* solids = *i;
 
     // test with all tiles in this rectangle
@@ -1410,8 +1437,7 @@ Sector::is_free_of_statics(const Rectf& rect, const MovingObject* ignore_object,
 
   if (!is_free_of_tiles(rect, ignoreUnisolid)) return false;
 
-  for(MovingObjects::const_iterator i = moving_objects.begin();
-      i != moving_objects.end(); ++i) {
+  for(auto i = moving_objects.begin(); i != moving_objects.end(); ++i) {
     const MovingObject* moving_object = *i;
     if (moving_object == ignore_object) continue;
     if (!moving_object->is_valid()) continue;
@@ -1430,8 +1456,7 @@ Sector::is_free_of_movingstatics(const Rectf& rect, const MovingObject* ignore_o
 
   if (!is_free_of_tiles(rect)) return false;
 
-  for(MovingObjects::const_iterator i = moving_objects.begin();
-      i != moving_objects.end(); ++i) {
+  for(auto i = moving_objects.begin(); i != moving_objects.end(); ++i) {
     const MovingObject* moving_object = *i;
     if (moving_object == ignore_object) continue;
     if (!moving_object->is_valid()) continue;
@@ -1446,23 +1471,6 @@ Sector::is_free_of_movingstatics(const Rectf& rect, const MovingObject* ignore_o
 }
 
 bool
-Sector::add_bullet(const Vector& pos, const PlayerStatus* player_status, float xm, Direction dir)
-{
-  // TODO remove this function and move these checks elsewhere...
-  if((player_status->bonus == FIRE_BONUS &&
-      (int)bullets.size() >= player_status->max_fire_bullets) ||
-     (player_status->bonus == ICE_BONUS &&
-      (int)bullets.size() >= player_status->max_ice_bullets))
-    return false;
-  auto new_bullet = std::make_shared<Bullet>(pos, xm, dir, player_status->bonus);
-  add_object(new_bullet);
-
-  SoundManager::current()->play("sounds/shoot.wav");
-
-  return true;
-}
-
-bool
 Sector::add_smoke_cloud(const Vector& pos)
 {
   add_object(std::make_shared<SmokeCloud>(pos));
@@ -1489,6 +1497,20 @@ Sector::play_music(MusicType type)
   }
 }
 
+void
+Sector::resume_music()
+{
+  if(SoundManager::current()->get_current_music() == music)
+  {
+    SoundManager::current()->resume_music(3.2f);
+  }
+  else
+  {
+    SoundManager::current()->stop_music();
+    SoundManager::current()->play_music(music, true);
+  }
+}
+
 MusicType
 Sector::get_music_type()
 {
@@ -1511,7 +1533,7 @@ Sector::get_total_badguys()
 bool
 Sector::inside(const Rectf& rect) const
 {
-  for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
+  for(auto i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
     TileMap* solids = *i;
 
     Rectf bbox = solids->get_bbox();
@@ -1527,8 +1549,7 @@ float
 Sector::get_width() const
 {
   float width = 0;
-  for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin();
-      i != solid_tilemaps.end(); i++) {
+  for(auto i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
     TileMap* solids = *i;
     width = std::max(width, solids->get_bbox().get_right());
   }
@@ -1540,7 +1561,7 @@ float
 Sector::get_height() const
 {
   float height = 0;
-  for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin();
+  for(auto i = solid_tilemaps.begin();
       i != solid_tilemaps.end(); i++) {
     TileMap* solids = *i;
     height = std::max(height, solids->get_bbox().get_bottom());
@@ -1552,7 +1573,7 @@ Sector::get_height() const
 void
 Sector::change_solid_tiles(uint32_t old_tile_id, uint32_t new_tile_id)
 {
-  for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
+  for(auto i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
     TileMap* solids = *i;
     solids->change_all(old_tile_id, new_tile_id);
   }
@@ -1604,9 +1625,7 @@ Sector::get_nearest_player (const Vector& pos)
   float nearest_dist = std::numeric_limits<float>::max();
 
   std::vector<Player*> players = Sector::current()->get_players();
-  for (std::vector<Player*>::iterator playerIter = players.begin();
-      playerIter != players.end();
-      ++playerIter)
+  for (auto playerIter = players.begin(); playerIter != players.end(); ++playerIter)
   {
     Player *this_player = *playerIter;
     if (this_player->is_dying() || this_player->is_dead())