Worldmap teleporter is now animated / All Worldmap special tiles can be arbitrary...
[supertux.git] / src / worldmap.cpp
index 3649bab..b667aec 100644 (file)
@@ -2,6 +2,7 @@
 //
 //  SuperTux -  A Jump'n Run
 //  Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
+//  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
 //
 //  This program is free software; you can redistribute it and/or
 //  modify it under the terms of the GNU General Public License
 #include <stdexcept>
 #include <sstream>
 #include <unistd.h>
-
-#include "gettext.h"
-#include "video/surface.h"
-#include "video/screen.h"
-#include "video/drawing_context.h"
-#include "sprite/sprite_manager.h"
-#include "audio/sound_manager.h"
-#include "lisp/parser.h"
-#include "lisp/lisp.h"
-#include "lisp/list_iterator.h"
-#include "lisp/writer.h"
-#include "game_session.h"
-#include "sector.h"
-#include "worldmap.h"
-#include "resources.h"
-#include "misc.h"
-#include "player_status.h"
-#include "textscroller.h"
-#include "main.h"
-#include "spawn_point.h"
-#include "file_system.h"
-#include "gui/menu.h"
-#include "gui/mousecursor.h"
-#include "control/joystickkeyboardcontroller.h"
-#include "object/background.h"
-#include "object/tilemap.h"
-#include "scripting/script_interpreter.h"
+#include <physfs.h>
+
+#include "gettext.hpp"
+#include "msg.hpp"
+#include "video/surface.hpp"
+#include "video/screen.hpp"
+#include "video/drawing_context.hpp"
+#include "sprite/sprite_manager.hpp"
+#include "audio/sound_manager.hpp"
+#include "lisp/parser.hpp"
+#include "lisp/lisp.hpp"
+#include "lisp/list_iterator.hpp"
+#include "lisp/writer.hpp"
+#include "game_session.hpp"
+#include "sector.hpp"
+#include "worldmap.hpp"
+#include "resources.hpp"
+#include "misc.hpp"
+#include "player_status.hpp"
+#include "textscroller.hpp"
+#include "main.hpp"
+#include "spawn_point.hpp"
+#include "file_system.hpp"
+#include "gui/menu.hpp"
+#include "gui/mousecursor.hpp"
+#include "control/joystickkeyboardcontroller.hpp"
+#include "object/background.hpp"
+#include "object/tilemap.hpp"
+#include "scripting/script_interpreter.hpp"
+#include "exceptions.hpp"
 
 Menu* worldmap_menu  = 0;
 
@@ -116,7 +120,7 @@ string_to_direction(const std::string& directory)
 Tux::Tux(WorldMap* worldmap_)
   : worldmap(worldmap_)
 {
-  tux_sprite = sprite_manager->create("worldmaptux");
+  tux_sprite = sprite_manager->create("images/worldmap/common/tux.sprite");
   
   offset = 0;
   moving = false;
@@ -132,21 +136,19 @@ Tux::~Tux()
 void
 Tux::draw(DrawingContext& context)
 {
-  switch (player_status.bonus) {
+  switch (player_status->bonus) {
     case GROWUP_BONUS:
-      tux_sprite->set_action("large");
+      tux_sprite->set_action(moving ? "large-walking" : "large-stop");
       break;
     case FIRE_BONUS:
-      tux_sprite->set_action("fire");
+      tux_sprite->set_action(moving ? "fire-walking" : "fire-stop");
       break;
     case NO_BONUS:
-      tux_sprite->set_action("small");
+      tux_sprite->set_action(moving ? "small-walking" : "small-stop");
       break;
     default:
-#ifdef DBEUG
-      std::cerr << "Bonus type not handled in worldmap.\n";
-#endif
-      tux_sprite->set_action("large");
+      msg_debug("Bonus type not handled in worldmap.");
+      tux_sprite->set_action("large-stop");
       break;
   }
 
@@ -196,150 +198,141 @@ Tux::set_direction(Direction dir)
   input_direction = dir;
 }
 
-void
-Tux::update(float delta)
+void 
+Tux::tryStartWalking() 
+{
+  if (moving) return;
+  if (input_direction == D_NONE) return;
+
+  WorldMap::Level* level = worldmap->at_level();
+
+  // We got a new direction, so lets start walking when possible
+  Vector next_tile;
+  if ((!level || level->solved) && worldmap->path_ok(input_direction, tile_pos, &next_tile))
+  {
+    tile_pos = next_tile;
+    moving = true;
+    direction = input_direction;
+    back_direction = reverse_dir(direction);
+  }
+  else if (input_direction == back_direction)
+  {
+    moving = true;
+    direction = input_direction;
+    tile_pos = worldmap->get_next_tile(tile_pos, direction);
+    back_direction = reverse_dir(direction);
+  }
+
+}
+
+bool 
+Tux::canWalk(const Tile* tile, Direction dir)
+{
+  return ((tile->getData() & Tile::WORLDMAP_NORTH && dir == D_NORTH) ||
+         (tile->getData() & Tile::WORLDMAP_SOUTH && dir == D_SOUTH) ||
+         (tile->getData() & Tile::WORLDMAP_EAST && dir == D_EAST) ||
+         (tile->getData() & Tile::WORLDMAP_WEST && dir == D_WEST));
+}
+
+void 
+Tux::tryContinueWalking(float elapsed_time)
 {
-  // check controller
-  if(main_controller->pressed(Controller::UP))
-    input_direction = D_NORTH;
-  else if(main_controller->pressed(Controller::DOWN))
-    input_direction = D_SOUTH;
-  else if(main_controller->pressed(Controller::LEFT))
-    input_direction = D_WEST;
-  else if(main_controller->pressed(Controller::RIGHT))
-    input_direction = D_EAST;
-
-  if(!moving)
+  if (!moving) return;
+
+  // Let tux walk
+  offset += TUXSPEED * elapsed_time;
+
+  // Do nothing if we have not yet reached the next tile
+  if (offset <= 32) return;
+
+  offset -= 32;
+
+  // if this is a special_tile with passive_message, display it
+  WorldMap::SpecialTile* special_tile = worldmap->at_special_tile();
+  if(special_tile && special_tile->passive_message)
+  {  
+    // direction and the apply_action_ are opposites, since they "see"
+    // directions in a different way
+    if((direction == D_NORTH && special_tile->apply_action_south) ||
+                   (direction == D_SOUTH && special_tile->apply_action_north) ||
+                   (direction == D_WEST && special_tile->apply_action_east) ||
+                   (direction == D_EAST && special_tile->apply_action_west))
     {
-      if (input_direction != D_NONE)
-        { 
-          WorldMap::Level* level = worldmap->at_level();
-
-          // We got a new direction, so lets start walking when possible
-          Vector next_tile;
-          if ((!level || level->solved)
-              && worldmap->path_ok(input_direction, tile_pos, &next_tile))
-            {
-              tile_pos = next_tile;
-              moving = true;
-              direction = input_direction;
-              back_direction = reverse_dir(direction);
-            }
-          else if (input_direction == back_direction)
-            {
-              moving = true;
-              direction = input_direction;
-              tile_pos = worldmap->get_next_tile(tile_pos, direction);
-              back_direction = reverse_dir(direction);
-            }
-        }
+      worldmap->passive_message = special_tile->map_message;
+      worldmap->passive_message_timer.start(map_message_TIME);
+    }
+  }
+
+  // stop if we reached a level, a WORLDMAP_STOP tile or a special tile without a passive_message
+  if ((worldmap->at_level()) || (worldmap->at(tile_pos)->getData() & Tile::WORLDMAP_STOP) || (special_tile && !special_tile->passive_message))
+  {
+    if(special_tile && !special_tile->map_message.empty() && !special_tile->passive_message) worldmap->passive_message_timer.start(0);
+    stop();
+    return;
+  }
+
+  // if user wants to change direction, try changing, else guess the direction in which to walk next
+  const Tile* tile = worldmap->at(tile_pos);
+  if (direction != input_direction)
+  { 
+    if(canWalk(tile, input_direction))
+    {  
+      direction = input_direction;
+      back_direction = reverse_dir(direction);
     }
+  }
   else
+  {
+    Direction dir = D_NONE;
+    if (tile->getData() & Tile::WORLDMAP_NORTH && back_direction != D_NORTH) dir = D_NORTH;
+    else if (tile->getData() & Tile::WORLDMAP_SOUTH && back_direction != D_SOUTH) dir = D_SOUTH;
+    else if (tile->getData() & Tile::WORLDMAP_EAST && back_direction != D_EAST) dir = D_EAST;
+    else if (tile->getData() & Tile::WORLDMAP_WEST && back_direction != D_WEST) dir = D_WEST;
+
+    if (dir == D_NONE) 
     {
-      // Let tux walk
-      offset += TUXSPEED * delta;
-
-      if (offset > 32)
-        { // We reached the next tile, so we check what to do now
-          offset -= 32;
-
-          WorldMap::SpecialTile* special_tile = worldmap->at_special_tile();
-          if(special_tile && special_tile->passive_message)
-            {  // direction and the apply_action_ are opposites, since they "see"
-               // directions in a different way
-            if((direction == D_NORTH && special_tile->apply_action_south) ||
-               (direction == D_SOUTH && special_tile->apply_action_north) ||
-               (direction == D_WEST && special_tile->apply_action_east) ||
-               (direction == D_EAST && special_tile->apply_action_west))
-              {
-              worldmap->passive_message = special_tile->map_message;
-              worldmap->passive_message_timer.start(map_message_TIME);
-              }
-            }
+      // Should never be reached if tiledata is good
+      msg_warning("Could not determine where to walk next");
+      stop();
+      return;
+    }
 
-          if (worldmap->at(tile_pos)->getData() & Tile::WORLDMAP_STOP ||
-             (special_tile && !special_tile->passive_message) ||
-              worldmap->at_level())
-            {
-              if(special_tile && !special_tile->map_message.empty() &&
-                !special_tile->passive_message)
-                worldmap->passive_message_timer.start(0);
-              stop();
-            }
-          else
-            {
-              const Tile* tile = worldmap->at(tile_pos);
-              if (direction != input_direction)
-                { 
-                  // Turn to a new direction
-                  const Tile* tile = worldmap->at(tile_pos);
-
-                  if((tile->getData() & Tile::WORLDMAP_NORTH 
-                      && input_direction == D_NORTH) ||
-                     (tile->getData() & Tile::WORLDMAP_SOUTH
-                      && input_direction == D_SOUTH) ||
-                     (tile->getData() & Tile::WORLDMAP_EAST
-                      && input_direction == D_EAST) ||
-                     (tile->getData() & Tile::WORLDMAP_WEST
-                      && input_direction == D_WEST))
-                    {  // player has changed direction during auto-movement
-                      direction = input_direction;
-                      back_direction = reverse_dir(direction);
-                    }
-                  else
-                    {  // player has changed to impossible tile
-                      back_direction = reverse_dir(direction);
-                      stop();
-                    }
-                }
-              else
-                {
-                Direction dir = D_NONE;
-              
-                if (tile->getData() & Tile::WORLDMAP_NORTH
-                    && back_direction != D_NORTH)
-                  dir = D_NORTH;
-                else if (tile->getData() & Tile::WORLDMAP_SOUTH
-                    && back_direction != D_SOUTH)
-                  dir = D_SOUTH;
-                else if (tile->getData() & Tile::WORLDMAP_EAST
-                    && back_direction != D_EAST)
-                  dir = D_EAST;
-                else if (tile->getData() & Tile::WORLDMAP_WEST
-                    && back_direction != D_WEST)
-                  dir = D_WEST;
-
-                if (dir != D_NONE)
-                  {
-                  direction = dir;
-                  input_direction = direction;
-                  back_direction = reverse_dir(direction);
-                  }
-                else
-                  {
-                  // Should never be reached if tiledata is good
-                  stop();
-                  return;
-                  }
-                }
-
-              // Walk automatically to the next tile
-              if(direction != D_NONE)
-                {
-                Vector next_tile;
-                if (worldmap->path_ok(direction, tile_pos, &next_tile))
-                  {
-                  tile_pos = next_tile;
-                  }
-                else
-                  {
-                  puts("Tilemap data is buggy");
-                  stop();
-                  }
-                }
-            }
-        }
+    direction = dir;
+    input_direction = direction;
+    back_direction = reverse_dir(direction);
+  }
+
+  // Walk automatically to the next tile
+  if(direction != D_NONE)
+  {
+    Vector next_tile;
+    if (worldmap->path_ok(direction, tile_pos, &next_tile))
+    {
+      tile_pos = next_tile;
+    }
+    else
+    {
+      msg_warning("Tilemap data is buggy");
+      stop();
     }
+  }
+}
+
+void
+Tux::updateInputDirection()
+{
+  if(main_controller->hold(Controller::UP)) input_direction = D_NORTH;
+  else if(main_controller->hold(Controller::DOWN)) input_direction = D_SOUTH;
+  else if(main_controller->hold(Controller::LEFT)) input_direction = D_WEST;
+  else if(main_controller->hold(Controller::RIGHT)) input_direction = D_EAST;
+}
+
+
+void
+Tux::update(float elapsed_time)
+{
+  updateInputDirection(); 
+  if (moving) tryContinueWalking(elapsed_time); else tryStartWalking();
 }
 
 //---------------------------------------------------------------------------
@@ -352,17 +345,11 @@ WorldMap::WorldMap()
   tux = new Tux(this);
   add_object(tux);
     
-  leveldot_green
-    = new Surface(datadir + "/images/tiles/worldmap/leveldot_green.png", true);
-  leveldot_red
-    = new Surface(datadir + "/images/tiles/worldmap/leveldot_red.png", true);
-  messagedot
-    = new Surface(datadir + "/images/tiles/worldmap/messagedot.png", true);
-  teleporterdot
-    = new Surface(datadir + "/images/tiles/worldmap/teleporterdot.png", true);
+  messagedot = new Surface("images/worldmap/common/messagedot.png");
+  teleporterdot = sprite_manager->create("images/worldmap/common/teleporter.sprite");
 
   name = "<no title>";
-  music = "salcon.mod";
+  music = "music/salcon.ogg";
   intro_displayed = false;
 
   total_stats.reset();
@@ -375,11 +362,16 @@ WorldMap::~WorldMap()
       i != spawn_points.end(); ++i) {
     delete *i;
   }
-    
+  for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
+    Level& level = *i;
+    delete level.sprite;
+  }
+  for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i) {
+    delete i->sprite;
+  }
+  
   delete tile_manager;
 
-  delete leveldot_green;
-  delete leveldot_red;
   delete messagedot;
   delete teleporterdot;
 }
@@ -415,27 +407,30 @@ WorldMap::load_map()
 
   try {
     lisp::Parser parser;
-    std::string filename = get_resource_filename(map_filename);
-    std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
+    std::auto_ptr<lisp::Lisp> root (parser.parse(map_filename));
 
-    const lisp::Lisp* lisp = root->get_lisp("supertux-worldmap");
+    const lisp::Lisp* lisp = root->get_lisp("supertux-level");
     if(!lisp)
-      throw new std::runtime_error("file isn't a supertux-worldmap file.");
+      throw std::runtime_error("file isn't a supertux-level file.");
 
+    lisp->get("name", name);
+    
+    const lisp::Lisp* sector = lisp->get_lisp("sector");
+    if(!sector)
+      throw std::runtime_error("No sector sepcified in worldmap file.");
+    
     clear_objects();
-    lisp::ListIterator iter(lisp);
+    lisp::ListIterator iter(sector);
     while(iter.next()) {
       if(iter.item() == "tilemap") {
         add_object(new TileMap(*(iter.lisp()), tile_manager));
       } else if(iter.item() == "background") {
         add_object(new Background(*(iter.lisp())));
-      } else if(iter.item() == "properties") {
-        const lisp::Lisp* props = iter.lisp();
-        props->get("name", name);
-        props->get("music", music);
+      } else if(iter.item() == "music") {
+        iter.value()->get(music);
       } else if(iter.item() == "intro-script") {
         iter.value()->get(intro_script);
-      } else if(iter.item() == "spawnpoint") {
+      } else if(iter.item() == "worldmap-spawnpoint") {
         SpawnPoint* sp = new SpawnPoint(iter.lisp());
         spawn_points.push_back(sp);
       } else if(iter.item() == "level") {
@@ -443,7 +438,7 @@ WorldMap::load_map()
       } else if(iter.item() == "special-tile") {
         parse_special_tile(iter.lisp());
       } else {
-        std::cerr << "Unknown token '" << iter.item() << "' in worldmap.\n";
+        msg_warning("Unknown token '" << iter.item() << "' in worldmap");
       }
     }
     if(solids == 0)
@@ -475,6 +470,14 @@ WorldMap::parse_special_tile(const lisp::Lisp* lisp)
   
   lisp->get("x", special_tile.pos.x);
   lisp->get("y", special_tile.pos.y);
+
+  std::string sprite;
+  if (lisp->get("sprite", sprite)) {
+    special_tile.sprite = sprite_manager->create(sprite);
+  } else {
+    special_tile.sprite = 0;
+  }
+
   lisp->get("map-message", special_tile.map_message);
   special_tile.passive_message = false;
   lisp->get("passive-message", special_tile.passive_message);
@@ -521,6 +524,10 @@ WorldMap::parse_level_tile(const lisp::Lisp* level_lisp)
   level.south = true;
   level.west  = true;
 
+  std::string sprite = "images/worldmap/common/leveldot.sprite";
+  level_lisp->get("sprite", sprite);
+  level.sprite = sprite_manager->create(sprite);
+
   level_lisp->get("extro-script", level.extro_script);
   level_lisp->get("next-worldmap", level.next_worldmap);
 
@@ -528,6 +535,17 @@ WorldMap::parse_level_tile(const lisp::Lisp* level_lisp)
   level_lisp->get("quit-worldmap", level.quit_worldmap);
 
   level_lisp->get("name", level.name);
+  
+  if (!PHYSFS_exists((levels_path + level.name).c_str()))
+  {
+       // Do we want to bail out instead...? We might get messages from modders
+       // who can't make their levels run because they're too dumb to watch
+       // their terminals...
+    msg_warning("level file '" << level.name
+      << "' does not exist and will not be added to the worldmap");
+    return;
+  }
+
   level_lisp->get("x", level.pos.x);
   level_lisp->get("y", level.pos.y);
 
@@ -548,8 +566,7 @@ WorldMap::get_level_title(Level& level)
 
   try {
     lisp::Parser parser;
-    std::auto_ptr<lisp::Lisp> root (
-        parser.parse(get_resource_filename(levels_path + level.name)));
+    std::auto_ptr<lisp::Lisp> root (parser.parse(levels_path + level.name));
 
     const lisp::Lisp* level_lisp = root->get_lisp("supertux-level");
     if(!level_lisp)
@@ -557,7 +574,7 @@ WorldMap::get_level_title(Level& level)
     
     level_lisp->get("name", level.title);
   } catch(std::exception& e) {
-    std::cerr << "Problem when reading leveltitle: " << e.what() << "\n";
+    msg_warning("Problem when reading leveltitle: " << e.what());
     return;
   }
 }
@@ -597,7 +614,7 @@ WorldMap::get_input()
       Menu::current()->event(event);
     main_controller->process_event(event);
     if(event.type == SDL_QUIT)
-      throw std::runtime_error("Received window close");
+      throw graceful_shutdown();
   }
 }
 
@@ -719,7 +736,7 @@ WorldMap::update(float delta)
         if (special_tile->teleport_dest != Vector(-1,-1))
           {
           // TODO: an animation, camera scrolling or a fading would be a nice touch
-          sound_manager->play_sound("warp");
+          sound_manager->play("sounds/warp.wav");
           tux->back_direction = D_NONE;
           tux->set_tile_pos(special_tile->teleport_dest);
           SDL_Delay(1000);
@@ -731,21 +748,22 @@ WorldMap::update(float delta)
       Level* level = at_level();
       if (!level)
         {
-        std::cout << "No level to enter at: "
-          << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y
-          << std::endl;
+        msg_warning("No level to enter at: "
+          << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y);
         return;
         }
 
 
       if (level->pos == tux->get_tile_pos())
         {
-          PlayerStatus old_player_status = player_status;
+          sound_manager->stop_music();
+          PlayerStatus old_player_status;
+          old_player_status = *player_status;
 
           // do a shriking fade to the level
           shrink_fade(Vector((level->pos.x*32 + 16 + offset.x),
                              (level->pos.y*32 + 16 + offset.y)), 500);
-          GameSession session(get_resource_filename(levels_path + level->name),
+          GameSession session(levels_path + level->name,
                               ST_GL_LOAD_LEVEL_FILE, &level->statistics);
 
           switch (session.run())
@@ -755,6 +773,7 @@ WorldMap::update(float delta)
                 level_finished = true;
                 bool old_level_state = level->solved;
                 level->solved = true;
+                level->sprite->set_action("solved");
 
                 // deal with statistics
                 level->statistics.merge(global_stats);
@@ -792,9 +811,9 @@ WorldMap::update(float delta)
               level_finished = false;
               /* In case the player's abort the level, keep it using the old
                   status. But the minimum lives and no bonus. */
-              player_status.coins = old_player_status.coins;
-              player_status.lives = std::min(old_player_status.lives, player_status.lives);
-              player_status.bonus = NO_BONUS;
+              player_status->coins = old_player_status.coins;
+              player_status->lives = std::min(old_player_status.lives, player_status->lives);
+              player_status->bonus = NO_BONUS;
 
               break;
             case GameSession::ES_GAME_OVER:
@@ -813,7 +832,7 @@ WorldMap::update(float delta)
               context.draw_text(blue_text, _("GAMEOVER"), 
                   Vector(SCREEN_WIDTH/2, 200), CENTER_ALLIGN, LAYER_FOREGROUND1);
 
-              sprintf(str, _("COINS: %d"), player_status.coins);
+              sprintf(str, _("COINS: %d"), player_status->coins);
               context.draw_text(gold_text, str,
                   Vector(SCREEN_WIDTH/2, SCREEN_WIDTH - 32), CENTER_ALLIGN,
                   LAYER_FOREGROUND1);
@@ -825,7 +844,7 @@ WorldMap::update(float delta)
               wait_for_event(2.0, 6.0);
 
               quit = true;
-              player_status.reset();
+              player_status->reset();
               break;
               }
             case GameSession::ES_NONE:
@@ -834,7 +853,7 @@ WorldMap::update(float delta)
               break;
             }
 
-          sound_manager->play_music(song);
+          sound_manager->play_music(music);
           Menu::set_current(0);
           if (!savegame_file.empty())
             savegame(savegame_file);
@@ -847,11 +866,10 @@ WorldMap::update(float delta)
             std::auto_ptr<ScriptInterpreter> interpreter 
               (new ScriptInterpreter(levels_path));
             std::istringstream in(level->extro_script);
-            interpreter->load_script(in, "level-extro-script");
-            interpreter->start_script();
+            interpreter->run_script(in, "level-extro-script");
             add_object(interpreter.release());
           } catch(std::exception& e) {
-            std::cerr << "Couldn't run level-extro-script:" << e.what() << "\n";
+            msg_warning("Couldn't run level-extro-script:" << e.what());
           }
         }
 
@@ -911,12 +929,8 @@ WorldMap::draw(DrawingContext& context)
   
   for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
     {
-      if (i->solved)
-        context.draw_surface(leveldot_green,
-            Vector(i->pos.x*32, i->pos.y*32), LAYER_TILES+1);
-      else
-        context.draw_surface(leveldot_red,
-            Vector(i->pos.x*32, i->pos.y*32), LAYER_TILES+1);
+      const Level& level = *i;
+      level.sprite->draw(context, level.pos*32 + Vector(16, 16), LAYER_TILES+1);
     }
 
   for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
@@ -924,9 +938,11 @@ WorldMap::draw(DrawingContext& context)
       if(i->invisible)
         continue;
 
-      if (i->teleport_dest != Vector(-1, -1))
-        context.draw_surface(teleporterdot,
-                Vector(i->pos.x*32, i->pos.y*32), LAYER_TILES+1);
+      if (i->sprite)
+       i->sprite->draw(context, i->pos*32 + Vector(16, 16), LAYER_TILES+1);
+
+      else if (i->teleport_dest != Vector(-1, -1))
+       teleporterdot->draw(context, i->pos*32 + Vector(16, 16), LAYER_TILES+1);
 
       else if (!i->map_message.empty() && !i->passive_message)
         context.draw_surface(messagedot,
@@ -942,7 +958,7 @@ WorldMap::draw_status(DrawingContext& context)
   context.push_transform();
   context.set_translation(Vector(0, 0));
  
-  player_status.draw(context);
+  player_status->draw(context);
 
   if (!tux->is_moving())
     {
@@ -977,7 +993,7 @@ WorldMap::draw_status(DrawingContext& context)
         }
     }
   /* Display a passive message in the map, if needed */
-  if(passive_message_timer.check())
+  if(passive_message_timer.started())
     context.draw_text(gold_text, passive_message, 
             Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - white_text->get_height() - 60),
             CENTER_ALLIGN, LAYER_FOREGROUND1);
@@ -992,20 +1008,18 @@ WorldMap::display()
 
   quit = false;
 
-  song = sound_manager->load_music(datadir +  "/music/" + music);
-  sound_manager->play_music(song);
+  sound_manager->play_music(music);
 
   if(!intro_displayed && intro_script != "") {
     try {
       std::auto_ptr<ScriptInterpreter> interpreter 
         (new ScriptInterpreter(levels_path));
       std::istringstream in(intro_script);
-      interpreter->load_script(in, "worldmap-intro-script");
-      interpreter->start_script();
+      interpreter->run_script(in, "worldmap-intro-script");
       add_object(interpreter.release());
     } catch(std::exception& e) {
-      std::cerr << "Couldn't execute worldmap-intro-script: "
-        << e.what() << "\n";
+      msg_warning("Couldn't execute worldmap-intro-script: "
+        << e.what());
     }
                                            
     intro_displayed = true;
@@ -1016,7 +1030,7 @@ WorldMap::display()
   while(!quit) {
     Uint32 ticks = SDL_GetTicks();
     float elapsed_time = float(ticks - lastticks) / 1000;
-    global_time += elapsed_time;
+    game_time += elapsed_time;
     lastticks = ticks;
     
     // 40 fps minimum // TODO use same code as in GameSession here
@@ -1043,6 +1057,7 @@ WorldMap::display()
     context.pop_transform();
     get_input();
     update(elapsed_time);
+    sound_manager->update();
       
     if(Menu::current()) {
       Menu::current()->draw(context);
@@ -1058,8 +1073,20 @@ WorldMap::savegame(const std::string& filename)
   if(filename == "")
     return;
 
-  std::ofstream file(filename.c_str(), std::ios::out);
-  lisp::Writer writer(file);
+  std::string dir = FileSystem::dirname(filename);
+  if(PHYSFS_exists(dir.c_str()) == 0 && PHYSFS_mkdir(dir.c_str()) != 0) {
+    std::ostringstream msg;
+    msg << "Couldn't create directory '" << dir << "' for savegame:"
+        << PHYSFS_getLastError();
+    throw std::runtime_error(msg.str());
+  }
+  if(!PHYSFS_isDirectory(dir.c_str())) {
+    std::ostringstream msg;
+    msg << "'" << dir << "' is not a directory.";
+    throw std::runtime_error(msg.str());
+  }
+  
+  lisp::Writer writer(filename);
 
   int nb_solved_levels = 0, total_levels = 0;
   for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
@@ -1086,7 +1113,7 @@ WorldMap::savegame(const std::string& filename)
   writer.write_float("x", tux->get_tile_pos().x);
   writer.write_float("y", tux->get_tile_pos().y);
   writer.write_string("back", direction_to_string(tux->back_direction));
-  player_status.write(writer);
+  player_status->write(writer);
   writer.write_string("back", direction_to_string(tux->back_direction));
 
   writer.end_list("tux");
@@ -1115,76 +1142,85 @@ WorldMap::savegame(const std::string& filename)
 void
 WorldMap::loadgame(const std::string& filename)
 {
-  std::cout << "loadgame: " << filename << std::endl;
+  msg_debug("loadgame: " << filename);
   savegame_file = filename;
-
-  try {
-    lisp::Parser parser;
-    std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
   
-    const lisp::Lisp* savegame = root->get_lisp("supertux-savegame");
-    if(!savegame)
-      throw std::runtime_error("File is not a supertux-savegame file.");
-
-    /* Get the Map filename and then load it before setting level settings */
-    std::string cur_map_filename = map_filename;
-    savegame->get("map", map_filename);
-    load_map(); 
-
-    savegame->get("intro-displayed", intro_displayed);
-    savegame->get("lives", player_status.lives);
-    savegame->get("coins", player_status.coins);
-    savegame->get("max-score-multiplier", player_status.max_score_multiplier);
-    if (player_status.lives < 0)
-      player_status.reset();
-
-    const lisp::Lisp* tux_lisp = savegame->get_lisp("tux");
-    if(tux)
-    {
-      Vector p;
-      std::string back_str = "none";
+  if (PHYSFS_exists(filename.c_str())) // savegame exists
+  {
+    try {
+      lisp::Parser parser;
+      
+      std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
+    
+      const lisp::Lisp* savegame = root->get_lisp("supertux-savegame");
+      if(!savegame)
+        throw std::runtime_error("File is not a supertux-savegame file.");
+
+      /* Get the Map filename and then load it before setting level settings */
+      std::string cur_map_filename = map_filename;
+      savegame->get("map", map_filename);
+      load_map(); 
+
+      savegame->get("intro-displayed", intro_displayed);
+      savegame->get("lives", player_status->lives);
+      savegame->get("coins", player_status->coins);
+      savegame->get("max-score-multiplier", player_status->max_score_multiplier);
+      if (player_status->lives < 0)
+      player_status->reset();
+
+      const lisp::Lisp* tux_lisp = savegame->get_lisp("tux");
+      if(tux)
+      {
+        Vector p;
+        std::string back_str = "none";
 
-      tux_lisp->get("x", p.x);
-      tux_lisp->get("y", p.y);
-      tux_lisp->get("back", back_str);
-      player_status.read(*tux_lisp);
+        tux_lisp->get("x", p.x);
+        tux_lisp->get("y", p.y);
+        tux_lisp->get("back", back_str);
+          player_status->read(*tux_lisp);
       
-      tux->back_direction = string_to_direction(back_str);      
-      tux->set_tile_pos(p);
-    }
+        tux->back_direction = string_to_direction(back_str);      
+        tux->set_tile_pos(p);
+      }
 
-    const lisp::Lisp* levels_lisp = savegame->get_lisp("levels");
-    if(levels_lisp) {
-      lisp::ListIterator iter(levels_lisp);
-      while(iter.next()) {
-        if(iter.item() == "level") {
-          std::string name;
-          bool solved = false;
+      const lisp::Lisp* levels_lisp = savegame->get_lisp("levels");
+      if(levels_lisp) {
+        lisp::ListIterator iter(levels_lisp);
+        while(iter.next()) {
+          if(iter.item() == "level") {
+            std::string name;
+            bool solved = false;
 
-          const lisp::Lisp* level = iter.lisp();
-          level->get("name", name);
-          level->get("solved", solved);
+            const lisp::Lisp* level = iter.lisp();
+            level->get("name", name);
+            level->get("solved", solved);
 
-          for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
-          {
-            if (name == i->name)
+            for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
             {
-              i->solved = solved;
-              i->statistics.parse(*level);
-              break;
+              if (name == i->name)
+              {
+                i->solved = solved;
+                i->sprite->set_action(solved ? "solved" : "default");
+                i->statistics.parse(*level);
+                break;
+              }
             }
+          } else {
+            msg_warning("Unknown token '" << iter.item() 
+                      << "' in levels block in worldmap");
           }
-        } else {
-          std::cerr << "Unknown token '" << iter.item() 
-                    << "' in levels block in worldmap.\n";
         }
       }
+    } catch(std::exception& e) {
+      msg_warning("Problem loading game '" << filename << "': " << e.what());
+      load_map();
+      player_status->reset();
     }
-  } catch(std::exception& e) {
-    std::cerr << "Problem loading game '" << filename << "': " << e.what() 
-              << "\n";
-    load_map();
-    player_status.reset();
+  }
+  else
+  {
+       load_map();
+    player_status->reset();
   }
 
   calculate_total_stats();