From 54f328beeb2a814e0303f152f957a4a9a1f23d8e Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Fri, 22 Aug 2025 21:27:31 +0100 Subject: [PATCH] Codechange: Remove output pointer from GetTileArea(). (#14530) This simplifies things and removes undocumented type `ETileArea` --- src/base_station_base.h | 5 +- src/newgrf_station.cpp | 59 +++++++++++------------ src/pathfinder/pathfinder_func.h | 3 +- src/pathfinder/yapf/yapf_ship_regions.cpp | 4 +- src/station_base.h | 2 +- src/station_cmd.cpp | 27 +++-------- src/waypoint.cpp | 19 ++------ src/waypoint_base.h | 2 +- src/waypoint_gui.cpp | 4 +- 9 files changed, 44 insertions(+), 81 deletions(-) diff --git a/src/base_station_base.h b/src/base_station_base.h index fd493d7853..fadf565127 100644 --- a/src/base_station_base.h +++ b/src/base_station_base.h @@ -130,11 +130,10 @@ struct BaseStation : StationPool::PoolItem<&_station_pool> { /** * Get the tile area for a given station type. - * @param ta tile area to fill. * @param type the type of the area + * @return The tile area. */ - virtual void GetTileArea(TileArea *ta, StationType type) const = 0; - + virtual TileArea GetTileArea(StationType type) const = 0; /** * Obtain the length of a platform diff --git a/src/newgrf_station.cpp b/src/newgrf_station.cpp index 2adc68750b..d307e6dd9d 100644 --- a/src/newgrf_station.cpp +++ b/src/newgrf_station.cpp @@ -73,38 +73,36 @@ enum TriggerArea : uint8_t { TA_WHOLE, }; -struct ETileArea : TileArea { - ETileArea(const BaseStation *st, TileIndex tile, TriggerArea ta) - { - switch (ta) { - default: NOT_REACHED(); +/** + * Get the tile area of a rail station with trigger area type. + * @param st The rail station or rail waypoint. + * @param tile Origin tile. + * @param ta Trigger area type. + * @return The tile area. + */ +TileArea GetRailTileArea(const BaseStation *st, TileIndex tile, TriggerArea ta) +{ + switch (ta) { + default: NOT_REACHED(); - case TA_TILE: - this->tile = tile; - this->w = 1; - this->h = 1; - break; + case TA_TILE: + return {tile, 1, 1}; - case TA_PLATFORM: { - TileIndex start, end; - Axis axis = GetRailStationAxis(tile); - TileIndexDiff delta = TileOffsByAxis(axis); + case TA_PLATFORM: { + TileIndex start, end; + Axis axis = GetRailStationAxis(tile); + TileIndexDiff delta = TileOffsByAxis(axis); - for (end = tile; IsRailStationTile(end + delta) && IsCompatibleTrainStationTile(end + delta, tile); end += delta) { /* Nothing */ } - for (start = tile; IsRailStationTile(start - delta) && IsCompatibleTrainStationTile(start - delta, tile); start -= delta) { /* Nothing */ } + for (end = tile; IsRailStationTile(end + delta) && IsCompatibleTrainStationTile(end + delta, tile); end += delta) { /* Nothing */ } + for (start = tile; IsRailStationTile(start - delta) && IsCompatibleTrainStationTile(start - delta, tile); start -= delta) { /* Nothing */ } - this->tile = start; - this->w = TileX(end) - TileX(start) + 1; - this->h = TileY(end) - TileY(start) + 1; - break; - } - - case TA_WHOLE: - st->GetTileArea(this, Station::IsExpected(st) ? StationType::Rail : StationType::RailWaypoint); - break; + return TileArea(start, TileX(end) - TileX(start) + 1, TileY(end) - TileY(start) + 1); } + + case TA_WHOLE: + return st->GetTileArea(Station::IsExpected(st) ? StationType::Rail : StationType::RailWaypoint); } -}; +} /** @@ -745,9 +743,8 @@ void DeallocateSpecFromStation(BaseStation *st, uint8_t specindex) /* specindex of 0 (default) is never freeable */ if (specindex == 0) return; - ETileArea area = ETileArea(st, INVALID_TILE, TA_WHOLE); /* Check all tiles over the station to check if the specindex is still in use */ - for (TileIndex tile : area) { + for (TileIndex tile : GetRailTileArea(st, INVALID_TILE, TA_WHOLE)) { if (st->TileBelongsToRailStation(tile) && GetCustomStationSpecIndex(tile) == specindex) { return; } @@ -911,10 +908,9 @@ void TriggerStationAnimation(BaseStation *st, TileIndex trigger_tile, StationAni if (!st->cached_anim_triggers.Test(trigger)) return; uint16_t random_bits = Random(); - ETileArea area = ETileArea(st, trigger_tile, tas[static_cast(trigger)]); /* Check all tiles over the station to check if the specindex is still in use */ - for (TileIndex tile : area) { + for (TileIndex tile : GetRailTileArea(st, trigger_tile, tas[static_cast(trigger)])) { if (st->TileBelongsToRailStation(tile)) { const StationSpec *ss = GetStationSpec(tile); if (ss != nullptr && ss->animation.triggers.Test(trigger)) { @@ -951,7 +947,6 @@ void TriggerStationRandomisation(BaseStation *st, TileIndex trigger_tile, Statio if (IsValidCargoType(cargo_type) && !HasBit(st->cached_cargo_triggers, cargo_type)) return; uint32_t whole_reseed = 0; - ETileArea area = ETileArea(st, trigger_tile, tas[static_cast(trigger)]); /* Bitmask of completely empty cargo types to be matched. */ CargoTypes empty_mask{}; @@ -964,7 +959,7 @@ void TriggerStationRandomisation(BaseStation *st, TileIndex trigger_tile, Statio StationRandomTriggers used_random_triggers; /* Check all tiles over the station to check if the specindex is still in use */ - for (TileIndex tile : area) { + for (TileIndex tile : GetRailTileArea(st, trigger_tile, tas[static_cast(trigger)])) { if (st->TileBelongsToRailStation(tile)) { const StationSpec *ss = GetStationSpec(tile); if (ss == nullptr) continue; diff --git a/src/pathfinder/pathfinder_func.h b/src/pathfinder/pathfinder_func.h index 444b100ce7..7d37917b2d 100644 --- a/src/pathfinder/pathfinder_func.h +++ b/src/pathfinder/pathfinder_func.h @@ -25,8 +25,7 @@ inline TileIndex CalcClosestStationTile(StationID station, TileIndex tile, StationType station_type) { const BaseStation *st = BaseStation::Get(station); - TileArea ta; - st->GetTileArea(&ta, station_type); + TileArea ta = st->GetTileArea(station_type); /* If the rail station is (temporarily) not present, use the station sign to drive near the station */ if (ta.tile == INVALID_TILE) return st->xy; diff --git a/src/pathfinder/yapf/yapf_ship_regions.cpp b/src/pathfinder/yapf/yapf_ship_regions.cpp index 19ea441613..202936a7bd 100644 --- a/src/pathfinder/yapf/yapf_ship_regions.cpp +++ b/src/pathfinder/yapf/yapf_ship_regions.cpp @@ -184,9 +184,7 @@ public: if (v->current_order.IsType(OT_GOTO_STATION)) { StationID station_id = v->current_order.GetDestination().ToStationID(); const BaseStation *station = BaseStation::Get(station_id); - TileArea tile_area; - station->GetTileArea(&tile_area, StationType::Dock); - for (const auto &tile : tile_area) { + for (const auto &tile : station->GetTileArea(StationType::Dock)) { if (IsDockingTile(tile) && IsShipDestinationTile(tile, station_id)) { pf.AddOrigin(GetWaterRegionPatchInfo(tile)); } diff --git a/src/station_base.h b/src/station_base.h index c503118f9e..b1d26cc721 100644 --- a/src/station_base.h +++ b/src/station_base.h @@ -579,7 +579,7 @@ public: uint32_t GetNewGRFVariable(const ResolverObject &object, uint8_t variable, uint8_t parameter, bool &available) const override; - void GetTileArea(TileArea *ta, StationType type) const override; + TileArea GetTileArea(StationType type) const override; }; /** Iterator to iterate over all tiles belonging to an airport. */ diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index ff920c72e8..fc7e7df25b 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -411,30 +411,15 @@ static Station *GetClosestDeletedStation(TileIndex tile) } -void Station::GetTileArea(TileArea *ta, StationType type) const +TileArea Station::GetTileArea(StationType type) const { switch (type) { - case StationType::Rail: - *ta = this->train_station; - return; - - case StationType::Airport: - *ta = this->airport; - return; - - case StationType::Truck: - *ta = this->truck_station; - return; - - case StationType::Bus: - *ta = this->bus_station; - return; - + case StationType::Rail: return this->train_station; + case StationType::Airport: return this->airport; + case StationType::Truck: return this->truck_station; + case StationType::Bus: return this->bus_station; case StationType::Dock: - case StationType::Oilrig: - *ta = this->docking_station; - return; - + case StationType::Oilrig: return this->docking_station; default: NOT_REACHED(); } } diff --git a/src/waypoint.cpp b/src/waypoint.cpp index c5486b5cd5..1bf842a420 100644 --- a/src/waypoint.cpp +++ b/src/waypoint.cpp @@ -32,23 +32,12 @@ void DrawWaypointSprite(int x, int y, StationClassID station_class, uint16_t sta } } -void Waypoint::GetTileArea(TileArea *ta, StationType type) const +TileArea Waypoint::GetTileArea(StationType type) const { switch (type) { - case StationType::RailWaypoint: - *ta = this->train_station; - return; - - case StationType::RoadWaypoint: - *ta = this->road_waypoint_area; - return; - - case StationType::Buoy: - ta->tile = this->xy; - ta->w = 1; - ta->h = 1; - break; - + case StationType::RailWaypoint: return this->train_station; + case StationType::RoadWaypoint: return this->road_waypoint_area; + case StationType::Buoy: return {this->xy, 1, 1}; default: NOT_REACHED(); } } diff --git a/src/waypoint_base.h b/src/waypoint_base.h index 9855949326..dd1a1ca748 100644 --- a/src/waypoint_base.h +++ b/src/waypoint_base.h @@ -43,7 +43,7 @@ struct Waypoint final : SpecializedStation { uint32_t GetNewGRFVariable(const struct ResolverObject &object, uint8_t variable, uint8_t parameter, bool &available) const override; - void GetTileArea(TileArea *ta, StationType type) const override; + TileArea GetTileArea(StationType type) const override; uint GetPlatformLength(TileIndex, DiagDirection) const override { diff --git a/src/waypoint_gui.cpp b/src/waypoint_gui.cpp index 7d61c1af01..d25020282f 100644 --- a/src/waypoint_gui.cpp +++ b/src/waypoint_gui.cpp @@ -60,9 +60,7 @@ private: default: NOT_REACHED(); } - TileArea ta; - this->wp->GetTileArea(&ta, type); - return ta.GetCenterTile(); + return this->wp->GetTileArea(type).GetCenterTile(); } public: