Massive copyright update. I'm sorry if I'm crediting Matze for something he didn...
[supertux.git] / src / world.cpp
index fb82096..4d9d811 100644 (file)
@@ -1,5 +1,5 @@
-//  $Id: level_subset.cpp 3118 2006-03-25 17:29:08Z sommer $
-// 
+//  $Id$
+//
 //  SuperTux
 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
 //
@@ -12,7 +12,7 @@
 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 //  GNU General Public License for more details.
-// 
+//
 //  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
@@ -31,7 +31,9 @@
 #include "script_manager.hpp"
 #include "scripting/wrapper_util.hpp"
 #include "scripting/serialize.hpp"
-#include "msg.hpp"
+#include "log.hpp"
+#include "worldmap.hpp"
+#include "mainloop.hpp"
 
 static bool has_suffix(const std::string& data, const std::string& suffix)
 {
@@ -59,6 +61,22 @@ void
 World::set_savegame_filename(const std::string& filename)
 {
   this->savegame_filename = filename;
+  // make sure the savegame directory exists
+  std::string dirname = FileSystem::dirname(filename);
+  if(!PHYSFS_exists(dirname.c_str())) {
+      if(PHYSFS_mkdir(dirname.c_str())) {
+          std::ostringstream msg;
+          msg << "Couldn't create directory for savegames '"
+              << dirname << "': " <<PHYSFS_getLastError();
+          throw std::runtime_error(msg.str());
+      }
+  }
+  if(!PHYSFS_isDirectory(dirname.c_str())) {
+      std::ostringstream msg;
+      msg << "Savegame path '" << dirname << "' is not a directory";
+      throw std::runtime_error(msg.str());
+  }
 }
 
 void
@@ -90,7 +108,7 @@ World::load(const std::string& filename)
   std::string path = basedir + "/";
   char** files = PHYSFS_enumerateFiles(path.c_str());
   if(!files) {
-    msg_warning << "Couldn't read subset dir '" << path << "'" << std::endl;
+    log_warning << "Couldn't read subset dir '" << path << "'" << std::endl;
     return;
   }
 
@@ -120,10 +138,18 @@ World::run()
   load_state();
   
   std::string filename = basedir + "/world.nut";
-  IFileStream in(filename);
+  try {
+    IFileStream in(filename);
 
-  HSQUIRRELVM new_vm = ScriptManager::instance->create_thread();
-  Scripting::compile_and_run(new_vm, in, filename);
+    HSQUIRRELVM new_vm = ScriptManager::instance->create_thread();
+    Scripting::compile_and_run(new_vm, in, filename);
+  } catch(std::exception& e) {
+    using namespace WorldMapNS;
+    // fallback try to load worldmap
+    std::auto_ptr<WorldMap> worldmap (new WorldMap);
+    worldmap->loadmap(basedir + "worldmap.stwm");
+    main_loop->push_screen(worldmap.release());
+  }
 }
 
 void
@@ -133,6 +159,15 @@ World::save_state()
 
   writer.start_list("supertux-savegame");
   writer.write_int("version", 1);
+  
+  using namespace WorldMapNS;
+  if(WorldMap::current() != NULL) {
+    std::ostringstream title;
+    title << WorldMap::current()->get_title();
+    title << " (" << WorldMap::current()->solved_level_count() 
+          << "/" << WorldMap::current()->level_count() << ")";
+    writer.write_string("title", title.str());
+  }
 
   writer.start_list("tux");
   player_status->write(writer);
@@ -190,7 +225,7 @@ World::load_state()
       throw std::runtime_error("Couldn't create state table");
     sq_pop(vm, 1); 
   } catch(std::exception& e) {
-    msg_debug << "Couldn't load savegame: " << e.what() << std::endl;
+    log_debug << "Couldn't load savegame: " << e.what() << std::endl;
   }
 }
 
@@ -206,3 +241,8 @@ World::get_num_levels() const
   return levels.size();
 }
 
+const std::string&
+World::get_basedir() const
+{
+  return basedir;
+}