OpenTTD Source  20240917-master-g9ab0a47812
yapf_destrail.hpp
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 #ifndef YAPF_DESTRAIL_HPP
11 #define YAPF_DESTRAIL_HPP
12 
14 protected:
15  RailTypes m_compatible_railtypes;
16 
17 public:
18  void SetDestination(const Train *v, bool override_rail_type = false)
19  {
20  m_compatible_railtypes = v->compatible_railtypes;
21  if (override_rail_type) m_compatible_railtypes |= GetRailTypeInfo(v->railtype)->compatible_railtypes;
22  }
23 
24  bool IsCompatibleRailType(RailType rt)
25  {
26  return HasBit(m_compatible_railtypes, rt);
27  }
28 
29  RailTypes GetCompatibleRailTypes() const
30  {
31  return m_compatible_railtypes;
32  }
33 };
34 
35 template <class Types>
37 public:
38  typedef typename Types::Tpf Tpf;
39  typedef typename Types::NodeList::Titem Node;
40  typedef typename Node::Key Key;
41 
43  Tpf &Yapf()
44  {
45  return *static_cast<Tpf *>(this);
46  }
47 
49  inline bool PfDetectDestination(Node &n)
50  {
51  return PfDetectDestination(n.GetLastTile(), n.GetLastTrackdir());
52  }
53 
56  {
57  bool bDest = IsRailDepotTile(tile);
58  return bDest;
59  }
60 
65  inline bool PfCalcEstimate(Node &n)
66  {
67  n.m_estimate = n.m_cost;
68  return true;
69  }
70 };
71 
72 template <class Types>
74 public:
75  typedef typename Types::Tpf Tpf;
76  typedef typename Types::NodeList::Titem Node;
77  typedef typename Node::Key Key;
78  typedef typename Types::TrackFollower TrackFollower;
79 
81  Tpf &Yapf()
82  {
83  return *static_cast<Tpf *>(this);
84  }
85 
87  inline bool PfDetectDestination(Node &n)
88  {
89  return PfDetectDestination(n.GetLastTile(), n.GetLastTrackdir());
90  }
91 
93  inline bool PfDetectDestination(TileIndex tile, Trackdir td)
94  {
95  return IsSafeWaitingPosition(Yapf().GetVehicle(), tile, td, true, !TrackFollower::Allow90degTurns()) &&
96  IsWaitingPositionFree(Yapf().GetVehicle(), tile, td, !TrackFollower::Allow90degTurns());
97  }
98 
103  inline bool PfCalcEstimate(Node &n)
104  {
105  n.m_estimate = n.m_cost;
106  return true;
107  }
108 };
109 
110 template <class Types>
112 public:
113  typedef typename Types::Tpf Tpf;
114  typedef typename Types::NodeList::Titem Node;
115  typedef typename Node::Key Key;
116 
117 protected:
118  TileIndex m_destTile;
119  TrackdirBits m_destTrackdirs;
120  StationID m_dest_station_id;
121  bool m_any_depot;
122 
125  {
126  return *static_cast<Tpf *>(this);
127  }
128 
129 public:
130  void SetDestination(const Train *v)
131  {
132  m_any_depot = false;
133  switch (v->current_order.GetType()) {
134  case OT_GOTO_WAYPOINT:
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  case OT_GOTO_DEPOT:
153  m_any_depot = true;
154  }
155  [[fallthrough]];
156 
157  default:
158  m_destTile = v->dest_tile;
159  m_dest_station_id = INVALID_STATION;
161  break;
162  }
163  CYapfDestinationRailBase::SetDestination(v);
164  }
165 
167  inline bool PfDetectDestination(Node &n)
168  {
169  return PfDetectDestination(n.GetLastTile(), n.GetLastTrackdir());
170  }
171 
173  inline bool PfDetectDestination(TileIndex tile, Trackdir td)
174  {
175  if (m_dest_station_id != INVALID_STATION) {
176  return HasStationTileRail(tile)
177  && (GetStationIndex(tile) == m_dest_station_id)
178  && (GetRailStationTrack(tile) == TrackdirToTrack(td));
179  }
180 
181  if (m_any_depot) {
182  return IsRailDepotTile(tile);
183  }
184 
185  return (tile == m_destTile) && HasTrackdir(m_destTrackdirs, td);
186  }
187 
192  inline bool PfCalcEstimate(Node &n)
193  {
194  static const int dg_dir_to_x_offs[] = {-1, 0, 1, 0};
195  static const int dg_dir_to_y_offs[] = {0, 1, 0, -1};
196  if (PfDetectDestination(n)) {
197  n.m_estimate = n.m_cost;
198  return true;
199  }
200 
201  TileIndex tile = n.GetLastTile();
202  DiagDirection exitdir = TrackdirToExitdir(n.GetLastTrackdir());
203  int x1 = 2 * TileX(tile) + dg_dir_to_x_offs[(int)exitdir];
204  int y1 = 2 * TileY(tile) + dg_dir_to_y_offs[(int)exitdir];
205  int x2 = 2 * TileX(m_destTile);
206  int y2 = 2 * TileY(m_destTile);
207  int dx = abs(x1 - x2);
208  int dy = abs(y1 - y2);
209  int dmin = std::min(dx, dy);
210  int dxy = abs(dx - dy);
211  int d = dmin * YAPF_TILE_CORNER_LENGTH + (dxy - 1) * (YAPF_TILE_LENGTH / 2);
212  n.m_estimate = n.m_cost + d;
213  assert(n.m_estimate >= n.m_parent->m_estimate);
214  return true;
215  }
216 };
217 
218 #endif /* YAPF_DESTRAIL_HPP */
CYapfDestinationAnySafeTileRailT::Yapf
Tpf & Yapf()
to access inherited path finder
Definition: yapf_destrail.hpp:81
TileY
static debug_inline uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:437
CYapfDestinationTileOrStationRailT::PfCalcEstimate
bool PfCalcEstimate(Node &n)
Called by YAPF to calculate cost estimate.
Definition: yapf_destrail.hpp:192
CYapfDestinationTileOrStationRailT
Definition: yapf_destrail.hpp:111
Order::IsType
bool IsType(OrderType type) const
Check whether this order is of the given type.
Definition: order_base.h:70
TrackStatusToTrackdirBits
TrackdirBits TrackStatusToTrackdirBits(TrackStatus ts)
Returns the present-trackdir-information of a TrackStatus.
Definition: track_func.h:352
GetRailTypeInfo
const RailTypeInfo * GetRailTypeInfo(RailType railtype)
Returns a pointer to the Railtype information for a given railtype.
Definition: rail.h:307
Order::GetDestination
DestinationID GetDestination() const
Gets the destination of this order.
Definition: order_base.h:103
TrackdirToTrack
Track TrackdirToTrack(Trackdir trackdir)
Returns the Track that a given Trackdir represents.
Definition: track_func.h:262
TrackdirToExitdir
DiagDirection TrackdirToExitdir(Trackdir trackdir)
Maps a trackdir to the (4-way) direction the tile is exited when following that trackdir.
Definition: track_func.h:439
RailType
RailType
Enumeration for all possible railtypes.
Definition: rail_type.h:27
CYapfDestinationTileOrStationRailT::Node
Types::NodeList::Titem Node
this will be our node type
Definition: yapf_destrail.hpp:114
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition: direction_type.h:73
SpecializedStation< Waypoint, true >::Get
static Waypoint * Get(size_t index)
Gets station with given index.
Definition: base_station_base.h:254
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
RailTypes
RailTypes
Allow incrementing of Track variables.
Definition: rail_type.h:44
CYapfDestinationAnySafeTileRailT::Key
Node::Key Key
key to hash tables
Definition: yapf_destrail.hpp:77
CYapfDestinationTileOrStationRailT::Tpf
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Definition: yapf_destrail.hpp:113
GetTileTrackStatus
TrackStatus GetTileTrackStatus(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
Returns information about trackdirs and signal states.
Definition: landscape.cpp:553
CYapfDestinationAnyDepotRailT::Key
Node::Key Key
key to hash tables
Definition: yapf_destrail.hpp:40
Order::GetType
OrderType GetType() const
Get the type of order of this order.
Definition: order_base.h:76
RailTypeInfo::compatible_railtypes
RailTypes compatible_railtypes
bitmask to the OTHER railtypes on which an engine of THIS railtype can physically travel
Definition: rail.h:191
Vehicle::dest_tile
TileIndex dest_tile
Heading for this tile.
Definition: vehicle_base.h:271
IsWaitingPositionFree
bool IsWaitingPositionFree(const Train *v, TileIndex tile, Trackdir trackdir, bool forbid_90deg)
Check if a safe position is free.
Definition: pbs.cpp:426
Waypoint::IsSingleTile
bool IsSingleTile() const
Is this a single tile waypoint?
Definition: waypoint_base.h:62
GetRailStationTrack
Track GetRailStationTrack(Tile t)
Get the rail track of a rail station tile.
Definition: station_map.h:508
YAPF_TILE_LENGTH
static const int YAPF_TILE_LENGTH
Length (penalty) of one tile with YAPF.
Definition: pathfinder_type.h:16
Vehicle::tile
TileIndex tile
Current tile index.
Definition: vehicle_base.h:264
Vehicle::current_order
Order current_order
The current order (+ status, like: loading)
Definition: vehicle_base.h:356
CYapfDestinationAnyDepotRailT::Yapf
Tpf & Yapf()
to access inherited path finder
Definition: yapf_destrail.hpp:43
HasStationTileRail
bool HasStationTileRail(Tile t)
Has this station tile a rail? In other words, is this station tile a rail station or rail waypoint?
Definition: station_map.h:146
ODATFB_NEAREST_DEPOT
@ ODATFB_NEAREST_DEPOT
Send the vehicle to the nearest depot.
Definition: order_type.h:105
CYapfDestinationRailBase
Definition: yapf_destrail.hpp:13
Train
'Train' is either a loco or a wagon.
Definition: train.h:89
CYapfDestinationAnyDepotRailT::Tpf
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Definition: yapf_destrail.hpp:38
IsSafeWaitingPosition
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:380
CYapfDestinationAnyDepotRailT::PfCalcEstimate
bool PfCalcEstimate(Node &n)
Called by YAPF to calculate cost estimate.
Definition: yapf_destrail.hpp:65
CYapfDestinationTileOrStationRailT::Key
Node::Key Key
key to hash tables
Definition: yapf_destrail.hpp:115
CYapfDestinationAnySafeTileRailT::PfDetectDestination
bool PfDetectDestination(Node &n)
Called by YAPF to detect if node ends in the desired destination.
Definition: yapf_destrail.hpp:87
CYapfDestinationAnySafeTileRailT::Node
Types::NodeList::Titem Node
this will be our node type
Definition: yapf_destrail.hpp:76
CYapfDestinationTileOrStationRailT::PfDetectDestination
bool PfDetectDestination(TileIndex tile, Trackdir td)
Called by YAPF to detect if node ends in the desired destination.
Definition: yapf_destrail.hpp:173
CYapfDestinationAnyDepotRailT
Definition: yapf_destrail.hpp:36
CYapfDestinationAnyDepotRailT::Node
Types::NodeList::Titem Node
this will be our node type
Definition: yapf_destrail.hpp:39
Trackdir
Trackdir
Enumeration for tracks and directions.
Definition: track_type.h:67
CYapfDestinationTileOrStationRailT::Yapf
Tpf & Yapf()
to access inherited path finder
Definition: yapf_destrail.hpp:124
IsRailDepotTile
static debug_inline bool IsRailDepotTile(Tile t)
Is this tile rail tile and a rail depot?
Definition: rail_map.h:105
abs
constexpr T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition: math_func.hpp:23
TRANSPORT_RAIL
@ TRANSPORT_RAIL
Transport by train.
Definition: transport_type.h:27
GetStationIndex
StationID GetStationIndex(Tile t)
Get StationID from a tile.
Definition: station_map.h:28
CalcClosestStationTile
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...
Definition: pathfinder_func.h:25
CYapfDestinationAnyDepotRailT::PfDetectDestination
bool PfDetectDestination(TileIndex tile, Trackdir)
Called by YAPF to detect if node ends in the desired destination.
Definition: yapf_destrail.hpp:55
CYapfDestinationAnySafeTileRailT::PfDetectDestination
bool PfDetectDestination(TileIndex tile, Trackdir td)
Called by YAPF to detect if node ends in the desired destination.
Definition: yapf_destrail.hpp:93
CYapfDestinationTileOrStationRailT::PfDetectDestination
bool PfDetectDestination(Node &n)
Called by YAPF to detect if node ends in the desired destination.
Definition: yapf_destrail.hpp:167
CYapfDestinationAnySafeTileRailT::PfCalcEstimate
bool PfCalcEstimate(Node &n)
Called by YAPF to calculate cost estimate.
Definition: yapf_destrail.hpp:103
YAPF_TILE_CORNER_LENGTH
static const int YAPF_TILE_CORNER_LENGTH
Length (penalty) of a corner with YAPF.
Definition: pathfinder_type.h:19
CYapfDestinationAnySafeTileRailT
Definition: yapf_destrail.hpp:73
CYapfDestinationAnyDepotRailT::PfDetectDestination
bool PfDetectDestination(Node &n)
Called by YAPF to detect if node ends in the desired destination.
Definition: yapf_destrail.hpp:49
TrackdirBits
TrackdirBits
Allow incrementing of Trackdir variables.
Definition: track_type.h:98
TileX
static debug_inline uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:427
HasTrackdir
bool HasTrackdir(TrackdirBits trackdirs, Trackdir trackdir)
Checks whether a TrackdirBits has a given Trackdir.
Definition: track_func.h:340
Order::GetDepotActionType
OrderDepotActionFlags GetDepotActionType() const
What are we going to do when in the depot.
Definition: order_base.h:146
CYapfDestinationAnySafeTileRailT::Tpf
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Definition: yapf_destrail.hpp:75
INVALID_TRACKDIR_BIT
@ INVALID_TRACKDIR_BIT
Flag for an invalid trackdirbit value.
Definition: track_type.h:114
CYapfDestinationAnySafeTileRailT::TrackFollower
Types::TrackFollower TrackFollower
TrackFollower. Need to typedef for gcc 2.95.
Definition: yapf_destrail.hpp:78
HasBit
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103