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