OpenTTD
follow_track.hpp
Go to the documentation of this file.
1 /* $Id: follow_track.hpp 27107 2015-01-02 12:11:20Z rubidium $ */
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 FOLLOW_TRACK_HPP
13 #define FOLLOW_TRACK_HPP
14 
15 #include "../pbs.h"
16 #include "../roadveh.h"
17 #include "../station_base.h"
18 #include "../train.h"
19 #include "../tunnelbridge.h"
20 #include "../tunnelbridge_map.h"
21 #include "../depot_map.h"
22 #include "pf_performance_timer.hpp"
23 
29 template <TransportType Ttr_type_, typename VehicleType, bool T90deg_turns_allowed_ = true, bool Tmask_reserved_tracks = false>
31 {
32  enum ErrorCode {
33  EC_NONE,
34  EC_OWNER,
35  EC_RAIL_TYPE,
36  EC_90DEG,
37  EC_NO_WAY,
38  EC_RESERVED,
39  };
40 
41  const VehicleType *m_veh;
48  bool m_is_tunnel;
49  bool m_is_bridge;
50  bool m_is_station;
52  ErrorCode m_err;
53  CPerformanceTimer *m_pPerf;
54  RailTypes m_railtypes;
55 
56  inline CFollowTrackT(const VehicleType *v = NULL, RailTypes railtype_override = INVALID_RAILTYPES, CPerformanceTimer *pPerf = NULL)
57  {
58  Init(v, railtype_override, pPerf);
59  }
60 
61  inline CFollowTrackT(Owner o, RailTypes railtype_override = INVALID_RAILTYPES, CPerformanceTimer *pPerf = NULL)
62  {
63  m_veh = NULL;
64  Init(o, railtype_override, pPerf);
65  }
66 
67  inline void Init(const VehicleType *v, RailTypes railtype_override, CPerformanceTimer *pPerf)
68  {
69  assert(!IsRailTT() || (v != NULL && v->type == VEH_TRAIN));
70  m_veh = v;
71  Init(v != NULL ? v->owner : INVALID_OWNER, IsRailTT() && railtype_override == INVALID_RAILTYPES ? Train::From(v)->compatible_railtypes : railtype_override, pPerf);
72  }
73 
74  inline void Init(Owner o, RailTypes railtype_override, CPerformanceTimer *pPerf)
75  {
76  assert((!IsRoadTT() || m_veh != NULL) && (!IsRailTT() || railtype_override != INVALID_RAILTYPES));
77  m_veh_owner = o;
78  m_pPerf = pPerf;
79  /* don't worry, all is inlined so compiler should remove unnecessary initializations */
86  m_tiles_skipped = 0;
87  m_err = EC_NONE;
88  m_railtypes = railtype_override;
89  }
90 
91  inline static TransportType TT() { return Ttr_type_; }
92  inline static bool IsWaterTT() { return TT() == TRANSPORT_WATER; }
93  inline static bool IsRailTT() { return TT() == TRANSPORT_RAIL; }
94  inline bool IsTram() { return IsRoadTT() && HasBit(RoadVehicle::From(m_veh)->compatible_roadtypes, ROADTYPE_TRAM); }
95  inline static bool IsRoadTT() { return TT() == TRANSPORT_ROAD; }
96  inline static bool Allow90degTurns() { return T90deg_turns_allowed_; }
97  inline static bool DoTrackMasking() { return Tmask_reserved_tracks; }
98 
101  {
102  assert(IsTram()); // this function shouldn't be called in other cases
103 
104  if (IsNormalRoadTile(tile)) {
105  RoadBits rb = GetRoadBits(tile, ROADTYPE_TRAM);
106  switch (rb) {
107  case ROAD_NW: return DIAGDIR_NW;
108  case ROAD_SW: return DIAGDIR_SW;
109  case ROAD_SE: return DIAGDIR_SE;
110  case ROAD_NE: return DIAGDIR_NE;
111  default: break;
112  }
113  }
114  return INVALID_DIAGDIR;
115  }
116 
121  inline bool Follow(TileIndex old_tile, Trackdir old_td)
122  {
123  m_old_tile = old_tile;
124  m_old_td = old_td;
125  m_err = EC_NONE;
126  assert(((TrackStatusToTrackdirBits(GetTileTrackStatus(m_old_tile, TT(), IsRoadTT() ? RoadVehicle::From(m_veh)->compatible_roadtypes : 0)) & TrackdirToTrackdirBits(m_old_td)) != 0) ||
127  (IsTram() && GetSingleTramBit(m_old_tile) != INVALID_DIAGDIR)); // Disable the assertion for single tram bits
129  if (ForcedReverse()) return true;
130  if (!CanExitOldTile()) return false;
131  FollowTileExit();
132  if (!QueryNewTileTrackStatus()) return TryReverse();
135  /* In case we can't enter the next tile, but are
136  * a normal road vehicle, then we can actually
137  * try to reverse as this is the end of the road.
138  * Trams can only turn on the appropriate bits in
139  * which case reaching this would mean a dead end
140  * near a building and in that case there would
141  * a "false" QueryNewTileTrackStatus result and
142  * as such reversing is already tried. The fact
143  * that function failed can have to do with a
144  * missing road bit, or inability to connect the
145  * different bits due to slopes. */
146  if (IsRoadTT() && !IsTram() && TryReverse()) return true;
147  m_err = EC_NO_WAY;
148  return false;
149  }
150  if (!Allow90degTurns()) {
153  m_err = EC_90DEG;
154  return false;
155  }
156  }
157  return true;
158  }
159 
160  inline bool MaskReservedTracks()
161  {
162  if (!DoTrackMasking()) return true;
163 
164  if (m_is_station) {
165  /* Check skipped station tiles as well. */
167  for (TileIndex tile = m_new_tile - diff * m_tiles_skipped; tile != m_new_tile; tile += diff) {
168  if (HasStationReservation(tile)) {
170  m_err = EC_RESERVED;
171  return false;
172  }
173  }
174  }
175 
177  /* Mask already reserved trackdirs. */
179  /* Mask out all trackdirs that conflict with the reservation. */
180  Track t;
183  }
185  m_err = EC_RESERVED;
186  return false;
187  }
188  return true;
189  }
190 
191 protected:
193  inline void FollowTileExit()
194  {
196  m_tiles_skipped = 0;
197 
198  /* extra handling for tunnels and bridges in our direction */
201  if (enterdir == m_exitdir) {
202  /* we are entering the tunnel / bridge */
203  if (IsTunnel(m_old_tile)) {
204  m_is_tunnel = true;
206  } else { // IsBridge(m_old_tile)
207  m_is_bridge = true;
209  }
211  return;
212  }
213  assert(ReverseDiagDir(enterdir) == m_exitdir);
214  }
215 
216  /* normal or station tile, do one step */
218  m_new_tile = TILE_ADD(m_old_tile, diff);
219 
220  /* special handling for stations */
221  if (IsRailTT() && HasStationTileRail(m_new_tile)) {
222  m_is_station = true;
223  } else if (IsRoadTT() && IsRoadStopTile(m_new_tile)) {
224  m_is_station = true;
225  } else {
226  m_is_station = false;
227  }
228  }
229 
232  {
233  CPerfStart perf(*m_pPerf);
234  if (IsRailTT() && IsPlainRailTile(m_new_tile)) {
236  } else {
237  m_new_td_bits = TrackStatusToTrackdirBits(GetTileTrackStatus(m_new_tile, TT(), IsRoadTT() ? RoadVehicle::From(m_veh)->compatible_roadtypes : 0));
238 
239  if (IsTram() && m_new_td_bits == 0) {
240  /* GetTileTrackStatus() returns 0 for single tram bits.
241  * As we cannot change it there (easily) without breaking something, change it here */
242  switch (GetSingleTramBit(m_new_tile)) {
243  case DIAGDIR_NE:
244  case DIAGDIR_SW:
246  break;
247 
248  case DIAGDIR_NW:
249  case DIAGDIR_SE:
251  break;
252 
253  default: break;
254  }
255  }
256  }
257  return (m_new_td_bits != TRACKDIR_BIT_NONE);
258  }
259 
261  inline bool CanExitOldTile()
262  {
263  /* road stop can be left at one direction only unless it's a drive-through stop */
264  if (IsRoadTT() && IsStandardRoadStopTile(m_old_tile)) {
266  if (exitdir != m_exitdir) {
267  m_err = EC_NO_WAY;
268  return false;
269  }
270  }
271 
272  /* single tram bits can only be left in one direction */
273  if (IsTram()) {
275  if (single_tram != INVALID_DIAGDIR && single_tram != m_exitdir) {
276  m_err = EC_NO_WAY;
277  return false;
278  }
279  }
280 
281  /* road depots can be also left in one direction only */
282  if (IsRoadTT() && IsDepotTypeTile(m_old_tile, TT())) {
284  if (exitdir != m_exitdir) {
285  m_err = EC_NO_WAY;
286  return false;
287  }
288  }
289  return true;
290  }
291 
293  inline bool CanEnterNewTile()
294  {
295  if (IsRoadTT() && IsStandardRoadStopTile(m_new_tile)) {
296  /* road stop can be entered from one direction only unless it's a drive-through stop */
298  if (ReverseDiagDir(exitdir) != m_exitdir) {
299  m_err = EC_NO_WAY;
300  return false;
301  }
302  }
303 
304  /* single tram bits can only be entered from one direction */
305  if (IsTram()) {
307  if (single_tram != INVALID_DIAGDIR && single_tram != ReverseDiagDir(m_exitdir)) {
308  m_err = EC_NO_WAY;
309  return false;
310  }
311  }
312 
313  /* road and rail depots can also be entered from one direction only */
314  if (IsRoadTT() && IsDepotTypeTile(m_new_tile, TT())) {
316  if (ReverseDiagDir(exitdir) != m_exitdir) {
317  m_err = EC_NO_WAY;
318  return false;
319  }
320  /* don't try to enter other company's depots */
322  m_err = EC_OWNER;
323  return false;
324  }
325  }
326  if (IsRailTT() && IsDepotTypeTile(m_new_tile, TT())) {
328  if (ReverseDiagDir(exitdir) != m_exitdir) {
329  m_err = EC_NO_WAY;
330  return false;
331  }
332  }
333 
334  /* rail transport is possible only on tiles with the same owner as vehicle */
335  if (IsRailTT() && GetTileOwner(m_new_tile) != m_veh_owner) {
336  /* different owner */
337  m_err = EC_NO_WAY;
338  return false;
339  }
340 
341  /* rail transport is possible only on compatible rail types */
342  if (IsRailTT()) {
343  RailType rail_type = GetTileRailType(m_new_tile);
344  if (!HasBit(m_railtypes, rail_type)) {
345  /* incompatible rail type */
346  m_err = EC_RAIL_TYPE;
347  return false;
348  }
349  }
350 
351  /* tunnel holes and bridge ramps can be entered only from proper direction */
353  if (IsTunnel(m_new_tile)) {
354  if (!m_is_tunnel) {
356  if (tunnel_enterdir != m_exitdir) {
357  m_err = EC_NO_WAY;
358  return false;
359  }
360  }
361  } else { // IsBridge(m_new_tile)
362  if (!m_is_bridge) {
364  if (ramp_enderdir != m_exitdir) {
365  m_err = EC_NO_WAY;
366  return false;
367  }
368  }
369  }
370  }
371 
372  /* special handling for rail stations - get to the end of platform */
373  if (IsRailTT() && m_is_station) {
374  /* entered railway station
375  * get platform length */
377  /* how big step we must do to get to the last platform tile; */
378  m_tiles_skipped = length - 1;
379  /* move to the platform end */
381  diff *= m_tiles_skipped;
382  m_new_tile = TILE_ADD(m_new_tile, diff);
383  return true;
384  }
385 
386  return true;
387  }
388 
390  inline bool ForcedReverse()
391  {
392  /* rail and road depots cause reversing */
393  if (!IsWaterTT() && IsDepotTypeTile(m_old_tile, TT())) {
395  if (exitdir != m_exitdir) {
396  /* reverse */
399  m_exitdir = exitdir;
400  m_tiles_skipped = 0;
402  return true;
403  }
404  }
405 
406  /* single tram bits cause reversing */
407  if (IsTram() && GetSingleTramBit(m_old_tile) == ReverseDiagDir(m_exitdir)) {
408  /* reverse */
412  m_tiles_skipped = 0;
414  return true;
415  }
416 
417  return false;
418  }
419 
421  inline bool TryReverse()
422  {
423  if (IsRoadTT() && !IsTram()) {
424  /* if we reached the end of road, we can reverse the RV and continue moving */
426  /* new tile will be the same as old one */
428  /* set new trackdir bits to all reachable trackdirs */
432  /* we have some trackdirs reachable after reversal */
433  return true;
434  }
435  }
436  m_err = EC_NO_WAY;
437  return false;
438  }
439 
440 public:
442  int GetSpeedLimit(int *pmin_speed = NULL) const
443  {
444  int min_speed = 0;
445  int max_speed = INT_MAX; // no limit
446 
447  /* Check for on-bridge speed limit */
448  if (!IsWaterTT() && IsBridgeTile(m_old_tile)) {
450  if (IsRoadTT()) spd *= 2;
451  if (max_speed > spd) max_speed = spd;
452  }
453  /* Check for speed limit imposed by railtype */
454  if (IsRailTT()) {
455  uint16 rail_speed = GetRailTypeInfo(GetRailType(m_old_tile))->max_speed;
456  if (rail_speed > 0) max_speed = min(max_speed, rail_speed);
457  }
458 
459  /* if min speed was requested, return it */
460  if (pmin_speed != NULL) *pmin_speed = min_speed;
461  return max_speed;
462  }
463 };
464 
468 
472 
475 
476 #endif /* FOLLOW_TRACK_HPP */