map.cpp

Go to the documentation of this file.
00001 /* $Id: map.cpp 21995 2011-02-06 14:33:04Z frosch $ */
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 "tile_map.h"
00016 #include "water_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 
00050   DEBUG(map, 1, "Allocating map of size %dx%d", size_x, size_y);
00051 
00052   _map_log_x = FindFirstBit(size_x);
00053   _map_log_y = FindFirstBit(size_y);
00054   _map_size_x = size_x;
00055   _map_size_y = size_y;
00056   _map_size = size_x * size_y;
00057   _map_tile_mask = _map_size - 1;
00058 
00059   free(_m);
00060   free(_me);
00061 
00062   _m = CallocT<Tile>(_map_size);
00063   _me = CallocT<TileExtended>(_map_size);
00064 }
00065 
00066 
00067 #ifdef _DEBUG
00068 TileIndex TileAdd(TileIndex tile, TileIndexDiff add,
00069   const char *exp, const char *file, int line)
00070 {
00071   int dx;
00072   int dy;
00073   uint x;
00074   uint y;
00075 
00076   dx = add & MapMaxX();
00077   if (dx >= (int)MapSizeX() / 2) dx -= MapSizeX();
00078   dy = (add - dx) / (int)MapSizeX();
00079 
00080   x = TileX(tile) + dx;
00081   y = TileY(tile) + dy;
00082 
00083   if (x >= MapSizeX() || y >= MapSizeY()) {
00084     char buf[512];
00085 
00086     snprintf(buf, lengthof(buf), "TILE_ADD(%s) when adding 0x%.4X and 0x%.4X failed",
00087       exp, tile, add);
00088 #if !defined(_MSC_VER) || defined(WINCE)
00089     fprintf(stderr, "%s:%d %s\n", file, line, buf);
00090 #else
00091     _assert(buf, (char*)file, line);
00092 #endif
00093   }
00094 
00095   assert(TileXY(x, y) == TILE_MASK(tile + add));
00096 
00097   return TileXY(x, y);
00098 }
00099 #endif
00100 
00114 TileIndex TileAddWrap(TileIndex tile, int addx, int addy)
00115 {
00116   uint x = TileX(tile) + addx;
00117   uint y = TileY(tile) + addy;
00118 
00119   /* Disallow void tiles at the north border. */
00120   if (_settings_game.construction.freeform_edges && (x == 0 || y == 0)) return INVALID_TILE;
00121 
00122   /* Are we about to wrap? */
00123   if (x < MapMaxX() && y < MapMaxY()) return tile + TileDiffXY(addx, addy);
00124 
00125   return INVALID_TILE;
00126 }
00127 
00129 extern const TileIndexDiffC _tileoffs_by_diagdir[] = {
00130   {-1,  0}, 
00131   { 0,  1}, 
00132   { 1,  0}, 
00133   { 0, -1}  
00134 };
00135 
00137 extern const TileIndexDiffC _tileoffs_by_dir[] = {
00138   {-1, -1}, 
00139   {-1,  0}, 
00140   {-1,  1}, 
00141   { 0,  1}, 
00142   { 1,  1}, 
00143   { 1,  0}, 
00144   { 1, -1}, 
00145   { 0, -1}  
00146 };
00147 
00157 uint DistanceManhattan(TileIndex t0, TileIndex t1)
00158 {
00159   const uint dx = Delta(TileX(t0), TileX(t1));
00160   const uint dy = Delta(TileY(t0), TileY(t1));
00161   return dx + dy;
00162 }
00163 
00164 
00174 uint DistanceSquare(TileIndex t0, TileIndex t1)
00175 {
00176   const int dx = TileX(t0) - TileX(t1);
00177   const int dy = TileY(t0) - TileY(t1);
00178   return dx * dx + dy * dy;
00179 }
00180 
00181 
00189 uint DistanceMax(TileIndex t0, TileIndex t1)
00190 {
00191   const uint dx = Delta(TileX(t0), TileX(t1));
00192   const uint dy = Delta(TileY(t0), TileY(t1));
00193   return max(dx, dy);
00194 }
00195 
00196 
00205 uint DistanceMaxPlusManhattan(TileIndex t0, TileIndex t1)
00206 {
00207   const uint dx = Delta(TileX(t0), TileX(t1));
00208   const uint dy = Delta(TileY(t0), TileY(t1));
00209   return dx > dy ? 2 * dx + dy : 2 * dy + dx;
00210 }
00211 
00217 uint DistanceFromEdge(TileIndex tile)
00218 {
00219   const uint xl = TileX(tile);
00220   const uint yl = TileY(tile);
00221   const uint xh = MapSizeX() - 1 - xl;
00222   const uint yh = MapSizeY() - 1 - yl;
00223   const uint minl = min(xl, yl);
00224   const uint minh = min(xh, yh);
00225   return min(minl, minh);
00226 }
00227 
00234 uint DistanceFromEdgeDir(TileIndex tile, DiagDirection dir)
00235 {
00236   switch (dir) {
00237     case DIAGDIR_NE: return             TileX(tile) - (_settings_game.construction.freeform_edges ? 1 : 0);
00238     case DIAGDIR_NW: return             TileY(tile) - (_settings_game.construction.freeform_edges ? 1 : 0);
00239     case DIAGDIR_SW: return MapMaxX() - TileX(tile) - 1;
00240     case DIAGDIR_SE: return MapMaxY() - TileY(tile) - 1;
00241     default: NOT_REACHED();
00242   }
00243 }
00244 
00258 bool CircularTileSearch(TileIndex *tile, uint size, TestTileOnSearchProc proc, void *user_data)
00259 {
00260   assert(proc != NULL);
00261   assert(size > 0);
00262 
00263   if (size % 2 == 1) {
00264     /* If the length of the side is uneven, the center has to be checked
00265      * separately, as the pattern of uneven sides requires to go around the center */
00266     if (proc(*tile, user_data)) return true;
00267 
00268     /* If tile test is not successful, get one tile up,
00269      * ready for a test in first circle around center tile */
00270     *tile = TILE_ADD(*tile, TileOffsByDir(DIR_N));
00271     return CircularTileSearch(tile, size / 2, 1, 1, proc, user_data);
00272   } else {
00273     return CircularTileSearch(tile, size / 2, 0, 0, proc, user_data);
00274   }
00275 }
00276 
00296 bool CircularTileSearch(TileIndex *tile, uint radius, uint w, uint h, TestTileOnSearchProc proc, void *user_data)
00297 {
00298   assert(proc != NULL);
00299   assert(radius > 0);
00300 
00301   uint x = TileX(*tile) + w + 1;
00302   uint y = TileY(*tile);
00303 
00304   const uint extent[DIAGDIR_END] = { w, h, w, h };
00305 
00306   for (uint n = 0; n < radius; n++) {
00307     for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
00308       /* Is the tile within the map? */
00309       for (uint j = extent[dir] + n * 2 + 1; j != 0; j--) {
00310         if (x < MapSizeX() && y < MapSizeY()) {
00311           TileIndex t = TileXY(x, y);
00312           /* Is the callback successful? */
00313           if (proc(t, user_data)) {
00314             /* Stop the search */
00315             *tile = t;
00316             return true;
00317           }
00318         }
00319 
00320         /* Step to the next 'neighbour' in the circular line */
00321         x += _tileoffs_by_diagdir[dir].x;
00322         y += _tileoffs_by_diagdir[dir].y;
00323       }
00324     }
00325     /* Jump to next circle to test */
00326     x += _tileoffs_by_dir[DIR_W].x;
00327     y += _tileoffs_by_dir[DIR_W].y;
00328   }
00329 
00330   *tile = INVALID_TILE;
00331   return false;
00332 }
00333 
00340 uint GetClosestWaterDistance(TileIndex tile, bool water)
00341 {
00342   if (HasTileWaterGround(tile) == water) return 0;
00343 
00344   uint max_dist = water ? 0x7F : 0x200;
00345 
00346   int x = TileX(tile);
00347   int y = TileY(tile);
00348 
00349   uint max_x = MapMaxX();
00350   uint max_y = MapMaxY();
00351   uint min_xy = _settings_game.construction.freeform_edges ? 1 : 0;
00352 
00353   /* go in a 'spiral' with increasing manhattan distance in each iteration */
00354   for (uint dist = 1; dist < max_dist; dist++) {
00355     /* next 'diameter' */
00356     y--;
00357 
00358     /* going counter-clockwise around this square */
00359     for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
00360       static const int8 ddx[DIAGDIR_END] = { -1,  1,  1, -1};
00361       static const int8 ddy[DIAGDIR_END] = {  1,  1, -1, -1};
00362 
00363       int dx = ddx[dir];
00364       int dy = ddy[dir];
00365 
00366       /* each side of this square has length 'dist' */
00367       for (uint a = 0; a < dist; a++) {
00368         /* MP_VOID tiles are not checked (interval is [min; max) for IsInsideMM())*/
00369         if (IsInsideMM(x, min_xy, max_x) && IsInsideMM(y, min_xy, max_y)) {
00370           TileIndex t = TileXY(x, y);
00371           if (HasTileWaterGround(t) == water) return dist;
00372         }
00373         x += dx;
00374         y += dy;
00375       }
00376     }
00377   }
00378 
00379   if (!water) {
00380     /* no land found - is this a water-only map? */
00381     for (TileIndex t = 0; t < MapSize(); t++) {
00382       if (!IsTileType(t, MP_VOID) && !IsTileType(t, MP_WATER)) return 0x1FF;
00383     }
00384   }
00385 
00386   return max_dist;
00387 }

Generated on Sun May 15 19:20:09 2011 for OpenTTD by  doxygen 1.6.1