mirror of https://github.com/OpenTTD/OpenTTD.git
(svn r21490) -Codechange: abstract/encapsulate the map area iterating a bit
This commit is contained in:
parent
e0351ab414
commit
cdd35c6a51
|
@ -159,7 +159,6 @@ Industry::~Industry()
|
||||||
|
|
||||||
/* Remove the farmland and convert it to regular tiles over time. */
|
/* Remove the farmland and convert it to regular tiles over time. */
|
||||||
TILE_AREA_LOOP(tile_cur, ta) {
|
TILE_AREA_LOOP(tile_cur, ta) {
|
||||||
tile_cur = TILE_MASK(tile_cur);
|
|
||||||
if (IsTileType(tile_cur, MP_CLEAR) && IsClearGround(tile_cur, CLEAR_FIELDS) &&
|
if (IsTileType(tile_cur, MP_CLEAR) && IsClearGround(tile_cur, CLEAR_FIELDS) &&
|
||||||
GetIndustryIndexOfField(tile_cur) == this->index) {
|
GetIndustryIndexOfField(tile_cur) == this->index) {
|
||||||
SetIndustryIndexOfField(tile_cur, INVALID_INDUSTRY);
|
SetIndustryIndexOfField(tile_cur, INVALID_INDUSTRY);
|
||||||
|
|
|
@ -333,30 +333,6 @@ uint DistanceMaxPlusManhattan(TileIndex, TileIndex); ///< Max + Manhattan
|
||||||
uint DistanceFromEdge(TileIndex); ///< shortest distance from any edge of the map
|
uint DistanceFromEdge(TileIndex); ///< shortest distance from any edge of the map
|
||||||
uint DistanceFromEdgeDir(TileIndex, DiagDirection); ///< distance from the map edge in given direction
|
uint DistanceFromEdgeDir(TileIndex, DiagDirection); ///< distance from the map edge in given direction
|
||||||
|
|
||||||
/**
|
|
||||||
* A loop which iterates to a square of tiles
|
|
||||||
*
|
|
||||||
* This macro starts 2 nested loops which iterates over a square of tiles.
|
|
||||||
*
|
|
||||||
* @param var The name of the variable which contains the current tile
|
|
||||||
* @param w The width (x-width) of the square
|
|
||||||
* @param h The heigth (y-width) of the square
|
|
||||||
* @param tile The start tile of the square
|
|
||||||
*/
|
|
||||||
#define TILE_LOOP(var, w, h, tile) \
|
|
||||||
for (uint var = tile, cur_h = (h); cur_h > 0; --cur_h, var += TileDiffXY(0, 1) - (w)) \
|
|
||||||
for (uint cur_w = (w); cur_w > 0; --cur_w, var++)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A loop which iterates over the tiles of a TileArea
|
|
||||||
*
|
|
||||||
* This macro starts 2 nested loops which iterates over a square of tiles.
|
|
||||||
*
|
|
||||||
* @param var The name of the variable which contains the current tile
|
|
||||||
* @param ta The tile area to search over
|
|
||||||
*/
|
|
||||||
#define TILE_AREA_LOOP(var, ta) TILE_LOOP(var, ta.w, ta.h, ta.tile)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert a DiagDirection to a TileIndexDiff
|
* Convert a DiagDirection to a TileIndexDiff
|
||||||
*
|
*
|
||||||
|
|
|
@ -60,4 +60,58 @@ struct TileArea {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** Iterator to iterate over a tile area (rectangle) of the map. */
|
||||||
|
class TileIterator {
|
||||||
|
private:
|
||||||
|
TileIndex tile; ///< The current tile we are at.
|
||||||
|
int w; ///< The width of the iterated area.
|
||||||
|
int x; ///< The current 'x' position in the rectangle.
|
||||||
|
int y; ///< The current 'y' position in the rectangle.
|
||||||
|
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* Construct the iterator.
|
||||||
|
* @param ta Area, i.e. begin point and width/height of to-be-iterated area.
|
||||||
|
*/
|
||||||
|
TileIterator(const TileArea &ta) : tile(ta.tile), w(ta.w), x(ta.w), y(ta.h)
|
||||||
|
{
|
||||||
|
if (ta.w == 0 || ta.h == 0) this->tile = INVALID_TILE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the tile we are currently at.
|
||||||
|
* @return The tile we are at, or INVALID_TILE when we're done.
|
||||||
|
*/
|
||||||
|
FORCEINLINE operator TileIndex () const
|
||||||
|
{
|
||||||
|
return this->tile;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Move ourselves to the next tile in the rectange on the map.
|
||||||
|
*/
|
||||||
|
FORCEINLINE TileIterator& operator ++()
|
||||||
|
{
|
||||||
|
assert(this->tile != INVALID_TILE);
|
||||||
|
|
||||||
|
if (--this->x > 0) {
|
||||||
|
this->tile++;
|
||||||
|
} else if (--this->y > 0) {
|
||||||
|
this->x = this->w;
|
||||||
|
this->tile += TileDiffXY(1, 1) - this->w;
|
||||||
|
} else {
|
||||||
|
this->tile = INVALID_TILE;
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A loop which iterates over the tiles of a TileArea.
|
||||||
|
* @param var The name of the variable which contains the current tile.
|
||||||
|
* This variable will be allocated in this \c for of this loop.
|
||||||
|
* @param ta The tile area to search over.
|
||||||
|
*/
|
||||||
|
#define TILE_AREA_LOOP(var, ta) for (TileIterator var(ta); var != INVALID_TILE; ++var)
|
||||||
|
|
||||||
#endif /* TILEAREA_TYPE_H */
|
#endif /* TILEAREA_TYPE_H */
|
||||||
|
|
Loading…
Reference in New Issue