OpenTTD Source 20250205-master-gfd85ab1e2c
newgrf_roadtype.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_roadtype.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 RoadTypeScopeResolver::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 RoadTypeScopeResolver::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 (IsRoadDepotTile(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 (IsRoadDepotTile(this->tile)) {
49 t = Depot::GetByTile(this->tile)->town;
50 } else {
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 road type tile variable 0x{:X}", variable);
58
59 available = false;
60 return UINT_MAX;
61}
62
64{
65 RoadType rt = GetRoadTypeByLabel(this->roadtype_scope.rti->label, false);
66 switch (GetRoadTramType(rt)) {
67 case RTT_ROAD: return GSF_ROADTYPES;
68 case RTT_TRAM: return GSF_TRAMTYPES;
69 default: return GSF_INVALID;
70 }
71}
72
74{
75 return this->roadtype_scope.rti->label;
76}
77
87RoadTypeResolverObject::RoadTypeResolverObject(const RoadTypeInfo *rti, TileIndex tile, TileContext context, RoadTypeSpriteGroup rtsg, uint32_t param1, uint32_t param2)
88 : ResolverObject(rti != nullptr ? rti->grffile[rtsg] : nullptr, CBID_NO_CALLBACK, param1, param2), roadtype_scope(*this, rti, tile, context)
89{
90 this->root_spritegroup = rti != nullptr ? rti->group[rtsg] : nullptr;
91}
92
102SpriteID GetCustomRoadSprite(const RoadTypeInfo *rti, TileIndex tile, RoadTypeSpriteGroup rtsg, TileContext context, uint *num_results)
103{
104 assert(rtsg < ROTSG_END);
105
106 if (rti->group[rtsg] == nullptr) return 0;
107
108 RoadTypeResolverObject object(rti, tile, context, rtsg);
109 const SpriteGroup *group = object.Resolve();
110 if (group == nullptr || group->GetNumResults() == 0) return 0;
111
112 if (num_results) *num_results = group->GetNumResults();
113
114 return group->GetResult();
115}
116
124RoadType GetRoadTypeTranslation(RoadTramType rtt, uint8_t tracktype, const GRFFile *grffile)
125{
126 /* Because OpenTTD mixes RoadTypes and TramTypes into the same type,
127 * the mapping of the original road- and tramtypes does not match the default GRF-local mapping.
128 * So, this function cannot provide any similar behavior to GetCargoTranslation() and GetRailTypeTranslation()
129 * when the GRF defines no translation table.
130 * But since there is only one default road/tram-type, this makes little sense anyway.
131 * So for GRF without translation table, we always return INVALID_ROADTYPE.
132 */
133
134 if (grffile == nullptr) return INVALID_ROADTYPE;
135
136 const auto &list = rtt == RTT_TRAM ? grffile->tramtype_list : grffile->roadtype_list;
137 if (tracktype >= list.size()) return INVALID_ROADTYPE;
138
139 /* Look up roadtype including alternate labels. */
140 RoadType result = GetRoadTypeByLabel(list[tracktype]);
141
142 /* Check whether the result is actually the wanted road/tram-type */
143 if (result != INVALID_ROADTYPE && GetRoadTramType(result) != rtt) return INVALID_ROADTYPE;
144
145 return result;
146}
147
154uint8_t GetReverseRoadTypeTranslation(RoadType roadtype, const GRFFile *grffile)
155{
156 /* No road type table present, return road type as-is */
157 if (grffile == nullptr) return roadtype;
158
159 const std::vector<RoadTypeLabel> *list = RoadTypeIsRoad(roadtype) ? &grffile->roadtype_list : &grffile->tramtype_list;
160 if (list->empty()) return roadtype;
161
162 /* Look for a matching road type label in the table */
163 RoadTypeLabel label = GetRoadTypeInfo(roadtype)->label;
164
165 int index = find_index(*list, label);
166 if (index >= 0) return index;
167
168 /* If not found, return as invalid */
169 return 0xFF;
170}
171
172std::vector<LabelObject<RoadTypeLabel>> _roadtype_list;
173
179{
180 std::vector<RoadType> roadtype_conversion_map;
181 bool needs_conversion = false;
182 for (auto it = std::begin(_roadtype_list); it != std::end(_roadtype_list); ++it) {
183 RoadType rt = GetRoadTypeByLabel(it->label);
184 if (rt == INVALID_ROADTYPE || GetRoadTramType(rt) != it->subtype) {
185 rt = it->subtype ? ROADTYPE_TRAM : ROADTYPE_ROAD;
186 }
187
188 roadtype_conversion_map.push_back(rt);
189
190 /* Conversion is needed if the road type is in a different position than the list. */
191 if (it->label != 0 && rt != std::distance(std::begin(_roadtype_list), it)) needs_conversion = true;
192 }
193 if (!needs_conversion) return;
194
195 for (TileIndex t : Map::Iterate()) {
196 switch (GetTileType(t)) {
197 case MP_ROAD:
198 if (RoadType rt = GetRoadTypeRoad(t); rt != INVALID_ROADTYPE) SetRoadTypeRoad(t, roadtype_conversion_map[rt]);
199 if (RoadType rt = GetRoadTypeTram(t); rt != INVALID_ROADTYPE) SetRoadTypeTram(t, roadtype_conversion_map[rt]);
200 break;
201
202 case MP_STATION:
203 if (IsStationRoadStop(t) || IsRoadWaypoint(t)) {
204 if (RoadType rt = GetRoadTypeRoad(t); rt != INVALID_ROADTYPE) SetRoadTypeRoad(t, roadtype_conversion_map[rt]);
205 if (RoadType rt = GetRoadTypeTram(t); rt != INVALID_ROADTYPE) SetRoadTypeTram(t, roadtype_conversion_map[rt]);
206 }
207 break;
208
209 case MP_TUNNELBRIDGE:
211 if (RoadType rt = GetRoadTypeRoad(t); rt != INVALID_ROADTYPE) SetRoadTypeRoad(t, roadtype_conversion_map[rt]);
212 if (RoadType rt = GetRoadTypeTram(t); rt != INVALID_ROADTYPE) SetRoadTypeTram(t, roadtype_conversion_map[rt]);
213 }
214 break;
215
216 default:
217 break;
218 }
219 }
220}
221
224{
225 _roadtype_list.clear();
226 for (RoadType rt = ROADTYPE_BEGIN; rt != ROADTYPE_END; rt++) {
227 _roadtype_list.push_back({GetRoadTypeInfo(rt)->label, GetRoadTramType(rt)});
228 }
229}
230
231void ClearRoadTypeLabelList()
232{
233 _roadtype_list.clear();
234}
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.
RoadTypeLabel label
Unique 32 bit road type identifier.
Definition road.h:137
const SpriteGroup * group[ROTSG_END]
Sprite groups for resolving sprites.
Definition road.h:182
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
@ GSF_INVALID
An invalid spec feature.
Definition newgrf.h:95
@ 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.
void SetCurrentRoadTypeLabelList()
Populate road type label list with current values.
SpriteID GetCustomRoadSprite(const RoadTypeInfo *rti, TileIndex tile, RoadTypeSpriteGroup rtsg, TileContext context, uint *num_results)
Get the sprite to draw for the given tile.
void ConvertRoadTypes()
Test if any saved road type labels are different to the currently loaded road types.
uint8_t GetReverseRoadTypeTranslation(RoadType roadtype, const GRFFile *grffile)
Perform a reverse roadtype lookup to get the GRF internal ID.
RoadType GetRoadTypeTranslation(RoadTramType rtt, uint8_t tracktype, const GRFFile *grffile)
Translate an index to the GRF-local road/tramtype-translation table into a RoadType.
NewGRF handling of road types.
RoadType GetRoadTypeByLabel(RoadTypeLabel label, bool allow_alternate_labels)
Get the road type for a given label.
Definition road.cpp:254
const RoadTypeInfo * GetRoadTypeInfo(RoadType roadtype)
Returns a pointer to the Roadtype information for a given roadtype.
Definition road.h:217
RoadTypeSpriteGroup
Sprite groups for a roadtype.
Definition road.h:49
void SetRoadTypeRoad(Tile t, RoadType rt)
Set the road road type of a tile.
Definition road_map.h:579
void SetRoadTypeTram(Tile t, RoadType rt)
Set the tram road type of a tile.
Definition road_map.h:591
bool IsLevelCrossingTile(Tile t)
Return whether a tile is a level crossing tile.
Definition road_map.h:95
static debug_inline bool IsRoadDepotTile(Tile t)
Return whether a tile is a road depot tile.
Definition road_map.h:116
bool IsCrossingBarred(Tile t)
Check if the level crossing is barred.
Definition road_map.h:416
RoadType
The different roadtypes we support.
Definition road_type.h:25
@ INVALID_ROADTYPE
flag for invalid roadtype
Definition road_type.h:30
@ ROADTYPE_TRAM
Trams.
Definition road_type.h:28
@ ROADTYPE_ROAD
Basic road type.
Definition road_type.h:27
@ ROADTYPE_END
Used for iterations.
Definition road_type.h:29
@ ROADTYPE_BEGIN
Used for iterations.
Definition road_type.h:26
A number of safeguards to prevent using unsafe methods.
bool IsRoadWaypoint(Tile t)
Is the station at t a road waypoint?
bool IsStationRoadStop(Tile t)
Is the station at t a road station?
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< RoadTypeLabel > roadtype_list
Roadtype translation table (road)
Definition newgrf.h:136
std::vector< RoadTypeLabel > tramtype_list
Roadtype translation table (tram)
Definition newgrf.h:139
static IterateWrapper Iterate()
Returns an iterable ensemble of all Tiles.
Definition map_func.h:363
Interface for SpriteGroup-s to access the gamestate.
const SpriteGroup * root_spritegroup
Root SpriteGroup to use for resolving.
Resolver object for road types.
RoadTypeScopeResolver roadtype_scope
Resolver for the roadtype scope.
RoadTypeResolverObject(const RoadTypeInfo *rti, TileIndex tile, TileContext context, RoadTypeSpriteGroup rtsg, uint32_t param1=0, uint32_t param2=0)
Resolver object for road types.
uint32_t GetDebugID() const override
Get an identifier for the item being resolved.
GrfSpecFeature GetFeature() const override
Get the feature number being resolved for.
TileContext context
Are we resolving sprites for the upper halftile, or on a bridge?
uint32_t GetVariable(uint8_t variable, uint32_t parameter, bool &available) const override
Get a variable value.
uint32_t GetRandomBits() const override
Get a few random bits.
TileIndex tile
Tracktile. For track on a bridge this is the southern bridgehead.
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
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_ROAD
Transport by road vehicle.
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...