tron@2186: /* $Id$ */ tron@2186: belugas@6527: /** @file map.cpp */ belugas@6527: tron@679: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@1299: #include "debug.h" tron@1218: #include "functions.h" tron@2159: #include "macros.h" tron@679: #include "map.h" belugas@5118: #include "direction.h" rubidium@5838: #include "helpers.hpp" tron@679: Darkvater@2482: #if defined(_MSC_VER) && _MSC_VER >= 1400 /* VStudio 2005 is stupid! */ Darkvater@2482: /* Why the hell is that not in all MSVC headers?? */ rubidium@5838: extern "C" _CRTIMP void __cdecl _assert(void *, void *, unsigned); Darkvater@2482: #endif Darkvater@2482: rubidium@9620: uint _map_log_x; ///< 2^_map_log_x == _map_size_x rubidium@9620: uint _map_size_x; ///< Size of the map along the X rubidium@9620: uint _map_size_y; ///< Size of the map along the Y rubidium@9620: uint _map_size; ///< The number of tiles on the map rubidium@9620: uint _map_tile_mask; ///< _map_size - 1 (to mask the mapsize) tron@689: rubidium@9620: Tile *_m = NULL; ///< Tiles of the map rubidium@9620: TileExtended *_me = NULL; ///< Extended Tiles of the map tron@863: tron@1218: rubidium@9620: /** rubidium@9620: * (Re)allocates a map with the given dimension rubidium@9620: * @param size_x the width of the map along the NE/SW edge rubidium@9620: * @param size_y the 'height' of the map along the SE/NW edge rubidium@9620: */ ludde@2051: void AllocateMap(uint size_x, uint size_y) tron@1218: { belugas@6527: /* Make sure that the map size is within the limits and that belugas@6527: * the x axis size is a power of 2. */ ludde@2051: if (size_x < 64 || size_x > 2048 || ludde@2051: size_y < 64 || size_y > 2048 || rubidium@9620: (size_x & (size_x - 1)) != 0 || rubidium@9620: (size_y & (size_y - 1)) != 0) tron@1244: error("Invalid map size"); tron@1218: Darkvater@5568: DEBUG(map, 1, "Allocating map of size %dx%d", size_x, size_y); tron@1218: ludde@2051: _map_log_x = FindFirstBit(size_x); ludde@2051: _map_size_x = size_x; ludde@2051: _map_size_y = size_y; ludde@2051: _map_size = size_x * size_y; ludde@2051: _map_tile_mask = _map_size - 1; tron@1218: tron@2049: free(_m); truelight@9476: free(_me); tron@1218: truelight@9476: _m = CallocT(_map_size); rubidium@9601: _me = CallocT(_map_size); truelight@9476: rubidium@9601: /* XXX @todo handle memory shortage more gracefully rubidium@9601: * Maybe some attemps could be made to try with smaller maps down to 64x64 rubidium@9601: * Maybe check for available memory before doing the calls, after all, we know how big rubidium@9601: * the map is */ truelight@9476: if ((_m == NULL) || (_me == NULL)) error("Failed to allocate memory for the map"); tron@1218: } tron@900: tron@955: tron@955: #ifdef _DEBUG tron@955: TileIndex TileAdd(TileIndex tile, TileIndexDiff add, tron@955: const char *exp, const char *file, int line) tron@955: { tron@955: int dx; tron@955: int dy; tron@955: uint x; tron@955: uint y; tron@955: tron@955: dx = add & MapMaxX(); darkvater@957: if (dx >= (int)MapSizeX() / 2) dx -= MapSizeX(); tron@955: dy = (add - dx) / (int)MapSizeX(); tron@955: tron@955: x = TileX(tile) + dx; tron@955: y = TileY(tile) + dy; tron@955: tron@955: if (x >= MapSizeX() || y >= MapSizeY()) { tron@955: char buf[512]; tron@955: Darkvater@5170: snprintf(buf, lengthof(buf), "TILE_ADD(%s) when adding 0x%.4X and 0x%.4X failed", tron@955: exp, tile, add); tron@955: #if !defined(_MSC_VER) tron@955: fprintf(stderr, "%s:%d %s\n", file, line, buf); tron@955: #else tron@955: _assert(buf, (char*)file, line); tron@955: #endif tron@955: } tron@955: rubidium@9601: assert(TileXY(x, y) == TILE_MASK(tile + add)); tron@955: rubidium@9601: return TileXY(x, y); tron@955: } tron@955: #endif tron@955: rubidium@9620: /** rubidium@9620: * Scales the given value by the map size, where the given value is rubidium@9620: * for a 256 by 256 map rubidium@9620: * @param n the value to scale rubidium@9620: * @return the scaled size rubidium@9620: */ tron@1202: uint ScaleByMapSize(uint n) tron@1202: { belugas@6527: /* First shift by 12 to prevent integer overflow for large values of n. belugas@6527: * >>12 is safe since the min mapsize is 64x64 belugas@6527: * Add (1<<4)-1 to round upwards. */ rubidium@9601: return (n * (MapSize() >> 12) + (1 << 4) - 1) >> 4; tron@1202: } tron@1202: tron@1202: rubidium@9620: /** rubidium@9620: * Scales the given value by the maps circumference, where the given rubidium@9620: * value is for a 256 by 256 map rubidium@9620: * @param n the value to scale rubidium@9620: * @return the scaled size rubidium@9620: */ tron@1202: uint ScaleByMapSize1D(uint n) tron@1202: { belugas@6527: /* Normal circumference for the X+Y is 256+256 = 1<<9 belugas@6527: * Note, not actually taking the full circumference into account, belugas@6527: * just half of it. belugas@6527: * (1<<9) - 1 is there to scale upwards. */ rubidium@9601: return (n * (MapSizeX() + MapSizeY()) + (1 << 9) - 1) >> 9; tron@1202: } tron@1202: tron@1202: rubidium@9620: /** rubidium@9620: * This function checks if we add addx/addy to tile, if we belugas@6527: * do wrap around the edges. For example, tile = (10,2) and belugas@6527: * addx = +3 and addy = -4. This function will now return belugas@6527: * INVALID_TILE, because the y is wrapped. This is needed in belugas@6527: * for example, farmland. When the tile is not wrapped, rubidium@9620: * the result will be tile + TileDiffXY(addx, addy) rubidium@9620: * @param tile the 'starting' point of the adding rubidium@9620: * @param addx the amount of tiles in the X direction to add rubidium@9620: * @param addy the amount of tiles in the Y direction to add rubidium@9620: * @return translated tile, or INVALID_TILE when it would've wrapped. rubidium@9620: */ matthijs@1247: uint TileAddWrap(TileIndex tile, int addx, int addy) matthijs@1247: { tron@2548: uint x = TileX(tile) + addx; tron@2548: uint y = TileY(tile) + addy; matthijs@1247: belugas@6527: /* Are we about to wrap? */ tron@1981: if (x < MapMaxX() && y < MapMaxY()) tron@1981: return tile + TileDiffXY(addx, addy); matthijs@1247: matthijs@1247: return INVALID_TILE; matthijs@1247: } matthijs@1247: rubidium@9620: /** 'Lookup table' for tile offsets given a DiagDirection */ rubidium@5838: extern const TileIndexDiffC _tileoffs_by_diagdir[] = { belugas@6527: {-1, 0}, ///< DIAGDIR_NE belugas@6527: { 0, 1}, ///< DIAGDIR_SE belugas@6527: { 1, 0}, ///< DIAGDIR_SW belugas@6527: { 0, -1} ///< DIAGDIR_NW Darkvater@4559: }; Darkvater@4559: rubidium@9620: /** 'Lookup table' for tile offsets given a Direction */ rubidium@5838: extern const TileIndexDiffC _tileoffs_by_dir[] = { belugas@6527: {-1, -1}, ///< DIR_N belugas@6527: {-1, 0}, ///< DIR_NE belugas@6527: {-1, 1}, ///< DIR_E belugas@6527: { 0, 1}, ///< DIR_SE belugas@6527: { 1, 1}, ///< DIR_S belugas@6527: { 1, 0}, ///< DIR_SW belugas@6527: { 1, -1}, ///< DIR_W belugas@6527: { 0, -1} ///< DIR_NW matthijs@1247: }; matthijs@1247: rubidium@9620: /** rubidium@9620: * Gets the Manhattan distance between the two given tiles. rubidium@9620: * The Manhattan distance is the sum of the delta of both the rubidium@9620: * X and Y component. rubidium@9620: * Also known as L1-Norm rubidium@9620: * @param t0 the start tile rubidium@9620: * @param t1 the end tile rubidium@9620: * @return the distance rubidium@9620: */ tron@1245: uint DistanceManhattan(TileIndex t0, TileIndex t1) tron@1245: { rubidium@5838: const uint dx = delta(TileX(t0), TileX(t1)); rubidium@5838: const uint dy = delta(TileY(t0), TileY(t1)); matthijs@1677: return dx + dy; tron@1245: } tron@1245: tron@1245: rubidium@9620: /** rubidium@9620: * Gets the 'Square' distance between the two given tiles. rubidium@9620: * The 'Square' distance is the square of the shortest (straight line) rubidium@9620: * distance between the two tiles. rubidium@9620: * Also known as euclidian- or L2-Norm squared. rubidium@9620: * @param t0 the start tile rubidium@9620: * @param t1 the end tile rubidium@9620: * @return the distance rubidium@9620: */ tron@1245: uint DistanceSquare(TileIndex t0, TileIndex t1) tron@1245: { matthijs@1677: const int dx = TileX(t0) - TileX(t1); matthijs@1677: const int dy = TileY(t0) - TileY(t1); matthijs@1677: return dx * dx + dy * dy; tron@1245: } tron@1245: tron@1245: rubidium@9620: /** rubidium@9620: * Gets the biggest distance component (x or y) between the two given tiles. rubidium@9620: * Also known as L-Infinity-Norm. rubidium@9620: * @param t0 the start tile rubidium@9620: * @param t1 the end tile rubidium@9620: * @return the distance rubidium@9620: */ tron@1245: uint DistanceMax(TileIndex t0, TileIndex t1) tron@1245: { rubidium@5838: const uint dx = delta(TileX(t0), TileX(t1)); rubidium@5838: const uint dy = delta(TileY(t0), TileY(t1)); matthijs@1677: return dx > dy ? dx : dy; tron@1245: } tron@1245: tron@1245: rubidium@9620: /** rubidium@9620: * Gets the biggest distance component (x or y) between the two given tiles rubidium@9620: * plus the Manhattan distance, i.e. two times the biggest distance component rubidium@9620: * and once the smallest component. rubidium@9620: * @param t0 the start tile rubidium@9620: * @param t1 the end tile rubidium@9620: * @return the distance rubidium@9620: */ tron@1245: uint DistanceMaxPlusManhattan(TileIndex t0, TileIndex t1) tron@1245: { rubidium@5838: const uint dx = delta(TileX(t0), TileX(t1)); rubidium@5838: const uint dy = delta(TileY(t0), TileY(t1)); matthijs@1677: return dx > dy ? 2 * dx + dy : 2 * dy + dx; tron@1245: } tron@1245: rubidium@9620: /** rubidium@9620: * Param the minimum distance to an edge rubidium@9620: * @param tile the tile to get the distance from rubidium@9620: * @return the distance from the edge in tiles rubidium@9620: */ tron@1245: uint DistanceFromEdge(TileIndex tile) tron@1245: { tron@1245: const uint xl = TileX(tile); tron@1245: const uint yl = TileY(tile); tron@1245: const uint xh = MapSizeX() - 1 - xl; tron@1245: const uint yh = MapSizeY() - 1 - yl; tron@1245: const uint minl = xl < yl ? xl : yl; tron@1245: const uint minh = xh < yh ? xh : yh; tron@1245: return minl < minh ? minl : minh; tron@1245: } belugas@5118: belugas@5118: /** belugas@5118: * Function performing a search around a center tile and going outward, thus in circle. belugas@5118: * Although it really is a square search... belugas@5118: * Every tile will be tested by means of the callback function proc, belugas@5118: * which will determine if yes or no the given tile meets criteria of search. belugas@5118: * @param tile to start the search from belugas@5118: * @param size: number of tiles per side of the desired search area belugas@5118: * @param proc: callback testing function pointer. belugas@5118: * @param data to be passed to the callback function. Depends on the implementation belugas@6527: * @return result of the search belugas@5118: * @pre proc != NULL belugas@5118: * @pre size > 0 belugas@5118: */ belugas@5118: bool CircularTileSearch(TileIndex tile, uint size, TestTileOnSearchProc proc, uint32 data) belugas@5118: { belugas@5118: uint n, x, y; belugas@5118: DiagDirection dir; belugas@5118: belugas@5118: assert(proc != NULL); belugas@5118: assert(size > 0); belugas@5118: belugas@5118: x = TileX(tile); belugas@5118: y = TileY(tile); belugas@5118: belugas@5118: if (size % 2 == 1) { belugas@5118: /* If the length of the side is uneven, the center has to be checked belugas@5118: * separately, as the pattern of uneven sides requires to go around the center */ belugas@5118: n = 2; belugas@5118: if (proc(TileXY(x, y), data)) return true; belugas@5118: rubidium@9694: /* If tile test is not successful, get one tile down and left, belugas@5118: * ready for a test in first circle around center tile */ belugas@5118: x += _tileoffs_by_dir[DIR_W].x; belugas@5118: y += _tileoffs_by_dir[DIR_W].y; belugas@5118: } else { belugas@5118: n = 1; belugas@5118: /* To use _tileoffs_by_diagdir's order, we must relocate to belugas@5118: * another tile, as we now first go 'up', 'right', 'down', 'left' belugas@5118: * instead of 'right', 'down', 'left', 'up', which the calling belugas@5118: * function assume. */ belugas@5118: x++; belugas@5118: } belugas@5118: belugas@5118: for (; n < size; n += 2) { belugas@5118: for (dir = DIAGDIR_NE; dir < DIAGDIR_END; dir++) { belugas@5118: uint j; belugas@5118: for (j = n; j != 0; j--) { belugas@5118: if (x <= MapMaxX() && y <= MapMaxY() && ///< Is the tile within the map? rubidium@9694: proc(TileXY(x, y), data)) { ///< Is the callback successful? belugas@5118: return true; ///< then stop the search belugas@5118: } belugas@5118: belugas@5118: /* Step to the next 'neighbour' in the circular line */ belugas@5118: x += _tileoffs_by_diagdir[dir].x; belugas@5118: y += _tileoffs_by_diagdir[dir].y; belugas@5118: } belugas@5118: } belugas@5118: /* Jump to next circle to test */ belugas@5118: x += _tileoffs_by_dir[DIR_W].x; belugas@5118: y += _tileoffs_by_dir[DIR_W].y; belugas@5118: } belugas@5118: return false; belugas@5118: }