OpenTTD Source 20260421-master-gc2fbc6fdeb
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
9
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
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>
41public:
42 typedef typename Types::Tpf Tpf;
43 typedef typename Types::NodeList::Item Node;
44 typedef typename Node::Key Key;
45
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, [[maybe_unused]] Trackdir td)
60 {
61 return IsRailDepotTile(tile);
62 }
63
65 inline bool PfCalcEstimate(Node &n)
66 {
67 n.estimate = n.cost;
68 return true;
69 }
70};
71
72template <class Types>
74public:
75 typedef typename Types::Tpf Tpf;
76 typedef typename Types::NodeList::Item Node;
77 typedef typename Node::Key Key;
78 typedef typename Types::TrackFollower TrackFollower;
79
82 {
83 return *static_cast<Tpf *>(this);
84 }
85
87 inline bool PfDetectDestination(Node &n)
88 {
89 return this->PfDetectDestination(n.GetLastTile(), n.GetLastTrackdir());
90 }
91
94 {
95 return IsSafeWaitingPosition(Yapf().GetVehicle(), tile, td, true, !TrackFollower::Allow90degTurns()) &&
96 IsWaitingPositionFree(Yapf().GetVehicle(), tile, td, !TrackFollower::Allow90degTurns());
97 }
98
100 inline bool PfCalcEstimate(Node &n)
101 {
102 n.estimate = n.cost;
103 return true;
104 }
105};
106
107template <class Types>
109public:
110 typedef typename Types::Tpf Tpf;
111 typedef typename Types::NodeList::Item Node;
112 typedef typename Node::Key Key;
113
114protected:
115 TileIndex dest_tile;
116 TrackdirBits dest_trackdirs;
117 StationID dest_station_id;
118 bool any_depot;
119
122 {
123 return *static_cast<Tpf *>(this);
124 }
125
126public:
127 void SetDestination(const Train *v)
128 {
129 this->any_depot = false;
130 switch (v->current_order.GetType()) {
131 case OT_GOTO_WAYPOINT:
132 if (!Waypoint::Get(v->current_order.GetDestination().ToStationID())->IsSingleTile()) {
133 /* In case of 'complex' waypoints we need to do a look
134 * ahead. This look ahead messes a bit about, which
135 * means that it 'corrupts' the cache. To prevent this
136 * we disable caching when we're looking for a complex
137 * waypoint. */
138 Yapf().DisableCache(true);
139 }
140 [[fallthrough]];
141
142 case OT_GOTO_STATION:
144 this->dest_station_id = v->current_order.GetDestination().ToStationID();
145 this->dest_trackdirs = INVALID_TRACKDIR_BIT;
146 break;
147
148 case OT_GOTO_DEPOT:
150 this->any_depot = true;
151 }
152 [[fallthrough]];
153
154 default:
155 this->dest_tile = v->dest_tile == INVALID_TILE ? TileIndex{} : v->dest_tile;
156 this->dest_station_id = StationID::Invalid();
157 this->dest_trackdirs = TrackStatusToTrackdirBits(GetTileTrackStatus(this->dest_tile, TRANSPORT_RAIL, RoadTramType::Invalid));
158 break;
159 }
160 this->CYapfDestinationRailBase::SetDestination(v);
161 }
162
164 inline bool PfDetectDestination(Node &n)
165 {
166 return this->PfDetectDestination(n.GetLastTile(), n.GetLastTrackdir());
167 }
168
171 {
172 if (this->dest_station_id != StationID::Invalid()) {
173 return HasStationTileRail(tile)
174 && (GetStationIndex(tile) == this->dest_station_id)
175 && (GetRailStationTrack(tile) == TrackdirToTrack(td));
176 }
177
178 if (this->any_depot) {
179 return IsRailDepotTile(tile);
180 }
181
182 return (tile == this->dest_tile) && HasTrackdir(this->dest_trackdirs, td);
183 }
184
186 inline bool PfCalcEstimate(Node &n)
187 {
188 if (this->PfDetectDestination(n)) {
189 n.estimate = n.cost;
190 return true;
191 }
192
193 n.estimate = n.cost + OctileDistanceCost(n.GetLastTile(), n.GetLastTrackdir(), this->dest_tile);
194 assert(n.estimate >= n.parent->estimate);
195 return true;
196 }
197};
198
199#endif /* YAPF_DESTRAIL_HPP */
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Timpl & Set()
Set all bits.
bool PfDetectDestination(TileIndex tile, Trackdir td)
Called by YAPF to detect if node ends in the desired destination.
Types::NodeList::Item Node
this will be our node type
bool PfCalcEstimate(Node &n)
Called by YAPF to calculate cost estimate.
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Tpf & Yapf()
Access the inherited path finder.
Node::Key Key
key to hash tables
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.
bool PfDetectDestination(TileIndex tile, Trackdir td)
Called by YAPF to detect if node ends in the desired destination.
bool PfCalcEstimate(Node &n)
Called by YAPF to calculate cost estimate.
Tpf & Yapf()
Access the inherited path finder.
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
bool PfDetectDestination(Node &n)
Called by YAPF to detect if node ends in the desired destination.
Types::NodeList::Item Node
this will be our node type
Node::Key Key
key to hash tables
Node::Key Key
key to hash tables
bool PfDetectDestination(TileIndex tile, Trackdir td)
Called by YAPF to detect if node ends in the desired destination.
bool PfDetectDestination(Node &n)
Called by YAPF to detect if node ends in the desired destination.
Types::Tpf Tpf
the pathfinder class (derived from THIS class)
Tpf & Yapf()
Access the inherited path finder.
Types::NodeList::Item Node
this will be our node type
bool PfCalcEstimate(Node &n)
Called by YAPF to calculate cost estimate.
TrackStatus GetTileTrackStatus(TileIndex tile, TransportType mode, RoadTramType sub_mode, DiagDirection side)
Returns information about trackdirs and signal states.
@ NearestDepot
Send the vehicle to the nearest depot.
Definition order_type.h:119
General functions related to pathfinders.
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...
General types related to pathfinders.
bool IsWaitingPositionFree(const Train *v, TileIndex tile, Trackdir trackdir, bool forbid_90deg)
Check if a safe position is free.
Definition pbs.cpp:441
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:395
RailTypes GetAllCompatibleRailTypes(RailTypes railtypes)
Returns all compatible railtypes for a set of railtypes.
Definition rail.h:313
static bool IsRailDepotTile(Tile t)
Is this tile rail tile and a rail depot?
Definition rail_map.h:105
EnumBitSet< RailType, uint64_t > RailTypes
Allow incrementing of Track variables.
Definition rail_type.h:38
RailType
Enumeration for all possible railtypes.
Definition rail_type.h:25
@ Invalid
Invalid marker.
Definition road_type.h:41
Track GetRailStationTrack(Tile t)
Get the rail track of a rail station tile.
StationID GetStationIndex(Tile t)
Get StationID from a tile.
Definition station_map.h:28
bool HasStationTileRail(Tile t)
Has this station tile a rail?
@ Rail
Railways/train station.
@ RailWaypoint
Waypoint for trains.
DestinationID GetDestination() const
Gets the destination of this order.
Definition order_base.h:100
bool IsType(OrderType type) const
Check whether this order is of the given type.
Definition order_base.h:67
OrderType GetType() const
Get the type of order of this order.
Definition order_base.h:73
OrderDepotActionFlags GetDepotActionType() const
What are we going to do when in the depot.
Definition order_base.h:176
static Waypoint * Get(auto index)
T * GetMovingFront() const
Get the moving front of the vehicle chain.
'Train' is either a loco or a wagon.
Definition train.h:97
RailTypes railtypes
On which rail types the train can run.
Definition train.h:108
RailTypes compatible_railtypes
With which rail types the train is compatible.
Definition train.h:107
Order current_order
The current order (+ status, like: loading).
TileIndex tile
Current tile index.
TileIndex dest_tile
Heading for this tile.
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:100
Track TrackdirToTrack(Trackdir trackdir)
Returns the Track that a given Trackdir represents.
Definition track_func.h:262
TrackdirBits TrackStatusToTrackdirBits(TrackStatus ts)
Returns the present-trackdir-information of a TrackStatus.
Definition track_func.h:354
bool HasTrackdir(TrackdirBits trackdirs, Trackdir trackdir)
Checks whether a TrackdirBits has a given Trackdir.
Definition track_func.h:342
Trackdir
Enumeration for tracks and directions.
Definition track_type.h:66
TrackdirBits
Allow incrementing of Trackdir variables.
Definition track_type.h:97
@ INVALID_TRACKDIR_BIT
Flag for an invalid trackdirbit value.
Definition track_type.h:113
Base for the train class.
@ TRANSPORT_RAIL
Transport by train.
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.