From d08636c8416e5349aa3a8954cc03d0d8fa1e3e86 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Thu, 18 Apr 2024 12:09:44 +0100 Subject: [PATCH] Codechange: Store station layout tiles as std::span. Using std::span provides both the start and end of the list, which allows validating that the requested layout is in range. --- src/station_cmd.cpp | 10 +++++++++- src/station_type.h | 3 ++- src/table/station_land.h | 4 ++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index 84844a4e65..49d9b3e52f 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -3008,9 +3008,17 @@ static CommandCost RemoveDock(TileIndex tile, DoCommandFlag flags) #include "table/station_land.h" +/** + * Get station tile layout for a station type and its station gfx. + * @param st Station type to draw. + * @param gfx StationGfx of tile to draw. + * @return Tile layout to draw. + */ const DrawTileSprites *GetStationTileLayout(StationType st, uint8_t gfx) { - return &_station_display_datas[st][gfx]; + const auto &layouts = _station_display_datas[st]; + if (gfx >= layouts.size()) gfx &= 1; + return layouts.data() + gfx; } /** diff --git a/src/station_type.h b/src/station_type.h index 54d7939e66..a108e85b13 100644 --- a/src/station_type.h +++ b/src/station_type.h @@ -28,7 +28,7 @@ static const StationID INVALID_STATION = 0xFFFF; typedef SmallStack StationIDStack; /** Station types */ -enum StationType { +enum StationType : uint8_t { STATION_RAIL, STATION_AIRPORT, STATION_TRUCK, @@ -38,6 +38,7 @@ enum StationType { STATION_BUOY, STATION_WAYPOINT, STATION_ROADWAYPOINT, + STATION_END, }; /** Types of RoadStops */ diff --git a/src/table/station_land.h b/src/table/station_land.h index fc47de8d77..7ac1aa02ff 100644 --- a/src/table/station_land.h +++ b/src/table/station_land.h @@ -1013,7 +1013,7 @@ static const DrawTileSprites _station_display_datas_waypoint[] = { * As these are drawn/build like stations, they may use the same number of layouts. */ static_assert(lengthof(_station_display_datas_rail) == lengthof(_station_display_datas_waypoint)); -static const DrawTileSprites * const _station_display_datas[] = { +static const std::array, STATION_END> _station_display_datas = {{ _station_display_datas_rail, _station_display_datas_airport, _station_display_datas_truck, @@ -1023,4 +1023,4 @@ static const DrawTileSprites * const _station_display_datas[] = { _station_display_datas_buoy, _station_display_datas_waypoint, _station_display_datas_road_waypoint, -}; +}};