1 // Copyright (C) 2003 Michael Bartl
2 // Copyright (C) 2004, 2005, 2006 Ulf Lorenz
3 // Copyright (C) 2007, 2008, 2009 Ben Asselstine
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.
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 Library General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 #include <sigc++/trackable.h>
35 //! A list of Tile objects in a terrain theme.
37 * Tileset is a list of Tile objects. It acts as the themeing mechanism for
38 * the look (and partially the behaviour) of terrain objects in the game.
39 * The Tileset dictates the pixel size of the tiles, and is used to lookup
40 * Tile and TileStyle objects. It is implemented as a singleton because many
41 * classes use it for looking up Tile and TileStyle objects.
43 * Tileset objects are often referred to by their subdirectory
44 * (Tileset::d_subdir).
46 * Tileset objects reside on disk in the tilesets/ directory, each of which is
47 * inside it's own directory.
49 * The tileset configuration file is a same named XML file inside the Tileset's
50 * directory. E.g. tilesets/${Tileset::d_subdir}/${Tileset::d_subdir}.lwt.
52 class Tileset : public sigc::trackable, public std::vector<Tile*>, public Set
55 //! The xml tag of this object in a tileset configuration file.
56 static std::string d_tag;
58 //! The xml tag of the road smallmap section of the tileset.
59 static std::string d_road_smallmap_tag;
61 //! tilesets have this extension. e.g. ".lwt".
62 static std::string file_extension;
64 //! Default constructor.
68 * @param id A unique numeric identifier among all tilesets.
69 * @param name The name of the Tileset. Analagous to Tileset::d_name.
71 Tileset(guint32 id, std::string name);
73 //! Loading constructor.
75 * Make a new Tileset object by loading the data from a tileset
78 * @param helper The opened tileset configuration file to load the
81 Tileset(XML_Helper* helper, std::string directory);
89 //! Get the unique identifier for this tileset.
91 * Analagous to the tileset.d_id XML entity in the tileset
94 guint32 getId() const {return d_id;}
96 //! Return the subdirectory of this Tileset.
97 std::string getSubDir() const {return d_subdir;}
99 //! Returns the name of the tileset.
100 std::string getName() const {return _(d_name.c_str());}
102 //! Returns the copyright holders of the tileset.
103 std::string getCopyright () const {return d_copyright;};
105 //! Returns the license of the tileset.
106 std::string getLicense() const {return d_license;};
108 //! Returns the description of the tileset.
109 std::string getInfo() const {return _(d_info.c_str());}
111 //! Returns the tilesize of the tileset.
112 guint32 getTileSize() const {return d_tileSize;}
114 //! Returns the basename of the file containing big selector images.
115 std::string getLargeSelectorFilename() {return d_large_selector;};
117 //! Returns the basename of the file containing small selector images.
118 std::string getSmallSelectorFilename() {return d_small_selector;};
120 //! Returns the basename of the file containing the explosion image.
121 std::string getExplosionFilename() {return d_explosion;};
123 //! Returns the basename of the file containing the road images.
124 std::string getRoadsFilename() {return d_roads;};
126 //! Returns the basename of the file containing the bridge images.
127 std::string getBridgesFilename() {return d_bridges;};
129 //! Returns the basename of the file containing the fog images.
130 std::string getFogFilename() {return d_fog;};
132 //! Returns the basename of the file containing the flag images.
133 std::string getFlagsFilename() {return d_flags;};
135 //! Get the colour associated with the road on the smallmap.
136 Gdk::Color getRoadColor() const {return d_road_color;};
138 //! Get the explosion image.
139 PixMask *getExplosionImage() {return explosion;};
141 //! Get a road image. Pass in the index.
142 PixMask *getRoadImage(guint32 i) {return roadpic[i];};
144 //! Get a bridge image. Pass in the index.
145 PixMask *getBridgeImage(guint32 i) {return bridgepic[i];};
147 //! Get a flag image. Pass in the index.
148 PixMask *getFlagImage(guint32 i) {return flagpic[i];};
150 //! Get the flag mask. Pass in the index.
151 PixMask *getFlagMask(guint32 i) {return flagmask[i];};
153 //! Get the fog image. Passin the index.
154 PixMask *getFogImage(guint32 i) {return fogpic[i];};
156 //! Get the big selector image. Pass in the index.
157 PixMask *getSelectorImage(guint32 i) {return selector[i];};
159 //! Get the big selector mask. Pass in the index.
160 PixMask *getSelectorMask(guint32 i) {return selectormask[i];};
162 //! Get the small selector image. Pass in the index.
163 PixMask *getSmallSelectorImage(guint32 i) {return smallselector[i];};
165 //! Get the small selector mask. Pass in the index.
166 PixMask *getSmallSelectorMask(guint32 i) {return smallselectormask[i];};
168 //! Get the number of animation frames in the big selector image.
169 guint32 getNumberOfSelectorFrames() {return number_of_selector_frames;};
171 //! Get the number of animation frames in the small selector image.
172 guint32 getNumberOfSmallSelectorFrames() {return number_of_small_selector_frames;};
174 //! Get filenames in this tileset, excepting the configuration file.
175 void getFilenames(std::list<std::string> &files);
177 std::string getConfigurationFile() const;
181 //! Set the subdirectory of where this Tileset resides on disk.
182 void setSubDir(std::string dir);
184 //! Set the unique identifier for this tileset.
186 * @note This method is only used in the tileset editor.
188 void setId(guint32 id) {d_id = id;}
190 //! Set the name of the tileset.
192 * @note This method is only used in the tileset editor.
194 void setName(std::string name) {d_name = name;}
196 //! Sets the copyright holders of the tileset.
197 void setCopyright(std::string copy) {d_copyright = copy;};
199 //! Sets the license of the tileset.
200 void setLicense(std::string license) {d_license = license;};
202 //! Set the description of the tileset.
204 * @note This method is only used in the tileset editor.
206 void setInfo(std::string info) {d_info = info;}
208 //! Sets the tilesize of the tileset.
209 void setTileSize(guint32 tileSize) {d_tileSize = tileSize;}
211 //! Sets the basename of the file containing the big selector images.
212 void setLargeSelectorFilename(std::string p){d_large_selector = p;};
214 //! Sets the basename of the file containing the small selector images.
215 void setSmallSelectorFilename(std::string p){d_small_selector = p;};
217 //! Sets the basename of the file containing the explosion image.
218 void setExplosionFilename(std::string p){d_explosion = p;};
220 //! Sets the basename of the file containing the road images.
221 void setRoadsFilename(std::string p){d_roads = p;};
223 //! Sets the basename of the file containing the bridge images.
224 void setBridgesFilename(std::string p){d_bridges = p;};
226 //! Sets the basename of the file containing the fog images.
227 void setFogFilename(std::string p){d_fog = p;};
229 //! Sets the basename of the file containing the flag images.
230 void setFlagsFilename(std::string p){d_flags = p;};
232 //! Sets the colour of the road on the smallmap.
233 void setRoadColor(Gdk::Color color) {d_road_color = color;};
235 //! Sets the explosion image.
236 void setExplosionImage(PixMask *p) {explosion = p;};
238 //! Sets a road image.
239 void setRoadImage(guint32 i, PixMask *p) {roadpic[i] = p;};
241 //! Sets a bridge image.
242 void setBridgeImage(guint32 i, PixMask *p) {bridgepic[i] = p;};
244 //! Sets a flag image.
245 void setFlagImage(guint32 i, PixMask *p) {flagpic[i] = p;};
247 //! Sets a flag mask.
248 void setFlagMask(guint32 i, PixMask *p) {flagmask[i] = p;};
250 //! Sets a fog image.
251 void setFogImage(guint32 i, PixMask *p) {fogpic[i] = p;};
253 //! Sets a big selector image.
254 void setSelectorImage(guint32 i, PixMask *p) {selector[i] = p;};
256 //! Sets a big selector mask.
257 void setSelectorMask(guint32 i, PixMask *p) {selectormask[i] = p;};
259 //! Sets a small selector image.
260 void setSmallSelectorImage(guint32 i, PixMask *p) {smallselector[i] = p;};
261 //! Sets a small selector mask.
262 void setSmallSelectorMask(guint32 i, PixMask *p) {smallselectormask[i] = p;};
264 //! Sets the number of animation frames in the big selector.
265 void setNumberOfSelectorFrames(guint32 s) {selector.reserve(s); selectormask.reserve(s); number_of_selector_frames = s;};
267 //! Sets the number of animation frames in the small selector.
268 void setNumberOfSmallSelectorFrames(guint32 s) {smallselector.reserve(s);smallselectormask.reserve(s); number_of_small_selector_frames = s;};
271 //Methods that operate on class data and modify the class data.
273 //! Destroy the images assoicated with this tileset.
274 void uninstantiateImages();
276 //! Load the images assoicated with this tileset.
277 void instantiateImages();
280 //Methods that operate on class data and do not modify the class data.
282 //! Returns the index to the given terrain type.
283 int getIndex(Tile::Type type) const;
285 //! Lookup tilestyle by it's id in this tileset.
286 TileStyle *getTileStyle(guint32 id) const;
288 //! Lookup a random tile style.
290 * Scan the TileStyles for the given Tile (given by index) for a
291 * TileStyle that matches the given style. When there is more than
292 * one TileStyle to choose from, randomly pick one from all of the
293 * matching TileStyle objects.
295 * @param index The index of the Tile in this set to operate on.
296 * @param style The kind of style we're looking for.
298 * @return A pointer to the matching TileStyle object, or NULL if no
299 * TileStyle could be found with that given style.
301 TileStyle *getRandomTileStyle(guint32 index, TileStyle::Type style) const;
303 //! Save a Tileset to an opened tile configuration file.
305 * @param The opened XML tile configuration file.
307 bool save(XML_Helper *helper) const;
309 //! Get a unique tile style id among all tile syles in this tileset.
310 int getFreeTileStyleId() const;
312 //! Get the largest tile style id of all tile styles in this tileset.
313 int getLargestTileStyleId() const;
315 //! Check to see if this tileset is suitable for use within the game.
316 bool validate() const;
321 //! Return the default height and width of a tile in the tileset.
322 static guint32 getDefaultTileSize();
324 //! Create a tileset from the given tileset configuration file.
325 static Tileset *create(std::string file);
327 //! Return a list of tileset subdirs in the user's personal collection.
328 static std::list<std::string> scanUserCollection();
330 //! Return a list of tileset subdirs in the system collection.
331 static std::list<std::string> scanSystemCollection();
334 //! Callback to load Tile objects into the Tileset.
335 bool loadTile(std::string, XML_Helper* helper);
337 //! Load the various images from the given filenames.
338 void instantiateImages(std::string explosion_filename,
339 std::string roads_filename,
340 std::string bridges_filename,
341 std::string fog_filename,
342 std::string flags_filename,
343 std::string selector_filename,
344 std::string small_selector_filename);
347 //! The name of the Tileset.
349 * Equates to the tileset.d_name XML entity in the tileset
350 * configuration file.
351 * This value appears in dialogs where the user is asked to select
352 * a Tileset among all other Tileset objects available to the game.
356 //! The copyright holders of the tileset.
357 std::string d_copyright;
359 //! The license of the tileset.
360 std::string d_license;
362 //! A unique numeric identifier among all tilesets.
365 //! The description of the Tileset.
367 * Equates to the tileset.d_info XML entity in the tileset
368 * configuration file.
369 * This value is not used.
373 //! The size of the graphic tiles in the Tileset.
375 * Equates to the tileset.d_tilesize XML entity in the tileset
376 * configuration file.
377 * It represents the size in pixels of the width and height of tile
382 //! The subdirectory of the Tileset.
384 * This is the name of the subdirectory that the Tileset files are
385 * residing in. It does not contain a path (e.g. no slashes).
386 * Tileset directories sit in the tileset/ directory.
388 std::string d_subdir;
390 //! The basename of the small selector image.
392 * The small selector is the graphic that appears on the bigmap when
393 * a stack is selected that only has one army unit in it.
395 * The image contains many animation frames, and is masked.
397 * This basename does not contain any slashes, and it does not contain
398 * a file extension. It refers to a png file in the directory of
401 std::string d_small_selector;
403 //! The basename of the large selector image.
405 * The large selector is the graphic that appears on the bigmap when
406 * a stack is selected that only has more than one army unit in it.
408 * The image contains many animation frames, and is masked.
410 * This basename does not contain any slashes, and it does not contain
411 * a file extension. It refers to a png file in the directory of
414 std::string d_large_selector;
416 //! The basename of the explosion image.
418 * The explosion image appears on the bigmap when stacks are fighting,
419 * and it also appears in the fight window when an army unit dies.
421 * This basename does not contain any slashes, and it does not contain
422 * a file extension. It refers to a png file in the directory of
425 std::string d_explosion;
427 //! The basename of the fog image.
429 * The fog images appear on the bigmap when playing with a hidden map.
431 * The number and order of frames in the image correlates to the
432 * FogMap::ShadeType enumeration.
434 * This basename does not contain any slashes, and it does not contain
435 * a file extension. It refers to a png file in the directory of
440 //! The basename of the road image.
442 * The road images appear on the bigmap overlaid on top of all kinds
443 * of tiles except for water.
445 * The number and order of frames in the image correlates to the
446 * Road::Type enumeration.
448 * This basename does not contain any slashes, and it does not contain
449 * a file extension. It refers to a png file in the directory of
454 //! The basename of the bridge image.
456 * The bridge images appear on the bigmap overlaid on top of certain
459 * The number and order of frames in the image correlates to the
460 * Bridge::Type enumeration.
462 * This basename does not contain any slashes, and it does not contain
463 * a file extension. It refers to a png file in the directory of
466 std::string d_bridges;
468 //! The basename of the flag image.
470 * The flag images appear on the bigmap beside a stack to indicate the
471 * number of army units in the stack.
473 * The number of frames in the image corresponds to the maximum number
474 * of army units in a stack. See the FLAG_TYPES constant in defs.h.
476 * This basename does not contain any slashes, and it does not contain
477 * a file extension. It refers to a png file in the directory of
482 typedef std::map<guint32, TileStyle*> TileStyleIdMap;
483 //! A map that provides a TileStyle when supplying a TileStyle id.
484 TileStyleIdMap d_tilestyles;
486 //! The colour of roads on the smallmap.
487 Gdk::Color d_road_color;
490 PixMask* roadpic[ROAD_TYPES];
492 //! The bridge images.
493 PixMask* bridgepic[BRIDGE_TYPES];
496 PixMask* flagpic[FLAG_TYPES];
499 PixMask* flagmask[FLAG_TYPES];
501 //! The number of animation frames in the big selector.
502 guint32 number_of_selector_frames;
504 //! The image frames in the big selector.
505 std::vector<PixMask* > selector;
507 //! The mask frames of the big selector.
508 std::vector<PixMask* > selectormask;
510 //! The number of animation frames in the small selector.
511 guint32 number_of_small_selector_frames;
513 //! The image frames of the small selector.
514 std::vector<PixMask* > smallselector;
516 //! The mask frames of the small selector.
517 std::vector<PixMask* > smallselectormask;
519 //! The exposion image.
523 PixMask*fogpic[FOG_TYPES];