399816cec5d1a74e200ccf589ccecddb9fd67a19
[supertux.git] / src / supertux / world.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
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.
8 //
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.
13 //
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/>.
16
17 #include "lisp/parser.hpp"
18 #include "lisp/writer.hpp"
19 #include "physfs/ifile_stream.hpp"
20 #include "scripting/serialize.hpp"
21 #include "scripting/squirrel_util.hpp"
22 #include "supertux/globals.hpp"
23 #include "supertux/screen_manager.hpp"
24 #include "supertux/player_status.hpp"
25 #include "supertux/world.hpp"
26 #include "util/file_system.hpp"
27 #include "util/reader.hpp"
28 #include "util/string_util.hpp"
29 #include "worldmap/worldmap.hpp"
30
31 World* World::current_ = NULL;
32
33 World::World() :
34   levels(),
35   basedir(),
36   savegame_filename(),
37   state_table(),
38   world_thread(),
39   title(),
40   description(),
41   player_status(),
42   hide_from_contribs(),
43   is_levelset()
44 {
45   player_status.reset(new PlayerStatus());
46
47   is_levelset = true;
48   hide_from_contribs = false;
49   sq_resetobject(&world_thread);
50 }
51
52 World::~World()
53 {
54   sq_release(scripting::global_vm, &world_thread);
55   if(current_ == this)
56     current_ = NULL;
57 }
58
59 void
60 World::set_savegame_filename(const std::string& filename)
61 {
62   this->savegame_filename = filename;
63   // make sure the savegame directory exists
64   std::string dirname = FileSystem::dirname(filename);
65   if(!PHYSFS_exists(dirname.c_str())) {
66     if(PHYSFS_mkdir(dirname.c_str())) {
67       std::ostringstream msg;
68       msg << "Couldn't create directory for savegames '"
69           << dirname << "': " <<PHYSFS_getLastError();
70       throw std::runtime_error(msg.str());
71     }
72   }
73
74   if(!PHYSFS_isDirectory(dirname.c_str())) {
75     std::ostringstream msg;
76     msg << "Savegame path '" << dirname << "' is not a directory";
77     throw std::runtime_error(msg.str());
78   }
79 }
80
81 void
82 World::load(const std::string& filename)
83 {
84   basedir = FileSystem::dirname(filename);
85
86   lisp::Parser parser;
87   const lisp::Lisp* root = parser.parse(filename);
88
89   const lisp::Lisp* info = root->get_lisp("supertux-world");
90   if(info == NULL)
91     info = root->get_lisp("supertux-level-subset");
92   if(info == NULL)
93     throw std::runtime_error("File is not a world or levelsubset file");
94
95   hide_from_contribs = false;
96   is_levelset = true;
97
98   info->get("title", title);
99   info->get("description", description);
100   info->get("levelset", is_levelset);
101   info->get("levels", levels);
102   info->get("hide-from-contribs", hide_from_contribs);
103
104   // Level info file doesn't define any levels, so read the
105   // directory to see what we can find
106
107   std::string path = basedir;
108   char** files = PHYSFS_enumerateFiles(path.c_str());
109   if(!files) {
110     log_warning << "Couldn't read subset dir '" << path << "'" << std::endl;
111     return;
112   }
113
114   for(const char* const* filename = files; *filename != 0; ++filename) {
115     if(StringUtil::has_suffix(*filename, ".stl")) {
116       levels.push_back(path + *filename);
117     }
118   }
119   PHYSFS_freeList(files);
120 }
121
122 void
123 World::run()
124 {
125   using namespace scripting;
126
127   current_ = this;
128
129   // create new squirrel table for persistent game state
130   HSQUIRRELVM vm = scripting::global_vm;
131
132   sq_pushroottable(vm);
133   sq_pushstring(vm, "state", -1);
134   sq_newtable(vm);
135   if(SQ_FAILED(sq_createslot(vm, -3)))
136     throw scripting::SquirrelError(vm, "Couldn't create state table");
137   sq_pop(vm, 1);
138
139   load_state();
140
141   std::string filename = basedir + "/world.nut";
142   try {
143     IFileStream in(filename);
144
145     sq_release(global_vm, &world_thread);
146     world_thread = create_thread(global_vm);
147     compile_and_run(object_to_vm(world_thread), in, filename);
148   } catch(std::exception& ) {
149     // fallback: try to load worldmap worldmap.stwm
150     using namespace worldmap;
151     g_screen_manager->push_screen(new WorldMap(basedir + "worldmap.stwm", get_player_status()));
152   }
153 }
154
155 void
156 World::save_state()
157 {
158   using namespace scripting;
159
160   lisp::Writer writer(savegame_filename);
161
162   writer.start_list("supertux-savegame");
163   writer.write("version", 1);
164
165   using namespace worldmap;
166   if(WorldMap::current() != NULL) {
167     std::ostringstream title;
168     title << WorldMap::current()->get_title();
169     title << " (" << WorldMap::current()->solved_level_count()
170           << "/" << WorldMap::current()->level_count() << ")";
171     writer.write("title", title.str());
172   }
173
174   writer.start_list("tux");
175   player_status->write(writer);
176   writer.end_list("tux");
177
178   writer.start_list("state");
179
180   sq_pushroottable(global_vm);
181   sq_pushstring(global_vm, "state", -1);
182   if(SQ_SUCCEEDED(sq_get(global_vm, -2))) {
183     scripting::save_squirrel_table(global_vm, -1, writer);
184     sq_pop(global_vm, 1);
185   }
186   sq_pop(global_vm, 1);
187   writer.end_list("state");
188
189   writer.end_list("supertux-savegame");
190 }
191
192 void
193 World::load_state()
194 {
195   using namespace scripting;
196
197   try {
198     lisp::Parser parser;
199     const lisp::Lisp* root = parser.parse(savegame_filename);
200
201     const lisp::Lisp* lisp = root->get_lisp("supertux-savegame");
202     if(lisp == NULL)
203       throw std::runtime_error("file is not a supertux-savegame file");
204
205     int version = 1;
206     lisp->get("version", version);
207     if(version != 1)
208       throw std::runtime_error("incompatible savegame version");
209
210     const lisp::Lisp* tux = lisp->get_lisp("tux");
211     if(tux == NULL)
212       throw std::runtime_error("No tux section in savegame");
213     player_status->read(*tux);
214
215     const lisp::Lisp* state = lisp->get_lisp("state");
216     if(state == NULL)
217       throw std::runtime_error("No state section in savegame");
218
219     sq_pushroottable(global_vm);
220     sq_pushstring(global_vm, "state", -1);
221     if(SQ_FAILED(sq_deleteslot(global_vm, -2, SQFalse)))
222       sq_pop(global_vm, 1);
223
224     sq_pushstring(global_vm, "state", -1);
225     sq_newtable(global_vm);
226     load_squirrel_table(global_vm, -1, *state);
227     if(SQ_FAILED(sq_createslot(global_vm, -3)))
228       throw std::runtime_error("Couldn't create state table");
229     sq_pop(global_vm, 1);
230   } catch(std::exception& e) {
231     log_debug << "Couldn't load savegame: " << e.what() << std::endl;
232   }
233 }
234
235 const std::string&
236 World::get_level_filename(unsigned int i) const
237 {
238   return levels[i];
239 }
240
241 unsigned int
242 World::get_num_levels() const
243 {
244   return levels.size();
245 }
246
247 const std::string&
248 World::get_basedir() const
249 {
250   return basedir;
251 }
252
253 const std::string&
254 World::get_title() const
255 {
256   return title;
257 }
258
259 /* EOF */