map_func.h
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
00049 void AllocateMap(uint size_x, uint size_y);
00050
00056 static inline uint MapLogX()
00057 {
00058 extern uint _map_log_x;
00059 return _map_log_x;
00060 }
00061
00067 static inline uint MapLogY()
00068 {
00069 extern uint _map_log_y;
00070 return _map_log_y;
00071 }
00072
00077 static inline uint MapSizeX()
00078 {
00079 extern uint _map_size_x;
00080 return _map_size_x;
00081 }
00082
00087 static inline uint MapSizeY()
00088 {
00089 extern uint _map_size_y;
00090 return _map_size_y;
00091 }
00092
00097 static inline uint MapSize()
00098 {
00099 extern uint _map_size;
00100 return _map_size;
00101 }
00102
00107 static inline uint MapMaxX()
00108 {
00109 return MapSizeX() - 1;
00110 }
00111
00116 static inline uint MapMaxY()
00117 {
00118 return MapSizeY() - 1;
00119 }
00120
00127 static inline uint ScaleByMapSize(uint n)
00128 {
00129
00130
00131 return CeilDiv(n << (MapLogX() + MapLogY() - 12), 1 << 4);
00132 }
00133
00134
00141 static inline uint ScaleByMapSize1D(uint n)
00142 {
00143
00144
00145
00146 return CeilDiv((n << MapLogX()) + (n << MapLogY()), 1 << 9);
00147 }
00148
00159 typedef int32 TileIndexDiff;
00160
00168 static inline TileIndex TileXY(uint x, uint y)
00169 {
00170 return (y << MapLogX()) + x;
00171 }
00172
00184 static inline TileIndexDiff TileDiffXY(int x, int y)
00185 {
00186
00187
00188
00189
00190 return (y * MapSizeX()) + x;
00191 }
00192
00193 static inline TileIndex TileVirtXY(uint x, uint y)
00194 {
00195 return (y >> 4 << MapLogX()) + (x >> 4);
00196 }
00197
00198
00204 static inline uint TileX(TileIndex tile)
00205 {
00206 return tile & MapMaxX();
00207 }
00208
00214 static inline uint TileY(TileIndex tile)
00215 {
00216 return tile >> MapLogX();
00217 }
00218
00229 static inline TileIndexDiff ToTileIndexDiff(TileIndexDiffC tidc)
00230 {
00231 return (tidc.y << MapLogX()) + tidc.x;
00232 }
00233
00234
00235 #ifndef _DEBUG
00236
00243 #define TILE_ADD(x, y) ((x) + (y))
00244 #else
00245 extern TileIndex TileAdd(TileIndex tile, TileIndexDiff add,
00246 const char *exp, const char *file, int line);
00247 #define TILE_ADD(x, y) (TileAdd((x), (y), #x " + " #y, __FILE__, __LINE__))
00248 #endif
00249
00257 #define TILE_ADDXY(tile, x, y) TILE_ADD(tile, TileDiffXY(x, y))
00258
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
00409 bool CircularTileSearch(TileIndex *tile, uint size, TestTileOnSearchProc proc, void *user_data);
00410
00414 bool CircularTileSearch(TileIndex *tile, uint radius, uint w, uint h, TestTileOnSearchProc proc, void *user_data);
00415
00421 static inline TileIndex RandomTileSeed(uint32 r)
00422 {
00423 return TILE_MASK(r);
00424 }
00425
00432 #define RandomTile() RandomTileSeed(Random())
00433
00437 uint GetClosestWaterDistance(TileIndex tile, bool water);
00438
00439 #endif