tron@679: #ifndef MAP_H tron@679: #define MAP_H tron@679: tron@927: #define TILE_FROM_XY(x,y) (int)((((y) >> 4) << MapLogX()) + ((x) >> 4)) tron@927: #define TILE_XY(x,y) (int)(((y) << MapLogX()) + (x)) tron@679: tron@979: #define TILE_MASK(x) ((x) & ((1 << (MapLogX() + MapLogY())) - 1)) tron@926: truelight@817: extern byte _map_type_and_height[]; truelight@817: extern byte _map5[]; truelight@817: extern byte _map3_lo[]; truelight@817: extern byte _map3_hi[]; truelight@817: extern byte _map_owner[]; truelight@817: extern uint16 _map2[]; truelight@817: extern byte _map_extra_bits[]; 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@926: tron@926: typedef uint16 TileIndex; 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@900: typedef int16 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: tron@955: 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@1035: tron@1044: static inline uint TileHeight(TileIndex tile) tron@1044: { tron@1044: assert(tile < MapSize()); tron@1044: return _map_type_and_height[tile] & 0xf; tron@1044: } tron@1044: tron@1041: static inline uint TilePixelHeight(TileIndex tile) tron@1035: { tron@1044: return TileHeight(tile) * 8; tron@1035: } tron@1035: tron@1035: static inline int TileType(TileIndex tile) tron@1035: { tron@1035: assert(tile < MapSize()); tron@1035: return _map_type_and_height[tile] >> 4; tron@1035: } tron@1035: tron@1035: static inline bool IsTileType(TileIndex tile, int type) tron@1035: { tron@1035: return TileType(tile) == type; tron@1035: } tron@1035: tron@679: #endif