Made Mr_Tree graphic smaller, from 99 to 85 pixels.
[supertux.git] / src / worldmap.cpp
index 8649852..2896d64 100644 (file)
@@ -25,6 +25,7 @@
 #include <stdexcept>
 #include <sstream>
 #include <unistd.h>
+#include <physfs.h>
 
 #include "gettext.hpp"
 #include "video/surface.hpp"
@@ -52,6 +53,7 @@
 #include "object/background.hpp"
 #include "object/tilemap.hpp"
 #include "scripting/script_interpreter.hpp"
+#include "exceptions.hpp"
 
 Menu* worldmap_menu  = 0;
 
@@ -116,7 +118,7 @@ string_to_direction(const std::string& directory)
 Tux::Tux(WorldMap* worldmap_)
   : worldmap(worldmap_)
 {
-  tux_sprite = sprite_manager->create("worldmaptux");
+  tux_sprite = sprite_manager->create("images/worldmap/common/tux.sprite");
   
   offset = 0;
   moving = false;
@@ -134,19 +136,19 @@ Tux::draw(DrawingContext& context)
 {
   switch (player_status->bonus) {
     case GROWUP_BONUS:
-      tux_sprite->set_action("large");
+      tux_sprite->set_action(moving ? "large-walking" : "large-stop");
       break;
     case FIRE_BONUS:
-      tux_sprite->set_action("fire");
+      tux_sprite->set_action(moving ? "fire-walking" : "fire-stop");
       break;
     case NO_BONUS:
-      tux_sprite->set_action("small");
+      tux_sprite->set_action(moving ? "small-walking" : "small-stop");
       break;
     default:
 #ifdef DEBUG
       std::cerr << "Bonus type not handled in worldmap.\n";
 #endif
-      tux_sprite->set_action("large");
+      tux_sprite->set_action("large-stop");
       break;
   }
 
@@ -356,7 +358,7 @@ WorldMap::WorldMap()
   teleporterdot = new Surface("images/worldmap/common/teleporterdot.png");
 
   name = "<no title>";
-  music = "salcon.ogg";
+  music = "music/salcon.ogg";
   intro_displayed = false;
 
   total_stats.reset();
@@ -527,6 +529,17 @@ WorldMap::parse_level_tile(const lisp::Lisp* level_lisp)
   level_lisp->get("quit-worldmap", level.quit_worldmap);
 
   level_lisp->get("name", level.name);
+  
+  if (!PHYSFS_exists((levels_path + level.name).c_str()))
+  {
+       // Do we want to bail out instead...? We might get messages from modders
+       // who can't make their levels run because they're too dumb to watch
+       // their terminals...
+    std::cerr << "Error: level file '" << level.name
+      << "' does not exist and will not be added to the worldmap." << std::endl;
+    return;
+  }
+
   level_lisp->get("x", level.pos.x);
   level_lisp->get("y", level.pos.y);
 
@@ -595,7 +608,7 @@ WorldMap::get_input()
       Menu::current()->event(event);
     main_controller->process_event(event);
     if(event.type == SDL_QUIT)
-      throw std::runtime_error("Received window close");
+      throw graceful_shutdown();
   }
 }
 
@@ -835,7 +848,7 @@ WorldMap::update(float delta)
               break;
             }
 
-          sound_manager->play_music(std::string("music/") + music);
+          sound_manager->play_music(music);
           Menu::set_current(0);
           if (!savegame_file.empty())
             savegame(savegame_file);
@@ -988,7 +1001,7 @@ WorldMap::display()
 
   quit = false;
 
-  sound_manager->play_music(std::string("music/") + music);
+  sound_manager->play_music(music);
 
   if(!intro_displayed && intro_script != "") {
     try {
@@ -1053,6 +1066,19 @@ WorldMap::savegame(const std::string& filename)
   if(filename == "")
     return;
 
+  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;
@@ -1111,74 +1137,83 @@ WorldMap::loadgame(const std::string& filename)
 {
   std::cout << "loadgame: " << filename << std::endl;
   savegame_file = filename;
-
-  try {
-    lisp::Parser parser;
-    std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
   
-    const lisp::Lisp* savegame = root->get_lisp("supertux-savegame");
-    if(!savegame)
-      throw std::runtime_error("File is not a supertux-savegame file.");
-
-    /* Get the Map filename and then load it before setting level settings */
-    std::string cur_map_filename = map_filename;
-    savegame->get("map", map_filename);
-    load_map(); 
-
-    savegame->get("intro-displayed", intro_displayed);
-    savegame->get("lives", player_status->lives);
-    savegame->get("coins", player_status->coins);
-    savegame->get("max-score-multiplier", player_status->max_score_multiplier);
-    if (player_status->lives < 0)
+  if (PHYSFS_exists(filename.c_str())) // savegame exists
+  {
+    try {
+      lisp::Parser parser;
+      
+      std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
+    
+      const lisp::Lisp* savegame = root->get_lisp("supertux-savegame");
+      if(!savegame)
+        throw std::runtime_error("File is not a supertux-savegame file.");
+
+      /* Get the Map filename and then load it before setting level settings */
+      std::string cur_map_filename = map_filename;
+      savegame->get("map", map_filename);
+      load_map(); 
+
+      savegame->get("intro-displayed", intro_displayed);
+      savegame->get("lives", player_status->lives);
+      savegame->get("coins", player_status->coins);
+      savegame->get("max-score-multiplier", player_status->max_score_multiplier);
+      if (player_status->lives < 0)
       player_status->reset();
 
-    const lisp::Lisp* tux_lisp = savegame->get_lisp("tux");
-    if(tux)
-    {
-      Vector p;
-      std::string back_str = "none";
+      const lisp::Lisp* tux_lisp = savegame->get_lisp("tux");
+      if(tux)
+      {
+        Vector p;
+        std::string back_str = "none";
 
-      tux_lisp->get("x", p.x);
-      tux_lisp->get("y", p.y);
-      tux_lisp->get("back", back_str);
-      player_status->read(*tux_lisp);
+        tux_lisp->get("x", p.x);
+        tux_lisp->get("y", p.y);
+        tux_lisp->get("back", back_str);
+          player_status->read(*tux_lisp);
       
-      tux->back_direction = string_to_direction(back_str);      
-      tux->set_tile_pos(p);
-    }
+        tux->back_direction = string_to_direction(back_str);      
+        tux->set_tile_pos(p);
+      }
 
-    const lisp::Lisp* levels_lisp = savegame->get_lisp("levels");
-    if(levels_lisp) {
-      lisp::ListIterator iter(levels_lisp);
-      while(iter.next()) {
-        if(iter.item() == "level") {
-          std::string name;
-          bool solved = false;
+      const lisp::Lisp* levels_lisp = savegame->get_lisp("levels");
+      if(levels_lisp) {
+        lisp::ListIterator iter(levels_lisp);
+        while(iter.next()) {
+          if(iter.item() == "level") {
+            std::string name;
+            bool solved = false;
 
-          const lisp::Lisp* level = iter.lisp();
-          level->get("name", name);
-          level->get("solved", solved);
+            const lisp::Lisp* level = iter.lisp();
+            level->get("name", name);
+            level->get("solved", solved);
 
-          for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
-          {
-            if (name == i->name)
+            for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
             {
-              i->solved = solved;
-              i->sprite->set_action(solved ? "solved" : "default");
-              i->statistics.parse(*level);
-              break;
+              if (name == i->name)
+              {
+                i->solved = solved;
+                i->sprite->set_action(solved ? "solved" : "default");
+                i->statistics.parse(*level);
+                break;
+              }
             }
+          } else {
+            std::cerr << "Unknown token '" << iter.item() 
+                      << "' in levels block in worldmap.\n";
           }
-        } else {
-          std::cerr << "Unknown token '" << iter.item() 
-                    << "' in levels block in worldmap.\n";
         }
       }
+    } catch(std::exception& e) {
+      std::cerr << "Problem loading game '" << filename << "': " << e.what() 
+                << "\n";
+      load_map();
+      player_status->reset();
     }
-  } catch(std::exception& e) {
-    std::cerr << "Problem loading game '" << filename << "': " << e.what() 
-              << "\n";
-    load_map();
+  }
+  else
+  {
+       load_map();
     player_status->reset();
   }