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 
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 
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 
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 }
const SpriteGroup * group[RTSG_END]
Sprite groups for resolving sprites.
Definition: rail.h:270
Resolver object for rail types.
NewGRF handling of rail types.
static const RailtypeInfo * GetRailTypeInfo(RailType railtype)
Returns a pointer to the Railtype information for a given railtype.
Definition: rail.h:296
SignalType
Type of signal, i.e.
Definition: signal_type.h:25
RailType
Enumeration for all possible railtypes.
Definition: rail_type.h:29
Functions related to dates.
Functions related to debugging.
Interface for SpriteGroup-s to access the gamestate.
uint32 GetTerrainType(TileIndex tile, TileContext context)
Function used by houses (and soon industries) to get information on type of "terrain" the tile it is ...
SignalState
These are states in which a signal can be.
Definition: signal_type.h:46
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:207
Base for all depots (except hangars)
Set when using the callback resolve system, but not to resolve a callback.
Nothing special.
byte num_loaded
Number of loaded groups.
static const uint TILE_SIZE
Tile size in world coordinates.
Definition: tile_type.h:15
TileContext context
Are we resolving sprites for the upper halftile, or on a bridge?
This struct contains all the info that is needed to draw and construct tracks.
Definition: rail.h:116
Date build_date
Date of construction.
Definition: depot_base.h:27
const SpriteGroup * root_spritegroup
Root SpriteGroup to use for resolving.
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
SpriteID GetCustomRailSprite(const RailtypeInfo *rti, TileIndex tile, RailTypeSpriteGroup rtsg, TileContext context, uint *num_results)
Get the sprite to draw for the given tile.
virtual const SpriteGroup * Resolve(ResolverObject &object) const
Base sprite group resolver.
const SpriteGroup ** loaded
List of loaded groups (can be SpriteIDs or Callback results)
uint Length() const
Get the number of items in the list.
SmallVector< RailTypeLabel, 4 > railtype_list
Railtype translation table.
Definition: newgrf.h:128
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
TileIndex tile
Tracktile. For track on a bridge this is the southern bridgehead.
SpriteID GetCustomSignalSprite(const RailtypeInfo *rti, TileIndex tile, SignalType type, SignalVariant var, SignalState state, bool gui)
Get the sprite to draw for a given signal.
const SpriteGroup ** loading
List of loading groups (can be SpriteIDs or Callback results)
HouseZonesBits GetTownRadiusGroup(const Town *t, TileIndex tile)
Returns the bit corresponding to the town zone of the specified tile.
Definition: town_cmd.cpp:1998
RailTypeLabel label
Unique 32 bit rail type identifier.
Definition: rail.h:225
const SpriteGroup * ResolveReal(const RealSpriteGroup *group) const
Get the real sprites of the grf.
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:39
byte num_loading
Number of loading groups.
const GRFFile * grffile
GRFFile the resolved SpriteGroup belongs to.
RailTypeSpriteGroup
Sprite groups for a railtype.
Definition: rail.h:39
int FindIndex(const T &item) const
Search for the first occurrence of an item.
static bool IsRailDepotTile(TileIndex t)
Is this tile rail tile and a rail depot?
Definition: rail_map.h:106
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:19
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
static bool IsLevelCrossingTile(TileIndex t)
Return whether a tile is a level crossing tile.
Definition: road_map.h:78
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:217
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Town data structure.
Definition: town.h:55
uint8 GetReverseRailTypeTranslation(RailType railtype, const GRFFile *grffile)
Perform a reverse railtype lookup to get the GRF internal ID.
static uint CountBits(T value)
Counts the number of set bits in a variable.
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:85
Base of the town class.
static bool IsCrossingBarred(TileIndex t)
Check if the level crossing is barred.
Definition: road_map.h:386
Signal images.
Definition: rail.h:51
SignalVariant
Variant of the signal, i.e.
Definition: signal_type.h:18
Date _date
Current date in days (day counter)
Definition: date.cpp:28
RailTypeResolverObject(const RailtypeInfo *rti, TileIndex tile, TileContext context, RailTypeSpriteGroup rtsg, uint32 param1=0, uint32 param2=0)
Resolver object for rail types.
uint32 GetVariable(byte variable, uint32 parameter, bool *available) const
Get a variable value.
uint32 GetRandomBits() const
Get a few random bits.
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:104
TileContext
Context for tile accesses.