85f3d9c9ff0e9eab6f10d9111853910031ccd1bf
[supertux.git] / src / world.cpp
1 //  $Id: level_subset.cpp 3118 2006-03-25 17:29:08Z sommer $
2 // 
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 // 
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 //  02111-1307, USA.
20 #include <config.h>
21
22 #include <stddef.h>
23 #include <physfs.h>
24 #include <stdexcept>
25
26 #include "world.hpp"
27 #include "file_system.hpp"
28 #include "lisp/parser.hpp"
29 #include "lisp/lisp.hpp"
30 #include "physfs/physfs_stream.hpp"
31 #include "script_manager.hpp"
32 #include "scripting/wrapper_util.hpp"
33 #include "scripting/serialize.hpp"
34 #include "msg.hpp"
35 #include "worldmap.hpp"
36 #include "mainloop.hpp"
37
38 static bool has_suffix(const std::string& data, const std::string& suffix)
39 {
40   if (data.length() >= suffix.length())
41     return data.compare(data.length() - suffix.length(), suffix.length(), suffix) == 0;
42   else
43     return false;
44 }
45
46 World* World::current_ = NULL;
47
48 World::World()
49 {
50   is_levelset = true;
51   hide_from_contribs = false;
52 }
53
54 World::~World()
55 {
56   if(current_ == this)
57     current_ = NULL;
58 }
59
60 void
61 World::set_savegame_filename(const std::string& filename)
62 {
63   this->savegame_filename = filename;
64 }
65
66 void
67 World::load(const std::string& filename)
68 {
69   basedir = FileSystem::dirname(filename);
70   
71   lisp::Parser parser;
72   std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
73
74   const lisp::Lisp* info = root->get_lisp("supertux-world");
75   if(info == NULL)
76     info = root->get_lisp("supertux-level-subset");
77   if(info == NULL)
78     throw std::runtime_error("File is not a world or levelsubset file");
79
80   hide_from_contribs = false;
81   is_levelset = true;
82
83   info->get("title", title);
84   info->get("description", description);
85   info->get("levelset", is_levelset);
86   info->get_vector("levels", levels);
87   info->get("hide-from-contribs", hide_from_contribs);
88
89   // Level info file doesn't define any levels, so read the
90   // directory to see what we can find
91       
92   std::string path = basedir + "/";
93   char** files = PHYSFS_enumerateFiles(path.c_str());
94   if(!files) {
95     msg_warning << "Couldn't read subset dir '" << path << "'" << std::endl;
96     return;
97   }
98
99   for(const char* const* filename = files; *filename != 0; ++filename) {
100     if(has_suffix(*filename, ".stl")) {
101       levels.push_back(path + *filename);
102     }
103   }
104   PHYSFS_freeList(files);
105 }
106
107 void
108 World::run()
109 {
110   current_ = this;
111   
112   // create new squirrel table for persisten game state
113   HSQUIRRELVM vm = ScriptManager::instance->get_vm();
114
115   sq_pushroottable(vm);
116   sq_pushstring(vm, "state", -1);
117   sq_newtable(vm);
118   if(SQ_FAILED(sq_createslot(vm, -3)))
119     throw Scripting::SquirrelError(vm, "Couldn't create state table");
120   sq_pop(vm, 1);
121
122   load_state();
123   
124   std::string filename = basedir + "/world.nut";
125   try {
126     IFileStream in(filename);
127
128     HSQUIRRELVM new_vm = ScriptManager::instance->create_thread();
129     Scripting::compile_and_run(new_vm, in, filename);
130   } catch(std::exception& e) {
131     using namespace WorldMapNS;
132     // fallback try to load worldmap
133     std::auto_ptr<WorldMap> worldmap (new WorldMap);
134     worldmap->loadmap(basedir + "worldmap.stwm");
135     main_loop->push_screen(worldmap.release());
136   }
137 }
138
139 void
140 World::save_state()
141 {
142   lisp::Writer writer(savegame_filename);
143
144   writer.start_list("supertux-savegame");
145   writer.write_int("version", 1);
146   
147   using namespace WorldMapNS;
148   if(WorldMap::current() != NULL) {
149     std::ostringstream title;
150     title << WorldMap::current()->get_title();
151     title << " (" << WorldMap::current()->solved_level_count() 
152           << "/" << WorldMap::current()->level_count() << ")";
153     writer.write_string("title", title.str());
154   }
155
156   writer.start_list("tux");
157   player_status->write(writer);
158   writer.end_list("tux");
159
160   writer.start_list("state");
161   HSQUIRRELVM vm = ScriptManager::instance->get_vm();
162   sq_pushroottable(vm);
163   sq_pushstring(vm, "state", -1);
164   if(SQ_SUCCEEDED(sq_get(vm, -2))) {
165     Scripting::save_squirrel_table(vm, -1, writer);
166     sq_pop(vm, 1);
167   }
168   sq_pop(vm, 1);
169   writer.end_list("state");
170   
171   writer.end_list("supertux-savegame");
172 }
173
174 void
175 World::load_state()
176 {
177   try {
178     lisp::Parser parser;
179     std::auto_ptr<lisp::Lisp> root (parser.parse(savegame_filename));
180
181     const lisp::Lisp* lisp = root->get_lisp("supertux-savegame");
182     if(lisp == NULL)
183       throw std::runtime_error("file is not a supertux-savegame file");
184
185     int version = 1;
186     lisp->get("version", version);
187     if(version != 1)
188       throw std::runtime_error("incompatible savegame version");
189
190     const lisp::Lisp* tux = lisp->get_lisp("tux");
191     if(tux == NULL)
192       throw std::runtime_error("No tux section in savegame");
193     player_status->read(*tux);
194
195     const lisp::Lisp* state = lisp->get_lisp("state");
196     if(state == NULL)
197       throw std::runtime_error("No state section in savegame");
198     
199     HSQUIRRELVM vm = ScriptManager::instance->get_vm();
200     sq_pushroottable(vm);
201     sq_pushstring(vm, "state", -1);
202     if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
203       sq_pop(vm, 1);
204     
205     sq_pushstring(vm, "state", -1);
206     sq_newtable(vm);
207     Scripting::load_squirrel_table(vm, -1, state);
208     if(SQ_FAILED(sq_createslot(vm, -3)))
209       throw std::runtime_error("Couldn't create state table");
210     sq_pop(vm, 1); 
211   } catch(std::exception& e) {
212     msg_debug << "Couldn't load savegame: " << e.what() << std::endl;
213   }
214 }
215
216 const std::string&
217 World::get_level_filename(unsigned int i) const
218 {
219   return levels[i];
220 }
221
222 unsigned int
223 World::get_num_levels() const
224 {
225   return levels.size();
226 }
227
228 const std::string&
229 World::get_basedir() const
230 {
231   return basedir;
232 }