5daaa459952a0cdb6f0a2f30f5855989c0c37dd4
[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   m_tiles_path()
32 {  
33 }
34
35 void
36 TileSetParser::parse()
37 {
38   m_tiles_path = FileSystem::dirname(m_filename);
39
40   m_tileset.tiles.resize(1, 0);
41   m_tileset.tiles[0] = new Tile();
42
43   lisp::Parser parser;
44   const lisp::Lisp* root = parser.parse(m_filename);
45
46   const lisp::Lisp* tiles_lisp = root->get_lisp("supertux-tiles");
47   if(!tiles_lisp)
48     throw std::runtime_error("file is not a supertux tiles file.");
49
50   lisp::ListIterator iter(tiles_lisp);
51   while(iter.next()) 
52   {
53     if (iter.item() == "tile") 
54     {
55       parse_tile(*iter.lisp());
56     } 
57     else if (iter.item() == "tilegroup") 
58     {
59       /* tilegroups are only interesting for the editor */
60     } 
61     else if (iter.item() == "tiles") 
62     {
63       parse_tiles(*iter.lisp());
64     }
65     else 
66     {
67       log_warning << "Unknown symbol '" << iter.item() << "' in tileset file" << std::endl;
68     }
69   }
70 }
71
72 void
73 TileSetParser::parse_tile(const Reader& reader)
74 {
75   uint32_t id;
76   if (!reader.get("id", id)) 
77   {
78     throw std::runtime_error("Missing tile-id.");
79   }
80
81   uint32_t attributes = 0;
82
83   bool value = false;
84   if(reader.get("solid", value) && value)
85     attributes |= Tile::SOLID;
86   if(reader.get("unisolid", value) && value)
87     attributes |= Tile::UNISOLID | Tile::SOLID;
88   if(reader.get("brick", value) && value)
89     attributes |= Tile::BRICK;
90   if(reader.get("ice", value) && value)
91     attributes |= Tile::ICE;
92   if(reader.get("water", value) && value)
93     attributes |= Tile::WATER;
94   if(reader.get("hurts", value) && value)
95     attributes |= Tile::HURTS;
96   if(reader.get("fire", value) && value)
97     attributes |= Tile::FIRE;
98   if(reader.get("fullbox", value) && value)
99     attributes |= Tile::FULLBOX;
100   if(reader.get("coin", value) && value)
101     attributes |= Tile::COIN;
102   if(reader.get("goal", value) && value)
103     attributes |= Tile::GOAL;
104
105   uint32_t data = 0;
106
107   if(reader.get("north", value) && value)
108     data |= Tile::WORLDMAP_NORTH;
109   if(reader.get("south", value) && value)
110     data |= Tile::WORLDMAP_SOUTH;
111   if(reader.get("west", value) && value)
112     data |= Tile::WORLDMAP_WEST;
113   if(reader.get("east", value) && value)
114     data |= Tile::WORLDMAP_EAST;
115   if(reader.get("stop", value) && value)
116     data |= Tile::WORLDMAP_STOP;
117
118   reader.get("data", data);
119
120   float fps = 10;
121   reader.get("fps", fps);
122
123   if(reader.get("slope-type", data)) 
124   {
125     attributes |= Tile::SOLID | Tile::SLOPE;
126   }
127
128   std::vector<Tile::ImageSpec> editor_imagespecs;
129   const lisp::Lisp* editor_images;
130   editor_images = reader.get_lisp("editor-images");
131   if(editor_images)
132     editor_imagespecs = parse_tile_images(*editor_images);
133
134   std::vector<Tile::ImageSpec> imagespecs;
135   const lisp::Lisp* images;
136   images = reader.get_lisp("images");
137   if(images)
138       imagespecs = parse_tile_images(*images);
139
140   std::auto_ptr<Tile> tile(new Tile(imagespecs, editor_imagespecs, attributes, data, fps));
141
142   if (id >= m_tileset.tiles.size())
143     m_tileset.tiles.resize(id+1, 0);
144
145   if (m_tileset.tiles[id] != 0) 
146   {
147     log_warning << "Tile with ID " << id << " redefined" << std::endl;
148   } 
149   else 
150   {
151     m_tileset.tiles[id] = tile.release();
152   }
153 }
154
155 std::vector<Tile::ImageSpec>
156 TileSetParser::parse_tile_images(const Reader& images_lisp)
157 {
158   std::vector<Tile::ImageSpec> imagespecs;
159
160   const lisp::Lisp* list = &images_lisp;
161   while(list) 
162   {
163     const lisp::Lisp* cur = list->get_car();
164
165     if(cur->get_type() == lisp::Lisp::TYPE_STRING) 
166     {
167       std::string file;
168       cur->get(file);
169       imagespecs.push_back(Tile::ImageSpec(m_tiles_path + file, Rectf(0, 0, 0, 0)));
170     }
171     else if(cur->get_type() == lisp::Lisp::TYPE_CONS &&
172             cur->get_car()->get_type() == lisp::Lisp::TYPE_SYMBOL &&
173             cur->get_car()->get_symbol() == "region") 
174     {
175       const lisp::Lisp* ptr = cur->get_cdr();
176
177       std::string file;
178       float x = 0;
179       float y = 0;
180       float w = 0;
181       float h = 0;
182       ptr->get_car()->get(file); ptr = ptr->get_cdr();
183       ptr->get_car()->get(x); ptr = ptr->get_cdr();
184       ptr->get_car()->get(y); ptr = ptr->get_cdr();
185       ptr->get_car()->get(w); ptr = ptr->get_cdr();
186       ptr->get_car()->get(h);
187       imagespecs.push_back(Tile::ImageSpec(m_tiles_path + file, Rectf(x, y, x+w, y+h)));
188     } 
189     else 
190     {
191       log_warning << "Expected string or list in images tag" << std::endl;
192     }
193
194     list = list->get_cdr();
195   }
196
197   return imagespecs;
198 }
199
200 void
201 TileSetParser::parse_tiles(const Reader& reader)
202 {
203   // List of ids (use 0 if the tile should be ignored)
204   std::vector<uint32_t> ids;
205   // List of attributes of the tile
206   std::vector<uint32_t> attributes;
207   // List of data for the tiles
208   std::vector<uint32_t> datas;
209   //List of frames that the tiles come in
210   std::vector<std::string> images;
211   //List of frames that the editor tiles come in
212   std::vector<std::string> editor_images;
213   // Name used to report errors.
214   std::string image_name;
215
216   // width and height of the image in tile units, this is used for two
217   // purposes:
218   //  a) so we don't have to load the image here to know its dimensions
219   //  b) so that the resulting 'tiles' entry is more robust,
220   //  ie. enlarging the image won't break the tile id mapping
221   // FIXME: height is actually not used, since width might be enough for
222   // all purposes, still feels somewhat more natural this way
223   unsigned int width  = 0;
224   unsigned int height = 0;
225
226   reader.get("ids",        ids);
227   bool has_attributes = reader.get("attributes", attributes);
228   bool has_datas = reader.get("datas", datas);
229
230   reader.get("image", images) || reader.get("images", images);
231   reader.get("editor-images", editor_images);
232
233   if (images.size() > 0)
234     image_name = images[0];
235   else
236     image_name = "(no image)";
237
238   reader.get("width",      width);
239   reader.get("height",     height);
240
241   float fps = 10;
242   reader.get("fps",     fps);
243
244   if (width <= 0)
245   {
246     throw std::runtime_error("Width is zero.");
247   }
248   else if (height <= 0)
249   {
250     throw std::runtime_error("Height is zero.");
251   }
252   else if (fps < 0) 
253   {
254     throw std::runtime_error("Negative fps.");
255   }
256   else if (ids.size() != width*height) 
257   {
258     std::ostringstream err;
259     err << "Number of ids (" << ids.size() <<  ") and "
260       "dimensions of image (" << width << "x" << height << " = " << width*height << ") "
261       "differ for image " << image_name;
262     throw std::runtime_error(err.str());
263   }
264   else if (has_attributes && (ids.size() != attributes.size()))
265   {
266     std::ostringstream err;
267     err << "Number of ids (" << ids.size() <<  ") and attributes (" << attributes.size()
268         << ") mismatch for image '" << image_name << "', but must be equal";
269     throw std::runtime_error(err.str());
270   }
271   else if (has_datas && ids.size() != datas.size()) 
272   {        
273     std::ostringstream err;
274     err << "Number of ids (" << ids.size() <<  ") and datas (" << datas.size()
275         << ") mismatch for image '" << image_name << "', but must be equal";
276     throw std::runtime_error(err.str());
277   }
278   else
279   {
280     for(std::vector<uint32_t>::size_type i = 0; i < ids.size() && i < width*height; ++i) 
281     {
282       if (ids[i] != 0)
283       {
284         if (ids[i] >= m_tileset.tiles.size())
285           m_tileset.tiles.resize(ids[i]+1, 0);
286
287         int x = 32*(i % width);
288         int y = 32*(i / width);
289
290         std::vector<Tile::ImageSpec> imagespecs;
291         for(std::vector<std::string>::const_iterator j = images.begin(); j != images.end(); ++j) 
292         {
293           imagespecs.push_back(Tile::ImageSpec(m_tiles_path + *j, Rectf(x, y, x + 32, y + 32)));
294         }
295
296         std::vector<Tile::ImageSpec> editor_imagespecs;
297         for(std::vector<std::string>::const_iterator j = editor_images.begin(); j != editor_images.end(); ++j) 
298         {
299           editor_imagespecs.push_back(Tile::ImageSpec(m_tiles_path + *j, Rectf(x, y, x + 32, y + 32)));
300         }
301
302         std::auto_ptr<Tile> tile(new Tile(imagespecs, editor_imagespecs,
303                                           (has_attributes ? attributes[i] : 0), (has_datas ? datas[i] : 0), fps));
304         if (m_tileset.tiles[ids[i]] == 0) {
305           m_tileset.tiles[ids[i]] = tile.release();
306         } else {
307           log_warning << "Tile with ID " << ids[i] << " redefined" << std::endl;
308         }
309       }
310     }
311   }  
312 }
313
314 /* EOF */