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"
51 : name("noname"), author("Mr. X")
56 Level::load(const std::string& filepath)
60 std::auto_ptr<lisp::Lisp> root (parser.parse(filepath));
62 const lisp::Lisp* level = root->get_lisp("supertux-level");
64 throw std::runtime_error("file is not a supertux-level file.");
67 level->get("version", version);
69 load_old_format(*level);
73 lisp::ListIterator iter(level);
75 const std::string& token = iter.item();
76 if(token == "version") {
77 iter.value()->get(version);
79 log_warning << "level format newer than application" << std::endl;
81 } else if(token == "name") {
82 iter.value()->get(name);
83 } else if(token == "author") {
84 iter.value()->get(author);
85 } else if(token == "on-menukey-script") {
86 iter.value()->get(on_menukey_script);
87 } else if(token == "sector") {
88 Sector* sector = new Sector(this);
89 sector->parse(*(iter.lisp()));
92 log_warning << "Unknown token '" << token << "' in level file" << std::endl;
96 } catch(std::exception& e) {
97 std::stringstream msg;
98 msg << "Problem when reading level '" << filepath << "': " << e.what();
99 throw std::runtime_error(msg.str());
104 Level::load_old_format(const lisp::Lisp& reader)
106 reader.get("name", name);
107 reader.get("author", author);
109 Sector* sector = new Sector(this);
110 sector->parse_old_format(reader);
115 Level::save(const std::string& filename)
117 lisp::Writer* writer = new lisp::Writer(filename);
119 writer->write_comment("Level made using SuperTux's built-in Level Editor");
121 writer->start_list("supertux-level");
124 writer->write_int("version", version);
126 writer->write_string("name", name, true);
127 writer->write_string("author", author);
128 if(on_menukey_script != "")
129 writer->write_string("on-menukey-script", on_menukey_script);
131 for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i) {
133 writer->start_list("sector");
134 sector->write(*writer);
135 writer->end_list("sector");
138 writer->end_list("supertux-level");
145 for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i)
150 Level::add_sector(Sector* sector)
152 Sector* test = get_sector(sector->get_name());
154 throw std::runtime_error("Trying to add 2 sectors with same name");
156 sectors.push_back(sector);
160 Level::get_sector(const std::string& name)
162 for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i) {
164 if(sector->get_name() == name)
172 Level::get_sector_count()
174 return sectors.size();
178 Level::get_sector(size_t num)
180 return sectors.at(num);
184 Level::get_total_coins()
186 // FIXME not really correct as coins can also be inside blocks...
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);
201 Level::get_total_badguys()
203 int total_badguys = 0;
204 for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i)
205 total_badguys += (*i)->get_total_badguys();
206 return total_badguys;