tron@2186: /* $Id$ */ tron@2186: belugas@6527: /** @file map.h */ belugas@6527: tron@679: #ifndef MAP_H tron@679: #define MAP_H tron@679: tron@1210: #include "stdafx.h" rubidium@9694: #include "direction.h" tron@1210: ludde@2051: extern uint _map_tile_mask; rubidium@9620: rubidium@9620: /** rubidium@9620: * 'Wraps' the given tile to it is within the map. It does rubidium@9620: * this by masking the 'high' bits of. rubidium@9620: * @param x the tile to 'wrap' rubidium@9620: */ ludde@2051: ludde@2051: #define TILE_MASK(x) ((x) & _map_tile_mask) rubidium@9620: /** rubidium@9620: * Asserts when the tile is outside of the map. rubidium@9620: * @param x the tile to check rubidium@9620: */ tron@1394: #define TILE_ASSERT(x) assert(TILE_MASK(x) == (x)); tron@926: rubidium@9620: /** rubidium@9620: * Data that is stored per tile. Also used TileExtended for this. rubidium@9620: * Look at docs/landscape.html for the exact meaning of the members. rubidium@9620: */ rubidium@6574: struct Tile { rubidium@9620: byte type_height; ///< The type (bits 4..7) and height of the northern corner rubidium@9620: byte m1; ///< Primarily used for ownership information rubidium@9620: uint16 m2; ///< Primarily used for indices to towns, industries and stations rubidium@9620: byte m3; ///< General purpose rubidium@9620: byte m4; ///< General purpose rubidium@9620: byte m5; ///< General purpose rubidium@9620: byte m6; ///< Primarily used for bridges and rainforest/desert rubidium@6574: }; tron@2049: rubidium@9620: /** rubidium@9620: * Data that is stored per tile. Also used Tile for this. rubidium@9620: * Look at docs/landscape.html for the exact meaning of the members. rubidium@9620: */ truelight@9476: struct TileExtended { rubidium@9620: byte m7; ///< Primarily used for newgrf support truelight@9476: }; truelight@9476: truelight@9476: extern Tile *_m; truelight@9476: extern TileExtended *_me; tron@1218: ludde@2051: void AllocateMap(uint size_x, uint size_y); ludde@2051: rubidium@9620: /** rubidium@9620: * Logarithm of the map size along the X side. rubidium@9620: * @note try to avoid using this one rubidium@9620: * @return 2^"return value" == MapSizeX() rubidium@9620: */ rubidium@9620: static inline uint MapLogX() rubidium@9620: { rubidium@9620: extern uint _map_log_x; rubidium@9620: return _map_log_x; rubidium@9620: } rubidium@9620: rubidium@9620: /** rubidium@9620: * Get the size of the map along the X rubidium@9620: * @return the number of tiles along the X of the map rubidium@9620: */ rubidium@9620: static inline uint MapSizeX() rubidium@9620: { rubidium@9620: extern uint _map_size_x; rubidium@9620: return _map_size_x; rubidium@9620: } rubidium@9620: rubidium@9620: /** rubidium@9620: * Get the size of the map along the Y rubidium@9620: * @return the number of tiles along the Y of the map rubidium@9620: */ rubidium@9620: static inline uint MapSizeY() rubidium@9620: { rubidium@9620: extern uint _map_size_y; rubidium@9620: return _map_size_y; rubidium@9620: } rubidium@9620: rubidium@9620: /** rubidium@9620: * Get the size of the map rubidium@9620: * @return the number of tiles of the map rubidium@9620: */ rubidium@9620: static inline uint MapSize() rubidium@9620: { rubidium@9620: extern uint _map_size; rubidium@9620: return _map_size; rubidium@9620: } rubidium@9620: rubidium@9620: /** rubidium@9620: * Gets the maximum X coordinate within the map, including MP_VOID rubidium@9620: * @return the maximum X coordinate rubidium@9620: */ rubidium@9620: static inline uint MapMaxX() rubidium@9620: { rubidium@9620: return MapSizeX() - 1; rubidium@9620: } rubidium@9620: rubidium@9620: /** rubidium@9620: * Gets the maximum X coordinate within the map, including MP_VOID rubidium@9620: * @return the maximum X coordinate rubidium@9620: */ rubidium@9620: static inline uint MapMaxY() rubidium@9620: { rubidium@9620: return MapSizeY() - 1; rubidium@9620: } tron@689: belugas@6527: /* 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@6527: /* Multiplication gives much better optimization on MSVC than shifting. belugas@6527: * 0 << shift isn't optimized to 0 properly. belugas@6527: * Typically x and y are constants, and then this doesn't result belugas@6527: * 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@9620: INVALID_TILE = (TileIndex)-1 ///< The very nice invalid tile marker matthijs@1247: }; tron@926: matthijs@1942: enum { belugas@6527: TILE_SIZE = 16, ///< Tiles are 16x16 "units" in size belugas@6527: TILE_PIXELS = 32, ///< a tile is 32x32 pixels belugas@6527: TILE_HEIGHT = 8, ///< The standard height-difference between tiles on two levels is 8 (z-diff 8) matthijs@1942: }; matthijs@1942: matthijs@1942: rubidium@9620: /** rubidium@9620: * Get the X component of a tile rubidium@9620: * @param tile the tile to get the X component of rubidium@9620: * @return the X component rubidium@9620: */ tron@926: static inline uint TileX(TileIndex tile) tron@926: { tron@926: return tile & MapMaxX(); tron@926: } tron@926: rubidium@9620: /** rubidium@9620: * Get the Y component of a tile rubidium@9620: * @param tile the tile to get the Y component of rubidium@9620: * @return the Y component rubidium@9620: */ tron@926: static inline uint TileY(TileIndex tile) tron@926: { tron@926: return tile >> MapLogX(); tron@926: } tron@926: tron@926: rubidium@6574: struct TileIndexDiffC { tron@909: int16 x; tron@909: int16 y; rubidium@6574: }; 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@9694: static inline TileIndexDiffC TileIndexDiffCByDiagDir(DiagDirection dir) rubidium@9694: { rubidium@9694: extern const TileIndexDiffC _tileoffs_by_diagdir[DIAGDIR_END]; glx@4561: rubidium@9694: 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@9694: static inline TileIndex AddTileIndexDiffCWrap(TileIndex tile, TileIndexDiffC diff) rubidium@9694: { 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: rubidium@9620: /** rubidium@9620: * Returns the diff between two tiles rubidium@9620: * rubidium@9620: * @param tile_a from tile rubidium@9620: * @param tile_b to tile rubidium@9620: * @return the difference between tila_a and tile_b rubidium@9620: */ rubidium@9620: static inline TileIndexDiffC TileIndexToTileIndexDiffC(TileIndex tile_a, TileIndex tile_b) rubidium@9620: { rubidium@9620: TileIndexDiffC difference; rubidium@9620: rubidium@9620: difference.x = TileX(tile_a) - TileX(tile_b); rubidium@9620: difference.y = TileY(tile_a) - TileY(tile_b); rubidium@9620: rubidium@9620: return difference; rubidium@9620: } rubidium@9620: belugas@6527: /* Functions to calculate distances */ belugas@6527: uint DistanceManhattan(TileIndex, TileIndex); ///< also known as L1-Norm. Is the shortest distance one could go over diagonal tracks (or roads) belugas@6527: uint DistanceSquare(TileIndex, TileIndex); ///< euclidian- or L2-Norm squared belugas@6527: uint DistanceMax(TileIndex, TileIndex); ///< also known as L-Infinity-Norm belugas@6527: uint DistanceMaxPlusManhattan(TileIndex, TileIndex); ///< Max + Manhattan belugas@6527: uint DistanceFromEdge(TileIndex); ///< shortest distance from any edge of the map tron@1245: tron@1245: rubidium@9601: #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@9601: #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@9694: static inline TileIndexDiff TileOffsByDiagDir(DiagDirection dir) Darkvater@4559: { rubidium@9694: extern const TileIndexDiffC _tileoffs_by_diagdir[DIAGDIR_END]; tron@2159: rubidium@9694: assert(IsValidDiagDirection(dir)); Darkvater@4559: return ToTileIndexDiff(_tileoffs_by_diagdir[dir]); Darkvater@4559: } Darkvater@4559: rubidium@9694: static inline TileIndexDiff TileOffsByDir(Direction dir) tron@900: { rubidium@9694: extern const TileIndexDiffC _tileoffs_by_dir[DIR_END]; tron@900: rubidium@9694: 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 */