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