OpenTTD Source 20250205-master-gfd85ab1e2c
newgrf_railtype.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10#include "stdafx.h"
12#include "debug.h"
13#include "newgrf_railtype.h"
15#include "depot_base.h"
16#include "town.h"
17#include "tunnelbridge_map.h"
18
19#include "safeguards.h"
20
21/* virtual */ uint32_t RailTypeScopeResolver::GetRandomBits() const
22{
23 uint tmp = CountBits(this->tile.base() + (TileX(this->tile) + TileY(this->tile)) * TILE_SIZE);
24 return GB(tmp, 0, 2);
25}
26
27/* virtual */ uint32_t RailTypeScopeResolver::GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const
28{
29 if (this->tile == INVALID_TILE) {
30 switch (variable) {
31 case 0x40: return 0;
32 case 0x41: return 0;
33 case 0x42: return 0;
34 case 0x43: return TimerGameCalendar::date.base();
35 case 0x44: return HZB_TOWN_EDGE;
36 }
37 }
38
39 switch (variable) {
40 case 0x40: return GetTerrainType(this->tile, this->context);
41 case 0x41: return 0;
42 case 0x42: return IsLevelCrossingTile(this->tile) && IsCrossingBarred(this->tile);
43 case 0x43:
44 if (IsRailDepotTile(this->tile)) return Depot::GetByTile(this->tile)->build_date.base();
45 return TimerGameCalendar::date.base();
46 case 0x44: {
47 const Town *t = nullptr;
48 if (IsRailDepotTile(this->tile)) {
49 t = Depot::GetByTile(this->tile)->town;
50 } else if (IsLevelCrossingTile(this->tile)) {
51 t = ClosestTownFromTile(this->tile, UINT_MAX);
52 }
53 return t != nullptr ? GetTownRadiusGroup(t, this->tile) : HZB_TOWN_EDGE;
54 }
55 }
56
57 Debug(grf, 1, "Unhandled rail type tile variable 0x{:X}", variable);
58
59 available = false;
60 return UINT_MAX;
61}
62
64{
65 return GSF_RAILTYPES;
66}
67
69{
70 return this->railtype_scope.rti->label;
71}
72
82RailTypeResolverObject::RailTypeResolverObject(const RailTypeInfo *rti, TileIndex tile, TileContext context, RailTypeSpriteGroup rtsg, uint32_t param1, uint32_t param2)
83 : ResolverObject(rti != nullptr ? rti->grffile[rtsg] : nullptr, CBID_NO_CALLBACK, param1, param2), railtype_scope(*this, rti, tile, context)
84{
85 this->root_spritegroup = rti != nullptr ? rti->group[rtsg] : nullptr;
86}
87
97SpriteID GetCustomRailSprite(const RailTypeInfo *rti, TileIndex tile, RailTypeSpriteGroup rtsg, TileContext context, uint *num_results)
98{
99 assert(rtsg < RTSG_END);
100
101 if (rti->group[rtsg] == nullptr) return 0;
102
103 RailTypeResolverObject object(rti, tile, context, rtsg);
104 const SpriteGroup *group = object.Resolve();
105 if (group == nullptr || group->GetNumResults() == 0) return 0;
106
107 if (num_results) *num_results = group->GetNumResults();
108
109 return group->GetResult();
110}
111
123{
124 if (rti->group[RTSG_SIGNALS] == nullptr) return 0;
125
126 uint32_t param1 = gui ? 0x10 : 0x00;
127 uint32_t param2 = (type << 16) | (var << 8) | state;
128 RailTypeResolverObject object(rti, tile, TCX_NORMAL, RTSG_SIGNALS, param1, param2);
129
130 const SpriteGroup *group = object.Resolve();
131 if (group == nullptr || group->GetNumResults() == 0) return 0;
132
133 return group->GetResult();
134}
135
142RailType GetRailTypeTranslation(uint8_t railtype, const GRFFile *grffile)
143{
144 if (grffile == nullptr || grffile->railtype_list.empty()) {
145 /* No railtype table present. Return railtype as-is (if valid), so it works for original railtypes. */
146 if (railtype >= RAILTYPE_END || GetRailTypeInfo(static_cast<RailType>(railtype))->label == 0) return INVALID_RAILTYPE;
147
148 return static_cast<RailType>(railtype);
149 } else {
150 /* Railtype table present, but invalid index, return invalid type. */
151 if (railtype >= grffile->railtype_list.size()) return INVALID_RAILTYPE;
152
153 /* Look up railtype including alternate labels. */
154 return GetRailTypeByLabel(grffile->railtype_list[railtype]);
155 }
156}
157
164uint8_t GetReverseRailTypeTranslation(RailType railtype, const GRFFile *grffile)
165{
166 /* No rail type table present, return rail type as-is */
167 if (grffile == nullptr || grffile->railtype_list.empty()) return railtype;
168
169 /* Look for a matching rail type label in the table */
170 RailTypeLabel label = GetRailTypeInfo(railtype)->label;
171
172 int idx = find_index(grffile->railtype_list, label);
173 if (idx >= 0) return idx;
174
175 /* If not found, return as invalid */
176 return 0xFF;
177}
178
179std::vector<LabelObject<RailTypeLabel>> _railtype_list;
180
186{
187 std::vector<RailType> railtype_conversion_map;
188 bool needs_conversion = false;
189
190 for (auto it = std::begin(_railtype_list); it != std::end(_railtype_list); ++it) {
191 RailType rt = GetRailTypeByLabel(it->label);
192 if (rt == INVALID_RAILTYPE) {
193 rt = RAILTYPE_RAIL;
194 }
195
196 railtype_conversion_map.push_back(rt);
197
198 /* Conversion is needed if the rail type is in a different position than the list. */
199 if (it->label != 0 && rt != std::distance(std::begin(_railtype_list), it)) needs_conversion = true;
200 }
201
202 if (!needs_conversion) return;
203
204 for (const auto t : Map::Iterate()) {
205 switch (GetTileType(t)) {
206 case MP_RAILWAY:
207 SetRailType(t, railtype_conversion_map[GetRailType(t)]);
208 break;
209
210 case MP_ROAD:
211 if (IsLevelCrossing(t)) {
212 SetRailType(t, railtype_conversion_map[GetRailType(t)]);
213 }
214 break;
215
216 case MP_STATION:
217 if (HasStationRail(t)) {
218 SetRailType(t, railtype_conversion_map[GetRailType(t)]);
219 }
220 break;
221
222 case MP_TUNNELBRIDGE:
224 SetRailType(t, railtype_conversion_map[GetRailType(t)]);
225 }
226 break;
227
228 default:
229 break;
230 }
231 }
232}
233
236{
237 _railtype_list.clear();
238
239 for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
240 _railtype_list.push_back({GetRailTypeInfo(rt)->label, 0});
241 }
242}
243
244void ClearRailTypeLabelList()
245{
246 _railtype_list.clear();
247}
constexpr uint CountBits(T value)
Counts the number of set bits in a variable.
debug_inline static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
This struct contains all the info that is needed to draw and construct tracks.
Definition rail.h:117
RailTypeLabel label
Unique 32 bit rail type identifier.
Definition rail.h:226
const SpriteGroup * group[RTSG_END]
Sprite groups for resolving sprites.
Definition rail.h:271
static Date date
Current date in days (day counter).
Some simple functions to help with accessing containers.
int find_index(Container const &container, typename Container::const_reference item)
Helper function to get the index of an item Consider using std::set, std::unordered_set or std::flat_...
Functions related to debugging.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition debug.h:37
Base for all depots (except hangars)
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
static debug_inline uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:425
static debug_inline uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:415
GrfSpecFeature
Definition newgrf.h:68
@ CBID_NO_CALLBACK
Set when using the callback resolve system, but not to resolve a callback.
uint32_t GetTerrainType(TileIndex tile, TileContext context)
Function used by houses (and soon industries) to get information on type of "terrain" the tile it is ...
TileContext
Context for tile accesses.
@ TCX_NORMAL
Nothing special.
SpriteID GetCustomSignalSprite(const RailTypeInfo *rti, TileIndex tile, SignalType type, SignalVariant var, SignalState state, bool gui)
Get the sprite to draw for a given signal.
void ConvertRailTypes()
Test if any saved rail type labels are different to the currently loaded rail types.
uint8_t GetReverseRailTypeTranslation(RailType railtype, const GRFFile *grffile)
Perform a reverse railtype lookup to get the GRF internal ID.
SpriteID GetCustomRailSprite(const RailTypeInfo *rti, TileIndex tile, RailTypeSpriteGroup rtsg, TileContext context, uint *num_results)
Get the sprite to draw for the given tile.
void SetCurrentRailTypeLabelList()
Populate railtype label list with current values.
RailType GetRailTypeTranslation(uint8_t railtype, const GRFFile *grffile)
Translate an index to the GRF-local railtype-translation table into a RailType.
NewGRF handling of rail types.
RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels)
Get the rail type for a given label.
Definition rail.cpp:311
const RailTypeInfo * GetRailTypeInfo(RailType railtype)
Returns a pointer to the Railtype information for a given railtype.
Definition rail.h:297
RailTypeSpriteGroup
Sprite groups for a railtype.
Definition rail.h:39
@ RTSG_SIGNALS
Signal images.
Definition rail.h:51
RailType GetRailType(Tile t)
Gets the rail type of the given tile.
Definition rail_map.h:115
static debug_inline bool IsRailDepotTile(Tile t)
Is this tile rail tile and a rail depot?
Definition rail_map.h:105
void SetRailType(Tile t, RailType r)
Sets the rail type of the given tile.
Definition rail_map.h:125
RailType
Enumeration for all possible railtypes.
Definition rail_type.h:27
@ RAILTYPE_BEGIN
Used for iterations.
Definition rail_type.h:28
@ RAILTYPE_END
Used for iterations.
Definition rail_type.h:33
@ INVALID_RAILTYPE
Flag for invalid railtype.
Definition rail_type.h:34
@ RAILTYPE_RAIL
Standard non-electric rails.
Definition rail_type.h:29
bool IsLevelCrossingTile(Tile t)
Return whether a tile is a level crossing tile.
Definition road_map.h:95
bool IsCrossingBarred(Tile t)
Check if the level crossing is barred.
Definition road_map.h:416
bool IsLevelCrossing(Tile t)
Return whether a tile is a level crossing.
Definition road_map.h:85
A number of safeguards to prevent using unsafe methods.
SignalType
Type of signal, i.e.
Definition signal_type.h:23
SignalState
These are states in which a signal can be.
Definition signal_type.h:42
SignalVariant
Variant of the signal, i.e.
Definition signal_type.h:16
bool HasStationRail(Tile t)
Has this station tile a rail? In other words, is this station tile a rail station or rail waypoint?
Definition of base types and functions in a cross-platform compatible way.
TimerGameCalendar::Date build_date
Date of construction.
Definition depot_base.h:26
Dynamic data of a loaded NewGRF.
Definition newgrf.h:109
std::vector< RailTypeLabel > railtype_list
Railtype translation table.
Definition newgrf.h:133
static IterateWrapper Iterate()
Returns an iterable ensemble of all Tiles.
Definition map_func.h:363
Resolver object for rail types.
GrfSpecFeature GetFeature() const override
Get the feature number being resolved for.
RailTypeResolverObject(const RailTypeInfo *rti, TileIndex tile, TileContext context, RailTypeSpriteGroup rtsg, uint32_t param1=0, uint32_t param2=0)
Resolver object for rail types.
uint32_t GetDebugID() const override
Get an identifier for the item being resolved.
RailTypeScopeResolver railtype_scope
Resolver for the railtype scope.
uint32_t GetVariable(uint8_t variable, uint32_t parameter, bool &available) const override
Get a variable value.
TileContext context
Are we resolving sprites for the upper halftile, or on a bridge?
uint32_t GetRandomBits() const override
Get a few random bits.
TileIndex tile
Tracktile. For track on a bridge this is the southern bridgehead.
Interface for SpriteGroup-s to access the gamestate.
const SpriteGroup * root_spritegroup
Root SpriteGroup to use for resolving.
virtual const SpriteGroup * Resolve(ResolverObject &object) const
Base sprite group resolver.
Town data structure.
Definition town.h:52
static debug_inline TileType GetTileType(Tile tile)
Get the tiletype of a given tile.
Definition tile_map.h:96
static const uint TILE_SIZE
Tile size in world coordinates.
Definition tile_type.h:15
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:95
@ MP_ROAD
A tile with road (or tram tracks)
Definition tile_type.h:50
@ MP_STATION
A tile of a station.
Definition tile_type.h:53
@ MP_TUNNELBRIDGE
Tunnel entry/exit and bridge heads.
Definition tile_type.h:57
@ MP_RAILWAY
A railway.
Definition tile_type.h:49
Definition of the game-calendar-timer.
Base of the town class.
HouseZonesBits GetTownRadiusGroup(const Town *t, TileIndex tile)
Returns the bit corresponding to the town zone of the specified tile.
Town * ClosestTownFromTile(TileIndex tile, uint threshold)
Return the town closest (in distance or ownership) to a given tile, within a given threshold.
@ TRANSPORT_RAIL
Transport by train.
Functions that have tunnels and bridges in common.
TransportType GetTunnelBridgeTransportType(Tile t)
Tunnel: Get the transport type of the tunnel (road or rail) Bridge: Get the transport type of the bri...