Changeset - r23560:8847b4008fad
[Not reviewed]
master
0 2 0
PeterN - 6 years ago 2019-03-29 17:43:06
peter@fuzzle.org
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.
2 files changed with 22 insertions and 19 deletions:
0 comments (0 inline, 0 general)
src/script/api/script_airport.cpp
Show inline comments
 
@@ -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)
src/station_cmd.cpp
Show inline comments
 
@@ -2164,18 +2164,15 @@ static uint GetMinimalAirportDistanceToT
 
 * 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 
 
 * 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 ti
 
	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(TileInd
 
		 * 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) {
0 comments (0 inline, 0 general)