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" belugas@5118: #include "direction.h" 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?? */ Darkvater@2482: _CRTIMP void __cdecl _assert(void *, void *, unsigned); Darkvater@2482: #endif Darkvater@2482: 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: 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); peter1138@3717: _m = calloc(_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: 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: 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: { tron@2548: uint x = TileX(tile) + addx; tron@2548: uint 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: Darkvater@4559: const TileIndexDiffC _tileoffs_by_diagdir[] = { Darkvater@4559: {-1, 0}, // DIAGDIR_NE Darkvater@4559: { 0, 1}, // DIAGDIR_SE Darkvater@4559: { 1, 0}, // DIAGDIR_SW Darkvater@4559: { 0, -1} // DIAGDIR_NW Darkvater@4559: }; Darkvater@4559: matthijs@1247: const TileIndexDiffC _tileoffs_by_dir[] = { Darkvater@4559: {-1, -1}, // DIR_N Darkvater@4559: {-1, 0}, // DIR_NE Darkvater@4559: {-1, 1}, // DIR_E Darkvater@4559: { 0, 1}, // DIR_SE Darkvater@4559: { 1, 1}, // DIR_S Darkvater@4559: { 1, 0}, // DIR_SW Darkvater@4559: { 1, -1}, // DIR_W Darkvater@4559: { 0, -1} // DIR_NW 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: 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@5118: * @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: belugas@5118: /* If tile test is not successfull, 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? belugas@5118: proc(TileXY(x, y), data)) { ///< Is the callback successfulll? 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: }