From 45886e50b21fd1dee461e910267781e264574790 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Thu, 18 Apr 2024 18:54:10 +0100 Subject: [PATCH] Codechange: Unify where rail station tile flags are set. (#12531) This avoids repeating the logic in three places. --- src/saveload/afterload.cpp | 14 +------------- src/station_cmd.cpp | 31 ++++++++++++++++++++----------- src/station_func.h | 1 + src/waypoint_cmd.cpp | 11 +---------- 4 files changed, 23 insertions(+), 34 deletions(-) diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index 75f062fc4b..88dee51b9c 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -2878,19 +2878,7 @@ bool AfterLoadGame() /* Station blocked, wires and pylon flags need to be stored in the map. This is effectively cached data, so no * version check is necessary. This is done here as the SLV_182 check below needs the blocked status. */ for (auto t : Map::Iterate()) { - if (HasStationTileRail(t)) { - StationGfx gfx = GetStationGfx(t); - const StationSpec *statspec = GetStationSpec(t); - - bool blocked = statspec != nullptr && HasBit(statspec->blocked, gfx); - /* Default stations do not draw pylons under roofs (gfx >= 4) */ - bool pylons = statspec != nullptr ? HasBit(statspec->pylons, gfx) : gfx < 4; - bool wires = statspec == nullptr || !HasBit(statspec->wires, gfx); - - SetStationTileBlocked(t, blocked); - SetStationTileHavePylons(t, pylons); - SetStationTileHaveWires(t, wires); - } + if (HasStationTileRail(t)) SetRailStationTileFlags(t, GetStationSpec(t)); } } diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index 62ef3128b3..aece206dfc 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -1294,6 +1294,24 @@ static CommandCost CalculateRailStationCost(TileArea tile_area, DoCommandFlag fl return cost; } +/** + * Set rail station tile flags for the given tile. + * @param tile Tile to set flags on. + * @param statspec Statspec of the tile. + */ +void SetRailStationTileFlags(TileIndex tile, const StationSpec *statspec) +{ + const StationGfx gfx = GetStationGfx(tile); + bool blocked = statspec != nullptr && HasBit(statspec->blocked, gfx); + /* Default stations do not draw pylons under roofs (gfx >= 4) */ + bool pylons = statspec != nullptr ? HasBit(statspec->pylons, gfx) : gfx < 4; + bool wires = statspec == nullptr || !HasBit(statspec->wires, gfx); + + SetStationTileBlocked(tile, blocked); + SetStationTileHavePylons(tile, pylons); + SetStationTileHaveWires(tile, wires); +} + /** * Build rail station * @param flags operation to perform @@ -1456,18 +1474,9 @@ CommandCost CmdBuildRailStation(DoCommandFlag flags, TileIndex tile_org, RailTyp TriggerStationAnimation(st, tile, SAT_BUILT); } - /* Should be the same as layout but axis component could be wrong... */ - StationGfx gfx = GetStationGfx(tile); - bool blocked = statspec != nullptr && HasBit(statspec->blocked, gfx); - /* Default stations do not draw pylons under roofs (gfx >= 4) */ - bool pylons = statspec != nullptr ? HasBit(statspec->pylons, gfx) : gfx < 4; - bool wires = statspec == nullptr || !HasBit(statspec->wires, gfx); + SetRailStationTileFlags(tile, statspec); - SetStationTileBlocked(tile, blocked); - SetStationTileHavePylons(tile, pylons); - SetStationTileHaveWires(tile, wires); - - if (!blocked) c->infrastructure.rail[rt]++; + if (!IsStationTileBlocked(tile)) c->infrastructure.rail[rt]++; c->infrastructure.station++; tile += tile_delta; diff --git a/src/station_func.h b/src/station_func.h index b71130f5d8..ab24b6cc9f 100644 --- a/src/station_func.h +++ b/src/station_func.h @@ -33,6 +33,7 @@ void UpdateStationAcceptance(Station *st, bool show_msg); CargoTypes GetAcceptanceMask(const Station *st); CargoTypes GetEmptyMask(const Station *st); +void SetRailStationTileFlags(TileIndex tile, const StationSpec *statspec); const DrawTileSprites *GetStationTileLayout(StationType st, uint8_t gfx); void StationPickerDrawSprite(int x, int y, StationType st, RailType railtype, RoadType roadtype, int image); diff --git a/src/waypoint_cmd.cpp b/src/waypoint_cmd.cpp index 2bf7ce5e52..0ffffcfc09 100644 --- a/src/waypoint_cmd.cpp +++ b/src/waypoint_cmd.cpp @@ -276,16 +276,7 @@ CommandCost CmdBuildRailWaypoint(DoCommandFlag flags, TileIndex start_tile, Axis MakeRailWaypoint(tile, wp->owner, wp->index, axis, layout[i], GetRailType(tile)); SetCustomStationSpecIndex(tile, map_spec_index); - /* Should be the same as layout but axis component could be wrong... */ - StationGfx gfx = GetStationGfx(tile); - bool blocked = spec != nullptr && HasBit(spec->blocked, gfx); - /* Default stations do not draw pylons under roofs (gfx >= 4) */ - bool pylons = spec != nullptr ? HasBit(spec->pylons, gfx) : gfx < 4; - bool wires = spec == nullptr || !HasBit(spec->wires, gfx); - - SetStationTileBlocked(tile, blocked); - SetStationTileHavePylons(tile, pylons); - SetStationTileHaveWires(tile, wires); + SetRailStationTileFlags(tile, spec); SetRailStationReservation(tile, reserved); MarkTileDirtyByTile(tile);