Show a message when more than one bad guys is killed in a row.
[supertux.git] / src / worldmap.cpp
index a00932b..9971ef1 100644 (file)
@@ -28,6 +28,7 @@
 #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"
@@ -447,6 +448,8 @@ WorldMap::WorldMap()
 
   name = "<no title>";
   music = "SALCON.MOD";
+
+  total_stats.reset();
 }
 
 WorldMap::~WorldMap()
@@ -512,7 +515,7 @@ WorldMap::load_map()
 
                       reader.read_int("x", special_tile.x);
                       reader.read_int("y", special_tile.y);
-                      reader.read_string("level", special_tile.level_name, true);
+                      reader.read_string("level", special_tile.level_name, false);
 
                       special_tile.vertical_flip = false;
                       reader.read_bool("vertical-flip", special_tile.vertical_flip);
@@ -623,6 +626,18 @@ 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()
 {
@@ -821,7 +836,8 @@ WorldMap::update(float delta)
               shrink_fade(Vector((special_tile->x*32 + 16 + offset.x),(special_tile->y*32 + 16
                       + offset.y)), 500);
               GameSession session(datadir +  "/levels/" + special_tile->level_name,
-                                  ST_GL_LOAD_LEVEL_FILE, special_tile->vertical_flip);
+                                  ST_GL_LOAD_LEVEL_FILE, special_tile->vertical_flip,
+                                  &special_tile->statistics);
 
               switch (session.run())
                 {
@@ -831,6 +847,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;
@@ -870,7 +890,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;
@@ -880,30 +899,28 @@ 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;
                   context.draw_gradient(Color (200,240,220), Color(200,200,220),
                       LAYER_BACKGROUND0);
 
-                  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);
+                  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_center(gold_text, str,
-                      Vector(0, screen->w - 32), LAYER_FOREGROUND1);
+                  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,5000,true);
+                  wait_for_event(event,2000,6000,true);
 
                   quit = true;
                   player_status.reset();
@@ -1047,25 +1064,25 @@ 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);
+  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),
-      LAYER_FOREGROUND1);
+      LEFT_ALLIGN, LAYER_FOREGROUND1);
   context.draw_text(gold_text, str, Vector(screen->w/2 + (16*5)/2, 0),
-        LAYER_FOREGROUND1);
+        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),
-          LAYER_FOREGROUND1);
+          LEFT_ALLIGN, LAYER_FOREGROUND1);
       context.draw_surface(tux_life, Vector(screen->w -
-            gold_text->get_text_width("9"), 0), LAYER_FOREGROUND1);
+            gold_text->get_text_width("9"), 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
     }
   else
     {
@@ -1076,7 +1093,7 @@ WorldMap::draw_status(DrawingContext& context)
     }
   context.draw_text(white_text, _("LIVES"),
       Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width("   99"), 0),
-      LAYER_FOREGROUND1);
+      LEFT_ALLIGN, LAYER_FOREGROUND1);
 
   if (!tux->is_moving())
     {
@@ -1090,25 +1107,27 @@ WorldMap::draw_status(DrawingContext& context)
                 if(i->title == "")
                   get_level_title(*i);
 
-                context.draw_text_center(white_text, i->title, 
-                    Vector(0, screen->h - white_text->get_height() - 30),
-                    LAYER_FOREGROUND1);
+                context.draw_text(white_text, i->title, 
+                    Vector(screen->w/2, screen->h - white_text->get_height() - 30),
+                    CENTER_ALLIGN, LAYER_FOREGROUND1);
+
+                i->statistics.draw_worldmap_info(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_center(gold_text, i->map_message, 
-                    Vector(0, screen->h - white_text->get_height() - 60),
-                    LAYER_FOREGROUND1);
+                context.draw_text(gold_text, i->map_message, 
+                    Vector(screen->w/2, screen->h - white_text->get_height() - 60),
+                    CENTER_ALLIGN, 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);
+    context.draw_text(gold_text, passive_message, 
+            Vector(screen->w/2, screen->h - white_text->get_height() - 60),
+            CENTER_ALLIGN, LAYER_FOREGROUND1);
 }
 
 void
@@ -1174,38 +1193,60 @@ WorldMap::savegame(const std::string& 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"
-      << "  (levels\n";
-  
   for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
     {
       if (i->solved && !i->level_name.empty())
         {
-          out << "     (level (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
@@ -1213,11 +1254,13 @@ WorldMap::loadgame(const std::string& filename)
 {
   std::cout << "loadgame: " << filename << std::endl;
   savegame_file = filename;
-  map_filename = "icyisland.stwm";
 
   if (access(filename.c_str(), F_OK) != 0)
     {
     load_map();
+
+    player_status.reset();
+
     return;
     }
   
@@ -1241,11 +1284,12 @@ WorldMap::loadgame(const std::string& filename)
   LispReader reader(cur);
 
   /* Get the Map filename and then load it before setting special_tile settings */
+  std::string cur_map_filename = map_filename;
   reader.read_string("map", map_filename);
-  load_map(); 
+  if(cur_map_filename != map_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)
@@ -1283,13 +1327,17 @@ WorldMap::loadgame(const std::string& filename)
               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;
+                    }
                 }
             }
 
@@ -1298,6 +1346,8 @@ WorldMap::loadgame(const std::string& filename)
     }
  
   lisp_free(savegame);
+
+  calculate_total_stats();
 }
 
 void