OpenTTD
newgrf_roadtype.cpp
Go to the documentation of this file.
1 /* $Id$ */
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_roadtype.h"
15 #include "date_func.h"
16 #include "depot_base.h"
17 #include "town.h"
18 
19 #include "safeguards.h"
20 
21 /* virtual */ uint32 RoadTypeScopeResolver::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 RoadTypeScopeResolver::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 (IsRoadDepotTile(this->tile)) return Depot::GetByTile(this->tile)->build_date;
45  return _date;
46  case 0x44: {
47  const Town *t = nullptr;
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);
52  }
53  return t != nullptr ? GetTownRadiusGroup(t, this->tile) : HZB_TOWN_EDGE;
54  }
55  }
56 
57  DEBUG(grf, 1, "Unhandled road type tile variable 0x%X", variable);
58 
59  *available = false;
60  return UINT_MAX;
61 }
62 
63 /* virtual */ const SpriteGroup *RoadTypeResolverObject::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 nullptr;
68 }
69 
77 {
78  this->tile = tile;
79  this->context = context;
80 }
81 
92  : ResolverObject(rti != nullptr ? rti->grffile[rtsg] : nullptr, CBID_NO_CALLBACK, param1, param2), roadtype_scope(*this, tile, context)
93 {
94  this->root_spritegroup = rti != nullptr ? rti->group[rtsg] : nullptr;
95 }
96 
106 SpriteID GetCustomRoadSprite(const RoadTypeInfo *rti, TileIndex tile, RoadTypeSpriteGroup rtsg, TileContext context, uint *num_results)
107 {
108  assert(rtsg < ROTSG_END);
109 
110  if (rti->group[rtsg] == nullptr) return 0;
111 
112  RoadTypeResolverObject object(rti, tile, context, rtsg);
113  const SpriteGroup *group = object.Resolve();
114  if (group == nullptr || group->GetNumResults() == 0) return 0;
115 
116  if (num_results) *num_results = group->GetNumResults();
117 
118  return group->GetResult();
119 }
120 
128 {
129  /* No road type table present, return road type as-is */
130  if (grffile == nullptr) return roadtype;
131 
132  const std::vector<RoadTypeLabel> *list = RoadTypeIsRoad(roadtype) ? &grffile->roadtype_list : &grffile->tramtype_list;
133  if (list->size() == 0) return roadtype;
134 
135  /* Look for a matching road type label in the table */
136  RoadTypeLabel label = GetRoadTypeInfo(roadtype)->label;
137 
138  int index = find_index(*list, label);
139  if (index >= 0) return index;
140 
141  /* If not found, return as invalid */
142  return 0xFF;
143 }
std::vector< RoadTypeLabel > roadtype_list
Roadtype translation table (road)
Definition: newgrf.h:134
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:59
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:45
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:207
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:27
const SpriteGroup * group[ROTSG_END]
Sprite groups for resolving sprites.
Definition: road.h:191
static const uint TILE_SIZE
Tile size in world coordinates.
Definition: tile_type.h:15
uint32 GetVariable(byte variable, uint32 parameter, bool *available) const
Get a variable value.
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:3499
static const RoadTypeInfo * GetRoadTypeInfo(RoadType roadtype)
Returns a pointer to the Roadtype information for a given roadtype.
Definition: road.h:226
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:152
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:117
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:2191
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:37
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: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:96
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
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:85
Base of the town class.
static bool IsCrossingBarred(TileIndex t)
Check if the level crossing is barred.
Definition: road_map.h:418
Date _date
Current date in days (day counter)
Definition: date.cpp:28
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:146
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:107
TileContext
Context for tile accesses.