OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

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>
30 struct CFollowTrackT
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;
42 Owner m_veh_owner;
43 TileIndex m_old_tile;
44 Trackdir m_old_td;
45 TileIndex m_new_tile;
46 TrackdirBits m_new_td_bits;
47 DiagDirection m_exitdir;
48 bool m_is_tunnel;
49 bool m_is_bridge;
50 bool m_is_station;
51 int m_tiles_skipped;
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 */
80 m_old_tile = INVALID_TILE;
81 m_old_td = INVALID_TRACKDIR;
82 m_new_tile = INVALID_TILE;
83 m_new_td_bits = TRACKDIR_BIT_NONE;
84 m_exitdir = INVALID_DIAGDIR;
85 m_is_station = m_is_bridge = m_is_tunnel = false;
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
100 inline DiagDirection GetSingleTramBit(TileIndex tile)
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
128 m_exitdir = TrackdirToExitdir(m_old_td);
129 if (ForcedReverse()) return true;
130 if (!CanExitOldTile()) return false;
131 FollowTileExit();
132 if (!QueryNewTileTrackStatus()) return TryReverse();
133 m_new_td_bits &= DiagdirReachesTrackdirs(m_exitdir);
134 if (m_new_td_bits == TRACKDIR_BIT_NONE || !CanEnterNewTile()) {
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()) {
151 m_new_td_bits &= (TrackdirBits)~(int)TrackdirCrossesTrackdirs(m_old_td);
152 if (m_new_td_bits == TRACKDIR_BIT_NONE) {
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. */
166 TileIndexDiff diff = TileOffsByDiagDir(m_exitdir);
167 for (TileIndex tile = m_new_tile - diff * m_tiles_skipped; tile != m_new_tile; tile += diff) {
168 if (HasStationReservation(tile)) {
169 m_new_td_bits = TRACKDIR_BIT_NONE;
170 m_err = EC_RESERVED;
171 return false;
172 }
173 }
174 }
175
176 TrackBits reserved = GetReservedTrackbits(m_new_tile);
177 /* Mask already reserved trackdirs. */
178 m_new_td_bits &= ~TrackBitsToTrackdirBits(reserved);
179 /* Mask out all trackdirs that conflict with the reservation. */
180 Track t;
181 FOR_EACH_SET_TRACK(t, TrackdirBitsToTrackBits(m_new_td_bits)) {
182 if (TracksOverlap(reserved | TrackToTrackBits(t))) m_new_td_bits &= ~TrackToTrackdirBits(t);
183 }
184 if (m_new_td_bits == TRACKDIR_BIT_NONE) {
185 m_err = EC_RESERVED;
186 return false;
187 }
188 return true;
189 }
190
191 protected:
193 inline void FollowTileExit()
194 {
195 m_is_station = m_is_bridge = m_is_tunnel = false;
196 m_tiles_skipped = 0;
197
198 /* extra handling for tunnels and bridges in our direction */
199 if (IsTileType(m_old_tile, MP_TUNNELBRIDGE)) {
200 DiagDirection enterdir = GetTunnelBridgeDirection(m_old_tile);
201 if (enterdir == m_exitdir) {
202 /* we are entering the tunnel / bridge */
203 if (IsTunnel(m_old_tile)) {
204 m_is_tunnel = true;
205 m_new_tile = GetOtherTunnelEnd(m_old_tile);
206 } else { // IsBridge(m_old_tile)
207 m_is_bridge = true;
208 m_new_tile = GetOtherBridgeEnd(m_old_tile);
209 }
210 m_tiles_skipped = GetTunnelBridgeLength(m_new_tile, m_old_tile);
211 return;
212 }
213 assert(ReverseDiagDir(enterdir) == m_exitdir);
214 }
215
216 /* normal or station tile, do one step */
217 TileIndexDiff diff = TileOffsByDiagDir(m_exitdir);
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
231 inline bool QueryNewTileTrackStatus()
232 {
233 CPerfStart perf(*m_pPerf);
234 if (IsRailTT() && IsPlainRailTile(m_new_tile)) {
235 m_new_td_bits = (TrackdirBits)(GetTrackBits(m_new_tile) * 0x101);
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:
245 m_new_td_bits = TRACKDIR_BIT_X_NE | TRACKDIR_BIT_X_SW;
246 break;
247
248 case DIAGDIR_NW:
249 case DIAGDIR_SE:
250 m_new_td_bits = TRACKDIR_BIT_Y_NW | TRACKDIR_BIT_Y_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)) {
265 DiagDirection exitdir = GetRoadStopDir(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()) {
274 DiagDirection single_tram = GetSingleTramBit(m_old_tile);
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())) {
283 DiagDirection exitdir = GetRoadDepotDirection(m_old_tile);
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 */
297 DiagDirection exitdir = GetRoadStopDir(m_new_tile);
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()) {
306 DiagDirection single_tram = GetSingleTramBit(m_new_tile);
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())) {
315 DiagDirection exitdir = GetRoadDepotDirection(m_new_tile);
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 */
321 if (GetTileOwner(m_new_tile) != m_veh_owner) {
322 m_err = EC_OWNER;
323 return false;
324 }
325 }
326 if (IsRailTT() && IsDepotTypeTile(m_new_tile, TT())) {
327 DiagDirection exitdir = GetRailDepotDirection(m_new_tile);
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 */
352 if (IsTileType(m_new_tile, MP_TUNNELBRIDGE)) {
353 if (IsTunnel(m_new_tile)) {
354 if (!m_is_tunnel) {
355 DiagDirection tunnel_enterdir = GetTunnelBridgeDirection(m_new_tile);
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) {
363 DiagDirection ramp_enderdir = GetTunnelBridgeDirection(m_new_tile);
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 */
376 uint length = BaseStation::GetByTile(m_new_tile)->GetPlatformLength(m_new_tile, TrackdirToExitdir(m_old_td));
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 */
380 TileIndexDiff diff = TileOffsByDiagDir(m_exitdir);
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())) {
394 DiagDirection exitdir = IsRailTT() ? GetRailDepotDirection(m_old_tile) : GetRoadDepotDirection(m_old_tile);
395 if (exitdir != m_exitdir) {
396 /* reverse */
397 m_new_tile = m_old_tile;
398 m_new_td_bits = TrackdirToTrackdirBits(ReverseTrackdir(m_old_td));
399 m_exitdir = exitdir;
400 m_tiles_skipped = 0;
401 m_is_tunnel = m_is_bridge = m_is_station = false;
402 return true;
403 }
404 }
405
406 /* single tram bits cause reversing */
407 if (IsTram() && GetSingleTramBit(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;
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 */
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;
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)) {
449 int spd = GetBridgeSpec(GetBridgeType(m_old_tile))->speed;
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
465 typedef CFollowTrackT<TRANSPORT_WATER, Ship, true > CFollowTrackWater;
466 typedef CFollowTrackT<TRANSPORT_ROAD, RoadVehicle, true > CFollowTrackRoad;
467 typedef CFollowTrackT<TRANSPORT_RAIL, Train, true > CFollowTrackRail;
468
469 typedef CFollowTrackT<TRANSPORT_WATER, Ship, false> CFollowTrackWaterNo90;
470 typedef CFollowTrackT<TRANSPORT_ROAD, RoadVehicle, false> CFollowTrackRoadNo90;
471 typedef CFollowTrackT<TRANSPORT_RAIL, Train, false> CFollowTrackRailNo90;
472
473 typedef CFollowTrackT<TRANSPORT_RAIL, Train, true, true > CFollowTrackFreeRail;
474 typedef CFollowTrackT<TRANSPORT_RAIL, Train, false, true > CFollowTrackFreeRailNo90;
475
476 #endif /* FOLLOW_TRACK_HPP */