summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeterN <peter@fuzzle.org>2019-03-29 17:43:06 +0000
committerGitHub <noreply@github.com>2019-03-29 17:43:06 +0000
commitb6e3e30d86e66e84d52e94a9f91b9952ae699ab5 (patch)
treea8353a549be4070c5faa41c2d2dcc19a7f725b9e
parent9244dad3f111b19950350f165dc2f61a62425f44 (diff)
downloadopenttd-b6e3e30d86e66e84d52e94a9f91b9952ae699ab5.tar.xz
Codechange: Distance between town and airport has already just been found, so use it. (#7427)
Previously the distance was thrown away, only to be expensively recalculated again.
-rw-r--r--src/script/api/script_airport.cpp14
-rw-r--r--src/station_cmd.cpp27
2 files changed, 22 insertions, 19 deletions
diff --git a/src/script/api/script_airport.cpp b/src/script/api/script_airport.cpp
index 8e19d257d..dab868b75 100644
--- a/src/script/api/script_airport.cpp
+++ b/src/script/api/script_airport.cpp
@@ -130,8 +130,8 @@
/* static */ int ScriptAirport::GetNoiseLevelIncrease(TileIndex tile, AirportType type)
{
- extern Town *AirportGetNearestTown(const AirportSpec *as, const TileIterator &it);
- extern uint8 GetAirportNoiseLevelForTown(const AirportSpec *as, TileIterator &it, TileIndex town_tile);
+ extern Town *AirportGetNearestTown(const AirportSpec *as, const TileIterator &it, uint &mindist);
+ extern uint8 GetAirportNoiseLevelForDistance(const AirportSpec *as, uint distance);
if (!::IsValidTile(tile)) return -1;
if (!IsAirportInformationAvailable(type)) return -1;
@@ -139,8 +139,9 @@
if (_settings_game.economy.station_noise_level) {
const AirportSpec *as = ::AirportSpec::Get(type);
AirportTileTableIterator it(as->table[0], tile);
- const Town *t = AirportGetNearestTown(as, it);
- return GetAirportNoiseLevelForTown(as, it, t->xy);
+ uint dist;
+ AirportGetNearestTown(as, it, dist);
+ return GetAirportNoiseLevelForDistance(as, dist);
}
return 1;
@@ -148,13 +149,14 @@
/* static */ TownID ScriptAirport::GetNearestTown(TileIndex tile, AirportType type)
{
- extern Town *AirportGetNearestTown(const AirportSpec *as, const TileIterator &it);
+ extern Town *AirportGetNearestTown(const AirportSpec *as, const TileIterator &it, uint &mindist);
if (!::IsValidTile(tile)) return INVALID_TOWN;
if (!IsAirportInformationAvailable(type)) return INVALID_TOWN;
const AirportSpec *as = AirportSpec::Get(type);
- return AirportGetNearestTown(as, AirportTileTableIterator(as->table[0], tile))->index;
+ uint dist;
+ return AirportGetNearestTown(as, AirportTileTableIterator(as->table[0], tile), dist)->index;
}
/* static */ uint16 ScriptAirport::GetMaintenanceCostFactor(AirportType type)
diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp
index 7a939cd8b..b15fc1543 100644
--- a/src/station_cmd.cpp
+++ b/src/station_cmd.cpp
@@ -2164,18 +2164,15 @@ static uint GetMinimalAirportDistanceToTile(TileIterator &it, TileIndex town_til
* The further you get, the less noise you generate.
* So all those folks at city council can now happily slee... work in their offices
* @param as airport information
- * @param it An iterator over all airport tiles.
- * @param town_tile TileIndex of town's center, the one who will receive the airport's candidature
+ * @param distance minimum distance between town and airport
* @return the noise that will be generated, according to distance
*/
-uint8 GetAirportNoiseLevelForTown(const AirportSpec *as, TileIterator &it, TileIndex town_tile)
+uint8 GetAirportNoiseLevelForDistance(const AirportSpec *as, uint distance)
{
/* 0 cannot be accounted, and 1 is the lowest that can be reduced from town.
* So no need to go any further*/
if (as->noise_level < 2) return as->noise_level;
- uint distance = GetMinimalAirportDistanceToTile(it, town_tile);
-
/* The steps for measuring noise reduction are based on the "magical" (and arbitrary) 8 base distance
* adding the town_council_tolerance 4 times, as a way to graduate, depending of the tolerance.
* Basically, it says that the less tolerant a town is, the bigger the distance before
@@ -2196,13 +2193,14 @@ uint8 GetAirportNoiseLevelForTown(const AirportSpec *as, TileIterator &it, TileI
* If two towns have the same distance, town with lower index is returned.
* @param as airport's description
* @param it An iterator over all airport tiles
+ * @param[out] mindist Minimum distance to town
* @return nearest town to airport
*/
-Town *AirportGetNearestTown(const AirportSpec *as, const TileIterator &it)
+Town *AirportGetNearestTown(const AirportSpec *as, const TileIterator &it, uint &mindist)
{
Town *t, *nearest = NULL;
uint add = as->size_x + as->size_y - 2; // GetMinimalAirportDistanceToTile can differ from DistanceManhattan by this much
- uint mindist = UINT_MAX - add; // prevent overflow
+ mindist = UINT_MAX - add; // prevent overflow
FOR_ALL_TOWNS(t) {
if (DistanceManhattan(t->xy, it) < mindist + add) { // avoid calling GetMinimalAirportDistanceToTile too often
TileIterator *copy = it.Clone();
@@ -2231,8 +2229,9 @@ void UpdateAirportsNoise()
if (st->airport.tile != INVALID_TILE && st->airport.type != AT_OILRIG) {
const AirportSpec *as = st->airport.GetSpec();
AirportTileIterator it(st);
- Town *nearest = AirportGetNearestTown(as, it);
- nearest->noise_reached += GetAirportNoiseLevelForTown(as, it, nearest->xy);
+ uint dist;
+ Town *nearest = AirportGetNearestTown(as, it, dist);
+ nearest->noise_reached += GetAirportNoiseLevelForDistance(as, dist);
}
}
}
@@ -2285,8 +2284,9 @@ CommandCost CmdBuildAirport(TileIndex tile, DoCommandFlag flags, uint32 p1, uint
if (cost.Failed()) return cost;
/* The noise level is the noise from the airport and reduce it to account for the distance to the town center. */
- Town *nearest = AirportGetNearestTown(as, iter);
- uint newnoise_level = GetAirportNoiseLevelForTown(as, iter, nearest->xy);
+ uint dist;
+ Town *nearest = AirportGetNearestTown(as, iter, dist);
+ uint newnoise_level = GetAirportNoiseLevelForDistance(as, dist);
/* Check if local auth would allow a new airport */
StringID authority_refuse_message = STR_NULL;
@@ -2405,8 +2405,9 @@ static CommandCost RemoveAirport(TileIndex tile, DoCommandFlag flags)
* And as for construction, always remove it, even if the setting is not set, in order to avoid the
* need of recalculation */
AirportTileIterator it(st);
- Town *nearest = AirportGetNearestTown(as, it);
- nearest->noise_reached -= GetAirportNoiseLevelForTown(as, it, nearest->xy);
+ uint dist;
+ Town *nearest = AirportGetNearestTown(as, it, dist);
+ nearest->noise_reached -= GetAirportNoiseLevelForDistance(as, dist);
}
TILE_AREA_LOOP(tile_cur, st->airport) {