Update: Translations from eints
[openttd-github.git] / src / pathfinder / follow_track.hpp
blobd384568b62f7454bf226f3b7f035ca0002b49ecd
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 */
8 /** @file follow_track.hpp Template function for track followers */
10 #ifndef FOLLOW_TRACK_HPP
11 #define FOLLOW_TRACK_HPP
13 #include "../pbs.h"
14 #include "../roadveh.h"
15 #include "../station_base.h"
16 #include "../train.h"
17 #include "../tunnelbridge.h"
18 #include "../tunnelbridge_map.h"
19 #include "../depot_map.h"
20 #include "pathfinder_func.h"
22 /**
23 * Track follower helper template class (can serve pathfinders and vehicle
24 * controllers). See 6 different typedefs below for 3 different transport
25 * types w/ or w/o 90-deg turns allowed
27 template <TransportType Ttr_type_, typename VehicleType, bool T90deg_turns_allowed_ = true, bool Tmask_reserved_tracks = false>
28 struct CFollowTrackT
30 enum ErrorCode {
31 EC_NONE,
32 EC_OWNER,
33 EC_RAIL_ROAD_TYPE,
34 EC_90DEG,
35 EC_NO_WAY,
36 EC_RESERVED,
39 const VehicleType *m_veh; ///< moving vehicle
40 Owner m_veh_owner; ///< owner of the vehicle
41 TileIndex m_old_tile; ///< the origin (vehicle moved from) before move
42 Trackdir m_old_td; ///< the trackdir (the vehicle was on) before move
43 TileIndex m_new_tile; ///< the new tile (the vehicle has entered)
44 TrackdirBits m_new_td_bits; ///< the new set of available trackdirs
45 DiagDirection m_exitdir; ///< exit direction (leaving the old tile)
46 bool m_is_tunnel; ///< last turn passed tunnel
47 bool m_is_bridge; ///< last turn passed bridge ramp
48 bool m_is_station; ///< last turn passed station
49 int m_tiles_skipped; ///< number of skipped tunnel or station tiles
50 ErrorCode m_err;
51 RailTypes m_railtypes;
53 inline CFollowTrackT(const VehicleType *v = nullptr, RailTypes railtype_override = INVALID_RAILTYPES)
55 Init(v, railtype_override);
58 inline CFollowTrackT(Owner o, RailTypes railtype_override = INVALID_RAILTYPES)
60 assert(IsRailTT());
61 m_veh = nullptr;
62 Init(o, railtype_override);
65 inline void Init(const VehicleType *v, RailTypes railtype_override)
67 assert(!IsRailTT() || (v != nullptr && v->type == VEH_TRAIN));
68 m_veh = v;
69 Init(v != nullptr ? v->owner : INVALID_OWNER, IsRailTT() && railtype_override == INVALID_RAILTYPES ? Train::From(v)->compatible_railtypes : railtype_override);
72 inline void Init(Owner o, RailTypes railtype_override)
74 assert(!IsRoadTT() || m_veh != nullptr);
75 assert(!IsRailTT() || railtype_override != INVALID_RAILTYPES);
76 m_veh_owner = o;
77 /* don't worry, all is inlined so compiler should remove unnecessary initializations */
78 m_old_tile = INVALID_TILE;
79 m_old_td = INVALID_TRACKDIR;
80 m_new_tile = INVALID_TILE;
81 m_new_td_bits = TRACKDIR_BIT_NONE;
82 m_exitdir = INVALID_DIAGDIR;
83 m_is_station = m_is_bridge = m_is_tunnel = false;
84 m_tiles_skipped = 0;
85 m_err = EC_NONE;
86 m_railtypes = railtype_override;
89 debug_inline static TransportType TT() { return Ttr_type_; }
90 debug_inline static bool IsWaterTT() { return TT() == TRANSPORT_WATER; }
91 debug_inline static bool IsRailTT() { return TT() == TRANSPORT_RAIL; }
92 inline bool IsTram() { return IsRoadTT() && RoadTypeIsTram(RoadVehicle::From(m_veh)->roadtype); }
93 debug_inline static bool IsRoadTT() { return TT() == TRANSPORT_ROAD; }
94 inline static bool Allow90degTurns() { return T90deg_turns_allowed_; }
95 inline static bool DoTrackMasking() { return Tmask_reserved_tracks; }
97 /** Tests if a tile is a road tile with a single tramtrack (tram can reverse) */
98 inline DiagDirection GetSingleTramBit(TileIndex tile)
100 assert(IsTram()); // this function shouldn't be called in other cases
102 if (IsNormalRoadTile(tile)) {
103 RoadBits rb = GetRoadBits(tile, RTT_TRAM);
104 switch (rb) {
105 case ROAD_NW: return DIAGDIR_NW;
106 case ROAD_SW: return DIAGDIR_SW;
107 case ROAD_SE: return DIAGDIR_SE;
108 case ROAD_NE: return DIAGDIR_NE;
109 default: break;
112 return INVALID_DIAGDIR;
116 * main follower routine. Fills all members and return true on success.
117 * Otherwise returns false if track can't be followed.
119 inline bool Follow(TileIndex old_tile, Trackdir old_td)
121 m_old_tile = old_tile;
122 m_old_td = old_td;
123 m_err = EC_NONE;
125 assert([&]() {
126 if (IsTram() && GetSingleTramBit(m_old_tile) != INVALID_DIAGDIR) return true; // Skip the check for single tram bits
127 const uint sub_mode = (IsRoadTT() && m_veh != nullptr) ? (this->IsTram() ? RTT_TRAM : RTT_ROAD) : 0;
128 const TrackdirBits old_tile_valid_dirs = TrackStatusToTrackdirBits(GetTileTrackStatus(m_old_tile, TT(), sub_mode));
129 return (old_tile_valid_dirs & TrackdirToTrackdirBits(m_old_td)) != TRACKDIR_BIT_NONE;
130 }());
132 m_exitdir = TrackdirToExitdir(m_old_td);
133 if (ForcedReverse()) return true;
134 if (!CanExitOldTile()) return false;
135 FollowTileExit();
136 if (!QueryNewTileTrackStatus()) return TryReverse();
137 m_new_td_bits &= DiagdirReachesTrackdirs(m_exitdir);
138 if (m_new_td_bits == TRACKDIR_BIT_NONE || !CanEnterNewTile()) {
139 /* In case we can't enter the next tile, but are
140 * a normal road vehicle, then we can actually
141 * try to reverse as this is the end of the road.
142 * Trams can only turn on the appropriate bits in
143 * which case reaching this would mean a dead end
144 * near a building and in that case there would
145 * a "false" QueryNewTileTrackStatus result and
146 * as such reversing is already tried. The fact
147 * that function failed can have to do with a
148 * missing road bit, or inability to connect the
149 * different bits due to slopes. */
150 if (IsRoadTT() && !IsTram() && TryReverse()) return true;
152 /* CanEnterNewTile already set a reason.
153 * Do NOT overwrite it (important for example for EC_RAIL_ROAD_TYPE).
154 * Only set a reason if CanEnterNewTile was not called */
155 if (m_new_td_bits == TRACKDIR_BIT_NONE) m_err = EC_NO_WAY;
157 return false;
159 if ((!IsRailTT() && !Allow90degTurns()) || (IsRailTT() && Rail90DegTurnDisallowed(GetTileRailType(m_old_tile), GetTileRailType(m_new_tile), !Allow90degTurns()))) {
160 m_new_td_bits &= (TrackdirBits)~(int)TrackdirCrossesTrackdirs(m_old_td);
161 if (m_new_td_bits == TRACKDIR_BIT_NONE) {
162 m_err = EC_90DEG;
163 return false;
166 return true;
169 inline bool MaskReservedTracks()
171 if (!DoTrackMasking()) return true;
173 if (m_is_station) {
174 /* Check skipped station tiles as well. */
175 TileIndexDiff diff = TileOffsByDiagDir(m_exitdir);
176 for (TileIndex tile = m_new_tile - diff * m_tiles_skipped; tile != m_new_tile; tile += diff) {
177 if (HasStationReservation(tile)) {
178 m_new_td_bits = TRACKDIR_BIT_NONE;
179 m_err = EC_RESERVED;
180 return false;
185 TrackBits reserved = GetReservedTrackbits(m_new_tile);
186 /* Mask already reserved trackdirs. */
187 m_new_td_bits &= ~TrackBitsToTrackdirBits(reserved);
188 /* Mask out all trackdirs that conflict with the reservation. */
189 for (Track t : SetTrackBitIterator(TrackdirBitsToTrackBits(m_new_td_bits))) {
190 if (TracksOverlap(reserved | TrackToTrackBits(t))) m_new_td_bits &= ~TrackToTrackdirBits(t);
192 if (m_new_td_bits == TRACKDIR_BIT_NONE) {
193 m_err = EC_RESERVED;
194 return false;
196 return true;
199 protected:
200 /** Follow the m_exitdir from m_old_tile and fill m_new_tile and m_tiles_skipped */
201 inline void FollowTileExit()
203 m_is_station = m_is_bridge = m_is_tunnel = false;
204 m_tiles_skipped = 0;
206 /* extra handling for tunnels and bridges in our direction */
207 if (IsTileType(m_old_tile, MP_TUNNELBRIDGE)) {
208 DiagDirection enterdir = GetTunnelBridgeDirection(m_old_tile);
209 if (enterdir == m_exitdir) {
210 /* we are entering the tunnel / bridge */
211 if (IsTunnel(m_old_tile)) {
212 m_is_tunnel = true;
213 m_new_tile = GetOtherTunnelEnd(m_old_tile);
214 } else { // IsBridge(m_old_tile)
215 m_is_bridge = true;
216 m_new_tile = GetOtherBridgeEnd(m_old_tile);
218 m_tiles_skipped = GetTunnelBridgeLength(m_new_tile, m_old_tile);
219 return;
221 assert(ReverseDiagDir(enterdir) == m_exitdir);
224 /* normal or station tile, do one step */
225 m_new_tile = TileAddByDiagDir(m_old_tile, m_exitdir);
227 /* special handling for stations */
228 if (IsRailTT() && HasStationTileRail(m_new_tile)) {
229 m_is_station = true;
230 } else if (IsRoadTT() && IsStationRoadStopTile(m_new_tile)) {
231 m_is_station = true;
235 /** stores track status (available trackdirs) for the new tile into m_new_td_bits */
236 inline bool QueryNewTileTrackStatus()
238 if (IsRailTT() && IsPlainRailTile(m_new_tile)) {
239 m_new_td_bits = (TrackdirBits)(GetTrackBits(m_new_tile) * 0x101);
240 } else if (IsRoadTT()) {
241 m_new_td_bits = GetTrackdirBitsForRoad(m_new_tile, this->IsTram() ? RTT_TRAM : RTT_ROAD);
242 } else {
243 m_new_td_bits = TrackStatusToTrackdirBits(GetTileTrackStatus(m_new_tile, TT(), 0));
245 return (m_new_td_bits != TRACKDIR_BIT_NONE);
248 /** return true if we can leave m_old_tile in m_exitdir */
249 inline bool CanExitOldTile()
251 /* road stop can be left at one direction only unless it's a drive-through stop */
252 if (IsRoadTT() && IsBayRoadStopTile(m_old_tile)) {
253 DiagDirection exitdir = GetRoadStopDir(m_old_tile);
254 if (exitdir != m_exitdir) {
255 m_err = EC_NO_WAY;
256 return false;
260 /* single tram bits can only be left in one direction */
261 if (IsTram()) {
262 DiagDirection single_tram = GetSingleTramBit(m_old_tile);
263 if (single_tram != INVALID_DIAGDIR && single_tram != m_exitdir) {
264 m_err = EC_NO_WAY;
265 return false;
269 /* road depots can be also left in one direction only */
270 if (IsRoadTT() && IsDepotTypeTile(m_old_tile, TT())) {
271 DiagDirection exitdir = GetRoadDepotDirection(m_old_tile);
272 if (exitdir != m_exitdir) {
273 m_err = EC_NO_WAY;
274 return false;
277 return true;
280 /** return true if we can enter m_new_tile from m_exitdir */
281 inline bool CanEnterNewTile()
283 if (IsRoadTT() && IsBayRoadStopTile(m_new_tile)) {
284 /* road stop can be entered from one direction only unless it's a drive-through stop */
285 DiagDirection exitdir = GetRoadStopDir(m_new_tile);
286 if (ReverseDiagDir(exitdir) != m_exitdir) {
287 m_err = EC_NO_WAY;
288 return false;
292 /* single tram bits can only be entered from one direction */
293 if (IsTram()) {
294 DiagDirection single_tram = GetSingleTramBit(m_new_tile);
295 if (single_tram != INVALID_DIAGDIR && single_tram != ReverseDiagDir(m_exitdir)) {
296 m_err = EC_NO_WAY;
297 return false;
301 /* road and rail depots can also be entered from one direction only */
302 if (IsRoadTT() && IsDepotTypeTile(m_new_tile, TT())) {
303 DiagDirection exitdir = GetRoadDepotDirection(m_new_tile);
304 if (ReverseDiagDir(exitdir) != m_exitdir) {
305 m_err = EC_NO_WAY;
306 return false;
308 /* don't try to enter other company's depots */
309 if (GetTileOwner(m_new_tile) != m_veh_owner) {
310 m_err = EC_OWNER;
311 return false;
314 if (IsRailTT() && IsDepotTypeTile(m_new_tile, TT())) {
315 DiagDirection exitdir = GetRailDepotDirection(m_new_tile);
316 if (ReverseDiagDir(exitdir) != m_exitdir) {
317 m_err = EC_NO_WAY;
318 return false;
322 /* rail transport is possible only on tiles with the same owner as vehicle */
323 if (IsRailTT() && GetTileOwner(m_new_tile) != m_veh_owner) {
324 /* different owner */
325 m_err = EC_NO_WAY;
326 return false;
329 /* rail transport is possible only on compatible rail types */
330 if (IsRailTT()) {
331 RailType rail_type = GetTileRailType(m_new_tile);
332 if (!HasBit(m_railtypes, rail_type)) {
333 /* incompatible rail type */
334 m_err = EC_RAIL_ROAD_TYPE;
335 return false;
339 /* road transport is possible only on compatible road types */
340 if (IsRoadTT()) {
341 const RoadVehicle *v = RoadVehicle::From(m_veh);
342 RoadType roadtype = GetRoadType(m_new_tile, GetRoadTramType(v->roadtype));
343 if (!HasBit(v->compatible_roadtypes, roadtype)) {
344 /* incompatible road type */
345 m_err = EC_RAIL_ROAD_TYPE;
346 return false;
350 /* tunnel holes and bridge ramps can be entered only from proper direction */
351 if (IsTileType(m_new_tile, MP_TUNNELBRIDGE)) {
352 if (IsTunnel(m_new_tile)) {
353 if (!m_is_tunnel) {
354 DiagDirection tunnel_enterdir = GetTunnelBridgeDirection(m_new_tile);
355 if (tunnel_enterdir != m_exitdir) {
356 m_err = EC_NO_WAY;
357 return false;
360 } else { // IsBridge(m_new_tile)
361 if (!m_is_bridge) {
362 DiagDirection ramp_enderdir = GetTunnelBridgeDirection(m_new_tile);
363 if (ramp_enderdir != m_exitdir) {
364 m_err = EC_NO_WAY;
365 return false;
371 /* special handling for rail stations - get to the end of platform */
372 if (IsRailTT() && m_is_station) {
373 /* entered railway station
374 * get platform length */
375 uint length = BaseStation::GetByTile(m_new_tile)->GetPlatformLength(m_new_tile, TrackdirToExitdir(m_old_td));
376 /* how big step we must do to get to the last platform tile? */
377 m_tiles_skipped = length - 1;
378 /* move to the platform end */
379 TileIndexDiff diff = TileOffsByDiagDir(m_exitdir);
380 diff *= m_tiles_skipped;
381 m_new_tile = TileAdd(m_new_tile, diff);
382 return true;
385 return true;
388 /** return true if we must reverse (in depots and single tram bits) */
389 inline bool ForcedReverse()
391 /* rail and road depots cause reversing */
392 if (!IsWaterTT() && IsDepotTypeTile(m_old_tile, TT())) {
393 DiagDirection exitdir = IsRailTT() ? GetRailDepotDirection(m_old_tile) : GetRoadDepotDirection(m_old_tile);
394 if (exitdir != m_exitdir) {
395 /* reverse */
396 m_new_tile = m_old_tile;
397 m_new_td_bits = TrackdirToTrackdirBits(ReverseTrackdir(m_old_td));
398 m_exitdir = exitdir;
399 m_tiles_skipped = 0;
400 m_is_tunnel = m_is_bridge = m_is_station = false;
401 return true;
405 /* Single tram bits and standard road stops cause reversing. */
406 if (IsRoadTT() && ((IsTram() && GetSingleTramBit(m_old_tile) == ReverseDiagDir(m_exitdir)) ||
407 (IsBayRoadStopTile(m_old_tile) && GetRoadStopDir(m_old_tile) == ReverseDiagDir(m_exitdir)))) {
408 /* reverse */
409 m_new_tile = m_old_tile;
410 m_new_td_bits = TrackdirToTrackdirBits(ReverseTrackdir(m_old_td));
411 m_exitdir = ReverseDiagDir(m_exitdir);
412 m_tiles_skipped = 0;
413 m_is_tunnel = m_is_bridge = m_is_station = false;
414 return true;
417 return false;
420 /** return true if we successfully reversed at end of road/track */
421 inline bool TryReverse()
423 if (IsRoadTT() && !IsTram()) {
424 /* if we reached the end of road, we can reverse the RV and continue moving */
425 m_exitdir = ReverseDiagDir(m_exitdir);
426 /* new tile will be the same as old one */
427 m_new_tile = m_old_tile;
428 /* set new trackdir bits to all reachable trackdirs */
429 QueryNewTileTrackStatus();
430 m_new_td_bits &= DiagdirReachesTrackdirs(m_exitdir);
431 if (m_new_td_bits != TRACKDIR_BIT_NONE) {
432 /* we have some trackdirs reachable after reversal */
433 return true;
436 m_err = EC_NO_WAY;
437 return false;
440 public:
441 /** Helper for pathfinders - get min/max speed on the m_old_tile/m_old_td */
442 int GetSpeedLimit(int *pmin_speed = nullptr) const
444 int min_speed = 0;
445 int max_speed = INT_MAX; // no limit
447 /* Check for on-bridge speed limit */
448 if (!IsWaterTT() && IsBridgeTile(m_old_tile)) {
449 int spd = GetBridgeSpec(GetBridgeType(m_old_tile))->speed;
450 if (IsRoadTT()) spd *= 2;
451 max_speed = std::min(max_speed, spd);
453 /* Check for speed limit imposed by railtype */
454 if (IsRailTT()) {
455 uint16_t rail_speed = GetRailTypeInfo(GetRailType(m_old_tile))->max_speed;
456 if (rail_speed > 0) max_speed = std::min<int>(max_speed, rail_speed);
458 if (IsRoadTT()) {
459 /* max_speed is already in roadvehicle units, no need to further modify (divide by 2) */
460 uint16_t road_speed = GetRoadTypeInfo(GetRoadType(m_old_tile, GetRoadTramType(RoadVehicle::From(m_veh)->roadtype)))->max_speed;
461 if (road_speed > 0) max_speed = std::min<int>(max_speed, road_speed);
464 /* if min speed was requested, return it */
465 if (pmin_speed != nullptr) *pmin_speed = min_speed;
466 return max_speed;
470 typedef CFollowTrackT<TRANSPORT_WATER, Ship, true > CFollowTrackWater;
471 typedef CFollowTrackT<TRANSPORT_ROAD, RoadVehicle, true > CFollowTrackRoad;
472 typedef CFollowTrackT<TRANSPORT_RAIL, Train, true > CFollowTrackRail;
474 typedef CFollowTrackT<TRANSPORT_RAIL, Train, false> CFollowTrackRailNo90;
476 typedef CFollowTrackT<TRANSPORT_RAIL, Train, true, true > CFollowTrackFreeRail;
477 typedef CFollowTrackT<TRANSPORT_RAIL, Train, false, true > CFollowTrackFreeRailNo90;
479 #endif /* FOLLOW_TRACK_HPP */