OpenTTD
follow_track.hpp
Go to the documentation of this file.
1 /* $Id: follow_track.hpp 26109 2013-11-25 14:34:09Z 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();
133  if (!CanEnterNewTile()) return false;
136  /* In case we can't enter the next tile, but are
137  * a normal road vehicle, then we can actually
138  * try to reverse as this is the end of the road.
139  * Trams can only turn on the appropriate bits in
140  * which case reaching this would mean a dead end
141  * near a building and in that case there would
142  * a "false" QueryNewTileTrackStatus result and
143  * as such reversing is already tried. The fact
144  * that function failed can have to do with a
145  * missing road bit, or inability to connect the
146  * different bits due to slopes. */
147  if (IsRoadTT() && !IsTram() && TryReverse()) return true;
148  m_err = EC_NO_WAY;
149  return false;
150  }
151  if (!Allow90degTurns()) {
154  m_err = EC_90DEG;
155  return false;
156  }
157  }
158  return true;
159  }
160 
161  inline bool MaskReservedTracks()
162  {
163  if (!DoTrackMasking()) return true;
164 
165  if (m_is_station) {
166  /* Check skipped station tiles as well. */
168  for (TileIndex tile = m_new_tile - diff * m_tiles_skipped; tile != m_new_tile; tile += diff) {
169  if (HasStationReservation(tile)) {
171  m_err = EC_RESERVED;
172  return false;
173  }
174  }
175  }
176 
178  /* Mask already reserved trackdirs. */
180  /* Mask out all trackdirs that conflict with the reservation. */
181  Track t;
184  }
186  m_err = EC_RESERVED;
187  return false;
188  }
189  return true;
190  }
191 
192 protected:
194  inline void FollowTileExit()
195  {
197  m_tiles_skipped = 0;
198 
199  /* extra handling for tunnels and bridges in our direction */
202  if (enterdir == m_exitdir) {
203  /* we are entering the tunnel / bridge */
204  if (IsTunnel(m_old_tile)) {
205  m_is_tunnel = true;
207  } else { // IsBridge(m_old_tile)
208  m_is_bridge = true;
210  }
212  return;
213  }
214  assert(ReverseDiagDir(enterdir) == m_exitdir);
215  }
216 
217  /* normal or station tile, do one step */
219  m_new_tile = TILE_ADD(m_old_tile, diff);
220 
221  /* special handling for stations */
222  if (IsRailTT() && HasStationTileRail(m_new_tile)) {
223  m_is_station = true;
224  } else if (IsRoadTT() && IsRoadStopTile(m_new_tile)) {
225  m_is_station = true;
226  } else {
227  m_is_station = false;
228  }
229  }
230 
233  {
234  CPerfStart perf(*m_pPerf);
235  if (IsRailTT() && IsPlainRailTile(m_new_tile)) {
237  } else {
238  m_new_td_bits = TrackStatusToTrackdirBits(GetTileTrackStatus(m_new_tile, TT(), IsRoadTT() ? RoadVehicle::From(m_veh)->compatible_roadtypes : 0));
239 
240  if (IsTram() && m_new_td_bits == 0) {
241  /* GetTileTrackStatus() returns 0 for single tram bits.
242  * As we cannot change it there (easily) without breaking something, change it here */
243  switch (GetSingleTramBit(m_new_tile)) {
244  case DIAGDIR_NE:
245  case DIAGDIR_SW:
247  break;
248 
249  case DIAGDIR_NW:
250  case DIAGDIR_SE:
252  break;
253 
254  default: break;
255  }
256  }
257  }
258  return (m_new_td_bits != TRACKDIR_BIT_NONE);
259  }
260 
262  inline bool CanExitOldTile()
263  {
264  /* road stop can be left at one direction only unless it's a drive-through stop */
265  if (IsRoadTT() && IsStandardRoadStopTile(m_old_tile)) {
267  if (exitdir != m_exitdir) {
268  m_err = EC_NO_WAY;
269  return false;
270  }
271  }
272 
273  /* single tram bits can only be left in one direction */
274  if (IsTram()) {
276  if (single_tram != INVALID_DIAGDIR && single_tram != m_exitdir) {
277  m_err = EC_NO_WAY;
278  return false;
279  }
280  }
281 
282  /* road depots can be also left in one direction only */
283  if (IsRoadTT() && IsDepotTypeTile(m_old_tile, TT())) {
285  if (exitdir != m_exitdir) {
286  m_err = EC_NO_WAY;
287  return false;
288  }
289  }
290  return true;
291  }
292 
294  inline bool CanEnterNewTile()
295  {
296  if (IsRoadTT() && IsStandardRoadStopTile(m_new_tile)) {
297  /* road stop can be entered from one direction only unless it's a drive-through stop */
299  if (ReverseDiagDir(exitdir) != m_exitdir) {
300  m_err = EC_NO_WAY;
301  return false;
302  }
303  }
304 
305  /* single tram bits can only be entered from one direction */
306  if (IsTram()) {
308  if (single_tram != INVALID_DIAGDIR && single_tram != ReverseDiagDir(m_exitdir)) {
309  m_err = EC_NO_WAY;
310  return false;
311  }
312  }
313 
314  /* road and rail depots can also be entered from one direction only */
315  if (IsRoadTT() && IsDepotTypeTile(m_new_tile, TT())) {
317  if (ReverseDiagDir(exitdir) != m_exitdir) {
318  m_err = EC_NO_WAY;
319  return false;
320  }
321  /* don't try to enter other company's depots */
323  m_err = EC_OWNER;
324  return false;
325  }
326  }
327  if (IsRailTT() && IsDepotTypeTile(m_new_tile, TT())) {
329  if (ReverseDiagDir(exitdir) != m_exitdir) {
330  m_err = EC_NO_WAY;
331  return false;
332  }
333  }
334 
335  /* rail transport is possible only on tiles with the same owner as vehicle */
336  if (IsRailTT() && GetTileOwner(m_new_tile) != m_veh_owner) {
337  /* different owner */
338  m_err = EC_NO_WAY;
339  return false;
340  }
341 
342  /* rail transport is possible only on compatible rail types */
343  if (IsRailTT()) {
344  RailType rail_type = GetTileRailType(m_new_tile);
345  if (!HasBit(m_railtypes, rail_type)) {
346  /* incompatible rail type */
347  m_err = EC_RAIL_TYPE;
348  return false;
349  }
350  }
351 
352  /* tunnel holes and bridge ramps can be entered only from proper direction */
354  if (IsTunnel(m_new_tile)) {
355  if (!m_is_tunnel) {
357  if (tunnel_enterdir != m_exitdir) {
358  m_err = EC_NO_WAY;
359  return false;
360  }
361  }
362  } else { // IsBridge(m_new_tile)
363  if (!m_is_bridge) {
365  if (ramp_enderdir != m_exitdir) {
366  m_err = EC_NO_WAY;
367  return false;
368  }
369  }
370  }
371  }
372 
373  /* special handling for rail stations - get to the end of platform */
374  if (IsRailTT() && m_is_station) {
375  /* entered railway station
376  * get platform length */
378  /* how big step we must do to get to the last platform tile; */
379  m_tiles_skipped = length - 1;
380  /* move to the platform end */
382  diff *= m_tiles_skipped;
383  m_new_tile = TILE_ADD(m_new_tile, diff);
384  return true;
385  }
386 
387  return true;
388  }
389 
391  inline bool ForcedReverse()
392  {
393  /* rail and road depots cause reversing */
394  if (!IsWaterTT() && IsDepotTypeTile(m_old_tile, TT())) {
396  if (exitdir != m_exitdir) {
397  /* reverse */
400  m_exitdir = exitdir;
401  m_tiles_skipped = 0;
403  return true;
404  }
405  }
406 
407  /* single tram bits cause reversing */
408  if (IsTram() && GetSingleTramBit(m_old_tile) == ReverseDiagDir(m_exitdir)) {
409  /* reverse */
413  m_tiles_skipped = 0;
415  return true;
416  }
417 
418  return false;
419  }
420 
422  inline bool TryReverse()
423  {
424  if (IsRoadTT() && !IsTram()) {
425  /* if we reached the end of road, we can reverse the RV and continue moving */
427  /* new tile will be the same as old one */
429  /* set new trackdir bits to all reachable trackdirs */
433  /* we have some trackdirs reachable after reversal */
434  return true;
435  }
436  }
437  m_err = EC_NO_WAY;
438  return false;
439  }
440 
441 public:
443  int GetSpeedLimit(int *pmin_speed = NULL) const
444  {
445  int min_speed = 0;
446  int max_speed = INT_MAX; // no limit
447 
448  /* Check for on-bridge speed limit */
449  if (!IsWaterTT() && IsBridgeTile(m_old_tile)) {
451  if (IsRoadTT()) spd *= 2;
452  if (max_speed > spd) max_speed = spd;
453  }
454  /* Check for speed limit imposed by railtype */
455  if (IsRailTT()) {
456  uint16 rail_speed = GetRailTypeInfo(GetRailType(m_old_tile))->max_speed;
457  if (rail_speed > 0) max_speed = min(max_speed, rail_speed);
458  }
459 
460  /* if min speed was requested, return it */
461  if (pmin_speed != NULL) *pmin_speed = min_speed;
462  return max_speed;
463  }
464 };
465 
469 
473 
476 
477 #endif /* FOLLOW_TRACK_HPP */