Fix ICU iterators on leading/trailing whitespace
[openttd/fttd.git] / src / newgrf_railtype.cpp
blob5551f09f9e0de9f956f4ac677d029adf1862f3e5
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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 */
10 /** @file newgrf_railtype.cpp NewGRF handling of rail types. */
12 #include "stdafx.h"
13 #include "debug.h"
14 #include "newgrf_railtype.h"
15 #include "date_func.h"
16 #include "depot_base.h"
17 #include "town.h"
18 #include "map/road.h"
20 /* virtual */ uint32 RailTypeScopeResolver::GetRandomBits() const
22 uint tmp = CountBits(this->tile + (TileX(this->tile) + TileY(this->tile)) * TILE_SIZE);
23 return GB(tmp, 0, 2);
26 /* virtual */ uint32 RailTypeScopeResolver::GetVariable(byte variable, uint32 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 _date;
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;
44 return _date;
45 case 0x44: {
46 const Town *t = NULL;
47 if (IsRailDepotTile(this->tile)) {
48 t = Depot::GetByTile(this->tile)->town;
49 } else if (IsLevelCrossingTile(this->tile)) {
50 t = ClosestTownFromTile(this->tile);
52 return t != NULL ? 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 /* virtual */ const SpriteGroup *RailTypeResolverObject::ResolveReal(const RealSpriteGroup *group) const
64 if (group->num_loading > 0) return group->loading[0];
65 if (group->num_loaded > 0) return group->loaded[0];
66 return NULL;
69 /**
70 * Constructor of the railtype scope resolvers.
71 * @param ro Surrounding resolver.
72 * @param tile %Tile containing the track. For track on a bridge this is the southern bridgehead.
73 * @param context Are we resolving sprites for the upper halftile, or on a bridge?
75 RailTypeScopeResolver::RailTypeScopeResolver(ResolverObject &ro, TileIndex tile, TileContext context) : ScopeResolver(ro)
77 this->tile = tile;
78 this->context = context;
81 /**
82 * Resolver object for rail types.
83 * @param tile %Tile containing the track. For track on a bridge this is the southern bridgehead.
84 * @param context Are we resolving sprites for the upper halftile, or on a bridge?
85 * @param grffile The GRF to do the lookup for.
86 * @param param1 Extra parameter (first parameter of the callback, except railtypes do not have callbacks).
87 * @param param2 Extra parameter (second parameter of the callback, except railtypes do not have callbacks).
89 RailTypeResolverObject::RailTypeResolverObject(TileIndex tile, TileContext context, const GRFFile *grffile, uint32 param1, uint32 param2)
90 : ResolverObject(grffile, CBID_NO_CALLBACK, param1, param2), railtype_scope(*this, tile, context)
94 /**
95 * Get the sprite to draw for the given tile.
96 * @param rti The rail type data (spec).
97 * @param tile The tile to get the sprite for.
98 * @param rtsg The type of sprite to draw.
99 * @param content Where are we drawing the tile?
100 * @return The sprite to draw.
102 SpriteID GetCustomRailSprite(const RailtypeInfo *rti, TileIndex tile, RailTypeSpriteGroup rtsg, TileContext context)
104 assert(rtsg < RTSG_END);
106 if (rti->group[rtsg] == NULL) return 0;
108 RailTypeResolverObject object(tile, context, rti->grffile[rtsg]);
109 const SpriteGroup *group = SpriteGroup::Resolve(rti->group[rtsg], object);
110 if (group == NULL || group->GetNumResults() == 0) return 0;
112 return group->GetResult();
116 * Get the sprite to draw for a given signal.
117 * @param rti The rail type data (spec).
118 * @param tile The tile to get the sprite for.
119 * @param type Signal type.
120 * @param var Signal variant.
121 * @param state Signal state.
122 * @param gui Is the sprite being used on the map or in the GUI?
123 * @return The sprite to draw.
125 SpriteID GetCustomSignalSprite(const RailtypeInfo *rti, TileIndex tile, SignalType type, SignalVariant var, SignalState state, bool gui)
127 if (rti->group[RTSG_SIGNALS] == NULL) return 0;
129 uint32 param1 = gui ? 0x10 : 0x00;
130 uint32 param2 = (type << 16) | (var << 8) | state;
131 RailTypeResolverObject object(tile, TCX_NORMAL, rti->grffile[RTSG_SIGNALS], param1, param2);
133 const SpriteGroup *group = SpriteGroup::Resolve(rti->group[RTSG_SIGNALS], object);
134 if (group == NULL || group->GetNumResults() == 0) return 0;
136 return group->GetResult();
140 * Perform a reverse railtype lookup to get the GRF internal ID.
141 * @param railtype The global (OpenTTD) railtype.
142 * @param grffile The GRF to do the lookup for.
143 * @return the GRF internal ID.
145 uint8 GetReverseRailTypeTranslation(RailType railtype, const GRFFile *grffile)
147 /* No rail type table present, return rail type as-is */
148 if (grffile == NULL || grffile->railtype_list.Length() == 0) return railtype;
150 /* Look for a matching rail type label in the table */
151 RailTypeLabel label = GetRailTypeInfo(railtype)->label;
152 int index = grffile->railtype_list.FindIndex(label);
153 if (index >= 0) return index;
155 /* If not found, return as invalid */
156 return 0xFF;