Changeset - r21202:3a70a357c1f1
[Not reviewed]
master
0 2 0
fonsinchen - 11 years ago 2014-02-02 14:53:26
fonsinchen@openttd.org
(svn r26289) -Codechange: Make tile areas and iterators more consistent to each other.
2 files changed with 128 insertions and 33 deletions:
0 comments (0 inline, 0 general)
src/tilearea.cpp
Show inline comments
 
@@ -18,8 +18,11 @@
 
 * @param start the start of the area
 
 * @param end   the end of the area
 
 */
 
TileArea::TileArea(TileIndex start, TileIndex end)
 
OrthogonalTileArea::OrthogonalTileArea(TileIndex start, TileIndex end)
 
{
 
	assert(start < MapSize());
 
	assert(end < MapSize());
 

	
 
	uint sx = TileX(start);
 
	uint sy = TileY(start);
 
	uint ex = TileX(end);
 
@@ -37,7 +40,7 @@ TileArea::TileArea(TileIndex start, Tile
 
 * Add a single tile to a tile area; enlarge if needed.
 
 * @param to_add The tile to add
 
 */
 
void TileArea::Add(TileIndex to_add)
 
void OrthogonalTileArea::Add(TileIndex to_add)
 
{
 
	if (this->tile == INVALID_TILE) {
 
		this->tile = to_add;
 
@@ -69,7 +72,7 @@ void TileArea::Add(TileIndex to_add)
 
 * @param ta the other tile area to check against.
 
 * @return true if they intersect.
 
 */
 
bool TileArea::Intersects(const TileArea &ta) const
 
bool OrthogonalTileArea::Intersects(const OrthogonalTileArea &ta) const
 
{
 
	if (ta.w == 0 || this->w == 0) return false;
 

	
 
@@ -98,7 +101,7 @@ bool TileArea::Intersects(const TileArea
 
 * @param tile Tile to test for.
 
 * @return True if the tile is inside the area.
 
 */
 
bool TileArea::Contains(TileIndex tile) const
 
bool OrthogonalTileArea::Contains(TileIndex tile) const
 
{
 
	if (this->w == 0) return false;
 

	
 
@@ -115,7 +118,7 @@ bool TileArea::Contains(TileIndex tile) 
 
/**
 
 * Clamp the tile area to map borders.
 
 */
 
void TileArea::ClampToMap()
 
void OrthogonalTileArea::ClampToMap()
 
{
 
	assert(this->tile < MapSize());
 
	this->w = min(this->w, MapSizeX() - TileX(this->tile));
 
@@ -123,42 +126,70 @@ void TileArea::ClampToMap()
 
}
 

	
 
/**
 
 * Construct the iterator.
 
 * @param corner1 Tile from where to begin iterating.
 
 * @param corner2 Tile where to end the iterating.
 
 * Create a diagonal tile area from two corners.
 
 * @param start First corner of the area.
 
 * @param end Second corner of the area.
 
 */
 
DiagonalTileIterator::DiagonalTileIterator(TileIndex corner1, TileIndex corner2) : TileIterator(corner2), base_x(TileX(corner2)), base_y(TileY(corner2)), a_cur(0), b_cur(0)
 
DiagonalTileArea::DiagonalTileArea(TileIndex start, TileIndex end) : tile(start)
 
{
 
	assert(corner1 < MapSize());
 
	assert(corner2 < MapSize());
 

	
 
	int dist_x = TileX(corner1) - TileX(corner2);
 
	int dist_y = TileY(corner1) - TileY(corner2);
 
	this->a_max = dist_x + dist_y;
 
	this->b_max = dist_y - dist_x;
 
	assert(start < MapSize());
 
	assert(end < MapSize());
 

	
 
	/* 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;
 
	 *
 
	 * We also need to increment here to have equality as marker for the end of a row or
 
	 * column. Like that it's shorter than having another if/else in operator++
 
	 */
 
	if (this->a_max > 0) {
 
		this->a_max++;
 
	 * 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++;
 
	} else {
 
		this->a_max--;
 
		this->a--;
 
	}
 

	
 
	if (this->b_max > 0) {
 
		this->b_max++;
 
	if (this->b > 0) {
 
		this->b++;
 
	} else {
 
		this->b_max--;
 
		this->b--;
 
	}
 
}
 

	
 
/**
 
 * 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);
 
}
 

	
 
/**
 
 * Move ourselves to the next tile in the rectangle on the map.
 
 */
 
TileIterator &DiagonalTileIterator::operator++()
src/tilearea_type.h
Show inline comments
 
@@ -15,7 +15,7 @@
 
#include "map_func.h"
 

	
 
/** Represents the covered area of e.g. a rail station */
 
struct TileArea {
 
struct OrthogonalTileArea {
 
	TileIndex tile; ///< The base tile of the area
 
	uint16 w;       ///< The width of the area
 
	uint16 h;       ///< The height of the area
 
@@ -26,10 +26,11 @@ struct TileArea {
 
	 * @param w the width
 
	 * @param h the height
 
	 */
 
	TileArea(TileIndex tile = INVALID_TILE, uint8 w = 0, uint8 h = 0) : tile(tile), w(w), h(h) {}
 
	OrthogonalTileArea(TileIndex tile = INVALID_TILE, uint8 w = 0, uint8 h = 0) : tile(tile), w(w), h(h)
 
	{
 
	}
 

	
 
	TileArea(TileIndex start, TileIndex end);
 

	
 
	OrthogonalTileArea(TileIndex start, TileIndex end);
 

	
 
	void Add(TileIndex to_add);
 

	
 
@@ -43,7 +44,7 @@ struct TileArea {
 
		this->h    = 0;
 
	}
 

	
 
	bool Intersects(const TileArea &ta) const;
 
	bool Intersects(const OrthogonalTileArea &ta) const;
 

	
 
	bool Contains(TileIndex tile) const;
 

	
 
@@ -59,6 +60,41 @@ struct TileArea {
 
	}
 
};
 

	
 
/** Represents a diagonal tile area. */
 
struct DiagonalTileArea {
 

	
 
	TileIndex tile; ///< Base tile of the area
 
	int16 a;        ///< Extent in diagonal "x" direction (may be negative to signify the area stretches to the left)
 
	int16 b;        ///< Extent in diagonal "y" direction (may be negative to signify the area stretches upwards)
 

	
 
	/**
 
	 * Construct this tile area with some set values.
 
	 * @param tile The base tile.
 
	 * @param a The "x" extent.
 
	 * @param b The "y" estent.
 
	 */
 
	DiagonalTileArea(TileIndex tile = INVALID_TILE, int8 a = 0, int8 b = 0) : tile(tile), a(a), b(b)
 
	{
 
	}
 

	
 
	DiagonalTileArea(TileIndex start, TileIndex end);
 

	
 
	/**
 
	 * Clears the TileArea by making the tile invalid and setting a and b to 0.
 
	 */
 
	void Clear()
 
	{
 
		this->tile = INVALID_TILE;
 
		this->a    = 0;
 
		this->b    = 0;
 
	}
 

	
 
	bool Contains(TileIndex tile) const;
 
};
 

	
 
/** Shorthand for the much more common orthogonal tile area. */
 
typedef OrthogonalTileArea TileArea;
 

	
 
/** Base class for tile iterators. */
 
class TileIterator {
 
protected:
 
@@ -68,7 +104,7 @@ protected:
 
	 * Initialise the iterator starting at this tile.
 
	 * @param tile The tile we start iterating from.
 
	 */
 
	TileIterator(TileIndex tile) : tile(tile)
 
	TileIterator(TileIndex tile = INVALID_TILE) : tile(tile)
 
	{
 
	}
 

	
 
@@ -110,11 +146,21 @@ public:
 
	 * Construct the iterator.
 
	 * @param ta Area, i.e. begin point and width/height of to-be-iterated area.
 
	 */
 
	OrthogonalTileIterator(const TileArea &ta) : TileIterator(ta.w == 0 || ta.h == 0 ? INVALID_TILE : ta.tile), w(ta.w), x(ta.w), y(ta.h)
 
	OrthogonalTileIterator(const OrthogonalTileArea &ta) : TileIterator(ta.w == 0 || ta.h == 0 ? INVALID_TILE : ta.tile), w(ta.w), x(ta.w), y(ta.h)
 
	{
 
	}
 

	
 
	/**
 
	 * Construct the iterator.
 
	 * @param corner1 Tile from where to begin iterating.
 
	 * @param corner2 Tile where to end the iterating.
 
	 */
 
	OrthogonalTileIterator(TileIndex corner1, TileIndex corner2)
 
	{
 
		*this = OrthogonalTileIterator(OrthogonalTileArea(corner1, corner2));
 
	}
 

	
 
	/**
 
	 * Move ourselves to the next tile in the rectangle on the map.
 
	 */
 
	inline TileIterator& operator ++()
 
@@ -149,7 +195,25 @@ private:
 
	int b_max;   ///< The (rotated) y coordinate of the end of the iteration.
 

	
 
public:
 
	DiagonalTileIterator(TileIndex begin, TileIndex end);
 

	
 
	/**
 
	 * Construct the iterator.
 
	 * @param ta Area, i.e. begin point and (diagonal) width/height of to-be-iterated area.
 
	 */
 
	DiagonalTileIterator(const DiagonalTileArea &ta) :
 
		TileIterator(ta.tile), base_x(TileX(ta.tile)), base_y(TileY(ta.tile)), a_cur(0), b_cur(0), a_max(ta.a), b_max(ta.b)
 
	{
 
	}
 

	
 
	/**
 
	 * Construct the iterator.
 
	 * @param corner1 Tile from where to begin iterating.
 
	 * @param corner2 Tile where to end the iterating.
 
	 */
 
	DiagonalTileIterator(TileIndex corner1, TileIndex corner2)
 
	{
 
		*this = DiagonalTileIterator(DiagonalTileArea(corner1, corner2));
 
	}
 

	
 
	TileIterator& operator ++();
 

	
0 comments (0 inline, 0 general)