refactored some supertux mainloops
[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 <physfs.h>
23 #include <stdexcept>
24
25 #include "world.hpp"
26 #include "file_system.hpp"
27 #include "lisp/parser.hpp"
28 #include "lisp/lisp.hpp"
29 #include "physfs/physfs_stream.hpp"
30 #include "scripting/script_interpreter.hpp"
31 #include "msg.hpp"
32
33 static bool has_suffix(const std::string& data, const std::string& suffix)
34 {
35   if (data.length() >= suffix.length())
36     return data.compare(data.length() - suffix.length(), suffix.length(), suffix) == 0;
37   else
38     return false;
39 }
40
41 World::World()
42 {
43   is_levelset = true;
44   hide_from_contribs = false;
45 }
46
47 World::~World()
48 {
49 }
50
51 void
52 World::load(const std::string& filename)
53 {
54   basedir = FileSystem::dirname(filename);
55   
56   lisp::Parser parser;
57   std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
58
59   const lisp::Lisp* info = root->get_lisp("supertux-world");
60   if(info == NULL)
61     info = root->get_lisp("supertux-level-subset");
62   if(info == NULL)
63     throw std::runtime_error("File is not a world or levelsubset file");
64
65   hide_from_contribs = false;
66   is_levelset = true;
67
68   info->get("title", title);
69   info->get("description", description);
70   info->get("levelset", is_levelset);
71   info->get_vector("levels", levels);
72   info->get("hide-from-contribs", hide_from_contribs);
73
74   // Level info file doesn't define any levels, so read the
75   // directory to see what we can find
76       
77   std::string path = basedir + "/";
78   char** files = PHYSFS_enumerateFiles(path.c_str());
79   if(!files) {
80     msg_warning("Couldn't read subset dir '" 
81               << path << "'");
82     return;
83   }
84
85   for(const char* const* filename = files; *filename != 0; ++filename) {
86     if(has_suffix(*filename, ".stl")) {
87       levels.push_back(path + *filename);
88     }
89   }
90   PHYSFS_freeList(files);
91 }
92
93 void
94 World::run()
95 {
96   try {
97     std::string filename = basedir + "/world.nut";
98     std::auto_ptr<ScriptInterpreter> interpeter (new ScriptInterpreter(basedir));
99     IFileStream in(filename);
100   
101     interpeter->run_script(in, filename, true);
102   } catch(std::exception& e) {
103     msg_warning("Problem running world script: " << e.what());
104   }
105 }
106
107 const std::string&
108 World::get_level_filename(unsigned int i) const
109 {
110   return levels[i];
111 }
112
113 unsigned int
114 World::get_num_levels() const
115 {
116   return levels.size();
117 }
118