Use std::unique_ptr<> in 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 <algorithm>
18
19 #include "lisp/parser.hpp"
20 #include "lisp/writer.hpp"
21 #include "physfs/ifile_streambuf.hpp"
22 #include "scripting/serialize.hpp"
23 #include "scripting/squirrel_util.hpp"
24 #include "supertux/globals.hpp"
25 #include "supertux/screen_fade.hpp"
26 #include "supertux/screen_manager.hpp"
27 #include "supertux/player_status.hpp"
28 #include "supertux/world.hpp"
29 #include "util/file_system.hpp"
30 #include "util/reader.hpp"
31 #include "util/string_util.hpp"
32 #include "worldmap/worldmap.hpp"
33
34 World* World::current_ = NULL;
35
36 World::World() :
37   levels(),
38   basedir(),
39   savegame_filename(),
40   state_table(),
41   world_thread(),
42   title(),
43   description(),
44   player_status(),
45   hide_from_contribs(),
46   is_levelset()
47 {
48   player_status.reset(new PlayerStatus());
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("hide-from-contribs", hide_from_contribs);
105
106   // Level info file doesn't define any levels, so read the
107   // directory to see what we can find
108
109   std::string path = basedir;
110   char** files = PHYSFS_enumerateFiles(path.c_str());
111   if(!files) {
112     log_warning << "Couldn't read subset dir '" << path << "'" << std::endl;
113     return;
114   }
115
116   for(const char* const* filename = files; *filename != 0; ++filename) {
117     if(StringUtil::has_suffix(*filename, ".stl")) {
118       levels.push_back(path + *filename);
119     }
120   }
121   PHYSFS_freeList(files);
122
123   std::sort(levels.begin(), levels.end(), StringUtil::numeric_less);
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     IFileStreambuf ins(filename);
148     std::istream in(&ins);
149
150     sq_release(global_vm, &world_thread);
151     world_thread = create_thread(global_vm);
152     compile_and_run(object_to_vm(world_thread), in, filename);
153   } catch(std::exception& ) {
154     // fallback: try to load worldmap worldmap.stwm
155     using namespace worldmap;
156     g_screen_manager->push_screen(std::unique_ptr<Screen>(new WorldMap(basedir + "worldmap.stwm", get_player_status())));
157   }
158 }
159
160 void
161 World::save_state()
162 {
163   using namespace scripting;
164
165   lisp::Writer writer(savegame_filename);
166
167   writer.start_list("supertux-savegame");
168   writer.write("version", 1);
169
170   using namespace worldmap;
171   if(WorldMap::current() != NULL) {
172     std::ostringstream title;
173     title << WorldMap::current()->get_title();
174     title << " (" << WorldMap::current()->solved_level_count()
175           << "/" << WorldMap::current()->level_count() << ")";
176     writer.write("title", title.str());
177   }
178
179   writer.start_list("tux");
180   player_status->write(writer);
181   writer.end_list("tux");
182
183   writer.start_list("state");
184
185   sq_pushroottable(global_vm);
186   sq_pushstring(global_vm, "state", -1);
187   if(SQ_SUCCEEDED(sq_get(global_vm, -2))) {
188     scripting::save_squirrel_table(global_vm, -1, writer);
189     sq_pop(global_vm, 1);
190   }
191   sq_pop(global_vm, 1);
192   writer.end_list("state");
193
194   writer.end_list("supertux-savegame");
195 }
196
197 void
198 World::load_state()
199 {
200   using namespace scripting;
201
202   if(PHYSFS_exists(savegame_filename.c_str())) {
203     try {
204       lisp::Parser parser;
205       const lisp::Lisp* root = parser.parse(savegame_filename);
206
207       const lisp::Lisp* lisp = root->get_lisp("supertux-savegame");
208       if(lisp == NULL)
209         throw std::runtime_error("file is not a supertux-savegame file");
210
211       int version = 1;
212       lisp->get("version", version);
213       if(version != 1)
214         throw std::runtime_error("incompatible savegame version");
215
216       const lisp::Lisp* tux = lisp->get_lisp("tux");
217       if(tux == NULL)
218         throw std::runtime_error("No tux section in savegame");
219       player_status->read(*tux);
220
221       const lisp::Lisp* state = lisp->get_lisp("state");
222       if(state == NULL)
223         throw std::runtime_error("No state section in savegame");
224
225       sq_pushroottable(global_vm);
226       sq_pushstring(global_vm, "state", -1);
227       if(SQ_FAILED(sq_deleteslot(global_vm, -2, SQFalse)))
228         sq_pop(global_vm, 1);
229
230       sq_pushstring(global_vm, "state", -1);
231       sq_newtable(global_vm);
232       load_squirrel_table(global_vm, -1, *state);
233       if(SQ_FAILED(sq_createslot(global_vm, -3)))
234         throw std::runtime_error("Couldn't create state table");
235       sq_pop(global_vm, 1);
236     } catch(std::exception& e) {
237       log_fatal << "Couldn't load savegame: " << e.what() << std::endl;
238     }
239   }
240 }
241
242 const std::string&
243 World::get_level_filename(unsigned int i) const
244 {
245   return levels[i];
246 }
247
248 unsigned int
249 World::get_num_levels() const
250 {
251   return levels.size();
252 }
253
254 const std::string&
255 World::get_basedir() const
256 {
257   return basedir;
258 }
259
260 const std::string&
261 World::get_title() const
262 {
263   return title;
264 }
265
266 /* EOF */