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/>.
8 /** @file newgrf_roadtype.cpp NewGRF handling of road types. */
11 #include "core/container_func.hpp"
13 #include "newgrf_roadtype.h"
14 #include "timer/timer_game_calendar.h"
15 #include "depot_base.h"
18 #include "safeguards.h"
20 /* virtual */ uint32_t RoadTypeScopeResolver::GetRandomBits() const
22 uint tmp
= CountBits(this->tile
.base() + (TileX(this->tile
) + TileY(this->tile
)) * TILE_SIZE
);
26 /* virtual */ uint32_t RoadTypeScopeResolver::GetVariable(uint8_t variable
, [[maybe_unused
]] uint32_t parameter
, bool &available
) const
28 if (this->tile
== INVALID_TILE
) {
33 case 0x43: return TimerGameCalendar::date
.base();
34 case 0x44: return HZB_TOWN_EDGE
;
39 case 0x40: return GetTerrainType(this->tile
, this->context
);
41 case 0x42: return IsLevelCrossingTile(this->tile
) && IsCrossingBarred(this->tile
);
43 if (IsRoadDepotTile(this->tile
)) return Depot::GetByTile(this->tile
)->build_date
.base();
44 return TimerGameCalendar::date
.base();
46 const Town
*t
= nullptr;
47 if (IsRoadDepotTile(this->tile
)) {
48 t
= Depot::GetByTile(this->tile
)->town
;
50 t
= ClosestTownFromTile(this->tile
, UINT_MAX
);
52 return t
!= nullptr ? GetTownRadiusGroup(t
, this->tile
) : HZB_TOWN_EDGE
;
56 Debug(grf
, 1, "Unhandled road type tile variable 0x{:X}", variable
);
62 GrfSpecFeature
RoadTypeResolverObject::GetFeature() const
64 RoadType rt
= GetRoadTypeByLabel(this->roadtype_scope
.rti
->label
, false);
65 switch (GetRoadTramType(rt
)) {
66 case RTT_ROAD
: return GSF_ROADTYPES
;
67 case RTT_TRAM
: return GSF_TRAMTYPES
;
68 default: return GSF_INVALID
;
72 uint32_t RoadTypeResolverObject::GetDebugID() const
74 return this->roadtype_scope
.rti
->label
;
78 * Resolver object for road types.
79 * @param rti Roadtype. nullptr in NewGRF Inspect window.
80 * @param tile %Tile containing the track. For track on a bridge this is the southern bridgehead.
81 * @param context Are we resolving sprites for the upper halftile, or on a bridge?
82 * @param rtsg Roadpart of interest
83 * @param param1 Extra parameter (first parameter of the callback, except roadtypes do not have callbacks).
84 * @param param2 Extra parameter (second parameter of the callback, except roadtypes do not have callbacks).
86 RoadTypeResolverObject::RoadTypeResolverObject(const RoadTypeInfo
*rti
, TileIndex tile
, TileContext context
, RoadTypeSpriteGroup rtsg
, uint32_t param1
, uint32_t param2
)
87 : ResolverObject(rti
!= nullptr ? rti
->grffile
[rtsg
] : nullptr, CBID_NO_CALLBACK
, param1
, param2
), roadtype_scope(*this, rti
, tile
, context
)
89 this->root_spritegroup
= rti
!= nullptr ? rti
->group
[rtsg
] : nullptr;
93 * Get the sprite to draw for the given tile.
94 * @param rti The road type data (spec).
95 * @param tile The tile to get the sprite for.
96 * @param rtsg The type of sprite to draw.
97 * @param content Where are we drawing the tile?
98 * @param [out] num_results If not nullptr, return the number of sprites in the spriteset.
99 * @return The sprite to draw.
101 SpriteID
GetCustomRoadSprite(const RoadTypeInfo
*rti
, TileIndex tile
, RoadTypeSpriteGroup rtsg
, TileContext context
, uint
*num_results
)
103 assert(rtsg
< ROTSG_END
);
105 if (rti
->group
[rtsg
] == nullptr) return 0;
107 RoadTypeResolverObject
object(rti
, tile
, context
, rtsg
);
108 const SpriteGroup
*group
= object
.Resolve();
109 if (group
== nullptr || group
->GetNumResults() == 0) return 0;
111 if (num_results
) *num_results
= group
->GetNumResults();
113 return group
->GetResult();
117 * Translate an index to the GRF-local road/tramtype-translation table into a RoadType.
118 * @param rtt Whether to index the road- or tramtype-table.
119 * @param tracktype Index into GRF-local translation table.
120 * @param grffile Originating GRF file.
121 * @return RoadType or INVALID_ROADTYPE if the roadtype is unknown.
123 RoadType
GetRoadTypeTranslation(RoadTramType rtt
, uint8_t tracktype
, const GRFFile
*grffile
)
125 /* Because OpenTTD mixes RoadTypes and TramTypes into the same type,
126 * the mapping of the original road- and tramtypes does not match the default GRF-local mapping.
127 * So, this function cannot provide any similar behavior to GetCargoTranslation() and GetRailTypeTranslation()
128 * when the GRF defines no translation table.
129 * But since there is only one default road/tram-type, this makes little sense anyway.
130 * So for GRF without translation table, we always return INVALID_ROADTYPE.
133 if (grffile
== nullptr) return INVALID_ROADTYPE
;
135 const auto &list
= rtt
== RTT_TRAM
? grffile
->tramtype_list
: grffile
->roadtype_list
;
136 if (tracktype
>= list
.size()) return INVALID_ROADTYPE
;
138 /* Look up roadtype including alternate labels. */
139 RoadType result
= GetRoadTypeByLabel(list
[tracktype
]);
141 /* Check whether the result is actually the wanted road/tram-type */
142 if (result
!= INVALID_ROADTYPE
&& GetRoadTramType(result
) != rtt
) return INVALID_ROADTYPE
;
148 * Perform a reverse roadtype lookup to get the GRF internal ID.
149 * @param roadtype The global (OpenTTD) roadtype.
150 * @param grffile The GRF to do the lookup for.
151 * @return the GRF internal ID.
153 uint8_t GetReverseRoadTypeTranslation(RoadType roadtype
, const GRFFile
*grffile
)
155 /* No road type table present, return road type as-is */
156 if (grffile
== nullptr) return roadtype
;
158 const std::vector
<RoadTypeLabel
> *list
= RoadTypeIsRoad(roadtype
) ? &grffile
->roadtype_list
: &grffile
->tramtype_list
;
159 if (list
->empty()) return roadtype
;
161 /* Look for a matching road type label in the table */
162 RoadTypeLabel label
= GetRoadTypeInfo(roadtype
)->label
;
164 int index
= find_index(*list
, label
);
165 if (index
>= 0) return index
;
167 /* If not found, return as invalid */