# map.cpp

Go to the documentation of this file.

    00001 /* $Id: map.cpp 17693 2009-10-04 17:16:41Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "debug.h"
    00014 #include "core/alloc_func.hpp"
    00015 #include "core/math_func.hpp"
    00016 #include "tile_map.h"
    00017
    00018 #if defined(_MSC_VER)
    00019 /* Why the hell is that not in all MSVC headers?? */
    00020 extern "C" _CRTIMP void __cdecl _assert(void *, void *, unsigned);
    00021 #endif
    00022
    00023 uint _map_log_x;
    00024 uint _map_log_y;
    00025 uint _map_size_x;
    00026 uint _map_size_y;
    00027 uint _map_size;
    00028 uint _map_tile_mask;
    00029
    00030 Tile *_m = NULL;
    00031 TileExtended *_me = NULL;
    00032
    00033
    00039 void AllocateMap(uint size_x, uint size_y)
    00040 {
    00041   /* Make sure that the map size is within the limits and that
    00042    * size of both axes is a power of 2. */
    00043   if (!IsInsideMM(size_x, MIN_MAP_SIZE, MAX_MAP_SIZE + 1) ||
    00044       !IsInsideMM(size_y, MIN_MAP_SIZE, MAX_MAP_SIZE + 1) ||
    00045       (size_x & (size_x - 1)) != 0 ||
    00046       (size_y & (size_y - 1)) != 0)
    00047     error("Invalid map size");
    00048
    00049   DEBUG(map, 1, "Allocating map of size %dx%d", size_x, size_y);
    00050
    00051   _map_log_x = FindFirstBit(size_x);
    00052   _map_log_y = FindFirstBit(size_y);
    00053   _map_size_x = size_x;
    00054   _map_size_y = size_y;
    00055   _map_size = size_x * size_y;
    00056   _map_tile_mask = _map_size - 1;
    00057
    00058   free(_m);
    00059   free(_me);
    00060
    00061   _m = CallocT<Tile>(_map_size);
    00062   _me = CallocT<TileExtended>(_map_size);
    00063 }
    00064
    00065
    00066 #ifdef _DEBUG
    00067 TileIndex TileAdd(TileIndex tile, TileIndexDiff add,
    00068   const char *exp, const char *file, int line)
    00069 {
    00070   int dx;
    00071   int dy;
    00072   uint x;
    00073   uint y;
    00074
    00075   dx = add & MapMaxX();
    00076   if (dx >= (int)MapSizeX() / 2) dx -= MapSizeX();
    00077   dy = (add - dx) / (int)MapSizeX();
    00078
    00079   x = TileX(tile) + dx;
    00080   y = TileY(tile) + dy;
    00081
    00082   if (x >= MapSizeX() || y >= MapSizeY()) {
    00083     char buf[512];
    00084
    00085     snprintf(buf, lengthof(buf), "TILE_ADD(%s) when adding 0x%.4X and 0x%.4X failed",
    00086       exp, tile, add);
    00087 #if !defined(_MSC_VER) || defined(WINCE)
    00088     fprintf(stderr, "%s:%d %s\n", file, line, buf);
    00089 #else
    00090     _assert(buf, (char*)file, line);
    00091 #endif
    00092   }
    00093
    00094   assert(TileXY(x, y) == TILE_MASK(tile + add));
    00095
    00096   return TileXY(x, y);
    00097 }
    00098 #endif
    00099
    00113 TileIndex TileAddWrap(TileIndex tile, int addx, int addy)
    00114 {
    00115   uint x = TileX(tile) + addx;
    00116   uint y = TileY(tile) + addy;
    00117
    00118   /* Are we about to wrap? */
    00119   if (x < MapMaxX() && y < MapMaxY())
    00120     return tile + TileDiffXY(addx, addy);
    00121
    00122   return INVALID_TILE;
    00123 }
    00124
    00126 extern const TileIndexDiffC _tileoffs_by_diagdir[] = {
    00127   {-1,  0},
    00128   { 0,  1},
    00129   { 1,  0},
    00130   { 0, -1}
    00131 };
    00132
    00134 extern const TileIndexDiffC _tileoffs_by_dir[] = {
    00135   {-1, -1},
    00136   {-1,  0},
    00137   {-1,  1},
    00138   { 0,  1},
    00139   { 1,  1},
    00140   { 1,  0},
    00141   { 1, -1},
    00142   { 0, -1}
    00143 };
    00144
    00154 uint DistanceManhattan(TileIndex t0, TileIndex t1)
    00155 {
    00156   const uint dx = Delta(TileX(t0), TileX(t1));
    00157   const uint dy = Delta(TileY(t0), TileY(t1));
    00158   return dx + dy;
    00159 }
    00160
    00161
    00171 uint DistanceSquare(TileIndex t0, TileIndex t1)
    00172 {
    00173   const int dx = TileX(t0) - TileX(t1);
    00174   const int dy = TileY(t0) - TileY(t1);
    00175   return dx * dx + dy * dy;
    00176 }
    00177
    00178
    00186 uint DistanceMax(TileIndex t0, TileIndex t1)
    00187 {
    00188   const uint dx = Delta(TileX(t0), TileX(t1));
    00189   const uint dy = Delta(TileY(t0), TileY(t1));
    00190   return max(dx, dy);
    00191 }
    00192
    00193
    00202 uint DistanceMaxPlusManhattan(TileIndex t0, TileIndex t1)
    00203 {
    00204   const uint dx = Delta(TileX(t0), TileX(t1));
    00205   const uint dy = Delta(TileY(t0), TileY(t1));
    00206   return dx > dy ? 2 * dx + dy : 2 * dy + dx;
    00207 }
    00208
    00214 uint DistanceFromEdge(TileIndex tile)
    00215 {
    00216   const uint xl = TileX(tile);
    00217   const uint yl = TileY(tile);
    00218   const uint xh = MapSizeX() - 1 - xl;
    00219   const uint yh = MapSizeY() - 1 - yl;
    00220   const uint minl = min(xl, yl);
    00221   const uint minh = min(xh, yh);
    00222   return min(minl, minh);
    00223 }
    00224
    00238 bool CircularTileSearch(TileIndex *tile, uint size, TestTileOnSearchProc proc, void *user_data)
    00239 {
    00240   assert(proc != NULL);
    00241   assert(size > 0);
    00242
    00243   if (size % 2 == 1) {
    00244     /* If the length of the side is uneven, the center has to be checked
    00245      * separately, as the pattern of uneven sides requires to go around the center */
    00246     if (proc(*tile, user_data)) return true;
    00247
    00248     /* If tile test is not successful, get one tile down and left,
    00249      * ready for a test in first circle around center tile */
    00250     *tile = TILE_ADD(*tile, TileOffsByDir(DIR_W));
    00251     return CircularTileSearch(tile, size / 2, 1, 1, proc, user_data);
    00252   } else {
    00253     return CircularTileSearch(tile, size / 2, 0, 0, proc, user_data);
    00254   }
    00255 }
    00256
    00275 bool CircularTileSearch(TileIndex *tile, uint radius, uint w, uint h, TestTileOnSearchProc proc, void *user_data)
    00276 {
    00277   assert(proc != NULL);
    00278   assert(radius > 0);
    00279
    00280   uint x = TileX(*tile) + w + 1;
    00281   uint y = TileY(*tile);
    00282
    00283   const uint extent[DIAGDIR_END] = { w, h, w, h };
    00284
    00285   for (uint n = 0; n < radius; n++) {
    00286     for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
    00287       /* Is the tile within the map? */
    00288       for (uint j = extent[dir] + n * 2 + 1; j != 0; j--) {
    00289         if (x < MapSizeX() && y < MapSizeY()) {
    00290           TileIndex t = TileXY(x, y);
    00291           /* Is the callback successful? */
    00292           if (proc(t, user_data)) {
    00293             /* Stop the search */
    00294             *tile = t;
    00295             return true;
    00296           }
    00297         }
    00298
    00299         /* Step to the next 'neighbour' in the circular line */
    00300         x += _tileoffs_by_diagdir[dir].x;
    00301         y += _tileoffs_by_diagdir[dir].y;
    00302       }
    00303     }
    00304     /* Jump to next circle to test */
    00305     x += _tileoffs_by_dir[DIR_W].x;
    00306     y += _tileoffs_by_dir[DIR_W].y;
    00307   }
    00308
    00309   *tile = INVALID_TILE;
    00310   return false;
    00311 }
    00312
    00320 uint GetClosestWaterDistance(TileIndex tile, bool water)
    00321 {
    00322   if (IsTileType(tile, MP_WATER) == water) return 0;
    00323
    00324   uint max_dist = water ? 0x7F : 0x200;
    00325
    00326   int x = TileX(tile);
    00327   int y = TileY(tile);
    00328
    00329   uint max_x = MapMaxX();
    00330   uint max_y = MapMaxY();
    00331   uint min_xy = _settings_game.construction.freeform_edges ? 1 : 0;
    00332
    00333   /* go in a 'spiral' with increasing manhattan distance in each iteration */
    00334   for (uint dist = 1; dist < max_dist; dist++) {
    00335     /* next 'diameter' */
    00336     y--;
    00337
    00338     /* going counter-clockwise around this square */
    00339     for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
    00340       static const int8 ddx[DIAGDIR_END] = { -1,  1,  1, -1};
    00341       static const int8 ddy[DIAGDIR_END] = {  1,  1, -1, -1};
    00342
    00343       int dx = ddx[dir];
    00344       int dy = ddy[dir];
    00345
    00346       /* each side of this square has length 'dist' */
    00347       for (uint a = 0; a < dist; a++) {
    00348         /* MP_VOID tiles are not checked (interval is [min; max) for IsInsideMM())*/
    00349         if (IsInsideMM(x, min_xy, max_x) && IsInsideMM(y, min_xy, max_y)) {
    00350           TileIndex t = TileXY(x, y);
    00351           if (IsTileType(t, MP_WATER) == water) return dist;
    00352         }
    00353         x += dx;
    00354         y += dy;
    00355       }
    00356     }
    00357   }
    00358
    00359   if (!water) {
    00360     /* no land found - is this a water-only map? */
    00361     for (TileIndex t = 0; t < MapSize(); t++) {
    00362       if (!IsTileType(t, MP_VOID) && !IsTileType(t, MP_WATER)) return 0x1FF;
    00363     }
    00364   }
    00365
    00366   return max_dist;
    00367 }