OpenTTD Source  1.11.2
newgrf_railtype.cpp
Go to the documentation of this file.
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  */
7 
10 #include "stdafx.h"
11 #include "debug.h"
12 #include "newgrf_railtype.h"
13 #include "date_func.h"
14 #include "depot_base.h"
15 #include "town.h"
16 
17 #include "safeguards.h"
18 
19 /* virtual */ uint32 RailTypeScopeResolver::GetRandomBits() const
20 {
21  uint tmp = CountBits(this->tile + (TileX(this->tile) + TileY(this->tile)) * TILE_SIZE);
22  return GB(tmp, 0, 2);
23 }
24 
25 /* virtual */ uint32 RailTypeScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
26 {
27  if (this->tile == INVALID_TILE) {
28  switch (variable) {
29  case 0x40: return 0;
30  case 0x41: return 0;
31  case 0x42: return 0;
32  case 0x43: return _date;
33  case 0x44: return HZB_TOWN_EDGE;
34  }
35  }
36 
37  switch (variable) {
38  case 0x40: return GetTerrainType(this->tile, this->context);
39  case 0x41: return 0;
40  case 0x42: return IsLevelCrossingTile(this->tile) && IsCrossingBarred(this->tile);
41  case 0x43:
42  if (IsRailDepotTile(this->tile)) return Depot::GetByTile(this->tile)->build_date;
43  return _date;
44  case 0x44: {
45  const Town *t = nullptr;
46  if (IsRailDepotTile(this->tile)) {
47  t = Depot::GetByTile(this->tile)->town;
48  } else if (IsLevelCrossingTile(this->tile)) {
49  t = ClosestTownFromTile(this->tile, UINT_MAX);
50  }
51  return t != nullptr ? GetTownRadiusGroup(t, this->tile) : HZB_TOWN_EDGE;
52  }
53  }
54 
55  DEBUG(grf, 1, "Unhandled rail type tile variable 0x%X", variable);
56 
57  *available = false;
58  return UINT_MAX;
59 }
60 
61 /* virtual */ const SpriteGroup *RailTypeResolverObject::ResolveReal(const RealSpriteGroup *group) const
62 {
63  if (group->num_loading > 0) return group->loading[0];
64  if (group->num_loaded > 0) return group->loaded[0];
65  return nullptr;
66 }
67 
69 {
70  return GSF_RAILTYPES;
71 }
72 
74 {
75  return this->railtype_scope.rti->label;
76 }
77 
87 RailTypeResolverObject::RailTypeResolverObject(const RailtypeInfo *rti, TileIndex tile, TileContext context, RailTypeSpriteGroup rtsg, uint32 param1, uint32 param2)
88  : ResolverObject(rti != nullptr ? rti->grffile[rtsg] : nullptr, CBID_NO_CALLBACK, param1, param2), railtype_scope(*this, rti, tile, context)
89 {
90  this->root_spritegroup = rti != nullptr ? rti->group[rtsg] : nullptr;
91 }
92 
102 SpriteID GetCustomRailSprite(const RailtypeInfo *rti, TileIndex tile, RailTypeSpriteGroup rtsg, TileContext context, uint *num_results)
103 {
104  assert(rtsg < RTSG_END);
105 
106  if (rti->group[rtsg] == nullptr) return 0;
107 
108  RailTypeResolverObject object(rti, tile, context, rtsg);
109  const SpriteGroup *group = object.Resolve();
110  if (group == nullptr || group->GetNumResults() == 0) return 0;
111 
112  if (num_results) *num_results = group->GetNumResults();
113 
114  return group->GetResult();
115 }
116 
128 {
129  if (rti->group[RTSG_SIGNALS] == nullptr) return 0;
130 
131  uint32 param1 = gui ? 0x10 : 0x00;
132  uint32 param2 = (type << 16) | (var << 8) | state;
133  RailTypeResolverObject object(rti, tile, TCX_NORMAL, RTSG_SIGNALS, param1, param2);
134 
135  const SpriteGroup *group = object.Resolve();
136  if (group == nullptr || group->GetNumResults() == 0) return 0;
137 
138  return group->GetResult();
139 }
140 
147 RailType GetRailTypeTranslation(uint8 railtype, const GRFFile *grffile)
148 {
149  if (grffile == nullptr || grffile->railtype_list.size() == 0) {
150  /* No railtype table present. Return railtype as-is (if valid), so it works for original railtypes. */
151  if (railtype >= RAILTYPE_END || GetRailTypeInfo(static_cast<RailType>(railtype))->label == 0) return INVALID_RAILTYPE;
152 
153  return static_cast<RailType>(railtype);
154  } else {
155  /* Railtype table present, but invalid index, return invalid type. */
156  if (railtype >= grffile->railtype_list.size()) return INVALID_RAILTYPE;
157 
158  /* Look up railtype including alternate labels. */
159  return GetRailTypeByLabel(grffile->railtype_list[railtype]);
160  }
161 }
162 
169 uint8 GetReverseRailTypeTranslation(RailType railtype, const GRFFile *grffile)
170 {
171  /* No rail type table present, return rail type as-is */
172  if (grffile == nullptr || grffile->railtype_list.size() == 0) return railtype;
173 
174  /* Look for a matching rail type label in the table */
175  RailTypeLabel label = GetRailTypeInfo(railtype)->label;
176 
177  int idx = find_index(grffile->railtype_list, label);
178  if (idx >= 0) return idx;
179 
180  /* If not found, return as invalid */
181  return 0xFF;
182 }
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:83
RailTypeScopeResolver::context
TileContext context
Are we resolving sprites for the upper halftile, or on a bridge?
Definition: newgrf_railtype.h:20
RealSpriteGroup::loaded
const SpriteGroup ** loaded
List of loaded groups (can be SpriteIDs or Callback results)
Definition: newgrf_spritegroup.h:92
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:32
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:3598
RealSpriteGroup::num_loaded
byte num_loaded
Number of loaded groups.
Definition: newgrf_spritegroup.h:90
RailTypeScopeResolver::GetRandomBits
uint32 GetRandomBits() const override
Get a few random bits.
Definition: newgrf_railtype.cpp:19
RTSG_SIGNALS
@ RTSG_SIGNALS
Signal images.
Definition: rail.h:58
ResolverObject
Interface for SpriteGroup-s to access the gamestate.
Definition: newgrf_spritegroup.h:315
RailtypeInfo
This struct contains all the info that is needed to draw and construct tracks.
Definition: rail.h:124
TILE_SIZE
static const uint TILE_SIZE
Tile size in world coordinates.
Definition: tile_type.h:13
SignalState
SignalState
These are states in which a signal can be.
Definition: signal_type.h:44
town.h
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:215
RailTypeSpriteGroup
RailTypeSpriteGroup
Sprite groups for a railtype.
Definition: rail.h:46
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:205
CountBits
static uint CountBits(T value)
Counts the number of set bits in a variable.
Definition: bitmath_func.hpp:251
SignalType
SignalType
Type of signal, i.e.
Definition: signal_type.h:23
GetTownRadiusGroup
HouseZonesBits GetTownRadiusGroup(const Town *t, TileIndex tile)
Returns the bit corresponding to the town zone of the specified tile.
Definition: town_cmd.cpp:2256
SpriteID
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:17
GetRailTypeInfo
static const RailtypeInfo * GetRailTypeInfo(RailType railtype)
Returns a pointer to the Railtype information for a given railtype.
Definition: rail.h:304
RealSpriteGroup::loading
const SpriteGroup ** loading
List of loading groups (can be SpriteIDs or Callback results)
Definition: newgrf_spritegroup.h:93
depot_base.h
RailType
RailType
Enumeration for all possible railtypes.
Definition: rail_type.h:27
_date
Date _date
Current date in days (day counter)
Definition: date.cpp:28
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
CBID_NO_CALLBACK
@ CBID_NO_CALLBACK
Set when using the callback resolve system, but not to resolve a callback.
Definition: newgrf_callbacks.h:22
ResolverObject::root_spritegroup
const SpriteGroup * root_spritegroup
Root SpriteGroup to use for resolving.
Definition: newgrf_spritegroup.h:344
find_index
int find_index(std::vector< T > const &vec, T const &item)
Helper function to get the index of an item Consider using std::set, std::unordered_set or std::flat_...
Definition: smallvec_type.hpp:44
IsCrossingBarred
static bool IsCrossingBarred(TileIndex t)
Check if the level crossing is barred.
Definition: road_map.h:416
TileContext
TileContext
Context for tile accesses.
Definition: newgrf_commons.h:24
safeguards.h
IsRailDepotTile
static bool IsRailDepotTile(TileIndex t)
Is this tile rail tile and a rail depot?
Definition: rail_map.h:105
RailTypeResolverObject::GetDebugID
uint32 GetDebugID() const override
Get an identifier for the item being resolved.
Definition: newgrf_railtype.cpp:73
TCX_NORMAL
@ TCX_NORMAL
Nothing special.
Definition: newgrf_commons.h:25
RailTypeScopeResolver::tile
TileIndex tile
Tracktile. For track on a bridge this is the southern bridgehead.
Definition: newgrf_railtype.h:19
date_func.h
stdafx.h
RailTypeScopeResolver::GetVariable
uint32 GetVariable(byte variable, uint32 parameter, bool *available) const override
Get a variable value.
Definition: newgrf_railtype.cpp:25
RailTypeResolverObject::GetFeature
GrfSpecFeature GetFeature() const override
Get the feature number being resolved for.
Definition: newgrf_railtype.cpp:68
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:87
RailTypeResolverObject::ResolveReal
const SpriteGroup * ResolveReal(const RealSpriteGroup *group) const override
Get the real sprites of the grf.
Definition: newgrf_railtype.cpp:61
GrfSpecFeature
GrfSpecFeature
Definition: newgrf.h:66
SpriteGroup::Resolve
virtual const SpriteGroup * Resolve(ResolverObject &object) const
Base sprite group resolver.
Definition: newgrf_spritegroup.h:61
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:102
SignalVariant
SignalVariant
Variant of the signal, i.e.
Definition: signal_type.h:16
RAILTYPE_END
@ RAILTYPE_END
Used for iterations.
Definition: rail_type.h:33
RealSpriteGroup
Definition: newgrf_spritegroup.h:79
RailtypeInfo::label
RailTypeLabel label
Unique 32 bit rail type identifier.
Definition: rail.h:233
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:127
GetRailTypeTranslation
RailType GetRailTypeTranslation(uint8 railtype, const GRFFile *grffile)
Translate an index to the GRF-local railtype-translation table into a RailType.
Definition: newgrf_railtype.cpp:147
IsLevelCrossingTile
static bool IsLevelCrossingTile(TileIndex t)
Return whether a tile is a level crossing tile.
Definition: road_map.h:94
GetReverseRailTypeTranslation
uint8 GetReverseRailTypeTranslation(RailType railtype, const GRFFile *grffile)
Perform a reverse railtype lookup to get the GRF internal ID.
Definition: newgrf_railtype.cpp:169
Depot::build_date
Date build_date
Date of construction.
Definition: depot_base.h:25
Town
Town data structure.
Definition: town.h:50
RealSpriteGroup::num_loading
byte num_loading
Number of loading groups.
Definition: newgrf_spritegroup.h:91
INVALID_TILE
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:88
GetRailTypeByLabel
RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels)
Get the rail type for a given label.
Definition: rail.cpp:311
RailTypeResolverObject
Resolver object for rail types.
Definition: newgrf_railtype.h:39
RailtypeInfo::group
const SpriteGroup * group[RTSG_END]
Sprite groups for resolving sprites.
Definition: rail.h:278
GRFFile::railtype_list
std::vector< RailTypeLabel > railtype_list
Railtype translation table.
Definition: newgrf.h:129
SpriteGroup
Definition: newgrf_spritegroup.h:57
newgrf_railtype.h
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:348
GRFFile
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:105
debug.h
INVALID_RAILTYPE
@ INVALID_RAILTYPE
Flag for invalid railtype.
Definition: rail_type.h:34
RailTypeResolverObject::railtype_scope
RailTypeScopeResolver railtype_scope
Resolver for the railtype scope.
Definition: newgrf_railtype.h:40