Add templated versions of CeilDiv and Ceil maths functions
[openttd-joker.git] / src / clear_map.h
blobd6e4cbf8336e2b14945ac1a3326e03741bc901c7
1 /* $Id: clear_map.h 25852 2013-10-12 22:23:43Z zuu $ */
3 /*
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/>.
8 */
10 /** @file clear_map.h Map accessors for 'clear' tiles */
12 #ifndef CLEAR_MAP_H
13 #define CLEAR_MAP_H
15 #include "bridge_map.h"
16 #include "industry_type.h"
18 /**
19 * Ground types. Valid densities in comments after the enum.
21 enum ClearGround {
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
31 /**
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);
43 /**
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);
55 /**
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);
67 /**
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;
79 /**
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)
83 * @return the density
85 static inline uint GetClearDensity(TileIndex t)
87 assert(IsTileType(t, MP_CLEAR));
88 return GB(_m[t].m5, 0, 2);
91 /**
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
100 _m[t].m5 += d;
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
137 _m[t].m5 += c << 5;
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);
212 _m[t].m2 = i;
217 * Is there a fence at the given border?
218 * @param t the tile to check for fences
219 * @param side the border to check
220 * @pre IsClearGround(t, CLEAR_FIELDS)
221 * @return 0 if there is no fence, otherwise the fence type
223 static inline uint GetFence(TileIndex t, DiagDirection side)
225 assert(IsClearGround(t, CLEAR_FIELDS));
226 switch (side) {
227 default: NOT_REACHED();
228 case DIAGDIR_SE: return GB(_m[t].m4, 2, 3);
229 case DIAGDIR_SW: return GB(_m[t].m4, 5, 3);
230 case DIAGDIR_NE: return GB(_m[t].m3, 5, 3);
231 case DIAGDIR_NW: return GB(_me[t].m6, 2, 3);
236 * Sets the type of fence (and whether there is one) for the given border.
237 * @param t the tile to check for fences
238 * @param side the border to check
239 * @param h 0 if there is no fence, otherwise the fence type
240 * @pre IsClearGround(t, CLEAR_FIELDS)
242 static inline void SetFence(TileIndex t, DiagDirection side, uint h)
244 assert(IsClearGround(t, CLEAR_FIELDS));
245 switch (side) {
246 default: NOT_REACHED();
247 case DIAGDIR_SE: SB(_m[t].m4, 2, 3, h); break;
248 case DIAGDIR_SW: SB(_m[t].m4, 5, 3, h); break;
249 case DIAGDIR_NE: SB(_m[t].m3, 5, 3, h); break;
250 case DIAGDIR_NW: SB(_me[t].m6, 2, 3, h); break;
256 * Make a clear tile.
257 * @param t the tile to make a clear tile
258 * @param g the type of ground
259 * @param density the density of the grass/snow/desert etc
261 static inline void MakeClear(TileIndex t, ClearGround g, uint density)
263 SetTileType(t, MP_CLEAR);
264 _m[t].m1 = 0;
265 SetTileOwner(t, OWNER_NONE);
266 _m[t].m2 = 0;
267 _m[t].m3 = 0;
268 _m[t].m4 = 0 << 5 | 0 << 2;
269 SetClearGroundDensity(t, g, density); // Sets m5
270 _me[t].m6 = 0;
271 _me[t].m7 = 0;
276 * Make a (farm) field tile.
277 * @param t the tile to make a farm field
278 * @param field_type the 'growth' level of the field
279 * @param industry the industry this tile belongs to
281 static inline void MakeField(TileIndex t, uint field_type, IndustryID industry)
283 SetTileType(t, MP_CLEAR);
284 _m[t].m1 = 0;
285 SetTileOwner(t, OWNER_NONE);
286 _m[t].m2 = industry;
287 _m[t].m3 = field_type;
288 _m[t].m4 = 0 << 5 | 0 << 2;
289 SetClearGroundDensity(t, CLEAR_FIELDS, 3);
290 SB(_me[t].m6, 2, 4, 0);
291 _me[t].m7 = 0;
295 * Make a snow tile.
296 * @param t the tile to make snowy
297 * @param density The density of snowiness.
298 * @pre GetClearGround(t) != CLEAR_SNOW
300 static inline void MakeSnow(TileIndex t, uint density = 0)
302 assert(GetClearGround(t) != CLEAR_SNOW);
303 SetBit(_m[t].m3, 4);
304 if (GetRawClearGround(t) == CLEAR_FIELDS) {
305 SetClearGroundDensity(t, CLEAR_GRASS, density);
306 } else {
307 SetClearDensity(t, density);
312 * Clear the snow from a tile and return it to its previous type.
313 * @param t the tile to clear of snow
314 * @pre GetClearGround(t) == CLEAR_SNOW
316 static inline void ClearSnow(TileIndex t)
318 assert(GetClearGround(t) == CLEAR_SNOW);
319 ClrBit(_m[t].m3, 4);
320 SetClearDensity(t, 3);
323 #endif /* CLEAR_MAP_H */