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