Codechange: Replace duplicated code with TileArea::Expand() (#7467)

This commit is contained in:
2019-04-13 14:12:34 +01:00
committed by GitHub
parent 801cbea9cc
commit abe8cf4985
7 changed files with 39 additions and 56 deletions

View File

@@ -117,6 +117,27 @@ bool OrthogonalTileArea::Contains(TileIndex tile) const
return IsInsideBS(tile_x, left, this->w) && IsInsideBS(tile_y, top, this->h);
}
/**
* 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;
}
/**
* Clamp the tile area to map borders.
*/