Moved Tile parsing code into TileSetParser
[supertux.git] / src / supertux / tile_set_parser.cpp
1 //  SuperTux
2 //  Copyright (C) 2008 Matthias Braun <matze@braunis.de>
3 //                     Ingo Ruhnke <grumbel@gmx.de>
4 //
5 //  This program is free software: you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation, either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 #include "supertux/tile_set_parser.hpp"
19
20 #include <stdexcept>
21 #include <sstream>
22
23 #include "lisp/list_iterator.hpp"
24 #include "lisp/parser.hpp"
25 #include "supertux/tile_set.hpp"
26 #include "util/file_system.hpp"
27
28 TileSetParser::TileSetParser(TileSet& tileset, const std::string& filename) :
29   m_tileset(tileset),
30   m_filename(filename)
31 {  
32 }
33
34 void
35 TileSetParser::parse()
36 {
37   m_tileset.tiles_path = FileSystem::dirname(m_filename);
38
39   m_tileset.tiles.resize(1, 0);
40   m_tileset.tiles[0] = new Tile(m_tileset);
41
42   lisp::Parser parser;
43   const lisp::Lisp* root = parser.parse(m_filename);
44
45   const lisp::Lisp* tiles_lisp = root->get_lisp("supertux-tiles");
46   if(!tiles_lisp)
47     throw std::runtime_error("file is not a supertux tiles file.");
48
49   lisp::ListIterator iter(tiles_lisp);
50   while(iter.next()) {
51     if(iter.item() == "tile") {
52       std::auto_ptr<Tile> tile(new Tile(m_tileset));
53       uint32_t id = parse_tile(*tile, *iter.lisp());
54
55       if(id >= m_tileset.tiles.size())
56         m_tileset.tiles.resize(id+1, 0);
57
58       if(m_tileset.tiles[id] != 0) {
59         log_warning << "Tile with ID " << id << " redefined" << std::endl;
60       } else {
61         m_tileset.tiles[id] = tile.release();
62       }
63     } else if(iter.item() == "tilegroup") {
64       /* tilegroups are only interesting for the editor */
65     } else if (iter.item() == "tiles") {
66       // List of ids (use 0 if the tile should be ignored)
67       std::vector<uint32_t> ids;
68       // List of attributes of the tile
69       std::vector<uint32_t> attributes;
70       // List of data for the tiles
71       std::vector<uint32_t> datas;
72       //List of frames that the tiles come in
73       std::vector<std::string> images;
74
75       // width and height of the image in tile units, this is used for two
76       // purposes:
77       //  a) so we don't have to load the image here to know its dimensions
78       //  b) so that the resulting 'tiles' entry is more robust,
79       //  ie. enlarging the image won't break the tile id mapping
80       // FIXME: height is actually not used, since width might be enough for
81       // all purposes, still feels somewhat more natural this way
82       unsigned int width  = 0;
83       unsigned int height = 0;
84
85       iter.lisp()->get("ids",        ids);
86       bool has_attributes = iter.lisp()->get("attributes", attributes);
87       bool has_datas = iter.lisp()->get("datas", datas);
88
89       if(!iter.lisp()->get("image",      images))
90         iter.lisp()->get( "images",      images);
91
92       iter.lisp()->get("width",      width);
93       iter.lisp()->get("height",     height);
94
95       float animfps = 10;
96       iter.lisp()->get("anim-fps",     animfps);
97
98       if(images.size() <= 0) {
99         throw std::runtime_error("No images in tile.");
100       }
101       if(animfps < 0) {
102         throw std::runtime_error("Negative fps.");
103       }
104       if (ids.size() != width*height) {
105         std::ostringstream err;
106         err << "Number of ids (" << ids.size() <<  ") and size of image (" << width*height
107             << ") mismatch for image '" << images[0] << "', but must be equal";
108         throw std::runtime_error(err.str());
109       }
110
111       if (has_attributes && ids.size() != attributes.size()) {
112         std::ostringstream err;
113         err << "Number of ids (" << ids.size() <<  ") and attributes (" << attributes.size()
114             << ") mismatch for image '" << images[0] << "', but must be equal";
115         throw std::runtime_error(err.str());
116       }
117
118       if (has_datas && ids.size() != datas.size()) {
119         std::ostringstream err;
120         err << "Number of ids (" << ids.size() <<  ") and datas (" << datas.size()
121             << ") mismatch for image '" << images[0] << "', but must be equal";
122         throw std::runtime_error(err.str());
123       }
124
125       for(std::vector<uint32_t>::size_type i = 0; i < ids.size() && i < width*height; ++i) {
126         if (ids[i] == 0)
127           continue;
128
129         if(ids[i] >= m_tileset.tiles.size())
130           m_tileset.tiles.resize(ids[i]+1, 0);
131
132         int x = 32*(i % width);
133         int y = 32*(i / width);
134         std::auto_ptr<Tile> tile(new Tile(m_tileset, images, Rect(x, y, x + 32, y + 32),
135                                           (has_attributes ? attributes[i] : 0), (has_datas ? datas[i] : 0), animfps));
136         if (m_tileset.tiles[ids[i]] == 0) {
137           m_tileset.tiles[ids[i]] = tile.release();
138         } else {
139           log_warning << "Tile with ID " << ids[i] << " redefined" << std::endl;
140         }
141       }
142     } else if(iter.item() == "properties") {
143       // deprecated
144     } else {
145       log_warning << "Unknown symbol '" << iter.item() << "' in tileset file" << std::endl;
146     }
147   }
148 }
149
150 uint32_t
151 TileSetParser::parse_tile(Tile& tile, const Reader& reader)
152 {
153   uint32_t id;
154   if(!reader.get("id", id)) {
155     throw std::runtime_error("Missing tile-id.");
156   }
157
158   bool value = false;
159   if(reader.get("solid", value) && value)
160     tile.attributes |= Tile::SOLID;
161   if(reader.get("unisolid", value) && value)
162     tile.attributes |= Tile::UNISOLID | Tile::SOLID;
163   if(reader.get("brick", value) && value)
164     tile.attributes |= Tile::BRICK;
165   if(reader.get("ice", value) && value)
166     tile.attributes |= Tile::ICE;
167   if(reader.get("water", value) && value)
168     tile.attributes |= Tile::WATER;
169   if(reader.get("hurts", value) && value)
170     tile.attributes |= Tile::HURTS;
171   if(reader.get("fire", value) && value)
172     tile.attributes |= Tile::FIRE;
173   if(reader.get("fullbox", value) && value)
174     tile.attributes |= Tile::FULLBOX;
175   if(reader.get("coin", value) && value)
176     tile.attributes |= Tile::COIN;
177   if(reader.get("goal", value) && value)
178     tile.attributes |= Tile::GOAL;
179
180   if(reader.get("north", value) && value)
181     tile.data |= Tile::WORLDMAP_NORTH;
182   if(reader.get("south", value) && value)
183     tile.data |= Tile::WORLDMAP_SOUTH;
184   if(reader.get("west", value) && value)
185     tile.data |= Tile::WORLDMAP_WEST;
186   if(reader.get("east", value) && value)
187     tile.data |= Tile::WORLDMAP_EAST;
188   if(reader.get("stop", value) && value)
189     tile.data |= Tile::WORLDMAP_STOP;
190
191   reader.get("data", tile.data);
192   reader.get("anim-fps", tile.anim_fps);
193
194   if(reader.get("slope-type", tile.data)) {
195     tile.attributes |= Tile::SOLID | Tile::SLOPE;
196   }
197
198   const lisp::Lisp* images;
199 #ifndef NDEBUG
200   images = reader.get_lisp("editor-images");
201   if(images)
202     parse_images(tile, *images);
203   else {
204 #endif
205     images = reader.get_lisp("images");
206     if(images)
207       parse_images(tile, *images);
208 #ifndef NDEBUG
209   }
210 #endif
211
212   tile.correct_attributes();
213
214   return id;
215 }
216
217 void
218 TileSetParser::parse_images(Tile& tile, const Reader& images_lisp)
219 {
220   const lisp::Lisp* list = &images_lisp;
221   while(list) {
222     const lisp::Lisp* cur = list->get_car();
223     if(cur->get_type() == lisp::Lisp::TYPE_STRING) {
224       std::string file;
225       cur->get(file);
226       tile.imagespecs.push_back(Tile::ImageSpec(file, Rect(0, 0, 0, 0)));
227     } else if(cur->get_type() == lisp::Lisp::TYPE_CONS &&
228               cur->get_car()->get_type() == lisp::Lisp::TYPE_SYMBOL &&
229               cur->get_car()->get_symbol() == "region") {
230       const lisp::Lisp* ptr = cur->get_cdr();
231
232       std::string file;
233       float x = 0, y = 0, w = 0, h = 0;
234       ptr->get_car()->get(file); ptr = ptr->get_cdr();
235       ptr->get_car()->get(x); ptr = ptr->get_cdr();
236       ptr->get_car()->get(y); ptr = ptr->get_cdr();
237       ptr->get_car()->get(w); ptr = ptr->get_cdr();
238       ptr->get_car()->get(h);
239       tile.imagespecs.push_back(Tile::ImageSpec(file, Rect(x, y, x+w, y+h)));
240     } else {
241       log_warning << "Expected string or list in images tag" << std::endl;
242       continue;
243     }
244
245     list = list->get_cdr();
246   }
247 }
248
249 /* EOF */