(svn r26289) -Codechange: Make tile areas and iterators more consistent to each other.

This commit is contained in:
fonsinchen 2014-02-02 14:53:26 +00:00
parent 1fc589bd2d
commit 547e521381
2 changed files with 128 additions and 33 deletions

View File

@ -18,8 +18,11 @@
* @param start the start of the area * @param start the start of the area
* @param end the end 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 sx = TileX(start);
uint sy = TileY(start); uint sy = TileY(start);
uint ex = TileX(end); uint ex = TileX(end);
@ -37,7 +40,7 @@ TileArea::TileArea(TileIndex start, TileIndex end)
* Add a single tile to a tile area; enlarge if needed. * Add a single tile to a tile area; enlarge if needed.
* @param to_add The tile to add * @param to_add The tile to add
*/ */
void TileArea::Add(TileIndex to_add) void OrthogonalTileArea::Add(TileIndex to_add)
{ {
if (this->tile == INVALID_TILE) { if (this->tile == INVALID_TILE) {
this->tile = to_add; this->tile = to_add;
@ -69,7 +72,7 @@ void TileArea::Add(TileIndex to_add)
* @param ta the other tile area to check against. * @param ta the other tile area to check against.
* @return true if they intersect. * @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; if (ta.w == 0 || this->w == 0) return false;
@ -98,7 +101,7 @@ bool TileArea::Intersects(const TileArea &ta) const
* @param tile Tile to test for. * @param tile Tile to test for.
* @return True if the tile is inside the area. * @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; if (this->w == 0) return false;
@ -115,7 +118,7 @@ bool TileArea::Contains(TileIndex tile) const
/** /**
* Clamp the tile area to map borders. * Clamp the tile area to map borders.
*/ */
void TileArea::ClampToMap() void OrthogonalTileArea::ClampToMap()
{ {
assert(this->tile < MapSize()); assert(this->tile < MapSize());
this->w = min(this->w, MapSizeX() - TileX(this->tile)); this->w = min(this->w, MapSizeX() - TileX(this->tile));
@ -123,41 +126,69 @@ void TileArea::ClampToMap()
} }
/** /**
* Construct the iterator. * Create a diagonal tile area from two corners.
* @param corner1 Tile from where to begin iterating. * @param start First corner of the area.
* @param corner2 Tile where to end the iterating. * @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(start < MapSize());
assert(corner2 < MapSize()); assert(end < 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;
/* Unfortunately we can't find a new base and make all a and b positive because /* 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 * 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 * tile. If we try anyway the result is either inaccurate ("one off" half of the
* time) or the code gets much more complex; * 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 * We also need to increment/decrement a and b here to have one-past-end semantics
* column. Like that it's shorter than having another if/else in operator++ * for a and b, just the way the orthogonal tile area does it for w and h. */
*/
if (this->a_max > 0) { this->a = TileY(end) + TileX(end) - TileY(start) - TileX(start);
this->a_max++; this->b = TileY(end) - TileX(end) - TileY(start) + TileX(start);
if (this->a > 0) {
this->a++;
} else { } else {
this->a_max--; this->a--;
} }
if (this->b_max > 0) { if (this->b > 0) {
this->b_max++; this->b++;
} else { } 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. * Move ourselves to the next tile in the rectangle on the map.
*/ */

View File

@ -15,7 +15,7 @@
#include "map_func.h" #include "map_func.h"
/** Represents the covered area of e.g. a rail station */ /** Represents the covered area of e.g. a rail station */
struct TileArea { struct OrthogonalTileArea {
TileIndex tile; ///< The base tile of the area TileIndex tile; ///< The base tile of the area
uint16 w; ///< The width of the area uint16 w; ///< The width of the area
uint16 h; ///< The height of the area uint16 h; ///< The height of the area
@ -26,10 +26,11 @@ struct TileArea {
* @param w the width * @param w the width
* @param h the height * @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); void Add(TileIndex to_add);
@ -43,7 +44,7 @@ struct TileArea {
this->h = 0; this->h = 0;
} }
bool Intersects(const TileArea &ta) const; bool Intersects(const OrthogonalTileArea &ta) const;
bool Contains(TileIndex tile) 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. */ /** Base class for tile iterators. */
class TileIterator { class TileIterator {
protected: protected:
@ -68,7 +104,7 @@ protected:
* Initialise the iterator starting at this tile. * Initialise the iterator starting at this tile.
* @param tile The tile we start iterating from. * @param tile The tile we start iterating from.
*/ */
TileIterator(TileIndex tile) : tile(tile) TileIterator(TileIndex tile = INVALID_TILE) : tile(tile)
{ {
} }
@ -110,10 +146,20 @@ public:
* Construct the iterator. * Construct the iterator.
* @param ta Area, i.e. begin point and width/height of to-be-iterated area. * @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. * Move ourselves to the next tile in the rectangle on the map.
*/ */
@ -149,7 +195,25 @@ private:
int b_max; ///< The (rotated) y coordinate of the end of the iteration. int b_max; ///< The (rotated) y coordinate of the end of the iteration.
public: 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 ++(); TileIterator& operator ++();