* Coins inside boxes are now being counted. (We'll have to decide if we want the...
[supertux.git] / src / level.cpp
1 //  $Id$
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 <map>
23 #include <cstdlib>
24 #include <cstdio>
25 #include <cstring>
26 #include <iostream>
27 #include <fstream>
28 #include <sstream>
29 #include <memory>
30 #include <stdexcept>
31
32 #include "log.hpp"
33 #include "lisp/parser.hpp"
34 #include "lisp/lisp.hpp"
35 #include "lisp/list_iterator.hpp"
36 #include "lisp/writer.hpp"
37 #include "level.hpp"
38 #include "physic.hpp"
39 #include "sector.hpp"
40 #include "tile.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"
48
49 using namespace std;
50
51 Level::Level()
52   : name("noname"), author("Mr. X")
53 {
54 }
55
56 void
57 Level::load(const std::string& filepath)
58 {
59   try {
60     lisp::Parser parser;
61     std::auto_ptr<lisp::Lisp> root (parser.parse(filepath));
62
63     const lisp::Lisp* level = root->get_lisp("supertux-level");
64     if(!level)
65       throw std::runtime_error("file is not a supertux-level file.");
66
67     int version = 1;
68     level->get("version", version);
69     if(version == 1) {
70       load_old_format(*level);
71       return;
72     }
73
74     lisp::ListIterator iter(level);
75     while(iter.next()) {
76       const std::string& token = iter.item();
77       if(token == "version") {
78         iter.value()->get(version);
79         if(version > 2) {
80           log_warning << "level format newer than application" << std::endl;
81         }
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()));
91         add_sector(sector);
92       } else {
93         log_warning << "Unknown token '" << token << "' in level file" << std::endl;
94       }
95     }
96
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());
101   }
102 }
103
104 void
105 Level::load_old_format(const lisp::Lisp& reader)
106 {
107   reader.get("name", name);
108   reader.get("author", author);
109
110   Sector* sector = new Sector(this);
111   sector->parse_old_format(reader);
112   add_sector(sector);
113 }
114
115 void
116 Level::save(const std::string& filename)
117 {
118   lisp::Writer* writer = new lisp::Writer(filename);
119
120   writer->write_comment("Level made using SuperTux's built-in Level Editor");
121
122   writer->start_list("supertux-level");
123
124   int version = 2;
125   writer->write_int("version", version);
126
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);
131
132   for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i) {
133     Sector* sector = *i;
134     writer->start_list("sector");
135     sector->write(*writer);
136     writer->end_list("sector");
137   }
138
139   writer->end_list("supertux-level");
140
141   delete writer;
142 }
143
144 Level::~Level()
145 {
146   for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i)
147     delete *i;
148 }
149
150 void
151 Level::add_sector(Sector* sector)
152 {
153   Sector* test = get_sector(sector->get_name());
154   if(test != 0) {
155     throw std::runtime_error("Trying to add 2 sectors with same name");
156   }
157   sectors.push_back(sector);
158 }
159
160 Sector*
161 Level::get_sector(const std::string& name)
162 {
163   for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i) {
164     Sector* sector = *i;
165     if(sector->get_name() == name)
166       return sector;
167   }
168
169   return 0;
170 }
171
172 size_t
173 Level::get_sector_count()
174 {
175   return sectors.size();
176 }
177
178 Sector*
179 Level::get_sector(size_t num)
180 {
181   return sectors.at(num);
182 }
183
184 int
185 Level::get_total_coins()
186 {
187   int total_coins = 0;
188   for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i) {
189     Sector* sector = *i;
190     for(Sector::GameObjects::iterator o = sector->gameobjects.begin();
191         o != sector->gameobjects.end(); ++o) {
192       Coin* coin = dynamic_cast<Coin*> (*o);
193       if(coin)
194       {
195         total_coins++;
196         continue;
197       }
198       BonusBlock *block = dynamic_cast<BonusBlock*> (*o);
199       if(block)
200       {
201         if (block->contents == BonusBlock::CONTENT_COIN)
202         {
203           total_coins++;
204           continue;
205         }
206 #if 0
207         // FIXME: do we want this? q.v. src/object/oneup.cpp
208         else if (block->contents == BonusBlock::CONTENT_1UP)
209         {
210           total_coins += 100;
211           continue;
212         }
213 #endif
214       }
215     }
216   }
217   return total_coins;
218 }
219
220 int
221 Level::get_total_badguys()
222 {
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;
227 }