tron@679: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@1299: #include "debug.h" tron@1218: #include "functions.h" tron@679: #include "map.h" tron@679: tron@1218: uint _map_log_x; tron@1218: uint _map_log_y; tron@689: tron@1218: byte *_map_type_and_height = NULL; tron@1218: byte *_map_owner = NULL; tron@1218: uint16 *_map2 = NULL; tron@1218: byte *_map3_lo = NULL; tron@1218: byte *_map3_hi = NULL; tron@1218: byte *_map5 = NULL; tron@1218: byte *_map_extra_bits = NULL; tron@863: tron@1218: tron@1218: void InitMap(uint log_x, uint log_y) tron@1218: { tron@1218: uint map_size; tron@1218: tron@1244: if (log_x < 6 || log_x > 11 || log_y < 6 || log_y > 11) tron@1244: error("Invalid map size"); tron@1218: tron@1233: DEBUG(map, 1)("Allocating map of size %dx%d", log_x, log_y); tron@1233: Darkvater@1410: // XXX - MSVC6 volatile workaround Darkvater@1410: *(volatile uint*)&_map_log_x = log_x; Darkvater@1410: *(volatile uint*)&_map_log_y = log_y; tron@1218: tron@1218: map_size = MapSize(); tron@1218: tron@1218: _map_type_and_height = tron@1218: realloc(_map_type_and_height, map_size * sizeof(_map_type_and_height[0])); tron@1218: _map_owner = realloc(_map_owner, map_size * sizeof(_map_owner[0])); tron@1218: _map2 = realloc(_map2, map_size * sizeof(_map2[0])); tron@1218: _map3_lo = realloc(_map3_lo, map_size * sizeof(_map3_lo[0])); tron@1218: _map3_hi = realloc(_map3_hi, map_size * sizeof(_map3_hi[0])); tron@1218: _map5 = realloc(_map5, map_size * sizeof(_map5[0])); tron@1218: _map_extra_bits = tron@1218: realloc(_map_extra_bits, map_size * sizeof(_map_extra_bits[0] / 4)); tron@1218: tron@1218: // XXX TODO handle memory shortage more gracefully tron@1218: if (_map_type_and_height == NULL || tron@1218: _map_owner == NULL || tron@1218: _map2 == NULL || tron@1218: _map3_lo == NULL || tron@1218: _map3_hi == NULL || tron@1218: _map5 == NULL || tron@1218: _map_extra_bits == NULL) tron@1218: 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@955: assert(TILE_XY(x,y) == TILE_MASK(tile + add)); tron@955: tron@955: return TILE_XY(x,y); tron@955: } tron@955: #endif tron@955: tron@955: tron@1202: uint ScaleByMapSize(uint n) tron@1202: { tron@1202: int shift = (int)MapLogX() - 8 + (int)MapLogY() - 8; tron@1202: tron@1202: if (shift < 0) tron@1202: return (n + (1 << -shift) - 1) >> -shift; tron@1202: else tron@1202: return n << shift; tron@1202: } tron@1202: tron@1202: tron@1202: uint ScaleByMapSize1D(uint n) tron@1202: { tron@1202: int shift = ((int)MapLogX() - 8 + (int)MapLogY() - 8) / 2; tron@1202: tron@1202: if (shift < 0) tron@1202: return (n + (1 << -shift) - 1) >> -shift; tron@1202: else tron@1202: return n << shift; 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, matthijs@1247: // the result will be tile + TILE_XY(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? matthijs@1247: if (x < MapMaxX() && y < MapMaxY()) matthijs@1247: return tile + TILE_XY(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: