mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Check airport layout would fit within map bounds before iterating tiles. (#7429)
parent
32fda83d39
commit
e1069eee05
|
@ -130,6 +130,24 @@ bool AirportSpec::IsAvailable() const
|
||||||
return _cur_year <= this->max_year;
|
return _cur_year <= this->max_year;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the airport would be within the map bounds at the given tile.
|
||||||
|
* @param table Selected layout table. This affects airport rotation, and therefore dimenions.
|
||||||
|
* @param tile Top corner of the airport.
|
||||||
|
* @return true iff the airport would be within the map bounds at the given tile.
|
||||||
|
*/
|
||||||
|
bool AirportSpec::IsWithinMapBounds(byte table, TileIndex tile) const
|
||||||
|
{
|
||||||
|
if (table >= this->num_table) return false;
|
||||||
|
|
||||||
|
byte w = this->size_x;
|
||||||
|
byte h = this->size_y;
|
||||||
|
if (this->rotation[table] == DIR_E || this->rotation[table] == DIR_W) Swap(w, h);
|
||||||
|
|
||||||
|
return TileX(tile) + w < MapSizeX() &&
|
||||||
|
TileY(tile) + h < MapSizeY();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function initializes the airportspec array.
|
* This function initializes the airportspec array.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -123,6 +123,7 @@ struct AirportSpec {
|
||||||
static AirportSpec *GetWithoutOverride(byte type);
|
static AirportSpec *GetWithoutOverride(byte type);
|
||||||
|
|
||||||
bool IsAvailable() const;
|
bool IsAvailable() const;
|
||||||
|
bool IsWithinMapBounds(byte table, TileIndex index) const;
|
||||||
|
|
||||||
static void ResetAirports();
|
static void ResetAirports();
|
||||||
|
|
||||||
|
|
|
@ -136,8 +136,10 @@
|
||||||
if (!::IsValidTile(tile)) return -1;
|
if (!::IsValidTile(tile)) return -1;
|
||||||
if (!IsAirportInformationAvailable(type)) return -1;
|
if (!IsAirportInformationAvailable(type)) return -1;
|
||||||
|
|
||||||
if (_settings_game.economy.station_noise_level) {
|
|
||||||
const AirportSpec *as = ::AirportSpec::Get(type);
|
const AirportSpec *as = ::AirportSpec::Get(type);
|
||||||
|
if (!as->IsWithinMapBounds(0, tile)) return -1;
|
||||||
|
|
||||||
|
if (_settings_game.economy.station_noise_level) {
|
||||||
AirportTileTableIterator it(as->table[0], tile);
|
AirportTileTableIterator it(as->table[0], tile);
|
||||||
uint dist;
|
uint dist;
|
||||||
AirportGetNearestTown(as, it, dist);
|
AirportGetNearestTown(as, it, dist);
|
||||||
|
@ -155,6 +157,8 @@
|
||||||
if (!IsAirportInformationAvailable(type)) return INVALID_TOWN;
|
if (!IsAirportInformationAvailable(type)) return INVALID_TOWN;
|
||||||
|
|
||||||
const AirportSpec *as = AirportSpec::Get(type);
|
const AirportSpec *as = AirportSpec::Get(type);
|
||||||
|
if (!as->IsWithinMapBounds(0, tile)) return INVALID_TOWN;
|
||||||
|
|
||||||
uint dist;
|
uint dist;
|
||||||
return AirportGetNearestTown(as, AirportTileTableIterator(as->table[0], tile), dist)->index;
|
return AirportGetNearestTown(as, AirportTileTableIterator(as->table[0], tile), dist)->index;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2268,6 +2268,7 @@ CommandCost CmdBuildAirport(TileIndex tile, DoCommandFlag flags, uint32 p1, uint
|
||||||
/* Check if a valid, buildable airport was chosen for construction */
|
/* Check if a valid, buildable airport was chosen for construction */
|
||||||
const AirportSpec *as = AirportSpec::Get(airport_type);
|
const AirportSpec *as = AirportSpec::Get(airport_type);
|
||||||
if (!as->IsAvailable() || layout >= as->num_table) return CMD_ERROR;
|
if (!as->IsAvailable() || layout >= as->num_table) return CMD_ERROR;
|
||||||
|
if (!as->IsWithinMapBounds(layout, tile)) return CMD_ERROR;
|
||||||
|
|
||||||
Direction rotation = as->rotation[layout];
|
Direction rotation = as->rotation[layout];
|
||||||
int w = as->size_x;
|
int w = as->size_x;
|
||||||
|
|
Loading…
Reference in New Issue