Update: Translations from eints
[openttd-github.git] / src / newgrf_railtype.cpp
blobf6ca99148d25a1a24e2bca60ed0fada2d3d4aa01
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 "core/container_func.hpp"
12 #include "debug.h"
13 #include "newgrf_railtype.h"
14 #include "timer/timer_game_calendar.h"
15 #include "depot_base.h"
16 #include "town.h"
18 #include "safeguards.h"
20 /* virtual */ uint32_t RailTypeScopeResolver::GetRandomBits() const
22 uint tmp = CountBits(this->tile.base() + (TileX(this->tile) + TileY(this->tile)) * TILE_SIZE);
23 return GB(tmp, 0, 2);
26 /* virtual */ uint32_t RailTypeScopeResolver::GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const
28 if (this->tile == INVALID_TILE) {
29 switch (variable) {
30 case 0x40: return 0;
31 case 0x41: return 0;
32 case 0x42: return 0;
33 case 0x43: return TimerGameCalendar::date.base();
34 case 0x44: return HZB_TOWN_EDGE;
38 switch (variable) {
39 case 0x40: return GetTerrainType(this->tile, this->context);
40 case 0x41: return 0;
41 case 0x42: return IsLevelCrossingTile(this->tile) && IsCrossingBarred(this->tile);
42 case 0x43:
43 if (IsRailDepotTile(this->tile)) return Depot::GetByTile(this->tile)->build_date.base();
44 return TimerGameCalendar::date.base();
45 case 0x44: {
46 const Town *t = nullptr;
47 if (IsRailDepotTile(this->tile)) {
48 t = Depot::GetByTile(this->tile)->town;
49 } else if (IsLevelCrossingTile(this->tile)) {
50 t = ClosestTownFromTile(this->tile, UINT_MAX);
52 return t != nullptr ? GetTownRadiusGroup(t, this->tile) : HZB_TOWN_EDGE;
56 Debug(grf, 1, "Unhandled rail type tile variable 0x{:X}", variable);
58 available = false;
59 return UINT_MAX;
62 GrfSpecFeature RailTypeResolverObject::GetFeature() const
64 return GSF_RAILTYPES;
67 uint32_t RailTypeResolverObject::GetDebugID() const
69 return this->railtype_scope.rti->label;
72 /**
73 * Resolver object for rail types.
74 * @param rti Railtype. nullptr in NewGRF Inspect window.
75 * @param tile %Tile containing the track. For track on a bridge this is the southern bridgehead.
76 * @param context Are we resolving sprites for the upper halftile, or on a bridge?
77 * @param rtsg Railpart of interest
78 * @param param1 Extra parameter (first parameter of the callback, except railtypes do not have callbacks).
79 * @param param2 Extra parameter (second parameter of the callback, except railtypes do not have callbacks).
81 RailTypeResolverObject::RailTypeResolverObject(const RailTypeInfo *rti, TileIndex tile, TileContext context, RailTypeSpriteGroup rtsg, uint32_t param1, uint32_t param2)
82 : ResolverObject(rti != nullptr ? rti->grffile[rtsg] : nullptr, CBID_NO_CALLBACK, param1, param2), railtype_scope(*this, rti, tile, context)
84 this->root_spritegroup = rti != nullptr ? rti->group[rtsg] : nullptr;
87 /**
88 * Get the sprite to draw for the given tile.
89 * @param rti The rail type data (spec).
90 * @param tile The tile to get the sprite for.
91 * @param rtsg The type of sprite to draw.
92 * @param context Where are we drawing the tile?
93 * @param[out] num_results If not nullptr, return the number of sprites in the spriteset.
94 * @return The sprite to draw.
96 SpriteID GetCustomRailSprite(const RailTypeInfo *rti, TileIndex tile, RailTypeSpriteGroup rtsg, TileContext context, uint *num_results)
98 assert(rtsg < RTSG_END);
100 if (rti->group[rtsg] == nullptr) return 0;
102 RailTypeResolverObject object(rti, tile, context, rtsg);
103 const SpriteGroup *group = object.Resolve();
104 if (group == nullptr || group->GetNumResults() == 0) return 0;
106 if (num_results) *num_results = group->GetNumResults();
108 return group->GetResult();
112 * Get the sprite to draw for a given signal.
113 * @param rti The rail type data (spec).
114 * @param tile The tile to get the sprite for.
115 * @param type Signal type.
116 * @param var Signal variant.
117 * @param state Signal state.
118 * @param gui Is the sprite being used on the map or in the GUI?
119 * @return The sprite to draw.
121 SpriteID GetCustomSignalSprite(const RailTypeInfo *rti, TileIndex tile, SignalType type, SignalVariant var, SignalState state, bool gui)
123 if (rti->group[RTSG_SIGNALS] == nullptr) return 0;
125 uint32_t param1 = gui ? 0x10 : 0x00;
126 uint32_t param2 = (type << 16) | (var << 8) | state;
127 RailTypeResolverObject object(rti, tile, TCX_NORMAL, RTSG_SIGNALS, param1, param2);
129 const SpriteGroup *group = object.Resolve();
130 if (group == nullptr || group->GetNumResults() == 0) return 0;
132 return group->GetResult();
136 * Translate an index to the GRF-local railtype-translation table into a RailType.
137 * @param railtype Index into GRF-local translation table.
138 * @param grffile Originating GRF file.
139 * @return RailType or INVALID_RAILTYPE if the railtype is unknown.
141 RailType GetRailTypeTranslation(uint8_t railtype, const GRFFile *grffile)
143 if (grffile == nullptr || grffile->railtype_list.empty()) {
144 /* No railtype table present. Return railtype as-is (if valid), so it works for original railtypes. */
145 if (railtype >= RAILTYPE_END || GetRailTypeInfo(static_cast<RailType>(railtype))->label == 0) return INVALID_RAILTYPE;
147 return static_cast<RailType>(railtype);
148 } else {
149 /* Railtype table present, but invalid index, return invalid type. */
150 if (railtype >= grffile->railtype_list.size()) return INVALID_RAILTYPE;
152 /* Look up railtype including alternate labels. */
153 return GetRailTypeByLabel(grffile->railtype_list[railtype]);
158 * Perform a reverse railtype lookup to get the GRF internal ID.
159 * @param railtype The global (OpenTTD) railtype.
160 * @param grffile The GRF to do the lookup for.
161 * @return the GRF internal ID.
163 uint8_t GetReverseRailTypeTranslation(RailType railtype, const GRFFile *grffile)
165 /* No rail type table present, return rail type as-is */
166 if (grffile == nullptr || grffile->railtype_list.empty()) return railtype;
168 /* Look for a matching rail type label in the table */
169 RailTypeLabel label = GetRailTypeInfo(railtype)->label;
171 int idx = find_index(grffile->railtype_list, label);
172 if (idx >= 0) return idx;
174 /* If not found, return as invalid */
175 return 0xFF;