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 "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 /* Why the hell is that not in all MSVC headers?? */
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   /* Make sure that the map size is within the limits and that
00054    * size of both axes is a power of 2. */
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   /* Are we about to wrap? */
00132   if (x >= MapMaxX() || y >= MapMaxY()) return INVALID_TILE;
00133 
00134   if (IsTileType(TileXY(x, y), MP_VOID)) return INVALID_TILE;
00135 
00136   /* Don't allow going in another area. */
00137   if (!_generating_world && GetAreaByTile(tile) != GetAreaByTile(TileXY(x, y))) return INVALID_TILE;
00138 
00139   return TileXY(x, y);
00140 }
00141 
00142 TileIndex TileAddAreaWrap(TileIndex tile, int addx, int addy)
00143 {
00144   if (_generating_world) return TileAddWrap(tile, addx, addy);
00145 
00146   int x = TileX(tile) + addx;
00147   int y = TileY(tile) + addy;
00148 
00149   uint8 backup_h2h = _head_to_head;
00150   _head_to_head = GetAreaByTile(tile);
00151   if (x < (int)AreaMinX() || y < (int)AreaMinY() || x >= (int)AreaMaxX() || y >= (int)AreaMaxY()) {
00152     tile = INVALID_TILE;
00153   } else {
00154     tile = TileXY(x, y);
00155   }
00156   _head_to_head = backup_h2h;
00157 
00158   return tile;
00159 }
00160 
00162 extern const TileIndexDiffC _tileoffs_by_diagdir[] = {
00163   {-1,  0}, 
00164   { 0,  1}, 
00165   { 1,  0}, 
00166   { 0, -1}  
00167 };
00168 
00170 extern const TileIndexDiffC _tileoffs_by_dir[] = {
00171   {-1, -1}, 
00172   {-1,  0}, 
00173   {-1,  1}, 
00174   { 0,  1}, 
00175   { 1,  1}, 
00176   { 1,  0}, 
00177   { 1, -1}, 
00178   { 0, -1}  
00179 };
00180 
00190 uint DistanceManhattan(TileIndex t0, TileIndex t1)
00191 {
00192   const uint dx = Delta(TileX(t0), TileX(t1));
00193   const uint dy = Delta(TileY(t0), TileY(t1));
00194   return dx + dy;
00195 }
00196 
00197 
00207 uint DistanceSquare(TileIndex t0, TileIndex t1)
00208 {
00209   const int dx = TileX(t0) - TileX(t1);
00210   const int dy = TileY(t0) - TileY(t1);
00211   return dx * dx + dy * dy;
00212 }
00213 
00214 
00222 uint DistanceMax(TileIndex t0, TileIndex t1)
00223 {
00224   const uint dx = Delta(TileX(t0), TileX(t1));
00225   const uint dy = Delta(TileY(t0), TileY(t1));
00226   return max(dx, dy);
00227 }
00228 
00229 
00238 uint DistanceMaxPlusManhattan(TileIndex t0, TileIndex t1)
00239 {
00240   const uint dx = Delta(TileX(t0), TileX(t1));
00241   const uint dy = Delta(TileY(t0), TileY(t1));
00242   return dx > dy ? 2 * dx + dy : 2 * dy + dx;
00243 }
00244 
00250 uint DistanceFromEdge(TileIndex tile)
00251 {
00252   uint8 h2h_backup = _head_to_head;
00253   _head_to_head = GetAreaByTile(tile);
00254   const uint xl = TileX(tile) - AreaMinX();
00255   const uint yl = TileY(tile) - AreaMinY();
00256   const uint xh = AreaMaxX() - 1 - xl;
00257   const uint yh = AreaMaxY() - 1 - yl;
00258   const uint minl = min(xl, yl);
00259   const uint minh = min(xh, yh);
00260   _head_to_head = h2h_backup;
00261   return min(minl, minh);
00262 }
00263 
00270 uint DistanceFromEdgeDir(TileIndex tile, DiagDirection dir)
00271 {
00272   switch (dir) {
00273     case DIAGDIR_NE: return             TileX(tile) - (_settings_game.construction.freeform_edges ? 1 : 0);
00274     case DIAGDIR_NW: return             TileY(tile) - (_settings_game.construction.freeform_edges ? 1 : 0);
00275     case DIAGDIR_SW: return MapMaxX() - TileX(tile) - 1;
00276     case DIAGDIR_SE: return MapMaxY() - TileY(tile) - 1;
00277     default: NOT_REACHED();
00278   }
00279 }
00280 
00294 bool CircularTileSearch(TileIndex *tile, uint size, TestTileOnSearchProc proc, void *user_data)
00295 {
00296   assert(proc != NULL);
00297   assert(size > 0);
00298 
00299   if (size % 2 == 1) {
00300     /* If the length of the side is uneven, the center has to be checked
00301      * separately, as the pattern of uneven sides requires to go around the center */
00302     if (proc(*tile, user_data)) return true;
00303 
00304     /* If tile test is not successful, get one tile up,
00305      * ready for a test in first circle around center tile */
00306     *tile = TILE_ADD(*tile, TileOffsByDir(DIR_N));
00307     return CircularTileSearch(tile, size / 2, 1, 1, proc, user_data);
00308   } else {
00309     return CircularTileSearch(tile, size / 2, 0, 0, proc, user_data);
00310   }
00311 }
00312 
00332 bool CircularTileSearch(TileIndex *tile, uint radius, uint w, uint h, TestTileOnSearchProc proc, void *user_data)
00333 {
00334   assert(proc != NULL);
00335   assert(radius > 0);
00336 
00337   uint x = TileX(*tile) + w + 1;
00338   uint y = TileY(*tile);
00339 
00340   const uint extent[DIAGDIR_END] = { w, h, w, h };
00341 
00342   for (uint n = 0; n < radius; n++) {
00343     for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
00344       /* Is the tile within the map? */
00345       for (uint j = extent[dir] + n * 2 + 1; j != 0; j--) {
00346         if (x < MapSizeX() && y < MapSizeY()) {
00347           TileIndex t = TileXY(x, y);
00348           /* Is the callback successful? */
00349           if (proc(t, user_data)) {
00350             /* Stop the search */
00351             *tile = t;
00352             return true;
00353           }
00354         }
00355 
00356         /* Step to the next 'neighbour' in the circular line */
00357         x += _tileoffs_by_diagdir[dir].x;
00358         y += _tileoffs_by_diagdir[dir].y;
00359       }
00360     }
00361     /* Jump to next circle to test */
00362     x += _tileoffs_by_dir[DIR_W].x;
00363     y += _tileoffs_by_dir[DIR_W].y;
00364   }
00365 
00366   *tile = INVALID_TILE;
00367   return false;
00368 }
00369 
00376 uint GetClosestWaterDistance(TileIndex tile, bool water)
00377 {
00378   if (HasTileWaterGround(tile) == water) return 0;
00379 
00380   uint max_dist = water ? 0x7F : 0x200;
00381 
00382   int x = TileX(tile);
00383   int y = TileY(tile);
00384 
00385   uint max_x = MapMaxX();
00386   uint max_y = MapMaxY();
00387   uint min_xy = _settings_game.construction.freeform_edges ? 1 : 0;
00388 
00389   /* go in a 'spiral' with increasing manhattan distance in each iteration */
00390   for (uint dist = 1; dist < max_dist; dist++) {
00391     /* next 'diameter' */
00392     y--;
00393 
00394     /* going counter-clockwise around this square */
00395     for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
00396       static const int8 ddx[DIAGDIR_END] = { -1,  1,  1, -1};
00397       static const int8 ddy[DIAGDIR_END] = {  1,  1, -1, -1};
00398 
00399       int dx = ddx[dir];
00400       int dy = ddy[dir];
00401 
00402       /* each side of this square has length 'dist' */
00403       for (uint a = 0; a < dist; a++) {
00404         /* MP_VOID tiles are not checked (interval is [min; max) for IsInsideMM())*/
00405         if (IsInsideMM(x, min_xy, max_x) && IsInsideMM(y, min_xy, max_y)) {
00406           TileIndex t = TileXY(x, y);
00407           if (HasTileWaterGround(t) == water) return dist;
00408         }
00409         x += dx;
00410         y += dy;
00411       }
00412     }
00413   }
00414 
00415   if (!water) {
00416     /* no land found - is this a water-only map? */
00417     for (TileIndex t = 0; t < MapSize(); t++) {
00418       if (!IsTileType(t, MP_VOID) && !IsTileType(t, MP_WATER)) return 0x1FF;
00419     }
00420   }
00421 
00422   return max_dist;
00423 }