initial commit, lordsawar source, slightly modified
[lordsawar] / src / Tile.h
1 // Copyright (C) 2001, 2002, 2003 Michael Bartl
2 // Copyright (C) 2001, 2002, 2004, 2005, 2006 Ulf Lorenz
3 // Copyright (C) 2007, 2008 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 TILE_H
21 #define TILE_H
22
23 #include <string>
24 #include <gtkmm.h>
25
26 #include "xmlhelper.h"
27
28 #include "tilestyleset.h"
29
30 class Tileset;
31 class SmallTile;
32 //! Describes a kind of tile that a Stack can traverse.
33 /** 
34  * Many tiles are put together to form a tileset. Thus, a tile describes a
35  * single terrain type. It keeps the name, movement points, type (grass, water
36  * etc.) and also keeps the images together.
37  *
38  * Each tile holds a list of TileStyleSet objects which hold a list of 
39  * TileStyle objects.  Each TileStyleSet holds a bunch of pictures of
40  * what this tile can look like.  A TileStyle is a single picture.
41  * These TileStyle pictures are displayed on the BigMap using the MapRenderer.
42  *
43  * Tile objects are held in a Tileset object.
44  * Maptile objects refer to Tile objects.
45  */
46 class Tile : public std::list<TileStyleSet*>
47 {
48     public:
49         //! The xml tag of this object in a tileset configuration file.
50         static std::string d_tag; 
51
52         //! Enumerate the kinds of terrain that a Stack can potentially move on.
53         enum Type { 
54           //! Synomymous with GRASS.
55           NONE = 0, 
56           //! Grassy plain.  Flat.  Open.  Easy to pass through.
57           GRASS = NONE, 
58           //! Lake, ocean, river, puddle, moat, or anything else watery.
59           WATER = 1, 
60           //! Trees in great abundance, also includes shrubberies.
61           FOREST = 2, 
62           //! Hilly terrain, generally passable.
63           HILLS = 4,
64           //! Very hilly terrain, generally not passable except by flight.
65           MOUNTAIN = 8, 
66           //! Marshy terrain.
67           SWAMP = 16,
68           //! Terrain that no army units can cross, even when flying.
69           VOID = 32
70         };
71
72         //! Default constructor.
73         Tile();
74
75         //! Loading constructor.
76         /**
77          * Loads the tileset.tile XML entities in the tileset configuration 
78          * files.
79          * */
80         Tile(XML_Helper* helper);
81
82         //! Destructor.
83         ~Tile();
84
85         // Get Methods
86
87         //! Get the number of movement points needed to cross this tile
88         guint32 getMoves() const {return d_moves;}
89
90         //! Get the type (grass, hill,...) of this tile type.
91         Type getType() const {return d_type;}
92
93         //! Get the name of this kind of tile (used in the editor).
94         std::string getName() const {return d_name;}
95
96         int getTypeIndex() {return getTypeIndexForType(d_type);}
97
98         SmallTile * getSmallTile() {return d_smalltile;};
99
100
101         // Set Methods
102
103         void setType(Type type) {d_type = type;}
104
105         //! Set the name of this kind of tile (used in the editor).
106         void setName(std::string name) {d_name = name;}
107
108         void setTypeByIndex(int idx);
109
110         //! Set the SmallTile object associated with this tile.
111         void setSmallTile(SmallTile *smalltile) {d_smalltile = smalltile;};
112
113
114         // Methods the operate on the class data but do not modify the class
115
116         //! Save a Tile to an opened tile configuration file.
117         /**
118          * @param  The opened XML tile configuration file.
119          */
120         bool save(XML_Helper *helper) const;
121
122         //! Check to see if this tile is suitable for use within the game.
123         bool validate() const;
124
125         //! Check to see if the tilestylesets only contain simple tilestyles.
126         bool consistsOnlyOfLoneAndOtherStyles() const;
127
128         //! Lookup a random tile style for this tile.
129         /**
130          * Scan the TileStyles for this Tile for a TileStyle that matches 
131          * the given style.  When there is more than one TileStyle to choose 
132          * from, randomly pick one from all of the matching TileStyle objects.
133          *
134          * @param style  The kind of style we're looking for.
135          *
136          * @return A pointer to the matching TileStyle object, or NULL if no 
137          *         TileStyle could be found with that given style.
138          */
139         TileStyle *getRandomTileStyle (TileStyle::Type style) const;
140
141         // Methods the operate on the class data and modify the class
142
143         //! Destroy the images associated with this tile.
144         void uninstantiateImages();
145
146         //! Load the images associated with this tile.
147         void instantiateImages(int tilesize, Tileset *ts);
148
149
150         // Static Methods
151
152         //! Convert a Tile::Type enumerated value to a string.
153         static std::string tileTypeToString(const Tile::Type type);
154
155         //! Convert a string represenation of a Tile::Type to an enum value.
156         static Tile::Type tileTypeFromString(const std::string str);
157
158         //! If an army unit can move on these kinds of terrains, it is flying.
159         static guint32 isFlying() 
160           {return FOREST | HILLS | WATER | SWAMP | MOUNTAIN;};
161
162         static int getTypeIndexForType(Tile::Type type);
163
164     private:
165
166         //! Check to see if the grass tilestyles are suitable for in-game use.
167         bool validateGrass(std::list<TileStyle::Type> types) const;
168
169         //! Check to see if the other tilestyles are suitable for in-game use.
170         bool validateFeature(std::list<TileStyle::Type> types) const;
171
172         // DATA
173
174         //! The name of this kind of a tile.
175         /**
176          * The name is taken from the tileset configuration file.
177          * This value doesn't change during gameplay.
178          * It used in the scenario editor, but not used in the game.
179          * Equates to the tileset.tile.d_name XML entities in the tileset
180          * configuration file.
181          */
182         std::string d_name;
183
184         //! The number of movement points required to cross this tile.
185         /**
186          * If an Army unit cannot traverse the tile efficiently it pays
187          * this number of movement points to walk over this tile.
188          * This value doesn't change during gameplay.
189          * Equates to the tileset.tile.d_moves XML entities in the tileset
190          * configuration file.
191          */
192         guint32 d_moves;
193         
194         //! The kind of terrain tile this instance represents.
195         /**
196          * Equates to the tileset.tile.d_type XML entities in the tileset
197          * configuration file.
198          */
199         Type d_type;
200
201         //! What this Tile looks like when it's shown on the miniature map.
202         SmallTile *d_smalltile;
203
204 };
205
206 #endif // TILE_H
207
208 // End of file