00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "debug.h"
00014 #include "core/alloc_func.hpp"
00015 #include "core/random_func.hpp"
00016 #include "tile_map.h"
00017 #include "genworld.h"
00018 #include "water_map.h"
00019
00020 uint8 _head_to_head;
00021
00022 #if defined(_MSC_VER)
00023
00024 extern "C" _CRTIMP void __cdecl _assert(void *, void *, unsigned);
00025 #endif
00026
00027 uint _map_log_x;
00028 uint _map_log_y;
00029 uint _map_size_x;
00030 uint _map_size_y;
00031 uint _map_size;
00032 uint _map_tile_mask;
00033
00034 Tile *_m = NULL;
00035 TileExtended *_me = NULL;
00036
00037 TileIndex RandomTileInArea(uint area)
00038 {
00039 uint r = Random();
00040 uint x = GB(r, 0, MapLogX());
00041 uint y = GB(r, MapLogX(), MapLogY() - FIND_FIRST_BIT(_settings_game.game_creation.head_to_head_areas)) + (area - 1) * MapSizeY() / _settings_game.game_creation.head_to_head_areas;
00042 assert(GetAreaByTile(TileXY(x, y)) == area);
00043 return TileXY(x, y);
00044 }
00045
00051 void AllocateMap(uint size_x, uint size_y)
00052 {
00053
00054
00055 if (!IsInsideMM(size_x, MIN_MAP_SIZE, MAX_MAP_SIZE + 1) ||
00056 !IsInsideMM(size_y, MIN_MAP_SIZE, MAX_MAP_SIZE + 1) ||
00057 (size_x & (size_x - 1)) != 0 ||
00058 (size_y & (size_y - 1)) != 0) {
00059 error("Invalid map size");
00060 }
00061
00062 DEBUG(map, 1, "Allocating map of size %dx%d", size_x, size_y);
00063
00064 _map_log_x = FindFirstBit(size_x);
00065 _map_log_y = FindFirstBit(size_y);
00066 _map_size_x = size_x;
00067 _map_size_y = size_y;
00068 _map_size = size_x * size_y;
00069 _map_tile_mask = _map_size - 1;
00070
00071 free(_m);
00072 free(_me);
00073
00074 _m = CallocT<Tile>(_map_size);
00075 _me = CallocT<TileExtended>(_map_size);
00076 }
00077
00078
00079 #ifdef _DEBUG
00080 TileIndex TileAdd(TileIndex tile, TileIndexDiff add,
00081 const char *exp, const char *file, int line)
00082 {
00083 int dx;
00084 int dy;
00085 uint x;
00086 uint y;
00087
00088 dx = add & MapMaxX();
00089 if (dx >= (int)MapSizeX() / 2) dx -= MapSizeX();
00090 dy = (add - dx) / (int)MapSizeX();
00091
00092 x = TileX(tile) + dx;
00093 y = TileY(tile) + dy;
00094
00095 if (x >= MapSizeX() || y >= MapSizeY()) {
00096 char buf[512];
00097
00098 snprintf(buf, lengthof(buf), "TILE_ADD(%s) when adding 0x%.4X and 0x%.4X failed",
00099 exp, tile, add);
00100 #if !defined(_MSC_VER) || defined(WINCE)
00101 fprintf(stderr, "%s:%d %s\n", file, line, buf);
00102 #else
00103 _assert(buf, (char*)file, line);
00104 #endif
00105 }
00106
00107 assert(TileXY(x, y) == TILE_MASK(tile + add));
00108
00109 return TileXY(x, y);
00110 }
00111 #endif
00112
00126 TileIndex TileAddWrap(TileIndex tile, int addx, int addy)
00127 {
00128 uint x = TileX(tile) + addx;
00129 uint y = TileY(tile) + addy;
00130
00131
00132 if ((x == 0 || y == 0) && _settings_game.construction.freeform_edges) return INVALID_TILE;
00133
00134
00135 if (x >= MapMaxX() || y >= MapMaxY()) return INVALID_TILE;
00136
00137 return TileXY(x, y);
00138 }
00139
00140 TileIndex TileAddAreaWrap(TileIndex tile, int addx, int addy)
00141 {
00142 if (_generating_world) return TileAddWrap(tile, addx, addy);
00143
00144 int x = TileX(tile) + addx;
00145 int y = TileY(tile) + addy;
00146
00147 uint8 backup_h2h = _head_to_head;
00148 _head_to_head = GetAreaByTile(tile);
00149 if (x < (int)AreaMinX() || y < (int)AreaMinY() || x >= (int)AreaMaxX() || y >= (int)AreaMaxY()) {
00150 tile = INVALID_TILE;
00151 } else {
00152 tile = TileXY(x, y);
00153 }
00154 _head_to_head = backup_h2h;
00155
00156 return tile;
00157 }
00158
00160 extern const TileIndexDiffC _tileoffs_by_diagdir[] = {
00161 {-1, 0},
00162 { 0, 1},
00163 { 1, 0},
00164 { 0, -1}
00165 };
00166
00168 extern const TileIndexDiffC _tileoffs_by_dir[] = {
00169 {-1, -1},
00170 {-1, 0},
00171 {-1, 1},
00172 { 0, 1},
00173 { 1, 1},
00174 { 1, 0},
00175 { 1, -1},
00176 { 0, -1}
00177 };
00178
00188 uint DistanceManhattan(TileIndex t0, TileIndex t1)
00189 {
00190 const uint dx = Delta(TileX(t0), TileX(t1));
00191 const uint dy = Delta(TileY(t0), TileY(t1));
00192 return dx + dy;
00193 }
00194
00195
00205 uint DistanceSquare(TileIndex t0, TileIndex t1)
00206 {
00207 const int dx = TileX(t0) - TileX(t1);
00208 const int dy = TileY(t0) - TileY(t1);
00209 return dx * dx + dy * dy;
00210 }
00211
00212
00220 uint DistanceMax(TileIndex t0, TileIndex t1)
00221 {
00222 const uint dx = Delta(TileX(t0), TileX(t1));
00223 const uint dy = Delta(TileY(t0), TileY(t1));
00224 return max(dx, dy);
00225 }
00226
00227
00236 uint DistanceMaxPlusManhattan(TileIndex t0, TileIndex t1)
00237 {
00238 const uint dx = Delta(TileX(t0), TileX(t1));
00239 const uint dy = Delta(TileY(t0), TileY(t1));
00240 return dx > dy ? 2 * dx + dy : 2 * dy + dx;
00241 }
00242
00248 uint DistanceFromEdge(TileIndex tile)
00249 {
00250 uint8 h2h_backup = _head_to_head;
00251 _head_to_head = GetAreaByTile(tile);
00252 const uint xl = TileX(tile) - AreaMinX();
00253 const uint yl = TileY(tile) - AreaMinY();
00254 const uint xh = AreaMaxX() - 1 - xl;
00255 const uint yh = AreaMaxY() - 1 - yl;
00256 const uint minl = min(xl, yl);
00257 const uint minh = min(xh, yh);
00258 _head_to_head = h2h_backup;
00259 return min(minl, minh);
00260 }
00261
00268 uint DistanceFromEdgeDir(TileIndex tile, DiagDirection dir)
00269 {
00270 switch (dir) {
00271 case DIAGDIR_NE: return TileX(tile) - (_settings_game.construction.freeform_edges ? 1 : 0);
00272 case DIAGDIR_NW: return TileY(tile) - (_settings_game.construction.freeform_edges ? 1 : 0);
00273 case DIAGDIR_SW: return MapMaxX() - TileX(tile) - 1;
00274 case DIAGDIR_SE: return MapMaxY() - TileY(tile) - 1;
00275 default: NOT_REACHED();
00276 }
00277 }
00278
00292 bool CircularTileSearch(TileIndex *tile, uint size, TestTileOnSearchProc proc, void *user_data)
00293 {
00294 assert(proc != NULL);
00295 assert(size > 0);
00296
00297 if (size % 2 == 1) {
00298
00299
00300 if (proc(*tile, user_data)) return true;
00301
00302
00303
00304 *tile = TILE_ADD(*tile, TileOffsByDir(DIR_N));
00305 return CircularTileSearch(tile, size / 2, 1, 1, proc, user_data);
00306 } else {
00307 return CircularTileSearch(tile, size / 2, 0, 0, proc, user_data);
00308 }
00309 }
00310
00330 bool CircularTileSearch(TileIndex *tile, uint radius, uint w, uint h, TestTileOnSearchProc proc, void *user_data)
00331 {
00332 assert(proc != NULL);
00333 assert(radius > 0);
00334
00335 uint x = TileX(*tile) + w + 1;
00336 uint y = TileY(*tile);
00337
00338 const uint extent[DIAGDIR_END] = { w, h, w, h };
00339
00340 for (uint n = 0; n < radius; n++) {
00341 for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
00342
00343 for (uint j = extent[dir] + n * 2 + 1; j != 0; j--) {
00344 if (x < MapSizeX() && y < MapSizeY()) {
00345 TileIndex t = TileXY(x, y);
00346
00347 if (proc(t, user_data)) {
00348
00349 *tile = t;
00350 return true;
00351 }
00352 }
00353
00354
00355 x += _tileoffs_by_diagdir[dir].x;
00356 y += _tileoffs_by_diagdir[dir].y;
00357 }
00358 }
00359
00360 x += _tileoffs_by_dir[DIR_W].x;
00361 y += _tileoffs_by_dir[DIR_W].y;
00362 }
00363
00364 *tile = INVALID_TILE;
00365 return false;
00366 }
00367
00374 uint GetClosestWaterDistance(TileIndex tile, bool water)
00375 {
00376 if (HasTileWaterGround(tile) == water) return 0;
00377
00378 uint max_dist = water ? 0x7F : 0x200;
00379
00380 int x = TileX(tile);
00381 int y = TileY(tile);
00382
00383 uint max_x = MapMaxX();
00384 uint max_y = MapMaxY();
00385 uint min_xy = _settings_game.construction.freeform_edges ? 1 : 0;
00386
00387
00388 for (uint dist = 1; dist < max_dist; dist++) {
00389
00390 y--;
00391
00392
00393 for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
00394 static const int8 ddx[DIAGDIR_END] = { -1, 1, 1, -1};
00395 static const int8 ddy[DIAGDIR_END] = { 1, 1, -1, -1};
00396
00397 int dx = ddx[dir];
00398 int dy = ddy[dir];
00399
00400
00401 for (uint a = 0; a < dist; a++) {
00402
00403 if (IsInsideMM(x, min_xy, max_x) && IsInsideMM(y, min_xy, max_y)) {
00404 TileIndex t = TileXY(x, y);
00405 if (HasTileWaterGround(t) == water) return dist;
00406 }
00407 x += dx;
00408 y += dy;
00409 }
00410 }
00411 }
00412
00413 if (!water) {
00414
00415 for (TileIndex t = 0; t < MapSize(); t++) {
00416 if (!IsTileType(t, MP_VOID) && !IsTileType(t, MP_WATER)) return 0x1FF;
00417 }
00418 }
00419
00420 return max_dist;
00421 }