OpenTTD

newgrf_railtype.cpp
Go to the documentation of this file.
1 /* $Id: newgrf_railtype.cpp 27984 2018-03-11 13:19:41Z frosch $ */
2
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 */
9
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
19 #include "safeguards.h"
20
21 /* virtual */ uint32 RailTypeScopeResolver::GetRandomBits() const
22 {
23 uint tmp = CountBits(this->tile + (TileX(this->tile) + TileY(this->tile)) * TILE_SIZE);
24 return GB(tmp, 0, 2);
25 }
26
27 /* virtual */ uint32 RailTypeScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
28 {
29 if (this->tile == INVALID_TILE) {
30 switch (variable) {
31 case 0x40: return 0;
32 case 0x41: return 0;
33 case 0x42: return 0;
34 case 0x43: return _date;
35 case 0x44: return HZB_TOWN_EDGE;
36 }
37 }
38
39 switch (variable) {
40 case 0x40: return GetTerrainType(this->tile, this->context);
41 case 0x41: return 0;
42 case 0x42: return IsLevelCrossingTile(this->tile) && IsCrossingBarred(this->tile);
43 case 0x43:
44 if (IsRailDepotTile(this->tile)) return Depot::GetByTile(this->tile)->build_date;
45 return _date;
46 case 0x44: {
47 const Town *t = NULL;
48 if (IsRailDepotTile(this->tile)) {
49 t = Depot::GetByTile(this->tile)->town;
50 } else if (IsLevelCrossingTile(this->tile)) {
51 t = ClosestTownFromTile(this->tile, UINT_MAX);
52 }
53 return t != NULL ? GetTownRadiusGroup(t, this->tile) : HZB_TOWN_EDGE;
54 }
55 }
56
57 DEBUG(grf, 1, "Unhandled rail type tile variable 0x%X", variable);
58
59 *available = false;
60 return UINT_MAX;
61 }
62
63 /* virtual */ const SpriteGroup *RailTypeResolverObject::ResolveReal(const RealSpriteGroup *group) const
64 {
65 if (group->num_loading > 0) return group->loading[0];
66 if (group->num_loaded > 0) return group->loaded[0];
67 return NULL;
68 }
69
79 RailTypeResolverObject::RailTypeResolverObject(const RailtypeInfo *rti, TileIndex tile, TileContext context, RailTypeSpriteGroup rtsg, uint32 param1, uint32 param2)
80 : ResolverObject(rti != NULL ? rti->grffile[rtsg] : NULL, CBID_NO_CALLBACK, param1, param2), railtype_scope(*this, tile, context)
81 {
82 this->root_spritegroup = rti != NULL ? rti->group[rtsg] : NULL;
83 }
84
94 SpriteID GetCustomRailSprite(const RailtypeInfo *rti, TileIndex tile, RailTypeSpriteGroup rtsg, TileContext context, uint *num_results)
95 {
96 assert(rtsg < RTSG_END);
97
98 if (rti->group[rtsg] == NULL) return 0;
99
100 RailTypeResolverObject object(rti, tile, context, rtsg);
101 const SpriteGroup *group = object.Resolve();
102 if (group == NULL || group->GetNumResults() == 0) return 0;
103
104 if (num_results) *num_results = group->GetNumResults();
105
106 return group->GetResult();
107 }
108
119 SpriteID GetCustomSignalSprite(const RailtypeInfo *rti, TileIndex tile, SignalType type, SignalVariant var, SignalState state, bool gui)
120 {
121 if (rti->group[RTSG_SIGNALS] == NULL) return 0;
122
123 uint32 param1 = gui ? 0x10 : 0x00;
124 uint32 param2 = (type << 16) | (var << 8) | state;
125 RailTypeResolverObject object(rti, tile, TCX_NORMAL, RTSG_SIGNALS, param1, param2);
126
127 const SpriteGroup *group = object.Resolve();
128 if (group == NULL || group->GetNumResults() == 0) return 0;
129
130 return group->GetResult();
131 }
132
139 uint8 GetReverseRailTypeTranslation(RailType railtype, const GRFFile *grffile)
140 {
141 /* No rail type table present, return rail type as-is */
142 if (grffile == NULL || grffile->railtype_list.Length() == 0) return railtype;
143
144 /* Look for a matching rail type label in the table */
145 RailTypeLabel label = GetRailTypeInfo(railtype)->label;
146 int index = grffile->railtype_list.FindIndex(label);
147 if (index >= 0) return index;
148
149 /* If not found, return as invalid */
150 return 0xFF;
151 }
RailtypeInfo::group
const SpriteGroup * group[RTSG_END]
Sprite groups for resolving sprites.
Definition: rail.h:270
RailTypeResolverObject
Resolver object for rail types.
Definition: newgrf_railtype.h:40
newgrf_railtype.h
NewGRF handling of rail types.
GetRailTypeInfo
static const RailtypeInfo * GetRailTypeInfo(RailType railtype)
Returns a pointer to the Railtype information for a given railtype.
Definition: rail.h:296
SignalType
SignalType
Type of signal, i.e.
Definition: signal_type.h:25
RailType
RailType
Enumeration for all possible railtypes.
Definition: rail_type.h:29
date_func.h
Functions related to dates.
debug.h
Functions related to debugging.
ResolverObject
Interface for SpriteGroup-s to access the gamestate.
Definition: newgrf_spritegroup.h:310
GetTerrainType
uint32 GetTerrainType(TileIndex tile, TileContext context)
Function used by houses (and soon industries) to get information on type of "terrain" the tile it is ...
Definition: newgrf_commons.cpp:350
SignalState
SignalState
These are states in which a signal can be.
Definition: signal_type.h:46
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:207
depot_base.h
Base for all depots (except hangars)
CBID_NO_CALLBACK
Set when using the callback resolve system, but not to resolve a callback.
Definition: newgrf_callbacks.h:24
TCX_NORMAL
Nothing special.
Definition: newgrf_commons.h:27
RealSpriteGroup::num_loaded
byte num_loaded
Number of loaded groups.
Definition: newgrf_spritegroup.h:90
TILE_SIZE
static const uint TILE_SIZE
Tile size in world coordinates.
Definition: tile_type.h:15
RailTypeScopeResolver::context
TileContext context
Are we resolving sprites for the upper halftile, or on a bridge?
Definition: newgrf_railtype.h:22
RailtypeInfo
This struct contains all the info that is needed to draw and construct tracks.
Definition: rail.h:116
Depot::build_date
Date build_date
Date of construction.
Definition: depot_base.h:27
RealSpriteGroup
Definition: newgrf_spritegroup.h:79
ResolverObject::root_spritegroup
const SpriteGroup * root_spritegroup
Root SpriteGroup to use for resolving.
Definition: newgrf_spritegroup.h:339
ClosestTownFromTile
Town * ClosestTownFromTile(TileIndex tile, uint threshold)
Return the town closest (in distance or ownership) to a given tile, within a given threshold...
Definition: town_cmd.cpp:3239
GetCustomRailSprite
SpriteID GetCustomRailSprite(const RailtypeInfo *rti, TileIndex tile, RailTypeSpriteGroup rtsg, TileContext context, uint *num_results)
Get the sprite to draw for the given tile.
Definition: newgrf_railtype.cpp:94
SpriteGroup::Resolve
virtual const SpriteGroup * Resolve(ResolverObject &object) const
Base sprite group resolver.
Definition: newgrf_spritegroup.h:62
RealSpriteGroup::loaded
const SpriteGroup ** loaded
List of loaded groups (can be SpriteIDs or Callback results)
Definition: newgrf_spritegroup.h:92
SmallVector::Length
uint Length() const
Get the number of items in the list.
Definition: smallvec_type.hpp:262
GRFFile::railtype_list
SmallVector< RailTypeLabel, 4 > railtype_list
Railtype translation table.
Definition: newgrf.h:128
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
safeguards.h
A number of safeguards to prevent using unsafe methods.
SpriteGroup
Definition: newgrf_spritegroup.h:58
RailTypeScopeResolver::tile
TileIndex tile
Tracktile. For track on a bridge this is the southern bridgehead.
Definition: newgrf_railtype.h:21
GetCustomSignalSprite
SpriteID GetCustomSignalSprite(const RailtypeInfo *rti, TileIndex tile, SignalType type, SignalVariant var, SignalState state, bool gui)
Get the sprite to draw for a given signal.
Definition: newgrf_railtype.cpp:119
RealSpriteGroup::loading
const SpriteGroup ** loading
List of loading groups (can be SpriteIDs or Callback results)
Definition: newgrf_spritegroup.h:93
GetTownRadiusGroup
HouseZonesBits GetTownRadiusGroup(const Town *t, TileIndex tile)
Returns the bit corresponding to the town zone of the specified tile.
Definition: town_cmd.cpp:1998
RailtypeInfo::label
RailTypeLabel label
Unique 32 bit rail type identifier.
Definition: rail.h:225
RailTypeResolverObject::ResolveReal
const SpriteGroup * ResolveReal(const RealSpriteGroup *group) const
Get the real sprites of the grf.
Definition: newgrf_railtype.cpp:63
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:39
RealSpriteGroup::num_loading
byte num_loading
Number of loading groups.
Definition: newgrf_spritegroup.h:91
ResolverObject::grffile
const GRFFile * grffile
GRFFile the resolved SpriteGroup belongs to.
Definition: newgrf_spritegroup.h:338
RailTypeSpriteGroup
RailTypeSpriteGroup
Sprite groups for a railtype.
Definition: rail.h:39
SmallVector::FindIndex
int FindIndex(const T &item) const
Search for the first occurrence of an item.
Definition: smallvec_type.hpp:195
IsRailDepotTile
static bool IsRailDepotTile(TileIndex t)
Is this tile rail tile and a rail depot?
Definition: rail_map.h:106
SpriteID
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:19
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
IsLevelCrossingTile
static bool IsLevelCrossingTile(TileIndex t)
Return whether a tile is a level crossing tile.
Definition: road_map.h:78
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:217
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:34
Town
Town data structure.
Definition: town.h:55
GetReverseRailTypeTranslation
uint8 GetReverseRailTypeTranslation(RailType railtype, const GRFFile *grffile)
Perform a reverse railtype lookup to get the GRF internal ID.
Definition: newgrf_railtype.cpp:139
CountBits
static uint CountBits(T value)
Counts the number of set bits in a variable.
Definition: bitmath_func.hpp:253
INVALID_TILE
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:85
town.h
Base of the town class.
IsCrossingBarred
static bool IsCrossingBarred(TileIndex t)
Check if the level crossing is barred.
Definition: road_map.h:386
RTSG_SIGNALS
Signal images.
Definition: rail.h:51
SignalVariant
SignalVariant
Variant of the signal, i.e.
Definition: signal_type.h:18
_date
Date _date
Current date in days (day counter)
Definition: date.cpp:28
RailTypeResolverObject::RailTypeResolverObject
RailTypeResolverObject(const RailtypeInfo *rti, TileIndex tile, TileContext context, RailTypeSpriteGroup rtsg, uint32 param1=0, uint32 param2=0)
Resolver object for rail types.
Definition: newgrf_railtype.cpp:79
RailTypeScopeResolver::GetVariable
uint32 GetVariable(byte variable, uint32 parameter, bool *available) const
Get a variable value.
Definition: newgrf_railtype.cpp:27
RailTypeScopeResolver::GetRandomBits
uint32 GetRandomBits() const
Get a few random bits.
Definition: newgrf_railtype.cpp:21
GRFFile
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:104
TileContext
TileContext
Context for tile accesses.
Definition: newgrf_commons.h:26