OpenTTD
newgrf_roadtype.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_roadtype.h"
13 #include "date_func.h"
14 #include "depot_base.h"
15 #include "town.h"
16 
17 #include "safeguards.h"
18 
19 /* virtual */ uint32 RoadTypeScopeResolver::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 RoadTypeScopeResolver::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 (IsRoadDepotTile(this->tile)) return Depot::GetByTile(this->tile)->build_date;
43  return _date;
44  case 0x44: {
45  const Town *t = nullptr;
46  if (IsRoadDepotTile(this->tile)) {
47  t = Depot::GetByTile(this->tile)->town;
48  } else if (IsTileType(this->tile, MP_ROAD)) {
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 road type tile variable 0x%X", variable);
56 
57  *available = false;
58  return UINT_MAX;
59 }
60 
61 /* virtual */ const SpriteGroup *RoadTypeResolverObject::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 
75 {
76  this->tile = tile;
77  this->context = context;
78 }
79 
90  : ResolverObject(rti != nullptr ? rti->grffile[rtsg] : nullptr, CBID_NO_CALLBACK, param1, param2), roadtype_scope(*this, tile, context)
91 {
92  this->root_spritegroup = rti != nullptr ? rti->group[rtsg] : nullptr;
93 }
94 
104 SpriteID GetCustomRoadSprite(const RoadTypeInfo *rti, TileIndex tile, RoadTypeSpriteGroup rtsg, TileContext context, uint *num_results)
105 {
106  assert(rtsg < ROTSG_END);
107 
108  if (rti->group[rtsg] == nullptr) return 0;
109 
110  RoadTypeResolverObject object(rti, tile, context, rtsg);
111  const SpriteGroup *group = object.Resolve();
112  if (group == nullptr || group->GetNumResults() == 0) return 0;
113 
114  if (num_results) *num_results = group->GetNumResults();
115 
116  return group->GetResult();
117 }
118 
126 {
127  /* No road type table present, return road type as-is */
128  if (grffile == nullptr) return roadtype;
129 
130  const std::vector<RoadTypeLabel> *list = RoadTypeIsRoad(roadtype) ? &grffile->roadtype_list : &grffile->tramtype_list;
131  if (list->size() == 0) return roadtype;
132 
133  /* Look for a matching road type label in the table */
134  RoadTypeLabel label = GetRoadTypeInfo(roadtype)->label;
135 
136  int index = find_index(*list, label);
137  if (index >= 0) return index;
138 
139  /* If not found, return as invalid */
140  return 0xFF;
141 }
std::vector< RoadTypeLabel > roadtype_list
Roadtype translation table (road)
Definition: newgrf.h:132
Interface to query and set values specific to a single VarSpriteGroupScope (action 2 scope)...
ResolverObject & ro
Surrounding resolver object.
RoadTypeSpriteGroup
Sprite groups for a roadtype.
Definition: road.h:57
Functions related to dates.
uint8 GetReverseRoadTypeTranslation(RoadType roadtype, const GRFFile *grffile)
Perform a reverse roadtype lookup to get the GRF internal ID.
Functions related to debugging.
A tile with road (or tram tracks)
Definition: tile_type.h:43
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 ...
RoadTypeResolverObject(const RoadTypeInfo *rti, TileIndex tile, TileContext context, RoadTypeSpriteGroup rtsg, uint32 param1=0, uint32 param2=0)
Resolver object for road types.
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:205
Base for all depots (except hangars)
TileContext context
Are we resolving sprites for the upper halftile, or on a bridge?
Set when using the callback resolve system, but not to resolve a callback.
uint32 GetRandomBits() const
Get a few random bits.
byte num_loaded
Number of loaded groups.
RoadType
The different roadtypes we support.
Definition: road_type.h:22
const SpriteGroup * group[ROTSG_END]
Sprite groups for resolving sprites.
Definition: road.h:189
static const uint TILE_SIZE
Tile size in world coordinates.
Definition: tile_type.h:13
uint32 GetVariable(byte variable, uint32 parameter, bool *available) const
Get a variable value.
Date build_date
Date of construction.
Definition: depot_base.h:25
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:3488
static const RoadTypeInfo * GetRoadTypeInfo(RoadType roadtype)
Returns a pointer to the Roadtype information for a given roadtype.
Definition: road.h:224
virtual const SpriteGroup * Resolve(ResolverObject &object) const
Base sprite group resolver.
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
const SpriteGroup ** loaded
List of loaded groups (can be SpriteIDs or Callback results)
SpriteID GetCustomRoadSprite(const RoadTypeInfo *rti, TileIndex tile, RoadTypeSpriteGroup rtsg, TileContext context, uint *num_results)
Get the sprite to draw for the given tile.
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
static bool IsRoadDepotTile(TileIndex t)
Return whether a tile is a road depot tile.
Definition: road_map.h:115
NewGRF handling of road types.
const SpriteGroup ** loading
List of loading groups (can be SpriteIDs or Callback results)
TileIndex tile
Tracktile. For track on a bridge this is the southern bridgehead.
const SpriteGroup * ResolveReal(const RealSpriteGroup *group) const
Get the real sprites of the grf.
HouseZonesBits GetTownRadiusGroup(const Town *t, TileIndex tile)
Returns the bit corresponding to the town zone of the specified tile.
Definition: town_cmd.cpp:2181
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
byte num_loading
Number of loading groups.
const GRFFile * grffile
GRFFile the resolved SpriteGroup belongs to.
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:17
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
static bool IsLevelCrossingTile(TileIndex t)
Return whether a tile is a level crossing tile.
Definition: road_map.h:94
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:215
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:53
static uint CountBits(T value)
Counts the number of set bits in a variable.
Resolver object for road types.
RoadTypeScopeResolver(ResolverObject &ro, TileIndex tile, TileContext context)
Constructor of the roadtype scope resolvers.
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:83
Base of the town class.
static bool IsCrossingBarred(TileIndex t)
Check if the level crossing is barred.
Definition: road_map.h:416
Date _date
Current date in days (day counter)
Definition: date.cpp:26
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_...
RoadTypeLabel label
Unique 32 bit road type identifier.
Definition: road.h:144
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:105
TileContext
Context for tile accesses.