* Some Mac fixes that don't break Linux (inclusion of stddef.h for size_t, explicit...
[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 "msg.hpp"
34
35 static bool has_suffix(const std::string& data, const std::string& suffix)
36 {
37   if (data.length() >= suffix.length())
38     return data.compare(data.length() - suffix.length(), suffix.length(), suffix) == 0;
39   else
40     return false;
41 }
42
43 World::World()
44 {
45   is_levelset = true;
46   hide_from_contribs = false;
47 }
48
49 World::~World()
50 {
51 }
52
53 void
54 World::load(const std::string& filename)
55 {
56   basedir = FileSystem::dirname(filename);
57   
58   lisp::Parser parser;
59   std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
60
61   const lisp::Lisp* info = root->get_lisp("supertux-world");
62   if(info == NULL)
63     info = root->get_lisp("supertux-level-subset");
64   if(info == NULL)
65     throw std::runtime_error("File is not a world or levelsubset file");
66
67   hide_from_contribs = false;
68   is_levelset = true;
69
70   info->get("title", title);
71   info->get("description", description);
72   info->get("levelset", is_levelset);
73   info->get_vector("levels", levels);
74   info->get("hide-from-contribs", hide_from_contribs);
75
76   // Level info file doesn't define any levels, so read the
77   // directory to see what we can find
78       
79   std::string path = basedir + "/";
80   char** files = PHYSFS_enumerateFiles(path.c_str());
81   if(!files) {
82     msg_warning << "Couldn't read subset dir '" << path << "'" << std::endl;
83     return;
84   }
85
86   for(const char* const* filename = files; *filename != 0; ++filename) {
87     if(has_suffix(*filename, ".stl")) {
88       levels.push_back(path + *filename);
89     }
90   }
91   PHYSFS_freeList(files);
92 }
93
94 void
95 World::run()
96 {
97   std::string filename = basedir + "/world.nut";
98   IFileStream in(filename);
99
100   HSQUIRRELVM vm = script_manager->create_thread();
101   Scripting::compile_and_run(vm, in, filename);
102 }
103
104 const std::string&
105 World::get_level_filename(unsigned int i) const
106 {
107   return levels[i];
108 }
109
110 unsigned int
111 World::get_num_levels() const
112 {
113   return levels.size();
114 }
115