map.cpp

Go to the documentation of this file.
00001 /* $Id: map.cpp 18937 2010-01-28 19:27:10Z yexo $ */
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   /* Disallow void tiles at the north border. */
00119   if (_settings_game.construction.freeform_edges && (x == 0 || y == 0)) return INVALID_TILE;
00120 
00121   /* Are we about to wrap? */
00122   if (x < MapMaxX() && y < MapMaxY()) return tile + TileDiffXY(addx, addy);
00123 
00124   return INVALID_TILE;
00125 }
00126 
00128 extern const TileIndexDiffC _tileoffs_by_diagdir[] = {
00129   {-1,  0}, 
00130   { 0,  1}, 
00131   { 1,  0}, 
00132   { 0, -1}  
00133 };
00134 
00136 extern const TileIndexDiffC _tileoffs_by_dir[] = {
00137   {-1, -1}, 
00138   {-1,  0}, 
00139   {-1,  1}, 
00140   { 0,  1}, 
00141   { 1,  1}, 
00142   { 1,  0}, 
00143   { 1, -1}, 
00144   { 0, -1}  
00145 };
00146 
00156 uint DistanceManhattan(TileIndex t0, TileIndex t1)
00157 {
00158   const uint dx = Delta(TileX(t0), TileX(t1));
00159   const uint dy = Delta(TileY(t0), TileY(t1));
00160   return dx + dy;
00161 }
00162 
00163 
00173 uint DistanceSquare(TileIndex t0, TileIndex t1)
00174 {
00175   const int dx = TileX(t0) - TileX(t1);
00176   const int dy = TileY(t0) - TileY(t1);
00177   return dx * dx + dy * dy;
00178 }
00179 
00180 
00188 uint DistanceMax(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 max(dx, dy);
00193 }
00194 
00195 
00204 uint DistanceMaxPlusManhattan(TileIndex t0, TileIndex t1)
00205 {
00206   const uint dx = Delta(TileX(t0), TileX(t1));
00207   const uint dy = Delta(TileY(t0), TileY(t1));
00208   return dx > dy ? 2 * dx + dy : 2 * dy + dx;
00209 }
00210 
00216 uint DistanceFromEdge(TileIndex tile)
00217 {
00218   const uint xl = TileX(tile);
00219   const uint yl = TileY(tile);
00220   const uint xh = MapSizeX() - 1 - xl;
00221   const uint yh = MapSizeY() - 1 - yl;
00222   const uint minl = min(xl, yl);
00223   const uint minh = min(xh, yh);
00224   return min(minl, minh);
00225 }
00226 
00240 bool CircularTileSearch(TileIndex *tile, uint size, TestTileOnSearchProc proc, void *user_data)
00241 {
00242   assert(proc != NULL);
00243   assert(size > 0);
00244 
00245   if (size % 2 == 1) {
00246     /* If the length of the side is uneven, the center has to be checked
00247      * separately, as the pattern of uneven sides requires to go around the center */
00248     if (proc(*tile, user_data)) return true;
00249 
00250     /* If tile test is not successful, get one tile up,
00251      * ready for a test in first circle around center tile */
00252     *tile = TILE_ADD(*tile, TileOffsByDir(DIR_N));
00253     return CircularTileSearch(tile, size / 2, 1, 1, proc, user_data);
00254   } else {
00255     return CircularTileSearch(tile, size / 2, 0, 0, proc, user_data);
00256   }
00257 }
00258 
00278 bool CircularTileSearch(TileIndex *tile, uint radius, uint w, uint h, TestTileOnSearchProc proc, void *user_data)
00279 {
00280   assert(proc != NULL);
00281   assert(radius > 0);
00282 
00283   uint x = TileX(*tile) + w + 1;
00284   uint y = TileY(*tile);
00285 
00286   const uint extent[DIAGDIR_END] = { w, h, w, h };
00287 
00288   for (uint n = 0; n < radius; n++) {
00289     for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
00290       /* Is the tile within the map? */
00291       for (uint j = extent[dir] + n * 2 + 1; j != 0; j--) {
00292         if (x < MapSizeX() && y < MapSizeY()) {
00293           TileIndex t = TileXY(x, y);
00294           /* Is the callback successful? */
00295           if (proc(t, user_data)) {
00296             /* Stop the search */
00297             *tile = t;
00298             return true;
00299           }
00300         }
00301 
00302         /* Step to the next 'neighbour' in the circular line */
00303         x += _tileoffs_by_diagdir[dir].x;
00304         y += _tileoffs_by_diagdir[dir].y;
00305       }
00306     }
00307     /* Jump to next circle to test */
00308     x += _tileoffs_by_dir[DIR_W].x;
00309     y += _tileoffs_by_dir[DIR_W].y;
00310   }
00311 
00312   *tile = INVALID_TILE;
00313   return false;
00314 }
00315 
00323 uint GetClosestWaterDistance(TileIndex tile, bool water)
00324 {
00325   if (IsTileType(tile, MP_WATER) == water) return 0;
00326 
00327   uint max_dist = water ? 0x7F : 0x200;
00328 
00329   int x = TileX(tile);
00330   int y = TileY(tile);
00331 
00332   uint max_x = MapMaxX();
00333   uint max_y = MapMaxY();
00334   uint min_xy = _settings_game.construction.freeform_edges ? 1 : 0;
00335 
00336   /* go in a 'spiral' with increasing manhattan distance in each iteration */
00337   for (uint dist = 1; dist < max_dist; dist++) {
00338     /* next 'diameter' */
00339     y--;
00340 
00341     /* going counter-clockwise around this square */
00342     for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
00343       static const int8 ddx[DIAGDIR_END] = { -1,  1,  1, -1};
00344       static const int8 ddy[DIAGDIR_END] = {  1,  1, -1, -1};
00345 
00346       int dx = ddx[dir];
00347       int dy = ddy[dir];
00348 
00349       /* each side of this square has length 'dist' */
00350       for (uint a = 0; a < dist; a++) {
00351         /* MP_VOID tiles are not checked (interval is [min; max) for IsInsideMM())*/
00352         if (IsInsideMM(x, min_xy, max_x) && IsInsideMM(y, min_xy, max_y)) {
00353           TileIndex t = TileXY(x, y);
00354           if (IsTileType(t, MP_WATER) == water) return dist;
00355         }
00356         x += dx;
00357         y += dy;
00358       }
00359     }
00360   }
00361 
00362   if (!water) {
00363     /* no land found - is this a water-only map? */
00364     for (TileIndex t = 0; t < MapSize(); t++) {
00365       if (!IsTileType(t, MP_VOID) && !IsTileType(t, MP_WATER)) return 0x1FF;
00366     }
00367   }
00368 
00369   return max_dist;
00370 }

Generated on Wed Mar 3 23:32:22 2010 for OpenTTD by  doxygen 1.6.1