tron@679: #ifndef MAP_H tron@679: #define MAP_H tron@679: tron@1210: #include "stdafx.h" tron@1210: tron@927: #define TILE_FROM_XY(x,y) (int)((((y) >> 4) << MapLogX()) + ((x) >> 4)) tron@1174: #define TILE_XY(x,y) (((y) << MapLogX()) + (x)) tron@679: tron@979: #define TILE_MASK(x) ((x) & ((1 << (MapLogX() + MapLogY())) - 1)) tron@926: tron@1218: extern byte *_map_type_and_height; tron@1218: extern byte *_map_owner; tron@1218: extern uint16 *_map2; tron@1218: extern byte *_map3_lo; tron@1218: extern byte *_map3_hi; tron@1218: extern byte *_map5; tron@1218: extern byte *_map_extra_bits; tron@1218: tron@1218: void InitMap(uint log_x, uint log_y); tron@679: tron@689: // binary logarithm of the map size, try to avoid using this one tron@689: static inline uint MapLogX(void) { extern uint _map_log_x; return _map_log_x; } tron@689: static inline uint MapLogY(void) { extern uint _map_log_y; return _map_log_y; } tron@689: /* The size of the map */ tron@689: static inline uint MapSizeX(void) { return 1 << MapLogX(); } tron@689: static inline uint MapSizeY(void) { return 1 << MapLogY(); } tron@689: /* The maximum coordinates */ tron@689: static inline uint MapMaxX(void) { return MapSizeX() - 1; } tron@689: static inline uint MapMaxY(void) { return MapSizeY() - 1; } tron@689: /* The number of tiles in the map */ tron@689: static inline uint MapSize(void) { return MapSizeX() * MapSizeY(); } tron@689: tron@1202: // Scale a number relative to the map size tron@1202: uint ScaleByMapSize(uint); // Scale relative to the number of tiles tron@1202: uint ScaleByMapSize1D(uint); // Scale relative to the circumference of the map tron@1202: tron@1174: typedef uint32 TileIndex; matthijs@1247: enum { matthijs@1247: INVALID_TILE = (uint32) -1 matthijs@1247: }; tron@926: tron@926: static inline uint TileX(TileIndex tile) tron@926: { tron@926: return tile & MapMaxX(); tron@926: } tron@926: tron@926: static inline uint TileY(TileIndex tile) tron@926: { tron@926: return tile >> MapLogX(); tron@926: } tron@926: tron@926: tron@1174: typedef int32 TileIndexDiff; tron@900: tron@909: typedef struct TileIndexDiffC { tron@909: int16 x; tron@909: int16 y; tron@909: } TileIndexDiffC; tron@909: tron@909: static inline TileIndexDiff ToTileIndexDiff(TileIndexDiffC tidc) tron@909: { tron@909: return (tidc.y << MapLogX()) + tidc.x; tron@909: } tron@900: tron@955: tron@955: #ifndef _DEBUG tron@955: #define TILE_ADD(x,y) ((x) + (y)) tron@955: #else tron@955: extern TileIndex TileAdd(TileIndex tile, TileIndexDiff add, tron@955: const char *exp, const char *file, int line); tron@955: #define TILE_ADD(x, y) (TileAdd((x), (y), #x " + " #y, __FILE__, __LINE__)) tron@955: #endif tron@955: tron@955: #define TILE_ADDXY(tile, x, y) TILE_ADD(tile, TILE_XY(x, y)) tron@955: matthijs@1247: uint TileAddWrap(TileIndex tile, int addx, int addy); matthijs@1247: matthijs@1247: static inline TileIndexDiffC TileIndexDiffCByDir(uint dir) { matthijs@1247: extern const TileIndexDiffC _tileoffs_by_dir[4]; matthijs@1247: return _tileoffs_by_dir[dir]; matthijs@1247: } matthijs@1247: matthijs@1247: /* Returns tile + the diff given in diff. If the result tile would end up matthijs@1247: * outside of the map, INVALID_TILE is returned instead. matthijs@1247: */ matthijs@1247: static inline TileIndex AddTileIndexDiffCWrap(TileIndex tile, TileIndexDiffC diff) { matthijs@1247: int x = TileX(tile) + diff.x; matthijs@1247: int y = TileY(tile) + diff.y; matthijs@1247: if (x < 0 || y < 0 || x > (int)MapMaxX() || y > (int)MapMaxY()) matthijs@1247: return INVALID_TILE; matthijs@1247: else matthijs@1247: return TILE_XY(x, y); matthijs@1247: } tron@955: tron@1245: // Functions to calculate distances tron@1245: uint DistanceManhattan(TileIndex, TileIndex); // also known as L1-Norm tron@1245: uint DistanceSquare(TileIndex, TileIndex); // euclidian- or L2-Norm squared tron@1245: uint DistanceMax(TileIndex, TileIndex); // also known as L-Infinity-Norm tron@1245: uint DistanceMaxPlusManhattan(TileIndex, TileIndex); // Max + Manhattan tron@1245: uint DistanceFromEdge(TileIndex); // shortest distance from any edge of the map tron@1245: tron@1245: tron@900: static inline TileIndexDiff TileOffsByDir(uint dir) tron@900: { tron@909: extern const TileIndexDiffC _tileoffs_by_dir[4]; tron@900: tron@900: assert(dir < lengthof(_tileoffs_by_dir)); tron@909: return ToTileIndexDiff(_tileoffs_by_dir[dir]); tron@900: } tron@900: tron@679: #endif