hopefully fixed the crash on exit, keep sectors script bundled in the sector and...
[supertux.git] / src / worldmap.cpp
index 655710d..209ea14 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
 //  You should have received a copy of the GNU General Public License
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
-
 #include <config.h>
 
 #include <iostream>
 #include <fstream>
 #include <vector>
 #include <cassert>
+#include <stdexcept>
+#include <sstream>
 #include <unistd.h>
-
-#include "app/globals.h"
-#include "video/surface.h"
-#include "video/screen.h"
-#include "video/drawing_context.h"
-#include "utils/lispreader.h"
-#include "utils/lispwriter.h"
-#include "special/frame_rate.h"
-#include "gameloop.h"
-#include "app/setup.h"
-#include "sector.h"
-#include "worldmap.h"
-#include "audio/sound_manager.h"
-#include "resources.h"
-#include "app/gettext.h"
-#include "misc.h"
-#include "scene.h"
-
-#define map_message_TIME 2.8
+#include <physfs.h>
+
+#include "gettext.hpp"
+#include "msg.hpp"
+#include "mainloop.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 "msg.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"
 
 Menu* worldmap_menu  = 0;
 
+static const float TUXSPEED = 200;
+static const float map_message_TIME = 2.8;
+
 namespace WorldMapNS {
 
+WorldMap* WorldMap::current_ = NULL;
+
 Direction reverse_dir(Direction direction)
 {
   switch(direction)
@@ -99,167 +117,44 @@ string_to_direction(const std::string& directory)
     return D_NONE;
 }
 
-TileManager::TileManager()
-{
-  std::string stwt_filename = datadir +  "/images/worldmap/antarctica.stwt";
-  lisp_object_t* root_obj = lisp_read_from_file(stwt_filename);
-  if (!root_obj)
-    Termination::abort("Couldn't load file", stwt_filename);
-
-  if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap-tiles") == 0)
-    {
-      lisp_object_t* cur = lisp_cdr(root_obj);
-
-      while(!lisp_nil_p(cur))
-        {
-          lisp_object_t* element = lisp_car(cur);
-
-          if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0)
-            {
-              int id = 0;
-
-              Tile* tile = new Tile;             
-              tile->north = tile->east = tile->south = tile->west = true;
-              tile->stop  = true;
-              tile->auto_walk = false;
-
-              LispReader reader(lisp_cdr(element));
-              reader.read_int("id", id);
-
-              std::string temp;
-              reader.read_string("possible-directions", temp);
-              if(!temp.empty())
-                {
-                tile->north = tile->east = tile->south = tile->west = false;
-                if(temp.find("north") != std::string::npos)
-                  tile->north = true;
-                if(temp.find("south") != std::string::npos)
-                  tile->south = true;
-                if(temp.find("east") != std::string::npos)
-                  tile->east = true;
-                if(temp.find("west") != std::string::npos)
-                  tile->west = true;
-                }
-
-              /* For backward compatibility */
-              reader.read_bool("north", tile->north);
-              reader.read_bool("south", tile->south);
-              reader.read_bool("west",  tile->west);
-              reader.read_bool("east",  tile->east);
-
-              reader.read_bool("stop",  tile->stop);
-              reader.read_bool("auto-walk",  tile->auto_walk);
-
-              reader.read_string("one-way", temp);
-              tile->one_way = BOTH_WAYS;
-              if(!temp.empty())
-                {
-                if(temp == "north-south")
-                  tile->one_way = NORTH_SOUTH_WAY;
-                else if(temp == "south-north")
-                  tile->one_way = SOUTH_NORTH_WAY;
-                else if(temp == "east-west")
-                  tile->one_way = EAST_WEST_WAY;
-                else if(temp == "west-east")
-                  tile->one_way = WEST_EAST_WAY;
-                }
-
-              std::vector<std::string> filenames;
-              reader.read_string_vector("image", filenames);
-
-              if(filenames.size() == 0)
-                std::cerr << "Warning: no image specified for tile " << id
-                          << ".\nIgnoring...\n" << std::endl;
-
-              for(int i = 0; static_cast<unsigned int>(i) < filenames.size(); i++)
-                {
-                Surface* image = new Surface(
-                         datadir +  "/images/worldmap/" + filenames[i], true);
-                tile->images.push_back(image);
-                }
-
-              tile->anim_speed = 25;
-              reader.read_int("anim-speed", tile->anim_speed);
-
-
-              if (id >= int(tiles.size()))
-                tiles.resize(id+1);
-
-              tiles[id] = tile;
-            }
-          else
-            {
-              puts("Unhandled symbol");
-            }
-
-          cur = lisp_cdr(cur);
-        }
-    }
-  else
-    {
-      assert(0);
-    }
-
-  lisp_free(root_obj);
-}
-
-TileManager::~TileManager()
-{
-  for(std::vector<Tile*>::iterator i = tiles.begin(); i != tiles.end(); ++i)
-    delete *i;
-}
-
-Tile*
-TileManager::get(int i)
-{
-  assert(i >=0 && i < int(tiles.size()));
-  return tiles[i];
-}
-
 //---------------------------------------------------------------------------
 
 Tux::Tux(WorldMap* worldmap_)
   : worldmap(worldmap_)
 {
-  largetux_sprite = new Surface(datadir +  "/images/worldmap/tux.png", true);
-  firetux_sprite = new Surface(datadir +  "/images/worldmap/firetux.png", true);
-  smalltux_sprite = new Surface(datadir +  "/images/worldmap/smalltux.png", true);
-
+  tux_sprite = sprite_manager->create("images/worldmap/common/tux.sprite");
+  
   offset = 0;
   moving = false;
-  tile_pos.x = worldmap->get_start_x();
-  tile_pos.y = worldmap->get_start_y();
   direction = D_NONE;
   input_direction = D_NONE;
 }
 
 Tux::~Tux()
 {
-  delete smalltux_sprite;
-  delete firetux_sprite;
-  delete largetux_sprite;
+  delete tux_sprite;
 }
 
 void
-Tux::draw(DrawingContext& context, const Vector& offset)
+Tux::draw(DrawingContext& context)
 {
-  Vector pos = get_pos();
-  switch (player_status.bonus)
-    {
-    case PlayerStatus::GROWUP_BONUS:
-      context.draw_surface(largetux_sprite,
-          Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
+  switch (player_status->bonus) {
+    case GROWUP_BONUS:
+      tux_sprite->set_action(moving ? "large-walking" : "large-stop");
       break;
-    case PlayerStatus::FLOWER_BONUS:
-      context.draw_surface(firetux_sprite,
-          Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
+    case FIRE_BONUS:
+      tux_sprite->set_action(moving ? "fire-walking" : "fire-stop");
       break;
-    case PlayerStatus::NO_BONUS:
-      context.draw_surface(smalltux_sprite,
-          Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
+    case NO_BONUS:
+      tux_sprite->set_action(moving ? "small-walking" : "small-stop");
       break;
-    }
+    default:
+      msg_debug << "Bonus type not handled in worldmap." << std::endl;
+      tux_sprite->set_action("large-stop");
+      break;
+  }
+
+  tux_sprite->draw(context, get_pos(), LAYER_OBJECTS);
 }
 
 
@@ -287,7 +182,7 @@ Tux::get_pos()
       break;
     }
   
-  return Vector((int)x, (int)y); 
+  return Vector(x, y);
 }
 
 void
@@ -302,356 +197,389 @@ Tux::stop()
 void
 Tux::set_direction(Direction dir)
 {
-input_direction = dir;
+  input_direction = dir;
 }
 
-void
-Tux::action(float delta)
+void 
+Tux::tryStartWalking() 
 {
-  if (!moving)
-    {
-      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);
-            }
-        }
-    }
-  else
-    {
-      // Let tux walk a few pixels (20 pixel/sec)
-      offset += 20.0f * 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);
-              }
-            }
-
-          if (worldmap->at(tile_pos)->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
-            {
-              if (worldmap->at(tile_pos)->auto_walk || direction != input_direction)
-                { // Turn to a new direction
-                  Tile* tile = worldmap->at(tile_pos);
-
-                  if(direction != input_direction && 
-                     ((tile->north && input_direction == D_NORTH) ||
-                     (tile->south && input_direction == D_SOUTH) ||
-                     (tile->east && input_direction == D_EAST) ||
-                     (tile->west && input_direction == D_WEST)))
-                    {  // player has changed direction during auto-movement
-                    direction = input_direction;
-                    back_direction = reverse_dir(direction);
-                    }
-                  else if(direction != input_direction)
-                    {  // player has changed to impossible tile
-                      back_direction = reverse_dir(direction);
-                      stop();
-                    }
-                  else
-                    {
-                    Direction dir = D_NONE;
-                  
-                    if (tile->north && back_direction != D_NORTH)
-                      dir = D_NORTH;
-                    else if (tile->south && back_direction != D_SOUTH)
-                      dir = D_SOUTH;
-                    else if (tile->east && back_direction != D_EAST)
-                      dir = D_EAST;
-                    else if (tile->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();
-                  }
-                }
-            }
-        }
-    }
-}
+  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);
+  }
 
-//---------------------------------------------------------------------------
-Tile::Tile()
-{
 }
 
-Tile::~Tile()
+bool 
+Tux::canWalk(const Tile* tile, Direction dir)
 {
-  for(std::vector<Surface*>::iterator i = images.begin(); i != images.end(); i++)
-    delete *i;
+  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
-Tile::draw(DrawingContext& context, Vector pos)
+void 
+Tux::tryContinueWalking(float elapsed_time)
 {
-  // same code as from tile_manager.cpp draw_tile()
+  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))
+    {
+      worldmap->passive_message = special_tile->map_message;
+      worldmap->passive_message_timer.start(map_message_TIME);
+    }
+  }
 
-  if(!images.size())
+  // 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(images.size() > 1)
+  // 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) 
     {
-    size_t frame 
-      = ((global_frame_counter*25) / anim_speed) % images.size();
+      // Should never be reached if tiledata is good
+      msg_warning << "Could not determine where to walk next" << std::endl;
+      stop();
+      return;
+    }
+
+    direction = dir;
+    input_direction = direction;
+    back_direction = reverse_dir(direction);
+  }
 
-    context.draw_surface(images[frame], pos, LAYER_TILES);
+  // 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 if (images.size() == 1)
+    else
     {
-    context.draw_surface(images[0], pos, LAYER_TILES);
+      msg_warning << "Tilemap data is buggy" << std::endl;
+      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();
 }
 
 //---------------------------------------------------------------------------
 
 WorldMap::WorldMap()
+  : tux(0), solids(0)
 {
-  tile_manager = new TileManager();
-  //tux = new Tux(this);
+  tile_manager = new TileManager("images/worldmap.strf");
   
-  width  = 20;
-  height = 15;
-  
-  start_x = 4;
-  start_y = 5;
-
   tux = new Tux(this);
-  
-  leveldot_green = new Surface(datadir +  "/images/worldmap/leveldot_green.png", true);
-  leveldot_red = new Surface(datadir +  "/images/worldmap/leveldot_red.png", true);
-  messagedot   = new Surface(datadir +  "/images/worldmap/messagedot.png", true);
-  teleporterdot   = new Surface(datadir +  "/images/worldmap/teleporterdot.png", true);
-
-  enter_level = false;
+  add_object(tux);
+    
+  messagedot = new Surface("images/worldmap/common/messagedot.png");
+  teleporterdot = sprite_manager->create("images/worldmap/common/teleporter.sprite");
 
   name = "<no title>";
-  music = "salcon.mod";
-
-  global_frame_counter = 0;
+  music = "music/salcon.ogg";
+  intro_displayed = false;
 
   total_stats.reset();
 }
 
 WorldMap::~WorldMap()
 {
-  delete tux;
+  clear_objects();
+  for(SpawnPoints::iterator i = spawn_points.begin();
+      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;
 }
 
+void
+WorldMap::add_object(GameObject* object)
+{
+  TileMap* tilemap = dynamic_cast<TileMap*> (object);
+  if(tilemap != 0 && tilemap->is_solid()) {
+    solids = tilemap;
+  }
+
+  game_objects.push_back(object);
+}
+
+void
+WorldMap::clear_objects()
+{
+  for(GameObjects::iterator i = game_objects.begin();
+      i != game_objects.end(); ++i)
+    delete *i;
+  game_objects.clear();
+  solids = 0;
+  tux = new Tux(this);
+  add_object(tux);
+}
+
 // Don't forget to set map_filename before calling this
 void
 WorldMap::load_map()
 {
-  lisp_object_t* root_obj = lisp_read_from_file(datadir + "/levels/worldmap/" + map_filename);
-  if (!root_obj)
-    Termination::abort("Couldn't load file", datadir + "/levels/worldmap/" + map_filename);
+  levels_path = FileSystem::dirname(map_filename);
+
+  try {
+    lisp::Parser parser;
+    std::auto_ptr<lisp::Lisp> root (parser.parse(map_filename));
+
+    const lisp::Lisp* lisp = root->get_lisp("supertux-level");
+    if(!lisp)
+      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(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() == "music") {
+        iter.value()->get(music);
+      } else if(iter.item() == "intro-script") {
+        iter.value()->get(intro_script);
+      } else if(iter.item() == "worldmap-spawnpoint") {
+        SpawnPoint* sp = new SpawnPoint(iter.lisp());
+        spawn_points.push_back(sp);
+      } else if(iter.item() == "level") {
+        parse_level_tile(iter.lisp());
+      } else if(iter.item() == "special-tile") {
+        parse_special_tile(iter.lisp());
+      } else if(iter.item() == "name") {
+        // skip
+      } else {
+        msg_warning << "Unknown token '" << iter.item() << "' in worldmap" << std::endl;
+      }
+    }
+    if(solids == 0)
+      throw std::runtime_error("No solid tilemap specified");
+
+    // search for main spawnpoint
+    for(SpawnPoints::iterator i = spawn_points.begin();
+        i != spawn_points.end(); ++i) {
+      SpawnPoint* sp = *i;
+      if(sp->name == "main") {
+        Vector p = sp->pos;
+        tux->set_tile_pos(p);
+        break;
+      }
+    }
 
-  if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap") == 0)
-    {
-      lisp_object_t* cur = lisp_cdr(root_obj);
+  } catch(std::exception& e) {
+    std::stringstream msg;
+    msg << "Problem when parsing worldmap '" << map_filename << "': " <<
+      e.what();
+    throw std::runtime_error(msg.str());
+  }
+}
 
-      while(!lisp_nil_p(cur))
-        {
-          lisp_object_t* element = lisp_car(cur);
+void
+WorldMap::parse_special_tile(const lisp::Lisp* lisp)
+{
+  SpecialTile special_tile;
+  
+  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;
+  }
 
-          if (strcmp(lisp_symbol(lisp_car(element)), "tilemap") == 0)
-            {
-              LispReader reader(lisp_cdr(element));
-              reader.read_int("width",  width);
-              reader.read_int("height", height);
-              reader.read_int_vector("data", tilemap);
-            }
-          else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0)
-            {
-              LispReader reader(lisp_cdr(element));
-              reader.read_string("name", name, true);
-              reader.read_string("music", music);
-             reader.read_int("start_pos_x", start_x);
-             reader.read_int("start_pos_y", start_y);
-            }
-          else if (strcmp(lisp_symbol(lisp_car(element)), "special-tiles") == 0 ||
-                   strcmp(lisp_symbol(lisp_car(element)), "levels") == 0)
-            {
-              lisp_object_t* cur = lisp_cdr(element);
-              
-              while(!lisp_nil_p(cur))
-                {
-                  lisp_object_t* element = lisp_car(cur);
-
-                  if (strcmp(lisp_symbol(lisp_car(element)), "special-tile") == 0)
-                    {
-                      SpecialTile special_tile;
-                      LispReader reader(lisp_cdr(element));
-                      
-                      reader.read_float("x", special_tile.pos.x);
-                      reader.read_float("y", special_tile.pos.y);
-
-                      special_tile.map_message.erase();
-                      reader.read_string("map-message", special_tile.map_message);
-                      special_tile.passive_message = false;
-                      reader.read_bool("passive-message", special_tile.passive_message);
-
-                      special_tile.teleport_dest = Vector(-1,-1);
-                      reader.read_float("teleport-to-x", special_tile.teleport_dest.x);
-                      reader.read_float("teleport-to-y", special_tile.teleport_dest.y);
-
-                      special_tile.invisible = false;
-                      reader.read_bool("invisible-tile", special_tile.invisible);
-
-                      special_tile.apply_action_north = special_tile.apply_action_south =
-                          special_tile.apply_action_east = special_tile.apply_action_west =
-                          true;
-
-                      std::string apply_direction;
-                      reader.read_string("apply-to-direction", apply_direction);
-                      if(!apply_direction.empty())
-                        {
-                        special_tile.apply_action_north = special_tile.apply_action_south =
-                            special_tile.apply_action_east = special_tile.apply_action_west =
-                            false;
-                        if(apply_direction.find("north") != std::string::npos)
-                          special_tile.apply_action_north = true;
-                        if(apply_direction.find("south") != std::string::npos)
-                          special_tile.apply_action_south = true;
-                        if(apply_direction.find("east") != std::string::npos)
-                          special_tile.apply_action_east = true;
-                        if(apply_direction.find("west") != std::string::npos)
-                          special_tile.apply_action_west = true;
-                        }
-
-                      special_tiles.push_back(special_tile);
-                    }
-
-                  else if (strcmp(lisp_symbol(lisp_car(element)), "level") == 0)
-                    {
-                      Level level;
-                      LispReader reader(lisp_cdr(element));
-                      level.solved = false;
-                      
-                      level.north = true;
-                      level.east  = true;
-                      level.south = true;
-                      level.west  = true;
-
-                      reader.read_string("extro-filename", level.extro_filename);
-                      reader.read_string("next-worldmap", level.next_worldmap);
-
-                      level.quit_worldmap = false;
-                      reader.read_bool("quit-worldmap", level.quit_worldmap);
-
-                      reader.read_string("name", level.name, true);
-                      reader.read_float("x", level.pos.x);
-                      reader.read_float("y", level.pos.y);
-
-                      level.auto_path = true;
-                      reader.read_bool("auto-path", level.auto_path);
-
-                      level.vertical_flip = false;
-                      reader.read_bool("vertical-flip", level.vertical_flip);
-
-                      levels.push_back(level);
-                    }
+  lisp->get("map-message", special_tile.map_message);
+  special_tile.passive_message = false;
+  lisp->get("passive-message", special_tile.passive_message);
+  special_tile.teleport_dest = Vector(-1,-1);
+  lisp->get("teleport-to-x", special_tile.teleport_dest.x);
+  lisp->get("teleport-to-y", special_tile.teleport_dest.y);
+  special_tile.invisible = false;
+  lisp->get("invisible-tile", special_tile.invisible);
+
+  special_tile.apply_action_north = true;
+  special_tile.apply_action_south = true;
+  special_tile.apply_action_east = true;
+  special_tile.apply_action_west = true;
+
+  std::string apply_direction;
+  lisp->get("apply-to-direction", apply_direction);
+  if(!apply_direction.empty()) {
+    special_tile.apply_action_north = false;
+    special_tile.apply_action_south = false;
+    special_tile.apply_action_east = false;
+    special_tile.apply_action_west = false;
+    if(apply_direction.find("north") != std::string::npos)
+      special_tile.apply_action_north = true;
+    if(apply_direction.find("south") != std::string::npos)
+      special_tile.apply_action_south = true;
+    if(apply_direction.find("east") != std::string::npos)
+      special_tile.apply_action_east = true;
+    if(apply_direction.find("west") != std::string::npos)
+      special_tile.apply_action_west = true;
+  }
+  
+  special_tiles.push_back(special_tile);
+}
+
+void
+WorldMap::parse_level_tile(const lisp::Lisp* level_lisp)
+{
+  Level level;
+
+  level.solved = false;
                   
-                  cur = lisp_cdr(cur);      
-                }
-            }
-          else
-            {
-              
-            }
-          
-          cur = lisp_cdr(cur);
-        }
-    }
+  level.north = true;
+  level.east  = true;
+  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);
 
-    lisp_free(root_obj);
+  level_lisp->get("extro-script", level.extro_script);
+  level_lisp->get("next-worldmap", level.next_worldmap);
+
+  level.quit_worldmap = false;
+  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" << std::endl;
+    return;
+  }
 
-    delete tux;
-    tux = new Tux(this);
+  level_lisp->get("x", level.pos.x);
+  level_lisp->get("y", level.pos.y);
+
+  level.auto_path = true;
+  level_lisp->get("auto-path", level.auto_path);
+
+  level.vertical_flip = false;
+  level_lisp->get("vertical-flip", level.vertical_flip);
+
+  levels.push_back(level);
 }
 
-void WorldMap::get_level_title(Level& level)
+void
+WorldMap::get_level_title(Level& level)
 {
   /** get special_tile's title */
   level.title = "<no title>";
 
-  LispReader* reader = LispReader::load(datadir + "/levels/" + level.name, "supertux-level");
-  if(!reader)
-    {
-    std::cerr << "Error: Could not open level file. Ignoring...\n";
-    return;
-    }
+  try {
+    lisp::Parser parser;
+    std::auto_ptr<lisp::Lisp> root (parser.parse(levels_path + level.name));
 
-  reader->read_string("name", level.title, true);
-  delete reader;
+    const lisp::Lisp* level_lisp = root->get_lisp("supertux-level");
+    if(!level_lisp)
+      return;
+    
+    level_lisp->get("name", level.title);
+  } catch(std::exception& e) {
+    msg_warning << "Problem when reading leveltitle: " << e.what() << std::endl;
+    return;
+  }
 }
 
 void WorldMap::calculate_total_stats()
@@ -670,103 +598,18 @@ void
 WorldMap::on_escape_press()
 {
   // Show or hide the menu
-  if(!Menu::current())
-    {
-    Menu::set_current(worldmap_menu); 
+  if(!Menu::current()) {
+    Menu::set_current(worldmap_menu);
     tux->set_direction(D_NONE);  // stop tux movement when menu is called
-    }
-  else
-    Menu::set_current(0); 
-}
-
-void
-WorldMap::get_input()
-{
-  enter_level = false;
-  SDLKey key;
-
-  SDL_Event event;
-  while (SDL_PollEvent(&event))
-    {
-      if (Menu::current())
-        {
-          Menu::current()->event(event);
-        }
-      else
-        {
-          switch(event.type)
-            {
-            case SDL_QUIT:
-              Termination::abort("Received window close", "");
-              break;
-          
-            case SDL_KEYDOWN:
-              key = event.key.keysym.sym;
-
-              if(key == SDLK_ESCAPE)
-                on_escape_press();
-              else if(key == SDLK_RETURN || key == keymap.power)
-                enter_level = true;
-              else if(key == SDLK_LEFT || key == keymap.power)
-                tux->set_direction(D_WEST);
-              else if(key == SDLK_RIGHT || key == keymap.right)
-                tux->set_direction(D_EAST);
-              else if(key == SDLK_UP || key == keymap.up ||
-                key == keymap.jump)
-                  // there might be ppl that use jump as up key
-                tux->set_direction(D_NORTH);
-              else if(key == SDLK_DOWN || key == keymap.down)
-                tux->set_direction(D_SOUTH);
-              break;
-
-            case SDL_JOYHATMOTION:
-              if(event.jhat.value & SDL_HAT_UP) {
-                tux->set_direction(D_NORTH);
-              } else if(event.jhat.value & SDL_HAT_DOWN) {
-                tux->set_direction(D_SOUTH);
-              } else if(event.jhat.value & SDL_HAT_LEFT) {
-                tux->set_direction(D_WEST);
-              } else if(event.jhat.value & SDL_HAT_RIGHT) {
-                tux->set_direction(D_EAST);
-              }
-              break;
-          
-            case SDL_JOYAXISMOTION:
-              if (event.jaxis.axis == joystick_keymap.x_axis)
-                {
-                  if (event.jaxis.value < -joystick_keymap.dead_zone)
-                    tux->set_direction(D_WEST);
-                  else if (event.jaxis.value > joystick_keymap.dead_zone)
-                    tux->set_direction(D_EAST);
-                }
-              else if (event.jaxis.axis == joystick_keymap.y_axis)
-                {
-                  if (event.jaxis.value > joystick_keymap.dead_zone)
-                    tux->set_direction(D_SOUTH);
-                  else if (event.jaxis.value < -joystick_keymap.dead_zone)
-                    tux->set_direction(D_NORTH);
-                }
-              break;
-
-            case SDL_JOYBUTTONDOWN:
-              if (event.jbutton.button == joystick_keymap.b_button)
-                enter_level = true;
-              else if (event.jbutton.button == joystick_keymap.start_button)
-                on_escape_press();
-              break;
-
-            default:
-              break;
-            }
-        }
-    }
+  } else {
+    Menu::set_current(0);
+  }
 }
 
 Vector
 WorldMap::get_next_tile(Vector pos, Direction direction)
 {
-  switch(direction)
-    {
+  switch(direction) {
     case D_WEST:
       pos.x -= 1;
       break;
@@ -781,7 +624,7 @@ WorldMap::get_next_tile(Vector pos, Direction direction)
       break;
     case D_NONE:
       break;
-    }
+  }
   return pos;
 }
 
@@ -790,36 +633,30 @@ WorldMap::path_ok(Direction direction, Vector old_pos, Vector* new_pos)
 {
   *new_pos = get_next_tile(old_pos, direction);
 
-  if (!(new_pos->x >= 0 && new_pos->x < width
-        && new_pos->y >= 0 && new_pos->y < height))
+  if (!(new_pos->x >= 0 && new_pos->x < solids->get_width()
+        && new_pos->y >= 0 && new_pos->y < solids->get_height()))
     { // New position is outsite the tilemap
       return false;
     }
-  else if(at(*new_pos)->one_way != BOTH_WAYS)
-    {
-std::cerr << "one way only\n";
-      if((at(*new_pos)->one_way == NORTH_SOUTH_WAY && direction != D_SOUTH) ||
-         (at(*new_pos)->one_way == SOUTH_NORTH_WAY && direction != D_NORTH) ||
-         (at(*new_pos)->one_way == EAST_WEST_WAY && direction != D_WEST) ||
-         (at(*new_pos)->one_way == WEST_EAST_WAY && direction != D_EAST))
-        return false;
-      return true;
-    }
   else
-    { // Check if we the tile allows us to go to new_pos
+    { // Check if the tile allows us to go to new_pos
       switch(direction)
         {
         case D_WEST:
-          return (at(old_pos)->west && at(*new_pos)->east);
+          return (at(old_pos)->getData() & Tile::WORLDMAP_WEST
+              && at(*new_pos)->getData() & Tile::WORLDMAP_EAST);
 
         case D_EAST:
-          return (at(old_pos)->east && at(*new_pos)->west);
+          return (at(old_pos)->getData() & Tile::WORLDMAP_EAST
+              && at(*new_pos)->getData() & Tile::WORLDMAP_WEST);
 
         case D_NORTH:
-          return (at(old_pos)->north && at(*new_pos)->south);
+          return (at(old_pos)->getData() & Tile::WORLDMAP_NORTH
+              && at(*new_pos)->getData() & Tile::WORLDMAP_SOUTH);
 
         case D_SOUTH:
-          return (at(old_pos)->south && at(*new_pos)->north);
+          return (at(old_pos)->getData() & Tile::WORLDMAP_SOUTH
+              && at(*new_pos)->getData() & Tile::WORLDMAP_NORTH);
 
         case D_NONE:
           assert(!"path_ok() can't work if direction is NONE");
@@ -829,12 +666,136 @@ std::cerr << "one way only\n";
 }
 
 void
+WorldMap::finished_level(const std::string& filename)
+{
+  // TODO calculate level from filename?
+  (void) filename;
+  Level* level = at_level();
+
+  bool old_level_state = level->solved;
+  level->solved = true;
+  level->sprite->set_action("solved");
+
+  // deal with statistics
+  level->statistics.merge(global_stats);
+  calculate_total_stats();
+
+  if(savegame_file != "")
+    savegame(savegame_file);
+
+  if (old_level_state != level->solved && level->auto_path) {
+    // Try to detect the next direction to which we should walk
+    // FIXME: Mostly a hack
+    Direction dir = D_NONE;
+  
+    const Tile* tile = at(tux->get_tile_pos());
+
+    if (tile->getData() & Tile::WORLDMAP_NORTH
+        && tux->back_direction != D_NORTH)
+      dir = D_NORTH;
+    else if (tile->getData() & Tile::WORLDMAP_SOUTH
+        && tux->back_direction != D_SOUTH)
+      dir = D_SOUTH;
+    else if (tile->getData() & Tile::WORLDMAP_EAST
+        && tux->back_direction != D_EAST)
+      dir = D_EAST;
+    else if (tile->getData() & Tile::WORLDMAP_WEST
+        && tux->back_direction != D_WEST)
+      dir = D_WEST;
+
+    if (dir != D_NONE) {
+      tux->set_direction(dir);
+    }
+  }
+
+  if (level->extro_script != "") {
+    /* TODO
+    try {
+      std::auto_ptr<ScriptInterpreter> interpreter
+        (new ScriptInterpreter(levels_path));
+      std::istringstream in(level->extro_script);
+      interpreter->run_script(in, "level-extro-script");
+      add_object(interpreter.release());
+    } catch(std::exception& e) {
+      msg_fatal << "Couldn't run level-extro-script:" << e.what() << std::endl;
+    }
+    */
+  }
+  
+  if (!level->next_worldmap.empty()) {
+    // Load given worldmap
+    loadmap(level->next_worldmap);
+  }
+  
+  if (level->quit_worldmap)
+    main_loop->exit_screen();
+}
+
+void
 WorldMap::update(float delta)
 {
-  if(!frame_timer.check()) {
-    global_frame_counter++;
+  Menu* menu = Menu::current();
+  if(menu) {
+    menu->update();
+
+    if(menu == worldmap_menu) {
+      switch (worldmap_menu->check())
+      {
+        case MNID_RETURNWORLDMAP: // Return to game  
+          Menu::set_current(0);
+          break;
+        case MNID_QUITWORLDMAP: // Quit Worldmap
+          main_loop->exit_screen();
+          break;
+      }
+    }
+
+    return;
+  }
+
+  // update GameObjects
+  for(GameObjects::iterator i = game_objects.begin();
+      i != game_objects.end(); ++i) {
+    GameObject* object = *i;
+    object->update(delta);
+  }
+
+  // remove old GameObjects
+  for(GameObjects::iterator i = game_objects.begin();
+      i != game_objects.end(); ) {
+    GameObject* object = *i;
+    if(!object->is_valid()) {
+      delete object;
+      i = game_objects.erase(i);
+    } else {
+      ++i;
+    }
   }
 
+  // position "camera"
+  Vector tux_pos = tux->get_pos();
+  camera_offset.x = tux_pos.x - SCREEN_WIDTH/2;
+  camera_offset.y = tux_pos.y - SCREEN_HEIGHT/2;
+
+  if (camera_offset.x < 0)
+    camera_offset.x = 0;
+  if (camera_offset.y < 0)
+    camera_offset.y = 0;
+
+  if (camera_offset.x > solids->get_width()*32 - SCREEN_WIDTH)
+    camera_offset.x = solids->get_width()*32 - SCREEN_WIDTH;
+  if (camera_offset.y > solids->get_height()*32 - SCREEN_HEIGHT)
+    camera_offset.y = solids->get_height()*32 - SCREEN_HEIGHT;
+
+  // handle input
+  bool enter_level = false;
+  if(main_controller->pressed(Controller::ACTION)
+      || main_controller->pressed(Controller::JUMP)
+      || main_controller->pressed(Controller::MENU_SELECT))
+    enter_level = true;
+  if(main_controller->pressed(Controller::PAUSE_MENU))
+    on_escape_press();
+  
   if (enter_level && !tux->is_moving())
     {
       /* Check special tile action */
@@ -844,7 +805,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
-          SoundManager::get()->play_sound(IDToSound(SND_WARP));
+          sound_manager->play("sounds/warp.wav");
           tux->back_direction = D_NONE;
           tux->set_tile_pos(special_tile->teleport_dest);
           SDL_Delay(1000);
@@ -852,185 +813,37 @@ WorldMap::update(float delta)
         }
 
       /* Check level action */
-      bool level_finished = true;
       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;
+      if (!level) {
+        msg_warning << "No level to enter at: " << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
         return;
-        }
-
-
-          if (level->pos == tux->get_tile_pos())
-            {
-              PlayerStatus old_player_status = player_status;
-
-              std::cout << "Enter the current level: " << level->name << std::endl;
-              // 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(level->name,
-                                  ST_GL_LOAD_LEVEL_FILE, level->vertical_flip,
-                                  &level->statistics);
-
-              switch (session.run())
-                {
-                case GameSession::ES_LEVEL_FINISHED:
-                  {
-                    level_finished = true;
-                    bool old_level_state = level->solved;
-                    level->solved = true;
-
-                    // deal with statistics
-                    level->statistics.merge(global_stats);
-                    calculate_total_stats();
-
-                    if (session.get_current_sector()->player->got_power !=
-                          session.get_current_sector()->player->NONE_POWER)
-                      player_status.bonus = PlayerStatus::FLOWER_BONUS;
-                    else if (session.get_current_sector()->player->size == BIG)
-                      player_status.bonus = PlayerStatus::GROWUP_BONUS;
-                    else
-                      player_status.bonus = PlayerStatus::NO_BONUS;
-
-                    if (old_level_state != level->solved && level->auto_path)
-                      { // Try to detect the next direction to which we should walk
-                        // FIXME: Mostly a hack
-                        Direction dir = D_NONE;
-                    
-                        Tile* tile = at(tux->get_tile_pos());
-
-                        if (tile->north && tux->back_direction != D_NORTH)
-                          dir = D_NORTH;
-                        else if (tile->south && tux->back_direction != D_SOUTH)
-                          dir = D_SOUTH;
-                        else if (tile->east && tux->back_direction != D_EAST)
-                          dir = D_EAST;
-                        else if (tile->west && tux->back_direction != D_WEST)
-                          dir = D_WEST;
-
-                        if (dir != D_NONE)
-                          {
-                            tux->set_direction(dir);
-                            //tux->update(delta);
-                          }
-
-                        std::cout << "Walk to dir: " << dir << std::endl;
-                      }
-                  }
-
-                  break;
-                case GameSession::ES_LEVEL_ABORT:
-                  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.distros = old_player_status.distros;
-                  player_status.lives = std::min(old_player_status.lives, player_status.lives);
-                  player_status.bonus = player_status.NO_BONUS;
-
-                  break;
-                case GameSession::ES_GAME_OVER:
-                  {
-                  level_finished = false;
-                  /* draw an end screen */
-                  /* TODO: in the future, this should make a dialog a la SuperMario, asking
-                  if the player wants to restart the world map with no score and from
-                  level 1 */
-                  char str[80];
-
-                  DrawingContext context;
-                  context.draw_gradient(Color (200,240,220), Color(200,200,220),
-                      LAYER_BACKGROUND0);
-
-                  context.draw_text(blue_text, _("GAMEOVER"), 
-                      Vector(screen->w/2, 200), CENTER_ALLIGN, LAYER_FOREGROUND1);
-
-                  sprintf(str, _("COINS: %d"), player_status.distros);
-                  context.draw_text(gold_text, str,
-                      Vector(screen->w/2, screen->w - 32), CENTER_ALLIGN, LAYER_FOREGROUND1);
-
-                  total_stats.draw_message_info(context, _("Total Statistics"));
-
-                  context.do_drawing();
-  
-                  SDL_Event event;
-                  wait_for_event(event,2000,6000,true);
-
-                  quit = true;
-                  player_status.reset();
-                  break;
-                  }
-                case GameSession::ES_NONE:
-                  assert(false);
-                  // Should never be reached 
-                  break;
-                }
-
-              SoundManager::get()->play_music(song);
-              Menu::set_current(0);
-              if (!savegame_file.empty())
-                savegame(savegame_file);
-            }
-      /* The porpose of the next checking is that if the player lost
-         the level (in case there is one), don't show anything */
-      if(level_finished)
-        {
-        if (!level->extro_filename.empty())
-          {
-          // Display a text file
-          display_text_file(level->extro_filename, SCROLL_SPEED_MESSAGE, white_big_text , white_text, white_small_text, blue_text );
-          }
+      }
 
-        if (!level->next_worldmap.empty())
-          {
-          // Load given worldmap
-          loadmap(level->next_worldmap);
-          }
-        if (level->quit_worldmap)
-          quit = true;
+      if (level->pos == tux->get_tile_pos()) {
+        // 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);
+
+        try {
+          GameSession *session =
+            new GameSession(levels_path + level->name,
+                ST_GL_LOAD_LEVEL_FILE, &level->statistics);
+          main_loop->push_screen(session);
+        } catch(std::exception& e) {
+          msg_fatal << "Couldn't load level: " << e.what() << std::endl;
         }
+      }
     }
   else
     {
-      tux->action(delta);
 //      tux->set_direction(input_direction);
     }
-  
-  Menu* menu = Menu::current();
-  if(menu)
-    {
-      menu->action();
-
-      if(menu == worldmap_menu)
-        {
-          switch (worldmap_menu->check())
-            {
-            case MNID_RETURNWORLDMAP: // Return to game
-              break;
-            case MNID_QUITWORLDMAP: // Quit Worldmap
-              quit = true;
-              break;
-            }
-        }
-      else if(menu == options_menu)
-        {
-          process_options_menu();
-        }
-    }
 }
 
-Tile*
+const Tile*
 WorldMap::at(Vector p)
 {
-  assert(p.x >= 0 
-         && p.x < width
-         && p.y >= 0
-         && p.y < height);
-
-  int x = int(p.x);
-  int y = int(p.y);
-  return tile_manager->get(tilemap[width * y + x]);
+  return solids->get_tile((int) p.x, (int) p.y);
 }
 
 WorldMap::Level*
@@ -1057,25 +870,22 @@ WorldMap::at_special_tile()
   return 0;
 }
 
-
 void
-WorldMap::draw(DrawingContext& context, const Vector& offset)
+WorldMap::draw(DrawingContext& context)
 {
-  for(int y = 0; y < height; ++y)
-    for(int x = 0; x < width; ++x)
-      {
-        Tile* tile = at(Vector(x, y));
-        tile->draw(context, Vector(x*32 + offset.x, y*32 + offset.y));
-      }
-
+  context.push_transform();
+  context.set_translation(camera_offset);
+  
+  for(GameObjects::iterator i = game_objects.begin();
+      i != game_objects.end(); ++i) {
+    GameObject* object = *i;
+    object->draw(context);
+  }
+  
   for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
     {
-      if (i->solved)
-        context.draw_surface(leveldot_green,
-            Vector(i->pos.x*32 + offset.x, i->pos.y*32 + offset.y), LAYER_TILES+1);
-      else
-        context.draw_surface(leveldot_red,
-            Vector(i->pos.x*32 + offset.x, i->pos.y*32 + offset.y), 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)
@@ -1083,53 +893,28 @@ WorldMap::draw(DrawingContext& context, const Vector& offset)
       if(i->invisible)
         continue;
 
-      if (i->teleport_dest != Vector(-1, -1))
-        context.draw_surface(teleporterdot,
-                Vector(i->pos.x*32 + offset.x, i->pos.y*32 + offset.y), 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,
-                Vector(i->pos.x*32 + offset.x, i->pos.y*32 + offset.y), LAYER_TILES+1);
+                Vector(i->pos.x*32, i->pos.y*32), LAYER_TILES+1);
     }
 
-  tux->draw(context, offset);
   draw_status(context);
+  context.pop_transform();
 }
 
 void
 WorldMap::draw_status(DrawingContext& context)
 {
-  char str[80];
-  sprintf(str, " %d", total_stats.get_points(SCORE_STAT));
-
-  context.draw_text(white_text, _("SCORE"), Vector(0, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
-  context.draw_text(gold_text, str, Vector(96, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
-
-  sprintf(str, "%d", player_status.distros);
-  context.draw_text(white_text, _("COINS"), Vector(screen->w/2 - 16*5, 0),
-      LEFT_ALLIGN, LAYER_FOREGROUND1);
-  context.draw_text(gold_text, str, Vector(screen->w/2 + (16*5)/2, 0),
-        LEFT_ALLIGN, LAYER_FOREGROUND1);
-
-  if (player_status.lives >= 5)
-    {
-      sprintf(str, "%dx", player_status.lives);
-      context.draw_text(gold_text, str, 
-          Vector(screen->w - gold_text->get_text_width(str) - tux_life->w, 0),
-          LEFT_ALLIGN, LAYER_FOREGROUND1);
-      context.draw_surface(tux_life, Vector(screen->w -
-            gold_text->get_text_width("9"), 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
-    }
-  else
-    {
-      for(int i= 0; i < player_status.lives; ++i)
-        context.draw_surface(tux_life,
-            Vector(screen->w - tux_life->w*4 + (tux_life->w*i), 0),
-            LAYER_FOREGROUND1);
-    }
-  context.draw_text(white_text, _("LIVES"),
-      Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width("   99"), 0),
-      LEFT_ALLIGN, LAYER_FOREGROUND1);
+  context.push_transform();
+  context.set_translation(Vector(0, 0));
+  player_status->draw(context);
 
   if (!tux->is_moving())
     {
@@ -1141,8 +926,8 @@ WorldMap::draw_status(DrawingContext& context)
                 get_level_title(*i);
 
               context.draw_text(white_text, i->title, 
-                  Vector(screen->w/2,
-                         screen->h - white_text->get_height() - 30),
+                  Vector(SCREEN_WIDTH/2,
+                         SCREEN_HEIGHT - white_text->get_height() - 30),
                   CENTER_ALLIGN, LAYER_FOREGROUND1);
 
               i->statistics.draw_worldmap_info(context);
@@ -1156,75 +941,29 @@ WorldMap::draw_status(DrawingContext& context)
                /* Display an in-map message in the map, if any as been selected */
               if(!i->map_message.empty() && !i->passive_message)
                 context.draw_text(gold_text, i->map_message, 
-                    Vector(screen->w/2,
-                           screen->h - white_text->get_height() - 60),
+                    Vector(SCREEN_WIDTH/2,
+                           SCREEN_HEIGHT - white_text->get_height() - 60),
                     CENTER_ALLIGN, LAYER_FOREGROUND1);
               break;
             }
         }
     }
   /* 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->w/2, screen->h - white_text->get_height() - 60),
+            Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - white_text->get_height() - 60),
             CENTER_ALLIGN, LAYER_FOREGROUND1);
+
+  context.pop_transform();
 }
 
 void
-WorldMap::display()
+WorldMap::setup()
 {
-  Menu::set_current(0);
-
-  quit = false;
-
-  song = SoundManager::get()->load_music(datadir +  "/music/" + music);
-  SoundManager::get()->play_music(song);
-
-  FrameRate frame_rate(10);
-  frame_rate.set_frame_limit(false);
-
-  frame_rate.start();
-  frame_timer.start(.25, true);
-
-  DrawingContext context;
-  while(!quit)
-    {
-      float delta = frame_rate.get();
-
-      delta *= 1.3f;
-
-      if (delta > 10.0f)
-        delta = .3f;
-       
-      frame_rate.update();
-
-      Vector tux_pos = tux->get_pos();
-      if (1)
-        {
-          offset.x = -tux_pos.x + screen->w/2;
-          offset.y = -tux_pos.y + screen->h/2;
-
-          if (offset.x > 0) offset.x = 0;
-          if (offset.y > 0) offset.y = 0;
-
-          if (offset.x < screen->w - width*32) offset.x = screen->w - width*32;
-          if (offset.y < screen->h - height*32) offset.y = screen->h - height*32;
-        } 
-
-      draw(context, offset);
-      get_input();
-      update(delta);
-      
-      if(Menu::current())
-        {
-          Menu::current()->draw(context);
-          mouse_cursor->draw(context);
-        }
+  sound_manager->play_music(music);
+  Menu::set_current(NULL);
 
-      context.do_drawing();
-
-      SDL_Delay(20);
-    }
+  current_ = this;
 }
 
 void
@@ -1233,161 +972,149 @@ WorldMap::savegame(const std::string& filename)
   if(filename == "")
     return;
 
-  std::cout << "savegame: " << filename << std::endl;
-
-   std::ofstream file(filename.c_str(), std::ios::out);
-   LispWriter* writer = new LispWriter(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)
-    {
-      ++total_levels;
-      if (i->solved)
-        ++nb_solved_levels;
-    }
+  for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {
+    ++total_levels;
+    if (i->solved)
+      ++nb_solved_levels;
+  }
   char nb_solved_levels_str[80], total_levels_str[80];
   sprintf(nb_solved_levels_str, "%d", nb_solved_levels);
   sprintf(total_levels_str, "%d", total_levels);
 
-  writer->write_comment("Worldmap save file");
+  writer.write_comment("Worldmap save file");
 
-  writer->start_list("supertux-savegame");
+  writer.start_list("supertux-savegame");
 
-  writer->write_int("version", 1);
-  writer->write_string("title", std::string(name + " - " + nb_solved_levels_str + "/" + total_levels_str));
-  writer->write_string("map", map_filename);
-  writer->write_int("lives", player_status.lives);
-  writer->write_int("distros", player_status.lives);
-  writer->write_int("max-score-multiplier", player_status.max_score_multiplier);
+  writer.write_int("version", 1);
+  writer.write_string("title",
+      std::string(name + " - " + nb_solved_levels_str+"/"+total_levels_str));
+  writer.write_string("map", map_filename);
+  writer.write_bool("intro-displayed", intro_displayed);
 
-  writer->start_list("tux");
+  writer.start_list("tux");
 
-  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));
-  writer->write_string("bonus", bonus_to_string(player_status.bonus));
+  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);
+  writer.write_string("back", direction_to_string(tux->back_direction));
 
-  writer->end_list("tux");
+  writer.end_list("tux");
 
-  writer->start_list("levels");
+  writer.start_list("levels");
 
   for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
     {
       if (i->solved)
         {
-        writer->start_list("level");
+        writer.start_list("level");
 
-        writer->write_string("name", i->name);
-        writer->write_bool("solved", true);
-        i->statistics.write(*writer);
+        writer.write_string("name", i->name);
+        writer.write_bool("solved", true);
+        i->statistics.write(writer);
 
-        writer->end_list("level");
+        writer.end_list("level");
         }
     }  
 
-  writer->end_list("levels");
+  writer.end_list("levels");
 
-  writer->end_list("supertux-savegame");
+  writer.end_list("supertux-savegame");
 }
 
 void
 WorldMap::loadgame(const std::string& filename)
 {
-  std::cout << "loadgame: " << filename << std::endl;
+  msg_debug << "loadgame: " << filename << std::endl;
   savegame_file = filename;
-
-  if (access(filename.c_str(), F_OK) != 0)
-    {
-    load_map();
-
-    player_status.reset();
-
-    return;
-    }
   
-  lisp_object_t* savegame = lisp_read_from_file(filename);
-  if (!savegame)
-    {
-      std::cout << "WorldMap:loadgame: File not found: " << filename << std::endl;
-      load_map();
-      return;
-    }
-
-  lisp_object_t* cur = savegame;
-
-  if (strcmp(lisp_symbol(lisp_car(cur)), "supertux-savegame") != 0)
-    {
-    load_map();
-    return;
-    }
-
-  cur = lisp_cdr(cur);
-  LispReader reader(cur);
+  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;
-  reader.read_string("map", map_filename);
-//  if(cur_map_filename != map_filename)
-    load_map(); 
+      /* 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(); 
 
-  reader.read_int("lives", player_status.lives);
-  reader.read_int("distros", player_status.distros);
-  reader.read_int("max-score-multiplier", player_status.max_score_multiplier);
+      savegame->get("intro-displayed", intro_displayed);
 
-  if (player_status.lives < 0)
-    player_status.lives = START_LIVES;
+      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);
+        if (player_status->coins < 0) player_status->reset();
+        tux->back_direction = string_to_direction(back_str);      
+        tux->set_tile_pos(p);
+      }
 
-  lisp_object_t* tux_cur = 0;
-  if (reader.read_lisp("tux", tux_cur))
-    {
-      Vector p;
-      std::string back_str = "none";
-      std::string bonus_str = "none";
-
-      LispReader tux_reader(tux_cur);
-      tux_reader.read_float("x", p.x);
-      tux_reader.read_float("y", p.y);
-      tux_reader.read_string("back", back_str);
-      tux_reader.read_string("bonus", bonus_str);
-      
-      player_status.bonus = string_to_bonus(bonus_str);
-      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;
 
-  lisp_object_t* level_cur = 0;
-  if (reader.read_lisp("levels", level_cur))
-    {
-      while(level_cur)
-        {
-          lisp_object_t* sym  = lisp_car(lisp_car(level_cur));
-          lisp_object_t* data = lisp_cdr(lisp_car(level_cur));
+            const lisp::Lisp* level = iter.lisp();
+            level->get("name", name);
+            level->get("solved", solved);
 
-          if (strcmp(lisp_symbol(sym), "level") == 0)
+            for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
             {
-              std::string name;
-              bool solved = false;
-
-              LispReader level_reader(data);
-              level_reader.read_string("name", name);
-              level_reader.read_bool("solved", solved);
-
-              for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
-                {
-                  if (name == i->name)
-                    {
-                    i->solved = solved;
-                    i->statistics.parse(level_reader);
-                    break;
-                    }
-                }
+              if (name == i->name)
+              {
+                i->solved = solved;
+                i->sprite->set_action(solved ? "solved" : "default");
+                i->statistics.parse(*level);
+                break;
+              }
             }
-
-          level_cur = lisp_cdr(level_cur);
+          } else {
+            msg_warning << "Unknown token '" << iter.item() << "' in levels block in worldmap" << std::endl;
+          }
         }
+      }
+    } catch(std::exception& e) {
+      msg_warning << "Problem loading game '" << filename << "': " << e.what() << std::endl;
+      load_map();
+      player_status->reset();
     }
-  lisp_free(savegame);
+  }
+  else
+  {
+    load_map();
+    player_status->reset();
+  }
 
   calculate_total_stats();
 }
@@ -1401,8 +1128,3 @@ WorldMap::loadmap(const std::string& filename)
 }
 
 } // namespace WorldMapNS
-
-/* Local Variables: */
-/* mode:c++ */
-/* End: */
-