2010-02-06 12:56:13 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file tilearea.cpp Handling of tile areas. */
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
|
|
|
|
#include "tilearea_type.h"
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2010-02-06 12:56:13 +00:00
|
|
|
/**
|
|
|
|
* Construct this tile area based on two points.
|
|
|
|
* @param start the start of the area
|
|
|
|
* @param end the end of the area
|
|
|
|
*/
|
2014-02-02 14:53:26 +00:00
|
|
|
OrthogonalTileArea::OrthogonalTileArea(TileIndex start, TileIndex end)
|
2010-02-06 12:56:13 +00:00
|
|
|
{
|
2014-02-02 14:53:26 +00:00
|
|
|
assert(start < MapSize());
|
|
|
|
assert(end < MapSize());
|
|
|
|
|
2010-02-06 12:56:13 +00:00
|
|
|
uint sx = TileX(start);
|
|
|
|
uint sy = TileY(start);
|
|
|
|
uint ex = TileX(end);
|
|
|
|
uint ey = TileY(end);
|
|
|
|
|
|
|
|
if (sx > ex) Swap(sx, ex);
|
|
|
|
if (sy > ey) Swap(sy, ey);
|
|
|
|
|
|
|
|
this->tile = TileXY(sx, sy);
|
|
|
|
this->w = ex - sx + 1;
|
|
|
|
this->h = ey - sy + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a single tile to a tile area; enlarge if needed.
|
|
|
|
* @param to_add The tile to add
|
|
|
|
*/
|
2014-02-02 14:53:26 +00:00
|
|
|
void OrthogonalTileArea::Add(TileIndex to_add)
|
2010-02-06 12:56:13 +00:00
|
|
|
{
|
|
|
|
if (this->tile == INVALID_TILE) {
|
|
|
|
this->tile = to_add;
|
|
|
|
this->w = 1;
|
|
|
|
this->h = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint sx = TileX(this->tile);
|
|
|
|
uint sy = TileY(this->tile);
|
|
|
|
uint ex = sx + this->w - 1;
|
|
|
|
uint ey = sy + this->h - 1;
|
|
|
|
|
|
|
|
uint ax = TileX(to_add);
|
|
|
|
uint ay = TileY(to_add);
|
|
|
|
|
|
|
|
sx = min(ax, sx);
|
|
|
|
sy = min(ay, sy);
|
|
|
|
ex = max(ax, ex);
|
|
|
|
ey = max(ay, ey);
|
|
|
|
|
|
|
|
this->tile = TileXY(sx, sy);
|
|
|
|
this->w = ex - sx + 1;
|
|
|
|
this->h = ey - sy + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does this tile area intersect with another?
|
|
|
|
* @param ta the other tile area to check against.
|
|
|
|
* @return true if they intersect.
|
|
|
|
*/
|
2014-02-02 14:53:26 +00:00
|
|
|
bool OrthogonalTileArea::Intersects(const OrthogonalTileArea &ta) const
|
2010-02-06 12:56:13 +00:00
|
|
|
{
|
|
|
|
if (ta.w == 0 || this->w == 0) return false;
|
|
|
|
|
|
|
|
assert(ta.w != 0 && ta.h != 0 && this->w != 0 && this->h != 0);
|
|
|
|
|
|
|
|
uint left1 = TileX(this->tile);
|
|
|
|
uint top1 = TileY(this->tile);
|
|
|
|
uint right1 = left1 + this->w - 1;
|
|
|
|
uint bottom1 = top1 + this->h - 1;
|
|
|
|
|
|
|
|
uint left2 = TileX(ta.tile);
|
|
|
|
uint top2 = TileY(ta.tile);
|
|
|
|
uint right2 = left2 + ta.w - 1;
|
|
|
|
uint bottom2 = top2 + ta.h - 1;
|
|
|
|
|
|
|
|
return !(
|
|
|
|
left2 > right1 ||
|
|
|
|
right2 < left1 ||
|
|
|
|
top2 > bottom1 ||
|
|
|
|
bottom2 < top1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-12-03 20:19:33 +00:00
|
|
|
/**
|
|
|
|
* Does this tile area contain a tile?
|
|
|
|
* @param tile Tile to test for.
|
|
|
|
* @return True if the tile is inside the area.
|
|
|
|
*/
|
2014-02-02 14:53:26 +00:00
|
|
|
bool OrthogonalTileArea::Contains(TileIndex tile) const
|
2011-12-03 20:19:33 +00:00
|
|
|
{
|
|
|
|
if (this->w == 0) return false;
|
|
|
|
|
|
|
|
assert(this->w != 0 && this->h != 0);
|
|
|
|
|
|
|
|
uint left = TileX(this->tile);
|
|
|
|
uint top = TileY(this->tile);
|
|
|
|
uint tile_x = TileX(tile);
|
|
|
|
uint tile_y = TileY(tile);
|
|
|
|
|
|
|
|
return IsInsideBS(tile_x, left, this->w) && IsInsideBS(tile_y, top, this->h);
|
|
|
|
}
|
|
|
|
|
2019-04-13 13:12:34 +00:00
|
|
|
/**
|
|
|
|
* Expand a tile area by rad tiles in each direction, keeping within map bounds.
|
|
|
|
* @param rad Number of tiles to expand
|
|
|
|
* @return The OrthogonalTileArea.
|
|
|
|
*/
|
|
|
|
OrthogonalTileArea &OrthogonalTileArea::Expand(int rad)
|
|
|
|
{
|
|
|
|
int x = TileX(this->tile);
|
|
|
|
int y = TileY(this->tile);
|
|
|
|
|
|
|
|
int sx = max(x - rad, 0);
|
|
|
|
int sy = max(y - rad, 0);
|
|
|
|
int ex = min(x + this->w + rad, MapSizeX());
|
|
|
|
int ey = min(y + this->h + rad, MapSizeY());
|
|
|
|
|
|
|
|
this->tile = TileXY(sx, sy);
|
|
|
|
this->w = ex - sx;
|
|
|
|
this->h = ey - sy;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2010-02-06 13:19:46 +00:00
|
|
|
/**
|
|
|
|
* Clamp the tile area to map borders.
|
|
|
|
*/
|
2014-02-02 14:53:26 +00:00
|
|
|
void OrthogonalTileArea::ClampToMap()
|
2010-02-06 13:19:46 +00:00
|
|
|
{
|
|
|
|
assert(this->tile < MapSize());
|
|
|
|
this->w = min(this->w, MapSizeX() - TileX(this->tile));
|
|
|
|
this->h = min(this->h, MapSizeY() - TileY(this->tile));
|
|
|
|
}
|
|
|
|
|
2011-01-18 21:58:22 +00:00
|
|
|
/**
|
2014-02-02 14:53:26 +00:00
|
|
|
* Create a diagonal tile area from two corners.
|
|
|
|
* @param start First corner of the area.
|
|
|
|
* @param end Second corner of the area.
|
2011-01-18 21:58:22 +00:00
|
|
|
*/
|
2014-02-02 14:53:26 +00:00
|
|
|
DiagonalTileArea::DiagonalTileArea(TileIndex start, TileIndex end) : tile(start)
|
2010-12-13 15:13:05 +00:00
|
|
|
{
|
2014-02-02 14:53:26 +00:00
|
|
|
assert(start < MapSize());
|
|
|
|
assert(end < MapSize());
|
2010-12-13 15:13:05 +00:00
|
|
|
|
|
|
|
/* Unfortunately we can't find a new base and make all a and b positive because
|
|
|
|
* the new base might be a "flattened" corner where there actually is no single
|
|
|
|
* tile. If we try anyway the result is either inaccurate ("one off" half of the
|
|
|
|
* time) or the code gets much more complex;
|
|
|
|
*
|
2014-02-02 14:53:26 +00:00
|
|
|
* We also need to increment/decrement a and b here to have one-past-end semantics
|
|
|
|
* for a and b, just the way the orthogonal tile area does it for w and h. */
|
|
|
|
|
|
|
|
this->a = TileY(end) + TileX(end) - TileY(start) - TileX(start);
|
|
|
|
this->b = TileY(end) - TileX(end) - TileY(start) + TileX(start);
|
|
|
|
if (this->a > 0) {
|
|
|
|
this->a++;
|
2010-12-13 15:13:05 +00:00
|
|
|
} else {
|
2014-02-02 14:53:26 +00:00
|
|
|
this->a--;
|
2010-12-13 15:13:05 +00:00
|
|
|
}
|
|
|
|
|
2014-02-02 14:53:26 +00:00
|
|
|
if (this->b > 0) {
|
|
|
|
this->b++;
|
2010-12-13 15:13:05 +00:00
|
|
|
} else {
|
2014-02-02 14:53:26 +00:00
|
|
|
this->b--;
|
2010-12-13 15:13:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-02 14:53:26 +00:00
|
|
|
/**
|
|
|
|
* Does this tile area contain a tile?
|
|
|
|
* @param tile Tile to test for.
|
|
|
|
* @return True if the tile is inside the area.
|
|
|
|
*/
|
|
|
|
bool DiagonalTileArea::Contains(TileIndex tile) const
|
|
|
|
{
|
|
|
|
int a = TileY(tile) + TileX(tile);
|
|
|
|
int b = TileY(tile) - TileX(tile);
|
|
|
|
|
|
|
|
int start_a = TileY(this->tile) + TileX(this->tile);
|
|
|
|
int start_b = TileY(this->tile) - TileX(this->tile);
|
|
|
|
|
|
|
|
int end_a = start_a + this->a;
|
|
|
|
int end_b = start_b + this->b;
|
|
|
|
|
|
|
|
/* Swap if necessary, preserving the "one past end" semantics. */
|
|
|
|
if (start_a > end_a) {
|
|
|
|
int tmp = start_a;
|
|
|
|
start_a = end_a + 1;
|
|
|
|
end_a = tmp + 1;
|
|
|
|
}
|
|
|
|
if (start_b > end_b) {
|
|
|
|
int tmp = start_b;
|
|
|
|
start_b = end_b + 1;
|
|
|
|
end_b = tmp + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (a >= start_a && a < end_a && b >= start_b && b < end_b);
|
|
|
|
}
|
|
|
|
|
2011-01-18 21:58:22 +00:00
|
|
|
/**
|
2013-01-08 22:46:42 +00:00
|
|
|
* Move ourselves to the next tile in the rectangle on the map.
|
2011-01-18 21:58:22 +00:00
|
|
|
*/
|
2010-12-13 15:13:05 +00:00
|
|
|
TileIterator &DiagonalTileIterator::operator++()
|
|
|
|
{
|
|
|
|
assert(this->tile != INVALID_TILE);
|
|
|
|
|
2011-01-11 16:45:45 +00:00
|
|
|
/* Determine the next tile, while clipping at map borders */
|
2011-01-09 14:55:22 +00:00
|
|
|
bool new_line = false;
|
2010-12-13 15:13:05 +00:00
|
|
|
do {
|
|
|
|
/* Iterate using the rotated coordinates. */
|
2011-01-11 16:45:45 +00:00
|
|
|
if (this->a_max == 1 || this->a_max == -1) {
|
|
|
|
/* Special case: Every second column has zero length, skip them completely */
|
|
|
|
this->a_cur = 0;
|
2010-12-13 15:13:05 +00:00
|
|
|
if (this->b_max > 0) {
|
2011-01-11 16:45:45 +00:00
|
|
|
this->b_cur = min(this->b_cur + 2, this->b_max);
|
2010-12-13 15:13:05 +00:00
|
|
|
} else {
|
2011-01-11 16:45:45 +00:00
|
|
|
this->b_cur = max(this->b_cur - 2, this->b_max);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Every column has at least one tile to process */
|
|
|
|
if (this->a_max > 0) {
|
|
|
|
this->a_cur += 2;
|
|
|
|
new_line = this->a_cur >= this->a_max;
|
|
|
|
} else {
|
|
|
|
this->a_cur -= 2;
|
|
|
|
new_line = this->a_cur <= this->a_max;
|
|
|
|
}
|
|
|
|
if (new_line) {
|
|
|
|
/* offset of initial a_cur: one tile in the same direction as a_max
|
|
|
|
* every second line.
|
|
|
|
*/
|
|
|
|
this->a_cur = abs(this->a_cur) % 2 ? 0 : (this->a_max > 0 ? 1 : -1);
|
|
|
|
|
|
|
|
if (this->b_max > 0) {
|
|
|
|
++this->b_cur;
|
|
|
|
} else {
|
|
|
|
--this->b_cur;
|
|
|
|
}
|
2010-12-13 15:13:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And convert the coordinates back once we've gone to the next tile. */
|
|
|
|
uint x = this->base_x + (this->a_cur - this->b_cur) / 2;
|
|
|
|
uint y = this->base_y + (this->b_cur + this->a_cur) / 2;
|
|
|
|
/* Prevent wrapping around the map's borders. */
|
|
|
|
this->tile = x >= MapSizeX() || y >= MapSizeY() ? INVALID_TILE : TileXY(x, y);
|
|
|
|
} while (this->tile > MapSize() && this->b_max != this->b_cur);
|
|
|
|
|
|
|
|
if (this->b_max == this->b_cur) this->tile = INVALID_TILE;
|
|
|
|
return *this;
|
|
|
|
}
|