map.cpp

Go to the documentation of this file.
00001 /* $Id$ */
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 
00017 #if defined(_MSC_VER)
00018 /* Why the hell is that not in all MSVC headers?? */
00019 extern "C" _CRTIMP void __cdecl _assert(void *, void *, unsigned);
00020 #endif
00021 
00022 uint _map_log_x;     
00023 uint _map_log_y;     
00024 uint _map_size_x;    
00025 uint _map_size_y;    
00026 uint _map_size;      
00027 uint _map_tile_mask; 
00028 
00029 Tile *_m = NULL;          
00030 TileExtended *_me = NULL; 
00031 
00032 
00038 void AllocateMap(uint size_x, uint size_y)
00039 {
00040   /* Make sure that the map size is within the limits and that
00041    * size of both axes is a power of 2. */
00042   if (!IsInsideMM(size_x, MIN_MAP_SIZE, MAX_MAP_SIZE + 1) ||
00043       !IsInsideMM(size_y, MIN_MAP_SIZE, MAX_MAP_SIZE + 1) ||
00044       (size_x & (size_x - 1)) != 0 ||
00045       (size_y & (size_y - 1)) != 0) {
00046     error("Invalid map size");
00047   }
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 
00233 uint DistanceFromEdgeDir(TileIndex tile, DiagDirection dir)
00234 {
00235   switch (dir) {
00236     case DIAGDIR_NE: return             TileX(tile) - (_settings_game.construction.freeform_edges ? 1 : 0);
00237     case DIAGDIR_NW: return             TileY(tile) - (_settings_game.construction.freeform_edges ? 1 : 0);
00238     case DIAGDIR_SW: return MapMaxX() - TileX(tile) - 1;
00239     case DIAGDIR_SE: return MapMaxY() - TileY(tile) - 1;
00240     default: NOT_REACHED();
00241   }
00242 }
00243 
00257 bool CircularTileSearch(TileIndex *tile, uint size, TestTileOnSearchProc proc, void *user_data)
00258 {
00259   assert(proc != NULL);
00260   assert(size > 0);
00261 
00262   if (size % 2 == 1) {
00263     /* If the length of the side is uneven, the center has to be checked
00264      * separately, as the pattern of uneven sides requires to go around the center */
00265     if (proc(*tile, user_data)) return true;
00266 
00267     /* If tile test is not successful, get one tile up,
00268      * ready for a test in first circle around center tile */
00269     *tile = TILE_ADD(*tile, TileOffsByDir(DIR_N));
00270     return CircularTileSearch(tile, size / 2, 1, 1, proc, user_data);
00271   } else {
00272     return CircularTileSearch(tile, size / 2, 0, 0, proc, user_data);
00273   }
00274 }
00275 
00295 bool CircularTileSearch(TileIndex *tile, uint radius, uint w, uint h, TestTileOnSearchProc proc, void *user_data)
00296 {
00297   assert(proc != NULL);
00298   assert(radius > 0);
00299 
00300   uint x = TileX(*tile) + w + 1;
00301   uint y = TileY(*tile);
00302 
00303   const uint extent[DIAGDIR_END] = { w, h, w, h };
00304 
00305   for (uint n = 0; n < radius; n++) {
00306     for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
00307       /* Is the tile within the map? */
00308       for (uint j = extent[dir] + n * 2 + 1; j != 0; j--) {
00309         if (x < MapSizeX() && y < MapSizeY()) {
00310           TileIndex t = TileXY(x, y);
00311           /* Is the callback successful? */
00312           if (proc(t, user_data)) {
00313             /* Stop the search */
00314             *tile = t;
00315             return true;
00316           }
00317         }
00318 
00319         /* Step to the next 'neighbour' in the circular line */
00320         x += _tileoffs_by_diagdir[dir].x;
00321         y += _tileoffs_by_diagdir[dir].y;
00322       }
00323     }
00324     /* Jump to next circle to test */
00325     x += _tileoffs_by_dir[DIR_W].x;
00326     y += _tileoffs_by_dir[DIR_W].y;
00327   }
00328 
00329   *tile = INVALID_TILE;
00330   return false;
00331 }
00332 
00340 uint GetClosestWaterDistance(TileIndex tile, bool water)
00341 {
00342   if (IsTileType(tile, MP_WATER) == 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 (IsTileType(t, MP_WATER) == 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 Jan 23 01:49:03 2011 for OpenTTD by  doxygen 1.6.1