OpenTTD Source: src/pathfinder/yapf/yapf_destrail.hpp Source File

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#ifndef YAPF_DESTRAIL_HPP
11#define YAPF_DESTRAIL_HPP
12
13#include "../../train.h"
14#include "../pathfinder_func.h"
15#include "../pathfinder_type.h"
16
17 class CYapfDestinationRailBase {
18protected:
19 RailTypes compatible_railtypes;
20
21public:
22 void SetDestination(const Train *v, bool override_rail_type = false)
23 {
24 this->compatible_railtypes = v->compatible_railtypes;
25 if (override_rail_type) this->compatible_railtypes.Set(GetAllCompatibleRailTypes(v->railtypes));
26 }
27
28 bool IsCompatibleRailType(RailType rt)
29 {
30 return this->compatible_railtypes.Test(rt);
31 }
32
33 RailTypes GetCompatibleRailTypes() const
34 {
35 return this->compatible_railtypes;
36 }
37};
38
39template <class Types>
40 class CYapfDestinationAnyDepotRailT : public CYapfDestinationRailBase {
41public:
42 typedef typename Types::Tpf Tpf;
43 typedef typename Types::NodeList::Item Node;
44 typedef typename Node::Key Key;
45
47 Tpf &Yapf()
48 {
49 return *static_cast<Tpf *>(this);
50 }
51
53 inline bool PfDetectDestination(Node &n)
54 {
55 return this->PfDetectDestination(n.GetLastTile(), n.GetLastTrackdir());
56 }
57
59 inline bool PfDetectDestination(TileIndex tile, Trackdir)
60 {
61 return IsRailDepotTile(tile);
62 }
63
68 inline bool PfCalcEstimate(Node &n)
69 {
70 n.estimate = n.cost;
71 return true;
72 }
73};
74
75template <class Types>
76 class CYapfDestinationAnySafeTileRailT : public CYapfDestinationRailBase {
77public:
78 typedef typename Types::Tpf Tpf;
79 typedef typename Types::NodeList::Item Node;
80 typedef typename Node::Key Key;
81 typedef typename Types::TrackFollower TrackFollower;
82
84 Tpf &Yapf()
85 {
86 return *static_cast<Tpf *>(this);
87 }
88
90 inline bool PfDetectDestination(Node &n)
91 {
92 return this->PfDetectDestination(n.GetLastTile(), n.GetLastTrackdir());
93 }
94
96 inline bool PfDetectDestination(TileIndex tile, Trackdir td)
97 {
98 return IsSafeWaitingPosition(Yapf().GetVehicle(), tile, td, true, !TrackFollower::Allow90degTurns()) &&
99 IsWaitingPositionFree(Yapf().GetVehicle(), tile, td, !TrackFollower::Allow90degTurns());
100 }
101
106 inline bool PfCalcEstimate(Node &n)
107 {
108 n.estimate = n.cost;
109 return true;
110 }
111};
112
113template <class Types>
114 class CYapfDestinationTileOrStationRailT : public CYapfDestinationRailBase {
115public:
116 typedef typename Types::Tpf Tpf;
117 typedef typename Types::NodeList::Item Node;
118 typedef typename Node::Key Key;
119
120protected:
121 TileIndex dest_tile;
122 TrackdirBits dest_trackdirs;
123 StationID dest_station_id;
124 bool any_depot;
125
127 Tpf &Yapf()
128 {
129 return *static_cast<Tpf *>(this);
130 }
131
132public:
133 void SetDestination(const Train *v)
134 {
135 this->any_depot = false;
136 switch (v->current_order.GetType()) {
137 case OT_GOTO_WAYPOINT:
138 if (!Waypoint::Get(v->current_order.GetDestination().ToStationID())->IsSingleTile()) {
139 /* In case of 'complex' waypoints we need to do a look
140 * ahead. This look ahead messes a bit about, which
141 * means that it 'corrupts' the cache. To prevent this
142 * we disable caching when we're looking for a complex
143 * waypoint. */
144 Yapf().DisableCache(true);
145 }
146 [[fallthrough]];
147
148 case OT_GOTO_STATION:
149 this->dest_tile = CalcClosestStationTile(v->current_order.GetDestination().ToStationID(), v->tile, v->current_order.IsType(OT_GOTO_STATION) ? StationType::Rail : StationType::RailWaypoint);
150 this->dest_station_id = v->current_order.GetDestination().ToStationID();
151 this->dest_trackdirs = INVALID_TRACKDIR_BIT;
152 break;
153
154 case OT_GOTO_DEPOT:
155 if (v->current_order.GetDepotActionType().Test(OrderDepotActionFlag::NearestDepot)) {
156 this->any_depot = true;
157 }
158 [[fallthrough]];
159
160 default:
161 this->dest_tile = v->dest_tile;
162 this->dest_station_id = StationID::Invalid();
163 this->dest_trackdirs = TrackStatusToTrackdirBits(GetTileTrackStatus(v->dest_tile, TRANSPORT_RAIL, 0));
164 break;
165 }
166 this->CYapfDestinationRailBase::SetDestination(v);
167 }
168
170 inline bool PfDetectDestination(Node &n)
171 {
172 return this->PfDetectDestination(n.GetLastTile(), n.GetLastTrackdir());
173 }
174
176 inline bool PfDetectDestination(TileIndex tile, Trackdir td)
177 {
178 if (this->dest_station_id != StationID::Invalid()) {
179 return HasStationTileRail(tile)
180 && (GetStationIndex(tile) == this->dest_station_id)
181 && (GetRailStationTrack(tile) == TrackdirToTrack(td));
182 }
183
184 if (this->any_depot) {
185 return IsRailDepotTile(tile);
186 }
187
188 return (tile == this->dest_tile) && HasTrackdir(this->dest_trackdirs, td);
189 }
190
195 inline bool PfCalcEstimate(Node &n)
196 {
197 if (this->PfDetectDestination(n)) {
198 n.estimate = n.cost;
199 return true;
200 }
201
202 n.estimate = n.cost + OctileDistanceCost(n.GetLastTile(), n.GetLastTrackdir(), this->dest_tile);
203 assert(n.estimate >= n.parent->estimate);
204 return true;
205 }
206};
207
208#endif /* YAPF_DESTRAIL_HPP */
BaseBitSet::Test
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Definition base_bitset_type.hpp:141
BaseBitSet::Set
constexpr Timpl & Set()
Set all bits.
Definition base_bitset_type.hpp:40
CYapfDestinationAnyDepotRailT
Definition yapf_destrail.hpp:40
CYapfDestinationAnyDepotRailT::Node
Types::NodeList::Item Node
this will be our node type
Definition yapf_destrail.hpp:43
CYapfDestinationAnyDepotRailT::PfCalcEstimate
bool PfCalcEstimate(Node &n)
Called by YAPF to calculate cost estimate.
Definition yapf_destrail.hpp:68
CYapfDestinationAnyDepotRailT::Tpf
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Definition yapf_destrail.hpp:42
CYapfDestinationAnyDepotRailT::Yapf
Tpf & Yapf()
to access inherited path finder
Definition yapf_destrail.hpp:47
CYapfDestinationAnyDepotRailT::Key
Node::Key Key
key to hash tables
Definition yapf_destrail.hpp:44
CYapfDestinationAnyDepotRailT::PfDetectDestination
bool PfDetectDestination(TileIndex tile, Trackdir)
Called by YAPF to detect if node ends in the desired destination.
Definition yapf_destrail.hpp:59
CYapfDestinationAnyDepotRailT::PfDetectDestination
bool PfDetectDestination(Node &n)
Called by YAPF to detect if node ends in the desired destination.
Definition yapf_destrail.hpp:53
CYapfDestinationAnySafeTileRailT
Definition yapf_destrail.hpp:76
CYapfDestinationAnySafeTileRailT::TrackFollower
Types::TrackFollower TrackFollower
TrackFollower. Need to typedef for gcc 2.95.
Definition yapf_destrail.hpp:81
CYapfDestinationAnySafeTileRailT::PfDetectDestination
bool PfDetectDestination(TileIndex tile, Trackdir td)
Called by YAPF to detect if node ends in the desired destination.
Definition yapf_destrail.hpp:96
CYapfDestinationAnySafeTileRailT::PfCalcEstimate
bool PfCalcEstimate(Node &n)
Called by YAPF to calculate cost estimate.
Definition yapf_destrail.hpp:106
CYapfDestinationAnySafeTileRailT::Yapf
Tpf & Yapf()
to access inherited path finder
Definition yapf_destrail.hpp:84
CYapfDestinationAnySafeTileRailT::Tpf
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Definition yapf_destrail.hpp:78
CYapfDestinationAnySafeTileRailT::PfDetectDestination
bool PfDetectDestination(Node &n)
Called by YAPF to detect if node ends in the desired destination.
Definition yapf_destrail.hpp:90
CYapfDestinationAnySafeTileRailT::Node
Types::NodeList::Item Node
this will be our node type
Definition yapf_destrail.hpp:79
CYapfDestinationAnySafeTileRailT::Key
Node::Key Key
key to hash tables
Definition yapf_destrail.hpp:80
CYapfDestinationRailBase
Definition yapf_destrail.hpp:17
CYapfDestinationTileOrStationRailT
Definition yapf_destrail.hpp:114
CYapfDestinationTileOrStationRailT::Key
Node::Key Key
key to hash tables
Definition yapf_destrail.hpp:118
CYapfDestinationTileOrStationRailT::PfDetectDestination
bool PfDetectDestination(TileIndex tile, Trackdir td)
Called by YAPF to detect if node ends in the desired destination.
Definition yapf_destrail.hpp:176
CYapfDestinationTileOrStationRailT::PfDetectDestination
bool PfDetectDestination(Node &n)
Called by YAPF to detect if node ends in the desired destination.
Definition yapf_destrail.hpp:170
CYapfDestinationTileOrStationRailT::Tpf
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Definition yapf_destrail.hpp:116
CYapfDestinationTileOrStationRailT::Yapf
Tpf & Yapf()
to access inherited path finder
Definition yapf_destrail.hpp:127
CYapfDestinationTileOrStationRailT::Node
Types::NodeList::Item Node
this will be our node type
Definition yapf_destrail.hpp:117
CYapfDestinationTileOrStationRailT::PfCalcEstimate
bool PfCalcEstimate(Node &n)
Called by YAPF to calculate cost estimate.
Definition yapf_destrail.hpp:195
EnumBitSet< RailType, uint64_t >
GetTileTrackStatus
TrackStatus GetTileTrackStatus(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
Returns information about trackdirs and signal states.
Definition landscape.cpp:560
OrderDepotActionFlag::NearestDepot
@ NearestDepot
Send the vehicle to the nearest depot.
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
IsWaitingPositionFree
bool IsWaitingPositionFree(const Train *v, TileIndex tile, Trackdir trackdir, bool forbid_90deg)
Check if a safe position is free.
Definition pbs.cpp:427
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:381
GetAllCompatibleRailTypes
RailTypes GetAllCompatibleRailTypes(RailTypes railtypes)
Returns all compatible railtypes for a set of railtypes.
Definition rail.h:312
IsRailDepotTile
static bool IsRailDepotTile(Tile t)
Is this tile rail tile and a rail depot?
Definition rail_map.h:105
RailType
RailType
Enumeration for all possible railtypes.
Definition rail_type.h:25
GetRailStationTrack
Track GetRailStationTrack(Tile t)
Get the rail track of a rail station tile.
Definition station_map.h:515
GetStationIndex
StationID GetStationIndex(Tile t)
Get StationID from a tile.
Definition station_map.h:28
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
StationType
StationType
Station types.
Definition station_type.h:29
Order::GetDestination
DestinationID GetDestination() const
Gets the destination of this order.
Definition order_base.h:99
Order::IsType
bool IsType(OrderType type) const
Check whether this order is of the given type.
Definition order_base.h:66
Order::GetType
OrderType GetType() const
Get the type of order of this order.
Definition order_base.h:72
Order::GetDepotActionType
OrderDepotActionFlags GetDepotActionType() const
What are we going to do when in the depot.
Definition order_base.h:152
PoolID< uint16_t, struct StationIDTag, 64000, 0xFFFF >
SpecializedStation< Waypoint, true >::Get
static Waypoint * Get(auto index)
Gets station with given index.
Definition base_station_base.h:248
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
Train
'Train' is either a loco or a wagon.
Definition train.h:91
Vehicle::current_order
Order current_order
The current order (+ status, like: loading)
Definition vehicle_base.h:311
Vehicle::tile
TileIndex tile
Current tile index.
Definition vehicle_base.h:225
Vehicle::dest_tile
TileIndex dest_tile
Heading for this tile.
Definition vehicle_base.h:232
Waypoint::IsSingleTile
bool IsSingleTile() const
Is this a single tile waypoint?
Definition waypoint_base.h:62
TrackdirToTrack
Track TrackdirToTrack(Trackdir trackdir)
Returns the Track that a given Trackdir represents.
Definition track_func.h:262
TrackStatusToTrackdirBits
TrackdirBits TrackStatusToTrackdirBits(TrackStatus ts)
Returns the present-trackdir-information of a TrackStatus.
Definition track_func.h:352
HasTrackdir
bool HasTrackdir(TrackdirBits trackdirs, Trackdir trackdir)
Checks whether a TrackdirBits has a given Trackdir.
Definition track_func.h:340
Trackdir
Trackdir
Enumeration for tracks and directions.
Definition track_type.h:66
TrackdirBits
TrackdirBits
Allow incrementing of Trackdir variables.
Definition track_type.h:97
INVALID_TRACKDIR_BIT
@ INVALID_TRACKDIR_BIT
Flag for an invalid trackdirbit value.
Definition track_type.h:113
TRANSPORT_RAIL
@ TRANSPORT_RAIL
Transport by train.
Definition transport_type.h:25
OctileDistanceCost
int OctileDistanceCost(TileIndex start_tile, Trackdir start_td, TileIndex destination_tile)
Calculates the octile distance cost between a starting tile / trackdir and a destination tile.
Definition yapf_common.hpp:104