Fix possible incorrect tile index in FindNearestHangar

See also: https://github.com/OpenTTD/OpenTTD/issues/7619
This commit is contained in:
Jonathan G Rennison
2019-11-13 22:16:02 +00:00
parent bbd487a2a7
commit b07a1f8426
2 changed files with 15 additions and 4 deletions

View File

@@ -124,7 +124,7 @@ static StationID FindNearestHangar(const Aircraft *v)
const Station *st;
uint best = 0;
StationID index = INVALID_STATION;
TileIndex vtile = TileVirtXY(v->x_pos, v->y_pos);
TileIndex vtile = TileVirtXYClampedToMap(v->x_pos, v->y_pos);
const AircraftVehicleInfo *avi = AircraftVehInfo(v->engine_type);
uint max_range = v->acache.cached_max_range_sqr;
@@ -714,9 +714,7 @@ static int UpdateAircraftSpeed(Aircraft *v, uint speed_limit = SPEED_LIMIT_NONE,
*/
int GetTileHeightBelowAircraft(const Vehicle *v)
{
int safe_x = Clamp(v->x_pos, 0, MapMaxX() * TILE_SIZE);
int safe_y = Clamp(v->y_pos, 0, MapMaxY() * TILE_SIZE);
return TileHeight(TileVirtXY(safe_x, safe_y)) * TILE_HEIGHT;
return TileHeight(TileVirtXYClampedToMap(v->x_pos, v->y_pos)) * TILE_HEIGHT;
}
/**

View File

@@ -197,6 +197,19 @@ static inline TileIndex TileVirtXY(uint x, uint y)
return (y >> 4 << MapLogX()) + (x >> 4);
}
/**
* Get a tile from the virtual XY-coordinate.
* This is clamped to be within the map bounds.
* @param x The virtual x coordinate of the tile.
* @param y The virtual y coordinate of the tile.
* @return The TileIndex calculated by the coordinate.
*/
static inline TileIndex TileVirtXYClampedToMap(int x, int y)
{
int safe_x = Clamp<int>(x, 0, MapMaxX() * TILE_SIZE);
int safe_y = Clamp<int>(y, 0, MapMaxY() * TILE_SIZE);
return TileVirtXY((uint) safe_x, (uint) safe_y);
}
/**
* Get the X component of a tile