tron@2186: /* $Id$ */ tron@2186: 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" tron@679: tron@1218: uint _map_log_x; ludde@2051: uint _map_size_x; ludde@2051: uint _map_size_y; ludde@2051: uint _map_tile_mask; ludde@2051: uint _map_size; tron@689: tron@2049: Tile* _m = NULL; tron@863: tron@1218: ludde@2051: void AllocateMap(uint size_x, uint size_y) tron@1218: { ludde@2051: // Make sure that the map size is within the limits and that ludde@2051: // 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 || ludde@2051: (size_x&(size_x-1)) != 0 || ludde@2051: (size_y&(size_y-1)) != 0) tron@1244: error("Invalid map size"); tron@1218: ludde@2051: 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: ludde@2051: // free/malloc uses less memory than realloc. tron@2049: free(_m); ludde@2051: _m = malloc(_map_size * sizeof(*_m)); tron@1218: tron@1218: // XXX TODO handle memory shortage more gracefully tron@2049: if (_m == 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: tron@955: sprintf(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: tron@1981: assert(TileXY(x,y) == TILE_MASK(tile + add)); tron@955: tron@1981: return TileXY(x,y); tron@955: } tron@955: #endif tron@955: tron@955: tron@1202: uint ScaleByMapSize(uint n) tron@1202: { ludde@2051: // First shift by 12 to prevent integer overflow for large values of n. ludde@2051: // >>12 is safe since the min mapsize is 64x64 ludde@2051: // Add (1<<4)-1 to round upwards. ludde@2051: return (n * (MapSize() >> 12) + (1<<4) - 1) >> 4; tron@1202: } tron@1202: tron@1202: ludde@2051: // Scale relative to the circumference of the map tron@1202: uint ScaleByMapSize1D(uint n) tron@1202: { ludde@2051: // Normal circumference for the X+Y is 256+256 = 1<<9 ludde@2051: // Note, not actually taking the full circumference into account, ludde@2051: // just half of it. ludde@2051: // (1<<9) - 1 is there to scale upwards. ludde@2051: return (n * (MapSizeX() + MapSizeY()) + (1<<9) - 1) >> 9; tron@1202: } tron@1202: tron@1202: matthijs@1247: // This function checks if we add addx/addy to tile, if we matthijs@1247: // do wrap around the edges. For example, tile = (10,2) and matthijs@1247: // addx = +3 and addy = -4. This function will now return matthijs@1247: // INVALID_TILE, because the y is wrapped. This is needed in matthijs@1247: // for example, farmland. When the tile is not wrapped, tron@1981: // the result will be tile + TileDiffXY(addx, addy) matthijs@1247: uint TileAddWrap(TileIndex tile, int addx, int addy) matthijs@1247: { matthijs@1247: uint x, y; matthijs@1247: x = TileX(tile) + addx; matthijs@1247: y = TileY(tile) + addy; matthijs@1247: matthijs@1247: // 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: matthijs@1247: const TileIndexDiffC _tileoffs_by_dir[] = { matthijs@1247: {-1, 0}, matthijs@1247: { 0, 1}, matthijs@1247: { 1, 0}, matthijs@1247: { 0, -1} matthijs@1247: }; matthijs@1247: tron@1245: uint DistanceManhattan(TileIndex t0, TileIndex t1) tron@1245: { matthijs@1677: const uint dx = abs(TileX(t0) - TileX(t1)); matthijs@1677: const uint dy = abs(TileY(t0) - TileY(t1)); matthijs@1677: return dx + dy; tron@1245: } tron@1245: tron@1245: 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: tron@1245: uint DistanceMax(TileIndex t0, TileIndex t1) tron@1245: { matthijs@1677: const uint dx = abs(TileX(t0) - TileX(t1)); matthijs@1677: const uint dy = abs(TileY(t0) - TileY(t1)); matthijs@1677: return dx > dy ? dx : dy; tron@1245: } tron@1245: tron@1245: tron@1245: uint DistanceMaxPlusManhattan(TileIndex t0, TileIndex t1) tron@1245: { matthijs@1677: const uint dx = abs(TileX(t0) - TileX(t1)); matthijs@1677: const uint dy = abs(TileY(t0) - TileY(t1)); matthijs@1677: return dx > dy ? 2 * dx + dy : 2 * dy + dx; tron@1245: } tron@1245: matthijs@1677: uint DistanceTrack(TileIndex t0, TileIndex t1) matthijs@1677: { matthijs@1677: const uint dx = abs(TileX(t0) - TileX(t1)); matthijs@1677: const uint dy = abs(TileY(t0) - TileY(t1)); matthijs@1677: matthijs@1677: const uint straightTracks = 2 * min(dx, dy); /* The number of straight (not full length) tracks */ matthijs@1677: /* OPTIMISATION: matthijs@1677: * Original: diagTracks = max(dx, dy) - min(dx,dy); matthijs@1677: * Proof: matthijs@1677: * (dx-dy) - straightTracks == (min + max) - straightTracks = min + // max - 2 * min = max - min */ matthijs@1677: const uint diagTracks = dx + dy - straightTracks; /* The number of diagonal (full tile length) tracks. */ matthijs@1677: matthijs@1677: return diagTracks + straightTracks * STRAIGHT_TRACK_LENGTH; matthijs@1677: } tron@1245: 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: } tron@1245: