Added drawing of statistics on levels messages and game over.
[supertux.git] / src / worldmap.cpp
index 5401090..5c09101 100644 (file)
@@ -28,6 +28,8 @@
 #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"
@@ -37,6 +39,8 @@
 #include "app/gettext.h"
 #include "misc.h"
 
+#define map_message_TIME 2800
+
 Menu* worldmap_menu  = 0;
 
 namespace WorldMapNS {
@@ -114,23 +118,52 @@ TileManager::TileManager()
               std::string filename = "<invalid>";
 
               Tile* tile = new Tile;             
-              tile->north = true;
-              tile->east  = true;
-              tile->south = true;
-              tile->west  = true;
+              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("image", filename);
 
+              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;
+                }
+
               tile->sprite = new Surface(
                            datadir +  "/images/worldmap/" + filename, 
                            true);
@@ -247,10 +280,17 @@ Tux::stop()
 {
   offset = 0;
   direction = D_NONE;
+  input_direction = D_NONE;
   moving = false;
 }
 
 void
+Tux::set_direction(Direction dir)
+{
+input_direction = dir;
+}
+
+void
 Tux::action(float delta)
 {
   if (!moving)
@@ -271,7 +311,6 @@ Tux::action(float delta)
             }
           else if (input_direction == back_direction)
             {
-              std::cout << "Back triggered" << std::endl;
               moving = true;
               direction = input_direction;
               tile_pos = worldmap->get_next_tile(tile_pos, direction);
@@ -288,49 +327,89 @@ Tux::action(float delta)
         { // We reached the next tile, so we check what to do now
           offset -= 32;
 
-          if (worldmap->at(tile_pos)->stop || worldmap->at_special_tile())
+          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))
             {
+              if(special_tile && !special_tile->map_message.empty() &&
+                !special_tile->passive_message)
+                worldmap->passive_message_timer.stop();
               stop();
             }
           else
             {
-              if (worldmap->at(tile_pos)->auto_walk)
+              if (worldmap->at(tile_pos)->auto_walk || direction != input_direction)
                 { // Turn to a new direction
                   Tile* tile = worldmap->at(tile_pos);
-                  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;
+
+                  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
-              Vector next_tile;
-              if (worldmap->path_ok(direction, tile_pos, &next_tile))
+              if(direction != D_NONE)
                 {
+                Vector next_tile;
+                if (worldmap->path_ok(direction, tile_pos, &next_tile))
+                  {
                   tile_pos = next_tile;
-                }
-              else
-                {
+                  }
+                else
+                  {
                   puts("Tilemap data is buggy");
                   stop();
+                  }
                 }
             }
         }
@@ -360,11 +439,11 @@ WorldMap::WorldMap()
   start_x = 4;
   start_y = 5;
   
-  level_sprite = new Surface(datadir +  "/images/worldmap/levelmarker.png", true);
   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);
 
-  input_direction = D_NONE;
   enter_level = false;
 
   name = "<no title>";
@@ -376,17 +455,18 @@ WorldMap::~WorldMap()
   delete tux;
   delete tile_manager;
 
-  delete level_sprite;
   delete leveldot_green;
   delete leveldot_red;
+  delete messagedot;
+  delete teleporterdot;
 }
 
 void
 WorldMap::load_map()
 {
-  lisp_object_t* root_obj = lisp_read_from_file(datadir + "/special_tiles/worldmap/" + map_filename);
+  lisp_object_t* root_obj = lisp_read_from_file(datadir + "/levels/worldmap/" + map_filename);
   if (!root_obj)
-    Termination::abort("Couldn't load file", datadir + "/special_tiles/worldmap/" + map_filename);
+    Termination::abort("Couldn't load file", datadir + "/levels/worldmap/" + map_filename);
 
   if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap") == 0)
     {
@@ -411,7 +491,8 @@ WorldMap::load_map()
              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)
+          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);
               
@@ -430,22 +511,53 @@ WorldMap::load_map()
                       special_tile.south = true;
                       special_tile.west  = true;
 
-                      reader.read_string("extro-filename", special_tile.extro_filename);
-                      reader.read_string("map-message", special_tile.display_map_message);
-                      reader.read_string("next-world", special_tile.next_worldmap);
-                      reader.read_string("level", special_tile.level_name, true);
                       reader.read_int("x", special_tile.x);
                       reader.read_int("y", special_tile.y);
-                      special_tile.auto_path = true;
-                      reader.read_bool("auto-path", special_tile.auto_path);
-                      special_tile.swap_x = special_tile.swap_y = -1;
-                      reader.read_int("swap-x", special_tile.swap_x);
-                      reader.read_int("swap-y", special_tile.swap_y);
+                      reader.read_string("level", special_tile.level_name, false);
+
                       special_tile.vertical_flip = false;
-                      reader.read_bool("flip-special_tile", special_tile.vertical_flip);
+                      reader.read_bool("vertical-flip", special_tile.vertical_flip);
+
+                      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_x = special_tile.teleport_dest_y = -1;
+                      reader.read_int("teleport-to-x", special_tile.teleport_dest_x);
+                      reader.read_int("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;
+                        }
+
+                      reader.read_string("extro-filename", special_tile.extro_filename);
+                      reader.read_string("next-world", special_tile.next_worldmap);
                       special_tile.quit_worldmap = false;
                       reader.read_bool("exit-game", special_tile.quit_worldmap);
 
+                      special_tile.auto_path = true;
+                      reader.read_bool("auto-path", special_tile.auto_path);
+
                       special_tiles.push_back(special_tile);
                     }
 
@@ -461,14 +573,21 @@ WorldMap::load_map()
                       special_tile.south = true;
                       special_tile.west  = true;
 
+                      special_tile.invisible = false;
+
+                      special_tile.apply_action_north = special_tile.apply_action_south =
+                          special_tile.apply_action_east = special_tile.apply_action_west =
+                          true;
+                      special_tile.vertical_flip = false;
+                      special_tile.teleport_dest_x = special_tile.teleport_dest_y = -1;
+
                       reader.read_string("extro-filename", special_tile.extro_filename);
                       if(!special_tile.extro_filename.empty())
                         special_tile.quit_worldmap = true;
                       reader.read_string("name", special_tile.level_name, true);
                       reader.read_int("x", special_tile.x);
                       reader.read_int("y", special_tile.y);
-                      special_tile.vertical_flip = false;
-                      special_tile.swap_x = special_tile.swap_y = -1;
+
 
                       special_tiles.push_back(special_tile);
                     }
@@ -494,10 +613,10 @@ void WorldMap::get_level_title(SpecialTile& special_tile)
   /** get special_tile's title */
   special_tile.title = "<no title>";
 
-  LispReader* reader = LispReader::load(datadir + "/special_tiles/" + special_tile.level_name, "supertux-special_tile");
+  LispReader* reader = LispReader::load(datadir + "/levels/" + special_tile.level_name, "supertux-level");
   if(!reader)
     {
-    std::cerr << "Error: Could not open special_tile file. Ignoring...\n";
+    std::cerr << "Error: Could not open level file. Ignoring...\n";
     return;
     }
 
@@ -505,12 +624,27 @@ void WorldMap::get_level_title(SpecialTile& special_tile)
   delete reader;
 }
 
+void WorldMap::calculate_total_stats()
+{
+  total_stats.reset();
+  for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
+    {
+    if (!i->level_name.empty() && i->solved)
+      {
+      total_stats += i->statistics;
+      }
+    }
+}
+
 void
 WorldMap::on_escape_press()
 {
   // Show or hide the 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); 
 }
@@ -519,7 +653,6 @@ void
 WorldMap::get_input()
 {
   enter_level = false;
-  input_direction = D_NONE;
    
   SDL_Event event;
   while (SDL_PollEvent(&event))
@@ -546,25 +679,51 @@ WorldMap::get_input()
                 case SDLK_RETURN:
                   enter_level = true;
                   break;
+
+                case SDLK_LEFT:
+                  tux->set_direction(D_WEST);
+                  break;
+                case SDLK_RIGHT:
+                  tux->set_direction(D_EAST);
+                  break;
+                case SDLK_UP:
+                  tux->set_direction(D_NORTH);
+                  break;
+                case SDLK_DOWN:
+                  tux->set_direction(D_SOUTH);
+                  break;
+
                 default:
                   break;
                 }
               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)
-                    input_direction = D_WEST;
+                    tux->set_direction(D_WEST);
                   else if (event.jaxis.value > joystick_keymap.dead_zone)
-                    input_direction = D_EAST;
+                    tux->set_direction(D_EAST);
                 }
               else if (event.jaxis.axis == joystick_keymap.y_axis)
                 {
                   if (event.jaxis.value > joystick_keymap.dead_zone)
-                    input_direction = D_SOUTH;
+                    tux->set_direction(D_SOUTH);
                   else if (event.jaxis.value < -joystick_keymap.dead_zone)
-                    input_direction = D_NORTH;
+                    tux->set_direction(D_NORTH);
                 }
               break;
 
@@ -580,20 +739,6 @@ WorldMap::get_input()
             }
         }
     }
-
-  if (!Menu::current())
-    {
-      Uint8 *keystate = SDL_GetKeyState(NULL);
-  
-      if (keystate[SDLK_LEFT])
-        input_direction = D_WEST;
-      else if (keystate[SDLK_RIGHT])
-        input_direction = D_EAST;
-      else if (keystate[SDLK_UP])
-        input_direction = D_NORTH;
-      else if (keystate[SDLK_DOWN])
-        input_direction = D_SOUTH;
-    }
 }
 
 Vector
@@ -629,6 +774,16 @@ WorldMap::path_ok(Direction direction, Vector old_pos, Vector* new_pos)
     { // 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
       switch(direction)
@@ -674,12 +829,13 @@ WorldMap::update(float delta)
             {
               PlayerStatus old_player_status = player_status;
 
-              std::cout << "Enter the current special_tile: " << special_tile->level_name << std::endl;
+              std::cout << "Enter the current level: " << special_tile->level_name << std::endl;
               // do a shriking fade to the special_tile
               shrink_fade(Vector((special_tile->x*32 + 16 + offset.x),(special_tile->y*32 + 16
                       + offset.y)), 500);
-              GameSession session(datadir +  "/special_tiles/" + special_tile->level_name,
-                                  ST_GL_LOAD_LEVEL_FILE, special_tile->vertical_flip);
+              GameSession session(datadir +  "/levels/" + special_tile->level_name,
+                                  ST_GL_LOAD_LEVEL_FILE, special_tile->vertical_flip,
+                                  &special_tile->statistics);
 
               switch (session.run())
                 {
@@ -689,6 +845,10 @@ WorldMap::update(float delta)
                     bool old_level_state = special_tile->solved;
                     special_tile->solved = true;
 
+                    // deal with statistics
+                    special_tile->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;
@@ -728,7 +888,6 @@ WorldMap::update(float delta)
                   level_finished = false;
                   /* In case the player's abort the special_tile, keep it using the old
                       status. But the minimum lives and no bonus. */
-                  player_status.score = old_player_status.score;
                   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;
@@ -738,9 +897,9 @@ WorldMap::update(float delta)
                   {
                   level_finished = false;
                   /* draw an end screen */
-                  /* in the future, this should make a dialog a la SuperMario, asking
+                  /* 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
-                  special_tile 1 */
+                  level 1 */
                   char str[80];
 
                   DrawingContext context;
@@ -750,18 +909,20 @@ WorldMap::update(float delta)
                   context.draw_text_center(blue_text, _("GAMEOVER"), 
                       Vector(0, 200), LAYER_FOREGROUND1);
 
-                  sprintf(str, _("SCORE: %d"), player_status.score);
-                  context.draw_text_center(gold_text, str,
-                      Vector(0, 230), LAYER_FOREGROUND1);
+//                  sprintf(str, _("SCORE: %d"), total_stats.get_points(SCORE_STAT));
+//                  context.draw_text_center(gold_text, str,
+//                      Vector(0, 230), LAYER_FOREGROUND1);
 
                   sprintf(str, _("COINS: %d"), player_status.distros);
                   context.draw_text_center(gold_text, str,
                       Vector(0, screen->w - 32), LAYER_FOREGROUND1);
 
+                  total_stats.draw_message_info(context, _("Total Statistics"));
+
                   context.do_drawing();
   
                   SDL_Event event;
-                  wait_for_event(event,2000,5000,true);
+                  wait_for_event(event,2000,6000,true);
 
                   quit = true;
                   player_status.reset();
@@ -788,10 +949,13 @@ WorldMap::update(float delta)
           // Display a text file
           display_text_file(special_tile->extro_filename, SCROLL_SPEED_MESSAGE, white_big_text , white_text, white_small_text, blue_text );
           }
-        if (special_tile->swap_x != -1 && special_tile->swap_y != -1)
+        if (special_tile->teleport_dest_x != -1 && special_tile->teleport_dest_y != -1)
           {
-          // TODO: add an effect, like a camera scrolling, or at least, a fading
-          tux->set_tile_pos(Vector(special_tile->swap_x, special_tile->swap_y));
+          // TODO: an animation, camera scrolling or a fading would be a nice touch
+          SoundManager::get()->play_sound(IDToSound(SND_WARP));
+          tux->back_direction = D_NONE;
+          tux->set_tile_pos(Vector(special_tile->teleport_dest_x, special_tile->teleport_dest_y));
+          SDL_Delay(1000);
           }
         if (!special_tile->next_worldmap.empty())
           {
@@ -805,7 +969,7 @@ WorldMap::update(float delta)
   else
     {
       tux->action(delta);
-      tux->set_direction(input_direction);
+//      tux->set_direction(input_direction);
     }
   
   Menu* menu = Menu::current();
@@ -871,8 +1035,20 @@ WorldMap::draw(DrawingContext& context, const Vector& offset)
   
   for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
     {
+      if(i->invisible)
+        continue;
       if(i->level_name.empty())
+        {
+        if (i->teleport_dest_x != -1 && i->teleport_dest_y != -1)
+          context.draw_surface(teleporterdot,
+              Vector(i->x*32 + offset.x, i->y*32 + offset.y), LAYER_TILES+1);
+
+        else if (!i->map_message.empty() && !i->passive_message)
+          context.draw_surface(messagedot,
+              Vector(i->x*32 + offset.x, i->y*32 + offset.y), LAYER_TILES+1);
+
         continue;
+        }
 
       if (i->solved)
         context.draw_surface(leveldot_green,
@@ -890,7 +1066,7 @@ void
 WorldMap::draw_status(DrawingContext& context)
 {
   char str[80];
-  sprintf(str, " %d", player_status.score);
+  sprintf(str, " %d", total_stats.get_points(SCORE_STAT));
 
   context.draw_text(white_text, _("SCORE"), Vector(0, 0), LAYER_FOREGROUND1);
   context.draw_text(gold_text, str, Vector(96, 0), LAYER_FOREGROUND1);
@@ -936,17 +1112,24 @@ WorldMap::draw_status(DrawingContext& context)
                 context.draw_text_center(white_text, i->title, 
                     Vector(0, screen->h - white_text->get_height() - 30),
                     LAYER_FOREGROUND1);
+
+                i->statistics.draw_worldmap_info(context);
                 }
 
-              /* Display a message in the map, if any as been selected */
-              if(!i->display_map_message.empty())
-                context.draw_text_center(gold_text, i->display_map_message, 
+              /* Display an in-map message in the map, if any as been selected */
+              if(!i->map_message.empty() && !i->passive_message)
+                context.draw_text_center(gold_text, i->map_message, 
                     Vector(0, screen->h - white_text->get_height() - 60),
                     LAYER_FOREGROUND1);
               break;
             }
         }
     }
+  /* Display a passive message in the map, if needed */
+  if(passive_message_timer.check())
+    context.draw_text_center(gold_text, passive_message, 
+            Vector(0, screen->h - white_text->get_height() - 60),
+            LAYER_FOREGROUND1);
 }
 
 void
@@ -958,24 +1141,23 @@ WorldMap::display()
 
   song = SoundManager::get()->load_music(datadir +  "/music/" + music);
   SoundManager::get()->play_music(song);
-  
-  unsigned int last_update_time;
-  unsigned int update_time;
 
-  last_update_time = update_time = Ticks::get();
+  FrameRate frame_rate(10);
+  frame_rate.set_frame_limit(false);
+
+  frame_rate.start();
 
   DrawingContext context;
   while(!quit)
     {
-      float delta = ((float)(update_time-last_update_time))/100.0;
+      float delta = frame_rate.get();
 
       delta *= 1.3f;
 
       if (delta > 10.0f)
         delta = .3f;
-      
-      last_update_time = update_time;
-      update_time      = Ticks::get();
+       
+      frame_rate.update();
 
       Vector tux_pos = tux->get_pos();
       if (1)
@@ -993,7 +1175,7 @@ WorldMap::display()
       draw(context, offset);
       get_input();
       update(delta);
-
+      
       if(Menu::current())
         {
           Menu::current()->draw(context);
@@ -1009,42 +1191,64 @@ WorldMap::display()
 void
 WorldMap::savegame(const std::string& filename)
 {
-  if(filename != "")
+  if(filename == "")
     return;
 
   std::cout << "savegame: " << filename << std::endl;
-  std::ofstream out(filename.c_str());
 
-  int nb_solved_levels = 0;
+   std::ofstream file(filename.c_str(), std::ios::out);
+   LispWriter* writer = new LispWriter(file);
+
+  int nb_solved_levels = 0, total_levels = 0;
   for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
     {
+      if(!i->level_name.empty())
+        ++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->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->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->end_list("tux");
+
+  writer->start_list("levels");
 
-  out << "(supertux-savegame\n"
-      << "  (version 1)\n"
-      << "  (title  \"" << name << " - " << nb_solved_levels << "/" << special_tiles.size() << "\")\n"
-      << "  (map    \"" << map_filename << "\")\n"
-      << "  (lives   " << player_status.lives << ")\n"
-      << "  (score   " << player_status.score << ")\n"
-      << "  (distros " << player_status.distros << ")\n"
-      << "  (tux (x " << tux->get_tile_pos().x << ") (y " << tux->get_tile_pos().y << ")\n"
-      << "       (back \"" << direction_to_string(tux->back_direction) << "\")\n"
-      << "       (bonus \"" << bonus_to_string(player_status.bonus) <<  "\"))\n"
-      << "  (special_tiles\n";
-  
   for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
     {
       if (i->solved && !i->level_name.empty())
         {
-          out << "     (special_tile (name \"" << i->level_name << "\")\n"
-              << "            (solved #t))\n";
+        writer->start_list("level");
+
+        writer->write_string("name", i->level_name);
+        writer->write_bool("solved", true);
+        i->statistics.write(*writer);
+
+        writer->end_list("level");
         }
     }  
 
-  out << "   )\n"
-      << " )\n\n;; EOF ;;" << std::endl;
+  writer->end_list("levels");
+
+  writer->end_list("supertux-savegame");
 }
 
 void
@@ -1084,7 +1288,6 @@ WorldMap::loadgame(const std::string& filename)
   load_map(); 
 
   reader.read_int("lives", player_status.lives);
-  reader.read_int("score", player_status.score);
   reader.read_int("distros", player_status.distros);
 
   if (player_status.lives < 0)
@@ -1109,26 +1312,30 @@ WorldMap::loadgame(const std::string& filename)
     }
 
   lisp_object_t* level_cur = 0;
-  if (reader.read_lisp("special_tiles", level_cur))
+  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));
 
-          if (strcmp(lisp_symbol(sym), "special_tile") == 0)
+          if (strcmp(lisp_symbol(sym), "level") == 0)
             {
               std::string name;
               bool solved = false;
 
               LispReader level_reader(data);
-              level_reader.read_string("name", name, true);
+              level_reader.read_string("name", name);
               level_reader.read_bool("solved", solved);
 
               for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
                 {
                   if (name == i->level_name)
+                    {
                     i->solved = solved;
+                    i->statistics.parse(level_reader);
+                    break;
+                    }
                 }
             }
 
@@ -1137,6 +1344,8 @@ WorldMap::loadgame(const std::string& filename)
     }
  
   lisp_free(savegame);
+
+  calculate_total_stats();
 }
 
 void