# map.cpp

Go to the documentation of this file.

    00001 /* $Id: map.cpp 15299 2009-01-31 20:16:06Z smatz $ */
    00002
    00005 #include "stdafx.h"
    00006 #include "debug.h"
    00007 #include "core/bitmath_func.hpp"
    00008 #include "core/alloc_func.hpp"
    00009 #include "core/math_func.hpp"
    00010 #include "map_func.h"
    00011
    00012 #if defined(_MSC_VER)
    00013 /* Why the hell is that not in all MSVC headers?? */
    00014 extern "C" _CRTIMP void __cdecl _assert(void *, void *, unsigned);
    00015 #endif
    00016
    00017 uint _map_log_x;
    00018 uint _map_log_y;
    00019 uint _map_size_x;
    00020 uint _map_size_y;
    00021 uint _map_size;
    00022 uint _map_tile_mask;
    00023
    00024 Tile *_m = NULL;
    00025 TileExtended *_me = NULL;
    00026
    00027
    00033 void AllocateMap(uint size_x, uint size_y)
    00034 {
    00035   /* Make sure that the map size is within the limits and that
    00036    * the x axis size is a power of 2. */
    00037   if (size_x < 64 || size_x > 2048 ||
    00038       size_y < 64 || size_y > 2048 ||
    00039       (size_x & (size_x - 1)) != 0 ||
    00040       (size_y & (size_y - 1)) != 0)
    00041     error("Invalid map size");
    00042
    00043   DEBUG(map, 1, "Allocating map of size %dx%d", size_x, size_y);
    00044
    00045   _map_log_x = FindFirstBit(size_x);
    00046   _map_log_y = FindFirstBit(size_y);
    00047   _map_size_x = size_x;
    00048   _map_size_y = size_y;
    00049   _map_size = size_x * size_y;
    00050   _map_tile_mask = _map_size - 1;
    00051
    00052   free(_m);
    00053   free(_me);
    00054
    00055   /* XXX @todo handle memory shortage more gracefully
    00056    * CallocT does the out-of-memory check
    00057    * Maybe some attemps could be made to try with smaller maps down to 64x64
    00058    * Maybe check for available memory before doing the calls, after all, we know how big
    00059    * the map is */
    00060   _m = CallocT<Tile>(_map_size);
    00061   _me = CallocT<TileExtended>(_map_size);
    00062 }
    00063
    00064
    00065 #ifdef _DEBUG
    00066 TileIndex TileAdd(TileIndex tile, TileIndexDiff add,
    00067   const char *exp, const char *file, int line)
    00068 {
    00069   int dx;
    00070   int dy;
    00071   uint x;
    00072   uint y;
    00073
    00074   dx = add & MapMaxX();
    00075   if (dx >= (int)MapSizeX() / 2) dx -= MapSizeX();
    00076   dy = (add - dx) / (int)MapSizeX();
    00077
    00078   x = TileX(tile) + dx;
    00079   y = TileY(tile) + dy;
    00080
    00081   if (x >= MapSizeX() || y >= MapSizeY()) {
    00082     char buf[512];
    00083
    00084     snprintf(buf, lengthof(buf), "TILE_ADD(%s) when adding 0x%.4X and 0x%.4X failed",
    00085       exp, tile, add);
    00086 #if !defined(_MSC_VER) || defined(WINCE)
    00087     fprintf(stderr, "%s:%d %s\n", file, line, buf);
    00088 #else
    00089     _assert(buf, (char*)file, line);
    00090 #endif
    00091   }
    00092
    00093   assert(TileXY(x, y) == TILE_MASK(tile + add));
    00094
    00095   return TileXY(x, y);
    00096 }
    00097 #endif
    00098
    00105 uint ScaleByMapSize(uint n)
    00106 {
    00107   /* First shift by 12 to prevent integer overflow for large values of n.
    00108    * >>12 is safe since the min mapsize is 64x64
    00109    * Add (1<<4)-1 to round upwards. */
    00110   return (n * (MapSize() >> 12) + (1 << 4) - 1) >> 4;
    00111 }
    00112
    00113
    00120 uint ScaleByMapSize1D(uint n)
    00121 {
    00122   /* Normal circumference for the X+Y is 256+256 = 1<<9
    00123    * Note, not actually taking the full circumference into account,
    00124    * just half of it.
    00125    * (1<<9) - 1 is there to scale upwards. */
    00126   return (n * (MapSizeX() + MapSizeY()) + (1 << 9) - 1) >> 9;
    00127 }
    00128
    00129
    00143 TileIndex TileAddWrap(TileIndex tile, int addx, int addy)
    00144 {
    00145   uint x = TileX(tile) + addx;
    00146   uint y = TileY(tile) + addy;
    00147
    00148   /* Are we about to wrap? */
    00149   if (x < MapMaxX() && y < MapMaxY())
    00150     return tile + TileDiffXY(addx, addy);
    00151
    00152   return INVALID_TILE;
    00153 }
    00154
    00156 extern const TileIndexDiffC _tileoffs_by_diagdir[] = {
    00157   {-1,  0},
    00158   { 0,  1},
    00159   { 1,  0},
    00160   { 0, -1}
    00161 };
    00162
    00164 extern const TileIndexDiffC _tileoffs_by_dir[] = {
    00165   {-1, -1},
    00166   {-1,  0},
    00167   {-1,  1},
    00168   { 0,  1},
    00169   { 1,  1},
    00170   { 1,  0},
    00171   { 1, -1},
    00172   { 0, -1}
    00173 };
    00174
    00184 uint DistanceManhattan(TileIndex t0, TileIndex t1)
    00185 {
    00186   const uint dx = Delta(TileX(t0), TileX(t1));
    00187   const uint dy = Delta(TileY(t0), TileY(t1));
    00188   return dx + dy;
    00189 }
    00190
    00191
    00201 uint DistanceSquare(TileIndex t0, TileIndex t1)
    00202 {
    00203   const int dx = TileX(t0) - TileX(t1);
    00204   const int dy = TileY(t0) - TileY(t1);
    00205   return dx * dx + dy * dy;
    00206 }
    00207
    00208
    00216 uint DistanceMax(TileIndex t0, TileIndex t1)
    00217 {
    00218   const uint dx = Delta(TileX(t0), TileX(t1));
    00219   const uint dy = Delta(TileY(t0), TileY(t1));
    00220   return max(dx, dy);
    00221 }
    00222
    00223
    00232 uint DistanceMaxPlusManhattan(TileIndex t0, TileIndex t1)
    00233 {
    00234   const uint dx = Delta(TileX(t0), TileX(t1));
    00235   const uint dy = Delta(TileY(t0), TileY(t1));
    00236   return dx > dy ? 2 * dx + dy : 2 * dy + dx;
    00237 }
    00238
    00244 uint DistanceFromEdge(TileIndex tile)
    00245 {
    00246   const uint xl = TileX(tile);
    00247   const uint yl = TileY(tile);
    00248   const uint xh = MapSizeX() - 1 - xl;
    00249   const uint yh = MapSizeY() - 1 - yl;
    00250   const uint minl = min(xl, yl);
    00251   const uint minh = min(xh, yh);
    00252   return min(minl, minh);
    00253 }
    00254
    00268 bool CircularTileSearch(TileIndex *tile, uint size, TestTileOnSearchProc proc, void *user_data)
    00269 {
    00270   assert(proc != NULL);
    00271   assert(size > 0);
    00272
    00273   if (size % 2 == 1) {
    00274     /* If the length of the side is uneven, the center has to be checked
    00275      * separately, as the pattern of uneven sides requires to go around the center */
    00276     if (proc(*tile, user_data)) return true;
    00277
    00278     /* If tile test is not successful, get one tile down and left,
    00279      * ready for a test in first circle around center tile */
    00280     *tile = TILE_ADD(*tile, TileOffsByDir(DIR_W));
    00281     return CircularTileSearch(tile, size / 2, 1, 1, proc, user_data);
    00282   } else {
    00283     return CircularTileSearch(tile, size / 2, 0, 0, proc, user_data);
    00284   }
    00285 }
    00286
    00303 bool CircularTileSearch(TileIndex *tile, uint radius, uint w, uint h, TestTileOnSearchProc proc, void *user_data)
    00304 {
    00305   assert(proc != NULL);
    00306   assert(radius > 0);
    00307
    00308   uint x = TileX(*tile) + w + 1;
    00309   uint y = TileY(*tile);
    00310
    00311   uint extent[DIAGDIR_END] = { w, h, w, h };
    00312
    00313   for (uint n = 0; n < radius; n++) {
    00314     for (DiagDirection dir = DIAGDIR_NE; dir < DIAGDIR_END; dir++) {
    00315       for (uint j = extent[dir] + n * 2 + 1; j != 0; j--) {
    00316         if (x <= MapMaxX() && y <= MapMaxY() &&
    00317             proc(TileXY(x, y), user_data)) {
    00318           *tile = TileXY(x, y);
    00319           return true;
    00320         }
    00321
    00322         /* Step to the next 'neighbour' in the circular line */
    00323         x += _tileoffs_by_diagdir[dir].x;
    00324         y += _tileoffs_by_diagdir[dir].y;
    00325       }
    00326     }
    00327     /* Jump to next circle to test */
    00328     x += _tileoffs_by_dir[DIR_W].x;
    00329     y += _tileoffs_by_dir[DIR_W].y;
    00330   }
    00331
    00332   *tile = INVALID_TILE;
    00333   return false;
    00334 }