Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
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
00127
00128 return CeilDiv(n << (MapLogX() + MapLogY() - 12), 1 << 4);
00129 }
00130
00131
00138 static inline uint ScaleByMapSize1D(uint n)
00139 {
00140
00141
00142
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
00184
00185
00186
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
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
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