map_func.h

Go to the documentation of this file.
00001 /* $Id: map_func.h 21845 2011-01-18 22:31:06Z rubidium $ */
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 
00190 static inline TileIndex TileVirtXY(uint x, uint y)
00191 {
00192   return (y >> 4 << MapLogX()) + (x >> 4);
00193 }
00194 
00195 
00201 static inline uint TileX(TileIndex tile)
00202 {
00203   return tile & MapMaxX();
00204 }
00205 
00211 static inline uint TileY(TileIndex tile)
00212 {
00213   return tile >> MapLogX();
00214 }
00215 
00226 static inline TileIndexDiff ToTileIndexDiff(TileIndexDiffC tidc)
00227 {
00228   return (tidc.y << MapLogX()) + tidc.x;
00229 }
00230 
00231 
00232 #ifndef _DEBUG
00233 
00240   #define TILE_ADD(x, y) ((x) + (y))
00241 #else
00242   extern TileIndex TileAdd(TileIndex tile, TileIndexDiff add,
00243     const char *exp, const char *file, int line);
00244   #define TILE_ADD(x, y) (TileAdd((x), (y), #x " + " #y, __FILE__, __LINE__))
00245 #endif
00246 
00254 #define TILE_ADDXY(tile, x, y) TILE_ADD(tile, TileDiffXY(x, y))
00255 
00256 TileIndex TileAddWrap(TileIndex tile, int addx, int addy);
00257 
00264 static inline TileIndexDiffC TileIndexDiffCByDiagDir(DiagDirection dir)
00265 {
00266   extern const TileIndexDiffC _tileoffs_by_diagdir[DIAGDIR_END];
00267 
00268   assert(IsValidDiagDirection(dir));
00269   return _tileoffs_by_diagdir[dir];
00270 }
00271 
00278 static inline TileIndexDiffC TileIndexDiffCByDir(Direction dir)
00279 {
00280   extern const TileIndexDiffC _tileoffs_by_dir[DIR_END];
00281 
00282   assert(IsValidDirection(dir));
00283   return _tileoffs_by_dir[dir];
00284 }
00285 
00296 static inline TileIndex AddTileIndexDiffCWrap(TileIndex tile, TileIndexDiffC diff)
00297 {
00298   int x = TileX(tile) + diff.x;
00299   int y = TileY(tile) + diff.y;
00300   /* Negative value will become big positive value after cast */
00301   if ((uint)x >= MapSizeX() || (uint)y >= MapSizeY()) return INVALID_TILE;
00302   return TileXY(x, y);
00303 }
00304 
00312 static inline TileIndexDiffC TileIndexToTileIndexDiffC(TileIndex tile_a, TileIndex tile_b)
00313 {
00314   TileIndexDiffC difference;
00315 
00316   difference.x = TileX(tile_a) - TileX(tile_b);
00317   difference.y = TileY(tile_a) - TileY(tile_b);
00318 
00319   return difference;
00320 }
00321 
00322 /* Functions to calculate distances */
00323 uint DistanceManhattan(TileIndex, TileIndex); 
00324 uint DistanceSquare(TileIndex, TileIndex); 
00325 uint DistanceMax(TileIndex, TileIndex); 
00326 uint DistanceMaxPlusManhattan(TileIndex, TileIndex); 
00327 uint DistanceFromEdge(TileIndex); 
00328 uint DistanceFromEdgeDir(TileIndex, DiagDirection); 
00329 
00337 static inline TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
00338 {
00339   extern const TileIndexDiffC _tileoffs_by_diagdir[DIAGDIR_END];
00340 
00341   assert(IsValidDiagDirection(dir));
00342   return ToTileIndexDiff(_tileoffs_by_diagdir[dir]);
00343 }
00344 
00351 static inline TileIndexDiff TileOffsByDir(Direction dir)
00352 {
00353   extern const TileIndexDiffC _tileoffs_by_dir[DIR_END];
00354 
00355   assert(IsValidDirection(dir));
00356   return ToTileIndexDiff(_tileoffs_by_dir[dir]);
00357 }
00358 
00366 static inline TileIndex TileAddByDiagDir(TileIndex tile, DiagDirection dir)
00367 {
00368   return TILE_ADD(tile, TileOffsByDiagDir(dir));
00369 }
00370 
00378 static inline DiagDirection DiagdirBetweenTiles(TileIndex tile_from, TileIndex tile_to)
00379 {
00380   int dx = (int)TileX(tile_to) - (int)TileX(tile_from);
00381   int dy = (int)TileY(tile_to) - (int)TileY(tile_from);
00382   if (dx == 0) {
00383     if (dy == 0) return INVALID_DIAGDIR;
00384     return (dy < 0 ? DIAGDIR_NW : DIAGDIR_SE);
00385   } else {
00386     if (dy != 0) return INVALID_DIAGDIR;
00387     return (dx < 0 ? DIAGDIR_NE : DIAGDIR_SW);
00388   }
00389 }
00390 
00398 typedef bool TestTileOnSearchProc(TileIndex tile, void *user_data);
00399 
00400 bool CircularTileSearch(TileIndex *tile, uint size, TestTileOnSearchProc proc, void *user_data);
00401 bool CircularTileSearch(TileIndex *tile, uint radius, uint w, uint h, TestTileOnSearchProc proc, void *user_data);
00402 
00408 static inline TileIndex RandomTileSeed(uint32 r)
00409 {
00410   return TILE_MASK(r);
00411 }
00412 
00419 #define RandomTile() RandomTileSeed(Random())
00420 
00421 uint GetClosestWaterDistance(TileIndex tile, bool water);
00422 
00423 #endif /* MAP_FUNC_H */

Generated on Fri Mar 18 23:17:36 2011 for OpenTTD by  doxygen 1.6.1