Codechange: Check airport layout would fit within map bounds before iterating tiles. (#7429)

pull/82/head
PeterN 5 years ago committed by GitHub
parent 32fda83d39
commit e1069eee05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -130,6 +130,24 @@ bool AirportSpec::IsAvailable() const
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.
*/

@ -123,6 +123,7 @@ struct AirportSpec {
static AirportSpec *GetWithoutOverride(byte type);
bool IsAvailable() const;
bool IsWithinMapBounds(byte table, TileIndex index) const;
static void ResetAirports();

@ -136,8 +136,10 @@
if (!::IsValidTile(tile)) return -1;
if (!IsAirportInformationAvailable(type)) return -1;
const AirportSpec *as = ::AirportSpec::Get(type);
if (!as->IsWithinMapBounds(0, tile)) return -1;
if (_settings_game.economy.station_noise_level) {
const AirportSpec *as = ::AirportSpec::Get(type);
AirportTileTableIterator it(as->table[0], tile);
uint dist;
AirportGetNearestTown(as, it, dist);
@ -155,6 +157,8 @@
if (!IsAirportInformationAvailable(type)) return INVALID_TOWN;
const AirportSpec *as = AirportSpec::Get(type);
if (!as->IsWithinMapBounds(0, tile)) return INVALID_TOWN;
uint dist;
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 */
const AirportSpec *as = AirportSpec::Get(airport_type);
if (!as->IsAvailable() || layout >= as->num_table) return CMD_ERROR;
if (!as->IsWithinMapBounds(layout, tile)) return CMD_ERROR;
Direction rotation = as->rotation[layout];
int w = as->size_x;

Loading…
Cancel
Save