4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file clear_map.h Map accessors for 'clear' tiles */
15 #include "bridge_map.h"
16 #include "industry_type.h"
19 * Ground types. Valid densities in comments after the enum.
22 CLEAR_GRASS
= 0, ///< 0-3
23 CLEAR_ROUGH
= 1, ///< 3
24 CLEAR_ROCKS
= 2, ///< 3
25 CLEAR_FIELDS
= 3, ///< 3
26 CLEAR_SNOW
= 4, ///< 0-3
27 CLEAR_DESERT
= 5 ///< 1,3
32 * Test if a tile is covered with snow.
33 * @param t the tile to check
34 * @pre IsTileType(t, MP_CLEAR)
35 * @return whether the tile is covered with snow.
37 static inline bool IsSnowTile(TileIndex t
)
39 assert(IsTileType(t
, MP_CLEAR
));
40 return HasBit(_m
[t
].m3
, 4);
44 * Get the type of clear tile but never return CLEAR_SNOW.
45 * @param t the tile to get the clear ground type of
46 * @pre IsTileType(t, MP_CLEAR)
47 * @return the ground type
49 static inline ClearGround
GetRawClearGround(TileIndex t
)
51 assert(IsTileType(t
, MP_CLEAR
));
52 return (ClearGround
)GB(_m
[t
].m5
, 2, 3);
56 * Get the type of clear tile.
57 * @param t the tile to get the clear ground type of
58 * @pre IsTileType(t, MP_CLEAR)
59 * @return the ground type
61 static inline ClearGround
GetClearGround(TileIndex t
)
63 if (IsSnowTile(t
)) return CLEAR_SNOW
;
64 return GetRawClearGround(t
);
68 * Set the type of clear tile.
69 * @param t the tile to set the clear ground type of
70 * @param ct the ground type
71 * @pre IsTileType(t, MP_CLEAR)
73 static inline bool IsClearGround(TileIndex t
, ClearGround ct
)
75 return GetClearGround(t
) == ct
;
80 * Get the density of a non-field clear tile.
81 * @param t the tile to get the density of
82 * @pre IsTileType(t, MP_CLEAR)
85 static inline uint
GetClearDensity(TileIndex t
)
87 assert(IsTileType(t
, MP_CLEAR
));
88 return GB(_m
[t
].m5
, 0, 2);
92 * Increment the density of a non-field clear tile.
93 * @param t the tile to increment the density of
94 * @param d the amount to increment the density with
95 * @pre IsTileType(t, MP_CLEAR)
97 static inline void AddClearDensity(TileIndex t
, int d
)
99 assert(IsTileType(t
, MP_CLEAR
)); // XXX incomplete
104 * Set the density of a non-field clear tile.
105 * @param t the tile to set the density of
106 * @param d the new density
107 * @pre IsTileType(t, MP_CLEAR)
109 static inline void SetClearDensity(TileIndex t
, uint d
)
111 assert(IsTileType(t
, MP_CLEAR
));
112 SB(_m
[t
].m5
, 0, 2, d
);
117 * Get the counter used to advance to the next clear density/field type.
118 * @param t the tile to get the counter of
119 * @pre IsTileType(t, MP_CLEAR)
120 * @return the value of the counter
122 static inline uint
GetClearCounter(TileIndex t
)
124 assert(IsTileType(t
, MP_CLEAR
));
125 return GB(_m
[t
].m5
, 5, 3);
129 * Increments the counter used to advance to the next clear density/field type.
130 * @param t the tile to increment the counter of
131 * @param c the amount to increment the counter with
132 * @pre IsTileType(t, MP_CLEAR)
134 static inline void AddClearCounter(TileIndex t
, int c
)
136 assert(IsTileType(t
, MP_CLEAR
)); // XXX incomplete
141 * Sets the counter used to advance to the next clear density/field type.
142 * @param t the tile to set the counter of
143 * @param c the amount to set the counter to
144 * @pre IsTileType(t, MP_CLEAR)
146 static inline void SetClearCounter(TileIndex t
, uint c
)
148 assert(IsTileType(t
, MP_CLEAR
)); // XXX incomplete
149 SB(_m
[t
].m5
, 5, 3, c
);
154 * Sets ground type and density in one go, also sets the counter to 0
155 * @param t the tile to set the ground type and density for
156 * @param type the new ground type of the tile
157 * @param density the density of the ground tile
158 * @pre IsTileType(t, MP_CLEAR)
160 static inline void SetClearGroundDensity(TileIndex t
, ClearGround type
, uint density
)
162 assert(IsTileType(t
, MP_CLEAR
)); // XXX incomplete
163 _m
[t
].m5
= 0 << 5 | type
<< 2 | density
;
168 * Get the field type (production stage) of the field
169 * @param t the field to get the type of
170 * @pre GetClearGround(t) == CLEAR_FIELDS
171 * @return the field type
173 static inline uint
GetFieldType(TileIndex t
)
175 assert(GetClearGround(t
) == CLEAR_FIELDS
);
176 return GB(_m
[t
].m3
, 0, 4);
180 * Set the field type (production stage) of the field
181 * @param t the field to get the type of
182 * @param f the field type
183 * @pre GetClearGround(t) == CLEAR_FIELDS
185 static inline void SetFieldType(TileIndex t
, uint f
)
187 assert(GetClearGround(t
) == CLEAR_FIELDS
); // XXX incomplete
188 SB(_m
[t
].m3
, 0, 4, f
);
192 * Get the industry (farm) that made the field
193 * @param t the field to get creating industry of
194 * @pre GetClearGround(t) == CLEAR_FIELDS
195 * @return the industry that made the field
197 static inline IndustryID
GetIndustryIndexOfField(TileIndex t
)
199 assert(GetClearGround(t
) == CLEAR_FIELDS
);
200 return(IndustryID
) _m
[t
].m2
;
204 * Set the industry (farm) that made the field
205 * @param t the field to get creating industry of
206 * @param i the industry that made the field
207 * @pre GetClearGround(t) == CLEAR_FIELDS
209 static inline void SetIndustryIndexOfField(TileIndex t
, IndustryID i
)
211 assert(GetClearGround(t
) == CLEAR_FIELDS
);
217 * Is there a fence at the south eastern border?
218 * @param t the tile to check for fences
219 * @pre IsTileType(t, MP_CLEAR) || IsTileType(t, MP_TREES)
220 * @return 0 if there is no fence, otherwise the fence type
222 static inline uint
GetFenceSE(TileIndex t
)
224 assert(IsTileType(t
, MP_CLEAR
) || IsTileType(t
, MP_TREES
));
225 return GB(_m
[t
].m4
, 2, 3);
229 * Sets the type of fence (and whether there is one) for the south
231 * @param t the tile to check for fences
232 * @param h 0 if there is no fence, otherwise the fence type
233 * @pre IsTileType(t, MP_CLEAR) || IsTileType(t, MP_TREES)
235 static inline void SetFenceSE(TileIndex t
, uint h
)
237 assert(IsTileType(t
, MP_CLEAR
) || IsTileType(t
, MP_TREES
)); // XXX incomplete
238 SB(_m
[t
].m4
, 2, 3, h
);
242 * Is there a fence at the south western border?
243 * @param t the tile to check for fences
244 * @pre IsTileType(t, MP_CLEAR) || IsTileType(t, MP_TREES)
245 * @return 0 if there is no fence, otherwise the fence type
247 static inline uint
GetFenceSW(TileIndex t
)
249 assert(IsTileType(t
, MP_CLEAR
) || IsTileType(t
, MP_TREES
));
250 return GB(_m
[t
].m4
, 5, 3);
254 * Sets the type of fence (and whether there is one) for the south
256 * @param t the tile to check for fences
257 * @param h 0 if there is no fence, otherwise the fence type
258 * @pre IsTileType(t, MP_CLEAR) || IsTileType(t, MP_TREES)
260 static inline void SetFenceSW(TileIndex t
, uint h
)
262 assert(IsTileType(t
, MP_CLEAR
) || IsTileType(t
, MP_TREES
)); // XXX incomplete
263 SB(_m
[t
].m4
, 5, 3, h
);
269 * @param t the tile to make a clear tile
270 * @param g the type of ground
271 * @param density the density of the grass/snow/desert etc
273 static inline void MakeClear(TileIndex t
, ClearGround g
, uint density
)
275 /* If this is a non-bridgeable tile, clear the bridge bits while the rest
276 * of the tile information is still here. */
277 if (!MayHaveBridgeAbove(t
)) SB(_m
[t
].m6
, 6, 2, 0);
279 SetTileType(t
, MP_CLEAR
);
281 SetTileOwner(t
, OWNER_NONE
);
284 _m
[t
].m4
= 0 << 5 | 0 << 2;
285 SetClearGroundDensity(t
, g
, density
); // Sets m5
286 SB(_m
[t
].m6
, 2, 4, 0); // Other bits are "tropic zone" and "bridge above"
292 * Make a (farm) field tile.
293 * @param t the tile to make a farm field
294 * @param field_type the 'growth' level of the field
295 * @param industry the industry this tile belongs to
297 static inline void MakeField(TileIndex t
, uint field_type
, IndustryID industry
)
299 SetTileType(t
, MP_CLEAR
);
301 SetTileOwner(t
, OWNER_NONE
);
303 _m
[t
].m3
= field_type
;
304 _m
[t
].m4
= 0 << 5 | 0 << 2;
305 SetClearGroundDensity(t
, CLEAR_FIELDS
, 3);
306 SB(_m
[t
].m6
, 2, 4, 0);
312 * @param t the tile to make snowy
313 * @param density The density of snowiness.
314 * @pre GetClearGround(t) != CLEAR_SNOW
316 static inline void MakeSnow(TileIndex t
, uint density
= 0)
318 assert(GetClearGround(t
) != CLEAR_SNOW
);
320 if (GetRawClearGround(t
) == CLEAR_FIELDS
) {
321 SetClearGroundDensity(t
, CLEAR_GRASS
, density
);
323 SetClearDensity(t
, density
);
328 * Clear the snow from a tile and return it to its previous type.
329 * @param t the tile to clear of snow
330 * @pre GetClearGround(t) == CLEAR_SNOW
332 static inline void ClearSnow(TileIndex t
)
334 assert(GetClearGround(t
) == CLEAR_SNOW
);
336 SetClearDensity(t
, 3);
339 #endif /* CLEAR_MAP_H */