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