tron@2186: /* $Id$ */ tron@2186: glx@8844: /** @file map_func.h */ belugas@6527: rubidium@8635: #ifndef MAP_FUNC_H rubidium@8635: #define MAP_FUNC_H tron@679: rubidium@8635: #include "tile_type.h" rubidium@8635: #include "map_type.h" rubidium@8596: #include "direction_func.h" tron@1210: ludde@2051: extern uint _map_tile_mask; rubidium@7036: rubidium@7036: /** rubidium@7036: * 'Wraps' the given tile to it is within the map. It does rubidium@7036: * this by masking the 'high' bits of. rubidium@7036: * @param x the tile to 'wrap' rubidium@7036: */ ludde@2051: ludde@2051: #define TILE_MASK(x) ((x) & _map_tile_mask) rubidium@7036: /** rubidium@7036: * Asserts when the tile is outside of the map. rubidium@7036: * @param x the tile to check rubidium@7036: */ tron@1394: #define TILE_ASSERT(x) assert(TILE_MASK(x) == (x)); tron@926: rubidium@7036: /** rubidium@8042: * Pointer to the tile-array. rubidium@8042: * rubidium@8042: * This variable points to the tile-array which contains the tiles of rubidium@8042: * the map. rubidium@8042: */ maedhros@6658: extern Tile *_m; rubidium@8042: rubidium@8042: /** rubidium@8042: * Pointer to the extended tile-array. rubidium@8042: * rubidium@8042: * This variable points to the extended tile-array which contains the tiles rubidium@8042: * of the map. rubidium@8042: */ maedhros@6658: extern TileExtended *_me; tron@1218: rubidium@8042: /** rubidium@8042: * Allocate a new map with the given size. rubidium@8042: */ ludde@2051: void AllocateMap(uint size_x, uint size_y); ludde@2051: rubidium@7036: /** rubidium@7036: * Logarithm of the map size along the X side. rubidium@7036: * @note try to avoid using this one rubidium@7036: * @return 2^"return value" == MapSizeX() rubidium@7036: */ rubidium@7036: static inline uint MapLogX() rubidium@7036: { rubidium@7036: extern uint _map_log_x; rubidium@7036: return _map_log_x; rubidium@7036: } rubidium@7036: rubidium@7036: /** belugas@8887: * Logarithm of the map size along the y side. belugas@8887: * @note try to avoid using this one belugas@8887: * @return 2^"return value" == MapSizeY() belugas@8887: */ belugas@8887: static inline uint MapLogY() belugas@8887: { belugas@8887: extern uint _map_log_y; belugas@8887: return _map_log_y; belugas@8887: } belugas@8887: belugas@8887: /** rubidium@7036: * Get the size of the map along the X rubidium@7036: * @return the number of tiles along the X of the map rubidium@7036: */ rubidium@7036: static inline uint MapSizeX() rubidium@7036: { rubidium@7036: extern uint _map_size_x; rubidium@7036: return _map_size_x; rubidium@7036: } rubidium@7036: rubidium@7036: /** rubidium@7036: * Get the size of the map along the Y rubidium@7036: * @return the number of tiles along the Y of the map rubidium@7036: */ rubidium@7036: static inline uint MapSizeY() rubidium@7036: { rubidium@7036: extern uint _map_size_y; rubidium@7036: return _map_size_y; rubidium@7036: } rubidium@7036: rubidium@7036: /** rubidium@7036: * Get the size of the map rubidium@7036: * @return the number of tiles of the map rubidium@7036: */ rubidium@7036: static inline uint MapSize() rubidium@7036: { rubidium@7036: extern uint _map_size; rubidium@7036: return _map_size; rubidium@7036: } rubidium@7036: rubidium@7036: /** rubidium@7036: * Gets the maximum X coordinate within the map, including MP_VOID rubidium@7036: * @return the maximum X coordinate rubidium@7036: */ rubidium@7036: static inline uint MapMaxX() rubidium@7036: { rubidium@7036: return MapSizeX() - 1; rubidium@7036: } rubidium@7036: rubidium@7036: /** rubidium@7036: * Gets the maximum X coordinate within the map, including MP_VOID rubidium@7036: * @return the maximum X coordinate rubidium@7036: */ rubidium@7036: static inline uint MapMaxY() rubidium@7036: { rubidium@7036: return MapSizeY() - 1; rubidium@7036: } tron@689: rubidium@8042: /** rubidium@8042: * Scales relative to the number of tiles. rubidium@8042: */ rubidium@8042: uint ScaleByMapSize(uint); tron@1202: rubidium@8042: /** rubidium@8042: * Scale relative to the circumference of the map. rubidium@8042: */ rubidium@8042: uint ScaleByMapSize1D(uint); rubidium@8042: rubidium@8042: /** rubidium@8042: * An offset value between to tiles. rubidium@8042: * rubidium@8042: * This value is used fro the difference between rubidium@8042: * to tiles. It can be added to a tileindex to get rubidium@8042: * the resulting tileindex of the start tile applied rubidium@8042: * with this saved difference. rubidium@8042: * rubidium@8042: * @see TileDiffXY(int, int) rubidium@8042: */ tron@1981: typedef int32 TileIndexDiff; tron@1981: rubidium@8042: /** rubidium@8042: * Returns the TileIndex of a coordinate. rubidium@8042: * rubidium@8042: * @param x The x coordinate of the tile rubidium@8042: * @param y The y coordinate of the tile rubidium@8042: * @return The TileIndex calculated by the coordinate rubidium@8042: */ tron@1981: static inline TileIndex TileXY(uint x, uint y) tron@1981: { ludde@2051: return (y * MapSizeX()) + x; tron@1981: } tron@1981: rubidium@8042: /** rubidium@8042: * Calculates an offset for the given coordinate(-offset). rubidium@8042: * rubidium@8042: * This function calculate an offset value which can be added to an rubidium@8042: * #TileIndex. The coordinates can be negative. rubidium@8042: * rubidium@8042: * @param x The offset in x direction rubidium@8042: * @param y The offset in y direction rubidium@8042: * @return The resulting offset value of the given coordinate rubidium@8042: * @see ToTileIndexDiff(TileIndexDiffC) rubidium@8042: */ 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: rubidium@7036: /** rubidium@7036: * Get the X component of a tile rubidium@7036: * @param tile the tile to get the X component of rubidium@7036: * @return the X component rubidium@7036: */ tron@926: static inline uint TileX(TileIndex tile) tron@926: { tron@926: return tile & MapMaxX(); tron@926: } tron@926: rubidium@7036: /** rubidium@7036: * Get the Y component of a tile rubidium@7036: * @param tile the tile to get the Y component of rubidium@7036: * @return the Y component rubidium@7036: */ tron@926: static inline uint TileY(TileIndex tile) tron@926: { tron@926: return tile >> MapLogX(); tron@926: } tron@926: rubidium@8042: /** rubidium@8042: * Return the offset between to tiles from a TileIndexDiffC struct. rubidium@8042: * rubidium@8042: * This function works like #TileDiffXY(int, int) and returns the rubidium@8042: * difference between two tiles. rubidium@8042: * rubidium@8042: * @param tidc The coordinate of the offset as TileIndexDiffC rubidium@8042: * @return The difference between two tiles. rubidium@8042: * @see TileDiffXY(int, int) rubidium@8042: */ 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 rubidium@8635: /** rubidium@8635: * Adds to tiles together. rubidium@8635: * rubidium@8635: * @param x One tile rubidium@8635: * @param y An other tile to add rubidium@8635: * @return The resulting tile(index) rubidium@8635: */ 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: rubidium@8042: /** rubidium@8042: * Adds a given offset to a tile. rubidium@8042: * rubidium@8042: * @param tile The tile to add an offset on it rubidium@8042: * @param x The x offset to add to the tile rubidium@8042: * @param y The y offset to add to the tile rubidium@8042: */ tron@1981: #define TILE_ADDXY(tile, x, y) TILE_ADD(tile, TileDiffXY(x, y)) tron@955: rubidium@8042: /** rubidium@8042: * Adds an offset to a tile and check if we are still on the map. rubidium@8042: */ frosch@8827: TileIndex TileAddWrap(TileIndex tile, int addx, int addy); matthijs@1247: rubidium@8042: /** rubidium@8042: * Returns the TileIndexDiffC offset from a DiagDirection. rubidium@8042: * rubidium@8042: * @param dir The given direction rubidium@8042: * @return The offset as TileIndexDiffC value rubidium@8042: */ rubidium@7813: static inline TileIndexDiffC TileIndexDiffCByDiagDir(DiagDirection dir) rubidium@7813: { rubidium@7813: extern const TileIndexDiffC _tileoffs_by_diagdir[DIAGDIR_END]; glx@4561: rubidium@7813: assert(IsValidDiagDirection(dir)); glx@4561: return _tileoffs_by_diagdir[dir]; matthijs@1247: } rubidium@8635: rubidium@8042: /** frosch@8876: * Returns the TileIndexDiffC offset from a Direction. frosch@8876: * frosch@8876: * @param dir The given direction frosch@8876: * @return The offset as TileIndexDiffC value frosch@8876: */ frosch@8876: static inline TileIndexDiffC TileIndexDiffCByDir(Direction dir) frosch@8876: { frosch@8876: extern const TileIndexDiffC _tileoffs_by_dir[DIR_END]; frosch@8876: frosch@8876: assert(IsValidDirection(dir)); frosch@8876: return _tileoffs_by_dir[dir]; frosch@8876: } frosch@8876: frosch@8876: /** rubidium@8042: * Add a TileIndexDiffC to a TileIndex and returns the new one. rubidium@8042: * rubidium@8042: * 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. rubidium@8042: * rubidium@8042: * @param tile The base tile to add the offset on rubidium@8042: * @param diff The offset to add on the tile rubidium@8042: * @return The resulting TileIndex matthijs@1247: */ rubidium@7813: static inline TileIndex AddTileIndexDiffCWrap(TileIndex tile, TileIndexDiffC diff) rubidium@7813: { 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@7067: /** belugas@7067: * Returns the diff between two tiles belugas@7067: * belugas@7067: * @param tile_a from tile belugas@7067: * @param tile_b to tile belugas@7067: * @return the difference between tila_a and tile_b belugas@7067: */ belugas@7067: static inline TileIndexDiffC TileIndexToTileIndexDiffC(TileIndex tile_a, TileIndex tile_b) belugas@7067: { belugas@7067: TileIndexDiffC difference; belugas@7067: belugas@7067: difference.x = TileX(tile_a) - TileX(tile_b); belugas@7067: difference.y = TileY(tile_a) - TileY(tile_b); belugas@7067: belugas@7067: return difference; belugas@7067: } belugas@7067: 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: rubidium@8042: /** rubidium@8042: * Starts a loop which iterates to a square of tiles rubidium@8042: * rubidium@8042: * This macro starts 2 nested loops which iterates over a square of tiles. rubidium@8042: * rubidium@8042: * @param var The name of the variable which contains the current tile rubidium@8042: * @param w The width (x-width) of the square rubidium@8042: * @param h The heigth (y-width) of the square rubidium@8042: * @param tile The start tile of the square rubidium@8042: */ rubidium@6987: #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 { rubidium@8042: /** rubidium@8042: * Ends the square-loop used before rubidium@8042: * rubidium@8042: * @see BEGIN_TILE_LOOP rubidium@8042: */ rubidium@6987: #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: } rubidium@8042: /** rubidium@8042: * Convert a DiagDirection to a TileIndexDiff rubidium@8042: * rubidium@8042: * @param dir The DiagDirection rubidium@8042: * @return The resulting TileIndexDiff rubidium@8042: * @see TileIndexDiffCByDiagDir rubidium@8042: */ rubidium@7813: static inline TileIndexDiff TileOffsByDiagDir(DiagDirection dir) Darkvater@4559: { rubidium@7813: extern const TileIndexDiffC _tileoffs_by_diagdir[DIAGDIR_END]; tron@2159: rubidium@7813: assert(IsValidDiagDirection(dir)); Darkvater@4559: return ToTileIndexDiff(_tileoffs_by_diagdir[dir]); Darkvater@4559: } Darkvater@4559: rubidium@8042: /** rubidium@8042: * Convert a Direction to a TileIndexDiff. rubidium@8042: * rubidium@8042: * @param dir The direction to convert from rubidium@8042: * @return The resulting TileIndexDiff rubidium@8042: */ rubidium@7813: static inline TileIndexDiff TileOffsByDir(Direction dir) tron@900: { rubidium@7813: extern const TileIndexDiffC _tileoffs_by_dir[DIR_END]; tron@900: rubidium@7813: assert(IsValidDirection(dir)); tron@909: return ToTileIndexDiff(_tileoffs_by_dir[dir]); tron@900: } tron@900: rubidium@8042: /** rubidium@8137: * Adds a DiagDir to a tile. rubidium@8137: * rubidium@8137: * @param tile The current tile rubidium@8137: * @param dir The direction in which we want to step rubidium@8137: * @return the moved tile rubidium@8137: */ rubidium@8137: static inline TileIndex TileAddByDiagDir(TileIndex tile, DiagDirection dir) rubidium@8137: { rubidium@8137: return TILE_ADD(tile, TileOffsByDiagDir(dir)); rubidium@8137: } rubidium@8137: rubidium@8137: /** rubidium@8042: * A callback function type for searching tiles. rubidium@8042: * rubidium@8042: * @param tile The tile to test rubidium@8042: * @param data additional data for the callback function to use rubidium@8042: * @return A boolean value, depend on the definition of the function. rubidium@8042: */ belugas@5118: typedef bool TestTileOnSearchProc(TileIndex tile, uint32 data); rubidium@8042: rubidium@8042: /** rubidium@8042: * Searches for some cirumstances of a tile around a given tile with a helper function. rubidium@8042: */ belugas@5118: bool CircularTileSearch(TileIndex tile, uint size, TestTileOnSearchProc proc, uint32 data); belugas@5118: rubidium@8635: /** rubidium@8635: * Get a random tile out of a given seed. rubidium@8635: * @param r the random 'seed' rubidium@8635: * @return a valid tile rubidium@8635: */ rubidium@8635: static inline TileIndex RandomTileSeed(uint32 r) rubidium@8635: { rubidium@8635: return TILE_MASK(r); rubidium@8635: } matthijs@1677: rubidium@8635: /** rubidium@8635: * Get a valid random tile. rubidium@8635: * @note a define so 'random' gets inserted in the place where it is actually rubidium@8635: * called, thus making the random traces more explicit. rubidium@8635: * @return a valid tile rubidium@8635: */ rubidium@8635: #define RandomTile() RandomTileSeed(Random()) rubidium@8635: rubidium@8635: #endif /* MAP_FUNC_H */