4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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
33 #include "lisp/parser.hpp"
34 #include "lisp/lisp.hpp"
35 #include "lisp/list_iterator.hpp"
36 #include "lisp/writer.hpp"
41 #include "resources.hpp"
42 #include "file_system.hpp"
43 #include "object/gameobjs.hpp"
44 #include "object/camera.hpp"
45 #include "object/tilemap.hpp"
46 #include "object/coin.hpp"
47 #include "object/block.hpp"
52 : name("noname"), author("Mr. X")
57 Level::load(const std::string& filepath)
61 const lisp::Lisp* root = parser.parse(filepath);
63 const lisp::Lisp* level = root->get_lisp("supertux-level");
65 throw std::runtime_error("file is not a supertux-level file.");
68 level->get("version", version);
70 load_old_format(*level);
74 lisp::ListIterator iter(level);
76 const std::string& token = iter.item();
77 if(token == "version") {
78 iter.value()->get(version);
80 log_warning << "level format newer than application" << std::endl;
82 } else if(token == "name") {
83 iter.value()->get(name);
84 } else if(token == "author") {
85 iter.value()->get(author);
86 } else if(token == "on-menukey-script") {
87 iter.value()->get(on_menukey_script);
88 } else if(token == "sector") {
89 Sector* sector = new Sector(this);
90 sector->parse(*(iter.lisp()));
93 log_warning << "Unknown token '" << token << "' in level file" << std::endl;
97 } catch(std::exception& e) {
98 std::stringstream msg;
99 msg << "Problem when reading level '" << filepath << "': " << e.what();
100 throw std::runtime_error(msg.str());
105 Level::load_old_format(const lisp::Lisp& reader)
107 reader.get("name", name);
108 reader.get("author", author);
110 Sector* sector = new Sector(this);
111 sector->parse_old_format(reader);
116 Level::save(const std::string& filename)
118 lisp::Writer* writer = new lisp::Writer(filename);
120 writer->write_comment("Level made using SuperTux's built-in Level Editor");
122 writer->start_list("supertux-level");
125 writer->write_int("version", version);
127 writer->write_string("name", name, true);
128 writer->write_string("author", author);
129 if(on_menukey_script != "")
130 writer->write_string("on-menukey-script", on_menukey_script);
132 for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i) {
134 writer->start_list("sector");
135 sector->write(*writer);
136 writer->end_list("sector");
139 writer->end_list("supertux-level");
146 for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i)
151 Level::add_sector(Sector* sector)
153 Sector* test = get_sector(sector->get_name());
155 throw std::runtime_error("Trying to add 2 sectors with same name");
157 sectors.push_back(sector);
161 Level::get_sector(const std::string& name)
163 for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i) {
165 if(sector->get_name() == name)
173 Level::get_sector_count()
175 return sectors.size();
179 Level::get_sector(size_t num)
181 return sectors.at(num);
185 Level::get_total_coins()
188 for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i) {
190 for(Sector::GameObjects::iterator o = sector->gameobjects.begin();
191 o != sector->gameobjects.end(); ++o) {
192 Coin* coin = dynamic_cast<Coin*> (*o);
198 BonusBlock *block = dynamic_cast<BonusBlock*> (*o);
201 if (block->contents == BonusBlock::CONTENT_COIN)
207 // FIXME: do we want this? q.v. src/object/oneup.cpp
208 else if (block->contents == BonusBlock::CONTENT_1UP)
221 Level::get_total_badguys()
223 int total_badguys = 0;
224 for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i)
225 total_badguys += (*i)->get_total_badguys();
226 return total_badguys;