Fix cc39fa9: New orders are non-stop by default (#8689)
[openttd-github.git] / src / newgrf_roadtype.cpp
blob025d03bb6a46b14ab276614b012cf9d684215ff7
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 */
8 /** @file newgrf_roadtype.cpp NewGRF handling of road types. */
10 #include "stdafx.h"
11 #include "debug.h"
12 #include "newgrf_roadtype.h"
13 #include "date_func.h"
14 #include "depot_base.h"
15 #include "town.h"
17 #include "safeguards.h"
19 /* virtual */ uint32 RoadTypeScopeResolver::GetRandomBits() const
21 uint tmp = CountBits(this->tile + (TileX(this->tile) + TileY(this->tile)) * TILE_SIZE);
22 return GB(tmp, 0, 2);
25 /* virtual */ uint32 RoadTypeScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
27 if (this->tile == INVALID_TILE) {
28 switch (variable) {
29 case 0x40: return 0;
30 case 0x41: return 0;
31 case 0x42: return 0;
32 case 0x43: return _date;
33 case 0x44: return HZB_TOWN_EDGE;
37 switch (variable) {
38 case 0x40: return GetTerrainType(this->tile, this->context);
39 case 0x41: return 0;
40 case 0x42: return IsLevelCrossingTile(this->tile) && IsCrossingBarred(this->tile);
41 case 0x43:
42 if (IsRoadDepotTile(this->tile)) return Depot::GetByTile(this->tile)->build_date;
43 return _date;
44 case 0x44: {
45 const Town *t = nullptr;
46 if (IsRoadDepotTile(this->tile)) {
47 t = Depot::GetByTile(this->tile)->town;
48 } else {
49 t = ClosestTownFromTile(this->tile, UINT_MAX);
51 return t != nullptr ? GetTownRadiusGroup(t, this->tile) : HZB_TOWN_EDGE;
55 DEBUG(grf, 1, "Unhandled road type tile variable 0x%X", variable);
57 *available = false;
58 return UINT_MAX;
61 /* virtual */ const SpriteGroup *RoadTypeResolverObject::ResolveReal(const RealSpriteGroup *group) const
63 if (group->num_loading > 0) return group->loading[0];
64 if (group->num_loaded > 0) return group->loaded[0];
65 return nullptr;
68 GrfSpecFeature RoadTypeResolverObject::GetFeature() const
70 RoadType rt = GetRoadTypeByLabel(this->roadtype_scope.rti->label, false);
71 switch (GetRoadTramType(rt)) {
72 case RTT_ROAD: return GSF_ROADTYPES;
73 case RTT_TRAM: return GSF_TRAMTYPES;
74 default: return GSF_INVALID;
78 uint32 RoadTypeResolverObject::GetDebugID() const
80 return this->roadtype_scope.rti->label;
83 /**
84 * Constructor of the roadtype scope resolvers.
85 * @param ro Surrounding resolver.
86 * @param tile %Tile containing the track. For track on a bridge this is the southern bridgehead.
87 * @param context Are we resolving sprites for the upper halftile, or on a bridge?
89 RoadTypeScopeResolver::RoadTypeScopeResolver(ResolverObject &ro, const RoadTypeInfo *rti, TileIndex tile, TileContext context) : ScopeResolver(ro)
91 this->tile = tile;
92 this->context = context;
93 this->rti = rti;
96 /**
97 * Resolver object for road types.
98 * @param rti Roadtype. nullptr in NewGRF Inspect window.
99 * @param tile %Tile containing the track. For track on a bridge this is the southern bridgehead.
100 * @param context Are we resolving sprites for the upper halftile, or on a bridge?
101 * @param rtsg Roadpart of interest
102 * @param param1 Extra parameter (first parameter of the callback, except roadtypes do not have callbacks).
103 * @param param2 Extra parameter (second parameter of the callback, except roadtypes do not have callbacks).
105 RoadTypeResolverObject::RoadTypeResolverObject(const RoadTypeInfo *rti, TileIndex tile, TileContext context, RoadTypeSpriteGroup rtsg, uint32 param1, uint32 param2)
106 : ResolverObject(rti != nullptr ? rti->grffile[rtsg] : nullptr, CBID_NO_CALLBACK, param1, param2), roadtype_scope(*this, rti, tile, context)
108 this->root_spritegroup = rti != nullptr ? rti->group[rtsg] : nullptr;
112 * Get the sprite to draw for the given tile.
113 * @param rti The road type data (spec).
114 * @param tile The tile to get the sprite for.
115 * @param rtsg The type of sprite to draw.
116 * @param content Where are we drawing the tile?
117 * @param [out] num_results If not nullptr, return the number of sprites in the spriteset.
118 * @return The sprite to draw.
120 SpriteID GetCustomRoadSprite(const RoadTypeInfo *rti, TileIndex tile, RoadTypeSpriteGroup rtsg, TileContext context, uint *num_results)
122 assert(rtsg < ROTSG_END);
124 if (rti->group[rtsg] == nullptr) return 0;
126 RoadTypeResolverObject object(rti, tile, context, rtsg);
127 const SpriteGroup *group = object.Resolve();
128 if (group == nullptr || group->GetNumResults() == 0) return 0;
130 if (num_results) *num_results = group->GetNumResults();
132 return group->GetResult();
136 * Translate an index to the GRF-local road/tramtype-translation table into a RoadType.
137 * @param rtt Whether to index the road- or tramtype-table.
138 * @param tracktype Index into GRF-local translation table.
139 * @param grffile Originating GRF file.
140 * @return RoadType or INVALID_ROADTYPE if the roadtype is unknown.
142 RoadType GetRoadTypeTranslation(RoadTramType rtt, uint8 tracktype, const GRFFile *grffile)
144 /* Because OpenTTD mixes RoadTypes and TramTypes into the same type,
145 * the mapping of the original road- and tramtypes does not match the default GRF-local mapping.
146 * So, this function cannot provide any similar behavior to GetCargoTranslation() and GetRailTypeTranslation()
147 * when the GRF defines no translation table.
148 * But since there is only one default road/tram-type, this makes little sense anyway.
149 * So for GRF without translation table, we always return INVALID_ROADTYPE.
152 if (grffile == nullptr) return INVALID_ROADTYPE;
154 const auto &list = rtt == RTT_TRAM ? grffile->tramtype_list : grffile->roadtype_list;
155 if (tracktype >= list.size()) return INVALID_ROADTYPE;
157 /* Look up roadtype including alternate labels. */
158 RoadType result = GetRoadTypeByLabel(list[tracktype]);
160 /* Check whether the result is actually the wanted road/tram-type */
161 if (result != INVALID_ROADTYPE && GetRoadTramType(result) != rtt) return INVALID_ROADTYPE;
163 return result;
167 * Perform a reverse roadtype lookup to get the GRF internal ID.
168 * @param roadtype The global (OpenTTD) roadtype.
169 * @param grffile The GRF to do the lookup for.
170 * @return the GRF internal ID.
172 uint8 GetReverseRoadTypeTranslation(RoadType roadtype, const GRFFile *grffile)
174 /* No road type table present, return road type as-is */
175 if (grffile == nullptr) return roadtype;
177 const std::vector<RoadTypeLabel> *list = RoadTypeIsRoad(roadtype) ? &grffile->roadtype_list : &grffile->tramtype_list;
178 if (list->size() == 0) return roadtype;
180 /* Look for a matching road type label in the table */
181 RoadTypeLabel label = GetRoadTypeInfo(roadtype)->label;
183 int index = find_index(*list, label);
184 if (index >= 0) return index;
186 /* If not found, return as invalid */
187 return 0xFF;