1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
8 /** @file tree_map.h Map accessors for tree tiles. */
9 
10 #ifndef TREE_MAP_H
11 #define TREE_MAP_H
12 
13 #include "tile_map.h"
14 #include "water_map.h"
15 
16 /**
17  * List of tree types along all landscape types.
18  *
19  * This enumeration contains a list of the different tree types along
20  * all landscape types. The values for the enumerations may be used for
21  * offsets from the grfs files. These points to the start of
22  * the tree list for a landscape. See the TREE_COUNT_* enumerations
23  * for the amount of different trees for a specific landscape.
24  */
25 enum TreeType {
26 	TREE_TEMPERATE    = 0x00, ///< temperate tree
27 	TREE_SUB_ARCTIC   = 0x0C, ///< tree on a sub_arctic landscape
28 	TREE_RAINFOREST   = 0x14, ///< tree on the 'green part' on a sub-tropical map
29 	TREE_CACTUS       = 0x1B, ///< a cactus for the 'desert part' on a sub-tropical map
30 	TREE_SUB_TROPICAL = 0x1C, ///< tree on a sub-tropical map, non-rainforest, non-desert
31 	TREE_TOYLAND      = 0x20, ///< tree on a toyland map
32 	TREE_INVALID      = 0xFF, ///< An invalid tree
33 };
34 
35 /* Counts the number of tree types for each landscape.
36  *
37  * This list contains the counts of different tree types for each landscape. This list contains
38  * 5 entries instead of 4 (as there are only 4 landscape types) as the sub tropic landscape
39  * has two types of area, one for normal trees and one only for cacti.
40  */
41 static const uint TREE_COUNT_TEMPERATE    = TREE_SUB_ARCTIC - TREE_TEMPERATE;    ///< number of tree types on a temperate map.
42 static const uint TREE_COUNT_SUB_ARCTIC   = TREE_RAINFOREST - TREE_SUB_ARCTIC;   ///< number of tree types on a sub arctic map.
43 static const uint TREE_COUNT_RAINFOREST   = TREE_CACTUS     - TREE_RAINFOREST;   ///< number of tree types for the 'rainforest part' of a sub-tropic map.
44 static const uint TREE_COUNT_SUB_TROPICAL = TREE_TOYLAND    - TREE_SUB_TROPICAL; ///< number of tree types for the 'sub-tropic part' of a sub-tropic map.
45 static const uint TREE_COUNT_TOYLAND      = 9;                                   ///< number of tree types on a toyland map.
46 
47 /**
48  * Enumeration for ground types of tiles with trees.
49  *
50  * This enumeration defines the ground types for tiles with trees on it.
51  */
52 enum TreeGround {
53 	TREE_GROUND_GRASS       = 0, ///< normal grass
54 	TREE_GROUND_ROUGH       = 1, ///< some rough tile
55 	TREE_GROUND_SNOW_DESERT = 2, ///< a desert or snow tile, depend on landscape
56 	TREE_GROUND_SHORE       = 3, ///< shore
57 	TREE_GROUND_ROUGH_SNOW  = 4, ///< A snow tile that is rough underneath.
58 };
59 
60 
61 /**
62  * Returns the treetype of a tile.
63  *
64  * This function returns the treetype of a given tile. As there are more
65  * possible treetypes for a tile in a game as the enumeration #TreeType defines
66  * this function may be return a value which isn't catch by an entry of the
67  * enumeration #TreeType. But there is no problem known about it.
68  *
69  * @param t The tile to get the treetype from
70  * @return The treetype of the given tile with trees
71  * @pre Tile t must be of type MP_TREES
72  */
GetTreeType(TileIndex t)73 static inline TreeType GetTreeType(TileIndex t)
74 {
75 	assert(IsTileType(t, MP_TREES));
76 	return (TreeType)_m[t].m3;
77 }
78 
79 /**
80  * Returns the groundtype for tree tiles.
81  *
82  * This function returns the groundtype of a tile with trees.
83  *
84  * @param t The tile to get the groundtype from
85  * @return The groundtype of the tile
86  * @pre Tile must be of type MP_TREES
87  */
GetTreeGround(TileIndex t)88 static inline TreeGround GetTreeGround(TileIndex t)
89 {
90 	assert(IsTileType(t, MP_TREES));
91 	return (TreeGround)GB(_m[t].m2, 6, 3);
92 }
93 
94 /**
95  * Returns the 'density' of a tile with trees.
96  *
97  * This function returns the density of a tile which got trees. Note
98  * that this value doesn't count the number of trees on a tile, use
99  * #GetTreeCount instead. This function instead returns some kind of
100  * groundtype of the tile. As the map-array is finite in size and
101  * the information about the trees must be saved somehow other
102  * information about a tile must be saved somewhere encoded in the
103  * tile. So this function returns the density of a tile for sub arctic
104  * and sub tropical games. This means for sub arctic the type of snowline
105  * (0 to 3 for all 4 types of snowtiles) and for sub tropical the value
106  * 3 for a desert (and 0 for non-desert). The function name is not read as
107  * "get the tree density of a tile" but "get the density of a tile which got trees".
108  *
109  * @param t The tile to get the 'density'
110  * @pre Tile must be of type MP_TREES
111  * @see GetTreeCount
112  */
GetTreeDensity(TileIndex t)113 static inline uint GetTreeDensity(TileIndex t)
114 {
115 	assert(IsTileType(t, MP_TREES));
116 	return GB(_m[t].m2, 4, 2);
117 }
118 
119 /**
120  * Set the density and ground type of a tile with trees.
121  *
122  * This functions saves the ground type and the density which belongs to it
123  * for a given tile.
124  *
125  * @param t The tile to set the density and ground type
126  * @param g The ground type to save
127  * @param d The density to save with
128  * @pre Tile must be of type MP_TREES
129  */
SetTreeGroundDensity(TileIndex t,TreeGround g,uint d)130 static inline void SetTreeGroundDensity(TileIndex t, TreeGround g, uint d)
131 {
132 	assert(IsTileType(t, MP_TREES)); // XXX incomplete
133 	SB(_m[t].m2, 4, 2, d);
134 	SB(_m[t].m2, 6, 3, g);
135 	SetWaterClass(t, g == TREE_GROUND_SHORE ? WATER_CLASS_SEA : WATER_CLASS_INVALID);
136 }
137 
138 /**
139  * Returns the number of trees on a tile.
140  *
141  * This function returns the number of trees of a tile (1-4).
142  * The tile must be contains at least one tree or be more specific: it must be
143  * of type MP_TREES.
144  *
145  * @param t The index to get the number of trees
146  * @return The number of trees (1-4)
147  * @pre Tile must be of type MP_TREES
148  */
GetTreeCount(TileIndex t)149 static inline uint GetTreeCount(TileIndex t)
150 {
151 	assert(IsTileType(t, MP_TREES));
152 	return GB(_m[t].m5, 6, 2) + 1;
153 }
154 
155 /**
156  * Add a amount to the tree-count value of a tile with trees.
157  *
158  * This function add a value to the tree-count value of a tile. This
159  * value may be negative to reduce the tree-counter. If the resulting
160  * value reach 0 it doesn't get converted to a "normal" tile.
161  *
162  * @param t The tile to change the tree amount
163  * @param c The value to add (or reduce) on the tree-count value
164  * @pre Tile must be of type MP_TREES
165  */
AddTreeCount(TileIndex t,int c)166 static inline void AddTreeCount(TileIndex t, int c)
167 {
168 	assert(IsTileType(t, MP_TREES)); // XXX incomplete
169 	_m[t].m5 += c << 6;
170 }
171 
172 /**
173  * Returns the tree growth status.
174  *
175  * This function returns the tree growth status of a tile with trees.
176  *
177  * @param t The tile to get the tree growth status
178  * @return The tree growth status
179  * @pre Tile must be of type MP_TREES
180  */
GetTreeGrowth(TileIndex t)181 static inline uint GetTreeGrowth(TileIndex t)
182 {
183 	assert(IsTileType(t, MP_TREES));
184 	return GB(_m[t].m5, 0, 3);
185 }
186 
187 /**
188  * Add a value to the tree growth status.
189  *
190  * This function adds a value to the tree grow status of a tile.
191  *
192  * @param t The tile to add the value on
193  * @param a The value to add on the tree growth status
194  * @pre Tile must be of type MP_TREES
195  */
AddTreeGrowth(TileIndex t,int a)196 static inline void AddTreeGrowth(TileIndex t, int a)
197 {
198 	assert(IsTileType(t, MP_TREES)); // XXX incomplete
199 	_m[t].m5 += a;
200 }
201 
202 /**
203  * Sets the tree growth status of a tile.
204  *
205  * This function sets the tree growth status of a tile directly with
206  * the given value.
207  *
208  * @param t The tile to change the tree growth status
209  * @param g The new value
210  * @pre Tile must be of type MP_TREES
211  */
SetTreeGrowth(TileIndex t,uint g)212 static inline void SetTreeGrowth(TileIndex t, uint g)
213 {
214 	assert(IsTileType(t, MP_TREES)); // XXX incomplete
215 	SB(_m[t].m5, 0, 3, g);
216 }
217 
218 /**
219  * Get the tick counter of a tree tile.
220  *
221  * Returns the saved tick counter of a given tile.
222  *
223  * @param t The tile to get the counter value from
224  * @pre Tile must be of type MP_TREES
225  */
GetTreeCounter(TileIndex t)226 static inline uint GetTreeCounter(TileIndex t)
227 {
228 	assert(IsTileType(t, MP_TREES));
229 	return GB(_m[t].m2, 0, 4);
230 }
231 
232 /**
233  * Add a value on the tick counter of a tree-tile
234  *
235  * This function adds a value on the tick counter of a tree-tile.
236  *
237  * @param t The tile to add the value on
238  * @param a The value to add on the tick counter
239  * @pre Tile must be of type MP_TREES
240  */
AddTreeCounter(TileIndex t,int a)241 static inline void AddTreeCounter(TileIndex t, int a)
242 {
243 	assert(IsTileType(t, MP_TREES)); // XXX incomplete
244 	_m[t].m2 += a;
245 }
246 
247 /**
248  * Set the tick counter for a tree-tile
249  *
250  * This function sets directly the tick counter for a tree-tile.
251  *
252  * @param t The tile to set the tick counter
253  * @param c The new tick counter value
254  * @pre Tile must be of type MP_TREES
255  */
SetTreeCounter(TileIndex t,uint c)256 static inline void SetTreeCounter(TileIndex t, uint c)
257 {
258 	assert(IsTileType(t, MP_TREES)); // XXX incomplete
259 	SB(_m[t].m2, 0, 4, c);
260 }
261 
262 /**
263  * Make a tree-tile.
264  *
265  * This functions change the tile to a tile with trees and all information which belongs to it.
266  *
267  * @param t The tile to make a tree-tile from
268  * @param type The type of the tree
269  * @param count the number of trees
270  * @param growth the growth status
271  * @param ground the ground type
272  * @param density the density (not the number of trees)
273  */
MakeTree(TileIndex t,TreeType type,uint count,uint growth,TreeGround ground,uint density)274 static inline void MakeTree(TileIndex t, TreeType type, uint count, uint growth, TreeGround ground, uint density)
275 {
276 	SetTileType(t, MP_TREES);
277 	SetTileOwner(t, OWNER_NONE);
278 	SetWaterClass(t, ground == TREE_GROUND_SHORE ? WATER_CLASS_SEA : WATER_CLASS_INVALID);
279 	_m[t].m2 = ground << 6 | density << 4 | 0;
280 	_m[t].m3 = type;
281 	_m[t].m4 = 0 << 5 | 0 << 2;
282 	_m[t].m5 = count << 6 | growth;
283 	SB(_me[t].m6, 2, 4, 0);
284 	_me[t].m7 = 0;
285 }
286 
287 #endif /* TREE_MAP_H */
288