Add utility function: TileAddSaturating
This commit is contained in:
20
src/map.cpp
20
src/map.cpp
@@ -146,6 +146,26 @@ TileIndex TileAddWrap(TileIndex tile, int addx, int addy)
|
|||||||
return TileXY(x, y);
|
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 */
|
/** 'Lookup table' for tile offsets given a DiagDirection */
|
||||||
extern const TileIndexDiffC _tileoffs_by_diagdir[] = {
|
extern const TileIndexDiffC _tileoffs_by_diagdir[] = {
|
||||||
{-1, 0}, ///< DIAGDIR_NE
|
{-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))
|
#define TILE_ADDXY(tile, x, y) TILE_ADD(tile, TileDiffXY(x, y))
|
||||||
|
|
||||||
TileIndex TileAddWrap(TileIndex tile, int addx, int addy);
|
TileIndex TileAddWrap(TileIndex tile, int addx, int addy);
|
||||||
|
TileIndex TileAddSaturating(TileIndex tile, int addx, int addy);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the TileIndexDiffC offset from a DiagDirection.
|
* Returns the TileIndexDiffC offset from a DiagDirection.
|
||||||
|
Reference in New Issue
Block a user