summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPeterN <peter@fuzzle.org>2019-03-30 22:20:26 +0000
committerGitHub <noreply@github.com>2019-03-30 22:20:26 +0000
commite1069eee05b648a64ff5dac3dd5701b01cfa2034 (patch)
tree019da19b2cc4b98fc7fb0f09e1738db17fe8a307 /src
parent32fda83d3916197a46215c83ddd85b2e50128a02 (diff)
downloadopenttd-e1069eee05b648a64ff5dac3dd5701b01cfa2034.tar.xz
Codechange: Check airport layout would fit within map bounds before iterating tiles. (#7429)
Diffstat (limited to 'src')
-rw-r--r--src/newgrf_airport.cpp18
-rw-r--r--src/newgrf_airport.h1
-rw-r--r--src/script/api/script_airport.cpp6
-rw-r--r--src/station_cmd.cpp1
4 files changed, 25 insertions, 1 deletions
diff --git a/src/newgrf_airport.cpp b/src/newgrf_airport.cpp
index 46f15b0e1..615292aef 100644
--- a/src/newgrf_airport.cpp
+++ b/src/newgrf_airport.cpp
@@ -131,6 +131,24 @@ bool AirportSpec::IsAvailable() const
}
/**
+ * 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.
*/
void AirportSpec::ResetAirports()
diff --git a/src/newgrf_airport.h b/src/newgrf_airport.h
index 867362e9a..7b752e741 100644
--- a/src/newgrf_airport.h
+++ b/src/newgrf_airport.h
@@ -123,6 +123,7 @@ struct AirportSpec {
static AirportSpec *GetWithoutOverride(byte type);
bool IsAvailable() const;
+ bool IsWithinMapBounds(byte table, TileIndex index) const;
static void ResetAirports();
diff --git a/src/script/api/script_airport.cpp b/src/script/api/script_airport.cpp
index dab868b75..7d80f97af 100644
--- a/src/script/api/script_airport.cpp
+++ b/src/script/api/script_airport.cpp
@@ -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;
}
diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp
index b15fc1543..a79480448 100644
--- a/src/station_cmd.cpp
+++ b/src/station_cmd.cpp
@@ -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;