1 /* $Id: tile_map.h 25849 2013-10-12 22:07:58Z zuu $ */
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 tile_map.h Map writing/reading functions for tiles. */
15 #include "slope_type.h"
17 #include "core/bitmath_func.hpp"
18 #include "settings_type.h"
21 * Returns the height of a tile
23 * This function returns the height of the northern corner of a tile.
24 * This is saved in the global map-array. It does not take affect by
25 * any slope-data of the tile.
27 * @param tile The tile to get the height from
28 * @return the height of the tile
29 * @pre tile < MapSize()
31 static inline uint
TileHeight(TileIndex tile
)
33 assert(tile
< MapSize());
34 return _m
[tile
].height
;
37 uint
TileHeightOutsideMap(int x
, int y
);
40 * Sets the height of a tile.
42 * This function sets the height of the northern corner of a tile.
44 * @param tile The tile to change the height
45 * @param height The new height value of the tile
46 * @pre tile < MapSize()
47 * @pre heigth <= MAX_TILE_HEIGHT
49 static inline void SetTileHeight(TileIndex tile
, uint height
)
51 assert(tile
< MapSize());
52 assert(height
<= MAX_TILE_HEIGHT
);
53 _m
[tile
].height
= height
;
57 * Returns the height of a tile in pixels.
59 * This function returns the height of the northern corner of a tile in pixels.
61 * @param tile The tile to get the height
62 * @return The height of the tile in pixel
64 static inline uint
TilePixelHeight(TileIndex tile
)
66 return TileHeight(tile
) * TILE_HEIGHT
;
70 * Returns the tile height for a coordinate outside map. Such a height is
71 * needed for painting the area outside map using completely black tiles.
72 * The idea is descending to heightlevel 0 as fast as possible.
73 * @param x The X-coordinate (same unit as TileX).
74 * @param y The Y-coordinate (same unit as TileY).
75 * @return The height in pixels in the same unit as TilePixelHeight.
77 static inline uint
TilePixelHeightOutsideMap(int x
, int y
)
79 return TileHeightOutsideMap(x
, y
) * TILE_HEIGHT
;
83 * Get the tiletype of a given tile.
85 * @param tile The tile to get the TileType
86 * @return The tiletype of the tile
87 * @pre tile < MapSize()
89 static inline TileType
GetTileType(TileIndex tile
)
91 assert(tile
< MapSize());
92 return (TileType
)GB(_m
[tile
].type
, 4, 4);
96 * Check if a tile is within the map (not a border)
98 * @param tile The tile to check
99 * @return Whether the tile is in the interior of the map
100 * @pre tile < MapSize()
102 static inline bool IsInnerTile(TileIndex tile
)
104 assert(tile
< MapSize());
106 uint x
= TileX(tile
);
107 uint y
= TileY(tile
);
109 return x
< MapMaxX() && y
< MapMaxY() && ((x
> 0 && y
> 0) || !_settings_game
.construction
.freeform_edges
);
113 * Set the type of a tile
115 * This functions sets the type of a tile. If the type
116 * MP_VOID is selected the tile must be at the south-west or
117 * south-east edges of the map and vice versa.
119 * @param tile The tile to save the new type
120 * @param type The type to save
121 * @pre tile < MapSize()
122 * @pre type MP_VOID <=> tile is on the south-east or south-west edge.
124 static inline void SetTileType(TileIndex tile
, TileType type
)
126 assert(tile
< MapSize());
127 /* VOID tiles (and no others) are exactly allowed at the lower left and right
128 * edges of the map. If _settings_game.construction.freeform_edges is true,
129 * the upper edges of the map are also VOID tiles. */
130 assert(IsInnerTile(tile
) == (type
!= MP_VOID
));
131 SB(_m
[tile
].type
, 4, 4, type
);
135 * Checks if a tile is a give tiletype.
137 * This function checks if a tile got the given tiletype.
139 * @param tile The tile to check
140 * @param type The type to check against
141 * @return true If the type matches against the type of the tile
143 static inline bool IsTileType(TileIndex tile
, TileType type
)
145 return GetTileType(tile
) == type
;
149 * Checks if a tile is valid
151 * @param tile The tile to check
152 * @return True if the tile is on the map and not one of MP_VOID.
154 static inline bool IsValidTile(TileIndex tile
)
156 return tile
< MapSize() && !IsTileType(tile
, MP_VOID
);
160 * Returns the owner of a tile
162 * This function returns the owner of a tile. This cannot used
163 * for tiles which type is one of MP_HOUSE, MP_VOID and MP_INDUSTRY
164 * as no company owned any of these buildings.
166 * @param tile The tile to check
167 * @return The owner of the tile
168 * @pre IsValidTile(tile)
169 * @pre The type of the tile must not be MP_HOUSE and MP_INDUSTRY
171 static inline Owner
GetTileOwner(TileIndex tile
)
173 assert(IsValidTile(tile
));
174 assert(!IsTileType(tile
, MP_HOUSE
));
175 assert(!IsTileType(tile
, MP_INDUSTRY
));
177 return (Owner
)GB(_m
[tile
].m1
, 0, 5);
181 * Sets the owner of a tile
183 * This function sets the owner status of a tile. Note that you cannot
184 * set a owner for tiles of type MP_HOUSE, MP_VOID and MP_INDUSTRY.
186 * @param tile The tile to change the owner status.
187 * @param owner The new owner.
188 * @pre IsValidTile(tile)
189 * @pre The type of the tile must not be MP_HOUSE and MP_INDUSTRY
191 static inline void SetTileOwner(TileIndex tile
, Owner owner
)
193 assert(IsValidTile(tile
));
194 assert(!IsTileType(tile
, MP_HOUSE
));
195 assert(!IsTileType(tile
, MP_INDUSTRY
));
197 SB(_m
[tile
].m1
, 0, 5, owner
);
201 * Checks if a tile belongs to the given owner
203 * @param tile The tile to check
204 * @param owner The owner to check against
205 * @return True if a tile belongs the the given owner
207 static inline bool IsTileOwner(TileIndex tile
, Owner owner
)
209 return GetTileOwner(tile
) == owner
;
213 * Set the tropic zone
214 * @param tile the tile to set the zone of
215 * @param type the new type
216 * @pre tile < MapSize()
218 static inline void SetTropicZone(TileIndex tile
, TropicZone type
)
220 assert(tile
< MapSize());
221 assert(!IsTileType(tile
, MP_VOID
) || type
== TROPICZONE_NORMAL
);
222 SB(_m
[tile
].type
, 0, 2, type
);
226 * Get the tropic zone
227 * @param tile the tile to get the zone of
228 * @pre tile < MapSize()
229 * @return the zone type
231 static inline TropicZone
GetTropicZone(TileIndex tile
)
233 assert(tile
< MapSize());
234 return (TropicZone
)GB(_m
[tile
].type
, 0, 2);
238 * Get the current animation frame
240 * @pre IsTileType(t, MP_HOUSE) || IsTileType(t, MP_OBJECT) || IsTileType(t, MP_INDUSTRY) ||IsTileType(t, MP_STATION)
241 * @return frame number
243 static inline byte
GetAnimationFrame(TileIndex t
)
245 assert(IsTileType(t
, MP_HOUSE
) || IsTileType(t
, MP_OBJECT
) || IsTileType(t
, MP_INDUSTRY
) ||IsTileType(t
, MP_STATION
));
250 * Set a new animation frame
252 * @param frame the new frame number
253 * @pre IsTileType(t, MP_HOUSE) || IsTileType(t, MP_OBJECT) || IsTileType(t, MP_INDUSTRY) ||IsTileType(t, MP_STATION)
255 static inline void SetAnimationFrame(TileIndex t
, byte frame
)
257 assert(IsTileType(t
, MP_HOUSE
) || IsTileType(t
, MP_OBJECT
) || IsTileType(t
, MP_INDUSTRY
) ||IsTileType(t
, MP_STATION
));
261 Slope
GetTileSlope(TileIndex tile
, int *h
= nullptr);
262 int GetTileZ(TileIndex tile
);
263 int GetTileMaxZ(TileIndex tile
);
265 bool IsTileFlat(TileIndex tile
, int *h
= nullptr);
268 * Return the slope of a given tile
269 * @param tile Tile to compute slope of
270 * @param h If not \c nullptr, pointer to storage of z height
271 * @return Slope of the tile, except for the HALFTILE part
273 static inline Slope
GetTilePixelSlope(TileIndex tile
, int *h
)
275 Slope s
= GetTileSlope(tile
, h
);
276 if (h
!= nullptr) *h
*= TILE_HEIGHT
;
280 Slope
GetTilePixelSlopeOutsideMap(int x
, int y
, int *h
);
283 * Get bottom height of the tile
284 * @param tile Tile to compute height of
285 * @return Minimum height of the tile
287 static inline int GetTilePixelZ(TileIndex tile
)
289 return GetTileZ(tile
) * TILE_HEIGHT
;
292 int GetTilePixelZOutsideMap(int x
, int y
);
295 * Get top height of the tile
296 * @param t Tile to compute height of
297 * @return Maximum height of the tile
299 static inline int GetTileMaxPixelZ(TileIndex tile
)
301 return GetTileMaxZ(tile
) * TILE_HEIGHT
;
304 int GetTileMaxPixelZOutsideMap(int x
, int y
);
308 * Calculate a hash value from a tile position
310 * @param x The X coordinate
311 * @param y The Y coordinate
312 * @return The hash of the tile
314 static inline uint
TileHash(uint x
, uint y
)
324 * Get the last two bits of the TileHash
325 * from a tile position.
328 * @param x The X coordinate
329 * @param y The Y coordinate
330 * @return The last two bits from hash of the tile
332 static inline uint
TileHash2Bit(uint x
, uint y
)
334 return GB(TileHash(x
, y
), 0, 2);
337 #endif /* TILE_MAP_H */