00001
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
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
00036
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 _m = CallocT<Tile>(_map_size);
00056 _me = CallocT<TileExtended>(_map_size);
00057 }
00058
00059
00060 #ifdef _DEBUG
00061 TileIndex TileAdd(TileIndex tile, TileIndexDiff add,
00062 const char *exp, const char *file, int line)
00063 {
00064 int dx;
00065 int dy;
00066 uint x;
00067 uint y;
00068
00069 dx = add & MapMaxX();
00070 if (dx >= (int)MapSizeX() / 2) dx -= MapSizeX();
00071 dy = (add - dx) / (int)MapSizeX();
00072
00073 x = TileX(tile) + dx;
00074 y = TileY(tile) + dy;
00075
00076 if (x >= MapSizeX() || y >= MapSizeY()) {
00077 char buf[512];
00078
00079 snprintf(buf, lengthof(buf), "TILE_ADD(%s) when adding 0x%.4X and 0x%.4X failed",
00080 exp, tile, add);
00081 #if !defined(_MSC_VER) || defined(WINCE)
00082 fprintf(stderr, "%s:%d %s\n", file, line, buf);
00083 #else
00084 _assert(buf, (char*)file, line);
00085 #endif
00086 }
00087
00088 assert(TileXY(x, y) == TILE_MASK(tile + add));
00089
00090 return TileXY(x, y);
00091 }
00092 #endif
00093
00100 uint ScaleByMapSize(uint n)
00101 {
00102
00103
00104
00105 return (n * (MapSize() >> 12) + (1 << 4) - 1) >> 4;
00106 }
00107
00108
00115 uint ScaleByMapSize1D(uint n)
00116 {
00117
00118
00119
00120
00121 return (n * (MapSizeX() + MapSizeY()) + (1 << 9) - 1) >> 9;
00122 }
00123
00124
00138 TileIndex TileAddWrap(TileIndex tile, int addx, int addy)
00139 {
00140 uint x = TileX(tile) + addx;
00141 uint y = TileY(tile) + addy;
00142
00143
00144 if (x < MapMaxX() && y < MapMaxY())
00145 return tile + TileDiffXY(addx, addy);
00146
00147 return INVALID_TILE;
00148 }
00149
00151 extern const TileIndexDiffC _tileoffs_by_diagdir[] = {
00152 {-1, 0},
00153 { 0, 1},
00154 { 1, 0},
00155 { 0, -1}
00156 };
00157
00159 extern const TileIndexDiffC _tileoffs_by_dir[] = {
00160 {-1, -1},
00161 {-1, 0},
00162 {-1, 1},
00163 { 0, 1},
00164 { 1, 1},
00165 { 1, 0},
00166 { 1, -1},
00167 { 0, -1}
00168 };
00169
00179 uint DistanceManhattan(TileIndex t0, TileIndex t1)
00180 {
00181 const uint dx = Delta(TileX(t0), TileX(t1));
00182 const uint dy = Delta(TileY(t0), TileY(t1));
00183 return dx + dy;
00184 }
00185
00186
00196 uint DistanceSquare(TileIndex t0, TileIndex t1)
00197 {
00198 const int dx = TileX(t0) - TileX(t1);
00199 const int dy = TileY(t0) - TileY(t1);
00200 return dx * dx + dy * dy;
00201 }
00202
00203
00211 uint DistanceMax(TileIndex t0, TileIndex t1)
00212 {
00213 const uint dx = Delta(TileX(t0), TileX(t1));
00214 const uint dy = Delta(TileY(t0), TileY(t1));
00215 return max(dx, dy);
00216 }
00217
00218
00227 uint DistanceMaxPlusManhattan(TileIndex t0, TileIndex t1)
00228 {
00229 const uint dx = Delta(TileX(t0), TileX(t1));
00230 const uint dy = Delta(TileY(t0), TileY(t1));
00231 return dx > dy ? 2 * dx + dy : 2 * dy + dx;
00232 }
00233
00239 uint DistanceFromEdge(TileIndex tile)
00240 {
00241 const uint xl = TileX(tile);
00242 const uint yl = TileY(tile);
00243 const uint xh = MapSizeX() - 1 - xl;
00244 const uint yh = MapSizeY() - 1 - yl;
00245 const uint minl = min(xl, yl);
00246 const uint minh = min(xh, yh);
00247 return min(minl, minh);
00248 }
00249
00263 bool CircularTileSearch(TileIndex *tile, uint size, TestTileOnSearchProc proc, void *user_data)
00264 {
00265 assert(proc != NULL);
00266 assert(size > 0);
00267
00268 if (size % 2 == 1) {
00269
00270
00271 if (proc(*tile, user_data)) return true;
00272
00273
00274
00275 *tile = TILE_ADD(*tile, TileOffsByDir(DIR_W));
00276 return CircularTileSearch(tile, size / 2, 1, 1, proc, user_data);
00277 } else {
00278 return CircularTileSearch(tile, size / 2, 0, 0, proc, user_data);
00279 }
00280 }
00281
00298 bool CircularTileSearch(TileIndex *tile, uint radius, uint w, uint h, TestTileOnSearchProc proc, void *user_data)
00299 {
00300 assert(proc != NULL);
00301 assert(radius > 0);
00302
00303 uint x = TileX(*tile) + w + 1;
00304 uint y = TileY(*tile);
00305
00306 uint extent[DIAGDIR_END] = { w, h, w, h };
00307
00308 for (uint n = 0; n < radius; n++) {
00309 for (DiagDirection dir = DIAGDIR_NE; dir < DIAGDIR_END; dir++) {
00310 for (uint j = extent[dir] + n * 2 + 1; j != 0; j--) {
00311 if (x <= MapMaxX() && y <= MapMaxY() &&
00312 proc(TileXY(x, y), user_data)) {
00313 *tile = TileXY(x, y);
00314 return true;
00315 }
00316
00317
00318 x += _tileoffs_by_diagdir[dir].x;
00319 y += _tileoffs_by_diagdir[dir].y;
00320 }
00321 }
00322
00323 x += _tileoffs_by_dir[DIR_W].x;
00324 y += _tileoffs_by_dir[DIR_W].y;
00325 }
00326
00327 *tile = INVALID_TILE;
00328 return false;
00329 }