Had a bit of time today and worked on supertux:
[supertux.git] / src / level.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 SuperTux Development Team, see AUTHORS for details
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 "app/globals.h"
33 #include "app/setup.h"
34 #include "video/screen.h"
35 #include "lisp/parser.h"
36 #include "lisp/lisp.h"
37 #include "lisp/list_iterator.h"
38 #include "lisp/writer.h"
39 #include "level.h"
40 #include "math/physic.h"
41 #include "sector.h"
42 #include "tile.h"
43 #include "resources.h"
44 #include "object/gameobjs.h"
45 #include "object/camera.h"
46 #include "object/tilemap.h"
47 #include "object/coin.h"
48
49 // test
50 #include "flip_level_transformer.h"
51
52 using namespace std;
53
54 Level::Level()
55   : name("noname"), author("Mr. X"), timelimit(500)
56 {
57 }
58
59 void
60 Level::load(const std::string& filepath)
61 {
62   try {
63     lisp::Parser parser;
64     std::auto_ptr<lisp::Lisp> root (parser.parse(filepath));
65
66     const lisp::Lisp* level = root->get_lisp("supertux-level");
67     if(!level)
68       throw std::runtime_error("file is not a supertux-level file.");
69
70     int version = 1;
71     level->get("version", version);
72     if(version == 1) {
73       load_old_format(*level);
74
75 #if 0
76       // test for now
77       FlipLevelTransformer* transformer = new FlipLevelTransformer();  
78       transformer->transform(this);
79 #endif
80      
81       return;
82     }
83
84     lisp::ListIterator iter(level);
85     while(iter.next()) {
86       const std::string& token = iter.item();
87       if(token == "version") {
88         iter.value()->get(version);
89         if(version > 2) {
90           std::cerr << "Warning: level format newer than application.\n";
91         }
92       } else if(token == "name") {
93         iter.value()->get(name);
94       } else if(token == "author") {
95         iter.value()->get(author);
96       } else if(token == "time") {
97         iter.value()->get(timelimit);
98       } else if(token == "sector") {
99         Sector* sector = new Sector;
100         sector->parse(*(iter.lisp()));
101         add_sector(sector);
102       } else {
103         std::cerr << "Unknown token '" << token << "' in level file.\n";
104         continue;
105       }
106     }
107     
108   } catch(std::exception& e) {
109     std::stringstream msg;
110     msg << "Problem when reading level '" << filepath << "': " << e.what();
111     throw std::runtime_error(msg.str());
112   }
113 }
114
115 void
116 Level::load_old_format(const lisp::Lisp& reader)
117 {
118   reader.get("name", name);
119   reader.get("author", author);
120   reader.get("time", timelimit);
121
122   Sector* sector = new Sector;
123   sector->parse_old_format(reader);
124   add_sector(sector);
125 }
126
127 void
128 Level::save(const std::string& filename)
129 {
130   std::string filepath = "levels/" + filename;
131   int last_slash = filepath.find_last_of('/');
132   FileSystem::fcreatedir(filepath.substr(0,last_slash).c_str());
133   filepath = st_dir + "/" + filepath;
134   ofstream file(filepath.c_str(), ios::out);
135   lisp::Writer* writer = new lisp::Writer(file);
136
137   writer->write_comment("Level made using SuperTux's built-in Level Editor");
138
139   writer->start_list("supertux-level");
140
141   int version = 2;
142   writer->write_int("version", version);
143
144   writer->write_string("name", name, true);
145   writer->write_string("author", author);
146   writer->write_int("time", timelimit);
147
148   for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i) {
149     Sector* sector = *i;
150     writer->start_list("sector");
151     sector->write(*writer);
152     writer->end_list("sector");
153   }
154
155   writer->end_list("supertux-level");
156
157   delete writer;
158   file.close();
159 }
160
161 Level::~Level()
162 {
163   for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i)
164     delete *i;
165 }
166
167 void
168 Level::add_sector(Sector* sector)
169 {
170   Sector* test = get_sector(sector->get_name());
171   if(test != 0) {
172     throw std::runtime_error("Trying to add 2 sectors with same name");
173   }
174   sectors.push_back(sector);
175 }
176
177 Sector*
178 Level::get_sector(const std::string& name)
179 {
180   for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i) {
181     Sector* sector = *i;
182     if(sector->get_name() == name)
183       return sector;
184   }
185
186   return 0;
187 }
188
189 size_t
190 Level::get_sector_count()
191 {
192   return sectors.size();
193 }
194
195 Sector*
196 Level::get_sector(size_t num)
197 {
198   return sectors.at(num);
199 }
200
201 int
202 Level::get_total_badguys()
203 {
204   int total_badguys = 0;
205   for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i)
206     total_badguys += (*i)->get_total_badguys();
207   return total_badguys;
208 }
209
210 int
211 Level::get_total_coins()
212 {
213   // FIXME not really correct as coins can also be inside blocks...
214   int total_coins = 0;
215   for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i) {
216     Sector* sector = *i;
217     for(Sector::GameObjects::iterator o = sector->gameobjects.begin();
218         o != sector->gameobjects.end(); ++o) {
219       Coin* coin = dynamic_cast<Coin*> (*o);
220       if(coin)
221         total_coins++;
222     }
223   }
224   return total_coins;
225 }
226