yapf/yapf_costrail.hpp
author KUDr
Tue, 02 Jan 2007 18:40:37 +0000
branchcustombridgeheads
changeset 5641 d4d00a16ef26
parent 5627 f5c656cf0a0e
permissions -rw-r--r--
(svn r7755) [cbh] - Fix: [NTP] now works with cbh (needs testing)
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     1
/* $Id$ */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     2
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     3
#ifndef  YAPF_COSTRAIL_HPP
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     4
#define  YAPF_COSTRAIL_HPP
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     5
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     6
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     7
template <class Types>
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     8
class CYapfCostRailT
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
     9
	: public CYapfCostBase
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    10
	, public CostRailSettings
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    11
{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    12
public:
3914
f5118020cd84 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
    13
	typedef typename Types::Tpf Tpf;              ///< the pathfinder class (derived from THIS class)
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    14
	typedef typename Types::TrackFollower TrackFollower;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    15
	typedef typename Types::NodeList::Titem Node; ///< this will be our node type
3914
f5118020cd84 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
    16
	typedef typename Node::Key Key;               ///< key to hash tables
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    17
	typedef typename Node::CachedData CachedData;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    18
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    19
protected:
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    20
	int           m_max_cost;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    21
	CBlobT<int>   m_sig_look_ahead_costs;
5623
ef2a8a524a95 (svn r7721) [cbh] - Sync with 7607:7720 from trunk
celestar
parents: 5621
diff changeset
    22
public:
ef2a8a524a95 (svn r7721) [cbh] - Sync with 7607:7720 from trunk
celestar
parents: 5621
diff changeset
    23
	bool          m_stopped_on_first_two_way_signal;
ef2a8a524a95 (svn r7721) [cbh] - Sync with 7607:7720 from trunk
celestar
parents: 5621
diff changeset
    24
protected:
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    25
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    26
	static const int s_max_segment_cost = 10000;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    27
5623
ef2a8a524a95 (svn r7721) [cbh] - Sync with 7607:7720 from trunk
celestar
parents: 5621
diff changeset
    28
	CYapfCostRailT()
ef2a8a524a95 (svn r7721) [cbh] - Sync with 7607:7720 from trunk
celestar
parents: 5621
diff changeset
    29
		: m_max_cost(0)
ef2a8a524a95 (svn r7721) [cbh] - Sync with 7607:7720 from trunk
celestar
parents: 5621
diff changeset
    30
		, m_stopped_on_first_two_way_signal(false)
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    31
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    32
		// pre-compute look-ahead penalties into array
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    33
		int p0 = Yapf().PfGetSettings().rail_look_ahead_signal_p0;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    34
		int p1 = Yapf().PfGetSettings().rail_look_ahead_signal_p1;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    35
		int p2 = Yapf().PfGetSettings().rail_look_ahead_signal_p2;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    36
		int *pen = m_sig_look_ahead_costs.GrowSizeNC(Yapf().PfGetSettings().rail_look_ahead_max_signals);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    37
		for (uint i = 0; i < Yapf().PfGetSettings().rail_look_ahead_max_signals; i++)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    38
			pen[i] = p0 + i * (p1 + i * p2);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    39
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    40
3914
f5118020cd84 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
    41
	/// to access inherited path finder
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    42
	Tpf& Yapf() {return *static_cast<Tpf*>(this);}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    43
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    44
public:
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    45
	FORCEINLINE int SlopeCost(TileIndex tile, Trackdir td)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    46
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    47
		CPerfStart perf_cost(Yapf().m_perf_slope_cost);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    48
		if (!stSlopeCost(tile, td)) return 0;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    49
		return Yapf().PfGetSettings().rail_slope_penalty;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    50
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    51
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    52
	FORCEINLINE int CurveCost(Trackdir td1, Trackdir td2)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    53
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    54
		int cost = 0;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    55
		if (TrackFollower::Allow90degTurns()
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    56
				&& ((TrackdirToTrackdirBits(td2) & (TrackdirBits)TrackdirCrossesTrackdirs(td1)) != 0)) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    57
			// 90-deg curve penalty
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    58
			cost += Yapf().PfGetSettings().rail_curve90_penalty;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    59
		} else if (td2 != NextTrackdir(td1)) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    60
			// 45-deg curve penalty
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    61
			cost += Yapf().PfGetSettings().rail_curve45_penalty;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    62
		}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    63
		return cost;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    64
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    65
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    66
	/** return one tile cost. If tile is a tunnel entry, it is moved to the end of tunnel */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    67
	FORCEINLINE int OneTileCost(TileIndex& tile, Trackdir trackdir)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    68
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    69
		int cost = 0;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    70
		// set base cost
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    71
		if (IsDiagonalTrackdir(trackdir)) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    72
			cost += YAPF_TILE_LENGTH;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    73
			switch (GetTileType(tile)) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    74
				case MP_STREET:
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    75
					/* Increase the cost for level crossings */
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    76
					if (IsLevelCrossing(tile))
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    77
						cost += Yapf().PfGetSettings().rail_crossing_penalty;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    78
					break;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    79
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    80
				case MP_STATION:
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    81
					// penalty for passing station tiles
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    82
					cost += Yapf().PfGetSettings().rail_station_penalty;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    83
					break;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    84
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    85
				default:
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    86
					break;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    87
			}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    88
		} else {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    89
			// non-diagonal trackdir
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    90
			cost = YAPF_TILE_CORNER_LENGTH;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    91
		}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    92
		return cost;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    93
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    94
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    95
	int SignalCost(Node& n, TileIndex tile, Trackdir trackdir)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    96
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    97
		int cost = 0;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    98
		// if there is one-way signal in the opposite direction, then it is not our way
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
    99
		CPerfStart perf_cost(Yapf().m_perf_other_cost);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   100
		if (IsTileType(tile, MP_RAILWAY)) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   101
			bool has_signal_against = HasSignalOnTrackdir(tile, ReverseTrackdir(trackdir));
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   102
			bool has_signal_along = HasSignalOnTrackdir(tile, trackdir);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   103
			if (has_signal_against && !has_signal_along) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   104
				// one-way signal in opposite direction
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   105
				n.m_segment->flags_u.flags_s.m_end_of_line = true;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   106
			} else if (has_signal_along) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   107
				SignalState sig_state = GetSignalStateByTrackdir(tile, trackdir);
5095
fd35de11728c (svn r7164) -Fix: [YAPF] assert when look-ahead polynomial result (red-signal penalty) is negative. Such penalty is now added as positive penalty for the appropriate green signal. (DannyA)
KUDr
parents: 4549
diff changeset
   108
				// cache the look-ahead polynomial constant only if we didn't pass more signals than the look-ahead limit is
fd35de11728c (svn r7164) -Fix: [YAPF] assert when look-ahead polynomial result (red-signal penalty) is negative. Such penalty is now added as positive penalty for the appropriate green signal. (DannyA)
KUDr
parents: 4549
diff changeset
   109
				int look_ahead_cost = (n.m_num_signals_passed < m_sig_look_ahead_costs.Size()) ? m_sig_look_ahead_costs.Data()[n.m_num_signals_passed] : 0;
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   110
				if (sig_state != SIGNAL_STATE_RED) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   111
					// green signal
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   112
					n.flags_u.flags_s.m_last_signal_was_red = false;
5095
fd35de11728c (svn r7164) -Fix: [YAPF] assert when look-ahead polynomial result (red-signal penalty) is negative. Such penalty is now added as positive penalty for the appropriate green signal. (DannyA)
KUDr
parents: 4549
diff changeset
   113
					// negative look-ahead red-signal penalties would cause problems later, so use them as positive penalties for green signal
fd35de11728c (svn r7164) -Fix: [YAPF] assert when look-ahead polynomial result (red-signal penalty) is negative. Such penalty is now added as positive penalty for the appropriate green signal. (DannyA)
KUDr
parents: 4549
diff changeset
   114
					if (look_ahead_cost < 0) {
fd35de11728c (svn r7164) -Fix: [YAPF] assert when look-ahead polynomial result (red-signal penalty) is negative. Such penalty is now added as positive penalty for the appropriate green signal. (DannyA)
KUDr
parents: 4549
diff changeset
   115
						// add its negation to the cost
fd35de11728c (svn r7164) -Fix: [YAPF] assert when look-ahead polynomial result (red-signal penalty) is negative. Such penalty is now added as positive penalty for the appropriate green signal. (DannyA)
KUDr
parents: 4549
diff changeset
   116
						cost -= look_ahead_cost;
fd35de11728c (svn r7164) -Fix: [YAPF] assert when look-ahead polynomial result (red-signal penalty) is negative. Such penalty is now added as positive penalty for the appropriate green signal. (DannyA)
KUDr
parents: 4549
diff changeset
   117
					}
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   118
				} else {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   119
					// we have a red signal in our direction
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   120
					// was it first signal which is two-way?
3979
4a9826b12e56 (svn r5163) - CodeChange: [YAPF] "treat first red two-way signal as dead end" is not applied if train didn't pass choice.
KUDr
parents: 3932
diff changeset
   121
					if (Yapf().TreatFirstRedTwoWaySignalAsEOL() && n.flags_u.flags_s.m_choice_seen && has_signal_against && n.m_num_signals_passed == 0) {
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   122
						// yes, the first signal is two-way red signal => DEAD END
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   123
						n.m_segment->flags_u.flags_s.m_end_of_line = true;
5623
ef2a8a524a95 (svn r7721) [cbh] - Sync with 7607:7720 from trunk
celestar
parents: 5621
diff changeset
   124
						Yapf().m_stopped_on_first_two_way_signal = true;
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   125
						return -1;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   126
					}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   127
					SignalType sig_type = GetSignalType(tile);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   128
					n.m_last_red_signal_type = sig_type;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   129
					n.flags_u.flags_s.m_last_signal_was_red = true;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   130
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   131
					// look-ahead signal penalty
5095
fd35de11728c (svn r7164) -Fix: [YAPF] assert when look-ahead polynomial result (red-signal penalty) is negative. Such penalty is now added as positive penalty for the appropriate green signal. (DannyA)
KUDr
parents: 4549
diff changeset
   132
					if (look_ahead_cost > 0) {
fd35de11728c (svn r7164) -Fix: [YAPF] assert when look-ahead polynomial result (red-signal penalty) is negative. Such penalty is now added as positive penalty for the appropriate green signal. (DannyA)
KUDr
parents: 4549
diff changeset
   133
						// add the look ahead penalty only if it is positive
fd35de11728c (svn r7164) -Fix: [YAPF] assert when look-ahead polynomial result (red-signal penalty) is negative. Such penalty is now added as positive penalty for the appropriate green signal. (DannyA)
KUDr
parents: 4549
diff changeset
   134
						cost += look_ahead_cost;
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   135
					}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   136
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   137
					// special signal penalties
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   138
					if (n.m_num_signals_passed == 0) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   139
						switch (sig_type) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   140
							case SIGTYPE_COMBO:
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   141
							case SIGTYPE_EXIT:   cost += Yapf().PfGetSettings().rail_firstred_exit_penalty; break; // first signal is red pre-signal-exit
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   142
							case SIGTYPE_NORMAL:
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   143
							case SIGTYPE_ENTRY:  cost += Yapf().PfGetSettings().rail_firstred_penalty; break;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   144
						};
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   145
					}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   146
				}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   147
				n.m_num_signals_passed++;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   148
				n.m_segment->m_last_signal_tile = tile;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   149
				n.m_segment->m_last_signal_td = trackdir;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   150
			}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   151
		}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   152
		return cost;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   153
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   154
3931
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   155
	FORCEINLINE int PlatformLengthPenalty(int platform_length)
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   156
	{
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   157
		int cost = 0;
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   158
		const Vehicle* v = Yapf().GetVehicle();
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   159
		assert(v != NULL);
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   160
		assert(v->type == VEH_Train);
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   161
		assert(v->u.rail.cached_total_length != 0);
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   162
		int needed_platform_length = (v->u.rail.cached_total_length + TILE_SIZE - 1) / TILE_SIZE;
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   163
		if (platform_length > needed_platform_length) {
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   164
			// apply penalty for longer platform than needed
3932
882af4997b60 (svn r5067) -Codechange: [YAPF] Platform selection feature now applies penalty per platform instead of per tile (should give better results)
KUDr
parents: 3931
diff changeset
   165
			cost += Yapf().PfGetSettings().rail_longer_platform_penalty;
882af4997b60 (svn r5067) -Codechange: [YAPF] Platform selection feature now applies penalty per platform instead of per tile (should give better results)
KUDr
parents: 3931
diff changeset
   166
		} else if (needed_platform_length > platform_length) {
3931
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   167
			// apply penalty for shorter platform than needed
3932
882af4997b60 (svn r5067) -Codechange: [YAPF] Platform selection feature now applies penalty per platform instead of per tile (should give better results)
KUDr
parents: 3931
diff changeset
   168
			cost += Yapf().PfGetSettings().rail_shorter_platform_penalty;
3931
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   169
		}
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   170
		return cost;
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   171
	}
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   172
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   173
public:
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   174
	FORCEINLINE void SetMaxCost(int max_cost) {m_max_cost = max_cost;}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   175
3914
f5118020cd84 (svn r5018) [YAPF] Added some comments to YAPF implementation.
KUDr
parents: 3900
diff changeset
   176
	/** Called by YAPF to calculate the cost from the origin to the given node.
4549
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 3979
diff changeset
   177
	 *  Calculates only the cost of given node, adds it to the parent node cost
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 3979
diff changeset
   178
	 *  and stores the result into Node::m_cost member */
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   179
	FORCEINLINE bool PfCalcCost(Node& n)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   180
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   181
		assert(!n.flags_u.flags_s.m_targed_seen);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   182
		CPerfStart perf_cost(Yapf().m_perf_cost);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   183
		int parent_cost = (n.m_parent != NULL) ? n.m_parent->m_cost : 0;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   184
		int first_tile_cost = 0;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   185
		int segment_cost = 0;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   186
		int extra_cost = 0;
3915
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3914
diff changeset
   187
		const Vehicle* v = Yapf().GetVehicle();
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   188
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   189
		// start at n.m_key.m_tile / n.m_key.m_td and walk to the end of segment
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   190
		TileIndex prev_tile      = (n.m_parent != NULL) ? n.m_parent->GetLastTile() : INVALID_TILE;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   191
		Trackdir  prev_trackdir  = (n.m_parent != NULL) ? n.m_parent->GetLastTrackdir() : INVALID_TRACKDIR;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   192
		TileType  prev_tile_type = (n.m_parent != NULL) ? GetTileType(n.m_parent->GetLastTile()) : MP_VOID;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   193
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   194
		TileIndex tile = n.m_key.m_tile;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   195
		Trackdir trackdir = n.m_key.m_td;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   196
		TileType tile_type = GetTileType(tile);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   197
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   198
		DEBUG(yapf, 3, "PfCalcCost(Node:tile=%04X td=%s; Parent:tile=%04X td=%s)", tile, GetTrackdirName(trackdir), prev_tile, GetTrackdirName(prev_trackdir));
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   199
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   200
		RailType rail_type = GetTileRailType(tile, trackdir);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   201
5609
ec38986d2c8e (svn r7655) [cbh] - Fix: [YAPF] another assert (on opposite cbh when it contained choice). Now it is possible to reach choice when exiting wormhole. So the wormhole cost must be taken into consideration when starting new YAPF node.
KUDr
parents: 5095
diff changeset
   202
		// detect exit from bridge wormhole
5627
f5c656cf0a0e (svn r7729) [cbh] - Fix: [YAPF] several cbh related issues. YAPF should now work with cbh
KUDr
parents: 5626
diff changeset
   203
		Trackdir intermediate_trackdir = INVALID_TRACKDIR;
5609
ec38986d2c8e (svn r7655) [cbh] - Fix: [YAPF] another assert (on opposite cbh when it contained choice). Now it is possible to reach choice when exiting wormhole. So the wormhole cost must be taken into consideration when starting new YAPF node.
KUDr
parents: 5095
diff changeset
   204
		if (IsBridgeTile(tile) && TrackdirToExitdir(ReverseTrackdir(trackdir)) == GetBridgeRampDirection(tile)) {
ec38986d2c8e (svn r7655) [cbh] - Fix: [YAPF] another assert (on opposite cbh when it contained choice). Now it is possible to reach choice when exiting wormhole. So the wormhole cost must be taken into consideration when starting new YAPF node.
KUDr
parents: 5095
diff changeset
   205
			// we are jumping over bridge (possible now with custom bridge heads) we must add the cost of skipped tiles
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   206
			int skipped_tiles = DistanceManhattan(prev_tile, tile) - 1;
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   207
			if (skipped_tiles > 0) {
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   208
				segment_cost += skipped_tiles * YAPF_TILE_LENGTH;
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   209
				DEBUG(yapf, 6, "    Cost: skipped=%d", skipped_tiles * YAPF_TILE_LENGTH);
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   210
				intermediate_trackdir = DiagdirToDiagTrackdir(ReverseDiagDir(GetBridgeRampDirection(tile)));
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   211
			}
5609
ec38986d2c8e (svn r7655) [cbh] - Fix: [YAPF] another assert (on opposite cbh when it contained choice). Now it is possible to reach choice when exiting wormhole. So the wormhole cost must be taken into consideration when starting new YAPF node.
KUDr
parents: 5095
diff changeset
   212
		}
ec38986d2c8e (svn r7655) [cbh] - Fix: [YAPF] another assert (on opposite cbh when it contained choice). Now it is possible to reach choice when exiting wormhole. So the wormhole cost must be taken into consideration when starting new YAPF node.
KUDr
parents: 5095
diff changeset
   213
3930
635b28d85743 (svn r5065) -CodeChange: [YAPF] Added PfDetectDestination(tile, trackdir) for trains (to be used by platform selection feature)
KUDr
parents: 3915
diff changeset
   214
		bool target_seen = Yapf().PfDetectDestination(tile, trackdir);
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   215
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   216
		while (true) {
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   217
			int tile_cost = Yapf().OneTileCost(tile, trackdir);
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   218
			int curve_cost;
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   219
			if (intermediate_trackdir == INVALID_TRACKDIR) {
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   220
				curve_cost = Yapf().CurveCost(prev_trackdir, trackdir);
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   221
			} else {
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   222
				curve_cost = Yapf().CurveCost(prev_trackdir, intermediate_trackdir) + Yapf().CurveCost(intermediate_trackdir, trackdir);
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   223
				intermediate_trackdir = INVALID_TRACKDIR;
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   224
			}
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   225
			int slope_cost = Yapf().SlopeCost(tile, trackdir);
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   226
			int signal_cost = Yapf().SignalCost(n, tile, trackdir);
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   227
			DEBUG(yapf, 6, "    Cost: tile=%d, curve=%d, slope=%d, signal=%d", tile_cost, curve_cost, slope_cost, signal_cost);
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   228
			segment_cost += tile_cost + curve_cost + slope_cost + signal_cost;
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   229
			if (n.m_segment->flags_u.flags_s.m_end_of_line) {
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   230
				DEBUG(yapf, 4, "  end: EOL (signal)");
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   231
				break;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   232
			}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   233
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   234
			// finish if we have reached the destination
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   235
			if (target_seen) {
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   236
				DEBUG(yapf, 4, "  end: target_seen");
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   237
				break;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   238
			}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   239
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   240
			// finish on first station tile - segment should end here to avoid target skipping
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   241
			// when cached segments are used
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   242
			if (tile_type == MP_STATION && prev_tile_type != MP_STATION) {
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   243
				DEBUG(yapf, 4, "  end: first station tile");
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   244
				break;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   245
			}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   246
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   247
			// finish also on waypoint - same workaround as for first station tile
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   248
			if (tile_type == MP_RAILWAY && IsRailWaypoint(tile)) {
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   249
				DEBUG(yapf, 4, "  end: waypoint");
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   250
				break;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   251
			}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   252
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   253
			// if there are no reachable trackdirs on the next tile, we have end of road
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   254
			TrackFollower F(v, &Yapf().m_perf_ts_cost);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   255
			if (!F.Follow(tile, trackdir)) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   256
				// we can't continue?
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   257
				// n.m_segment->flags_u.flags_s.m_end_of_line = true;
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   258
				DEBUG(yapf, 4, "  end: can't foolow (dead end?)");
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   259
				break;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   260
			}
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   261
			if (F.m_is_bridge && F.m_tiles_skipped > 0) intermediate_trackdir = F.m_intermediate_trackdir;
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   262
			DEBUG(yapf, 4, " next: tile=%04X td=%s", F.m_new_tile, GetTrackdirBitsName(F.m_new_td_bits).Data());
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   263
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   264
			// if there are more trackdirs available & reachable, we are at the end of segment
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   265
			if (KillFirstBit2x64(F.m_new_td_bits) != 0) {
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   266
				DEBUG(yapf, 4, "  end: choice");
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   267
				break;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   268
			}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   269
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   270
			Trackdir new_td = (Trackdir)FindFirstBit2x64(F.m_new_td_bits);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   271
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   272
			{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   273
				// end segment if train is about to enter simple loop with no junctions
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   274
				// so next time it should stop on the next if
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   275
				if (segment_cost > s_max_segment_cost && IsTileType(F.m_new_tile, MP_RAILWAY)) {
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   276
					DEBUG(yapf, 4, "  end: loop fuse");
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   277
					break;
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   278
				}
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   279
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   280
				// stop if train is on simple loop with no junctions
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   281
				if (F.m_new_tile == n.m_key.m_tile && new_td == n.m_key.m_td) {
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   282
					DEBUG(yapf, 4, "  end: loop detected");
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   283
					return false;
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   284
				}
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   285
			}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   286
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   287
			// if rail type changes, finish segment (cached segment can't contain more rail types)
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   288
			{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   289
				RailType new_rail_type = GetTileRailType(F.m_new_tile, (Trackdir)FindFirstBit2x64(F.m_new_td_bits));
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   290
				if (new_rail_type != rail_type) {
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   291
					DEBUG(yapf, 4, "  end: rail type changes");
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   292
					break;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   293
				}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   294
				rail_type = new_rail_type;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   295
			}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   296
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   297
			// move to the next tile
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   298
			prev_tile = tile;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   299
			prev_trackdir = trackdir;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   300
			prev_tile_type = tile_type;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   301
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   302
			tile = F.m_new_tile;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   303
			trackdir = new_td;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   304
			tile_type = GetTileType(tile);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   305
3930
635b28d85743 (svn r5065) -CodeChange: [YAPF] Added PfDetectDestination(tile, trackdir) for trains (to be used by platform selection feature)
KUDr
parents: 3915
diff changeset
   306
			target_seen = Yapf().PfDetectDestination(tile, trackdir);
635b28d85743 (svn r5065) -CodeChange: [YAPF] Added PfDetectDestination(tile, trackdir) for trains (to be used by platform selection feature)
KUDr
parents: 3915
diff changeset
   307
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   308
			// reversing in depot penalty
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   309
			if (tile == prev_tile) {
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   310
				int reverse_in_depot_cost = Yapf().PfGetSettings().rail_depot_reverse_penalty;
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   311
				segment_cost += reverse_in_depot_cost;
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   312
				DEBUG(yapf, 4, "  end: reversing in depot (cost=%d)", reverse_in_depot_cost);
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   313
				break;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   314
			}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   315
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   316
			// if we skipped some tunnel tiles, add their cost
3931
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   317
			segment_cost += YAPF_TILE_LENGTH * F.m_tiles_skipped;
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   318
			DEBUG(yapf, 6, "    Cost: wormhole=%d", YAPF_TILE_LENGTH * F.m_tiles_skipped);
3931
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   319
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   320
			// add penalty for skipped station tiles
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   321
			if (F.m_is_station)
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   322
			{
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   323
				if (target_seen) {
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   324
					// it is our destination station
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   325
					uint platform_length = F.m_tiles_skipped + 1;
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   326
					int platform_length_cost = PlatformLengthPenalty(platform_length);
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   327
					segment_cost += platform_length_cost;
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   328
					DEBUG(yapf, 6, "    Cost: platform=%d", platform_length_cost);
3931
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   329
				} else {
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   330
					// station is not our destination station, apply penalty for skipped platform tiles
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   331
					int station_cost = Yapf().PfGetSettings().rail_station_penalty * F.m_tiles_skipped;
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   332
					segment_cost += station_cost;
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   333
					DEBUG(yapf, 6, "    Cost: station=%d", station_cost);
3931
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   334
				}
ca6abd14504a (svn r5066) -Feature: [YAPF] Train selects the best station platform by length
KUDr
parents: 3930
diff changeset
   335
			}
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   336
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   337
			// add min/max speed penalties
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   338
			int min_speed = 0;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   339
			int max_speed = F.GetSpeedLimit(&min_speed);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   340
			if (max_speed < v->max_speed)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   341
				segment_cost += YAPF_TILE_LENGTH * (v->max_speed - max_speed) / v->max_speed;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   342
			if (min_speed > v->max_speed)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   343
				segment_cost += YAPF_TILE_LENGTH * (min_speed - v->max_speed);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   344
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   345
			// finish if we already exceeded the maximum cost
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   346
			if (m_max_cost > 0 && (parent_cost + first_tile_cost + segment_cost) > m_max_cost) {
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   347
				DEBUG(yapf, 4, "  abort: maximum cost reached");
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   348
				return false;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   349
			}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   350
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   351
			if (first_tile_cost == 0) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   352
				// we just have done first tile
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   353
				first_tile_cost = segment_cost;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   354
				segment_cost = 0;
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   355
				DEBUG(yapf, 5, "    TotalCost: first_tile=%d", first_tile_cost);
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   356
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   357
				// look if we can reuse existing (cached) segment cost
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   358
				if (n.m_segment->m_cost >= 0) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   359
					// reuse the cached segment cost
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   360
					DEBUG(yapf, 4, "  end: reusing the cached segment cost");
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   361
					break;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   362
				}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   363
			}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   364
			// segment cost was not filled yes, we have not cached it yet
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   365
			n.SetLastTileTrackdir(tile, trackdir);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   366
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   367
		} // while (true)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   368
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   369
		if (first_tile_cost == 0) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   370
			// we have just finished first tile
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   371
			first_tile_cost = segment_cost;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   372
			segment_cost = 0;
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   373
			DEBUG(yapf, 5, "    TotalCost: first_tile=%d", first_tile_cost);
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   374
		}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   375
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   376
		// do we have cached segment cost?
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   377
		if (n.m_segment->m_cost >= 0) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   378
			// reuse the cached segment cost
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   379
			segment_cost = n.m_segment->m_cost;
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   380
			DEBUG(yapf, 5, "    TotalCost: cached_segment=%d", segment_cost);
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   381
		} else {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   382
			// save segment cost
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   383
			n.m_segment->m_cost = segment_cost;
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   384
			DEBUG(yapf, 5, "    TotalCost: segment=%d", segment_cost);
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   385
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   386
			// save end of segment back to the node
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   387
			n.SetLastTileTrackdir(tile, trackdir);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   388
		}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   389
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   390
		// special costs for the case we have reached our target
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   391
		if (target_seen) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   392
			n.flags_u.flags_s.m_targed_seen = true;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   393
			if (n.flags_u.flags_s.m_last_signal_was_red) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   394
				if (n.m_last_red_signal_type == SIGTYPE_EXIT) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   395
					// last signal was red pre-signal-exit
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   396
					extra_cost += Yapf().PfGetSettings().rail_lastred_exit_penalty;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   397
				} else {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   398
					// last signal was red, but not exit
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   399
					extra_cost += Yapf().PfGetSettings().rail_lastred_penalty;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   400
				}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   401
			}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   402
		}
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   403
		DEBUG(yapf, 5, "    TotalCost: extra=%d", extra_cost);
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   404
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   405
		// total node cost
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   406
		n.m_cost = parent_cost + first_tile_cost + segment_cost + extra_cost;
5626
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   407
		DEBUG(yapf, 3, "  leaving: last_tile=%04X, last_td=%s, cost=%d", n.m_segment->m_last_tile, GetTrackdirName(n.m_segment->m_last_td), n.m_cost);
1811beeb472f (svn r7728) -Codechange: [YAPF] added some YAPF debug messages plus 2 new files (string class)
KUDr
parents: 5623
diff changeset
   408
		DEBUG(yapf, 3, " returning %s", n.m_segment->flags_u.flags_s.m_end_of_line ? "false" : "true");
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   409
		return !n.m_segment->flags_u.flags_s.m_end_of_line;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   410
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   411
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   412
	FORCEINLINE bool CanUseGlobalCache(Node& n) const
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   413
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   414
		return (n.m_parent != NULL)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   415
			&& (n.m_parent->m_num_signals_passed >= m_sig_look_ahead_costs.Size());
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   416
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   417
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   418
	FORCEINLINE void ConnectNodeToCachedData(Node& n, CachedData& ci)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   419
	{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   420
		n.m_segment = &ci;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   421
		if (n.m_segment->m_cost < 0) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   422
			n.m_segment->m_last_tile = n.m_key.m_tile;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   423
			n.m_segment->m_last_td = n.m_key.m_td;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   424
		}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   425
	}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   426
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   427
};
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   428
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   429
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   430
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
diff changeset
   431
#endif /* YAPF_COSTRAIL_HPP */