Skip to content

Commit

Permalink
Fix: AirportGetNearestTown incorrectly assumed first TileIterator res…
Browse files Browse the repository at this point in the history
…ult was origin.

Not all TileIterators are equal, and some do not start at the top-corner, so the perimeter check was wrong. As the caller already has thie origin tile, use that instead.
  • Loading branch information
PeterN committed Dec 9, 2023
1 parent 2dcb00a commit 1115f8a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/script/api/script_airport.cpp
Expand Up @@ -139,7 +139,7 @@
if (_settings_game.economy.station_noise_level) {
AirportTileTableIterator it(as->table[0], tile);
uint dist;
AirportGetNearestTown(as, it, dist);
AirportGetNearestTown(as, tile, it, dist);
return GetAirportNoiseLevelForDistance(as, dist);
}

Expand All @@ -155,7 +155,7 @@
if (!as->IsWithinMapBounds(0, tile)) return INVALID_TOWN;

uint dist;
return AirportGetNearestTown(as, AirportTileTableIterator(as->table[0], tile), dist)->index;
return AirportGetNearestTown(as, tile, AirportTileTableIterator(as->table[0], tile), dist)->index;
}

/* static */ SQInteger ScriptAirport::GetMaintenanceCostFactor(AirportType type)
Expand Down
13 changes: 7 additions & 6 deletions src/station_cmd.cpp
Expand Up @@ -2303,18 +2303,19 @@ uint8_t GetAirportNoiseLevelForDistance(const AirportSpec *as, uint distance)
* Finds the town nearest to given airport. Based on minimal manhattan distance to any airport's tile.
* If two towns have the same distance, town with lower index is returned.
* @param as airport's description
* @param tile origin tile (top-corner)
* @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, uint &mindist)
Town *AirportGetNearestTown(const AirportSpec *as, TileIndex tile, const TileIterator &it, uint &mindist)
{
assert(Town::GetNumItems() > 0);

Town *nearest = nullptr;

uint perimeter_min_x = TileX(it);
uint perimeter_min_y = TileY(it);
uint perimeter_min_x = TileX(tile);
uint perimeter_min_y = TileY(tile);
uint perimeter_max_x = perimeter_min_x + as->size_x - 1;
uint perimeter_max_y = perimeter_min_y + as->size_y - 1;

Expand Down Expand Up @@ -2349,7 +2350,7 @@ void UpdateAirportsNoise()
const AirportSpec *as = st->airport.GetSpec();
AirportTileIterator it(st);
uint dist;
Town *nearest = AirportGetNearestTown(as, it, dist);
Town *nearest = AirportGetNearestTown(as, st->airport.tile, it, dist);
nearest->noise_reached += GetAirportNoiseLevelForDistance(as, dist);
}
}
Expand Down Expand Up @@ -2399,7 +2400,7 @@ CommandCost CmdBuildAirport(DoCommandFlag flags, TileIndex tile, byte airport_ty

/* The noise level is the noise from the airport and reduce it to account for the distance to the town center. */
uint dist;
Town *nearest = AirportGetNearestTown(as, tile_iter, dist);
Town *nearest = AirportGetNearestTown(as, tile, tile_iter, dist);
uint newnoise_level = GetAirportNoiseLevelForDistance(as, dist);

/* Check if local auth would allow a new airport */
Expand Down Expand Up @@ -2527,7 +2528,7 @@ static CommandCost RemoveAirport(TileIndex tile, DoCommandFlag flags)
* need of recalculation */
AirportTileIterator it(st);
uint dist;
Town *nearest = AirportGetNearestTown(as, it, dist);
Town *nearest = AirportGetNearestTown(as, st->airport.tile, it, dist);
nearest->noise_reached -= GetAirportNoiseLevelForDistance(as, dist);

if (_settings_game.economy.station_noise_level) {
Expand Down
2 changes: 1 addition & 1 deletion src/station_cmd.h
Expand Up @@ -16,7 +16,7 @@
enum StationClassID : byte;
enum RoadStopClassID : byte;

extern Town *AirportGetNearestTown(const struct AirportSpec *as, const TileIterator &it, uint &mindist);
extern Town *AirportGetNearestTown(const struct AirportSpec *as, TileIndex tile, const TileIterator &it, uint &mindist);
extern uint8_t GetAirportNoiseLevelForDistance(const struct AirportSpec *as, uint distance);

CommandCost CmdBuildAirport(DoCommandFlag flags, TileIndex tile, byte airport_type, byte layout, StationID station_to_join, bool allow_adjacent);
Expand Down

0 comments on commit 1115f8a

Please sign in to comment.