several bugfixes to squirrel serialisation code, serializer/load worldmap state from...
[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
36 static bool has_suffix(const std::string& data, const std::string& suffix)
37 {
38   if (data.length() >= suffix.length())
39     return data.compare(data.length() - suffix.length(), suffix.length(), suffix) == 0;
40   else
41     return false;
42 }
43
44 World* World::current_ = NULL;
45
46 World::World()
47 {
48   is_levelset = true;
49   hide_from_contribs = false;
50 }
51
52 World::~World()
53 {
54   if(current_ == this)
55     current_ = NULL;
56 }
57
58 void
59 World::set_savegame_filename(const std::string& filename)
60 {
61   this->savegame_filename = filename;
62 }
63
64 void
65 World::load(const std::string& filename)
66 {
67   basedir = FileSystem::dirname(filename);
68   
69   lisp::Parser parser;
70   std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
71
72   const lisp::Lisp* info = root->get_lisp("supertux-world");
73   if(info == NULL)
74     info = root->get_lisp("supertux-level-subset");
75   if(info == NULL)
76     throw std::runtime_error("File is not a world or levelsubset file");
77
78   hide_from_contribs = false;
79   is_levelset = true;
80
81   info->get("title", title);
82   info->get("description", description);
83   info->get("levelset", is_levelset);
84   info->get_vector("levels", levels);
85   info->get("hide-from-contribs", hide_from_contribs);
86
87   // Level info file doesn't define any levels, so read the
88   // directory to see what we can find
89       
90   std::string path = basedir + "/";
91   char** files = PHYSFS_enumerateFiles(path.c_str());
92   if(!files) {
93     msg_warning << "Couldn't read subset dir '" << path << "'" << std::endl;
94     return;
95   }
96
97   for(const char* const* filename = files; *filename != 0; ++filename) {
98     if(has_suffix(*filename, ".stl")) {
99       levels.push_back(path + *filename);
100     }
101   }
102   PHYSFS_freeList(files);
103 }
104
105 void
106 World::run()
107 {
108   current_ = this;
109   
110   // create new squirrel table for persisten game state
111   HSQUIRRELVM vm = ScriptManager::instance->get_vm();
112
113   sq_pushroottable(vm);
114   sq_pushstring(vm, "state", -1);
115   sq_newtable(vm);
116   if(SQ_FAILED(sq_createslot(vm, -3)))
117     throw Scripting::SquirrelError(vm, "Couldn't create state table");
118   sq_pop(vm, 1);
119
120   load_state();
121   
122   std::string filename = basedir + "/world.nut";
123   IFileStream in(filename);
124
125   HSQUIRRELVM new_vm = ScriptManager::instance->create_thread();
126   Scripting::compile_and_run(new_vm, in, filename);
127 }
128
129 void
130 World::save_state()
131 {
132   lisp::Writer writer(savegame_filename);
133
134   writer.start_list("supertux-savegame");
135   writer.write_int("version", 1);
136
137   writer.start_list("tux");
138   player_status->write(writer);
139   writer.end_list("tux");
140
141   writer.start_list("state");
142   HSQUIRRELVM vm = ScriptManager::instance->get_vm();
143   sq_pushroottable(vm);
144   sq_pushstring(vm, "state", -1);
145   if(SQ_SUCCEEDED(sq_get(vm, -2))) {
146     Scripting::save_squirrel_table(vm, -1, writer);
147     sq_pop(vm, 1);
148   }
149   sq_pop(vm, 1);
150   writer.end_list("state");
151   
152   writer.end_list("supertux-savegame");
153 }
154
155 void
156 World::load_state()
157 {
158   try {
159     lisp::Parser parser;
160     std::auto_ptr<lisp::Lisp> root (parser.parse(savegame_filename));
161
162     const lisp::Lisp* lisp = root->get_lisp("supertux-savegame");
163     if(lisp == NULL)
164       throw std::runtime_error("file is not a supertux-savegame file");
165
166     int version = 1;
167     lisp->get("version", version);
168     if(version != 1)
169       throw std::runtime_error("incompatible savegame version");
170
171     const lisp::Lisp* tux = lisp->get_lisp("tux");
172     if(tux == NULL)
173       throw std::runtime_error("No tux section in savegame");
174     player_status->read(*tux);
175
176     const lisp::Lisp* state = lisp->get_lisp("state");
177     if(state == NULL)
178       throw std::runtime_error("No state section in savegame");
179     
180     HSQUIRRELVM vm = ScriptManager::instance->get_vm();
181     sq_pushroottable(vm);
182     sq_pushstring(vm, "state", -1);
183     if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
184       sq_pop(vm, 1);
185     
186     sq_pushstring(vm, "state", -1);
187     sq_newtable(vm);
188     Scripting::load_squirrel_table(vm, -1, state);
189     if(SQ_FAILED(sq_createslot(vm, -3)))
190       throw std::runtime_error("Couldn't create state table");
191     sq_pop(vm, 1); 
192   } catch(std::exception& e) {
193     msg_debug << "Couldn't load savegame: " << e.what() << std::endl;
194   }
195 }
196
197 const std::string&
198 World::get_level_filename(unsigned int i) const
199 {
200   return levels[i];
201 }
202
203 unsigned int
204 World::get_num_levels() const
205 {
206   return levels.size();
207 }
208