hopefully fixed the crash on exit, keep sectors script bundled in the sector and...
[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()
45 {
46   is_levelset = true;
47   hide_from_contribs = false;
48 }
49
50 World::~World()
51 {
52 }
53
54 void
55 World::set_savegame_filename(const std::string& filename)
56 {
57   this->savegame_filename = filename;
58 }
59
60 void
61 World::load(const std::string& filename)
62 {
63   basedir = FileSystem::dirname(filename);
64   
65   lisp::Parser parser;
66   std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
67
68   const lisp::Lisp* info = root->get_lisp("supertux-world");
69   if(info == NULL)
70     info = root->get_lisp("supertux-level-subset");
71   if(info == NULL)
72     throw std::runtime_error("File is not a world or levelsubset file");
73
74   hide_from_contribs = false;
75   is_levelset = true;
76
77   info->get("title", title);
78   info->get("description", description);
79   info->get("levelset", is_levelset);
80   info->get_vector("levels", levels);
81   info->get("hide-from-contribs", hide_from_contribs);
82
83   // Level info file doesn't define any levels, so read the
84   // directory to see what we can find
85       
86   std::string path = basedir + "/";
87   char** files = PHYSFS_enumerateFiles(path.c_str());
88   if(!files) {
89     msg_warning << "Couldn't read subset dir '" << path << "'" << std::endl;
90     return;
91   }
92
93   for(const char* const* filename = files; *filename != 0; ++filename) {
94     if(has_suffix(*filename, ".stl")) {
95       levels.push_back(path + *filename);
96     }
97   }
98   PHYSFS_freeList(files);
99 }
100
101 void
102 World::run()
103 {
104   // create new squirrel table for persisten game state
105   HSQUIRRELVM vm = ScriptManager::instance->get_vm();
106
107   sq_pushroottable(vm);
108   sq_pushstring(vm, "state", -1);
109   sq_newtable(vm);
110   if(SQ_FAILED(sq_createslot(vm, -3)))
111     throw Scripting::SquirrelError(vm, "Couldn't create state table");
112   sq_pop(vm, 1);
113   
114   std::string filename = basedir + "/world.nut";
115   IFileStream in(filename);
116
117   HSQUIRRELVM new_vm = ScriptManager::instance->create_thread();
118   Scripting::compile_and_run(new_vm, in, filename);
119 }
120
121 void
122 World::save()
123 {
124   lisp::Writer writer(savegame_filename);
125
126   writer.start_list("supertux-savegame");
127   writer.write_int("version", 1);
128
129   writer.start_list("tux");
130   player_status->write(writer);
131   writer.end_list("tux");
132
133   writer.start_list("state");
134   HSQUIRRELVM vm = ScriptManager::instance->get_vm();
135   sq_pushroottable(vm);
136   sq_pushstring(vm, "state", -1);
137   if(SQ_SUCCEEDED(sq_get(vm, -2))) {
138     Scripting::save_squirrel_table(vm, -1, writer);
139     sq_pop(vm, 1);
140   }
141   sq_pop(vm, 1);
142   writer.end_list("state");
143   
144   writer.end_list("supertux-savegame");
145 }
146
147 const std::string&
148 World::get_level_filename(unsigned int i) const
149 {
150   return levels[i];
151 }
152
153 unsigned int
154 World::get_num_levels() const
155 {
156   return levels.size();
157 }
158