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_roadtype.cpp NewGRF handling of road types. */
14 #include "newgrf_roadtype.h"
15 #include "date_func.h"
16 #include "depot_base.h"
19 #include "safeguards.h"
21 /* virtual */ uint32
RoadTypeScopeResolver::GetRandomBits() const
23 uint tmp
= CountBits(this->tile
+ (TileX(this->tile
) + TileY(this->tile
)) * TILE_SIZE
);
27 /* virtual */ uint32
RoadTypeScopeResolver::GetVariable(byte variable
, uint32 parameter
, bool *available
) const
29 if (this->tile
== INVALID_TILE
) {
34 case 0x43: return _date
;
35 case 0x44: return HZB_TOWN_EDGE
;
40 case 0x40: return GetTerrainType(this->tile
, this->context
);
42 case 0x42: return IsLevelCrossingTile(this->tile
) && IsCrossingBarred(this->tile
);
44 if (IsRoadDepotTile(this->tile
)) return Depot::GetByTile(this->tile
)->build_date
;
48 if (IsRoadDepotTile(this->tile
)) {
49 t
= Depot::GetByTile(this->tile
)->town
;
50 } else if (IsTileType(this->tile
, MP_ROAD
)) {
51 t
= ClosestTownFromTile(this->tile
, UINT_MAX
);
53 return t
!= NULL
? GetTownRadiusGroup(t
, this->tile
) : HZB_TOWN_EDGE
;
57 DEBUG(grf
, 1, "Unhandled road type tile variable 0x%X", variable
);
63 /* virtual */ const SpriteGroup
*RoadTypeResolverObject::ResolveReal(const RealSpriteGroup
*group
) const
65 if (group
->num_loading
> 0) return group
->loading
[0];
66 if (group
->num_loaded
> 0) return group
->loaded
[0];
71 * Constructor of the roadtype scope resolvers.
72 * @param ro Surrounding resolver.
73 * @param tile %Tile containing the track. For track on a bridge this is the southern bridgehead.
74 * @param context Are we resolving sprites for the upper halftile, or on a bridge?
76 RoadTypeScopeResolver::RoadTypeScopeResolver(ResolverObject
&ro
, TileIndex tile
, TileContext context
) : ScopeResolver(ro
)
79 this->context
= context
;
83 * Resolver object for road types.
84 * @param rti Roadtype. NULL in NewGRF Inspect window.
85 * @param tile %Tile containing the track. For track on a bridge this is the southern bridgehead.
86 * @param context Are we resolving sprites for the upper halftile, or on a bridge?
87 * @param rtsg Roadpart of interest
88 * @param param1 Extra parameter (first parameter of the callback, except roadtypes do not have callbacks).
89 * @param param2 Extra parameter (second parameter of the callback, except roadtypes do not have callbacks).
91 RoadTypeResolverObject::RoadTypeResolverObject(const RoadtypeInfo
*rti
, TileIndex tile
, TileContext context
, RoadTypeSpriteGroup rtsg
, uint32 param1
, uint32 param2
)
92 : ResolverObject(rti
!= NULL
? rti
->grffile
[rtsg
] : NULL
, CBID_NO_CALLBACK
, param1
, param2
), roadtype_scope(*this, tile
, context
)
94 this->root_spritegroup
= rti
!= NULL
? rti
->group
[rtsg
] : NULL
;
98 * Get the sprite to draw for the given tile.
99 * @param rti The road type data (spec).
100 * @param tile The tile to get the sprite for.
101 * @param rtsg The type of sprite to draw.
102 * @param content Where are we drawing the tile?
103 * @param [out] num_results If not NULL, return the number of sprites in the spriteset.
104 * @return The sprite to draw.
106 SpriteID
GetCustomRoadSprite(const RoadtypeInfo
*rti
, TileIndex tile
, RoadTypeSpriteGroup rtsg
, TileContext context
, uint
*num_results
)
108 assert(rtsg
< ROTSG_END
);
110 if (rti
->group
[rtsg
] == NULL
) return 0;
112 RoadTypeResolverObject
object(rti
, tile
, context
, rtsg
);
113 const SpriteGroup
*group
= object
.Resolve();
114 if (group
== NULL
|| group
->GetNumResults() == 0) return 0;
116 if (num_results
) *num_results
= group
->GetNumResults();
118 return group
->GetResult();
122 * Perform a reverse roadtype lookup to get the GRF internal ID.
123 * @param rtid The global (OpenTTD) roadtype.
124 * @param grffile The GRF to do the lookup for.
125 * @return the GRF internal ID.
127 uint8
GetReverseRoadTypeTranslation(RoadTypeIdentifier rtid
, const GRFFile
*grffile
)
129 /* No road type table present, return road type as-is */
130 if (grffile
== NULL
|| grffile
->roadtype_list
[rtid
.basetype
].Length() == 0) return rtid
.subtype
;
132 /* Look for a matching road type label in the table */
133 RoadTypeLabel label
= GetRoadTypeInfo(rtid
)->label
;
134 int index
= grffile
->roadtype_list
[rtid
.basetype
].FindIndex(label
);
135 if (index
>= 0) return index
;
137 /* If not found, return as invalid */