tron@2186: /* $Id$ */ tron@2186: belugas@6201: /** @file map.h */ belugas@6201: tron@679: #ifndef MAP_H tron@679: #define MAP_H tron@679: tron@1210: #include "stdafx.h" rubidium@7317: #include "direction.h" tron@1210: ludde@2051: extern uint _map_tile_mask; rubidium@6540: rubidium@6540: /** rubidium@6540: * 'Wraps' the given tile to it is within the map. It does rubidium@6540: * this by masking the 'high' bits of. rubidium@6540: * @param x the tile to 'wrap' rubidium@6540: */ ludde@2051: ludde@2051: #define TILE_MASK(x) ((x) & _map_tile_mask) rubidium@6540: /** rubidium@6540: * Asserts when the tile is outside of the map. rubidium@6540: * @param x the tile to check rubidium@6540: */ tron@1394: #define TILE_ASSERT(x) assert(TILE_MASK(x) == (x)); tron@926: rubidium@6540: /** rubidium@6540: * Data that is stored per tile. Also used TileExtended for this. rubidium@6540: * Look at docs/landscape.html for the exact meaning of the members. rubidium@6540: */ rubidium@6248: struct Tile { rubidium@6540: byte type_height; ///< The type (bits 4..7) and height of the northern corner rubidium@6540: byte m1; ///< Primarily used for ownership information rubidium@6540: uint16 m2; ///< Primarily used for indices to towns, industries and stations rubidium@6540: byte m3; ///< General purpose rubidium@6540: byte m4; ///< General purpose rubidium@6540: byte m5; ///< General purpose rubidium@6540: byte m6; ///< Primarily used for bridges and rainforest/desert rubidium@6248: }; tron@2049: rubidium@6540: /** rubidium@6540: * Data that is stored per tile. Also used Tile for this. rubidium@6540: * Look at docs/landscape.html for the exact meaning of the members. rubidium@6540: */ maedhros@6332: struct TileExtended { rubidium@6540: byte m7; ///< Primarily used for newgrf support maedhros@6332: }; maedhros@6332: maedhros@6332: extern Tile *_m; maedhros@6332: extern TileExtended *_me; tron@1218: ludde@2051: void AllocateMap(uint size_x, uint size_y); ludde@2051: rubidium@6540: /** rubidium@6540: * Logarithm of the map size along the X side. rubidium@6540: * @note try to avoid using this one rubidium@6540: * @return 2^"return value" == MapSizeX() rubidium@6540: */ rubidium@6540: static inline uint MapLogX() rubidium@6540: { rubidium@6540: extern uint _map_log_x; rubidium@6540: return _map_log_x; rubidium@6540: } rubidium@6540: rubidium@6540: /** rubidium@6540: * Get the size of the map along the X rubidium@6540: * @return the number of tiles along the X of the map rubidium@6540: */ rubidium@6540: static inline uint MapSizeX() rubidium@6540: { rubidium@6540: extern uint _map_size_x; rubidium@6540: return _map_size_x; rubidium@6540: } rubidium@6540: rubidium@6540: /** rubidium@6540: * Get the size of the map along the Y rubidium@6540: * @return the number of tiles along the Y of the map rubidium@6540: */ rubidium@6540: static inline uint MapSizeY() rubidium@6540: { rubidium@6540: extern uint _map_size_y; rubidium@6540: return _map_size_y; rubidium@6540: } rubidium@6540: rubidium@6540: /** rubidium@6540: * Get the size of the map rubidium@6540: * @return the number of tiles of the map rubidium@6540: */ rubidium@6540: static inline uint MapSize() rubidium@6540: { rubidium@6540: extern uint _map_size; rubidium@6540: return _map_size; rubidium@6540: } rubidium@6540: rubidium@6540: /** rubidium@6540: * Gets the maximum X coordinate within the map, including MP_VOID rubidium@6540: * @return the maximum X coordinate rubidium@6540: */ rubidium@6540: static inline uint MapMaxX() rubidium@6540: { rubidium@6540: return MapSizeX() - 1; rubidium@6540: } rubidium@6540: rubidium@6540: /** rubidium@6540: * Gets the maximum X coordinate within the map, including MP_VOID rubidium@6540: * @return the maximum X coordinate rubidium@6540: */ rubidium@6540: static inline uint MapMaxY() rubidium@6540: { rubidium@6540: return MapSizeY() - 1; rubidium@6540: } tron@689: belugas@6201: /* 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; tron@1981: typedef int32 TileIndexDiff; tron@1981: tron@1981: static inline TileIndex TileXY(uint x, uint y) tron@1981: { ludde@2051: return (y * MapSizeX()) + x; tron@1981: } tron@1981: tron@1981: static inline TileIndexDiff TileDiffXY(int x, int y) tron@1981: { belugas@6201: /* Multiplication gives much better optimization on MSVC than shifting. belugas@6201: * 0 << shift isn't optimized to 0 properly. belugas@6201: * Typically x and y are constants, and then this doesn't result belugas@6201: * in any actual multiplication in the assembly code.. */ ludde@2051: return (y * MapSizeX()) + x; tron@1981: } matthijs@1330: tron@1980: static inline TileIndex TileVirtXY(uint x, uint y) tron@1980: { tron@1980: return (y >> 4 << MapLogX()) + (x >> 4); tron@1980: } tron@1980: matthijs@1330: matthijs@1247: enum { rubidium@6540: INVALID_TILE = (TileIndex)-1 ///< The very nice invalid tile marker matthijs@1247: }; tron@926: matthijs@1942: enum { belugas@6201: TILE_SIZE = 16, ///< Tiles are 16x16 "units" in size belugas@6201: TILE_PIXELS = 32, ///< a tile is 32x32 pixels belugas@6201: TILE_HEIGHT = 8, ///< The standard height-difference between tiles on two levels is 8 (z-diff 8) matthijs@1942: }; matthijs@1942: matthijs@1942: rubidium@6540: /** rubidium@6540: * Get the X component of a tile rubidium@6540: * @param tile the tile to get the X component of rubidium@6540: * @return the X component rubidium@6540: */ tron@926: static inline uint TileX(TileIndex tile) tron@926: { tron@926: return tile & MapMaxX(); tron@926: } tron@926: rubidium@6540: /** rubidium@6540: * Get the Y component of a tile rubidium@6540: * @param tile the tile to get the Y component of rubidium@6540: * @return the Y component rubidium@6540: */ tron@926: static inline uint TileY(TileIndex tile) tron@926: { tron@926: return tile >> MapLogX(); tron@926: } tron@926: tron@926: rubidium@6248: struct TileIndexDiffC { tron@909: int16 x; tron@909: int16 y; rubidium@6248: }; 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@1981: #define TILE_ADDXY(tile, x, y) TILE_ADD(tile, TileDiffXY(x, y)) tron@955: matthijs@1247: uint TileAddWrap(TileIndex tile, int addx, int addy); matthijs@1247: rubidium@7317: static inline TileIndexDiffC TileIndexDiffCByDiagDir(DiagDirection dir) rubidium@7317: { rubidium@7317: extern const TileIndexDiffC _tileoffs_by_diagdir[DIAGDIR_END]; glx@4561: rubidium@7317: assert(IsValidDiagDirection(dir)); glx@4561: return _tileoffs_by_diagdir[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: */ rubidium@7317: static inline TileIndex AddTileIndexDiffCWrap(TileIndex tile, TileIndexDiffC diff) rubidium@7317: { 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 tron@1981: return TileXY(x, y); matthijs@1247: } tron@955: belugas@6571: /** belugas@6571: * Returns the diff between two tiles belugas@6571: * belugas@6571: * @param tile_a from tile belugas@6571: * @param tile_b to tile belugas@6571: * @return the difference between tila_a and tile_b belugas@6571: */ belugas@6571: static inline TileIndexDiffC TileIndexToTileIndexDiffC(TileIndex tile_a, TileIndex tile_b) belugas@6571: { belugas@6571: TileIndexDiffC difference; belugas@6571: belugas@6571: difference.x = TileX(tile_a) - TileX(tile_b); belugas@6571: difference.y = TileY(tile_a) - TileY(tile_b); belugas@6571: belugas@6571: return difference; belugas@6571: } belugas@6571: belugas@6201: /* Functions to calculate distances */ belugas@6201: uint DistanceManhattan(TileIndex, TileIndex); ///< also known as L1-Norm. Is the shortest distance one could go over diagonal tracks (or roads) belugas@6201: uint DistanceSquare(TileIndex, TileIndex); ///< euclidian- or L2-Norm squared belugas@6201: uint DistanceMax(TileIndex, TileIndex); ///< also known as L-Infinity-Norm belugas@6201: uint DistanceMaxPlusManhattan(TileIndex, TileIndex); ///< Max + Manhattan belugas@6201: uint DistanceFromEdge(TileIndex); ///< shortest distance from any edge of the map tron@1245: tron@1245: rubidium@6491: #define BEGIN_TILE_LOOP(var, w, h, tile) \ tron@2159: { \ tron@2159: int h_cur = h; \ tron@2159: uint var = tile; \ tron@2159: do { \ tron@2159: int w_cur = w; \ tron@2159: do { tron@2159: rubidium@6491: #define END_TILE_LOOP(var, w, h, tile) \ tron@2159: } while (++var, --w_cur != 0); \ tron@2159: } while (var += TileDiffXY(0, 1) - (w), --h_cur != 0); \ tron@2159: } tron@2159: rubidium@7317: static inline TileIndexDiff TileOffsByDiagDir(DiagDirection dir) Darkvater@4559: { rubidium@7317: extern const TileIndexDiffC _tileoffs_by_diagdir[DIAGDIR_END]; tron@2159: rubidium@7317: assert(IsValidDiagDirection(dir)); Darkvater@4559: return ToTileIndexDiff(_tileoffs_by_diagdir[dir]); Darkvater@4559: } Darkvater@4559: rubidium@7317: static inline TileIndexDiff TileOffsByDir(Direction dir) tron@900: { rubidium@7317: extern const TileIndexDiffC _tileoffs_by_dir[DIR_END]; tron@900: rubidium@7317: assert(IsValidDirection(dir)); tron@909: return ToTileIndexDiff(_tileoffs_by_dir[dir]); tron@900: } tron@900: belugas@5118: typedef bool TestTileOnSearchProc(TileIndex tile, uint32 data); belugas@5118: bool CircularTileSearch(TileIndex tile, uint size, TestTileOnSearchProc proc, uint32 data); belugas@5118: matthijs@1677: /* Approximation of the length of a straight track, relative to a diagonal matthijs@1677: * track (ie the size of a tile side). #defined instead of const so it can matthijs@1677: * stay integer. (no runtime float operations) Is this needed? matthijs@1679: * Watch out! There are _no_ brackets around here, to prevent intermediate matthijs@1679: * rounding! Be careful when using this! matthijs@1677: * This value should be sqrt(2)/2 ~ 0.7071 */ matthijs@1679: #define STRAIGHT_TRACK_LENGTH 7071/10000 matthijs@1677: Darkvater@2436: #endif /* MAP_H */