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