OpenTTD-patches/map.c

176 lines
3.8 KiB
C

#include "stdafx.h"
#include "openttd.h"
#include "debug.h"
#include "functions.h"
#include "map.h"
uint _map_log_x;
uint _map_log_y;
Tile* _m = NULL;
void InitMap(uint log_x, uint log_y)
{
uint map_size;
if (log_x < 6 || log_x > 11 || log_y < 6 || log_y > 11)
error("Invalid map size");
DEBUG(map, 1)("Allocating map of size %dx%d", log_x, log_y);
_map_log_x = log_x;
_map_log_y = log_y;
// XXX - MSVC6 workaround
map_size = 1 << (log_x + log_y);
free(_m);
_m = malloc(map_size * sizeof(*_m));
// XXX TODO handle memory shortage more gracefully
if (_m == NULL) error("Failed to allocate memory for the map");
}
#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();
if (dx >= (int)MapSizeX() / 2) dx -= MapSizeX();
dy = (add - dx) / (int)MapSizeX();
x = TileX(tile) + dx;
y = TileY(tile) + dy;
if (x >= MapSizeX() || y >= MapSizeY()) {
char buf[512];
sprintf(buf, "TILE_ADD(%s) when adding 0x%.4X and 0x%.4X failed",
exp, tile, add);
#if !defined(_MSC_VER)
fprintf(stderr, "%s:%d %s\n", file, line, buf);
#else
_assert(buf, (char*)file, line);
#endif
}
assert(TileXY(x,y) == TILE_MASK(tile + add));
return TileXY(x,y);
}
#endif
uint ScaleByMapSize(uint n)
{
int shift = (int)MapLogX() - 8 + (int)MapLogY() - 8;
if (shift < 0)
return (n + (1 << -shift) - 1) >> -shift;
else
return n << shift;
}
uint ScaleByMapSize1D(uint n)
{
int shift = ((int)MapLogX() - 8 + (int)MapLogY() - 8) / 2;
if (shift < 0)
return (n + (1 << -shift) - 1) >> -shift;
else
return n << shift;
}
// This function checks if we add addx/addy to tile, if we
// 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)
uint TileAddWrap(TileIndex tile, int addx, int addy)
{
uint x, y;
x = TileX(tile) + addx;
y = TileY(tile) + addy;
// Are we about to wrap?
if (x < MapMaxX() && y < MapMaxY())
return tile + TileDiffXY(addx, addy);
return INVALID_TILE;
}
const TileIndexDiffC _tileoffs_by_dir[] = {
{-1, 0},
{ 0, 1},
{ 1, 0},
{ 0, -1}
};
uint DistanceManhattan(TileIndex t0, TileIndex t1)
{
const uint dx = abs(TileX(t0) - TileX(t1));
const uint dy = abs(TileY(t0) - TileY(t1));
return dx + dy;
}
uint DistanceSquare(TileIndex t0, TileIndex t1)
{
const int dx = TileX(t0) - TileX(t1);
const int dy = TileY(t0) - TileY(t1);
return dx * dx + dy * dy;
}
uint DistanceMax(TileIndex t0, TileIndex t1)
{
const uint dx = abs(TileX(t0) - TileX(t1));
const uint dy = abs(TileY(t0) - TileY(t1));
return dx > dy ? dx : dy;
}
uint DistanceMaxPlusManhattan(TileIndex t0, TileIndex t1)
{
const uint dx = abs(TileX(t0) - TileX(t1));
const uint dy = abs(TileY(t0) - TileY(t1));
return dx > dy ? 2 * dx + dy : 2 * dy + dx;
}
uint DistanceTrack(TileIndex t0, TileIndex t1)
{
const uint dx = abs(TileX(t0) - TileX(t1));
const uint dy = abs(TileY(t0) - TileY(t1));
const uint straightTracks = 2 * min(dx, dy); /* The number of straight (not full length) tracks */
/* OPTIMISATION:
* Original: diagTracks = max(dx, dy) - min(dx,dy);
* Proof:
* (dx-dy) - straightTracks == (min + max) - straightTracks = min + // max - 2 * min = max - min */
const uint diagTracks = dx + dy - straightTracks; /* The number of diagonal (full tile length) tracks. */
return diagTracks + straightTracks * STRAIGHT_TRACK_LENGTH;
}
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;
const uint minl = xl < yl ? xl : yl;
const uint minh = xh < yh ? xh : yh;
return minl < minh ? minl : minh;
}