1 //       _________ __                 __
2 //      /   _____//  |_____________ _/  |______     ____  __ __  ______
3 //      \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
4 //      /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ |
5 //     /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
6 //             \/                  \/          \//_____/            \/
7 //  ______________________                           ______________________
8 //                        T H E   W A R   B E G I N S
9 //         Stratagus - A free fantasy real time strategy game engine
10 //
11 /**@name tileset.h - The tileset headerfile. */
12 //
13 //      (c) Copyright 1998-2005 by Lutz Sammer and Jimmy Salmon
14 //
15 //      This program is free software; you can redistribute it and/or modify
16 //      it under the terms of the GNU General Public License as published by
17 //      the Free Software Foundation; only version 2 of the License.
18 //
19 //      This program is distributed in the hope that it will be useful,
20 //      but WITHOUT ANY WARRANTY; without even the implied warranty of
21 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 //      GNU General Public License for more details.
23 //
24 //      You should have received a copy of the GNU General Public License
25 //      along with this program; if not, write to the Free Software
26 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 //      02111-1307, USA.
28 //
29 
30 #ifndef TILESET_H
31 #define TILESET_H
32 
33 //@{
34 
35 /*----------------------------------------------------------------------------
36 --  Declarations
37 ----------------------------------------------------------------------------*/
38 
39 #include "vec2i.h"
40 #include <string>
41 #include <vector>
42 
43 struct lua_State;
44 
45 // Not used until now:
46 #define MapFieldSpeedMask 0x0003  /// Move faster on this tile
47 
48 #define MapFieldOpaque 0x0004  /// Units can't see through this field (FoW)
49 
50 #define MapFieldHuman 0x0008  /// Human is owner of the field (walls)
51 
52 #define MapFieldLandAllowed  0x0010  /// Land units allowed
53 #define MapFieldCoastAllowed 0x0020  /// Coast (transporter) units allowed
54 #define MapFieldWaterAllowed 0x0040  /// Water units allowed
55 #define MapFieldNoBuilding   0x0080  /// No buildings allowed
56 
57 #define MapFieldUnpassable 0x0100  /// Field is movement blocked
58 #define MapFieldWall       0x0200  /// Field contains wall
59 #define MapFieldRocks      0x0400  /// Field contains rocks
60 #define MapFieldForest     0x0800  /// Field contains forest
61 
62 #define MapFieldLandUnit 0x1000  /// Land unit on field
63 #define MapFieldAirUnit  0x2000  /// Air unit on field
64 #define MapFieldSeaUnit  0x4000  /// Water unit on field
65 #define MapFieldBuilding 0x8000  /// Building on field
66 
67 #define MapFieldDecorative 0x10000  /// A field that needs no mixing with the surroundings, for the editor
68 
69 /**
70 **  These are used for lookup tiles types
71 **  mainly used for the FOW implementation of the seen woods/rocks
72 **
73 **  @todo I think this can be removed, we can use the flags?
74 **  I'm not sure, if we have seen and real time to considere.
75 */
76 enum TileType {
77 	TileTypeUnknown,    /// Unknown tile type
78 	TileTypeWood,       /// Any wood tile
79 	TileTypeRock,       /// Any rock tile
80 	TileTypeCoast,      /// Any coast tile
81 	TileTypeHumanWall,  /// Any human wall tile
82 	TileTypeOrcWall,    /// Any orc wall tile
83 	TileTypeWater       /// Any water tile
84 };
85 
86 /// Single tile definition
87 struct CTileInfo {
88 public:
CTileInfoCTileInfo89 	CTileInfo() : BaseTerrain(0), MixTerrain(0)
90 	{}
CTileInfoCTileInfo91 	CTileInfo(unsigned char base, unsigned char mix) : BaseTerrain(base), MixTerrain(mix)
92 	{}
93 
94 	bool operator ==(const CTileInfo &rhs) const
95 	{
96 		return BaseTerrain == rhs.BaseTerrain && MixTerrain == rhs.MixTerrain;
97 	}
98 	bool operator !=(const CTileInfo &rhs) const { return !(*this == rhs); }
99 
100 public:
101 	unsigned char BaseTerrain; /// Basic terrain of the tile
102 	unsigned char MixTerrain;  /// Terrain mixed with this
103 };
104 
105 /// Definition for a terrain type
106 struct SolidTerrainInfo {
107 	std::string TerrainName;  /// Name of the terrain
108 	// TODO: When drawing with the editor add some kind fo probabilities for every tile.
109 };
110 
111 class CTile
112 {
113 public:
CTile()114 	CTile() : tile(0), flag(0) {}
115 
116 public:
117 	unsigned short tile;  /// graphical pos
118 	unsigned int flag;    /// Flag
119 	CTileInfo tileinfo;   /// Tile descriptions
120 };
121 
122 /// Tileset definition
123 class CTileset
124 {
125 public:
126 	void clear();
127 
getTileCount()128 	unsigned int getTileCount() const { return tiles.size(); }
129 
130 	unsigned int getDefaultTileIndex() const;
131 	unsigned int getDefaultWoodTileIndex() const;
132 
133 	bool isAWallTile(unsigned tile) const;
134 	bool isARaceWallTile(unsigned tile, bool human) const;
135 	bool isAWoodTile(unsigned tile) const;
136 	bool isARockTile(unsigned tile) const;
137 
getPixelTileSize()138 	const PixelSize &getPixelTileSize() const { return pixelTileSize; }
139 
getRemovedRockTile()140 	unsigned getRemovedRockTile() const { return removedRockTile; }
getRemovedTreeTile()141 	unsigned getRemovedTreeTile() const { return removedTreeTile; }
getBottomOneTreeTile()142 	unsigned getBottomOneTreeTile() const { return botOneTreeTile; }
getTopOneTreeTile()143 	unsigned getTopOneTreeTile() const { return topOneTreeTile; }
getMidOneTreeTile()144 	unsigned getMidOneTreeTile() const { return midOneTreeTile; }
145 
146 	unsigned getWallDirection(int tileIndex, bool human) const;
147 
148 	unsigned getHumanWallTileIndex(int dirFlag) const;
149 	unsigned getOrcWallTileIndex(int dirFlag) const;
150 	unsigned getHumanWallTileIndex_broken(int dirFlag) const;
151 	unsigned getOrcWallTileIndex_broken(int dirFlag) const;
152 	unsigned getHumanWallTileIndex_destroyed(int dirFlag) const;
153 	unsigned getOrcWallTileIndex_destroyed(int dirFlag) const;
154 
155 	unsigned int getSolidTerrainCount() const;
156 
157 	const std::string &getTerrainName(int solidTerrainIndex) const;
158 
159 	int findTileIndexByTile(unsigned int tile) const;
160 	unsigned int getTileNumber(int basic, bool random, bool filler) const;
161 	void fillSolidTiles(std::vector<unsigned int> *solidTiles) const;
162 
163 	unsigned getQuadFromTile(unsigned int tile) const;
164 	int getTileBySurrounding(unsigned short type,
165 							 int up, int right,
166 							 int bottom, int left) const;
167 	int tileFromQuad(unsigned fixed, unsigned quad) const;
168 	bool isEquivalentTile(unsigned int tile1, unsigned int tile2) const;
169 
170 	void parse(lua_State *l);
171 	void buildTable(lua_State *l);
172 	int parseTilesetTileFlags(lua_State *l, int *back, int *j);
173 
174 private:
175 	unsigned int getOrAddSolidTileIndexByName(const std::string &name);
176 	int findTileIndex(unsigned char baseTerrain, unsigned char mixTerrain = 0) const;
177 	int getTileIndex(unsigned char baseTerrain, unsigned char mixTerrain, unsigned int quad) const;
178 	void buildWallReplacementTable();
179 	void parseSlots(lua_State *l, int t);
180 	void parseSpecial(lua_State *l);
181 	void parseSolid(lua_State *l);
182 	void parseMixed(lua_State *l);
183 	int findTilePath(int base, int goal, int length, std::vector<char> &marks, int *tileIndex) const;
184 public:
185 	std::string Name;           /// Nice name to display
186 	std::string ImageFile;      /// File containing image data
187 
188 public:
189 	std::vector<CTile> tiles;
190 
191 	// TODO: currently hardcoded
192 	std::vector<unsigned char> TileTypeTable;  /// For fast lookup of tile type
193 private:
194 	PixelSize pixelTileSize;    /// Size of a tile in pixel
195 	std::vector<SolidTerrainInfo> solidTerrainTypes; /// Information about solid terrains.
196 #if 1
197 	std::vector<int> mixedLookupTable;  /// Lookup for what part of tile used
198 	unsigned topOneTreeTile;   /// Tile for one tree top
199 	unsigned midOneTreeTile;   /// Tile for one tree middle
200 	unsigned botOneTreeTile;   /// Tile for one tree bottom
201 	unsigned removedTreeTile;  /// Tile placed where trees are gone
202 	int woodTable[20];     /// Table for tree removable
203 	unsigned topOneRockTile;   /// Tile for one rock top
204 	unsigned midOneRockTile;   /// Tile for one rock middle
205 	unsigned botOneRockTile;   /// Tile for one rock bottom
206 	unsigned removedRockTile;  /// Tile placed where rocks are gone
207 	int rockTable[20];     /// Removed rock placement table
208 	unsigned humanWallTable[16];  /// Human wall placement table
209 	unsigned orcWallTable[16];    /// Orc wall placement table
210 #endif
211 };
212 
213 /*----------------------------------------------------------------------------
214 --  Functions
215 ----------------------------------------------------------------------------*/
216 
217 extern void ParseTilesetTileFlags(lua_State *l, int *back, int *j);
218 
219 //@}
220 
221 #endif // !TILESET_H
222