src/supertux/world.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include <algorithm>
00018 
00019 #include "lisp/parser.hpp"
00020 #include "lisp/writer.hpp"
00021 #include "physfs/ifile_stream.hpp"
00022 #include "scripting/serialize.hpp"
00023 #include "scripting/squirrel_util.hpp"
00024 #include "supertux/globals.hpp"
00025 #include "supertux/screen_manager.hpp"
00026 #include "supertux/player_status.hpp"
00027 #include "supertux/world.hpp"
00028 #include "util/file_system.hpp"
00029 #include "util/reader.hpp"
00030 #include "util/string_util.hpp"
00031 #include "worldmap/worldmap.hpp"
00032 
00033 World* World::current_ = NULL;
00034 
00035 World::World() :
00036   levels(),
00037   basedir(),
00038   savegame_filename(),
00039   state_table(),
00040   world_thread(),
00041   title(),
00042   description(),
00043   player_status(),
00044   hide_from_contribs(),
00045   is_levelset()
00046 {
00047   player_status.reset(new PlayerStatus());
00048 
00049   is_levelset = true;
00050   hide_from_contribs = false;
00051   sq_resetobject(&world_thread);
00052 }
00053 
00054 World::~World()
00055 {
00056   sq_release(scripting::global_vm, &world_thread);
00057   if(current_ == this)
00058     current_ = NULL;
00059 }
00060 
00061 void
00062 World::set_savegame_filename(const std::string& filename)
00063 {
00064   this->savegame_filename = filename;
00065   // make sure the savegame directory exists
00066   std::string dirname = FileSystem::dirname(filename);
00067   if(!PHYSFS_exists(dirname.c_str())) {
00068     if(!PHYSFS_mkdir(dirname.c_str())) {
00069       std::ostringstream msg;
00070       msg << "Couldn't create directory for savegames '"
00071           << dirname << "': " <<PHYSFS_getLastError();
00072       throw std::runtime_error(msg.str());
00073     }
00074   }
00075 
00076   if(!PHYSFS_isDirectory(dirname.c_str())) {
00077     std::ostringstream msg;
00078     msg << "Savegame path '" << dirname << "' is not a directory";
00079     throw std::runtime_error(msg.str());
00080   }
00081 }
00082 
00083 void
00084 World::load(const std::string& filename)
00085 {
00086   basedir = FileSystem::dirname(filename);
00087 
00088   lisp::Parser parser;
00089   const lisp::Lisp* root = parser.parse(filename);
00090 
00091   const lisp::Lisp* info = root->get_lisp("supertux-world");
00092   if(info == NULL)
00093     info = root->get_lisp("supertux-level-subset");
00094   if(info == NULL)
00095     throw std::runtime_error("File is not a world or levelsubset file");
00096 
00097   hide_from_contribs = false;
00098   is_levelset = true;
00099 
00100   info->get("title", title);
00101   info->get("description", description);
00102   info->get("levelset", is_levelset);
00103   info->get("hide-from-contribs", hide_from_contribs);
00104 
00105   // Level info file doesn't define any levels, so read the
00106   // directory to see what we can find
00107 
00108   std::string path = basedir;
00109   char** files = PHYSFS_enumerateFiles(path.c_str());
00110   if(!files) {
00111     log_warning << "Couldn't read subset dir '" << path << "'" << std::endl;
00112     return;
00113   }
00114 
00115   for(const char* const* filename = files; *filename != 0; ++filename) {
00116     if(StringUtil::has_suffix(*filename, ".stl")) {
00117       levels.push_back(path + *filename);
00118     }
00119   }
00120   PHYSFS_freeList(files);
00121 
00122   std::sort(levels.begin(), levels.end(), StringUtil::numeric_less);
00123 }
00124 
00125 void
00126 World::run()
00127 {
00128   using namespace scripting;
00129 
00130   current_ = this;
00131 
00132   // create new squirrel table for persistent game state
00133   HSQUIRRELVM vm = scripting::global_vm;
00134 
00135   sq_pushroottable(vm);
00136   sq_pushstring(vm, "state", -1);
00137   sq_newtable(vm);
00138   if(SQ_FAILED(sq_createslot(vm, -3)))
00139     throw scripting::SquirrelError(vm, "Couldn't create state table");
00140   sq_pop(vm, 1);
00141 
00142   load_state();
00143 
00144   std::string filename = basedir + "/world.nut";
00145   try {
00146     IFileStream in(filename);
00147 
00148     sq_release(global_vm, &world_thread);
00149     world_thread = create_thread(global_vm);
00150     compile_and_run(object_to_vm(world_thread), in, filename);
00151   } catch(std::exception& ) {
00152     // fallback: try to load worldmap worldmap.stwm
00153     using namespace worldmap;
00154     g_screen_manager->push_screen(new WorldMap(basedir + "worldmap.stwm", get_player_status()));
00155   }
00156 }
00157 
00158 void
00159 World::save_state()
00160 {
00161   using namespace scripting;
00162 
00163   lisp::Writer writer(savegame_filename);
00164 
00165   writer.start_list("supertux-savegame");
00166   writer.write("version", 1);
00167 
00168   using namespace worldmap;
00169   if(WorldMap::current() != NULL) {
00170     std::ostringstream title;
00171     title << WorldMap::current()->get_title();
00172     title << " (" << WorldMap::current()->solved_level_count()
00173           << "/" << WorldMap::current()->level_count() << ")";
00174     writer.write("title", title.str());
00175   }
00176 
00177   writer.start_list("tux");
00178   player_status->write(writer);
00179   writer.end_list("tux");
00180 
00181   writer.start_list("state");
00182 
00183   sq_pushroottable(global_vm);
00184   sq_pushstring(global_vm, "state", -1);
00185   if(SQ_SUCCEEDED(sq_get(global_vm, -2))) {
00186     scripting::save_squirrel_table(global_vm, -1, writer);
00187     sq_pop(global_vm, 1);
00188   }
00189   sq_pop(global_vm, 1);
00190   writer.end_list("state");
00191 
00192   writer.end_list("supertux-savegame");
00193 }
00194 
00195 void
00196 World::load_state()
00197 {
00198   using namespace scripting;
00199 
00200   if(PHYSFS_exists(savegame_filename.c_str())) {
00201     try {
00202       lisp::Parser parser;
00203       const lisp::Lisp* root = parser.parse(savegame_filename);
00204 
00205       const lisp::Lisp* lisp = root->get_lisp("supertux-savegame");
00206       if(lisp == NULL)
00207         throw std::runtime_error("file is not a supertux-savegame file");
00208 
00209       int version = 1;
00210       lisp->get("version", version);
00211       if(version != 1)
00212         throw std::runtime_error("incompatible savegame version");
00213 
00214       const lisp::Lisp* tux = lisp->get_lisp("tux");
00215       if(tux == NULL)
00216         throw std::runtime_error("No tux section in savegame");
00217       player_status->read(*tux);
00218 
00219       const lisp::Lisp* state = lisp->get_lisp("state");
00220       if(state == NULL)
00221         throw std::runtime_error("No state section in savegame");
00222 
00223       sq_pushroottable(global_vm);
00224       sq_pushstring(global_vm, "state", -1);
00225       if(SQ_FAILED(sq_deleteslot(global_vm, -2, SQFalse)))
00226         sq_pop(global_vm, 1);
00227 
00228       sq_pushstring(global_vm, "state", -1);
00229       sq_newtable(global_vm);
00230       load_squirrel_table(global_vm, -1, *state);
00231       if(SQ_FAILED(sq_createslot(global_vm, -3)))
00232         throw std::runtime_error("Couldn't create state table");
00233       sq_pop(global_vm, 1);
00234     } catch(std::exception& e) {
00235       log_fatal << "Couldn't load savegame: " << e.what() << std::endl;
00236     }
00237   }
00238 }
00239 
00240 const std::string&
00241 World::get_level_filename(unsigned int i) const
00242 {
00243   return levels[i];
00244 }
00245 
00246 unsigned int
00247 World::get_num_levels() const
00248 {
00249   return levels.size();
00250 }
00251 
00252 const std::string&
00253 World::get_basedir() const
00254 {
00255   return basedir;
00256 }
00257 
00258 const std::string&
00259 World::get_title() const
00260 {
00261   return title;
00262 }
00263 
00264 /* EOF */

Generated on Mon Jun 9 03:38:23 2014 for SuperTux by  doxygen 1.5.1