Skip to content

Commit

Permalink
Codechange: Unify where rail station tile flags are set. (OpenTTD#12531)
Browse files Browse the repository at this point in the history
This avoids repeating the logic in three places.
  • Loading branch information
PeterN authored Apr 18, 2024
1 parent 04a3bf7 commit 45886e5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 34 deletions.
14 changes: 1 addition & 13 deletions src/saveload/afterload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down
31 changes: 20 additions & 11 deletions src/station_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

SetStationTileBlocked(tile, blocked);
SetStationTileHavePylons(tile, pylons);
SetStationTileHaveWires(tile, wires);
SetRailStationTileFlags(tile, statspec);

if (!blocked) c->infrastructure.rail[rt]++;
if (!IsStationTileBlocked(tile)) c->infrastructure.rail[rt]++;
c->infrastructure.station++;

tile += tile_delta;
Expand Down
1 change: 1 addition & 0 deletions src/station_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
11 changes: 1 addition & 10 deletions src/waypoint_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 45886e5

Please sign in to comment.