yapf/follow_track.hpp
author tron
Mon, 05 Jun 2006 11:28:31 +0000
changeset 3962 b53bf74fbcef
parent 3933 231ae3c419f4
child 3976 6135bc445350
permissions -rw-r--r--
(svn r5121) Add forgotten file in r5120
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     1
/* $Id$ */
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     2
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     3
#ifndef  FOLLOW_TRACK_HPP
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     4
#define  FOLLOW_TRACK_HPP
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     5
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     6
#include "yapf.hpp"
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     7
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     8
/** Track follower helper template class (can serve pathfinders and vehicle
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     9
    controllers). See 6 different typedefs below for 3 different transport
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    10
    types w/ of w/o 90-deg turns allowed */
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    11
template <TransportType Ttr_type_, bool T90deg_turns_allowed_ = true>
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    12
struct CFollowTrackT : public FollowTrack_t
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    13
{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    14
	CPerformanceTimer* m_pPerf;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    15
3915
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3912
diff changeset
    16
	FORCEINLINE CFollowTrackT(const Vehicle* v = NULL, CPerformanceTimer* pPerf = NULL)
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    17
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    18
		Init(v, pPerf);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    19
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    20
3915
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3912
diff changeset
    21
	FORCEINLINE void Init(const Vehicle* v, CPerformanceTimer* pPerf)
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    22
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    23
		assert(!IsRailTT() || (v != NULL && v->type == VEH_Train));
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    24
		m_veh = v;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    25
		m_pPerf = pPerf;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    26
		// don't worry, all is inlined so compiler should remove unnecessary initializations
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    27
		m_new_tile = INVALID_TILE;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    28
		m_new_td_bits = TRACKDIR_BIT_NONE;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    29
		m_exitdir = INVALID_DIAGDIR;
3933
231ae3c419f4 (svn r5070) Merged the bridge branch
celestar
parents: 3931
diff changeset
    30
		m_is_station = m_is_bridge = m_is_tunnel = false;
3931
b946fc6e7188 (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3915
diff changeset
    31
		m_tiles_skipped = 0;
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    32
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    33
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    34
	FORCEINLINE static TransportType TT() {return Ttr_type_;}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    35
	FORCEINLINE static bool IsWaterTT() {return TT() == TRANSPORT_WATER;}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    36
	FORCEINLINE static bool IsRailTT() {return TT() == TRANSPORT_RAIL;}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    37
	FORCEINLINE static bool IsRoadTT() {return TT() == TRANSPORT_ROAD;}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    38
	FORCEINLINE static bool Allow90degTurns() {return T90deg_turns_allowed_;}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    39
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    40
	/** main follower routine. Fills all members and return true on success.
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    41
	    Otherwise returns false if track can't be followed. */
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    42
	FORCEINLINE bool Follow(TileIndex old_tile, Trackdir old_td)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    43
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    44
		m_old_tile = old_tile;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    45
		m_old_td = old_td;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    46
		assert((GetTileTrackStatus(m_old_tile, TT()) & TrackdirToTrackdirBits(m_old_td)) != 0);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    47
		m_exitdir = TrackdirToExitdir(m_old_td);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    48
		if (EnteredRailDepot()) return true;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    49
		if (!CanExitOldTile()) return false;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    50
		FollowTileExit();
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    51
		if (!QueryNewTileTrackStatus()) return false;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    52
		if (!CanEnterNewTile()) return false;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    53
		m_new_td_bits &= DiagdirReachesTrackdirs(m_exitdir);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    54
		if (!Allow90degTurns())
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    55
			m_new_td_bits &= (TrackdirBits)~(int)TrackdirCrossesTrackdirs(m_old_td);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    56
		return (m_new_td_bits != TRACKDIR_BIT_NONE);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    57
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    58
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    59
protected:
3931
b946fc6e7188 (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3915
diff changeset
    60
	/** Follow the m_exitdir from m_old_tile and fill m_new_tile and m_tiles_skipped */
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    61
	FORCEINLINE void FollowTileExit()
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    62
	{
3933
231ae3c419f4 (svn r5070) Merged the bridge branch
celestar
parents: 3931
diff changeset
    63
		m_is_station = m_is_bridge = m_is_tunnel = false;
231ae3c419f4 (svn r5070) Merged the bridge branch
celestar
parents: 3931
diff changeset
    64
		m_tiles_skipped = 0;
231ae3c419f4 (svn r5070) Merged the bridge branch
celestar
parents: 3931
diff changeset
    65
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    66
		// extra handling for tunnels in our direction
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    67
		if (IsTunnelTile(m_old_tile)) {
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    68
			DiagDirection tunnel_enterdir = GetTunnelDirection(m_old_tile);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    69
			if (tunnel_enterdir == m_exitdir) {
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    70
				// we are entering the tunnel
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    71
				FindLengthOfTunnelResult flotr = FindLengthOfTunnel(m_old_tile, m_exitdir);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    72
				m_new_tile = flotr.tile;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    73
				m_is_tunnel = true;
3931
b946fc6e7188 (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3915
diff changeset
    74
				m_tiles_skipped = flotr.length - 1;
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    75
				return;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    76
			}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    77
			assert(ReverseDiagDir(tunnel_enterdir) == m_exitdir);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    78
		}
3931
b946fc6e7188 (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3915
diff changeset
    79
3933
231ae3c419f4 (svn r5070) Merged the bridge branch
celestar
parents: 3931
diff changeset
    80
		// extra handling for bridge ramp in our direction
231ae3c419f4 (svn r5070) Merged the bridge branch
celestar
parents: 3931
diff changeset
    81
		if (IsBridgeTile(m_old_tile)) {
231ae3c419f4 (svn r5070) Merged the bridge branch
celestar
parents: 3931
diff changeset
    82
			DiagDirection bridge_enterdir = GetBridgeRampDirection(m_old_tile);
231ae3c419f4 (svn r5070) Merged the bridge branch
celestar
parents: 3931
diff changeset
    83
			if (bridge_enterdir == m_exitdir) {
231ae3c419f4 (svn r5070) Merged the bridge branch
celestar
parents: 3931
diff changeset
    84
				// we are entering the bridge ramp
231ae3c419f4 (svn r5070) Merged the bridge branch
celestar
parents: 3931
diff changeset
    85
				m_new_tile = GetOtherBridgeEnd(m_old_tile);
231ae3c419f4 (svn r5070) Merged the bridge branch
celestar
parents: 3931
diff changeset
    86
				uint32 bridge_length = GetBridgeLength(m_old_tile, m_new_tile);
231ae3c419f4 (svn r5070) Merged the bridge branch
celestar
parents: 3931
diff changeset
    87
				m_tiles_skipped = bridge_length;
231ae3c419f4 (svn r5070) Merged the bridge branch
celestar
parents: 3931
diff changeset
    88
				m_is_bridge = true;
231ae3c419f4 (svn r5070) Merged the bridge branch
celestar
parents: 3931
diff changeset
    89
				return;
231ae3c419f4 (svn r5070) Merged the bridge branch
celestar
parents: 3931
diff changeset
    90
			}
231ae3c419f4 (svn r5070) Merged the bridge branch
celestar
parents: 3931
diff changeset
    91
			assert(ReverseDiagDir(bridge_enterdir) == m_exitdir);
231ae3c419f4 (svn r5070) Merged the bridge branch
celestar
parents: 3931
diff changeset
    92
		}
231ae3c419f4 (svn r5070) Merged the bridge branch
celestar
parents: 3931
diff changeset
    93
231ae3c419f4 (svn r5070) Merged the bridge branch
celestar
parents: 3931
diff changeset
    94
		// normal or station tile, do one step
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    95
		TileIndexDiff diff = TileOffsByDir(m_exitdir);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    96
		m_new_tile = TILE_ADD(m_old_tile, diff);
3931
b946fc6e7188 (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3915
diff changeset
    97
b946fc6e7188 (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3915
diff changeset
    98
		// special handling for stations
b946fc6e7188 (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3915
diff changeset
    99
		if (IsRailTT() && IsRailwayStationTile(m_new_tile)) {
b946fc6e7188 (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3915
diff changeset
   100
			m_is_station = true;
b946fc6e7188 (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3915
diff changeset
   101
		} else if (IsRoadTT() && IsRoadStopTile(m_new_tile)) {
b946fc6e7188 (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3915
diff changeset
   102
			m_is_station = true;
b946fc6e7188 (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3915
diff changeset
   103
		} else {
b946fc6e7188 (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3915
diff changeset
   104
			m_is_station = false;
b946fc6e7188 (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3915
diff changeset
   105
		}
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   106
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   107
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   108
	/** stores track status (available trackdirs) for the new tile into m_new_td_bits */
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   109
	FORCEINLINE bool QueryNewTileTrackStatus()
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   110
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   111
		CPerfStart perf(*m_pPerf);
3912
08c70885ec45 (svn r5013) -Fix: [YAPF] RVs trying to plan route through railway.
KUDr
parents: 3900
diff changeset
   112
		if (IsRailTT() && GetTileType(m_new_tile) == MP_RAILWAY && IsPlainRailTile(m_new_tile)) {
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   113
			m_new_td_bits = (TrackdirBits)(GetTrackBits(m_new_tile) * 0x101);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   114
		} else {
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   115
			uint32 ts = GetTileTrackStatus(m_new_tile, TT());
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   116
			m_new_td_bits = (TrackdirBits)(ts & TRACKDIR_BIT_MASK);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   117
		}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   118
		return (m_new_td_bits != TRACKDIR_BIT_NONE);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   119
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   120
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   121
	/** return true if we can leave m_old_tile in m_exitdir */
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   122
	FORCEINLINE bool CanExitOldTile()
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   123
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   124
		// road stop can be left at one direction only
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   125
		if (IsRoadTT() && IsRoadStopTile(m_old_tile)) {
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   126
			DiagDirection exitdir = GetRoadStopDir(m_old_tile);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   127
			if (exitdir != m_exitdir)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   128
				return false;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   129
		}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   130
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   131
		// road depots can be also left in one direction only
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   132
		if (IsRoadTT() && IsTileDepotType(m_old_tile, TT())) {
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   133
			DiagDirection exitdir = GetRoadDepotDirection(m_old_tile);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   134
			if (exitdir != m_exitdir)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   135
				return false;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   136
		}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   137
		return true;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   138
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   139
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   140
	/** return true if we can enter m_new_tile from m_exitdir */
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   141
	FORCEINLINE bool CanEnterNewTile()
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   142
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   143
		if (IsRoadTT() && IsRoadStopTile(m_new_tile)) {
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   144
			// road stop can be entered from one direction only
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   145
			DiagDirection exitdir = GetRoadStopDir(m_new_tile);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   146
			if (ReverseDiagDir(exitdir) != m_exitdir)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   147
				return false;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   148
		}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   149
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   150
		// road and rail depots can also be entered from one direction only
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   151
		if (IsRoadTT() && IsTileDepotType(m_new_tile, TT())) {
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   152
			DiagDirection exitdir = GetRoadDepotDirection(m_new_tile);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   153
			if (ReverseDiagDir(exitdir) != m_exitdir)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   154
				return false;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   155
		}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   156
		if (IsRailTT() && IsTileDepotType(m_new_tile, TT())) {
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   157
			DiagDirection exitdir = GetRailDepotDirection(m_new_tile);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   158
			if (ReverseDiagDir(exitdir) != m_exitdir)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   159
				return false;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   160
		}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   161
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   162
		// rail transport is possible only on tiles with the same owner as vehicle
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   163
		if (IsRailTT() && GetTileOwner(m_new_tile) != m_veh->owner) {
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   164
			// different owner
3933
231ae3c419f4 (svn r5070) Merged the bridge branch
celestar
parents: 3931
diff changeset
   165
			return false;
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   166
		}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   167
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   168
		// rail transport is possible only on compatible rail types
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   169
		if (IsRailTT()) {
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   170
			RailType rail_type = GetTileRailType(m_new_tile, DiagdirToDiagTrackdir(m_exitdir));
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   171
			if (((1 << rail_type) & m_veh->u.rail.compatible_railtypes) == 0) {
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   172
				// incompatible rail type
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   173
				return false;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   174
			}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   175
		}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   176
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   177
		// tunnel tiles can be entered only from proper direction
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   178
		if (!IsWaterTT() && !m_is_tunnel && IsTunnelTile(m_new_tile)) {
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   179
			DiagDirection tunnel_enterdir = GetTunnelDirection(m_new_tile);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   180
			if (tunnel_enterdir != m_exitdir)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   181
				return false;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   182
		}
3931
b946fc6e7188 (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3915
diff changeset
   183
b946fc6e7188 (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3915
diff changeset
   184
		// special handling for rail stations - get to the end of platform
b946fc6e7188 (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3915
diff changeset
   185
		if (IsRailTT() && m_is_station) {
b946fc6e7188 (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3915
diff changeset
   186
			// entered railway station
b946fc6e7188 (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3915
diff changeset
   187
			// get platform length
b946fc6e7188 (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3915
diff changeset
   188
			uint length = GetPlatformLength(m_new_tile, TrackdirToExitdir(m_old_td));
b946fc6e7188 (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3915
diff changeset
   189
			// how big step we must do to get to the last platform tile;
b946fc6e7188 (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3915
diff changeset
   190
			m_tiles_skipped = length - 1;
b946fc6e7188 (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3915
diff changeset
   191
			// move to the platform end
b946fc6e7188 (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3915
diff changeset
   192
			TileIndexDiff diff = TileOffsByDir(m_exitdir);
b946fc6e7188 (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3915
diff changeset
   193
			diff *= m_tiles_skipped;
b946fc6e7188 (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3915
diff changeset
   194
			m_new_tile = TILE_ADD(m_new_tile, diff);
b946fc6e7188 (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3915
diff changeset
   195
			return true;
b946fc6e7188 (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3915
diff changeset
   196
		}
b946fc6e7188 (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3915
diff changeset
   197
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   198
		return true;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   199
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   200
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   201
	FORCEINLINE bool EnteredRailDepot()
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   202
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   203
		// rail depots cause reversing
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   204
		if (IsRailTT() && IsTileDepotType(m_old_tile, TT())) {
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   205
			DiagDirection exitdir = GetRailDepotDirection(m_old_tile);
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   206
			if (exitdir != m_exitdir) {
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   207
				// reverse
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   208
				m_new_tile = m_old_tile;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   209
				m_new_td_bits = TrackdirToTrackdirBits(ReverseTrackdir(m_old_td));
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   210
				m_exitdir = exitdir;
3931
b946fc6e7188 (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3915
diff changeset
   211
				m_tiles_skipped = 0;
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   212
				m_is_tunnel = false;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   213
				return true;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   214
			}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   215
		}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   216
		return false;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   217
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   218
public:
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   219
	/** Helper for pathfinders - get min/max speed on the m_old_tile/m_old_td */
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   220
	int GetSpeedLimit(int *pmin_speed = NULL)
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   221
	{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   222
		int min_speed = 0;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   223
		int max_speed = INT_MAX; // no limit
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   224
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   225
		// for now we handle only on-bridge speed limit
3933
231ae3c419f4 (svn r5070) Merged the bridge branch
celestar
parents: 3931
diff changeset
   226
		if (!IsWaterTT() && IsBridgeTile(m_old_tile)) {
231ae3c419f4 (svn r5070) Merged the bridge branch
celestar
parents: 3931
diff changeset
   227
			int spd = _bridge[GetBridgeType(m_old_tile)].speed;
231ae3c419f4 (svn r5070) Merged the bridge branch
celestar
parents: 3931
diff changeset
   228
			if (IsRoadTT()) spd *= 2;
231ae3c419f4 (svn r5070) Merged the bridge branch
celestar
parents: 3931
diff changeset
   229
			if (max_speed > spd) max_speed = spd;
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   230
		}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   231
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   232
		// if min speed was requested, return it
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   233
		if (pmin_speed) *pmin_speed = min_speed;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   234
		return max_speed;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   235
	}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   236
};
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   237
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   238
typedef CFollowTrackT<TRANSPORT_WATER, true > CFollowTrackWater;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   239
typedef CFollowTrackT<TRANSPORT_ROAD , true > CFollowTrackRoad;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   240
typedef CFollowTrackT<TRANSPORT_RAIL , true > CFollowTrackRail;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   241
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   242
typedef CFollowTrackT<TRANSPORT_WATER, false> CFollowTrackWaterNo90;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   243
typedef CFollowTrackT<TRANSPORT_ROAD , false> CFollowTrackRoadNo90;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   244
typedef CFollowTrackT<TRANSPORT_RAIL , false> CFollowTrackRailNo90;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   245
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   246
#endif /* FOLLOW_TRACK_HPP */