Massive copyright update. I'm sorry if I'm crediting Matze for something he didn...
[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.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     using namespace WorldMapNS;
148     // fallback try to load worldmap
149     std::auto_ptr<WorldMap> worldmap (new WorldMap);
150     worldmap->loadmap(basedir + "worldmap.stwm");
151     main_loop->push_screen(worldmap.release());
152   }
153 }
154
155 void
156 World::save_state()
157 {
158   lisp::Writer writer(savegame_filename);
159
160   writer.start_list("supertux-savegame");
161   writer.write_int("version", 1);
162   
163   using namespace WorldMapNS;
164   if(WorldMap::current() != NULL) {
165     std::ostringstream title;
166     title << WorldMap::current()->get_title();
167     title << " (" << WorldMap::current()->solved_level_count() 
168           << "/" << WorldMap::current()->level_count() << ")";
169     writer.write_string("title", title.str());
170   }
171
172   writer.start_list("tux");
173   player_status->write(writer);
174   writer.end_list("tux");
175
176   writer.start_list("state");
177   HSQUIRRELVM vm = ScriptManager::instance->get_vm();
178   sq_pushroottable(vm);
179   sq_pushstring(vm, "state", -1);
180   if(SQ_SUCCEEDED(sq_get(vm, -2))) {
181     Scripting::save_squirrel_table(vm, -1, writer);
182     sq_pop(vm, 1);
183   }
184   sq_pop(vm, 1);
185   writer.end_list("state");
186   
187   writer.end_list("supertux-savegame");
188 }
189
190 void
191 World::load_state()
192 {
193   try {
194     lisp::Parser parser;
195     std::auto_ptr<lisp::Lisp> root (parser.parse(savegame_filename));
196
197     const lisp::Lisp* lisp = root->get_lisp("supertux-savegame");
198     if(lisp == NULL)
199       throw std::runtime_error("file is not a supertux-savegame file");
200
201     int version = 1;
202     lisp->get("version", version);
203     if(version != 1)
204       throw std::runtime_error("incompatible savegame version");
205
206     const lisp::Lisp* tux = lisp->get_lisp("tux");
207     if(tux == NULL)
208       throw std::runtime_error("No tux section in savegame");
209     player_status->read(*tux);
210
211     const lisp::Lisp* state = lisp->get_lisp("state");
212     if(state == NULL)
213       throw std::runtime_error("No state section in savegame");
214     
215     HSQUIRRELVM vm = ScriptManager::instance->get_vm();
216     sq_pushroottable(vm);
217     sq_pushstring(vm, "state", -1);
218     if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
219       sq_pop(vm, 1);
220     
221     sq_pushstring(vm, "state", -1);
222     sq_newtable(vm);
223     Scripting::load_squirrel_table(vm, -1, state);
224     if(SQ_FAILED(sq_createslot(vm, -3)))
225       throw std::runtime_error("Couldn't create state table");
226     sq_pop(vm, 1); 
227   } catch(std::exception& e) {
228     log_debug << "Couldn't load savegame: " << e.what() << std::endl;
229   }
230 }
231
232 const std::string&
233 World::get_level_filename(unsigned int i) const
234 {
235   return levels[i];
236 }
237
238 unsigned int
239 World::get_num_levels() const
240 {
241   return levels.size();
242 }
243
244 const std::string&
245 World::get_basedir() const
246 {
247   return basedir;
248 }