2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2007-03-03 04:04:22 +00:00
|
|
|
/** @file map.cpp */
|
|
|
|
|
2004-12-15 22:18:54 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2005-02-05 15:58:59 +00:00
|
|
|
#include "debug.h"
|
2005-01-29 19:45:14 +00:00
|
|
|
#include "functions.h"
|
2005-07-21 22:15:02 +00:00
|
|
|
#include "macros.h"
|
2004-12-15 22:18:54 +00:00
|
|
|
#include "map.h"
|
2006-11-17 23:01:58 +00:00
|
|
|
#include "direction.h"
|
2007-01-10 18:56:51 +00:00
|
|
|
#include "helpers.hpp"
|
2004-12-15 22:18:54 +00:00
|
|
|
|
2005-10-02 22:39:56 +00:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER >= 1400 /* VStudio 2005 is stupid! */
|
|
|
|
/* Why the hell is that not in all MSVC headers?? */
|
2007-01-10 18:56:51 +00:00
|
|
|
extern "C" _CRTIMP void __cdecl _assert(void *, void *, unsigned);
|
2005-10-02 22:39:56 +00:00
|
|
|
#endif
|
|
|
|
|
2007-04-27 21:29:36 +00:00
|
|
|
uint _map_log_x; ///< 2^_map_log_x == _map_size_x
|
|
|
|
uint _map_size_x; ///< Size of the map along the X
|
|
|
|
uint _map_size_y; ///< Size of the map along the Y
|
|
|
|
uint _map_size; ///< The number of tiles on the map
|
|
|
|
uint _map_tile_mask; ///< _map_size - 1 (to mask the mapsize)
|
2005-01-07 17:40:23 +00:00
|
|
|
|
2007-04-27 21:29:36 +00:00
|
|
|
Tile *_m = NULL; ///< Tiles of the map
|
|
|
|
TileExtended *_me = NULL; ///< Extended Tiles of the map
|
2004-12-16 12:30:13 +00:00
|
|
|
|
2005-01-03 18:59:58 +00:00
|
|
|
|
2007-09-09 10:21:27 +00:00
|
|
|
/*!
|
2007-04-27 21:29:36 +00:00
|
|
|
* (Re)allocates a map with the given dimension
|
|
|
|
* @param size_x the width of the map along the NE/SW edge
|
|
|
|
* @param size_y the 'height' of the map along the SE/NW edge
|
|
|
|
*/
|
2005-07-13 19:51:31 +00:00
|
|
|
void AllocateMap(uint size_x, uint size_y)
|
2005-01-29 19:45:14 +00:00
|
|
|
{
|
2007-03-03 04:04:22 +00:00
|
|
|
/* Make sure that the map size is within the limits and that
|
|
|
|
* the x axis size is a power of 2. */
|
2005-07-13 19:51:31 +00:00
|
|
|
if (size_x < 64 || size_x > 2048 ||
|
|
|
|
size_y < 64 || size_y > 2048 ||
|
2007-04-27 21:29:36 +00:00
|
|
|
(size_x & (size_x - 1)) != 0 ||
|
|
|
|
(size_y & (size_y - 1)) != 0)
|
2005-01-31 06:46:53 +00:00
|
|
|
error("Invalid map size");
|
2005-01-29 19:45:14 +00:00
|
|
|
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(map, 1, "Allocating map of size %dx%d", size_x, size_y);
|
2005-01-29 19:45:14 +00:00
|
|
|
|
2005-07-13 19:51:31 +00:00
|
|
|
_map_log_x = FindFirstBit(size_x);
|
|
|
|
_map_size_x = size_x;
|
|
|
|
_map_size_y = size_y;
|
|
|
|
_map_size = size_x * size_y;
|
|
|
|
_map_tile_mask = _map_size - 1;
|
2005-01-29 19:45:14 +00:00
|
|
|
|
2005-07-13 18:04:01 +00:00
|
|
|
free(_m);
|
2007-03-19 11:27:30 +00:00
|
|
|
free(_me);
|
|
|
|
|
2007-04-18 22:41:53 +00:00
|
|
|
/* XXX @todo handle memory shortage more gracefully
|
2007-12-08 15:47:23 +00:00
|
|
|
* CallocT does the out-of-memory check
|
2007-04-18 22:41:53 +00:00
|
|
|
* Maybe some attemps could be made to try with smaller maps down to 64x64
|
|
|
|
* Maybe check for available memory before doing the calls, after all, we know how big
|
|
|
|
* the map is */
|
2007-12-08 15:47:23 +00:00
|
|
|
_m = CallocT<Tile>(_map_size);
|
|
|
|
_me = CallocT<TileExtended>(_map_size);
|
2005-01-29 19:45:14 +00:00
|
|
|
}
|
2005-01-05 13:32:03 +00:00
|
|
|
|
2005-01-09 17:55:11 +00:00
|
|
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
TileIndex TileAdd(TileIndex tile, TileIndexDiff add,
|
|
|
|
const char *exp, const char *file, int line)
|
|
|
|
{
|
|
|
|
int dx;
|
|
|
|
int dy;
|
|
|
|
uint x;
|
|
|
|
uint y;
|
|
|
|
|
|
|
|
dx = add & MapMaxX();
|
2005-01-09 18:59:16 +00:00
|
|
|
if (dx >= (int)MapSizeX() / 2) dx -= MapSizeX();
|
2005-01-09 17:55:11 +00:00
|
|
|
dy = (add - dx) / (int)MapSizeX();
|
|
|
|
|
|
|
|
x = TileX(tile) + dx;
|
|
|
|
y = TileY(tile) + dy;
|
|
|
|
|
|
|
|
if (x >= MapSizeX() || y >= MapSizeY()) {
|
|
|
|
char buf[512];
|
|
|
|
|
2006-11-28 20:55:16 +00:00
|
|
|
snprintf(buf, lengthof(buf), "TILE_ADD(%s) when adding 0x%.4X and 0x%.4X failed",
|
2005-01-09 17:55:11 +00:00
|
|
|
exp, tile, add);
|
2007-08-03 23:26:12 +00:00
|
|
|
#if !defined(_MSC_VER) || defined(WINCE)
|
2005-01-09 17:55:11 +00:00
|
|
|
fprintf(stderr, "%s:%d %s\n", file, line, buf);
|
|
|
|
#else
|
|
|
|
_assert(buf, (char*)file, line);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-04-18 22:10:36 +00:00
|
|
|
assert(TileXY(x, y) == TILE_MASK(tile + add));
|
2005-01-09 17:55:11 +00:00
|
|
|
|
2007-04-18 22:10:36 +00:00
|
|
|
return TileXY(x, y);
|
2005-01-09 17:55:11 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-09-09 10:21:27 +00:00
|
|
|
/*!
|
2007-04-27 21:29:36 +00:00
|
|
|
* Scales the given value by the map size, where the given value is
|
2007-09-09 10:21:27 +00:00
|
|
|
* for a 256 by 256 map.
|
2007-04-27 21:29:36 +00:00
|
|
|
* @param n the value to scale
|
|
|
|
* @return the scaled size
|
|
|
|
*/
|
2005-01-28 15:31:04 +00:00
|
|
|
uint ScaleByMapSize(uint n)
|
|
|
|
{
|
2007-03-03 04:04:22 +00:00
|
|
|
/* First shift by 12 to prevent integer overflow for large values of n.
|
|
|
|
* >>12 is safe since the min mapsize is 64x64
|
|
|
|
* Add (1<<4)-1 to round upwards. */
|
2007-04-18 22:10:36 +00:00
|
|
|
return (n * (MapSize() >> 12) + (1 << 4) - 1) >> 4;
|
2005-01-28 15:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-09 10:21:27 +00:00
|
|
|
/*!
|
2007-04-27 21:29:36 +00:00
|
|
|
* Scales the given value by the maps circumference, where the given
|
|
|
|
* value is for a 256 by 256 map
|
|
|
|
* @param n the value to scale
|
|
|
|
* @return the scaled size
|
|
|
|
*/
|
2005-01-28 15:31:04 +00:00
|
|
|
uint ScaleByMapSize1D(uint n)
|
|
|
|
{
|
2007-03-03 04:04:22 +00:00
|
|
|
/* Normal circumference for the X+Y is 256+256 = 1<<9
|
|
|
|
* Note, not actually taking the full circumference into account,
|
|
|
|
* just half of it.
|
|
|
|
* (1<<9) - 1 is there to scale upwards. */
|
2007-04-18 22:10:36 +00:00
|
|
|
return (n * (MapSizeX() + MapSizeY()) + (1 << 9) - 1) >> 9;
|
2005-01-28 15:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-09 10:21:27 +00:00
|
|
|
/*!
|
2007-04-27 21:29:36 +00:00
|
|
|
* This function checks if we add addx/addy to tile, if we
|
2007-09-09 10:21:27 +00:00
|
|
|
* do wrap around the edges. For example, tile = (10,2) and
|
|
|
|
* addx = +3 and addy = -4. This function will now return
|
|
|
|
* INVALID_TILE, because the y is wrapped. This is needed in
|
|
|
|
* for example, farmland. When the tile is not wrapped,
|
|
|
|
* the result will be tile + TileDiffXY(addx, addy)
|
|
|
|
*
|
2007-04-27 21:29:36 +00:00
|
|
|
* @param tile the 'starting' point of the adding
|
|
|
|
* @param addx the amount of tiles in the X direction to add
|
|
|
|
* @param addy the amount of tiles in the Y direction to add
|
|
|
|
* @return translated tile, or INVALID_TILE when it would've wrapped.
|
|
|
|
*/
|
2005-01-31 11:23:10 +00:00
|
|
|
uint TileAddWrap(TileIndex tile, int addx, int addy)
|
|
|
|
{
|
2005-10-22 06:39:32 +00:00
|
|
|
uint x = TileX(tile) + addx;
|
|
|
|
uint y = TileY(tile) + addy;
|
2005-01-31 11:23:10 +00:00
|
|
|
|
2007-03-03 04:04:22 +00:00
|
|
|
/* Are we about to wrap? */
|
2005-06-25 16:44:57 +00:00
|
|
|
if (x < MapMaxX() && y < MapMaxY())
|
|
|
|
return tile + TileDiffXY(addx, addy);
|
2005-01-31 11:23:10 +00:00
|
|
|
|
|
|
|
return INVALID_TILE;
|
|
|
|
}
|
|
|
|
|
2007-04-27 21:29:36 +00:00
|
|
|
/** 'Lookup table' for tile offsets given a DiagDirection */
|
2007-01-10 18:56:51 +00:00
|
|
|
extern const TileIndexDiffC _tileoffs_by_diagdir[] = {
|
2007-03-03 04:04:22 +00:00
|
|
|
{-1, 0}, ///< DIAGDIR_NE
|
|
|
|
{ 0, 1}, ///< DIAGDIR_SE
|
|
|
|
{ 1, 0}, ///< DIAGDIR_SW
|
|
|
|
{ 0, -1} ///< DIAGDIR_NW
|
2006-09-05 23:21:41 +00:00
|
|
|
};
|
|
|
|
|
2007-04-27 21:29:36 +00:00
|
|
|
/** 'Lookup table' for tile offsets given a Direction */
|
2007-01-10 18:56:51 +00:00
|
|
|
extern const TileIndexDiffC _tileoffs_by_dir[] = {
|
2007-03-03 04:04:22 +00:00
|
|
|
{-1, -1}, ///< DIR_N
|
|
|
|
{-1, 0}, ///< DIR_NE
|
|
|
|
{-1, 1}, ///< DIR_E
|
|
|
|
{ 0, 1}, ///< DIR_SE
|
|
|
|
{ 1, 1}, ///< DIR_S
|
|
|
|
{ 1, 0}, ///< DIR_SW
|
|
|
|
{ 1, -1}, ///< DIR_W
|
|
|
|
{ 0, -1} ///< DIR_NW
|
2005-01-31 11:23:10 +00:00
|
|
|
};
|
|
|
|
|
2007-09-09 10:21:27 +00:00
|
|
|
/*!
|
2007-04-27 21:29:36 +00:00
|
|
|
* Gets the Manhattan distance between the two given tiles.
|
|
|
|
* The Manhattan distance is the sum of the delta of both the
|
|
|
|
* X and Y component.
|
|
|
|
* Also known as L1-Norm
|
|
|
|
* @param t0 the start tile
|
|
|
|
* @param t1 the end tile
|
|
|
|
* @return the distance
|
|
|
|
*/
|
2005-01-31 07:23:15 +00:00
|
|
|
uint DistanceManhattan(TileIndex t0, TileIndex t1)
|
|
|
|
{
|
2007-11-26 16:01:29 +00:00
|
|
|
const uint dx = Delta(TileX(t0), TileX(t1));
|
|
|
|
const uint dy = Delta(TileY(t0), TileY(t1));
|
2005-04-11 19:14:48 +00:00
|
|
|
return dx + dy;
|
2005-01-31 07:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-09 10:21:27 +00:00
|
|
|
/*!
|
2007-04-27 21:29:36 +00:00
|
|
|
* Gets the 'Square' distance between the two given tiles.
|
|
|
|
* The 'Square' distance is the square of the shortest (straight line)
|
|
|
|
* distance between the two tiles.
|
|
|
|
* Also known as euclidian- or L2-Norm squared.
|
|
|
|
* @param t0 the start tile
|
|
|
|
* @param t1 the end tile
|
|
|
|
* @return the distance
|
|
|
|
*/
|
2005-01-31 07:23:15 +00:00
|
|
|
uint DistanceSquare(TileIndex t0, TileIndex t1)
|
|
|
|
{
|
2005-04-11 19:14:48 +00:00
|
|
|
const int dx = TileX(t0) - TileX(t1);
|
|
|
|
const int dy = TileY(t0) - TileY(t1);
|
|
|
|
return dx * dx + dy * dy;
|
2005-01-31 07:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-09 10:21:27 +00:00
|
|
|
/*!
|
2007-04-27 21:29:36 +00:00
|
|
|
* Gets the biggest distance component (x or y) between the two given tiles.
|
|
|
|
* Also known as L-Infinity-Norm.
|
|
|
|
* @param t0 the start tile
|
|
|
|
* @param t1 the end tile
|
|
|
|
* @return the distance
|
|
|
|
*/
|
2005-01-31 07:23:15 +00:00
|
|
|
uint DistanceMax(TileIndex t0, TileIndex t1)
|
|
|
|
{
|
2007-11-26 16:01:29 +00:00
|
|
|
const uint dx = Delta(TileX(t0), TileX(t1));
|
|
|
|
const uint dy = Delta(TileY(t0), TileY(t1));
|
2007-11-20 12:57:24 +00:00
|
|
|
return max(dx, dy);
|
2005-01-31 07:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-09 10:21:27 +00:00
|
|
|
/*!
|
2007-04-27 21:29:36 +00:00
|
|
|
* Gets the biggest distance component (x or y) between the two given tiles
|
|
|
|
* plus the Manhattan distance, i.e. two times the biggest distance component
|
|
|
|
* and once the smallest component.
|
|
|
|
* @param t0 the start tile
|
|
|
|
* @param t1 the end tile
|
|
|
|
* @return the distance
|
|
|
|
*/
|
2005-01-31 07:23:15 +00:00
|
|
|
uint DistanceMaxPlusManhattan(TileIndex t0, TileIndex t1)
|
|
|
|
{
|
2007-11-26 16:01:29 +00:00
|
|
|
const uint dx = Delta(TileX(t0), TileX(t1));
|
|
|
|
const uint dy = Delta(TileY(t0), TileY(t1));
|
2005-04-11 19:14:48 +00:00
|
|
|
return dx > dy ? 2 * dx + dy : 2 * dy + dx;
|
2005-01-31 07:23:15 +00:00
|
|
|
}
|
|
|
|
|
2007-09-09 10:21:27 +00:00
|
|
|
/*!
|
2007-04-27 21:29:36 +00:00
|
|
|
* Param the minimum distance to an edge
|
|
|
|
* @param tile the tile to get the distance from
|
|
|
|
* @return the distance from the edge in tiles
|
|
|
|
*/
|
2005-01-31 07:23:15 +00:00
|
|
|
uint DistanceFromEdge(TileIndex tile)
|
|
|
|
{
|
|
|
|
const uint xl = TileX(tile);
|
|
|
|
const uint yl = TileY(tile);
|
|
|
|
const uint xh = MapSizeX() - 1 - xl;
|
|
|
|
const uint yh = MapSizeY() - 1 - yl;
|
2007-11-20 12:57:24 +00:00
|
|
|
const uint minl = min(xl, yl);
|
|
|
|
const uint minh = min(xh, yh);
|
|
|
|
return min(minl, minh);
|
2005-01-31 07:23:15 +00:00
|
|
|
}
|
2006-11-17 23:01:58 +00:00
|
|
|
|
2007-09-09 10:21:27 +00:00
|
|
|
/*!
|
2006-11-17 23:01:58 +00:00
|
|
|
* Function performing a search around a center tile and going outward, thus in circle.
|
|
|
|
* Although it really is a square search...
|
|
|
|
* Every tile will be tested by means of the callback function proc,
|
|
|
|
* which will determine if yes or no the given tile meets criteria of search.
|
|
|
|
* @param tile to start the search from
|
|
|
|
* @param size: number of tiles per side of the desired search area
|
|
|
|
* @param proc: callback testing function pointer.
|
|
|
|
* @param data to be passed to the callback function. Depends on the implementation
|
2007-03-03 04:04:22 +00:00
|
|
|
* @return result of the search
|
2006-11-17 23:01:58 +00:00
|
|
|
* @pre proc != NULL
|
|
|
|
* @pre size > 0
|
|
|
|
*/
|
|
|
|
bool CircularTileSearch(TileIndex tile, uint size, TestTileOnSearchProc proc, uint32 data)
|
|
|
|
{
|
|
|
|
uint n, x, y;
|
|
|
|
DiagDirection dir;
|
|
|
|
|
|
|
|
assert(proc != NULL);
|
|
|
|
assert(size > 0);
|
|
|
|
|
|
|
|
x = TileX(tile);
|
|
|
|
y = TileY(tile);
|
|
|
|
|
|
|
|
if (size % 2 == 1) {
|
|
|
|
/* If the length of the side is uneven, the center has to be checked
|
|
|
|
* separately, as the pattern of uneven sides requires to go around the center */
|
|
|
|
n = 2;
|
|
|
|
if (proc(TileXY(x, y), data)) return true;
|
|
|
|
|
2007-07-30 13:36:09 +00:00
|
|
|
/* If tile test is not successful, get one tile down and left,
|
2006-11-17 23:01:58 +00:00
|
|
|
* ready for a test in first circle around center tile */
|
|
|
|
x += _tileoffs_by_dir[DIR_W].x;
|
|
|
|
y += _tileoffs_by_dir[DIR_W].y;
|
|
|
|
} else {
|
|
|
|
n = 1;
|
|
|
|
/* To use _tileoffs_by_diagdir's order, we must relocate to
|
|
|
|
* another tile, as we now first go 'up', 'right', 'down', 'left'
|
|
|
|
* instead of 'right', 'down', 'left', 'up', which the calling
|
|
|
|
* function assume. */
|
|
|
|
x++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; n < size; n += 2) {
|
|
|
|
for (dir = DIAGDIR_NE; dir < DIAGDIR_END; dir++) {
|
|
|
|
uint j;
|
|
|
|
for (j = n; j != 0; j--) {
|
|
|
|
if (x <= MapMaxX() && y <= MapMaxY() && ///< Is the tile within the map?
|
2007-07-30 13:36:09 +00:00
|
|
|
proc(TileXY(x, y), data)) { ///< Is the callback successful?
|
2006-11-17 23:01:58 +00:00
|
|
|
return true; ///< then stop the search
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Step to the next 'neighbour' in the circular line */
|
|
|
|
x += _tileoffs_by_diagdir[dir].x;
|
|
|
|
y += _tileoffs_by_diagdir[dir].y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Jump to next circle to test */
|
|
|
|
x += _tileoffs_by_dir[DIR_W].x;
|
|
|
|
y += _tileoffs_by_dir[DIR_W].y;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|