OpenTTD
yapf_destrail.hpp
Go to the documentation of this file.
1 /* $Id: yapf_destrail.hpp 27893 2017-08-13 18:38:42Z 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 #ifndef YAPF_DESTRAIL_HPP
13 #define YAPF_DESTRAIL_HPP
14 
16 protected:
17  RailTypes m_compatible_railtypes;
18 
19 public:
20  void SetDestination(const Train *v, bool override_rail_type = false)
21  {
22  m_compatible_railtypes = v->compatible_railtypes;
23  if (override_rail_type) m_compatible_railtypes |= GetRailTypeInfo(v->railtype)->compatible_railtypes;
24  }
25 
26  bool IsCompatibleRailType(RailType rt)
27  {
28  return HasBit(m_compatible_railtypes, rt);
29  }
30 
31  RailTypes GetCompatibleRailTypes() const
32  {
33  return m_compatible_railtypes;
34  }
35 };
36 
37 template <class Types>
39 public:
40  typedef typename Types::Tpf Tpf;
41  typedef typename Types::NodeList::Titem Node;
42  typedef typename Node::Key Key;
43 
45  Tpf& Yapf()
46  {
47  return *static_cast<Tpf *>(this);
48  }
49 
51  inline bool PfDetectDestination(Node &n)
52  {
53  return PfDetectDestination(n.GetLastTile(), n.GetLastTrackdir());
54  }
55 
57  inline bool PfDetectDestination(TileIndex tile, Trackdir td)
58  {
59  bool bDest = IsRailDepotTile(tile);
60  return bDest;
61  }
62 
67  inline bool PfCalcEstimate(Node &n)
68  {
69  n.m_estimate = n.m_cost;
70  return true;
71  }
72 };
73 
74 template <class Types>
76 public:
77  typedef typename Types::Tpf Tpf;
78  typedef typename Types::NodeList::Titem Node;
79  typedef typename Node::Key Key;
80  typedef typename Types::TrackFollower TrackFollower;
81 
83  Tpf& Yapf()
84  {
85  return *static_cast<Tpf *>(this);
86  }
87 
89  inline bool PfDetectDestination(Node &n)
90  {
91  return PfDetectDestination(n.GetLastTile(), n.GetLastTrackdir());
92  }
93 
95  inline bool PfDetectDestination(TileIndex tile, Trackdir td)
96  {
97  return IsSafeWaitingPosition(Yapf().GetVehicle(), tile, td, true, !TrackFollower::Allow90degTurns()) &&
98  IsWaitingPositionFree(Yapf().GetVehicle(), tile, td, !TrackFollower::Allow90degTurns());
99  }
100 
105  inline bool PfCalcEstimate(Node &n)
106  {
107  n.m_estimate = n.m_cost;
108  return true;
109  }
110 };
111 
112 template <class Types>
114 public:
115  typedef typename Types::Tpf Tpf;
116  typedef typename Types::NodeList::Titem Node;
117  typedef typename Node::Key Key;
118 
119 protected:
120  TileIndex m_destTile;
121  TrackdirBits m_destTrackdirs;
122  StationID m_dest_station_id;
123 
125  Tpf& Yapf()
126  {
127  return *static_cast<Tpf *>(this);
128  }
129 
130 public:
131  void SetDestination(const Train *v)
132  {
133  switch (v->current_order.GetType()) {
134  case OT_GOTO_WAYPOINT:
135  if (!Waypoint::Get(v->current_order.GetDestination())->IsSingleTile()) {
136  /* In case of 'complex' waypoints we need to do a look
137  * ahead. This look ahead messes a bit about, which
138  * means that it 'corrupts' the cache. To prevent this
139  * we disable caching when we're looking for a complex
140  * waypoint. */
141  Yapf().DisableCache(true);
142  }
143  FALLTHROUGH;
144 
145  case OT_GOTO_STATION:
146  m_destTile = CalcClosestStationTile(v->current_order.GetDestination(), v->tile, v->current_order.IsType(OT_GOTO_STATION) ? STATION_RAIL : STATION_WAYPOINT);
147  m_dest_station_id = v->current_order.GetDestination();
148  m_destTrackdirs = INVALID_TRACKDIR_BIT;
149  break;
150 
151  default:
152  m_destTile = v->dest_tile;
153  m_dest_station_id = INVALID_STATION;
155  break;
156  }
157  CYapfDestinationRailBase::SetDestination(v);
158  }
159 
161  inline bool PfDetectDestination(Node &n)
162  {
163  return PfDetectDestination(n.GetLastTile(), n.GetLastTrackdir());
164  }
165 
167  inline bool PfDetectDestination(TileIndex tile, Trackdir td)
168  {
169  bool bDest;
170  if (m_dest_station_id != INVALID_STATION) {
171  bDest = HasStationTileRail(tile)
172  && (GetStationIndex(tile) == m_dest_station_id)
173  && (GetRailStationTrack(tile) == TrackdirToTrack(td));
174  } else {
175  bDest = (tile == m_destTile)
176  && ((m_destTrackdirs & TrackdirToTrackdirBits(td)) != TRACKDIR_BIT_NONE);
177  }
178  return bDest;
179  }
180 
185  inline bool PfCalcEstimate(Node &n)
186  {
187  static const int dg_dir_to_x_offs[] = {-1, 0, 1, 0};
188  static const int dg_dir_to_y_offs[] = {0, 1, 0, -1};
189  if (PfDetectDestination(n)) {
190  n.m_estimate = n.m_cost;
191  return true;
192  }
193 
194  TileIndex tile = n.GetLastTile();
195  DiagDirection exitdir = TrackdirToExitdir(n.GetLastTrackdir());
196  int x1 = 2 * TileX(tile) + dg_dir_to_x_offs[(int)exitdir];
197  int y1 = 2 * TileY(tile) + dg_dir_to_y_offs[(int)exitdir];
198  int x2 = 2 * TileX(m_destTile);
199  int y2 = 2 * TileY(m_destTile);
200  int dx = abs(x1 - x2);
201  int dy = abs(y1 - y2);
202  int dmin = min(dx, dy);
203  int dxy = abs(dx - dy);
204  int d = dmin * YAPF_TILE_CORNER_LENGTH + (dxy - 1) * (YAPF_TILE_LENGTH / 2);
205  n.m_estimate = n.m_cost + d;
206  assert(n.m_estimate >= n.m_parent->m_estimate);
207  return true;
208  }
209 };
210 
211 #endif /* YAPF_DESTRAIL_HPP */
bool PfDetectDestination(Node &n)
Called by YAPF to detect if node ends in the desired destination.
static const RailtypeInfo * GetRailTypeInfo(RailType railtype)
Returns a pointer to the Railtype information for a given railtype.
Definition: rail.h:296
RailTypes
The different roadtypes we support, but then a bitmask of them.
Definition: rail_type.h:52
RailType
Enumeration for all possible railtypes.
Definition: rail_type.h:29
Types::NodeList::Titem Node
this will be our node type
bool PfDetectDestination(TileIndex tile, Trackdir td)
Called by YAPF to detect if node ends in the desired destination.
static Track TrackdirToTrack(Trackdir trackdir)
Returns the Track that a given Trackdir represents.
Definition: track_func.h:272
Flag for an invalid trackdirbit value.
Definition: track_type.h:122
bool PfDetectDestination(TileIndex tile, Trackdir td)
Called by YAPF to detect if node ends in the desired destination.
Node::Key Key
key to hash tables
TileIndex dest_tile
Heading for this tile.
Definition: vehicle_base.h:237
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:207
Tpf & Yapf()
to access inherited path finder
static Track GetRailStationTrack(TileIndex t)
Get the rail track of a rail station tile.
Definition: station_map.h:350
RailTypes compatible_railtypes
bitmask to the OTHER railtypes on which an engine of THIS railtype can physically travel ...
Definition: rail.h:180
static DiagDirection TrackdirToExitdir(Trackdir trackdir)
Maps a trackdir to the (4-way) direction the tile is exited when following that trackdir.
Definition: track_func.h:427
bool PfDetectDestination(Node &n)
Called by YAPF to detect if node ends in the desired destination.
Node::Key Key
key to hash tables
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
static TrackdirBits TrackdirToTrackdirBits(Trackdir trackdir)
Maps a Trackdir to the corresponding TrackdirBits value.
Definition: track_func.h:121
bool IsType(OrderType type) const
Check whether this order is of the given type.
Definition: order_base.h:63
bool IsSafeWaitingPosition(const Train *v, TileIndex tile, Trackdir trackdir, bool include_line_end, bool forbid_90deg)
Determine whether a certain track on a tile is a safe position to end a path.
Definition: pbs.cpp:383
static bool HasStationTileRail(TileIndex t)
Has this station tile a rail? In other words, is this station tile a rail station or rail waypoint...
Definition: station_map.h:147
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
bool IsWaitingPositionFree(const Train *v, TileIndex tile, Trackdir trackdir, bool forbid_90deg)
Check if a safe position is free.
Definition: pbs.cpp:429
bool PfCalcEstimate(Node &n)
Called by YAPF to calculate cost estimate.
static const int YAPF_TILE_LENGTH
Length (penalty) of one tile with YAPF.
Trackdir
Enumeration for tracks and directions.
Definition: track_type.h:74
bool PfCalcEstimate(Node &n)
Called by YAPF to calculate cost estimate.
TileIndex tile
Current tile index.
Definition: vehicle_base.h:230
bool PfDetectDestination(TileIndex tile, Trackdir td)
Called by YAPF to detect if node ends in the desired destination.
Tpf & Yapf()
to access inherited path finder
DiagDirection
Enumeration for diagonal directions.
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:42
Tpf & Yapf()
to access inherited path finder
static TileIndex CalcClosestStationTile(StationID station, TileIndex tile, StationType station_type)
Calculates the tile of given station that is closest to a given tile for this we assume the station i...
TrackStatus GetTileTrackStatus(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
Returns information about trackdirs and signal states.
Definition: landscape.cpp:509
&#39;Train&#39; is either a loco or a wagon.
Definition: train.h:88
Transport by train.
static StationID GetStationIndex(TileIndex t)
Get StationID from a tile.
Definition: station_map.h:29
Types::NodeList::Titem Node
this will be our node type
bool PfCalcEstimate(Node &n)
Called by YAPF to calculate cost estimate.
DestinationID GetDestination() const
Gets the destination of this order.
Definition: order_base.h:96
static bool IsRailDepotTile(TileIndex t)
Is this tile rail tile and a rail depot?
Definition: rail_map.h:106
Types::NodeList::Titem Node
this will be our node type
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:217
static T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition: math_func.hpp:83
TrackdirBits
Enumeration of bitmasks for the TrackDirs.
Definition: track_type.h:106
bool PfDetectDestination(Node &n)
Called by YAPF to detect if node ends in the desired destination.
Types::TrackFollower TrackFollower
TrackFollower. Need to typedef for gcc 2.95.
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
No track build.
Definition: track_type.h:107
OrderType GetType() const
Get the type of order of this order.
Definition: order_base.h:69
static const int YAPF_TILE_CORNER_LENGTH
Length (penalty) of a corner with YAPF.
static Waypoint * Get(size_t index)
Gets station with given index.
static TrackdirBits TrackStatusToTrackdirBits(TrackStatus ts)
Returns the present-trackdir-information of a TrackStatus.
Definition: track_func.h:340
Order current_order
The current order (+ status, like: loading)
Definition: vehicle_base.h:318
Node::Key Key
key to hash tables