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/>.
10 /** @file newgrf_railtype.cpp NewGRF handling of rail types. */
14 #include "newgrf_railtype.h"
15 #include "date_func.h"
16 #include "depot_base.h"
20 /* virtual */ uint32
RailTypeScopeResolver::GetRandomBits() const
22 uint tmp
= CountBits(this->tile
+ (TileX(this->tile
) + TileY(this->tile
)) * TILE_SIZE
);
26 /* virtual */ uint32
RailTypeScopeResolver::GetVariable(byte variable
, uint32 parameter
, bool *available
) const
28 if (this->tile
== INVALID_TILE
) {
33 case 0x43: return _date
;
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 (IsRailDepotTile(this->tile
)) return Depot::GetByTile(this->tile
)->build_date
;
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
);
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];
70 * Constructor of the railtype scope resolvers.
71 * @param tile %Tile containing the track. For track on a bridge this is the southern bridgehead.
72 * @param context Are we resolving sprites for the upper halftile, or on a bridge?
74 RailTypeScopeResolver::RailTypeScopeResolver (TileIndex tile
, TileContext context
)
78 this->context
= context
;
82 * Resolver object for rail types.
83 * @param rti Railtype. NULL in NewGRF Inspect window.
84 * @param tile %Tile containing the track. For track on a bridge this is the southern bridgehead.
85 * @param context Are we resolving sprites for the upper halftile, or on a bridge?
86 * @param rtsg Railpart of interest
87 * @param param1 Extra parameter (first parameter of the callback, except railtypes do not have callbacks).
88 * @param param2 Extra parameter (second parameter of the callback, except railtypes do not have callbacks).
90 RailTypeResolverObject::RailTypeResolverObject(const RailtypeInfo
*rti
, TileIndex tile
, TileContext context
, RailTypeSpriteGroup rtsg
, uint32 param1
, uint32 param2
)
91 : ResolverObject(rti
!= NULL
? rti
->grffile
[rtsg
] : NULL
, CBID_NO_CALLBACK
, param1
, param2
),
92 railtype_scope(tile
, context
)
96 static inline const SpriteGroup
*RailTypeResolve (const RailtypeInfo
*rti
,
97 TileIndex tile
, TileContext context
, RailTypeSpriteGroup rtsg
,
98 uint32 param1
= 0, uint32 param2
= 0)
100 RailTypeResolverObject
object (rti
, tile
, context
, rtsg
, param1
, param2
);
101 const SpriteGroup
*root
= rti
!= NULL
? rti
->group
[rtsg
] : NULL
;
102 return SpriteGroup::Resolve (root
, object
);
106 * Get the sprite group to draw for the given tile.
107 * @param rti The rail type data (spec).
108 * @param tile The tile to get the sprite for.
109 * @param rtsg The type of sprite to draw.
110 * @param content Where are we drawing the tile?
111 * @return The sprite group to draw.
113 const SpriteGroup
*GetCustomRailSpriteGroup (const RailtypeInfo
*rti
, TileIndex tile
, RailTypeSpriteGroup rtsg
, TileContext context
)
115 assert(rtsg
< RTSG_END
);
117 if (rti
->group
[rtsg
] == NULL
) return NULL
;
119 const SpriteGroup
*group
= RailTypeResolve (rti
, tile
, context
, rtsg
);
120 if (group
== NULL
|| group
->GetNumResults() == 0) return NULL
;
126 * Get the sprite to draw for the given tile.
127 * @param rti The rail type data (spec).
128 * @param tile The tile to get the sprite for.
129 * @param rtsg The type of sprite to draw.
130 * @param content Where are we drawing the tile?
131 * @return The sprite to draw.
133 SpriteID
GetCustomRailSprite(const RailtypeInfo
*rti
, TileIndex tile
, RailTypeSpriteGroup rtsg
, TileContext context
)
135 const SpriteGroup
*group
= GetCustomRailSpriteGroup (rti
, tile
, rtsg
, context
);
136 return (group
!= NULL
) ? group
->GetResult() : 0;
140 * Get the sprite to draw for a given signal.
141 * @param rti The rail type data (spec).
142 * @param tile The tile to get the sprite for.
143 * @param type Signal type.
144 * @param var Signal variant.
145 * @param state Signal state.
146 * @param gui Is the sprite being used on the map or in the GUI?
147 * @return The sprite to draw.
149 SpriteID
GetCustomSignalSprite(const RailtypeInfo
*rti
, TileIndex tile
, SignalType type
, SignalVariant var
, SignalState state
, bool gui
)
151 if (rti
->group
[RTSG_SIGNALS
] == NULL
) return 0;
153 uint32 param1
= gui
? 0x10 : 0x00;
154 uint32 param2
= (type
<< 16) | (var
<< 8) | state
;
156 const SpriteGroup
*group
= RailTypeResolve (rti
, tile
, TCX_NORMAL
, RTSG_SIGNALS
, param1
, param2
);
157 if (group
== NULL
|| group
->GetNumResults() == 0) return 0;
159 return group
->GetResult();
163 * Perform a reverse railtype lookup to get the GRF internal ID.
164 * @param railtype The global (OpenTTD) railtype.
165 * @param grffile The GRF to do the lookup for.
166 * @return the GRF internal ID.
168 uint8
GetReverseRailTypeTranslation(RailType railtype
, const GRFFile
*grffile
)
170 /* No rail type table present, return rail type as-is */
171 if (grffile
== NULL
|| grffile
->railtype_list
.Length() == 0) return railtype
;
173 /* Look for a matching rail type label in the table */
174 RailTypeLabel label
= GetRailTypeInfo(railtype
)->label
;
175 int index
= grffile
->railtype_list
.FindIndex(label
);
176 if (index
>= 0) return index
;
178 /* If not found, return as invalid */