# tile_map.h

Go to the documentation of this file.

    00001 /* $Id: tile_map.h 25849 2013-10-12 22:07:58Z zuu $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #ifndef TILE_MAP_H
    00013 #define TILE_MAP_H
    00014
    00015 #include "slope_type.h"
    00016 #include "map_func.h"
    00017 #include "core/bitmath_func.hpp"
    00018 #include "settings_type.h"
    00019
    00031 static inline uint TileHeight(TileIndex tile)
    00032 {
    00033   assert(tile < MapSize());
    00034   return GB(_m[tile].type_height, 0, 4);
    00035 }
    00036
    00047 static inline void SetTileHeight(TileIndex tile, uint height)
    00048 {
    00049   assert(tile < MapSize());
    00050   assert(height <= MAX_TILE_HEIGHT);
    00051   SB(_m[tile].type_height, 0, 4, height);
    00052 }
    00053
    00062 static inline uint TilePixelHeight(TileIndex tile)
    00063 {
    00064   return TileHeight(tile) * TILE_HEIGHT;
    00065 }
    00066
    00074 static inline TileType GetTileType(TileIndex tile)
    00075 {
    00076   assert(tile < MapSize());
    00077   return (TileType)GB(_m[tile].type_height, 4, 4);
    00078 }
    00079
    00087 static inline bool IsInnerTile(TileIndex tile)
    00088 {
    00089   assert(tile < MapSize());
    00090
    00091   uint x = TileX(tile);
    00092   uint y = TileY(tile);
    00093
    00094   return x < MapMaxX() && y < MapMaxY() && ((x > 0 && y > 0) || !_settings_game.construction.freeform_edges);
    00095 }
    00096
    00109 static inline void SetTileType(TileIndex tile, TileType type)
    00110 {
    00111   assert(tile < MapSize());
    00112   /* VOID tiles (and no others) are exactly allowed at the lower left and right
    00113    * edges of the map. If _settings_game.construction.freeform_edges is true,
    00114    * the upper edges of the map are also VOID tiles. */
    00115   assert(IsInnerTile(tile) == (type != MP_VOID));
    00116   SB(_m[tile].type_height, 4, 4, type);
    00117 }
    00118
    00128 static inline bool IsTileType(TileIndex tile, TileType type)
    00129 {
    00130   return GetTileType(tile) == type;
    00131 }
    00132
    00139 static inline bool IsValidTile(TileIndex tile)
    00140 {
    00141   return tile < MapSize() && !IsTileType(tile, MP_VOID);
    00142 }
    00143
    00156 static inline Owner GetTileOwner(TileIndex tile)
    00157 {
    00158   assert(IsValidTile(tile));
    00159   assert(!IsTileType(tile, MP_HOUSE));
    00160   assert(!IsTileType(tile, MP_INDUSTRY));
    00161
    00162   return (Owner)GB(_m[tile].m1, 0, 5);
    00163 }
    00164
    00176 static inline void SetTileOwner(TileIndex tile, Owner owner)
    00177 {
    00178   assert(IsValidTile(tile));
    00179   assert(!IsTileType(tile, MP_HOUSE));
    00180   assert(!IsTileType(tile, MP_INDUSTRY));
    00181
    00182   SB(_m[tile].m1, 0, 5, owner);
    00183 }
    00184
    00192 static inline bool IsTileOwner(TileIndex tile, Owner owner)
    00193 {
    00194   return GetTileOwner(tile) == owner;
    00195 }
    00196
    00203 static inline void SetTropicZone(TileIndex tile, TropicZone type)
    00204 {
    00205   assert(tile < MapSize());
    00206   assert(!IsTileType(tile, MP_VOID) || type == TROPICZONE_NORMAL);
    00207   SB(_m[tile].m6, 0, 2, type);
    00208 }
    00209
    00216 static inline TropicZone GetTropicZone(TileIndex tile)
    00217 {
    00218   assert(tile < MapSize());
    00219   return (TropicZone)GB(_m[tile].m6, 0, 2);
    00220 }
    00221
    00228 static inline byte GetAnimationFrame(TileIndex t)
    00229 {
    00230   assert(IsTileType(t, MP_HOUSE) || IsTileType(t, MP_OBJECT) || IsTileType(t, MP_INDUSTRY) ||IsTileType(t, MP_STATION));
    00231   return _me[t].m7;
    00232 }
    00233
    00240 static inline void SetAnimationFrame(TileIndex t, byte frame)
    00241 {
    00242   assert(IsTileType(t, MP_HOUSE) || IsTileType(t, MP_OBJECT) || IsTileType(t, MP_INDUSTRY) ||IsTileType(t, MP_STATION));
    00243   _me[t].m7 = frame;
    00244 }
    00245
    00246 Slope GetTileSlope(TileIndex tile, int *h = NULL);
    00247 int GetTileZ(TileIndex tile);
    00248 int GetTileMaxZ(TileIndex tile);
    00249
    00250 bool IsTileFlat(TileIndex tile, int *h = NULL);
    00251
    00258 static inline Slope GetTilePixelSlope(TileIndex tile, int *h)
    00259 {
    00260   Slope s = GetTileSlope(tile, h);
    00261   if (h != NULL) *h *= TILE_HEIGHT;
    00262   return s;
    00263 }
    00264
    00270 static inline int GetTilePixelZ(TileIndex tile)
    00271 {
    00272   return GetTileZ(tile) * TILE_HEIGHT;
    00273 }
    00274
    00280 static inline int GetTileMaxPixelZ(TileIndex tile)
    00281 {
    00282   return GetTileMaxZ(tile) * TILE_HEIGHT;
    00283 }
    00284
    00285
    00293 static inline uint TileHash(uint x, uint y)
    00294 {
    00295   uint hash = x >> 4;
    00296   hash ^= x >> 6;
    00297   hash ^= y >> 4;
    00298   hash -= y >> 6;
    00299   return hash;
    00300 }
    00301
    00311 static inline uint TileHash2Bit(uint x, uint y)
    00312 {
    00313   return GB(TileHash(x, y), 0, 2);
    00314 }
    00315
    00316 #endif /* TILE_MAP_H */