Add utility function: TileAddSaturating

pull/91/head
Jonathan G Rennison 5 years ago
parent 8a3dcfb1c5
commit 09edd07003

@ -146,6 +146,26 @@ TileIndex TileAddWrap(TileIndex tile, int addx, int addy)
return TileXY(x, y);
}
/**
* This function checks if we add addx/addy to tile, if we
* do wrap around the edges. Instead of wrapping, saturate at the map edge.
*
* @param tile the 'starting' point of the adding
* @param addx the amount of tiles in the X direction to add
* @param addy the amount of tiles in the Y direction to add
* @return translated tile
*/
TileIndex TileAddSaturating(TileIndex tile, int addx, int addy)
{
int x = TileX(tile) + addx;
int y = TileY(tile) + addy;
auto clamp = [&](int coord, int map_max) -> uint {
return Clamp<int>(coord, _settings_game.construction.freeform_edges ? 1 : 0, map_max - 1);
};
return TileXY(clamp(x, MapMaxX()), clamp(y, MapMaxY()));
}
/** 'Lookup table' for tile offsets given a DiagDirection */
extern const TileIndexDiffC _tileoffs_by_diagdir[] = {
{-1, 0}, ///< DIAGDIR_NE

@ -261,6 +261,7 @@ static inline TileIndexDiff ToTileIndexDiff(TileIndexDiffC tidc)
#define TILE_ADDXY(tile, x, y) TILE_ADD(tile, TileDiffXY(x, y))
TileIndex TileAddWrap(TileIndex tile, int addx, int addy);
TileIndex TileAddSaturating(TileIndex tile, int addx, int addy);
/**
* Returns the TileIndexDiffC offset from a DiagDirection.

Loading…
Cancel
Save