Codechange: Avoid unnecessary re-reads/seeks in RandomAccessFile::ReadBlock
[openttd-github.git] / src / clear_map.h
blob64eb640d12bce94821995b4ce079eb9a99c417e1
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 */
8 /** @file clear_map.h Map accessors for 'clear' tiles */
10 #ifndef CLEAR_MAP_H
11 #define CLEAR_MAP_H
13 #include "bridge_map.h"
14 #include "industry_type.h"
16 /**
17 * Ground types. Valid densities in comments after the enum.
19 enum ClearGround {
20 CLEAR_GRASS = 0, ///< 0-3
21 CLEAR_ROUGH = 1, ///< 3
22 CLEAR_ROCKS = 2, ///< 3
23 CLEAR_FIELDS = 3, ///< 3
24 CLEAR_SNOW = 4, ///< 0-3
25 CLEAR_DESERT = 5, ///< 1,3
29 /**
30 * Test if a tile is covered with snow.
31 * @param t the tile to check
32 * @pre IsTileType(t, MP_CLEAR)
33 * @return whether the tile is covered with snow.
35 inline bool IsSnowTile(Tile t)
37 assert(IsTileType(t, MP_CLEAR));
38 return HasBit(t.m3(), 4);
41 /**
42 * Get the type of clear tile but never return CLEAR_SNOW.
43 * @param t the tile to get the clear ground type of
44 * @pre IsTileType(t, MP_CLEAR)
45 * @return the ground type
47 inline ClearGround GetRawClearGround(Tile t)
49 assert(IsTileType(t, MP_CLEAR));
50 return (ClearGround)GB(t.m5(), 2, 3);
53 /**
54 * Get the type of clear tile.
55 * @param t the tile to get the clear ground type of
56 * @pre IsTileType(t, MP_CLEAR)
57 * @return the ground type
59 inline ClearGround GetClearGround(Tile t)
61 if (IsSnowTile(t)) return CLEAR_SNOW;
62 return GetRawClearGround(t);
65 /**
66 * Set the type of clear tile.
67 * @param t the tile to set the clear ground type of
68 * @param ct the ground type
69 * @pre IsTileType(t, MP_CLEAR)
71 inline bool IsClearGround(Tile t, ClearGround ct)
73 return GetClearGround(t) == ct;
77 /**
78 * Get the density of a non-field clear tile.
79 * @param t the tile to get the density of
80 * @pre IsTileType(t, MP_CLEAR)
81 * @return the density
83 inline uint GetClearDensity(Tile t)
85 assert(IsTileType(t, MP_CLEAR));
86 return GB(t.m5(), 0, 2);
89 /**
90 * Increment the density of a non-field clear tile.
91 * @param t the tile to increment the density of
92 * @param d the amount to increment the density with
93 * @pre IsTileType(t, MP_CLEAR)
95 inline void AddClearDensity(Tile t, int d)
97 assert(IsTileType(t, MP_CLEAR)); // XXX incomplete
98 t.m5() += d;
102 * Set the density of a non-field clear tile.
103 * @param t the tile to set the density of
104 * @param d the new density
105 * @pre IsTileType(t, MP_CLEAR)
107 inline void SetClearDensity(Tile t, uint d)
109 assert(IsTileType(t, MP_CLEAR));
110 SB(t.m5(), 0, 2, d);
115 * Get the counter used to advance to the next clear density/field type.
116 * @param t the tile to get the counter of
117 * @pre IsTileType(t, MP_CLEAR)
118 * @return the value of the counter
120 inline uint GetClearCounter(Tile t)
122 assert(IsTileType(t, MP_CLEAR));
123 return GB(t.m5(), 5, 3);
127 * Increments the counter used to advance to the next clear density/field type.
128 * @param t the tile to increment the counter of
129 * @param c the amount to increment the counter with
130 * @pre IsTileType(t, MP_CLEAR)
132 inline void AddClearCounter(Tile t, int c)
134 assert(IsTileType(t, MP_CLEAR)); // XXX incomplete
135 t.m5() += c << 5;
139 * Sets the counter used to advance to the next clear density/field type.
140 * @param t the tile to set the counter of
141 * @param c the amount to set the counter to
142 * @pre IsTileType(t, MP_CLEAR)
144 inline void SetClearCounter(Tile t, uint c)
146 assert(IsTileType(t, MP_CLEAR)); // XXX incomplete
147 SB(t.m5(), 5, 3, c);
152 * Sets ground type and density in one go, also sets the counter to 0
153 * @param t the tile to set the ground type and density for
154 * @param type the new ground type of the tile
155 * @param density the density of the ground tile
156 * @pre IsTileType(t, MP_CLEAR)
158 inline void SetClearGroundDensity(Tile t, ClearGround type, uint density)
160 assert(IsTileType(t, MP_CLEAR)); // XXX incomplete
161 t.m5() = 0 << 5 | type << 2 | density;
166 * Get the field type (production stage) of the field
167 * @param t the field to get the type of
168 * @pre GetClearGround(t) == CLEAR_FIELDS
169 * @return the field type
171 inline uint GetFieldType(Tile t)
173 assert(GetClearGround(t) == CLEAR_FIELDS);
174 return GB(t.m3(), 0, 4);
178 * Set the field type (production stage) of the field
179 * @param t the field to get the type of
180 * @param f the field type
181 * @pre GetClearGround(t) == CLEAR_FIELDS
183 inline void SetFieldType(Tile t, uint f)
185 assert(GetClearGround(t) == CLEAR_FIELDS); // XXX incomplete
186 SB(t.m3(), 0, 4, f);
190 * Get the industry (farm) that made the field
191 * @param t the field to get creating industry of
192 * @pre GetClearGround(t) == CLEAR_FIELDS
193 * @return the industry that made the field
195 inline IndustryID GetIndustryIndexOfField(Tile t)
197 assert(GetClearGround(t) == CLEAR_FIELDS);
198 return(IndustryID) t.m2();
202 * Set the industry (farm) that made the field
203 * @param t the field to get creating industry of
204 * @param i the industry that made the field
205 * @pre GetClearGround(t) == CLEAR_FIELDS
207 inline void SetIndustryIndexOfField(Tile t, IndustryID i)
209 assert(GetClearGround(t) == CLEAR_FIELDS);
210 t.m2() = i;
215 * Is there a fence at the given border?
216 * @param t the tile to check for fences
217 * @param side the border to check
218 * @pre IsClearGround(t, CLEAR_FIELDS)
219 * @return 0 if there is no fence, otherwise the fence type
221 inline uint GetFence(Tile t, DiagDirection side)
223 assert(IsClearGround(t, CLEAR_FIELDS));
224 switch (side) {
225 default: NOT_REACHED();
226 case DIAGDIR_SE: return GB(t.m4(), 2, 3);
227 case DIAGDIR_SW: return GB(t.m4(), 5, 3);
228 case DIAGDIR_NE: return GB(t.m3(), 5, 3);
229 case DIAGDIR_NW: return GB(t.m6(), 2, 3);
234 * Sets the type of fence (and whether there is one) for the given border.
235 * @param t the tile to check for fences
236 * @param side the border to check
237 * @param h 0 if there is no fence, otherwise the fence type
238 * @pre IsClearGround(t, CLEAR_FIELDS)
240 inline void SetFence(Tile t, DiagDirection side, uint h)
242 assert(IsClearGround(t, CLEAR_FIELDS));
243 switch (side) {
244 default: NOT_REACHED();
245 case DIAGDIR_SE: SB(t.m4(), 2, 3, h); break;
246 case DIAGDIR_SW: SB(t.m4(), 5, 3, h); break;
247 case DIAGDIR_NE: SB(t.m3(), 5, 3, h); break;
248 case DIAGDIR_NW: SB(t.m6(), 2, 3, h); break;
254 * Make a clear tile.
255 * @param t the tile to make a clear tile
256 * @param g the type of ground
257 * @param density the density of the grass/snow/desert etc
259 inline void MakeClear(Tile t, ClearGround g, uint density)
261 SetTileType(t, MP_CLEAR);
262 t.m1() = 0;
263 SetTileOwner(t, OWNER_NONE);
264 t.m2() = 0;
265 t.m3() = 0;
266 t.m4() = 0 << 5 | 0 << 2;
267 SetClearGroundDensity(t, g, density); // Sets m5
268 t.m6() = 0;
269 t.m7() = 0;
270 t.m8() = 0;
275 * Make a (farm) field tile.
276 * @param t the tile to make a farm field
277 * @param field_type the 'growth' level of the field
278 * @param industry the industry this tile belongs to
280 inline void MakeField(Tile t, uint field_type, IndustryID industry)
282 SetTileType(t, MP_CLEAR);
283 t.m1() = 0;
284 SetTileOwner(t, OWNER_NONE);
285 t.m2() = industry;
286 t.m3() = field_type;
287 t.m4() = 0 << 5 | 0 << 2;
288 SetClearGroundDensity(t, CLEAR_FIELDS, 3);
289 SB(t.m6(), 2, 4, 0);
290 t.m7() = 0;
291 t.m8() = 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 inline void MakeSnow(Tile t, uint density = 0)
302 assert(GetClearGround(t) != CLEAR_SNOW);
303 SetBit(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 inline void ClearSnow(Tile t)
318 assert(GetClearGround(t) == CLEAR_SNOW);
319 ClrBit(t.m3(), 4);
320 SetClearDensity(t, 3);
323 #endif /* CLEAR_MAP_H */