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 clear_map.h Map accessors for 'clear' tiles */
9
10 #ifndef CLEAR_MAP_H
11 #define CLEAR_MAP_H
12
13 #include "bridge_map.h"
14 #include "industry_type.h"
15
16 /**
17 * Ground types. Valid densities in comments after the enum.
18 */
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
26 };
27
28
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.
34 */
IsSnowTile(TileIndex t)35 static inline bool IsSnowTile(TileIndex t)
36 {
37 assert(IsTileType(t, MP_CLEAR));
38 return HasBit(_m[t].m3, 4);
39 }
40
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
46 */
GetRawClearGround(TileIndex t)47 static inline ClearGround GetRawClearGround(TileIndex t)
48 {
49 assert(IsTileType(t, MP_CLEAR));
50 return (ClearGround)GB(_m[t].m5, 2, 3);
51 }
52
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
58 */
GetClearGround(TileIndex t)59 static inline ClearGround GetClearGround(TileIndex t)
60 {
61 if (IsSnowTile(t)) return CLEAR_SNOW;
62 return GetRawClearGround(t);
63 }
64
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)
70 */
IsClearGround(TileIndex t,ClearGround ct)71 static inline bool IsClearGround(TileIndex t, ClearGround ct)
72 {
73 return GetClearGround(t) == ct;
74 }
75
76
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
82 */
GetClearDensity(TileIndex t)83 static inline uint GetClearDensity(TileIndex t)
84 {
85 assert(IsTileType(t, MP_CLEAR));
86 return GB(_m[t].m5, 0, 2);
87 }
88
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)
94 */
AddClearDensity(TileIndex t,int d)95 static inline void AddClearDensity(TileIndex t, int d)
96 {
97 assert(IsTileType(t, MP_CLEAR)); // XXX incomplete
98 _m[t].m5 += d;
99 }
100
101 /**
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)
106 */
SetClearDensity(TileIndex t,uint d)107 static inline void SetClearDensity(TileIndex t, uint d)
108 {
109 assert(IsTileType(t, MP_CLEAR));
110 SB(_m[t].m5, 0, 2, d);
111 }
112
113
114 /**
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
119 */
GetClearCounter(TileIndex t)120 static inline uint GetClearCounter(TileIndex t)
121 {
122 assert(IsTileType(t, MP_CLEAR));
123 return GB(_m[t].m5, 5, 3);
124 }
125
126 /**
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)
131 */
AddClearCounter(TileIndex t,int c)132 static inline void AddClearCounter(TileIndex t, int c)
133 {
134 assert(IsTileType(t, MP_CLEAR)); // XXX incomplete
135 _m[t].m5 += c << 5;
136 }
137
138 /**
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)
143 */
SetClearCounter(TileIndex t,uint c)144 static inline void SetClearCounter(TileIndex t, uint c)
145 {
146 assert(IsTileType(t, MP_CLEAR)); // XXX incomplete
147 SB(_m[t].m5, 5, 3, c);
148 }
149
150
151 /**
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)
157 */
SetClearGroundDensity(TileIndex t,ClearGround type,uint density)158 static inline void SetClearGroundDensity(TileIndex t, ClearGround type, uint density)
159 {
160 assert(IsTileType(t, MP_CLEAR)); // XXX incomplete
161 _m[t].m5 = 0 << 5 | type << 2 | density;
162 }
163
164
165 /**
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
170 */
GetFieldType(TileIndex t)171 static inline uint GetFieldType(TileIndex t)
172 {
173 assert(GetClearGround(t) == CLEAR_FIELDS);
174 return GB(_m[t].m3, 0, 4);
175 }
176
177 /**
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
182 */
SetFieldType(TileIndex t,uint f)183 static inline void SetFieldType(TileIndex t, uint f)
184 {
185 assert(GetClearGround(t) == CLEAR_FIELDS); // XXX incomplete
186 SB(_m[t].m3, 0, 4, f);
187 }
188
189 /**
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
194 */
GetIndustryIndexOfField(TileIndex t)195 static inline IndustryID GetIndustryIndexOfField(TileIndex t)
196 {
197 assert(GetClearGround(t) == CLEAR_FIELDS);
198 return(IndustryID) _m[t].m2;
199 }
200
201 /**
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
206 */
SetIndustryIndexOfField(TileIndex t,IndustryID i)207 static inline void SetIndustryIndexOfField(TileIndex t, IndustryID i)
208 {
209 assert(GetClearGround(t) == CLEAR_FIELDS);
210 _m[t].m2 = i;
211 }
212
213
214 /**
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
220 */
GetFence(TileIndex t,DiagDirection side)221 static inline uint GetFence(TileIndex t, DiagDirection side)
222 {
223 assert(IsClearGround(t, CLEAR_FIELDS));
224 switch (side) {
225 default: NOT_REACHED();
226 case DIAGDIR_SE: return GB(_m[t].m4, 2, 3);
227 case DIAGDIR_SW: return GB(_m[t].m4, 5, 3);
228 case DIAGDIR_NE: return GB(_m[t].m3, 5, 3);
229 case DIAGDIR_NW: return GB(_me[t].m6, 2, 3);
230 }
231 }
232
233 /**
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)
239 */
SetFence(TileIndex t,DiagDirection side,uint h)240 static inline void SetFence(TileIndex t, DiagDirection side, uint h)
241 {
242 assert(IsClearGround(t, CLEAR_FIELDS));
243 switch (side) {
244 default: NOT_REACHED();
245 case DIAGDIR_SE: SB(_m[t].m4, 2, 3, h); break;
246 case DIAGDIR_SW: SB(_m[t].m4, 5, 3, h); break;
247 case DIAGDIR_NE: SB(_m[t].m3, 5, 3, h); break;
248 case DIAGDIR_NW: SB(_me[t].m6, 2, 3, h); break;
249 }
250 }
251
252
253 /**
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
258 */
MakeClear(TileIndex t,ClearGround g,uint density)259 static inline void MakeClear(TileIndex t, ClearGround g, uint density)
260 {
261 SetTileType(t, MP_CLEAR);
262 _m[t].m1 = 0;
263 SetTileOwner(t, OWNER_NONE);
264 _m[t].m2 = 0;
265 _m[t].m3 = 0;
266 _m[t].m4 = 0 << 5 | 0 << 2;
267 SetClearGroundDensity(t, g, density); // Sets m5
268 _me[t].m6 = 0;
269 _me[t].m7 = 0;
270 _me[t].m8 = 0;
271 }
272
273
274 /**
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
279 */
MakeField(TileIndex t,uint field_type,IndustryID industry)280 static inline void MakeField(TileIndex t, uint field_type, IndustryID industry)
281 {
282 SetTileType(t, MP_CLEAR);
283 _m[t].m1 = 0;
284 SetTileOwner(t, OWNER_NONE);
285 _m[t].m2 = industry;
286 _m[t].m3 = field_type;
287 _m[t].m4 = 0 << 5 | 0 << 2;
288 SetClearGroundDensity(t, CLEAR_FIELDS, 3);
289 SB(_me[t].m6, 2, 4, 0);
290 _me[t].m7 = 0;
291 _me[t].m8 = 0;
292 }
293
294 /**
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
299 */
300 static inline void MakeSnow(TileIndex t, uint density = 0)
301 {
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);
308 }
309 }
310
311 /**
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
315 */
ClearSnow(TileIndex t)316 static inline void ClearSnow(TileIndex t)
317 {
318 assert(GetClearGround(t) == CLEAR_SNOW);
319 ClrBit(_m[t].m3, 4);
320 SetClearDensity(t, 3);
321 }
322
323 #endif /* CLEAR_MAP_H */
324