2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "lisp/parser.hpp"
20 #include "lisp/writer.hpp"
21 #include "physfs/ifile_streambuf.hpp"
22 #include "scripting/serialize.hpp"
23 #include "scripting/squirrel_util.hpp"
24 #include "supertux/globals.hpp"
25 #include "supertux/screen_fade.hpp"
26 #include "supertux/screen_manager.hpp"
27 #include "supertux/player_status.hpp"
28 #include "supertux/world.hpp"
29 #include "util/file_system.hpp"
30 #include "util/reader.hpp"
31 #include "util/string_util.hpp"
32 #include "worldmap/worldmap.hpp"
34 std::unique_ptr<World>
35 World::load(const std::string& filename)
37 std::unique_ptr<World> world(new World);
38 world->load_(filename);
39 return std::move(world);
43 m_worldmap_filename(),
46 m_savegame_filename(),
50 m_player_status(new PlayerStatus),
51 m_hide_from_contribs(false),
54 sq_resetobject(&m_world_thread);
59 sq_release(scripting::global_vm, &m_world_thread);
63 World::set_savegame_filename(const std::string& filename)
65 m_savegame_filename = filename;
67 // make sure the savegame directory exists
68 std::string dirname = FileSystem::dirname(filename);
69 if(!PHYSFS_exists(dirname.c_str())) {
70 if(!PHYSFS_mkdir(dirname.c_str())) {
71 std::ostringstream msg;
72 msg << "Couldn't create directory for savegames '"
73 << dirname << "': " <<PHYSFS_getLastError();
74 throw std::runtime_error(msg.str());
78 if(!PHYSFS_isDirectory(dirname.c_str())) {
79 std::ostringstream msg;
80 msg << "Savegame path '" << dirname << "' is not a directory";
81 throw std::runtime_error(msg.str());
86 World::load_(const std::string& filename)
88 m_basedir = FileSystem::dirname(filename);
89 m_worldmap_filename = m_basedir + "worldmap.stwm";
92 const lisp::Lisp* root = parser.parse(filename);
94 const lisp::Lisp* info = root->get_lisp("supertux-world");
96 info = root->get_lisp("supertux-level-subset");
98 throw std::runtime_error("File is not a world or levelsubset file");
100 m_hide_from_contribs = false;
101 m_is_levelset = true;
103 info->get("title", m_title);
104 info->get("description", m_description);
105 info->get("levelset", m_is_levelset);
106 info->get("hide-from-contribs", m_hide_from_contribs);
108 // Level info file doesn't define any levels, so read the
109 // directory to see what we can find
111 std::string path = m_basedir;
112 char** files = PHYSFS_enumerateFiles(path.c_str());
115 log_warning << "Couldn't read subset dir '" << path << "'" << std::endl;
119 for(const char* const* filename = files; *filename != 0; ++filename)
121 if(StringUtil::has_suffix(*filename, ".stl"))
124 level.fullpath = path + *filename;
125 level.name = *filename;
126 m_levels.push_back(level);
129 PHYSFS_freeList(files);
131 std::sort(m_levels.begin(), m_levels.end(),
132 [](const Level& lhs, const Level& rhs)
134 return StringUtil::numeric_less(lhs.fullpath, rhs.fullpath);
141 // create new squirrel table for persistent game state
142 HSQUIRRELVM vm = scripting::global_vm;
144 sq_pushroottable(vm);
145 sq_pushstring(vm, "state", -1);
147 if(SQ_FAILED(sq_createslot(vm, -3)))
149 throw scripting::SquirrelError(vm, "Couldn't create state table");
157 std::string filename = m_basedir + "/world.nut";
160 IFileStreambuf ins(filename);
161 std::istream in(&ins);
163 sq_release(scripting::global_vm, &m_world_thread);
164 m_world_thread = scripting::create_thread(scripting::global_vm);
165 scripting::compile_and_run(scripting::object_to_vm(m_world_thread), in, filename);
167 catch(const std::exception& )
169 // fallback: try to load worldmap worldmap.stwm
170 g_screen_manager->push_screen(std::unique_ptr<Screen>(
171 new worldmap::WorldMap(m_basedir + "worldmap.stwm",
172 get_player_status())));
180 HSQUIRRELVM vm = scripting::global_vm;
182 lisp::Writer writer(m_savegame_filename);
184 writer.start_list("supertux-savegame");
185 writer.write("version", 1);
187 using namespace worldmap;
188 if(WorldMap::current() != NULL)
190 std::ostringstream title;
191 title << WorldMap::current()->get_title();
192 title << " (" << WorldMap::current()->solved_level_count()
193 << "/" << WorldMap::current()->level_count() << ")";
194 writer.write("title", title.str());
197 writer.start_list("tux");
198 m_player_status->write(writer);
199 writer.end_list("tux");
201 writer.start_list("state");
203 sq_pushroottable(vm);
204 sq_pushstring(vm, "state", -1);
205 if(SQ_SUCCEEDED(sq_get(vm, -2))) {
206 scripting::save_squirrel_table(vm, -1, writer);
210 writer.end_list("state");
212 writer.end_list("supertux-savegame");
218 if(!PHYSFS_exists(m_savegame_filename.c_str()))
220 log_info << m_savegame_filename << ": doesn't exist, not loading state" << std::endl;
226 HSQUIRRELVM vm = scripting::global_vm;
229 const lisp::Lisp* root = parser.parse(m_savegame_filename);
231 const lisp::Lisp* lisp = root->get_lisp("supertux-savegame");
233 throw std::runtime_error("file is not a supertux-savegame file");
236 lisp->get("version", version);
238 throw std::runtime_error("incompatible savegame version");
240 const lisp::Lisp* tux = lisp->get_lisp("tux");
242 throw std::runtime_error("No tux section in savegame");
243 m_player_status->read(*tux);
245 const lisp::Lisp* state = lisp->get_lisp("state");
247 throw std::runtime_error("No state section in savegame");
249 sq_pushroottable(vm);
250 sq_pushstring(vm, "state", -1);
251 if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
254 sq_pushstring(vm, "state", -1);
256 scripting::load_squirrel_table(vm, -1, *state);
257 if(SQ_FAILED(sq_createslot(vm, -3)))
258 throw std::runtime_error("Couldn't create state table");
261 catch(const std::exception& e)
263 log_fatal << "Couldn't load savegame: " << e.what() << std::endl;
269 World::get_level_filename(unsigned int i) const
271 return m_levels[i].fullpath;
275 World::get_num_levels() const
277 return m_levels.size();
281 World::get_num_solved_levels() const
283 int num_solved_levels = 0;
285 HSQUIRRELVM vm = scripting::global_vm;
286 int oldtop = sq_gettop(vm);
288 sq_pushroottable(vm);
289 sq_pushstring(vm, "state", -1);
290 if(SQ_FAILED(sq_get(vm, -2)))
292 log_warning << "failed to get 'state' table" << std::endl;
296 sq_pushstring(vm, "worlds", -1);
297 if(SQ_FAILED(sq_get(vm, -2)))
299 log_warning << "failed to get 'state.worlds' table" << std::endl;
303 sq_pushstring(vm, m_worldmap_filename.c_str(), -1);
304 if(SQ_FAILED(sq_get(vm, -2)))
306 log_warning << "failed to get state.worlds['" << m_worldmap_filename << "']" << std::endl;
310 sq_pushstring(vm, "levels", -1);
311 if(SQ_FAILED(sq_get(vm, -2)))
313 log_warning << "failed to get state.worlds['" << m_worldmap_filename << "'].levels" << std::endl;
317 for(auto level : m_levels)
319 sq_pushstring(vm, level.name.c_str(), -1);
320 if(SQ_FAILED(sq_get(vm, -2)))
322 log_warning << "failed to get state.worlds['" << m_worldmap_filename << "'].levels['"
323 << level.name << "']" << std::endl;
327 bool solved = scripting::read_bool(vm, "solved");
330 num_solved_levels += 1;
340 sq_settop(vm, oldtop);
342 return num_solved_levels;
346 World::get_basedir() const
352 World::get_title() const