tron@679: #include "stdafx.h" tron@679: #include "ttd.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@1218: assert(log_x <= 15 && log_y <= 15); tron@1218: tron@1218: _map_log_x = log_x; tron@1218: _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: tron@909: const TileIndexDiffC _tileoffs_by_dir[] = { tron@909: {-1, 0}, tron@909: { 0, 1}, tron@909: { 1, 0}, tron@909: { 0, -1} tron@900: };