# map_func.h

Go to the documentation of this file.

    00001 /* $Id: map_func.h 24900 2013-01-08 22:46:42Z planetmaker $ */
    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 MAP_FUNC_H
    00013 #define MAP_FUNC_H
    00014
    00015 #include "core/math_func.hpp"
    00016 #include "tile_type.h"
    00017 #include "map_type.h"
    00018 #include "direction_func.h"
    00019
    00020 extern uint _map_tile_mask;
    00021
    00028 #define TILE_MASK(x) ((x) & _map_tile_mask)
    00029
    00036 extern Tile *_m;
    00037
    00044 extern TileExtended *_me;
    00045
    00046 void AllocateMap(uint size_x, uint size_y);
    00047
    00053 static inline uint MapLogX()
    00054 {
    00055   extern uint _map_log_x;
    00056   return _map_log_x;
    00057 }
    00058
    00064 static inline uint MapLogY()
    00065 {
    00066   extern uint _map_log_y;
    00067   return _map_log_y;
    00068 }
    00069
    00074 static inline uint MapSizeX()
    00075 {
    00076   extern uint _map_size_x;
    00077   return _map_size_x;
    00078 }
    00079
    00084 static inline uint MapSizeY()
    00085 {
    00086   extern uint _map_size_y;
    00087   return _map_size_y;
    00088 }
    00089
    00094 static inline uint MapSize()
    00095 {
    00096   extern uint _map_size;
    00097   return _map_size;
    00098 }
    00099
    00104 static inline uint MapMaxX()
    00105 {
    00106   return MapSizeX() - 1;
    00107 }
    00108
    00113 static inline uint MapMaxY()
    00114 {
    00115   return MapSizeY() - 1;
    00116 }
    00117
    00124 static inline uint ScaleByMapSize(uint n)
    00125 {
    00126   /* Subtract 12 from shift in order to prevent integer overflow
    00127    * for large values of n. It's safe since the min mapsize is 64x64. */
    00128   return CeilDiv(n << (MapLogX() + MapLogY() - 12), 1 << 4);
    00129 }
    00130
    00131
    00138 static inline uint ScaleByMapSize1D(uint n)
    00139 {
    00140   /* Normal circumference for the X+Y is 256+256 = 1<<9
    00141    * Note, not actually taking the full circumference into account,
    00142    * just half of it. */
    00143   return CeilDiv((n << MapLogX()) + (n << MapLogY()), 1 << 9);
    00144 }
    00145
    00156 typedef int32 TileIndexDiff;
    00157
    00165 static inline TileIndex TileXY(uint x, uint y)
    00166 {
    00167   return (y << MapLogX()) + x;
    00168 }
    00169
    00181 static inline TileIndexDiff TileDiffXY(int x, int y)
    00182 {
    00183   /* Multiplication gives much better optimization on MSVC than shifting.
    00184    * 0 << shift isn't optimized to 0 properly.
    00185    * Typically x and y are constants, and then this doesn't result
    00186    * in any actual multiplication in the assembly code.. */
    00187   return (y * MapSizeX()) + x;
    00188 }
    00189
    00196 static inline TileIndex TileVirtXY(uint x, uint y)
    00197 {
    00198   return (y >> 4 << MapLogX()) + (x >> 4);
    00199 }
    00200
    00201
    00207 static inline uint TileX(TileIndex tile)
    00208 {
    00209   return tile & MapMaxX();
    00210 }
    00211
    00217 static inline uint TileY(TileIndex tile)
    00218 {
    00219   return tile >> MapLogX();
    00220 }
    00221
    00232 static inline TileIndexDiff ToTileIndexDiff(TileIndexDiffC tidc)
    00233 {
    00234   return (tidc.y << MapLogX()) + tidc.x;
    00235 }
    00236
    00237
    00238 #ifndef _DEBUG
    00239
    00246   #define TILE_ADD(x, y) ((x) + (y))
    00247 #else
    00248   extern TileIndex TileAdd(TileIndex tile, TileIndexDiff add,
    00249     const char *exp, const char *file, int line);
    00250   #define TILE_ADD(x, y) (TileAdd((x), (y), #x " + " #y, __FILE__, __LINE__))
    00251 #endif
    00252
    00260 #define TILE_ADDXY(tile, x, y) TILE_ADD(tile, TileDiffXY(x, y))
    00261
    00262 TileIndex TileAddWrap(TileIndex tile, int addx, int addy);
    00263
    00270 static inline TileIndexDiffC TileIndexDiffCByDiagDir(DiagDirection dir)
    00271 {
    00272   extern const TileIndexDiffC _tileoffs_by_diagdir[DIAGDIR_END];
    00273
    00274   assert(IsValidDiagDirection(dir));
    00275   return _tileoffs_by_diagdir[dir];
    00276 }
    00277
    00284 static inline TileIndexDiffC TileIndexDiffCByDir(Direction dir)
    00285 {
    00286   extern const TileIndexDiffC _tileoffs_by_dir[DIR_END];
    00287
    00288   assert(IsValidDirection(dir));
    00289   return _tileoffs_by_dir[dir];
    00290 }
    00291
    00302 static inline TileIndex AddTileIndexDiffCWrap(TileIndex tile, TileIndexDiffC diff)
    00303 {
    00304   int x = TileX(tile) + diff.x;
    00305   int y = TileY(tile) + diff.y;
    00306   /* Negative value will become big positive value after cast */
    00307   if ((uint)x >= MapSizeX() || (uint)y >= MapSizeY()) return INVALID_TILE;
    00308   return TileXY(x, y);
    00309 }
    00310
    00318 static inline TileIndexDiffC TileIndexToTileIndexDiffC(TileIndex tile_a, TileIndex tile_b)
    00319 {
    00320   TileIndexDiffC difference;
    00321
    00322   difference.x = TileX(tile_a) - TileX(tile_b);
    00323   difference.y = TileY(tile_a) - TileY(tile_b);
    00324
    00325   return difference;
    00326 }
    00327
    00328 /* Functions to calculate distances */
    00329 uint DistanceManhattan(TileIndex, TileIndex);
    00330 uint DistanceSquare(TileIndex, TileIndex);
    00331 uint DistanceMax(TileIndex, TileIndex);
    00332 uint DistanceMaxPlusManhattan(TileIndex, TileIndex);
    00333 uint DistanceFromEdge(TileIndex);
    00334 uint DistanceFromEdgeDir(TileIndex, DiagDirection);
    00335
    00343 static inline TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
    00344 {
    00345   extern const TileIndexDiffC _tileoffs_by_diagdir[DIAGDIR_END];
    00346
    00347   assert(IsValidDiagDirection(dir));
    00348   return ToTileIndexDiff(_tileoffs_by_diagdir[dir]);
    00349 }
    00350
    00357 static inline TileIndexDiff TileOffsByDir(Direction dir)
    00358 {
    00359   extern const TileIndexDiffC _tileoffs_by_dir[DIR_END];
    00360
    00361   assert(IsValidDirection(dir));
    00362   return ToTileIndexDiff(_tileoffs_by_dir[dir]);
    00363 }
    00364
    00372 static inline TileIndex TileAddByDiagDir(TileIndex tile, DiagDirection dir)
    00373 {
    00374   return TILE_ADD(tile, TileOffsByDiagDir(dir));
    00375 }
    00376
    00384 static inline DiagDirection DiagdirBetweenTiles(TileIndex tile_from, TileIndex tile_to)
    00385 {
    00386   int dx = (int)TileX(tile_to) - (int)TileX(tile_from);
    00387   int dy = (int)TileY(tile_to) - (int)TileY(tile_from);
    00388   if (dx == 0) {
    00389     if (dy == 0) return INVALID_DIAGDIR;
    00390     return (dy < 0 ? DIAGDIR_NW : DIAGDIR_SE);
    00391   } else {
    00392     if (dy != 0) return INVALID_DIAGDIR;
    00393     return (dx < 0 ? DIAGDIR_NE : DIAGDIR_SW);
    00394   }
    00395 }
    00396
    00404 typedef bool TestTileOnSearchProc(TileIndex tile, void *user_data);
    00405
    00406 bool CircularTileSearch(TileIndex *tile, uint size, TestTileOnSearchProc proc, void *user_data);
    00407 bool CircularTileSearch(TileIndex *tile, uint radius, uint w, uint h, TestTileOnSearchProc proc, void *user_data);
    00408
    00414 static inline TileIndex RandomTileSeed(uint32 r)
    00415 {
    00416   return TILE_MASK(r);
    00417 }
    00418
    00425 #define RandomTile() RandomTileSeed(Random())
    00426
    00427 uint GetClosestWaterDistance(TileIndex tile, bool water);
    00428
    00429 #endif /* MAP_FUNC_H */