#include <world.hpp>
Public Member Functions | |
World () | |
~World () | |
void | set_savegame_filename (const std::string &filename) |
void | load (const std::string &filename) |
void | save_state () |
void | load_state () |
unsigned int | get_num_levels () const |
const std::string & | get_level_filename (unsigned int i) const |
const std::string & | get_basedir () const |
const std::string & | get_title () const |
PlayerStatus * | get_player_status () const |
returns player status | |
void | run () |
Static Public Member Functions | |
static World * | current () |
Public Attributes | |
bool | hide_from_contribs |
bool | is_levelset |
Private Attributes | |
std::vector< std::string > | levels |
std::string | basedir |
std::string | savegame_filename |
HSQOBJECT | state_table |
squirrel table that saves persistent state (about the world) | |
HSQOBJECT | world_thread |
std::string | title |
std::string | description |
std::auto_ptr< PlayerStatus > | player_status |
Static Private Attributes | |
static World * | current_ |
Definition at line 26 of file world.hpp.
World::World | ( | ) |
Definition at line 35 of file world.cpp.
References hide_from_contribs, is_levelset, player_status, and world_thread.
00035 : 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 }
World::~World | ( | ) |
Definition at line 54 of file world.cpp.
References current_, scripting::global_vm, and world_thread.
00055 { 00056 sq_release(scripting::global_vm, &world_thread); 00057 if(current_ == this) 00058 current_ = NULL; 00059 }
static World* World::current | ( | ) | [inline, static] |
Definition at line 29 of file world.hpp.
References current_.
Referenced by scripting::load_worldmap(), worldmap::WorldMap::save_state(), and scripting::save_state().
00030 { 00031 return current_; 00032 }
void World::set_savegame_filename | ( | const std::string & | filename | ) |
Definition at line 62 of file world.cpp.
References FileSystem::dirname(), and savegame_filename.
Referenced by TitleScreen::start_game().
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 }
void World::load | ( | const std::string & | filename | ) |
Definition at line 84 of file world.cpp.
References basedir, description, FileSystem::dirname(), lisp::Lisp::get(), lisp::Lisp::get_lisp(), StringUtil::has_suffix(), hide_from_contribs, is_levelset, levels, log_warning, StringUtil::numeric_less(), lisp::Parser::parse(), and title.
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 }
void World::save_state | ( | ) |
Definition at line 159 of file world.cpp.
References lisp::Writer::end_list(), scripting::global_vm, player_status, scripting::save_squirrel_table(), savegame_filename, lisp::Writer::start_list(), title, and lisp::Writer::write().
Referenced by worldmap::WorldMap::save_state(), and scripting::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 }
void World::load_state | ( | ) |
Definition at line 196 of file world.cpp.
References lisp::Lisp::get(), lisp::Lisp::get_lisp(), scripting::global_vm, scripting::load_squirrel_table(), log_fatal, lisp::Parser::parse(), player_status, and savegame_filename.
Referenced by run().
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 }
unsigned int World::get_num_levels | ( | ) | const |
Definition at line 247 of file world.cpp.
References levels.
Referenced by ContribWorldMenu::ContribWorldMenu().
00248 { 00249 return levels.size(); 00250 }
const std::string & World::get_level_filename | ( | unsigned int | i | ) | const |
Definition at line 241 of file world.cpp.
References levels.
Referenced by ContribWorldMenu::check_menu(), and ContribWorldMenu::ContribWorldMenu().
00242 { 00243 return levels[i]; 00244 }
const std::string & World::get_basedir | ( | ) | const |
Definition at line 253 of file world.cpp.
References basedir.
Referenced by TitleScreen::start_game().
00254 { 00255 return basedir; 00256 }
const std::string & World::get_title | ( | ) | const |
Definition at line 259 of file world.cpp.
References title.
Referenced by ContribWorldMenu::ContribWorldMenu().
00260 { 00261 return title; 00262 }
PlayerStatus* World::get_player_status | ( | ) | const [inline] |
returns player status
Definition at line 53 of file world.hpp.
References player_status.
Referenced by ContribWorldMenu::check_menu(), and run().
00053 { return player_status.get(); }
void World::run | ( | ) |
Definition at line 126 of file world.cpp.
References basedir, scripting::compile_and_run(), scripting::create_thread(), current_, g_screen_manager, get_player_status(), scripting::global_vm, load_state(), scripting::object_to_vm(), ScreenManager::push_screen(), and world_thread.
Referenced by TitleScreen::start_game().
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 }
World * World::current_ [static, private] |
std::vector<std::string> World::levels [private] |
Definition at line 59 of file world.hpp.
Referenced by get_level_filename(), get_num_levels(), and load().
std::string World::basedir [private] |
std::string World::savegame_filename [private] |
Definition at line 61 of file world.hpp.
Referenced by load_state(), save_state(), and set_savegame_filename().
HSQOBJECT World::state_table [private] |
HSQOBJECT World::world_thread [private] |
std::string World::title [private] |
std::string World::description [private] |
std::auto_ptr<PlayerStatus> World::player_status [private] |
Definition at line 67 of file world.hpp.
Referenced by get_player_status(), load_state(), save_state(), and World().
bool World::is_levelset |
Definition at line 71 of file world.hpp.
Referenced by ContribMenu::check_menu(), load(), and World().