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