- fixed warnings pointed out by Frank
[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
21 #include <map>
22 #include <cstdlib>
23 #include <cstdio>
24 #include <cstring>
25 #include <iostream>
26 #include <fstream>
27 #include <stdexcept>
28
29 #include "app/globals.h"
30 #include "app/setup.h"
31 #include "camera.h"
32 #include "video/screen.h"
33 #include "level.h"
34 #include "math/physic.h"
35 #include "scene.h"
36 #include "sector.h"
37 #include "tile.h"
38 #include "utils/lispreader.h"
39 #include "resources.h"
40 #include "gameobjs.h"
41 #include "utils/lispwriter.h"
42 #include "tilemap.h"
43
44 using namespace std;
45
46 Level::Level()
47   : name("noname"), author("mr. x"), time_left(500)
48
49 {
50 }
51
52 void
53 Level::load(const std::string& filename)
54 {
55   LispReader* level = LispReader::load(filename, "supertux-level");
56
57   int version = 1;
58   level->read_int("version", version);
59   if(version == 1) {
60     load_old_format(*level);
61     delete level;
62     return;
63   }
64
65   for(lisp_object_t* cur = level->get_lisp(); !lisp_nil_p(cur);
66       cur = lisp_cdr(cur)) {
67     std::string token = lisp_symbol(lisp_car(lisp_car(cur)));
68     lisp_object_t* data = lisp_car(lisp_cdr(lisp_car(cur)));
69     LispReader reader(lisp_cdr(lisp_car(cur)));
70
71     if(token == "name") {
72       name = lisp_string(data);
73     } else if(token == "author") {
74       author = lisp_string(data);
75     } else if(token == "time") {
76       time_left = lisp_integer(data);
77     } else if(token == "sector") {
78       Sector* sector = new Sector;
79       sector->parse(reader);
80       add_sector(sector);
81     } else {
82       std::cerr << "Unknown token '" << token << "' in level file.\n";
83       continue;
84     }
85   }
86   
87   delete level;
88 }
89
90 void
91 Level::load_old_format(LispReader& reader)
92 {
93   reader.read_string("name", name, true);
94   reader.read_string("author", author);
95   reader.read_int("time", time_left);
96
97   Sector* sector = new Sector;
98   sector->parse_old_format(reader);
99   add_sector(sector);
100 }
101
102 void
103 Level::save(const std::string& filename)
104 {
105  ofstream file(filename.c_str(), ios::out);
106  LispWriter* writer = new LispWriter(file);
107
108  writer->write_comment("Level made using SuperTux's built-in Level Editor");
109
110  writer->start_list("supertux-level");
111
112  int version = 2;
113  writer->write_int("version", version);
114
115  writer->write_string("name", name);
116  writer->write_string("author", author);
117  writer->write_int("time", time_left);
118
119  for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i)
120    {
121    writer->start_list("sector");
122    i->second->write(*writer);
123    writer->end_list("sector");
124    }
125
126  writer->end_list("supertux-level");
127
128  delete writer;
129  file.close();
130 }
131
132 Level::~Level()
133 {
134   for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i)
135     delete i->second;
136 }
137
138 void
139 Level::do_vertical_flip()
140 {
141   for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i)
142     i->second->do_vertical_flip();
143 }
144
145 void
146 Level::add_sector(Sector* sector)
147 {
148   sectors.insert(std::make_pair(sector->get_name(), sector));       
149 }
150
151 Sector*
152 Level::get_sector(const std::string& name)
153 {
154   Sectors::iterator i = sectors.find(name);
155   if(i == sectors.end())
156     return 0;
157
158   return i->second;
159 }
160
161 int
162 Level::get_total_badguys()
163 {
164   int total_badguys = 0;
165   for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i)
166     total_badguys += i->second->get_total_badguys();
167   return total_badguys;
168 }
169
170 int
171 Level::get_total_coins()
172 {
173   int total_coins = 0;
174   for(Sectors::iterator it = sectors.begin(); it != sectors.end(); ++it)
175     for(int x = 0; static_cast<unsigned int>(x) < it->second->solids->get_width(); x++)
176       for(int y = 0; static_cast<unsigned int>(y) < it->second->solids->get_height(); y++)
177         if(it->second->solids->get_tile(x,y)->attributes & Tile::COIN)
178           total_coins++;
179   return total_coins;
180 }