Feature: NewGRF callback profiling (#7868)
[openttd-github.git] / src / newgrf_railtype.cpp
blob2a98948e7b60de0b4ffc58b1c2386f1666049ced
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_railtype.cpp NewGRF handling of rail types. */
10 #include "stdafx.h"
11 #include "debug.h"
12 #include "newgrf_railtype.h"
13 #include "date_func.h"
14 #include "depot_base.h"
15 #include "town.h"
17 #include "safeguards.h"
19 /* virtual */ uint32 RailTypeScopeResolver::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 RailTypeScopeResolver::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 (IsRailDepotTile(this->tile)) return Depot::GetByTile(this->tile)->build_date;
43 return _date;
44 case 0x44: {
45 const Town *t = nullptr;
46 if (IsRailDepotTile(this->tile)) {
47 t = Depot::GetByTile(this->tile)->town;
48 } else if (IsLevelCrossingTile(this->tile)) {
49 t = ClosestTownFromTile(this->tile, UINT_MAX);
51 return t != nullptr ? GetTownRadiusGroup(t, this->tile) : HZB_TOWN_EDGE;
55 DEBUG(grf, 1, "Unhandled rail type tile variable 0x%X", variable);
57 *available = false;
58 return UINT_MAX;
61 /* virtual */ const SpriteGroup *RailTypeResolverObject::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 RailTypeResolverObject::GetFeature() const
70 return GSF_RAILTYPES;
73 uint32 RailTypeResolverObject::GetDebugID() const
75 return this->railtype_scope.rti->label;
78 /**
79 * Resolver object for rail types.
80 * @param rti Railtype. nullptr in NewGRF Inspect window.
81 * @param tile %Tile containing the track. For track on a bridge this is the southern bridgehead.
82 * @param context Are we resolving sprites for the upper halftile, or on a bridge?
83 * @param rtsg Railpart of interest
84 * @param param1 Extra parameter (first parameter of the callback, except railtypes do not have callbacks).
85 * @param param2 Extra parameter (second parameter of the callback, except railtypes do not have callbacks).
87 RailTypeResolverObject::RailTypeResolverObject(const RailtypeInfo *rti, TileIndex tile, TileContext context, RailTypeSpriteGroup rtsg, uint32 param1, uint32 param2)
88 : ResolverObject(rti != nullptr ? rti->grffile[rtsg] : nullptr, CBID_NO_CALLBACK, param1, param2), railtype_scope(*this, rti, tile, context)
90 this->root_spritegroup = rti != nullptr ? rti->group[rtsg] : nullptr;
93 /**
94 * Get the sprite to draw for the given tile.
95 * @param rti The rail type data (spec).
96 * @param tile The tile to get the sprite for.
97 * @param rtsg The type of sprite to draw.
98 * @param context Where are we drawing the tile?
99 * @param[out] num_results If not nullptr, return the number of sprites in the spriteset.
100 * @return The sprite to draw.
102 SpriteID GetCustomRailSprite(const RailtypeInfo *rti, TileIndex tile, RailTypeSpriteGroup rtsg, TileContext context, uint *num_results)
104 assert(rtsg < RTSG_END);
106 if (rti->group[rtsg] == nullptr) return 0;
108 RailTypeResolverObject object(rti, tile, context, rtsg);
109 const SpriteGroup *group = object.Resolve();
110 if (group == nullptr || group->GetNumResults() == 0) return 0;
112 if (num_results) *num_results = group->GetNumResults();
114 return group->GetResult();
118 * Get the sprite to draw for a given signal.
119 * @param rti The rail type data (spec).
120 * @param tile The tile to get the sprite for.
121 * @param type Signal type.
122 * @param var Signal variant.
123 * @param state Signal state.
124 * @param gui Is the sprite being used on the map or in the GUI?
125 * @return The sprite to draw.
127 SpriteID GetCustomSignalSprite(const RailtypeInfo *rti, TileIndex tile, SignalType type, SignalVariant var, SignalState state, bool gui)
129 if (rti->group[RTSG_SIGNALS] == nullptr) return 0;
131 uint32 param1 = gui ? 0x10 : 0x00;
132 uint32 param2 = (type << 16) | (var << 8) | state;
133 RailTypeResolverObject object(rti, tile, TCX_NORMAL, RTSG_SIGNALS, param1, param2);
135 const SpriteGroup *group = object.Resolve();
136 if (group == nullptr || group->GetNumResults() == 0) return 0;
138 return group->GetResult();
142 * Perform a reverse railtype lookup to get the GRF internal ID.
143 * @param railtype The global (OpenTTD) railtype.
144 * @param grffile The GRF to do the lookup for.
145 * @return the GRF internal ID.
147 uint8 GetReverseRailTypeTranslation(RailType railtype, const GRFFile *grffile)
149 /* No rail type table present, return rail type as-is */
150 if (grffile == nullptr || grffile->railtype_list.size() == 0) return railtype;
152 /* Look for a matching rail type label in the table */
153 RailTypeLabel label = GetRailTypeInfo(railtype)->label;
155 int idx = find_index(grffile->railtype_list, label);
156 if (idx >= 0) return idx;
158 /* If not found, return as invalid */
159 return 0xFF;