Removed friendship between TileSetParser and Tile, use proper constructor instead
[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(m_tileset);
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   uint32_t data = 0;
83   std::vector<Tile::ImageSpec> imagespecs;
84
85   float anim_fps = 10;
86
87   bool value = false;
88   if(reader.get("solid", value) && value)
89     attributes |= Tile::SOLID;
90   if(reader.get("unisolid", value) && value)
91     attributes |= Tile::UNISOLID | Tile::SOLID;
92   if(reader.get("brick", value) && value)
93     attributes |= Tile::BRICK;
94   if(reader.get("ice", value) && value)
95     attributes |= Tile::ICE;
96   if(reader.get("water", value) && value)
97     attributes |= Tile::WATER;
98   if(reader.get("hurts", value) && value)
99     attributes |= Tile::HURTS;
100   if(reader.get("fire", value) && value)
101     attributes |= Tile::FIRE;
102   if(reader.get("fullbox", value) && value)
103     attributes |= Tile::FULLBOX;
104   if(reader.get("coin", value) && value)
105     attributes |= Tile::COIN;
106   if(reader.get("goal", value) && value)
107     attributes |= Tile::GOAL;
108
109   if(reader.get("north", value) && value)
110     data |= Tile::WORLDMAP_NORTH;
111   if(reader.get("south", value) && value)
112     data |= Tile::WORLDMAP_SOUTH;
113   if(reader.get("west", value) && value)
114     data |= Tile::WORLDMAP_WEST;
115   if(reader.get("east", value) && value)
116     data |= Tile::WORLDMAP_EAST;
117   if(reader.get("stop", value) && value)
118     data |= Tile::WORLDMAP_STOP;
119
120   reader.get("data", data);
121   reader.get("anim-fps", anim_fps);
122
123   uint32_t throwaway_data;
124   if(reader.get("slope-type", throwaway_data)) 
125   {
126     attributes |= Tile::SOLID | Tile::SLOPE;
127   }
128
129   const lisp::Lisp* images;
130 #ifndef NDEBUG
131   images = reader.get_lisp("editor-images");
132   if(images)
133     imagespecs = parse_tile_images(*images);
134   else {
135 #endif
136     images = reader.get_lisp("images");
137     if(images)
138       imagespecs = parse_tile_images(*images);
139 #ifndef NDEBUG
140   }
141 #endif
142
143   std::auto_ptr<Tile> tile(new Tile(m_tileset, imagespecs, attributes, data, anim_fps));
144
145   if (id >= m_tileset.tiles.size())
146     m_tileset.tiles.resize(id+1, 0);
147
148   if (m_tileset.tiles[id] != 0) 
149   {
150     log_warning << "Tile with ID " << id << " redefined" << std::endl;
151   } 
152   else 
153   {
154     m_tileset.tiles[id] = tile.release();
155   }
156 }
157
158 std::vector<Tile::ImageSpec>
159 TileSetParser::parse_tile_images(const Reader& images_lisp)
160 {
161   std::vector<Tile::ImageSpec> imagespecs;
162
163   const lisp::Lisp* list = &images_lisp;
164   while(list) 
165   {
166     const lisp::Lisp* cur = list->get_car();
167
168     if(cur->get_type() == lisp::Lisp::TYPE_STRING) 
169     {
170       std::string file;
171       cur->get(file);
172       imagespecs.push_back(Tile::ImageSpec(m_tiles_path + file, Rect(0, 0, 0, 0)));
173     }
174     else if(cur->get_type() == lisp::Lisp::TYPE_CONS &&
175             cur->get_car()->get_type() == lisp::Lisp::TYPE_SYMBOL &&
176             cur->get_car()->get_symbol() == "region") 
177     {
178       const lisp::Lisp* ptr = cur->get_cdr();
179
180       std::string file;
181       float x = 0;
182       float y = 0;
183       float w = 0;
184       float h = 0;
185       ptr->get_car()->get(file); ptr = ptr->get_cdr();
186       ptr->get_car()->get(x); ptr = ptr->get_cdr();
187       ptr->get_car()->get(y); ptr = ptr->get_cdr();
188       ptr->get_car()->get(w); ptr = ptr->get_cdr();
189       ptr->get_car()->get(h);
190       imagespecs.push_back(Tile::ImageSpec(m_tiles_path + file, Rect(x, y, x+w, y+h)));
191     } 
192     else 
193     {
194       log_warning << "Expected string or list in images tag" << std::endl;
195     }
196
197     list = list->get_cdr();
198   }
199
200   return imagespecs;
201 }
202
203 void
204 TileSetParser::parse_tiles(const Reader& reader)
205 {
206   // List of ids (use 0 if the tile should be ignored)
207   std::vector<uint32_t> ids;
208   // List of attributes of the tile
209   std::vector<uint32_t> attributes;
210   // List of data for the tiles
211   std::vector<uint32_t> datas;
212   //List of frames that the tiles come in
213   std::vector<std::string> images;
214
215   // width and height of the image in tile units, this is used for two
216   // purposes:
217   //  a) so we don't have to load the image here to know its dimensions
218   //  b) so that the resulting 'tiles' entry is more robust,
219   //  ie. enlarging the image won't break the tile id mapping
220   // FIXME: height is actually not used, since width might be enough for
221   // all purposes, still feels somewhat more natural this way
222   unsigned int width  = 0;
223   unsigned int height = 0;
224
225   reader.get("ids",        ids);
226   bool has_attributes = reader.get("attributes", attributes);
227   bool has_datas = reader.get("datas", datas);
228
229   if (!reader.get("image", images))
230   {
231     reader.get("images", images);
232   }
233
234   reader.get("width",      width);
235   reader.get("height",     height);
236
237   float animfps = 10;
238   reader.get("anim-fps",     animfps);
239
240   if (images.size() <= 0) 
241   {
242     throw std::runtime_error("No images in tile.");
243   }
244   else if (animfps < 0) 
245   {
246     throw std::runtime_error("Negative fps.");
247   }
248   else if (ids.size() != width*height) 
249   {
250     std::ostringstream err;
251     err << "Number of ids (" << ids.size() <<  ") and size of image (" << width*height
252         << ") mismatch for image '" << images[0] << "', but must be equal";
253     throw std::runtime_error(err.str());
254   }
255   else if (has_attributes && ids.size() != attributes.size()) 
256   {
257     std::ostringstream err;
258     err << "Number of ids (" << ids.size() <<  ") and attributes (" << attributes.size()
259         << ") mismatch for image '" << images[0] << "', but must be equal";
260     throw std::runtime_error(err.str());
261   }
262   else if (has_datas && ids.size() != datas.size()) 
263   {        
264     std::ostringstream err;
265     err << "Number of ids (" << ids.size() <<  ") and datas (" << datas.size()
266         << ") mismatch for image '" << images[0] << "', but must be equal";
267     throw std::runtime_error(err.str());
268   }
269   else
270   {
271     for(std::vector<uint32_t>::size_type i = 0; i < ids.size() && i < width*height; ++i) 
272     {
273       if (ids[i] != 0)
274       {
275         if (ids[i] >= m_tileset.tiles.size())
276           m_tileset.tiles.resize(ids[i]+1, 0);
277
278         int x = 32*(i % width);
279         int y = 32*(i / width);
280
281         std::vector<Tile::ImageSpec> imagespecs;
282         for(std::vector<std::string>::const_iterator j = images.begin(); j != images.end(); ++j) 
283         {
284           imagespecs.push_back(Tile::ImageSpec(m_tiles_path + *j, Rect(x, y, x + 32, y + 32)));
285         }
286
287         std::auto_ptr<Tile> tile(new Tile(m_tileset, imagespecs,
288                                           (has_attributes ? attributes[i] : 0), (has_datas ? datas[i] : 0), animfps));
289         if (m_tileset.tiles[ids[i]] == 0) {
290           m_tileset.tiles[ids[i]] = tile.release();
291         } else {
292           log_warning << "Tile with ID " << ids[i] << " redefined" << std::endl;
293         }
294       }
295     }
296   }  
297 }
298
299 /* EOF */