2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "supertux/level.hpp"
19 #include "lisp/list_iterator.hpp"
20 #include "lisp/parser.hpp"
21 #include "object/bonus_block.hpp"
22 #include "object/coin.hpp"
23 #include "supertux/sector.hpp"
24 #include "supertux/tile_manager.hpp"
25 #include "supertux/tile_set.hpp"
26 #include "trigger/secretarea_trigger.hpp"
46 for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i)
53 Level::load(const std::string& filepath)
58 const lisp::Lisp* root = parser.parse(filepath);
60 const lisp::Lisp* level = root->get_lisp("supertux-level");
62 throw std::runtime_error("file is not a supertux-level file.");
65 level->get("version", version);
67 log_info << "level uses old format: version 1" << std::endl;
68 tileset = tile_manager->get_tileset("images/tiles.strf");
69 load_old_format(*level);
73 const lisp::Lisp* tilesets_lisp = level->get_lisp("tilesets");
74 if(tilesets_lisp != NULL) {
75 tileset = tile_manager->parse_tileset_definition(*tilesets_lisp);
78 std::string tileset_name;
79 if(level->get("tileset", tileset_name)) {
81 log_warning << "multiple tilesets specified in level" << std::endl;
83 tileset = tile_manager->get_tileset(tileset_name);
86 /* load default tileset */
88 tileset = tile_manager->get_tileset("images/tiles.strf");
90 current_tileset = tileset;
95 lisp::ListIterator iter(level);
97 const std::string& token = iter.item();
98 if(token == "version") {
99 iter.value()->get(version);
101 log_warning << "level format newer than application" << std::endl;
103 } else if(token == "tileset" || token == "tilesets") {
105 } else if(token == "name") {
106 iter.value()->get(name);
107 } else if(token == "author") {
108 iter.value()->get(author);
109 } else if(token == "contact") {
110 iter.value()->get(contact);
111 } else if(token == "license") {
112 iter.value()->get(license);
113 } else if(token == "on-menukey-script") {
114 iter.value()->get(on_menukey_script);
115 } else if(token == "sector") {
116 Sector* sector = new Sector(this);
117 sector->parse(*(iter.lisp()));
120 log_warning << "Unknown token '" << token << "' in level file" << std::endl;
125 log_warning << "The level author did not specify a license for this level. You might not be allowed to share it." << std::endl;
128 } catch(std::exception& e) {
129 std::stringstream msg;
130 msg << "Problem when reading level '" << filepath << "': " << e.what();
131 throw std::runtime_error(msg.str());
134 current_tileset = NULL;
138 Level::load_old_format(const Reader& reader)
140 reader.get("name", name);
141 reader.get("author", author);
143 Sector* sector = new Sector(this);
144 sector->parse_old_format(reader);
149 Level::add_sector(Sector* sector)
151 Sector* test = get_sector(sector->get_name());
153 throw std::runtime_error("Trying to add 2 sectors with same name");
155 sectors.push_back(sector);
159 Level::get_sector(const std::string& name)
161 for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i) {
163 if(sector->get_name() == name)
171 Level::get_sector_count()
173 return sectors.size();
177 Level::get_sector(size_t num)
179 return sectors.at(num);
183 Level::get_total_coins()
186 for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i) {
188 for(Sector::GameObjects::iterator o = sector->gameobjects.begin();
189 o != sector->gameobjects.end(); ++o) {
190 Coin* coin = dynamic_cast<Coin*> (*o);
196 BonusBlock *block = dynamic_cast<BonusBlock*> (*o);
199 if (block->contents == BonusBlock::CONTENT_COIN)
205 // FIXME: do we want this? q.v. src/object/oneup.cpp
206 else if (block->contents == BonusBlock::CONTENT_1UP)
219 Level::get_total_badguys()
221 int total_badguys = 0;
222 for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i)
223 total_badguys += (*i)->get_total_badguys();
224 return total_badguys;
228 Level::get_total_secrets()
230 int total_secrets = 0;
231 for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i)
232 total_secrets += (*i)->get_total_count<SecretAreaTrigger>();
233 return total_secrets;