initial commit, lordsawar source, slightly modified
[lordsawar] / src / tileset.h
1 // Copyright (C) 2003 Michael Bartl
2 // Copyright (C) 2004, 2005, 2006 Ulf Lorenz
3 // Copyright (C) 2007, 2008, 2009 Ben Asselstine
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 Library 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, write to the Free Software
17 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
18 //  02110-1301, USA.
19
20 #ifndef TILESET_H
21 #define TILESET_H
22
23 #include <string>
24 #include <vector>
25 #include <sigc++/trackable.h>
26
27 #include "Tile.h"
28 #include "defs.h"
29 #include "File.h"
30 #include "set.h"
31 using namespace std;
32
33 class XML_Helper;
34
35 //! A list of Tile objects in a terrain theme.
36 /** 
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.
42  * 
43  * Tileset objects are often referred to by their subdirectory 
44  * (Tileset::d_subdir).
45  *
46  * Tileset objects reside on disk in the tilesets/ directory, each of which is
47  * inside it's own directory.
48  *
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.
51  */
52 class Tileset : public sigc::trackable, public std::vector<Tile*>, public Set
53 {
54     public:
55         //! The xml tag of this object in a tileset configuration file.
56         static std::string d_tag; 
57
58         //! The xml tag of the road smallmap section of the tileset.
59         static std::string d_road_smallmap_tag; 
60
61         //! tilesets have this extension. e.g. ".lwt".
62         static std::string file_extension; 
63
64         //! Default constructor.
65         /**
66          * Make a new Tileset.
67          *
68          * @param id    A unique numeric identifier among all tilesets.
69          * @param name  The name of the Tileset.  Analagous to Tileset::d_name.
70          */
71         Tileset(guint32 id, std::string name);
72
73         //! Loading constructor.
74         /**
75          * Make a new Tileset object by loading the data from a tileset
76          * configuration file.
77          *
78          * @param helper  The opened tileset configuration file to load the
79          *                tileset from.
80          */
81         Tileset(XML_Helper* helper, std::string directory);
82
83         //! Destructor.
84         ~Tileset();
85
86
87         // Get Methods
88
89         //! Get the unique identifier for this tileset.
90         /**
91          * Analagous to the tileset.d_id XML entity in the tileset 
92          * configuration file.
93          */
94         guint32 getId() const {return d_id;}
95
96         //! Return the subdirectory of this Tileset.
97         std::string getSubDir() const {return d_subdir;}
98
99         //! Returns the name of the tileset.
100         std::string getName() const {return _(d_name.c_str());}
101
102         //! Returns the copyright holders of the tileset.
103         std::string getCopyright () const {return d_copyright;};
104
105         //! Returns the license of the tileset.
106         std::string getLicense() const {return d_license;};
107
108         //! Returns the description of the tileset.
109         std::string getInfo() const {return _(d_info.c_str());}
110
111         //! Returns the tilesize of the tileset.
112         guint32 getTileSize() const {return d_tileSize;}
113
114         //! Returns the basename of the file containing big selector images.
115         std::string getLargeSelectorFilename() {return d_large_selector;};
116
117         //! Returns the basename of the file containing small selector images.
118         std::string getSmallSelectorFilename() {return d_small_selector;};
119
120         //! Returns the basename of the file containing the explosion image.
121         std::string getExplosionFilename() {return d_explosion;};
122
123         //! Returns the basename of the file containing the road images.
124         std::string getRoadsFilename() {return d_roads;};
125
126         //! Returns the basename of the file containing the bridge images.
127         std::string getBridgesFilename() {return d_bridges;};
128
129         //! Returns the basename of the file containing the fog images.
130         std::string getFogFilename() {return d_fog;};
131
132         //! Returns the basename of the file containing the flag images.
133         std::string getFlagsFilename() {return d_flags;};
134
135         //! Get the colour associated with the road on the smallmap.
136         Gdk::Color getRoadColor() const {return d_road_color;};
137
138         //! Get the explosion image.
139         PixMask *getExplosionImage() {return explosion;};
140
141         //! Get a road image.  Pass in the index.
142         PixMask *getRoadImage(guint32 i) {return roadpic[i];};
143
144         //! Get a bridge image.  Pass in the index.
145         PixMask *getBridgeImage(guint32 i) {return bridgepic[i];};
146
147         //! Get a flag image.  Pass in the index.
148         PixMask *getFlagImage(guint32 i) {return flagpic[i];};
149
150         //! Get the flag mask.  Pass in the index.
151         PixMask *getFlagMask(guint32 i) {return flagmask[i];};
152
153         //! Get the fog image.  Passin the index.
154         PixMask *getFogImage(guint32 i) {return fogpic[i];};
155
156         //! Get the big selector image.  Pass in the index.
157         PixMask *getSelectorImage(guint32 i) {return selector[i];};
158
159         //! Get the big selector mask.  Pass in the index.
160         PixMask *getSelectorMask(guint32 i) {return selectormask[i];};
161
162         //! Get the small selector image.  Pass in the index.
163         PixMask *getSmallSelectorImage(guint32 i) {return smallselector[i];};
164
165         //! Get the small selector mask.  Pass in the index.
166         PixMask *getSmallSelectorMask(guint32 i) {return smallselectormask[i];};
167
168         //! Get the number of animation frames in the big selector image.
169         guint32 getNumberOfSelectorFrames() {return number_of_selector_frames;};
170
171         //! Get the number of animation frames in the small selector image.
172         guint32 getNumberOfSmallSelectorFrames() {return number_of_small_selector_frames;};
173
174         //! Get filenames in this tileset, excepting the configuration file.
175         void getFilenames(std::list<std::string> &files);
176
177         std::string getConfigurationFile() const;
178
179         // Set Methods
180
181         //! Set the subdirectory of where this Tileset resides on disk.
182         void setSubDir(std::string dir);
183
184         //! Set the unique identifier for this tileset.
185         /**
186          * @note This method is only used in the tileset editor.  
187          */
188         void setId(guint32 id) {d_id = id;}
189
190         //! Set the name of the tileset.
191         /**
192          * @note This method is only used in the tileset editor.
193          */
194         void setName(std::string name) {d_name = name;}
195
196         //! Sets the copyright holders of the tileset.
197         void setCopyright(std::string copy) {d_copyright = copy;};
198
199         //! Sets the license of the tileset.
200         void setLicense(std::string license) {d_license = license;};
201
202         //! Set the description of the tileset.
203         /**
204          * @note This method is only used in the tileset editor.
205          */
206         void setInfo(std::string info) {d_info = info;}
207
208         //!  Sets the tilesize of the tileset.
209         void setTileSize(guint32 tileSize) {d_tileSize = tileSize;}
210
211         //! Sets the basename of the file containing the big selector images.
212         void setLargeSelectorFilename(std::string p){d_large_selector = p;};
213
214         //! Sets the basename of the file containing the small selector images.
215         void setSmallSelectorFilename(std::string p){d_small_selector = p;};
216
217         //! Sets the basename of the file containing the explosion image.
218         void setExplosionFilename(std::string p){d_explosion = p;};
219
220         //! Sets the basename of the file containing the road images.
221         void setRoadsFilename(std::string p){d_roads = p;};
222
223         //! Sets the basename of the file containing the bridge images.
224         void setBridgesFilename(std::string p){d_bridges = p;};
225
226         //! Sets the basename of the file containing the fog images.
227         void setFogFilename(std::string p){d_fog = p;};
228
229         //! Sets the basename of the file containing the flag images.
230         void setFlagsFilename(std::string p){d_flags = p;};
231
232         //! Sets the colour of the road on the smallmap.
233         void setRoadColor(Gdk::Color color) {d_road_color = color;};
234
235         //! Sets the explosion image.
236         void setExplosionImage(PixMask *p) {explosion = p;};
237
238         //! Sets a road image.
239         void setRoadImage(guint32 i, PixMask *p) {roadpic[i] = p;};
240
241         //! Sets a bridge image.
242         void setBridgeImage(guint32 i, PixMask *p) {bridgepic[i] = p;};
243
244         //! Sets a flag image.
245         void setFlagImage(guint32 i, PixMask *p) {flagpic[i] = p;};
246
247         //! Sets a flag mask.
248         void setFlagMask(guint32 i, PixMask *p) {flagmask[i] = p;};
249
250         //! Sets a fog image.
251         void setFogImage(guint32 i, PixMask *p) {fogpic[i] = p;};
252
253         //! Sets a big selector image.
254         void setSelectorImage(guint32 i, PixMask *p) {selector[i] = p;};
255
256         //! Sets a big selector mask.
257         void setSelectorMask(guint32 i, PixMask *p) {selectormask[i] = p;};
258
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;};
263
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;};
266
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;};
269
270
271         //Methods that operate on class data and modify the class data.
272
273         //! Destroy the images assoicated with this tileset.
274         void uninstantiateImages();
275
276         //! Load the images assoicated with this tileset.
277         void instantiateImages();
278
279
280         //Methods that operate on class data and do not modify the class data.
281
282         //! Returns the index to the given terrain type.
283         int getIndex(Tile::Type type) const;
284
285         //! Lookup tilestyle by it's id in this tileset.
286         TileStyle *getTileStyle(guint32 id) const;
287
288         //! Lookup a random tile style.
289         /**
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.
294          *
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.
297          *
298          * @return A pointer to the matching TileStyle object, or NULL if no 
299          *         TileStyle could be found with that given style.
300          */
301         TileStyle *getRandomTileStyle(guint32 index, TileStyle::Type style) const;
302
303         //! Save a Tileset to an opened tile configuration file.
304         /**
305          * @param  The opened XML tile configuration file.
306          */
307         bool save(XML_Helper *helper) const;
308
309         //! Get a unique tile style id among all tile syles in this tileset.
310         int getFreeTileStyleId() const;
311
312         //! Get the largest tile style id of all tile styles in this tileset.
313         int getLargestTileStyleId() const;
314
315         //! Check to see if this tileset is suitable for use within the game.
316         bool validate() const;
317
318           
319         // Static Methods
320
321         //! Return the default height and width of a tile in the tileset.
322         static guint32 getDefaultTileSize();
323
324         //! Create a tileset from the given tileset configuration file.
325         static Tileset *create(std::string file);
326
327         //! Return a list of tileset subdirs in the user's personal collection.
328         static std::list<std::string> scanUserCollection();
329
330         //! Return a list of tileset subdirs in the system collection.
331         static std::list<std::string> scanSystemCollection();
332         
333     private:
334         //! Callback to load Tile objects into the Tileset.
335         bool loadTile(std::string, XML_Helper* helper);
336
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);
345         // DATA
346
347         //! The name of the Tileset.
348         /**
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.
353          */
354         std::string d_name;
355
356         //! The copyright holders of the tileset.
357         std::string d_copyright;
358
359         //! The license of the tileset.
360         std::string d_license;
361
362         //! A unique numeric identifier among all tilesets.
363         guint32 d_id;
364
365         //! The description of the Tileset.
366         /**
367          * Equates to the tileset.d_info XML entity in the tileset
368          * configuration file.
369          * This value is not used.
370          */
371         std::string d_info;
372
373         //! The size of the graphic tiles in the Tileset.
374         /**
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
378          * imagery onscreen.
379          */
380         guint32 d_tileSize;
381
382         //! The subdirectory of the Tileset.
383         /**
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.
387          */
388         std::string d_subdir;
389
390         //! The basename of the small selector image.
391         /**
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.
394          *
395          * The image contains many animation frames, and is masked.
396          *
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 
399          * tileset.
400          */
401         std::string d_small_selector;
402
403         //! The basename of the large selector image.
404         /**
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.
407          *
408          * The image contains many animation frames, and is masked.
409          *
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 
412          * tileset.
413          */
414         std::string d_large_selector;
415
416         //! The basename of the explosion image.
417         /**
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.
420          *
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 
423          * tileset.
424          */
425         std::string d_explosion;
426
427         //! The basename of the fog image.
428         /**
429          * The fog images appear on the bigmap when playing with a hidden map.
430          *
431          * The number and order of frames in the image correlates to the 
432          * FogMap::ShadeType enumeration.
433          *
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 
436          * tileset.
437          */
438         std::string d_fog;
439
440         //! The basename of the road image.
441         /**
442          * The road images appear on the bigmap overlaid on top of all kinds
443          * of tiles except for water.
444          *
445          * The number and order of frames in the image correlates to the 
446          * Road::Type enumeration.
447          *
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 
450          * tileset.
451          */
452         std::string d_roads;
453
454         //! The basename of the bridge image.
455         /**
456          * The bridge images appear on the bigmap overlaid on top of certain
457          * water tiles.
458          *
459          * The number and order of frames in the image correlates to the 
460          * Bridge::Type enumeration.
461          *
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 
464          * tileset.
465          */
466         std::string d_bridges;
467
468         //! The basename of the flag image.
469         /**
470          * The flag images appear on the bigmap beside a stack to indicate the
471          * number of army units in the stack.
472          *
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.
475          *
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 
478          * tileset.
479          */
480         std::string d_flags;
481
482         typedef std::map<guint32, TileStyle*> TileStyleIdMap;
483         //! A map that provides a TileStyle when supplying a TileStyle id.
484         TileStyleIdMap d_tilestyles;
485
486         //! The colour of roads on the smallmap.
487         Gdk::Color d_road_color;
488
489         //! The road images.
490         PixMask* roadpic[ROAD_TYPES];
491
492         //! The bridge images.
493         PixMask* bridgepic[BRIDGE_TYPES];
494
495         //! The flag images.
496         PixMask* flagpic[FLAG_TYPES];
497
498         //! The flag masks.
499         PixMask* flagmask[FLAG_TYPES];
500
501         //! The number of animation frames in the big selector.
502         guint32 number_of_selector_frames;
503
504         //! The image frames in the big selector.
505         std::vector<PixMask* > selector;
506
507         //! The mask frames of the big selector.
508         std::vector<PixMask* > selectormask;
509
510         //! The number of animation frames in the small selector.
511         guint32 number_of_small_selector_frames;
512
513         //! The image frames of the small selector.
514         std::vector<PixMask* > smallselector;
515
516         //! The mask frames of the small selector.
517         std::vector<PixMask* > smallselectormask;
518
519         //! The exposion image.
520         PixMask* explosion;
521
522         //! The fog images.
523         PixMask*fogpic[FOG_TYPES];
524 };
525 #endif // TILESET_H
526
527 // End of file