From 89ddc87c16b511109c85c219998929673e6f9bbf Mon Sep 17 00:00:00 2001 From: Cyprian Klimaszewski Date: Tue, 27 Jan 2026 21:41:45 +0100 Subject: [PATCH] Codefix 97f3e5b: Restore defensive programming for _tile_type_procs. (#15165) --- src/landscape.cpp | 8 +++++++- src/tile_cmd.h | 2 +- src/tile_map.h | 4 ++-- src/tile_type.h | 4 ++++ 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/landscape.cpp b/src/landscape.cpp index c09f7d8135..423616a457 100644 --- a/src/landscape.cpp +++ b/src/landscape.cpp @@ -66,7 +66,7 @@ extern const TileTypeProcs * @ingroup TileCallbackGroup * @see TileType */ -const EnumClassIndexContainer, TileType> _tile_type_procs = { +const EnumClassIndexContainer, TileType> _tile_type_procs = { &_tile_type_clear_procs, // Callback functions for TileType::Clear tiles &_tile_type_rail_procs, // Callback functions for TileType::Railway tiles &_tile_type_road_procs, // Callback functions for TileType::Road tiles @@ -78,6 +78,12 @@ const EnumClassIndexContainer sprite */ diff --git a/src/tile_cmd.h b/src/tile_cmd.h index 89dba0271e..1fb89d88cf 100644 --- a/src/tile_cmd.h +++ b/src/tile_cmd.h @@ -169,7 +169,7 @@ struct TileTypeProcs { CheckBuildAboveProc *check_build_above_proc; }; -extern const EnumClassIndexContainer, TileType> _tile_type_procs; +extern const EnumClassIndexContainer, TileType> _tile_type_procs; TrackStatus GetTileTrackStatus(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side = INVALID_DIAGDIR); VehicleEnterTileStates VehicleEnterTile(Vehicle *v, TileIndex tile, int x, int y); diff --git a/src/tile_map.h b/src/tile_map.h index 5860c6b55b..0934231906 100644 --- a/src/tile_map.h +++ b/src/tile_map.h @@ -96,7 +96,7 @@ inline uint TilePixelHeightOutsideMap(int x, int y) [[debug_inline]] inline static TileType GetTileType(Tile tile) { assert(tile < Map::Size()); - return TileType(GB(tile.type(), 4, 4)); + return TileType(GB(tile.type(), 4, TILE_TYPE_BITS)); } /** @@ -135,7 +135,7 @@ inline void SetTileType(Tile tile, TileType type) * edges of the map. If _settings_game.construction.freeform_edges is true, * the upper edges of the map are also VOID tiles. */ assert(IsInnerTile(tile) == (type != TileType::Void)); - SB(tile.type(), 4, 4, to_underlying(type)); + SB(tile.type(), 4, TILE_TYPE_BITS, to_underlying(type)); } /** diff --git a/src/tile_type.h b/src/tile_type.h index 75af407c13..290a441b50 100644 --- a/src/tile_type.h +++ b/src/tile_type.h @@ -36,6 +36,7 @@ static constexpr uint MAX_SNOWLINE_HEIGHT = (MAX_TILE_HEIGHT - 2); ///< Maximum static constexpr uint DEF_SNOW_COVERAGE = 40; ///< Default snow coverage. static constexpr uint DEF_DESERT_COVERAGE = 50; ///< Default desert coverage. +static constexpr size_t TILE_TYPE_BITS = 4; ///< How many bits in map array are dedicated for type of each tile. /** * The different types of tiles. @@ -57,8 +58,11 @@ enum class TileType : uint8_t { TunnelBridge, ///< Tunnel entry/exit and bridge heads. Object, ///< Contains objects such as transmitters and owned land. End, ///< End marker. + MaxSize = 1U << TILE_TYPE_BITS, ///< The maximum possible number of tile types to be stored in map. }; +static_assert(TileType::End <= TileType::MaxSize); + /** * Additional infos of a tile on a tropic game. *